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