do_balan.c 54 KB

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