blob: 5129efc6f2e6a0fa549fd7a7791e11bf195ec368 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Jeff Mahoney098297b2014-04-23 10:00:36 -04005/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Fabian Frederick170939912014-08-08 14:21:12 -070013#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/time.h>
Al Virof466c6f2012-03-17 01:16:43 -040015#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/buffer_head.h>
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -080017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -040019static 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
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070056inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
57 struct buffer_head *bh, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Jeff Mahoney09f1b802014-04-23 10:00:39 -040059 journal_mark_dirty(tb->transaction_handle, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
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
Jeff Mahoney098297b2014-04-23 10:00:36 -040065/*
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.
Jeff Mahoney098297b2014-04-23 10:00:36 -040075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jeff Mahoney83a3a562014-04-23 10:01:02 -040077static 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
Jeff Mahoney098297b2014-04-23 10:00:36 -0400230/*
231 * Balance leaf node in case of delete or cut: insert_size[0] < 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
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
Jeff Mahoney098297b2014-04-23 10:00:36 -0400236 * >0 means to shift entirely or partly the specified number of items
237 * to the neighbor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700239static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242 struct buffer_info bi;
243 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400252 buffer_info_init_tbS0(tb, &bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 /* Delete or truncate the item */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jeff Mahoney83a3a562014-04-23 10:01:02 -0400256 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Jeff Mahoney098297b2014-04-23 10:00:36 -0400263 /*
264 * the rule is that no shifting occurs unless by shifting
265 * a node can be freed
266 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267 n = B_NR_ITEMS(tbS0);
Jeff Mahoney83a3a562014-04-23 10:01:02 -0400268
269
Jeff Mahoney098297b2014-04-23 10:00:36 -0400270 /* L[0] takes part in balancing */
Jeff Mahoney83a3a562014-04-23 10:01:02 -0400271 if (tb->lnum[0])
272 return balance_leaf_when_delete_left(tb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273
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 }
280
281 RFALSE(tb->rnum[0],
282 "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400286static unsigned int balance_leaf_insert_left(struct tree_balance *tb,
287 struct item_head *const ih,
288 const char * const body)
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400289{
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400290 int ret;
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400291 struct buffer_info bi;
292 int n = B_NR_ITEMS(tb->L[0]);
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400293 unsigned body_shift_bytes = 0;
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400294
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400295 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;
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400298
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400299 ret = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400300
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400301 /* Calculate item length to insert to S[0] */
302 new_item_len = ih_item_len(ih) - tb->lbytes;
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400303
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400304 /* Calculate and check item length to insert to L[0] */
305 put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400306
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400307 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));
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400310
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400311 /* 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)));
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400315
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400316 /*
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;
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400323
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400324 add_le_ih_k_offset(ih, tb->lbytes << shift);
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400325
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400326 put_ih_item_len(ih, new_item_len);
327 if (tb->lbytes > tb->zeroes_num) {
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400328 body_shift_bytes = tb->lbytes - tb->zeroes_num;
Jeff Mahoney4bf4de62014-04-23 10:00:56 -0400329 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 }
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400348 return body_shift_bytes;
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400349}
350
Jeff Mahoney3fade932014-04-23 10:00:57 -0400351static void balance_leaf_paste_left_shift_dirent(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400352 struct item_head * const ih,
353 const char * const body)
Jeff Mahoney3fade932014-04-23 10:00:57 -0400354{
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
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400413static unsigned int balance_leaf_paste_left_shift(struct tree_balance *tb,
414 struct item_head * const ih,
415 const char * const body)
Jeff Mahoney3fade932014-04-23 10:00:57 -0400416{
417 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
418 int n = B_NR_ITEMS(tb->L[0]);
419 struct buffer_info bi;
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400420 int body_shift_bytes = 0;
Jeff Mahoney3fade932014-04-23 10:00:57 -0400421
422 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
423 balance_leaf_paste_left_shift_dirent(tb, ih, body);
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400424 return 0;
Jeff Mahoney3fade932014-04-23 10:00:57 -0400425 }
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) {
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400498 body_shift_bytes = l_n - tb->zeroes_num;
Jeff Mahoney3fade932014-04-23 10:00:57 -0400499 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 }
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400527 return body_shift_bytes;
Jeff Mahoney3fade932014-04-23 10:00:57 -0400528}
529
530
531/* appended item will be in L[0] in whole */
532static void balance_leaf_paste_left_whole(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400533 struct item_head * const ih,
534 const char * const body)
Jeff Mahoney3fade932014-04-23 10:00:57 -0400535{
536 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
537 int n = B_NR_ITEMS(tb->L[0]);
538 struct buffer_info bi;
539 struct item_head *pasted;
540 int ret;
541
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);
581
582 tb->insert_size[0] = 0;
583 tb->zeroes_num = 0;
584}
585
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400586static unsigned int balance_leaf_paste_left(struct tree_balance *tb,
587 struct item_head * const ih,
588 const char * const body)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Jeff Mahoney3fade932014-04-23 10:00:57 -0400590 /* we must shift the part of the appended item */
591 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1)
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400592 return balance_leaf_paste_left_shift(tb, ih, body);
Jeff Mahoney3fade932014-04-23 10:00:57 -0400593 else
594 balance_leaf_paste_left_whole(tb, ih, body);
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400595 return 0;
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400596}
597
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400598/* Shift lnum[0] items from S[0] to the left neighbor L[0] */
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400599static unsigned int balance_leaf_left(struct tree_balance *tb,
600 struct item_head * const ih,
601 const char * const body, int flag)
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400602{
603 if (tb->lnum[0] <= 0)
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400604 return 0;
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400605
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)
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400611 return balance_leaf_insert_left(tb, ih, body);
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400612 else /* M_PASTE */
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400613 return balance_leaf_paste_left(tb, ih, body);
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400614 } else
615 /* new item doesn't fall into L[0] */
616 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400617 return 0;
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400618}
619
620
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400621static void balance_leaf_insert_right(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400622 struct item_head * const ih,
623 const char * const body)
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400624{
625
626 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
627 int n = B_NR_ITEMS(tbS0);
628 struct buffer_info bi;
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400629
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400630 /* new item or part of it doesn't fall into R[0] */
631 if (n - tb->rnum[0] >= tb->item_pos) {
632 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
633 return;
634 }
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400635
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400636 /* new item or its part falls to R[0] */
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400637
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400638 /* part of new item falls into R[0] */
639 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) {
640 loff_t old_key_comp, old_len, r_zeroes_number;
641 const char *r_body;
zhengbinda5184c2019-09-25 16:47:16 -0700642 int shift;
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400643 loff_t offset;
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400644
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400645 leaf_shift_right(tb, tb->rnum[0] - 1, -1);
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400646
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400647 /* Remember key component and item length */
648 old_key_comp = le_ih_k_offset(ih);
649 old_len = ih_item_len(ih);
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400650
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400651 /*
652 * Calculate key component and item length to insert
653 * into R[0]
654 */
655 shift = 0;
656 if (is_indirect_le_ih(ih))
657 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
658 offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << shift);
659 set_le_ih_k_offset(ih, offset);
660 put_ih_item_len(ih, tb->rbytes);
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400661
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400662 /* Insert part of the item into R[0] */
663 buffer_info_init_right(tb, &bi);
664 if ((old_len - tb->rbytes) > tb->zeroes_num) {
665 r_zeroes_number = 0;
666 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
667 } else {
668 r_body = body;
669 r_zeroes_number = tb->zeroes_num -
670 (old_len - tb->rbytes);
671 tb->zeroes_num -= r_zeroes_number;
672 }
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400673
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400674 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400675
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400676 /* Replace right delimiting key by first key in R[0] */
677 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400678
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400679 /*
680 * Calculate key component and item length to
681 * insert into S[0]
682 */
683 set_le_ih_k_offset(ih, old_key_comp);
684 put_ih_item_len(ih, old_len - tb->rbytes);
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400685
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400686 tb->insert_size[0] -= tb->rbytes;
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400687
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400688 } else {
689 /* whole new item falls into R[0] */
690
691 /* Shift rnum[0]-1 items to R[0] */
Jason Yanb25bab12019-09-25 16:47:22 -0700692 leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
Jeff Mahoney8dbf0d82014-04-23 10:00:58 -0400693
694 /* Insert new item into R[0] */
695 buffer_info_init_right(tb, &bi);
696 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
697 ih, body, tb->zeroes_num);
698
699 if (tb->item_pos - n + tb->rnum[0] - 1 == 0)
700 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
701
702 tb->zeroes_num = tb->insert_size[0] = 0;
703 }
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400704}
705
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400706
707static void balance_leaf_paste_right_shift_dirent(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400708 struct item_head * const ih,
709 const char * const body)
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400710{
711 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
712 struct buffer_info bi;
713 int entry_count;
714
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400715 RFALSE(tb->zeroes_num,
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400716 "PAP-12145: invalid parameter in case of a directory");
717 entry_count = ih_entry_count(item_head(tbS0, tb->item_pos));
718
719 /* new directory entry falls into R[0] */
720 if (entry_count - tb->rbytes < tb->pos_in_item) {
721 int paste_entry_position;
722
723 RFALSE(tb->rbytes - 1 >= entry_count || !tb->insert_size[0],
724 "PAP-12150: no enough of entries to shift to R[0]: "
725 "rbytes=%d, entry_count=%d", tb->rbytes, entry_count);
726
727 /*
728 * Shift rnum[0]-1 items in whole.
729 * Shift rbytes-1 directory entries from directory
730 * item number rnum[0]
731 */
732 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
733
734 /* Paste given directory entry to directory item */
735 paste_entry_position = tb->pos_in_item - entry_count +
736 tb->rbytes - 1;
737 buffer_info_init_right(tb, &bi);
738 leaf_paste_in_buffer(&bi, 0, paste_entry_position,
739 tb->insert_size[0], body, tb->zeroes_num);
740
741 /* paste entry */
742 leaf_paste_entries(&bi, 0, paste_entry_position, 1,
743 (struct reiserfs_de_head *) body,
744 body + DEH_SIZE, tb->insert_size[0]);
745
746 /* change delimiting keys */
747 if (paste_entry_position == 0)
748 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
749
750 tb->insert_size[0] = 0;
751 tb->pos_in_item++;
752 } else {
753 /* new directory entry doesn't fall into R[0] */
754 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
755 }
756}
757
758static void balance_leaf_paste_right_shift(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400759 struct item_head * const ih,
760 const char * const body)
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400761{
762 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
763 int n_shift, n_rem, r_zeroes_number, version;
764 unsigned long temp_rem;
765 const char *r_body;
766 struct buffer_info bi;
767
768 /* we append to directory item */
769 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
770 balance_leaf_paste_right_shift_dirent(tb, ih, body);
771 return;
772 }
773
774 /* regular object */
775
776 /*
777 * Calculate number of bytes which must be shifted
778 * from appended item
779 */
780 n_shift = tb->rbytes - tb->insert_size[0];
781 if (n_shift < 0)
782 n_shift = 0;
783
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400784 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400785 "PAP-12155: invalid position to paste. ih_item_len=%d, "
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400786 "pos_in_item=%d", tb->pos_in_item,
787 ih_item_len(item_head(tbS0, tb->item_pos)));
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400788
789 leaf_shift_right(tb, tb->rnum[0], n_shift);
790
791 /*
792 * Calculate number of bytes which must remain in body
793 * after appending to R[0]
794 */
795 n_rem = tb->insert_size[0] - tb->rbytes;
796 if (n_rem < 0)
797 n_rem = 0;
798
799 temp_rem = n_rem;
800
801 version = ih_version(item_head(tb->R[0], 0));
802
803 if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
804 int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
805 temp_rem = n_rem << shift;
806 }
807
808 add_le_key_k_offset(version, leaf_key(tb->R[0], 0), temp_rem);
809 add_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
810 temp_rem);
811
812 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
813
814 /* Append part of body into R[0] */
815 buffer_info_init_right(tb, &bi);
816 if (n_rem > tb->zeroes_num) {
817 r_zeroes_number = 0;
818 r_body = body + n_rem - tb->zeroes_num;
819 } else {
820 r_body = body;
821 r_zeroes_number = tb->zeroes_num - n_rem;
822 tb->zeroes_num -= r_zeroes_number;
823 }
824
825 leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem,
826 r_body, r_zeroes_number);
827
828 if (is_indirect_le_ih(item_head(tb->R[0], 0)))
829 set_ih_free_space(item_head(tb->R[0], 0), 0);
830
831 tb->insert_size[0] = n_rem;
832 if (!n_rem)
833 tb->pos_in_item++;
834}
835
836static void balance_leaf_paste_right_whole(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400837 struct item_head * const ih,
838 const char * const body)
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400839{
840 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
841 int n = B_NR_ITEMS(tbS0);
842 struct item_head *pasted;
843 struct buffer_info bi;
844
Colin Ian King5404e7e2020-04-06 20:11:46 -0700845 buffer_info_init_right(tb, &bi);
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400846 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
847
848 /* append item in R[0] */
849 if (tb->pos_in_item >= 0) {
850 buffer_info_init_right(tb, &bi);
851 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0],
852 tb->pos_in_item, tb->insert_size[0], body,
853 tb->zeroes_num);
854 }
855
856 /* paste new entry, if item is directory item */
857 pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
858 if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
859 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
860 tb->pos_in_item, 1,
861 (struct reiserfs_de_head *)body,
862 body + DEH_SIZE, tb->insert_size[0]);
863
864 if (!tb->pos_in_item) {
865
866 RFALSE(tb->item_pos - n + tb->rnum[0],
867 "PAP-12165: directory item must be first "
868 "item of node when pasting is in 0th position");
869
870 /* update delimiting keys */
871 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
872 }
873 }
874
875 if (is_indirect_le_ih(pasted))
876 set_ih_free_space(pasted, 0);
877 tb->zeroes_num = tb->insert_size[0] = 0;
878}
879
Jeff Mahoney3f0eb272014-04-23 10:00:50 -0400880static void balance_leaf_paste_right(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400881 struct item_head * const ih,
882 const char * const body)
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400883{
884 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -0400885 int n = B_NR_ITEMS(tbS0);
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400886
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400887 /* new item doesn't fall into R[0] */
888 if (n - tb->rnum[0] > tb->item_pos) {
889 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
890 return;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400893 /* pasted item or part of it falls to R[0] */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700894
Jeff Mahoney2369ecd2014-04-23 10:00:59 -0400895 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1)
896 /* we must shift the part of the appended item */
897 balance_leaf_paste_right_shift(tb, ih, body);
898 else
899 /* pasted item in whole falls into R[0] */
900 balance_leaf_paste_right_whole(tb, ih, body);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -0400901}
902
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400903/* shift rnum[0] items from S[0] to the right neighbor R[0] */
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400904static void balance_leaf_right(struct tree_balance *tb,
905 struct item_head * const ih,
906 const char * const body, int flag)
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400907{
908 if (tb->rnum[0] <= 0)
909 return;
910
911 BUG_ON(flag != M_INSERT && flag != M_PASTE);
912
913 if (flag == M_INSERT)
914 balance_leaf_insert_right(tb, ih, body);
915 else /* M_PASTE */
916 balance_leaf_paste_right(tb, ih, body);
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400917}
918
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400919static void balance_leaf_new_nodes_insert(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -0400920 struct item_head * const ih,
921 const char * const body,
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400922 struct item_head *insert_key,
923 struct buffer_head **insert_ptr,
924 int i)
925{
926 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
927 int n = B_NR_ITEMS(tbS0);
928 struct buffer_info bi;
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400929 int shift;
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400930
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400931 /* new item or it part don't falls into S_new[i] */
932 if (n - tb->snum[i] >= tb->item_pos) {
933 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
934 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
935 return;
936 }
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400937
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400938 /* new item or it's part falls to first new node S_new[i] */
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400939
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400940 /* part of new item falls into S_new[i] */
941 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) {
942 int old_key_comp, old_len, r_zeroes_number;
943 const char *r_body;
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400944
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400945 /* Move snum[i]-1 items from S[0] to S_new[i] */
946 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i] - 1, -1,
947 tb->S_new[i]);
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400948
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400949 /* Remember key component and item length */
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400950 old_key_comp = le_ih_k_offset(ih);
951 old_len = ih_item_len(ih);
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400952
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400953 /*
954 * Calculate key component and item length to insert
955 * into S_new[i]
956 */
957 shift = 0;
958 if (is_indirect_le_ih(ih))
959 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
960 set_le_ih_k_offset(ih,
961 le_ih_k_offset(ih) +
962 ((old_len - tb->sbytes[i]) << shift));
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400963
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400964 put_ih_item_len(ih, tb->sbytes[i]);
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400965
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400966 /* Insert part of the item into S_new[i] before 0-th item */
967 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400968
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400969 if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
970 r_zeroes_number = 0;
971 r_body = body + (old_len - tb->sbytes[i]) -
972 tb->zeroes_num;
973 } else {
974 r_body = body;
975 r_zeroes_number = tb->zeroes_num - (old_len -
976 tb->sbytes[i]);
977 tb->zeroes_num -= r_zeroes_number;
978 }
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400979
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400980 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400981
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400982 /*
983 * Calculate key component and item length to
984 * insert into S[i]
985 */
986 set_le_ih_k_offset(ih, old_key_comp);
987 put_ih_item_len(ih, old_len - tb->sbytes[i]);
988 tb->insert_size[0] -= tb->sbytes[i];
989 } else {
990 /* whole new item falls into S_new[i] */
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400991
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -0400992 /*
993 * Shift snum[0] - 1 items to S_new[i]
994 * (sbytes[i] of split item)
995 */
996 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
997 tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
998
999 /* Insert new item into S_new[i] */
1000 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1001 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
1002 ih, body, tb->zeroes_num);
1003
1004 tb->zeroes_num = tb->insert_size[0] = 0;
1005 }
Jeff Mahoney65ab18c2014-04-23 10:00:51 -04001006}
1007
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001008/* we append to directory item */
1009static void balance_leaf_new_nodes_paste_dirent(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001010 struct item_head * const ih,
1011 const char * const body,
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001012 struct item_head *insert_key,
1013 struct buffer_head **insert_ptr,
1014 int i)
1015{
1016 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1017 struct item_head *aux_ih = item_head(tbS0, tb->item_pos);
1018 int entry_count = ih_entry_count(aux_ih);
1019 struct buffer_info bi;
1020
1021 if (entry_count - tb->sbytes[i] < tb->pos_in_item &&
1022 tb->pos_in_item <= entry_count) {
1023 /* new directory entry falls into S_new[i] */
1024
1025 RFALSE(!tb->insert_size[0],
1026 "PAP-12215: insert_size is already 0");
1027 RFALSE(tb->sbytes[i] - 1 >= entry_count,
1028 "PAP-12220: there are no so much entries (%d), only %d",
1029 tb->sbytes[i] - 1, entry_count);
1030
1031 /*
1032 * Shift snum[i]-1 items in whole.
1033 * Shift sbytes[i] directory entries
1034 * from directory item number snum[i]
1035 */
1036 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i],
1037 tb->sbytes[i] - 1, tb->S_new[i]);
1038
1039 /*
1040 * Paste given directory entry to
1041 * directory item
1042 */
1043 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1044 leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count +
1045 tb->sbytes[i] - 1, tb->insert_size[0],
1046 body, tb->zeroes_num);
1047
1048 /* paste new directory entry */
1049 leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count +
1050 tb->sbytes[i] - 1, 1,
1051 (struct reiserfs_de_head *) body,
1052 body + DEH_SIZE, tb->insert_size[0]);
1053
1054 tb->insert_size[0] = 0;
1055 tb->pos_in_item++;
1056 } else {
1057 /* new directory entry doesn't fall into S_new[i] */
1058 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i],
1059 tb->sbytes[i], tb->S_new[i]);
1060 }
1061
1062}
1063
1064static void balance_leaf_new_nodes_paste_shift(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001065 struct item_head * const ih,
1066 const char * const body,
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001067 struct item_head *insert_key,
1068 struct buffer_head **insert_ptr,
1069 int i)
1070{
1071 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1072 struct item_head *aux_ih = item_head(tbS0, tb->item_pos);
1073 int n_shift, n_rem, r_zeroes_number, shift;
1074 const char *r_body;
1075 struct item_head *tmp;
1076 struct buffer_info bi;
1077
1078 RFALSE(ih, "PAP-12210: ih must be 0");
1079
1080 if (is_direntry_le_ih(aux_ih)) {
1081 balance_leaf_new_nodes_paste_dirent(tb, ih, body, insert_key,
1082 insert_ptr, i);
1083 return;
1084 }
1085
1086 /* regular object */
1087
1088
1089 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) ||
1090 tb->insert_size[0] <= 0,
1091 "PAP-12225: item too short or insert_size <= 0");
1092
1093 /*
1094 * Calculate number of bytes which must be shifted from appended item
1095 */
1096 n_shift = tb->sbytes[i] - tb->insert_size[0];
1097 if (n_shift < 0)
1098 n_shift = 0;
1099 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift,
1100 tb->S_new[i]);
1101
1102 /*
1103 * Calculate number of bytes which must remain in body after
1104 * append to S_new[i]
1105 */
1106 n_rem = tb->insert_size[0] - tb->sbytes[i];
1107 if (n_rem < 0)
1108 n_rem = 0;
1109
1110 /* Append part of body into S_new[0] */
1111 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1112 if (n_rem > tb->zeroes_num) {
1113 r_zeroes_number = 0;
1114 r_body = body + n_rem - tb->zeroes_num;
1115 } else {
1116 r_body = body;
1117 r_zeroes_number = tb->zeroes_num - n_rem;
1118 tb->zeroes_num -= r_zeroes_number;
1119 }
1120
1121 leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem,
1122 r_body, r_zeroes_number);
1123
1124 tmp = item_head(tb->S_new[i], 0);
1125 shift = 0;
1126 if (is_indirect_le_ih(tmp)) {
1127 set_ih_free_space(tmp, 0);
1128 shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
1129 }
1130 add_le_ih_k_offset(tmp, n_rem << shift);
1131
1132 tb->insert_size[0] = n_rem;
1133 if (!n_rem)
1134 tb->pos_in_item++;
1135}
1136
1137static void balance_leaf_new_nodes_paste_whole(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001138 struct item_head * const ih,
1139 const char * const body,
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001140 struct item_head *insert_key,
1141 struct buffer_head **insert_ptr,
1142 int i)
1143
1144{
1145 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1146 int n = B_NR_ITEMS(tbS0);
1147 int leaf_mi;
1148 struct item_head *pasted;
1149 struct buffer_info bi;
1150
1151#ifdef CONFIG_REISERFS_CHECK
1152 struct item_head *ih_check = item_head(tbS0, tb->item_pos);
1153
1154 if (!is_direntry_le_ih(ih_check) &&
1155 (tb->pos_in_item != ih_item_len(ih_check) ||
1156 tb->insert_size[0] <= 0))
1157 reiserfs_panic(tb->tb_sb,
1158 "PAP-12235",
1159 "pos_in_item must be equal to ih_item_len");
1160#endif
1161
1162 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i],
1163 tb->sbytes[i], tb->S_new[i]);
1164
1165 RFALSE(leaf_mi,
1166 "PAP-12240: unexpected value returned by leaf_move_items (%d)",
1167 leaf_mi);
1168
1169 /* paste into item */
1170 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
1171 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->snum[i],
1172 tb->pos_in_item, tb->insert_size[0],
1173 body, tb->zeroes_num);
1174
1175 pasted = item_head(tb->S_new[i], tb->item_pos - n +
1176 tb->snum[i]);
1177 if (is_direntry_le_ih(pasted))
1178 leaf_paste_entries(&bi, tb->item_pos - n + tb->snum[i],
1179 tb->pos_in_item, 1,
1180 (struct reiserfs_de_head *)body,
1181 body + DEH_SIZE, tb->insert_size[0]);
1182
1183 /* if we paste to indirect item update ih_free_space */
1184 if (is_indirect_le_ih(pasted))
1185 set_ih_free_space(pasted, 0);
1186
1187 tb->zeroes_num = tb->insert_size[0] = 0;
1188
1189}
Jeff Mahoney9d496552014-04-23 10:00:52 -04001190static void balance_leaf_new_nodes_paste(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001191 struct item_head * const ih,
1192 const char * const body,
Jeff Mahoney9d496552014-04-23 10:00:52 -04001193 struct item_head *insert_key,
1194 struct buffer_head **insert_ptr,
1195 int i)
1196{
1197 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1198 int n = B_NR_ITEMS(tbS0);
Jeff Mahoney9d496552014-04-23 10:00:52 -04001199
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001200 /* pasted item doesn't fall into S_new[i] */
1201 if (n - tb->snum[i] > tb->item_pos) {
1202 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
1203 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
1204 return;
1205 }
Jeff Mahoney9d496552014-04-23 10:00:52 -04001206
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001207 /* pasted item or part if it falls to S_new[i] */
Jeff Mahoney9d496552014-04-23 10:00:52 -04001208
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001209 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1)
1210 /* we must shift part of the appended item */
1211 balance_leaf_new_nodes_paste_shift(tb, ih, body, insert_key,
1212 insert_ptr, i);
1213 else
1214 /* item falls wholly into S_new[i] */
1215 balance_leaf_new_nodes_paste_whole(tb, ih, body, insert_key,
1216 insert_ptr, i);
Jeff Mahoney9d496552014-04-23 10:00:52 -04001217}
1218
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001219/* Fill new nodes that appear in place of S[0] */
1220static void balance_leaf_new_nodes(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001221 struct item_head * const ih,
1222 const char * const body,
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001223 struct item_head *insert_key,
1224 struct buffer_head **insert_ptr,
1225 int flag)
1226{
1227 int i;
1228 for (i = tb->blknum[0] - 2; i >= 0; i--) {
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001229 BUG_ON(flag != M_INSERT && flag != M_PASTE);
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001230
1231 RFALSE(!tb->snum[i],
1232 "PAP-12200: snum[%d] == %d. Must be > 0", i,
1233 tb->snum[i]);
1234
1235 /* here we shift from S to S_new nodes */
1236
1237 tb->S_new[i] = get_FEB(tb);
1238
1239 /* initialized block type and tree level */
1240 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
1241
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001242 if (flag == M_INSERT)
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001243 balance_leaf_new_nodes_insert(tb, ih, body, insert_key,
1244 insert_ptr, i);
Jeff Mahoneyb54b8c92014-04-23 10:01:00 -04001245 else /* M_PASTE */
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001246 balance_leaf_new_nodes_paste(tb, ih, body, insert_key,
1247 insert_ptr, i);
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001248
1249 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1250 insert_ptr[i] = tb->S_new[i];
1251
1252 RFALSE(!buffer_journaled(tb->S_new[i])
1253 || buffer_journal_dirty(tb->S_new[i])
1254 || buffer_dirty(tb->S_new[i]),
1255 "PAP-12247: S_new[%d] : (%b)",
Jan Kara19ef1222014-06-11 17:29:26 +02001256 i, tb->S_new[i]);
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001257 }
1258}
1259
Jeff Mahoney8c480ea2014-04-23 10:00:53 -04001260static void balance_leaf_finish_node_insert(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001261 struct item_head * const ih,
1262 const char * const body)
Jeff Mahoney8c480ea2014-04-23 10:00:53 -04001263{
1264 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1265 struct buffer_info bi;
Jeff Mahoney441378c2014-04-23 10:01:01 -04001266 buffer_info_init_tbS0(tb, &bi);
1267 leaf_insert_into_buf(&bi, tb->item_pos, ih, body, tb->zeroes_num);
Jeff Mahoney8c480ea2014-04-23 10:00:53 -04001268
Jeff Mahoney441378c2014-04-23 10:01:01 -04001269 /* If we insert the first key change the delimiting key */
1270 if (tb->item_pos == 0) {
1271 if (tb->CFL[0]) /* can be 0 in reiserfsck */
1272 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1273
1274 }
1275}
1276
1277static void balance_leaf_finish_node_paste_dirent(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001278 struct item_head * const ih,
1279 const char * const body)
Jeff Mahoney441378c2014-04-23 10:01:01 -04001280{
1281 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1282 struct item_head *pasted = item_head(tbS0, tb->item_pos);
1283 struct buffer_info bi;
1284
1285 if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
1286 RFALSE(!tb->insert_size[0],
1287 "PAP-12260: insert_size is 0 already");
1288
1289 /* prepare space */
1290 buffer_info_init_tbS0(tb, &bi);
1291 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1292 tb->insert_size[0], body, tb->zeroes_num);
1293
1294 /* paste entry */
1295 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
1296 (struct reiserfs_de_head *)body,
1297 body + DEH_SIZE, tb->insert_size[0]);
1298
1299 if (!tb->item_pos && !tb->pos_in_item) {
1300 RFALSE(!tb->CFL[0] || !tb->L[0],
1301 "PAP-12270: CFL[0]/L[0] must be specified");
1302 if (tb->CFL[0])
1303 replace_key(tb, tb->CFL[0], tb->lkey[0],
1304 tbS0, 0);
1305 }
1306
1307 tb->insert_size[0] = 0;
1308 }
Jeff Mahoney8c480ea2014-04-23 10:00:53 -04001309}
1310
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001311static void balance_leaf_finish_node_paste(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001312 struct item_head * const ih,
1313 const char * const body)
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001314{
1315 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1316 struct buffer_info bi;
Jeff Mahoney441378c2014-04-23 10:01:01 -04001317 struct item_head *pasted = item_head(tbS0, tb->item_pos);
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001318
Jeff Mahoney441378c2014-04-23 10:01:01 -04001319 /* when directory, may be new entry already pasted */
1320 if (is_direntry_le_ih(pasted)) {
1321 balance_leaf_finish_node_paste_dirent(tb, ih, body);
1322 return;
1323 }
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001324
Jeff Mahoney441378c2014-04-23 10:01:01 -04001325 /* regular object */
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001326
Jeff Mahoney441378c2014-04-23 10:01:01 -04001327 if (tb->pos_in_item == ih_item_len(pasted)) {
1328 RFALSE(tb->insert_size[0] <= 0,
1329 "PAP-12275: insert size must not be %d",
1330 tb->insert_size[0]);
1331 buffer_info_init_tbS0(tb, &bi);
1332 leaf_paste_in_buffer(&bi, tb->item_pos,
1333 tb->pos_in_item, tb->insert_size[0], body,
1334 tb->zeroes_num);
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001335
Jeff Mahoney441378c2014-04-23 10:01:01 -04001336 if (is_indirect_le_ih(pasted))
1337 set_ih_free_space(pasted, 0);
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001338
Jeff Mahoney441378c2014-04-23 10:01:01 -04001339 tb->insert_size[0] = 0;
1340 }
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001341#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney441378c2014-04-23 10:01:01 -04001342 else if (tb->insert_size[0]) {
1343 print_cur_tb("12285");
1344 reiserfs_panic(tb->tb_sb, "PAP-12285",
1345 "insert_size must be 0 (%d)", tb->insert_size[0]);
1346 }
1347#endif
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001348}
1349
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001350/*
1351 * if the affected item was not wholly shifted then we
1352 * perform all necessary operations on that part or whole
1353 * of the affected item which remains in S
1354 */
1355static void balance_leaf_finish_node(struct tree_balance *tb,
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001356 struct item_head * const ih,
1357 const char * const body, int flag)
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001358{
1359 /* if we must insert or append into buffer S[0] */
1360 if (0 <= tb->item_pos && tb->item_pos < tb->s0num) {
1361 if (flag == M_INSERT)
1362 balance_leaf_finish_node_insert(tb, ih, body);
1363 else /* M_PASTE */
1364 balance_leaf_finish_node_paste(tb, ih, body);
1365 }
1366}
1367
Jeff Mahoney3f0eb272014-04-23 10:00:50 -04001368/**
1369 * balance_leaf - reiserfs tree balancing algorithm
1370 * @tb: tree balance state
1371 * @ih: item header of inserted item (little endian)
1372 * @body: body of inserted item or bytes to paste
1373 * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance)
1374 * passed back:
1375 * @insert_key: key to insert new nodes
1376 * @insert_ptr: array of nodes to insert at the next level
1377 *
1378 * In our processing of one level we sometimes determine what must be
1379 * inserted into the next higher level. This insertion consists of a
1380 * key or two keys and their corresponding pointers.
1381 */
1382static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1383 const char *body, int flag,
1384 struct item_head *insert_key,
1385 struct buffer_head **insert_ptr)
1386{
1387 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -04001388
1389 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
1390
1391 /* Make balance in case insert_size[0] < 0 */
1392 if (tb->insert_size[0] < 0)
1393 return balance_leaf_when_delete(tb, flag);
1394
1395 tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
1396 tb->pos_in_item = tb->tb_path->pos_in_item,
1397 tb->zeroes_num = 0;
1398 if (flag == M_INSERT && !body)
1399 tb->zeroes_num = ih_item_len(ih);
1400
1401 /*
1402 * for indirect item pos_in_item is measured in unformatted node
1403 * pointers. Recalculate to bytes
1404 */
1405 if (flag != M_INSERT
1406 && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
1407 tb->pos_in_item *= UNFM_P_SIZE;
1408
Jeff Mahoney27d0e5b2014-08-04 19:51:47 -04001409 body += balance_leaf_left(tb, ih, body, flag);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -04001410
1411 /* tb->lnum[0] > 0 */
1412 /* Calculate new item position */
1413 tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
1414
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001415 balance_leaf_right(tb, ih, body, flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001416
1417 /* tb->rnum[0] > 0 */
1418 RFALSE(tb->blknum[0] > 3,
Dave Jones416e2ab2014-02-17 16:21:24 -05001419 "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001420 RFALSE(tb->blknum[0] < 0,
Dave Jones416e2ab2014-02-17 16:21:24 -05001421 "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001422
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001423 /*
1424 * if while adding to a node we discover that it is possible to split
1425 * it in two, and merge the left part into the left neighbor and the
1426 * right part into the right neighbor, eliminating the node
1427 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 if (tb->blknum[0] == 0) { /* node S[0] is empty now */
1429
1430 RFALSE(!tb->lnum[0] || !tb->rnum[0],
1431 "PAP-12190: lnum and rnum must not be zero");
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001432 /*
1433 * if insertion was done before 0-th position in R[0], right
1434 * delimiting key of the tb->L[0]'s and left delimiting key are
1435 * not set correctly
1436 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001437 if (tb->CFL[0]) {
1438 if (!tb->CFR[0])
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001439 reiserfs_panic(tb->tb_sb, "vs-12195",
1440 "CFR not initialized");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001441 copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
1442 internal_key(tb->CFR[0], tb->rkey[0]));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001443 do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
1444 }
1445
1446 reiserfs_invalidate_buffer(tb, tbS0);
1447 return 0;
1448 }
1449
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001450 balance_leaf_new_nodes(tb, ih, body, insert_key, insert_ptr, flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001451
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001452 balance_leaf_finish_node(tb, ih, body, flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001453
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001454#ifdef CONFIG_REISERFS_CHECK
1455 if (flag == M_PASTE && tb->insert_size[0]) {
1456 print_cur_tb("12290");
1457 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001458 "PAP-12290", "insert_size is still not 0 (%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001459 tb->insert_size[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001461#endif
1462
1463 /* Leaf level of the tree is balanced (end of balance_leaf) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467/* Make empty node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001468void make_empty_node(struct buffer_info *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001470 struct block_head *blkh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001472 RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001474 blkh = B_BLK_HEAD(bi->bi_bh);
1475 set_blkh_nr_item(blkh, 0);
1476 set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001478 if (bi->bi_parent)
1479 B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482/* Get first empty buffer */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001483struct buffer_head *get_FEB(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001485 int i;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001486 struct buffer_info bi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001488 for (i = 0; i < MAX_FEB_SIZE; i++)
Al Viro9dce07f2008-03-29 03:07:28 +00001489 if (tb->FEB[i] != NULL)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001492 if (i == MAX_FEB_SIZE)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001493 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001495 buffer_info_init_bh(tb, &bi, tb->FEB[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001496 make_empty_node(&bi);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001497 set_buffer_uptodate(tb->FEB[i]);
1498 tb->used[i] = tb->FEB[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001499 tb->FEB[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001501 return tb->used[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Jeff Mahoney098297b2014-04-23 10:00:36 -04001504/* This is now used because reiserfs_free_block has to be able to schedule. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001505static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001507 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001509 if (buffer_dirty(bh))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001510 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1511 "called with dirty buffer");
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001512 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001513 if (!tb->thrown[i]) {
1514 tb->thrown[i] = bh;
1515 get_bh(bh); /* free_thrown puts this */
1516 return;
1517 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001518 reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1519 "too many thrown buffers");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001522static void free_thrown(struct tree_balance *tb)
1523{
1524 int i;
1525 b_blocknr_t blocknr;
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001526 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001527 if (tb->thrown[i]) {
1528 blocknr = tb->thrown[i]->b_blocknr;
1529 if (buffer_dirty(tb->thrown[i]))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001530 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1531 "called with dirty buffer %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001532 blocknr);
1533 brelse(tb->thrown[i]); /* incremented in store_thrown */
1534 reiserfs_free_block(tb->transaction_handle, NULL,
1535 blocknr, 0);
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001540void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001542 struct block_head *blkh;
1543 blkh = B_BLK_HEAD(bh);
1544 set_blkh_level(blkh, FREE_LEVEL);
1545 set_blkh_nr_item(blkh, 0);
1546
1547 clear_buffer_dirty(bh);
1548 store_thrown(tb, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551/* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001552void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1553 struct buffer_head *src, int n_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001556 RFALSE(dest == NULL || src == NULL,
1557 "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
1558 src, dest);
1559 RFALSE(!B_IS_KEYS_LEVEL(dest),
1560 "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
1561 dest);
1562 RFALSE(n_dest < 0 || n_src < 0,
1563 "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
1564 RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
1565 "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
1566 n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001568 if (B_IS_ITEMS_LEVEL(src))
1569 /* source buffer contains leaf node */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001570 memcpy(internal_key(dest, n_dest), item_head(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001571 KEY_SIZE);
1572 else
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001573 memcpy(internal_key(dest, n_dest), internal_key(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001574 KEY_SIZE);
1575
1576 do_balance_mark_internal_dirty(tb, dest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001579int get_left_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001581 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Al Viro9dce07f2008-03-29 03:07:28 +00001583 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001584 "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
1585 h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001587 if (Sh_position == 0)
1588 return B_NR_ITEMS(tb->FL[h]);
1589 else
1590 return Sh_position - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001593int get_right_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001595 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Al Viro9dce07f2008-03-29 03:07:28 +00001597 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001598 "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
1599 h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001601 if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1602 return 0;
1603 else
1604 return Sh_position + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607#ifdef CONFIG_REISERFS_CHECK
1608
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001609int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
1610static void check_internal_node(struct super_block *s, struct buffer_head *bh,
1611 char *mes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001613 struct disk_child *dc;
1614 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001616 RFALSE(!bh, "PAP-12336: bh == 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001618 if (!bh || !B_IS_IN_TREE(bh))
1619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001621 RFALSE(!buffer_dirty(bh) &&
1622 !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
1623 "PAP-12337: buffer (%b) must be dirty", bh);
1624 dc = B_N_CHILD(bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001626 for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
1627 if (!is_reusable(s, dc_block_number(dc), 1)) {
1628 print_cur_tb(mes);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001629 reiserfs_panic(s, "PAP-12338",
1630 "invalid child pointer %y in %b",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001631 dc, bh);
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001634}
1635
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001636static int locked_or_not_in_tree(struct tree_balance *tb,
1637 struct buffer_head *bh, char *which)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001638{
1639 if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
1640 !B_IS_IN_TREE(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001641 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001642 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001647static int check_before_balancing(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001649 int retval = 0;
1650
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001651 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001652 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1653 "occurred based on cur_tb not being null at "
1654 "this point in code. do_balance cannot properly "
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001655 "handle concurrent tree accesses on a same "
1656 "mount point.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001657 }
1658
Jeff Mahoney098297b2014-04-23 10:00:36 -04001659 /*
1660 * double check that buffers that we will modify are unlocked.
1661 * (fix_nodes should already have prepped all of these for us).
1662 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001663 if (tb->lnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001664 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1665 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1666 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001667 check_leaf(tb->L[0]);
1668 }
1669 if (tb->rnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001670 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1671 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1672 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001673 check_leaf(tb->R[0]);
1674 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001675 retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1676 "S[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001677 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1678
1679 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001682static void check_after_balance_leaf(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001684 if (tb->lnum[0]) {
1685 if (B_FREE_SPACE(tb->L[0]) !=
1686 MAX_CHILD_SIZE(tb->L[0]) -
1687 dc_size(B_N_CHILD
1688 (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1689 print_cur_tb("12221");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001690 reiserfs_panic(tb->tb_sb, "PAP-12355",
1691 "shift to left was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001692 }
1693 }
1694 if (tb->rnum[0]) {
1695 if (B_FREE_SPACE(tb->R[0]) !=
1696 MAX_CHILD_SIZE(tb->R[0]) -
1697 dc_size(B_N_CHILD
1698 (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1699 print_cur_tb("12222");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001700 reiserfs_panic(tb->tb_sb, "PAP-12360",
1701 "shift to right was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001702 }
1703 }
1704 if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1705 (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1706 (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1707 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1708 PATH_H_POSITION(tb->tb_path, 1)))))) {
1709 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1710 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1711 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1712 PATH_H_POSITION(tb->tb_path,
1713 1))));
1714 print_cur_tb("12223");
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001715 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001716 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1717 "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
1718 left,
1719 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1720 PATH_H_PBUFFER(tb->tb_path, 1),
1721 PATH_H_POSITION(tb->tb_path, 1),
1722 dc_size(B_N_CHILD
1723 (PATH_H_PBUFFER(tb->tb_path, 1),
1724 PATH_H_POSITION(tb->tb_path, 1))),
1725 right);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001726 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001727 }
1728}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001730static void check_leaf_level(struct tree_balance *tb)
1731{
1732 check_leaf(tb->L[0]);
1733 check_leaf(tb->R[0]);
1734 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1735}
1736
1737static void check_internal_levels(struct tree_balance *tb)
1738{
1739 int h;
1740
1741 /* check all internal nodes */
1742 for (h = 1; tb->insert_size[h]; h++) {
1743 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1744 "BAD BUFFER ON PATH");
1745 if (tb->lnum[h])
1746 check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1747 if (tb->rnum[h])
1748 check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751}
1752
1753#endif
1754
Jeff Mahoney098297b2014-04-23 10:00:36 -04001755/*
1756 * Now we have all of the buffers that must be used in balancing of
1757 * the tree. We rely on the assumption that schedule() will not occur
1758 * while do_balance works. ( Only interrupt handlers are acceptable.)
1759 * We balance the tree according to the analysis made before this,
1760 * using buffers already obtained. For SMP support it will someday be
1761 * necessary to add ordered locking of tb.
1762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Jeff Mahoney098297b2014-04-23 10:00:36 -04001764/*
1765 * Some interesting rules of balancing:
1766 * we delete a maximum of two nodes per level per balancing: we never
1767 * delete R, when we delete two of three nodes L, S, R then we move
1768 * them into R.
1769 *
1770 * we only delete L if we are deleting two nodes, if we delete only
1771 * one node we delete S
1772 *
1773 * if we shift leaves then we shift as much as we can: this is a
1774 * deliberate policy of extremism in node packing which results in
1775 * higher average utilization after repeated random balance operations
1776 * at the cost of more memory copies and more balancing as a result of
1777 * small insertions to full nodes.
1778 *
1779 * if we shift internal nodes we try to evenly balance the node
1780 * utilization, with consequent less balancing at the cost of lower
1781 * utilization.
1782 *
1783 * one could argue that the policy for directories in leaves should be
1784 * that of internal nodes, but we will wait until another day to
1785 * evaluate this.... It would be nice to someday measure and prove
1786 * these assumptions as to what is optimal....
1787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001789static inline void do_balance_starts(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Jeff Mahoney098297b2014-04-23 10:00:36 -04001791 /* use print_cur_tb() to see initial state of struct tree_balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001793 /* store_print_tb (tb); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001795 /* do not delete, just comment it out */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001796 /*
1797 print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1798 tb->tb_path->pos_in_item, tb, "check");
1799 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001800 RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801#ifdef CONFIG_REISERFS_CHECK
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001802 REISERFS_SB(tb->tb_sb)->cur_tb = tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803#endif
1804}
1805
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001806static inline void do_balance_completed(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001810 check_leaf_level(tb);
1811 check_internal_levels(tb);
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001812 REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813#endif
1814
Jeff Mahoney098297b2014-04-23 10:00:36 -04001815 /*
1816 * reiserfs_free_block is no longer schedule safe. So, we need to
1817 * put the buffers we want freed on the thrown list during do_balance,
1818 * and then free them now
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001821 REISERFS_SB(tb->tb_sb)->s_do_balance++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001823 /* release all nodes hold to perform the balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 unfix_nodes(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001826 free_thrown(tb);
1827}
1828
Jeff Mahoney098297b2014-04-23 10:00:36 -04001829/*
1830 * do_balance - balance the tree
1831 *
1832 * @tb: tree_balance structure
1833 * @ih: item header of inserted item
1834 * @body: body of inserted item or bytes to paste
1835 * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste
1836 *
1837 * Cut means delete part of an item (includes removing an entry from a
1838 * directory).
1839 *
1840 * Delete means delete whole item.
1841 *
1842 * Insert means add a new item into the tree.
1843 *
1844 * Paste means to append to the end of an existing file or to
1845 * insert a directory entry.
1846 */
1847void do_balance(struct tree_balance *tb, struct item_head *ih,
1848 const char *body, int flag)
1849{
1850 int child_pos; /* position of a child node in its parent */
1851 int h; /* level of the tree being processed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001852
Jeff Mahoney098297b2014-04-23 10:00:36 -04001853 /*
1854 * in our processing of one level we sometimes determine what
1855 * must be inserted into the next higher level. This insertion
1856 * consists of a key or two keys and their corresponding
1857 * pointers
1858 */
1859 struct item_head insert_key[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001860
Jeff Mahoney098297b2014-04-23 10:00:36 -04001861 /* inserted node-ptrs for the next level */
1862 struct buffer_head *insert_ptr[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001863
1864 tb->tb_mode = flag;
1865 tb->need_balance_dirty = 0;
1866
1867 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001868 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1869 "changed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001870 }
1871 /* if we have no real work to do */
1872 if (!tb->insert_size[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001873 reiserfs_warning(tb->tb_sb, "PAP-12350",
1874 "insert_size == 0, mode == %c", flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001875 unfix_nodes(tb);
1876 return;
1877 }
1878
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001879 atomic_inc(&fs_generation(tb->tb_sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001880 do_balance_starts(tb);
1881
Jeff Mahoney098297b2014-04-23 10:00:36 -04001882 /*
1883 * balance_leaf returns 0 except if combining L R and S into
1884 * one node. see balance_internal() for explanation of this
1885 * line of code.
1886 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001887 child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1888 balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001891 check_after_balance_leaf(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#endif
1893
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001894 /* Balance internal level of the tree. */
1895 for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
Jeff Mahoney83a3a562014-04-23 10:01:02 -04001896 child_pos = balance_internal(tb, h, child_pos, insert_key,
1897 insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001899 do_balance_completed(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}