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