Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README | |
3 | */ | |
4 | ||
098297b2 JM |
5 | /* |
6 | * Now we have all buffers that must be used in balancing of the tree | |
7 | * Further calculations can not cause schedule(), and thus the buffer | |
8 | * tree will be stable until the balancing will be finished | |
9 | * balance the tree according to the analysis made before, | |
10 | * and using buffers obtained after all above. | |
11 | */ | |
1da177e4 | 12 | |
1da177e4 LT |
13 | #include <asm/uaccess.h> |
14 | #include <linux/time.h> | |
f466c6fd | 15 | #include "reiserfs.h" |
1da177e4 | 16 | #include <linux/buffer_head.h> |
79a81aef | 17 | #include <linux/kernel.h> |
1da177e4 | 18 | |
fba4ebb5 JM |
19 | static 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 | ||
28 | static 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 | ||
37 | static 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 | ||
46 | static inline void buffer_info_init_bh(struct tree_balance *tb, | |
47 | struct buffer_info *bi, | |
48 | struct buffer_head *bh) | |
49 | { | |
50 | bi->tb = tb; | |
51 | bi->bi_bh = bh; | |
52 | bi->bi_parent = NULL; | |
53 | bi->bi_position = 0; | |
54 | } | |
55 | ||
bd4c625c LT |
56 | inline void do_balance_mark_leaf_dirty(struct tree_balance *tb, |
57 | struct buffer_head *bh, int flag) | |
1da177e4 | 58 | { |
09f1b80b | 59 | journal_mark_dirty(tb->transaction_handle, bh); |
1da177e4 LT |
60 | } |
61 | ||
62 | #define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty | |
63 | #define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty | |
64 | ||
098297b2 JM |
65 | /* |
66 | * summary: | |
67 | * if deleting something ( tb->insert_size[0] < 0 ) | |
68 | * return(balance_leaf_when_delete()); (flag d handled here) | |
69 | * else | |
70 | * if lnum is larger than 0 we put items into the left node | |
71 | * if rnum is larger than 0 we put items into the right node | |
72 | * if snum1 is larger than 0 we put items into the new node s1 | |
73 | * if snum2 is larger than 0 we put items into the new node s2 | |
74 | * Note that all *num* count new items being created. | |
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 | */ | |
89 | ||
90 | /* | |
91 | * Balance leaf node in case of delete or cut: insert_size[0] < 0 | |
1da177e4 LT |
92 | * |
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 | |
098297b2 JM |
96 | * >0 means to shift entirely or partly the specified number of items |
97 | * to the neighbor | |
1da177e4 | 98 | */ |
bd4c625c | 99 | static int balance_leaf_when_delete(struct tree_balance *tb, int flag) |
1da177e4 | 100 | { |
bd4c625c LT |
101 | 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; | |
1da177e4 | 107 | |
bd4c625c LT |
108 | 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"); | |
1da177e4 | 114 | |
4cf5f7ad | 115 | ih = item_head(tbS0, item_pos); |
fba4ebb5 | 116 | buffer_info_init_tbS0(tb, &bi); |
1da177e4 | 117 | |
bd4c625c | 118 | /* Delete or truncate the item */ |
1da177e4 | 119 | |
bd4c625c LT |
120 | switch (flag) { |
121 | case M_DELETE: /* delete item in S[0] */ | |
122 | ||
123 | 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); | |
126 | ||
bd4c625c LT |
127 | leaf_delete_items(&bi, 0, item_pos, 1, -1); |
128 | ||
129 | 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 | } | |
139 | } | |
1da177e4 | 140 | |
bd4c625c LT |
141 | RFALSE(!item_pos && !tb->CFL[0], |
142 | "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0], | |
143 | tb->L[0]); | |
1da177e4 | 144 | |
bd4c625c LT |
145 | break; |
146 | ||
147 | case M_CUT:{ /* cut item in S[0] */ | |
bd4c625c LT |
148 | if (is_direntry_le_ih(ih)) { |
149 | ||
098297b2 JM |
150 | /* |
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 | */ | |
bd4c625c LT |
160 | tb->insert_size[0] = -1; |
161 | leaf_cut_from_buffer(&bi, item_pos, pos_in_item, | |
162 | -tb->insert_size[0]); | |
163 | ||
164 | RFALSE(!item_pos && !pos_in_item && !tb->CFL[0], | |
165 | "PAP-12030: can not change delimiting key. CFL[0]=%p", | |
166 | tb->CFL[0]); | |
167 | ||
168 | 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; | |
1da177e4 | 180 | } |
1da177e4 | 181 | |
bd4c625c LT |
182 | default: |
183 | print_cur_tb("12040"); | |
c3a9c210 JM |
184 | reiserfs_panic(tb->tb_sb, "PAP-12040", |
185 | "unexpected mode: %s(%d)", | |
bd4c625c LT |
186 | (flag == |
187 | M_PASTE) ? "PASTE" : ((flag == | |
188 | M_INSERT) ? "INSERT" : | |
189 | "UNKNOWN"), flag); | |
190 | } | |
1da177e4 | 191 | |
098297b2 JM |
192 | /* |
193 | * the rule is that no shifting occurs unless by shifting | |
194 | * a node can be freed | |
195 | */ | |
bd4c625c | 196 | n = B_NR_ITEMS(tbS0); |
098297b2 JM |
197 | /* 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) { | |
bd4c625c | 203 | if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) { |
098297b2 JM |
204 | /* |
205 | * all contents of all the 3 buffers | |
206 | * will be in L[0] | |
207 | */ | |
bd4c625c LT |
208 | 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); | |
213 | ||
214 | 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); | |
219 | ||
220 | reiserfs_invalidate_buffer(tb, tbS0); | |
221 | reiserfs_invalidate_buffer(tb, | |
222 | tb->R[0]); | |
223 | ||
224 | return 0; | |
225 | } | |
098297b2 JM |
226 | /* |
227 | * all contents of all the 3 buffers will | |
228 | * be in R[0] | |
229 | */ | |
bd4c625c LT |
230 | 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); | |
1da177e4 | 238 | |
bd4c625c LT |
239 | reiserfs_invalidate_buffer(tb, tbS0); |
240 | reiserfs_invalidate_buffer(tb, tb->L[0]); | |
1da177e4 | 241 | |
bd4c625c LT |
242 | return -1; |
243 | } | |
1da177e4 | 244 | |
bd4c625c LT |
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); | |
1da177e4 | 249 | |
bd4c625c LT |
250 | reiserfs_invalidate_buffer(tb, tbS0); |
251 | ||
252 | return 0; | |
253 | } | |
098297b2 JM |
254 | |
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 | */ | |
bd4c625c LT |
259 | |
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; | |
1da177e4 | 279 | } |
1da177e4 | 280 | |
bd4c625c LT |
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 | } | |
1da177e4 | 287 | |
bd4c625c LT |
288 | RFALSE(tb->rnum[0], |
289 | "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]); | |
1da177e4 | 290 | return 0; |
1da177e4 LT |
291 | } |
292 | ||
f1f007c3 JM |
293 | static void balance_leaf_insert_left(struct tree_balance *tb, |
294 | struct item_head *ih, const char *body) | |
295 | { | |
296 | int ret_val; | |
297 | struct buffer_info bi; | |
298 | int n = B_NR_ITEMS(tb->L[0]); | |
299 | ||
300 | if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) { | |
301 | /* part of new item falls into L[0] */ | |
302 | int new_item_len; | |
303 | int version; | |
304 | ||
305 | ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, -1); | |
306 | ||
307 | /* Calculate item length to insert to S[0] */ | |
308 | new_item_len = ih_item_len(ih) - tb->lbytes; | |
309 | /* Calculate and check item length to insert to L[0] */ | |
310 | put_ih_item_len(ih, ih_item_len(ih) - new_item_len); | |
311 | ||
312 | RFALSE(ih_item_len(ih) <= 0, | |
313 | "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d", | |
314 | ih_item_len(ih)); | |
315 | ||
316 | /* Insert new item into L[0] */ | |
317 | buffer_info_init_left(tb, &bi); | |
318 | leaf_insert_into_buf(&bi, | |
319 | n + tb->item_pos - ret_val, ih, body, | |
320 | tb->zeroes_num > ih_item_len(ih) ? ih_item_len(ih) : tb->zeroes_num); | |
321 | ||
322 | version = ih_version(ih); | |
323 | ||
324 | /* Calculate key component, item length and body to insert into S[0] */ | |
325 | set_le_ih_k_offset(ih, le_ih_k_offset(ih) + | |
326 | (tb->lbytes << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0))); | |
327 | ||
328 | put_ih_item_len(ih, new_item_len); | |
329 | if (tb->lbytes > tb->zeroes_num) { | |
330 | body += (tb->lbytes - tb->zeroes_num); | |
331 | tb->zeroes_num = 0; | |
332 | } else | |
333 | tb->zeroes_num -= tb->lbytes; | |
334 | ||
335 | RFALSE(ih_item_len(ih) <= 0, | |
336 | "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d", | |
337 | ih_item_len(ih)); | |
338 | } else { | |
339 | /* new item in whole falls into L[0] */ | |
340 | /* Shift lnum[0]-1 items to L[0] */ | |
341 | ret_val = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes); | |
342 | /* Insert new item into L[0] */ | |
343 | buffer_info_init_left(tb, &bi); | |
344 | leaf_insert_into_buf(&bi, n + tb->item_pos - ret_val, ih, body, tb->zeroes_num); | |
345 | tb->insert_size[0] = 0; | |
346 | tb->zeroes_num = 0; | |
347 | } | |
348 | ||
349 | } | |
350 | ||
cf22df18 JM |
351 | static void balance_leaf_paste_left(struct tree_balance *tb, |
352 | struct item_head *ih, const char *body) | |
1da177e4 | 353 | { |
bd4c625c | 354 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
bd4c625c | 355 | int ret_val; |
cf22df18 JM |
356 | struct buffer_info bi; |
357 | int n = B_NR_ITEMS(tb->L[0]); | |
bd4c625c | 358 | |
b49fb112 | 359 | if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) { |
bd4c625c | 360 | /* we must shift the part of the appended item */ |
b49fb112 | 361 | if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { |
bd4c625c | 362 | |
b49fb112 | 363 | RFALSE(tb->zeroes_num, |
bd4c625c LT |
364 | "PAP-12090: invalid parameter in case of a directory"); |
365 | /* directory item */ | |
b49fb112 | 366 | if (tb->lbytes > tb->pos_in_item) { |
bd4c625c | 367 | /* new directory entry falls into L[0] */ |
416e2abd | 368 | struct item_head *pasted; |
b49fb112 | 369 | int l_pos_in_item = tb->pos_in_item; |
bd4c625c LT |
370 | |
371 | /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */ | |
416e2abd | 372 | ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes-1); |
b49fb112 | 373 | if (ret_val && !tb->item_pos) { |
4cf5f7ad JM |
374 | pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1); |
375 | l_pos_in_item += ih_entry_count(pasted) - (tb->lbytes -1); | |
bd4c625c LT |
376 | } |
377 | ||
378 | /* Append given directory entry to directory item */ | |
fba4ebb5 | 379 | buffer_info_init_left(tb, &bi); |
b49fb112 | 380 | leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val, l_pos_in_item, tb->insert_size[0], body, tb->zeroes_num); |
bd4c625c LT |
381 | |
382 | /* previous string prepared space for pasting new entry, following string pastes this entry */ | |
383 | ||
384 | /* when we have merge directory item, pos_in_item has been changed too */ | |
385 | ||
386 | /* paste new directory entry. 1 is entry number */ | |
b49fb112 | 387 | leaf_paste_entries(&bi, n + tb->item_pos - ret_val, l_pos_in_item, |
416e2abd DJ |
388 | 1, (struct reiserfs_de_head *) body, |
389 | body + DEH_SIZE, tb->insert_size[0]); | |
bd4c625c LT |
390 | tb->insert_size[0] = 0; |
391 | } else { | |
392 | /* new directory item doesn't fall into L[0] */ | |
393 | /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */ | |
416e2abd | 394 | leaf_shift_left(tb, tb->lnum[0], tb->lbytes); |
bd4c625c LT |
395 | } |
396 | /* Calculate new position to append in item body */ | |
b49fb112 | 397 | tb->pos_in_item -= tb->lbytes; |
bd4c625c LT |
398 | } else { |
399 | /* regular object */ | |
416e2abd | 400 | RFALSE(tb->lbytes <= 0, "PAP-12095: there is nothing to shift to L[0]. lbytes=%d", tb->lbytes); |
b49fb112 | 401 | RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)), |
bd4c625c | 402 | "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d", |
b49fb112 | 403 | ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item); |
bd4c625c | 404 | |
b49fb112 | 405 | if (tb->lbytes >= tb->pos_in_item) { |
bd4c625c LT |
406 | /* appended item will be in L[0] in whole */ |
407 | int l_n; | |
408 | ||
409 | /* this bytes number must be appended to the last item of L[h] */ | |
b49fb112 | 410 | l_n = tb->lbytes - tb->pos_in_item; |
bd4c625c LT |
411 | |
412 | /* Calculate new insert_size[0] */ | |
416e2abd | 413 | tb->insert_size[0] -= l_n; |
bd4c625c | 414 | |
416e2abd | 415 | RFALSE(tb->insert_size[0] <= 0, |
bd4c625c | 416 | "PAP-12105: there is nothing to paste into L[0]. insert_size=%d", |
416e2abd DJ |
417 | tb->insert_size[0]); |
418 | ret_val = leaf_shift_left(tb, tb->lnum[0], ih_item_len | |
b49fb112 | 419 | (item_head(tbS0, tb->item_pos))); |
bd4c625c | 420 | /* Append to body of item in L[0] */ |
fba4ebb5 | 421 | buffer_info_init_left(tb, &bi); |
bd4c625c | 422 | leaf_paste_in_buffer |
b49fb112 JM |
423 | (&bi, n + tb->item_pos - ret_val, ih_item_len |
424 | (item_head(tb->L[0], n + tb->item_pos - ret_val)), | |
416e2abd | 425 | l_n, body, |
b49fb112 | 426 | tb->zeroes_num > l_n ? l_n : tb->zeroes_num); |
bd4c625c LT |
427 | /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */ |
428 | { | |
429 | int version; | |
416e2abd DJ |
430 | int temp_l = l_n; |
431 | ||
4cf5f7ad | 432 | RFALSE(ih_item_len(item_head(tbS0, 0)), |
bd4c625c | 433 | "PAP-12106: item length must be 0"); |
4cf5f7ad | 434 | RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key |
b49fb112 | 435 | (tb->L[0], n + tb->item_pos - ret_val)), |
bd4c625c | 436 | "PAP-12107: items must be of the same file"); |
b49fb112 | 437 | if (is_indirect_le_ih(item_head(tb->L[0], n + tb->item_pos - ret_val))) { |
416e2abd | 438 | temp_l = l_n << (tb->tb_sb-> s_blocksize_bits - UNFM_P_SHIFT); |
bd4c625c LT |
439 | } |
440 | /* update key of first item in S0 */ | |
4cf5f7ad JM |
441 | version = ih_version(item_head(tbS0, 0)); |
442 | set_le_key_k_offset(version, leaf_key(tbS0, 0), | |
443 | le_key_k_offset(version,leaf_key(tbS0, 0)) + temp_l); | |
bd4c625c | 444 | /* update left delimiting key */ |
4cf5f7ad JM |
445 | set_le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0]), |
446 | le_key_k_offset(version, internal_key(tb->CFL[0], tb->lkey[0])) + temp_l); | |
bd4c625c LT |
447 | } |
448 | ||
449 | /* Calculate new body, position in item and insert_size[0] */ | |
b49fb112 JM |
450 | if (l_n > tb->zeroes_num) { |
451 | body += (l_n - tb->zeroes_num); | |
452 | tb->zeroes_num = 0; | |
bd4c625c | 453 | } else |
b49fb112 JM |
454 | tb->zeroes_num -= l_n; |
455 | tb->pos_in_item = 0; | |
bd4c625c | 456 | |
4cf5f7ad JM |
457 | RFALSE(comp_short_le_keys(leaf_key(tbS0, 0), leaf_key(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1)) |
458 | || !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size) | |
459 | || !op_is_left_mergeable(internal_key(tb->CFL[0], tb->lkey[0]), tbS0->b_size), | |
bd4c625c LT |
460 | "PAP-12120: item must be merge-able with left neighboring item"); |
461 | } else { /* only part of the appended item will be in L[0] */ | |
462 | ||
463 | /* Calculate position in item for append in S[0] */ | |
b49fb112 | 464 | tb->pos_in_item -= tb->lbytes; |
bd4c625c | 465 | |
b49fb112 | 466 | RFALSE(tb->pos_in_item <= 0, "PAP-12125: no place for paste. pos_in_item=%d", tb->pos_in_item); |
bd4c625c LT |
467 | |
468 | /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */ | |
416e2abd | 469 | leaf_shift_left(tb, tb->lnum[0], tb->lbytes); |
bd4c625c LT |
470 | } |
471 | } | |
472 | } else { /* appended item will be in L[0] in whole */ | |
473 | ||
474 | struct item_head *pasted; | |
475 | ||
b49fb112 | 476 | 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 */ |
bd4c625c | 477 | /* then increment pos_in_item by the size of the last item in L[0] */ |
4cf5f7ad | 478 | pasted = item_head(tb->L[0], n - 1); |
bd4c625c | 479 | if (is_direntry_le_ih(pasted)) |
b49fb112 | 480 | tb->pos_in_item += ih_entry_count(pasted); |
bd4c625c | 481 | else |
b49fb112 | 482 | tb->pos_in_item += ih_item_len(pasted); |
bd4c625c LT |
483 | } |
484 | ||
485 | /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */ | |
416e2abd | 486 | ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes); |
bd4c625c | 487 | /* Append to body of item in L[0] */ |
fba4ebb5 | 488 | buffer_info_init_left(tb, &bi); |
b49fb112 JM |
489 | leaf_paste_in_buffer(&bi, n + tb->item_pos - ret_val, |
490 | tb->pos_in_item, | |
bd4c625c | 491 | tb->insert_size[0], |
b49fb112 | 492 | body, tb->zeroes_num); |
bd4c625c LT |
493 | |
494 | /* if appended item is directory, paste entry */ | |
b49fb112 | 495 | pasted = item_head(tb->L[0], n + tb->item_pos - ret_val); |
bd4c625c | 496 | if (is_direntry_le_ih(pasted)) |
b49fb112 JM |
497 | leaf_paste_entries(&bi, n + tb->item_pos - ret_val, |
498 | tb->pos_in_item, 1, | |
416e2abd DJ |
499 | (struct reiserfs_de_head *) body, |
500 | body + DEH_SIZE, | |
501 | tb->insert_size[0]); | |
bd4c625c LT |
502 | /* if appended item is indirect item, put unformatted node into un list */ |
503 | if (is_indirect_le_ih(pasted)) | |
504 | set_ih_free_space(pasted, 0); | |
505 | tb->insert_size[0] = 0; | |
b49fb112 | 506 | tb->zeroes_num = 0; |
bd4c625c | 507 | } |
cf22df18 JM |
508 | |
509 | } | |
510 | ||
511 | /** | |
512 | * balance_leaf - reiserfs tree balancing algorithm | |
513 | * @tb: tree balance state | |
514 | * @ih: item header of inserted item (little endian) | |
515 | * @body: body of inserted item or bytes to paste | |
516 | * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance) | |
517 | * passed back: | |
518 | * @insert_key: key to insert new nodes | |
519 | * @insert_ptr: array of nodes to insert at the next level | |
520 | * | |
521 | * In our processing of one level we sometimes determine what must be | |
522 | * inserted into the next higher level. This insertion consists of a | |
523 | * key or two keys and their corresponding pointers. | |
524 | */ | |
525 | static int balance_leaf(struct tree_balance *tb, struct item_head *ih, | |
526 | const char *body, int flag, | |
527 | struct item_head *insert_key, | |
528 | struct buffer_head **insert_ptr) | |
529 | { | |
530 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
531 | struct buffer_info bi; | |
532 | int n, i; | |
533 | int ret_val; | |
534 | ||
535 | PROC_INFO_INC(tb->tb_sb, balance_at[0]); | |
536 | ||
537 | /* Make balance in case insert_size[0] < 0 */ | |
538 | if (tb->insert_size[0] < 0) | |
539 | return balance_leaf_when_delete(tb, flag); | |
540 | ||
541 | tb->item_pos = PATH_LAST_POSITION(tb->tb_path), | |
542 | tb->pos_in_item = tb->tb_path->pos_in_item, | |
543 | tb->zeroes_num = 0; | |
544 | if (flag == M_INSERT && !body) | |
545 | tb->zeroes_num = ih_item_len(ih); | |
546 | ||
547 | /* | |
548 | * for indirect item pos_in_item is measured in unformatted node | |
549 | * pointers. Recalculate to bytes | |
550 | */ | |
551 | if (flag != M_INSERT | |
552 | && is_indirect_le_ih(item_head(tbS0, tb->item_pos))) | |
553 | tb->pos_in_item *= UNFM_P_SIZE; | |
554 | ||
555 | if (tb->lnum[0] > 0) { | |
556 | /* Shift lnum[0] items from S[0] to the left neighbor L[0] */ | |
557 | if (tb->item_pos < tb->lnum[0]) { | |
558 | /* new item or it part falls to L[0], shift it too */ | |
559 | n = B_NR_ITEMS(tb->L[0]); | |
560 | ||
561 | switch (flag) { | |
562 | case M_INSERT: /* insert item into L[0] */ | |
563 | balance_leaf_insert_left(tb, ih, body); | |
564 | break; | |
565 | ||
566 | case M_PASTE: /* append item in L[0] */ | |
567 | balance_leaf_paste_left(tb, ih, body); | |
bd4c625c LT |
568 | break; |
569 | default: /* cases d and t */ | |
c3a9c210 JM |
570 | reiserfs_panic(tb->tb_sb, "PAP-12130", |
571 | "lnum > 0: unexpected mode: " | |
572 | " %s(%d)", | |
416e2abd | 573 | (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag); |
1da177e4 | 574 | } |
bd4c625c LT |
575 | } else { |
576 | /* new item doesn't fall into L[0] */ | |
577 | leaf_shift_left(tb, tb->lnum[0], tb->lbytes); | |
1da177e4 | 578 | } |
1da177e4 | 579 | } |
1da177e4 | 580 | |
bd4c625c LT |
581 | /* tb->lnum[0] > 0 */ |
582 | /* Calculate new item position */ | |
b49fb112 | 583 | tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0)); |
bd4c625c LT |
584 | |
585 | if (tb->rnum[0] > 0) { | |
586 | /* shift rnum[0] items from S[0] to the right neighbor R[0] */ | |
587 | n = B_NR_ITEMS(tbS0); | |
588 | switch (flag) { | |
589 | ||
590 | case M_INSERT: /* insert item */ | |
b49fb112 JM |
591 | if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */ |
592 | if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */ | |
593 | loff_t old_key_comp, old_len, r_zeroes_number; | |
bd4c625c LT |
594 | const char *r_body; |
595 | int version; | |
596 | loff_t offset; | |
597 | ||
416e2abd | 598 | leaf_shift_right(tb, tb->rnum[0] - 1, -1); |
bd4c625c LT |
599 | |
600 | version = ih_version(ih); | |
601 | /* Remember key component and item length */ | |
602 | old_key_comp = le_ih_k_offset(ih); | |
603 | old_len = ih_item_len(ih); | |
604 | ||
605 | /* Calculate key component and item length to insert into R[0] */ | |
416e2abd | 606 | 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)); |
bd4c625c LT |
607 | set_le_ih_k_offset(ih, offset); |
608 | put_ih_item_len(ih, tb->rbytes); | |
609 | /* Insert part of the item into R[0] */ | |
fba4ebb5 | 610 | buffer_info_init_right(tb, &bi); |
b49fb112 JM |
611 | if ((old_len - tb->rbytes) > tb->zeroes_num) { |
612 | r_zeroes_number = 0; | |
613 | r_body = body + (old_len - tb->rbytes) - tb->zeroes_num; | |
bd4c625c LT |
614 | } else { |
615 | r_body = body; | |
b49fb112 JM |
616 | r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes); |
617 | tb->zeroes_num -= r_zeroes_number; | |
bd4c625c LT |
618 | } |
619 | ||
620 | leaf_insert_into_buf(&bi, 0, ih, r_body, | |
b49fb112 | 621 | r_zeroes_number); |
bd4c625c LT |
622 | |
623 | /* Replace right delimiting key by first key in R[0] */ | |
624 | replace_key(tb, tb->CFR[0], tb->rkey[0], | |
625 | tb->R[0], 0); | |
626 | ||
627 | /* Calculate key component and item length to insert into S[0] */ | |
628 | set_le_ih_k_offset(ih, old_key_comp); | |
416e2abd | 629 | put_ih_item_len(ih, old_len - tb->rbytes); |
bd4c625c LT |
630 | |
631 | tb->insert_size[0] -= tb->rbytes; | |
632 | ||
633 | } else { /* whole new item falls into R[0] */ | |
634 | ||
635 | /* Shift rnum[0]-1 items to R[0] */ | |
416e2abd | 636 | ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes); |
bd4c625c | 637 | /* Insert new item into R[0] */ |
fba4ebb5 | 638 | buffer_info_init_right(tb, &bi); |
b49fb112 JM |
639 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1, |
640 | ih, body, tb->zeroes_num); | |
bd4c625c | 641 | |
b49fb112 | 642 | if (tb->item_pos - n + tb->rnum[0] - 1 == 0) { |
bd4c625c LT |
643 | replace_key(tb, tb->CFR[0], |
644 | tb->rkey[0], | |
645 | tb->R[0], 0); | |
646 | ||
647 | } | |
b49fb112 | 648 | tb->zeroes_num = tb->insert_size[0] = 0; |
bd4c625c LT |
649 | } |
650 | } else { /* new item or part of it doesn't fall into R[0] */ | |
1da177e4 | 651 | |
bd4c625c | 652 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
1da177e4 | 653 | } |
bd4c625c LT |
654 | break; |
655 | ||
656 | case M_PASTE: /* append item */ | |
657 | ||
b49fb112 JM |
658 | if (n - tb->rnum[0] <= tb->item_pos) { /* pasted item or part of it falls to R[0] */ |
659 | if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */ | |
660 | if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */ | |
bd4c625c LT |
661 | int entry_count; |
662 | ||
b49fb112 | 663 | RFALSE(tb->zeroes_num, |
bd4c625c | 664 | "PAP-12145: invalid parameter in case of a directory"); |
4cf5f7ad | 665 | entry_count = ih_entry_count(item_head |
b49fb112 | 666 | (tbS0, tb->item_pos)); |
bd4c625c | 667 | if (entry_count - tb->rbytes < |
b49fb112 | 668 | tb->pos_in_item) |
bd4c625c LT |
669 | /* new directory entry falls into R[0] */ |
670 | { | |
671 | int paste_entry_position; | |
672 | ||
416e2abd | 673 | RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0], |
bd4c625c | 674 | "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d", |
416e2abd | 675 | tb->rbytes, entry_count); |
bd4c625c | 676 | /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */ |
416e2abd | 677 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1); |
bd4c625c | 678 | /* Paste given directory entry to directory item */ |
b49fb112 | 679 | paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1; |
fba4ebb5 | 680 | buffer_info_init_right(tb, &bi); |
b49fb112 | 681 | leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num); |
bd4c625c | 682 | /* paste entry */ |
416e2abd DJ |
683 | leaf_paste_entries(&bi, 0, paste_entry_position, 1, |
684 | (struct reiserfs_de_head *) body, | |
685 | body + DEH_SIZE, tb->insert_size[0]); | |
686 | ||
687 | if (paste_entry_position == 0) { | |
bd4c625c | 688 | /* change delimiting keys */ |
416e2abd | 689 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0); |
bd4c625c LT |
690 | } |
691 | ||
692 | tb->insert_size[0] = 0; | |
b49fb112 | 693 | tb->pos_in_item++; |
bd4c625c LT |
694 | } else { /* new directory entry doesn't fall into R[0] */ |
695 | ||
416e2abd | 696 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
bd4c625c LT |
697 | } |
698 | } else { /* regular object */ | |
699 | ||
b49fb112 | 700 | int n_shift, n_rem, r_zeroes_number; |
bd4c625c LT |
701 | const char *r_body; |
702 | ||
703 | /* Calculate number of bytes which must be shifted from appended item */ | |
416e2abd | 704 | if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0) |
bd4c625c LT |
705 | n_shift = 0; |
706 | ||
b49fb112 JM |
707 | RFALSE(tb->pos_in_item != ih_item_len |
708 | (item_head(tbS0, tb->item_pos)), | |
bd4c625c | 709 | "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d", |
b49fb112 JM |
710 | tb->pos_in_item, ih_item_len |
711 | (item_head(tbS0, tb->item_pos))); | |
416e2abd DJ |
712 | |
713 | leaf_shift_right(tb, tb->rnum[0], n_shift); | |
bd4c625c | 714 | /* Calculate number of bytes which must remain in body after appending to R[0] */ |
416e2abd | 715 | if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0) |
bd4c625c LT |
716 | n_rem = 0; |
717 | ||
718 | { | |
719 | int version; | |
416e2abd DJ |
720 | unsigned long temp_rem = n_rem; |
721 | ||
4cf5f7ad JM |
722 | version = ih_version(item_head(tb->R[0], 0)); |
723 | if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) { | |
416e2abd | 724 | temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT); |
bd4c625c | 725 | } |
4cf5f7ad JM |
726 | set_le_key_k_offset(version, leaf_key(tb->R[0], 0), |
727 | le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem); | |
728 | set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]), | |
729 | le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem); | |
bd4c625c | 730 | } |
4cf5f7ad JM |
731 | /* k_offset (leaf_key(tb->R[0],0)) += n_rem; |
732 | k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/ | |
416e2abd | 733 | do_balance_mark_internal_dirty(tb, tb->CFR[0], 0); |
bd4c625c LT |
734 | |
735 | /* Append part of body into R[0] */ | |
fba4ebb5 | 736 | buffer_info_init_right(tb, &bi); |
b49fb112 JM |
737 | if (n_rem > tb->zeroes_num) { |
738 | r_zeroes_number = 0; | |
739 | r_body = body + n_rem - tb->zeroes_num; | |
bd4c625c LT |
740 | } else { |
741 | r_body = body; | |
b49fb112 JM |
742 | r_zeroes_number = tb->zeroes_num - n_rem; |
743 | tb->zeroes_num -= r_zeroes_number; | |
bd4c625c LT |
744 | } |
745 | ||
416e2abd DJ |
746 | leaf_paste_in_buffer(&bi, 0, n_shift, |
747 | tb->insert_size[0] - n_rem, | |
b49fb112 | 748 | r_body, r_zeroes_number); |
416e2abd | 749 | |
4cf5f7ad | 750 | if (is_indirect_le_ih(item_head(tb->R[0], 0))) { |
1da177e4 | 751 | #if 0 |
bd4c625c LT |
752 | RFALSE(n_rem, |
753 | "PAP-12160: paste more than one unformatted node pointer"); | |
1da177e4 | 754 | #endif |
4cf5f7ad | 755 | set_ih_free_space(item_head(tb->R[0], 0), 0); |
bd4c625c LT |
756 | } |
757 | tb->insert_size[0] = n_rem; | |
758 | if (!n_rem) | |
b49fb112 | 759 | tb->pos_in_item++; |
bd4c625c LT |
760 | } |
761 | } else { /* pasted item in whole falls into R[0] */ | |
762 | ||
763 | struct item_head *pasted; | |
764 | ||
416e2abd | 765 | ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
bd4c625c | 766 | /* append item in R[0] */ |
b49fb112 | 767 | if (tb->pos_in_item >= 0) { |
fba4ebb5 | 768 | buffer_info_init_right(tb, &bi); |
b49fb112 JM |
769 | leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item, |
770 | tb->insert_size[0], body, tb->zeroes_num); | |
bd4c625c LT |
771 | } |
772 | ||
773 | /* paste new entry, if item is directory item */ | |
b49fb112 JM |
774 | pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]); |
775 | if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) { | |
776 | leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0], | |
777 | tb->pos_in_item, 1, | |
416e2abd DJ |
778 | (struct reiserfs_de_head *) body, |
779 | body + DEH_SIZE, tb->insert_size[0]); | |
b49fb112 | 780 | if (!tb->pos_in_item) { |
bd4c625c | 781 | |
b49fb112 | 782 | RFALSE(tb->item_pos - n + tb->rnum[0], |
bd4c625c LT |
783 | "PAP-12165: directory item must be first item of node when pasting is in 0th position"); |
784 | ||
785 | /* update delimiting keys */ | |
416e2abd | 786 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0); |
bd4c625c LT |
787 | } |
788 | } | |
789 | ||
790 | if (is_indirect_le_ih(pasted)) | |
791 | set_ih_free_space(pasted, 0); | |
b49fb112 | 792 | tb->zeroes_num = tb->insert_size[0] = 0; |
bd4c625c LT |
793 | } |
794 | } else { /* new item doesn't fall into R[0] */ | |
1da177e4 | 795 | |
bd4c625c | 796 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
1da177e4 | 797 | } |
bd4c625c LT |
798 | break; |
799 | default: /* cases d and t */ | |
c3a9c210 JM |
800 | reiserfs_panic(tb->tb_sb, "PAP-12175", |
801 | "rnum > 0: unexpected mode: %s(%d)", | |
416e2abd | 802 | (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag); |
1da177e4 | 803 | } |
1da177e4 | 804 | |
bd4c625c | 805 | } |
1da177e4 | 806 | |
bd4c625c LT |
807 | /* tb->rnum[0] > 0 */ |
808 | RFALSE(tb->blknum[0] > 3, | |
416e2abd | 809 | "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]); |
bd4c625c | 810 | RFALSE(tb->blknum[0] < 0, |
416e2abd | 811 | "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]); |
bd4c625c | 812 | |
97fd4b97 JM |
813 | /* |
814 | * if while adding to a node we discover that it is possible to split | |
815 | * it in two, and merge the left part into the left neighbor and the | |
816 | * right part into the right neighbor, eliminating the node | |
817 | */ | |
bd4c625c LT |
818 | if (tb->blknum[0] == 0) { /* node S[0] is empty now */ |
819 | ||
820 | RFALSE(!tb->lnum[0] || !tb->rnum[0], | |
821 | "PAP-12190: lnum and rnum must not be zero"); | |
97fd4b97 JM |
822 | /* |
823 | * if insertion was done before 0-th position in R[0], right | |
824 | * delimiting key of the tb->L[0]'s and left delimiting key are | |
825 | * not set correctly | |
826 | */ | |
bd4c625c LT |
827 | if (tb->CFL[0]) { |
828 | if (!tb->CFR[0]) | |
c3a9c210 JM |
829 | reiserfs_panic(tb->tb_sb, "vs-12195", |
830 | "CFR not initialized"); | |
4cf5f7ad JM |
831 | copy_key(internal_key(tb->CFL[0], tb->lkey[0]), |
832 | internal_key(tb->CFR[0], tb->rkey[0])); | |
bd4c625c LT |
833 | do_balance_mark_internal_dirty(tb, tb->CFL[0], 0); |
834 | } | |
1da177e4 | 835 | |
bd4c625c LT |
836 | reiserfs_invalidate_buffer(tb, tbS0); |
837 | return 0; | |
838 | } | |
1da177e4 | 839 | |
bd4c625c | 840 | /* Fill new nodes that appear in place of S[0] */ |
bd4c625c LT |
841 | for (i = tb->blknum[0] - 2; i >= 0; i--) { |
842 | ||
b49fb112 JM |
843 | RFALSE(!tb->snum[i], |
844 | "PAP-12200: snum[%d] == %d. Must be > 0", i, | |
845 | tb->snum[i]); | |
bd4c625c LT |
846 | |
847 | /* here we shift from S to S_new nodes */ | |
848 | ||
b49fb112 | 849 | tb->S_new[i] = get_FEB(tb); |
bd4c625c LT |
850 | |
851 | /* initialized block type and tree level */ | |
b49fb112 | 852 | set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL); |
bd4c625c LT |
853 | |
854 | n = B_NR_ITEMS(tbS0); | |
855 | ||
856 | switch (flag) { | |
857 | case M_INSERT: /* insert item */ | |
858 | ||
b49fb112 JM |
859 | if (n - tb->snum[i] < tb->item_pos) { /* new item or it's part falls to first new node S_new[i] */ |
860 | if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { /* part of new item falls into S_new[i] */ | |
861 | int old_key_comp, old_len, r_zeroes_number; | |
bd4c625c LT |
862 | const char *r_body; |
863 | int version; | |
864 | ||
865 | /* Move snum[i]-1 items from S[0] to S_new[i] */ | |
866 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, | |
b49fb112 JM |
867 | tb->snum[i] - 1, -1, |
868 | tb->S_new[i]); | |
bd4c625c LT |
869 | /* Remember key component and item length */ |
870 | version = ih_version(ih); | |
871 | old_key_comp = le_ih_k_offset(ih); | |
872 | old_len = ih_item_len(ih); | |
873 | ||
874 | /* Calculate key component and item length to insert into S_new[i] */ | |
416e2abd | 875 | set_le_ih_k_offset(ih, le_ih_k_offset(ih) + |
b49fb112 | 876 | ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0))); |
bd4c625c | 877 | |
b49fb112 | 878 | put_ih_item_len(ih, tb->sbytes[i]); |
bd4c625c LT |
879 | |
880 | /* Insert part of the item into S_new[i] before 0-th item */ | |
b49fb112 | 881 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
bd4c625c | 882 | |
b49fb112 JM |
883 | if ((old_len - tb->sbytes[i]) > tb->zeroes_num) { |
884 | r_zeroes_number = 0; | |
885 | r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num; | |
bd4c625c LT |
886 | } else { |
887 | r_body = body; | |
b49fb112 JM |
888 | r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]); |
889 | tb->zeroes_num -= r_zeroes_number; | |
bd4c625c LT |
890 | } |
891 | ||
b49fb112 | 892 | leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number); |
bd4c625c LT |
893 | |
894 | /* Calculate key component and item length to insert into S[i] */ | |
895 | set_le_ih_k_offset(ih, old_key_comp); | |
b49fb112 JM |
896 | put_ih_item_len(ih, old_len - tb->sbytes[i]); |
897 | tb->insert_size[0] -= tb->sbytes[i]; | |
bd4c625c LT |
898 | } else { /* whole new item falls into S_new[i] */ |
899 | ||
900 | /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */ | |
901 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, | |
b49fb112 | 902 | tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]); |
bd4c625c LT |
903 | |
904 | /* Insert new item into S_new[i] */ | |
b49fb112 JM |
905 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
906 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1, | |
907 | ih, body, tb->zeroes_num); | |
bd4c625c | 908 | |
b49fb112 | 909 | tb->zeroes_num = tb->insert_size[0] = 0; |
bd4c625c LT |
910 | } |
911 | } | |
1da177e4 | 912 | |
bd4c625c | 913 | else { /* new item or it part don't falls into S_new[i] */ |
1da177e4 | 914 | |
bd4c625c | 915 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, |
b49fb112 | 916 | tb->snum[i], tb->sbytes[i], tb->S_new[i]); |
1da177e4 | 917 | } |
bd4c625c LT |
918 | break; |
919 | ||
920 | case M_PASTE: /* append item */ | |
921 | ||
b49fb112 JM |
922 | if (n - tb->snum[i] <= tb->item_pos) { /* pasted item or part if it falls to S_new[i] */ |
923 | if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) { /* we must shift part of the appended item */ | |
bd4c625c LT |
924 | struct item_head *aux_ih; |
925 | ||
926 | RFALSE(ih, "PAP-12210: ih must be 0"); | |
927 | ||
b49fb112 | 928 | aux_ih = item_head(tbS0, tb->item_pos); |
1d965fe0 | 929 | if (is_direntry_le_ih(aux_ih)) { |
bd4c625c LT |
930 | /* we append to directory item */ |
931 | ||
932 | int entry_count; | |
933 | ||
416e2abd | 934 | entry_count = ih_entry_count(aux_ih); |
bd4c625c | 935 | |
b49fb112 | 936 | if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) { |
bd4c625c LT |
937 | /* new directory entry falls into S_new[i] */ |
938 | ||
416e2abd | 939 | RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0"); |
b49fb112 | 940 | RFALSE(tb->sbytes[i] - 1 >= entry_count, |
bd4c625c | 941 | "PAP-12220: there are no so much entries (%d), only %d", |
b49fb112 | 942 | tb->sbytes[i] - 1, entry_count); |
bd4c625c LT |
943 | |
944 | /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */ | |
b49fb112 | 945 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]); |
bd4c625c | 946 | /* Paste given directory entry to directory item */ |
b49fb112 JM |
947 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
948 | leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, | |
949 | tb->insert_size[0], body, tb->zeroes_num); | |
bd4c625c | 950 | /* paste new directory entry */ |
b49fb112 | 951 | leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1, |
416e2abd DJ |
952 | (struct reiserfs_de_head *) body, |
953 | body + DEH_SIZE, tb->insert_size[0]); | |
bd4c625c | 954 | tb->insert_size[0] = 0; |
b49fb112 | 955 | tb->pos_in_item++; |
bd4c625c | 956 | } else { /* new directory entry doesn't fall into S_new[i] */ |
b49fb112 | 957 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]); |
bd4c625c LT |
958 | } |
959 | } else { /* regular object */ | |
960 | ||
b49fb112 | 961 | int n_shift, n_rem, r_zeroes_number; |
bd4c625c LT |
962 | const char *r_body; |
963 | ||
b49fb112 | 964 | RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0, |
bd4c625c LT |
965 | "PAP-12225: item too short or insert_size <= 0"); |
966 | ||
967 | /* Calculate number of bytes which must be shifted from appended item */ | |
b49fb112 | 968 | n_shift = tb->sbytes[i] - tb->insert_size[0]; |
bd4c625c LT |
969 | if (n_shift < 0) |
970 | n_shift = 0; | |
b49fb112 | 971 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]); |
bd4c625c LT |
972 | |
973 | /* Calculate number of bytes which must remain in body after append to S_new[i] */ | |
b49fb112 | 974 | n_rem = tb->insert_size[0] - tb->sbytes[i]; |
bd4c625c LT |
975 | if (n_rem < 0) |
976 | n_rem = 0; | |
977 | /* Append part of body into S_new[0] */ | |
b49fb112 JM |
978 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
979 | if (n_rem > tb->zeroes_num) { | |
980 | r_zeroes_number = 0; | |
981 | r_body = body + n_rem - tb->zeroes_num; | |
bd4c625c LT |
982 | } else { |
983 | r_body = body; | |
b49fb112 JM |
984 | r_zeroes_number = tb->zeroes_num - n_rem; |
985 | tb->zeroes_num -= r_zeroes_number; | |
bd4c625c LT |
986 | } |
987 | ||
416e2abd DJ |
988 | leaf_paste_in_buffer(&bi, 0, n_shift, |
989 | tb->insert_size[0] - n_rem, | |
b49fb112 | 990 | r_body, r_zeroes_number); |
bd4c625c LT |
991 | { |
992 | struct item_head *tmp; | |
993 | ||
b49fb112 | 994 | tmp = item_head(tb->S_new[i], 0); |
bd4c625c LT |
995 | if (is_indirect_le_ih |
996 | (tmp)) { | |
416e2abd DJ |
997 | set_ih_free_space(tmp, 0); |
998 | set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT))); | |
bd4c625c | 999 | } else { |
416e2abd | 1000 | set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem); |
bd4c625c LT |
1001 | } |
1002 | } | |
1003 | ||
1004 | tb->insert_size[0] = n_rem; | |
1005 | if (!n_rem) | |
b49fb112 | 1006 | tb->pos_in_item++; |
bd4c625c LT |
1007 | } |
1008 | } else | |
1009 | /* item falls wholly into S_new[i] */ | |
1010 | { | |
8acc570f | 1011 | int leaf_mi; |
bd4c625c | 1012 | struct item_head *pasted; |
1da177e4 | 1013 | |
bd4c625c | 1014 | #ifdef CONFIG_REISERFS_CHECK |
b49fb112 | 1015 | struct item_head *ih_check = item_head(tbS0, tb->item_pos); |
bd4c625c | 1016 | |
8acc570f | 1017 | if (!is_direntry_le_ih(ih_check) |
b49fb112 | 1018 | && (tb->pos_in_item != ih_item_len(ih_check) |
bd4c625c LT |
1019 | || tb->insert_size[0] <= 0)) |
1020 | reiserfs_panic(tb->tb_sb, | |
c3a9c210 JM |
1021 | "PAP-12235", |
1022 | "pos_in_item " | |
1023 | "must be equal " | |
1024 | "to ih_item_len"); | |
bd4c625c LT |
1025 | #endif /* CONFIG_REISERFS_CHECK */ |
1026 | ||
416e2abd | 1027 | leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW, |
b49fb112 JM |
1028 | tb, tb->snum[i], |
1029 | tb->sbytes[i], | |
1030 | tb->S_new[i]); | |
bd4c625c | 1031 | |
8acc570f | 1032 | RFALSE(leaf_mi, |
bd4c625c | 1033 | "PAP-12240: unexpected value returned by leaf_move_items (%d)", |
8acc570f | 1034 | leaf_mi); |
bd4c625c LT |
1035 | |
1036 | /* paste into item */ | |
b49fb112 | 1037 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
bd4c625c | 1038 | leaf_paste_in_buffer(&bi, |
b49fb112 JM |
1039 | tb->item_pos - n + tb->snum[i], |
1040 | tb->pos_in_item, | |
bd4c625c | 1041 | tb->insert_size[0], |
b49fb112 | 1042 | body, tb->zeroes_num); |
bd4c625c | 1043 | |
b49fb112 | 1044 | pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]); |
bd4c625c | 1045 | if (is_direntry_le_ih(pasted)) { |
eba00305 | 1046 | leaf_paste_entries(&bi, |
b49fb112 JM |
1047 | tb->item_pos - n + tb->snum[i], |
1048 | tb->pos_in_item, 1, | |
416e2abd DJ |
1049 | (struct reiserfs_de_head *)body, |
1050 | body + DEH_SIZE, | |
1051 | tb->insert_size[0] | |
bd4c625c LT |
1052 | ); |
1053 | } | |
1054 | ||
1055 | /* if we paste to indirect item update ih_free_space */ | |
1056 | if (is_indirect_le_ih(pasted)) | |
1057 | set_ih_free_space(pasted, 0); | |
b49fb112 | 1058 | tb->zeroes_num = tb->insert_size[0] = 0; |
bd4c625c | 1059 | } |
1da177e4 LT |
1060 | } |
1061 | ||
bd4c625c | 1062 | else { /* pasted item doesn't fall into S_new[i] */ |
1da177e4 | 1063 | |
bd4c625c | 1064 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, |
b49fb112 | 1065 | tb->snum[i], tb->sbytes[i], tb->S_new[i]); |
bd4c625c LT |
1066 | } |
1067 | break; | |
1068 | default: /* cases d and t */ | |
c3a9c210 JM |
1069 | reiserfs_panic(tb->tb_sb, "PAP-12245", |
1070 | "blknum > 2: unexpected mode: %s(%d)", | |
416e2abd | 1071 | (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag); |
1da177e4 | 1072 | } |
1da177e4 | 1073 | |
b49fb112 JM |
1074 | memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE); |
1075 | insert_ptr[i] = tb->S_new[i]; | |
bd4c625c | 1076 | |
b49fb112 JM |
1077 | RFALSE(!buffer_journaled(tb->S_new[i]) |
1078 | || buffer_journal_dirty(tb->S_new[i]) | |
1079 | || buffer_dirty(tb->S_new[i]), | |
1080 | "PAP-12247: S_new[%d] : (%b)", | |
1081 | i, tb->S_new[i]); | |
1da177e4 LT |
1082 | } |
1083 | ||
bd4c625c LT |
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 */ | |
b49fb112 | 1086 | if (0 <= tb->item_pos && tb->item_pos < tb->s0num) { /* if we must insert or append into buffer S[0] */ |
bd4c625c LT |
1087 | |
1088 | switch (flag) { | |
1089 | case M_INSERT: /* insert item into S[0] */ | |
fba4ebb5 | 1090 | buffer_info_init_tbS0(tb, &bi); |
b49fb112 JM |
1091 | leaf_insert_into_buf(&bi, tb->item_pos, ih, |
1092 | body, tb->zeroes_num); | |
bd4c625c LT |
1093 | |
1094 | /* If we insert the first key change the delimiting key */ | |
b49fb112 | 1095 | if (tb->item_pos == 0) { |
bd4c625c | 1096 | if (tb->CFL[0]) /* can be 0 in reiserfsck */ |
416e2abd | 1097 | replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); |
1da177e4 | 1098 | } |
bd4c625c LT |
1099 | break; |
1100 | ||
1101 | case M_PASTE:{ /* append item in S[0] */ | |
1102 | struct item_head *pasted; | |
1103 | ||
b49fb112 | 1104 | pasted = item_head(tbS0, tb->item_pos); |
bd4c625c LT |
1105 | /* when directory, may be new entry already pasted */ |
1106 | if (is_direntry_le_ih(pasted)) { | |
b49fb112 | 1107 | if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) { |
bd4c625c LT |
1108 | |
1109 | RFALSE(!tb->insert_size[0], | |
1110 | "PAP-12260: insert_size is 0 already"); | |
1111 | ||
1112 | /* prepare space */ | |
fba4ebb5 | 1113 | buffer_info_init_tbS0(tb, &bi); |
b49fb112 | 1114 | leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item, |
416e2abd | 1115 | tb->insert_size[0], body, |
b49fb112 | 1116 | tb->zeroes_num); |
bd4c625c LT |
1117 | |
1118 | /* paste entry */ | |
b49fb112 | 1119 | leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1, |
416e2abd DJ |
1120 | (struct reiserfs_de_head *)body, |
1121 | body + DEH_SIZE, | |
1122 | tb->insert_size[0]); | |
b49fb112 | 1123 | if (!tb->item_pos && !tb->pos_in_item) { |
416e2abd | 1124 | RFALSE(!tb->CFL[0] || !tb->L[0], |
bd4c625c | 1125 | "PAP-12270: CFL[0]/L[0] must be specified"); |
416e2abd DJ |
1126 | if (tb->CFL[0]) |
1127 | replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); | |
bd4c625c LT |
1128 | } |
1129 | tb->insert_size[0] = 0; | |
1130 | } | |
1131 | } else { /* regular object */ | |
b49fb112 | 1132 | if (tb->pos_in_item == ih_item_len(pasted)) { |
bd4c625c LT |
1133 | |
1134 | RFALSE(tb->insert_size[0] <= 0, | |
1135 | "PAP-12275: insert size must not be %d", | |
1136 | tb->insert_size[0]); | |
fba4ebb5 | 1137 | buffer_info_init_tbS0(tb, &bi); |
b49fb112 JM |
1138 | leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item, |
1139 | tb->insert_size[0], body, tb->zeroes_num); | |
bd4c625c LT |
1140 | |
1141 | if (is_indirect_le_ih(pasted)) { | |
1da177e4 | 1142 | #if 0 |
bd4c625c LT |
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]); | |
1da177e4 | 1150 | #endif |
416e2abd | 1151 | set_ih_free_space(pasted, 0); |
bd4c625c LT |
1152 | } |
1153 | tb->insert_size[0] = 0; | |
1154 | } | |
1da177e4 | 1155 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
1156 | else { |
1157 | if (tb->insert_size[0]) { | |
1158 | print_cur_tb("12285"); | |
416e2abd | 1159 | reiserfs_panic(tb->tb_sb, |
c3a9c210 JM |
1160 | "PAP-12285", |
1161 | "insert_size " | |
1162 | "must be 0 " | |
1163 | "(%d)", | |
1164 | tb->insert_size[0]); | |
bd4c625c LT |
1165 | } |
1166 | } | |
1167 | #endif /* CONFIG_REISERFS_CHECK */ | |
1168 | ||
1169 | } | |
1170 | } /* case M_PASTE: */ | |
1da177e4 | 1171 | } |
1da177e4 | 1172 | } |
1da177e4 | 1173 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
1174 | if (flag == M_PASTE && tb->insert_size[0]) { |
1175 | print_cur_tb("12290"); | |
1176 | reiserfs_panic(tb->tb_sb, | |
c3a9c210 | 1177 | "PAP-12290", "insert_size is still not 0 (%d)", |
bd4c625c LT |
1178 | tb->insert_size[0]); |
1179 | } | |
97fd4b97 JM |
1180 | #endif |
1181 | ||
1182 | /* Leaf level of the tree is balanced (end of balance_leaf) */ | |
bd4c625c | 1183 | return 0; |
97fd4b97 | 1184 | } |
1da177e4 LT |
1185 | |
1186 | /* Make empty node */ | |
bd4c625c | 1187 | void make_empty_node(struct buffer_info *bi) |
1da177e4 | 1188 | { |
bd4c625c | 1189 | struct block_head *blkh; |
1da177e4 | 1190 | |
bd4c625c | 1191 | RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL"); |
1da177e4 | 1192 | |
bd4c625c LT |
1193 | 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)); | |
1da177e4 | 1196 | |
bd4c625c LT |
1197 | if (bi->bi_parent) |
1198 | B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */ | |
1da177e4 LT |
1199 | } |
1200 | ||
1da177e4 | 1201 | /* Get first empty buffer */ |
bd4c625c | 1202 | struct buffer_head *get_FEB(struct tree_balance *tb) |
1da177e4 | 1203 | { |
bd4c625c | 1204 | int i; |
bd4c625c | 1205 | struct buffer_info bi; |
1da177e4 | 1206 | |
bd4c625c | 1207 | for (i = 0; i < MAX_FEB_SIZE; i++) |
9dce07f1 | 1208 | if (tb->FEB[i] != NULL) |
bd4c625c LT |
1209 | break; |
1210 | ||
1211 | if (i == MAX_FEB_SIZE) | |
c3a9c210 | 1212 | reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty"); |
bd4c625c | 1213 | |
fba4ebb5 | 1214 | buffer_info_init_bh(tb, &bi, tb->FEB[i]); |
bd4c625c | 1215 | make_empty_node(&bi); |
fba4ebb5 JM |
1216 | set_buffer_uptodate(tb->FEB[i]); |
1217 | tb->used[i] = tb->FEB[i]; | |
bd4c625c | 1218 | tb->FEB[i] = NULL; |
bd4c625c | 1219 | |
fba4ebb5 | 1220 | return tb->used[i]; |
bd4c625c | 1221 | } |
1da177e4 | 1222 | |
098297b2 | 1223 | /* This is now used because reiserfs_free_block has to be able to schedule. */ |
bd4c625c | 1224 | static void store_thrown(struct tree_balance *tb, struct buffer_head *bh) |
1da177e4 | 1225 | { |
bd4c625c LT |
1226 | int i; |
1227 | ||
1228 | if (buffer_dirty(bh)) | |
45b03d5e JM |
1229 | reiserfs_warning(tb->tb_sb, "reiserfs-12320", |
1230 | "called with dirty buffer"); | |
79a81aef | 1231 | for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) |
bd4c625c LT |
1232 | if (!tb->thrown[i]) { |
1233 | tb->thrown[i] = bh; | |
1234 | get_bh(bh); /* free_thrown puts this */ | |
1235 | return; | |
1236 | } | |
45b03d5e JM |
1237 | reiserfs_warning(tb->tb_sb, "reiserfs-12321", |
1238 | "too many thrown buffers"); | |
1da177e4 LT |
1239 | } |
1240 | ||
bd4c625c LT |
1241 | static void free_thrown(struct tree_balance *tb) |
1242 | { | |
1243 | int i; | |
1244 | b_blocknr_t blocknr; | |
79a81aef | 1245 | for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) { |
bd4c625c LT |
1246 | if (tb->thrown[i]) { |
1247 | blocknr = tb->thrown[i]->b_blocknr; | |
1248 | if (buffer_dirty(tb->thrown[i])) | |
45b03d5e JM |
1249 | reiserfs_warning(tb->tb_sb, "reiserfs-12322", |
1250 | "called with dirty buffer %d", | |
bd4c625c LT |
1251 | blocknr); |
1252 | brelse(tb->thrown[i]); /* incremented in store_thrown */ | |
1253 | reiserfs_free_block(tb->transaction_handle, NULL, | |
1254 | blocknr, 0); | |
1255 | } | |
1da177e4 | 1256 | } |
1da177e4 LT |
1257 | } |
1258 | ||
bd4c625c | 1259 | void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh) |
1da177e4 | 1260 | { |
bd4c625c LT |
1261 | 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); | |
1da177e4 LT |
1268 | } |
1269 | ||
1270 | /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/ | |
bd4c625c LT |
1271 | void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest, |
1272 | struct buffer_head *src, int n_src) | |
1da177e4 LT |
1273 | { |
1274 | ||
bd4c625c LT |
1275 | 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)); | |
1286 | ||
1287 | if (B_IS_ITEMS_LEVEL(src)) | |
1288 | /* source buffer contains leaf node */ | |
4cf5f7ad | 1289 | memcpy(internal_key(dest, n_dest), item_head(src, n_src), |
bd4c625c LT |
1290 | KEY_SIZE); |
1291 | else | |
4cf5f7ad | 1292 | memcpy(internal_key(dest, n_dest), internal_key(src, n_src), |
bd4c625c LT |
1293 | KEY_SIZE); |
1294 | ||
1295 | do_balance_mark_internal_dirty(tb, dest, 0); | |
1da177e4 LT |
1296 | } |
1297 | ||
bd4c625c | 1298 | int get_left_neighbor_position(struct tree_balance *tb, int h) |
1da177e4 | 1299 | { |
bd4c625c | 1300 | int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1); |
1da177e4 | 1301 | |
9dce07f1 | 1302 | RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL, |
bd4c625c LT |
1303 | "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)); | |
1da177e4 | 1305 | |
bd4c625c LT |
1306 | if (Sh_position == 0) |
1307 | return B_NR_ITEMS(tb->FL[h]); | |
1308 | else | |
1309 | return Sh_position - 1; | |
1da177e4 LT |
1310 | } |
1311 | ||
bd4c625c | 1312 | int get_right_neighbor_position(struct tree_balance *tb, int h) |
1da177e4 | 1313 | { |
bd4c625c | 1314 | int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1); |
1da177e4 | 1315 | |
9dce07f1 | 1316 | RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL, |
bd4c625c LT |
1317 | "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]); | |
1da177e4 | 1319 | |
bd4c625c LT |
1320 | if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h))) |
1321 | return 0; | |
1322 | else | |
1323 | return Sh_position + 1; | |
1da177e4 LT |
1324 | } |
1325 | ||
1da177e4 LT |
1326 | #ifdef CONFIG_REISERFS_CHECK |
1327 | ||
bd4c625c LT |
1328 | int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value); |
1329 | static void check_internal_node(struct super_block *s, struct buffer_head *bh, | |
1330 | char *mes) | |
1da177e4 | 1331 | { |
bd4c625c LT |
1332 | struct disk_child *dc; |
1333 | int i; | |
1da177e4 | 1334 | |
bd4c625c | 1335 | RFALSE(!bh, "PAP-12336: bh == 0"); |
1da177e4 | 1336 | |
bd4c625c LT |
1337 | if (!bh || !B_IS_IN_TREE(bh)) |
1338 | return; | |
1da177e4 | 1339 | |
bd4c625c LT |
1340 | 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); | |
1da177e4 | 1344 | |
bd4c625c LT |
1345 | 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); | |
c3a9c210 JM |
1348 | reiserfs_panic(s, "PAP-12338", |
1349 | "invalid child pointer %y in %b", | |
bd4c625c LT |
1350 | dc, bh); |
1351 | } | |
1352 | } | |
1da177e4 LT |
1353 | } |
1354 | ||
45b03d5e JM |
1355 | static int locked_or_not_in_tree(struct tree_balance *tb, |
1356 | struct buffer_head *bh, char *which) | |
bd4c625c LT |
1357 | { |
1358 | if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) || | |
1359 | !B_IS_IN_TREE(bh)) { | |
45b03d5e | 1360 | reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh); |
bd4c625c LT |
1361 | return 1; |
1362 | } | |
1363 | return 0; | |
1364 | } | |
1da177e4 | 1365 | |
bd4c625c | 1366 | static int check_before_balancing(struct tree_balance *tb) |
1da177e4 | 1367 | { |
bd4c625c LT |
1368 | int retval = 0; |
1369 | ||
08f14fc8 | 1370 | if (REISERFS_SB(tb->tb_sb)->cur_tb) { |
c3a9c210 JM |
1371 | 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 " | |
08f14fc8 FW |
1374 | "handle concurrent tree accesses on a same " |
1375 | "mount point."); | |
1da177e4 | 1376 | } |
bd4c625c | 1377 | |
098297b2 JM |
1378 | /* |
1379 | * double check that buffers that we will modify are unlocked. | |
1380 | * (fix_nodes should already have prepped all of these for us). | |
1381 | */ | |
bd4c625c | 1382 | if (tb->lnum[0]) { |
45b03d5e JM |
1383 | 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]"); | |
bd4c625c | 1386 | check_leaf(tb->L[0]); |
1da177e4 | 1387 | } |
bd4c625c | 1388 | if (tb->rnum[0]) { |
45b03d5e JM |
1389 | 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]"); | |
bd4c625c LT |
1392 | check_leaf(tb->R[0]); |
1393 | } | |
45b03d5e JM |
1394 | retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path), |
1395 | "S[0]"); | |
bd4c625c | 1396 | check_leaf(PATH_PLAST_BUFFER(tb->tb_path)); |
1da177e4 | 1397 | |
bd4c625c LT |
1398 | return retval; |
1399 | } | |
1da177e4 | 1400 | |
bd4c625c | 1401 | static void check_after_balance_leaf(struct tree_balance *tb) |
1da177e4 | 1402 | { |
bd4c625c LT |
1403 | 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"); | |
c3a9c210 JM |
1409 | reiserfs_panic(tb->tb_sb, "PAP-12355", |
1410 | "shift to left was incorrect"); | |
bd4c625c LT |
1411 | } |
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"); | |
c3a9c210 JM |
1419 | reiserfs_panic(tb->tb_sb, "PAP-12360", |
1420 | "shift to right was incorrect"); | |
bd4c625c LT |
1421 | } |
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"); | |
45b03d5e | 1434 | reiserfs_warning(tb->tb_sb, "reiserfs-12363", |
bd4c625c LT |
1435 | "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); | |
c3a9c210 | 1445 | reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect"); |
bd4c625c | 1446 | } |
1da177e4 LT |
1447 | } |
1448 | ||
bd4c625c | 1449 | static void check_leaf_level(struct tree_balance *tb) |
1da177e4 | 1450 | { |
bd4c625c LT |
1451 | check_leaf(tb->L[0]); |
1452 | check_leaf(tb->R[0]); | |
1453 | check_leaf(PATH_PLAST_BUFFER(tb->tb_path)); | |
1454 | } | |
1da177e4 | 1455 | |
bd4c625c LT |
1456 | static 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 | } | |
1da177e4 LT |
1469 | |
1470 | } | |
1471 | ||
1472 | #endif | |
1473 | ||
098297b2 JM |
1474 | /* |
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 | */ | |
1da177e4 | 1482 | |
098297b2 JM |
1483 | /* |
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 | */ | |
1da177e4 | 1507 | |
bd4c625c | 1508 | static inline void do_balance_starts(struct tree_balance *tb) |
1da177e4 | 1509 | { |
098297b2 | 1510 | /* use print_cur_tb() to see initial state of struct tree_balance */ |
1da177e4 | 1511 | |
bd4c625c | 1512 | /* store_print_tb (tb); */ |
1da177e4 | 1513 | |
bd4c625c | 1514 | /* do not delete, just comment it out */ |
098297b2 JM |
1515 | /* |
1516 | print_tb(flag, PATH_LAST_POSITION(tb->tb_path), | |
1517 | tb->tb_path->pos_in_item, tb, "check"); | |
1518 | */ | |
bd4c625c | 1519 | RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB"); |
1da177e4 | 1520 | #ifdef CONFIG_REISERFS_CHECK |
08f14fc8 | 1521 | REISERFS_SB(tb->tb_sb)->cur_tb = tb; |
1da177e4 LT |
1522 | #endif |
1523 | } | |
1524 | ||
bd4c625c | 1525 | static inline void do_balance_completed(struct tree_balance *tb) |
1da177e4 | 1526 | { |
bd4c625c | 1527 | |
1da177e4 | 1528 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
1529 | check_leaf_level(tb); |
1530 | check_internal_levels(tb); | |
08f14fc8 | 1531 | REISERFS_SB(tb->tb_sb)->cur_tb = NULL; |
1da177e4 LT |
1532 | #endif |
1533 | ||
098297b2 JM |
1534 | /* |
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 | |
bd4c625c | 1538 | */ |
1da177e4 | 1539 | |
bd4c625c | 1540 | REISERFS_SB(tb->tb_sb)->s_do_balance++; |
1da177e4 | 1541 | |
bd4c625c LT |
1542 | /* release all nodes hold to perform the balancing */ |
1543 | unfix_nodes(tb); | |
1da177e4 | 1544 | |
bd4c625c | 1545 | free_thrown(tb); |
1da177e4 LT |
1546 | } |
1547 | ||
098297b2 JM |
1548 | /* |
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 | */ | |
1566 | void 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 */ | |
1571 | ||
1572 | /* | |
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]; | |
1579 | ||
1580 | /* inserted node-ptrs for the next level */ | |
1581 | struct buffer_head *insert_ptr[2]; | |
bd4c625c LT |
1582 | |
1583 | tb->tb_mode = flag; | |
1584 | tb->need_balance_dirty = 0; | |
1585 | ||
1586 | if (FILESYSTEM_CHANGED_TB(tb)) { | |
c3a9c210 JM |
1587 | reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has " |
1588 | "changed"); | |
bd4c625c LT |
1589 | } |
1590 | /* if we have no real work to do */ | |
1591 | if (!tb->insert_size[0]) { | |
45b03d5e JM |
1592 | reiserfs_warning(tb->tb_sb, "PAP-12350", |
1593 | "insert_size == 0, mode == %c", flag); | |
bd4c625c LT |
1594 | unfix_nodes(tb); |
1595 | return; | |
1596 | } | |
1da177e4 | 1597 | |
a228bf8f | 1598 | atomic_inc(&fs_generation(tb->tb_sb)); |
bd4c625c | 1599 | do_balance_starts(tb); |
1da177e4 | 1600 | |
098297b2 JM |
1601 | /* |
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 | */ | |
bd4c625c LT |
1606 | child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) + |
1607 | balance_leaf(tb, ih, body, flag, insert_key, insert_ptr); | |
1da177e4 LT |
1608 | |
1609 | #ifdef CONFIG_REISERFS_CHECK | |
bd4c625c | 1610 | check_after_balance_leaf(tb); |
1da177e4 LT |
1611 | #endif |
1612 | ||
bd4c625c LT |
1613 | /* 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); | |
1da177e4 | 1617 | |
bd4c625c | 1618 | do_balance_completed(tb); |
1da177e4 LT |
1619 | |
1620 | } |