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 | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 19 | #include <linux/module.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 | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 23 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 24 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 25 | *root, struct btrfs_path *path, int level); |
| 26 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 27 | *root, struct btrfs_key *ins_key, |
| 28 | struct btrfs_path *path, int data_size); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 30 | *root, struct buffer_head *dst, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | *src); |
| 32 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 33 | btrfs_root *root, struct buffer_head *dst_buf, |
| 34 | struct buffer_head *src_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 35 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 36 | struct btrfs_path *path, int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 37 | |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 38 | inline void btrfs_init_path(struct btrfs_path *p) |
| 39 | { |
| 40 | memset(p, 0, sizeof(*p)); |
| 41 | } |
| 42 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 43 | struct btrfs_path *btrfs_alloc_path(void) |
| 44 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 45 | struct btrfs_path *path; |
| 46 | path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); |
| 47 | if (path) |
| 48 | btrfs_init_path(path); |
| 49 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void btrfs_free_path(struct btrfs_path *p) |
| 53 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 54 | btrfs_release_path(NULL, p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 55 | kmem_cache_free(btrfs_path_cachep, p); |
| 56 | } |
| 57 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 58 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 59 | { |
| 60 | int i; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 61 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 62 | if (!p->nodes[i]) |
| 63 | break; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 64 | btrfs_block_release(root, p->nodes[i]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 65 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 66 | memset(p, 0, sizeof(*p)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 69 | static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 70 | *root, struct buffer_head *buf, struct buffer_head |
| 71 | *parent, int parent_slot, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 72 | **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 73 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 74 | struct buffer_head *cow; |
| 75 | struct btrfs_node *cow_node; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 76 | |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 77 | if (btrfs_header_generation(btrfs_buffer_header(buf)) == |
| 78 | trans->transid) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 79 | *cow_ret = buf; |
| 80 | return 0; |
| 81 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 82 | cow = btrfs_alloc_free_block(trans, root, buf->b_blocknr); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 83 | cow_node = btrfs_buffer_node(cow); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 84 | if (buf->b_size != root->blocksize || cow->b_size != root->blocksize) |
| 85 | WARN_ON(1); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 86 | memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 87 | btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 88 | btrfs_set_header_generation(&cow_node->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 89 | btrfs_set_header_owner(&cow_node->header, root->root_key.objectid); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 90 | btrfs_inc_ref(trans, root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 91 | if (buf == root->node) { |
| 92 | root->node = cow; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 93 | get_bh(cow); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 94 | if (buf != root->commit_root) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 95 | btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 96 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 97 | btrfs_block_release(root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 98 | } else { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 99 | btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 100 | bh_blocknr(cow)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 101 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 102 | btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 103 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 104 | btrfs_block_release(root, buf); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 105 | mark_buffer_dirty(cow); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 106 | *cow_ret = cow; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 110 | /* |
| 111 | * The leaf data grows from end-to-front in the node. |
| 112 | * this returns the address of the start of the last item, |
| 113 | * which is the stop of the leaf data stack |
| 114 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 115 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
| 116 | struct btrfs_leaf *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 117 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 118 | u32 nr = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 119 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 120 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 121 | return btrfs_item_offset(leaf->items + nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 124 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 125 | * compare two keys in a memcmp fashion |
| 126 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 127 | 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] | 128 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 129 | struct btrfs_key k1; |
| 130 | |
| 131 | btrfs_disk_key_to_cpu(&k1, disk); |
| 132 | |
| 133 | if (k1.objectid > k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 134 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 135 | if (k1.objectid < k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 136 | return -1; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 137 | if (k1.flags > k2->flags) |
| 138 | return 1; |
| 139 | if (k1.flags < k2->flags) |
| 140 | return -1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 141 | if (k1.offset > k2->offset) |
| 142 | return 1; |
| 143 | if (k1.offset < k2->offset) |
| 144 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 147 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 148 | static int check_node(struct btrfs_root *root, struct btrfs_path *path, |
| 149 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 150 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 151 | struct btrfs_node *parent = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 152 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 153 | int parent_slot; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 154 | int slot; |
| 155 | struct btrfs_key cpukey; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 156 | u32 nritems = btrfs_header_nritems(&node->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 157 | |
| 158 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 159 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 160 | parent_slot = path->slots[level + 1]; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 161 | slot = path->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 162 | BUG_ON(nritems == 0); |
| 163 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 164 | struct btrfs_disk_key *parent_key; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 165 | parent_key = &parent->ptrs[parent_slot].key; |
| 166 | BUG_ON(memcmp(parent_key, &node->ptrs[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 167 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 168 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 169 | btrfs_header_blocknr(&node->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 170 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 171 | BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 172 | if (slot != 0) { |
| 173 | btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key); |
| 174 | BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0); |
| 175 | } |
| 176 | if (slot < nritems - 1) { |
| 177 | btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key); |
| 178 | BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 183 | static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 184 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 185 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 186 | struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 187 | struct btrfs_node *parent = NULL; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 188 | int parent_slot; |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 189 | int slot = path->slots[0]; |
| 190 | struct btrfs_key cpukey; |
| 191 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 192 | u32 nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 193 | |
| 194 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 195 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 196 | parent_slot = path->slots[level + 1]; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 197 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 198 | |
| 199 | if (nritems == 0) |
| 200 | return 0; |
| 201 | |
| 202 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 203 | struct btrfs_disk_key *parent_key; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 204 | parent_key = &parent->ptrs[parent_slot].key; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 205 | BUG_ON(memcmp(parent_key, &leaf->items[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 206 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 207 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 208 | btrfs_header_blocknr(&leaf->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 209 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 210 | if (slot != 0) { |
| 211 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key); |
| 212 | BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0); |
| 213 | BUG_ON(btrfs_item_offset(leaf->items + slot - 1) != |
| 214 | btrfs_item_end(leaf->items + slot)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 215 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 216 | if (slot < nritems - 1) { |
| 217 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key); |
| 218 | BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0); |
| 219 | BUG_ON(btrfs_item_offset(leaf->items + slot) != |
| 220 | btrfs_item_end(leaf->items + slot + 1)); |
| 221 | } |
| 222 | BUG_ON(btrfs_item_offset(leaf->items) + |
| 223 | btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 227 | static int check_block(struct btrfs_root *root, struct btrfs_path *path, |
| 228 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 229 | { |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 230 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
| 231 | if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid, |
| 232 | sizeof(node->header.fsid))) |
| 233 | BUG(); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 234 | if (level == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 235 | return check_leaf(root, path, level); |
| 236 | return check_node(root, path, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 239 | /* |
| 240 | * search for key in the array p. items p are item_size apart |
| 241 | * and there are 'max' items in p |
| 242 | * the slot in the array is returned via slot, and it points to |
| 243 | * the place where you would insert key if it is not found in |
| 244 | * the array. |
| 245 | * |
| 246 | * slot may point to max if the key is bigger than all of the keys |
| 247 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 248 | static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 249 | int max, int *slot) |
| 250 | { |
| 251 | int low = 0; |
| 252 | int high = max; |
| 253 | int mid; |
| 254 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 255 | struct btrfs_disk_key *tmp; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 256 | |
| 257 | while(low < high) { |
| 258 | mid = (low + high) / 2; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 259 | tmp = (struct btrfs_disk_key *)(p + mid * item_size); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 260 | ret = comp_keys(tmp, key); |
| 261 | |
| 262 | if (ret < 0) |
| 263 | low = mid + 1; |
| 264 | else if (ret > 0) |
| 265 | high = mid; |
| 266 | else { |
| 267 | *slot = mid; |
| 268 | return 0; |
| 269 | } |
| 270 | } |
| 271 | *slot = low; |
| 272 | return 1; |
| 273 | } |
| 274 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 275 | /* |
| 276 | * simple bin_search frontend that does the right thing for |
| 277 | * leaves vs nodes |
| 278 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 279 | static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 280 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 281 | if (btrfs_is_leaf(c)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 282 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 283 | return generic_bin_search((void *)l->items, |
| 284 | sizeof(struct btrfs_item), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 285 | key, btrfs_header_nritems(&c->header), |
| 286 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 287 | } else { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 288 | return generic_bin_search((void *)c->ptrs, |
| 289 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 290 | key, btrfs_header_nritems(&c->header), |
| 291 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 292 | } |
| 293 | return -1; |
| 294 | } |
| 295 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 296 | static struct buffer_head *read_node_slot(struct btrfs_root *root, |
| 297 | struct buffer_head *parent_buf, |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 298 | int slot) |
| 299 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 300 | struct btrfs_node *node = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 301 | if (slot < 0) |
| 302 | return NULL; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 303 | if (slot >= btrfs_header_nritems(&node->header)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 304 | return NULL; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 305 | return read_tree_block(root, btrfs_node_blockptr(node, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 308 | static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root |
| 309 | *root, struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 310 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 311 | struct buffer_head *right_buf; |
| 312 | struct buffer_head *mid_buf; |
| 313 | struct buffer_head *left_buf; |
| 314 | struct buffer_head *parent_buf = NULL; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 315 | struct btrfs_node *right = NULL; |
| 316 | struct btrfs_node *mid; |
| 317 | struct btrfs_node *left = NULL; |
| 318 | struct btrfs_node *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 319 | int ret = 0; |
| 320 | int wret; |
| 321 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 322 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 323 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 324 | |
| 325 | if (level == 0) |
| 326 | return 0; |
| 327 | |
| 328 | mid_buf = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 329 | mid = btrfs_buffer_node(mid_buf); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 330 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 331 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 332 | if (level < BTRFS_MAX_LEVEL - 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 333 | parent_buf = path->nodes[level + 1]; |
| 334 | pslot = path->slots[level + 1]; |
| 335 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 336 | /* |
| 337 | * deal with the case where there is only one pointer in the root |
| 338 | * by promoting the node below to a root |
| 339 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 340 | if (!parent_buf) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 341 | struct buffer_head *child; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 342 | u64 blocknr = bh_blocknr(mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 343 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 344 | if (btrfs_header_nritems(&mid->header) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 345 | return 0; |
| 346 | |
| 347 | /* promote the child to a root */ |
| 348 | child = read_node_slot(root, mid_buf, 0); |
| 349 | BUG_ON(!child); |
| 350 | root->node = child; |
| 351 | path->nodes[level] = NULL; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 352 | clean_tree_block(trans, root, mid_buf); |
| 353 | wait_on_buffer(mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 354 | /* once for the path */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 355 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 356 | /* once for the root ptr */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 357 | btrfs_block_release(root, mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 358 | return btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 359 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 360 | parent = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 361 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 362 | if (btrfs_header_nritems(&mid->header) > |
| 363 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 364 | return 0; |
| 365 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 366 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 367 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 368 | |
| 369 | /* first, try to make some room in the middle buffer */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 370 | if (left_buf) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 371 | btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1, |
| 372 | &left_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 373 | left = btrfs_buffer_node(left_buf); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 374 | orig_slot += btrfs_header_nritems(&left->header); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 375 | wret = push_node_left(trans, root, left_buf, mid_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 376 | if (wret < 0) |
| 377 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 378 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * then try to empty the right most buffer into the middle |
| 382 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 383 | if (right_buf) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 384 | btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1, |
| 385 | &right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 386 | right = btrfs_buffer_node(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 387 | wret = push_node_left(trans, root, mid_buf, right_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 388 | if (wret < 0) |
| 389 | ret = wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 390 | if (btrfs_header_nritems(&right->header) == 0) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 391 | u64 blocknr = bh_blocknr(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 392 | clean_tree_block(trans, root, right_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 393 | wait_on_buffer(right_buf); |
| 394 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 395 | right_buf = NULL; |
| 396 | right = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 397 | wret = del_ptr(trans, root, path, level + 1, pslot + |
| 398 | 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 399 | if (wret) |
| 400 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 401 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 402 | if (wret) |
| 403 | ret = wret; |
| 404 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 405 | btrfs_memcpy(root, parent, |
| 406 | &parent->ptrs[pslot + 1].key, |
| 407 | &right->ptrs[0].key, |
| 408 | sizeof(struct btrfs_disk_key)); |
| 409 | btrfs_mark_buffer_dirty(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 410 | } |
| 411 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 412 | if (btrfs_header_nritems(&mid->header) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 413 | /* |
| 414 | * we're not allowed to leave a node with one item in the |
| 415 | * tree during a delete. A deletion from lower in the tree |
| 416 | * could try to delete the only pointer in this node. |
| 417 | * So, pull some keys from the left. |
| 418 | * There has to be a left pointer at this point because |
| 419 | * otherwise we would have pulled some pointers from the |
| 420 | * right |
| 421 | */ |
| 422 | BUG_ON(!left_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 423 | wret = balance_node_right(trans, root, mid_buf, left_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 424 | if (wret < 0) |
| 425 | ret = wret; |
| 426 | BUG_ON(wret == 1); |
| 427 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 428 | if (btrfs_header_nritems(&mid->header) == 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 429 | /* we've managed to empty the middle node, drop it */ |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 430 | u64 blocknr = bh_blocknr(mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 431 | clean_tree_block(trans, root, mid_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 432 | wait_on_buffer(mid_buf); |
| 433 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 434 | mid_buf = NULL; |
| 435 | mid = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 436 | wret = del_ptr(trans, root, path, level + 1, pslot); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 437 | if (wret) |
| 438 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 439 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 440 | if (wret) |
| 441 | ret = wret; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 442 | } else { |
| 443 | /* update the parent key to reflect our changes */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 444 | btrfs_memcpy(root, parent, |
| 445 | &parent->ptrs[pslot].key, &mid->ptrs[0].key, |
| 446 | sizeof(struct btrfs_disk_key)); |
| 447 | btrfs_mark_buffer_dirty(parent_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 448 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 449 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 450 | /* update the path */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 451 | if (left_buf) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 452 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 453 | get_bh(left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 454 | path->nodes[level] = left_buf; |
| 455 | path->slots[level + 1] -= 1; |
| 456 | path->slots[level] = orig_slot; |
| 457 | if (mid_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 458 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 459 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 460 | orig_slot -= btrfs_header_nritems(&left->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 461 | path->slots[level] = orig_slot; |
| 462 | } |
| 463 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 464 | /* double check we haven't messed things up */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 465 | check_block(root, path, level); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 466 | if (orig_ptr != |
| 467 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]), |
| 468 | path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 469 | BUG(); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 470 | |
| 471 | if (right_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 472 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 473 | if (left_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 474 | btrfs_block_release(root, left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 475 | return ret; |
| 476 | } |
| 477 | |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 478 | /* returns zero if the push worked, non-zero otherwise */ |
| 479 | static int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
| 480 | struct btrfs_root *root, |
| 481 | struct btrfs_path *path, int level) |
| 482 | { |
| 483 | struct buffer_head *right_buf; |
| 484 | struct buffer_head *mid_buf; |
| 485 | struct buffer_head *left_buf; |
| 486 | struct buffer_head *parent_buf = NULL; |
| 487 | struct btrfs_node *right = NULL; |
| 488 | struct btrfs_node *mid; |
| 489 | struct btrfs_node *left = NULL; |
| 490 | struct btrfs_node *parent = NULL; |
| 491 | int ret = 0; |
| 492 | int wret; |
| 493 | int pslot; |
| 494 | int orig_slot = path->slots[level]; |
| 495 | u64 orig_ptr; |
| 496 | |
| 497 | if (level == 0) |
| 498 | return 1; |
| 499 | |
| 500 | mid_buf = path->nodes[level]; |
| 501 | mid = btrfs_buffer_node(mid_buf); |
| 502 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
| 503 | |
| 504 | if (level < BTRFS_MAX_LEVEL - 1) |
| 505 | parent_buf = path->nodes[level + 1]; |
| 506 | pslot = path->slots[level + 1]; |
| 507 | |
| 508 | if (!parent_buf) |
| 509 | return 1; |
| 510 | parent = btrfs_buffer_node(parent_buf); |
| 511 | |
| 512 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 513 | |
| 514 | /* first, try to make some room in the middle buffer */ |
| 515 | if (left_buf) { |
| 516 | u32 left_nr; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 517 | left = btrfs_buffer_node(left_buf); |
| 518 | left_nr = btrfs_header_nritems(&left->header); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 519 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 520 | wret = 1; |
| 521 | } else { |
| 522 | btrfs_cow_block(trans, root, left_buf, parent_buf, |
| 523 | pslot - 1, &left_buf); |
| 524 | left = btrfs_buffer_node(left_buf); |
| 525 | wret = push_node_left(trans, root, left_buf, mid_buf); |
| 526 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 527 | if (wret < 0) |
| 528 | ret = wret; |
| 529 | if (wret == 0) { |
| 530 | orig_slot += left_nr; |
| 531 | btrfs_memcpy(root, parent, |
| 532 | &parent->ptrs[pslot].key, |
| 533 | &mid->ptrs[0].key, |
| 534 | sizeof(struct btrfs_disk_key)); |
| 535 | btrfs_mark_buffer_dirty(parent_buf); |
| 536 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
| 537 | path->nodes[level] = left_buf; |
| 538 | path->slots[level + 1] -= 1; |
| 539 | path->slots[level] = orig_slot; |
| 540 | btrfs_block_release(root, mid_buf); |
| 541 | } else { |
| 542 | orig_slot -= |
| 543 | btrfs_header_nritems(&left->header); |
| 544 | path->slots[level] = orig_slot; |
| 545 | btrfs_block_release(root, left_buf); |
| 546 | } |
| 547 | check_node(root, path, level); |
| 548 | return 0; |
| 549 | } |
| 550 | btrfs_block_release(root, left_buf); |
| 551 | } |
| 552 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
| 553 | |
| 554 | /* |
| 555 | * then try to empty the right most buffer into the middle |
| 556 | */ |
| 557 | if (right_buf) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 558 | u32 right_nr; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 559 | right = btrfs_buffer_node(right_buf); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 560 | right_nr = btrfs_header_nritems(&right->header); |
| 561 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 562 | wret = 1; |
| 563 | } else { |
| 564 | btrfs_cow_block(trans, root, right_buf, |
| 565 | parent_buf, pslot + 1, &right_buf); |
| 566 | right = btrfs_buffer_node(right_buf); |
| 567 | wret = balance_node_right(trans, root, |
| 568 | right_buf, mid_buf); |
| 569 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 570 | if (wret < 0) |
| 571 | ret = wret; |
| 572 | if (wret == 0) { |
| 573 | btrfs_memcpy(root, parent, |
| 574 | &parent->ptrs[pslot + 1].key, |
| 575 | &right->ptrs[0].key, |
| 576 | sizeof(struct btrfs_disk_key)); |
| 577 | btrfs_mark_buffer_dirty(parent_buf); |
| 578 | if (btrfs_header_nritems(&mid->header) <= orig_slot) { |
| 579 | path->nodes[level] = right_buf; |
| 580 | path->slots[level + 1] += 1; |
| 581 | path->slots[level] = orig_slot - |
| 582 | btrfs_header_nritems(&mid->header); |
| 583 | btrfs_block_release(root, mid_buf); |
| 584 | } else { |
| 585 | btrfs_block_release(root, right_buf); |
| 586 | } |
| 587 | check_node(root, path, level); |
| 588 | return 0; |
| 589 | } |
| 590 | btrfs_block_release(root, right_buf); |
| 591 | } |
| 592 | check_node(root, path, level); |
| 593 | return 1; |
| 594 | } |
| 595 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 596 | /* |
| 597 | * look for key in the tree. path is filled in with nodes along the way |
| 598 | * if key is found, we return zero and you can find the item in the leaf |
| 599 | * level of the path (level 0) |
| 600 | * |
| 601 | * 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] | 602 | * be inserted, and 1 is returned. If there are other errors during the |
| 603 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 604 | * |
| 605 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 606 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 607 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 608 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 609 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 610 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 611 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 612 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 613 | struct buffer_head *b; |
| 614 | struct buffer_head *cow_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 615 | struct btrfs_node *c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 616 | int slot; |
| 617 | int ret; |
| 618 | int level; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 619 | |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 620 | WARN_ON(p->nodes[0] != NULL); |
| 621 | WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 622 | again: |
| 623 | b = root->node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 624 | get_bh(b); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 625 | while (b) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 626 | c = btrfs_buffer_node(b); |
| 627 | level = btrfs_header_level(&c->header); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 628 | if (cow) { |
| 629 | int wret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 630 | wret = btrfs_cow_block(trans, root, b, |
| 631 | p->nodes[level + 1], |
| 632 | p->slots[level + 1], |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 633 | &cow_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 634 | b = cow_buf; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 635 | c = btrfs_buffer_node(b); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 636 | } |
| 637 | BUG_ON(!cow && ins_len); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 638 | if (level != btrfs_header_level(&c->header)) |
| 639 | WARN_ON(1); |
| 640 | level = btrfs_header_level(&c->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 641 | p->nodes[level] = b; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 642 | ret = check_block(root, p, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 643 | if (ret) |
| 644 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 645 | ret = bin_search(c, key, &slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 646 | if (!btrfs_is_leaf(c)) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 647 | if (ret && slot > 0) |
| 648 | slot -= 1; |
| 649 | p->slots[level] = slot; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 650 | if (ins_len > 0 && btrfs_header_nritems(&c->header) >= |
| 651 | BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 652 | int sret = split_node(trans, root, p, level); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 653 | BUG_ON(sret > 0); |
| 654 | if (sret) |
| 655 | return sret; |
| 656 | b = p->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 657 | c = btrfs_buffer_node(b); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 658 | slot = p->slots[level]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 659 | } else if (ins_len < 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 660 | int sret = balance_level(trans, root, p, |
| 661 | level); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 662 | if (sret) |
| 663 | return sret; |
| 664 | b = p->nodes[level]; |
| 665 | if (!b) |
| 666 | goto again; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 667 | c = btrfs_buffer_node(b); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 668 | slot = p->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 669 | BUG_ON(btrfs_header_nritems(&c->header) == 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 670 | } |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 671 | b = read_tree_block(root, btrfs_node_blockptr(c, slot)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 672 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 673 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 674 | p->slots[level] = slot; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 675 | if (ins_len > 0 && btrfs_leaf_free_space(root, l) < |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 676 | sizeof(struct btrfs_item) + ins_len) { |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 677 | int sret = split_leaf(trans, root, key, |
| 678 | p, ins_len); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 679 | BUG_ON(sret > 0); |
| 680 | if (sret) |
| 681 | return sret; |
| 682 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 683 | return ret; |
| 684 | } |
| 685 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 686 | return 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 687 | } |
| 688 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 689 | /* |
| 690 | * adjust the pointers going up the tree, starting at level |
| 691 | * making sure the right key of each node is points to 'key'. |
| 692 | * This is used after shifting pointers to the left, so it stops |
| 693 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 694 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 695 | * |
| 696 | * If this fails to write a tree block, it returns -1, but continues |
| 697 | * fixing up the blocks in ram so the tree is consistent. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 698 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 699 | static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root |
| 700 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 701 | *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 702 | { |
| 703 | int i; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 704 | int ret = 0; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 705 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 706 | struct btrfs_node *t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 707 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 708 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 709 | break; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 710 | t = btrfs_buffer_node(path->nodes[i]); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 711 | btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key)); |
| 712 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 713 | if (tslot != 0) |
| 714 | break; |
| 715 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 716 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 719 | /* |
| 720 | * 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] | 721 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 722 | * |
| 723 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 724 | * 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] | 725 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 726 | static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 727 | *root, struct buffer_head *dst_buf, struct |
| 728 | buffer_head *src_buf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 729 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 730 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 731 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 732 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 733 | int src_nritems; |
| 734 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 735 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 736 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 737 | src_nritems = btrfs_header_nritems(&src->header); |
| 738 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 739 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 740 | if (push_items <= 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 741 | return 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 742 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 743 | |
| 744 | if (src_nritems < push_items) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 745 | push_items = src_nritems; |
| 746 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 747 | btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs, |
| 748 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 749 | if (push_items < src_nritems) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 750 | btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 751 | (src_nritems - push_items) * |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 752 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 753 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 754 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 755 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 756 | btrfs_mark_buffer_dirty(src_buf); |
| 757 | btrfs_mark_buffer_dirty(dst_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 758 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 759 | } |
| 760 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 761 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 762 | * try to push data from one node into the next node right in the |
| 763 | * tree. |
| 764 | * |
| 765 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 766 | * error, and > 0 if there was no room in the right hand block. |
| 767 | * |
| 768 | * this will only push up to 1/2 the contents of the left node over |
| 769 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 770 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 771 | btrfs_root *root, struct buffer_head *dst_buf, |
| 772 | struct buffer_head *src_buf) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 773 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 774 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 775 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 776 | int push_items = 0; |
| 777 | int max_push; |
| 778 | int src_nritems; |
| 779 | int dst_nritems; |
| 780 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 781 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 782 | src_nritems = btrfs_header_nritems(&src->header); |
| 783 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 784 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 785 | if (push_items <= 0) { |
| 786 | return 1; |
| 787 | } |
| 788 | |
| 789 | max_push = src_nritems / 2 + 1; |
| 790 | /* don't try to empty the node */ |
| 791 | if (max_push > src_nritems) |
| 792 | return 1; |
| 793 | if (max_push < push_items) |
| 794 | push_items = max_push; |
| 795 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 796 | btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs, |
| 797 | dst_nritems * sizeof(struct btrfs_key_ptr)); |
| 798 | |
| 799 | btrfs_memcpy(root, dst, dst->ptrs, |
| 800 | src->ptrs + src_nritems - push_items, |
| 801 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 802 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 803 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 804 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 805 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 806 | btrfs_mark_buffer_dirty(src_buf); |
| 807 | btrfs_mark_buffer_dirty(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 812 | * helper function to insert a new root level in the tree. |
| 813 | * A new node is allocated, and a single item is inserted to |
| 814 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 815 | * |
| 816 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 817 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 818 | static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 819 | *root, struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 820 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 821 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 822 | struct btrfs_node *lower; |
| 823 | struct btrfs_node *c; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 824 | struct btrfs_disk_key *lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 825 | |
| 826 | BUG_ON(path->nodes[level]); |
| 827 | BUG_ON(path->nodes[level-1] != root->node); |
| 828 | |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 829 | t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 830 | c = btrfs_buffer_node(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 831 | memset(c, 0, root->blocksize); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 832 | btrfs_set_header_nritems(&c->header, 1); |
| 833 | btrfs_set_header_level(&c->header, level); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 834 | btrfs_set_header_blocknr(&c->header, bh_blocknr(t)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 835 | btrfs_set_header_generation(&c->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 836 | btrfs_set_header_owner(&c->header, root->root_key.objectid); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 837 | lower = btrfs_buffer_node(path->nodes[level-1]); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 838 | memcpy(c->header.fsid, root->fs_info->disk_super->fsid, |
| 839 | sizeof(c->header.fsid)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 840 | if (btrfs_is_leaf(lower)) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 841 | lower_key = &((struct btrfs_leaf *)lower)->items[0].key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 842 | else |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 843 | lower_key = &lower->ptrs[0].key; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 844 | btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key, |
| 845 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 846 | btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1])); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 847 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 848 | btrfs_mark_buffer_dirty(t); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 849 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 850 | /* the super has an extra ref to root->node */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 851 | btrfs_block_release(root, root->node); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 852 | root->node = t; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 853 | get_bh(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 854 | path->nodes[level] = t; |
| 855 | path->slots[level] = 0; |
| 856 | return 0; |
| 857 | } |
| 858 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 859 | /* |
| 860 | * worker function to insert a single pointer in a node. |
| 861 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 862 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 863 | * slot and level indicate where you want the key to go, and |
| 864 | * blocknr is the block the key points to. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 865 | * |
| 866 | * returns zero on success and < 0 on any error |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 867 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 868 | static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root |
| 869 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 870 | *key, u64 blocknr, int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 871 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 872 | struct btrfs_node *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 873 | int nritems; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 874 | |
| 875 | BUG_ON(!path->nodes[level]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 876 | lower = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 877 | nritems = btrfs_header_nritems(&lower->header); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 878 | if (slot > nritems) |
| 879 | BUG(); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 880 | if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 881 | BUG(); |
| 882 | if (slot != nritems) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 883 | btrfs_memmove(root, lower, lower->ptrs + slot + 1, |
| 884 | lower->ptrs + slot, |
| 885 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 886 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 887 | btrfs_memcpy(root, lower, &lower->ptrs[slot].key, |
| 888 | key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 889 | btrfs_set_node_blockptr(lower, slot, blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 890 | btrfs_set_header_nritems(&lower->header, nritems + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 891 | btrfs_mark_buffer_dirty(path->nodes[level]); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 892 | check_node(root, path, level); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 896 | /* |
| 897 | * split the node at the specified level in path in two. |
| 898 | * The path is corrected to point to the appropriate node after the split |
| 899 | * |
| 900 | * Before splitting this tries to make some room in the node by pushing |
| 901 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 902 | * |
| 903 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 904 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 905 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 906 | *root, struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 907 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 908 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 909 | struct btrfs_node *c; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 910 | struct buffer_head *split_buffer; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 911 | struct btrfs_node *split; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 912 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 913 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 914 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 915 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 916 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 917 | t = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 918 | c = btrfs_buffer_node(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 919 | if (t == root->node) { |
| 920 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 921 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 922 | if (ret) |
| 923 | return ret; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 924 | } else { |
| 925 | ret = push_nodes_for_insert(trans, root, path, level); |
| 926 | t = path->nodes[level]; |
| 927 | c = btrfs_buffer_node(t); |
| 928 | if (!ret && |
| 929 | btrfs_header_nritems(&c->header) < |
| 930 | BTRFS_NODEPTRS_PER_BLOCK(root) - 1) |
| 931 | return 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 932 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 933 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 934 | c_nritems = btrfs_header_nritems(&c->header); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 935 | split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 936 | split = btrfs_buffer_node(split_buffer); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 937 | btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); |
Chris Mason | 9a6f11e | 2007-03-27 09:06:38 -0400 | [diff] [blame] | 938 | btrfs_set_header_level(&split->header, btrfs_header_level(&c->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 939 | btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 940 | btrfs_set_header_generation(&split->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 941 | btrfs_set_header_owner(&split->header, root->root_key.objectid); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 942 | memcpy(split->header.fsid, root->fs_info->disk_super->fsid, |
| 943 | sizeof(split->header.fsid)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 944 | mid = (c_nritems + 1) / 2; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 945 | btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid, |
| 946 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 947 | btrfs_set_header_nritems(&split->header, c_nritems - mid); |
| 948 | btrfs_set_header_nritems(&c->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 949 | ret = 0; |
| 950 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 951 | btrfs_mark_buffer_dirty(t); |
| 952 | btrfs_mark_buffer_dirty(split_buffer); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 953 | wret = insert_ptr(trans, root, path, &split->ptrs[0].key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 954 | bh_blocknr(split_buffer), path->slots[level + 1] + 1, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 955 | level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 956 | if (wret) |
| 957 | ret = wret; |
| 958 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 959 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 960 | path->slots[level] -= mid; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 961 | btrfs_block_release(root, t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 962 | path->nodes[level] = split_buffer; |
| 963 | path->slots[level + 1] += 1; |
| 964 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 965 | btrfs_block_release(root, split_buffer); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 966 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 967 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 968 | } |
| 969 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 970 | /* |
| 971 | * how many bytes are required to store the items in a leaf. start |
| 972 | * and nr indicate which items in the leaf to check. This totals up the |
| 973 | * space used both by the item structs and the item data |
| 974 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 975 | static int leaf_space_used(struct btrfs_leaf *l, int start, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 976 | { |
| 977 | int data_len; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 978 | int nritems = btrfs_header_nritems(&l->header); |
| 979 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 980 | |
| 981 | if (!nr) |
| 982 | return 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 983 | data_len = btrfs_item_end(l->items + start); |
| 984 | data_len = data_len - btrfs_item_offset(l->items + end); |
| 985 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 986 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 987 | return data_len; |
| 988 | } |
| 989 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 990 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 991 | * The space between the end of the leaf items and |
| 992 | * the start of the leaf data. IOW, how much room |
| 993 | * the leaf has left for both items and data |
| 994 | */ |
| 995 | int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) |
| 996 | { |
| 997 | int nritems = btrfs_header_nritems(&leaf->header); |
| 998 | return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 999 | } |
| 1000 | |
| 1001 | /* |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1002 | * push some data in the path leaf to the right, trying to free up at |
| 1003 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1004 | * |
| 1005 | * returns 1 if the push failed because the other node didn't have enough |
| 1006 | * 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] | 1007 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1008 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1009 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1010 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1011 | struct buffer_head *left_buf = path->nodes[0]; |
| 1012 | struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1013 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1014 | struct buffer_head *right_buf; |
| 1015 | struct buffer_head *upper; |
| 1016 | struct btrfs_node *upper_node; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1017 | int slot; |
| 1018 | int i; |
| 1019 | int free_space; |
| 1020 | int push_space = 0; |
| 1021 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1022 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1023 | u32 left_nritems; |
| 1024 | u32 right_nritems; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1025 | |
| 1026 | slot = path->slots[1]; |
| 1027 | if (!path->nodes[1]) { |
| 1028 | return 1; |
| 1029 | } |
| 1030 | upper = path->nodes[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1031 | upper_node = btrfs_buffer_node(upper); |
| 1032 | if (slot >= btrfs_header_nritems(&upper_node->header) - 1) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1033 | return 1; |
| 1034 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1035 | right_buf = read_tree_block(root, |
| 1036 | btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1)); |
| 1037 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1038 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1039 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1040 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1041 | return 1; |
| 1042 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1043 | /* cow and double check */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1044 | btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1045 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1046 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1047 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1048 | btrfs_block_release(root, right_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1049 | return 1; |
| 1050 | } |
| 1051 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1052 | left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1053 | if (left_nritems == 0) { |
| 1054 | btrfs_block_release(root, right_buf); |
| 1055 | return 1; |
| 1056 | } |
| 1057 | for (i = left_nritems - 1; i >= 1; i--) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1058 | item = left->items + i; |
| 1059 | if (path->slots[0] == i) |
| 1060 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1061 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 1062 | free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1063 | break; |
| 1064 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1065 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1066 | } |
| 1067 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1068 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1069 | return 1; |
| 1070 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1071 | if (push_items == left_nritems) |
| 1072 | WARN_ON(1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1073 | right_nritems = btrfs_header_nritems(&right->header); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1074 | /* push left to right */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1075 | push_space = btrfs_item_end(left->items + left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1076 | push_space -= leaf_data_end(root, left); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1077 | /* make room in the right data area */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1078 | btrfs_memmove(root, right, btrfs_leaf_data(right) + |
| 1079 | leaf_data_end(root, right) - push_space, |
| 1080 | btrfs_leaf_data(right) + |
| 1081 | leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) - |
| 1082 | leaf_data_end(root, right)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1083 | /* copy from the left data area */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1084 | btrfs_memcpy(root, right, btrfs_leaf_data(right) + |
| 1085 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 1086 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 1087 | push_space); |
| 1088 | btrfs_memmove(root, right, right->items + push_items, right->items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1089 | right_nritems * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1090 | /* copy the items from left to right */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1091 | btrfs_memcpy(root, right, right->items, left->items + |
| 1092 | left_nritems - push_items, |
| 1093 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1094 | |
| 1095 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1096 | right_nritems += push_items; |
| 1097 | btrfs_set_header_nritems(&right->header, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1098 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1099 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1100 | btrfs_set_item_offset(right->items + i, push_space - |
| 1101 | btrfs_item_size(right->items + i)); |
| 1102 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1103 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1104 | left_nritems -= push_items; |
| 1105 | btrfs_set_header_nritems(&left->header, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1106 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1107 | btrfs_mark_buffer_dirty(left_buf); |
| 1108 | btrfs_mark_buffer_dirty(right_buf); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1109 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1110 | btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1111 | &right->items[0].key, sizeof(struct btrfs_disk_key)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1112 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1113 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1114 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1115 | if (path->slots[0] >= left_nritems) { |
| 1116 | path->slots[0] -= left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1117 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1118 | path->nodes[0] = right_buf; |
| 1119 | path->slots[1] += 1; |
| 1120 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1121 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1122 | } |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1123 | if (path->nodes[1]) |
| 1124 | check_node(root, path, 1); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1128 | * push some data in the path leaf to the left, trying to free up at |
| 1129 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 1130 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1131 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1132 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1133 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1134 | struct buffer_head *right_buf = path->nodes[0]; |
| 1135 | struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf); |
| 1136 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1137 | struct btrfs_leaf *left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1138 | int slot; |
| 1139 | int i; |
| 1140 | int free_space; |
| 1141 | int push_space = 0; |
| 1142 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1143 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1144 | u32 old_left_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1145 | int ret = 0; |
| 1146 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1147 | |
| 1148 | slot = path->slots[1]; |
| 1149 | if (slot == 0) { |
| 1150 | return 1; |
| 1151 | } |
| 1152 | if (!path->nodes[1]) { |
| 1153 | return 1; |
| 1154 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1155 | t = read_tree_block(root, |
| 1156 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1)); |
| 1157 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1158 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1159 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1160 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1161 | return 1; |
| 1162 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1163 | |
| 1164 | /* cow and double check */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1165 | btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1166 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1167 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1168 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1169 | btrfs_block_release(root, t); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1170 | return 1; |
| 1171 | } |
| 1172 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1173 | if (btrfs_header_nritems(&right->header) == 0) { |
| 1174 | btrfs_block_release(root, t); |
| 1175 | return 1; |
| 1176 | } |
| 1177 | |
| 1178 | for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1179 | item = right->items + i; |
| 1180 | if (path->slots[0] == i) |
| 1181 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1182 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 1183 | free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1184 | break; |
| 1185 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1186 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1187 | } |
| 1188 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1189 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1190 | return 1; |
| 1191 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1192 | if (push_items == btrfs_header_nritems(&right->header)) |
| 1193 | WARN_ON(1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1194 | /* push data from right to left */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1195 | btrfs_memcpy(root, left, left->items + |
| 1196 | btrfs_header_nritems(&left->header), |
| 1197 | right->items, push_items * sizeof(struct btrfs_item)); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1198 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1199 | btrfs_item_offset(right->items + push_items -1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1200 | btrfs_memcpy(root, left, btrfs_leaf_data(left) + |
| 1201 | leaf_data_end(root, left) - push_space, |
| 1202 | btrfs_leaf_data(right) + |
| 1203 | btrfs_item_offset(right->items + push_items - 1), |
| 1204 | push_space); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1205 | old_left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1206 | BUG_ON(old_left_nritems < 0); |
| 1207 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1208 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1209 | u32 ioff = btrfs_item_offset(left->items + i); |
| 1210 | btrfs_set_item_offset(left->items + i, ioff - |
| 1211 | (BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1212 | btrfs_item_offset(left->items + |
| 1213 | old_left_nritems - 1))); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1214 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1215 | btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1216 | |
| 1217 | /* fixup right node */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1218 | push_space = btrfs_item_offset(right->items + push_items - 1) - |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1219 | leaf_data_end(root, right); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1220 | btrfs_memmove(root, right, btrfs_leaf_data(right) + |
| 1221 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 1222 | btrfs_leaf_data(right) + |
| 1223 | leaf_data_end(root, right), push_space); |
| 1224 | btrfs_memmove(root, right, right->items, right->items + push_items, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1225 | (btrfs_header_nritems(&right->header) - push_items) * |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1226 | sizeof(struct btrfs_item)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1227 | btrfs_set_header_nritems(&right->header, |
| 1228 | btrfs_header_nritems(&right->header) - |
| 1229 | push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1230 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1231 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1232 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1233 | btrfs_set_item_offset(right->items + i, push_space - |
| 1234 | btrfs_item_size(right->items + i)); |
| 1235 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1236 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1237 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1238 | btrfs_mark_buffer_dirty(t); |
| 1239 | btrfs_mark_buffer_dirty(right_buf); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1240 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1241 | wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1242 | if (wret) |
| 1243 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1244 | |
| 1245 | /* then fixup the leaf pointer in the path */ |
| 1246 | if (path->slots[0] < push_items) { |
| 1247 | path->slots[0] += old_left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1248 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1249 | path->nodes[0] = t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1250 | path->slots[1] -= 1; |
| 1251 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1252 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1253 | path->slots[0] -= push_items; |
| 1254 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1255 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1256 | if (path->nodes[1]) |
| 1257 | check_node(root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1258 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1259 | } |
| 1260 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1261 | /* |
| 1262 | * split the path's leaf in two, making sure there is at least data_size |
| 1263 | * available for the resulting leaf level of the path. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1264 | * |
| 1265 | * returns 0 if all went well and < 0 on failure. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1266 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1267 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1268 | *root, struct btrfs_key *ins_key, |
| 1269 | struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1270 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1271 | struct buffer_head *l_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1272 | struct btrfs_leaf *l; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1273 | u32 nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1274 | int mid; |
| 1275 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1276 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1277 | struct buffer_head *right_buffer; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1278 | int space_needed = data_size + sizeof(struct btrfs_item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1279 | int data_copy_size; |
| 1280 | int rt_data_off; |
| 1281 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1282 | int ret = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1283 | int wret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1284 | int double_split = 0; |
| 1285 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1286 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1287 | /* first try to make some room by pushing left and right */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1288 | wret = push_leaf_left(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1289 | if (wret < 0) |
| 1290 | return wret; |
| 1291 | if (wret) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1292 | wret = push_leaf_right(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1293 | if (wret < 0) |
| 1294 | return wret; |
| 1295 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1296 | l_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1297 | l = btrfs_buffer_leaf(l_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1298 | |
| 1299 | /* did the pushes work? */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1300 | if (btrfs_leaf_free_space(root, l) >= |
| 1301 | sizeof(struct btrfs_item) + data_size) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1302 | return 0; |
| 1303 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1304 | if (!path->nodes[1]) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1305 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1306 | if (ret) |
| 1307 | return ret; |
| 1308 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1309 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1310 | nritems = btrfs_header_nritems(&l->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1311 | mid = (nritems + 1)/ 2; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1312 | right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1313 | BUG_ON(!right_buffer); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1314 | right = btrfs_buffer_leaf(right_buffer); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1315 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1316 | btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 1317 | btrfs_set_header_generation(&right->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1318 | btrfs_set_header_owner(&right->header, root->root_key.objectid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1319 | btrfs_set_header_level(&right->header, 0); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1320 | memcpy(right->header.fsid, root->fs_info->disk_super->fsid, |
| 1321 | sizeof(right->header.fsid)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1322 | if (mid <= slot) { |
| 1323 | if (nritems == 1 || |
| 1324 | leaf_space_used(l, mid, nritems - mid) + space_needed > |
| 1325 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 1326 | if (slot >= nritems) { |
| 1327 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1328 | btrfs_set_header_nritems(&right->header, 0); |
| 1329 | wret = insert_ptr(trans, root, path, |
| 1330 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1331 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1332 | path->slots[1] + 1, 1); |
| 1333 | if (wret) |
| 1334 | ret = wret; |
| 1335 | btrfs_block_release(root, path->nodes[0]); |
| 1336 | path->nodes[0] = right_buffer; |
| 1337 | path->slots[0] = 0; |
| 1338 | path->slots[1] += 1; |
| 1339 | return ret; |
| 1340 | } |
| 1341 | mid = slot; |
| 1342 | double_split = 1; |
| 1343 | } |
| 1344 | } else { |
| 1345 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
| 1346 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 1347 | if (slot == 0) { |
| 1348 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1349 | btrfs_set_header_nritems(&right->header, 0); |
| 1350 | wret = insert_ptr(trans, root, path, |
| 1351 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1352 | bh_blocknr(right_buffer), |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1353 | path->slots[1], 1); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1354 | if (wret) |
| 1355 | ret = wret; |
| 1356 | btrfs_block_release(root, path->nodes[0]); |
| 1357 | path->nodes[0] = right_buffer; |
| 1358 | path->slots[0] = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1359 | if (path->slots[1] == 0) { |
| 1360 | wret = fixup_low_keys(trans, root, |
| 1361 | path, &disk_key, 1); |
| 1362 | if (wret) |
| 1363 | ret = wret; |
| 1364 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1365 | return ret; |
| 1366 | } |
| 1367 | mid = slot; |
| 1368 | double_split = 1; |
| 1369 | } |
| 1370 | } |
| 1371 | btrfs_set_header_nritems(&right->header, nritems - mid); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1372 | data_copy_size = btrfs_item_end(l->items + mid) - |
| 1373 | leaf_data_end(root, l); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1374 | btrfs_memcpy(root, right, right->items, l->items + mid, |
| 1375 | (nritems - mid) * sizeof(struct btrfs_item)); |
| 1376 | btrfs_memcpy(root, right, |
| 1377 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 1378 | data_copy_size, btrfs_leaf_data(l) + |
| 1379 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1380 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 1381 | btrfs_item_end(l->items + mid); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1382 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1383 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1384 | u32 ioff = btrfs_item_offset(right->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1385 | btrfs_set_item_offset(right->items + i, ioff + rt_data_off); |
| 1386 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1387 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1388 | btrfs_set_header_nritems(&l->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1389 | ret = 0; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1390 | wret = insert_ptr(trans, root, path, &right->items[0].key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1391 | bh_blocknr(right_buffer), path->slots[1] + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1392 | if (wret) |
| 1393 | ret = wret; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1394 | btrfs_mark_buffer_dirty(right_buffer); |
| 1395 | btrfs_mark_buffer_dirty(l_buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1396 | BUG_ON(path->slots[0] != slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1397 | if (mid <= slot) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1398 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1399 | path->nodes[0] = right_buffer; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1400 | path->slots[0] -= mid; |
| 1401 | path->slots[1] += 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1402 | } else |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1403 | btrfs_block_release(root, right_buffer); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1404 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 1405 | check_node(root, path, 1); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1406 | |
| 1407 | if (!double_split) |
| 1408 | return ret; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1409 | right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1410 | BUG_ON(!right_buffer); |
| 1411 | right = btrfs_buffer_leaf(right_buffer); |
| 1412 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1413 | btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1414 | btrfs_set_header_generation(&right->header, trans->transid); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1415 | btrfs_set_header_owner(&right->header, root->root_key.objectid); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1416 | btrfs_set_header_level(&right->header, 0); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1417 | memcpy(right->header.fsid, root->fs_info->disk_super->fsid, |
| 1418 | sizeof(right->header.fsid)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1419 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1420 | btrfs_set_header_nritems(&right->header, 0); |
| 1421 | wret = insert_ptr(trans, root, path, |
| 1422 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1423 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1424 | path->slots[1], 1); |
| 1425 | if (wret) |
| 1426 | ret = wret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1427 | if (path->slots[1] == 0) { |
| 1428 | wret = fixup_low_keys(trans, root, path, &disk_key, 1); |
| 1429 | if (wret) |
| 1430 | ret = wret; |
| 1431 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1432 | btrfs_block_release(root, path->nodes[0]); |
| 1433 | path->nodes[0] = right_buffer; |
| 1434 | path->slots[0] = 0; |
| 1435 | check_node(root, path, 1); |
| 1436 | check_leaf(root, path, 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1437 | return ret; |
| 1438 | } |
| 1439 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1440 | int btrfs_truncate_item(struct btrfs_trans_handle *trans, |
| 1441 | struct btrfs_root *root, |
| 1442 | struct btrfs_path *path, |
| 1443 | u32 new_size) |
| 1444 | { |
| 1445 | int ret = 0; |
| 1446 | int slot; |
| 1447 | int slot_orig; |
| 1448 | struct btrfs_leaf *leaf; |
| 1449 | struct buffer_head *leaf_buf; |
| 1450 | u32 nritems; |
| 1451 | unsigned int data_end; |
| 1452 | unsigned int old_data_start; |
| 1453 | unsigned int old_size; |
| 1454 | unsigned int size_diff; |
| 1455 | int i; |
| 1456 | |
| 1457 | slot_orig = path->slots[0]; |
| 1458 | leaf_buf = path->nodes[0]; |
| 1459 | leaf = btrfs_buffer_leaf(leaf_buf); |
| 1460 | |
| 1461 | nritems = btrfs_header_nritems(&leaf->header); |
| 1462 | data_end = leaf_data_end(root, leaf); |
| 1463 | |
| 1464 | slot = path->slots[0]; |
| 1465 | old_data_start = btrfs_item_offset(leaf->items + slot); |
| 1466 | old_size = btrfs_item_size(leaf->items + slot); |
| 1467 | BUG_ON(old_size <= new_size); |
| 1468 | size_diff = old_size - new_size; |
| 1469 | |
| 1470 | BUG_ON(slot < 0); |
| 1471 | BUG_ON(slot >= nritems); |
| 1472 | |
| 1473 | /* |
| 1474 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1475 | */ |
| 1476 | /* first correct the data pointers */ |
| 1477 | for (i = slot; i < nritems; i++) { |
| 1478 | u32 ioff = btrfs_item_offset(leaf->items + i); |
| 1479 | btrfs_set_item_offset(leaf->items + i, |
| 1480 | ioff + size_diff); |
| 1481 | } |
| 1482 | /* shift the data */ |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1483 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1484 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 1485 | data_end, old_data_start + new_size - data_end); |
| 1486 | btrfs_set_item_size(leaf->items + slot, new_size); |
| 1487 | btrfs_mark_buffer_dirty(leaf_buf); |
| 1488 | |
| 1489 | ret = 0; |
| 1490 | if (btrfs_leaf_free_space(root, leaf) < 0) |
| 1491 | BUG(); |
| 1492 | check_leaf(root, path, 0); |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1496 | int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1497 | *root, struct btrfs_path *path, u32 data_size) |
| 1498 | { |
| 1499 | int ret = 0; |
| 1500 | int slot; |
| 1501 | int slot_orig; |
| 1502 | struct btrfs_leaf *leaf; |
| 1503 | struct buffer_head *leaf_buf; |
| 1504 | u32 nritems; |
| 1505 | unsigned int data_end; |
| 1506 | unsigned int old_data; |
| 1507 | unsigned int old_size; |
| 1508 | int i; |
| 1509 | |
| 1510 | slot_orig = path->slots[0]; |
| 1511 | leaf_buf = path->nodes[0]; |
| 1512 | leaf = btrfs_buffer_leaf(leaf_buf); |
| 1513 | |
| 1514 | nritems = btrfs_header_nritems(&leaf->header); |
| 1515 | data_end = leaf_data_end(root, leaf); |
| 1516 | |
| 1517 | if (btrfs_leaf_free_space(root, leaf) < data_size) |
| 1518 | BUG(); |
| 1519 | slot = path->slots[0]; |
| 1520 | old_data = btrfs_item_end(leaf->items + slot); |
| 1521 | |
| 1522 | BUG_ON(slot < 0); |
| 1523 | BUG_ON(slot >= nritems); |
| 1524 | |
| 1525 | /* |
| 1526 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1527 | */ |
| 1528 | /* first correct the data pointers */ |
| 1529 | for (i = slot; i < nritems; i++) { |
| 1530 | u32 ioff = btrfs_item_offset(leaf->items + i); |
| 1531 | btrfs_set_item_offset(leaf->items + i, |
| 1532 | ioff - data_size); |
| 1533 | } |
| 1534 | /* shift the data */ |
| 1535 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1536 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 1537 | data_end, old_data - data_end); |
| 1538 | data_end = old_data; |
| 1539 | old_size = btrfs_item_size(leaf->items + slot); |
| 1540 | btrfs_set_item_size(leaf->items + slot, old_size + data_size); |
| 1541 | btrfs_mark_buffer_dirty(leaf_buf); |
| 1542 | |
| 1543 | ret = 0; |
| 1544 | if (btrfs_leaf_free_space(root, leaf) < 0) |
| 1545 | BUG(); |
| 1546 | check_leaf(root, path, 0); |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1550 | /* |
| 1551 | * Given a key and some data, insert an item into the tree. |
| 1552 | * This does all the path init required, making room in the tree if needed. |
| 1553 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1554 | int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1555 | *root, struct btrfs_path *path, struct btrfs_key |
| 1556 | *cpu_key, u32 data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1557 | { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1558 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1559 | int slot; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1560 | int slot_orig; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1561 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1562 | struct buffer_head *leaf_buf; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1563 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1564 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1565 | struct btrfs_disk_key disk_key; |
| 1566 | |
| 1567 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1568 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1569 | /* create a root if there isn't one */ |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1570 | if (!root->node) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1571 | BUG(); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1572 | ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1573 | if (ret == 0) { |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1574 | return -EEXIST; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1575 | } |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1576 | if (ret < 0) |
| 1577 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1578 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1579 | slot_orig = path->slots[0]; |
| 1580 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1581 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1582 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1583 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1584 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1585 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1586 | if (btrfs_leaf_free_space(root, leaf) < |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1587 | sizeof(struct btrfs_item) + data_size) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1588 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1589 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1590 | slot = path->slots[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1591 | BUG_ON(slot < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1592 | if (slot != nritems) { |
| 1593 | int i; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1594 | unsigned int old_data = btrfs_item_end(leaf->items + slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1595 | |
| 1596 | /* |
| 1597 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1598 | */ |
| 1599 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1600 | for (i = slot; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1601 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1602 | btrfs_set_item_offset(leaf->items + i, |
| 1603 | ioff - data_size); |
| 1604 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1605 | |
| 1606 | /* shift the items */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1607 | btrfs_memmove(root, leaf, leaf->items + slot + 1, |
| 1608 | leaf->items + slot, |
| 1609 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1610 | |
| 1611 | /* shift the data */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1612 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1613 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 1614 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1615 | data_end = old_data; |
| 1616 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1617 | /* setup the item for the new data */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1618 | btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key, |
| 1619 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1620 | btrfs_set_item_offset(leaf->items + slot, data_end - data_size); |
| 1621 | btrfs_set_item_size(leaf->items + slot, data_size); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1622 | btrfs_set_header_nritems(&leaf->header, nritems + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1623 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1624 | |
| 1625 | ret = 0; |
Chris Mason | 8e19f2c | 2007-02-28 09:27:02 -0500 | [diff] [blame] | 1626 | if (slot == 0) |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1627 | ret = fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1628 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1629 | if (btrfs_leaf_free_space(root, leaf) < 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1630 | BUG(); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1631 | check_leaf(root, path, 0); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1632 | out: |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1633 | return ret; |
| 1634 | } |
| 1635 | |
| 1636 | /* |
| 1637 | * Given a key and some data, insert an item into the tree. |
| 1638 | * This does all the path init required, making room in the tree if needed. |
| 1639 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1640 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1641 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 1642 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1643 | { |
| 1644 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1645 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1646 | u8 *ptr; |
| 1647 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1648 | path = btrfs_alloc_path(); |
| 1649 | BUG_ON(!path); |
| 1650 | btrfs_init_path(path); |
| 1651 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1652 | if (!ret) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1653 | ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1654 | path->slots[0], u8); |
| 1655 | btrfs_memcpy(root, path->nodes[0]->b_data, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1656 | ptr, data, data_size); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1657 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1658 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1659 | btrfs_release_path(root, path); |
| 1660 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1661 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1662 | } |
| 1663 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1664 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1665 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1666 | * |
| 1667 | * If the delete empties a node, the node is removed from the tree, |
| 1668 | * continuing all the way the root if required. The root is converted into |
| 1669 | * a leaf if all the nodes are emptied. |
| 1670 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1671 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1672 | struct btrfs_path *path, int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1673 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1674 | struct btrfs_node *node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1675 | struct buffer_head *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1676 | u32 nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1677 | int ret = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1678 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1679 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1680 | node = btrfs_buffer_node(parent); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1681 | nritems = btrfs_header_nritems(&node->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1682 | if (slot != nritems -1) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1683 | btrfs_memmove(root, node, node->ptrs + slot, |
| 1684 | node->ptrs + slot + 1, |
| 1685 | sizeof(struct btrfs_key_ptr) * |
| 1686 | (nritems - slot - 1)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1687 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1688 | nritems--; |
| 1689 | btrfs_set_header_nritems(&node->header, nritems); |
| 1690 | if (nritems == 0 && parent == root->node) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1691 | struct btrfs_header *header = btrfs_buffer_header(root->node); |
| 1692 | BUG_ON(btrfs_header_level(header) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1693 | /* just turn the root into a leaf and break */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1694 | btrfs_set_header_level(header, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1695 | } else if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1696 | wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1697 | level + 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1698 | if (wret) |
| 1699 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1700 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1701 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1702 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1703 | } |
| 1704 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1705 | /* |
| 1706 | * delete the item at the leaf level in path. If that empties |
| 1707 | * the leaf, remove it from the tree |
| 1708 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1709 | int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1710 | struct btrfs_path *path) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1711 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1712 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1713 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1714 | struct buffer_head *leaf_buf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1715 | int doff; |
| 1716 | int dsize; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1717 | int ret = 0; |
| 1718 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1719 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1720 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1721 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1722 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1723 | slot = path->slots[0]; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1724 | doff = btrfs_item_offset(leaf->items + slot); |
| 1725 | dsize = btrfs_item_size(leaf->items + slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1726 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1727 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1728 | if (slot != nritems - 1) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1729 | int i; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1730 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1731 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1732 | data_end + dsize, |
| 1733 | btrfs_leaf_data(leaf) + data_end, |
| 1734 | doff - data_end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1735 | for (i = slot + 1; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1736 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1737 | btrfs_set_item_offset(leaf->items + i, ioff + dsize); |
| 1738 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1739 | btrfs_memmove(root, leaf, leaf->items + slot, |
| 1740 | leaf->items + slot + 1, |
| 1741 | sizeof(struct btrfs_item) * |
| 1742 | (nritems - slot - 1)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1743 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1744 | btrfs_set_header_nritems(&leaf->header, nritems - 1); |
| 1745 | nritems--; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1746 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1747 | if (nritems == 0) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1748 | if (leaf_buf == root->node) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1749 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1750 | } else { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1751 | clean_tree_block(trans, root, leaf_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1752 | wait_on_buffer(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1753 | wret = del_ptr(trans, root, path, 1, path->slots[1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1754 | if (wret) |
| 1755 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1756 | wret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1757 | bh_blocknr(leaf_buf), 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1758 | if (wret) |
| 1759 | ret = wret; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1760 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1761 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1762 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1763 | if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1764 | wret = fixup_low_keys(trans, root, path, |
| 1765 | &leaf->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1766 | if (wret) |
| 1767 | ret = wret; |
| 1768 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1769 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1770 | /* delete the leaf if it is mostly empty */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1771 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1772 | /* push_leaf_left fixes the path. |
| 1773 | * make sure the path still points to our leaf |
| 1774 | * for possible call to del_ptr below |
| 1775 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1776 | slot = path->slots[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1777 | get_bh(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1778 | wret = push_leaf_left(trans, root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1779 | if (wret < 0) |
| 1780 | ret = wret; |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1781 | if (path->nodes[0] == leaf_buf && |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1782 | btrfs_header_nritems(&leaf->header)) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1783 | wret = push_leaf_right(trans, root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1784 | if (wret < 0) |
| 1785 | ret = wret; |
| 1786 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1787 | if (btrfs_header_nritems(&leaf->header) == 0) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1788 | u64 blocknr = bh_blocknr(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1789 | clean_tree_block(trans, root, leaf_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1790 | wait_on_buffer(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1791 | wret = del_ptr(trans, root, path, 1, slot); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1792 | if (wret) |
| 1793 | ret = wret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1794 | btrfs_block_release(root, leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1795 | wret = btrfs_free_extent(trans, root, blocknr, |
| 1796 | 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1797 | if (wret) |
| 1798 | ret = wret; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1799 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1800 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1801 | btrfs_block_release(root, leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1802 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1803 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1804 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1805 | } |
| 1806 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1807 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1808 | } |
| 1809 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1810 | /* |
| 1811 | * 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] | 1812 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 1813 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1814 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1815 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1816 | { |
| 1817 | int slot; |
| 1818 | int level = 1; |
| 1819 | u64 blocknr; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1820 | struct buffer_head *c; |
| 1821 | struct btrfs_node *c_node; |
| 1822 | struct buffer_head *next = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1823 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1824 | while(level < BTRFS_MAX_LEVEL) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1825 | if (!path->nodes[level]) |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1826 | return 1; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1827 | slot = path->slots[level] + 1; |
| 1828 | c = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1829 | c_node = btrfs_buffer_node(c); |
| 1830 | if (slot >= btrfs_header_nritems(&c_node->header)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1831 | level++; |
| 1832 | continue; |
| 1833 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1834 | blocknr = btrfs_node_blockptr(c_node, slot); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1835 | if (next) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1836 | btrfs_block_release(root, next); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1837 | next = read_tree_block(root, blocknr); |
| 1838 | break; |
| 1839 | } |
| 1840 | path->slots[level] = slot; |
| 1841 | while(1) { |
| 1842 | level--; |
| 1843 | c = path->nodes[level]; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1844 | btrfs_block_release(root, c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1845 | path->nodes[level] = next; |
| 1846 | path->slots[level] = 0; |
| 1847 | if (!level) |
| 1848 | break; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1849 | next = read_tree_block(root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1850 | btrfs_node_blockptr(btrfs_buffer_node(next), 0)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1851 | } |
| 1852 | return 0; |
| 1853 | } |