blob: b4783bf8bf4fc2729c1cedaacd272917fcb88885 [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 Mason7f5c1512007-03-23 15:56:19 -04004#include "transaction.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05005
Chris Masone089f052007-03-16 16:20:31 -04006static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
7 *root, struct btrfs_path *path, int level);
8static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04009 *root, struct btrfs_key *ins_key,
10 struct btrfs_path *path, int data_size);
Chris Masone089f052007-03-16 16:20:31 -040011static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040012 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040013 *src);
14static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040015 btrfs_root *root, struct buffer_head *dst_buf,
16 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040017static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
18 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050019
Chris Masondf24a2b2007-04-04 09:36:31 -040020inline void btrfs_init_path(struct btrfs_path *p)
21{
22 memset(p, 0, sizeof(*p));
23}
24
Chris Mason2c90e5d2007-04-02 10:50:19 -040025struct btrfs_path *btrfs_alloc_path(void)
26{
Chris Masondf24a2b2007-04-04 09:36:31 -040027 struct btrfs_path *path;
28 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
29 if (path)
30 btrfs_init_path(path);
31 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040032}
33
34void btrfs_free_path(struct btrfs_path *p)
35{
Chris Masondf24a2b2007-04-04 09:36:31 -040036 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040037 kmem_cache_free(btrfs_path_cachep, p);
38}
39
Chris Mason234b63a2007-03-13 10:46:10 -040040void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050041{
42 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040043 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050044 if (!p->nodes[i])
45 break;
Chris Mason234b63a2007-03-13 10:46:10 -040046 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050047 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050048 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050049}
50
Chris Masone089f052007-03-16 16:20:31 -040051static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040052 *root, struct buffer_head *buf, struct buffer_head
53 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040054 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050055{
Chris Masone20d96d2007-03-22 12:13:20 -040056 struct buffer_head *cow;
57 struct btrfs_node *cow_node;
Chris Mason02217ed2007-03-02 16:08:05 -050058
Chris Mason7f5c1512007-03-23 15:56:19 -040059 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
60 trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -050061 *cow_ret = buf;
62 return 0;
63 }
Chris Masone089f052007-03-16 16:20:31 -040064 cow = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -040065 cow_node = btrfs_buffer_node(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040066 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
67 WARN_ON(1);
Chris Masone20d96d2007-03-22 12:13:20 -040068 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
Chris Mason7eccb902007-04-11 15:53:25 -040069 btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow));
Chris Mason7f5c1512007-03-23 15:56:19 -040070 btrfs_set_header_generation(&cow_node->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -040071 btrfs_set_header_owner(&cow_node->header, root->root_key.objectid);
Chris Masone089f052007-03-16 16:20:31 -040072 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050073 if (buf == root->node) {
74 root->node = cow;
Chris Masone20d96d2007-03-22 12:13:20 -040075 get_bh(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040076 if (buf != root->commit_root) {
Chris Mason7eccb902007-04-11 15:53:25 -040077 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
Chris Mason2c90e5d2007-04-02 10:50:19 -040078 }
Chris Mason234b63a2007-03-13 10:46:10 -040079 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050080 } else {
Chris Masone20d96d2007-03-22 12:13:20 -040081 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
Chris Mason7eccb902007-04-11 15:53:25 -040082 bh_blocknr(cow));
Chris Masond6025572007-03-30 14:27:56 -040083 btrfs_mark_buffer_dirty(parent);
Chris Mason7eccb902007-04-11 15:53:25 -040084 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050085 }
Chris Mason234b63a2007-03-13 10:46:10 -040086 btrfs_block_release(root, buf);
Chris Masondf24a2b2007-04-04 09:36:31 -040087 mark_buffer_dirty(cow);
Chris Mason2c90e5d2007-04-02 10:50:19 -040088 *cow_ret = cow;
Chris Mason02217ed2007-03-02 16:08:05 -050089 return 0;
90}
91
Chris Mason74123bd2007-02-02 11:05:29 -050092/*
93 * The leaf data grows from end-to-front in the node.
94 * this returns the address of the start of the last item,
95 * which is the stop of the leaf data stack
96 */
Chris Mason123abc82007-03-14 14:14:43 -040097static inline unsigned int leaf_data_end(struct btrfs_root *root,
98 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050099{
Chris Mason7518a232007-03-12 12:01:18 -0400100 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500101 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400102 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -0400103 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500104}
105
Chris Mason74123bd2007-02-02 11:05:29 -0500106/*
Chris Mason74123bd2007-02-02 11:05:29 -0500107 * compare two keys in a memcmp fashion
108 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400109static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500110{
Chris Masone2fa7222007-03-12 16:22:34 -0400111 struct btrfs_key k1;
112
113 btrfs_disk_key_to_cpu(&k1, disk);
114
115 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500116 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400117 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500118 return -1;
Chris Masonf254e522007-03-29 15:15:27 -0400119 if (k1.flags > k2->flags)
120 return 1;
121 if (k1.flags < k2->flags)
122 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400123 if (k1.offset > k2->offset)
124 return 1;
125 if (k1.offset < k2->offset)
126 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500127 return 0;
128}
Chris Mason74123bd2007-02-02 11:05:29 -0500129
Chris Mason123abc82007-03-14 14:14:43 -0400130static int check_node(struct btrfs_root *root, struct btrfs_path *path,
131 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500132{
133 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400134 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400135 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500136 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400137 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500138
139 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400140 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500141 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400142 BUG_ON(nritems == 0);
143 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400144 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400145 parent_key = &parent->ptrs[parent_slot].key;
146 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400147 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400148 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400149 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500150 }
Chris Mason123abc82007-03-14 14:14:43 -0400151 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400152 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400153 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400154 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
155 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500156 }
157 return 0;
158}
159
Chris Mason123abc82007-03-14 14:14:43 -0400160static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
161 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500162{
163 int i;
Chris Masone20d96d2007-03-22 12:13:20 -0400164 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400165 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500166 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400167 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500168
169 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400170 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500171 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400172 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400173
174 if (nritems == 0)
175 return 0;
176
177 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400178 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400179 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500180 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400181 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400182 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400183 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500184 }
Chris Mason7518a232007-03-12 12:01:18 -0400185 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400186 struct btrfs_key cpukey;
187 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500188 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400189 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400190 BUG_ON(btrfs_item_offset(leaf->items + i) !=
191 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500192 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400193 BUG_ON(btrfs_item_offset(leaf->items + i) +
194 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400195 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500196 }
197 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500198 return 0;
199}
200
Chris Mason123abc82007-03-14 14:14:43 -0400201static int check_block(struct btrfs_root *root, struct btrfs_path *path,
202 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500203{
Chris Mason3eb03142007-04-05 14:28:50 -0400204 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
205 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
206 sizeof(node->header.fsid)))
207 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500208 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400209 return check_leaf(root, path, level);
210 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500211}
212
Chris Mason74123bd2007-02-02 11:05:29 -0500213/*
214 * search for key in the array p. items p are item_size apart
215 * and there are 'max' items in p
216 * the slot in the array is returned via slot, and it points to
217 * the place where you would insert key if it is not found in
218 * the array.
219 *
220 * slot may point to max if the key is bigger than all of the keys
221 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400222static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500223 int max, int *slot)
224{
225 int low = 0;
226 int high = max;
227 int mid;
228 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400229 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500230
231 while(low < high) {
232 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400233 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500234 ret = comp_keys(tmp, key);
235
236 if (ret < 0)
237 low = mid + 1;
238 else if (ret > 0)
239 high = mid;
240 else {
241 *slot = mid;
242 return 0;
243 }
244 }
245 *slot = low;
246 return 1;
247}
248
Chris Mason97571fd2007-02-24 13:39:08 -0500249/*
250 * simple bin_search frontend that does the right thing for
251 * leaves vs nodes
252 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400253static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500254{
Chris Mason7518a232007-03-12 12:01:18 -0400255 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400256 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400257 return generic_bin_search((void *)l->items,
258 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400259 key, btrfs_header_nritems(&c->header),
260 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500261 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400262 return generic_bin_search((void *)c->ptrs,
263 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400264 key, btrfs_header_nritems(&c->header),
265 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500266 }
267 return -1;
268}
269
Chris Masone20d96d2007-03-22 12:13:20 -0400270static struct buffer_head *read_node_slot(struct btrfs_root *root,
271 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500272 int slot)
273{
Chris Masone20d96d2007-03-22 12:13:20 -0400274 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500275 if (slot < 0)
276 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400277 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500278 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400279 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500280}
281
Chris Masone089f052007-03-16 16:20:31 -0400282static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
283 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500284{
Chris Masone20d96d2007-03-22 12:13:20 -0400285 struct buffer_head *right_buf;
286 struct buffer_head *mid_buf;
287 struct buffer_head *left_buf;
288 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400289 struct btrfs_node *right = NULL;
290 struct btrfs_node *mid;
291 struct btrfs_node *left = NULL;
292 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500293 int ret = 0;
294 int wret;
295 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500296 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500297 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500298
299 if (level == 0)
300 return 0;
301
302 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400303 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400304 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500305
Chris Mason234b63a2007-03-13 10:46:10 -0400306 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500307 parent_buf = path->nodes[level + 1];
308 pslot = path->slots[level + 1];
309
Chris Mason40689472007-03-17 14:29:23 -0400310 /*
311 * deal with the case where there is only one pointer in the root
312 * by promoting the node below to a root
313 */
Chris Masonbb803952007-03-01 12:04:21 -0500314 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400315 struct buffer_head *child;
Chris Mason7eccb902007-04-11 15:53:25 -0400316 u64 blocknr = bh_blocknr(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500317
Chris Mason7518a232007-03-12 12:01:18 -0400318 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500319 return 0;
320
321 /* promote the child to a root */
322 child = read_node_slot(root, mid_buf, 0);
323 BUG_ON(!child);
324 root->node = child;
325 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400326 clean_tree_block(trans, root, mid_buf);
327 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500328 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400329 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500330 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400331 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400332 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500333 }
Chris Masone20d96d2007-03-22 12:13:20 -0400334 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500335
Chris Mason123abc82007-03-14 14:14:43 -0400336 if (btrfs_header_nritems(&mid->header) >
337 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500338 return 0;
339
Chris Masonbb803952007-03-01 12:04:21 -0500340 left_buf = read_node_slot(root, parent_buf, pslot - 1);
341 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500342
343 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500344 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400345 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
346 &left_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400347 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400348 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400349 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500350 if (wret < 0)
351 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500352 }
Chris Mason79f95c82007-03-01 15:16:26 -0500353
354 /*
355 * then try to empty the right most buffer into the middle
356 */
Chris Masonbb803952007-03-01 12:04:21 -0500357 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400358 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
359 &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400360 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400361 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500362 if (wret < 0)
363 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400364 if (btrfs_header_nritems(&right->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -0400365 u64 blocknr = bh_blocknr(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400366 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400367 wait_on_buffer(right_buf);
368 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500369 right_buf = NULL;
370 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400371 wret = del_ptr(trans, root, path, level + 1, pslot +
372 1);
Chris Masonbb803952007-03-01 12:04:21 -0500373 if (wret)
374 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400375 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500376 if (wret)
377 ret = wret;
378 } else {
Chris Masond6025572007-03-30 14:27:56 -0400379 btrfs_memcpy(root, parent,
380 &parent->ptrs[pslot + 1].key,
381 &right->ptrs[0].key,
382 sizeof(struct btrfs_disk_key));
383 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500384 }
385 }
Chris Mason7518a232007-03-12 12:01:18 -0400386 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500387 /*
388 * we're not allowed to leave a node with one item in the
389 * tree during a delete. A deletion from lower in the tree
390 * could try to delete the only pointer in this node.
391 * So, pull some keys from the left.
392 * There has to be a left pointer at this point because
393 * otherwise we would have pulled some pointers from the
394 * right
395 */
396 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400397 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500398 if (wret < 0)
399 ret = wret;
400 BUG_ON(wret == 1);
401 }
Chris Mason7518a232007-03-12 12:01:18 -0400402 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500403 /* we've managed to empty the middle node, drop it */
Chris Mason7eccb902007-04-11 15:53:25 -0400404 u64 blocknr = bh_blocknr(mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400405 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400406 wait_on_buffer(mid_buf);
407 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500408 mid_buf = NULL;
409 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400410 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500411 if (wret)
412 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400413 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500414 if (wret)
415 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500416 } else {
417 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400418 btrfs_memcpy(root, parent,
419 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
420 sizeof(struct btrfs_disk_key));
421 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500422 }
Chris Masonbb803952007-03-01 12:04:21 -0500423
Chris Mason79f95c82007-03-01 15:16:26 -0500424 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500425 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400426 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400427 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500428 path->nodes[level] = left_buf;
429 path->slots[level + 1] -= 1;
430 path->slots[level] = orig_slot;
431 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400432 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500433 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400434 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500435 path->slots[level] = orig_slot;
436 }
437 }
Chris Mason79f95c82007-03-01 15:16:26 -0500438 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400439 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400440 if (orig_ptr !=
441 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
442 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500443 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500444
445 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400446 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500447 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400448 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500449 return ret;
450}
451
Chris Masone66f7092007-04-20 13:16:02 -0400452/* returns zero if the push worked, non-zero otherwise */
453static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
454 struct btrfs_root *root,
455 struct btrfs_path *path, int level)
456{
457 struct buffer_head *right_buf;
458 struct buffer_head *mid_buf;
459 struct buffer_head *left_buf;
460 struct buffer_head *parent_buf = NULL;
461 struct btrfs_node *right = NULL;
462 struct btrfs_node *mid;
463 struct btrfs_node *left = NULL;
464 struct btrfs_node *parent = NULL;
465 int ret = 0;
466 int wret;
467 int pslot;
468 int orig_slot = path->slots[level];
469 u64 orig_ptr;
470
471 if (level == 0)
472 return 1;
473
474 mid_buf = path->nodes[level];
475 mid = btrfs_buffer_node(mid_buf);
476 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
477
478 if (level < BTRFS_MAX_LEVEL - 1)
479 parent_buf = path->nodes[level + 1];
480 pslot = path->slots[level + 1];
481
482 if (!parent_buf)
483 return 1;
484 parent = btrfs_buffer_node(parent_buf);
485
486 left_buf = read_node_slot(root, parent_buf, pslot - 1);
487
488 /* first, try to make some room in the middle buffer */
489 if (left_buf) {
490 u32 left_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400491 left = btrfs_buffer_node(left_buf);
492 left_nr = btrfs_header_nritems(&left->header);
Chris Mason33ade1f2007-04-20 13:48:57 -0400493 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
494 wret = 1;
495 } else {
496 btrfs_cow_block(trans, root, left_buf, parent_buf,
497 pslot - 1, &left_buf);
498 left = btrfs_buffer_node(left_buf);
499 wret = push_node_left(trans, root, left_buf, mid_buf);
500 }
Chris Masone66f7092007-04-20 13:16:02 -0400501 if (wret < 0)
502 ret = wret;
503 if (wret == 0) {
504 orig_slot += left_nr;
505 btrfs_memcpy(root, parent,
506 &parent->ptrs[pslot].key,
507 &mid->ptrs[0].key,
508 sizeof(struct btrfs_disk_key));
509 btrfs_mark_buffer_dirty(parent_buf);
510 if (btrfs_header_nritems(&left->header) > orig_slot) {
511 path->nodes[level] = left_buf;
512 path->slots[level + 1] -= 1;
513 path->slots[level] = orig_slot;
514 btrfs_block_release(root, mid_buf);
515 } else {
516 orig_slot -=
517 btrfs_header_nritems(&left->header);
518 path->slots[level] = orig_slot;
519 btrfs_block_release(root, left_buf);
520 }
521 check_node(root, path, level);
522 return 0;
523 }
524 btrfs_block_release(root, left_buf);
525 }
526 right_buf = read_node_slot(root, parent_buf, pslot + 1);
527
528 /*
529 * then try to empty the right most buffer into the middle
530 */
531 if (right_buf) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400532 u32 right_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400533 right = btrfs_buffer_node(right_buf);
Chris Mason33ade1f2007-04-20 13:48:57 -0400534 right_nr = btrfs_header_nritems(&right->header);
535 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
536 wret = 1;
537 } else {
538 btrfs_cow_block(trans, root, right_buf,
539 parent_buf, pslot + 1, &right_buf);
540 right = btrfs_buffer_node(right_buf);
541 wret = balance_node_right(trans, root,
542 right_buf, mid_buf);
543 }
Chris Masone66f7092007-04-20 13:16:02 -0400544 if (wret < 0)
545 ret = wret;
546 if (wret == 0) {
547 btrfs_memcpy(root, parent,
548 &parent->ptrs[pslot + 1].key,
549 &right->ptrs[0].key,
550 sizeof(struct btrfs_disk_key));
551 btrfs_mark_buffer_dirty(parent_buf);
552 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
553 path->nodes[level] = right_buf;
554 path->slots[level + 1] += 1;
555 path->slots[level] = orig_slot -
556 btrfs_header_nritems(&mid->header);
557 btrfs_block_release(root, mid_buf);
558 } else {
559 btrfs_block_release(root, right_buf);
560 }
561 check_node(root, path, level);
562 return 0;
563 }
564 btrfs_block_release(root, right_buf);
565 }
566 check_node(root, path, level);
567 return 1;
568}
569
Chris Mason74123bd2007-02-02 11:05:29 -0500570/*
571 * look for key in the tree. path is filled in with nodes along the way
572 * if key is found, we return zero and you can find the item in the leaf
573 * level of the path (level 0)
574 *
575 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500576 * be inserted, and 1 is returned. If there are other errors during the
577 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500578 *
579 * if ins_len > 0, nodes and leaves will be split as we walk down the
580 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
581 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500582 */
Chris Masone089f052007-03-16 16:20:31 -0400583int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
584 *root, struct btrfs_key *key, struct btrfs_path *p, int
585 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500586{
Chris Masone20d96d2007-03-22 12:13:20 -0400587 struct buffer_head *b;
588 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400589 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500590 int slot;
591 int ret;
592 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500593
Chris Mason22b0ebd2007-03-30 08:47:31 -0400594 WARN_ON(p->nodes[0] != NULL);
595 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500596again:
597 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400598 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500599 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400600 c = btrfs_buffer_node(b);
601 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500602 if (cow) {
603 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400604 wret = btrfs_cow_block(trans, root, b,
605 p->nodes[level + 1],
606 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400607 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500608 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400609 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500610 }
611 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400612 if (level != btrfs_header_level(&c->header))
613 WARN_ON(1);
614 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500615 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400616 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500617 if (ret)
618 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500619 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400620 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500621 if (ret && slot > 0)
622 slot -= 1;
623 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400624 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
625 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400626 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500627 BUG_ON(sret > 0);
628 if (sret)
629 return sret;
630 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400631 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500632 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500633 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400634 int sret = balance_level(trans, root, p,
635 level);
Chris Masonbb803952007-03-01 12:04:21 -0500636 if (sret)
637 return sret;
638 b = p->nodes[level];
639 if (!b)
640 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400641 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500642 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400643 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500644 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400645 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500646 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400647 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500648 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400649 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400650 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400651 int sret = split_leaf(trans, root, key,
652 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500653 BUG_ON(sret > 0);
654 if (sret)
655 return sret;
656 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500657 return ret;
658 }
659 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500660 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500661}
662
Chris Mason74123bd2007-02-02 11:05:29 -0500663/*
664 * adjust the pointers going up the tree, starting at level
665 * making sure the right key of each node is points to 'key'.
666 * This is used after shifting pointers to the left, so it stops
667 * fixing up pointers when a given leaf/node is not in slot 0 of the
668 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500669 *
670 * If this fails to write a tree block, it returns -1, but continues
671 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500672 */
Chris Masone089f052007-03-16 16:20:31 -0400673static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
674 *root, struct btrfs_path *path, struct btrfs_disk_key
675 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500676{
677 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500678 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400679 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
680 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500681 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500682 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500683 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400684 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400685 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
686 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500687 if (tslot != 0)
688 break;
689 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500690 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500691}
692
Chris Mason74123bd2007-02-02 11:05:29 -0500693/*
694 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500695 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500696 *
697 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
698 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500699 */
Chris Masone089f052007-03-16 16:20:31 -0400700static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400701 *root, struct buffer_head *dst_buf, struct
702 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500703{
Chris Masone20d96d2007-03-22 12:13:20 -0400704 struct btrfs_node *src = btrfs_buffer_node(src_buf);
705 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500706 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500707 int src_nritems;
708 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500709 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500710
Chris Mason7518a232007-03-12 12:01:18 -0400711 src_nritems = btrfs_header_nritems(&src->header);
712 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400713 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500714 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500715 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500716 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500717
718 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500719 push_items = src_nritems;
720
Chris Masond6025572007-03-30 14:27:56 -0400721 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
722 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500723 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400724 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400725 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400726 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500727 }
Chris Mason7518a232007-03-12 12:01:18 -0400728 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
729 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -0400730 btrfs_mark_buffer_dirty(src_buf);
731 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500732 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500733}
734
Chris Mason97571fd2007-02-24 13:39:08 -0500735/*
Chris Mason79f95c82007-03-01 15:16:26 -0500736 * try to push data from one node into the next node right in the
737 * tree.
738 *
739 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
740 * error, and > 0 if there was no room in the right hand block.
741 *
742 * this will only push up to 1/2 the contents of the left node over
743 */
Chris Masone089f052007-03-16 16:20:31 -0400744static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400745 btrfs_root *root, struct buffer_head *dst_buf,
746 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500747{
Chris Masone20d96d2007-03-22 12:13:20 -0400748 struct btrfs_node *src = btrfs_buffer_node(src_buf);
749 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500750 int push_items = 0;
751 int max_push;
752 int src_nritems;
753 int dst_nritems;
754 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500755
Chris Mason7518a232007-03-12 12:01:18 -0400756 src_nritems = btrfs_header_nritems(&src->header);
757 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400758 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500759 if (push_items <= 0) {
760 return 1;
761 }
762
763 max_push = src_nritems / 2 + 1;
764 /* don't try to empty the node */
765 if (max_push > src_nritems)
766 return 1;
767 if (max_push < push_items)
768 push_items = max_push;
769
Chris Masond6025572007-03-30 14:27:56 -0400770 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
771 dst_nritems * sizeof(struct btrfs_key_ptr));
772
773 btrfs_memcpy(root, dst, dst->ptrs,
774 src->ptrs + src_nritems - push_items,
775 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500776
Chris Mason7518a232007-03-12 12:01:18 -0400777 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
778 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500779
Chris Masond6025572007-03-30 14:27:56 -0400780 btrfs_mark_buffer_dirty(src_buf);
781 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500782 return ret;
783}
784
785/*
Chris Mason97571fd2007-02-24 13:39:08 -0500786 * helper function to insert a new root level in the tree.
787 * A new node is allocated, and a single item is inserted to
788 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500789 *
790 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500791 */
Chris Masone089f052007-03-16 16:20:31 -0400792static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
793 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500794{
Chris Masone20d96d2007-03-22 12:13:20 -0400795 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400796 struct btrfs_node *lower;
797 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400798 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500799
800 BUG_ON(path->nodes[level]);
801 BUG_ON(path->nodes[level-1] != root->node);
802
Chris Masone089f052007-03-16 16:20:31 -0400803 t = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400804 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400805 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400806 btrfs_set_header_nritems(&c->header, 1);
807 btrfs_set_header_level(&c->header, level);
Chris Mason7eccb902007-04-11 15:53:25 -0400808 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
Chris Mason7f5c1512007-03-23 15:56:19 -0400809 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -0400810 btrfs_set_header_owner(&c->header, root->root_key.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -0400811 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -0400812 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
813 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400814 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400815 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500816 else
Chris Mason123abc82007-03-14 14:14:43 -0400817 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -0400818 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
819 sizeof(struct btrfs_disk_key));
Chris Mason7eccb902007-04-11 15:53:25 -0400820 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
Chris Masond5719762007-03-23 10:01:08 -0400821
Chris Masond6025572007-03-30 14:27:56 -0400822 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -0400823
Chris Mason5c680ed2007-02-22 11:39:13 -0500824 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400825 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500826 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400827 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500828 path->nodes[level] = t;
829 path->slots[level] = 0;
830 return 0;
831}
832
Chris Mason74123bd2007-02-02 11:05:29 -0500833/*
834 * worker function to insert a single pointer in a node.
835 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500836 *
Chris Mason74123bd2007-02-02 11:05:29 -0500837 * slot and level indicate where you want the key to go, and
838 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500839 *
840 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500841 */
Chris Masone089f052007-03-16 16:20:31 -0400842static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
843 *root, struct btrfs_path *path, struct btrfs_disk_key
844 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500845{
Chris Mason234b63a2007-03-13 10:46:10 -0400846 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500847 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500848
849 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400850 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400851 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500852 if (slot > nritems)
853 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400854 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500855 BUG();
856 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400857 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
858 lower->ptrs + slot,
859 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500860 }
Chris Masond6025572007-03-30 14:27:56 -0400861 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
862 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400863 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400864 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -0400865 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500866 return 0;
867}
868
Chris Mason97571fd2007-02-24 13:39:08 -0500869/*
870 * split the node at the specified level in path in two.
871 * The path is corrected to point to the appropriate node after the split
872 *
873 * Before splitting this tries to make some room in the node by pushing
874 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500875 *
876 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500877 */
Chris Masone089f052007-03-16 16:20:31 -0400878static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
879 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500880{
Chris Masone20d96d2007-03-22 12:13:20 -0400881 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400882 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400883 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400884 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500885 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500886 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500887 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400888 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500889
Chris Mason5c680ed2007-02-22 11:39:13 -0500890 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400891 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500892 if (t == root->node) {
893 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400894 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500895 if (ret)
896 return ret;
Chris Masone66f7092007-04-20 13:16:02 -0400897 } else {
898 ret = push_nodes_for_insert(trans, root, path, level);
899 t = path->nodes[level];
900 c = btrfs_buffer_node(t);
901 if (!ret &&
902 btrfs_header_nritems(&c->header) <
903 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
904 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500905 }
Chris Masone66f7092007-04-20 13:16:02 -0400906
Chris Mason7518a232007-03-12 12:01:18 -0400907 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400908 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Masone20d96d2007-03-22 12:13:20 -0400909 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400910 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -0400911 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Mason7eccb902007-04-11 15:53:25 -0400912 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -0400913 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -0400914 btrfs_set_header_owner(&split->header, root->root_key.objectid);
Chris Mason3eb03142007-04-05 14:28:50 -0400915 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
916 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400917 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -0400918 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
919 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400920 btrfs_set_header_nritems(&split->header, c_nritems - mid);
921 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500922 ret = 0;
923
Chris Masond6025572007-03-30 14:27:56 -0400924 btrfs_mark_buffer_dirty(t);
925 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -0400926 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -0400927 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400928 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500929 if (wret)
930 ret = wret;
931
Chris Mason5de08d72007-02-24 06:24:44 -0500932 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500933 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400934 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500935 path->nodes[level] = split_buffer;
936 path->slots[level + 1] += 1;
937 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400938 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500939 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500940 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500941}
942
Chris Mason74123bd2007-02-02 11:05:29 -0500943/*
944 * how many bytes are required to store the items in a leaf. start
945 * and nr indicate which items in the leaf to check. This totals up the
946 * space used both by the item structs and the item data
947 */
Chris Mason234b63a2007-03-13 10:46:10 -0400948static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500949{
950 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -0400951 int nritems = btrfs_header_nritems(&l->header);
952 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500953
954 if (!nr)
955 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400956 data_len = btrfs_item_end(l->items + start);
957 data_len = data_len - btrfs_item_offset(l->items + end);
958 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -0400959 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500960 return data_len;
961}
962
Chris Mason74123bd2007-02-02 11:05:29 -0500963/*
Chris Masond4dbff92007-04-04 14:08:15 -0400964 * The space between the end of the leaf items and
965 * the start of the leaf data. IOW, how much room
966 * the leaf has left for both items and data
967 */
968int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
969{
970 int nritems = btrfs_header_nritems(&leaf->header);
971 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
972}
973
974/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500975 * push some data in the path leaf to the right, trying to free up at
976 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500977 *
978 * returns 1 if the push failed because the other node didn't have enough
979 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500980 */
Chris Masone089f052007-03-16 16:20:31 -0400981static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
982 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500983{
Chris Masone20d96d2007-03-22 12:13:20 -0400984 struct buffer_head *left_buf = path->nodes[0];
985 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400986 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -0400987 struct buffer_head *right_buf;
988 struct buffer_head *upper;
989 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -0500990 int slot;
991 int i;
992 int free_space;
993 int push_space = 0;
994 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400995 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400996 u32 left_nritems;
997 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500998
999 slot = path->slots[1];
1000 if (!path->nodes[1]) {
1001 return 1;
1002 }
1003 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001004 upper_node = btrfs_buffer_node(upper);
1005 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001006 return 1;
1007 }
Chris Masone20d96d2007-03-22 12:13:20 -04001008 right_buf = read_tree_block(root,
1009 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1010 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001011 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001012 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001013 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001014 return 1;
1015 }
Chris Mason02217ed2007-03-02 16:08:05 -05001016 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -04001017 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -04001018 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001019 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001020 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001021 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -05001022 return 1;
1023 }
1024
Chris Mason7518a232007-03-12 12:01:18 -04001025 left_nritems = btrfs_header_nritems(&left->header);
Chris Masona429e512007-04-18 16:15:28 -04001026 if (left_nritems == 0) {
1027 btrfs_block_release(root, right_buf);
1028 return 1;
1029 }
1030 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001031 item = left->items + i;
1032 if (path->slots[0] == i)
1033 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001034 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1035 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001036 break;
1037 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001038 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001039 }
1040 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001041 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001042 return 1;
1043 }
Chris Masona429e512007-04-18 16:15:28 -04001044 if (push_items == left_nritems)
1045 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001046 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -05001047 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -04001048 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001049 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -05001050 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -04001051 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1052 leaf_data_end(root, right) - push_space,
1053 btrfs_leaf_data(right) +
1054 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1055 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -05001056 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -04001057 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1058 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1059 btrfs_leaf_data(left) + leaf_data_end(root, left),
1060 push_space);
1061 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -04001062 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001063 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -04001064 btrfs_memcpy(root, right, right->items, left->items +
1065 left_nritems - push_items,
1066 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001067
1068 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001069 right_nritems += push_items;
1070 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001071 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001072 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001073 btrfs_set_item_offset(right->items + i, push_space -
1074 btrfs_item_size(right->items + i));
1075 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001076 }
Chris Mason7518a232007-03-12 12:01:18 -04001077 left_nritems -= push_items;
1078 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001079
Chris Masond6025572007-03-30 14:27:56 -04001080 btrfs_mark_buffer_dirty(left_buf);
1081 btrfs_mark_buffer_dirty(right_buf);
Chris Masona429e512007-04-18 16:15:28 -04001082
Chris Masond6025572007-03-30 14:27:56 -04001083 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -04001084 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -04001085 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001086
Chris Mason00ec4c52007-02-24 12:47:20 -05001087 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001088 if (path->slots[0] >= left_nritems) {
1089 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001090 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -05001091 path->nodes[0] = right_buf;
1092 path->slots[1] += 1;
1093 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001094 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001095 }
1096 return 0;
1097}
1098/*
Chris Mason74123bd2007-02-02 11:05:29 -05001099 * push some data in the path leaf to the left, trying to free up at
1100 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1101 */
Chris Masone089f052007-03-16 16:20:31 -04001102static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1103 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001104{
Chris Masone20d96d2007-03-22 12:13:20 -04001105 struct buffer_head *right_buf = path->nodes[0];
1106 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1107 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001108 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001109 int slot;
1110 int i;
1111 int free_space;
1112 int push_space = 0;
1113 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001114 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001115 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001116 int ret = 0;
1117 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001118
1119 slot = path->slots[1];
1120 if (slot == 0) {
1121 return 1;
1122 }
1123 if (!path->nodes[1]) {
1124 return 1;
1125 }
Chris Masone20d96d2007-03-22 12:13:20 -04001126 t = read_tree_block(root,
1127 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1128 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001129 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001130 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001131 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001132 return 1;
1133 }
Chris Mason02217ed2007-03-02 16:08:05 -05001134
1135 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -04001136 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Masone20d96d2007-03-22 12:13:20 -04001137 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001138 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001139 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001140 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001141 return 1;
1142 }
1143
Chris Masona429e512007-04-18 16:15:28 -04001144 if (btrfs_header_nritems(&right->header) == 0) {
1145 btrfs_block_release(root, t);
1146 return 1;
1147 }
1148
1149 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001150 item = right->items + i;
1151 if (path->slots[0] == i)
1152 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001153 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1154 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001155 break;
1156 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001157 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001158 }
1159 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001160 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001161 return 1;
1162 }
Chris Masona429e512007-04-18 16:15:28 -04001163 if (push_items == btrfs_header_nritems(&right->header))
1164 WARN_ON(1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001165 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001166 btrfs_memcpy(root, left, left->items +
1167 btrfs_header_nritems(&left->header),
1168 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001169 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001170 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001171 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1172 leaf_data_end(root, left) - push_space,
1173 btrfs_leaf_data(right) +
1174 btrfs_item_offset(right->items + push_items - 1),
1175 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001176 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001177 BUG_ON(old_left_nritems < 0);
1178
Chris Mason0783fcf2007-03-12 20:12:07 -04001179 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001180 u32 ioff = btrfs_item_offset(left->items + i);
1181 btrfs_set_item_offset(left->items + i, ioff -
1182 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001183 btrfs_item_offset(left->items +
1184 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001185 }
Chris Mason7518a232007-03-12 12:01:18 -04001186 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001187
1188 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001189 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001190 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001191 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1192 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1193 btrfs_leaf_data(right) +
1194 leaf_data_end(root, right), push_space);
1195 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001196 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001197 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001198 btrfs_set_header_nritems(&right->header,
1199 btrfs_header_nritems(&right->header) -
1200 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001201 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001202
Chris Mason7518a232007-03-12 12:01:18 -04001203 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001204 btrfs_set_item_offset(right->items + i, push_space -
1205 btrfs_item_size(right->items + i));
1206 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001207 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001208
Chris Masond6025572007-03-30 14:27:56 -04001209 btrfs_mark_buffer_dirty(t);
1210 btrfs_mark_buffer_dirty(right_buf);
Chris Masone089f052007-03-16 16:20:31 -04001211 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001212 if (wret)
1213 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001214
1215 /* then fixup the leaf pointer in the path */
1216 if (path->slots[0] < push_items) {
1217 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001218 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001219 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001220 path->slots[1] -= 1;
1221 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001222 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001223 path->slots[0] -= push_items;
1224 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001225 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001226 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001227}
1228
Chris Mason74123bd2007-02-02 11:05:29 -05001229/*
1230 * split the path's leaf in two, making sure there is at least data_size
1231 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001232 *
1233 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001234 */
Chris Masone089f052007-03-16 16:20:31 -04001235static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001236 *root, struct btrfs_key *ins_key,
1237 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001238{
Chris Masone20d96d2007-03-22 12:13:20 -04001239 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001240 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001241 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001242 int mid;
1243 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001244 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001245 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001246 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001247 int data_copy_size;
1248 int rt_data_off;
1249 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001250 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001251 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001252 int double_split = 0;
1253 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001254
Chris Mason40689472007-03-17 14:29:23 -04001255 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001256 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001257 if (wret < 0)
1258 return wret;
1259 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001260 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001261 if (wret < 0)
1262 return wret;
1263 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001264 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001265 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001266
1267 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001268 if (btrfs_leaf_free_space(root, l) >=
1269 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001270 return 0;
1271
Chris Mason5c680ed2007-02-22 11:39:13 -05001272 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001273 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001274 if (ret)
1275 return ret;
1276 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001277 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001278 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001279 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001280 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001281 BUG_ON(!right_buffer);
Chris Masone20d96d2007-03-22 12:13:20 -04001282 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001283 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001284 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001285 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001286 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Mason7518a232007-03-12 12:01:18 -04001287 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001288 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1289 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001290 if (mid <= slot) {
1291 if (nritems == 1 ||
1292 leaf_space_used(l, mid, nritems - mid) + space_needed >
1293 BTRFS_LEAF_DATA_SIZE(root)) {
1294 if (slot >= nritems) {
1295 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1296 btrfs_set_header_nritems(&right->header, 0);
1297 wret = insert_ptr(trans, root, path,
1298 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001299 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001300 path->slots[1] + 1, 1);
1301 if (wret)
1302 ret = wret;
1303 btrfs_block_release(root, path->nodes[0]);
1304 path->nodes[0] = right_buffer;
1305 path->slots[0] = 0;
1306 path->slots[1] += 1;
1307 return ret;
1308 }
1309 mid = slot;
1310 double_split = 1;
1311 }
1312 } else {
1313 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1314 BTRFS_LEAF_DATA_SIZE(root)) {
1315 if (slot == 0) {
1316 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1317 btrfs_set_header_nritems(&right->header, 0);
1318 wret = insert_ptr(trans, root, path,
1319 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001320 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001321 path->slots[1] - 1, 1);
1322 if (wret)
1323 ret = wret;
1324 btrfs_block_release(root, path->nodes[0]);
1325 path->nodes[0] = right_buffer;
1326 path->slots[0] = 0;
1327 path->slots[1] -= 1;
Chris Masona429e512007-04-18 16:15:28 -04001328 if (path->slots[1] == 0) {
1329 wret = fixup_low_keys(trans, root,
1330 path, &disk_key, 1);
1331 if (wret)
1332 ret = wret;
1333 }
Chris Masond4dbff92007-04-04 14:08:15 -04001334 return ret;
1335 }
1336 mid = slot;
1337 double_split = 1;
1338 }
1339 }
1340 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001341 data_copy_size = btrfs_item_end(l->items + mid) -
1342 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001343 btrfs_memcpy(root, right, right->items, l->items + mid,
1344 (nritems - mid) * sizeof(struct btrfs_item));
1345 btrfs_memcpy(root, right,
1346 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1347 data_copy_size, btrfs_leaf_data(l) +
1348 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001349 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1350 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001351
Chris Mason0783fcf2007-03-12 20:12:07 -04001352 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001353 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001354 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1355 }
Chris Mason74123bd2007-02-02 11:05:29 -05001356
Chris Mason7518a232007-03-12 12:01:18 -04001357 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001358 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001359 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001360 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001361 if (wret)
1362 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001363 btrfs_mark_buffer_dirty(right_buffer);
1364 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001365 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001366 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001367 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001368 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001369 path->slots[0] -= mid;
1370 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001371 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001372 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001373 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001374
1375 if (!double_split)
1376 return ret;
1377 right_buffer = btrfs_alloc_free_block(trans, root);
1378 BUG_ON(!right_buffer);
1379 right = btrfs_buffer_leaf(right_buffer);
1380 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001381 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Masond4dbff92007-04-04 14:08:15 -04001382 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001383 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Masond4dbff92007-04-04 14:08:15 -04001384 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001385 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1386 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001387 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1388 btrfs_set_header_nritems(&right->header, 0);
1389 wret = insert_ptr(trans, root, path,
1390 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001391 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001392 path->slots[1], 1);
1393 if (wret)
1394 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001395 if (path->slots[1] == 0) {
1396 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1397 if (wret)
1398 ret = wret;
1399 }
Chris Masond4dbff92007-04-04 14:08:15 -04001400 btrfs_block_release(root, path->nodes[0]);
1401 path->nodes[0] = right_buffer;
1402 path->slots[0] = 0;
1403 check_node(root, path, 1);
1404 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001405 return ret;
1406}
1407
Chris Masonb18c6682007-04-17 13:26:50 -04001408int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1409 struct btrfs_root *root,
1410 struct btrfs_path *path,
1411 u32 new_size)
1412{
1413 int ret = 0;
1414 int slot;
1415 int slot_orig;
1416 struct btrfs_leaf *leaf;
1417 struct buffer_head *leaf_buf;
1418 u32 nritems;
1419 unsigned int data_end;
1420 unsigned int old_data_start;
1421 unsigned int old_size;
1422 unsigned int size_diff;
1423 int i;
1424
1425 slot_orig = path->slots[0];
1426 leaf_buf = path->nodes[0];
1427 leaf = btrfs_buffer_leaf(leaf_buf);
1428
1429 nritems = btrfs_header_nritems(&leaf->header);
1430 data_end = leaf_data_end(root, leaf);
1431
1432 slot = path->slots[0];
1433 old_data_start = btrfs_item_offset(leaf->items + slot);
1434 old_size = btrfs_item_size(leaf->items + slot);
1435 BUG_ON(old_size <= new_size);
1436 size_diff = old_size - new_size;
1437
1438 BUG_ON(slot < 0);
1439 BUG_ON(slot >= nritems);
1440
1441 /*
1442 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1443 */
1444 /* first correct the data pointers */
1445 for (i = slot; i < nritems; i++) {
1446 u32 ioff = btrfs_item_offset(leaf->items + i);
1447 btrfs_set_item_offset(leaf->items + i,
1448 ioff + size_diff);
1449 }
1450 /* shift the data */
Chris Masonb18c6682007-04-17 13:26:50 -04001451 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1452 data_end + size_diff, btrfs_leaf_data(leaf) +
1453 data_end, old_data_start + new_size - data_end);
1454 btrfs_set_item_size(leaf->items + slot, new_size);
1455 btrfs_mark_buffer_dirty(leaf_buf);
1456
1457 ret = 0;
1458 if (btrfs_leaf_free_space(root, leaf) < 0)
1459 BUG();
1460 check_leaf(root, path, 0);
1461 return ret;
1462}
1463
Chris Mason6567e832007-04-16 09:22:45 -04001464int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1465 *root, struct btrfs_path *path, u32 data_size)
1466{
1467 int ret = 0;
1468 int slot;
1469 int slot_orig;
1470 struct btrfs_leaf *leaf;
1471 struct buffer_head *leaf_buf;
1472 u32 nritems;
1473 unsigned int data_end;
1474 unsigned int old_data;
1475 unsigned int old_size;
1476 int i;
1477
1478 slot_orig = path->slots[0];
1479 leaf_buf = path->nodes[0];
1480 leaf = btrfs_buffer_leaf(leaf_buf);
1481
1482 nritems = btrfs_header_nritems(&leaf->header);
1483 data_end = leaf_data_end(root, leaf);
1484
1485 if (btrfs_leaf_free_space(root, leaf) < data_size)
1486 BUG();
1487 slot = path->slots[0];
1488 old_data = btrfs_item_end(leaf->items + slot);
1489
1490 BUG_ON(slot < 0);
1491 BUG_ON(slot >= nritems);
1492
1493 /*
1494 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1495 */
1496 /* first correct the data pointers */
1497 for (i = slot; i < nritems; i++) {
1498 u32 ioff = btrfs_item_offset(leaf->items + i);
1499 btrfs_set_item_offset(leaf->items + i,
1500 ioff - data_size);
1501 }
1502 /* shift the data */
1503 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1504 data_end - data_size, btrfs_leaf_data(leaf) +
1505 data_end, old_data - data_end);
1506 data_end = old_data;
1507 old_size = btrfs_item_size(leaf->items + slot);
1508 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1509 btrfs_mark_buffer_dirty(leaf_buf);
1510
1511 ret = 0;
1512 if (btrfs_leaf_free_space(root, leaf) < 0)
1513 BUG();
1514 check_leaf(root, path, 0);
1515 return ret;
1516}
1517
Chris Mason74123bd2007-02-02 11:05:29 -05001518/*
1519 * Given a key and some data, insert an item into the tree.
1520 * This does all the path init required, making room in the tree if needed.
1521 */
Chris Masone089f052007-03-16 16:20:31 -04001522int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1523 *root, struct btrfs_path *path, struct btrfs_key
1524 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001525{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001526 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001527 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001528 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001529 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001530 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001531 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001532 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001533 struct btrfs_disk_key disk_key;
1534
1535 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001536
Chris Mason74123bd2007-02-02 11:05:29 -05001537 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001538 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001539 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001540 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001541 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001542 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001543 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001544 if (ret < 0)
1545 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001546
Chris Mason62e27492007-03-15 12:56:47 -04001547 slot_orig = path->slots[0];
1548 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001549 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001550
Chris Mason7518a232007-03-12 12:01:18 -04001551 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001552 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001553
Chris Mason123abc82007-03-14 14:14:43 -04001554 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001555 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001556 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001557 }
Chris Mason62e27492007-03-15 12:56:47 -04001558 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001559 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001560 if (slot != nritems) {
1561 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001562 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563
1564 /*
1565 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1566 */
1567 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001568 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001569 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001570 btrfs_set_item_offset(leaf->items + i,
1571 ioff - data_size);
1572 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001573
1574 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001575 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1576 leaf->items + slot,
1577 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001578
1579 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001580 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1581 data_end - data_size, btrfs_leaf_data(leaf) +
1582 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001583 data_end = old_data;
1584 }
Chris Mason62e27492007-03-15 12:56:47 -04001585 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001586 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1587 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001588 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1589 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001590 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001591 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001592
1593 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001594 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001595 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001596
Chris Mason123abc82007-03-14 14:14:43 -04001597 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001598 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001599 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001600out:
Chris Mason62e27492007-03-15 12:56:47 -04001601 return ret;
1602}
1603
1604/*
1605 * Given a key and some data, insert an item into the tree.
1606 * This does all the path init required, making room in the tree if needed.
1607 */
Chris Masone089f052007-03-16 16:20:31 -04001608int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1609 *root, struct btrfs_key *cpu_key, void *data, u32
1610 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001611{
1612 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001613 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001614 u8 *ptr;
1615
Chris Mason2c90e5d2007-04-02 10:50:19 -04001616 path = btrfs_alloc_path();
1617 BUG_ON(!path);
1618 btrfs_init_path(path);
1619 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001620 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001621 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1622 path->slots[0], u8);
1623 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001624 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001625 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001626 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001627 btrfs_release_path(root, path);
1628 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001629 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001630}
1631
Chris Mason74123bd2007-02-02 11:05:29 -05001632/*
Chris Mason5de08d72007-02-24 06:24:44 -05001633 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001634 *
1635 * If the delete empties a node, the node is removed from the tree,
1636 * continuing all the way the root if required. The root is converted into
1637 * a leaf if all the nodes are emptied.
1638 */
Chris Masone089f052007-03-16 16:20:31 -04001639static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1640 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001641{
Chris Mason234b63a2007-03-13 10:46:10 -04001642 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001643 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001644 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001645 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001646 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001647
Chris Masone20d96d2007-03-22 12:13:20 -04001648 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001649 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001650 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001651 btrfs_memmove(root, node, node->ptrs + slot,
1652 node->ptrs + slot + 1,
1653 sizeof(struct btrfs_key_ptr) *
1654 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001655 }
Chris Mason7518a232007-03-12 12:01:18 -04001656 nritems--;
1657 btrfs_set_header_nritems(&node->header, nritems);
1658 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001659 struct btrfs_header *header = btrfs_buffer_header(root->node);
1660 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001661 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001662 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001663 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001664 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001665 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001666 if (wret)
1667 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001668 }
Chris Masond6025572007-03-30 14:27:56 -04001669 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001670 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001671}
1672
Chris Mason74123bd2007-02-02 11:05:29 -05001673/*
1674 * delete the item at the leaf level in path. If that empties
1675 * the leaf, remove it from the tree
1676 */
Chris Masone089f052007-03-16 16:20:31 -04001677int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1678 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001679{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001680 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001681 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001682 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001683 int doff;
1684 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001685 int ret = 0;
1686 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001687 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001688
Chris Masoneb60cea2007-02-02 09:18:22 -05001689 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001690 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001691 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001692 doff = btrfs_item_offset(leaf->items + slot);
1693 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001694 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001695
Chris Mason7518a232007-03-12 12:01:18 -04001696 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001697 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001698 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04001699 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1700 data_end + dsize,
1701 btrfs_leaf_data(leaf) + data_end,
1702 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001703 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001704 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001705 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1706 }
Chris Masond6025572007-03-30 14:27:56 -04001707 btrfs_memmove(root, leaf, leaf->items + slot,
1708 leaf->items + slot + 1,
1709 sizeof(struct btrfs_item) *
1710 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001711 }
Chris Mason7518a232007-03-12 12:01:18 -04001712 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1713 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001714 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001715 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001716 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001717 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001718 } else {
Chris Masone089f052007-03-16 16:20:31 -04001719 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001720 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001721 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001722 if (wret)
1723 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001724 wret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001725 bh_blocknr(leaf_buf), 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001726 if (wret)
1727 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001728 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001729 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001730 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001731 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001732 wret = fixup_low_keys(trans, root, path,
1733 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001734 if (wret)
1735 ret = wret;
1736 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001737
Chris Mason74123bd2007-02-02 11:05:29 -05001738 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001739 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001740 /* push_leaf_left fixes the path.
1741 * make sure the path still points to our leaf
1742 * for possible call to del_ptr below
1743 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001744 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001745 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001746 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001747 if (wret < 0)
1748 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001749 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001750 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001751 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001752 if (wret < 0)
1753 ret = wret;
1754 }
Chris Mason7518a232007-03-12 12:01:18 -04001755 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -04001756 u64 blocknr = bh_blocknr(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001757 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001758 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001759 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001760 if (wret)
1761 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001762 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001763 wret = btrfs_free_extent(trans, root, blocknr,
1764 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001765 if (wret)
1766 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001767 } else {
Chris Masond6025572007-03-30 14:27:56 -04001768 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001769 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001770 }
Chris Masond5719762007-03-23 10:01:08 -04001771 } else {
Chris Masond6025572007-03-30 14:27:56 -04001772 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773 }
1774 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001775 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776}
1777
Chris Mason97571fd2007-02-24 13:39:08 -05001778/*
1779 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001780 * returns 0 if it found something or 1 if there are no greater leaves.
1781 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001782 */
Chris Mason234b63a2007-03-13 10:46:10 -04001783int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001784{
1785 int slot;
1786 int level = 1;
1787 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001788 struct buffer_head *c;
1789 struct btrfs_node *c_node;
1790 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001791
Chris Mason234b63a2007-03-13 10:46:10 -04001792 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001793 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001794 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001795 slot = path->slots[level] + 1;
1796 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001797 c_node = btrfs_buffer_node(c);
1798 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001799 level++;
1800 continue;
1801 }
Chris Masone20d96d2007-03-22 12:13:20 -04001802 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001803 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001804 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001805 next = read_tree_block(root, blocknr);
1806 break;
1807 }
1808 path->slots[level] = slot;
1809 while(1) {
1810 level--;
1811 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001812 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001813 path->nodes[level] = next;
1814 path->slots[level] = 0;
1815 if (!level)
1816 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001817 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001818 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001819 }
1820 return 0;
1821}