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