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