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