do_balan.c 57 KB

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