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