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