blob: e690e2bb47d20750e077fe11316d83b88b955698 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masoneb60cea2007-02-02 09:18:22 -05002#include "ctree.h"
3#include "disk-io.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05004
Chris Masone089f052007-03-16 16:20:31 -04005static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
6 *root, struct btrfs_path *path, int level);
7static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
8 *root, struct btrfs_path *path, int data_size);
9static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040010 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040011 *src);
12static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040013 btrfs_root *root, struct buffer_head *dst_buf,
14 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040015static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
16 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050017
Chris Mason234b63a2007-03-13 10:46:10 -040018inline void btrfs_init_path(struct btrfs_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050019{
20 memset(p, 0, sizeof(*p));
21}
22
Chris Mason234b63a2007-03-13 10:46:10 -040023void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050024{
25 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040026 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050027 if (!p->nodes[i])
28 break;
Chris Mason234b63a2007-03-13 10:46:10 -040029 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050030 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050031 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050032}
33
Chris Masone089f052007-03-16 16:20:31 -040034static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040035 *root, struct buffer_head *buf, struct buffer_head
36 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040037 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050038{
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct buffer_head *cow;
40 struct btrfs_node *cow_node;
Chris Mason02217ed2007-03-02 16:08:05 -050041
Chris Masone20d96d2007-03-22 12:13:20 -040042 if (!buffer_dirty(buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -050043 *cow_ret = buf;
44 return 0;
45 }
Chris Masone089f052007-03-16 16:20:31 -040046 cow = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -040047 cow_node = btrfs_buffer_node(cow);
48 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
49 btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050050 *cow_ret = cow;
Chris Masone089f052007-03-16 16:20:31 -040051 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050052 if (buf == root->node) {
53 root->node = cow;
Chris Masone20d96d2007-03-22 12:13:20 -040054 get_bh(cow);
Chris Masona28ec192007-03-06 20:08:01 -050055 if (buf != root->commit_root)
Chris Masone20d96d2007-03-22 12:13:20 -040056 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -040057 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050058 } else {
Chris Masone20d96d2007-03-22 12:13:20 -040059 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
60 cow->b_blocknr);
61 BUG_ON(!buffer_dirty(parent));
62 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050063 }
Chris Mason234b63a2007-03-13 10:46:10 -040064 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050065 return 0;
66}
67
Chris Mason74123bd2007-02-02 11:05:29 -050068/*
69 * The leaf data grows from end-to-front in the node.
70 * this returns the address of the start of the last item,
71 * which is the stop of the leaf data stack
72 */
Chris Mason123abc82007-03-14 14:14:43 -040073static inline unsigned int leaf_data_end(struct btrfs_root *root,
74 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050075{
Chris Mason7518a232007-03-12 12:01:18 -040076 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050077 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -040078 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -040079 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050080}
81
Chris Mason74123bd2007-02-02 11:05:29 -050082/*
83 * The space between the end of the leaf items and
84 * the start of the leaf data. IOW, how much room
85 * the leaf has left for both items and data
86 */
Chris Mason123abc82007-03-14 14:14:43 -040087int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050088{
Chris Mason123abc82007-03-14 14:14:43 -040089 int data_end = leaf_data_end(root, leaf);
Chris Mason7518a232007-03-12 12:01:18 -040090 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050091 char *items_end = (char *)(leaf->items + nritems + 1);
Chris Mason123abc82007-03-14 14:14:43 -040092 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
Chris Masonbe0e5c02007-01-26 15:51:26 -050093}
94
Chris Mason74123bd2007-02-02 11:05:29 -050095/*
96 * compare two keys in a memcmp fashion
97 */
Chris Mason9aca1d52007-03-13 11:09:37 -040098static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -050099{
Chris Masone2fa7222007-03-12 16:22:34 -0400100 struct btrfs_key k1;
101
102 btrfs_disk_key_to_cpu(&k1, disk);
103
104 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500105 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400106 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500107 return -1;
Chris Mason62e27492007-03-15 12:56:47 -0400108 if (k1.flags > k2->flags)
109 return 1;
110 if (k1.flags < k2->flags)
111 return -1;
Chris Masona8a2ee02007-03-16 08:46:49 -0400112 if (k1.offset > k2->offset)
113 return 1;
114 if (k1.offset < k2->offset)
115 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500116 return 0;
117}
Chris Mason74123bd2007-02-02 11:05:29 -0500118
Chris Mason123abc82007-03-14 14:14:43 -0400119static int check_node(struct btrfs_root *root, struct btrfs_path *path,
120 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500121{
122 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400123 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400124 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500125 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400126 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500127
128 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400129 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500130 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400131 BUG_ON(nritems == 0);
132 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400133 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400134 parent_key = &parent->ptrs[parent_slot].key;
135 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400136 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400137 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400138 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500139 }
Chris Mason123abc82007-03-14 14:14:43 -0400140 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400141 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400142 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400143 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
144 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500145 }
146 return 0;
147}
148
Chris Mason123abc82007-03-14 14:14:43 -0400149static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
150 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500151{
152 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400153 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400154 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500155 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400156 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500157
158 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400159 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500160 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400161 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400162
163 if (nritems == 0)
164 return 0;
165
166 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400167 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400168 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500169 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400170 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400171 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400172 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500173 }
Chris Mason7518a232007-03-12 12:01:18 -0400174 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400175 struct btrfs_key cpukey;
176 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500177 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400178 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400179 BUG_ON(btrfs_item_offset(leaf->items + i) !=
180 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500181 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400182 BUG_ON(btrfs_item_offset(leaf->items + i) +
183 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400184 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500185 }
186 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500187 return 0;
188}
189
Chris Mason123abc82007-03-14 14:14:43 -0400190static int check_block(struct btrfs_root *root, struct btrfs_path *path,
191 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500192{
193 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400194 return check_leaf(root, path, level);
195 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500196}
197
Chris Mason74123bd2007-02-02 11:05:29 -0500198/*
199 * search for key in the array p. items p are item_size apart
200 * and there are 'max' items in p
201 * the slot in the array is returned via slot, and it points to
202 * the place where you would insert key if it is not found in
203 * the array.
204 *
205 * slot may point to max if the key is bigger than all of the keys
206 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400207static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500208 int max, int *slot)
209{
210 int low = 0;
211 int high = max;
212 int mid;
213 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400214 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500215
216 while(low < high) {
217 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400218 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500219 ret = comp_keys(tmp, key);
220
221 if (ret < 0)
222 low = mid + 1;
223 else if (ret > 0)
224 high = mid;
225 else {
226 *slot = mid;
227 return 0;
228 }
229 }
230 *slot = low;
231 return 1;
232}
233
Chris Mason97571fd2007-02-24 13:39:08 -0500234/*
235 * simple bin_search frontend that does the right thing for
236 * leaves vs nodes
237 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400238static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500239{
Chris Mason7518a232007-03-12 12:01:18 -0400240 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400241 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400242 return generic_bin_search((void *)l->items,
243 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400244 key, btrfs_header_nritems(&c->header),
245 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500246 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400247 return generic_bin_search((void *)c->ptrs,
248 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400249 key, btrfs_header_nritems(&c->header),
250 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500251 }
252 return -1;
253}
254
Chris Masone20d96d2007-03-22 12:13:20 -0400255static struct buffer_head *read_node_slot(struct btrfs_root *root,
256 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500257 int slot)
258{
Chris Masone20d96d2007-03-22 12:13:20 -0400259 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500260 if (slot < 0)
261 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400262 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500263 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400264 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500265}
266
Chris Masone089f052007-03-16 16:20:31 -0400267static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
268 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500269{
Chris Masone20d96d2007-03-22 12:13:20 -0400270 struct buffer_head *right_buf;
271 struct buffer_head *mid_buf;
272 struct buffer_head *left_buf;
273 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400274 struct btrfs_node *right = NULL;
275 struct btrfs_node *mid;
276 struct btrfs_node *left = NULL;
277 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500278 int ret = 0;
279 int wret;
280 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500281 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500282 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500283
284 if (level == 0)
285 return 0;
286
287 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400288 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400289 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500290
Chris Mason234b63a2007-03-13 10:46:10 -0400291 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500292 parent_buf = path->nodes[level + 1];
293 pslot = path->slots[level + 1];
294
Chris Mason40689472007-03-17 14:29:23 -0400295 /*
296 * deal with the case where there is only one pointer in the root
297 * by promoting the node below to a root
298 */
Chris Masonbb803952007-03-01 12:04:21 -0500299 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400300 struct buffer_head *child;
301 u64 blocknr = mid_buf->b_blocknr;
Chris Masonbb803952007-03-01 12:04:21 -0500302
Chris Mason7518a232007-03-12 12:01:18 -0400303 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500304 return 0;
305
306 /* promote the child to a root */
307 child = read_node_slot(root, mid_buf, 0);
308 BUG_ON(!child);
309 root->node = child;
310 path->nodes[level] = NULL;
311 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400312 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500313 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400314 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400315 clean_tree_block(trans, root, mid_buf);
316 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500317 }
Chris Masone20d96d2007-03-22 12:13:20 -0400318 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500319
Chris Mason123abc82007-03-14 14:14:43 -0400320 if (btrfs_header_nritems(&mid->header) >
321 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500322 return 0;
323
Chris Masonbb803952007-03-01 12:04:21 -0500324 left_buf = read_node_slot(root, parent_buf, pslot - 1);
325 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500326
327 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500328 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400329 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
330 &left_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400331 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400332 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400333 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500334 if (wret < 0)
335 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500336 }
Chris Mason79f95c82007-03-01 15:16:26 -0500337
338 /*
339 * then try to empty the right most buffer into the middle
340 */
Chris Masonbb803952007-03-01 12:04:21 -0500341 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400342 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
343 &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400344 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400345 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500346 if (wret < 0)
347 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400348 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -0400349 u64 blocknr = right_buf->b_blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400350 btrfs_block_release(root, right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400351 clean_tree_block(trans, root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500352 right_buf = NULL;
353 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400354 wret = del_ptr(trans, root, path, level + 1, pslot +
355 1);
Chris Masonbb803952007-03-01 12:04:21 -0500356 if (wret)
357 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400358 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500359 if (wret)
360 ret = wret;
361 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400362 memcpy(&parent->ptrs[pslot + 1].key,
363 &right->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400364 sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400365 BUG_ON(!buffer_dirty(parent_buf));
Chris Masonbb803952007-03-01 12:04:21 -0500366 }
367 }
Chris Mason7518a232007-03-12 12:01:18 -0400368 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500369 /*
370 * we're not allowed to leave a node with one item in the
371 * tree during a delete. A deletion from lower in the tree
372 * could try to delete the only pointer in this node.
373 * So, pull some keys from the left.
374 * There has to be a left pointer at this point because
375 * otherwise we would have pulled some pointers from the
376 * right
377 */
378 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400379 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500380 if (wret < 0)
381 ret = wret;
382 BUG_ON(wret == 1);
383 }
Chris Mason7518a232007-03-12 12:01:18 -0400384 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500385 /* we've managed to empty the middle node, drop it */
Chris Masone20d96d2007-03-22 12:13:20 -0400386 u64 blocknr = mid_buf->b_blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400387 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400388 clean_tree_block(trans, root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500389 mid_buf = NULL;
390 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400391 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500392 if (wret)
393 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400394 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500395 if (wret)
396 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500397 } else {
398 /* update the parent key to reflect our changes */
Chris Mason123abc82007-03-14 14:14:43 -0400399 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400400 sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400401 BUG_ON(!buffer_dirty(parent_buf));
Chris Mason79f95c82007-03-01 15:16:26 -0500402 }
Chris Masonbb803952007-03-01 12:04:21 -0500403
Chris Mason79f95c82007-03-01 15:16:26 -0500404 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500405 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400406 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400407 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500408 path->nodes[level] = left_buf;
409 path->slots[level + 1] -= 1;
410 path->slots[level] = orig_slot;
411 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400412 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500413 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400414 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500415 path->slots[level] = orig_slot;
416 }
417 }
Chris Mason79f95c82007-03-01 15:16:26 -0500418 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400419 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400420 if (orig_ptr !=
421 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
422 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500423 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500424
425 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400426 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500427 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400428 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500429 return ret;
430}
431
Chris Mason74123bd2007-02-02 11:05:29 -0500432/*
433 * look for key in the tree. path is filled in with nodes along the way
434 * if key is found, we return zero and you can find the item in the leaf
435 * level of the path (level 0)
436 *
437 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500438 * be inserted, and 1 is returned. If there are other errors during the
439 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500440 *
441 * if ins_len > 0, nodes and leaves will be split as we walk down the
442 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
443 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500444 */
Chris Masone089f052007-03-16 16:20:31 -0400445int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
446 *root, struct btrfs_key *key, struct btrfs_path *p, int
447 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500448{
Chris Masone20d96d2007-03-22 12:13:20 -0400449 struct buffer_head *b;
450 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400451 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500452 int slot;
453 int ret;
454 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500455
Chris Masonbb803952007-03-01 12:04:21 -0500456again:
457 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400458 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500459 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400460 c = btrfs_buffer_node(b);
461 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500462 if (cow) {
463 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400464 wret = btrfs_cow_block(trans, root, b,
465 p->nodes[level + 1],
466 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400467 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500468 b = cow_buf;
469 }
470 BUG_ON(!cow && ins_len);
Chris Masone20d96d2007-03-22 12:13:20 -0400471 c = btrfs_buffer_node(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500472 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400473 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500474 if (ret)
475 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500476 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400477 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500478 if (ret && slot > 0)
479 slot -= 1;
480 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400481 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
Chris Mason123abc82007-03-14 14:14:43 -0400482 BTRFS_NODEPTRS_PER_BLOCK(root)) {
Chris Masone089f052007-03-16 16:20:31 -0400483 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500484 BUG_ON(sret > 0);
485 if (sret)
486 return sret;
487 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400488 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500489 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500490 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400491 int sret = balance_level(trans, root, p,
492 level);
Chris Masonbb803952007-03-01 12:04:21 -0500493 if (sret)
494 return sret;
495 b = p->nodes[level];
496 if (!b)
497 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400498 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500499 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400500 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500501 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400502 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500503 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400504 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500505 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400506 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400507 sizeof(struct btrfs_item) + ins_len) {
Chris Masone089f052007-03-16 16:20:31 -0400508 int sret = split_leaf(trans, root, p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500509 BUG_ON(sret > 0);
510 if (sret)
511 return sret;
512 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500513 return ret;
514 }
515 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500516 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500517}
518
Chris Mason74123bd2007-02-02 11:05:29 -0500519/*
520 * adjust the pointers going up the tree, starting at level
521 * making sure the right key of each node is points to 'key'.
522 * This is used after shifting pointers to the left, so it stops
523 * fixing up pointers when a given leaf/node is not in slot 0 of the
524 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500525 *
526 * If this fails to write a tree block, it returns -1, but continues
527 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500528 */
Chris Masone089f052007-03-16 16:20:31 -0400529static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
530 *root, struct btrfs_path *path, struct btrfs_disk_key
531 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500532{
533 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500534 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400535 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
536 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500537 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500538 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500539 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400540 t = btrfs_buffer_node(path->nodes[i]);
Chris Mason123abc82007-03-14 14:14:43 -0400541 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
Chris Masone20d96d2007-03-22 12:13:20 -0400542 BUG_ON(!buffer_dirty(path->nodes[i]));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500543 if (tslot != 0)
544 break;
545 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500546 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500547}
548
Chris Mason74123bd2007-02-02 11:05:29 -0500549/*
550 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500551 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500552 *
553 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
554 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500555 */
Chris Masone089f052007-03-16 16:20:31 -0400556static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400557 *root, struct buffer_head *dst_buf, struct
558 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500559{
Chris Masone20d96d2007-03-22 12:13:20 -0400560 struct btrfs_node *src = btrfs_buffer_node(src_buf);
561 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500562 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500563 int src_nritems;
564 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500565 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500566
Chris Mason7518a232007-03-12 12:01:18 -0400567 src_nritems = btrfs_header_nritems(&src->header);
568 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400569 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500570 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500571 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500572 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500573
574 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500575 push_items = src_nritems;
576
Chris Mason123abc82007-03-14 14:14:43 -0400577 memcpy(dst->ptrs + dst_nritems, src->ptrs,
578 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500579 if (push_items < src_nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400580 memmove(src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400581 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400582 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500583 }
Chris Mason7518a232007-03-12 12:01:18 -0400584 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
585 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masone20d96d2007-03-22 12:13:20 -0400586 BUG_ON(!buffer_dirty(src_buf));
587 BUG_ON(!buffer_dirty(dst_buf));
Chris Masonbb803952007-03-01 12:04:21 -0500588 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500589}
590
Chris Mason97571fd2007-02-24 13:39:08 -0500591/*
Chris Mason79f95c82007-03-01 15:16:26 -0500592 * try to push data from one node into the next node right in the
593 * tree.
594 *
595 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
596 * error, and > 0 if there was no room in the right hand block.
597 *
598 * this will only push up to 1/2 the contents of the left node over
599 */
Chris Masone089f052007-03-16 16:20:31 -0400600static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400601 btrfs_root *root, struct buffer_head *dst_buf,
602 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500603{
Chris Masone20d96d2007-03-22 12:13:20 -0400604 struct btrfs_node *src = btrfs_buffer_node(src_buf);
605 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500606 int push_items = 0;
607 int max_push;
608 int src_nritems;
609 int dst_nritems;
610 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500611
Chris Mason7518a232007-03-12 12:01:18 -0400612 src_nritems = btrfs_header_nritems(&src->header);
613 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400614 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500615 if (push_items <= 0) {
616 return 1;
617 }
618
619 max_push = src_nritems / 2 + 1;
620 /* don't try to empty the node */
621 if (max_push > src_nritems)
622 return 1;
623 if (max_push < push_items)
624 push_items = max_push;
625
Chris Mason123abc82007-03-14 14:14:43 -0400626 memmove(dst->ptrs + push_items, dst->ptrs,
627 dst_nritems * sizeof(struct btrfs_key_ptr));
628 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
629 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500630
Chris Mason7518a232007-03-12 12:01:18 -0400631 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
632 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500633
Chris Masone20d96d2007-03-22 12:13:20 -0400634 BUG_ON(!buffer_dirty(src_buf));
635 BUG_ON(!buffer_dirty(dst_buf));
Chris Mason79f95c82007-03-01 15:16:26 -0500636 return ret;
637}
638
639/*
Chris Mason97571fd2007-02-24 13:39:08 -0500640 * helper function to insert a new root level in the tree.
641 * A new node is allocated, and a single item is inserted to
642 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500643 *
644 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500645 */
Chris Masone089f052007-03-16 16:20:31 -0400646static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
647 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500648{
Chris Masone20d96d2007-03-22 12:13:20 -0400649 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400650 struct btrfs_node *lower;
651 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400652 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500653
654 BUG_ON(path->nodes[level]);
655 BUG_ON(path->nodes[level-1] != root->node);
656
Chris Masone089f052007-03-16 16:20:31 -0400657 t = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400658 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400659 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400660 btrfs_set_header_nritems(&c->header, 1);
661 btrfs_set_header_level(&c->header, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400662 btrfs_set_header_blocknr(&c->header, t->b_blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400663 btrfs_set_header_parentid(&c->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400664 btrfs_header_parentid(btrfs_buffer_header(root->node)));
665 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason7518a232007-03-12 12:01:18 -0400666 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400667 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500668 else
Chris Mason123abc82007-03-14 14:14:43 -0400669 lower_key = &lower->ptrs[0].key;
670 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400671 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
Chris Mason5c680ed2007-02-22 11:39:13 -0500672 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400673 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500674 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400675 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500676 path->nodes[level] = t;
677 path->slots[level] = 0;
678 return 0;
679}
680
Chris Mason74123bd2007-02-02 11:05:29 -0500681/*
682 * worker function to insert a single pointer in a node.
683 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500684 *
Chris Mason74123bd2007-02-02 11:05:29 -0500685 * slot and level indicate where you want the key to go, and
686 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500687 *
688 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500689 */
Chris Masone089f052007-03-16 16:20:31 -0400690static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
691 *root, struct btrfs_path *path, struct btrfs_disk_key
692 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500693{
Chris Mason234b63a2007-03-13 10:46:10 -0400694 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500695 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500696
697 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400698 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400699 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500700 if (slot > nritems)
701 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400702 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500703 BUG();
704 if (slot != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400705 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
706 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500707 }
Chris Mason123abc82007-03-14 14:14:43 -0400708 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400709 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400710 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masone20d96d2007-03-22 12:13:20 -0400711 BUG_ON(!buffer_dirty(path->nodes[level]));
Chris Mason74123bd2007-02-02 11:05:29 -0500712 return 0;
713}
714
Chris Mason97571fd2007-02-24 13:39:08 -0500715/*
716 * split the node at the specified level in path in two.
717 * The path is corrected to point to the appropriate node after the split
718 *
719 * Before splitting this tries to make some room in the node by pushing
720 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500721 *
722 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500723 */
Chris Masone089f052007-03-16 16:20:31 -0400724static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
725 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726{
Chris Masone20d96d2007-03-22 12:13:20 -0400727 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400728 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400729 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400730 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500732 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500733 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400734 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735
Chris Mason5c680ed2007-02-22 11:39:13 -0500736 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400737 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500738 if (t == root->node) {
739 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400740 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500741 if (ret)
742 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743 }
Chris Mason7518a232007-03-12 12:01:18 -0400744 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400745 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400746 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400747 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Masone20d96d2007-03-22 12:13:20 -0400748 btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400749 btrfs_set_header_parentid(&split->header,
Chris Masone20d96d2007-03-22 12:13:20 -0400750 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason7518a232007-03-12 12:01:18 -0400751 mid = (c_nritems + 1) / 2;
Chris Mason123abc82007-03-14 14:14:43 -0400752 memcpy(split->ptrs, c->ptrs + mid,
753 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400754 btrfs_set_header_nritems(&split->header, c_nritems - mid);
755 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500756 ret = 0;
757
Chris Masone20d96d2007-03-22 12:13:20 -0400758 BUG_ON(!buffer_dirty(t));
Chris Masone089f052007-03-16 16:20:31 -0400759 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -0400760 split_buffer->b_blocknr, path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400761 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500762 if (wret)
763 ret = wret;
764
Chris Mason5de08d72007-02-24 06:24:44 -0500765 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500766 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400767 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500768 path->nodes[level] = split_buffer;
769 path->slots[level + 1] += 1;
770 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400771 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500772 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500773 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500774}
775
Chris Mason74123bd2007-02-02 11:05:29 -0500776/*
777 * how many bytes are required to store the items in a leaf. start
778 * and nr indicate which items in the leaf to check. This totals up the
779 * space used both by the item structs and the item data
780 */
Chris Mason234b63a2007-03-13 10:46:10 -0400781static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500782{
783 int data_len;
784 int end = start + nr - 1;
785
786 if (!nr)
787 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400788 data_len = btrfs_item_end(l->items + start);
789 data_len = data_len - btrfs_item_offset(l->items + end);
790 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500791 return data_len;
792}
793
Chris Mason74123bd2007-02-02 11:05:29 -0500794/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500795 * push some data in the path leaf to the right, trying to free up at
796 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500797 *
798 * returns 1 if the push failed because the other node didn't have enough
799 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500800 */
Chris Masone089f052007-03-16 16:20:31 -0400801static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
802 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500803{
Chris Masone20d96d2007-03-22 12:13:20 -0400804 struct buffer_head *left_buf = path->nodes[0];
805 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400806 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -0400807 struct buffer_head *right_buf;
808 struct buffer_head *upper;
809 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -0500810 int slot;
811 int i;
812 int free_space;
813 int push_space = 0;
814 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400815 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400816 u32 left_nritems;
817 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500818
819 slot = path->slots[1];
820 if (!path->nodes[1]) {
821 return 1;
822 }
823 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -0400824 upper_node = btrfs_buffer_node(upper);
825 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500826 return 1;
827 }
Chris Masone20d96d2007-03-22 12:13:20 -0400828 right_buf = read_tree_block(root,
829 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
830 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400831 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400832 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400833 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500834 return 1;
835 }
Chris Mason02217ed2007-03-02 16:08:05 -0500836 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400837 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400838 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -0400839 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400840 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400841 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500842 return 1;
843 }
844
Chris Mason7518a232007-03-12 12:01:18 -0400845 left_nritems = btrfs_header_nritems(&left->header);
846 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500847 item = left->items + i;
848 if (path->slots[0] == i)
849 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400850 if (btrfs_item_size(item) + sizeof(*item) + push_space >
851 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500852 break;
853 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400854 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500855 }
856 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400857 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500858 return 1;
859 }
Chris Mason7518a232007-03-12 12:01:18 -0400860 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500861 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400862 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400863 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500864 /* make room in the right data area */
Chris Mason123abc82007-03-14 14:14:43 -0400865 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
866 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
867 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500868 /* copy from the left data area */
Chris Mason123abc82007-03-14 14:14:43 -0400869 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
870 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
Chris Mason00ec4c52007-02-24 12:47:20 -0500871 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400872 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500873 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400874 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400875 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500876
877 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400878 right_nritems += push_items;
879 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400880 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400881 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400882 btrfs_set_item_offset(right->items + i, push_space -
883 btrfs_item_size(right->items + i));
884 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500885 }
Chris Mason7518a232007-03-12 12:01:18 -0400886 left_nritems -= push_items;
887 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500888
Chris Masone20d96d2007-03-22 12:13:20 -0400889 BUG_ON(!buffer_dirty(left_buf));
890 BUG_ON(!buffer_dirty(right_buf));
891 memcpy(&upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400892 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masone20d96d2007-03-22 12:13:20 -0400893 BUG_ON(!buffer_dirty(upper));
Chris Mason02217ed2007-03-02 16:08:05 -0500894
Chris Mason00ec4c52007-02-24 12:47:20 -0500895 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400896 if (path->slots[0] >= left_nritems) {
897 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400898 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500899 path->nodes[0] = right_buf;
900 path->slots[1] += 1;
901 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400902 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500903 }
904 return 0;
905}
906/*
Chris Mason74123bd2007-02-02 11:05:29 -0500907 * push some data in the path leaf to the left, trying to free up at
908 * least data_size bytes. returns zero if the push worked, nonzero otherwise
909 */
Chris Masone089f052007-03-16 16:20:31 -0400910static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
911 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500912{
Chris Masone20d96d2007-03-22 12:13:20 -0400913 struct buffer_head *right_buf = path->nodes[0];
914 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
915 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400916 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500917 int slot;
918 int i;
919 int free_space;
920 int push_space = 0;
921 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400922 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400923 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500924 int ret = 0;
925 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500926
927 slot = path->slots[1];
928 if (slot == 0) {
929 return 1;
930 }
931 if (!path->nodes[1]) {
932 return 1;
933 }
Chris Masone20d96d2007-03-22 12:13:20 -0400934 t = read_tree_block(root,
935 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
936 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400937 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400938 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400939 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500940 return 1;
941 }
Chris Mason02217ed2007-03-02 16:08:05 -0500942
943 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400944 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Masone20d96d2007-03-22 12:13:20 -0400945 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -0400946 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400947 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400948 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500949 return 1;
950 }
951
Chris Mason7518a232007-03-12 12:01:18 -0400952 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500953 item = right->items + i;
954 if (path->slots[0] == i)
955 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400956 if (btrfs_item_size(item) + sizeof(*item) + push_space >
957 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500958 break;
959 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400960 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500961 }
962 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400963 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500964 return 1;
965 }
966 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400967 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400968 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -0400969 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400970 btrfs_item_offset(right->items + push_items -1);
Chris Mason123abc82007-03-14 14:14:43 -0400971 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
972 btrfs_leaf_data(right) +
973 btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500974 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400975 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500976 BUG_ON(old_left_nritems < 0);
977
Chris Mason0783fcf2007-03-12 20:12:07 -0400978 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -0400979 u32 ioff = btrfs_item_offset(left->items + i);
980 btrfs_set_item_offset(left->items + i, ioff -
981 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400982 btrfs_item_offset(left->items +
983 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500984 }
Chris Mason7518a232007-03-12 12:01:18 -0400985 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500986
987 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400988 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -0400989 leaf_data_end(root, right);
990 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
991 push_space, btrfs_leaf_data(right) +
992 leaf_data_end(root, right), push_space);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500993 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400994 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -0400995 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -0400996 btrfs_set_header_nritems(&right->header,
997 btrfs_header_nritems(&right->header) -
998 push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400999 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001000
Chris Mason7518a232007-03-12 12:01:18 -04001001 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001002 btrfs_set_item_offset(right->items + i, push_space -
1003 btrfs_item_size(right->items + i));
1004 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001005 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001006
Chris Masone20d96d2007-03-22 12:13:20 -04001007 BUG_ON(!buffer_dirty(t));
1008 BUG_ON(!buffer_dirty(right_buf));
Chris Masoneb60cea2007-02-02 09:18:22 -05001009
Chris Masone089f052007-03-16 16:20:31 -04001010 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001011 if (wret)
1012 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001013
1014 /* then fixup the leaf pointer in the path */
1015 if (path->slots[0] < push_items) {
1016 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001017 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001018 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001019 path->slots[1] -= 1;
1020 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001021 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001022 path->slots[0] -= push_items;
1023 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001024 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001025 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001026}
1027
Chris Mason74123bd2007-02-02 11:05:29 -05001028/*
1029 * split the path's leaf in two, making sure there is at least data_size
1030 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001031 *
1032 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001033 */
Chris Masone089f052007-03-16 16:20:31 -04001034static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1035 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001036{
Chris Masone20d96d2007-03-22 12:13:20 -04001037 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001038 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001039 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001040 int mid;
1041 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001042 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001043 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001044 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001045 int data_copy_size;
1046 int rt_data_off;
1047 int i;
1048 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001049 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001050
Chris Mason40689472007-03-17 14:29:23 -04001051 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001052 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001053 if (wret < 0)
1054 return wret;
1055 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001056 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001057 if (wret < 0)
1058 return wret;
1059 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001060 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001061 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001062
1063 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001064 if (btrfs_leaf_free_space(root, l) >=
1065 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001066 return 0;
1067
Chris Mason5c680ed2007-02-22 11:39:13 -05001068 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001069 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001070 if (ret)
1071 return ret;
1072 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001073 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001074 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001075 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001076 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001077 BUG_ON(!right_buffer);
1078 BUG_ON(mid == nritems);
Chris Masone20d96d2007-03-22 12:13:20 -04001079 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001080 memset(&right->header, 0, sizeof(right->header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001081 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001082 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001083 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001084 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001085 BUG();
1086 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001087 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001088 if (leaf_space_used(l, 0, mid + 1) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001089 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001090 BUG();
1091 }
Chris Mason7518a232007-03-12 12:01:18 -04001092 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Masone20d96d2007-03-22 12:13:20 -04001093 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
Chris Mason7518a232007-03-12 12:01:18 -04001094 btrfs_set_header_level(&right->header, 0);
1095 btrfs_set_header_parentid(&right->header,
Chris Masone20d96d2007-03-22 12:13:20 -04001096 btrfs_header_parentid(btrfs_buffer_header(root->node)));
Chris Mason123abc82007-03-14 14:14:43 -04001097 data_copy_size = btrfs_item_end(l->items + mid) -
1098 leaf_data_end(root, l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001100 (nritems - mid) * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001101 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1102 data_copy_size, btrfs_leaf_data(l) +
1103 leaf_data_end(root, l), data_copy_size);
1104 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1105 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001106
Chris Mason0783fcf2007-03-12 20:12:07 -04001107 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001108 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001109 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1110 }
Chris Mason74123bd2007-02-02 11:05:29 -05001111
Chris Mason7518a232007-03-12 12:01:18 -04001112 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001113 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001114 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Masone20d96d2007-03-22 12:13:20 -04001115 right_buffer->b_blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001116 if (wret)
1117 ret = wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001118 BUG_ON(!buffer_dirty(right_buffer));
1119 BUG_ON(!buffer_dirty(l_buf));
Chris Masoneb60cea2007-02-02 09:18:22 -05001120 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001121 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001122 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001123 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001124 path->slots[0] -= mid;
1125 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001126 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001127 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001128 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001129 return ret;
1130}
1131
Chris Mason74123bd2007-02-02 11:05:29 -05001132/*
1133 * Given a key and some data, insert an item into the tree.
1134 * This does all the path init required, making room in the tree if needed.
1135 */
Chris Masone089f052007-03-16 16:20:31 -04001136int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1137 *root, struct btrfs_path *path, struct btrfs_key
1138 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001139{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001140 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001141 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001142 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001143 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001144 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001145 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001146 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001147 struct btrfs_disk_key disk_key;
1148
1149 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001150
Chris Mason74123bd2007-02-02 11:05:29 -05001151 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001152 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001153 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001154 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001155 if (ret == 0) {
Chris Mason62e27492007-03-15 12:56:47 -04001156 btrfs_release_path(root, path);
Chris Masonf0930a32007-03-02 09:47:58 -05001157 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001158 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001159 if (ret < 0)
1160 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001161
Chris Mason62e27492007-03-15 12:56:47 -04001162 slot_orig = path->slots[0];
1163 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001164 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001165
Chris Mason7518a232007-03-12 12:01:18 -04001166 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001167 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001168
Chris Mason123abc82007-03-14 14:14:43 -04001169 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason234b63a2007-03-13 10:46:10 -04001170 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001171 BUG();
1172
Chris Mason62e27492007-03-15 12:56:47 -04001173 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001174 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001175 if (slot != nritems) {
1176 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001177 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001178
1179 /*
1180 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1181 */
1182 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001183 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001184 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001185 btrfs_set_item_offset(leaf->items + i,
1186 ioff - data_size);
1187 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001188
1189 /* shift the items */
1190 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001191 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001192
1193 /* shift the data */
Chris Mason123abc82007-03-14 14:14:43 -04001194 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1195 btrfs_leaf_data(leaf) +
Chris Masonbe0e5c02007-01-26 15:51:26 -05001196 data_end, old_data - data_end);
1197 data_end = old_data;
1198 }
Chris Mason62e27492007-03-15 12:56:47 -04001199 /* setup the item for the new data */
Chris Masone2fa7222007-03-12 16:22:34 -04001200 memcpy(&leaf->items[slot].key, &disk_key,
1201 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001202 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1203 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001204 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001205
1206 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001207 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001208 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001209
Chris Masone20d96d2007-03-22 12:13:20 -04001210 BUG_ON(!buffer_dirty(leaf_buf));
Chris Mason123abc82007-03-14 14:14:43 -04001211 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001212 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001213 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001214out:
Chris Mason62e27492007-03-15 12:56:47 -04001215 return ret;
1216}
1217
1218/*
1219 * Given a key and some data, insert an item into the tree.
1220 * This does all the path init required, making room in the tree if needed.
1221 */
Chris Masone089f052007-03-16 16:20:31 -04001222int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1223 *root, struct btrfs_key *cpu_key, void *data, u32
1224 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001225{
1226 int ret = 0;
1227 struct btrfs_path path;
1228 u8 *ptr;
1229
1230 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -04001231 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001232 if (!ret) {
Chris Masone20d96d2007-03-22 12:13:20 -04001233 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
1234 path.slots[0], u8);
Chris Mason62e27492007-03-15 12:56:47 -04001235 memcpy(ptr, data, data_size);
1236 }
Chris Mason234b63a2007-03-13 10:46:10 -04001237 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001238 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001239}
1240
Chris Mason74123bd2007-02-02 11:05:29 -05001241/*
Chris Mason5de08d72007-02-24 06:24:44 -05001242 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001243 *
1244 * If the delete empties a node, the node is removed from the tree,
1245 * continuing all the way the root if required. The root is converted into
1246 * a leaf if all the nodes are emptied.
1247 */
Chris Masone089f052007-03-16 16:20:31 -04001248static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1249 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001250{
Chris Mason234b63a2007-03-13 10:46:10 -04001251 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001252 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001253 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001254 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001255 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001256
Chris Masone20d96d2007-03-22 12:13:20 -04001257 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001258 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001259 if (slot != nritems -1) {
Chris Mason123abc82007-03-14 14:14:43 -04001260 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1261 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001262 }
Chris Mason7518a232007-03-12 12:01:18 -04001263 nritems--;
1264 btrfs_set_header_nritems(&node->header, nritems);
1265 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001266 struct btrfs_header *header = btrfs_buffer_header(root->node);
1267 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001268 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001269 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001270 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001271 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001272 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001273 if (wret)
1274 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001275 }
Chris Masone20d96d2007-03-22 12:13:20 -04001276 BUG_ON(!buffer_dirty(parent));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001277 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001278}
1279
Chris Mason74123bd2007-02-02 11:05:29 -05001280/*
1281 * delete the item at the leaf level in path. If that empties
1282 * the leaf, remove it from the tree
1283 */
Chris Masone089f052007-03-16 16:20:31 -04001284int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1285 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001286{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001287 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001288 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001289 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001290 int doff;
1291 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001292 int ret = 0;
1293 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001294 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001295
Chris Masoneb60cea2007-02-02 09:18:22 -05001296 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001297 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001298 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001299 doff = btrfs_item_offset(leaf->items + slot);
1300 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001301 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001302
Chris Mason7518a232007-03-12 12:01:18 -04001303 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001304 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001305 int data_end = leaf_data_end(root, leaf);
1306 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1307 btrfs_leaf_data(leaf) + data_end,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001308 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001309 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001310 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001311 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1312 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001313 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001314 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001315 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001316 }
Chris Mason7518a232007-03-12 12:01:18 -04001317 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1318 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001319 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001320 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001321 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001322 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001323 } else {
Chris Masone089f052007-03-16 16:20:31 -04001324 clean_tree_block(trans, root, leaf_buf);
1325 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001326 if (wret)
1327 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001328 wret = btrfs_free_extent(trans, root,
Chris Masone20d96d2007-03-22 12:13:20 -04001329 leaf_buf->b_blocknr, 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001330 if (wret)
1331 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001332 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001333 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001334 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001335 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001336 wret = fixup_low_keys(trans, root, path,
1337 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001338 if (wret)
1339 ret = wret;
1340 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001341
Chris Mason74123bd2007-02-02 11:05:29 -05001342 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001343 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001344 /* push_leaf_left fixes the path.
1345 * make sure the path still points to our leaf
1346 * for possible call to del_ptr below
1347 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001348 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001349 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001350 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001351 if (wret < 0)
1352 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001353 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001354 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001355 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001356 if (wret < 0)
1357 ret = wret;
1358 }
Chris Mason7518a232007-03-12 12:01:18 -04001359 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Masone20d96d2007-03-22 12:13:20 -04001360 u64 blocknr = leaf_buf->b_blocknr;
Chris Masone089f052007-03-16 16:20:31 -04001361 clean_tree_block(trans, root, leaf_buf);
1362 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001363 if (wret)
1364 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001365 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001366 wret = btrfs_free_extent(trans, root, blocknr,
1367 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001368 if (wret)
1369 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001370 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001371 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001372 }
1373 }
1374 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001375 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001376}
1377
Chris Mason97571fd2007-02-24 13:39:08 -05001378/*
1379 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001380 * returns 0 if it found something or 1 if there are no greater leaves.
1381 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001382 */
Chris Mason234b63a2007-03-13 10:46:10 -04001383int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001384{
1385 int slot;
1386 int level = 1;
1387 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001388 struct buffer_head *c;
1389 struct btrfs_node *c_node;
1390 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001391
Chris Mason234b63a2007-03-13 10:46:10 -04001392 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001393 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001394 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001395 slot = path->slots[level] + 1;
1396 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001397 c_node = btrfs_buffer_node(c);
1398 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001399 level++;
1400 continue;
1401 }
Chris Masone20d96d2007-03-22 12:13:20 -04001402 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001403 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001404 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001405 next = read_tree_block(root, blocknr);
1406 break;
1407 }
1408 path->slots[level] = slot;
1409 while(1) {
1410 level--;
1411 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001412 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001413 path->nodes[level] = next;
1414 path->slots[level] = 0;
1415 if (!level)
1416 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001417 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001418 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001419 }
1420 return 0;
1421}