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