ibalance.c 35 KB

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