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