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); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 40 | static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 41 | struct btrfs_path *path, int level, int slot, |
| 42 | int tree_mod_log); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 43 | static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, |
| 44 | struct extent_buffer *eb); |
| 45 | struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr, |
| 46 | u32 blocksize, u64 parent_transid, |
| 47 | u64 time_seq); |
| 48 | struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root, |
| 49 | u64 bytenr, u32 blocksize, |
| 50 | u64 time_seq); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 51 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 52 | struct btrfs_path *btrfs_alloc_path(void) |
| 53 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 54 | struct btrfs_path *path; |
Jeff Mahoney | e00f730 | 2009-02-12 14:11:25 -0500 | [diff] [blame] | 55 | path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 56 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 59 | /* |
| 60 | * set all locked nodes in the path to blocking locks. This should |
| 61 | * be done before scheduling |
| 62 | */ |
| 63 | noinline void btrfs_set_path_blocking(struct btrfs_path *p) |
| 64 | { |
| 65 | int i; |
| 66 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 67 | if (!p->nodes[i] || !p->locks[i]) |
| 68 | continue; |
| 69 | btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 70 | if (p->locks[i] == BTRFS_READ_LOCK) |
| 71 | p->locks[i] = BTRFS_READ_LOCK_BLOCKING; |
| 72 | else if (p->locks[i] == BTRFS_WRITE_LOCK) |
| 73 | p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * reset all the locked nodes in the patch to spinning locks. |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 79 | * |
| 80 | * held is used to keep lockdep happy, when lockdep is enabled |
| 81 | * we set held to a blocking lock before we go around and |
| 82 | * retake all the spinlocks in the path. You can safely use NULL |
| 83 | * for held |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 84 | */ |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 85 | noinline void btrfs_clear_path_blocking(struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 86 | struct extent_buffer *held, int held_rw) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 87 | { |
| 88 | int i; |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 89 | |
| 90 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 91 | /* lockdep really cares that we take all of these spinlocks |
| 92 | * in the right order. If any of the locks in the path are not |
| 93 | * currently blocking, it is going to complain. So, make really |
| 94 | * really sure by forcing the path to blocking before we clear |
| 95 | * the path blocking. |
| 96 | */ |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 97 | if (held) { |
| 98 | btrfs_set_lock_blocking_rw(held, held_rw); |
| 99 | if (held_rw == BTRFS_WRITE_LOCK) |
| 100 | held_rw = BTRFS_WRITE_LOCK_BLOCKING; |
| 101 | else if (held_rw == BTRFS_READ_LOCK) |
| 102 | held_rw = BTRFS_READ_LOCK_BLOCKING; |
| 103 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 104 | btrfs_set_path_blocking(p); |
| 105 | #endif |
| 106 | |
| 107 | for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 108 | if (p->nodes[i] && p->locks[i]) { |
| 109 | btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 110 | if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING) |
| 111 | p->locks[i] = BTRFS_WRITE_LOCK; |
| 112 | else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING) |
| 113 | p->locks[i] = BTRFS_READ_LOCK; |
| 114 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 115 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 116 | |
| 117 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 118 | if (held) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 119 | btrfs_clear_lock_blocking_rw(held, held_rw); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 120 | #endif |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 123 | /* this also releases the path */ |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 124 | void btrfs_free_path(struct btrfs_path *p) |
| 125 | { |
Jesper Juhl | ff175d5 | 2010-12-25 21:22:30 +0000 | [diff] [blame] | 126 | if (!p) |
| 127 | return; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 128 | btrfs_release_path(p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 129 | kmem_cache_free(btrfs_path_cachep, p); |
| 130 | } |
| 131 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 132 | /* |
| 133 | * path release drops references on the extent buffers in the path |
| 134 | * and it drops any locks held by this path |
| 135 | * |
| 136 | * It is safe to call this on paths that no locks or extent buffers held. |
| 137 | */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 138 | noinline void btrfs_release_path(struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 139 | { |
| 140 | int i; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 141 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 142 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 143 | p->slots[i] = 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 144 | if (!p->nodes[i]) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 145 | continue; |
| 146 | if (p->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 147 | btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 148 | p->locks[i] = 0; |
| 149 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 150 | free_extent_buffer(p->nodes[i]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 151 | p->nodes[i] = NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 155 | /* |
| 156 | * safely gets a reference on the root node of a tree. A lock |
| 157 | * is not taken, so a concurrent writer may put a different node |
| 158 | * at the root of the tree. See btrfs_lock_root_node for the |
| 159 | * looping required. |
| 160 | * |
| 161 | * The extent buffer returned by this has a reference taken, so |
| 162 | * it won't disappear. It may stop being the root of the tree |
| 163 | * at any time because there are no locks held. |
| 164 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 165 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
| 166 | { |
| 167 | struct extent_buffer *eb; |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 168 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 169 | while (1) { |
| 170 | rcu_read_lock(); |
| 171 | eb = rcu_dereference(root->node); |
| 172 | |
| 173 | /* |
| 174 | * RCU really hurts here, we could free up the root node because |
| 175 | * it was cow'ed but we may not get the new root node yet so do |
| 176 | * the inc_not_zero dance and if it doesn't work then |
| 177 | * synchronize_rcu and try again. |
| 178 | */ |
| 179 | if (atomic_inc_not_zero(&eb->refs)) { |
| 180 | rcu_read_unlock(); |
| 181 | break; |
| 182 | } |
| 183 | rcu_read_unlock(); |
| 184 | synchronize_rcu(); |
| 185 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 186 | return eb; |
| 187 | } |
| 188 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 189 | /* loop around taking references on and locking the root node of the |
| 190 | * tree until you end up with a lock on the root. A locked buffer |
| 191 | * is returned, with a reference held. |
| 192 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 193 | struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) |
| 194 | { |
| 195 | struct extent_buffer *eb; |
| 196 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 197 | while (1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 198 | eb = btrfs_root_node(root); |
| 199 | btrfs_tree_lock(eb); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 200 | if (eb == root->node) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 201 | break; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 202 | btrfs_tree_unlock(eb); |
| 203 | free_extent_buffer(eb); |
| 204 | } |
| 205 | return eb; |
| 206 | } |
| 207 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 208 | /* loop around taking references on and locking the root node of the |
| 209 | * tree until you end up with a lock on the root. A locked buffer |
| 210 | * is returned, with a reference held. |
| 211 | */ |
| 212 | struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) |
| 213 | { |
| 214 | struct extent_buffer *eb; |
| 215 | |
| 216 | while (1) { |
| 217 | eb = btrfs_root_node(root); |
| 218 | btrfs_tree_read_lock(eb); |
| 219 | if (eb == root->node) |
| 220 | break; |
| 221 | btrfs_tree_read_unlock(eb); |
| 222 | free_extent_buffer(eb); |
| 223 | } |
| 224 | return eb; |
| 225 | } |
| 226 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 227 | /* cowonly root (everything not a reference counted cow subvolume), just get |
| 228 | * put onto a simple dirty list. transaction.c walks this to make sure they |
| 229 | * get properly updated on disk. |
| 230 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 231 | static void add_root_to_dirty_list(struct btrfs_root *root) |
| 232 | { |
Chris Mason | e5846fc | 2012-05-03 12:08:48 -0400 | [diff] [blame] | 233 | spin_lock(&root->fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 234 | if (root->track_dirty && list_empty(&root->dirty_list)) { |
| 235 | list_add(&root->dirty_list, |
| 236 | &root->fs_info->dirty_cowonly_roots); |
| 237 | } |
Chris Mason | e5846fc | 2012-05-03 12:08:48 -0400 | [diff] [blame] | 238 | spin_unlock(&root->fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 241 | /* |
| 242 | * used by snapshot creation to make a copy of a root for a tree with |
| 243 | * a given objectid. The buffer with the new root node is returned in |
| 244 | * cow_ret, and this func returns zero on success or a negative error code. |
| 245 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 246 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
| 247 | struct btrfs_root *root, |
| 248 | struct extent_buffer *buf, |
| 249 | struct extent_buffer **cow_ret, u64 new_root_objectid) |
| 250 | { |
| 251 | struct extent_buffer *cow; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 252 | int ret = 0; |
| 253 | int level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 254 | struct btrfs_disk_key disk_key; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 255 | |
| 256 | WARN_ON(root->ref_cows && trans->transid != |
| 257 | root->fs_info->running_transaction->transid); |
| 258 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
| 259 | |
| 260 | level = btrfs_header_level(buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 261 | if (level == 0) |
| 262 | btrfs_item_key(buf, &disk_key, 0); |
| 263 | else |
| 264 | btrfs_node_key(buf, &disk_key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 265 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 266 | cow = btrfs_alloc_free_block(trans, root, buf->len, 0, |
| 267 | new_root_objectid, &disk_key, level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 268 | buf->start, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 269 | if (IS_ERR(cow)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 270 | return PTR_ERR(cow); |
| 271 | |
| 272 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
| 273 | btrfs_set_header_bytenr(cow, cow->start); |
| 274 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 275 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 276 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 277 | BTRFS_HEADER_FLAG_RELOC); |
| 278 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 279 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 280 | else |
| 281 | btrfs_set_header_owner(cow, new_root_objectid); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 282 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 283 | write_extent_buffer(cow, root->fs_info->fsid, |
| 284 | (unsigned long)btrfs_header_fsid(cow), |
| 285 | BTRFS_FSID_SIZE); |
| 286 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 287 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 288 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 289 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 290 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 291 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 292 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 293 | if (ret) |
| 294 | return ret; |
| 295 | |
| 296 | btrfs_mark_buffer_dirty(cow); |
| 297 | *cow_ret = cow; |
| 298 | return 0; |
| 299 | } |
| 300 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 301 | enum mod_log_op { |
| 302 | MOD_LOG_KEY_REPLACE, |
| 303 | MOD_LOG_KEY_ADD, |
| 304 | MOD_LOG_KEY_REMOVE, |
| 305 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, |
| 306 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, |
| 307 | MOD_LOG_MOVE_KEYS, |
| 308 | MOD_LOG_ROOT_REPLACE, |
| 309 | }; |
| 310 | |
| 311 | struct tree_mod_move { |
| 312 | int dst_slot; |
| 313 | int nr_items; |
| 314 | }; |
| 315 | |
| 316 | struct tree_mod_root { |
| 317 | u64 logical; |
| 318 | u8 level; |
| 319 | }; |
| 320 | |
| 321 | struct tree_mod_elem { |
| 322 | struct rb_node node; |
| 323 | u64 index; /* shifted logical */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 324 | u64 seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 325 | enum mod_log_op op; |
| 326 | |
| 327 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ |
| 328 | int slot; |
| 329 | |
| 330 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ |
| 331 | u64 generation; |
| 332 | |
| 333 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ |
| 334 | struct btrfs_disk_key key; |
| 335 | u64 blockptr; |
| 336 | |
| 337 | /* this is used for op == MOD_LOG_MOVE_KEYS */ |
| 338 | struct tree_mod_move move; |
| 339 | |
| 340 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ |
| 341 | struct tree_mod_root old_root; |
| 342 | }; |
| 343 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 344 | 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] | 345 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 346 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 349 | 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] | 350 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 351 | read_unlock(&fs_info->tree_mod_log_lock); |
| 352 | } |
| 353 | |
| 354 | static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info) |
| 355 | { |
| 356 | write_lock(&fs_info->tree_mod_log_lock); |
| 357 | } |
| 358 | |
| 359 | static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info) |
| 360 | { |
| 361 | write_unlock(&fs_info->tree_mod_log_lock); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * This adds a new blocker to the tree mod log's blocker list if the @elem |
| 366 | * passed does not already have a sequence number set. So when a caller expects |
| 367 | * to record tree modifications, it should ensure to set elem->seq to zero |
| 368 | * before calling btrfs_get_tree_mod_seq. |
| 369 | * Returns a fresh, unused tree log modification sequence number, even if no new |
| 370 | * blocker was added. |
| 371 | */ |
| 372 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 373 | struct seq_list *elem) |
| 374 | { |
| 375 | u64 seq; |
| 376 | |
| 377 | tree_mod_log_write_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 378 | spin_lock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 379 | if (!elem->seq) { |
| 380 | elem->seq = btrfs_inc_tree_mod_seq(fs_info); |
| 381 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 382 | } |
| 383 | seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 384 | spin_unlock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 385 | tree_mod_log_write_unlock(fs_info); |
| 386 | |
| 387 | return seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 391 | struct seq_list *elem) |
| 392 | { |
| 393 | struct rb_root *tm_root; |
| 394 | struct rb_node *node; |
| 395 | struct rb_node *next; |
| 396 | struct seq_list *cur_elem; |
| 397 | struct tree_mod_elem *tm; |
| 398 | u64 min_seq = (u64)-1; |
| 399 | u64 seq_putting = elem->seq; |
| 400 | |
| 401 | if (!seq_putting) |
| 402 | return; |
| 403 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 404 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 405 | list_del(&elem->list); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 406 | elem->seq = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 407 | |
| 408 | 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] | 409 | if (cur_elem->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 410 | if (seq_putting > cur_elem->seq) { |
| 411 | /* |
| 412 | * blocker with lower sequence number exists, we |
| 413 | * cannot remove anything from the log |
| 414 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 415 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 416 | return; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 417 | } |
| 418 | min_seq = cur_elem->seq; |
| 419 | } |
| 420 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 421 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 422 | |
| 423 | /* |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 424 | * anything that's lower than the lowest existing (read: blocked) |
| 425 | * sequence number can be removed from the tree. |
| 426 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 427 | tree_mod_log_write_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 428 | tm_root = &fs_info->tree_mod_log; |
| 429 | for (node = rb_first(tm_root); node; node = next) { |
| 430 | next = rb_next(node); |
| 431 | tm = container_of(node, struct tree_mod_elem, node); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 432 | if (tm->seq > min_seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 433 | continue; |
| 434 | rb_erase(node, tm_root); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 435 | kfree(tm); |
| 436 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 437 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
| 441 | * key order of the log: |
| 442 | * index -> sequence |
| 443 | * |
| 444 | * the index is the shifted logical of the *new* root node for root replace |
| 445 | * operations, or the shifted logical of the affected block for all other |
| 446 | * operations. |
| 447 | */ |
| 448 | static noinline int |
| 449 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 450 | { |
| 451 | struct rb_root *tm_root; |
| 452 | struct rb_node **new; |
| 453 | struct rb_node *parent = NULL; |
| 454 | struct tree_mod_elem *cur; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 455 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 456 | BUG_ON(!tm || !tm->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 457 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 458 | tm_root = &fs_info->tree_mod_log; |
| 459 | new = &tm_root->rb_node; |
| 460 | while (*new) { |
| 461 | cur = container_of(*new, struct tree_mod_elem, node); |
| 462 | parent = *new; |
| 463 | if (cur->index < tm->index) |
| 464 | new = &((*new)->rb_left); |
| 465 | else if (cur->index > tm->index) |
| 466 | new = &((*new)->rb_right); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 467 | else if (cur->seq < tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 468 | new = &((*new)->rb_left); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 469 | else if (cur->seq > tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 470 | new = &((*new)->rb_right); |
| 471 | else { |
| 472 | kfree(tm); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 473 | return -EEXIST; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | rb_link_node(&tm->node, parent, new); |
| 478 | rb_insert_color(&tm->node, tm_root); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 479 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 482 | /* |
| 483 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it |
| 484 | * returns zero with the tree_mod_log_lock acquired. The caller must hold |
| 485 | * this until all tree mod log insertions are recorded in the rb tree and then |
| 486 | * call tree_mod_log_write_unlock() to release. |
| 487 | */ |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 488 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 489 | struct extent_buffer *eb) { |
| 490 | smp_mb(); |
| 491 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 492 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 493 | if (eb && btrfs_header_level(eb) == 0) |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 494 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 495 | |
| 496 | tree_mod_log_write_lock(fs_info); |
| 497 | if (list_empty(&fs_info->tree_mod_seq_list)) { |
| 498 | /* |
| 499 | * someone emptied the list while we were waiting for the lock. |
| 500 | * we must not add to the list when no blocker exists. |
| 501 | */ |
| 502 | tree_mod_log_write_unlock(fs_info); |
| 503 | return 1; |
| 504 | } |
| 505 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 509 | /* |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 510 | * This allocates memory and gets a tree modification sequence number. |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 511 | * |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 512 | * Returns <0 on error. |
| 513 | * Returns >0 (the added sequence number) on success. |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 514 | */ |
Jan Schmidt | 926dd8a | 2012-05-31 14:00:15 +0200 | [diff] [blame] | 515 | static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags, |
| 516 | struct tree_mod_elem **tm_ret) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 517 | { |
| 518 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 519 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 520 | /* |
| 521 | * once we switch from spin locks to something different, we should |
| 522 | * honor the flags parameter here. |
| 523 | */ |
| 524 | tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 525 | if (!tm) |
| 526 | return -ENOMEM; |
| 527 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 528 | tm->seq = btrfs_inc_tree_mod_seq(fs_info); |
| 529 | return tm->seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 532 | static inline int |
| 533 | __tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, |
| 534 | struct extent_buffer *eb, int slot, |
| 535 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 536 | { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 537 | int ret; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 538 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 539 | |
| 540 | ret = tree_mod_alloc(fs_info, flags, &tm); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 541 | if (ret < 0) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 542 | return ret; |
| 543 | |
| 544 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 545 | if (op != MOD_LOG_KEY_ADD) { |
| 546 | btrfs_node_key(eb, &tm->key, slot); |
| 547 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 548 | } |
| 549 | tm->op = op; |
| 550 | tm->slot = slot; |
| 551 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
| 552 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 553 | return __tree_mod_log_insert(fs_info, tm); |
| 554 | } |
| 555 | |
| 556 | static noinline int |
| 557 | tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info, |
| 558 | struct extent_buffer *eb, int slot, |
| 559 | enum mod_log_op op, gfp_t flags) |
| 560 | { |
| 561 | int ret; |
| 562 | |
| 563 | if (tree_mod_dont_log(fs_info, eb)) |
| 564 | return 0; |
| 565 | |
| 566 | ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags); |
| 567 | |
| 568 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 569 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static noinline int |
| 573 | tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 574 | int slot, enum mod_log_op op) |
| 575 | { |
| 576 | return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS); |
| 577 | } |
| 578 | |
| 579 | static noinline int |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 580 | tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info, |
| 581 | struct extent_buffer *eb, int slot, |
| 582 | enum mod_log_op op) |
| 583 | { |
| 584 | return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS); |
| 585 | } |
| 586 | |
| 587 | static noinline int |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 588 | tree_mod_log_insert_move(struct btrfs_fs_info *fs_info, |
| 589 | struct extent_buffer *eb, int dst_slot, int src_slot, |
| 590 | int nr_items, gfp_t flags) |
| 591 | { |
| 592 | struct tree_mod_elem *tm; |
| 593 | int ret; |
| 594 | int i; |
| 595 | |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 596 | if (tree_mod_dont_log(fs_info, eb)) |
| 597 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 598 | |
| 599 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 600 | ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot, |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 601 | MOD_LOG_KEY_REMOVE_WHILE_MOVING); |
| 602 | BUG_ON(ret < 0); |
| 603 | } |
| 604 | |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 605 | ret = tree_mod_alloc(fs_info, flags, &tm); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 606 | if (ret < 0) |
| 607 | goto out; |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 608 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 609 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 610 | tm->slot = src_slot; |
| 611 | tm->move.dst_slot = dst_slot; |
| 612 | tm->move.nr_items = nr_items; |
| 613 | tm->op = MOD_LOG_MOVE_KEYS; |
| 614 | |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 615 | ret = __tree_mod_log_insert(fs_info, tm); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 616 | out: |
| 617 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 618 | return ret; |
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--) { |
| 633 | ret = tree_mod_log_insert_key_locked(fs_info, eb, i, |
| 634 | MOD_LOG_KEY_REMOVE_WHILE_FREEING); |
| 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, |
| 642 | struct extent_buffer *new_root, gfp_t flags) |
| 643 | { |
| 644 | struct tree_mod_elem *tm; |
| 645 | int ret; |
| 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 | |
| 650 | __tree_mod_log_free_eb(fs_info, old_root); |
| 651 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 652 | ret = tree_mod_alloc(fs_info, flags, &tm); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 653 | if (ret < 0) |
| 654 | goto out; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 655 | |
| 656 | tm->index = new_root->start >> PAGE_CACHE_SHIFT; |
| 657 | tm->old_root.logical = old_root->start; |
| 658 | tm->old_root.level = btrfs_header_level(old_root); |
| 659 | tm->generation = btrfs_header_generation(old_root); |
| 660 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 661 | |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 662 | ret = __tree_mod_log_insert(fs_info, tm); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 663 | out: |
| 664 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | 3310c36 | 2012-06-11 10:52:38 +0200 | [diff] [blame] | 665 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static struct tree_mod_elem * |
| 669 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 670 | int smallest) |
| 671 | { |
| 672 | struct rb_root *tm_root; |
| 673 | struct rb_node *node; |
| 674 | struct tree_mod_elem *cur = NULL; |
| 675 | struct tree_mod_elem *found = NULL; |
| 676 | u64 index = start >> PAGE_CACHE_SHIFT; |
| 677 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 678 | tree_mod_log_read_lock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 679 | tm_root = &fs_info->tree_mod_log; |
| 680 | node = tm_root->rb_node; |
| 681 | while (node) { |
| 682 | cur = container_of(node, struct tree_mod_elem, node); |
| 683 | if (cur->index < index) { |
| 684 | node = node->rb_left; |
| 685 | } else if (cur->index > index) { |
| 686 | node = node->rb_right; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 687 | } else if (cur->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 688 | node = node->rb_left; |
| 689 | } else if (!smallest) { |
| 690 | /* we want the node with the highest seq */ |
| 691 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 692 | BUG_ON(found->seq > cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 693 | found = cur; |
| 694 | node = node->rb_left; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 695 | } else if (cur->seq > min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 696 | /* we want the node with the smallest seq */ |
| 697 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 698 | BUG_ON(found->seq < cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 699 | found = cur; |
| 700 | node = node->rb_right; |
| 701 | } else { |
| 702 | found = cur; |
| 703 | break; |
| 704 | } |
| 705 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 706 | tree_mod_log_read_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 707 | |
| 708 | return found; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * this returns the element from the log with the smallest time sequence |
| 713 | * value that's in the log (the oldest log item). any element with a time |
| 714 | * sequence lower than min_seq will be ignored. |
| 715 | */ |
| 716 | static struct tree_mod_elem * |
| 717 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 718 | u64 min_seq) |
| 719 | { |
| 720 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * this returns the element from the log with the largest time sequence |
| 725 | * value that's in the log (the most recent log item). any element with |
| 726 | * a time sequence lower than min_seq will be ignored. |
| 727 | */ |
| 728 | static struct tree_mod_elem * |
| 729 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 730 | { |
| 731 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 732 | } |
| 733 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 734 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 735 | tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 736 | struct extent_buffer *src, unsigned long dst_offset, |
| 737 | unsigned long src_offset, int nr_items) |
| 738 | { |
| 739 | int ret; |
| 740 | int i; |
| 741 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 742 | if (tree_mod_dont_log(fs_info, NULL)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 743 | return; |
| 744 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 745 | if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) { |
| 746 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 747 | return; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 748 | } |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 749 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 750 | for (i = 0; i < nr_items; i++) { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 751 | ret = tree_mod_log_insert_key_locked(fs_info, src, |
| 752 | i + src_offset, |
| 753 | MOD_LOG_KEY_REMOVE); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 754 | BUG_ON(ret < 0); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 755 | ret = tree_mod_log_insert_key_locked(fs_info, dst, |
| 756 | i + dst_offset, |
| 757 | MOD_LOG_KEY_ADD); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 758 | BUG_ON(ret < 0); |
| 759 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 760 | |
| 761 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static inline void |
| 765 | tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 766 | int dst_offset, int src_offset, int nr_items) |
| 767 | { |
| 768 | int ret; |
| 769 | ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset, |
| 770 | nr_items, GFP_NOFS); |
| 771 | BUG_ON(ret < 0); |
| 772 | } |
| 773 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 774 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 775 | tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info, |
| 776 | struct extent_buffer *eb, |
| 777 | struct btrfs_disk_key *disk_key, int slot, int atomic) |
| 778 | { |
| 779 | int ret; |
| 780 | |
| 781 | ret = tree_mod_log_insert_key_mask(fs_info, eb, slot, |
| 782 | MOD_LOG_KEY_REPLACE, |
| 783 | atomic ? GFP_ATOMIC : GFP_NOFS); |
| 784 | BUG_ON(ret < 0); |
| 785 | } |
| 786 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 787 | static noinline void |
| 788 | 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] | 789 | { |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 790 | if (tree_mod_dont_log(fs_info, eb)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 791 | return; |
| 792 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 793 | __tree_mod_log_free_eb(fs_info, eb); |
| 794 | |
| 795 | tree_mod_log_write_unlock(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 798 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 799 | tree_mod_log_set_root_pointer(struct btrfs_root *root, |
| 800 | struct extent_buffer *new_root_node) |
| 801 | { |
| 802 | int ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 803 | ret = tree_mod_log_insert_root(root->fs_info, root->node, |
| 804 | new_root_node, GFP_NOFS); |
| 805 | BUG_ON(ret < 0); |
| 806 | } |
| 807 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 808 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 809 | * check if the tree block can be shared by multiple trees |
| 810 | */ |
| 811 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 812 | struct extent_buffer *buf) |
| 813 | { |
| 814 | /* |
| 815 | * Tree blocks not in refernece counted trees and tree roots |
| 816 | * are never shared. If a block was allocated after the last |
| 817 | * snapshot and the block was not allocated by tree relocation, |
| 818 | * we know the block is not shared. |
| 819 | */ |
| 820 | if (root->ref_cows && |
| 821 | buf != root->node && buf != root->commit_root && |
| 822 | (btrfs_header_generation(buf) <= |
| 823 | btrfs_root_last_snapshot(&root->root_item) || |
| 824 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 825 | return 1; |
| 826 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 827 | if (root->ref_cows && |
| 828 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 829 | return 1; |
| 830 | #endif |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 835 | struct btrfs_root *root, |
| 836 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 837 | struct extent_buffer *cow, |
| 838 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 839 | { |
| 840 | u64 refs; |
| 841 | u64 owner; |
| 842 | u64 flags; |
| 843 | u64 new_flags = 0; |
| 844 | int ret; |
| 845 | |
| 846 | /* |
| 847 | * Backrefs update rules: |
| 848 | * |
| 849 | * Always use full backrefs for extent pointers in tree block |
| 850 | * allocated by tree relocation. |
| 851 | * |
| 852 | * If a shared tree block is no longer referenced by its owner |
| 853 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 854 | * use full backrefs for extent pointers in tree block. |
| 855 | * |
| 856 | * If a tree block is been relocating |
| 857 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 858 | * use full backrefs for extent pointers in tree block. |
| 859 | * The reason for this is some operations (such as drop tree) |
| 860 | * are only allowed for blocks use full backrefs. |
| 861 | */ |
| 862 | |
| 863 | if (btrfs_block_can_be_shared(root, buf)) { |
| 864 | ret = btrfs_lookup_extent_info(trans, root, buf->start, |
| 865 | buf->len, &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 866 | if (ret) |
| 867 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 868 | if (refs == 0) { |
| 869 | ret = -EROFS; |
| 870 | btrfs_std_error(root->fs_info, ret); |
| 871 | return ret; |
| 872 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 873 | } else { |
| 874 | refs = 1; |
| 875 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 876 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 877 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 878 | else |
| 879 | flags = 0; |
| 880 | } |
| 881 | |
| 882 | owner = btrfs_header_owner(buf); |
| 883 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 884 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 885 | |
| 886 | if (refs > 1) { |
| 887 | if ((owner == root->root_key.objectid || |
| 888 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 889 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 890 | ret = btrfs_inc_ref(trans, root, buf, 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 | if (root->root_key.objectid == |
| 894 | BTRFS_TREE_RELOC_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 895 | ret = btrfs_dec_ref(trans, root, buf, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 896 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 897 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 898 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 899 | } |
| 900 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 901 | } else { |
| 902 | |
| 903 | if (root->root_key.objectid == |
| 904 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 905 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 906 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 907 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 908 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 909 | } |
| 910 | if (new_flags != 0) { |
| 911 | ret = btrfs_set_disk_extent_flags(trans, root, |
| 912 | buf->start, |
| 913 | buf->len, |
| 914 | new_flags, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 915 | if (ret) |
| 916 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 917 | } |
| 918 | } else { |
| 919 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 920 | if (root->root_key.objectid == |
| 921 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 922 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 923 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 924 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 925 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 926 | ret = btrfs_dec_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 927 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 928 | } |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 929 | /* |
| 930 | * don't log freeing in case we're freeing the root node, this |
| 931 | * is done by tree_mod_log_set_root_pointer later |
| 932 | */ |
| 933 | if (buf != root->node && btrfs_header_level(buf) != 0) |
| 934 | tree_mod_log_free_eb(root->fs_info, buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 935 | clean_tree_block(trans, root, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 936 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 942 | * does the dirty work in cow of a single block. The parent block (if |
| 943 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 944 | * dirty and returned locked. If you modify the block it needs to be marked |
| 945 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 946 | * |
| 947 | * search_start -- an allocation hint for the new block |
| 948 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 949 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 950 | * bytes the allocator should try to find free next to the block it returns. |
| 951 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 952 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 953 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 954 | struct btrfs_root *root, |
| 955 | struct extent_buffer *buf, |
| 956 | struct extent_buffer *parent, int parent_slot, |
| 957 | struct extent_buffer **cow_ret, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 958 | u64 search_start, u64 empty_size) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 959 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 960 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 961 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 962 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 963 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 964 | int unlock_orig = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 965 | u64 parent_start; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 966 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 967 | if (*cow_ret == buf) |
| 968 | unlock_orig = 1; |
| 969 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 970 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 971 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 972 | WARN_ON(root->ref_cows && trans->transid != |
| 973 | root->fs_info->running_transaction->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 974 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 975 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 976 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 977 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 978 | if (level == 0) |
| 979 | btrfs_item_key(buf, &disk_key, 0); |
| 980 | else |
| 981 | btrfs_node_key(buf, &disk_key, 0); |
| 982 | |
| 983 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 984 | if (parent) |
| 985 | parent_start = parent->start; |
| 986 | else |
| 987 | parent_start = 0; |
| 988 | } else |
| 989 | parent_start = 0; |
| 990 | |
| 991 | cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, |
| 992 | root->root_key.objectid, &disk_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 993 | level, search_start, empty_size); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 994 | if (IS_ERR(cow)) |
| 995 | return PTR_ERR(cow); |
| 996 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 997 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 998 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 999 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1000 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1001 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1002 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 1003 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 1004 | BTRFS_HEADER_FLAG_RELOC); |
| 1005 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1006 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 1007 | else |
| 1008 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1009 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1010 | write_extent_buffer(cow, root->fs_info->fsid, |
| 1011 | (unsigned long)btrfs_header_fsid(cow), |
| 1012 | BTRFS_FSID_SIZE); |
| 1013 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1014 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1015 | if (ret) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1016 | btrfs_abort_transaction(trans, root, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1017 | return ret; |
| 1018 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1019 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1020 | if (root->ref_cows) |
| 1021 | btrfs_reloc_cow_block(trans, root, buf, cow); |
| 1022 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1023 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1024 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1025 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1026 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1027 | parent_start = buf->start; |
| 1028 | else |
| 1029 | parent_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1030 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1031 | extent_buffer_get(cow); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1032 | tree_mod_log_set_root_pointer(root, cow); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1033 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1034 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1035 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1036 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1037 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1038 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1039 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1040 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1041 | parent_start = parent->start; |
| 1042 | else |
| 1043 | parent_start = 0; |
| 1044 | |
| 1045 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1046 | tree_mod_log_insert_key(root->fs_info, parent, parent_slot, |
| 1047 | MOD_LOG_KEY_REPLACE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1048 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1049 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1050 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1051 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1052 | btrfs_mark_buffer_dirty(parent); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1053 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1054 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1055 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1056 | if (unlock_orig) |
| 1057 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1058 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1059 | btrfs_mark_buffer_dirty(cow); |
| 1060 | *cow_ret = cow; |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1064 | /* |
| 1065 | * returns the logical address of the oldest predecessor of the given root. |
| 1066 | * entries older than time_seq are ignored. |
| 1067 | */ |
| 1068 | static struct tree_mod_elem * |
| 1069 | __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info, |
| 1070 | struct btrfs_root *root, u64 time_seq) |
| 1071 | { |
| 1072 | struct tree_mod_elem *tm; |
| 1073 | struct tree_mod_elem *found = NULL; |
| 1074 | u64 root_logical = root->node->start; |
| 1075 | int looped = 0; |
| 1076 | |
| 1077 | if (!time_seq) |
| 1078 | return 0; |
| 1079 | |
| 1080 | /* |
| 1081 | * the very last operation that's logged for a root is the replacement |
| 1082 | * operation (if it is replaced at all). this has the index of the *new* |
| 1083 | * root, making it the very first operation that's logged for this root. |
| 1084 | */ |
| 1085 | while (1) { |
| 1086 | tm = tree_mod_log_search_oldest(fs_info, root_logical, |
| 1087 | time_seq); |
| 1088 | if (!looped && !tm) |
| 1089 | return 0; |
| 1090 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1091 | * if there are no tree operation for the oldest root, we simply |
| 1092 | * return it. this should only happen if that (old) root is at |
| 1093 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1094 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1095 | if (!tm) |
| 1096 | break; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1097 | |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1098 | /* |
| 1099 | * if there's an operation that's not a root replacement, we |
| 1100 | * found the oldest version of our root. normally, we'll find a |
| 1101 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1102 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1103 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1104 | break; |
| 1105 | |
| 1106 | found = tm; |
| 1107 | root_logical = tm->old_root.logical; |
| 1108 | BUG_ON(root_logical == root->node->start); |
| 1109 | looped = 1; |
| 1110 | } |
| 1111 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1112 | /* if there's no old root to return, return what we found instead */ |
| 1113 | if (!found) |
| 1114 | found = tm; |
| 1115 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1116 | return found; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * tm is a pointer to the first operation to rewind within eb. then, all |
| 1121 | * previous operations will be rewinded (until we reach something older than |
| 1122 | * time_seq). |
| 1123 | */ |
| 1124 | static void |
| 1125 | __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq, |
| 1126 | struct tree_mod_elem *first_tm) |
| 1127 | { |
| 1128 | u32 n; |
| 1129 | struct rb_node *next; |
| 1130 | struct tree_mod_elem *tm = first_tm; |
| 1131 | unsigned long o_dst; |
| 1132 | unsigned long o_src; |
| 1133 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1134 | |
| 1135 | n = btrfs_header_nritems(eb); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1136 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1137 | /* |
| 1138 | * all the operations are recorded with the operator used for |
| 1139 | * the modification. as we're going backwards, we do the |
| 1140 | * opposite of each operation here. |
| 1141 | */ |
| 1142 | switch (tm->op) { |
| 1143 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1144 | BUG_ON(tm->slot < n); |
| 1145 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
| 1146 | case MOD_LOG_KEY_REMOVE: |
| 1147 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1148 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1149 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1150 | tm->generation); |
| 1151 | n++; |
| 1152 | break; |
| 1153 | case MOD_LOG_KEY_REPLACE: |
| 1154 | BUG_ON(tm->slot >= n); |
| 1155 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1156 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1157 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1158 | tm->generation); |
| 1159 | break; |
| 1160 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1161 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1162 | n--; |
| 1163 | break; |
| 1164 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1165 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1166 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1167 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1168 | tm->move.nr_items * p_size); |
| 1169 | break; |
| 1170 | case MOD_LOG_ROOT_REPLACE: |
| 1171 | /* |
| 1172 | * this operation is special. for roots, this must be |
| 1173 | * handled explicitly before rewinding. |
| 1174 | * for non-roots, this operation may exist if the node |
| 1175 | * was a root: root A -> child B; then A gets empty and |
| 1176 | * B is promoted to the new root. in the mod log, we'll |
| 1177 | * have a root-replace operation for B, a tree block |
| 1178 | * that is no root. we simply ignore that operation. |
| 1179 | */ |
| 1180 | break; |
| 1181 | } |
| 1182 | next = rb_next(&tm->node); |
| 1183 | if (!next) |
| 1184 | break; |
| 1185 | tm = container_of(next, struct tree_mod_elem, node); |
| 1186 | if (tm->index != first_tm->index) |
| 1187 | break; |
| 1188 | } |
| 1189 | btrfs_set_header_nritems(eb, n); |
| 1190 | } |
| 1191 | |
| 1192 | static struct extent_buffer * |
| 1193 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1194 | u64 time_seq) |
| 1195 | { |
| 1196 | struct extent_buffer *eb_rewin; |
| 1197 | struct tree_mod_elem *tm; |
| 1198 | |
| 1199 | if (!time_seq) |
| 1200 | return eb; |
| 1201 | |
| 1202 | if (btrfs_header_level(eb) == 0) |
| 1203 | return eb; |
| 1204 | |
| 1205 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1206 | if (!tm) |
| 1207 | return eb; |
| 1208 | |
| 1209 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1210 | BUG_ON(tm->slot != 0); |
| 1211 | eb_rewin = alloc_dummy_extent_buffer(eb->start, |
| 1212 | fs_info->tree_root->nodesize); |
| 1213 | BUG_ON(!eb_rewin); |
| 1214 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1215 | btrfs_set_header_backref_rev(eb_rewin, |
| 1216 | btrfs_header_backref_rev(eb)); |
| 1217 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1218 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1219 | } else { |
| 1220 | eb_rewin = btrfs_clone_extent_buffer(eb); |
| 1221 | BUG_ON(!eb_rewin); |
| 1222 | } |
| 1223 | |
| 1224 | extent_buffer_get(eb_rewin); |
| 1225 | free_extent_buffer(eb); |
| 1226 | |
| 1227 | __tree_mod_log_rewind(eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame^] | 1228 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
| 1229 | BTRFS_NODEPTRS_PER_BLOCK(fs_info->fs_root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1230 | |
| 1231 | return eb_rewin; |
| 1232 | } |
| 1233 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1234 | /* |
| 1235 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1236 | * value. If there are no changes, the current root->root_node is returned. If |
| 1237 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1238 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1239 | * Returns NULL on error (with no locks held). |
| 1240 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1241 | static inline struct extent_buffer * |
| 1242 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1243 | { |
| 1244 | struct tree_mod_elem *tm; |
| 1245 | struct extent_buffer *eb; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1246 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1247 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1248 | u64 logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1249 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1250 | eb = btrfs_read_lock_root_node(root); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1251 | tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq); |
| 1252 | if (!tm) |
| 1253 | return root->node; |
| 1254 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1255 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1256 | old_root = &tm->old_root; |
| 1257 | old_generation = tm->generation; |
| 1258 | logical = old_root->logical; |
| 1259 | } else { |
| 1260 | logical = root->node->start; |
| 1261 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1262 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1263 | tm = tree_mod_log_search(root->fs_info, logical, time_seq); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1264 | if (old_root) |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1265 | eb = alloc_dummy_extent_buffer(logical, root->nodesize); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1266 | else |
| 1267 | eb = btrfs_clone_extent_buffer(root->node); |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1268 | btrfs_tree_read_unlock(root->node); |
| 1269 | free_extent_buffer(root->node); |
| 1270 | if (!eb) |
| 1271 | return NULL; |
| 1272 | btrfs_tree_read_lock(eb); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1273 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1274 | btrfs_set_header_bytenr(eb, eb->start); |
| 1275 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
| 1276 | btrfs_set_header_owner(eb, root->root_key.objectid); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1277 | btrfs_set_header_level(eb, old_root->level); |
| 1278 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1279 | } |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1280 | if (tm) |
| 1281 | __tree_mod_log_rewind(eb, time_seq, tm); |
| 1282 | else |
| 1283 | WARN_ON(btrfs_header_level(eb) != 0); |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1284 | extent_buffer_get(eb); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame^] | 1285 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1286 | |
| 1287 | return eb; |
| 1288 | } |
| 1289 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1290 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1291 | struct btrfs_root *root, |
| 1292 | struct extent_buffer *buf) |
| 1293 | { |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1294 | /* ensure we can see the force_cow */ |
| 1295 | smp_rmb(); |
| 1296 | |
| 1297 | /* |
| 1298 | * We do not need to cow a block if |
| 1299 | * 1) this block is not created or changed in this transaction; |
| 1300 | * 2) this block does not belong to TREE_RELOC tree; |
| 1301 | * 3) the root is not forced COW. |
| 1302 | * |
| 1303 | * What is forced COW: |
| 1304 | * when we create snapshot during commiting the transaction, |
| 1305 | * after we've finished coping src root, we must COW the shared |
| 1306 | * block to ensure the metadata consistency. |
| 1307 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1308 | if (btrfs_header_generation(buf) == trans->transid && |
| 1309 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1310 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1311 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
| 1312 | !root->force_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1313 | return 0; |
| 1314 | return 1; |
| 1315 | } |
| 1316 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1317 | /* |
| 1318 | * cows a single block, see __btrfs_cow_block for the real work. |
| 1319 | * This version of it has extra checks so that a block isn't cow'd more than |
| 1320 | * once per transaction, as long as it hasn't been written yet |
| 1321 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1322 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1323 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1324 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1325 | struct extent_buffer **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1326 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1327 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1328 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1329 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1330 | if (trans->transaction != root->fs_info->running_transaction) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1331 | printk(KERN_CRIT "trans %llu running %llu\n", |
| 1332 | (unsigned long long)trans->transid, |
| 1333 | (unsigned long long) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1334 | root->fs_info->running_transaction->transid); |
| 1335 | WARN_ON(1); |
| 1336 | } |
| 1337 | if (trans->transid != root->fs_info->generation) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1338 | printk(KERN_CRIT "trans %llu running %llu\n", |
| 1339 | (unsigned long long)trans->transid, |
| 1340 | (unsigned long long)root->fs_info->generation); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1341 | WARN_ON(1); |
| 1342 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1343 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1344 | if (!should_cow_block(trans, root, buf)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1345 | *cow_ret = buf; |
| 1346 | return 0; |
| 1347 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1348 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1349 | search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1350 | |
| 1351 | if (parent) |
| 1352 | btrfs_set_lock_blocking(parent); |
| 1353 | btrfs_set_lock_blocking(buf); |
| 1354 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1355 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1356 | parent_slot, cow_ret, search_start, 0); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1357 | |
| 1358 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1359 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1360 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1363 | /* |
| 1364 | * helper function for defrag to decide if two blocks pointed to by a |
| 1365 | * node are actually close by |
| 1366 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1367 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1368 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1369 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1370 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1371 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1372 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1373 | return 0; |
| 1374 | } |
| 1375 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1376 | /* |
| 1377 | * compare two keys in a memcmp fashion |
| 1378 | */ |
| 1379 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
| 1380 | { |
| 1381 | struct btrfs_key k1; |
| 1382 | |
| 1383 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1384 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1385 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1386 | } |
| 1387 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1388 | /* |
| 1389 | * same as comp_keys only with two btrfs_key's |
| 1390 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1391 | 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] | 1392 | { |
| 1393 | if (k1->objectid > k2->objectid) |
| 1394 | return 1; |
| 1395 | if (k1->objectid < k2->objectid) |
| 1396 | return -1; |
| 1397 | if (k1->type > k2->type) |
| 1398 | return 1; |
| 1399 | if (k1->type < k2->type) |
| 1400 | return -1; |
| 1401 | if (k1->offset > k2->offset) |
| 1402 | return 1; |
| 1403 | if (k1->offset < k2->offset) |
| 1404 | return -1; |
| 1405 | return 0; |
| 1406 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1407 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1408 | /* |
| 1409 | * this is used by the defrag code to go through all the |
| 1410 | * leaves pointed to by a node and reallocate them so that |
| 1411 | * disk order is close to key order |
| 1412 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1413 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1414 | struct btrfs_root *root, struct extent_buffer *parent, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1415 | int start_slot, int cache_only, u64 *last_ret, |
| 1416 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1417 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1418 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1419 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1420 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1421 | u64 search_start = *last_ret; |
| 1422 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1423 | u64 other; |
| 1424 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1425 | int end_slot; |
| 1426 | int i; |
| 1427 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1428 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1429 | int uptodate; |
| 1430 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1431 | int progress_passed = 0; |
| 1432 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1433 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1434 | parent_level = btrfs_header_level(parent); |
| 1435 | if (cache_only && parent_level != 1) |
| 1436 | return 0; |
| 1437 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1438 | if (trans->transaction != root->fs_info->running_transaction) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1439 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1440 | if (trans->transid != root->fs_info->generation) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1441 | WARN_ON(1); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1442 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1443 | parent_nritems = btrfs_header_nritems(parent); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1444 | blocksize = btrfs_level_size(root, parent_level - 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1445 | end_slot = parent_nritems; |
| 1446 | |
| 1447 | if (parent_nritems == 1) |
| 1448 | return 0; |
| 1449 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1450 | btrfs_set_lock_blocking(parent); |
| 1451 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1452 | for (i = start_slot; i < end_slot; i++) { |
| 1453 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1454 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1455 | btrfs_node_key(parent, &disk_key, i); |
| 1456 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1457 | continue; |
| 1458 | |
| 1459 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1460 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1461 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1462 | if (last_block == 0) |
| 1463 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1464 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1465 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1466 | other = btrfs_node_blockptr(parent, i - 1); |
| 1467 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1468 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1469 | if (!close && i < end_slot - 2) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1470 | other = btrfs_node_blockptr(parent, i + 1); |
| 1471 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1472 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1473 | if (close) { |
| 1474 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1475 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1476 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1477 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1478 | cur = btrfs_find_tree_block(root, blocknr, blocksize); |
| 1479 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1480 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1481 | else |
| 1482 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1483 | if (!cur || !uptodate) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1484 | if (cache_only) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1485 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1486 | continue; |
| 1487 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1488 | if (!cur) { |
| 1489 | cur = read_tree_block(root, blocknr, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1490 | blocksize, gen); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1491 | if (!cur) |
| 1492 | return -EIO; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1493 | } else if (!uptodate) { |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 1494 | err = btrfs_read_buffer(cur, gen); |
| 1495 | if (err) { |
| 1496 | free_extent_buffer(cur); |
| 1497 | return err; |
| 1498 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1499 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1500 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1501 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1502 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1503 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1504 | btrfs_tree_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1505 | btrfs_set_lock_blocking(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1506 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1507 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1508 | min(16 * blocksize, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1509 | (end_slot - i) * blocksize)); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1510 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1511 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1512 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1513 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1514 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1515 | search_start = cur->start; |
| 1516 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1517 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1518 | btrfs_tree_unlock(cur); |
| 1519 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1520 | } |
| 1521 | return err; |
| 1522 | } |
| 1523 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1524 | /* |
| 1525 | * The leaf data grows from end-to-front in the node. |
| 1526 | * this returns the address of the start of the last item, |
| 1527 | * which is the stop of the leaf data stack |
| 1528 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1529 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1530 | struct extent_buffer *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1531 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1532 | u32 nr = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1533 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1534 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1535 | return btrfs_item_offset_nr(leaf, nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1536 | } |
| 1537 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1538 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1539 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1540 | * search for key in the extent_buffer. The items start at offset p, |
| 1541 | * and they are item_size apart. There are 'max' items in p. |
| 1542 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1543 | * the slot in the array is returned via slot, and it points to |
| 1544 | * the place where you would insert key if it is not found in |
| 1545 | * the array. |
| 1546 | * |
| 1547 | * slot may point to max if the key is bigger than all of the keys |
| 1548 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1549 | static noinline int generic_bin_search(struct extent_buffer *eb, |
| 1550 | unsigned long p, |
| 1551 | int item_size, struct btrfs_key *key, |
| 1552 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1553 | { |
| 1554 | int low = 0; |
| 1555 | int high = max; |
| 1556 | int mid; |
| 1557 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1558 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1559 | struct btrfs_disk_key unaligned; |
| 1560 | unsigned long offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1561 | char *kaddr = NULL; |
| 1562 | unsigned long map_start = 0; |
| 1563 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1564 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1565 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1566 | while (low < high) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1567 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1568 | offset = p + mid * item_size; |
| 1569 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1570 | if (!kaddr || offset < map_start || |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1571 | (offset + sizeof(struct btrfs_disk_key)) > |
| 1572 | map_start + map_len) { |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1573 | |
| 1574 | err = map_private_extent_buffer(eb, offset, |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1575 | sizeof(struct btrfs_disk_key), |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1576 | &kaddr, &map_start, &map_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1577 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1578 | if (!err) { |
| 1579 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1580 | map_start); |
| 1581 | } else { |
| 1582 | read_extent_buffer(eb, &unaligned, |
| 1583 | offset, sizeof(unaligned)); |
| 1584 | tmp = &unaligned; |
| 1585 | } |
| 1586 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1587 | } else { |
| 1588 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1589 | map_start); |
| 1590 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1591 | ret = comp_keys(tmp, key); |
| 1592 | |
| 1593 | if (ret < 0) |
| 1594 | low = mid + 1; |
| 1595 | else if (ret > 0) |
| 1596 | high = mid; |
| 1597 | else { |
| 1598 | *slot = mid; |
| 1599 | return 0; |
| 1600 | } |
| 1601 | } |
| 1602 | *slot = low; |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1606 | /* |
| 1607 | * simple bin_search frontend that does the right thing for |
| 1608 | * leaves vs nodes |
| 1609 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1610 | static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1611 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1612 | { |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1613 | if (level == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1614 | return generic_bin_search(eb, |
| 1615 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1616 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1617 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1618 | slot); |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1619 | else |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1620 | return generic_bin_search(eb, |
| 1621 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1622 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1623 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1624 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1625 | } |
| 1626 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1627 | int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1628 | int level, int *slot) |
| 1629 | { |
| 1630 | return bin_search(eb, key, level, slot); |
| 1631 | } |
| 1632 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1633 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1634 | { |
| 1635 | spin_lock(&root->accounting_lock); |
| 1636 | btrfs_set_root_used(&root->root_item, |
| 1637 | btrfs_root_used(&root->root_item) + size); |
| 1638 | spin_unlock(&root->accounting_lock); |
| 1639 | } |
| 1640 | |
| 1641 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1642 | { |
| 1643 | spin_lock(&root->accounting_lock); |
| 1644 | btrfs_set_root_used(&root->root_item, |
| 1645 | btrfs_root_used(&root->root_item) - size); |
| 1646 | spin_unlock(&root->accounting_lock); |
| 1647 | } |
| 1648 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1649 | /* given a node and slot number, this reads the blocks it points to. The |
| 1650 | * extent buffer is returned with a reference taken (but unlocked). |
| 1651 | * NULL is returned on error. |
| 1652 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1653 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1654 | struct extent_buffer *parent, int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1655 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1656 | int level = btrfs_header_level(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1657 | if (slot < 0) |
| 1658 | return NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1659 | if (slot >= btrfs_header_nritems(parent)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1660 | return NULL; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1661 | |
| 1662 | BUG_ON(level == 0); |
| 1663 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1664 | return read_tree_block(root, btrfs_node_blockptr(parent, slot), |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1665 | btrfs_level_size(root, level - 1), |
| 1666 | btrfs_node_ptr_generation(parent, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1667 | } |
| 1668 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1669 | /* |
| 1670 | * node level balancing, used to make sure nodes are in proper order for |
| 1671 | * item deletion. We balance from the top down, so we have to make sure |
| 1672 | * that a deletion won't leave an node completely empty later on. |
| 1673 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1674 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1675 | struct btrfs_root *root, |
| 1676 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1677 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1678 | struct extent_buffer *right = NULL; |
| 1679 | struct extent_buffer *mid; |
| 1680 | struct extent_buffer *left = NULL; |
| 1681 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1682 | int ret = 0; |
| 1683 | int wret; |
| 1684 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1685 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1686 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1687 | |
| 1688 | if (level == 0) |
| 1689 | return 0; |
| 1690 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1691 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1692 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1693 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1694 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1695 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1696 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1697 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1698 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1699 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1700 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1701 | pslot = path->slots[level + 1]; |
| 1702 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1703 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1704 | /* |
| 1705 | * deal with the case where there is only one pointer in the root |
| 1706 | * by promoting the node below to a root |
| 1707 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1708 | if (!parent) { |
| 1709 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1710 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1711 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1712 | return 0; |
| 1713 | |
| 1714 | /* promote the child to a root */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1715 | child = read_node_slot(root, mid, 0); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1716 | if (!child) { |
| 1717 | ret = -EROFS; |
| 1718 | btrfs_std_error(root->fs_info, ret); |
| 1719 | goto enospc; |
| 1720 | } |
| 1721 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1722 | btrfs_tree_lock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1723 | btrfs_set_lock_blocking(child); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1724 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1725 | if (ret) { |
| 1726 | btrfs_tree_unlock(child); |
| 1727 | free_extent_buffer(child); |
| 1728 | goto enospc; |
| 1729 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1730 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1731 | tree_mod_log_set_root_pointer(root, child); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1732 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1733 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1734 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1735 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1736 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1737 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1738 | path->nodes[level] = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1739 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1740 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1741 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1742 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1743 | |
| 1744 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1745 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1746 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1747 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1748 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1749 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1750 | if (btrfs_header_nritems(mid) > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1751 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1752 | return 0; |
| 1753 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1754 | left = read_node_slot(root, parent, pslot - 1); |
| 1755 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1756 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1757 | btrfs_set_lock_blocking(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1758 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1759 | parent, pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1760 | if (wret) { |
| 1761 | ret = wret; |
| 1762 | goto enospc; |
| 1763 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1764 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1765 | right = read_node_slot(root, parent, pslot + 1); |
| 1766 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1767 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1768 | btrfs_set_lock_blocking(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1769 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1770 | parent, pslot + 1, &right); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1771 | if (wret) { |
| 1772 | ret = wret; |
| 1773 | goto enospc; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1778 | if (left) { |
| 1779 | orig_slot += btrfs_header_nritems(left); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1780 | wret = push_node_left(trans, root, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1781 | if (wret < 0) |
| 1782 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1783 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1784 | |
| 1785 | /* |
| 1786 | * then try to empty the right most buffer into the middle |
| 1787 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1788 | if (right) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1789 | wret = push_node_left(trans, root, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1790 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1791 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1792 | if (btrfs_header_nritems(right) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1793 | clean_tree_block(trans, root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1794 | btrfs_tree_unlock(right); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 1795 | del_ptr(trans, root, path, level + 1, pslot + 1, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1796 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1797 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1798 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1799 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1800 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1801 | struct btrfs_disk_key right_key; |
| 1802 | btrfs_node_key(right, &right_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1803 | tree_mod_log_set_node_key(root->fs_info, parent, |
| 1804 | &right_key, pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1805 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1806 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1807 | } |
| 1808 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1809 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1810 | /* |
| 1811 | * we're not allowed to leave a node with one item in the |
| 1812 | * tree during a delete. A deletion from lower in the tree |
| 1813 | * could try to delete the only pointer in this node. |
| 1814 | * So, pull some keys from the left. |
| 1815 | * There has to be a left pointer at this point because |
| 1816 | * otherwise we would have pulled some pointers from the |
| 1817 | * right |
| 1818 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1819 | if (!left) { |
| 1820 | ret = -EROFS; |
| 1821 | btrfs_std_error(root->fs_info, ret); |
| 1822 | goto enospc; |
| 1823 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1824 | wret = balance_node_right(trans, root, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1825 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1826 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1827 | goto enospc; |
| 1828 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1829 | if (wret == 1) { |
| 1830 | wret = push_node_left(trans, root, left, mid, 1); |
| 1831 | if (wret < 0) |
| 1832 | ret = wret; |
| 1833 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1834 | BUG_ON(wret == 1); |
| 1835 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1836 | if (btrfs_header_nritems(mid) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1837 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1838 | btrfs_tree_unlock(mid); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 1839 | del_ptr(trans, root, path, level + 1, pslot, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1840 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1841 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1842 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1843 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1844 | } else { |
| 1845 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1846 | struct btrfs_disk_key mid_key; |
| 1847 | btrfs_node_key(mid, &mid_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1848 | tree_mod_log_set_node_key(root->fs_info, parent, &mid_key, |
| 1849 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1850 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1851 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1852 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1853 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1854 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1855 | if (left) { |
| 1856 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1857 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1858 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1859 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1860 | path->slots[level + 1] -= 1; |
| 1861 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1862 | if (mid) { |
| 1863 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1864 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1865 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1866 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1867 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1868 | path->slots[level] = orig_slot; |
| 1869 | } |
| 1870 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1871 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1872 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1873 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1874 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1875 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1876 | if (right) { |
| 1877 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1878 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1879 | } |
| 1880 | if (left) { |
| 1881 | if (path->nodes[level] != left) |
| 1882 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1883 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1884 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1885 | return ret; |
| 1886 | } |
| 1887 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1888 | /* Node balancing for insertion. Here we only split or push nodes around |
| 1889 | * when they are completely full. This is also done top down, so we |
| 1890 | * have to be pessimistic. |
| 1891 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1892 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1893 | struct btrfs_root *root, |
| 1894 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1895 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1896 | struct extent_buffer *right = NULL; |
| 1897 | struct extent_buffer *mid; |
| 1898 | struct extent_buffer *left = NULL; |
| 1899 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1900 | int ret = 0; |
| 1901 | int wret; |
| 1902 | int pslot; |
| 1903 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1904 | |
| 1905 | if (level == 0) |
| 1906 | return 1; |
| 1907 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1908 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1909 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1910 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1911 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1912 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1913 | pslot = path->slots[level + 1]; |
| 1914 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1915 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1916 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1917 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1918 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1919 | left = read_node_slot(root, parent, pslot - 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1920 | |
| 1921 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1922 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1923 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1924 | |
| 1925 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1926 | btrfs_set_lock_blocking(left); |
| 1927 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1928 | left_nr = btrfs_header_nritems(left); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1929 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1930 | wret = 1; |
| 1931 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1932 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1933 | pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1934 | if (ret) |
| 1935 | wret = 1; |
| 1936 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1937 | wret = push_node_left(trans, root, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1938 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1939 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1940 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1941 | if (wret < 0) |
| 1942 | ret = wret; |
| 1943 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1944 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1945 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1946 | btrfs_node_key(mid, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1947 | tree_mod_log_set_node_key(root->fs_info, parent, |
| 1948 | &disk_key, pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1949 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 1950 | btrfs_mark_buffer_dirty(parent); |
| 1951 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1952 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1953 | path->slots[level + 1] -= 1; |
| 1954 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1955 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1956 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1957 | } else { |
| 1958 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1959 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1960 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1961 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1962 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1963 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1964 | return 0; |
| 1965 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1966 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1967 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1968 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1969 | right = read_node_slot(root, parent, pslot + 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1970 | |
| 1971 | /* |
| 1972 | * then try to empty the right most buffer into the middle |
| 1973 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1974 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1975 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1976 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1977 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1978 | btrfs_set_lock_blocking(right); |
| 1979 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1980 | right_nr = btrfs_header_nritems(right); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1981 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1982 | wret = 1; |
| 1983 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1984 | ret = btrfs_cow_block(trans, root, right, |
| 1985 | parent, pslot + 1, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1986 | &right); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1987 | if (ret) |
| 1988 | wret = 1; |
| 1989 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1990 | wret = balance_node_right(trans, root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1991 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1992 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1993 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1994 | if (wret < 0) |
| 1995 | ret = wret; |
| 1996 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1997 | struct btrfs_disk_key disk_key; |
| 1998 | |
| 1999 | btrfs_node_key(right, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2000 | tree_mod_log_set_node_key(root->fs_info, parent, |
| 2001 | &disk_key, pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2002 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2003 | btrfs_mark_buffer_dirty(parent); |
| 2004 | |
| 2005 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2006 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2007 | path->slots[level + 1] += 1; |
| 2008 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2009 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2010 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2011 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2012 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2013 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2014 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2015 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2016 | return 0; |
| 2017 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2018 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2019 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2020 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2021 | return 1; |
| 2022 | } |
| 2023 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2024 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2025 | * readahead one full node of leaves, finding things that are close |
| 2026 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2027 | */ |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2028 | static void reada_for_search(struct btrfs_root *root, |
| 2029 | struct btrfs_path *path, |
| 2030 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2031 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2032 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2033 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2034 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2035 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2036 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2037 | u64 nread = 0; |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2038 | u64 gen; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2039 | int direction = path->reada; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2040 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2041 | u32 nr; |
| 2042 | u32 blocksize; |
| 2043 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2044 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2045 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2046 | return; |
| 2047 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2048 | if (!path->nodes[level]) |
| 2049 | return; |
| 2050 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2051 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2052 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2053 | search = btrfs_node_blockptr(node, slot); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2054 | blocksize = btrfs_level_size(root, level - 1); |
| 2055 | eb = btrfs_find_tree_block(root, search, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2056 | if (eb) { |
| 2057 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2058 | return; |
| 2059 | } |
| 2060 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2061 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2062 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2063 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2064 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2065 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2066 | while (1) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2067 | if (direction < 0) { |
| 2068 | if (nr == 0) |
| 2069 | break; |
| 2070 | nr--; |
| 2071 | } else if (direction > 0) { |
| 2072 | nr++; |
| 2073 | if (nr >= nritems) |
| 2074 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2075 | } |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2076 | if (path->reada < 0 && objectid) { |
| 2077 | btrfs_node_key(node, &disk_key, nr); |
| 2078 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2079 | break; |
| 2080 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2081 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2082 | if ((search <= target && target - search <= 65536) || |
| 2083 | (search > target && search - target <= 65536)) { |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2084 | gen = btrfs_node_ptr_generation(node, nr); |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 2085 | readahead_tree_block(root, search, blocksize, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2086 | nread += blocksize; |
| 2087 | } |
| 2088 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2089 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2090 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2091 | } |
| 2092 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2093 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2094 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2095 | * returns -EAGAIN if it had to drop the path, or zero if everything was in |
| 2096 | * cache |
| 2097 | */ |
| 2098 | static noinline int reada_for_balance(struct btrfs_root *root, |
| 2099 | struct btrfs_path *path, int level) |
| 2100 | { |
| 2101 | int slot; |
| 2102 | int nritems; |
| 2103 | struct extent_buffer *parent; |
| 2104 | struct extent_buffer *eb; |
| 2105 | u64 gen; |
| 2106 | u64 block1 = 0; |
| 2107 | u64 block2 = 0; |
| 2108 | int ret = 0; |
| 2109 | int blocksize; |
| 2110 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2111 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2112 | if (!parent) |
| 2113 | return 0; |
| 2114 | |
| 2115 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2116 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2117 | blocksize = btrfs_level_size(root, level); |
| 2118 | |
| 2119 | if (slot > 0) { |
| 2120 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2121 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
| 2122 | eb = btrfs_find_tree_block(root, block1, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2123 | /* |
| 2124 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2125 | * don't want to return eagain here. That will loop |
| 2126 | * forever |
| 2127 | */ |
| 2128 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2129 | block1 = 0; |
| 2130 | free_extent_buffer(eb); |
| 2131 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2132 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2133 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2134 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
| 2135 | eb = btrfs_find_tree_block(root, block2, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2136 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2137 | block2 = 0; |
| 2138 | free_extent_buffer(eb); |
| 2139 | } |
| 2140 | if (block1 || block2) { |
| 2141 | ret = -EAGAIN; |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2142 | |
| 2143 | /* release the whole path */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2144 | btrfs_release_path(path); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2145 | |
| 2146 | /* read the blocks */ |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2147 | if (block1) |
| 2148 | readahead_tree_block(root, block1, blocksize, 0); |
| 2149 | if (block2) |
| 2150 | readahead_tree_block(root, block2, blocksize, 0); |
| 2151 | |
| 2152 | if (block1) { |
| 2153 | eb = read_tree_block(root, block1, blocksize, 0); |
| 2154 | free_extent_buffer(eb); |
| 2155 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2156 | if (block2) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2157 | eb = read_tree_block(root, block2, blocksize, 0); |
| 2158 | free_extent_buffer(eb); |
| 2159 | } |
| 2160 | } |
| 2161 | return ret; |
| 2162 | } |
| 2163 | |
| 2164 | |
| 2165 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2166 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2167 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2168 | * operations on the tree might require changing key pointers higher up in the |
| 2169 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2170 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2171 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2172 | * the lock if the path points to the last slot in the block. This is part of |
| 2173 | * 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] | 2174 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2175 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2176 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2177 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2178 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2179 | int lowest_unlock, int min_write_lock_level, |
| 2180 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2181 | { |
| 2182 | int i; |
| 2183 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2184 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2185 | struct extent_buffer *t; |
| 2186 | |
| 2187 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2188 | if (!path->nodes[i]) |
| 2189 | break; |
| 2190 | if (!path->locks[i]) |
| 2191 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2192 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2193 | skip_level = i + 1; |
| 2194 | continue; |
| 2195 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2196 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2197 | u32 nritems; |
| 2198 | t = path->nodes[i]; |
| 2199 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2200 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2201 | skip_level = i + 1; |
| 2202 | continue; |
| 2203 | } |
| 2204 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2205 | if (skip_level < i && i >= lowest_unlock) |
| 2206 | no_skips = 1; |
| 2207 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2208 | t = path->nodes[i]; |
| 2209 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2210 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2211 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2212 | if (write_lock_level && |
| 2213 | i > min_write_lock_level && |
| 2214 | i <= *write_lock_level) { |
| 2215 | *write_lock_level = i - 1; |
| 2216 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2221 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2222 | * This releases any locks held in the path starting at level and |
| 2223 | * going all the way up to the root. |
| 2224 | * |
| 2225 | * btrfs_search_slot will keep the lock held on higher nodes in a few |
| 2226 | * corner cases, such as COW of the block at slot zero in the node. This |
| 2227 | * ignores those rules, and it should only be called when there are no |
| 2228 | * more updates to be done higher up in the tree. |
| 2229 | */ |
| 2230 | noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) |
| 2231 | { |
| 2232 | int i; |
| 2233 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2234 | if (path->keep_locks) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2235 | return; |
| 2236 | |
| 2237 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2238 | if (!path->nodes[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2239 | continue; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2240 | if (!path->locks[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2241 | continue; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2242 | btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2243 | path->locks[i] = 0; |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2248 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2249 | * in cache without setting the path to blocking. If we find the block |
| 2250 | * we return zero and the path is unchanged. |
| 2251 | * |
| 2252 | * If we can't find the block, we set the path blocking and do some |
| 2253 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2254 | */ |
| 2255 | static int |
| 2256 | read_block_for_search(struct btrfs_trans_handle *trans, |
| 2257 | struct btrfs_root *root, struct btrfs_path *p, |
| 2258 | struct extent_buffer **eb_ret, int level, int slot, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2259 | struct btrfs_key *key, u64 time_seq) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2260 | { |
| 2261 | u64 blocknr; |
| 2262 | u64 gen; |
| 2263 | u32 blocksize; |
| 2264 | struct extent_buffer *b = *eb_ret; |
| 2265 | struct extent_buffer *tmp; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2266 | int ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2267 | |
| 2268 | blocknr = btrfs_node_blockptr(b, slot); |
| 2269 | gen = btrfs_node_ptr_generation(b, slot); |
| 2270 | blocksize = btrfs_level_size(root, level - 1); |
| 2271 | |
| 2272 | tmp = btrfs_find_tree_block(root, blocknr, blocksize); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2273 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2274 | /* first we do an atomic uptodate check */ |
| 2275 | if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) { |
| 2276 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2277 | /* |
| 2278 | * we found an up to date block without |
| 2279 | * sleeping, return |
| 2280 | * right away |
| 2281 | */ |
| 2282 | *eb_ret = tmp; |
| 2283 | return 0; |
| 2284 | } |
| 2285 | /* the pages were up to date, but we failed |
| 2286 | * the generation number check. Do a full |
| 2287 | * read for the generation number that is correct. |
| 2288 | * We must do this without dropping locks so |
| 2289 | * we can trust our generation number |
| 2290 | */ |
| 2291 | free_extent_buffer(tmp); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2292 | btrfs_set_path_blocking(p); |
| 2293 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2294 | /* now we're allowed to do a blocking uptodate check */ |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2295 | tmp = read_tree_block(root, blocknr, blocksize, gen); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2296 | if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) { |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2297 | *eb_ret = tmp; |
| 2298 | return 0; |
| 2299 | } |
| 2300 | free_extent_buffer(tmp); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2301 | btrfs_release_path(p); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2302 | return -EIO; |
| 2303 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | * reduce lock contention at high levels |
| 2308 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2309 | * we read. Don't release the lock on the current |
| 2310 | * level because we need to walk this node to figure |
| 2311 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2312 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2313 | btrfs_unlock_up_safe(p, level + 1); |
| 2314 | btrfs_set_path_blocking(p); |
| 2315 | |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2316 | free_extent_buffer(tmp); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2317 | if (p->reada) |
| 2318 | reada_for_search(root, p, level, slot, key->objectid); |
| 2319 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2320 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2321 | |
| 2322 | ret = -EAGAIN; |
Yan, Zheng | 5bdd353 | 2010-05-26 11:20:30 -0400 | [diff] [blame] | 2323 | tmp = read_tree_block(root, blocknr, blocksize, 0); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2324 | if (tmp) { |
| 2325 | /* |
| 2326 | * If the read above didn't mark this buffer up to date, |
| 2327 | * it will never end up being up to date. Set ret to EIO now |
| 2328 | * and give up so that our caller doesn't loop forever |
| 2329 | * on our EAGAINs. |
| 2330 | */ |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2331 | if (!btrfs_buffer_uptodate(tmp, 0, 0)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2332 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2333 | free_extent_buffer(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2334 | } |
| 2335 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | /* |
| 2339 | * helper function for btrfs_search_slot. This does all of the checks |
| 2340 | * for node-level blocks and does any balancing required based on |
| 2341 | * the ins_len. |
| 2342 | * |
| 2343 | * If no extra work was required, zero is returned. If we had to |
| 2344 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2345 | * start over |
| 2346 | */ |
| 2347 | static int |
| 2348 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2349 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2350 | struct extent_buffer *b, int level, int ins_len, |
| 2351 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2352 | { |
| 2353 | int ret; |
| 2354 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
| 2355 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { |
| 2356 | int sret; |
| 2357 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2358 | if (*write_lock_level < level + 1) { |
| 2359 | *write_lock_level = level + 1; |
| 2360 | btrfs_release_path(p); |
| 2361 | goto again; |
| 2362 | } |
| 2363 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2364 | sret = reada_for_balance(root, p, level); |
| 2365 | if (sret) |
| 2366 | goto again; |
| 2367 | |
| 2368 | btrfs_set_path_blocking(p); |
| 2369 | sret = split_node(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2370 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2371 | |
| 2372 | BUG_ON(sret > 0); |
| 2373 | if (sret) { |
| 2374 | ret = sret; |
| 2375 | goto done; |
| 2376 | } |
| 2377 | b = p->nodes[level]; |
| 2378 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Chris Mason | cfbb930 | 2009-05-18 10:41:58 -0400 | [diff] [blame] | 2379 | BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2380 | int sret; |
| 2381 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2382 | if (*write_lock_level < level + 1) { |
| 2383 | *write_lock_level = level + 1; |
| 2384 | btrfs_release_path(p); |
| 2385 | goto again; |
| 2386 | } |
| 2387 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2388 | sret = reada_for_balance(root, p, level); |
| 2389 | if (sret) |
| 2390 | goto again; |
| 2391 | |
| 2392 | btrfs_set_path_blocking(p); |
| 2393 | sret = balance_level(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2394 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2395 | |
| 2396 | if (sret) { |
| 2397 | ret = sret; |
| 2398 | goto done; |
| 2399 | } |
| 2400 | b = p->nodes[level]; |
| 2401 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2402 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2403 | goto again; |
| 2404 | } |
| 2405 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2406 | } |
| 2407 | return 0; |
| 2408 | |
| 2409 | again: |
| 2410 | ret = -EAGAIN; |
| 2411 | done: |
| 2412 | return ret; |
| 2413 | } |
| 2414 | |
| 2415 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2416 | * look for key in the tree. path is filled in with nodes along the way |
| 2417 | * if key is found, we return zero and you can find the item in the leaf |
| 2418 | * level of the path (level 0) |
| 2419 | * |
| 2420 | * 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] | 2421 | * be inserted, and 1 is returned. If there are other errors during the |
| 2422 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2423 | * |
| 2424 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 2425 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 2426 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2427 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2428 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 2429 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 2430 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2431 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2432 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2433 | int slot; |
| 2434 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2435 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2436 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2437 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2438 | int root_lock; |
| 2439 | /* everything at write_lock_level or lower must be write locked */ |
| 2440 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2441 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2442 | int min_write_lock_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2443 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2444 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2445 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2446 | WARN_ON(p->nodes[0] != NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2447 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2448 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2449 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2450 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2451 | /* when we are removing items, we might have to go up to level |
| 2452 | * two as we update tree pointers Make sure we keep write |
| 2453 | * for those levels as well |
| 2454 | */ |
| 2455 | write_lock_level = 2; |
| 2456 | } else if (ins_len > 0) { |
| 2457 | /* |
| 2458 | * for inserting items, make sure we have a write lock on |
| 2459 | * level 1 so we can update keys |
| 2460 | */ |
| 2461 | write_lock_level = 1; |
| 2462 | } |
| 2463 | |
| 2464 | if (!cow) |
| 2465 | write_lock_level = -1; |
| 2466 | |
| 2467 | if (cow && (p->keep_locks || p->lowest_level)) |
| 2468 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2469 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2470 | min_write_lock_level = write_lock_level; |
| 2471 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2472 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2473 | /* |
| 2474 | * we try very hard to do read locks on the root |
| 2475 | */ |
| 2476 | root_lock = BTRFS_READ_LOCK; |
| 2477 | level = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2478 | if (p->search_commit_root) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2479 | /* |
| 2480 | * the commit roots are read only |
| 2481 | * so we always do read locks |
| 2482 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2483 | b = root->commit_root; |
| 2484 | extent_buffer_get(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2485 | level = btrfs_header_level(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2486 | if (!p->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2487 | btrfs_tree_read_lock(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2488 | } else { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2489 | if (p->skip_locking) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2490 | b = btrfs_root_node(root); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2491 | level = btrfs_header_level(b); |
| 2492 | } else { |
| 2493 | /* we don't know the level of the root node |
| 2494 | * until we actually have it read locked |
| 2495 | */ |
| 2496 | b = btrfs_read_lock_root_node(root); |
| 2497 | level = btrfs_header_level(b); |
| 2498 | if (level <= write_lock_level) { |
| 2499 | /* whoops, must trade for write lock */ |
| 2500 | btrfs_tree_read_unlock(b); |
| 2501 | free_extent_buffer(b); |
| 2502 | b = btrfs_lock_root_node(root); |
| 2503 | root_lock = BTRFS_WRITE_LOCK; |
| 2504 | |
| 2505 | /* the level might have changed, check again */ |
| 2506 | level = btrfs_header_level(b); |
| 2507 | } |
| 2508 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2509 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2510 | p->nodes[level] = b; |
| 2511 | if (!p->skip_locking) |
| 2512 | p->locks[level] = root_lock; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2513 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2514 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2515 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2516 | |
| 2517 | /* |
| 2518 | * setup the path here so we can release it under lock |
| 2519 | * contention with the cow code |
| 2520 | */ |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2521 | if (cow) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2522 | /* |
| 2523 | * if we don't really need to cow this block |
| 2524 | * then we don't want to set the path blocking, |
| 2525 | * so we test it here |
| 2526 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2527 | if (!should_cow_block(trans, root, b)) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2528 | goto cow_done; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2529 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2530 | btrfs_set_path_blocking(p); |
| 2531 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2532 | /* |
| 2533 | * must have write locks on this node and the |
| 2534 | * parent |
| 2535 | */ |
| 2536 | if (level + 1 > write_lock_level) { |
| 2537 | write_lock_level = level + 1; |
| 2538 | btrfs_release_path(p); |
| 2539 | goto again; |
| 2540 | } |
| 2541 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2542 | err = btrfs_cow_block(trans, root, b, |
| 2543 | p->nodes[level + 1], |
| 2544 | p->slots[level + 1], &b); |
| 2545 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2546 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2547 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2548 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2549 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2550 | cow_done: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2551 | BUG_ON(!cow && ins_len); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2552 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2553 | p->nodes[level] = b; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2554 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2555 | |
| 2556 | /* |
| 2557 | * we have a lock on b and as long as we aren't changing |
| 2558 | * the tree, there is no way to for the items in b to change. |
| 2559 | * It is safe to drop the lock on our parent before we |
| 2560 | * go through the expensive btree search on b. |
| 2561 | * |
| 2562 | * If cow is true, then we might be changing slot zero, |
| 2563 | * which may require changing the parent. So, we can't |
| 2564 | * drop the lock until after we know which slot we're |
| 2565 | * operating on. |
| 2566 | */ |
| 2567 | if (!cow) |
| 2568 | btrfs_unlock_up_safe(p, level + 1); |
| 2569 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2570 | ret = bin_search(b, key, level, &slot); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2571 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2572 | if (level != 0) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2573 | int dec = 0; |
| 2574 | if (ret && slot > 0) { |
| 2575 | dec = 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2576 | slot -= 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2577 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2578 | p->slots[level] = slot; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2579 | err = setup_nodes_for_search(trans, root, p, b, level, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2580 | ins_len, &write_lock_level); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2581 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2582 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2583 | if (err) { |
| 2584 | ret = err; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2585 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2586 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2587 | b = p->nodes[level]; |
| 2588 | slot = p->slots[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2589 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2590 | /* |
| 2591 | * slot 0 is special, if we change the key |
| 2592 | * we have to update the parent pointer |
| 2593 | * which means we must have a write lock |
| 2594 | * on the parent |
| 2595 | */ |
| 2596 | if (slot == 0 && cow && |
| 2597 | write_lock_level < level + 1) { |
| 2598 | write_lock_level = level + 1; |
| 2599 | btrfs_release_path(p); |
| 2600 | goto again; |
| 2601 | } |
| 2602 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2603 | unlock_up(p, level, lowest_unlock, |
| 2604 | min_write_lock_level, &write_lock_level); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 2605 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2606 | if (level == lowest_level) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2607 | if (dec) |
| 2608 | p->slots[level]++; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2609 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2610 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2611 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2612 | err = read_block_for_search(trans, root, p, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2613 | &b, level, slot, key, 0); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2614 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2615 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2616 | if (err) { |
| 2617 | ret = err; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2618 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2619 | } |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2620 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2621 | if (!p->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2622 | level = btrfs_header_level(b); |
| 2623 | if (level <= write_lock_level) { |
| 2624 | err = btrfs_try_tree_write_lock(b); |
| 2625 | if (!err) { |
| 2626 | btrfs_set_path_blocking(p); |
| 2627 | btrfs_tree_lock(b); |
| 2628 | btrfs_clear_path_blocking(p, b, |
| 2629 | BTRFS_WRITE_LOCK); |
| 2630 | } |
| 2631 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2632 | } else { |
| 2633 | err = btrfs_try_tree_read_lock(b); |
| 2634 | if (!err) { |
| 2635 | btrfs_set_path_blocking(p); |
| 2636 | btrfs_tree_read_lock(b); |
| 2637 | btrfs_clear_path_blocking(p, b, |
| 2638 | BTRFS_READ_LOCK); |
| 2639 | } |
| 2640 | p->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2641 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2642 | p->nodes[level] = b; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2643 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2644 | } else { |
| 2645 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2646 | if (ins_len > 0 && |
| 2647 | btrfs_leaf_free_space(root, b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2648 | if (write_lock_level < 1) { |
| 2649 | write_lock_level = 1; |
| 2650 | btrfs_release_path(p); |
| 2651 | goto again; |
| 2652 | } |
| 2653 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2654 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2655 | err = split_leaf(trans, root, key, |
| 2656 | p, ins_len, ret == 0); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2657 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2658 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2659 | BUG_ON(err > 0); |
| 2660 | if (err) { |
| 2661 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2662 | goto done; |
| 2663 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2664 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2665 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2666 | unlock_up(p, level, lowest_unlock, |
| 2667 | min_write_lock_level, &write_lock_level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2668 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2669 | } |
| 2670 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2671 | ret = 1; |
| 2672 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2673 | /* |
| 2674 | * we don't really know what they plan on doing with the path |
| 2675 | * from here on, so for now just mark it as blocking |
| 2676 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2677 | if (!p->leave_spinning) |
| 2678 | btrfs_set_path_blocking(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2679 | if (ret < 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2680 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2681 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2682 | } |
| 2683 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2684 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2685 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2686 | * current state of the tree together with the operations recorded in the tree |
| 2687 | * modification log to search for the key in a previous version of this tree, as |
| 2688 | * denoted by the time_seq parameter. |
| 2689 | * |
| 2690 | * Naturally, there is no support for insert, delete or cow operations. |
| 2691 | * |
| 2692 | * The resulting path and return value will be set up as if we called |
| 2693 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2694 | */ |
| 2695 | int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 2696 | struct btrfs_path *p, u64 time_seq) |
| 2697 | { |
| 2698 | struct extent_buffer *b; |
| 2699 | int slot; |
| 2700 | int ret; |
| 2701 | int err; |
| 2702 | int level; |
| 2703 | int lowest_unlock = 1; |
| 2704 | u8 lowest_level = 0; |
| 2705 | |
| 2706 | lowest_level = p->lowest_level; |
| 2707 | WARN_ON(p->nodes[0] != NULL); |
| 2708 | |
| 2709 | if (p->search_commit_root) { |
| 2710 | BUG_ON(time_seq); |
| 2711 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2712 | } |
| 2713 | |
| 2714 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2715 | b = get_old_root(root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2716 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2717 | p->locks[level] = BTRFS_READ_LOCK; |
| 2718 | |
| 2719 | while (b) { |
| 2720 | level = btrfs_header_level(b); |
| 2721 | p->nodes[level] = b; |
| 2722 | btrfs_clear_path_blocking(p, NULL, 0); |
| 2723 | |
| 2724 | /* |
| 2725 | * we have a lock on b and as long as we aren't changing |
| 2726 | * the tree, there is no way to for the items in b to change. |
| 2727 | * It is safe to drop the lock on our parent before we |
| 2728 | * go through the expensive btree search on b. |
| 2729 | */ |
| 2730 | btrfs_unlock_up_safe(p, level + 1); |
| 2731 | |
| 2732 | ret = bin_search(b, key, level, &slot); |
| 2733 | |
| 2734 | if (level != 0) { |
| 2735 | int dec = 0; |
| 2736 | if (ret && slot > 0) { |
| 2737 | dec = 1; |
| 2738 | slot -= 1; |
| 2739 | } |
| 2740 | p->slots[level] = slot; |
| 2741 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2742 | |
| 2743 | if (level == lowest_level) { |
| 2744 | if (dec) |
| 2745 | p->slots[level]++; |
| 2746 | goto done; |
| 2747 | } |
| 2748 | |
| 2749 | err = read_block_for_search(NULL, root, p, &b, level, |
| 2750 | slot, key, time_seq); |
| 2751 | if (err == -EAGAIN) |
| 2752 | goto again; |
| 2753 | if (err) { |
| 2754 | ret = err; |
| 2755 | goto done; |
| 2756 | } |
| 2757 | |
| 2758 | level = btrfs_header_level(b); |
| 2759 | err = btrfs_try_tree_read_lock(b); |
| 2760 | if (!err) { |
| 2761 | btrfs_set_path_blocking(p); |
| 2762 | btrfs_tree_read_lock(b); |
| 2763 | btrfs_clear_path_blocking(p, b, |
| 2764 | BTRFS_READ_LOCK); |
| 2765 | } |
| 2766 | p->locks[level] = BTRFS_READ_LOCK; |
| 2767 | p->nodes[level] = b; |
| 2768 | b = tree_mod_log_rewind(root->fs_info, b, time_seq); |
| 2769 | if (b != p->nodes[level]) { |
| 2770 | btrfs_tree_unlock_rw(p->nodes[level], |
| 2771 | p->locks[level]); |
| 2772 | p->locks[level] = 0; |
| 2773 | p->nodes[level] = b; |
| 2774 | } |
| 2775 | } else { |
| 2776 | p->slots[level] = slot; |
| 2777 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2778 | goto done; |
| 2779 | } |
| 2780 | } |
| 2781 | ret = 1; |
| 2782 | done: |
| 2783 | if (!p->leave_spinning) |
| 2784 | btrfs_set_path_blocking(p); |
| 2785 | if (ret < 0) |
| 2786 | btrfs_release_path(p); |
| 2787 | |
| 2788 | return ret; |
| 2789 | } |
| 2790 | |
| 2791 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2792 | * helper to use instead of search slot if no exact match is needed but |
| 2793 | * instead the next or previous item should be returned. |
| 2794 | * When find_higher is true, the next higher item is returned, the next lower |
| 2795 | * otherwise. |
| 2796 | * When return_any and find_higher are both true, and no higher item is found, |
| 2797 | * return the next lower instead. |
| 2798 | * When return_any is true and find_higher is false, and no lower item is found, |
| 2799 | * return the next higher instead. |
| 2800 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 2801 | * < 0 on error |
| 2802 | */ |
| 2803 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
| 2804 | struct btrfs_key *key, struct btrfs_path *p, |
| 2805 | int find_higher, int return_any) |
| 2806 | { |
| 2807 | int ret; |
| 2808 | struct extent_buffer *leaf; |
| 2809 | |
| 2810 | again: |
| 2811 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2812 | if (ret <= 0) |
| 2813 | return ret; |
| 2814 | /* |
| 2815 | * a return value of 1 means the path is at the position where the |
| 2816 | * item should be inserted. Normally this is the next bigger item, |
| 2817 | * but in case the previous item is the last in a leaf, path points |
| 2818 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 2819 | * item. |
| 2820 | */ |
| 2821 | leaf = p->nodes[0]; |
| 2822 | |
| 2823 | if (find_higher) { |
| 2824 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2825 | ret = btrfs_next_leaf(root, p); |
| 2826 | if (ret <= 0) |
| 2827 | return ret; |
| 2828 | if (!return_any) |
| 2829 | return 1; |
| 2830 | /* |
| 2831 | * no higher item found, return the next |
| 2832 | * lower instead |
| 2833 | */ |
| 2834 | return_any = 0; |
| 2835 | find_higher = 0; |
| 2836 | btrfs_release_path(p); |
| 2837 | goto again; |
| 2838 | } |
| 2839 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2840 | if (p->slots[0] == 0) { |
| 2841 | ret = btrfs_prev_leaf(root, p); |
| 2842 | if (ret < 0) |
| 2843 | return ret; |
| 2844 | if (!ret) { |
| 2845 | p->slots[0] = btrfs_header_nritems(leaf) - 1; |
| 2846 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2847 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2848 | if (!return_any) |
| 2849 | return 1; |
| 2850 | /* |
| 2851 | * no lower item found, return the next |
| 2852 | * higher instead |
| 2853 | */ |
| 2854 | return_any = 0; |
| 2855 | find_higher = 1; |
| 2856 | btrfs_release_path(p); |
| 2857 | goto again; |
| 2858 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2859 | --p->slots[0]; |
| 2860 | } |
| 2861 | } |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2866 | * adjust the pointers going up the tree, starting at level |
| 2867 | * making sure the right key of each node is points to 'key'. |
| 2868 | * This is used after shifting pointers to the left, so it stops |
| 2869 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 2870 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2871 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2872 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2873 | static void fixup_low_keys(struct btrfs_trans_handle *trans, |
| 2874 | struct btrfs_root *root, struct btrfs_path *path, |
| 2875 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2876 | { |
| 2877 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2878 | struct extent_buffer *t; |
| 2879 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2880 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2881 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2882 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2883 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2884 | t = path->nodes[i]; |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2885 | tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2886 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2887 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2888 | if (tslot != 0) |
| 2889 | break; |
| 2890 | } |
| 2891 | } |
| 2892 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2893 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2894 | * update item key. |
| 2895 | * |
| 2896 | * This function isn't completely safe. It's the caller's responsibility |
| 2897 | * that the new key won't break the order |
| 2898 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2899 | void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, |
| 2900 | struct btrfs_root *root, struct btrfs_path *path, |
| 2901 | struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2902 | { |
| 2903 | struct btrfs_disk_key disk_key; |
| 2904 | struct extent_buffer *eb; |
| 2905 | int slot; |
| 2906 | |
| 2907 | eb = path->nodes[0]; |
| 2908 | slot = path->slots[0]; |
| 2909 | if (slot > 0) { |
| 2910 | btrfs_item_key(eb, &disk_key, slot - 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2911 | BUG_ON(comp_keys(&disk_key, new_key) >= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2912 | } |
| 2913 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 2914 | btrfs_item_key(eb, &disk_key, slot + 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2915 | BUG_ON(comp_keys(&disk_key, new_key) <= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 2919 | btrfs_set_item_key(eb, &disk_key, slot); |
| 2920 | btrfs_mark_buffer_dirty(eb); |
| 2921 | if (slot == 0) |
| 2922 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2926 | * 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] | 2927 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2928 | * |
| 2929 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 2930 | * 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] | 2931 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2932 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 2933 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2934 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2935 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2936 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2937 | int src_nritems; |
| 2938 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2939 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2940 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2941 | src_nritems = btrfs_header_nritems(src); |
| 2942 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2943 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2944 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 2945 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2946 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2947 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2948 | return 1; |
| 2949 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2950 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2951 | return 1; |
| 2952 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2953 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2954 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2955 | if (push_items < src_nritems) { |
| 2956 | /* leave at least 8 pointers in the node if |
| 2957 | * we aren't going to empty it |
| 2958 | */ |
| 2959 | if (src_nritems - push_items < 8) { |
| 2960 | if (push_items <= 8) |
| 2961 | return 1; |
| 2962 | push_items -= 8; |
| 2963 | } |
| 2964 | } |
| 2965 | } else |
| 2966 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2967 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2968 | tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0, |
| 2969 | push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2970 | copy_extent_buffer(dst, src, |
| 2971 | btrfs_node_key_ptr_offset(dst_nritems), |
| 2972 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2973 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2974 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2975 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame^] | 2976 | /* |
| 2977 | * don't call tree_mod_log_eb_move here, key removal was already |
| 2978 | * fully logged by tree_mod_log_eb_copy above. |
| 2979 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2980 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 2981 | btrfs_node_key_ptr_offset(push_items), |
| 2982 | (src_nritems - push_items) * |
| 2983 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2984 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2985 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 2986 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 2987 | btrfs_mark_buffer_dirty(src); |
| 2988 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2989 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2990 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2991 | } |
| 2992 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2993 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2994 | * try to push data from one node into the next node right in the |
| 2995 | * tree. |
| 2996 | * |
| 2997 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 2998 | * error, and > 0 if there was no room in the right hand block. |
| 2999 | * |
| 3000 | * this will only push up to 1/2 the contents of the left node over |
| 3001 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3002 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 3003 | struct btrfs_root *root, |
| 3004 | struct extent_buffer *dst, |
| 3005 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3006 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3007 | int push_items = 0; |
| 3008 | int max_push; |
| 3009 | int src_nritems; |
| 3010 | int dst_nritems; |
| 3011 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3012 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3013 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3014 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3015 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3016 | src_nritems = btrfs_header_nritems(src); |
| 3017 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3018 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3019 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3020 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3021 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3022 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3023 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3024 | |
| 3025 | max_push = src_nritems / 2 + 1; |
| 3026 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3027 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3028 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3029 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3030 | if (max_push < push_items) |
| 3031 | push_items = max_push; |
| 3032 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3033 | 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] | 3034 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3035 | btrfs_node_key_ptr_offset(0), |
| 3036 | (dst_nritems) * |
| 3037 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3038 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3039 | tree_mod_log_eb_copy(root->fs_info, dst, src, 0, |
| 3040 | src_nritems - push_items, push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3041 | copy_extent_buffer(dst, src, |
| 3042 | btrfs_node_key_ptr_offset(0), |
| 3043 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3044 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3045 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3046 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3047 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3048 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3049 | btrfs_mark_buffer_dirty(src); |
| 3050 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3051 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3052 | return ret; |
| 3053 | } |
| 3054 | |
| 3055 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3056 | * helper function to insert a new root level in the tree. |
| 3057 | * A new node is allocated, and a single item is inserted to |
| 3058 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3059 | * |
| 3060 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3061 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3062 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3063 | struct btrfs_root *root, |
| 3064 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3065 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3066 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3067 | struct extent_buffer *lower; |
| 3068 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3069 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3070 | struct btrfs_disk_key lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3071 | |
| 3072 | BUG_ON(path->nodes[level]); |
| 3073 | BUG_ON(path->nodes[level-1] != root->node); |
| 3074 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3075 | lower = path->nodes[level-1]; |
| 3076 | if (level == 1) |
| 3077 | btrfs_item_key(lower, &lower_key, 0); |
| 3078 | else |
| 3079 | btrfs_node_key(lower, &lower_key, 0); |
| 3080 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3081 | c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3082 | root->root_key.objectid, &lower_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3083 | level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3084 | if (IS_ERR(c)) |
| 3085 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3086 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3087 | root_add_used(root, root->nodesize); |
| 3088 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3089 | memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3090 | btrfs_set_header_nritems(c, 1); |
| 3091 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3092 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3093 | btrfs_set_header_generation(c, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3094 | btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3095 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3096 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3097 | write_extent_buffer(c, root->fs_info->fsid, |
| 3098 | (unsigned long)btrfs_header_fsid(c), |
| 3099 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3100 | |
| 3101 | write_extent_buffer(c, root->fs_info->chunk_tree_uuid, |
| 3102 | (unsigned long)btrfs_header_chunk_tree_uuid(c), |
| 3103 | BTRFS_UUID_SIZE); |
| 3104 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3105 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3106 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3107 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3108 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3109 | |
| 3110 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3111 | |
| 3112 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3113 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3114 | old = root->node; |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3115 | tree_mod_log_set_root_pointer(root, c); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3116 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3117 | |
| 3118 | /* the super has an extra ref to root->node */ |
| 3119 | free_extent_buffer(old); |
| 3120 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3121 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3122 | extent_buffer_get(c); |
| 3123 | path->nodes[level] = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 3124 | path->locks[level] = BTRFS_WRITE_LOCK; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3125 | path->slots[level] = 0; |
| 3126 | return 0; |
| 3127 | } |
| 3128 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3129 | /* |
| 3130 | * worker function to insert a single pointer in a node. |
| 3131 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3132 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3133 | * slot and level indicate where you want the key to go, and |
| 3134 | * blocknr is the block the key points to. |
| 3135 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3136 | static void insert_ptr(struct btrfs_trans_handle *trans, |
| 3137 | struct btrfs_root *root, struct btrfs_path *path, |
| 3138 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3139 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3140 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3141 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3142 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3143 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3144 | |
| 3145 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3146 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3147 | lower = path->nodes[level]; |
| 3148 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3149 | BUG_ON(slot > nritems); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3150 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3151 | if (slot != nritems) { |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3152 | if (level) |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3153 | tree_mod_log_eb_move(root->fs_info, lower, slot + 1, |
| 3154 | slot, nritems - slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3155 | memmove_extent_buffer(lower, |
| 3156 | btrfs_node_key_ptr_offset(slot + 1), |
| 3157 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3158 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3159 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3160 | if (level) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3161 | ret = tree_mod_log_insert_key(root->fs_info, lower, slot, |
| 3162 | MOD_LOG_KEY_ADD); |
| 3163 | BUG_ON(ret < 0); |
| 3164 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3165 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3166 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3167 | WARN_ON(trans->transid == 0); |
| 3168 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3169 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3170 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3171 | } |
| 3172 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3173 | /* |
| 3174 | * split the node at the specified level in path in two. |
| 3175 | * The path is corrected to point to the appropriate node after the split |
| 3176 | * |
| 3177 | * Before splitting this tries to make some room in the node by pushing |
| 3178 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3179 | * |
| 3180 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3181 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3182 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3183 | struct btrfs_root *root, |
| 3184 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3185 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3186 | struct extent_buffer *c; |
| 3187 | struct extent_buffer *split; |
| 3188 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3189 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3190 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3191 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3192 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3193 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3194 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3195 | if (c == root->node) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3196 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3197 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3198 | if (ret) |
| 3199 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3200 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3201 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3202 | c = path->nodes[level]; |
| 3203 | if (!ret && btrfs_header_nritems(c) < |
Chris Mason | c448acf | 2008-04-24 09:34:34 -0400 | [diff] [blame] | 3204 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3205 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3206 | if (ret < 0) |
| 3207 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3208 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3209 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3210 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3211 | mid = (c_nritems + 1) / 2; |
| 3212 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3213 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3214 | split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3215 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3216 | &disk_key, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3217 | if (IS_ERR(split)) |
| 3218 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3219 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3220 | root_add_used(root, root->nodesize); |
| 3221 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3222 | memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3223 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3224 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3225 | btrfs_set_header_generation(split, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3226 | btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3227 | btrfs_set_header_owner(split, root->root_key.objectid); |
| 3228 | write_extent_buffer(split, root->fs_info->fsid, |
| 3229 | (unsigned long)btrfs_header_fsid(split), |
| 3230 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3231 | write_extent_buffer(split, root->fs_info->chunk_tree_uuid, |
| 3232 | (unsigned long)btrfs_header_chunk_tree_uuid(split), |
| 3233 | BTRFS_UUID_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3234 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3235 | 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] | 3236 | copy_extent_buffer(split, c, |
| 3237 | btrfs_node_key_ptr_offset(0), |
| 3238 | btrfs_node_key_ptr_offset(mid), |
| 3239 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3240 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3241 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3242 | ret = 0; |
| 3243 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3244 | btrfs_mark_buffer_dirty(c); |
| 3245 | btrfs_mark_buffer_dirty(split); |
| 3246 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3247 | insert_ptr(trans, root, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3248 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3249 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3250 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3251 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3252 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3253 | free_extent_buffer(c); |
| 3254 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3255 | path->slots[level + 1] += 1; |
| 3256 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3257 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3258 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3259 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3260 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3261 | } |
| 3262 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3263 | /* |
| 3264 | * how many bytes are required to store the items in a leaf. start |
| 3265 | * and nr indicate which items in the leaf to check. This totals up the |
| 3266 | * space used both by the item structs and the item data |
| 3267 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3268 | 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] | 3269 | { |
| 3270 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3271 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3272 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3273 | |
| 3274 | if (!nr) |
| 3275 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3276 | data_len = btrfs_item_end_nr(l, start); |
| 3277 | data_len = data_len - btrfs_item_offset_nr(l, end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3278 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3279 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3280 | return data_len; |
| 3281 | } |
| 3282 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3283 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3284 | * The space between the end of the leaf items and |
| 3285 | * the start of the leaf data. IOW, how much room |
| 3286 | * the leaf has left for both items and data |
| 3287 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3288 | noinline int btrfs_leaf_free_space(struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3289 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3290 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3291 | int nritems = btrfs_header_nritems(leaf); |
| 3292 | int ret; |
| 3293 | ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 3294 | if (ret < 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3295 | printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " |
| 3296 | "used %d nritems %d\n", |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 3297 | ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3298 | leaf_space_used(leaf, 0, nritems), nritems); |
| 3299 | } |
| 3300 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3301 | } |
| 3302 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3303 | /* |
| 3304 | * min slot controls the lowest index we're willing to push to the |
| 3305 | * right. We'll push up to and including min_slot, but no lower |
| 3306 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3307 | static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, |
| 3308 | struct btrfs_root *root, |
| 3309 | struct btrfs_path *path, |
| 3310 | int data_size, int empty, |
| 3311 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3312 | int free_space, u32 left_nritems, |
| 3313 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3314 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3315 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3316 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3317 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3318 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3319 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3320 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3321 | int push_space = 0; |
| 3322 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3323 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3324 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3325 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3326 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3327 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3328 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3329 | btrfs_init_map_token(&token); |
| 3330 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3331 | if (empty) |
| 3332 | nr = 0; |
| 3333 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3334 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3335 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3336 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3337 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3338 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3339 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3340 | i = left_nritems - 1; |
| 3341 | while (i >= nr) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3342 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3343 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3344 | if (!empty && push_items > 0) { |
| 3345 | if (path->slots[0] > i) |
| 3346 | break; |
| 3347 | if (path->slots[0] == i) { |
| 3348 | int space = btrfs_leaf_free_space(root, left); |
| 3349 | if (space + push_space * 2 > free_space) |
| 3350 | break; |
| 3351 | } |
| 3352 | } |
| 3353 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3354 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3355 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3356 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3357 | this_item_size = btrfs_item_size(left, item); |
| 3358 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3359 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3360 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3361 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3362 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3363 | if (i == 0) |
| 3364 | break; |
| 3365 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3366 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3367 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3368 | if (push_items == 0) |
| 3369 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3370 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3371 | if (!empty && push_items == left_nritems) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3372 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3373 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3374 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3375 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3376 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3377 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3378 | push_space -= leaf_data_end(root, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3379 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3380 | /* make room in the right data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3381 | data_end = leaf_data_end(root, right); |
| 3382 | memmove_extent_buffer(right, |
| 3383 | btrfs_leaf_data(right) + data_end - push_space, |
| 3384 | btrfs_leaf_data(right) + data_end, |
| 3385 | BTRFS_LEAF_DATA_SIZE(root) - data_end); |
| 3386 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3387 | /* copy from the left data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3388 | copy_extent_buffer(right, left, btrfs_leaf_data(right) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3389 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3390 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 3391 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3392 | |
| 3393 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3394 | btrfs_item_nr_offset(0), |
| 3395 | right_nritems * sizeof(struct btrfs_item)); |
| 3396 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3397 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3398 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3399 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3400 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3401 | |
| 3402 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3403 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3404 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3405 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3406 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3407 | item = btrfs_item_nr(right, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3408 | push_space -= btrfs_token_item_size(right, item, &token); |
| 3409 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3410 | } |
| 3411 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3412 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3413 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3414 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3415 | if (left_nritems) |
| 3416 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3417 | else |
| 3418 | clean_tree_block(trans, root, left); |
| 3419 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3420 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3421 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3422 | btrfs_item_key(right, &disk_key, 0); |
| 3423 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3424 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3425 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3426 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3427 | if (path->slots[0] >= left_nritems) { |
| 3428 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3429 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 3430 | clean_tree_block(trans, root, path->nodes[0]); |
| 3431 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3432 | free_extent_buffer(path->nodes[0]); |
| 3433 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3434 | path->slots[1] += 1; |
| 3435 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3436 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3437 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3438 | } |
| 3439 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3440 | |
| 3441 | out_unlock: |
| 3442 | btrfs_tree_unlock(right); |
| 3443 | free_extent_buffer(right); |
| 3444 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3445 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3446 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3447 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3448 | * push some data in the path leaf to the right, trying to free up at |
| 3449 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3450 | * |
| 3451 | * returns 1 if the push failed because the other node didn't have enough |
| 3452 | * 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] | 3453 | * |
| 3454 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3455 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3456 | */ |
| 3457 | 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] | 3458 | *root, struct btrfs_path *path, |
| 3459 | int min_data_size, int data_size, |
| 3460 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3461 | { |
| 3462 | struct extent_buffer *left = path->nodes[0]; |
| 3463 | struct extent_buffer *right; |
| 3464 | struct extent_buffer *upper; |
| 3465 | int slot; |
| 3466 | int free_space; |
| 3467 | u32 left_nritems; |
| 3468 | int ret; |
| 3469 | |
| 3470 | if (!path->nodes[1]) |
| 3471 | return 1; |
| 3472 | |
| 3473 | slot = path->slots[1]; |
| 3474 | upper = path->nodes[1]; |
| 3475 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3476 | return 1; |
| 3477 | |
| 3478 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3479 | |
| 3480 | right = read_node_slot(root, upper, slot + 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3481 | if (right == NULL) |
| 3482 | return 1; |
| 3483 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3484 | btrfs_tree_lock(right); |
| 3485 | btrfs_set_lock_blocking(right); |
| 3486 | |
| 3487 | free_space = btrfs_leaf_free_space(root, right); |
| 3488 | if (free_space < data_size) |
| 3489 | goto out_unlock; |
| 3490 | |
| 3491 | /* cow and double check */ |
| 3492 | ret = btrfs_cow_block(trans, root, right, upper, |
| 3493 | slot + 1, &right); |
| 3494 | if (ret) |
| 3495 | goto out_unlock; |
| 3496 | |
| 3497 | free_space = btrfs_leaf_free_space(root, right); |
| 3498 | if (free_space < data_size) |
| 3499 | goto out_unlock; |
| 3500 | |
| 3501 | left_nritems = btrfs_header_nritems(left); |
| 3502 | if (left_nritems == 0) |
| 3503 | goto out_unlock; |
| 3504 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3505 | return __push_leaf_right(trans, root, path, min_data_size, empty, |
| 3506 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3507 | out_unlock: |
| 3508 | btrfs_tree_unlock(right); |
| 3509 | free_extent_buffer(right); |
| 3510 | return 1; |
| 3511 | } |
| 3512 | |
| 3513 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3514 | * push some data in the path leaf to the left, trying to free up at |
| 3515 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3516 | * |
| 3517 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3518 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3519 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3520 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3521 | static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, |
| 3522 | struct btrfs_root *root, |
| 3523 | struct btrfs_path *path, int data_size, |
| 3524 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3525 | int free_space, u32 right_nritems, |
| 3526 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3527 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3528 | struct btrfs_disk_key disk_key; |
| 3529 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3530 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3531 | int push_space = 0; |
| 3532 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3533 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3534 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3535 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3536 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3537 | u32 this_item_size; |
| 3538 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3539 | struct btrfs_map_token token; |
| 3540 | |
| 3541 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3542 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3543 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3544 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3545 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3546 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3547 | |
| 3548 | for (i = 0; i < nr; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3549 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3550 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3551 | if (!empty && push_items > 0) { |
| 3552 | if (path->slots[0] < i) |
| 3553 | break; |
| 3554 | if (path->slots[0] == i) { |
| 3555 | int space = btrfs_leaf_free_space(root, right); |
| 3556 | if (space + push_space * 2 > free_space) |
| 3557 | break; |
| 3558 | } |
| 3559 | } |
| 3560 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3561 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3562 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3563 | |
| 3564 | this_item_size = btrfs_item_size(right, item); |
| 3565 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3566 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3567 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3568 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3569 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3570 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3571 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3572 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3573 | ret = 1; |
| 3574 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3575 | } |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3576 | if (!empty && push_items == btrfs_header_nritems(right)) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3577 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3578 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3579 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3580 | copy_extent_buffer(left, right, |
| 3581 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3582 | btrfs_item_nr_offset(0), |
| 3583 | push_items * sizeof(struct btrfs_item)); |
| 3584 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3585 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3586 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3587 | |
| 3588 | copy_extent_buffer(left, right, btrfs_leaf_data(left) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3589 | leaf_data_end(root, left) - push_space, |
| 3590 | btrfs_leaf_data(right) + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3591 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3592 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3593 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3594 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3595 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3596 | 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] | 3597 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3598 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3599 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3600 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3601 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3602 | ioff = btrfs_token_item_offset(left, item, &token); |
| 3603 | btrfs_set_token_item_offset(left, item, |
| 3604 | ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size), |
| 3605 | &token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3606 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3607 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3608 | |
| 3609 | /* fixup right node */ |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3610 | if (push_items > right_nritems) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3611 | printk(KERN_CRIT "push items %d nr %u\n", push_items, |
| 3612 | right_nritems); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3613 | WARN_ON(1); |
| 3614 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3615 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3616 | if (push_items < right_nritems) { |
| 3617 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
| 3618 | leaf_data_end(root, right); |
| 3619 | memmove_extent_buffer(right, btrfs_leaf_data(right) + |
| 3620 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3621 | btrfs_leaf_data(right) + |
| 3622 | leaf_data_end(root, right), push_space); |
| 3623 | |
| 3624 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3625 | btrfs_item_nr_offset(push_items), |
| 3626 | (btrfs_header_nritems(right) - push_items) * |
| 3627 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3628 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3629 | right_nritems -= push_items; |
| 3630 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3631 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3632 | for (i = 0; i < right_nritems; i++) { |
| 3633 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3634 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3635 | push_space = push_space - btrfs_token_item_size(right, |
| 3636 | item, &token); |
| 3637 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3638 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3639 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3640 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3641 | if (right_nritems) |
| 3642 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3643 | else |
| 3644 | clean_tree_block(trans, root, right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3645 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3646 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3647 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3648 | |
| 3649 | /* then fixup the leaf pointer in the path */ |
| 3650 | if (path->slots[0] < push_items) { |
| 3651 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3652 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3653 | free_extent_buffer(path->nodes[0]); |
| 3654 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3655 | path->slots[1] -= 1; |
| 3656 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3657 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3658 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3659 | path->slots[0] -= push_items; |
| 3660 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3661 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3662 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3663 | out: |
| 3664 | btrfs_tree_unlock(left); |
| 3665 | free_extent_buffer(left); |
| 3666 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3667 | } |
| 3668 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3669 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3670 | * push some data in the path leaf to the left, trying to free up at |
| 3671 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3672 | * |
| 3673 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3674 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3675 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3676 | */ |
| 3677 | 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] | 3678 | *root, struct btrfs_path *path, int min_data_size, |
| 3679 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3680 | { |
| 3681 | struct extent_buffer *right = path->nodes[0]; |
| 3682 | struct extent_buffer *left; |
| 3683 | int slot; |
| 3684 | int free_space; |
| 3685 | u32 right_nritems; |
| 3686 | int ret = 0; |
| 3687 | |
| 3688 | slot = path->slots[1]; |
| 3689 | if (slot == 0) |
| 3690 | return 1; |
| 3691 | if (!path->nodes[1]) |
| 3692 | return 1; |
| 3693 | |
| 3694 | right_nritems = btrfs_header_nritems(right); |
| 3695 | if (right_nritems == 0) |
| 3696 | return 1; |
| 3697 | |
| 3698 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3699 | |
| 3700 | left = read_node_slot(root, path->nodes[1], slot - 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3701 | if (left == NULL) |
| 3702 | return 1; |
| 3703 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3704 | btrfs_tree_lock(left); |
| 3705 | btrfs_set_lock_blocking(left); |
| 3706 | |
| 3707 | free_space = btrfs_leaf_free_space(root, left); |
| 3708 | if (free_space < data_size) { |
| 3709 | ret = 1; |
| 3710 | goto out; |
| 3711 | } |
| 3712 | |
| 3713 | /* cow and double check */ |
| 3714 | ret = btrfs_cow_block(trans, root, left, |
| 3715 | path->nodes[1], slot - 1, &left); |
| 3716 | if (ret) { |
| 3717 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3718 | if (ret == -ENOSPC) |
| 3719 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3720 | goto out; |
| 3721 | } |
| 3722 | |
| 3723 | free_space = btrfs_leaf_free_space(root, left); |
| 3724 | if (free_space < data_size) { |
| 3725 | ret = 1; |
| 3726 | goto out; |
| 3727 | } |
| 3728 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3729 | return __push_leaf_left(trans, root, path, min_data_size, |
| 3730 | empty, left, free_space, right_nritems, |
| 3731 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3732 | out: |
| 3733 | btrfs_tree_unlock(left); |
| 3734 | free_extent_buffer(left); |
| 3735 | return ret; |
| 3736 | } |
| 3737 | |
| 3738 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3739 | * split the path's leaf in two, making sure there is at least data_size |
| 3740 | * available for the resulting leaf level of the path. |
| 3741 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3742 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
| 3743 | struct btrfs_root *root, |
| 3744 | struct btrfs_path *path, |
| 3745 | struct extent_buffer *l, |
| 3746 | struct extent_buffer *right, |
| 3747 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3748 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3749 | int data_copy_size; |
| 3750 | int rt_data_off; |
| 3751 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3752 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3753 | struct btrfs_map_token token; |
| 3754 | |
| 3755 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3756 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3757 | nritems = nritems - mid; |
| 3758 | btrfs_set_header_nritems(right, nritems); |
| 3759 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); |
| 3760 | |
| 3761 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 3762 | btrfs_item_nr_offset(mid), |
| 3763 | nritems * sizeof(struct btrfs_item)); |
| 3764 | |
| 3765 | copy_extent_buffer(right, l, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3766 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 3767 | data_copy_size, btrfs_leaf_data(l) + |
| 3768 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3769 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3770 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 3771 | btrfs_item_end_nr(l, mid); |
| 3772 | |
| 3773 | for (i = 0; i < nritems; i++) { |
| 3774 | struct btrfs_item *item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3775 | u32 ioff; |
| 3776 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3777 | ioff = btrfs_token_item_offset(right, item, &token); |
| 3778 | btrfs_set_token_item_offset(right, item, |
| 3779 | ioff + rt_data_off, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3780 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3781 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3782 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3783 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3784 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3785 | path->slots[1] + 1, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3786 | |
| 3787 | btrfs_mark_buffer_dirty(right); |
| 3788 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3789 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3790 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3791 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3792 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3793 | free_extent_buffer(path->nodes[0]); |
| 3794 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3795 | path->slots[0] -= mid; |
| 3796 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3797 | } else { |
| 3798 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3799 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3800 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3801 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3802 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3806 | * double splits happen when we need to insert a big item in the middle |
| 3807 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 3808 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 3809 | * A B C |
| 3810 | * |
| 3811 | * We avoid this by trying to push the items on either side of our target |
| 3812 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 3813 | * completely. |
| 3814 | */ |
| 3815 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 3816 | struct btrfs_root *root, |
| 3817 | struct btrfs_path *path, |
| 3818 | int data_size) |
| 3819 | { |
| 3820 | int ret; |
| 3821 | int progress = 0; |
| 3822 | int slot; |
| 3823 | u32 nritems; |
| 3824 | |
| 3825 | slot = path->slots[0]; |
| 3826 | |
| 3827 | /* |
| 3828 | * try to push all the items after our slot into the |
| 3829 | * right leaf |
| 3830 | */ |
| 3831 | ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); |
| 3832 | if (ret < 0) |
| 3833 | return ret; |
| 3834 | |
| 3835 | if (ret == 0) |
| 3836 | progress++; |
| 3837 | |
| 3838 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 3839 | /* |
| 3840 | * our goal is to get our slot at the start or end of a leaf. If |
| 3841 | * we've done so we're done |
| 3842 | */ |
| 3843 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 3844 | return 0; |
| 3845 | |
| 3846 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 3847 | return 0; |
| 3848 | |
| 3849 | /* try to push all the items before our slot into the next leaf */ |
| 3850 | slot = path->slots[0]; |
| 3851 | ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); |
| 3852 | if (ret < 0) |
| 3853 | return ret; |
| 3854 | |
| 3855 | if (ret == 0) |
| 3856 | progress++; |
| 3857 | |
| 3858 | if (progress) |
| 3859 | return 0; |
| 3860 | return 1; |
| 3861 | } |
| 3862 | |
| 3863 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3864 | * split the path's leaf in two, making sure there is at least data_size |
| 3865 | * available for the resulting leaf level of the path. |
| 3866 | * |
| 3867 | * returns 0 if all went well and < 0 on failure. |
| 3868 | */ |
| 3869 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 3870 | struct btrfs_root *root, |
| 3871 | struct btrfs_key *ins_key, |
| 3872 | struct btrfs_path *path, int data_size, |
| 3873 | int extend) |
| 3874 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3875 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3876 | struct extent_buffer *l; |
| 3877 | u32 nritems; |
| 3878 | int mid; |
| 3879 | int slot; |
| 3880 | struct extent_buffer *right; |
| 3881 | int ret = 0; |
| 3882 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3883 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3884 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3885 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3886 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 3887 | l = path->nodes[0]; |
| 3888 | slot = path->slots[0]; |
| 3889 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
| 3890 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) |
| 3891 | return -EOVERFLOW; |
| 3892 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3893 | /* first try to make some room by pushing left and right */ |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3894 | if (data_size) { |
| 3895 | wret = push_leaf_right(trans, root, path, data_size, |
| 3896 | data_size, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3897 | if (wret < 0) |
| 3898 | return wret; |
| 3899 | if (wret) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3900 | wret = push_leaf_left(trans, root, path, data_size, |
| 3901 | data_size, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3902 | if (wret < 0) |
| 3903 | return wret; |
| 3904 | } |
| 3905 | l = path->nodes[0]; |
| 3906 | |
| 3907 | /* did the pushes work? */ |
| 3908 | if (btrfs_leaf_free_space(root, l) >= data_size) |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
| 3912 | if (!path->nodes[1]) { |
| 3913 | ret = insert_new_root(trans, root, path, 1); |
| 3914 | if (ret) |
| 3915 | return ret; |
| 3916 | } |
| 3917 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3918 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3919 | l = path->nodes[0]; |
| 3920 | slot = path->slots[0]; |
| 3921 | nritems = btrfs_header_nritems(l); |
| 3922 | mid = (nritems + 1) / 2; |
| 3923 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3924 | if (mid <= slot) { |
| 3925 | if (nritems == 1 || |
| 3926 | leaf_space_used(l, mid, nritems - mid) + data_size > |
| 3927 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3928 | if (slot >= nritems) { |
| 3929 | split = 0; |
| 3930 | } else { |
| 3931 | mid = slot; |
| 3932 | if (mid != nritems && |
| 3933 | leaf_space_used(l, mid, nritems - mid) + |
| 3934 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3935 | if (data_size && !tried_avoid_double) |
| 3936 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3937 | split = 2; |
| 3938 | } |
| 3939 | } |
| 3940 | } |
| 3941 | } else { |
| 3942 | if (leaf_space_used(l, 0, mid) + data_size > |
| 3943 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3944 | if (!extend && data_size && slot == 0) { |
| 3945 | split = 0; |
| 3946 | } else if ((extend || !data_size) && slot == 0) { |
| 3947 | mid = 1; |
| 3948 | } else { |
| 3949 | mid = slot; |
| 3950 | if (mid != nritems && |
| 3951 | leaf_space_used(l, mid, nritems - mid) + |
| 3952 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3953 | if (data_size && !tried_avoid_double) |
| 3954 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3955 | split = 2 ; |
| 3956 | } |
| 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | if (split == 0) |
| 3962 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 3963 | else |
| 3964 | btrfs_item_key(l, &disk_key, mid); |
| 3965 | |
| 3966 | right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3967 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3968 | &disk_key, 0, l->start, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3969 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3970 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3971 | |
| 3972 | root_add_used(root, root->leafsize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3973 | |
| 3974 | memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); |
| 3975 | btrfs_set_header_bytenr(right, right->start); |
| 3976 | btrfs_set_header_generation(right, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3977 | btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3978 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 3979 | btrfs_set_header_level(right, 0); |
| 3980 | write_extent_buffer(right, root->fs_info->fsid, |
| 3981 | (unsigned long)btrfs_header_fsid(right), |
| 3982 | BTRFS_FSID_SIZE); |
| 3983 | |
| 3984 | write_extent_buffer(right, root->fs_info->chunk_tree_uuid, |
| 3985 | (unsigned long)btrfs_header_chunk_tree_uuid(right), |
| 3986 | BTRFS_UUID_SIZE); |
| 3987 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3988 | if (split == 0) { |
| 3989 | if (mid <= slot) { |
| 3990 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3991 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3992 | path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3993 | btrfs_tree_unlock(path->nodes[0]); |
| 3994 | free_extent_buffer(path->nodes[0]); |
| 3995 | path->nodes[0] = right; |
| 3996 | path->slots[0] = 0; |
| 3997 | path->slots[1] += 1; |
| 3998 | } else { |
| 3999 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4000 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4001 | path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4002 | btrfs_tree_unlock(path->nodes[0]); |
| 4003 | free_extent_buffer(path->nodes[0]); |
| 4004 | path->nodes[0] = right; |
| 4005 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4006 | if (path->slots[1] == 0) |
| 4007 | fixup_low_keys(trans, root, path, |
| 4008 | &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4009 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4010 | btrfs_mark_buffer_dirty(right); |
| 4011 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4012 | } |
| 4013 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4014 | copy_for_split(trans, root, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4015 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4016 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4017 | BUG_ON(num_doubles != 0); |
| 4018 | num_doubles++; |
| 4019 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4020 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4021 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4022 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4023 | |
| 4024 | push_for_double: |
| 4025 | push_for_double_split(trans, root, path, data_size); |
| 4026 | tried_avoid_double = 1; |
| 4027 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 4028 | return 0; |
| 4029 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4030 | } |
| 4031 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4032 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4033 | struct btrfs_root *root, |
| 4034 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4035 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4036 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4037 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4038 | struct btrfs_file_extent_item *fi; |
| 4039 | u64 extent_len = 0; |
| 4040 | u32 item_size; |
| 4041 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4042 | |
| 4043 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4044 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4045 | |
| 4046 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4047 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4048 | |
| 4049 | if (btrfs_leaf_free_space(root, leaf) >= ins_len) |
| 4050 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4051 | |
| 4052 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4053 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4054 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4055 | struct btrfs_file_extent_item); |
| 4056 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4057 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4058 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4059 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4060 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4061 | path->search_for_split = 1; |
| 4062 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4063 | path->search_for_split = 0; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4064 | if (ret < 0) |
| 4065 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4066 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4067 | ret = -EAGAIN; |
| 4068 | leaf = path->nodes[0]; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4069 | /* if our item isn't there or got smaller, return now */ |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4070 | if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
| 4071 | goto err; |
| 4072 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4073 | /* the leaf has changed, it now has room. return now */ |
| 4074 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) |
| 4075 | goto err; |
| 4076 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4077 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4078 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4079 | struct btrfs_file_extent_item); |
| 4080 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4081 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4082 | } |
| 4083 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4084 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4085 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4086 | if (ret) |
| 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 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4090 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4091 | return 0; |
| 4092 | err: |
| 4093 | path->keep_locks = 0; |
| 4094 | return ret; |
| 4095 | } |
| 4096 | |
| 4097 | static noinline int split_item(struct btrfs_trans_handle *trans, |
| 4098 | struct btrfs_root *root, |
| 4099 | struct btrfs_path *path, |
| 4100 | struct btrfs_key *new_key, |
| 4101 | unsigned long split_offset) |
| 4102 | { |
| 4103 | struct extent_buffer *leaf; |
| 4104 | struct btrfs_item *item; |
| 4105 | struct btrfs_item *new_item; |
| 4106 | int slot; |
| 4107 | char *buf; |
| 4108 | u32 nritems; |
| 4109 | u32 item_size; |
| 4110 | u32 orig_offset; |
| 4111 | struct btrfs_disk_key disk_key; |
| 4112 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4113 | leaf = path->nodes[0]; |
| 4114 | BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); |
| 4115 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4116 | btrfs_set_path_blocking(path); |
| 4117 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4118 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 4119 | orig_offset = btrfs_item_offset(leaf, item); |
| 4120 | item_size = btrfs_item_size(leaf, item); |
| 4121 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4122 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4123 | if (!buf) |
| 4124 | return -ENOMEM; |
| 4125 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4126 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4127 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4128 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4129 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4130 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4131 | if (slot != nritems) { |
| 4132 | /* shift the items */ |
| 4133 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4134 | btrfs_item_nr_offset(slot), |
| 4135 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4139 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4140 | |
| 4141 | new_item = btrfs_item_nr(leaf, slot); |
| 4142 | |
| 4143 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4144 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4145 | |
| 4146 | btrfs_set_item_offset(leaf, item, |
| 4147 | orig_offset + item_size - split_offset); |
| 4148 | btrfs_set_item_size(leaf, item, split_offset); |
| 4149 | |
| 4150 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4151 | |
| 4152 | /* write the data for the start of the original item */ |
| 4153 | write_extent_buffer(leaf, buf, |
| 4154 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4155 | split_offset); |
| 4156 | |
| 4157 | /* write the data for the new item */ |
| 4158 | write_extent_buffer(leaf, buf + split_offset, |
| 4159 | btrfs_item_ptr_offset(leaf, slot), |
| 4160 | item_size - split_offset); |
| 4161 | btrfs_mark_buffer_dirty(leaf); |
| 4162 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4163 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4164 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4165 | return 0; |
| 4166 | } |
| 4167 | |
| 4168 | /* |
| 4169 | * This function splits a single item into two items, |
| 4170 | * giving 'new_key' to the new item and splitting the |
| 4171 | * old one at split_offset (from the start of the item). |
| 4172 | * |
| 4173 | * The path may be released by this operation. After |
| 4174 | * the split, the path is pointing to the old item. The |
| 4175 | * new item is going to be in the same node as the old one. |
| 4176 | * |
| 4177 | * Note, the item being split must be smaller enough to live alone on |
| 4178 | * a tree block with room for one extra struct btrfs_item |
| 4179 | * |
| 4180 | * This allows us to split the item in place, keeping a lock on the |
| 4181 | * leaf the entire time. |
| 4182 | */ |
| 4183 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4184 | struct btrfs_root *root, |
| 4185 | struct btrfs_path *path, |
| 4186 | struct btrfs_key *new_key, |
| 4187 | unsigned long split_offset) |
| 4188 | { |
| 4189 | int ret; |
| 4190 | ret = setup_leaf_for_split(trans, root, path, |
| 4191 | sizeof(struct btrfs_item)); |
| 4192 | if (ret) |
| 4193 | return ret; |
| 4194 | |
| 4195 | ret = split_item(trans, root, path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4196 | return ret; |
| 4197 | } |
| 4198 | |
| 4199 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4200 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4201 | * It guarantees both items live in the same tree leaf and the new item |
| 4202 | * is contiguous with the original item. |
| 4203 | * |
| 4204 | * This allows us to split file extent in place, keeping a lock on the |
| 4205 | * leaf the entire time. |
| 4206 | */ |
| 4207 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4208 | struct btrfs_root *root, |
| 4209 | struct btrfs_path *path, |
| 4210 | struct btrfs_key *new_key) |
| 4211 | { |
| 4212 | struct extent_buffer *leaf; |
| 4213 | int ret; |
| 4214 | u32 item_size; |
| 4215 | |
| 4216 | leaf = path->nodes[0]; |
| 4217 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4218 | ret = setup_leaf_for_split(trans, root, path, |
| 4219 | item_size + sizeof(struct btrfs_item)); |
| 4220 | if (ret) |
| 4221 | return ret; |
| 4222 | |
| 4223 | path->slots[0]++; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4224 | setup_items_for_insert(trans, root, path, new_key, &item_size, |
| 4225 | item_size, item_size + |
| 4226 | sizeof(struct btrfs_item), 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4227 | leaf = path->nodes[0]; |
| 4228 | memcpy_extent_buffer(leaf, |
| 4229 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4230 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4231 | item_size); |
| 4232 | return 0; |
| 4233 | } |
| 4234 | |
| 4235 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4236 | * make the item pointed to by the path smaller. new_size indicates |
| 4237 | * how small to make it, and from_end tells us if we just chop bytes |
| 4238 | * off the end of the item or if we shift the item to chop bytes off |
| 4239 | * the front. |
| 4240 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4241 | void btrfs_truncate_item(struct btrfs_trans_handle *trans, |
| 4242 | struct btrfs_root *root, |
| 4243 | struct btrfs_path *path, |
| 4244 | u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4245 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4246 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4247 | struct extent_buffer *leaf; |
| 4248 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4249 | u32 nritems; |
| 4250 | unsigned int data_end; |
| 4251 | unsigned int old_data_start; |
| 4252 | unsigned int old_size; |
| 4253 | unsigned int size_diff; |
| 4254 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4255 | struct btrfs_map_token token; |
| 4256 | |
| 4257 | btrfs_init_map_token(&token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4258 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4259 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4260 | slot = path->slots[0]; |
| 4261 | |
| 4262 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4263 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4264 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4265 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4266 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4267 | data_end = leaf_data_end(root, leaf); |
| 4268 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4269 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4270 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4271 | size_diff = old_size - new_size; |
| 4272 | |
| 4273 | BUG_ON(slot < 0); |
| 4274 | BUG_ON(slot >= nritems); |
| 4275 | |
| 4276 | /* |
| 4277 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4278 | */ |
| 4279 | /* first correct the data pointers */ |
| 4280 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4281 | u32 ioff; |
| 4282 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4283 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4284 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4285 | btrfs_set_token_item_offset(leaf, item, |
| 4286 | ioff + size_diff, &token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4287 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4288 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4289 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4290 | if (from_end) { |
| 4291 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4292 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4293 | data_end, old_data_start + new_size - data_end); |
| 4294 | } else { |
| 4295 | struct btrfs_disk_key disk_key; |
| 4296 | u64 offset; |
| 4297 | |
| 4298 | btrfs_item_key(leaf, &disk_key, slot); |
| 4299 | |
| 4300 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4301 | unsigned long ptr; |
| 4302 | struct btrfs_file_extent_item *fi; |
| 4303 | |
| 4304 | fi = btrfs_item_ptr(leaf, slot, |
| 4305 | struct btrfs_file_extent_item); |
| 4306 | fi = (struct btrfs_file_extent_item *)( |
| 4307 | (unsigned long)fi - size_diff); |
| 4308 | |
| 4309 | if (btrfs_file_extent_type(leaf, fi) == |
| 4310 | BTRFS_FILE_EXTENT_INLINE) { |
| 4311 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4312 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4313 | (unsigned long)fi, |
| 4314 | offsetof(struct btrfs_file_extent_item, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4315 | disk_bytenr)); |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4320 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4321 | data_end, old_data_start - data_end); |
| 4322 | |
| 4323 | offset = btrfs_disk_key_offset(&disk_key); |
| 4324 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4325 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4326 | if (slot == 0) |
| 4327 | fixup_low_keys(trans, root, path, &disk_key, 1); |
| 4328 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4329 | |
| 4330 | item = btrfs_item_nr(leaf, slot); |
| 4331 | btrfs_set_item_size(leaf, item, new_size); |
| 4332 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4333 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4334 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4335 | btrfs_print_leaf(root, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4336 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4337 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4338 | } |
| 4339 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4340 | /* |
| 4341 | * make the item pointed to by the path bigger, data_size is the new size. |
| 4342 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4343 | void btrfs_extend_item(struct btrfs_trans_handle *trans, |
| 4344 | struct btrfs_root *root, struct btrfs_path *path, |
| 4345 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4346 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4347 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4348 | struct extent_buffer *leaf; |
| 4349 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4350 | u32 nritems; |
| 4351 | unsigned int data_end; |
| 4352 | unsigned int old_data; |
| 4353 | unsigned int old_size; |
| 4354 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4355 | struct btrfs_map_token token; |
| 4356 | |
| 4357 | btrfs_init_map_token(&token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4358 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4359 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4360 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4361 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4362 | data_end = leaf_data_end(root, leaf); |
| 4363 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4364 | if (btrfs_leaf_free_space(root, leaf) < data_size) { |
| 4365 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4366 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4367 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4368 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4369 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4370 | |
| 4371 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4372 | if (slot >= nritems) { |
| 4373 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4374 | printk(KERN_CRIT "slot %d too large, nritems %d\n", |
| 4375 | slot, nritems); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4376 | BUG_ON(1); |
| 4377 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4378 | |
| 4379 | /* |
| 4380 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4381 | */ |
| 4382 | /* first correct the data pointers */ |
| 4383 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4384 | u32 ioff; |
| 4385 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4386 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4387 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4388 | btrfs_set_token_item_offset(leaf, item, |
| 4389 | ioff - data_size, &token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4390 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4391 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4392 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4393 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4394 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 4395 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4396 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4397 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4398 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4399 | item = btrfs_item_nr(leaf, slot); |
| 4400 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4401 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4402 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4403 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4404 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4405 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4406 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4407 | } |
| 4408 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4409 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4410 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
| 4411 | * to save stack depth by doing the bulk of the work in a function |
| 4412 | * that doesn't call btrfs_search_slot |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4413 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4414 | void setup_items_for_insert(struct btrfs_trans_handle *trans, |
| 4415 | struct btrfs_root *root, struct btrfs_path *path, |
| 4416 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4417 | u32 total_data, u32 total_size, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4418 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4419 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4420 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4421 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4422 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4423 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4424 | struct extent_buffer *leaf; |
| 4425 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4426 | struct btrfs_map_token token; |
| 4427 | |
| 4428 | btrfs_init_map_token(&token); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4429 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4430 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4431 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4432 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4433 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4434 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4435 | |
Chris Mason | f25956c | 2008-09-12 15:32:53 -0400 | [diff] [blame] | 4436 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4437 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4438 | printk(KERN_CRIT "not enough freespace need %u have %d\n", |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4439 | total_size, btrfs_leaf_free_space(root, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4440 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4441 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4442 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4443 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4444 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4445 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4446 | if (old_data < data_end) { |
| 4447 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4448 | printk(KERN_CRIT "slot %d old_data %d data_end %d\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4449 | slot, old_data, data_end); |
| 4450 | BUG_ON(1); |
| 4451 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4452 | /* |
| 4453 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4454 | */ |
| 4455 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4456 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4457 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4458 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4459 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4460 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4461 | btrfs_set_token_item_offset(leaf, item, |
| 4462 | ioff - total_data, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4463 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4464 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4465 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4466 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4467 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4468 | |
| 4469 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4470 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4471 | data_end - total_data, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4472 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4473 | data_end = old_data; |
| 4474 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4475 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4476 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4477 | for (i = 0; i < nr; i++) { |
| 4478 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4479 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
| 4480 | item = btrfs_item_nr(leaf, slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4481 | btrfs_set_token_item_offset(leaf, item, |
| 4482 | data_end - data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4483 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4484 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4485 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4486 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4487 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4488 | |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4489 | if (slot == 0) { |
| 4490 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4491 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4492 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4493 | btrfs_unlock_up_safe(path, 1); |
| 4494 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4495 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4496 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4497 | btrfs_print_leaf(root, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4498 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4499 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | /* |
| 4503 | * Given a key and some data, insert items into the tree. |
| 4504 | * This does all the path init required, making room in the tree if needed. |
| 4505 | */ |
| 4506 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4507 | struct btrfs_root *root, |
| 4508 | struct btrfs_path *path, |
| 4509 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4510 | int nr) |
| 4511 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4512 | int ret = 0; |
| 4513 | int slot; |
| 4514 | int i; |
| 4515 | u32 total_size = 0; |
| 4516 | u32 total_data = 0; |
| 4517 | |
| 4518 | for (i = 0; i < nr; i++) |
| 4519 | total_data += data_size[i]; |
| 4520 | |
| 4521 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4522 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4523 | if (ret == 0) |
| 4524 | return -EEXIST; |
| 4525 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4526 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4527 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4528 | slot = path->slots[0]; |
| 4529 | BUG_ON(slot < 0); |
| 4530 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4531 | setup_items_for_insert(trans, root, path, cpu_key, data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4532 | total_data, total_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4533 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | /* |
| 4537 | * Given a key and some data, insert an item into the tree. |
| 4538 | * This does all the path init required, making room in the tree if needed. |
| 4539 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4540 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 4541 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 4542 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4543 | { |
| 4544 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4545 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4546 | struct extent_buffer *leaf; |
| 4547 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4548 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4549 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4550 | if (!path) |
| 4551 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4552 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4553 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4554 | leaf = path->nodes[0]; |
| 4555 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4556 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4557 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4558 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4559 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4560 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4561 | } |
| 4562 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4563 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4564 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4565 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4566 | * the tree should have been previously balanced so the deletion does not |
| 4567 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4568 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4569 | static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4570 | struct btrfs_path *path, int level, int slot, |
| 4571 | int tree_mod_log) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4572 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4573 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4574 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4575 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4576 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4577 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4578 | if (slot != nritems - 1) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4579 | if (tree_mod_log && level) |
| 4580 | tree_mod_log_eb_move(root->fs_info, parent, slot, |
| 4581 | slot + 1, nritems - slot - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4582 | memmove_extent_buffer(parent, |
| 4583 | btrfs_node_key_ptr_offset(slot), |
| 4584 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4585 | sizeof(struct btrfs_key_ptr) * |
| 4586 | (nritems - slot - 1)); |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 4587 | } else if (tree_mod_log && level) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4588 | ret = tree_mod_log_insert_key(root->fs_info, parent, slot, |
| 4589 | MOD_LOG_KEY_REMOVE); |
| 4590 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4591 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4592 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4593 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4594 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4595 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4596 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4597 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4598 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4599 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4600 | struct btrfs_disk_key disk_key; |
| 4601 | |
| 4602 | btrfs_node_key(parent, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4603 | fixup_low_keys(trans, root, path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4604 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4605 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4606 | } |
| 4607 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4608 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4609 | * 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] | 4610 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4611 | * |
| 4612 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4613 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4614 | * |
| 4615 | * The path must have already been setup for deleting the leaf, including |
| 4616 | * all the proper balancing. path->nodes[1] must be locked. |
| 4617 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4618 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4619 | struct btrfs_root *root, |
| 4620 | struct btrfs_path *path, |
| 4621 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4622 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4623 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4624 | del_ptr(trans, root, path, 1, path->slots[1], 1); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4625 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4626 | /* |
| 4627 | * btrfs_free_extent is expensive, we want to make sure we |
| 4628 | * aren't holding any locks when we call it |
| 4629 | */ |
| 4630 | btrfs_unlock_up_safe(path, 0); |
| 4631 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4632 | root_sub_used(root, leaf->len); |
| 4633 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4634 | extent_buffer_get(leaf); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4635 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4636 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4637 | } |
| 4638 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4639 | * delete the item at the leaf level in path. If that empties |
| 4640 | * the leaf, remove it from the tree |
| 4641 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4642 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4643 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4644 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4645 | struct extent_buffer *leaf; |
| 4646 | struct btrfs_item *item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4647 | int last_off; |
| 4648 | int dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4649 | int ret = 0; |
| 4650 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4651 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4652 | u32 nritems; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4653 | struct btrfs_map_token token; |
| 4654 | |
| 4655 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4656 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4657 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4658 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4659 | |
| 4660 | for (i = 0; i < nr; i++) |
| 4661 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4662 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4663 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4664 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4665 | if (slot + nr != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4666 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4667 | |
| 4668 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4669 | data_end + dsize, |
| 4670 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4671 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4672 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4673 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4674 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4675 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4676 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4677 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4678 | btrfs_set_token_item_offset(leaf, item, |
| 4679 | ioff + dsize, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4680 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4681 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4682 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4683 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4684 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4685 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4686 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4687 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4688 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4690 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4691 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4692 | if (leaf == root->node) { |
| 4693 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4694 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4695 | btrfs_set_path_blocking(path); |
| 4696 | clean_tree_block(trans, root, leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4697 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4698 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4699 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4700 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4701 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4702 | struct btrfs_disk_key disk_key; |
| 4703 | |
| 4704 | btrfs_item_key(leaf, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4705 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4706 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4707 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4708 | /* delete the leaf if it is mostly empty */ |
Yan Zheng | d717aa1 | 2009-07-24 12:42:46 -0400 | [diff] [blame] | 4709 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4710 | /* push_leaf_left fixes the path. |
| 4711 | * make sure the path still points to our leaf |
| 4712 | * for possible call to del_ptr below |
| 4713 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 4714 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4715 | extent_buffer_get(leaf); |
| 4716 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4717 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4718 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 4719 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4720 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4721 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4722 | |
| 4723 | if (path->nodes[0] == leaf && |
| 4724 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4725 | wret = push_leaf_right(trans, root, path, 1, |
| 4726 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4727 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4728 | ret = wret; |
| 4729 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4730 | |
| 4731 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4732 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4733 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4734 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4735 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4736 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4737 | /* if we're still in the path, make sure |
| 4738 | * we're dirty. Otherwise, one of the |
| 4739 | * push_leaf functions must have already |
| 4740 | * dirtied this buffer |
| 4741 | */ |
| 4742 | if (path->nodes[0] == leaf) |
| 4743 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4744 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4745 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 4746 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4747 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4748 | } |
| 4749 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4750 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4751 | } |
| 4752 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 4753 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4754 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4755 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 4756 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4757 | * |
| 4758 | * This may release the path, and so you may lose any locks held at the |
| 4759 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4760 | */ |
| 4761 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
| 4762 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4763 | struct btrfs_key key; |
| 4764 | struct btrfs_disk_key found_key; |
| 4765 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4766 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4767 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4768 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4769 | if (key.offset > 0) |
| 4770 | key.offset--; |
| 4771 | else if (key.type > 0) |
| 4772 | key.type--; |
| 4773 | else if (key.objectid > 0) |
| 4774 | key.objectid--; |
| 4775 | else |
| 4776 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4777 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4778 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4779 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4780 | if (ret < 0) |
| 4781 | return ret; |
| 4782 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 4783 | ret = comp_keys(&found_key, &key); |
| 4784 | if (ret < 0) |
| 4785 | return 0; |
| 4786 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4787 | } |
| 4788 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4789 | /* |
| 4790 | * A helper function to walk down the tree starting at min_key, and looking |
| 4791 | * for nodes or leaves that are either in cache or have a minimum |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4792 | * transaction id. This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4793 | * |
| 4794 | * This does not cow, but it does stuff the starting key it finds back |
| 4795 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 4796 | * key and get a writable path. |
| 4797 | * |
| 4798 | * This does lock as it descends, and path->keep_locks should be set |
| 4799 | * to 1 by the caller. |
| 4800 | * |
| 4801 | * This honors path->lowest_level to prevent descent past a given level |
| 4802 | * of the tree. |
| 4803 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4804 | * min_trans indicates the oldest transaction that you are interested |
| 4805 | * in walking through. Any nodes or leaves older than min_trans are |
| 4806 | * skipped over (without reading them). |
| 4807 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4808 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 4809 | * was nothing in the tree that matched the search criteria. |
| 4810 | */ |
| 4811 | 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] | 4812 | struct btrfs_key *max_key, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4813 | struct btrfs_path *path, int cache_only, |
| 4814 | u64 min_trans) |
| 4815 | { |
| 4816 | struct extent_buffer *cur; |
| 4817 | struct btrfs_key found_key; |
| 4818 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4819 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4820 | u32 nritems; |
| 4821 | int level; |
| 4822 | int ret = 1; |
| 4823 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4824 | WARN_ON(!path->keep_locks); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4825 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4826 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4827 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4828 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4829 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4830 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4831 | |
| 4832 | if (btrfs_header_generation(cur) < min_trans) { |
| 4833 | ret = 1; |
| 4834 | goto out; |
| 4835 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4836 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4837 | nritems = btrfs_header_nritems(cur); |
| 4838 | level = btrfs_header_level(cur); |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4839 | sret = bin_search(cur, min_key, level, &slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4840 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4841 | /* at the lowest level, we're done, setup the path and exit */ |
| 4842 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4843 | if (slot >= nritems) |
| 4844 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4845 | ret = 0; |
| 4846 | path->slots[level] = slot; |
| 4847 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 4848 | goto out; |
| 4849 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4850 | if (sret && slot > 0) |
| 4851 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4852 | /* |
| 4853 | * check this node pointer against the cache_only and |
| 4854 | * min_trans parameters. If it isn't in cache or is too |
| 4855 | * old, skip to the next one. |
| 4856 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4857 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4858 | u64 blockptr; |
| 4859 | u64 gen; |
| 4860 | struct extent_buffer *tmp; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4861 | struct btrfs_disk_key disk_key; |
| 4862 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4863 | blockptr = btrfs_node_blockptr(cur, slot); |
| 4864 | gen = btrfs_node_ptr_generation(cur, slot); |
| 4865 | if (gen < min_trans) { |
| 4866 | slot++; |
| 4867 | continue; |
| 4868 | } |
| 4869 | if (!cache_only) |
| 4870 | break; |
| 4871 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4872 | if (max_key) { |
| 4873 | btrfs_node_key(cur, &disk_key, slot); |
| 4874 | if (comp_keys(&disk_key, max_key) >= 0) { |
| 4875 | ret = 1; |
| 4876 | goto out; |
| 4877 | } |
| 4878 | } |
| 4879 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4880 | tmp = btrfs_find_tree_block(root, blockptr, |
| 4881 | btrfs_level_size(root, level - 1)); |
| 4882 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 4883 | if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4884 | free_extent_buffer(tmp); |
| 4885 | break; |
| 4886 | } |
| 4887 | if (tmp) |
| 4888 | free_extent_buffer(tmp); |
| 4889 | slot++; |
| 4890 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4891 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4892 | /* |
| 4893 | * we didn't find a candidate key in this node, walk forward |
| 4894 | * and find another one |
| 4895 | */ |
| 4896 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4897 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4898 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4899 | sret = btrfs_find_next_key(root, path, min_key, level, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4900 | cache_only, min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4901 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4902 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4903 | goto again; |
| 4904 | } else { |
| 4905 | goto out; |
| 4906 | } |
| 4907 | } |
| 4908 | /* save our key for returning back */ |
| 4909 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 4910 | path->slots[level] = slot; |
| 4911 | if (level == path->lowest_level) { |
| 4912 | ret = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4913 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4914 | goto out; |
| 4915 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4916 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4917 | cur = read_node_slot(root, cur, slot); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4918 | BUG_ON(!cur); /* -ENOMEM */ |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4919 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4920 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4921 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4922 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4923 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4924 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4925 | btrfs_clear_path_blocking(path, NULL, 0); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4926 | } |
| 4927 | out: |
| 4928 | if (ret == 0) |
| 4929 | memcpy(min_key, &found_key, sizeof(found_key)); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4930 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4931 | return ret; |
| 4932 | } |
| 4933 | |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4934 | static void tree_move_down(struct btrfs_root *root, |
| 4935 | struct btrfs_path *path, |
| 4936 | int *level, int root_level) |
| 4937 | { |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4938 | BUG_ON(*level == 0); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4939 | path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level], |
| 4940 | path->slots[*level]); |
| 4941 | path->slots[*level - 1] = 0; |
| 4942 | (*level)--; |
| 4943 | } |
| 4944 | |
| 4945 | static int tree_move_next_or_upnext(struct btrfs_root *root, |
| 4946 | struct btrfs_path *path, |
| 4947 | int *level, int root_level) |
| 4948 | { |
| 4949 | int ret = 0; |
| 4950 | int nritems; |
| 4951 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 4952 | |
| 4953 | path->slots[*level]++; |
| 4954 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4955 | while (path->slots[*level] >= nritems) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 4956 | if (*level == root_level) |
| 4957 | return -1; |
| 4958 | |
| 4959 | /* move upnext */ |
| 4960 | path->slots[*level] = 0; |
| 4961 | free_extent_buffer(path->nodes[*level]); |
| 4962 | path->nodes[*level] = NULL; |
| 4963 | (*level)++; |
| 4964 | path->slots[*level]++; |
| 4965 | |
| 4966 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 4967 | ret = 1; |
| 4968 | } |
| 4969 | return ret; |
| 4970 | } |
| 4971 | |
| 4972 | /* |
| 4973 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 4974 | * or down. |
| 4975 | */ |
| 4976 | static int tree_advance(struct btrfs_root *root, |
| 4977 | struct btrfs_path *path, |
| 4978 | int *level, int root_level, |
| 4979 | int allow_down, |
| 4980 | struct btrfs_key *key) |
| 4981 | { |
| 4982 | int ret; |
| 4983 | |
| 4984 | if (*level == 0 || !allow_down) { |
| 4985 | ret = tree_move_next_or_upnext(root, path, level, root_level); |
| 4986 | } else { |
| 4987 | tree_move_down(root, path, level, root_level); |
| 4988 | ret = 0; |
| 4989 | } |
| 4990 | if (ret >= 0) { |
| 4991 | if (*level == 0) |
| 4992 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 4993 | path->slots[*level]); |
| 4994 | else |
| 4995 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 4996 | path->slots[*level]); |
| 4997 | } |
| 4998 | return ret; |
| 4999 | } |
| 5000 | |
| 5001 | static int tree_compare_item(struct btrfs_root *left_root, |
| 5002 | struct btrfs_path *left_path, |
| 5003 | struct btrfs_path *right_path, |
| 5004 | char *tmp_buf) |
| 5005 | { |
| 5006 | int cmp; |
| 5007 | int len1, len2; |
| 5008 | unsigned long off1, off2; |
| 5009 | |
| 5010 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 5011 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 5012 | if (len1 != len2) |
| 5013 | return 1; |
| 5014 | |
| 5015 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 5016 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 5017 | right_path->slots[0]); |
| 5018 | |
| 5019 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 5020 | |
| 5021 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 5022 | if (cmp) |
| 5023 | return 1; |
| 5024 | return 0; |
| 5025 | } |
| 5026 | |
| 5027 | #define ADVANCE 1 |
| 5028 | #define ADVANCE_ONLY_NEXT -1 |
| 5029 | |
| 5030 | /* |
| 5031 | * This function compares two trees and calls the provided callback for |
| 5032 | * every changed/new/deleted item it finds. |
| 5033 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 5034 | * the compare pretty fast on snapshotted subvolumes. |
| 5035 | * |
| 5036 | * This currently works on commit roots only. As commit roots are read only, |
| 5037 | * we don't do any locking. The commit roots are protected with transactions. |
| 5038 | * Transactions are ended and rejoined when a commit is tried in between. |
| 5039 | * |
| 5040 | * This function checks for modifications done to the trees while comparing. |
| 5041 | * If it detects a change, it aborts immediately. |
| 5042 | */ |
| 5043 | int btrfs_compare_trees(struct btrfs_root *left_root, |
| 5044 | struct btrfs_root *right_root, |
| 5045 | btrfs_changed_cb_t changed_cb, void *ctx) |
| 5046 | { |
| 5047 | int ret; |
| 5048 | int cmp; |
| 5049 | struct btrfs_trans_handle *trans = NULL; |
| 5050 | struct btrfs_path *left_path = NULL; |
| 5051 | struct btrfs_path *right_path = NULL; |
| 5052 | struct btrfs_key left_key; |
| 5053 | struct btrfs_key right_key; |
| 5054 | char *tmp_buf = NULL; |
| 5055 | int left_root_level; |
| 5056 | int right_root_level; |
| 5057 | int left_level; |
| 5058 | int right_level; |
| 5059 | int left_end_reached; |
| 5060 | int right_end_reached; |
| 5061 | int advance_left; |
| 5062 | int advance_right; |
| 5063 | u64 left_blockptr; |
| 5064 | u64 right_blockptr; |
| 5065 | u64 left_start_ctransid; |
| 5066 | u64 right_start_ctransid; |
| 5067 | u64 ctransid; |
| 5068 | |
| 5069 | left_path = btrfs_alloc_path(); |
| 5070 | if (!left_path) { |
| 5071 | ret = -ENOMEM; |
| 5072 | goto out; |
| 5073 | } |
| 5074 | right_path = btrfs_alloc_path(); |
| 5075 | if (!right_path) { |
| 5076 | ret = -ENOMEM; |
| 5077 | goto out; |
| 5078 | } |
| 5079 | |
| 5080 | tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS); |
| 5081 | if (!tmp_buf) { |
| 5082 | ret = -ENOMEM; |
| 5083 | goto out; |
| 5084 | } |
| 5085 | |
| 5086 | left_path->search_commit_root = 1; |
| 5087 | left_path->skip_locking = 1; |
| 5088 | right_path->search_commit_root = 1; |
| 5089 | right_path->skip_locking = 1; |
| 5090 | |
| 5091 | spin_lock(&left_root->root_times_lock); |
| 5092 | left_start_ctransid = btrfs_root_ctransid(&left_root->root_item); |
| 5093 | spin_unlock(&left_root->root_times_lock); |
| 5094 | |
| 5095 | spin_lock(&right_root->root_times_lock); |
| 5096 | right_start_ctransid = btrfs_root_ctransid(&right_root->root_item); |
| 5097 | spin_unlock(&right_root->root_times_lock); |
| 5098 | |
| 5099 | trans = btrfs_join_transaction(left_root); |
| 5100 | if (IS_ERR(trans)) { |
| 5101 | ret = PTR_ERR(trans); |
| 5102 | trans = NULL; |
| 5103 | goto out; |
| 5104 | } |
| 5105 | |
| 5106 | /* |
| 5107 | * Strategy: Go to the first items of both trees. Then do |
| 5108 | * |
| 5109 | * If both trees are at level 0 |
| 5110 | * Compare keys of current items |
| 5111 | * If left < right treat left item as new, advance left tree |
| 5112 | * and repeat |
| 5113 | * If left > right treat right item as deleted, advance right tree |
| 5114 | * and repeat |
| 5115 | * If left == right do deep compare of items, treat as changed if |
| 5116 | * needed, advance both trees and repeat |
| 5117 | * If both trees are at the same level but not at level 0 |
| 5118 | * Compare keys of current nodes/leafs |
| 5119 | * If left < right advance left tree and repeat |
| 5120 | * If left > right advance right tree and repeat |
| 5121 | * If left == right compare blockptrs of the next nodes/leafs |
| 5122 | * If they match advance both trees but stay at the same level |
| 5123 | * and repeat |
| 5124 | * If they don't match advance both trees while allowing to go |
| 5125 | * deeper and repeat |
| 5126 | * If tree levels are different |
| 5127 | * Advance the tree that needs it and repeat |
| 5128 | * |
| 5129 | * Advancing a tree means: |
| 5130 | * If we are at level 0, try to go to the next slot. If that's not |
| 5131 | * possible, go one level up and repeat. Stop when we found a level |
| 5132 | * where we could go to the next slot. We may at this point be on a |
| 5133 | * node or a leaf. |
| 5134 | * |
| 5135 | * If we are not at level 0 and not on shared tree blocks, go one |
| 5136 | * level deeper. |
| 5137 | * |
| 5138 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 5139 | * the right if possible or go up and right. |
| 5140 | */ |
| 5141 | |
| 5142 | left_level = btrfs_header_level(left_root->commit_root); |
| 5143 | left_root_level = left_level; |
| 5144 | left_path->nodes[left_level] = left_root->commit_root; |
| 5145 | extent_buffer_get(left_path->nodes[left_level]); |
| 5146 | |
| 5147 | right_level = btrfs_header_level(right_root->commit_root); |
| 5148 | right_root_level = right_level; |
| 5149 | right_path->nodes[right_level] = right_root->commit_root; |
| 5150 | extent_buffer_get(right_path->nodes[right_level]); |
| 5151 | |
| 5152 | if (left_level == 0) |
| 5153 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 5154 | &left_key, left_path->slots[left_level]); |
| 5155 | else |
| 5156 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 5157 | &left_key, left_path->slots[left_level]); |
| 5158 | if (right_level == 0) |
| 5159 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 5160 | &right_key, right_path->slots[right_level]); |
| 5161 | else |
| 5162 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 5163 | &right_key, right_path->slots[right_level]); |
| 5164 | |
| 5165 | left_end_reached = right_end_reached = 0; |
| 5166 | advance_left = advance_right = 0; |
| 5167 | |
| 5168 | while (1) { |
| 5169 | /* |
| 5170 | * We need to make sure the transaction does not get committed |
| 5171 | * while we do anything on commit roots. This means, we need to |
| 5172 | * join and leave transactions for every item that we process. |
| 5173 | */ |
| 5174 | if (trans && btrfs_should_end_transaction(trans, left_root)) { |
| 5175 | btrfs_release_path(left_path); |
| 5176 | btrfs_release_path(right_path); |
| 5177 | |
| 5178 | ret = btrfs_end_transaction(trans, left_root); |
| 5179 | trans = NULL; |
| 5180 | if (ret < 0) |
| 5181 | goto out; |
| 5182 | } |
| 5183 | /* now rejoin the transaction */ |
| 5184 | if (!trans) { |
| 5185 | trans = btrfs_join_transaction(left_root); |
| 5186 | if (IS_ERR(trans)) { |
| 5187 | ret = PTR_ERR(trans); |
| 5188 | trans = NULL; |
| 5189 | goto out; |
| 5190 | } |
| 5191 | |
| 5192 | spin_lock(&left_root->root_times_lock); |
| 5193 | ctransid = btrfs_root_ctransid(&left_root->root_item); |
| 5194 | spin_unlock(&left_root->root_times_lock); |
| 5195 | if (ctransid != left_start_ctransid) |
| 5196 | left_start_ctransid = 0; |
| 5197 | |
| 5198 | spin_lock(&right_root->root_times_lock); |
| 5199 | ctransid = btrfs_root_ctransid(&right_root->root_item); |
| 5200 | spin_unlock(&right_root->root_times_lock); |
| 5201 | if (ctransid != right_start_ctransid) |
| 5202 | right_start_ctransid = 0; |
| 5203 | |
| 5204 | if (!left_start_ctransid || !right_start_ctransid) { |
| 5205 | WARN(1, KERN_WARNING |
| 5206 | "btrfs: btrfs_compare_tree detected " |
| 5207 | "a change in one of the trees while " |
| 5208 | "iterating. This is probably a " |
| 5209 | "bug.\n"); |
| 5210 | ret = -EIO; |
| 5211 | goto out; |
| 5212 | } |
| 5213 | |
| 5214 | /* |
| 5215 | * the commit root may have changed, so start again |
| 5216 | * where we stopped |
| 5217 | */ |
| 5218 | left_path->lowest_level = left_level; |
| 5219 | right_path->lowest_level = right_level; |
| 5220 | ret = btrfs_search_slot(NULL, left_root, |
| 5221 | &left_key, left_path, 0, 0); |
| 5222 | if (ret < 0) |
| 5223 | goto out; |
| 5224 | ret = btrfs_search_slot(NULL, right_root, |
| 5225 | &right_key, right_path, 0, 0); |
| 5226 | if (ret < 0) |
| 5227 | goto out; |
| 5228 | } |
| 5229 | |
| 5230 | if (advance_left && !left_end_reached) { |
| 5231 | ret = tree_advance(left_root, left_path, &left_level, |
| 5232 | left_root_level, |
| 5233 | advance_left != ADVANCE_ONLY_NEXT, |
| 5234 | &left_key); |
| 5235 | if (ret < 0) |
| 5236 | left_end_reached = ADVANCE; |
| 5237 | advance_left = 0; |
| 5238 | } |
| 5239 | if (advance_right && !right_end_reached) { |
| 5240 | ret = tree_advance(right_root, right_path, &right_level, |
| 5241 | right_root_level, |
| 5242 | advance_right != ADVANCE_ONLY_NEXT, |
| 5243 | &right_key); |
| 5244 | if (ret < 0) |
| 5245 | right_end_reached = ADVANCE; |
| 5246 | advance_right = 0; |
| 5247 | } |
| 5248 | |
| 5249 | if (left_end_reached && right_end_reached) { |
| 5250 | ret = 0; |
| 5251 | goto out; |
| 5252 | } else if (left_end_reached) { |
| 5253 | if (right_level == 0) { |
| 5254 | ret = changed_cb(left_root, right_root, |
| 5255 | left_path, right_path, |
| 5256 | &right_key, |
| 5257 | BTRFS_COMPARE_TREE_DELETED, |
| 5258 | ctx); |
| 5259 | if (ret < 0) |
| 5260 | goto out; |
| 5261 | } |
| 5262 | advance_right = ADVANCE; |
| 5263 | continue; |
| 5264 | } else if (right_end_reached) { |
| 5265 | if (left_level == 0) { |
| 5266 | ret = changed_cb(left_root, right_root, |
| 5267 | left_path, right_path, |
| 5268 | &left_key, |
| 5269 | BTRFS_COMPARE_TREE_NEW, |
| 5270 | ctx); |
| 5271 | if (ret < 0) |
| 5272 | goto out; |
| 5273 | } |
| 5274 | advance_left = ADVANCE; |
| 5275 | continue; |
| 5276 | } |
| 5277 | |
| 5278 | if (left_level == 0 && right_level == 0) { |
| 5279 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5280 | if (cmp < 0) { |
| 5281 | ret = changed_cb(left_root, right_root, |
| 5282 | left_path, right_path, |
| 5283 | &left_key, |
| 5284 | BTRFS_COMPARE_TREE_NEW, |
| 5285 | ctx); |
| 5286 | if (ret < 0) |
| 5287 | goto out; |
| 5288 | advance_left = ADVANCE; |
| 5289 | } else if (cmp > 0) { |
| 5290 | ret = changed_cb(left_root, right_root, |
| 5291 | left_path, right_path, |
| 5292 | &right_key, |
| 5293 | BTRFS_COMPARE_TREE_DELETED, |
| 5294 | ctx); |
| 5295 | if (ret < 0) |
| 5296 | goto out; |
| 5297 | advance_right = ADVANCE; |
| 5298 | } else { |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5299 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5300 | ret = tree_compare_item(left_root, left_path, |
| 5301 | right_path, tmp_buf); |
| 5302 | if (ret) { |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5303 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5304 | ret = changed_cb(left_root, right_root, |
| 5305 | left_path, right_path, |
| 5306 | &left_key, |
| 5307 | BTRFS_COMPARE_TREE_CHANGED, |
| 5308 | ctx); |
| 5309 | if (ret < 0) |
| 5310 | goto out; |
| 5311 | } |
| 5312 | advance_left = ADVANCE; |
| 5313 | advance_right = ADVANCE; |
| 5314 | } |
| 5315 | } else if (left_level == right_level) { |
| 5316 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5317 | if (cmp < 0) { |
| 5318 | advance_left = ADVANCE; |
| 5319 | } else if (cmp > 0) { |
| 5320 | advance_right = ADVANCE; |
| 5321 | } else { |
| 5322 | left_blockptr = btrfs_node_blockptr( |
| 5323 | left_path->nodes[left_level], |
| 5324 | left_path->slots[left_level]); |
| 5325 | right_blockptr = btrfs_node_blockptr( |
| 5326 | right_path->nodes[right_level], |
| 5327 | right_path->slots[right_level]); |
| 5328 | if (left_blockptr == right_blockptr) { |
| 5329 | /* |
| 5330 | * As we're on a shared block, don't |
| 5331 | * allow to go deeper. |
| 5332 | */ |
| 5333 | advance_left = ADVANCE_ONLY_NEXT; |
| 5334 | advance_right = ADVANCE_ONLY_NEXT; |
| 5335 | } else { |
| 5336 | advance_left = ADVANCE; |
| 5337 | advance_right = ADVANCE; |
| 5338 | } |
| 5339 | } |
| 5340 | } else if (left_level < right_level) { |
| 5341 | advance_right = ADVANCE; |
| 5342 | } else { |
| 5343 | advance_left = ADVANCE; |
| 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | out: |
| 5348 | btrfs_free_path(left_path); |
| 5349 | btrfs_free_path(right_path); |
| 5350 | kfree(tmp_buf); |
| 5351 | |
| 5352 | if (trans) { |
| 5353 | if (!ret) |
| 5354 | ret = btrfs_end_transaction(trans, left_root); |
| 5355 | else |
| 5356 | btrfs_end_transaction(trans, left_root); |
| 5357 | } |
| 5358 | |
| 5359 | return ret; |
| 5360 | } |
| 5361 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5362 | /* |
| 5363 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5364 | * and fixup the path. It looks for and returns the next key in the |
| 5365 | * tree based on the current path and the cache_only and min_trans |
| 5366 | * parameters. |
| 5367 | * |
| 5368 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5369 | * and 1 is returned if there are no higher keys in the tree |
| 5370 | * |
| 5371 | * path->keep_locks should be set to 1 on the search made before |
| 5372 | * calling this function. |
| 5373 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5374 | int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5375 | struct btrfs_key *key, int level, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5376 | int cache_only, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5377 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5378 | int slot; |
| 5379 | struct extent_buffer *c; |
| 5380 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5381 | WARN_ON(!path->keep_locks); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5382 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5383 | if (!path->nodes[level]) |
| 5384 | return 1; |
| 5385 | |
| 5386 | slot = path->slots[level] + 1; |
| 5387 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5388 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5389 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5390 | int ret; |
| 5391 | int orig_lowest; |
| 5392 | struct btrfs_key cur_key; |
| 5393 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5394 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5395 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5396 | |
| 5397 | if (path->locks[level + 1]) { |
| 5398 | level++; |
| 5399 | continue; |
| 5400 | } |
| 5401 | |
| 5402 | slot = btrfs_header_nritems(c) - 1; |
| 5403 | if (level == 0) |
| 5404 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5405 | else |
| 5406 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5407 | |
| 5408 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5409 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5410 | path->lowest_level = level; |
| 5411 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5412 | 0, 0); |
| 5413 | path->lowest_level = orig_lowest; |
| 5414 | if (ret < 0) |
| 5415 | return ret; |
| 5416 | |
| 5417 | c = path->nodes[level]; |
| 5418 | slot = path->slots[level]; |
| 5419 | if (ret == 0) |
| 5420 | slot++; |
| 5421 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5422 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5423 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5424 | if (level == 0) |
| 5425 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5426 | else { |
| 5427 | u64 blockptr = btrfs_node_blockptr(c, slot); |
| 5428 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5429 | |
| 5430 | if (cache_only) { |
| 5431 | struct extent_buffer *cur; |
| 5432 | cur = btrfs_find_tree_block(root, blockptr, |
| 5433 | btrfs_level_size(root, level - 1)); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 5434 | if (!cur || |
| 5435 | btrfs_buffer_uptodate(cur, gen, 1) <= 0) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5436 | slot++; |
| 5437 | if (cur) |
| 5438 | free_extent_buffer(cur); |
| 5439 | goto next; |
| 5440 | } |
| 5441 | free_extent_buffer(cur); |
| 5442 | } |
| 5443 | if (gen < min_trans) { |
| 5444 | slot++; |
| 5445 | goto next; |
| 5446 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5447 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5448 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5449 | return 0; |
| 5450 | } |
| 5451 | return 1; |
| 5452 | } |
| 5453 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5454 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5455 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5456 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5457 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5458 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5459 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5460 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5461 | return btrfs_next_old_leaf(root, path, 0); |
| 5462 | } |
| 5463 | |
| 5464 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5465 | u64 time_seq) |
| 5466 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5467 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5468 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5469 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5470 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5471 | struct btrfs_key key; |
| 5472 | u32 nritems; |
| 5473 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5474 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5475 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5476 | |
| 5477 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5478 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5479 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5480 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5481 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5482 | again: |
| 5483 | level = 1; |
| 5484 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5485 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5486 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5487 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5488 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5489 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5490 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5491 | if (time_seq) |
| 5492 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5493 | else |
| 5494 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5495 | path->keep_locks = 0; |
| 5496 | |
| 5497 | if (ret < 0) |
| 5498 | return ret; |
| 5499 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5500 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5501 | /* |
| 5502 | * by releasing the path above we dropped all our locks. A balance |
| 5503 | * could have added more items next to the key that used to be |
| 5504 | * at the very end of the block. So, check again here and |
| 5505 | * advance the path if there are now more items available. |
| 5506 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5507 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5508 | if (ret == 0) |
| 5509 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5510 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5511 | goto done; |
| 5512 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5513 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5514 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5515 | if (!path->nodes[level]) { |
| 5516 | ret = 1; |
| 5517 | goto done; |
| 5518 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5519 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5520 | slot = path->slots[level] + 1; |
| 5521 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5522 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5523 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5524 | if (level == BTRFS_MAX_LEVEL) { |
| 5525 | ret = 1; |
| 5526 | goto done; |
| 5527 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5528 | continue; |
| 5529 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5530 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5531 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5532 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5533 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5534 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5535 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5536 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5537 | next_rw_lock = path->locks[level]; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5538 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5539 | slot, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5540 | if (ret == -EAGAIN) |
| 5541 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5542 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5543 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5544 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5545 | goto done; |
| 5546 | } |
| 5547 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5548 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5549 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5550 | if (!ret && time_seq) { |
| 5551 | /* |
| 5552 | * If we don't get the lock, we may be racing |
| 5553 | * with push_leaf_left, holding that lock while |
| 5554 | * itself waiting for the leaf we've currently |
| 5555 | * locked. To solve this situation, we give up |
| 5556 | * on our lock and cycle. |
| 5557 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5558 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5559 | btrfs_release_path(path); |
| 5560 | cond_resched(); |
| 5561 | goto again; |
| 5562 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5563 | if (!ret) { |
| 5564 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5565 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5566 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5567 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5568 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5569 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5570 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5571 | break; |
| 5572 | } |
| 5573 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5574 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5575 | level--; |
| 5576 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5577 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5578 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5579 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5580 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5581 | path->nodes[level] = next; |
| 5582 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5583 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5584 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5585 | if (!level) |
| 5586 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5587 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5588 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5589 | 0, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5590 | if (ret == -EAGAIN) |
| 5591 | goto again; |
| 5592 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5593 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5594 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5595 | goto done; |
| 5596 | } |
| 5597 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5598 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5599 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5600 | if (!ret) { |
| 5601 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5602 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5603 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5604 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5605 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5606 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5607 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5608 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5609 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5610 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5611 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5612 | path->leave_spinning = old_spinning; |
| 5613 | if (!old_spinning) |
| 5614 | btrfs_set_path_blocking(path); |
| 5615 | |
| 5616 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5617 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5618 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5619 | /* |
| 5620 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5621 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5622 | * |
| 5623 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5624 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5625 | int btrfs_previous_item(struct btrfs_root *root, |
| 5626 | struct btrfs_path *path, u64 min_objectid, |
| 5627 | int type) |
| 5628 | { |
| 5629 | struct btrfs_key found_key; |
| 5630 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5631 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5632 | int ret; |
| 5633 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5634 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5635 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5636 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5637 | ret = btrfs_prev_leaf(root, path); |
| 5638 | if (ret != 0) |
| 5639 | return ret; |
| 5640 | } else { |
| 5641 | path->slots[0]--; |
| 5642 | } |
| 5643 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5644 | nritems = btrfs_header_nritems(leaf); |
| 5645 | if (nritems == 0) |
| 5646 | return 1; |
| 5647 | if (path->slots[0] == nritems) |
| 5648 | path->slots[0]--; |
| 5649 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5650 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5651 | if (found_key.objectid < min_objectid) |
| 5652 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5653 | if (found_key.type == type) |
| 5654 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5655 | if (found_key.objectid == min_objectid && |
| 5656 | found_key.type < type) |
| 5657 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5658 | } |
| 5659 | return 1; |
| 5660 | } |