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