blob: 6dbe55ff408ebceeffbb8a8cdcc551e53af2d70d [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 Mahoney97fd4b92014-04-23 10:00:45 -0400293/**
294 * balance_leaf - reiserfs tree balancing algorithm
295 * @tb: tree balance state
296 * @ih: item header of inserted item (little endian)
297 * @body: body of inserted item or bytes to paste
298 * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance)
299 * passed back:
300 * @insert_key: key to insert new nodes
301 * @insert_ptr: array of nodes to insert at the next level
302 *
303 * In our processing of one level we sometimes determine what must be
304 * inserted into the next higher level. This insertion consists of a
305 * key or two keys and their corresponding pointers.
306 */
307static int balance_leaf(struct tree_balance *tb, struct item_head *ih,
308 const char *body, int flag,
309 struct item_head *insert_key,
310 struct buffer_head **insert_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700312 struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 struct buffer_info bi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 int n, i;
315 int ret_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700317 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700319 /* Make balance in case insert_size[0] < 0 */
320 if (tb->insert_size[0] < 0)
321 return balance_leaf_when_delete(tb, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400323 tb->item_pos = PATH_LAST_POSITION(tb->tb_path),
324 tb->pos_in_item = tb->tb_path->pos_in_item,
325 tb->zeroes_num = 0;
Al Viro9dce07f2008-03-29 03:07:28 +0000326 if (flag == M_INSERT && !body)
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400327 tb->zeroes_num = ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400329 /*
330 * for indirect item pos_in_item is measured in unformatted node
331 * pointers. Recalculate to bytes
332 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700333 if (flag != M_INSERT
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400334 && is_indirect_le_ih(item_head(tbS0, tb->item_pos)))
335 tb->pos_in_item *= UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700337 if (tb->lnum[0] > 0) {
338 /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400339 if (tb->item_pos < tb->lnum[0]) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700340 /* new item or it part falls to L[0], shift it too */
341 n = B_NR_ITEMS(tb->L[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700343 switch (flag) {
344 case M_INSERT: /* insert item into L[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400346 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700347 /* part of new item falls into L[0] */
348 int new_item_len;
349 int version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dave Jones416e2ab2014-02-17 16:21:24 -0500351 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 /* Calculate item length to insert to S[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500354 new_item_len = ih_item_len(ih) - tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355 /* Calculate and check item length to insert to L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500356 put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700358 RFALSE(ih_item_len(ih) <= 0,
359 "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
360 ih_item_len(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 /* Insert new item into L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400363 buffer_info_init_left(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 leaf_insert_into_buf(&bi,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400365 n + tb->item_pos - ret_val, ih, body,
366 tb->zeroes_num > ih_item_len(ih) ? ih_item_len(ih) : tb->zeroes_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700368 version = ih_version(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700370 /* Calculate key component, item length and body to insert into S[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500371 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
372 (tb-> lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT : 0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 put_ih_item_len(ih, new_item_len);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400375 if (tb->lbytes > tb->zeroes_num) {
376 body += (tb->lbytes - tb->zeroes_num);
377 tb->zeroes_num = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378 } else
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400379 tb->zeroes_num -= tb->lbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 RFALSE(ih_item_len(ih) <= 0,
382 "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
383 ih_item_len(ih));
384 } else {
385 /* new item in whole falls into L[0] */
386 /* Shift lnum[0]-1 items to L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500387 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 /* Insert new item into L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400389 buffer_info_init_left(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400390 leaf_insert_into_buf(&bi, n + tb->item_pos - ret_val, ih, body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 tb->insert_size[0] = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400392 tb->zeroes_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700396 case M_PASTE: /* append item in L[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400398 if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 /* we must shift the part of the appended item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400400 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700401
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400402 RFALSE(tb->zeroes_num,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700403 "PAP-12090: invalid parameter in case of a directory");
404 /* directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400405 if (tb->lbytes > tb->pos_in_item) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406 /* new directory entry falls into L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500407 struct item_head *pasted;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400408 int l_pos_in_item = tb->pos_in_item;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409
410 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500411 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400412 if (ret_val && !tb->item_pos) {
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400413 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
414 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415 }
416
417 /* Append given directory entry to directory item */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400418 buffer_info_init_left(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400419 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 -0700420
421 /* previous string prepared space for pasting new entry, following string pastes this entry */
422
423 /* when we have merge directory item, pos_in_item has been changed too */
424
425 /* paste new directory entry. 1 is entry number */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400426 leaf_paste_entries(&bi, n + tb->item_pos - ret_val, l_pos_in_item,
Dave Jones416e2ab2014-02-17 16:21:24 -0500427 1, (struct reiserfs_de_head *) body,
428 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429 tb->insert_size[0] = 0;
430 } else {
431 /* new directory item doesn't fall into L[0] */
432 /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500433 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 }
435 /* Calculate new position to append in item body */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400436 tb->pos_in_item -= tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 } else {
438 /* regular object */
Dave Jones416e2ab2014-02-17 16:21:24 -0500439 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 -0400440 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400442 ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400444 if (tb->lbytes >= tb->pos_in_item) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700445 /* appended item will be in L[0] in whole */
446 int l_n;
447
448 /* this bytes number must be appended to the last item of L[h] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400449 l_n = tb->lbytes - tb->pos_in_item;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450
451 /* Calculate new insert_size[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500452 tb->insert_size[0] -= l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453
Dave Jones416e2ab2014-02-17 16:21:24 -0500454 RFALSE(tb->insert_size[0] <= 0,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500456 tb->insert_size[0]);
457 ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400458 (item_head(tbS0, tb->item_pos)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 /* Append to body of item in L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400460 buffer_info_init_left(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 leaf_paste_in_buffer
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400462 (&bi, n + tb->item_pos - ret_val, ih_item_len
463 (item_head(tb->L[0], n + tb->item_pos - ret_val)),
Dave Jones416e2ab2014-02-17 16:21:24 -0500464 l_n, body,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400465 tb->zeroes_num > l_n ? l_n : tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700466 /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
467 {
468 int version;
Dave Jones416e2ab2014-02-17 16:21:24 -0500469 int temp_l = l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400471 RFALSE(ih_item_len(item_head(tbS0, 0)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 "PAP-12106: item length must be 0");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400473 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400474 (tb->L[0], n + tb->item_pos - ret_val)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 "PAP-12107: items must be of the same file");
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400476 if (is_indirect_le_ih(item_head(tb->L[0], n + tb->item_pos - ret_val))) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500477 temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 }
479 /* update key of first item in S0 */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400480 version = ih_version(item_head(tbS0, 0));
481 set_le_key_k_offset(version, leaf_key(tbS0, 0),
482 le_key_k_offset(version,leaf_key(tbS0, 0)) + temp_l);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 /* update left delimiting key */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400484 set_le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0]),
485 le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0])) + temp_l);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 }
487
488 /* Calculate new body, position in item and insert_size[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400489 if (l_n > tb->zeroes_num) {
490 body += (l_n - tb->zeroes_num);
491 tb->zeroes_num = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 } else
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400493 tb->zeroes_num -= l_n;
494 tb->pos_in_item = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700495
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400496 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1))
497 || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)
498 || !op_is_left_mergeable(internal_key(tb->CFL[0], tb->lkey[0]), tbS0->b_size),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700499 "PAP-12120: item must be merge-able with left neighboring item");
500 } else { /* only part of the appended item will be in L[0] */
501
502 /* Calculate position in item for append in S[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400503 tb->pos_in_item -= tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400505 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 -0700506
507 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500508 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700509 }
510 }
511 } else { /* appended item will be in L[0] in whole */
512
513 struct item_head *pasted;
514
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400515 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 -0700516 /* then increment pos_in_item by the size of the last item in L[0] */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400517 pasted = item_head(tb->L[0], n - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700518 if (is_direntry_le_ih(pasted))
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400519 tb->pos_in_item += ih_entry_count(pasted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520 else
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400521 tb->pos_in_item += ih_item_len(pasted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700522 }
523
524 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500525 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700526 /* Append to body of item in L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400527 buffer_info_init_left(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400528 leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val,
529 tb->pos_in_item,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700530 tb->insert_size[0],
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400531 body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532
533 /* if appended item is directory, paste entry */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400534 pasted = item_head(tb->L[0], n + tb->item_pos - ret_val);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535 if (is_direntry_le_ih(pasted))
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400536 leaf_paste_entries(&bi, n + tb->item_pos - ret_val,
537 tb->pos_in_item, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -0500538 (struct reiserfs_de_head *) body,
539 body + DEH_SIZE,
540 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541 /* if appended item is indirect item, put unformatted node into un list */
542 if (is_indirect_le_ih(pasted))
543 set_ih_free_space(pasted, 0);
544 tb->insert_size[0] = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400545 tb->zeroes_num = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 }
547 break;
548 default: /* cases d and t */
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400549 reiserfs_panic(tb->tb_sb, "PAP-12130",
550 "lnum > 0: unexpected mode: "
551 " %s(%d)",
Dave Jones416e2ab2014-02-17 16:21:24 -0500552 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700554 } else {
555 /* new item doesn't fall into L[0] */
556 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700560 /* tb->lnum[0] > 0 */
561 /* Calculate new item position */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400562 tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700564 if (tb->rnum[0] > 0) {
565 /* shift rnum[0] items from S[0] to the right neighbor R[0] */
566 n = B_NR_ITEMS(tbS0);
567 switch (flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700569 case M_INSERT: /* insert item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400570 if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */
571 if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */
572 loff_t old_key_comp, old_len, r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 const char *r_body;
574 int version;
575 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Dave Jones416e2ab2014-02-17 16:21:24 -0500577 leaf_shift_right(tb, tb->rnum[0] - 1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700579 version = ih_version(ih);
580 /* Remember key component and item length */
581 old_key_comp = le_ih_k_offset(ih);
582 old_len = ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700584 /* Calculate key component and item length to insert into R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500585 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));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700586 set_le_ih_k_offset(ih, offset);
587 put_ih_item_len(ih, tb->rbytes);
588 /* Insert part of the item into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400589 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400590 if ((old_len - tb->rbytes) > tb->zeroes_num) {
591 r_zeroes_number = 0;
592 r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700593 } else {
594 r_body = body;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400595 r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes);
596 tb->zeroes_num -= r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700599 leaf_insert_into_buf(&bi, 0, ih, r_body,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400600 r_zeroes_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700602 /* Replace right delimiting key by first key in R[0] */
603 replace_key(tb, tb->CFR[0], tb->rkey[0],
604 tb->R[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700606 /* Calculate key component and item length to insert into S[0] */
607 set_le_ih_k_offset(ih, old_key_comp);
Dave Jones416e2ab2014-02-17 16:21:24 -0500608 put_ih_item_len(ih, old_len - tb->rbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700610 tb->insert_size[0] -= tb->rbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700612 } else { /* whole new item falls into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700614 /* Shift rnum[0]-1 items to R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500615 ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700616 /* Insert new item into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400617 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400618 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
619 ih, body, tb->zeroes_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400621 if (tb->item_pos - n + tb->rnum[0] - 1 == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622 replace_key(tb, tb->CFR[0],
623 tb->rkey[0],
624 tb->R[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626 }
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400627 tb->zeroes_num = tb->insert_size[0] = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628 }
629 } else { /* new item or part of it doesn't fall into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700631 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700635 case M_PASTE: /* append item */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400637 if (n - tb->rnum[0] <= tb->item_pos) { /* pasted item or part of it falls to R[0] */
638 if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */
639 if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700640 int entry_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400642 RFALSE(tb->zeroes_num,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 "PAP-12145: invalid parameter in case of a directory");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400644 entry_count = ih_entry_count(item_head
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400645 (tbS0, tb->item_pos));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700646 if (entry_count - tb->rbytes <
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400647 tb->pos_in_item)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700648 /* new directory entry falls into R[0] */
649 {
650 int paste_entry_position;
651
Dave Jones416e2ab2014-02-17 16:21:24 -0500652 RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653 "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500654 tb->rbytes, entry_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 /* 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 -0500656 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 /* Paste given directory entry to directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400658 paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1;
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400659 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400660 leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700661 /* paste entry */
Dave Jones416e2ab2014-02-17 16:21:24 -0500662 leaf_paste_entries(&bi, 0, paste_entry_position, 1,
663 (struct reiserfs_de_head *) body,
664 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700665
Dave Jones416e2ab2014-02-17 16:21:24 -0500666 if (paste_entry_position == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700667 /* change delimiting keys */
Dave Jones416e2ab2014-02-17 16:21:24 -0500668 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 }
670
671 tb->insert_size[0] = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400672 tb->pos_in_item++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700673 } else { /* new directory entry doesn't fall into R[0] */
674
Dave Jones416e2ab2014-02-17 16:21:24 -0500675 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700676 }
677 } else { /* regular object */
678
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400679 int n_shift, n_rem, r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700680 const char *r_body;
681
682 /* Calculate number of bytes which must be shifted from appended item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500683 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700684 n_shift = 0;
685
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400686 RFALSE(tb->pos_in_item != ih_item_len
687 (item_head(tbS0, tb->item_pos)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688 "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400689 tb->pos_in_item, ih_item_len
690 (item_head(tbS0, tb->item_pos)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691
Dave Jones416e2ab2014-02-17 16:21:24 -0500692 leaf_shift_right(tb, tb->rnum[0], n_shift);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 /* Calculate number of bytes which must remain in body after appending to R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500694 if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695 n_rem = 0;
696
697 {
698 int version;
Dave Jones416e2ab2014-02-17 16:21:24 -0500699 unsigned long temp_rem = n_rem;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400701 version = ih_version(item_head(tb->R[0], 0));
702 if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500703 temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700704 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400705 set_le_key_k_offset(version, leaf_key(tb->R[0], 0),
706 le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem);
707 set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
708 le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700709 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400710/* k_offset (leaf_key(tb->R[0],0)) += n_rem;
711 k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/
Dave Jones416e2ab2014-02-17 16:21:24 -0500712 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714 /* Append part of body into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400715 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400716 if (n_rem > tb->zeroes_num) {
717 r_zeroes_number = 0;
718 r_body = body + n_rem - tb->zeroes_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 } else {
720 r_body = body;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400721 r_zeroes_number = tb->zeroes_num - n_rem;
722 tb->zeroes_num -= r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dave Jones416e2ab2014-02-17 16:21:24 -0500725 leaf_paste_in_buffer(&bi, 0, n_shift,
726 tb->insert_size[0] - n_rem,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400727 r_body, r_zeroes_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400729 if (is_indirect_le_ih(item_head(tb->R[0], 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#if 0
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 RFALSE(n_rem,
732 "PAP-12160: paste more than one unformatted node pointer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733#endif
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400734 set_ih_free_space(item_head(tb->R[0], 0), 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700735 }
736 tb->insert_size[0] = n_rem;
737 if (!n_rem)
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400738 tb->pos_in_item++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700739 }
740 } else { /* pasted item in whole falls into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700742 struct item_head *pasted;
743
Dave Jones416e2ab2014-02-17 16:21:24 -0500744 ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700745 /* append item in R[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400746 if (tb->pos_in_item >= 0) {
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400747 buffer_info_init_right(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400748 leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item,
749 tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700750 }
751
752 /* paste new entry, if item is directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400753 pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
754 if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
755 leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
756 tb->pos_in_item, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -0500757 (struct reiserfs_de_head *) body,
758 body + DEH_SIZE, tb->insert_size[0]);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400759 if (!tb->pos_in_item) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400761 RFALSE(tb->item_pos - n + tb->rnum[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 "PAP-12165: directory item must be first item of node when pasting is in 0th position");
763
764 /* update delimiting keys */
Dave Jones416e2ab2014-02-17 16:21:24 -0500765 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766 }
767 }
768
769 if (is_indirect_le_ih(pasted))
770 set_ih_free_space(pasted, 0);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400771 tb->zeroes_num = tb->insert_size[0] = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700772 }
773 } else { /* new item doesn't fall into R[0] */
774
775 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
776 }
777 break;
778 default: /* cases d and t */
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400779 reiserfs_panic(tb->tb_sb, "PAP-12175",
780 "rnum > 0: unexpected mode: %s(%d)",
Dave Jones416e2ab2014-02-17 16:21:24 -0500781 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700782 }
783
784 }
785
786 /* tb->rnum[0] > 0 */
787 RFALSE(tb->blknum[0] > 3,
Dave Jones416e2ab2014-02-17 16:21:24 -0500788 "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700789 RFALSE(tb->blknum[0] < 0,
Dave Jones416e2ab2014-02-17 16:21:24 -0500790 "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400792 /*
793 * if while adding to a node we discover that it is possible to split
794 * it in two, and merge the left part into the left neighbor and the
795 * right part into the right neighbor, eliminating the node
796 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700797 if (tb->blknum[0] == 0) { /* node S[0] is empty now */
798
799 RFALSE(!tb->lnum[0] || !tb->rnum[0],
800 "PAP-12190: lnum and rnum must not be zero");
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400801 /*
802 * if insertion was done before 0-th position in R[0], right
803 * delimiting key of the tb->L[0]'s and left delimiting key are
804 * not set correctly
805 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 if (tb->CFL[0]) {
807 if (!tb->CFR[0])
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400808 reiserfs_panic(tb->tb_sb, "vs-12195",
809 "CFR not initialized");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400810 copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
811 internal_key(tb->CFR[0], tb->rkey[0]));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700812 do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
813 }
814
815 reiserfs_invalidate_buffer(tb, tbS0);
816 return 0;
817 }
818
819 /* Fill new nodes that appear in place of S[0] */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700820 for (i = tb->blknum[0] - 2; i >= 0; i--) {
821
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400822 RFALSE(!tb->snum[i],
823 "PAP-12200: snum[%d] == %d. Must be > 0", i,
824 tb->snum[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700825
826 /* here we shift from S to S_new nodes */
827
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400828 tb->S_new[i] = get_FEB(tb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829
830 /* initialized block type and tree level */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400831 set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700832
833 n = B_NR_ITEMS(tbS0);
834
835 switch (flag) {
836 case M_INSERT: /* insert item */
837
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400838 if (n - tb->snum[i] < tb->item_pos) { /* new item or it's part falls to first new node S_new[i] */
839 if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { /* part of new item falls into S_new[i] */
840 int old_key_comp, old_len, r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700841 const char *r_body;
842 int version;
843
844 /* Move snum[i]-1 items from S[0] to S_new[i] */
845 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400846 tb->snum[i] - 1, -1,
847 tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700848 /* Remember key component and item length */
849 version = ih_version(ih);
850 old_key_comp = le_ih_k_offset(ih);
851 old_len = ih_item_len(ih);
852
853 /* Calculate key component and item length to insert into S_new[i] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500854 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400855 ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400857 put_ih_item_len(ih, tb->sbytes[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700858
859 /* Insert part of the item into S_new[i] before 0-th item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400860 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400862 if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
863 r_zeroes_number = 0;
864 r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700865 } else {
866 r_body = body;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400867 r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]);
868 tb->zeroes_num -= r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700869 }
870
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400871 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700872
873 /* Calculate key component and item length to insert into S[i] */
874 set_le_ih_k_offset(ih, old_key_comp);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400875 put_ih_item_len(ih, old_len - tb->sbytes[i]);
876 tb->insert_size[0] -= tb->sbytes[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877 } else { /* whole new item falls into S_new[i] */
878
879 /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
880 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400881 tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882
883 /* Insert new item into S_new[i] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400884 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
885 leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1,
886 ih, body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400888 tb->zeroes_num = tb->insert_size[0] = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 }
890 }
891
892 else { /* new item or it part don't falls into S_new[i] */
893
894 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400895 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700896 }
897 break;
898
899 case M_PASTE: /* append item */
900
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400901 if (n - tb->snum[i] <= tb->item_pos) { /* pasted item or part if it falls to S_new[i] */
902 if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) { /* we must shift part of the appended item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700903 struct item_head *aux_ih;
904
905 RFALSE(ih, "PAP-12210: ih must be 0");
906
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400907 aux_ih = item_head(tbS0, tb->item_pos);
Jeff Mahoney1d965fe2009-06-17 16:26:29 -0700908 if (is_direntry_le_ih(aux_ih)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700909 /* we append to directory item */
910
911 int entry_count;
912
Dave Jones416e2ab2014-02-17 16:21:24 -0500913 entry_count = ih_entry_count(aux_ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400915 if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700916 /* new directory entry falls into S_new[i] */
917
Dave Jones416e2ab2014-02-17 16:21:24 -0500918 RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0");
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400919 RFALSE(tb->sbytes[i] - 1 >= entry_count,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 "PAP-12220: there are no so much entries (%d), only %d",
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400921 tb->sbytes[i] - 1, entry_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700922
923 /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400924 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700925 /* Paste given directory entry to directory item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400926 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
927 leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1,
928 tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 /* paste new directory entry */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400930 leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -0500931 (struct reiserfs_de_head *) body,
932 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700933 tb->insert_size[0] = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400934 tb->pos_in_item++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935 } else { /* new directory entry doesn't fall into S_new[i] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400936 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700937 }
938 } else { /* regular object */
939
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400940 int n_shift, n_rem, r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700941 const char *r_body;
942
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400943 RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944 "PAP-12225: item too short or insert_size <= 0");
945
946 /* Calculate number of bytes which must be shifted from appended item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400947 n_shift = tb->sbytes[i] - tb->insert_size[0];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700948 if (n_shift < 0)
949 n_shift = 0;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400950 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700951
952 /* Calculate number of bytes which must remain in body after append to S_new[i] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400953 n_rem = tb->insert_size[0] - tb->sbytes[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 if (n_rem < 0)
955 n_rem = 0;
956 /* Append part of body into S_new[0] */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400957 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
958 if (n_rem > tb->zeroes_num) {
959 r_zeroes_number = 0;
960 r_body = body + n_rem - tb->zeroes_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700961 } else {
962 r_body = body;
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400963 r_zeroes_number = tb->zeroes_num - n_rem;
964 tb->zeroes_num -= r_zeroes_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965 }
966
Dave Jones416e2ab2014-02-17 16:21:24 -0500967 leaf_paste_in_buffer(&bi, 0, n_shift,
968 tb->insert_size[0] - n_rem,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400969 r_body, r_zeroes_number);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700970 {
971 struct item_head *tmp;
972
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400973 tmp = item_head(tb->S_new[i], 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700974 if (is_indirect_le_ih
975 (tmp)) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500976 set_ih_free_space(tmp, 0);
977 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700978 } else {
Dave Jones416e2ab2014-02-17 16:21:24 -0500979 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700980 }
981 }
982
983 tb->insert_size[0] = n_rem;
984 if (!n_rem)
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400985 tb->pos_in_item++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700986 }
987 } else
988 /* item falls wholly into S_new[i] */
989 {
Harvey Harrison8acc5702008-04-28 02:16:21 -0700990 int leaf_mi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700991 struct item_head *pasted;
992
993#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400994 struct item_head *ih_check = item_head(tbS0, tb->item_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700995
Harvey Harrison8acc5702008-04-28 02:16:21 -0700996 if (!is_direntry_le_ih(ih_check)
Jeff Mahoneyb49fb112014-04-23 10:00:46 -0400997 && (tb->pos_in_item != ih_item_len(ih_check)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 || tb->insert_size[0] <= 0))
999 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001000 "PAP-12235",
1001 "pos_in_item "
1002 "must be equal "
1003 "to ih_item_len");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001004#endif /* CONFIG_REISERFS_CHECK */
1005
Dave Jones416e2ab2014-02-17 16:21:24 -05001006 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001007 tb, tb->snum[i],
1008 tb->sbytes[i],
1009 tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001010
Harvey Harrison8acc5702008-04-28 02:16:21 -07001011 RFALSE(leaf_mi,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 "PAP-12240: unexpected value returned by leaf_move_items (%d)",
Harvey Harrison8acc5702008-04-28 02:16:21 -07001013 leaf_mi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001014
1015 /* paste into item */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001016 buffer_info_init_bh(tb, &bi, tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017 leaf_paste_in_buffer(&bi,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001018 tb->item_pos - n + tb->snum[i],
1019 tb->pos_in_item,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 tb->insert_size[0],
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001021 body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001022
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001023 pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001024 if (is_direntry_le_ih(pasted)) {
Jeff Mahoneyeba00302009-03-30 14:02:18 -04001025 leaf_paste_entries(&bi,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001026 tb->item_pos - n + tb->snum[i],
1027 tb->pos_in_item, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -05001028 (struct reiserfs_de_head *)body,
1029 body + DEH_SIZE,
1030 tb->insert_size[0]
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001031 );
1032 }
1033
1034 /* if we paste to indirect item update ih_free_space */
1035 if (is_indirect_le_ih(pasted))
1036 set_ih_free_space(pasted, 0);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001037 tb->zeroes_num = tb->insert_size[0] = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001038 }
1039 }
1040
1041 else { /* pasted item doesn't fall into S_new[i] */
1042
1043 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001044 tb->snum[i], tb->sbytes[i], tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001045 }
1046 break;
1047 default: /* cases d and t */
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001048 reiserfs_panic(tb->tb_sb, "PAP-12245",
1049 "blknum > 2: unexpected mode: %s(%d)",
Dave Jones416e2ab2014-02-17 16:21:24 -05001050 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001051 }
1052
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001053 memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE);
1054 insert_ptr[i] = tb->S_new[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001056 RFALSE(!buffer_journaled(tb->S_new[i])
1057 || buffer_journal_dirty(tb->S_new[i])
1058 || buffer_dirty(tb->S_new[i]),
1059 "PAP-12247: S_new[%d] : (%b)",
1060 i, tb->S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001061 }
1062
1063 /* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the
1064 affected item which remains in S */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001065 if (0 <= tb->item_pos && tb->item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001066
1067 switch (flag) {
1068 case M_INSERT: /* insert item into S[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001069 buffer_info_init_tbS0(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001070 leaf_insert_into_buf(&bi, tb->item_pos, ih,
1071 body, tb->zeroes_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073 /* If we insert the first key change the delimiting key */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001074 if (tb->item_pos == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001075 if (tb->CFL[0]) /* can be 0 in reiserfsck */
Dave Jones416e2ab2014-02-17 16:21:24 -05001076 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001080 case M_PASTE:{ /* append item in S[0] */
1081 struct item_head *pasted;
1082
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001083 pasted = item_head(tbS0, tb->item_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084 /* when directory, may be new entry already pasted */
1085 if (is_direntry_le_ih(pasted)) {
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001086 if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087
1088 RFALSE(!tb->insert_size[0],
1089 "PAP-12260: insert_size is 0 already");
1090
1091 /* prepare space */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001092 buffer_info_init_tbS0(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001093 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
Dave Jones416e2ab2014-02-17 16:21:24 -05001094 tb->insert_size[0], body,
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001095 tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096
1097 /* paste entry */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001098 leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1,
Dave Jones416e2ab2014-02-17 16:21:24 -05001099 (struct reiserfs_de_head *)body,
1100 body + DEH_SIZE,
1101 tb->insert_size[0]);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001102 if (!tb->item_pos && !tb->pos_in_item) {
Dave Jones416e2ab2014-02-17 16:21:24 -05001103 RFALSE(!tb->CFL[0] || !tb->L[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001104 "PAP-12270: CFL[0]/L[0] must be specified");
Dave Jones416e2ab2014-02-17 16:21:24 -05001105 if (tb->CFL[0])
1106 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001107 }
1108 tb->insert_size[0] = 0;
1109 }
1110 } else { /* regular object */
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001111 if (tb->pos_in_item == ih_item_len(pasted)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001112
1113 RFALSE(tb->insert_size[0] <= 0,
1114 "PAP-12275: insert size must not be %d",
1115 tb->insert_size[0]);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001116 buffer_info_init_tbS0(tb, &bi);
Jeff Mahoneyb49fb112014-04-23 10:00:46 -04001117 leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item,
1118 tb->insert_size[0], body, tb->zeroes_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001119
1120 if (is_indirect_le_ih(pasted)) {
1121#if 0
1122 RFALSE(tb->
1123 insert_size[0] !=
1124 UNFM_P_SIZE,
1125 "PAP-12280: insert_size for indirect item must be %d, not %d",
1126 UNFM_P_SIZE,
1127 tb->
1128 insert_size[0]);
1129#endif
Dave Jones416e2ab2014-02-17 16:21:24 -05001130 set_ih_free_space(pasted, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001131 }
1132 tb->insert_size[0] = 0;
1133 }
1134#ifdef CONFIG_REISERFS_CHECK
1135 else {
1136 if (tb->insert_size[0]) {
1137 print_cur_tb("12285");
Dave Jones416e2ab2014-02-17 16:21:24 -05001138 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001139 "PAP-12285",
1140 "insert_size "
1141 "must be 0 "
1142 "(%d)",
1143 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144 }
1145 }
1146#endif /* CONFIG_REISERFS_CHECK */
1147
1148 }
1149 } /* case M_PASTE: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001152#ifdef CONFIG_REISERFS_CHECK
1153 if (flag == M_PASTE && tb->insert_size[0]) {
1154 print_cur_tb("12290");
1155 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001156 "PAP-12290", "insert_size is still not 0 (%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001157 tb->insert_size[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001159#endif
1160
1161 /* Leaf level of the tree is balanced (end of balance_leaf) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165/* Make empty node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001166void make_empty_node(struct buffer_info *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 struct block_head *blkh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001170 RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172 blkh = B_BLK_HEAD(bi->bi_bh);
1173 set_blkh_nr_item(blkh, 0);
1174 set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001176 if (bi->bi_parent)
1177 B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180/* Get first empty buffer */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001181struct buffer_head *get_FEB(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 int i;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001184 struct buffer_info bi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186 for (i = 0; i < MAX_FEB_SIZE; i++)
Al Viro9dce07f2008-03-29 03:07:28 +00001187 if (tb->FEB[i] != NULL)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001190 if (i == MAX_FEB_SIZE)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001191 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001193 buffer_info_init_bh(tb, &bi, tb->FEB[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001194 make_empty_node(&bi);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001195 set_buffer_uptodate(tb->FEB[i]);
1196 tb->used[i] = tb->FEB[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 tb->FEB[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001199 return tb->used[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Jeff Mahoney098297b2014-04-23 10:00:36 -04001202/* This is now used because reiserfs_free_block has to be able to schedule. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001203static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001207 if (buffer_dirty(bh))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001208 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1209 "called with dirty buffer");
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001210 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211 if (!tb->thrown[i]) {
1212 tb->thrown[i] = bh;
1213 get_bh(bh); /* free_thrown puts this */
1214 return;
1215 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001216 reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1217 "too many thrown buffers");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001220static void free_thrown(struct tree_balance *tb)
1221{
1222 int i;
1223 b_blocknr_t blocknr;
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001224 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001225 if (tb->thrown[i]) {
1226 blocknr = tb->thrown[i]->b_blocknr;
1227 if (buffer_dirty(tb->thrown[i]))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001228 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1229 "called with dirty buffer %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001230 blocknr);
1231 brelse(tb->thrown[i]); /* incremented in store_thrown */
1232 reiserfs_free_block(tb->transaction_handle, NULL,
1233 blocknr, 0);
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001238void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001240 struct block_head *blkh;
1241 blkh = B_BLK_HEAD(bh);
1242 set_blkh_level(blkh, FREE_LEVEL);
1243 set_blkh_nr_item(blkh, 0);
1244
1245 clear_buffer_dirty(bh);
1246 store_thrown(tb, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249/* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001250void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1251 struct buffer_head *src, int n_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001254 RFALSE(dest == NULL || src == NULL,
1255 "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
1256 src, dest);
1257 RFALSE(!B_IS_KEYS_LEVEL(dest),
1258 "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
1259 dest);
1260 RFALSE(n_dest < 0 || n_src < 0,
1261 "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
1262 RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
1263 "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
1264 n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266 if (B_IS_ITEMS_LEVEL(src))
1267 /* source buffer contains leaf node */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001268 memcpy(internal_key(dest, n_dest), item_head(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001269 KEY_SIZE);
1270 else
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001271 memcpy(internal_key(dest, n_dest), internal_key(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 KEY_SIZE);
1273
1274 do_balance_mark_internal_dirty(tb, dest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277int get_left_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001279 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Al Viro9dce07f2008-03-29 03:07:28 +00001281 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001282 "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
1283 h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285 if (Sh_position == 0)
1286 return B_NR_ITEMS(tb->FL[h]);
1287 else
1288 return Sh_position - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291int get_right_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001293 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Al Viro9dce07f2008-03-29 03:07:28 +00001295 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001296 "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
1297 h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001299 if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1300 return 0;
1301 else
1302 return Sh_position + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305#ifdef CONFIG_REISERFS_CHECK
1306
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001307int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
1308static void check_internal_node(struct super_block *s, struct buffer_head *bh,
1309 char *mes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001311 struct disk_child *dc;
1312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001314 RFALSE(!bh, "PAP-12336: bh == 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001316 if (!bh || !B_IS_IN_TREE(bh))
1317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001319 RFALSE(!buffer_dirty(bh) &&
1320 !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
1321 "PAP-12337: buffer (%b) must be dirty", bh);
1322 dc = B_N_CHILD(bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001324 for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
1325 if (!is_reusable(s, dc_block_number(dc), 1)) {
1326 print_cur_tb(mes);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001327 reiserfs_panic(s, "PAP-12338",
1328 "invalid child pointer %y in %b",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001329 dc, bh);
1330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001332}
1333
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001334static int locked_or_not_in_tree(struct tree_balance *tb,
1335 struct buffer_head *bh, char *which)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336{
1337 if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
1338 !B_IS_IN_TREE(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001339 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001340 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001345static int check_before_balancing(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001347 int retval = 0;
1348
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001349 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001350 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1351 "occurred based on cur_tb not being null at "
1352 "this point in code. do_balance cannot properly "
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001353 "handle concurrent tree accesses on a same "
1354 "mount point.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001355 }
1356
Jeff Mahoney098297b2014-04-23 10:00:36 -04001357 /*
1358 * double check that buffers that we will modify are unlocked.
1359 * (fix_nodes should already have prepped all of these for us).
1360 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001361 if (tb->lnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001362 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1363 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1364 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001365 check_leaf(tb->L[0]);
1366 }
1367 if (tb->rnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001368 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1369 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1370 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001371 check_leaf(tb->R[0]);
1372 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001373 retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1374 "S[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001375 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1376
1377 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001380static void check_after_balance_leaf(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001382 if (tb->lnum[0]) {
1383 if (B_FREE_SPACE(tb->L[0]) !=
1384 MAX_CHILD_SIZE(tb->L[0]) -
1385 dc_size(B_N_CHILD
1386 (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1387 print_cur_tb("12221");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001388 reiserfs_panic(tb->tb_sb, "PAP-12355",
1389 "shift to left was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001390 }
1391 }
1392 if (tb->rnum[0]) {
1393 if (B_FREE_SPACE(tb->R[0]) !=
1394 MAX_CHILD_SIZE(tb->R[0]) -
1395 dc_size(B_N_CHILD
1396 (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1397 print_cur_tb("12222");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001398 reiserfs_panic(tb->tb_sb, "PAP-12360",
1399 "shift to right was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001400 }
1401 }
1402 if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1403 (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1404 (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1405 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1406 PATH_H_POSITION(tb->tb_path, 1)))))) {
1407 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1408 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1409 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1410 PATH_H_POSITION(tb->tb_path,
1411 1))));
1412 print_cur_tb("12223");
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001413 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001414 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1415 "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
1416 left,
1417 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1418 PATH_H_PBUFFER(tb->tb_path, 1),
1419 PATH_H_POSITION(tb->tb_path, 1),
1420 dc_size(B_N_CHILD
1421 (PATH_H_PBUFFER(tb->tb_path, 1),
1422 PATH_H_POSITION(tb->tb_path, 1))),
1423 right);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001424 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001425 }
1426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428static void check_leaf_level(struct tree_balance *tb)
1429{
1430 check_leaf(tb->L[0]);
1431 check_leaf(tb->R[0]);
1432 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1433}
1434
1435static void check_internal_levels(struct tree_balance *tb)
1436{
1437 int h;
1438
1439 /* check all internal nodes */
1440 for (h = 1; tb->insert_size[h]; h++) {
1441 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1442 "BAD BUFFER ON PATH");
1443 if (tb->lnum[h])
1444 check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1445 if (tb->rnum[h])
1446 check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449}
1450
1451#endif
1452
Jeff Mahoney098297b2014-04-23 10:00:36 -04001453/*
1454 * Now we have all of the buffers that must be used in balancing of
1455 * the tree. We rely on the assumption that schedule() will not occur
1456 * while do_balance works. ( Only interrupt handlers are acceptable.)
1457 * We balance the tree according to the analysis made before this,
1458 * using buffers already obtained. For SMP support it will someday be
1459 * necessary to add ordered locking of tb.
1460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jeff Mahoney098297b2014-04-23 10:00:36 -04001462/*
1463 * Some interesting rules of balancing:
1464 * we delete a maximum of two nodes per level per balancing: we never
1465 * delete R, when we delete two of three nodes L, S, R then we move
1466 * them into R.
1467 *
1468 * we only delete L if we are deleting two nodes, if we delete only
1469 * one node we delete S
1470 *
1471 * if we shift leaves then we shift as much as we can: this is a
1472 * deliberate policy of extremism in node packing which results in
1473 * higher average utilization after repeated random balance operations
1474 * at the cost of more memory copies and more balancing as a result of
1475 * small insertions to full nodes.
1476 *
1477 * if we shift internal nodes we try to evenly balance the node
1478 * utilization, with consequent less balancing at the cost of lower
1479 * utilization.
1480 *
1481 * one could argue that the policy for directories in leaves should be
1482 * that of internal nodes, but we will wait until another day to
1483 * evaluate this.... It would be nice to someday measure and prove
1484 * these assumptions as to what is optimal....
1485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001487static inline void do_balance_starts(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Jeff Mahoney098297b2014-04-23 10:00:36 -04001489 /* use print_cur_tb() to see initial state of struct tree_balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001491 /* store_print_tb (tb); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001493 /* do not delete, just comment it out */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001494 /*
1495 print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1496 tb->tb_path->pos_in_item, tb, "check");
1497 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001498 RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499#ifdef CONFIG_REISERFS_CHECK
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001500 REISERFS_SB(tb->tb_sb)->cur_tb = tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501#endif
1502}
1503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001504static inline void do_balance_completed(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001508 check_leaf_level(tb);
1509 check_internal_levels(tb);
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001510 REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511#endif
1512
Jeff Mahoney098297b2014-04-23 10:00:36 -04001513 /*
1514 * reiserfs_free_block is no longer schedule safe. So, we need to
1515 * put the buffers we want freed on the thrown list during do_balance,
1516 * and then free them now
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 REISERFS_SB(tb->tb_sb)->s_do_balance++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001521 /* release all nodes hold to perform the balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 unfix_nodes(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001524 free_thrown(tb);
1525}
1526
Jeff Mahoney098297b2014-04-23 10:00:36 -04001527/*
1528 * do_balance - balance the tree
1529 *
1530 * @tb: tree_balance structure
1531 * @ih: item header of inserted item
1532 * @body: body of inserted item or bytes to paste
1533 * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste
1534 *
1535 * Cut means delete part of an item (includes removing an entry from a
1536 * directory).
1537 *
1538 * Delete means delete whole item.
1539 *
1540 * Insert means add a new item into the tree.
1541 *
1542 * Paste means to append to the end of an existing file or to
1543 * insert a directory entry.
1544 */
1545void do_balance(struct tree_balance *tb, struct item_head *ih,
1546 const char *body, int flag)
1547{
1548 int child_pos; /* position of a child node in its parent */
1549 int h; /* level of the tree being processed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001550
Jeff Mahoney098297b2014-04-23 10:00:36 -04001551 /*
1552 * in our processing of one level we sometimes determine what
1553 * must be inserted into the next higher level. This insertion
1554 * consists of a key or two keys and their corresponding
1555 * pointers
1556 */
1557 struct item_head insert_key[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001558
Jeff Mahoney098297b2014-04-23 10:00:36 -04001559 /* inserted node-ptrs for the next level */
1560 struct buffer_head *insert_ptr[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001561
1562 tb->tb_mode = flag;
1563 tb->need_balance_dirty = 0;
1564
1565 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001566 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1567 "changed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001568 }
1569 /* if we have no real work to do */
1570 if (!tb->insert_size[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001571 reiserfs_warning(tb->tb_sb, "PAP-12350",
1572 "insert_size == 0, mode == %c", flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001573 unfix_nodes(tb);
1574 return;
1575 }
1576
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001577 atomic_inc(&fs_generation(tb->tb_sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001578 do_balance_starts(tb);
1579
Jeff Mahoney098297b2014-04-23 10:00:36 -04001580 /*
1581 * balance_leaf returns 0 except if combining L R and S into
1582 * one node. see balance_internal() for explanation of this
1583 * line of code.
1584 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001585 child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1586 balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001589 check_after_balance_leaf(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590#endif
1591
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001592 /* Balance internal level of the tree. */
1593 for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
1594 child_pos =
1595 balance_internal(tb, h, child_pos, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001597 do_balance_completed(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599}