Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2 | #include "ctree.h" |
| 3 | #include "disk-io.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 5 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 6 | *root, struct btrfs_path *path, int level); |
| 7 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
| 8 | *root, struct btrfs_path *path, int data_size); |
| 9 | 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] | 10 | *root, struct buffer_head *dst, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 11 | *src); |
| 12 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 13 | btrfs_root *root, struct buffer_head *dst_buf, |
| 14 | struct buffer_head *src_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 15 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 16 | struct btrfs_path *path, int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 17 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 18 | inline void btrfs_init_path(struct btrfs_path *p) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 19 | { |
| 20 | memset(p, 0, sizeof(*p)); |
| 21 | } |
| 22 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 23 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 24 | { |
| 25 | int i; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 26 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 27 | if (!p->nodes[i]) |
| 28 | break; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 29 | btrfs_block_release(root, p->nodes[i]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 30 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 31 | memset(p, 0, sizeof(*p)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 34 | 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] | 35 | *root, struct buffer_head *buf, struct buffer_head |
| 36 | *parent, int parent_slot, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 37 | **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 38 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 39 | struct buffer_head *cow; |
| 40 | struct btrfs_node *cow_node; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 41 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 42 | if (buffer_dirty(buf)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 43 | *cow_ret = buf; |
| 44 | return 0; |
| 45 | } |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 46 | cow = btrfs_alloc_free_block(trans, root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 47 | cow_node = btrfs_buffer_node(cow); |
| 48 | memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize); |
| 49 | btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 50 | *cow_ret = cow; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 51 | mark_buffer_dirty(cow); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 52 | btrfs_inc_ref(trans, root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 53 | if (buf == root->node) { |
| 54 | root->node = cow; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 55 | get_bh(cow); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 56 | if (buf != root->commit_root) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 57 | btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 58 | btrfs_block_release(root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 59 | } else { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 60 | btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot, |
| 61 | cow->b_blocknr); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 62 | mark_buffer_dirty(parent); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 63 | btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 64 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 65 | btrfs_block_release(root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 69 | /* |
| 70 | * The leaf data grows from end-to-front in the node. |
| 71 | * this returns the address of the start of the last item, |
| 72 | * which is the stop of the leaf data stack |
| 73 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 74 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
| 75 | struct btrfs_leaf *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 76 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 77 | u32 nr = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 78 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 79 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 80 | return btrfs_item_offset(leaf->items + nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 83 | /* |
| 84 | * The space between the end of the leaf items and |
| 85 | * the start of the leaf data. IOW, how much room |
| 86 | * the leaf has left for both items and data |
| 87 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 88 | int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 89 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 90 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 91 | int nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 92 | char *items_end = (char *)(leaf->items + nritems + 1); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 93 | return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 96 | /* |
| 97 | * compare two keys in a memcmp fashion |
| 98 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 99 | 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] | 100 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 101 | struct btrfs_key k1; |
| 102 | |
| 103 | btrfs_disk_key_to_cpu(&k1, disk); |
| 104 | |
| 105 | if (k1.objectid > k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 106 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 107 | if (k1.objectid < k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 108 | return -1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 109 | if (k1.flags > k2->flags) |
| 110 | return 1; |
| 111 | if (k1.flags < k2->flags) |
| 112 | return -1; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 113 | if (k1.offset > k2->offset) |
| 114 | return 1; |
| 115 | if (k1.offset < k2->offset) |
| 116 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 117 | return 0; |
| 118 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 119 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 120 | static int check_node(struct btrfs_root *root, struct btrfs_path *path, |
| 121 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 122 | { |
| 123 | int i; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 124 | struct btrfs_node *parent = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 125 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 126 | int parent_slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 127 | u32 nritems = btrfs_header_nritems(&node->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 128 | |
| 129 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 130 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 131 | parent_slot = path->slots[level + 1]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 132 | BUG_ON(nritems == 0); |
| 133 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 134 | struct btrfs_disk_key *parent_key; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 135 | parent_key = &parent->ptrs[parent_slot].key; |
| 136 | BUG_ON(memcmp(parent_key, &node->ptrs[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 137 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 138 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 139 | btrfs_header_blocknr(&node->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 140 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 141 | BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 142 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 143 | struct btrfs_key cpukey; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 144 | btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key); |
| 145 | BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 150 | static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 151 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 152 | { |
| 153 | int i; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 154 | struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 155 | struct btrfs_node *parent = NULL; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 156 | int parent_slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 157 | u32 nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 158 | |
| 159 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 160 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 161 | parent_slot = path->slots[level + 1]; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 162 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 163 | |
| 164 | if (nritems == 0) |
| 165 | return 0; |
| 166 | |
| 167 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 168 | struct btrfs_disk_key *parent_key; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 169 | parent_key = &parent->ptrs[parent_slot].key; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 170 | BUG_ON(memcmp(parent_key, &leaf->items[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 171 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 172 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 173 | btrfs_header_blocknr(&leaf->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 174 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 175 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 176 | struct btrfs_key cpukey; |
| 177 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 178 | BUG_ON(comp_keys(&leaf->items[i].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 179 | &cpukey) >= 0); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 180 | BUG_ON(btrfs_item_offset(leaf->items + i) != |
| 181 | btrfs_item_end(leaf->items + i + 1)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 182 | if (i == 0) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 183 | BUG_ON(btrfs_item_offset(leaf->items + i) + |
| 184 | btrfs_item_size(leaf->items + i) != |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 185 | BTRFS_LEAF_DATA_SIZE(root)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 191 | static int check_block(struct btrfs_root *root, struct btrfs_path *path, |
| 192 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 193 | { |
| 194 | if (level == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 195 | return check_leaf(root, path, level); |
| 196 | return check_node(root, path, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 199 | /* |
| 200 | * search for key in the array p. items p are item_size apart |
| 201 | * and there are 'max' items in p |
| 202 | * the slot in the array is returned via slot, and it points to |
| 203 | * the place where you would insert key if it is not found in |
| 204 | * the array. |
| 205 | * |
| 206 | * slot may point to max if the key is bigger than all of the keys |
| 207 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 208 | 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] | 209 | int max, int *slot) |
| 210 | { |
| 211 | int low = 0; |
| 212 | int high = max; |
| 213 | int mid; |
| 214 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 215 | struct btrfs_disk_key *tmp; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 216 | |
| 217 | while(low < high) { |
| 218 | mid = (low + high) / 2; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 219 | tmp = (struct btrfs_disk_key *)(p + mid * item_size); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 220 | ret = comp_keys(tmp, key); |
| 221 | |
| 222 | if (ret < 0) |
| 223 | low = mid + 1; |
| 224 | else if (ret > 0) |
| 225 | high = mid; |
| 226 | else { |
| 227 | *slot = mid; |
| 228 | return 0; |
| 229 | } |
| 230 | } |
| 231 | *slot = low; |
| 232 | return 1; |
| 233 | } |
| 234 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 235 | /* |
| 236 | * simple bin_search frontend that does the right thing for |
| 237 | * leaves vs nodes |
| 238 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 239 | 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] | 240 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 241 | if (btrfs_is_leaf(c)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 242 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 243 | return generic_bin_search((void *)l->items, |
| 244 | sizeof(struct btrfs_item), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 245 | key, btrfs_header_nritems(&c->header), |
| 246 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 247 | } else { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 248 | return generic_bin_search((void *)c->ptrs, |
| 249 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 250 | key, btrfs_header_nritems(&c->header), |
| 251 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 252 | } |
| 253 | return -1; |
| 254 | } |
| 255 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 256 | static struct buffer_head *read_node_slot(struct btrfs_root *root, |
| 257 | struct buffer_head *parent_buf, |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 258 | int slot) |
| 259 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 260 | struct btrfs_node *node = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 261 | if (slot < 0) |
| 262 | return NULL; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 263 | if (slot >= btrfs_header_nritems(&node->header)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 264 | return NULL; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 265 | return read_tree_block(root, btrfs_node_blockptr(node, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 268 | static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root |
| 269 | *root, struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 270 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 271 | struct buffer_head *right_buf; |
| 272 | struct buffer_head *mid_buf; |
| 273 | struct buffer_head *left_buf; |
| 274 | struct buffer_head *parent_buf = NULL; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 275 | struct btrfs_node *right = NULL; |
| 276 | struct btrfs_node *mid; |
| 277 | struct btrfs_node *left = NULL; |
| 278 | struct btrfs_node *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 279 | int ret = 0; |
| 280 | int wret; |
| 281 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 282 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 283 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 284 | |
| 285 | if (level == 0) |
| 286 | return 0; |
| 287 | |
| 288 | mid_buf = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 289 | mid = btrfs_buffer_node(mid_buf); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 290 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 291 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 292 | if (level < BTRFS_MAX_LEVEL - 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 293 | parent_buf = path->nodes[level + 1]; |
| 294 | pslot = path->slots[level + 1]; |
| 295 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 296 | /* |
| 297 | * deal with the case where there is only one pointer in the root |
| 298 | * by promoting the node below to a root |
| 299 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 300 | if (!parent_buf) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 301 | struct buffer_head *child; |
| 302 | u64 blocknr = mid_buf->b_blocknr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 303 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 304 | if (btrfs_header_nritems(&mid->header) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 305 | return 0; |
| 306 | |
| 307 | /* promote the child to a root */ |
| 308 | child = read_node_slot(root, mid_buf, 0); |
| 309 | BUG_ON(!child); |
| 310 | root->node = child; |
| 311 | path->nodes[level] = NULL; |
| 312 | /* once for the path */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 313 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 314 | /* once for the root ptr */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 315 | btrfs_block_release(root, mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 316 | clean_tree_block(trans, root, mid_buf); |
| 317 | return btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 318 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 319 | parent = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 320 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 321 | if (btrfs_header_nritems(&mid->header) > |
| 322 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 323 | return 0; |
| 324 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 325 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 326 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 327 | |
| 328 | /* first, try to make some room in the middle buffer */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 329 | if (left_buf) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 330 | btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1, |
| 331 | &left_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 332 | left = btrfs_buffer_node(left_buf); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 333 | orig_slot += btrfs_header_nritems(&left->header); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 334 | wret = push_node_left(trans, root, left_buf, mid_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 335 | if (wret < 0) |
| 336 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 337 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * then try to empty the right most buffer into the middle |
| 341 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 342 | if (right_buf) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 343 | btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1, |
| 344 | &right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 345 | right = btrfs_buffer_node(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 346 | wret = push_node_left(trans, root, mid_buf, right_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 347 | if (wret < 0) |
| 348 | ret = wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 349 | if (btrfs_header_nritems(&right->header) == 0) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 350 | u64 blocknr = right_buf->b_blocknr; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 351 | btrfs_block_release(root, right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 352 | clean_tree_block(trans, root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 353 | right_buf = NULL; |
| 354 | right = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 355 | wret = del_ptr(trans, root, path, level + 1, pslot + |
| 356 | 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 357 | if (wret) |
| 358 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 359 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 360 | if (wret) |
| 361 | ret = wret; |
| 362 | } else { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 363 | memcpy(&parent->ptrs[pslot + 1].key, |
| 364 | &right->ptrs[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 365 | sizeof(struct btrfs_disk_key)); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 366 | mark_buffer_dirty(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 367 | } |
| 368 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 369 | if (btrfs_header_nritems(&mid->header) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 370 | /* |
| 371 | * we're not allowed to leave a node with one item in the |
| 372 | * tree during a delete. A deletion from lower in the tree |
| 373 | * could try to delete the only pointer in this node. |
| 374 | * So, pull some keys from the left. |
| 375 | * There has to be a left pointer at this point because |
| 376 | * otherwise we would have pulled some pointers from the |
| 377 | * right |
| 378 | */ |
| 379 | BUG_ON(!left_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 380 | wret = balance_node_right(trans, root, mid_buf, left_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 381 | if (wret < 0) |
| 382 | ret = wret; |
| 383 | BUG_ON(wret == 1); |
| 384 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 385 | if (btrfs_header_nritems(&mid->header) == 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 386 | /* we've managed to empty the middle node, drop it */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 387 | u64 blocknr = mid_buf->b_blocknr; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 388 | btrfs_block_release(root, mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 389 | clean_tree_block(trans, root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 390 | mid_buf = NULL; |
| 391 | mid = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 392 | wret = del_ptr(trans, root, path, level + 1, pslot); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 393 | if (wret) |
| 394 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 395 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 396 | if (wret) |
| 397 | ret = wret; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 398 | } else { |
| 399 | /* update the parent key to reflect our changes */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 400 | memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 401 | sizeof(struct btrfs_disk_key)); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 402 | mark_buffer_dirty(parent_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 403 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 404 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 405 | /* update the path */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 406 | if (left_buf) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 407 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 408 | get_bh(left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 409 | path->nodes[level] = left_buf; |
| 410 | path->slots[level + 1] -= 1; |
| 411 | path->slots[level] = orig_slot; |
| 412 | if (mid_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 413 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 414 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 415 | orig_slot -= btrfs_header_nritems(&left->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 416 | path->slots[level] = orig_slot; |
| 417 | } |
| 418 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 419 | /* double check we haven't messed things up */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 420 | check_block(root, path, level); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 421 | if (orig_ptr != |
| 422 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]), |
| 423 | path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 424 | BUG(); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 425 | |
| 426 | if (right_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 427 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 428 | if (left_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 429 | btrfs_block_release(root, left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 430 | return ret; |
| 431 | } |
| 432 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 433 | /* |
| 434 | * look for key in the tree. path is filled in with nodes along the way |
| 435 | * if key is found, we return zero and you can find the item in the leaf |
| 436 | * level of the path (level 0) |
| 437 | * |
| 438 | * 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] | 439 | * be inserted, and 1 is returned. If there are other errors during the |
| 440 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 441 | * |
| 442 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 443 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 444 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 445 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 446 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 447 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 448 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 449 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 450 | struct buffer_head *b; |
| 451 | struct buffer_head *cow_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 452 | struct btrfs_node *c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 453 | int slot; |
| 454 | int ret; |
| 455 | int level; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 456 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 457 | again: |
| 458 | b = root->node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 459 | get_bh(b); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 460 | while (b) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 461 | c = btrfs_buffer_node(b); |
| 462 | level = btrfs_header_level(&c->header); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 463 | if (cow) { |
| 464 | int wret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 465 | wret = btrfs_cow_block(trans, root, b, |
| 466 | p->nodes[level + 1], |
| 467 | p->slots[level + 1], |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 468 | &cow_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 469 | b = cow_buf; |
| 470 | } |
| 471 | BUG_ON(!cow && ins_len); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 472 | c = btrfs_buffer_node(b); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 473 | p->nodes[level] = b; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 474 | ret = check_block(root, p, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 475 | if (ret) |
| 476 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 477 | ret = bin_search(c, key, &slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 478 | if (!btrfs_is_leaf(c)) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 479 | if (ret && slot > 0) |
| 480 | slot -= 1; |
| 481 | p->slots[level] = slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 482 | if (ins_len > 0 && btrfs_header_nritems(&c->header) == |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 483 | BTRFS_NODEPTRS_PER_BLOCK(root)) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 484 | int sret = split_node(trans, root, p, level); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 485 | BUG_ON(sret > 0); |
| 486 | if (sret) |
| 487 | return sret; |
| 488 | b = p->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 489 | c = btrfs_buffer_node(b); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 490 | slot = p->slots[level]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 491 | } else if (ins_len < 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 492 | int sret = balance_level(trans, root, p, |
| 493 | level); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 494 | if (sret) |
| 495 | return sret; |
| 496 | b = p->nodes[level]; |
| 497 | if (!b) |
| 498 | goto again; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 499 | c = btrfs_buffer_node(b); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 500 | slot = p->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 501 | BUG_ON(btrfs_header_nritems(&c->header) == 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 502 | } |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 503 | b = read_tree_block(root, btrfs_node_blockptr(c, slot)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 504 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 505 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 506 | p->slots[level] = slot; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 507 | if (ins_len > 0 && btrfs_leaf_free_space(root, l) < |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 508 | sizeof(struct btrfs_item) + ins_len) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 509 | int sret = split_leaf(trans, root, p, ins_len); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 510 | BUG_ON(sret > 0); |
| 511 | if (sret) |
| 512 | return sret; |
| 513 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 517 | return 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 518 | } |
| 519 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 520 | /* |
| 521 | * adjust the pointers going up the tree, starting at level |
| 522 | * making sure the right key of each node is points to 'key'. |
| 523 | * This is used after shifting pointers to the left, so it stops |
| 524 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 525 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 526 | * |
| 527 | * If this fails to write a tree block, it returns -1, but continues |
| 528 | * fixing up the blocks in ram so the tree is consistent. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 529 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 530 | static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root |
| 531 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 532 | *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 533 | { |
| 534 | int i; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 535 | int ret = 0; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 536 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 537 | struct btrfs_node *t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 538 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 539 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 540 | break; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 541 | t = btrfs_buffer_node(path->nodes[i]); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 542 | memcpy(&t->ptrs[tslot].key, key, sizeof(*key)); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 543 | mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 544 | if (tslot != 0) |
| 545 | break; |
| 546 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 547 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 548 | } |
| 549 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 550 | /* |
| 551 | * 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] | 552 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 553 | * |
| 554 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 555 | * 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] | 556 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 557 | 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] | 558 | *root, struct buffer_head *dst_buf, struct |
| 559 | buffer_head *src_buf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 560 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 561 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 562 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 563 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 564 | int src_nritems; |
| 565 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 566 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 567 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 568 | src_nritems = btrfs_header_nritems(&src->header); |
| 569 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 570 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 571 | if (push_items <= 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 572 | return 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 573 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 574 | |
| 575 | if (src_nritems < push_items) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 576 | push_items = src_nritems; |
| 577 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 578 | memcpy(dst->ptrs + dst_nritems, src->ptrs, |
| 579 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 580 | if (push_items < src_nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 581 | memmove(src->ptrs, src->ptrs + push_items, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 582 | (src_nritems - push_items) * |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 583 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 584 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 585 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 586 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 587 | mark_buffer_dirty(src_buf); |
| 588 | mark_buffer_dirty(dst_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 589 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 590 | } |
| 591 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 592 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 593 | * try to push data from one node into the next node right in the |
| 594 | * tree. |
| 595 | * |
| 596 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 597 | * error, and > 0 if there was no room in the right hand block. |
| 598 | * |
| 599 | * this will only push up to 1/2 the contents of the left node over |
| 600 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 601 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 602 | btrfs_root *root, struct buffer_head *dst_buf, |
| 603 | struct buffer_head *src_buf) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 604 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 605 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 606 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 607 | int push_items = 0; |
| 608 | int max_push; |
| 609 | int src_nritems; |
| 610 | int dst_nritems; |
| 611 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 612 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 613 | src_nritems = btrfs_header_nritems(&src->header); |
| 614 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 615 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 616 | if (push_items <= 0) { |
| 617 | return 1; |
| 618 | } |
| 619 | |
| 620 | max_push = src_nritems / 2 + 1; |
| 621 | /* don't try to empty the node */ |
| 622 | if (max_push > src_nritems) |
| 623 | return 1; |
| 624 | if (max_push < push_items) |
| 625 | push_items = max_push; |
| 626 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 627 | memmove(dst->ptrs + push_items, dst->ptrs, |
| 628 | dst_nritems * sizeof(struct btrfs_key_ptr)); |
| 629 | memcpy(dst->ptrs, src->ptrs + src_nritems - push_items, |
| 630 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 631 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 632 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 633 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 634 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 635 | mark_buffer_dirty(src_buf); |
| 636 | mark_buffer_dirty(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 641 | * helper function to insert a new root level in the tree. |
| 642 | * A new node is allocated, and a single item is inserted to |
| 643 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 644 | * |
| 645 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 646 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 647 | static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 648 | *root, struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 649 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 650 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 651 | struct btrfs_node *lower; |
| 652 | struct btrfs_node *c; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 653 | struct btrfs_disk_key *lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 654 | |
| 655 | BUG_ON(path->nodes[level]); |
| 656 | BUG_ON(path->nodes[level-1] != root->node); |
| 657 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 658 | t = btrfs_alloc_free_block(trans, root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 659 | c = btrfs_buffer_node(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 660 | memset(c, 0, root->blocksize); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 661 | btrfs_set_header_nritems(&c->header, 1); |
| 662 | btrfs_set_header_level(&c->header, level); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 663 | btrfs_set_header_blocknr(&c->header, t->b_blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 664 | btrfs_set_header_parentid(&c->header, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 665 | btrfs_header_parentid(btrfs_buffer_header(root->node))); |
| 666 | lower = btrfs_buffer_node(path->nodes[level-1]); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 667 | if (btrfs_is_leaf(lower)) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 668 | lower_key = &((struct btrfs_leaf *)lower)->items[0].key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 669 | else |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 670 | lower_key = &lower->ptrs[0].key; |
| 671 | memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key)); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 672 | btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 673 | |
| 674 | mark_buffer_dirty(t); |
| 675 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 676 | /* the super has an extra ref to root->node */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 677 | btrfs_block_release(root, root->node); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 678 | root->node = t; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 679 | get_bh(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 680 | path->nodes[level] = t; |
| 681 | path->slots[level] = 0; |
| 682 | return 0; |
| 683 | } |
| 684 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 685 | /* |
| 686 | * worker function to insert a single pointer in a node. |
| 687 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 688 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 689 | * slot and level indicate where you want the key to go, and |
| 690 | * blocknr is the block the key points to. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 691 | * |
| 692 | * returns zero on success and < 0 on any error |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 693 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 694 | static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root |
| 695 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 696 | *key, u64 blocknr, int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 697 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 698 | struct btrfs_node *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 699 | int nritems; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 700 | |
| 701 | BUG_ON(!path->nodes[level]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 702 | lower = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 703 | nritems = btrfs_header_nritems(&lower->header); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 704 | if (slot > nritems) |
| 705 | BUG(); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 706 | if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 707 | BUG(); |
| 708 | if (slot != nritems) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 709 | memmove(lower->ptrs + slot + 1, lower->ptrs + slot, |
| 710 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 711 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 712 | memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 713 | btrfs_set_node_blockptr(lower, slot, blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 714 | btrfs_set_header_nritems(&lower->header, nritems + 1); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 715 | mark_buffer_dirty(path->nodes[level]); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 719 | /* |
| 720 | * split the node at the specified level in path in two. |
| 721 | * The path is corrected to point to the appropriate node after the split |
| 722 | * |
| 723 | * Before splitting this tries to make some room in the node by pushing |
| 724 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 725 | * |
| 726 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 727 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 728 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 729 | *root, struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 730 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 731 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 732 | struct btrfs_node *c; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 733 | struct buffer_head *split_buffer; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 734 | struct btrfs_node *split; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 735 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 736 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 737 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 738 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 739 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 740 | t = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 741 | c = btrfs_buffer_node(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 742 | if (t == root->node) { |
| 743 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 744 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 745 | if (ret) |
| 746 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 747 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 748 | c_nritems = btrfs_header_nritems(&c->header); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 749 | split_buffer = btrfs_alloc_free_block(trans, root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 750 | split = btrfs_buffer_node(split_buffer); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 751 | btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 752 | btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 753 | btrfs_set_header_parentid(&split->header, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 754 | btrfs_header_parentid(btrfs_buffer_header(root->node))); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 755 | mid = (c_nritems + 1) / 2; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 756 | memcpy(split->ptrs, c->ptrs + mid, |
| 757 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 758 | btrfs_set_header_nritems(&split->header, c_nritems - mid); |
| 759 | btrfs_set_header_nritems(&c->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 760 | ret = 0; |
| 761 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 762 | mark_buffer_dirty(t); |
| 763 | mark_buffer_dirty(split_buffer); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 764 | wret = insert_ptr(trans, root, path, &split->ptrs[0].key, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 765 | split_buffer->b_blocknr, path->slots[level + 1] + 1, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 766 | level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 767 | if (wret) |
| 768 | ret = wret; |
| 769 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 770 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 771 | path->slots[level] -= mid; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 772 | btrfs_block_release(root, t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 773 | path->nodes[level] = split_buffer; |
| 774 | path->slots[level + 1] += 1; |
| 775 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 776 | btrfs_block_release(root, split_buffer); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 777 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 778 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 781 | /* |
| 782 | * how many bytes are required to store the items in a leaf. start |
| 783 | * and nr indicate which items in the leaf to check. This totals up the |
| 784 | * space used both by the item structs and the item data |
| 785 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 786 | 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] | 787 | { |
| 788 | int data_len; |
| 789 | int end = start + nr - 1; |
| 790 | |
| 791 | if (!nr) |
| 792 | return 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 793 | data_len = btrfs_item_end(l->items + start); |
| 794 | data_len = data_len - btrfs_item_offset(l->items + end); |
| 795 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 796 | return data_len; |
| 797 | } |
| 798 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 799 | /* |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 800 | * push some data in the path leaf to the right, trying to free up at |
| 801 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 802 | * |
| 803 | * returns 1 if the push failed because the other node didn't have enough |
| 804 | * 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] | 805 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 806 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root |
| 807 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 808 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 809 | struct buffer_head *left_buf = path->nodes[0]; |
| 810 | struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 811 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 812 | struct buffer_head *right_buf; |
| 813 | struct buffer_head *upper; |
| 814 | struct btrfs_node *upper_node; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 815 | int slot; |
| 816 | int i; |
| 817 | int free_space; |
| 818 | int push_space = 0; |
| 819 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 820 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 821 | u32 left_nritems; |
| 822 | u32 right_nritems; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 823 | |
| 824 | slot = path->slots[1]; |
| 825 | if (!path->nodes[1]) { |
| 826 | return 1; |
| 827 | } |
| 828 | upper = path->nodes[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 829 | upper_node = btrfs_buffer_node(upper); |
| 830 | if (slot >= btrfs_header_nritems(&upper_node->header) - 1) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 831 | return 1; |
| 832 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 833 | right_buf = read_tree_block(root, |
| 834 | btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1)); |
| 835 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 836 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 837 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 838 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 839 | return 1; |
| 840 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 841 | /* cow and double check */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 842 | btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 843 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 844 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 845 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 846 | btrfs_block_release(root, right_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 847 | return 1; |
| 848 | } |
| 849 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 850 | left_nritems = btrfs_header_nritems(&left->header); |
| 851 | for (i = left_nritems - 1; i >= 0; i--) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 852 | item = left->items + i; |
| 853 | if (path->slots[0] == i) |
| 854 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 855 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 856 | free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 857 | break; |
| 858 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 859 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 860 | } |
| 861 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 862 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 863 | return 1; |
| 864 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 865 | right_nritems = btrfs_header_nritems(&right->header); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 866 | /* push left to right */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 867 | push_space = btrfs_item_end(left->items + left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 868 | push_space -= leaf_data_end(root, left); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 869 | /* make room in the right data area */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 870 | memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) - |
| 871 | push_space, btrfs_leaf_data(right) + leaf_data_end(root, right), |
| 872 | BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 873 | /* copy from the left data area */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 874 | memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 875 | btrfs_leaf_data(left) + leaf_data_end(root, left), push_space); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 876 | memmove(right->items + push_items, right->items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 877 | right_nritems * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 878 | /* copy the items from left to right */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 879 | memcpy(right->items, left->items + left_nritems - push_items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 880 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 881 | |
| 882 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 883 | right_nritems += push_items; |
| 884 | btrfs_set_header_nritems(&right->header, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 885 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 886 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 887 | btrfs_set_item_offset(right->items + i, push_space - |
| 888 | btrfs_item_size(right->items + i)); |
| 889 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 890 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 891 | left_nritems -= push_items; |
| 892 | btrfs_set_header_nritems(&left->header, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 893 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 894 | mark_buffer_dirty(left_buf); |
| 895 | mark_buffer_dirty(right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 896 | memcpy(&upper_node->ptrs[slot + 1].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 897 | &right->items[0].key, sizeof(struct btrfs_disk_key)); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 898 | mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 899 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 900 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 901 | if (path->slots[0] >= left_nritems) { |
| 902 | path->slots[0] -= left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 903 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 904 | path->nodes[0] = right_buf; |
| 905 | path->slots[1] += 1; |
| 906 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 907 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 908 | } |
| 909 | return 0; |
| 910 | } |
| 911 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 912 | * push some data in the path leaf to the left, trying to free up at |
| 913 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 914 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 915 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root |
| 916 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 917 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 918 | struct buffer_head *right_buf = path->nodes[0]; |
| 919 | struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf); |
| 920 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 921 | struct btrfs_leaf *left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 922 | int slot; |
| 923 | int i; |
| 924 | int free_space; |
| 925 | int push_space = 0; |
| 926 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 927 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 928 | u32 old_left_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 929 | int ret = 0; |
| 930 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 931 | |
| 932 | slot = path->slots[1]; |
| 933 | if (slot == 0) { |
| 934 | return 1; |
| 935 | } |
| 936 | if (!path->nodes[1]) { |
| 937 | return 1; |
| 938 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 939 | t = read_tree_block(root, |
| 940 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1)); |
| 941 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 942 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 943 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 944 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 945 | return 1; |
| 946 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 947 | |
| 948 | /* cow and double check */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 949 | btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 950 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 951 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 952 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 953 | btrfs_block_release(root, t); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 954 | return 1; |
| 955 | } |
| 956 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 957 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 958 | item = right->items + i; |
| 959 | if (path->slots[0] == i) |
| 960 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 961 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 962 | free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 963 | break; |
| 964 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 965 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 966 | } |
| 967 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 968 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 969 | return 1; |
| 970 | } |
| 971 | /* push data from right to left */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 972 | memcpy(left->items + btrfs_header_nritems(&left->header), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 973 | right->items, push_items * sizeof(struct btrfs_item)); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 974 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 975 | btrfs_item_offset(right->items + push_items -1); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 976 | memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space, |
| 977 | btrfs_leaf_data(right) + |
| 978 | btrfs_item_offset(right->items + push_items - 1), |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 979 | push_space); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 980 | old_left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 981 | BUG_ON(old_left_nritems < 0); |
| 982 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 983 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 984 | u32 ioff = btrfs_item_offset(left->items + i); |
| 985 | btrfs_set_item_offset(left->items + i, ioff - |
| 986 | (BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 987 | btrfs_item_offset(left->items + |
| 988 | old_left_nritems - 1))); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 989 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 990 | btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 991 | |
| 992 | /* fixup right node */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 993 | push_space = btrfs_item_offset(right->items + push_items - 1) - |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 994 | leaf_data_end(root, right); |
| 995 | memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 996 | push_space, btrfs_leaf_data(right) + |
| 997 | leaf_data_end(root, right), push_space); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 998 | memmove(right->items, right->items + push_items, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 999 | (btrfs_header_nritems(&right->header) - push_items) * |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1000 | sizeof(struct btrfs_item)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1001 | btrfs_set_header_nritems(&right->header, |
| 1002 | btrfs_header_nritems(&right->header) - |
| 1003 | push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1004 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1005 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1006 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1007 | btrfs_set_item_offset(right->items + i, push_space - |
| 1008 | btrfs_item_size(right->items + i)); |
| 1009 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1010 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1011 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1012 | mark_buffer_dirty(t); |
| 1013 | mark_buffer_dirty(right_buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1014 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1015 | wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1016 | if (wret) |
| 1017 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1018 | |
| 1019 | /* then fixup the leaf pointer in the path */ |
| 1020 | if (path->slots[0] < push_items) { |
| 1021 | path->slots[0] += old_left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1022 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1023 | path->nodes[0] = t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1024 | path->slots[1] -= 1; |
| 1025 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1026 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1027 | path->slots[0] -= push_items; |
| 1028 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1029 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1030 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1031 | } |
| 1032 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1033 | /* |
| 1034 | * split the path's leaf in two, making sure there is at least data_size |
| 1035 | * available for the resulting leaf level of the path. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1036 | * |
| 1037 | * returns 0 if all went well and < 0 on failure. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1038 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1039 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1040 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1041 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1042 | struct buffer_head *l_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1043 | struct btrfs_leaf *l; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1044 | u32 nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1045 | int mid; |
| 1046 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1047 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1048 | struct buffer_head *right_buffer; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1049 | int space_needed = data_size + sizeof(struct btrfs_item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1050 | int data_copy_size; |
| 1051 | int rt_data_off; |
| 1052 | int i; |
| 1053 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1054 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1055 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1056 | /* first try to make some room by pushing left and right */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1057 | wret = push_leaf_left(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1058 | if (wret < 0) |
| 1059 | return wret; |
| 1060 | if (wret) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1061 | wret = push_leaf_right(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1062 | if (wret < 0) |
| 1063 | return wret; |
| 1064 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1065 | l_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1066 | l = btrfs_buffer_leaf(l_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1067 | |
| 1068 | /* did the pushes work? */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1069 | if (btrfs_leaf_free_space(root, l) >= |
| 1070 | sizeof(struct btrfs_item) + data_size) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1071 | return 0; |
| 1072 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1073 | if (!path->nodes[1]) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1074 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1075 | if (ret) |
| 1076 | return ret; |
| 1077 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1078 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1079 | nritems = btrfs_header_nritems(&l->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1080 | mid = (nritems + 1)/ 2; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1081 | right_buffer = btrfs_alloc_free_block(trans, root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1082 | BUG_ON(!right_buffer); |
| 1083 | BUG_ON(mid == nritems); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1084 | right = btrfs_buffer_leaf(right_buffer); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1085 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1086 | if (mid <= slot) { |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1087 | /* FIXME, just alloc a new leaf here */ |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1088 | if (leaf_space_used(l, mid, nritems - mid) + space_needed > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1089 | BTRFS_LEAF_DATA_SIZE(root)) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1090 | BUG(); |
| 1091 | } else { |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1092 | /* FIXME, just alloc a new leaf here */ |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1093 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1094 | BTRFS_LEAF_DATA_SIZE(root)) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1095 | BUG(); |
| 1096 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1097 | btrfs_set_header_nritems(&right->header, nritems - mid); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1098 | btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1099 | btrfs_set_header_level(&right->header, 0); |
| 1100 | btrfs_set_header_parentid(&right->header, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1101 | btrfs_header_parentid(btrfs_buffer_header(root->node))); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1102 | data_copy_size = btrfs_item_end(l->items + mid) - |
| 1103 | leaf_data_end(root, l); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1104 | memcpy(right->items, l->items + mid, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1105 | (nritems - mid) * sizeof(struct btrfs_item)); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1106 | memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 1107 | data_copy_size, btrfs_leaf_data(l) + |
| 1108 | leaf_data_end(root, l), data_copy_size); |
| 1109 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 1110 | btrfs_item_end(l->items + mid); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1111 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1112 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1113 | u32 ioff = btrfs_item_offset(right->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1114 | btrfs_set_item_offset(right->items + i, ioff + rt_data_off); |
| 1115 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1116 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1117 | btrfs_set_header_nritems(&l->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1118 | ret = 0; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1119 | wret = insert_ptr(trans, root, path, &right->items[0].key, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1120 | right_buffer->b_blocknr, path->slots[1] + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1121 | if (wret) |
| 1122 | ret = wret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1123 | mark_buffer_dirty(right_buffer); |
| 1124 | mark_buffer_dirty(l_buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1125 | BUG_ON(path->slots[0] != slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1126 | if (mid <= slot) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1127 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1128 | path->nodes[0] = right_buffer; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1129 | path->slots[0] -= mid; |
| 1130 | path->slots[1] += 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1131 | } else |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1132 | btrfs_block_release(root, right_buffer); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1133 | BUG_ON(path->slots[0] < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1134 | return ret; |
| 1135 | } |
| 1136 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1137 | /* |
| 1138 | * Given a key and some data, insert an item into the tree. |
| 1139 | * This does all the path init required, making room in the tree if needed. |
| 1140 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1141 | int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1142 | *root, struct btrfs_path *path, struct btrfs_key |
| 1143 | *cpu_key, u32 data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1144 | { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1145 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1146 | int slot; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1147 | int slot_orig; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1148 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1149 | struct buffer_head *leaf_buf; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1150 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1151 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1152 | struct btrfs_disk_key disk_key; |
| 1153 | |
| 1154 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1155 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1156 | /* create a root if there isn't one */ |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1157 | if (!root->node) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1158 | BUG(); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1159 | ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1160 | if (ret == 0) { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1161 | btrfs_release_path(root, path); |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1162 | return -EEXIST; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1163 | } |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1164 | if (ret < 0) |
| 1165 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1166 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1167 | slot_orig = path->slots[0]; |
| 1168 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1169 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1170 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1171 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1172 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1173 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1174 | if (btrfs_leaf_free_space(root, leaf) < |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1175 | sizeof(struct btrfs_item) + data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1176 | BUG(); |
| 1177 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1178 | slot = path->slots[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1179 | BUG_ON(slot < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1180 | if (slot != nritems) { |
| 1181 | int i; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1182 | unsigned int old_data = btrfs_item_end(leaf->items + slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1186 | */ |
| 1187 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1188 | for (i = slot; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1189 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1190 | btrfs_set_item_offset(leaf->items + i, |
| 1191 | ioff - data_size); |
| 1192 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1193 | |
| 1194 | /* shift the items */ |
| 1195 | memmove(leaf->items + slot + 1, leaf->items + slot, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1196 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1197 | |
| 1198 | /* shift the data */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1199 | memmove(btrfs_leaf_data(leaf) + data_end - data_size, |
| 1200 | btrfs_leaf_data(leaf) + |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1201 | data_end, old_data - data_end); |
| 1202 | data_end = old_data; |
| 1203 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1204 | /* setup the item for the new data */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1205 | memcpy(&leaf->items[slot].key, &disk_key, |
| 1206 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1207 | btrfs_set_item_offset(leaf->items + slot, data_end - data_size); |
| 1208 | btrfs_set_item_size(leaf->items + slot, data_size); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1209 | btrfs_set_header_nritems(&leaf->header, nritems + 1); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1210 | mark_buffer_dirty(leaf_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1211 | |
| 1212 | ret = 0; |
Chris Mason | 8e19f2c | 2007-02-28 09:27:02 -0500 | [diff] [blame] | 1213 | if (slot == 0) |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1214 | ret = fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1215 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1216 | if (btrfs_leaf_free_space(root, leaf) < 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1217 | BUG(); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1218 | check_leaf(root, path, 0); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1219 | out: |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1220 | return ret; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Given a key and some data, insert an item into the tree. |
| 1225 | * This does all the path init required, making room in the tree if needed. |
| 1226 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1227 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1228 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 1229 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1230 | { |
| 1231 | int ret = 0; |
| 1232 | struct btrfs_path path; |
| 1233 | u8 *ptr; |
| 1234 | |
| 1235 | btrfs_init_path(&path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1236 | ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1237 | if (!ret) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1238 | ptr = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), |
| 1239 | path.slots[0], u8); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1240 | memcpy(ptr, data, data_size); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1241 | mark_buffer_dirty(path.nodes[0]); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1242 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1243 | btrfs_release_path(root, &path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1244 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1247 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1248 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1249 | * |
| 1250 | * If the delete empties a node, the node is removed from the tree, |
| 1251 | * continuing all the way the root if required. The root is converted into |
| 1252 | * a leaf if all the nodes are emptied. |
| 1253 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1254 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1255 | struct btrfs_path *path, int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1256 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1257 | struct btrfs_node *node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1258 | struct buffer_head *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1259 | u32 nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1260 | int ret = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1261 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1262 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1263 | node = btrfs_buffer_node(parent); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1264 | nritems = btrfs_header_nritems(&node->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1265 | if (slot != nritems -1) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1266 | memmove(node->ptrs + slot, node->ptrs + slot + 1, |
| 1267 | sizeof(struct btrfs_key_ptr) * (nritems - slot - 1)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1268 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1269 | nritems--; |
| 1270 | btrfs_set_header_nritems(&node->header, nritems); |
| 1271 | if (nritems == 0 && parent == root->node) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1272 | struct btrfs_header *header = btrfs_buffer_header(root->node); |
| 1273 | BUG_ON(btrfs_header_level(header) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1274 | /* just turn the root into a leaf and break */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1275 | btrfs_set_header_level(header, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1276 | } else if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1277 | wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1278 | level + 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1279 | if (wret) |
| 1280 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1281 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1282 | mark_buffer_dirty(parent); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1283 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1284 | } |
| 1285 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1286 | /* |
| 1287 | * delete the item at the leaf level in path. If that empties |
| 1288 | * the leaf, remove it from the tree |
| 1289 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1290 | int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1291 | struct btrfs_path *path) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1292 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1293 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1294 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1295 | struct buffer_head *leaf_buf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1296 | int doff; |
| 1297 | int dsize; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1298 | int ret = 0; |
| 1299 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1300 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1301 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1302 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1303 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1304 | slot = path->slots[0]; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1305 | doff = btrfs_item_offset(leaf->items + slot); |
| 1306 | dsize = btrfs_item_size(leaf->items + slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1307 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1308 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1309 | if (slot != nritems - 1) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1310 | int i; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1311 | int data_end = leaf_data_end(root, leaf); |
| 1312 | memmove(btrfs_leaf_data(leaf) + data_end + dsize, |
| 1313 | btrfs_leaf_data(leaf) + data_end, |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1314 | doff - data_end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1315 | for (i = slot + 1; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1316 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1317 | btrfs_set_item_offset(leaf->items + i, ioff + dsize); |
| 1318 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1319 | memmove(leaf->items + slot, leaf->items + slot + 1, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1320 | sizeof(struct btrfs_item) * |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1321 | (nritems - slot - 1)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1322 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1323 | btrfs_set_header_nritems(&leaf->header, nritems - 1); |
| 1324 | nritems--; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1325 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1326 | if (nritems == 0) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1327 | if (leaf_buf == root->node) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1328 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1329 | } else { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1330 | clean_tree_block(trans, root, leaf_buf); |
| 1331 | wret = del_ptr(trans, root, path, 1, path->slots[1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1332 | if (wret) |
| 1333 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1334 | wret = btrfs_free_extent(trans, root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1335 | leaf_buf->b_blocknr, 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1336 | if (wret) |
| 1337 | ret = wret; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1338 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1339 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1340 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1341 | if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1342 | wret = fixup_low_keys(trans, root, path, |
| 1343 | &leaf->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1344 | if (wret) |
| 1345 | ret = wret; |
| 1346 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1347 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1348 | /* delete the leaf if it is mostly empty */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1349 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1350 | /* push_leaf_left fixes the path. |
| 1351 | * make sure the path still points to our leaf |
| 1352 | * for possible call to del_ptr below |
| 1353 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1354 | slot = path->slots[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1355 | get_bh(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1356 | wret = push_leaf_left(trans, root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1357 | if (wret < 0) |
| 1358 | ret = wret; |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1359 | if (path->nodes[0] == leaf_buf && |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1360 | btrfs_header_nritems(&leaf->header)) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1361 | wret = push_leaf_right(trans, root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1362 | if (wret < 0) |
| 1363 | ret = wret; |
| 1364 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1365 | if (btrfs_header_nritems(&leaf->header) == 0) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1366 | u64 blocknr = leaf_buf->b_blocknr; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1367 | clean_tree_block(trans, root, leaf_buf); |
| 1368 | wret = del_ptr(trans, root, path, 1, slot); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1369 | if (wret) |
| 1370 | ret = wret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1371 | btrfs_block_release(root, leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1372 | wret = btrfs_free_extent(trans, root, blocknr, |
| 1373 | 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1374 | if (wret) |
| 1375 | ret = wret; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1376 | } else { |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1377 | mark_buffer_dirty(leaf_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1378 | btrfs_block_release(root, leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1379 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame^] | 1380 | } else { |
| 1381 | mark_buffer_dirty(leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1382 | } |
| 1383 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1384 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1385 | } |
| 1386 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1387 | /* |
| 1388 | * 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] | 1389 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 1390 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1391 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1392 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1393 | { |
| 1394 | int slot; |
| 1395 | int level = 1; |
| 1396 | u64 blocknr; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1397 | struct buffer_head *c; |
| 1398 | struct btrfs_node *c_node; |
| 1399 | struct buffer_head *next = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1400 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1401 | while(level < BTRFS_MAX_LEVEL) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1402 | if (!path->nodes[level]) |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1403 | return 1; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1404 | slot = path->slots[level] + 1; |
| 1405 | c = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1406 | c_node = btrfs_buffer_node(c); |
| 1407 | if (slot >= btrfs_header_nritems(&c_node->header)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1408 | level++; |
| 1409 | continue; |
| 1410 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1411 | blocknr = btrfs_node_blockptr(c_node, slot); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1412 | if (next) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1413 | btrfs_block_release(root, next); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1414 | next = read_tree_block(root, blocknr); |
| 1415 | break; |
| 1416 | } |
| 1417 | path->slots[level] = slot; |
| 1418 | while(1) { |
| 1419 | level--; |
| 1420 | c = path->nodes[level]; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1421 | btrfs_block_release(root, c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1422 | path->nodes[level] = next; |
| 1423 | path->slots[level] = 0; |
| 1424 | if (!level) |
| 1425 | break; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1426 | next = read_tree_block(root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1427 | btrfs_node_blockptr(btrfs_buffer_node(next), 0)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1428 | } |
| 1429 | return 0; |
| 1430 | } |