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); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 44 | static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 45 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 46 | struct btrfs_path *btrfs_alloc_path(void) |
| 47 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 48 | struct btrfs_path *path; |
Jeff Mahoney | e00f730 | 2009-02-12 14:11:25 -0500 | [diff] [blame] | 49 | path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 50 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 53 | /* |
| 54 | * set all locked nodes in the path to blocking locks. This should |
| 55 | * be done before scheduling |
| 56 | */ |
| 57 | noinline void btrfs_set_path_blocking(struct btrfs_path *p) |
| 58 | { |
| 59 | int i; |
| 60 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 61 | if (!p->nodes[i] || !p->locks[i]) |
| 62 | continue; |
| 63 | btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 64 | if (p->locks[i] == BTRFS_READ_LOCK) |
| 65 | p->locks[i] = BTRFS_READ_LOCK_BLOCKING; |
| 66 | else if (p->locks[i] == BTRFS_WRITE_LOCK) |
| 67 | p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * reset all the locked nodes in the patch to spinning locks. |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 73 | * |
| 74 | * held is used to keep lockdep happy, when lockdep is enabled |
| 75 | * we set held to a blocking lock before we go around and |
| 76 | * retake all the spinlocks in the path. You can safely use NULL |
| 77 | * for held |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 78 | */ |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 79 | noinline void btrfs_clear_path_blocking(struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 80 | struct extent_buffer *held, int held_rw) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 81 | { |
| 82 | int i; |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 83 | |
| 84 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 85 | /* lockdep really cares that we take all of these spinlocks |
| 86 | * in the right order. If any of the locks in the path are not |
| 87 | * currently blocking, it is going to complain. So, make really |
| 88 | * really sure by forcing the path to blocking before we clear |
| 89 | * the path blocking. |
| 90 | */ |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 91 | if (held) { |
| 92 | btrfs_set_lock_blocking_rw(held, held_rw); |
| 93 | if (held_rw == BTRFS_WRITE_LOCK) |
| 94 | held_rw = BTRFS_WRITE_LOCK_BLOCKING; |
| 95 | else if (held_rw == BTRFS_READ_LOCK) |
| 96 | held_rw = BTRFS_READ_LOCK_BLOCKING; |
| 97 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 98 | btrfs_set_path_blocking(p); |
| 99 | #endif |
| 100 | |
| 101 | for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 102 | if (p->nodes[i] && p->locks[i]) { |
| 103 | btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 104 | if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING) |
| 105 | p->locks[i] = BTRFS_WRITE_LOCK; |
| 106 | else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING) |
| 107 | p->locks[i] = BTRFS_READ_LOCK; |
| 108 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 109 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 110 | |
| 111 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 112 | if (held) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 113 | btrfs_clear_lock_blocking_rw(held, held_rw); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 114 | #endif |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 117 | /* this also releases the path */ |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 118 | void btrfs_free_path(struct btrfs_path *p) |
| 119 | { |
Jesper Juhl | ff175d5 | 2010-12-25 21:22:30 +0000 | [diff] [blame] | 120 | if (!p) |
| 121 | return; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 122 | btrfs_release_path(p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 123 | kmem_cache_free(btrfs_path_cachep, p); |
| 124 | } |
| 125 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 126 | /* |
| 127 | * path release drops references on the extent buffers in the path |
| 128 | * and it drops any locks held by this path |
| 129 | * |
| 130 | * It is safe to call this on paths that no locks or extent buffers held. |
| 131 | */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 132 | noinline void btrfs_release_path(struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 133 | { |
| 134 | int i; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 135 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 136 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 137 | p->slots[i] = 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 138 | if (!p->nodes[i]) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 139 | continue; |
| 140 | if (p->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 141 | btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 142 | p->locks[i] = 0; |
| 143 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 144 | free_extent_buffer(p->nodes[i]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 145 | p->nodes[i] = NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 149 | /* |
| 150 | * safely gets a reference on the root node of a tree. A lock |
| 151 | * is not taken, so a concurrent writer may put a different node |
| 152 | * at the root of the tree. See btrfs_lock_root_node for the |
| 153 | * looping required. |
| 154 | * |
| 155 | * The extent buffer returned by this has a reference taken, so |
| 156 | * it won't disappear. It may stop being the root of the tree |
| 157 | * at any time because there are no locks held. |
| 158 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 159 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
| 160 | { |
| 161 | struct extent_buffer *eb; |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 162 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 163 | while (1) { |
| 164 | rcu_read_lock(); |
| 165 | eb = rcu_dereference(root->node); |
| 166 | |
| 167 | /* |
| 168 | * RCU really hurts here, we could free up the root node because |
| 169 | * it was cow'ed but we may not get the new root node yet so do |
| 170 | * the inc_not_zero dance and if it doesn't work then |
| 171 | * synchronize_rcu and try again. |
| 172 | */ |
| 173 | if (atomic_inc_not_zero(&eb->refs)) { |
| 174 | rcu_read_unlock(); |
| 175 | break; |
| 176 | } |
| 177 | rcu_read_unlock(); |
| 178 | synchronize_rcu(); |
| 179 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 180 | return eb; |
| 181 | } |
| 182 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 183 | /* loop around taking references on and locking the root node of the |
| 184 | * tree until you end up with a lock on the root. A locked buffer |
| 185 | * is returned, with a reference held. |
| 186 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 187 | struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) |
| 188 | { |
| 189 | struct extent_buffer *eb; |
| 190 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 191 | while (1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 192 | eb = btrfs_root_node(root); |
| 193 | btrfs_tree_lock(eb); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 194 | if (eb == root->node) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 195 | break; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 196 | btrfs_tree_unlock(eb); |
| 197 | free_extent_buffer(eb); |
| 198 | } |
| 199 | return eb; |
| 200 | } |
| 201 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 202 | /* loop around taking references on and locking the root node of the |
| 203 | * tree until you end up with a lock on the root. A locked buffer |
| 204 | * is returned, with a reference held. |
| 205 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 206 | 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] | 207 | { |
| 208 | struct extent_buffer *eb; |
| 209 | |
| 210 | while (1) { |
| 211 | eb = btrfs_root_node(root); |
| 212 | btrfs_tree_read_lock(eb); |
| 213 | if (eb == root->node) |
| 214 | break; |
| 215 | btrfs_tree_read_unlock(eb); |
| 216 | free_extent_buffer(eb); |
| 217 | } |
| 218 | return eb; |
| 219 | } |
| 220 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 221 | /* cowonly root (everything not a reference counted cow subvolume), just get |
| 222 | * put onto a simple dirty list. transaction.c walks this to make sure they |
| 223 | * get properly updated on disk. |
| 224 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 225 | static void add_root_to_dirty_list(struct btrfs_root *root) |
| 226 | { |
Chris Mason | e5846fc | 2012-05-03 12:08:48 -0400 | [diff] [blame] | 227 | spin_lock(&root->fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 228 | if (root->track_dirty && list_empty(&root->dirty_list)) { |
| 229 | list_add(&root->dirty_list, |
| 230 | &root->fs_info->dirty_cowonly_roots); |
| 231 | } |
Chris Mason | e5846fc | 2012-05-03 12:08:48 -0400 | [diff] [blame] | 232 | spin_unlock(&root->fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 235 | /* |
| 236 | * used by snapshot creation to make a copy of a root for a tree with |
| 237 | * a given objectid. The buffer with the new root node is returned in |
| 238 | * cow_ret, and this func returns zero on success or a negative error code. |
| 239 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 240 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
| 241 | struct btrfs_root *root, |
| 242 | struct extent_buffer *buf, |
| 243 | struct extent_buffer **cow_ret, u64 new_root_objectid) |
| 244 | { |
| 245 | struct extent_buffer *cow; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 246 | int ret = 0; |
| 247 | int level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 248 | struct btrfs_disk_key disk_key; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 249 | |
| 250 | WARN_ON(root->ref_cows && trans->transid != |
| 251 | root->fs_info->running_transaction->transid); |
| 252 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
| 253 | |
| 254 | level = btrfs_header_level(buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 255 | if (level == 0) |
| 256 | btrfs_item_key(buf, &disk_key, 0); |
| 257 | else |
| 258 | btrfs_node_key(buf, &disk_key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 259 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 260 | cow = btrfs_alloc_free_block(trans, root, buf->len, 0, |
| 261 | new_root_objectid, &disk_key, level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 262 | buf->start, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 263 | if (IS_ERR(cow)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 264 | return PTR_ERR(cow); |
| 265 | |
| 266 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
| 267 | btrfs_set_header_bytenr(cow, cow->start); |
| 268 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 269 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 270 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 271 | BTRFS_HEADER_FLAG_RELOC); |
| 272 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 273 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 274 | else |
| 275 | btrfs_set_header_owner(cow, new_root_objectid); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 276 | |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame] | 277 | write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(cow), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 278 | BTRFS_FSID_SIZE); |
| 279 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 280 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 281 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 282 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 283 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 284 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 285 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 286 | if (ret) |
| 287 | return ret; |
| 288 | |
| 289 | btrfs_mark_buffer_dirty(cow); |
| 290 | *cow_ret = cow; |
| 291 | return 0; |
| 292 | } |
| 293 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 294 | enum mod_log_op { |
| 295 | MOD_LOG_KEY_REPLACE, |
| 296 | MOD_LOG_KEY_ADD, |
| 297 | MOD_LOG_KEY_REMOVE, |
| 298 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, |
| 299 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, |
| 300 | MOD_LOG_MOVE_KEYS, |
| 301 | MOD_LOG_ROOT_REPLACE, |
| 302 | }; |
| 303 | |
| 304 | struct tree_mod_move { |
| 305 | int dst_slot; |
| 306 | int nr_items; |
| 307 | }; |
| 308 | |
| 309 | struct tree_mod_root { |
| 310 | u64 logical; |
| 311 | u8 level; |
| 312 | }; |
| 313 | |
| 314 | struct tree_mod_elem { |
| 315 | struct rb_node node; |
| 316 | u64 index; /* shifted logical */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 317 | u64 seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 318 | enum mod_log_op op; |
| 319 | |
| 320 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ |
| 321 | int slot; |
| 322 | |
| 323 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ |
| 324 | u64 generation; |
| 325 | |
| 326 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ |
| 327 | struct btrfs_disk_key key; |
| 328 | u64 blockptr; |
| 329 | |
| 330 | /* this is used for op == MOD_LOG_MOVE_KEYS */ |
| 331 | struct tree_mod_move move; |
| 332 | |
| 333 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ |
| 334 | struct tree_mod_root old_root; |
| 335 | }; |
| 336 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 337 | 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] | 338 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 339 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 342 | 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] | 343 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 344 | read_unlock(&fs_info->tree_mod_log_lock); |
| 345 | } |
| 346 | |
| 347 | static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info) |
| 348 | { |
| 349 | write_lock(&fs_info->tree_mod_log_lock); |
| 350 | } |
| 351 | |
| 352 | static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info) |
| 353 | { |
| 354 | write_unlock(&fs_info->tree_mod_log_lock); |
| 355 | } |
| 356 | |
| 357 | /* |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 358 | * Increment the upper half of tree_mod_seq, set lower half zero. |
| 359 | * |
| 360 | * Must be called with fs_info->tree_mod_seq_lock held. |
| 361 | */ |
| 362 | static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info) |
| 363 | { |
| 364 | u64 seq = atomic64_read(&fs_info->tree_mod_seq); |
| 365 | seq &= 0xffffffff00000000ull; |
| 366 | seq += 1ull << 32; |
| 367 | atomic64_set(&fs_info->tree_mod_seq, seq); |
| 368 | return seq; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Increment the lower half of tree_mod_seq. |
| 373 | * |
| 374 | * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers |
| 375 | * are generated should not technically require a spin lock here. (Rationale: |
| 376 | * incrementing the minor while incrementing the major seq number is between its |
| 377 | * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it |
| 378 | * just returns a unique sequence number as usual.) We have decided to leave |
| 379 | * that requirement in here and rethink it once we notice it really imposes a |
| 380 | * problem on some workload. |
| 381 | */ |
| 382 | static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info) |
| 383 | { |
| 384 | return atomic64_inc_return(&fs_info->tree_mod_seq); |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * return the last minor in the previous major tree_mod_seq number |
| 389 | */ |
| 390 | u64 btrfs_tree_mod_seq_prev(u64 seq) |
| 391 | { |
| 392 | return (seq & 0xffffffff00000000ull) - 1ull; |
| 393 | } |
| 394 | |
| 395 | /* |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 396 | * This adds a new blocker to the tree mod log's blocker list if the @elem |
| 397 | * passed does not already have a sequence number set. So when a caller expects |
| 398 | * to record tree modifications, it should ensure to set elem->seq to zero |
| 399 | * before calling btrfs_get_tree_mod_seq. |
| 400 | * Returns a fresh, unused tree log modification sequence number, even if no new |
| 401 | * blocker was added. |
| 402 | */ |
| 403 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 404 | struct seq_list *elem) |
| 405 | { |
| 406 | u64 seq; |
| 407 | |
| 408 | tree_mod_log_write_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 409 | spin_lock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 410 | if (!elem->seq) { |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 411 | elem->seq = btrfs_inc_tree_mod_seq_major(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 412 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 413 | } |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 414 | seq = btrfs_inc_tree_mod_seq_minor(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 415 | spin_unlock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 416 | tree_mod_log_write_unlock(fs_info); |
| 417 | |
| 418 | return seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 422 | struct seq_list *elem) |
| 423 | { |
| 424 | struct rb_root *tm_root; |
| 425 | struct rb_node *node; |
| 426 | struct rb_node *next; |
| 427 | struct seq_list *cur_elem; |
| 428 | struct tree_mod_elem *tm; |
| 429 | u64 min_seq = (u64)-1; |
| 430 | u64 seq_putting = elem->seq; |
| 431 | |
| 432 | if (!seq_putting) |
| 433 | return; |
| 434 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 435 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 436 | list_del(&elem->list); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 437 | elem->seq = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 438 | |
| 439 | 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] | 440 | if (cur_elem->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 441 | if (seq_putting > cur_elem->seq) { |
| 442 | /* |
| 443 | * blocker with lower sequence number exists, we |
| 444 | * cannot remove anything from the log |
| 445 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 446 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 447 | return; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 448 | } |
| 449 | min_seq = cur_elem->seq; |
| 450 | } |
| 451 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 452 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 453 | |
| 454 | /* |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 455 | * anything that's lower than the lowest existing (read: blocked) |
| 456 | * sequence number can be removed from the tree. |
| 457 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 458 | tree_mod_log_write_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 459 | tm_root = &fs_info->tree_mod_log; |
| 460 | for (node = rb_first(tm_root); node; node = next) { |
| 461 | next = rb_next(node); |
| 462 | tm = container_of(node, struct tree_mod_elem, node); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 463 | if (tm->seq > min_seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 464 | continue; |
| 465 | rb_erase(node, tm_root); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 466 | kfree(tm); |
| 467 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 468 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* |
| 472 | * key order of the log: |
| 473 | * index -> sequence |
| 474 | * |
| 475 | * the index is the shifted logical of the *new* root node for root replace |
| 476 | * operations, or the shifted logical of the affected block for all other |
| 477 | * operations. |
| 478 | */ |
| 479 | static noinline int |
| 480 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 481 | { |
| 482 | struct rb_root *tm_root; |
| 483 | struct rb_node **new; |
| 484 | struct rb_node *parent = NULL; |
| 485 | struct tree_mod_elem *cur; |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 486 | int ret = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 487 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 488 | BUG_ON(!tm); |
| 489 | |
| 490 | tree_mod_log_write_lock(fs_info); |
| 491 | if (list_empty(&fs_info->tree_mod_seq_list)) { |
| 492 | tree_mod_log_write_unlock(fs_info); |
| 493 | /* |
| 494 | * Ok we no longer care about logging modifications, free up tm |
| 495 | * and return 0. Any callers shouldn't be using tm after |
| 496 | * calling tree_mod_log_insert, but if they do we can just |
| 497 | * change this to return a special error code to let the callers |
| 498 | * do their own thing. |
| 499 | */ |
| 500 | kfree(tm); |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 505 | tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info); |
| 506 | spin_unlock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 507 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 508 | tm_root = &fs_info->tree_mod_log; |
| 509 | new = &tm_root->rb_node; |
| 510 | while (*new) { |
| 511 | cur = container_of(*new, struct tree_mod_elem, node); |
| 512 | parent = *new; |
| 513 | if (cur->index < tm->index) |
| 514 | new = &((*new)->rb_left); |
| 515 | else if (cur->index > tm->index) |
| 516 | new = &((*new)->rb_right); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 517 | else if (cur->seq < tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 518 | new = &((*new)->rb_left); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 519 | else if (cur->seq > tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 520 | new = &((*new)->rb_right); |
| 521 | else { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 522 | ret = -EEXIST; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 523 | kfree(tm); |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 524 | goto out; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | rb_link_node(&tm->node, parent, new); |
| 529 | rb_insert_color(&tm->node, tm_root); |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 530 | out: |
| 531 | tree_mod_log_write_unlock(fs_info); |
| 532 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 535 | /* |
| 536 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it |
| 537 | * returns zero with the tree_mod_log_lock acquired. The caller must hold |
| 538 | * this until all tree mod log insertions are recorded in the rb tree and then |
| 539 | * call tree_mod_log_write_unlock() to release. |
| 540 | */ |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 541 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 542 | struct extent_buffer *eb) { |
| 543 | smp_mb(); |
| 544 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 545 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 546 | if (eb && btrfs_header_level(eb) == 0) |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 547 | return 1; |
| 548 | return 0; |
| 549 | } |
| 550 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 551 | static inline int |
| 552 | __tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, |
| 553 | struct extent_buffer *eb, int slot, |
| 554 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 555 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 556 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 557 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 558 | tm = kzalloc(sizeof(*tm), flags); |
| 559 | if (!tm) |
| 560 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 561 | |
| 562 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 563 | if (op != MOD_LOG_KEY_ADD) { |
| 564 | btrfs_node_key(eb, &tm->key, slot); |
| 565 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 566 | } |
| 567 | tm->op = op; |
| 568 | tm->slot = slot; |
| 569 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
| 570 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 571 | return __tree_mod_log_insert(fs_info, tm); |
| 572 | } |
| 573 | |
| 574 | static noinline int |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 575 | tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, |
| 576 | struct extent_buffer *eb, int slot, |
| 577 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 578 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 579 | if (tree_mod_dont_log(fs_info, eb)) |
| 580 | return 0; |
| 581 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 582 | return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static noinline int |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 586 | tree_mod_log_insert_move(struct btrfs_fs_info *fs_info, |
| 587 | struct extent_buffer *eb, int dst_slot, int src_slot, |
| 588 | int nr_items, gfp_t flags) |
| 589 | { |
| 590 | struct tree_mod_elem *tm; |
| 591 | int ret; |
| 592 | int i; |
| 593 | |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 594 | if (tree_mod_dont_log(fs_info, eb)) |
| 595 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 596 | |
Jan Schmidt | 01763a2 | 2012-10-23 15:02:12 +0200 | [diff] [blame] | 597 | /* |
| 598 | * When we override something during the move, we log these removals. |
| 599 | * This can only happen when we move towards the beginning of the |
| 600 | * buffer, i.e. dst_slot < src_slot. |
| 601 | */ |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 602 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 603 | ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot, |
| 604 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 605 | BUG_ON(ret < 0); |
| 606 | } |
| 607 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 608 | tm = kzalloc(sizeof(*tm), flags); |
| 609 | if (!tm) |
| 610 | return -ENOMEM; |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 611 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 612 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 613 | tm->slot = src_slot; |
| 614 | tm->move.dst_slot = dst_slot; |
| 615 | tm->move.nr_items = nr_items; |
| 616 | tm->op = MOD_LOG_MOVE_KEYS; |
| 617 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 618 | return __tree_mod_log_insert(fs_info, tm); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 621 | static inline void |
| 622 | __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb) |
| 623 | { |
| 624 | int i; |
| 625 | u32 nritems; |
| 626 | int ret; |
| 627 | |
Chris Mason | b12a3b1 | 2012-08-07 15:34:49 -0400 | [diff] [blame] | 628 | if (btrfs_header_level(eb) == 0) |
| 629 | return; |
| 630 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 631 | nritems = btrfs_header_nritems(eb); |
| 632 | for (i = nritems - 1; i >= 0; i--) { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 633 | ret = __tree_mod_log_insert_key(fs_info, eb, i, |
| 634 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 635 | BUG_ON(ret < 0); |
| 636 | } |
| 637 | } |
| 638 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 639 | static noinline int |
| 640 | tree_mod_log_insert_root(struct btrfs_fs_info *fs_info, |
| 641 | struct extent_buffer *old_root, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 642 | struct extent_buffer *new_root, gfp_t flags, |
| 643 | int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 644 | { |
| 645 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 646 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 647 | if (tree_mod_dont_log(fs_info, NULL)) |
| 648 | return 0; |
| 649 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 650 | if (log_removal) |
| 651 | __tree_mod_log_free_eb(fs_info, old_root); |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 652 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 653 | tm = kzalloc(sizeof(*tm), flags); |
| 654 | if (!tm) |
| 655 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 656 | |
| 657 | tm->index = new_root->start >> PAGE_CACHE_SHIFT; |
| 658 | tm->old_root.logical = old_root->start; |
| 659 | tm->old_root.level = btrfs_header_level(old_root); |
| 660 | tm->generation = btrfs_header_generation(old_root); |
| 661 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 662 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 663 | return __tree_mod_log_insert(fs_info, tm); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static struct tree_mod_elem * |
| 667 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 668 | int smallest) |
| 669 | { |
| 670 | struct rb_root *tm_root; |
| 671 | struct rb_node *node; |
| 672 | struct tree_mod_elem *cur = NULL; |
| 673 | struct tree_mod_elem *found = NULL; |
| 674 | u64 index = start >> PAGE_CACHE_SHIFT; |
| 675 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 676 | tree_mod_log_read_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 677 | tm_root = &fs_info->tree_mod_log; |
| 678 | node = tm_root->rb_node; |
| 679 | while (node) { |
| 680 | cur = container_of(node, struct tree_mod_elem, node); |
| 681 | if (cur->index < index) { |
| 682 | node = node->rb_left; |
| 683 | } else if (cur->index > index) { |
| 684 | node = node->rb_right; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 685 | } else if (cur->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 686 | node = node->rb_left; |
| 687 | } else if (!smallest) { |
| 688 | /* we want the node with the highest seq */ |
| 689 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 690 | BUG_ON(found->seq > cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 691 | found = cur; |
| 692 | node = node->rb_left; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 693 | } else if (cur->seq > min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 694 | /* we want the node with the smallest seq */ |
| 695 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 696 | BUG_ON(found->seq < cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 697 | found = cur; |
| 698 | node = node->rb_right; |
| 699 | } else { |
| 700 | found = cur; |
| 701 | break; |
| 702 | } |
| 703 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 704 | tree_mod_log_read_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 705 | |
| 706 | return found; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * this returns the element from the log with the smallest time sequence |
| 711 | * value that's in the log (the oldest log item). any element with a time |
| 712 | * sequence lower than min_seq will be ignored. |
| 713 | */ |
| 714 | static struct tree_mod_elem * |
| 715 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 716 | u64 min_seq) |
| 717 | { |
| 718 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * this returns the element from the log with the largest time sequence |
| 723 | * value that's in the log (the most recent log item). any element with |
| 724 | * a time sequence lower than min_seq will be ignored. |
| 725 | */ |
| 726 | static struct tree_mod_elem * |
| 727 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 728 | { |
| 729 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 730 | } |
| 731 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 732 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 733 | tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 734 | struct extent_buffer *src, unsigned long dst_offset, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 735 | unsigned long src_offset, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 736 | { |
| 737 | int ret; |
| 738 | int i; |
| 739 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 740 | if (tree_mod_dont_log(fs_info, NULL)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 741 | return; |
| 742 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 743 | if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 744 | return; |
| 745 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 746 | for (i = 0; i < nr_items; i++) { |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 747 | ret = __tree_mod_log_insert_key(fs_info, src, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 748 | i + src_offset, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 749 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 750 | BUG_ON(ret < 0); |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 751 | ret = __tree_mod_log_insert_key(fs_info, dst, |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 752 | i + dst_offset, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 753 | MOD_LOG_KEY_ADD, |
| 754 | GFP_NOFS); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 755 | BUG_ON(ret < 0); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static inline void |
| 760 | tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 761 | int dst_offset, int src_offset, int nr_items) |
| 762 | { |
| 763 | int ret; |
| 764 | ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset, |
| 765 | nr_items, GFP_NOFS); |
| 766 | BUG_ON(ret < 0); |
| 767 | } |
| 768 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 769 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 770 | tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 771 | struct extent_buffer *eb, int slot, int atomic) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 772 | { |
| 773 | int ret; |
| 774 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 775 | ret = __tree_mod_log_insert_key(fs_info, eb, slot, |
| 776 | MOD_LOG_KEY_REPLACE, |
| 777 | atomic ? GFP_ATOMIC : GFP_NOFS); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 778 | BUG_ON(ret < 0); |
| 779 | } |
| 780 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 781 | static noinline void |
| 782 | 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] | 783 | { |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 784 | if (tree_mod_dont_log(fs_info, eb)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 785 | return; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 786 | __tree_mod_log_free_eb(fs_info, eb); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 787 | } |
| 788 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 789 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 790 | tree_mod_log_set_root_pointer(struct btrfs_root *root, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 791 | struct extent_buffer *new_root_node, |
| 792 | int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 793 | { |
| 794 | int ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 795 | ret = tree_mod_log_insert_root(root->fs_info, root->node, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 796 | new_root_node, GFP_NOFS, log_removal); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 797 | BUG_ON(ret < 0); |
| 798 | } |
| 799 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 800 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 801 | * check if the tree block can be shared by multiple trees |
| 802 | */ |
| 803 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 804 | struct extent_buffer *buf) |
| 805 | { |
| 806 | /* |
| 807 | * Tree blocks not in refernece counted trees and tree roots |
| 808 | * are never shared. If a block was allocated after the last |
| 809 | * snapshot and the block was not allocated by tree relocation, |
| 810 | * we know the block is not shared. |
| 811 | */ |
| 812 | if (root->ref_cows && |
| 813 | buf != root->node && buf != root->commit_root && |
| 814 | (btrfs_header_generation(buf) <= |
| 815 | btrfs_root_last_snapshot(&root->root_item) || |
| 816 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 817 | return 1; |
| 818 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 819 | if (root->ref_cows && |
| 820 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 821 | return 1; |
| 822 | #endif |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 827 | struct btrfs_root *root, |
| 828 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 829 | struct extent_buffer *cow, |
| 830 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 831 | { |
| 832 | u64 refs; |
| 833 | u64 owner; |
| 834 | u64 flags; |
| 835 | u64 new_flags = 0; |
| 836 | int ret; |
| 837 | |
| 838 | /* |
| 839 | * Backrefs update rules: |
| 840 | * |
| 841 | * Always use full backrefs for extent pointers in tree block |
| 842 | * allocated by tree relocation. |
| 843 | * |
| 844 | * If a shared tree block is no longer referenced by its owner |
| 845 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 846 | * use full backrefs for extent pointers in tree block. |
| 847 | * |
| 848 | * If a tree block is been relocating |
| 849 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 850 | * use full backrefs for extent pointers in tree block. |
| 851 | * The reason for this is some operations (such as drop tree) |
| 852 | * are only allowed for blocks use full backrefs. |
| 853 | */ |
| 854 | |
| 855 | if (btrfs_block_can_be_shared(root, buf)) { |
| 856 | ret = btrfs_lookup_extent_info(trans, root, buf->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 857 | btrfs_header_level(buf), 1, |
| 858 | &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 859 | if (ret) |
| 860 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 861 | if (refs == 0) { |
| 862 | ret = -EROFS; |
| 863 | btrfs_std_error(root->fs_info, ret); |
| 864 | return ret; |
| 865 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 866 | } else { |
| 867 | refs = 1; |
| 868 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 869 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 870 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 871 | else |
| 872 | flags = 0; |
| 873 | } |
| 874 | |
| 875 | owner = btrfs_header_owner(buf); |
| 876 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 877 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 878 | |
| 879 | if (refs > 1) { |
| 880 | if ((owner == root->root_key.objectid || |
| 881 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 882 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 883 | ret = btrfs_inc_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 884 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 885 | |
| 886 | if (root->root_key.objectid == |
| 887 | BTRFS_TREE_RELOC_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 888 | ret = btrfs_dec_ref(trans, root, buf, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 889 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 890 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 891 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 892 | } |
| 893 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 894 | } else { |
| 895 | |
| 896 | if (root->root_key.objectid == |
| 897 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 898 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 899 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 900 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 901 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 902 | } |
| 903 | if (new_flags != 0) { |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 904 | int level = btrfs_header_level(buf); |
| 905 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 906 | ret = btrfs_set_disk_extent_flags(trans, root, |
| 907 | buf->start, |
| 908 | buf->len, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 909 | new_flags, level, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 910 | if (ret) |
| 911 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 912 | } |
| 913 | } else { |
| 914 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 915 | if (root->root_key.objectid == |
| 916 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 917 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 919 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 920 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 921 | ret = btrfs_dec_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 922 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 923 | } |
| 924 | clean_tree_block(trans, root, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 925 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 926 | } |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 931 | * does the dirty work in cow of a single block. The parent block (if |
| 932 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 933 | * dirty and returned locked. If you modify the block it needs to be marked |
| 934 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 935 | * |
| 936 | * search_start -- an allocation hint for the new block |
| 937 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 938 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 939 | * bytes the allocator should try to find free next to the block it returns. |
| 940 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 941 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 942 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 943 | struct btrfs_root *root, |
| 944 | struct extent_buffer *buf, |
| 945 | struct extent_buffer *parent, int parent_slot, |
| 946 | struct extent_buffer **cow_ret, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 947 | u64 search_start, u64 empty_size) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 948 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 949 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 950 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 951 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 952 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 953 | int unlock_orig = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 954 | u64 parent_start; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 955 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 956 | if (*cow_ret == buf) |
| 957 | unlock_orig = 1; |
| 958 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 959 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 960 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 961 | WARN_ON(root->ref_cows && trans->transid != |
| 962 | root->fs_info->running_transaction->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 963 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 964 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 965 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 966 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 967 | if (level == 0) |
| 968 | btrfs_item_key(buf, &disk_key, 0); |
| 969 | else |
| 970 | btrfs_node_key(buf, &disk_key, 0); |
| 971 | |
| 972 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 973 | if (parent) |
| 974 | parent_start = parent->start; |
| 975 | else |
| 976 | parent_start = 0; |
| 977 | } else |
| 978 | parent_start = 0; |
| 979 | |
| 980 | cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, |
| 981 | root->root_key.objectid, &disk_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 982 | level, search_start, empty_size); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 983 | if (IS_ERR(cow)) |
| 984 | return PTR_ERR(cow); |
| 985 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 986 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 987 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 988 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 989 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 990 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 991 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 992 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 993 | BTRFS_HEADER_FLAG_RELOC); |
| 994 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 995 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 996 | else |
| 997 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 998 | |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame] | 999 | write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(cow), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1000 | BTRFS_FSID_SIZE); |
| 1001 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1002 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1003 | if (ret) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1004 | btrfs_abort_transaction(trans, root, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1005 | return ret; |
| 1006 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1007 | |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1008 | if (root->ref_cows) { |
| 1009 | ret = btrfs_reloc_cow_block(trans, root, buf, cow); |
| 1010 | if (ret) |
| 1011 | return ret; |
| 1012 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1013 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1014 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1015 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1016 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1017 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1018 | parent_start = buf->start; |
| 1019 | else |
| 1020 | parent_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1021 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1022 | extent_buffer_get(cow); |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1023 | tree_mod_log_set_root_pointer(root, cow, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1024 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1025 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1026 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1027 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1028 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1029 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1030 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1031 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1032 | parent_start = parent->start; |
| 1033 | else |
| 1034 | parent_start = 0; |
| 1035 | |
| 1036 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1037 | tree_mod_log_insert_key(root->fs_info, parent, parent_slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 1038 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1039 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1040 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1041 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1042 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1043 | btrfs_mark_buffer_dirty(parent); |
Josef Bacik | 7fb7d76f | 2013-07-01 16:10:16 -0400 | [diff] [blame] | 1044 | if (last_ref) |
| 1045 | tree_mod_log_free_eb(root->fs_info, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1046 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1047 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1048 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1049 | if (unlock_orig) |
| 1050 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1051 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1052 | btrfs_mark_buffer_dirty(cow); |
| 1053 | *cow_ret = cow; |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1057 | /* |
| 1058 | * returns the logical address of the oldest predecessor of the given root. |
| 1059 | * entries older than time_seq are ignored. |
| 1060 | */ |
| 1061 | static struct tree_mod_elem * |
| 1062 | __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1063 | struct extent_buffer *eb_root, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1064 | { |
| 1065 | struct tree_mod_elem *tm; |
| 1066 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1067 | u64 root_logical = eb_root->start; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1068 | int looped = 0; |
| 1069 | |
| 1070 | if (!time_seq) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1071 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1072 | |
| 1073 | /* |
| 1074 | * the very last operation that's logged for a root is the replacement |
| 1075 | * operation (if it is replaced at all). this has the index of the *new* |
| 1076 | * root, making it the very first operation that's logged for this root. |
| 1077 | */ |
| 1078 | while (1) { |
| 1079 | tm = tree_mod_log_search_oldest(fs_info, root_logical, |
| 1080 | time_seq); |
| 1081 | if (!looped && !tm) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1082 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1083 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1084 | * if there are no tree operation for the oldest root, we simply |
| 1085 | * return it. this should only happen if that (old) root is at |
| 1086 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1087 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1088 | if (!tm) |
| 1089 | break; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1090 | |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1091 | /* |
| 1092 | * if there's an operation that's not a root replacement, we |
| 1093 | * found the oldest version of our root. normally, we'll find a |
| 1094 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1095 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1096 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1097 | break; |
| 1098 | |
| 1099 | found = tm; |
| 1100 | root_logical = tm->old_root.logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1101 | looped = 1; |
| 1102 | } |
| 1103 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1104 | /* if there's no old root to return, return what we found instead */ |
| 1105 | if (!found) |
| 1106 | found = tm; |
| 1107 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1108 | return found; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * tm is a pointer to the first operation to rewind within eb. then, all |
| 1113 | * previous operations will be rewinded (until we reach something older than |
| 1114 | * time_seq). |
| 1115 | */ |
| 1116 | static void |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1117 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1118 | u64 time_seq, struct tree_mod_elem *first_tm) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1119 | { |
| 1120 | u32 n; |
| 1121 | struct rb_node *next; |
| 1122 | struct tree_mod_elem *tm = first_tm; |
| 1123 | unsigned long o_dst; |
| 1124 | unsigned long o_src; |
| 1125 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1126 | |
| 1127 | n = btrfs_header_nritems(eb); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1128 | tree_mod_log_read_lock(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1129 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1130 | /* |
| 1131 | * all the operations are recorded with the operator used for |
| 1132 | * the modification. as we're going backwards, we do the |
| 1133 | * opposite of each operation here. |
| 1134 | */ |
| 1135 | switch (tm->op) { |
| 1136 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1137 | BUG_ON(tm->slot < n); |
Eric Sandeen | 1c697d4 | 2013-01-31 00:54:56 +0000 | [diff] [blame] | 1138 | /* Fallthrough */ |
Liu Bo | 95c80bb | 2012-10-19 09:50:52 +0000 | [diff] [blame] | 1139 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1140 | case MOD_LOG_KEY_REMOVE: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1141 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1142 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1143 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1144 | tm->generation); |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1145 | n++; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1146 | break; |
| 1147 | case MOD_LOG_KEY_REPLACE: |
| 1148 | BUG_ON(tm->slot >= n); |
| 1149 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1150 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1151 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1152 | tm->generation); |
| 1153 | break; |
| 1154 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1155 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1156 | n--; |
| 1157 | break; |
| 1158 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1159 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1160 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1161 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1162 | tm->move.nr_items * p_size); |
| 1163 | break; |
| 1164 | case MOD_LOG_ROOT_REPLACE: |
| 1165 | /* |
| 1166 | * this operation is special. for roots, this must be |
| 1167 | * handled explicitly before rewinding. |
| 1168 | * for non-roots, this operation may exist if the node |
| 1169 | * was a root: root A -> child B; then A gets empty and |
| 1170 | * B is promoted to the new root. in the mod log, we'll |
| 1171 | * have a root-replace operation for B, a tree block |
| 1172 | * that is no root. we simply ignore that operation. |
| 1173 | */ |
| 1174 | break; |
| 1175 | } |
| 1176 | next = rb_next(&tm->node); |
| 1177 | if (!next) |
| 1178 | break; |
| 1179 | tm = container_of(next, struct tree_mod_elem, node); |
| 1180 | if (tm->index != first_tm->index) |
| 1181 | break; |
| 1182 | } |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1183 | tree_mod_log_read_unlock(fs_info); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1184 | btrfs_set_header_nritems(eb, n); |
| 1185 | } |
| 1186 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1187 | /* |
| 1188 | * Called with eb read locked. If the buffer cannot be rewinded, the same buffer |
| 1189 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
| 1190 | * returned buffer is always read-locked. If the returned buffer is not the |
| 1191 | * input buffer, the lock on the input buffer is released and the input buffer |
| 1192 | * is freed (its refcount is decremented). |
| 1193 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1194 | static struct extent_buffer * |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1195 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 1196 | struct extent_buffer *eb, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1197 | { |
| 1198 | struct extent_buffer *eb_rewin; |
| 1199 | struct tree_mod_elem *tm; |
| 1200 | |
| 1201 | if (!time_seq) |
| 1202 | return eb; |
| 1203 | |
| 1204 | if (btrfs_header_level(eb) == 0) |
| 1205 | return eb; |
| 1206 | |
| 1207 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1208 | if (!tm) |
| 1209 | return eb; |
| 1210 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1211 | btrfs_set_path_blocking(path); |
| 1212 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1213 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1214 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1215 | BUG_ON(tm->slot != 0); |
| 1216 | eb_rewin = alloc_dummy_extent_buffer(eb->start, |
| 1217 | fs_info->tree_root->nodesize); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1218 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1219 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1220 | free_extent_buffer(eb); |
| 1221 | return NULL; |
| 1222 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1223 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1224 | btrfs_set_header_backref_rev(eb_rewin, |
| 1225 | btrfs_header_backref_rev(eb)); |
| 1226 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1227 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1228 | } else { |
| 1229 | eb_rewin = btrfs_clone_extent_buffer(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1230 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1231 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1232 | free_extent_buffer(eb); |
| 1233 | return NULL; |
| 1234 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1237 | btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK); |
| 1238 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1239 | free_extent_buffer(eb); |
| 1240 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1241 | extent_buffer_get(eb_rewin); |
| 1242 | btrfs_tree_read_lock(eb_rewin); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1243 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1244 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
Arne Jansen | 2a745b1 | 2013-02-13 04:20:01 -0700 | [diff] [blame] | 1245 | BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1246 | |
| 1247 | return eb_rewin; |
| 1248 | } |
| 1249 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1250 | /* |
| 1251 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1252 | * value. If there are no changes, the current root->root_node is returned. If |
| 1253 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1254 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1255 | * Returns NULL on error (with no locks held). |
| 1256 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1257 | static inline struct extent_buffer * |
| 1258 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1259 | { |
| 1260 | struct tree_mod_elem *tm; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1261 | struct extent_buffer *eb = NULL; |
| 1262 | struct extent_buffer *eb_root; |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1263 | struct extent_buffer *old; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1264 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1265 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1266 | u64 logical; |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1267 | u32 blocksize; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1268 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1269 | eb_root = btrfs_read_lock_root_node(root); |
| 1270 | 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] | 1271 | if (!tm) |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1272 | return eb_root; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1273 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1274 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1275 | old_root = &tm->old_root; |
| 1276 | old_generation = tm->generation; |
| 1277 | logical = old_root->logical; |
| 1278 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1279 | logical = eb_root->start; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1280 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1281 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1282 | tm = tree_mod_log_search(root->fs_info, logical, time_seq); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1283 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1284 | btrfs_tree_read_unlock(eb_root); |
| 1285 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1286 | blocksize = btrfs_level_size(root, old_root->level); |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1287 | old = read_tree_block(root, logical, blocksize, 0); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1288 | if (!old || !extent_buffer_uptodate(old)) { |
| 1289 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1290 | pr_warn("btrfs: failed to read tree block %llu from get_old_root\n", |
| 1291 | logical); |
| 1292 | WARN_ON(1); |
| 1293 | } else { |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1294 | eb = btrfs_clone_extent_buffer(old); |
| 1295 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1296 | } |
| 1297 | } else if (old_root) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1298 | btrfs_tree_read_unlock(eb_root); |
| 1299 | free_extent_buffer(eb_root); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1300 | eb = alloc_dummy_extent_buffer(logical, root->nodesize); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1301 | } else { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1302 | btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1303 | eb = btrfs_clone_extent_buffer(eb_root); |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1304 | btrfs_tree_read_unlock_blocking(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1305 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1308 | if (!eb) |
| 1309 | return NULL; |
Jan Schmidt | d638108 | 2012-10-23 14:21:05 +0200 | [diff] [blame] | 1310 | extent_buffer_get(eb); |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1311 | btrfs_tree_read_lock(eb); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1312 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1313 | btrfs_set_header_bytenr(eb, eb->start); |
| 1314 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1315 | btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1316 | btrfs_set_header_level(eb, old_root->level); |
| 1317 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1318 | } |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1319 | if (tm) |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1320 | __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1321 | else |
| 1322 | WARN_ON(btrfs_header_level(eb) != 0); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1323 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1324 | |
| 1325 | return eb; |
| 1326 | } |
| 1327 | |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1328 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
| 1329 | { |
| 1330 | struct tree_mod_elem *tm; |
| 1331 | int level; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1332 | struct extent_buffer *eb_root = btrfs_root_node(root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1333 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1334 | 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] | 1335 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1336 | level = tm->old_root.level; |
| 1337 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1338 | level = btrfs_header_level(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1339 | } |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1340 | free_extent_buffer(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1341 | |
| 1342 | return level; |
| 1343 | } |
| 1344 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1345 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1346 | struct btrfs_root *root, |
| 1347 | struct extent_buffer *buf) |
| 1348 | { |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1349 | /* ensure we can see the force_cow */ |
| 1350 | smp_rmb(); |
| 1351 | |
| 1352 | /* |
| 1353 | * We do not need to cow a block if |
| 1354 | * 1) this block is not created or changed in this transaction; |
| 1355 | * 2) this block does not belong to TREE_RELOC tree; |
| 1356 | * 3) the root is not forced COW. |
| 1357 | * |
| 1358 | * What is forced COW: |
| 1359 | * when we create snapshot during commiting the transaction, |
| 1360 | * after we've finished coping src root, we must COW the shared |
| 1361 | * block to ensure the metadata consistency. |
| 1362 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1363 | if (btrfs_header_generation(buf) == trans->transid && |
| 1364 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1365 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1366 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
| 1367 | !root->force_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1368 | return 0; |
| 1369 | return 1; |
| 1370 | } |
| 1371 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1372 | /* |
| 1373 | * cows a single block, see __btrfs_cow_block for the real work. |
| 1374 | * This version of it has extra checks so that a block isn't cow'd more than |
| 1375 | * once per transaction, as long as it hasn't been written yet |
| 1376 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1377 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1378 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1379 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1380 | struct extent_buffer **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1381 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1382 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1383 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1384 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1385 | if (trans->transaction != root->fs_info->running_transaction) |
| 1386 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1387 | trans->transid, |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1388 | root->fs_info->running_transaction->transid); |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (trans->transid != root->fs_info->generation) |
| 1391 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1392 | trans->transid, root->fs_info->generation); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1393 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1394 | if (!should_cow_block(trans, root, buf)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1395 | *cow_ret = buf; |
| 1396 | return 0; |
| 1397 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1398 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1399 | search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1400 | |
| 1401 | if (parent) |
| 1402 | btrfs_set_lock_blocking(parent); |
| 1403 | btrfs_set_lock_blocking(buf); |
| 1404 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1405 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1406 | parent_slot, cow_ret, search_start, 0); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1407 | |
| 1408 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1409 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1410 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1411 | } |
| 1412 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1413 | /* |
| 1414 | * helper function for defrag to decide if two blocks pointed to by a |
| 1415 | * node are actually close by |
| 1416 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1417 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1418 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1419 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1420 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1421 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1422 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1426 | /* |
| 1427 | * compare two keys in a memcmp fashion |
| 1428 | */ |
| 1429 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
| 1430 | { |
| 1431 | struct btrfs_key k1; |
| 1432 | |
| 1433 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1434 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1435 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1436 | } |
| 1437 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1438 | /* |
| 1439 | * same as comp_keys only with two btrfs_key's |
| 1440 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1441 | 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] | 1442 | { |
| 1443 | if (k1->objectid > k2->objectid) |
| 1444 | return 1; |
| 1445 | if (k1->objectid < k2->objectid) |
| 1446 | return -1; |
| 1447 | if (k1->type > k2->type) |
| 1448 | return 1; |
| 1449 | if (k1->type < k2->type) |
| 1450 | return -1; |
| 1451 | if (k1->offset > k2->offset) |
| 1452 | return 1; |
| 1453 | if (k1->offset < k2->offset) |
| 1454 | return -1; |
| 1455 | return 0; |
| 1456 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1457 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1458 | /* |
| 1459 | * this is used by the defrag code to go through all the |
| 1460 | * leaves pointed to by a node and reallocate them so that |
| 1461 | * disk order is close to key order |
| 1462 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1463 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1464 | struct btrfs_root *root, struct extent_buffer *parent, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1465 | int start_slot, u64 *last_ret, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1466 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1467 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1468 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1469 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1470 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1471 | u64 search_start = *last_ret; |
| 1472 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1473 | u64 other; |
| 1474 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1475 | int end_slot; |
| 1476 | int i; |
| 1477 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1478 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1479 | int uptodate; |
| 1480 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1481 | int progress_passed = 0; |
| 1482 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1483 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1484 | parent_level = btrfs_header_level(parent); |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1485 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1486 | WARN_ON(trans->transaction != root->fs_info->running_transaction); |
| 1487 | WARN_ON(trans->transid != root->fs_info->generation); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1488 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1489 | parent_nritems = btrfs_header_nritems(parent); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1490 | blocksize = btrfs_level_size(root, parent_level - 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1491 | end_slot = parent_nritems; |
| 1492 | |
| 1493 | if (parent_nritems == 1) |
| 1494 | return 0; |
| 1495 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1496 | btrfs_set_lock_blocking(parent); |
| 1497 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1498 | for (i = start_slot; i < end_slot; i++) { |
| 1499 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1500 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1501 | btrfs_node_key(parent, &disk_key, i); |
| 1502 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1503 | continue; |
| 1504 | |
| 1505 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1506 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1507 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1508 | if (last_block == 0) |
| 1509 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1510 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1511 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1512 | other = btrfs_node_blockptr(parent, i - 1); |
| 1513 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1514 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1515 | if (!close && i < end_slot - 2) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1516 | other = btrfs_node_blockptr(parent, i + 1); |
| 1517 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1518 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1519 | if (close) { |
| 1520 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1521 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1522 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1523 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1524 | cur = btrfs_find_tree_block(root, blocknr, blocksize); |
| 1525 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1526 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1527 | else |
| 1528 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1529 | if (!cur || !uptodate) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1530 | if (!cur) { |
| 1531 | cur = read_tree_block(root, blocknr, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1532 | blocksize, gen); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1533 | if (!cur || !extent_buffer_uptodate(cur)) { |
| 1534 | free_extent_buffer(cur); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1535 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1536 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1537 | } else if (!uptodate) { |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 1538 | err = btrfs_read_buffer(cur, gen); |
| 1539 | if (err) { |
| 1540 | free_extent_buffer(cur); |
| 1541 | return err; |
| 1542 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1543 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1544 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1545 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1546 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1547 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1548 | btrfs_tree_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1549 | btrfs_set_lock_blocking(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1550 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1551 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1552 | min(16 * blocksize, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1553 | (end_slot - i) * blocksize)); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1554 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1555 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1556 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1557 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1558 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1559 | search_start = cur->start; |
| 1560 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1561 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1562 | btrfs_tree_unlock(cur); |
| 1563 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1564 | } |
| 1565 | return err; |
| 1566 | } |
| 1567 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1568 | /* |
| 1569 | * The leaf data grows from end-to-front in the node. |
| 1570 | * this returns the address of the start of the last item, |
| 1571 | * which is the stop of the leaf data stack |
| 1572 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1573 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1574 | struct extent_buffer *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1575 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1576 | u32 nr = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1577 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1578 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1579 | return btrfs_item_offset_nr(leaf, nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1580 | } |
| 1581 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1582 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1583 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1584 | * search for key in the extent_buffer. The items start at offset p, |
| 1585 | * and they are item_size apart. There are 'max' items in p. |
| 1586 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1587 | * the slot in the array is returned via slot, and it points to |
| 1588 | * the place where you would insert key if it is not found in |
| 1589 | * the array. |
| 1590 | * |
| 1591 | * slot may point to max if the key is bigger than all of the keys |
| 1592 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1593 | static noinline int generic_bin_search(struct extent_buffer *eb, |
| 1594 | unsigned long p, |
| 1595 | int item_size, struct btrfs_key *key, |
| 1596 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1597 | { |
| 1598 | int low = 0; |
| 1599 | int high = max; |
| 1600 | int mid; |
| 1601 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1602 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1603 | struct btrfs_disk_key unaligned; |
| 1604 | unsigned long offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1605 | char *kaddr = NULL; |
| 1606 | unsigned long map_start = 0; |
| 1607 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1608 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1609 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1610 | while (low < high) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1611 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1612 | offset = p + mid * item_size; |
| 1613 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1614 | if (!kaddr || offset < map_start || |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1615 | (offset + sizeof(struct btrfs_disk_key)) > |
| 1616 | map_start + map_len) { |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1617 | |
| 1618 | err = map_private_extent_buffer(eb, offset, |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1619 | sizeof(struct btrfs_disk_key), |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1620 | &kaddr, &map_start, &map_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1621 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1622 | if (!err) { |
| 1623 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1624 | map_start); |
| 1625 | } else { |
| 1626 | read_extent_buffer(eb, &unaligned, |
| 1627 | offset, sizeof(unaligned)); |
| 1628 | tmp = &unaligned; |
| 1629 | } |
| 1630 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1631 | } else { |
| 1632 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1633 | map_start); |
| 1634 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1635 | ret = comp_keys(tmp, key); |
| 1636 | |
| 1637 | if (ret < 0) |
| 1638 | low = mid + 1; |
| 1639 | else if (ret > 0) |
| 1640 | high = mid; |
| 1641 | else { |
| 1642 | *slot = mid; |
| 1643 | return 0; |
| 1644 | } |
| 1645 | } |
| 1646 | *slot = low; |
| 1647 | return 1; |
| 1648 | } |
| 1649 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1650 | /* |
| 1651 | * simple bin_search frontend that does the right thing for |
| 1652 | * leaves vs nodes |
| 1653 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1654 | static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1655 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1656 | { |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1657 | if (level == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1658 | return generic_bin_search(eb, |
| 1659 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1660 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1661 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1662 | slot); |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1663 | else |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1664 | return generic_bin_search(eb, |
| 1665 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1666 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1667 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1668 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1669 | } |
| 1670 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1671 | int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1672 | int level, int *slot) |
| 1673 | { |
| 1674 | return bin_search(eb, key, level, slot); |
| 1675 | } |
| 1676 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1677 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1678 | { |
| 1679 | spin_lock(&root->accounting_lock); |
| 1680 | btrfs_set_root_used(&root->root_item, |
| 1681 | btrfs_root_used(&root->root_item) + size); |
| 1682 | spin_unlock(&root->accounting_lock); |
| 1683 | } |
| 1684 | |
| 1685 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1686 | { |
| 1687 | spin_lock(&root->accounting_lock); |
| 1688 | btrfs_set_root_used(&root->root_item, |
| 1689 | btrfs_root_used(&root->root_item) - size); |
| 1690 | spin_unlock(&root->accounting_lock); |
| 1691 | } |
| 1692 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1693 | /* given a node and slot number, this reads the blocks it points to. The |
| 1694 | * extent buffer is returned with a reference taken (but unlocked). |
| 1695 | * NULL is returned on error. |
| 1696 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1697 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1698 | struct extent_buffer *parent, int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1699 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1700 | int level = btrfs_header_level(parent); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1701 | struct extent_buffer *eb; |
| 1702 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1703 | if (slot < 0) |
| 1704 | return NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1705 | if (slot >= btrfs_header_nritems(parent)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1706 | return NULL; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1707 | |
| 1708 | BUG_ON(level == 0); |
| 1709 | |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1710 | eb = read_tree_block(root, btrfs_node_blockptr(parent, slot), |
| 1711 | btrfs_level_size(root, level - 1), |
| 1712 | btrfs_node_ptr_generation(parent, slot)); |
| 1713 | if (eb && !extent_buffer_uptodate(eb)) { |
| 1714 | free_extent_buffer(eb); |
| 1715 | eb = NULL; |
| 1716 | } |
| 1717 | |
| 1718 | return eb; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1719 | } |
| 1720 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1721 | /* |
| 1722 | * node level balancing, used to make sure nodes are in proper order for |
| 1723 | * item deletion. We balance from the top down, so we have to make sure |
| 1724 | * that a deletion won't leave an node completely empty later on. |
| 1725 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1726 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1727 | struct btrfs_root *root, |
| 1728 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1729 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1730 | struct extent_buffer *right = NULL; |
| 1731 | struct extent_buffer *mid; |
| 1732 | struct extent_buffer *left = NULL; |
| 1733 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1734 | int ret = 0; |
| 1735 | int wret; |
| 1736 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1737 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1738 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1739 | |
| 1740 | if (level == 0) |
| 1741 | return 0; |
| 1742 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1743 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1744 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1745 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1746 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1747 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1748 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1749 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1750 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1751 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1752 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1753 | pslot = path->slots[level + 1]; |
| 1754 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1755 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1756 | /* |
| 1757 | * deal with the case where there is only one pointer in the root |
| 1758 | * by promoting the node below to a root |
| 1759 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1760 | if (!parent) { |
| 1761 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1762 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1763 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1764 | return 0; |
| 1765 | |
| 1766 | /* promote the child to a root */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1767 | child = read_node_slot(root, mid, 0); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1768 | if (!child) { |
| 1769 | ret = -EROFS; |
| 1770 | btrfs_std_error(root->fs_info, ret); |
| 1771 | goto enospc; |
| 1772 | } |
| 1773 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1774 | btrfs_tree_lock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1775 | btrfs_set_lock_blocking(child); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1776 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1777 | if (ret) { |
| 1778 | btrfs_tree_unlock(child); |
| 1779 | free_extent_buffer(child); |
| 1780 | goto enospc; |
| 1781 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1782 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1783 | tree_mod_log_set_root_pointer(root, child, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1784 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1785 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1786 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1787 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1788 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1789 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1790 | path->nodes[level] = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1791 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1792 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1793 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1794 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1795 | |
| 1796 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1797 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1798 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1799 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1800 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1801 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1802 | if (btrfs_header_nritems(mid) > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1803 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1804 | return 0; |
| 1805 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1806 | left = read_node_slot(root, parent, pslot - 1); |
| 1807 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1808 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1809 | btrfs_set_lock_blocking(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1810 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1811 | parent, pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1812 | if (wret) { |
| 1813 | ret = wret; |
| 1814 | goto enospc; |
| 1815 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1816 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1817 | right = read_node_slot(root, parent, pslot + 1); |
| 1818 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1819 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1820 | btrfs_set_lock_blocking(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1821 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1822 | parent, pslot + 1, &right); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1823 | if (wret) { |
| 1824 | ret = wret; |
| 1825 | goto enospc; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1830 | if (left) { |
| 1831 | orig_slot += btrfs_header_nritems(left); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1832 | wret = push_node_left(trans, root, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1833 | if (wret < 0) |
| 1834 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1835 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1836 | |
| 1837 | /* |
| 1838 | * then try to empty the right most buffer into the middle |
| 1839 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1840 | if (right) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1841 | wret = push_node_left(trans, root, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1842 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1843 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1844 | if (btrfs_header_nritems(right) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1845 | clean_tree_block(trans, root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1846 | btrfs_tree_unlock(right); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1847 | del_ptr(root, path, level + 1, pslot + 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1848 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1849 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1850 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1851 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1852 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1853 | struct btrfs_disk_key right_key; |
| 1854 | btrfs_node_key(right, &right_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1855 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1856 | pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1857 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1858 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1859 | } |
| 1860 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1861 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1862 | /* |
| 1863 | * we're not allowed to leave a node with one item in the |
| 1864 | * tree during a delete. A deletion from lower in the tree |
| 1865 | * could try to delete the only pointer in this node. |
| 1866 | * So, pull some keys from the left. |
| 1867 | * There has to be a left pointer at this point because |
| 1868 | * otherwise we would have pulled some pointers from the |
| 1869 | * right |
| 1870 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1871 | if (!left) { |
| 1872 | ret = -EROFS; |
| 1873 | btrfs_std_error(root->fs_info, ret); |
| 1874 | goto enospc; |
| 1875 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1876 | wret = balance_node_right(trans, root, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1877 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1878 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1879 | goto enospc; |
| 1880 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1881 | if (wret == 1) { |
| 1882 | wret = push_node_left(trans, root, left, mid, 1); |
| 1883 | if (wret < 0) |
| 1884 | ret = wret; |
| 1885 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1886 | BUG_ON(wret == 1); |
| 1887 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1888 | if (btrfs_header_nritems(mid) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1889 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1890 | btrfs_tree_unlock(mid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1891 | del_ptr(root, path, level + 1, pslot); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1892 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1893 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1894 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1895 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1896 | } else { |
| 1897 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1898 | struct btrfs_disk_key mid_key; |
| 1899 | btrfs_node_key(mid, &mid_key, 0); |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1900 | tree_mod_log_set_node_key(root->fs_info, parent, |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1901 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1902 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1903 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1904 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1905 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1906 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1907 | if (left) { |
| 1908 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1909 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1910 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1911 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1912 | path->slots[level + 1] -= 1; |
| 1913 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1914 | if (mid) { |
| 1915 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1916 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1917 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1918 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1919 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1920 | path->slots[level] = orig_slot; |
| 1921 | } |
| 1922 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1923 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1924 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1925 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1926 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1927 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1928 | if (right) { |
| 1929 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1930 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1931 | } |
| 1932 | if (left) { |
| 1933 | if (path->nodes[level] != left) |
| 1934 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1935 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1936 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1937 | return ret; |
| 1938 | } |
| 1939 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1940 | /* Node balancing for insertion. Here we only split or push nodes around |
| 1941 | * when they are completely full. This is also done top down, so we |
| 1942 | * have to be pessimistic. |
| 1943 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1944 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1945 | struct btrfs_root *root, |
| 1946 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1947 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1948 | struct extent_buffer *right = NULL; |
| 1949 | struct extent_buffer *mid; |
| 1950 | struct extent_buffer *left = NULL; |
| 1951 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1952 | int ret = 0; |
| 1953 | int wret; |
| 1954 | int pslot; |
| 1955 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1956 | |
| 1957 | if (level == 0) |
| 1958 | return 1; |
| 1959 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1960 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1961 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1962 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1963 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1964 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1965 | pslot = path->slots[level + 1]; |
| 1966 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1967 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1968 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1969 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1970 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1971 | left = read_node_slot(root, parent, pslot - 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1972 | |
| 1973 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1974 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1975 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1976 | |
| 1977 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1978 | btrfs_set_lock_blocking(left); |
| 1979 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1980 | left_nr = btrfs_header_nritems(left); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1981 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1982 | wret = 1; |
| 1983 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1984 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1985 | pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1986 | if (ret) |
| 1987 | wret = 1; |
| 1988 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1989 | wret = push_node_left(trans, root, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1990 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1991 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1992 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1993 | if (wret < 0) |
| 1994 | ret = wret; |
| 1995 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1996 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1997 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1998 | btrfs_node_key(mid, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1999 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2000 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2001 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 2002 | btrfs_mark_buffer_dirty(parent); |
| 2003 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2004 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2005 | path->slots[level + 1] -= 1; |
| 2006 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2007 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2008 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2009 | } else { |
| 2010 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2011 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2012 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2013 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2014 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2015 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2016 | return 0; |
| 2017 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2018 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2019 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2020 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2021 | right = read_node_slot(root, parent, pslot + 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2022 | |
| 2023 | /* |
| 2024 | * then try to empty the right most buffer into the middle |
| 2025 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2026 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2027 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2028 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2029 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2030 | btrfs_set_lock_blocking(right); |
| 2031 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2032 | right_nr = btrfs_header_nritems(right); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2033 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 2034 | wret = 1; |
| 2035 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2036 | ret = btrfs_cow_block(trans, root, right, |
| 2037 | parent, pslot + 1, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 2038 | &right); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2039 | if (ret) |
| 2040 | wret = 1; |
| 2041 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2042 | wret = balance_node_right(trans, root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2043 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2044 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2045 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2046 | if (wret < 0) |
| 2047 | ret = wret; |
| 2048 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2049 | struct btrfs_disk_key disk_key; |
| 2050 | |
| 2051 | btrfs_node_key(right, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2052 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2053 | pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2054 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2055 | btrfs_mark_buffer_dirty(parent); |
| 2056 | |
| 2057 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2058 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2059 | path->slots[level + 1] += 1; |
| 2060 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2061 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2062 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2063 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2064 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2065 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2066 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2067 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2068 | return 0; |
| 2069 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2070 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2071 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2072 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2073 | return 1; |
| 2074 | } |
| 2075 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2076 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2077 | * readahead one full node of leaves, finding things that are close |
| 2078 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2079 | */ |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2080 | static void reada_for_search(struct btrfs_root *root, |
| 2081 | struct btrfs_path *path, |
| 2082 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2083 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2084 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2085 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2086 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2087 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2088 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2089 | u64 nread = 0; |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2090 | u64 gen; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2091 | int direction = path->reada; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2092 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2093 | u32 nr; |
| 2094 | u32 blocksize; |
| 2095 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2096 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2097 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2098 | return; |
| 2099 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2100 | if (!path->nodes[level]) |
| 2101 | return; |
| 2102 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2103 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2104 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2105 | search = btrfs_node_blockptr(node, slot); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2106 | blocksize = btrfs_level_size(root, level - 1); |
| 2107 | eb = btrfs_find_tree_block(root, search, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2108 | if (eb) { |
| 2109 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2110 | return; |
| 2111 | } |
| 2112 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2113 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2114 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2115 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2116 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2117 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2118 | while (1) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2119 | if (direction < 0) { |
| 2120 | if (nr == 0) |
| 2121 | break; |
| 2122 | nr--; |
| 2123 | } else if (direction > 0) { |
| 2124 | nr++; |
| 2125 | if (nr >= nritems) |
| 2126 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2127 | } |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2128 | if (path->reada < 0 && objectid) { |
| 2129 | btrfs_node_key(node, &disk_key, nr); |
| 2130 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2131 | break; |
| 2132 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2133 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2134 | if ((search <= target && target - search <= 65536) || |
| 2135 | (search > target && search - target <= 65536)) { |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2136 | gen = btrfs_node_ptr_generation(node, nr); |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2137 | readahead_tree_block(root, search, blocksize, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2138 | nread += blocksize; |
| 2139 | } |
| 2140 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2141 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2142 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2143 | } |
| 2144 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2145 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2146 | static noinline void reada_for_balance(struct btrfs_root *root, |
| 2147 | struct btrfs_path *path, int level) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2148 | { |
| 2149 | int slot; |
| 2150 | int nritems; |
| 2151 | struct extent_buffer *parent; |
| 2152 | struct extent_buffer *eb; |
| 2153 | u64 gen; |
| 2154 | u64 block1 = 0; |
| 2155 | u64 block2 = 0; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2156 | int blocksize; |
| 2157 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2158 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2159 | if (!parent) |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2160 | return; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2161 | |
| 2162 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2163 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2164 | blocksize = btrfs_level_size(root, level); |
| 2165 | |
| 2166 | if (slot > 0) { |
| 2167 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2168 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
| 2169 | eb = btrfs_find_tree_block(root, block1, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2170 | /* |
| 2171 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2172 | * don't want to return eagain here. That will loop |
| 2173 | * forever |
| 2174 | */ |
| 2175 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2176 | block1 = 0; |
| 2177 | free_extent_buffer(eb); |
| 2178 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2179 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2180 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2181 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
| 2182 | eb = btrfs_find_tree_block(root, block2, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2183 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2184 | block2 = 0; |
| 2185 | free_extent_buffer(eb); |
| 2186 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2187 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2188 | if (block1) |
| 2189 | readahead_tree_block(root, block1, blocksize, 0); |
| 2190 | if (block2) |
| 2191 | readahead_tree_block(root, block2, blocksize, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | |
| 2195 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2196 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2197 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2198 | * operations on the tree might require changing key pointers higher up in the |
| 2199 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2200 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2201 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2202 | * the lock if the path points to the last slot in the block. This is part of |
| 2203 | * 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] | 2204 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2205 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2206 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2207 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2208 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2209 | int lowest_unlock, int min_write_lock_level, |
| 2210 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2211 | { |
| 2212 | int i; |
| 2213 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2214 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2215 | struct extent_buffer *t; |
| 2216 | |
| 2217 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2218 | if (!path->nodes[i]) |
| 2219 | break; |
| 2220 | if (!path->locks[i]) |
| 2221 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2222 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2223 | skip_level = i + 1; |
| 2224 | continue; |
| 2225 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2226 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2227 | u32 nritems; |
| 2228 | t = path->nodes[i]; |
| 2229 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2230 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2231 | skip_level = i + 1; |
| 2232 | continue; |
| 2233 | } |
| 2234 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2235 | if (skip_level < i && i >= lowest_unlock) |
| 2236 | no_skips = 1; |
| 2237 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2238 | t = path->nodes[i]; |
| 2239 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2240 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2241 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2242 | if (write_lock_level && |
| 2243 | i > min_write_lock_level && |
| 2244 | i <= *write_lock_level) { |
| 2245 | *write_lock_level = i - 1; |
| 2246 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2251 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2252 | * This releases any locks held in the path starting at level and |
| 2253 | * going all the way up to the root. |
| 2254 | * |
| 2255 | * btrfs_search_slot will keep the lock held on higher nodes in a few |
| 2256 | * corner cases, such as COW of the block at slot zero in the node. This |
| 2257 | * ignores those rules, and it should only be called when there are no |
| 2258 | * more updates to be done higher up in the tree. |
| 2259 | */ |
| 2260 | noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) |
| 2261 | { |
| 2262 | int i; |
| 2263 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2264 | if (path->keep_locks) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2265 | return; |
| 2266 | |
| 2267 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2268 | if (!path->nodes[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2269 | continue; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2270 | if (!path->locks[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2271 | continue; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2272 | btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2273 | path->locks[i] = 0; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2278 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2279 | * in cache without setting the path to blocking. If we find the block |
| 2280 | * we return zero and the path is unchanged. |
| 2281 | * |
| 2282 | * If we can't find the block, we set the path blocking and do some |
| 2283 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2284 | */ |
| 2285 | static int |
| 2286 | read_block_for_search(struct btrfs_trans_handle *trans, |
| 2287 | struct btrfs_root *root, struct btrfs_path *p, |
| 2288 | struct extent_buffer **eb_ret, int level, int slot, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2289 | struct btrfs_key *key, u64 time_seq) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2290 | { |
| 2291 | u64 blocknr; |
| 2292 | u64 gen; |
| 2293 | u32 blocksize; |
| 2294 | struct extent_buffer *b = *eb_ret; |
| 2295 | struct extent_buffer *tmp; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2296 | int ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2297 | |
| 2298 | blocknr = btrfs_node_blockptr(b, slot); |
| 2299 | gen = btrfs_node_ptr_generation(b, slot); |
| 2300 | blocksize = btrfs_level_size(root, level - 1); |
| 2301 | |
| 2302 | tmp = btrfs_find_tree_block(root, blocknr, blocksize); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2303 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2304 | /* first we do an atomic uptodate check */ |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2305 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
| 2306 | *eb_ret = tmp; |
| 2307 | return 0; |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2308 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2309 | |
| 2310 | /* the pages were up to date, but we failed |
| 2311 | * the generation number check. Do a full |
| 2312 | * read for the generation number that is correct. |
| 2313 | * We must do this without dropping locks so |
| 2314 | * we can trust our generation number |
| 2315 | */ |
| 2316 | btrfs_set_path_blocking(p); |
| 2317 | |
| 2318 | /* now we're allowed to do a blocking uptodate check */ |
| 2319 | ret = btrfs_read_buffer(tmp, gen); |
| 2320 | if (!ret) { |
| 2321 | *eb_ret = tmp; |
| 2322 | return 0; |
| 2323 | } |
| 2324 | free_extent_buffer(tmp); |
| 2325 | btrfs_release_path(p); |
| 2326 | return -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | /* |
| 2330 | * reduce lock contention at high levels |
| 2331 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2332 | * we read. Don't release the lock on the current |
| 2333 | * level because we need to walk this node to figure |
| 2334 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2335 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2336 | btrfs_unlock_up_safe(p, level + 1); |
| 2337 | btrfs_set_path_blocking(p); |
| 2338 | |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2339 | free_extent_buffer(tmp); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2340 | if (p->reada) |
| 2341 | reada_for_search(root, p, level, slot, key->objectid); |
| 2342 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2343 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2344 | |
| 2345 | ret = -EAGAIN; |
Yan, Zheng | 5bdd353 | 2010-05-26 11:20:30 -0400 | [diff] [blame] | 2346 | tmp = read_tree_block(root, blocknr, blocksize, 0); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2347 | if (tmp) { |
| 2348 | /* |
| 2349 | * If the read above didn't mark this buffer up to date, |
| 2350 | * it will never end up being up to date. Set ret to EIO now |
| 2351 | * and give up so that our caller doesn't loop forever |
| 2352 | * on our EAGAINs. |
| 2353 | */ |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2354 | if (!btrfs_buffer_uptodate(tmp, 0, 0)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2355 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2356 | free_extent_buffer(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2357 | } |
| 2358 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | /* |
| 2362 | * helper function for btrfs_search_slot. This does all of the checks |
| 2363 | * for node-level blocks and does any balancing required based on |
| 2364 | * the ins_len. |
| 2365 | * |
| 2366 | * If no extra work was required, zero is returned. If we had to |
| 2367 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2368 | * start over |
| 2369 | */ |
| 2370 | static int |
| 2371 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2372 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2373 | struct extent_buffer *b, int level, int ins_len, |
| 2374 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2375 | { |
| 2376 | int ret; |
| 2377 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
| 2378 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { |
| 2379 | int sret; |
| 2380 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2381 | if (*write_lock_level < level + 1) { |
| 2382 | *write_lock_level = level + 1; |
| 2383 | btrfs_release_path(p); |
| 2384 | goto again; |
| 2385 | } |
| 2386 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2387 | btrfs_set_path_blocking(p); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2388 | reada_for_balance(root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2389 | sret = split_node(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2390 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2391 | |
| 2392 | BUG_ON(sret > 0); |
| 2393 | if (sret) { |
| 2394 | ret = sret; |
| 2395 | goto done; |
| 2396 | } |
| 2397 | b = p->nodes[level]; |
| 2398 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Chris Mason | cfbb930 | 2009-05-18 10:41:58 -0400 | [diff] [blame] | 2399 | BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2400 | int sret; |
| 2401 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2402 | if (*write_lock_level < level + 1) { |
| 2403 | *write_lock_level = level + 1; |
| 2404 | btrfs_release_path(p); |
| 2405 | goto again; |
| 2406 | } |
| 2407 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2408 | btrfs_set_path_blocking(p); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2409 | reada_for_balance(root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2410 | sret = balance_level(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2411 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2412 | |
| 2413 | if (sret) { |
| 2414 | ret = sret; |
| 2415 | goto done; |
| 2416 | } |
| 2417 | b = p->nodes[level]; |
| 2418 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2419 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2420 | goto again; |
| 2421 | } |
| 2422 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2423 | } |
| 2424 | return 0; |
| 2425 | |
| 2426 | again: |
| 2427 | ret = -EAGAIN; |
| 2428 | done: |
| 2429 | return ret; |
| 2430 | } |
| 2431 | |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2432 | static void key_search_validate(struct extent_buffer *b, |
| 2433 | struct btrfs_key *key, |
| 2434 | int level) |
| 2435 | { |
| 2436 | #ifdef CONFIG_BTRFS_ASSERT |
| 2437 | struct btrfs_disk_key disk_key; |
| 2438 | |
| 2439 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 2440 | |
| 2441 | if (level == 0) |
| 2442 | ASSERT(!memcmp_extent_buffer(b, &disk_key, |
| 2443 | offsetof(struct btrfs_leaf, items[0].key), |
| 2444 | sizeof(disk_key))); |
| 2445 | else |
| 2446 | ASSERT(!memcmp_extent_buffer(b, &disk_key, |
| 2447 | offsetof(struct btrfs_node, ptrs[0].key), |
| 2448 | sizeof(disk_key))); |
| 2449 | #endif |
| 2450 | } |
| 2451 | |
| 2452 | static int key_search(struct extent_buffer *b, struct btrfs_key *key, |
| 2453 | int level, int *prev_cmp, int *slot) |
| 2454 | { |
| 2455 | if (*prev_cmp != 0) { |
| 2456 | *prev_cmp = bin_search(b, key, level, slot); |
| 2457 | return *prev_cmp; |
| 2458 | } |
| 2459 | |
| 2460 | key_search_validate(b, key, level); |
| 2461 | *slot = 0; |
| 2462 | |
| 2463 | return 0; |
| 2464 | } |
| 2465 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2466 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2467 | * look for key in the tree. path is filled in with nodes along the way |
| 2468 | * if key is found, we return zero and you can find the item in the leaf |
| 2469 | * level of the path (level 0) |
| 2470 | * |
| 2471 | * 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] | 2472 | * be inserted, and 1 is returned. If there are other errors during the |
| 2473 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2474 | * |
| 2475 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 2476 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 2477 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2478 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2479 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 2480 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 2481 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2482 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2483 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2484 | int slot; |
| 2485 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2486 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2487 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2488 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2489 | int root_lock; |
| 2490 | /* everything at write_lock_level or lower must be write locked */ |
| 2491 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2492 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2493 | int min_write_lock_level; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2494 | int prev_cmp; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2495 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2496 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2497 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2498 | WARN_ON(p->nodes[0] != NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2499 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2500 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2501 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2502 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2503 | /* when we are removing items, we might have to go up to level |
| 2504 | * two as we update tree pointers Make sure we keep write |
| 2505 | * for those levels as well |
| 2506 | */ |
| 2507 | write_lock_level = 2; |
| 2508 | } else if (ins_len > 0) { |
| 2509 | /* |
| 2510 | * for inserting items, make sure we have a write lock on |
| 2511 | * level 1 so we can update keys |
| 2512 | */ |
| 2513 | write_lock_level = 1; |
| 2514 | } |
| 2515 | |
| 2516 | if (!cow) |
| 2517 | write_lock_level = -1; |
| 2518 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2519 | if (cow && (p->keep_locks || p->lowest_level)) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2520 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2521 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2522 | min_write_lock_level = write_lock_level; |
| 2523 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2524 | again: |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2525 | prev_cmp = -1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2526 | /* |
| 2527 | * we try very hard to do read locks on the root |
| 2528 | */ |
| 2529 | root_lock = BTRFS_READ_LOCK; |
| 2530 | level = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2531 | if (p->search_commit_root) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2532 | /* |
| 2533 | * the commit roots are read only |
| 2534 | * so we always do read locks |
| 2535 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2536 | b = root->commit_root; |
| 2537 | extent_buffer_get(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2538 | level = btrfs_header_level(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2539 | if (!p->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2540 | btrfs_tree_read_lock(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2541 | } else { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2542 | if (p->skip_locking) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2543 | b = btrfs_root_node(root); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2544 | level = btrfs_header_level(b); |
| 2545 | } else { |
| 2546 | /* we don't know the level of the root node |
| 2547 | * until we actually have it read locked |
| 2548 | */ |
| 2549 | b = btrfs_read_lock_root_node(root); |
| 2550 | level = btrfs_header_level(b); |
| 2551 | if (level <= write_lock_level) { |
| 2552 | /* whoops, must trade for write lock */ |
| 2553 | btrfs_tree_read_unlock(b); |
| 2554 | free_extent_buffer(b); |
| 2555 | b = btrfs_lock_root_node(root); |
| 2556 | root_lock = BTRFS_WRITE_LOCK; |
| 2557 | |
| 2558 | /* the level might have changed, check again */ |
| 2559 | level = btrfs_header_level(b); |
| 2560 | } |
| 2561 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2562 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2563 | p->nodes[level] = b; |
| 2564 | if (!p->skip_locking) |
| 2565 | p->locks[level] = root_lock; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2566 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2567 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2568 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2569 | |
| 2570 | /* |
| 2571 | * setup the path here so we can release it under lock |
| 2572 | * contention with the cow code |
| 2573 | */ |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2574 | if (cow) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2575 | /* |
| 2576 | * if we don't really need to cow this block |
| 2577 | * then we don't want to set the path blocking, |
| 2578 | * so we test it here |
| 2579 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2580 | if (!should_cow_block(trans, root, b)) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2581 | goto cow_done; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2582 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2583 | btrfs_set_path_blocking(p); |
| 2584 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2585 | /* |
| 2586 | * must have write locks on this node and the |
| 2587 | * parent |
| 2588 | */ |
Josef Bacik | 5124e00 | 2012-11-07 13:44:13 -0500 | [diff] [blame] | 2589 | if (level > write_lock_level || |
| 2590 | (level + 1 > write_lock_level && |
| 2591 | level + 1 < BTRFS_MAX_LEVEL && |
| 2592 | p->nodes[level + 1])) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2593 | write_lock_level = level + 1; |
| 2594 | btrfs_release_path(p); |
| 2595 | goto again; |
| 2596 | } |
| 2597 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2598 | err = btrfs_cow_block(trans, root, b, |
| 2599 | p->nodes[level + 1], |
| 2600 | p->slots[level + 1], &b); |
| 2601 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2602 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2603 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2604 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2605 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2606 | cow_done: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2607 | BUG_ON(!cow && ins_len); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2608 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2609 | p->nodes[level] = b; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2610 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2611 | |
| 2612 | /* |
| 2613 | * we have a lock on b and as long as we aren't changing |
| 2614 | * the tree, there is no way to for the items in b to change. |
| 2615 | * It is safe to drop the lock on our parent before we |
| 2616 | * go through the expensive btree search on b. |
| 2617 | * |
| 2618 | * If cow is true, then we might be changing slot zero, |
| 2619 | * which may require changing the parent. So, we can't |
| 2620 | * drop the lock until after we know which slot we're |
| 2621 | * operating on. |
| 2622 | */ |
| 2623 | if (!cow) |
| 2624 | btrfs_unlock_up_safe(p, level + 1); |
| 2625 | |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2626 | ret = key_search(b, key, level, &prev_cmp, &slot); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2627 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2628 | if (level != 0) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2629 | int dec = 0; |
| 2630 | if (ret && slot > 0) { |
| 2631 | dec = 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2632 | slot -= 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2633 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2634 | p->slots[level] = slot; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2635 | err = setup_nodes_for_search(trans, root, p, b, level, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2636 | ins_len, &write_lock_level); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2637 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2638 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2639 | if (err) { |
| 2640 | ret = err; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2641 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2642 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2643 | b = p->nodes[level]; |
| 2644 | slot = p->slots[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2645 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2646 | /* |
| 2647 | * slot 0 is special, if we change the key |
| 2648 | * we have to update the parent pointer |
| 2649 | * which means we must have a write lock |
| 2650 | * on the parent |
| 2651 | */ |
| 2652 | if (slot == 0 && cow && |
| 2653 | write_lock_level < level + 1) { |
| 2654 | write_lock_level = level + 1; |
| 2655 | btrfs_release_path(p); |
| 2656 | goto again; |
| 2657 | } |
| 2658 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2659 | unlock_up(p, level, lowest_unlock, |
| 2660 | min_write_lock_level, &write_lock_level); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 2661 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2662 | if (level == lowest_level) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2663 | if (dec) |
| 2664 | p->slots[level]++; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2665 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2666 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2667 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2668 | err = read_block_for_search(trans, root, p, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2669 | &b, level, slot, key, 0); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2670 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2671 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2672 | if (err) { |
| 2673 | ret = err; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2674 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2675 | } |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2676 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2677 | if (!p->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2678 | level = btrfs_header_level(b); |
| 2679 | if (level <= write_lock_level) { |
| 2680 | err = btrfs_try_tree_write_lock(b); |
| 2681 | if (!err) { |
| 2682 | btrfs_set_path_blocking(p); |
| 2683 | btrfs_tree_lock(b); |
| 2684 | btrfs_clear_path_blocking(p, b, |
| 2685 | BTRFS_WRITE_LOCK); |
| 2686 | } |
| 2687 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2688 | } else { |
| 2689 | err = btrfs_try_tree_read_lock(b); |
| 2690 | if (!err) { |
| 2691 | btrfs_set_path_blocking(p); |
| 2692 | btrfs_tree_read_lock(b); |
| 2693 | btrfs_clear_path_blocking(p, b, |
| 2694 | BTRFS_READ_LOCK); |
| 2695 | } |
| 2696 | p->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2697 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2698 | p->nodes[level] = b; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2699 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2700 | } else { |
| 2701 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2702 | if (ins_len > 0 && |
| 2703 | btrfs_leaf_free_space(root, b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2704 | if (write_lock_level < 1) { |
| 2705 | write_lock_level = 1; |
| 2706 | btrfs_release_path(p); |
| 2707 | goto again; |
| 2708 | } |
| 2709 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2710 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2711 | err = split_leaf(trans, root, key, |
| 2712 | p, ins_len, ret == 0); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2713 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2714 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2715 | BUG_ON(err > 0); |
| 2716 | if (err) { |
| 2717 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2718 | goto done; |
| 2719 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2720 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2721 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2722 | unlock_up(p, level, lowest_unlock, |
| 2723 | min_write_lock_level, &write_lock_level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2724 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2725 | } |
| 2726 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2727 | ret = 1; |
| 2728 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2729 | /* |
| 2730 | * we don't really know what they plan on doing with the path |
| 2731 | * from here on, so for now just mark it as blocking |
| 2732 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2733 | if (!p->leave_spinning) |
| 2734 | btrfs_set_path_blocking(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2735 | if (ret < 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2736 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2737 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2738 | } |
| 2739 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2740 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2741 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2742 | * current state of the tree together with the operations recorded in the tree |
| 2743 | * modification log to search for the key in a previous version of this tree, as |
| 2744 | * denoted by the time_seq parameter. |
| 2745 | * |
| 2746 | * Naturally, there is no support for insert, delete or cow operations. |
| 2747 | * |
| 2748 | * The resulting path and return value will be set up as if we called |
| 2749 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2750 | */ |
| 2751 | int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 2752 | struct btrfs_path *p, u64 time_seq) |
| 2753 | { |
| 2754 | struct extent_buffer *b; |
| 2755 | int slot; |
| 2756 | int ret; |
| 2757 | int err; |
| 2758 | int level; |
| 2759 | int lowest_unlock = 1; |
| 2760 | u8 lowest_level = 0; |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame^] | 2761 | int prev_cmp = -1; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2762 | |
| 2763 | lowest_level = p->lowest_level; |
| 2764 | WARN_ON(p->nodes[0] != NULL); |
| 2765 | |
| 2766 | if (p->search_commit_root) { |
| 2767 | BUG_ON(time_seq); |
| 2768 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2769 | } |
| 2770 | |
| 2771 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2772 | b = get_old_root(root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2773 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2774 | p->locks[level] = BTRFS_READ_LOCK; |
| 2775 | |
| 2776 | while (b) { |
| 2777 | level = btrfs_header_level(b); |
| 2778 | p->nodes[level] = b; |
| 2779 | btrfs_clear_path_blocking(p, NULL, 0); |
| 2780 | |
| 2781 | /* |
| 2782 | * we have a lock on b and as long as we aren't changing |
| 2783 | * the tree, there is no way to for the items in b to change. |
| 2784 | * It is safe to drop the lock on our parent before we |
| 2785 | * go through the expensive btree search on b. |
| 2786 | */ |
| 2787 | btrfs_unlock_up_safe(p, level + 1); |
| 2788 | |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame^] | 2789 | /* |
| 2790 | * Since we can unwind eb's we want to do a real search every |
| 2791 | * time. |
| 2792 | */ |
| 2793 | prev_cmp = -1; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2794 | ret = key_search(b, key, level, &prev_cmp, &slot); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2795 | |
| 2796 | if (level != 0) { |
| 2797 | int dec = 0; |
| 2798 | if (ret && slot > 0) { |
| 2799 | dec = 1; |
| 2800 | slot -= 1; |
| 2801 | } |
| 2802 | p->slots[level] = slot; |
| 2803 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2804 | |
| 2805 | if (level == lowest_level) { |
| 2806 | if (dec) |
| 2807 | p->slots[level]++; |
| 2808 | goto done; |
| 2809 | } |
| 2810 | |
| 2811 | err = read_block_for_search(NULL, root, p, &b, level, |
| 2812 | slot, key, time_seq); |
| 2813 | if (err == -EAGAIN) |
| 2814 | goto again; |
| 2815 | if (err) { |
| 2816 | ret = err; |
| 2817 | goto done; |
| 2818 | } |
| 2819 | |
| 2820 | level = btrfs_header_level(b); |
| 2821 | err = btrfs_try_tree_read_lock(b); |
| 2822 | if (!err) { |
| 2823 | btrfs_set_path_blocking(p); |
| 2824 | btrfs_tree_read_lock(b); |
| 2825 | btrfs_clear_path_blocking(p, b, |
| 2826 | BTRFS_READ_LOCK); |
| 2827 | } |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 2828 | b = tree_mod_log_rewind(root->fs_info, p, b, time_seq); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 2829 | if (!b) { |
| 2830 | ret = -ENOMEM; |
| 2831 | goto done; |
| 2832 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2833 | p->locks[level] = BTRFS_READ_LOCK; |
| 2834 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2835 | } else { |
| 2836 | p->slots[level] = slot; |
| 2837 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2838 | goto done; |
| 2839 | } |
| 2840 | } |
| 2841 | ret = 1; |
| 2842 | done: |
| 2843 | if (!p->leave_spinning) |
| 2844 | btrfs_set_path_blocking(p); |
| 2845 | if (ret < 0) |
| 2846 | btrfs_release_path(p); |
| 2847 | |
| 2848 | return ret; |
| 2849 | } |
| 2850 | |
| 2851 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2852 | * helper to use instead of search slot if no exact match is needed but |
| 2853 | * instead the next or previous item should be returned. |
| 2854 | * When find_higher is true, the next higher item is returned, the next lower |
| 2855 | * otherwise. |
| 2856 | * When return_any and find_higher are both true, and no higher item is found, |
| 2857 | * return the next lower instead. |
| 2858 | * When return_any is true and find_higher is false, and no lower item is found, |
| 2859 | * return the next higher instead. |
| 2860 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 2861 | * < 0 on error |
| 2862 | */ |
| 2863 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
| 2864 | struct btrfs_key *key, struct btrfs_path *p, |
| 2865 | int find_higher, int return_any) |
| 2866 | { |
| 2867 | int ret; |
| 2868 | struct extent_buffer *leaf; |
| 2869 | |
| 2870 | again: |
| 2871 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2872 | if (ret <= 0) |
| 2873 | return ret; |
| 2874 | /* |
| 2875 | * a return value of 1 means the path is at the position where the |
| 2876 | * item should be inserted. Normally this is the next bigger item, |
| 2877 | * but in case the previous item is the last in a leaf, path points |
| 2878 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 2879 | * item. |
| 2880 | */ |
| 2881 | leaf = p->nodes[0]; |
| 2882 | |
| 2883 | if (find_higher) { |
| 2884 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2885 | ret = btrfs_next_leaf(root, p); |
| 2886 | if (ret <= 0) |
| 2887 | return ret; |
| 2888 | if (!return_any) |
| 2889 | return 1; |
| 2890 | /* |
| 2891 | * no higher item found, return the next |
| 2892 | * lower instead |
| 2893 | */ |
| 2894 | return_any = 0; |
| 2895 | find_higher = 0; |
| 2896 | btrfs_release_path(p); |
| 2897 | goto again; |
| 2898 | } |
| 2899 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2900 | if (p->slots[0] == 0) { |
| 2901 | ret = btrfs_prev_leaf(root, p); |
| 2902 | if (ret < 0) |
| 2903 | return ret; |
| 2904 | if (!ret) { |
| 2905 | p->slots[0] = btrfs_header_nritems(leaf) - 1; |
| 2906 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2907 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2908 | if (!return_any) |
| 2909 | return 1; |
| 2910 | /* |
| 2911 | * no lower item found, return the next |
| 2912 | * higher instead |
| 2913 | */ |
| 2914 | return_any = 0; |
| 2915 | find_higher = 1; |
| 2916 | btrfs_release_path(p); |
| 2917 | goto again; |
| 2918 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2919 | --p->slots[0]; |
| 2920 | } |
| 2921 | } |
| 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2926 | * adjust the pointers going up the tree, starting at level |
| 2927 | * making sure the right key of each node is points to 'key'. |
| 2928 | * This is used after shifting pointers to the left, so it stops |
| 2929 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 2930 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2931 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2932 | */ |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 2933 | 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] | 2934 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2935 | { |
| 2936 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2937 | struct extent_buffer *t; |
| 2938 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2939 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2940 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2941 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2942 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2943 | t = path->nodes[i]; |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2944 | tree_mod_log_set_node_key(root->fs_info, t, tslot, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2945 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2946 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2947 | if (tslot != 0) |
| 2948 | break; |
| 2949 | } |
| 2950 | } |
| 2951 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2952 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2953 | * update item key. |
| 2954 | * |
| 2955 | * This function isn't completely safe. It's the caller's responsibility |
| 2956 | * that the new key won't break the order |
| 2957 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 2958 | 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] | 2959 | struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2960 | { |
| 2961 | struct btrfs_disk_key disk_key; |
| 2962 | struct extent_buffer *eb; |
| 2963 | int slot; |
| 2964 | |
| 2965 | eb = path->nodes[0]; |
| 2966 | slot = path->slots[0]; |
| 2967 | if (slot > 0) { |
| 2968 | btrfs_item_key(eb, &disk_key, slot - 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2969 | BUG_ON(comp_keys(&disk_key, new_key) >= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2970 | } |
| 2971 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 2972 | btrfs_item_key(eb, &disk_key, slot + 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2973 | BUG_ON(comp_keys(&disk_key, new_key) <= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 2977 | btrfs_set_item_key(eb, &disk_key, slot); |
| 2978 | btrfs_mark_buffer_dirty(eb); |
| 2979 | if (slot == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 2980 | fixup_low_keys(root, path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2984 | * 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] | 2985 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2986 | * |
| 2987 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 2988 | * 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] | 2989 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2990 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 2991 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2992 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2993 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2994 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2995 | int src_nritems; |
| 2996 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2997 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2998 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2999 | src_nritems = btrfs_header_nritems(src); |
| 3000 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3001 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3002 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3003 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3004 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3005 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3006 | return 1; |
| 3007 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3008 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3009 | return 1; |
| 3010 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3011 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3012 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3013 | if (push_items < src_nritems) { |
| 3014 | /* leave at least 8 pointers in the node if |
| 3015 | * we aren't going to empty it |
| 3016 | */ |
| 3017 | if (src_nritems - push_items < 8) { |
| 3018 | if (push_items <= 8) |
| 3019 | return 1; |
| 3020 | push_items -= 8; |
| 3021 | } |
| 3022 | } |
| 3023 | } else |
| 3024 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3025 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3026 | 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] | 3027 | push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3028 | copy_extent_buffer(dst, src, |
| 3029 | btrfs_node_key_ptr_offset(dst_nritems), |
| 3030 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3031 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3032 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3033 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3034 | /* |
| 3035 | * don't call tree_mod_log_eb_move here, key removal was already |
| 3036 | * fully logged by tree_mod_log_eb_copy above. |
| 3037 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3038 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 3039 | btrfs_node_key_ptr_offset(push_items), |
| 3040 | (src_nritems - push_items) * |
| 3041 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3042 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3043 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3044 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 3045 | btrfs_mark_buffer_dirty(src); |
| 3046 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3047 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3048 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3049 | } |
| 3050 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3051 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3052 | * try to push data from one node into the next node right in the |
| 3053 | * tree. |
| 3054 | * |
| 3055 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 3056 | * error, and > 0 if there was no room in the right hand block. |
| 3057 | * |
| 3058 | * this will only push up to 1/2 the contents of the left node over |
| 3059 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3060 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 3061 | struct btrfs_root *root, |
| 3062 | struct extent_buffer *dst, |
| 3063 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3064 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3065 | int push_items = 0; |
| 3066 | int max_push; |
| 3067 | int src_nritems; |
| 3068 | int dst_nritems; |
| 3069 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3070 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3071 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3072 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3073 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3074 | src_nritems = btrfs_header_nritems(src); |
| 3075 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3076 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3077 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3078 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3079 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3080 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3081 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3082 | |
| 3083 | max_push = src_nritems / 2 + 1; |
| 3084 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3085 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3086 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3087 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3088 | if (max_push < push_items) |
| 3089 | push_items = max_push; |
| 3090 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3091 | 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] | 3092 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3093 | btrfs_node_key_ptr_offset(0), |
| 3094 | (dst_nritems) * |
| 3095 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3096 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3097 | tree_mod_log_eb_copy(root->fs_info, dst, src, 0, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3098 | src_nritems - push_items, push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3099 | copy_extent_buffer(dst, src, |
| 3100 | btrfs_node_key_ptr_offset(0), |
| 3101 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3102 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3103 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3104 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3105 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3106 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3107 | btrfs_mark_buffer_dirty(src); |
| 3108 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3109 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3110 | return ret; |
| 3111 | } |
| 3112 | |
| 3113 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3114 | * helper function to insert a new root level in the tree. |
| 3115 | * A new node is allocated, and a single item is inserted to |
| 3116 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3117 | * |
| 3118 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3119 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3120 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3121 | struct btrfs_root *root, |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3122 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3123 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3124 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3125 | struct extent_buffer *lower; |
| 3126 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3127 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3128 | struct btrfs_disk_key lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3129 | |
| 3130 | BUG_ON(path->nodes[level]); |
| 3131 | BUG_ON(path->nodes[level-1] != root->node); |
| 3132 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3133 | lower = path->nodes[level-1]; |
| 3134 | if (level == 1) |
| 3135 | btrfs_item_key(lower, &lower_key, 0); |
| 3136 | else |
| 3137 | btrfs_node_key(lower, &lower_key, 0); |
| 3138 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3139 | c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3140 | root->root_key.objectid, &lower_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3141 | level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3142 | if (IS_ERR(c)) |
| 3143 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3144 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3145 | root_add_used(root, root->nodesize); |
| 3146 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3147 | memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3148 | btrfs_set_header_nritems(c, 1); |
| 3149 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3150 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3151 | btrfs_set_header_generation(c, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3152 | btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3153 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3154 | |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame] | 3155 | write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(c), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3156 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3157 | |
| 3158 | write_extent_buffer(c, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | b308bc2 | 2013-08-20 13:20:15 +0200 | [diff] [blame] | 3159 | btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3160 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3161 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3162 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3163 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3164 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3165 | |
| 3166 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3167 | |
| 3168 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3169 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3170 | old = root->node; |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3171 | tree_mod_log_set_root_pointer(root, c, 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3172 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3173 | |
| 3174 | /* the super has an extra ref to root->node */ |
| 3175 | free_extent_buffer(old); |
| 3176 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3177 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3178 | extent_buffer_get(c); |
| 3179 | path->nodes[level] = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 3180 | path->locks[level] = BTRFS_WRITE_LOCK; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3181 | path->slots[level] = 0; |
| 3182 | return 0; |
| 3183 | } |
| 3184 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3185 | /* |
| 3186 | * worker function to insert a single pointer in a node. |
| 3187 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3188 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3189 | * slot and level indicate where you want the key to go, and |
| 3190 | * blocknr is the block the key points to. |
| 3191 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3192 | static void insert_ptr(struct btrfs_trans_handle *trans, |
| 3193 | struct btrfs_root *root, struct btrfs_path *path, |
| 3194 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3195 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3196 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3197 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3198 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3199 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3200 | |
| 3201 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3202 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3203 | lower = path->nodes[level]; |
| 3204 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3205 | BUG_ON(slot > nritems); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3206 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3207 | if (slot != nritems) { |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3208 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3209 | tree_mod_log_eb_move(root->fs_info, lower, slot + 1, |
| 3210 | slot, nritems - slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3211 | memmove_extent_buffer(lower, |
| 3212 | btrfs_node_key_ptr_offset(slot + 1), |
| 3213 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3214 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3215 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3216 | if (level) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3217 | ret = tree_mod_log_insert_key(root->fs_info, lower, slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 3218 | MOD_LOG_KEY_ADD, GFP_NOFS); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3219 | BUG_ON(ret < 0); |
| 3220 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3221 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3222 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3223 | WARN_ON(trans->transid == 0); |
| 3224 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3225 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3226 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3227 | } |
| 3228 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3229 | /* |
| 3230 | * split the node at the specified level in path in two. |
| 3231 | * The path is corrected to point to the appropriate node after the split |
| 3232 | * |
| 3233 | * Before splitting this tries to make some room in the node by pushing |
| 3234 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3235 | * |
| 3236 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3237 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3238 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3239 | struct btrfs_root *root, |
| 3240 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3241 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3242 | struct extent_buffer *c; |
| 3243 | struct extent_buffer *split; |
| 3244 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3245 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3246 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3247 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3248 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3249 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3250 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3251 | if (c == root->node) { |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3252 | /* |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3253 | * trying to split the root, lets make a new one |
| 3254 | * |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3255 | * tree mod log: We don't log_removal old root in |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3256 | * insert_new_root, because that root buffer will be kept as a |
| 3257 | * normal node. We are going to log removal of half of the |
| 3258 | * elements below with tree_mod_log_eb_copy. We're holding a |
| 3259 | * tree lock on the buffer, which is why we cannot race with |
| 3260 | * other tree_mod_log users. |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3261 | */ |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3262 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3263 | if (ret) |
| 3264 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3265 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3266 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3267 | c = path->nodes[level]; |
| 3268 | if (!ret && btrfs_header_nritems(c) < |
Chris Mason | c448acf | 2008-04-24 09:34:34 -0400 | [diff] [blame] | 3269 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3270 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3271 | if (ret < 0) |
| 3272 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3273 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3274 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3275 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3276 | mid = (c_nritems + 1) / 2; |
| 3277 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3278 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3279 | split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3280 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3281 | &disk_key, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3282 | if (IS_ERR(split)) |
| 3283 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3284 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3285 | root_add_used(root, root->nodesize); |
| 3286 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3287 | memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3288 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3289 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3290 | btrfs_set_header_generation(split, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3291 | btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3292 | btrfs_set_header_owner(split, root->root_key.objectid); |
| 3293 | write_extent_buffer(split, root->fs_info->fsid, |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame] | 3294 | btrfs_header_fsid(split), BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3295 | write_extent_buffer(split, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | b308bc2 | 2013-08-20 13:20:15 +0200 | [diff] [blame] | 3296 | btrfs_header_chunk_tree_uuid(split), |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3297 | BTRFS_UUID_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3298 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3299 | 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] | 3300 | copy_extent_buffer(split, c, |
| 3301 | btrfs_node_key_ptr_offset(0), |
| 3302 | btrfs_node_key_ptr_offset(mid), |
| 3303 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3304 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3305 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3306 | ret = 0; |
| 3307 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3308 | btrfs_mark_buffer_dirty(c); |
| 3309 | btrfs_mark_buffer_dirty(split); |
| 3310 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3311 | insert_ptr(trans, root, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3312 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3313 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3314 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3315 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3316 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3317 | free_extent_buffer(c); |
| 3318 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3319 | path->slots[level + 1] += 1; |
| 3320 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3321 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3322 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3323 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3324 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3325 | } |
| 3326 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3327 | /* |
| 3328 | * how many bytes are required to store the items in a leaf. start |
| 3329 | * and nr indicate which items in the leaf to check. This totals up the |
| 3330 | * space used both by the item structs and the item data |
| 3331 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3332 | 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] | 3333 | { |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3334 | struct btrfs_item *start_item; |
| 3335 | struct btrfs_item *end_item; |
| 3336 | struct btrfs_map_token token; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3337 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3338 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3339 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3340 | |
| 3341 | if (!nr) |
| 3342 | return 0; |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3343 | btrfs_init_map_token(&token); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3344 | start_item = btrfs_item_nr(start); |
| 3345 | end_item = btrfs_item_nr(end); |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3346 | data_len = btrfs_token_item_offset(l, start_item, &token) + |
| 3347 | btrfs_token_item_size(l, start_item, &token); |
| 3348 | data_len = data_len - btrfs_token_item_offset(l, end_item, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3349 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3350 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3351 | return data_len; |
| 3352 | } |
| 3353 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3354 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3355 | * The space between the end of the leaf items and |
| 3356 | * the start of the leaf data. IOW, how much room |
| 3357 | * the leaf has left for both items and data |
| 3358 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3359 | noinline int btrfs_leaf_free_space(struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3360 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3361 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3362 | int nritems = btrfs_header_nritems(leaf); |
| 3363 | int ret; |
| 3364 | ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 3365 | if (ret < 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3366 | printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " |
| 3367 | "used %d nritems %d\n", |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 3368 | ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3369 | leaf_space_used(leaf, 0, nritems), nritems); |
| 3370 | } |
| 3371 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3372 | } |
| 3373 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3374 | /* |
| 3375 | * min slot controls the lowest index we're willing to push to the |
| 3376 | * right. We'll push up to and including min_slot, but no lower |
| 3377 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3378 | static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, |
| 3379 | struct btrfs_root *root, |
| 3380 | struct btrfs_path *path, |
| 3381 | int data_size, int empty, |
| 3382 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3383 | int free_space, u32 left_nritems, |
| 3384 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3385 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3386 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3387 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3388 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3389 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3390 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3391 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3392 | int push_space = 0; |
| 3393 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3394 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3395 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3396 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3397 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3398 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3399 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3400 | btrfs_init_map_token(&token); |
| 3401 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3402 | if (empty) |
| 3403 | nr = 0; |
| 3404 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3405 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3406 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3407 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3408 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3409 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3410 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3411 | i = left_nritems - 1; |
| 3412 | while (i >= nr) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3413 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3414 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3415 | if (!empty && push_items > 0) { |
| 3416 | if (path->slots[0] > i) |
| 3417 | break; |
| 3418 | if (path->slots[0] == i) { |
| 3419 | int space = btrfs_leaf_free_space(root, left); |
| 3420 | if (space + push_space * 2 > free_space) |
| 3421 | break; |
| 3422 | } |
| 3423 | } |
| 3424 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3425 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3426 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3427 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3428 | this_item_size = btrfs_item_size(left, item); |
| 3429 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3430 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3431 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3432 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3433 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3434 | if (i == 0) |
| 3435 | break; |
| 3436 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3437 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3438 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3439 | if (push_items == 0) |
| 3440 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3441 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 3442 | WARN_ON(!empty && push_items == left_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3443 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3444 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3445 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3446 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3447 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3448 | push_space -= leaf_data_end(root, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3449 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3450 | /* make room in the right data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3451 | data_end = leaf_data_end(root, right); |
| 3452 | memmove_extent_buffer(right, |
| 3453 | btrfs_leaf_data(right) + data_end - push_space, |
| 3454 | btrfs_leaf_data(right) + data_end, |
| 3455 | BTRFS_LEAF_DATA_SIZE(root) - data_end); |
| 3456 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3457 | /* copy from the left data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3458 | copy_extent_buffer(right, left, btrfs_leaf_data(right) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3459 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3460 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 3461 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3462 | |
| 3463 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3464 | btrfs_item_nr_offset(0), |
| 3465 | right_nritems * sizeof(struct btrfs_item)); |
| 3466 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3467 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3468 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3469 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3470 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3471 | |
| 3472 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3473 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3474 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3475 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3476 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3477 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3478 | push_space -= btrfs_token_item_size(right, item, &token); |
| 3479 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3480 | } |
| 3481 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3482 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3483 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3484 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3485 | if (left_nritems) |
| 3486 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3487 | else |
| 3488 | clean_tree_block(trans, root, left); |
| 3489 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3490 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3491 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3492 | btrfs_item_key(right, &disk_key, 0); |
| 3493 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3494 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3495 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3496 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3497 | if (path->slots[0] >= left_nritems) { |
| 3498 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3499 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 3500 | clean_tree_block(trans, root, path->nodes[0]); |
| 3501 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3502 | free_extent_buffer(path->nodes[0]); |
| 3503 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3504 | path->slots[1] += 1; |
| 3505 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3506 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3507 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3508 | } |
| 3509 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3510 | |
| 3511 | out_unlock: |
| 3512 | btrfs_tree_unlock(right); |
| 3513 | free_extent_buffer(right); |
| 3514 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3515 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3516 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3517 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3518 | * push some data in the path leaf to the right, trying to free up at |
| 3519 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3520 | * |
| 3521 | * returns 1 if the push failed because the other node didn't have enough |
| 3522 | * 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] | 3523 | * |
| 3524 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3525 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3526 | */ |
| 3527 | 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] | 3528 | *root, struct btrfs_path *path, |
| 3529 | int min_data_size, int data_size, |
| 3530 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3531 | { |
| 3532 | struct extent_buffer *left = path->nodes[0]; |
| 3533 | struct extent_buffer *right; |
| 3534 | struct extent_buffer *upper; |
| 3535 | int slot; |
| 3536 | int free_space; |
| 3537 | u32 left_nritems; |
| 3538 | int ret; |
| 3539 | |
| 3540 | if (!path->nodes[1]) |
| 3541 | return 1; |
| 3542 | |
| 3543 | slot = path->slots[1]; |
| 3544 | upper = path->nodes[1]; |
| 3545 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3546 | return 1; |
| 3547 | |
| 3548 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3549 | |
| 3550 | right = read_node_slot(root, upper, slot + 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3551 | if (right == NULL) |
| 3552 | return 1; |
| 3553 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3554 | btrfs_tree_lock(right); |
| 3555 | btrfs_set_lock_blocking(right); |
| 3556 | |
| 3557 | free_space = btrfs_leaf_free_space(root, right); |
| 3558 | if (free_space < data_size) |
| 3559 | goto out_unlock; |
| 3560 | |
| 3561 | /* cow and double check */ |
| 3562 | ret = btrfs_cow_block(trans, root, right, upper, |
| 3563 | slot + 1, &right); |
| 3564 | if (ret) |
| 3565 | goto out_unlock; |
| 3566 | |
| 3567 | free_space = btrfs_leaf_free_space(root, right); |
| 3568 | if (free_space < data_size) |
| 3569 | goto out_unlock; |
| 3570 | |
| 3571 | left_nritems = btrfs_header_nritems(left); |
| 3572 | if (left_nritems == 0) |
| 3573 | goto out_unlock; |
| 3574 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3575 | return __push_leaf_right(trans, root, path, min_data_size, empty, |
| 3576 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3577 | out_unlock: |
| 3578 | btrfs_tree_unlock(right); |
| 3579 | free_extent_buffer(right); |
| 3580 | return 1; |
| 3581 | } |
| 3582 | |
| 3583 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3584 | * push some data in the path leaf to the left, trying to free up at |
| 3585 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3586 | * |
| 3587 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3588 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3589 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3590 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3591 | static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, |
| 3592 | struct btrfs_root *root, |
| 3593 | struct btrfs_path *path, int data_size, |
| 3594 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3595 | int free_space, u32 right_nritems, |
| 3596 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3597 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3598 | struct btrfs_disk_key disk_key; |
| 3599 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3600 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3601 | int push_space = 0; |
| 3602 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3603 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3604 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3605 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3606 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3607 | u32 this_item_size; |
| 3608 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3609 | struct btrfs_map_token token; |
| 3610 | |
| 3611 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3612 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3613 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3614 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3615 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3616 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3617 | |
| 3618 | for (i = 0; i < nr; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3619 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3620 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3621 | if (!empty && push_items > 0) { |
| 3622 | if (path->slots[0] < i) |
| 3623 | break; |
| 3624 | if (path->slots[0] == i) { |
| 3625 | int space = btrfs_leaf_free_space(root, right); |
| 3626 | if (space + push_space * 2 > free_space) |
| 3627 | break; |
| 3628 | } |
| 3629 | } |
| 3630 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3631 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3632 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3633 | |
| 3634 | this_item_size = btrfs_item_size(right, item); |
| 3635 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3636 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3637 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3638 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3639 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3640 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3641 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3642 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3643 | ret = 1; |
| 3644 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3645 | } |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3646 | if (!empty && push_items == btrfs_header_nritems(right)) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3647 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3648 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3649 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3650 | copy_extent_buffer(left, right, |
| 3651 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3652 | btrfs_item_nr_offset(0), |
| 3653 | push_items * sizeof(struct btrfs_item)); |
| 3654 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3655 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3656 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3657 | |
| 3658 | copy_extent_buffer(left, right, btrfs_leaf_data(left) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3659 | leaf_data_end(root, left) - push_space, |
| 3660 | btrfs_leaf_data(right) + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3661 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3662 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3663 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3664 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3665 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3666 | 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] | 3667 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3668 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3669 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3670 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3671 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3672 | ioff = btrfs_token_item_offset(left, item, &token); |
| 3673 | btrfs_set_token_item_offset(left, item, |
| 3674 | ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size), |
| 3675 | &token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3676 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3677 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3678 | |
| 3679 | /* fixup right node */ |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3680 | if (push_items > right_nritems) |
| 3681 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3682 | right_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3683 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3684 | if (push_items < right_nritems) { |
| 3685 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
| 3686 | leaf_data_end(root, right); |
| 3687 | memmove_extent_buffer(right, btrfs_leaf_data(right) + |
| 3688 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3689 | btrfs_leaf_data(right) + |
| 3690 | leaf_data_end(root, right), push_space); |
| 3691 | |
| 3692 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3693 | btrfs_item_nr_offset(push_items), |
| 3694 | (btrfs_header_nritems(right) - push_items) * |
| 3695 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3696 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3697 | right_nritems -= push_items; |
| 3698 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3699 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3700 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3701 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3702 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3703 | push_space = push_space - btrfs_token_item_size(right, |
| 3704 | item, &token); |
| 3705 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3706 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3707 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3708 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3709 | if (right_nritems) |
| 3710 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3711 | else |
| 3712 | clean_tree_block(trans, root, right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3713 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3714 | btrfs_item_key(right, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 3715 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3716 | |
| 3717 | /* then fixup the leaf pointer in the path */ |
| 3718 | if (path->slots[0] < push_items) { |
| 3719 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3720 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3721 | free_extent_buffer(path->nodes[0]); |
| 3722 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3723 | path->slots[1] -= 1; |
| 3724 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3725 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3726 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3727 | path->slots[0] -= push_items; |
| 3728 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3729 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3730 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3731 | out: |
| 3732 | btrfs_tree_unlock(left); |
| 3733 | free_extent_buffer(left); |
| 3734 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3735 | } |
| 3736 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3737 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3738 | * push some data in the path leaf to the left, trying to free up at |
| 3739 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3740 | * |
| 3741 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3742 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3743 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3744 | */ |
| 3745 | 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] | 3746 | *root, struct btrfs_path *path, int min_data_size, |
| 3747 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3748 | { |
| 3749 | struct extent_buffer *right = path->nodes[0]; |
| 3750 | struct extent_buffer *left; |
| 3751 | int slot; |
| 3752 | int free_space; |
| 3753 | u32 right_nritems; |
| 3754 | int ret = 0; |
| 3755 | |
| 3756 | slot = path->slots[1]; |
| 3757 | if (slot == 0) |
| 3758 | return 1; |
| 3759 | if (!path->nodes[1]) |
| 3760 | return 1; |
| 3761 | |
| 3762 | right_nritems = btrfs_header_nritems(right); |
| 3763 | if (right_nritems == 0) |
| 3764 | return 1; |
| 3765 | |
| 3766 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3767 | |
| 3768 | left = read_node_slot(root, path->nodes[1], slot - 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3769 | if (left == NULL) |
| 3770 | return 1; |
| 3771 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3772 | btrfs_tree_lock(left); |
| 3773 | btrfs_set_lock_blocking(left); |
| 3774 | |
| 3775 | free_space = btrfs_leaf_free_space(root, left); |
| 3776 | if (free_space < data_size) { |
| 3777 | ret = 1; |
| 3778 | goto out; |
| 3779 | } |
| 3780 | |
| 3781 | /* cow and double check */ |
| 3782 | ret = btrfs_cow_block(trans, root, left, |
| 3783 | path->nodes[1], slot - 1, &left); |
| 3784 | if (ret) { |
| 3785 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3786 | if (ret == -ENOSPC) |
| 3787 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3788 | goto out; |
| 3789 | } |
| 3790 | |
| 3791 | free_space = btrfs_leaf_free_space(root, left); |
| 3792 | if (free_space < data_size) { |
| 3793 | ret = 1; |
| 3794 | goto out; |
| 3795 | } |
| 3796 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3797 | return __push_leaf_left(trans, root, path, min_data_size, |
| 3798 | empty, left, free_space, right_nritems, |
| 3799 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3800 | out: |
| 3801 | btrfs_tree_unlock(left); |
| 3802 | free_extent_buffer(left); |
| 3803 | return ret; |
| 3804 | } |
| 3805 | |
| 3806 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3807 | * split the path's leaf in two, making sure there is at least data_size |
| 3808 | * available for the resulting leaf level of the path. |
| 3809 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3810 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
| 3811 | struct btrfs_root *root, |
| 3812 | struct btrfs_path *path, |
| 3813 | struct extent_buffer *l, |
| 3814 | struct extent_buffer *right, |
| 3815 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3816 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3817 | int data_copy_size; |
| 3818 | int rt_data_off; |
| 3819 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3820 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3821 | struct btrfs_map_token token; |
| 3822 | |
| 3823 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3824 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3825 | nritems = nritems - mid; |
| 3826 | btrfs_set_header_nritems(right, nritems); |
| 3827 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); |
| 3828 | |
| 3829 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 3830 | btrfs_item_nr_offset(mid), |
| 3831 | nritems * sizeof(struct btrfs_item)); |
| 3832 | |
| 3833 | copy_extent_buffer(right, l, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3834 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 3835 | data_copy_size, btrfs_leaf_data(l) + |
| 3836 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3837 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3838 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 3839 | btrfs_item_end_nr(l, mid); |
| 3840 | |
| 3841 | for (i = 0; i < nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3842 | struct btrfs_item *item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3843 | u32 ioff; |
| 3844 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3845 | ioff = btrfs_token_item_offset(right, item, &token); |
| 3846 | btrfs_set_token_item_offset(right, item, |
| 3847 | ioff + rt_data_off, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3848 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3849 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3850 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3851 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3852 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3853 | path->slots[1] + 1, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3854 | |
| 3855 | btrfs_mark_buffer_dirty(right); |
| 3856 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3857 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3858 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3859 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3860 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3861 | free_extent_buffer(path->nodes[0]); |
| 3862 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3863 | path->slots[0] -= mid; |
| 3864 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3865 | } else { |
| 3866 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3867 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3868 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3869 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3870 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3874 | * double splits happen when we need to insert a big item in the middle |
| 3875 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 3876 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 3877 | * A B C |
| 3878 | * |
| 3879 | * We avoid this by trying to push the items on either side of our target |
| 3880 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 3881 | * completely. |
| 3882 | */ |
| 3883 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 3884 | struct btrfs_root *root, |
| 3885 | struct btrfs_path *path, |
| 3886 | int data_size) |
| 3887 | { |
| 3888 | int ret; |
| 3889 | int progress = 0; |
| 3890 | int slot; |
| 3891 | u32 nritems; |
| 3892 | |
| 3893 | slot = path->slots[0]; |
| 3894 | |
| 3895 | /* |
| 3896 | * try to push all the items after our slot into the |
| 3897 | * right leaf |
| 3898 | */ |
| 3899 | ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); |
| 3900 | if (ret < 0) |
| 3901 | return ret; |
| 3902 | |
| 3903 | if (ret == 0) |
| 3904 | progress++; |
| 3905 | |
| 3906 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 3907 | /* |
| 3908 | * our goal is to get our slot at the start or end of a leaf. If |
| 3909 | * we've done so we're done |
| 3910 | */ |
| 3911 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 3912 | return 0; |
| 3913 | |
| 3914 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 3915 | return 0; |
| 3916 | |
| 3917 | /* try to push all the items before our slot into the next leaf */ |
| 3918 | slot = path->slots[0]; |
| 3919 | ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); |
| 3920 | if (ret < 0) |
| 3921 | return ret; |
| 3922 | |
| 3923 | if (ret == 0) |
| 3924 | progress++; |
| 3925 | |
| 3926 | if (progress) |
| 3927 | return 0; |
| 3928 | return 1; |
| 3929 | } |
| 3930 | |
| 3931 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3932 | * split the path's leaf in two, making sure there is at least data_size |
| 3933 | * available for the resulting leaf level of the path. |
| 3934 | * |
| 3935 | * returns 0 if all went well and < 0 on failure. |
| 3936 | */ |
| 3937 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 3938 | struct btrfs_root *root, |
| 3939 | struct btrfs_key *ins_key, |
| 3940 | struct btrfs_path *path, int data_size, |
| 3941 | int extend) |
| 3942 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3943 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3944 | struct extent_buffer *l; |
| 3945 | u32 nritems; |
| 3946 | int mid; |
| 3947 | int slot; |
| 3948 | struct extent_buffer *right; |
| 3949 | int ret = 0; |
| 3950 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3951 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3952 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3953 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3954 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 3955 | l = path->nodes[0]; |
| 3956 | slot = path->slots[0]; |
| 3957 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
| 3958 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) |
| 3959 | return -EOVERFLOW; |
| 3960 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3961 | /* first try to make some room by pushing left and right */ |
Liu Bo | 33157e0 | 2013-05-22 12:07:06 +0000 | [diff] [blame] | 3962 | if (data_size && path->nodes[1]) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3963 | wret = push_leaf_right(trans, root, path, data_size, |
| 3964 | data_size, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3965 | if (wret < 0) |
| 3966 | return wret; |
| 3967 | if (wret) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3968 | wret = push_leaf_left(trans, root, path, data_size, |
| 3969 | data_size, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3970 | if (wret < 0) |
| 3971 | return wret; |
| 3972 | } |
| 3973 | l = path->nodes[0]; |
| 3974 | |
| 3975 | /* did the pushes work? */ |
| 3976 | if (btrfs_leaf_free_space(root, l) >= data_size) |
| 3977 | return 0; |
| 3978 | } |
| 3979 | |
| 3980 | if (!path->nodes[1]) { |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3981 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3982 | if (ret) |
| 3983 | return ret; |
| 3984 | } |
| 3985 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3986 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3987 | l = path->nodes[0]; |
| 3988 | slot = path->slots[0]; |
| 3989 | nritems = btrfs_header_nritems(l); |
| 3990 | mid = (nritems + 1) / 2; |
| 3991 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3992 | if (mid <= slot) { |
| 3993 | if (nritems == 1 || |
| 3994 | leaf_space_used(l, mid, nritems - mid) + data_size > |
| 3995 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3996 | if (slot >= nritems) { |
| 3997 | split = 0; |
| 3998 | } else { |
| 3999 | mid = slot; |
| 4000 | if (mid != nritems && |
| 4001 | leaf_space_used(l, mid, nritems - mid) + |
| 4002 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4003 | if (data_size && !tried_avoid_double) |
| 4004 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4005 | split = 2; |
| 4006 | } |
| 4007 | } |
| 4008 | } |
| 4009 | } else { |
| 4010 | if (leaf_space_used(l, 0, mid) + data_size > |
| 4011 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 4012 | if (!extend && data_size && slot == 0) { |
| 4013 | split = 0; |
| 4014 | } else if ((extend || !data_size) && slot == 0) { |
| 4015 | mid = 1; |
| 4016 | } else { |
| 4017 | mid = slot; |
| 4018 | if (mid != nritems && |
| 4019 | leaf_space_used(l, mid, nritems - mid) + |
| 4020 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4021 | if (data_size && !tried_avoid_double) |
| 4022 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4023 | split = 2 ; |
| 4024 | } |
| 4025 | } |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | if (split == 0) |
| 4030 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 4031 | else |
| 4032 | btrfs_item_key(l, &disk_key, mid); |
| 4033 | |
| 4034 | right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4035 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4036 | &disk_key, 0, l->start, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4037 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4038 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4039 | |
| 4040 | root_add_used(root, root->leafsize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4041 | |
| 4042 | memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); |
| 4043 | btrfs_set_header_bytenr(right, right->start); |
| 4044 | btrfs_set_header_generation(right, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4045 | btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4046 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 4047 | btrfs_set_header_level(right, 0); |
| 4048 | write_extent_buffer(right, root->fs_info->fsid, |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame] | 4049 | btrfs_header_fsid(right), BTRFS_FSID_SIZE); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4050 | |
| 4051 | write_extent_buffer(right, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | b308bc2 | 2013-08-20 13:20:15 +0200 | [diff] [blame] | 4052 | btrfs_header_chunk_tree_uuid(right), |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4053 | BTRFS_UUID_SIZE); |
| 4054 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4055 | if (split == 0) { |
| 4056 | if (mid <= slot) { |
| 4057 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4058 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4059 | path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4060 | btrfs_tree_unlock(path->nodes[0]); |
| 4061 | free_extent_buffer(path->nodes[0]); |
| 4062 | path->nodes[0] = right; |
| 4063 | path->slots[0] = 0; |
| 4064 | path->slots[1] += 1; |
| 4065 | } else { |
| 4066 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4067 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4068 | path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4069 | btrfs_tree_unlock(path->nodes[0]); |
| 4070 | free_extent_buffer(path->nodes[0]); |
| 4071 | path->nodes[0] = right; |
| 4072 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4073 | if (path->slots[1] == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4074 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4075 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4076 | btrfs_mark_buffer_dirty(right); |
| 4077 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4078 | } |
| 4079 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4080 | copy_for_split(trans, root, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4081 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4082 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4083 | BUG_ON(num_doubles != 0); |
| 4084 | num_doubles++; |
| 4085 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4086 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4087 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4088 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4089 | |
| 4090 | push_for_double: |
| 4091 | push_for_double_split(trans, root, path, data_size); |
| 4092 | tried_avoid_double = 1; |
| 4093 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 4094 | return 0; |
| 4095 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4096 | } |
| 4097 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4098 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4099 | struct btrfs_root *root, |
| 4100 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4101 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4102 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4103 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4104 | struct btrfs_file_extent_item *fi; |
| 4105 | u64 extent_len = 0; |
| 4106 | u32 item_size; |
| 4107 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4108 | |
| 4109 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4110 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4111 | |
| 4112 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4113 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4114 | |
| 4115 | if (btrfs_leaf_free_space(root, leaf) >= ins_len) |
| 4116 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4117 | |
| 4118 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4119 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4120 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4121 | struct btrfs_file_extent_item); |
| 4122 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4123 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4124 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4125 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4126 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4127 | path->search_for_split = 1; |
| 4128 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4129 | path->search_for_split = 0; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4130 | if (ret < 0) |
| 4131 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4132 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4133 | ret = -EAGAIN; |
| 4134 | leaf = path->nodes[0]; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4135 | /* if our item isn't there or got smaller, return now */ |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4136 | if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
| 4137 | goto err; |
| 4138 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4139 | /* the leaf has changed, it now has room. return now */ |
| 4140 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) |
| 4141 | goto err; |
| 4142 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4143 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4144 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4145 | struct btrfs_file_extent_item); |
| 4146 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4147 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4148 | } |
| 4149 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4150 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4151 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4152 | if (ret) |
| 4153 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4154 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4155 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4156 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4157 | return 0; |
| 4158 | err: |
| 4159 | path->keep_locks = 0; |
| 4160 | return ret; |
| 4161 | } |
| 4162 | |
| 4163 | static noinline int split_item(struct btrfs_trans_handle *trans, |
| 4164 | struct btrfs_root *root, |
| 4165 | struct btrfs_path *path, |
| 4166 | struct btrfs_key *new_key, |
| 4167 | unsigned long split_offset) |
| 4168 | { |
| 4169 | struct extent_buffer *leaf; |
| 4170 | struct btrfs_item *item; |
| 4171 | struct btrfs_item *new_item; |
| 4172 | int slot; |
| 4173 | char *buf; |
| 4174 | u32 nritems; |
| 4175 | u32 item_size; |
| 4176 | u32 orig_offset; |
| 4177 | struct btrfs_disk_key disk_key; |
| 4178 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4179 | leaf = path->nodes[0]; |
| 4180 | BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); |
| 4181 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4182 | btrfs_set_path_blocking(path); |
| 4183 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4184 | item = btrfs_item_nr(path->slots[0]); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4185 | orig_offset = btrfs_item_offset(leaf, item); |
| 4186 | item_size = btrfs_item_size(leaf, item); |
| 4187 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4188 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4189 | if (!buf) |
| 4190 | return -ENOMEM; |
| 4191 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4192 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4193 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4194 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4195 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4196 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4197 | if (slot != nritems) { |
| 4198 | /* shift the items */ |
| 4199 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4200 | btrfs_item_nr_offset(slot), |
| 4201 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4205 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4206 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4207 | new_item = btrfs_item_nr(slot); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4208 | |
| 4209 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4210 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4211 | |
| 4212 | btrfs_set_item_offset(leaf, item, |
| 4213 | orig_offset + item_size - split_offset); |
| 4214 | btrfs_set_item_size(leaf, item, split_offset); |
| 4215 | |
| 4216 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4217 | |
| 4218 | /* write the data for the start of the original item */ |
| 4219 | write_extent_buffer(leaf, buf, |
| 4220 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4221 | split_offset); |
| 4222 | |
| 4223 | /* write the data for the new item */ |
| 4224 | write_extent_buffer(leaf, buf + split_offset, |
| 4225 | btrfs_item_ptr_offset(leaf, slot), |
| 4226 | item_size - split_offset); |
| 4227 | btrfs_mark_buffer_dirty(leaf); |
| 4228 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4229 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4230 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4231 | return 0; |
| 4232 | } |
| 4233 | |
| 4234 | /* |
| 4235 | * This function splits a single item into two items, |
| 4236 | * giving 'new_key' to the new item and splitting the |
| 4237 | * old one at split_offset (from the start of the item). |
| 4238 | * |
| 4239 | * The path may be released by this operation. After |
| 4240 | * the split, the path is pointing to the old item. The |
| 4241 | * new item is going to be in the same node as the old one. |
| 4242 | * |
| 4243 | * Note, the item being split must be smaller enough to live alone on |
| 4244 | * a tree block with room for one extra struct btrfs_item |
| 4245 | * |
| 4246 | * This allows us to split the item in place, keeping a lock on the |
| 4247 | * leaf the entire time. |
| 4248 | */ |
| 4249 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4250 | struct btrfs_root *root, |
| 4251 | struct btrfs_path *path, |
| 4252 | struct btrfs_key *new_key, |
| 4253 | unsigned long split_offset) |
| 4254 | { |
| 4255 | int ret; |
| 4256 | ret = setup_leaf_for_split(trans, root, path, |
| 4257 | sizeof(struct btrfs_item)); |
| 4258 | if (ret) |
| 4259 | return ret; |
| 4260 | |
| 4261 | ret = split_item(trans, root, path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4262 | return ret; |
| 4263 | } |
| 4264 | |
| 4265 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4266 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4267 | * It guarantees both items live in the same tree leaf and the new item |
| 4268 | * is contiguous with the original item. |
| 4269 | * |
| 4270 | * This allows us to split file extent in place, keeping a lock on the |
| 4271 | * leaf the entire time. |
| 4272 | */ |
| 4273 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4274 | struct btrfs_root *root, |
| 4275 | struct btrfs_path *path, |
| 4276 | struct btrfs_key *new_key) |
| 4277 | { |
| 4278 | struct extent_buffer *leaf; |
| 4279 | int ret; |
| 4280 | u32 item_size; |
| 4281 | |
| 4282 | leaf = path->nodes[0]; |
| 4283 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4284 | ret = setup_leaf_for_split(trans, root, path, |
| 4285 | item_size + sizeof(struct btrfs_item)); |
| 4286 | if (ret) |
| 4287 | return ret; |
| 4288 | |
| 4289 | path->slots[0]++; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4290 | setup_items_for_insert(root, path, new_key, &item_size, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4291 | item_size, item_size + |
| 4292 | sizeof(struct btrfs_item), 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4293 | leaf = path->nodes[0]; |
| 4294 | memcpy_extent_buffer(leaf, |
| 4295 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4296 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4297 | item_size); |
| 4298 | return 0; |
| 4299 | } |
| 4300 | |
| 4301 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4302 | * make the item pointed to by the path smaller. new_size indicates |
| 4303 | * how small to make it, and from_end tells us if we just chop bytes |
| 4304 | * off the end of the item or if we shift the item to chop bytes off |
| 4305 | * the front. |
| 4306 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4307 | void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4308 | u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4309 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4310 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4311 | struct extent_buffer *leaf; |
| 4312 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4313 | u32 nritems; |
| 4314 | unsigned int data_end; |
| 4315 | unsigned int old_data_start; |
| 4316 | unsigned int old_size; |
| 4317 | unsigned int size_diff; |
| 4318 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4319 | struct btrfs_map_token token; |
| 4320 | |
| 4321 | btrfs_init_map_token(&token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4322 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4323 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4324 | slot = path->slots[0]; |
| 4325 | |
| 4326 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4327 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4328 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4329 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4330 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4331 | data_end = leaf_data_end(root, leaf); |
| 4332 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4333 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4334 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4335 | size_diff = old_size - new_size; |
| 4336 | |
| 4337 | BUG_ON(slot < 0); |
| 4338 | BUG_ON(slot >= nritems); |
| 4339 | |
| 4340 | /* |
| 4341 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4342 | */ |
| 4343 | /* first correct the data pointers */ |
| 4344 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4345 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4346 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4347 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4348 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4349 | btrfs_set_token_item_offset(leaf, item, |
| 4350 | ioff + size_diff, &token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4351 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4352 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4353 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4354 | if (from_end) { |
| 4355 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4356 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4357 | data_end, old_data_start + new_size - data_end); |
| 4358 | } else { |
| 4359 | struct btrfs_disk_key disk_key; |
| 4360 | u64 offset; |
| 4361 | |
| 4362 | btrfs_item_key(leaf, &disk_key, slot); |
| 4363 | |
| 4364 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4365 | unsigned long ptr; |
| 4366 | struct btrfs_file_extent_item *fi; |
| 4367 | |
| 4368 | fi = btrfs_item_ptr(leaf, slot, |
| 4369 | struct btrfs_file_extent_item); |
| 4370 | fi = (struct btrfs_file_extent_item *)( |
| 4371 | (unsigned long)fi - size_diff); |
| 4372 | |
| 4373 | if (btrfs_file_extent_type(leaf, fi) == |
| 4374 | BTRFS_FILE_EXTENT_INLINE) { |
| 4375 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4376 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4377 | (unsigned long)fi, |
| 4378 | offsetof(struct btrfs_file_extent_item, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4379 | disk_bytenr)); |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4384 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4385 | data_end, old_data_start - data_end); |
| 4386 | |
| 4387 | offset = btrfs_disk_key_offset(&disk_key); |
| 4388 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4389 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4390 | if (slot == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4391 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4392 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4393 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4394 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4395 | btrfs_set_item_size(leaf, item, new_size); |
| 4396 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4397 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4398 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4399 | btrfs_print_leaf(root, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4400 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4401 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4402 | } |
| 4403 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4404 | /* |
Stefan Behrens | 8f69dbd | 2013-05-07 10:23:30 +0000 | [diff] [blame] | 4405 | * 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] | 4406 | */ |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 4407 | void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4408 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4409 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4410 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4411 | struct extent_buffer *leaf; |
| 4412 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4413 | u32 nritems; |
| 4414 | unsigned int data_end; |
| 4415 | unsigned int old_data; |
| 4416 | unsigned int old_size; |
| 4417 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4418 | struct btrfs_map_token token; |
| 4419 | |
| 4420 | btrfs_init_map_token(&token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4421 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4422 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4423 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4424 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4425 | data_end = leaf_data_end(root, leaf); |
| 4426 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4427 | if (btrfs_leaf_free_space(root, leaf) < data_size) { |
| 4428 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4429 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4430 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4431 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4432 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4433 | |
| 4434 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4435 | if (slot >= nritems) { |
| 4436 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4437 | printk(KERN_CRIT "slot %d too large, nritems %d\n", |
| 4438 | slot, nritems); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4439 | BUG_ON(1); |
| 4440 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4441 | |
| 4442 | /* |
| 4443 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4444 | */ |
| 4445 | /* first correct the data pointers */ |
| 4446 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4447 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4448 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4449 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4450 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4451 | btrfs_set_token_item_offset(leaf, item, |
| 4452 | ioff - data_size, &token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4453 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4454 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4455 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4456 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4457 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 4458 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4459 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4460 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4461 | old_size = btrfs_item_size_nr(leaf, slot); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4462 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4463 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4464 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4465 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4466 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4467 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4468 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4469 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4470 | } |
| 4471 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4472 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4473 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
| 4474 | * to save stack depth by doing the bulk of the work in a function |
| 4475 | * that doesn't call btrfs_search_slot |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4476 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4477 | 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] | 4478 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4479 | u32 total_data, u32 total_size, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4480 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4481 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4482 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4483 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4484 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4485 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4486 | struct extent_buffer *leaf; |
| 4487 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4488 | struct btrfs_map_token token; |
| 4489 | |
| 4490 | btrfs_init_map_token(&token); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4491 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4492 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4493 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4494 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4495 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4496 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4497 | |
Chris Mason | f25956c | 2008-09-12 15:32:53 -0400 | [diff] [blame] | 4498 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4499 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4500 | printk(KERN_CRIT "not enough freespace need %u have %d\n", |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4501 | total_size, btrfs_leaf_free_space(root, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4502 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4503 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4504 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4505 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4506 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4507 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4508 | if (old_data < data_end) { |
| 4509 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4510 | printk(KERN_CRIT "slot %d old_data %d data_end %d\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4511 | slot, old_data, data_end); |
| 4512 | BUG_ON(1); |
| 4513 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4514 | /* |
| 4515 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4516 | */ |
| 4517 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4518 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4519 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4520 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4521 | item = btrfs_item_nr( i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4522 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4523 | btrfs_set_token_item_offset(leaf, item, |
| 4524 | ioff - total_data, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4525 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4526 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4527 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4528 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4529 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4530 | |
| 4531 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4532 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4533 | data_end - total_data, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4534 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4535 | data_end = old_data; |
| 4536 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4537 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4538 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4539 | for (i = 0; i < nr; i++) { |
| 4540 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4541 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4542 | item = btrfs_item_nr(slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4543 | btrfs_set_token_item_offset(leaf, item, |
| 4544 | data_end - data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4545 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4546 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4547 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4548 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4549 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4550 | |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4551 | if (slot == 0) { |
| 4552 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4553 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4554 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4555 | btrfs_unlock_up_safe(path, 1); |
| 4556 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4557 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4558 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4559 | btrfs_print_leaf(root, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4560 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4561 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* |
| 4565 | * Given a key and some data, insert items into the tree. |
| 4566 | * This does all the path init required, making room in the tree if needed. |
| 4567 | */ |
| 4568 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4569 | struct btrfs_root *root, |
| 4570 | struct btrfs_path *path, |
| 4571 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4572 | int nr) |
| 4573 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4574 | int ret = 0; |
| 4575 | int slot; |
| 4576 | int i; |
| 4577 | u32 total_size = 0; |
| 4578 | u32 total_data = 0; |
| 4579 | |
| 4580 | for (i = 0; i < nr; i++) |
| 4581 | total_data += data_size[i]; |
| 4582 | |
| 4583 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4584 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4585 | if (ret == 0) |
| 4586 | return -EEXIST; |
| 4587 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4588 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4589 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4590 | slot = path->slots[0]; |
| 4591 | BUG_ON(slot < 0); |
| 4592 | |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4593 | setup_items_for_insert(root, path, cpu_key, data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4594 | total_data, total_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4595 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | /* |
| 4599 | * Given a key and some data, insert an item into the tree. |
| 4600 | * This does all the path init required, making room in the tree if needed. |
| 4601 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4602 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 4603 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 4604 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4605 | { |
| 4606 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4607 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4608 | struct extent_buffer *leaf; |
| 4609 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4610 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4611 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4612 | if (!path) |
| 4613 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4614 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4615 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4616 | leaf = path->nodes[0]; |
| 4617 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4618 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4619 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4620 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4621 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4622 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4623 | } |
| 4624 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4625 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4626 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4627 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4628 | * the tree should have been previously balanced so the deletion does not |
| 4629 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4630 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4631 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 4632 | int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4633 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4634 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4635 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4636 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4637 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4638 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4639 | if (slot != nritems - 1) { |
Liu Bo | 0e411ec | 2012-10-19 09:50:54 +0000 | [diff] [blame] | 4640 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4641 | tree_mod_log_eb_move(root->fs_info, parent, slot, |
| 4642 | slot + 1, nritems - slot - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4643 | memmove_extent_buffer(parent, |
| 4644 | btrfs_node_key_ptr_offset(slot), |
| 4645 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4646 | sizeof(struct btrfs_key_ptr) * |
| 4647 | (nritems - slot - 1)); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4648 | } else if (level) { |
| 4649 | ret = tree_mod_log_insert_key(root->fs_info, parent, slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 4650 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4651 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4652 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4653 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4654 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4655 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4656 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4657 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4658 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4659 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4660 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4661 | struct btrfs_disk_key disk_key; |
| 4662 | |
| 4663 | btrfs_node_key(parent, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4664 | fixup_low_keys(root, path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4665 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4666 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4667 | } |
| 4668 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4669 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4670 | * 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] | 4671 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4672 | * |
| 4673 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4674 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4675 | * |
| 4676 | * The path must have already been setup for deleting the leaf, including |
| 4677 | * all the proper balancing. path->nodes[1] must be locked. |
| 4678 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4679 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4680 | struct btrfs_root *root, |
| 4681 | struct btrfs_path *path, |
| 4682 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4683 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4684 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4685 | del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4686 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4687 | /* |
| 4688 | * btrfs_free_extent is expensive, we want to make sure we |
| 4689 | * aren't holding any locks when we call it |
| 4690 | */ |
| 4691 | btrfs_unlock_up_safe(path, 0); |
| 4692 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4693 | root_sub_used(root, leaf->len); |
| 4694 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4695 | extent_buffer_get(leaf); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4696 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4697 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4698 | } |
| 4699 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4700 | * delete the item at the leaf level in path. If that empties |
| 4701 | * the leaf, remove it from the tree |
| 4702 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4703 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4704 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4705 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4706 | struct extent_buffer *leaf; |
| 4707 | struct btrfs_item *item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4708 | int last_off; |
| 4709 | int dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4710 | int ret = 0; |
| 4711 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4712 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4713 | u32 nritems; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4714 | struct btrfs_map_token token; |
| 4715 | |
| 4716 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4717 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4718 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4719 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4720 | |
| 4721 | for (i = 0; i < nr; i++) |
| 4722 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4723 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4724 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4725 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4726 | if (slot + nr != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4727 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4728 | |
| 4729 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4730 | data_end + dsize, |
| 4731 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4732 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4733 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4734 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4735 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4736 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4737 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4738 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4739 | btrfs_set_token_item_offset(leaf, item, |
| 4740 | ioff + dsize, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4741 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4742 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4743 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4744 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4745 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4746 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4747 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4748 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4749 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4750 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4751 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4752 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4753 | if (leaf == root->node) { |
| 4754 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4755 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4756 | btrfs_set_path_blocking(path); |
| 4757 | clean_tree_block(trans, root, leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4758 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4759 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4760 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4761 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4762 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4763 | struct btrfs_disk_key disk_key; |
| 4764 | |
| 4765 | btrfs_item_key(leaf, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4766 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4767 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4768 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4769 | /* delete the leaf if it is mostly empty */ |
Yan Zheng | d717aa1 | 2009-07-24 12:42:46 -0400 | [diff] [blame] | 4770 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4771 | /* push_leaf_left fixes the path. |
| 4772 | * make sure the path still points to our leaf |
| 4773 | * for possible call to del_ptr below |
| 4774 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 4775 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4776 | extent_buffer_get(leaf); |
| 4777 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4778 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4779 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 4780 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4781 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4782 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4783 | |
| 4784 | if (path->nodes[0] == leaf && |
| 4785 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4786 | wret = push_leaf_right(trans, root, path, 1, |
| 4787 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4788 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4789 | ret = wret; |
| 4790 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4791 | |
| 4792 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4793 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4794 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4795 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4796 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4797 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4798 | /* if we're still in the path, make sure |
| 4799 | * we're dirty. Otherwise, one of the |
| 4800 | * push_leaf functions must have already |
| 4801 | * dirtied this buffer |
| 4802 | */ |
| 4803 | if (path->nodes[0] == leaf) |
| 4804 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4805 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4806 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 4807 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4808 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4809 | } |
| 4810 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4811 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4812 | } |
| 4813 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 4814 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4815 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4816 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 4817 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4818 | * |
| 4819 | * This may release the path, and so you may lose any locks held at the |
| 4820 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4821 | */ |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 4822 | static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4823 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4824 | struct btrfs_key key; |
| 4825 | struct btrfs_disk_key found_key; |
| 4826 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4827 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4828 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4829 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4830 | if (key.offset > 0) |
| 4831 | key.offset--; |
| 4832 | else if (key.type > 0) |
| 4833 | key.type--; |
| 4834 | else if (key.objectid > 0) |
| 4835 | key.objectid--; |
| 4836 | else |
| 4837 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4838 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4839 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4840 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4841 | if (ret < 0) |
| 4842 | return ret; |
| 4843 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 4844 | ret = comp_keys(&found_key, &key); |
| 4845 | if (ret < 0) |
| 4846 | return 0; |
| 4847 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4848 | } |
| 4849 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4850 | /* |
| 4851 | * 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] | 4852 | * for nodes or leaves that are have a minimum transaction id. |
| 4853 | * This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4854 | * |
| 4855 | * This does not cow, but it does stuff the starting key it finds back |
| 4856 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 4857 | * key and get a writable path. |
| 4858 | * |
| 4859 | * This does lock as it descends, and path->keep_locks should be set |
| 4860 | * to 1 by the caller. |
| 4861 | * |
| 4862 | * This honors path->lowest_level to prevent descent past a given level |
| 4863 | * of the tree. |
| 4864 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4865 | * min_trans indicates the oldest transaction that you are interested |
| 4866 | * in walking through. Any nodes or leaves older than min_trans are |
| 4867 | * skipped over (without reading them). |
| 4868 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4869 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 4870 | * was nothing in the tree that matched the search criteria. |
| 4871 | */ |
| 4872 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4873 | struct btrfs_key *max_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 blockptr; |
| 4919 | u64 gen; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4920 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4921 | blockptr = btrfs_node_blockptr(cur, slot); |
| 4922 | gen = btrfs_node_ptr_generation(cur, slot); |
| 4923 | if (gen < min_trans) { |
| 4924 | slot++; |
| 4925 | continue; |
| 4926 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4927 | break; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4928 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4929 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4930 | /* |
| 4931 | * we didn't find a candidate key in this node, walk forward |
| 4932 | * and find another one |
| 4933 | */ |
| 4934 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4935 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4936 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4937 | sret = btrfs_find_next_key(root, path, min_key, level, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4938 | min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4939 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4940 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4941 | goto again; |
| 4942 | } else { |
| 4943 | goto out; |
| 4944 | } |
| 4945 | } |
| 4946 | /* save our key for returning back */ |
| 4947 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 4948 | path->slots[level] = slot; |
| 4949 | if (level == path->lowest_level) { |
| 4950 | ret = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4951 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4952 | goto out; |
| 4953 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4954 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4955 | cur = read_node_slot(root, cur, slot); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4956 | BUG_ON(!cur); /* -ENOMEM */ |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4957 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4958 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4959 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4960 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4961 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4962 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4963 | btrfs_clear_path_blocking(path, NULL, 0); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4964 | } |
| 4965 | out: |
| 4966 | if (ret == 0) |
| 4967 | memcpy(min_key, &found_key, sizeof(found_key)); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4968 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4969 | return ret; |
| 4970 | } |
| 4971 | |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4972 | static void tree_move_down(struct btrfs_root *root, |
| 4973 | struct btrfs_path *path, |
| 4974 | int *level, int root_level) |
| 4975 | { |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4976 | BUG_ON(*level == 0); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4977 | path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level], |
| 4978 | path->slots[*level]); |
| 4979 | path->slots[*level - 1] = 0; |
| 4980 | (*level)--; |
| 4981 | } |
| 4982 | |
| 4983 | static int tree_move_next_or_upnext(struct btrfs_root *root, |
| 4984 | struct btrfs_path *path, |
| 4985 | int *level, int root_level) |
| 4986 | { |
| 4987 | int ret = 0; |
| 4988 | int nritems; |
| 4989 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 4990 | |
| 4991 | path->slots[*level]++; |
| 4992 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4993 | while (path->slots[*level] >= nritems) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4994 | if (*level == root_level) |
| 4995 | return -1; |
| 4996 | |
| 4997 | /* move upnext */ |
| 4998 | path->slots[*level] = 0; |
| 4999 | free_extent_buffer(path->nodes[*level]); |
| 5000 | path->nodes[*level] = NULL; |
| 5001 | (*level)++; |
| 5002 | path->slots[*level]++; |
| 5003 | |
| 5004 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 5005 | ret = 1; |
| 5006 | } |
| 5007 | return ret; |
| 5008 | } |
| 5009 | |
| 5010 | /* |
| 5011 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 5012 | * or down. |
| 5013 | */ |
| 5014 | static int tree_advance(struct btrfs_root *root, |
| 5015 | struct btrfs_path *path, |
| 5016 | int *level, int root_level, |
| 5017 | int allow_down, |
| 5018 | struct btrfs_key *key) |
| 5019 | { |
| 5020 | int ret; |
| 5021 | |
| 5022 | if (*level == 0 || !allow_down) { |
| 5023 | ret = tree_move_next_or_upnext(root, path, level, root_level); |
| 5024 | } else { |
| 5025 | tree_move_down(root, path, level, root_level); |
| 5026 | ret = 0; |
| 5027 | } |
| 5028 | if (ret >= 0) { |
| 5029 | if (*level == 0) |
| 5030 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 5031 | path->slots[*level]); |
| 5032 | else |
| 5033 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 5034 | path->slots[*level]); |
| 5035 | } |
| 5036 | return ret; |
| 5037 | } |
| 5038 | |
| 5039 | static int tree_compare_item(struct btrfs_root *left_root, |
| 5040 | struct btrfs_path *left_path, |
| 5041 | struct btrfs_path *right_path, |
| 5042 | char *tmp_buf) |
| 5043 | { |
| 5044 | int cmp; |
| 5045 | int len1, len2; |
| 5046 | unsigned long off1, off2; |
| 5047 | |
| 5048 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 5049 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 5050 | if (len1 != len2) |
| 5051 | return 1; |
| 5052 | |
| 5053 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 5054 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 5055 | right_path->slots[0]); |
| 5056 | |
| 5057 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 5058 | |
| 5059 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 5060 | if (cmp) |
| 5061 | return 1; |
| 5062 | return 0; |
| 5063 | } |
| 5064 | |
| 5065 | #define ADVANCE 1 |
| 5066 | #define ADVANCE_ONLY_NEXT -1 |
| 5067 | |
| 5068 | /* |
| 5069 | * This function compares two trees and calls the provided callback for |
| 5070 | * every changed/new/deleted item it finds. |
| 5071 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 5072 | * the compare pretty fast on snapshotted subvolumes. |
| 5073 | * |
| 5074 | * This currently works on commit roots only. As commit roots are read only, |
| 5075 | * we don't do any locking. The commit roots are protected with transactions. |
| 5076 | * Transactions are ended and rejoined when a commit is tried in between. |
| 5077 | * |
| 5078 | * This function checks for modifications done to the trees while comparing. |
| 5079 | * If it detects a change, it aborts immediately. |
| 5080 | */ |
| 5081 | int btrfs_compare_trees(struct btrfs_root *left_root, |
| 5082 | struct btrfs_root *right_root, |
| 5083 | btrfs_changed_cb_t changed_cb, void *ctx) |
| 5084 | { |
| 5085 | int ret; |
| 5086 | int cmp; |
| 5087 | struct btrfs_trans_handle *trans = NULL; |
| 5088 | struct btrfs_path *left_path = NULL; |
| 5089 | struct btrfs_path *right_path = NULL; |
| 5090 | struct btrfs_key left_key; |
| 5091 | struct btrfs_key right_key; |
| 5092 | char *tmp_buf = NULL; |
| 5093 | int left_root_level; |
| 5094 | int right_root_level; |
| 5095 | int left_level; |
| 5096 | int right_level; |
| 5097 | int left_end_reached; |
| 5098 | int right_end_reached; |
| 5099 | int advance_left; |
| 5100 | int advance_right; |
| 5101 | u64 left_blockptr; |
| 5102 | u64 right_blockptr; |
| 5103 | u64 left_start_ctransid; |
| 5104 | u64 right_start_ctransid; |
| 5105 | u64 ctransid; |
| 5106 | |
| 5107 | left_path = btrfs_alloc_path(); |
| 5108 | if (!left_path) { |
| 5109 | ret = -ENOMEM; |
| 5110 | goto out; |
| 5111 | } |
| 5112 | right_path = btrfs_alloc_path(); |
| 5113 | if (!right_path) { |
| 5114 | ret = -ENOMEM; |
| 5115 | goto out; |
| 5116 | } |
| 5117 | |
| 5118 | tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS); |
| 5119 | if (!tmp_buf) { |
| 5120 | ret = -ENOMEM; |
| 5121 | goto out; |
| 5122 | } |
| 5123 | |
| 5124 | left_path->search_commit_root = 1; |
| 5125 | left_path->skip_locking = 1; |
| 5126 | right_path->search_commit_root = 1; |
| 5127 | right_path->skip_locking = 1; |
| 5128 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5129 | spin_lock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5130 | left_start_ctransid = btrfs_root_ctransid(&left_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5131 | spin_unlock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5132 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5133 | spin_lock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5134 | right_start_ctransid = btrfs_root_ctransid(&right_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5135 | spin_unlock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5136 | |
| 5137 | trans = btrfs_join_transaction(left_root); |
| 5138 | if (IS_ERR(trans)) { |
| 5139 | ret = PTR_ERR(trans); |
| 5140 | trans = NULL; |
| 5141 | goto out; |
| 5142 | } |
| 5143 | |
| 5144 | /* |
| 5145 | * Strategy: Go to the first items of both trees. Then do |
| 5146 | * |
| 5147 | * If both trees are at level 0 |
| 5148 | * Compare keys of current items |
| 5149 | * If left < right treat left item as new, advance left tree |
| 5150 | * and repeat |
| 5151 | * If left > right treat right item as deleted, advance right tree |
| 5152 | * and repeat |
| 5153 | * If left == right do deep compare of items, treat as changed if |
| 5154 | * needed, advance both trees and repeat |
| 5155 | * If both trees are at the same level but not at level 0 |
| 5156 | * Compare keys of current nodes/leafs |
| 5157 | * If left < right advance left tree and repeat |
| 5158 | * If left > right advance right tree and repeat |
| 5159 | * If left == right compare blockptrs of the next nodes/leafs |
| 5160 | * If they match advance both trees but stay at the same level |
| 5161 | * and repeat |
| 5162 | * If they don't match advance both trees while allowing to go |
| 5163 | * deeper and repeat |
| 5164 | * If tree levels are different |
| 5165 | * Advance the tree that needs it and repeat |
| 5166 | * |
| 5167 | * Advancing a tree means: |
| 5168 | * If we are at level 0, try to go to the next slot. If that's not |
| 5169 | * possible, go one level up and repeat. Stop when we found a level |
| 5170 | * where we could go to the next slot. We may at this point be on a |
| 5171 | * node or a leaf. |
| 5172 | * |
| 5173 | * If we are not at level 0 and not on shared tree blocks, go one |
| 5174 | * level deeper. |
| 5175 | * |
| 5176 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 5177 | * the right if possible or go up and right. |
| 5178 | */ |
| 5179 | |
| 5180 | left_level = btrfs_header_level(left_root->commit_root); |
| 5181 | left_root_level = left_level; |
| 5182 | left_path->nodes[left_level] = left_root->commit_root; |
| 5183 | extent_buffer_get(left_path->nodes[left_level]); |
| 5184 | |
| 5185 | right_level = btrfs_header_level(right_root->commit_root); |
| 5186 | right_root_level = right_level; |
| 5187 | right_path->nodes[right_level] = right_root->commit_root; |
| 5188 | extent_buffer_get(right_path->nodes[right_level]); |
| 5189 | |
| 5190 | if (left_level == 0) |
| 5191 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 5192 | &left_key, left_path->slots[left_level]); |
| 5193 | else |
| 5194 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 5195 | &left_key, left_path->slots[left_level]); |
| 5196 | if (right_level == 0) |
| 5197 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 5198 | &right_key, right_path->slots[right_level]); |
| 5199 | else |
| 5200 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 5201 | &right_key, right_path->slots[right_level]); |
| 5202 | |
| 5203 | left_end_reached = right_end_reached = 0; |
| 5204 | advance_left = advance_right = 0; |
| 5205 | |
| 5206 | while (1) { |
| 5207 | /* |
| 5208 | * We need to make sure the transaction does not get committed |
| 5209 | * while we do anything on commit roots. This means, we need to |
| 5210 | * join and leave transactions for every item that we process. |
| 5211 | */ |
| 5212 | if (trans && btrfs_should_end_transaction(trans, left_root)) { |
| 5213 | btrfs_release_path(left_path); |
| 5214 | btrfs_release_path(right_path); |
| 5215 | |
| 5216 | ret = btrfs_end_transaction(trans, left_root); |
| 5217 | trans = NULL; |
| 5218 | if (ret < 0) |
| 5219 | goto out; |
| 5220 | } |
| 5221 | /* now rejoin the transaction */ |
| 5222 | if (!trans) { |
| 5223 | trans = btrfs_join_transaction(left_root); |
| 5224 | if (IS_ERR(trans)) { |
| 5225 | ret = PTR_ERR(trans); |
| 5226 | trans = NULL; |
| 5227 | goto out; |
| 5228 | } |
| 5229 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5230 | spin_lock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5231 | ctransid = btrfs_root_ctransid(&left_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5232 | spin_unlock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5233 | if (ctransid != left_start_ctransid) |
| 5234 | left_start_ctransid = 0; |
| 5235 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5236 | spin_lock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5237 | ctransid = btrfs_root_ctransid(&right_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5238 | spin_unlock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5239 | if (ctransid != right_start_ctransid) |
| 5240 | right_start_ctransid = 0; |
| 5241 | |
| 5242 | if (!left_start_ctransid || !right_start_ctransid) { |
| 5243 | WARN(1, KERN_WARNING |
| 5244 | "btrfs: btrfs_compare_tree detected " |
| 5245 | "a change in one of the trees while " |
| 5246 | "iterating. This is probably a " |
| 5247 | "bug.\n"); |
| 5248 | ret = -EIO; |
| 5249 | goto out; |
| 5250 | } |
| 5251 | |
| 5252 | /* |
| 5253 | * the commit root may have changed, so start again |
| 5254 | * where we stopped |
| 5255 | */ |
| 5256 | left_path->lowest_level = left_level; |
| 5257 | right_path->lowest_level = right_level; |
| 5258 | ret = btrfs_search_slot(NULL, left_root, |
| 5259 | &left_key, left_path, 0, 0); |
| 5260 | if (ret < 0) |
| 5261 | goto out; |
| 5262 | ret = btrfs_search_slot(NULL, right_root, |
| 5263 | &right_key, right_path, 0, 0); |
| 5264 | if (ret < 0) |
| 5265 | goto out; |
| 5266 | } |
| 5267 | |
| 5268 | if (advance_left && !left_end_reached) { |
| 5269 | ret = tree_advance(left_root, left_path, &left_level, |
| 5270 | left_root_level, |
| 5271 | advance_left != ADVANCE_ONLY_NEXT, |
| 5272 | &left_key); |
| 5273 | if (ret < 0) |
| 5274 | left_end_reached = ADVANCE; |
| 5275 | advance_left = 0; |
| 5276 | } |
| 5277 | if (advance_right && !right_end_reached) { |
| 5278 | ret = tree_advance(right_root, right_path, &right_level, |
| 5279 | right_root_level, |
| 5280 | advance_right != ADVANCE_ONLY_NEXT, |
| 5281 | &right_key); |
| 5282 | if (ret < 0) |
| 5283 | right_end_reached = ADVANCE; |
| 5284 | advance_right = 0; |
| 5285 | } |
| 5286 | |
| 5287 | if (left_end_reached && right_end_reached) { |
| 5288 | ret = 0; |
| 5289 | goto out; |
| 5290 | } else if (left_end_reached) { |
| 5291 | if (right_level == 0) { |
| 5292 | ret = changed_cb(left_root, right_root, |
| 5293 | left_path, right_path, |
| 5294 | &right_key, |
| 5295 | BTRFS_COMPARE_TREE_DELETED, |
| 5296 | ctx); |
| 5297 | if (ret < 0) |
| 5298 | goto out; |
| 5299 | } |
| 5300 | advance_right = ADVANCE; |
| 5301 | continue; |
| 5302 | } else if (right_end_reached) { |
| 5303 | if (left_level == 0) { |
| 5304 | ret = changed_cb(left_root, right_root, |
| 5305 | left_path, right_path, |
| 5306 | &left_key, |
| 5307 | BTRFS_COMPARE_TREE_NEW, |
| 5308 | ctx); |
| 5309 | if (ret < 0) |
| 5310 | goto out; |
| 5311 | } |
| 5312 | advance_left = ADVANCE; |
| 5313 | continue; |
| 5314 | } |
| 5315 | |
| 5316 | if (left_level == 0 && right_level == 0) { |
| 5317 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5318 | if (cmp < 0) { |
| 5319 | ret = changed_cb(left_root, right_root, |
| 5320 | left_path, right_path, |
| 5321 | &left_key, |
| 5322 | BTRFS_COMPARE_TREE_NEW, |
| 5323 | ctx); |
| 5324 | if (ret < 0) |
| 5325 | goto out; |
| 5326 | advance_left = ADVANCE; |
| 5327 | } else if (cmp > 0) { |
| 5328 | ret = changed_cb(left_root, right_root, |
| 5329 | left_path, right_path, |
| 5330 | &right_key, |
| 5331 | BTRFS_COMPARE_TREE_DELETED, |
| 5332 | ctx); |
| 5333 | if (ret < 0) |
| 5334 | goto out; |
| 5335 | advance_right = ADVANCE; |
| 5336 | } else { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5337 | enum btrfs_compare_tree_result cmp; |
| 5338 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5339 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5340 | ret = tree_compare_item(left_root, left_path, |
| 5341 | right_path, tmp_buf); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5342 | if (ret) |
| 5343 | cmp = BTRFS_COMPARE_TREE_CHANGED; |
| 5344 | else |
| 5345 | cmp = BTRFS_COMPARE_TREE_SAME; |
| 5346 | ret = changed_cb(left_root, right_root, |
| 5347 | left_path, right_path, |
| 5348 | &left_key, cmp, ctx); |
| 5349 | if (ret < 0) |
| 5350 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5351 | advance_left = ADVANCE; |
| 5352 | advance_right = ADVANCE; |
| 5353 | } |
| 5354 | } else if (left_level == right_level) { |
| 5355 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5356 | if (cmp < 0) { |
| 5357 | advance_left = ADVANCE; |
| 5358 | } else if (cmp > 0) { |
| 5359 | advance_right = ADVANCE; |
| 5360 | } else { |
| 5361 | left_blockptr = btrfs_node_blockptr( |
| 5362 | left_path->nodes[left_level], |
| 5363 | left_path->slots[left_level]); |
| 5364 | right_blockptr = btrfs_node_blockptr( |
| 5365 | right_path->nodes[right_level], |
| 5366 | right_path->slots[right_level]); |
| 5367 | if (left_blockptr == right_blockptr) { |
| 5368 | /* |
| 5369 | * As we're on a shared block, don't |
| 5370 | * allow to go deeper. |
| 5371 | */ |
| 5372 | advance_left = ADVANCE_ONLY_NEXT; |
| 5373 | advance_right = ADVANCE_ONLY_NEXT; |
| 5374 | } else { |
| 5375 | advance_left = ADVANCE; |
| 5376 | advance_right = ADVANCE; |
| 5377 | } |
| 5378 | } |
| 5379 | } else if (left_level < right_level) { |
| 5380 | advance_right = ADVANCE; |
| 5381 | } else { |
| 5382 | advance_left = ADVANCE; |
| 5383 | } |
| 5384 | } |
| 5385 | |
| 5386 | out: |
| 5387 | btrfs_free_path(left_path); |
| 5388 | btrfs_free_path(right_path); |
| 5389 | kfree(tmp_buf); |
| 5390 | |
| 5391 | if (trans) { |
| 5392 | if (!ret) |
| 5393 | ret = btrfs_end_transaction(trans, left_root); |
| 5394 | else |
| 5395 | btrfs_end_transaction(trans, left_root); |
| 5396 | } |
| 5397 | |
| 5398 | return ret; |
| 5399 | } |
| 5400 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5401 | /* |
| 5402 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5403 | * 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] | 5404 | * tree based on the current path and the min_trans parameters. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5405 | * |
| 5406 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5407 | * and 1 is returned if there are no higher keys in the tree |
| 5408 | * |
| 5409 | * path->keep_locks should be set to 1 on the search made before |
| 5410 | * calling this function. |
| 5411 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5412 | 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] | 5413 | struct btrfs_key *key, int level, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5414 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5415 | int slot; |
| 5416 | struct extent_buffer *c; |
| 5417 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5418 | WARN_ON(!path->keep_locks); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5419 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5420 | if (!path->nodes[level]) |
| 5421 | return 1; |
| 5422 | |
| 5423 | slot = path->slots[level] + 1; |
| 5424 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5425 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5426 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5427 | int ret; |
| 5428 | int orig_lowest; |
| 5429 | struct btrfs_key cur_key; |
| 5430 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5431 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5432 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5433 | |
| 5434 | if (path->locks[level + 1]) { |
| 5435 | level++; |
| 5436 | continue; |
| 5437 | } |
| 5438 | |
| 5439 | slot = btrfs_header_nritems(c) - 1; |
| 5440 | if (level == 0) |
| 5441 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5442 | else |
| 5443 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5444 | |
| 5445 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5446 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5447 | path->lowest_level = level; |
| 5448 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5449 | 0, 0); |
| 5450 | path->lowest_level = orig_lowest; |
| 5451 | if (ret < 0) |
| 5452 | return ret; |
| 5453 | |
| 5454 | c = path->nodes[level]; |
| 5455 | slot = path->slots[level]; |
| 5456 | if (ret == 0) |
| 5457 | slot++; |
| 5458 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5459 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5460 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5461 | if (level == 0) |
| 5462 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5463 | else { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5464 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5465 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5466 | if (gen < min_trans) { |
| 5467 | slot++; |
| 5468 | goto next; |
| 5469 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5470 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5471 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5472 | return 0; |
| 5473 | } |
| 5474 | return 1; |
| 5475 | } |
| 5476 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5477 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5478 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5479 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5480 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5481 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5482 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5483 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5484 | return btrfs_next_old_leaf(root, path, 0); |
| 5485 | } |
| 5486 | |
| 5487 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5488 | u64 time_seq) |
| 5489 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5490 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5491 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5492 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5493 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5494 | struct btrfs_key key; |
| 5495 | u32 nritems; |
| 5496 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5497 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5498 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5499 | |
| 5500 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5501 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5502 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5503 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5504 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5505 | again: |
| 5506 | level = 1; |
| 5507 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5508 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5509 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5510 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5511 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5512 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5513 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5514 | if (time_seq) |
| 5515 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5516 | else |
| 5517 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5518 | path->keep_locks = 0; |
| 5519 | |
| 5520 | if (ret < 0) |
| 5521 | return ret; |
| 5522 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5523 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5524 | /* |
| 5525 | * by releasing the path above we dropped all our locks. A balance |
| 5526 | * could have added more items next to the key that used to be |
| 5527 | * at the very end of the block. So, check again here and |
| 5528 | * advance the path if there are now more items available. |
| 5529 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5530 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5531 | if (ret == 0) |
| 5532 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5533 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5534 | goto done; |
| 5535 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5536 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5537 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5538 | if (!path->nodes[level]) { |
| 5539 | ret = 1; |
| 5540 | goto done; |
| 5541 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5542 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5543 | slot = path->slots[level] + 1; |
| 5544 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5545 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5546 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5547 | if (level == BTRFS_MAX_LEVEL) { |
| 5548 | ret = 1; |
| 5549 | goto done; |
| 5550 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5551 | continue; |
| 5552 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5553 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5554 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5555 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5556 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5557 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5558 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5559 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5560 | next_rw_lock = path->locks[level]; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5561 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5562 | slot, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5563 | if (ret == -EAGAIN) |
| 5564 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5565 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5566 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5567 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5568 | goto done; |
| 5569 | } |
| 5570 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5571 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5572 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5573 | if (!ret && time_seq) { |
| 5574 | /* |
| 5575 | * If we don't get the lock, we may be racing |
| 5576 | * with push_leaf_left, holding that lock while |
| 5577 | * itself waiting for the leaf we've currently |
| 5578 | * locked. To solve this situation, we give up |
| 5579 | * on our lock and cycle. |
| 5580 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5581 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5582 | btrfs_release_path(path); |
| 5583 | cond_resched(); |
| 5584 | goto again; |
| 5585 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5586 | if (!ret) { |
| 5587 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5588 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5589 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5590 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5591 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5592 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5593 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5594 | break; |
| 5595 | } |
| 5596 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5597 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5598 | level--; |
| 5599 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5600 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5601 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5602 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5603 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5604 | path->nodes[level] = next; |
| 5605 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5606 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5607 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5608 | if (!level) |
| 5609 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5610 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5611 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5612 | 0, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5613 | if (ret == -EAGAIN) |
| 5614 | goto again; |
| 5615 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5616 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5617 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5618 | goto done; |
| 5619 | } |
| 5620 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5621 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5622 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5623 | if (!ret) { |
| 5624 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5625 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5626 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5627 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5628 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5629 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5630 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5631 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5632 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5633 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5634 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5635 | path->leave_spinning = old_spinning; |
| 5636 | if (!old_spinning) |
| 5637 | btrfs_set_path_blocking(path); |
| 5638 | |
| 5639 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5640 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5641 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5642 | /* |
| 5643 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5644 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5645 | * |
| 5646 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5647 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5648 | int btrfs_previous_item(struct btrfs_root *root, |
| 5649 | struct btrfs_path *path, u64 min_objectid, |
| 5650 | int type) |
| 5651 | { |
| 5652 | struct btrfs_key found_key; |
| 5653 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5654 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5655 | int ret; |
| 5656 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5657 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5658 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5659 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5660 | ret = btrfs_prev_leaf(root, path); |
| 5661 | if (ret != 0) |
| 5662 | return ret; |
| 5663 | } else { |
| 5664 | path->slots[0]--; |
| 5665 | } |
| 5666 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5667 | nritems = btrfs_header_nritems(leaf); |
| 5668 | if (nritems == 0) |
| 5669 | return 1; |
| 5670 | if (path->slots[0] == nritems) |
| 5671 | path->slots[0]--; |
| 5672 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5673 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5674 | if (found_key.objectid < min_objectid) |
| 5675 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5676 | if (found_key.type == type) |
| 5677 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5678 | if (found_key.objectid == min_objectid && |
| 5679 | found_key.type < type) |
| 5680 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5681 | } |
| 5682 | return 1; |
| 5683 | } |