blob: 9fbd07c37fde77e83e69d61d55477555ed62e0d3 [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
10 *root, struct btrfs_buffer *dst, struct btrfs_buffer
11 *src);
12static int balance_node_right(struct btrfs_trans_handle *trans, struct
13 btrfs_root *root, struct btrfs_buffer *dst_buf,
Chris Mason234b63a2007-03-13 10:46:10 -040014 struct btrfs_buffer *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
35 *root, struct btrfs_buffer *buf, struct btrfs_buffer
36 *parent, int parent_slot, struct btrfs_buffer
37 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050038{
Chris Mason234b63a2007-03-13 10:46:10 -040039 struct btrfs_buffer *cow;
Chris Mason02217ed2007-03-02 16:08:05 -050040
41 if (!list_empty(&buf->dirty)) {
42 *cow_ret = buf;
43 return 0;
44 }
Chris Masone089f052007-03-16 16:20:31 -040045 cow = btrfs_alloc_free_block(trans, root);
Chris Mason123abc82007-03-14 14:14:43 -040046 memcpy(&cow->node, &buf->node, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -040047 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050048 *cow_ret = cow;
Chris Masone089f052007-03-16 16:20:31 -040049 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050050 if (buf == root->node) {
51 root->node = cow;
52 cow->count++;
Chris Masona28ec192007-03-06 20:08:01 -050053 if (buf != root->commit_root)
Chris Masone089f052007-03-16 16:20:31 -040054 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -040055 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050056 } else {
Chris Mason1d4f8a02007-03-13 09:28:32 -040057 btrfs_set_node_blockptr(&parent->node, parent_slot,
58 cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050059 BUG_ON(list_empty(&parent->dirty));
Chris Masone089f052007-03-16 16:20:31 -040060 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050061 }
Chris Mason234b63a2007-03-13 10:46:10 -040062 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050063 return 0;
64}
65
Chris Mason74123bd2007-02-02 11:05:29 -050066/*
67 * The leaf data grows from end-to-front in the node.
68 * this returns the address of the start of the last item,
69 * which is the stop of the leaf data stack
70 */
Chris Mason123abc82007-03-14 14:14:43 -040071static inline unsigned int leaf_data_end(struct btrfs_root *root,
72 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050073{
Chris Mason7518a232007-03-12 12:01:18 -040074 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050075 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -040076 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -040077 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050078}
79
Chris Mason74123bd2007-02-02 11:05:29 -050080/*
81 * The space between the end of the leaf items and
82 * the start of the leaf data. IOW, how much room
83 * the leaf has left for both items and data
84 */
Chris Mason123abc82007-03-14 14:14:43 -040085int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050086{
Chris Mason123abc82007-03-14 14:14:43 -040087 int data_end = leaf_data_end(root, leaf);
Chris Mason7518a232007-03-12 12:01:18 -040088 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050089 char *items_end = (char *)(leaf->items + nritems + 1);
Chris Mason123abc82007-03-14 14:14:43 -040090 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
Chris Masonbe0e5c02007-01-26 15:51:26 -050091}
92
Chris Mason74123bd2007-02-02 11:05:29 -050093/*
94 * compare two keys in a memcmp fashion
95 */
Chris Mason9aca1d52007-03-13 11:09:37 -040096static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -050097{
Chris Masone2fa7222007-03-12 16:22:34 -040098 struct btrfs_key k1;
99
100 btrfs_disk_key_to_cpu(&k1, disk);
101
102 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500103 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400104 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500105 return -1;
Chris Mason62e27492007-03-15 12:56:47 -0400106 if (k1.flags > k2->flags)
107 return 1;
108 if (k1.flags < k2->flags)
109 return -1;
Chris Masona8a2ee02007-03-16 08:46:49 -0400110 if (k1.offset > k2->offset)
111 return 1;
112 if (k1.offset < k2->offset)
113 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500114 return 0;
115}
Chris Mason74123bd2007-02-02 11:05:29 -0500116
Chris Mason123abc82007-03-14 14:14:43 -0400117static int check_node(struct btrfs_root *root, struct btrfs_path *path,
118 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500119{
120 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400121 struct btrfs_node *parent = NULL;
122 struct btrfs_node *node = &path->nodes[level]->node;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500123 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400124 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500125
126 if (path->nodes[level + 1])
127 parent = &path->nodes[level + 1]->node;
128 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400129 BUG_ON(nritems == 0);
130 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400131 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400132 parent_key = &parent->ptrs[parent_slot].key;
133 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400134 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400135 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400136 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500137 }
Chris Mason123abc82007-03-14 14:14:43 -0400138 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400139 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400140 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400141 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
142 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500143 }
144 return 0;
145}
146
Chris Mason123abc82007-03-14 14:14:43 -0400147static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
148 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500149{
150 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400151 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
152 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500153 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400154 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500155
156 if (path->nodes[level + 1])
157 parent = &path->nodes[level + 1]->node;
158 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400159 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400160
161 if (nritems == 0)
162 return 0;
163
164 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400165 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400166 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500167 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400168 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400169 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400170 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500171 }
Chris Mason7518a232007-03-12 12:01:18 -0400172 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400173 struct btrfs_key cpukey;
174 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500175 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400176 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400177 BUG_ON(btrfs_item_offset(leaf->items + i) !=
178 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500179 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400180 BUG_ON(btrfs_item_offset(leaf->items + i) +
181 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400182 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500183 }
184 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500185 return 0;
186}
187
Chris Mason123abc82007-03-14 14:14:43 -0400188static int check_block(struct btrfs_root *root, struct btrfs_path *path,
189 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500190{
191 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400192 return check_leaf(root, path, level);
193 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500194}
195
Chris Mason74123bd2007-02-02 11:05:29 -0500196/*
197 * search for key in the array p. items p are item_size apart
198 * and there are 'max' items in p
199 * the slot in the array is returned via slot, and it points to
200 * the place where you would insert key if it is not found in
201 * the array.
202 *
203 * slot may point to max if the key is bigger than all of the keys
204 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400205static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500206 int max, int *slot)
207{
208 int low = 0;
209 int high = max;
210 int mid;
211 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400212 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500213
214 while(low < high) {
215 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400216 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500217 ret = comp_keys(tmp, key);
218
219 if (ret < 0)
220 low = mid + 1;
221 else if (ret > 0)
222 high = mid;
223 else {
224 *slot = mid;
225 return 0;
226 }
227 }
228 *slot = low;
229 return 1;
230}
231
Chris Mason97571fd2007-02-24 13:39:08 -0500232/*
233 * simple bin_search frontend that does the right thing for
234 * leaves vs nodes
235 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400236static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500237{
Chris Mason7518a232007-03-12 12:01:18 -0400238 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400239 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400240 return generic_bin_search((void *)l->items,
241 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400242 key, btrfs_header_nritems(&c->header),
243 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500244 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400245 return generic_bin_search((void *)c->ptrs,
246 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400247 key, btrfs_header_nritems(&c->header),
248 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500249 }
250 return -1;
251}
252
Chris Mason9aca1d52007-03-13 11:09:37 -0400253static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason234b63a2007-03-13 10:46:10 -0400254 struct btrfs_buffer *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500255 int slot)
256{
Chris Mason234b63a2007-03-13 10:46:10 -0400257 struct btrfs_node *node = &parent_buf->node;
Chris Masonbb803952007-03-01 12:04:21 -0500258 if (slot < 0)
259 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400260 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500261 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400262 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500263}
264
Chris Masone089f052007-03-16 16:20:31 -0400265static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
266 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500267{
Chris Mason234b63a2007-03-13 10:46:10 -0400268 struct btrfs_buffer *right_buf;
269 struct btrfs_buffer *mid_buf;
270 struct btrfs_buffer *left_buf;
271 struct btrfs_buffer *parent_buf = NULL;
272 struct btrfs_node *right = NULL;
273 struct btrfs_node *mid;
274 struct btrfs_node *left = NULL;
275 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500276 int ret = 0;
277 int wret;
278 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500279 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500280 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500281
282 if (level == 0)
283 return 0;
284
285 mid_buf = path->nodes[level];
286 mid = &mid_buf->node;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400287 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500288
Chris Mason234b63a2007-03-13 10:46:10 -0400289 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500290 parent_buf = path->nodes[level + 1];
291 pslot = path->slots[level + 1];
292
Chris Mason40689472007-03-17 14:29:23 -0400293 /*
294 * deal with the case where there is only one pointer in the root
295 * by promoting the node below to a root
296 */
Chris Masonbb803952007-03-01 12:04:21 -0500297 if (!parent_buf) {
Chris Mason234b63a2007-03-13 10:46:10 -0400298 struct btrfs_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500299 u64 blocknr = mid_buf->blocknr;
300
Chris Mason7518a232007-03-12 12:01:18 -0400301 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500302 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 Mason234b63a2007-03-13 10:46:10 -0400310 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500311 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400312 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400313 clean_tree_block(trans, root, mid_buf);
314 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500315 }
316 parent = &parent_buf->node;
317
Chris Mason123abc82007-03-14 14:14:43 -0400318 if (btrfs_header_nritems(&mid->header) >
319 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500320 return 0;
321
Chris Masonbb803952007-03-01 12:04:21 -0500322 left_buf = read_node_slot(root, parent_buf, pslot - 1);
323 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500324
325 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500326 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400327 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
328 &left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500329 left = &left_buf->node;
Chris Mason7518a232007-03-12 12:01:18 -0400330 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400331 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500332 if (wret < 0)
333 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500334 }
Chris Mason79f95c82007-03-01 15:16:26 -0500335
336 /*
337 * then try to empty the right most buffer into the middle
338 */
Chris Masonbb803952007-03-01 12:04:21 -0500339 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400340 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
341 &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500342 right = &right_buf->node;
Chris Masone089f052007-03-16 16:20:31 -0400343 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500344 if (wret < 0)
345 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400346 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masonbb803952007-03-01 12:04:21 -0500347 u64 blocknr = right_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400348 btrfs_block_release(root, right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400349 clean_tree_block(trans, root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500350 right_buf = NULL;
351 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400352 wret = del_ptr(trans, root, path, level + 1, pslot +
353 1);
Chris Masonbb803952007-03-01 12:04:21 -0500354 if (wret)
355 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400356 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500357 if (wret)
358 ret = wret;
359 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400360 memcpy(&parent->ptrs[pslot + 1].key,
361 &right->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400362 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500363 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500364 }
365 }
Chris Mason7518a232007-03-12 12:01:18 -0400366 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500367 /*
368 * we're not allowed to leave a node with one item in the
369 * tree during a delete. A deletion from lower in the tree
370 * could try to delete the only pointer in this node.
371 * So, pull some keys from the left.
372 * There has to be a left pointer at this point because
373 * otherwise we would have pulled some pointers from the
374 * right
375 */
376 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400377 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500378 if (wret < 0)
379 ret = wret;
380 BUG_ON(wret == 1);
381 }
Chris Mason7518a232007-03-12 12:01:18 -0400382 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500383 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500384 u64 blocknr = mid_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400385 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400386 clean_tree_block(trans, root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500387 mid_buf = NULL;
388 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400389 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500390 if (wret)
391 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400392 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500393 if (wret)
394 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500395 } else {
396 /* update the parent key to reflect our changes */
Chris Mason123abc82007-03-14 14:14:43 -0400397 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400398 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500399 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500400 }
Chris Masonbb803952007-03-01 12:04:21 -0500401
Chris Mason79f95c82007-03-01 15:16:26 -0500402 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500403 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400404 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500405 left_buf->count++; // released below
406 path->nodes[level] = left_buf;
407 path->slots[level + 1] -= 1;
408 path->slots[level] = orig_slot;
409 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400410 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500411 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400412 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500413 path->slots[level] = orig_slot;
414 }
415 }
Chris Mason79f95c82007-03-01 15:16:26 -0500416 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400417 check_block(root, path, level);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400418 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
419 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500420 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500421
422 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400423 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500424 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400425 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500426 return ret;
427}
428
Chris Mason74123bd2007-02-02 11:05:29 -0500429/*
430 * look for key in the tree. path is filled in with nodes along the way
431 * if key is found, we return zero and you can find the item in the leaf
432 * level of the path (level 0)
433 *
434 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500435 * be inserted, and 1 is returned. If there are other errors during the
436 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500437 *
438 * if ins_len > 0, nodes and leaves will be split as we walk down the
439 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
440 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500441 */
Chris Masone089f052007-03-16 16:20:31 -0400442int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
443 *root, struct btrfs_key *key, struct btrfs_path *p, int
444 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500445{
Chris Mason234b63a2007-03-13 10:46:10 -0400446 struct btrfs_buffer *b;
447 struct btrfs_buffer *cow_buf;
448 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500449 int slot;
450 int ret;
451 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500452
Chris Masonbb803952007-03-01 12:04:21 -0500453again:
454 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500455 b->count++;
456 while (b) {
Chris Mason7518a232007-03-12 12:01:18 -0400457 level = btrfs_header_level(&b->node.header);
Chris Mason02217ed2007-03-02 16:08:05 -0500458 if (cow) {
459 int wret;
Chris Masone089f052007-03-16 16:20:31 -0400460 wret = btrfs_cow_block(trans, root, b, p->nodes[level +
461 1], p->slots[level + 1],
462 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500463 b = cow_buf;
464 }
465 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500466 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500467 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400468 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500469 if (ret)
470 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500471 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400472 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500473 if (ret && slot > 0)
474 slot -= 1;
475 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400476 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
Chris Mason123abc82007-03-14 14:14:43 -0400477 BTRFS_NODEPTRS_PER_BLOCK(root)) {
Chris Masone089f052007-03-16 16:20:31 -0400478 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500479 BUG_ON(sret > 0);
480 if (sret)
481 return sret;
482 b = p->nodes[level];
483 c = &b->node;
484 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500485 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400486 int sret = balance_level(trans, root, p,
487 level);
Chris Masonbb803952007-03-01 12:04:21 -0500488 if (sret)
489 return sret;
490 b = p->nodes[level];
491 if (!b)
492 goto again;
493 c = &b->node;
494 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400495 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500496 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400497 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500498 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400499 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500500 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400501 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400502 sizeof(struct btrfs_item) + ins_len) {
Chris Masone089f052007-03-16 16:20:31 -0400503 int sret = split_leaf(trans, root, p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500504 BUG_ON(sret > 0);
505 if (sret)
506 return sret;
507 }
Chris Masonbb803952007-03-01 12:04:21 -0500508 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500509 return ret;
510 }
511 }
Chris Masonbb803952007-03-01 12:04:21 -0500512 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500513 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500514}
515
Chris Mason74123bd2007-02-02 11:05:29 -0500516/*
517 * adjust the pointers going up the tree, starting at level
518 * making sure the right key of each node is points to 'key'.
519 * This is used after shifting pointers to the left, so it stops
520 * fixing up pointers when a given leaf/node is not in slot 0 of the
521 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500522 *
523 * If this fails to write a tree block, it returns -1, but continues
524 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500525 */
Chris Masone089f052007-03-16 16:20:31 -0400526static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
527 *root, struct btrfs_path *path, struct btrfs_disk_key
528 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500529{
530 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500531 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400532 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
533 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500534 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500535 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500536 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500537 t = &path->nodes[i]->node;
Chris Mason123abc82007-03-14 14:14:43 -0400538 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500539 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500540 if (tslot != 0)
541 break;
542 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500543 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500544}
545
Chris Mason74123bd2007-02-02 11:05:29 -0500546/*
547 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500548 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500549 *
550 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
551 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500552 */
Chris Masone089f052007-03-16 16:20:31 -0400553static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
554 *root, struct btrfs_buffer *dst_buf, struct
555 btrfs_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500556{
Chris Mason234b63a2007-03-13 10:46:10 -0400557 struct btrfs_node *src = &src_buf->node;
558 struct btrfs_node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500559 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500560 int src_nritems;
561 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500562 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563
Chris Mason7518a232007-03-12 12:01:18 -0400564 src_nritems = btrfs_header_nritems(&src->header);
565 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400566 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500567 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500568 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500569 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500570
571 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500572 push_items = src_nritems;
573
Chris Mason123abc82007-03-14 14:14:43 -0400574 memcpy(dst->ptrs + dst_nritems, src->ptrs,
575 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500576 if (push_items < src_nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400577 memmove(src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400578 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400579 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500580 }
Chris Mason7518a232007-03-12 12:01:18 -0400581 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
582 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason02217ed2007-03-02 16:08:05 -0500583 BUG_ON(list_empty(&src_buf->dirty));
584 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500585 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500586}
587
Chris Mason97571fd2007-02-24 13:39:08 -0500588/*
Chris Mason79f95c82007-03-01 15:16:26 -0500589 * try to push data from one node into the next node right in the
590 * tree.
591 *
592 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
593 * error, and > 0 if there was no room in the right hand block.
594 *
595 * this will only push up to 1/2 the contents of the left node over
596 */
Chris Masone089f052007-03-16 16:20:31 -0400597static int balance_node_right(struct btrfs_trans_handle *trans, struct
598 btrfs_root *root, struct btrfs_buffer *dst_buf,
Chris Mason234b63a2007-03-13 10:46:10 -0400599 struct btrfs_buffer *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500600{
Chris Mason234b63a2007-03-13 10:46:10 -0400601 struct btrfs_node *src = &src_buf->node;
602 struct btrfs_node *dst = &dst_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500603 int push_items = 0;
604 int max_push;
605 int src_nritems;
606 int dst_nritems;
607 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500608
Chris Mason7518a232007-03-12 12:01:18 -0400609 src_nritems = btrfs_header_nritems(&src->header);
610 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400611 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500612 if (push_items <= 0) {
613 return 1;
614 }
615
616 max_push = src_nritems / 2 + 1;
617 /* don't try to empty the node */
618 if (max_push > src_nritems)
619 return 1;
620 if (max_push < push_items)
621 push_items = max_push;
622
Chris Mason123abc82007-03-14 14:14:43 -0400623 memmove(dst->ptrs + push_items, dst->ptrs,
624 dst_nritems * sizeof(struct btrfs_key_ptr));
625 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
626 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500627
Chris Mason7518a232007-03-12 12:01:18 -0400628 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
629 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500630
Chris Mason02217ed2007-03-02 16:08:05 -0500631 BUG_ON(list_empty(&src_buf->dirty));
632 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500633 return ret;
634}
635
636/*
Chris Mason97571fd2007-02-24 13:39:08 -0500637 * helper function to insert a new root level in the tree.
638 * A new node is allocated, and a single item is inserted to
639 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500640 *
641 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500642 */
Chris Masone089f052007-03-16 16:20:31 -0400643static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
644 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500645{
Chris Mason234b63a2007-03-13 10:46:10 -0400646 struct btrfs_buffer *t;
647 struct btrfs_node *lower;
648 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400649 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500650
651 BUG_ON(path->nodes[level]);
652 BUG_ON(path->nodes[level-1] != root->node);
653
Chris Masone089f052007-03-16 16:20:31 -0400654 t = btrfs_alloc_free_block(trans, root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500655 c = &t->node;
Chris Mason123abc82007-03-14 14:14:43 -0400656 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400657 btrfs_set_header_nritems(&c->header, 1);
658 btrfs_set_header_level(&c->header, level);
659 btrfs_set_header_blocknr(&c->header, t->blocknr);
660 btrfs_set_header_parentid(&c->header,
661 btrfs_header_parentid(&root->node->node.header));
Chris Mason5c680ed2007-02-22 11:39:13 -0500662 lower = &path->nodes[level-1]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400663 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400664 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500665 else
Chris Mason123abc82007-03-14 14:14:43 -0400666 lower_key = &lower->ptrs[0].key;
667 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400668 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
Chris Mason5c680ed2007-02-22 11:39:13 -0500669 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400670 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500671 root->node = t;
672 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500673 path->nodes[level] = t;
674 path->slots[level] = 0;
675 return 0;
676}
677
Chris Mason74123bd2007-02-02 11:05:29 -0500678/*
679 * worker function to insert a single pointer in a node.
680 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500681 *
Chris Mason74123bd2007-02-02 11:05:29 -0500682 * slot and level indicate where you want the key to go, and
683 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500684 *
685 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500686 */
Chris Masone089f052007-03-16 16:20:31 -0400687static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
688 *root, struct btrfs_path *path, struct btrfs_disk_key
689 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500690{
Chris Mason234b63a2007-03-13 10:46:10 -0400691 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500692 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500693
694 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500695 lower = &path->nodes[level]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400696 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500697 if (slot > nritems)
698 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400699 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500700 BUG();
701 if (slot != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400702 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
703 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500704 }
Chris Mason123abc82007-03-14 14:14:43 -0400705 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400706 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400707 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Mason02217ed2007-03-02 16:08:05 -0500708 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500709 return 0;
710}
711
Chris Mason97571fd2007-02-24 13:39:08 -0500712/*
713 * split the node at the specified level in path in two.
714 * The path is corrected to point to the appropriate node after the split
715 *
716 * Before splitting this tries to make some room in the node by pushing
717 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500718 *
719 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500720 */
Chris Masone089f052007-03-16 16:20:31 -0400721static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
722 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723{
Chris Mason234b63a2007-03-13 10:46:10 -0400724 struct btrfs_buffer *t;
725 struct btrfs_node *c;
726 struct btrfs_buffer *split_buffer;
727 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500729 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500730 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400731 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500732
Chris Mason5c680ed2007-02-22 11:39:13 -0500733 t = path->nodes[level];
734 c = &t->node;
735 if (t == root->node) {
736 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400737 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500738 if (ret)
739 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500740 }
Chris Mason7518a232007-03-12 12:01:18 -0400741 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400742 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500743 split = &split_buffer->node;
Chris Mason7518a232007-03-12 12:01:18 -0400744 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
745 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
746 btrfs_set_header_parentid(&split->header,
747 btrfs_header_parentid(&root->node->node.header));
748 mid = (c_nritems + 1) / 2;
Chris Mason123abc82007-03-14 14:14:43 -0400749 memcpy(split->ptrs, c->ptrs + mid,
750 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400751 btrfs_set_header_nritems(&split->header, c_nritems - mid);
752 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500753 ret = 0;
754
Chris Mason02217ed2007-03-02 16:08:05 -0500755 BUG_ON(list_empty(&t->dirty));
Chris Masone089f052007-03-16 16:20:31 -0400756 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -0400757 split_buffer->blocknr, path->slots[level + 1] + 1,
758 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500759 if (wret)
760 ret = wret;
761
Chris Mason5de08d72007-02-24 06:24:44 -0500762 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500763 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400764 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500765 path->nodes[level] = split_buffer;
766 path->slots[level + 1] += 1;
767 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400768 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500769 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500770 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500771}
772
Chris Mason74123bd2007-02-02 11:05:29 -0500773/*
774 * how many bytes are required to store the items in a leaf. start
775 * and nr indicate which items in the leaf to check. This totals up the
776 * space used both by the item structs and the item data
777 */
Chris Mason234b63a2007-03-13 10:46:10 -0400778static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500779{
780 int data_len;
781 int end = start + nr - 1;
782
783 if (!nr)
784 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400785 data_len = btrfs_item_end(l->items + start);
786 data_len = data_len - btrfs_item_offset(l->items + end);
787 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500788 return data_len;
789}
790
Chris Mason74123bd2007-02-02 11:05:29 -0500791/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500792 * push some data in the path leaf to the right, trying to free up at
793 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500794 *
795 * returns 1 if the push failed because the other node didn't have enough
796 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500797 */
Chris Masone089f052007-03-16 16:20:31 -0400798static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
799 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500800{
Chris Mason234b63a2007-03-13 10:46:10 -0400801 struct btrfs_buffer *left_buf = path->nodes[0];
802 struct btrfs_leaf *left = &left_buf->leaf;
803 struct btrfs_leaf *right;
804 struct btrfs_buffer *right_buf;
805 struct btrfs_buffer *upper;
Chris Mason00ec4c52007-02-24 12:47:20 -0500806 int slot;
807 int i;
808 int free_space;
809 int push_space = 0;
810 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400811 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400812 u32 left_nritems;
813 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500814
815 slot = path->slots[1];
816 if (!path->nodes[1]) {
817 return 1;
818 }
819 upper = path->nodes[1];
Chris Mason7518a232007-03-12 12:01:18 -0400820 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500821 return 1;
822 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400823 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
824 slot + 1));
Chris Mason00ec4c52007-02-24 12:47:20 -0500825 right = &right_buf->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400826 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400827 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400828 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500829 return 1;
830 }
Chris Mason02217ed2007-03-02 16:08:05 -0500831 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400832 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500833 right = &right_buf->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400834 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400835 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400836 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500837 return 1;
838 }
839
Chris Mason7518a232007-03-12 12:01:18 -0400840 left_nritems = btrfs_header_nritems(&left->header);
841 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500842 item = left->items + i;
843 if (path->slots[0] == i)
844 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400845 if (btrfs_item_size(item) + sizeof(*item) + push_space >
846 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500847 break;
848 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400849 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500850 }
851 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400852 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500853 return 1;
854 }
Chris Mason7518a232007-03-12 12:01:18 -0400855 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500856 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400857 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400858 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500859 /* make room in the right data area */
Chris Mason123abc82007-03-14 14:14:43 -0400860 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
861 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
862 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500863 /* copy from the left data area */
Chris Mason123abc82007-03-14 14:14:43 -0400864 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
865 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
Chris Mason00ec4c52007-02-24 12:47:20 -0500866 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400867 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500868 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400869 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400870 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500871
872 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400873 right_nritems += push_items;
874 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400875 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400876 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400877 btrfs_set_item_offset(right->items + i, push_space -
878 btrfs_item_size(right->items + i));
879 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500880 }
Chris Mason7518a232007-03-12 12:01:18 -0400881 left_nritems -= push_items;
882 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500883
Chris Mason02217ed2007-03-02 16:08:05 -0500884 BUG_ON(list_empty(&left_buf->dirty));
885 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason123abc82007-03-14 14:14:43 -0400886 memcpy(&upper->node.ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400887 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500888 BUG_ON(list_empty(&upper->dirty));
889
Chris Mason00ec4c52007-02-24 12:47:20 -0500890 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400891 if (path->slots[0] >= left_nritems) {
892 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400893 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500894 path->nodes[0] = right_buf;
895 path->slots[1] += 1;
896 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400897 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500898 }
899 return 0;
900}
901/*
Chris Mason74123bd2007-02-02 11:05:29 -0500902 * push some data in the path leaf to the left, trying to free up at
903 * least data_size bytes. returns zero if the push worked, nonzero otherwise
904 */
Chris Masone089f052007-03-16 16:20:31 -0400905static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
906 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500907{
Chris Mason234b63a2007-03-13 10:46:10 -0400908 struct btrfs_buffer *right_buf = path->nodes[0];
909 struct btrfs_leaf *right = &right_buf->leaf;
910 struct btrfs_buffer *t;
911 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500912 int slot;
913 int i;
914 int free_space;
915 int push_space = 0;
916 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400917 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400918 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500919 int ret = 0;
920 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500921
922 slot = path->slots[1];
923 if (slot == 0) {
924 return 1;
925 }
926 if (!path->nodes[1]) {
927 return 1;
928 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400929 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
930 slot - 1));
Chris Masoneb60cea2007-02-02 09:18:22 -0500931 left = &t->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400932 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400933 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400934 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500935 return 1;
936 }
Chris Mason02217ed2007-03-02 16:08:05 -0500937
938 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400939 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Mason02217ed2007-03-02 16:08:05 -0500940 left = &t->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400941 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400942 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400943 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500944 return 1;
945 }
946
Chris Mason7518a232007-03-12 12:01:18 -0400947 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948 item = right->items + i;
949 if (path->slots[0] == i)
950 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400951 if (btrfs_item_size(item) + sizeof(*item) + push_space >
952 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500953 break;
954 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400955 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500956 }
957 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400958 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500959 return 1;
960 }
961 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400962 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400963 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -0400964 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400965 btrfs_item_offset(right->items + push_items -1);
Chris Mason123abc82007-03-14 14:14:43 -0400966 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
967 btrfs_leaf_data(right) +
968 btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500969 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400970 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500971 BUG_ON(old_left_nritems < 0);
972
Chris Mason0783fcf2007-03-12 20:12:07 -0400973 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -0400974 u32 ioff = btrfs_item_offset(left->items + i);
975 btrfs_set_item_offset(left->items + i, ioff -
976 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400977 btrfs_item_offset(left->items +
978 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500979 }
Chris Mason7518a232007-03-12 12:01:18 -0400980 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500981
982 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400983 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -0400984 leaf_data_end(root, right);
985 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
986 push_space, btrfs_leaf_data(right) +
987 leaf_data_end(root, right), push_space);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500988 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400989 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -0400990 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -0400991 btrfs_set_header_nritems(&right->header,
992 btrfs_header_nritems(&right->header) -
993 push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400994 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500995
Chris Mason7518a232007-03-12 12:01:18 -0400996 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400997 btrfs_set_item_offset(right->items + i, push_space -
998 btrfs_item_size(right->items + i));
999 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001000 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001001
Chris Mason02217ed2007-03-02 16:08:05 -05001002 BUG_ON(list_empty(&t->dirty));
1003 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001004
Chris Masone089f052007-03-16 16:20:31 -04001005 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001006 if (wret)
1007 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001008
1009 /* then fixup the leaf pointer in the path */
1010 if (path->slots[0] < push_items) {
1011 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001012 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001013 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001014 path->slots[1] -= 1;
1015 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001016 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001017 path->slots[0] -= push_items;
1018 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001019 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001020 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001021}
1022
Chris Mason74123bd2007-02-02 11:05:29 -05001023/*
1024 * split the path's leaf in two, making sure there is at least data_size
1025 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001026 *
1027 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001028 */
Chris Masone089f052007-03-16 16:20:31 -04001029static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1030 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001031{
Chris Mason234b63a2007-03-13 10:46:10 -04001032 struct btrfs_buffer *l_buf;
1033 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001034 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001035 int mid;
1036 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001037 struct btrfs_leaf *right;
1038 struct btrfs_buffer *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001039 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001040 int data_copy_size;
1041 int rt_data_off;
1042 int i;
1043 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001044 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001045
Chris Mason40689472007-03-17 14:29:23 -04001046 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001047 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001048 if (wret < 0)
1049 return wret;
1050 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001051 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001052 if (wret < 0)
1053 return wret;
1054 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001055 l_buf = path->nodes[0];
1056 l = &l_buf->leaf;
1057
1058 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001059 if (btrfs_leaf_free_space(root, l) >=
1060 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001061 return 0;
1062
Chris Mason5c680ed2007-02-22 11:39:13 -05001063 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001064 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001065 if (ret)
1066 return ret;
1067 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001068 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001069 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001070 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001071 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001072 BUG_ON(!right_buffer);
1073 BUG_ON(mid == nritems);
1074 right = &right_buffer->leaf;
Chris Mason123abc82007-03-14 14:14:43 -04001075 memset(&right->header, 0, sizeof(right->header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001076 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001077 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001078 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001079 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001080 BUG();
1081 } else {
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, 0, mid + 1) + 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 }
Chris Mason7518a232007-03-12 12:01:18 -04001087 btrfs_set_header_nritems(&right->header, nritems - mid);
1088 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1089 btrfs_set_header_level(&right->header, 0);
1090 btrfs_set_header_parentid(&right->header,
1091 btrfs_header_parentid(&root->node->node.header));
Chris Mason123abc82007-03-14 14:14:43 -04001092 data_copy_size = btrfs_item_end(l->items + mid) -
1093 leaf_data_end(root, l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001094 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001095 (nritems - mid) * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001096 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1097 data_copy_size, btrfs_leaf_data(l) +
1098 leaf_data_end(root, l), data_copy_size);
1099 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1100 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001101
Chris Mason0783fcf2007-03-12 20:12:07 -04001102 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001103 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001104 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1105 }
Chris Mason74123bd2007-02-02 11:05:29 -05001106
Chris Mason7518a232007-03-12 12:01:18 -04001107 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001108 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001109 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001110 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001111 if (wret)
1112 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001113 BUG_ON(list_empty(&right_buffer->dirty));
1114 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001115 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001116 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001117 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001118 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001119 path->slots[0] -= mid;
1120 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001121 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001122 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001123 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001124 return ret;
1125}
1126
Chris Mason74123bd2007-02-02 11:05:29 -05001127/*
1128 * Given a key and some data, insert an item into the tree.
1129 * This does all the path init required, making room in the tree if needed.
1130 */
Chris Masone089f052007-03-16 16:20:31 -04001131int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1132 *root, struct btrfs_path *path, struct btrfs_key
1133 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001134{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001135 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001136 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001137 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001138 struct btrfs_leaf *leaf;
1139 struct btrfs_buffer *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001140 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001141 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001142 struct btrfs_disk_key disk_key;
1143
1144 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001145
Chris Mason74123bd2007-02-02 11:05:29 -05001146 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001147 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001148 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001149 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001150 if (ret == 0) {
Chris Mason62e27492007-03-15 12:56:47 -04001151 btrfs_release_path(root, path);
Chris Masonf0930a32007-03-02 09:47:58 -05001152 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001153 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001154 if (ret < 0)
1155 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001156
Chris Mason62e27492007-03-15 12:56:47 -04001157 slot_orig = path->slots[0];
1158 leaf_buf = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001159 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001160
Chris Mason7518a232007-03-12 12:01:18 -04001161 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001162 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001163
Chris Mason123abc82007-03-14 14:14:43 -04001164 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason234b63a2007-03-13 10:46:10 -04001165 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001166 BUG();
1167
Chris Mason62e27492007-03-15 12:56:47 -04001168 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001169 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001170 if (slot != nritems) {
1171 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001172 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001173
1174 /*
1175 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1176 */
1177 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001178 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001179 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001180 btrfs_set_item_offset(leaf->items + i,
1181 ioff - data_size);
1182 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001183
1184 /* shift the items */
1185 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001186 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001187
1188 /* shift the data */
Chris Mason123abc82007-03-14 14:14:43 -04001189 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1190 btrfs_leaf_data(leaf) +
Chris Masonbe0e5c02007-01-26 15:51:26 -05001191 data_end, old_data - data_end);
1192 data_end = old_data;
1193 }
Chris Mason62e27492007-03-15 12:56:47 -04001194 /* setup the item for the new data */
Chris Masone2fa7222007-03-12 16:22:34 -04001195 memcpy(&leaf->items[slot].key, &disk_key,
1196 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001197 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1198 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001199 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001200
1201 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001202 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001203 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001204
Chris Mason02217ed2007-03-02 16:08:05 -05001205 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason123abc82007-03-14 14:14:43 -04001206 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001207 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001208 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001209out:
Chris Mason62e27492007-03-15 12:56:47 -04001210 return ret;
1211}
1212
1213/*
1214 * Given a key and some data, insert an item into the tree.
1215 * This does all the path init required, making room in the tree if needed.
1216 */
Chris Masone089f052007-03-16 16:20:31 -04001217int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1218 *root, struct btrfs_key *cpu_key, void *data, u32
1219 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001220{
1221 int ret = 0;
1222 struct btrfs_path path;
1223 u8 *ptr;
1224
1225 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -04001226 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001227 if (!ret) {
1228 ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
1229 memcpy(ptr, data, data_size);
1230 }
Chris Mason234b63a2007-03-13 10:46:10 -04001231 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001232 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001233}
1234
Chris Mason74123bd2007-02-02 11:05:29 -05001235/*
Chris Mason5de08d72007-02-24 06:24:44 -05001236 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001237 *
1238 * If the delete empties a node, the node is removed from the tree,
1239 * continuing all the way the root if required. The root is converted into
1240 * a leaf if all the nodes are emptied.
1241 */
Chris Masone089f052007-03-16 16:20:31 -04001242static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1243 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001244{
Chris Mason234b63a2007-03-13 10:46:10 -04001245 struct btrfs_node *node;
1246 struct btrfs_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001247 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001248 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001249 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001250
Chris Masonbb803952007-03-01 12:04:21 -05001251 node = &parent->node;
Chris Mason7518a232007-03-12 12:01:18 -04001252 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001253 if (slot != nritems -1) {
Chris Mason123abc82007-03-14 14:14:43 -04001254 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1255 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001256 }
Chris Mason7518a232007-03-12 12:01:18 -04001257 nritems--;
1258 btrfs_set_header_nritems(&node->header, nritems);
1259 if (nritems == 0 && parent == root->node) {
1260 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001261 /* just turn the root into a leaf and break */
Chris Mason7518a232007-03-12 12:01:18 -04001262 btrfs_set_header_level(&root->node->node.header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001263 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001264 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001265 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001266 if (wret)
1267 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001268 }
Chris Mason02217ed2007-03-02 16:08:05 -05001269 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001270 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001271}
1272
Chris Mason74123bd2007-02-02 11:05:29 -05001273/*
1274 * delete the item at the leaf level in path. If that empties
1275 * the leaf, remove it from the tree
1276 */
Chris Masone089f052007-03-16 16:20:31 -04001277int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1278 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001279{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001280 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001281 struct btrfs_leaf *leaf;
1282 struct btrfs_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001283 int doff;
1284 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001285 int ret = 0;
1286 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001287 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001288
Chris Masoneb60cea2007-02-02 09:18:22 -05001289 leaf_buf = path->nodes[0];
1290 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001291 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001292 doff = btrfs_item_offset(leaf->items + slot);
1293 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001294 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001295
Chris Mason7518a232007-03-12 12:01:18 -04001296 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001297 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001298 int data_end = leaf_data_end(root, leaf);
1299 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1300 btrfs_leaf_data(leaf) + data_end,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001301 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001302 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001303 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001304 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1305 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001306 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001307 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001308 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001309 }
Chris Mason7518a232007-03-12 12:01:18 -04001310 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1311 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001312 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001313 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001314 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001315 btrfs_set_header_level(&leaf->header, 0);
Chris Mason02217ed2007-03-02 16:08:05 -05001316 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001317 } else {
Chris Masone089f052007-03-16 16:20:31 -04001318 clean_tree_block(trans, root, leaf_buf);
1319 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001320 if (wret)
1321 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001322 wret = btrfs_free_extent(trans, root,
1323 leaf_buf->blocknr, 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001324 if (wret)
1325 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001326 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001327 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001328 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001329 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001330 wret = fixup_low_keys(trans, root, path,
1331 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001332 if (wret)
1333 ret = wret;
1334 }
Chris Mason02217ed2007-03-02 16:08:05 -05001335 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001336
Chris Mason74123bd2007-02-02 11:05:29 -05001337 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001338 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001339 /* push_leaf_left fixes the path.
1340 * make sure the path still points to our leaf
1341 * for possible call to del_ptr below
1342 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001343 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001344 leaf_buf->count++;
Chris Masone089f052007-03-16 16:20:31 -04001345 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001346 if (wret < 0)
1347 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001348 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001349 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001350 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001351 if (wret < 0)
1352 ret = wret;
1353 }
Chris Mason7518a232007-03-12 12:01:18 -04001354 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001355 u64 blocknr = leaf_buf->blocknr;
Chris Masone089f052007-03-16 16:20:31 -04001356 clean_tree_block(trans, root, leaf_buf);
1357 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001358 if (wret)
1359 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001360 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001361 wret = btrfs_free_extent(trans, root, blocknr,
1362 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001363 if (wret)
1364 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001365 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001366 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001367 }
1368 }
1369 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001370 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001371}
1372
Chris Mason97571fd2007-02-24 13:39:08 -05001373/*
1374 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001375 * returns 0 if it found something or 1 if there are no greater leaves.
1376 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001377 */
Chris Mason234b63a2007-03-13 10:46:10 -04001378int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001379{
1380 int slot;
1381 int level = 1;
1382 u64 blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -04001383 struct btrfs_buffer *c;
1384 struct btrfs_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001385
Chris Mason234b63a2007-03-13 10:46:10 -04001386 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001387 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001388 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001389 slot = path->slots[level] + 1;
1390 c = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001391 if (slot >= btrfs_header_nritems(&c->node.header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001392 level++;
1393 continue;
1394 }
Chris Mason1d4f8a02007-03-13 09:28:32 -04001395 blocknr = btrfs_node_blockptr(&c->node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001396 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001397 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001398 next = read_tree_block(root, blocknr);
1399 break;
1400 }
1401 path->slots[level] = slot;
1402 while(1) {
1403 level--;
1404 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001405 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001406 path->nodes[level] = next;
1407 path->slots[level] = 0;
1408 if (!level)
1409 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001410 next = read_tree_block(root,
1411 btrfs_node_blockptr(&next->node, 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001412 }
1413 return 0;
1414}