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