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