Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 19 | #include "ctree.h" |
| 20 | #include "disk-io.h" |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 21 | #include "transaction.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 22 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 23 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 24 | *root, struct btrfs_path *path, int level); |
| 25 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 26 | *root, struct btrfs_key *ins_key, |
| 27 | struct btrfs_path *path, int data_size); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 28 | static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 29 | *root, struct buffer_head *dst, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 30 | *src); |
| 31 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 32 | btrfs_root *root, struct buffer_head *dst_buf, |
| 33 | struct buffer_head *src_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 34 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 35 | struct btrfs_path *path, int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 36 | |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 37 | inline void btrfs_init_path(struct btrfs_path *p) |
| 38 | { |
| 39 | memset(p, 0, sizeof(*p)); |
| 40 | } |
| 41 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 42 | struct btrfs_path *btrfs_alloc_path(void) |
| 43 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 44 | struct btrfs_path *path; |
| 45 | path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 46 | if (path) { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 47 | btrfs_init_path(path); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 48 | path->reada = 1; |
| 49 | } |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 50 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void btrfs_free_path(struct btrfs_path *p) |
| 54 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 55 | btrfs_release_path(NULL, p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 56 | kmem_cache_free(btrfs_path_cachep, p); |
| 57 | } |
| 58 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 59 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 60 | { |
| 61 | int i; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 62 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 63 | if (!p->nodes[i]) |
| 64 | break; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 65 | btrfs_block_release(root, p->nodes[i]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 66 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 67 | memset(p, 0, sizeof(*p)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 70 | static int __btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root |
| 71 | *root, struct buffer_head *buf, struct buffer_head |
| 72 | *parent, int parent_slot, struct buffer_head |
| 73 | **cow_ret, u64 search_start, u64 empty_size) |
| 74 | { |
| 75 | struct buffer_head *cow; |
| 76 | struct btrfs_node *cow_node; |
| 77 | int ret = 0; |
| 78 | int different_trans = 0; |
| 79 | |
| 80 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
| 81 | WARN_ON(!buffer_uptodate(buf)); |
| 82 | cow = btrfs_alloc_free_block(trans, root, search_start, empty_size); |
| 83 | if (IS_ERR(cow)) |
| 84 | return PTR_ERR(cow); |
| 85 | |
| 86 | cow_node = btrfs_buffer_node(cow); |
| 87 | if (buf->b_size != root->blocksize || cow->b_size != root->blocksize) |
| 88 | WARN_ON(1); |
| 89 | |
| 90 | memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize); |
| 91 | btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow)); |
| 92 | btrfs_set_header_generation(&cow_node->header, trans->transid); |
| 93 | btrfs_set_header_owner(&cow_node->header, root->root_key.objectid); |
| 94 | |
| 95 | WARN_ON(btrfs_header_generation(btrfs_buffer_header(buf)) > |
| 96 | trans->transid); |
| 97 | if (btrfs_header_generation(btrfs_buffer_header(buf)) != |
| 98 | trans->transid) { |
| 99 | different_trans = 1; |
| 100 | ret = btrfs_inc_ref(trans, root, buf); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | } else { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 104 | clean_tree_block(trans, root, buf); |
| 105 | } |
| 106 | |
| 107 | if (buf == root->node) { |
| 108 | root->node = cow; |
| 109 | get_bh(cow); |
| 110 | if (buf != root->commit_root) { |
| 111 | btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); |
| 112 | } |
| 113 | btrfs_block_release(root, buf); |
| 114 | } else { |
| 115 | btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot, |
| 116 | bh_blocknr(cow)); |
| 117 | btrfs_mark_buffer_dirty(parent); |
| 118 | WARN_ON(btrfs_header_generation(btrfs_buffer_header(parent)) != |
| 119 | trans->transid); |
| 120 | btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); |
| 121 | } |
| 122 | btrfs_block_release(root, buf); |
| 123 | btrfs_mark_buffer_dirty(cow); |
| 124 | *cow_ret = cow; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 129 | *root, struct buffer_head *buf, struct buffer_head |
| 130 | *parent, int parent_slot, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 131 | **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 132 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 133 | u64 search_start; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 134 | if (trans->transaction != root->fs_info->running_transaction) { |
| 135 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 136 | root->fs_info->running_transaction->transid); |
| 137 | WARN_ON(1); |
| 138 | } |
| 139 | if (trans->transid != root->fs_info->generation) { |
| 140 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 141 | root->fs_info->generation); |
| 142 | WARN_ON(1); |
| 143 | } |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 144 | if (btrfs_header_generation(btrfs_buffer_header(buf)) == |
| 145 | trans->transid) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 146 | *cow_ret = buf; |
| 147 | return 0; |
| 148 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 149 | |
| 150 | search_start = bh_blocknr(buf) & ~((u64)65535); |
| 151 | return __btrfs_cow_block(trans, root, buf, parent, |
| 152 | parent_slot, cow_ret, search_start, 0); |
| 153 | } |
| 154 | |
| 155 | static int close_blocks(u64 blocknr, u64 other) |
| 156 | { |
| 157 | if (blocknr < other && other - blocknr < 8) |
| 158 | return 1; |
| 159 | if (blocknr > other && blocknr - other < 8) |
| 160 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 164 | static int should_defrag_leaf(struct buffer_head *bh) |
| 165 | { |
| 166 | struct btrfs_leaf *leaf = btrfs_buffer_leaf(bh); |
| 167 | struct btrfs_disk_key *key; |
| 168 | u32 nritems; |
| 169 | |
| 170 | if (buffer_defrag(bh)) |
| 171 | return 1; |
| 172 | |
| 173 | nritems = btrfs_header_nritems(&leaf->header); |
| 174 | if (nritems == 0) |
| 175 | return 0; |
| 176 | |
| 177 | key = &leaf->items[0].key; |
| 178 | if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY) |
| 179 | return 1; |
| 180 | |
| 181 | key = &leaf->items[nritems-1].key; |
| 182 | if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY) |
| 183 | return 1; |
| 184 | if (nritems > 4) { |
| 185 | key = &leaf->items[nritems/2].key; |
| 186 | if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY) |
| 187 | return 1; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 192 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
| 193 | struct btrfs_root *root, struct buffer_head *parent, |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 194 | int cache_only, u64 *last_ret) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 195 | { |
| 196 | struct btrfs_node *parent_node; |
| 197 | struct buffer_head *cur_bh; |
| 198 | struct buffer_head *tmp_bh; |
| 199 | u64 blocknr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 200 | u64 search_start = *last_ret; |
| 201 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 202 | u64 other; |
| 203 | u32 parent_nritems; |
| 204 | int start_slot; |
| 205 | int end_slot; |
| 206 | int i; |
| 207 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 208 | int parent_level; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 209 | |
| 210 | if (trans->transaction != root->fs_info->running_transaction) { |
| 211 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 212 | root->fs_info->running_transaction->transid); |
| 213 | WARN_ON(1); |
| 214 | } |
| 215 | if (trans->transid != root->fs_info->generation) { |
| 216 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 217 | root->fs_info->generation); |
| 218 | WARN_ON(1); |
| 219 | } |
| 220 | parent_node = btrfs_buffer_node(parent); |
| 221 | parent_nritems = btrfs_header_nritems(&parent_node->header); |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 222 | parent_level = btrfs_header_level(&parent_node->header); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 223 | |
| 224 | start_slot = 0; |
| 225 | end_slot = parent_nritems; |
| 226 | |
| 227 | if (parent_nritems == 1) |
| 228 | return 0; |
| 229 | |
| 230 | for (i = start_slot; i < end_slot; i++) { |
| 231 | int close = 1; |
| 232 | blocknr = btrfs_node_blockptr(parent_node, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 233 | if (last_block == 0) |
| 234 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 235 | if (i > 0) { |
| 236 | other = btrfs_node_blockptr(parent_node, i - 1); |
| 237 | close = close_blocks(blocknr, other); |
| 238 | } |
| 239 | if (close && i < end_slot - 1) { |
| 240 | other = btrfs_node_blockptr(parent_node, i + 1); |
| 241 | close = close_blocks(blocknr, other); |
| 242 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 243 | if (close) { |
| 244 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 245 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 246 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 247 | |
| 248 | cur_bh = btrfs_find_tree_block(root, blocknr); |
| 249 | if (!cur_bh || !buffer_uptodate(cur_bh) || |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 250 | buffer_locked(cur_bh) || |
| 251 | (parent_level != 1 && !buffer_defrag(cur_bh)) || |
| 252 | (parent_level == 1 && !should_defrag_leaf(cur_bh))) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 253 | if (cache_only) { |
| 254 | brelse(cur_bh); |
| 255 | continue; |
| 256 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 257 | if (!cur_bh || !buffer_uptodate(cur_bh) || |
| 258 | buffer_locked(cur_bh)) { |
| 259 | brelse(cur_bh); |
| 260 | cur_bh = read_tree_block(root, blocknr); |
| 261 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 262 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 263 | if (search_start == 0) |
| 264 | search_start = last_block & ~((u64)65535); |
| 265 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 266 | err = __btrfs_cow_block(trans, root, cur_bh, parent, i, |
| 267 | &tmp_bh, search_start, |
| 268 | min(8, end_slot - i)); |
| 269 | if (err) |
| 270 | break; |
| 271 | search_start = bh_blocknr(tmp_bh); |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 272 | *last_ret = search_start; |
| 273 | if (parent_level == 1) |
| 274 | clear_buffer_defrag(tmp_bh); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 275 | brelse(tmp_bh); |
| 276 | } |
| 277 | return err; |
| 278 | } |
| 279 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 280 | /* |
| 281 | * The leaf data grows from end-to-front in the node. |
| 282 | * this returns the address of the start of the last item, |
| 283 | * which is the stop of the leaf data stack |
| 284 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 285 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
| 286 | struct btrfs_leaf *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 287 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 288 | u32 nr = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 289 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 290 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 291 | return btrfs_item_offset(leaf->items + nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 294 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 295 | * compare two keys in a memcmp fashion |
| 296 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 297 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 298 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 299 | struct btrfs_key k1; |
| 300 | |
| 301 | btrfs_disk_key_to_cpu(&k1, disk); |
| 302 | |
| 303 | if (k1.objectid > k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 304 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 305 | if (k1.objectid < k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 306 | return -1; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 307 | if (k1.flags > k2->flags) |
| 308 | return 1; |
| 309 | if (k1.flags < k2->flags) |
| 310 | return -1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 311 | if (k1.offset > k2->offset) |
| 312 | return 1; |
| 313 | if (k1.offset < k2->offset) |
| 314 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 315 | return 0; |
| 316 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 317 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 318 | static int check_node(struct btrfs_root *root, struct btrfs_path *path, |
| 319 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 320 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 321 | struct btrfs_node *parent = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 322 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 323 | int parent_slot; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 324 | int slot; |
| 325 | struct btrfs_key cpukey; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 326 | u32 nritems = btrfs_header_nritems(&node->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 327 | |
| 328 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 329 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 330 | |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 331 | slot = path->slots[level]; |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 332 | BUG_ON(!buffer_uptodate(path->nodes[level])); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 333 | BUG_ON(nritems == 0); |
| 334 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 335 | struct btrfs_disk_key *parent_key; |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 336 | |
| 337 | parent_slot = path->slots[level + 1]; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 338 | parent_key = &parent->ptrs[parent_slot].key; |
| 339 | BUG_ON(memcmp(parent_key, &node->ptrs[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 340 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 341 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 342 | btrfs_header_blocknr(&node->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 343 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 344 | BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 345 | if (slot != 0) { |
| 346 | btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key); |
| 347 | BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0); |
| 348 | } |
| 349 | if (slot < nritems - 1) { |
| 350 | btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key); |
| 351 | BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 356 | static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 357 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 358 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 359 | struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 360 | struct btrfs_node *parent = NULL; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 361 | int parent_slot; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 362 | int slot = path->slots[0]; |
| 363 | struct btrfs_key cpukey; |
| 364 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 365 | u32 nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 366 | |
| 367 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 368 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 369 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 370 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 371 | |
| 372 | if (nritems == 0) |
| 373 | return 0; |
| 374 | |
| 375 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 376 | struct btrfs_disk_key *parent_key; |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 377 | |
| 378 | parent_slot = path->slots[level + 1]; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 379 | parent_key = &parent->ptrs[parent_slot].key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 380 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 381 | BUG_ON(memcmp(parent_key, &leaf->items[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 382 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 383 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 384 | btrfs_header_blocknr(&leaf->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 385 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 386 | if (slot != 0) { |
| 387 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key); |
| 388 | BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0); |
| 389 | BUG_ON(btrfs_item_offset(leaf->items + slot - 1) != |
| 390 | btrfs_item_end(leaf->items + slot)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 391 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 392 | if (slot < nritems - 1) { |
| 393 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key); |
| 394 | BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0); |
| 395 | BUG_ON(btrfs_item_offset(leaf->items + slot) != |
| 396 | btrfs_item_end(leaf->items + slot + 1)); |
| 397 | } |
| 398 | BUG_ON(btrfs_item_offset(leaf->items) + |
| 399 | btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 403 | static int check_block(struct btrfs_root *root, struct btrfs_path *path, |
| 404 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 405 | { |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 406 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
| 407 | if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid, |
| 408 | sizeof(node->header.fsid))) |
| 409 | BUG(); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 410 | if (level == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 411 | return check_leaf(root, path, level); |
| 412 | return check_node(root, path, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 415 | /* |
| 416 | * search for key in the array p. items p are item_size apart |
| 417 | * and there are 'max' items in p |
| 418 | * the slot in the array is returned via slot, and it points to |
| 419 | * the place where you would insert key if it is not found in |
| 420 | * the array. |
| 421 | * |
| 422 | * slot may point to max if the key is bigger than all of the keys |
| 423 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 424 | static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 425 | int max, int *slot) |
| 426 | { |
| 427 | int low = 0; |
| 428 | int high = max; |
| 429 | int mid; |
| 430 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 431 | struct btrfs_disk_key *tmp; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 432 | |
| 433 | while(low < high) { |
| 434 | mid = (low + high) / 2; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 435 | tmp = (struct btrfs_disk_key *)(p + mid * item_size); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 436 | ret = comp_keys(tmp, key); |
| 437 | |
| 438 | if (ret < 0) |
| 439 | low = mid + 1; |
| 440 | else if (ret > 0) |
| 441 | high = mid; |
| 442 | else { |
| 443 | *slot = mid; |
| 444 | return 0; |
| 445 | } |
| 446 | } |
| 447 | *slot = low; |
| 448 | return 1; |
| 449 | } |
| 450 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 451 | /* |
| 452 | * simple bin_search frontend that does the right thing for |
| 453 | * leaves vs nodes |
| 454 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 455 | static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 456 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 457 | if (btrfs_is_leaf(c)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 458 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 459 | return generic_bin_search((void *)l->items, |
| 460 | sizeof(struct btrfs_item), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 461 | key, btrfs_header_nritems(&c->header), |
| 462 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 463 | } else { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 464 | return generic_bin_search((void *)c->ptrs, |
| 465 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 466 | key, btrfs_header_nritems(&c->header), |
| 467 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 468 | } |
| 469 | return -1; |
| 470 | } |
| 471 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 472 | static struct buffer_head *read_node_slot(struct btrfs_root *root, |
| 473 | struct buffer_head *parent_buf, |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 474 | int slot) |
| 475 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 476 | struct btrfs_node *node = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 477 | if (slot < 0) |
| 478 | return NULL; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 479 | if (slot >= btrfs_header_nritems(&node->header)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 480 | return NULL; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 481 | return read_tree_block(root, btrfs_node_blockptr(node, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 484 | static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root |
| 485 | *root, struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 486 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 487 | struct buffer_head *right_buf; |
| 488 | struct buffer_head *mid_buf; |
| 489 | struct buffer_head *left_buf; |
| 490 | struct buffer_head *parent_buf = NULL; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 491 | struct btrfs_node *right = NULL; |
| 492 | struct btrfs_node *mid; |
| 493 | struct btrfs_node *left = NULL; |
| 494 | struct btrfs_node *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 495 | int ret = 0; |
| 496 | int wret; |
| 497 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 498 | int orig_slot = path->slots[level]; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 499 | int err_on_enospc = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 500 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 501 | |
| 502 | if (level == 0) |
| 503 | return 0; |
| 504 | |
| 505 | mid_buf = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 506 | mid = btrfs_buffer_node(mid_buf); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 507 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 508 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 509 | if (level < BTRFS_MAX_LEVEL - 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 510 | parent_buf = path->nodes[level + 1]; |
| 511 | pslot = path->slots[level + 1]; |
| 512 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 513 | /* |
| 514 | * deal with the case where there is only one pointer in the root |
| 515 | * by promoting the node below to a root |
| 516 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 517 | if (!parent_buf) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 518 | struct buffer_head *child; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 519 | u64 blocknr = bh_blocknr(mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 520 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 521 | if (btrfs_header_nritems(&mid->header) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 522 | return 0; |
| 523 | |
| 524 | /* promote the child to a root */ |
| 525 | child = read_node_slot(root, mid_buf, 0); |
| 526 | BUG_ON(!child); |
| 527 | root->node = child; |
| 528 | path->nodes[level] = NULL; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 529 | clean_tree_block(trans, root, mid_buf); |
| 530 | wait_on_buffer(mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 531 | /* once for the path */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 532 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 533 | /* once for the root ptr */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 534 | btrfs_block_release(root, mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 535 | return btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 536 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 537 | parent = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 538 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 539 | if (btrfs_header_nritems(&mid->header) > |
| 540 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 541 | return 0; |
| 542 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 543 | if (btrfs_header_nritems(&mid->header) < 2) |
| 544 | err_on_enospc = 1; |
| 545 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 546 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 547 | if (left_buf) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 548 | wret = btrfs_cow_block(trans, root, left_buf, |
| 549 | parent_buf, pslot - 1, &left_buf); |
| 550 | if (wret) { |
| 551 | ret = wret; |
| 552 | goto enospc; |
| 553 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 554 | } |
| 555 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
| 556 | if (right_buf) { |
| 557 | wret = btrfs_cow_block(trans, root, right_buf, |
| 558 | parent_buf, pslot + 1, &right_buf); |
| 559 | if (wret) { |
| 560 | ret = wret; |
| 561 | goto enospc; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* first, try to make some room in the middle buffer */ |
| 566 | if (left_buf) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 567 | left = btrfs_buffer_node(left_buf); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 568 | orig_slot += btrfs_header_nritems(&left->header); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 569 | wret = push_node_left(trans, root, left_buf, mid_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 570 | if (wret < 0) |
| 571 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 572 | if (btrfs_header_nritems(&mid->header) < 2) |
| 573 | err_on_enospc = 1; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 574 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * then try to empty the right most buffer into the middle |
| 578 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 579 | if (right_buf) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 580 | right = btrfs_buffer_node(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 581 | wret = push_node_left(trans, root, mid_buf, right_buf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 582 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 583 | ret = wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 584 | if (btrfs_header_nritems(&right->header) == 0) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 585 | u64 blocknr = bh_blocknr(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 586 | clean_tree_block(trans, root, right_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 587 | wait_on_buffer(right_buf); |
| 588 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 589 | right_buf = NULL; |
| 590 | right = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 591 | wret = del_ptr(trans, root, path, level + 1, pslot + |
| 592 | 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 593 | if (wret) |
| 594 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 595 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 596 | if (wret) |
| 597 | ret = wret; |
| 598 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 599 | btrfs_memcpy(root, parent, |
| 600 | &parent->ptrs[pslot + 1].key, |
| 601 | &right->ptrs[0].key, |
| 602 | sizeof(struct btrfs_disk_key)); |
| 603 | btrfs_mark_buffer_dirty(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 604 | } |
| 605 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 606 | if (btrfs_header_nritems(&mid->header) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 607 | /* |
| 608 | * we're not allowed to leave a node with one item in the |
| 609 | * tree during a delete. A deletion from lower in the tree |
| 610 | * could try to delete the only pointer in this node. |
| 611 | * So, pull some keys from the left. |
| 612 | * There has to be a left pointer at this point because |
| 613 | * otherwise we would have pulled some pointers from the |
| 614 | * right |
| 615 | */ |
| 616 | BUG_ON(!left_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 617 | wret = balance_node_right(trans, root, mid_buf, left_buf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 618 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 619 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 620 | goto enospc; |
| 621 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 622 | BUG_ON(wret == 1); |
| 623 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 624 | if (btrfs_header_nritems(&mid->header) == 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 625 | /* we've managed to empty the middle node, drop it */ |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 626 | u64 blocknr = bh_blocknr(mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 627 | clean_tree_block(trans, root, mid_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 628 | wait_on_buffer(mid_buf); |
| 629 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 630 | mid_buf = NULL; |
| 631 | mid = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 632 | wret = del_ptr(trans, root, path, level + 1, pslot); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 633 | if (wret) |
| 634 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 635 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 636 | if (wret) |
| 637 | ret = wret; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 638 | } else { |
| 639 | /* update the parent key to reflect our changes */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 640 | btrfs_memcpy(root, parent, |
| 641 | &parent->ptrs[pslot].key, &mid->ptrs[0].key, |
| 642 | sizeof(struct btrfs_disk_key)); |
| 643 | btrfs_mark_buffer_dirty(parent_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 644 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 645 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 646 | /* update the path */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 647 | if (left_buf) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 648 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 649 | get_bh(left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 650 | path->nodes[level] = left_buf; |
| 651 | path->slots[level + 1] -= 1; |
| 652 | path->slots[level] = orig_slot; |
| 653 | if (mid_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 654 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 655 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 656 | orig_slot -= btrfs_header_nritems(&left->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 657 | path->slots[level] = orig_slot; |
| 658 | } |
| 659 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 660 | /* double check we haven't messed things up */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 661 | check_block(root, path, level); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 662 | if (orig_ptr != |
| 663 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]), |
| 664 | path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 665 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 666 | enospc: |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 667 | if (right_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 668 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 669 | if (left_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 670 | btrfs_block_release(root, left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 671 | return ret; |
| 672 | } |
| 673 | |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 674 | /* returns zero if the push worked, non-zero otherwise */ |
| 675 | static int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
| 676 | struct btrfs_root *root, |
| 677 | struct btrfs_path *path, int level) |
| 678 | { |
| 679 | struct buffer_head *right_buf; |
| 680 | struct buffer_head *mid_buf; |
| 681 | struct buffer_head *left_buf; |
| 682 | struct buffer_head *parent_buf = NULL; |
| 683 | struct btrfs_node *right = NULL; |
| 684 | struct btrfs_node *mid; |
| 685 | struct btrfs_node *left = NULL; |
| 686 | struct btrfs_node *parent = NULL; |
| 687 | int ret = 0; |
| 688 | int wret; |
| 689 | int pslot; |
| 690 | int orig_slot = path->slots[level]; |
| 691 | u64 orig_ptr; |
| 692 | |
| 693 | if (level == 0) |
| 694 | return 1; |
| 695 | |
| 696 | mid_buf = path->nodes[level]; |
| 697 | mid = btrfs_buffer_node(mid_buf); |
| 698 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
| 699 | |
| 700 | if (level < BTRFS_MAX_LEVEL - 1) |
| 701 | parent_buf = path->nodes[level + 1]; |
| 702 | pslot = path->slots[level + 1]; |
| 703 | |
| 704 | if (!parent_buf) |
| 705 | return 1; |
| 706 | parent = btrfs_buffer_node(parent_buf); |
| 707 | |
| 708 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 709 | |
| 710 | /* first, try to make some room in the middle buffer */ |
| 711 | if (left_buf) { |
| 712 | u32 left_nr; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 713 | left = btrfs_buffer_node(left_buf); |
| 714 | left_nr = btrfs_header_nritems(&left->header); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 715 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 716 | wret = 1; |
| 717 | } else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 718 | ret = btrfs_cow_block(trans, root, left_buf, parent_buf, |
| 719 | pslot - 1, &left_buf); |
| 720 | if (ret) |
| 721 | wret = 1; |
| 722 | else { |
| 723 | left = btrfs_buffer_node(left_buf); |
| 724 | wret = push_node_left(trans, root, |
| 725 | left_buf, mid_buf); |
| 726 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 727 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 728 | if (wret < 0) |
| 729 | ret = wret; |
| 730 | if (wret == 0) { |
| 731 | orig_slot += left_nr; |
| 732 | btrfs_memcpy(root, parent, |
| 733 | &parent->ptrs[pslot].key, |
| 734 | &mid->ptrs[0].key, |
| 735 | sizeof(struct btrfs_disk_key)); |
| 736 | btrfs_mark_buffer_dirty(parent_buf); |
| 737 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
| 738 | path->nodes[level] = left_buf; |
| 739 | path->slots[level + 1] -= 1; |
| 740 | path->slots[level] = orig_slot; |
| 741 | btrfs_block_release(root, mid_buf); |
| 742 | } else { |
| 743 | orig_slot -= |
| 744 | btrfs_header_nritems(&left->header); |
| 745 | path->slots[level] = orig_slot; |
| 746 | btrfs_block_release(root, left_buf); |
| 747 | } |
| 748 | check_node(root, path, level); |
| 749 | return 0; |
| 750 | } |
| 751 | btrfs_block_release(root, left_buf); |
| 752 | } |
| 753 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
| 754 | |
| 755 | /* |
| 756 | * then try to empty the right most buffer into the middle |
| 757 | */ |
| 758 | if (right_buf) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 759 | u32 right_nr; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 760 | right = btrfs_buffer_node(right_buf); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 761 | right_nr = btrfs_header_nritems(&right->header); |
| 762 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 763 | wret = 1; |
| 764 | } else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 765 | ret = btrfs_cow_block(trans, root, right_buf, |
| 766 | parent_buf, pslot + 1, |
| 767 | &right_buf); |
| 768 | if (ret) |
| 769 | wret = 1; |
| 770 | else { |
| 771 | right = btrfs_buffer_node(right_buf); |
| 772 | wret = balance_node_right(trans, root, |
| 773 | right_buf, mid_buf); |
| 774 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 775 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 776 | if (wret < 0) |
| 777 | ret = wret; |
| 778 | if (wret == 0) { |
| 779 | btrfs_memcpy(root, parent, |
| 780 | &parent->ptrs[pslot + 1].key, |
| 781 | &right->ptrs[0].key, |
| 782 | sizeof(struct btrfs_disk_key)); |
| 783 | btrfs_mark_buffer_dirty(parent_buf); |
| 784 | if (btrfs_header_nritems(&mid->header) <= orig_slot) { |
| 785 | path->nodes[level] = right_buf; |
| 786 | path->slots[level + 1] += 1; |
| 787 | path->slots[level] = orig_slot - |
| 788 | btrfs_header_nritems(&mid->header); |
| 789 | btrfs_block_release(root, mid_buf); |
| 790 | } else { |
| 791 | btrfs_block_release(root, right_buf); |
| 792 | } |
| 793 | check_node(root, path, level); |
| 794 | return 0; |
| 795 | } |
| 796 | btrfs_block_release(root, right_buf); |
| 797 | } |
| 798 | check_node(root, path, level); |
| 799 | return 1; |
| 800 | } |
| 801 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 802 | /* |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 803 | * readahead one full node of leaves |
| 804 | */ |
| 805 | static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 806 | int level, int slot) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 807 | { |
| 808 | struct btrfs_node *node; |
| 809 | int i; |
| 810 | u32 nritems; |
| 811 | u64 item_objectid; |
| 812 | u64 blocknr; |
| 813 | u64 search; |
| 814 | u64 cluster_start; |
| 815 | int ret; |
| 816 | int nread = 0; |
| 817 | int direction = path->reada; |
| 818 | struct radix_tree_root found; |
| 819 | unsigned long gang[8]; |
| 820 | struct buffer_head *bh; |
| 821 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 822 | if (level == 0) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 823 | return; |
| 824 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 825 | if (!path->nodes[level]) |
| 826 | return; |
| 827 | |
| 828 | node = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 829 | search = btrfs_node_blockptr(node, slot); |
| 830 | bh = btrfs_find_tree_block(root, search); |
| 831 | if (bh) { |
| 832 | brelse(bh); |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | init_bit_radix(&found); |
| 837 | nritems = btrfs_header_nritems(&node->header); |
| 838 | for (i = slot; i < nritems; i++) { |
| 839 | item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key); |
| 840 | blocknr = btrfs_node_blockptr(node, i); |
| 841 | set_radix_bit(&found, blocknr); |
| 842 | } |
| 843 | if (direction > 0) { |
| 844 | cluster_start = search - 4; |
| 845 | if (cluster_start > search) |
| 846 | cluster_start = 0; |
| 847 | } else |
| 848 | cluster_start = search + 4; |
| 849 | while(1) { |
| 850 | ret = find_first_radix_bit(&found, gang, 0, ARRAY_SIZE(gang)); |
| 851 | if (!ret) |
| 852 | break; |
| 853 | for (i = 0; i < ret; i++) { |
| 854 | blocknr = gang[i]; |
| 855 | clear_radix_bit(&found, blocknr); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 856 | if (path->reada == 1 && nread > 16) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 857 | continue; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 858 | if (close_blocks(cluster_start, blocknr)) { |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 859 | readahead_tree_block(root, blocknr); |
| 860 | nread++; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 861 | cluster_start = blocknr; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 867 | * look for key in the tree. path is filled in with nodes along the way |
| 868 | * if key is found, we return zero and you can find the item in the leaf |
| 869 | * level of the path (level 0) |
| 870 | * |
| 871 | * If the key isn't found, the path points to the slot where it should |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 872 | * be inserted, and 1 is returned. If there are other errors during the |
| 873 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 874 | * |
| 875 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 876 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 877 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 878 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 879 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 880 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 881 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 882 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 883 | struct buffer_head *b; |
| 884 | struct buffer_head *cow_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 885 | struct btrfs_node *c; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 886 | u64 blocknr; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 887 | int slot; |
| 888 | int ret; |
| 889 | int level; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 890 | int should_reada = p->reada; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 891 | u8 lowest_level = 0; |
| 892 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 893 | lowest_level = p->lowest_level; |
| 894 | WARN_ON(lowest_level && ins_len); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 895 | WARN_ON(p->nodes[0] != NULL); |
| 896 | WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 897 | again: |
| 898 | b = root->node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 899 | get_bh(b); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 900 | while (b) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 901 | c = btrfs_buffer_node(b); |
| 902 | level = btrfs_header_level(&c->header); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 903 | if (cow) { |
| 904 | int wret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 905 | wret = btrfs_cow_block(trans, root, b, |
| 906 | p->nodes[level + 1], |
| 907 | p->slots[level + 1], |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 908 | &cow_buf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 909 | if (wret) { |
| 910 | btrfs_block_release(root, cow_buf); |
| 911 | return wret; |
| 912 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 913 | b = cow_buf; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 914 | c = btrfs_buffer_node(b); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 915 | } |
| 916 | BUG_ON(!cow && ins_len); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 917 | if (level != btrfs_header_level(&c->header)) |
| 918 | WARN_ON(1); |
| 919 | level = btrfs_header_level(&c->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 920 | p->nodes[level] = b; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 921 | ret = check_block(root, p, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 922 | if (ret) |
| 923 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 924 | ret = bin_search(c, key, &slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 925 | if (!btrfs_is_leaf(c)) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 926 | if (ret && slot > 0) |
| 927 | slot -= 1; |
| 928 | p->slots[level] = slot; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 929 | if (ins_len > 0 && btrfs_header_nritems(&c->header) >= |
| 930 | BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 931 | int sret = split_node(trans, root, p, level); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 932 | BUG_ON(sret > 0); |
| 933 | if (sret) |
| 934 | return sret; |
| 935 | b = p->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 936 | c = btrfs_buffer_node(b); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 937 | slot = p->slots[level]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 938 | } else if (ins_len < 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 939 | int sret = balance_level(trans, root, p, |
| 940 | level); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 941 | if (sret) |
| 942 | return sret; |
| 943 | b = p->nodes[level]; |
| 944 | if (!b) |
| 945 | goto again; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 946 | c = btrfs_buffer_node(b); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 947 | slot = p->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 948 | BUG_ON(btrfs_header_nritems(&c->header) == 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 949 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 950 | /* this is only true while dropping a snapshot */ |
| 951 | if (level == lowest_level) |
| 952 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 953 | blocknr = btrfs_node_blockptr(c, slot); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 954 | if (should_reada) |
| 955 | reada_for_search(root, p, level, slot); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 956 | b = read_tree_block(root, btrfs_node_blockptr(c, slot)); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 957 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 958 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 959 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 960 | p->slots[level] = slot; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 961 | if (ins_len > 0 && btrfs_leaf_free_space(root, l) < |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 962 | sizeof(struct btrfs_item) + ins_len) { |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 963 | int sret = split_leaf(trans, root, key, |
| 964 | p, ins_len); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 965 | BUG_ON(sret > 0); |
| 966 | if (sret) |
| 967 | return sret; |
| 968 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 969 | return ret; |
| 970 | } |
| 971 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 972 | return 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 973 | } |
| 974 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 975 | /* |
| 976 | * adjust the pointers going up the tree, starting at level |
| 977 | * making sure the right key of each node is points to 'key'. |
| 978 | * This is used after shifting pointers to the left, so it stops |
| 979 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 980 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 981 | * |
| 982 | * If this fails to write a tree block, it returns -1, but continues |
| 983 | * fixing up the blocks in ram so the tree is consistent. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 984 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 985 | static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root |
| 986 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 987 | *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 988 | { |
| 989 | int i; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 990 | int ret = 0; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 991 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 992 | struct btrfs_node *t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 993 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 994 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 995 | break; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 996 | t = btrfs_buffer_node(path->nodes[i]); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 997 | btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key)); |
| 998 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 999 | if (tslot != 0) |
| 1000 | break; |
| 1001 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1002 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1005 | /* |
| 1006 | * try to push data from one node into the next node left in the |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1007 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1008 | * |
| 1009 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 1010 | * error, and > 0 if there was no room in the left hand block. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1011 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1012 | static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1013 | *root, struct buffer_head *dst_buf, struct |
| 1014 | buffer_head *src_buf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1015 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1016 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 1017 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1018 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1019 | int src_nritems; |
| 1020 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1021 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1022 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1023 | src_nritems = btrfs_header_nritems(&src->header); |
| 1024 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1025 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1026 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1027 | if (push_items <= 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1028 | return 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1029 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1030 | |
| 1031 | if (src_nritems < push_items) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1032 | push_items = src_nritems; |
| 1033 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1034 | btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs, |
| 1035 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1036 | if (push_items < src_nritems) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1037 | btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1038 | (src_nritems - push_items) * |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1039 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1040 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1041 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 1042 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1043 | btrfs_mark_buffer_dirty(src_buf); |
| 1044 | btrfs_mark_buffer_dirty(dst_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1045 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1046 | } |
| 1047 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1048 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1049 | * try to push data from one node into the next node right in the |
| 1050 | * tree. |
| 1051 | * |
| 1052 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 1053 | * error, and > 0 if there was no room in the right hand block. |
| 1054 | * |
| 1055 | * this will only push up to 1/2 the contents of the left node over |
| 1056 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1057 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1058 | btrfs_root *root, struct buffer_head *dst_buf, |
| 1059 | struct buffer_head *src_buf) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1060 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1061 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 1062 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1063 | int push_items = 0; |
| 1064 | int max_push; |
| 1065 | int src_nritems; |
| 1066 | int dst_nritems; |
| 1067 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1068 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1069 | src_nritems = btrfs_header_nritems(&src->header); |
| 1070 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1071 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1072 | if (push_items <= 0) { |
| 1073 | return 1; |
| 1074 | } |
| 1075 | |
| 1076 | max_push = src_nritems / 2 + 1; |
| 1077 | /* don't try to empty the node */ |
| 1078 | if (max_push > src_nritems) |
| 1079 | return 1; |
| 1080 | if (max_push < push_items) |
| 1081 | push_items = max_push; |
| 1082 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1083 | btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs, |
| 1084 | dst_nritems * sizeof(struct btrfs_key_ptr)); |
| 1085 | |
| 1086 | btrfs_memcpy(root, dst, dst->ptrs, |
| 1087 | src->ptrs + src_nritems - push_items, |
| 1088 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1089 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1090 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 1091 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1092 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1093 | btrfs_mark_buffer_dirty(src_buf); |
| 1094 | btrfs_mark_buffer_dirty(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1095 | return ret; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1099 | * helper function to insert a new root level in the tree. |
| 1100 | * A new node is allocated, and a single item is inserted to |
| 1101 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1102 | * |
| 1103 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1104 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1105 | static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1106 | *root, struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1107 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1108 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1109 | struct btrfs_node *lower; |
| 1110 | struct btrfs_node *c; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1111 | struct btrfs_disk_key *lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1112 | |
| 1113 | BUG_ON(path->nodes[level]); |
| 1114 | BUG_ON(path->nodes[level-1] != root->node); |
| 1115 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1116 | t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1117 | if (IS_ERR(t)) |
| 1118 | return PTR_ERR(t); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1119 | c = btrfs_buffer_node(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1120 | memset(c, 0, root->blocksize); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1121 | btrfs_set_header_nritems(&c->header, 1); |
| 1122 | btrfs_set_header_level(&c->header, level); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1123 | btrfs_set_header_blocknr(&c->header, bh_blocknr(t)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 1124 | btrfs_set_header_generation(&c->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1125 | btrfs_set_header_owner(&c->header, root->root_key.objectid); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1126 | lower = btrfs_buffer_node(path->nodes[level-1]); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1127 | memcpy(c->header.fsid, root->fs_info->disk_super->fsid, |
| 1128 | sizeof(c->header.fsid)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1129 | if (btrfs_is_leaf(lower)) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1130 | lower_key = &((struct btrfs_leaf *)lower)->items[0].key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1131 | else |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1132 | lower_key = &lower->ptrs[0].key; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1133 | btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key, |
| 1134 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1135 | btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1])); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1136 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1137 | btrfs_mark_buffer_dirty(t); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1138 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1139 | /* the super has an extra ref to root->node */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1140 | btrfs_block_release(root, root->node); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1141 | root->node = t; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1142 | get_bh(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1143 | path->nodes[level] = t; |
| 1144 | path->slots[level] = 0; |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1148 | /* |
| 1149 | * worker function to insert a single pointer in a node. |
| 1150 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1151 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1152 | * slot and level indicate where you want the key to go, and |
| 1153 | * blocknr is the block the key points to. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1154 | * |
| 1155 | * returns zero on success and < 0 on any error |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1156 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1157 | static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1158 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 1159 | *key, u64 blocknr, int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1160 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1161 | struct btrfs_node *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1162 | int nritems; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1163 | |
| 1164 | BUG_ON(!path->nodes[level]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1165 | lower = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1166 | nritems = btrfs_header_nritems(&lower->header); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1167 | if (slot > nritems) |
| 1168 | BUG(); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1169 | if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1170 | BUG(); |
| 1171 | if (slot != nritems) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1172 | btrfs_memmove(root, lower, lower->ptrs + slot + 1, |
| 1173 | lower->ptrs + slot, |
| 1174 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1175 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1176 | btrfs_memcpy(root, lower, &lower->ptrs[slot].key, |
| 1177 | key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1178 | btrfs_set_node_blockptr(lower, slot, blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1179 | btrfs_set_header_nritems(&lower->header, nritems + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1180 | btrfs_mark_buffer_dirty(path->nodes[level]); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1181 | check_node(root, path, level); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1182 | return 0; |
| 1183 | } |
| 1184 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1185 | /* |
| 1186 | * split the node at the specified level in path in two. |
| 1187 | * The path is corrected to point to the appropriate node after the split |
| 1188 | * |
| 1189 | * Before splitting this tries to make some room in the node by pushing |
| 1190 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1191 | * |
| 1192 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1193 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1194 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1195 | *root, struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1196 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1197 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1198 | struct btrfs_node *c; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1199 | struct buffer_head *split_buffer; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1200 | struct btrfs_node *split; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1201 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1202 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1203 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1204 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1205 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1206 | t = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1207 | c = btrfs_buffer_node(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1208 | if (t == root->node) { |
| 1209 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1210 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1211 | if (ret) |
| 1212 | return ret; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1213 | } else { |
| 1214 | ret = push_nodes_for_insert(trans, root, path, level); |
| 1215 | t = path->nodes[level]; |
| 1216 | c = btrfs_buffer_node(t); |
| 1217 | if (!ret && |
| 1218 | btrfs_header_nritems(&c->header) < |
| 1219 | BTRFS_NODEPTRS_PER_BLOCK(root) - 1) |
| 1220 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1221 | if (ret < 0) |
| 1222 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1223 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1224 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1225 | c_nritems = btrfs_header_nritems(&c->header); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1226 | split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1227 | if (IS_ERR(split_buffer)) |
| 1228 | return PTR_ERR(split_buffer); |
| 1229 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1230 | split = btrfs_buffer_node(split_buffer); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1231 | btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); |
Chris Mason | 9a6f11e | 2007-03-27 09:06:38 -0400 | [diff] [blame] | 1232 | btrfs_set_header_level(&split->header, btrfs_header_level(&c->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1233 | btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 1234 | btrfs_set_header_generation(&split->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1235 | btrfs_set_header_owner(&split->header, root->root_key.objectid); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1236 | memcpy(split->header.fsid, root->fs_info->disk_super->fsid, |
| 1237 | sizeof(split->header.fsid)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1238 | mid = (c_nritems + 1) / 2; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1239 | btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid, |
| 1240 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1241 | btrfs_set_header_nritems(&split->header, c_nritems - mid); |
| 1242 | btrfs_set_header_nritems(&c->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1243 | ret = 0; |
| 1244 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1245 | btrfs_mark_buffer_dirty(t); |
| 1246 | btrfs_mark_buffer_dirty(split_buffer); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1247 | wret = insert_ptr(trans, root, path, &split->ptrs[0].key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1248 | bh_blocknr(split_buffer), path->slots[level + 1] + 1, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1249 | level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1250 | if (wret) |
| 1251 | ret = wret; |
| 1252 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1253 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1254 | path->slots[level] -= mid; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1255 | btrfs_block_release(root, t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1256 | path->nodes[level] = split_buffer; |
| 1257 | path->slots[level + 1] += 1; |
| 1258 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1259 | btrfs_block_release(root, split_buffer); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1260 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1261 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1262 | } |
| 1263 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1264 | /* |
| 1265 | * how many bytes are required to store the items in a leaf. start |
| 1266 | * and nr indicate which items in the leaf to check. This totals up the |
| 1267 | * space used both by the item structs and the item data |
| 1268 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1269 | static int leaf_space_used(struct btrfs_leaf *l, int start, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1270 | { |
| 1271 | int data_len; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1272 | int nritems = btrfs_header_nritems(&l->header); |
| 1273 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1274 | |
| 1275 | if (!nr) |
| 1276 | return 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1277 | data_len = btrfs_item_end(l->items + start); |
| 1278 | data_len = data_len - btrfs_item_offset(l->items + end); |
| 1279 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1280 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1281 | return data_len; |
| 1282 | } |
| 1283 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1284 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1285 | * The space between the end of the leaf items and |
| 1286 | * the start of the leaf data. IOW, how much room |
| 1287 | * the leaf has left for both items and data |
| 1288 | */ |
| 1289 | int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) |
| 1290 | { |
| 1291 | int nritems = btrfs_header_nritems(&leaf->header); |
| 1292 | return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 1293 | } |
| 1294 | |
| 1295 | /* |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1296 | * push some data in the path leaf to the right, trying to free up at |
| 1297 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1298 | * |
| 1299 | * returns 1 if the push failed because the other node didn't have enough |
| 1300 | * room, 0 if everything worked out and < 0 if there were major errors. |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1301 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1302 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1303 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1304 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1305 | struct buffer_head *left_buf = path->nodes[0]; |
| 1306 | struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1307 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1308 | struct buffer_head *right_buf; |
| 1309 | struct buffer_head *upper; |
| 1310 | struct btrfs_node *upper_node; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1311 | int slot; |
| 1312 | int i; |
| 1313 | int free_space; |
| 1314 | int push_space = 0; |
| 1315 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1316 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1317 | u32 left_nritems; |
| 1318 | u32 right_nritems; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1319 | int ret; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1320 | |
| 1321 | slot = path->slots[1]; |
| 1322 | if (!path->nodes[1]) { |
| 1323 | return 1; |
| 1324 | } |
| 1325 | upper = path->nodes[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1326 | upper_node = btrfs_buffer_node(upper); |
| 1327 | if (slot >= btrfs_header_nritems(&upper_node->header) - 1) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1328 | return 1; |
| 1329 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1330 | right_buf = read_tree_block(root, |
| 1331 | btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1)); |
| 1332 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1333 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1334 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1335 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1336 | return 1; |
| 1337 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1338 | /* cow and double check */ |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1339 | ret = btrfs_cow_block(trans, root, right_buf, upper, |
| 1340 | slot + 1, &right_buf); |
| 1341 | if (ret) { |
| 1342 | btrfs_block_release(root, right_buf); |
| 1343 | return 1; |
| 1344 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1345 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1346 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1347 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1348 | btrfs_block_release(root, right_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1349 | return 1; |
| 1350 | } |
| 1351 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1352 | left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1353 | if (left_nritems == 0) { |
| 1354 | btrfs_block_release(root, right_buf); |
| 1355 | return 1; |
| 1356 | } |
| 1357 | for (i = left_nritems - 1; i >= 1; i--) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1358 | item = left->items + i; |
| 1359 | if (path->slots[0] == i) |
| 1360 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1361 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 1362 | free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1363 | break; |
| 1364 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1365 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1366 | } |
| 1367 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1368 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1369 | return 1; |
| 1370 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1371 | if (push_items == left_nritems) |
| 1372 | WARN_ON(1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1373 | right_nritems = btrfs_header_nritems(&right->header); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1374 | /* push left to right */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1375 | push_space = btrfs_item_end(left->items + left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1376 | push_space -= leaf_data_end(root, left); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1377 | /* make room in the right data area */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1378 | btrfs_memmove(root, right, btrfs_leaf_data(right) + |
| 1379 | leaf_data_end(root, right) - push_space, |
| 1380 | btrfs_leaf_data(right) + |
| 1381 | leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) - |
| 1382 | leaf_data_end(root, right)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1383 | /* copy from the left data area */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1384 | btrfs_memcpy(root, right, btrfs_leaf_data(right) + |
| 1385 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 1386 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 1387 | push_space); |
| 1388 | btrfs_memmove(root, right, right->items + push_items, right->items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1389 | right_nritems * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1390 | /* copy the items from left to right */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1391 | btrfs_memcpy(root, right, right->items, left->items + |
| 1392 | left_nritems - push_items, |
| 1393 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1394 | |
| 1395 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1396 | right_nritems += push_items; |
| 1397 | btrfs_set_header_nritems(&right->header, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1398 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1399 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1400 | btrfs_set_item_offset(right->items + i, push_space - |
| 1401 | btrfs_item_size(right->items + i)); |
| 1402 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1403 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1404 | left_nritems -= push_items; |
| 1405 | btrfs_set_header_nritems(&left->header, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1406 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1407 | btrfs_mark_buffer_dirty(left_buf); |
| 1408 | btrfs_mark_buffer_dirty(right_buf); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1409 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1410 | btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1411 | &right->items[0].key, sizeof(struct btrfs_disk_key)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1412 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1413 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1414 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1415 | if (path->slots[0] >= left_nritems) { |
| 1416 | path->slots[0] -= left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1417 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1418 | path->nodes[0] = right_buf; |
| 1419 | path->slots[1] += 1; |
| 1420 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1421 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1422 | } |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1423 | if (path->nodes[1]) |
| 1424 | check_node(root, path, 1); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1425 | return 0; |
| 1426 | } |
| 1427 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1428 | * push some data in the path leaf to the left, trying to free up at |
| 1429 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 1430 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1431 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1432 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1433 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1434 | struct buffer_head *right_buf = path->nodes[0]; |
| 1435 | struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf); |
| 1436 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1437 | struct btrfs_leaf *left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1438 | int slot; |
| 1439 | int i; |
| 1440 | int free_space; |
| 1441 | int push_space = 0; |
| 1442 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1443 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1444 | u32 old_left_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1445 | int ret = 0; |
| 1446 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1447 | |
| 1448 | slot = path->slots[1]; |
| 1449 | if (slot == 0) { |
| 1450 | return 1; |
| 1451 | } |
| 1452 | if (!path->nodes[1]) { |
| 1453 | return 1; |
| 1454 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1455 | t = read_tree_block(root, |
| 1456 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1)); |
| 1457 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1458 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1459 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1460 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1461 | return 1; |
| 1462 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1463 | |
| 1464 | /* cow and double check */ |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1465 | ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t); |
| 1466 | if (ret) { |
| 1467 | /* we hit -ENOSPC, but it isn't fatal here */ |
| 1468 | return 1; |
| 1469 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1470 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1471 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1472 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1473 | btrfs_block_release(root, t); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1474 | return 1; |
| 1475 | } |
| 1476 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1477 | if (btrfs_header_nritems(&right->header) == 0) { |
| 1478 | btrfs_block_release(root, t); |
| 1479 | return 1; |
| 1480 | } |
| 1481 | |
| 1482 | for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1483 | item = right->items + i; |
| 1484 | if (path->slots[0] == i) |
| 1485 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1486 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 1487 | free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1488 | break; |
| 1489 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1490 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1491 | } |
| 1492 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1493 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1494 | return 1; |
| 1495 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1496 | if (push_items == btrfs_header_nritems(&right->header)) |
| 1497 | WARN_ON(1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1498 | /* push data from right to left */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1499 | btrfs_memcpy(root, left, left->items + |
| 1500 | btrfs_header_nritems(&left->header), |
| 1501 | right->items, push_items * sizeof(struct btrfs_item)); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1502 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1503 | btrfs_item_offset(right->items + push_items -1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1504 | btrfs_memcpy(root, left, btrfs_leaf_data(left) + |
| 1505 | leaf_data_end(root, left) - push_space, |
| 1506 | btrfs_leaf_data(right) + |
| 1507 | btrfs_item_offset(right->items + push_items - 1), |
| 1508 | push_space); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1509 | old_left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1510 | BUG_ON(old_left_nritems < 0); |
| 1511 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1512 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1513 | u32 ioff = btrfs_item_offset(left->items + i); |
| 1514 | btrfs_set_item_offset(left->items + i, ioff - |
| 1515 | (BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1516 | btrfs_item_offset(left->items + |
| 1517 | old_left_nritems - 1))); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1518 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1519 | btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1520 | |
| 1521 | /* fixup right node */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1522 | push_space = btrfs_item_offset(right->items + push_items - 1) - |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1523 | leaf_data_end(root, right); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1524 | btrfs_memmove(root, right, btrfs_leaf_data(right) + |
| 1525 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 1526 | btrfs_leaf_data(right) + |
| 1527 | leaf_data_end(root, right), push_space); |
| 1528 | btrfs_memmove(root, right, right->items, right->items + push_items, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1529 | (btrfs_header_nritems(&right->header) - push_items) * |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1530 | sizeof(struct btrfs_item)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1531 | btrfs_set_header_nritems(&right->header, |
| 1532 | btrfs_header_nritems(&right->header) - |
| 1533 | push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1534 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1535 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1536 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1537 | btrfs_set_item_offset(right->items + i, push_space - |
| 1538 | btrfs_item_size(right->items + i)); |
| 1539 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1540 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1541 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1542 | btrfs_mark_buffer_dirty(t); |
| 1543 | btrfs_mark_buffer_dirty(right_buf); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1544 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1545 | wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1546 | if (wret) |
| 1547 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1548 | |
| 1549 | /* then fixup the leaf pointer in the path */ |
| 1550 | if (path->slots[0] < push_items) { |
| 1551 | path->slots[0] += old_left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1552 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1553 | path->nodes[0] = t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1554 | path->slots[1] -= 1; |
| 1555 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1556 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1557 | path->slots[0] -= push_items; |
| 1558 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1559 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1560 | if (path->nodes[1]) |
| 1561 | check_node(root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1562 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1563 | } |
| 1564 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1565 | /* |
| 1566 | * split the path's leaf in two, making sure there is at least data_size |
| 1567 | * available for the resulting leaf level of the path. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1568 | * |
| 1569 | * returns 0 if all went well and < 0 on failure. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1570 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1571 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1572 | *root, struct btrfs_key *ins_key, |
| 1573 | struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1574 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1575 | struct buffer_head *l_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1576 | struct btrfs_leaf *l; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1577 | u32 nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1578 | int mid; |
| 1579 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1580 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1581 | struct buffer_head *right_buffer; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1582 | int space_needed = data_size + sizeof(struct btrfs_item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1583 | int data_copy_size; |
| 1584 | int rt_data_off; |
| 1585 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1586 | int ret = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1587 | int wret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1588 | int double_split = 0; |
| 1589 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1590 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1591 | /* first try to make some room by pushing left and right */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1592 | wret = push_leaf_left(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1593 | if (wret < 0) |
| 1594 | return wret; |
| 1595 | if (wret) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1596 | wret = push_leaf_right(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1597 | if (wret < 0) |
| 1598 | return wret; |
| 1599 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1600 | l_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1601 | l = btrfs_buffer_leaf(l_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1602 | |
| 1603 | /* did the pushes work? */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1604 | if (btrfs_leaf_free_space(root, l) >= |
| 1605 | sizeof(struct btrfs_item) + data_size) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1606 | return 0; |
| 1607 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1608 | if (!path->nodes[1]) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1609 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1610 | if (ret) |
| 1611 | return ret; |
| 1612 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1613 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1614 | nritems = btrfs_header_nritems(&l->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1615 | mid = (nritems + 1)/ 2; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1616 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1617 | right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1618 | if (IS_ERR(right_buffer)) |
| 1619 | return PTR_ERR(right_buffer); |
| 1620 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1621 | right = btrfs_buffer_leaf(right_buffer); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1622 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1623 | btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 1624 | btrfs_set_header_generation(&right->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1625 | btrfs_set_header_owner(&right->header, root->root_key.objectid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1626 | btrfs_set_header_level(&right->header, 0); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1627 | memcpy(right->header.fsid, root->fs_info->disk_super->fsid, |
| 1628 | sizeof(right->header.fsid)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1629 | if (mid <= slot) { |
| 1630 | if (nritems == 1 || |
| 1631 | leaf_space_used(l, mid, nritems - mid) + space_needed > |
| 1632 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 1633 | if (slot >= nritems) { |
| 1634 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1635 | btrfs_set_header_nritems(&right->header, 0); |
| 1636 | wret = insert_ptr(trans, root, path, |
| 1637 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1638 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1639 | path->slots[1] + 1, 1); |
| 1640 | if (wret) |
| 1641 | ret = wret; |
| 1642 | btrfs_block_release(root, path->nodes[0]); |
| 1643 | path->nodes[0] = right_buffer; |
| 1644 | path->slots[0] = 0; |
| 1645 | path->slots[1] += 1; |
| 1646 | return ret; |
| 1647 | } |
| 1648 | mid = slot; |
| 1649 | double_split = 1; |
| 1650 | } |
| 1651 | } else { |
| 1652 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
| 1653 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 1654 | if (slot == 0) { |
| 1655 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1656 | btrfs_set_header_nritems(&right->header, 0); |
| 1657 | wret = insert_ptr(trans, root, path, |
| 1658 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1659 | bh_blocknr(right_buffer), |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1660 | path->slots[1], 1); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1661 | if (wret) |
| 1662 | ret = wret; |
| 1663 | btrfs_block_release(root, path->nodes[0]); |
| 1664 | path->nodes[0] = right_buffer; |
| 1665 | path->slots[0] = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1666 | if (path->slots[1] == 0) { |
| 1667 | wret = fixup_low_keys(trans, root, |
| 1668 | path, &disk_key, 1); |
| 1669 | if (wret) |
| 1670 | ret = wret; |
| 1671 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1672 | return ret; |
| 1673 | } |
| 1674 | mid = slot; |
| 1675 | double_split = 1; |
| 1676 | } |
| 1677 | } |
| 1678 | btrfs_set_header_nritems(&right->header, nritems - mid); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1679 | data_copy_size = btrfs_item_end(l->items + mid) - |
| 1680 | leaf_data_end(root, l); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1681 | btrfs_memcpy(root, right, right->items, l->items + mid, |
| 1682 | (nritems - mid) * sizeof(struct btrfs_item)); |
| 1683 | btrfs_memcpy(root, right, |
| 1684 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 1685 | data_copy_size, btrfs_leaf_data(l) + |
| 1686 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1687 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 1688 | btrfs_item_end(l->items + mid); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1689 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1690 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1691 | u32 ioff = btrfs_item_offset(right->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1692 | btrfs_set_item_offset(right->items + i, ioff + rt_data_off); |
| 1693 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1694 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1695 | btrfs_set_header_nritems(&l->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1696 | ret = 0; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1697 | wret = insert_ptr(trans, root, path, &right->items[0].key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1698 | bh_blocknr(right_buffer), path->slots[1] + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1699 | if (wret) |
| 1700 | ret = wret; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1701 | btrfs_mark_buffer_dirty(right_buffer); |
| 1702 | btrfs_mark_buffer_dirty(l_buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1703 | BUG_ON(path->slots[0] != slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1704 | if (mid <= slot) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1705 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1706 | path->nodes[0] = right_buffer; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1707 | path->slots[0] -= mid; |
| 1708 | path->slots[1] += 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1709 | } else |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1710 | btrfs_block_release(root, right_buffer); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1711 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1712 | check_node(root, path, 1); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1713 | |
| 1714 | if (!double_split) |
| 1715 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1716 | right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1717 | if (IS_ERR(right_buffer)) |
| 1718 | return PTR_ERR(right_buffer); |
| 1719 | |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1720 | right = btrfs_buffer_leaf(right_buffer); |
| 1721 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1722 | btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1723 | btrfs_set_header_generation(&right->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1724 | btrfs_set_header_owner(&right->header, root->root_key.objectid); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1725 | btrfs_set_header_level(&right->header, 0); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1726 | memcpy(right->header.fsid, root->fs_info->disk_super->fsid, |
| 1727 | sizeof(right->header.fsid)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1728 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1729 | btrfs_set_header_nritems(&right->header, 0); |
| 1730 | wret = insert_ptr(trans, root, path, |
| 1731 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1732 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1733 | path->slots[1], 1); |
| 1734 | if (wret) |
| 1735 | ret = wret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1736 | if (path->slots[1] == 0) { |
| 1737 | wret = fixup_low_keys(trans, root, path, &disk_key, 1); |
| 1738 | if (wret) |
| 1739 | ret = wret; |
| 1740 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1741 | btrfs_block_release(root, path->nodes[0]); |
| 1742 | path->nodes[0] = right_buffer; |
| 1743 | path->slots[0] = 0; |
| 1744 | check_node(root, path, 1); |
| 1745 | check_leaf(root, path, 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1746 | return ret; |
| 1747 | } |
| 1748 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1749 | int btrfs_truncate_item(struct btrfs_trans_handle *trans, |
| 1750 | struct btrfs_root *root, |
| 1751 | struct btrfs_path *path, |
| 1752 | u32 new_size) |
| 1753 | { |
| 1754 | int ret = 0; |
| 1755 | int slot; |
| 1756 | int slot_orig; |
| 1757 | struct btrfs_leaf *leaf; |
| 1758 | struct buffer_head *leaf_buf; |
| 1759 | u32 nritems; |
| 1760 | unsigned int data_end; |
| 1761 | unsigned int old_data_start; |
| 1762 | unsigned int old_size; |
| 1763 | unsigned int size_diff; |
| 1764 | int i; |
| 1765 | |
| 1766 | slot_orig = path->slots[0]; |
| 1767 | leaf_buf = path->nodes[0]; |
| 1768 | leaf = btrfs_buffer_leaf(leaf_buf); |
| 1769 | |
| 1770 | nritems = btrfs_header_nritems(&leaf->header); |
| 1771 | data_end = leaf_data_end(root, leaf); |
| 1772 | |
| 1773 | slot = path->slots[0]; |
| 1774 | old_data_start = btrfs_item_offset(leaf->items + slot); |
| 1775 | old_size = btrfs_item_size(leaf->items + slot); |
| 1776 | BUG_ON(old_size <= new_size); |
| 1777 | size_diff = old_size - new_size; |
| 1778 | |
| 1779 | BUG_ON(slot < 0); |
| 1780 | BUG_ON(slot >= nritems); |
| 1781 | |
| 1782 | /* |
| 1783 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1784 | */ |
| 1785 | /* first correct the data pointers */ |
| 1786 | for (i = slot; i < nritems; i++) { |
| 1787 | u32 ioff = btrfs_item_offset(leaf->items + i); |
| 1788 | btrfs_set_item_offset(leaf->items + i, |
| 1789 | ioff + size_diff); |
| 1790 | } |
| 1791 | /* shift the data */ |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1792 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1793 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 1794 | data_end, old_data_start + new_size - data_end); |
| 1795 | btrfs_set_item_size(leaf->items + slot, new_size); |
| 1796 | btrfs_mark_buffer_dirty(leaf_buf); |
| 1797 | |
| 1798 | ret = 0; |
| 1799 | if (btrfs_leaf_free_space(root, leaf) < 0) |
| 1800 | BUG(); |
| 1801 | check_leaf(root, path, 0); |
| 1802 | return ret; |
| 1803 | } |
| 1804 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1805 | int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1806 | *root, struct btrfs_path *path, u32 data_size) |
| 1807 | { |
| 1808 | int ret = 0; |
| 1809 | int slot; |
| 1810 | int slot_orig; |
| 1811 | struct btrfs_leaf *leaf; |
| 1812 | struct buffer_head *leaf_buf; |
| 1813 | u32 nritems; |
| 1814 | unsigned int data_end; |
| 1815 | unsigned int old_data; |
| 1816 | unsigned int old_size; |
| 1817 | int i; |
| 1818 | |
| 1819 | slot_orig = path->slots[0]; |
| 1820 | leaf_buf = path->nodes[0]; |
| 1821 | leaf = btrfs_buffer_leaf(leaf_buf); |
| 1822 | |
| 1823 | nritems = btrfs_header_nritems(&leaf->header); |
| 1824 | data_end = leaf_data_end(root, leaf); |
| 1825 | |
| 1826 | if (btrfs_leaf_free_space(root, leaf) < data_size) |
| 1827 | BUG(); |
| 1828 | slot = path->slots[0]; |
| 1829 | old_data = btrfs_item_end(leaf->items + slot); |
| 1830 | |
| 1831 | BUG_ON(slot < 0); |
| 1832 | BUG_ON(slot >= nritems); |
| 1833 | |
| 1834 | /* |
| 1835 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1836 | */ |
| 1837 | /* first correct the data pointers */ |
| 1838 | for (i = slot; i < nritems; i++) { |
| 1839 | u32 ioff = btrfs_item_offset(leaf->items + i); |
| 1840 | btrfs_set_item_offset(leaf->items + i, |
| 1841 | ioff - data_size); |
| 1842 | } |
| 1843 | /* shift the data */ |
| 1844 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1845 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 1846 | data_end, old_data - data_end); |
| 1847 | data_end = old_data; |
| 1848 | old_size = btrfs_item_size(leaf->items + slot); |
| 1849 | btrfs_set_item_size(leaf->items + slot, old_size + data_size); |
| 1850 | btrfs_mark_buffer_dirty(leaf_buf); |
| 1851 | |
| 1852 | ret = 0; |
| 1853 | if (btrfs_leaf_free_space(root, leaf) < 0) |
| 1854 | BUG(); |
| 1855 | check_leaf(root, path, 0); |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1859 | /* |
| 1860 | * Given a key and some data, insert an item into the tree. |
| 1861 | * This does all the path init required, making room in the tree if needed. |
| 1862 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1863 | int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1864 | *root, struct btrfs_path *path, struct btrfs_key |
| 1865 | *cpu_key, u32 data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1866 | { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1867 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1868 | int slot; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1869 | int slot_orig; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1870 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1871 | struct buffer_head *leaf_buf; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1872 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1873 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1874 | struct btrfs_disk_key disk_key; |
| 1875 | |
| 1876 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1877 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1878 | /* create a root if there isn't one */ |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1879 | if (!root->node) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1880 | BUG(); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1881 | ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1882 | if (ret == 0) { |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1883 | return -EEXIST; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1884 | } |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1885 | if (ret < 0) |
| 1886 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1887 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1888 | slot_orig = path->slots[0]; |
| 1889 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1890 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1891 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1892 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1893 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1894 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1895 | if (btrfs_leaf_free_space(root, leaf) < |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1896 | sizeof(struct btrfs_item) + data_size) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1897 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1898 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1899 | slot = path->slots[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1900 | BUG_ON(slot < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1901 | if (slot != nritems) { |
| 1902 | int i; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1903 | unsigned int old_data = btrfs_item_end(leaf->items + slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1904 | |
| 1905 | /* |
| 1906 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1907 | */ |
| 1908 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1909 | for (i = slot; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1910 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1911 | btrfs_set_item_offset(leaf->items + i, |
| 1912 | ioff - data_size); |
| 1913 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1914 | |
| 1915 | /* shift the items */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1916 | btrfs_memmove(root, leaf, leaf->items + slot + 1, |
| 1917 | leaf->items + slot, |
| 1918 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1919 | |
| 1920 | /* shift the data */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1921 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1922 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 1923 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1924 | data_end = old_data; |
| 1925 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1926 | /* setup the item for the new data */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1927 | btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key, |
| 1928 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1929 | btrfs_set_item_offset(leaf->items + slot, data_end - data_size); |
| 1930 | btrfs_set_item_size(leaf->items + slot, data_size); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1931 | btrfs_set_header_nritems(&leaf->header, nritems + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1932 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1933 | |
| 1934 | ret = 0; |
Chris Mason | 8e19f2c | 2007-02-28 09:27:02 -0500 | [diff] [blame] | 1935 | if (slot == 0) |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1936 | ret = fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1937 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1938 | if (btrfs_leaf_free_space(root, leaf) < 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1939 | BUG(); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1940 | check_leaf(root, path, 0); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1941 | out: |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1942 | return ret; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Given a key and some data, insert an item into the tree. |
| 1947 | * This does all the path init required, making room in the tree if needed. |
| 1948 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1949 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1950 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 1951 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1952 | { |
| 1953 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1954 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1955 | u8 *ptr; |
| 1956 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1957 | path = btrfs_alloc_path(); |
| 1958 | BUG_ON(!path); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1959 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1960 | if (!ret) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1961 | ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1962 | path->slots[0], u8); |
| 1963 | btrfs_memcpy(root, path->nodes[0]->b_data, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1964 | ptr, data, data_size); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1965 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1966 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1967 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1968 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1969 | } |
| 1970 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1971 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1972 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1973 | * |
| 1974 | * If the delete empties a node, the node is removed from the tree, |
| 1975 | * continuing all the way the root if required. The root is converted into |
| 1976 | * a leaf if all the nodes are emptied. |
| 1977 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1978 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1979 | struct btrfs_path *path, int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1980 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1981 | struct btrfs_node *node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1982 | struct buffer_head *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1983 | u32 nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1984 | int ret = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1985 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1986 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1987 | node = btrfs_buffer_node(parent); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1988 | nritems = btrfs_header_nritems(&node->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1989 | if (slot != nritems -1) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1990 | btrfs_memmove(root, node, node->ptrs + slot, |
| 1991 | node->ptrs + slot + 1, |
| 1992 | sizeof(struct btrfs_key_ptr) * |
| 1993 | (nritems - slot - 1)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1994 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1995 | nritems--; |
| 1996 | btrfs_set_header_nritems(&node->header, nritems); |
| 1997 | if (nritems == 0 && parent == root->node) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1998 | struct btrfs_header *header = btrfs_buffer_header(root->node); |
| 1999 | BUG_ON(btrfs_header_level(header) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2000 | /* just turn the root into a leaf and break */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2001 | btrfs_set_header_level(header, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2002 | } else if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2003 | wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2004 | level + 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2005 | if (wret) |
| 2006 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2007 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2008 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2009 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2010 | } |
| 2011 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2012 | /* |
| 2013 | * delete the item at the leaf level in path. If that empties |
| 2014 | * the leaf, remove it from the tree |
| 2015 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2016 | int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2017 | struct btrfs_path *path) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2018 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2019 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2020 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2021 | struct buffer_head *leaf_buf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2022 | int doff; |
| 2023 | int dsize; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2024 | int ret = 0; |
| 2025 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2026 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2027 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2028 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2029 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 2030 | slot = path->slots[0]; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2031 | doff = btrfs_item_offset(leaf->items + slot); |
| 2032 | dsize = btrfs_item_size(leaf->items + slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2033 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2034 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2035 | if (slot != nritems - 1) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2036 | int i; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2037 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2038 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 2039 | data_end + dsize, |
| 2040 | btrfs_leaf_data(leaf) + data_end, |
| 2041 | doff - data_end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2042 | for (i = slot + 1; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2043 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2044 | btrfs_set_item_offset(leaf->items + i, ioff + dsize); |
| 2045 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2046 | btrfs_memmove(root, leaf, leaf->items + slot, |
| 2047 | leaf->items + slot + 1, |
| 2048 | sizeof(struct btrfs_item) * |
| 2049 | (nritems - slot - 1)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2050 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2051 | btrfs_set_header_nritems(&leaf->header, nritems - 1); |
| 2052 | nritems--; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2053 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2054 | if (nritems == 0) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2055 | if (leaf_buf == root->node) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2056 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 2057 | } else { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2058 | clean_tree_block(trans, root, leaf_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2059 | wait_on_buffer(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2060 | wret = del_ptr(trans, root, path, 1, path->slots[1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2061 | if (wret) |
| 2062 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2063 | wret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 2064 | bh_blocknr(leaf_buf), 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2065 | if (wret) |
| 2066 | ret = wret; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 2067 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2068 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2069 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2070 | if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2071 | wret = fixup_low_keys(trans, root, path, |
| 2072 | &leaf->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2073 | if (wret) |
| 2074 | ret = wret; |
| 2075 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2076 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2077 | /* delete the leaf if it is mostly empty */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2078 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2079 | /* push_leaf_left fixes the path. |
| 2080 | * make sure the path still points to our leaf |
| 2081 | * for possible call to del_ptr below |
| 2082 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 2083 | slot = path->slots[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2084 | get_bh(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2085 | wret = push_leaf_left(trans, root, path, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2086 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2087 | ret = wret; |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 2088 | if (path->nodes[0] == leaf_buf && |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2089 | btrfs_header_nritems(&leaf->header)) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2090 | wret = push_leaf_right(trans, root, path, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2091 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2092 | ret = wret; |
| 2093 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2094 | if (btrfs_header_nritems(&leaf->header) == 0) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 2095 | u64 blocknr = bh_blocknr(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2096 | clean_tree_block(trans, root, leaf_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2097 | wait_on_buffer(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2098 | wret = del_ptr(trans, root, path, 1, slot); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2099 | if (wret) |
| 2100 | ret = wret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2101 | btrfs_block_release(root, leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2102 | wret = btrfs_free_extent(trans, root, blocknr, |
| 2103 | 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2104 | if (wret) |
| 2105 | ret = wret; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 2106 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2107 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2108 | btrfs_block_release(root, leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2109 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2110 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2111 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2112 | } |
| 2113 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2114 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2115 | } |
| 2116 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2117 | /* |
| 2118 | * walk up the tree as far as required to find the next leaf. |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2119 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 2120 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2121 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2122 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2123 | { |
| 2124 | int slot; |
| 2125 | int level = 1; |
| 2126 | u64 blocknr; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2127 | struct buffer_head *c; |
| 2128 | struct btrfs_node *c_node; |
| 2129 | struct buffer_head *next = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2130 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2131 | while(level < BTRFS_MAX_LEVEL) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2132 | if (!path->nodes[level]) |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2133 | return 1; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2134 | slot = path->slots[level] + 1; |
| 2135 | c = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2136 | c_node = btrfs_buffer_node(c); |
| 2137 | if (slot >= btrfs_header_nritems(&c_node->header)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2138 | level++; |
| 2139 | continue; |
| 2140 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2141 | blocknr = btrfs_node_blockptr(c_node, slot); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 2142 | if (next) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2143 | btrfs_block_release(root, next); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2144 | if (path->reada) |
| 2145 | reada_for_search(root, path, level, slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2146 | next = read_tree_block(root, blocknr); |
| 2147 | break; |
| 2148 | } |
| 2149 | path->slots[level] = slot; |
| 2150 | while(1) { |
| 2151 | level--; |
| 2152 | c = path->nodes[level]; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2153 | btrfs_block_release(root, c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2154 | path->nodes[level] = next; |
| 2155 | path->slots[level] = 0; |
| 2156 | if (!level) |
| 2157 | break; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2158 | if (path->reada) |
Yan | 3202061 | 2007-08-27 15:17:54 -0400 | [diff] [blame] | 2159 | reada_for_search(root, path, level, 0); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 2160 | next = read_tree_block(root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2161 | btrfs_node_blockptr(btrfs_buffer_node(next), 0)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 2162 | } |
| 2163 | return 0; |
| 2164 | } |