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