fix_node.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/time.h>
  5. #include <linux/slab.h>
  6. #include <linux/string.h>
  7. #include "reiserfs.h"
  8. #include <linux/buffer_head.h>
  9. /*
  10. * To make any changes in the tree we find a node that contains item
  11. * to be changed/deleted or position in the node we insert a new item
  12. * to. We call this node S. To do balancing we need to decide what we
  13. * will shift to left/right neighbor, or to a new node, where new item
  14. * will be etc. To make this analysis simpler we build virtual
  15. * node. Virtual node is an array of items, that will replace items of
  16. * node S. (For instance if we are going to delete an item, virtual
  17. * node does not contain it). Virtual node keeps information about
  18. * item sizes and types, mergeability of first and last items, sizes
  19. * of all entries in directory item. We use this array of items when
  20. * calculating what we can shift to neighbors and how many nodes we
  21. * have to have if we do not any shiftings, if we shift to left/right
  22. * neighbor or to both.
  23. */
  24. /*
  25. * Takes item number in virtual node, returns number of item
  26. * that it has in source buffer
  27. */
  28. static inline int old_item_num(int new_num, int affected_item_num, int mode)
  29. {
  30. if (mode == M_PASTE || mode == M_CUT || new_num < affected_item_num)
  31. return new_num;
  32. if (mode == M_INSERT) {
  33. RFALSE(new_num == 0,
  34. "vs-8005: for INSERT mode and item number of inserted item");
  35. return new_num - 1;
  36. }
  37. RFALSE(mode != M_DELETE,
  38. "vs-8010: old_item_num: mode must be M_DELETE (mode = \'%c\'",
  39. mode);
  40. /* delete mode */
  41. return new_num + 1;
  42. }
  43. static void create_virtual_node(struct tree_balance *tb, int h)
  44. {
  45. struct item_head *ih;
  46. struct virtual_node *vn = tb->tb_vn;
  47. int new_num;
  48. struct buffer_head *Sh; /* this comes from tb->S[h] */
  49. Sh = PATH_H_PBUFFER(tb->tb_path, h);
  50. /* size of changed node */
  51. vn->vn_size =
  52. MAX_CHILD_SIZE(Sh) - B_FREE_SPACE(Sh) + tb->insert_size[h];
  53. /* for internal nodes array if virtual items is not created */
  54. if (h) {
  55. vn->vn_nr_item = (vn->vn_size - DC_SIZE) / (DC_SIZE + KEY_SIZE);
  56. return;
  57. }
  58. /* number of items in virtual node */
  59. vn->vn_nr_item =
  60. B_NR_ITEMS(Sh) + ((vn->vn_mode == M_INSERT) ? 1 : 0) -
  61. ((vn->vn_mode == M_DELETE) ? 1 : 0);
  62. /* first virtual item */
  63. vn->vn_vi = (struct virtual_item *)(tb->tb_vn + 1);
  64. memset(vn->vn_vi, 0, vn->vn_nr_item * sizeof(struct virtual_item));
  65. vn->vn_free_ptr += vn->vn_nr_item * sizeof(struct virtual_item);
  66. /* first item in the node */
  67. ih = item_head(Sh, 0);
  68. /* define the mergeability for 0-th item (if it is not being deleted) */
  69. if (op_is_left_mergeable(&ih->ih_key, Sh->b_size)
  70. && (vn->vn_mode != M_DELETE || vn->vn_affected_item_num))
  71. vn->vn_vi[0].vi_type |= VI_TYPE_LEFT_MERGEABLE;
  72. /*
  73. * go through all items that remain in the virtual
  74. * node (except for the new (inserted) one)
  75. */
  76. for (new_num = 0; new_num < vn->vn_nr_item; new_num++) {
  77. int j;
  78. struct virtual_item *vi = vn->vn_vi + new_num;
  79. int is_affected =
  80. ((new_num != vn->vn_affected_item_num) ? 0 : 1);
  81. if (is_affected && vn->vn_mode == M_INSERT)
  82. continue;
  83. /* get item number in source node */
  84. j = old_item_num(new_num, vn->vn_affected_item_num,
  85. vn->vn_mode);
  86. vi->vi_item_len += ih_item_len(ih + j) + IH_SIZE;
  87. vi->vi_ih = ih + j;
  88. vi->vi_item = ih_item_body(Sh, ih + j);
  89. vi->vi_uarea = vn->vn_free_ptr;
  90. /*
  91. * FIXME: there is no check that item operation did not
  92. * consume too much memory
  93. */
  94. vn->vn_free_ptr +=
  95. op_create_vi(vn, vi, is_affected, tb->insert_size[0]);
  96. if (tb->vn_buf + tb->vn_buf_size < vn->vn_free_ptr)
  97. reiserfs_panic(tb->tb_sb, "vs-8030",
  98. "virtual node space consumed");
  99. if (!is_affected)
  100. /* this is not being changed */
  101. continue;
  102. if (vn->vn_mode == M_PASTE || vn->vn_mode == M_CUT) {
  103. vn->vn_vi[new_num].vi_item_len += tb->insert_size[0];
  104. /* pointer to data which is going to be pasted */
  105. vi->vi_new_data = vn->vn_data;
  106. }
  107. }
  108. /* virtual inserted item is not defined yet */
  109. if (vn->vn_mode == M_INSERT) {
  110. struct virtual_item *vi = vn->vn_vi + vn->vn_affected_item_num;
  111. RFALSE(vn->vn_ins_ih == NULL,
  112. "vs-8040: item header of inserted item is not specified");
  113. vi->vi_item_len = tb->insert_size[0];
  114. vi->vi_ih = vn->vn_ins_ih;
  115. vi->vi_item = vn->vn_data;
  116. vi->vi_uarea = vn->vn_free_ptr;
  117. op_create_vi(vn, vi, 0 /*not pasted or cut */ ,
  118. tb->insert_size[0]);
  119. }
  120. /*
  121. * set right merge flag we take right delimiting key and
  122. * check whether it is a mergeable item
  123. */
  124. if (tb->CFR[0]) {
  125. struct reiserfs_key *key;
  126. key = internal_key(tb->CFR[0], tb->rkey[0]);
  127. if (op_is_left_mergeable(key, Sh->b_size)
  128. && (vn->vn_mode != M_DELETE
  129. || vn->vn_affected_item_num != B_NR_ITEMS(Sh) - 1))
  130. vn->vn_vi[vn->vn_nr_item - 1].vi_type |=
  131. VI_TYPE_RIGHT_MERGEABLE;
  132. #ifdef CONFIG_REISERFS_CHECK
  133. if (op_is_left_mergeable(key, Sh->b_size) &&
  134. !(vn->vn_mode != M_DELETE
  135. || vn->vn_affected_item_num != B_NR_ITEMS(Sh) - 1)) {
  136. /*
  137. * we delete last item and it could be merged
  138. * with right neighbor's first item
  139. */
  140. if (!
  141. (B_NR_ITEMS(Sh) == 1
  142. && is_direntry_le_ih(item_head(Sh, 0))
  143. && ih_entry_count(item_head(Sh, 0)) == 1)) {
  144. /*
  145. * node contains more than 1 item, or item
  146. * is not directory item, or this item
  147. * contains more than 1 entry
  148. */
  149. print_block(Sh, 0, -1, -1);
  150. reiserfs_panic(tb->tb_sb, "vs-8045",
  151. "rdkey %k, affected item==%d "
  152. "(mode==%c) Must be %c",
  153. key, vn->vn_affected_item_num,
  154. vn->vn_mode, M_DELETE);
  155. }
  156. }
  157. #endif
  158. }
  159. }
  160. /*
  161. * Using virtual node check, how many items can be
  162. * shifted to left neighbor
  163. */
  164. static void check_left(struct tree_balance *tb, int h, int cur_free)
  165. {
  166. int i;
  167. struct virtual_node *vn = tb->tb_vn;
  168. struct virtual_item *vi;
  169. int d_size, ih_size;
  170. RFALSE(cur_free < 0, "vs-8050: cur_free (%d) < 0", cur_free);
  171. /* internal level */
  172. if (h > 0) {
  173. tb->lnum[h] = cur_free / (DC_SIZE + KEY_SIZE);
  174. return;
  175. }
  176. /* leaf level */
  177. if (!cur_free || !vn->vn_nr_item) {
  178. /* no free space or nothing to move */
  179. tb->lnum[h] = 0;
  180. tb->lbytes = -1;
  181. return;
  182. }
  183. RFALSE(!PATH_H_PPARENT(tb->tb_path, 0),
  184. "vs-8055: parent does not exist or invalid");
  185. vi = vn->vn_vi;
  186. if ((unsigned int)cur_free >=
  187. (vn->vn_size -
  188. ((vi->vi_type & VI_TYPE_LEFT_MERGEABLE) ? IH_SIZE : 0))) {
  189. /* all contents of S[0] fits into L[0] */
  190. RFALSE(vn->vn_mode == M_INSERT || vn->vn_mode == M_PASTE,
  191. "vs-8055: invalid mode or balance condition failed");
  192. tb->lnum[0] = vn->vn_nr_item;
  193. tb->lbytes = -1;
  194. return;
  195. }
  196. d_size = 0, ih_size = IH_SIZE;
  197. /* first item may be merge with last item in left neighbor */
  198. if (vi->vi_type & VI_TYPE_LEFT_MERGEABLE)
  199. d_size = -((int)IH_SIZE), ih_size = 0;
  200. tb->lnum[0] = 0;
  201. for (i = 0; i < vn->vn_nr_item;
  202. i++, ih_size = IH_SIZE, d_size = 0, vi++) {
  203. d_size += vi->vi_item_len;
  204. if (cur_free >= d_size) {
  205. /* the item can be shifted entirely */
  206. cur_free -= d_size;
  207. tb->lnum[0]++;
  208. continue;
  209. }
  210. /* the item cannot be shifted entirely, try to split it */
  211. /*
  212. * check whether L[0] can hold ih and at least one byte
  213. * of the item body
  214. */
  215. /* cannot shift even a part of the current item */
  216. if (cur_free <= ih_size) {
  217. tb->lbytes = -1;
  218. return;
  219. }
  220. cur_free -= ih_size;
  221. tb->lbytes = op_check_left(vi, cur_free, 0, 0);
  222. if (tb->lbytes != -1)
  223. /* count partially shifted item */
  224. tb->lnum[0]++;
  225. break;
  226. }
  227. return;
  228. }
  229. /*
  230. * Using virtual node check, how many items can be
  231. * shifted to right neighbor
  232. */
  233. static void check_right(struct tree_balance *tb, int h, int cur_free)
  234. {
  235. int i;
  236. struct virtual_node *vn = tb->tb_vn;
  237. struct virtual_item *vi;
  238. int d_size, ih_size;
  239. RFALSE(cur_free < 0, "vs-8070: cur_free < 0");
  240. /* internal level */
  241. if (h > 0) {
  242. tb->rnum[h] = cur_free / (DC_SIZE + KEY_SIZE);
  243. return;
  244. }
  245. /* leaf level */
  246. if (!cur_free || !vn->vn_nr_item) {
  247. /* no free space */
  248. tb->rnum[h] = 0;
  249. tb->rbytes = -1;
  250. return;
  251. }
  252. RFALSE(!PATH_H_PPARENT(tb->tb_path, 0),
  253. "vs-8075: parent does not exist or invalid");
  254. vi = vn->vn_vi + vn->vn_nr_item - 1;
  255. if ((unsigned int)cur_free >=
  256. (vn->vn_size -
  257. ((vi->vi_type & VI_TYPE_RIGHT_MERGEABLE) ? IH_SIZE : 0))) {
  258. /* all contents of S[0] fits into R[0] */
  259. RFALSE(vn->vn_mode == M_INSERT || vn->vn_mode == M_PASTE,
  260. "vs-8080: invalid mode or balance condition failed");
  261. tb->rnum[h] = vn->vn_nr_item;
  262. tb->rbytes = -1;
  263. return;
  264. }
  265. d_size = 0, ih_size = IH_SIZE;
  266. /* last item may be merge with first item in right neighbor */
  267. if (vi->vi_type & VI_TYPE_RIGHT_MERGEABLE)
  268. d_size = -(int)IH_SIZE, ih_size = 0;
  269. tb->rnum[0] = 0;
  270. for (i = vn->vn_nr_item - 1; i >= 0;
  271. i--, d_size = 0, ih_size = IH_SIZE, vi--) {
  272. d_size += vi->vi_item_len;
  273. if (cur_free >= d_size) {
  274. /* the item can be shifted entirely */
  275. cur_free -= d_size;
  276. tb->rnum[0]++;
  277. continue;
  278. }
  279. /*
  280. * check whether R[0] can hold ih and at least one
  281. * byte of the item body
  282. */
  283. /* cannot shift even a part of the current item */
  284. if (cur_free <= ih_size) {
  285. tb->rbytes = -1;
  286. return;
  287. }
  288. /*
  289. * R[0] can hold the header of the item and at least
  290. * one byte of its body
  291. */
  292. cur_free -= ih_size; /* cur_free is still > 0 */
  293. tb->rbytes = op_check_right(vi, cur_free);
  294. if (tb->rbytes != -1)
  295. /* count partially shifted item */
  296. tb->rnum[0]++;
  297. break;
  298. }
  299. return;
  300. }
  301. /*
  302. * from - number of items, which are shifted to left neighbor entirely
  303. * to - number of item, which are shifted to right neighbor entirely
  304. * from_bytes - number of bytes of boundary item (or directory entries)
  305. * which are shifted to left neighbor
  306. * to_bytes - number of bytes of boundary item (or directory entries)
  307. * which are shifted to right neighbor
  308. */
  309. static int get_num_ver(int mode, struct tree_balance *tb, int h,
  310. int from, int from_bytes,
  311. int to, int to_bytes, short *snum012, int flow)
  312. {
  313. int i;
  314. int cur_free;
  315. int units;
  316. struct virtual_node *vn = tb->tb_vn;
  317. int total_node_size, max_node_size, current_item_size;
  318. int needed_nodes;
  319. /* position of item we start filling node from */
  320. int start_item;
  321. /* position of item we finish filling node by */
  322. int end_item;
  323. /*
  324. * number of first bytes (entries for directory) of start_item-th item
  325. * we do not include into node that is being filled
  326. */
  327. int start_bytes;
  328. /*
  329. * number of last bytes (entries for directory) of end_item-th item
  330. * we do node include into node that is being filled
  331. */
  332. int end_bytes;
  333. /*
  334. * these are positions in virtual item of items, that are split
  335. * between S[0] and S1new and S1new and S2new
  336. */
  337. int split_item_positions[2];
  338. split_item_positions[0] = -1;
  339. split_item_positions[1] = -1;
  340. /*
  341. * We only create additional nodes if we are in insert or paste mode
  342. * or we are in replace mode at the internal level. If h is 0 and
  343. * the mode is M_REPLACE then in fix_nodes we change the mode to
  344. * paste or insert before we get here in the code.
  345. */
  346. RFALSE(tb->insert_size[h] < 0 || (mode != M_INSERT && mode != M_PASTE),
  347. "vs-8100: insert_size < 0 in overflow");
  348. max_node_size = MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, h));
  349. /*
  350. * snum012 [0-2] - number of items, that lay
  351. * to S[0], first new node and second new node
  352. */
  353. snum012[3] = -1; /* s1bytes */
  354. snum012[4] = -1; /* s2bytes */
  355. /* internal level */
  356. if (h > 0) {
  357. i = ((to - from) * (KEY_SIZE + DC_SIZE) + DC_SIZE);
  358. if (i == max_node_size)
  359. return 1;
  360. return (i / max_node_size + 1);
  361. }
  362. /* leaf level */
  363. needed_nodes = 1;
  364. total_node_size = 0;
  365. cur_free = max_node_size;
  366. /* start from 'from'-th item */
  367. start_item = from;
  368. /* skip its first 'start_bytes' units */
  369. start_bytes = ((from_bytes != -1) ? from_bytes : 0);
  370. /* last included item is the 'end_item'-th one */
  371. end_item = vn->vn_nr_item - to - 1;
  372. /* do not count last 'end_bytes' units of 'end_item'-th item */
  373. end_bytes = (to_bytes != -1) ? to_bytes : 0;
  374. /*
  375. * go through all item beginning from the start_item-th item
  376. * and ending by the end_item-th item. Do not count first
  377. * 'start_bytes' units of 'start_item'-th item and last
  378. * 'end_bytes' of 'end_item'-th item
  379. */
  380. for (i = start_item; i <= end_item; i++) {
  381. struct virtual_item *vi = vn->vn_vi + i;
  382. int skip_from_end = ((i == end_item) ? end_bytes : 0);
  383. RFALSE(needed_nodes > 3, "vs-8105: too many nodes are needed");
  384. /* get size of current item */
  385. current_item_size = vi->vi_item_len;
  386. /*
  387. * do not take in calculation head part (from_bytes)
  388. * of from-th item
  389. */
  390. current_item_size -=
  391. op_part_size(vi, 0 /*from start */ , start_bytes);
  392. /* do not take in calculation tail part of last item */
  393. current_item_size -=
  394. op_part_size(vi, 1 /*from end */ , skip_from_end);
  395. /* if item fits into current node entierly */
  396. if (total_node_size + current_item_size <= max_node_size) {
  397. snum012[needed_nodes - 1]++;
  398. total_node_size += current_item_size;
  399. start_bytes = 0;
  400. continue;
  401. }
  402. /*
  403. * virtual item length is longer, than max size of item in
  404. * a node. It is impossible for direct item
  405. */
  406. if (current_item_size > max_node_size) {
  407. RFALSE(is_direct_le_ih(vi->vi_ih),
  408. "vs-8110: "
  409. "direct item length is %d. It can not be longer than %d",
  410. current_item_size, max_node_size);
  411. /* we will try to split it */
  412. flow = 1;
  413. }
  414. /* as we do not split items, take new node and continue */
  415. if (!flow) {
  416. needed_nodes++;
  417. i--;
  418. total_node_size = 0;
  419. continue;
  420. }
  421. /*
  422. * calculate number of item units which fit into node being
  423. * filled
  424. */
  425. {
  426. int free_space;
  427. free_space = max_node_size - total_node_size - IH_SIZE;
  428. units =
  429. op_check_left(vi, free_space, start_bytes,
  430. skip_from_end);
  431. /*
  432. * nothing fits into current node, take new
  433. * node and continue
  434. */
  435. if (units == -1) {
  436. needed_nodes++, i--, total_node_size = 0;
  437. continue;
  438. }
  439. }
  440. /* something fits into the current node */
  441. start_bytes += units;
  442. snum012[needed_nodes - 1 + 3] = units;
  443. if (needed_nodes > 2)
  444. reiserfs_warning(tb->tb_sb, "vs-8111",
  445. "split_item_position is out of range");
  446. snum012[needed_nodes - 1]++;
  447. split_item_positions[needed_nodes - 1] = i;
  448. needed_nodes++;
  449. /* continue from the same item with start_bytes != -1 */
  450. start_item = i;
  451. i--;
  452. total_node_size = 0;
  453. }
  454. /*
  455. * sum012[4] (if it is not -1) contains number of units of which
  456. * are to be in S1new, snum012[3] - to be in S0. They are supposed
  457. * to be S1bytes and S2bytes correspondingly, so recalculate
  458. */
  459. if (snum012[4] > 0) {
  460. int split_item_num;
  461. int bytes_to_r, bytes_to_l;
  462. int bytes_to_S1new;
  463. split_item_num = split_item_positions[1];
  464. bytes_to_l =
  465. ((from == split_item_num
  466. && from_bytes != -1) ? from_bytes : 0);
  467. bytes_to_r =
  468. ((end_item == split_item_num
  469. && end_bytes != -1) ? end_bytes : 0);
  470. bytes_to_S1new =
  471. ((split_item_positions[0] ==
  472. split_item_positions[1]) ? snum012[3] : 0);
  473. /* s2bytes */
  474. snum012[4] =
  475. op_unit_num(&vn->vn_vi[split_item_num]) - snum012[4] -
  476. bytes_to_r - bytes_to_l - bytes_to_S1new;
  477. if (vn->vn_vi[split_item_num].vi_index != TYPE_DIRENTRY &&
  478. vn->vn_vi[split_item_num].vi_index != TYPE_INDIRECT)
  479. reiserfs_warning(tb->tb_sb, "vs-8115",
  480. "not directory or indirect item");
  481. }
  482. /* now we know S2bytes, calculate S1bytes */
  483. if (snum012[3] > 0) {
  484. int split_item_num;
  485. int bytes_to_r, bytes_to_l;
  486. int bytes_to_S2new;
  487. split_item_num = split_item_positions[0];
  488. bytes_to_l =
  489. ((from == split_item_num
  490. && from_bytes != -1) ? from_bytes : 0);
  491. bytes_to_r =
  492. ((end_item == split_item_num
  493. && end_bytes != -1) ? end_bytes : 0);
  494. bytes_to_S2new =
  495. ((split_item_positions[0] == split_item_positions[1]
  496. && snum012[4] != -1) ? snum012[4] : 0);
  497. /* s1bytes */
  498. snum012[3] =
  499. op_unit_num(&vn->vn_vi[split_item_num]) - snum012[3] -
  500. bytes_to_r - bytes_to_l - bytes_to_S2new;
  501. }
  502. return needed_nodes;
  503. }
  504. /*
  505. * Set parameters for balancing.
  506. * Performs write of results of analysis of balancing into structure tb,
  507. * where it will later be used by the functions that actually do the balancing.
  508. * Parameters:
  509. * tb tree_balance structure;
  510. * h current level of the node;
  511. * lnum number of items from S[h] that must be shifted to L[h];
  512. * rnum number of items from S[h] that must be shifted to R[h];
  513. * blk_num number of blocks that S[h] will be splitted into;
  514. * s012 number of items that fall into splitted nodes.
  515. * lbytes number of bytes which flow to the left neighbor from the
  516. * item that is not not shifted entirely
  517. * rbytes number of bytes which flow to the right neighbor from the
  518. * item that is not not shifted entirely
  519. * s1bytes number of bytes which flow to the first new node when
  520. * S[0] splits (this number is contained in s012 array)
  521. */
  522. static void set_parameters(struct tree_balance *tb, int h, int lnum,
  523. int rnum, int blk_num, short *s012, int lb, int rb)
  524. {
  525. tb->lnum[h] = lnum;
  526. tb->rnum[h] = rnum;
  527. tb->blknum[h] = blk_num;
  528. /* only for leaf level */
  529. if (h == 0) {
  530. if (s012 != NULL) {
  531. tb->s0num = *s012++;
  532. tb->snum[0] = *s012++;
  533. tb->snum[1] = *s012++;
  534. tb->sbytes[0] = *s012++;
  535. tb->sbytes[1] = *s012;
  536. }
  537. tb->lbytes = lb;
  538. tb->rbytes = rb;
  539. }
  540. PROC_INFO_ADD(tb->tb_sb, lnum[h], lnum);
  541. PROC_INFO_ADD(tb->tb_sb, rnum[h], rnum);
  542. PROC_INFO_ADD(tb->tb_sb, lbytes[h], lb);
  543. PROC_INFO_ADD(tb->tb_sb, rbytes[h], rb);
  544. }
  545. /*
  546. * check if node disappears if we shift tb->lnum[0] items to left
  547. * neighbor and tb->rnum[0] to the right one.
  548. */
  549. static int is_leaf_removable(struct tree_balance *tb)
  550. {
  551. struct virtual_node *vn = tb->tb_vn;
  552. int to_left, to_right;
  553. int size;
  554. int remain_items;
  555. /*
  556. * number of items that will be shifted to left (right) neighbor
  557. * entirely
  558. */
  559. to_left = tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0);
  560. to_right = tb->rnum[0] - ((tb->rbytes != -1) ? 1 : 0);
  561. remain_items = vn->vn_nr_item;
  562. /* how many items remain in S[0] after shiftings to neighbors */
  563. remain_items -= (to_left + to_right);
  564. /* all content of node can be shifted to neighbors */
  565. if (remain_items < 1) {
  566. set_parameters(tb, 0, to_left, vn->vn_nr_item - to_left, 0,
  567. NULL, -1, -1);
  568. return 1;
  569. }
  570. /* S[0] is not removable */
  571. if (remain_items > 1 || tb->lbytes == -1 || tb->rbytes == -1)
  572. return 0;
  573. /* check whether we can divide 1 remaining item between neighbors */
  574. /* get size of remaining item (in item units) */
  575. size = op_unit_num(&vn->vn_vi[to_left]);
  576. if (tb->lbytes + tb->rbytes >= size) {
  577. set_parameters(tb, 0, to_left + 1, to_right + 1, 0, NULL,
  578. tb->lbytes, -1);
  579. return 1;
  580. }
  581. return 0;
  582. }
  583. /* check whether L, S, R can be joined in one node */
  584. static int are_leaves_removable(struct tree_balance *tb, int lfree, int rfree)
  585. {
  586. struct virtual_node *vn = tb->tb_vn;
  587. int ih_size;
  588. struct buffer_head *S0;
  589. S0 = PATH_H_PBUFFER(tb->tb_path, 0);
  590. ih_size = 0;
  591. if (vn->vn_nr_item) {
  592. if (vn->vn_vi[0].vi_type & VI_TYPE_LEFT_MERGEABLE)
  593. ih_size += IH_SIZE;
  594. if (vn->vn_vi[vn->vn_nr_item - 1].
  595. vi_type & VI_TYPE_RIGHT_MERGEABLE)
  596. ih_size += IH_SIZE;
  597. } else {
  598. /* there was only one item and it will be deleted */
  599. struct item_head *ih;
  600. RFALSE(B_NR_ITEMS(S0) != 1,
  601. "vs-8125: item number must be 1: it is %d",
  602. B_NR_ITEMS(S0));
  603. ih = item_head(S0, 0);
  604. if (tb->CFR[0]
  605. && !comp_short_le_keys(&ih->ih_key,
  606. internal_key(tb->CFR[0],
  607. tb->rkey[0])))
  608. /*
  609. * Directory must be in correct state here: that is
  610. * somewhere at the left side should exist first
  611. * directory item. But the item being deleted can
  612. * not be that first one because its right neighbor
  613. * is item of the same directory. (But first item
  614. * always gets deleted in last turn). So, neighbors
  615. * of deleted item can be merged, so we can save
  616. * ih_size
  617. */
  618. if (is_direntry_le_ih(ih)) {
  619. ih_size = IH_SIZE;
  620. /*
  621. * we might check that left neighbor exists
  622. * and is of the same directory
  623. */
  624. RFALSE(le_ih_k_offset(ih) == DOT_OFFSET,
  625. "vs-8130: first directory item can not be removed until directory is not empty");
  626. }
  627. }
  628. if (MAX_CHILD_SIZE(S0) + vn->vn_size <= rfree + lfree + ih_size) {
  629. set_parameters(tb, 0, -1, -1, -1, NULL, -1, -1);
  630. PROC_INFO_INC(tb->tb_sb, leaves_removable);
  631. return 1;
  632. }
  633. return 0;
  634. }
  635. /* when we do not split item, lnum and rnum are numbers of entire items */
  636. #define SET_PAR_SHIFT_LEFT \
  637. if (h)\
  638. {\
  639. int to_l;\
  640. \
  641. to_l = (MAX_NR_KEY(Sh)+1 - lpar + vn->vn_nr_item + 1) / 2 -\
  642. (MAX_NR_KEY(Sh) + 1 - lpar);\
  643. \
  644. set_parameters (tb, h, to_l, 0, lnver, NULL, -1, -1);\
  645. }\
  646. else \
  647. {\
  648. if (lset==LEFT_SHIFT_FLOW)\
  649. set_parameters (tb, h, lpar, 0, lnver, snum012+lset,\
  650. tb->lbytes, -1);\
  651. else\
  652. set_parameters (tb, h, lpar - (tb->lbytes!=-1), 0, lnver, snum012+lset,\
  653. -1, -1);\
  654. }
  655. #define SET_PAR_SHIFT_RIGHT \
  656. if (h)\
  657. {\
  658. int to_r;\
  659. \
  660. to_r = (MAX_NR_KEY(Sh)+1 - rpar + vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 - rpar);\
  661. \
  662. set_parameters (tb, h, 0, to_r, rnver, NULL, -1, -1);\
  663. }\
  664. else \
  665. {\
  666. if (rset==RIGHT_SHIFT_FLOW)\
  667. set_parameters (tb, h, 0, rpar, rnver, snum012+rset,\
  668. -1, tb->rbytes);\
  669. else\
  670. set_parameters (tb, h, 0, rpar - (tb->rbytes!=-1), rnver, snum012+rset,\
  671. -1, -1);\
  672. }
  673. static void free_buffers_in_tb(struct tree_balance *tb)
  674. {
  675. int i;
  676. pathrelse(tb->tb_path);
  677. for (i = 0; i < MAX_HEIGHT; i++) {
  678. brelse(tb->L[i]);
  679. brelse(tb->R[i]);
  680. brelse(tb->FL[i]);
  681. brelse(tb->FR[i]);
  682. brelse(tb->CFL[i]);
  683. brelse(tb->CFR[i]);
  684. tb->L[i] = NULL;
  685. tb->R[i] = NULL;
  686. tb->FL[i] = NULL;
  687. tb->FR[i] = NULL;
  688. tb->CFL[i] = NULL;
  689. tb->CFR[i] = NULL;
  690. }
  691. }
  692. /*
  693. * Get new buffers for storing new nodes that are created while balancing.
  694. * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked;
  695. * CARRY_ON - schedule didn't occur while the function worked;
  696. * NO_DISK_SPACE - no disk space.
  697. */
  698. /* The function is NOT SCHEDULE-SAFE! */
  699. static int get_empty_nodes(struct tree_balance *tb, int h)
  700. {
  701. struct buffer_head *new_bh, *Sh = PATH_H_PBUFFER(tb->tb_path, h);
  702. b_blocknr_t *blocknr, blocknrs[MAX_AMOUNT_NEEDED] = { 0, };
  703. int counter, number_of_freeblk;
  704. int amount_needed; /* number of needed empty blocks */
  705. int retval = CARRY_ON;
  706. struct super_block *sb = tb->tb_sb;
  707. /*
  708. * number_of_freeblk is the number of empty blocks which have been
  709. * acquired for use by the balancing algorithm minus the number of
  710. * empty blocks used in the previous levels of the analysis,
  711. * number_of_freeblk = tb->cur_blknum can be non-zero if a schedule
  712. * occurs after empty blocks are acquired, and the balancing analysis
  713. * is then restarted, amount_needed is the number needed by this
  714. * level (h) of the balancing analysis.
  715. *
  716. * Note that for systems with many processes writing, it would be
  717. * more layout optimal to calculate the total number needed by all
  718. * levels and then to run reiserfs_new_blocks to get all of them at
  719. * once.
  720. */
  721. /*
  722. * Initiate number_of_freeblk to the amount acquired prior to the
  723. * restart of the analysis or 0 if not restarted, then subtract the
  724. * amount needed by all of the levels of the tree below h.
  725. */
  726. /* blknum includes S[h], so we subtract 1 in this calculation */
  727. for (counter = 0, number_of_freeblk = tb->cur_blknum;
  728. counter < h; counter++)
  729. number_of_freeblk -=
  730. (tb->blknum[counter]) ? (tb->blknum[counter] -
  731. 1) : 0;
  732. /* Allocate missing empty blocks. */
  733. /* if Sh == 0 then we are getting a new root */
  734. amount_needed = (Sh) ? (tb->blknum[h] - 1) : 1;
  735. /*
  736. * Amount_needed = the amount that we need more than the
  737. * amount that we have.
  738. */
  739. if (amount_needed > number_of_freeblk)
  740. amount_needed -= number_of_freeblk;
  741. else /* If we have enough already then there is nothing to do. */
  742. return CARRY_ON;
  743. /*
  744. * No need to check quota - is not allocated for blocks used
  745. * for formatted nodes
  746. */
  747. if (reiserfs_new_form_blocknrs(tb, blocknrs,
  748. amount_needed) == NO_DISK_SPACE)
  749. return NO_DISK_SPACE;
  750. /* for each blocknumber we just got, get a buffer and stick it on FEB */
  751. for (blocknr = blocknrs, counter = 0;
  752. counter < amount_needed; blocknr++, counter++) {
  753. RFALSE(!*blocknr,
  754. "PAP-8135: reiserfs_new_blocknrs failed when got new blocks");
  755. new_bh = sb_getblk(sb, *blocknr);
  756. RFALSE(buffer_dirty(new_bh) ||
  757. buffer_journaled(new_bh) ||
  758. buffer_journal_dirty(new_bh),
  759. "PAP-8140: journaled or dirty buffer %b for the new block",
  760. new_bh);
  761. /* Put empty buffers into the array. */
  762. RFALSE(tb->FEB[tb->cur_blknum],
  763. "PAP-8141: busy slot for new buffer");
  764. set_buffer_journal_new(new_bh);
  765. tb->FEB[tb->cur_blknum++] = new_bh;
  766. }
  767. if (retval == CARRY_ON && FILESYSTEM_CHANGED_TB(tb))
  768. retval = REPEAT_SEARCH;
  769. return retval;
  770. }
  771. /*
  772. * Get free space of the left neighbor, which is stored in the parent
  773. * node of the left neighbor.
  774. */
  775. static int get_lfree(struct tree_balance *tb, int h)
  776. {
  777. struct buffer_head *l, *f;
  778. int order;
  779. if ((f = PATH_H_PPARENT(tb->tb_path, h)) == NULL ||
  780. (l = tb->FL[h]) == NULL)
  781. return 0;
  782. if (f == l)
  783. order = PATH_H_B_ITEM_ORDER(tb->tb_path, h) - 1;
  784. else {
  785. order = B_NR_ITEMS(l);
  786. f = l;
  787. }
  788. return (MAX_CHILD_SIZE(f) - dc_size(B_N_CHILD(f, order)));
  789. }
  790. /*
  791. * Get free space of the right neighbor,
  792. * which is stored in the parent node of the right neighbor.
  793. */
  794. static int get_rfree(struct tree_balance *tb, int h)
  795. {
  796. struct buffer_head *r, *f;
  797. int order;
  798. if ((f = PATH_H_PPARENT(tb->tb_path, h)) == NULL ||
  799. (r = tb->FR[h]) == NULL)
  800. return 0;
  801. if (f == r)
  802. order = PATH_H_B_ITEM_ORDER(tb->tb_path, h) + 1;
  803. else {
  804. order = 0;
  805. f = r;
  806. }
  807. return (MAX_CHILD_SIZE(f) - dc_size(B_N_CHILD(f, order)));
  808. }
  809. /* Check whether left neighbor is in memory. */
  810. static int is_left_neighbor_in_cache(struct tree_balance *tb, int h)
  811. {
  812. struct buffer_head *father, *left;
  813. struct super_block *sb = tb->tb_sb;
  814. b_blocknr_t left_neighbor_blocknr;
  815. int left_neighbor_position;
  816. /* Father of the left neighbor does not exist. */
  817. if (!tb->FL[h])
  818. return 0;
  819. /* Calculate father of the node to be balanced. */
  820. father = PATH_H_PBUFFER(tb->tb_path, h + 1);
  821. RFALSE(!father ||
  822. !B_IS_IN_TREE(father) ||
  823. !B_IS_IN_TREE(tb->FL[h]) ||
  824. !buffer_uptodate(father) ||
  825. !buffer_uptodate(tb->FL[h]),
  826. "vs-8165: F[h] (%b) or FL[h] (%b) is invalid",
  827. father, tb->FL[h]);
  828. /*
  829. * Get position of the pointer to the left neighbor
  830. * into the left father.
  831. */
  832. left_neighbor_position = (father == tb->FL[h]) ?
  833. tb->lkey[h] : B_NR_ITEMS(tb->FL[h]);
  834. /* Get left neighbor block number. */
  835. left_neighbor_blocknr =
  836. B_N_CHILD_NUM(tb->FL[h], left_neighbor_position);
  837. /* Look for the left neighbor in the cache. */
  838. if ((left = sb_find_get_block(sb, left_neighbor_blocknr))) {
  839. RFALSE(buffer_uptodate(left) && !B_IS_IN_TREE(left),
  840. "vs-8170: left neighbor (%b %z) is not in the tree",
  841. left, left);
  842. put_bh(left);
  843. return 1;
  844. }
  845. return 0;
  846. }
  847. #define LEFT_PARENTS 'l'
  848. #define RIGHT_PARENTS 'r'
  849. static void decrement_key(struct cpu_key *key)
  850. {
  851. /* call item specific function for this key */
  852. item_ops[cpu_key_k_type(key)]->decrement_key(key);
  853. }
  854. /*
  855. * Calculate far left/right parent of the left/right neighbor of the
  856. * current node, that is calculate the left/right (FL[h]/FR[h]) neighbor
  857. * of the parent F[h].
  858. * Calculate left/right common parent of the current node and L[h]/R[h].
  859. * Calculate left/right delimiting key position.
  860. * Returns: PATH_INCORRECT - path in the tree is not correct
  861. * SCHEDULE_OCCURRED - schedule occurred while the function worked
  862. * CARRY_ON - schedule didn't occur while the function
  863. * worked
  864. */
  865. static int get_far_parent(struct tree_balance *tb,
  866. int h,
  867. struct buffer_head **pfather,
  868. struct buffer_head **pcom_father, char c_lr_par)
  869. {
  870. struct buffer_head *parent;
  871. INITIALIZE_PATH(s_path_to_neighbor_father);
  872. struct treepath *path = tb->tb_path;
  873. struct cpu_key s_lr_father_key;
  874. int counter,
  875. position = INT_MAX,
  876. first_last_position = 0,
  877. path_offset = PATH_H_PATH_OFFSET(path, h);
  878. /*
  879. * Starting from F[h] go upwards in the tree, and look for the common
  880. * ancestor of F[h], and its neighbor l/r, that should be obtained.
  881. */
  882. counter = path_offset;
  883. RFALSE(counter < FIRST_PATH_ELEMENT_OFFSET,
  884. "PAP-8180: invalid path length");
  885. for (; counter > FIRST_PATH_ELEMENT_OFFSET; counter--) {
  886. /*
  887. * Check whether parent of the current buffer in the path
  888. * is really parent in the tree.
  889. */
  890. if (!B_IS_IN_TREE
  891. (parent = PATH_OFFSET_PBUFFER(path, counter - 1)))
  892. return REPEAT_SEARCH;
  893. /* Check whether position in the parent is correct. */
  894. if ((position =
  895. PATH_OFFSET_POSITION(path,
  896. counter - 1)) >
  897. B_NR_ITEMS(parent))
  898. return REPEAT_SEARCH;
  899. /*
  900. * Check whether parent at the path really points
  901. * to the child.
  902. */
  903. if (B_N_CHILD_NUM(parent, position) !=
  904. PATH_OFFSET_PBUFFER(path, counter)->b_blocknr)
  905. return REPEAT_SEARCH;
  906. /*
  907. * Return delimiting key if position in the parent is not
  908. * equal to first/last one.
  909. */
  910. if (c_lr_par == RIGHT_PARENTS)
  911. first_last_position = B_NR_ITEMS(parent);
  912. if (position != first_last_position) {
  913. *pcom_father = parent;
  914. get_bh(*pcom_father);
  915. /*(*pcom_father = parent)->b_count++; */
  916. break;
  917. }
  918. }
  919. /* if we are in the root of the tree, then there is no common father */
  920. if (counter == FIRST_PATH_ELEMENT_OFFSET) {
  921. /*
  922. * Check whether first buffer in the path is the
  923. * root of the tree.
  924. */
  925. if (PATH_OFFSET_PBUFFER
  926. (tb->tb_path,
  927. FIRST_PATH_ELEMENT_OFFSET)->b_blocknr ==
  928. SB_ROOT_BLOCK(tb->tb_sb)) {
  929. *pfather = *pcom_father = NULL;
  930. return CARRY_ON;
  931. }
  932. return REPEAT_SEARCH;
  933. }
  934. RFALSE(B_LEVEL(*pcom_father) <= DISK_LEAF_NODE_LEVEL,
  935. "PAP-8185: (%b %z) level too small",
  936. *pcom_father, *pcom_father);
  937. /* Check whether the common parent is locked. */
  938. if (buffer_locked(*pcom_father)) {
  939. /* Release the write lock while the buffer is busy */
  940. int depth = reiserfs_write_unlock_nested(tb->tb_sb);
  941. __wait_on_buffer(*pcom_father);
  942. reiserfs_write_lock_nested(tb->tb_sb, depth);
  943. if (FILESYSTEM_CHANGED_TB(tb)) {
  944. brelse(*pcom_father);
  945. return REPEAT_SEARCH;
  946. }
  947. }
  948. /*
  949. * So, we got common parent of the current node and its
  950. * left/right neighbor. Now we are getting the parent of the
  951. * left/right neighbor.
  952. */
  953. /* Form key to get parent of the left/right neighbor. */
  954. le_key2cpu_key(&s_lr_father_key,
  955. internal_key(*pcom_father,
  956. (c_lr_par ==
  957. LEFT_PARENTS) ? (tb->lkey[h - 1] =
  958. position -
  959. 1) : (tb->rkey[h -
  960. 1] =
  961. position)));
  962. if (c_lr_par == LEFT_PARENTS)
  963. decrement_key(&s_lr_father_key);
  964. if (search_by_key
  965. (tb->tb_sb, &s_lr_father_key, &s_path_to_neighbor_father,
  966. h + 1) == IO_ERROR)
  967. /* path is released */
  968. return IO_ERROR;
  969. if (FILESYSTEM_CHANGED_TB(tb)) {
  970. pathrelse(&s_path_to_neighbor_father);
  971. brelse(*pcom_father);
  972. return REPEAT_SEARCH;
  973. }
  974. *pfather = PATH_PLAST_BUFFER(&s_path_to_neighbor_father);
  975. RFALSE(B_LEVEL(*pfather) != h + 1,
  976. "PAP-8190: (%b %z) level too small", *pfather, *pfather);
  977. RFALSE(s_path_to_neighbor_father.path_length <
  978. FIRST_PATH_ELEMENT_OFFSET, "PAP-8192: path length is too small");
  979. s_path_to_neighbor_father.path_length--;
  980. pathrelse(&s_path_to_neighbor_father);
  981. return CARRY_ON;
  982. }
  983. /*
  984. * Get parents of neighbors of node in the path(S[path_offset]) and
  985. * common parents of S[path_offset] and L[path_offset]/R[path_offset]:
  986. * F[path_offset], FL[path_offset], FR[path_offset], CFL[path_offset],
  987. * CFR[path_offset].
  988. * Calculate numbers of left and right delimiting keys position:
  989. * lkey[path_offset], rkey[path_offset].
  990. * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked
  991. * CARRY_ON - schedule didn't occur while the function worked
  992. */
  993. static int get_parents(struct tree_balance *tb, int h)
  994. {
  995. struct treepath *path = tb->tb_path;
  996. int position,
  997. ret,
  998. path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h);
  999. struct buffer_head *curf, *curcf;
  1000. /* Current node is the root of the tree or will be root of the tree */
  1001. if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
  1002. /*
  1003. * The root can not have parents.
  1004. * Release nodes which previously were obtained as
  1005. * parents of the current node neighbors.
  1006. */
  1007. brelse(tb->FL[h]);
  1008. brelse(tb->CFL[h]);
  1009. brelse(tb->FR[h]);
  1010. brelse(tb->CFR[h]);
  1011. tb->FL[h] = NULL;
  1012. tb->CFL[h] = NULL;
  1013. tb->FR[h] = NULL;
  1014. tb->CFR[h] = NULL;
  1015. return CARRY_ON;
  1016. }
  1017. /* Get parent FL[path_offset] of L[path_offset]. */
  1018. position = PATH_OFFSET_POSITION(path, path_offset - 1);
  1019. if (position) {
  1020. /* Current node is not the first child of its parent. */
  1021. curf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1022. curcf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1023. get_bh(curf);
  1024. get_bh(curf);
  1025. tb->lkey[h] = position - 1;
  1026. } else {
  1027. /*
  1028. * Calculate current parent of L[path_offset], which is the
  1029. * left neighbor of the current node. Calculate current
  1030. * common parent of L[path_offset] and the current node.
  1031. * Note that CFL[path_offset] not equal FL[path_offset] and
  1032. * CFL[path_offset] not equal F[path_offset].
  1033. * Calculate lkey[path_offset].
  1034. */
  1035. if ((ret = get_far_parent(tb, h + 1, &curf,
  1036. &curcf,
  1037. LEFT_PARENTS)) != CARRY_ON)
  1038. return ret;
  1039. }
  1040. brelse(tb->FL[h]);
  1041. tb->FL[h] = curf; /* New initialization of FL[h]. */
  1042. brelse(tb->CFL[h]);
  1043. tb->CFL[h] = curcf; /* New initialization of CFL[h]. */
  1044. RFALSE((curf && !B_IS_IN_TREE(curf)) ||
  1045. (curcf && !B_IS_IN_TREE(curcf)),
  1046. "PAP-8195: FL (%b) or CFL (%b) is invalid", curf, curcf);
  1047. /* Get parent FR[h] of R[h]. */
  1048. /* Current node is the last child of F[h]. FR[h] != F[h]. */
  1049. if (position == B_NR_ITEMS(PATH_H_PBUFFER(path, h + 1))) {
  1050. /*
  1051. * Calculate current parent of R[h], which is the right
  1052. * neighbor of F[h]. Calculate current common parent of
  1053. * R[h] and current node. Note that CFR[h] not equal
  1054. * FR[path_offset] and CFR[h] not equal F[h].
  1055. */
  1056. if ((ret =
  1057. get_far_parent(tb, h + 1, &curf, &curcf,
  1058. RIGHT_PARENTS)) != CARRY_ON)
  1059. return ret;
  1060. } else {
  1061. /* Current node is not the last child of its parent F[h]. */
  1062. curf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1063. curcf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1064. get_bh(curf);
  1065. get_bh(curf);
  1066. tb->rkey[h] = position;
  1067. }
  1068. brelse(tb->FR[h]);
  1069. /* New initialization of FR[path_offset]. */
  1070. tb->FR[h] = curf;
  1071. brelse(tb->CFR[h]);
  1072. /* New initialization of CFR[path_offset]. */
  1073. tb->CFR[h] = curcf;
  1074. RFALSE((curf && !B_IS_IN_TREE(curf)) ||
  1075. (curcf && !B_IS_IN_TREE(curcf)),
  1076. "PAP-8205: FR (%b) or CFR (%b) is invalid", curf, curcf);
  1077. return CARRY_ON;
  1078. }
  1079. /*
  1080. * it is possible to remove node as result of shiftings to
  1081. * neighbors even when we insert or paste item.
  1082. */
  1083. static inline int can_node_be_removed(int mode, int lfree, int sfree, int rfree,
  1084. struct tree_balance *tb, int h)
  1085. {
  1086. struct buffer_head *Sh = PATH_H_PBUFFER(tb->tb_path, h);
  1087. int levbytes = tb->insert_size[h];
  1088. struct item_head *ih;
  1089. struct reiserfs_key *r_key = NULL;
  1090. ih = item_head(Sh, 0);
  1091. if (tb->CFR[h])
  1092. r_key = internal_key(tb->CFR[h], tb->rkey[h]);
  1093. if (lfree + rfree + sfree < MAX_CHILD_SIZE(Sh) + levbytes
  1094. /* shifting may merge items which might save space */
  1095. -
  1096. ((!h
  1097. && op_is_left_mergeable(&ih->ih_key, Sh->b_size)) ? IH_SIZE : 0)
  1098. -
  1099. ((!h && r_key
  1100. && op_is_left_mergeable(r_key, Sh->b_size)) ? IH_SIZE : 0)
  1101. + ((h) ? KEY_SIZE : 0)) {
  1102. /* node can not be removed */
  1103. if (sfree >= levbytes) {
  1104. /* new item fits into node S[h] without any shifting */
  1105. if (!h)
  1106. tb->s0num =
  1107. B_NR_ITEMS(Sh) +
  1108. ((mode == M_INSERT) ? 1 : 0);
  1109. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1110. return NO_BALANCING_NEEDED;
  1111. }
  1112. }
  1113. PROC_INFO_INC(tb->tb_sb, can_node_be_removed[h]);
  1114. return !NO_BALANCING_NEEDED;
  1115. }
  1116. /*
  1117. * Check whether current node S[h] is balanced when increasing its size by
  1118. * Inserting or Pasting.
  1119. * Calculate parameters for balancing for current level h.
  1120. * Parameters:
  1121. * tb tree_balance structure;
  1122. * h current level of the node;
  1123. * inum item number in S[h];
  1124. * mode i - insert, p - paste;
  1125. * Returns: 1 - schedule occurred;
  1126. * 0 - balancing for higher levels needed;
  1127. * -1 - no balancing for higher levels needed;
  1128. * -2 - no disk space.
  1129. */
  1130. /* ip means Inserting or Pasting */
  1131. static int ip_check_balance(struct tree_balance *tb, int h)
  1132. {
  1133. struct virtual_node *vn = tb->tb_vn;
  1134. /*
  1135. * Number of bytes that must be inserted into (value is negative
  1136. * if bytes are deleted) buffer which contains node being balanced.
  1137. * The mnemonic is that the attempted change in node space used
  1138. * level is levbytes bytes.
  1139. */
  1140. int levbytes;
  1141. int ret;
  1142. int lfree, sfree, rfree /* free space in L, S and R */ ;
  1143. /*
  1144. * nver is short for number of vertixes, and lnver is the number if
  1145. * we shift to the left, rnver is the number if we shift to the
  1146. * right, and lrnver is the number if we shift in both directions.
  1147. * The goal is to minimize first the number of vertixes, and second,
  1148. * the number of vertixes whose contents are changed by shifting,
  1149. * and third the number of uncached vertixes whose contents are
  1150. * changed by shifting and must be read from disk.
  1151. */
  1152. int nver, lnver, rnver, lrnver;
  1153. /*
  1154. * used at leaf level only, S0 = S[0] is the node being balanced,
  1155. * sInum [ I = 0,1,2 ] is the number of items that will
  1156. * remain in node SI after balancing. S1 and S2 are new
  1157. * nodes that might be created.
  1158. */
  1159. /*
  1160. * we perform 8 calls to get_num_ver(). For each call we
  1161. * calculate five parameters. where 4th parameter is s1bytes
  1162. * and 5th - s2bytes
  1163. *
  1164. * s0num, s1num, s2num for 8 cases
  1165. * 0,1 - do not shift and do not shift but bottle
  1166. * 2 - shift only whole item to left
  1167. * 3 - shift to left and bottle as much as possible
  1168. * 4,5 - shift to right (whole items and as much as possible
  1169. * 6,7 - shift to both directions (whole items and as much as possible)
  1170. */
  1171. short snum012[40] = { 0, };
  1172. /* Sh is the node whose balance is currently being checked */
  1173. struct buffer_head *Sh;
  1174. Sh = PATH_H_PBUFFER(tb->tb_path, h);
  1175. levbytes = tb->insert_size[h];
  1176. /* Calculate balance parameters for creating new root. */
  1177. if (!Sh) {
  1178. if (!h)
  1179. reiserfs_panic(tb->tb_sb, "vs-8210",
  1180. "S[0] can not be 0");
  1181. switch (ret = get_empty_nodes(tb, h)) {
  1182. /* no balancing for higher levels needed */
  1183. case CARRY_ON:
  1184. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1185. return NO_BALANCING_NEEDED;
  1186. case NO_DISK_SPACE:
  1187. case REPEAT_SEARCH:
  1188. return ret;
  1189. default:
  1190. reiserfs_panic(tb->tb_sb, "vs-8215", "incorrect "
  1191. "return value of get_empty_nodes");
  1192. }
  1193. }
  1194. /* get parents of S[h] neighbors. */
  1195. ret = get_parents(tb, h);
  1196. if (ret != CARRY_ON)
  1197. return ret;
  1198. sfree = B_FREE_SPACE(Sh);
  1199. /* get free space of neighbors */
  1200. rfree = get_rfree(tb, h);
  1201. lfree = get_lfree(tb, h);
  1202. /* and new item fits into node S[h] without any shifting */
  1203. if (can_node_be_removed(vn->vn_mode, lfree, sfree, rfree, tb, h) ==
  1204. NO_BALANCING_NEEDED)
  1205. return NO_BALANCING_NEEDED;
  1206. create_virtual_node(tb, h);
  1207. /*
  1208. * determine maximal number of items we can shift to the left
  1209. * neighbor (in tb structure) and the maximal number of bytes
  1210. * that can flow to the left neighbor from the left most liquid
  1211. * item that cannot be shifted from S[0] entirely (returned value)
  1212. */
  1213. check_left(tb, h, lfree);
  1214. /*
  1215. * determine maximal number of items we can shift to the right
  1216. * neighbor (in tb structure) and the maximal number of bytes
  1217. * that can flow to the right neighbor from the right most liquid
  1218. * item that cannot be shifted from S[0] entirely (returned value)
  1219. */
  1220. check_right(tb, h, rfree);
  1221. /*
  1222. * all contents of internal node S[h] can be moved into its
  1223. * neighbors, S[h] will be removed after balancing
  1224. */
  1225. if (h && (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1)) {
  1226. int to_r;
  1227. /*
  1228. * Since we are working on internal nodes, and our internal
  1229. * nodes have fixed size entries, then we can balance by the
  1230. * number of items rather than the space they consume. In this
  1231. * routine we set the left node equal to the right node,
  1232. * allowing a difference of less than or equal to 1 child
  1233. * pointer.
  1234. */
  1235. to_r =
  1236. ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] - tb->rnum[h] +
  1237. vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 -
  1238. tb->rnum[h]);
  1239. set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r, 0, NULL,
  1240. -1, -1);
  1241. return CARRY_ON;
  1242. }
  1243. /*
  1244. * this checks balance condition, that any two neighboring nodes
  1245. * can not fit in one node
  1246. */
  1247. RFALSE(h &&
  1248. (tb->lnum[h] >= vn->vn_nr_item + 1 ||
  1249. tb->rnum[h] >= vn->vn_nr_item + 1),
  1250. "vs-8220: tree is not balanced on internal level");
  1251. RFALSE(!h && ((tb->lnum[h] >= vn->vn_nr_item && (tb->lbytes == -1)) ||
  1252. (tb->rnum[h] >= vn->vn_nr_item && (tb->rbytes == -1))),
  1253. "vs-8225: tree is not balanced on leaf level");
  1254. /*
  1255. * all contents of S[0] can be moved into its neighbors
  1256. * S[0] will be removed after balancing.
  1257. */
  1258. if (!h && is_leaf_removable(tb))
  1259. return CARRY_ON;
  1260. /*
  1261. * why do we perform this check here rather than earlier??
  1262. * Answer: we can win 1 node in some cases above. Moreover we
  1263. * checked it above, when we checked, that S[0] is not removable
  1264. * in principle
  1265. */
  1266. /* new item fits into node S[h] without any shifting */
  1267. if (sfree >= levbytes) {
  1268. if (!h)
  1269. tb->s0num = vn->vn_nr_item;
  1270. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1271. return NO_BALANCING_NEEDED;
  1272. }
  1273. {
  1274. int lpar, rpar, nset, lset, rset, lrset;
  1275. /* regular overflowing of the node */
  1276. /*
  1277. * get_num_ver works in 2 modes (FLOW & NO_FLOW)
  1278. * lpar, rpar - number of items we can shift to left/right
  1279. * neighbor (including splitting item)
  1280. * nset, lset, rset, lrset - shows, whether flowing items
  1281. * give better packing
  1282. */
  1283. #define FLOW 1
  1284. #define NO_FLOW 0 /* do not any splitting */
  1285. /* we choose one of the following */
  1286. #define NOTHING_SHIFT_NO_FLOW 0
  1287. #define NOTHING_SHIFT_FLOW 5
  1288. #define LEFT_SHIFT_NO_FLOW 10
  1289. #define LEFT_SHIFT_FLOW 15
  1290. #define RIGHT_SHIFT_NO_FLOW 20
  1291. #define RIGHT_SHIFT_FLOW 25
  1292. #define LR_SHIFT_NO_FLOW 30
  1293. #define LR_SHIFT_FLOW 35
  1294. lpar = tb->lnum[h];
  1295. rpar = tb->rnum[h];
  1296. /*
  1297. * calculate number of blocks S[h] must be split into when
  1298. * nothing is shifted to the neighbors, as well as number of
  1299. * items in each part of the split node (s012 numbers),
  1300. * and number of bytes (s1bytes) of the shared drop which
  1301. * flow to S1 if any
  1302. */
  1303. nset = NOTHING_SHIFT_NO_FLOW;
  1304. nver = get_num_ver(vn->vn_mode, tb, h,
  1305. 0, -1, h ? vn->vn_nr_item : 0, -1,
  1306. snum012, NO_FLOW);
  1307. if (!h) {
  1308. int nver1;
  1309. /*
  1310. * note, that in this case we try to bottle
  1311. * between S[0] and S1 (S1 - the first new node)
  1312. */
  1313. nver1 = get_num_ver(vn->vn_mode, tb, h,
  1314. 0, -1, 0, -1,
  1315. snum012 + NOTHING_SHIFT_FLOW, FLOW);
  1316. if (nver > nver1)
  1317. nset = NOTHING_SHIFT_FLOW, nver = nver1;
  1318. }
  1319. /*
  1320. * calculate number of blocks S[h] must be split into when
  1321. * l_shift_num first items and l_shift_bytes of the right
  1322. * most liquid item to be shifted are shifted to the left
  1323. * neighbor, as well as number of items in each part of the
  1324. * splitted node (s012 numbers), and number of bytes
  1325. * (s1bytes) of the shared drop which flow to S1 if any
  1326. */
  1327. lset = LEFT_SHIFT_NO_FLOW;
  1328. lnver = get_num_ver(vn->vn_mode, tb, h,
  1329. lpar - ((h || tb->lbytes == -1) ? 0 : 1),
  1330. -1, h ? vn->vn_nr_item : 0, -1,
  1331. snum012 + LEFT_SHIFT_NO_FLOW, NO_FLOW);
  1332. if (!h) {
  1333. int lnver1;
  1334. lnver1 = get_num_ver(vn->vn_mode, tb, h,
  1335. lpar -
  1336. ((tb->lbytes != -1) ? 1 : 0),
  1337. tb->lbytes, 0, -1,
  1338. snum012 + LEFT_SHIFT_FLOW, FLOW);
  1339. if (lnver > lnver1)
  1340. lset = LEFT_SHIFT_FLOW, lnver = lnver1;
  1341. }
  1342. /*
  1343. * calculate number of blocks S[h] must be split into when
  1344. * r_shift_num first items and r_shift_bytes of the left most
  1345. * liquid item to be shifted are shifted to the right neighbor,
  1346. * as well as number of items in each part of the splitted
  1347. * node (s012 numbers), and number of bytes (s1bytes) of the
  1348. * shared drop which flow to S1 if any
  1349. */
  1350. rset = RIGHT_SHIFT_NO_FLOW;
  1351. rnver = get_num_ver(vn->vn_mode, tb, h,
  1352. 0, -1,
  1353. h ? (vn->vn_nr_item - rpar) : (rpar -
  1354. ((tb->
  1355. rbytes !=
  1356. -1) ? 1 :
  1357. 0)), -1,
  1358. snum012 + RIGHT_SHIFT_NO_FLOW, NO_FLOW);
  1359. if (!h) {
  1360. int rnver1;
  1361. rnver1 = get_num_ver(vn->vn_mode, tb, h,
  1362. 0, -1,
  1363. (rpar -
  1364. ((tb->rbytes != -1) ? 1 : 0)),
  1365. tb->rbytes,
  1366. snum012 + RIGHT_SHIFT_FLOW, FLOW);
  1367. if (rnver > rnver1)
  1368. rset = RIGHT_SHIFT_FLOW, rnver = rnver1;
  1369. }
  1370. /*
  1371. * calculate number of blocks S[h] must be split into when
  1372. * items are shifted in both directions, as well as number
  1373. * of items in each part of the splitted node (s012 numbers),
  1374. * and number of bytes (s1bytes) of the shared drop which
  1375. * flow to S1 if any
  1376. */
  1377. lrset = LR_SHIFT_NO_FLOW;
  1378. lrnver = get_num_ver(vn->vn_mode, tb, h,
  1379. lpar - ((h || tb->lbytes == -1) ? 0 : 1),
  1380. -1,
  1381. h ? (vn->vn_nr_item - rpar) : (rpar -
  1382. ((tb->
  1383. rbytes !=
  1384. -1) ? 1 :
  1385. 0)), -1,
  1386. snum012 + LR_SHIFT_NO_FLOW, NO_FLOW);
  1387. if (!h) {
  1388. int lrnver1;
  1389. lrnver1 = get_num_ver(vn->vn_mode, tb, h,
  1390. lpar -
  1391. ((tb->lbytes != -1) ? 1 : 0),
  1392. tb->lbytes,
  1393. (rpar -
  1394. ((tb->rbytes != -1) ? 1 : 0)),
  1395. tb->rbytes,
  1396. snum012 + LR_SHIFT_FLOW, FLOW);
  1397. if (lrnver > lrnver1)
  1398. lrset = LR_SHIFT_FLOW, lrnver = lrnver1;
  1399. }
  1400. /*
  1401. * Our general shifting strategy is:
  1402. * 1) to minimized number of new nodes;
  1403. * 2) to minimized number of neighbors involved in shifting;
  1404. * 3) to minimized number of disk reads;
  1405. */
  1406. /* we can win TWO or ONE nodes by shifting in both directions */
  1407. if (lrnver < lnver && lrnver < rnver) {
  1408. RFALSE(h &&
  1409. (tb->lnum[h] != 1 ||
  1410. tb->rnum[h] != 1 ||
  1411. lrnver != 1 || rnver != 2 || lnver != 2
  1412. || h != 1), "vs-8230: bad h");
  1413. if (lrset == LR_SHIFT_FLOW)
  1414. set_parameters(tb, h, tb->lnum[h], tb->rnum[h],
  1415. lrnver, snum012 + lrset,
  1416. tb->lbytes, tb->rbytes);
  1417. else
  1418. set_parameters(tb, h,
  1419. tb->lnum[h] -
  1420. ((tb->lbytes == -1) ? 0 : 1),
  1421. tb->rnum[h] -
  1422. ((tb->rbytes == -1) ? 0 : 1),
  1423. lrnver, snum012 + lrset, -1, -1);
  1424. return CARRY_ON;
  1425. }
  1426. /*
  1427. * if shifting doesn't lead to better packing
  1428. * then don't shift
  1429. */
  1430. if (nver == lrnver) {
  1431. set_parameters(tb, h, 0, 0, nver, snum012 + nset, -1,
  1432. -1);
  1433. return CARRY_ON;
  1434. }
  1435. /*
  1436. * now we know that for better packing shifting in only one
  1437. * direction either to the left or to the right is required
  1438. */
  1439. /*
  1440. * if shifting to the left is better than
  1441. * shifting to the right
  1442. */
  1443. if (lnver < rnver) {
  1444. SET_PAR_SHIFT_LEFT;
  1445. return CARRY_ON;
  1446. }
  1447. /*
  1448. * if shifting to the right is better than
  1449. * shifting to the left
  1450. */
  1451. if (lnver > rnver) {
  1452. SET_PAR_SHIFT_RIGHT;
  1453. return CARRY_ON;
  1454. }
  1455. /*
  1456. * now shifting in either direction gives the same number
  1457. * of nodes and we can make use of the cached neighbors
  1458. */
  1459. if (is_left_neighbor_in_cache(tb, h)) {
  1460. SET_PAR_SHIFT_LEFT;
  1461. return CARRY_ON;
  1462. }
  1463. /*
  1464. * shift to the right independently on whether the
  1465. * right neighbor in cache or not
  1466. */
  1467. SET_PAR_SHIFT_RIGHT;
  1468. return CARRY_ON;
  1469. }
  1470. }
  1471. /*
  1472. * Check whether current node S[h] is balanced when Decreasing its size by
  1473. * Deleting or Cutting for INTERNAL node of S+tree.
  1474. * Calculate parameters for balancing for current level h.
  1475. * Parameters:
  1476. * tb tree_balance structure;
  1477. * h current level of the node;
  1478. * inum item number in S[h];
  1479. * mode i - insert, p - paste;
  1480. * Returns: 1 - schedule occurred;
  1481. * 0 - balancing for higher levels needed;
  1482. * -1 - no balancing for higher levels needed;
  1483. * -2 - no disk space.
  1484. *
  1485. * Note: Items of internal nodes have fixed size, so the balance condition for
  1486. * the internal part of S+tree is as for the B-trees.
  1487. */
  1488. static int dc_check_balance_internal(struct tree_balance *tb, int h)
  1489. {
  1490. struct virtual_node *vn = tb->tb_vn;
  1491. /*
  1492. * Sh is the node whose balance is currently being checked,
  1493. * and Fh is its father.
  1494. */
  1495. struct buffer_head *Sh, *Fh;
  1496. int maxsize, ret;
  1497. int lfree, rfree /* free space in L and R */ ;
  1498. Sh = PATH_H_PBUFFER(tb->tb_path, h);
  1499. Fh = PATH_H_PPARENT(tb->tb_path, h);
  1500. maxsize = MAX_CHILD_SIZE(Sh);
  1501. /*
  1502. * using tb->insert_size[h], which is negative in this case,
  1503. * create_virtual_node calculates:
  1504. * new_nr_item = number of items node would have if operation is
  1505. * performed without balancing (new_nr_item);
  1506. */
  1507. create_virtual_node(tb, h);
  1508. if (!Fh) { /* S[h] is the root. */
  1509. /* no balancing for higher levels needed */
  1510. if (vn->vn_nr_item > 0) {
  1511. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1512. return NO_BALANCING_NEEDED;
  1513. }
  1514. /*
  1515. * new_nr_item == 0.
  1516. * Current root will be deleted resulting in
  1517. * decrementing the tree height.
  1518. */
  1519. set_parameters(tb, h, 0, 0, 0, NULL, -1, -1);
  1520. return CARRY_ON;
  1521. }
  1522. if ((ret = get_parents(tb, h)) != CARRY_ON)
  1523. return ret;
  1524. /* get free space of neighbors */
  1525. rfree = get_rfree(tb, h);
  1526. lfree = get_lfree(tb, h);
  1527. /* determine maximal number of items we can fit into neighbors */
  1528. check_left(tb, h, lfree);
  1529. check_right(tb, h, rfree);
  1530. /*
  1531. * Balance condition for the internal node is valid.
  1532. * In this case we balance only if it leads to better packing.
  1533. */
  1534. if (vn->vn_nr_item >= MIN_NR_KEY(Sh)) {
  1535. /*
  1536. * Here we join S[h] with one of its neighbors,
  1537. * which is impossible with greater values of new_nr_item.
  1538. */
  1539. if (vn->vn_nr_item == MIN_NR_KEY(Sh)) {
  1540. /* All contents of S[h] can be moved to L[h]. */
  1541. if (tb->lnum[h] >= vn->vn_nr_item + 1) {
  1542. int n;
  1543. int order_L;
  1544. order_L =
  1545. ((n =
  1546. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1547. h)) ==
  1548. 0) ? B_NR_ITEMS(tb->FL[h]) : n - 1;
  1549. n = dc_size(B_N_CHILD(tb->FL[h], order_L)) /
  1550. (DC_SIZE + KEY_SIZE);
  1551. set_parameters(tb, h, -n - 1, 0, 0, NULL, -1,
  1552. -1);
  1553. return CARRY_ON;
  1554. }
  1555. /* All contents of S[h] can be moved to R[h]. */
  1556. if (tb->rnum[h] >= vn->vn_nr_item + 1) {
  1557. int n;
  1558. int order_R;
  1559. order_R =
  1560. ((n =
  1561. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1562. h)) ==
  1563. B_NR_ITEMS(Fh)) ? 0 : n + 1;
  1564. n = dc_size(B_N_CHILD(tb->FR[h], order_R)) /
  1565. (DC_SIZE + KEY_SIZE);
  1566. set_parameters(tb, h, 0, -n - 1, 0, NULL, -1,
  1567. -1);
  1568. return CARRY_ON;
  1569. }
  1570. }
  1571. /*
  1572. * All contents of S[h] can be moved to the neighbors
  1573. * (L[h] & R[h]).
  1574. */
  1575. if (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1) {
  1576. int to_r;
  1577. to_r =
  1578. ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] -
  1579. tb->rnum[h] + vn->vn_nr_item + 1) / 2 -
  1580. (MAX_NR_KEY(Sh) + 1 - tb->rnum[h]);
  1581. set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r,
  1582. 0, NULL, -1, -1);
  1583. return CARRY_ON;
  1584. }
  1585. /* Balancing does not lead to better packing. */
  1586. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1587. return NO_BALANCING_NEEDED;
  1588. }
  1589. /*
  1590. * Current node contain insufficient number of items.
  1591. * Balancing is required.
  1592. */
  1593. /* Check whether we can merge S[h] with left neighbor. */
  1594. if (tb->lnum[h] >= vn->vn_nr_item + 1)
  1595. if (is_left_neighbor_in_cache(tb, h)
  1596. || tb->rnum[h] < vn->vn_nr_item + 1 || !tb->FR[h]) {
  1597. int n;
  1598. int order_L;
  1599. order_L =
  1600. ((n =
  1601. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1602. h)) ==
  1603. 0) ? B_NR_ITEMS(tb->FL[h]) : n - 1;
  1604. n = dc_size(B_N_CHILD(tb->FL[h], order_L)) / (DC_SIZE +
  1605. KEY_SIZE);
  1606. set_parameters(tb, h, -n - 1, 0, 0, NULL, -1, -1);
  1607. return CARRY_ON;
  1608. }
  1609. /* Check whether we can merge S[h] with right neighbor. */
  1610. if (tb->rnum[h] >= vn->vn_nr_item + 1) {
  1611. int n;
  1612. int order_R;
  1613. order_R =
  1614. ((n =
  1615. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1616. h)) == B_NR_ITEMS(Fh)) ? 0 : (n + 1);
  1617. n = dc_size(B_N_CHILD(tb->FR[h], order_R)) / (DC_SIZE +
  1618. KEY_SIZE);
  1619. set_parameters(tb, h, 0, -n - 1, 0, NULL, -1, -1);
  1620. return CARRY_ON;
  1621. }
  1622. /* All contents of S[h] can be moved to the neighbors (L[h] & R[h]). */
  1623. if (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1) {
  1624. int to_r;
  1625. to_r =
  1626. ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] - tb->rnum[h] +
  1627. vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 -
  1628. tb->rnum[h]);
  1629. set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r, 0, NULL,
  1630. -1, -1);
  1631. return CARRY_ON;
  1632. }
  1633. /* For internal nodes try to borrow item from a neighbor */
  1634. RFALSE(!tb->FL[h] && !tb->FR[h], "vs-8235: trying to borrow for root");
  1635. /* Borrow one or two items from caching neighbor */
  1636. if (is_left_neighbor_in_cache(tb, h) || !tb->FR[h]) {
  1637. int from_l;
  1638. from_l =
  1639. (MAX_NR_KEY(Sh) + 1 - tb->lnum[h] + vn->vn_nr_item +
  1640. 1) / 2 - (vn->vn_nr_item + 1);
  1641. set_parameters(tb, h, -from_l, 0, 1, NULL, -1, -1);
  1642. return CARRY_ON;
  1643. }
  1644. set_parameters(tb, h, 0,
  1645. -((MAX_NR_KEY(Sh) + 1 - tb->rnum[h] + vn->vn_nr_item +
  1646. 1) / 2 - (vn->vn_nr_item + 1)), 1, NULL, -1, -1);
  1647. return CARRY_ON;
  1648. }
  1649. /*
  1650. * Check whether current node S[h] is balanced when Decreasing its size by
  1651. * Deleting or Truncating for LEAF node of S+tree.
  1652. * Calculate parameters for balancing for current level h.
  1653. * Parameters:
  1654. * tb tree_balance structure;
  1655. * h current level of the node;
  1656. * inum item number in S[h];
  1657. * mode i - insert, p - paste;
  1658. * Returns: 1 - schedule occurred;
  1659. * 0 - balancing for higher levels needed;
  1660. * -1 - no balancing for higher levels needed;
  1661. * -2 - no disk space.
  1662. */
  1663. static int dc_check_balance_leaf(struct tree_balance *tb, int h)
  1664. {
  1665. struct virtual_node *vn = tb->tb_vn;
  1666. /*
  1667. * Number of bytes that must be deleted from
  1668. * (value is negative if bytes are deleted) buffer which
  1669. * contains node being balanced. The mnemonic is that the
  1670. * attempted change in node space used level is levbytes bytes.
  1671. */
  1672. int levbytes;
  1673. /* the maximal item size */
  1674. int maxsize, ret;
  1675. /*
  1676. * S0 is the node whose balance is currently being checked,
  1677. * and F0 is its father.
  1678. */
  1679. struct buffer_head *S0, *F0;
  1680. int lfree, rfree /* free space in L and R */ ;
  1681. S0 = PATH_H_PBUFFER(tb->tb_path, 0);
  1682. F0 = PATH_H_PPARENT(tb->tb_path, 0);
  1683. levbytes = tb->insert_size[h];
  1684. maxsize = MAX_CHILD_SIZE(S0); /* maximal possible size of an item */
  1685. if (!F0) { /* S[0] is the root now. */
  1686. RFALSE(-levbytes >= maxsize - B_FREE_SPACE(S0),
  1687. "vs-8240: attempt to create empty buffer tree");
  1688. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1689. return NO_BALANCING_NEEDED;
  1690. }
  1691. if ((ret = get_parents(tb, h)) != CARRY_ON)
  1692. return ret;
  1693. /* get free space of neighbors */
  1694. rfree = get_rfree(tb, h);
  1695. lfree = get_lfree(tb, h);
  1696. create_virtual_node(tb, h);
  1697. /* if 3 leaves can be merge to one, set parameters and return */
  1698. if (are_leaves_removable(tb, lfree, rfree))
  1699. return CARRY_ON;
  1700. /*
  1701. * determine maximal number of items we can shift to the left/right
  1702. * neighbor and the maximal number of bytes that can flow to the
  1703. * left/right neighbor from the left/right most liquid item that
  1704. * cannot be shifted from S[0] entirely
  1705. */
  1706. check_left(tb, h, lfree);
  1707. check_right(tb, h, rfree);
  1708. /* check whether we can merge S with left neighbor. */
  1709. if (tb->lnum[0] >= vn->vn_nr_item && tb->lbytes == -1)
  1710. if (is_left_neighbor_in_cache(tb, h) || ((tb->rnum[0] - ((tb->rbytes == -1) ? 0 : 1)) < vn->vn_nr_item) || /* S can not be merged with R */
  1711. !tb->FR[h]) {
  1712. RFALSE(!tb->FL[h],
  1713. "vs-8245: dc_check_balance_leaf: FL[h] must exist");
  1714. /* set parameter to merge S[0] with its left neighbor */
  1715. set_parameters(tb, h, -1, 0, 0, NULL, -1, -1);
  1716. return CARRY_ON;
  1717. }
  1718. /* check whether we can merge S[0] with right neighbor. */
  1719. if (tb->rnum[0] >= vn->vn_nr_item && tb->rbytes == -1) {
  1720. set_parameters(tb, h, 0, -1, 0, NULL, -1, -1);
  1721. return CARRY_ON;
  1722. }
  1723. /*
  1724. * All contents of S[0] can be moved to the neighbors (L[0] & R[0]).
  1725. * Set parameters and return
  1726. */
  1727. if (is_leaf_removable(tb))
  1728. return CARRY_ON;
  1729. /* Balancing is not required. */
  1730. tb->s0num = vn->vn_nr_item;
  1731. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1732. return NO_BALANCING_NEEDED;
  1733. }
  1734. /*
  1735. * Check whether current node S[h] is balanced when Decreasing its size by
  1736. * Deleting or Cutting.
  1737. * Calculate parameters for balancing for current level h.
  1738. * Parameters:
  1739. * tb tree_balance structure;
  1740. * h current level of the node;
  1741. * inum item number in S[h];
  1742. * mode d - delete, c - cut.
  1743. * Returns: 1 - schedule occurred;
  1744. * 0 - balancing for higher levels needed;
  1745. * -1 - no balancing for higher levels needed;
  1746. * -2 - no disk space.
  1747. */
  1748. static int dc_check_balance(struct tree_balance *tb, int h)
  1749. {
  1750. RFALSE(!(PATH_H_PBUFFER(tb->tb_path, h)),
  1751. "vs-8250: S is not initialized");
  1752. if (h)
  1753. return dc_check_balance_internal(tb, h);
  1754. else
  1755. return dc_check_balance_leaf(tb, h);
  1756. }
  1757. /*
  1758. * Check whether current node S[h] is balanced.
  1759. * Calculate parameters for balancing for current level h.
  1760. * Parameters:
  1761. *
  1762. * tb tree_balance structure:
  1763. *
  1764. * tb is a large structure that must be read about in the header
  1765. * file at the same time as this procedure if the reader is
  1766. * to successfully understand this procedure
  1767. *
  1768. * h current level of the node;
  1769. * inum item number in S[h];
  1770. * mode i - insert, p - paste, d - delete, c - cut.
  1771. * Returns: 1 - schedule occurred;
  1772. * 0 - balancing for higher levels needed;
  1773. * -1 - no balancing for higher levels needed;
  1774. * -2 - no disk space.
  1775. */
  1776. static int check_balance(int mode,
  1777. struct tree_balance *tb,
  1778. int h,
  1779. int inum,
  1780. int pos_in_item,
  1781. struct item_head *ins_ih, const void *data)
  1782. {
  1783. struct virtual_node *vn;
  1784. vn = tb->tb_vn = (struct virtual_node *)(tb->vn_buf);
  1785. vn->vn_free_ptr = (char *)(tb->tb_vn + 1);
  1786. vn->vn_mode = mode;
  1787. vn->vn_affected_item_num = inum;
  1788. vn->vn_pos_in_item = pos_in_item;
  1789. vn->vn_ins_ih = ins_ih;
  1790. vn->vn_data = data;
  1791. RFALSE(mode == M_INSERT && !vn->vn_ins_ih,
  1792. "vs-8255: ins_ih can not be 0 in insert mode");
  1793. /* Calculate balance parameters when size of node is increasing. */
  1794. if (tb->insert_size[h] > 0)
  1795. return ip_check_balance(tb, h);
  1796. /* Calculate balance parameters when size of node is decreasing. */
  1797. return dc_check_balance(tb, h);
  1798. }
  1799. /* Check whether parent at the path is the really parent of the current node.*/
  1800. static int get_direct_parent(struct tree_balance *tb, int h)
  1801. {
  1802. struct buffer_head *bh;
  1803. struct treepath *path = tb->tb_path;
  1804. int position,
  1805. path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h);
  1806. /* We are in the root or in the new root. */
  1807. if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
  1808. RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET - 1,
  1809. "PAP-8260: invalid offset in the path");
  1810. if (PATH_OFFSET_PBUFFER(path, FIRST_PATH_ELEMENT_OFFSET)->
  1811. b_blocknr == SB_ROOT_BLOCK(tb->tb_sb)) {
  1812. /* Root is not changed. */
  1813. PATH_OFFSET_PBUFFER(path, path_offset - 1) = NULL;
  1814. PATH_OFFSET_POSITION(path, path_offset - 1) = 0;
  1815. return CARRY_ON;
  1816. }
  1817. /* Root is changed and we must recalculate the path. */
  1818. return REPEAT_SEARCH;
  1819. }
  1820. /* Parent in the path is not in the tree. */
  1821. if (!B_IS_IN_TREE
  1822. (bh = PATH_OFFSET_PBUFFER(path, path_offset - 1)))
  1823. return REPEAT_SEARCH;
  1824. if ((position =
  1825. PATH_OFFSET_POSITION(path,
  1826. path_offset - 1)) > B_NR_ITEMS(bh))
  1827. return REPEAT_SEARCH;
  1828. /* Parent in the path is not parent of the current node in the tree. */
  1829. if (B_N_CHILD_NUM(bh, position) !=
  1830. PATH_OFFSET_PBUFFER(path, path_offset)->b_blocknr)
  1831. return REPEAT_SEARCH;
  1832. if (buffer_locked(bh)) {
  1833. int depth = reiserfs_write_unlock_nested(tb->tb_sb);
  1834. __wait_on_buffer(bh);
  1835. reiserfs_write_lock_nested(tb->tb_sb, depth);
  1836. if (FILESYSTEM_CHANGED_TB(tb))
  1837. return REPEAT_SEARCH;
  1838. }
  1839. /*
  1840. * Parent in the path is unlocked and really parent
  1841. * of the current node.
  1842. */
  1843. return CARRY_ON;
  1844. }
  1845. /*
  1846. * Using lnum[h] and rnum[h] we should determine what neighbors
  1847. * of S[h] we
  1848. * need in order to balance S[h], and get them if necessary.
  1849. * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked;
  1850. * CARRY_ON - schedule didn't occur while the function worked;
  1851. */
  1852. static int get_neighbors(struct tree_balance *tb, int h)
  1853. {
  1854. int child_position,
  1855. path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h + 1);
  1856. unsigned long son_number;
  1857. struct super_block *sb = tb->tb_sb;
  1858. struct buffer_head *bh;
  1859. int depth;
  1860. PROC_INFO_INC(sb, get_neighbors[h]);
  1861. if (tb->lnum[h]) {
  1862. /* We need left neighbor to balance S[h]. */
  1863. PROC_INFO_INC(sb, need_l_neighbor[h]);
  1864. bh = PATH_OFFSET_PBUFFER(tb->tb_path, path_offset);
  1865. RFALSE(bh == tb->FL[h] &&
  1866. !PATH_OFFSET_POSITION(tb->tb_path, path_offset),
  1867. "PAP-8270: invalid position in the parent");
  1868. child_position =
  1869. (bh ==
  1870. tb->FL[h]) ? tb->lkey[h] : B_NR_ITEMS(tb->
  1871. FL[h]);
  1872. son_number = B_N_CHILD_NUM(tb->FL[h], child_position);
  1873. depth = reiserfs_write_unlock_nested(tb->tb_sb);
  1874. bh = sb_bread(sb, son_number);
  1875. reiserfs_write_lock_nested(tb->tb_sb, depth);
  1876. if (!bh)
  1877. return IO_ERROR;
  1878. if (FILESYSTEM_CHANGED_TB(tb)) {
  1879. brelse(bh);
  1880. PROC_INFO_INC(sb, get_neighbors_restart[h]);
  1881. return REPEAT_SEARCH;
  1882. }
  1883. RFALSE(!B_IS_IN_TREE(tb->FL[h]) ||
  1884. child_position > B_NR_ITEMS(tb->FL[h]) ||
  1885. B_N_CHILD_NUM(tb->FL[h], child_position) !=
  1886. bh->b_blocknr, "PAP-8275: invalid parent");
  1887. RFALSE(!B_IS_IN_TREE(bh), "PAP-8280: invalid child");
  1888. RFALSE(!h &&
  1889. B_FREE_SPACE(bh) !=
  1890. MAX_CHILD_SIZE(bh) -
  1891. dc_size(B_N_CHILD(tb->FL[0], child_position)),
  1892. "PAP-8290: invalid child size of left neighbor");
  1893. brelse(tb->L[h]);
  1894. tb->L[h] = bh;
  1895. }
  1896. /* We need right neighbor to balance S[path_offset]. */
  1897. if (tb->rnum[h]) {
  1898. PROC_INFO_INC(sb, need_r_neighbor[h]);
  1899. bh = PATH_OFFSET_PBUFFER(tb->tb_path, path_offset);
  1900. RFALSE(bh == tb->FR[h] &&
  1901. PATH_OFFSET_POSITION(tb->tb_path,
  1902. path_offset) >=
  1903. B_NR_ITEMS(bh),
  1904. "PAP-8295: invalid position in the parent");
  1905. child_position =
  1906. (bh == tb->FR[h]) ? tb->rkey[h] + 1 : 0;
  1907. son_number = B_N_CHILD_NUM(tb->FR[h], child_position);
  1908. depth = reiserfs_write_unlock_nested(tb->tb_sb);
  1909. bh = sb_bread(sb, son_number);
  1910. reiserfs_write_lock_nested(tb->tb_sb, depth);
  1911. if (!bh)
  1912. return IO_ERROR;
  1913. if (FILESYSTEM_CHANGED_TB(tb)) {
  1914. brelse(bh);
  1915. PROC_INFO_INC(sb, get_neighbors_restart[h]);
  1916. return REPEAT_SEARCH;
  1917. }
  1918. brelse(tb->R[h]);
  1919. tb->R[h] = bh;
  1920. RFALSE(!h
  1921. && B_FREE_SPACE(bh) !=
  1922. MAX_CHILD_SIZE(bh) -
  1923. dc_size(B_N_CHILD(tb->FR[0], child_position)),
  1924. "PAP-8300: invalid child size of right neighbor (%d != %d - %d)",
  1925. B_FREE_SPACE(bh), MAX_CHILD_SIZE(bh),
  1926. dc_size(B_N_CHILD(tb->FR[0], child_position)));
  1927. }
  1928. return CARRY_ON;
  1929. }
  1930. static int get_virtual_node_size(struct super_block *sb, struct buffer_head *bh)
  1931. {
  1932. int max_num_of_items;
  1933. int max_num_of_entries;
  1934. unsigned long blocksize = sb->s_blocksize;
  1935. #define MIN_NAME_LEN 1
  1936. max_num_of_items = (blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN);
  1937. max_num_of_entries = (blocksize - BLKH_SIZE - IH_SIZE) /
  1938. (DEH_SIZE + MIN_NAME_LEN);
  1939. return sizeof(struct virtual_node) +
  1940. max(max_num_of_items * sizeof(struct virtual_item),
  1941. sizeof(struct virtual_item) + sizeof(struct direntry_uarea) +
  1942. (max_num_of_entries - 1) * sizeof(__u16));
  1943. }
  1944. /*
  1945. * maybe we should fail balancing we are going to perform when kmalloc
  1946. * fails several times. But now it will loop until kmalloc gets
  1947. * required memory
  1948. */
  1949. static int get_mem_for_virtual_node(struct tree_balance *tb)
  1950. {
  1951. int check_fs = 0;
  1952. int size;
  1953. char *buf;
  1954. size = get_virtual_node_size(tb->tb_sb, PATH_PLAST_BUFFER(tb->tb_path));
  1955. /* we have to allocate more memory for virtual node */
  1956. if (size > tb->vn_buf_size) {
  1957. if (tb->vn_buf) {
  1958. /* free memory allocated before */
  1959. kfree(tb->vn_buf);
  1960. /* this is not needed if kfree is atomic */
  1961. check_fs = 1;
  1962. }
  1963. /* virtual node requires now more memory */
  1964. tb->vn_buf_size = size;
  1965. /* get memory for virtual item */
  1966. buf = kmalloc(size, GFP_ATOMIC | __GFP_NOWARN);
  1967. if (!buf) {
  1968. /*
  1969. * getting memory with GFP_KERNEL priority may involve
  1970. * balancing now (due to indirect_to_direct conversion
  1971. * on dcache shrinking). So, release path and collected
  1972. * resources here
  1973. */
  1974. free_buffers_in_tb(tb);
  1975. buf = kmalloc(size, GFP_NOFS);
  1976. if (!buf) {
  1977. tb->vn_buf_size = 0;
  1978. }
  1979. tb->vn_buf = buf;
  1980. schedule();
  1981. return REPEAT_SEARCH;
  1982. }
  1983. tb->vn_buf = buf;
  1984. }
  1985. if (check_fs && FILESYSTEM_CHANGED_TB(tb))
  1986. return REPEAT_SEARCH;
  1987. return CARRY_ON;
  1988. }
  1989. #ifdef CONFIG_REISERFS_CHECK
  1990. static void tb_buffer_sanity_check(struct super_block *sb,
  1991. struct buffer_head *bh,
  1992. const char *descr, int level)
  1993. {
  1994. if (bh) {
  1995. if (atomic_read(&(bh->b_count)) <= 0)
  1996. reiserfs_panic(sb, "jmacd-1", "negative or zero "
  1997. "reference counter for buffer %s[%d] "
  1998. "(%b)", descr, level, bh);
  1999. if (!buffer_uptodate(bh))
  2000. reiserfs_panic(sb, "jmacd-2", "buffer is not up "
  2001. "to date %s[%d] (%b)",
  2002. descr, level, bh);
  2003. if (!B_IS_IN_TREE(bh))
  2004. reiserfs_panic(sb, "jmacd-3", "buffer is not "
  2005. "in tree %s[%d] (%b)",
  2006. descr, level, bh);
  2007. if (bh->b_bdev != sb->s_bdev)
  2008. reiserfs_panic(sb, "jmacd-4", "buffer has wrong "
  2009. "device %s[%d] (%b)",
  2010. descr, level, bh);
  2011. if (bh->b_size != sb->s_blocksize)
  2012. reiserfs_panic(sb, "jmacd-5", "buffer has wrong "
  2013. "blocksize %s[%d] (%b)",
  2014. descr, level, bh);
  2015. if (bh->b_blocknr > SB_BLOCK_COUNT(sb))
  2016. reiserfs_panic(sb, "jmacd-6", "buffer block "
  2017. "number too high %s[%d] (%b)",
  2018. descr, level, bh);
  2019. }
  2020. }
  2021. #else
  2022. static void tb_buffer_sanity_check(struct super_block *sb,
  2023. struct buffer_head *bh,
  2024. const char *descr, int level)
  2025. {;
  2026. }
  2027. #endif
  2028. static int clear_all_dirty_bits(struct super_block *s, struct buffer_head *bh)
  2029. {
  2030. return reiserfs_prepare_for_journal(s, bh, 0);
  2031. }
  2032. static int wait_tb_buffers_until_unlocked(struct tree_balance *tb)
  2033. {
  2034. struct buffer_head *locked;
  2035. #ifdef CONFIG_REISERFS_CHECK
  2036. int repeat_counter = 0;
  2037. #endif
  2038. int i;
  2039. do {
  2040. locked = NULL;
  2041. for (i = tb->tb_path->path_length;
  2042. !locked && i > ILLEGAL_PATH_ELEMENT_OFFSET; i--) {
  2043. if (PATH_OFFSET_PBUFFER(tb->tb_path, i)) {
  2044. /*
  2045. * if I understand correctly, we can only
  2046. * be sure the last buffer in the path is
  2047. * in the tree --clm
  2048. */
  2049. #ifdef CONFIG_REISERFS_CHECK
  2050. if (PATH_PLAST_BUFFER(tb->tb_path) ==
  2051. PATH_OFFSET_PBUFFER(tb->tb_path, i))
  2052. tb_buffer_sanity_check(tb->tb_sb,
  2053. PATH_OFFSET_PBUFFER
  2054. (tb->tb_path,
  2055. i), "S",
  2056. tb->tb_path->
  2057. path_length - i);
  2058. #endif
  2059. if (!clear_all_dirty_bits(tb->tb_sb,
  2060. PATH_OFFSET_PBUFFER
  2061. (tb->tb_path,
  2062. i))) {
  2063. locked =
  2064. PATH_OFFSET_PBUFFER(tb->tb_path,
  2065. i);
  2066. }
  2067. }
  2068. }
  2069. for (i = 0; !locked && i < MAX_HEIGHT && tb->insert_size[i];
  2070. i++) {
  2071. if (tb->lnum[i]) {
  2072. if (tb->L[i]) {
  2073. tb_buffer_sanity_check(tb->tb_sb,
  2074. tb->L[i],
  2075. "L", i);
  2076. if (!clear_all_dirty_bits
  2077. (tb->tb_sb, tb->L[i]))
  2078. locked = tb->L[i];
  2079. }
  2080. if (!locked && tb->FL[i]) {
  2081. tb_buffer_sanity_check(tb->tb_sb,
  2082. tb->FL[i],
  2083. "FL", i);
  2084. if (!clear_all_dirty_bits
  2085. (tb->tb_sb, tb->FL[i]))
  2086. locked = tb->FL[i];
  2087. }
  2088. if (!locked && tb->CFL[i]) {
  2089. tb_buffer_sanity_check(tb->tb_sb,
  2090. tb->CFL[i],
  2091. "CFL", i);
  2092. if (!clear_all_dirty_bits
  2093. (tb->tb_sb, tb->CFL[i]))
  2094. locked = tb->CFL[i];
  2095. }
  2096. }
  2097. if (!locked && (tb->rnum[i])) {
  2098. if (tb->R[i]) {
  2099. tb_buffer_sanity_check(tb->tb_sb,
  2100. tb->R[i],
  2101. "R", i);
  2102. if (!clear_all_dirty_bits
  2103. (tb->tb_sb, tb->R[i]))
  2104. locked = tb->R[i];
  2105. }
  2106. if (!locked && tb->FR[i]) {
  2107. tb_buffer_sanity_check(tb->tb_sb,
  2108. tb->FR[i],
  2109. "FR", i);
  2110. if (!clear_all_dirty_bits
  2111. (tb->tb_sb, tb->FR[i]))
  2112. locked = tb->FR[i];
  2113. }
  2114. if (!locked && tb->CFR[i]) {
  2115. tb_buffer_sanity_check(tb->tb_sb,
  2116. tb->CFR[i],
  2117. "CFR", i);
  2118. if (!clear_all_dirty_bits
  2119. (tb->tb_sb, tb->CFR[i]))
  2120. locked = tb->CFR[i];
  2121. }
  2122. }
  2123. }
  2124. /*
  2125. * as far as I can tell, this is not required. The FEB list
  2126. * seems to be full of newly allocated nodes, which will
  2127. * never be locked, dirty, or anything else.
  2128. * To be safe, I'm putting in the checks and waits in.
  2129. * For the moment, they are needed to keep the code in
  2130. * journal.c from complaining about the buffer.
  2131. * That code is inside CONFIG_REISERFS_CHECK as well. --clm
  2132. */
  2133. for (i = 0; !locked && i < MAX_FEB_SIZE; i++) {
  2134. if (tb->FEB[i]) {
  2135. if (!clear_all_dirty_bits
  2136. (tb->tb_sb, tb->FEB[i]))
  2137. locked = tb->FEB[i];
  2138. }
  2139. }
  2140. if (locked) {
  2141. int depth;
  2142. #ifdef CONFIG_REISERFS_CHECK
  2143. repeat_counter++;
  2144. if ((repeat_counter % 10000) == 0) {
  2145. reiserfs_warning(tb->tb_sb, "reiserfs-8200",
  2146. "too many iterations waiting "
  2147. "for buffer to unlock "
  2148. "(%b)", locked);
  2149. /* Don't loop forever. Try to recover from possible error. */
  2150. return (FILESYSTEM_CHANGED_TB(tb)) ?
  2151. REPEAT_SEARCH : CARRY_ON;
  2152. }
  2153. #endif
  2154. depth = reiserfs_write_unlock_nested(tb->tb_sb);
  2155. __wait_on_buffer(locked);
  2156. reiserfs_write_lock_nested(tb->tb_sb, depth);
  2157. if (FILESYSTEM_CHANGED_TB(tb))
  2158. return REPEAT_SEARCH;
  2159. }
  2160. } while (locked);
  2161. return CARRY_ON;
  2162. }
  2163. /*
  2164. * Prepare for balancing, that is
  2165. * get all necessary parents, and neighbors;
  2166. * analyze what and where should be moved;
  2167. * get sufficient number of new nodes;
  2168. * Balancing will start only after all resources will be collected at a time.
  2169. *
  2170. * When ported to SMP kernels, only at the last moment after all needed nodes
  2171. * are collected in cache, will the resources be locked using the usual
  2172. * textbook ordered lock acquisition algorithms. Note that ensuring that
  2173. * this code neither write locks what it does not need to write lock nor locks
  2174. * out of order will be a pain in the butt that could have been avoided.
  2175. * Grumble grumble. -Hans
  2176. *
  2177. * fix is meant in the sense of render unchanging
  2178. *
  2179. * Latency might be improved by first gathering a list of what buffers
  2180. * are needed and then getting as many of them in parallel as possible? -Hans
  2181. *
  2182. * Parameters:
  2183. * op_mode i - insert, d - delete, c - cut (truncate), p - paste (append)
  2184. * tb tree_balance structure;
  2185. * inum item number in S[h];
  2186. * pos_in_item - comment this if you can
  2187. * ins_ih item head of item being inserted
  2188. * data inserted item or data to be pasted
  2189. * Returns: 1 - schedule occurred while the function worked;
  2190. * 0 - schedule didn't occur while the function worked;
  2191. * -1 - if no_disk_space
  2192. */
  2193. int fix_nodes(int op_mode, struct tree_balance *tb,
  2194. struct item_head *ins_ih, const void *data)
  2195. {
  2196. int ret, h, item_num = PATH_LAST_POSITION(tb->tb_path);
  2197. int pos_in_item;
  2198. /*
  2199. * we set wait_tb_buffers_run when we have to restore any dirty
  2200. * bits cleared during wait_tb_buffers_run
  2201. */
  2202. int wait_tb_buffers_run = 0;
  2203. struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
  2204. ++REISERFS_SB(tb->tb_sb)->s_fix_nodes;
  2205. pos_in_item = tb->tb_path->pos_in_item;
  2206. tb->fs_gen = get_generation(tb->tb_sb);
  2207. /*
  2208. * we prepare and log the super here so it will already be in the
  2209. * transaction when do_balance needs to change it.
  2210. * This way do_balance won't have to schedule when trying to prepare
  2211. * the super for logging
  2212. */
  2213. reiserfs_prepare_for_journal(tb->tb_sb,
  2214. SB_BUFFER_WITH_SB(tb->tb_sb), 1);
  2215. journal_mark_dirty(tb->transaction_handle,
  2216. SB_BUFFER_WITH_SB(tb->tb_sb));
  2217. if (FILESYSTEM_CHANGED_TB(tb))
  2218. return REPEAT_SEARCH;
  2219. /* if it possible in indirect_to_direct conversion */
  2220. if (buffer_locked(tbS0)) {
  2221. int depth = reiserfs_write_unlock_nested(tb->tb_sb);
  2222. __wait_on_buffer(tbS0);
  2223. reiserfs_write_lock_nested(tb->tb_sb, depth);
  2224. if (FILESYSTEM_CHANGED_TB(tb))
  2225. return REPEAT_SEARCH;
  2226. }
  2227. #ifdef CONFIG_REISERFS_CHECK
  2228. if (REISERFS_SB(tb->tb_sb)->cur_tb) {
  2229. print_cur_tb("fix_nodes");
  2230. reiserfs_panic(tb->tb_sb, "PAP-8305",
  2231. "there is pending do_balance");
  2232. }
  2233. if (!buffer_uptodate(tbS0) || !B_IS_IN_TREE(tbS0))
  2234. reiserfs_panic(tb->tb_sb, "PAP-8320", "S[0] (%b %z) is "
  2235. "not uptodate at the beginning of fix_nodes "
  2236. "or not in tree (mode %c)",
  2237. tbS0, tbS0, op_mode);
  2238. /* Check parameters. */
  2239. switch (op_mode) {
  2240. case M_INSERT:
  2241. if (item_num <= 0 || item_num > B_NR_ITEMS(tbS0))
  2242. reiserfs_panic(tb->tb_sb, "PAP-8330", "Incorrect "
  2243. "item number %d (in S0 - %d) in case "
  2244. "of insert", item_num,
  2245. B_NR_ITEMS(tbS0));
  2246. break;
  2247. case M_PASTE:
  2248. case M_DELETE:
  2249. case M_CUT:
  2250. if (item_num < 0 || item_num >= B_NR_ITEMS(tbS0)) {
  2251. print_block(tbS0, 0, -1, -1);
  2252. reiserfs_panic(tb->tb_sb, "PAP-8335", "Incorrect "
  2253. "item number(%d); mode = %c "
  2254. "insert_size = %d",
  2255. item_num, op_mode,
  2256. tb->insert_size[0]);
  2257. }
  2258. break;
  2259. default:
  2260. reiserfs_panic(tb->tb_sb, "PAP-8340", "Incorrect mode "
  2261. "of operation");
  2262. }
  2263. #endif
  2264. if (get_mem_for_virtual_node(tb) == REPEAT_SEARCH)
  2265. /* FIXME: maybe -ENOMEM when tb->vn_buf == 0? Now just repeat */
  2266. return REPEAT_SEARCH;
  2267. /* Starting from the leaf level; for all levels h of the tree. */
  2268. for (h = 0; h < MAX_HEIGHT && tb->insert_size[h]; h++) {
  2269. ret = get_direct_parent(tb, h);
  2270. if (ret != CARRY_ON)
  2271. goto repeat;
  2272. ret = check_balance(op_mode, tb, h, item_num,
  2273. pos_in_item, ins_ih, data);
  2274. if (ret != CARRY_ON) {
  2275. if (ret == NO_BALANCING_NEEDED) {
  2276. /* No balancing for higher levels needed. */
  2277. ret = get_neighbors(tb, h);
  2278. if (ret != CARRY_ON)
  2279. goto repeat;
  2280. if (h != MAX_HEIGHT - 1)
  2281. tb->insert_size[h + 1] = 0;
  2282. /*
  2283. * ok, analysis and resource gathering
  2284. * are complete
  2285. */
  2286. break;
  2287. }
  2288. goto repeat;
  2289. }
  2290. ret = get_neighbors(tb, h);
  2291. if (ret != CARRY_ON)
  2292. goto repeat;
  2293. /*
  2294. * No disk space, or schedule occurred and analysis may be
  2295. * invalid and needs to be redone.
  2296. */
  2297. ret = get_empty_nodes(tb, h);
  2298. if (ret != CARRY_ON)
  2299. goto repeat;
  2300. /*
  2301. * We have a positive insert size but no nodes exist on this
  2302. * level, this means that we are creating a new root.
  2303. */
  2304. if (!PATH_H_PBUFFER(tb->tb_path, h)) {
  2305. RFALSE(tb->blknum[h] != 1,
  2306. "PAP-8350: creating new empty root");
  2307. if (h < MAX_HEIGHT - 1)
  2308. tb->insert_size[h + 1] = 0;
  2309. } else if (!PATH_H_PBUFFER(tb->tb_path, h + 1)) {
  2310. /*
  2311. * The tree needs to be grown, so this node S[h]
  2312. * which is the root node is split into two nodes,
  2313. * and a new node (S[h+1]) will be created to
  2314. * become the root node.
  2315. */
  2316. if (tb->blknum[h] > 1) {
  2317. RFALSE(h == MAX_HEIGHT - 1,
  2318. "PAP-8355: attempt to create too high of a tree");
  2319. tb->insert_size[h + 1] =
  2320. (DC_SIZE +
  2321. KEY_SIZE) * (tb->blknum[h] - 1) +
  2322. DC_SIZE;
  2323. } else if (h < MAX_HEIGHT - 1)
  2324. tb->insert_size[h + 1] = 0;
  2325. } else
  2326. tb->insert_size[h + 1] =
  2327. (DC_SIZE + KEY_SIZE) * (tb->blknum[h] - 1);
  2328. }
  2329. ret = wait_tb_buffers_until_unlocked(tb);
  2330. if (ret == CARRY_ON) {
  2331. if (FILESYSTEM_CHANGED_TB(tb)) {
  2332. wait_tb_buffers_run = 1;
  2333. ret = REPEAT_SEARCH;
  2334. goto repeat;
  2335. } else {
  2336. return CARRY_ON;
  2337. }
  2338. } else {
  2339. wait_tb_buffers_run = 1;
  2340. goto repeat;
  2341. }
  2342. repeat:
  2343. /*
  2344. * fix_nodes was unable to perform its calculation due to
  2345. * filesystem got changed under us, lack of free disk space or i/o
  2346. * failure. If the first is the case - the search will be
  2347. * repeated. For now - free all resources acquired so far except
  2348. * for the new allocated nodes
  2349. */
  2350. {
  2351. int i;
  2352. /* Release path buffers. */
  2353. if (wait_tb_buffers_run) {
  2354. pathrelse_and_restore(tb->tb_sb, tb->tb_path);
  2355. } else {
  2356. pathrelse(tb->tb_path);
  2357. }
  2358. /* brelse all resources collected for balancing */
  2359. for (i = 0; i < MAX_HEIGHT; i++) {
  2360. if (wait_tb_buffers_run) {
  2361. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2362. tb->L[i]);
  2363. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2364. tb->R[i]);
  2365. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2366. tb->FL[i]);
  2367. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2368. tb->FR[i]);
  2369. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2370. tb->
  2371. CFL[i]);
  2372. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2373. tb->
  2374. CFR[i]);
  2375. }
  2376. brelse(tb->L[i]);
  2377. brelse(tb->R[i]);
  2378. brelse(tb->FL[i]);
  2379. brelse(tb->FR[i]);
  2380. brelse(tb->CFL[i]);
  2381. brelse(tb->CFR[i]);
  2382. tb->L[i] = NULL;
  2383. tb->R[i] = NULL;
  2384. tb->FL[i] = NULL;
  2385. tb->FR[i] = NULL;
  2386. tb->CFL[i] = NULL;
  2387. tb->CFR[i] = NULL;
  2388. }
  2389. if (wait_tb_buffers_run) {
  2390. for (i = 0; i < MAX_FEB_SIZE; i++) {
  2391. if (tb->FEB[i])
  2392. reiserfs_restore_prepared_buffer
  2393. (tb->tb_sb, tb->FEB[i]);
  2394. }
  2395. }
  2396. return ret;
  2397. }
  2398. }
  2399. void unfix_nodes(struct tree_balance *tb)
  2400. {
  2401. int i;
  2402. /* Release path buffers. */
  2403. pathrelse_and_restore(tb->tb_sb, tb->tb_path);
  2404. /* brelse all resources collected for balancing */
  2405. for (i = 0; i < MAX_HEIGHT; i++) {
  2406. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->L[i]);
  2407. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->R[i]);
  2408. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->FL[i]);
  2409. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->FR[i]);
  2410. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->CFL[i]);
  2411. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->CFR[i]);
  2412. brelse(tb->L[i]);
  2413. brelse(tb->R[i]);
  2414. brelse(tb->FL[i]);
  2415. brelse(tb->FR[i]);
  2416. brelse(tb->CFL[i]);
  2417. brelse(tb->CFR[i]);
  2418. }
  2419. /* deal with list of allocated (used and unused) nodes */
  2420. for (i = 0; i < MAX_FEB_SIZE; i++) {
  2421. if (tb->FEB[i]) {
  2422. b_blocknr_t blocknr = tb->FEB[i]->b_blocknr;
  2423. /*
  2424. * de-allocated block which was not used by
  2425. * balancing and bforget about buffer for it
  2426. */
  2427. brelse(tb->FEB[i]);
  2428. reiserfs_free_block(tb->transaction_handle, NULL,
  2429. blocknr, 0);
  2430. }
  2431. if (tb->used[i]) {
  2432. /* release used as new nodes including a new root */
  2433. brelse(tb->used[i]);
  2434. }
  2435. }
  2436. kfree(tb->vn_buf);
  2437. }