blob: bbc282b7007cc945b5d101ff377d5fb7eb1c34e5 [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);
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400313 /* index into the array of item headers in S[0] of the affected item */
314 int item_pos = PATH_LAST_POSITION(tb->tb_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700315 struct buffer_info bi;
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400316 /* new nodes allocated to hold what could not fit into S */
317 struct buffer_head *S_new[2];
318 /*
319 * number of items that will be placed into S_new
320 * (includes partially shifted items)
321 */
322 int snum[2];
323 /*
324 * if an item is partially shifted into S_new then if it is a
325 * directory item it is the number of entries from the item that
326 * are shifted into S_new else it is the number of bytes from
327 * the item that are shifted into S_new
328 */
329 int sbytes[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700330 int n, i;
331 int ret_val;
332 int pos_in_item;
333 int zeros_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 PROC_INFO_INC(tb->tb_sb, balance_at[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700337 /* Make balance in case insert_size[0] < 0 */
338 if (tb->insert_size[0] < 0)
339 return balance_leaf_when_delete(tb, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700341 zeros_num = 0;
Al Viro9dce07f2008-03-29 03:07:28 +0000342 if (flag == M_INSERT && !body)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700343 zeros_num = ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700345 pos_in_item = tb->tb_path->pos_in_item;
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400346 /*
347 * for indirect item pos_in_item is measured in unformatted node
348 * pointers. Recalculate to bytes
349 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700350 if (flag != M_INSERT
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400351 && is_indirect_le_ih(item_head(tbS0, item_pos)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700352 pos_in_item *= UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 if (tb->lnum[0] > 0) {
355 /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
356 if (item_pos < tb->lnum[0]) {
357 /* new item or it part falls to L[0], shift it too */
358 n = B_NR_ITEMS(tb->L[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700360 switch (flag) {
361 case M_INSERT: /* insert item into L[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dave Jones416e2ab2014-02-17 16:21:24 -0500363 if (item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 /* part of new item falls into L[0] */
365 int new_item_len;
366 int version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dave Jones416e2ab2014-02-17 16:21:24 -0500368 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700370 /* Calculate item length to insert to S[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500371 new_item_len = ih_item_len(ih) - tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 /* Calculate and check item length to insert to L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500373 put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 RFALSE(ih_item_len(ih) <= 0,
376 "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
377 ih_item_len(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379 /* Insert new item into L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400380 buffer_info_init_left(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 leaf_insert_into_buf(&bi,
Dave Jones416e2ab2014-02-17 16:21:24 -0500382 n + item_pos - ret_val, ih, body,
383 zeros_num > ih_item_len(ih) ? ih_item_len(ih) : zeros_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 version = ih_version(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 /* Calculate key component, item length and body to insert into S[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500388 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
389 (tb-> lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT : 0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 put_ih_item_len(ih, new_item_len);
392 if (tb->lbytes > zeros_num) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500393 body += (tb->lbytes - zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 zeros_num = 0;
395 } else
396 zeros_num -= tb->lbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398 RFALSE(ih_item_len(ih) <= 0,
399 "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
400 ih_item_len(ih));
401 } else {
402 /* new item in whole falls into L[0] */
403 /* Shift lnum[0]-1 items to L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500404 ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700405 /* Insert new item into L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400406 buffer_info_init_left(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -0500407 leaf_insert_into_buf(&bi, n + item_pos - ret_val, ih, body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408 tb->insert_size[0] = 0;
409 zeros_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 case M_PASTE: /* append item in L[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Dave Jones416e2ab2014-02-17 16:21:24 -0500415 if (item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700416 /* we must shift the part of the appended item */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400417 if (is_direntry_le_ih(item_head(tbS0, item_pos))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700418
419 RFALSE(zeros_num,
420 "PAP-12090: invalid parameter in case of a directory");
421 /* directory item */
422 if (tb->lbytes > pos_in_item) {
423 /* new directory entry falls into L[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500424 struct item_head *pasted;
425 int l_pos_in_item = pos_in_item;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426
427 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500428 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1);
429 if (ret_val && !item_pos) {
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400430 pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
431 l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700432 }
433
434 /* Append given directory entry to directory item */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400435 buffer_info_init_left(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -0500436 leaf_paste_in_buffer(&bi, n + item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437
438 /* previous string prepared space for pasting new entry, following string pastes this entry */
439
440 /* when we have merge directory item, pos_in_item has been changed too */
441
442 /* paste new directory entry. 1 is entry number */
Dave Jones416e2ab2014-02-17 16:21:24 -0500443 leaf_paste_entries(&bi, n + item_pos - ret_val, l_pos_in_item,
444 1, (struct reiserfs_de_head *) body,
445 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446 tb->insert_size[0] = 0;
447 } else {
448 /* new directory item doesn't fall into L[0] */
449 /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500450 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451 }
452 /* Calculate new position to append in item body */
453 pos_in_item -= tb->lbytes;
454 } else {
455 /* regular object */
Dave Jones416e2ab2014-02-17 16:21:24 -0500456 RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes);
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400457 RFALSE(pos_in_item != ih_item_len(item_head(tbS0, item_pos)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458 "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400459 ih_item_len(item_head(tbS0, item_pos)),pos_in_item);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460
461 if (tb->lbytes >= pos_in_item) {
462 /* appended item will be in L[0] in whole */
463 int l_n;
464
465 /* this bytes number must be appended to the last item of L[h] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500466 l_n = tb->lbytes - pos_in_item;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467
468 /* Calculate new insert_size[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500469 tb->insert_size[0] -= l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470
Dave Jones416e2ab2014-02-17 16:21:24 -0500471 RFALSE(tb->insert_size[0] <= 0,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500473 tb->insert_size[0]);
474 ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400475 (item_head(tbS0, item_pos)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700476 /* Append to body of item in L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400477 buffer_info_init_left(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 leaf_paste_in_buffer
Dave Jones416e2ab2014-02-17 16:21:24 -0500479 (&bi, n + item_pos - ret_val, ih_item_len
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400480 (item_head(tb->L[0], n + item_pos - ret_val)),
Dave Jones416e2ab2014-02-17 16:21:24 -0500481 l_n, body,
482 zeros_num > l_n ? l_n : zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
484 {
485 int version;
Dave Jones416e2ab2014-02-17 16:21:24 -0500486 int temp_l = l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400488 RFALSE(ih_item_len(item_head(tbS0, 0)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700489 "PAP-12106: item length must be 0");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400490 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key
Dave Jones416e2ab2014-02-17 16:21:24 -0500491 (tb->L[0], n + item_pos - ret_val)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 "PAP-12107: items must be of the same file");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400493 if (is_indirect_le_ih(item_head(tb->L[0], n + item_pos - ret_val))) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500494 temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700495 }
496 /* update key of first item in S0 */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400497 version = ih_version(item_head(tbS0, 0));
498 set_le_key_k_offset(version, leaf_key(tbS0, 0),
499 le_key_k_offset(version,leaf_key(tbS0, 0)) + temp_l);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700500 /* update left delimiting key */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400501 set_le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0]),
502 le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0])) + temp_l);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700503 }
504
505 /* Calculate new body, position in item and insert_size[0] */
506 if (l_n > zeros_num) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500507 body += (l_n - zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 zeros_num = 0;
509 } else
Dave Jones416e2ab2014-02-17 16:21:24 -0500510 zeros_num -= l_n;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 pos_in_item = 0;
512
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400513 RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1))
514 || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)
515 || !op_is_left_mergeable(internal_key(tb->CFL[0], tb->lkey[0]), tbS0->b_size),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700516 "PAP-12120: item must be merge-able with left neighboring item");
517 } else { /* only part of the appended item will be in L[0] */
518
519 /* Calculate position in item for append in S[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500520 pos_in_item -= tb->lbytes;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700521
Dave Jones416e2ab2014-02-17 16:21:24 -0500522 RFALSE(pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", pos_in_item);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523
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 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700526 }
527 }
528 } else { /* appended item will be in L[0] in whole */
529
530 struct item_head *pasted;
531
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400532 if (!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 -0700533 /* then increment pos_in_item by the size of the last item in L[0] */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400534 pasted = item_head(tb->L[0], n - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535 if (is_direntry_le_ih(pasted))
Dave Jones416e2ab2014-02-17 16:21:24 -0500536 pos_in_item += ih_entry_count(pasted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700537 else
Dave Jones416e2ab2014-02-17 16:21:24 -0500538 pos_in_item += ih_item_len(pasted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539 }
540
541 /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500542 ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700543 /* Append to body of item in L[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400544 buffer_info_init_left(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -0500545 leaf_paste_in_buffer(&bi, n + item_pos - ret_val,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 pos_in_item,
547 tb->insert_size[0],
548 body, zeros_num);
549
550 /* if appended item is directory, paste entry */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400551 pasted = item_head(tb->L[0], n + item_pos - ret_val);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700552 if (is_direntry_le_ih(pasted))
Dave Jones416e2ab2014-02-17 16:21:24 -0500553 leaf_paste_entries(&bi, n + item_pos - ret_val,
554 pos_in_item, 1,
555 (struct reiserfs_de_head *) body,
556 body + DEH_SIZE,
557 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700558 /* if appended item is indirect item, put unformatted node into un list */
559 if (is_indirect_le_ih(pasted))
560 set_ih_free_space(pasted, 0);
561 tb->insert_size[0] = 0;
562 zeros_num = 0;
563 }
564 break;
565 default: /* cases d and t */
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400566 reiserfs_panic(tb->tb_sb, "PAP-12130",
567 "lnum > 0: unexpected mode: "
568 " %s(%d)",
Dave Jones416e2ab2014-02-17 16:21:24 -0500569 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571 } else {
572 /* new item doesn't fall into L[0] */
573 leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700577 /* tb->lnum[0] > 0 */
578 /* Calculate new item position */
579 item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700581 if (tb->rnum[0] > 0) {
582 /* shift rnum[0] items from S[0] to the right neighbor R[0] */
583 n = B_NR_ITEMS(tbS0);
584 switch (flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700586 case M_INSERT: /* insert item */
587 if (n - tb->rnum[0] < item_pos) { /* new item or its part falls to R[0] */
588 if (item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500589 loff_t old_key_comp, old_len, r_zeros_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700590 const char *r_body;
591 int version;
592 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Jones416e2ab2014-02-17 16:21:24 -0500594 leaf_shift_right(tb, tb->rnum[0] - 1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 version = ih_version(ih);
597 /* Remember key component and item length */
598 old_key_comp = le_ih_k_offset(ih);
599 old_len = ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601 /* Calculate key component and item length to insert into R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500602 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 -0700603 set_le_ih_k_offset(ih, offset);
604 put_ih_item_len(ih, tb->rbytes);
605 /* Insert part of the item into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400606 buffer_info_init_right(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607 if ((old_len - tb->rbytes) > zeros_num) {
608 r_zeros_number = 0;
Dave Jones416e2ab2014-02-17 16:21:24 -0500609 r_body = body + (old_len - tb->rbytes) - zeros_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700610 } else {
611 r_body = body;
Dave Jones416e2ab2014-02-17 16:21:24 -0500612 r_zeros_number = zeros_num - (old_len - tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700613 zeros_num -= r_zeros_number;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700616 leaf_insert_into_buf(&bi, 0, ih, r_body,
617 r_zeros_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 /* Replace right delimiting key by first key in R[0] */
620 replace_key(tb, tb->CFR[0], tb->rkey[0],
621 tb->R[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 /* Calculate key component and item length to insert into S[0] */
624 set_le_ih_k_offset(ih, old_key_comp);
Dave Jones416e2ab2014-02-17 16:21:24 -0500625 put_ih_item_len(ih, old_len - tb->rbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627 tb->insert_size[0] -= tb->rbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700629 } else { /* whole new item falls into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700631 /* Shift rnum[0]-1 items to R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500632 ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 /* Insert new item into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400634 buffer_info_init_right(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -0500635 leaf_insert_into_buf(&bi, item_pos - n + tb->rnum[0] - 1,
636 ih, body, zeros_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 if (item_pos - n + tb->rnum[0] - 1 == 0) {
639 replace_key(tb, tb->CFR[0],
640 tb->rkey[0],
641 tb->R[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 }
644 zeros_num = tb->insert_size[0] = 0;
645 }
646 } else { /* new item or part of it doesn't fall into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700648 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700652 case M_PASTE: /* append item */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 if (n - tb->rnum[0] <= item_pos) { /* pasted item or part of it falls to R[0] */
655 if (item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400656 if (is_direntry_le_ih(item_head(tbS0, item_pos))) { /* we append to directory item */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 int entry_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700659 RFALSE(zeros_num,
660 "PAP-12145: invalid parameter in case of a directory");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400661 entry_count = ih_entry_count(item_head
Dave Jones416e2ab2014-02-17 16:21:24 -0500662 (tbS0, item_pos));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700663 if (entry_count - tb->rbytes <
664 pos_in_item)
665 /* new directory entry falls into R[0] */
666 {
667 int paste_entry_position;
668
Dave Jones416e2ab2014-02-17 16:21:24 -0500669 RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500671 tb->rbytes, entry_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 /* 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 -0500673 leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700674 /* Paste given directory entry to directory item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500675 paste_entry_position = pos_in_item - entry_count + tb->rbytes - 1;
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400676 buffer_info_init_right(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -0500677 leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 /* paste entry */
Dave Jones416e2ab2014-02-17 16:21:24 -0500679 leaf_paste_entries(&bi, 0, paste_entry_position, 1,
680 (struct reiserfs_de_head *) body,
681 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700682
Dave Jones416e2ab2014-02-17 16:21:24 -0500683 if (paste_entry_position == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700684 /* change delimiting keys */
Dave Jones416e2ab2014-02-17 16:21:24 -0500685 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700686 }
687
688 tb->insert_size[0] = 0;
689 pos_in_item++;
690 } else { /* new directory entry doesn't fall into R[0] */
691
Dave Jones416e2ab2014-02-17 16:21:24 -0500692 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 }
694 } else { /* regular object */
695
Dave Jones416e2ab2014-02-17 16:21:24 -0500696 int n_shift, n_rem, r_zeros_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697 const char *r_body;
698
699 /* Calculate number of bytes which must be shifted from appended item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500700 if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701 n_shift = 0;
702
Dave Jones416e2ab2014-02-17 16:21:24 -0500703 RFALSE(pos_in_item != ih_item_len
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400704 (item_head(tbS0, item_pos)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705 "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500706 pos_in_item, ih_item_len
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400707 (item_head(tbS0, item_pos)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708
Dave Jones416e2ab2014-02-17 16:21:24 -0500709 leaf_shift_right(tb, tb->rnum[0], n_shift);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 /* Calculate number of bytes which must remain in body after appending to R[0] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500711 if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700712 n_rem = 0;
713
714 {
715 int version;
Dave Jones416e2ab2014-02-17 16:21:24 -0500716 unsigned long temp_rem = n_rem;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400718 version = ih_version(item_head(tb->R[0], 0));
719 if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500720 temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700721 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400722 set_le_key_k_offset(version, leaf_key(tb->R[0], 0),
723 le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem);
724 set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
725 le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700726 }
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400727/* k_offset (leaf_key(tb->R[0],0)) += n_rem;
728 k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/
Dave Jones416e2ab2014-02-17 16:21:24 -0500729 do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 /* Append part of body into R[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400732 buffer_info_init_right(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733 if (n_rem > zeros_num) {
734 r_zeros_number = 0;
Dave Jones416e2ab2014-02-17 16:21:24 -0500735 r_body = body + n_rem - zeros_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 } else {
737 r_body = body;
Dave Jones416e2ab2014-02-17 16:21:24 -0500738 r_zeros_number = zeros_num - n_rem;
739 zeros_num -= r_zeros_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Jones416e2ab2014-02-17 16:21:24 -0500742 leaf_paste_in_buffer(&bi, 0, n_shift,
743 tb->insert_size[0] - n_rem,
744 r_body, r_zeros_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400746 if (is_indirect_le_ih(item_head(tb->R[0], 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747#if 0
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700748 RFALSE(n_rem,
749 "PAP-12160: paste more than one unformatted node pointer");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#endif
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400751 set_ih_free_space(item_head(tb->R[0], 0), 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700752 }
753 tb->insert_size[0] = n_rem;
754 if (!n_rem)
755 pos_in_item++;
756 }
757 } else { /* pasted item in whole falls into R[0] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 struct item_head *pasted;
760
Dave Jones416e2ab2014-02-17 16:21:24 -0500761 ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 /* append item in R[0] */
763 if (pos_in_item >= 0) {
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400764 buffer_info_init_right(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -0500765 leaf_paste_in_buffer(&bi, item_pos - n + tb->rnum[0], pos_in_item,
766 tb->insert_size[0], body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700767 }
768
769 /* paste new entry, if item is directory item */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400770 pasted = item_head(tb->R[0], item_pos - n + tb->rnum[0]);
Dave Jones416e2ab2014-02-17 16:21:24 -0500771 if (is_direntry_le_ih(pasted) && pos_in_item >= 0) {
772 leaf_paste_entries(&bi, item_pos - n + tb->rnum[0],
773 pos_in_item, 1,
774 (struct reiserfs_de_head *) body,
775 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700776 if (!pos_in_item) {
777
Dave Jones416e2ab2014-02-17 16:21:24 -0500778 RFALSE(item_pos - n + tb->rnum[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700779 "PAP-12165: directory item must be first item of node when pasting is in 0th position");
780
781 /* update delimiting keys */
Dave Jones416e2ab2014-02-17 16:21:24 -0500782 replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700783 }
784 }
785
786 if (is_indirect_le_ih(pasted))
787 set_ih_free_space(pasted, 0);
788 zeros_num = tb->insert_size[0] = 0;
789 }
790 } else { /* new item doesn't fall into R[0] */
791
792 leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
793 }
794 break;
795 default: /* cases d and t */
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400796 reiserfs_panic(tb->tb_sb, "PAP-12175",
797 "rnum > 0: unexpected mode: %s(%d)",
Dave Jones416e2ab2014-02-17 16:21:24 -0500798 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700799 }
800
801 }
802
803 /* tb->rnum[0] > 0 */
804 RFALSE(tb->blknum[0] > 3,
Dave Jones416e2ab2014-02-17 16:21:24 -0500805 "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 RFALSE(tb->blknum[0] < 0,
Dave Jones416e2ab2014-02-17 16:21:24 -0500807 "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700808
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400809 /*
810 * if while adding to a node we discover that it is possible to split
811 * it in two, and merge the left part into the left neighbor and the
812 * right part into the right neighbor, eliminating the node
813 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814 if (tb->blknum[0] == 0) { /* node S[0] is empty now */
815
816 RFALSE(!tb->lnum[0] || !tb->rnum[0],
817 "PAP-12190: lnum and rnum must not be zero");
Jeff Mahoney97fd4b92014-04-23 10:00:45 -0400818 /*
819 * if insertion was done before 0-th position in R[0], right
820 * delimiting key of the tb->L[0]'s and left delimiting key are
821 * not set correctly
822 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700823 if (tb->CFL[0]) {
824 if (!tb->CFR[0])
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400825 reiserfs_panic(tb->tb_sb, "vs-12195",
826 "CFR not initialized");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400827 copy_key(internal_key(tb->CFL[0], tb->lkey[0]),
828 internal_key(tb->CFR[0], tb->rkey[0]));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829 do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
830 }
831
832 reiserfs_invalidate_buffer(tb, tbS0);
833 return 0;
834 }
835
836 /* Fill new nodes that appear in place of S[0] */
837
838 /* I am told that this copying is because we need an array to enable
839 the looping code. -Hans */
840 snum[0] = tb->s1num, snum[1] = tb->s2num;
841 sbytes[0] = tb->s1bytes;
842 sbytes[1] = tb->s2bytes;
843 for (i = tb->blknum[0] - 2; i >= 0; i--) {
844
845 RFALSE(!snum[i], "PAP-12200: snum[%d] == %d. Must be > 0", i,
846 snum[i]);
847
848 /* here we shift from S to S_new nodes */
849
850 S_new[i] = get_FEB(tb);
851
852 /* initialized block type and tree level */
853 set_blkh_level(B_BLK_HEAD(S_new[i]), DISK_LEAF_NODE_LEVEL);
854
855 n = B_NR_ITEMS(tbS0);
856
857 switch (flag) {
858 case M_INSERT: /* insert item */
859
860 if (n - snum[i] < item_pos) { /* new item or it's part falls to first new node S_new[i] */
861 if (item_pos == n - snum[i] + 1 && sbytes[i] != -1) { /* part of new item falls into S_new[i] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500862 int old_key_comp, old_len, r_zeros_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700863 const char *r_body;
864 int version;
865
866 /* Move snum[i]-1 items from S[0] to S_new[i] */
867 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
868 snum[i] - 1, -1,
869 S_new[i]);
870 /* Remember key component and item length */
871 version = ih_version(ih);
872 old_key_comp = le_ih_k_offset(ih);
873 old_len = ih_item_len(ih);
874
875 /* Calculate key component and item length to insert into S_new[i] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500876 set_le_ih_k_offset(ih, le_ih_k_offset(ih) +
877 ((old_len - sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT : 0)));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700878
879 put_ih_item_len(ih, sbytes[i]);
880
881 /* Insert part of the item into S_new[i] before 0-th item */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400882 buffer_info_init_bh(tb, &bi, S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700883
884 if ((old_len - sbytes[i]) > zeros_num) {
885 r_zeros_number = 0;
Dave Jones416e2ab2014-02-17 16:21:24 -0500886 r_body = body + (old_len - sbytes[i]) - zeros_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887 } else {
888 r_body = body;
Dave Jones416e2ab2014-02-17 16:21:24 -0500889 r_zeros_number = zeros_num - (old_len - sbytes[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700890 zeros_num -= r_zeros_number;
891 }
892
Dave Jones416e2ab2014-02-17 16:21:24 -0500893 leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeros_number);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700894
895 /* Calculate key component and item length to insert into S[i] */
896 set_le_ih_k_offset(ih, old_key_comp);
Dave Jones416e2ab2014-02-17 16:21:24 -0500897 put_ih_item_len(ih, old_len - sbytes[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898 tb->insert_size[0] -= sbytes[i];
899 } else { /* whole new item falls into S_new[i] */
900
901 /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
902 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
Dave Jones416e2ab2014-02-17 16:21:24 -0500903 snum[i] - 1, sbytes[i], S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904
905 /* Insert new item into S_new[i] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400906 buffer_info_init_bh(tb, &bi, S_new[i]);
Dave Jones416e2ab2014-02-17 16:21:24 -0500907 leaf_insert_into_buf(&bi, item_pos - n + snum[i] - 1,
908 ih, body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700909
910 zeros_num = tb->insert_size[0] = 0;
911 }
912 }
913
914 else { /* new item or it part don't falls into S_new[i] */
915
916 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
917 snum[i], sbytes[i], S_new[i]);
918 }
919 break;
920
921 case M_PASTE: /* append item */
922
923 if (n - snum[i] <= item_pos) { /* pasted item or part if it falls to S_new[i] */
924 if (item_pos == n - snum[i] && sbytes[i] != -1) { /* we must shift part of the appended item */
925 struct item_head *aux_ih;
926
927 RFALSE(ih, "PAP-12210: ih must be 0");
928
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400929 aux_ih = item_head(tbS0, item_pos);
Jeff Mahoney1d965fe2009-06-17 16:26:29 -0700930 if (is_direntry_le_ih(aux_ih)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700931 /* we append to directory item */
932
933 int entry_count;
934
Dave Jones416e2ab2014-02-17 16:21:24 -0500935 entry_count = ih_entry_count(aux_ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700936
Dave Jones416e2ab2014-02-17 16:21:24 -0500937 if (entry_count - sbytes[i] < pos_in_item && pos_in_item <= entry_count) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700938 /* new directory entry falls into S_new[i] */
939
Dave Jones416e2ab2014-02-17 16:21:24 -0500940 RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0");
941 RFALSE(sbytes[i] - 1 >= entry_count,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700942 "PAP-12220: there are no so much entries (%d), only %d",
Dave Jones416e2ab2014-02-17 16:21:24 -0500943 sbytes[i] - 1, entry_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944
945 /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500946 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, snum[i], sbytes[i] - 1, S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 /* Paste given directory entry to directory item */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400948 buffer_info_init_bh(tb, &bi, S_new[i]);
Dave Jones416e2ab2014-02-17 16:21:24 -0500949 leaf_paste_in_buffer(&bi, 0, pos_in_item - entry_count + sbytes[i] - 1,
950 tb->insert_size[0], body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700951 /* paste new directory entry */
Dave Jones416e2ab2014-02-17 16:21:24 -0500952 leaf_paste_entries(&bi, 0, pos_in_item - entry_count + sbytes[i] - 1, 1,
953 (struct reiserfs_de_head *) body,
954 body + DEH_SIZE, tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700955 tb->insert_size[0] = 0;
956 pos_in_item++;
957 } else { /* new directory entry doesn't fall into S_new[i] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500958 leaf_move_items(LEAF_FROM_S_TO_SNEW,tb, snum[i], sbytes[i], S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700959 }
960 } else { /* regular object */
961
Dave Jones416e2ab2014-02-17 16:21:24 -0500962 int n_shift, n_rem, r_zeros_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700963 const char *r_body;
964
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400965 RFALSE(pos_in_item != ih_item_len(item_head(tbS0, item_pos)) || tb->insert_size[0] <= 0,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966 "PAP-12225: item too short or insert_size <= 0");
967
968 /* Calculate number of bytes which must be shifted from appended item */
Dave Jones416e2ab2014-02-17 16:21:24 -0500969 n_shift = sbytes[i] - tb->insert_size[0];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700970 if (n_shift < 0)
971 n_shift = 0;
Dave Jones416e2ab2014-02-17 16:21:24 -0500972 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, snum[i], n_shift, S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700973
974 /* Calculate number of bytes which must remain in body after append to S_new[i] */
Dave Jones416e2ab2014-02-17 16:21:24 -0500975 n_rem = tb->insert_size[0] - sbytes[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700976 if (n_rem < 0)
977 n_rem = 0;
978 /* Append part of body into S_new[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -0400979 buffer_info_init_bh(tb, &bi, S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700980 if (n_rem > zeros_num) {
981 r_zeros_number = 0;
Dave Jones416e2ab2014-02-17 16:21:24 -0500982 r_body = body + n_rem - zeros_num;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700983 } else {
984 r_body = body;
Dave Jones416e2ab2014-02-17 16:21:24 -0500985 r_zeros_number = zeros_num - n_rem;
986 zeros_num -= r_zeros_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700987 }
988
Dave Jones416e2ab2014-02-17 16:21:24 -0500989 leaf_paste_in_buffer(&bi, 0, n_shift,
990 tb->insert_size[0] - n_rem,
991 r_body, r_zeros_number);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700992 {
993 struct item_head *tmp;
994
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400995 tmp = item_head(S_new[i], 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700996 if (is_indirect_le_ih
997 (tmp)) {
Dave Jones416e2ab2014-02-17 16:21:24 -0500998 set_ih_free_space(tmp, 0);
999 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 -07001000 } else {
Dave Jones416e2ab2014-02-17 16:21:24 -05001001 set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001002 }
1003 }
1004
1005 tb->insert_size[0] = n_rem;
1006 if (!n_rem)
1007 pos_in_item++;
1008 }
1009 } else
1010 /* item falls wholly into S_new[i] */
1011 {
Harvey Harrison8acc5702008-04-28 02:16:21 -07001012 int leaf_mi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001013 struct item_head *pasted;
1014
1015#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001016 struct item_head *ih_check = item_head(tbS0, item_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017
Harvey Harrison8acc5702008-04-28 02:16:21 -07001018 if (!is_direntry_le_ih(ih_check)
1019 && (pos_in_item != ih_item_len(ih_check)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 || tb->insert_size[0] <= 0))
1021 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001022 "PAP-12235",
1023 "pos_in_item "
1024 "must be equal "
1025 "to ih_item_len");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001026#endif /* CONFIG_REISERFS_CHECK */
1027
Dave Jones416e2ab2014-02-17 16:21:24 -05001028 leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001029 tb, snum[i],
1030 sbytes[i],
1031 S_new[i]);
1032
Harvey Harrison8acc5702008-04-28 02:16:21 -07001033 RFALSE(leaf_mi,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001034 "PAP-12240: unexpected value returned by leaf_move_items (%d)",
Harvey Harrison8acc5702008-04-28 02:16:21 -07001035 leaf_mi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001036
1037 /* paste into item */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001038 buffer_info_init_bh(tb, &bi, S_new[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 leaf_paste_in_buffer(&bi,
Dave Jones416e2ab2014-02-17 16:21:24 -05001040 item_pos - n + snum[i],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001041 pos_in_item,
1042 tb->insert_size[0],
1043 body, zeros_num);
1044
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001045 pasted = item_head(S_new[i], item_pos - n + snum[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001046 if (is_direntry_le_ih(pasted)) {
Jeff Mahoneyeba00302009-03-30 14:02:18 -04001047 leaf_paste_entries(&bi,
Dave Jones416e2ab2014-02-17 16:21:24 -05001048 item_pos - n + snum[i],
1049 pos_in_item, 1,
1050 (struct reiserfs_de_head *)body,
1051 body + DEH_SIZE,
1052 tb->insert_size[0]
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 );
1054 }
1055
1056 /* if we paste to indirect item update ih_free_space */
1057 if (is_indirect_le_ih(pasted))
1058 set_ih_free_space(pasted, 0);
1059 zeros_num = tb->insert_size[0] = 0;
1060 }
1061 }
1062
1063 else { /* pasted item doesn't fall into S_new[i] */
1064
1065 leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
1066 snum[i], sbytes[i], S_new[i]);
1067 }
1068 break;
1069 default: /* cases d and t */
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001070 reiserfs_panic(tb->tb_sb, "PAP-12245",
1071 "blknum > 2: unexpected mode: %s(%d)",
Dave Jones416e2ab2014-02-17 16:21:24 -05001072 (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073 }
1074
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001075 memcpy(insert_key + i, leaf_key(S_new[i], 0), KEY_SIZE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001076 insert_ptr[i] = S_new[i];
1077
1078 RFALSE(!buffer_journaled(S_new[i])
1079 || buffer_journal_dirty(S_new[i])
1080 || buffer_dirty(S_new[i]), "PAP-12247: S_new[%d] : (%b)",
1081 i, S_new[i]);
1082 }
1083
1084 /* if the affected item was not wholly shifted then we perform all necessary operations on that part or whole of the
1085 affected item which remains in S */
1086 if (0 <= item_pos && item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */
1087
1088 switch (flag) {
1089 case M_INSERT: /* insert item into S[0] */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001090 buffer_info_init_tbS0(tb, &bi);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 leaf_insert_into_buf(&bi, item_pos, ih, body,
1092 zeros_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 /* If we insert the first key change the delimiting key */
1095 if (item_pos == 0) {
1096 if (tb->CFL[0]) /* can be 0 in reiserfsck */
Dave Jones416e2ab2014-02-17 16:21:24 -05001097 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001101 case M_PASTE:{ /* append item in S[0] */
1102 struct item_head *pasted;
1103
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001104 pasted = item_head(tbS0, item_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001105 /* when directory, may be new entry already pasted */
1106 if (is_direntry_le_ih(pasted)) {
Dave Jones416e2ab2014-02-17 16:21:24 -05001107 if (pos_in_item >= 0 && pos_in_item <= ih_entry_count(pasted)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001108
1109 RFALSE(!tb->insert_size[0],
1110 "PAP-12260: insert_size is 0 already");
1111
1112 /* prepare space */
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001113 buffer_info_init_tbS0(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -05001114 leaf_paste_in_buffer(&bi, item_pos, pos_in_item,
1115 tb->insert_size[0], body,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 zeros_num);
1117
1118 /* paste entry */
Dave Jones416e2ab2014-02-17 16:21:24 -05001119 leaf_paste_entries(&bi, item_pos, pos_in_item, 1,
1120 (struct reiserfs_de_head *)body,
1121 body + DEH_SIZE,
1122 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001123 if (!item_pos && !pos_in_item) {
Dave Jones416e2ab2014-02-17 16:21:24 -05001124 RFALSE(!tb->CFL[0] || !tb->L[0],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001125 "PAP-12270: CFL[0]/L[0] must be specified");
Dave Jones416e2ab2014-02-17 16:21:24 -05001126 if (tb->CFL[0])
1127 replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001128 }
1129 tb->insert_size[0] = 0;
1130 }
1131 } else { /* regular object */
1132 if (pos_in_item == ih_item_len(pasted)) {
1133
1134 RFALSE(tb->insert_size[0] <= 0,
1135 "PAP-12275: insert size must not be %d",
1136 tb->insert_size[0]);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001137 buffer_info_init_tbS0(tb, &bi);
Dave Jones416e2ab2014-02-17 16:21:24 -05001138 leaf_paste_in_buffer(&bi, item_pos, pos_in_item,
1139 tb->insert_size[0], body, zeros_num);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001140
1141 if (is_indirect_le_ih(pasted)) {
1142#if 0
1143 RFALSE(tb->
1144 insert_size[0] !=
1145 UNFM_P_SIZE,
1146 "PAP-12280: insert_size for indirect item must be %d, not %d",
1147 UNFM_P_SIZE,
1148 tb->
1149 insert_size[0]);
1150#endif
Dave Jones416e2ab2014-02-17 16:21:24 -05001151 set_ih_free_space(pasted, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001152 }
1153 tb->insert_size[0] = 0;
1154 }
1155#ifdef CONFIG_REISERFS_CHECK
1156 else {
1157 if (tb->insert_size[0]) {
1158 print_cur_tb("12285");
Dave Jones416e2ab2014-02-17 16:21:24 -05001159 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001160 "PAP-12285",
1161 "insert_size "
1162 "must be 0 "
1163 "(%d)",
1164 tb->insert_size[0]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001165 }
1166 }
1167#endif /* CONFIG_REISERFS_CHECK */
1168
1169 }
1170 } /* case M_PASTE: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001173#ifdef CONFIG_REISERFS_CHECK
1174 if (flag == M_PASTE && tb->insert_size[0]) {
1175 print_cur_tb("12290");
1176 reiserfs_panic(tb->tb_sb,
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001177 "PAP-12290", "insert_size is still not 0 (%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001178 tb->insert_size[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001180#endif
1181
1182 /* Leaf level of the tree is balanced (end of balance_leaf) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
Jeff Mahoney97fd4b92014-04-23 10:00:45 -04001184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186/* Make empty node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001187void make_empty_node(struct buffer_info *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001189 struct block_head *blkh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191 RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001193 blkh = B_BLK_HEAD(bi->bi_bh);
1194 set_blkh_nr_item(blkh, 0);
1195 set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 if (bi->bi_parent)
1198 B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/* Get first empty buffer */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001202struct buffer_head *get_FEB(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204 int i;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 struct buffer_info bi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001207 for (i = 0; i < MAX_FEB_SIZE; i++)
Al Viro9dce07f2008-03-29 03:07:28 +00001208 if (tb->FEB[i] != NULL)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211 if (i == MAX_FEB_SIZE)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001212 reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001214 buffer_info_init_bh(tb, &bi, tb->FEB[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 make_empty_node(&bi);
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001216 set_buffer_uptodate(tb->FEB[i]);
1217 tb->used[i] = tb->FEB[i];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 tb->FEB[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jeff Mahoneyfba4ebb2009-03-30 14:02:42 -04001220 return tb->used[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Jeff Mahoney098297b2014-04-23 10:00:36 -04001223/* This is now used because reiserfs_free_block has to be able to schedule. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001224static void store_thrown(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001226 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001228 if (buffer_dirty(bh))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001229 reiserfs_warning(tb->tb_sb, "reiserfs-12320",
1230 "called with dirty buffer");
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001231 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001232 if (!tb->thrown[i]) {
1233 tb->thrown[i] = bh;
1234 get_bh(bh); /* free_thrown puts this */
1235 return;
1236 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001237 reiserfs_warning(tb->tb_sb, "reiserfs-12321",
1238 "too many thrown buffers");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001241static void free_thrown(struct tree_balance *tb)
1242{
1243 int i;
1244 b_blocknr_t blocknr;
Ahmed S. Darwish79a81ae2007-02-12 00:52:09 -08001245 for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 if (tb->thrown[i]) {
1247 blocknr = tb->thrown[i]->b_blocknr;
1248 if (buffer_dirty(tb->thrown[i]))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001249 reiserfs_warning(tb->tb_sb, "reiserfs-12322",
1250 "called with dirty buffer %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001251 blocknr);
1252 brelse(tb->thrown[i]); /* incremented in store_thrown */
1253 reiserfs_free_block(tb->transaction_handle, NULL,
1254 blocknr, 0);
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001259void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 struct block_head *blkh;
1262 blkh = B_BLK_HEAD(bh);
1263 set_blkh_level(blkh, FREE_LEVEL);
1264 set_blkh_nr_item(blkh, 0);
1265
1266 clear_buffer_dirty(bh);
1267 store_thrown(tb, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270/* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001271void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest,
1272 struct buffer_head *src, int n_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001275 RFALSE(dest == NULL || src == NULL,
1276 "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)",
1277 src, dest);
1278 RFALSE(!B_IS_KEYS_LEVEL(dest),
1279 "vs-12310: invalid level (%z) for destination buffer. dest must be leaf",
1280 dest);
1281 RFALSE(n_dest < 0 || n_src < 0,
1282 "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest);
1283 RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src),
1284 "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big",
1285 n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 if (B_IS_ITEMS_LEVEL(src))
1288 /* source buffer contains leaf node */
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001289 memcpy(internal_key(dest, n_dest), item_head(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001290 KEY_SIZE);
1291 else
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -04001292 memcpy(internal_key(dest, n_dest), internal_key(src, n_src),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001293 KEY_SIZE);
1294
1295 do_balance_mark_internal_dirty(tb, dest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298int get_left_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001300 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Al Viro9dce07f2008-03-29 03:07:28 +00001302 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001303 "vs-12325: FL[%d](%p) or F[%d](%p) does not exist",
1304 h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001306 if (Sh_position == 0)
1307 return B_NR_ITEMS(tb->FL[h]);
1308 else
1309 return Sh_position - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001312int get_right_neighbor_position(struct tree_balance *tb, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001314 int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Al Viro9dce07f2008-03-29 03:07:28 +00001316 RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 "vs-12330: F[%d](%p) or FR[%d](%p) does not exist",
1318 h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001320 if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h)))
1321 return 0;
1322 else
1323 return Sh_position + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326#ifdef CONFIG_REISERFS_CHECK
1327
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001328int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value);
1329static void check_internal_node(struct super_block *s, struct buffer_head *bh,
1330 char *mes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001332 struct disk_child *dc;
1333 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001335 RFALSE(!bh, "PAP-12336: bh == 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337 if (!bh || !B_IS_IN_TREE(bh))
1338 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001340 RFALSE(!buffer_dirty(bh) &&
1341 !(buffer_journaled(bh) || buffer_journal_dirty(bh)),
1342 "PAP-12337: buffer (%b) must be dirty", bh);
1343 dc = B_N_CHILD(bh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001345 for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) {
1346 if (!is_reusable(s, dc_block_number(dc), 1)) {
1347 print_cur_tb(mes);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001348 reiserfs_panic(s, "PAP-12338",
1349 "invalid child pointer %y in %b",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001350 dc, bh);
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001353}
1354
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001355static int locked_or_not_in_tree(struct tree_balance *tb,
1356 struct buffer_head *bh, char *which)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001357{
1358 if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) ||
1359 !B_IS_IN_TREE(bh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001360 reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001361 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001366static int check_before_balancing(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001368 int retval = 0;
1369
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001370 if (REISERFS_SB(tb->tb_sb)->cur_tb) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001371 reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule "
1372 "occurred based on cur_tb not being null at "
1373 "this point in code. do_balance cannot properly "
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001374 "handle concurrent tree accesses on a same "
1375 "mount point.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001376 }
1377
Jeff Mahoney098297b2014-04-23 10:00:36 -04001378 /*
1379 * double check that buffers that we will modify are unlocked.
1380 * (fix_nodes should already have prepped all of these for us).
1381 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001382 if (tb->lnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001383 retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]");
1384 retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]");
1385 retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001386 check_leaf(tb->L[0]);
1387 }
1388 if (tb->rnum[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001389 retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]");
1390 retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]");
1391 retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001392 check_leaf(tb->R[0]);
1393 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001394 retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path),
1395 "S[0]");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001396 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1397
1398 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001401static void check_after_balance_leaf(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001403 if (tb->lnum[0]) {
1404 if (B_FREE_SPACE(tb->L[0]) !=
1405 MAX_CHILD_SIZE(tb->L[0]) -
1406 dc_size(B_N_CHILD
1407 (tb->FL[0], get_left_neighbor_position(tb, 0)))) {
1408 print_cur_tb("12221");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001409 reiserfs_panic(tb->tb_sb, "PAP-12355",
1410 "shift to left was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001411 }
1412 }
1413 if (tb->rnum[0]) {
1414 if (B_FREE_SPACE(tb->R[0]) !=
1415 MAX_CHILD_SIZE(tb->R[0]) -
1416 dc_size(B_N_CHILD
1417 (tb->FR[0], get_right_neighbor_position(tb, 0)))) {
1418 print_cur_tb("12222");
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001419 reiserfs_panic(tb->tb_sb, "PAP-12360",
1420 "shift to right was incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001421 }
1422 }
1423 if (PATH_H_PBUFFER(tb->tb_path, 1) &&
1424 (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) !=
1425 (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1426 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1427 PATH_H_POSITION(tb->tb_path, 1)))))) {
1428 int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0));
1429 int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) -
1430 dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1),
1431 PATH_H_POSITION(tb->tb_path,
1432 1))));
1433 print_cur_tb("12223");
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001434 reiserfs_warning(tb->tb_sb, "reiserfs-12363",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001435 "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; "
1436 "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d",
1437 left,
1438 MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)),
1439 PATH_H_PBUFFER(tb->tb_path, 1),
1440 PATH_H_POSITION(tb->tb_path, 1),
1441 dc_size(B_N_CHILD
1442 (PATH_H_PBUFFER(tb->tb_path, 1),
1443 PATH_H_POSITION(tb->tb_path, 1))),
1444 right);
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001445 reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001446 }
1447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001449static void check_leaf_level(struct tree_balance *tb)
1450{
1451 check_leaf(tb->L[0]);
1452 check_leaf(tb->R[0]);
1453 check_leaf(PATH_PLAST_BUFFER(tb->tb_path));
1454}
1455
1456static void check_internal_levels(struct tree_balance *tb)
1457{
1458 int h;
1459
1460 /* check all internal nodes */
1461 for (h = 1; tb->insert_size[h]; h++) {
1462 check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h),
1463 "BAD BUFFER ON PATH");
1464 if (tb->lnum[h])
1465 check_internal_node(tb->tb_sb, tb->L[h], "BAD L");
1466 if (tb->rnum[h])
1467 check_internal_node(tb->tb_sb, tb->R[h], "BAD R");
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470}
1471
1472#endif
1473
Jeff Mahoney098297b2014-04-23 10:00:36 -04001474/*
1475 * Now we have all of the buffers that must be used in balancing of
1476 * the tree. We rely on the assumption that schedule() will not occur
1477 * while do_balance works. ( Only interrupt handlers are acceptable.)
1478 * We balance the tree according to the analysis made before this,
1479 * using buffers already obtained. For SMP support it will someday be
1480 * necessary to add ordered locking of tb.
1481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jeff Mahoney098297b2014-04-23 10:00:36 -04001483/*
1484 * Some interesting rules of balancing:
1485 * we delete a maximum of two nodes per level per balancing: we never
1486 * delete R, when we delete two of three nodes L, S, R then we move
1487 * them into R.
1488 *
1489 * we only delete L if we are deleting two nodes, if we delete only
1490 * one node we delete S
1491 *
1492 * if we shift leaves then we shift as much as we can: this is a
1493 * deliberate policy of extremism in node packing which results in
1494 * higher average utilization after repeated random balance operations
1495 * at the cost of more memory copies and more balancing as a result of
1496 * small insertions to full nodes.
1497 *
1498 * if we shift internal nodes we try to evenly balance the node
1499 * utilization, with consequent less balancing at the cost of lower
1500 * utilization.
1501 *
1502 * one could argue that the policy for directories in leaves should be
1503 * that of internal nodes, but we will wait until another day to
1504 * evaluate this.... It would be nice to someday measure and prove
1505 * these assumptions as to what is optimal....
1506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001508static inline void do_balance_starts(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Jeff Mahoney098297b2014-04-23 10:00:36 -04001510 /* use print_cur_tb() to see initial state of struct tree_balance */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001512 /* store_print_tb (tb); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001514 /* do not delete, just comment it out */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001515 /*
1516 print_tb(flag, PATH_LAST_POSITION(tb->tb_path),
1517 tb->tb_path->pos_in_item, tb, "check");
1518 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520#ifdef CONFIG_REISERFS_CHECK
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001521 REISERFS_SB(tb->tb_sb)->cur_tb = tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522#endif
1523}
1524
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001525static inline void do_balance_completed(struct tree_balance *tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001529 check_leaf_level(tb);
1530 check_internal_levels(tb);
Frederic Weisbecker08f14fc2009-05-16 19:10:38 +02001531 REISERFS_SB(tb->tb_sb)->cur_tb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532#endif
1533
Jeff Mahoney098297b2014-04-23 10:00:36 -04001534 /*
1535 * reiserfs_free_block is no longer schedule safe. So, we need to
1536 * put the buffers we want freed on the thrown list during do_balance,
1537 * and then free them now
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001540 REISERFS_SB(tb->tb_sb)->s_do_balance++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001542 /* release all nodes hold to perform the balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 unfix_nodes(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001545 free_thrown(tb);
1546}
1547
Jeff Mahoney098297b2014-04-23 10:00:36 -04001548/*
1549 * do_balance - balance the tree
1550 *
1551 * @tb: tree_balance structure
1552 * @ih: item header of inserted item
1553 * @body: body of inserted item or bytes to paste
1554 * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste
1555 *
1556 * Cut means delete part of an item (includes removing an entry from a
1557 * directory).
1558 *
1559 * Delete means delete whole item.
1560 *
1561 * Insert means add a new item into the tree.
1562 *
1563 * Paste means to append to the end of an existing file or to
1564 * insert a directory entry.
1565 */
1566void do_balance(struct tree_balance *tb, struct item_head *ih,
1567 const char *body, int flag)
1568{
1569 int child_pos; /* position of a child node in its parent */
1570 int h; /* level of the tree being processed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001571
Jeff Mahoney098297b2014-04-23 10:00:36 -04001572 /*
1573 * in our processing of one level we sometimes determine what
1574 * must be inserted into the next higher level. This insertion
1575 * consists of a key or two keys and their corresponding
1576 * pointers
1577 */
1578 struct item_head insert_key[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001579
Jeff Mahoney098297b2014-04-23 10:00:36 -04001580 /* inserted node-ptrs for the next level */
1581 struct buffer_head *insert_ptr[2];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001582
1583 tb->tb_mode = flag;
1584 tb->need_balance_dirty = 0;
1585
1586 if (FILESYSTEM_CHANGED_TB(tb)) {
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001587 reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has "
1588 "changed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001589 }
1590 /* if we have no real work to do */
1591 if (!tb->insert_size[0]) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001592 reiserfs_warning(tb->tb_sb, "PAP-12350",
1593 "insert_size == 0, mode == %c", flag);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001594 unfix_nodes(tb);
1595 return;
1596 }
1597
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001598 atomic_inc(&fs_generation(tb->tb_sb));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001599 do_balance_starts(tb);
1600
Jeff Mahoney098297b2014-04-23 10:00:36 -04001601 /*
1602 * balance_leaf returns 0 except if combining L R and S into
1603 * one node. see balance_internal() for explanation of this
1604 * line of code.
1605 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001606 child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) +
1607 balance_leaf(tb, ih, body, flag, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001610 check_after_balance_leaf(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611#endif
1612
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001613 /* Balance internal level of the tree. */
1614 for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++)
1615 child_pos =
1616 balance_internal(tb, h, child_pos, insert_key, insert_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001618 do_balance_completed(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620}