Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README |
| 3 | */ |
| 4 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
| 14 | #include <linux/time.h> |
Al Viro | f466c6f | 2012-03-17 01:16:43 -0400 | [diff] [blame] | 15 | #include "reiserfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/buffer_head.h> |
Ahmed S. Darwish | 79a81ae | 2007-02-12 00:52:09 -0800 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Jeff Mahoney | fba4ebb | 2009-03-30 14:02:42 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 56 | inline void do_balance_mark_leaf_dirty(struct tree_balance *tb, |
| 57 | struct buffer_head *bh, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Jeff Mahoney | 09f1b80 | 2014-04-23 10:00:39 -0400 | [diff] [blame] | 59 | journal_mark_dirty(tb->transaction_handle, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 90 | /* |
| 91 | * Balance leaf node in case of delete or cut: insert_size[0] < 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 96 | * >0 means to shift entirely or partly the specified number of items |
| 97 | * to the neighbor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 99 | static int balance_leaf_when_delete(struct tree_balance *tb, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Jeff Mahoney | 4cf5f7a | 2014-04-23 10:00:35 -0400 | [diff] [blame] | 115 | ih = item_head(tbS0, item_pos); |
Jeff Mahoney | fba4ebb | 2009-03-30 14:02:42 -0400 | [diff] [blame] | 116 | buffer_info_init_tbS0(tb, &bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 118 | /* Delete or truncate the item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 120 | switch (flag) { |
| 121 | case M_DELETE: /* delete item in S[0] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 127 | leaf_delete_items(&bi, 0, item_pos, 1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 145 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 147 | case M_CUT:{ /* cut item in S[0] */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 148 | if (is_direntry_le_ih(ih)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 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 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 160 | tb->insert_size[0] = -1; |
| 161 | leaf_cut_from_buffer(&bi, item_pos, pos_in_item, |
| 162 | -tb->insert_size[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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; |
| 180 | } |
| 181 | |
| 182 | default: |
| 183 | print_cur_tb("12040"); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 184 | reiserfs_panic(tb->tb_sb, "PAP-12040", |
| 185 | "unexpected mode: %s(%d)", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 186 | (flag == |
| 187 | M_PASTE) ? "PASTE" : ((flag == |
| 188 | M_INSERT) ? "INSERT" : |
| 189 | "UNKNOWN"), flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 192 | /* |
| 193 | * the rule is that no shifting occurs unless by shifting |
| 194 | * a node can be freed |
| 195 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 196 | n = B_NR_ITEMS(tbS0); |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 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) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 203 | if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 204 | /* |
| 205 | * all contents of all the 3 buffers |
| 206 | * will be in L[0] |
| 207 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 220 | reiserfs_invalidate_buffer(tb, tbS0); |
| 221 | reiserfs_invalidate_buffer(tb, |
| 222 | tb->R[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 226 | /* |
| 227 | * all contents of all the 3 buffers will |
| 228 | * be in R[0] |
| 229 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
| 238 | |
| 239 | reiserfs_invalidate_buffer(tb, tbS0); |
| 240 | reiserfs_invalidate_buffer(tb, tb->L[0]); |
| 241 | |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | RFALSE(tb->rnum[0] != 0, |
| 246 | "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]); |
| 247 | /* all contents of L[0] and S[0] will be in L[0] */ |
| 248 | leaf_shift_left(tb, n, -1); |
| 249 | |
| 250 | reiserfs_invalidate_buffer(tb, tbS0); |
| 251 | |
| 252 | return 0; |
| 253 | } |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 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 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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; |
| 279 | } |
| 280 | |
| 281 | if (tb->rnum[0] == -1) { |
| 282 | /* all contents of R[0] and S[0] will be in R[0] */ |
| 283 | leaf_shift_right(tb, n, -1); |
| 284 | reiserfs_invalidate_buffer(tb, tbS0); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | RFALSE(tb->rnum[0], |
| 289 | "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 293 | static void balance_leaf_insert_left(struct tree_balance *tb, |
| 294 | struct item_head *ih, const char *body) |
| 295 | { |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 296 | int ret; |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 297 | struct buffer_info bi; |
| 298 | int n = B_NR_ITEMS(tb->L[0]); |
| 299 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 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; |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 304 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 305 | ret = leaf_shift_left(tb, tb->lnum[0] - 1, -1); |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 306 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 307 | /* Calculate item length to insert to S[0] */ |
| 308 | new_item_len = ih_item_len(ih) - tb->lbytes; |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 309 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 310 | /* Calculate and check item length to insert to L[0] */ |
| 311 | put_ih_item_len(ih, ih_item_len(ih) - new_item_len); |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 312 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 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)); |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 316 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 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))); |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 321 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 322 | version = ih_version(ih); |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 323 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 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; |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 331 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 332 | add_le_ih_k_offset(ih, tb->lbytes << shift); |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 333 | |
Jeff Mahoney | 4bf4de6 | 2014-04-23 10:00:56 -0400 | [diff] [blame] | 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 | } |
Jeff Mahoney | f1f007c | 2014-04-23 10:00:47 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Jeff Mahoney | 3fade93 | 2014-04-23 10:00:57 -0400 | [diff] [blame] | 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) |
| 423 | { |
| 424 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 425 | int n = B_NR_ITEMS(tb->L[0]); |
| 426 | struct buffer_info bi; |
| 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); |
| 542 | int n = B_NR_ITEMS(tb->L[0]); |
| 543 | struct buffer_info bi; |
| 544 | struct item_head *pasted; |
| 545 | int ret; |
| 546 | |
| 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); |
| 586 | |
| 587 | tb->insert_size[0] = 0; |
| 588 | tb->zeroes_num = 0; |
| 589 | } |
| 590 | |
Jeff Mahoney | cf22df1 | 2014-04-23 10:00:48 -0400 | [diff] [blame] | 591 | static void balance_leaf_paste_left(struct tree_balance *tb, |
| 592 | struct item_head *ih, const char *body) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Jeff Mahoney | 3fade93 | 2014-04-23 10:00:57 -0400 | [diff] [blame] | 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); |
Jeff Mahoney | cf22df1 | 2014-04-23 10:00:48 -0400 | [diff] [blame] | 599 | } |
| 600 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 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 | |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 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; |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 629 | int ret; |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 630 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 631 | /* new item or part of it doesn't fall into R[0] */ |
| 632 | if (n - tb->rnum[0] >= tb->item_pos) { |
| 633 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
| 634 | return; |
| 635 | } |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 636 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 637 | /* new item or its part falls to R[0] */ |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 638 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 639 | /* part of new item falls into R[0] */ |
| 640 | if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) { |
| 641 | loff_t old_key_comp, old_len, r_zeroes_number; |
| 642 | const char *r_body; |
| 643 | int version, shift; |
| 644 | loff_t offset; |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 645 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 646 | leaf_shift_right(tb, tb->rnum[0] - 1, -1); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 647 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 648 | version = ih_version(ih); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 649 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 650 | /* Remember key component and item length */ |
| 651 | old_key_comp = le_ih_k_offset(ih); |
| 652 | old_len = ih_item_len(ih); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 653 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 654 | /* |
| 655 | * Calculate key component and item length to insert |
| 656 | * into R[0] |
| 657 | */ |
| 658 | shift = 0; |
| 659 | if (is_indirect_le_ih(ih)) |
| 660 | shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT; |
| 661 | offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << shift); |
| 662 | set_le_ih_k_offset(ih, offset); |
| 663 | put_ih_item_len(ih, tb->rbytes); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 664 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 665 | /* Insert part of the item into R[0] */ |
| 666 | buffer_info_init_right(tb, &bi); |
| 667 | if ((old_len - tb->rbytes) > tb->zeroes_num) { |
| 668 | r_zeroes_number = 0; |
| 669 | r_body = body + (old_len - tb->rbytes) - tb->zeroes_num; |
| 670 | } else { |
| 671 | r_body = body; |
| 672 | r_zeroes_number = tb->zeroes_num - |
| 673 | (old_len - tb->rbytes); |
| 674 | tb->zeroes_num -= r_zeroes_number; |
| 675 | } |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 676 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 677 | leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 678 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 679 | /* Replace right delimiting key by first key in R[0] */ |
| 680 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 681 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 682 | /* |
| 683 | * Calculate key component and item length to |
| 684 | * insert into S[0] |
| 685 | */ |
| 686 | set_le_ih_k_offset(ih, old_key_comp); |
| 687 | put_ih_item_len(ih, old_len - tb->rbytes); |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 688 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 689 | tb->insert_size[0] -= tb->rbytes; |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 690 | |
Jeff Mahoney | 8dbf0d8 | 2014-04-23 10:00:58 -0400 | [diff] [blame] | 691 | } else { |
| 692 | /* whole new item falls into R[0] */ |
| 693 | |
| 694 | /* Shift rnum[0]-1 items to R[0] */ |
| 695 | ret = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes); |
| 696 | |
| 697 | /* Insert new item into R[0] */ |
| 698 | buffer_info_init_right(tb, &bi); |
| 699 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1, |
| 700 | ih, body, tb->zeroes_num); |
| 701 | |
| 702 | if (tb->item_pos - n + tb->rnum[0] - 1 == 0) |
| 703 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0); |
| 704 | |
| 705 | tb->zeroes_num = tb->insert_size[0] = 0; |
| 706 | } |
Jeff Mahoney | e80ef3d | 2014-04-23 10:00:49 -0400 | [diff] [blame] | 707 | } |
| 708 | |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 709 | |
| 710 | static void balance_leaf_paste_right_shift_dirent(struct tree_balance *tb, |
| 711 | struct item_head *ih, const char *body) |
| 712 | { |
| 713 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 714 | struct buffer_info bi; |
| 715 | int entry_count; |
| 716 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 717 | RFALSE(tb->zeroes_num, |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 718 | "PAP-12145: invalid parameter in case of a directory"); |
| 719 | entry_count = ih_entry_count(item_head(tbS0, tb->item_pos)); |
| 720 | |
| 721 | /* new directory entry falls into R[0] */ |
| 722 | if (entry_count - tb->rbytes < tb->pos_in_item) { |
| 723 | int paste_entry_position; |
| 724 | |
| 725 | RFALSE(tb->rbytes - 1 >= entry_count || !tb->insert_size[0], |
| 726 | "PAP-12150: no enough of entries to shift to R[0]: " |
| 727 | "rbytes=%d, entry_count=%d", tb->rbytes, entry_count); |
| 728 | |
| 729 | /* |
| 730 | * Shift rnum[0]-1 items in whole. |
| 731 | * Shift rbytes-1 directory entries from directory |
| 732 | * item number rnum[0] |
| 733 | */ |
| 734 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1); |
| 735 | |
| 736 | /* Paste given directory entry to directory item */ |
| 737 | paste_entry_position = tb->pos_in_item - entry_count + |
| 738 | tb->rbytes - 1; |
| 739 | buffer_info_init_right(tb, &bi); |
| 740 | leaf_paste_in_buffer(&bi, 0, paste_entry_position, |
| 741 | tb->insert_size[0], body, tb->zeroes_num); |
| 742 | |
| 743 | /* paste entry */ |
| 744 | leaf_paste_entries(&bi, 0, paste_entry_position, 1, |
| 745 | (struct reiserfs_de_head *) body, |
| 746 | body + DEH_SIZE, tb->insert_size[0]); |
| 747 | |
| 748 | /* change delimiting keys */ |
| 749 | if (paste_entry_position == 0) |
| 750 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0); |
| 751 | |
| 752 | tb->insert_size[0] = 0; |
| 753 | tb->pos_in_item++; |
| 754 | } else { |
| 755 | /* new directory entry doesn't fall into R[0] */ |
| 756 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | static void balance_leaf_paste_right_shift(struct tree_balance *tb, |
| 761 | struct item_head *ih, const char *body) |
| 762 | { |
| 763 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 764 | int n_shift, n_rem, r_zeroes_number, version; |
| 765 | unsigned long temp_rem; |
| 766 | const char *r_body; |
| 767 | struct buffer_info bi; |
| 768 | |
| 769 | /* we append to directory item */ |
| 770 | if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) { |
| 771 | balance_leaf_paste_right_shift_dirent(tb, ih, body); |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | /* regular object */ |
| 776 | |
| 777 | /* |
| 778 | * Calculate number of bytes which must be shifted |
| 779 | * from appended item |
| 780 | */ |
| 781 | n_shift = tb->rbytes - tb->insert_size[0]; |
| 782 | if (n_shift < 0) |
| 783 | n_shift = 0; |
| 784 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 785 | RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)), |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 786 | "PAP-12155: invalid position to paste. ih_item_len=%d, " |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 787 | "pos_in_item=%d", tb->pos_in_item, |
| 788 | ih_item_len(item_head(tbS0, tb->item_pos))); |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 789 | |
| 790 | leaf_shift_right(tb, tb->rnum[0], n_shift); |
| 791 | |
| 792 | /* |
| 793 | * Calculate number of bytes which must remain in body |
| 794 | * after appending to R[0] |
| 795 | */ |
| 796 | n_rem = tb->insert_size[0] - tb->rbytes; |
| 797 | if (n_rem < 0) |
| 798 | n_rem = 0; |
| 799 | |
| 800 | temp_rem = n_rem; |
| 801 | |
| 802 | version = ih_version(item_head(tb->R[0], 0)); |
| 803 | |
| 804 | if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) { |
| 805 | int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT; |
| 806 | temp_rem = n_rem << shift; |
| 807 | } |
| 808 | |
| 809 | add_le_key_k_offset(version, leaf_key(tb->R[0], 0), temp_rem); |
| 810 | add_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]), |
| 811 | temp_rem); |
| 812 | |
| 813 | do_balance_mark_internal_dirty(tb, tb->CFR[0], 0); |
| 814 | |
| 815 | /* Append part of body into R[0] */ |
| 816 | buffer_info_init_right(tb, &bi); |
| 817 | if (n_rem > tb->zeroes_num) { |
| 818 | r_zeroes_number = 0; |
| 819 | r_body = body + n_rem - tb->zeroes_num; |
| 820 | } else { |
| 821 | r_body = body; |
| 822 | r_zeroes_number = tb->zeroes_num - n_rem; |
| 823 | tb->zeroes_num -= r_zeroes_number; |
| 824 | } |
| 825 | |
| 826 | leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem, |
| 827 | r_body, r_zeroes_number); |
| 828 | |
| 829 | if (is_indirect_le_ih(item_head(tb->R[0], 0))) |
| 830 | set_ih_free_space(item_head(tb->R[0], 0), 0); |
| 831 | |
| 832 | tb->insert_size[0] = n_rem; |
| 833 | if (!n_rem) |
| 834 | tb->pos_in_item++; |
| 835 | } |
| 836 | |
| 837 | static void balance_leaf_paste_right_whole(struct tree_balance *tb, |
| 838 | struct item_head *ih, const char *body) |
| 839 | { |
| 840 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 841 | int n = B_NR_ITEMS(tbS0); |
| 842 | struct item_head *pasted; |
| 843 | struct buffer_info bi; |
| 844 | |
| 845 | buffer_info_init_right(tb, &bi); |
| 846 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
| 847 | |
| 848 | /* append item in R[0] */ |
| 849 | if (tb->pos_in_item >= 0) { |
| 850 | buffer_info_init_right(tb, &bi); |
| 851 | leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0], |
| 852 | tb->pos_in_item, tb->insert_size[0], body, |
| 853 | tb->zeroes_num); |
| 854 | } |
| 855 | |
| 856 | /* paste new entry, if item is directory item */ |
| 857 | pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]); |
| 858 | if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) { |
| 859 | leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0], |
| 860 | tb->pos_in_item, 1, |
| 861 | (struct reiserfs_de_head *)body, |
| 862 | body + DEH_SIZE, tb->insert_size[0]); |
| 863 | |
| 864 | if (!tb->pos_in_item) { |
| 865 | |
| 866 | RFALSE(tb->item_pos - n + tb->rnum[0], |
| 867 | "PAP-12165: directory item must be first " |
| 868 | "item of node when pasting is in 0th position"); |
| 869 | |
| 870 | /* update delimiting keys */ |
| 871 | replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | if (is_indirect_le_ih(pasted)) |
| 876 | set_ih_free_space(pasted, 0); |
| 877 | tb->zeroes_num = tb->insert_size[0] = 0; |
| 878 | } |
| 879 | |
Jeff Mahoney | 3f0eb27 | 2014-04-23 10:00:50 -0400 | [diff] [blame] | 880 | static void balance_leaf_paste_right(struct tree_balance *tb, |
| 881 | struct item_head *ih, const char *body) |
Jeff Mahoney | cf22df1 | 2014-04-23 10:00:48 -0400 | [diff] [blame] | 882 | { |
| 883 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
Jeff Mahoney | 3f0eb27 | 2014-04-23 10:00:50 -0400 | [diff] [blame] | 884 | int n = B_NR_ITEMS(tbS0); |
Jeff Mahoney | cf22df1 | 2014-04-23 10:00:48 -0400 | [diff] [blame] | 885 | |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 886 | /* new item doesn't fall into R[0] */ |
| 887 | if (n - tb->rnum[0] > tb->item_pos) { |
| 888 | leaf_shift_right(tb, tb->rnum[0], tb->rbytes); |
| 889 | return; |
| 890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 892 | /* pasted item or part of it falls to R[0] */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 893 | |
Jeff Mahoney | 2369ecd | 2014-04-23 10:00:59 -0400 | [diff] [blame] | 894 | if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1) |
| 895 | /* we must shift the part of the appended item */ |
| 896 | balance_leaf_paste_right_shift(tb, ih, body); |
| 897 | else |
| 898 | /* pasted item in whole falls into R[0] */ |
| 899 | balance_leaf_paste_right_whole(tb, ih, body); |
Jeff Mahoney | 3f0eb27 | 2014-04-23 10:00:50 -0400 | [diff] [blame] | 900 | } |
| 901 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 902 | /* shift rnum[0] items from S[0] to the right neighbor R[0] */ |
| 903 | static void balance_leaf_right(struct tree_balance *tb, struct item_head *ih, |
| 904 | const char *body, int flag) |
| 905 | { |
| 906 | if (tb->rnum[0] <= 0) |
| 907 | return; |
| 908 | |
| 909 | BUG_ON(flag != M_INSERT && flag != M_PASTE); |
| 910 | |
| 911 | if (flag == M_INSERT) |
| 912 | balance_leaf_insert_right(tb, ih, body); |
| 913 | else /* M_PASTE */ |
| 914 | balance_leaf_paste_right(tb, ih, body); |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 915 | } |
| 916 | |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 917 | static void balance_leaf_new_nodes_insert(struct tree_balance *tb, |
| 918 | struct item_head *ih, |
| 919 | const char *body, |
| 920 | struct item_head *insert_key, |
| 921 | struct buffer_head **insert_ptr, |
| 922 | int i) |
| 923 | { |
| 924 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 925 | int n = B_NR_ITEMS(tbS0); |
| 926 | struct buffer_info bi; |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 927 | int shift; |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 928 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 929 | /* new item or it part don't falls into S_new[i] */ |
| 930 | if (n - tb->snum[i] >= tb->item_pos) { |
| 931 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, |
| 932 | tb->snum[i], tb->sbytes[i], tb->S_new[i]); |
| 933 | return; |
| 934 | } |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 935 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 936 | /* new item or it's part falls to first new node S_new[i] */ |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 937 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 938 | /* part of new item falls into S_new[i] */ |
| 939 | if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) { |
| 940 | int old_key_comp, old_len, r_zeroes_number; |
| 941 | const char *r_body; |
| 942 | int version; |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 943 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 944 | /* Move snum[i]-1 items from S[0] to S_new[i] */ |
| 945 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i] - 1, -1, |
| 946 | tb->S_new[i]); |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 947 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 948 | /* Remember key component and item length */ |
| 949 | version = ih_version(ih); |
| 950 | old_key_comp = le_ih_k_offset(ih); |
| 951 | old_len = ih_item_len(ih); |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 952 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 953 | /* |
| 954 | * Calculate key component and item length to insert |
| 955 | * into S_new[i] |
| 956 | */ |
| 957 | shift = 0; |
| 958 | if (is_indirect_le_ih(ih)) |
| 959 | shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT; |
| 960 | set_le_ih_k_offset(ih, |
| 961 | le_ih_k_offset(ih) + |
| 962 | ((old_len - tb->sbytes[i]) << shift)); |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 963 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 964 | put_ih_item_len(ih, tb->sbytes[i]); |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 965 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 966 | /* Insert part of the item into S_new[i] before 0-th item */ |
| 967 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 968 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 969 | if ((old_len - tb->sbytes[i]) > tb->zeroes_num) { |
| 970 | r_zeroes_number = 0; |
| 971 | r_body = body + (old_len - tb->sbytes[i]) - |
| 972 | tb->zeroes_num; |
| 973 | } else { |
| 974 | r_body = body; |
| 975 | r_zeroes_number = tb->zeroes_num - (old_len - |
| 976 | tb->sbytes[i]); |
| 977 | tb->zeroes_num -= r_zeroes_number; |
| 978 | } |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 979 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 980 | leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number); |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 981 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 982 | /* |
| 983 | * Calculate key component and item length to |
| 984 | * insert into S[i] |
| 985 | */ |
| 986 | set_le_ih_k_offset(ih, old_key_comp); |
| 987 | put_ih_item_len(ih, old_len - tb->sbytes[i]); |
| 988 | tb->insert_size[0] -= tb->sbytes[i]; |
| 989 | } else { |
| 990 | /* whole new item falls into S_new[i] */ |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 991 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 992 | /* |
| 993 | * Shift snum[0] - 1 items to S_new[i] |
| 994 | * (sbytes[i] of split item) |
| 995 | */ |
| 996 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, |
| 997 | tb->snum[i] - 1, tb->sbytes[i], tb->S_new[i]); |
| 998 | |
| 999 | /* Insert new item into S_new[i] */ |
| 1000 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
| 1001 | leaf_insert_into_buf(&bi, tb->item_pos - n + tb->snum[i] - 1, |
| 1002 | ih, body, tb->zeroes_num); |
| 1003 | |
| 1004 | tb->zeroes_num = tb->insert_size[0] = 0; |
| 1005 | } |
Jeff Mahoney | 65ab18c | 2014-04-23 10:00:51 -0400 | [diff] [blame] | 1006 | } |
| 1007 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1008 | /* we append to directory item */ |
| 1009 | static void balance_leaf_new_nodes_paste_dirent(struct tree_balance *tb, |
| 1010 | struct item_head *ih, |
| 1011 | const char *body, |
| 1012 | struct item_head *insert_key, |
| 1013 | struct buffer_head **insert_ptr, |
| 1014 | int i) |
| 1015 | { |
| 1016 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 1017 | struct item_head *aux_ih = item_head(tbS0, tb->item_pos); |
| 1018 | int entry_count = ih_entry_count(aux_ih); |
| 1019 | struct buffer_info bi; |
| 1020 | |
| 1021 | if (entry_count - tb->sbytes[i] < tb->pos_in_item && |
| 1022 | tb->pos_in_item <= entry_count) { |
| 1023 | /* new directory entry falls into S_new[i] */ |
| 1024 | |
| 1025 | RFALSE(!tb->insert_size[0], |
| 1026 | "PAP-12215: insert_size is already 0"); |
| 1027 | RFALSE(tb->sbytes[i] - 1 >= entry_count, |
| 1028 | "PAP-12220: there are no so much entries (%d), only %d", |
| 1029 | tb->sbytes[i] - 1, entry_count); |
| 1030 | |
| 1031 | /* |
| 1032 | * Shift snum[i]-1 items in whole. |
| 1033 | * Shift sbytes[i] directory entries |
| 1034 | * from directory item number snum[i] |
| 1035 | */ |
| 1036 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], |
| 1037 | tb->sbytes[i] - 1, tb->S_new[i]); |
| 1038 | |
| 1039 | /* |
| 1040 | * Paste given directory entry to |
| 1041 | * directory item |
| 1042 | */ |
| 1043 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
| 1044 | leaf_paste_in_buffer(&bi, 0, tb->pos_in_item - entry_count + |
| 1045 | tb->sbytes[i] - 1, tb->insert_size[0], |
| 1046 | body, tb->zeroes_num); |
| 1047 | |
| 1048 | /* paste new directory entry */ |
| 1049 | leaf_paste_entries(&bi, 0, tb->pos_in_item - entry_count + |
| 1050 | tb->sbytes[i] - 1, 1, |
| 1051 | (struct reiserfs_de_head *) body, |
| 1052 | body + DEH_SIZE, tb->insert_size[0]); |
| 1053 | |
| 1054 | tb->insert_size[0] = 0; |
| 1055 | tb->pos_in_item++; |
| 1056 | } else { |
| 1057 | /* new directory entry doesn't fall into S_new[i] */ |
| 1058 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], |
| 1059 | tb->sbytes[i], tb->S_new[i]); |
| 1060 | } |
| 1061 | |
| 1062 | } |
| 1063 | |
| 1064 | static void balance_leaf_new_nodes_paste_shift(struct tree_balance *tb, |
| 1065 | struct item_head *ih, |
| 1066 | const char *body, |
| 1067 | struct item_head *insert_key, |
| 1068 | struct buffer_head **insert_ptr, |
| 1069 | int i) |
| 1070 | { |
| 1071 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 1072 | struct item_head *aux_ih = item_head(tbS0, tb->item_pos); |
| 1073 | int n_shift, n_rem, r_zeroes_number, shift; |
| 1074 | const char *r_body; |
| 1075 | struct item_head *tmp; |
| 1076 | struct buffer_info bi; |
| 1077 | |
| 1078 | RFALSE(ih, "PAP-12210: ih must be 0"); |
| 1079 | |
| 1080 | if (is_direntry_le_ih(aux_ih)) { |
| 1081 | balance_leaf_new_nodes_paste_dirent(tb, ih, body, insert_key, |
| 1082 | insert_ptr, i); |
| 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | /* regular object */ |
| 1087 | |
| 1088 | |
| 1089 | RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)) || |
| 1090 | tb->insert_size[0] <= 0, |
| 1091 | "PAP-12225: item too short or insert_size <= 0"); |
| 1092 | |
| 1093 | /* |
| 1094 | * Calculate number of bytes which must be shifted from appended item |
| 1095 | */ |
| 1096 | n_shift = tb->sbytes[i] - tb->insert_size[0]; |
| 1097 | if (n_shift < 0) |
| 1098 | n_shift = 0; |
| 1099 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], n_shift, |
| 1100 | tb->S_new[i]); |
| 1101 | |
| 1102 | /* |
| 1103 | * Calculate number of bytes which must remain in body after |
| 1104 | * append to S_new[i] |
| 1105 | */ |
| 1106 | n_rem = tb->insert_size[0] - tb->sbytes[i]; |
| 1107 | if (n_rem < 0) |
| 1108 | n_rem = 0; |
| 1109 | |
| 1110 | /* Append part of body into S_new[0] */ |
| 1111 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
| 1112 | if (n_rem > tb->zeroes_num) { |
| 1113 | r_zeroes_number = 0; |
| 1114 | r_body = body + n_rem - tb->zeroes_num; |
| 1115 | } else { |
| 1116 | r_body = body; |
| 1117 | r_zeroes_number = tb->zeroes_num - n_rem; |
| 1118 | tb->zeroes_num -= r_zeroes_number; |
| 1119 | } |
| 1120 | |
| 1121 | leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem, |
| 1122 | r_body, r_zeroes_number); |
| 1123 | |
| 1124 | tmp = item_head(tb->S_new[i], 0); |
| 1125 | shift = 0; |
| 1126 | if (is_indirect_le_ih(tmp)) { |
| 1127 | set_ih_free_space(tmp, 0); |
| 1128 | shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT; |
| 1129 | } |
| 1130 | add_le_ih_k_offset(tmp, n_rem << shift); |
| 1131 | |
| 1132 | tb->insert_size[0] = n_rem; |
| 1133 | if (!n_rem) |
| 1134 | tb->pos_in_item++; |
| 1135 | } |
| 1136 | |
| 1137 | static void balance_leaf_new_nodes_paste_whole(struct tree_balance *tb, |
| 1138 | struct item_head *ih, |
| 1139 | const char *body, |
| 1140 | struct item_head *insert_key, |
| 1141 | struct buffer_head **insert_ptr, |
| 1142 | int i) |
| 1143 | |
| 1144 | { |
| 1145 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 1146 | int n = B_NR_ITEMS(tbS0); |
| 1147 | int leaf_mi; |
| 1148 | struct item_head *pasted; |
| 1149 | struct buffer_info bi; |
| 1150 | |
| 1151 | #ifdef CONFIG_REISERFS_CHECK |
| 1152 | struct item_head *ih_check = item_head(tbS0, tb->item_pos); |
| 1153 | |
| 1154 | if (!is_direntry_le_ih(ih_check) && |
| 1155 | (tb->pos_in_item != ih_item_len(ih_check) || |
| 1156 | tb->insert_size[0] <= 0)) |
| 1157 | reiserfs_panic(tb->tb_sb, |
| 1158 | "PAP-12235", |
| 1159 | "pos_in_item must be equal to ih_item_len"); |
| 1160 | #endif |
| 1161 | |
| 1162 | leaf_mi = leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i], |
| 1163 | tb->sbytes[i], tb->S_new[i]); |
| 1164 | |
| 1165 | RFALSE(leaf_mi, |
| 1166 | "PAP-12240: unexpected value returned by leaf_move_items (%d)", |
| 1167 | leaf_mi); |
| 1168 | |
| 1169 | /* paste into item */ |
| 1170 | buffer_info_init_bh(tb, &bi, tb->S_new[i]); |
| 1171 | leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->snum[i], |
| 1172 | tb->pos_in_item, tb->insert_size[0], |
| 1173 | body, tb->zeroes_num); |
| 1174 | |
| 1175 | pasted = item_head(tb->S_new[i], tb->item_pos - n + |
| 1176 | tb->snum[i]); |
| 1177 | if (is_direntry_le_ih(pasted)) |
| 1178 | leaf_paste_entries(&bi, tb->item_pos - n + tb->snum[i], |
| 1179 | tb->pos_in_item, 1, |
| 1180 | (struct reiserfs_de_head *)body, |
| 1181 | body + DEH_SIZE, tb->insert_size[0]); |
| 1182 | |
| 1183 | /* if we paste to indirect item update ih_free_space */ |
| 1184 | if (is_indirect_le_ih(pasted)) |
| 1185 | set_ih_free_space(pasted, 0); |
| 1186 | |
| 1187 | tb->zeroes_num = tb->insert_size[0] = 0; |
| 1188 | |
| 1189 | } |
Jeff Mahoney | 9d49655 | 2014-04-23 10:00:52 -0400 | [diff] [blame] | 1190 | static void balance_leaf_new_nodes_paste(struct tree_balance *tb, |
| 1191 | struct item_head *ih, |
| 1192 | const char *body, |
| 1193 | struct item_head *insert_key, |
| 1194 | struct buffer_head **insert_ptr, |
| 1195 | int i) |
| 1196 | { |
| 1197 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 1198 | int n = B_NR_ITEMS(tbS0); |
Jeff Mahoney | 9d49655 | 2014-04-23 10:00:52 -0400 | [diff] [blame] | 1199 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1200 | /* pasted item doesn't fall into S_new[i] */ |
| 1201 | if (n - tb->snum[i] > tb->item_pos) { |
| 1202 | leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, |
| 1203 | tb->snum[i], tb->sbytes[i], tb->S_new[i]); |
| 1204 | return; |
| 1205 | } |
Jeff Mahoney | 9d49655 | 2014-04-23 10:00:52 -0400 | [diff] [blame] | 1206 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1207 | /* pasted item or part if it falls to S_new[i] */ |
Jeff Mahoney | 9d49655 | 2014-04-23 10:00:52 -0400 | [diff] [blame] | 1208 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1209 | if (tb->item_pos == n - tb->snum[i] && tb->sbytes[i] != -1) |
| 1210 | /* we must shift part of the appended item */ |
| 1211 | balance_leaf_new_nodes_paste_shift(tb, ih, body, insert_key, |
| 1212 | insert_ptr, i); |
| 1213 | else |
| 1214 | /* item falls wholly into S_new[i] */ |
| 1215 | balance_leaf_new_nodes_paste_whole(tb, ih, body, insert_key, |
| 1216 | insert_ptr, i); |
Jeff Mahoney | 9d49655 | 2014-04-23 10:00:52 -0400 | [diff] [blame] | 1217 | } |
| 1218 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1219 | /* Fill new nodes that appear in place of S[0] */ |
| 1220 | static void balance_leaf_new_nodes(struct tree_balance *tb, |
| 1221 | struct item_head *ih, |
| 1222 | const char *body, |
| 1223 | struct item_head *insert_key, |
| 1224 | struct buffer_head **insert_ptr, |
| 1225 | int flag) |
| 1226 | { |
| 1227 | int i; |
| 1228 | for (i = tb->blknum[0] - 2; i >= 0; i--) { |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1229 | BUG_ON(flag != M_INSERT && flag != M_PASTE); |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1230 | |
| 1231 | RFALSE(!tb->snum[i], |
| 1232 | "PAP-12200: snum[%d] == %d. Must be > 0", i, |
| 1233 | tb->snum[i]); |
| 1234 | |
| 1235 | /* here we shift from S to S_new nodes */ |
| 1236 | |
| 1237 | tb->S_new[i] = get_FEB(tb); |
| 1238 | |
| 1239 | /* initialized block type and tree level */ |
| 1240 | set_blkh_level(B_BLK_HEAD(tb->S_new[i]), DISK_LEAF_NODE_LEVEL); |
| 1241 | |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1242 | if (flag == M_INSERT) |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1243 | balance_leaf_new_nodes_insert(tb, ih, body, insert_key, |
| 1244 | insert_ptr, i); |
Jeff Mahoney | b54b8c9 | 2014-04-23 10:01:00 -0400 | [diff] [blame^] | 1245 | else /* M_PASTE */ |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1246 | balance_leaf_new_nodes_paste(tb, ih, body, insert_key, |
| 1247 | insert_ptr, i); |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1248 | |
| 1249 | memcpy(insert_key + i, leaf_key(tb->S_new[i], 0), KEY_SIZE); |
| 1250 | insert_ptr[i] = tb->S_new[i]; |
| 1251 | |
| 1252 | RFALSE(!buffer_journaled(tb->S_new[i]) |
| 1253 | || buffer_journal_dirty(tb->S_new[i]) |
| 1254 | || buffer_dirty(tb->S_new[i]), |
| 1255 | "PAP-12247: S_new[%d] : (%b)", |
| 1256 | i, format_bh(tb->S_new[i])); |
| 1257 | } |
| 1258 | } |
| 1259 | |
Jeff Mahoney | 8c480ea | 2014-04-23 10:00:53 -0400 | [diff] [blame] | 1260 | static void balance_leaf_finish_node_insert(struct tree_balance *tb, |
| 1261 | struct item_head *ih, |
| 1262 | const char *body) |
| 1263 | { |
| 1264 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 1265 | struct buffer_info bi; |
| 1266 | buffer_info_init_tbS0(tb, &bi); |
| 1267 | leaf_insert_into_buf(&bi, tb->item_pos, ih, |
| 1268 | body, tb->zeroes_num); |
| 1269 | |
| 1270 | /* |
| 1271 | * If we insert the first key |
| 1272 | * change the delimiting key |
| 1273 | */ |
| 1274 | if (tb->item_pos == 0) { |
| 1275 | if (tb->CFL[0]) /* can be 0 in reiserfsck */ |
| 1276 | replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); |
| 1277 | } |
| 1278 | } |
| 1279 | |
Jeff Mahoney | 7f447ba | 2014-04-23 10:00:54 -0400 | [diff] [blame] | 1280 | static void balance_leaf_finish_node_paste(struct tree_balance *tb, |
| 1281 | struct item_head *ih, |
| 1282 | const char *body) |
| 1283 | { |
| 1284 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
| 1285 | struct buffer_info bi; |
| 1286 | struct item_head *pasted; |
| 1287 | |
| 1288 | pasted = item_head(tbS0, tb->item_pos); |
| 1289 | /* when directory, may be new entry already pasted */ |
| 1290 | if (is_direntry_le_ih(pasted)) { |
| 1291 | if (tb->pos_in_item >= 0 && tb->pos_in_item <= ih_entry_count(pasted)) { |
| 1292 | |
| 1293 | RFALSE(!tb->insert_size[0], |
| 1294 | "PAP-12260: insert_size is 0 already"); |
| 1295 | |
| 1296 | /* prepare space */ |
| 1297 | buffer_info_init_tbS0(tb, &bi); |
| 1298 | leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item, |
| 1299 | tb->insert_size[0], body, |
| 1300 | tb->zeroes_num); |
| 1301 | |
| 1302 | /* paste entry */ |
| 1303 | leaf_paste_entries(&bi, tb->item_pos, tb->pos_in_item, 1, |
| 1304 | (struct reiserfs_de_head *)body, |
| 1305 | body + DEH_SIZE, |
| 1306 | tb->insert_size[0]); |
| 1307 | if (!tb->item_pos && !tb->pos_in_item) { |
| 1308 | RFALSE(!tb->CFL[0] || !tb->L[0], |
| 1309 | "PAP-12270: CFL[0]/L[0] must be specified"); |
| 1310 | if (tb->CFL[0]) |
| 1311 | replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0); |
| 1312 | } |
| 1313 | tb->insert_size[0] = 0; |
| 1314 | } |
| 1315 | } else { /* regular object */ |
| 1316 | if (tb->pos_in_item == ih_item_len(pasted)) { |
| 1317 | |
| 1318 | RFALSE(tb->insert_size[0] <= 0, |
| 1319 | "PAP-12275: insert size must not be %d", |
| 1320 | tb->insert_size[0]); |
| 1321 | buffer_info_init_tbS0(tb, &bi); |
| 1322 | leaf_paste_in_buffer(&bi, tb->item_pos, tb->pos_in_item, |
| 1323 | tb->insert_size[0], body, tb->zeroes_num); |
| 1324 | |
| 1325 | if (is_indirect_le_ih(pasted)) { |
| 1326 | #if 0 |
| 1327 | RFALSE(tb-> |
| 1328 | insert_size[0] != |
| 1329 | UNFM_P_SIZE, |
| 1330 | "PAP-12280: insert_size for indirect item must be %d, not %d", |
| 1331 | UNFM_P_SIZE, |
| 1332 | tb-> |
| 1333 | insert_size[0]); |
| 1334 | #endif |
| 1335 | set_ih_free_space(pasted, 0); |
| 1336 | } |
| 1337 | tb->insert_size[0] = 0; |
| 1338 | } |
| 1339 | #ifdef CONFIG_REISERFS_CHECK |
| 1340 | else { |
| 1341 | if (tb->insert_size[0]) { |
| 1342 | print_cur_tb("12285"); |
| 1343 | reiserfs_panic(tb->tb_sb, |
| 1344 | "PAP-12285", |
| 1345 | "insert_size " |
| 1346 | "must be 0 " |
| 1347 | "(%d)", |
| 1348 | tb->insert_size[0]); |
| 1349 | } |
| 1350 | } |
| 1351 | #endif /* CONFIG_REISERFS_CHECK */ |
| 1352 | |
| 1353 | } |
| 1354 | } |
| 1355 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1356 | /* |
| 1357 | * if the affected item was not wholly shifted then we |
| 1358 | * perform all necessary operations on that part or whole |
| 1359 | * of the affected item which remains in S |
| 1360 | */ |
| 1361 | static void balance_leaf_finish_node(struct tree_balance *tb, |
| 1362 | struct item_head *ih, |
| 1363 | const char *body, int flag) |
| 1364 | { |
| 1365 | /* if we must insert or append into buffer S[0] */ |
| 1366 | if (0 <= tb->item_pos && tb->item_pos < tb->s0num) { |
| 1367 | if (flag == M_INSERT) |
| 1368 | balance_leaf_finish_node_insert(tb, ih, body); |
| 1369 | else /* M_PASTE */ |
| 1370 | balance_leaf_finish_node_paste(tb, ih, body); |
| 1371 | } |
| 1372 | } |
| 1373 | |
Jeff Mahoney | 3f0eb27 | 2014-04-23 10:00:50 -0400 | [diff] [blame] | 1374 | /** |
| 1375 | * balance_leaf - reiserfs tree balancing algorithm |
| 1376 | * @tb: tree balance state |
| 1377 | * @ih: item header of inserted item (little endian) |
| 1378 | * @body: body of inserted item or bytes to paste |
| 1379 | * @flag: i - insert, d - delete, c - cut, p - paste (see do_balance) |
| 1380 | * passed back: |
| 1381 | * @insert_key: key to insert new nodes |
| 1382 | * @insert_ptr: array of nodes to insert at the next level |
| 1383 | * |
| 1384 | * In our processing of one level we sometimes determine what must be |
| 1385 | * inserted into the next higher level. This insertion consists of a |
| 1386 | * key or two keys and their corresponding pointers. |
| 1387 | */ |
| 1388 | static int balance_leaf(struct tree_balance *tb, struct item_head *ih, |
| 1389 | const char *body, int flag, |
| 1390 | struct item_head *insert_key, |
| 1391 | struct buffer_head **insert_ptr) |
| 1392 | { |
| 1393 | struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path); |
Jeff Mahoney | 3f0eb27 | 2014-04-23 10:00:50 -0400 | [diff] [blame] | 1394 | |
| 1395 | PROC_INFO_INC(tb->tb_sb, balance_at[0]); |
| 1396 | |
| 1397 | /* Make balance in case insert_size[0] < 0 */ |
| 1398 | if (tb->insert_size[0] < 0) |
| 1399 | return balance_leaf_when_delete(tb, flag); |
| 1400 | |
| 1401 | tb->item_pos = PATH_LAST_POSITION(tb->tb_path), |
| 1402 | tb->pos_in_item = tb->tb_path->pos_in_item, |
| 1403 | tb->zeroes_num = 0; |
| 1404 | if (flag == M_INSERT && !body) |
| 1405 | tb->zeroes_num = ih_item_len(ih); |
| 1406 | |
| 1407 | /* |
| 1408 | * for indirect item pos_in_item is measured in unformatted node |
| 1409 | * pointers. Recalculate to bytes |
| 1410 | */ |
| 1411 | if (flag != M_INSERT |
| 1412 | && is_indirect_le_ih(item_head(tbS0, tb->item_pos))) |
| 1413 | tb->pos_in_item *= UNFM_P_SIZE; |
| 1414 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1415 | balance_leaf_left(tb, ih, body, flag); |
Jeff Mahoney | 3f0eb27 | 2014-04-23 10:00:50 -0400 | [diff] [blame] | 1416 | |
| 1417 | /* tb->lnum[0] > 0 */ |
| 1418 | /* Calculate new item position */ |
| 1419 | tb->item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0)); |
| 1420 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1421 | balance_leaf_right(tb, ih, body, flag); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1422 | |
| 1423 | /* tb->rnum[0] > 0 */ |
| 1424 | RFALSE(tb->blknum[0] > 3, |
Dave Jones | 416e2ab | 2014-02-17 16:21:24 -0500 | [diff] [blame] | 1425 | "PAP-12180: blknum can not be %d. It must be <= 3", tb->blknum[0]); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1426 | RFALSE(tb->blknum[0] < 0, |
Dave Jones | 416e2ab | 2014-02-17 16:21:24 -0500 | [diff] [blame] | 1427 | "PAP-12185: blknum can not be %d. It must be >= 0", tb->blknum[0]); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1428 | |
Jeff Mahoney | 97fd4b9 | 2014-04-23 10:00:45 -0400 | [diff] [blame] | 1429 | /* |
| 1430 | * if while adding to a node we discover that it is possible to split |
| 1431 | * it in two, and merge the left part into the left neighbor and the |
| 1432 | * right part into the right neighbor, eliminating the node |
| 1433 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1434 | if (tb->blknum[0] == 0) { /* node S[0] is empty now */ |
| 1435 | |
| 1436 | RFALSE(!tb->lnum[0] || !tb->rnum[0], |
| 1437 | "PAP-12190: lnum and rnum must not be zero"); |
Jeff Mahoney | 97fd4b9 | 2014-04-23 10:00:45 -0400 | [diff] [blame] | 1438 | /* |
| 1439 | * if insertion was done before 0-th position in R[0], right |
| 1440 | * delimiting key of the tb->L[0]'s and left delimiting key are |
| 1441 | * not set correctly |
| 1442 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1443 | if (tb->CFL[0]) { |
| 1444 | if (!tb->CFR[0]) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1445 | reiserfs_panic(tb->tb_sb, "vs-12195", |
| 1446 | "CFR not initialized"); |
Jeff Mahoney | 4cf5f7a | 2014-04-23 10:00:35 -0400 | [diff] [blame] | 1447 | copy_key(internal_key(tb->CFL[0], tb->lkey[0]), |
| 1448 | internal_key(tb->CFR[0], tb->rkey[0])); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1449 | do_balance_mark_internal_dirty(tb, tb->CFL[0], 0); |
| 1450 | } |
| 1451 | |
| 1452 | reiserfs_invalidate_buffer(tb, tbS0); |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1456 | balance_leaf_new_nodes(tb, ih, body, insert_key, insert_ptr, flag); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1457 | |
Jeff Mahoney | 0080e9f | 2014-04-23 10:00:55 -0400 | [diff] [blame] | 1458 | balance_leaf_finish_node(tb, ih, body, flag); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1459 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1460 | #ifdef CONFIG_REISERFS_CHECK |
| 1461 | if (flag == M_PASTE && tb->insert_size[0]) { |
| 1462 | print_cur_tb("12290"); |
| 1463 | reiserfs_panic(tb->tb_sb, |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1464 | "PAP-12290", "insert_size is still not 0 (%d)", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1465 | tb->insert_size[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } |
Jeff Mahoney | 97fd4b9 | 2014-04-23 10:00:45 -0400 | [diff] [blame] | 1467 | #endif |
| 1468 | |
| 1469 | /* Leaf level of the tree is balanced (end of balance_leaf) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | return 0; |
Jeff Mahoney | 97fd4b9 | 2014-04-23 10:00:45 -0400 | [diff] [blame] | 1471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
| 1473 | /* Make empty node */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1474 | void make_empty_node(struct buffer_info *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1476 | struct block_head *blkh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1478 | RFALSE(bi->bi_bh == NULL, "PAP-12295: pointer to the buffer is NULL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1480 | blkh = B_BLK_HEAD(bi->bi_bh); |
| 1481 | set_blkh_nr_item(blkh, 0); |
| 1482 | set_blkh_free_space(blkh, MAX_CHILD_SIZE(bi->bi_bh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1484 | if (bi->bi_parent) |
| 1485 | B_N_CHILD(bi->bi_parent, bi->bi_position)->dc_size = 0; /* Endian safe if 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | /* Get first empty buffer */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1489 | struct buffer_head *get_FEB(struct tree_balance *tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1491 | int i; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1492 | struct buffer_info bi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1494 | for (i = 0; i < MAX_FEB_SIZE; i++) |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 1495 | if (tb->FEB[i] != NULL) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1496 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1498 | if (i == MAX_FEB_SIZE) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1499 | reiserfs_panic(tb->tb_sb, "vs-12300", "FEB list is empty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
Jeff Mahoney | fba4ebb | 2009-03-30 14:02:42 -0400 | [diff] [blame] | 1501 | buffer_info_init_bh(tb, &bi, tb->FEB[i]); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1502 | make_empty_node(&bi); |
Jeff Mahoney | fba4ebb | 2009-03-30 14:02:42 -0400 | [diff] [blame] | 1503 | set_buffer_uptodate(tb->FEB[i]); |
| 1504 | tb->used[i] = tb->FEB[i]; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1505 | tb->FEB[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Jeff Mahoney | fba4ebb | 2009-03-30 14:02:42 -0400 | [diff] [blame] | 1507 | return tb->used[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1510 | /* This is now used because reiserfs_free_block has to be able to schedule. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1511 | static void store_thrown(struct tree_balance *tb, struct buffer_head *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1513 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1515 | if (buffer_dirty(bh)) |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1516 | reiserfs_warning(tb->tb_sb, "reiserfs-12320", |
| 1517 | "called with dirty buffer"); |
Ahmed S. Darwish | 79a81ae | 2007-02-12 00:52:09 -0800 | [diff] [blame] | 1518 | for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1519 | if (!tb->thrown[i]) { |
| 1520 | tb->thrown[i] = bh; |
| 1521 | get_bh(bh); /* free_thrown puts this */ |
| 1522 | return; |
| 1523 | } |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1524 | reiserfs_warning(tb->tb_sb, "reiserfs-12321", |
| 1525 | "too many thrown buffers"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1528 | static void free_thrown(struct tree_balance *tb) |
| 1529 | { |
| 1530 | int i; |
| 1531 | b_blocknr_t blocknr; |
Ahmed S. Darwish | 79a81ae | 2007-02-12 00:52:09 -0800 | [diff] [blame] | 1532 | for (i = 0; i < ARRAY_SIZE(tb->thrown); i++) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1533 | if (tb->thrown[i]) { |
| 1534 | blocknr = tb->thrown[i]->b_blocknr; |
| 1535 | if (buffer_dirty(tb->thrown[i])) |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1536 | reiserfs_warning(tb->tb_sb, "reiserfs-12322", |
| 1537 | "called with dirty buffer %d", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1538 | blocknr); |
| 1539 | brelse(tb->thrown[i]); /* incremented in store_thrown */ |
| 1540 | reiserfs_free_block(tb->transaction_handle, NULL, |
| 1541 | blocknr, 0); |
| 1542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1546 | void reiserfs_invalidate_buffer(struct tree_balance *tb, struct buffer_head *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1548 | struct block_head *blkh; |
| 1549 | blkh = B_BLK_HEAD(bh); |
| 1550 | set_blkh_level(blkh, FREE_LEVEL); |
| 1551 | set_blkh_nr_item(blkh, 0); |
| 1552 | |
| 1553 | clear_buffer_dirty(bh); |
| 1554 | store_thrown(tb, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | /* Replace n_dest'th key in buffer dest by n_src'th key of buffer src.*/ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1558 | void replace_key(struct tree_balance *tb, struct buffer_head *dest, int n_dest, |
| 1559 | struct buffer_head *src, int n_src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | { |
| 1561 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1562 | RFALSE(dest == NULL || src == NULL, |
| 1563 | "vs-12305: source or destination buffer is 0 (src=%p, dest=%p)", |
| 1564 | src, dest); |
| 1565 | RFALSE(!B_IS_KEYS_LEVEL(dest), |
| 1566 | "vs-12310: invalid level (%z) for destination buffer. dest must be leaf", |
| 1567 | dest); |
| 1568 | RFALSE(n_dest < 0 || n_src < 0, |
| 1569 | "vs-12315: src(%d) or dest(%d) key number < 0", n_src, n_dest); |
| 1570 | RFALSE(n_dest >= B_NR_ITEMS(dest) || n_src >= B_NR_ITEMS(src), |
| 1571 | "vs-12320: src(%d(%d)) or dest(%d(%d)) key number is too big", |
| 1572 | n_src, B_NR_ITEMS(src), n_dest, B_NR_ITEMS(dest)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1574 | if (B_IS_ITEMS_LEVEL(src)) |
| 1575 | /* source buffer contains leaf node */ |
Jeff Mahoney | 4cf5f7a | 2014-04-23 10:00:35 -0400 | [diff] [blame] | 1576 | memcpy(internal_key(dest, n_dest), item_head(src, n_src), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1577 | KEY_SIZE); |
| 1578 | else |
Jeff Mahoney | 4cf5f7a | 2014-04-23 10:00:35 -0400 | [diff] [blame] | 1579 | memcpy(internal_key(dest, n_dest), internal_key(src, n_src), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1580 | KEY_SIZE); |
| 1581 | |
| 1582 | do_balance_mark_internal_dirty(tb, dest, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1585 | int get_left_neighbor_position(struct tree_balance *tb, int h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1587 | int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 1589 | RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FL[h] == NULL, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1590 | "vs-12325: FL[%d](%p) or F[%d](%p) does not exist", |
| 1591 | h, tb->FL[h], h, PATH_H_PPARENT(tb->tb_path, h)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1593 | if (Sh_position == 0) |
| 1594 | return B_NR_ITEMS(tb->FL[h]); |
| 1595 | else |
| 1596 | return Sh_position - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1599 | int get_right_neighbor_position(struct tree_balance *tb, int h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1601 | int Sh_position = PATH_H_POSITION(tb->tb_path, h + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 1603 | RFALSE(PATH_H_PPARENT(tb->tb_path, h) == NULL || tb->FR[h] == NULL, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1604 | "vs-12330: F[%d](%p) or FR[%d](%p) does not exist", |
| 1605 | h, PATH_H_PPARENT(tb->tb_path, h), h, tb->FR[h]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1607 | if (Sh_position == B_NR_ITEMS(PATH_H_PPARENT(tb->tb_path, h))) |
| 1608 | return 0; |
| 1609 | else |
| 1610 | return Sh_position + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | #ifdef CONFIG_REISERFS_CHECK |
| 1614 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1615 | int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value); |
| 1616 | static void check_internal_node(struct super_block *s, struct buffer_head *bh, |
| 1617 | char *mes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1619 | struct disk_child *dc; |
| 1620 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1622 | RFALSE(!bh, "PAP-12336: bh == 0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1624 | if (!bh || !B_IS_IN_TREE(bh)) |
| 1625 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1627 | RFALSE(!buffer_dirty(bh) && |
| 1628 | !(buffer_journaled(bh) || buffer_journal_dirty(bh)), |
| 1629 | "PAP-12337: buffer (%b) must be dirty", bh); |
| 1630 | dc = B_N_CHILD(bh, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1632 | for (i = 0; i <= B_NR_ITEMS(bh); i++, dc++) { |
| 1633 | if (!is_reusable(s, dc_block_number(dc), 1)) { |
| 1634 | print_cur_tb(mes); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1635 | reiserfs_panic(s, "PAP-12338", |
| 1636 | "invalid child pointer %y in %b", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1637 | dc, bh); |
| 1638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1642 | static int locked_or_not_in_tree(struct tree_balance *tb, |
| 1643 | struct buffer_head *bh, char *which) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1644 | { |
| 1645 | if ((!buffer_journal_prepared(bh) && buffer_locked(bh)) || |
| 1646 | !B_IS_IN_TREE(bh)) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1647 | reiserfs_warning(tb->tb_sb, "vs-12339", "%s (%b)", which, bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1648 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1650 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1653 | static int check_before_balancing(struct tree_balance *tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1655 | int retval = 0; |
| 1656 | |
Frederic Weisbecker | 08f14fc | 2009-05-16 19:10:38 +0200 | [diff] [blame] | 1657 | if (REISERFS_SB(tb->tb_sb)->cur_tb) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1658 | reiserfs_panic(tb->tb_sb, "vs-12335", "suspect that schedule " |
| 1659 | "occurred based on cur_tb not being null at " |
| 1660 | "this point in code. do_balance cannot properly " |
Frederic Weisbecker | 08f14fc | 2009-05-16 19:10:38 +0200 | [diff] [blame] | 1661 | "handle concurrent tree accesses on a same " |
| 1662 | "mount point."); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1665 | /* |
| 1666 | * double check that buffers that we will modify are unlocked. |
| 1667 | * (fix_nodes should already have prepped all of these for us). |
| 1668 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1669 | if (tb->lnum[0]) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1670 | retval |= locked_or_not_in_tree(tb, tb->L[0], "L[0]"); |
| 1671 | retval |= locked_or_not_in_tree(tb, tb->FL[0], "FL[0]"); |
| 1672 | retval |= locked_or_not_in_tree(tb, tb->CFL[0], "CFL[0]"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1673 | check_leaf(tb->L[0]); |
| 1674 | } |
| 1675 | if (tb->rnum[0]) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1676 | retval |= locked_or_not_in_tree(tb, tb->R[0], "R[0]"); |
| 1677 | retval |= locked_or_not_in_tree(tb, tb->FR[0], "FR[0]"); |
| 1678 | retval |= locked_or_not_in_tree(tb, tb->CFR[0], "CFR[0]"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1679 | check_leaf(tb->R[0]); |
| 1680 | } |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1681 | retval |= locked_or_not_in_tree(tb, PATH_PLAST_BUFFER(tb->tb_path), |
| 1682 | "S[0]"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1683 | check_leaf(PATH_PLAST_BUFFER(tb->tb_path)); |
| 1684 | |
| 1685 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1688 | static void check_after_balance_leaf(struct tree_balance *tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1690 | if (tb->lnum[0]) { |
| 1691 | if (B_FREE_SPACE(tb->L[0]) != |
| 1692 | MAX_CHILD_SIZE(tb->L[0]) - |
| 1693 | dc_size(B_N_CHILD |
| 1694 | (tb->FL[0], get_left_neighbor_position(tb, 0)))) { |
| 1695 | print_cur_tb("12221"); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1696 | reiserfs_panic(tb->tb_sb, "PAP-12355", |
| 1697 | "shift to left was incorrect"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | if (tb->rnum[0]) { |
| 1701 | if (B_FREE_SPACE(tb->R[0]) != |
| 1702 | MAX_CHILD_SIZE(tb->R[0]) - |
| 1703 | dc_size(B_N_CHILD |
| 1704 | (tb->FR[0], get_right_neighbor_position(tb, 0)))) { |
| 1705 | print_cur_tb("12222"); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1706 | reiserfs_panic(tb->tb_sb, "PAP-12360", |
| 1707 | "shift to right was incorrect"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | if (PATH_H_PBUFFER(tb->tb_path, 1) && |
| 1711 | (B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)) != |
| 1712 | (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) - |
| 1713 | dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1), |
| 1714 | PATH_H_POSITION(tb->tb_path, 1)))))) { |
| 1715 | int left = B_FREE_SPACE(PATH_H_PBUFFER(tb->tb_path, 0)); |
| 1716 | int right = (MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)) - |
| 1717 | dc_size(B_N_CHILD(PATH_H_PBUFFER(tb->tb_path, 1), |
| 1718 | PATH_H_POSITION(tb->tb_path, |
| 1719 | 1)))); |
| 1720 | print_cur_tb("12223"); |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1721 | reiserfs_warning(tb->tb_sb, "reiserfs-12363", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1722 | "B_FREE_SPACE (PATH_H_PBUFFER(tb->tb_path,0)) = %d; " |
| 1723 | "MAX_CHILD_SIZE (%d) - dc_size( %y, %d ) [%d] = %d", |
| 1724 | left, |
| 1725 | MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, 0)), |
| 1726 | PATH_H_PBUFFER(tb->tb_path, 1), |
| 1727 | PATH_H_POSITION(tb->tb_path, 1), |
| 1728 | dc_size(B_N_CHILD |
| 1729 | (PATH_H_PBUFFER(tb->tb_path, 1), |
| 1730 | PATH_H_POSITION(tb->tb_path, 1))), |
| 1731 | right); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1732 | reiserfs_panic(tb->tb_sb, "PAP-12365", "S is incorrect"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1733 | } |
| 1734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1736 | static void check_leaf_level(struct tree_balance *tb) |
| 1737 | { |
| 1738 | check_leaf(tb->L[0]); |
| 1739 | check_leaf(tb->R[0]); |
| 1740 | check_leaf(PATH_PLAST_BUFFER(tb->tb_path)); |
| 1741 | } |
| 1742 | |
| 1743 | static void check_internal_levels(struct tree_balance *tb) |
| 1744 | { |
| 1745 | int h; |
| 1746 | |
| 1747 | /* check all internal nodes */ |
| 1748 | for (h = 1; tb->insert_size[h]; h++) { |
| 1749 | check_internal_node(tb->tb_sb, PATH_H_PBUFFER(tb->tb_path, h), |
| 1750 | "BAD BUFFER ON PATH"); |
| 1751 | if (tb->lnum[h]) |
| 1752 | check_internal_node(tb->tb_sb, tb->L[h], "BAD L"); |
| 1753 | if (tb->rnum[h]) |
| 1754 | check_internal_node(tb->tb_sb, tb->R[h], "BAD R"); |
| 1755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | |
| 1757 | } |
| 1758 | |
| 1759 | #endif |
| 1760 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1761 | /* |
| 1762 | * Now we have all of the buffers that must be used in balancing of |
| 1763 | * the tree. We rely on the assumption that schedule() will not occur |
| 1764 | * while do_balance works. ( Only interrupt handlers are acceptable.) |
| 1765 | * We balance the tree according to the analysis made before this, |
| 1766 | * using buffers already obtained. For SMP support it will someday be |
| 1767 | * necessary to add ordered locking of tb. |
| 1768 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1770 | /* |
| 1771 | * Some interesting rules of balancing: |
| 1772 | * we delete a maximum of two nodes per level per balancing: we never |
| 1773 | * delete R, when we delete two of three nodes L, S, R then we move |
| 1774 | * them into R. |
| 1775 | * |
| 1776 | * we only delete L if we are deleting two nodes, if we delete only |
| 1777 | * one node we delete S |
| 1778 | * |
| 1779 | * if we shift leaves then we shift as much as we can: this is a |
| 1780 | * deliberate policy of extremism in node packing which results in |
| 1781 | * higher average utilization after repeated random balance operations |
| 1782 | * at the cost of more memory copies and more balancing as a result of |
| 1783 | * small insertions to full nodes. |
| 1784 | * |
| 1785 | * if we shift internal nodes we try to evenly balance the node |
| 1786 | * utilization, with consequent less balancing at the cost of lower |
| 1787 | * utilization. |
| 1788 | * |
| 1789 | * one could argue that the policy for directories in leaves should be |
| 1790 | * that of internal nodes, but we will wait until another day to |
| 1791 | * evaluate this.... It would be nice to someday measure and prove |
| 1792 | * these assumptions as to what is optimal.... |
| 1793 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1795 | static inline void do_balance_starts(struct tree_balance *tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1797 | /* use print_cur_tb() to see initial state of struct tree_balance */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1799 | /* store_print_tb (tb); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1801 | /* do not delete, just comment it out */ |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1802 | /* |
| 1803 | print_tb(flag, PATH_LAST_POSITION(tb->tb_path), |
| 1804 | tb->tb_path->pos_in_item, tb, "check"); |
| 1805 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1806 | RFALSE(check_before_balancing(tb), "PAP-12340: locked buffers in TB"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | #ifdef CONFIG_REISERFS_CHECK |
Frederic Weisbecker | 08f14fc | 2009-05-16 19:10:38 +0200 | [diff] [blame] | 1808 | REISERFS_SB(tb->tb_sb)->cur_tb = tb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | #endif |
| 1810 | } |
| 1811 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1812 | static inline void do_balance_completed(struct tree_balance *tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1816 | check_leaf_level(tb); |
| 1817 | check_internal_levels(tb); |
Frederic Weisbecker | 08f14fc | 2009-05-16 19:10:38 +0200 | [diff] [blame] | 1818 | REISERFS_SB(tb->tb_sb)->cur_tb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | #endif |
| 1820 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1821 | /* |
| 1822 | * reiserfs_free_block is no longer schedule safe. So, we need to |
| 1823 | * put the buffers we want freed on the thrown list during do_balance, |
| 1824 | * and then free them now |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1825 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1827 | REISERFS_SB(tb->tb_sb)->s_do_balance++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1829 | /* release all nodes hold to perform the balancing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | unfix_nodes(tb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1832 | free_thrown(tb); |
| 1833 | } |
| 1834 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1835 | /* |
| 1836 | * do_balance - balance the tree |
| 1837 | * |
| 1838 | * @tb: tree_balance structure |
| 1839 | * @ih: item header of inserted item |
| 1840 | * @body: body of inserted item or bytes to paste |
| 1841 | * @flag: 'i' - insert, 'd' - delete, 'c' - cut, 'p' paste |
| 1842 | * |
| 1843 | * Cut means delete part of an item (includes removing an entry from a |
| 1844 | * directory). |
| 1845 | * |
| 1846 | * Delete means delete whole item. |
| 1847 | * |
| 1848 | * Insert means add a new item into the tree. |
| 1849 | * |
| 1850 | * Paste means to append to the end of an existing file or to |
| 1851 | * insert a directory entry. |
| 1852 | */ |
| 1853 | void do_balance(struct tree_balance *tb, struct item_head *ih, |
| 1854 | const char *body, int flag) |
| 1855 | { |
| 1856 | int child_pos; /* position of a child node in its parent */ |
| 1857 | int h; /* level of the tree being processed */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1858 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1859 | /* |
| 1860 | * in our processing of one level we sometimes determine what |
| 1861 | * must be inserted into the next higher level. This insertion |
| 1862 | * consists of a key or two keys and their corresponding |
| 1863 | * pointers |
| 1864 | */ |
| 1865 | struct item_head insert_key[2]; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1866 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1867 | /* inserted node-ptrs for the next level */ |
| 1868 | struct buffer_head *insert_ptr[2]; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1869 | |
| 1870 | tb->tb_mode = flag; |
| 1871 | tb->need_balance_dirty = 0; |
| 1872 | |
| 1873 | if (FILESYSTEM_CHANGED_TB(tb)) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1874 | reiserfs_panic(tb->tb_sb, "clm-6000", "fs generation has " |
| 1875 | "changed"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1876 | } |
| 1877 | /* if we have no real work to do */ |
| 1878 | if (!tb->insert_size[0]) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1879 | reiserfs_warning(tb->tb_sb, "PAP-12350", |
| 1880 | "insert_size == 0, mode == %c", flag); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1881 | unfix_nodes(tb); |
| 1882 | return; |
| 1883 | } |
| 1884 | |
Jeff Mahoney | a228bf8 | 2014-04-23 10:00:42 -0400 | [diff] [blame] | 1885 | atomic_inc(&fs_generation(tb->tb_sb)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1886 | do_balance_starts(tb); |
| 1887 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 1888 | /* |
| 1889 | * balance_leaf returns 0 except if combining L R and S into |
| 1890 | * one node. see balance_internal() for explanation of this |
| 1891 | * line of code. |
| 1892 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1893 | child_pos = PATH_H_B_ITEM_ORDER(tb->tb_path, 0) + |
| 1894 | balance_leaf(tb, ih, body, flag, insert_key, insert_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
| 1896 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1897 | check_after_balance_leaf(tb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | #endif |
| 1899 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1900 | /* Balance internal level of the tree. */ |
| 1901 | for (h = 1; h < MAX_HEIGHT && tb->insert_size[h]; h++) |
| 1902 | child_pos = |
| 1903 | balance_internal(tb, h, child_pos, insert_key, insert_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1905 | do_balance_completed(tb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | |
| 1907 | } |