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