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