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