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