Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2 | * Copyright (C) 2007,2008 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 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 | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 20 | #include "ctree.h" |
| 21 | #include "disk-io.h" |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 22 | #include "transaction.h" |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 23 | #include "print-tree.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 24 | #include "locking.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 25 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 26 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 27 | *root, struct btrfs_path *path, int level); |
| 28 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 29 | *root, struct btrfs_key *ins_key, |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 30 | struct btrfs_path *path, int data_size, int extend); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 31 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 32 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 33 | struct extent_buffer *src, int empty); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 34 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 35 | struct btrfs_root *root, |
| 36 | struct extent_buffer *dst_buf, |
| 37 | struct extent_buffer *src_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 38 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 39 | struct btrfs_path *path, int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 40 | |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 41 | inline void btrfs_init_path(struct btrfs_path *p) |
| 42 | { |
| 43 | memset(p, 0, sizeof(*p)); |
| 44 | } |
| 45 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 46 | struct btrfs_path *btrfs_alloc_path(void) |
| 47 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 48 | struct btrfs_path *path; |
| 49 | path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 50 | if (path) { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 51 | btrfs_init_path(path); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 52 | path->reada = 1; |
| 53 | } |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 54 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 57 | /* this also releases the path */ |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 58 | void btrfs_free_path(struct btrfs_path *p) |
| 59 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 60 | btrfs_release_path(NULL, p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 61 | kmem_cache_free(btrfs_path_cachep, p); |
| 62 | } |
| 63 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 64 | /* |
| 65 | * path release drops references on the extent buffers in the path |
| 66 | * and it drops any locks held by this path |
| 67 | * |
| 68 | * It is safe to call this on paths that no locks or extent buffers held. |
| 69 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 70 | void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 71 | { |
| 72 | int i; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 73 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 74 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 75 | p->slots[i] = 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 76 | if (!p->nodes[i]) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 77 | continue; |
| 78 | if (p->locks[i]) { |
| 79 | btrfs_tree_unlock(p->nodes[i]); |
| 80 | p->locks[i] = 0; |
| 81 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 82 | free_extent_buffer(p->nodes[i]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 83 | p->nodes[i] = NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 87 | /* |
| 88 | * safely gets a reference on the root node of a tree. A lock |
| 89 | * is not taken, so a concurrent writer may put a different node |
| 90 | * at the root of the tree. See btrfs_lock_root_node for the |
| 91 | * looping required. |
| 92 | * |
| 93 | * The extent buffer returned by this has a reference taken, so |
| 94 | * it won't disappear. It may stop being the root of the tree |
| 95 | * at any time because there are no locks held. |
| 96 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 97 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
| 98 | { |
| 99 | struct extent_buffer *eb; |
| 100 | spin_lock(&root->node_lock); |
| 101 | eb = root->node; |
| 102 | extent_buffer_get(eb); |
| 103 | spin_unlock(&root->node_lock); |
| 104 | return eb; |
| 105 | } |
| 106 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 107 | /* loop around taking references on and locking the root node of the |
| 108 | * tree until you end up with a lock on the root. A locked buffer |
| 109 | * is returned, with a reference held. |
| 110 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 111 | struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) |
| 112 | { |
| 113 | struct extent_buffer *eb; |
| 114 | |
| 115 | while(1) { |
| 116 | eb = btrfs_root_node(root); |
| 117 | btrfs_tree_lock(eb); |
| 118 | |
| 119 | spin_lock(&root->node_lock); |
| 120 | if (eb == root->node) { |
| 121 | spin_unlock(&root->node_lock); |
| 122 | break; |
| 123 | } |
| 124 | spin_unlock(&root->node_lock); |
| 125 | |
| 126 | btrfs_tree_unlock(eb); |
| 127 | free_extent_buffer(eb); |
| 128 | } |
| 129 | return eb; |
| 130 | } |
| 131 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 132 | /* cowonly root (everything not a reference counted cow subvolume), just get |
| 133 | * put onto a simple dirty list. transaction.c walks this to make sure they |
| 134 | * get properly updated on disk. |
| 135 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 136 | static void add_root_to_dirty_list(struct btrfs_root *root) |
| 137 | { |
| 138 | if (root->track_dirty && list_empty(&root->dirty_list)) { |
| 139 | list_add(&root->dirty_list, |
| 140 | &root->fs_info->dirty_cowonly_roots); |
| 141 | } |
| 142 | } |
| 143 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 144 | /* |
| 145 | * used by snapshot creation to make a copy of a root for a tree with |
| 146 | * a given objectid. The buffer with the new root node is returned in |
| 147 | * cow_ret, and this func returns zero on success or a negative error code. |
| 148 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 149 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
| 150 | struct btrfs_root *root, |
| 151 | struct extent_buffer *buf, |
| 152 | struct extent_buffer **cow_ret, u64 new_root_objectid) |
| 153 | { |
| 154 | struct extent_buffer *cow; |
| 155 | u32 nritems; |
| 156 | int ret = 0; |
| 157 | int level; |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 158 | struct btrfs_root *new_root; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 159 | |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 160 | new_root = kmalloc(sizeof(*new_root), GFP_NOFS); |
| 161 | if (!new_root) |
| 162 | return -ENOMEM; |
| 163 | |
| 164 | memcpy(new_root, root, sizeof(*new_root)); |
| 165 | new_root->root_key.objectid = new_root_objectid; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 166 | |
| 167 | WARN_ON(root->ref_cows && trans->transid != |
| 168 | root->fs_info->running_transaction->transid); |
| 169 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
| 170 | |
| 171 | level = btrfs_header_level(buf); |
| 172 | nritems = btrfs_header_nritems(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 173 | |
| 174 | cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0, |
| 175 | new_root_objectid, trans->transid, |
| 176 | level, buf->start, 0); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 177 | if (IS_ERR(cow)) { |
| 178 | kfree(new_root); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 179 | return PTR_ERR(cow); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 180 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 181 | |
| 182 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
| 183 | btrfs_set_header_bytenr(cow, cow->start); |
| 184 | btrfs_set_header_generation(cow, trans->transid); |
| 185 | btrfs_set_header_owner(cow, new_root_objectid); |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 186 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 187 | |
| 188 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 189 | ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 190 | kfree(new_root); |
| 191 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 192 | if (ret) |
| 193 | return ret; |
| 194 | |
| 195 | btrfs_mark_buffer_dirty(cow); |
| 196 | *cow_ret = cow; |
| 197 | return 0; |
| 198 | } |
| 199 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 200 | /* |
| 201 | * does the dirty work in cow of a single block. The parent block |
| 202 | * (if supplied) is updated to point to the new cow copy. The new |
| 203 | * buffer is marked dirty and returned locked. If you modify the block |
| 204 | * it needs to be marked dirty again. |
| 205 | * |
| 206 | * search_start -- an allocation hint for the new block |
| 207 | * |
| 208 | * empty_size -- a hint that you plan on doing more cow. This is the size in bytes |
| 209 | * the allocator should try to find free next to the block it returns. This is |
| 210 | * just a hint and may be ignored by the allocator. |
| 211 | * |
| 212 | * prealloc_dest -- if you have already reserved a destination for the cow, |
| 213 | * this uses that block instead of allocating a new one. btrfs_alloc_reserved_extent |
| 214 | * is used to finish the allocation. |
| 215 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 216 | int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 217 | struct btrfs_root *root, |
| 218 | struct extent_buffer *buf, |
| 219 | struct extent_buffer *parent, int parent_slot, |
| 220 | struct extent_buffer **cow_ret, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 221 | u64 search_start, u64 empty_size, |
| 222 | u64 prealloc_dest) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 223 | { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 224 | u64 parent_start; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 225 | struct extent_buffer *cow; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 226 | u32 nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 227 | int ret = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 228 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 229 | int unlock_orig = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 230 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 231 | if (*cow_ret == buf) |
| 232 | unlock_orig = 1; |
| 233 | |
| 234 | WARN_ON(!btrfs_tree_locked(buf)); |
| 235 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 236 | if (parent) |
| 237 | parent_start = parent->start; |
| 238 | else |
| 239 | parent_start = 0; |
| 240 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 241 | WARN_ON(root->ref_cows && trans->transid != |
| 242 | root->fs_info->running_transaction->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 243 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 244 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 245 | level = btrfs_header_level(buf); |
| 246 | nritems = btrfs_header_nritems(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 247 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 248 | if (prealloc_dest) { |
| 249 | struct btrfs_key ins; |
| 250 | |
| 251 | ins.objectid = prealloc_dest; |
| 252 | ins.offset = buf->len; |
| 253 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 254 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 255 | ret = btrfs_alloc_reserved_extent(trans, root, parent_start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 256 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 257 | trans->transid, level, &ins); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 258 | BUG_ON(ret); |
| 259 | cow = btrfs_init_new_buffer(trans, root, prealloc_dest, |
| 260 | buf->len); |
| 261 | } else { |
| 262 | cow = btrfs_alloc_free_block(trans, root, buf->len, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 263 | parent_start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 264 | root->root_key.objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 265 | trans->transid, level, |
| 266 | search_start, empty_size); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 267 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 268 | if (IS_ERR(cow)) |
| 269 | return PTR_ERR(cow); |
| 270 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 271 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 272 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 273 | btrfs_set_header_generation(cow, trans->transid); |
| 274 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 275 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 276 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 277 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
| 278 | if (btrfs_header_generation(buf) != trans->transid) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 279 | u32 nr_extents; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 280 | ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 281 | if (ret) |
| 282 | return ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 283 | |
| 284 | ret = btrfs_cache_ref(trans, root, buf, nr_extents); |
| 285 | WARN_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 286 | } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) { |
| 287 | /* |
| 288 | * There are only two places that can drop reference to |
| 289 | * tree blocks owned by living reloc trees, one is here, |
| 290 | * the other place is btrfs_merge_path. In both places, |
| 291 | * we check reference count while tree block is locked. |
| 292 | * Furthermore, if reference count is one, it won't get |
| 293 | * increased by someone else. |
| 294 | */ |
| 295 | u32 refs; |
| 296 | ret = btrfs_lookup_extent_ref(trans, root, buf->start, |
| 297 | buf->len, &refs); |
| 298 | BUG_ON(ret); |
| 299 | if (refs == 1) { |
| 300 | ret = btrfs_update_ref(trans, root, buf, cow, |
| 301 | 0, nritems); |
| 302 | clean_tree_block(trans, root, buf); |
| 303 | } else { |
| 304 | ret = btrfs_inc_ref(trans, root, buf, cow, NULL); |
| 305 | } |
| 306 | BUG_ON(ret); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 307 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 308 | ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems); |
| 309 | if (ret) |
| 310 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 311 | clean_tree_block(trans, root, buf); |
| 312 | } |
| 313 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 314 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 315 | ret = btrfs_add_reloc_mapping(root, buf->start, |
| 316 | buf->len, cow->start); |
| 317 | BUG_ON(ret); |
| 318 | ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start); |
| 319 | WARN_ON(ret); |
| 320 | } |
| 321 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 322 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 323 | WARN_ON(parent && parent != buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 324 | |
| 325 | spin_lock(&root->node_lock); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 326 | root->node = cow; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 327 | extent_buffer_get(cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 328 | spin_unlock(&root->node_lock); |
| 329 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 330 | if (buf != root->commit_root) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 331 | btrfs_free_extent(trans, root, buf->start, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 332 | buf->len, buf->start, |
| 333 | root->root_key.objectid, |
| 334 | btrfs_header_generation(buf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 335 | level, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 336 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 337 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 338 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 339 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 340 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 341 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 342 | WARN_ON(trans->transid == 0); |
| 343 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 344 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 345 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 346 | WARN_ON(btrfs_header_generation(parent) != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 347 | btrfs_free_extent(trans, root, buf->start, buf->len, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 348 | parent_start, btrfs_header_owner(parent), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 349 | btrfs_header_generation(parent), level, 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 350 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 351 | if (unlock_orig) |
| 352 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 353 | free_extent_buffer(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 354 | btrfs_mark_buffer_dirty(cow); |
| 355 | *cow_ret = cow; |
| 356 | return 0; |
| 357 | } |
| 358 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 359 | /* |
| 360 | * cows a single block, see __btrfs_cow_block for the real work. |
| 361 | * This version of it has extra checks so that a block isn't cow'd more than |
| 362 | * once per transaction, as long as it hasn't been written yet |
| 363 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 364 | int noinline btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 365 | struct btrfs_root *root, struct extent_buffer *buf, |
| 366 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 367 | struct extent_buffer **cow_ret, u64 prealloc_dest) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 368 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 369 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 370 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 371 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 372 | if (trans->transaction != root->fs_info->running_transaction) { |
| 373 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 374 | root->fs_info->running_transaction->transid); |
| 375 | WARN_ON(1); |
| 376 | } |
| 377 | if (trans->transid != root->fs_info->generation) { |
| 378 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 379 | root->fs_info->generation); |
| 380 | WARN_ON(1); |
| 381 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 382 | |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 383 | spin_lock(&root->fs_info->hash_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 384 | if (btrfs_header_generation(buf) == trans->transid && |
| 385 | btrfs_header_owner(buf) == root->root_key.objectid && |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 386 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 387 | *cow_ret = buf; |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 388 | spin_unlock(&root->fs_info->hash_lock); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 389 | WARN_ON(prealloc_dest); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 390 | return 0; |
| 391 | } |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 392 | spin_unlock(&root->fs_info->hash_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 393 | search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 394 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 395 | parent_slot, cow_ret, search_start, 0, |
| 396 | prealloc_dest); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 397 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 400 | /* |
| 401 | * helper function for defrag to decide if two blocks pointed to by a |
| 402 | * node are actually close by |
| 403 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 404 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 405 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 406 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 407 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 408 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 409 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 413 | /* |
| 414 | * compare two keys in a memcmp fashion |
| 415 | */ |
| 416 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
| 417 | { |
| 418 | struct btrfs_key k1; |
| 419 | |
| 420 | btrfs_disk_key_to_cpu(&k1, disk); |
| 421 | |
| 422 | if (k1.objectid > k2->objectid) |
| 423 | return 1; |
| 424 | if (k1.objectid < k2->objectid) |
| 425 | return -1; |
| 426 | if (k1.type > k2->type) |
| 427 | return 1; |
| 428 | if (k1.type < k2->type) |
| 429 | return -1; |
| 430 | if (k1.offset > k2->offset) |
| 431 | return 1; |
| 432 | if (k1.offset < k2->offset) |
| 433 | return -1; |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 438 | /* |
| 439 | * this is used by the defrag code to go through all the |
| 440 | * leaves pointed to by a node and reallocate them so that |
| 441 | * disk order is close to key order |
| 442 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 443 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 444 | struct btrfs_root *root, struct extent_buffer *parent, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 445 | int start_slot, int cache_only, u64 *last_ret, |
| 446 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 447 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 448 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 449 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 450 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 451 | u64 search_start = *last_ret; |
| 452 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 453 | u64 other; |
| 454 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 455 | int end_slot; |
| 456 | int i; |
| 457 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 458 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 459 | int uptodate; |
| 460 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 461 | int progress_passed = 0; |
| 462 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 463 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 464 | parent_level = btrfs_header_level(parent); |
| 465 | if (cache_only && parent_level != 1) |
| 466 | return 0; |
| 467 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 468 | if (trans->transaction != root->fs_info->running_transaction) { |
| 469 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 470 | root->fs_info->running_transaction->transid); |
| 471 | WARN_ON(1); |
| 472 | } |
| 473 | if (trans->transid != root->fs_info->generation) { |
| 474 | printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, |
| 475 | root->fs_info->generation); |
| 476 | WARN_ON(1); |
| 477 | } |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 478 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 479 | parent_nritems = btrfs_header_nritems(parent); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 480 | blocksize = btrfs_level_size(root, parent_level - 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 481 | end_slot = parent_nritems; |
| 482 | |
| 483 | if (parent_nritems == 1) |
| 484 | return 0; |
| 485 | |
| 486 | for (i = start_slot; i < end_slot; i++) { |
| 487 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 488 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 489 | if (!parent->map_token) { |
| 490 | map_extent_buffer(parent, |
| 491 | btrfs_node_key_ptr_offset(i), |
| 492 | sizeof(struct btrfs_key_ptr), |
| 493 | &parent->map_token, &parent->kaddr, |
| 494 | &parent->map_start, &parent->map_len, |
| 495 | KM_USER1); |
| 496 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 497 | btrfs_node_key(parent, &disk_key, i); |
| 498 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 499 | continue; |
| 500 | |
| 501 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 502 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 503 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 504 | if (last_block == 0) |
| 505 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 506 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 507 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 508 | other = btrfs_node_blockptr(parent, i - 1); |
| 509 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 510 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 511 | if (!close && i < end_slot - 2) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 512 | other = btrfs_node_blockptr(parent, i + 1); |
| 513 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 514 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 515 | if (close) { |
| 516 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 517 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 518 | } |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 519 | if (parent->map_token) { |
| 520 | unmap_extent_buffer(parent, parent->map_token, |
| 521 | KM_USER1); |
| 522 | parent->map_token = NULL; |
| 523 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 524 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 525 | cur = btrfs_find_tree_block(root, blocknr, blocksize); |
| 526 | if (cur) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 527 | uptodate = btrfs_buffer_uptodate(cur, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 528 | else |
| 529 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 530 | if (!cur || !uptodate) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 531 | if (cache_only) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 532 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 533 | continue; |
| 534 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 535 | if (!cur) { |
| 536 | cur = read_tree_block(root, blocknr, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 537 | blocksize, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 538 | } else if (!uptodate) { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 539 | btrfs_read_buffer(cur, gen); |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 540 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 541 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 542 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 543 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 544 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 545 | btrfs_tree_lock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 546 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 547 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 548 | min(16 * blocksize, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 549 | (end_slot - i) * blocksize), 0); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 550 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 551 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 552 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 553 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 554 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 555 | search_start = cur->start; |
| 556 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 557 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 558 | btrfs_tree_unlock(cur); |
| 559 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 560 | } |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 561 | if (parent->map_token) { |
| 562 | unmap_extent_buffer(parent, parent->map_token, |
| 563 | KM_USER1); |
| 564 | parent->map_token = NULL; |
| 565 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 566 | return err; |
| 567 | } |
| 568 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 569 | /* |
| 570 | * The leaf data grows from end-to-front in the node. |
| 571 | * this returns the address of the start of the last item, |
| 572 | * which is the stop of the leaf data stack |
| 573 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 574 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 575 | struct extent_buffer *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 576 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 577 | u32 nr = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 578 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 579 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 580 | return btrfs_item_offset_nr(leaf, nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 581 | } |
| 582 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 583 | /* |
| 584 | * extra debugging checks to make sure all the items in a key are |
| 585 | * well formed and in the proper order |
| 586 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 587 | static int check_node(struct btrfs_root *root, struct btrfs_path *path, |
| 588 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 589 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 590 | struct extent_buffer *parent = NULL; |
| 591 | struct extent_buffer *node = path->nodes[level]; |
| 592 | struct btrfs_disk_key parent_key; |
| 593 | struct btrfs_disk_key node_key; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 594 | int parent_slot; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 595 | int slot; |
| 596 | struct btrfs_key cpukey; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 597 | u32 nritems = btrfs_header_nritems(node); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 598 | |
| 599 | if (path->nodes[level + 1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 600 | parent = path->nodes[level + 1]; |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 601 | |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 602 | slot = path->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 603 | BUG_ON(nritems == 0); |
| 604 | if (parent) { |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 605 | parent_slot = path->slots[level + 1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 606 | btrfs_node_key(parent, &parent_key, parent_slot); |
| 607 | btrfs_node_key(node, &node_key, 0); |
| 608 | BUG_ON(memcmp(&parent_key, &node_key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 609 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 610 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 611 | btrfs_header_bytenr(node)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 612 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 613 | BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 614 | if (slot != 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 615 | btrfs_node_key_to_cpu(node, &cpukey, slot - 1); |
| 616 | btrfs_node_key(node, &node_key, slot); |
| 617 | BUG_ON(comp_keys(&node_key, &cpukey) <= 0); |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 618 | } |
| 619 | if (slot < nritems - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 620 | btrfs_node_key_to_cpu(node, &cpukey, slot + 1); |
| 621 | btrfs_node_key(node, &node_key, slot); |
| 622 | BUG_ON(comp_keys(&node_key, &cpukey) >= 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 627 | /* |
| 628 | * extra checking to make sure all the items in a leaf are |
| 629 | * well formed and in the proper order |
| 630 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 631 | static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 632 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 633 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 634 | struct extent_buffer *leaf = path->nodes[level]; |
| 635 | struct extent_buffer *parent = NULL; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 636 | int parent_slot; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 637 | struct btrfs_key cpukey; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 638 | struct btrfs_disk_key parent_key; |
| 639 | struct btrfs_disk_key leaf_key; |
| 640 | int slot = path->slots[0]; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 641 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 642 | u32 nritems = btrfs_header_nritems(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 643 | |
| 644 | if (path->nodes[level + 1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 645 | parent = path->nodes[level + 1]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 646 | |
| 647 | if (nritems == 0) |
| 648 | return 0; |
| 649 | |
| 650 | if (parent) { |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 651 | parent_slot = path->slots[level + 1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 652 | btrfs_node_key(parent, &parent_key, parent_slot); |
| 653 | btrfs_item_key(leaf, &leaf_key, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 654 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 655 | BUG_ON(memcmp(&parent_key, &leaf_key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 656 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 657 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 658 | btrfs_header_bytenr(leaf)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 659 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 660 | #if 0 |
| 661 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
| 662 | btrfs_item_key_to_cpu(leaf, &cpukey, i + 1); |
| 663 | btrfs_item_key(leaf, &leaf_key, i); |
| 664 | if (comp_keys(&leaf_key, &cpukey) >= 0) { |
| 665 | btrfs_print_leaf(root, leaf); |
| 666 | printk("slot %d offset bad key\n", i); |
| 667 | BUG_ON(1); |
| 668 | } |
| 669 | if (btrfs_item_offset_nr(leaf, i) != |
| 670 | btrfs_item_end_nr(leaf, i + 1)) { |
| 671 | btrfs_print_leaf(root, leaf); |
| 672 | printk("slot %d offset bad\n", i); |
| 673 | BUG_ON(1); |
| 674 | } |
| 675 | if (i == 0) { |
| 676 | if (btrfs_item_offset_nr(leaf, i) + |
| 677 | btrfs_item_size_nr(leaf, i) != |
| 678 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 679 | btrfs_print_leaf(root, leaf); |
| 680 | printk("slot %d first offset bad\n", i); |
| 681 | BUG_ON(1); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | if (nritems > 0) { |
| 686 | if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) { |
| 687 | btrfs_print_leaf(root, leaf); |
| 688 | printk("slot %d bad size \n", nritems - 1); |
| 689 | BUG_ON(1); |
| 690 | } |
| 691 | } |
| 692 | #endif |
| 693 | if (slot != 0 && slot < nritems - 1) { |
| 694 | btrfs_item_key(leaf, &leaf_key, slot); |
| 695 | btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); |
| 696 | if (comp_keys(&leaf_key, &cpukey) <= 0) { |
| 697 | btrfs_print_leaf(root, leaf); |
| 698 | printk("slot %d offset bad key\n", slot); |
| 699 | BUG_ON(1); |
| 700 | } |
| 701 | if (btrfs_item_offset_nr(leaf, slot - 1) != |
| 702 | btrfs_item_end_nr(leaf, slot)) { |
| 703 | btrfs_print_leaf(root, leaf); |
| 704 | printk("slot %d offset bad\n", slot); |
| 705 | BUG_ON(1); |
| 706 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 707 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 708 | if (slot < nritems - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 709 | btrfs_item_key(leaf, &leaf_key, slot); |
| 710 | btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); |
| 711 | BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0); |
| 712 | if (btrfs_item_offset_nr(leaf, slot) != |
| 713 | btrfs_item_end_nr(leaf, slot + 1)) { |
| 714 | btrfs_print_leaf(root, leaf); |
| 715 | printk("slot %d offset bad\n", slot); |
| 716 | BUG_ON(1); |
| 717 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 718 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 719 | BUG_ON(btrfs_item_offset_nr(leaf, 0) + |
| 720 | btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 724 | static int noinline check_block(struct btrfs_root *root, |
| 725 | struct btrfs_path *path, int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 726 | { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 727 | u64 found_start; |
Chris Mason | 85d824c | 2008-04-10 10:23:19 -0400 | [diff] [blame] | 728 | return 0; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 729 | if (btrfs_header_level(path->nodes[level]) != level) |
| 730 | printk("warning: bad level %Lu wanted %d found %d\n", |
| 731 | path->nodes[level]->start, level, |
| 732 | btrfs_header_level(path->nodes[level])); |
| 733 | found_start = btrfs_header_bytenr(path->nodes[level]); |
| 734 | if (found_start != path->nodes[level]->start) { |
| 735 | printk("warning: bad bytentr %Lu found %Lu\n", |
| 736 | path->nodes[level]->start, found_start); |
| 737 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 738 | #if 0 |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 739 | struct extent_buffer *buf = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 740 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 741 | if (memcmp_extent_buffer(buf, root->fs_info->fsid, |
| 742 | (unsigned long)btrfs_header_fsid(buf), |
| 743 | BTRFS_FSID_SIZE)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 744 | printk("warning bad block %Lu\n", buf->start); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 745 | return 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 746 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 747 | #endif |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 748 | if (level == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 749 | return check_leaf(root, path, level); |
| 750 | return check_node(root, path, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 751 | } |
| 752 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 753 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 754 | * search for key in the extent_buffer. The items start at offset p, |
| 755 | * and they are item_size apart. There are 'max' items in p. |
| 756 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 757 | * the slot in the array is returned via slot, and it points to |
| 758 | * the place where you would insert key if it is not found in |
| 759 | * the array. |
| 760 | * |
| 761 | * slot may point to max if the key is bigger than all of the keys |
| 762 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 763 | static noinline int generic_bin_search(struct extent_buffer *eb, |
| 764 | unsigned long p, |
| 765 | int item_size, struct btrfs_key *key, |
| 766 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 767 | { |
| 768 | int low = 0; |
| 769 | int high = max; |
| 770 | int mid; |
| 771 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 772 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 773 | struct btrfs_disk_key unaligned; |
| 774 | unsigned long offset; |
| 775 | char *map_token = NULL; |
| 776 | char *kaddr = NULL; |
| 777 | unsigned long map_start = 0; |
| 778 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 779 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 780 | |
| 781 | while(low < high) { |
| 782 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 783 | offset = p + mid * item_size; |
| 784 | |
| 785 | if (!map_token || offset < map_start || |
| 786 | (offset + sizeof(struct btrfs_disk_key)) > |
| 787 | map_start + map_len) { |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 788 | if (map_token) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 789 | unmap_extent_buffer(eb, map_token, KM_USER0); |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 790 | map_token = NULL; |
| 791 | } |
| 792 | err = map_extent_buffer(eb, offset, |
| 793 | sizeof(struct btrfs_disk_key), |
| 794 | &map_token, &kaddr, |
| 795 | &map_start, &map_len, KM_USER0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 796 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 797 | if (!err) { |
| 798 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 799 | map_start); |
| 800 | } else { |
| 801 | read_extent_buffer(eb, &unaligned, |
| 802 | offset, sizeof(unaligned)); |
| 803 | tmp = &unaligned; |
| 804 | } |
| 805 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 806 | } else { |
| 807 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 808 | map_start); |
| 809 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 810 | ret = comp_keys(tmp, key); |
| 811 | |
| 812 | if (ret < 0) |
| 813 | low = mid + 1; |
| 814 | else if (ret > 0) |
| 815 | high = mid; |
| 816 | else { |
| 817 | *slot = mid; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 818 | if (map_token) |
| 819 | unmap_extent_buffer(eb, map_token, KM_USER0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | } |
| 823 | *slot = low; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 824 | if (map_token) |
| 825 | unmap_extent_buffer(eb, map_token, KM_USER0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 826 | return 1; |
| 827 | } |
| 828 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 829 | /* |
| 830 | * simple bin_search frontend that does the right thing for |
| 831 | * leaves vs nodes |
| 832 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 833 | static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 834 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 835 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 836 | if (level == 0) { |
| 837 | return generic_bin_search(eb, |
| 838 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 839 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 840 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 841 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 842 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 843 | return generic_bin_search(eb, |
| 844 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 845 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 846 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 847 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 848 | } |
| 849 | return -1; |
| 850 | } |
| 851 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 852 | /* given a node and slot number, this reads the blocks it points to. The |
| 853 | * extent buffer is returned with a reference taken (but unlocked). |
| 854 | * NULL is returned on error. |
| 855 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 856 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 857 | struct extent_buffer *parent, int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 858 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 859 | int level = btrfs_header_level(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 860 | if (slot < 0) |
| 861 | return NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 862 | if (slot >= btrfs_header_nritems(parent)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 863 | return NULL; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 864 | |
| 865 | BUG_ON(level == 0); |
| 866 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 867 | return read_tree_block(root, btrfs_node_blockptr(parent, slot), |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 868 | btrfs_level_size(root, level - 1), |
| 869 | btrfs_node_ptr_generation(parent, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 870 | } |
| 871 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 872 | /* |
| 873 | * node level balancing, used to make sure nodes are in proper order for |
| 874 | * item deletion. We balance from the top down, so we have to make sure |
| 875 | * that a deletion won't leave an node completely empty later on. |
| 876 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 877 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 878 | struct btrfs_root *root, |
| 879 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 880 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 881 | struct extent_buffer *right = NULL; |
| 882 | struct extent_buffer *mid; |
| 883 | struct extent_buffer *left = NULL; |
| 884 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 885 | int ret = 0; |
| 886 | int wret; |
| 887 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 888 | int orig_slot = path->slots[level]; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 889 | int err_on_enospc = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 890 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 891 | |
| 892 | if (level == 0) |
| 893 | return 0; |
| 894 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 895 | mid = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 896 | WARN_ON(!path->locks[level]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 897 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 898 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 899 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 900 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 901 | if (level < BTRFS_MAX_LEVEL - 1) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 902 | parent = path->nodes[level + 1]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 903 | pslot = path->slots[level + 1]; |
| 904 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 905 | /* |
| 906 | * deal with the case where there is only one pointer in the root |
| 907 | * by promoting the node below to a root |
| 908 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 909 | if (!parent) { |
| 910 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 911 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 912 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 913 | return 0; |
| 914 | |
| 915 | /* promote the child to a root */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 916 | child = read_node_slot(root, mid, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 917 | btrfs_tree_lock(child); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 918 | BUG_ON(!child); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 919 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0); |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 920 | BUG_ON(ret); |
| 921 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 922 | spin_lock(&root->node_lock); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 923 | root->node = child; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 924 | spin_unlock(&root->node_lock); |
| 925 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 926 | ret = btrfs_update_extent_ref(trans, root, child->start, |
| 927 | mid->start, child->start, |
| 928 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 929 | trans->transid, level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 930 | BUG_ON(ret); |
| 931 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 932 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 933 | btrfs_tree_unlock(child); |
| 934 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 935 | path->nodes[level] = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 936 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 937 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 938 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 939 | free_extent_buffer(mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 940 | ret = btrfs_free_extent(trans, root, mid->start, mid->len, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 941 | mid->start, root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 942 | btrfs_header_generation(mid), |
| 943 | level, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 944 | /* once for the root ptr */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 945 | free_extent_buffer(mid); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 946 | return ret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 947 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 948 | if (btrfs_header_nritems(mid) > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 949 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 950 | return 0; |
| 951 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 952 | if (btrfs_header_nritems(mid) < 2) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 953 | err_on_enospc = 1; |
| 954 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 955 | left = read_node_slot(root, parent, pslot - 1); |
| 956 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 957 | btrfs_tree_lock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 958 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 959 | parent, pslot - 1, &left, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 960 | if (wret) { |
| 961 | ret = wret; |
| 962 | goto enospc; |
| 963 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 964 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 965 | right = read_node_slot(root, parent, pslot + 1); |
| 966 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 967 | btrfs_tree_lock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 968 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 969 | parent, pslot + 1, &right, 0); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 970 | if (wret) { |
| 971 | ret = wret; |
| 972 | goto enospc; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 977 | if (left) { |
| 978 | orig_slot += btrfs_header_nritems(left); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 979 | wret = push_node_left(trans, root, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 980 | if (wret < 0) |
| 981 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 982 | if (btrfs_header_nritems(mid) < 2) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 983 | err_on_enospc = 1; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 984 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 985 | |
| 986 | /* |
| 987 | * then try to empty the right most buffer into the middle |
| 988 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 989 | if (right) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 990 | wret = push_node_left(trans, root, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 991 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 992 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 993 | if (btrfs_header_nritems(right) == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 994 | u64 bytenr = right->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 995 | u64 generation = btrfs_header_generation(parent); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 996 | u32 blocksize = right->len; |
| 997 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 998 | clean_tree_block(trans, root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 999 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1000 | free_extent_buffer(right); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1001 | right = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1002 | wret = del_ptr(trans, root, path, level + 1, pslot + |
| 1003 | 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1004 | if (wret) |
| 1005 | ret = wret; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1006 | wret = btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1007 | blocksize, parent->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1008 | btrfs_header_owner(parent), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1009 | generation, level, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1010 | if (wret) |
| 1011 | ret = wret; |
| 1012 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1013 | struct btrfs_disk_key right_key; |
| 1014 | btrfs_node_key(right, &right_key, 0); |
| 1015 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1016 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1017 | } |
| 1018 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1019 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1020 | /* |
| 1021 | * we're not allowed to leave a node with one item in the |
| 1022 | * tree during a delete. A deletion from lower in the tree |
| 1023 | * could try to delete the only pointer in this node. |
| 1024 | * So, pull some keys from the left. |
| 1025 | * There has to be a left pointer at this point because |
| 1026 | * otherwise we would have pulled some pointers from the |
| 1027 | * right |
| 1028 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1029 | BUG_ON(!left); |
| 1030 | wret = balance_node_right(trans, root, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1031 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1032 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1033 | goto enospc; |
| 1034 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1035 | if (wret == 1) { |
| 1036 | wret = push_node_left(trans, root, left, mid, 1); |
| 1037 | if (wret < 0) |
| 1038 | ret = wret; |
| 1039 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1040 | BUG_ON(wret == 1); |
| 1041 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1042 | if (btrfs_header_nritems(mid) == 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1043 | /* we've managed to empty the middle node, drop it */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1044 | u64 root_gen = btrfs_header_generation(parent); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1045 | u64 bytenr = mid->start; |
| 1046 | u32 blocksize = mid->len; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1047 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1048 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1049 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1050 | free_extent_buffer(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1051 | mid = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1052 | wret = del_ptr(trans, root, path, level + 1, pslot); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1053 | if (wret) |
| 1054 | ret = wret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1055 | wret = btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1056 | parent->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1057 | btrfs_header_owner(parent), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1058 | root_gen, level, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1059 | if (wret) |
| 1060 | ret = wret; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1061 | } else { |
| 1062 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1063 | struct btrfs_disk_key mid_key; |
| 1064 | btrfs_node_key(mid, &mid_key, 0); |
| 1065 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1066 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1067 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1068 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1069 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1070 | if (left) { |
| 1071 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1072 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1073 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1074 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1075 | path->slots[level + 1] -= 1; |
| 1076 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1077 | if (mid) { |
| 1078 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1079 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1080 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1081 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1082 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1083 | path->slots[level] = orig_slot; |
| 1084 | } |
| 1085 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1086 | /* double check we haven't messed things up */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1087 | check_block(root, path, level); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1088 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1089 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1090 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1091 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1092 | if (right) { |
| 1093 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1094 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1095 | } |
| 1096 | if (left) { |
| 1097 | if (path->nodes[level] != left) |
| 1098 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1099 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1100 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1101 | return ret; |
| 1102 | } |
| 1103 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1104 | /* Node balancing for insertion. Here we only split or push nodes around |
| 1105 | * when they are completely full. This is also done top down, so we |
| 1106 | * have to be pessimistic. |
| 1107 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1108 | static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, |
| 1109 | struct btrfs_root *root, |
| 1110 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1111 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1112 | struct extent_buffer *right = NULL; |
| 1113 | struct extent_buffer *mid; |
| 1114 | struct extent_buffer *left = NULL; |
| 1115 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1116 | int ret = 0; |
| 1117 | int wret; |
| 1118 | int pslot; |
| 1119 | int orig_slot = path->slots[level]; |
| 1120 | u64 orig_ptr; |
| 1121 | |
| 1122 | if (level == 0) |
| 1123 | return 1; |
| 1124 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1125 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1126 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1127 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
| 1128 | |
| 1129 | if (level < BTRFS_MAX_LEVEL - 1) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1130 | parent = path->nodes[level + 1]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1131 | pslot = path->slots[level + 1]; |
| 1132 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1133 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1134 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1135 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1136 | left = read_node_slot(root, parent, pslot - 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1137 | |
| 1138 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1139 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1140 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1141 | |
| 1142 | btrfs_tree_lock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1143 | left_nr = btrfs_header_nritems(left); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1144 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1145 | wret = 1; |
| 1146 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1147 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1148 | pslot - 1, &left, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1149 | if (ret) |
| 1150 | wret = 1; |
| 1151 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1152 | wret = push_node_left(trans, root, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1153 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1154 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1155 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1156 | if (wret < 0) |
| 1157 | ret = wret; |
| 1158 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1159 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1160 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1161 | btrfs_node_key(mid, &disk_key, 0); |
| 1162 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 1163 | btrfs_mark_buffer_dirty(parent); |
| 1164 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1165 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1166 | path->slots[level + 1] -= 1; |
| 1167 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1168 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1169 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1170 | } else { |
| 1171 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1172 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1173 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1174 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1175 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1176 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1177 | return 0; |
| 1178 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1179 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1180 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1181 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1182 | right = read_node_slot(root, parent, pslot + 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * then try to empty the right most buffer into the middle |
| 1186 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1187 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1188 | u32 right_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1189 | btrfs_tree_lock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1190 | right_nr = btrfs_header_nritems(right); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1191 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1192 | wret = 1; |
| 1193 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1194 | ret = btrfs_cow_block(trans, root, right, |
| 1195 | parent, pslot + 1, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1196 | &right, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1197 | if (ret) |
| 1198 | wret = 1; |
| 1199 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1200 | wret = balance_node_right(trans, root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1201 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1202 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1203 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1204 | if (wret < 0) |
| 1205 | ret = wret; |
| 1206 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1207 | struct btrfs_disk_key disk_key; |
| 1208 | |
| 1209 | btrfs_node_key(right, &disk_key, 0); |
| 1210 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 1211 | btrfs_mark_buffer_dirty(parent); |
| 1212 | |
| 1213 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 1214 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1215 | path->slots[level + 1] += 1; |
| 1216 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1217 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1218 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1219 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1220 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1221 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1222 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1223 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1226 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1227 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1228 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1229 | return 1; |
| 1230 | } |
| 1231 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1232 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1233 | * readahead one full node of leaves, finding things that are close |
| 1234 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1235 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1236 | static noinline void reada_for_search(struct btrfs_root *root, |
| 1237 | struct btrfs_path *path, |
| 1238 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1239 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1240 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 1241 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1242 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1243 | u64 search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1244 | u64 lowest_read; |
| 1245 | u64 highest_read; |
| 1246 | u64 nread = 0; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1247 | int direction = path->reada; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1248 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1249 | u32 nr; |
| 1250 | u32 blocksize; |
| 1251 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1252 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1253 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1254 | return; |
| 1255 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1256 | if (!path->nodes[level]) |
| 1257 | return; |
| 1258 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1259 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1260 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1261 | search = btrfs_node_blockptr(node, slot); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1262 | blocksize = btrfs_level_size(root, level - 1); |
| 1263 | eb = btrfs_find_tree_block(root, search, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1264 | if (eb) { |
| 1265 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1266 | return; |
| 1267 | } |
| 1268 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1269 | highest_read = search; |
| 1270 | lowest_read = search; |
| 1271 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1272 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1273 | nr = slot; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1274 | while(1) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1275 | if (direction < 0) { |
| 1276 | if (nr == 0) |
| 1277 | break; |
| 1278 | nr--; |
| 1279 | } else if (direction > 0) { |
| 1280 | nr++; |
| 1281 | if (nr >= nritems) |
| 1282 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1283 | } |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 1284 | if (path->reada < 0 && objectid) { |
| 1285 | btrfs_node_key(node, &disk_key, nr); |
| 1286 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 1287 | break; |
| 1288 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1289 | search = btrfs_node_blockptr(node, nr); |
| 1290 | if ((search >= lowest_read && search <= highest_read) || |
| 1291 | (search < lowest_read && lowest_read - search <= 32768) || |
| 1292 | (search > highest_read && search - highest_read <= 32768)) { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1293 | readahead_tree_block(root, search, blocksize, |
| 1294 | btrfs_node_ptr_generation(node, nr)); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1295 | nread += blocksize; |
| 1296 | } |
| 1297 | nscan++; |
| 1298 | if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32)) |
| 1299 | break; |
| 1300 | if(nread > (1024 * 1024) || nscan > 128) |
| 1301 | break; |
| 1302 | |
| 1303 | if (search < lowest_read) |
| 1304 | lowest_read = search; |
| 1305 | if (search > highest_read) |
| 1306 | highest_read = search; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1307 | } |
| 1308 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1309 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1310 | /* |
| 1311 | * when we walk down the tree, it is usually safe to unlock the higher layers in |
| 1312 | * the tree. The exceptions are when our path goes through slot 0, because operations |
| 1313 | * on the tree might require changing key pointers higher up in the tree. |
| 1314 | * |
| 1315 | * callers might also have set path->keep_locks, which tells this code to |
| 1316 | * keep the lock if the path points to the last slot in the block. This is |
| 1317 | * part of walking through the tree, and selecting the next slot in the higher |
| 1318 | * block. |
| 1319 | * |
| 1320 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. |
| 1321 | * so if lowest_unlock is 1, level 0 won't be unlocked |
| 1322 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1323 | static noinline void unlock_up(struct btrfs_path *path, int level, |
| 1324 | int lowest_unlock) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1325 | { |
| 1326 | int i; |
| 1327 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1328 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1329 | struct extent_buffer *t; |
| 1330 | |
| 1331 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 1332 | if (!path->nodes[i]) |
| 1333 | break; |
| 1334 | if (!path->locks[i]) |
| 1335 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1336 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1337 | skip_level = i + 1; |
| 1338 | continue; |
| 1339 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1340 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1341 | u32 nritems; |
| 1342 | t = path->nodes[i]; |
| 1343 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1344 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1345 | skip_level = i + 1; |
| 1346 | continue; |
| 1347 | } |
| 1348 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1349 | if (skip_level < i && i >= lowest_unlock) |
| 1350 | no_skips = 1; |
| 1351 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1352 | t = path->nodes[i]; |
| 1353 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
| 1354 | btrfs_tree_unlock(t); |
| 1355 | path->locks[i] = 0; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1360 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1361 | * look for key in the tree. path is filled in with nodes along the way |
| 1362 | * if key is found, we return zero and you can find the item in the leaf |
| 1363 | * level of the path (level 0) |
| 1364 | * |
| 1365 | * 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] | 1366 | * be inserted, and 1 is returned. If there are other errors during the |
| 1367 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1368 | * |
| 1369 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 1370 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 1371 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1372 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1373 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1374 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 1375 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1376 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1377 | struct extent_buffer *b; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1378 | struct extent_buffer *tmp; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1379 | int slot; |
| 1380 | int ret; |
| 1381 | int level; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1382 | int should_reada = p->reada; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1383 | int lowest_unlock = 1; |
Chris Mason | 594a24e | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1384 | int blocksize; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1385 | u8 lowest_level = 0; |
Chris Mason | 594a24e | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1386 | u64 blocknr; |
| 1387 | u64 gen; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1388 | struct btrfs_key prealloc_block; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1389 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1390 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 1391 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 1392 | WARN_ON(p->nodes[0] != NULL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1393 | WARN_ON(cow && root == root->fs_info->extent_root && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1394 | !mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1395 | if (ins_len < 0) |
| 1396 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1397 | |
| 1398 | prealloc_block.objectid = 0; |
| 1399 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1400 | again: |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1401 | if (p->skip_locking) |
| 1402 | b = btrfs_root_node(root); |
| 1403 | else |
| 1404 | b = btrfs_lock_root_node(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1405 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1406 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1407 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1408 | |
| 1409 | /* |
| 1410 | * setup the path here so we can release it under lock |
| 1411 | * contention with the cow code |
| 1412 | */ |
| 1413 | p->nodes[level] = b; |
| 1414 | if (!p->skip_locking) |
| 1415 | p->locks[level] = 1; |
| 1416 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1417 | if (cow) { |
| 1418 | int wret; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1419 | |
| 1420 | /* is a cow on this block not required */ |
| 1421 | spin_lock(&root->fs_info->hash_lock); |
| 1422 | if (btrfs_header_generation(b) == trans->transid && |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1423 | btrfs_header_owner(b) == root->root_key.objectid && |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1424 | !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 1425 | spin_unlock(&root->fs_info->hash_lock); |
| 1426 | goto cow_done; |
| 1427 | } |
| 1428 | spin_unlock(&root->fs_info->hash_lock); |
| 1429 | |
| 1430 | /* ok, we have to cow, is our old prealloc the right |
| 1431 | * size? |
| 1432 | */ |
| 1433 | if (prealloc_block.objectid && |
| 1434 | prealloc_block.offset != b->len) { |
| 1435 | btrfs_free_reserved_extent(root, |
| 1436 | prealloc_block.objectid, |
| 1437 | prealloc_block.offset); |
| 1438 | prealloc_block.objectid = 0; |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | * for higher level blocks, try not to allocate blocks |
| 1443 | * with the block and the parent locks held. |
| 1444 | */ |
| 1445 | if (level > 1 && !prealloc_block.objectid && |
| 1446 | btrfs_path_lock_waiting(p, level)) { |
| 1447 | u32 size = b->len; |
| 1448 | u64 hint = b->start; |
| 1449 | |
| 1450 | btrfs_release_path(root, p); |
| 1451 | ret = btrfs_reserve_extent(trans, root, |
| 1452 | size, size, 0, |
| 1453 | hint, (u64)-1, |
| 1454 | &prealloc_block, 0); |
| 1455 | BUG_ON(ret); |
| 1456 | goto again; |
| 1457 | } |
| 1458 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1459 | wret = btrfs_cow_block(trans, root, b, |
| 1460 | p->nodes[level + 1], |
| 1461 | p->slots[level + 1], |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1462 | &b, prealloc_block.objectid); |
| 1463 | prealloc_block.objectid = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1464 | if (wret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1465 | free_extent_buffer(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1466 | ret = wret; |
| 1467 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1468 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1469 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1470 | cow_done: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1471 | BUG_ON(!cow && ins_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1472 | if (level != btrfs_header_level(b)) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1473 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1474 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1475 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1476 | p->nodes[level] = b; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1477 | if (!p->skip_locking) |
| 1478 | p->locks[level] = 1; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1479 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1480 | ret = check_block(root, p, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1481 | if (ret) { |
| 1482 | ret = -1; |
| 1483 | goto done; |
| 1484 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1485 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1486 | ret = bin_search(b, key, level, &slot); |
| 1487 | if (level != 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1488 | if (ret && slot > 0) |
| 1489 | slot -= 1; |
| 1490 | p->slots[level] = slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1491 | if (ins_len > 0 && btrfs_header_nritems(b) >= |
Chris Mason | 1514794 | 2008-04-24 09:22:51 -0400 | [diff] [blame] | 1492 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1493 | int sret = split_node(trans, root, p, level); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1494 | BUG_ON(sret > 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1495 | if (sret) { |
| 1496 | ret = sret; |
| 1497 | goto done; |
| 1498 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1499 | b = p->nodes[level]; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1500 | slot = p->slots[level]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1501 | } else if (ins_len < 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1502 | int sret = balance_level(trans, root, p, |
| 1503 | level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1504 | if (sret) { |
| 1505 | ret = sret; |
| 1506 | goto done; |
| 1507 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1508 | b = p->nodes[level]; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1509 | if (!b) { |
| 1510 | btrfs_release_path(NULL, p); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1511 | goto again; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1512 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1513 | slot = p->slots[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1514 | BUG_ON(btrfs_header_nritems(b) == 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1515 | } |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 1516 | unlock_up(p, level, lowest_unlock); |
| 1517 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1518 | /* this is only true while dropping a snapshot */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1519 | if (level == lowest_level) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1520 | ret = 0; |
| 1521 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1522 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1523 | |
Chris Mason | 594a24e | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1524 | blocknr = btrfs_node_blockptr(b, slot); |
| 1525 | gen = btrfs_node_ptr_generation(b, slot); |
| 1526 | blocksize = btrfs_level_size(root, level - 1); |
| 1527 | |
| 1528 | tmp = btrfs_find_tree_block(root, blocknr, blocksize); |
| 1529 | if (tmp && btrfs_buffer_uptodate(tmp, gen)) { |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1530 | b = tmp; |
| 1531 | } else { |
| 1532 | /* |
| 1533 | * reduce lock contention at high levels |
| 1534 | * of the btree by dropping locks before |
| 1535 | * we read. |
| 1536 | */ |
| 1537 | if (level > 1) { |
| 1538 | btrfs_release_path(NULL, p); |
| 1539 | if (tmp) |
| 1540 | free_extent_buffer(tmp); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 1541 | if (should_reada) |
| 1542 | reada_for_search(root, p, |
| 1543 | level, slot, |
| 1544 | key->objectid); |
| 1545 | |
Chris Mason | 594a24e | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1546 | tmp = read_tree_block(root, blocknr, |
| 1547 | blocksize, gen); |
| 1548 | if (tmp) |
| 1549 | free_extent_buffer(tmp); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1550 | goto again; |
| 1551 | } else { |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1552 | if (tmp) |
| 1553 | free_extent_buffer(tmp); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 1554 | if (should_reada) |
| 1555 | reada_for_search(root, p, |
| 1556 | level, slot, |
| 1557 | key->objectid); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1558 | b = read_node_slot(root, b, slot); |
| 1559 | } |
| 1560 | } |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1561 | if (!p->skip_locking) |
| 1562 | btrfs_tree_lock(b); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1563 | } else { |
| 1564 | p->slots[level] = slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1565 | if (ins_len > 0 && btrfs_leaf_free_space(root, b) < |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1566 | sizeof(struct btrfs_item) + ins_len) { |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1567 | int sret = split_leaf(trans, root, key, |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 1568 | p, ins_len, ret == 0); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1569 | BUG_ON(sret > 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1570 | if (sret) { |
| 1571 | ret = sret; |
| 1572 | goto done; |
| 1573 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1574 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1575 | unlock_up(p, level, lowest_unlock); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1576 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1577 | } |
| 1578 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 1579 | ret = 1; |
| 1580 | done: |
| 1581 | if (prealloc_block.objectid) { |
| 1582 | btrfs_free_reserved_extent(root, |
| 1583 | prealloc_block.objectid, |
| 1584 | prealloc_block.offset); |
| 1585 | } |
| 1586 | |
| 1587 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1588 | } |
| 1589 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1590 | int btrfs_merge_path(struct btrfs_trans_handle *trans, |
| 1591 | struct btrfs_root *root, |
| 1592 | struct btrfs_key *node_keys, |
| 1593 | u64 *nodes, int lowest_level) |
| 1594 | { |
| 1595 | struct extent_buffer *eb; |
| 1596 | struct extent_buffer *parent; |
| 1597 | struct btrfs_key key; |
| 1598 | u64 bytenr; |
| 1599 | u64 generation; |
| 1600 | u32 blocksize; |
| 1601 | int level; |
| 1602 | int slot; |
| 1603 | int key_match; |
| 1604 | int ret; |
| 1605 | |
| 1606 | eb = btrfs_lock_root_node(root); |
| 1607 | ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0); |
| 1608 | BUG_ON(ret); |
| 1609 | |
| 1610 | parent = eb; |
| 1611 | while (1) { |
| 1612 | level = btrfs_header_level(parent); |
| 1613 | if (level == 0 || level <= lowest_level) |
| 1614 | break; |
| 1615 | |
| 1616 | ret = bin_search(parent, &node_keys[lowest_level], level, |
| 1617 | &slot); |
| 1618 | if (ret && slot > 0) |
| 1619 | slot--; |
| 1620 | |
| 1621 | bytenr = btrfs_node_blockptr(parent, slot); |
| 1622 | if (nodes[level - 1] == bytenr) |
| 1623 | break; |
| 1624 | |
| 1625 | blocksize = btrfs_level_size(root, level - 1); |
| 1626 | generation = btrfs_node_ptr_generation(parent, slot); |
| 1627 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 1628 | key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key)); |
| 1629 | |
| 1630 | /* |
| 1631 | * if node keys match and node pointer hasn't been modified |
| 1632 | * in the running transaction, we can merge the path. for |
| 1633 | * reloc trees, the node pointer check is skipped, this is |
| 1634 | * because the reloc trees are fully controlled by the space |
| 1635 | * balance code, no one else can modify them. |
| 1636 | */ |
| 1637 | if (!nodes[level - 1] || !key_match || |
| 1638 | (generation == trans->transid && |
| 1639 | root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)) { |
| 1640 | next_level: |
| 1641 | if (level == 1 || level == lowest_level + 1) |
| 1642 | break; |
| 1643 | |
| 1644 | eb = read_tree_block(root, bytenr, blocksize, |
| 1645 | generation); |
| 1646 | btrfs_tree_lock(eb); |
| 1647 | |
| 1648 | ret = btrfs_cow_block(trans, root, eb, parent, slot, |
| 1649 | &eb, 0); |
| 1650 | BUG_ON(ret); |
| 1651 | |
| 1652 | btrfs_tree_unlock(parent); |
| 1653 | free_extent_buffer(parent); |
| 1654 | parent = eb; |
| 1655 | continue; |
| 1656 | } |
| 1657 | |
| 1658 | if (generation == trans->transid) { |
| 1659 | u32 refs; |
| 1660 | BUG_ON(btrfs_header_owner(eb) != |
| 1661 | BTRFS_TREE_RELOC_OBJECTID); |
| 1662 | /* |
| 1663 | * lock the block to keep __btrfs_cow_block from |
| 1664 | * changing the reference count. |
| 1665 | */ |
| 1666 | eb = read_tree_block(root, bytenr, blocksize, |
| 1667 | generation); |
| 1668 | btrfs_tree_lock(eb); |
| 1669 | |
| 1670 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, |
| 1671 | blocksize, &refs); |
| 1672 | BUG_ON(ret); |
| 1673 | /* |
| 1674 | * if replace block whose reference count is one, |
| 1675 | * we have to "drop the subtree". so skip it for |
| 1676 | * simplicity |
| 1677 | */ |
| 1678 | if (refs == 1) { |
| 1679 | btrfs_tree_unlock(eb); |
| 1680 | free_extent_buffer(eb); |
| 1681 | goto next_level; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | btrfs_set_node_blockptr(parent, slot, nodes[level - 1]); |
| 1686 | btrfs_set_node_ptr_generation(parent, slot, trans->transid); |
| 1687 | btrfs_mark_buffer_dirty(parent); |
| 1688 | |
| 1689 | ret = btrfs_inc_extent_ref(trans, root, |
| 1690 | nodes[level - 1], |
| 1691 | blocksize, parent->start, |
| 1692 | btrfs_header_owner(parent), |
| 1693 | btrfs_header_generation(parent), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1694 | level - 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1695 | BUG_ON(ret); |
| 1696 | ret = btrfs_free_extent(trans, root, bytenr, |
| 1697 | blocksize, parent->start, |
| 1698 | btrfs_header_owner(parent), |
| 1699 | btrfs_header_generation(parent), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1700 | level - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1701 | BUG_ON(ret); |
| 1702 | |
| 1703 | if (generation == trans->transid) { |
| 1704 | btrfs_tree_unlock(eb); |
| 1705 | free_extent_buffer(eb); |
| 1706 | } |
| 1707 | break; |
| 1708 | } |
| 1709 | btrfs_tree_unlock(parent); |
| 1710 | free_extent_buffer(parent); |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1714 | /* |
| 1715 | * adjust the pointers going up the tree, starting at level |
| 1716 | * making sure the right key of each node is points to 'key'. |
| 1717 | * This is used after shifting pointers to the left, so it stops |
| 1718 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 1719 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1720 | * |
| 1721 | * If this fails to write a tree block, it returns -1, but continues |
| 1722 | * fixing up the blocks in ram so the tree is consistent. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1723 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1724 | static int fixup_low_keys(struct btrfs_trans_handle *trans, |
| 1725 | struct btrfs_root *root, struct btrfs_path *path, |
| 1726 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1727 | { |
| 1728 | int i; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1729 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1730 | struct extent_buffer *t; |
| 1731 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1732 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1733 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1734 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1735 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1736 | t = path->nodes[i]; |
| 1737 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1738 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1739 | if (tslot != 0) |
| 1740 | break; |
| 1741 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1742 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1743 | } |
| 1744 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1745 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1746 | * update item key. |
| 1747 | * |
| 1748 | * This function isn't completely safe. It's the caller's responsibility |
| 1749 | * that the new key won't break the order |
| 1750 | */ |
| 1751 | int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, |
| 1752 | struct btrfs_root *root, struct btrfs_path *path, |
| 1753 | struct btrfs_key *new_key) |
| 1754 | { |
| 1755 | struct btrfs_disk_key disk_key; |
| 1756 | struct extent_buffer *eb; |
| 1757 | int slot; |
| 1758 | |
| 1759 | eb = path->nodes[0]; |
| 1760 | slot = path->slots[0]; |
| 1761 | if (slot > 0) { |
| 1762 | btrfs_item_key(eb, &disk_key, slot - 1); |
| 1763 | if (comp_keys(&disk_key, new_key) >= 0) |
| 1764 | return -1; |
| 1765 | } |
| 1766 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 1767 | btrfs_item_key(eb, &disk_key, slot + 1); |
| 1768 | if (comp_keys(&disk_key, new_key) <= 0) |
| 1769 | return -1; |
| 1770 | } |
| 1771 | |
| 1772 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 1773 | btrfs_set_item_key(eb, &disk_key, slot); |
| 1774 | btrfs_mark_buffer_dirty(eb); |
| 1775 | if (slot == 0) |
| 1776 | fixup_low_keys(trans, root, path, &disk_key, 1); |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1781 | * 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] | 1782 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1783 | * |
| 1784 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 1785 | * 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] | 1786 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1787 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 1788 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1789 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1790 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1791 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1792 | int src_nritems; |
| 1793 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1794 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1795 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1796 | src_nritems = btrfs_header_nritems(src); |
| 1797 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1798 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1799 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 1800 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1801 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1802 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1803 | return 1; |
| 1804 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1805 | if (push_items <= 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1806 | return 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1807 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1808 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1809 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1810 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1811 | if (push_items < src_nritems) { |
| 1812 | /* leave at least 8 pointers in the node if |
| 1813 | * we aren't going to empty it |
| 1814 | */ |
| 1815 | if (src_nritems - push_items < 8) { |
| 1816 | if (push_items <= 8) |
| 1817 | return 1; |
| 1818 | push_items -= 8; |
| 1819 | } |
| 1820 | } |
| 1821 | } else |
| 1822 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1823 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1824 | copy_extent_buffer(dst, src, |
| 1825 | btrfs_node_key_ptr_offset(dst_nritems), |
| 1826 | btrfs_node_key_ptr_offset(0), |
| 1827 | push_items * sizeof(struct btrfs_key_ptr)); |
| 1828 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1829 | if (push_items < src_nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1830 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 1831 | btrfs_node_key_ptr_offset(push_items), |
| 1832 | (src_nritems - push_items) * |
| 1833 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1834 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1835 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 1836 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 1837 | btrfs_mark_buffer_dirty(src); |
| 1838 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1839 | |
| 1840 | ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items); |
| 1841 | BUG_ON(ret); |
| 1842 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1843 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1844 | } |
| 1845 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1846 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1847 | * try to push data from one node into the next node right in the |
| 1848 | * tree. |
| 1849 | * |
| 1850 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 1851 | * error, and > 0 if there was no room in the right hand block. |
| 1852 | * |
| 1853 | * this will only push up to 1/2 the contents of the left node over |
| 1854 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1855 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 1856 | struct btrfs_root *root, |
| 1857 | struct extent_buffer *dst, |
| 1858 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1859 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1860 | int push_items = 0; |
| 1861 | int max_push; |
| 1862 | int src_nritems; |
| 1863 | int dst_nritems; |
| 1864 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1865 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1866 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 1867 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 1868 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1869 | src_nritems = btrfs_header_nritems(src); |
| 1870 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1871 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1872 | if (push_items <= 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1873 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | if (src_nritems < 4) { |
| 1877 | return 1; |
| 1878 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1879 | |
| 1880 | max_push = src_nritems / 2 + 1; |
| 1881 | /* don't try to empty the node */ |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1882 | if (max_push >= src_nritems) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1883 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1884 | } |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1885 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1886 | if (max_push < push_items) |
| 1887 | push_items = max_push; |
| 1888 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1889 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 1890 | btrfs_node_key_ptr_offset(0), |
| 1891 | (dst_nritems) * |
| 1892 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1893 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1894 | copy_extent_buffer(dst, src, |
| 1895 | btrfs_node_key_ptr_offset(0), |
| 1896 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
| 1897 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1898 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1899 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 1900 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1901 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1902 | btrfs_mark_buffer_dirty(src); |
| 1903 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1904 | |
| 1905 | ret = btrfs_update_ref(trans, root, src, dst, 0, push_items); |
| 1906 | BUG_ON(ret); |
| 1907 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1908 | return ret; |
| 1909 | } |
| 1910 | |
| 1911 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1912 | * helper function to insert a new root level in the tree. |
| 1913 | * A new node is allocated, and a single item is inserted to |
| 1914 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1915 | * |
| 1916 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1917 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1918 | static int noinline insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1919 | struct btrfs_root *root, |
| 1920 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1921 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1922 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1923 | struct extent_buffer *lower; |
| 1924 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1925 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1926 | struct btrfs_disk_key lower_key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1927 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1928 | |
| 1929 | BUG_ON(path->nodes[level]); |
| 1930 | BUG_ON(path->nodes[level-1] != root->node); |
| 1931 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1932 | lower = path->nodes[level-1]; |
| 1933 | if (level == 1) |
| 1934 | btrfs_item_key(lower, &lower_key, 0); |
| 1935 | else |
| 1936 | btrfs_node_key(lower, &lower_key, 0); |
| 1937 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1938 | c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
| 1939 | root->root_key.objectid, trans->transid, |
Christoph Hellwig | ad3d81b | 2008-09-05 16:43:28 -0400 | [diff] [blame] | 1940 | level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1941 | if (IS_ERR(c)) |
| 1942 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1943 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1944 | memset_extent_buffer(c, 0, 0, root->nodesize); |
| 1945 | btrfs_set_header_nritems(c, 1); |
| 1946 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1947 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1948 | btrfs_set_header_generation(c, trans->transid); |
| 1949 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1950 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1951 | write_extent_buffer(c, root->fs_info->fsid, |
| 1952 | (unsigned long)btrfs_header_fsid(c), |
| 1953 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1954 | |
| 1955 | write_extent_buffer(c, root->fs_info->chunk_tree_uuid, |
| 1956 | (unsigned long)btrfs_header_chunk_tree_uuid(c), |
| 1957 | BTRFS_UUID_SIZE); |
| 1958 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1959 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1960 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1961 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1962 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1963 | |
| 1964 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1965 | |
| 1966 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1967 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1968 | spin_lock(&root->node_lock); |
| 1969 | old = root->node; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1970 | root->node = c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1971 | spin_unlock(&root->node_lock); |
| 1972 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1973 | ret = btrfs_update_extent_ref(trans, root, lower->start, |
| 1974 | lower->start, c->start, |
| 1975 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1976 | trans->transid, level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1977 | BUG_ON(ret); |
| 1978 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1979 | /* the super has an extra ref to root->node */ |
| 1980 | free_extent_buffer(old); |
| 1981 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1982 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1983 | extent_buffer_get(c); |
| 1984 | path->nodes[level] = c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1985 | path->locks[level] = 1; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1986 | path->slots[level] = 0; |
| 1987 | return 0; |
| 1988 | } |
| 1989 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1990 | /* |
| 1991 | * worker function to insert a single pointer in a node. |
| 1992 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1993 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1994 | * slot and level indicate where you want the key to go, and |
| 1995 | * blocknr is the block the key points to. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1996 | * |
| 1997 | * returns zero on success and < 0 on any error |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1998 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1999 | static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root |
| 2000 | *root, struct btrfs_path *path, struct btrfs_disk_key |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2001 | *key, u64 bytenr, int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2002 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2003 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2004 | int nritems; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2005 | |
| 2006 | BUG_ON(!path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2007 | lower = path->nodes[level]; |
| 2008 | nritems = btrfs_header_nritems(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2009 | if (slot > nritems) |
| 2010 | BUG(); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2011 | if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2012 | BUG(); |
| 2013 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2014 | memmove_extent_buffer(lower, |
| 2015 | btrfs_node_key_ptr_offset(slot + 1), |
| 2016 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2017 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2018 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2019 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2020 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2021 | WARN_ON(trans->transid == 0); |
| 2022 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2023 | btrfs_set_header_nritems(lower, nritems + 1); |
| 2024 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2025 | return 0; |
| 2026 | } |
| 2027 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2028 | /* |
| 2029 | * split the node at the specified level in path in two. |
| 2030 | * The path is corrected to point to the appropriate node after the split |
| 2031 | * |
| 2032 | * Before splitting this tries to make some room in the node by pushing |
| 2033 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2034 | * |
| 2035 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2036 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2037 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 2038 | struct btrfs_root *root, |
| 2039 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2040 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2041 | struct extent_buffer *c; |
| 2042 | struct extent_buffer *split; |
| 2043 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2044 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2045 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2046 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2047 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2048 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2049 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2050 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2051 | if (c == root->node) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2052 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2053 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2054 | if (ret) |
| 2055 | return ret; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2056 | } else { |
| 2057 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2058 | c = path->nodes[level]; |
| 2059 | if (!ret && btrfs_header_nritems(c) < |
Chris Mason | c448acf | 2008-04-24 09:34:34 -0400 | [diff] [blame] | 2060 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2061 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2062 | if (ret < 0) |
| 2063 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2064 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2065 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2066 | c_nritems = btrfs_header_nritems(c); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2067 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2068 | split = btrfs_alloc_free_block(trans, root, root->nodesize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2069 | path->nodes[level + 1]->start, |
| 2070 | root->root_key.objectid, |
| 2071 | trans->transid, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2072 | if (IS_ERR(split)) |
| 2073 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2074 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2075 | btrfs_set_header_flags(split, btrfs_header_flags(c)); |
| 2076 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2077 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2078 | btrfs_set_header_generation(split, trans->transid); |
| 2079 | btrfs_set_header_owner(split, root->root_key.objectid); |
Chris Mason | 63b10fc | 2008-04-01 11:21:32 -0400 | [diff] [blame] | 2080 | btrfs_set_header_flags(split, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2081 | write_extent_buffer(split, root->fs_info->fsid, |
| 2082 | (unsigned long)btrfs_header_fsid(split), |
| 2083 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2084 | write_extent_buffer(split, root->fs_info->chunk_tree_uuid, |
| 2085 | (unsigned long)btrfs_header_chunk_tree_uuid(split), |
| 2086 | BTRFS_UUID_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2087 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2088 | mid = (c_nritems + 1) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2089 | |
| 2090 | copy_extent_buffer(split, c, |
| 2091 | btrfs_node_key_ptr_offset(0), |
| 2092 | btrfs_node_key_ptr_offset(mid), |
| 2093 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 2094 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 2095 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2096 | ret = 0; |
| 2097 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2098 | btrfs_mark_buffer_dirty(c); |
| 2099 | btrfs_mark_buffer_dirty(split); |
| 2100 | |
| 2101 | btrfs_node_key(split, &disk_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2102 | wret = insert_ptr(trans, root, path, &disk_key, split->start, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2103 | path->slots[level + 1] + 1, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2104 | level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2105 | if (wret) |
| 2106 | ret = wret; |
| 2107 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2108 | ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid); |
| 2109 | BUG_ON(ret); |
| 2110 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 2111 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2112 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2113 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2114 | free_extent_buffer(c); |
| 2115 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2116 | path->slots[level + 1] += 1; |
| 2117 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2118 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2119 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2120 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2121 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2122 | } |
| 2123 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2124 | /* |
| 2125 | * how many bytes are required to store the items in a leaf. start |
| 2126 | * and nr indicate which items in the leaf to check. This totals up the |
| 2127 | * space used both by the item structs and the item data |
| 2128 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2129 | static int leaf_space_used(struct extent_buffer *l, int start, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2130 | { |
| 2131 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2132 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2133 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2134 | |
| 2135 | if (!nr) |
| 2136 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2137 | data_len = btrfs_item_end_nr(l, start); |
| 2138 | data_len = data_len - btrfs_item_offset_nr(l, end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2139 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2140 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2141 | return data_len; |
| 2142 | } |
| 2143 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2144 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2145 | * The space between the end of the leaf items and |
| 2146 | * the start of the leaf data. IOW, how much room |
| 2147 | * the leaf has left for both items and data |
| 2148 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2149 | int noinline btrfs_leaf_free_space(struct btrfs_root *root, |
| 2150 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2151 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2152 | int nritems = btrfs_header_nritems(leaf); |
| 2153 | int ret; |
| 2154 | ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 2155 | if (ret < 0) { |
| 2156 | printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n", |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 2157 | ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2158 | leaf_space_used(leaf, 0, nritems), nritems); |
| 2159 | } |
| 2160 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /* |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2164 | * push some data in the path leaf to the right, trying to free up at |
| 2165 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2166 | * |
| 2167 | * returns 1 if the push failed because the other node didn't have enough |
| 2168 | * 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] | 2169 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2170 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2171 | *root, struct btrfs_path *path, int data_size, |
| 2172 | int empty) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2173 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2174 | struct extent_buffer *left = path->nodes[0]; |
| 2175 | struct extent_buffer *right; |
| 2176 | struct extent_buffer *upper; |
| 2177 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2178 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2179 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2180 | int free_space; |
| 2181 | int push_space = 0; |
| 2182 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2183 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2184 | u32 left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2185 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2186 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2187 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2188 | u32 this_item_size; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2189 | int ret; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2190 | |
| 2191 | slot = path->slots[1]; |
| 2192 | if (!path->nodes[1]) { |
| 2193 | return 1; |
| 2194 | } |
| 2195 | upper = path->nodes[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2196 | if (slot >= btrfs_header_nritems(upper) - 1) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2197 | return 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2198 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2199 | WARN_ON(!btrfs_tree_locked(path->nodes[1])); |
| 2200 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2201 | right = read_node_slot(root, upper, slot + 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2202 | btrfs_tree_lock(right); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2203 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2204 | if (free_space < data_size + sizeof(struct btrfs_item)) |
| 2205 | goto out_unlock; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2206 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2207 | /* cow and double check */ |
| 2208 | ret = btrfs_cow_block(trans, root, right, upper, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2209 | slot + 1, &right, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2210 | if (ret) |
| 2211 | goto out_unlock; |
| 2212 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2213 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2214 | if (free_space < data_size + sizeof(struct btrfs_item)) |
| 2215 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2216 | |
| 2217 | left_nritems = btrfs_header_nritems(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2218 | if (left_nritems == 0) |
| 2219 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2220 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2221 | if (empty) |
| 2222 | nr = 0; |
| 2223 | else |
| 2224 | nr = 1; |
| 2225 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2226 | if (path->slots[0] >= left_nritems) |
| 2227 | push_space += data_size + sizeof(*item); |
| 2228 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2229 | i = left_nritems - 1; |
| 2230 | while (i >= nr) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2231 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2232 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2233 | if (!empty && push_items > 0) { |
| 2234 | if (path->slots[0] > i) |
| 2235 | break; |
| 2236 | if (path->slots[0] == i) { |
| 2237 | int space = btrfs_leaf_free_space(root, left); |
| 2238 | if (space + push_space * 2 > free_space) |
| 2239 | break; |
| 2240 | } |
| 2241 | } |
| 2242 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2243 | if (path->slots[0] == i) |
| 2244 | push_space += data_size + sizeof(*item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2245 | |
| 2246 | if (!left->map_token) { |
| 2247 | map_extent_buffer(left, (unsigned long)item, |
| 2248 | sizeof(struct btrfs_item), |
| 2249 | &left->map_token, &left->kaddr, |
| 2250 | &left->map_start, &left->map_len, |
| 2251 | KM_USER1); |
| 2252 | } |
| 2253 | |
| 2254 | this_item_size = btrfs_item_size(left, item); |
| 2255 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2256 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2257 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2258 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2259 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2260 | if (i == 0) |
| 2261 | break; |
| 2262 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2263 | } |
| 2264 | if (left->map_token) { |
| 2265 | unmap_extent_buffer(left, left->map_token, KM_USER1); |
| 2266 | left->map_token = NULL; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2267 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2268 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2269 | if (push_items == 0) |
| 2270 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2271 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2272 | if (!empty && push_items == left_nritems) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 2273 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2274 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2275 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2276 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2277 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2278 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2279 | push_space -= leaf_data_end(root, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2280 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2281 | /* make room in the right data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2282 | data_end = leaf_data_end(root, right); |
| 2283 | memmove_extent_buffer(right, |
| 2284 | btrfs_leaf_data(right) + data_end - push_space, |
| 2285 | btrfs_leaf_data(right) + data_end, |
| 2286 | BTRFS_LEAF_DATA_SIZE(root) - data_end); |
| 2287 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2288 | /* copy from the left data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2289 | copy_extent_buffer(right, left, btrfs_leaf_data(right) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2290 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 2291 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 2292 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2293 | |
| 2294 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 2295 | btrfs_item_nr_offset(0), |
| 2296 | right_nritems * sizeof(struct btrfs_item)); |
| 2297 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2298 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2299 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 2300 | btrfs_item_nr_offset(left_nritems - push_items), |
| 2301 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2302 | |
| 2303 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2304 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2305 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2306 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2307 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2308 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2309 | if (!right->map_token) { |
| 2310 | map_extent_buffer(right, (unsigned long)item, |
| 2311 | sizeof(struct btrfs_item), |
| 2312 | &right->map_token, &right->kaddr, |
| 2313 | &right->map_start, &right->map_len, |
| 2314 | KM_USER1); |
| 2315 | } |
| 2316 | push_space -= btrfs_item_size(right, item); |
| 2317 | btrfs_set_item_offset(right, item, push_space); |
| 2318 | } |
| 2319 | |
| 2320 | if (right->map_token) { |
| 2321 | unmap_extent_buffer(right, right->map_token, KM_USER1); |
| 2322 | right->map_token = NULL; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2323 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2324 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2325 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2326 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2327 | if (left_nritems) |
| 2328 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2329 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 2330 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2331 | ret = btrfs_update_ref(trans, root, left, right, 0, push_items); |
| 2332 | BUG_ON(ret); |
| 2333 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2334 | btrfs_item_key(right, &disk_key, 0); |
| 2335 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2336 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2337 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2338 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2339 | if (path->slots[0] >= left_nritems) { |
| 2340 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2341 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 2342 | clean_tree_block(trans, root, path->nodes[0]); |
| 2343 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2344 | free_extent_buffer(path->nodes[0]); |
| 2345 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2346 | path->slots[1] += 1; |
| 2347 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2348 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2349 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2350 | } |
| 2351 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2352 | |
| 2353 | out_unlock: |
| 2354 | btrfs_tree_unlock(right); |
| 2355 | free_extent_buffer(right); |
| 2356 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2357 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2358 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 2359 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2360 | * push some data in the path leaf to the left, trying to free up at |
| 2361 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 2362 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2363 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2364 | *root, struct btrfs_path *path, int data_size, |
| 2365 | int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2366 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2367 | struct btrfs_disk_key disk_key; |
| 2368 | struct extent_buffer *right = path->nodes[0]; |
| 2369 | struct extent_buffer *left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2370 | int slot; |
| 2371 | int i; |
| 2372 | int free_space; |
| 2373 | int push_space = 0; |
| 2374 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2375 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2376 | u32 old_left_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2377 | u32 right_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2378 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2379 | int ret = 0; |
| 2380 | int wret; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2381 | u32 this_item_size; |
| 2382 | u32 old_left_item_size; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2383 | |
| 2384 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2385 | if (slot == 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2386 | return 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2387 | if (!path->nodes[1]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2388 | return 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2389 | |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2390 | right_nritems = btrfs_header_nritems(right); |
| 2391 | if (right_nritems == 0) { |
| 2392 | return 1; |
| 2393 | } |
| 2394 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2395 | WARN_ON(!btrfs_tree_locked(path->nodes[1])); |
| 2396 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2397 | left = read_node_slot(root, path->nodes[1], slot - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2398 | btrfs_tree_lock(left); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2399 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2400 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2401 | ret = 1; |
| 2402 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2403 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2404 | |
| 2405 | /* cow and double check */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2406 | ret = btrfs_cow_block(trans, root, left, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2407 | path->nodes[1], slot - 1, &left, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2408 | if (ret) { |
| 2409 | /* we hit -ENOSPC, but it isn't fatal here */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2410 | ret = 1; |
| 2411 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2412 | } |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2413 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2414 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2415 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2416 | ret = 1; |
| 2417 | goto out; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2418 | } |
| 2419 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2420 | if (empty) |
| 2421 | nr = right_nritems; |
| 2422 | else |
| 2423 | nr = right_nritems - 1; |
| 2424 | |
| 2425 | for (i = 0; i < nr; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2426 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2427 | if (!right->map_token) { |
| 2428 | map_extent_buffer(right, (unsigned long)item, |
| 2429 | sizeof(struct btrfs_item), |
| 2430 | &right->map_token, &right->kaddr, |
| 2431 | &right->map_start, &right->map_len, |
| 2432 | KM_USER1); |
| 2433 | } |
| 2434 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2435 | if (!empty && push_items > 0) { |
| 2436 | if (path->slots[0] < i) |
| 2437 | break; |
| 2438 | if (path->slots[0] == i) { |
| 2439 | int space = btrfs_leaf_free_space(root, right); |
| 2440 | if (space + push_space * 2 > free_space) |
| 2441 | break; |
| 2442 | } |
| 2443 | } |
| 2444 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2445 | if (path->slots[0] == i) |
| 2446 | push_space += data_size + sizeof(*item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2447 | |
| 2448 | this_item_size = btrfs_item_size(right, item); |
| 2449 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2450 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2451 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2452 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2453 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2454 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2455 | |
| 2456 | if (right->map_token) { |
| 2457 | unmap_extent_buffer(right, right->map_token, KM_USER1); |
| 2458 | right->map_token = NULL; |
| 2459 | } |
| 2460 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2461 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2462 | ret = 1; |
| 2463 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2464 | } |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2465 | if (!empty && push_items == btrfs_header_nritems(right)) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 2466 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2467 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2468 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2469 | copy_extent_buffer(left, right, |
| 2470 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 2471 | btrfs_item_nr_offset(0), |
| 2472 | push_items * sizeof(struct btrfs_item)); |
| 2473 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2474 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2475 | btrfs_item_offset_nr(right, push_items -1); |
| 2476 | |
| 2477 | copy_extent_buffer(left, right, btrfs_leaf_data(left) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2478 | leaf_data_end(root, left) - push_space, |
| 2479 | btrfs_leaf_data(right) + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2480 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2481 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2482 | old_left_nritems = btrfs_header_nritems(left); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2483 | BUG_ON(old_left_nritems < 0); |
| 2484 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2485 | old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2486 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2487 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2488 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2489 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2490 | if (!left->map_token) { |
| 2491 | map_extent_buffer(left, (unsigned long)item, |
| 2492 | sizeof(struct btrfs_item), |
| 2493 | &left->map_token, &left->kaddr, |
| 2494 | &left->map_start, &left->map_len, |
| 2495 | KM_USER1); |
| 2496 | } |
| 2497 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2498 | ioff = btrfs_item_offset(left, item); |
| 2499 | btrfs_set_item_offset(left, item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2500 | ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2501 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2502 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2503 | if (left->map_token) { |
| 2504 | unmap_extent_buffer(left, left->map_token, KM_USER1); |
| 2505 | left->map_token = NULL; |
| 2506 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2507 | |
| 2508 | /* fixup right node */ |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2509 | if (push_items > right_nritems) { |
| 2510 | printk("push items %d nr %u\n", push_items, right_nritems); |
| 2511 | WARN_ON(1); |
| 2512 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2513 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2514 | if (push_items < right_nritems) { |
| 2515 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
| 2516 | leaf_data_end(root, right); |
| 2517 | memmove_extent_buffer(right, btrfs_leaf_data(right) + |
| 2518 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 2519 | btrfs_leaf_data(right) + |
| 2520 | leaf_data_end(root, right), push_space); |
| 2521 | |
| 2522 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2523 | btrfs_item_nr_offset(push_items), |
| 2524 | (btrfs_header_nritems(right) - push_items) * |
| 2525 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2526 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 2527 | right_nritems -= push_items; |
| 2528 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2529 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2530 | for (i = 0; i < right_nritems; i++) { |
| 2531 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2532 | |
| 2533 | if (!right->map_token) { |
| 2534 | map_extent_buffer(right, (unsigned long)item, |
| 2535 | sizeof(struct btrfs_item), |
| 2536 | &right->map_token, &right->kaddr, |
| 2537 | &right->map_start, &right->map_len, |
| 2538 | KM_USER1); |
| 2539 | } |
| 2540 | |
| 2541 | push_space = push_space - btrfs_item_size(right, item); |
| 2542 | btrfs_set_item_offset(right, item, push_space); |
| 2543 | } |
| 2544 | if (right->map_token) { |
| 2545 | unmap_extent_buffer(right, right->map_token, KM_USER1); |
| 2546 | right->map_token = NULL; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2547 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2548 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2549 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2550 | if (right_nritems) |
| 2551 | btrfs_mark_buffer_dirty(right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 2552 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2553 | ret = btrfs_update_ref(trans, root, right, left, |
| 2554 | old_left_nritems, push_items); |
| 2555 | BUG_ON(ret); |
| 2556 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2557 | btrfs_item_key(right, &disk_key, 0); |
| 2558 | wret = fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2559 | if (wret) |
| 2560 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2561 | |
| 2562 | /* then fixup the leaf pointer in the path */ |
| 2563 | if (path->slots[0] < push_items) { |
| 2564 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2565 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 2566 | clean_tree_block(trans, root, path->nodes[0]); |
| 2567 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2568 | free_extent_buffer(path->nodes[0]); |
| 2569 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2570 | path->slots[1] -= 1; |
| 2571 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2572 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2573 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2574 | path->slots[0] -= push_items; |
| 2575 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2576 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2577 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2578 | out: |
| 2579 | btrfs_tree_unlock(left); |
| 2580 | free_extent_buffer(left); |
| 2581 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2582 | } |
| 2583 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2584 | /* |
| 2585 | * split the path's leaf in two, making sure there is at least data_size |
| 2586 | * available for the resulting leaf level of the path. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2587 | * |
| 2588 | * returns 0 if all went well and < 0 on failure. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2589 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2590 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 2591 | struct btrfs_root *root, |
| 2592 | struct btrfs_key *ins_key, |
| 2593 | struct btrfs_path *path, int data_size, |
| 2594 | int extend) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2595 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2596 | struct extent_buffer *l; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2597 | u32 nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2598 | int mid; |
| 2599 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2600 | struct extent_buffer *right; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2601 | int space_needed = data_size + sizeof(struct btrfs_item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2602 | int data_copy_size; |
| 2603 | int rt_data_off; |
| 2604 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2605 | int ret = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2606 | int wret; |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2607 | int double_split; |
| 2608 | int num_doubles = 0; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2609 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2610 | |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2611 | if (extend) |
| 2612 | space_needed = data_size; |
| 2613 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 2614 | /* first try to make some room by pushing left and right */ |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2615 | if (ins_key->type != BTRFS_DIR_ITEM_KEY) { |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2616 | wret = push_leaf_right(trans, root, path, data_size, 0); |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2617 | if (wret < 0) { |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 2618 | return wret; |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2619 | } |
| 2620 | if (wret) { |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 2621 | wret = push_leaf_left(trans, root, path, data_size, 0); |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2622 | if (wret < 0) |
| 2623 | return wret; |
| 2624 | } |
| 2625 | l = path->nodes[0]; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2626 | |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2627 | /* did the pushes work? */ |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2628 | if (btrfs_leaf_free_space(root, l) >= space_needed) |
Chris Mason | 3685f79 | 2007-10-19 09:23:27 -0400 | [diff] [blame] | 2629 | return 0; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2630 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2631 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2632 | if (!path->nodes[1]) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2633 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2634 | if (ret) |
| 2635 | return ret; |
| 2636 | } |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2637 | again: |
| 2638 | double_split = 0; |
| 2639 | l = path->nodes[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2640 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2641 | nritems = btrfs_header_nritems(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2642 | mid = (nritems + 1)/ 2; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2643 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2644 | right = btrfs_alloc_free_block(trans, root, root->leafsize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2645 | path->nodes[1]->start, |
| 2646 | root->root_key.objectid, |
| 2647 | trans->transid, 0, l->start, 0); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2648 | if (IS_ERR(right)) { |
| 2649 | BUG_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2650 | return PTR_ERR(right); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2651 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2652 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2653 | memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2654 | btrfs_set_header_bytenr(right, right->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2655 | btrfs_set_header_generation(right, trans->transid); |
| 2656 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 2657 | btrfs_set_header_level(right, 0); |
| 2658 | write_extent_buffer(right, root->fs_info->fsid, |
| 2659 | (unsigned long)btrfs_header_fsid(right), |
| 2660 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2661 | |
| 2662 | write_extent_buffer(right, root->fs_info->chunk_tree_uuid, |
| 2663 | (unsigned long)btrfs_header_chunk_tree_uuid(right), |
| 2664 | BTRFS_UUID_SIZE); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2665 | if (mid <= slot) { |
| 2666 | if (nritems == 1 || |
| 2667 | leaf_space_used(l, mid, nritems - mid) + space_needed > |
| 2668 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 2669 | if (slot >= nritems) { |
| 2670 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2671 | btrfs_set_header_nritems(right, 0); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2672 | wret = insert_ptr(trans, root, path, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2673 | &disk_key, right->start, |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2674 | path->slots[1] + 1, 1); |
| 2675 | if (wret) |
| 2676 | ret = wret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2677 | |
| 2678 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2679 | free_extent_buffer(path->nodes[0]); |
| 2680 | path->nodes[0] = right; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2681 | path->slots[0] = 0; |
| 2682 | path->slots[1] += 1; |
Chris Mason | 0ef8b24 | 2008-04-03 16:29:02 -0400 | [diff] [blame] | 2683 | btrfs_mark_buffer_dirty(right); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2684 | return ret; |
| 2685 | } |
| 2686 | mid = slot; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2687 | if (mid != nritems && |
| 2688 | leaf_space_used(l, mid, nritems - mid) + |
| 2689 | space_needed > BTRFS_LEAF_DATA_SIZE(root)) { |
| 2690 | double_split = 1; |
| 2691 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2692 | } |
| 2693 | } else { |
| 2694 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
| 2695 | BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2696 | if (!extend && slot == 0) { |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2697 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2698 | btrfs_set_header_nritems(right, 0); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2699 | wret = insert_ptr(trans, root, path, |
| 2700 | &disk_key, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2701 | right->start, |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 2702 | path->slots[1], 1); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2703 | if (wret) |
| 2704 | ret = wret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2705 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2706 | free_extent_buffer(path->nodes[0]); |
| 2707 | path->nodes[0] = right; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2708 | path->slots[0] = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 2709 | if (path->slots[1] == 0) { |
| 2710 | wret = fixup_low_keys(trans, root, |
| 2711 | path, &disk_key, 1); |
| 2712 | if (wret) |
| 2713 | ret = wret; |
| 2714 | } |
Chris Mason | 0ef8b24 | 2008-04-03 16:29:02 -0400 | [diff] [blame] | 2715 | btrfs_mark_buffer_dirty(right); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2716 | return ret; |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2717 | } else if (extend && slot == 0) { |
| 2718 | mid = 1; |
| 2719 | } else { |
| 2720 | mid = slot; |
| 2721 | if (mid != nritems && |
| 2722 | leaf_space_used(l, mid, nritems - mid) + |
| 2723 | space_needed > BTRFS_LEAF_DATA_SIZE(root)) { |
| 2724 | double_split = 1; |
| 2725 | } |
Chris Mason | 5ee78ac | 2007-10-19 14:01:21 -0400 | [diff] [blame] | 2726 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2727 | } |
| 2728 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2729 | nritems = nritems - mid; |
| 2730 | btrfs_set_header_nritems(right, nritems); |
| 2731 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); |
| 2732 | |
| 2733 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 2734 | btrfs_item_nr_offset(mid), |
| 2735 | nritems * sizeof(struct btrfs_item)); |
| 2736 | |
| 2737 | copy_extent_buffer(right, l, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2738 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 2739 | data_copy_size, btrfs_leaf_data(l) + |
| 2740 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2741 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2742 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 2743 | btrfs_item_end_nr(l, mid); |
| 2744 | |
| 2745 | for (i = 0; i < nritems; i++) { |
| 2746 | struct btrfs_item *item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2747 | u32 ioff; |
| 2748 | |
| 2749 | if (!right->map_token) { |
| 2750 | map_extent_buffer(right, (unsigned long)item, |
| 2751 | sizeof(struct btrfs_item), |
| 2752 | &right->map_token, &right->kaddr, |
| 2753 | &right->map_start, &right->map_len, |
| 2754 | KM_USER1); |
| 2755 | } |
| 2756 | |
| 2757 | ioff = btrfs_item_offset(right, item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2758 | btrfs_set_item_offset(right, item, ioff + rt_data_off); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 2759 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2760 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2761 | if (right->map_token) { |
| 2762 | unmap_extent_buffer(right, right->map_token, KM_USER1); |
| 2763 | right->map_token = NULL; |
| 2764 | } |
| 2765 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2766 | btrfs_set_header_nritems(l, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2767 | ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2768 | btrfs_item_key(right, &disk_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2769 | wret = insert_ptr(trans, root, path, &disk_key, right->start, |
| 2770 | path->slots[1] + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2771 | if (wret) |
| 2772 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2773 | |
| 2774 | btrfs_mark_buffer_dirty(right); |
| 2775 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2776 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2777 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2778 | ret = btrfs_update_ref(trans, root, l, right, 0, nritems); |
| 2779 | BUG_ON(ret); |
| 2780 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2781 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2782 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2783 | free_extent_buffer(path->nodes[0]); |
| 2784 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2785 | path->slots[0] -= mid; |
| 2786 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2787 | } else { |
| 2788 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2789 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2790 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2791 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2792 | BUG_ON(path->slots[0] < 0); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 2793 | |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 2794 | if (double_split) { |
| 2795 | BUG_ON(num_doubles != 0); |
| 2796 | num_doubles++; |
| 2797 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2798 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2799 | return ret; |
| 2800 | } |
| 2801 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2802 | /* |
| 2803 | * make the item pointed to by the path smaller. new_size indicates |
| 2804 | * how small to make it, and from_end tells us if we just chop bytes |
| 2805 | * off the end of the item or if we shift the item to chop bytes off |
| 2806 | * the front. |
| 2807 | */ |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2808 | int btrfs_truncate_item(struct btrfs_trans_handle *trans, |
| 2809 | struct btrfs_root *root, |
| 2810 | struct btrfs_path *path, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2811 | u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2812 | { |
| 2813 | int ret = 0; |
| 2814 | int slot; |
| 2815 | int slot_orig; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2816 | struct extent_buffer *leaf; |
| 2817 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2818 | u32 nritems; |
| 2819 | unsigned int data_end; |
| 2820 | unsigned int old_data_start; |
| 2821 | unsigned int old_size; |
| 2822 | unsigned int size_diff; |
| 2823 | int i; |
| 2824 | |
| 2825 | slot_orig = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2826 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2827 | slot = path->slots[0]; |
| 2828 | |
| 2829 | old_size = btrfs_item_size_nr(leaf, slot); |
| 2830 | if (old_size == new_size) |
| 2831 | return 0; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2832 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2833 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2834 | data_end = leaf_data_end(root, leaf); |
| 2835 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2836 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2837 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2838 | size_diff = old_size - new_size; |
| 2839 | |
| 2840 | BUG_ON(slot < 0); |
| 2841 | BUG_ON(slot >= nritems); |
| 2842 | |
| 2843 | /* |
| 2844 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 2845 | */ |
| 2846 | /* first correct the data pointers */ |
| 2847 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2848 | u32 ioff; |
| 2849 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2850 | |
| 2851 | if (!leaf->map_token) { |
| 2852 | map_extent_buffer(leaf, (unsigned long)item, |
| 2853 | sizeof(struct btrfs_item), |
| 2854 | &leaf->map_token, &leaf->kaddr, |
| 2855 | &leaf->map_start, &leaf->map_len, |
| 2856 | KM_USER1); |
| 2857 | } |
| 2858 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2859 | ioff = btrfs_item_offset(leaf, item); |
| 2860 | btrfs_set_item_offset(leaf, item, ioff + size_diff); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2861 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2862 | |
| 2863 | if (leaf->map_token) { |
| 2864 | unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); |
| 2865 | leaf->map_token = NULL; |
| 2866 | } |
| 2867 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2868 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2869 | if (from_end) { |
| 2870 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 2871 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 2872 | data_end, old_data_start + new_size - data_end); |
| 2873 | } else { |
| 2874 | struct btrfs_disk_key disk_key; |
| 2875 | u64 offset; |
| 2876 | |
| 2877 | btrfs_item_key(leaf, &disk_key, slot); |
| 2878 | |
| 2879 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 2880 | unsigned long ptr; |
| 2881 | struct btrfs_file_extent_item *fi; |
| 2882 | |
| 2883 | fi = btrfs_item_ptr(leaf, slot, |
| 2884 | struct btrfs_file_extent_item); |
| 2885 | fi = (struct btrfs_file_extent_item *)( |
| 2886 | (unsigned long)fi - size_diff); |
| 2887 | |
| 2888 | if (btrfs_file_extent_type(leaf, fi) == |
| 2889 | BTRFS_FILE_EXTENT_INLINE) { |
| 2890 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 2891 | memmove_extent_buffer(leaf, ptr, |
| 2892 | (unsigned long)fi, |
| 2893 | offsetof(struct btrfs_file_extent_item, |
| 2894 | disk_bytenr)); |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 2899 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 2900 | data_end, old_data_start - data_end); |
| 2901 | |
| 2902 | offset = btrfs_disk_key_offset(&disk_key); |
| 2903 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 2904 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 2905 | if (slot == 0) |
| 2906 | fixup_low_keys(trans, root, path, &disk_key, 1); |
| 2907 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2908 | |
| 2909 | item = btrfs_item_nr(leaf, slot); |
| 2910 | btrfs_set_item_size(leaf, item, new_size); |
| 2911 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2912 | |
| 2913 | ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2914 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 2915 | btrfs_print_leaf(root, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2916 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2917 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 2918 | return ret; |
| 2919 | } |
| 2920 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2921 | /* |
| 2922 | * make the item pointed to by the path bigger, data_size is the new size. |
| 2923 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2924 | int btrfs_extend_item(struct btrfs_trans_handle *trans, |
| 2925 | struct btrfs_root *root, struct btrfs_path *path, |
| 2926 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2927 | { |
| 2928 | int ret = 0; |
| 2929 | int slot; |
| 2930 | int slot_orig; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2931 | struct extent_buffer *leaf; |
| 2932 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2933 | u32 nritems; |
| 2934 | unsigned int data_end; |
| 2935 | unsigned int old_data; |
| 2936 | unsigned int old_size; |
| 2937 | int i; |
| 2938 | |
| 2939 | slot_orig = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2940 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2941 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2942 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2943 | data_end = leaf_data_end(root, leaf); |
| 2944 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2945 | if (btrfs_leaf_free_space(root, leaf) < data_size) { |
| 2946 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2947 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2948 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2949 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2950 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2951 | |
| 2952 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 2953 | if (slot >= nritems) { |
| 2954 | btrfs_print_leaf(root, leaf); |
| 2955 | printk("slot %d too large, nritems %d\n", slot, nritems); |
| 2956 | BUG_ON(1); |
| 2957 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2958 | |
| 2959 | /* |
| 2960 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 2961 | */ |
| 2962 | /* first correct the data pointers */ |
| 2963 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2964 | u32 ioff; |
| 2965 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2966 | |
| 2967 | if (!leaf->map_token) { |
| 2968 | map_extent_buffer(leaf, (unsigned long)item, |
| 2969 | sizeof(struct btrfs_item), |
| 2970 | &leaf->map_token, &leaf->kaddr, |
| 2971 | &leaf->map_start, &leaf->map_len, |
| 2972 | KM_USER1); |
| 2973 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2974 | ioff = btrfs_item_offset(leaf, item); |
| 2975 | btrfs_set_item_offset(leaf, item, ioff - data_size); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2976 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2977 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2978 | if (leaf->map_token) { |
| 2979 | unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); |
| 2980 | leaf->map_token = NULL; |
| 2981 | } |
| 2982 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2983 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2984 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2985 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 2986 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2987 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2988 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2989 | old_size = btrfs_item_size_nr(leaf, slot); |
| 2990 | item = btrfs_item_nr(leaf, slot); |
| 2991 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 2992 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2993 | |
| 2994 | ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2995 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 2996 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2997 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2998 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 2999 | return ret; |
| 3000 | } |
| 3001 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3002 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3003 | * Given a key and some data, insert items into the tree. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3004 | * This does all the path init required, making room in the tree if needed. |
| 3005 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3006 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3007 | struct btrfs_root *root, |
| 3008 | struct btrfs_path *path, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3009 | struct btrfs_key *cpu_key, u32 *data_size, |
| 3010 | int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3011 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3012 | struct extent_buffer *leaf; |
| 3013 | struct btrfs_item *item; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3014 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3015 | int slot; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3016 | int slot_orig; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3017 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3018 | u32 nritems; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3019 | u32 total_size = 0; |
| 3020 | u32 total_data = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3021 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3022 | struct btrfs_disk_key disk_key; |
| 3023 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3024 | for (i = 0; i < nr; i++) { |
| 3025 | total_data += data_size[i]; |
| 3026 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3027 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3028 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3029 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3030 | if (ret == 0) |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 3031 | return -EEXIST; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 3032 | if (ret < 0) |
| 3033 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3034 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3035 | slot_orig = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3036 | leaf = path->nodes[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3037 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3038 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3039 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3040 | |
Chris Mason | f25956c | 2008-09-12 15:32:53 -0400 | [diff] [blame] | 3041 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 3042 | btrfs_print_leaf(root, leaf); |
| 3043 | printk("not enough freespace need %u have %d\n", |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3044 | total_size, btrfs_leaf_free_space(root, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3045 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3046 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3047 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3048 | slot = path->slots[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3049 | BUG_ON(slot < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3050 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3051 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3052 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3053 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3054 | if (old_data < data_end) { |
| 3055 | btrfs_print_leaf(root, leaf); |
| 3056 | printk("slot %d old_data %d data_end %d\n", |
| 3057 | slot, old_data, data_end); |
| 3058 | BUG_ON(1); |
| 3059 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3060 | /* |
| 3061 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 3062 | */ |
| 3063 | /* first correct the data pointers */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3064 | WARN_ON(leaf->map_token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3065 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3066 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3067 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3068 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3069 | if (!leaf->map_token) { |
| 3070 | map_extent_buffer(leaf, (unsigned long)item, |
| 3071 | sizeof(struct btrfs_item), |
| 3072 | &leaf->map_token, &leaf->kaddr, |
| 3073 | &leaf->map_start, &leaf->map_len, |
| 3074 | KM_USER1); |
| 3075 | } |
| 3076 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3077 | ioff = btrfs_item_offset(leaf, item); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3078 | btrfs_set_item_offset(leaf, item, ioff - total_data); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3079 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3080 | if (leaf->map_token) { |
| 3081 | unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); |
| 3082 | leaf->map_token = NULL; |
| 3083 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3084 | |
| 3085 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3086 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3087 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3088 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3089 | |
| 3090 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3091 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3092 | data_end - total_data, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3093 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3094 | data_end = old_data; |
| 3095 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3096 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3097 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 3098 | for (i = 0; i < nr; i++) { |
| 3099 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 3100 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
| 3101 | item = btrfs_item_nr(leaf, slot + i); |
| 3102 | btrfs_set_item_offset(leaf, item, data_end - data_size[i]); |
| 3103 | data_end -= data_size[i]; |
| 3104 | btrfs_set_item_size(leaf, item, data_size[i]); |
| 3105 | } |
| 3106 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3107 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3108 | |
| 3109 | ret = 0; |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 3110 | if (slot == 0) { |
| 3111 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3112 | ret = fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 3113 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3114 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3115 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 3116 | btrfs_print_leaf(root, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3117 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3118 | } |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 3119 | out: |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3120 | return ret; |
| 3121 | } |
| 3122 | |
| 3123 | /* |
| 3124 | * Given a key and some data, insert an item into the tree. |
| 3125 | * This does all the path init required, making room in the tree if needed. |
| 3126 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3127 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 3128 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 3129 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3130 | { |
| 3131 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3132 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3133 | struct extent_buffer *leaf; |
| 3134 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3135 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3136 | path = btrfs_alloc_path(); |
| 3137 | BUG_ON(!path); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3138 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3139 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3140 | leaf = path->nodes[0]; |
| 3141 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3142 | write_extent_buffer(leaf, data, ptr, data_size); |
| 3143 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3144 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3145 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3146 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3147 | } |
| 3148 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3149 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3150 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3151 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3152 | * the tree should have been previously balanced so the deletion does not |
| 3153 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3154 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3155 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 3156 | struct btrfs_path *path, int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3157 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3158 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3159 | u32 nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3160 | int ret = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3161 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3162 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3163 | nritems = btrfs_header_nritems(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3164 | if (slot != nritems -1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3165 | memmove_extent_buffer(parent, |
| 3166 | btrfs_node_key_ptr_offset(slot), |
| 3167 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3168 | sizeof(struct btrfs_key_ptr) * |
| 3169 | (nritems - slot - 1)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3170 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3171 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3172 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3173 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3174 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3175 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3176 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3177 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3178 | struct btrfs_disk_key disk_key; |
| 3179 | |
| 3180 | btrfs_node_key(parent, &disk_key, 0); |
| 3181 | wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 3182 | if (wret) |
| 3183 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3184 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3185 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3186 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3187 | } |
| 3188 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3189 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3190 | * a helper function to delete the leaf pointed to by path->slots[1] and |
| 3191 | * path->nodes[1]. bytenr is the node block pointer, but since the callers |
| 3192 | * already know it, it is faster to have them pass it down than to |
| 3193 | * read it out of the node again. |
| 3194 | * |
| 3195 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 3196 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 3197 | * |
| 3198 | * The path must have already been setup for deleting the leaf, including |
| 3199 | * all the proper balancing. path->nodes[1] must be locked. |
| 3200 | */ |
| 3201 | noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 3202 | struct btrfs_root *root, |
| 3203 | struct btrfs_path *path, u64 bytenr) |
| 3204 | { |
| 3205 | int ret; |
| 3206 | u64 root_gen = btrfs_header_generation(path->nodes[1]); |
| 3207 | |
| 3208 | ret = del_ptr(trans, root, path, 1, path->slots[1]); |
| 3209 | if (ret) |
| 3210 | return ret; |
| 3211 | |
| 3212 | ret = btrfs_free_extent(trans, root, bytenr, |
| 3213 | btrfs_level_size(root, 0), |
| 3214 | path->nodes[1]->start, |
| 3215 | btrfs_header_owner(path->nodes[1]), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3216 | root_gen, 0, 1); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3217 | return ret; |
| 3218 | } |
| 3219 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3220 | * delete the item at the leaf level in path. If that empties |
| 3221 | * the leaf, remove it from the tree |
| 3222 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3223 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 3224 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3225 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3226 | struct extent_buffer *leaf; |
| 3227 | struct btrfs_item *item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3228 | int last_off; |
| 3229 | int dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3230 | int ret = 0; |
| 3231 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3232 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3233 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3234 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3235 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3236 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 3237 | |
| 3238 | for (i = 0; i < nr; i++) |
| 3239 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 3240 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3241 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3242 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3243 | if (slot + nr != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3244 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3245 | |
| 3246 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3247 | data_end + dsize, |
| 3248 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3249 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3250 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3251 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3252 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3253 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3254 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3255 | if (!leaf->map_token) { |
| 3256 | map_extent_buffer(leaf, (unsigned long)item, |
| 3257 | sizeof(struct btrfs_item), |
| 3258 | &leaf->map_token, &leaf->kaddr, |
| 3259 | &leaf->map_start, &leaf->map_len, |
| 3260 | KM_USER1); |
| 3261 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3262 | ioff = btrfs_item_offset(leaf, item); |
| 3263 | btrfs_set_item_offset(leaf, item, ioff + dsize); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3264 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3265 | |
| 3266 | if (leaf->map_token) { |
| 3267 | unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); |
| 3268 | leaf->map_token = NULL; |
| 3269 | } |
| 3270 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3271 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3272 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3273 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3274 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3275 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3276 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 3277 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3278 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3279 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3280 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3281 | if (leaf == root->node) { |
| 3282 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 3283 | } else { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3284 | ret = btrfs_del_leaf(trans, root, path, leaf->start); |
| 3285 | BUG_ON(ret); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 3286 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3287 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3288 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3289 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3290 | struct btrfs_disk_key disk_key; |
| 3291 | |
| 3292 | btrfs_item_key(leaf, &disk_key, 0); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3293 | wret = fixup_low_keys(trans, root, path, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3294 | &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3295 | if (wret) |
| 3296 | ret = wret; |
| 3297 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3298 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3299 | /* delete the leaf if it is mostly empty */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3300 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3301 | /* push_leaf_left fixes the path. |
| 3302 | * make sure the path still points to our leaf |
| 3303 | * for possible call to del_ptr below |
| 3304 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 3305 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3306 | extent_buffer_get(leaf); |
| 3307 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3308 | wret = push_leaf_left(trans, root, path, 1, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3309 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3310 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3311 | |
| 3312 | if (path->nodes[0] == leaf && |
| 3313 | btrfs_header_nritems(leaf)) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3314 | wret = push_leaf_right(trans, root, path, 1, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3315 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3316 | ret = wret; |
| 3317 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3318 | |
| 3319 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3320 | path->slots[1] = slot; |
| 3321 | ret = btrfs_del_leaf(trans, root, path, leaf->start); |
| 3322 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3323 | free_extent_buffer(leaf); |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3324 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3325 | /* if we're still in the path, make sure |
| 3326 | * we're dirty. Otherwise, one of the |
| 3327 | * push_leaf functions must have already |
| 3328 | * dirtied this buffer |
| 3329 | */ |
| 3330 | if (path->nodes[0] == leaf) |
| 3331 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3332 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3333 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3334 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3335 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3336 | } |
| 3337 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3338 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3339 | } |
| 3340 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3341 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3342 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3343 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 3344 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3345 | * |
| 3346 | * This may release the path, and so you may lose any locks held at the |
| 3347 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3348 | */ |
| 3349 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
| 3350 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3351 | struct btrfs_key key; |
| 3352 | struct btrfs_disk_key found_key; |
| 3353 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3354 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3355 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3356 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3357 | if (key.offset > 0) |
| 3358 | key.offset--; |
| 3359 | else if (key.type > 0) |
| 3360 | key.type--; |
| 3361 | else if (key.objectid > 0) |
| 3362 | key.objectid--; |
| 3363 | else |
| 3364 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3365 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3366 | btrfs_release_path(root, path); |
| 3367 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3368 | if (ret < 0) |
| 3369 | return ret; |
| 3370 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 3371 | ret = comp_keys(&found_key, &key); |
| 3372 | if (ret < 0) |
| 3373 | return 0; |
| 3374 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3375 | } |
| 3376 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3377 | /* |
| 3378 | * A helper function to walk down the tree starting at min_key, and looking |
| 3379 | * for nodes or leaves that are either in cache or have a minimum |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3380 | * transaction id. This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3381 | * |
| 3382 | * This does not cow, but it does stuff the starting key it finds back |
| 3383 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 3384 | * key and get a writable path. |
| 3385 | * |
| 3386 | * This does lock as it descends, and path->keep_locks should be set |
| 3387 | * to 1 by the caller. |
| 3388 | * |
| 3389 | * This honors path->lowest_level to prevent descent past a given level |
| 3390 | * of the tree. |
| 3391 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3392 | * min_trans indicates the oldest transaction that you are interested |
| 3393 | * in walking through. Any nodes or leaves older than min_trans are |
| 3394 | * skipped over (without reading them). |
| 3395 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3396 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 3397 | * was nothing in the tree that matched the search criteria. |
| 3398 | */ |
| 3399 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3400 | struct btrfs_key *max_key, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3401 | struct btrfs_path *path, int cache_only, |
| 3402 | u64 min_trans) |
| 3403 | { |
| 3404 | struct extent_buffer *cur; |
| 3405 | struct btrfs_key found_key; |
| 3406 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 3407 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3408 | u32 nritems; |
| 3409 | int level; |
| 3410 | int ret = 1; |
| 3411 | |
| 3412 | again: |
| 3413 | cur = btrfs_lock_root_node(root); |
| 3414 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3415 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3416 | path->nodes[level] = cur; |
| 3417 | path->locks[level] = 1; |
| 3418 | |
| 3419 | if (btrfs_header_generation(cur) < min_trans) { |
| 3420 | ret = 1; |
| 3421 | goto out; |
| 3422 | } |
| 3423 | while(1) { |
| 3424 | nritems = btrfs_header_nritems(cur); |
| 3425 | level = btrfs_header_level(cur); |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 3426 | sret = bin_search(cur, min_key, level, &slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3427 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3428 | /* at the lowest level, we're done, setup the path and exit */ |
| 3429 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3430 | if (slot >= nritems) |
| 3431 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3432 | ret = 0; |
| 3433 | path->slots[level] = slot; |
| 3434 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 3435 | goto out; |
| 3436 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 3437 | if (sret && slot > 0) |
| 3438 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3439 | /* |
| 3440 | * check this node pointer against the cache_only and |
| 3441 | * min_trans parameters. If it isn't in cache or is too |
| 3442 | * old, skip to the next one. |
| 3443 | */ |
| 3444 | while(slot < nritems) { |
| 3445 | u64 blockptr; |
| 3446 | u64 gen; |
| 3447 | struct extent_buffer *tmp; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3448 | struct btrfs_disk_key disk_key; |
| 3449 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3450 | blockptr = btrfs_node_blockptr(cur, slot); |
| 3451 | gen = btrfs_node_ptr_generation(cur, slot); |
| 3452 | if (gen < min_trans) { |
| 3453 | slot++; |
| 3454 | continue; |
| 3455 | } |
| 3456 | if (!cache_only) |
| 3457 | break; |
| 3458 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3459 | if (max_key) { |
| 3460 | btrfs_node_key(cur, &disk_key, slot); |
| 3461 | if (comp_keys(&disk_key, max_key) >= 0) { |
| 3462 | ret = 1; |
| 3463 | goto out; |
| 3464 | } |
| 3465 | } |
| 3466 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3467 | tmp = btrfs_find_tree_block(root, blockptr, |
| 3468 | btrfs_level_size(root, level - 1)); |
| 3469 | |
| 3470 | if (tmp && btrfs_buffer_uptodate(tmp, gen)) { |
| 3471 | free_extent_buffer(tmp); |
| 3472 | break; |
| 3473 | } |
| 3474 | if (tmp) |
| 3475 | free_extent_buffer(tmp); |
| 3476 | slot++; |
| 3477 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3478 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3479 | /* |
| 3480 | * we didn't find a candidate key in this node, walk forward |
| 3481 | * and find another one |
| 3482 | */ |
| 3483 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3484 | path->slots[level] = slot; |
| 3485 | sret = btrfs_find_next_key(root, path, min_key, level, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3486 | cache_only, min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3487 | if (sret == 0) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3488 | btrfs_release_path(root, path); |
| 3489 | goto again; |
| 3490 | } else { |
| 3491 | goto out; |
| 3492 | } |
| 3493 | } |
| 3494 | /* save our key for returning back */ |
| 3495 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 3496 | path->slots[level] = slot; |
| 3497 | if (level == path->lowest_level) { |
| 3498 | ret = 0; |
| 3499 | unlock_up(path, level, 1); |
| 3500 | goto out; |
| 3501 | } |
| 3502 | cur = read_node_slot(root, cur, slot); |
| 3503 | |
| 3504 | btrfs_tree_lock(cur); |
| 3505 | path->locks[level - 1] = 1; |
| 3506 | path->nodes[level - 1] = cur; |
| 3507 | unlock_up(path, level, 1); |
| 3508 | } |
| 3509 | out: |
| 3510 | if (ret == 0) |
| 3511 | memcpy(min_key, &found_key, sizeof(found_key)); |
| 3512 | return ret; |
| 3513 | } |
| 3514 | |
| 3515 | /* |
| 3516 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 3517 | * and fixup the path. It looks for and returns the next key in the |
| 3518 | * tree based on the current path and the cache_only and min_trans |
| 3519 | * parameters. |
| 3520 | * |
| 3521 | * 0 is returned if another key is found, < 0 if there are any errors |
| 3522 | * and 1 is returned if there are no higher keys in the tree |
| 3523 | * |
| 3524 | * path->keep_locks should be set to 1 on the search made before |
| 3525 | * calling this function. |
| 3526 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3527 | int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3528 | struct btrfs_key *key, int lowest_level, |
| 3529 | int cache_only, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3530 | { |
| 3531 | int level = lowest_level; |
| 3532 | int slot; |
| 3533 | struct extent_buffer *c; |
| 3534 | |
| 3535 | while(level < BTRFS_MAX_LEVEL) { |
| 3536 | if (!path->nodes[level]) |
| 3537 | return 1; |
| 3538 | |
| 3539 | slot = path->slots[level] + 1; |
| 3540 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3541 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3542 | if (slot >= btrfs_header_nritems(c)) { |
| 3543 | level++; |
| 3544 | if (level == BTRFS_MAX_LEVEL) { |
| 3545 | return 1; |
| 3546 | } |
| 3547 | continue; |
| 3548 | } |
| 3549 | if (level == 0) |
| 3550 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3551 | else { |
| 3552 | u64 blockptr = btrfs_node_blockptr(c, slot); |
| 3553 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 3554 | |
| 3555 | if (cache_only) { |
| 3556 | struct extent_buffer *cur; |
| 3557 | cur = btrfs_find_tree_block(root, blockptr, |
| 3558 | btrfs_level_size(root, level - 1)); |
| 3559 | if (!cur || !btrfs_buffer_uptodate(cur, gen)) { |
| 3560 | slot++; |
| 3561 | if (cur) |
| 3562 | free_extent_buffer(cur); |
| 3563 | goto next; |
| 3564 | } |
| 3565 | free_extent_buffer(cur); |
| 3566 | } |
| 3567 | if (gen < min_trans) { |
| 3568 | slot++; |
| 3569 | goto next; |
| 3570 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3571 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3572 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3573 | return 0; |
| 3574 | } |
| 3575 | return 1; |
| 3576 | } |
| 3577 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3578 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3579 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 3580 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 3581 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3582 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 3583 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3584 | { |
| 3585 | int slot; |
| 3586 | int level = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3587 | struct extent_buffer *c; |
| 3588 | struct extent_buffer *next = NULL; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3589 | struct btrfs_key key; |
| 3590 | u32 nritems; |
| 3591 | int ret; |
| 3592 | |
| 3593 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 3594 | if (nritems == 0) { |
| 3595 | return 1; |
| 3596 | } |
| 3597 | |
| 3598 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 3599 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3600 | btrfs_release_path(root, path); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3601 | path->keep_locks = 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3602 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3603 | path->keep_locks = 0; |
| 3604 | |
| 3605 | if (ret < 0) |
| 3606 | return ret; |
| 3607 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3608 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3609 | /* |
| 3610 | * by releasing the path above we dropped all our locks. A balance |
| 3611 | * could have added more items next to the key that used to be |
| 3612 | * at the very end of the block. So, check again here and |
| 3613 | * advance the path if there are now more items available. |
| 3614 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3615 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3616 | path->slots[0]++; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3617 | goto done; |
| 3618 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3619 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 3620 | while(level < BTRFS_MAX_LEVEL) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3621 | if (!path->nodes[level]) |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 3622 | return 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3623 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3624 | slot = path->slots[level] + 1; |
| 3625 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3626 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3627 | level++; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3628 | if (level == BTRFS_MAX_LEVEL) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3629 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3630 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3631 | continue; |
| 3632 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3633 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3634 | if (next) { |
| 3635 | btrfs_tree_unlock(next); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3636 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3637 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3638 | |
Chris Mason | 0bd40a7 | 2008-07-17 12:54:43 -0400 | [diff] [blame] | 3639 | if (level == 1 && (path->locks[1] || path->skip_locking) && |
| 3640 | path->reada) |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 3641 | reada_for_search(root, path, level, slot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3642 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3643 | next = read_node_slot(root, c, slot); |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3644 | if (!path->skip_locking) { |
| 3645 | WARN_ON(!btrfs_tree_locked(c)); |
| 3646 | btrfs_tree_lock(next); |
| 3647 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3648 | break; |
| 3649 | } |
| 3650 | path->slots[level] = slot; |
| 3651 | while(1) { |
| 3652 | level--; |
| 3653 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3654 | if (path->locks[level]) |
| 3655 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3657 | path->nodes[level] = next; |
| 3658 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3659 | if (!path->skip_locking) |
| 3660 | path->locks[level] = 1; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3661 | if (!level) |
| 3662 | break; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3663 | if (level == 1 && path->locks[1] && path->reada) |
| 3664 | reada_for_search(root, path, level, slot, 0); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3665 | next = read_node_slot(root, next, 0); |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3666 | if (!path->skip_locking) { |
| 3667 | WARN_ON(!btrfs_tree_locked(path->nodes[level])); |
| 3668 | btrfs_tree_lock(next); |
| 3669 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3670 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3671 | done: |
| 3672 | unlock_up(path, 0, 1); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 3673 | return 0; |
| 3674 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3675 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3676 | /* |
| 3677 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 3678 | * searching until it gets past min_objectid or finds an item of 'type' |
| 3679 | * |
| 3680 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 3681 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3682 | int btrfs_previous_item(struct btrfs_root *root, |
| 3683 | struct btrfs_path *path, u64 min_objectid, |
| 3684 | int type) |
| 3685 | { |
| 3686 | struct btrfs_key found_key; |
| 3687 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3688 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3689 | int ret; |
| 3690 | |
| 3691 | while(1) { |
| 3692 | if (path->slots[0] == 0) { |
| 3693 | ret = btrfs_prev_leaf(root, path); |
| 3694 | if (ret != 0) |
| 3695 | return ret; |
| 3696 | } else { |
| 3697 | path->slots[0]--; |
| 3698 | } |
| 3699 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3700 | nritems = btrfs_header_nritems(leaf); |
| 3701 | if (nritems == 0) |
| 3702 | return 1; |
| 3703 | if (path->slots[0] == nritems) |
| 3704 | path->slots[0]--; |
| 3705 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3706 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3707 | if (found_key.type == type) |
| 3708 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3709 | if (found_key.objectid < min_objectid) |
| 3710 | break; |
| 3711 | if (found_key.objectid == min_objectid && |
| 3712 | found_key.type < type) |
| 3713 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3714 | } |
| 3715 | return 1; |
| 3716 | } |