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