blob: ece8de7f38ef64f6e8699cc4d37482c27c157600 [file] [log] [blame]
Chris Masonbe0e5c02007-01-26 15:51:26 -05001#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05004#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
Chris Mason5de08d72007-02-24 06:24:44 -05007#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05008
Chris Mason234b63a2007-03-13 10:46:10 -04009static int split_node(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -050010 int level);
Chris Mason234b63a2007-03-13 10:46:10 -040011static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -050012 int data_size);
Chris Mason234b63a2007-03-13 10:46:10 -040013static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst,
14 struct btrfs_buffer *src);
15static int balance_node_right(struct btrfs_root *root,
16 struct btrfs_buffer *dst_buf,
17 struct btrfs_buffer *src_buf);
18static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
Chris Masonbb803952007-03-01 12:04:21 -050019 int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050020
Chris Mason234b63a2007-03-13 10:46:10 -040021inline void btrfs_init_path(struct btrfs_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050022{
23 memset(p, 0, sizeof(*p));
24}
25
Chris Mason234b63a2007-03-13 10:46:10 -040026void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050027{
28 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040029 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050030 if (!p->nodes[i])
31 break;
Chris Mason234b63a2007-03-13 10:46:10 -040032 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050033 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050034 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050035}
36
Chris Mason9aca1d52007-03-13 11:09:37 -040037static int btrfs_cow_block(struct btrfs_root *root,
Chris Mason234b63a2007-03-13 10:46:10 -040038 struct btrfs_buffer *buf,
39 struct btrfs_buffer *parent,
Chris Mason02217ed2007-03-02 16:08:05 -050040 int parent_slot,
Chris Mason234b63a2007-03-13 10:46:10 -040041 struct btrfs_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050042{
Chris Mason234b63a2007-03-13 10:46:10 -040043 struct btrfs_buffer *cow;
Chris Mason02217ed2007-03-02 16:08:05 -050044
45 if (!list_empty(&buf->dirty)) {
46 *cow_ret = buf;
47 return 0;
48 }
Chris Mason234b63a2007-03-13 10:46:10 -040049 cow = btrfs_alloc_free_block(root);
Chris Mason123abc82007-03-14 14:14:43 -040050 memcpy(&cow->node, &buf->node, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -040051 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050052 *cow_ret = cow;
Chris Masona28ec192007-03-06 20:08:01 -050053 btrfs_inc_ref(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050054 if (buf == root->node) {
55 root->node = cow;
56 cow->count++;
Chris Masona28ec192007-03-06 20:08:01 -050057 if (buf != root->commit_root)
Chris Mason234b63a2007-03-13 10:46:10 -040058 btrfs_free_extent(root, buf->blocknr, 1);
59 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050060 } else {
Chris Mason1d4f8a02007-03-13 09:28:32 -040061 btrfs_set_node_blockptr(&parent->node, parent_slot,
62 cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050063 BUG_ON(list_empty(&parent->dirty));
Chris Mason234b63a2007-03-13 10:46:10 -040064 btrfs_free_extent(root, buf->blocknr, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050065 }
Chris Mason234b63a2007-03-13 10:46:10 -040066 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050067 return 0;
68}
69
Chris Mason74123bd2007-02-02 11:05:29 -050070/*
71 * The leaf data grows from end-to-front in the node.
72 * this returns the address of the start of the last item,
73 * which is the stop of the leaf data stack
74 */
Chris Mason123abc82007-03-14 14:14:43 -040075static inline unsigned int leaf_data_end(struct btrfs_root *root,
76 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050077{
Chris Mason7518a232007-03-12 12:01:18 -040078 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050079 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -040080 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -040081 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050082}
83
Chris Mason74123bd2007-02-02 11:05:29 -050084/*
85 * The space between the end of the leaf items and
86 * the start of the leaf data. IOW, how much room
87 * the leaf has left for both items and data
88 */
Chris Mason123abc82007-03-14 14:14:43 -040089int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050090{
Chris Mason123abc82007-03-14 14:14:43 -040091 int data_end = leaf_data_end(root, leaf);
Chris Mason7518a232007-03-12 12:01:18 -040092 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050093 char *items_end = (char *)(leaf->items + nritems + 1);
Chris Mason123abc82007-03-14 14:14:43 -040094 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
Chris Masonbe0e5c02007-01-26 15:51:26 -050095}
96
Chris Mason74123bd2007-02-02 11:05:29 -050097/*
98 * compare two keys in a memcmp fashion
99 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400100static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500101{
Chris Masone2fa7222007-03-12 16:22:34 -0400102 struct btrfs_key k1;
103
104 btrfs_disk_key_to_cpu(&k1, disk);
105
106 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500107 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400108 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500109 return -1;
Chris Masone2fa7222007-03-12 16:22:34 -0400110 if (k1.flags > k2->flags)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500111 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400112 if (k1.flags < k2->flags)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500113 return -1;
Chris Masone2fa7222007-03-12 16:22:34 -0400114 if (k1.offset > k2->offset)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500115 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400116 if (k1.offset < k2->offset)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500117 return -1;
118 return 0;
119}
Chris Mason74123bd2007-02-02 11:05:29 -0500120
Chris Mason123abc82007-03-14 14:14:43 -0400121static int check_node(struct btrfs_root *root, struct btrfs_path *path,
122 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500123{
124 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400125 struct btrfs_node *parent = NULL;
126 struct btrfs_node *node = &path->nodes[level]->node;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500127 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400128 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500129
130 if (path->nodes[level + 1])
131 parent = &path->nodes[level + 1]->node;
132 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400133 BUG_ON(nritems == 0);
134 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400135 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400136 parent_key = &parent->ptrs[parent_slot].key;
137 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400138 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400139 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400140 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500141 }
Chris Mason123abc82007-03-14 14:14:43 -0400142 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400143 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400144 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400145 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
146 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500147 }
148 return 0;
149}
150
Chris Mason123abc82007-03-14 14:14:43 -0400151static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
152 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500153{
154 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400155 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
156 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500157 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400158 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500159
160 if (path->nodes[level + 1])
161 parent = &path->nodes[level + 1]->node;
162 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400163 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400164
165 if (nritems == 0)
166 return 0;
167
168 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400169 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400170 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500171 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400172 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400173 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400174 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500175 }
Chris Mason7518a232007-03-12 12:01:18 -0400176 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400177 struct btrfs_key cpukey;
178 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500179 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400180 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400181 BUG_ON(btrfs_item_offset(leaf->items + i) !=
182 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500183 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400184 BUG_ON(btrfs_item_offset(leaf->items + i) +
185 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400186 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500187 }
188 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500189 return 0;
190}
191
Chris Mason123abc82007-03-14 14:14:43 -0400192static int check_block(struct btrfs_root *root, struct btrfs_path *path,
193 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500194{
195 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400196 return check_leaf(root, path, level);
197 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500198}
199
Chris Mason74123bd2007-02-02 11:05:29 -0500200/*
201 * search for key in the array p. items p are item_size apart
202 * and there are 'max' items in p
203 * the slot in the array is returned via slot, and it points to
204 * the place where you would insert key if it is not found in
205 * the array.
206 *
207 * slot may point to max if the key is bigger than all of the keys
208 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400209static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500210 int max, int *slot)
211{
212 int low = 0;
213 int high = max;
214 int mid;
215 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400216 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500217
218 while(low < high) {
219 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400220 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500221 ret = comp_keys(tmp, key);
222
223 if (ret < 0)
224 low = mid + 1;
225 else if (ret > 0)
226 high = mid;
227 else {
228 *slot = mid;
229 return 0;
230 }
231 }
232 *slot = low;
233 return 1;
234}
235
Chris Mason97571fd2007-02-24 13:39:08 -0500236/*
237 * simple bin_search frontend that does the right thing for
238 * leaves vs nodes
239 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400240static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500241{
Chris Mason7518a232007-03-12 12:01:18 -0400242 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400243 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400244 return generic_bin_search((void *)l->items,
245 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400246 key, btrfs_header_nritems(&c->header),
247 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500248 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400249 return generic_bin_search((void *)c->ptrs,
250 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400251 key, btrfs_header_nritems(&c->header),
252 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500253 }
254 return -1;
255}
256
Chris Mason9aca1d52007-03-13 11:09:37 -0400257static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason234b63a2007-03-13 10:46:10 -0400258 struct btrfs_buffer *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500259 int slot)
260{
Chris Mason234b63a2007-03-13 10:46:10 -0400261 struct btrfs_node *node = &parent_buf->node;
Chris Masonbb803952007-03-01 12:04:21 -0500262 if (slot < 0)
263 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400264 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500265 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400266 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500267}
268
Chris Mason234b63a2007-03-13 10:46:10 -0400269static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonbb803952007-03-01 12:04:21 -0500270 int level)
271{
Chris Mason234b63a2007-03-13 10:46:10 -0400272 struct btrfs_buffer *right_buf;
273 struct btrfs_buffer *mid_buf;
274 struct btrfs_buffer *left_buf;
275 struct btrfs_buffer *parent_buf = NULL;
276 struct btrfs_node *right = NULL;
277 struct btrfs_node *mid;
278 struct btrfs_node *left = NULL;
279 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500280 int ret = 0;
281 int wret;
282 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500283 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500284 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500285
286 if (level == 0)
287 return 0;
288
289 mid_buf = path->nodes[level];
290 mid = &mid_buf->node;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400291 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500292
Chris Mason234b63a2007-03-13 10:46:10 -0400293 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500294 parent_buf = path->nodes[level + 1];
295 pslot = path->slots[level + 1];
296
297 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 Masoned2ff2c2007-03-01 18:59:40 -0500313 clean_tree_block(root, mid_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400314 return btrfs_free_extent(root, blocknr, 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 Mason02217ed2007-03-02 16:08:05 -0500327 btrfs_cow_block(root, left_buf, parent_buf,
328 pslot - 1, &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 Mason79f95c82007-03-01 15:16:26 -0500331 wret = push_node_left(root, left_buf, mid_buf);
332 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 Mason02217ed2007-03-02 16:08:05 -0500340 btrfs_cow_block(root, right_buf, parent_buf,
341 pslot + 1, &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500342 right = &right_buf->node;
343 wret = push_node_left(root, mid_buf, right_buf);
344 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 Masoned2ff2c2007-03-01 18:59:40 -0500349 clean_tree_block(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500350 right_buf = NULL;
351 right = NULL;
352 wret = del_ptr(root, path, level + 1, pslot + 1);
353 if (wret)
354 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400355 wret = btrfs_free_extent(root, blocknr, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500356 if (wret)
357 ret = wret;
358 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400359 memcpy(&parent->ptrs[pslot + 1].key,
360 &right->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400361 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500362 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500363 }
364 }
Chris Mason7518a232007-03-12 12:01:18 -0400365 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500366 /*
367 * we're not allowed to leave a node with one item in the
368 * tree during a delete. A deletion from lower in the tree
369 * could try to delete the only pointer in this node.
370 * So, pull some keys from the left.
371 * There has to be a left pointer at this point because
372 * otherwise we would have pulled some pointers from the
373 * right
374 */
375 BUG_ON(!left_buf);
376 wret = balance_node_right(root, mid_buf, left_buf);
377 if (wret < 0)
378 ret = wret;
379 BUG_ON(wret == 1);
380 }
Chris Mason7518a232007-03-12 12:01:18 -0400381 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500382 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500383 u64 blocknr = mid_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400384 btrfs_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500385 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500386 mid_buf = NULL;
387 mid = NULL;
388 wret = del_ptr(root, path, level + 1, pslot);
389 if (wret)
390 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -0400391 wret = btrfs_free_extent(root, blocknr, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500392 if (wret)
393 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500394 } else {
395 /* update the parent key to reflect our changes */
Chris Mason123abc82007-03-14 14:14:43 -0400396 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400397 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500398 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500399 }
Chris Masonbb803952007-03-01 12:04:21 -0500400
Chris Mason79f95c82007-03-01 15:16:26 -0500401 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500402 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400403 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500404 left_buf->count++; // released below
405 path->nodes[level] = left_buf;
406 path->slots[level + 1] -= 1;
407 path->slots[level] = orig_slot;
408 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400409 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500410 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400411 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500412 path->slots[level] = orig_slot;
413 }
414 }
Chris Mason79f95c82007-03-01 15:16:26 -0500415 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400416 check_block(root, path, level);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400417 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
418 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500419 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500420
421 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400422 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500423 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400424 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500425 return ret;
426}
427
Chris Mason74123bd2007-02-02 11:05:29 -0500428/*
429 * look for key in the tree. path is filled in with nodes along the way
430 * if key is found, we return zero and you can find the item in the leaf
431 * level of the path (level 0)
432 *
433 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500434 * be inserted, and 1 is returned. If there are other errors during the
435 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500436 *
437 * if ins_len > 0, nodes and leaves will be split as we walk down the
438 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
439 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500440 */
Chris Mason234b63a2007-03-13 10:46:10 -0400441int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
442 struct btrfs_path *p, int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500443{
Chris Mason234b63a2007-03-13 10:46:10 -0400444 struct btrfs_buffer *b;
445 struct btrfs_buffer *cow_buf;
446 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500447 int slot;
448 int ret;
449 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500450
Chris Masonbb803952007-03-01 12:04:21 -0500451again:
452 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500453 b->count++;
454 while (b) {
Chris Mason7518a232007-03-12 12:01:18 -0400455 level = btrfs_header_level(&b->node.header);
Chris Mason02217ed2007-03-02 16:08:05 -0500456 if (cow) {
457 int wret;
458 wret = btrfs_cow_block(root, b, p->nodes[level + 1],
459 p->slots[level + 1], &cow_buf);
460 b = cow_buf;
461 }
462 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500463 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500464 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400465 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500466 if (ret)
467 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500468 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400469 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500470 if (ret && slot > 0)
471 slot -= 1;
472 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400473 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
Chris Mason123abc82007-03-14 14:14:43 -0400474 BTRFS_NODEPTRS_PER_BLOCK(root)) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500475 int sret = split_node(root, p, level);
476 BUG_ON(sret > 0);
477 if (sret)
478 return sret;
479 b = p->nodes[level];
480 c = &b->node;
481 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500482 } else if (ins_len < 0) {
483 int sret = balance_level(root, p, level);
484 if (sret)
485 return sret;
486 b = p->nodes[level];
487 if (!b)
488 goto again;
489 c = &b->node;
490 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400491 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500492 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400493 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500494 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400495 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500496 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400497 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400498 sizeof(struct btrfs_item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500499 int sret = split_leaf(root, p, ins_len);
500 BUG_ON(sret > 0);
501 if (sret)
502 return sret;
503 }
Chris Masonbb803952007-03-01 12:04:21 -0500504 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500505 return ret;
506 }
507 }
Chris Masonbb803952007-03-01 12:04:21 -0500508 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500509 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500510}
511
Chris Mason74123bd2007-02-02 11:05:29 -0500512/*
513 * adjust the pointers going up the tree, starting at level
514 * making sure the right key of each node is points to 'key'.
515 * This is used after shifting pointers to the left, so it stops
516 * fixing up pointers when a given leaf/node is not in slot 0 of the
517 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500518 *
519 * If this fails to write a tree block, it returns -1, but continues
520 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500521 */
Chris Mason234b63a2007-03-13 10:46:10 -0400522static int fixup_low_keys(struct btrfs_root *root,
523 struct btrfs_path *path, struct btrfs_disk_key *key,
Chris Masoneb60cea2007-02-02 09:18:22 -0500524 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500525{
526 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500527 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400528 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
529 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500530 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500531 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500532 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500533 t = &path->nodes[i]->node;
Chris Mason123abc82007-03-14 14:14:43 -0400534 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500535 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500536 if (tslot != 0)
537 break;
538 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500539 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500540}
541
Chris Mason74123bd2007-02-02 11:05:29 -0500542/*
543 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500544 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500545 *
546 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
547 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500548 */
Chris Mason234b63a2007-03-13 10:46:10 -0400549static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
550 struct btrfs_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500551{
Chris Mason234b63a2007-03-13 10:46:10 -0400552 struct btrfs_node *src = &src_buf->node;
553 struct btrfs_node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500554 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500555 int src_nritems;
556 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500557 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500558
Chris Mason7518a232007-03-12 12:01:18 -0400559 src_nritems = btrfs_header_nritems(&src->header);
560 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400561 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500562 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500564 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500565
566 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500567 push_items = src_nritems;
568
Chris Mason123abc82007-03-14 14:14:43 -0400569 memcpy(dst->ptrs + dst_nritems, src->ptrs,
570 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500571 if (push_items < src_nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400572 memmove(src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400573 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400574 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500575 }
Chris Mason7518a232007-03-12 12:01:18 -0400576 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
577 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason02217ed2007-03-02 16:08:05 -0500578 BUG_ON(list_empty(&src_buf->dirty));
579 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500580 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500581}
582
Chris Mason97571fd2007-02-24 13:39:08 -0500583/*
Chris Mason79f95c82007-03-01 15:16:26 -0500584 * try to push data from one node into the next node right in the
585 * tree.
586 *
587 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
588 * error, and > 0 if there was no room in the right hand block.
589 *
590 * this will only push up to 1/2 the contents of the left node over
591 */
Chris Mason234b63a2007-03-13 10:46:10 -0400592static int balance_node_right(struct btrfs_root *root,
593 struct btrfs_buffer *dst_buf,
594 struct btrfs_buffer *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500595{
Chris Mason234b63a2007-03-13 10:46:10 -0400596 struct btrfs_node *src = &src_buf->node;
597 struct btrfs_node *dst = &dst_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500598 int push_items = 0;
599 int max_push;
600 int src_nritems;
601 int dst_nritems;
602 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500603
Chris Mason7518a232007-03-12 12:01:18 -0400604 src_nritems = btrfs_header_nritems(&src->header);
605 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400606 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500607 if (push_items <= 0) {
608 return 1;
609 }
610
611 max_push = src_nritems / 2 + 1;
612 /* don't try to empty the node */
613 if (max_push > src_nritems)
614 return 1;
615 if (max_push < push_items)
616 push_items = max_push;
617
Chris Mason123abc82007-03-14 14:14:43 -0400618 memmove(dst->ptrs + push_items, dst->ptrs,
619 dst_nritems * sizeof(struct btrfs_key_ptr));
620 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
621 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500622
Chris Mason7518a232007-03-12 12:01:18 -0400623 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
624 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500625
Chris Mason02217ed2007-03-02 16:08:05 -0500626 BUG_ON(list_empty(&src_buf->dirty));
627 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500628 return ret;
629}
630
631/*
Chris Mason97571fd2007-02-24 13:39:08 -0500632 * helper function to insert a new root level in the tree.
633 * A new node is allocated, and a single item is inserted to
634 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500635 *
636 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500637 */
Chris Mason234b63a2007-03-13 10:46:10 -0400638static int insert_new_root(struct btrfs_root *root,
639 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500640{
Chris Mason234b63a2007-03-13 10:46:10 -0400641 struct btrfs_buffer *t;
642 struct btrfs_node *lower;
643 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400644 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500645
646 BUG_ON(path->nodes[level]);
647 BUG_ON(path->nodes[level-1] != root->node);
648
Chris Mason234b63a2007-03-13 10:46:10 -0400649 t = btrfs_alloc_free_block(root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500650 c = &t->node;
Chris Mason123abc82007-03-14 14:14:43 -0400651 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400652 btrfs_set_header_nritems(&c->header, 1);
653 btrfs_set_header_level(&c->header, level);
654 btrfs_set_header_blocknr(&c->header, t->blocknr);
655 btrfs_set_header_parentid(&c->header,
656 btrfs_header_parentid(&root->node->node.header));
Chris Mason5c680ed2007-02-22 11:39:13 -0500657 lower = &path->nodes[level-1]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400658 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400659 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500660 else
Chris Mason123abc82007-03-14 14:14:43 -0400661 lower_key = &lower->ptrs[0].key;
662 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400663 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
Chris Mason5c680ed2007-02-22 11:39:13 -0500664 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400665 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500666 root->node = t;
667 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500668 path->nodes[level] = t;
669 path->slots[level] = 0;
670 return 0;
671}
672
Chris Mason74123bd2007-02-02 11:05:29 -0500673/*
674 * worker function to insert a single pointer in a node.
675 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500676 *
Chris Mason74123bd2007-02-02 11:05:29 -0500677 * slot and level indicate where you want the key to go, and
678 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500679 *
680 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500681 */
Chris Mason234b63a2007-03-13 10:46:10 -0400682static int insert_ptr(struct btrfs_root *root,
683 struct btrfs_path *path, struct btrfs_disk_key *key,
Chris Mason74123bd2007-02-02 11:05:29 -0500684 u64 blocknr, int slot, int level)
685{
Chris Mason234b63a2007-03-13 10:46:10 -0400686 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500687 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500688
689 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500690 lower = &path->nodes[level]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400691 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500692 if (slot > nritems)
693 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400694 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500695 BUG();
696 if (slot != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400697 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
698 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500699 }
Chris Mason123abc82007-03-14 14:14:43 -0400700 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400701 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400702 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Mason02217ed2007-03-02 16:08:05 -0500703 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500704 return 0;
705}
706
Chris Mason97571fd2007-02-24 13:39:08 -0500707/*
708 * split the node at the specified level in path in two.
709 * The path is corrected to point to the appropriate node after the split
710 *
711 * Before splitting this tries to make some room in the node by pushing
712 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500713 *
714 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500715 */
Chris Mason234b63a2007-03-13 10:46:10 -0400716static int split_node(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -0500717 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500718{
Chris Mason234b63a2007-03-13 10:46:10 -0400719 struct btrfs_buffer *t;
720 struct btrfs_node *c;
721 struct btrfs_buffer *split_buffer;
722 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500724 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500725 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400726 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727
Chris Mason5c680ed2007-02-22 11:39:13 -0500728 t = path->nodes[level];
729 c = &t->node;
730 if (t == root->node) {
731 /* trying to split the root, lets make a new one */
732 ret = insert_new_root(root, path, level + 1);
733 if (ret)
734 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735 }
Chris Mason7518a232007-03-12 12:01:18 -0400736 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason234b63a2007-03-13 10:46:10 -0400737 split_buffer = btrfs_alloc_free_block(root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500738 split = &split_buffer->node;
Chris Mason7518a232007-03-12 12:01:18 -0400739 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
740 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
741 btrfs_set_header_parentid(&split->header,
742 btrfs_header_parentid(&root->node->node.header));
743 mid = (c_nritems + 1) / 2;
Chris Mason123abc82007-03-14 14:14:43 -0400744 memcpy(split->ptrs, c->ptrs + mid,
745 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400746 btrfs_set_header_nritems(&split->header, c_nritems - mid);
747 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500748 ret = 0;
749
Chris Mason02217ed2007-03-02 16:08:05 -0500750 BUG_ON(list_empty(&t->dirty));
Chris Mason123abc82007-03-14 14:14:43 -0400751 wret = insert_ptr(root, path, &split->ptrs[0].key,
752 split_buffer->blocknr, path->slots[level + 1] + 1,
753 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500754 if (wret)
755 ret = wret;
756
Chris Mason5de08d72007-02-24 06:24:44 -0500757 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500758 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400759 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500760 path->nodes[level] = split_buffer;
761 path->slots[level + 1] += 1;
762 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400763 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500764 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500765 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500766}
767
Chris Mason74123bd2007-02-02 11:05:29 -0500768/*
769 * how many bytes are required to store the items in a leaf. start
770 * and nr indicate which items in the leaf to check. This totals up the
771 * space used both by the item structs and the item data
772 */
Chris Mason234b63a2007-03-13 10:46:10 -0400773static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500774{
775 int data_len;
776 int end = start + nr - 1;
777
778 if (!nr)
779 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400780 data_len = btrfs_item_end(l->items + start);
781 data_len = data_len - btrfs_item_offset(l->items + end);
782 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500783 return data_len;
784}
785
Chris Mason74123bd2007-02-02 11:05:29 -0500786/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500787 * push some data in the path leaf to the right, trying to free up at
788 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500789 *
790 * returns 1 if the push failed because the other node didn't have enough
791 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500792 */
Chris Mason234b63a2007-03-13 10:46:10 -0400793static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -0500794 int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500795{
Chris Mason234b63a2007-03-13 10:46:10 -0400796 struct btrfs_buffer *left_buf = path->nodes[0];
797 struct btrfs_leaf *left = &left_buf->leaf;
798 struct btrfs_leaf *right;
799 struct btrfs_buffer *right_buf;
800 struct btrfs_buffer *upper;
Chris Mason00ec4c52007-02-24 12:47:20 -0500801 int slot;
802 int i;
803 int free_space;
804 int push_space = 0;
805 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400806 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400807 u32 left_nritems;
808 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500809
810 slot = path->slots[1];
811 if (!path->nodes[1]) {
812 return 1;
813 }
814 upper = path->nodes[1];
Chris Mason7518a232007-03-12 12:01:18 -0400815 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500816 return 1;
817 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400818 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
819 slot + 1));
Chris Mason00ec4c52007-02-24 12:47:20 -0500820 right = &right_buf->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400821 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400822 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400823 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500824 return 1;
825 }
Chris Mason02217ed2007-03-02 16:08:05 -0500826 /* cow and double check */
827 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
828 right = &right_buf->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400829 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400830 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400831 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500832 return 1;
833 }
834
Chris Mason7518a232007-03-12 12:01:18 -0400835 left_nritems = btrfs_header_nritems(&left->header);
836 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500837 item = left->items + i;
838 if (path->slots[0] == i)
839 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400840 if (btrfs_item_size(item) + sizeof(*item) + push_space >
841 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500842 break;
843 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400844 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500845 }
846 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400847 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500848 return 1;
849 }
Chris Mason7518a232007-03-12 12:01:18 -0400850 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500851 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400852 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400853 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500854 /* make room in the right data area */
Chris Mason123abc82007-03-14 14:14:43 -0400855 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
856 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
857 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500858 /* copy from the left data area */
Chris Mason123abc82007-03-14 14:14:43 -0400859 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
860 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
Chris Mason00ec4c52007-02-24 12:47:20 -0500861 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400862 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500863 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400864 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400865 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500866
867 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400868 right_nritems += push_items;
869 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400870 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400871 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400872 btrfs_set_item_offset(right->items + i, push_space -
873 btrfs_item_size(right->items + i));
874 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500875 }
Chris Mason7518a232007-03-12 12:01:18 -0400876 left_nritems -= push_items;
877 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500878
Chris Mason02217ed2007-03-02 16:08:05 -0500879 BUG_ON(list_empty(&left_buf->dirty));
880 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason123abc82007-03-14 14:14:43 -0400881 memcpy(&upper->node.ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400882 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500883 BUG_ON(list_empty(&upper->dirty));
884
Chris Mason00ec4c52007-02-24 12:47:20 -0500885 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400886 if (path->slots[0] >= left_nritems) {
887 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400888 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500889 path->nodes[0] = right_buf;
890 path->slots[1] += 1;
891 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400892 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500893 }
894 return 0;
895}
896/*
Chris Mason74123bd2007-02-02 11:05:29 -0500897 * push some data in the path leaf to the left, trying to free up at
898 * least data_size bytes. returns zero if the push worked, nonzero otherwise
899 */
Chris Mason234b63a2007-03-13 10:46:10 -0400900static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -0500901 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500902{
Chris Mason234b63a2007-03-13 10:46:10 -0400903 struct btrfs_buffer *right_buf = path->nodes[0];
904 struct btrfs_leaf *right = &right_buf->leaf;
905 struct btrfs_buffer *t;
906 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500907 int slot;
908 int i;
909 int free_space;
910 int push_space = 0;
911 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400912 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400913 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500914 int ret = 0;
915 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500916
917 slot = path->slots[1];
918 if (slot == 0) {
919 return 1;
920 }
921 if (!path->nodes[1]) {
922 return 1;
923 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400924 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
925 slot - 1));
Chris Masoneb60cea2007-02-02 09:18:22 -0500926 left = &t->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400927 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400928 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400929 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500930 return 1;
931 }
Chris Mason02217ed2007-03-02 16:08:05 -0500932
933 /* cow and double check */
934 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
935 left = &t->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400936 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400937 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400938 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500939 return 1;
940 }
941
Chris Mason7518a232007-03-12 12:01:18 -0400942 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500943 item = right->items + i;
944 if (path->slots[0] == i)
945 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400946 if (btrfs_item_size(item) + sizeof(*item) + push_space >
947 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948 break;
949 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400950 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500951 }
952 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400953 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500954 return 1;
955 }
956 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400957 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400958 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -0400959 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400960 btrfs_item_offset(right->items + push_items -1);
Chris Mason123abc82007-03-14 14:14:43 -0400961 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
962 btrfs_leaf_data(right) +
963 btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500964 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400965 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500966 BUG_ON(old_left_nritems < 0);
967
Chris Mason0783fcf2007-03-12 20:12:07 -0400968 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -0400969 u32 ioff = btrfs_item_offset(left->items + i);
970 btrfs_set_item_offset(left->items + i, ioff -
971 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400972 btrfs_item_offset(left->items +
973 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500974 }
Chris Mason7518a232007-03-12 12:01:18 -0400975 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500976
977 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400978 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -0400979 leaf_data_end(root, right);
980 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
981 push_space, btrfs_leaf_data(right) +
982 leaf_data_end(root, right), push_space);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500983 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400984 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -0400985 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -0400986 btrfs_set_header_nritems(&right->header,
987 btrfs_header_nritems(&right->header) -
988 push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400989 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500990
Chris Mason7518a232007-03-12 12:01:18 -0400991 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400992 btrfs_set_item_offset(right->items + i, push_space -
993 btrfs_item_size(right->items + i));
994 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500995 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500996
Chris Mason02217ed2007-03-02 16:08:05 -0500997 BUG_ON(list_empty(&t->dirty));
998 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500999
Chris Masonaa5d6be2007-02-28 16:35:06 -05001000 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
1001 if (wret)
1002 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001003
1004 /* then fixup the leaf pointer in the path */
1005 if (path->slots[0] < push_items) {
1006 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001007 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001008 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001009 path->slots[1] -= 1;
1010 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001011 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001012 path->slots[0] -= push_items;
1013 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001014 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001015 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001016}
1017
Chris Mason74123bd2007-02-02 11:05:29 -05001018/*
1019 * split the path's leaf in two, making sure there is at least data_size
1020 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001021 *
1022 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001023 */
Chris Mason234b63a2007-03-13 10:46:10 -04001024static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
Chris Masonaa5d6be2007-02-28 16:35:06 -05001025 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001026{
Chris Mason234b63a2007-03-13 10:46:10 -04001027 struct btrfs_buffer *l_buf;
1028 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001029 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001030 int mid;
1031 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001032 struct btrfs_leaf *right;
1033 struct btrfs_buffer *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001034 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001035 int data_copy_size;
1036 int rt_data_off;
1037 int i;
1038 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001039 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001040
Chris Masoneaee50e2007-03-13 11:17:52 -04001041 wret = push_leaf_left(root, path, data_size);
1042 if (wret < 0)
1043 return wret;
1044 if (wret) {
1045 wret = push_leaf_right(root, path, data_size);
1046 if (wret < 0)
1047 return wret;
1048 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001049 l_buf = path->nodes[0];
1050 l = &l_buf->leaf;
1051
1052 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001053 if (btrfs_leaf_free_space(root, l) >=
1054 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001055 return 0;
1056
Chris Mason5c680ed2007-02-22 11:39:13 -05001057 if (!path->nodes[1]) {
1058 ret = insert_new_root(root, path, 1);
1059 if (ret)
1060 return ret;
1061 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001062 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001063 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001064 mid = (nritems + 1)/ 2;
Chris Mason234b63a2007-03-13 10:46:10 -04001065 right_buffer = btrfs_alloc_free_block(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001066 BUG_ON(!right_buffer);
1067 BUG_ON(mid == nritems);
1068 right = &right_buffer->leaf;
Chris Mason123abc82007-03-14 14:14:43 -04001069 memset(&right->header, 0, sizeof(right->header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001070 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001071 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001072 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001073 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001074 BUG();
1075 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001076 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001077 if (leaf_space_used(l, 0, mid + 1) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001078 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001079 BUG();
1080 }
Chris Mason7518a232007-03-12 12:01:18 -04001081 btrfs_set_header_nritems(&right->header, nritems - mid);
1082 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1083 btrfs_set_header_level(&right->header, 0);
1084 btrfs_set_header_parentid(&right->header,
1085 btrfs_header_parentid(&root->node->node.header));
Chris Mason123abc82007-03-14 14:14:43 -04001086 data_copy_size = btrfs_item_end(l->items + mid) -
1087 leaf_data_end(root, l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001088 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001089 (nritems - mid) * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001090 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1091 data_copy_size, btrfs_leaf_data(l) +
1092 leaf_data_end(root, l), data_copy_size);
1093 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1094 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001095
Chris Mason0783fcf2007-03-12 20:12:07 -04001096 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001097 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001098 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1099 }
Chris Mason74123bd2007-02-02 11:05:29 -05001100
Chris Mason7518a232007-03-12 12:01:18 -04001101 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001102 ret = 0;
1103 wret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001104 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001105 if (wret)
1106 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001107 BUG_ON(list_empty(&right_buffer->dirty));
1108 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001109 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001110 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001111 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001112 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001113 path->slots[0] -= mid;
1114 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001115 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001116 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001117 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001118 return ret;
1119}
1120
Chris Mason74123bd2007-02-02 11:05:29 -05001121/*
1122 * Given a key and some data, insert an item into the tree.
1123 * This does all the path init required, making room in the tree if needed.
1124 */
Chris Mason234b63a2007-03-13 10:46:10 -04001125int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001126 void *data, int data_size)
1127{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001128 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001129 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001130 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001131 struct btrfs_leaf *leaf;
1132 struct btrfs_buffer *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001133 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001134 unsigned int data_end;
Chris Mason234b63a2007-03-13 10:46:10 -04001135 struct btrfs_path path;
Chris Masone2fa7222007-03-12 16:22:34 -04001136 struct btrfs_disk_key disk_key;
1137
1138 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001139
Chris Mason74123bd2007-02-02 11:05:29 -05001140 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001141 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001142 BUG();
Chris Mason234b63a2007-03-13 10:46:10 -04001143 btrfs_init_path(&path);
1144 ret = btrfs_search_slot(root, cpu_key, &path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001145 if (ret == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001146 btrfs_release_path(root, &path);
Chris Masonf0930a32007-03-02 09:47:58 -05001147 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001148 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001149 if (ret < 0)
1150 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001151
Chris Masoneb60cea2007-02-02 09:18:22 -05001152 slot_orig = path.slots[0];
1153 leaf_buf = path.nodes[0];
1154 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001155
Chris Mason7518a232007-03-12 12:01:18 -04001156 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001157 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001158
Chris Mason123abc82007-03-14 14:14:43 -04001159 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason234b63a2007-03-13 10:46:10 -04001160 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001161 BUG();
1162
1163 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001164 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001165 if (slot != nritems) {
1166 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001167 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001168
1169 /*
1170 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1171 */
1172 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001173 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001174 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001175 btrfs_set_item_offset(leaf->items + i,
1176 ioff - data_size);
1177 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001178
1179 /* shift the items */
1180 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001181 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001182
1183 /* shift the data */
Chris Mason123abc82007-03-14 14:14:43 -04001184 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1185 btrfs_leaf_data(leaf) +
Chris Masonbe0e5c02007-01-26 15:51:26 -05001186 data_end, old_data - data_end);
1187 data_end = old_data;
1188 }
Chris Mason74123bd2007-02-02 11:05:29 -05001189 /* copy the new data in */
Chris Masone2fa7222007-03-12 16:22:34 -04001190 memcpy(&leaf->items[slot].key, &disk_key,
1191 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001192 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1193 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason123abc82007-03-14 14:14:43 -04001194 memcpy(btrfs_leaf_data(leaf) + data_end - data_size, data, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001195 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001196
1197 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001198 if (slot == 0)
Chris Masone2fa7222007-03-12 16:22:34 -04001199 ret = fixup_low_keys(root, &path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001200
Chris Mason02217ed2007-03-02 16:08:05 -05001201 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason123abc82007-03-14 14:14:43 -04001202 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001203 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001204 check_leaf(root, &path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001205out:
Chris Mason234b63a2007-03-13 10:46:10 -04001206 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001207 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001208}
1209
Chris Mason74123bd2007-02-02 11:05:29 -05001210/*
Chris Mason5de08d72007-02-24 06:24:44 -05001211 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001212 *
1213 * If the delete empties a node, the node is removed from the tree,
1214 * continuing all the way the root if required. The root is converted into
1215 * a leaf if all the nodes are emptied.
1216 */
Chris Mason234b63a2007-03-13 10:46:10 -04001217static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
Chris Masonbb803952007-03-01 12:04:21 -05001218 int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001219{
Chris Mason234b63a2007-03-13 10:46:10 -04001220 struct btrfs_node *node;
1221 struct btrfs_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001222 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001223 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001224 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001225
Chris Masonbb803952007-03-01 12:04:21 -05001226 node = &parent->node;
Chris Mason7518a232007-03-12 12:01:18 -04001227 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001228 if (slot != nritems -1) {
Chris Mason123abc82007-03-14 14:14:43 -04001229 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1230 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001231 }
Chris Mason7518a232007-03-12 12:01:18 -04001232 nritems--;
1233 btrfs_set_header_nritems(&node->header, nritems);
1234 if (nritems == 0 && parent == root->node) {
1235 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001236 /* just turn the root into a leaf and break */
Chris Mason7518a232007-03-12 12:01:18 -04001237 btrfs_set_header_level(&root->node->node.header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001238 } else if (slot == 0) {
Chris Mason123abc82007-03-14 14:14:43 -04001239 wret = fixup_low_keys(root, path, &node->ptrs[0].key,
1240 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001241 if (wret)
1242 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001243 }
Chris Mason02217ed2007-03-02 16:08:05 -05001244 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001245 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001246}
1247
Chris Mason74123bd2007-02-02 11:05:29 -05001248/*
1249 * delete the item at the leaf level in path. If that empties
1250 * the leaf, remove it from the tree
1251 */
Chris Mason234b63a2007-03-13 10:46:10 -04001252int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001253{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001254 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001255 struct btrfs_leaf *leaf;
1256 struct btrfs_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001257 int doff;
1258 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001259 int ret = 0;
1260 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001261 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001262
Chris Masoneb60cea2007-02-02 09:18:22 -05001263 leaf_buf = path->nodes[0];
1264 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001265 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001266 doff = btrfs_item_offset(leaf->items + slot);
1267 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001268 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001269
Chris Mason7518a232007-03-12 12:01:18 -04001270 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001271 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001272 int data_end = leaf_data_end(root, leaf);
1273 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1274 btrfs_leaf_data(leaf) + data_end,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001275 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001276 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001277 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001278 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1279 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001280 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001281 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001282 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001283 }
Chris Mason7518a232007-03-12 12:01:18 -04001284 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1285 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001286 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001287 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001288 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001289 btrfs_set_header_level(&leaf->header, 0);
Chris Mason02217ed2007-03-02 16:08:05 -05001290 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001291 } else {
Chris Masoned2ff2c2007-03-01 18:59:40 -05001292 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001293 wret = del_ptr(root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001294 if (wret)
1295 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001296 wret = btrfs_free_extent(root, leaf_buf->blocknr, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001297 if (wret)
1298 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001299 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001300 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001301 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001302 if (slot == 0) {
1303 wret = fixup_low_keys(root, path,
1304 &leaf->items[0].key, 1);
1305 if (wret)
1306 ret = wret;
1307 }
Chris Mason02217ed2007-03-02 16:08:05 -05001308 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001309
Chris Mason74123bd2007-02-02 11:05:29 -05001310 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001311 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001312 /* push_leaf_left fixes the path.
1313 * make sure the path still points to our leaf
1314 * for possible call to del_ptr below
1315 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001316 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001317 leaf_buf->count++;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001318 wret = push_leaf_left(root, path, 1);
1319 if (wret < 0)
1320 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001321 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001322 btrfs_header_nritems(&leaf->header)) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001323 wret = push_leaf_right(root, path, 1);
1324 if (wret < 0)
1325 ret = wret;
1326 }
Chris Mason7518a232007-03-12 12:01:18 -04001327 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001328 u64 blocknr = leaf_buf->blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001329 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001330 wret = del_ptr(root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001331 if (wret)
1332 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001333 btrfs_block_release(root, leaf_buf);
1334 wret = btrfs_free_extent(root, blocknr, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001335 if (wret)
1336 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001337 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001338 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001339 }
1340 }
1341 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001342 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001343}
1344
Chris Mason97571fd2007-02-24 13:39:08 -05001345/*
1346 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001347 * returns 0 if it found something or 1 if there are no greater leaves.
1348 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001349 */
Chris Mason234b63a2007-03-13 10:46:10 -04001350int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001351{
1352 int slot;
1353 int level = 1;
1354 u64 blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -04001355 struct btrfs_buffer *c;
1356 struct btrfs_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001357
Chris Mason234b63a2007-03-13 10:46:10 -04001358 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001359 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001360 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001361 slot = path->slots[level] + 1;
1362 c = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001363 if (slot >= btrfs_header_nritems(&c->node.header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001364 level++;
1365 continue;
1366 }
Chris Mason1d4f8a02007-03-13 09:28:32 -04001367 blocknr = btrfs_node_blockptr(&c->node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001368 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001369 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001370 next = read_tree_block(root, blocknr);
1371 break;
1372 }
1373 path->slots[level] = slot;
1374 while(1) {
1375 level--;
1376 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001377 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001378 path->nodes[level] = next;
1379 path->slots[level] = 0;
1380 if (!level)
1381 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001382 next = read_tree_block(root,
1383 btrfs_node_blockptr(&next->node, 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001384 }
1385 return 0;
1386}
1387
Chris Mason02217ed2007-03-02 16:08:05 -05001388