ibalance.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <asm/uaccess.h>
  5. #include <linux/string.h>
  6. #include <linux/time.h>
  7. #include <linux/reiserfs_fs.h>
  8. #include <linux/buffer_head.h>
  9. /* this is one and only function that is used outside (do_balance.c) */
  10. int balance_internal(struct tree_balance *,
  11. int, int, struct item_head *, struct buffer_head **);
  12. /* modes of internal_shift_left, internal_shift_right and internal_insert_childs */
  13. #define INTERNAL_SHIFT_FROM_S_TO_L 0
  14. #define INTERNAL_SHIFT_FROM_R_TO_S 1
  15. #define INTERNAL_SHIFT_FROM_L_TO_S 2
  16. #define INTERNAL_SHIFT_FROM_S_TO_R 3
  17. #define INTERNAL_INSERT_TO_S 4
  18. #define INTERNAL_INSERT_TO_L 5
  19. #define INTERNAL_INSERT_TO_R 6
  20. static void internal_define_dest_src_infos(int shift_mode,
  21. struct tree_balance *tb,
  22. int h,
  23. struct buffer_info *dest_bi,
  24. struct buffer_info *src_bi,
  25. int *d_key, struct buffer_head **cf)
  26. {
  27. memset(dest_bi, 0, sizeof(struct buffer_info));
  28. memset(src_bi, 0, sizeof(struct buffer_info));
  29. /* define dest, src, dest parent, dest position */
  30. switch (shift_mode) {
  31. case INTERNAL_SHIFT_FROM_S_TO_L: /* used in internal_shift_left */
  32. src_bi->tb = tb;
  33. src_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  34. src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  35. src_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  36. dest_bi->tb = tb;
  37. dest_bi->bi_bh = tb->L[h];
  38. dest_bi->bi_parent = tb->FL[h];
  39. dest_bi->bi_position = get_left_neighbor_position(tb, h);
  40. *d_key = tb->lkey[h];
  41. *cf = tb->CFL[h];
  42. break;
  43. case INTERNAL_SHIFT_FROM_L_TO_S:
  44. src_bi->tb = tb;
  45. src_bi->bi_bh = tb->L[h];
  46. src_bi->bi_parent = tb->FL[h];
  47. src_bi->bi_position = get_left_neighbor_position(tb, h);
  48. dest_bi->tb = tb;
  49. dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  50. dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  51. dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1); /* dest position is analog of dest->b_item_order */
  52. *d_key = tb->lkey[h];
  53. *cf = tb->CFL[h];
  54. break;
  55. case INTERNAL_SHIFT_FROM_R_TO_S: /* used in internal_shift_left */
  56. src_bi->tb = tb;
  57. src_bi->bi_bh = tb->R[h];
  58. src_bi->bi_parent = tb->FR[h];
  59. src_bi->bi_position = get_right_neighbor_position(tb, h);
  60. dest_bi->tb = tb;
  61. dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  62. dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  63. dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  64. *d_key = tb->rkey[h];
  65. *cf = tb->CFR[h];
  66. break;
  67. case INTERNAL_SHIFT_FROM_S_TO_R:
  68. src_bi->tb = tb;
  69. src_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  70. src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  71. src_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  72. dest_bi->tb = tb;
  73. dest_bi->bi_bh = tb->R[h];
  74. dest_bi->bi_parent = tb->FR[h];
  75. dest_bi->bi_position = get_right_neighbor_position(tb, h);
  76. *d_key = tb->rkey[h];
  77. *cf = tb->CFR[h];
  78. break;
  79. case INTERNAL_INSERT_TO_L:
  80. dest_bi->tb = tb;
  81. dest_bi->bi_bh = tb->L[h];
  82. dest_bi->bi_parent = tb->FL[h];
  83. dest_bi->bi_position = get_left_neighbor_position(tb, h);
  84. break;
  85. case INTERNAL_INSERT_TO_S:
  86. dest_bi->tb = tb;
  87. dest_bi->bi_bh = PATH_H_PBUFFER(tb->tb_path, h);
  88. dest_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  89. dest_bi->bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  90. break;
  91. case INTERNAL_INSERT_TO_R:
  92. dest_bi->tb = tb;
  93. dest_bi->bi_bh = tb->R[h];
  94. dest_bi->bi_parent = tb->FR[h];
  95. dest_bi->bi_position = get_right_neighbor_position(tb, h);
  96. break;
  97. default:
  98. reiserfs_panic(tb->tb_sb,
  99. "internal_define_dest_src_infos: shift type is unknown (%d)",
  100. shift_mode);
  101. }
  102. }
  103. /* Insert count node pointers into buffer cur before position to + 1.
  104. * Insert count items into buffer cur before position to.
  105. * Items and node pointers are specified by inserted and bh respectively.
  106. */
  107. static void internal_insert_childs(struct buffer_info *cur_bi,
  108. int to, int count,
  109. struct item_head *inserted,
  110. struct buffer_head **bh)
  111. {
  112. struct buffer_head *cur = cur_bi->bi_bh;
  113. struct block_head *blkh;
  114. int nr;
  115. struct reiserfs_key *ih;
  116. struct disk_child new_dc[2];
  117. struct disk_child *dc;
  118. int i;
  119. if (count <= 0)
  120. return;
  121. blkh = B_BLK_HEAD(cur);
  122. nr = blkh_nr_item(blkh);
  123. RFALSE(count > 2, "too many children (%d) are to be inserted", count);
  124. RFALSE(B_FREE_SPACE(cur) < count * (KEY_SIZE + DC_SIZE),
  125. "no enough free space (%d), needed %d bytes",
  126. B_FREE_SPACE(cur), count * (KEY_SIZE + DC_SIZE));
  127. /* prepare space for count disk_child */
  128. dc = B_N_CHILD(cur, to + 1);
  129. memmove(dc + count, dc, (nr + 1 - (to + 1)) * DC_SIZE);
  130. /* copy to_be_insert disk children */
  131. for (i = 0; i < count; i++) {
  132. put_dc_size(&(new_dc[i]),
  133. MAX_CHILD_SIZE(bh[i]) - B_FREE_SPACE(bh[i]));
  134. put_dc_block_number(&(new_dc[i]), bh[i]->b_blocknr);
  135. }
  136. memcpy(dc, new_dc, DC_SIZE * count);
  137. /* prepare space for count items */
  138. ih = B_N_PDELIM_KEY(cur, ((to == -1) ? 0 : to));
  139. memmove(ih + count, ih,
  140. (nr - to) * KEY_SIZE + (nr + 1 + count) * DC_SIZE);
  141. /* copy item headers (keys) */
  142. memcpy(ih, inserted, KEY_SIZE);
  143. if (count > 1)
  144. memcpy(ih + 1, inserted + 1, KEY_SIZE);
  145. /* sizes, item number */
  146. set_blkh_nr_item(blkh, blkh_nr_item(blkh) + count);
  147. set_blkh_free_space(blkh,
  148. blkh_free_space(blkh) - count * (DC_SIZE +
  149. KEY_SIZE));
  150. do_balance_mark_internal_dirty(cur_bi->tb, cur, 0);
  151. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  152. check_internal(cur);
  153. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  154. if (cur_bi->bi_parent) {
  155. struct disk_child *t_dc =
  156. B_N_CHILD(cur_bi->bi_parent, cur_bi->bi_position);
  157. put_dc_size(t_dc,
  158. dc_size(t_dc) + (count * (DC_SIZE + KEY_SIZE)));
  159. do_balance_mark_internal_dirty(cur_bi->tb, cur_bi->bi_parent,
  160. 0);
  161. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  162. check_internal(cur_bi->bi_parent);
  163. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  164. }
  165. }
  166. /* Delete del_num items and node pointers from buffer cur starting from *
  167. * the first_i'th item and first_p'th pointers respectively. */
  168. static void internal_delete_pointers_items(struct buffer_info *cur_bi,
  169. int first_p,
  170. int first_i, int del_num)
  171. {
  172. struct buffer_head *cur = cur_bi->bi_bh;
  173. int nr;
  174. struct block_head *blkh;
  175. struct reiserfs_key *key;
  176. struct disk_child *dc;
  177. RFALSE(cur == NULL, "buffer is 0");
  178. RFALSE(del_num < 0,
  179. "negative number of items (%d) can not be deleted", del_num);
  180. RFALSE(first_p < 0 || first_p + del_num > B_NR_ITEMS(cur) + 1
  181. || first_i < 0,
  182. "first pointer order (%d) < 0 or "
  183. "no so many pointers (%d), only (%d) or "
  184. "first key order %d < 0", first_p, first_p + del_num,
  185. B_NR_ITEMS(cur) + 1, first_i);
  186. if (del_num == 0)
  187. return;
  188. blkh = B_BLK_HEAD(cur);
  189. nr = blkh_nr_item(blkh);
  190. if (first_p == 0 && del_num == nr + 1) {
  191. RFALSE(first_i != 0,
  192. "1st deleted key must have order 0, not %d", first_i);
  193. make_empty_node(cur_bi);
  194. return;
  195. }
  196. RFALSE(first_i + del_num > B_NR_ITEMS(cur),
  197. "first_i = %d del_num = %d "
  198. "no so many keys (%d) in the node (%b)(%z)",
  199. first_i, del_num, first_i + del_num, cur, cur);
  200. /* deleting */
  201. dc = B_N_CHILD(cur, first_p);
  202. memmove(dc, dc + del_num, (nr + 1 - first_p - del_num) * DC_SIZE);
  203. key = B_N_PDELIM_KEY(cur, first_i);
  204. memmove(key, key + del_num,
  205. (nr - first_i - del_num) * KEY_SIZE + (nr + 1 -
  206. del_num) * DC_SIZE);
  207. /* sizes, item number */
  208. set_blkh_nr_item(blkh, blkh_nr_item(blkh) - del_num);
  209. set_blkh_free_space(blkh,
  210. blkh_free_space(blkh) +
  211. (del_num * (KEY_SIZE + DC_SIZE)));
  212. do_balance_mark_internal_dirty(cur_bi->tb, cur, 0);
  213. /*&&&&&&&&&&&&&&&&&&&&&&& */
  214. check_internal(cur);
  215. /*&&&&&&&&&&&&&&&&&&&&&&& */
  216. if (cur_bi->bi_parent) {
  217. struct disk_child *t_dc;
  218. t_dc = B_N_CHILD(cur_bi->bi_parent, cur_bi->bi_position);
  219. put_dc_size(t_dc,
  220. dc_size(t_dc) - (del_num * (KEY_SIZE + DC_SIZE)));
  221. do_balance_mark_internal_dirty(cur_bi->tb, cur_bi->bi_parent,
  222. 0);
  223. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  224. check_internal(cur_bi->bi_parent);
  225. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  226. }
  227. }
  228. /* delete n node pointers and items starting from given position */
  229. static void internal_delete_childs(struct buffer_info *cur_bi, int from, int n)
  230. {
  231. int i_from;
  232. i_from = (from == 0) ? from : from - 1;
  233. /* delete n pointers starting from `from' position in CUR;
  234. delete n keys starting from 'i_from' position in CUR;
  235. */
  236. internal_delete_pointers_items(cur_bi, from, i_from, n);
  237. }
  238. /* copy cpy_num node pointers and cpy_num - 1 items from buffer src to buffer dest
  239. * last_first == FIRST_TO_LAST means, that we copy first items from src to tail of dest
  240. * last_first == LAST_TO_FIRST means, that we copy last items from src to head of dest
  241. */
  242. static void internal_copy_pointers_items(struct buffer_info *dest_bi,
  243. struct buffer_head *src,
  244. int last_first, int cpy_num)
  245. {
  246. /* ATTENTION! Number of node pointers in DEST is equal to number of items in DEST *
  247. * as delimiting key have already inserted to buffer dest.*/
  248. struct buffer_head *dest = dest_bi->bi_bh;
  249. int nr_dest, nr_src;
  250. int dest_order, src_order;
  251. struct block_head *blkh;
  252. struct reiserfs_key *key;
  253. struct disk_child *dc;
  254. nr_src = B_NR_ITEMS(src);
  255. RFALSE(dest == NULL || src == NULL,
  256. "src (%p) or dest (%p) buffer is 0", src, dest);
  257. RFALSE(last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST,
  258. "invalid last_first parameter (%d)", last_first);
  259. RFALSE(nr_src < cpy_num - 1,
  260. "no so many items (%d) in src (%d)", cpy_num, nr_src);
  261. RFALSE(cpy_num < 0, "cpy_num less than 0 (%d)", cpy_num);
  262. RFALSE(cpy_num - 1 + B_NR_ITEMS(dest) > (int)MAX_NR_KEY(dest),
  263. "cpy_num (%d) + item number in dest (%d) can not be > MAX_NR_KEY(%d)",
  264. cpy_num, B_NR_ITEMS(dest), MAX_NR_KEY(dest));
  265. if (cpy_num == 0)
  266. return;
  267. /* coping */
  268. blkh = B_BLK_HEAD(dest);
  269. nr_dest = blkh_nr_item(blkh);
  270. /*dest_order = (last_first == LAST_TO_FIRST) ? 0 : nr_dest; */
  271. /*src_order = (last_first == LAST_TO_FIRST) ? (nr_src - cpy_num + 1) : 0; */
  272. (last_first == LAST_TO_FIRST) ? (dest_order = 0, src_order =
  273. nr_src - cpy_num + 1) : (dest_order =
  274. nr_dest,
  275. src_order =
  276. 0);
  277. /* prepare space for cpy_num pointers */
  278. dc = B_N_CHILD(dest, dest_order);
  279. memmove(dc + cpy_num, dc, (nr_dest - dest_order) * DC_SIZE);
  280. /* insert pointers */
  281. memcpy(dc, B_N_CHILD(src, src_order), DC_SIZE * cpy_num);
  282. /* prepare space for cpy_num - 1 item headers */
  283. key = B_N_PDELIM_KEY(dest, dest_order);
  284. memmove(key + cpy_num - 1, key,
  285. KEY_SIZE * (nr_dest - dest_order) + DC_SIZE * (nr_dest +
  286. cpy_num));
  287. /* insert headers */
  288. memcpy(key, B_N_PDELIM_KEY(src, src_order), KEY_SIZE * (cpy_num - 1));
  289. /* sizes, item number */
  290. set_blkh_nr_item(blkh, blkh_nr_item(blkh) + (cpy_num - 1));
  291. set_blkh_free_space(blkh,
  292. blkh_free_space(blkh) - (KEY_SIZE * (cpy_num - 1) +
  293. DC_SIZE * cpy_num));
  294. do_balance_mark_internal_dirty(dest_bi->tb, dest, 0);
  295. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  296. check_internal(dest);
  297. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  298. if (dest_bi->bi_parent) {
  299. struct disk_child *t_dc;
  300. t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
  301. put_dc_size(t_dc,
  302. dc_size(t_dc) + (KEY_SIZE * (cpy_num - 1) +
  303. DC_SIZE * cpy_num));
  304. do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
  305. 0);
  306. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  307. check_internal(dest_bi->bi_parent);
  308. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  309. }
  310. }
  311. /* Copy cpy_num node pointers and cpy_num - 1 items from buffer src to buffer dest.
  312. * Delete cpy_num - del_par items and node pointers from buffer src.
  313. * last_first == FIRST_TO_LAST means, that we copy/delete first items from src.
  314. * last_first == LAST_TO_FIRST means, that we copy/delete last items from src.
  315. */
  316. static void internal_move_pointers_items(struct buffer_info *dest_bi,
  317. struct buffer_info *src_bi,
  318. int last_first, int cpy_num,
  319. int del_par)
  320. {
  321. int first_pointer;
  322. int first_item;
  323. internal_copy_pointers_items(dest_bi, src_bi->bi_bh, last_first,
  324. cpy_num);
  325. if (last_first == FIRST_TO_LAST) { /* shift_left occurs */
  326. first_pointer = 0;
  327. first_item = 0;
  328. /* delete cpy_num - del_par pointers and keys starting for pointers with first_pointer,
  329. for key - with first_item */
  330. internal_delete_pointers_items(src_bi, first_pointer,
  331. first_item, cpy_num - del_par);
  332. } else { /* shift_right occurs */
  333. int i, j;
  334. i = (cpy_num - del_par ==
  335. (j =
  336. B_NR_ITEMS(src_bi->bi_bh)) + 1) ? 0 : j - cpy_num +
  337. del_par;
  338. internal_delete_pointers_items(src_bi,
  339. j + 1 - cpy_num + del_par, i,
  340. cpy_num - del_par);
  341. }
  342. }
  343. /* Insert n_src'th key of buffer src before n_dest'th key of buffer dest. */
  344. static void internal_insert_key(struct buffer_info *dest_bi, int dest_position_before, /* insert key before key with n_dest number */
  345. struct buffer_head *src, int src_position)
  346. {
  347. struct buffer_head *dest = dest_bi->bi_bh;
  348. int nr;
  349. struct block_head *blkh;
  350. struct reiserfs_key *key;
  351. RFALSE(dest == NULL || src == NULL,
  352. "source(%p) or dest(%p) buffer is 0", src, dest);
  353. RFALSE(dest_position_before < 0 || src_position < 0,
  354. "source(%d) or dest(%d) key number less than 0",
  355. src_position, dest_position_before);
  356. RFALSE(dest_position_before > B_NR_ITEMS(dest) ||
  357. src_position >= B_NR_ITEMS(src),
  358. "invalid position in dest (%d (key number %d)) or in src (%d (key number %d))",
  359. dest_position_before, B_NR_ITEMS(dest),
  360. src_position, B_NR_ITEMS(src));
  361. RFALSE(B_FREE_SPACE(dest) < KEY_SIZE,
  362. "no enough free space (%d) in dest buffer", B_FREE_SPACE(dest));
  363. blkh = B_BLK_HEAD(dest);
  364. nr = blkh_nr_item(blkh);
  365. /* prepare space for inserting key */
  366. key = B_N_PDELIM_KEY(dest, dest_position_before);
  367. memmove(key + 1, key,
  368. (nr - dest_position_before) * KEY_SIZE + (nr + 1) * DC_SIZE);
  369. /* insert key */
  370. memcpy(key, B_N_PDELIM_KEY(src, src_position), KEY_SIZE);
  371. /* Change dirt, free space, item number fields. */
  372. set_blkh_nr_item(blkh, blkh_nr_item(blkh) + 1);
  373. set_blkh_free_space(blkh, blkh_free_space(blkh) - KEY_SIZE);
  374. do_balance_mark_internal_dirty(dest_bi->tb, dest, 0);
  375. if (dest_bi->bi_parent) {
  376. struct disk_child *t_dc;
  377. t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
  378. put_dc_size(t_dc, dc_size(t_dc) + KEY_SIZE);
  379. do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
  380. 0);
  381. }
  382. }
  383. /* Insert d_key'th (delimiting) key from buffer cfl to tail of dest.
  384. * Copy pointer_amount node pointers and pointer_amount - 1 items from buffer src to buffer dest.
  385. * Replace d_key'th key in buffer cfl.
  386. * Delete pointer_amount items and node pointers from buffer src.
  387. */
  388. /* this can be invoked both to shift from S to L and from R to S */
  389. static void internal_shift_left(int mode, /* INTERNAL_FROM_S_TO_L | INTERNAL_FROM_R_TO_S */
  390. struct tree_balance *tb,
  391. int h, int pointer_amount)
  392. {
  393. struct buffer_info dest_bi, src_bi;
  394. struct buffer_head *cf;
  395. int d_key_position;
  396. internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
  397. &d_key_position, &cf);
  398. /*printk("pointer_amount = %d\n",pointer_amount); */
  399. if (pointer_amount) {
  400. /* insert delimiting key from common father of dest and src to node dest into position B_NR_ITEM(dest) */
  401. internal_insert_key(&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf,
  402. d_key_position);
  403. if (B_NR_ITEMS(src_bi.bi_bh) == pointer_amount - 1) {
  404. if (src_bi.bi_position /*src->b_item_order */ == 0)
  405. replace_key(tb, cf, d_key_position,
  406. src_bi.
  407. bi_parent /*src->b_parent */ , 0);
  408. } else
  409. replace_key(tb, cf, d_key_position, src_bi.bi_bh,
  410. pointer_amount - 1);
  411. }
  412. /* last parameter is del_parameter */
  413. internal_move_pointers_items(&dest_bi, &src_bi, FIRST_TO_LAST,
  414. pointer_amount, 0);
  415. }
  416. /* Insert delimiting key to L[h].
  417. * Copy n node pointers and n - 1 items from buffer S[h] to L[h].
  418. * Delete n - 1 items and node pointers from buffer S[h].
  419. */
  420. /* it always shifts from S[h] to L[h] */
  421. static void internal_shift1_left(struct tree_balance *tb,
  422. int h, int pointer_amount)
  423. {
  424. struct buffer_info dest_bi, src_bi;
  425. struct buffer_head *cf;
  426. int d_key_position;
  427. internal_define_dest_src_infos(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
  428. &dest_bi, &src_bi, &d_key_position, &cf);
  429. if (pointer_amount > 0) /* insert lkey[h]-th key from CFL[h] to left neighbor L[h] */
  430. internal_insert_key(&dest_bi, B_NR_ITEMS(dest_bi.bi_bh), cf,
  431. d_key_position);
  432. /* internal_insert_key (tb->L[h], B_NR_ITEM(tb->L[h]), tb->CFL[h], tb->lkey[h]); */
  433. /* last parameter is del_parameter */
  434. internal_move_pointers_items(&dest_bi, &src_bi, FIRST_TO_LAST,
  435. pointer_amount, 1);
  436. /* internal_move_pointers_items (tb->L[h], tb->S[h], FIRST_TO_LAST, pointer_amount, 1); */
  437. }
  438. /* Insert d_key'th (delimiting) key from buffer cfr to head of dest.
  439. * Copy n node pointers and n - 1 items from buffer src to buffer dest.
  440. * Replace d_key'th key in buffer cfr.
  441. * Delete n items and node pointers from buffer src.
  442. */
  443. static void internal_shift_right(int mode, /* INTERNAL_FROM_S_TO_R | INTERNAL_FROM_L_TO_S */
  444. struct tree_balance *tb,
  445. int h, int pointer_amount)
  446. {
  447. struct buffer_info dest_bi, src_bi;
  448. struct buffer_head *cf;
  449. int d_key_position;
  450. int nr;
  451. internal_define_dest_src_infos(mode, tb, h, &dest_bi, &src_bi,
  452. &d_key_position, &cf);
  453. nr = B_NR_ITEMS(src_bi.bi_bh);
  454. if (pointer_amount > 0) {
  455. /* insert delimiting key from common father of dest and src to dest node into position 0 */
  456. internal_insert_key(&dest_bi, 0, cf, d_key_position);
  457. if (nr == pointer_amount - 1) {
  458. RFALSE(src_bi.bi_bh != PATH_H_PBUFFER(tb->tb_path, h) /*tb->S[h] */ ||
  459. dest_bi.bi_bh != tb->R[h],
  460. "src (%p) must be == tb->S[h](%p) when it disappears",
  461. src_bi.bi_bh, PATH_H_PBUFFER(tb->tb_path, h));
  462. /* when S[h] disappers replace left delemiting key as well */
  463. if (tb->CFL[h])
  464. replace_key(tb, cf, d_key_position, tb->CFL[h],
  465. tb->lkey[h]);
  466. } else
  467. replace_key(tb, cf, d_key_position, src_bi.bi_bh,
  468. nr - pointer_amount);
  469. }
  470. /* last parameter is del_parameter */
  471. internal_move_pointers_items(&dest_bi, &src_bi, LAST_TO_FIRST,
  472. pointer_amount, 0);
  473. }
  474. /* Insert delimiting key to R[h].
  475. * Copy n node pointers and n - 1 items from buffer S[h] to R[h].
  476. * Delete n - 1 items and node pointers from buffer S[h].
  477. */
  478. /* it always shift from S[h] to R[h] */
  479. static void internal_shift1_right(struct tree_balance *tb,
  480. int h, int pointer_amount)
  481. {
  482. struct buffer_info dest_bi, src_bi;
  483. struct buffer_head *cf;
  484. int d_key_position;
  485. internal_define_dest_src_infos(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  486. &dest_bi, &src_bi, &d_key_position, &cf);
  487. if (pointer_amount > 0) /* insert rkey from CFR[h] to right neighbor R[h] */
  488. internal_insert_key(&dest_bi, 0, cf, d_key_position);
  489. /* internal_insert_key (tb->R[h], 0, tb->CFR[h], tb->rkey[h]); */
  490. /* last parameter is del_parameter */
  491. internal_move_pointers_items(&dest_bi, &src_bi, LAST_TO_FIRST,
  492. pointer_amount, 1);
  493. /* internal_move_pointers_items (tb->R[h], tb->S[h], LAST_TO_FIRST, pointer_amount, 1); */
  494. }
  495. /* Delete insert_num node pointers together with their left items
  496. * and balance current node.*/
  497. static void balance_internal_when_delete(struct tree_balance *tb,
  498. int h, int child_pos)
  499. {
  500. int insert_num;
  501. int n;
  502. struct buffer_head *tbSh = PATH_H_PBUFFER(tb->tb_path, h);
  503. struct buffer_info bi;
  504. insert_num = tb->insert_size[h] / ((int)(DC_SIZE + KEY_SIZE));
  505. /* delete child-node-pointer(s) together with their left item(s) */
  506. bi.tb = tb;
  507. bi.bi_bh = tbSh;
  508. bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  509. bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  510. internal_delete_childs(&bi, child_pos, -insert_num);
  511. RFALSE(tb->blknum[h] > 1,
  512. "tb->blknum[%d]=%d when insert_size < 0", h, tb->blknum[h]);
  513. n = B_NR_ITEMS(tbSh);
  514. if (tb->lnum[h] == 0 && tb->rnum[h] == 0) {
  515. if (tb->blknum[h] == 0) {
  516. /* node S[h] (root of the tree) is empty now */
  517. struct buffer_head *new_root;
  518. RFALSE(n
  519. || B_FREE_SPACE(tbSh) !=
  520. MAX_CHILD_SIZE(tbSh) - DC_SIZE,
  521. "buffer must have only 0 keys (%d)", n);
  522. RFALSE(bi.bi_parent, "root has parent (%p)",
  523. bi.bi_parent);
  524. /* choose a new root */
  525. if (!tb->L[h - 1] || !B_NR_ITEMS(tb->L[h - 1]))
  526. new_root = tb->R[h - 1];
  527. else
  528. new_root = tb->L[h - 1];
  529. /* switch super block's tree root block number to the new value */
  530. PUT_SB_ROOT_BLOCK(tb->tb_sb, new_root->b_blocknr);
  531. //REISERFS_SB(tb->tb_sb)->s_rs->s_tree_height --;
  532. PUT_SB_TREE_HEIGHT(tb->tb_sb,
  533. SB_TREE_HEIGHT(tb->tb_sb) - 1);
  534. do_balance_mark_sb_dirty(tb,
  535. REISERFS_SB(tb->tb_sb)->s_sbh,
  536. 1);
  537. /*&&&&&&&&&&&&&&&&&&&&&& */
  538. if (h > 1)
  539. /* use check_internal if new root is an internal node */
  540. check_internal(new_root);
  541. /*&&&&&&&&&&&&&&&&&&&&&& */
  542. /* do what is needed for buffer thrown from tree */
  543. reiserfs_invalidate_buffer(tb, tbSh);
  544. return;
  545. }
  546. return;
  547. }
  548. if (tb->L[h] && tb->lnum[h] == -B_NR_ITEMS(tb->L[h]) - 1) { /* join S[h] with L[h] */
  549. RFALSE(tb->rnum[h] != 0,
  550. "invalid tb->rnum[%d]==%d when joining S[h] with L[h]",
  551. h, tb->rnum[h]);
  552. internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h, n + 1);
  553. reiserfs_invalidate_buffer(tb, tbSh);
  554. return;
  555. }
  556. if (tb->R[h] && tb->rnum[h] == -B_NR_ITEMS(tb->R[h]) - 1) { /* join S[h] with R[h] */
  557. RFALSE(tb->lnum[h] != 0,
  558. "invalid tb->lnum[%d]==%d when joining S[h] with R[h]",
  559. h, tb->lnum[h]);
  560. internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h, n + 1);
  561. reiserfs_invalidate_buffer(tb, tbSh);
  562. return;
  563. }
  564. if (tb->lnum[h] < 0) { /* borrow from left neighbor L[h] */
  565. RFALSE(tb->rnum[h] != 0,
  566. "wrong tb->rnum[%d]==%d when borrow from L[h]", h,
  567. tb->rnum[h]);
  568. /*internal_shift_right (tb, h, tb->L[h], tb->CFL[h], tb->lkey[h], tb->S[h], -tb->lnum[h]); */
  569. internal_shift_right(INTERNAL_SHIFT_FROM_L_TO_S, tb, h,
  570. -tb->lnum[h]);
  571. return;
  572. }
  573. if (tb->rnum[h] < 0) { /* borrow from right neighbor R[h] */
  574. RFALSE(tb->lnum[h] != 0,
  575. "invalid tb->lnum[%d]==%d when borrow from R[h]",
  576. h, tb->lnum[h]);
  577. internal_shift_left(INTERNAL_SHIFT_FROM_R_TO_S, tb, h, -tb->rnum[h]); /*tb->S[h], tb->CFR[h], tb->rkey[h], tb->R[h], -tb->rnum[h]); */
  578. return;
  579. }
  580. if (tb->lnum[h] > 0) { /* split S[h] into two parts and put them into neighbors */
  581. RFALSE(tb->rnum[h] == 0 || tb->lnum[h] + tb->rnum[h] != n + 1,
  582. "invalid tb->lnum[%d]==%d or tb->rnum[%d]==%d when S[h](item number == %d) is split between them",
  583. h, tb->lnum[h], h, tb->rnum[h], n);
  584. internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h, tb->lnum[h]); /*tb->L[h], tb->CFL[h], tb->lkey[h], tb->S[h], tb->lnum[h]); */
  585. internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  586. tb->rnum[h]);
  587. reiserfs_invalidate_buffer(tb, tbSh);
  588. return;
  589. }
  590. reiserfs_panic(tb->tb_sb,
  591. "balance_internal_when_delete: unexpected tb->lnum[%d]==%d or tb->rnum[%d]==%d",
  592. h, tb->lnum[h], h, tb->rnum[h]);
  593. }
  594. /* Replace delimiting key of buffers L[h] and S[h] by the given key.*/
  595. static void replace_lkey(struct tree_balance *tb, int h, struct item_head *key)
  596. {
  597. RFALSE(tb->L[h] == NULL || tb->CFL[h] == NULL,
  598. "L[h](%p) and CFL[h](%p) must exist in replace_lkey",
  599. tb->L[h], tb->CFL[h]);
  600. if (B_NR_ITEMS(PATH_H_PBUFFER(tb->tb_path, h)) == 0)
  601. return;
  602. memcpy(B_N_PDELIM_KEY(tb->CFL[h], tb->lkey[h]), key, KEY_SIZE);
  603. do_balance_mark_internal_dirty(tb, tb->CFL[h], 0);
  604. }
  605. /* Replace delimiting key of buffers S[h] and R[h] by the given key.*/
  606. static void replace_rkey(struct tree_balance *tb, int h, struct item_head *key)
  607. {
  608. RFALSE(tb->R[h] == NULL || tb->CFR[h] == NULL,
  609. "R[h](%p) and CFR[h](%p) must exist in replace_rkey",
  610. tb->R[h], tb->CFR[h]);
  611. RFALSE(B_NR_ITEMS(tb->R[h]) == 0,
  612. "R[h] can not be empty if it exists (item number=%d)",
  613. B_NR_ITEMS(tb->R[h]));
  614. memcpy(B_N_PDELIM_KEY(tb->CFR[h], tb->rkey[h]), key, KEY_SIZE);
  615. do_balance_mark_internal_dirty(tb, tb->CFR[h], 0);
  616. }
  617. int balance_internal(struct tree_balance *tb, /* tree_balance structure */
  618. int h, /* level of the tree */
  619. int child_pos, struct item_head *insert_key, /* key for insertion on higher level */
  620. struct buffer_head **insert_ptr /* node for insertion on higher level */
  621. )
  622. /* if inserting/pasting
  623. {
  624. child_pos is the position of the node-pointer in S[h] that *
  625. pointed to S[h-1] before balancing of the h-1 level; *
  626. this means that new pointers and items must be inserted AFTER *
  627. child_pos
  628. }
  629. else
  630. {
  631. it is the position of the leftmost pointer that must be deleted (together with
  632. its corresponding key to the left of the pointer)
  633. as a result of the previous level's balancing.
  634. }
  635. */
  636. {
  637. struct buffer_head *tbSh = PATH_H_PBUFFER(tb->tb_path, h);
  638. struct buffer_info bi;
  639. int order; /* we return this: it is 0 if there is no S[h], else it is tb->S[h]->b_item_order */
  640. int insert_num, n, k;
  641. struct buffer_head *S_new;
  642. struct item_head new_insert_key;
  643. struct buffer_head *new_insert_ptr = NULL;
  644. struct item_head *new_insert_key_addr = insert_key;
  645. RFALSE(h < 1, "h (%d) can not be < 1 on internal level", h);
  646. PROC_INFO_INC(tb->tb_sb, balance_at[h]);
  647. order =
  648. (tbSh) ? PATH_H_POSITION(tb->tb_path,
  649. h + 1) /*tb->S[h]->b_item_order */ : 0;
  650. /* Using insert_size[h] calculate the number insert_num of items
  651. that must be inserted to or deleted from S[h]. */
  652. insert_num = tb->insert_size[h] / ((int)(KEY_SIZE + DC_SIZE));
  653. /* Check whether insert_num is proper * */
  654. RFALSE(insert_num < -2 || insert_num > 2,
  655. "incorrect number of items inserted to the internal node (%d)",
  656. insert_num);
  657. RFALSE(h > 1 && (insert_num > 1 || insert_num < -1),
  658. "incorrect number of items (%d) inserted to the internal node on a level (h=%d) higher than last internal level",
  659. insert_num, h);
  660. /* Make balance in case insert_num < 0 */
  661. if (insert_num < 0) {
  662. balance_internal_when_delete(tb, h, child_pos);
  663. return order;
  664. }
  665. k = 0;
  666. if (tb->lnum[h] > 0) {
  667. /* shift lnum[h] items from S[h] to the left neighbor L[h].
  668. check how many of new items fall into L[h] or CFL[h] after
  669. shifting */
  670. n = B_NR_ITEMS(tb->L[h]); /* number of items in L[h] */
  671. if (tb->lnum[h] <= child_pos) {
  672. /* new items don't fall into L[h] or CFL[h] */
  673. internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
  674. tb->lnum[h]);
  675. /*internal_shift_left (tb->L[h],tb->CFL[h],tb->lkey[h],tbSh,tb->lnum[h]); */
  676. child_pos -= tb->lnum[h];
  677. } else if (tb->lnum[h] > child_pos + insert_num) {
  678. /* all new items fall into L[h] */
  679. internal_shift_left(INTERNAL_SHIFT_FROM_S_TO_L, tb, h,
  680. tb->lnum[h] - insert_num);
  681. /* internal_shift_left(tb->L[h],tb->CFL[h],tb->lkey[h],tbSh,
  682. tb->lnum[h]-insert_num);
  683. */
  684. /* insert insert_num keys and node-pointers into L[h] */
  685. bi.tb = tb;
  686. bi.bi_bh = tb->L[h];
  687. bi.bi_parent = tb->FL[h];
  688. bi.bi_position = get_left_neighbor_position(tb, h);
  689. internal_insert_childs(&bi,
  690. /*tb->L[h], tb->S[h-1]->b_next */
  691. n + child_pos + 1,
  692. insert_num, insert_key,
  693. insert_ptr);
  694. insert_num = 0;
  695. } else {
  696. struct disk_child *dc;
  697. /* some items fall into L[h] or CFL[h], but some don't fall */
  698. internal_shift1_left(tb, h, child_pos + 1);
  699. /* calculate number of new items that fall into L[h] */
  700. k = tb->lnum[h] - child_pos - 1;
  701. bi.tb = tb;
  702. bi.bi_bh = tb->L[h];
  703. bi.bi_parent = tb->FL[h];
  704. bi.bi_position = get_left_neighbor_position(tb, h);
  705. internal_insert_childs(&bi,
  706. /*tb->L[h], tb->S[h-1]->b_next, */
  707. n + child_pos + 1, k,
  708. insert_key, insert_ptr);
  709. replace_lkey(tb, h, insert_key + k);
  710. /* replace the first node-ptr in S[h] by node-ptr to insert_ptr[k] */
  711. dc = B_N_CHILD(tbSh, 0);
  712. put_dc_size(dc,
  713. MAX_CHILD_SIZE(insert_ptr[k]) -
  714. B_FREE_SPACE(insert_ptr[k]));
  715. put_dc_block_number(dc, insert_ptr[k]->b_blocknr);
  716. do_balance_mark_internal_dirty(tb, tbSh, 0);
  717. k++;
  718. insert_key += k;
  719. insert_ptr += k;
  720. insert_num -= k;
  721. child_pos = 0;
  722. }
  723. }
  724. /* tb->lnum[h] > 0 */
  725. if (tb->rnum[h] > 0) {
  726. /*shift rnum[h] items from S[h] to the right neighbor R[h] */
  727. /* check how many of new items fall into R or CFR after shifting */
  728. n = B_NR_ITEMS(tbSh); /* number of items in S[h] */
  729. if (n - tb->rnum[h] >= child_pos)
  730. /* new items fall into S[h] */
  731. /*internal_shift_right(tb,h,tbSh,tb->CFR[h],tb->rkey[h],tb->R[h],tb->rnum[h]); */
  732. internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  733. tb->rnum[h]);
  734. else if (n + insert_num - tb->rnum[h] < child_pos) {
  735. /* all new items fall into R[h] */
  736. /*internal_shift_right(tb,h,tbSh,tb->CFR[h],tb->rkey[h],tb->R[h],
  737. tb->rnum[h] - insert_num); */
  738. internal_shift_right(INTERNAL_SHIFT_FROM_S_TO_R, tb, h,
  739. tb->rnum[h] - insert_num);
  740. /* insert insert_num keys and node-pointers into R[h] */
  741. bi.tb = tb;
  742. bi.bi_bh = tb->R[h];
  743. bi.bi_parent = tb->FR[h];
  744. bi.bi_position = get_right_neighbor_position(tb, h);
  745. internal_insert_childs(&bi,
  746. /*tb->R[h],tb->S[h-1]->b_next */
  747. child_pos - n - insert_num +
  748. tb->rnum[h] - 1,
  749. insert_num, insert_key,
  750. insert_ptr);
  751. insert_num = 0;
  752. } else {
  753. struct disk_child *dc;
  754. /* one of the items falls into CFR[h] */
  755. internal_shift1_right(tb, h, n - child_pos + 1);
  756. /* calculate number of new items that fall into R[h] */
  757. k = tb->rnum[h] - n + child_pos - 1;
  758. bi.tb = tb;
  759. bi.bi_bh = tb->R[h];
  760. bi.bi_parent = tb->FR[h];
  761. bi.bi_position = get_right_neighbor_position(tb, h);
  762. internal_insert_childs(&bi,
  763. /*tb->R[h], tb->R[h]->b_child, */
  764. 0, k, insert_key + 1,
  765. insert_ptr + 1);
  766. replace_rkey(tb, h, insert_key + insert_num - k - 1);
  767. /* replace the first node-ptr in R[h] by node-ptr insert_ptr[insert_num-k-1] */
  768. dc = B_N_CHILD(tb->R[h], 0);
  769. put_dc_size(dc,
  770. MAX_CHILD_SIZE(insert_ptr
  771. [insert_num - k - 1]) -
  772. B_FREE_SPACE(insert_ptr
  773. [insert_num - k - 1]));
  774. put_dc_block_number(dc,
  775. insert_ptr[insert_num - k -
  776. 1]->b_blocknr);
  777. do_balance_mark_internal_dirty(tb, tb->R[h], 0);
  778. insert_num -= (k + 1);
  779. }
  780. }
  781. /** Fill new node that appears instead of S[h] **/
  782. RFALSE(tb->blknum[h] > 2, "blknum can not be > 2 for internal level");
  783. RFALSE(tb->blknum[h] < 0, "blknum can not be < 0");
  784. if (!tb->blknum[h]) { /* node S[h] is empty now */
  785. RFALSE(!tbSh, "S[h] is equal NULL");
  786. /* do what is needed for buffer thrown from tree */
  787. reiserfs_invalidate_buffer(tb, tbSh);
  788. return order;
  789. }
  790. if (!tbSh) {
  791. /* create new root */
  792. struct disk_child *dc;
  793. struct buffer_head *tbSh_1 = PATH_H_PBUFFER(tb->tb_path, h - 1);
  794. struct block_head *blkh;
  795. if (tb->blknum[h] != 1)
  796. reiserfs_panic(NULL,
  797. "balance_internal: One new node required for creating the new root");
  798. /* S[h] = empty buffer from the list FEB. */
  799. tbSh = get_FEB(tb);
  800. blkh = B_BLK_HEAD(tbSh);
  801. set_blkh_level(blkh, h + 1);
  802. /* Put the unique node-pointer to S[h] that points to S[h-1]. */
  803. dc = B_N_CHILD(tbSh, 0);
  804. put_dc_block_number(dc, tbSh_1->b_blocknr);
  805. put_dc_size(dc,
  806. (MAX_CHILD_SIZE(tbSh_1) - B_FREE_SPACE(tbSh_1)));
  807. tb->insert_size[h] -= DC_SIZE;
  808. set_blkh_free_space(blkh, blkh_free_space(blkh) - DC_SIZE);
  809. do_balance_mark_internal_dirty(tb, tbSh, 0);
  810. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  811. check_internal(tbSh);
  812. /*&&&&&&&&&&&&&&&&&&&&&&&& */
  813. /* put new root into path structure */
  814. PATH_OFFSET_PBUFFER(tb->tb_path, ILLEGAL_PATH_ELEMENT_OFFSET) =
  815. tbSh;
  816. /* Change root in structure super block. */
  817. PUT_SB_ROOT_BLOCK(tb->tb_sb, tbSh->b_blocknr);
  818. PUT_SB_TREE_HEIGHT(tb->tb_sb, SB_TREE_HEIGHT(tb->tb_sb) + 1);
  819. do_balance_mark_sb_dirty(tb, REISERFS_SB(tb->tb_sb)->s_sbh, 1);
  820. }
  821. if (tb->blknum[h] == 2) {
  822. int snum;
  823. struct buffer_info dest_bi, src_bi;
  824. /* S_new = free buffer from list FEB */
  825. S_new = get_FEB(tb);
  826. set_blkh_level(B_BLK_HEAD(S_new), h + 1);
  827. dest_bi.tb = tb;
  828. dest_bi.bi_bh = S_new;
  829. dest_bi.bi_parent = NULL;
  830. dest_bi.bi_position = 0;
  831. src_bi.tb = tb;
  832. src_bi.bi_bh = tbSh;
  833. src_bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  834. src_bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  835. n = B_NR_ITEMS(tbSh); /* number of items in S[h] */
  836. snum = (insert_num + n + 1) / 2;
  837. if (n - snum >= child_pos) {
  838. /* new items don't fall into S_new */
  839. /* store the delimiting key for the next level */
  840. /* new_insert_key = (n - snum)'th key in S[h] */
  841. memcpy(&new_insert_key, B_N_PDELIM_KEY(tbSh, n - snum),
  842. KEY_SIZE);
  843. /* last parameter is del_par */
  844. internal_move_pointers_items(&dest_bi, &src_bi,
  845. LAST_TO_FIRST, snum, 0);
  846. /* internal_move_pointers_items(S_new, tbSh, LAST_TO_FIRST, snum, 0); */
  847. } else if (n + insert_num - snum < child_pos) {
  848. /* all new items fall into S_new */
  849. /* store the delimiting key for the next level */
  850. /* new_insert_key = (n + insert_item - snum)'th key in S[h] */
  851. memcpy(&new_insert_key,
  852. B_N_PDELIM_KEY(tbSh, n + insert_num - snum),
  853. KEY_SIZE);
  854. /* last parameter is del_par */
  855. internal_move_pointers_items(&dest_bi, &src_bi,
  856. LAST_TO_FIRST,
  857. snum - insert_num, 0);
  858. /* internal_move_pointers_items(S_new,tbSh,1,snum - insert_num,0); */
  859. /* insert insert_num keys and node-pointers into S_new */
  860. internal_insert_childs(&dest_bi,
  861. /*S_new,tb->S[h-1]->b_next, */
  862. child_pos - n - insert_num +
  863. snum - 1,
  864. insert_num, insert_key,
  865. insert_ptr);
  866. insert_num = 0;
  867. } else {
  868. struct disk_child *dc;
  869. /* some items fall into S_new, but some don't fall */
  870. /* last parameter is del_par */
  871. internal_move_pointers_items(&dest_bi, &src_bi,
  872. LAST_TO_FIRST,
  873. n - child_pos + 1, 1);
  874. /* internal_move_pointers_items(S_new,tbSh,1,n - child_pos + 1,1); */
  875. /* calculate number of new items that fall into S_new */
  876. k = snum - n + child_pos - 1;
  877. internal_insert_childs(&dest_bi, /*S_new, */ 0, k,
  878. insert_key + 1, insert_ptr + 1);
  879. /* new_insert_key = insert_key[insert_num - k - 1] */
  880. memcpy(&new_insert_key, insert_key + insert_num - k - 1,
  881. KEY_SIZE);
  882. /* replace first node-ptr in S_new by node-ptr to insert_ptr[insert_num-k-1] */
  883. dc = B_N_CHILD(S_new, 0);
  884. put_dc_size(dc,
  885. (MAX_CHILD_SIZE
  886. (insert_ptr[insert_num - k - 1]) -
  887. B_FREE_SPACE(insert_ptr
  888. [insert_num - k - 1])));
  889. put_dc_block_number(dc,
  890. insert_ptr[insert_num - k -
  891. 1]->b_blocknr);
  892. do_balance_mark_internal_dirty(tb, S_new, 0);
  893. insert_num -= (k + 1);
  894. }
  895. /* new_insert_ptr = node_pointer to S_new */
  896. new_insert_ptr = S_new;
  897. RFALSE(!buffer_journaled(S_new) || buffer_journal_dirty(S_new)
  898. || buffer_dirty(S_new), "cm-00001: bad S_new (%b)",
  899. S_new);
  900. // S_new is released in unfix_nodes
  901. }
  902. n = B_NR_ITEMS(tbSh); /*number of items in S[h] */
  903. if (0 <= child_pos && child_pos <= n && insert_num > 0) {
  904. bi.tb = tb;
  905. bi.bi_bh = tbSh;
  906. bi.bi_parent = PATH_H_PPARENT(tb->tb_path, h);
  907. bi.bi_position = PATH_H_POSITION(tb->tb_path, h + 1);
  908. internal_insert_childs(&bi, /*tbSh, */
  909. /* ( tb->S[h-1]->b_parent == tb->S[h] ) ? tb->S[h-1]->b_next : tb->S[h]->b_child->b_next, */
  910. child_pos, insert_num, insert_key,
  911. insert_ptr);
  912. }
  913. memcpy(new_insert_key_addr, &new_insert_key, KEY_SIZE);
  914. insert_ptr[0] = new_insert_ptr;
  915. return order;
  916. }