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