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