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