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