ibalance.c 34 KB

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