ibalance.c 35 KB

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