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