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