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