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