do_balan.c 54 KB

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