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