2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
6 * Now we have all buffers that must be used in balancing of the tree
7 * Further calculations can not cause schedule(), and thus the buffer
8 * tree will be stable until the balancing will be finished
9 * balance the tree according to the analysis made before,
10 * and using buffers obtained after all above.
13 #include <asm/uaccess.h>
14 #include <linux/time.h>
16 #include <linux/buffer_head.h>
17 #include <linux/kernel.h>
19 static inline void buffer_info_init_left(struct tree_balance *tb,
20 struct buffer_info *bi)
24 bi->bi_parent = tb->FL[0];
25 bi->bi_position = get_left_neighbor_position(tb, 0);
28 static inline void buffer_info_init_right(struct tree_balance *tb,
29 struct buffer_info *bi)
33 bi->bi_parent = tb->FR[0];
34 bi->bi_position = get_right_neighbor_position(tb, 0);
37 static inline void buffer_info_init_tbS0(struct tree_balance *tb,
38 struct buffer_info *bi)
41 bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
42 bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
43 bi->bi_position = PATH_H_POSITION(tb->tb_path, 1);
46 static inline void buffer_info_init_bh(struct tree_balance *tb,
47 struct buffer_info *bi,
48 struct buffer_head *bh)
56 inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
57 struct buffer_head *bh, int flag)
59 journal_mark_dirty(tb->transaction_handle, bh);
62 #define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty
63 #define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty
67 * if deleting something ( tb->insert_size[0] < 0 )
68 * return(balance_leaf_when_delete()); (flag d handled here)
70 * if lnum is larger than 0 we put items into the left node
71 * if rnum is larger than 0 we put items into the right node
72 * if snum1 is larger than 0 we put items into the new node s1
73 * if snum2 is larger than 0 we put items into the new node s2
74 * Note that all *num* count new items being created.
76 * It would be easier to read balance_leaf() if each of these summary
77 * lines was a separate procedure rather than being inlined. I think
78 * that there are many passages here and in balance_leaf_when_delete() in
79 * which two calls to one procedure can replace two passages, and it
80 * might save cache space and improve software maintenance costs to do so.
82 * Vladimir made the perceptive comment that we should offload most of
83 * the decision making in this function into fix_nodes/check_balance, and
84 * then create some sort of structure in tb that says what actions should
85 * be performed by do_balance.
91 * Balance leaf node in case of delete or cut: insert_size[0] < 0
93 * lnum, rnum can have values >= -1
94 * -1 means that the neighbor must be joined with S
95 * 0 means that nothing should be done with the neighbor
96 * >0 means to shift entirely or partly the specified number of items
99 static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
101 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
102 int item_pos = PATH_LAST_POSITION(tb->tb_path);
103 int pos_in_item = tb->tb_path->pos_in_item;
104 struct buffer_info bi;
106 struct item_head *ih;
108 RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
109 "vs- 12000: level: wrong FR %z", tb->FR[0]);
110 RFALSE(tb->blknum[0] > 1,
111 "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
112 RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
113 "PAP-12010: tree can not be empty");
115 ih = item_head(tbS0, item_pos);
116 buffer_info_init_tbS0(tb, &bi);
118 /* Delete or truncate the item */
121 case M_DELETE: /* delete item in S[0] */
123 RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
124 "vs-12013: mode Delete, insert size %d, ih to be deleted %h",
125 -tb->insert_size[0], ih);
127 leaf_delete_items(&bi, 0, item_pos, 1, -1);
129 if (!item_pos && tb->CFL[0]) {
130 if (B_NR_ITEMS(tbS0)) {
131 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0,
134 if (!PATH_H_POSITION(tb->tb_path, 1))
135 replace_key(tb, tb->CFL[0], tb->lkey[0],
136 PATH_H_PPARENT(tb->tb_path,
141 RFALSE(!item_pos && !tb->CFL[0],
142 "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
147 case M_CUT:{ /* cut item in S[0] */
148 if (is_direntry_le_ih(ih)) {
151 * UFS unlink semantics are such that you
152 * can only delete one directory entry at
157 * when we cut a directory tb->insert_size[0]
158 * means number of entries to be cut (always 1)
160 tb->insert_size[0] = -1;
161 leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
162 -tb->insert_size[0]);
164 RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
165 "PAP-12030: can not change delimiting key. CFL[0]=%p",
168 if (!item_pos && !pos_in_item && tb->CFL[0]) {
169 replace_key(tb, tb->CFL[0], tb->lkey[0],
173 leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
174 -tb->insert_size[0]);
176 RFALSE(!ih_item_len(ih),
177 "PAP-12035: cut must leave non-zero dynamic length of item");
183 print_cur_tb("12040");
184 reiserfs_panic(tb->tb_sb, "PAP-12040",
185 "unexpected mode: %s(%d)",
187 M_PASTE) ? "PASTE" : ((flag ==
188 M_INSERT) ? "INSERT" :
193 * the rule is that no shifting occurs unless by shifting
194 * a node can be freed
196 n = B_NR_ITEMS(tbS0);
197 /* L[0] takes part in balancing */
199 /* L[0] must be joined with S[0] */
200 if (tb->lnum[0] == -1) {
201 /* R[0] must be also joined with S[0] */
202 if (tb->rnum[0] == -1) {
203 if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
205 * all contents of all the 3 buffers
208 if (PATH_H_POSITION(tb->tb_path, 1) == 0
209 && 1 < B_NR_ITEMS(tb->FR[0]))
210 replace_key(tb, tb->CFL[0],
214 leaf_move_items(LEAF_FROM_S_TO_L, tb, n,
216 leaf_move_items(LEAF_FROM_R_TO_L, tb,
217 B_NR_ITEMS(tb->R[0]),
220 reiserfs_invalidate_buffer(tb, tbS0);
221 reiserfs_invalidate_buffer(tb,
227 * all contents of all the 3 buffers will
230 leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1,
232 leaf_move_items(LEAF_FROM_L_TO_R, tb,
233 B_NR_ITEMS(tb->L[0]), -1, NULL);
235 /* right_delimiting_key is correct in R[0] */
236 replace_key(tb, tb->CFR[0], tb->rkey[0],
239 reiserfs_invalidate_buffer(tb, tbS0);
240 reiserfs_invalidate_buffer(tb, tb->L[0]);
245 RFALSE(tb->rnum[0] != 0,
246 "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
247 /* all contents of L[0] and S[0] will be in L[0] */
248 leaf_shift_left(tb, n, -1);
250 reiserfs_invalidate_buffer(tb, tbS0);
256 * a part of contents of S[0] will be in L[0] and the
257 * rest part of S[0] will be in R[0]
260 RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
261 (tb->lnum[0] + tb->rnum[0] > n + 1),
262 "PAP-12050: rnum(%d) and lnum(%d) and item number(%d) in S[0] are not consistent",
263 tb->rnum[0], tb->lnum[0], n);
264 RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
265 (tb->lbytes != -1 || tb->rbytes != -1),
266 "PAP-12055: bad rbytes (%d)/lbytes (%d) parameters when items are not split",
267 tb->rbytes, tb->lbytes);
268 RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
269 (tb->lbytes < 1 || tb->rbytes != -1),
270 "PAP-12060: bad rbytes (%d)/lbytes (%d) parameters when items are split",
271 tb->rbytes, tb->lbytes);
273 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
274 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
276 reiserfs_invalidate_buffer(tb, tbS0);
281 if (tb->rnum[0] == -1) {
282 /* all contents of R[0] and S[0] will be in R[0] */
283 leaf_shift_right(tb, n, -1);
284 reiserfs_invalidate_buffer(tb, tbS0);
289 "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
293 static void balance_leaf_insert_left(struct tree_balance *tb,
294 struct item_head *ih, const char *body)
297 struct buffer_info bi;
298 int n = B_NR_ITEMS(tb->L[0]);
300 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
301 /* part of new item falls into L[0] */
305 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
307 /* Calculate item length to insert to S[0] */
308 new_item_len = ih_item_len(ih) - tb->lbytes;
309 /* Calculate and check item length to insert to L[0] */
310 put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
312 RFALSE(ih_item_len(ih) <= 0,
313 "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
316 /* Insert new item into L[0] */
317 buffer_info_init_left(tb, &bi);
318 leaf_insert_into_buf(&bi,
319 n + tb->item_pos - ret_val, ih, body,
320 tb->zeroes_num > ih_item_len(ih) ? ih_item_len(ih) : tb->zeroes_num);
322 version = ih_version(ih);
324 /* Calculate key component, item length and body to insert into S[0] */
325 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
326 (tb->lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
328 put_ih_item_len(ih, new_item_len);
329 if (tb->lbytes > tb->zeroes_num) {
330 body += (tb->lbytes - tb->zeroes_num);
333 tb->zeroes_num -= tb->lbytes;
335 RFALSE(ih_item_len(ih) <= 0,
336 "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
339 /* new item in whole falls into L[0] */
340 /* Shift lnum[0]-1 items to L[0] */
341 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
342 /* Insert new item into L[0] */
343 buffer_info_init_left(tb, &bi);
344 leaf_insert_into_buf(&bi, n + tb->item_pos - ret_val, ih, body, tb->zeroes_num);
345 tb->insert_size[0] = 0;
351 static void balance_leaf_paste_left(struct tree_balance *tb,
352 struct item_head *ih, const char *body)
354 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
356 struct buffer_info bi;
357 int n = B_NR_ITEMS(tb->L[0]);
359 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
360 /* we must shift the part of the appended item */
361 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
363 RFALSE(tb->zeroes_num,
364 "PAP-12090: invalid parameter in case of a directory");
366 if (tb->lbytes > tb->pos_in_item) {
367 /* new directory entry falls into L[0] */
368 struct item_head *pasted;
369 int l_pos_in_item = tb->pos_in_item;
371 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
372 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1);
373 if (ret_val && !tb->item_pos) {
374 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
375 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1);
378 /* Append given directory entry to directory item */
379 buffer_info_init_left(tb, &bi);
380 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, tb->zeroes_num);
382 /* previous string prepared space for pasting new entry, following string pastes this entry */
384 /* when we have merge directory item, pos_in_item has been changed too */
386 /* paste new directory entry. 1 is entry number */
387 leaf_paste_entries(&bi, n + tb->item_pos - ret_val, l_pos_in_item,
388 1, (struct reiserfs_de_head *) body,
389 body + DEH_SIZE, tb->insert_size[0]);
390 tb->insert_size[0] = 0;
392 /* new directory item doesn't fall into L[0] */
393 /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
394 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
396 /* Calculate new position to append in item body */
397 tb->pos_in_item -= tb->lbytes;
400 RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes);
401 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
402 "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
403 ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
405 if (tb->lbytes >= tb->pos_in_item) {
406 /* appended item will be in L[0] in whole */
409 /* this bytes number must be appended to the last item of L[h] */
410 l_n = tb->lbytes - tb->pos_in_item;
412 /* Calculate new insert_size[0] */
413 tb->insert_size[0] -= l_n;
415 RFALSE(tb->insert_size[0] <= 0,
416 "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
418 ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len
419 (item_head(tbS0, tb->item_pos)));
420 /* Append to body of item in L[0] */
421 buffer_info_init_left(tb, &bi);
423 (&bi, n + tb->item_pos - ret_val, ih_item_len
424 (item_head(tb->L[0], n + tb->item_pos - ret_val)),
426 tb->zeroes_num > l_n ? l_n : tb->zeroes_num);
427 /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
432 RFALSE(ih_item_len(item_head(tbS0, 0)),
433 "PAP-12106: item length must be 0");
434 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key
435 (tb->L[0], n + tb->item_pos - ret_val)),
436 "PAP-12107: items must be of the same file");
437 if (is_indirect_le_ih(item_head(tb->L[0], n + tb->item_pos - ret_val))) {
438 temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT);
440 /* update key of first item in S0 */
441 version = ih_version(item_head(tbS0, 0));
442 set_le_key_k_offset(version, leaf_key(tbS0, 0),
443 le_key_k_offset(version,leaf_key(tbS0, 0)) + temp_l);
444 /* update left delimiting key */
445 set_le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0]),
446 le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0])) + temp_l);
449 /* Calculate new body, position in item and insert_size[0] */
450 if (l_n > tb->zeroes_num) {
451 body += (l_n - tb->zeroes_num);
454 tb->zeroes_num -= l_n;
457 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1))
458 || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)
459 || !op_is_left_mergeable(internal_key(tb->CFL[0], tb->lkey[0]), tbS0->b_size),
460 "PAP-12120: item must be merge-able with left neighboring item");
461 } else { /* only part of the appended item will be in L[0] */
463 /* Calculate position in item for append in S[0] */
464 tb->pos_in_item -= tb->lbytes;
466 RFALSE(tb->pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", tb->pos_in_item);
468 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
469 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
472 } else { /* appended item will be in L[0] in whole */
474 struct item_head *pasted;
476 if (!tb->item_pos && op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)) { /* if we paste into first item of S[0] and it is left mergable */
477 /* then increment pos_in_item by the size of the last item in L[0] */
478 pasted = item_head(tb->L[0], n - 1);
479 if (is_direntry_le_ih(pasted))
480 tb->pos_in_item += ih_entry_count(pasted);
482 tb->pos_in_item += ih_item_len(pasted);
485 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
486 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
487 /* Append to body of item in L[0] */
488 buffer_info_init_left(tb, &bi);
489 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val,
492 body, tb->zeroes_num);
494 /* if appended item is directory, paste entry */
495 pasted = item_head(tb->L[0], n + tb->item_pos - ret_val);
496 if (is_direntry_le_ih(pasted))
497 leaf_paste_entries(&bi, n + tb->item_pos - ret_val,
499 (struct reiserfs_de_head *) body,
502 /* if appended item is indirect item, put unformatted node into un list */
503 if (is_indirect_le_ih(pasted))
504 set_ih_free_space(pasted, 0);
505 tb->insert_size[0] = 0;
511 static void balance_leaf_insert_right(struct tree_balance *tb,
512 struct item_head *ih, const char *body)
515 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
516 int n = B_NR_ITEMS(tbS0);
517 struct buffer_info bi;
519 if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */
520 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */
521 loff_t old_key_comp, old_len, r_zeroes_number;
526 leaf_shift_right(tb, tb->rnum[0] - 1, -1);
528 version = ih_version(ih);
529 /* Remember key component and item length */
530 old_key_comp = le_ih_k_offset(ih);
531 old_len = ih_item_len(ih);
533 /* Calculate key component and item length to insert into R[0] */
534 offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0));
535 set_le_ih_k_offset(ih, offset);
536 put_ih_item_len(ih, tb->rbytes);
537 /* Insert part of the item into R[0] */
538 buffer_info_init_right(tb, &bi);
539 if ((old_len - tb->rbytes) > tb->zeroes_num) {
541 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
544 r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes);
545 tb->zeroes_num -= r_zeroes_number;
548 leaf_insert_into_buf(&bi, 0, ih, r_body,
551 /* Replace right delimiting key by first key in R[0] */
552 replace_key(tb, tb->CFR[0], tb->rkey[0],
555 /* Calculate key component and item length to insert into S[0] */
556 set_le_ih_k_offset(ih, old_key_comp);
557 put_ih_item_len(ih, old_len - tb->rbytes);
559 tb->insert_size[0] -= tb->rbytes;
561 } else { /* whole new item falls into R[0] */
563 /* Shift rnum[0]-1 items to R[0] */
564 ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
565 /* Insert new item into R[0] */
566 buffer_info_init_right(tb, &bi);
567 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
568 ih, body, tb->zeroes_num);
570 if (tb->item_pos - n + tb->rnum[0] - 1 == 0) {
571 replace_key(tb, tb->CFR[0],
576 tb->zeroes_num = tb->insert_size[0] = 0;
578 } else { /* new item or part of it doesn't fall into R[0] */
580 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
586 * balance_leaf - reiserfs tree balancing algorithm
587 * @tb: tree balance state
588 * @ih: item header of inserted item (little endian)
589 * @body: body of inserted item or bytes to paste
590 * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance)
592 * @insert_key: key to insert new nodes
593 * @insert_ptr: array of nodes to insert at the next level
595 * In our processing of one level we sometimes determine what must be
596 * inserted into the next higher level. This insertion consists of a
597 * key or two keys and their corresponding pointers.
599 static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
600 const char *body, int flag,
601 struct item_head *insert_key,
602 struct buffer_head **insert_ptr)
604 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
605 struct buffer_info bi;
609 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
611 /* Make balance in case insert_size[0] < 0 */
612 if (tb->insert_size[0] < 0)
613 return balance_leaf_when_delete(tb, flag);
615 tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
616 tb->pos_in_item = tb->tb_path->pos_in_item,
618 if (flag == M_INSERT && !body)
619 tb->zeroes_num = ih_item_len(ih);
622 * for indirect item pos_in_item is measured in unformatted node
623 * pointers. Recalculate to bytes
626 && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
627 tb->pos_in_item *= UNFM_P_SIZE;
629 if (tb->lnum[0] > 0) {
630 /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
631 if (tb->item_pos < tb->lnum[0]) {
632 /* new item or it part falls to L[0], shift it too */
633 n = B_NR_ITEMS(tb->L[0]);
636 case M_INSERT: /* insert item into L[0] */
637 balance_leaf_insert_left(tb, ih, body);
640 case M_PASTE: /* append item in L[0] */
641 balance_leaf_paste_left(tb, ih, body);
643 default: /* cases d and t */
644 reiserfs_panic(tb->tb_sb, "PAP-12130",
645 "lnum > 0: unexpected mode: "
647 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
650 /* new item doesn't fall into L[0] */
651 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
655 /* tb->lnum[0] > 0 */
656 /* Calculate new item position */
657 tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
659 if (tb->rnum[0] > 0) {
660 /* shift rnum[0] items from S[0] to the right neighbor R[0] */
661 n = B_NR_ITEMS(tbS0);
664 case M_INSERT: /* insert item */
665 balance_leaf_insert_right(tb, ih, body);
668 case M_PASTE: /* append item */
670 if (n - tb->rnum[0] <= tb->item_pos) { /* pasted item or part of it falls to R[0] */
671 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */
672 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */
675 RFALSE(tb->zeroes_num,
676 "PAP-12145: invalid parameter in case of a directory");
677 entry_count = ih_entry_count(item_head
678 (tbS0, tb->item_pos));
679 if (entry_count - tb->rbytes <
681 /* new directory entry falls into R[0] */
683 int paste_entry_position;
685 RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0],
686 "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
687 tb->rbytes, entry_count);
688 /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
689 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
690 /* Paste given directory entry to directory item */
691 paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1;
692 buffer_info_init_right(tb, &bi);
693 leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num);
695 leaf_paste_entries(&bi, 0, paste_entry_position, 1,
696 (struct reiserfs_de_head *) body,
697 body + DEH_SIZE, tb->insert_size[0]);
699 if (paste_entry_position == 0) {
700 /* change delimiting keys */
701 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0);
704 tb->insert_size[0] = 0;
706 } else { /* new directory entry doesn't fall into R[0] */
708 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
710 } else { /* regular object */
712 int n_shift, n_rem, r_zeroes_number;
715 /* Calculate number of bytes which must be shifted from appended item */
716 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0)
719 RFALSE(tb->pos_in_item != ih_item_len
720 (item_head(tbS0, tb->item_pos)),
721 "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
722 tb->pos_in_item, ih_item_len
723 (item_head(tbS0, tb->item_pos)));
725 leaf_shift_right(tb, tb->rnum[0], n_shift);
726 /* Calculate number of bytes which must remain in body after appending to R[0] */
727 if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0)
732 unsigned long temp_rem = n_rem;
734 version = ih_version(item_head(tb->R[0], 0));
735 if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
736 temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT);
738 set_le_key_k_offset(version, leaf_key(tb->R[0], 0),
739 le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem);
740 set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
741 le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem);
743 /* k_offset (leaf_key(tb->R[0],0)) += n_rem;
744 k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/
745 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
747 /* Append part of body into R[0] */
748 buffer_info_init_right(tb, &bi);
749 if (n_rem > tb->zeroes_num) {
751 r_body = body + n_rem - tb->zeroes_num;
754 r_zeroes_number = tb->zeroes_num - n_rem;
755 tb->zeroes_num -= r_zeroes_number;
758 leaf_paste_in_buffer(&bi, 0, n_shift,
759 tb->insert_size[0] - n_rem,
760 r_body, r_zeroes_number);
762 if (is_indirect_le_ih(item_head(tb->R[0], 0))) {
765 "PAP-12160: paste more than one unformatted node pointer");
767 set_ih_free_space(item_head(tb->R[0], 0), 0);
769 tb->insert_size[0] = n_rem;
773 } else { /* pasted item in whole falls into R[0] */
775 struct item_head *pasted;
777 ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
778 /* append item in R[0] */
779 if (tb->pos_in_item >= 0) {
780 buffer_info_init_right(tb, &bi);
781 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item,
782 tb->insert_size[0], body, tb->zeroes_num);
785 /* paste new entry, if item is directory item */
786 pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
787 if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
788 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
790 (struct reiserfs_de_head *) body,
791 body + DEH_SIZE, tb->insert_size[0]);
792 if (!tb->pos_in_item) {
794 RFALSE(tb->item_pos - n + tb->rnum[0],
795 "PAP-12165: directory item must be first item of node when pasting is in 0th position");
797 /* update delimiting keys */
798 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
802 if (is_indirect_le_ih(pasted))
803 set_ih_free_space(pasted, 0);
804 tb->zeroes_num = tb->insert_size[0] = 0;
806 } else { /* new item doesn't fall into R[0] */
808 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
811 default: /* cases d and t */
812 reiserfs_panic(tb->tb_sb, "PAP-12175",
813 "rnum > 0: unexpected mode: %s(%d)",
814 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
819 /* tb->rnum[0] > 0 */
820 RFALSE(tb->blknum[0] > 3,
821 "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
822 RFALSE(tb->blknum[0] < 0,
823 "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
826 * if while adding to a node we discover that it is possible to split
827 * it in two, and merge the left part into the left neighbor and the
828 * right part into the right neighbor, eliminating the node
830 if (tb->blknum[0] == 0) { /* node S[0] is empty now */
832 RFALSE(!tb->lnum[0] || !tb->rnum[0],
833 "PAP-12190: lnum and rnum must not be zero");
835 * if insertion was done before 0-th position in R[0], right
836 * delimiting key of the tb->L[0]'s and left delimiting key are
841 reiserfs_panic(tb->tb_sb, "vs-12195",
842 "CFR not initialized");
843 copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
844 internal_key(tb->CFR[0], tb->rkey[0]));
845 do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
848 reiserfs_invalidate_buffer(tb, tbS0);
852 /* Fill new nodes that appear in place of S[0] */
853 for (i = tb->blknum[0] - 2; i >= 0; i--) {
856 "PAP-12200: snum[%d] == %d. Must be > 0", i,
859 /* here we shift from S to S_new nodes */
861 tb->S_new[i] = get_FEB(tb);
863 /* initialized block type and tree level */
864 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
866 n = B_NR_ITEMS(tbS0);
869 case M_INSERT: /* insert item */
871 if (n - tb->snum[i] < tb->item_pos) { /* new item or it's part falls to first new node S_new[i] */
872 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { /* part of new item falls into S_new[i] */
873 int old_key_comp, old_len, r_zeroes_number;
877 /* Move snum[i]-1 items from S[0] to S_new[i] */
878 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
881 /* Remember key component and item length */
882 version = ih_version(ih);
883 old_key_comp = le_ih_k_offset(ih);
884 old_len = ih_item_len(ih);
886 /* Calculate key component and item length to insert into S_new[i] */
887 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
888 ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
890 put_ih_item_len(ih, tb->sbytes[i]);
892 /* Insert part of the item into S_new[i] before 0-th item */
893 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
895 if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
897 r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num;
900 r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]);
901 tb->zeroes_num -= r_zeroes_number;
904 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
906 /* Calculate key component and item length to insert into S[i] */
907 set_le_ih_k_offset(ih, old_key_comp);
908 put_ih_item_len(ih, old_len - tb->sbytes[i]);
909 tb->insert_size[0] -= tb->sbytes[i];
910 } else { /* whole new item falls into S_new[i] */
912 /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
913 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
914 tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
916 /* Insert new item into S_new[i] */
917 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
918 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
919 ih, body, tb->zeroes_num);
921 tb->zeroes_num = tb->insert_size[0] = 0;
925 else { /* new item or it part don't falls into S_new[i] */
927 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
928 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
932 case M_PASTE: /* append item */
934 if (n - tb->snum[i] <= tb->item_pos) { /* pasted item or part if it falls to S_new[i] */
935 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) { /* we must shift part of the appended item */
936 struct item_head *aux_ih;
938 RFALSE(ih, "PAP-12210: ih must be 0");
940 aux_ih = item_head(tbS0, tb->item_pos);
941 if (is_direntry_le_ih(aux_ih)) {
942 /* we append to directory item */
946 entry_count = ih_entry_count(aux_ih);
948 if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) {
949 /* new directory entry falls into S_new[i] */
951 RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0");
952 RFALSE(tb->sbytes[i] - 1 >= entry_count,
953 "PAP-12220: there are no so much entries (%d), only %d",
954 tb->sbytes[i] - 1, entry_count);
956 /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
957 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]);
958 /* Paste given directory entry to directory item */
959 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
960 leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1,
961 tb->insert_size[0], body, tb->zeroes_num);
962 /* paste new directory entry */
963 leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1,
964 (struct reiserfs_de_head *) body,
965 body + DEH_SIZE, tb->insert_size[0]);
966 tb->insert_size[0] = 0;
968 } else { /* new directory entry doesn't fall into S_new[i] */
969 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]);
971 } else { /* regular object */
973 int n_shift, n_rem, r_zeroes_number;
976 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0,
977 "PAP-12225: item too short or insert_size <= 0");
979 /* Calculate number of bytes which must be shifted from appended item */
980 n_shift = tb->sbytes[i] - tb->insert_size[0];
983 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]);
985 /* Calculate number of bytes which must remain in body after append to S_new[i] */
986 n_rem = tb->insert_size[0] - tb->sbytes[i];
989 /* Append part of body into S_new[0] */
990 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
991 if (n_rem > tb->zeroes_num) {
993 r_body = body + n_rem - tb->zeroes_num;
996 r_zeroes_number = tb->zeroes_num - n_rem;
997 tb->zeroes_num -= r_zeroes_number;
1000 leaf_paste_in_buffer(&bi, 0, n_shift,
1001 tb->insert_size[0] - n_rem,
1002 r_body, r_zeroes_number);
1004 struct item_head *tmp;
1006 tmp = item_head(tb->S_new[i], 0);
1007 if (is_indirect_le_ih
1009 set_ih_free_space(tmp, 0);
1010 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT)));
1012 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem);
1016 tb->insert_size[0] = n_rem;
1021 /* item falls wholly into S_new[i] */
1024 struct item_head *pasted;
1026 #ifdef CONFIG_REISERFS_CHECK
1027 struct item_head *ih_check = item_head(tbS0, tb->item_pos);
1029 if (!is_direntry_le_ih(ih_check)
1030 && (tb->pos_in_item != ih_item_len(ih_check)
1031 || tb->insert_size[0] <= 0))
1032 reiserfs_panic(tb->tb_sb,
1037 #endif /* CONFIG_REISERFS_CHECK */
1039 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW,
1045 "PAP-12240: unexpected value returned by leaf_move_items (%d)",
1048 /* paste into item */
1049 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1050 leaf_paste_in_buffer(&bi,
1051 tb->item_pos - n + tb->snum[i],
1054 body, tb->zeroes_num);
1056 pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]);
1057 if (is_direntry_le_ih(pasted)) {
1058 leaf_paste_entries(&bi,
1059 tb->item_pos - n + tb->snum[i],
1061 (struct reiserfs_de_head *)body,
1067 /* if we paste to indirect item update ih_free_space */
1068 if (is_indirect_le_ih(pasted))
1069 set_ih_free_space(pasted, 0);
1070 tb->zeroes_num = tb->insert_size[0] = 0;
1074 else { /* pasted item doesn't fall into S_new[i] */
1076 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
1077 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
1080 default: /* cases d and t */
1081 reiserfs_panic(tb->tb_sb, "PAP-12245",
1082 "blknum > 2: unexpected mode: %s(%d)",
1083 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
1086 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1087 insert_ptr[i] = tb->S_new[i];
1089 RFALSE(!buffer_journaled(tb->S_new[i])
1090 || buffer_journal_dirty(tb->S_new[i])
1091 || buffer_dirty(tb->S_new[i]),
1092 "PAP-12247: S_new[%d] : (%b)",
1096 /* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the
1097 affected item which remains in S */
1098 if (0 <= tb->item_pos && tb->item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */
1101 case M_INSERT: /* insert item into S[0] */
1102 buffer_info_init_tbS0(tb, &bi);
1103 leaf_insert_into_buf(&bi, tb->item_pos, ih,
1104 body, tb->zeroes_num);
1106 /* If we insert the first key change the delimiting key */
1107 if (tb->item_pos == 0) {
1108 if (tb->CFL[0]) /* can be 0 in reiserfsck */
1109 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1113 case M_PASTE:{ /* append item in S[0] */
1114 struct item_head *pasted;
1116 pasted = item_head(tbS0, tb->item_pos);
1117 /* when directory, may be new entry already pasted */
1118 if (is_direntry_le_ih(pasted)) {
1119 if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
1121 RFALSE(!tb->insert_size[0],
1122 "PAP-12260: insert_size is 0 already");
1125 buffer_info_init_tbS0(tb, &bi);
1126 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1127 tb->insert_size[0], body,
1131 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
1132 (struct reiserfs_de_head *)body,
1134 tb->insert_size[0]);
1135 if (!tb->item_pos && !tb->pos_in_item) {
1136 RFALSE(!tb->CFL[0] || !tb->L[0],
1137 "PAP-12270: CFL[0]/L[0] must be specified");
1139 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1141 tb->insert_size[0] = 0;
1143 } else { /* regular object */
1144 if (tb->pos_in_item == ih_item_len(pasted)) {
1146 RFALSE(tb->insert_size[0] <= 0,
1147 "PAP-12275: insert size must not be %d",
1148 tb->insert_size[0]);
1149 buffer_info_init_tbS0(tb, &bi);
1150 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1151 tb->insert_size[0], body, tb->zeroes_num);
1153 if (is_indirect_le_ih(pasted)) {
1158 "PAP-12280: insert_size for indirect item must be %d, not %d",
1163 set_ih_free_space(pasted, 0);
1165 tb->insert_size[0] = 0;
1167 #ifdef CONFIG_REISERFS_CHECK
1169 if (tb->insert_size[0]) {
1170 print_cur_tb("12285");
1171 reiserfs_panic(tb->tb_sb,
1176 tb->insert_size[0]);
1179 #endif /* CONFIG_REISERFS_CHECK */
1182 } /* case M_PASTE: */
1185 #ifdef CONFIG_REISERFS_CHECK
1186 if (flag == M_PASTE && tb->insert_size[0]) {
1187 print_cur_tb("12290");
1188 reiserfs_panic(tb->tb_sb,
1189 "PAP-12290", "insert_size is still not 0 (%d)",
1190 tb->insert_size[0]);
1194 /* Leaf level of the tree is balanced (end of balance_leaf) */
1198 /* Make empty node */
1199 void make_empty_node(struct buffer_info *bi)
1201 struct block_head *blkh;
1203 RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
1205 blkh = B_BLK_HEAD(bi->bi_bh);
1206 set_blkh_nr_item(blkh, 0);
1207 set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
1210 B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
1213 /* Get first empty buffer */
1214 struct buffer_head *get_FEB(struct tree_balance *tb)
1217 struct buffer_info bi;
1219 for (i = 0; i < MAX_FEB_SIZE; i++)
1220 if (tb->FEB[i] != NULL)
1223 if (i == MAX_FEB_SIZE)
1224 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
1226 buffer_info_init_bh(tb, &bi, tb->FEB[i]);
1227 make_empty_node(&bi);
1228 set_buffer_uptodate(tb->FEB[i]);
1229 tb->used[i] = tb->FEB[i];
1235 /* This is now used because reiserfs_free_block has to be able to schedule. */
1236 static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
1240 if (buffer_dirty(bh))
1241 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1242 "called with dirty buffer");
1243 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
1244 if (!tb->thrown[i]) {
1246 get_bh(bh); /* free_thrown puts this */
1249 reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1250 "too many thrown buffers");
1253 static void free_thrown(struct tree_balance *tb)
1256 b_blocknr_t blocknr;
1257 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
1258 if (tb->thrown[i]) {
1259 blocknr = tb->thrown[i]->b_blocknr;
1260 if (buffer_dirty(tb->thrown[i]))
1261 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1262 "called with dirty buffer %d",
1264 brelse(tb->thrown[i]); /* incremented in store_thrown */
1265 reiserfs_free_block(tb->transaction_handle, NULL,
1271 void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
1273 struct block_head *blkh;
1274 blkh = B_BLK_HEAD(bh);
1275 set_blkh_level(blkh, FREE_LEVEL);
1276 set_blkh_nr_item(blkh, 0);
1278 clear_buffer_dirty(bh);
1279 store_thrown(tb, bh);
1282 /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
1283 void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1284 struct buffer_head *src, int n_src)
1287 RFALSE(dest == NULL || src == NULL,
1288 "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
1290 RFALSE(!B_IS_KEYS_LEVEL(dest),
1291 "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
1293 RFALSE(n_dest < 0 || n_src < 0,
1294 "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
1295 RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
1296 "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
1297 n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
1299 if (B_IS_ITEMS_LEVEL(src))
1300 /* source buffer contains leaf node */
1301 memcpy(internal_key(dest, n_dest), item_head(src, n_src),
1304 memcpy(internal_key(dest, n_dest), internal_key(src, n_src),
1307 do_balance_mark_internal_dirty(tb, dest, 0);
1310 int get_left_neighbor_position(struct tree_balance *tb, int h)
1312 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1314 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
1315 "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
1316 h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
1318 if (Sh_position == 0)
1319 return B_NR_ITEMS(tb->FL[h]);
1321 return Sh_position - 1;
1324 int get_right_neighbor_position(struct tree_balance *tb, int h)
1326 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1328 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
1329 "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
1330 h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
1332 if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1335 return Sh_position + 1;
1338 #ifdef CONFIG_REISERFS_CHECK
1340 int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
1341 static void check_internal_node(struct super_block *s, struct buffer_head *bh,
1344 struct disk_child *dc;
1347 RFALSE(!bh, "PAP-12336: bh == 0");
1349 if (!bh || !B_IS_IN_TREE(bh))
1352 RFALSE(!buffer_dirty(bh) &&
1353 !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
1354 "PAP-12337: buffer (%b) must be dirty", bh);
1355 dc = B_N_CHILD(bh, 0);
1357 for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
1358 if (!is_reusable(s, dc_block_number(dc), 1)) {
1360 reiserfs_panic(s, "PAP-12338",
1361 "invalid child pointer %y in %b",
1367 static int locked_or_not_in_tree(struct tree_balance *tb,
1368 struct buffer_head *bh, char *which)
1370 if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
1371 !B_IS_IN_TREE(bh)) {
1372 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
1378 static int check_before_balancing(struct tree_balance *tb)
1382 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
1383 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1384 "occurred based on cur_tb not being null at "
1385 "this point in code. do_balance cannot properly "
1386 "handle concurrent tree accesses on a same "
1391 * double check that buffers that we will modify are unlocked.
1392 * (fix_nodes should already have prepped all of these for us).
1395 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1396 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1397 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
1398 check_leaf(tb->L[0]);
1401 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1402 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1403 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
1404 check_leaf(tb->R[0]);
1406 retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1408 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1413 static void check_after_balance_leaf(struct tree_balance *tb)
1416 if (B_FREE_SPACE(tb->L[0]) !=
1417 MAX_CHILD_SIZE(tb->L[0]) -
1419 (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1420 print_cur_tb("12221");
1421 reiserfs_panic(tb->tb_sb, "PAP-12355",
1422 "shift to left was incorrect");
1426 if (B_FREE_SPACE(tb->R[0]) !=
1427 MAX_CHILD_SIZE(tb->R[0]) -
1429 (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1430 print_cur_tb("12222");
1431 reiserfs_panic(tb->tb_sb, "PAP-12360",
1432 "shift to right was incorrect");
1435 if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1436 (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1437 (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1438 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1439 PATH_H_POSITION(tb->tb_path, 1)))))) {
1440 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1441 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1442 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1443 PATH_H_POSITION(tb->tb_path,
1445 print_cur_tb("12223");
1446 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
1447 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1448 "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
1450 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1451 PATH_H_PBUFFER(tb->tb_path, 1),
1452 PATH_H_POSITION(tb->tb_path, 1),
1454 (PATH_H_PBUFFER(tb->tb_path, 1),
1455 PATH_H_POSITION(tb->tb_path, 1))),
1457 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
1461 static void check_leaf_level(struct tree_balance *tb)
1463 check_leaf(tb->L[0]);
1464 check_leaf(tb->R[0]);
1465 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1468 static void check_internal_levels(struct tree_balance *tb)
1472 /* check all internal nodes */
1473 for (h = 1; tb->insert_size[h]; h++) {
1474 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1475 "BAD BUFFER ON PATH");
1477 check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1479 check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1487 * Now we have all of the buffers that must be used in balancing of
1488 * the tree. We rely on the assumption that schedule() will not occur
1489 * while do_balance works. ( Only interrupt handlers are acceptable.)
1490 * We balance the tree according to the analysis made before this,
1491 * using buffers already obtained. For SMP support it will someday be
1492 * necessary to add ordered locking of tb.
1496 * Some interesting rules of balancing:
1497 * we delete a maximum of two nodes per level per balancing: we never
1498 * delete R, when we delete two of three nodes L, S, R then we move
1501 * we only delete L if we are deleting two nodes, if we delete only
1502 * one node we delete S
1504 * if we shift leaves then we shift as much as we can: this is a
1505 * deliberate policy of extremism in node packing which results in
1506 * higher average utilization after repeated random balance operations
1507 * at the cost of more memory copies and more balancing as a result of
1508 * small insertions to full nodes.
1510 * if we shift internal nodes we try to evenly balance the node
1511 * utilization, with consequent less balancing at the cost of lower
1514 * one could argue that the policy for directories in leaves should be
1515 * that of internal nodes, but we will wait until another day to
1516 * evaluate this.... It would be nice to someday measure and prove
1517 * these assumptions as to what is optimal....
1520 static inline void do_balance_starts(struct tree_balance *tb)
1522 /* use print_cur_tb() to see initial state of struct tree_balance */
1524 /* store_print_tb (tb); */
1526 /* do not delete, just comment it out */
1528 print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1529 tb->tb_path->pos_in_item, tb, "check");
1531 RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
1532 #ifdef CONFIG_REISERFS_CHECK
1533 REISERFS_SB(tb->tb_sb)->cur_tb = tb;
1537 static inline void do_balance_completed(struct tree_balance *tb)
1540 #ifdef CONFIG_REISERFS_CHECK
1541 check_leaf_level(tb);
1542 check_internal_levels(tb);
1543 REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
1547 * reiserfs_free_block is no longer schedule safe. So, we need to
1548 * put the buffers we want freed on the thrown list during do_balance,
1549 * and then free them now
1552 REISERFS_SB(tb->tb_sb)->s_do_balance++;
1554 /* release all nodes hold to perform the balancing */
1561 * do_balance - balance the tree
1563 * @tb: tree_balance structure
1564 * @ih: item header of inserted item
1565 * @body: body of inserted item or bytes to paste
1566 * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste
1568 * Cut means delete part of an item (includes removing an entry from a
1571 * Delete means delete whole item.
1573 * Insert means add a new item into the tree.
1575 * Paste means to append to the end of an existing file or to
1576 * insert a directory entry.
1578 void do_balance(struct tree_balance *tb, struct item_head *ih,
1579 const char *body, int flag)
1581 int child_pos; /* position of a child node in its parent */
1582 int h; /* level of the tree being processed */
1585 * in our processing of one level we sometimes determine what
1586 * must be inserted into the next higher level. This insertion
1587 * consists of a key or two keys and their corresponding
1590 struct item_head insert_key[2];
1592 /* inserted node-ptrs for the next level */
1593 struct buffer_head *insert_ptr[2];
1596 tb->need_balance_dirty = 0;
1598 if (FILESYSTEM_CHANGED_TB(tb)) {
1599 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1602 /* if we have no real work to do */
1603 if (!tb->insert_size[0]) {
1604 reiserfs_warning(tb->tb_sb, "PAP-12350",
1605 "insert_size == 0, mode == %c", flag);
1610 atomic_inc(&fs_generation(tb->tb_sb));
1611 do_balance_starts(tb);
1614 * balance_leaf returns 0 except if combining L R and S into
1615 * one node. see balance_internal() for explanation of this
1618 child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1619 balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
1621 #ifdef CONFIG_REISERFS_CHECK
1622 check_after_balance_leaf(tb);
1625 /* Balance internal level of the tree. */
1626 for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
1628 balance_internal(tb, h, child_pos, insert_key, insert_ptr);
1630 do_balance_completed(tb);