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 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1008 | if (root->ref_cows) |
| 1009 | btrfs_reloc_cow_block(trans, root, buf, cow); |
| 1010 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1011 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1012 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1013 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1014 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1015 | parent_start = buf->start; |
| 1016 | else |
| 1017 | parent_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1018 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1019 | extent_buffer_get(cow); |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1020 | tree_mod_log_set_root_pointer(root, cow, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1021 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1022 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1023 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1024 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1025 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1026 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1027 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1028 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1029 | parent_start = parent->start; |
| 1030 | else |
| 1031 | parent_start = 0; |
| 1032 | |
| 1033 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1034 | tree_mod_log_insert_key(root->fs_info, parent, parent_slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 1035 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1036 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1037 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1038 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1039 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1040 | btrfs_mark_buffer_dirty(parent); |
Josef Bacik | 7fb7d76f | 2013-07-01 16:10:16 -0400 | [diff] [blame] | 1041 | if (last_ref) |
| 1042 | tree_mod_log_free_eb(root->fs_info, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1043 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1044 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1045 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1046 | if (unlock_orig) |
| 1047 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1048 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1049 | btrfs_mark_buffer_dirty(cow); |
| 1050 | *cow_ret = cow; |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1054 | /* |
| 1055 | * returns the logical address of the oldest predecessor of the given root. |
| 1056 | * entries older than time_seq are ignored. |
| 1057 | */ |
| 1058 | static struct tree_mod_elem * |
| 1059 | __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1060 | struct extent_buffer *eb_root, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1061 | { |
| 1062 | struct tree_mod_elem *tm; |
| 1063 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1064 | u64 root_logical = eb_root->start; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1065 | int looped = 0; |
| 1066 | |
| 1067 | if (!time_seq) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1068 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* |
| 1071 | * the very last operation that's logged for a root is the replacement |
| 1072 | * operation (if it is replaced at all). this has the index of the *new* |
| 1073 | * root, making it the very first operation that's logged for this root. |
| 1074 | */ |
| 1075 | while (1) { |
| 1076 | tm = tree_mod_log_search_oldest(fs_info, root_logical, |
| 1077 | time_seq); |
| 1078 | if (!looped && !tm) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1079 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1080 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1081 | * if there are no tree operation for the oldest root, we simply |
| 1082 | * return it. this should only happen if that (old) root is at |
| 1083 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1084 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1085 | if (!tm) |
| 1086 | break; |
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 | /* |
| 1089 | * if there's an operation that's not a root replacement, we |
| 1090 | * found the oldest version of our root. normally, we'll find a |
| 1091 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1092 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1093 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1094 | break; |
| 1095 | |
| 1096 | found = tm; |
| 1097 | root_logical = tm->old_root.logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1098 | looped = 1; |
| 1099 | } |
| 1100 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1101 | /* if there's no old root to return, return what we found instead */ |
| 1102 | if (!found) |
| 1103 | found = tm; |
| 1104 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1105 | return found; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * tm is a pointer to the first operation to rewind within eb. then, all |
| 1110 | * previous operations will be rewinded (until we reach something older than |
| 1111 | * time_seq). |
| 1112 | */ |
| 1113 | static void |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1114 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1115 | u64 time_seq, struct tree_mod_elem *first_tm) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1116 | { |
| 1117 | u32 n; |
| 1118 | struct rb_node *next; |
| 1119 | struct tree_mod_elem *tm = first_tm; |
| 1120 | unsigned long o_dst; |
| 1121 | unsigned long o_src; |
| 1122 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1123 | |
| 1124 | n = btrfs_header_nritems(eb); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1125 | tree_mod_log_read_lock(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1126 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1127 | /* |
| 1128 | * all the operations are recorded with the operator used for |
| 1129 | * the modification. as we're going backwards, we do the |
| 1130 | * opposite of each operation here. |
| 1131 | */ |
| 1132 | switch (tm->op) { |
| 1133 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1134 | BUG_ON(tm->slot < n); |
Eric Sandeen | 1c697d4 | 2013-01-31 00:54:56 +0000 | [diff] [blame] | 1135 | /* Fallthrough */ |
Liu Bo | 95c80bb | 2012-10-19 09:50:52 +0000 | [diff] [blame] | 1136 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1137 | case MOD_LOG_KEY_REMOVE: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1138 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1139 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1140 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1141 | tm->generation); |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1142 | n++; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1143 | break; |
| 1144 | case MOD_LOG_KEY_REPLACE: |
| 1145 | BUG_ON(tm->slot >= n); |
| 1146 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1147 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1148 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1149 | tm->generation); |
| 1150 | break; |
| 1151 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1152 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1153 | n--; |
| 1154 | break; |
| 1155 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1156 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1157 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1158 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1159 | tm->move.nr_items * p_size); |
| 1160 | break; |
| 1161 | case MOD_LOG_ROOT_REPLACE: |
| 1162 | /* |
| 1163 | * this operation is special. for roots, this must be |
| 1164 | * handled explicitly before rewinding. |
| 1165 | * for non-roots, this operation may exist if the node |
| 1166 | * was a root: root A -> child B; then A gets empty and |
| 1167 | * B is promoted to the new root. in the mod log, we'll |
| 1168 | * have a root-replace operation for B, a tree block |
| 1169 | * that is no root. we simply ignore that operation. |
| 1170 | */ |
| 1171 | break; |
| 1172 | } |
| 1173 | next = rb_next(&tm->node); |
| 1174 | if (!next) |
| 1175 | break; |
| 1176 | tm = container_of(next, struct tree_mod_elem, node); |
| 1177 | if (tm->index != first_tm->index) |
| 1178 | break; |
| 1179 | } |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1180 | tree_mod_log_read_unlock(fs_info); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1181 | btrfs_set_header_nritems(eb, n); |
| 1182 | } |
| 1183 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1184 | /* |
| 1185 | * Called with eb read locked. If the buffer cannot be rewinded, the same buffer |
| 1186 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
| 1187 | * returned buffer is always read-locked. If the returned buffer is not the |
| 1188 | * input buffer, the lock on the input buffer is released and the input buffer |
| 1189 | * is freed (its refcount is decremented). |
| 1190 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1191 | static struct extent_buffer * |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1192 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 1193 | struct extent_buffer *eb, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1194 | { |
| 1195 | struct extent_buffer *eb_rewin; |
| 1196 | struct tree_mod_elem *tm; |
| 1197 | |
| 1198 | if (!time_seq) |
| 1199 | return eb; |
| 1200 | |
| 1201 | if (btrfs_header_level(eb) == 0) |
| 1202 | return eb; |
| 1203 | |
| 1204 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1205 | if (!tm) |
| 1206 | return eb; |
| 1207 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1208 | btrfs_set_path_blocking(path); |
| 1209 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1210 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1211 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1212 | BUG_ON(tm->slot != 0); |
| 1213 | eb_rewin = alloc_dummy_extent_buffer(eb->start, |
| 1214 | fs_info->tree_root->nodesize); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1215 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1216 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1217 | free_extent_buffer(eb); |
| 1218 | return NULL; |
| 1219 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1220 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1221 | btrfs_set_header_backref_rev(eb_rewin, |
| 1222 | btrfs_header_backref_rev(eb)); |
| 1223 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1224 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1225 | } else { |
| 1226 | eb_rewin = btrfs_clone_extent_buffer(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1227 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1228 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1229 | free_extent_buffer(eb); |
| 1230 | return NULL; |
| 1231 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1234 | btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK); |
| 1235 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1236 | free_extent_buffer(eb); |
| 1237 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1238 | extent_buffer_get(eb_rewin); |
| 1239 | btrfs_tree_read_lock(eb_rewin); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1240 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1241 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
Arne Jansen | 2a745b1 | 2013-02-13 04:20:01 -0700 | [diff] [blame] | 1242 | BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1243 | |
| 1244 | return eb_rewin; |
| 1245 | } |
| 1246 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1247 | /* |
| 1248 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1249 | * value. If there are no changes, the current root->root_node is returned. If |
| 1250 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1251 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1252 | * Returns NULL on error (with no locks held). |
| 1253 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1254 | static inline struct extent_buffer * |
| 1255 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1256 | { |
| 1257 | struct tree_mod_elem *tm; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1258 | struct extent_buffer *eb = NULL; |
| 1259 | struct extent_buffer *eb_root; |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1260 | struct extent_buffer *old; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1261 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1262 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1263 | u64 logical; |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1264 | u32 blocksize; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1265 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1266 | eb_root = btrfs_read_lock_root_node(root); |
| 1267 | 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] | 1268 | if (!tm) |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1269 | return eb_root; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1270 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1271 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1272 | old_root = &tm->old_root; |
| 1273 | old_generation = tm->generation; |
| 1274 | logical = old_root->logical; |
| 1275 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1276 | logical = eb_root->start; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1277 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1278 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1279 | tm = tree_mod_log_search(root->fs_info, logical, time_seq); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1280 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1281 | btrfs_tree_read_unlock(eb_root); |
| 1282 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1283 | blocksize = btrfs_level_size(root, old_root->level); |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1284 | old = read_tree_block(root, logical, blocksize, 0); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1285 | if (!old || !extent_buffer_uptodate(old)) { |
| 1286 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1287 | pr_warn("btrfs: failed to read tree block %llu from get_old_root\n", |
| 1288 | logical); |
| 1289 | WARN_ON(1); |
| 1290 | } else { |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1291 | eb = btrfs_clone_extent_buffer(old); |
| 1292 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1293 | } |
| 1294 | } else if (old_root) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1295 | btrfs_tree_read_unlock(eb_root); |
| 1296 | free_extent_buffer(eb_root); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1297 | eb = alloc_dummy_extent_buffer(logical, root->nodesize); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1298 | } else { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1299 | btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1300 | eb = btrfs_clone_extent_buffer(eb_root); |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1301 | btrfs_tree_read_unlock_blocking(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1302 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1305 | if (!eb) |
| 1306 | return NULL; |
Jan Schmidt | d638108 | 2012-10-23 14:21:05 +0200 | [diff] [blame] | 1307 | extent_buffer_get(eb); |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1308 | btrfs_tree_read_lock(eb); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1309 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1310 | btrfs_set_header_bytenr(eb, eb->start); |
| 1311 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1312 | btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1313 | btrfs_set_header_level(eb, old_root->level); |
| 1314 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1315 | } |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1316 | if (tm) |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1317 | __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1318 | else |
| 1319 | WARN_ON(btrfs_header_level(eb) != 0); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1320 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1321 | |
| 1322 | return eb; |
| 1323 | } |
| 1324 | |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1325 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
| 1326 | { |
| 1327 | struct tree_mod_elem *tm; |
| 1328 | int level; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1329 | struct extent_buffer *eb_root = btrfs_root_node(root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1330 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1331 | 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] | 1332 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1333 | level = tm->old_root.level; |
| 1334 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1335 | level = btrfs_header_level(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1336 | } |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1337 | free_extent_buffer(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1338 | |
| 1339 | return level; |
| 1340 | } |
| 1341 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1342 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1343 | struct btrfs_root *root, |
| 1344 | struct extent_buffer *buf) |
| 1345 | { |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1346 | /* ensure we can see the force_cow */ |
| 1347 | smp_rmb(); |
| 1348 | |
| 1349 | /* |
| 1350 | * We do not need to cow a block if |
| 1351 | * 1) this block is not created or changed in this transaction; |
| 1352 | * 2) this block does not belong to TREE_RELOC tree; |
| 1353 | * 3) the root is not forced COW. |
| 1354 | * |
| 1355 | * What is forced COW: |
| 1356 | * when we create snapshot during commiting the transaction, |
| 1357 | * after we've finished coping src root, we must COW the shared |
| 1358 | * block to ensure the metadata consistency. |
| 1359 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1360 | if (btrfs_header_generation(buf) == trans->transid && |
| 1361 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1362 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1363 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
| 1364 | !root->force_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1365 | return 0; |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1369 | /* |
| 1370 | * cows a single block, see __btrfs_cow_block for the real work. |
| 1371 | * This version of it has extra checks so that a block isn't cow'd more than |
| 1372 | * once per transaction, as long as it hasn't been written yet |
| 1373 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1374 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1375 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1376 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1377 | struct extent_buffer **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1378 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1379 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1380 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1381 | |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1382 | if (trans->transaction != root->fs_info->running_transaction) |
| 1383 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1384 | trans->transid, |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1385 | root->fs_info->running_transaction->transid); |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1386 | |
| 1387 | if (trans->transid != root->fs_info->generation) |
| 1388 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1389 | trans->transid, root->fs_info->generation); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1390 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1391 | if (!should_cow_block(trans, root, buf)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1392 | *cow_ret = buf; |
| 1393 | return 0; |
| 1394 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1395 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1396 | search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1397 | |
| 1398 | if (parent) |
| 1399 | btrfs_set_lock_blocking(parent); |
| 1400 | btrfs_set_lock_blocking(buf); |
| 1401 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1402 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1403 | parent_slot, cow_ret, search_start, 0); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1404 | |
| 1405 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1406 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1407 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1408 | } |
| 1409 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1410 | /* |
| 1411 | * helper function for defrag to decide if two blocks pointed to by a |
| 1412 | * node are actually close by |
| 1413 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1414 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1415 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1416 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1417 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1418 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1419 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1423 | /* |
| 1424 | * compare two keys in a memcmp fashion |
| 1425 | */ |
| 1426 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
| 1427 | { |
| 1428 | struct btrfs_key k1; |
| 1429 | |
| 1430 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1431 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1432 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1433 | } |
| 1434 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1435 | /* |
| 1436 | * same as comp_keys only with two btrfs_key's |
| 1437 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1438 | 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] | 1439 | { |
| 1440 | if (k1->objectid > k2->objectid) |
| 1441 | return 1; |
| 1442 | if (k1->objectid < k2->objectid) |
| 1443 | return -1; |
| 1444 | if (k1->type > k2->type) |
| 1445 | return 1; |
| 1446 | if (k1->type < k2->type) |
| 1447 | return -1; |
| 1448 | if (k1->offset > k2->offset) |
| 1449 | return 1; |
| 1450 | if (k1->offset < k2->offset) |
| 1451 | return -1; |
| 1452 | return 0; |
| 1453 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1454 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1455 | /* |
| 1456 | * this is used by the defrag code to go through all the |
| 1457 | * leaves pointed to by a node and reallocate them so that |
| 1458 | * disk order is close to key order |
| 1459 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1460 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1461 | struct btrfs_root *root, struct extent_buffer *parent, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1462 | int start_slot, u64 *last_ret, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1463 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1464 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1465 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1466 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1467 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1468 | u64 search_start = *last_ret; |
| 1469 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1470 | u64 other; |
| 1471 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1472 | int end_slot; |
| 1473 | int i; |
| 1474 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1475 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1476 | int uptodate; |
| 1477 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1478 | int progress_passed = 0; |
| 1479 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1480 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1481 | parent_level = btrfs_header_level(parent); |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1482 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1483 | WARN_ON(trans->transaction != root->fs_info->running_transaction); |
| 1484 | WARN_ON(trans->transid != root->fs_info->generation); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1485 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1486 | parent_nritems = btrfs_header_nritems(parent); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1487 | blocksize = btrfs_level_size(root, parent_level - 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1488 | end_slot = parent_nritems; |
| 1489 | |
| 1490 | if (parent_nritems == 1) |
| 1491 | return 0; |
| 1492 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1493 | btrfs_set_lock_blocking(parent); |
| 1494 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1495 | for (i = start_slot; i < end_slot; i++) { |
| 1496 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1497 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1498 | btrfs_node_key(parent, &disk_key, i); |
| 1499 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1500 | continue; |
| 1501 | |
| 1502 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1503 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1504 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1505 | if (last_block == 0) |
| 1506 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1507 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1508 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1509 | other = btrfs_node_blockptr(parent, i - 1); |
| 1510 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1511 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1512 | if (!close && i < end_slot - 2) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1513 | other = btrfs_node_blockptr(parent, i + 1); |
| 1514 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1515 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1516 | if (close) { |
| 1517 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1518 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1519 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1520 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1521 | cur = btrfs_find_tree_block(root, blocknr, blocksize); |
| 1522 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1523 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1524 | else |
| 1525 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1526 | if (!cur || !uptodate) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1527 | if (!cur) { |
| 1528 | cur = read_tree_block(root, blocknr, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1529 | blocksize, gen); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1530 | if (!cur || !extent_buffer_uptodate(cur)) { |
| 1531 | free_extent_buffer(cur); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1532 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1533 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1534 | } else if (!uptodate) { |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 1535 | err = btrfs_read_buffer(cur, gen); |
| 1536 | if (err) { |
| 1537 | free_extent_buffer(cur); |
| 1538 | return err; |
| 1539 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1540 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1541 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1542 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1543 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1544 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1545 | btrfs_tree_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1546 | btrfs_set_lock_blocking(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1547 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1548 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1549 | min(16 * blocksize, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1550 | (end_slot - i) * blocksize)); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1551 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1552 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1553 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1554 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1555 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1556 | search_start = cur->start; |
| 1557 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1558 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1559 | btrfs_tree_unlock(cur); |
| 1560 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1561 | } |
| 1562 | return err; |
| 1563 | } |
| 1564 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1565 | /* |
| 1566 | * The leaf data grows from end-to-front in the node. |
| 1567 | * this returns the address of the start of the last item, |
| 1568 | * which is the stop of the leaf data stack |
| 1569 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1570 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1571 | struct extent_buffer *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1572 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1573 | u32 nr = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1574 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1575 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1576 | return btrfs_item_offset_nr(leaf, nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1579 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1580 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1581 | * search for key in the extent_buffer. The items start at offset p, |
| 1582 | * and they are item_size apart. There are 'max' items in p. |
| 1583 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1584 | * the slot in the array is returned via slot, and it points to |
| 1585 | * the place where you would insert key if it is not found in |
| 1586 | * the array. |
| 1587 | * |
| 1588 | * slot may point to max if the key is bigger than all of the keys |
| 1589 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1590 | static noinline int generic_bin_search(struct extent_buffer *eb, |
| 1591 | unsigned long p, |
| 1592 | int item_size, struct btrfs_key *key, |
| 1593 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1594 | { |
| 1595 | int low = 0; |
| 1596 | int high = max; |
| 1597 | int mid; |
| 1598 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1599 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1600 | struct btrfs_disk_key unaligned; |
| 1601 | unsigned long offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1602 | char *kaddr = NULL; |
| 1603 | unsigned long map_start = 0; |
| 1604 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1605 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1606 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1607 | while (low < high) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1608 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1609 | offset = p + mid * item_size; |
| 1610 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1611 | if (!kaddr || offset < map_start || |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1612 | (offset + sizeof(struct btrfs_disk_key)) > |
| 1613 | map_start + map_len) { |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1614 | |
| 1615 | err = map_private_extent_buffer(eb, offset, |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1616 | sizeof(struct btrfs_disk_key), |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1617 | &kaddr, &map_start, &map_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1618 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1619 | if (!err) { |
| 1620 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1621 | map_start); |
| 1622 | } else { |
| 1623 | read_extent_buffer(eb, &unaligned, |
| 1624 | offset, sizeof(unaligned)); |
| 1625 | tmp = &unaligned; |
| 1626 | } |
| 1627 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1628 | } else { |
| 1629 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1630 | map_start); |
| 1631 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1632 | ret = comp_keys(tmp, key); |
| 1633 | |
| 1634 | if (ret < 0) |
| 1635 | low = mid + 1; |
| 1636 | else if (ret > 0) |
| 1637 | high = mid; |
| 1638 | else { |
| 1639 | *slot = mid; |
| 1640 | return 0; |
| 1641 | } |
| 1642 | } |
| 1643 | *slot = low; |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1647 | /* |
| 1648 | * simple bin_search frontend that does the right thing for |
| 1649 | * leaves vs nodes |
| 1650 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1651 | static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1652 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1653 | { |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1654 | if (level == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1655 | return generic_bin_search(eb, |
| 1656 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1657 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1658 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1659 | slot); |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1660 | else |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1661 | return generic_bin_search(eb, |
| 1662 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1663 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1664 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1665 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1666 | } |
| 1667 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1668 | int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1669 | int level, int *slot) |
| 1670 | { |
| 1671 | return bin_search(eb, key, level, slot); |
| 1672 | } |
| 1673 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1674 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1675 | { |
| 1676 | spin_lock(&root->accounting_lock); |
| 1677 | btrfs_set_root_used(&root->root_item, |
| 1678 | btrfs_root_used(&root->root_item) + size); |
| 1679 | spin_unlock(&root->accounting_lock); |
| 1680 | } |
| 1681 | |
| 1682 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1683 | { |
| 1684 | spin_lock(&root->accounting_lock); |
| 1685 | btrfs_set_root_used(&root->root_item, |
| 1686 | btrfs_root_used(&root->root_item) - size); |
| 1687 | spin_unlock(&root->accounting_lock); |
| 1688 | } |
| 1689 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1690 | /* given a node and slot number, this reads the blocks it points to. The |
| 1691 | * extent buffer is returned with a reference taken (but unlocked). |
| 1692 | * NULL is returned on error. |
| 1693 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1694 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1695 | struct extent_buffer *parent, int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1696 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1697 | int level = btrfs_header_level(parent); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1698 | struct extent_buffer *eb; |
| 1699 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1700 | if (slot < 0) |
| 1701 | return NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1702 | if (slot >= btrfs_header_nritems(parent)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1703 | return NULL; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1704 | |
| 1705 | BUG_ON(level == 0); |
| 1706 | |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1707 | eb = read_tree_block(root, btrfs_node_blockptr(parent, slot), |
| 1708 | btrfs_level_size(root, level - 1), |
| 1709 | btrfs_node_ptr_generation(parent, slot)); |
| 1710 | if (eb && !extent_buffer_uptodate(eb)) { |
| 1711 | free_extent_buffer(eb); |
| 1712 | eb = NULL; |
| 1713 | } |
| 1714 | |
| 1715 | return eb; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1716 | } |
| 1717 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1718 | /* |
| 1719 | * node level balancing, used to make sure nodes are in proper order for |
| 1720 | * item deletion. We balance from the top down, so we have to make sure |
| 1721 | * that a deletion won't leave an node completely empty later on. |
| 1722 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1723 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1724 | struct btrfs_root *root, |
| 1725 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1726 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1727 | struct extent_buffer *right = NULL; |
| 1728 | struct extent_buffer *mid; |
| 1729 | struct extent_buffer *left = NULL; |
| 1730 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1731 | int ret = 0; |
| 1732 | int wret; |
| 1733 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1734 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1735 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1736 | |
| 1737 | if (level == 0) |
| 1738 | return 0; |
| 1739 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1740 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1741 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1742 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1743 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1744 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1745 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1746 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1747 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1748 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1749 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1750 | pslot = path->slots[level + 1]; |
| 1751 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1752 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1753 | /* |
| 1754 | * deal with the case where there is only one pointer in the root |
| 1755 | * by promoting the node below to a root |
| 1756 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1757 | if (!parent) { |
| 1758 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1759 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1760 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1761 | return 0; |
| 1762 | |
| 1763 | /* promote the child to a root */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1764 | child = read_node_slot(root, mid, 0); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1765 | if (!child) { |
| 1766 | ret = -EROFS; |
| 1767 | btrfs_std_error(root->fs_info, ret); |
| 1768 | goto enospc; |
| 1769 | } |
| 1770 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1771 | btrfs_tree_lock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1772 | btrfs_set_lock_blocking(child); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1773 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1774 | if (ret) { |
| 1775 | btrfs_tree_unlock(child); |
| 1776 | free_extent_buffer(child); |
| 1777 | goto enospc; |
| 1778 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1779 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1780 | tree_mod_log_set_root_pointer(root, child, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1781 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1782 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1783 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1784 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1785 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1786 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1787 | path->nodes[level] = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1788 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1789 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1790 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1791 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1792 | |
| 1793 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1794 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1795 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1796 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1797 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1798 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1799 | if (btrfs_header_nritems(mid) > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1800 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1801 | return 0; |
| 1802 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1803 | left = read_node_slot(root, parent, pslot - 1); |
| 1804 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1805 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1806 | btrfs_set_lock_blocking(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1807 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1808 | parent, pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1809 | if (wret) { |
| 1810 | ret = wret; |
| 1811 | goto enospc; |
| 1812 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1813 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1814 | right = read_node_slot(root, parent, pslot + 1); |
| 1815 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1816 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1817 | btrfs_set_lock_blocking(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1818 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1819 | parent, pslot + 1, &right); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1820 | if (wret) { |
| 1821 | ret = wret; |
| 1822 | goto enospc; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1827 | if (left) { |
| 1828 | orig_slot += btrfs_header_nritems(left); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1829 | wret = push_node_left(trans, root, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1830 | if (wret < 0) |
| 1831 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1832 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1833 | |
| 1834 | /* |
| 1835 | * then try to empty the right most buffer into the middle |
| 1836 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1837 | if (right) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1838 | wret = push_node_left(trans, root, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1839 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1840 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1841 | if (btrfs_header_nritems(right) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1842 | clean_tree_block(trans, root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1843 | btrfs_tree_unlock(right); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1844 | del_ptr(root, path, level + 1, pslot + 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1845 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1846 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1847 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1848 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1849 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1850 | struct btrfs_disk_key right_key; |
| 1851 | btrfs_node_key(right, &right_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1852 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1853 | pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1854 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1855 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1856 | } |
| 1857 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1858 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1859 | /* |
| 1860 | * we're not allowed to leave a node with one item in the |
| 1861 | * tree during a delete. A deletion from lower in the tree |
| 1862 | * could try to delete the only pointer in this node. |
| 1863 | * So, pull some keys from the left. |
| 1864 | * There has to be a left pointer at this point because |
| 1865 | * otherwise we would have pulled some pointers from the |
| 1866 | * right |
| 1867 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1868 | if (!left) { |
| 1869 | ret = -EROFS; |
| 1870 | btrfs_std_error(root->fs_info, ret); |
| 1871 | goto enospc; |
| 1872 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1873 | wret = balance_node_right(trans, root, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1874 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1875 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1876 | goto enospc; |
| 1877 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1878 | if (wret == 1) { |
| 1879 | wret = push_node_left(trans, root, left, mid, 1); |
| 1880 | if (wret < 0) |
| 1881 | ret = wret; |
| 1882 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1883 | BUG_ON(wret == 1); |
| 1884 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1885 | if (btrfs_header_nritems(mid) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1886 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1887 | btrfs_tree_unlock(mid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1888 | del_ptr(root, path, level + 1, pslot); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1889 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1890 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1891 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1892 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1893 | } else { |
| 1894 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1895 | struct btrfs_disk_key mid_key; |
| 1896 | btrfs_node_key(mid, &mid_key, 0); |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1897 | tree_mod_log_set_node_key(root->fs_info, parent, |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1898 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1899 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1900 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1901 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1902 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1903 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1904 | if (left) { |
| 1905 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1906 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1907 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1908 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1909 | path->slots[level + 1] -= 1; |
| 1910 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1911 | if (mid) { |
| 1912 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1913 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1914 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1915 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1916 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1917 | path->slots[level] = orig_slot; |
| 1918 | } |
| 1919 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1920 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1921 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1922 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1923 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1924 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1925 | if (right) { |
| 1926 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1927 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1928 | } |
| 1929 | if (left) { |
| 1930 | if (path->nodes[level] != left) |
| 1931 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1932 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1933 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1934 | return ret; |
| 1935 | } |
| 1936 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1937 | /* Node balancing for insertion. Here we only split or push nodes around |
| 1938 | * when they are completely full. This is also done top down, so we |
| 1939 | * have to be pessimistic. |
| 1940 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1941 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1942 | struct btrfs_root *root, |
| 1943 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1944 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1945 | struct extent_buffer *right = NULL; |
| 1946 | struct extent_buffer *mid; |
| 1947 | struct extent_buffer *left = NULL; |
| 1948 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1949 | int ret = 0; |
| 1950 | int wret; |
| 1951 | int pslot; |
| 1952 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1953 | |
| 1954 | if (level == 0) |
| 1955 | return 1; |
| 1956 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1957 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1958 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1959 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1960 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1961 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1962 | pslot = path->slots[level + 1]; |
| 1963 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1964 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1965 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1966 | return 1; |
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 | left = read_node_slot(root, parent, pslot - 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1969 | |
| 1970 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1971 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1972 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1973 | |
| 1974 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1975 | btrfs_set_lock_blocking(left); |
| 1976 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1977 | left_nr = btrfs_header_nritems(left); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1978 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1979 | wret = 1; |
| 1980 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1981 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1982 | pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1983 | if (ret) |
| 1984 | wret = 1; |
| 1985 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1986 | wret = push_node_left(trans, root, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1987 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1988 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1989 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1990 | if (wret < 0) |
| 1991 | ret = wret; |
| 1992 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1993 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1994 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1995 | btrfs_node_key(mid, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1996 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 1997 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1998 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 1999 | btrfs_mark_buffer_dirty(parent); |
| 2000 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2001 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2002 | path->slots[level + 1] -= 1; |
| 2003 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2004 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2005 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2006 | } else { |
| 2007 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2008 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2009 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2010 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2011 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2012 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2013 | return 0; |
| 2014 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2015 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2016 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2017 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2018 | right = read_node_slot(root, parent, pslot + 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2019 | |
| 2020 | /* |
| 2021 | * then try to empty the right most buffer into the middle |
| 2022 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2023 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2024 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2025 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2026 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2027 | btrfs_set_lock_blocking(right); |
| 2028 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2029 | right_nr = btrfs_header_nritems(right); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2030 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 2031 | wret = 1; |
| 2032 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2033 | ret = btrfs_cow_block(trans, root, right, |
| 2034 | parent, pslot + 1, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 2035 | &right); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2036 | if (ret) |
| 2037 | wret = 1; |
| 2038 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2039 | wret = balance_node_right(trans, root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2040 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2041 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2042 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2043 | if (wret < 0) |
| 2044 | ret = wret; |
| 2045 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2046 | struct btrfs_disk_key disk_key; |
| 2047 | |
| 2048 | btrfs_node_key(right, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2049 | tree_mod_log_set_node_key(root->fs_info, parent, |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2050 | pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2051 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2052 | btrfs_mark_buffer_dirty(parent); |
| 2053 | |
| 2054 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2055 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2056 | path->slots[level + 1] += 1; |
| 2057 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2058 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2059 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2060 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2061 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2062 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2063 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2064 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2065 | return 0; |
| 2066 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2067 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2068 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2069 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2070 | return 1; |
| 2071 | } |
| 2072 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2073 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2074 | * readahead one full node of leaves, finding things that are close |
| 2075 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2076 | */ |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2077 | static void reada_for_search(struct btrfs_root *root, |
| 2078 | struct btrfs_path *path, |
| 2079 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2080 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2081 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2082 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2083 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2084 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2085 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2086 | u64 nread = 0; |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2087 | u64 gen; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2088 | int direction = path->reada; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2089 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2090 | u32 nr; |
| 2091 | u32 blocksize; |
| 2092 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2093 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2094 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2095 | return; |
| 2096 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2097 | if (!path->nodes[level]) |
| 2098 | return; |
| 2099 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2100 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2101 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2102 | search = btrfs_node_blockptr(node, slot); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2103 | blocksize = btrfs_level_size(root, level - 1); |
| 2104 | eb = btrfs_find_tree_block(root, search, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2105 | if (eb) { |
| 2106 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2107 | return; |
| 2108 | } |
| 2109 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2110 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2111 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2112 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2113 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2114 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2115 | while (1) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2116 | if (direction < 0) { |
| 2117 | if (nr == 0) |
| 2118 | break; |
| 2119 | nr--; |
| 2120 | } else if (direction > 0) { |
| 2121 | nr++; |
| 2122 | if (nr >= nritems) |
| 2123 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2124 | } |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2125 | if (path->reada < 0 && objectid) { |
| 2126 | btrfs_node_key(node, &disk_key, nr); |
| 2127 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2128 | break; |
| 2129 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2130 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2131 | if ((search <= target && target - search <= 65536) || |
| 2132 | (search > target && search - target <= 65536)) { |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2133 | gen = btrfs_node_ptr_generation(node, nr); |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2134 | readahead_tree_block(root, search, blocksize, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2135 | nread += blocksize; |
| 2136 | } |
| 2137 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2138 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2139 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2140 | } |
| 2141 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2142 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2143 | static noinline void reada_for_balance(struct btrfs_root *root, |
| 2144 | struct btrfs_path *path, int level) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2145 | { |
| 2146 | int slot; |
| 2147 | int nritems; |
| 2148 | struct extent_buffer *parent; |
| 2149 | struct extent_buffer *eb; |
| 2150 | u64 gen; |
| 2151 | u64 block1 = 0; |
| 2152 | u64 block2 = 0; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2153 | int blocksize; |
| 2154 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2155 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2156 | if (!parent) |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2157 | return; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2158 | |
| 2159 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2160 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2161 | blocksize = btrfs_level_size(root, level); |
| 2162 | |
| 2163 | if (slot > 0) { |
| 2164 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2165 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
| 2166 | eb = btrfs_find_tree_block(root, block1, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2167 | /* |
| 2168 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2169 | * don't want to return eagain here. That will loop |
| 2170 | * forever |
| 2171 | */ |
| 2172 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2173 | block1 = 0; |
| 2174 | free_extent_buffer(eb); |
| 2175 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2176 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2177 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2178 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
| 2179 | eb = btrfs_find_tree_block(root, block2, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2180 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2181 | block2 = 0; |
| 2182 | free_extent_buffer(eb); |
| 2183 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2184 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2185 | if (block1) |
| 2186 | readahead_tree_block(root, block1, blocksize, 0); |
| 2187 | if (block2) |
| 2188 | readahead_tree_block(root, block2, blocksize, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | |
| 2192 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2193 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2194 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2195 | * operations on the tree might require changing key pointers higher up in the |
| 2196 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2197 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2198 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2199 | * the lock if the path points to the last slot in the block. This is part of |
| 2200 | * 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] | 2201 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2202 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2203 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2204 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2205 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2206 | int lowest_unlock, int min_write_lock_level, |
| 2207 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2208 | { |
| 2209 | int i; |
| 2210 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2211 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2212 | struct extent_buffer *t; |
| 2213 | |
| 2214 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2215 | if (!path->nodes[i]) |
| 2216 | break; |
| 2217 | if (!path->locks[i]) |
| 2218 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2219 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2220 | skip_level = i + 1; |
| 2221 | continue; |
| 2222 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2223 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2224 | u32 nritems; |
| 2225 | t = path->nodes[i]; |
| 2226 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2227 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2228 | skip_level = i + 1; |
| 2229 | continue; |
| 2230 | } |
| 2231 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2232 | if (skip_level < i && i >= lowest_unlock) |
| 2233 | no_skips = 1; |
| 2234 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2235 | t = path->nodes[i]; |
| 2236 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2237 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2238 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2239 | if (write_lock_level && |
| 2240 | i > min_write_lock_level && |
| 2241 | i <= *write_lock_level) { |
| 2242 | *write_lock_level = i - 1; |
| 2243 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2248 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2249 | * This releases any locks held in the path starting at level and |
| 2250 | * going all the way up to the root. |
| 2251 | * |
| 2252 | * btrfs_search_slot will keep the lock held on higher nodes in a few |
| 2253 | * corner cases, such as COW of the block at slot zero in the node. This |
| 2254 | * ignores those rules, and it should only be called when there are no |
| 2255 | * more updates to be done higher up in the tree. |
| 2256 | */ |
| 2257 | noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) |
| 2258 | { |
| 2259 | int i; |
| 2260 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2261 | if (path->keep_locks) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2262 | return; |
| 2263 | |
| 2264 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2265 | if (!path->nodes[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2266 | continue; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2267 | if (!path->locks[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2268 | continue; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2269 | btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2270 | path->locks[i] = 0; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2275 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2276 | * in cache without setting the path to blocking. If we find the block |
| 2277 | * we return zero and the path is unchanged. |
| 2278 | * |
| 2279 | * If we can't find the block, we set the path blocking and do some |
| 2280 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2281 | */ |
| 2282 | static int |
| 2283 | read_block_for_search(struct btrfs_trans_handle *trans, |
| 2284 | struct btrfs_root *root, struct btrfs_path *p, |
| 2285 | struct extent_buffer **eb_ret, int level, int slot, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2286 | struct btrfs_key *key, u64 time_seq) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2287 | { |
| 2288 | u64 blocknr; |
| 2289 | u64 gen; |
| 2290 | u32 blocksize; |
| 2291 | struct extent_buffer *b = *eb_ret; |
| 2292 | struct extent_buffer *tmp; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2293 | int ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2294 | |
| 2295 | blocknr = btrfs_node_blockptr(b, slot); |
| 2296 | gen = btrfs_node_ptr_generation(b, slot); |
| 2297 | blocksize = btrfs_level_size(root, level - 1); |
| 2298 | |
| 2299 | tmp = btrfs_find_tree_block(root, blocknr, blocksize); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2300 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2301 | /* first we do an atomic uptodate check */ |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2302 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
| 2303 | *eb_ret = tmp; |
| 2304 | return 0; |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2305 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2306 | |
| 2307 | /* the pages were up to date, but we failed |
| 2308 | * the generation number check. Do a full |
| 2309 | * read for the generation number that is correct. |
| 2310 | * We must do this without dropping locks so |
| 2311 | * we can trust our generation number |
| 2312 | */ |
| 2313 | btrfs_set_path_blocking(p); |
| 2314 | |
| 2315 | /* now we're allowed to do a blocking uptodate check */ |
| 2316 | ret = btrfs_read_buffer(tmp, gen); |
| 2317 | if (!ret) { |
| 2318 | *eb_ret = tmp; |
| 2319 | return 0; |
| 2320 | } |
| 2321 | free_extent_buffer(tmp); |
| 2322 | btrfs_release_path(p); |
| 2323 | return -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | * reduce lock contention at high levels |
| 2328 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2329 | * we read. Don't release the lock on the current |
| 2330 | * level because we need to walk this node to figure |
| 2331 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2332 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2333 | btrfs_unlock_up_safe(p, level + 1); |
| 2334 | btrfs_set_path_blocking(p); |
| 2335 | |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2336 | free_extent_buffer(tmp); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2337 | if (p->reada) |
| 2338 | reada_for_search(root, p, level, slot, key->objectid); |
| 2339 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2340 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2341 | |
| 2342 | ret = -EAGAIN; |
Yan, Zheng | 5bdd353 | 2010-05-26 11:20:30 -0400 | [diff] [blame] | 2343 | tmp = read_tree_block(root, blocknr, blocksize, 0); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2344 | if (tmp) { |
| 2345 | /* |
| 2346 | * If the read above didn't mark this buffer up to date, |
| 2347 | * it will never end up being up to date. Set ret to EIO now |
| 2348 | * and give up so that our caller doesn't loop forever |
| 2349 | * on our EAGAINs. |
| 2350 | */ |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2351 | if (!btrfs_buffer_uptodate(tmp, 0, 0)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2352 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2353 | free_extent_buffer(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2354 | } |
| 2355 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /* |
| 2359 | * helper function for btrfs_search_slot. This does all of the checks |
| 2360 | * for node-level blocks and does any balancing required based on |
| 2361 | * the ins_len. |
| 2362 | * |
| 2363 | * If no extra work was required, zero is returned. If we had to |
| 2364 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2365 | * start over |
| 2366 | */ |
| 2367 | static int |
| 2368 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2369 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2370 | struct extent_buffer *b, int level, int ins_len, |
| 2371 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2372 | { |
| 2373 | int ret; |
| 2374 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
| 2375 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { |
| 2376 | int sret; |
| 2377 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2378 | if (*write_lock_level < level + 1) { |
| 2379 | *write_lock_level = level + 1; |
| 2380 | btrfs_release_path(p); |
| 2381 | goto again; |
| 2382 | } |
| 2383 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2384 | btrfs_set_path_blocking(p); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2385 | reada_for_balance(root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2386 | sret = split_node(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2387 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2388 | |
| 2389 | BUG_ON(sret > 0); |
| 2390 | if (sret) { |
| 2391 | ret = sret; |
| 2392 | goto done; |
| 2393 | } |
| 2394 | b = p->nodes[level]; |
| 2395 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Chris Mason | cfbb930 | 2009-05-18 10:41:58 -0400 | [diff] [blame] | 2396 | BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2397 | int sret; |
| 2398 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2399 | if (*write_lock_level < level + 1) { |
| 2400 | *write_lock_level = level + 1; |
| 2401 | btrfs_release_path(p); |
| 2402 | goto again; |
| 2403 | } |
| 2404 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2405 | btrfs_set_path_blocking(p); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2406 | reada_for_balance(root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2407 | sret = balance_level(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2408 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2409 | |
| 2410 | if (sret) { |
| 2411 | ret = sret; |
| 2412 | goto done; |
| 2413 | } |
| 2414 | b = p->nodes[level]; |
| 2415 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2416 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2417 | goto again; |
| 2418 | } |
| 2419 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2420 | } |
| 2421 | return 0; |
| 2422 | |
| 2423 | again: |
| 2424 | ret = -EAGAIN; |
| 2425 | done: |
| 2426 | return ret; |
| 2427 | } |
| 2428 | |
| 2429 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2430 | * look for key in the tree. path is filled in with nodes along the way |
| 2431 | * if key is found, we return zero and you can find the item in the leaf |
| 2432 | * level of the path (level 0) |
| 2433 | * |
| 2434 | * 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] | 2435 | * be inserted, and 1 is returned. If there are other errors during the |
| 2436 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2437 | * |
| 2438 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 2439 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 2440 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2441 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2442 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 2443 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 2444 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2445 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2446 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2447 | int slot; |
| 2448 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2449 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2450 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2451 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2452 | int root_lock; |
| 2453 | /* everything at write_lock_level or lower must be write locked */ |
| 2454 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2455 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2456 | int min_write_lock_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2457 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2458 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2459 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2460 | WARN_ON(p->nodes[0] != NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2461 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2462 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2463 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2464 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2465 | /* when we are removing items, we might have to go up to level |
| 2466 | * two as we update tree pointers Make sure we keep write |
| 2467 | * for those levels as well |
| 2468 | */ |
| 2469 | write_lock_level = 2; |
| 2470 | } else if (ins_len > 0) { |
| 2471 | /* |
| 2472 | * for inserting items, make sure we have a write lock on |
| 2473 | * level 1 so we can update keys |
| 2474 | */ |
| 2475 | write_lock_level = 1; |
| 2476 | } |
| 2477 | |
| 2478 | if (!cow) |
| 2479 | write_lock_level = -1; |
| 2480 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2481 | if (cow && (p->keep_locks || p->lowest_level)) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2482 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2483 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2484 | min_write_lock_level = write_lock_level; |
| 2485 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2486 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2487 | /* |
| 2488 | * we try very hard to do read locks on the root |
| 2489 | */ |
| 2490 | root_lock = BTRFS_READ_LOCK; |
| 2491 | level = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2492 | if (p->search_commit_root) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2493 | /* |
| 2494 | * the commit roots are read only |
| 2495 | * so we always do read locks |
| 2496 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2497 | b = root->commit_root; |
| 2498 | extent_buffer_get(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2499 | level = btrfs_header_level(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2500 | if (!p->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2501 | btrfs_tree_read_lock(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2502 | } else { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2503 | if (p->skip_locking) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2504 | b = btrfs_root_node(root); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2505 | level = btrfs_header_level(b); |
| 2506 | } else { |
| 2507 | /* we don't know the level of the root node |
| 2508 | * until we actually have it read locked |
| 2509 | */ |
| 2510 | b = btrfs_read_lock_root_node(root); |
| 2511 | level = btrfs_header_level(b); |
| 2512 | if (level <= write_lock_level) { |
| 2513 | /* whoops, must trade for write lock */ |
| 2514 | btrfs_tree_read_unlock(b); |
| 2515 | free_extent_buffer(b); |
| 2516 | b = btrfs_lock_root_node(root); |
| 2517 | root_lock = BTRFS_WRITE_LOCK; |
| 2518 | |
| 2519 | /* the level might have changed, check again */ |
| 2520 | level = btrfs_header_level(b); |
| 2521 | } |
| 2522 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2523 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2524 | p->nodes[level] = b; |
| 2525 | if (!p->skip_locking) |
| 2526 | p->locks[level] = root_lock; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2527 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2528 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2529 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2530 | |
| 2531 | /* |
| 2532 | * setup the path here so we can release it under lock |
| 2533 | * contention with the cow code |
| 2534 | */ |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2535 | if (cow) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2536 | /* |
| 2537 | * if we don't really need to cow this block |
| 2538 | * then we don't want to set the path blocking, |
| 2539 | * so we test it here |
| 2540 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2541 | if (!should_cow_block(trans, root, b)) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2542 | goto cow_done; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2543 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2544 | btrfs_set_path_blocking(p); |
| 2545 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2546 | /* |
| 2547 | * must have write locks on this node and the |
| 2548 | * parent |
| 2549 | */ |
Josef Bacik | 5124e00 | 2012-11-07 13:44:13 -0500 | [diff] [blame] | 2550 | if (level > write_lock_level || |
| 2551 | (level + 1 > write_lock_level && |
| 2552 | level + 1 < BTRFS_MAX_LEVEL && |
| 2553 | p->nodes[level + 1])) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2554 | write_lock_level = level + 1; |
| 2555 | btrfs_release_path(p); |
| 2556 | goto again; |
| 2557 | } |
| 2558 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2559 | err = btrfs_cow_block(trans, root, b, |
| 2560 | p->nodes[level + 1], |
| 2561 | p->slots[level + 1], &b); |
| 2562 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2563 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2564 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2565 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2566 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2567 | cow_done: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2568 | BUG_ON(!cow && ins_len); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2569 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2570 | p->nodes[level] = b; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2571 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2572 | |
| 2573 | /* |
| 2574 | * we have a lock on b and as long as we aren't changing |
| 2575 | * the tree, there is no way to for the items in b to change. |
| 2576 | * It is safe to drop the lock on our parent before we |
| 2577 | * go through the expensive btree search on b. |
| 2578 | * |
| 2579 | * If cow is true, then we might be changing slot zero, |
| 2580 | * which may require changing the parent. So, we can't |
| 2581 | * drop the lock until after we know which slot we're |
| 2582 | * operating on. |
| 2583 | */ |
| 2584 | if (!cow) |
| 2585 | btrfs_unlock_up_safe(p, level + 1); |
| 2586 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2587 | ret = bin_search(b, key, level, &slot); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2588 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2589 | if (level != 0) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2590 | int dec = 0; |
| 2591 | if (ret && slot > 0) { |
| 2592 | dec = 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2593 | slot -= 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2594 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2595 | p->slots[level] = slot; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2596 | err = setup_nodes_for_search(trans, root, p, b, level, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2597 | ins_len, &write_lock_level); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2598 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2599 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2600 | if (err) { |
| 2601 | ret = err; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2602 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2603 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2604 | b = p->nodes[level]; |
| 2605 | slot = p->slots[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2606 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2607 | /* |
| 2608 | * slot 0 is special, if we change the key |
| 2609 | * we have to update the parent pointer |
| 2610 | * which means we must have a write lock |
| 2611 | * on the parent |
| 2612 | */ |
| 2613 | if (slot == 0 && cow && |
| 2614 | write_lock_level < level + 1) { |
| 2615 | write_lock_level = level + 1; |
| 2616 | btrfs_release_path(p); |
| 2617 | goto again; |
| 2618 | } |
| 2619 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2620 | unlock_up(p, level, lowest_unlock, |
| 2621 | min_write_lock_level, &write_lock_level); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 2622 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2623 | if (level == lowest_level) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2624 | if (dec) |
| 2625 | p->slots[level]++; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2626 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2627 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2628 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2629 | err = read_block_for_search(trans, root, p, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2630 | &b, level, slot, key, 0); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2631 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2632 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2633 | if (err) { |
| 2634 | ret = err; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2635 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2636 | } |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2637 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2638 | if (!p->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2639 | level = btrfs_header_level(b); |
| 2640 | if (level <= write_lock_level) { |
| 2641 | err = btrfs_try_tree_write_lock(b); |
| 2642 | if (!err) { |
| 2643 | btrfs_set_path_blocking(p); |
| 2644 | btrfs_tree_lock(b); |
| 2645 | btrfs_clear_path_blocking(p, b, |
| 2646 | BTRFS_WRITE_LOCK); |
| 2647 | } |
| 2648 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2649 | } else { |
| 2650 | err = btrfs_try_tree_read_lock(b); |
| 2651 | if (!err) { |
| 2652 | btrfs_set_path_blocking(p); |
| 2653 | btrfs_tree_read_lock(b); |
| 2654 | btrfs_clear_path_blocking(p, b, |
| 2655 | BTRFS_READ_LOCK); |
| 2656 | } |
| 2657 | p->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2658 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2659 | p->nodes[level] = b; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2660 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2661 | } else { |
| 2662 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2663 | if (ins_len > 0 && |
| 2664 | btrfs_leaf_free_space(root, b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2665 | if (write_lock_level < 1) { |
| 2666 | write_lock_level = 1; |
| 2667 | btrfs_release_path(p); |
| 2668 | goto again; |
| 2669 | } |
| 2670 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2671 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2672 | err = split_leaf(trans, root, key, |
| 2673 | p, ins_len, ret == 0); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2674 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2675 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2676 | BUG_ON(err > 0); |
| 2677 | if (err) { |
| 2678 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2679 | goto done; |
| 2680 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2681 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2682 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2683 | unlock_up(p, level, lowest_unlock, |
| 2684 | min_write_lock_level, &write_lock_level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2685 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2686 | } |
| 2687 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2688 | ret = 1; |
| 2689 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2690 | /* |
| 2691 | * we don't really know what they plan on doing with the path |
| 2692 | * from here on, so for now just mark it as blocking |
| 2693 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2694 | if (!p->leave_spinning) |
| 2695 | btrfs_set_path_blocking(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2696 | if (ret < 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2697 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2698 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2699 | } |
| 2700 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2701 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2702 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2703 | * current state of the tree together with the operations recorded in the tree |
| 2704 | * modification log to search for the key in a previous version of this tree, as |
| 2705 | * denoted by the time_seq parameter. |
| 2706 | * |
| 2707 | * Naturally, there is no support for insert, delete or cow operations. |
| 2708 | * |
| 2709 | * The resulting path and return value will be set up as if we called |
| 2710 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2711 | */ |
| 2712 | int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 2713 | struct btrfs_path *p, u64 time_seq) |
| 2714 | { |
| 2715 | struct extent_buffer *b; |
| 2716 | int slot; |
| 2717 | int ret; |
| 2718 | int err; |
| 2719 | int level; |
| 2720 | int lowest_unlock = 1; |
| 2721 | u8 lowest_level = 0; |
| 2722 | |
| 2723 | lowest_level = p->lowest_level; |
| 2724 | WARN_ON(p->nodes[0] != NULL); |
| 2725 | |
| 2726 | if (p->search_commit_root) { |
| 2727 | BUG_ON(time_seq); |
| 2728 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2729 | } |
| 2730 | |
| 2731 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2732 | b = get_old_root(root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2733 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2734 | p->locks[level] = BTRFS_READ_LOCK; |
| 2735 | |
| 2736 | while (b) { |
| 2737 | level = btrfs_header_level(b); |
| 2738 | p->nodes[level] = b; |
| 2739 | btrfs_clear_path_blocking(p, NULL, 0); |
| 2740 | |
| 2741 | /* |
| 2742 | * we have a lock on b and as long as we aren't changing |
| 2743 | * the tree, there is no way to for the items in b to change. |
| 2744 | * It is safe to drop the lock on our parent before we |
| 2745 | * go through the expensive btree search on b. |
| 2746 | */ |
| 2747 | btrfs_unlock_up_safe(p, level + 1); |
| 2748 | |
| 2749 | ret = bin_search(b, key, level, &slot); |
| 2750 | |
| 2751 | if (level != 0) { |
| 2752 | int dec = 0; |
| 2753 | if (ret && slot > 0) { |
| 2754 | dec = 1; |
| 2755 | slot -= 1; |
| 2756 | } |
| 2757 | p->slots[level] = slot; |
| 2758 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2759 | |
| 2760 | if (level == lowest_level) { |
| 2761 | if (dec) |
| 2762 | p->slots[level]++; |
| 2763 | goto done; |
| 2764 | } |
| 2765 | |
| 2766 | err = read_block_for_search(NULL, root, p, &b, level, |
| 2767 | slot, key, time_seq); |
| 2768 | if (err == -EAGAIN) |
| 2769 | goto again; |
| 2770 | if (err) { |
| 2771 | ret = err; |
| 2772 | goto done; |
| 2773 | } |
| 2774 | |
| 2775 | level = btrfs_header_level(b); |
| 2776 | err = btrfs_try_tree_read_lock(b); |
| 2777 | if (!err) { |
| 2778 | btrfs_set_path_blocking(p); |
| 2779 | btrfs_tree_read_lock(b); |
| 2780 | btrfs_clear_path_blocking(p, b, |
| 2781 | BTRFS_READ_LOCK); |
| 2782 | } |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 2783 | b = tree_mod_log_rewind(root->fs_info, p, b, time_seq); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 2784 | if (!b) { |
| 2785 | ret = -ENOMEM; |
| 2786 | goto done; |
| 2787 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2788 | p->locks[level] = BTRFS_READ_LOCK; |
| 2789 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2790 | } else { |
| 2791 | p->slots[level] = slot; |
| 2792 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2793 | goto done; |
| 2794 | } |
| 2795 | } |
| 2796 | ret = 1; |
| 2797 | done: |
| 2798 | if (!p->leave_spinning) |
| 2799 | btrfs_set_path_blocking(p); |
| 2800 | if (ret < 0) |
| 2801 | btrfs_release_path(p); |
| 2802 | |
| 2803 | return ret; |
| 2804 | } |
| 2805 | |
| 2806 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2807 | * helper to use instead of search slot if no exact match is needed but |
| 2808 | * instead the next or previous item should be returned. |
| 2809 | * When find_higher is true, the next higher item is returned, the next lower |
| 2810 | * otherwise. |
| 2811 | * When return_any and find_higher are both true, and no higher item is found, |
| 2812 | * return the next lower instead. |
| 2813 | * When return_any is true and find_higher is false, and no lower item is found, |
| 2814 | * return the next higher instead. |
| 2815 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 2816 | * < 0 on error |
| 2817 | */ |
| 2818 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
| 2819 | struct btrfs_key *key, struct btrfs_path *p, |
| 2820 | int find_higher, int return_any) |
| 2821 | { |
| 2822 | int ret; |
| 2823 | struct extent_buffer *leaf; |
| 2824 | |
| 2825 | again: |
| 2826 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2827 | if (ret <= 0) |
| 2828 | return ret; |
| 2829 | /* |
| 2830 | * a return value of 1 means the path is at the position where the |
| 2831 | * item should be inserted. Normally this is the next bigger item, |
| 2832 | * but in case the previous item is the last in a leaf, path points |
| 2833 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 2834 | * item. |
| 2835 | */ |
| 2836 | leaf = p->nodes[0]; |
| 2837 | |
| 2838 | if (find_higher) { |
| 2839 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2840 | ret = btrfs_next_leaf(root, p); |
| 2841 | if (ret <= 0) |
| 2842 | return ret; |
| 2843 | if (!return_any) |
| 2844 | return 1; |
| 2845 | /* |
| 2846 | * no higher item found, return the next |
| 2847 | * lower instead |
| 2848 | */ |
| 2849 | return_any = 0; |
| 2850 | find_higher = 0; |
| 2851 | btrfs_release_path(p); |
| 2852 | goto again; |
| 2853 | } |
| 2854 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2855 | if (p->slots[0] == 0) { |
| 2856 | ret = btrfs_prev_leaf(root, p); |
| 2857 | if (ret < 0) |
| 2858 | return ret; |
| 2859 | if (!ret) { |
| 2860 | p->slots[0] = btrfs_header_nritems(leaf) - 1; |
| 2861 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2862 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2863 | if (!return_any) |
| 2864 | return 1; |
| 2865 | /* |
| 2866 | * no lower item found, return the next |
| 2867 | * higher instead |
| 2868 | */ |
| 2869 | return_any = 0; |
| 2870 | find_higher = 1; |
| 2871 | btrfs_release_path(p); |
| 2872 | goto again; |
| 2873 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2874 | --p->slots[0]; |
| 2875 | } |
| 2876 | } |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
| 2880 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2881 | * adjust the pointers going up the tree, starting at level |
| 2882 | * making sure the right key of each node is points to 'key'. |
| 2883 | * This is used after shifting pointers to the left, so it stops |
| 2884 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 2885 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2886 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2887 | */ |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 2888 | 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] | 2889 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2890 | { |
| 2891 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2892 | struct extent_buffer *t; |
| 2893 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2894 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2895 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2896 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2897 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2898 | t = path->nodes[i]; |
Liu Bo | 32adf09 | 2012-10-19 12:52:15 +0000 | [diff] [blame] | 2899 | tree_mod_log_set_node_key(root->fs_info, t, tslot, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2900 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2901 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2902 | if (tslot != 0) |
| 2903 | break; |
| 2904 | } |
| 2905 | } |
| 2906 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2907 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2908 | * update item key. |
| 2909 | * |
| 2910 | * This function isn't completely safe. It's the caller's responsibility |
| 2911 | * that the new key won't break the order |
| 2912 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 2913 | 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] | 2914 | struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2915 | { |
| 2916 | struct btrfs_disk_key disk_key; |
| 2917 | struct extent_buffer *eb; |
| 2918 | int slot; |
| 2919 | |
| 2920 | eb = path->nodes[0]; |
| 2921 | slot = path->slots[0]; |
| 2922 | if (slot > 0) { |
| 2923 | btrfs_item_key(eb, &disk_key, slot - 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2924 | BUG_ON(comp_keys(&disk_key, new_key) >= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2925 | } |
| 2926 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 2927 | btrfs_item_key(eb, &disk_key, slot + 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2928 | BUG_ON(comp_keys(&disk_key, new_key) <= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 2932 | btrfs_set_item_key(eb, &disk_key, slot); |
| 2933 | btrfs_mark_buffer_dirty(eb); |
| 2934 | if (slot == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 2935 | fixup_low_keys(root, path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2939 | * 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] | 2940 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2941 | * |
| 2942 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 2943 | * 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] | 2944 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2945 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 2946 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2947 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2948 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2949 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2950 | int src_nritems; |
| 2951 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2952 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2953 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2954 | src_nritems = btrfs_header_nritems(src); |
| 2955 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2956 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2957 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 2958 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2959 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2960 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2961 | return 1; |
| 2962 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2963 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2964 | return 1; |
| 2965 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2966 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2967 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2968 | if (push_items < src_nritems) { |
| 2969 | /* leave at least 8 pointers in the node if |
| 2970 | * we aren't going to empty it |
| 2971 | */ |
| 2972 | if (src_nritems - push_items < 8) { |
| 2973 | if (push_items <= 8) |
| 2974 | return 1; |
| 2975 | push_items -= 8; |
| 2976 | } |
| 2977 | } |
| 2978 | } else |
| 2979 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2980 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2981 | 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] | 2982 | push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2983 | copy_extent_buffer(dst, src, |
| 2984 | btrfs_node_key_ptr_offset(dst_nritems), |
| 2985 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2986 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2987 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2988 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 2989 | /* |
| 2990 | * don't call tree_mod_log_eb_move here, key removal was already |
| 2991 | * fully logged by tree_mod_log_eb_copy above. |
| 2992 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2993 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 2994 | btrfs_node_key_ptr_offset(push_items), |
| 2995 | (src_nritems - push_items) * |
| 2996 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2997 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2998 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 2999 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 3000 | btrfs_mark_buffer_dirty(src); |
| 3001 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3002 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3003 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3004 | } |
| 3005 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3006 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3007 | * try to push data from one node into the next node right in the |
| 3008 | * tree. |
| 3009 | * |
| 3010 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 3011 | * error, and > 0 if there was no room in the right hand block. |
| 3012 | * |
| 3013 | * this will only push up to 1/2 the contents of the left node over |
| 3014 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3015 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 3016 | struct btrfs_root *root, |
| 3017 | struct extent_buffer *dst, |
| 3018 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3019 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3020 | int push_items = 0; |
| 3021 | int max_push; |
| 3022 | int src_nritems; |
| 3023 | int dst_nritems; |
| 3024 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3025 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3026 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3027 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3028 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3029 | src_nritems = btrfs_header_nritems(src); |
| 3030 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3031 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3032 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3033 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3034 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3035 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3036 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3037 | |
| 3038 | max_push = src_nritems / 2 + 1; |
| 3039 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3040 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3041 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3042 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3043 | if (max_push < push_items) |
| 3044 | push_items = max_push; |
| 3045 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3046 | 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] | 3047 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3048 | btrfs_node_key_ptr_offset(0), |
| 3049 | (dst_nritems) * |
| 3050 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3051 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3052 | tree_mod_log_eb_copy(root->fs_info, dst, src, 0, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3053 | src_nritems - push_items, push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3054 | copy_extent_buffer(dst, src, |
| 3055 | btrfs_node_key_ptr_offset(0), |
| 3056 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3057 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3058 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3059 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3060 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3061 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3062 | btrfs_mark_buffer_dirty(src); |
| 3063 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3064 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3065 | return ret; |
| 3066 | } |
| 3067 | |
| 3068 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3069 | * helper function to insert a new root level in the tree. |
| 3070 | * A new node is allocated, and a single item is inserted to |
| 3071 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3072 | * |
| 3073 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3074 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3075 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3076 | struct btrfs_root *root, |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3077 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3078 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3079 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3080 | struct extent_buffer *lower; |
| 3081 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3082 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3083 | struct btrfs_disk_key lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3084 | |
| 3085 | BUG_ON(path->nodes[level]); |
| 3086 | BUG_ON(path->nodes[level-1] != root->node); |
| 3087 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3088 | lower = path->nodes[level-1]; |
| 3089 | if (level == 1) |
| 3090 | btrfs_item_key(lower, &lower_key, 0); |
| 3091 | else |
| 3092 | btrfs_node_key(lower, &lower_key, 0); |
| 3093 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3094 | c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3095 | root->root_key.objectid, &lower_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3096 | level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3097 | if (IS_ERR(c)) |
| 3098 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3099 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3100 | root_add_used(root, root->nodesize); |
| 3101 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3102 | memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3103 | btrfs_set_header_nritems(c, 1); |
| 3104 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3105 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3106 | btrfs_set_header_generation(c, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3107 | btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3108 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3109 | |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame^] | 3110 | write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(c), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3111 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3112 | |
| 3113 | write_extent_buffer(c, root->fs_info->chunk_tree_uuid, |
| 3114 | (unsigned long)btrfs_header_chunk_tree_uuid(c), |
| 3115 | BTRFS_UUID_SIZE); |
| 3116 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3117 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3118 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3119 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3120 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3121 | |
| 3122 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3123 | |
| 3124 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3125 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3126 | old = root->node; |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3127 | tree_mod_log_set_root_pointer(root, c, 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3128 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3129 | |
| 3130 | /* the super has an extra ref to root->node */ |
| 3131 | free_extent_buffer(old); |
| 3132 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3133 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3134 | extent_buffer_get(c); |
| 3135 | path->nodes[level] = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 3136 | path->locks[level] = BTRFS_WRITE_LOCK; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3137 | path->slots[level] = 0; |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3141 | /* |
| 3142 | * worker function to insert a single pointer in a node. |
| 3143 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3144 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3145 | * slot and level indicate where you want the key to go, and |
| 3146 | * blocknr is the block the key points to. |
| 3147 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3148 | static void insert_ptr(struct btrfs_trans_handle *trans, |
| 3149 | struct btrfs_root *root, struct btrfs_path *path, |
| 3150 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3151 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3152 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3153 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3154 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3155 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3156 | |
| 3157 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3158 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3159 | lower = path->nodes[level]; |
| 3160 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3161 | BUG_ON(slot > nritems); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3162 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3163 | if (slot != nritems) { |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3164 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3165 | tree_mod_log_eb_move(root->fs_info, lower, slot + 1, |
| 3166 | slot, nritems - slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3167 | memmove_extent_buffer(lower, |
| 3168 | btrfs_node_key_ptr_offset(slot + 1), |
| 3169 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3170 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3171 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3172 | if (level) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3173 | ret = tree_mod_log_insert_key(root->fs_info, lower, slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 3174 | MOD_LOG_KEY_ADD, GFP_NOFS); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3175 | BUG_ON(ret < 0); |
| 3176 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3177 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3178 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3179 | WARN_ON(trans->transid == 0); |
| 3180 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3181 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3182 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3183 | } |
| 3184 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3185 | /* |
| 3186 | * split the node at the specified level in path in two. |
| 3187 | * The path is corrected to point to the appropriate node after the split |
| 3188 | * |
| 3189 | * Before splitting this tries to make some room in the node by pushing |
| 3190 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3191 | * |
| 3192 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3193 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3194 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3195 | struct btrfs_root *root, |
| 3196 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3197 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3198 | struct extent_buffer *c; |
| 3199 | struct extent_buffer *split; |
| 3200 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3201 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3202 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3203 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3204 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3205 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3206 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3207 | if (c == root->node) { |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3208 | /* |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3209 | * trying to split the root, lets make a new one |
| 3210 | * |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3211 | * tree mod log: We don't log_removal old root in |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3212 | * insert_new_root, because that root buffer will be kept as a |
| 3213 | * normal node. We are going to log removal of half of the |
| 3214 | * elements below with tree_mod_log_eb_copy. We're holding a |
| 3215 | * tree lock on the buffer, which is why we cannot race with |
| 3216 | * other tree_mod_log users. |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3217 | */ |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3218 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3219 | if (ret) |
| 3220 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3221 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3222 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3223 | c = path->nodes[level]; |
| 3224 | if (!ret && btrfs_header_nritems(c) < |
Chris Mason | c448acf | 2008-04-24 09:34:34 -0400 | [diff] [blame] | 3225 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3226 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3227 | if (ret < 0) |
| 3228 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3229 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3230 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3231 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3232 | mid = (c_nritems + 1) / 2; |
| 3233 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3234 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3235 | split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3236 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3237 | &disk_key, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3238 | if (IS_ERR(split)) |
| 3239 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3240 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3241 | root_add_used(root, root->nodesize); |
| 3242 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3243 | memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3244 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3245 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3246 | btrfs_set_header_generation(split, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3247 | btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3248 | btrfs_set_header_owner(split, root->root_key.objectid); |
| 3249 | write_extent_buffer(split, root->fs_info->fsid, |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame^] | 3250 | btrfs_header_fsid(split), BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3251 | write_extent_buffer(split, root->fs_info->chunk_tree_uuid, |
| 3252 | (unsigned long)btrfs_header_chunk_tree_uuid(split), |
| 3253 | BTRFS_UUID_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3254 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3255 | 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] | 3256 | copy_extent_buffer(split, c, |
| 3257 | btrfs_node_key_ptr_offset(0), |
| 3258 | btrfs_node_key_ptr_offset(mid), |
| 3259 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3260 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3261 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3262 | ret = 0; |
| 3263 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3264 | btrfs_mark_buffer_dirty(c); |
| 3265 | btrfs_mark_buffer_dirty(split); |
| 3266 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3267 | insert_ptr(trans, root, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3268 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3269 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3270 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3271 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3272 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3273 | free_extent_buffer(c); |
| 3274 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3275 | path->slots[level + 1] += 1; |
| 3276 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3277 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3278 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3279 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3280 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3281 | } |
| 3282 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3283 | /* |
| 3284 | * how many bytes are required to store the items in a leaf. start |
| 3285 | * and nr indicate which items in the leaf to check. This totals up the |
| 3286 | * space used both by the item structs and the item data |
| 3287 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3288 | 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] | 3289 | { |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3290 | struct btrfs_item *start_item; |
| 3291 | struct btrfs_item *end_item; |
| 3292 | struct btrfs_map_token token; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3293 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3294 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3295 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3296 | |
| 3297 | if (!nr) |
| 3298 | return 0; |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3299 | btrfs_init_map_token(&token); |
| 3300 | start_item = btrfs_item_nr(l, start); |
| 3301 | end_item = btrfs_item_nr(l, end); |
| 3302 | data_len = btrfs_token_item_offset(l, start_item, &token) + |
| 3303 | btrfs_token_item_size(l, start_item, &token); |
| 3304 | data_len = data_len - btrfs_token_item_offset(l, end_item, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3305 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3306 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3307 | return data_len; |
| 3308 | } |
| 3309 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3310 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3311 | * The space between the end of the leaf items and |
| 3312 | * the start of the leaf data. IOW, how much room |
| 3313 | * the leaf has left for both items and data |
| 3314 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3315 | noinline int btrfs_leaf_free_space(struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3316 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3317 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3318 | int nritems = btrfs_header_nritems(leaf); |
| 3319 | int ret; |
| 3320 | ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 3321 | if (ret < 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3322 | printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " |
| 3323 | "used %d nritems %d\n", |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 3324 | ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3325 | leaf_space_used(leaf, 0, nritems), nritems); |
| 3326 | } |
| 3327 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3328 | } |
| 3329 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3330 | /* |
| 3331 | * min slot controls the lowest index we're willing to push to the |
| 3332 | * right. We'll push up to and including min_slot, but no lower |
| 3333 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3334 | static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, |
| 3335 | struct btrfs_root *root, |
| 3336 | struct btrfs_path *path, |
| 3337 | int data_size, int empty, |
| 3338 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3339 | int free_space, u32 left_nritems, |
| 3340 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3341 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3342 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3343 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3344 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3345 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3346 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3347 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3348 | int push_space = 0; |
| 3349 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3350 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3351 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3352 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3353 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3354 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3355 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3356 | btrfs_init_map_token(&token); |
| 3357 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3358 | if (empty) |
| 3359 | nr = 0; |
| 3360 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3361 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3362 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3363 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3364 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3365 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3366 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3367 | i = left_nritems - 1; |
| 3368 | while (i >= nr) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3369 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3370 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3371 | if (!empty && push_items > 0) { |
| 3372 | if (path->slots[0] > i) |
| 3373 | break; |
| 3374 | if (path->slots[0] == i) { |
| 3375 | int space = btrfs_leaf_free_space(root, left); |
| 3376 | if (space + push_space * 2 > free_space) |
| 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3381 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3382 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3383 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3384 | this_item_size = btrfs_item_size(left, item); |
| 3385 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3386 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3387 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3388 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3389 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3390 | if (i == 0) |
| 3391 | break; |
| 3392 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3393 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3394 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3395 | if (push_items == 0) |
| 3396 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3397 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 3398 | WARN_ON(!empty && push_items == left_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3399 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3400 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3401 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3402 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3403 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3404 | push_space -= leaf_data_end(root, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3405 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3406 | /* make room in the right data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3407 | data_end = leaf_data_end(root, right); |
| 3408 | memmove_extent_buffer(right, |
| 3409 | btrfs_leaf_data(right) + data_end - push_space, |
| 3410 | btrfs_leaf_data(right) + data_end, |
| 3411 | BTRFS_LEAF_DATA_SIZE(root) - data_end); |
| 3412 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3413 | /* copy from the left data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3414 | copy_extent_buffer(right, left, btrfs_leaf_data(right) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3415 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3416 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 3417 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3418 | |
| 3419 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3420 | btrfs_item_nr_offset(0), |
| 3421 | right_nritems * sizeof(struct btrfs_item)); |
| 3422 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3423 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3424 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3425 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3426 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3427 | |
| 3428 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3429 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3430 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3431 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3432 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3433 | item = btrfs_item_nr(right, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3434 | push_space -= btrfs_token_item_size(right, item, &token); |
| 3435 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3436 | } |
| 3437 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3438 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3439 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3440 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3441 | if (left_nritems) |
| 3442 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3443 | else |
| 3444 | clean_tree_block(trans, root, left); |
| 3445 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3446 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3447 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3448 | btrfs_item_key(right, &disk_key, 0); |
| 3449 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3450 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3451 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3452 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3453 | if (path->slots[0] >= left_nritems) { |
| 3454 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3455 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 3456 | clean_tree_block(trans, root, path->nodes[0]); |
| 3457 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3458 | free_extent_buffer(path->nodes[0]); |
| 3459 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3460 | path->slots[1] += 1; |
| 3461 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3462 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3463 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3464 | } |
| 3465 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3466 | |
| 3467 | out_unlock: |
| 3468 | btrfs_tree_unlock(right); |
| 3469 | free_extent_buffer(right); |
| 3470 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3471 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3472 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3473 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3474 | * push some data in the path leaf to the right, trying to free up at |
| 3475 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3476 | * |
| 3477 | * returns 1 if the push failed because the other node didn't have enough |
| 3478 | * 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] | 3479 | * |
| 3480 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3481 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3482 | */ |
| 3483 | 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] | 3484 | *root, struct btrfs_path *path, |
| 3485 | int min_data_size, int data_size, |
| 3486 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3487 | { |
| 3488 | struct extent_buffer *left = path->nodes[0]; |
| 3489 | struct extent_buffer *right; |
| 3490 | struct extent_buffer *upper; |
| 3491 | int slot; |
| 3492 | int free_space; |
| 3493 | u32 left_nritems; |
| 3494 | int ret; |
| 3495 | |
| 3496 | if (!path->nodes[1]) |
| 3497 | return 1; |
| 3498 | |
| 3499 | slot = path->slots[1]; |
| 3500 | upper = path->nodes[1]; |
| 3501 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3502 | return 1; |
| 3503 | |
| 3504 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3505 | |
| 3506 | right = read_node_slot(root, upper, slot + 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3507 | if (right == NULL) |
| 3508 | return 1; |
| 3509 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3510 | btrfs_tree_lock(right); |
| 3511 | btrfs_set_lock_blocking(right); |
| 3512 | |
| 3513 | free_space = btrfs_leaf_free_space(root, right); |
| 3514 | if (free_space < data_size) |
| 3515 | goto out_unlock; |
| 3516 | |
| 3517 | /* cow and double check */ |
| 3518 | ret = btrfs_cow_block(trans, root, right, upper, |
| 3519 | slot + 1, &right); |
| 3520 | if (ret) |
| 3521 | goto out_unlock; |
| 3522 | |
| 3523 | free_space = btrfs_leaf_free_space(root, right); |
| 3524 | if (free_space < data_size) |
| 3525 | goto out_unlock; |
| 3526 | |
| 3527 | left_nritems = btrfs_header_nritems(left); |
| 3528 | if (left_nritems == 0) |
| 3529 | goto out_unlock; |
| 3530 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3531 | return __push_leaf_right(trans, root, path, min_data_size, empty, |
| 3532 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3533 | out_unlock: |
| 3534 | btrfs_tree_unlock(right); |
| 3535 | free_extent_buffer(right); |
| 3536 | return 1; |
| 3537 | } |
| 3538 | |
| 3539 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3540 | * push some data in the path leaf to the left, trying to free up at |
| 3541 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3542 | * |
| 3543 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3544 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3545 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3546 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3547 | static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, |
| 3548 | struct btrfs_root *root, |
| 3549 | struct btrfs_path *path, int data_size, |
| 3550 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3551 | int free_space, u32 right_nritems, |
| 3552 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3553 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3554 | struct btrfs_disk_key disk_key; |
| 3555 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3556 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3557 | int push_space = 0; |
| 3558 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3559 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3560 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3561 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3562 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3563 | u32 this_item_size; |
| 3564 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3565 | struct btrfs_map_token token; |
| 3566 | |
| 3567 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3568 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3569 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3570 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3571 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3572 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3573 | |
| 3574 | for (i = 0; i < nr; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3575 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3576 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3577 | if (!empty && push_items > 0) { |
| 3578 | if (path->slots[0] < i) |
| 3579 | break; |
| 3580 | if (path->slots[0] == i) { |
| 3581 | int space = btrfs_leaf_free_space(root, right); |
| 3582 | if (space + push_space * 2 > free_space) |
| 3583 | break; |
| 3584 | } |
| 3585 | } |
| 3586 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3587 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3588 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3589 | |
| 3590 | this_item_size = btrfs_item_size(right, item); |
| 3591 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3592 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3593 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3594 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3595 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3596 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3597 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3598 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3599 | ret = 1; |
| 3600 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3601 | } |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3602 | if (!empty && push_items == btrfs_header_nritems(right)) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3603 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3604 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3605 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3606 | copy_extent_buffer(left, right, |
| 3607 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3608 | btrfs_item_nr_offset(0), |
| 3609 | push_items * sizeof(struct btrfs_item)); |
| 3610 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3611 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3612 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3613 | |
| 3614 | copy_extent_buffer(left, right, btrfs_leaf_data(left) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3615 | leaf_data_end(root, left) - push_space, |
| 3616 | btrfs_leaf_data(right) + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3617 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3618 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3619 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3620 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3621 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3622 | 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] | 3623 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3624 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3625 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3626 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3627 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3628 | ioff = btrfs_token_item_offset(left, item, &token); |
| 3629 | btrfs_set_token_item_offset(left, item, |
| 3630 | ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size), |
| 3631 | &token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3632 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3633 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3634 | |
| 3635 | /* fixup right node */ |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3636 | if (push_items > right_nritems) |
| 3637 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3638 | right_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3639 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3640 | if (push_items < right_nritems) { |
| 3641 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
| 3642 | leaf_data_end(root, right); |
| 3643 | memmove_extent_buffer(right, btrfs_leaf_data(right) + |
| 3644 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3645 | btrfs_leaf_data(right) + |
| 3646 | leaf_data_end(root, right), push_space); |
| 3647 | |
| 3648 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3649 | btrfs_item_nr_offset(push_items), |
| 3650 | (btrfs_header_nritems(right) - push_items) * |
| 3651 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3652 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3653 | right_nritems -= push_items; |
| 3654 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3655 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | for (i = 0; i < right_nritems; i++) { |
| 3657 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3658 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3659 | push_space = push_space - btrfs_token_item_size(right, |
| 3660 | item, &token); |
| 3661 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3662 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3663 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3664 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3665 | if (right_nritems) |
| 3666 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3667 | else |
| 3668 | clean_tree_block(trans, root, right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3669 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3670 | btrfs_item_key(right, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 3671 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3672 | |
| 3673 | /* then fixup the leaf pointer in the path */ |
| 3674 | if (path->slots[0] < push_items) { |
| 3675 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3676 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3677 | free_extent_buffer(path->nodes[0]); |
| 3678 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3679 | path->slots[1] -= 1; |
| 3680 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3681 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3682 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3683 | path->slots[0] -= push_items; |
| 3684 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3685 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3686 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3687 | out: |
| 3688 | btrfs_tree_unlock(left); |
| 3689 | free_extent_buffer(left); |
| 3690 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3691 | } |
| 3692 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3693 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3694 | * push some data in the path leaf to the left, trying to free up at |
| 3695 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3696 | * |
| 3697 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3698 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3699 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3700 | */ |
| 3701 | 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] | 3702 | *root, struct btrfs_path *path, int min_data_size, |
| 3703 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3704 | { |
| 3705 | struct extent_buffer *right = path->nodes[0]; |
| 3706 | struct extent_buffer *left; |
| 3707 | int slot; |
| 3708 | int free_space; |
| 3709 | u32 right_nritems; |
| 3710 | int ret = 0; |
| 3711 | |
| 3712 | slot = path->slots[1]; |
| 3713 | if (slot == 0) |
| 3714 | return 1; |
| 3715 | if (!path->nodes[1]) |
| 3716 | return 1; |
| 3717 | |
| 3718 | right_nritems = btrfs_header_nritems(right); |
| 3719 | if (right_nritems == 0) |
| 3720 | return 1; |
| 3721 | |
| 3722 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3723 | |
| 3724 | left = read_node_slot(root, path->nodes[1], slot - 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3725 | if (left == NULL) |
| 3726 | return 1; |
| 3727 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3728 | btrfs_tree_lock(left); |
| 3729 | btrfs_set_lock_blocking(left); |
| 3730 | |
| 3731 | free_space = btrfs_leaf_free_space(root, left); |
| 3732 | if (free_space < data_size) { |
| 3733 | ret = 1; |
| 3734 | goto out; |
| 3735 | } |
| 3736 | |
| 3737 | /* cow and double check */ |
| 3738 | ret = btrfs_cow_block(trans, root, left, |
| 3739 | path->nodes[1], slot - 1, &left); |
| 3740 | if (ret) { |
| 3741 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3742 | if (ret == -ENOSPC) |
| 3743 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3744 | goto out; |
| 3745 | } |
| 3746 | |
| 3747 | free_space = btrfs_leaf_free_space(root, left); |
| 3748 | if (free_space < data_size) { |
| 3749 | ret = 1; |
| 3750 | goto out; |
| 3751 | } |
| 3752 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3753 | return __push_leaf_left(trans, root, path, min_data_size, |
| 3754 | empty, left, free_space, right_nritems, |
| 3755 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3756 | out: |
| 3757 | btrfs_tree_unlock(left); |
| 3758 | free_extent_buffer(left); |
| 3759 | return ret; |
| 3760 | } |
| 3761 | |
| 3762 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3763 | * split the path's leaf in two, making sure there is at least data_size |
| 3764 | * available for the resulting leaf level of the path. |
| 3765 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3766 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
| 3767 | struct btrfs_root *root, |
| 3768 | struct btrfs_path *path, |
| 3769 | struct extent_buffer *l, |
| 3770 | struct extent_buffer *right, |
| 3771 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3772 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3773 | int data_copy_size; |
| 3774 | int rt_data_off; |
| 3775 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3776 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3777 | struct btrfs_map_token token; |
| 3778 | |
| 3779 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3780 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3781 | nritems = nritems - mid; |
| 3782 | btrfs_set_header_nritems(right, nritems); |
| 3783 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); |
| 3784 | |
| 3785 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 3786 | btrfs_item_nr_offset(mid), |
| 3787 | nritems * sizeof(struct btrfs_item)); |
| 3788 | |
| 3789 | copy_extent_buffer(right, l, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3790 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 3791 | data_copy_size, btrfs_leaf_data(l) + |
| 3792 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3793 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3794 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 3795 | btrfs_item_end_nr(l, mid); |
| 3796 | |
| 3797 | for (i = 0; i < nritems; i++) { |
| 3798 | struct btrfs_item *item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3799 | u32 ioff; |
| 3800 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3801 | ioff = btrfs_token_item_offset(right, item, &token); |
| 3802 | btrfs_set_token_item_offset(right, item, |
| 3803 | ioff + rt_data_off, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3804 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3805 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3806 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3807 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3808 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3809 | path->slots[1] + 1, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3810 | |
| 3811 | btrfs_mark_buffer_dirty(right); |
| 3812 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3813 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3814 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3815 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3816 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3817 | free_extent_buffer(path->nodes[0]); |
| 3818 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3819 | path->slots[0] -= mid; |
| 3820 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3821 | } else { |
| 3822 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3823 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3824 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3825 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3826 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3830 | * double splits happen when we need to insert a big item in the middle |
| 3831 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 3832 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 3833 | * A B C |
| 3834 | * |
| 3835 | * We avoid this by trying to push the items on either side of our target |
| 3836 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 3837 | * completely. |
| 3838 | */ |
| 3839 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 3840 | struct btrfs_root *root, |
| 3841 | struct btrfs_path *path, |
| 3842 | int data_size) |
| 3843 | { |
| 3844 | int ret; |
| 3845 | int progress = 0; |
| 3846 | int slot; |
| 3847 | u32 nritems; |
| 3848 | |
| 3849 | slot = path->slots[0]; |
| 3850 | |
| 3851 | /* |
| 3852 | * try to push all the items after our slot into the |
| 3853 | * right leaf |
| 3854 | */ |
| 3855 | ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); |
| 3856 | if (ret < 0) |
| 3857 | return ret; |
| 3858 | |
| 3859 | if (ret == 0) |
| 3860 | progress++; |
| 3861 | |
| 3862 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 3863 | /* |
| 3864 | * our goal is to get our slot at the start or end of a leaf. If |
| 3865 | * we've done so we're done |
| 3866 | */ |
| 3867 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 3868 | return 0; |
| 3869 | |
| 3870 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 3871 | return 0; |
| 3872 | |
| 3873 | /* try to push all the items before our slot into the next leaf */ |
| 3874 | slot = path->slots[0]; |
| 3875 | ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); |
| 3876 | if (ret < 0) |
| 3877 | return ret; |
| 3878 | |
| 3879 | if (ret == 0) |
| 3880 | progress++; |
| 3881 | |
| 3882 | if (progress) |
| 3883 | return 0; |
| 3884 | return 1; |
| 3885 | } |
| 3886 | |
| 3887 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3888 | * split the path's leaf in two, making sure there is at least data_size |
| 3889 | * available for the resulting leaf level of the path. |
| 3890 | * |
| 3891 | * returns 0 if all went well and < 0 on failure. |
| 3892 | */ |
| 3893 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 3894 | struct btrfs_root *root, |
| 3895 | struct btrfs_key *ins_key, |
| 3896 | struct btrfs_path *path, int data_size, |
| 3897 | int extend) |
| 3898 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3899 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3900 | struct extent_buffer *l; |
| 3901 | u32 nritems; |
| 3902 | int mid; |
| 3903 | int slot; |
| 3904 | struct extent_buffer *right; |
| 3905 | int ret = 0; |
| 3906 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3907 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3908 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3909 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3910 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 3911 | l = path->nodes[0]; |
| 3912 | slot = path->slots[0]; |
| 3913 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
| 3914 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) |
| 3915 | return -EOVERFLOW; |
| 3916 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3917 | /* first try to make some room by pushing left and right */ |
Liu Bo | 33157e0 | 2013-05-22 12:07:06 +0000 | [diff] [blame] | 3918 | if (data_size && path->nodes[1]) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3919 | wret = push_leaf_right(trans, root, path, data_size, |
| 3920 | data_size, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3921 | if (wret < 0) |
| 3922 | return wret; |
| 3923 | if (wret) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3924 | wret = push_leaf_left(trans, root, path, data_size, |
| 3925 | data_size, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3926 | if (wret < 0) |
| 3927 | return wret; |
| 3928 | } |
| 3929 | l = path->nodes[0]; |
| 3930 | |
| 3931 | /* did the pushes work? */ |
| 3932 | if (btrfs_leaf_free_space(root, l) >= data_size) |
| 3933 | return 0; |
| 3934 | } |
| 3935 | |
| 3936 | if (!path->nodes[1]) { |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3937 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3938 | if (ret) |
| 3939 | return ret; |
| 3940 | } |
| 3941 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3942 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3943 | l = path->nodes[0]; |
| 3944 | slot = path->slots[0]; |
| 3945 | nritems = btrfs_header_nritems(l); |
| 3946 | mid = (nritems + 1) / 2; |
| 3947 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3948 | if (mid <= slot) { |
| 3949 | if (nritems == 1 || |
| 3950 | leaf_space_used(l, mid, nritems - mid) + data_size > |
| 3951 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3952 | if (slot >= nritems) { |
| 3953 | split = 0; |
| 3954 | } else { |
| 3955 | mid = slot; |
| 3956 | if (mid != nritems && |
| 3957 | leaf_space_used(l, mid, nritems - mid) + |
| 3958 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3959 | if (data_size && !tried_avoid_double) |
| 3960 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3961 | split = 2; |
| 3962 | } |
| 3963 | } |
| 3964 | } |
| 3965 | } else { |
| 3966 | if (leaf_space_used(l, 0, mid) + data_size > |
| 3967 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3968 | if (!extend && data_size && slot == 0) { |
| 3969 | split = 0; |
| 3970 | } else if ((extend || !data_size) && slot == 0) { |
| 3971 | mid = 1; |
| 3972 | } else { |
| 3973 | mid = slot; |
| 3974 | if (mid != nritems && |
| 3975 | leaf_space_used(l, mid, nritems - mid) + |
| 3976 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3977 | if (data_size && !tried_avoid_double) |
| 3978 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3979 | split = 2 ; |
| 3980 | } |
| 3981 | } |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | if (split == 0) |
| 3986 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 3987 | else |
| 3988 | btrfs_item_key(l, &disk_key, mid); |
| 3989 | |
| 3990 | right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3991 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3992 | &disk_key, 0, l->start, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3993 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3994 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3995 | |
| 3996 | root_add_used(root, root->leafsize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3997 | |
| 3998 | memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); |
| 3999 | btrfs_set_header_bytenr(right, right->start); |
| 4000 | btrfs_set_header_generation(right, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4001 | btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4002 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 4003 | btrfs_set_header_level(right, 0); |
| 4004 | write_extent_buffer(right, root->fs_info->fsid, |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame^] | 4005 | btrfs_header_fsid(right), BTRFS_FSID_SIZE); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4006 | |
| 4007 | write_extent_buffer(right, root->fs_info->chunk_tree_uuid, |
| 4008 | (unsigned long)btrfs_header_chunk_tree_uuid(right), |
| 4009 | BTRFS_UUID_SIZE); |
| 4010 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4011 | if (split == 0) { |
| 4012 | if (mid <= slot) { |
| 4013 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4014 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4015 | path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4016 | btrfs_tree_unlock(path->nodes[0]); |
| 4017 | free_extent_buffer(path->nodes[0]); |
| 4018 | path->nodes[0] = right; |
| 4019 | path->slots[0] = 0; |
| 4020 | path->slots[1] += 1; |
| 4021 | } else { |
| 4022 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4023 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4024 | path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4025 | btrfs_tree_unlock(path->nodes[0]); |
| 4026 | free_extent_buffer(path->nodes[0]); |
| 4027 | path->nodes[0] = right; |
| 4028 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4029 | if (path->slots[1] == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4030 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4031 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4032 | btrfs_mark_buffer_dirty(right); |
| 4033 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4034 | } |
| 4035 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4036 | copy_for_split(trans, root, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4037 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4038 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4039 | BUG_ON(num_doubles != 0); |
| 4040 | num_doubles++; |
| 4041 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4042 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4043 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4044 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4045 | |
| 4046 | push_for_double: |
| 4047 | push_for_double_split(trans, root, path, data_size); |
| 4048 | tried_avoid_double = 1; |
| 4049 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 4050 | return 0; |
| 4051 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4052 | } |
| 4053 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4054 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4055 | struct btrfs_root *root, |
| 4056 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4057 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4058 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4059 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4060 | struct btrfs_file_extent_item *fi; |
| 4061 | u64 extent_len = 0; |
| 4062 | u32 item_size; |
| 4063 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4064 | |
| 4065 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4066 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4067 | |
| 4068 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4069 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4070 | |
| 4071 | if (btrfs_leaf_free_space(root, leaf) >= ins_len) |
| 4072 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4073 | |
| 4074 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4075 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4076 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4077 | struct btrfs_file_extent_item); |
| 4078 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4079 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4080 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4081 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4082 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4083 | path->search_for_split = 1; |
| 4084 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4085 | path->search_for_split = 0; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4086 | if (ret < 0) |
| 4087 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4088 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4089 | ret = -EAGAIN; |
| 4090 | leaf = path->nodes[0]; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4091 | /* if our item isn't there or got smaller, return now */ |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4092 | if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
| 4093 | goto err; |
| 4094 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4095 | /* the leaf has changed, it now has room. return now */ |
| 4096 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) |
| 4097 | goto err; |
| 4098 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4099 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4100 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4101 | struct btrfs_file_extent_item); |
| 4102 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4103 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4104 | } |
| 4105 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4106 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4107 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4108 | if (ret) |
| 4109 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4110 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4111 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4112 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4113 | return 0; |
| 4114 | err: |
| 4115 | path->keep_locks = 0; |
| 4116 | return ret; |
| 4117 | } |
| 4118 | |
| 4119 | static noinline int split_item(struct btrfs_trans_handle *trans, |
| 4120 | struct btrfs_root *root, |
| 4121 | struct btrfs_path *path, |
| 4122 | struct btrfs_key *new_key, |
| 4123 | unsigned long split_offset) |
| 4124 | { |
| 4125 | struct extent_buffer *leaf; |
| 4126 | struct btrfs_item *item; |
| 4127 | struct btrfs_item *new_item; |
| 4128 | int slot; |
| 4129 | char *buf; |
| 4130 | u32 nritems; |
| 4131 | u32 item_size; |
| 4132 | u32 orig_offset; |
| 4133 | struct btrfs_disk_key disk_key; |
| 4134 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4135 | leaf = path->nodes[0]; |
| 4136 | BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); |
| 4137 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4138 | btrfs_set_path_blocking(path); |
| 4139 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4140 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 4141 | orig_offset = btrfs_item_offset(leaf, item); |
| 4142 | item_size = btrfs_item_size(leaf, item); |
| 4143 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4144 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4145 | if (!buf) |
| 4146 | return -ENOMEM; |
| 4147 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4148 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4149 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4150 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4151 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4152 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4153 | if (slot != nritems) { |
| 4154 | /* shift the items */ |
| 4155 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4156 | btrfs_item_nr_offset(slot), |
| 4157 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4161 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4162 | |
| 4163 | new_item = btrfs_item_nr(leaf, slot); |
| 4164 | |
| 4165 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4166 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4167 | |
| 4168 | btrfs_set_item_offset(leaf, item, |
| 4169 | orig_offset + item_size - split_offset); |
| 4170 | btrfs_set_item_size(leaf, item, split_offset); |
| 4171 | |
| 4172 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4173 | |
| 4174 | /* write the data for the start of the original item */ |
| 4175 | write_extent_buffer(leaf, buf, |
| 4176 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4177 | split_offset); |
| 4178 | |
| 4179 | /* write the data for the new item */ |
| 4180 | write_extent_buffer(leaf, buf + split_offset, |
| 4181 | btrfs_item_ptr_offset(leaf, slot), |
| 4182 | item_size - split_offset); |
| 4183 | btrfs_mark_buffer_dirty(leaf); |
| 4184 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4185 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4186 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
| 4189 | |
| 4190 | /* |
| 4191 | * This function splits a single item into two items, |
| 4192 | * giving 'new_key' to the new item and splitting the |
| 4193 | * old one at split_offset (from the start of the item). |
| 4194 | * |
| 4195 | * The path may be released by this operation. After |
| 4196 | * the split, the path is pointing to the old item. The |
| 4197 | * new item is going to be in the same node as the old one. |
| 4198 | * |
| 4199 | * Note, the item being split must be smaller enough to live alone on |
| 4200 | * a tree block with room for one extra struct btrfs_item |
| 4201 | * |
| 4202 | * This allows us to split the item in place, keeping a lock on the |
| 4203 | * leaf the entire time. |
| 4204 | */ |
| 4205 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4206 | struct btrfs_root *root, |
| 4207 | struct btrfs_path *path, |
| 4208 | struct btrfs_key *new_key, |
| 4209 | unsigned long split_offset) |
| 4210 | { |
| 4211 | int ret; |
| 4212 | ret = setup_leaf_for_split(trans, root, path, |
| 4213 | sizeof(struct btrfs_item)); |
| 4214 | if (ret) |
| 4215 | return ret; |
| 4216 | |
| 4217 | ret = split_item(trans, root, path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4218 | return ret; |
| 4219 | } |
| 4220 | |
| 4221 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4222 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4223 | * It guarantees both items live in the same tree leaf and the new item |
| 4224 | * is contiguous with the original item. |
| 4225 | * |
| 4226 | * This allows us to split file extent in place, keeping a lock on the |
| 4227 | * leaf the entire time. |
| 4228 | */ |
| 4229 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4230 | struct btrfs_root *root, |
| 4231 | struct btrfs_path *path, |
| 4232 | struct btrfs_key *new_key) |
| 4233 | { |
| 4234 | struct extent_buffer *leaf; |
| 4235 | int ret; |
| 4236 | u32 item_size; |
| 4237 | |
| 4238 | leaf = path->nodes[0]; |
| 4239 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4240 | ret = setup_leaf_for_split(trans, root, path, |
| 4241 | item_size + sizeof(struct btrfs_item)); |
| 4242 | if (ret) |
| 4243 | return ret; |
| 4244 | |
| 4245 | path->slots[0]++; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4246 | setup_items_for_insert(root, path, new_key, &item_size, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4247 | item_size, item_size + |
| 4248 | sizeof(struct btrfs_item), 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4249 | leaf = path->nodes[0]; |
| 4250 | memcpy_extent_buffer(leaf, |
| 4251 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4252 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4253 | item_size); |
| 4254 | return 0; |
| 4255 | } |
| 4256 | |
| 4257 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4258 | * make the item pointed to by the path smaller. new_size indicates |
| 4259 | * how small to make it, and from_end tells us if we just chop bytes |
| 4260 | * off the end of the item or if we shift the item to chop bytes off |
| 4261 | * the front. |
| 4262 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4263 | void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4264 | u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4265 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4266 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4267 | struct extent_buffer *leaf; |
| 4268 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4269 | u32 nritems; |
| 4270 | unsigned int data_end; |
| 4271 | unsigned int old_data_start; |
| 4272 | unsigned int old_size; |
| 4273 | unsigned int size_diff; |
| 4274 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4275 | struct btrfs_map_token token; |
| 4276 | |
| 4277 | btrfs_init_map_token(&token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4278 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4279 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4280 | slot = path->slots[0]; |
| 4281 | |
| 4282 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4283 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4284 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4285 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4286 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4287 | data_end = leaf_data_end(root, leaf); |
| 4288 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4289 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4290 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4291 | size_diff = old_size - new_size; |
| 4292 | |
| 4293 | BUG_ON(slot < 0); |
| 4294 | BUG_ON(slot >= nritems); |
| 4295 | |
| 4296 | /* |
| 4297 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4298 | */ |
| 4299 | /* first correct the data pointers */ |
| 4300 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4301 | u32 ioff; |
| 4302 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4303 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4304 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4305 | btrfs_set_token_item_offset(leaf, item, |
| 4306 | ioff + size_diff, &token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4307 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4308 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4309 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4310 | if (from_end) { |
| 4311 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4312 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4313 | data_end, old_data_start + new_size - data_end); |
| 4314 | } else { |
| 4315 | struct btrfs_disk_key disk_key; |
| 4316 | u64 offset; |
| 4317 | |
| 4318 | btrfs_item_key(leaf, &disk_key, slot); |
| 4319 | |
| 4320 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4321 | unsigned long ptr; |
| 4322 | struct btrfs_file_extent_item *fi; |
| 4323 | |
| 4324 | fi = btrfs_item_ptr(leaf, slot, |
| 4325 | struct btrfs_file_extent_item); |
| 4326 | fi = (struct btrfs_file_extent_item *)( |
| 4327 | (unsigned long)fi - size_diff); |
| 4328 | |
| 4329 | if (btrfs_file_extent_type(leaf, fi) == |
| 4330 | BTRFS_FILE_EXTENT_INLINE) { |
| 4331 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4332 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4333 | (unsigned long)fi, |
| 4334 | offsetof(struct btrfs_file_extent_item, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4335 | disk_bytenr)); |
| 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4340 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4341 | data_end, old_data_start - data_end); |
| 4342 | |
| 4343 | offset = btrfs_disk_key_offset(&disk_key); |
| 4344 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4345 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4346 | if (slot == 0) |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4347 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4348 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4349 | |
| 4350 | item = btrfs_item_nr(leaf, slot); |
| 4351 | btrfs_set_item_size(leaf, item, new_size); |
| 4352 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4353 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4354 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4355 | btrfs_print_leaf(root, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4356 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4357 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4358 | } |
| 4359 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4360 | /* |
Stefan Behrens | 8f69dbd | 2013-05-07 10:23:30 +0000 | [diff] [blame] | 4361 | * 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] | 4362 | */ |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 4363 | void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4364 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4365 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4366 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4367 | struct extent_buffer *leaf; |
| 4368 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4369 | u32 nritems; |
| 4370 | unsigned int data_end; |
| 4371 | unsigned int old_data; |
| 4372 | unsigned int old_size; |
| 4373 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4374 | struct btrfs_map_token token; |
| 4375 | |
| 4376 | btrfs_init_map_token(&token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4377 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4378 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4379 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4380 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4381 | data_end = leaf_data_end(root, leaf); |
| 4382 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4383 | if (btrfs_leaf_free_space(root, leaf) < data_size) { |
| 4384 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4385 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4386 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4387 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4388 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4389 | |
| 4390 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4391 | if (slot >= nritems) { |
| 4392 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4393 | printk(KERN_CRIT "slot %d too large, nritems %d\n", |
| 4394 | slot, nritems); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4395 | BUG_ON(1); |
| 4396 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4397 | |
| 4398 | /* |
| 4399 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4400 | */ |
| 4401 | /* first correct the data pointers */ |
| 4402 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4403 | u32 ioff; |
| 4404 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4405 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4406 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4407 | btrfs_set_token_item_offset(leaf, item, |
| 4408 | ioff - data_size, &token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4409 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4410 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4411 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4412 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4413 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 4414 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4415 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4416 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4417 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4418 | item = btrfs_item_nr(leaf, slot); |
| 4419 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4420 | btrfs_mark_buffer_dirty(leaf); |
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 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4423 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4424 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4425 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4426 | } |
| 4427 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4428 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4429 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
| 4430 | * to save stack depth by doing the bulk of the work in a function |
| 4431 | * that doesn't call btrfs_search_slot |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4432 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4433 | 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] | 4434 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4435 | u32 total_data, u32 total_size, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4436 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4437 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4438 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4439 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4440 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4441 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4442 | struct extent_buffer *leaf; |
| 4443 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4444 | struct btrfs_map_token token; |
| 4445 | |
| 4446 | btrfs_init_map_token(&token); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4447 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4448 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4449 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4450 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4451 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4452 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4453 | |
Chris Mason | f25956c | 2008-09-12 15:32:53 -0400 | [diff] [blame] | 4454 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4455 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4456 | printk(KERN_CRIT "not enough freespace need %u have %d\n", |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4457 | total_size, btrfs_leaf_free_space(root, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4458 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4459 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4460 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4461 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4462 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4463 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4464 | if (old_data < data_end) { |
| 4465 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4466 | printk(KERN_CRIT "slot %d old_data %d data_end %d\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4467 | slot, old_data, data_end); |
| 4468 | BUG_ON(1); |
| 4469 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4470 | /* |
| 4471 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4472 | */ |
| 4473 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4474 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4475 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4476 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4477 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4478 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4479 | btrfs_set_token_item_offset(leaf, item, |
| 4480 | ioff - total_data, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4481 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4482 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4483 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4484 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4485 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4486 | |
| 4487 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4488 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4489 | data_end - total_data, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4490 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4491 | data_end = old_data; |
| 4492 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4493 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4494 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4495 | for (i = 0; i < nr; i++) { |
| 4496 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4497 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
| 4498 | item = btrfs_item_nr(leaf, slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4499 | btrfs_set_token_item_offset(leaf, item, |
| 4500 | data_end - data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4501 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4502 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4503 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4504 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4505 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4506 | |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4507 | if (slot == 0) { |
| 4508 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4509 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4510 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4511 | btrfs_unlock_up_safe(path, 1); |
| 4512 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4513 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4514 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4515 | btrfs_print_leaf(root, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4516 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4517 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | /* |
| 4521 | * Given a key and some data, insert items into the tree. |
| 4522 | * This does all the path init required, making room in the tree if needed. |
| 4523 | */ |
| 4524 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4525 | struct btrfs_root *root, |
| 4526 | struct btrfs_path *path, |
| 4527 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4528 | int nr) |
| 4529 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4530 | int ret = 0; |
| 4531 | int slot; |
| 4532 | int i; |
| 4533 | u32 total_size = 0; |
| 4534 | u32 total_data = 0; |
| 4535 | |
| 4536 | for (i = 0; i < nr; i++) |
| 4537 | total_data += data_size[i]; |
| 4538 | |
| 4539 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4540 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4541 | if (ret == 0) |
| 4542 | return -EEXIST; |
| 4543 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4544 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4545 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4546 | slot = path->slots[0]; |
| 4547 | BUG_ON(slot < 0); |
| 4548 | |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4549 | setup_items_for_insert(root, path, cpu_key, data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4550 | total_data, total_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4551 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | /* |
| 4555 | * Given a key and some data, insert an item into the tree. |
| 4556 | * This does all the path init required, making room in the tree if needed. |
| 4557 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4558 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 4559 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 4560 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4561 | { |
| 4562 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4563 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4564 | struct extent_buffer *leaf; |
| 4565 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4566 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4567 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4568 | if (!path) |
| 4569 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4570 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4571 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4572 | leaf = path->nodes[0]; |
| 4573 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4574 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4575 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4576 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4577 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4578 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4579 | } |
| 4580 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4581 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4582 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4583 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4584 | * the tree should have been previously balanced so the deletion does not |
| 4585 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4586 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4587 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 4588 | int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4589 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4590 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4591 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4592 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4593 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4594 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4595 | if (slot != nritems - 1) { |
Liu Bo | 0e411ec | 2012-10-19 09:50:54 +0000 | [diff] [blame] | 4596 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4597 | tree_mod_log_eb_move(root->fs_info, parent, slot, |
| 4598 | slot + 1, nritems - slot - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4599 | memmove_extent_buffer(parent, |
| 4600 | btrfs_node_key_ptr_offset(slot), |
| 4601 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4602 | sizeof(struct btrfs_key_ptr) * |
| 4603 | (nritems - slot - 1)); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4604 | } else if (level) { |
| 4605 | ret = tree_mod_log_insert_key(root->fs_info, parent, slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 4606 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4607 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4608 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4609 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4610 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4611 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4612 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4613 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4614 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4615 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4616 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4617 | struct btrfs_disk_key disk_key; |
| 4618 | |
| 4619 | btrfs_node_key(parent, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4620 | fixup_low_keys(root, path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4621 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4622 | btrfs_mark_buffer_dirty(parent); |
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 | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4626 | * 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] | 4627 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4628 | * |
| 4629 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4630 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4631 | * |
| 4632 | * The path must have already been setup for deleting the leaf, including |
| 4633 | * all the proper balancing. path->nodes[1] must be locked. |
| 4634 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4635 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4636 | struct btrfs_root *root, |
| 4637 | struct btrfs_path *path, |
| 4638 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4639 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4640 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4641 | del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4642 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4643 | /* |
| 4644 | * btrfs_free_extent is expensive, we want to make sure we |
| 4645 | * aren't holding any locks when we call it |
| 4646 | */ |
| 4647 | btrfs_unlock_up_safe(path, 0); |
| 4648 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4649 | root_sub_used(root, leaf->len); |
| 4650 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4651 | extent_buffer_get(leaf); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4652 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4653 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4654 | } |
| 4655 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4656 | * delete the item at the leaf level in path. If that empties |
| 4657 | * the leaf, remove it from the tree |
| 4658 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4659 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4660 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4661 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4662 | struct extent_buffer *leaf; |
| 4663 | struct btrfs_item *item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4664 | int last_off; |
| 4665 | int dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4666 | int ret = 0; |
| 4667 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4668 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4669 | u32 nritems; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4670 | struct btrfs_map_token token; |
| 4671 | |
| 4672 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4673 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4674 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4675 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4676 | |
| 4677 | for (i = 0; i < nr; i++) |
| 4678 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4679 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4680 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4681 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4682 | if (slot + nr != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4683 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4684 | |
| 4685 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4686 | data_end + dsize, |
| 4687 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4688 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4690 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4691 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4692 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4693 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4694 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4695 | btrfs_set_token_item_offset(leaf, item, |
| 4696 | ioff + dsize, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4697 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4698 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4699 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4700 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4701 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4702 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4703 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4704 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4705 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4706 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4707 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4708 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4709 | if (leaf == root->node) { |
| 4710 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4711 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4712 | btrfs_set_path_blocking(path); |
| 4713 | clean_tree_block(trans, root, leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4714 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4715 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4716 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4717 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4718 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4719 | struct btrfs_disk_key disk_key; |
| 4720 | |
| 4721 | btrfs_item_key(leaf, &disk_key, 0); |
Tsutomu Itoh | d6a0a12 | 2013-04-16 05:18:02 +0000 | [diff] [blame] | 4722 | fixup_low_keys(root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4723 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4724 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4725 | /* delete the leaf if it is mostly empty */ |
Yan Zheng | d717aa1 | 2009-07-24 12:42:46 -0400 | [diff] [blame] | 4726 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4727 | /* push_leaf_left fixes the path. |
| 4728 | * make sure the path still points to our leaf |
| 4729 | * for possible call to del_ptr below |
| 4730 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 4731 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4732 | extent_buffer_get(leaf); |
| 4733 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4734 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4735 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 4736 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4737 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4738 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4739 | |
| 4740 | if (path->nodes[0] == leaf && |
| 4741 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4742 | wret = push_leaf_right(trans, root, path, 1, |
| 4743 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4744 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4745 | ret = wret; |
| 4746 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4747 | |
| 4748 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4749 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4750 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4751 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4752 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4753 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4754 | /* if we're still in the path, make sure |
| 4755 | * we're dirty. Otherwise, one of the |
| 4756 | * push_leaf functions must have already |
| 4757 | * dirtied this buffer |
| 4758 | */ |
| 4759 | if (path->nodes[0] == leaf) |
| 4760 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4761 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4762 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 4763 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4764 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4765 | } |
| 4766 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4767 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4768 | } |
| 4769 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 4770 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4771 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4772 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 4773 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4774 | * |
| 4775 | * This may release the path, and so you may lose any locks held at the |
| 4776 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4777 | */ |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 4778 | 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] | 4779 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4780 | struct btrfs_key key; |
| 4781 | struct btrfs_disk_key found_key; |
| 4782 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4783 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4784 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4785 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4786 | if (key.offset > 0) |
| 4787 | key.offset--; |
| 4788 | else if (key.type > 0) |
| 4789 | key.type--; |
| 4790 | else if (key.objectid > 0) |
| 4791 | key.objectid--; |
| 4792 | else |
| 4793 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4794 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4795 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4796 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4797 | if (ret < 0) |
| 4798 | return ret; |
| 4799 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 4800 | ret = comp_keys(&found_key, &key); |
| 4801 | if (ret < 0) |
| 4802 | return 0; |
| 4803 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4804 | } |
| 4805 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4806 | /* |
| 4807 | * 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] | 4808 | * for nodes or leaves that are have a minimum transaction id. |
| 4809 | * This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4810 | * |
| 4811 | * This does not cow, but it does stuff the starting key it finds back |
| 4812 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 4813 | * key and get a writable path. |
| 4814 | * |
| 4815 | * This does lock as it descends, and path->keep_locks should be set |
| 4816 | * to 1 by the caller. |
| 4817 | * |
| 4818 | * This honors path->lowest_level to prevent descent past a given level |
| 4819 | * of the tree. |
| 4820 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4821 | * min_trans indicates the oldest transaction that you are interested |
| 4822 | * in walking through. Any nodes or leaves older than min_trans are |
| 4823 | * skipped over (without reading them). |
| 4824 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4825 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 4826 | * was nothing in the tree that matched the search criteria. |
| 4827 | */ |
| 4828 | 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] | 4829 | struct btrfs_key *max_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4830 | struct btrfs_path *path, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4831 | u64 min_trans) |
| 4832 | { |
| 4833 | struct extent_buffer *cur; |
| 4834 | struct btrfs_key found_key; |
| 4835 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4836 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4837 | u32 nritems; |
| 4838 | int level; |
| 4839 | int ret = 1; |
| 4840 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4841 | WARN_ON(!path->keep_locks); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4842 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4843 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4844 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4845 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4846 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4847 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4848 | |
| 4849 | if (btrfs_header_generation(cur) < min_trans) { |
| 4850 | ret = 1; |
| 4851 | goto out; |
| 4852 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4853 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4854 | nritems = btrfs_header_nritems(cur); |
| 4855 | level = btrfs_header_level(cur); |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4856 | sret = bin_search(cur, min_key, level, &slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4857 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4858 | /* at the lowest level, we're done, setup the path and exit */ |
| 4859 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4860 | if (slot >= nritems) |
| 4861 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4862 | ret = 0; |
| 4863 | path->slots[level] = slot; |
| 4864 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 4865 | goto out; |
| 4866 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4867 | if (sret && slot > 0) |
| 4868 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4869 | /* |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4870 | * check this node pointer against the min_trans parameters. |
| 4871 | * If it is too old, old, skip to the next one. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4872 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4873 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4874 | u64 blockptr; |
| 4875 | u64 gen; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4876 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4877 | blockptr = btrfs_node_blockptr(cur, slot); |
| 4878 | gen = btrfs_node_ptr_generation(cur, slot); |
| 4879 | if (gen < min_trans) { |
| 4880 | slot++; |
| 4881 | continue; |
| 4882 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4883 | break; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4884 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4885 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4886 | /* |
| 4887 | * we didn't find a candidate key in this node, walk forward |
| 4888 | * and find another one |
| 4889 | */ |
| 4890 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4891 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4892 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4893 | sret = btrfs_find_next_key(root, path, min_key, level, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 4894 | min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4895 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4896 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4897 | goto again; |
| 4898 | } else { |
| 4899 | goto out; |
| 4900 | } |
| 4901 | } |
| 4902 | /* save our key for returning back */ |
| 4903 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 4904 | path->slots[level] = slot; |
| 4905 | if (level == path->lowest_level) { |
| 4906 | ret = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4907 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4908 | goto out; |
| 4909 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4910 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4911 | cur = read_node_slot(root, cur, slot); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4912 | BUG_ON(!cur); /* -ENOMEM */ |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4913 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4914 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4915 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4916 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4917 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4918 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4919 | btrfs_clear_path_blocking(path, NULL, 0); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4920 | } |
| 4921 | out: |
| 4922 | if (ret == 0) |
| 4923 | memcpy(min_key, &found_key, sizeof(found_key)); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4924 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4925 | return ret; |
| 4926 | } |
| 4927 | |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4928 | static void tree_move_down(struct btrfs_root *root, |
| 4929 | struct btrfs_path *path, |
| 4930 | int *level, int root_level) |
| 4931 | { |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4932 | BUG_ON(*level == 0); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4933 | path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level], |
| 4934 | path->slots[*level]); |
| 4935 | path->slots[*level - 1] = 0; |
| 4936 | (*level)--; |
| 4937 | } |
| 4938 | |
| 4939 | static int tree_move_next_or_upnext(struct btrfs_root *root, |
| 4940 | struct btrfs_path *path, |
| 4941 | int *level, int root_level) |
| 4942 | { |
| 4943 | int ret = 0; |
| 4944 | int nritems; |
| 4945 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 4946 | |
| 4947 | path->slots[*level]++; |
| 4948 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4949 | while (path->slots[*level] >= nritems) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4950 | if (*level == root_level) |
| 4951 | return -1; |
| 4952 | |
| 4953 | /* move upnext */ |
| 4954 | path->slots[*level] = 0; |
| 4955 | free_extent_buffer(path->nodes[*level]); |
| 4956 | path->nodes[*level] = NULL; |
| 4957 | (*level)++; |
| 4958 | path->slots[*level]++; |
| 4959 | |
| 4960 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 4961 | ret = 1; |
| 4962 | } |
| 4963 | return ret; |
| 4964 | } |
| 4965 | |
| 4966 | /* |
| 4967 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 4968 | * or down. |
| 4969 | */ |
| 4970 | static int tree_advance(struct btrfs_root *root, |
| 4971 | struct btrfs_path *path, |
| 4972 | int *level, int root_level, |
| 4973 | int allow_down, |
| 4974 | struct btrfs_key *key) |
| 4975 | { |
| 4976 | int ret; |
| 4977 | |
| 4978 | if (*level == 0 || !allow_down) { |
| 4979 | ret = tree_move_next_or_upnext(root, path, level, root_level); |
| 4980 | } else { |
| 4981 | tree_move_down(root, path, level, root_level); |
| 4982 | ret = 0; |
| 4983 | } |
| 4984 | if (ret >= 0) { |
| 4985 | if (*level == 0) |
| 4986 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 4987 | path->slots[*level]); |
| 4988 | else |
| 4989 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 4990 | path->slots[*level]); |
| 4991 | } |
| 4992 | return ret; |
| 4993 | } |
| 4994 | |
| 4995 | static int tree_compare_item(struct btrfs_root *left_root, |
| 4996 | struct btrfs_path *left_path, |
| 4997 | struct btrfs_path *right_path, |
| 4998 | char *tmp_buf) |
| 4999 | { |
| 5000 | int cmp; |
| 5001 | int len1, len2; |
| 5002 | unsigned long off1, off2; |
| 5003 | |
| 5004 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 5005 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 5006 | if (len1 != len2) |
| 5007 | return 1; |
| 5008 | |
| 5009 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 5010 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 5011 | right_path->slots[0]); |
| 5012 | |
| 5013 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 5014 | |
| 5015 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 5016 | if (cmp) |
| 5017 | return 1; |
| 5018 | return 0; |
| 5019 | } |
| 5020 | |
| 5021 | #define ADVANCE 1 |
| 5022 | #define ADVANCE_ONLY_NEXT -1 |
| 5023 | |
| 5024 | /* |
| 5025 | * This function compares two trees and calls the provided callback for |
| 5026 | * every changed/new/deleted item it finds. |
| 5027 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 5028 | * the compare pretty fast on snapshotted subvolumes. |
| 5029 | * |
| 5030 | * This currently works on commit roots only. As commit roots are read only, |
| 5031 | * we don't do any locking. The commit roots are protected with transactions. |
| 5032 | * Transactions are ended and rejoined when a commit is tried in between. |
| 5033 | * |
| 5034 | * This function checks for modifications done to the trees while comparing. |
| 5035 | * If it detects a change, it aborts immediately. |
| 5036 | */ |
| 5037 | int btrfs_compare_trees(struct btrfs_root *left_root, |
| 5038 | struct btrfs_root *right_root, |
| 5039 | btrfs_changed_cb_t changed_cb, void *ctx) |
| 5040 | { |
| 5041 | int ret; |
| 5042 | int cmp; |
| 5043 | struct btrfs_trans_handle *trans = NULL; |
| 5044 | struct btrfs_path *left_path = NULL; |
| 5045 | struct btrfs_path *right_path = NULL; |
| 5046 | struct btrfs_key left_key; |
| 5047 | struct btrfs_key right_key; |
| 5048 | char *tmp_buf = NULL; |
| 5049 | int left_root_level; |
| 5050 | int right_root_level; |
| 5051 | int left_level; |
| 5052 | int right_level; |
| 5053 | int left_end_reached; |
| 5054 | int right_end_reached; |
| 5055 | int advance_left; |
| 5056 | int advance_right; |
| 5057 | u64 left_blockptr; |
| 5058 | u64 right_blockptr; |
| 5059 | u64 left_start_ctransid; |
| 5060 | u64 right_start_ctransid; |
| 5061 | u64 ctransid; |
| 5062 | |
| 5063 | left_path = btrfs_alloc_path(); |
| 5064 | if (!left_path) { |
| 5065 | ret = -ENOMEM; |
| 5066 | goto out; |
| 5067 | } |
| 5068 | right_path = btrfs_alloc_path(); |
| 5069 | if (!right_path) { |
| 5070 | ret = -ENOMEM; |
| 5071 | goto out; |
| 5072 | } |
| 5073 | |
| 5074 | tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS); |
| 5075 | if (!tmp_buf) { |
| 5076 | ret = -ENOMEM; |
| 5077 | goto out; |
| 5078 | } |
| 5079 | |
| 5080 | left_path->search_commit_root = 1; |
| 5081 | left_path->skip_locking = 1; |
| 5082 | right_path->search_commit_root = 1; |
| 5083 | right_path->skip_locking = 1; |
| 5084 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5085 | spin_lock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5086 | left_start_ctransid = btrfs_root_ctransid(&left_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5087 | spin_unlock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5088 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5089 | spin_lock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5090 | right_start_ctransid = btrfs_root_ctransid(&right_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5091 | spin_unlock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5092 | |
| 5093 | trans = btrfs_join_transaction(left_root); |
| 5094 | if (IS_ERR(trans)) { |
| 5095 | ret = PTR_ERR(trans); |
| 5096 | trans = NULL; |
| 5097 | goto out; |
| 5098 | } |
| 5099 | |
| 5100 | /* |
| 5101 | * Strategy: Go to the first items of both trees. Then do |
| 5102 | * |
| 5103 | * If both trees are at level 0 |
| 5104 | * Compare keys of current items |
| 5105 | * If left < right treat left item as new, advance left tree |
| 5106 | * and repeat |
| 5107 | * If left > right treat right item as deleted, advance right tree |
| 5108 | * and repeat |
| 5109 | * If left == right do deep compare of items, treat as changed if |
| 5110 | * needed, advance both trees and repeat |
| 5111 | * If both trees are at the same level but not at level 0 |
| 5112 | * Compare keys of current nodes/leafs |
| 5113 | * If left < right advance left tree and repeat |
| 5114 | * If left > right advance right tree and repeat |
| 5115 | * If left == right compare blockptrs of the next nodes/leafs |
| 5116 | * If they match advance both trees but stay at the same level |
| 5117 | * and repeat |
| 5118 | * If they don't match advance both trees while allowing to go |
| 5119 | * deeper and repeat |
| 5120 | * If tree levels are different |
| 5121 | * Advance the tree that needs it and repeat |
| 5122 | * |
| 5123 | * Advancing a tree means: |
| 5124 | * If we are at level 0, try to go to the next slot. If that's not |
| 5125 | * possible, go one level up and repeat. Stop when we found a level |
| 5126 | * where we could go to the next slot. We may at this point be on a |
| 5127 | * node or a leaf. |
| 5128 | * |
| 5129 | * If we are not at level 0 and not on shared tree blocks, go one |
| 5130 | * level deeper. |
| 5131 | * |
| 5132 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 5133 | * the right if possible or go up and right. |
| 5134 | */ |
| 5135 | |
| 5136 | left_level = btrfs_header_level(left_root->commit_root); |
| 5137 | left_root_level = left_level; |
| 5138 | left_path->nodes[left_level] = left_root->commit_root; |
| 5139 | extent_buffer_get(left_path->nodes[left_level]); |
| 5140 | |
| 5141 | right_level = btrfs_header_level(right_root->commit_root); |
| 5142 | right_root_level = right_level; |
| 5143 | right_path->nodes[right_level] = right_root->commit_root; |
| 5144 | extent_buffer_get(right_path->nodes[right_level]); |
| 5145 | |
| 5146 | if (left_level == 0) |
| 5147 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 5148 | &left_key, left_path->slots[left_level]); |
| 5149 | else |
| 5150 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 5151 | &left_key, left_path->slots[left_level]); |
| 5152 | if (right_level == 0) |
| 5153 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 5154 | &right_key, right_path->slots[right_level]); |
| 5155 | else |
| 5156 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 5157 | &right_key, right_path->slots[right_level]); |
| 5158 | |
| 5159 | left_end_reached = right_end_reached = 0; |
| 5160 | advance_left = advance_right = 0; |
| 5161 | |
| 5162 | while (1) { |
| 5163 | /* |
| 5164 | * We need to make sure the transaction does not get committed |
| 5165 | * while we do anything on commit roots. This means, we need to |
| 5166 | * join and leave transactions for every item that we process. |
| 5167 | */ |
| 5168 | if (trans && btrfs_should_end_transaction(trans, left_root)) { |
| 5169 | btrfs_release_path(left_path); |
| 5170 | btrfs_release_path(right_path); |
| 5171 | |
| 5172 | ret = btrfs_end_transaction(trans, left_root); |
| 5173 | trans = NULL; |
| 5174 | if (ret < 0) |
| 5175 | goto out; |
| 5176 | } |
| 5177 | /* now rejoin the transaction */ |
| 5178 | if (!trans) { |
| 5179 | trans = btrfs_join_transaction(left_root); |
| 5180 | if (IS_ERR(trans)) { |
| 5181 | ret = PTR_ERR(trans); |
| 5182 | trans = NULL; |
| 5183 | goto out; |
| 5184 | } |
| 5185 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5186 | spin_lock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5187 | ctransid = btrfs_root_ctransid(&left_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5188 | spin_unlock(&left_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5189 | if (ctransid != left_start_ctransid) |
| 5190 | left_start_ctransid = 0; |
| 5191 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5192 | spin_lock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5193 | ctransid = btrfs_root_ctransid(&right_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5194 | spin_unlock(&right_root->root_item_lock); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5195 | if (ctransid != right_start_ctransid) |
| 5196 | right_start_ctransid = 0; |
| 5197 | |
| 5198 | if (!left_start_ctransid || !right_start_ctransid) { |
| 5199 | WARN(1, KERN_WARNING |
| 5200 | "btrfs: btrfs_compare_tree detected " |
| 5201 | "a change in one of the trees while " |
| 5202 | "iterating. This is probably a " |
| 5203 | "bug.\n"); |
| 5204 | ret = -EIO; |
| 5205 | goto out; |
| 5206 | } |
| 5207 | |
| 5208 | /* |
| 5209 | * the commit root may have changed, so start again |
| 5210 | * where we stopped |
| 5211 | */ |
| 5212 | left_path->lowest_level = left_level; |
| 5213 | right_path->lowest_level = right_level; |
| 5214 | ret = btrfs_search_slot(NULL, left_root, |
| 5215 | &left_key, left_path, 0, 0); |
| 5216 | if (ret < 0) |
| 5217 | goto out; |
| 5218 | ret = btrfs_search_slot(NULL, right_root, |
| 5219 | &right_key, right_path, 0, 0); |
| 5220 | if (ret < 0) |
| 5221 | goto out; |
| 5222 | } |
| 5223 | |
| 5224 | if (advance_left && !left_end_reached) { |
| 5225 | ret = tree_advance(left_root, left_path, &left_level, |
| 5226 | left_root_level, |
| 5227 | advance_left != ADVANCE_ONLY_NEXT, |
| 5228 | &left_key); |
| 5229 | if (ret < 0) |
| 5230 | left_end_reached = ADVANCE; |
| 5231 | advance_left = 0; |
| 5232 | } |
| 5233 | if (advance_right && !right_end_reached) { |
| 5234 | ret = tree_advance(right_root, right_path, &right_level, |
| 5235 | right_root_level, |
| 5236 | advance_right != ADVANCE_ONLY_NEXT, |
| 5237 | &right_key); |
| 5238 | if (ret < 0) |
| 5239 | right_end_reached = ADVANCE; |
| 5240 | advance_right = 0; |
| 5241 | } |
| 5242 | |
| 5243 | if (left_end_reached && right_end_reached) { |
| 5244 | ret = 0; |
| 5245 | goto out; |
| 5246 | } else if (left_end_reached) { |
| 5247 | if (right_level == 0) { |
| 5248 | ret = changed_cb(left_root, right_root, |
| 5249 | left_path, right_path, |
| 5250 | &right_key, |
| 5251 | BTRFS_COMPARE_TREE_DELETED, |
| 5252 | ctx); |
| 5253 | if (ret < 0) |
| 5254 | goto out; |
| 5255 | } |
| 5256 | advance_right = ADVANCE; |
| 5257 | continue; |
| 5258 | } else if (right_end_reached) { |
| 5259 | if (left_level == 0) { |
| 5260 | ret = changed_cb(left_root, right_root, |
| 5261 | left_path, right_path, |
| 5262 | &left_key, |
| 5263 | BTRFS_COMPARE_TREE_NEW, |
| 5264 | ctx); |
| 5265 | if (ret < 0) |
| 5266 | goto out; |
| 5267 | } |
| 5268 | advance_left = ADVANCE; |
| 5269 | continue; |
| 5270 | } |
| 5271 | |
| 5272 | if (left_level == 0 && right_level == 0) { |
| 5273 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5274 | if (cmp < 0) { |
| 5275 | ret = changed_cb(left_root, right_root, |
| 5276 | left_path, right_path, |
| 5277 | &left_key, |
| 5278 | BTRFS_COMPARE_TREE_NEW, |
| 5279 | ctx); |
| 5280 | if (ret < 0) |
| 5281 | goto out; |
| 5282 | advance_left = ADVANCE; |
| 5283 | } else if (cmp > 0) { |
| 5284 | ret = changed_cb(left_root, right_root, |
| 5285 | left_path, right_path, |
| 5286 | &right_key, |
| 5287 | BTRFS_COMPARE_TREE_DELETED, |
| 5288 | ctx); |
| 5289 | if (ret < 0) |
| 5290 | goto out; |
| 5291 | advance_right = ADVANCE; |
| 5292 | } else { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5293 | enum btrfs_compare_tree_result cmp; |
| 5294 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5295 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5296 | ret = tree_compare_item(left_root, left_path, |
| 5297 | right_path, tmp_buf); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5298 | if (ret) |
| 5299 | cmp = BTRFS_COMPARE_TREE_CHANGED; |
| 5300 | else |
| 5301 | cmp = BTRFS_COMPARE_TREE_SAME; |
| 5302 | ret = changed_cb(left_root, right_root, |
| 5303 | left_path, right_path, |
| 5304 | &left_key, cmp, ctx); |
| 5305 | if (ret < 0) |
| 5306 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5307 | advance_left = ADVANCE; |
| 5308 | advance_right = ADVANCE; |
| 5309 | } |
| 5310 | } else if (left_level == right_level) { |
| 5311 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5312 | if (cmp < 0) { |
| 5313 | advance_left = ADVANCE; |
| 5314 | } else if (cmp > 0) { |
| 5315 | advance_right = ADVANCE; |
| 5316 | } else { |
| 5317 | left_blockptr = btrfs_node_blockptr( |
| 5318 | left_path->nodes[left_level], |
| 5319 | left_path->slots[left_level]); |
| 5320 | right_blockptr = btrfs_node_blockptr( |
| 5321 | right_path->nodes[right_level], |
| 5322 | right_path->slots[right_level]); |
| 5323 | if (left_blockptr == right_blockptr) { |
| 5324 | /* |
| 5325 | * As we're on a shared block, don't |
| 5326 | * allow to go deeper. |
| 5327 | */ |
| 5328 | advance_left = ADVANCE_ONLY_NEXT; |
| 5329 | advance_right = ADVANCE_ONLY_NEXT; |
| 5330 | } else { |
| 5331 | advance_left = ADVANCE; |
| 5332 | advance_right = ADVANCE; |
| 5333 | } |
| 5334 | } |
| 5335 | } else if (left_level < right_level) { |
| 5336 | advance_right = ADVANCE; |
| 5337 | } else { |
| 5338 | advance_left = ADVANCE; |
| 5339 | } |
| 5340 | } |
| 5341 | |
| 5342 | out: |
| 5343 | btrfs_free_path(left_path); |
| 5344 | btrfs_free_path(right_path); |
| 5345 | kfree(tmp_buf); |
| 5346 | |
| 5347 | if (trans) { |
| 5348 | if (!ret) |
| 5349 | ret = btrfs_end_transaction(trans, left_root); |
| 5350 | else |
| 5351 | btrfs_end_transaction(trans, left_root); |
| 5352 | } |
| 5353 | |
| 5354 | return ret; |
| 5355 | } |
| 5356 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5357 | /* |
| 5358 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5359 | * 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] | 5360 | * tree based on the current path and the min_trans parameters. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5361 | * |
| 5362 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5363 | * and 1 is returned if there are no higher keys in the tree |
| 5364 | * |
| 5365 | * path->keep_locks should be set to 1 on the search made before |
| 5366 | * calling this function. |
| 5367 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5368 | 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] | 5369 | struct btrfs_key *key, int level, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5370 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5371 | int slot; |
| 5372 | struct extent_buffer *c; |
| 5373 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5374 | WARN_ON(!path->keep_locks); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5375 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5376 | if (!path->nodes[level]) |
| 5377 | return 1; |
| 5378 | |
| 5379 | slot = path->slots[level] + 1; |
| 5380 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5381 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5382 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5383 | int ret; |
| 5384 | int orig_lowest; |
| 5385 | struct btrfs_key cur_key; |
| 5386 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5387 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5388 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5389 | |
| 5390 | if (path->locks[level + 1]) { |
| 5391 | level++; |
| 5392 | continue; |
| 5393 | } |
| 5394 | |
| 5395 | slot = btrfs_header_nritems(c) - 1; |
| 5396 | if (level == 0) |
| 5397 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5398 | else |
| 5399 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5400 | |
| 5401 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5402 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5403 | path->lowest_level = level; |
| 5404 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5405 | 0, 0); |
| 5406 | path->lowest_level = orig_lowest; |
| 5407 | if (ret < 0) |
| 5408 | return ret; |
| 5409 | |
| 5410 | c = path->nodes[level]; |
| 5411 | slot = path->slots[level]; |
| 5412 | if (ret == 0) |
| 5413 | slot++; |
| 5414 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5415 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5416 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5417 | if (level == 0) |
| 5418 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5419 | else { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5420 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5421 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5422 | if (gen < min_trans) { |
| 5423 | slot++; |
| 5424 | goto next; |
| 5425 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5426 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5427 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5428 | return 0; |
| 5429 | } |
| 5430 | return 1; |
| 5431 | } |
| 5432 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5433 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5434 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5435 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5436 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5437 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5438 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5439 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5440 | return btrfs_next_old_leaf(root, path, 0); |
| 5441 | } |
| 5442 | |
| 5443 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5444 | u64 time_seq) |
| 5445 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5446 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5447 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5448 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5449 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5450 | struct btrfs_key key; |
| 5451 | u32 nritems; |
| 5452 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5453 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5454 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5455 | |
| 5456 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5457 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5458 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5459 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5460 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5461 | again: |
| 5462 | level = 1; |
| 5463 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5464 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5465 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5466 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5467 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5468 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5469 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5470 | if (time_seq) |
| 5471 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5472 | else |
| 5473 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5474 | path->keep_locks = 0; |
| 5475 | |
| 5476 | if (ret < 0) |
| 5477 | return ret; |
| 5478 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5479 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5480 | /* |
| 5481 | * by releasing the path above we dropped all our locks. A balance |
| 5482 | * could have added more items next to the key that used to be |
| 5483 | * at the very end of the block. So, check again here and |
| 5484 | * advance the path if there are now more items available. |
| 5485 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5486 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5487 | if (ret == 0) |
| 5488 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5489 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5490 | goto done; |
| 5491 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5492 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5493 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5494 | if (!path->nodes[level]) { |
| 5495 | ret = 1; |
| 5496 | goto done; |
| 5497 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5498 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5499 | slot = path->slots[level] + 1; |
| 5500 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5501 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5502 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5503 | if (level == BTRFS_MAX_LEVEL) { |
| 5504 | ret = 1; |
| 5505 | goto done; |
| 5506 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5507 | continue; |
| 5508 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5509 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5510 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5511 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5512 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5513 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5514 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5515 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5516 | next_rw_lock = path->locks[level]; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5517 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5518 | slot, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5519 | if (ret == -EAGAIN) |
| 5520 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5521 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5522 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5523 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5524 | goto done; |
| 5525 | } |
| 5526 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5527 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5528 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5529 | if (!ret && time_seq) { |
| 5530 | /* |
| 5531 | * If we don't get the lock, we may be racing |
| 5532 | * with push_leaf_left, holding that lock while |
| 5533 | * itself waiting for the leaf we've currently |
| 5534 | * locked. To solve this situation, we give up |
| 5535 | * on our lock and cycle. |
| 5536 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5537 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5538 | btrfs_release_path(path); |
| 5539 | cond_resched(); |
| 5540 | goto again; |
| 5541 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5542 | if (!ret) { |
| 5543 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5544 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5545 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5546 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5547 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5548 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5549 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5550 | break; |
| 5551 | } |
| 5552 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5553 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5554 | level--; |
| 5555 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5556 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5557 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5558 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5559 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5560 | path->nodes[level] = next; |
| 5561 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5562 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5563 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5564 | if (!level) |
| 5565 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5566 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5567 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5568 | 0, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5569 | if (ret == -EAGAIN) |
| 5570 | goto again; |
| 5571 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5572 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5573 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5574 | goto done; |
| 5575 | } |
| 5576 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5577 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5578 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5579 | if (!ret) { |
| 5580 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5581 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5582 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5583 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5584 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5585 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5586 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5587 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5588 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5589 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5590 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5591 | path->leave_spinning = old_spinning; |
| 5592 | if (!old_spinning) |
| 5593 | btrfs_set_path_blocking(path); |
| 5594 | |
| 5595 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5596 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5597 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5598 | /* |
| 5599 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5600 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5601 | * |
| 5602 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5603 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5604 | int btrfs_previous_item(struct btrfs_root *root, |
| 5605 | struct btrfs_path *path, u64 min_objectid, |
| 5606 | int type) |
| 5607 | { |
| 5608 | struct btrfs_key found_key; |
| 5609 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5610 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5611 | int ret; |
| 5612 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5613 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5614 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5615 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5616 | ret = btrfs_prev_leaf(root, path); |
| 5617 | if (ret != 0) |
| 5618 | return ret; |
| 5619 | } else { |
| 5620 | path->slots[0]--; |
| 5621 | } |
| 5622 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5623 | nritems = btrfs_header_nritems(leaf); |
| 5624 | if (nritems == 0) |
| 5625 | return 1; |
| 5626 | if (path->slots[0] == nritems) |
| 5627 | path->slots[0]--; |
| 5628 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5629 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5630 | if (found_key.objectid < min_objectid) |
| 5631 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5632 | if (found_key.type == type) |
| 5633 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5634 | if (found_key.objectid == min_objectid && |
| 5635 | found_key.type < type) |
| 5636 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5637 | } |
| 5638 | return 1; |
| 5639 | } |