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 */ |
| 324 | struct seq_list elem; |
| 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 | |
| 344 | static inline void |
| 345 | __get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem) |
| 346 | { |
| 347 | elem->seq = atomic_inc_return(&fs_info->tree_mod_seq); |
| 348 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 349 | } |
| 350 | |
| 351 | void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 352 | struct seq_list *elem) |
| 353 | { |
| 354 | elem->flags = 1; |
| 355 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 356 | __get_tree_mod_seq(fs_info, elem); |
| 357 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 358 | } |
| 359 | |
| 360 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 361 | struct seq_list *elem) |
| 362 | { |
| 363 | struct rb_root *tm_root; |
| 364 | struct rb_node *node; |
| 365 | struct rb_node *next; |
| 366 | struct seq_list *cur_elem; |
| 367 | struct tree_mod_elem *tm; |
| 368 | u64 min_seq = (u64)-1; |
| 369 | u64 seq_putting = elem->seq; |
| 370 | |
| 371 | if (!seq_putting) |
| 372 | return; |
| 373 | |
| 374 | BUG_ON(!(elem->flags & 1)); |
| 375 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 376 | list_del(&elem->list); |
| 377 | |
| 378 | list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) { |
| 379 | if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) { |
| 380 | if (seq_putting > cur_elem->seq) { |
| 381 | /* |
| 382 | * blocker with lower sequence number exists, we |
| 383 | * cannot remove anything from the log |
| 384 | */ |
| 385 | goto out; |
| 386 | } |
| 387 | min_seq = cur_elem->seq; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * anything that's lower than the lowest existing (read: blocked) |
| 393 | * sequence number can be removed from the tree. |
| 394 | */ |
| 395 | write_lock(&fs_info->tree_mod_log_lock); |
| 396 | tm_root = &fs_info->tree_mod_log; |
| 397 | for (node = rb_first(tm_root); node; node = next) { |
| 398 | next = rb_next(node); |
| 399 | tm = container_of(node, struct tree_mod_elem, node); |
| 400 | if (tm->elem.seq > min_seq) |
| 401 | continue; |
| 402 | rb_erase(node, tm_root); |
| 403 | list_del(&tm->elem.list); |
| 404 | kfree(tm); |
| 405 | } |
| 406 | write_unlock(&fs_info->tree_mod_log_lock); |
| 407 | out: |
| 408 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * key order of the log: |
| 413 | * index -> sequence |
| 414 | * |
| 415 | * the index is the shifted logical of the *new* root node for root replace |
| 416 | * operations, or the shifted logical of the affected block for all other |
| 417 | * operations. |
| 418 | */ |
| 419 | static noinline int |
| 420 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 421 | { |
| 422 | struct rb_root *tm_root; |
| 423 | struct rb_node **new; |
| 424 | struct rb_node *parent = NULL; |
| 425 | struct tree_mod_elem *cur; |
| 426 | int ret = 0; |
| 427 | |
| 428 | BUG_ON(!tm || !tm->elem.seq); |
| 429 | |
| 430 | write_lock(&fs_info->tree_mod_log_lock); |
| 431 | tm_root = &fs_info->tree_mod_log; |
| 432 | new = &tm_root->rb_node; |
| 433 | while (*new) { |
| 434 | cur = container_of(*new, struct tree_mod_elem, node); |
| 435 | parent = *new; |
| 436 | if (cur->index < tm->index) |
| 437 | new = &((*new)->rb_left); |
| 438 | else if (cur->index > tm->index) |
| 439 | new = &((*new)->rb_right); |
| 440 | else if (cur->elem.seq < tm->elem.seq) |
| 441 | new = &((*new)->rb_left); |
| 442 | else if (cur->elem.seq > tm->elem.seq) |
| 443 | new = &((*new)->rb_right); |
| 444 | else { |
| 445 | kfree(tm); |
| 446 | ret = -EEXIST; |
| 447 | goto unlock; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | rb_link_node(&tm->node, parent, new); |
| 452 | rb_insert_color(&tm->node, tm_root); |
| 453 | unlock: |
| 454 | write_unlock(&fs_info->tree_mod_log_lock); |
| 455 | return ret; |
| 456 | } |
| 457 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 458 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 459 | struct extent_buffer *eb) { |
| 460 | smp_mb(); |
| 461 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 462 | return 1; |
| 463 | if (!eb) |
| 464 | return 0; |
| 465 | if (btrfs_header_level(eb) == 0) |
| 466 | return 1; |
| 467 | return 0; |
| 468 | } |
| 469 | |
Jan Schmidt | 926dd8a | 2012-05-31 14:00:15 +0200 | [diff] [blame] | 470 | static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags, |
| 471 | struct tree_mod_elem **tm_ret) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 472 | { |
| 473 | struct tree_mod_elem *tm; |
Jan Schmidt | 926dd8a | 2012-05-31 14:00:15 +0200 | [diff] [blame] | 474 | int seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 475 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 476 | if (tree_mod_dont_log(fs_info, NULL)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 477 | return 0; |
| 478 | |
| 479 | tm = *tm_ret = kzalloc(sizeof(*tm), flags); |
| 480 | if (!tm) |
| 481 | return -ENOMEM; |
| 482 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 483 | tm->elem.flags = 0; |
Jan Schmidt | 926dd8a | 2012-05-31 14:00:15 +0200 | [diff] [blame] | 484 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 485 | if (list_empty(&fs_info->tree_mod_seq_list)) { |
| 486 | /* |
| 487 | * someone emptied the list while we were waiting for the lock. |
| 488 | * we must not add to the list, because no blocker exists. items |
| 489 | * are removed from the list only when the existing blocker is |
| 490 | * removed from the list. |
| 491 | */ |
| 492 | kfree(tm); |
| 493 | seq = 0; |
| 494 | } else { |
| 495 | __get_tree_mod_seq(fs_info, &tm->elem); |
| 496 | seq = tm->elem.seq; |
| 497 | } |
| 498 | spin_unlock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 499 | |
| 500 | return seq; |
| 501 | } |
| 502 | |
| 503 | static noinline int |
| 504 | tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info, |
| 505 | struct extent_buffer *eb, int slot, |
| 506 | enum mod_log_op op, gfp_t flags) |
| 507 | { |
| 508 | struct tree_mod_elem *tm; |
| 509 | int ret; |
| 510 | |
| 511 | ret = tree_mod_alloc(fs_info, flags, &tm); |
| 512 | if (ret <= 0) |
| 513 | return ret; |
| 514 | |
| 515 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 516 | if (op != MOD_LOG_KEY_ADD) { |
| 517 | btrfs_node_key(eb, &tm->key, slot); |
| 518 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 519 | } |
| 520 | tm->op = op; |
| 521 | tm->slot = slot; |
| 522 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
| 523 | |
| 524 | return __tree_mod_log_insert(fs_info, tm); |
| 525 | } |
| 526 | |
| 527 | static noinline int |
| 528 | tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 529 | int slot, enum mod_log_op op) |
| 530 | { |
| 531 | return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS); |
| 532 | } |
| 533 | |
| 534 | static noinline int |
| 535 | tree_mod_log_insert_move(struct btrfs_fs_info *fs_info, |
| 536 | struct extent_buffer *eb, int dst_slot, int src_slot, |
| 537 | int nr_items, gfp_t flags) |
| 538 | { |
| 539 | struct tree_mod_elem *tm; |
| 540 | int ret; |
| 541 | int i; |
| 542 | |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame^] | 543 | if (tree_mod_dont_log(fs_info, eb)) |
| 544 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 545 | |
| 546 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
| 547 | ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot, |
| 548 | MOD_LOG_KEY_REMOVE_WHILE_MOVING); |
| 549 | BUG_ON(ret < 0); |
| 550 | } |
| 551 | |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame^] | 552 | ret = tree_mod_alloc(fs_info, flags, &tm); |
| 553 | if (ret <= 0) |
| 554 | return ret; |
| 555 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 556 | tm->index = eb->start >> PAGE_CACHE_SHIFT; |
| 557 | tm->slot = src_slot; |
| 558 | tm->move.dst_slot = dst_slot; |
| 559 | tm->move.nr_items = nr_items; |
| 560 | tm->op = MOD_LOG_MOVE_KEYS; |
| 561 | |
| 562 | return __tree_mod_log_insert(fs_info, tm); |
| 563 | } |
| 564 | |
| 565 | static noinline int |
| 566 | tree_mod_log_insert_root(struct btrfs_fs_info *fs_info, |
| 567 | struct extent_buffer *old_root, |
| 568 | struct extent_buffer *new_root, gfp_t flags) |
| 569 | { |
| 570 | struct tree_mod_elem *tm; |
| 571 | int ret; |
| 572 | |
| 573 | ret = tree_mod_alloc(fs_info, flags, &tm); |
| 574 | if (ret <= 0) |
| 575 | return ret; |
| 576 | |
| 577 | tm->index = new_root->start >> PAGE_CACHE_SHIFT; |
| 578 | tm->old_root.logical = old_root->start; |
| 579 | tm->old_root.level = btrfs_header_level(old_root); |
| 580 | tm->generation = btrfs_header_generation(old_root); |
| 581 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 582 | |
| 583 | return __tree_mod_log_insert(fs_info, tm); |
| 584 | } |
| 585 | |
| 586 | static struct tree_mod_elem * |
| 587 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 588 | int smallest) |
| 589 | { |
| 590 | struct rb_root *tm_root; |
| 591 | struct rb_node *node; |
| 592 | struct tree_mod_elem *cur = NULL; |
| 593 | struct tree_mod_elem *found = NULL; |
| 594 | u64 index = start >> PAGE_CACHE_SHIFT; |
| 595 | |
| 596 | read_lock(&fs_info->tree_mod_log_lock); |
| 597 | tm_root = &fs_info->tree_mod_log; |
| 598 | node = tm_root->rb_node; |
| 599 | while (node) { |
| 600 | cur = container_of(node, struct tree_mod_elem, node); |
| 601 | if (cur->index < index) { |
| 602 | node = node->rb_left; |
| 603 | } else if (cur->index > index) { |
| 604 | node = node->rb_right; |
| 605 | } else if (cur->elem.seq < min_seq) { |
| 606 | node = node->rb_left; |
| 607 | } else if (!smallest) { |
| 608 | /* we want the node with the highest seq */ |
| 609 | if (found) |
| 610 | BUG_ON(found->elem.seq > cur->elem.seq); |
| 611 | found = cur; |
| 612 | node = node->rb_left; |
| 613 | } else if (cur->elem.seq > min_seq) { |
| 614 | /* we want the node with the smallest seq */ |
| 615 | if (found) |
| 616 | BUG_ON(found->elem.seq < cur->elem.seq); |
| 617 | found = cur; |
| 618 | node = node->rb_right; |
| 619 | } else { |
| 620 | found = cur; |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | read_unlock(&fs_info->tree_mod_log_lock); |
| 625 | |
| 626 | return found; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * this returns the element from the log with the smallest time sequence |
| 631 | * value that's in the log (the oldest log item). any element with a time |
| 632 | * sequence lower than min_seq will be ignored. |
| 633 | */ |
| 634 | static struct tree_mod_elem * |
| 635 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 636 | u64 min_seq) |
| 637 | { |
| 638 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * this returns the element from the log with the largest time sequence |
| 643 | * value that's in the log (the most recent log item). any element with |
| 644 | * a time sequence lower than min_seq will be ignored. |
| 645 | */ |
| 646 | static struct tree_mod_elem * |
| 647 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 648 | { |
| 649 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 650 | } |
| 651 | |
| 652 | static inline void |
| 653 | tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 654 | struct extent_buffer *src, unsigned long dst_offset, |
| 655 | unsigned long src_offset, int nr_items) |
| 656 | { |
| 657 | int ret; |
| 658 | int i; |
| 659 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 660 | if (tree_mod_dont_log(fs_info, NULL)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 661 | return; |
| 662 | |
| 663 | if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) |
| 664 | return; |
| 665 | |
| 666 | /* speed this up by single seq for all operations? */ |
| 667 | for (i = 0; i < nr_items; i++) { |
| 668 | ret = tree_mod_log_insert_key(fs_info, src, i + src_offset, |
| 669 | MOD_LOG_KEY_REMOVE); |
| 670 | BUG_ON(ret < 0); |
| 671 | ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset, |
| 672 | MOD_LOG_KEY_ADD); |
| 673 | BUG_ON(ret < 0); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static inline void |
| 678 | tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 679 | int dst_offset, int src_offset, int nr_items) |
| 680 | { |
| 681 | int ret; |
| 682 | ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset, |
| 683 | nr_items, GFP_NOFS); |
| 684 | BUG_ON(ret < 0); |
| 685 | } |
| 686 | |
| 687 | static inline void |
| 688 | tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info, |
| 689 | struct extent_buffer *eb, |
| 690 | struct btrfs_disk_key *disk_key, int slot, int atomic) |
| 691 | { |
| 692 | int ret; |
| 693 | |
| 694 | ret = tree_mod_log_insert_key_mask(fs_info, eb, slot, |
| 695 | MOD_LOG_KEY_REPLACE, |
| 696 | atomic ? GFP_ATOMIC : GFP_NOFS); |
| 697 | BUG_ON(ret < 0); |
| 698 | } |
| 699 | |
| 700 | static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, |
| 701 | struct extent_buffer *eb) |
| 702 | { |
| 703 | int i; |
| 704 | int ret; |
| 705 | u32 nritems; |
| 706 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 707 | if (tree_mod_dont_log(fs_info, eb)) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 708 | return; |
| 709 | |
| 710 | nritems = btrfs_header_nritems(eb); |
| 711 | for (i = nritems - 1; i >= 0; i--) { |
| 712 | ret = tree_mod_log_insert_key(fs_info, eb, i, |
| 713 | MOD_LOG_KEY_REMOVE_WHILE_FREEING); |
| 714 | BUG_ON(ret < 0); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static inline void |
| 719 | tree_mod_log_set_root_pointer(struct btrfs_root *root, |
| 720 | struct extent_buffer *new_root_node) |
| 721 | { |
| 722 | int ret; |
| 723 | tree_mod_log_free_eb(root->fs_info, root->node); |
| 724 | ret = tree_mod_log_insert_root(root->fs_info, root->node, |
| 725 | new_root_node, GFP_NOFS); |
| 726 | BUG_ON(ret < 0); |
| 727 | } |
| 728 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 729 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 730 | * check if the tree block can be shared by multiple trees |
| 731 | */ |
| 732 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 733 | struct extent_buffer *buf) |
| 734 | { |
| 735 | /* |
| 736 | * Tree blocks not in refernece counted trees and tree roots |
| 737 | * are never shared. If a block was allocated after the last |
| 738 | * snapshot and the block was not allocated by tree relocation, |
| 739 | * we know the block is not shared. |
| 740 | */ |
| 741 | if (root->ref_cows && |
| 742 | buf != root->node && buf != root->commit_root && |
| 743 | (btrfs_header_generation(buf) <= |
| 744 | btrfs_root_last_snapshot(&root->root_item) || |
| 745 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 746 | return 1; |
| 747 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 748 | if (root->ref_cows && |
| 749 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 750 | return 1; |
| 751 | #endif |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 756 | struct btrfs_root *root, |
| 757 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 758 | struct extent_buffer *cow, |
| 759 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 760 | { |
| 761 | u64 refs; |
| 762 | u64 owner; |
| 763 | u64 flags; |
| 764 | u64 new_flags = 0; |
| 765 | int ret; |
| 766 | |
| 767 | /* |
| 768 | * Backrefs update rules: |
| 769 | * |
| 770 | * Always use full backrefs for extent pointers in tree block |
| 771 | * allocated by tree relocation. |
| 772 | * |
| 773 | * If a shared tree block is no longer referenced by its owner |
| 774 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 775 | * use full backrefs for extent pointers in tree block. |
| 776 | * |
| 777 | * If a tree block is been relocating |
| 778 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 779 | * use full backrefs for extent pointers in tree block. |
| 780 | * The reason for this is some operations (such as drop tree) |
| 781 | * are only allowed for blocks use full backrefs. |
| 782 | */ |
| 783 | |
| 784 | if (btrfs_block_can_be_shared(root, buf)) { |
| 785 | ret = btrfs_lookup_extent_info(trans, root, buf->start, |
| 786 | buf->len, &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 787 | if (ret) |
| 788 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 789 | if (refs == 0) { |
| 790 | ret = -EROFS; |
| 791 | btrfs_std_error(root->fs_info, ret); |
| 792 | return ret; |
| 793 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 794 | } else { |
| 795 | refs = 1; |
| 796 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 797 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 798 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 799 | else |
| 800 | flags = 0; |
| 801 | } |
| 802 | |
| 803 | owner = btrfs_header_owner(buf); |
| 804 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 805 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 806 | |
| 807 | if (refs > 1) { |
| 808 | if ((owner == root->root_key.objectid || |
| 809 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 810 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 811 | ret = btrfs_inc_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 812 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 813 | |
| 814 | if (root->root_key.objectid == |
| 815 | BTRFS_TREE_RELOC_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 816 | ret = btrfs_dec_ref(trans, root, buf, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 817 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 818 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 819 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 820 | } |
| 821 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 822 | } else { |
| 823 | |
| 824 | if (root->root_key.objectid == |
| 825 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 826 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 827 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 828 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 829 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 830 | } |
| 831 | if (new_flags != 0) { |
| 832 | ret = btrfs_set_disk_extent_flags(trans, root, |
| 833 | buf->start, |
| 834 | buf->len, |
| 835 | new_flags, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 836 | if (ret) |
| 837 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 838 | } |
| 839 | } else { |
| 840 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 841 | if (root->root_key.objectid == |
| 842 | BTRFS_TREE_RELOC_OBJECTID) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 843 | ret = btrfs_inc_ref(trans, root, cow, 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 844 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 845 | ret = btrfs_inc_ref(trans, root, cow, 0, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 846 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 847 | ret = btrfs_dec_ref(trans, root, buf, 1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 848 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 849 | } |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 850 | /* |
| 851 | * don't log freeing in case we're freeing the root node, this |
| 852 | * is done by tree_mod_log_set_root_pointer later |
| 853 | */ |
| 854 | if (buf != root->node && btrfs_header_level(buf) != 0) |
| 855 | tree_mod_log_free_eb(root->fs_info, buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 856 | clean_tree_block(trans, root, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 857 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 858 | } |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 863 | * does the dirty work in cow of a single block. The parent block (if |
| 864 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 865 | * dirty and returned locked. If you modify the block it needs to be marked |
| 866 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 867 | * |
| 868 | * search_start -- an allocation hint for the new block |
| 869 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 870 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 871 | * bytes the allocator should try to find free next to the block it returns. |
| 872 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 873 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 874 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 875 | struct btrfs_root *root, |
| 876 | struct extent_buffer *buf, |
| 877 | struct extent_buffer *parent, int parent_slot, |
| 878 | struct extent_buffer **cow_ret, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 879 | u64 search_start, u64 empty_size) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 880 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 881 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 882 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 883 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 884 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 885 | int unlock_orig = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 886 | u64 parent_start; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 887 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 888 | if (*cow_ret == buf) |
| 889 | unlock_orig = 1; |
| 890 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 891 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 892 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 893 | WARN_ON(root->ref_cows && trans->transid != |
| 894 | root->fs_info->running_transaction->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 895 | WARN_ON(root->ref_cows && trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 896 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 897 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 898 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 899 | if (level == 0) |
| 900 | btrfs_item_key(buf, &disk_key, 0); |
| 901 | else |
| 902 | btrfs_node_key(buf, &disk_key, 0); |
| 903 | |
| 904 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 905 | if (parent) |
| 906 | parent_start = parent->start; |
| 907 | else |
| 908 | parent_start = 0; |
| 909 | } else |
| 910 | parent_start = 0; |
| 911 | |
| 912 | cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start, |
| 913 | root->root_key.objectid, &disk_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 914 | level, search_start, empty_size); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 915 | if (IS_ERR(cow)) |
| 916 | return PTR_ERR(cow); |
| 917 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 918 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 919 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 920 | copy_extent_buffer(cow, buf, 0, 0, cow->len); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 921 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 922 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 923 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 924 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 925 | BTRFS_HEADER_FLAG_RELOC); |
| 926 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 927 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 928 | else |
| 929 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 930 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 931 | write_extent_buffer(cow, root->fs_info->fsid, |
| 932 | (unsigned long)btrfs_header_fsid(cow), |
| 933 | BTRFS_FSID_SIZE); |
| 934 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 935 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 936 | if (ret) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 937 | btrfs_abort_transaction(trans, root, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 938 | return ret; |
| 939 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 940 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 941 | if (root->ref_cows) |
| 942 | btrfs_reloc_cow_block(trans, root, buf, cow); |
| 943 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 944 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 945 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 946 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 947 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 948 | parent_start = buf->start; |
| 949 | else |
| 950 | parent_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 951 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 952 | extent_buffer_get(cow); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 953 | tree_mod_log_set_root_pointer(root, cow); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 954 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 955 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 956 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 957 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 958 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 959 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 960 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 961 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 962 | parent_start = parent->start; |
| 963 | else |
| 964 | parent_start = 0; |
| 965 | |
| 966 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 967 | tree_mod_log_insert_key(root->fs_info, parent, parent_slot, |
| 968 | MOD_LOG_KEY_REPLACE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 969 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 970 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 971 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 972 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 973 | btrfs_mark_buffer_dirty(parent); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 974 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 975 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 976 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 977 | if (unlock_orig) |
| 978 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 979 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 980 | btrfs_mark_buffer_dirty(cow); |
| 981 | *cow_ret = cow; |
| 982 | return 0; |
| 983 | } |
| 984 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 985 | /* |
| 986 | * returns the logical address of the oldest predecessor of the given root. |
| 987 | * entries older than time_seq are ignored. |
| 988 | */ |
| 989 | static struct tree_mod_elem * |
| 990 | __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info, |
| 991 | struct btrfs_root *root, u64 time_seq) |
| 992 | { |
| 993 | struct tree_mod_elem *tm; |
| 994 | struct tree_mod_elem *found = NULL; |
| 995 | u64 root_logical = root->node->start; |
| 996 | int looped = 0; |
| 997 | |
| 998 | if (!time_seq) |
| 999 | return 0; |
| 1000 | |
| 1001 | /* |
| 1002 | * the very last operation that's logged for a root is the replacement |
| 1003 | * operation (if it is replaced at all). this has the index of the *new* |
| 1004 | * root, making it the very first operation that's logged for this root. |
| 1005 | */ |
| 1006 | while (1) { |
| 1007 | tm = tree_mod_log_search_oldest(fs_info, root_logical, |
| 1008 | time_seq); |
| 1009 | if (!looped && !tm) |
| 1010 | return 0; |
| 1011 | /* |
| 1012 | * we must have key remove operations in the log before the |
| 1013 | * replace operation. |
| 1014 | */ |
| 1015 | BUG_ON(!tm); |
| 1016 | |
| 1017 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1018 | break; |
| 1019 | |
| 1020 | found = tm; |
| 1021 | root_logical = tm->old_root.logical; |
| 1022 | BUG_ON(root_logical == root->node->start); |
| 1023 | looped = 1; |
| 1024 | } |
| 1025 | |
| 1026 | return found; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * tm is a pointer to the first operation to rewind within eb. then, all |
| 1031 | * previous operations will be rewinded (until we reach something older than |
| 1032 | * time_seq). |
| 1033 | */ |
| 1034 | static void |
| 1035 | __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq, |
| 1036 | struct tree_mod_elem *first_tm) |
| 1037 | { |
| 1038 | u32 n; |
| 1039 | struct rb_node *next; |
| 1040 | struct tree_mod_elem *tm = first_tm; |
| 1041 | unsigned long o_dst; |
| 1042 | unsigned long o_src; |
| 1043 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1044 | |
| 1045 | n = btrfs_header_nritems(eb); |
| 1046 | while (tm && tm->elem.seq >= time_seq) { |
| 1047 | /* |
| 1048 | * all the operations are recorded with the operator used for |
| 1049 | * the modification. as we're going backwards, we do the |
| 1050 | * opposite of each operation here. |
| 1051 | */ |
| 1052 | switch (tm->op) { |
| 1053 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1054 | BUG_ON(tm->slot < n); |
| 1055 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
| 1056 | case MOD_LOG_KEY_REMOVE: |
| 1057 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1058 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1059 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1060 | tm->generation); |
| 1061 | n++; |
| 1062 | break; |
| 1063 | case MOD_LOG_KEY_REPLACE: |
| 1064 | BUG_ON(tm->slot >= n); |
| 1065 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1066 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1067 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1068 | tm->generation); |
| 1069 | break; |
| 1070 | case MOD_LOG_KEY_ADD: |
| 1071 | if (tm->slot != n - 1) { |
| 1072 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1073 | o_src = btrfs_node_key_ptr_offset(tm->slot + 1); |
| 1074 | memmove_extent_buffer(eb, o_dst, o_src, p_size); |
| 1075 | } |
| 1076 | n--; |
| 1077 | break; |
| 1078 | case MOD_LOG_MOVE_KEYS: |
| 1079 | memmove_extent_buffer(eb, tm->slot, tm->move.dst_slot, |
| 1080 | tm->move.nr_items * p_size); |
| 1081 | break; |
| 1082 | case MOD_LOG_ROOT_REPLACE: |
| 1083 | /* |
| 1084 | * this operation is special. for roots, this must be |
| 1085 | * handled explicitly before rewinding. |
| 1086 | * for non-roots, this operation may exist if the node |
| 1087 | * was a root: root A -> child B; then A gets empty and |
| 1088 | * B is promoted to the new root. in the mod log, we'll |
| 1089 | * have a root-replace operation for B, a tree block |
| 1090 | * that is no root. we simply ignore that operation. |
| 1091 | */ |
| 1092 | break; |
| 1093 | } |
| 1094 | next = rb_next(&tm->node); |
| 1095 | if (!next) |
| 1096 | break; |
| 1097 | tm = container_of(next, struct tree_mod_elem, node); |
| 1098 | if (tm->index != first_tm->index) |
| 1099 | break; |
| 1100 | } |
| 1101 | btrfs_set_header_nritems(eb, n); |
| 1102 | } |
| 1103 | |
| 1104 | static struct extent_buffer * |
| 1105 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1106 | u64 time_seq) |
| 1107 | { |
| 1108 | struct extent_buffer *eb_rewin; |
| 1109 | struct tree_mod_elem *tm; |
| 1110 | |
| 1111 | if (!time_seq) |
| 1112 | return eb; |
| 1113 | |
| 1114 | if (btrfs_header_level(eb) == 0) |
| 1115 | return eb; |
| 1116 | |
| 1117 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1118 | if (!tm) |
| 1119 | return eb; |
| 1120 | |
| 1121 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1122 | BUG_ON(tm->slot != 0); |
| 1123 | eb_rewin = alloc_dummy_extent_buffer(eb->start, |
| 1124 | fs_info->tree_root->nodesize); |
| 1125 | BUG_ON(!eb_rewin); |
| 1126 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1127 | btrfs_set_header_backref_rev(eb_rewin, |
| 1128 | btrfs_header_backref_rev(eb)); |
| 1129 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
| 1130 | } else { |
| 1131 | eb_rewin = btrfs_clone_extent_buffer(eb); |
| 1132 | BUG_ON(!eb_rewin); |
| 1133 | } |
| 1134 | |
| 1135 | extent_buffer_get(eb_rewin); |
| 1136 | free_extent_buffer(eb); |
| 1137 | |
| 1138 | __tree_mod_log_rewind(eb_rewin, time_seq, tm); |
| 1139 | |
| 1140 | return eb_rewin; |
| 1141 | } |
| 1142 | |
| 1143 | static inline struct extent_buffer * |
| 1144 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1145 | { |
| 1146 | struct tree_mod_elem *tm; |
| 1147 | struct extent_buffer *eb; |
| 1148 | struct tree_mod_root *old_root; |
| 1149 | u64 old_generation; |
| 1150 | |
| 1151 | tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq); |
| 1152 | if (!tm) |
| 1153 | return root->node; |
| 1154 | |
| 1155 | old_root = &tm->old_root; |
| 1156 | old_generation = tm->generation; |
| 1157 | |
| 1158 | tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq); |
| 1159 | /* |
| 1160 | * there was an item in the log when __tree_mod_log_oldest_root |
| 1161 | * returned. this one must not go away, because the time_seq passed to |
| 1162 | * us must be blocking its removal. |
| 1163 | */ |
| 1164 | BUG_ON(!tm); |
| 1165 | |
| 1166 | if (old_root->logical == root->node->start) { |
| 1167 | /* there are logged operations for the current root */ |
| 1168 | eb = btrfs_clone_extent_buffer(root->node); |
| 1169 | } else { |
| 1170 | /* there's a root replace operation for the current root */ |
| 1171 | eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT, |
| 1172 | root->nodesize); |
| 1173 | btrfs_set_header_bytenr(eb, eb->start); |
| 1174 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
| 1175 | btrfs_set_header_owner(eb, root->root_key.objectid); |
| 1176 | } |
| 1177 | if (!eb) |
| 1178 | return NULL; |
| 1179 | btrfs_set_header_level(eb, old_root->level); |
| 1180 | btrfs_set_header_generation(eb, old_generation); |
| 1181 | __tree_mod_log_rewind(eb, time_seq, tm); |
| 1182 | |
| 1183 | return eb; |
| 1184 | } |
| 1185 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1186 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1187 | struct btrfs_root *root, |
| 1188 | struct extent_buffer *buf) |
| 1189 | { |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1190 | /* ensure we can see the force_cow */ |
| 1191 | smp_rmb(); |
| 1192 | |
| 1193 | /* |
| 1194 | * We do not need to cow a block if |
| 1195 | * 1) this block is not created or changed in this transaction; |
| 1196 | * 2) this block does not belong to TREE_RELOC tree; |
| 1197 | * 3) the root is not forced COW. |
| 1198 | * |
| 1199 | * What is forced COW: |
| 1200 | * when we create snapshot during commiting the transaction, |
| 1201 | * after we've finished coping src root, we must COW the shared |
| 1202 | * block to ensure the metadata consistency. |
| 1203 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1204 | if (btrfs_header_generation(buf) == trans->transid && |
| 1205 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1206 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1207 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
| 1208 | !root->force_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1209 | return 0; |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1213 | /* |
| 1214 | * cows a single block, see __btrfs_cow_block for the real work. |
| 1215 | * This version of it has extra checks so that a block isn't cow'd more than |
| 1216 | * once per transaction, as long as it hasn't been written yet |
| 1217 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1218 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1219 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1220 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1221 | struct extent_buffer **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1222 | { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1223 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1224 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1225 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1226 | if (trans->transaction != root->fs_info->running_transaction) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1227 | printk(KERN_CRIT "trans %llu running %llu\n", |
| 1228 | (unsigned long long)trans->transid, |
| 1229 | (unsigned long long) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1230 | root->fs_info->running_transaction->transid); |
| 1231 | WARN_ON(1); |
| 1232 | } |
| 1233 | if (trans->transid != root->fs_info->generation) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1234 | printk(KERN_CRIT "trans %llu running %llu\n", |
| 1235 | (unsigned long long)trans->transid, |
| 1236 | (unsigned long long)root->fs_info->generation); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1237 | WARN_ON(1); |
| 1238 | } |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1239 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1240 | if (!should_cow_block(trans, root, buf)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1241 | *cow_ret = buf; |
| 1242 | return 0; |
| 1243 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1244 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1245 | search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1246 | |
| 1247 | if (parent) |
| 1248 | btrfs_set_lock_blocking(parent); |
| 1249 | btrfs_set_lock_blocking(buf); |
| 1250 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1251 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1252 | parent_slot, cow_ret, search_start, 0); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1253 | |
| 1254 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1255 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1256 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1257 | } |
| 1258 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1259 | /* |
| 1260 | * helper function for defrag to decide if two blocks pointed to by a |
| 1261 | * node are actually close by |
| 1262 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1263 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1264 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1265 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1266 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1267 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1268 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1269 | return 0; |
| 1270 | } |
| 1271 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1272 | /* |
| 1273 | * compare two keys in a memcmp fashion |
| 1274 | */ |
| 1275 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
| 1276 | { |
| 1277 | struct btrfs_key k1; |
| 1278 | |
| 1279 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1280 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1281 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1282 | } |
| 1283 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1284 | /* |
| 1285 | * same as comp_keys only with two btrfs_key's |
| 1286 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1287 | 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] | 1288 | { |
| 1289 | if (k1->objectid > k2->objectid) |
| 1290 | return 1; |
| 1291 | if (k1->objectid < k2->objectid) |
| 1292 | return -1; |
| 1293 | if (k1->type > k2->type) |
| 1294 | return 1; |
| 1295 | if (k1->type < k2->type) |
| 1296 | return -1; |
| 1297 | if (k1->offset > k2->offset) |
| 1298 | return 1; |
| 1299 | if (k1->offset < k2->offset) |
| 1300 | return -1; |
| 1301 | return 0; |
| 1302 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1303 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1304 | /* |
| 1305 | * this is used by the defrag code to go through all the |
| 1306 | * leaves pointed to by a node and reallocate them so that |
| 1307 | * disk order is close to key order |
| 1308 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1309 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1310 | struct btrfs_root *root, struct extent_buffer *parent, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1311 | int start_slot, int cache_only, u64 *last_ret, |
| 1312 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1313 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1314 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1315 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1316 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1317 | u64 search_start = *last_ret; |
| 1318 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1319 | u64 other; |
| 1320 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1321 | int end_slot; |
| 1322 | int i; |
| 1323 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1324 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1325 | int uptodate; |
| 1326 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1327 | int progress_passed = 0; |
| 1328 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1329 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1330 | parent_level = btrfs_header_level(parent); |
| 1331 | if (cache_only && parent_level != 1) |
| 1332 | return 0; |
| 1333 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1334 | if (trans->transaction != root->fs_info->running_transaction) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1335 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1336 | if (trans->transid != root->fs_info->generation) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1337 | WARN_ON(1); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1338 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1339 | parent_nritems = btrfs_header_nritems(parent); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1340 | blocksize = btrfs_level_size(root, parent_level - 1); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1341 | end_slot = parent_nritems; |
| 1342 | |
| 1343 | if (parent_nritems == 1) |
| 1344 | return 0; |
| 1345 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1346 | btrfs_set_lock_blocking(parent); |
| 1347 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1348 | for (i = start_slot; i < end_slot; i++) { |
| 1349 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1350 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1351 | btrfs_node_key(parent, &disk_key, i); |
| 1352 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1353 | continue; |
| 1354 | |
| 1355 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1356 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1357 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1358 | if (last_block == 0) |
| 1359 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1360 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1361 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1362 | other = btrfs_node_blockptr(parent, i - 1); |
| 1363 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1364 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1365 | if (!close && i < end_slot - 2) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1366 | other = btrfs_node_blockptr(parent, i + 1); |
| 1367 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1368 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1369 | if (close) { |
| 1370 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1371 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1372 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1373 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1374 | cur = btrfs_find_tree_block(root, blocknr, blocksize); |
| 1375 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1376 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1377 | else |
| 1378 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1379 | if (!cur || !uptodate) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1380 | if (cache_only) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1381 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1382 | continue; |
| 1383 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1384 | if (!cur) { |
| 1385 | cur = read_tree_block(root, blocknr, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1386 | blocksize, gen); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1387 | if (!cur) |
| 1388 | return -EIO; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1389 | } else if (!uptodate) { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1390 | btrfs_read_buffer(cur, gen); |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1391 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1392 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1393 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1394 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1395 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1396 | btrfs_tree_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1397 | btrfs_set_lock_blocking(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1398 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1399 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1400 | min(16 * blocksize, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1401 | (end_slot - i) * blocksize)); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1402 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1403 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1404 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1405 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1406 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1407 | search_start = cur->start; |
| 1408 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1409 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1410 | btrfs_tree_unlock(cur); |
| 1411 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1412 | } |
| 1413 | return err; |
| 1414 | } |
| 1415 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1416 | /* |
| 1417 | * The leaf data grows from end-to-front in the node. |
| 1418 | * this returns the address of the start of the last item, |
| 1419 | * which is the stop of the leaf data stack |
| 1420 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1421 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1422 | struct extent_buffer *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1423 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1424 | u32 nr = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1425 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1426 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1427 | return btrfs_item_offset_nr(leaf, nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1428 | } |
| 1429 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1430 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1431 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1432 | * search for key in the extent_buffer. The items start at offset p, |
| 1433 | * and they are item_size apart. There are 'max' items in p. |
| 1434 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1435 | * the slot in the array is returned via slot, and it points to |
| 1436 | * the place where you would insert key if it is not found in |
| 1437 | * the array. |
| 1438 | * |
| 1439 | * slot may point to max if the key is bigger than all of the keys |
| 1440 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1441 | static noinline int generic_bin_search(struct extent_buffer *eb, |
| 1442 | unsigned long p, |
| 1443 | int item_size, struct btrfs_key *key, |
| 1444 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1445 | { |
| 1446 | int low = 0; |
| 1447 | int high = max; |
| 1448 | int mid; |
| 1449 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1450 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1451 | struct btrfs_disk_key unaligned; |
| 1452 | unsigned long offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1453 | char *kaddr = NULL; |
| 1454 | unsigned long map_start = 0; |
| 1455 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1456 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1457 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1458 | while (low < high) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1459 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1460 | offset = p + mid * item_size; |
| 1461 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1462 | if (!kaddr || offset < map_start || |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1463 | (offset + sizeof(struct btrfs_disk_key)) > |
| 1464 | map_start + map_len) { |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1465 | |
| 1466 | err = map_private_extent_buffer(eb, offset, |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1467 | sizeof(struct btrfs_disk_key), |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1468 | &kaddr, &map_start, &map_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1469 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1470 | if (!err) { |
| 1471 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1472 | map_start); |
| 1473 | } else { |
| 1474 | read_extent_buffer(eb, &unaligned, |
| 1475 | offset, sizeof(unaligned)); |
| 1476 | tmp = &unaligned; |
| 1477 | } |
| 1478 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1479 | } else { |
| 1480 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1481 | map_start); |
| 1482 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1483 | ret = comp_keys(tmp, key); |
| 1484 | |
| 1485 | if (ret < 0) |
| 1486 | low = mid + 1; |
| 1487 | else if (ret > 0) |
| 1488 | high = mid; |
| 1489 | else { |
| 1490 | *slot = mid; |
| 1491 | return 0; |
| 1492 | } |
| 1493 | } |
| 1494 | *slot = low; |
| 1495 | return 1; |
| 1496 | } |
| 1497 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1498 | /* |
| 1499 | * simple bin_search frontend that does the right thing for |
| 1500 | * leaves vs nodes |
| 1501 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1502 | static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1503 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1504 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1505 | if (level == 0) { |
| 1506 | return generic_bin_search(eb, |
| 1507 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1508 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1509 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1510 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1511 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1512 | return generic_bin_search(eb, |
| 1513 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1514 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1515 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1516 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1517 | } |
| 1518 | return -1; |
| 1519 | } |
| 1520 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1521 | int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, |
| 1522 | int level, int *slot) |
| 1523 | { |
| 1524 | return bin_search(eb, key, level, slot); |
| 1525 | } |
| 1526 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1527 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1528 | { |
| 1529 | spin_lock(&root->accounting_lock); |
| 1530 | btrfs_set_root_used(&root->root_item, |
| 1531 | btrfs_root_used(&root->root_item) + size); |
| 1532 | spin_unlock(&root->accounting_lock); |
| 1533 | } |
| 1534 | |
| 1535 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1536 | { |
| 1537 | spin_lock(&root->accounting_lock); |
| 1538 | btrfs_set_root_used(&root->root_item, |
| 1539 | btrfs_root_used(&root->root_item) - size); |
| 1540 | spin_unlock(&root->accounting_lock); |
| 1541 | } |
| 1542 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1543 | /* given a node and slot number, this reads the blocks it points to. The |
| 1544 | * extent buffer is returned with a reference taken (but unlocked). |
| 1545 | * NULL is returned on error. |
| 1546 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1547 | static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1548 | struct extent_buffer *parent, int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1549 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1550 | int level = btrfs_header_level(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1551 | if (slot < 0) |
| 1552 | return NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1553 | if (slot >= btrfs_header_nritems(parent)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1554 | return NULL; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1555 | |
| 1556 | BUG_ON(level == 0); |
| 1557 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1558 | return read_tree_block(root, btrfs_node_blockptr(parent, slot), |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1559 | btrfs_level_size(root, level - 1), |
| 1560 | btrfs_node_ptr_generation(parent, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1561 | } |
| 1562 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1563 | /* |
| 1564 | * node level balancing, used to make sure nodes are in proper order for |
| 1565 | * item deletion. We balance from the top down, so we have to make sure |
| 1566 | * that a deletion won't leave an node completely empty later on. |
| 1567 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1568 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1569 | struct btrfs_root *root, |
| 1570 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1571 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1572 | struct extent_buffer *right = NULL; |
| 1573 | struct extent_buffer *mid; |
| 1574 | struct extent_buffer *left = NULL; |
| 1575 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1576 | int ret = 0; |
| 1577 | int wret; |
| 1578 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1579 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1580 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1581 | |
| 1582 | if (level == 0) |
| 1583 | return 0; |
| 1584 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1585 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1586 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1587 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1588 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1589 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1590 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1591 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1592 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1593 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1594 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1595 | pslot = path->slots[level + 1]; |
| 1596 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1597 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1598 | /* |
| 1599 | * deal with the case where there is only one pointer in the root |
| 1600 | * by promoting the node below to a root |
| 1601 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1602 | if (!parent) { |
| 1603 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1604 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1605 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1606 | return 0; |
| 1607 | |
| 1608 | /* promote the child to a root */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1609 | child = read_node_slot(root, mid, 0); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1610 | if (!child) { |
| 1611 | ret = -EROFS; |
| 1612 | btrfs_std_error(root->fs_info, ret); |
| 1613 | goto enospc; |
| 1614 | } |
| 1615 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1616 | btrfs_tree_lock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1617 | btrfs_set_lock_blocking(child); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1618 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1619 | if (ret) { |
| 1620 | btrfs_tree_unlock(child); |
| 1621 | free_extent_buffer(child); |
| 1622 | goto enospc; |
| 1623 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1624 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1625 | tree_mod_log_set_root_pointer(root, child); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1626 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1627 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1628 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1629 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1630 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1631 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1632 | path->nodes[level] = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1633 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1634 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1635 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1636 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1637 | |
| 1638 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1639 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1640 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1641 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1642 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1643 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1644 | if (btrfs_header_nritems(mid) > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1645 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1646 | return 0; |
| 1647 | |
Andi Kleen | 559af82 | 2010-10-29 15:14:37 -0400 | [diff] [blame] | 1648 | btrfs_header_nritems(mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1649 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1650 | left = read_node_slot(root, parent, pslot - 1); |
| 1651 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1652 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1653 | btrfs_set_lock_blocking(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1654 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1655 | parent, pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1656 | if (wret) { |
| 1657 | ret = wret; |
| 1658 | goto enospc; |
| 1659 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1660 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1661 | right = read_node_slot(root, parent, pslot + 1); |
| 1662 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1663 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1664 | btrfs_set_lock_blocking(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1665 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1666 | parent, pslot + 1, &right); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1667 | if (wret) { |
| 1668 | ret = wret; |
| 1669 | goto enospc; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1674 | if (left) { |
| 1675 | orig_slot += btrfs_header_nritems(left); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1676 | wret = push_node_left(trans, root, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1677 | if (wret < 0) |
| 1678 | ret = wret; |
Andi Kleen | 559af82 | 2010-10-29 15:14:37 -0400 | [diff] [blame] | 1679 | btrfs_header_nritems(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1680 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1681 | |
| 1682 | /* |
| 1683 | * then try to empty the right most buffer into the middle |
| 1684 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1685 | if (right) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1686 | wret = push_node_left(trans, root, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1687 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1688 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1689 | if (btrfs_header_nritems(right) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1690 | clean_tree_block(trans, root, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1691 | btrfs_tree_unlock(right); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 1692 | del_ptr(trans, root, path, level + 1, pslot + 1, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1693 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1694 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1695 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1696 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1697 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1698 | struct btrfs_disk_key right_key; |
| 1699 | btrfs_node_key(right, &right_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1700 | tree_mod_log_set_node_key(root->fs_info, parent, |
| 1701 | &right_key, pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1702 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1703 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1704 | } |
| 1705 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1706 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1707 | /* |
| 1708 | * we're not allowed to leave a node with one item in the |
| 1709 | * tree during a delete. A deletion from lower in the tree |
| 1710 | * could try to delete the only pointer in this node. |
| 1711 | * So, pull some keys from the left. |
| 1712 | * There has to be a left pointer at this point because |
| 1713 | * otherwise we would have pulled some pointers from the |
| 1714 | * right |
| 1715 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1716 | if (!left) { |
| 1717 | ret = -EROFS; |
| 1718 | btrfs_std_error(root->fs_info, ret); |
| 1719 | goto enospc; |
| 1720 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1721 | wret = balance_node_right(trans, root, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1722 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1723 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1724 | goto enospc; |
| 1725 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1726 | if (wret == 1) { |
| 1727 | wret = push_node_left(trans, root, left, mid, 1); |
| 1728 | if (wret < 0) |
| 1729 | ret = wret; |
| 1730 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1731 | BUG_ON(wret == 1); |
| 1732 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1733 | if (btrfs_header_nritems(mid) == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1734 | clean_tree_block(trans, root, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1735 | btrfs_tree_unlock(mid); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 1736 | del_ptr(trans, root, path, level + 1, pslot, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1737 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1738 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1739 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1740 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1741 | } else { |
| 1742 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1743 | struct btrfs_disk_key mid_key; |
| 1744 | btrfs_node_key(mid, &mid_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1745 | tree_mod_log_set_node_key(root->fs_info, parent, &mid_key, |
| 1746 | pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1747 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1748 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1749 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1750 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1751 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1752 | if (left) { |
| 1753 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1754 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1755 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1756 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1757 | path->slots[level + 1] -= 1; |
| 1758 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1759 | if (mid) { |
| 1760 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1761 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1762 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1763 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1764 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1765 | path->slots[level] = orig_slot; |
| 1766 | } |
| 1767 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1768 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1769 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1770 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1771 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1772 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1773 | if (right) { |
| 1774 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1775 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1776 | } |
| 1777 | if (left) { |
| 1778 | if (path->nodes[level] != left) |
| 1779 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1780 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1781 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1782 | return ret; |
| 1783 | } |
| 1784 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1785 | /* Node balancing for insertion. Here we only split or push nodes around |
| 1786 | * when they are completely full. This is also done top down, so we |
| 1787 | * have to be pessimistic. |
| 1788 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1789 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1790 | struct btrfs_root *root, |
| 1791 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1792 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1793 | struct extent_buffer *right = NULL; |
| 1794 | struct extent_buffer *mid; |
| 1795 | struct extent_buffer *left = NULL; |
| 1796 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1797 | int ret = 0; |
| 1798 | int wret; |
| 1799 | int pslot; |
| 1800 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1801 | |
| 1802 | if (level == 0) |
| 1803 | return 1; |
| 1804 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1805 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1806 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1807 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1808 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1809 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1810 | pslot = path->slots[level + 1]; |
| 1811 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1812 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1813 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1814 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1815 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1816 | left = read_node_slot(root, parent, pslot - 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1817 | |
| 1818 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1819 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1820 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1821 | |
| 1822 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1823 | btrfs_set_lock_blocking(left); |
| 1824 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1825 | left_nr = btrfs_header_nritems(left); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1826 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1827 | wret = 1; |
| 1828 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1829 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1830 | pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1831 | if (ret) |
| 1832 | wret = 1; |
| 1833 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1834 | wret = push_node_left(trans, root, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 1835 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1836 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1837 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1838 | if (wret < 0) |
| 1839 | ret = wret; |
| 1840 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1841 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1842 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1843 | btrfs_node_key(mid, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1844 | tree_mod_log_set_node_key(root->fs_info, parent, |
| 1845 | &disk_key, pslot, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1846 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 1847 | btrfs_mark_buffer_dirty(parent); |
| 1848 | if (btrfs_header_nritems(left) > orig_slot) { |
| 1849 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1850 | path->slots[level + 1] -= 1; |
| 1851 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1852 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1853 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1854 | } else { |
| 1855 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1856 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1857 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1858 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1859 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1860 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1863 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1864 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1865 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1866 | right = read_node_slot(root, parent, pslot + 1); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1867 | |
| 1868 | /* |
| 1869 | * then try to empty the right most buffer into the middle |
| 1870 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1871 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1872 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1873 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1874 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1875 | btrfs_set_lock_blocking(right); |
| 1876 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1877 | right_nr = btrfs_header_nritems(right); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1878 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 1879 | wret = 1; |
| 1880 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1881 | ret = btrfs_cow_block(trans, root, right, |
| 1882 | parent, pslot + 1, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1883 | &right); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1884 | if (ret) |
| 1885 | wret = 1; |
| 1886 | else { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1887 | wret = balance_node_right(trans, root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1888 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1889 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 1890 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1891 | if (wret < 0) |
| 1892 | ret = wret; |
| 1893 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1894 | struct btrfs_disk_key disk_key; |
| 1895 | |
| 1896 | btrfs_node_key(right, &disk_key, 0); |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 1897 | tree_mod_log_set_node_key(root->fs_info, parent, |
| 1898 | &disk_key, pslot + 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1899 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 1900 | btrfs_mark_buffer_dirty(parent); |
| 1901 | |
| 1902 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 1903 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1904 | path->slots[level + 1] += 1; |
| 1905 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1906 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1907 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1908 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1909 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1910 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1911 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1912 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1913 | return 0; |
| 1914 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1915 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1916 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1917 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 1918 | return 1; |
| 1919 | } |
| 1920 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1921 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1922 | * readahead one full node of leaves, finding things that are close |
| 1923 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1924 | */ |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1925 | static void reada_for_search(struct btrfs_root *root, |
| 1926 | struct btrfs_path *path, |
| 1927 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1928 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1929 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 1930 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1931 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1932 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 1933 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1934 | u64 nread = 0; |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 1935 | u64 gen; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1936 | int direction = path->reada; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1937 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1938 | u32 nr; |
| 1939 | u32 blocksize; |
| 1940 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1941 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1942 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1943 | return; |
| 1944 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1945 | if (!path->nodes[level]) |
| 1946 | return; |
| 1947 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1948 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1949 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1950 | search = btrfs_node_blockptr(node, slot); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1951 | blocksize = btrfs_level_size(root, level - 1); |
| 1952 | eb = btrfs_find_tree_block(root, search, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1953 | if (eb) { |
| 1954 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 1958 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1959 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1960 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1961 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 1962 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1963 | while (1) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1964 | if (direction < 0) { |
| 1965 | if (nr == 0) |
| 1966 | break; |
| 1967 | nr--; |
| 1968 | } else if (direction > 0) { |
| 1969 | nr++; |
| 1970 | if (nr >= nritems) |
| 1971 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1972 | } |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 1973 | if (path->reada < 0 && objectid) { |
| 1974 | btrfs_node_key(node, &disk_key, nr); |
| 1975 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 1976 | break; |
| 1977 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1978 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 1979 | if ((search <= target && target - search <= 65536) || |
| 1980 | (search > target && search - target <= 65536)) { |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 1981 | gen = btrfs_node_ptr_generation(node, nr); |
Josef Bacik | cb25c2e | 2011-05-11 12:17:34 -0400 | [diff] [blame] | 1982 | readahead_tree_block(root, search, blocksize, gen); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1983 | nread += blocksize; |
| 1984 | } |
| 1985 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 1986 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1987 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 1988 | } |
| 1989 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1990 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1991 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1992 | * returns -EAGAIN if it had to drop the path, or zero if everything was in |
| 1993 | * cache |
| 1994 | */ |
| 1995 | static noinline int reada_for_balance(struct btrfs_root *root, |
| 1996 | struct btrfs_path *path, int level) |
| 1997 | { |
| 1998 | int slot; |
| 1999 | int nritems; |
| 2000 | struct extent_buffer *parent; |
| 2001 | struct extent_buffer *eb; |
| 2002 | u64 gen; |
| 2003 | u64 block1 = 0; |
| 2004 | u64 block2 = 0; |
| 2005 | int ret = 0; |
| 2006 | int blocksize; |
| 2007 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2008 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2009 | if (!parent) |
| 2010 | return 0; |
| 2011 | |
| 2012 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2013 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2014 | blocksize = btrfs_level_size(root, level); |
| 2015 | |
| 2016 | if (slot > 0) { |
| 2017 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2018 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
| 2019 | eb = btrfs_find_tree_block(root, block1, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2020 | /* |
| 2021 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2022 | * don't want to return eagain here. That will loop |
| 2023 | * forever |
| 2024 | */ |
| 2025 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2026 | block1 = 0; |
| 2027 | free_extent_buffer(eb); |
| 2028 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2029 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2030 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2031 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
| 2032 | eb = btrfs_find_tree_block(root, block2, blocksize); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2033 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2034 | block2 = 0; |
| 2035 | free_extent_buffer(eb); |
| 2036 | } |
| 2037 | if (block1 || block2) { |
| 2038 | ret = -EAGAIN; |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2039 | |
| 2040 | /* release the whole path */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2041 | btrfs_release_path(path); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2042 | |
| 2043 | /* read the blocks */ |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2044 | if (block1) |
| 2045 | readahead_tree_block(root, block1, blocksize, 0); |
| 2046 | if (block2) |
| 2047 | readahead_tree_block(root, block2, blocksize, 0); |
| 2048 | |
| 2049 | if (block1) { |
| 2050 | eb = read_tree_block(root, block1, blocksize, 0); |
| 2051 | free_extent_buffer(eb); |
| 2052 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2053 | if (block2) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2054 | eb = read_tree_block(root, block2, blocksize, 0); |
| 2055 | free_extent_buffer(eb); |
| 2056 | } |
| 2057 | } |
| 2058 | return ret; |
| 2059 | } |
| 2060 | |
| 2061 | |
| 2062 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2063 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2064 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2065 | * operations on the tree might require changing key pointers higher up in the |
| 2066 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2067 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2068 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2069 | * the lock if the path points to the last slot in the block. This is part of |
| 2070 | * 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] | 2071 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2072 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2073 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2074 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2075 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2076 | int lowest_unlock, int min_write_lock_level, |
| 2077 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2078 | { |
| 2079 | int i; |
| 2080 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2081 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2082 | struct extent_buffer *t; |
| 2083 | |
| 2084 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2085 | if (!path->nodes[i]) |
| 2086 | break; |
| 2087 | if (!path->locks[i]) |
| 2088 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2089 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2090 | skip_level = i + 1; |
| 2091 | continue; |
| 2092 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2093 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2094 | u32 nritems; |
| 2095 | t = path->nodes[i]; |
| 2096 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2097 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2098 | skip_level = i + 1; |
| 2099 | continue; |
| 2100 | } |
| 2101 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2102 | if (skip_level < i && i >= lowest_unlock) |
| 2103 | no_skips = 1; |
| 2104 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2105 | t = path->nodes[i]; |
| 2106 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2107 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2108 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2109 | if (write_lock_level && |
| 2110 | i > min_write_lock_level && |
| 2111 | i <= *write_lock_level) { |
| 2112 | *write_lock_level = i - 1; |
| 2113 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2118 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2119 | * This releases any locks held in the path starting at level and |
| 2120 | * going all the way up to the root. |
| 2121 | * |
| 2122 | * btrfs_search_slot will keep the lock held on higher nodes in a few |
| 2123 | * corner cases, such as COW of the block at slot zero in the node. This |
| 2124 | * ignores those rules, and it should only be called when there are no |
| 2125 | * more updates to be done higher up in the tree. |
| 2126 | */ |
| 2127 | noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) |
| 2128 | { |
| 2129 | int i; |
| 2130 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2131 | if (path->keep_locks) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2132 | return; |
| 2133 | |
| 2134 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2135 | if (!path->nodes[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2136 | continue; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2137 | if (!path->locks[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2138 | continue; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2139 | btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2140 | path->locks[i] = 0; |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2145 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2146 | * in cache without setting the path to blocking. If we find the block |
| 2147 | * we return zero and the path is unchanged. |
| 2148 | * |
| 2149 | * If we can't find the block, we set the path blocking and do some |
| 2150 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2151 | */ |
| 2152 | static int |
| 2153 | read_block_for_search(struct btrfs_trans_handle *trans, |
| 2154 | struct btrfs_root *root, struct btrfs_path *p, |
| 2155 | struct extent_buffer **eb_ret, int level, int slot, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2156 | struct btrfs_key *key, u64 time_seq) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2157 | { |
| 2158 | u64 blocknr; |
| 2159 | u64 gen; |
| 2160 | u32 blocksize; |
| 2161 | struct extent_buffer *b = *eb_ret; |
| 2162 | struct extent_buffer *tmp; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2163 | int ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2164 | |
| 2165 | blocknr = btrfs_node_blockptr(b, slot); |
| 2166 | gen = btrfs_node_ptr_generation(b, slot); |
| 2167 | blocksize = btrfs_level_size(root, level - 1); |
| 2168 | |
| 2169 | tmp = btrfs_find_tree_block(root, blocknr, blocksize); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2170 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2171 | /* first we do an atomic uptodate check */ |
| 2172 | if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) { |
| 2173 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2174 | /* |
| 2175 | * we found an up to date block without |
| 2176 | * sleeping, return |
| 2177 | * right away |
| 2178 | */ |
| 2179 | *eb_ret = tmp; |
| 2180 | return 0; |
| 2181 | } |
| 2182 | /* the pages were up to date, but we failed |
| 2183 | * the generation number check. Do a full |
| 2184 | * read for the generation number that is correct. |
| 2185 | * We must do this without dropping locks so |
| 2186 | * we can trust our generation number |
| 2187 | */ |
| 2188 | free_extent_buffer(tmp); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2189 | btrfs_set_path_blocking(p); |
| 2190 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2191 | /* now we're allowed to do a blocking uptodate check */ |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2192 | tmp = read_tree_block(root, blocknr, blocksize, gen); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2193 | if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) { |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2194 | *eb_ret = tmp; |
| 2195 | return 0; |
| 2196 | } |
| 2197 | free_extent_buffer(tmp); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2198 | btrfs_release_path(p); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2199 | return -EIO; |
| 2200 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * reduce lock contention at high levels |
| 2205 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2206 | * we read. Don't release the lock on the current |
| 2207 | * level because we need to walk this node to figure |
| 2208 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2209 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2210 | btrfs_unlock_up_safe(p, level + 1); |
| 2211 | btrfs_set_path_blocking(p); |
| 2212 | |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2213 | free_extent_buffer(tmp); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2214 | if (p->reada) |
| 2215 | reada_for_search(root, p, level, slot, key->objectid); |
| 2216 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2217 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2218 | |
| 2219 | ret = -EAGAIN; |
Yan, Zheng | 5bdd353 | 2010-05-26 11:20:30 -0400 | [diff] [blame] | 2220 | tmp = read_tree_block(root, blocknr, blocksize, 0); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2221 | if (tmp) { |
| 2222 | /* |
| 2223 | * If the read above didn't mark this buffer up to date, |
| 2224 | * it will never end up being up to date. Set ret to EIO now |
| 2225 | * and give up so that our caller doesn't loop forever |
| 2226 | * on our EAGAINs. |
| 2227 | */ |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2228 | if (!btrfs_buffer_uptodate(tmp, 0, 0)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2229 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2230 | free_extent_buffer(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2231 | } |
| 2232 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | * helper function for btrfs_search_slot. This does all of the checks |
| 2237 | * for node-level blocks and does any balancing required based on |
| 2238 | * the ins_len. |
| 2239 | * |
| 2240 | * If no extra work was required, zero is returned. If we had to |
| 2241 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2242 | * start over |
| 2243 | */ |
| 2244 | static int |
| 2245 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2246 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2247 | struct extent_buffer *b, int level, int ins_len, |
| 2248 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2249 | { |
| 2250 | int ret; |
| 2251 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
| 2252 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { |
| 2253 | int sret; |
| 2254 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2255 | if (*write_lock_level < level + 1) { |
| 2256 | *write_lock_level = level + 1; |
| 2257 | btrfs_release_path(p); |
| 2258 | goto again; |
| 2259 | } |
| 2260 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2261 | sret = reada_for_balance(root, p, level); |
| 2262 | if (sret) |
| 2263 | goto again; |
| 2264 | |
| 2265 | btrfs_set_path_blocking(p); |
| 2266 | sret = split_node(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2267 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2268 | |
| 2269 | BUG_ON(sret > 0); |
| 2270 | if (sret) { |
| 2271 | ret = sret; |
| 2272 | goto done; |
| 2273 | } |
| 2274 | b = p->nodes[level]; |
| 2275 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Chris Mason | cfbb930 | 2009-05-18 10:41:58 -0400 | [diff] [blame] | 2276 | BTRFS_NODEPTRS_PER_BLOCK(root) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2277 | int sret; |
| 2278 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2279 | if (*write_lock_level < level + 1) { |
| 2280 | *write_lock_level = level + 1; |
| 2281 | btrfs_release_path(p); |
| 2282 | goto again; |
| 2283 | } |
| 2284 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2285 | sret = reada_for_balance(root, p, level); |
| 2286 | if (sret) |
| 2287 | goto again; |
| 2288 | |
| 2289 | btrfs_set_path_blocking(p); |
| 2290 | sret = balance_level(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2291 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2292 | |
| 2293 | if (sret) { |
| 2294 | ret = sret; |
| 2295 | goto done; |
| 2296 | } |
| 2297 | b = p->nodes[level]; |
| 2298 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2299 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2300 | goto again; |
| 2301 | } |
| 2302 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2303 | } |
| 2304 | return 0; |
| 2305 | |
| 2306 | again: |
| 2307 | ret = -EAGAIN; |
| 2308 | done: |
| 2309 | return ret; |
| 2310 | } |
| 2311 | |
| 2312 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2313 | * look for key in the tree. path is filled in with nodes along the way |
| 2314 | * if key is found, we return zero and you can find the item in the leaf |
| 2315 | * level of the path (level 0) |
| 2316 | * |
| 2317 | * 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] | 2318 | * be inserted, and 1 is returned. If there are other errors during the |
| 2319 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2320 | * |
| 2321 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 2322 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 2323 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2324 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2325 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 2326 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 2327 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2328 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2329 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2330 | int slot; |
| 2331 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2332 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2333 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2334 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2335 | int root_lock; |
| 2336 | /* everything at write_lock_level or lower must be write locked */ |
| 2337 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2338 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2339 | int min_write_lock_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2340 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2341 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2342 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2343 | WARN_ON(p->nodes[0] != NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2344 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2345 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2346 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2347 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2348 | /* when we are removing items, we might have to go up to level |
| 2349 | * two as we update tree pointers Make sure we keep write |
| 2350 | * for those levels as well |
| 2351 | */ |
| 2352 | write_lock_level = 2; |
| 2353 | } else if (ins_len > 0) { |
| 2354 | /* |
| 2355 | * for inserting items, make sure we have a write lock on |
| 2356 | * level 1 so we can update keys |
| 2357 | */ |
| 2358 | write_lock_level = 1; |
| 2359 | } |
| 2360 | |
| 2361 | if (!cow) |
| 2362 | write_lock_level = -1; |
| 2363 | |
| 2364 | if (cow && (p->keep_locks || p->lowest_level)) |
| 2365 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2366 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2367 | min_write_lock_level = write_lock_level; |
| 2368 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2369 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2370 | /* |
| 2371 | * we try very hard to do read locks on the root |
| 2372 | */ |
| 2373 | root_lock = BTRFS_READ_LOCK; |
| 2374 | level = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2375 | if (p->search_commit_root) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2376 | /* |
| 2377 | * the commit roots are read only |
| 2378 | * so we always do read locks |
| 2379 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2380 | b = root->commit_root; |
| 2381 | extent_buffer_get(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2382 | level = btrfs_header_level(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2383 | if (!p->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2384 | btrfs_tree_read_lock(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2385 | } else { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2386 | if (p->skip_locking) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2387 | b = btrfs_root_node(root); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2388 | level = btrfs_header_level(b); |
| 2389 | } else { |
| 2390 | /* we don't know the level of the root node |
| 2391 | * until we actually have it read locked |
| 2392 | */ |
| 2393 | b = btrfs_read_lock_root_node(root); |
| 2394 | level = btrfs_header_level(b); |
| 2395 | if (level <= write_lock_level) { |
| 2396 | /* whoops, must trade for write lock */ |
| 2397 | btrfs_tree_read_unlock(b); |
| 2398 | free_extent_buffer(b); |
| 2399 | b = btrfs_lock_root_node(root); |
| 2400 | root_lock = BTRFS_WRITE_LOCK; |
| 2401 | |
| 2402 | /* the level might have changed, check again */ |
| 2403 | level = btrfs_header_level(b); |
| 2404 | } |
| 2405 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2406 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2407 | p->nodes[level] = b; |
| 2408 | if (!p->skip_locking) |
| 2409 | p->locks[level] = root_lock; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2410 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2411 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2412 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2413 | |
| 2414 | /* |
| 2415 | * setup the path here so we can release it under lock |
| 2416 | * contention with the cow code |
| 2417 | */ |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2418 | if (cow) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2419 | /* |
| 2420 | * if we don't really need to cow this block |
| 2421 | * then we don't want to set the path blocking, |
| 2422 | * so we test it here |
| 2423 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2424 | if (!should_cow_block(trans, root, b)) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2425 | goto cow_done; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2426 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2427 | btrfs_set_path_blocking(p); |
| 2428 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2429 | /* |
| 2430 | * must have write locks on this node and the |
| 2431 | * parent |
| 2432 | */ |
| 2433 | if (level + 1 > write_lock_level) { |
| 2434 | write_lock_level = level + 1; |
| 2435 | btrfs_release_path(p); |
| 2436 | goto again; |
| 2437 | } |
| 2438 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2439 | err = btrfs_cow_block(trans, root, b, |
| 2440 | p->nodes[level + 1], |
| 2441 | p->slots[level + 1], &b); |
| 2442 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2443 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2444 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2445 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2446 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2447 | cow_done: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2448 | BUG_ON(!cow && ins_len); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2449 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2450 | p->nodes[level] = b; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2451 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2452 | |
| 2453 | /* |
| 2454 | * we have a lock on b and as long as we aren't changing |
| 2455 | * the tree, there is no way to for the items in b to change. |
| 2456 | * It is safe to drop the lock on our parent before we |
| 2457 | * go through the expensive btree search on b. |
| 2458 | * |
| 2459 | * If cow is true, then we might be changing slot zero, |
| 2460 | * which may require changing the parent. So, we can't |
| 2461 | * drop the lock until after we know which slot we're |
| 2462 | * operating on. |
| 2463 | */ |
| 2464 | if (!cow) |
| 2465 | btrfs_unlock_up_safe(p, level + 1); |
| 2466 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2467 | ret = bin_search(b, key, level, &slot); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2468 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2469 | if (level != 0) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2470 | int dec = 0; |
| 2471 | if (ret && slot > 0) { |
| 2472 | dec = 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2473 | slot -= 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2474 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2475 | p->slots[level] = slot; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2476 | err = setup_nodes_for_search(trans, root, p, b, level, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2477 | ins_len, &write_lock_level); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2478 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2479 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2480 | if (err) { |
| 2481 | ret = err; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2482 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2483 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2484 | b = p->nodes[level]; |
| 2485 | slot = p->slots[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2486 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2487 | /* |
| 2488 | * slot 0 is special, if we change the key |
| 2489 | * we have to update the parent pointer |
| 2490 | * which means we must have a write lock |
| 2491 | * on the parent |
| 2492 | */ |
| 2493 | if (slot == 0 && cow && |
| 2494 | write_lock_level < level + 1) { |
| 2495 | write_lock_level = level + 1; |
| 2496 | btrfs_release_path(p); |
| 2497 | goto again; |
| 2498 | } |
| 2499 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2500 | unlock_up(p, level, lowest_unlock, |
| 2501 | min_write_lock_level, &write_lock_level); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 2502 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2503 | if (level == lowest_level) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2504 | if (dec) |
| 2505 | p->slots[level]++; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2506 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2507 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2508 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2509 | err = read_block_for_search(trans, root, p, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2510 | &b, level, slot, key, 0); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2511 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2512 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2513 | if (err) { |
| 2514 | ret = err; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2515 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2516 | } |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2517 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2518 | if (!p->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2519 | level = btrfs_header_level(b); |
| 2520 | if (level <= write_lock_level) { |
| 2521 | err = btrfs_try_tree_write_lock(b); |
| 2522 | if (!err) { |
| 2523 | btrfs_set_path_blocking(p); |
| 2524 | btrfs_tree_lock(b); |
| 2525 | btrfs_clear_path_blocking(p, b, |
| 2526 | BTRFS_WRITE_LOCK); |
| 2527 | } |
| 2528 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2529 | } else { |
| 2530 | err = btrfs_try_tree_read_lock(b); |
| 2531 | if (!err) { |
| 2532 | btrfs_set_path_blocking(p); |
| 2533 | btrfs_tree_read_lock(b); |
| 2534 | btrfs_clear_path_blocking(p, b, |
| 2535 | BTRFS_READ_LOCK); |
| 2536 | } |
| 2537 | p->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2538 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2539 | p->nodes[level] = b; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2540 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2541 | } else { |
| 2542 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2543 | if (ins_len > 0 && |
| 2544 | btrfs_leaf_free_space(root, b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2545 | if (write_lock_level < 1) { |
| 2546 | write_lock_level = 1; |
| 2547 | btrfs_release_path(p); |
| 2548 | goto again; |
| 2549 | } |
| 2550 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2551 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2552 | err = split_leaf(trans, root, key, |
| 2553 | p, ins_len, ret == 0); |
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 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2556 | BUG_ON(err > 0); |
| 2557 | if (err) { |
| 2558 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2559 | goto done; |
| 2560 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2561 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2562 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2563 | unlock_up(p, level, lowest_unlock, |
| 2564 | min_write_lock_level, &write_lock_level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2565 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2566 | } |
| 2567 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2568 | ret = 1; |
| 2569 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2570 | /* |
| 2571 | * we don't really know what they plan on doing with the path |
| 2572 | * from here on, so for now just mark it as blocking |
| 2573 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2574 | if (!p->leave_spinning) |
| 2575 | btrfs_set_path_blocking(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2576 | if (ret < 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2577 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2578 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2579 | } |
| 2580 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2581 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2582 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2583 | * current state of the tree together with the operations recorded in the tree |
| 2584 | * modification log to search for the key in a previous version of this tree, as |
| 2585 | * denoted by the time_seq parameter. |
| 2586 | * |
| 2587 | * Naturally, there is no support for insert, delete or cow operations. |
| 2588 | * |
| 2589 | * The resulting path and return value will be set up as if we called |
| 2590 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2591 | */ |
| 2592 | int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 2593 | struct btrfs_path *p, u64 time_seq) |
| 2594 | { |
| 2595 | struct extent_buffer *b; |
| 2596 | int slot; |
| 2597 | int ret; |
| 2598 | int err; |
| 2599 | int level; |
| 2600 | int lowest_unlock = 1; |
| 2601 | u8 lowest_level = 0; |
| 2602 | |
| 2603 | lowest_level = p->lowest_level; |
| 2604 | WARN_ON(p->nodes[0] != NULL); |
| 2605 | |
| 2606 | if (p->search_commit_root) { |
| 2607 | BUG_ON(time_seq); |
| 2608 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2609 | } |
| 2610 | |
| 2611 | again: |
| 2612 | level = 0; |
| 2613 | b = get_old_root(root, time_seq); |
| 2614 | extent_buffer_get(b); |
| 2615 | level = btrfs_header_level(b); |
| 2616 | btrfs_tree_read_lock(b); |
| 2617 | p->locks[level] = BTRFS_READ_LOCK; |
| 2618 | |
| 2619 | while (b) { |
| 2620 | level = btrfs_header_level(b); |
| 2621 | p->nodes[level] = b; |
| 2622 | btrfs_clear_path_blocking(p, NULL, 0); |
| 2623 | |
| 2624 | /* |
| 2625 | * we have a lock on b and as long as we aren't changing |
| 2626 | * the tree, there is no way to for the items in b to change. |
| 2627 | * It is safe to drop the lock on our parent before we |
| 2628 | * go through the expensive btree search on b. |
| 2629 | */ |
| 2630 | btrfs_unlock_up_safe(p, level + 1); |
| 2631 | |
| 2632 | ret = bin_search(b, key, level, &slot); |
| 2633 | |
| 2634 | if (level != 0) { |
| 2635 | int dec = 0; |
| 2636 | if (ret && slot > 0) { |
| 2637 | dec = 1; |
| 2638 | slot -= 1; |
| 2639 | } |
| 2640 | p->slots[level] = slot; |
| 2641 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2642 | |
| 2643 | if (level == lowest_level) { |
| 2644 | if (dec) |
| 2645 | p->slots[level]++; |
| 2646 | goto done; |
| 2647 | } |
| 2648 | |
| 2649 | err = read_block_for_search(NULL, root, p, &b, level, |
| 2650 | slot, key, time_seq); |
| 2651 | if (err == -EAGAIN) |
| 2652 | goto again; |
| 2653 | if (err) { |
| 2654 | ret = err; |
| 2655 | goto done; |
| 2656 | } |
| 2657 | |
| 2658 | level = btrfs_header_level(b); |
| 2659 | err = btrfs_try_tree_read_lock(b); |
| 2660 | if (!err) { |
| 2661 | btrfs_set_path_blocking(p); |
| 2662 | btrfs_tree_read_lock(b); |
| 2663 | btrfs_clear_path_blocking(p, b, |
| 2664 | BTRFS_READ_LOCK); |
| 2665 | } |
| 2666 | p->locks[level] = BTRFS_READ_LOCK; |
| 2667 | p->nodes[level] = b; |
| 2668 | b = tree_mod_log_rewind(root->fs_info, b, time_seq); |
| 2669 | if (b != p->nodes[level]) { |
| 2670 | btrfs_tree_unlock_rw(p->nodes[level], |
| 2671 | p->locks[level]); |
| 2672 | p->locks[level] = 0; |
| 2673 | p->nodes[level] = b; |
| 2674 | } |
| 2675 | } else { |
| 2676 | p->slots[level] = slot; |
| 2677 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2678 | goto done; |
| 2679 | } |
| 2680 | } |
| 2681 | ret = 1; |
| 2682 | done: |
| 2683 | if (!p->leave_spinning) |
| 2684 | btrfs_set_path_blocking(p); |
| 2685 | if (ret < 0) |
| 2686 | btrfs_release_path(p); |
| 2687 | |
| 2688 | return ret; |
| 2689 | } |
| 2690 | |
| 2691 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2692 | * adjust the pointers going up the tree, starting at level |
| 2693 | * making sure the right key of each node is points to 'key'. |
| 2694 | * This is used after shifting pointers to the left, so it stops |
| 2695 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 2696 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2697 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2698 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2699 | static void fixup_low_keys(struct btrfs_trans_handle *trans, |
| 2700 | struct btrfs_root *root, struct btrfs_path *path, |
| 2701 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2702 | { |
| 2703 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2704 | struct extent_buffer *t; |
| 2705 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2706 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2707 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2708 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2709 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2710 | t = path->nodes[i]; |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2711 | 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] | 2712 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2713 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2714 | if (tslot != 0) |
| 2715 | break; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2719 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2720 | * update item key. |
| 2721 | * |
| 2722 | * This function isn't completely safe. It's the caller's responsibility |
| 2723 | * that the new key won't break the order |
| 2724 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2725 | void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans, |
| 2726 | struct btrfs_root *root, struct btrfs_path *path, |
| 2727 | struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2728 | { |
| 2729 | struct btrfs_disk_key disk_key; |
| 2730 | struct extent_buffer *eb; |
| 2731 | int slot; |
| 2732 | |
| 2733 | eb = path->nodes[0]; |
| 2734 | slot = path->slots[0]; |
| 2735 | if (slot > 0) { |
| 2736 | btrfs_item_key(eb, &disk_key, slot - 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2737 | BUG_ON(comp_keys(&disk_key, new_key) >= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2738 | } |
| 2739 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 2740 | btrfs_item_key(eb, &disk_key, slot + 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2741 | BUG_ON(comp_keys(&disk_key, new_key) <= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 2745 | btrfs_set_item_key(eb, &disk_key, slot); |
| 2746 | btrfs_mark_buffer_dirty(eb); |
| 2747 | if (slot == 0) |
| 2748 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2752 | * 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] | 2753 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2754 | * |
| 2755 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 2756 | * 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] | 2757 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2758 | static int push_node_left(struct btrfs_trans_handle *trans, |
| 2759 | struct btrfs_root *root, struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2760 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2761 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2762 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2763 | int src_nritems; |
| 2764 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2765 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2766 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2767 | src_nritems = btrfs_header_nritems(src); |
| 2768 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2769 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2770 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 2771 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2772 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2773 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2774 | return 1; |
| 2775 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2776 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2777 | return 1; |
| 2778 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2779 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2780 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2781 | if (push_items < src_nritems) { |
| 2782 | /* leave at least 8 pointers in the node if |
| 2783 | * we aren't going to empty it |
| 2784 | */ |
| 2785 | if (src_nritems - push_items < 8) { |
| 2786 | if (push_items <= 8) |
| 2787 | return 1; |
| 2788 | push_items -= 8; |
| 2789 | } |
| 2790 | } |
| 2791 | } else |
| 2792 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2793 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2794 | tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0, |
| 2795 | push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2796 | copy_extent_buffer(dst, src, |
| 2797 | btrfs_node_key_ptr_offset(dst_nritems), |
| 2798 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2799 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2800 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2801 | if (push_items < src_nritems) { |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2802 | tree_mod_log_eb_move(root->fs_info, src, 0, push_items, |
| 2803 | src_nritems - push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2804 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 2805 | btrfs_node_key_ptr_offset(push_items), |
| 2806 | (src_nritems - push_items) * |
| 2807 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2808 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2809 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 2810 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 2811 | btrfs_mark_buffer_dirty(src); |
| 2812 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2813 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2814 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2815 | } |
| 2816 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2817 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2818 | * try to push data from one node into the next node right in the |
| 2819 | * tree. |
| 2820 | * |
| 2821 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 2822 | * error, and > 0 if there was no room in the right hand block. |
| 2823 | * |
| 2824 | * this will only push up to 1/2 the contents of the left node over |
| 2825 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2826 | static int balance_node_right(struct btrfs_trans_handle *trans, |
| 2827 | struct btrfs_root *root, |
| 2828 | struct extent_buffer *dst, |
| 2829 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2830 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2831 | int push_items = 0; |
| 2832 | int max_push; |
| 2833 | int src_nritems; |
| 2834 | int dst_nritems; |
| 2835 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2836 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2837 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 2838 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 2839 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2840 | src_nritems = btrfs_header_nritems(src); |
| 2841 | dst_nritems = btrfs_header_nritems(dst); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2842 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2843 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2844 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2845 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2846 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 2847 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2848 | |
| 2849 | max_push = src_nritems / 2 + 1; |
| 2850 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2851 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2852 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 2853 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2854 | if (max_push < push_items) |
| 2855 | push_items = max_push; |
| 2856 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2857 | 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] | 2858 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 2859 | btrfs_node_key_ptr_offset(0), |
| 2860 | (dst_nritems) * |
| 2861 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2862 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2863 | tree_mod_log_eb_copy(root->fs_info, dst, src, 0, |
| 2864 | src_nritems - push_items, push_items); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2865 | copy_extent_buffer(dst, src, |
| 2866 | btrfs_node_key_ptr_offset(0), |
| 2867 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2868 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2869 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2870 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 2871 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2872 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2873 | btrfs_mark_buffer_dirty(src); |
| 2874 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2875 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2876 | return ret; |
| 2877 | } |
| 2878 | |
| 2879 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2880 | * helper function to insert a new root level in the tree. |
| 2881 | * A new node is allocated, and a single item is inserted to |
| 2882 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 2883 | * |
| 2884 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2885 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2886 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2887 | struct btrfs_root *root, |
| 2888 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2889 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2890 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2891 | struct extent_buffer *lower; |
| 2892 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2893 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2894 | struct btrfs_disk_key lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2895 | |
| 2896 | BUG_ON(path->nodes[level]); |
| 2897 | BUG_ON(path->nodes[level-1] != root->node); |
| 2898 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2899 | lower = path->nodes[level-1]; |
| 2900 | if (level == 1) |
| 2901 | btrfs_item_key(lower, &lower_key, 0); |
| 2902 | else |
| 2903 | btrfs_node_key(lower, &lower_key, 0); |
| 2904 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2905 | c = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2906 | root->root_key.objectid, &lower_key, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 2907 | level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2908 | if (IS_ERR(c)) |
| 2909 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2910 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2911 | root_add_used(root, root->nodesize); |
| 2912 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2913 | memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2914 | btrfs_set_header_nritems(c, 1); |
| 2915 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2916 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2917 | btrfs_set_header_generation(c, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2918 | btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2919 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2920 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2921 | write_extent_buffer(c, root->fs_info->fsid, |
| 2922 | (unsigned long)btrfs_header_fsid(c), |
| 2923 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2924 | |
| 2925 | write_extent_buffer(c, root->fs_info->chunk_tree_uuid, |
| 2926 | (unsigned long)btrfs_header_chunk_tree_uuid(c), |
| 2927 | BTRFS_UUID_SIZE); |
| 2928 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2929 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2930 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2931 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2932 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2933 | |
| 2934 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2935 | |
| 2936 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2937 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2938 | old = root->node; |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 2939 | tree_mod_log_set_root_pointer(root, c); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 2940 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2941 | |
| 2942 | /* the super has an extra ref to root->node */ |
| 2943 | free_extent_buffer(old); |
| 2944 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2945 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2946 | extent_buffer_get(c); |
| 2947 | path->nodes[level] = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2948 | path->locks[level] = BTRFS_WRITE_LOCK; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2949 | path->slots[level] = 0; |
| 2950 | return 0; |
| 2951 | } |
| 2952 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2953 | /* |
| 2954 | * worker function to insert a single pointer in a node. |
| 2955 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2956 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2957 | * slot and level indicate where you want the key to go, and |
| 2958 | * blocknr is the block the key points to. |
| 2959 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2960 | static void insert_ptr(struct btrfs_trans_handle *trans, |
| 2961 | struct btrfs_root *root, struct btrfs_path *path, |
| 2962 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 2963 | int slot, int level, int tree_mod_log) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2964 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2965 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2966 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 2967 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2968 | |
| 2969 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2970 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2971 | lower = path->nodes[level]; |
| 2972 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 2973 | BUG_ON(slot > nritems); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2974 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2975 | if (slot != nritems) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 2976 | if (tree_mod_log && level) |
| 2977 | tree_mod_log_eb_move(root->fs_info, lower, slot + 1, |
| 2978 | slot, nritems - slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2979 | memmove_extent_buffer(lower, |
| 2980 | btrfs_node_key_ptr_offset(slot + 1), |
| 2981 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 2982 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2983 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 2984 | if (tree_mod_log && level) { |
| 2985 | ret = tree_mod_log_insert_key(root->fs_info, lower, slot, |
| 2986 | MOD_LOG_KEY_ADD); |
| 2987 | BUG_ON(ret < 0); |
| 2988 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2989 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2990 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2991 | WARN_ON(trans->transid == 0); |
| 2992 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2993 | btrfs_set_header_nritems(lower, nritems + 1); |
| 2994 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2995 | } |
| 2996 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2997 | /* |
| 2998 | * split the node at the specified level in path in two. |
| 2999 | * The path is corrected to point to the appropriate node after the split |
| 3000 | * |
| 3001 | * Before splitting this tries to make some room in the node by pushing |
| 3002 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3003 | * |
| 3004 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3005 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3006 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3007 | struct btrfs_root *root, |
| 3008 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3009 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3010 | struct extent_buffer *c; |
| 3011 | struct extent_buffer *split; |
| 3012 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3013 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3014 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3015 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3016 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3017 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3018 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3019 | if (c == root->node) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3020 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3021 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3022 | if (ret) |
| 3023 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3024 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3025 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3026 | c = path->nodes[level]; |
| 3027 | if (!ret && btrfs_header_nritems(c) < |
Chris Mason | c448acf | 2008-04-24 09:34:34 -0400 | [diff] [blame] | 3028 | BTRFS_NODEPTRS_PER_BLOCK(root) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3029 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3030 | if (ret < 0) |
| 3031 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3032 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3033 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3034 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3035 | mid = (c_nritems + 1) / 2; |
| 3036 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3037 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3038 | split = btrfs_alloc_free_block(trans, root, root->nodesize, 0, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3039 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3040 | &disk_key, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3041 | if (IS_ERR(split)) |
| 3042 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3043 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3044 | root_add_used(root, root->nodesize); |
| 3045 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3046 | memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3047 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3048 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3049 | btrfs_set_header_generation(split, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3050 | btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3051 | btrfs_set_header_owner(split, root->root_key.objectid); |
| 3052 | write_extent_buffer(split, root->fs_info->fsid, |
| 3053 | (unsigned long)btrfs_header_fsid(split), |
| 3054 | BTRFS_FSID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3055 | write_extent_buffer(split, root->fs_info->chunk_tree_uuid, |
| 3056 | (unsigned long)btrfs_header_chunk_tree_uuid(split), |
| 3057 | BTRFS_UUID_SIZE); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3058 | |
Jan Schmidt | f230475 | 2012-05-26 11:43:17 +0200 | [diff] [blame] | 3059 | 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] | 3060 | copy_extent_buffer(split, c, |
| 3061 | btrfs_node_key_ptr_offset(0), |
| 3062 | btrfs_node_key_ptr_offset(mid), |
| 3063 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3064 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3065 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3066 | ret = 0; |
| 3067 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3068 | btrfs_mark_buffer_dirty(c); |
| 3069 | btrfs_mark_buffer_dirty(split); |
| 3070 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3071 | insert_ptr(trans, root, path, &disk_key, split->start, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3072 | path->slots[level + 1] + 1, level + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3073 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3074 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3075 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3076 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3077 | free_extent_buffer(c); |
| 3078 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3079 | path->slots[level + 1] += 1; |
| 3080 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3081 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3082 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3083 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3084 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3085 | } |
| 3086 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3087 | /* |
| 3088 | * how many bytes are required to store the items in a leaf. start |
| 3089 | * and nr indicate which items in the leaf to check. This totals up the |
| 3090 | * space used both by the item structs and the item data |
| 3091 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3092 | 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] | 3093 | { |
| 3094 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3095 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3096 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3097 | |
| 3098 | if (!nr) |
| 3099 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3100 | data_len = btrfs_item_end_nr(l, start); |
| 3101 | data_len = data_len - btrfs_item_offset_nr(l, end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3102 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3103 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3104 | return data_len; |
| 3105 | } |
| 3106 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3107 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3108 | * The space between the end of the leaf items and |
| 3109 | * the start of the leaf data. IOW, how much room |
| 3110 | * the leaf has left for both items and data |
| 3111 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3112 | noinline int btrfs_leaf_free_space(struct btrfs_root *root, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3113 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3114 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3115 | int nritems = btrfs_header_nritems(leaf); |
| 3116 | int ret; |
| 3117 | ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 3118 | if (ret < 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3119 | printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, " |
| 3120 | "used %d nritems %d\n", |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 3121 | ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3122 | leaf_space_used(leaf, 0, nritems), nritems); |
| 3123 | } |
| 3124 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3125 | } |
| 3126 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3127 | /* |
| 3128 | * min slot controls the lowest index we're willing to push to the |
| 3129 | * right. We'll push up to and including min_slot, but no lower |
| 3130 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3131 | static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, |
| 3132 | struct btrfs_root *root, |
| 3133 | struct btrfs_path *path, |
| 3134 | int data_size, int empty, |
| 3135 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3136 | int free_space, u32 left_nritems, |
| 3137 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3138 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3139 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3140 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3141 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3142 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3143 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3144 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3145 | int push_space = 0; |
| 3146 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3147 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3148 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3149 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3150 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3151 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3152 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3153 | btrfs_init_map_token(&token); |
| 3154 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3155 | if (empty) |
| 3156 | nr = 0; |
| 3157 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3158 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3159 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3160 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3161 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3162 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3163 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3164 | i = left_nritems - 1; |
| 3165 | while (i >= nr) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3166 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3167 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3168 | if (!empty && push_items > 0) { |
| 3169 | if (path->slots[0] > i) |
| 3170 | break; |
| 3171 | if (path->slots[0] == i) { |
| 3172 | int space = btrfs_leaf_free_space(root, left); |
| 3173 | if (space + push_space * 2 > free_space) |
| 3174 | break; |
| 3175 | } |
| 3176 | } |
| 3177 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3178 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3179 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3180 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3181 | this_item_size = btrfs_item_size(left, item); |
| 3182 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3183 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3184 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3185 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3186 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3187 | if (i == 0) |
| 3188 | break; |
| 3189 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3190 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3191 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3192 | if (push_items == 0) |
| 3193 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3194 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3195 | if (!empty && push_items == left_nritems) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3196 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3197 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3198 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3199 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3200 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3201 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3202 | push_space -= leaf_data_end(root, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3203 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3204 | /* make room in the right data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3205 | data_end = leaf_data_end(root, right); |
| 3206 | memmove_extent_buffer(right, |
| 3207 | btrfs_leaf_data(right) + data_end - push_space, |
| 3208 | btrfs_leaf_data(right) + data_end, |
| 3209 | BTRFS_LEAF_DATA_SIZE(root) - data_end); |
| 3210 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3211 | /* copy from the left data area */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3212 | copy_extent_buffer(right, left, btrfs_leaf_data(right) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3213 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3214 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 3215 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3216 | |
| 3217 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3218 | btrfs_item_nr_offset(0), |
| 3219 | right_nritems * sizeof(struct btrfs_item)); |
| 3220 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3221 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3222 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3223 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3224 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3225 | |
| 3226 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3227 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3228 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3229 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3230 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3231 | item = btrfs_item_nr(right, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3232 | push_space -= btrfs_token_item_size(right, item, &token); |
| 3233 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3234 | } |
| 3235 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3236 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3237 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3238 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3239 | if (left_nritems) |
| 3240 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3241 | else |
| 3242 | clean_tree_block(trans, root, left); |
| 3243 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3244 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3245 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3246 | btrfs_item_key(right, &disk_key, 0); |
| 3247 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3248 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3249 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3250 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3251 | if (path->slots[0] >= left_nritems) { |
| 3252 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3253 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
| 3254 | clean_tree_block(trans, root, path->nodes[0]); |
| 3255 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3256 | free_extent_buffer(path->nodes[0]); |
| 3257 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3258 | path->slots[1] += 1; |
| 3259 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3260 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3261 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3262 | } |
| 3263 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3264 | |
| 3265 | out_unlock: |
| 3266 | btrfs_tree_unlock(right); |
| 3267 | free_extent_buffer(right); |
| 3268 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3269 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3270 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3271 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3272 | * push some data in the path leaf to the right, trying to free up at |
| 3273 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3274 | * |
| 3275 | * returns 1 if the push failed because the other node didn't have enough |
| 3276 | * 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] | 3277 | * |
| 3278 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3279 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3280 | */ |
| 3281 | 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] | 3282 | *root, struct btrfs_path *path, |
| 3283 | int min_data_size, int data_size, |
| 3284 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3285 | { |
| 3286 | struct extent_buffer *left = path->nodes[0]; |
| 3287 | struct extent_buffer *right; |
| 3288 | struct extent_buffer *upper; |
| 3289 | int slot; |
| 3290 | int free_space; |
| 3291 | u32 left_nritems; |
| 3292 | int ret; |
| 3293 | |
| 3294 | if (!path->nodes[1]) |
| 3295 | return 1; |
| 3296 | |
| 3297 | slot = path->slots[1]; |
| 3298 | upper = path->nodes[1]; |
| 3299 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3300 | return 1; |
| 3301 | |
| 3302 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3303 | |
| 3304 | right = read_node_slot(root, upper, slot + 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3305 | if (right == NULL) |
| 3306 | return 1; |
| 3307 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3308 | btrfs_tree_lock(right); |
| 3309 | btrfs_set_lock_blocking(right); |
| 3310 | |
| 3311 | free_space = btrfs_leaf_free_space(root, right); |
| 3312 | if (free_space < data_size) |
| 3313 | goto out_unlock; |
| 3314 | |
| 3315 | /* cow and double check */ |
| 3316 | ret = btrfs_cow_block(trans, root, right, upper, |
| 3317 | slot + 1, &right); |
| 3318 | if (ret) |
| 3319 | goto out_unlock; |
| 3320 | |
| 3321 | free_space = btrfs_leaf_free_space(root, right); |
| 3322 | if (free_space < data_size) |
| 3323 | goto out_unlock; |
| 3324 | |
| 3325 | left_nritems = btrfs_header_nritems(left); |
| 3326 | if (left_nritems == 0) |
| 3327 | goto out_unlock; |
| 3328 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3329 | return __push_leaf_right(trans, root, path, min_data_size, empty, |
| 3330 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3331 | out_unlock: |
| 3332 | btrfs_tree_unlock(right); |
| 3333 | free_extent_buffer(right); |
| 3334 | return 1; |
| 3335 | } |
| 3336 | |
| 3337 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3338 | * push some data in the path leaf to the left, trying to free up at |
| 3339 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3340 | * |
| 3341 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3342 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3343 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3344 | */ |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3345 | static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, |
| 3346 | struct btrfs_root *root, |
| 3347 | struct btrfs_path *path, int data_size, |
| 3348 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3349 | int free_space, u32 right_nritems, |
| 3350 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3351 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3352 | struct btrfs_disk_key disk_key; |
| 3353 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3354 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3355 | int push_space = 0; |
| 3356 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3357 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3358 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3359 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3360 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3361 | u32 this_item_size; |
| 3362 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3363 | struct btrfs_map_token token; |
| 3364 | |
| 3365 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3366 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3367 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3368 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3369 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3370 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3371 | |
| 3372 | for (i = 0; i < nr; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3373 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3374 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3375 | if (!empty && push_items > 0) { |
| 3376 | if (path->slots[0] < i) |
| 3377 | break; |
| 3378 | if (path->slots[0] == i) { |
| 3379 | int space = btrfs_leaf_free_space(root, right); |
| 3380 | if (space + push_space * 2 > free_space) |
| 3381 | break; |
| 3382 | } |
| 3383 | } |
| 3384 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3385 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3386 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3387 | |
| 3388 | this_item_size = btrfs_item_size(right, item); |
| 3389 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3390 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3391 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3392 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3393 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3394 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3395 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3396 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3397 | ret = 1; |
| 3398 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3399 | } |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3400 | if (!empty && push_items == btrfs_header_nritems(right)) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3401 | WARN_ON(1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3402 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3403 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3404 | copy_extent_buffer(left, right, |
| 3405 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3406 | btrfs_item_nr_offset(0), |
| 3407 | push_items * sizeof(struct btrfs_item)); |
| 3408 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3409 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3410 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3411 | |
| 3412 | copy_extent_buffer(left, right, btrfs_leaf_data(left) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3413 | leaf_data_end(root, left) - push_space, |
| 3414 | btrfs_leaf_data(right) + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3415 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3416 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3417 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3418 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3419 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3420 | 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] | 3421 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3422 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3423 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3424 | item = btrfs_item_nr(left, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3425 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3426 | ioff = btrfs_token_item_offset(left, item, &token); |
| 3427 | btrfs_set_token_item_offset(left, item, |
| 3428 | ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size), |
| 3429 | &token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3430 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3431 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3432 | |
| 3433 | /* fixup right node */ |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3434 | if (push_items > right_nritems) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3435 | printk(KERN_CRIT "push items %d nr %u\n", push_items, |
| 3436 | right_nritems); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3437 | WARN_ON(1); |
| 3438 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3439 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3440 | if (push_items < right_nritems) { |
| 3441 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
| 3442 | leaf_data_end(root, right); |
| 3443 | memmove_extent_buffer(right, btrfs_leaf_data(right) + |
| 3444 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 3445 | btrfs_leaf_data(right) + |
| 3446 | leaf_data_end(root, right), push_space); |
| 3447 | |
| 3448 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3449 | btrfs_item_nr_offset(push_items), |
| 3450 | (btrfs_header_nritems(right) - push_items) * |
| 3451 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3452 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3453 | right_nritems -= push_items; |
| 3454 | btrfs_set_header_nritems(right, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3455 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3456 | for (i = 0; i < right_nritems; i++) { |
| 3457 | item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3458 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3459 | push_space = push_space - btrfs_token_item_size(right, |
| 3460 | item, &token); |
| 3461 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3462 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3463 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3464 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3465 | if (right_nritems) |
| 3466 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3467 | else |
| 3468 | clean_tree_block(trans, root, right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3469 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3470 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3471 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3472 | |
| 3473 | /* then fixup the leaf pointer in the path */ |
| 3474 | if (path->slots[0] < push_items) { |
| 3475 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3476 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3477 | free_extent_buffer(path->nodes[0]); |
| 3478 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3479 | path->slots[1] -= 1; |
| 3480 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3481 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3482 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3483 | path->slots[0] -= push_items; |
| 3484 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3485 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3486 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3487 | out: |
| 3488 | btrfs_tree_unlock(left); |
| 3489 | free_extent_buffer(left); |
| 3490 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3491 | } |
| 3492 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3493 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3494 | * push some data in the path leaf to the left, trying to free up at |
| 3495 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3496 | * |
| 3497 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3498 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3499 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3500 | */ |
| 3501 | 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] | 3502 | *root, struct btrfs_path *path, int min_data_size, |
| 3503 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3504 | { |
| 3505 | struct extent_buffer *right = path->nodes[0]; |
| 3506 | struct extent_buffer *left; |
| 3507 | int slot; |
| 3508 | int free_space; |
| 3509 | u32 right_nritems; |
| 3510 | int ret = 0; |
| 3511 | |
| 3512 | slot = path->slots[1]; |
| 3513 | if (slot == 0) |
| 3514 | return 1; |
| 3515 | if (!path->nodes[1]) |
| 3516 | return 1; |
| 3517 | |
| 3518 | right_nritems = btrfs_header_nritems(right); |
| 3519 | if (right_nritems == 0) |
| 3520 | return 1; |
| 3521 | |
| 3522 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3523 | |
| 3524 | left = read_node_slot(root, path->nodes[1], slot - 1); |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3525 | if (left == NULL) |
| 3526 | return 1; |
| 3527 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3528 | btrfs_tree_lock(left); |
| 3529 | btrfs_set_lock_blocking(left); |
| 3530 | |
| 3531 | free_space = btrfs_leaf_free_space(root, left); |
| 3532 | if (free_space < data_size) { |
| 3533 | ret = 1; |
| 3534 | goto out; |
| 3535 | } |
| 3536 | |
| 3537 | /* cow and double check */ |
| 3538 | ret = btrfs_cow_block(trans, root, left, |
| 3539 | path->nodes[1], slot - 1, &left); |
| 3540 | if (ret) { |
| 3541 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3542 | if (ret == -ENOSPC) |
| 3543 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3544 | goto out; |
| 3545 | } |
| 3546 | |
| 3547 | free_space = btrfs_leaf_free_space(root, left); |
| 3548 | if (free_space < data_size) { |
| 3549 | ret = 1; |
| 3550 | goto out; |
| 3551 | } |
| 3552 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3553 | return __push_leaf_left(trans, root, path, min_data_size, |
| 3554 | empty, left, free_space, right_nritems, |
| 3555 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3556 | out: |
| 3557 | btrfs_tree_unlock(left); |
| 3558 | free_extent_buffer(left); |
| 3559 | return ret; |
| 3560 | } |
| 3561 | |
| 3562 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3563 | * split the path's leaf in two, making sure there is at least data_size |
| 3564 | * available for the resulting leaf level of the path. |
| 3565 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3566 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
| 3567 | struct btrfs_root *root, |
| 3568 | struct btrfs_path *path, |
| 3569 | struct extent_buffer *l, |
| 3570 | struct extent_buffer *right, |
| 3571 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3572 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3573 | int data_copy_size; |
| 3574 | int rt_data_off; |
| 3575 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3576 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3577 | struct btrfs_map_token token; |
| 3578 | |
| 3579 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3580 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3581 | nritems = nritems - mid; |
| 3582 | btrfs_set_header_nritems(right, nritems); |
| 3583 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); |
| 3584 | |
| 3585 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 3586 | btrfs_item_nr_offset(mid), |
| 3587 | nritems * sizeof(struct btrfs_item)); |
| 3588 | |
| 3589 | copy_extent_buffer(right, l, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3590 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 3591 | data_copy_size, btrfs_leaf_data(l) + |
| 3592 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3593 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3594 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 3595 | btrfs_item_end_nr(l, mid); |
| 3596 | |
| 3597 | for (i = 0; i < nritems; i++) { |
| 3598 | struct btrfs_item *item = btrfs_item_nr(right, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3599 | u32 ioff; |
| 3600 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3601 | ioff = btrfs_token_item_offset(right, item, &token); |
| 3602 | btrfs_set_token_item_offset(right, item, |
| 3603 | ioff + rt_data_off, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3604 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3605 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3606 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3607 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3608 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3609 | path->slots[1] + 1, 1, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3610 | |
| 3611 | btrfs_mark_buffer_dirty(right); |
| 3612 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3613 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3614 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3615 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3616 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3617 | free_extent_buffer(path->nodes[0]); |
| 3618 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3619 | path->slots[0] -= mid; |
| 3620 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3621 | } else { |
| 3622 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3623 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3624 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3625 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3626 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3630 | * double splits happen when we need to insert a big item in the middle |
| 3631 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 3632 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 3633 | * A B C |
| 3634 | * |
| 3635 | * We avoid this by trying to push the items on either side of our target |
| 3636 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 3637 | * completely. |
| 3638 | */ |
| 3639 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 3640 | struct btrfs_root *root, |
| 3641 | struct btrfs_path *path, |
| 3642 | int data_size) |
| 3643 | { |
| 3644 | int ret; |
| 3645 | int progress = 0; |
| 3646 | int slot; |
| 3647 | u32 nritems; |
| 3648 | |
| 3649 | slot = path->slots[0]; |
| 3650 | |
| 3651 | /* |
| 3652 | * try to push all the items after our slot into the |
| 3653 | * right leaf |
| 3654 | */ |
| 3655 | ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot); |
| 3656 | if (ret < 0) |
| 3657 | return ret; |
| 3658 | |
| 3659 | if (ret == 0) |
| 3660 | progress++; |
| 3661 | |
| 3662 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 3663 | /* |
| 3664 | * our goal is to get our slot at the start or end of a leaf. If |
| 3665 | * we've done so we're done |
| 3666 | */ |
| 3667 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 3668 | return 0; |
| 3669 | |
| 3670 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 3671 | return 0; |
| 3672 | |
| 3673 | /* try to push all the items before our slot into the next leaf */ |
| 3674 | slot = path->slots[0]; |
| 3675 | ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot); |
| 3676 | if (ret < 0) |
| 3677 | return ret; |
| 3678 | |
| 3679 | if (ret == 0) |
| 3680 | progress++; |
| 3681 | |
| 3682 | if (progress) |
| 3683 | return 0; |
| 3684 | return 1; |
| 3685 | } |
| 3686 | |
| 3687 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3688 | * split the path's leaf in two, making sure there is at least data_size |
| 3689 | * available for the resulting leaf level of the path. |
| 3690 | * |
| 3691 | * returns 0 if all went well and < 0 on failure. |
| 3692 | */ |
| 3693 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 3694 | struct btrfs_root *root, |
| 3695 | struct btrfs_key *ins_key, |
| 3696 | struct btrfs_path *path, int data_size, |
| 3697 | int extend) |
| 3698 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3699 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3700 | struct extent_buffer *l; |
| 3701 | u32 nritems; |
| 3702 | int mid; |
| 3703 | int slot; |
| 3704 | struct extent_buffer *right; |
| 3705 | int ret = 0; |
| 3706 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3707 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3708 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3709 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3710 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 3711 | l = path->nodes[0]; |
| 3712 | slot = path->slots[0]; |
| 3713 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
| 3714 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root)) |
| 3715 | return -EOVERFLOW; |
| 3716 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3717 | /* first try to make some room by pushing left and right */ |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3718 | if (data_size) { |
| 3719 | wret = push_leaf_right(trans, root, path, data_size, |
| 3720 | data_size, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3721 | if (wret < 0) |
| 3722 | return wret; |
| 3723 | if (wret) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3724 | wret = push_leaf_left(trans, root, path, data_size, |
| 3725 | data_size, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3726 | if (wret < 0) |
| 3727 | return wret; |
| 3728 | } |
| 3729 | l = path->nodes[0]; |
| 3730 | |
| 3731 | /* did the pushes work? */ |
| 3732 | if (btrfs_leaf_free_space(root, l) >= data_size) |
| 3733 | return 0; |
| 3734 | } |
| 3735 | |
| 3736 | if (!path->nodes[1]) { |
| 3737 | ret = insert_new_root(trans, root, path, 1); |
| 3738 | if (ret) |
| 3739 | return ret; |
| 3740 | } |
| 3741 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3742 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3743 | l = path->nodes[0]; |
| 3744 | slot = path->slots[0]; |
| 3745 | nritems = btrfs_header_nritems(l); |
| 3746 | mid = (nritems + 1) / 2; |
| 3747 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3748 | if (mid <= slot) { |
| 3749 | if (nritems == 1 || |
| 3750 | leaf_space_used(l, mid, nritems - mid) + data_size > |
| 3751 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3752 | if (slot >= nritems) { |
| 3753 | split = 0; |
| 3754 | } else { |
| 3755 | mid = slot; |
| 3756 | if (mid != nritems && |
| 3757 | leaf_space_used(l, mid, nritems - mid) + |
| 3758 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3759 | if (data_size && !tried_avoid_double) |
| 3760 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3761 | split = 2; |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | } else { |
| 3766 | if (leaf_space_used(l, 0, mid) + data_size > |
| 3767 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 3768 | if (!extend && data_size && slot == 0) { |
| 3769 | split = 0; |
| 3770 | } else if ((extend || !data_size) && slot == 0) { |
| 3771 | mid = 1; |
| 3772 | } else { |
| 3773 | mid = slot; |
| 3774 | if (mid != nritems && |
| 3775 | leaf_space_used(l, mid, nritems - mid) + |
| 3776 | data_size > BTRFS_LEAF_DATA_SIZE(root)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3777 | if (data_size && !tried_avoid_double) |
| 3778 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3779 | split = 2 ; |
| 3780 | } |
| 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | if (split == 0) |
| 3786 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 3787 | else |
| 3788 | btrfs_item_key(l, &disk_key, mid); |
| 3789 | |
| 3790 | right = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3791 | root->root_key.objectid, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3792 | &disk_key, 0, l->start, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3793 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3794 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3795 | |
| 3796 | root_add_used(root, root->leafsize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3797 | |
| 3798 | memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); |
| 3799 | btrfs_set_header_bytenr(right, right->start); |
| 3800 | btrfs_set_header_generation(right, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3801 | btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3802 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 3803 | btrfs_set_header_level(right, 0); |
| 3804 | write_extent_buffer(right, root->fs_info->fsid, |
| 3805 | (unsigned long)btrfs_header_fsid(right), |
| 3806 | BTRFS_FSID_SIZE); |
| 3807 | |
| 3808 | write_extent_buffer(right, root->fs_info->chunk_tree_uuid, |
| 3809 | (unsigned long)btrfs_header_chunk_tree_uuid(right), |
| 3810 | BTRFS_UUID_SIZE); |
| 3811 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3812 | if (split == 0) { |
| 3813 | if (mid <= slot) { |
| 3814 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3815 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3816 | path->slots[1] + 1, 1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3817 | btrfs_tree_unlock(path->nodes[0]); |
| 3818 | free_extent_buffer(path->nodes[0]); |
| 3819 | path->nodes[0] = right; |
| 3820 | path->slots[0] = 0; |
| 3821 | path->slots[1] += 1; |
| 3822 | } else { |
| 3823 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3824 | insert_ptr(trans, root, path, &disk_key, right->start, |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3825 | path->slots[1], 1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3826 | btrfs_tree_unlock(path->nodes[0]); |
| 3827 | free_extent_buffer(path->nodes[0]); |
| 3828 | path->nodes[0] = right; |
| 3829 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3830 | if (path->slots[1] == 0) |
| 3831 | fixup_low_keys(trans, root, path, |
| 3832 | &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3833 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3834 | btrfs_mark_buffer_dirty(right); |
| 3835 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3836 | } |
| 3837 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3838 | copy_for_split(trans, root, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3839 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3840 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 3841 | BUG_ON(num_doubles != 0); |
| 3842 | num_doubles++; |
| 3843 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 3844 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3845 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3846 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3847 | |
| 3848 | push_for_double: |
| 3849 | push_for_double_split(trans, root, path, data_size); |
| 3850 | tried_avoid_double = 1; |
| 3851 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) |
| 3852 | return 0; |
| 3853 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3854 | } |
| 3855 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3856 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 3857 | struct btrfs_root *root, |
| 3858 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3859 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3860 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3861 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3862 | struct btrfs_file_extent_item *fi; |
| 3863 | u64 extent_len = 0; |
| 3864 | u32 item_size; |
| 3865 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3866 | |
| 3867 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3868 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3869 | |
| 3870 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 3871 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 3872 | |
| 3873 | if (btrfs_leaf_free_space(root, leaf) >= ins_len) |
| 3874 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3875 | |
| 3876 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3877 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 3878 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3879 | struct btrfs_file_extent_item); |
| 3880 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 3881 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3882 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3883 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3884 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3885 | path->search_for_split = 1; |
| 3886 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3887 | path->search_for_split = 0; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3888 | if (ret < 0) |
| 3889 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3890 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3891 | ret = -EAGAIN; |
| 3892 | leaf = path->nodes[0]; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3893 | /* if our item isn't there or got smaller, return now */ |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3894 | if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
| 3895 | goto err; |
| 3896 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 3897 | /* the leaf has changed, it now has room. return now */ |
| 3898 | if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) |
| 3899 | goto err; |
| 3900 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3901 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 3902 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3903 | struct btrfs_file_extent_item); |
| 3904 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 3905 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3906 | } |
| 3907 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3908 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3909 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3910 | if (ret) |
| 3911 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3912 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3913 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3914 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3915 | return 0; |
| 3916 | err: |
| 3917 | path->keep_locks = 0; |
| 3918 | return ret; |
| 3919 | } |
| 3920 | |
| 3921 | static noinline int split_item(struct btrfs_trans_handle *trans, |
| 3922 | struct btrfs_root *root, |
| 3923 | struct btrfs_path *path, |
| 3924 | struct btrfs_key *new_key, |
| 3925 | unsigned long split_offset) |
| 3926 | { |
| 3927 | struct extent_buffer *leaf; |
| 3928 | struct btrfs_item *item; |
| 3929 | struct btrfs_item *new_item; |
| 3930 | int slot; |
| 3931 | char *buf; |
| 3932 | u32 nritems; |
| 3933 | u32 item_size; |
| 3934 | u32 orig_offset; |
| 3935 | struct btrfs_disk_key disk_key; |
| 3936 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3937 | leaf = path->nodes[0]; |
| 3938 | BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); |
| 3939 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3940 | btrfs_set_path_blocking(path); |
| 3941 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3942 | item = btrfs_item_nr(leaf, path->slots[0]); |
| 3943 | orig_offset = btrfs_item_offset(leaf, item); |
| 3944 | item_size = btrfs_item_size(leaf, item); |
| 3945 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3946 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3947 | if (!buf) |
| 3948 | return -ENOMEM; |
| 3949 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3950 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 3951 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3952 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3953 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3954 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3955 | if (slot != nritems) { |
| 3956 | /* shift the items */ |
| 3957 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3958 | btrfs_item_nr_offset(slot), |
| 3959 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 3963 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 3964 | |
| 3965 | new_item = btrfs_item_nr(leaf, slot); |
| 3966 | |
| 3967 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 3968 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 3969 | |
| 3970 | btrfs_set_item_offset(leaf, item, |
| 3971 | orig_offset + item_size - split_offset); |
| 3972 | btrfs_set_item_size(leaf, item, split_offset); |
| 3973 | |
| 3974 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 3975 | |
| 3976 | /* write the data for the start of the original item */ |
| 3977 | write_extent_buffer(leaf, buf, |
| 3978 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 3979 | split_offset); |
| 3980 | |
| 3981 | /* write the data for the new item */ |
| 3982 | write_extent_buffer(leaf, buf + split_offset, |
| 3983 | btrfs_item_ptr_offset(leaf, slot), |
| 3984 | item_size - split_offset); |
| 3985 | btrfs_mark_buffer_dirty(leaf); |
| 3986 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3987 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3988 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 3989 | return 0; |
| 3990 | } |
| 3991 | |
| 3992 | /* |
| 3993 | * This function splits a single item into two items, |
| 3994 | * giving 'new_key' to the new item and splitting the |
| 3995 | * old one at split_offset (from the start of the item). |
| 3996 | * |
| 3997 | * The path may be released by this operation. After |
| 3998 | * the split, the path is pointing to the old item. The |
| 3999 | * new item is going to be in the same node as the old one. |
| 4000 | * |
| 4001 | * Note, the item being split must be smaller enough to live alone on |
| 4002 | * a tree block with room for one extra struct btrfs_item |
| 4003 | * |
| 4004 | * This allows us to split the item in place, keeping a lock on the |
| 4005 | * leaf the entire time. |
| 4006 | */ |
| 4007 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4008 | struct btrfs_root *root, |
| 4009 | struct btrfs_path *path, |
| 4010 | struct btrfs_key *new_key, |
| 4011 | unsigned long split_offset) |
| 4012 | { |
| 4013 | int ret; |
| 4014 | ret = setup_leaf_for_split(trans, root, path, |
| 4015 | sizeof(struct btrfs_item)); |
| 4016 | if (ret) |
| 4017 | return ret; |
| 4018 | |
| 4019 | ret = split_item(trans, root, path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4020 | return ret; |
| 4021 | } |
| 4022 | |
| 4023 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4024 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4025 | * It guarantees both items live in the same tree leaf and the new item |
| 4026 | * is contiguous with the original item. |
| 4027 | * |
| 4028 | * This allows us to split file extent in place, keeping a lock on the |
| 4029 | * leaf the entire time. |
| 4030 | */ |
| 4031 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4032 | struct btrfs_root *root, |
| 4033 | struct btrfs_path *path, |
| 4034 | struct btrfs_key *new_key) |
| 4035 | { |
| 4036 | struct extent_buffer *leaf; |
| 4037 | int ret; |
| 4038 | u32 item_size; |
| 4039 | |
| 4040 | leaf = path->nodes[0]; |
| 4041 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4042 | ret = setup_leaf_for_split(trans, root, path, |
| 4043 | item_size + sizeof(struct btrfs_item)); |
| 4044 | if (ret) |
| 4045 | return ret; |
| 4046 | |
| 4047 | path->slots[0]++; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4048 | setup_items_for_insert(trans, root, path, new_key, &item_size, |
| 4049 | item_size, item_size + |
| 4050 | sizeof(struct btrfs_item), 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4051 | leaf = path->nodes[0]; |
| 4052 | memcpy_extent_buffer(leaf, |
| 4053 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4054 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4055 | item_size); |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
| 4059 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4060 | * make the item pointed to by the path smaller. new_size indicates |
| 4061 | * how small to make it, and from_end tells us if we just chop bytes |
| 4062 | * off the end of the item or if we shift the item to chop bytes off |
| 4063 | * the front. |
| 4064 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4065 | void btrfs_truncate_item(struct btrfs_trans_handle *trans, |
| 4066 | struct btrfs_root *root, |
| 4067 | struct btrfs_path *path, |
| 4068 | u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4069 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4070 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4071 | struct extent_buffer *leaf; |
| 4072 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4073 | u32 nritems; |
| 4074 | unsigned int data_end; |
| 4075 | unsigned int old_data_start; |
| 4076 | unsigned int old_size; |
| 4077 | unsigned int size_diff; |
| 4078 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4079 | struct btrfs_map_token token; |
| 4080 | |
| 4081 | btrfs_init_map_token(&token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4082 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4083 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4084 | slot = path->slots[0]; |
| 4085 | |
| 4086 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4087 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4088 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4089 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4090 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4091 | data_end = leaf_data_end(root, leaf); |
| 4092 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4093 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4094 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4095 | size_diff = old_size - new_size; |
| 4096 | |
| 4097 | BUG_ON(slot < 0); |
| 4098 | BUG_ON(slot >= nritems); |
| 4099 | |
| 4100 | /* |
| 4101 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4102 | */ |
| 4103 | /* first correct the data pointers */ |
| 4104 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4105 | u32 ioff; |
| 4106 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4107 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4108 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4109 | btrfs_set_token_item_offset(leaf, item, |
| 4110 | ioff + size_diff, &token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4111 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4112 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4113 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4114 | if (from_end) { |
| 4115 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4116 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4117 | data_end, old_data_start + new_size - data_end); |
| 4118 | } else { |
| 4119 | struct btrfs_disk_key disk_key; |
| 4120 | u64 offset; |
| 4121 | |
| 4122 | btrfs_item_key(leaf, &disk_key, slot); |
| 4123 | |
| 4124 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4125 | unsigned long ptr; |
| 4126 | struct btrfs_file_extent_item *fi; |
| 4127 | |
| 4128 | fi = btrfs_item_ptr(leaf, slot, |
| 4129 | struct btrfs_file_extent_item); |
| 4130 | fi = (struct btrfs_file_extent_item *)( |
| 4131 | (unsigned long)fi - size_diff); |
| 4132 | |
| 4133 | if (btrfs_file_extent_type(leaf, fi) == |
| 4134 | BTRFS_FILE_EXTENT_INLINE) { |
| 4135 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4136 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4137 | (unsigned long)fi, |
| 4138 | offsetof(struct btrfs_file_extent_item, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4139 | disk_bytenr)); |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4144 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 4145 | data_end, old_data_start - data_end); |
| 4146 | |
| 4147 | offset = btrfs_disk_key_offset(&disk_key); |
| 4148 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4149 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4150 | if (slot == 0) |
| 4151 | fixup_low_keys(trans, root, path, &disk_key, 1); |
| 4152 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4153 | |
| 4154 | item = btrfs_item_nr(leaf, slot); |
| 4155 | btrfs_set_item_size(leaf, item, new_size); |
| 4156 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4157 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4158 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4159 | btrfs_print_leaf(root, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4160 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4161 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4162 | } |
| 4163 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4164 | /* |
| 4165 | * make the item pointed to by the path bigger, data_size is the new size. |
| 4166 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4167 | void btrfs_extend_item(struct btrfs_trans_handle *trans, |
| 4168 | struct btrfs_root *root, struct btrfs_path *path, |
| 4169 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4170 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4171 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4172 | struct extent_buffer *leaf; |
| 4173 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4174 | u32 nritems; |
| 4175 | unsigned int data_end; |
| 4176 | unsigned int old_data; |
| 4177 | unsigned int old_size; |
| 4178 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4179 | struct btrfs_map_token token; |
| 4180 | |
| 4181 | btrfs_init_map_token(&token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4182 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4183 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4184 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4185 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4186 | data_end = leaf_data_end(root, leaf); |
| 4187 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4188 | if (btrfs_leaf_free_space(root, leaf) < data_size) { |
| 4189 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4190 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4191 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4192 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4193 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4194 | |
| 4195 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4196 | if (slot >= nritems) { |
| 4197 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4198 | printk(KERN_CRIT "slot %d too large, nritems %d\n", |
| 4199 | slot, nritems); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4200 | BUG_ON(1); |
| 4201 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4202 | |
| 4203 | /* |
| 4204 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4205 | */ |
| 4206 | /* first correct the data pointers */ |
| 4207 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4208 | u32 ioff; |
| 4209 | item = btrfs_item_nr(leaf, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4210 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4211 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4212 | btrfs_set_token_item_offset(leaf, item, |
| 4213 | ioff - data_size, &token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4214 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4215 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4216 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4217 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4218 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 4219 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4220 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4221 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4222 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4223 | item = btrfs_item_nr(leaf, slot); |
| 4224 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4225 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4226 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4227 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4228 | btrfs_print_leaf(root, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4229 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4230 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4231 | } |
| 4232 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4233 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4234 | * Given a key and some data, insert items into the tree. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4235 | * This does all the path init required, making room in the tree if needed. |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4236 | * Returns the number of keys that were inserted. |
| 4237 | */ |
| 4238 | int btrfs_insert_some_items(struct btrfs_trans_handle *trans, |
| 4239 | struct btrfs_root *root, |
| 4240 | struct btrfs_path *path, |
| 4241 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4242 | int nr) |
| 4243 | { |
| 4244 | struct extent_buffer *leaf; |
| 4245 | struct btrfs_item *item; |
| 4246 | int ret = 0; |
| 4247 | int slot; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4248 | int i; |
| 4249 | u32 nritems; |
| 4250 | u32 total_data = 0; |
| 4251 | u32 total_size = 0; |
| 4252 | unsigned int data_end; |
| 4253 | struct btrfs_disk_key disk_key; |
| 4254 | struct btrfs_key found_key; |
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); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4258 | |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 4259 | for (i = 0; i < nr; i++) { |
| 4260 | if (total_size + data_size[i] + sizeof(struct btrfs_item) > |
| 4261 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 4262 | break; |
| 4263 | nr = i; |
| 4264 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4265 | total_data += data_size[i]; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 4266 | total_size += data_size[i] + sizeof(struct btrfs_item); |
| 4267 | } |
| 4268 | BUG_ON(nr == 0); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4269 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4270 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4271 | if (ret == 0) |
| 4272 | return -EEXIST; |
| 4273 | if (ret < 0) |
| 4274 | goto out; |
| 4275 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4276 | leaf = path->nodes[0]; |
| 4277 | |
| 4278 | nritems = btrfs_header_nritems(leaf); |
| 4279 | data_end = leaf_data_end(root, leaf); |
| 4280 | |
| 4281 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
| 4282 | for (i = nr; i >= 0; i--) { |
| 4283 | total_data -= data_size[i]; |
| 4284 | total_size -= data_size[i] + sizeof(struct btrfs_item); |
| 4285 | if (total_size < btrfs_leaf_free_space(root, leaf)) |
| 4286 | break; |
| 4287 | } |
| 4288 | nr = i; |
| 4289 | } |
| 4290 | |
| 4291 | slot = path->slots[0]; |
| 4292 | BUG_ON(slot < 0); |
| 4293 | |
| 4294 | if (slot != nritems) { |
| 4295 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
| 4296 | |
| 4297 | item = btrfs_item_nr(leaf, slot); |
| 4298 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4299 | |
| 4300 | /* figure out how many keys we can insert in here */ |
| 4301 | total_data = data_size[0]; |
| 4302 | for (i = 1; i < nr; i++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4303 | if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0) |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4304 | break; |
| 4305 | total_data += data_size[i]; |
| 4306 | } |
| 4307 | nr = i; |
| 4308 | |
| 4309 | if (old_data < data_end) { |
| 4310 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4311 | printk(KERN_CRIT "slot %d old_data %d data_end %d\n", |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4312 | slot, old_data, data_end); |
| 4313 | BUG_ON(1); |
| 4314 | } |
| 4315 | /* |
| 4316 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4317 | */ |
| 4318 | /* first correct the data pointers */ |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4319 | for (i = slot; i < nritems; i++) { |
| 4320 | u32 ioff; |
| 4321 | |
| 4322 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4323 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4324 | btrfs_set_token_item_offset(leaf, item, |
| 4325 | ioff - total_data, &token); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4326 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4327 | /* shift the items */ |
| 4328 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
| 4329 | btrfs_item_nr_offset(slot), |
| 4330 | (nritems - slot) * sizeof(struct btrfs_item)); |
| 4331 | |
| 4332 | /* shift the data */ |
| 4333 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
| 4334 | data_end - total_data, btrfs_leaf_data(leaf) + |
| 4335 | data_end, old_data - data_end); |
| 4336 | data_end = old_data; |
| 4337 | } else { |
| 4338 | /* |
| 4339 | * this sucks but it has to be done, if we are inserting at |
| 4340 | * the end of the leaf only insert 1 of the items, since we |
| 4341 | * have no way of knowing whats on the next leaf and we'd have |
| 4342 | * to drop our current locks to figure it out |
| 4343 | */ |
| 4344 | nr = 1; |
| 4345 | } |
| 4346 | |
| 4347 | /* setup the item for the new data */ |
| 4348 | for (i = 0; i < nr; i++) { |
| 4349 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4350 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
| 4351 | item = btrfs_item_nr(leaf, slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4352 | btrfs_set_token_item_offset(leaf, item, |
| 4353 | data_end - data_size[i], &token); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4354 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4355 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4356 | } |
| 4357 | btrfs_set_header_nritems(leaf, nritems + nr); |
| 4358 | btrfs_mark_buffer_dirty(leaf); |
| 4359 | |
| 4360 | ret = 0; |
| 4361 | if (slot == 0) { |
| 4362 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4363 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4367 | btrfs_print_leaf(root, leaf); |
| 4368 | BUG(); |
| 4369 | } |
| 4370 | out: |
| 4371 | if (!ret) |
| 4372 | ret = nr; |
| 4373 | return ret; |
| 4374 | } |
| 4375 | |
| 4376 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4377 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
| 4378 | * to save stack depth by doing the bulk of the work in a function |
| 4379 | * that doesn't call btrfs_search_slot |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4380 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4381 | void setup_items_for_insert(struct btrfs_trans_handle *trans, |
| 4382 | struct btrfs_root *root, struct btrfs_path *path, |
| 4383 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4384 | u32 total_data, u32 total_size, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4385 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4386 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4387 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4388 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4389 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4390 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4391 | struct extent_buffer *leaf; |
| 4392 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4393 | struct btrfs_map_token token; |
| 4394 | |
| 4395 | btrfs_init_map_token(&token); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4396 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4397 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4398 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4399 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4400 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4401 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4402 | |
Chris Mason | f25956c | 2008-09-12 15:32:53 -0400 | [diff] [blame] | 4403 | if (btrfs_leaf_free_space(root, leaf) < total_size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4404 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4405 | printk(KERN_CRIT "not enough freespace need %u have %d\n", |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4406 | total_size, btrfs_leaf_free_space(root, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4407 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4408 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4409 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4410 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4411 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4412 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4413 | if (old_data < data_end) { |
| 4414 | btrfs_print_leaf(root, leaf); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4415 | printk(KERN_CRIT "slot %d old_data %d data_end %d\n", |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4416 | slot, old_data, data_end); |
| 4417 | BUG_ON(1); |
| 4418 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4419 | /* |
| 4420 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4421 | */ |
| 4422 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4423 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4424 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4425 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4426 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4427 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4428 | btrfs_set_token_item_offset(leaf, item, |
| 4429 | ioff - total_data, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4430 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4431 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4432 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4433 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4434 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4435 | |
| 4436 | /* shift the data */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4437 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4438 | data_end - total_data, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4439 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4440 | data_end = old_data; |
| 4441 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4442 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4443 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4444 | for (i = 0; i < nr; i++) { |
| 4445 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4446 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
| 4447 | item = btrfs_item_nr(leaf, slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4448 | btrfs_set_token_item_offset(leaf, item, |
| 4449 | data_end - data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4450 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4451 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4452 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4453 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4454 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4455 | |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4456 | if (slot == 0) { |
| 4457 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4458 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | 5a01a2e | 2008-01-30 11:43:54 -0500 | [diff] [blame] | 4459 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4460 | btrfs_unlock_up_safe(path, 1); |
| 4461 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4462 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4463 | if (btrfs_leaf_free_space(root, leaf) < 0) { |
| 4464 | btrfs_print_leaf(root, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4465 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4466 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | /* |
| 4470 | * Given a key and some data, insert items into the tree. |
| 4471 | * This does all the path init required, making room in the tree if needed. |
| 4472 | */ |
| 4473 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4474 | struct btrfs_root *root, |
| 4475 | struct btrfs_path *path, |
| 4476 | struct btrfs_key *cpu_key, u32 *data_size, |
| 4477 | int nr) |
| 4478 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4479 | int ret = 0; |
| 4480 | int slot; |
| 4481 | int i; |
| 4482 | u32 total_size = 0; |
| 4483 | u32 total_data = 0; |
| 4484 | |
| 4485 | for (i = 0; i < nr; i++) |
| 4486 | total_data += data_size[i]; |
| 4487 | |
| 4488 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4489 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4490 | if (ret == 0) |
| 4491 | return -EEXIST; |
| 4492 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4493 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4494 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4495 | slot = path->slots[0]; |
| 4496 | BUG_ON(slot < 0); |
| 4497 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4498 | setup_items_for_insert(trans, root, path, cpu_key, data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4499 | total_data, total_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4500 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /* |
| 4504 | * Given a key and some data, insert an item into the tree. |
| 4505 | * This does all the path init required, making room in the tree if needed. |
| 4506 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4507 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 4508 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 4509 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4510 | { |
| 4511 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4512 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4513 | struct extent_buffer *leaf; |
| 4514 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4515 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4516 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4517 | if (!path) |
| 4518 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4519 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4520 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4521 | leaf = path->nodes[0]; |
| 4522 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4523 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4524 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4525 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4526 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4527 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4528 | } |
| 4529 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4530 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4531 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4532 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4533 | * the tree should have been previously balanced so the deletion does not |
| 4534 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4535 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4536 | 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] | 4537 | struct btrfs_path *path, int level, int slot, |
| 4538 | int tree_mod_log) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4539 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4540 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4541 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4542 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4543 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4544 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4545 | if (slot != nritems - 1) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4546 | if (tree_mod_log && level) |
| 4547 | tree_mod_log_eb_move(root->fs_info, parent, slot, |
| 4548 | slot + 1, nritems - slot - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4549 | memmove_extent_buffer(parent, |
| 4550 | btrfs_node_key_ptr_offset(slot), |
| 4551 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4552 | sizeof(struct btrfs_key_ptr) * |
| 4553 | (nritems - slot - 1)); |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame^] | 4554 | } else if (tree_mod_log && level) { |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4555 | ret = tree_mod_log_insert_key(root->fs_info, parent, slot, |
| 4556 | MOD_LOG_KEY_REMOVE); |
| 4557 | BUG_ON(ret < 0); |
| 4558 | } |
| 4559 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4560 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4561 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4562 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4563 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4564 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4565 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4566 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4567 | struct btrfs_disk_key disk_key; |
| 4568 | |
| 4569 | btrfs_node_key(parent, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4570 | fixup_low_keys(trans, root, path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4571 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4572 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4573 | } |
| 4574 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4575 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4576 | * 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] | 4577 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4578 | * |
| 4579 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4580 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4581 | * |
| 4582 | * The path must have already been setup for deleting the leaf, including |
| 4583 | * all the proper balancing. path->nodes[1] must be locked. |
| 4584 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4585 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4586 | struct btrfs_root *root, |
| 4587 | struct btrfs_path *path, |
| 4588 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4589 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4590 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4591 | del_ptr(trans, root, path, 1, path->slots[1], 1); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4592 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4593 | /* |
| 4594 | * btrfs_free_extent is expensive, we want to make sure we |
| 4595 | * aren't holding any locks when we call it |
| 4596 | */ |
| 4597 | btrfs_unlock_up_safe(path, 0); |
| 4598 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4599 | root_sub_used(root, leaf->len); |
| 4600 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4601 | extent_buffer_get(leaf); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4602 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4603 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4604 | } |
| 4605 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4606 | * delete the item at the leaf level in path. If that empties |
| 4607 | * the leaf, remove it from the tree |
| 4608 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4609 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4610 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4611 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4612 | struct extent_buffer *leaf; |
| 4613 | struct btrfs_item *item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4614 | int last_off; |
| 4615 | int dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4616 | int ret = 0; |
| 4617 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4618 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4619 | u32 nritems; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4620 | struct btrfs_map_token token; |
| 4621 | |
| 4622 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4623 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4624 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4625 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4626 | |
| 4627 | for (i = 0; i < nr; i++) |
| 4628 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4629 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4630 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4631 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4632 | if (slot + nr != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4633 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4634 | |
| 4635 | memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4636 | data_end + dsize, |
| 4637 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4638 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4639 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4640 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4641 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4642 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4643 | item = btrfs_item_nr(leaf, i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4644 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4645 | btrfs_set_token_item_offset(leaf, item, |
| 4646 | ioff + dsize, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4647 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4648 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4649 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4650 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4651 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4652 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4653 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4654 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4655 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4656 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4657 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4658 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4659 | if (leaf == root->node) { |
| 4660 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4661 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4662 | btrfs_set_path_blocking(path); |
| 4663 | clean_tree_block(trans, root, leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4664 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4665 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4666 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4667 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4668 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4669 | struct btrfs_disk_key disk_key; |
| 4670 | |
| 4671 | btrfs_item_key(leaf, &disk_key, 0); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4672 | fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4673 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4674 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4675 | /* delete the leaf if it is mostly empty */ |
Yan Zheng | d717aa1 | 2009-07-24 12:42:46 -0400 | [diff] [blame] | 4676 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4677 | /* push_leaf_left fixes the path. |
| 4678 | * make sure the path still points to our leaf |
| 4679 | * for possible call to del_ptr below |
| 4680 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 4681 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4682 | extent_buffer_get(leaf); |
| 4683 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4684 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4685 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 4686 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4687 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4688 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | |
| 4690 | if (path->nodes[0] == leaf && |
| 4691 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4692 | wret = push_leaf_right(trans, root, path, 1, |
| 4693 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4694 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4695 | ret = wret; |
| 4696 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4697 | |
| 4698 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4699 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4700 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4701 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4702 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4703 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4704 | /* if we're still in the path, make sure |
| 4705 | * we're dirty. Otherwise, one of the |
| 4706 | * push_leaf functions must have already |
| 4707 | * dirtied this buffer |
| 4708 | */ |
| 4709 | if (path->nodes[0] == leaf) |
| 4710 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4711 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4712 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 4713 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4714 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4715 | } |
| 4716 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4717 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4718 | } |
| 4719 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 4720 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4721 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4722 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 4723 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4724 | * |
| 4725 | * This may release the path, and so you may lose any locks held at the |
| 4726 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4727 | */ |
| 4728 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
| 4729 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4730 | struct btrfs_key key; |
| 4731 | struct btrfs_disk_key found_key; |
| 4732 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4733 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4734 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4735 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4736 | if (key.offset > 0) |
| 4737 | key.offset--; |
| 4738 | else if (key.type > 0) |
| 4739 | key.type--; |
| 4740 | else if (key.objectid > 0) |
| 4741 | key.objectid--; |
| 4742 | else |
| 4743 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4744 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4745 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4746 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4747 | if (ret < 0) |
| 4748 | return ret; |
| 4749 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 4750 | ret = comp_keys(&found_key, &key); |
| 4751 | if (ret < 0) |
| 4752 | return 0; |
| 4753 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4754 | } |
| 4755 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4756 | /* |
| 4757 | * A helper function to walk down the tree starting at min_key, and looking |
| 4758 | * 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] | 4759 | * 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] | 4760 | * |
| 4761 | * This does not cow, but it does stuff the starting key it finds back |
| 4762 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 4763 | * key and get a writable path. |
| 4764 | * |
| 4765 | * This does lock as it descends, and path->keep_locks should be set |
| 4766 | * to 1 by the caller. |
| 4767 | * |
| 4768 | * This honors path->lowest_level to prevent descent past a given level |
| 4769 | * of the tree. |
| 4770 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4771 | * min_trans indicates the oldest transaction that you are interested |
| 4772 | * in walking through. Any nodes or leaves older than min_trans are |
| 4773 | * skipped over (without reading them). |
| 4774 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4775 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 4776 | * was nothing in the tree that matched the search criteria. |
| 4777 | */ |
| 4778 | 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] | 4779 | struct btrfs_key *max_key, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4780 | struct btrfs_path *path, int cache_only, |
| 4781 | u64 min_trans) |
| 4782 | { |
| 4783 | struct extent_buffer *cur; |
| 4784 | struct btrfs_key found_key; |
| 4785 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4786 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4787 | u32 nritems; |
| 4788 | int level; |
| 4789 | int ret = 1; |
| 4790 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4791 | WARN_ON(!path->keep_locks); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4792 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4793 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4794 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4795 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4796 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4797 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4798 | |
| 4799 | if (btrfs_header_generation(cur) < min_trans) { |
| 4800 | ret = 1; |
| 4801 | goto out; |
| 4802 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4803 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4804 | nritems = btrfs_header_nritems(cur); |
| 4805 | level = btrfs_header_level(cur); |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4806 | sret = bin_search(cur, min_key, level, &slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4807 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4808 | /* at the lowest level, we're done, setup the path and exit */ |
| 4809 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4810 | if (slot >= nritems) |
| 4811 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4812 | ret = 0; |
| 4813 | path->slots[level] = slot; |
| 4814 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 4815 | goto out; |
| 4816 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 4817 | if (sret && slot > 0) |
| 4818 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4819 | /* |
| 4820 | * check this node pointer against the cache_only and |
| 4821 | * min_trans parameters. If it isn't in cache or is too |
| 4822 | * old, skip to the next one. |
| 4823 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4824 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4825 | u64 blockptr; |
| 4826 | u64 gen; |
| 4827 | struct extent_buffer *tmp; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4828 | struct btrfs_disk_key disk_key; |
| 4829 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4830 | blockptr = btrfs_node_blockptr(cur, slot); |
| 4831 | gen = btrfs_node_ptr_generation(cur, slot); |
| 4832 | if (gen < min_trans) { |
| 4833 | slot++; |
| 4834 | continue; |
| 4835 | } |
| 4836 | if (!cache_only) |
| 4837 | break; |
| 4838 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4839 | if (max_key) { |
| 4840 | btrfs_node_key(cur, &disk_key, slot); |
| 4841 | if (comp_keys(&disk_key, max_key) >= 0) { |
| 4842 | ret = 1; |
| 4843 | goto out; |
| 4844 | } |
| 4845 | } |
| 4846 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4847 | tmp = btrfs_find_tree_block(root, blockptr, |
| 4848 | btrfs_level_size(root, level - 1)); |
| 4849 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 4850 | if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4851 | free_extent_buffer(tmp); |
| 4852 | break; |
| 4853 | } |
| 4854 | if (tmp) |
| 4855 | free_extent_buffer(tmp); |
| 4856 | slot++; |
| 4857 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4858 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4859 | /* |
| 4860 | * we didn't find a candidate key in this node, walk forward |
| 4861 | * and find another one |
| 4862 | */ |
| 4863 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4864 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4865 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4866 | sret = btrfs_find_next_key(root, path, min_key, level, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4867 | cache_only, min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4868 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4869 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4870 | goto again; |
| 4871 | } else { |
| 4872 | goto out; |
| 4873 | } |
| 4874 | } |
| 4875 | /* save our key for returning back */ |
| 4876 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 4877 | path->slots[level] = slot; |
| 4878 | if (level == path->lowest_level) { |
| 4879 | ret = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4880 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4881 | goto out; |
| 4882 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4883 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4884 | cur = read_node_slot(root, cur, slot); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4885 | BUG_ON(!cur); /* -ENOMEM */ |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4886 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4887 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4888 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4889 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4890 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 4891 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4892 | btrfs_clear_path_blocking(path, NULL, 0); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4893 | } |
| 4894 | out: |
| 4895 | if (ret == 0) |
| 4896 | memcpy(min_key, &found_key, sizeof(found_key)); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4897 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4898 | return ret; |
| 4899 | } |
| 4900 | |
| 4901 | /* |
| 4902 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 4903 | * and fixup the path. It looks for and returns the next key in the |
| 4904 | * tree based on the current path and the cache_only and min_trans |
| 4905 | * parameters. |
| 4906 | * |
| 4907 | * 0 is returned if another key is found, < 0 if there are any errors |
| 4908 | * and 1 is returned if there are no higher keys in the tree |
| 4909 | * |
| 4910 | * path->keep_locks should be set to 1 on the search made before |
| 4911 | * calling this function. |
| 4912 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4913 | 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] | 4914 | struct btrfs_key *key, int level, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4915 | int cache_only, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4916 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4917 | int slot; |
| 4918 | struct extent_buffer *c; |
| 4919 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 4920 | WARN_ON(!path->keep_locks); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4921 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4922 | if (!path->nodes[level]) |
| 4923 | return 1; |
| 4924 | |
| 4925 | slot = path->slots[level] + 1; |
| 4926 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4927 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4928 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4929 | int ret; |
| 4930 | int orig_lowest; |
| 4931 | struct btrfs_key cur_key; |
| 4932 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 4933 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4934 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4935 | |
| 4936 | if (path->locks[level + 1]) { |
| 4937 | level++; |
| 4938 | continue; |
| 4939 | } |
| 4940 | |
| 4941 | slot = btrfs_header_nritems(c) - 1; |
| 4942 | if (level == 0) |
| 4943 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 4944 | else |
| 4945 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 4946 | |
| 4947 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4948 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4949 | path->lowest_level = level; |
| 4950 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 4951 | 0, 0); |
| 4952 | path->lowest_level = orig_lowest; |
| 4953 | if (ret < 0) |
| 4954 | return ret; |
| 4955 | |
| 4956 | c = path->nodes[level]; |
| 4957 | slot = path->slots[level]; |
| 4958 | if (ret == 0) |
| 4959 | slot++; |
| 4960 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4961 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4962 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4963 | if (level == 0) |
| 4964 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4965 | else { |
| 4966 | u64 blockptr = btrfs_node_blockptr(c, slot); |
| 4967 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 4968 | |
| 4969 | if (cache_only) { |
| 4970 | struct extent_buffer *cur; |
| 4971 | cur = btrfs_find_tree_block(root, blockptr, |
| 4972 | btrfs_level_size(root, level - 1)); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 4973 | if (!cur || |
| 4974 | btrfs_buffer_uptodate(cur, gen, 1) <= 0) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4975 | slot++; |
| 4976 | if (cur) |
| 4977 | free_extent_buffer(cur); |
| 4978 | goto next; |
| 4979 | } |
| 4980 | free_extent_buffer(cur); |
| 4981 | } |
| 4982 | if (gen < min_trans) { |
| 4983 | slot++; |
| 4984 | goto next; |
| 4985 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4986 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4987 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4988 | return 0; |
| 4989 | } |
| 4990 | return 1; |
| 4991 | } |
| 4992 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4993 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4994 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4995 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 4996 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 4997 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 4998 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 4999 | { |
| 5000 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5001 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5002 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5003 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5004 | struct btrfs_key key; |
| 5005 | u32 nritems; |
| 5006 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5007 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5008 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5009 | |
| 5010 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5011 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5012 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5013 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5014 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5015 | again: |
| 5016 | level = 1; |
| 5017 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5018 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5019 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5020 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5021 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5022 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5023 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5024 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5025 | path->keep_locks = 0; |
| 5026 | |
| 5027 | if (ret < 0) |
| 5028 | return ret; |
| 5029 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5030 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5031 | /* |
| 5032 | * by releasing the path above we dropped all our locks. A balance |
| 5033 | * could have added more items next to the key that used to be |
| 5034 | * at the very end of the block. So, check again here and |
| 5035 | * advance the path if there are now more items available. |
| 5036 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5037 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5038 | if (ret == 0) |
| 5039 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5040 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5041 | goto done; |
| 5042 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5043 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5044 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5045 | if (!path->nodes[level]) { |
| 5046 | ret = 1; |
| 5047 | goto done; |
| 5048 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5049 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5050 | slot = path->slots[level] + 1; |
| 5051 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5052 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5053 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5054 | if (level == BTRFS_MAX_LEVEL) { |
| 5055 | ret = 1; |
| 5056 | goto done; |
| 5057 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5058 | continue; |
| 5059 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5060 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5061 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5062 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5063 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5064 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5065 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5066 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5067 | next_rw_lock = path->locks[level]; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5068 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5069 | slot, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5070 | if (ret == -EAGAIN) |
| 5071 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5072 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5073 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5074 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5075 | goto done; |
| 5076 | } |
| 5077 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5078 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5079 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5080 | if (!ret) { |
| 5081 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5082 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5083 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5084 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5085 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5086 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5087 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5088 | break; |
| 5089 | } |
| 5090 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5091 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5092 | level--; |
| 5093 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5094 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5095 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5096 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5097 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5098 | path->nodes[level] = next; |
| 5099 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5100 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5101 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5102 | if (!level) |
| 5103 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5104 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5105 | ret = read_block_for_search(NULL, root, path, &next, level, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 5106 | 0, &key, 0); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5107 | if (ret == -EAGAIN) |
| 5108 | goto again; |
| 5109 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5110 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5111 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5112 | goto done; |
| 5113 | } |
| 5114 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5115 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5116 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5117 | if (!ret) { |
| 5118 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5119 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5120 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5121 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5122 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5123 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5124 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5125 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5126 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5127 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5128 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5129 | path->leave_spinning = old_spinning; |
| 5130 | if (!old_spinning) |
| 5131 | btrfs_set_path_blocking(path); |
| 5132 | |
| 5133 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5134 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5135 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5136 | /* |
| 5137 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5138 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5139 | * |
| 5140 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5141 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5142 | int btrfs_previous_item(struct btrfs_root *root, |
| 5143 | struct btrfs_path *path, u64 min_objectid, |
| 5144 | int type) |
| 5145 | { |
| 5146 | struct btrfs_key found_key; |
| 5147 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5148 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5149 | int ret; |
| 5150 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5151 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5152 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5153 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5154 | ret = btrfs_prev_leaf(root, path); |
| 5155 | if (ret != 0) |
| 5156 | return ret; |
| 5157 | } else { |
| 5158 | path->slots[0]--; |
| 5159 | } |
| 5160 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5161 | nritems = btrfs_header_nritems(leaf); |
| 5162 | if (nritems == 0) |
| 5163 | return 1; |
| 5164 | if (path->slots[0] == nritems) |
| 5165 | path->slots[0]--; |
| 5166 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5167 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5168 | if (found_key.objectid < min_objectid) |
| 5169 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5170 | if (found_key.type == type) |
| 5171 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5172 | if (found_key.objectid == min_objectid && |
| 5173 | found_key.type < type) |
| 5174 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5175 | } |
| 5176 | return 1; |
| 5177 | } |