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 | { | |
4bf4de6b | 296 | int ret; |
f1f007c3 JM |
297 | struct buffer_info bi; |
298 | int n = B_NR_ITEMS(tb->L[0]); | |
299 | ||
4bf4de6b JM |
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, shift; | |
303 | int version; | |
304 | ||
305 | ret = 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 | ||
310 | /* Calculate and check item length to insert to L[0] */ | |
311 | put_ih_item_len(ih, ih_item_len(ih) - new_item_len); | |
f1f007c3 | 312 | |
4bf4de6b JM |
313 | RFALSE(ih_item_len(ih) <= 0, |
314 | "PAP-12080: there is nothing to insert into L[0]: " | |
315 | "ih_item_len=%d", ih_item_len(ih)); | |
316 | ||
317 | /* Insert new item into L[0] */ | |
318 | buffer_info_init_left(tb, &bi); | |
319 | leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body, | |
320 | min_t(int, tb->zeroes_num, ih_item_len(ih))); | |
321 | ||
322 | version = ih_version(ih); | |
323 | ||
324 | /* | |
325 | * Calculate key component, item length and body to | |
326 | * insert into S[0] | |
327 | */ | |
328 | shift = 0; | |
329 | if (is_indirect_le_ih(ih)) | |
330 | shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT; | |
331 | ||
332 | add_le_ih_k_offset(ih, tb->lbytes << shift); | |
333 | ||
334 | put_ih_item_len(ih, new_item_len); | |
335 | if (tb->lbytes > tb->zeroes_num) { | |
336 | body += (tb->lbytes - tb->zeroes_num); | |
337 | tb->zeroes_num = 0; | |
338 | } else | |
339 | tb->zeroes_num -= tb->lbytes; | |
340 | ||
341 | RFALSE(ih_item_len(ih) <= 0, | |
342 | "PAP-12085: there is nothing to insert into S[0]: " | |
343 | "ih_item_len=%d", ih_item_len(ih)); | |
344 | } else { | |
345 | /* new item in whole falls into L[0] */ | |
346 | /* Shift lnum[0]-1 items to L[0] */ | |
347 | ret = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes); | |
348 | ||
349 | /* Insert new item into L[0] */ | |
350 | buffer_info_init_left(tb, &bi); | |
351 | leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body, | |
352 | tb->zeroes_num); | |
353 | tb->insert_size[0] = 0; | |
354 | tb->zeroes_num = 0; | |
355 | } | |
f1f007c3 JM |
356 | } |
357 | ||
3fade937 JM |
358 | static void balance_leaf_paste_left_shift_dirent(struct tree_balance *tb, |
359 | struct item_head *ih, | |
360 | const char *body) | |
361 | { | |
362 | int n = B_NR_ITEMS(tb->L[0]); | |
363 | struct buffer_info bi; | |
364 | ||
365 | RFALSE(tb->zeroes_num, | |
366 | "PAP-12090: invalid parameter in case of a directory"); | |
367 | ||
368 | /* directory item */ | |
369 | if (tb->lbytes > tb->pos_in_item) { | |
370 | /* new directory entry falls into L[0] */ | |
371 | struct item_head *pasted; | |
372 | int ret, l_pos_in_item = tb->pos_in_item; | |
373 | ||
374 | /* | |
375 | * Shift lnum[0] - 1 items in whole. | |
376 | * Shift lbytes - 1 entries from given directory item | |
377 | */ | |
378 | ret = leaf_shift_left(tb, tb->lnum[0], tb->lbytes - 1); | |
379 | if (ret && !tb->item_pos) { | |
380 | pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1); | |
381 | l_pos_in_item += ih_entry_count(pasted) - | |
382 | (tb->lbytes - 1); | |
383 | } | |
384 | ||
385 | /* Append given directory entry to directory item */ | |
386 | buffer_info_init_left(tb, &bi); | |
387 | leaf_paste_in_buffer(&bi, n + tb->item_pos - ret, | |
388 | l_pos_in_item, tb->insert_size[0], | |
389 | body, tb->zeroes_num); | |
390 | ||
391 | /* | |
392 | * previous string prepared space for pasting new entry, | |
393 | * following string pastes this entry | |
394 | */ | |
395 | ||
396 | /* | |
397 | * when we have merge directory item, pos_in_item | |
398 | * has been changed too | |
399 | */ | |
400 | ||
401 | /* paste new directory entry. 1 is entry number */ | |
402 | leaf_paste_entries(&bi, n + tb->item_pos - ret, | |
403 | l_pos_in_item, 1, | |
404 | (struct reiserfs_de_head *) body, | |
405 | body + DEH_SIZE, tb->insert_size[0]); | |
406 | tb->insert_size[0] = 0; | |
407 | } else { | |
408 | /* new directory item doesn't fall into L[0] */ | |
409 | /* | |
410 | * Shift lnum[0]-1 items in whole. Shift lbytes | |
411 | * directory entries from directory item number lnum[0] | |
412 | */ | |
413 | leaf_shift_left(tb, tb->lnum[0], tb->lbytes); | |
414 | } | |
415 | ||
416 | /* Calculate new position to append in item body */ | |
417 | tb->pos_in_item -= tb->lbytes; | |
418 | } | |
419 | ||
420 | static void balance_leaf_paste_left_shift(struct tree_balance *tb, | |
421 | struct item_head *ih, | |
422 | const char *body) | |
1da177e4 | 423 | { |
bd4c625c | 424 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
3fade937 | 425 | int n = B_NR_ITEMS(tb->L[0]); |
cf22df18 | 426 | struct buffer_info bi; |
3fade937 JM |
427 | |
428 | if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { | |
429 | balance_leaf_paste_left_shift_dirent(tb, ih, body); | |
430 | return; | |
431 | } | |
432 | ||
433 | RFALSE(tb->lbytes <= 0, | |
434 | "PAP-12095: there is nothing to shift to L[0]. " | |
435 | "lbytes=%d", tb->lbytes); | |
436 | RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)), | |
437 | "PAP-12100: incorrect position to paste: " | |
438 | "item_len=%d, pos_in_item=%d", | |
439 | ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item); | |
440 | ||
441 | /* appended item will be in L[0] in whole */ | |
442 | if (tb->lbytes >= tb->pos_in_item) { | |
443 | struct item_head *tbS0_pos_ih, *tbL0_ih; | |
444 | struct item_head *tbS0_0_ih; | |
445 | struct reiserfs_key *left_delim_key; | |
446 | int ret, l_n, version, temp_l; | |
447 | ||
448 | tbS0_pos_ih = item_head(tbS0, tb->item_pos); | |
449 | tbS0_0_ih = item_head(tbS0, 0); | |
450 | ||
451 | /* | |
452 | * this bytes number must be appended | |
453 | * to the last item of L[h] | |
454 | */ | |
455 | l_n = tb->lbytes - tb->pos_in_item; | |
456 | ||
457 | /* Calculate new insert_size[0] */ | |
458 | tb->insert_size[0] -= l_n; | |
459 | ||
460 | RFALSE(tb->insert_size[0] <= 0, | |
461 | "PAP-12105: there is nothing to paste into " | |
462 | "L[0]. insert_size=%d", tb->insert_size[0]); | |
463 | ||
464 | ret = leaf_shift_left(tb, tb->lnum[0], | |
465 | ih_item_len(tbS0_pos_ih)); | |
466 | ||
467 | tbL0_ih = item_head(tb->L[0], n + tb->item_pos - ret); | |
468 | ||
469 | /* Append to body of item in L[0] */ | |
470 | buffer_info_init_left(tb, &bi); | |
471 | leaf_paste_in_buffer(&bi, n + tb->item_pos - ret, | |
472 | ih_item_len(tbL0_ih), l_n, body, | |
473 | min_t(int, l_n, tb->zeroes_num)); | |
474 | ||
475 | /* | |
476 | * 0-th item in S0 can be only of DIRECT type | |
477 | * when l_n != 0 | |
478 | */ | |
479 | temp_l = l_n; | |
480 | ||
481 | RFALSE(ih_item_len(tbS0_0_ih), | |
482 | "PAP-12106: item length must be 0"); | |
483 | RFALSE(comp_short_le_keys(&tbS0_0_ih->ih_key, | |
484 | leaf_key(tb->L[0], n + tb->item_pos - ret)), | |
485 | "PAP-12107: items must be of the same file"); | |
486 | ||
487 | if (is_indirect_le_ih(tbL0_ih)) { | |
488 | int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT; | |
489 | temp_l = l_n << shift; | |
490 | } | |
491 | /* update key of first item in S0 */ | |
492 | version = ih_version(tbS0_0_ih); | |
493 | add_le_key_k_offset(version, &tbS0_0_ih->ih_key, temp_l); | |
494 | ||
495 | /* update left delimiting key */ | |
496 | left_delim_key = internal_key(tb->CFL[0], tb->lkey[0]); | |
497 | add_le_key_k_offset(version, left_delim_key, temp_l); | |
498 | ||
499 | /* | |
500 | * Calculate new body, position in item and | |
501 | * insert_size[0] | |
502 | */ | |
503 | if (l_n > tb->zeroes_num) { | |
504 | body += (l_n - tb->zeroes_num); | |
505 | tb->zeroes_num = 0; | |
506 | } else | |
507 | tb->zeroes_num -= l_n; | |
508 | tb->pos_in_item = 0; | |
509 | ||
510 | RFALSE(comp_short_le_keys(&tbS0_0_ih->ih_key, | |
511 | leaf_key(tb->L[0], | |
512 | B_NR_ITEMS(tb->L[0]) - 1)) || | |
513 | !op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size) || | |
514 | !op_is_left_mergeable(left_delim_key, tbS0->b_size), | |
515 | "PAP-12120: item must be merge-able with left " | |
516 | "neighboring item"); | |
517 | } else { | |
518 | /* only part of the appended item will be in L[0] */ | |
519 | ||
520 | /* Calculate position in item for append in S[0] */ | |
521 | tb->pos_in_item -= tb->lbytes; | |
522 | ||
523 | RFALSE(tb->pos_in_item <= 0, | |
524 | "PAP-12125: no place for paste. pos_in_item=%d", | |
525 | tb->pos_in_item); | |
526 | ||
527 | /* | |
528 | * Shift lnum[0] - 1 items in whole. | |
529 | * Shift lbytes - 1 byte from item number lnum[0] | |
530 | */ | |
531 | leaf_shift_left(tb, tb->lnum[0], tb->lbytes); | |
532 | } | |
533 | } | |
534 | ||
535 | ||
536 | /* appended item will be in L[0] in whole */ | |
537 | static void balance_leaf_paste_left_whole(struct tree_balance *tb, | |
538 | struct item_head *ih, | |
539 | const char *body) | |
540 | { | |
541 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
cf22df18 | 542 | int n = B_NR_ITEMS(tb->L[0]); |
3fade937 JM |
543 | struct buffer_info bi; |
544 | struct item_head *pasted; | |
545 | int ret; | |
bd4c625c | 546 | |
3fade937 JM |
547 | /* if we paste into first item of S[0] and it is left mergable */ |
548 | if (!tb->item_pos && | |
549 | op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)) { | |
550 | /* | |
551 | * then increment pos_in_item by the size of the | |
552 | * last item in L[0] | |
553 | */ | |
554 | pasted = item_head(tb->L[0], n - 1); | |
555 | if (is_direntry_le_ih(pasted)) | |
556 | tb->pos_in_item += ih_entry_count(pasted); | |
557 | else | |
558 | tb->pos_in_item += ih_item_len(pasted); | |
559 | } | |
560 | ||
561 | /* | |
562 | * Shift lnum[0] - 1 items in whole. | |
563 | * Shift lbytes - 1 byte from item number lnum[0] | |
564 | */ | |
565 | ret = leaf_shift_left(tb, tb->lnum[0], tb->lbytes); | |
566 | ||
567 | /* Append to body of item in L[0] */ | |
568 | buffer_info_init_left(tb, &bi); | |
569 | leaf_paste_in_buffer(&bi, n + tb->item_pos - ret, tb->pos_in_item, | |
570 | tb->insert_size[0], body, tb->zeroes_num); | |
571 | ||
572 | /* if appended item is directory, paste entry */ | |
573 | pasted = item_head(tb->L[0], n + tb->item_pos - ret); | |
574 | if (is_direntry_le_ih(pasted)) | |
575 | leaf_paste_entries(&bi, n + tb->item_pos - ret, | |
576 | tb->pos_in_item, 1, | |
577 | (struct reiserfs_de_head *)body, | |
578 | body + DEH_SIZE, tb->insert_size[0]); | |
579 | ||
580 | /* | |
581 | * if appended item is indirect item, put unformatted node | |
582 | * into un list | |
583 | */ | |
584 | if (is_indirect_le_ih(pasted)) | |
585 | set_ih_free_space(pasted, 0); | |
cf22df18 | 586 | |
3fade937 JM |
587 | tb->insert_size[0] = 0; |
588 | tb->zeroes_num = 0; | |
589 | } | |
590 | ||
591 | static void balance_leaf_paste_left(struct tree_balance *tb, | |
592 | struct item_head *ih, const char *body) | |
593 | { | |
594 | /* we must shift the part of the appended item */ | |
595 | if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) | |
596 | balance_leaf_paste_left_shift(tb, ih, body); | |
597 | else | |
598 | balance_leaf_paste_left_whole(tb, ih, body); | |
cf22df18 JM |
599 | } |
600 | ||
0080e9f9 JM |
601 | /* Shift lnum[0] items from S[0] to the left neighbor L[0] */ |
602 | static void balance_leaf_left(struct tree_balance *tb, struct item_head *ih, | |
603 | const char *body, int flag) | |
604 | { | |
605 | if (tb->lnum[0] <= 0) | |
606 | return; | |
607 | ||
608 | /* new item or it part falls to L[0], shift it too */ | |
609 | if (tb->item_pos < tb->lnum[0]) { | |
610 | BUG_ON(flag != M_INSERT && flag != M_PASTE); | |
611 | ||
612 | if (flag == M_INSERT) | |
613 | balance_leaf_insert_left(tb, ih, body); | |
614 | else /* M_PASTE */ | |
615 | balance_leaf_paste_left(tb, ih, body); | |
616 | } else | |
617 | /* new item doesn't fall into L[0] */ | |
618 | leaf_shift_left(tb, tb->lnum[0], tb->lbytes); | |
619 | } | |
620 | ||
621 | ||
e80ef3d1 JM |
622 | static void balance_leaf_insert_right(struct tree_balance *tb, |
623 | struct item_head *ih, const char *body) | |
624 | { | |
625 | ||
626 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
627 | int n = B_NR_ITEMS(tbS0); | |
628 | struct buffer_info bi; | |
629 | int ret_val; | |
630 | if (n - tb->rnum[0] < tb->item_pos) { /* new item or its part falls to R[0] */ | |
631 | if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { /* part of new item falls into R[0] */ | |
632 | loff_t old_key_comp, old_len, r_zeroes_number; | |
633 | const char *r_body; | |
634 | int version; | |
635 | loff_t offset; | |
636 | ||
637 | leaf_shift_right(tb, tb->rnum[0] - 1, -1); | |
638 | ||
639 | version = ih_version(ih); | |
640 | /* Remember key component and item length */ | |
641 | old_key_comp = le_ih_k_offset(ih); | |
642 | old_len = ih_item_len(ih); | |
643 | ||
644 | /* Calculate key component and item length to insert into R[0] */ | |
645 | 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)); | |
646 | set_le_ih_k_offset(ih, offset); | |
647 | put_ih_item_len(ih, tb->rbytes); | |
648 | /* Insert part of the item into R[0] */ | |
649 | buffer_info_init_right(tb, &bi); | |
650 | if ((old_len - tb->rbytes) > tb->zeroes_num) { | |
651 | r_zeroes_number = 0; | |
652 | r_body = body + (old_len - tb->rbytes) - tb->zeroes_num; | |
653 | } else { | |
654 | r_body = body; | |
655 | r_zeroes_number = tb->zeroes_num - (old_len - tb->rbytes); | |
656 | tb->zeroes_num -= r_zeroes_number; | |
657 | } | |
658 | ||
659 | leaf_insert_into_buf(&bi, 0, ih, r_body, | |
660 | r_zeroes_number); | |
661 | ||
662 | /* Replace right delimiting key by first key in R[0] */ | |
663 | replace_key(tb, tb->CFR[0], tb->rkey[0], | |
664 | tb->R[0], 0); | |
665 | ||
666 | /* Calculate key component and item length to insert into S[0] */ | |
667 | set_le_ih_k_offset(ih, old_key_comp); | |
668 | put_ih_item_len(ih, old_len - tb->rbytes); | |
669 | ||
670 | tb->insert_size[0] -= tb->rbytes; | |
671 | ||
672 | } else { /* whole new item falls into R[0] */ | |
673 | ||
674 | /* Shift rnum[0]-1 items to R[0] */ | |
675 | ret_val = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes); | |
676 | /* Insert new item into R[0] */ | |
677 | buffer_info_init_right(tb, &bi); | |
678 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1, | |
679 | ih, body, tb->zeroes_num); | |
680 | ||
681 | if (tb->item_pos - n + tb->rnum[0] - 1 == 0) { | |
682 | replace_key(tb, tb->CFR[0], | |
683 | tb->rkey[0], | |
684 | tb->R[0], 0); | |
685 | ||
686 | } | |
687 | tb->zeroes_num = tb->insert_size[0] = 0; | |
688 | } | |
689 | } else { /* new item or part of it doesn't fall into R[0] */ | |
690 | ||
691 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); | |
692 | } | |
693 | ||
694 | } | |
695 | ||
3f0eb276 JM |
696 | static void balance_leaf_paste_right(struct tree_balance *tb, |
697 | struct item_head *ih, const char *body) | |
cf22df18 JM |
698 | { |
699 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
3f0eb276 | 700 | int n = B_NR_ITEMS(tbS0); |
cf22df18 | 701 | struct buffer_info bi; |
cf22df18 JM |
702 | int ret_val; |
703 | ||
b49fb112 JM |
704 | if (n - tb->rnum[0] <= tb->item_pos) { /* pasted item or part of it falls to R[0] */ |
705 | if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) { /* we must shift the part of the appended item */ | |
706 | if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { /* we append to directory item */ | |
bd4c625c LT |
707 | int entry_count; |
708 | ||
b49fb112 | 709 | RFALSE(tb->zeroes_num, |
bd4c625c | 710 | "PAP-12145: invalid parameter in case of a directory"); |
4cf5f7ad | 711 | entry_count = ih_entry_count(item_head |
b49fb112 | 712 | (tbS0, tb->item_pos)); |
bd4c625c | 713 | if (entry_count - tb->rbytes < |
b49fb112 | 714 | tb->pos_in_item) |
bd4c625c LT |
715 | /* new directory entry falls into R[0] */ |
716 | { | |
717 | int paste_entry_position; | |
718 | ||
416e2abd | 719 | RFALSE(tb->rbytes - 1 >= entry_count || !tb-> insert_size[0], |
bd4c625c | 720 | "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d", |
416e2abd | 721 | tb->rbytes, entry_count); |
bd4c625c | 722 | /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */ |
416e2abd | 723 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1); |
bd4c625c | 724 | /* Paste given directory entry to directory item */ |
b49fb112 | 725 | paste_entry_position = tb->pos_in_item - entry_count + tb->rbytes - 1; |
fba4ebb5 | 726 | buffer_info_init_right(tb, &bi); |
b49fb112 | 727 | leaf_paste_in_buffer(&bi, 0, paste_entry_position, tb->insert_size[0], body, tb->zeroes_num); |
bd4c625c | 728 | /* paste entry */ |
416e2abd DJ |
729 | leaf_paste_entries(&bi, 0, paste_entry_position, 1, |
730 | (struct reiserfs_de_head *) body, | |
731 | body + DEH_SIZE, tb->insert_size[0]); | |
732 | ||
733 | if (paste_entry_position == 0) { | |
bd4c625c | 734 | /* change delimiting keys */ |
416e2abd | 735 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0],0); |
bd4c625c LT |
736 | } |
737 | ||
738 | tb->insert_size[0] = 0; | |
b49fb112 | 739 | tb->pos_in_item++; |
bd4c625c LT |
740 | } else { /* new directory entry doesn't fall into R[0] */ |
741 | ||
416e2abd | 742 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
bd4c625c LT |
743 | } |
744 | } else { /* regular object */ | |
745 | ||
b49fb112 | 746 | int n_shift, n_rem, r_zeroes_number; |
bd4c625c LT |
747 | const char *r_body; |
748 | ||
749 | /* Calculate number of bytes which must be shifted from appended item */ | |
416e2abd | 750 | if ((n_shift = tb->rbytes - tb->insert_size[0]) < 0) |
bd4c625c LT |
751 | n_shift = 0; |
752 | ||
b49fb112 JM |
753 | RFALSE(tb->pos_in_item != ih_item_len |
754 | (item_head(tbS0, tb->item_pos)), | |
bd4c625c | 755 | "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d", |
b49fb112 JM |
756 | tb->pos_in_item, ih_item_len |
757 | (item_head(tbS0, tb->item_pos))); | |
416e2abd DJ |
758 | |
759 | leaf_shift_right(tb, tb->rnum[0], n_shift); | |
bd4c625c | 760 | /* Calculate number of bytes which must remain in body after appending to R[0] */ |
416e2abd | 761 | if ((n_rem = tb->insert_size[0] - tb->rbytes) < 0) |
bd4c625c LT |
762 | n_rem = 0; |
763 | ||
764 | { | |
765 | int version; | |
416e2abd DJ |
766 | unsigned long temp_rem = n_rem; |
767 | ||
4cf5f7ad JM |
768 | version = ih_version(item_head(tb->R[0], 0)); |
769 | if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) { | |
416e2abd | 770 | temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT); |
bd4c625c | 771 | } |
4cf5f7ad JM |
772 | set_le_key_k_offset(version, leaf_key(tb->R[0], 0), |
773 | le_key_k_offset(version, leaf_key(tb->R[0], 0)) + temp_rem); | |
774 | set_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]), | |
775 | le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0])) + temp_rem); | |
bd4c625c | 776 | } |
4cf5f7ad JM |
777 | /* k_offset (leaf_key(tb->R[0],0)) += n_rem; |
778 | k_offset (internal_key(tb->CFR[0],tb->rkey[0])) += n_rem;*/ | |
416e2abd | 779 | do_balance_mark_internal_dirty(tb, tb->CFR[0], 0); |
bd4c625c LT |
780 | |
781 | /* Append part of body into R[0] */ | |
fba4ebb5 | 782 | buffer_info_init_right(tb, &bi); |
b49fb112 JM |
783 | if (n_rem > tb->zeroes_num) { |
784 | r_zeroes_number = 0; | |
785 | r_body = body + n_rem - tb->zeroes_num; | |
bd4c625c LT |
786 | } else { |
787 | r_body = body; | |
b49fb112 JM |
788 | r_zeroes_number = tb->zeroes_num - n_rem; |
789 | tb->zeroes_num -= r_zeroes_number; | |
bd4c625c LT |
790 | } |
791 | ||
416e2abd DJ |
792 | leaf_paste_in_buffer(&bi, 0, n_shift, |
793 | tb->insert_size[0] - n_rem, | |
b49fb112 | 794 | r_body, r_zeroes_number); |
416e2abd | 795 | |
4cf5f7ad | 796 | if (is_indirect_le_ih(item_head(tb->R[0], 0))) { |
1da177e4 | 797 | #if 0 |
bd4c625c LT |
798 | RFALSE(n_rem, |
799 | "PAP-12160: paste more than one unformatted node pointer"); | |
1da177e4 | 800 | #endif |
4cf5f7ad | 801 | set_ih_free_space(item_head(tb->R[0], 0), 0); |
bd4c625c LT |
802 | } |
803 | tb->insert_size[0] = n_rem; | |
804 | if (!n_rem) | |
b49fb112 | 805 | tb->pos_in_item++; |
bd4c625c LT |
806 | } |
807 | } else { /* pasted item in whole falls into R[0] */ | |
808 | ||
809 | struct item_head *pasted; | |
810 | ||
416e2abd | 811 | ret_val = leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
bd4c625c | 812 | /* append item in R[0] */ |
b49fb112 | 813 | if (tb->pos_in_item >= 0) { |
fba4ebb5 | 814 | buffer_info_init_right(tb, &bi); |
b49fb112 JM |
815 | leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], tb->pos_in_item, |
816 | tb->insert_size[0], body, tb->zeroes_num); | |
bd4c625c LT |
817 | } |
818 | ||
819 | /* paste new entry, if item is directory item */ | |
b49fb112 JM |
820 | pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]); |
821 | if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) { | |
822 | leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0], | |
823 | tb->pos_in_item, 1, | |
416e2abd DJ |
824 | (struct reiserfs_de_head *) body, |
825 | body + DEH_SIZE, tb->insert_size[0]); | |
b49fb112 | 826 | if (!tb->pos_in_item) { |
bd4c625c | 827 | |
b49fb112 | 828 | RFALSE(tb->item_pos - n + tb->rnum[0], |
bd4c625c LT |
829 | "PAP-12165: directory item must be first item of node when pasting is in 0th position"); |
830 | ||
831 | /* update delimiting keys */ | |
416e2abd | 832 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0); |
bd4c625c LT |
833 | } |
834 | } | |
835 | ||
836 | if (is_indirect_le_ih(pasted)) | |
837 | set_ih_free_space(pasted, 0); | |
b49fb112 | 838 | tb->zeroes_num = tb->insert_size[0] = 0; |
bd4c625c LT |
839 | } |
840 | } else { /* new item doesn't fall into R[0] */ | |
1da177e4 | 841 | |
bd4c625c | 842 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
1da177e4 | 843 | } |
3f0eb276 JM |
844 | |
845 | } | |
846 | ||
0080e9f9 JM |
847 | /* shift rnum[0] items from S[0] to the right neighbor R[0] */ |
848 | static void balance_leaf_right(struct tree_balance *tb, struct item_head *ih, | |
849 | const char *body, int flag) | |
850 | { | |
851 | if (tb->rnum[0] <= 0) | |
852 | return; | |
853 | ||
854 | BUG_ON(flag != M_INSERT && flag != M_PASTE); | |
855 | ||
856 | if (flag == M_INSERT) | |
857 | balance_leaf_insert_right(tb, ih, body); | |
858 | else /* M_PASTE */ | |
859 | balance_leaf_paste_right(tb, ih, body); | |
860 | ||
861 | } | |
862 | ||
65ab18cb JM |
863 | static void balance_leaf_new_nodes_insert(struct tree_balance *tb, |
864 | struct item_head *ih, | |
865 | const char *body, | |
866 | struct item_head *insert_key, | |
867 | struct buffer_head **insert_ptr, | |
868 | int i) | |
869 | { | |
870 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
871 | int n = B_NR_ITEMS(tbS0); | |
872 | struct buffer_info bi; | |
873 | if (n - tb->snum[i] < tb->item_pos) { /* new item or it's part falls to first new node S_new[i] */ | |
874 | if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { /* part of new item falls into S_new[i] */ | |
875 | int old_key_comp, old_len, r_zeroes_number; | |
876 | const char *r_body; | |
877 | int version; | |
878 | ||
879 | /* Move snum[i]-1 items from S[0] to S_new[i] */ | |
880 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, | |
881 | tb->snum[i] - 1, -1, | |
882 | tb->S_new[i]); | |
883 | /* Remember key component and item length */ | |
884 | version = ih_version(ih); | |
885 | old_key_comp = le_ih_k_offset(ih); | |
886 | old_len = ih_item_len(ih); | |
887 | ||
888 | /* Calculate key component and item length to insert into S_new[i] */ | |
889 | set_le_ih_k_offset(ih, le_ih_k_offset(ih) + | |
890 | ((old_len - tb->sbytes[i]) << (is_indirect_le_ih(ih) ? tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT : 0))); | |
891 | ||
892 | put_ih_item_len(ih, tb->sbytes[i]); | |
893 | ||
894 | /* Insert part of the item into S_new[i] before 0-th item */ | |
895 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); | |
896 | ||
897 | if ((old_len - tb->sbytes[i]) > tb->zeroes_num) { | |
898 | r_zeroes_number = 0; | |
899 | r_body = body + (old_len - tb->sbytes[i]) - tb->zeroes_num; | |
900 | } else { | |
901 | r_body = body; | |
902 | r_zeroes_number = tb->zeroes_num - (old_len - tb->sbytes[i]); | |
903 | tb->zeroes_num -= r_zeroes_number; | |
904 | } | |
905 | ||
906 | leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number); | |
907 | ||
908 | /* Calculate key component and item length to insert into S[i] */ | |
909 | set_le_ih_k_offset(ih, old_key_comp); | |
910 | put_ih_item_len(ih, old_len - tb->sbytes[i]); | |
911 | tb->insert_size[0] -= tb->sbytes[i]; | |
912 | } else { /* whole new item falls into S_new[i] */ | |
913 | ||
914 | /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */ | |
915 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, | |
916 | tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]); | |
917 | ||
918 | /* Insert new item into S_new[i] */ | |
919 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); | |
920 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1, | |
921 | ih, body, tb->zeroes_num); | |
922 | ||
923 | tb->zeroes_num = tb->insert_size[0] = 0; | |
924 | } | |
925 | } | |
926 | ||
927 | else { /* new item or it part don't falls into S_new[i] */ | |
928 | ||
929 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, | |
930 | tb->snum[i], tb->sbytes[i], tb->S_new[i]); | |
931 | } | |
932 | } | |
933 | ||
9d496552 JM |
934 | static void balance_leaf_new_nodes_paste(struct tree_balance *tb, |
935 | struct item_head *ih, | |
936 | const char *body, | |
937 | struct item_head *insert_key, | |
938 | struct buffer_head **insert_ptr, | |
939 | int i) | |
940 | { | |
941 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
942 | int n = B_NR_ITEMS(tbS0); | |
943 | struct buffer_info bi; | |
944 | if (n - tb->snum[i] <= tb->item_pos) { /* pasted item or part if it falls to S_new[i] */ | |
945 | if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) { /* we must shift part of the appended item */ | |
946 | struct item_head *aux_ih; | |
947 | ||
948 | RFALSE(ih, "PAP-12210: ih must be 0"); | |
949 | ||
950 | aux_ih = item_head(tbS0, tb->item_pos); | |
951 | if (is_direntry_le_ih(aux_ih)) { | |
952 | /* we append to directory item */ | |
953 | ||
954 | int entry_count; | |
955 | ||
956 | entry_count = ih_entry_count(aux_ih); | |
957 | ||
958 | if (entry_count - tb->sbytes[i] < tb->pos_in_item && tb->pos_in_item <= entry_count) { | |
959 | /* new directory entry falls into S_new[i] */ | |
960 | ||
961 | RFALSE(!tb->insert_size[0], "PAP-12215: insert_size is already 0"); | |
962 | RFALSE(tb->sbytes[i] - 1 >= entry_count, | |
963 | "PAP-12220: there are no so much entries (%d), only %d", | |
964 | tb->sbytes[i] - 1, entry_count); | |
965 | ||
966 | /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */ | |
967 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i] - 1, tb->S_new[i]); | |
968 | /* Paste given directory entry to directory item */ | |
969 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); | |
970 | leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, | |
971 | tb->insert_size[0], body, tb->zeroes_num); | |
972 | /* paste new directory entry */ | |
973 | leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + tb->sbytes[i] - 1, 1, | |
974 | (struct reiserfs_de_head *) body, | |
975 | body + DEH_SIZE, tb->insert_size[0]); | |
976 | tb->insert_size[0] = 0; | |
977 | tb->pos_in_item++; | |
978 | } else { /* new directory entry doesn't fall into S_new[i] */ | |
979 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], tb->sbytes[i], tb->S_new[i]); | |
980 | } | |
981 | } else { /* regular object */ | |
982 | ||
983 | int n_shift, n_rem, r_zeroes_number; | |
984 | const char *r_body; | |
985 | ||
986 | RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || tb->insert_size[0] <= 0, | |
987 | "PAP-12225: item too short or insert_size <= 0"); | |
988 | ||
989 | /* Calculate number of bytes which must be shifted from appended item */ | |
990 | n_shift = tb->sbytes[i] - tb->insert_size[0]; | |
991 | if (n_shift < 0) | |
992 | n_shift = 0; | |
993 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, tb->S_new[i]); | |
994 | ||
995 | /* Calculate number of bytes which must remain in body after append to S_new[i] */ | |
996 | n_rem = tb->insert_size[0] - tb->sbytes[i]; | |
997 | if (n_rem < 0) | |
998 | n_rem = 0; | |
999 | /* Append part of body into S_new[0] */ | |
1000 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); | |
1001 | if (n_rem > tb->zeroes_num) { | |
1002 | r_zeroes_number = 0; | |
1003 | r_body = body + n_rem - tb->zeroes_num; | |
1004 | } else { | |
1005 | r_body = body; | |
1006 | r_zeroes_number = tb->zeroes_num - n_rem; | |
1007 | tb->zeroes_num -= r_zeroes_number; | |
1008 | } | |
1009 | ||
1010 | leaf_paste_in_buffer(&bi, 0, n_shift, | |
1011 | tb->insert_size[0] - n_rem, | |
1012 | r_body, r_zeroes_number); | |
1013 | { | |
1014 | struct item_head *tmp; | |
1015 | ||
1016 | tmp = item_head(tb->S_new[i], 0); | |
1017 | if (is_indirect_le_ih | |
1018 | (tmp)) { | |
1019 | set_ih_free_space(tmp, 0); | |
1020 | set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT))); | |
1021 | } else { | |
1022 | set_le_ih_k_offset(tmp, le_ih_k_offset(tmp) + n_rem); | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | tb->insert_size[0] = n_rem; | |
1027 | if (!n_rem) | |
1028 | tb->pos_in_item++; | |
1029 | } | |
1030 | } else | |
1031 | /* item falls wholly into S_new[i] */ | |
1032 | { | |
1033 | int leaf_mi; | |
1034 | struct item_head *pasted; | |
1035 | ||
1036 | #ifdef CONFIG_REISERFS_CHECK | |
1037 | struct item_head *ih_check = item_head(tbS0, tb->item_pos); | |
1038 | ||
1039 | if (!is_direntry_le_ih(ih_check) | |
1040 | && (tb->pos_in_item != ih_item_len(ih_check) | |
1041 | || tb->insert_size[0] <= 0)) | |
1042 | reiserfs_panic(tb->tb_sb, | |
1043 | "PAP-12235", | |
1044 | "pos_in_item " | |
1045 | "must be equal " | |
1046 | "to ih_item_len"); | |
1047 | #endif /* CONFIG_REISERFS_CHECK */ | |
1048 | ||
1049 | leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW, | |
1050 | tb, tb->snum[i], | |
1051 | tb->sbytes[i], | |
1052 | tb->S_new[i]); | |
1053 | ||
1054 | RFALSE(leaf_mi, | |
1055 | "PAP-12240: unexpected value returned by leaf_move_items (%d)", | |
1056 | leaf_mi); | |
1057 | ||
1058 | /* paste into item */ | |
1059 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); | |
1060 | leaf_paste_in_buffer(&bi, | |
1061 | tb->item_pos - n + tb->snum[i], | |
1062 | tb->pos_in_item, | |
1063 | tb->insert_size[0], | |
1064 | body, tb->zeroes_num); | |
1065 | ||
1066 | pasted = item_head(tb->S_new[i], tb->item_pos - n + tb->snum[i]); | |
1067 | if (is_direntry_le_ih(pasted)) { | |
1068 | leaf_paste_entries(&bi, | |
1069 | tb->item_pos - n + tb->snum[i], | |
1070 | tb->pos_in_item, 1, | |
1071 | (struct reiserfs_de_head *)body, | |
1072 | body + DEH_SIZE, | |
1073 | tb->insert_size[0] | |
1074 | ); | |
1075 | } | |
1076 | ||
1077 | /* if we paste to indirect item update ih_free_space */ | |
1078 | if (is_indirect_le_ih(pasted)) | |
1079 | set_ih_free_space(pasted, 0); | |
1080 | tb->zeroes_num = tb->insert_size[0] = 0; | |
1081 | } | |
1082 | } | |
1083 | ||
1084 | else { /* pasted item doesn't fall into S_new[i] */ | |
1085 | ||
1086 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, | |
1087 | tb->snum[i], tb->sbytes[i], tb->S_new[i]); | |
1088 | } | |
1089 | ||
1090 | } | |
1091 | ||
0080e9f9 JM |
1092 | /* Fill new nodes that appear in place of S[0] */ |
1093 | static void balance_leaf_new_nodes(struct tree_balance *tb, | |
1094 | struct item_head *ih, | |
1095 | const char *body, | |
1096 | struct item_head *insert_key, | |
1097 | struct buffer_head **insert_ptr, | |
1098 | int flag) | |
1099 | { | |
1100 | int i; | |
1101 | for (i = tb->blknum[0] - 2; i >= 0; i--) { | |
1102 | ||
1103 | RFALSE(!tb->snum[i], | |
1104 | "PAP-12200: snum[%d] == %d. Must be > 0", i, | |
1105 | tb->snum[i]); | |
1106 | ||
1107 | /* here we shift from S to S_new nodes */ | |
1108 | ||
1109 | tb->S_new[i] = get_FEB(tb); | |
1110 | ||
1111 | /* initialized block type and tree level */ | |
1112 | set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL); | |
1113 | ||
1114 | switch (flag) { | |
1115 | case M_INSERT: /* insert item */ | |
1116 | balance_leaf_new_nodes_insert(tb, ih, body, insert_key, | |
1117 | insert_ptr, i); | |
1118 | break; | |
1119 | ||
1120 | case M_PASTE: /* append item */ | |
1121 | balance_leaf_new_nodes_paste(tb, ih, body, insert_key, | |
1122 | insert_ptr, i); | |
1123 | break; | |
1124 | default: /* cases d and t */ | |
1125 | reiserfs_panic(tb->tb_sb, "PAP-12245", | |
1126 | "blknum > 2: unexpected mode: %s(%d)", | |
1127 | (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag); | |
1128 | } | |
1129 | ||
1130 | memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE); | |
1131 | insert_ptr[i] = tb->S_new[i]; | |
1132 | ||
1133 | RFALSE(!buffer_journaled(tb->S_new[i]) | |
1134 | || buffer_journal_dirty(tb->S_new[i]) | |
1135 | || buffer_dirty(tb->S_new[i]), | |
1136 | "PAP-12247: S_new[%d] : (%b)", | |
1137 | i, format_bh(tb->S_new[i])); | |
1138 | } | |
1139 | } | |
1140 | ||
8c480ea1 JM |
1141 | static void balance_leaf_finish_node_insert(struct tree_balance *tb, |
1142 | struct item_head *ih, | |
1143 | const char *body) | |
1144 | { | |
1145 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
1146 | struct buffer_info bi; | |
1147 | buffer_info_init_tbS0(tb, &bi); | |
1148 | leaf_insert_into_buf(&bi, tb->item_pos, ih, | |
1149 | body, tb->zeroes_num); | |
1150 | ||
1151 | /* | |
1152 | * If we insert the first key | |
1153 | * change the delimiting key | |
1154 | */ | |
1155 | if (tb->item_pos == 0) { | |
1156 | if (tb->CFL[0]) /* can be 0 in reiserfsck */ | |
1157 | replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); | |
1158 | } | |
1159 | } | |
1160 | ||
7f447ba8 JM |
1161 | static void balance_leaf_finish_node_paste(struct tree_balance *tb, |
1162 | struct item_head *ih, | |
1163 | const char *body) | |
1164 | { | |
1165 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
1166 | struct buffer_info bi; | |
1167 | struct item_head *pasted; | |
1168 | ||
1169 | pasted = item_head(tbS0, tb->item_pos); | |
1170 | /* when directory, may be new entry already pasted */ | |
1171 | if (is_direntry_le_ih(pasted)) { | |
1172 | if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) { | |
1173 | ||
1174 | RFALSE(!tb->insert_size[0], | |
1175 | "PAP-12260: insert_size is 0 already"); | |
1176 | ||
1177 | /* prepare space */ | |
1178 | buffer_info_init_tbS0(tb, &bi); | |
1179 | leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item, | |
1180 | tb->insert_size[0], body, | |
1181 | tb->zeroes_num); | |
1182 | ||
1183 | /* paste entry */ | |
1184 | leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1, | |
1185 | (struct reiserfs_de_head *)body, | |
1186 | body + DEH_SIZE, | |
1187 | tb->insert_size[0]); | |
1188 | if (!tb->item_pos && !tb->pos_in_item) { | |
1189 | RFALSE(!tb->CFL[0] || !tb->L[0], | |
1190 | "PAP-12270: CFL[0]/L[0] must be specified"); | |
1191 | if (tb->CFL[0]) | |
1192 | replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); | |
1193 | } | |
1194 | tb->insert_size[0] = 0; | |
1195 | } | |
1196 | } else { /* regular object */ | |
1197 | if (tb->pos_in_item == ih_item_len(pasted)) { | |
1198 | ||
1199 | RFALSE(tb->insert_size[0] <= 0, | |
1200 | "PAP-12275: insert size must not be %d", | |
1201 | tb->insert_size[0]); | |
1202 | buffer_info_init_tbS0(tb, &bi); | |
1203 | leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item, | |
1204 | tb->insert_size[0], body, tb->zeroes_num); | |
1205 | ||
1206 | if (is_indirect_le_ih(pasted)) { | |
1207 | #if 0 | |
1208 | RFALSE(tb-> | |
1209 | insert_size[0] != | |
1210 | UNFM_P_SIZE, | |
1211 | "PAP-12280: insert_size for indirect item must be %d, not %d", | |
1212 | UNFM_P_SIZE, | |
1213 | tb-> | |
1214 | insert_size[0]); | |
1215 | #endif | |
1216 | set_ih_free_space(pasted, 0); | |
1217 | } | |
1218 | tb->insert_size[0] = 0; | |
1219 | } | |
1220 | #ifdef CONFIG_REISERFS_CHECK | |
1221 | else { | |
1222 | if (tb->insert_size[0]) { | |
1223 | print_cur_tb("12285"); | |
1224 | reiserfs_panic(tb->tb_sb, | |
1225 | "PAP-12285", | |
1226 | "insert_size " | |
1227 | "must be 0 " | |
1228 | "(%d)", | |
1229 | tb->insert_size[0]); | |
1230 | } | |
1231 | } | |
1232 | #endif /* CONFIG_REISERFS_CHECK */ | |
1233 | ||
1234 | } | |
1235 | } | |
1236 | ||
0080e9f9 JM |
1237 | /* |
1238 | * if the affected item was not wholly shifted then we | |
1239 | * perform all necessary operations on that part or whole | |
1240 | * of the affected item which remains in S | |
1241 | */ | |
1242 | static void balance_leaf_finish_node(struct tree_balance *tb, | |
1243 | struct item_head *ih, | |
1244 | const char *body, int flag) | |
1245 | { | |
1246 | /* if we must insert or append into buffer S[0] */ | |
1247 | if (0 <= tb->item_pos && tb->item_pos < tb->s0num) { | |
1248 | if (flag == M_INSERT) | |
1249 | balance_leaf_finish_node_insert(tb, ih, body); | |
1250 | else /* M_PASTE */ | |
1251 | balance_leaf_finish_node_paste(tb, ih, body); | |
1252 | } | |
1253 | } | |
1254 | ||
3f0eb276 JM |
1255 | /** |
1256 | * balance_leaf - reiserfs tree balancing algorithm | |
1257 | * @tb: tree balance state | |
1258 | * @ih: item header of inserted item (little endian) | |
1259 | * @body: body of inserted item or bytes to paste | |
1260 | * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance) | |
1261 | * passed back: | |
1262 | * @insert_key: key to insert new nodes | |
1263 | * @insert_ptr: array of nodes to insert at the next level | |
1264 | * | |
1265 | * In our processing of one level we sometimes determine what must be | |
1266 | * inserted into the next higher level. This insertion consists of a | |
1267 | * key or two keys and their corresponding pointers. | |
1268 | */ | |
1269 | static int balance_leaf(struct tree_balance *tb, struct item_head *ih, | |
1270 | const char *body, int flag, | |
1271 | struct item_head *insert_key, | |
1272 | struct buffer_head **insert_ptr) | |
1273 | { | |
1274 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); | |
3f0eb276 JM |
1275 | |
1276 | PROC_INFO_INC(tb->tb_sb, balance_at[0]); | |
1277 | ||
1278 | /* Make balance in case insert_size[0] < 0 */ | |
1279 | if (tb->insert_size[0] < 0) | |
1280 | return balance_leaf_when_delete(tb, flag); | |
1281 | ||
1282 | tb->item_pos = PATH_LAST_POSITION(tb->tb_path), | |
1283 | tb->pos_in_item = tb->tb_path->pos_in_item, | |
1284 | tb->zeroes_num = 0; | |
1285 | if (flag == M_INSERT && !body) | |
1286 | tb->zeroes_num = ih_item_len(ih); | |
1287 | ||
1288 | /* | |
1289 | * for indirect item pos_in_item is measured in unformatted node | |
1290 | * pointers. Recalculate to bytes | |
1291 | */ | |
1292 | if (flag != M_INSERT | |
1293 | && is_indirect_le_ih(item_head(tbS0, tb->item_pos))) | |
1294 | tb->pos_in_item *= UNFM_P_SIZE; | |
1295 | ||
0080e9f9 | 1296 | balance_leaf_left(tb, ih, body, flag); |
3f0eb276 JM |
1297 | |
1298 | /* tb->lnum[0] > 0 */ | |
1299 | /* Calculate new item position */ | |
1300 | tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0)); | |
1301 | ||
0080e9f9 | 1302 | balance_leaf_right(tb, ih, body, flag); |
1da177e4 | 1303 | |
bd4c625c LT |
1304 | /* tb->rnum[0] > 0 */ |
1305 | RFALSE(tb->blknum[0] > 3, | |
416e2abd | 1306 | "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]); |
bd4c625c | 1307 | RFALSE(tb->blknum[0] < 0, |
416e2abd | 1308 | "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]); |
bd4c625c | 1309 | |
97fd4b97 JM |
1310 | /* |
1311 | * if while adding to a node we discover that it is possible to split | |
1312 | * it in two, and merge the left part into the left neighbor and the | |
1313 | * right part into the right neighbor, eliminating the node | |
1314 | */ | |
bd4c625c LT |
1315 | if (tb->blknum[0] == 0) { /* node S[0] is empty now */ |
1316 | ||
1317 | RFALSE(!tb->lnum[0] || !tb->rnum[0], | |
1318 | "PAP-12190: lnum and rnum must not be zero"); | |
97fd4b97 JM |
1319 | /* |
1320 | * if insertion was done before 0-th position in R[0], right | |
1321 | * delimiting key of the tb->L[0]'s and left delimiting key are | |
1322 | * not set correctly | |
1323 | */ | |
bd4c625c LT |
1324 | if (tb->CFL[0]) { |
1325 | if (!tb->CFR[0]) | |
c3a9c210 JM |
1326 | reiserfs_panic(tb->tb_sb, "vs-12195", |
1327 | "CFR not initialized"); | |
4cf5f7ad JM |
1328 | copy_key(internal_key(tb->CFL[0], tb->lkey[0]), |
1329 | internal_key(tb->CFR[0], tb->rkey[0])); | |
bd4c625c LT |
1330 | do_balance_mark_internal_dirty(tb, tb->CFL[0], 0); |
1331 | } | |
1da177e4 | 1332 | |
bd4c625c LT |
1333 | reiserfs_invalidate_buffer(tb, tbS0); |
1334 | return 0; | |
1335 | } | |
1da177e4 | 1336 | |
0080e9f9 | 1337 | balance_leaf_new_nodes(tb, ih, body, insert_key, insert_ptr, flag); |
bd4c625c | 1338 | |
0080e9f9 | 1339 | balance_leaf_finish_node(tb, ih, body, flag); |
bd4c625c | 1340 | |
1da177e4 | 1341 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
1342 | if (flag == M_PASTE && tb->insert_size[0]) { |
1343 | print_cur_tb("12290"); | |
1344 | reiserfs_panic(tb->tb_sb, | |
c3a9c210 | 1345 | "PAP-12290", "insert_size is still not 0 (%d)", |
bd4c625c LT |
1346 | tb->insert_size[0]); |
1347 | } | |
97fd4b97 JM |
1348 | #endif |
1349 | ||
1350 | /* Leaf level of the tree is balanced (end of balance_leaf) */ | |
bd4c625c | 1351 | return 0; |
97fd4b97 | 1352 | } |
1da177e4 LT |
1353 | |
1354 | /* Make empty node */ | |
bd4c625c | 1355 | void make_empty_node(struct buffer_info *bi) |
1da177e4 | 1356 | { |
bd4c625c | 1357 | struct block_head *blkh; |
1da177e4 | 1358 | |
bd4c625c | 1359 | RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL"); |
1da177e4 | 1360 | |
bd4c625c LT |
1361 | blkh = B_BLK_HEAD(bi->bi_bh); |
1362 | set_blkh_nr_item(blkh, 0); | |
1363 | set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh)); | |
1da177e4 | 1364 | |
bd4c625c LT |
1365 | if (bi->bi_parent) |
1366 | B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */ | |
1da177e4 LT |
1367 | } |
1368 | ||
1da177e4 | 1369 | /* Get first empty buffer */ |
bd4c625c | 1370 | struct buffer_head *get_FEB(struct tree_balance *tb) |
1da177e4 | 1371 | { |
bd4c625c | 1372 | int i; |
bd4c625c | 1373 | struct buffer_info bi; |
1da177e4 | 1374 | |
bd4c625c | 1375 | for (i = 0; i < MAX_FEB_SIZE; i++) |
9dce07f1 | 1376 | if (tb->FEB[i] != NULL) |
bd4c625c LT |
1377 | break; |
1378 | ||
1379 | if (i == MAX_FEB_SIZE) | |
c3a9c210 | 1380 | reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty"); |
bd4c625c | 1381 | |
fba4ebb5 | 1382 | buffer_info_init_bh(tb, &bi, tb->FEB[i]); |
bd4c625c | 1383 | make_empty_node(&bi); |
fba4ebb5 JM |
1384 | set_buffer_uptodate(tb->FEB[i]); |
1385 | tb->used[i] = tb->FEB[i]; | |
bd4c625c | 1386 | tb->FEB[i] = NULL; |
bd4c625c | 1387 | |
fba4ebb5 | 1388 | return tb->used[i]; |
bd4c625c | 1389 | } |
1da177e4 | 1390 | |
098297b2 | 1391 | /* This is now used because reiserfs_free_block has to be able to schedule. */ |
bd4c625c | 1392 | static void store_thrown(struct tree_balance *tb, struct buffer_head *bh) |
1da177e4 | 1393 | { |
bd4c625c LT |
1394 | int i; |
1395 | ||
1396 | if (buffer_dirty(bh)) | |
45b03d5e JM |
1397 | reiserfs_warning(tb->tb_sb, "reiserfs-12320", |
1398 | "called with dirty buffer"); | |
79a81aef | 1399 | for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) |
bd4c625c LT |
1400 | if (!tb->thrown[i]) { |
1401 | tb->thrown[i] = bh; | |
1402 | get_bh(bh); /* free_thrown puts this */ | |
1403 | return; | |
1404 | } | |
45b03d5e JM |
1405 | reiserfs_warning(tb->tb_sb, "reiserfs-12321", |
1406 | "too many thrown buffers"); | |
1da177e4 LT |
1407 | } |
1408 | ||
bd4c625c LT |
1409 | static void free_thrown(struct tree_balance *tb) |
1410 | { | |
1411 | int i; | |
1412 | b_blocknr_t blocknr; | |
79a81aef | 1413 | for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) { |
bd4c625c LT |
1414 | if (tb->thrown[i]) { |
1415 | blocknr = tb->thrown[i]->b_blocknr; | |
1416 | if (buffer_dirty(tb->thrown[i])) | |
45b03d5e JM |
1417 | reiserfs_warning(tb->tb_sb, "reiserfs-12322", |
1418 | "called with dirty buffer %d", | |
bd4c625c LT |
1419 | blocknr); |
1420 | brelse(tb->thrown[i]); /* incremented in store_thrown */ | |
1421 | reiserfs_free_block(tb->transaction_handle, NULL, | |
1422 | blocknr, 0); | |
1423 | } | |
1da177e4 | 1424 | } |
1da177e4 LT |
1425 | } |
1426 | ||
bd4c625c | 1427 | void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh) |
1da177e4 | 1428 | { |
bd4c625c LT |
1429 | struct block_head *blkh; |
1430 | blkh = B_BLK_HEAD(bh); | |
1431 | set_blkh_level(blkh, FREE_LEVEL); | |
1432 | set_blkh_nr_item(blkh, 0); | |
1433 | ||
1434 | clear_buffer_dirty(bh); | |
1435 | store_thrown(tb, bh); | |
1da177e4 LT |
1436 | } |
1437 | ||
1438 | /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/ | |
bd4c625c LT |
1439 | void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest, |
1440 | struct buffer_head *src, int n_src) | |
1da177e4 LT |
1441 | { |
1442 | ||
bd4c625c LT |
1443 | RFALSE(dest == NULL || src == NULL, |
1444 | "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)", | |
1445 | src, dest); | |
1446 | RFALSE(!B_IS_KEYS_LEVEL(dest), | |
1447 | "vs-12310: invalid level (%z) for destination buffer. dest must be leaf", | |
1448 | dest); | |
1449 | RFALSE(n_dest < 0 || n_src < 0, | |
1450 | "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest); | |
1451 | RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src), | |
1452 | "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big", | |
1453 | n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest)); | |
1454 | ||
1455 | if (B_IS_ITEMS_LEVEL(src)) | |
1456 | /* source buffer contains leaf node */ | |
4cf5f7ad | 1457 | memcpy(internal_key(dest, n_dest), item_head(src, n_src), |
bd4c625c LT |
1458 | KEY_SIZE); |
1459 | else | |
4cf5f7ad | 1460 | memcpy(internal_key(dest, n_dest), internal_key(src, n_src), |
bd4c625c LT |
1461 | KEY_SIZE); |
1462 | ||
1463 | do_balance_mark_internal_dirty(tb, dest, 0); | |
1da177e4 LT |
1464 | } |
1465 | ||
bd4c625c | 1466 | int get_left_neighbor_position(struct tree_balance *tb, int h) |
1da177e4 | 1467 | { |
bd4c625c | 1468 | int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1); |
1da177e4 | 1469 | |
9dce07f1 | 1470 | RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL, |
bd4c625c LT |
1471 | "vs-12325: FL[%d](%p) or F[%d](%p) does not exist", |
1472 | h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h)); | |
1da177e4 | 1473 | |
bd4c625c LT |
1474 | if (Sh_position == 0) |
1475 | return B_NR_ITEMS(tb->FL[h]); | |
1476 | else | |
1477 | return Sh_position - 1; | |
1da177e4 LT |
1478 | } |
1479 | ||
bd4c625c | 1480 | int get_right_neighbor_position(struct tree_balance *tb, int h) |
1da177e4 | 1481 | { |
bd4c625c | 1482 | int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1); |
1da177e4 | 1483 | |
9dce07f1 | 1484 | RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL, |
bd4c625c LT |
1485 | "vs-12330: F[%d](%p) or FR[%d](%p) does not exist", |
1486 | h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]); | |
1da177e4 | 1487 | |
bd4c625c LT |
1488 | if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h))) |
1489 | return 0; | |
1490 | else | |
1491 | return Sh_position + 1; | |
1da177e4 LT |
1492 | } |
1493 | ||
1da177e4 LT |
1494 | #ifdef CONFIG_REISERFS_CHECK |
1495 | ||
bd4c625c LT |
1496 | int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value); |
1497 | static void check_internal_node(struct super_block *s, struct buffer_head *bh, | |
1498 | char *mes) | |
1da177e4 | 1499 | { |
bd4c625c LT |
1500 | struct disk_child *dc; |
1501 | int i; | |
1da177e4 | 1502 | |
bd4c625c | 1503 | RFALSE(!bh, "PAP-12336: bh == 0"); |
1da177e4 | 1504 | |
bd4c625c LT |
1505 | if (!bh || !B_IS_IN_TREE(bh)) |
1506 | return; | |
1da177e4 | 1507 | |
bd4c625c LT |
1508 | RFALSE(!buffer_dirty(bh) && |
1509 | !(buffer_journaled(bh) || buffer_journal_dirty(bh)), | |
1510 | "PAP-12337: buffer (%b) must be dirty", bh); | |
1511 | dc = B_N_CHILD(bh, 0); | |
1da177e4 | 1512 | |
bd4c625c LT |
1513 | for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) { |
1514 | if (!is_reusable(s, dc_block_number(dc), 1)) { | |
1515 | print_cur_tb(mes); | |
c3a9c210 JM |
1516 | reiserfs_panic(s, "PAP-12338", |
1517 | "invalid child pointer %y in %b", | |
bd4c625c LT |
1518 | dc, bh); |
1519 | } | |
1520 | } | |
1da177e4 LT |
1521 | } |
1522 | ||
45b03d5e JM |
1523 | static int locked_or_not_in_tree(struct tree_balance *tb, |
1524 | struct buffer_head *bh, char *which) | |
bd4c625c LT |
1525 | { |
1526 | if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) || | |
1527 | !B_IS_IN_TREE(bh)) { | |
45b03d5e | 1528 | reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh); |
bd4c625c LT |
1529 | return 1; |
1530 | } | |
1531 | return 0; | |
1532 | } | |
1da177e4 | 1533 | |
bd4c625c | 1534 | static int check_before_balancing(struct tree_balance *tb) |
1da177e4 | 1535 | { |
bd4c625c LT |
1536 | int retval = 0; |
1537 | ||
08f14fc8 | 1538 | if (REISERFS_SB(tb->tb_sb)->cur_tb) { |
c3a9c210 JM |
1539 | reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule " |
1540 | "occurred based on cur_tb not being null at " | |
1541 | "this point in code. do_balance cannot properly " | |
08f14fc8 FW |
1542 | "handle concurrent tree accesses on a same " |
1543 | "mount point."); | |
1da177e4 | 1544 | } |
bd4c625c | 1545 | |
098297b2 JM |
1546 | /* |
1547 | * double check that buffers that we will modify are unlocked. | |
1548 | * (fix_nodes should already have prepped all of these for us). | |
1549 | */ | |
bd4c625c | 1550 | if (tb->lnum[0]) { |
45b03d5e JM |
1551 | retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]"); |
1552 | retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]"); | |
1553 | retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]"); | |
bd4c625c | 1554 | check_leaf(tb->L[0]); |
1da177e4 | 1555 | } |
bd4c625c | 1556 | if (tb->rnum[0]) { |
45b03d5e JM |
1557 | retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]"); |
1558 | retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]"); | |
1559 | retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]"); | |
bd4c625c LT |
1560 | check_leaf(tb->R[0]); |
1561 | } | |
45b03d5e JM |
1562 | retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path), |
1563 | "S[0]"); | |
bd4c625c | 1564 | check_leaf(PATH_PLAST_BUFFER(tb->tb_path)); |
1da177e4 | 1565 | |
bd4c625c LT |
1566 | return retval; |
1567 | } | |
1da177e4 | 1568 | |
bd4c625c | 1569 | static void check_after_balance_leaf(struct tree_balance *tb) |
1da177e4 | 1570 | { |
bd4c625c LT |
1571 | if (tb->lnum[0]) { |
1572 | if (B_FREE_SPACE(tb->L[0]) != | |
1573 | MAX_CHILD_SIZE(tb->L[0]) - | |
1574 | dc_size(B_N_CHILD | |
1575 | (tb->FL[0], get_left_neighbor_position(tb, 0)))) { | |
1576 | print_cur_tb("12221"); | |
c3a9c210 JM |
1577 | reiserfs_panic(tb->tb_sb, "PAP-12355", |
1578 | "shift to left was incorrect"); | |
bd4c625c LT |
1579 | } |
1580 | } | |
1581 | if (tb->rnum[0]) { | |
1582 | if (B_FREE_SPACE(tb->R[0]) != | |
1583 | MAX_CHILD_SIZE(tb->R[0]) - | |
1584 | dc_size(B_N_CHILD | |
1585 | (tb->FR[0], get_right_neighbor_position(tb, 0)))) { | |
1586 | print_cur_tb("12222"); | |
c3a9c210 JM |
1587 | reiserfs_panic(tb->tb_sb, "PAP-12360", |
1588 | "shift to right was incorrect"); | |
bd4c625c LT |
1589 | } |
1590 | } | |
1591 | if (PATH_H_PBUFFER(tb->tb_path, 1) && | |
1592 | (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) != | |
1593 | (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) - | |
1594 | dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1), | |
1595 | PATH_H_POSITION(tb->tb_path, 1)))))) { | |
1596 | int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)); | |
1597 | int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) - | |
1598 | dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1), | |
1599 | PATH_H_POSITION(tb->tb_path, | |
1600 | 1)))); | |
1601 | print_cur_tb("12223"); | |
45b03d5e | 1602 | reiserfs_warning(tb->tb_sb, "reiserfs-12363", |
bd4c625c LT |
1603 | "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; " |
1604 | "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d", | |
1605 | left, | |
1606 | MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)), | |
1607 | PATH_H_PBUFFER(tb->tb_path, 1), | |
1608 | PATH_H_POSITION(tb->tb_path, 1), | |
1609 | dc_size(B_N_CHILD | |
1610 | (PATH_H_PBUFFER(tb->tb_path, 1), | |
1611 | PATH_H_POSITION(tb->tb_path, 1))), | |
1612 | right); | |
c3a9c210 | 1613 | reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect"); |
bd4c625c | 1614 | } |
1da177e4 LT |
1615 | } |
1616 | ||
bd4c625c | 1617 | static void check_leaf_level(struct tree_balance *tb) |
1da177e4 | 1618 | { |
bd4c625c LT |
1619 | check_leaf(tb->L[0]); |
1620 | check_leaf(tb->R[0]); | |
1621 | check_leaf(PATH_PLAST_BUFFER(tb->tb_path)); | |
1622 | } | |
1da177e4 | 1623 | |
bd4c625c LT |
1624 | static void check_internal_levels(struct tree_balance *tb) |
1625 | { | |
1626 | int h; | |
1627 | ||
1628 | /* check all internal nodes */ | |
1629 | for (h = 1; tb->insert_size[h]; h++) { | |
1630 | check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h), | |
1631 | "BAD BUFFER ON PATH"); | |
1632 | if (tb->lnum[h]) | |
1633 | check_internal_node(tb->tb_sb, tb->L[h], "BAD L"); | |
1634 | if (tb->rnum[h]) | |
1635 | check_internal_node(tb->tb_sb, tb->R[h], "BAD R"); | |
1636 | } | |
1da177e4 LT |
1637 | |
1638 | } | |
1639 | ||
1640 | #endif | |
1641 | ||
098297b2 JM |
1642 | /* |
1643 | * Now we have all of the buffers that must be used in balancing of | |
1644 | * the tree. We rely on the assumption that schedule() will not occur | |
1645 | * while do_balance works. ( Only interrupt handlers are acceptable.) | |
1646 | * We balance the tree according to the analysis made before this, | |
1647 | * using buffers already obtained. For SMP support it will someday be | |
1648 | * necessary to add ordered locking of tb. | |
1649 | */ | |
1da177e4 | 1650 | |
098297b2 JM |
1651 | /* |
1652 | * Some interesting rules of balancing: | |
1653 | * we delete a maximum of two nodes per level per balancing: we never | |
1654 | * delete R, when we delete two of three nodes L, S, R then we move | |
1655 | * them into R. | |
1656 | * | |
1657 | * we only delete L if we are deleting two nodes, if we delete only | |
1658 | * one node we delete S | |
1659 | * | |
1660 | * if we shift leaves then we shift as much as we can: this is a | |
1661 | * deliberate policy of extremism in node packing which results in | |
1662 | * higher average utilization after repeated random balance operations | |
1663 | * at the cost of more memory copies and more balancing as a result of | |
1664 | * small insertions to full nodes. | |
1665 | * | |
1666 | * if we shift internal nodes we try to evenly balance the node | |
1667 | * utilization, with consequent less balancing at the cost of lower | |
1668 | * utilization. | |
1669 | * | |
1670 | * one could argue that the policy for directories in leaves should be | |
1671 | * that of internal nodes, but we will wait until another day to | |
1672 | * evaluate this.... It would be nice to someday measure and prove | |
1673 | * these assumptions as to what is optimal.... | |
1674 | */ | |
1da177e4 | 1675 | |
bd4c625c | 1676 | static inline void do_balance_starts(struct tree_balance *tb) |
1da177e4 | 1677 | { |
098297b2 | 1678 | /* use print_cur_tb() to see initial state of struct tree_balance */ |
1da177e4 | 1679 | |
bd4c625c | 1680 | /* store_print_tb (tb); */ |
1da177e4 | 1681 | |
bd4c625c | 1682 | /* do not delete, just comment it out */ |
098297b2 JM |
1683 | /* |
1684 | print_tb(flag, PATH_LAST_POSITION(tb->tb_path), | |
1685 | tb->tb_path->pos_in_item, tb, "check"); | |
1686 | */ | |
bd4c625c | 1687 | RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB"); |
1da177e4 | 1688 | #ifdef CONFIG_REISERFS_CHECK |
08f14fc8 | 1689 | REISERFS_SB(tb->tb_sb)->cur_tb = tb; |
1da177e4 LT |
1690 | #endif |
1691 | } | |
1692 | ||
bd4c625c | 1693 | static inline void do_balance_completed(struct tree_balance *tb) |
1da177e4 | 1694 | { |
bd4c625c | 1695 | |
1da177e4 | 1696 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
1697 | check_leaf_level(tb); |
1698 | check_internal_levels(tb); | |
08f14fc8 | 1699 | REISERFS_SB(tb->tb_sb)->cur_tb = NULL; |
1da177e4 LT |
1700 | #endif |
1701 | ||
098297b2 JM |
1702 | /* |
1703 | * reiserfs_free_block is no longer schedule safe. So, we need to | |
1704 | * put the buffers we want freed on the thrown list during do_balance, | |
1705 | * and then free them now | |
bd4c625c | 1706 | */ |
1da177e4 | 1707 | |
bd4c625c | 1708 | REISERFS_SB(tb->tb_sb)->s_do_balance++; |
1da177e4 | 1709 | |
bd4c625c LT |
1710 | /* release all nodes hold to perform the balancing */ |
1711 | unfix_nodes(tb); | |
1da177e4 | 1712 | |
bd4c625c | 1713 | free_thrown(tb); |
1da177e4 LT |
1714 | } |
1715 | ||
098297b2 JM |
1716 | /* |
1717 | * do_balance - balance the tree | |
1718 | * | |
1719 | * @tb: tree_balance structure | |
1720 | * @ih: item header of inserted item | |
1721 | * @body: body of inserted item or bytes to paste | |
1722 | * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste | |
1723 | * | |
1724 | * Cut means delete part of an item (includes removing an entry from a | |
1725 | * directory). | |
1726 | * | |
1727 | * Delete means delete whole item. | |
1728 | * | |
1729 | * Insert means add a new item into the tree. | |
1730 | * | |
1731 | * Paste means to append to the end of an existing file or to | |
1732 | * insert a directory entry. | |
1733 | */ | |
1734 | void do_balance(struct tree_balance *tb, struct item_head *ih, | |
1735 | const char *body, int flag) | |
1736 | { | |
1737 | int child_pos; /* position of a child node in its parent */ | |
1738 | int h; /* level of the tree being processed */ | |
1739 | ||
1740 | /* | |
1741 | * in our processing of one level we sometimes determine what | |
1742 | * must be inserted into the next higher level. This insertion | |
1743 | * consists of a key or two keys and their corresponding | |
1744 | * pointers | |
1745 | */ | |
1746 | struct item_head insert_key[2]; | |
1747 | ||
1748 | /* inserted node-ptrs for the next level */ | |
1749 | struct buffer_head *insert_ptr[2]; | |
bd4c625c LT |
1750 | |
1751 | tb->tb_mode = flag; | |
1752 | tb->need_balance_dirty = 0; | |
1753 | ||
1754 | if (FILESYSTEM_CHANGED_TB(tb)) { | |
c3a9c210 JM |
1755 | reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has " |
1756 | "changed"); | |
bd4c625c LT |
1757 | } |
1758 | /* if we have no real work to do */ | |
1759 | if (!tb->insert_size[0]) { | |
45b03d5e JM |
1760 | reiserfs_warning(tb->tb_sb, "PAP-12350", |
1761 | "insert_size == 0, mode == %c", flag); | |
bd4c625c LT |
1762 | unfix_nodes(tb); |
1763 | return; | |
1764 | } | |
1da177e4 | 1765 | |
a228bf8f | 1766 | atomic_inc(&fs_generation(tb->tb_sb)); |
bd4c625c | 1767 | do_balance_starts(tb); |
1da177e4 | 1768 | |
098297b2 JM |
1769 | /* |
1770 | * balance_leaf returns 0 except if combining L R and S into | |
1771 | * one node. see balance_internal() for explanation of this | |
1772 | * line of code. | |
1773 | */ | |
bd4c625c LT |
1774 | child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) + |
1775 | balance_leaf(tb, ih, body, flag, insert_key, insert_ptr); | |
1da177e4 LT |
1776 | |
1777 | #ifdef CONFIG_REISERFS_CHECK | |
bd4c625c | 1778 | check_after_balance_leaf(tb); |
1da177e4 LT |
1779 | #endif |
1780 | ||
bd4c625c LT |
1781 | /* Balance internal level of the tree. */ |
1782 | for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++) | |
1783 | child_pos = | |
1784 | balance_internal(tb, h, child_pos, insert_key, insert_ptr); | |
1da177e4 | 1785 | |
bd4c625c | 1786 | do_balance_completed(tb); |
1da177e4 LT |
1787 | |
1788 | } |