reiserfs: balance_leaf refactor, format balance_leaf_insert_left
[firefly-linux-kernel-4.4.55.git] / fs / reiserfs / do_balan.c
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  */
4
5 /*
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.
11  */
12
13 #include <asm/uaccess.h>
14 #include <linux/time.h>
15 #include "reiserfs.h"
16 #include <linux/buffer_head.h>
17 #include <linux/kernel.h>
18
19 static inline void buffer_info_init_left(struct tree_balance *tb,
20                                          struct buffer_info *bi)
21 {
22         bi->tb          = tb;
23         bi->bi_bh       = tb->L[0];
24         bi->bi_parent   = tb->FL[0];
25         bi->bi_position = get_left_neighbor_position(tb, 0);
26 }
27
28 static inline void buffer_info_init_right(struct tree_balance *tb,
29                                           struct buffer_info *bi)
30 {
31         bi->tb          = tb;
32         bi->bi_bh       = tb->R[0];
33         bi->bi_parent   = tb->FR[0];
34         bi->bi_position = get_right_neighbor_position(tb, 0);
35 }
36
37 static inline void buffer_info_init_tbS0(struct tree_balance *tb,
38                                          struct buffer_info *bi)
39 {
40         bi->tb          = tb;
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);
44 }
45
46 static inline void buffer_info_init_bh(struct tree_balance *tb,
47                                        struct buffer_info *bi,
48                                        struct buffer_head *bh)
49 {
50         bi->tb          = tb;
51         bi->bi_bh       = bh;
52         bi->bi_parent   = NULL;
53         bi->bi_position = 0;
54 }
55
56 inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
57                                        struct buffer_head *bh, int flag)
58 {
59         journal_mark_dirty(tb->transaction_handle, bh);
60 }
61
62 #define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty
63 #define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty
64
65 /*
66  * summary:
67  *  if deleting something ( tb->insert_size[0] < 0 )
68  *    return(balance_leaf_when_delete()); (flag d handled here)
69  *  else
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.
75  *
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.
81  *
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.
86  *
87  * -Hans
88  */
89
90 /*
91  * Balance leaf node in case of delete or cut: insert_size[0] < 0
92  *
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
97  *         to the neighbor
98  */
99 static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
100 {
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;
105         int n;
106         struct item_head *ih;
107
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");
114
115         ih = item_head(tbS0, item_pos);
116         buffer_info_init_tbS0(tb, &bi);
117
118         /* Delete or truncate the item */
119
120         switch (flag) {
121         case M_DELETE:          /* delete item in S[0] */
122
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);
126
127                 leaf_delete_items(&bi, 0, item_pos, 1, -1);
128
129                 if (!item_pos && tb->CFL[0]) {
130                         if (B_NR_ITEMS(tbS0)) {
131                                 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0,
132                                             0);
133                         } else {
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,
137                                                                    0), 0);
138                         }
139                 }
140
141                 RFALSE(!item_pos && !tb->CFL[0],
142                        "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
143                        tb->L[0]);
144
145                 break;
146
147         case M_CUT:{            /* cut item in S[0] */
148                         if (is_direntry_le_ih(ih)) {
149
150                                 /*
151                                  * UFS unlink semantics are such that you
152                                  * can only delete one directory entry at
153                                  * a time.
154                                  */
155
156                                 /*
157                                  * when we cut a directory tb->insert_size[0]
158                                  * means number of entries to be cut (always 1)
159                                  */
160                                 tb->insert_size[0] = -1;
161                                 leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
162                                                      -tb->insert_size[0]);
163
164                                 RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
165                                        "PAP-12030: can not change delimiting key. CFL[0]=%p",
166                                        tb->CFL[0]);
167
168                                 if (!item_pos && !pos_in_item && tb->CFL[0]) {
169                                         replace_key(tb, tb->CFL[0], tb->lkey[0],
170                                                     tbS0, 0);
171                                 }
172                         } else {
173                                 leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
174                                                      -tb->insert_size[0]);
175
176                                 RFALSE(!ih_item_len(ih),
177                                        "PAP-12035: cut must leave non-zero dynamic length of item");
178                         }
179                         break;
180                 }
181
182         default:
183                 print_cur_tb("12040");
184                 reiserfs_panic(tb->tb_sb, "PAP-12040",
185                                "unexpected mode: %s(%d)",
186                                (flag ==
187                                 M_PASTE) ? "PASTE" : ((flag ==
188                                                        M_INSERT) ? "INSERT" :
189                                                       "UNKNOWN"), flag);
190         }
191
192         /*
193          * the rule is that no shifting occurs unless by shifting
194          * a node can be freed
195          */
196         n = B_NR_ITEMS(tbS0);
197         /* L[0] takes part in balancing */
198         if (tb->lnum[0]) {
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)) {
204                                         /*
205                                          * all contents of all the 3 buffers
206                                          * will be in L[0]
207                                          */
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],
211                                                             tb->lkey[0],
212                                                             tb->FR[0], 1);
213
214                                         leaf_move_items(LEAF_FROM_S_TO_L, tb, n,
215                                                         -1, NULL);
216                                         leaf_move_items(LEAF_FROM_R_TO_L, tb,
217                                                         B_NR_ITEMS(tb->R[0]),
218                                                         -1, NULL);
219
220                                         reiserfs_invalidate_buffer(tb, tbS0);
221                                         reiserfs_invalidate_buffer(tb,
222                                                                    tb->R[0]);
223
224                                         return 0;
225                                 }
226                                 /*
227                                  * all contents of all the 3 buffers will
228                                  * be in R[0]
229                                  */
230                                 leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1,
231                                                 NULL);
232                                 leaf_move_items(LEAF_FROM_L_TO_R, tb,
233                                                 B_NR_ITEMS(tb->L[0]), -1, NULL);
234
235                                 /* right_delimiting_key is correct in R[0] */
236                                 replace_key(tb, tb->CFR[0], tb->rkey[0],
237                                             tb->R[0], 0);
238
239                                 reiserfs_invalidate_buffer(tb, tbS0);
240                                 reiserfs_invalidate_buffer(tb, tb->L[0]);
241
242                                 return -1;
243                         }
244
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);
249
250                         reiserfs_invalidate_buffer(tb, tbS0);
251
252                         return 0;
253                 }
254
255                 /*
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]
258                  */
259
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);
272
273                 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
274                 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
275
276                 reiserfs_invalidate_buffer(tb, tbS0);
277
278                 return 0;
279         }
280
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);
285                 return 0;
286         }
287
288         RFALSE(tb->rnum[0],
289                "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
290         return 0;
291 }
292
293 static void balance_leaf_insert_left(struct tree_balance *tb,
294                                      struct item_head *ih, const char *body)
295 {
296         int ret;
297         struct buffer_info bi;
298         int n = B_NR_ITEMS(tb->L[0]);
299
300         if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
301                 /* part of new item falls into L[0] */
302                 int new_item_len, shift;
303                 int version;
304
305                 ret = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
306
307                 /* Calculate item length to insert to S[0] */
308                 new_item_len = ih_item_len(ih) - tb->lbytes;
309
310                 /* Calculate and check item length to insert to L[0] */
311                 put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
312
313                 RFALSE(ih_item_len(ih) <= 0,
314                        "PAP-12080: there is nothing to insert into L[0]: "
315                        "ih_item_len=%d", ih_item_len(ih));
316
317                 /* Insert new item into L[0] */
318                 buffer_info_init_left(tb, &bi);
319                 leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body,
320                              min_t(int, tb->zeroes_num, ih_item_len(ih)));
321
322                 version = ih_version(ih);
323
324                 /*
325                  * Calculate key component, item length and body to
326                  * insert into S[0]
327                  */
328                 shift = 0;
329                 if (is_indirect_le_ih(ih))
330                         shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
331
332                 add_le_ih_k_offset(ih, tb->lbytes << shift);
333
334                 put_ih_item_len(ih, new_item_len);
335                 if (tb->lbytes > tb->zeroes_num) {
336                         body += (tb->lbytes - tb->zeroes_num);
337                         tb->zeroes_num = 0;
338                 } else
339                         tb->zeroes_num -= tb->lbytes;
340
341                 RFALSE(ih_item_len(ih) <= 0,
342                        "PAP-12085: there is nothing to insert into S[0]: "
343                        "ih_item_len=%d", ih_item_len(ih));
344         } else {
345                 /* new item in whole falls into L[0] */
346                 /* Shift lnum[0]-1 items to L[0] */
347                 ret = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
348
349                 /* Insert new item into L[0] */
350                 buffer_info_init_left(tb, &bi);
351                 leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body,
352                                      tb->zeroes_num);
353                 tb->insert_size[0] = 0;
354                 tb->zeroes_num = 0;
355         }
356 }
357
358 static void balance_leaf_paste_left(struct tree_balance *tb,
359                                     struct item_head *ih, const char *body)
360 {
361         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
362         int ret_val;
363         struct buffer_info bi;
364         int n = B_NR_ITEMS(tb->L[0]);
365
366                                 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
367                                         /* we must shift the part of the appended item */
368                                         if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
369
370                                                 RFALSE(tb->zeroes_num,
371                                                        "PAP-12090: invalid parameter in case of a directory");
372                                                 /* directory item */
373                                                 if (tb->lbytes > tb->pos_in_item) {
374                                                         /* new directory entry falls into L[0] */
375                                                         struct item_head *pasted;
376                                                         int l_pos_in_item = tb->pos_in_item;
377
378                                                         /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
379                                                         ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1);
380                                                         if (ret_val && !tb->item_pos) {
381                                                                 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
382                                                                 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1);
383                                                         }
384
385                                                         /* Append given directory entry to directory item */
386                                                         buffer_info_init_left(tb, &bi);
387                                                         leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, tb->zeroes_num);
388
389                                                         /* previous string prepared space for pasting new entry, following string pastes this entry */
390
391                                                         /* when we have merge directory item, pos_in_item has been changed too */
392
393                                                         /* paste new directory entry. 1 is entry number */
394                                                         leaf_paste_entries(&bi, n + tb->item_pos - ret_val, l_pos_in_item,
395                                                                            1, (struct reiserfs_de_head *) body,
396                                                                            body + DEH_SIZE, tb->insert_size[0]);
397                                                         tb->insert_size[0] = 0;
398                                                 } else {
399                                                         /* new directory item doesn't fall into L[0] */
400                                                         /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
401                                                         leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
402                                                 }
403                                                 /* Calculate new position to append in item body */
404                                                 tb->pos_in_item -= tb->lbytes;
405                                         } else {
406                                                 /* regular object */
407                                                 RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes);
408                                                 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
409                                                        "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
410                                                        ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
411
412                                                 if (tb->lbytes >= tb->pos_in_item) {
413                                                         /* appended item will be in L[0] in whole */
414                                                         int l_n;
415
416                                                         /* this bytes number must be appended to the last item of L[h] */
417                                                         l_n = tb->lbytes - tb->pos_in_item;
418
419                                                         /* Calculate new insert_size[0] */
420                                                         tb->insert_size[0] -= l_n;
421
422                                                         RFALSE(tb->insert_size[0] <= 0,
423                                                                "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
424                                                                tb->insert_size[0]);
425                                                         ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len
426                                                                             (item_head(tbS0, tb->item_pos)));
427                                                         /* Append to body of item in L[0] */
428                                                         buffer_info_init_left(tb, &bi);
429                                                         leaf_paste_in_buffer
430                                                             (&bi, n + tb->item_pos - ret_val, ih_item_len
431                                                              (item_head(tb->L[0], n + tb->item_pos - ret_val)),
432                                                              l_n, body,
433                                                              tb->zeroes_num > l_n ? l_n : tb->zeroes_num);
434                                                         /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
435                                                         {
436                                                                 int version;
437                                                                 int temp_l = l_n;
438
439                                                                 RFALSE(ih_item_len(item_head(tbS0, 0)),
440                                                                      "PAP-12106: item length must be 0");
441                                                                 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key
442                                                                       (tb->L[0], n + tb->item_pos - ret_val)),
443                                                                      "PAP-12107: items must be of the same file");
444                                                                 if (is_indirect_le_ih(item_head(tb->L[0], n + tb->item_pos - ret_val))) {
445                                                                         temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT);
446                                                                 }
447                                                                 /* update key of first item in S0 */
448                                                                 version = ih_version(item_head(tbS0, 0));
449                                                                 set_le_key_k_offset(version, leaf_key(tbS0, 0),
450                                                                      le_key_k_offset(version,leaf_key(tbS0, 0)) + temp_l);
451                                                                 /* update left delimiting key */
452                                                                 set_le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0]),
453                                                                      le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0])) + temp_l);
454                                                         }
455
456                                                         /* Calculate new body, position in item and insert_size[0] */
457                                                         if (l_n > tb->zeroes_num) {
458                                                                 body += (l_n - tb->zeroes_num);
459                                                                 tb->zeroes_num = 0;
460                                                         } else
461                                                                 tb->zeroes_num -= l_n;
462                                                         tb->pos_in_item = 0;
463
464                                                         RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1))
465                                                              || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)
466                                                              || !op_is_left_mergeable(internal_key(tb->CFL[0], tb->lkey[0]), tbS0->b_size),
467                                                              "PAP-12120: item must be merge-able with left neighboring item");
468                                                 } else {        /* only part of the appended item will be in L[0] */
469
470                                                         /* Calculate position in item for append in S[0] */
471                                                         tb->pos_in_item -= tb->lbytes;
472
473                                                         RFALSE(tb->pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", tb->pos_in_item);
474
475                                                         /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
476                                                         leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
477                                                 }
478                                         }
479                                 } else {        /* appended item will be in L[0] in whole */
480
481                                         struct item_head *pasted;
482
483                                         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 */
484                                                 /* then increment pos_in_item by the size of the last item in L[0] */
485                                                 pasted = item_head(tb->L[0], n - 1);
486                                                 if (is_direntry_le_ih(pasted))
487                                                         tb->pos_in_item += ih_entry_count(pasted);
488                                                 else
489                                                         tb->pos_in_item += ih_item_len(pasted);
490                                         }
491
492                                         /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
493                                         ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
494                                         /* Append to body of item in L[0] */
495                                         buffer_info_init_left(tb, &bi);
496                                         leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val,
497                                                              tb->pos_in_item,
498                                                              tb->insert_size[0],
499                                                              body, tb->zeroes_num);
500
501                                         /* if appended item is directory, paste entry */
502                                         pasted = item_head(tb->L[0], n + tb->item_pos - ret_val);
503                                         if (is_direntry_le_ih(pasted))
504                                                 leaf_paste_entries(&bi, n + tb->item_pos - ret_val,
505                                                                    tb->pos_in_item, 1,
506                                                                    (struct reiserfs_de_head *) body,
507                                                                    body + DEH_SIZE,
508                                                                    tb->insert_size[0]);
509                                         /* if appended item is indirect item, put unformatted node into un list */
510                                         if (is_indirect_le_ih(pasted))
511                                                 set_ih_free_space(pasted, 0);
512                                         tb->insert_size[0] = 0;
513                                         tb->zeroes_num = 0;
514                                 }
515
516 }
517
518 /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
519 static void balance_leaf_left(struct tree_balance *tb, struct item_head *ih,
520                               const char *body, int flag)
521 {
522         if (tb->lnum[0] <= 0)
523                 return;
524
525         /* new item or it part falls to L[0], shift it too */
526         if (tb->item_pos < tb->lnum[0]) {
527                 BUG_ON(flag != M_INSERT && flag != M_PASTE);
528
529                 if (flag == M_INSERT)
530                         balance_leaf_insert_left(tb, ih, body);
531                 else /* M_PASTE */
532                         balance_leaf_paste_left(tb, ih, body);
533         } else
534                 /* new item doesn't fall into L[0] */
535                 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
536 }
537
538
539 static void balance_leaf_insert_right(struct tree_balance *tb,
540                                       struct item_head *ih, const char *body)
541 {
542
543         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
544         int n = B_NR_ITEMS(tbS0);
545         struct buffer_info bi;
546         int ret_val;
547                         if (n - tb->rnum[0] < tb->item_pos) {   /* new item or its part falls to R[0] */
548                                 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) {  /* part of new item falls into R[0] */
549                                         loff_t old_key_comp, old_len, r_zeroes_number;
550                                         const char *r_body;
551                                         int version;
552                                         loff_t offset;
553
554                                         leaf_shift_right(tb, tb->rnum[0] - 1, -1);
555
556                                         version = ih_version(ih);
557                                         /* Remember key component and item length */
558                                         old_key_comp = le_ih_k_offset(ih);
559                                         old_len = ih_item_len(ih);
560
561                                         /* Calculate key component and item length to insert into R[0] */
562                                         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));
563                                         set_le_ih_k_offset(ih, offset);
564                                         put_ih_item_len(ih, tb->rbytes);
565                                         /* Insert part of the item into R[0] */
566                                         buffer_info_init_right(tb, &bi);
567                                         if ((old_len - tb->rbytes) > tb->zeroes_num) {
568                                                 r_zeroes_number = 0;
569                                                 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
570                                         } else {
571                                                 r_body = body;
572                                                 r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes);
573                                                 tb->zeroes_num -= r_zeroes_number;
574                                         }
575
576                                         leaf_insert_into_buf(&bi, 0, ih, r_body,
577                                                              r_zeroes_number);
578
579                                         /* Replace right delimiting key by first key in R[0] */
580                                         replace_key(tb, tb->CFR[0], tb->rkey[0],
581                                                     tb->R[0], 0);
582
583                                         /* Calculate key component and item length to insert into S[0] */
584                                         set_le_ih_k_offset(ih, old_key_comp);
585                                         put_ih_item_len(ih, old_len - tb->rbytes);
586
587                                         tb->insert_size[0] -= tb->rbytes;
588
589                                 } else {        /* whole new item falls into R[0] */
590
591                                         /* Shift rnum[0]-1 items to R[0] */
592                                         ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
593                                         /* Insert new item into R[0] */
594                                         buffer_info_init_right(tb, &bi);
595                                         leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
596                                                              ih, body, tb->zeroes_num);
597
598                                         if (tb->item_pos - n + tb->rnum[0] - 1 == 0) {
599                                                 replace_key(tb, tb->CFR[0],
600                                                             tb->rkey[0],
601                                                             tb->R[0], 0);
602
603                                         }
604                                         tb->zeroes_num = tb->insert_size[0] = 0;
605                                 }
606                         } else {        /* new item or part of it doesn't fall into R[0] */
607
608                                 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
609                         }
610
611 }
612
613 static void balance_leaf_paste_right(struct tree_balance *tb,
614                                      struct item_head *ih, const char *body)
615 {
616         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
617         int n = B_NR_ITEMS(tbS0);
618         struct buffer_info bi;
619         int ret_val;
620
621                         if (n - tb->rnum[0] <= tb->item_pos) {  /* pasted item or part of it falls to R[0] */
622                                 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) {      /* we must shift the part of the appended item */
623                                         if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */
624                                                 int entry_count;
625
626                                                 RFALSE(tb->zeroes_num,
627                                                        "PAP-12145: invalid parameter in case of a directory");
628                                                 entry_count = ih_entry_count(item_head
629                                                                   (tbS0, tb->item_pos));
630                                                 if (entry_count - tb->rbytes <
631                                                     tb->pos_in_item)
632                                                         /* new directory entry falls into R[0] */
633                                                 {
634                                                         int paste_entry_position;
635
636                                                         RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0],
637                                                                "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
638                                                                tb->rbytes, entry_count);
639                                                         /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
640                                                         leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
641                                                         /* Paste given directory entry to directory item */
642                                                         paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1;
643                                                         buffer_info_init_right(tb, &bi);
644                                                         leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num);
645                                                         /* paste entry */
646                                                         leaf_paste_entries(&bi, 0, paste_entry_position, 1,
647                                                                            (struct reiserfs_de_head *) body,
648                                                                            body + DEH_SIZE, tb->insert_size[0]);
649
650                                                         if (paste_entry_position == 0) {
651                                                                 /* change delimiting keys */
652                                                                 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0);
653                                                         }
654
655                                                         tb->insert_size[0] = 0;
656                                                         tb->pos_in_item++;
657                                                 } else {        /* new directory entry doesn't fall into R[0] */
658
659                                                         leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
660                                                 }
661                                         } else {        /* regular object */
662
663                                                 int n_shift, n_rem, r_zeroes_number;
664                                                 const char *r_body;
665
666                                                 /* Calculate number of bytes which must be shifted from appended item */
667                                                 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0)
668                                                         n_shift = 0;
669
670                                                 RFALSE(tb->pos_in_item != ih_item_len
671                                                        (item_head(tbS0, tb->item_pos)),
672                                                        "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
673                                                        tb->pos_in_item, ih_item_len
674                                                        (item_head(tbS0, tb->item_pos)));
675
676                                                 leaf_shift_right(tb, tb->rnum[0], n_shift);
677                                                 /* Calculate number of bytes which must remain in body after appending to R[0] */
678                                                 if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0)
679                                                         n_rem = 0;
680
681                                                 {
682                                                         int version;
683                                                         unsigned long temp_rem = n_rem;
684
685                                                         version = ih_version(item_head(tb->R[0], 0));
686                                                         if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
687                                                                 temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT);
688                                                         }
689                                                         set_le_key_k_offset(version, leaf_key(tb->R[0], 0),
690                                                              le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem);
691                                                         set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
692                                                              le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem);
693                                                 }
694 /*                k_offset (leaf_key(tb->R[0],0)) += n_rem;
695                   k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/
696                                                 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
697
698                                                 /* Append part of body into R[0] */
699                                                 buffer_info_init_right(tb, &bi);
700                                                 if (n_rem > tb->zeroes_num) {
701                                                         r_zeroes_number = 0;
702                                                         r_body = body + n_rem - tb->zeroes_num;
703                                                 } else {
704                                                         r_body = body;
705                                                         r_zeroes_number = tb->zeroes_num - n_rem;
706                                                         tb->zeroes_num -= r_zeroes_number;
707                                                 }
708
709                                                 leaf_paste_in_buffer(&bi, 0, n_shift,
710                                                                      tb->insert_size[0] - n_rem,
711                                                                      r_body, r_zeroes_number);
712
713                                                 if (is_indirect_le_ih(item_head(tb->R[0], 0))) {
714 #if 0
715                                                         RFALSE(n_rem,
716                                                                "PAP-12160: paste more than one unformatted node pointer");
717 #endif
718                                                         set_ih_free_space(item_head(tb->R[0], 0), 0);
719                                                 }
720                                                 tb->insert_size[0] = n_rem;
721                                                 if (!n_rem)
722                                                         tb->pos_in_item++;
723                                         }
724                                 } else {        /* pasted item in whole falls into R[0] */
725
726                                         struct item_head *pasted;
727
728                                         ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
729                                         /* append item in R[0] */
730                                         if (tb->pos_in_item >= 0) {
731                                                 buffer_info_init_right(tb, &bi);
732                                                 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item,
733                                                                      tb->insert_size[0], body, tb->zeroes_num);
734                                         }
735
736                                         /* paste new entry, if item is directory item */
737                                         pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
738                                         if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
739                                                 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
740                                                                    tb->pos_in_item, 1,
741                                                                    (struct reiserfs_de_head *) body,
742                                                                    body + DEH_SIZE, tb->insert_size[0]);
743                                                 if (!tb->pos_in_item) {
744
745                                                         RFALSE(tb->item_pos - n + tb->rnum[0],
746                                                                "PAP-12165: directory item must be first item of node when pasting is in 0th position");
747
748                                                         /* update delimiting keys */
749                                                         replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
750                                                 }
751                                         }
752
753                                         if (is_indirect_le_ih(pasted))
754                                                 set_ih_free_space(pasted, 0);
755                                         tb->zeroes_num = tb->insert_size[0] = 0;
756                                 }
757                         } else {        /* new item doesn't fall into R[0] */
758
759                                 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
760                         }
761
762 }
763
764 /* shift rnum[0] items from S[0] to the right neighbor R[0] */
765 static void balance_leaf_right(struct tree_balance *tb, struct item_head *ih,
766                                const char *body, int flag)
767 {
768         if (tb->rnum[0] <= 0)
769                 return;
770
771         BUG_ON(flag != M_INSERT && flag != M_PASTE);
772
773         if (flag == M_INSERT)
774                 balance_leaf_insert_right(tb, ih, body);
775         else /* M_PASTE */
776                 balance_leaf_paste_right(tb, ih, body);
777
778 }
779
780 static void balance_leaf_new_nodes_insert(struct tree_balance *tb,
781                                           struct item_head *ih,
782                                           const char *body,
783                                           struct item_head *insert_key,
784                                           struct buffer_head **insert_ptr,
785                                           int i)
786 {
787         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
788         int n = B_NR_ITEMS(tbS0);
789         struct buffer_info bi;
790                         if (n - tb->snum[i] < tb->item_pos) {   /* new item or it's part falls to first new node S_new[i] */
791                                 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) {       /* part of new item falls into S_new[i] */
792                                         int old_key_comp, old_len, r_zeroes_number;
793                                         const char *r_body;
794                                         int version;
795
796                                         /* Move snum[i]-1 items from S[0] to S_new[i] */
797                                         leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
798                                                         tb->snum[i] - 1, -1,
799                                                         tb->S_new[i]);
800                                         /* Remember key component and item length */
801                                         version = ih_version(ih);
802                                         old_key_comp = le_ih_k_offset(ih);
803                                         old_len = ih_item_len(ih);
804
805                                         /* Calculate key component and item length to insert into S_new[i] */
806                                         set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
807                                                            ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
808
809                                         put_ih_item_len(ih, tb->sbytes[i]);
810
811                                         /* Insert part of the item into S_new[i] before 0-th item */
812                                         buffer_info_init_bh(tb, &bi, tb->S_new[i]);
813
814                                         if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
815                                                 r_zeroes_number = 0;
816                                                 r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num;
817                                         } else {
818                                                 r_body = body;
819                                                 r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]);
820                                                 tb->zeroes_num -= r_zeroes_number;
821                                         }
822
823                                         leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
824
825                                         /* Calculate key component and item length to insert into S[i] */
826                                         set_le_ih_k_offset(ih, old_key_comp);
827                                         put_ih_item_len(ih, old_len - tb->sbytes[i]);
828                                         tb->insert_size[0] -= tb->sbytes[i];
829                                 } else {        /* whole new item falls into S_new[i] */
830
831                                         /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
832                                         leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
833                                                         tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
834
835                                         /* Insert new item into S_new[i] */
836                                         buffer_info_init_bh(tb, &bi, tb->S_new[i]);
837                                         leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
838                                                              ih, body, tb->zeroes_num);
839
840                                         tb->zeroes_num = tb->insert_size[0] = 0;
841                                 }
842                         }
843
844                         else {  /* new item or it part don't falls into S_new[i] */
845
846                                 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
847                                                 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
848                         }
849 }
850
851 static void balance_leaf_new_nodes_paste(struct tree_balance *tb,
852                                          struct item_head *ih,
853                                          const char *body,
854                                          struct item_head *insert_key,
855                                          struct buffer_head **insert_ptr,
856                                          int i)
857 {
858         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
859         int n = B_NR_ITEMS(tbS0);
860         struct buffer_info bi;
861                         if (n - tb->snum[i] <= tb->item_pos) {  /* pasted item or part if it falls to S_new[i] */
862                                 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) {   /* we must shift part of the appended item */
863                                         struct item_head *aux_ih;
864
865                                         RFALSE(ih, "PAP-12210: ih must be 0");
866
867                                         aux_ih = item_head(tbS0, tb->item_pos);
868                                         if (is_direntry_le_ih(aux_ih)) {
869                                                 /* we append to directory item */
870
871                                                 int entry_count;
872
873                                                 entry_count = ih_entry_count(aux_ih);
874
875                                                 if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) {
876                                                         /* new directory entry falls into S_new[i] */
877
878                                                         RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0");
879                                                         RFALSE(tb->sbytes[i] - 1 >= entry_count,
880                                                                "PAP-12220: there are no so much entries (%d), only %d",
881                                                                tb->sbytes[i] - 1, entry_count);
882
883                                                         /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
884                                                         leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]);
885                                                         /* Paste given directory entry to directory item */
886                                                         buffer_info_init_bh(tb, &bi, tb->S_new[i]);
887                                                         leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1,
888                                                              tb->insert_size[0], body, tb->zeroes_num);
889                                                         /* paste new directory entry */
890                                                         leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1,
891                                                                            (struct reiserfs_de_head *) body,
892                                                                            body + DEH_SIZE, tb->insert_size[0]);
893                                                         tb->insert_size[0] = 0;
894                                                         tb->pos_in_item++;
895                                                 } else {        /* new directory entry doesn't fall into S_new[i] */
896                                                         leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]);
897                                                 }
898                                         } else {        /* regular object */
899
900                                                 int n_shift, n_rem, r_zeroes_number;
901                                                 const char *r_body;
902
903                                                 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0,
904                                                        "PAP-12225: item too short or insert_size <= 0");
905
906                                                 /* Calculate number of bytes which must be shifted from appended item */
907                                                 n_shift = tb->sbytes[i] - tb->insert_size[0];
908                                                 if (n_shift < 0)
909                                                         n_shift = 0;
910                                                 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]);
911
912                                                 /* Calculate number of bytes which must remain in body after append to S_new[i] */
913                                                 n_rem = tb->insert_size[0] - tb->sbytes[i];
914                                                 if (n_rem < 0)
915                                                         n_rem = 0;
916                                                 /* Append part of body into S_new[0] */
917                                                 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
918                                                 if (n_rem > tb->zeroes_num) {
919                                                         r_zeroes_number = 0;
920                                                         r_body = body + n_rem - tb->zeroes_num;
921                                                 } else {
922                                                         r_body = body;
923                                                         r_zeroes_number = tb->zeroes_num - n_rem;
924                                                         tb->zeroes_num -= r_zeroes_number;
925                                                 }
926
927                                                 leaf_paste_in_buffer(&bi, 0, n_shift,
928                                                                      tb->insert_size[0] - n_rem,
929                                                                      r_body, r_zeroes_number);
930                                                 {
931                                                         struct item_head *tmp;
932
933                                                         tmp = item_head(tb->S_new[i], 0);
934                                                         if (is_indirect_le_ih
935                                                             (tmp)) {
936                                                                 set_ih_free_space(tmp, 0);
937                                                                 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT)));
938                                                         } else {
939                                                                 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem);
940                                                         }
941                                                 }
942
943                                                 tb->insert_size[0] = n_rem;
944                                                 if (!n_rem)
945                                                         tb->pos_in_item++;
946                                         }
947                                 } else
948                                         /* item falls wholly into S_new[i] */
949                                 {
950                                         int leaf_mi;
951                                         struct item_head *pasted;
952
953 #ifdef CONFIG_REISERFS_CHECK
954                                         struct item_head *ih_check = item_head(tbS0, tb->item_pos);
955
956                                         if (!is_direntry_le_ih(ih_check)
957                                             && (tb->pos_in_item != ih_item_len(ih_check)
958                                                 || tb->insert_size[0] <= 0))
959                                                 reiserfs_panic(tb->tb_sb,
960                                                              "PAP-12235",
961                                                              "pos_in_item "
962                                                              "must be equal "
963                                                              "to ih_item_len");
964 #endif                          /* CONFIG_REISERFS_CHECK */
965
966                                         leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW,
967                                                             tb, tb->snum[i],
968                                                             tb->sbytes[i],
969                                                             tb->S_new[i]);
970
971                                         RFALSE(leaf_mi,
972                                                "PAP-12240: unexpected value returned by leaf_move_items (%d)",
973                                                leaf_mi);
974
975                                         /* paste into item */
976                                         buffer_info_init_bh(tb, &bi, tb->S_new[i]);
977                                         leaf_paste_in_buffer(&bi,
978                                                              tb->item_pos - n + tb->snum[i],
979                                                              tb->pos_in_item,
980                                                              tb->insert_size[0],
981                                                              body, tb->zeroes_num);
982
983                                         pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]);
984                                         if (is_direntry_le_ih(pasted)) {
985                                                 leaf_paste_entries(&bi,
986                                                                    tb->item_pos - n + tb->snum[i],
987                                                                    tb->pos_in_item, 1,
988                                                                    (struct reiserfs_de_head *)body,
989                                                                    body + DEH_SIZE,
990                                                                    tb->insert_size[0]
991                                                     );
992                                         }
993
994                                         /* if we paste to indirect item update ih_free_space */
995                                         if (is_indirect_le_ih(pasted))
996                                                 set_ih_free_space(pasted, 0);
997                                         tb->zeroes_num = tb->insert_size[0] = 0;
998                                 }
999                         }
1000
1001                         else {  /* pasted item doesn't fall into S_new[i] */
1002
1003                                 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
1004                                                 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
1005                         }
1006
1007 }
1008
1009 /* Fill new nodes that appear in place of S[0] */
1010 static void balance_leaf_new_nodes(struct tree_balance *tb,
1011                                    struct item_head *ih,
1012                                    const char *body,
1013                                    struct item_head *insert_key,
1014                                    struct buffer_head **insert_ptr,
1015                                    int flag)
1016 {
1017         int i;
1018         for (i = tb->blknum[0] - 2; i >= 0; i--) {
1019
1020                 RFALSE(!tb->snum[i],
1021                        "PAP-12200: snum[%d] == %d. Must be > 0", i,
1022                        tb->snum[i]);
1023
1024                 /* here we shift from S to S_new nodes */
1025
1026                 tb->S_new[i] = get_FEB(tb);
1027
1028                 /* initialized block type and tree level */
1029                 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
1030
1031                 switch (flag) {
1032                 case M_INSERT:  /* insert item */
1033                         balance_leaf_new_nodes_insert(tb, ih, body, insert_key,
1034                                                       insert_ptr, i);
1035                         break;
1036
1037                 case M_PASTE:   /* append item */
1038                         balance_leaf_new_nodes_paste(tb, ih, body, insert_key,
1039                                                      insert_ptr, i);
1040                         break;
1041                 default:        /* cases d and t */
1042                         reiserfs_panic(tb->tb_sb, "PAP-12245",
1043                                        "blknum > 2: unexpected mode: %s(%d)",
1044                                        (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
1045                 }
1046
1047                 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1048                 insert_ptr[i] = tb->S_new[i];
1049
1050                 RFALSE(!buffer_journaled(tb->S_new[i])
1051                        || buffer_journal_dirty(tb->S_new[i])
1052                        || buffer_dirty(tb->S_new[i]),
1053                        "PAP-12247: S_new[%d] : (%b)",
1054                        i, format_bh(tb->S_new[i]));
1055         }
1056 }
1057
1058 static void balance_leaf_finish_node_insert(struct tree_balance *tb,
1059                                             struct item_head *ih,
1060                                             const char *body)
1061 {
1062         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1063         struct buffer_info bi;
1064                         buffer_info_init_tbS0(tb, &bi);
1065                         leaf_insert_into_buf(&bi, tb->item_pos, ih,
1066                                              body, tb->zeroes_num);
1067
1068                         /*
1069                          * If we insert the first key
1070                          * change the delimiting key
1071                          */
1072                         if (tb->item_pos == 0) {
1073                                 if (tb->CFL[0]) /* can be 0 in reiserfsck */
1074                                         replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1075                         }
1076 }
1077
1078 static void balance_leaf_finish_node_paste(struct tree_balance *tb,
1079                                            struct item_head *ih,
1080                                            const char *body)
1081 {
1082         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1083         struct buffer_info bi;
1084                                 struct item_head *pasted;
1085
1086                                 pasted = item_head(tbS0, tb->item_pos);
1087                                 /* when directory, may be new entry already pasted */
1088                                 if (is_direntry_le_ih(pasted)) {
1089                                         if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
1090
1091                                                 RFALSE(!tb->insert_size[0],
1092                                                        "PAP-12260: insert_size is 0 already");
1093
1094                                                 /* prepare space */
1095                                                 buffer_info_init_tbS0(tb, &bi);
1096                                                 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1097                                                                      tb->insert_size[0], body,
1098                                                                      tb->zeroes_num);
1099
1100                                                 /* paste entry */
1101                                                 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
1102                                                                    (struct reiserfs_de_head *)body,
1103                                                                    body + DEH_SIZE,
1104                                                                    tb->insert_size[0]);
1105                                                 if (!tb->item_pos && !tb->pos_in_item) {
1106                                                         RFALSE(!tb->CFL[0] || !tb->L[0],
1107                                                                "PAP-12270: CFL[0]/L[0] must be specified");
1108                                                         if (tb->CFL[0])
1109                                                                 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1110                                                 }
1111                                                 tb->insert_size[0] = 0;
1112                                         }
1113                                 } else {        /* regular object */
1114                                         if (tb->pos_in_item == ih_item_len(pasted)) {
1115
1116                                                 RFALSE(tb->insert_size[0] <= 0,
1117                                                        "PAP-12275: insert size must not be %d",
1118                                                        tb->insert_size[0]);
1119                                                 buffer_info_init_tbS0(tb, &bi);
1120                                                 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1121                                                                      tb->insert_size[0], body, tb->zeroes_num);
1122
1123                                                 if (is_indirect_le_ih(pasted)) {
1124 #if 0
1125                                                         RFALSE(tb->
1126                                                                insert_size[0] !=
1127                                                                UNFM_P_SIZE,
1128                                                                "PAP-12280: insert_size for indirect item must be %d, not %d",
1129                                                                UNFM_P_SIZE,
1130                                                                tb->
1131                                                                insert_size[0]);
1132 #endif
1133                                                         set_ih_free_space(pasted, 0);
1134                                                 }
1135                                                 tb->insert_size[0] = 0;
1136                                         }
1137 #ifdef CONFIG_REISERFS_CHECK
1138                                         else {
1139                                                 if (tb->insert_size[0]) {
1140                                                         print_cur_tb("12285");
1141                                                         reiserfs_panic(tb->tb_sb,
1142                                                             "PAP-12285",
1143                                                             "insert_size "
1144                                                             "must be 0 "
1145                                                             "(%d)",
1146                                                             tb->insert_size[0]);
1147                                                 }
1148                                         }
1149 #endif                          /* CONFIG_REISERFS_CHECK */
1150
1151                                 }
1152 }
1153
1154 /*
1155  * if the affected item was not wholly shifted then we
1156  * perform all necessary operations on that part or whole
1157  * of the affected item which remains in S
1158  */
1159 static void balance_leaf_finish_node(struct tree_balance *tb,
1160                                       struct item_head *ih,
1161                                       const char *body, int flag)
1162 {
1163         /* if we must insert or append into buffer S[0] */
1164         if (0 <= tb->item_pos && tb->item_pos < tb->s0num) {
1165                 if (flag == M_INSERT)
1166                         balance_leaf_finish_node_insert(tb, ih, body);
1167                 else /* M_PASTE */
1168                         balance_leaf_finish_node_paste(tb, ih, body);
1169         }
1170 }
1171
1172 /**
1173  * balance_leaf - reiserfs tree balancing algorithm
1174  * @tb: tree balance state
1175  * @ih: item header of inserted item (little endian)
1176  * @body: body of inserted item or bytes to paste
1177  * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance)
1178  * passed back:
1179  * @insert_key: key to insert new nodes
1180  * @insert_ptr: array of nodes to insert at the next level
1181  *
1182  * In our processing of one level we sometimes determine what must be
1183  * inserted into the next higher level.  This insertion consists of a
1184  * key or two keys and their corresponding pointers.
1185  */
1186 static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1187                         const char *body, int flag,
1188                         struct item_head *insert_key,
1189                         struct buffer_head **insert_ptr)
1190 {
1191         struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1192
1193         PROC_INFO_INC(tb->tb_sb, balance_at[0]);
1194
1195         /* Make balance in case insert_size[0] < 0 */
1196         if (tb->insert_size[0] < 0)
1197                 return balance_leaf_when_delete(tb, flag);
1198
1199         tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
1200         tb->pos_in_item = tb->tb_path->pos_in_item,
1201         tb->zeroes_num = 0;
1202         if (flag == M_INSERT && !body)
1203                 tb->zeroes_num = ih_item_len(ih);
1204
1205         /*
1206          * for indirect item pos_in_item is measured in unformatted node
1207          * pointers. Recalculate to bytes
1208          */
1209         if (flag != M_INSERT
1210             && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
1211                 tb->pos_in_item *= UNFM_P_SIZE;
1212
1213         balance_leaf_left(tb, ih, body, flag);
1214
1215         /* tb->lnum[0] > 0 */
1216         /* Calculate new item position */
1217         tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
1218
1219         balance_leaf_right(tb, ih, body, flag);
1220
1221         /* tb->rnum[0] > 0 */
1222         RFALSE(tb->blknum[0] > 3,
1223                "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
1224         RFALSE(tb->blknum[0] < 0,
1225                "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
1226
1227         /*
1228          * if while adding to a node we discover that it is possible to split
1229          * it in two, and merge the left part into the left neighbor and the
1230          * right part into the right neighbor, eliminating the node
1231          */
1232         if (tb->blknum[0] == 0) {       /* node S[0] is empty now */
1233
1234                 RFALSE(!tb->lnum[0] || !tb->rnum[0],
1235                        "PAP-12190: lnum and rnum must not be zero");
1236                 /*
1237                  * if insertion was done before 0-th position in R[0], right
1238                  * delimiting key of the tb->L[0]'s and left delimiting key are
1239                  * not set correctly
1240                  */
1241                 if (tb->CFL[0]) {
1242                         if (!tb->CFR[0])
1243                                 reiserfs_panic(tb->tb_sb, "vs-12195",
1244                                                "CFR not initialized");
1245                         copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
1246                                  internal_key(tb->CFR[0], tb->rkey[0]));
1247                         do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
1248                 }
1249
1250                 reiserfs_invalidate_buffer(tb, tbS0);
1251                 return 0;
1252         }
1253
1254         balance_leaf_new_nodes(tb, ih, body, insert_key, insert_ptr, flag);
1255
1256         balance_leaf_finish_node(tb, ih, body, flag);
1257
1258 #ifdef CONFIG_REISERFS_CHECK
1259         if (flag == M_PASTE && tb->insert_size[0]) {
1260                 print_cur_tb("12290");
1261                 reiserfs_panic(tb->tb_sb,
1262                                "PAP-12290", "insert_size is still not 0 (%d)",
1263                                tb->insert_size[0]);
1264         }
1265 #endif
1266
1267         /* Leaf level of the tree is balanced (end of balance_leaf) */
1268         return 0;
1269 }
1270
1271 /* Make empty node */
1272 void make_empty_node(struct buffer_info *bi)
1273 {
1274         struct block_head *blkh;
1275
1276         RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
1277
1278         blkh = B_BLK_HEAD(bi->bi_bh);
1279         set_blkh_nr_item(blkh, 0);
1280         set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
1281
1282         if (bi->bi_parent)
1283                 B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
1284 }
1285
1286 /* Get first empty buffer */
1287 struct buffer_head *get_FEB(struct tree_balance *tb)
1288 {
1289         int i;
1290         struct buffer_info bi;
1291
1292         for (i = 0; i < MAX_FEB_SIZE; i++)
1293                 if (tb->FEB[i] != NULL)
1294                         break;
1295
1296         if (i == MAX_FEB_SIZE)
1297                 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
1298
1299         buffer_info_init_bh(tb, &bi, tb->FEB[i]);
1300         make_empty_node(&bi);
1301         set_buffer_uptodate(tb->FEB[i]);
1302         tb->used[i] = tb->FEB[i];
1303         tb->FEB[i] = NULL;
1304
1305         return tb->used[i];
1306 }
1307
1308 /* This is now used because reiserfs_free_block has to be able to schedule. */
1309 static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
1310 {
1311         int i;
1312
1313         if (buffer_dirty(bh))
1314                 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1315                                  "called with dirty buffer");
1316         for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
1317                 if (!tb->thrown[i]) {
1318                         tb->thrown[i] = bh;
1319                         get_bh(bh);     /* free_thrown puts this */
1320                         return;
1321                 }
1322         reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1323                          "too many thrown buffers");
1324 }
1325
1326 static void free_thrown(struct tree_balance *tb)
1327 {
1328         int i;
1329         b_blocknr_t blocknr;
1330         for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
1331                 if (tb->thrown[i]) {
1332                         blocknr = tb->thrown[i]->b_blocknr;
1333                         if (buffer_dirty(tb->thrown[i]))
1334                                 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1335                                                  "called with dirty buffer %d",
1336                                                  blocknr);
1337                         brelse(tb->thrown[i]);  /* incremented in store_thrown */
1338                         reiserfs_free_block(tb->transaction_handle, NULL,
1339                                             blocknr, 0);
1340                 }
1341         }
1342 }
1343
1344 void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
1345 {
1346         struct block_head *blkh;
1347         blkh = B_BLK_HEAD(bh);
1348         set_blkh_level(blkh, FREE_LEVEL);
1349         set_blkh_nr_item(blkh, 0);
1350
1351         clear_buffer_dirty(bh);
1352         store_thrown(tb, bh);
1353 }
1354
1355 /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
1356 void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1357                  struct buffer_head *src, int n_src)
1358 {
1359
1360         RFALSE(dest == NULL || src == NULL,
1361                "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
1362                src, dest);
1363         RFALSE(!B_IS_KEYS_LEVEL(dest),
1364                "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
1365                dest);
1366         RFALSE(n_dest < 0 || n_src < 0,
1367                "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
1368         RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
1369                "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
1370                n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
1371
1372         if (B_IS_ITEMS_LEVEL(src))
1373                 /* source buffer contains leaf node */
1374                 memcpy(internal_key(dest, n_dest), item_head(src, n_src),
1375                        KEY_SIZE);
1376         else
1377                 memcpy(internal_key(dest, n_dest), internal_key(src, n_src),
1378                        KEY_SIZE);
1379
1380         do_balance_mark_internal_dirty(tb, dest, 0);
1381 }
1382
1383 int get_left_neighbor_position(struct tree_balance *tb, int h)
1384 {
1385         int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1386
1387         RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
1388                "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
1389                h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
1390
1391         if (Sh_position == 0)
1392                 return B_NR_ITEMS(tb->FL[h]);
1393         else
1394                 return Sh_position - 1;
1395 }
1396
1397 int get_right_neighbor_position(struct tree_balance *tb, int h)
1398 {
1399         int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
1400
1401         RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
1402                "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
1403                h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
1404
1405         if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1406                 return 0;
1407         else
1408                 return Sh_position + 1;
1409 }
1410
1411 #ifdef CONFIG_REISERFS_CHECK
1412
1413 int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
1414 static void check_internal_node(struct super_block *s, struct buffer_head *bh,
1415                                 char *mes)
1416 {
1417         struct disk_child *dc;
1418         int i;
1419
1420         RFALSE(!bh, "PAP-12336: bh == 0");
1421
1422         if (!bh || !B_IS_IN_TREE(bh))
1423                 return;
1424
1425         RFALSE(!buffer_dirty(bh) &&
1426                !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
1427                "PAP-12337: buffer (%b) must be dirty", bh);
1428         dc = B_N_CHILD(bh, 0);
1429
1430         for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
1431                 if (!is_reusable(s, dc_block_number(dc), 1)) {
1432                         print_cur_tb(mes);
1433                         reiserfs_panic(s, "PAP-12338",
1434                                        "invalid child pointer %y in %b",
1435                                        dc, bh);
1436                 }
1437         }
1438 }
1439
1440 static int locked_or_not_in_tree(struct tree_balance *tb,
1441                                   struct buffer_head *bh, char *which)
1442 {
1443         if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
1444             !B_IS_IN_TREE(bh)) {
1445                 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
1446                 return 1;
1447         }
1448         return 0;
1449 }
1450
1451 static int check_before_balancing(struct tree_balance *tb)
1452 {
1453         int retval = 0;
1454
1455         if (REISERFS_SB(tb->tb_sb)->cur_tb) {
1456                 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1457                                "occurred based on cur_tb not being null at "
1458                                "this point in code. do_balance cannot properly "
1459                                "handle concurrent tree accesses on a same "
1460                                "mount point.");
1461         }
1462
1463         /*
1464          * double check that buffers that we will modify are unlocked.
1465          * (fix_nodes should already have prepped all of these for us).
1466          */
1467         if (tb->lnum[0]) {
1468                 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1469                 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1470                 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
1471                 check_leaf(tb->L[0]);
1472         }
1473         if (tb->rnum[0]) {
1474                 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1475                 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1476                 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
1477                 check_leaf(tb->R[0]);
1478         }
1479         retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1480                                         "S[0]");
1481         check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1482
1483         return retval;
1484 }
1485
1486 static void check_after_balance_leaf(struct tree_balance *tb)
1487 {
1488         if (tb->lnum[0]) {
1489                 if (B_FREE_SPACE(tb->L[0]) !=
1490                     MAX_CHILD_SIZE(tb->L[0]) -
1491                     dc_size(B_N_CHILD
1492                             (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1493                         print_cur_tb("12221");
1494                         reiserfs_panic(tb->tb_sb, "PAP-12355",
1495                                        "shift to left was incorrect");
1496                 }
1497         }
1498         if (tb->rnum[0]) {
1499                 if (B_FREE_SPACE(tb->R[0]) !=
1500                     MAX_CHILD_SIZE(tb->R[0]) -
1501                     dc_size(B_N_CHILD
1502                             (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1503                         print_cur_tb("12222");
1504                         reiserfs_panic(tb->tb_sb, "PAP-12360",
1505                                        "shift to right was incorrect");
1506                 }
1507         }
1508         if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1509             (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1510              (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1511               dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1512                                 PATH_H_POSITION(tb->tb_path, 1)))))) {
1513                 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1514                 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1515                              dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1516                                                PATH_H_POSITION(tb->tb_path,
1517                                                                1))));
1518                 print_cur_tb("12223");
1519                 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
1520                                  "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1521                                  "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
1522                                  left,
1523                                  MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1524                                  PATH_H_PBUFFER(tb->tb_path, 1),
1525                                  PATH_H_POSITION(tb->tb_path, 1),
1526                                  dc_size(B_N_CHILD
1527                                          (PATH_H_PBUFFER(tb->tb_path, 1),
1528                                           PATH_H_POSITION(tb->tb_path, 1))),
1529                                  right);
1530                 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
1531         }
1532 }
1533
1534 static void check_leaf_level(struct tree_balance *tb)
1535 {
1536         check_leaf(tb->L[0]);
1537         check_leaf(tb->R[0]);
1538         check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1539 }
1540
1541 static void check_internal_levels(struct tree_balance *tb)
1542 {
1543         int h;
1544
1545         /* check all internal nodes */
1546         for (h = 1; tb->insert_size[h]; h++) {
1547                 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1548                                     "BAD BUFFER ON PATH");
1549                 if (tb->lnum[h])
1550                         check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1551                 if (tb->rnum[h])
1552                         check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1553         }
1554
1555 }
1556
1557 #endif
1558
1559 /*
1560  * Now we have all of the buffers that must be used in balancing of
1561  * the tree.  We rely on the assumption that schedule() will not occur
1562  * while do_balance works. ( Only interrupt handlers are acceptable.)
1563  * We balance the tree according to the analysis made before this,
1564  * using buffers already obtained.  For SMP support it will someday be
1565  * necessary to add ordered locking of tb.
1566  */
1567
1568 /*
1569  * Some interesting rules of balancing:
1570  * we delete a maximum of two nodes per level per balancing: we never
1571  * delete R, when we delete two of three nodes L, S, R then we move
1572  * them into R.
1573  *
1574  * we only delete L if we are deleting two nodes, if we delete only
1575  * one node we delete S
1576  *
1577  * if we shift leaves then we shift as much as we can: this is a
1578  * deliberate policy of extremism in node packing which results in
1579  * higher average utilization after repeated random balance operations
1580  * at the cost of more memory copies and more balancing as a result of
1581  * small insertions to full nodes.
1582  *
1583  * if we shift internal nodes we try to evenly balance the node
1584  * utilization, with consequent less balancing at the cost of lower
1585  * utilization.
1586  *
1587  * one could argue that the policy for directories in leaves should be
1588  * that of internal nodes, but we will wait until another day to
1589  * evaluate this....  It would be nice to someday measure and prove
1590  * these assumptions as to what is optimal....
1591  */
1592
1593 static inline void do_balance_starts(struct tree_balance *tb)
1594 {
1595         /* use print_cur_tb() to see initial state of struct tree_balance */
1596
1597         /* store_print_tb (tb); */
1598
1599         /* do not delete, just comment it out */
1600         /*
1601         print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1602                  tb->tb_path->pos_in_item, tb, "check");
1603         */
1604         RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
1605 #ifdef CONFIG_REISERFS_CHECK
1606         REISERFS_SB(tb->tb_sb)->cur_tb = tb;
1607 #endif
1608 }
1609
1610 static inline void do_balance_completed(struct tree_balance *tb)
1611 {
1612
1613 #ifdef CONFIG_REISERFS_CHECK
1614         check_leaf_level(tb);
1615         check_internal_levels(tb);
1616         REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
1617 #endif
1618
1619         /*
1620          * reiserfs_free_block is no longer schedule safe.  So, we need to
1621          * put the buffers we want freed on the thrown list during do_balance,
1622          * and then free them now
1623          */
1624
1625         REISERFS_SB(tb->tb_sb)->s_do_balance++;
1626
1627         /* release all nodes hold to perform the balancing */
1628         unfix_nodes(tb);
1629
1630         free_thrown(tb);
1631 }
1632
1633 /*
1634  * do_balance - balance the tree
1635  *
1636  * @tb: tree_balance structure
1637  * @ih: item header of inserted item
1638  * @body: body of inserted item or bytes to paste
1639  * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste
1640  *
1641  * Cut means delete part of an item (includes removing an entry from a
1642  * directory).
1643  *
1644  * Delete means delete whole item.
1645  *
1646  * Insert means add a new item into the tree.
1647  *
1648  * Paste means to append to the end of an existing file or to
1649  * insert a directory entry.
1650  */
1651 void do_balance(struct tree_balance *tb, struct item_head *ih,
1652                 const char *body, int flag)
1653 {
1654         int child_pos;          /* position of a child node in its parent */
1655         int h;                  /* level of the tree being processed */
1656
1657         /*
1658          * in our processing of one level we sometimes determine what
1659          * must be inserted into the next higher level.  This insertion
1660          * consists of a key or two keys and their corresponding
1661          * pointers
1662          */
1663         struct item_head insert_key[2];
1664
1665         /* inserted node-ptrs for the next level */
1666         struct buffer_head *insert_ptr[2];
1667
1668         tb->tb_mode = flag;
1669         tb->need_balance_dirty = 0;
1670
1671         if (FILESYSTEM_CHANGED_TB(tb)) {
1672                 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1673                                "changed");
1674         }
1675         /* if we have no real work to do  */
1676         if (!tb->insert_size[0]) {
1677                 reiserfs_warning(tb->tb_sb, "PAP-12350",
1678                                  "insert_size == 0, mode == %c", flag);
1679                 unfix_nodes(tb);
1680                 return;
1681         }
1682
1683         atomic_inc(&fs_generation(tb->tb_sb));
1684         do_balance_starts(tb);
1685
1686         /*
1687          * balance_leaf returns 0 except if combining L R and S into
1688          * one node.  see balance_internal() for explanation of this
1689          * line of code.
1690          */
1691         child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1692             balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
1693
1694 #ifdef CONFIG_REISERFS_CHECK
1695         check_after_balance_leaf(tb);
1696 #endif
1697
1698         /* Balance internal level of the tree. */
1699         for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
1700                 child_pos =
1701                     balance_internal(tb, h, child_pos, insert_key, insert_ptr);
1702
1703         do_balance_completed(tb);
1704
1705 }