blob: b7085b7e7524737eccd2c79ed9d7b32c216d1108 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/uaccess.h>
14#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.
75 *
76 * It would be easier to read balance_leaf() if each of these summary
77 * lines was a separate procedure rather than being inlined. I think
78 * that there are many passages here and in balance_leaf_when_delete() in
79 * which two calls to one procedure can replace two passages, and it
80 * might save cache space and improve software maintenance costs to do so.
81 *
82 * Vladimir made the perceptive comment that we should offload most of
83 * the decision making in this function into fix_nodes/check_balance, and
84 * then create some sort of structure in tb that says what actions should
85 * be performed by do_balance.
86 *
87 * -Hans
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Jeff Mahoney098297b2014-04-23 10:00:36 -040090/*
91 * Balance leaf node in case of delete or cut: insert_size[0] < 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
93 * lnum, rnum can have values >= -1
94 * -1 means that the neighbor must be joined with S
95 * 0 means that nothing should be done with the neighbor
Jeff Mahoney098297b2014-04-23 10:00:36 -040096 * >0 means to shift entirely or partly the specified number of items
97 * to the neighbor
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700101 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
102 int item_pos = PATH_LAST_POSITION(tb->tb_path);
103 int pos_in_item = tb->tb_path->pos_in_item;
104 struct buffer_info bi;
105 int n;
106 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
109 "vs- 12000: level: wrong FR %z", tb->FR[0]);
110 RFALSE(tb->blknum[0] > 1,
111 "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
112 RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
113 "PAP-12010: tree can not be empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400115 ih = item_head(tbS0, item_pos);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400116 buffer_info_init_tbS0(tb, &bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118 /* Delete or truncate the item */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120 switch (flag) {
121 case M_DELETE: /* delete item in S[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
124 "vs-12013: mode Delete, insert size %d, ih to be deleted %h",
125 -tb->insert_size[0], ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 leaf_delete_items(&bi, 0, item_pos, 1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700129 if (!item_pos && tb->CFL[0]) {
130 if (B_NR_ITEMS(tbS0)) {
131 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0,
132 0);
133 } else {
134 if (!PATH_H_POSITION(tb->tb_path, 1))
135 replace_key(tb, tb->CFL[0], tb->lkey[0],
136 PATH_H_PPARENT(tb->tb_path,
137 0), 0);
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700141 RFALSE(!item_pos && !tb->CFL[0],
142 "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
143 tb->L[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 case M_CUT:{ /* cut item in S[0] */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148 if (is_direntry_le_ih(ih)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jeff Mahoney098297b2014-04-23 10:00:36 -0400150 /*
151 * UFS unlink semantics are such that you
152 * can only delete one directory entry at
153 * a time.
154 */
155
156 /*
157 * when we cut a directory tb->insert_size[0]
158 * means number of entries to be cut (always 1)
159 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700160 tb->insert_size[0] = -1;
161 leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
162 -tb->insert_size[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
165 "PAP-12030: can not change delimiting key. CFL[0]=%p",
166 tb->CFL[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 if (!item_pos && !pos_in_item && tb->CFL[0]) {
169 replace_key(tb, tb->CFL[0], tb->lkey[0],
170 tbS0, 0);
171 }
172 } else {
173 leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
174 -tb->insert_size[0]);
175
176 RFALSE(!ih_item_len(ih),
177 "PAP-12035: cut must leave non-zero dynamic length of item");
178 }
179 break;
180 }
181
182 default:
183 print_cur_tb("12040");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400184 reiserfs_panic(tb->tb_sb, "PAP-12040",
185 "unexpected mode: %s(%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 (flag ==
187 M_PASTE) ? "PASTE" : ((flag ==
188 M_INSERT) ? "INSERT" :
189 "UNKNOWN"), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Jeff Mahoney098297b2014-04-23 10:00:36 -0400192 /*
193 * the rule is that no shifting occurs unless by shifting
194 * a node can be freed
195 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 n = B_NR_ITEMS(tbS0);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400197 /* L[0] takes part in balancing */
198 if (tb->lnum[0]) {
199 /* L[0] must be joined with S[0] */
200 if (tb->lnum[0] == -1) {
201 /* R[0] must be also joined with S[0] */
202 if (tb->rnum[0] == -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400204 /*
205 * all contents of all the 3 buffers
206 * will be in L[0]
207 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 if (PATH_H_POSITION(tb->tb_path, 1) == 0
209 && 1 < B_NR_ITEMS(tb->FR[0]))
210 replace_key(tb, tb->CFL[0],
211 tb->lkey[0],
212 tb->FR[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 leaf_move_items(LEAF_FROM_S_TO_L, tb, n,
215 -1, NULL);
216 leaf_move_items(LEAF_FROM_R_TO_L, tb,
217 B_NR_ITEMS(tb->R[0]),
218 -1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 reiserfs_invalidate_buffer(tb, tbS0);
221 reiserfs_invalidate_buffer(tb,
222 tb->R[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 return 0;
225 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400226 /*
227 * all contents of all the 3 buffers will
228 * be in R[0]
229 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230 leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1,
231 NULL);
232 leaf_move_items(LEAF_FROM_L_TO_R, tb,
233 B_NR_ITEMS(tb->L[0]), -1, NULL);
234
235 /* right_delimiting_key is correct in R[0] */
236 replace_key(tb, tb->CFR[0], tb->rkey[0],
237 tb->R[0], 0);
238
239 reiserfs_invalidate_buffer(tb, tbS0);
240 reiserfs_invalidate_buffer(tb, tb->L[0]);
241
242 return -1;
243 }
244
245 RFALSE(tb->rnum[0] != 0,
246 "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
247 /* all contents of L[0] and S[0] will be in L[0] */
248 leaf_shift_left(tb, n, -1);
249
250 reiserfs_invalidate_buffer(tb, tbS0);
251
252 return 0;
253 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400254
255 /*
256 * a part of contents of S[0] will be in L[0] and the
257 * rest part of S[0] will be in R[0]
258 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700259
260 RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
261 (tb->lnum[0] + tb->rnum[0] > n + 1),
262 "PAP-12050: rnum(%d) and lnum(%d) and item number(%d) in S[0] are not consistent",
263 tb->rnum[0], tb->lnum[0], n);
264 RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
265 (tb->lbytes != -1 || tb->rbytes != -1),
266 "PAP-12055: bad rbytes (%d)/lbytes (%d) parameters when items are not split",
267 tb->rbytes, tb->lbytes);
268 RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
269 (tb->lbytes < 1 || tb->rbytes != -1),
270 "PAP-12060: bad rbytes (%d)/lbytes (%d) parameters when items are split",
271 tb->rbytes, tb->lbytes);
272
273 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
274 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
275
276 reiserfs_invalidate_buffer(tb, tbS0);
277
278 return 0;
279 }
280
281 if (tb->rnum[0] == -1) {
282 /* all contents of R[0] and S[0] will be in R[0] */
283 leaf_shift_right(tb, n, -1);
284 reiserfs_invalidate_buffer(tb, tbS0);
285 return 0;
286 }
287
288 RFALSE(tb->rnum[0],
289 "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Jeff Mahoneyf1f007c2014-04-23 10:00:47 -0400293static void balance_leaf_insert_left(struct tree_balance *tb,
294 struct item_head *ih, const char *body)
295{
296 int ret_val;
297 struct buffer_info bi;
298 int n = B_NR_ITEMS(tb->L[0]);
299
300 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
301 /* part of new item falls into L[0] */
302 int new_item_len;
303 int version;
304
305 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
306
307 /* Calculate item length to insert to S[0] */
308 new_item_len = ih_item_len(ih) - tb->lbytes;
309 /* Calculate and check item length to insert to L[0] */
310 put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
311
312 RFALSE(ih_item_len(ih) <= 0,
313 "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
314 ih_item_len(ih));
315
316 /* Insert new item into L[0] */
317 buffer_info_init_left(tb, &bi);
318 leaf_insert_into_buf(&bi,
319 n + tb->item_pos - ret_val, ih, body,
320 tb->zeroes_num > ih_item_len(ih) ? ih_item_len(ih) : tb->zeroes_num);
321
322 version = ih_version(ih);
323
324 /* Calculate key component, item length and body to insert into S[0] */
325 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
326 (tb->lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
327
328 put_ih_item_len(ih, new_item_len);
329 if (tb->lbytes > tb->zeroes_num) {
330 body += (tb->lbytes - tb->zeroes_num);
331 tb->zeroes_num = 0;
332 } else
333 tb->zeroes_num -= tb->lbytes;
334
335 RFALSE(ih_item_len(ih) <= 0,
336 "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
337 ih_item_len(ih));
338 } else {
339 /* new item in whole falls into L[0] */
340 /* Shift lnum[0]-1 items to L[0] */
341 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
342 /* Insert new item into L[0] */
343 buffer_info_init_left(tb, &bi);
344 leaf_insert_into_buf(&bi, n + tb->item_pos - ret_val, ih, body, tb->zeroes_num);
345 tb->insert_size[0] = 0;
346 tb->zeroes_num = 0;
347 }
348
349}
350
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400351static void balance_leaf_paste_left(struct tree_balance *tb,
352 struct item_head *ih, const char *body)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355 int ret_val;
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400356 struct buffer_info bi;
357 int n = B_NR_ITEMS(tb->L[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400359 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700360 /* we must shift the part of the appended item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400361 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400363 RFALSE(tb->zeroes_num,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 "PAP-12090: invalid parameter in case of a directory");
365 /* directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400366 if (tb->lbytes > tb->pos_in_item) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700367 /* new directory entry falls into L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500368 struct item_head *pasted;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400369 int l_pos_in_item = tb->pos_in_item;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700370
371 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500372 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400373 if (ret_val && !tb->item_pos) {
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400374 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
375 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700376 }
377
378 /* Append given directory entry to directory item */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400379 buffer_info_init_left(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400380 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381
382 /* previous string prepared space for pasting new entry, following string pastes this entry */
383
384 /* when we have merge directory item, pos_in_item has been changed too */
385
386 /* paste new directory entry. 1 is entry number */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400387 leaf_paste_entries(&bi, n + tb->item_pos - ret_val, l_pos_in_item,
Dave Jones416e2ab2014-02-17 16:21:24 -0500388 1, (struct reiserfs_de_head *) body,
389 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 tb->insert_size[0] = 0;
391 } else {
392 /* new directory item doesn't fall into L[0] */
393 /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500394 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700395 }
396 /* Calculate new position to append in item body */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400397 tb->pos_in_item -= tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398 } else {
399 /* regular object */
Dave Jones416e2ab2014-02-17 16:21:24 -0500400 RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400401 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700402 "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400403 ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700404
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400405 if (tb->lbytes >= tb->pos_in_item) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406 /* appended item will be in L[0] in whole */
407 int l_n;
408
409 /* this bytes number must be appended to the last item of L[h] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400410 l_n = tb->lbytes - tb->pos_in_item;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700411
412 /* Calculate new insert_size[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500413 tb->insert_size[0] -= l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700414
Dave Jones416e2ab2014-02-17 16:21:24 -0500415 RFALSE(tb->insert_size[0] <= 0,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700416 "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500417 tb->insert_size[0]);
418 ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400419 (item_head(tbS0, tb->item_pos)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700420 /* Append to body of item in L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400421 buffer_info_init_left(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700422 leaf_paste_in_buffer
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400423 (&bi, n + tb->item_pos - ret_val, ih_item_len
424 (item_head(tb->L[0], n + tb->item_pos - ret_val)),
Dave Jones416e2ab2014-02-17 16:21:24 -0500425 l_n, body,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400426 tb->zeroes_num > l_n ? l_n : tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700427 /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
428 {
429 int version;
Dave Jones416e2ab2014-02-17 16:21:24 -0500430 int temp_l = l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700431
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400432 RFALSE(ih_item_len(item_head(tbS0, 0)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433 "PAP-12106: item length must be 0");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400434 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400435 (tb->L[0], n + tb->item_pos - ret_val)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700436 "PAP-12107: items must be of the same file");
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400437 if (is_indirect_le_ih(item_head(tb->L[0], n + tb->item_pos - ret_val))) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500438 temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700439 }
440 /* update key of first item in S0 */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400441 version = ih_version(item_head(tbS0, 0));
442 set_le_key_k_offset(version, leaf_key(tbS0, 0),
443 le_key_k_offset(version,leaf_key(tbS0, 0)) + temp_l);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 /* update left delimiting key */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400445 set_le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0]),
446 le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0])) + temp_l);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 }
448
449 /* Calculate new body, position in item and insert_size[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400450 if (l_n > tb->zeroes_num) {
451 body += (l_n - tb->zeroes_num);
452 tb->zeroes_num = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453 } else
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400454 tb->zeroes_num -= l_n;
455 tb->pos_in_item = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400457 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1))
458 || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)
459 || !op_is_left_mergeable(internal_key(tb->CFL[0], tb->lkey[0]), tbS0->b_size),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 "PAP-12120: item must be merge-able with left neighboring item");
461 } else { /* only part of the appended item will be in L[0] */
462
463 /* Calculate position in item for append in S[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400464 tb->pos_in_item -= tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400466 RFALSE(tb->pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", tb->pos_in_item);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467
468 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500469 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470 }
471 }
472 } else { /* appended item will be in L[0] in whole */
473
474 struct item_head *pasted;
475
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400476 if (!tb->item_pos && op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)) { /* if we paste into first item of S[0] and it is left mergable */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 /* then increment pos_in_item by the size of the last item in L[0] */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400478 pasted = item_head(tb->L[0], n - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700479 if (is_direntry_le_ih(pasted))
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400480 tb->pos_in_item += ih_entry_count(pasted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700481 else
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400482 tb->pos_in_item += ih_item_len(pasted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 }
484
485 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500486 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 /* Append to body of item in L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400488 buffer_info_init_left(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400489 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val,
490 tb->pos_in_item,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491 tb->insert_size[0],
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400492 body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700493
494 /* if appended item is directory, paste entry */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400495 pasted = item_head(tb->L[0], n + tb->item_pos - ret_val);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700496 if (is_direntry_le_ih(pasted))
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400497 leaf_paste_entries(&bi, n + tb->item_pos - ret_val,
498 tb->pos_in_item, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -0500499 (struct reiserfs_de_head *) body,
500 body + DEH_SIZE,
501 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502 /* if appended item is indirect item, put unformatted node into un list */
503 if (is_indirect_le_ih(pasted))
504 set_ih_free_space(pasted, 0);
505 tb->insert_size[0] = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400506 tb->zeroes_num = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700507 }
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400508
509}
510
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400511/* Shift lnum[0] items from S[0] to the left neighbor L[0] */
512static void balance_leaf_left(struct tree_balance *tb, struct item_head *ih,
513 const char *body, int flag)
514{
515 if (tb->lnum[0] <= 0)
516 return;
517
518 /* new item or it part falls to L[0], shift it too */
519 if (tb->item_pos < tb->lnum[0]) {
520 BUG_ON(flag != M_INSERT && flag != M_PASTE);
521
522 if (flag == M_INSERT)
523 balance_leaf_insert_left(tb, ih, body);
524 else /* M_PASTE */
525 balance_leaf_paste_left(tb, ih, body);
526 } else
527 /* new item doesn't fall into L[0] */
528 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
529}
530
531
Jeff Mahoneye80ef3d2014-04-23 10:00:49 -0400532static void balance_leaf_insert_right(struct tree_balance *tb,
533 struct item_head *ih, const char *body)
534{
535
536 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
537 int n = B_NR_ITEMS(tbS0);
538 struct buffer_info bi;
539 int ret_val;
540 if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */
541 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */
542 loff_t old_key_comp, old_len, r_zeroes_number;
543 const char *r_body;
544 int version;
545 loff_t offset;
546
547 leaf_shift_right(tb, tb->rnum[0] - 1, -1);
548
549 version = ih_version(ih);
550 /* Remember key component and item length */
551 old_key_comp = le_ih_k_offset(ih);
552 old_len = ih_item_len(ih);
553
554 /* Calculate key component and item length to insert into R[0] */
555 offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0));
556 set_le_ih_k_offset(ih, offset);
557 put_ih_item_len(ih, tb->rbytes);
558 /* Insert part of the item into R[0] */
559 buffer_info_init_right(tb, &bi);
560 if ((old_len - tb->rbytes) > tb->zeroes_num) {
561 r_zeroes_number = 0;
562 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
563 } else {
564 r_body = body;
565 r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes);
566 tb->zeroes_num -= r_zeroes_number;
567 }
568
569 leaf_insert_into_buf(&bi, 0, ih, r_body,
570 r_zeroes_number);
571
572 /* Replace right delimiting key by first key in R[0] */
573 replace_key(tb, tb->CFR[0], tb->rkey[0],
574 tb->R[0], 0);
575
576 /* Calculate key component and item length to insert into S[0] */
577 set_le_ih_k_offset(ih, old_key_comp);
578 put_ih_item_len(ih, old_len - tb->rbytes);
579
580 tb->insert_size[0] -= tb->rbytes;
581
582 } else { /* whole new item falls into R[0] */
583
584 /* Shift rnum[0]-1 items to R[0] */
585 ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
586 /* Insert new item into R[0] */
587 buffer_info_init_right(tb, &bi);
588 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
589 ih, body, tb->zeroes_num);
590
591 if (tb->item_pos - n + tb->rnum[0] - 1 == 0) {
592 replace_key(tb, tb->CFR[0],
593 tb->rkey[0],
594 tb->R[0], 0);
595
596 }
597 tb->zeroes_num = tb->insert_size[0] = 0;
598 }
599 } else { /* new item or part of it doesn't fall into R[0] */
600
601 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
602 }
603
604}
605
Jeff Mahoney3f0eb272014-04-23 10:00:50 -0400606static void balance_leaf_paste_right(struct tree_balance *tb,
607 struct item_head *ih, const char *body)
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400608{
609 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -0400610 int n = B_NR_ITEMS(tbS0);
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400611 struct buffer_info bi;
Jeff Mahoneycf22df12014-04-23 10:00:48 -0400612 int ret_val;
613
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400614 if (n - tb->rnum[0] <= tb->item_pos) { /* pasted item or part of it falls to R[0] */
615 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */
616 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 int entry_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400619 RFALSE(tb->zeroes_num,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700620 "PAP-12145: invalid parameter in case of a directory");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400621 entry_count = ih_entry_count(item_head
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400622 (tbS0, tb->item_pos));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 if (entry_count - tb->rbytes <
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400624 tb->pos_in_item)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625 /* new directory entry falls into R[0] */
626 {
627 int paste_entry_position;
628
Dave Jones416e2ab2014-02-17 16:21:24 -0500629 RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700630 "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500631 tb->rbytes, entry_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700632 /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500633 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700634 /* Paste given directory entry to directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400635 paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1;
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400636 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400637 leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 /* paste entry */
Dave Jones416e2ab2014-02-17 16:21:24 -0500639 leaf_paste_entries(&bi, 0, paste_entry_position, 1,
640 (struct reiserfs_de_head *) body,
641 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700642
Dave Jones416e2ab2014-02-17 16:21:24 -0500643 if (paste_entry_position == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700644 /* change delimiting keys */
Dave Jones416e2ab2014-02-17 16:21:24 -0500645 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700646 }
647
648 tb->insert_size[0] = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400649 tb->pos_in_item++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700650 } else { /* new directory entry doesn't fall into R[0] */
651
Dave Jones416e2ab2014-02-17 16:21:24 -0500652 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653 }
654 } else { /* regular object */
655
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400656 int n_shift, n_rem, r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 const char *r_body;
658
659 /* Calculate number of bytes which must be shifted from appended item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500660 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700661 n_shift = 0;
662
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400663 RFALSE(tb->pos_in_item != ih_item_len
664 (item_head(tbS0, tb->item_pos)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700665 "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400666 tb->pos_in_item, ih_item_len
667 (item_head(tbS0, tb->item_pos)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668
Dave Jones416e2ab2014-02-17 16:21:24 -0500669 leaf_shift_right(tb, tb->rnum[0], n_shift);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 /* Calculate number of bytes which must remain in body after appending to R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500671 if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 n_rem = 0;
673
674 {
675 int version;
Dave Jones416e2ab2014-02-17 16:21:24 -0500676 unsigned long temp_rem = n_rem;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400678 version = ih_version(item_head(tb->R[0], 0));
679 if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500680 temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400682 set_le_key_k_offset(version, leaf_key(tb->R[0], 0),
683 le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem);
684 set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
685 le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700686 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400687/* k_offset (leaf_key(tb->R[0],0)) += n_rem;
688 k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/
Dave Jones416e2ab2014-02-17 16:21:24 -0500689 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691 /* Append part of body into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400692 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400693 if (n_rem > tb->zeroes_num) {
694 r_zeroes_number = 0;
695 r_body = body + n_rem - tb->zeroes_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696 } else {
697 r_body = body;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400698 r_zeroes_number = tb->zeroes_num - n_rem;
699 tb->zeroes_num -= r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dave Jones416e2ab2014-02-17 16:21:24 -0500702 leaf_paste_in_buffer(&bi, 0, n_shift,
703 tb->insert_size[0] - n_rem,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400704 r_body, r_zeroes_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400706 if (is_indirect_le_ih(item_head(tb->R[0], 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707#if 0
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708 RFALSE(n_rem,
709 "PAP-12160: paste more than one unformatted node pointer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710#endif
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400711 set_ih_free_space(item_head(tb->R[0], 0), 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700712 }
713 tb->insert_size[0] = n_rem;
714 if (!n_rem)
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400715 tb->pos_in_item++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 }
717 } else { /* pasted item in whole falls into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 struct item_head *pasted;
720
Dave Jones416e2ab2014-02-17 16:21:24 -0500721 ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 /* append item in R[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400723 if (tb->pos_in_item >= 0) {
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400724 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400725 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item,
726 tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700727 }
728
729 /* paste new entry, if item is directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400730 pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
731 if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
732 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
733 tb->pos_in_item, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -0500734 (struct reiserfs_de_head *) body,
735 body + DEH_SIZE, tb->insert_size[0]);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400736 if (!tb->pos_in_item) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700737
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400738 RFALSE(tb->item_pos - n + tb->rnum[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700739 "PAP-12165: directory item must be first item of node when pasting is in 0th position");
740
741 /* update delimiting keys */
Dave Jones416e2ab2014-02-17 16:21:24 -0500742 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 }
744 }
745
746 if (is_indirect_le_ih(pasted))
747 set_ih_free_space(pasted, 0);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400748 tb->zeroes_num = tb->insert_size[0] = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700749 }
750 } else { /* new item doesn't fall into R[0] */
751
752 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
753 }
Jeff Mahoney3f0eb272014-04-23 10:00:50 -0400754
755}
756
Jeff Mahoney0080e9f2014-04-23 10:00:55 -0400757/* shift rnum[0] items from S[0] to the right neighbor R[0] */
758static void balance_leaf_right(struct tree_balance *tb, struct item_head *ih,
759 const char *body, int flag)
760{
761 if (tb->rnum[0] <= 0)
762 return;
763
764 BUG_ON(flag != M_INSERT && flag != M_PASTE);
765
766 if (flag == M_INSERT)
767 balance_leaf_insert_right(tb, ih, body);
768 else /* M_PASTE */
769 balance_leaf_paste_right(tb, ih, body);
770
771}
772
Jeff Mahoney65ab18c2014-04-23 10:00:51 -0400773static void balance_leaf_new_nodes_insert(struct tree_balance *tb,
774 struct item_head *ih,
775 const char *body,
776 struct item_head *insert_key,
777 struct buffer_head **insert_ptr,
778 int i)
779{
780 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
781 int n = B_NR_ITEMS(tbS0);
782 struct buffer_info bi;
783 if (n - tb->snum[i] < tb->item_pos) { /* new item or it's part falls to first new node S_new[i] */
784 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { /* part of new item falls into S_new[i] */
785 int old_key_comp, old_len, r_zeroes_number;
786 const char *r_body;
787 int version;
788
789 /* Move snum[i]-1 items from S[0] to S_new[i] */
790 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
791 tb->snum[i] - 1, -1,
792 tb->S_new[i]);
793 /* Remember key component and item length */
794 version = ih_version(ih);
795 old_key_comp = le_ih_k_offset(ih);
796 old_len = ih_item_len(ih);
797
798 /* Calculate key component and item length to insert into S_new[i] */
799 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
800 ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
801
802 put_ih_item_len(ih, tb->sbytes[i]);
803
804 /* Insert part of the item into S_new[i] before 0-th item */
805 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
806
807 if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
808 r_zeroes_number = 0;
809 r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num;
810 } else {
811 r_body = body;
812 r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]);
813 tb->zeroes_num -= r_zeroes_number;
814 }
815
816 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
817
818 /* Calculate key component and item length to insert into S[i] */
819 set_le_ih_k_offset(ih, old_key_comp);
820 put_ih_item_len(ih, old_len - tb->sbytes[i]);
821 tb->insert_size[0] -= tb->sbytes[i];
822 } else { /* whole new item falls into S_new[i] */
823
824 /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
825 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
826 tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
827
828 /* Insert new item into S_new[i] */
829 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
830 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
831 ih, body, tb->zeroes_num);
832
833 tb->zeroes_num = tb->insert_size[0] = 0;
834 }
835 }
836
837 else { /* new item or it part don't falls into S_new[i] */
838
839 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
840 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
841 }
842}
843
Jeff Mahoney9d496552014-04-23 10:00:52 -0400844static void balance_leaf_new_nodes_paste(struct tree_balance *tb,
845 struct item_head *ih,
846 const char *body,
847 struct item_head *insert_key,
848 struct buffer_head **insert_ptr,
849 int i)
850{
851 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
852 int n = B_NR_ITEMS(tbS0);
853 struct buffer_info bi;
854 if (n - tb->snum[i] <= tb->item_pos) { /* pasted item or part if it falls to S_new[i] */
855 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) { /* we must shift part of the appended item */
856 struct item_head *aux_ih;
857
858 RFALSE(ih, "PAP-12210: ih must be 0");
859
860 aux_ih = item_head(tbS0, tb->item_pos);
861 if (is_direntry_le_ih(aux_ih)) {
862 /* we append to directory item */
863
864 int entry_count;
865
866 entry_count = ih_entry_count(aux_ih);
867
868 if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) {
869 /* new directory entry falls into S_new[i] */
870
871 RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0");
872 RFALSE(tb->sbytes[i] - 1 >= entry_count,
873 "PAP-12220: there are no so much entries (%d), only %d",
874 tb->sbytes[i] - 1, entry_count);
875
876 /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
877 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]);
878 /* Paste given directory entry to directory item */
879 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
880 leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1,
881 tb->insert_size[0], body, tb->zeroes_num);
882 /* paste new directory entry */
883 leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1,
884 (struct reiserfs_de_head *) body,
885 body + DEH_SIZE, tb->insert_size[0]);
886 tb->insert_size[0] = 0;
887 tb->pos_in_item++;
888 } else { /* new directory entry doesn't fall into S_new[i] */
889 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]);
890 }
891 } else { /* regular object */
892
893 int n_shift, n_rem, r_zeroes_number;
894 const char *r_body;
895
896 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0,
897 "PAP-12225: item too short or insert_size <= 0");
898
899 /* Calculate number of bytes which must be shifted from appended item */
900 n_shift = tb->sbytes[i] - tb->insert_size[0];
901 if (n_shift < 0)
902 n_shift = 0;
903 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]);
904
905 /* Calculate number of bytes which must remain in body after append to S_new[i] */
906 n_rem = tb->insert_size[0] - tb->sbytes[i];
907 if (n_rem < 0)
908 n_rem = 0;
909 /* Append part of body into S_new[0] */
910 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
911 if (n_rem > tb->zeroes_num) {
912 r_zeroes_number = 0;
913 r_body = body + n_rem - tb->zeroes_num;
914 } else {
915 r_body = body;
916 r_zeroes_number = tb->zeroes_num - n_rem;
917 tb->zeroes_num -= r_zeroes_number;
918 }
919
920 leaf_paste_in_buffer(&bi, 0, n_shift,
921 tb->insert_size[0] - n_rem,
922 r_body, r_zeroes_number);
923 {
924 struct item_head *tmp;
925
926 tmp = item_head(tb->S_new[i], 0);
927 if (is_indirect_le_ih
928 (tmp)) {
929 set_ih_free_space(tmp, 0);
930 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT)));
931 } else {
932 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem);
933 }
934 }
935
936 tb->insert_size[0] = n_rem;
937 if (!n_rem)
938 tb->pos_in_item++;
939 }
940 } else
941 /* item falls wholly into S_new[i] */
942 {
943 int leaf_mi;
944 struct item_head *pasted;
945
946#ifdef CONFIG_REISERFS_CHECK
947 struct item_head *ih_check = item_head(tbS0, tb->item_pos);
948
949 if (!is_direntry_le_ih(ih_check)
950 && (tb->pos_in_item != ih_item_len(ih_check)
951 || tb->insert_size[0] <= 0))
952 reiserfs_panic(tb->tb_sb,
953 "PAP-12235",
954 "pos_in_item "
955 "must be equal "
956 "to ih_item_len");
957#endif /* CONFIG_REISERFS_CHECK */
958
959 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW,
960 tb, tb->snum[i],
961 tb->sbytes[i],
962 tb->S_new[i]);
963
964 RFALSE(leaf_mi,
965 "PAP-12240: unexpected value returned by leaf_move_items (%d)",
966 leaf_mi);
967
968 /* paste into item */
969 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
970 leaf_paste_in_buffer(&bi,
971 tb->item_pos - n + tb->snum[i],
972 tb->pos_in_item,
973 tb->insert_size[0],
974 body, tb->zeroes_num);
975
976 pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]);
977 if (is_direntry_le_ih(pasted)) {
978 leaf_paste_entries(&bi,
979 tb->item_pos - n + tb->snum[i],
980 tb->pos_in_item, 1,
981 (struct reiserfs_de_head *)body,
982 body + DEH_SIZE,
983 tb->insert_size[0]
984 );
985 }
986
987 /* if we paste to indirect item update ih_free_space */
988 if (is_indirect_le_ih(pasted))
989 set_ih_free_space(pasted, 0);
990 tb->zeroes_num = tb->insert_size[0] = 0;
991 }
992 }
993
994 else { /* pasted item doesn't fall into S_new[i] */
995
996 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
997 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
998 }
999
1000}
1001
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001002/* Fill new nodes that appear in place of S[0] */
1003static void balance_leaf_new_nodes(struct tree_balance *tb,
1004 struct item_head *ih,
1005 const char *body,
1006 struct item_head *insert_key,
1007 struct buffer_head **insert_ptr,
1008 int flag)
1009{
1010 int i;
1011 for (i = tb->blknum[0] - 2; i >= 0; i--) {
1012
1013 RFALSE(!tb->snum[i],
1014 "PAP-12200: snum[%d] == %d. Must be > 0", i,
1015 tb->snum[i]);
1016
1017 /* here we shift from S to S_new nodes */
1018
1019 tb->S_new[i] = get_FEB(tb);
1020
1021 /* initialized block type and tree level */
1022 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
1023
1024 switch (flag) {
1025 case M_INSERT: /* insert item */
1026 balance_leaf_new_nodes_insert(tb, ih, body, insert_key,
1027 insert_ptr, i);
1028 break;
1029
1030 case M_PASTE: /* append item */
1031 balance_leaf_new_nodes_paste(tb, ih, body, insert_key,
1032 insert_ptr, i);
1033 break;
1034 default: /* cases d and t */
1035 reiserfs_panic(tb->tb_sb, "PAP-12245",
1036 "blknum > 2: unexpected mode: %s(%d)",
1037 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
1038 }
1039
1040 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1041 insert_ptr[i] = tb->S_new[i];
1042
1043 RFALSE(!buffer_journaled(tb->S_new[i])
1044 || buffer_journal_dirty(tb->S_new[i])
1045 || buffer_dirty(tb->S_new[i]),
1046 "PAP-12247: S_new[%d] : (%b)",
1047 i, format_bh(tb->S_new[i]));
1048 }
1049}
1050
Jeff Mahoney8c480ea2014-04-23 10:00:53 -04001051static void balance_leaf_finish_node_insert(struct tree_balance *tb,
1052 struct item_head *ih,
1053 const char *body)
1054{
1055 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1056 struct buffer_info bi;
1057 buffer_info_init_tbS0(tb, &bi);
1058 leaf_insert_into_buf(&bi, tb->item_pos, ih,
1059 body, tb->zeroes_num);
1060
1061 /*
1062 * If we insert the first key
1063 * change the delimiting key
1064 */
1065 if (tb->item_pos == 0) {
1066 if (tb->CFL[0]) /* can be 0 in reiserfsck */
1067 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1068 }
1069}
1070
Jeff Mahoney7f447ba2014-04-23 10:00:54 -04001071static void balance_leaf_finish_node_paste(struct tree_balance *tb,
1072 struct item_head *ih,
1073 const char *body)
1074{
1075 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
1076 struct buffer_info bi;
1077 struct item_head *pasted;
1078
1079 pasted = item_head(tbS0, tb->item_pos);
1080 /* when directory, may be new entry already pasted */
1081 if (is_direntry_le_ih(pasted)) {
1082 if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
1083
1084 RFALSE(!tb->insert_size[0],
1085 "PAP-12260: insert_size is 0 already");
1086
1087 /* prepare space */
1088 buffer_info_init_tbS0(tb, &bi);
1089 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1090 tb->insert_size[0], body,
1091 tb->zeroes_num);
1092
1093 /* paste entry */
1094 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
1095 (struct reiserfs_de_head *)body,
1096 body + DEH_SIZE,
1097 tb->insert_size[0]);
1098 if (!tb->item_pos && !tb->pos_in_item) {
1099 RFALSE(!tb->CFL[0] || !tb->L[0],
1100 "PAP-12270: CFL[0]/L[0] must be specified");
1101 if (tb->CFL[0])
1102 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
1103 }
1104 tb->insert_size[0] = 0;
1105 }
1106 } else { /* regular object */
1107 if (tb->pos_in_item == ih_item_len(pasted)) {
1108
1109 RFALSE(tb->insert_size[0] <= 0,
1110 "PAP-12275: insert size must not be %d",
1111 tb->insert_size[0]);
1112 buffer_info_init_tbS0(tb, &bi);
1113 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1114 tb->insert_size[0], body, tb->zeroes_num);
1115
1116 if (is_indirect_le_ih(pasted)) {
1117#if 0
1118 RFALSE(tb->
1119 insert_size[0] !=
1120 UNFM_P_SIZE,
1121 "PAP-12280: insert_size for indirect item must be %d, not %d",
1122 UNFM_P_SIZE,
1123 tb->
1124 insert_size[0]);
1125#endif
1126 set_ih_free_space(pasted, 0);
1127 }
1128 tb->insert_size[0] = 0;
1129 }
1130#ifdef CONFIG_REISERFS_CHECK
1131 else {
1132 if (tb->insert_size[0]) {
1133 print_cur_tb("12285");
1134 reiserfs_panic(tb->tb_sb,
1135 "PAP-12285",
1136 "insert_size "
1137 "must be 0 "
1138 "(%d)",
1139 tb->insert_size[0]);
1140 }
1141 }
1142#endif /* CONFIG_REISERFS_CHECK */
1143
1144 }
1145}
1146
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001147/*
1148 * if the affected item was not wholly shifted then we
1149 * perform all necessary operations on that part or whole
1150 * of the affected item which remains in S
1151 */
1152static void balance_leaf_finish_node(struct tree_balance *tb,
1153 struct item_head *ih,
1154 const char *body, int flag)
1155{
1156 /* if we must insert or append into buffer S[0] */
1157 if (0 <= tb->item_pos && tb->item_pos < tb->s0num) {
1158 if (flag == M_INSERT)
1159 balance_leaf_finish_node_insert(tb, ih, body);
1160 else /* M_PASTE */
1161 balance_leaf_finish_node_paste(tb, ih, body);
1162 }
1163}
1164
Jeff Mahoney3f0eb272014-04-23 10:00:50 -04001165/**
1166 * balance_leaf - reiserfs tree balancing algorithm
1167 * @tb: tree balance state
1168 * @ih: item header of inserted item (little endian)
1169 * @body: body of inserted item or bytes to paste
1170 * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance)
1171 * passed back:
1172 * @insert_key: key to insert new nodes
1173 * @insert_ptr: array of nodes to insert at the next level
1174 *
1175 * In our processing of one level we sometimes determine what must be
1176 * inserted into the next higher level. This insertion consists of a
1177 * key or two keys and their corresponding pointers.
1178 */
1179static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
1180 const char *body, int flag,
1181 struct item_head *insert_key,
1182 struct buffer_head **insert_ptr)
1183{
1184 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -04001185
1186 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
1187
1188 /* Make balance in case insert_size[0] < 0 */
1189 if (tb->insert_size[0] < 0)
1190 return balance_leaf_when_delete(tb, flag);
1191
1192 tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
1193 tb->pos_in_item = tb->tb_path->pos_in_item,
1194 tb->zeroes_num = 0;
1195 if (flag == M_INSERT && !body)
1196 tb->zeroes_num = ih_item_len(ih);
1197
1198 /*
1199 * for indirect item pos_in_item is measured in unformatted node
1200 * pointers. Recalculate to bytes
1201 */
1202 if (flag != M_INSERT
1203 && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
1204 tb->pos_in_item *= UNFM_P_SIZE;
1205
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001206 balance_leaf_left(tb, ih, body, flag);
Jeff Mahoney3f0eb272014-04-23 10:00:50 -04001207
1208 /* tb->lnum[0] > 0 */
1209 /* Calculate new item position */
1210 tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
1211
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001212 balance_leaf_right(tb, ih, body, flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213
1214 /* tb->rnum[0] > 0 */
1215 RFALSE(tb->blknum[0] > 3,
Dave Jones416e2ab2014-02-17 16:21:24 -05001216 "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001217 RFALSE(tb->blknum[0] < 0,
Dave Jones416e2ab2014-02-17 16:21:24 -05001218 "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001220 /*
1221 * if while adding to a node we discover that it is possible to split
1222 * it in two, and merge the left part into the left neighbor and the
1223 * right part into the right neighbor, eliminating the node
1224 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001225 if (tb->blknum[0] == 0) { /* node S[0] is empty now */
1226
1227 RFALSE(!tb->lnum[0] || !tb->rnum[0],
1228 "PAP-12190: lnum and rnum must not be zero");
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001229 /*
1230 * if insertion was done before 0-th position in R[0], right
1231 * delimiting key of the tb->L[0]'s and left delimiting key are
1232 * not set correctly
1233 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001234 if (tb->CFL[0]) {
1235 if (!tb->CFR[0])
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001236 reiserfs_panic(tb->tb_sb, "vs-12195",
1237 "CFR not initialized");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001238 copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
1239 internal_key(tb->CFR[0], tb->rkey[0]));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001240 do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
1241 }
1242
1243 reiserfs_invalidate_buffer(tb, tbS0);
1244 return 0;
1245 }
1246
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001247 balance_leaf_new_nodes(tb, ih, body, insert_key, insert_ptr, flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001248
Jeff Mahoney0080e9f2014-04-23 10:00:55 -04001249 balance_leaf_finish_node(tb, ih, body, flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001250
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001251#ifdef CONFIG_REISERFS_CHECK
1252 if (flag == M_PASTE && tb->insert_size[0]) {
1253 print_cur_tb("12290");
1254 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001255 "PAP-12290", "insert_size is still not 0 (%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001256 tb->insert_size[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001258#endif
1259
1260 /* Leaf level of the tree is balanced (end of balance_leaf) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264/* Make empty node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265void make_empty_node(struct buffer_info *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001267 struct block_head *blkh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001269 RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001271 blkh = B_BLK_HEAD(bi->bi_bh);
1272 set_blkh_nr_item(blkh, 0);
1273 set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001275 if (bi->bi_parent)
1276 B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279/* Get first empty buffer */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280struct buffer_head *get_FEB(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001282 int i;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001283 struct buffer_info bi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285 for (i = 0; i < MAX_FEB_SIZE; i++)
Al Viro9dce07f2008-03-29 03:07:28 +00001286 if (tb->FEB[i] != NULL)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001289 if (i == MAX_FEB_SIZE)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001290 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001292 buffer_info_init_bh(tb, &bi, tb->FEB[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001293 make_empty_node(&bi);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001294 set_buffer_uptodate(tb->FEB[i]);
1295 tb->used[i] = tb->FEB[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001296 tb->FEB[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001298 return tb->used[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Jeff Mahoney098297b2014-04-23 10:00:36 -04001301/* This is now used because reiserfs_free_block has to be able to schedule. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001302static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001304 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001306 if (buffer_dirty(bh))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001307 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1308 "called with dirty buffer");
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001309 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001310 if (!tb->thrown[i]) {
1311 tb->thrown[i] = bh;
1312 get_bh(bh); /* free_thrown puts this */
1313 return;
1314 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001315 reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1316 "too many thrown buffers");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001319static void free_thrown(struct tree_balance *tb)
1320{
1321 int i;
1322 b_blocknr_t blocknr;
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001323 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001324 if (tb->thrown[i]) {
1325 blocknr = tb->thrown[i]->b_blocknr;
1326 if (buffer_dirty(tb->thrown[i]))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001327 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1328 "called with dirty buffer %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001329 blocknr);
1330 brelse(tb->thrown[i]); /* incremented in store_thrown */
1331 reiserfs_free_block(tb->transaction_handle, NULL,
1332 blocknr, 0);
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 struct block_head *blkh;
1340 blkh = B_BLK_HEAD(bh);
1341 set_blkh_level(blkh, FREE_LEVEL);
1342 set_blkh_nr_item(blkh, 0);
1343
1344 clear_buffer_dirty(bh);
1345 store_thrown(tb, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348/* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001349void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1350 struct buffer_head *src, int n_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001353 RFALSE(dest == NULL || src == NULL,
1354 "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
1355 src, dest);
1356 RFALSE(!B_IS_KEYS_LEVEL(dest),
1357 "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
1358 dest);
1359 RFALSE(n_dest < 0 || n_src < 0,
1360 "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
1361 RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
1362 "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
1363 n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001365 if (B_IS_ITEMS_LEVEL(src))
1366 /* source buffer contains leaf node */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001367 memcpy(internal_key(dest, n_dest), item_head(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001368 KEY_SIZE);
1369 else
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001370 memcpy(internal_key(dest, n_dest), internal_key(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001371 KEY_SIZE);
1372
1373 do_balance_mark_internal_dirty(tb, dest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001376int get_left_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001378 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Al Viro9dce07f2008-03-29 03:07:28 +00001380 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001381 "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
1382 h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 if (Sh_position == 0)
1385 return B_NR_ITEMS(tb->FL[h]);
1386 else
1387 return Sh_position - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001390int get_right_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001392 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Al Viro9dce07f2008-03-29 03:07:28 +00001394 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001395 "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
1396 h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001398 if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1399 return 0;
1400 else
1401 return Sh_position + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404#ifdef CONFIG_REISERFS_CHECK
1405
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001406int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
1407static void check_internal_node(struct super_block *s, struct buffer_head *bh,
1408 char *mes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001410 struct disk_child *dc;
1411 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001413 RFALSE(!bh, "PAP-12336: bh == 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001415 if (!bh || !B_IS_IN_TREE(bh))
1416 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001418 RFALSE(!buffer_dirty(bh) &&
1419 !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
1420 "PAP-12337: buffer (%b) must be dirty", bh);
1421 dc = B_N_CHILD(bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001423 for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
1424 if (!is_reusable(s, dc_block_number(dc), 1)) {
1425 print_cur_tb(mes);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001426 reiserfs_panic(s, "PAP-12338",
1427 "invalid child pointer %y in %b",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 dc, bh);
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001431}
1432
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001433static int locked_or_not_in_tree(struct tree_balance *tb,
1434 struct buffer_head *bh, char *which)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001435{
1436 if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
1437 !B_IS_IN_TREE(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001438 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001439 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001444static int check_before_balancing(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001446 int retval = 0;
1447
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001448 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001449 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1450 "occurred based on cur_tb not being null at "
1451 "this point in code. do_balance cannot properly "
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001452 "handle concurrent tree accesses on a same "
1453 "mount point.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001454 }
1455
Jeff Mahoney098297b2014-04-23 10:00:36 -04001456 /*
1457 * double check that buffers that we will modify are unlocked.
1458 * (fix_nodes should already have prepped all of these for us).
1459 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001460 if (tb->lnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001461 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1462 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1463 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001464 check_leaf(tb->L[0]);
1465 }
1466 if (tb->rnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001467 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1468 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1469 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001470 check_leaf(tb->R[0]);
1471 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001472 retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1473 "S[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001474 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1475
1476 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001479static void check_after_balance_leaf(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001481 if (tb->lnum[0]) {
1482 if (B_FREE_SPACE(tb->L[0]) !=
1483 MAX_CHILD_SIZE(tb->L[0]) -
1484 dc_size(B_N_CHILD
1485 (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1486 print_cur_tb("12221");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001487 reiserfs_panic(tb->tb_sb, "PAP-12355",
1488 "shift to left was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001489 }
1490 }
1491 if (tb->rnum[0]) {
1492 if (B_FREE_SPACE(tb->R[0]) !=
1493 MAX_CHILD_SIZE(tb->R[0]) -
1494 dc_size(B_N_CHILD
1495 (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1496 print_cur_tb("12222");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001497 reiserfs_panic(tb->tb_sb, "PAP-12360",
1498 "shift to right was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001499 }
1500 }
1501 if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1502 (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1503 (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1504 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1505 PATH_H_POSITION(tb->tb_path, 1)))))) {
1506 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1507 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1508 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1509 PATH_H_POSITION(tb->tb_path,
1510 1))));
1511 print_cur_tb("12223");
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001512 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001513 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1514 "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
1515 left,
1516 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1517 PATH_H_PBUFFER(tb->tb_path, 1),
1518 PATH_H_POSITION(tb->tb_path, 1),
1519 dc_size(B_N_CHILD
1520 (PATH_H_PBUFFER(tb->tb_path, 1),
1521 PATH_H_POSITION(tb->tb_path, 1))),
1522 right);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001523 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001524 }
1525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001527static void check_leaf_level(struct tree_balance *tb)
1528{
1529 check_leaf(tb->L[0]);
1530 check_leaf(tb->R[0]);
1531 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1532}
1533
1534static void check_internal_levels(struct tree_balance *tb)
1535{
1536 int h;
1537
1538 /* check all internal nodes */
1539 for (h = 1; tb->insert_size[h]; h++) {
1540 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1541 "BAD BUFFER ON PATH");
1542 if (tb->lnum[h])
1543 check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1544 if (tb->rnum[h])
1545 check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548}
1549
1550#endif
1551
Jeff Mahoney098297b2014-04-23 10:00:36 -04001552/*
1553 * Now we have all of the buffers that must be used in balancing of
1554 * the tree. We rely on the assumption that schedule() will not occur
1555 * while do_balance works. ( Only interrupt handlers are acceptable.)
1556 * We balance the tree according to the analysis made before this,
1557 * using buffers already obtained. For SMP support it will someday be
1558 * necessary to add ordered locking of tb.
1559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Jeff Mahoney098297b2014-04-23 10:00:36 -04001561/*
1562 * Some interesting rules of balancing:
1563 * we delete a maximum of two nodes per level per balancing: we never
1564 * delete R, when we delete two of three nodes L, S, R then we move
1565 * them into R.
1566 *
1567 * we only delete L if we are deleting two nodes, if we delete only
1568 * one node we delete S
1569 *
1570 * if we shift leaves then we shift as much as we can: this is a
1571 * deliberate policy of extremism in node packing which results in
1572 * higher average utilization after repeated random balance operations
1573 * at the cost of more memory copies and more balancing as a result of
1574 * small insertions to full nodes.
1575 *
1576 * if we shift internal nodes we try to evenly balance the node
1577 * utilization, with consequent less balancing at the cost of lower
1578 * utilization.
1579 *
1580 * one could argue that the policy for directories in leaves should be
1581 * that of internal nodes, but we will wait until another day to
1582 * evaluate this.... It would be nice to someday measure and prove
1583 * these assumptions as to what is optimal....
1584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001586static inline void do_balance_starts(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Jeff Mahoney098297b2014-04-23 10:00:36 -04001588 /* use print_cur_tb() to see initial state of struct tree_balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001590 /* store_print_tb (tb); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001592 /* do not delete, just comment it out */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001593 /*
1594 print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1595 tb->tb_path->pos_in_item, tb, "check");
1596 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001597 RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#ifdef CONFIG_REISERFS_CHECK
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001599 REISERFS_SB(tb->tb_sb)->cur_tb = tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600#endif
1601}
1602
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001603static inline void do_balance_completed(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001607 check_leaf_level(tb);
1608 check_internal_levels(tb);
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001609 REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610#endif
1611
Jeff Mahoney098297b2014-04-23 10:00:36 -04001612 /*
1613 * reiserfs_free_block is no longer schedule safe. So, we need to
1614 * put the buffers we want freed on the thrown list during do_balance,
1615 * and then free them now
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001618 REISERFS_SB(tb->tb_sb)->s_do_balance++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001620 /* release all nodes hold to perform the balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 unfix_nodes(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001623 free_thrown(tb);
1624}
1625
Jeff Mahoney098297b2014-04-23 10:00:36 -04001626/*
1627 * do_balance - balance the tree
1628 *
1629 * @tb: tree_balance structure
1630 * @ih: item header of inserted item
1631 * @body: body of inserted item or bytes to paste
1632 * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste
1633 *
1634 * Cut means delete part of an item (includes removing an entry from a
1635 * directory).
1636 *
1637 * Delete means delete whole item.
1638 *
1639 * Insert means add a new item into the tree.
1640 *
1641 * Paste means to append to the end of an existing file or to
1642 * insert a directory entry.
1643 */
1644void do_balance(struct tree_balance *tb, struct item_head *ih,
1645 const char *body, int flag)
1646{
1647 int child_pos; /* position of a child node in its parent */
1648 int h; /* level of the tree being processed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001649
Jeff Mahoney098297b2014-04-23 10:00:36 -04001650 /*
1651 * in our processing of one level we sometimes determine what
1652 * must be inserted into the next higher level. This insertion
1653 * consists of a key or two keys and their corresponding
1654 * pointers
1655 */
1656 struct item_head insert_key[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001657
Jeff Mahoney098297b2014-04-23 10:00:36 -04001658 /* inserted node-ptrs for the next level */
1659 struct buffer_head *insert_ptr[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001660
1661 tb->tb_mode = flag;
1662 tb->need_balance_dirty = 0;
1663
1664 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001665 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1666 "changed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001667 }
1668 /* if we have no real work to do */
1669 if (!tb->insert_size[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001670 reiserfs_warning(tb->tb_sb, "PAP-12350",
1671 "insert_size == 0, mode == %c", flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001672 unfix_nodes(tb);
1673 return;
1674 }
1675
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001676 atomic_inc(&fs_generation(tb->tb_sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001677 do_balance_starts(tb);
1678
Jeff Mahoney098297b2014-04-23 10:00:36 -04001679 /*
1680 * balance_leaf returns 0 except if combining L R and S into
1681 * one node. see balance_internal() for explanation of this
1682 * line of code.
1683 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001684 child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1685 balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001688 check_after_balance_leaf(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689#endif
1690
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001691 /* Balance internal level of the tree. */
1692 for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
1693 child_pos =
1694 balance_internal(tb, h, child_pos, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001696 do_balance_completed(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698}