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> |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 21 | #include <linux/rbtree.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "disk-io.h" |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 24 | #include "transaction.h" |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 25 | #include "print-tree.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 26 | #include "locking.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 27 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 28 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 29 | *root, struct btrfs_path *path, int level); |
| 30 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 31 | *root, struct btrfs_key *ins_key, |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 32 | struct btrfs_path *path, int data_size, int extend); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 33 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 34 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 35 | struct extent_buffer *src, int empty); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 36 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 37 | struct btrfs_root *root, |
| 38 | struct extent_buffer *dst_buf, |
| 39 | struct extent_buffer *src_buf); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 40 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 41 | int level, int slot); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 42 | static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, |
| 43 | struct extent_buffer *eb); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 44 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 45 | struct btrfs_path *btrfs_alloc_path(void) |
| 46 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 47 | struct btrfs_path *path; |
Jeff Mahoney | e00f730 | 2009-02-12 14:11:25 -0500 | [diff] [blame] | 48 | path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 49 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 52 | /* |
| 53 | * set all locked nodes in the path to blocking locks. This should |
| 54 | * be done before scheduling |
| 55 | */ |
| 56 | noinline void btrfs_set_path_blocking(struct btrfs_path *p) |
| 57 | { |
| 58 | int i; |
| 59 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 60 | if (!p->nodes[i] || !p->locks[i]) |
| 61 | continue; |
| 62 | btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 63 | if (p->locks[i] == BTRFS_READ_LOCK) |
| 64 | p->locks[i] = BTRFS_READ_LOCK_BLOCKING; |
| 65 | else if (p->locks[i] == BTRFS_WRITE_LOCK) |
| 66 | p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * reset all the locked nodes in the patch to spinning locks. |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 72 | * |
| 73 | * held is used to keep lockdep happy, when lockdep is enabled |
| 74 | * we set held to a blocking lock before we go around and |
| 75 | * retake all the spinlocks in the path. You can safely use NULL |
| 76 | * for held |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 77 | */ |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 78 | noinline void btrfs_clear_path_blocking(struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 79 | struct extent_buffer *held, int held_rw) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 80 | { |
| 81 | int i; |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 82 | |
| 83 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 84 | /* lockdep really cares that we take all of these spinlocks |
| 85 | * in the right order. If any of the locks in the path are not |
| 86 | * currently blocking, it is going to complain. So, make really |
| 87 | * really sure by forcing the path to blocking before we clear |
| 88 | * the path blocking. |
| 89 | */ |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 90 | if (held) { |
| 91 | btrfs_set_lock_blocking_rw(held, held_rw); |
| 92 | if (held_rw == BTRFS_WRITE_LOCK) |
| 93 | held_rw = BTRFS_WRITE_LOCK_BLOCKING; |
| 94 | else if (held_rw == BTRFS_READ_LOCK) |
| 95 | held_rw = BTRFS_READ_LOCK_BLOCKING; |
| 96 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 97 | btrfs_set_path_blocking(p); |
| 98 | #endif |
| 99 | |
| 100 | for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 101 | if (p->nodes[i] && p->locks[i]) { |
| 102 | btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 103 | if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING) |
| 104 | p->locks[i] = BTRFS_WRITE_LOCK; |
| 105 | else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING) |
| 106 | p->locks[i] = BTRFS_READ_LOCK; |
| 107 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 108 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 109 | |
| 110 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 111 | if (held) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 112 | btrfs_clear_lock_blocking_rw(held, held_rw); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 113 | #endif |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 116 | /* this also releases the path */ |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 117 | void btrfs_free_path(struct btrfs_path *p) |
| 118 | { |
Jesper Juhl | ff175d5 | 2010-12-25 21:22:30 +0000 | [diff] [blame] | 119 | if (!p) |
| 120 | return; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 121 | btrfs_release_path(p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 122 | kmem_cache_free(btrfs_path_cachep, p); |
| 123 | } |
| 124 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 125 | /* |
| 126 | * path release drops references on the extent buffers in the path |
| 127 | * and it drops any locks held by this path |
| 128 | * |
| 129 | * It is safe to call this on paths that no locks or extent buffers held. |
| 130 | */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 131 | noinline void btrfs_release_path(struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 132 | { |
| 133 | int i; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 134 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 135 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 136 | p->slots[i] = 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 137 | if (!p->nodes[i]) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 138 | continue; |
| 139 | if (p->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 140 | btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 141 | p->locks[i] = 0; |
| 142 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 143 | free_extent_buffer(p->nodes[i]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 144 | p->nodes[i] = NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 148 | /* |
| 149 | * safely gets a reference on the root node of a tree. A lock |
| 150 | * is not taken, so a concurrent writer may put a different node |
| 151 | * at the root of the tree. See btrfs_lock_root_node for the |
| 152 | * looping required. |
| 153 | * |
| 154 | * The extent buffer returned by this has a reference taken, so |
| 155 | * it won't disappear. It may stop being the root of the tree |
| 156 | * at any time because there are no locks held. |
| 157 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 158 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
| 159 | { |
| 160 | struct extent_buffer *eb; |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 161 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 162 | while (1) { |
| 163 | rcu_read_lock(); |
| 164 | eb = rcu_dereference(root->node); |
| 165 | |
| 166 | /* |
| 167 | * RCU really hurts here, we could free up the root node because |
| 168 | * it was cow'ed but we may not get the new root node yet so do |
| 169 | * the inc_not_zero dance and if it doesn't work then |
| 170 | * synchronize_rcu and try again. |
| 171 | */ |
| 172 | if (atomic_inc_not_zero(&eb->refs)) { |
| 173 | rcu_read_unlock(); |
| 174 | break; |
| 175 | } |
| 176 | rcu_read_unlock(); |
| 177 | synchronize_rcu(); |
| 178 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 179 | return eb; |
| 180 | } |
| 181 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 182 | /* loop around taking references on and locking the root node of the |
| 183 | * tree until you end up with a lock on the root. A locked buffer |
| 184 | * is returned, with a reference held. |
| 185 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 186 | struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) |
| 187 | { |
| 188 | struct extent_buffer *eb; |
| 189 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 190 | while (1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 191 | eb = btrfs_root_node(root); |
| 192 | btrfs_tree_lock(eb); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 193 | if (eb == root->node) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 194 | break; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 195 | btrfs_tree_unlock(eb); |
| 196 | free_extent_buffer(eb); |
| 197 | } |
| 198 | return eb; |
| 199 | } |
| 200 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 201 | /* loop around taking references on and locking the root node of the |
| 202 | * tree until you end up with a lock on the root. A locked buffer |
| 203 | * is returned, with a reference held. |
| 204 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 205 | static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 206 | { |
| 207 | struct extent_buffer *eb; |
| 208 | |
| 209 | while (1) { |
| 210 | eb = btrfs_root_node(root); |
| 211 | btrfs_tree_read_lock(eb); |
| 212 | if (eb == root->node) |
| 213 | break; |
| 214 | btrfs_tree_read_unlock(eb); |
| 215 | free_extent_buffer(eb); |
| 216 | } |
| 217 | return eb; |
| 218 | } |
| 219 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 220 | /* cowonly root (everything not a reference counted cow subvolume), just get |
| 221 | * put onto a simple dirty list. transaction.c walks this to make sure they |
| 222 | * get properly updated on disk. |
| 223 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 224 | static void add_root_to_dirty_list(struct btrfs_root *root) |
| 225 | { |
Chris Mason | e5846fc | 2012-05-03 12:08:48 -0400 | [diff] [blame] | 226 | spin_lock(&root->fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 227 | if (root->track_dirty && list_empty(&root->dirty_list)) { |
| 228 | list_add(&root->dirty_list, |
| 229 | &root->fs_info->dirty_cowonly_roots); |
| 230 | } |
Chris Mason | e5846fc | 2012-05-03 12:08:48 -0400 | [diff] [blame] | 231 | spin_unlock(&root->fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 234 | /* |
| 235 | * used by snapshot creation to make a copy of a root for a tree with |
| 236 | * a given objectid. The buffer with the new root node is returned in |
| 237 | * cow_ret, and this func returns zero on success or a negative error code. |
| 238 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 239 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
| 240 | struct btrfs_root *root, |
| 241 | struct extent_buffer *buf, |
| 242 | struct extent_buffer **cow_ret, u64 new_root_objectid) |
| 243 | { |
| 244 | struct extent_buffer *cow; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 245 | int ret = 0; |
| 246 | int level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 247 | struct btrfs_disk_key disk_key; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 248 | |
| 249 | WARN_ON(root->ref_cows && trans->transid != |
| 250 | root->fs_info->running_transaction->transid); |
| 251 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
| 252 | |
| 253 | level = btrfs_header_level(buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 254 | if (level == 0) |
| 255 | btrfs_item_key(buf, &disk_key, 0); |
| 256 | else |
| 257 | btrfs_node_key(buf, &disk_key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 258 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 259 | cow = btrfs_alloc_free_block(trans, root, buf->len, 0, |
| 260 | new_root_objectid, &disk_key, level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 261 | buf->start, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 262 | if (IS_ERR(cow)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 263 | return PTR_ERR(cow); |
| 264 | |
| 265 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
| 266 | btrfs_set_header_bytenr(cow, cow->start); |
| 267 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 268 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 269 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 270 | BTRFS_HEADER_FLAG_RELOC); |
| 271 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 272 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 273 | else |
| 274 | btrfs_set_header_owner(cow, new_root_objectid); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 275 | |
Ross Kirk | 0a4e558 | 2013-09-24 10:12:38 +0100 | [diff] [blame] | 276 | write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 277 | BTRFS_FSID_SIZE); |
| 278 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 279 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 280 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 281 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 282 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 283 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 284 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | btrfs_mark_buffer_dirty(cow); |
| 289 | *cow_ret = cow; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 293 | enum mod_log_op { |
| 294 | MOD_LOG_KEY_REPLACE, |
| 295 | MOD_LOG_KEY_ADD, |
| 296 | MOD_LOG_KEY_REMOVE, |
| 297 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, |
| 298 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, |
| 299 | MOD_LOG_MOVE_KEYS, |
| 300 | MOD_LOG_ROOT_REPLACE, |
| 301 | }; |
| 302 | |
| 303 | struct tree_mod_move { |
| 304 | int dst_slot; |
| 305 | int nr_items; |
| 306 | }; |
| 307 | |
| 308 | struct tree_mod_root { |
| 309 | u64 logical; |
| 310 | u8 level; |
| 311 | }; |
| 312 | |
| 313 | struct tree_mod_elem { |
| 314 | struct rb_node node; |
| 315 | u64 index; /* shifted logical */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 316 | u64 seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 317 | enum mod_log_op op; |
| 318 | |
| 319 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ |
| 320 | int slot; |
| 321 | |
| 322 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ |
| 323 | u64 generation; |
| 324 | |
| 325 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ |
| 326 | struct btrfs_disk_key key; |
| 327 | u64 blockptr; |
| 328 | |
| 329 | /* this is used for op == MOD_LOG_MOVE_KEYS */ |
| 330 | struct tree_mod_move move; |
| 331 | |
| 332 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ |
| 333 | struct tree_mod_root old_root; |
| 334 | }; |
| 335 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 336 | static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 337 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 338 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 341 | static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 342 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 343 | read_unlock(&fs_info->tree_mod_log_lock); |
| 344 | } |
| 345 | |
| 346 | static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info) |
| 347 | { |
| 348 | write_lock(&fs_info->tree_mod_log_lock); |
| 349 | } |
| 350 | |
| 351 | static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info) |
| 352 | { |
| 353 | write_unlock(&fs_info->tree_mod_log_lock); |
| 354 | } |
| 355 | |
| 356 | /* |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 357 | * Increment the upper half of tree_mod_seq, set lower half zero. |
| 358 | * |
| 359 | * Must be called with fs_info->tree_mod_seq_lock held. |
| 360 | */ |
| 361 | static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info) |
| 362 | { |
| 363 | u64 seq = atomic64_read(&fs_info->tree_mod_seq); |
| 364 | seq &= 0xffffffff00000000ull; |
| 365 | seq += 1ull << 32; |
| 366 | atomic64_set(&fs_info->tree_mod_seq, seq); |
| 367 | return seq; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Increment the lower half of tree_mod_seq. |
| 372 | * |
| 373 | * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers |
| 374 | * are generated should not technically require a spin lock here. (Rationale: |
| 375 | * incrementing the minor while incrementing the major seq number is between its |
| 376 | * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it |
| 377 | * just returns a unique sequence number as usual.) We have decided to leave |
| 378 | * that requirement in here and rethink it once we notice it really imposes a |
| 379 | * problem on some workload. |
| 380 | */ |
| 381 | static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info) |
| 382 | { |
| 383 | return atomic64_inc_return(&fs_info->tree_mod_seq); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * return the last minor in the previous major tree_mod_seq number |
| 388 | */ |
| 389 | u64 btrfs_tree_mod_seq_prev(u64 seq) |
| 390 | { |
| 391 | return (seq & 0xffffffff00000000ull) - 1ull; |
| 392 | } |
| 393 | |
| 394 | /* |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 395 | * This adds a new blocker to the tree mod log's blocker list if the @elem |
| 396 | * passed does not already have a sequence number set. So when a caller expects |
| 397 | * to record tree modifications, it should ensure to set elem->seq to zero |
| 398 | * before calling btrfs_get_tree_mod_seq. |
| 399 | * Returns a fresh, unused tree log modification sequence number, even if no new |
| 400 | * blocker was added. |
| 401 | */ |
| 402 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 403 | struct seq_list *elem) |
| 404 | { |
| 405 | u64 seq; |
| 406 | |
| 407 | tree_mod_log_write_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 408 | spin_lock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 409 | if (!elem->seq) { |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 410 | elem->seq = btrfs_inc_tree_mod_seq_major(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 411 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 412 | } |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 413 | seq = btrfs_inc_tree_mod_seq_minor(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 414 | spin_unlock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 415 | tree_mod_log_write_unlock(fs_info); |
| 416 | |
| 417 | return seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 421 | struct seq_list *elem) |
| 422 | { |
| 423 | struct rb_root *tm_root; |
| 424 | struct rb_node *node; |
| 425 | struct rb_node *next; |
| 426 | struct seq_list *cur_elem; |
| 427 | struct tree_mod_elem *tm; |
| 428 | u64 min_seq = (u64)-1; |
| 429 | u64 seq_putting = elem->seq; |
| 430 | |
| 431 | if (!seq_putting) |
| 432 | return; |
| 433 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 434 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 435 | list_del(&elem->list); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 436 | elem->seq = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 437 | |
| 438 | list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 439 | if (cur_elem->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 440 | if (seq_putting > cur_elem->seq) { |
| 441 | /* |
| 442 | * blocker with lower sequence number exists, we |
| 443 | * cannot remove anything from the log |
| 444 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 445 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 446 | return; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 447 | } |
| 448 | min_seq = cur_elem->seq; |
| 449 | } |
| 450 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 451 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 452 | |
| 453 | /* |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 454 | * anything that's lower than the lowest existing (read: blocked) |
| 455 | * sequence number can be removed from the tree. |
| 456 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 457 | tree_mod_log_write_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 458 | tm_root = &fs_info->tree_mod_log; |
| 459 | for (node = rb_first(tm_root); node; node = next) { |
| 460 | next = rb_next(node); |
| 461 | tm = container_of(node, struct tree_mod_elem, node); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 462 | if (tm->seq > min_seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 463 | continue; |
| 464 | rb_erase(node, tm_root); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 465 | kfree(tm); |
| 466 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 467 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
| 471 | * key order of the log: |
| 472 | * index -> sequence |
| 473 | * |
| 474 | * the index is the shifted logical of the *new* root node for root replace |
| 475 | * operations, or the shifted logical of the affected block for all other |
| 476 | * operations. |
| 477 | */ |
| 478 | static noinline int |
| 479 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 480 | { |
| 481 | struct rb_root *tm_root; |
| 482 | struct rb_node **new; |
| 483 | struct rb_node *parent = NULL; |
| 484 | struct tree_mod_elem *cur; |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 485 | int ret = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 486 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 487 | BUG_ON(!tm); |
| 488 | |
| 489 | tree_mod_log_write_lock(fs_info); |
| 490 | if (list_empty(&fs_info->tree_mod_seq_list)) { |
| 491 | tree_mod_log_write_unlock(fs_info); |
| 492 | /* |
| 493 | * Ok we no longer care about logging modifications, free up tm |
| 494 | * and return 0. Any callers shouldn't be using tm after |
| 495 | * calling tree_mod_log_insert, but if they do we can just |
| 496 | * change this to return a special error code to let the callers |
| 497 | * do their own thing. |
| 498 | */ |
| 499 | kfree(tm); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 504 | tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info); |
| 505 | spin_unlock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 506 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 507 | tm_root = &fs_info->tree_mod_log; |
| 508 | new = &tm_root->rb_node; |
| 509 | while (*new) { |
| 510 | cur = container_of(*new, struct tree_mod_elem, node); |
| 511 | parent = *new; |
| 512 | if (cur->index < tm->index) |
| 513 | new = &((*new)->rb_left); |
| 514 | else if (cur->index > tm->index) |
| 515 | new = &((*new)->rb_right); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 516 | else if (cur->seq < tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 517 | new = &((*new)->rb_left); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 518 | else if (cur->seq > tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 519 | new = &((*new)->rb_right); |
| 520 | else { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 521 | ret = -EEXIST; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 522 | kfree(tm); |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 523 | goto out; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | rb_link_node(&tm->node, parent, new); |
| 528 | rb_insert_color(&tm->node, tm_root); |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 529 | out: |
| 530 | tree_mod_log_write_unlock(fs_info); |
| 531 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 534 | /* |
| 535 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it |
| 536 | * returns zero with the tree_mod_log_lock acquired. The caller must hold |
| 537 | * this until all tree mod log insertions are recorded in the rb tree and then |
| 538 | * call tree_mod_log_write_unlock() to release. |
| 539 | */ |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 540 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 541 | struct extent_buffer *eb) { |
| 542 | smp_mb(); |
| 543 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 544 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 545 | if (eb && btrfs_header_level(eb) == 0) |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 546 | return 1; |
| 547 | return 0; |
| 548 | } |
| 549 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 550 | static inline int |
| 551 | __tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, |
| 552 | struct extent_buffer *eb, int slot, |
| 553 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 554 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 555 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 556 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 557 | tm = kzalloc(sizeof(*tm), flags); |
| 558 | if (!tm) |
| 559 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 560 | |
| 561 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 562 | if (op != MOD_LOG_KEY_ADD) { |
| 563 | btrfs_node_key(eb, &tm->key, slot); |
| 564 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 565 | } |
| 566 | tm->op = op; |
| 567 | tm->slot = slot; |
| 568 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
| 569 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 570 | return __tree_mod_log_insert(fs_info, tm); |
| 571 | } |
| 572 | |
| 573 | static noinline int |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 574 | tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, |
| 575 | struct extent_buffer *eb, int slot, |
| 576 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 577 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 578 | if (tree_mod_dont_log(fs_info, eb)) |
| 579 | return 0; |
| 580 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 581 | return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static noinline int |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 585 | tree_mod_log_insert_move(struct btrfs_fs_info *fs_info, |
| 586 | struct extent_buffer *eb, int dst_slot, int src_slot, |
| 587 | int nr_items, gfp_t flags) |
| 588 | { |
| 589 | struct tree_mod_elem *tm; |
| 590 | int ret; |
| 591 | int i; |
| 592 | |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 593 | if (tree_mod_dont_log(fs_info, eb)) |
| 594 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 595 | |
Jan Schmidt | 01763a2 | 2012-10-23 15:02:12 +0200 | [diff] [blame] | 596 | /* |
| 597 | * When we override something during the move, we log these removals. |
| 598 | * This can only happen when we move towards the beginning of the |
| 599 | * buffer, i.e. dst_slot < src_slot. |
| 600 | */ |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 601 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 602 | ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot, |
| 603 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 604 | BUG_ON(ret < 0); |
| 605 | } |
| 606 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 607 | tm = kzalloc(sizeof(*tm), flags); |
| 608 | if (!tm) |
| 609 | return -ENOMEM; |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 610 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 611 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 612 | tm->slot = src_slot; |
| 613 | tm->move.dst_slot = dst_slot; |
| 614 | tm->move.nr_items = nr_items; |
| 615 | tm->op = MOD_LOG_MOVE_KEYS; |
| 616 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 617 | return __tree_mod_log_insert(fs_info, tm); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 620 | static inline void |
| 621 | __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb) |
| 622 | { |
| 623 | int i; |
| 624 | u32 nritems; |
| 625 | int ret; |
| 626 | |
Chris Mason | b12a3b1 | 2012-08-07 15:34:49 -0400 | [diff] [blame] | 627 | if (btrfs_header_level(eb) == 0) |
| 628 | return; |
| 629 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 630 | nritems = btrfs_header_nritems(eb); |
| 631 | for (i = nritems - 1; i >= 0; i--) { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 632 | ret = __tree_mod_log_insert_key(fs_info, eb, i, |
| 633 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 634 | BUG_ON(ret < 0); |
| 635 | } |
| 636 | } |
| 637 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 638 | static noinline int |
| 639 | tree_mod_log_insert_root(struct btrfs_fs_info *fs_info, |
| 640 | struct extent_buffer *old_root, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 641 | struct extent_buffer *new_root, gfp_t flags, |
| 642 | int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 643 | { |
| 644 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 645 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 646 | if (tree_mod_dont_log(fs_info, NULL)) |
| 647 | return 0; |
| 648 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 649 | if (log_removal) |
| 650 | __tree_mod_log_free_eb(fs_info, old_root); |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 651 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 652 | tm = kzalloc(sizeof(*tm), flags); |
| 653 | if (!tm) |
| 654 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 655 | |
| 656 | tm->index = new_root->start >> PAGE_CACHE_SHIFT; |
| 657 | tm->old_root.logical = old_root->start; |
| 658 | tm->old_root.level = btrfs_header_level(old_root); |
| 659 | tm->generation = btrfs_header_generation(old_root); |
| 660 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 661 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 662 | return __tree_mod_log_insert(fs_info, tm); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | static struct tree_mod_elem * |
| 666 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 667 | int smallest) |
| 668 | { |
| 669 | struct rb_root *tm_root; |
| 670 | struct rb_node *node; |
| 671 | struct tree_mod_elem *cur = NULL; |
| 672 | struct tree_mod_elem *found = NULL; |
| 673 | u64 index = start >> PAGE_CACHE_SHIFT; |
| 674 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 675 | tree_mod_log_read_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 676 | tm_root = &fs_info->tree_mod_log; |
| 677 | node = tm_root->rb_node; |
| 678 | while (node) { |
| 679 | cur = container_of(node, struct tree_mod_elem, node); |
| 680 | if (cur->index < index) { |
| 681 | node = node->rb_left; |
| 682 | } else if (cur->index > index) { |
| 683 | node = node->rb_right; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 684 | } else if (cur->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 685 | node = node->rb_left; |
| 686 | } else if (!smallest) { |
| 687 | /* we want the node with the highest seq */ |
| 688 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 689 | BUG_ON(found->seq > cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 690 | found = cur; |
| 691 | node = node->rb_left; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 692 | } else if (cur->seq > min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 693 | /* we want the node with the smallest seq */ |
| 694 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 695 | BUG_ON(found->seq < cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 696 | found = cur; |
| 697 | node = node->rb_right; |
| 698 | } else { |
| 699 | found = cur; |
| 700 | break; |
| 701 | } |
| 702 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 703 | tree_mod_log_read_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 704 | |
| 705 | return found; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * this returns the element from the log with the smallest time sequence |
| 710 | * value that's in the log (the oldest log item). any element with a time |
| 711 | * sequence lower than min_seq will be ignored. |
| 712 | */ |
| 713 | static struct tree_mod_elem * |
| 714 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 715 | u64 min_seq) |
| 716 | { |
| 717 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * this returns the element from the log with the largest time sequence |
| 722 | * value that's in the log (the most recent log item). any element with |
| 723 | * a time sequence lower than min_seq will be ignored. |
| 724 | */ |
| 725 | static struct tree_mod_elem * |
| 726 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 727 | { |
| 728 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 729 | } |
| 730 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 731 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 732 | tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 733 | struct extent_buffer *src, unsigned long dst_offset, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 734 | unsigned long src_offset, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 735 | { |
| 736 | int ret; |
| 737 | int i; |
| 738 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 739 | if (tree_mod_dont_log(fs_info, NULL)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 740 | return; |
| 741 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 742 | if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 743 | return; |
| 744 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 745 | for (i = 0; i < nr_items; i++) { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 746 | ret = __tree_mod_log_insert_key(fs_info, src, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 747 | i + src_offset, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 748 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 749 | BUG_ON(ret < 0); |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 750 | ret = __tree_mod_log_insert_key(fs_info, dst, |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 751 | i + dst_offset, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 752 | MOD_LOG_KEY_ADD, |
| 753 | GFP_NOFS); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 754 | BUG_ON(ret < 0); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | static inline void |
| 759 | tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 760 | int dst_offset, int src_offset, int nr_items) |
| 761 | { |
| 762 | int ret; |
| 763 | ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset, |
| 764 | nr_items, GFP_NOFS); |
| 765 | BUG_ON(ret < 0); |
| 766 | } |
| 767 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 768 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 769 | tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 770 | struct extent_buffer *eb, int slot, int atomic) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 771 | { |
| 772 | int ret; |
| 773 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 774 | ret = __tree_mod_log_insert_key(fs_info, eb, slot, |
| 775 | MOD_LOG_KEY_REPLACE, |
| 776 | atomic ? GFP_ATOMIC : GFP_NOFS); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 777 | BUG_ON(ret < 0); |
| 778 | } |
| 779 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 780 | static noinline void |
| 781 | tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 782 | { |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 783 | if (tree_mod_dont_log(fs_info, eb)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 784 | return; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 785 | __tree_mod_log_free_eb(fs_info, eb); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 786 | } |
| 787 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 788 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 789 | tree_mod_log_set_root_pointer(struct btrfs_root *root, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 790 | struct extent_buffer *new_root_node, |
| 791 | int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 792 | { |
| 793 | int ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 794 | ret = tree_mod_log_insert_root(root->fs_info, root->node, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 795 | new_root_node, GFP_NOFS, log_removal); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 796 | BUG_ON(ret < 0); |
| 797 | } |
| 798 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 799 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 800 | * check if the tree block can be shared by multiple trees |
| 801 | */ |
| 802 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 803 | struct extent_buffer *buf) |
| 804 | { |
| 805 | /* |
| 806 | * Tree blocks not in refernece counted trees and tree roots |
| 807 | * are never shared. If a block was allocated after the last |
| 808 | * snapshot and the block was not allocated by tree relocation, |
| 809 | * we know the block is not shared. |
| 810 | */ |
| 811 | if (root->ref_cows && |
| 812 | buf != root->node && buf != root->commit_root && |
| 813 | (btrfs_header_generation(buf) <= |
| 814 | btrfs_root_last_snapshot(&root->root_item) || |
| 815 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 816 | return 1; |
| 817 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 818 | if (root->ref_cows && |
| 819 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 820 | return 1; |
| 821 | #endif |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 826 | struct btrfs_root *root, |
| 827 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 828 | struct extent_buffer *cow, |
| 829 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 830 | { |
| 831 | u64 refs; |
| 832 | u64 owner; |
| 833 | u64 flags; |
| 834 | u64 new_flags = 0; |
| 835 | int ret; |
| 836 | |
| 837 | /* |
| 838 | * Backrefs update rules: |
| 839 | * |
| 840 | * Always use full backrefs for extent pointers in tree block |
| 841 | * allocated by tree relocation. |
| 842 | * |
| 843 | * If a shared tree block is no longer referenced by its owner |
| 844 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 845 | * use full backrefs for extent pointers in tree block. |
| 846 | * |
| 847 | * If a tree block is been relocating |
| 848 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 849 | * use full backrefs for extent pointers in tree block. |
| 850 | * The reason for this is some operations (such as drop tree) |
| 851 | * are only allowed for blocks use full backrefs. |
| 852 | */ |
| 853 | |
| 854 | if (btrfs_block_can_be_shared(root, buf)) { |
| 855 | ret = btrfs_lookup_extent_info(trans, root, buf->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 856 | btrfs_header_level(buf), 1, |
| 857 | &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 858 | if (ret) |
| 859 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 860 | if (refs == 0) { |
| 861 | ret = -EROFS; |
| 862 | btrfs_std_error(root->fs_info, ret); |
| 863 | return ret; |
| 864 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 865 | } else { |
| 866 | refs = 1; |
| 867 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 868 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 869 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 870 | else |
| 871 | flags = 0; |
| 872 | } |
| 873 | |
| 874 | owner = btrfs_header_owner(buf); |
| 875 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 876 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 877 | |
| 878 | if (refs > 1) { |
| 879 | if ((owner == root->root_key.objectid || |
| 880 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 881 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 882 | ret = btrfs_inc_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 883 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 884 | |
| 885 | if (root->root_key.objectid == |
| 886 | BTRFS_TREE_RELOC_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 887 | ret = btrfs_dec_ref(trans, root, buf, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 888 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 889 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 890 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 891 | } |
| 892 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 893 | } else { |
| 894 | |
| 895 | if (root->root_key.objectid == |
| 896 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 897 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 898 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 899 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 900 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 901 | } |
| 902 | if (new_flags != 0) { |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 903 | int level = btrfs_header_level(buf); |
| 904 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 905 | ret = btrfs_set_disk_extent_flags(trans, root, |
| 906 | buf->start, |
| 907 | buf->len, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 908 | new_flags, level, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 909 | if (ret) |
| 910 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 911 | } |
| 912 | } else { |
| 913 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 914 | if (root->root_key.objectid == |
| 915 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 916 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 917 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 918 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 919 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 920 | ret = btrfs_dec_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 921 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 922 | } |
| 923 | clean_tree_block(trans, root, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 924 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 925 | } |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 930 | * does the dirty work in cow of a single block. The parent block (if |
| 931 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 932 | * dirty and returned locked. If you modify the block it needs to be marked |
| 933 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 934 | * |
| 935 | * search_start -- an allocation hint for the new block |
| 936 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 937 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 938 | * bytes the allocator should try to find free next to the block it returns. |
| 939 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 940 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 941 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 942 | struct btrfs_root *root, |
| 943 | struct extent_buffer *buf, |
| 944 | struct extent_buffer *parent, int parent_slot, |
| 945 | struct extent_buffer **cow_ret, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 946 | u64 search_start, u64 empty_size) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 947 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 948 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 949 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 950 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 951 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 952 | int unlock_orig = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 953 | u64 parent_start; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 954 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 955 | if (*cow_ret == buf) |
| 956 | unlock_orig = 1; |
| 957 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 958 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 959 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 960 | WARN_ON(root->ref_cows && trans->transid != |
| 961 | root->fs_info->running_transaction->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 962 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 963 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 964 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 965 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 966 | if (level == 0) |
| 967 | btrfs_item_key(buf, &disk_key, 0); |
| 968 | else |
| 969 | btrfs_node_key(buf, &disk_key, 0); |
| 970 | |
| 971 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 972 | if (parent) |
| 973 | parent_start = parent->start; |
| 974 | else |
| 975 | parent_start = 0; |
| 976 | } else |
| 977 | parent_start = 0; |
| 978 | |
| 979 | cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, |
| 980 | root->root_key.objectid, &disk_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 981 | level, search_start, empty_size); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 982 | if (IS_ERR(cow)) |
| 983 | return PTR_ERR(cow); |
| 984 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 985 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 986 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 987 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 988 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 989 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 990 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 991 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 992 | BTRFS_HEADER_FLAG_RELOC); |
| 993 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 994 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 995 | else |
| 996 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 997 | |
Ross Kirk | 0a4e558 | 2013-09-24 10:12:38 +0100 | [diff] [blame] | 998 | write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 999 | BTRFS_FSID_SIZE); |
| 1000 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1001 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1002 | if (ret) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1003 | btrfs_abort_transaction(trans, root, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1004 | return ret; |
| 1005 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1006 | |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1007 | if (root->ref_cows) { |
| 1008 | ret = btrfs_reloc_cow_block(trans, root, buf, cow); |
| 1009 | if (ret) |
| 1010 | return ret; |
| 1011 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1012 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1013 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1014 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1015 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1016 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1017 | parent_start = buf->start; |
| 1018 | else |
| 1019 | parent_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1020 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1021 | extent_buffer_get(cow); |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1022 | tree_mod_log_set_root_pointer(root, cow, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1023 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1024 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1025 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1026 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1027 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1028 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1029 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1030 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1031 | parent_start = parent->start; |
| 1032 | else |
| 1033 | parent_start = 0; |
| 1034 | |
| 1035 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1036 | tree_mod_log_insert_key(root->fs_info, parent, parent_slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 1037 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1038 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1039 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1040 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1041 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1042 | btrfs_mark_buffer_dirty(parent); |
Josef Bacik | 7fb7d76f | 2013-07-01 16:10:16 -0400 | [diff] [blame] | 1043 | if (last_ref) |
| 1044 | tree_mod_log_free_eb(root->fs_info, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1045 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1046 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1047 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1048 | if (unlock_orig) |
| 1049 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1050 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1051 | btrfs_mark_buffer_dirty(cow); |
| 1052 | *cow_ret = cow; |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1056 | /* |
| 1057 | * returns the logical address of the oldest predecessor of the given root. |
| 1058 | * entries older than time_seq are ignored. |
| 1059 | */ |
| 1060 | static struct tree_mod_elem * |
| 1061 | __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1062 | struct extent_buffer *eb_root, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1063 | { |
| 1064 | struct tree_mod_elem *tm; |
| 1065 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1066 | u64 root_logical = eb_root->start; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1067 | int looped = 0; |
| 1068 | |
| 1069 | if (!time_seq) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1070 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1071 | |
| 1072 | /* |
| 1073 | * the very last operation that's logged for a root is the replacement |
| 1074 | * operation (if it is replaced at all). this has the index of the *new* |
| 1075 | * root, making it the very first operation that's logged for this root. |
| 1076 | */ |
| 1077 | while (1) { |
| 1078 | tm = tree_mod_log_search_oldest(fs_info, root_logical, |
| 1079 | time_seq); |
| 1080 | if (!looped && !tm) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1081 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1082 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1083 | * if there are no tree operation for the oldest root, we simply |
| 1084 | * return it. this should only happen if that (old) root is at |
| 1085 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1086 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1087 | if (!tm) |
| 1088 | break; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1089 | |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1090 | /* |
| 1091 | * if there's an operation that's not a root replacement, we |
| 1092 | * found the oldest version of our root. normally, we'll find a |
| 1093 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1094 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1095 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1096 | break; |
| 1097 | |
| 1098 | found = tm; |
| 1099 | root_logical = tm->old_root.logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1100 | looped = 1; |
| 1101 | } |
| 1102 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1103 | /* if there's no old root to return, return what we found instead */ |
| 1104 | if (!found) |
| 1105 | found = tm; |
| 1106 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1107 | return found; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * tm is a pointer to the first operation to rewind within eb. then, all |
| 1112 | * previous operations will be rewinded (until we reach something older than |
| 1113 | * time_seq). |
| 1114 | */ |
| 1115 | static void |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1116 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1117 | u64 time_seq, struct tree_mod_elem *first_tm) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1118 | { |
| 1119 | u32 n; |
| 1120 | struct rb_node *next; |
| 1121 | struct tree_mod_elem *tm = first_tm; |
| 1122 | unsigned long o_dst; |
| 1123 | unsigned long o_src; |
| 1124 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1125 | |
| 1126 | n = btrfs_header_nritems(eb); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1127 | tree_mod_log_read_lock(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1128 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1129 | /* |
| 1130 | * all the operations are recorded with the operator used for |
| 1131 | * the modification. as we're going backwards, we do the |
| 1132 | * opposite of each operation here. |
| 1133 | */ |
| 1134 | switch (tm->op) { |
| 1135 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1136 | BUG_ON(tm->slot < n); |
Eric Sandeen | 1c697d4 | 2013-01-31 00:54:56 +0000 | [diff] [blame] | 1137 | /* Fallthrough */ |
Liu Bo | 95c80bb | 2012-10-19 09:50:52 +0000 | [diff] [blame] | 1138 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1139 | case MOD_LOG_KEY_REMOVE: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1140 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1141 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1142 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1143 | tm->generation); |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1144 | n++; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1145 | break; |
| 1146 | case MOD_LOG_KEY_REPLACE: |
| 1147 | BUG_ON(tm->slot >= n); |
| 1148 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1149 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1150 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1151 | tm->generation); |
| 1152 | break; |
| 1153 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1154 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1155 | n--; |
| 1156 | break; |
| 1157 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1158 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1159 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1160 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1161 | tm->move.nr_items * p_size); |
| 1162 | break; |
| 1163 | case MOD_LOG_ROOT_REPLACE: |
| 1164 | /* |
| 1165 | * this operation is special. for roots, this must be |
| 1166 | * handled explicitly before rewinding. |
| 1167 | * for non-roots, this operation may exist if the node |
| 1168 | * was a root: root A -> child B; then A gets empty and |
| 1169 | * B is promoted to the new root. in the mod log, we'll |
| 1170 | * have a root-replace operation for B, a tree block |
| 1171 | * that is no root. we simply ignore that operation. |
| 1172 | */ |
| 1173 | break; |
| 1174 | } |
| 1175 | next = rb_next(&tm->node); |
| 1176 | if (!next) |
| 1177 | break; |
| 1178 | tm = container_of(next, struct tree_mod_elem, node); |
| 1179 | if (tm->index != first_tm->index) |
| 1180 | break; |
| 1181 | } |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1182 | tree_mod_log_read_unlock(fs_info); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1183 | btrfs_set_header_nritems(eb, n); |
| 1184 | } |
| 1185 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1186 | /* |
| 1187 | * Called with eb read locked. If the buffer cannot be rewinded, the same buffer |
| 1188 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
| 1189 | * returned buffer is always read-locked. If the returned buffer is not the |
| 1190 | * input buffer, the lock on the input buffer is released and the input buffer |
| 1191 | * is freed (its refcount is decremented). |
| 1192 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1193 | static struct extent_buffer * |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1194 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 1195 | struct extent_buffer *eb, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1196 | { |
| 1197 | struct extent_buffer *eb_rewin; |
| 1198 | struct tree_mod_elem *tm; |
| 1199 | |
| 1200 | if (!time_seq) |
| 1201 | return eb; |
| 1202 | |
| 1203 | if (btrfs_header_level(eb) == 0) |
| 1204 | return eb; |
| 1205 | |
| 1206 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1207 | if (!tm) |
| 1208 | return eb; |
| 1209 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1210 | btrfs_set_path_blocking(path); |
| 1211 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1212 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1213 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1214 | BUG_ON(tm->slot != 0); |
| 1215 | eb_rewin = alloc_dummy_extent_buffer(eb->start, |
| 1216 | fs_info->tree_root->nodesize); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1217 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1218 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1219 | free_extent_buffer(eb); |
| 1220 | return NULL; |
| 1221 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1222 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1223 | btrfs_set_header_backref_rev(eb_rewin, |
| 1224 | btrfs_header_backref_rev(eb)); |
| 1225 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1226 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1227 | } else { |
| 1228 | eb_rewin = btrfs_clone_extent_buffer(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1229 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1230 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1231 | free_extent_buffer(eb); |
| 1232 | return NULL; |
| 1233 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1236 | btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK); |
| 1237 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1238 | free_extent_buffer(eb); |
| 1239 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1240 | extent_buffer_get(eb_rewin); |
| 1241 | btrfs_tree_read_lock(eb_rewin); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1242 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1243 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
Arne Jansen | 2a745b1 | 2013-02-13 04:20:01 -0700 | [diff] [blame] | 1244 | BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1245 | |
| 1246 | return eb_rewin; |
| 1247 | } |
| 1248 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1249 | /* |
| 1250 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1251 | * value. If there are no changes, the current root->root_node is returned. If |
| 1252 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1253 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1254 | * Returns NULL on error (with no locks held). |
| 1255 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1256 | static inline struct extent_buffer * |
| 1257 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1258 | { |
| 1259 | struct tree_mod_elem *tm; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1260 | struct extent_buffer *eb = NULL; |
| 1261 | struct extent_buffer *eb_root; |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1262 | struct extent_buffer *old; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1263 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1264 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1265 | u64 logical; |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1266 | u32 blocksize; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1267 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1268 | eb_root = btrfs_read_lock_root_node(root); |
| 1269 | tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1270 | if (!tm) |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1271 | return eb_root; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1272 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1273 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1274 | old_root = &tm->old_root; |
| 1275 | old_generation = tm->generation; |
| 1276 | logical = old_root->logical; |
| 1277 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1278 | logical = eb_root->start; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1279 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1280 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1281 | tm = tree_mod_log_search(root->fs_info, logical, time_seq); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1282 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1283 | btrfs_tree_read_unlock(eb_root); |
| 1284 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1285 | blocksize = btrfs_level_size(root, old_root->level); |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1286 | old = read_tree_block(root, logical, blocksize, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1287 | if (WARN_ON(!old || !extent_buffer_uptodate(old))) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1288 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1289 | pr_warn("btrfs: failed to read tree block %llu from get_old_root\n", |
| 1290 | logical); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1291 | } else { |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1292 | eb = btrfs_clone_extent_buffer(old); |
| 1293 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1294 | } |
| 1295 | } else if (old_root) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1296 | btrfs_tree_read_unlock(eb_root); |
| 1297 | free_extent_buffer(eb_root); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1298 | eb = alloc_dummy_extent_buffer(logical, root->nodesize); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1299 | } else { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1300 | btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1301 | eb = btrfs_clone_extent_buffer(eb_root); |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1302 | btrfs_tree_read_unlock_blocking(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1303 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1304 | } |
| 1305 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1306 | if (!eb) |
| 1307 | return NULL; |
Jan Schmidt | d638108 | 2012-10-23 14:21:05 +0200 | [diff] [blame] | 1308 | extent_buffer_get(eb); |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1309 | btrfs_tree_read_lock(eb); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1310 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1311 | btrfs_set_header_bytenr(eb, eb->start); |
| 1312 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1313 | btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1314 | btrfs_set_header_level(eb, old_root->level); |
| 1315 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1316 | } |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1317 | if (tm) |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1318 | __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1319 | else |
| 1320 | WARN_ON(btrfs_header_level(eb) != 0); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1321 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1322 | |
| 1323 | return eb; |
| 1324 | } |
| 1325 | |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1326 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
| 1327 | { |
| 1328 | struct tree_mod_elem *tm; |
| 1329 | int level; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1330 | struct extent_buffer *eb_root = btrfs_root_node(root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1331 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1332 | tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1333 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1334 | level = tm->old_root.level; |
| 1335 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1336 | level = btrfs_header_level(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1337 | } |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1338 | free_extent_buffer(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1339 | |
| 1340 | return level; |
| 1341 | } |
| 1342 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1343 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1344 | struct btrfs_root *root, |
| 1345 | struct extent_buffer *buf) |
| 1346 | { |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1347 | /* ensure we can see the force_cow */ |
| 1348 | smp_rmb(); |
| 1349 | |
| 1350 | /* |
| 1351 | * We do not need to cow a block if |
| 1352 | * 1) this block is not created or changed in this transaction; |
| 1353 | * 2) this block does not belong to TREE_RELOC tree; |
| 1354 | * 3) the root is not forced COW. |
| 1355 | * |
| 1356 | * What is forced COW: |
| 1357 | * when we create snapshot during commiting the transaction, |
| 1358 | * after we've finished coping src root, we must COW the shared |
| 1359 | * block to ensure the metadata consistency. |
| 1360 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1361 | if (btrfs_header_generation(buf) == trans->transid && |
| 1362 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1363 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1364 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
| 1365 | !root->force_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1366 | return 0; |
| 1367 | return 1; |
| 1368 | } |
| 1369 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1370 | /* |
| 1371 | * cows a single block, see __btrfs_cow_block for the real work. |
| 1372 | * This version of it has extra checks so that a block isn't cow'd more than |
| 1373 | * once per transaction, as long as it hasn't been written yet |
| 1374 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1375 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1376 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1377 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1378 | struct extent_buffer **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1379 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1380 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1381 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1382 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1383 | if (trans->transaction != root->fs_info->running_transaction) |
| 1384 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1385 | trans->transid, |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1386 | root->fs_info->running_transaction->transid); |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1387 | |
| 1388 | if (trans->transid != root->fs_info->generation) |
| 1389 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1390 | trans->transid, root->fs_info->generation); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1391 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1392 | if (!should_cow_block(trans, root, buf)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1393 | *cow_ret = buf; |
| 1394 | return 0; |
| 1395 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1396 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1397 | search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1398 | |
| 1399 | if (parent) |
| 1400 | btrfs_set_lock_blocking(parent); |
| 1401 | btrfs_set_lock_blocking(buf); |
| 1402 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1403 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1404 | parent_slot, cow_ret, search_start, 0); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1405 | |
| 1406 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1407 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1408 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1409 | } |
| 1410 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1411 | /* |
| 1412 | * helper function for defrag to decide if two blocks pointed to by a |
| 1413 | * node are actually close by |
| 1414 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1415 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1416 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1417 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1418 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1419 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1420 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1424 | /* |
| 1425 | * compare two keys in a memcmp fashion |
| 1426 | */ |
| 1427 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
| 1428 | { |
| 1429 | struct btrfs_key k1; |
| 1430 | |
| 1431 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1432 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1433 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1434 | } |
| 1435 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1436 | /* |
| 1437 | * same as comp_keys only with two btrfs_key's |
| 1438 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1439 | 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] | 1440 | { |
| 1441 | if (k1->objectid > k2->objectid) |
| 1442 | return 1; |
| 1443 | if (k1->objectid < k2->objectid) |
| 1444 | return -1; |
| 1445 | if (k1->type > k2->type) |
| 1446 | return 1; |
| 1447 | if (k1->type < k2->type) |
| 1448 | return -1; |
| 1449 | if (k1->offset > k2->offset) |
| 1450 | return 1; |
| 1451 | if (k1->offset < k2->offset) |
| 1452 | return -1; |
| 1453 | return 0; |
| 1454 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1455 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1456 | /* |
| 1457 | * this is used by the defrag code to go through all the |
| 1458 | * leaves pointed to by a node and reallocate them so that |
| 1459 | * disk order is close to key order |
| 1460 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1461 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1462 | struct btrfs_root *root, struct extent_buffer *parent, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1463 | int start_slot, u64 *last_ret, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1464 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1465 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1466 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1467 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1468 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1469 | u64 search_start = *last_ret; |
| 1470 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1471 | u64 other; |
| 1472 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1473 | int end_slot; |
| 1474 | int i; |
| 1475 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1476 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1477 | int uptodate; |
| 1478 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1479 | int progress_passed = 0; |
| 1480 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1481 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1482 | parent_level = btrfs_header_level(parent); |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1483 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1484 | WARN_ON(trans->transaction != root->fs_info->running_transaction); |
| 1485 | WARN_ON(trans->transid != root->fs_info->generation); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1486 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1487 | parent_nritems = btrfs_header_nritems(parent); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1488 | blocksize = btrfs_level_size(root, parent_level - 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1489 | end_slot = parent_nritems; |
| 1490 | |
| 1491 | if (parent_nritems == 1) |
| 1492 | return 0; |
| 1493 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1494 | btrfs_set_lock_blocking(parent); |
| 1495 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1496 | for (i = start_slot; i < end_slot; i++) { |
| 1497 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1498 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1499 | btrfs_node_key(parent, &disk_key, i); |
| 1500 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1501 | continue; |
| 1502 | |
| 1503 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1504 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1505 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1506 | if (last_block == 0) |
| 1507 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1508 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1509 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1510 | other = btrfs_node_blockptr(parent, i - 1); |
| 1511 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1512 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1513 | if (!close && i < end_slot - 2) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1514 | other = btrfs_node_blockptr(parent, i + 1); |
| 1515 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1516 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1517 | if (close) { |
| 1518 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1519 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1520 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1521 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1522 | cur = btrfs_find_tree_block(root, blocknr, blocksize); |
| 1523 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1524 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1525 | else |
| 1526 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1527 | if (!cur || !uptodate) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1528 | if (!cur) { |
| 1529 | cur = read_tree_block(root, blocknr, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1530 | blocksize, gen); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1531 | if (!cur || !extent_buffer_uptodate(cur)) { |
| 1532 | free_extent_buffer(cur); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1533 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1534 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1535 | } else if (!uptodate) { |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 1536 | err = btrfs_read_buffer(cur, gen); |
| 1537 | if (err) { |
| 1538 | free_extent_buffer(cur); |
| 1539 | return err; |
| 1540 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1541 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1542 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1543 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1544 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1545 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1546 | btrfs_tree_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1547 | btrfs_set_lock_blocking(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1548 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1549 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1550 | min(16 * blocksize, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1551 | (end_slot - i) * blocksize)); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1552 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1553 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1554 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1555 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1556 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1557 | search_start = cur->start; |
| 1558 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1559 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1560 | btrfs_tree_unlock(cur); |
| 1561 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1562 | } |
| 1563 | return err; |
| 1564 | } |
| 1565 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1566 | /* |
| 1567 | * The leaf data grows from end-to-front in the node. |
| 1568 | * this returns the address of the start of the last item, |
| 1569 | * which is the stop of the leaf data stack |
| 1570 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1571 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1572 | struct extent_buffer *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1573 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1574 | u32 nr = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1575 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1576 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1577 | return btrfs_item_offset_nr(leaf, nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1578 | } |
| 1579 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1580 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1581 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1582 | * search for key in the extent_buffer. The items start at offset p, |
| 1583 | * and they are item_size apart. There are 'max' items in p. |
| 1584 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1585 | * the slot in the array is returned via slot, and it points to |
| 1586 | * the place where you would insert key if it is not found in |
| 1587 | * the array. |
| 1588 | * |
| 1589 | * slot may point to max if the key is bigger than all of the keys |
| 1590 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1591 | static noinline int generic_bin_search(struct extent_buffer *eb, |
| 1592 | unsigned long p, |
| 1593 | int item_size, struct btrfs_key *key, |
| 1594 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1595 | { |
| 1596 | int low = 0; |
| 1597 | int high = max; |
| 1598 | int mid; |
| 1599 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1600 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1601 | struct btrfs_disk_key unaligned; |
| 1602 | unsigned long offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1603 | char *kaddr = NULL; |
| 1604 | unsigned long map_start = 0; |
| 1605 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1606 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1607 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1608 | while (low < high) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1609 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1610 | offset = p + mid * item_size; |
| 1611 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1612 | if (!kaddr || offset < map_start || |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1613 | (offset + sizeof(struct btrfs_disk_key)) > |
| 1614 | map_start + map_len) { |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1615 | |
| 1616 | err = map_private_extent_buffer(eb, offset, |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1617 | sizeof(struct btrfs_disk_key), |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1618 | &kaddr, &map_start, &map_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1619 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1620 | if (!err) { |
| 1621 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1622 | map_start); |
| 1623 | } else { |
| 1624 | read_extent_buffer(eb, &unaligned, |
| 1625 | offset, sizeof(unaligned)); |
| 1626 | tmp = &unaligned; |
| 1627 | } |
| 1628 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1629 | } else { |
| 1630 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1631 | map_start); |
| 1632 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1633 | ret = comp_keys(tmp, key); |
| 1634 | |
| 1635 | if (ret < 0) |
| 1636 | low = mid + 1; |
| 1637 | else if (ret > 0) |
| 1638 | high = mid; |
| 1639 | else { |
| 1640 | *slot = mid; |
| 1641 | return 0; |
| 1642 | } |
| 1643 | } |
| 1644 | *slot = low; |
| 1645 | return 1; |
| 1646 | } |
| 1647 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1648 | /* |
| 1649 | * simple bin_search frontend that does the right thing for |
| 1650 | * leaves vs nodes |
| 1651 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1652 | static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1653 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1654 | { |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1655 | if (level == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1656 | return generic_bin_search(eb, |
| 1657 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1658 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1659 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1660 | slot); |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1661 | else |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1662 | return generic_bin_search(eb, |
| 1663 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1664 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1665 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1666 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1667 | } |
| 1668 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1669 | int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1670 | int level, int *slot) |
| 1671 | { |
| 1672 | return bin_search(eb, key, level, slot); |
| 1673 | } |
| 1674 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1675 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1676 | { |
| 1677 | spin_lock(&root->accounting_lock); |
| 1678 | btrfs_set_root_used(&root->root_item, |
| 1679 | btrfs_root_used(&root->root_item) + size); |
| 1680 | spin_unlock(&root->accounting_lock); |
| 1681 | } |
| 1682 | |
| 1683 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1684 | { |
| 1685 | spin_lock(&root->accounting_lock); |
| 1686 | btrfs_set_root_used(&root->root_item, |
| 1687 | btrfs_root_used(&root->root_item) - size); |
| 1688 | spin_unlock(&root->accounting_lock); |
| 1689 | } |
| 1690 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1691 | /* given a node and slot number, this reads the blocks it points to. The |
| 1692 | * extent buffer is returned with a reference taken (but unlocked). |
| 1693 | * NULL is returned on error. |
| 1694 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1695 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1696 | struct extent_buffer *parent, int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1697 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1698 | int level = btrfs_header_level(parent); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1699 | struct extent_buffer *eb; |
| 1700 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1701 | if (slot < 0) |
| 1702 | return NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1703 | if (slot >= btrfs_header_nritems(parent)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1704 | return NULL; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1705 | |
| 1706 | BUG_ON(level == 0); |
| 1707 | |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1708 | eb = read_tree_block(root, btrfs_node_blockptr(parent, slot), |
| 1709 | btrfs_level_size(root, level - 1), |
| 1710 | btrfs_node_ptr_generation(parent, slot)); |
| 1711 | if (eb && !extent_buffer_uptodate(eb)) { |
| 1712 | free_extent_buffer(eb); |
| 1713 | eb = NULL; |
| 1714 | } |
| 1715 | |
| 1716 | return eb; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1717 | } |
| 1718 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1719 | /* |
| 1720 | * node level balancing, used to make sure nodes are in proper order for |
| 1721 | * item deletion. We balance from the top down, so we have to make sure |
| 1722 | * that a deletion won't leave an node completely empty later on. |
| 1723 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1724 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1725 | struct btrfs_root *root, |
| 1726 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1727 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1728 | struct extent_buffer *right = NULL; |
| 1729 | struct extent_buffer *mid; |
| 1730 | struct extent_buffer *left = NULL; |
| 1731 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1732 | int ret = 0; |
| 1733 | int wret; |
| 1734 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1735 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1736 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1737 | |
| 1738 | if (level == 0) |
| 1739 | return 0; |
| 1740 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1741 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1742 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1743 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1744 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1745 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1746 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1747 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1748 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1749 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1750 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1751 | pslot = path->slots[level + 1]; |
| 1752 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1753 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1754 | /* |
| 1755 | * deal with the case where there is only one pointer in the root |
| 1756 | * by promoting the node below to a root |
| 1757 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1758 | if (!parent) { |
| 1759 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1760 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1761 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1762 | return 0; |
| 1763 | |
| 1764 | /* promote the child to a root */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1765 | child = read_node_slot(root, mid, 0); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1766 | if (!child) { |
| 1767 | ret = -EROFS; |
| 1768 | btrfs_std_error(root->fs_info, ret); |
| 1769 | goto enospc; |
| 1770 | } |
| 1771 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1772 | btrfs_tree_lock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1773 | btrfs_set_lock_blocking(child); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1774 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1775 | if (ret) { |
| 1776 | btrfs_tree_unlock(child); |
| 1777 | free_extent_buffer(child); |
| 1778 | goto enospc; |
| 1779 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1780 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1781 | tree_mod_log_set_root_pointer(root, child, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1782 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1783 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1784 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1785 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1786 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1787 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1788 | path->nodes[level] = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1789 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1790 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1791 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1792 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1793 | |
| 1794 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1795 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1796 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1797 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1798 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1799 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1800 | if (btrfs_header_nritems(mid) > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1801 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1802 | return 0; |
| 1803 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1804 | left = read_node_slot(root, parent, pslot - 1); |
| 1805 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1806 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1807 | btrfs_set_lock_blocking(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1808 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1809 | parent, pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1810 | if (wret) { |
| 1811 | ret = wret; |
| 1812 | goto enospc; |
| 1813 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1814 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1815 | right = read_node_slot(root, parent, pslot + 1); |
| 1816 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1817 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1818 | btrfs_set_lock_blocking(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1819 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1820 | parent, pslot + 1, &right); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1821 | if (wret) { |
| 1822 | ret = wret; |
| 1823 | goto enospc; |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1828 | if (left) { |
| 1829 | orig_slot += btrfs_header_nritems(left); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1830 | wret = push_node_left(trans, root, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1831 | if (wret < 0) |
| 1832 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1833 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1834 | |
| 1835 | /* |
| 1836 | * then try to empty the right most buffer into the middle |
| 1837 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1838 | if (right) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1839 | wret = push_node_left(trans, root, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1840 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1841 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1842 | if (btrfs_header_nritems(right) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1843 | clean_tree_block(trans, root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1844 | btrfs_tree_unlock(right); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1845 | del_ptr(root, path, level + 1, pslot + 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1846 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1847 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1848 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1849 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1850 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1851 | struct btrfs_disk_key right_key; |
| 1852 | btrfs_node_key(right, &right_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1853 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1854 | pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1855 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1856 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1857 | } |
| 1858 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1859 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1860 | /* |
| 1861 | * we're not allowed to leave a node with one item in the |
| 1862 | * tree during a delete. A deletion from lower in the tree |
| 1863 | * could try to delete the only pointer in this node. |
| 1864 | * So, pull some keys from the left. |
| 1865 | * There has to be a left pointer at this point because |
| 1866 | * otherwise we would have pulled some pointers from the |
| 1867 | * right |
| 1868 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1869 | if (!left) { |
| 1870 | ret = -EROFS; |
| 1871 | btrfs_std_error(root->fs_info, ret); |
| 1872 | goto enospc; |
| 1873 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1874 | wret = balance_node_right(trans, root, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1875 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1876 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1877 | goto enospc; |
| 1878 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1879 | if (wret == 1) { |
| 1880 | wret = push_node_left(trans, root, left, mid, 1); |
| 1881 | if (wret < 0) |
| 1882 | ret = wret; |
| 1883 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1884 | BUG_ON(wret == 1); |
| 1885 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1886 | if (btrfs_header_nritems(mid) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1887 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1888 | btrfs_tree_unlock(mid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1889 | del_ptr(root, path, level + 1, pslot); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1890 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1891 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1892 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1893 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1894 | } else { |
| 1895 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1896 | struct btrfs_disk_key mid_key; |
| 1897 | btrfs_node_key(mid, &mid_key, 0); |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1898 | tree_mod_log_set_node_key(root->fs_info, parent, |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1899 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1900 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1901 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1902 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1903 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1904 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1905 | if (left) { |
| 1906 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1907 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1908 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1909 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1910 | path->slots[level + 1] -= 1; |
| 1911 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1912 | if (mid) { |
| 1913 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1914 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1915 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1916 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1917 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1918 | path->slots[level] = orig_slot; |
| 1919 | } |
| 1920 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1921 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1922 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1923 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1924 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1925 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1926 | if (right) { |
| 1927 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1928 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1929 | } |
| 1930 | if (left) { |
| 1931 | if (path->nodes[level] != left) |
| 1932 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1933 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1934 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1935 | return ret; |
| 1936 | } |
| 1937 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1938 | /* Node balancing for insertion. Here we only split or push nodes around |
| 1939 | * when they are completely full. This is also done top down, so we |
| 1940 | * have to be pessimistic. |
| 1941 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1942 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1943 | struct btrfs_root *root, |
| 1944 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1945 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1946 | struct extent_buffer *right = NULL; |
| 1947 | struct extent_buffer *mid; |
| 1948 | struct extent_buffer *left = NULL; |
| 1949 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1950 | int ret = 0; |
| 1951 | int wret; |
| 1952 | int pslot; |
| 1953 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1954 | |
| 1955 | if (level == 0) |
| 1956 | return 1; |
| 1957 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1958 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1959 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1960 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1961 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1962 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1963 | pslot = path->slots[level + 1]; |
| 1964 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1965 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1966 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1967 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1968 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1969 | left = read_node_slot(root, parent, pslot - 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1970 | |
| 1971 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1972 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1973 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1974 | |
| 1975 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1976 | btrfs_set_lock_blocking(left); |
| 1977 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1978 | left_nr = btrfs_header_nritems(left); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1979 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1980 | wret = 1; |
| 1981 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1982 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1983 | pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1984 | if (ret) |
| 1985 | wret = 1; |
| 1986 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1987 | wret = push_node_left(trans, root, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1988 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1989 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1990 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1991 | if (wret < 0) |
| 1992 | ret = wret; |
| 1993 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1994 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1995 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1996 | btrfs_node_key(mid, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1997 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1998 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1999 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 2000 | btrfs_mark_buffer_dirty(parent); |
| 2001 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2002 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2003 | path->slots[level + 1] -= 1; |
| 2004 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2005 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2006 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2007 | } else { |
| 2008 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2009 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2010 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2011 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2012 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2013 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2014 | return 0; |
| 2015 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2016 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2017 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2018 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2019 | right = read_node_slot(root, parent, pslot + 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2020 | |
| 2021 | /* |
| 2022 | * then try to empty the right most buffer into the middle |
| 2023 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2024 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2025 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2026 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2027 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2028 | btrfs_set_lock_blocking(right); |
| 2029 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2030 | right_nr = btrfs_header_nritems(right); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2031 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 2032 | wret = 1; |
| 2033 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2034 | ret = btrfs_cow_block(trans, root, right, |
| 2035 | parent, pslot + 1, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 2036 | &right); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2037 | if (ret) |
| 2038 | wret = 1; |
| 2039 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2040 | wret = balance_node_right(trans, root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2041 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2042 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2043 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2044 | if (wret < 0) |
| 2045 | ret = wret; |
| 2046 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2047 | struct btrfs_disk_key disk_key; |
| 2048 | |
| 2049 | btrfs_node_key(right, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2050 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2051 | pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2052 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2053 | btrfs_mark_buffer_dirty(parent); |
| 2054 | |
| 2055 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2056 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2057 | path->slots[level + 1] += 1; |
| 2058 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2059 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2060 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2061 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2062 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2063 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2064 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2065 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2066 | return 0; |
| 2067 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2068 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2069 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2070 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2071 | return 1; |
| 2072 | } |
| 2073 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2074 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2075 | * readahead one full node of leaves, finding things that are close |
| 2076 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2077 | */ |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2078 | static void reada_for_search(struct btrfs_root *root, |
| 2079 | struct btrfs_path *path, |
| 2080 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2081 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2082 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2083 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2084 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2085 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2086 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2087 | u64 nread = 0; |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2088 | u64 gen; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2089 | int direction = path->reada; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2090 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2091 | u32 nr; |
| 2092 | u32 blocksize; |
| 2093 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2094 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2095 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2096 | return; |
| 2097 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2098 | if (!path->nodes[level]) |
| 2099 | return; |
| 2100 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2101 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2102 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2103 | search = btrfs_node_blockptr(node, slot); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2104 | blocksize = btrfs_level_size(root, level - 1); |
| 2105 | eb = btrfs_find_tree_block(root, search, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2106 | if (eb) { |
| 2107 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2108 | return; |
| 2109 | } |
| 2110 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2111 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2112 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2113 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2114 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2115 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2116 | while (1) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2117 | if (direction < 0) { |
| 2118 | if (nr == 0) |
| 2119 | break; |
| 2120 | nr--; |
| 2121 | } else if (direction > 0) { |
| 2122 | nr++; |
| 2123 | if (nr >= nritems) |
| 2124 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2125 | } |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2126 | if (path->reada < 0 && objectid) { |
| 2127 | btrfs_node_key(node, &disk_key, nr); |
| 2128 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2129 | break; |
| 2130 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2131 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2132 | if ((search <= target && target - search <= 65536) || |
| 2133 | (search > target && search - target <= 65536)) { |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2134 | gen = btrfs_node_ptr_generation(node, nr); |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2135 | readahead_tree_block(root, search, blocksize, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2136 | nread += blocksize; |
| 2137 | } |
| 2138 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2139 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2140 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2141 | } |
| 2142 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2143 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2144 | static noinline void reada_for_balance(struct btrfs_root *root, |
| 2145 | struct btrfs_path *path, int level) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2146 | { |
| 2147 | int slot; |
| 2148 | int nritems; |
| 2149 | struct extent_buffer *parent; |
| 2150 | struct extent_buffer *eb; |
| 2151 | u64 gen; |
| 2152 | u64 block1 = 0; |
| 2153 | u64 block2 = 0; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2154 | int blocksize; |
| 2155 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2156 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2157 | if (!parent) |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2158 | return; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2159 | |
| 2160 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2161 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2162 | blocksize = btrfs_level_size(root, level); |
| 2163 | |
| 2164 | if (slot > 0) { |
| 2165 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2166 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
| 2167 | eb = btrfs_find_tree_block(root, block1, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2168 | /* |
| 2169 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2170 | * don't want to return eagain here. That will loop |
| 2171 | * forever |
| 2172 | */ |
| 2173 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2174 | block1 = 0; |
| 2175 | free_extent_buffer(eb); |
| 2176 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2177 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2178 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2179 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
| 2180 | eb = btrfs_find_tree_block(root, block2, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2181 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2182 | block2 = 0; |
| 2183 | free_extent_buffer(eb); |
| 2184 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2185 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2186 | if (block1) |
| 2187 | readahead_tree_block(root, block1, blocksize, 0); |
| 2188 | if (block2) |
| 2189 | readahead_tree_block(root, block2, blocksize, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | |
| 2193 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2194 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2195 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2196 | * operations on the tree might require changing key pointers higher up in the |
| 2197 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2198 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2199 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2200 | * the lock if the path points to the last slot in the block. This is part of |
| 2201 | * 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] | 2202 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2203 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2204 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2205 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2206 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2207 | int lowest_unlock, int min_write_lock_level, |
| 2208 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2209 | { |
| 2210 | int i; |
| 2211 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2212 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2213 | struct extent_buffer *t; |
| 2214 | |
| 2215 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2216 | if (!path->nodes[i]) |
| 2217 | break; |
| 2218 | if (!path->locks[i]) |
| 2219 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2220 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2221 | skip_level = i + 1; |
| 2222 | continue; |
| 2223 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2224 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2225 | u32 nritems; |
| 2226 | t = path->nodes[i]; |
| 2227 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2228 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2229 | skip_level = i + 1; |
| 2230 | continue; |
| 2231 | } |
| 2232 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2233 | if (skip_level < i && i >= lowest_unlock) |
| 2234 | no_skips = 1; |
| 2235 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2236 | t = path->nodes[i]; |
| 2237 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2238 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2239 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2240 | if (write_lock_level && |
| 2241 | i > min_write_lock_level && |
| 2242 | i <= *write_lock_level) { |
| 2243 | *write_lock_level = i - 1; |
| 2244 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2249 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2250 | * This releases any locks held in the path starting at level and |
| 2251 | * going all the way up to the root. |
| 2252 | * |
| 2253 | * btrfs_search_slot will keep the lock held on higher nodes in a few |
| 2254 | * corner cases, such as COW of the block at slot zero in the node. This |
| 2255 | * ignores those rules, and it should only be called when there are no |
| 2256 | * more updates to be done higher up in the tree. |
| 2257 | */ |
| 2258 | noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) |
| 2259 | { |
| 2260 | int i; |
| 2261 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2262 | if (path->keep_locks) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2263 | return; |
| 2264 | |
| 2265 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2266 | if (!path->nodes[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2267 | continue; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2268 | if (!path->locks[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2269 | continue; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2270 | btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2271 | path->locks[i] = 0; |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2276 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2277 | * in cache without setting the path to blocking. If we find the block |
| 2278 | * we return zero and the path is unchanged. |
| 2279 | * |
| 2280 | * If we can't find the block, we set the path blocking and do some |
| 2281 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2282 | */ |
| 2283 | static int |
| 2284 | read_block_for_search(struct btrfs_trans_handle *trans, |
| 2285 | struct btrfs_root *root, struct btrfs_path *p, |
| 2286 | struct extent_buffer **eb_ret, int level, int slot, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2287 | struct btrfs_key *key, u64 time_seq) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2288 | { |
| 2289 | u64 blocknr; |
| 2290 | u64 gen; |
| 2291 | u32 blocksize; |
| 2292 | struct extent_buffer *b = *eb_ret; |
| 2293 | struct extent_buffer *tmp; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2294 | int ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2295 | |
| 2296 | blocknr = btrfs_node_blockptr(b, slot); |
| 2297 | gen = btrfs_node_ptr_generation(b, slot); |
| 2298 | blocksize = btrfs_level_size(root, level - 1); |
| 2299 | |
| 2300 | tmp = btrfs_find_tree_block(root, blocknr, blocksize); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2301 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2302 | /* first we do an atomic uptodate check */ |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2303 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
| 2304 | *eb_ret = tmp; |
| 2305 | return 0; |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2306 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2307 | |
| 2308 | /* the pages were up to date, but we failed |
| 2309 | * the generation number check. Do a full |
| 2310 | * read for the generation number that is correct. |
| 2311 | * We must do this without dropping locks so |
| 2312 | * we can trust our generation number |
| 2313 | */ |
| 2314 | btrfs_set_path_blocking(p); |
| 2315 | |
| 2316 | /* now we're allowed to do a blocking uptodate check */ |
| 2317 | ret = btrfs_read_buffer(tmp, gen); |
| 2318 | if (!ret) { |
| 2319 | *eb_ret = tmp; |
| 2320 | return 0; |
| 2321 | } |
| 2322 | free_extent_buffer(tmp); |
| 2323 | btrfs_release_path(p); |
| 2324 | return -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | /* |
| 2328 | * reduce lock contention at high levels |
| 2329 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2330 | * we read. Don't release the lock on the current |
| 2331 | * level because we need to walk this node to figure |
| 2332 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2333 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2334 | btrfs_unlock_up_safe(p, level + 1); |
| 2335 | btrfs_set_path_blocking(p); |
| 2336 | |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2337 | free_extent_buffer(tmp); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2338 | if (p->reada) |
| 2339 | reada_for_search(root, p, level, slot, key->objectid); |
| 2340 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2341 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2342 | |
| 2343 | ret = -EAGAIN; |
Yan, Zheng | 5bdd353 | 2010-05-26 11:20:30 -0400 | [diff] [blame] | 2344 | tmp = read_tree_block(root, blocknr, blocksize, 0); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2345 | if (tmp) { |
| 2346 | /* |
| 2347 | * If the read above didn't mark this buffer up to date, |
| 2348 | * it will never end up being up to date. Set ret to EIO now |
| 2349 | * and give up so that our caller doesn't loop forever |
| 2350 | * on our EAGAINs. |
| 2351 | */ |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2352 | if (!btrfs_buffer_uptodate(tmp, 0, 0)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2353 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2354 | free_extent_buffer(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2355 | } |
| 2356 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * helper function for btrfs_search_slot. This does all of the checks |
| 2361 | * for node-level blocks and does any balancing required based on |
| 2362 | * the ins_len. |
| 2363 | * |
| 2364 | * If no extra work was required, zero is returned. If we had to |
| 2365 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2366 | * start over |
| 2367 | */ |
| 2368 | static int |
| 2369 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2370 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2371 | struct extent_buffer *b, int level, int ins_len, |
| 2372 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2373 | { |
| 2374 | int ret; |
| 2375 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
| 2376 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { |
| 2377 | int sret; |
| 2378 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2379 | if (*write_lock_level < level + 1) { |
| 2380 | *write_lock_level = level + 1; |
| 2381 | btrfs_release_path(p); |
| 2382 | goto again; |
| 2383 | } |
| 2384 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2385 | btrfs_set_path_blocking(p); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2386 | reada_for_balance(root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2387 | sret = split_node(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2388 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2389 | |
| 2390 | BUG_ON(sret > 0); |
| 2391 | if (sret) { |
| 2392 | ret = sret; |
| 2393 | goto done; |
| 2394 | } |
| 2395 | b = p->nodes[level]; |
| 2396 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Chris Mason | cfbb930 | 2009-05-18 10:41:58 -0400 | [diff] [blame] | 2397 | BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2398 | int sret; |
| 2399 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2400 | if (*write_lock_level < level + 1) { |
| 2401 | *write_lock_level = level + 1; |
| 2402 | btrfs_release_path(p); |
| 2403 | goto again; |
| 2404 | } |
| 2405 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2406 | btrfs_set_path_blocking(p); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2407 | reada_for_balance(root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2408 | sret = balance_level(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2409 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2410 | |
| 2411 | if (sret) { |
| 2412 | ret = sret; |
| 2413 | goto done; |
| 2414 | } |
| 2415 | b = p->nodes[level]; |
| 2416 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2417 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2418 | goto again; |
| 2419 | } |
| 2420 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2421 | } |
| 2422 | return 0; |
| 2423 | |
| 2424 | again: |
| 2425 | ret = -EAGAIN; |
| 2426 | done: |
| 2427 | return ret; |
| 2428 | } |
| 2429 | |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2430 | static void key_search_validate(struct extent_buffer *b, |
| 2431 | struct btrfs_key *key, |
| 2432 | int level) |
| 2433 | { |
| 2434 | #ifdef CONFIG_BTRFS_ASSERT |
| 2435 | struct btrfs_disk_key disk_key; |
| 2436 | |
| 2437 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 2438 | |
| 2439 | if (level == 0) |
| 2440 | ASSERT(!memcmp_extent_buffer(b, &disk_key, |
| 2441 | offsetof(struct btrfs_leaf, items[0].key), |
| 2442 | sizeof(disk_key))); |
| 2443 | else |
| 2444 | ASSERT(!memcmp_extent_buffer(b, &disk_key, |
| 2445 | offsetof(struct btrfs_node, ptrs[0].key), |
| 2446 | sizeof(disk_key))); |
| 2447 | #endif |
| 2448 | } |
| 2449 | |
| 2450 | static int key_search(struct extent_buffer *b, struct btrfs_key *key, |
| 2451 | int level, int *prev_cmp, int *slot) |
| 2452 | { |
| 2453 | if (*prev_cmp != 0) { |
| 2454 | *prev_cmp = bin_search(b, key, level, slot); |
| 2455 | return *prev_cmp; |
| 2456 | } |
| 2457 | |
| 2458 | key_search_validate(b, key, level); |
| 2459 | *slot = 0; |
| 2460 | |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2464 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2465 | * look for key in the tree. path is filled in with nodes along the way |
| 2466 | * if key is found, we return zero and you can find the item in the leaf |
| 2467 | * level of the path (level 0) |
| 2468 | * |
| 2469 | * 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] | 2470 | * be inserted, and 1 is returned. If there are other errors during the |
| 2471 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2472 | * |
| 2473 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 2474 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 2475 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2476 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2477 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 2478 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 2479 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2480 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2481 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2482 | int slot; |
| 2483 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2484 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2485 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2486 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2487 | int root_lock; |
| 2488 | /* everything at write_lock_level or lower must be write locked */ |
| 2489 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2490 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2491 | int min_write_lock_level; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2492 | int prev_cmp; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2493 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2494 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2495 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2496 | WARN_ON(p->nodes[0] != NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2497 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2498 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2499 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2500 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2501 | /* when we are removing items, we might have to go up to level |
| 2502 | * two as we update tree pointers Make sure we keep write |
| 2503 | * for those levels as well |
| 2504 | */ |
| 2505 | write_lock_level = 2; |
| 2506 | } else if (ins_len > 0) { |
| 2507 | /* |
| 2508 | * for inserting items, make sure we have a write lock on |
| 2509 | * level 1 so we can update keys |
| 2510 | */ |
| 2511 | write_lock_level = 1; |
| 2512 | } |
| 2513 | |
| 2514 | if (!cow) |
| 2515 | write_lock_level = -1; |
| 2516 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2517 | if (cow && (p->keep_locks || p->lowest_level)) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2518 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2519 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2520 | min_write_lock_level = write_lock_level; |
| 2521 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2522 | again: |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2523 | prev_cmp = -1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2524 | /* |
| 2525 | * we try very hard to do read locks on the root |
| 2526 | */ |
| 2527 | root_lock = BTRFS_READ_LOCK; |
| 2528 | level = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2529 | if (p->search_commit_root) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2530 | /* |
| 2531 | * the commit roots are read only |
| 2532 | * so we always do read locks |
| 2533 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2534 | b = root->commit_root; |
| 2535 | extent_buffer_get(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2536 | level = btrfs_header_level(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2537 | if (!p->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2538 | btrfs_tree_read_lock(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2539 | } else { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2540 | if (p->skip_locking) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2541 | b = btrfs_root_node(root); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2542 | level = btrfs_header_level(b); |
| 2543 | } else { |
| 2544 | /* we don't know the level of the root node |
| 2545 | * until we actually have it read locked |
| 2546 | */ |
| 2547 | b = btrfs_read_lock_root_node(root); |
| 2548 | level = btrfs_header_level(b); |
| 2549 | if (level <= write_lock_level) { |
| 2550 | /* whoops, must trade for write lock */ |
| 2551 | btrfs_tree_read_unlock(b); |
| 2552 | free_extent_buffer(b); |
| 2553 | b = btrfs_lock_root_node(root); |
| 2554 | root_lock = BTRFS_WRITE_LOCK; |
| 2555 | |
| 2556 | /* the level might have changed, check again */ |
| 2557 | level = btrfs_header_level(b); |
| 2558 | } |
| 2559 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2560 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2561 | p->nodes[level] = b; |
| 2562 | if (!p->skip_locking) |
| 2563 | p->locks[level] = root_lock; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2564 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2565 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2566 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2567 | |
| 2568 | /* |
| 2569 | * setup the path here so we can release it under lock |
| 2570 | * contention with the cow code |
| 2571 | */ |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2572 | if (cow) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2573 | /* |
| 2574 | * if we don't really need to cow this block |
| 2575 | * then we don't want to set the path blocking, |
| 2576 | * so we test it here |
| 2577 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2578 | if (!should_cow_block(trans, root, b)) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2579 | goto cow_done; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2580 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2581 | btrfs_set_path_blocking(p); |
| 2582 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2583 | /* |
| 2584 | * must have write locks on this node and the |
| 2585 | * parent |
| 2586 | */ |
Josef Bacik | 5124e00 | 2012-11-07 13:44:13 -0500 | [diff] [blame] | 2587 | if (level > write_lock_level || |
| 2588 | (level + 1 > write_lock_level && |
| 2589 | level + 1 < BTRFS_MAX_LEVEL && |
| 2590 | p->nodes[level + 1])) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2591 | write_lock_level = level + 1; |
| 2592 | btrfs_release_path(p); |
| 2593 | goto again; |
| 2594 | } |
| 2595 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2596 | err = btrfs_cow_block(trans, root, b, |
| 2597 | p->nodes[level + 1], |
| 2598 | p->slots[level + 1], &b); |
| 2599 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2600 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2601 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2602 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2603 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2604 | cow_done: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2605 | BUG_ON(!cow && ins_len); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2606 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2607 | p->nodes[level] = b; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2608 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2609 | |
| 2610 | /* |
| 2611 | * we have a lock on b and as long as we aren't changing |
| 2612 | * the tree, there is no way to for the items in b to change. |
| 2613 | * It is safe to drop the lock on our parent before we |
| 2614 | * go through the expensive btree search on b. |
| 2615 | * |
| 2616 | * If cow is true, then we might be changing slot zero, |
| 2617 | * which may require changing the parent. So, we can't |
| 2618 | * drop the lock until after we know which slot we're |
| 2619 | * operating on. |
| 2620 | */ |
| 2621 | if (!cow) |
| 2622 | btrfs_unlock_up_safe(p, level + 1); |
| 2623 | |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2624 | ret = key_search(b, key, level, &prev_cmp, &slot); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2625 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2626 | if (level != 0) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2627 | int dec = 0; |
| 2628 | if (ret && slot > 0) { |
| 2629 | dec = 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2630 | slot -= 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2631 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2632 | p->slots[level] = slot; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2633 | err = setup_nodes_for_search(trans, root, p, b, level, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2634 | ins_len, &write_lock_level); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2635 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2636 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2637 | if (err) { |
| 2638 | ret = err; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2639 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2640 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2641 | b = p->nodes[level]; |
| 2642 | slot = p->slots[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2643 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2644 | /* |
| 2645 | * slot 0 is special, if we change the key |
| 2646 | * we have to update the parent pointer |
| 2647 | * which means we must have a write lock |
| 2648 | * on the parent |
| 2649 | */ |
| 2650 | if (slot == 0 && cow && |
| 2651 | write_lock_level < level + 1) { |
| 2652 | write_lock_level = level + 1; |
| 2653 | btrfs_release_path(p); |
| 2654 | goto again; |
| 2655 | } |
| 2656 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2657 | unlock_up(p, level, lowest_unlock, |
| 2658 | min_write_lock_level, &write_lock_level); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 2659 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2660 | if (level == lowest_level) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2661 | if (dec) |
| 2662 | p->slots[level]++; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2663 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2664 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2665 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2666 | err = read_block_for_search(trans, root, p, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2667 | &b, level, slot, key, 0); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2668 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2669 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2670 | if (err) { |
| 2671 | ret = err; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2672 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2673 | } |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2674 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2675 | if (!p->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2676 | level = btrfs_header_level(b); |
| 2677 | if (level <= write_lock_level) { |
| 2678 | err = btrfs_try_tree_write_lock(b); |
| 2679 | if (!err) { |
| 2680 | btrfs_set_path_blocking(p); |
| 2681 | btrfs_tree_lock(b); |
| 2682 | btrfs_clear_path_blocking(p, b, |
| 2683 | BTRFS_WRITE_LOCK); |
| 2684 | } |
| 2685 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2686 | } else { |
| 2687 | err = btrfs_try_tree_read_lock(b); |
| 2688 | if (!err) { |
| 2689 | btrfs_set_path_blocking(p); |
| 2690 | btrfs_tree_read_lock(b); |
| 2691 | btrfs_clear_path_blocking(p, b, |
| 2692 | BTRFS_READ_LOCK); |
| 2693 | } |
| 2694 | p->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2695 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2696 | p->nodes[level] = b; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2697 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2698 | } else { |
| 2699 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2700 | if (ins_len > 0 && |
| 2701 | btrfs_leaf_free_space(root, b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2702 | if (write_lock_level < 1) { |
| 2703 | write_lock_level = 1; |
| 2704 | btrfs_release_path(p); |
| 2705 | goto again; |
| 2706 | } |
| 2707 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2708 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2709 | err = split_leaf(trans, root, key, |
| 2710 | p, ins_len, ret == 0); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2711 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2712 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2713 | BUG_ON(err > 0); |
| 2714 | if (err) { |
| 2715 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2716 | goto done; |
| 2717 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2718 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2719 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2720 | unlock_up(p, level, lowest_unlock, |
| 2721 | min_write_lock_level, &write_lock_level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2722 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2723 | } |
| 2724 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2725 | ret = 1; |
| 2726 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2727 | /* |
| 2728 | * we don't really know what they plan on doing with the path |
| 2729 | * from here on, so for now just mark it as blocking |
| 2730 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2731 | if (!p->leave_spinning) |
| 2732 | btrfs_set_path_blocking(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2733 | if (ret < 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2734 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2735 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2736 | } |
| 2737 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2738 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2739 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2740 | * current state of the tree together with the operations recorded in the tree |
| 2741 | * modification log to search for the key in a previous version of this tree, as |
| 2742 | * denoted by the time_seq parameter. |
| 2743 | * |
| 2744 | * Naturally, there is no support for insert, delete or cow operations. |
| 2745 | * |
| 2746 | * The resulting path and return value will be set up as if we called |
| 2747 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2748 | */ |
| 2749 | int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 2750 | struct btrfs_path *p, u64 time_seq) |
| 2751 | { |
| 2752 | struct extent_buffer *b; |
| 2753 | int slot; |
| 2754 | int ret; |
| 2755 | int err; |
| 2756 | int level; |
| 2757 | int lowest_unlock = 1; |
| 2758 | u8 lowest_level = 0; |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame] | 2759 | int prev_cmp = -1; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2760 | |
| 2761 | lowest_level = p->lowest_level; |
| 2762 | WARN_ON(p->nodes[0] != NULL); |
| 2763 | |
| 2764 | if (p->search_commit_root) { |
| 2765 | BUG_ON(time_seq); |
| 2766 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2767 | } |
| 2768 | |
| 2769 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2770 | b = get_old_root(root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2771 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2772 | p->locks[level] = BTRFS_READ_LOCK; |
| 2773 | |
| 2774 | while (b) { |
| 2775 | level = btrfs_header_level(b); |
| 2776 | p->nodes[level] = b; |
| 2777 | btrfs_clear_path_blocking(p, NULL, 0); |
| 2778 | |
| 2779 | /* |
| 2780 | * we have a lock on b and as long as we aren't changing |
| 2781 | * the tree, there is no way to for the items in b to change. |
| 2782 | * It is safe to drop the lock on our parent before we |
| 2783 | * go through the expensive btree search on b. |
| 2784 | */ |
| 2785 | btrfs_unlock_up_safe(p, level + 1); |
| 2786 | |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame] | 2787 | /* |
| 2788 | * Since we can unwind eb's we want to do a real search every |
| 2789 | * time. |
| 2790 | */ |
| 2791 | prev_cmp = -1; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2792 | ret = key_search(b, key, level, &prev_cmp, &slot); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2793 | |
| 2794 | if (level != 0) { |
| 2795 | int dec = 0; |
| 2796 | if (ret && slot > 0) { |
| 2797 | dec = 1; |
| 2798 | slot -= 1; |
| 2799 | } |
| 2800 | p->slots[level] = slot; |
| 2801 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2802 | |
| 2803 | if (level == lowest_level) { |
| 2804 | if (dec) |
| 2805 | p->slots[level]++; |
| 2806 | goto done; |
| 2807 | } |
| 2808 | |
| 2809 | err = read_block_for_search(NULL, root, p, &b, level, |
| 2810 | slot, key, time_seq); |
| 2811 | if (err == -EAGAIN) |
| 2812 | goto again; |
| 2813 | if (err) { |
| 2814 | ret = err; |
| 2815 | goto done; |
| 2816 | } |
| 2817 | |
| 2818 | level = btrfs_header_level(b); |
| 2819 | err = btrfs_try_tree_read_lock(b); |
| 2820 | if (!err) { |
| 2821 | btrfs_set_path_blocking(p); |
| 2822 | btrfs_tree_read_lock(b); |
| 2823 | btrfs_clear_path_blocking(p, b, |
| 2824 | BTRFS_READ_LOCK); |
| 2825 | } |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 2826 | b = tree_mod_log_rewind(root->fs_info, p, b, time_seq); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 2827 | if (!b) { |
| 2828 | ret = -ENOMEM; |
| 2829 | goto done; |
| 2830 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2831 | p->locks[level] = BTRFS_READ_LOCK; |
| 2832 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2833 | } else { |
| 2834 | p->slots[level] = slot; |
| 2835 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2836 | goto done; |
| 2837 | } |
| 2838 | } |
| 2839 | ret = 1; |
| 2840 | done: |
| 2841 | if (!p->leave_spinning) |
| 2842 | btrfs_set_path_blocking(p); |
| 2843 | if (ret < 0) |
| 2844 | btrfs_release_path(p); |
| 2845 | |
| 2846 | return ret; |
| 2847 | } |
| 2848 | |
| 2849 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2850 | * helper to use instead of search slot if no exact match is needed but |
| 2851 | * instead the next or previous item should be returned. |
| 2852 | * When find_higher is true, the next higher item is returned, the next lower |
| 2853 | * otherwise. |
| 2854 | * When return_any and find_higher are both true, and no higher item is found, |
| 2855 | * return the next lower instead. |
| 2856 | * When return_any is true and find_higher is false, and no lower item is found, |
| 2857 | * return the next higher instead. |
| 2858 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 2859 | * < 0 on error |
| 2860 | */ |
| 2861 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
| 2862 | struct btrfs_key *key, struct btrfs_path *p, |
| 2863 | int find_higher, int return_any) |
| 2864 | { |
| 2865 | int ret; |
| 2866 | struct extent_buffer *leaf; |
| 2867 | |
| 2868 | again: |
| 2869 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2870 | if (ret <= 0) |
| 2871 | return ret; |
| 2872 | /* |
| 2873 | * a return value of 1 means the path is at the position where the |
| 2874 | * item should be inserted. Normally this is the next bigger item, |
| 2875 | * but in case the previous item is the last in a leaf, path points |
| 2876 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 2877 | * item. |
| 2878 | */ |
| 2879 | leaf = p->nodes[0]; |
| 2880 | |
| 2881 | if (find_higher) { |
| 2882 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2883 | ret = btrfs_next_leaf(root, p); |
| 2884 | if (ret <= 0) |
| 2885 | return ret; |
| 2886 | if (!return_any) |
| 2887 | return 1; |
| 2888 | /* |
| 2889 | * no higher item found, return the next |
| 2890 | * lower instead |
| 2891 | */ |
| 2892 | return_any = 0; |
| 2893 | find_higher = 0; |
| 2894 | btrfs_release_path(p); |
| 2895 | goto again; |
| 2896 | } |
| 2897 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2898 | if (p->slots[0] == 0) { |
| 2899 | ret = btrfs_prev_leaf(root, p); |
| 2900 | if (ret < 0) |
| 2901 | return ret; |
| 2902 | if (!ret) { |
| 2903 | p->slots[0] = btrfs_header_nritems(leaf) - 1; |
| 2904 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2905 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2906 | if (!return_any) |
| 2907 | return 1; |
| 2908 | /* |
| 2909 | * no lower item found, return the next |
| 2910 | * higher instead |
| 2911 | */ |
| 2912 | return_any = 0; |
| 2913 | find_higher = 1; |
| 2914 | btrfs_release_path(p); |
| 2915 | goto again; |
| 2916 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2917 | --p->slots[0]; |
| 2918 | } |
| 2919 | } |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
| 2923 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2924 | * adjust the pointers going up the tree, starting at level |
| 2925 | * making sure the right key of each node is points to 'key'. |
| 2926 | * This is used after shifting pointers to the left, so it stops |
| 2927 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 2928 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2929 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2930 | */ |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 2931 | static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2932 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2933 | { |
| 2934 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2935 | struct extent_buffer *t; |
| 2936 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2937 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2938 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2939 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2940 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2941 | t = path->nodes[i]; |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2942 | tree_mod_log_set_node_key(root->fs_info, t, tslot, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2943 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2944 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2945 | if (tslot != 0) |
| 2946 | break; |
| 2947 | } |
| 2948 | } |
| 2949 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2950 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2951 | * update item key. |
| 2952 | * |
| 2953 | * This function isn't completely safe. It's the caller's responsibility |
| 2954 | * that the new key won't break the order |
| 2955 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 2956 | void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2957 | struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2958 | { |
| 2959 | struct btrfs_disk_key disk_key; |
| 2960 | struct extent_buffer *eb; |
| 2961 | int slot; |
| 2962 | |
| 2963 | eb = path->nodes[0]; |
| 2964 | slot = path->slots[0]; |
| 2965 | if (slot > 0) { |
| 2966 | btrfs_item_key(eb, &disk_key, slot - 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2967 | BUG_ON(comp_keys(&disk_key, new_key) >= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2968 | } |
| 2969 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 2970 | btrfs_item_key(eb, &disk_key, slot + 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2971 | BUG_ON(comp_keys(&disk_key, new_key) <= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 2975 | btrfs_set_item_key(eb, &disk_key, slot); |
| 2976 | btrfs_mark_buffer_dirty(eb); |
| 2977 | if (slot == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 2978 | fixup_low_keys(root, path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2982 | * 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] | 2983 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2984 | * |
| 2985 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 2986 | * 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] | 2987 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2988 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 2989 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2990 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2991 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2992 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2993 | int src_nritems; |
| 2994 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2995 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2996 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2997 | src_nritems = btrfs_header_nritems(src); |
| 2998 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2999 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3000 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3001 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3002 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3003 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3004 | return 1; |
| 3005 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3006 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3007 | return 1; |
| 3008 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3009 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3010 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3011 | if (push_items < src_nritems) { |
| 3012 | /* leave at least 8 pointers in the node if |
| 3013 | * we aren't going to empty it |
| 3014 | */ |
| 3015 | if (src_nritems - push_items < 8) { |
| 3016 | if (push_items <= 8) |
| 3017 | return 1; |
| 3018 | push_items -= 8; |
| 3019 | } |
| 3020 | } |
| 3021 | } else |
| 3022 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3023 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3024 | tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3025 | push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3026 | copy_extent_buffer(dst, src, |
| 3027 | btrfs_node_key_ptr_offset(dst_nritems), |
| 3028 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3029 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3030 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3031 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3032 | /* |
| 3033 | * don't call tree_mod_log_eb_move here, key removal was already |
| 3034 | * fully logged by tree_mod_log_eb_copy above. |
| 3035 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3036 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 3037 | btrfs_node_key_ptr_offset(push_items), |
| 3038 | (src_nritems - push_items) * |
| 3039 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3040 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3041 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3042 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 3043 | btrfs_mark_buffer_dirty(src); |
| 3044 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3045 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3046 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3047 | } |
| 3048 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3049 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3050 | * try to push data from one node into the next node right in the |
| 3051 | * tree. |
| 3052 | * |
| 3053 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 3054 | * error, and > 0 if there was no room in the right hand block. |
| 3055 | * |
| 3056 | * this will only push up to 1/2 the contents of the left node over |
| 3057 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3058 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 3059 | struct btrfs_root *root, |
| 3060 | struct extent_buffer *dst, |
| 3061 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3062 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3063 | int push_items = 0; |
| 3064 | int max_push; |
| 3065 | int src_nritems; |
| 3066 | int dst_nritems; |
| 3067 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3068 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3069 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3070 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3071 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3072 | src_nritems = btrfs_header_nritems(src); |
| 3073 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3074 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3075 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3076 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3077 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3078 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3079 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3080 | |
| 3081 | max_push = src_nritems / 2 + 1; |
| 3082 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3083 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3084 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3085 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3086 | if (max_push < push_items) |
| 3087 | push_items = max_push; |
| 3088 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3089 | tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3090 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3091 | btrfs_node_key_ptr_offset(0), |
| 3092 | (dst_nritems) * |
| 3093 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3094 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3095 | tree_mod_log_eb_copy(root->fs_info, dst, src, 0, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3096 | src_nritems - push_items, push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3097 | copy_extent_buffer(dst, src, |
| 3098 | btrfs_node_key_ptr_offset(0), |
| 3099 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3100 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3101 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3102 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3103 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3104 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3105 | btrfs_mark_buffer_dirty(src); |
| 3106 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3107 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3108 | return ret; |
| 3109 | } |
| 3110 | |
| 3111 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3112 | * helper function to insert a new root level in the tree. |
| 3113 | * A new node is allocated, and a single item is inserted to |
| 3114 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3115 | * |
| 3116 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3117 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3118 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3119 | struct btrfs_root *root, |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3120 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3121 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3122 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3123 | struct extent_buffer *lower; |
| 3124 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3125 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3126 | struct btrfs_disk_key lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3127 | |
| 3128 | BUG_ON(path->nodes[level]); |
| 3129 | BUG_ON(path->nodes[level-1] != root->node); |
| 3130 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3131 | lower = path->nodes[level-1]; |
| 3132 | if (level == 1) |
| 3133 | btrfs_item_key(lower, &lower_key, 0); |
| 3134 | else |
| 3135 | btrfs_node_key(lower, &lower_key, 0); |
| 3136 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3137 | c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3138 | root->root_key.objectid, &lower_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3139 | level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3140 | if (IS_ERR(c)) |
| 3141 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3142 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3143 | root_add_used(root, root->nodesize); |
| 3144 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3145 | memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3146 | btrfs_set_header_nritems(c, 1); |
| 3147 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3148 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3149 | btrfs_set_header_generation(c, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3150 | btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3151 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3152 | |
Ross Kirk | 0a4e558 | 2013-09-24 10:12:38 +0100 | [diff] [blame] | 3153 | write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3154 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3155 | |
| 3156 | write_extent_buffer(c, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | b308bc2 | 2013-08-20 13:20:15 +0200 | [diff] [blame] | 3157 | btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3158 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3159 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3160 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3161 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3162 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3163 | |
| 3164 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3165 | |
| 3166 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3167 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3168 | old = root->node; |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3169 | tree_mod_log_set_root_pointer(root, c, 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3170 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3171 | |
| 3172 | /* the super has an extra ref to root->node */ |
| 3173 | free_extent_buffer(old); |
| 3174 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3175 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3176 | extent_buffer_get(c); |
| 3177 | path->nodes[level] = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 3178 | path->locks[level] = BTRFS_WRITE_LOCK; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3179 | path->slots[level] = 0; |
| 3180 | return 0; |
| 3181 | } |
| 3182 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3183 | /* |
| 3184 | * worker function to insert a single pointer in a node. |
| 3185 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3186 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3187 | * slot and level indicate where you want the key to go, and |
| 3188 | * blocknr is the block the key points to. |
| 3189 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3190 | static void insert_ptr(struct btrfs_trans_handle *trans, |
| 3191 | struct btrfs_root *root, struct btrfs_path *path, |
| 3192 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3193 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3194 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3195 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3196 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3197 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3198 | |
| 3199 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3200 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3201 | lower = path->nodes[level]; |
| 3202 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3203 | BUG_ON(slot > nritems); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3204 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3205 | if (slot != nritems) { |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3206 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3207 | tree_mod_log_eb_move(root->fs_info, lower, slot + 1, |
| 3208 | slot, nritems - slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3209 | memmove_extent_buffer(lower, |
| 3210 | btrfs_node_key_ptr_offset(slot + 1), |
| 3211 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3212 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3213 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3214 | if (level) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3215 | ret = tree_mod_log_insert_key(root->fs_info, lower, slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 3216 | MOD_LOG_KEY_ADD, GFP_NOFS); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3217 | BUG_ON(ret < 0); |
| 3218 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3219 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3220 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3221 | WARN_ON(trans->transid == 0); |
| 3222 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3223 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3224 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3225 | } |
| 3226 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3227 | /* |
| 3228 | * split the node at the specified level in path in two. |
| 3229 | * The path is corrected to point to the appropriate node after the split |
| 3230 | * |
| 3231 | * Before splitting this tries to make some room in the node by pushing |
| 3232 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3233 | * |
| 3234 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3235 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3236 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3237 | struct btrfs_root *root, |
| 3238 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3239 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3240 | struct extent_buffer *c; |
| 3241 | struct extent_buffer *split; |
| 3242 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3243 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3244 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3245 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3246 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3247 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3248 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3249 | if (c == root->node) { |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3250 | /* |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3251 | * trying to split the root, lets make a new one |
| 3252 | * |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3253 | * tree mod log: We don't log_removal old root in |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3254 | * insert_new_root, because that root buffer will be kept as a |
| 3255 | * normal node. We are going to log removal of half of the |
| 3256 | * elements below with tree_mod_log_eb_copy. We're holding a |
| 3257 | * tree lock on the buffer, which is why we cannot race with |
| 3258 | * other tree_mod_log users. |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3259 | */ |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3260 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3261 | if (ret) |
| 3262 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3263 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3264 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3265 | c = path->nodes[level]; |
| 3266 | if (!ret && btrfs_header_nritems(c) < |
Chris Mason | c448acf | 2008-04-24 09:34:34 -0400 | [diff] [blame] | 3267 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3268 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3269 | if (ret < 0) |
| 3270 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3271 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3272 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3273 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3274 | mid = (c_nritems + 1) / 2; |
| 3275 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3276 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3277 | split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3278 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3279 | &disk_key, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3280 | if (IS_ERR(split)) |
| 3281 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3282 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3283 | root_add_used(root, root->nodesize); |
| 3284 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3285 | memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3286 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3287 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3288 | btrfs_set_header_generation(split, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3289 | btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3290 | btrfs_set_header_owner(split, root->root_key.objectid); |
| 3291 | write_extent_buffer(split, root->fs_info->fsid, |
Ross Kirk | 0a4e558 | 2013-09-24 10:12:38 +0100 | [diff] [blame] | 3292 | btrfs_header_fsid(), BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3293 | write_extent_buffer(split, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | b308bc2 | 2013-08-20 13:20:15 +0200 | [diff] [blame] | 3294 | btrfs_header_chunk_tree_uuid(split), |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3295 | BTRFS_UUID_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3296 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3297 | tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3298 | copy_extent_buffer(split, c, |
| 3299 | btrfs_node_key_ptr_offset(0), |
| 3300 | btrfs_node_key_ptr_offset(mid), |
| 3301 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3302 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3303 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3304 | ret = 0; |
| 3305 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3306 | btrfs_mark_buffer_dirty(c); |
| 3307 | btrfs_mark_buffer_dirty(split); |
| 3308 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3309 | insert_ptr(trans, root, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3310 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3311 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3312 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3313 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3314 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3315 | free_extent_buffer(c); |
| 3316 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3317 | path->slots[level + 1] += 1; |
| 3318 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3319 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3320 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3321 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3322 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3323 | } |
| 3324 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3325 | /* |
| 3326 | * how many bytes are required to store the items in a leaf. start |
| 3327 | * and nr indicate which items in the leaf to check. This totals up the |
| 3328 | * space used both by the item structs and the item data |
| 3329 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3330 | 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] | 3331 | { |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3332 | struct btrfs_item *start_item; |
| 3333 | struct btrfs_item *end_item; |
| 3334 | struct btrfs_map_token token; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3335 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3336 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3337 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3338 | |
| 3339 | if (!nr) |
| 3340 | return 0; |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3341 | btrfs_init_map_token(&token); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3342 | start_item = btrfs_item_nr(start); |
| 3343 | end_item = btrfs_item_nr(end); |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3344 | data_len = btrfs_token_item_offset(l, start_item, &token) + |
| 3345 | btrfs_token_item_size(l, start_item, &token); |
| 3346 | data_len = data_len - btrfs_token_item_offset(l, end_item, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3347 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3348 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3349 | return data_len; |
| 3350 | } |
| 3351 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3352 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3353 | * The space between the end of the leaf items and |
| 3354 | * the start of the leaf data. IOW, how much room |
| 3355 | * the leaf has left for both items and data |
| 3356 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3357 | noinline int btrfs_leaf_free_space(struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3358 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3359 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3360 | int nritems = btrfs_header_nritems(leaf); |
| 3361 | int ret; |
| 3362 | ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 3363 | if (ret < 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3364 | printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " |
| 3365 | "used %d nritems %d\n", |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 3366 | ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3367 | leaf_space_used(leaf, 0, nritems), nritems); |
| 3368 | } |
| 3369 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3370 | } |
| 3371 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3372 | /* |
| 3373 | * min slot controls the lowest index we're willing to push to the |
| 3374 | * right. We'll push up to and including min_slot, but no lower |
| 3375 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3376 | static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, |
| 3377 | struct btrfs_root *root, |
| 3378 | struct btrfs_path *path, |
| 3379 | int data_size, int empty, |
| 3380 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3381 | int free_space, u32 left_nritems, |
| 3382 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3383 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3384 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3385 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3386 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3387 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3388 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3389 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3390 | int push_space = 0; |
| 3391 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3392 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3393 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3394 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3395 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3396 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3397 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3398 | btrfs_init_map_token(&token); |
| 3399 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3400 | if (empty) |
| 3401 | nr = 0; |
| 3402 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3403 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3404 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3405 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3406 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3407 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3408 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3409 | i = left_nritems - 1; |
| 3410 | while (i >= nr) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3411 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3412 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3413 | if (!empty && push_items > 0) { |
| 3414 | if (path->slots[0] > i) |
| 3415 | break; |
| 3416 | if (path->slots[0] == i) { |
| 3417 | int space = btrfs_leaf_free_space(root, left); |
| 3418 | if (space + push_space * 2 > free_space) |
| 3419 | break; |
| 3420 | } |
| 3421 | } |
| 3422 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3423 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3424 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3425 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3426 | this_item_size = btrfs_item_size(left, item); |
| 3427 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3428 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3429 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3430 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3431 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3432 | if (i == 0) |
| 3433 | break; |
| 3434 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3435 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3436 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3437 | if (push_items == 0) |
| 3438 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3439 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 3440 | WARN_ON(!empty && push_items == left_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3441 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3442 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3443 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3444 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3445 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3446 | push_space -= leaf_data_end(root, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3447 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3448 | /* make room in the right data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3449 | data_end = leaf_data_end(root, right); |
| 3450 | memmove_extent_buffer(right, |
| 3451 | btrfs_leaf_data(right) + data_end - push_space, |
| 3452 | btrfs_leaf_data(right) + data_end, |
| 3453 | BTRFS_LEAF_DATA_SIZE(root) - data_end); |
| 3454 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3455 | /* copy from the left data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3456 | copy_extent_buffer(right, left, btrfs_leaf_data(right) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3457 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3458 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 3459 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3460 | |
| 3461 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3462 | btrfs_item_nr_offset(0), |
| 3463 | right_nritems * sizeof(struct btrfs_item)); |
| 3464 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3465 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3466 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3467 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3468 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3469 | |
| 3470 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3471 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3472 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3473 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3474 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3475 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3476 | push_space -= btrfs_token_item_size(right, item, &token); |
| 3477 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3478 | } |
| 3479 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3480 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3481 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3482 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3483 | if (left_nritems) |
| 3484 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3485 | else |
| 3486 | clean_tree_block(trans, root, left); |
| 3487 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3488 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3489 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3490 | btrfs_item_key(right, &disk_key, 0); |
| 3491 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3492 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3493 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3494 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3495 | if (path->slots[0] >= left_nritems) { |
| 3496 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3497 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 3498 | clean_tree_block(trans, root, path->nodes[0]); |
| 3499 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3500 | free_extent_buffer(path->nodes[0]); |
| 3501 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3502 | path->slots[1] += 1; |
| 3503 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3504 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3505 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3506 | } |
| 3507 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3508 | |
| 3509 | out_unlock: |
| 3510 | btrfs_tree_unlock(right); |
| 3511 | free_extent_buffer(right); |
| 3512 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3513 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3514 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3515 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3516 | * push some data in the path leaf to the right, trying to free up at |
| 3517 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3518 | * |
| 3519 | * returns 1 if the push failed because the other node didn't have enough |
| 3520 | * 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] | 3521 | * |
| 3522 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3523 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3524 | */ |
| 3525 | 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] | 3526 | *root, struct btrfs_path *path, |
| 3527 | int min_data_size, int data_size, |
| 3528 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3529 | { |
| 3530 | struct extent_buffer *left = path->nodes[0]; |
| 3531 | struct extent_buffer *right; |
| 3532 | struct extent_buffer *upper; |
| 3533 | int slot; |
| 3534 | int free_space; |
| 3535 | u32 left_nritems; |
| 3536 | int ret; |
| 3537 | |
| 3538 | if (!path->nodes[1]) |
| 3539 | return 1; |
| 3540 | |
| 3541 | slot = path->slots[1]; |
| 3542 | upper = path->nodes[1]; |
| 3543 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3544 | return 1; |
| 3545 | |
| 3546 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3547 | |
| 3548 | right = read_node_slot(root, upper, slot + 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3549 | if (right == NULL) |
| 3550 | return 1; |
| 3551 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3552 | btrfs_tree_lock(right); |
| 3553 | btrfs_set_lock_blocking(right); |
| 3554 | |
| 3555 | free_space = btrfs_leaf_free_space(root, right); |
| 3556 | if (free_space < data_size) |
| 3557 | goto out_unlock; |
| 3558 | |
| 3559 | /* cow and double check */ |
| 3560 | ret = btrfs_cow_block(trans, root, right, upper, |
| 3561 | slot + 1, &right); |
| 3562 | if (ret) |
| 3563 | goto out_unlock; |
| 3564 | |
| 3565 | free_space = btrfs_leaf_free_space(root, right); |
| 3566 | if (free_space < data_size) |
| 3567 | goto out_unlock; |
| 3568 | |
| 3569 | left_nritems = btrfs_header_nritems(left); |
| 3570 | if (left_nritems == 0) |
| 3571 | goto out_unlock; |
| 3572 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3573 | return __push_leaf_right(trans, root, path, min_data_size, empty, |
| 3574 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3575 | out_unlock: |
| 3576 | btrfs_tree_unlock(right); |
| 3577 | free_extent_buffer(right); |
| 3578 | return 1; |
| 3579 | } |
| 3580 | |
| 3581 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3582 | * push some data in the path leaf to the left, trying to free up at |
| 3583 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3584 | * |
| 3585 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3586 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3587 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3588 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3589 | static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, |
| 3590 | struct btrfs_root *root, |
| 3591 | struct btrfs_path *path, int data_size, |
| 3592 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3593 | int free_space, u32 right_nritems, |
| 3594 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3595 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3596 | struct btrfs_disk_key disk_key; |
| 3597 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3598 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3599 | int push_space = 0; |
| 3600 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3601 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3602 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3603 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3604 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3605 | u32 this_item_size; |
| 3606 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3607 | struct btrfs_map_token token; |
| 3608 | |
| 3609 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3610 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3611 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3612 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3613 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3614 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3615 | |
| 3616 | for (i = 0; i < nr; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3617 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3618 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3619 | if (!empty && push_items > 0) { |
| 3620 | if (path->slots[0] < i) |
| 3621 | break; |
| 3622 | if (path->slots[0] == i) { |
| 3623 | int space = btrfs_leaf_free_space(root, right); |
| 3624 | if (space + push_space * 2 > free_space) |
| 3625 | break; |
| 3626 | } |
| 3627 | } |
| 3628 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3629 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3630 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3631 | |
| 3632 | this_item_size = btrfs_item_size(right, item); |
| 3633 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3634 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3635 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3636 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3637 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3638 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3639 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3640 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3641 | ret = 1; |
| 3642 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3643 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3644 | WARN_ON(!empty && push_items == btrfs_header_nritems(right)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3645 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3646 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3647 | copy_extent_buffer(left, right, |
| 3648 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3649 | btrfs_item_nr_offset(0), |
| 3650 | push_items * sizeof(struct btrfs_item)); |
| 3651 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3652 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3653 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3654 | |
| 3655 | copy_extent_buffer(left, right, btrfs_leaf_data(left) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3656 | leaf_data_end(root, left) - push_space, |
| 3657 | btrfs_leaf_data(right) + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3658 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3659 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3660 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3661 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3662 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3663 | 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] | 3664 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3665 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3666 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3667 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3668 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3669 | ioff = btrfs_token_item_offset(left, item, &token); |
| 3670 | btrfs_set_token_item_offset(left, item, |
| 3671 | ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size), |
| 3672 | &token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3673 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3674 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3675 | |
| 3676 | /* fixup right node */ |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3677 | if (push_items > right_nritems) |
| 3678 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3679 | right_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3680 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3681 | if (push_items < right_nritems) { |
| 3682 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
| 3683 | leaf_data_end(root, right); |
| 3684 | memmove_extent_buffer(right, btrfs_leaf_data(right) + |
| 3685 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3686 | btrfs_leaf_data(right) + |
| 3687 | leaf_data_end(root, right), push_space); |
| 3688 | |
| 3689 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3690 | btrfs_item_nr_offset(push_items), |
| 3691 | (btrfs_header_nritems(right) - push_items) * |
| 3692 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3693 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3694 | right_nritems -= push_items; |
| 3695 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3696 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3697 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3698 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3699 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3700 | push_space = push_space - btrfs_token_item_size(right, |
| 3701 | item, &token); |
| 3702 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3703 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3704 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3705 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3706 | if (right_nritems) |
| 3707 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3708 | else |
| 3709 | clean_tree_block(trans, root, right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3710 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3711 | btrfs_item_key(right, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 3712 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3713 | |
| 3714 | /* then fixup the leaf pointer in the path */ |
| 3715 | if (path->slots[0] < push_items) { |
| 3716 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3717 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3718 | free_extent_buffer(path->nodes[0]); |
| 3719 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3720 | path->slots[1] -= 1; |
| 3721 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3722 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3723 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3724 | path->slots[0] -= push_items; |
| 3725 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3726 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3727 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3728 | out: |
| 3729 | btrfs_tree_unlock(left); |
| 3730 | free_extent_buffer(left); |
| 3731 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3732 | } |
| 3733 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3734 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3735 | * push some data in the path leaf to the left, trying to free up at |
| 3736 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3737 | * |
| 3738 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3739 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3740 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3741 | */ |
| 3742 | 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] | 3743 | *root, struct btrfs_path *path, int min_data_size, |
| 3744 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3745 | { |
| 3746 | struct extent_buffer *right = path->nodes[0]; |
| 3747 | struct extent_buffer *left; |
| 3748 | int slot; |
| 3749 | int free_space; |
| 3750 | u32 right_nritems; |
| 3751 | int ret = 0; |
| 3752 | |
| 3753 | slot = path->slots[1]; |
| 3754 | if (slot == 0) |
| 3755 | return 1; |
| 3756 | if (!path->nodes[1]) |
| 3757 | return 1; |
| 3758 | |
| 3759 | right_nritems = btrfs_header_nritems(right); |
| 3760 | if (right_nritems == 0) |
| 3761 | return 1; |
| 3762 | |
| 3763 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3764 | |
| 3765 | left = read_node_slot(root, path->nodes[1], slot - 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3766 | if (left == NULL) |
| 3767 | return 1; |
| 3768 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3769 | btrfs_tree_lock(left); |
| 3770 | btrfs_set_lock_blocking(left); |
| 3771 | |
| 3772 | free_space = btrfs_leaf_free_space(root, left); |
| 3773 | if (free_space < data_size) { |
| 3774 | ret = 1; |
| 3775 | goto out; |
| 3776 | } |
| 3777 | |
| 3778 | /* cow and double check */ |
| 3779 | ret = btrfs_cow_block(trans, root, left, |
| 3780 | path->nodes[1], slot - 1, &left); |
| 3781 | if (ret) { |
| 3782 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3783 | if (ret == -ENOSPC) |
| 3784 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3785 | goto out; |
| 3786 | } |
| 3787 | |
| 3788 | free_space = btrfs_leaf_free_space(root, left); |
| 3789 | if (free_space < data_size) { |
| 3790 | ret = 1; |
| 3791 | goto out; |
| 3792 | } |
| 3793 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3794 | return __push_leaf_left(trans, root, path, min_data_size, |
| 3795 | empty, left, free_space, right_nritems, |
| 3796 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3797 | out: |
| 3798 | btrfs_tree_unlock(left); |
| 3799 | free_extent_buffer(left); |
| 3800 | return ret; |
| 3801 | } |
| 3802 | |
| 3803 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3804 | * split the path's leaf in two, making sure there is at least data_size |
| 3805 | * available for the resulting leaf level of the path. |
| 3806 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3807 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
| 3808 | struct btrfs_root *root, |
| 3809 | struct btrfs_path *path, |
| 3810 | struct extent_buffer *l, |
| 3811 | struct extent_buffer *right, |
| 3812 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3813 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3814 | int data_copy_size; |
| 3815 | int rt_data_off; |
| 3816 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3817 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3818 | struct btrfs_map_token token; |
| 3819 | |
| 3820 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3821 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3822 | nritems = nritems - mid; |
| 3823 | btrfs_set_header_nritems(right, nritems); |
| 3824 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); |
| 3825 | |
| 3826 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 3827 | btrfs_item_nr_offset(mid), |
| 3828 | nritems * sizeof(struct btrfs_item)); |
| 3829 | |
| 3830 | copy_extent_buffer(right, l, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3831 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 3832 | data_copy_size, btrfs_leaf_data(l) + |
| 3833 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3834 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3835 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 3836 | btrfs_item_end_nr(l, mid); |
| 3837 | |
| 3838 | for (i = 0; i < nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3839 | struct btrfs_item *item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3840 | u32 ioff; |
| 3841 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3842 | ioff = btrfs_token_item_offset(right, item, &token); |
| 3843 | btrfs_set_token_item_offset(right, item, |
| 3844 | ioff + rt_data_off, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3845 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3846 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3847 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3848 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3849 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3850 | path->slots[1] + 1, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3851 | |
| 3852 | btrfs_mark_buffer_dirty(right); |
| 3853 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3854 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3855 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3856 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3857 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3858 | free_extent_buffer(path->nodes[0]); |
| 3859 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3860 | path->slots[0] -= mid; |
| 3861 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3862 | } else { |
| 3863 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3864 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3865 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3866 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3867 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3871 | * double splits happen when we need to insert a big item in the middle |
| 3872 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 3873 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 3874 | * A B C |
| 3875 | * |
| 3876 | * We avoid this by trying to push the items on either side of our target |
| 3877 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 3878 | * completely. |
| 3879 | */ |
| 3880 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 3881 | struct btrfs_root *root, |
| 3882 | struct btrfs_path *path, |
| 3883 | int data_size) |
| 3884 | { |
| 3885 | int ret; |
| 3886 | int progress = 0; |
| 3887 | int slot; |
| 3888 | u32 nritems; |
| 3889 | |
| 3890 | slot = path->slots[0]; |
| 3891 | |
| 3892 | /* |
| 3893 | * try to push all the items after our slot into the |
| 3894 | * right leaf |
| 3895 | */ |
| 3896 | ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); |
| 3897 | if (ret < 0) |
| 3898 | return ret; |
| 3899 | |
| 3900 | if (ret == 0) |
| 3901 | progress++; |
| 3902 | |
| 3903 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 3904 | /* |
| 3905 | * our goal is to get our slot at the start or end of a leaf. If |
| 3906 | * we've done so we're done |
| 3907 | */ |
| 3908 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 3909 | return 0; |
| 3910 | |
| 3911 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 3912 | return 0; |
| 3913 | |
| 3914 | /* try to push all the items before our slot into the next leaf */ |
| 3915 | slot = path->slots[0]; |
| 3916 | ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); |
| 3917 | if (ret < 0) |
| 3918 | return ret; |
| 3919 | |
| 3920 | if (ret == 0) |
| 3921 | progress++; |
| 3922 | |
| 3923 | if (progress) |
| 3924 | return 0; |
| 3925 | return 1; |
| 3926 | } |
| 3927 | |
| 3928 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3929 | * split the path's leaf in two, making sure there is at least data_size |
| 3930 | * available for the resulting leaf level of the path. |
| 3931 | * |
| 3932 | * returns 0 if all went well and < 0 on failure. |
| 3933 | */ |
| 3934 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 3935 | struct btrfs_root *root, |
| 3936 | struct btrfs_key *ins_key, |
| 3937 | struct btrfs_path *path, int data_size, |
| 3938 | int extend) |
| 3939 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3940 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3941 | struct extent_buffer *l; |
| 3942 | u32 nritems; |
| 3943 | int mid; |
| 3944 | int slot; |
| 3945 | struct extent_buffer *right; |
| 3946 | int ret = 0; |
| 3947 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3948 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3949 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3950 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3951 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 3952 | l = path->nodes[0]; |
| 3953 | slot = path->slots[0]; |
| 3954 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
| 3955 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) |
| 3956 | return -EOVERFLOW; |
| 3957 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3958 | /* first try to make some room by pushing left and right */ |
Liu Bo | 33157e0 | 2013-05-22 12:07:06 +0000 | [diff] [blame] | 3959 | if (data_size && path->nodes[1]) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3960 | wret = push_leaf_right(trans, root, path, data_size, |
| 3961 | data_size, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3962 | if (wret < 0) |
| 3963 | return wret; |
| 3964 | if (wret) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3965 | wret = push_leaf_left(trans, root, path, data_size, |
| 3966 | data_size, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3967 | if (wret < 0) |
| 3968 | return wret; |
| 3969 | } |
| 3970 | l = path->nodes[0]; |
| 3971 | |
| 3972 | /* did the pushes work? */ |
| 3973 | if (btrfs_leaf_free_space(root, l) >= data_size) |
| 3974 | return 0; |
| 3975 | } |
| 3976 | |
| 3977 | if (!path->nodes[1]) { |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3978 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3979 | if (ret) |
| 3980 | return ret; |
| 3981 | } |
| 3982 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3983 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3984 | l = path->nodes[0]; |
| 3985 | slot = path->slots[0]; |
| 3986 | nritems = btrfs_header_nritems(l); |
| 3987 | mid = (nritems + 1) / 2; |
| 3988 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3989 | if (mid <= slot) { |
| 3990 | if (nritems == 1 || |
| 3991 | leaf_space_used(l, mid, nritems - mid) + data_size > |
| 3992 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3993 | if (slot >= nritems) { |
| 3994 | split = 0; |
| 3995 | } else { |
| 3996 | mid = slot; |
| 3997 | if (mid != nritems && |
| 3998 | leaf_space_used(l, mid, nritems - mid) + |
| 3999 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4000 | if (data_size && !tried_avoid_double) |
| 4001 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4002 | split = 2; |
| 4003 | } |
| 4004 | } |
| 4005 | } |
| 4006 | } else { |
| 4007 | if (leaf_space_used(l, 0, mid) + data_size > |
| 4008 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 4009 | if (!extend && data_size && slot == 0) { |
| 4010 | split = 0; |
| 4011 | } else if ((extend || !data_size) && slot == 0) { |
| 4012 | mid = 1; |
| 4013 | } else { |
| 4014 | mid = slot; |
| 4015 | if (mid != nritems && |
| 4016 | leaf_space_used(l, mid, nritems - mid) + |
| 4017 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4018 | if (data_size && !tried_avoid_double) |
| 4019 | goto push_for_double; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4020 | split = 2; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | if (split == 0) |
| 4027 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 4028 | else |
| 4029 | btrfs_item_key(l, &disk_key, mid); |
| 4030 | |
| 4031 | right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4032 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4033 | &disk_key, 0, l->start, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4034 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4035 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4036 | |
| 4037 | root_add_used(root, root->leafsize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4038 | |
| 4039 | memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); |
| 4040 | btrfs_set_header_bytenr(right, right->start); |
| 4041 | btrfs_set_header_generation(right, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4042 | btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4043 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 4044 | btrfs_set_header_level(right, 0); |
| 4045 | write_extent_buffer(right, root->fs_info->fsid, |
Ross Kirk | 0a4e558 | 2013-09-24 10:12:38 +0100 | [diff] [blame] | 4046 | btrfs_header_fsid(), BTRFS_FSID_SIZE); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4047 | |
| 4048 | write_extent_buffer(right, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | b308bc2 | 2013-08-20 13:20:15 +0200 | [diff] [blame] | 4049 | btrfs_header_chunk_tree_uuid(right), |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4050 | BTRFS_UUID_SIZE); |
| 4051 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4052 | if (split == 0) { |
| 4053 | if (mid <= slot) { |
| 4054 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4055 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4056 | path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4057 | btrfs_tree_unlock(path->nodes[0]); |
| 4058 | free_extent_buffer(path->nodes[0]); |
| 4059 | path->nodes[0] = right; |
| 4060 | path->slots[0] = 0; |
| 4061 | path->slots[1] += 1; |
| 4062 | } else { |
| 4063 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4064 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4065 | path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4066 | btrfs_tree_unlock(path->nodes[0]); |
| 4067 | free_extent_buffer(path->nodes[0]); |
| 4068 | path->nodes[0] = right; |
| 4069 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4070 | if (path->slots[1] == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4071 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4072 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4073 | btrfs_mark_buffer_dirty(right); |
| 4074 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4075 | } |
| 4076 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4077 | copy_for_split(trans, root, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4078 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4079 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4080 | BUG_ON(num_doubles != 0); |
| 4081 | num_doubles++; |
| 4082 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4083 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4084 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4085 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4086 | |
| 4087 | push_for_double: |
| 4088 | push_for_double_split(trans, root, path, data_size); |
| 4089 | tried_avoid_double = 1; |
| 4090 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 4091 | return 0; |
| 4092 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4093 | } |
| 4094 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4095 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4096 | struct btrfs_root *root, |
| 4097 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4098 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4099 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4100 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4101 | struct btrfs_file_extent_item *fi; |
| 4102 | u64 extent_len = 0; |
| 4103 | u32 item_size; |
| 4104 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4105 | |
| 4106 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4107 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4108 | |
| 4109 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4110 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4111 | |
| 4112 | if (btrfs_leaf_free_space(root, leaf) >= ins_len) |
| 4113 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4114 | |
| 4115 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4116 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4117 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4118 | struct btrfs_file_extent_item); |
| 4119 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4120 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4121 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4122 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4123 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4124 | path->search_for_split = 1; |
| 4125 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4126 | path->search_for_split = 0; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4127 | if (ret < 0) |
| 4128 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4129 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4130 | ret = -EAGAIN; |
| 4131 | leaf = path->nodes[0]; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4132 | /* if our item isn't there or got smaller, return now */ |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4133 | if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
| 4134 | goto err; |
| 4135 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4136 | /* the leaf has changed, it now has room. return now */ |
| 4137 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) |
| 4138 | goto err; |
| 4139 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4140 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4141 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4142 | struct btrfs_file_extent_item); |
| 4143 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4144 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4145 | } |
| 4146 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4147 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4148 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4149 | if (ret) |
| 4150 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4151 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4152 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4153 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4154 | return 0; |
| 4155 | err: |
| 4156 | path->keep_locks = 0; |
| 4157 | return ret; |
| 4158 | } |
| 4159 | |
| 4160 | static noinline int split_item(struct btrfs_trans_handle *trans, |
| 4161 | struct btrfs_root *root, |
| 4162 | struct btrfs_path *path, |
| 4163 | struct btrfs_key *new_key, |
| 4164 | unsigned long split_offset) |
| 4165 | { |
| 4166 | struct extent_buffer *leaf; |
| 4167 | struct btrfs_item *item; |
| 4168 | struct btrfs_item *new_item; |
| 4169 | int slot; |
| 4170 | char *buf; |
| 4171 | u32 nritems; |
| 4172 | u32 item_size; |
| 4173 | u32 orig_offset; |
| 4174 | struct btrfs_disk_key disk_key; |
| 4175 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4176 | leaf = path->nodes[0]; |
| 4177 | BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); |
| 4178 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4179 | btrfs_set_path_blocking(path); |
| 4180 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4181 | item = btrfs_item_nr(path->slots[0]); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4182 | orig_offset = btrfs_item_offset(leaf, item); |
| 4183 | item_size = btrfs_item_size(leaf, item); |
| 4184 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4185 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4186 | if (!buf) |
| 4187 | return -ENOMEM; |
| 4188 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4189 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4190 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4191 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4192 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4193 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4194 | if (slot != nritems) { |
| 4195 | /* shift the items */ |
| 4196 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4197 | btrfs_item_nr_offset(slot), |
| 4198 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4202 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4203 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4204 | new_item = btrfs_item_nr(slot); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4205 | |
| 4206 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4207 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4208 | |
| 4209 | btrfs_set_item_offset(leaf, item, |
| 4210 | orig_offset + item_size - split_offset); |
| 4211 | btrfs_set_item_size(leaf, item, split_offset); |
| 4212 | |
| 4213 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4214 | |
| 4215 | /* write the data for the start of the original item */ |
| 4216 | write_extent_buffer(leaf, buf, |
| 4217 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4218 | split_offset); |
| 4219 | |
| 4220 | /* write the data for the new item */ |
| 4221 | write_extent_buffer(leaf, buf + split_offset, |
| 4222 | btrfs_item_ptr_offset(leaf, slot), |
| 4223 | item_size - split_offset); |
| 4224 | btrfs_mark_buffer_dirty(leaf); |
| 4225 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4226 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4227 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4228 | return 0; |
| 4229 | } |
| 4230 | |
| 4231 | /* |
| 4232 | * This function splits a single item into two items, |
| 4233 | * giving 'new_key' to the new item and splitting the |
| 4234 | * old one at split_offset (from the start of the item). |
| 4235 | * |
| 4236 | * The path may be released by this operation. After |
| 4237 | * the split, the path is pointing to the old item. The |
| 4238 | * new item is going to be in the same node as the old one. |
| 4239 | * |
| 4240 | * Note, the item being split must be smaller enough to live alone on |
| 4241 | * a tree block with room for one extra struct btrfs_item |
| 4242 | * |
| 4243 | * This allows us to split the item in place, keeping a lock on the |
| 4244 | * leaf the entire time. |
| 4245 | */ |
| 4246 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4247 | struct btrfs_root *root, |
| 4248 | struct btrfs_path *path, |
| 4249 | struct btrfs_key *new_key, |
| 4250 | unsigned long split_offset) |
| 4251 | { |
| 4252 | int ret; |
| 4253 | ret = setup_leaf_for_split(trans, root, path, |
| 4254 | sizeof(struct btrfs_item)); |
| 4255 | if (ret) |
| 4256 | return ret; |
| 4257 | |
| 4258 | ret = split_item(trans, root, path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4259 | return ret; |
| 4260 | } |
| 4261 | |
| 4262 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4263 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4264 | * It guarantees both items live in the same tree leaf and the new item |
| 4265 | * is contiguous with the original item. |
| 4266 | * |
| 4267 | * This allows us to split file extent in place, keeping a lock on the |
| 4268 | * leaf the entire time. |
| 4269 | */ |
| 4270 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4271 | struct btrfs_root *root, |
| 4272 | struct btrfs_path *path, |
| 4273 | struct btrfs_key *new_key) |
| 4274 | { |
| 4275 | struct extent_buffer *leaf; |
| 4276 | int ret; |
| 4277 | u32 item_size; |
| 4278 | |
| 4279 | leaf = path->nodes[0]; |
| 4280 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4281 | ret = setup_leaf_for_split(trans, root, path, |
| 4282 | item_size + sizeof(struct btrfs_item)); |
| 4283 | if (ret) |
| 4284 | return ret; |
| 4285 | |
| 4286 | path->slots[0]++; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4287 | setup_items_for_insert(root, path, new_key, &item_size, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4288 | item_size, item_size + |
| 4289 | sizeof(struct btrfs_item), 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4290 | leaf = path->nodes[0]; |
| 4291 | memcpy_extent_buffer(leaf, |
| 4292 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4293 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4294 | item_size); |
| 4295 | return 0; |
| 4296 | } |
| 4297 | |
| 4298 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4299 | * make the item pointed to by the path smaller. new_size indicates |
| 4300 | * how small to make it, and from_end tells us if we just chop bytes |
| 4301 | * off the end of the item or if we shift the item to chop bytes off |
| 4302 | * the front. |
| 4303 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4304 | void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4305 | u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4306 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4307 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4308 | struct extent_buffer *leaf; |
| 4309 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4310 | u32 nritems; |
| 4311 | unsigned int data_end; |
| 4312 | unsigned int old_data_start; |
| 4313 | unsigned int old_size; |
| 4314 | unsigned int size_diff; |
| 4315 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4316 | struct btrfs_map_token token; |
| 4317 | |
| 4318 | btrfs_init_map_token(&token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4319 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4320 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4321 | slot = path->slots[0]; |
| 4322 | |
| 4323 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4324 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4325 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4326 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4327 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4328 | data_end = leaf_data_end(root, leaf); |
| 4329 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4330 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4331 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4332 | size_diff = old_size - new_size; |
| 4333 | |
| 4334 | BUG_ON(slot < 0); |
| 4335 | BUG_ON(slot >= nritems); |
| 4336 | |
| 4337 | /* |
| 4338 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4339 | */ |
| 4340 | /* first correct the data pointers */ |
| 4341 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4342 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4343 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4344 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4345 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4346 | btrfs_set_token_item_offset(leaf, item, |
| 4347 | ioff + size_diff, &token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4348 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4349 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4350 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4351 | if (from_end) { |
| 4352 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4353 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4354 | data_end, old_data_start + new_size - data_end); |
| 4355 | } else { |
| 4356 | struct btrfs_disk_key disk_key; |
| 4357 | u64 offset; |
| 4358 | |
| 4359 | btrfs_item_key(leaf, &disk_key, slot); |
| 4360 | |
| 4361 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4362 | unsigned long ptr; |
| 4363 | struct btrfs_file_extent_item *fi; |
| 4364 | |
| 4365 | fi = btrfs_item_ptr(leaf, slot, |
| 4366 | struct btrfs_file_extent_item); |
| 4367 | fi = (struct btrfs_file_extent_item *)( |
| 4368 | (unsigned long)fi - size_diff); |
| 4369 | |
| 4370 | if (btrfs_file_extent_type(leaf, fi) == |
| 4371 | BTRFS_FILE_EXTENT_INLINE) { |
| 4372 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4373 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4374 | (unsigned long)fi, |
| 4375 | offsetof(struct btrfs_file_extent_item, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4376 | disk_bytenr)); |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4381 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4382 | data_end, old_data_start - data_end); |
| 4383 | |
| 4384 | offset = btrfs_disk_key_offset(&disk_key); |
| 4385 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4386 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4387 | if (slot == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4388 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4389 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4390 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4391 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4392 | btrfs_set_item_size(leaf, item, new_size); |
| 4393 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4394 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4395 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4396 | btrfs_print_leaf(root, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4397 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4398 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4399 | } |
| 4400 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4401 | /* |
Stefan Behrens | 8f69dbd | 2013-05-07 10:23:30 +0000 | [diff] [blame] | 4402 | * make the item pointed to by the path bigger, data_size is the added size. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4403 | */ |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 4404 | void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4405 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4406 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4407 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4408 | struct extent_buffer *leaf; |
| 4409 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4410 | u32 nritems; |
| 4411 | unsigned int data_end; |
| 4412 | unsigned int old_data; |
| 4413 | unsigned int old_size; |
| 4414 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4415 | struct btrfs_map_token token; |
| 4416 | |
| 4417 | btrfs_init_map_token(&token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4418 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4419 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4420 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4421 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4422 | data_end = leaf_data_end(root, leaf); |
| 4423 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4424 | if (btrfs_leaf_free_space(root, leaf) < data_size) { |
| 4425 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4426 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4427 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4428 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4429 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4430 | |
| 4431 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4432 | if (slot >= nritems) { |
| 4433 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4434 | printk(KERN_CRIT "slot %d too large, nritems %d\n", |
| 4435 | slot, nritems); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4436 | BUG_ON(1); |
| 4437 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4438 | |
| 4439 | /* |
| 4440 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4441 | */ |
| 4442 | /* first correct the data pointers */ |
| 4443 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4444 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4445 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4446 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4447 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4448 | btrfs_set_token_item_offset(leaf, item, |
| 4449 | ioff - data_size, &token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4450 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4451 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4452 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4453 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4454 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 4455 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4456 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4457 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4458 | old_size = btrfs_item_size_nr(leaf, slot); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4459 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4460 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4461 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4462 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4463 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4464 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4465 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4466 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4467 | } |
| 4468 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4469 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4470 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
| 4471 | * to save stack depth by doing the bulk of the work in a function |
| 4472 | * that doesn't call btrfs_search_slot |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4473 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4474 | void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4475 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4476 | u32 total_data, u32 total_size, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4477 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4478 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4479 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4480 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4481 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4482 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4483 | struct extent_buffer *leaf; |
| 4484 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4485 | struct btrfs_map_token token; |
| 4486 | |
| 4487 | btrfs_init_map_token(&token); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4488 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4489 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4490 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4491 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4492 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4493 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4494 | |
Chris Mason | f25956c | 2008-09-12 15:32:53 -0400 | [diff] [blame] | 4495 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4496 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4497 | printk(KERN_CRIT "not enough freespace need %u have %d\n", |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4498 | total_size, btrfs_leaf_free_space(root, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4499 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4500 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4501 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4502 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4503 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4504 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4505 | if (old_data < data_end) { |
| 4506 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4507 | printk(KERN_CRIT "slot %d old_data %d data_end %d\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4508 | slot, old_data, data_end); |
| 4509 | BUG_ON(1); |
| 4510 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4511 | /* |
| 4512 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4513 | */ |
| 4514 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4515 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4516 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4517 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4518 | item = btrfs_item_nr( i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4519 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4520 | btrfs_set_token_item_offset(leaf, item, |
| 4521 | ioff - total_data, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4522 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4523 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4524 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4525 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4526 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4527 | |
| 4528 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4529 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4530 | data_end - total_data, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4531 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4532 | data_end = old_data; |
| 4533 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4534 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4535 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4536 | for (i = 0; i < nr; i++) { |
| 4537 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4538 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4539 | item = btrfs_item_nr(slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4540 | btrfs_set_token_item_offset(leaf, item, |
| 4541 | data_end - data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4542 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4543 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4544 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4545 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4546 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4547 | |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4548 | if (slot == 0) { |
| 4549 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4550 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4551 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4552 | btrfs_unlock_up_safe(path, 1); |
| 4553 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4554 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4555 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4556 | btrfs_print_leaf(root, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4557 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4558 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | /* |
| 4562 | * Given a key and some data, insert items into the tree. |
| 4563 | * This does all the path init required, making room in the tree if needed. |
| 4564 | */ |
| 4565 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4566 | struct btrfs_root *root, |
| 4567 | struct btrfs_path *path, |
| 4568 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4569 | int nr) |
| 4570 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4571 | int ret = 0; |
| 4572 | int slot; |
| 4573 | int i; |
| 4574 | u32 total_size = 0; |
| 4575 | u32 total_data = 0; |
| 4576 | |
| 4577 | for (i = 0; i < nr; i++) |
| 4578 | total_data += data_size[i]; |
| 4579 | |
| 4580 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4581 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4582 | if (ret == 0) |
| 4583 | return -EEXIST; |
| 4584 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4585 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4586 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4587 | slot = path->slots[0]; |
| 4588 | BUG_ON(slot < 0); |
| 4589 | |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4590 | setup_items_for_insert(root, path, cpu_key, data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4591 | total_data, total_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4592 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | /* |
| 4596 | * Given a key and some data, insert an item into the tree. |
| 4597 | * This does all the path init required, making room in the tree if needed. |
| 4598 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4599 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 4600 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 4601 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4602 | { |
| 4603 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4604 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4605 | struct extent_buffer *leaf; |
| 4606 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4607 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4608 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4609 | if (!path) |
| 4610 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4611 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4612 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4613 | leaf = path->nodes[0]; |
| 4614 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4615 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4616 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4617 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4618 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4619 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4620 | } |
| 4621 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4622 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4623 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4624 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4625 | * the tree should have been previously balanced so the deletion does not |
| 4626 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4627 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4628 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 4629 | int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4630 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4631 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4632 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4633 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4634 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4635 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4636 | if (slot != nritems - 1) { |
Liu Bo | 0e411ec | 2012-10-19 09:50:54 +0000 | [diff] [blame] | 4637 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4638 | tree_mod_log_eb_move(root->fs_info, parent, slot, |
| 4639 | slot + 1, nritems - slot - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4640 | memmove_extent_buffer(parent, |
| 4641 | btrfs_node_key_ptr_offset(slot), |
| 4642 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4643 | sizeof(struct btrfs_key_ptr) * |
| 4644 | (nritems - slot - 1)); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4645 | } else if (level) { |
| 4646 | ret = tree_mod_log_insert_key(root->fs_info, parent, slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 4647 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4648 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4649 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4650 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4651 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4652 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4653 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4654 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4655 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4656 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4657 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4658 | struct btrfs_disk_key disk_key; |
| 4659 | |
| 4660 | btrfs_node_key(parent, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4661 | fixup_low_keys(root, path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4662 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4663 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4664 | } |
| 4665 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4666 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4667 | * 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] | 4668 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4669 | * |
| 4670 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4671 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4672 | * |
| 4673 | * The path must have already been setup for deleting the leaf, including |
| 4674 | * all the proper balancing. path->nodes[1] must be locked. |
| 4675 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4676 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4677 | struct btrfs_root *root, |
| 4678 | struct btrfs_path *path, |
| 4679 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4680 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4681 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4682 | del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4683 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4684 | /* |
| 4685 | * btrfs_free_extent is expensive, we want to make sure we |
| 4686 | * aren't holding any locks when we call it |
| 4687 | */ |
| 4688 | btrfs_unlock_up_safe(path, 0); |
| 4689 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4690 | root_sub_used(root, leaf->len); |
| 4691 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4692 | extent_buffer_get(leaf); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4693 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4694 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4695 | } |
| 4696 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4697 | * delete the item at the leaf level in path. If that empties |
| 4698 | * the leaf, remove it from the tree |
| 4699 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4700 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4701 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4702 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4703 | struct extent_buffer *leaf; |
| 4704 | struct btrfs_item *item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4705 | int last_off; |
| 4706 | int dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4707 | int ret = 0; |
| 4708 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4709 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4710 | u32 nritems; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4711 | struct btrfs_map_token token; |
| 4712 | |
| 4713 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4714 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4715 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4716 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4717 | |
| 4718 | for (i = 0; i < nr; i++) |
| 4719 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4720 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4721 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4722 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4723 | if (slot + nr != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4724 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4725 | |
| 4726 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4727 | data_end + dsize, |
| 4728 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4729 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4730 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4731 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4732 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4733 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4734 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4735 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4736 | btrfs_set_token_item_offset(leaf, item, |
| 4737 | ioff + dsize, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4738 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4739 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4740 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4741 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4742 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4743 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4744 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4745 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4746 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4747 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4748 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4749 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4750 | if (leaf == root->node) { |
| 4751 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4752 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4753 | btrfs_set_path_blocking(path); |
| 4754 | clean_tree_block(trans, root, leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4755 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4756 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4757 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4758 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4759 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4760 | struct btrfs_disk_key disk_key; |
| 4761 | |
| 4762 | btrfs_item_key(leaf, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4763 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4764 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4765 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4766 | /* delete the leaf if it is mostly empty */ |
Yan Zheng | d717aa1 | 2009-07-24 12:42:46 -0400 | [diff] [blame] | 4767 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4768 | /* push_leaf_left fixes the path. |
| 4769 | * make sure the path still points to our leaf |
| 4770 | * for possible call to del_ptr below |
| 4771 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 4772 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4773 | extent_buffer_get(leaf); |
| 4774 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4775 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4776 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 4777 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4778 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4779 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4780 | |
| 4781 | if (path->nodes[0] == leaf && |
| 4782 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4783 | wret = push_leaf_right(trans, root, path, 1, |
| 4784 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4785 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4786 | ret = wret; |
| 4787 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4788 | |
| 4789 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4790 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4791 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4792 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4793 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4794 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4795 | /* if we're still in the path, make sure |
| 4796 | * we're dirty. Otherwise, one of the |
| 4797 | * push_leaf functions must have already |
| 4798 | * dirtied this buffer |
| 4799 | */ |
| 4800 | if (path->nodes[0] == leaf) |
| 4801 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4802 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4803 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 4804 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4805 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4806 | } |
| 4807 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4808 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4809 | } |
| 4810 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 4811 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4812 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4813 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 4814 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4815 | * |
| 4816 | * This may release the path, and so you may lose any locks held at the |
| 4817 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4818 | */ |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame^] | 4819 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4820 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4821 | struct btrfs_key key; |
| 4822 | struct btrfs_disk_key found_key; |
| 4823 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4824 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4825 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4826 | |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 4827 | if (key.offset > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4828 | key.offset--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 4829 | } else if (key.type > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4830 | key.type--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 4831 | key.offset = (u64)-1; |
| 4832 | } else if (key.objectid > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4833 | key.objectid--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 4834 | key.type = (u8)-1; |
| 4835 | key.offset = (u64)-1; |
| 4836 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4837 | return 1; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 4838 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4839 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4840 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4841 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4842 | if (ret < 0) |
| 4843 | return ret; |
| 4844 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 4845 | ret = comp_keys(&found_key, &key); |
| 4846 | if (ret < 0) |
| 4847 | return 0; |
| 4848 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4849 | } |
| 4850 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4851 | /* |
| 4852 | * A helper function to walk down the tree starting at min_key, and looking |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4853 | * for nodes or leaves that are have a minimum transaction id. |
| 4854 | * This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4855 | * |
| 4856 | * This does not cow, but it does stuff the starting key it finds back |
| 4857 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 4858 | * key and get a writable path. |
| 4859 | * |
| 4860 | * This does lock as it descends, and path->keep_locks should be set |
| 4861 | * to 1 by the caller. |
| 4862 | * |
| 4863 | * This honors path->lowest_level to prevent descent past a given level |
| 4864 | * of the tree. |
| 4865 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4866 | * min_trans indicates the oldest transaction that you are interested |
| 4867 | * in walking through. Any nodes or leaves older than min_trans are |
| 4868 | * skipped over (without reading them). |
| 4869 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4870 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 4871 | * was nothing in the tree that matched the search criteria. |
| 4872 | */ |
| 4873 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4874 | struct btrfs_path *path, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4875 | u64 min_trans) |
| 4876 | { |
| 4877 | struct extent_buffer *cur; |
| 4878 | struct btrfs_key found_key; |
| 4879 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4880 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4881 | u32 nritems; |
| 4882 | int level; |
| 4883 | int ret = 1; |
| 4884 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4885 | WARN_ON(!path->keep_locks); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4886 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4887 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4888 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4889 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4890 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4891 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4892 | |
| 4893 | if (btrfs_header_generation(cur) < min_trans) { |
| 4894 | ret = 1; |
| 4895 | goto out; |
| 4896 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4897 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4898 | nritems = btrfs_header_nritems(cur); |
| 4899 | level = btrfs_header_level(cur); |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4900 | sret = bin_search(cur, min_key, level, &slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4901 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4902 | /* at the lowest level, we're done, setup the path and exit */ |
| 4903 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4904 | if (slot >= nritems) |
| 4905 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4906 | ret = 0; |
| 4907 | path->slots[level] = slot; |
| 4908 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 4909 | goto out; |
| 4910 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4911 | if (sret && slot > 0) |
| 4912 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4913 | /* |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4914 | * check this node pointer against the min_trans parameters. |
| 4915 | * If it is too old, old, skip to the next one. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4916 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4917 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4918 | u64 gen; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4919 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4920 | gen = btrfs_node_ptr_generation(cur, slot); |
| 4921 | if (gen < min_trans) { |
| 4922 | slot++; |
| 4923 | continue; |
| 4924 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4925 | break; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4926 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4927 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4928 | /* |
| 4929 | * we didn't find a candidate key in this node, walk forward |
| 4930 | * and find another one |
| 4931 | */ |
| 4932 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4933 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4934 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4935 | sret = btrfs_find_next_key(root, path, min_key, level, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4936 | min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4937 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4938 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4939 | goto again; |
| 4940 | } else { |
| 4941 | goto out; |
| 4942 | } |
| 4943 | } |
| 4944 | /* save our key for returning back */ |
| 4945 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 4946 | path->slots[level] = slot; |
| 4947 | if (level == path->lowest_level) { |
| 4948 | ret = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4949 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4950 | goto out; |
| 4951 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4952 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4953 | cur = read_node_slot(root, cur, slot); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4954 | BUG_ON(!cur); /* -ENOMEM */ |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4955 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4956 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4957 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4958 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4959 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4960 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4961 | btrfs_clear_path_blocking(path, NULL, 0); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4962 | } |
| 4963 | out: |
| 4964 | if (ret == 0) |
| 4965 | memcpy(min_key, &found_key, sizeof(found_key)); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4966 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4967 | return ret; |
| 4968 | } |
| 4969 | |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4970 | static void tree_move_down(struct btrfs_root *root, |
| 4971 | struct btrfs_path *path, |
| 4972 | int *level, int root_level) |
| 4973 | { |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4974 | BUG_ON(*level == 0); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4975 | path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level], |
| 4976 | path->slots[*level]); |
| 4977 | path->slots[*level - 1] = 0; |
| 4978 | (*level)--; |
| 4979 | } |
| 4980 | |
| 4981 | static int tree_move_next_or_upnext(struct btrfs_root *root, |
| 4982 | struct btrfs_path *path, |
| 4983 | int *level, int root_level) |
| 4984 | { |
| 4985 | int ret = 0; |
| 4986 | int nritems; |
| 4987 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 4988 | |
| 4989 | path->slots[*level]++; |
| 4990 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4991 | while (path->slots[*level] >= nritems) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4992 | if (*level == root_level) |
| 4993 | return -1; |
| 4994 | |
| 4995 | /* move upnext */ |
| 4996 | path->slots[*level] = 0; |
| 4997 | free_extent_buffer(path->nodes[*level]); |
| 4998 | path->nodes[*level] = NULL; |
| 4999 | (*level)++; |
| 5000 | path->slots[*level]++; |
| 5001 | |
| 5002 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 5003 | ret = 1; |
| 5004 | } |
| 5005 | return ret; |
| 5006 | } |
| 5007 | |
| 5008 | /* |
| 5009 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 5010 | * or down. |
| 5011 | */ |
| 5012 | static int tree_advance(struct btrfs_root *root, |
| 5013 | struct btrfs_path *path, |
| 5014 | int *level, int root_level, |
| 5015 | int allow_down, |
| 5016 | struct btrfs_key *key) |
| 5017 | { |
| 5018 | int ret; |
| 5019 | |
| 5020 | if (*level == 0 || !allow_down) { |
| 5021 | ret = tree_move_next_or_upnext(root, path, level, root_level); |
| 5022 | } else { |
| 5023 | tree_move_down(root, path, level, root_level); |
| 5024 | ret = 0; |
| 5025 | } |
| 5026 | if (ret >= 0) { |
| 5027 | if (*level == 0) |
| 5028 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 5029 | path->slots[*level]); |
| 5030 | else |
| 5031 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 5032 | path->slots[*level]); |
| 5033 | } |
| 5034 | return ret; |
| 5035 | } |
| 5036 | |
| 5037 | static int tree_compare_item(struct btrfs_root *left_root, |
| 5038 | struct btrfs_path *left_path, |
| 5039 | struct btrfs_path *right_path, |
| 5040 | char *tmp_buf) |
| 5041 | { |
| 5042 | int cmp; |
| 5043 | int len1, len2; |
| 5044 | unsigned long off1, off2; |
| 5045 | |
| 5046 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 5047 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 5048 | if (len1 != len2) |
| 5049 | return 1; |
| 5050 | |
| 5051 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 5052 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 5053 | right_path->slots[0]); |
| 5054 | |
| 5055 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 5056 | |
| 5057 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 5058 | if (cmp) |
| 5059 | return 1; |
| 5060 | return 0; |
| 5061 | } |
| 5062 | |
| 5063 | #define ADVANCE 1 |
| 5064 | #define ADVANCE_ONLY_NEXT -1 |
| 5065 | |
| 5066 | /* |
| 5067 | * This function compares two trees and calls the provided callback for |
| 5068 | * every changed/new/deleted item it finds. |
| 5069 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 5070 | * the compare pretty fast on snapshotted subvolumes. |
| 5071 | * |
| 5072 | * This currently works on commit roots only. As commit roots are read only, |
| 5073 | * we don't do any locking. The commit roots are protected with transactions. |
| 5074 | * Transactions are ended and rejoined when a commit is tried in between. |
| 5075 | * |
| 5076 | * This function checks for modifications done to the trees while comparing. |
| 5077 | * If it detects a change, it aborts immediately. |
| 5078 | */ |
| 5079 | int btrfs_compare_trees(struct btrfs_root *left_root, |
| 5080 | struct btrfs_root *right_root, |
| 5081 | btrfs_changed_cb_t changed_cb, void *ctx) |
| 5082 | { |
| 5083 | int ret; |
| 5084 | int cmp; |
| 5085 | struct btrfs_trans_handle *trans = NULL; |
| 5086 | struct btrfs_path *left_path = NULL; |
| 5087 | struct btrfs_path *right_path = NULL; |
| 5088 | struct btrfs_key left_key; |
| 5089 | struct btrfs_key right_key; |
| 5090 | char *tmp_buf = NULL; |
| 5091 | int left_root_level; |
| 5092 | int right_root_level; |
| 5093 | int left_level; |
| 5094 | int right_level; |
| 5095 | int left_end_reached; |
| 5096 | int right_end_reached; |
| 5097 | int advance_left; |
| 5098 | int advance_right; |
| 5099 | u64 left_blockptr; |
| 5100 | u64 right_blockptr; |
| 5101 | u64 left_start_ctransid; |
| 5102 | u64 right_start_ctransid; |
| 5103 | u64 ctransid; |
| 5104 | |
| 5105 | left_path = btrfs_alloc_path(); |
| 5106 | if (!left_path) { |
| 5107 | ret = -ENOMEM; |
| 5108 | goto out; |
| 5109 | } |
| 5110 | right_path = btrfs_alloc_path(); |
| 5111 | if (!right_path) { |
| 5112 | ret = -ENOMEM; |
| 5113 | goto out; |
| 5114 | } |
| 5115 | |
| 5116 | tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS); |
| 5117 | if (!tmp_buf) { |
| 5118 | ret = -ENOMEM; |
| 5119 | goto out; |
| 5120 | } |
| 5121 | |
| 5122 | left_path->search_commit_root = 1; |
| 5123 | left_path->skip_locking = 1; |
| 5124 | right_path->search_commit_root = 1; |
| 5125 | right_path->skip_locking = 1; |
| 5126 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5127 | spin_lock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5128 | left_start_ctransid = btrfs_root_ctransid(&left_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5129 | spin_unlock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5130 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5131 | spin_lock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5132 | right_start_ctransid = btrfs_root_ctransid(&right_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5133 | spin_unlock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5134 | |
| 5135 | trans = btrfs_join_transaction(left_root); |
| 5136 | if (IS_ERR(trans)) { |
| 5137 | ret = PTR_ERR(trans); |
| 5138 | trans = NULL; |
| 5139 | goto out; |
| 5140 | } |
| 5141 | |
| 5142 | /* |
| 5143 | * Strategy: Go to the first items of both trees. Then do |
| 5144 | * |
| 5145 | * If both trees are at level 0 |
| 5146 | * Compare keys of current items |
| 5147 | * If left < right treat left item as new, advance left tree |
| 5148 | * and repeat |
| 5149 | * If left > right treat right item as deleted, advance right tree |
| 5150 | * and repeat |
| 5151 | * If left == right do deep compare of items, treat as changed if |
| 5152 | * needed, advance both trees and repeat |
| 5153 | * If both trees are at the same level but not at level 0 |
| 5154 | * Compare keys of current nodes/leafs |
| 5155 | * If left < right advance left tree and repeat |
| 5156 | * If left > right advance right tree and repeat |
| 5157 | * If left == right compare blockptrs of the next nodes/leafs |
| 5158 | * If they match advance both trees but stay at the same level |
| 5159 | * and repeat |
| 5160 | * If they don't match advance both trees while allowing to go |
| 5161 | * deeper and repeat |
| 5162 | * If tree levels are different |
| 5163 | * Advance the tree that needs it and repeat |
| 5164 | * |
| 5165 | * Advancing a tree means: |
| 5166 | * If we are at level 0, try to go to the next slot. If that's not |
| 5167 | * possible, go one level up and repeat. Stop when we found a level |
| 5168 | * where we could go to the next slot. We may at this point be on a |
| 5169 | * node or a leaf. |
| 5170 | * |
| 5171 | * If we are not at level 0 and not on shared tree blocks, go one |
| 5172 | * level deeper. |
| 5173 | * |
| 5174 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 5175 | * the right if possible or go up and right. |
| 5176 | */ |
| 5177 | |
| 5178 | left_level = btrfs_header_level(left_root->commit_root); |
| 5179 | left_root_level = left_level; |
| 5180 | left_path->nodes[left_level] = left_root->commit_root; |
| 5181 | extent_buffer_get(left_path->nodes[left_level]); |
| 5182 | |
| 5183 | right_level = btrfs_header_level(right_root->commit_root); |
| 5184 | right_root_level = right_level; |
| 5185 | right_path->nodes[right_level] = right_root->commit_root; |
| 5186 | extent_buffer_get(right_path->nodes[right_level]); |
| 5187 | |
| 5188 | if (left_level == 0) |
| 5189 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 5190 | &left_key, left_path->slots[left_level]); |
| 5191 | else |
| 5192 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 5193 | &left_key, left_path->slots[left_level]); |
| 5194 | if (right_level == 0) |
| 5195 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 5196 | &right_key, right_path->slots[right_level]); |
| 5197 | else |
| 5198 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 5199 | &right_key, right_path->slots[right_level]); |
| 5200 | |
| 5201 | left_end_reached = right_end_reached = 0; |
| 5202 | advance_left = advance_right = 0; |
| 5203 | |
| 5204 | while (1) { |
| 5205 | /* |
| 5206 | * We need to make sure the transaction does not get committed |
| 5207 | * while we do anything on commit roots. This means, we need to |
| 5208 | * join and leave transactions for every item that we process. |
| 5209 | */ |
| 5210 | if (trans && btrfs_should_end_transaction(trans, left_root)) { |
| 5211 | btrfs_release_path(left_path); |
| 5212 | btrfs_release_path(right_path); |
| 5213 | |
| 5214 | ret = btrfs_end_transaction(trans, left_root); |
| 5215 | trans = NULL; |
| 5216 | if (ret < 0) |
| 5217 | goto out; |
| 5218 | } |
| 5219 | /* now rejoin the transaction */ |
| 5220 | if (!trans) { |
| 5221 | trans = btrfs_join_transaction(left_root); |
| 5222 | if (IS_ERR(trans)) { |
| 5223 | ret = PTR_ERR(trans); |
| 5224 | trans = NULL; |
| 5225 | goto out; |
| 5226 | } |
| 5227 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5228 | spin_lock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5229 | ctransid = btrfs_root_ctransid(&left_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5230 | spin_unlock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5231 | if (ctransid != left_start_ctransid) |
| 5232 | left_start_ctransid = 0; |
| 5233 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5234 | spin_lock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5235 | ctransid = btrfs_root_ctransid(&right_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5236 | spin_unlock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5237 | if (ctransid != right_start_ctransid) |
| 5238 | right_start_ctransid = 0; |
| 5239 | |
| 5240 | if (!left_start_ctransid || !right_start_ctransid) { |
| 5241 | WARN(1, KERN_WARNING |
| 5242 | "btrfs: btrfs_compare_tree detected " |
| 5243 | "a change in one of the trees while " |
| 5244 | "iterating. This is probably a " |
| 5245 | "bug.\n"); |
| 5246 | ret = -EIO; |
| 5247 | goto out; |
| 5248 | } |
| 5249 | |
| 5250 | /* |
| 5251 | * the commit root may have changed, so start again |
| 5252 | * where we stopped |
| 5253 | */ |
| 5254 | left_path->lowest_level = left_level; |
| 5255 | right_path->lowest_level = right_level; |
| 5256 | ret = btrfs_search_slot(NULL, left_root, |
| 5257 | &left_key, left_path, 0, 0); |
| 5258 | if (ret < 0) |
| 5259 | goto out; |
| 5260 | ret = btrfs_search_slot(NULL, right_root, |
| 5261 | &right_key, right_path, 0, 0); |
| 5262 | if (ret < 0) |
| 5263 | goto out; |
| 5264 | } |
| 5265 | |
| 5266 | if (advance_left && !left_end_reached) { |
| 5267 | ret = tree_advance(left_root, left_path, &left_level, |
| 5268 | left_root_level, |
| 5269 | advance_left != ADVANCE_ONLY_NEXT, |
| 5270 | &left_key); |
| 5271 | if (ret < 0) |
| 5272 | left_end_reached = ADVANCE; |
| 5273 | advance_left = 0; |
| 5274 | } |
| 5275 | if (advance_right && !right_end_reached) { |
| 5276 | ret = tree_advance(right_root, right_path, &right_level, |
| 5277 | right_root_level, |
| 5278 | advance_right != ADVANCE_ONLY_NEXT, |
| 5279 | &right_key); |
| 5280 | if (ret < 0) |
| 5281 | right_end_reached = ADVANCE; |
| 5282 | advance_right = 0; |
| 5283 | } |
| 5284 | |
| 5285 | if (left_end_reached && right_end_reached) { |
| 5286 | ret = 0; |
| 5287 | goto out; |
| 5288 | } else if (left_end_reached) { |
| 5289 | if (right_level == 0) { |
| 5290 | ret = changed_cb(left_root, right_root, |
| 5291 | left_path, right_path, |
| 5292 | &right_key, |
| 5293 | BTRFS_COMPARE_TREE_DELETED, |
| 5294 | ctx); |
| 5295 | if (ret < 0) |
| 5296 | goto out; |
| 5297 | } |
| 5298 | advance_right = ADVANCE; |
| 5299 | continue; |
| 5300 | } else if (right_end_reached) { |
| 5301 | if (left_level == 0) { |
| 5302 | ret = changed_cb(left_root, right_root, |
| 5303 | left_path, right_path, |
| 5304 | &left_key, |
| 5305 | BTRFS_COMPARE_TREE_NEW, |
| 5306 | ctx); |
| 5307 | if (ret < 0) |
| 5308 | goto out; |
| 5309 | } |
| 5310 | advance_left = ADVANCE; |
| 5311 | continue; |
| 5312 | } |
| 5313 | |
| 5314 | if (left_level == 0 && right_level == 0) { |
| 5315 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5316 | if (cmp < 0) { |
| 5317 | ret = changed_cb(left_root, right_root, |
| 5318 | left_path, right_path, |
| 5319 | &left_key, |
| 5320 | BTRFS_COMPARE_TREE_NEW, |
| 5321 | ctx); |
| 5322 | if (ret < 0) |
| 5323 | goto out; |
| 5324 | advance_left = ADVANCE; |
| 5325 | } else if (cmp > 0) { |
| 5326 | ret = changed_cb(left_root, right_root, |
| 5327 | left_path, right_path, |
| 5328 | &right_key, |
| 5329 | BTRFS_COMPARE_TREE_DELETED, |
| 5330 | ctx); |
| 5331 | if (ret < 0) |
| 5332 | goto out; |
| 5333 | advance_right = ADVANCE; |
| 5334 | } else { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5335 | enum btrfs_compare_tree_result cmp; |
| 5336 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5337 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5338 | ret = tree_compare_item(left_root, left_path, |
| 5339 | right_path, tmp_buf); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5340 | if (ret) |
| 5341 | cmp = BTRFS_COMPARE_TREE_CHANGED; |
| 5342 | else |
| 5343 | cmp = BTRFS_COMPARE_TREE_SAME; |
| 5344 | ret = changed_cb(left_root, right_root, |
| 5345 | left_path, right_path, |
| 5346 | &left_key, cmp, ctx); |
| 5347 | if (ret < 0) |
| 5348 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5349 | advance_left = ADVANCE; |
| 5350 | advance_right = ADVANCE; |
| 5351 | } |
| 5352 | } else if (left_level == right_level) { |
| 5353 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5354 | if (cmp < 0) { |
| 5355 | advance_left = ADVANCE; |
| 5356 | } else if (cmp > 0) { |
| 5357 | advance_right = ADVANCE; |
| 5358 | } else { |
| 5359 | left_blockptr = btrfs_node_blockptr( |
| 5360 | left_path->nodes[left_level], |
| 5361 | left_path->slots[left_level]); |
| 5362 | right_blockptr = btrfs_node_blockptr( |
| 5363 | right_path->nodes[right_level], |
| 5364 | right_path->slots[right_level]); |
| 5365 | if (left_blockptr == right_blockptr) { |
| 5366 | /* |
| 5367 | * As we're on a shared block, don't |
| 5368 | * allow to go deeper. |
| 5369 | */ |
| 5370 | advance_left = ADVANCE_ONLY_NEXT; |
| 5371 | advance_right = ADVANCE_ONLY_NEXT; |
| 5372 | } else { |
| 5373 | advance_left = ADVANCE; |
| 5374 | advance_right = ADVANCE; |
| 5375 | } |
| 5376 | } |
| 5377 | } else if (left_level < right_level) { |
| 5378 | advance_right = ADVANCE; |
| 5379 | } else { |
| 5380 | advance_left = ADVANCE; |
| 5381 | } |
| 5382 | } |
| 5383 | |
| 5384 | out: |
| 5385 | btrfs_free_path(left_path); |
| 5386 | btrfs_free_path(right_path); |
| 5387 | kfree(tmp_buf); |
| 5388 | |
| 5389 | if (trans) { |
| 5390 | if (!ret) |
| 5391 | ret = btrfs_end_transaction(trans, left_root); |
| 5392 | else |
| 5393 | btrfs_end_transaction(trans, left_root); |
| 5394 | } |
| 5395 | |
| 5396 | return ret; |
| 5397 | } |
| 5398 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5399 | /* |
| 5400 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5401 | * and fixup the path. It looks for and returns the next key in the |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5402 | * tree based on the current path and the min_trans parameters. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5403 | * |
| 5404 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5405 | * and 1 is returned if there are no higher keys in the tree |
| 5406 | * |
| 5407 | * path->keep_locks should be set to 1 on the search made before |
| 5408 | * calling this function. |
| 5409 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5410 | int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5411 | struct btrfs_key *key, int level, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5412 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5413 | int slot; |
| 5414 | struct extent_buffer *c; |
| 5415 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5416 | WARN_ON(!path->keep_locks); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5417 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5418 | if (!path->nodes[level]) |
| 5419 | return 1; |
| 5420 | |
| 5421 | slot = path->slots[level] + 1; |
| 5422 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5423 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5424 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5425 | int ret; |
| 5426 | int orig_lowest; |
| 5427 | struct btrfs_key cur_key; |
| 5428 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5429 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5430 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5431 | |
| 5432 | if (path->locks[level + 1]) { |
| 5433 | level++; |
| 5434 | continue; |
| 5435 | } |
| 5436 | |
| 5437 | slot = btrfs_header_nritems(c) - 1; |
| 5438 | if (level == 0) |
| 5439 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5440 | else |
| 5441 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5442 | |
| 5443 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5444 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5445 | path->lowest_level = level; |
| 5446 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5447 | 0, 0); |
| 5448 | path->lowest_level = orig_lowest; |
| 5449 | if (ret < 0) |
| 5450 | return ret; |
| 5451 | |
| 5452 | c = path->nodes[level]; |
| 5453 | slot = path->slots[level]; |
| 5454 | if (ret == 0) |
| 5455 | slot++; |
| 5456 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5457 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5458 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5459 | if (level == 0) |
| 5460 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5461 | else { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5462 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5463 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5464 | if (gen < min_trans) { |
| 5465 | slot++; |
| 5466 | goto next; |
| 5467 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5468 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5469 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5470 | return 0; |
| 5471 | } |
| 5472 | return 1; |
| 5473 | } |
| 5474 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5475 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5476 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5477 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5478 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5479 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5480 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5481 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5482 | return btrfs_next_old_leaf(root, path, 0); |
| 5483 | } |
| 5484 | |
| 5485 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5486 | u64 time_seq) |
| 5487 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5488 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5489 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5490 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5491 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5492 | struct btrfs_key key; |
| 5493 | u32 nritems; |
| 5494 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5495 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5496 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5497 | |
| 5498 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5499 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5500 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5501 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5502 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5503 | again: |
| 5504 | level = 1; |
| 5505 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5506 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5507 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5508 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5509 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5510 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5511 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5512 | if (time_seq) |
| 5513 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5514 | else |
| 5515 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5516 | path->keep_locks = 0; |
| 5517 | |
| 5518 | if (ret < 0) |
| 5519 | return ret; |
| 5520 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5521 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5522 | /* |
| 5523 | * by releasing the path above we dropped all our locks. A balance |
| 5524 | * could have added more items next to the key that used to be |
| 5525 | * at the very end of the block. So, check again here and |
| 5526 | * advance the path if there are now more items available. |
| 5527 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5528 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5529 | if (ret == 0) |
| 5530 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5531 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5532 | goto done; |
| 5533 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5534 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5535 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5536 | if (!path->nodes[level]) { |
| 5537 | ret = 1; |
| 5538 | goto done; |
| 5539 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5540 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5541 | slot = path->slots[level] + 1; |
| 5542 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5543 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5544 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5545 | if (level == BTRFS_MAX_LEVEL) { |
| 5546 | ret = 1; |
| 5547 | goto done; |
| 5548 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5549 | continue; |
| 5550 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5551 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5552 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5553 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5554 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5555 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5556 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5557 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5558 | next_rw_lock = path->locks[level]; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5559 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5560 | slot, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5561 | if (ret == -EAGAIN) |
| 5562 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5563 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5564 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5565 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5566 | goto done; |
| 5567 | } |
| 5568 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5569 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5570 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5571 | if (!ret && time_seq) { |
| 5572 | /* |
| 5573 | * If we don't get the lock, we may be racing |
| 5574 | * with push_leaf_left, holding that lock while |
| 5575 | * itself waiting for the leaf we've currently |
| 5576 | * locked. To solve this situation, we give up |
| 5577 | * on our lock and cycle. |
| 5578 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5579 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5580 | btrfs_release_path(path); |
| 5581 | cond_resched(); |
| 5582 | goto again; |
| 5583 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5584 | if (!ret) { |
| 5585 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5586 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5587 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5588 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5589 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5590 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5591 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5592 | break; |
| 5593 | } |
| 5594 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5595 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5596 | level--; |
| 5597 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5598 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5599 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5600 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5601 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5602 | path->nodes[level] = next; |
| 5603 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5604 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5605 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5606 | if (!level) |
| 5607 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5608 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5609 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5610 | 0, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5611 | if (ret == -EAGAIN) |
| 5612 | goto again; |
| 5613 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5614 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5615 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5616 | goto done; |
| 5617 | } |
| 5618 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5619 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5620 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5621 | if (!ret) { |
| 5622 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5623 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5624 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5625 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5626 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5627 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5628 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5629 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5630 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5631 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5632 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5633 | path->leave_spinning = old_spinning; |
| 5634 | if (!old_spinning) |
| 5635 | btrfs_set_path_blocking(path); |
| 5636 | |
| 5637 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5638 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5639 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5640 | /* |
| 5641 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5642 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5643 | * |
| 5644 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5645 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5646 | int btrfs_previous_item(struct btrfs_root *root, |
| 5647 | struct btrfs_path *path, u64 min_objectid, |
| 5648 | int type) |
| 5649 | { |
| 5650 | struct btrfs_key found_key; |
| 5651 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5652 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5653 | int ret; |
| 5654 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5655 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5656 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5657 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5658 | ret = btrfs_prev_leaf(root, path); |
| 5659 | if (ret != 0) |
| 5660 | return ret; |
| 5661 | } else { |
| 5662 | path->slots[0]--; |
| 5663 | } |
| 5664 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5665 | nritems = btrfs_header_nritems(leaf); |
| 5666 | if (nritems == 0) |
| 5667 | return 1; |
| 5668 | if (path->slots[0] == nritems) |
| 5669 | path->slots[0]--; |
| 5670 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5671 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5672 | if (found_key.objectid < min_objectid) |
| 5673 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5674 | if (found_key.type == type) |
| 5675 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5676 | if (found_key.objectid == min_objectid && |
| 5677 | found_key.type < type) |
| 5678 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5679 | } |
| 5680 | return 1; |
| 5681 | } |