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