do_balan.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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. }