do_balan.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. /* Now we have all buffers that must be used in balancing of the tree */
  5. /* Further calculations can not cause schedule(), and thus the buffer */
  6. /* tree will be stable until the balancing will be finished */
  7. /* balance the tree according to the analysis made before, */
  8. /* and using buffers obtained after all above. */
  9. /**
  10. ** balance_leaf_when_delete
  11. ** balance_leaf
  12. ** do_balance
  13. **
  14. **/
  15. #include <asm/uaccess.h>
  16. #include <linux/time.h>
  17. #include <linux/reiserfs_fs.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/kernel.h>
  20. static inline void buffer_info_init_left(struct tree_balance *tb,
  21. struct buffer_info *bi)
  22. {
  23. bi->tb = tb;
  24. bi->bi_bh = tb->L[0];
  25. bi->bi_parent = tb->FL[0];
  26. bi->bi_position = get_left_neighbor_position(tb, 0);
  27. }
  28. static inline void buffer_info_init_right(struct tree_balance *tb,
  29. struct buffer_info *bi)
  30. {
  31. bi->tb = tb;
  32. bi->bi_bh = tb->R[0];
  33. bi->bi_parent = tb->FR[0];
  34. bi->bi_position = get_right_neighbor_position(tb, 0);
  35. }
  36. static inline void buffer_info_init_tbS0(struct tree_balance *tb,
  37. struct buffer_info *bi)
  38. {
  39. bi->tb = tb;
  40. bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
  41. bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
  42. bi->bi_position = PATH_H_POSITION(tb->tb_path, 1);
  43. }
  44. static inline void buffer_info_init_bh(struct tree_balance *tb,
  45. struct buffer_info *bi,
  46. struct buffer_head *bh)
  47. {
  48. bi->tb = tb;
  49. bi->bi_bh = bh;
  50. bi->bi_parent = NULL;
  51. bi->bi_position = 0;
  52. }
  53. inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
  54. struct buffer_head *bh, int flag)
  55. {
  56. journal_mark_dirty(tb->transaction_handle,
  57. tb->transaction_handle->t_super, bh);
  58. }
  59. #define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty
  60. #define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty
  61. /* summary:
  62. if deleting something ( tb->insert_size[0] < 0 )
  63. return(balance_leaf_when_delete()); (flag d handled here)
  64. else
  65. if lnum is larger than 0 we put items into the left node
  66. if rnum is larger than 0 we put items into the right node
  67. if snum1 is larger than 0 we put items into the new node s1
  68. if snum2 is larger than 0 we put items into the new node s2
  69. Note that all *num* count new items being created.
  70. It would be easier to read balance_leaf() if each of these summary
  71. lines was a separate procedure rather than being inlined. I think
  72. that there are many passages here and in balance_leaf_when_delete() in
  73. which two calls to one procedure can replace two passages, and it
  74. might save cache space and improve software maintenance costs to do so.
  75. Vladimir made the perceptive comment that we should offload most of
  76. the decision making in this function into fix_nodes/check_balance, and
  77. then create some sort of structure in tb that says what actions should
  78. be performed by do_balance.
  79. -Hans */
  80. /* Balance leaf node in case of delete or cut: insert_size[0] < 0
  81. *
  82. * lnum, rnum can have values >= -1
  83. * -1 means that the neighbor must be joined with S
  84. * 0 means that nothing should be done with the neighbor
  85. * >0 means to shift entirely or partly the specified number of items to the neighbor
  86. */
  87. static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
  88. {
  89. struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
  90. int item_pos = PATH_LAST_POSITION(tb->tb_path);
  91. int pos_in_item = tb->tb_path->pos_in_item;
  92. struct buffer_info bi;
  93. int n;
  94. struct item_head *ih;
  95. RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
  96. "vs- 12000: level: wrong FR %z", tb->FR[0]);
  97. RFALSE(tb->blknum[0] > 1,
  98. "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
  99. RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
  100. "PAP-12010: tree can not be empty");
  101. ih = B_N_PITEM_HEAD(tbS0, item_pos);
  102. buffer_info_init_tbS0(tb, &bi);
  103. /* Delete or truncate the item */
  104. switch (flag) {
  105. case M_DELETE: /* delete item in S[0] */
  106. RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
  107. "vs-12013: mode Delete, insert size %d, ih to be deleted %h",
  108. -tb->insert_size[0], ih);
  109. leaf_delete_items(&bi, 0, item_pos, 1, -1);
  110. if (!item_pos && tb->CFL[0]) {
  111. if (B_NR_ITEMS(tbS0)) {
  112. replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0,
  113. 0);
  114. } else {
  115. if (!PATH_H_POSITION(tb->tb_path, 1))
  116. replace_key(tb, tb->CFL[0], tb->lkey[0],
  117. PATH_H_PPARENT(tb->tb_path,
  118. 0), 0);
  119. }
  120. }
  121. RFALSE(!item_pos && !tb->CFL[0],
  122. "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
  123. tb->L[0]);
  124. break;
  125. case M_CUT:{ /* cut item in S[0] */
  126. if (is_direntry_le_ih(ih)) {
  127. /* UFS unlink semantics are such that you can only delete one directory entry at a time. */
  128. /* when we cut a directory tb->insert_size[0] means number of entries to be cut (always 1) */
  129. tb->insert_size[0] = -1;
  130. leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
  131. -tb->insert_size[0]);
  132. RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
  133. "PAP-12030: can not change delimiting key. CFL[0]=%p",
  134. tb->CFL[0]);
  135. if (!item_pos && !pos_in_item && tb->CFL[0]) {
  136. replace_key(tb, tb->CFL[0], tb->lkey[0],
  137. tbS0, 0);
  138. }
  139. } else {
  140. leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
  141. -tb->insert_size[0]);
  142. RFALSE(!ih_item_len(ih),
  143. "PAP-12035: cut must leave non-zero dynamic length of item");
  144. }
  145. break;
  146. }
  147. default:
  148. print_cur_tb("12040");
  149. reiserfs_panic(tb->tb_sb, "PAP-12040",
  150. "unexpected mode: %s(%d)",
  151. (flag ==
  152. M_PASTE) ? "PASTE" : ((flag ==
  153. M_INSERT) ? "INSERT" :
  154. "UNKNOWN"), flag);
  155. }
  156. /* the rule is that no shifting occurs unless by shifting a node can be freed */
  157. n = B_NR_ITEMS(tbS0);
  158. if (tb->lnum[0]) { /* L[0] takes part in balancing */
  159. if (tb->lnum[0] == -1) { /* L[0] must be joined with S[0] */
  160. if (tb->rnum[0] == -1) { /* R[0] must be also joined with S[0] */
  161. if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
  162. /* all contents of all the 3 buffers will be in L[0] */
  163. if (PATH_H_POSITION(tb->tb_path, 1) == 0
  164. && 1 < B_NR_ITEMS(tb->FR[0]))
  165. replace_key(tb, tb->CFL[0],
  166. tb->lkey[0],
  167. tb->FR[0], 1);
  168. leaf_move_items(LEAF_FROM_S_TO_L, tb, n,
  169. -1, NULL);
  170. leaf_move_items(LEAF_FROM_R_TO_L, tb,
  171. B_NR_ITEMS(tb->R[0]),
  172. -1, NULL);
  173. reiserfs_invalidate_buffer(tb, tbS0);
  174. reiserfs_invalidate_buffer(tb,
  175. tb->R[0]);
  176. return 0;
  177. }
  178. /* all contents of all the 3 buffers will be in R[0] */
  179. leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1,
  180. NULL);
  181. leaf_move_items(LEAF_FROM_L_TO_R, tb,
  182. B_NR_ITEMS(tb->L[0]), -1, NULL);
  183. /* right_delimiting_key is correct in R[0] */
  184. replace_key(tb, tb->CFR[0], tb->rkey[0],
  185. tb->R[0], 0);
  186. reiserfs_invalidate_buffer(tb, tbS0);
  187. reiserfs_invalidate_buffer(tb, tb->L[0]);
  188. return -1;
  189. }
  190. RFALSE(tb->rnum[0] != 0,
  191. "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
  192. /* all contents of L[0] and S[0] will be in L[0] */
  193. leaf_shift_left(tb, n, -1);
  194. reiserfs_invalidate_buffer(tb, tbS0);
  195. return 0;
  196. }
  197. /* a part of contents of S[0] will be in L[0] and the rest part of S[0] will be in R[0] */
  198. RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
  199. (tb->lnum[0] + tb->rnum[0] > n + 1),
  200. "PAP-12050: rnum(%d) and lnum(%d) and item number(%d) in S[0] are not consistent",
  201. tb->rnum[0], tb->lnum[0], n);
  202. RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
  203. (tb->lbytes != -1 || tb->rbytes != -1),
  204. "PAP-12055: bad rbytes (%d)/lbytes (%d) parameters when items are not split",
  205. tb->rbytes, tb->lbytes);
  206. RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
  207. (tb->lbytes < 1 || tb->rbytes != -1),
  208. "PAP-12060: bad rbytes (%d)/lbytes (%d) parameters when items are split",
  209. tb->rbytes, tb->lbytes);
  210. leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
  211. leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
  212. reiserfs_invalidate_buffer(tb, tbS0);
  213. return 0;
  214. }
  215. if (tb->rnum[0] == -1) {
  216. /* all contents of R[0] and S[0] will be in R[0] */
  217. leaf_shift_right(tb, n, -1);
  218. reiserfs_invalidate_buffer(tb, tbS0);
  219. return 0;
  220. }
  221. RFALSE(tb->rnum[0],
  222. "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
  223. return 0;
  224. }
  225. static int balance_leaf(struct tree_balance *tb, struct item_head *ih, /* item header of inserted item (this is on little endian) */
  226. const char *body, /* body of inserted item or bytes to paste */
  227. int flag, /* i - insert, d - delete, c - cut, p - paste
  228. (see comment to do_balance) */
  229. struct item_head *insert_key, /* in our processing of one level we sometimes determine what
  230. must be inserted into the next higher level. This insertion
  231. consists of a key or two keys and their corresponding
  232. pointers */
  233. struct buffer_head **insert_ptr /* inserted node-ptrs for the next level */
  234. )
  235. {
  236. struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
  237. int item_pos = PATH_LAST_POSITION(tb->tb_path); /* index into the array of item headers in S[0]
  238. of the affected item */
  239. struct buffer_info bi;
  240. struct buffer_head *S_new[2]; /* new nodes allocated to hold what could not fit into S */
  241. int snum[2]; /* number of items that will be placed
  242. into S_new (includes partially shifted
  243. items) */
  244. int sbytes[2]; /* if an item is partially shifted into S_new then
  245. if it is a directory item
  246. it is the number of entries from the item that are shifted into S_new
  247. else
  248. it is the number of bytes from the item that are shifted into S_new
  249. */
  250. int n, i;
  251. int ret_val;
  252. int pos_in_item;
  253. int zeros_num;
  254. PROC_INFO_INC(tb->tb_sb, balance_at[0]);
  255. /* Make balance in case insert_size[0] < 0 */
  256. if (tb->insert_size[0] < 0)
  257. return balance_leaf_when_delete(tb, flag);
  258. zeros_num = 0;
  259. if (flag == M_INSERT && !body)
  260. zeros_num = ih_item_len(ih);
  261. pos_in_item = tb->tb_path->pos_in_item;
  262. /* for indirect item pos_in_item is measured in unformatted node
  263. pointers. Recalculate to bytes */
  264. if (flag != M_INSERT
  265. && is_indirect_le_ih(B_N_PITEM_HEAD(tbS0, item_pos)))
  266. pos_in_item *= UNFM_P_SIZE;
  267. if (tb->lnum[0] > 0) {
  268. /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
  269. if (item_pos < tb->lnum[0]) {
  270. /* new item or it part falls to L[0], shift it too */
  271. n = B_NR_ITEMS(tb->L[0]);
  272. switch (flag) {
  273. case M_INSERT: /* insert item into L[0] */
  274. if (item_pos == tb->lnum[0] - 1
  275. && tb->lbytes != -1) {
  276. /* part of new item falls into L[0] */
  277. int new_item_len;
  278. int version;
  279. ret_val =
  280. leaf_shift_left(tb, tb->lnum[0] - 1,
  281. -1);
  282. /* Calculate item length to insert to S[0] */
  283. new_item_len =
  284. ih_item_len(ih) - tb->lbytes;
  285. /* Calculate and check item length to insert to L[0] */
  286. put_ih_item_len(ih,
  287. ih_item_len(ih) -
  288. new_item_len);
  289. RFALSE(ih_item_len(ih) <= 0,
  290. "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
  291. ih_item_len(ih));
  292. /* Insert new item into L[0] */
  293. buffer_info_init_left(tb, &bi);
  294. leaf_insert_into_buf(&bi,
  295. n + item_pos -
  296. ret_val, ih, body,
  297. zeros_num >
  298. ih_item_len(ih) ?
  299. ih_item_len(ih) :
  300. zeros_num);
  301. version = ih_version(ih);
  302. /* Calculate key component, item length and body to insert into S[0] */
  303. set_le_ih_k_offset(ih,
  304. le_ih_k_offset(ih) +
  305. (tb->
  306. lbytes <<
  307. (is_indirect_le_ih
  308. (ih) ? tb->tb_sb->
  309. s_blocksize_bits -
  310. UNFM_P_SHIFT :
  311. 0)));
  312. put_ih_item_len(ih, new_item_len);
  313. if (tb->lbytes > zeros_num) {
  314. body +=
  315. (tb->lbytes - zeros_num);
  316. zeros_num = 0;
  317. } else
  318. zeros_num -= tb->lbytes;
  319. RFALSE(ih_item_len(ih) <= 0,
  320. "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
  321. ih_item_len(ih));
  322. } else {
  323. /* new item in whole falls into L[0] */
  324. /* Shift lnum[0]-1 items to L[0] */
  325. ret_val =
  326. leaf_shift_left(tb, tb->lnum[0] - 1,
  327. tb->lbytes);
  328. /* Insert new item into L[0] */
  329. buffer_info_init_left(tb, &bi);
  330. leaf_insert_into_buf(&bi,
  331. n + item_pos -
  332. ret_val, ih, body,
  333. zeros_num);
  334. tb->insert_size[0] = 0;
  335. zeros_num = 0;
  336. }
  337. break;
  338. case M_PASTE: /* append item in L[0] */
  339. if (item_pos == tb->lnum[0] - 1
  340. && tb->lbytes != -1) {
  341. /* we must shift the part of the appended item */
  342. if (is_direntry_le_ih
  343. (B_N_PITEM_HEAD(tbS0, item_pos))) {
  344. RFALSE(zeros_num,
  345. "PAP-12090: invalid parameter in case of a directory");
  346. /* directory item */
  347. if (tb->lbytes > pos_in_item) {
  348. /* new directory entry falls into L[0] */
  349. struct item_head
  350. *pasted;
  351. int l_pos_in_item =
  352. pos_in_item;
  353. /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
  354. ret_val =
  355. leaf_shift_left(tb,
  356. tb->
  357. lnum
  358. [0],
  359. tb->
  360. lbytes
  361. -
  362. 1);
  363. if (ret_val
  364. && !item_pos) {
  365. pasted =
  366. B_N_PITEM_HEAD
  367. (tb->L[0],
  368. B_NR_ITEMS
  369. (tb->
  370. L[0]) -
  371. 1);
  372. l_pos_in_item +=
  373. I_ENTRY_COUNT
  374. (pasted) -
  375. (tb->
  376. lbytes -
  377. 1);
  378. }
  379. /* Append given directory entry to directory item */
  380. buffer_info_init_left(tb, &bi);
  381. leaf_paste_in_buffer
  382. (&bi,
  383. n + item_pos -
  384. ret_val,
  385. l_pos_in_item,
  386. tb->insert_size[0],
  387. body, zeros_num);
  388. /* previous string prepared space for pasting new entry, following string pastes this entry */
  389. /* when we have merge directory item, pos_in_item has been changed too */
  390. /* paste new directory entry. 1 is entry number */
  391. leaf_paste_entries(&bi,
  392. n +
  393. item_pos
  394. -
  395. ret_val,
  396. l_pos_in_item,
  397. 1,
  398. (struct
  399. reiserfs_de_head
  400. *)
  401. body,
  402. body
  403. +
  404. DEH_SIZE,
  405. tb->
  406. insert_size
  407. [0]
  408. );
  409. tb->insert_size[0] = 0;
  410. } else {
  411. /* new directory item doesn't fall into L[0] */
  412. /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
  413. leaf_shift_left(tb,
  414. tb->
  415. lnum[0],
  416. tb->
  417. lbytes);
  418. }
  419. /* Calculate new position to append in item body */
  420. pos_in_item -= tb->lbytes;
  421. } else {
  422. /* regular object */
  423. RFALSE(tb->lbytes <= 0,
  424. "PAP-12095: there is nothing to shift to L[0]. lbytes=%d",
  425. tb->lbytes);
  426. RFALSE(pos_in_item !=
  427. ih_item_len
  428. (B_N_PITEM_HEAD
  429. (tbS0, item_pos)),
  430. "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
  431. ih_item_len
  432. (B_N_PITEM_HEAD
  433. (tbS0, item_pos)),
  434. pos_in_item);
  435. if (tb->lbytes >= pos_in_item) {
  436. /* appended item will be in L[0] in whole */
  437. int l_n;
  438. /* this bytes number must be appended to the last item of L[h] */
  439. l_n =
  440. tb->lbytes -
  441. pos_in_item;
  442. /* Calculate new insert_size[0] */
  443. tb->insert_size[0] -=
  444. l_n;
  445. RFALSE(tb->
  446. insert_size[0] <=
  447. 0,
  448. "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
  449. tb->
  450. insert_size[0]);
  451. ret_val =
  452. leaf_shift_left(tb,
  453. tb->
  454. lnum
  455. [0],
  456. ih_item_len
  457. (B_N_PITEM_HEAD
  458. (tbS0,
  459. item_pos)));
  460. /* Append to body of item in L[0] */
  461. buffer_info_init_left(tb, &bi);
  462. leaf_paste_in_buffer
  463. (&bi,
  464. n + item_pos -
  465. ret_val,
  466. ih_item_len
  467. (B_N_PITEM_HEAD
  468. (tb->L[0],
  469. n + item_pos -
  470. ret_val)), l_n,
  471. body,
  472. zeros_num >
  473. l_n ? l_n :
  474. zeros_num);
  475. /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
  476. {
  477. int version;
  478. int temp_l =
  479. l_n;
  480. RFALSE
  481. (ih_item_len
  482. (B_N_PITEM_HEAD
  483. (tbS0,
  484. 0)),
  485. "PAP-12106: item length must be 0");
  486. RFALSE
  487. (comp_short_le_keys
  488. (B_N_PKEY
  489. (tbS0, 0),
  490. B_N_PKEY
  491. (tb->L[0],
  492. n +
  493. item_pos
  494. -
  495. ret_val)),
  496. "PAP-12107: items must be of the same file");
  497. if (is_indirect_le_ih(B_N_PITEM_HEAD(tb->L[0], n + item_pos - ret_val))) {
  498. temp_l =
  499. l_n
  500. <<
  501. (tb->
  502. tb_sb->
  503. s_blocksize_bits
  504. -
  505. UNFM_P_SHIFT);
  506. }
  507. /* update key of first item in S0 */
  508. version =
  509. ih_version
  510. (B_N_PITEM_HEAD
  511. (tbS0, 0));
  512. set_le_key_k_offset
  513. (version,
  514. B_N_PKEY
  515. (tbS0, 0),
  516. le_key_k_offset
  517. (version,
  518. B_N_PKEY
  519. (tbS0,
  520. 0)) +
  521. temp_l);
  522. /* update left delimiting key */
  523. set_le_key_k_offset
  524. (version,
  525. B_N_PDELIM_KEY
  526. (tb->
  527. CFL[0],
  528. tb->
  529. lkey[0]),
  530. le_key_k_offset
  531. (version,
  532. B_N_PDELIM_KEY
  533. (tb->
  534. CFL[0],
  535. tb->
  536. lkey[0]))
  537. + temp_l);
  538. }
  539. /* Calculate new body, position in item and insert_size[0] */
  540. if (l_n > zeros_num) {
  541. body +=
  542. (l_n -
  543. zeros_num);
  544. zeros_num = 0;
  545. } else
  546. zeros_num -=
  547. l_n;
  548. pos_in_item = 0;
  549. RFALSE
  550. (comp_short_le_keys
  551. (B_N_PKEY(tbS0, 0),
  552. B_N_PKEY(tb->L[0],
  553. B_NR_ITEMS
  554. (tb->
  555. L[0]) -
  556. 1))
  557. ||
  558. !op_is_left_mergeable
  559. (B_N_PKEY(tbS0, 0),
  560. tbS0->b_size)
  561. ||
  562. !op_is_left_mergeable
  563. (B_N_PDELIM_KEY
  564. (tb->CFL[0],
  565. tb->lkey[0]),
  566. tbS0->b_size),
  567. "PAP-12120: item must be merge-able with left neighboring item");
  568. } else { /* only part of the appended item will be in L[0] */
  569. /* Calculate position in item for append in S[0] */
  570. pos_in_item -=
  571. tb->lbytes;
  572. RFALSE(pos_in_item <= 0,
  573. "PAP-12125: no place for paste. pos_in_item=%d",
  574. pos_in_item);
  575. /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
  576. leaf_shift_left(tb,
  577. tb->
  578. lnum[0],
  579. tb->
  580. lbytes);
  581. }
  582. }
  583. } else { /* appended item will be in L[0] in whole */
  584. struct item_head *pasted;
  585. if (!item_pos && op_is_left_mergeable(B_N_PKEY(tbS0, 0), tbS0->b_size)) { /* if we paste into first item of S[0] and it is left mergable */
  586. /* then increment pos_in_item by the size of the last item in L[0] */
  587. pasted =
  588. B_N_PITEM_HEAD(tb->L[0],
  589. n - 1);
  590. if (is_direntry_le_ih(pasted))
  591. pos_in_item +=
  592. ih_entry_count
  593. (pasted);
  594. else
  595. pos_in_item +=
  596. ih_item_len(pasted);
  597. }
  598. /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
  599. ret_val =
  600. leaf_shift_left(tb, tb->lnum[0],
  601. tb->lbytes);
  602. /* Append to body of item in L[0] */
  603. buffer_info_init_left(tb, &bi);
  604. leaf_paste_in_buffer(&bi,
  605. n + item_pos -
  606. ret_val,
  607. pos_in_item,
  608. tb->insert_size[0],
  609. body, zeros_num);
  610. /* if appended item is directory, paste entry */
  611. pasted =
  612. B_N_PITEM_HEAD(tb->L[0],
  613. n + item_pos -
  614. ret_val);
  615. if (is_direntry_le_ih(pasted))
  616. leaf_paste_entries(&bi,
  617. n +
  618. item_pos -
  619. ret_val,
  620. pos_in_item,
  621. 1,
  622. (struct
  623. reiserfs_de_head
  624. *)body,
  625. body +
  626. DEH_SIZE,
  627. tb->
  628. insert_size
  629. [0]
  630. );
  631. /* if appended item is indirect item, put unformatted node into un list */
  632. if (is_indirect_le_ih(pasted))
  633. set_ih_free_space(pasted, 0);
  634. tb->insert_size[0] = 0;
  635. zeros_num = 0;
  636. }
  637. break;
  638. default: /* cases d and t */
  639. reiserfs_panic(tb->tb_sb, "PAP-12130",
  640. "lnum > 0: unexpected mode: "
  641. " %s(%d)",
  642. (flag ==
  643. M_DELETE) ? "DELETE" : ((flag ==
  644. M_CUT)
  645. ? "CUT"
  646. :
  647. "UNKNOWN"),
  648. flag);
  649. }
  650. } else {
  651. /* new item doesn't fall into L[0] */
  652. leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
  653. }
  654. }
  655. /* tb->lnum[0] > 0 */
  656. /* Calculate new item position */
  657. item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
  658. if (tb->rnum[0] > 0) {
  659. /* shift rnum[0] items from S[0] to the right neighbor R[0] */
  660. n = B_NR_ITEMS(tbS0);
  661. switch (flag) {
  662. case M_INSERT: /* insert item */
  663. if (n - tb->rnum[0] < item_pos) { /* new item or its part falls to R[0] */
  664. if (item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */
  665. loff_t old_key_comp, old_len,
  666. r_zeros_number;
  667. const char *r_body;
  668. int version;
  669. loff_t offset;
  670. leaf_shift_right(tb, tb->rnum[0] - 1,
  671. -1);
  672. version = ih_version(ih);
  673. /* Remember key component and item length */
  674. old_key_comp = le_ih_k_offset(ih);
  675. old_len = ih_item_len(ih);
  676. /* Calculate key component and item length to insert into R[0] */
  677. offset =
  678. le_ih_k_offset(ih) +
  679. ((old_len -
  680. tb->
  681. rbytes) << (is_indirect_le_ih(ih)
  682. ? tb->tb_sb->
  683. s_blocksize_bits -
  684. UNFM_P_SHIFT : 0));
  685. set_le_ih_k_offset(ih, offset);
  686. put_ih_item_len(ih, tb->rbytes);
  687. /* Insert part of the item into R[0] */
  688. buffer_info_init_right(tb, &bi);
  689. if ((old_len - tb->rbytes) > zeros_num) {
  690. r_zeros_number = 0;
  691. r_body =
  692. body + (old_len -
  693. tb->rbytes) -
  694. zeros_num;
  695. } else {
  696. r_body = body;
  697. r_zeros_number =
  698. zeros_num - (old_len -
  699. tb->rbytes);
  700. zeros_num -= r_zeros_number;
  701. }
  702. leaf_insert_into_buf(&bi, 0, ih, r_body,
  703. r_zeros_number);
  704. /* Replace right delimiting key by first key in R[0] */
  705. replace_key(tb, tb->CFR[0], tb->rkey[0],
  706. tb->R[0], 0);
  707. /* Calculate key component and item length to insert into S[0] */
  708. set_le_ih_k_offset(ih, old_key_comp);
  709. put_ih_item_len(ih,
  710. old_len - tb->rbytes);
  711. tb->insert_size[0] -= tb->rbytes;
  712. } else { /* whole new item falls into R[0] */
  713. /* Shift rnum[0]-1 items to R[0] */
  714. ret_val =
  715. leaf_shift_right(tb,
  716. tb->rnum[0] - 1,
  717. tb->rbytes);
  718. /* Insert new item into R[0] */
  719. buffer_info_init_right(tb, &bi);
  720. leaf_insert_into_buf(&bi,
  721. item_pos - n +
  722. tb->rnum[0] - 1,
  723. ih, body,
  724. zeros_num);
  725. if (item_pos - n + tb->rnum[0] - 1 == 0) {
  726. replace_key(tb, tb->CFR[0],
  727. tb->rkey[0],
  728. tb->R[0], 0);
  729. }
  730. zeros_num = tb->insert_size[0] = 0;
  731. }
  732. } else { /* new item or part of it doesn't fall into R[0] */
  733. leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
  734. }
  735. break;
  736. case M_PASTE: /* append item */
  737. if (n - tb->rnum[0] <= item_pos) { /* pasted item or part of it falls to R[0] */
  738. if (item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */
  739. if (is_direntry_le_ih(B_N_PITEM_HEAD(tbS0, item_pos))) { /* we append to directory item */
  740. int entry_count;
  741. RFALSE(zeros_num,
  742. "PAP-12145: invalid parameter in case of a directory");
  743. entry_count =
  744. I_ENTRY_COUNT(B_N_PITEM_HEAD
  745. (tbS0,
  746. item_pos));
  747. if (entry_count - tb->rbytes <
  748. pos_in_item)
  749. /* new directory entry falls into R[0] */
  750. {
  751. int paste_entry_position;
  752. RFALSE(tb->rbytes - 1 >=
  753. entry_count
  754. || !tb->
  755. insert_size[0],
  756. "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
  757. tb->rbytes,
  758. entry_count);
  759. /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
  760. leaf_shift_right(tb,
  761. tb->
  762. rnum
  763. [0],
  764. tb->
  765. rbytes
  766. - 1);
  767. /* Paste given directory entry to directory item */
  768. paste_entry_position =
  769. pos_in_item -
  770. entry_count +
  771. tb->rbytes - 1;
  772. buffer_info_init_right(tb, &bi);
  773. leaf_paste_in_buffer
  774. (&bi, 0,
  775. paste_entry_position,
  776. tb->insert_size[0],
  777. body, zeros_num);
  778. /* paste entry */
  779. leaf_paste_entries(&bi,
  780. 0,
  781. paste_entry_position,
  782. 1,
  783. (struct
  784. reiserfs_de_head
  785. *)
  786. body,
  787. body
  788. +
  789. DEH_SIZE,
  790. tb->
  791. insert_size
  792. [0]
  793. );
  794. if (paste_entry_position
  795. == 0) {
  796. /* change delimiting keys */
  797. replace_key(tb,
  798. tb->
  799. CFR
  800. [0],
  801. tb->
  802. rkey
  803. [0],
  804. tb->
  805. R
  806. [0],
  807. 0);
  808. }
  809. tb->insert_size[0] = 0;
  810. pos_in_item++;
  811. } else { /* new directory entry doesn't fall into R[0] */
  812. leaf_shift_right(tb,
  813. tb->
  814. rnum
  815. [0],
  816. tb->
  817. rbytes);
  818. }
  819. } else { /* regular object */
  820. int n_shift, n_rem,
  821. r_zeros_number;
  822. const char *r_body;
  823. /* Calculate number of bytes which must be shifted from appended item */
  824. if ((n_shift =
  825. tb->rbytes -
  826. tb->insert_size[0]) < 0)
  827. n_shift = 0;
  828. RFALSE(pos_in_item !=
  829. ih_item_len
  830. (B_N_PITEM_HEAD
  831. (tbS0, item_pos)),
  832. "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
  833. pos_in_item,
  834. ih_item_len
  835. (B_N_PITEM_HEAD
  836. (tbS0, item_pos)));
  837. leaf_shift_right(tb,
  838. tb->rnum[0],
  839. n_shift);
  840. /* Calculate number of bytes which must remain in body after appending to R[0] */
  841. if ((n_rem =
  842. tb->insert_size[0] -
  843. tb->rbytes) < 0)
  844. n_rem = 0;
  845. {
  846. int version;
  847. unsigned long temp_rem =
  848. n_rem;
  849. version =
  850. ih_version
  851. (B_N_PITEM_HEAD
  852. (tb->R[0], 0));
  853. if (is_indirect_le_key
  854. (version,
  855. B_N_PKEY(tb->R[0],
  856. 0))) {
  857. temp_rem =
  858. n_rem <<
  859. (tb->tb_sb->
  860. s_blocksize_bits
  861. -
  862. UNFM_P_SHIFT);
  863. }
  864. set_le_key_k_offset
  865. (version,
  866. B_N_PKEY(tb->R[0],
  867. 0),
  868. le_key_k_offset
  869. (version,
  870. B_N_PKEY(tb->R[0],
  871. 0)) +
  872. temp_rem);
  873. set_le_key_k_offset
  874. (version,
  875. B_N_PDELIM_KEY(tb->
  876. CFR
  877. [0],
  878. tb->
  879. rkey
  880. [0]),
  881. le_key_k_offset
  882. (version,
  883. B_N_PDELIM_KEY
  884. (tb->CFR[0],
  885. tb->rkey[0])) +
  886. temp_rem);
  887. }
  888. /* k_offset (B_N_PKEY(tb->R[0],0)) += n_rem;
  889. k_offset (B_N_PDELIM_KEY(tb->CFR[0],tb->rkey[0])) += n_rem;*/
  890. do_balance_mark_internal_dirty
  891. (tb, tb->CFR[0], 0);
  892. /* Append part of body into R[0] */
  893. buffer_info_init_right(tb, &bi);
  894. if (n_rem > zeros_num) {
  895. r_zeros_number = 0;
  896. r_body =
  897. body + n_rem -
  898. zeros_num;
  899. } else {
  900. r_body = body;
  901. r_zeros_number =
  902. zeros_num - n_rem;
  903. zeros_num -=
  904. r_zeros_number;
  905. }
  906. leaf_paste_in_buffer(&bi, 0,
  907. n_shift,
  908. tb->
  909. insert_size
  910. [0] -
  911. n_rem,
  912. r_body,
  913. r_zeros_number);
  914. if (is_indirect_le_ih
  915. (B_N_PITEM_HEAD
  916. (tb->R[0], 0))) {
  917. #if 0
  918. RFALSE(n_rem,
  919. "PAP-12160: paste more than one unformatted node pointer");
  920. #endif
  921. set_ih_free_space
  922. (B_N_PITEM_HEAD
  923. (tb->R[0], 0), 0);
  924. }
  925. tb->insert_size[0] = n_rem;
  926. if (!n_rem)
  927. pos_in_item++;
  928. }
  929. } else { /* pasted item in whole falls into R[0] */
  930. struct item_head *pasted;
  931. ret_val =
  932. leaf_shift_right(tb, tb->rnum[0],
  933. tb->rbytes);
  934. /* append item in R[0] */
  935. if (pos_in_item >= 0) {
  936. buffer_info_init_right(tb, &bi);
  937. leaf_paste_in_buffer(&bi,
  938. item_pos -
  939. n +
  940. tb->
  941. rnum[0],
  942. pos_in_item,
  943. tb->
  944. insert_size
  945. [0], body,
  946. zeros_num);
  947. }
  948. /* paste new entry, if item is directory item */
  949. pasted =
  950. B_N_PITEM_HEAD(tb->R[0],
  951. item_pos - n +
  952. tb->rnum[0]);
  953. if (is_direntry_le_ih(pasted)
  954. && pos_in_item >= 0) {
  955. leaf_paste_entries(&bi,
  956. item_pos -
  957. n +
  958. tb->rnum[0],
  959. pos_in_item,
  960. 1,
  961. (struct
  962. reiserfs_de_head
  963. *)body,
  964. body +
  965. DEH_SIZE,
  966. tb->
  967. insert_size
  968. [0]
  969. );
  970. if (!pos_in_item) {
  971. RFALSE(item_pos - n +
  972. tb->rnum[0],
  973. "PAP-12165: directory item must be first item of node when pasting is in 0th position");
  974. /* update delimiting keys */
  975. replace_key(tb,
  976. tb->CFR[0],
  977. tb->rkey[0],
  978. tb->R[0],
  979. 0);
  980. }
  981. }
  982. if (is_indirect_le_ih(pasted))
  983. set_ih_free_space(pasted, 0);
  984. zeros_num = tb->insert_size[0] = 0;
  985. }
  986. } else { /* new item doesn't fall into R[0] */
  987. leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
  988. }
  989. break;
  990. default: /* cases d and t */
  991. reiserfs_panic(tb->tb_sb, "PAP-12175",
  992. "rnum > 0: unexpected mode: %s(%d)",
  993. (flag ==
  994. M_DELETE) ? "DELETE" : ((flag ==
  995. M_CUT) ? "CUT"
  996. : "UNKNOWN"),
  997. flag);
  998. }
  999. }
  1000. /* tb->rnum[0] > 0 */
  1001. RFALSE(tb->blknum[0] > 3,
  1002. "PAP-12180: blknum can not be %d. It must be <= 3",
  1003. tb->blknum[0]);
  1004. RFALSE(tb->blknum[0] < 0,
  1005. "PAP-12185: blknum can not be %d. It must be >= 0",
  1006. tb->blknum[0]);
  1007. /* if while adding to a node we discover that it is possible to split
  1008. it in two, and merge the left part into the left neighbor and the
  1009. right part into the right neighbor, eliminating the node */
  1010. if (tb->blknum[0] == 0) { /* node S[0] is empty now */
  1011. RFALSE(!tb->lnum[0] || !tb->rnum[0],
  1012. "PAP-12190: lnum and rnum must not be zero");
  1013. /* if insertion was done before 0-th position in R[0], right
  1014. delimiting key of the tb->L[0]'s and left delimiting key are
  1015. not set correctly */
  1016. if (tb->CFL[0]) {
  1017. if (!tb->CFR[0])
  1018. reiserfs_panic(tb->tb_sb, "vs-12195",
  1019. "CFR not initialized");
  1020. copy_key(B_N_PDELIM_KEY(tb->CFL[0], tb->lkey[0]),
  1021. B_N_PDELIM_KEY(tb->CFR[0], tb->rkey[0]));
  1022. do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
  1023. }
  1024. reiserfs_invalidate_buffer(tb, tbS0);
  1025. return 0;
  1026. }
  1027. /* Fill new nodes that appear in place of S[0] */
  1028. /* I am told that this copying is because we need an array to enable
  1029. the looping code. -Hans */
  1030. snum[0] = tb->s1num, snum[1] = tb->s2num;
  1031. sbytes[0] = tb->s1bytes;
  1032. sbytes[1] = tb->s2bytes;
  1033. for (i = tb->blknum[0] - 2; i >= 0; i--) {
  1034. RFALSE(!snum[i], "PAP-12200: snum[%d] == %d. Must be > 0", i,
  1035. snum[i]);
  1036. /* here we shift from S to S_new nodes */
  1037. S_new[i] = get_FEB(tb);
  1038. /* initialized block type and tree level */
  1039. set_blkh_level(B_BLK_HEAD(S_new[i]), DISK_LEAF_NODE_LEVEL);
  1040. n = B_NR_ITEMS(tbS0);
  1041. switch (flag) {
  1042. case M_INSERT: /* insert item */
  1043. if (n - snum[i] < item_pos) { /* new item or it's part falls to first new node S_new[i] */
  1044. if (item_pos == n - snum[i] + 1 && sbytes[i] != -1) { /* part of new item falls into S_new[i] */
  1045. int old_key_comp, old_len,
  1046. r_zeros_number;
  1047. const char *r_body;
  1048. int version;
  1049. /* Move snum[i]-1 items from S[0] to S_new[i] */
  1050. leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  1051. snum[i] - 1, -1,
  1052. S_new[i]);
  1053. /* Remember key component and item length */
  1054. version = ih_version(ih);
  1055. old_key_comp = le_ih_k_offset(ih);
  1056. old_len = ih_item_len(ih);
  1057. /* Calculate key component and item length to insert into S_new[i] */
  1058. set_le_ih_k_offset(ih,
  1059. le_ih_k_offset(ih) +
  1060. ((old_len -
  1061. sbytes[i]) <<
  1062. (is_indirect_le_ih
  1063. (ih) ? tb->tb_sb->
  1064. s_blocksize_bits -
  1065. UNFM_P_SHIFT :
  1066. 0)));
  1067. put_ih_item_len(ih, sbytes[i]);
  1068. /* Insert part of the item into S_new[i] before 0-th item */
  1069. buffer_info_init_bh(tb, &bi, S_new[i]);
  1070. if ((old_len - sbytes[i]) > zeros_num) {
  1071. r_zeros_number = 0;
  1072. r_body =
  1073. body + (old_len -
  1074. sbytes[i]) -
  1075. zeros_num;
  1076. } else {
  1077. r_body = body;
  1078. r_zeros_number =
  1079. zeros_num - (old_len -
  1080. sbytes[i]);
  1081. zeros_num -= r_zeros_number;
  1082. }
  1083. leaf_insert_into_buf(&bi, 0, ih, r_body,
  1084. r_zeros_number);
  1085. /* Calculate key component and item length to insert into S[i] */
  1086. set_le_ih_k_offset(ih, old_key_comp);
  1087. put_ih_item_len(ih,
  1088. old_len - sbytes[i]);
  1089. tb->insert_size[0] -= sbytes[i];
  1090. } else { /* whole new item falls into S_new[i] */
  1091. /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
  1092. leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  1093. snum[i] - 1, sbytes[i],
  1094. S_new[i]);
  1095. /* Insert new item into S_new[i] */
  1096. buffer_info_init_bh(tb, &bi, S_new[i]);
  1097. leaf_insert_into_buf(&bi,
  1098. item_pos - n +
  1099. snum[i] - 1, ih,
  1100. body, zeros_num);
  1101. zeros_num = tb->insert_size[0] = 0;
  1102. }
  1103. }
  1104. else { /* new item or it part don't falls into S_new[i] */
  1105. leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  1106. snum[i], sbytes[i], S_new[i]);
  1107. }
  1108. break;
  1109. case M_PASTE: /* append item */
  1110. if (n - snum[i] <= item_pos) { /* pasted item or part if it falls to S_new[i] */
  1111. if (item_pos == n - snum[i] && sbytes[i] != -1) { /* we must shift part of the appended item */
  1112. struct item_head *aux_ih;
  1113. RFALSE(ih, "PAP-12210: ih must be 0");
  1114. aux_ih = B_N_PITEM_HEAD(tbS0, item_pos);
  1115. if (is_direntry_le_ih(aux_ih)) {
  1116. /* we append to directory item */
  1117. int entry_count;
  1118. entry_count =
  1119. ih_entry_count(aux_ih);
  1120. if (entry_count - sbytes[i] <
  1121. pos_in_item
  1122. && pos_in_item <=
  1123. entry_count) {
  1124. /* new directory entry falls into S_new[i] */
  1125. RFALSE(!tb->
  1126. insert_size[0],
  1127. "PAP-12215: insert_size is already 0");
  1128. RFALSE(sbytes[i] - 1 >=
  1129. entry_count,
  1130. "PAP-12220: there are no so much entries (%d), only %d",
  1131. sbytes[i] - 1,
  1132. entry_count);
  1133. /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
  1134. leaf_move_items
  1135. (LEAF_FROM_S_TO_SNEW,
  1136. tb, snum[i],
  1137. sbytes[i] - 1,
  1138. S_new[i]);
  1139. /* Paste given directory entry to directory item */
  1140. buffer_info_init_bh(tb, &bi, S_new[i]);
  1141. leaf_paste_in_buffer
  1142. (&bi, 0,
  1143. pos_in_item -
  1144. entry_count +
  1145. sbytes[i] - 1,
  1146. tb->insert_size[0],
  1147. body, zeros_num);
  1148. /* paste new directory entry */
  1149. leaf_paste_entries(&bi,
  1150. 0,
  1151. pos_in_item
  1152. -
  1153. entry_count
  1154. +
  1155. sbytes
  1156. [i] -
  1157. 1, 1,
  1158. (struct
  1159. reiserfs_de_head
  1160. *)
  1161. body,
  1162. body
  1163. +
  1164. DEH_SIZE,
  1165. tb->
  1166. insert_size
  1167. [0]
  1168. );
  1169. tb->insert_size[0] = 0;
  1170. pos_in_item++;
  1171. } else { /* new directory entry doesn't fall into S_new[i] */
  1172. leaf_move_items
  1173. (LEAF_FROM_S_TO_SNEW,
  1174. tb, snum[i],
  1175. sbytes[i],
  1176. S_new[i]);
  1177. }
  1178. } else { /* regular object */
  1179. int n_shift, n_rem,
  1180. r_zeros_number;
  1181. const char *r_body;
  1182. RFALSE(pos_in_item !=
  1183. ih_item_len
  1184. (B_N_PITEM_HEAD
  1185. (tbS0, item_pos))
  1186. || tb->insert_size[0] <=
  1187. 0,
  1188. "PAP-12225: item too short or insert_size <= 0");
  1189. /* Calculate number of bytes which must be shifted from appended item */
  1190. n_shift =
  1191. sbytes[i] -
  1192. tb->insert_size[0];
  1193. if (n_shift < 0)
  1194. n_shift = 0;
  1195. leaf_move_items
  1196. (LEAF_FROM_S_TO_SNEW, tb,
  1197. snum[i], n_shift,
  1198. S_new[i]);
  1199. /* Calculate number of bytes which must remain in body after append to S_new[i] */
  1200. n_rem =
  1201. tb->insert_size[0] -
  1202. sbytes[i];
  1203. if (n_rem < 0)
  1204. n_rem = 0;
  1205. /* Append part of body into S_new[0] */
  1206. buffer_info_init_bh(tb, &bi, S_new[i]);
  1207. if (n_rem > zeros_num) {
  1208. r_zeros_number = 0;
  1209. r_body =
  1210. body + n_rem -
  1211. zeros_num;
  1212. } else {
  1213. r_body = body;
  1214. r_zeros_number =
  1215. zeros_num - n_rem;
  1216. zeros_num -=
  1217. r_zeros_number;
  1218. }
  1219. leaf_paste_in_buffer(&bi, 0,
  1220. n_shift,
  1221. tb->
  1222. insert_size
  1223. [0] -
  1224. n_rem,
  1225. r_body,
  1226. r_zeros_number);
  1227. {
  1228. struct item_head *tmp;
  1229. tmp =
  1230. B_N_PITEM_HEAD(S_new
  1231. [i],
  1232. 0);
  1233. if (is_indirect_le_ih
  1234. (tmp)) {
  1235. set_ih_free_space
  1236. (tmp, 0);
  1237. set_le_ih_k_offset
  1238. (tmp,
  1239. le_ih_k_offset
  1240. (tmp) +
  1241. (n_rem <<
  1242. (tb->
  1243. tb_sb->
  1244. s_blocksize_bits
  1245. -
  1246. UNFM_P_SHIFT)));
  1247. } else {
  1248. set_le_ih_k_offset
  1249. (tmp,
  1250. le_ih_k_offset
  1251. (tmp) +
  1252. n_rem);
  1253. }
  1254. }
  1255. tb->insert_size[0] = n_rem;
  1256. if (!n_rem)
  1257. pos_in_item++;
  1258. }
  1259. } else
  1260. /* item falls wholly into S_new[i] */
  1261. {
  1262. int leaf_mi;
  1263. struct item_head *pasted;
  1264. #ifdef CONFIG_REISERFS_CHECK
  1265. struct item_head *ih_check =
  1266. B_N_PITEM_HEAD(tbS0, item_pos);
  1267. if (!is_direntry_le_ih(ih_check)
  1268. && (pos_in_item != ih_item_len(ih_check)
  1269. || tb->insert_size[0] <= 0))
  1270. reiserfs_panic(tb->tb_sb,
  1271. "PAP-12235",
  1272. "pos_in_item "
  1273. "must be equal "
  1274. "to ih_item_len");
  1275. #endif /* CONFIG_REISERFS_CHECK */
  1276. leaf_mi =
  1277. leaf_move_items(LEAF_FROM_S_TO_SNEW,
  1278. tb, snum[i],
  1279. sbytes[i],
  1280. S_new[i]);
  1281. RFALSE(leaf_mi,
  1282. "PAP-12240: unexpected value returned by leaf_move_items (%d)",
  1283. leaf_mi);
  1284. /* paste into item */
  1285. buffer_info_init_bh(tb, &bi, S_new[i]);
  1286. leaf_paste_in_buffer(&bi,
  1287. item_pos - n +
  1288. snum[i],
  1289. pos_in_item,
  1290. tb->insert_size[0],
  1291. body, zeros_num);
  1292. pasted =
  1293. B_N_PITEM_HEAD(S_new[i],
  1294. item_pos - n +
  1295. snum[i]);
  1296. if (is_direntry_le_ih(pasted)) {
  1297. leaf_paste_entries(&bi,
  1298. item_pos -
  1299. n + snum[i],
  1300. pos_in_item,
  1301. 1,
  1302. (struct
  1303. reiserfs_de_head
  1304. *)body,
  1305. body +
  1306. DEH_SIZE,
  1307. tb->
  1308. insert_size
  1309. [0]
  1310. );
  1311. }
  1312. /* if we paste to indirect item update ih_free_space */
  1313. if (is_indirect_le_ih(pasted))
  1314. set_ih_free_space(pasted, 0);
  1315. zeros_num = tb->insert_size[0] = 0;
  1316. }
  1317. }
  1318. else { /* pasted item doesn't fall into S_new[i] */
  1319. leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
  1320. snum[i], sbytes[i], S_new[i]);
  1321. }
  1322. break;
  1323. default: /* cases d and t */
  1324. reiserfs_panic(tb->tb_sb, "PAP-12245",
  1325. "blknum > 2: unexpected mode: %s(%d)",
  1326. (flag ==
  1327. M_DELETE) ? "DELETE" : ((flag ==
  1328. M_CUT) ? "CUT"
  1329. : "UNKNOWN"),
  1330. flag);
  1331. }
  1332. memcpy(insert_key + i, B_N_PKEY(S_new[i], 0), KEY_SIZE);
  1333. insert_ptr[i] = S_new[i];
  1334. RFALSE(!buffer_journaled(S_new[i])
  1335. || buffer_journal_dirty(S_new[i])
  1336. || buffer_dirty(S_new[i]), "PAP-12247: S_new[%d] : (%b)",
  1337. i, S_new[i]);
  1338. }
  1339. /* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the
  1340. affected item which remains in S */
  1341. if (0 <= item_pos && item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */
  1342. switch (flag) {
  1343. case M_INSERT: /* insert item into S[0] */
  1344. buffer_info_init_tbS0(tb, &bi);
  1345. leaf_insert_into_buf(&bi, item_pos, ih, body,
  1346. zeros_num);
  1347. /* If we insert the first key change the delimiting key */
  1348. if (item_pos == 0) {
  1349. if (tb->CFL[0]) /* can be 0 in reiserfsck */
  1350. replace_key(tb, tb->CFL[0], tb->lkey[0],
  1351. tbS0, 0);
  1352. }
  1353. break;
  1354. case M_PASTE:{ /* append item in S[0] */
  1355. struct item_head *pasted;
  1356. pasted = B_N_PITEM_HEAD(tbS0, item_pos);
  1357. /* when directory, may be new entry already pasted */
  1358. if (is_direntry_le_ih(pasted)) {
  1359. if (pos_in_item >= 0 &&
  1360. pos_in_item <=
  1361. ih_entry_count(pasted)) {
  1362. RFALSE(!tb->insert_size[0],
  1363. "PAP-12260: insert_size is 0 already");
  1364. /* prepare space */
  1365. buffer_info_init_tbS0(tb, &bi);
  1366. leaf_paste_in_buffer(&bi,
  1367. item_pos,
  1368. pos_in_item,
  1369. tb->
  1370. insert_size
  1371. [0], body,
  1372. zeros_num);
  1373. /* paste entry */
  1374. leaf_paste_entries(&bi,
  1375. item_pos,
  1376. pos_in_item,
  1377. 1,
  1378. (struct
  1379. reiserfs_de_head
  1380. *)body,
  1381. body +
  1382. DEH_SIZE,
  1383. tb->
  1384. insert_size
  1385. [0]
  1386. );
  1387. if (!item_pos && !pos_in_item) {
  1388. RFALSE(!tb->CFL[0]
  1389. || !tb->L[0],
  1390. "PAP-12270: CFL[0]/L[0] must be specified");
  1391. if (tb->CFL[0]) {
  1392. replace_key(tb,
  1393. tb->
  1394. CFL
  1395. [0],
  1396. tb->
  1397. lkey
  1398. [0],
  1399. tbS0,
  1400. 0);
  1401. }
  1402. }
  1403. tb->insert_size[0] = 0;
  1404. }
  1405. } else { /* regular object */
  1406. if (pos_in_item == ih_item_len(pasted)) {
  1407. RFALSE(tb->insert_size[0] <= 0,
  1408. "PAP-12275: insert size must not be %d",
  1409. tb->insert_size[0]);
  1410. buffer_info_init_tbS0(tb, &bi);
  1411. leaf_paste_in_buffer(&bi,
  1412. item_pos,
  1413. pos_in_item,
  1414. tb->
  1415. insert_size
  1416. [0], body,
  1417. zeros_num);
  1418. if (is_indirect_le_ih(pasted)) {
  1419. #if 0
  1420. RFALSE(tb->
  1421. insert_size[0] !=
  1422. UNFM_P_SIZE,
  1423. "PAP-12280: insert_size for indirect item must be %d, not %d",
  1424. UNFM_P_SIZE,
  1425. tb->
  1426. insert_size[0]);
  1427. #endif
  1428. set_ih_free_space
  1429. (pasted, 0);
  1430. }
  1431. tb->insert_size[0] = 0;
  1432. }
  1433. #ifdef CONFIG_REISERFS_CHECK
  1434. else {
  1435. if (tb->insert_size[0]) {
  1436. print_cur_tb("12285");
  1437. reiserfs_panic(tb->
  1438. tb_sb,
  1439. "PAP-12285",
  1440. "insert_size "
  1441. "must be 0 "
  1442. "(%d)",
  1443. tb->insert_size[0]);
  1444. }
  1445. }
  1446. #endif /* CONFIG_REISERFS_CHECK */
  1447. }
  1448. } /* case M_PASTE: */
  1449. }
  1450. }
  1451. #ifdef CONFIG_REISERFS_CHECK
  1452. if (flag == M_PASTE && tb->insert_size[0]) {
  1453. print_cur_tb("12290");
  1454. reiserfs_panic(tb->tb_sb,
  1455. "PAP-12290", "insert_size is still not 0 (%d)",
  1456. tb->insert_size[0]);
  1457. }
  1458. #endif /* CONFIG_REISERFS_CHECK */
  1459. return 0;
  1460. } /* Leaf level of the tree is balanced (end of balance_leaf) */
  1461. /* Make empty node */
  1462. void make_empty_node(struct buffer_info *bi)
  1463. {
  1464. struct block_head *blkh;
  1465. RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
  1466. blkh = B_BLK_HEAD(bi->bi_bh);
  1467. set_blkh_nr_item(blkh, 0);
  1468. set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
  1469. if (bi->bi_parent)
  1470. B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
  1471. }
  1472. /* Get first empty buffer */
  1473. struct buffer_head *get_FEB(struct tree_balance *tb)
  1474. {
  1475. int i;
  1476. struct buffer_info bi;
  1477. for (i = 0; i < MAX_FEB_SIZE; i++)
  1478. if (tb->FEB[i] != NULL)
  1479. break;
  1480. if (i == MAX_FEB_SIZE)
  1481. reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
  1482. buffer_info_init_bh(tb, &bi, tb->FEB[i]);
  1483. make_empty_node(&bi);
  1484. set_buffer_uptodate(tb->FEB[i]);
  1485. tb->used[i] = tb->FEB[i];
  1486. tb->FEB[i] = NULL;
  1487. return tb->used[i];
  1488. }
  1489. /* This is now used because reiserfs_free_block has to be able to
  1490. ** schedule.
  1491. */
  1492. static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
  1493. {
  1494. int i;
  1495. if (buffer_dirty(bh))
  1496. reiserfs_warning(tb->tb_sb, "reiserfs-12320",
  1497. "called with dirty buffer");
  1498. for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
  1499. if (!tb->thrown[i]) {
  1500. tb->thrown[i] = bh;
  1501. get_bh(bh); /* free_thrown puts this */
  1502. return;
  1503. }
  1504. reiserfs_warning(tb->tb_sb, "reiserfs-12321",
  1505. "too many thrown buffers");
  1506. }
  1507. static void free_thrown(struct tree_balance *tb)
  1508. {
  1509. int i;
  1510. b_blocknr_t blocknr;
  1511. for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
  1512. if (tb->thrown[i]) {
  1513. blocknr = tb->thrown[i]->b_blocknr;
  1514. if (buffer_dirty(tb->thrown[i]))
  1515. reiserfs_warning(tb->tb_sb, "reiserfs-12322",
  1516. "called with dirty buffer %d",
  1517. blocknr);
  1518. brelse(tb->thrown[i]); /* incremented in store_thrown */
  1519. reiserfs_free_block(tb->transaction_handle, NULL,
  1520. blocknr, 0);
  1521. }
  1522. }
  1523. }
  1524. void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
  1525. {
  1526. struct block_head *blkh;
  1527. blkh = B_BLK_HEAD(bh);
  1528. set_blkh_level(blkh, FREE_LEVEL);
  1529. set_blkh_nr_item(blkh, 0);
  1530. clear_buffer_dirty(bh);
  1531. store_thrown(tb, bh);
  1532. }
  1533. /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
  1534. void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
  1535. struct buffer_head *src, int n_src)
  1536. {
  1537. RFALSE(dest == NULL || src == NULL,
  1538. "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
  1539. src, dest);
  1540. RFALSE(!B_IS_KEYS_LEVEL(dest),
  1541. "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
  1542. dest);
  1543. RFALSE(n_dest < 0 || n_src < 0,
  1544. "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
  1545. RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
  1546. "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
  1547. n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
  1548. if (B_IS_ITEMS_LEVEL(src))
  1549. /* source buffer contains leaf node */
  1550. memcpy(B_N_PDELIM_KEY(dest, n_dest), B_N_PITEM_HEAD(src, n_src),
  1551. KEY_SIZE);
  1552. else
  1553. memcpy(B_N_PDELIM_KEY(dest, n_dest), B_N_PDELIM_KEY(src, n_src),
  1554. KEY_SIZE);
  1555. do_balance_mark_internal_dirty(tb, dest, 0);
  1556. }
  1557. int get_left_neighbor_position(struct tree_balance *tb, int h)
  1558. {
  1559. int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
  1560. RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
  1561. "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
  1562. h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
  1563. if (Sh_position == 0)
  1564. return B_NR_ITEMS(tb->FL[h]);
  1565. else
  1566. return Sh_position - 1;
  1567. }
  1568. int get_right_neighbor_position(struct tree_balance *tb, int h)
  1569. {
  1570. int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
  1571. RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
  1572. "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
  1573. h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
  1574. if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
  1575. return 0;
  1576. else
  1577. return Sh_position + 1;
  1578. }
  1579. #ifdef CONFIG_REISERFS_CHECK
  1580. int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
  1581. static void check_internal_node(struct super_block *s, struct buffer_head *bh,
  1582. char *mes)
  1583. {
  1584. struct disk_child *dc;
  1585. int i;
  1586. RFALSE(!bh, "PAP-12336: bh == 0");
  1587. if (!bh || !B_IS_IN_TREE(bh))
  1588. return;
  1589. RFALSE(!buffer_dirty(bh) &&
  1590. !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
  1591. "PAP-12337: buffer (%b) must be dirty", bh);
  1592. dc = B_N_CHILD(bh, 0);
  1593. for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
  1594. if (!is_reusable(s, dc_block_number(dc), 1)) {
  1595. print_cur_tb(mes);
  1596. reiserfs_panic(s, "PAP-12338",
  1597. "invalid child pointer %y in %b",
  1598. dc, bh);
  1599. }
  1600. }
  1601. }
  1602. static int locked_or_not_in_tree(struct tree_balance *tb,
  1603. struct buffer_head *bh, char *which)
  1604. {
  1605. if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
  1606. !B_IS_IN_TREE(bh)) {
  1607. reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
  1608. return 1;
  1609. }
  1610. return 0;
  1611. }
  1612. static int check_before_balancing(struct tree_balance *tb)
  1613. {
  1614. int retval = 0;
  1615. if (REISERFS_SB(tb->tb_sb)->cur_tb) {
  1616. reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
  1617. "occurred based on cur_tb not being null at "
  1618. "this point in code. do_balance cannot properly "
  1619. "handle concurrent tree accesses on a same "
  1620. "mount point.");
  1621. }
  1622. /* double check that buffers that we will modify are unlocked. (fix_nodes should already have
  1623. prepped all of these for us). */
  1624. if (tb->lnum[0]) {
  1625. retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
  1626. retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
  1627. retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
  1628. check_leaf(tb->L[0]);
  1629. }
  1630. if (tb->rnum[0]) {
  1631. retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
  1632. retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
  1633. retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
  1634. check_leaf(tb->R[0]);
  1635. }
  1636. retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
  1637. "S[0]");
  1638. check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
  1639. return retval;
  1640. }
  1641. static void check_after_balance_leaf(struct tree_balance *tb)
  1642. {
  1643. if (tb->lnum[0]) {
  1644. if (B_FREE_SPACE(tb->L[0]) !=
  1645. MAX_CHILD_SIZE(tb->L[0]) -
  1646. dc_size(B_N_CHILD
  1647. (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
  1648. print_cur_tb("12221");
  1649. reiserfs_panic(tb->tb_sb, "PAP-12355",
  1650. "shift to left was incorrect");
  1651. }
  1652. }
  1653. if (tb->rnum[0]) {
  1654. if (B_FREE_SPACE(tb->R[0]) !=
  1655. MAX_CHILD_SIZE(tb->R[0]) -
  1656. dc_size(B_N_CHILD
  1657. (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
  1658. print_cur_tb("12222");
  1659. reiserfs_panic(tb->tb_sb, "PAP-12360",
  1660. "shift to right was incorrect");
  1661. }
  1662. }
  1663. if (PATH_H_PBUFFER(tb->tb_path, 1) &&
  1664. (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
  1665. (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
  1666. dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
  1667. PATH_H_POSITION(tb->tb_path, 1)))))) {
  1668. int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
  1669. int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
  1670. dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
  1671. PATH_H_POSITION(tb->tb_path,
  1672. 1))));
  1673. print_cur_tb("12223");
  1674. reiserfs_warning(tb->tb_sb, "reiserfs-12363",
  1675. "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
  1676. "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
  1677. left,
  1678. MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
  1679. PATH_H_PBUFFER(tb->tb_path, 1),
  1680. PATH_H_POSITION(tb->tb_path, 1),
  1681. dc_size(B_N_CHILD
  1682. (PATH_H_PBUFFER(tb->tb_path, 1),
  1683. PATH_H_POSITION(tb->tb_path, 1))),
  1684. right);
  1685. reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
  1686. }
  1687. }
  1688. static void check_leaf_level(struct tree_balance *tb)
  1689. {
  1690. check_leaf(tb->L[0]);
  1691. check_leaf(tb->R[0]);
  1692. check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
  1693. }
  1694. static void check_internal_levels(struct tree_balance *tb)
  1695. {
  1696. int h;
  1697. /* check all internal nodes */
  1698. for (h = 1; tb->insert_size[h]; h++) {
  1699. check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
  1700. "BAD BUFFER ON PATH");
  1701. if (tb->lnum[h])
  1702. check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
  1703. if (tb->rnum[h])
  1704. check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
  1705. }
  1706. }
  1707. #endif
  1708. /* Now we have all of the buffers that must be used in balancing of
  1709. the tree. We rely on the assumption that schedule() will not occur
  1710. while do_balance works. ( Only interrupt handlers are acceptable.)
  1711. We balance the tree according to the analysis made before this,
  1712. using buffers already obtained. For SMP support it will someday be
  1713. necessary to add ordered locking of tb. */
  1714. /* Some interesting rules of balancing:
  1715. we delete a maximum of two nodes per level per balancing: we never
  1716. delete R, when we delete two of three nodes L, S, R then we move
  1717. them into R.
  1718. we only delete L if we are deleting two nodes, if we delete only
  1719. one node we delete S
  1720. if we shift leaves then we shift as much as we can: this is a
  1721. deliberate policy of extremism in node packing which results in
  1722. higher average utilization after repeated random balance operations
  1723. at the cost of more memory copies and more balancing as a result of
  1724. small insertions to full nodes.
  1725. if we shift internal nodes we try to evenly balance the node
  1726. utilization, with consequent less balancing at the cost of lower
  1727. utilization.
  1728. one could argue that the policy for directories in leaves should be
  1729. that of internal nodes, but we will wait until another day to
  1730. evaluate this.... It would be nice to someday measure and prove
  1731. these assumptions as to what is optimal....
  1732. */
  1733. static inline void do_balance_starts(struct tree_balance *tb)
  1734. {
  1735. /* use print_cur_tb() to see initial state of struct
  1736. tree_balance */
  1737. /* store_print_tb (tb); */
  1738. /* do not delete, just comment it out */
  1739. /* print_tb(flag, PATH_LAST_POSITION(tb->tb_path), tb->tb_path->pos_in_item, tb,
  1740. "check");*/
  1741. RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
  1742. #ifdef CONFIG_REISERFS_CHECK
  1743. REISERFS_SB(tb->tb_sb)->cur_tb = tb;
  1744. #endif
  1745. }
  1746. static inline void do_balance_completed(struct tree_balance *tb)
  1747. {
  1748. #ifdef CONFIG_REISERFS_CHECK
  1749. check_leaf_level(tb);
  1750. check_internal_levels(tb);
  1751. REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
  1752. #endif
  1753. /* reiserfs_free_block is no longer schedule safe. So, we need to
  1754. ** put the buffers we want freed on the thrown list during do_balance,
  1755. ** and then free them now
  1756. */
  1757. REISERFS_SB(tb->tb_sb)->s_do_balance++;
  1758. /* release all nodes hold to perform the balancing */
  1759. unfix_nodes(tb);
  1760. free_thrown(tb);
  1761. }
  1762. void do_balance(struct tree_balance *tb, /* tree_balance structure */
  1763. struct item_head *ih, /* item header of inserted item */
  1764. const char *body, /* body of inserted item or bytes to paste */
  1765. int flag)
  1766. { /* i - insert, d - delete
  1767. c - cut, p - paste
  1768. Cut means delete part of an item
  1769. (includes removing an entry from a
  1770. directory).
  1771. Delete means delete whole item.
  1772. Insert means add a new item into the
  1773. tree.
  1774. Paste means to append to the end of an
  1775. existing file or to insert a directory
  1776. entry. */
  1777. int child_pos, /* position of a child node in its parent */
  1778. h; /* level of the tree being processed */
  1779. struct item_head insert_key[2]; /* in our processing of one level
  1780. we sometimes determine what
  1781. must be inserted into the next
  1782. higher level. This insertion
  1783. consists of a key or two keys
  1784. and their corresponding
  1785. pointers */
  1786. struct buffer_head *insert_ptr[2]; /* inserted node-ptrs for the next
  1787. level */
  1788. tb->tb_mode = flag;
  1789. tb->need_balance_dirty = 0;
  1790. if (FILESYSTEM_CHANGED_TB(tb)) {
  1791. reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
  1792. "changed");
  1793. }
  1794. /* if we have no real work to do */
  1795. if (!tb->insert_size[0]) {
  1796. reiserfs_warning(tb->tb_sb, "PAP-12350",
  1797. "insert_size == 0, mode == %c", flag);
  1798. unfix_nodes(tb);
  1799. return;
  1800. }
  1801. atomic_inc(&(fs_generation(tb->tb_sb)));
  1802. do_balance_starts(tb);
  1803. /* balance leaf returns 0 except if combining L R and S into
  1804. one node. see balance_internal() for explanation of this
  1805. line of code. */
  1806. child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
  1807. balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
  1808. #ifdef CONFIG_REISERFS_CHECK
  1809. check_after_balance_leaf(tb);
  1810. #endif
  1811. /* Balance internal level of the tree. */
  1812. for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
  1813. child_pos =
  1814. balance_internal(tb, h, child_pos, insert_key, insert_ptr);
  1815. do_balance_completed(tb);
  1816. }