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