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