blob: 990d297a7a1395361d2aebd18f439dea9dc2c4b5 [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 Mason31f3c992007-04-30 15:25:45 -040064 cow = btrfs_alloc_free_block(trans, root, buf->b_blocknr);
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{
Chris Mason234b63a2007-03-13 10:46:10 -0400133 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400134 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500135 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400136 int slot;
137 struct btrfs_key cpukey;
Chris Mason7518a232007-03-12 12:01:18 -0400138 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500139
140 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400141 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500142 parent_slot = path->slots[level + 1];
Chris Mason8d7be552007-05-10 11:24:42 -0400143 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400144 BUG_ON(nritems == 0);
145 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400146 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400147 parent_key = &parent->ptrs[parent_slot].key;
148 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400149 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400150 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400151 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500152 }
Chris Mason123abc82007-03-14 14:14:43 -0400153 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400154 if (slot != 0) {
155 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key);
156 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0);
157 }
158 if (slot < nritems - 1) {
159 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key);
160 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500161 }
162 return 0;
163}
164
Chris Mason123abc82007-03-14 14:14:43 -0400165static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
166 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500167{
Chris Masone20d96d2007-03-22 12:13:20 -0400168 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400169 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500170 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400171 int slot = path->slots[0];
172 struct btrfs_key cpukey;
173
Chris Mason7518a232007-03-12 12:01:18 -0400174 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500175
176 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400177 parent = btrfs_buffer_node(path->nodes[level + 1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500178 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400179 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400180
181 if (nritems == 0)
182 return 0;
183
184 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400185 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400186 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500187 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400188 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400189 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400190 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500191 }
Chris Mason8d7be552007-05-10 11:24:42 -0400192 if (slot != 0) {
193 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
194 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
195 BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
196 btrfs_item_end(leaf->items + slot));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500197 }
Chris Mason8d7be552007-05-10 11:24:42 -0400198 if (slot < nritems - 1) {
199 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
200 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
201 BUG_ON(btrfs_item_offset(leaf->items + slot) !=
202 btrfs_item_end(leaf->items + slot + 1));
203 }
204 BUG_ON(btrfs_item_offset(leaf->items) +
205 btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500206 return 0;
207}
208
Chris Mason123abc82007-03-14 14:14:43 -0400209static int check_block(struct btrfs_root *root, struct btrfs_path *path,
210 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500211{
Chris Mason3eb03142007-04-05 14:28:50 -0400212 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
213 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
214 sizeof(node->header.fsid)))
215 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500216 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400217 return check_leaf(root, path, level);
218 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500219}
220
Chris Mason74123bd2007-02-02 11:05:29 -0500221/*
222 * search for key in the array p. items p are item_size apart
223 * and there are 'max' items in p
224 * the slot in the array is returned via slot, and it points to
225 * the place where you would insert key if it is not found in
226 * the array.
227 *
228 * slot may point to max if the key is bigger than all of the keys
229 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400230static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500231 int max, int *slot)
232{
233 int low = 0;
234 int high = max;
235 int mid;
236 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400237 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500238
239 while(low < high) {
240 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400241 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500242 ret = comp_keys(tmp, key);
243
244 if (ret < 0)
245 low = mid + 1;
246 else if (ret > 0)
247 high = mid;
248 else {
249 *slot = mid;
250 return 0;
251 }
252 }
253 *slot = low;
254 return 1;
255}
256
Chris Mason97571fd2007-02-24 13:39:08 -0500257/*
258 * simple bin_search frontend that does the right thing for
259 * leaves vs nodes
260 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400261static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500262{
Chris Mason7518a232007-03-12 12:01:18 -0400263 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400264 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400265 return generic_bin_search((void *)l->items,
266 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400267 key, btrfs_header_nritems(&c->header),
268 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500269 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400270 return generic_bin_search((void *)c->ptrs,
271 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400272 key, btrfs_header_nritems(&c->header),
273 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500274 }
275 return -1;
276}
277
Chris Masone20d96d2007-03-22 12:13:20 -0400278static struct buffer_head *read_node_slot(struct btrfs_root *root,
279 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500280 int slot)
281{
Chris Masone20d96d2007-03-22 12:13:20 -0400282 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500283 if (slot < 0)
284 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400285 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500286 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400287 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500288}
289
Chris Masone089f052007-03-16 16:20:31 -0400290static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
291 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500292{
Chris Masone20d96d2007-03-22 12:13:20 -0400293 struct buffer_head *right_buf;
294 struct buffer_head *mid_buf;
295 struct buffer_head *left_buf;
296 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400297 struct btrfs_node *right = NULL;
298 struct btrfs_node *mid;
299 struct btrfs_node *left = NULL;
300 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500301 int ret = 0;
302 int wret;
303 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500304 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500305 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500306
307 if (level == 0)
308 return 0;
309
310 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400311 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400312 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500313
Chris Mason234b63a2007-03-13 10:46:10 -0400314 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500315 parent_buf = path->nodes[level + 1];
316 pslot = path->slots[level + 1];
317
Chris Mason40689472007-03-17 14:29:23 -0400318 /*
319 * deal with the case where there is only one pointer in the root
320 * by promoting the node below to a root
321 */
Chris Masonbb803952007-03-01 12:04:21 -0500322 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400323 struct buffer_head *child;
Chris Mason7eccb902007-04-11 15:53:25 -0400324 u64 blocknr = bh_blocknr(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500325
Chris Mason7518a232007-03-12 12:01:18 -0400326 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500327 return 0;
328
329 /* promote the child to a root */
330 child = read_node_slot(root, mid_buf, 0);
331 BUG_ON(!child);
332 root->node = child;
333 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400334 clean_tree_block(trans, root, mid_buf);
335 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500336 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400337 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500338 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400339 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400340 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500341 }
Chris Masone20d96d2007-03-22 12:13:20 -0400342 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500343
Chris Mason123abc82007-03-14 14:14:43 -0400344 if (btrfs_header_nritems(&mid->header) >
345 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500346 return 0;
347
Chris Masonbb803952007-03-01 12:04:21 -0500348 left_buf = read_node_slot(root, parent_buf, pslot - 1);
349 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500350
351 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500352 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400353 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
354 &left_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400355 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400356 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400357 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500358 if (wret < 0)
359 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500360 }
Chris Mason79f95c82007-03-01 15:16:26 -0500361
362 /*
363 * then try to empty the right most buffer into the middle
364 */
Chris Masonbb803952007-03-01 12:04:21 -0500365 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400366 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
367 &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400368 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400369 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500370 if (wret < 0)
371 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400372 if (btrfs_header_nritems(&right->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -0400373 u64 blocknr = bh_blocknr(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400374 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400375 wait_on_buffer(right_buf);
376 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500377 right_buf = NULL;
378 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400379 wret = del_ptr(trans, root, path, level + 1, pslot +
380 1);
Chris Masonbb803952007-03-01 12:04:21 -0500381 if (wret)
382 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400383 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500384 if (wret)
385 ret = wret;
386 } else {
Chris Masond6025572007-03-30 14:27:56 -0400387 btrfs_memcpy(root, parent,
388 &parent->ptrs[pslot + 1].key,
389 &right->ptrs[0].key,
390 sizeof(struct btrfs_disk_key));
391 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500392 }
393 }
Chris Mason7518a232007-03-12 12:01:18 -0400394 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500395 /*
396 * we're not allowed to leave a node with one item in the
397 * tree during a delete. A deletion from lower in the tree
398 * could try to delete the only pointer in this node.
399 * So, pull some keys from the left.
400 * There has to be a left pointer at this point because
401 * otherwise we would have pulled some pointers from the
402 * right
403 */
404 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400405 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500406 if (wret < 0)
407 ret = wret;
408 BUG_ON(wret == 1);
409 }
Chris Mason7518a232007-03-12 12:01:18 -0400410 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500411 /* we've managed to empty the middle node, drop it */
Chris Mason7eccb902007-04-11 15:53:25 -0400412 u64 blocknr = bh_blocknr(mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400413 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400414 wait_on_buffer(mid_buf);
415 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500416 mid_buf = NULL;
417 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400418 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500419 if (wret)
420 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400421 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500422 if (wret)
423 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500424 } else {
425 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400426 btrfs_memcpy(root, parent,
427 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
428 sizeof(struct btrfs_disk_key));
429 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500430 }
Chris Masonbb803952007-03-01 12:04:21 -0500431
Chris Mason79f95c82007-03-01 15:16:26 -0500432 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500433 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400434 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400435 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500436 path->nodes[level] = left_buf;
437 path->slots[level + 1] -= 1;
438 path->slots[level] = orig_slot;
439 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400440 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500441 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400442 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500443 path->slots[level] = orig_slot;
444 }
445 }
Chris Mason79f95c82007-03-01 15:16:26 -0500446 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400447 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400448 if (orig_ptr !=
449 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
450 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500451 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500452
453 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400454 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500455 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400456 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500457 return ret;
458}
459
Chris Masone66f7092007-04-20 13:16:02 -0400460/* returns zero if the push worked, non-zero otherwise */
461static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
462 struct btrfs_root *root,
463 struct btrfs_path *path, int level)
464{
465 struct buffer_head *right_buf;
466 struct buffer_head *mid_buf;
467 struct buffer_head *left_buf;
468 struct buffer_head *parent_buf = NULL;
469 struct btrfs_node *right = NULL;
470 struct btrfs_node *mid;
471 struct btrfs_node *left = NULL;
472 struct btrfs_node *parent = NULL;
473 int ret = 0;
474 int wret;
475 int pslot;
476 int orig_slot = path->slots[level];
477 u64 orig_ptr;
478
479 if (level == 0)
480 return 1;
481
482 mid_buf = path->nodes[level];
483 mid = btrfs_buffer_node(mid_buf);
484 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
485
486 if (level < BTRFS_MAX_LEVEL - 1)
487 parent_buf = path->nodes[level + 1];
488 pslot = path->slots[level + 1];
489
490 if (!parent_buf)
491 return 1;
492 parent = btrfs_buffer_node(parent_buf);
493
494 left_buf = read_node_slot(root, parent_buf, pslot - 1);
495
496 /* first, try to make some room in the middle buffer */
497 if (left_buf) {
498 u32 left_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400499 left = btrfs_buffer_node(left_buf);
500 left_nr = btrfs_header_nritems(&left->header);
Chris Mason33ade1f2007-04-20 13:48:57 -0400501 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
502 wret = 1;
503 } else {
504 btrfs_cow_block(trans, root, left_buf, parent_buf,
505 pslot - 1, &left_buf);
506 left = btrfs_buffer_node(left_buf);
507 wret = push_node_left(trans, root, left_buf, mid_buf);
508 }
Chris Masone66f7092007-04-20 13:16:02 -0400509 if (wret < 0)
510 ret = wret;
511 if (wret == 0) {
512 orig_slot += left_nr;
513 btrfs_memcpy(root, parent,
514 &parent->ptrs[pslot].key,
515 &mid->ptrs[0].key,
516 sizeof(struct btrfs_disk_key));
517 btrfs_mark_buffer_dirty(parent_buf);
518 if (btrfs_header_nritems(&left->header) > orig_slot) {
519 path->nodes[level] = left_buf;
520 path->slots[level + 1] -= 1;
521 path->slots[level] = orig_slot;
522 btrfs_block_release(root, mid_buf);
523 } else {
524 orig_slot -=
525 btrfs_header_nritems(&left->header);
526 path->slots[level] = orig_slot;
527 btrfs_block_release(root, left_buf);
528 }
529 check_node(root, path, level);
530 return 0;
531 }
532 btrfs_block_release(root, left_buf);
533 }
534 right_buf = read_node_slot(root, parent_buf, pslot + 1);
535
536 /*
537 * then try to empty the right most buffer into the middle
538 */
539 if (right_buf) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400540 u32 right_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400541 right = btrfs_buffer_node(right_buf);
Chris Mason33ade1f2007-04-20 13:48:57 -0400542 right_nr = btrfs_header_nritems(&right->header);
543 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
544 wret = 1;
545 } else {
546 btrfs_cow_block(trans, root, right_buf,
547 parent_buf, pslot + 1, &right_buf);
548 right = btrfs_buffer_node(right_buf);
549 wret = balance_node_right(trans, root,
550 right_buf, mid_buf);
551 }
Chris Masone66f7092007-04-20 13:16:02 -0400552 if (wret < 0)
553 ret = wret;
554 if (wret == 0) {
555 btrfs_memcpy(root, parent,
556 &parent->ptrs[pslot + 1].key,
557 &right->ptrs[0].key,
558 sizeof(struct btrfs_disk_key));
559 btrfs_mark_buffer_dirty(parent_buf);
560 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
561 path->nodes[level] = right_buf;
562 path->slots[level + 1] += 1;
563 path->slots[level] = orig_slot -
564 btrfs_header_nritems(&mid->header);
565 btrfs_block_release(root, mid_buf);
566 } else {
567 btrfs_block_release(root, right_buf);
568 }
569 check_node(root, path, level);
570 return 0;
571 }
572 btrfs_block_release(root, right_buf);
573 }
574 check_node(root, path, level);
575 return 1;
576}
577
Chris Mason74123bd2007-02-02 11:05:29 -0500578/*
579 * look for key in the tree. path is filled in with nodes along the way
580 * if key is found, we return zero and you can find the item in the leaf
581 * level of the path (level 0)
582 *
583 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500584 * be inserted, and 1 is returned. If there are other errors during the
585 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500586 *
587 * if ins_len > 0, nodes and leaves will be split as we walk down the
588 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
589 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500590 */
Chris Masone089f052007-03-16 16:20:31 -0400591int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
592 *root, struct btrfs_key *key, struct btrfs_path *p, int
593 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500594{
Chris Masone20d96d2007-03-22 12:13:20 -0400595 struct buffer_head *b;
596 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400597 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500598 int slot;
599 int ret;
600 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500601
Chris Mason22b0ebd2007-03-30 08:47:31 -0400602 WARN_ON(p->nodes[0] != NULL);
603 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500604again:
605 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400606 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500607 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400608 c = btrfs_buffer_node(b);
609 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500610 if (cow) {
611 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400612 wret = btrfs_cow_block(trans, root, b,
613 p->nodes[level + 1],
614 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400615 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500616 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400617 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500618 }
619 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400620 if (level != btrfs_header_level(&c->header))
621 WARN_ON(1);
622 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500623 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400624 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500625 if (ret)
626 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500627 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400628 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500629 if (ret && slot > 0)
630 slot -= 1;
631 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400632 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
633 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400634 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500635 BUG_ON(sret > 0);
636 if (sret)
637 return sret;
638 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400639 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500640 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500641 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400642 int sret = balance_level(trans, root, p,
643 level);
Chris Masonbb803952007-03-01 12:04:21 -0500644 if (sret)
645 return sret;
646 b = p->nodes[level];
647 if (!b)
648 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400649 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500650 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400651 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500652 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400653 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500654 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400655 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500656 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400657 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400658 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400659 int sret = split_leaf(trans, root, key,
660 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500661 BUG_ON(sret > 0);
662 if (sret)
663 return sret;
664 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500665 return ret;
666 }
667 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500668 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500669}
670
Chris Mason74123bd2007-02-02 11:05:29 -0500671/*
672 * adjust the pointers going up the tree, starting at level
673 * making sure the right key of each node is points to 'key'.
674 * This is used after shifting pointers to the left, so it stops
675 * fixing up pointers when a given leaf/node is not in slot 0 of the
676 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500677 *
678 * If this fails to write a tree block, it returns -1, but continues
679 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500680 */
Chris Masone089f052007-03-16 16:20:31 -0400681static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
682 *root, struct btrfs_path *path, struct btrfs_disk_key
683 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500684{
685 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500686 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400687 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
688 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500689 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500690 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500691 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400692 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400693 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
694 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500695 if (tslot != 0)
696 break;
697 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500698 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500699}
700
Chris Mason74123bd2007-02-02 11:05:29 -0500701/*
702 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500703 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500704 *
705 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
706 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500707 */
Chris Masone089f052007-03-16 16:20:31 -0400708static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400709 *root, struct buffer_head *dst_buf, struct
710 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500711{
Chris Masone20d96d2007-03-22 12:13:20 -0400712 struct btrfs_node *src = btrfs_buffer_node(src_buf);
713 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500714 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500715 int src_nritems;
716 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500717 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500718
Chris Mason7518a232007-03-12 12:01:18 -0400719 src_nritems = btrfs_header_nritems(&src->header);
720 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400721 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500722 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500724 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500725
726 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500727 push_items = src_nritems;
728
Chris Masond6025572007-03-30 14:27:56 -0400729 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
730 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500731 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400732 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400733 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400734 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500735 }
Chris Mason7518a232007-03-12 12:01:18 -0400736 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
737 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -0400738 btrfs_mark_buffer_dirty(src_buf);
739 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500740 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500741}
742
Chris Mason97571fd2007-02-24 13:39:08 -0500743/*
Chris Mason79f95c82007-03-01 15:16:26 -0500744 * try to push data from one node into the next node right in the
745 * tree.
746 *
747 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
748 * error, and > 0 if there was no room in the right hand block.
749 *
750 * this will only push up to 1/2 the contents of the left node over
751 */
Chris Masone089f052007-03-16 16:20:31 -0400752static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -0400753 btrfs_root *root, struct buffer_head *dst_buf,
754 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500755{
Chris Masone20d96d2007-03-22 12:13:20 -0400756 struct btrfs_node *src = btrfs_buffer_node(src_buf);
757 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500758 int push_items = 0;
759 int max_push;
760 int src_nritems;
761 int dst_nritems;
762 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500763
Chris Mason7518a232007-03-12 12:01:18 -0400764 src_nritems = btrfs_header_nritems(&src->header);
765 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400766 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500767 if (push_items <= 0) {
768 return 1;
769 }
770
771 max_push = src_nritems / 2 + 1;
772 /* don't try to empty the node */
773 if (max_push > src_nritems)
774 return 1;
775 if (max_push < push_items)
776 push_items = max_push;
777
Chris Masond6025572007-03-30 14:27:56 -0400778 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
779 dst_nritems * sizeof(struct btrfs_key_ptr));
780
781 btrfs_memcpy(root, dst, dst->ptrs,
782 src->ptrs + src_nritems - push_items,
783 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500784
Chris Mason7518a232007-03-12 12:01:18 -0400785 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
786 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500787
Chris Masond6025572007-03-30 14:27:56 -0400788 btrfs_mark_buffer_dirty(src_buf);
789 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500790 return ret;
791}
792
793/*
Chris Mason97571fd2007-02-24 13:39:08 -0500794 * helper function to insert a new root level in the tree.
795 * A new node is allocated, and a single item is inserted to
796 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500797 *
798 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500799 */
Chris Masone089f052007-03-16 16:20:31 -0400800static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
801 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500802{
Chris Masone20d96d2007-03-22 12:13:20 -0400803 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400804 struct btrfs_node *lower;
805 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400806 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500807
808 BUG_ON(path->nodes[level]);
809 BUG_ON(path->nodes[level-1] != root->node);
810
Chris Mason31f3c992007-04-30 15:25:45 -0400811 t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr);
Chris Masone20d96d2007-03-22 12:13:20 -0400812 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -0400813 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400814 btrfs_set_header_nritems(&c->header, 1);
815 btrfs_set_header_level(&c->header, level);
Chris Mason7eccb902007-04-11 15:53:25 -0400816 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
Chris Mason7f5c1512007-03-23 15:56:19 -0400817 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -0400818 btrfs_set_header_owner(&c->header, root->root_key.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -0400819 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -0400820 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
821 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400822 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400823 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500824 else
Chris Mason123abc82007-03-14 14:14:43 -0400825 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -0400826 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
827 sizeof(struct btrfs_disk_key));
Chris Mason7eccb902007-04-11 15:53:25 -0400828 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
Chris Masond5719762007-03-23 10:01:08 -0400829
Chris Masond6025572007-03-30 14:27:56 -0400830 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -0400831
Chris Mason5c680ed2007-02-22 11:39:13 -0500832 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400833 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500834 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -0400835 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500836 path->nodes[level] = t;
837 path->slots[level] = 0;
838 return 0;
839}
840
Chris Mason74123bd2007-02-02 11:05:29 -0500841/*
842 * worker function to insert a single pointer in a node.
843 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500844 *
Chris Mason74123bd2007-02-02 11:05:29 -0500845 * slot and level indicate where you want the key to go, and
846 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500847 *
848 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500849 */
Chris Masone089f052007-03-16 16:20:31 -0400850static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
851 *root, struct btrfs_path *path, struct btrfs_disk_key
852 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500853{
Chris Mason234b63a2007-03-13 10:46:10 -0400854 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500855 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500856
857 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -0400858 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -0400859 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500860 if (slot > nritems)
861 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400862 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500863 BUG();
864 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400865 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
866 lower->ptrs + slot,
867 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500868 }
Chris Masond6025572007-03-30 14:27:56 -0400869 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
870 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400871 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400872 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -0400873 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500874 return 0;
875}
876
Chris Mason97571fd2007-02-24 13:39:08 -0500877/*
878 * split the node at the specified level in path in two.
879 * The path is corrected to point to the appropriate node after the split
880 *
881 * Before splitting this tries to make some room in the node by pushing
882 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500883 *
884 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500885 */
Chris Masone089f052007-03-16 16:20:31 -0400886static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
887 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500888{
Chris Masone20d96d2007-03-22 12:13:20 -0400889 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -0400890 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -0400891 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -0400892 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500893 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500894 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500895 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400896 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500897
Chris Mason5c680ed2007-02-22 11:39:13 -0500898 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400899 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500900 if (t == root->node) {
901 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400902 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500903 if (ret)
904 return ret;
Chris Masone66f7092007-04-20 13:16:02 -0400905 } else {
906 ret = push_nodes_for_insert(trans, root, path, level);
907 t = path->nodes[level];
908 c = btrfs_buffer_node(t);
909 if (!ret &&
910 btrfs_header_nritems(&c->header) <
911 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
912 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500913 }
Chris Masone66f7092007-04-20 13:16:02 -0400914
Chris Mason7518a232007-03-12 12:01:18 -0400915 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason31f3c992007-04-30 15:25:45 -0400916 split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr);
Chris Masone20d96d2007-03-22 12:13:20 -0400917 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -0400918 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -0400919 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Mason7eccb902007-04-11 15:53:25 -0400920 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -0400921 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -0400922 btrfs_set_header_owner(&split->header, root->root_key.objectid);
Chris Mason3eb03142007-04-05 14:28:50 -0400923 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
924 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -0400925 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -0400926 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
927 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400928 btrfs_set_header_nritems(&split->header, c_nritems - mid);
929 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500930 ret = 0;
931
Chris Masond6025572007-03-30 14:27:56 -0400932 btrfs_mark_buffer_dirty(t);
933 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -0400934 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -0400935 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -0400936 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500937 if (wret)
938 ret = wret;
939
Chris Mason5de08d72007-02-24 06:24:44 -0500940 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500941 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400942 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500943 path->nodes[level] = split_buffer;
944 path->slots[level + 1] += 1;
945 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400946 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500947 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500948 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500949}
950
Chris Mason74123bd2007-02-02 11:05:29 -0500951/*
952 * how many bytes are required to store the items in a leaf. start
953 * and nr indicate which items in the leaf to check. This totals up the
954 * space used both by the item structs and the item data
955 */
Chris Mason234b63a2007-03-13 10:46:10 -0400956static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500957{
958 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -0400959 int nritems = btrfs_header_nritems(&l->header);
960 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500961
962 if (!nr)
963 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400964 data_len = btrfs_item_end(l->items + start);
965 data_len = data_len - btrfs_item_offset(l->items + end);
966 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -0400967 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500968 return data_len;
969}
970
Chris Mason74123bd2007-02-02 11:05:29 -0500971/*
Chris Masond4dbff92007-04-04 14:08:15 -0400972 * The space between the end of the leaf items and
973 * the start of the leaf data. IOW, how much room
974 * the leaf has left for both items and data
975 */
976int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
977{
978 int nritems = btrfs_header_nritems(&leaf->header);
979 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
980}
981
982/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500983 * push some data in the path leaf to the right, trying to free up at
984 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500985 *
986 * returns 1 if the push failed because the other node didn't have enough
987 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500988 */
Chris Masone089f052007-03-16 16:20:31 -0400989static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
990 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500991{
Chris Masone20d96d2007-03-22 12:13:20 -0400992 struct buffer_head *left_buf = path->nodes[0];
993 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -0400994 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -0400995 struct buffer_head *right_buf;
996 struct buffer_head *upper;
997 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -0500998 int slot;
999 int i;
1000 int free_space;
1001 int push_space = 0;
1002 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001003 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001004 u32 left_nritems;
1005 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -05001006
1007 slot = path->slots[1];
1008 if (!path->nodes[1]) {
1009 return 1;
1010 }
1011 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001012 upper_node = btrfs_buffer_node(upper);
1013 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001014 return 1;
1015 }
Chris Masone20d96d2007-03-22 12:13:20 -04001016 right_buf = read_tree_block(root,
1017 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1018 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 Mason00ec4c52007-02-24 12:47:20 -05001022 return 1;
1023 }
Chris Mason02217ed2007-03-02 16:08:05 -05001024 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -04001025 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Masone20d96d2007-03-22 12:13:20 -04001026 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001027 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001028 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001029 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -05001030 return 1;
1031 }
1032
Chris Mason7518a232007-03-12 12:01:18 -04001033 left_nritems = btrfs_header_nritems(&left->header);
Chris Masona429e512007-04-18 16:15:28 -04001034 if (left_nritems == 0) {
1035 btrfs_block_release(root, right_buf);
1036 return 1;
1037 }
1038 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001039 item = left->items + i;
1040 if (path->slots[0] == i)
1041 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001042 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1043 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001044 break;
1045 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001046 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001047 }
1048 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001049 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001050 return 1;
1051 }
Chris Masona429e512007-04-18 16:15:28 -04001052 if (push_items == left_nritems)
1053 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001054 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -05001055 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -04001056 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001057 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -05001058 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -04001059 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1060 leaf_data_end(root, right) - push_space,
1061 btrfs_leaf_data(right) +
1062 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1063 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -05001064 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -04001065 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1066 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1067 btrfs_leaf_data(left) + leaf_data_end(root, left),
1068 push_space);
1069 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -04001070 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001071 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -04001072 btrfs_memcpy(root, right, right->items, left->items +
1073 left_nritems - push_items,
1074 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001075
1076 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001077 right_nritems += push_items;
1078 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001079 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001080 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001081 btrfs_set_item_offset(right->items + i, push_space -
1082 btrfs_item_size(right->items + i));
1083 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001084 }
Chris Mason7518a232007-03-12 12:01:18 -04001085 left_nritems -= push_items;
1086 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001087
Chris Masond6025572007-03-30 14:27:56 -04001088 btrfs_mark_buffer_dirty(left_buf);
1089 btrfs_mark_buffer_dirty(right_buf);
Chris Masona429e512007-04-18 16:15:28 -04001090
Chris Masond6025572007-03-30 14:27:56 -04001091 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -04001092 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -04001093 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001094
Chris Mason00ec4c52007-02-24 12:47:20 -05001095 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001096 if (path->slots[0] >= left_nritems) {
1097 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001098 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -05001099 path->nodes[0] = right_buf;
1100 path->slots[1] += 1;
1101 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001102 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001103 }
1104 return 0;
1105}
1106/*
Chris Mason74123bd2007-02-02 11:05:29 -05001107 * push some data in the path leaf to the left, trying to free up at
1108 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1109 */
Chris Masone089f052007-03-16 16:20:31 -04001110static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1111 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001112{
Chris Masone20d96d2007-03-22 12:13:20 -04001113 struct buffer_head *right_buf = path->nodes[0];
1114 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1115 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001116 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001117 int slot;
1118 int i;
1119 int free_space;
1120 int push_space = 0;
1121 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001122 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001123 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001124 int ret = 0;
1125 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001126
1127 slot = path->slots[1];
1128 if (slot == 0) {
1129 return 1;
1130 }
1131 if (!path->nodes[1]) {
1132 return 1;
1133 }
Chris Masone20d96d2007-03-22 12:13:20 -04001134 t = read_tree_block(root,
1135 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1136 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001137 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001138 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001139 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001140 return 1;
1141 }
Chris Mason02217ed2007-03-02 16:08:05 -05001142
1143 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -04001144 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Masone20d96d2007-03-22 12:13:20 -04001145 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001146 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001147 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001148 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001149 return 1;
1150 }
1151
Chris Masona429e512007-04-18 16:15:28 -04001152 if (btrfs_header_nritems(&right->header) == 0) {
1153 btrfs_block_release(root, t);
1154 return 1;
1155 }
1156
1157 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001158 item = right->items + i;
1159 if (path->slots[0] == i)
1160 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001161 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1162 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001163 break;
1164 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001165 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001166 }
1167 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001168 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001169 return 1;
1170 }
Chris Masona429e512007-04-18 16:15:28 -04001171 if (push_items == btrfs_header_nritems(&right->header))
1172 WARN_ON(1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001173 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001174 btrfs_memcpy(root, left, left->items +
1175 btrfs_header_nritems(&left->header),
1176 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001177 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001178 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001179 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1180 leaf_data_end(root, left) - push_space,
1181 btrfs_leaf_data(right) +
1182 btrfs_item_offset(right->items + push_items - 1),
1183 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001184 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001185 BUG_ON(old_left_nritems < 0);
1186
Chris Mason0783fcf2007-03-12 20:12:07 -04001187 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001188 u32 ioff = btrfs_item_offset(left->items + i);
1189 btrfs_set_item_offset(left->items + i, ioff -
1190 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001191 btrfs_item_offset(left->items +
1192 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001193 }
Chris Mason7518a232007-03-12 12:01:18 -04001194 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001195
1196 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001197 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001198 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001199 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1200 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1201 btrfs_leaf_data(right) +
1202 leaf_data_end(root, right), push_space);
1203 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001204 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001205 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001206 btrfs_set_header_nritems(&right->header,
1207 btrfs_header_nritems(&right->header) -
1208 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001209 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001210
Chris Mason7518a232007-03-12 12:01:18 -04001211 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001212 btrfs_set_item_offset(right->items + i, push_space -
1213 btrfs_item_size(right->items + i));
1214 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001215 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001216
Chris Masond6025572007-03-30 14:27:56 -04001217 btrfs_mark_buffer_dirty(t);
1218 btrfs_mark_buffer_dirty(right_buf);
Chris Masone089f052007-03-16 16:20:31 -04001219 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001220 if (wret)
1221 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001222
1223 /* then fixup the leaf pointer in the path */
1224 if (path->slots[0] < push_items) {
1225 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001226 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001227 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001228 path->slots[1] -= 1;
1229 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001230 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001231 path->slots[0] -= push_items;
1232 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001233 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001234 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001235}
1236
Chris Mason74123bd2007-02-02 11:05:29 -05001237/*
1238 * split the path's leaf in two, making sure there is at least data_size
1239 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001240 *
1241 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001242 */
Chris Masone089f052007-03-16 16:20:31 -04001243static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001244 *root, struct btrfs_key *ins_key,
1245 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001246{
Chris Masone20d96d2007-03-22 12:13:20 -04001247 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001248 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001249 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001250 int mid;
1251 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001252 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001253 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001254 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001255 int data_copy_size;
1256 int rt_data_off;
1257 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001258 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001259 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001260 int double_split = 0;
1261 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001262
Chris Mason40689472007-03-17 14:29:23 -04001263 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001264 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001265 if (wret < 0)
1266 return wret;
1267 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001268 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001269 if (wret < 0)
1270 return wret;
1271 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001272 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001273 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001274
1275 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001276 if (btrfs_leaf_free_space(root, l) >=
1277 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001278 return 0;
1279
Chris Mason5c680ed2007-02-22 11:39:13 -05001280 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001281 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001282 if (ret)
1283 return ret;
1284 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001285 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001286 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001287 mid = (nritems + 1)/ 2;
Chris Mason31f3c992007-04-30 15:25:45 -04001288 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr);
Chris Masoneb60cea2007-02-02 09:18:22 -05001289 BUG_ON(!right_buffer);
Chris Masone20d96d2007-03-22 12:13:20 -04001290 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001291 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001292 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001293 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001294 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Mason7518a232007-03-12 12:01:18 -04001295 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001296 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1297 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001298 if (mid <= slot) {
1299 if (nritems == 1 ||
1300 leaf_space_used(l, mid, nritems - mid) + space_needed >
1301 BTRFS_LEAF_DATA_SIZE(root)) {
1302 if (slot >= nritems) {
1303 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1304 btrfs_set_header_nritems(&right->header, 0);
1305 wret = insert_ptr(trans, root, path,
1306 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001307 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001308 path->slots[1] + 1, 1);
1309 if (wret)
1310 ret = wret;
1311 btrfs_block_release(root, path->nodes[0]);
1312 path->nodes[0] = right_buffer;
1313 path->slots[0] = 0;
1314 path->slots[1] += 1;
1315 return ret;
1316 }
1317 mid = slot;
1318 double_split = 1;
1319 }
1320 } else {
1321 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1322 BTRFS_LEAF_DATA_SIZE(root)) {
1323 if (slot == 0) {
1324 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1325 btrfs_set_header_nritems(&right->header, 0);
1326 wret = insert_ptr(trans, root, path,
1327 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001328 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001329 path->slots[1] - 1, 1);
1330 if (wret)
1331 ret = wret;
1332 btrfs_block_release(root, path->nodes[0]);
1333 path->nodes[0] = right_buffer;
1334 path->slots[0] = 0;
1335 path->slots[1] -= 1;
Chris Masona429e512007-04-18 16:15:28 -04001336 if (path->slots[1] == 0) {
1337 wret = fixup_low_keys(trans, root,
1338 path, &disk_key, 1);
1339 if (wret)
1340 ret = wret;
1341 }
Chris Masond4dbff92007-04-04 14:08:15 -04001342 return ret;
1343 }
1344 mid = slot;
1345 double_split = 1;
1346 }
1347 }
1348 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001349 data_copy_size = btrfs_item_end(l->items + mid) -
1350 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001351 btrfs_memcpy(root, right, right->items, l->items + mid,
1352 (nritems - mid) * sizeof(struct btrfs_item));
1353 btrfs_memcpy(root, right,
1354 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1355 data_copy_size, btrfs_leaf_data(l) +
1356 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001357 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1358 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001359
Chris Mason0783fcf2007-03-12 20:12:07 -04001360 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001361 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001362 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1363 }
Chris Mason74123bd2007-02-02 11:05:29 -05001364
Chris Mason7518a232007-03-12 12:01:18 -04001365 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001366 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001367 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001368 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001369 if (wret)
1370 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001371 btrfs_mark_buffer_dirty(right_buffer);
1372 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001373 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001374 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001375 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001376 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001377 path->slots[0] -= mid;
1378 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001379 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001380 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001381 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001382
1383 if (!double_split)
1384 return ret;
Chris Mason31f3c992007-04-30 15:25:45 -04001385 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr);
Chris Masond4dbff92007-04-04 14:08:15 -04001386 BUG_ON(!right_buffer);
1387 right = btrfs_buffer_leaf(right_buffer);
1388 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001389 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Masond4dbff92007-04-04 14:08:15 -04001390 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001391 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Masond4dbff92007-04-04 14:08:15 -04001392 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001393 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1394 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001395 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1396 btrfs_set_header_nritems(&right->header, 0);
1397 wret = insert_ptr(trans, root, path,
1398 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001399 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001400 path->slots[1], 1);
1401 if (wret)
1402 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001403 if (path->slots[1] == 0) {
1404 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1405 if (wret)
1406 ret = wret;
1407 }
Chris Masond4dbff92007-04-04 14:08:15 -04001408 btrfs_block_release(root, path->nodes[0]);
1409 path->nodes[0] = right_buffer;
1410 path->slots[0] = 0;
1411 check_node(root, path, 1);
1412 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001413 return ret;
1414}
1415
Chris Masonb18c6682007-04-17 13:26:50 -04001416int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1417 struct btrfs_root *root,
1418 struct btrfs_path *path,
1419 u32 new_size)
1420{
1421 int ret = 0;
1422 int slot;
1423 int slot_orig;
1424 struct btrfs_leaf *leaf;
1425 struct buffer_head *leaf_buf;
1426 u32 nritems;
1427 unsigned int data_end;
1428 unsigned int old_data_start;
1429 unsigned int old_size;
1430 unsigned int size_diff;
1431 int i;
1432
1433 slot_orig = path->slots[0];
1434 leaf_buf = path->nodes[0];
1435 leaf = btrfs_buffer_leaf(leaf_buf);
1436
1437 nritems = btrfs_header_nritems(&leaf->header);
1438 data_end = leaf_data_end(root, leaf);
1439
1440 slot = path->slots[0];
1441 old_data_start = btrfs_item_offset(leaf->items + slot);
1442 old_size = btrfs_item_size(leaf->items + slot);
1443 BUG_ON(old_size <= new_size);
1444 size_diff = old_size - new_size;
1445
1446 BUG_ON(slot < 0);
1447 BUG_ON(slot >= nritems);
1448
1449 /*
1450 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1451 */
1452 /* first correct the data pointers */
1453 for (i = slot; i < nritems; i++) {
1454 u32 ioff = btrfs_item_offset(leaf->items + i);
1455 btrfs_set_item_offset(leaf->items + i,
1456 ioff + size_diff);
1457 }
1458 /* shift the data */
Chris Masonb18c6682007-04-17 13:26:50 -04001459 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1460 data_end + size_diff, btrfs_leaf_data(leaf) +
1461 data_end, old_data_start + new_size - data_end);
1462 btrfs_set_item_size(leaf->items + slot, new_size);
1463 btrfs_mark_buffer_dirty(leaf_buf);
1464
1465 ret = 0;
1466 if (btrfs_leaf_free_space(root, leaf) < 0)
1467 BUG();
1468 check_leaf(root, path, 0);
1469 return ret;
1470}
1471
Chris Mason6567e832007-04-16 09:22:45 -04001472int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1473 *root, struct btrfs_path *path, u32 data_size)
1474{
1475 int ret = 0;
1476 int slot;
1477 int slot_orig;
1478 struct btrfs_leaf *leaf;
1479 struct buffer_head *leaf_buf;
1480 u32 nritems;
1481 unsigned int data_end;
1482 unsigned int old_data;
1483 unsigned int old_size;
1484 int i;
1485
1486 slot_orig = path->slots[0];
1487 leaf_buf = path->nodes[0];
1488 leaf = btrfs_buffer_leaf(leaf_buf);
1489
1490 nritems = btrfs_header_nritems(&leaf->header);
1491 data_end = leaf_data_end(root, leaf);
1492
1493 if (btrfs_leaf_free_space(root, leaf) < data_size)
1494 BUG();
1495 slot = path->slots[0];
1496 old_data = btrfs_item_end(leaf->items + slot);
1497
1498 BUG_ON(slot < 0);
1499 BUG_ON(slot >= nritems);
1500
1501 /*
1502 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1503 */
1504 /* first correct the data pointers */
1505 for (i = slot; i < nritems; i++) {
1506 u32 ioff = btrfs_item_offset(leaf->items + i);
1507 btrfs_set_item_offset(leaf->items + i,
1508 ioff - data_size);
1509 }
1510 /* shift the data */
1511 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1512 data_end - data_size, btrfs_leaf_data(leaf) +
1513 data_end, old_data - data_end);
1514 data_end = old_data;
1515 old_size = btrfs_item_size(leaf->items + slot);
1516 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1517 btrfs_mark_buffer_dirty(leaf_buf);
1518
1519 ret = 0;
1520 if (btrfs_leaf_free_space(root, leaf) < 0)
1521 BUG();
1522 check_leaf(root, path, 0);
1523 return ret;
1524}
1525
Chris Mason74123bd2007-02-02 11:05:29 -05001526/*
1527 * Given a key and some data, insert an item into the tree.
1528 * This does all the path init required, making room in the tree if needed.
1529 */
Chris Masone089f052007-03-16 16:20:31 -04001530int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1531 *root, struct btrfs_path *path, struct btrfs_key
1532 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001533{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001534 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001535 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001536 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001537 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001538 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001539 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001540 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001541 struct btrfs_disk_key disk_key;
1542
1543 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001544
Chris Mason74123bd2007-02-02 11:05:29 -05001545 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001546 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001547 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001548 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001549 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001550 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001551 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001552 if (ret < 0)
1553 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001554
Chris Mason62e27492007-03-15 12:56:47 -04001555 slot_orig = path->slots[0];
1556 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001557 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001558
Chris Mason7518a232007-03-12 12:01:18 -04001559 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001560 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001561
Chris Mason123abc82007-03-14 14:14:43 -04001562 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001563 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001564 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001565 }
Chris Mason62e27492007-03-15 12:56:47 -04001566 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001567 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001568 if (slot != nritems) {
1569 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001570 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001571
1572 /*
1573 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1574 */
1575 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001576 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001577 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001578 btrfs_set_item_offset(leaf->items + i,
1579 ioff - data_size);
1580 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001581
1582 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001583 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1584 leaf->items + slot,
1585 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001586
1587 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001588 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1589 data_end - data_size, btrfs_leaf_data(leaf) +
1590 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001591 data_end = old_data;
1592 }
Chris Mason62e27492007-03-15 12:56:47 -04001593 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001594 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1595 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001596 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1597 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001598 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001599 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001600
1601 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001602 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001603 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001604
Chris Mason123abc82007-03-14 14:14:43 -04001605 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001606 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001607 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001608out:
Chris Mason62e27492007-03-15 12:56:47 -04001609 return ret;
1610}
1611
1612/*
1613 * Given a key and some data, insert an item into the tree.
1614 * This does all the path init required, making room in the tree if needed.
1615 */
Chris Masone089f052007-03-16 16:20:31 -04001616int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1617 *root, struct btrfs_key *cpu_key, void *data, u32
1618 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001619{
1620 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001621 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001622 u8 *ptr;
1623
Chris Mason2c90e5d2007-04-02 10:50:19 -04001624 path = btrfs_alloc_path();
1625 BUG_ON(!path);
1626 btrfs_init_path(path);
1627 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001628 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001629 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1630 path->slots[0], u8);
1631 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001632 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001633 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001634 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001635 btrfs_release_path(root, path);
1636 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001637 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001638}
1639
Chris Mason74123bd2007-02-02 11:05:29 -05001640/*
Chris Mason5de08d72007-02-24 06:24:44 -05001641 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001642 *
1643 * If the delete empties a node, the node is removed from the tree,
1644 * continuing all the way the root if required. The root is converted into
1645 * a leaf if all the nodes are emptied.
1646 */
Chris Masone089f052007-03-16 16:20:31 -04001647static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1648 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001649{
Chris Mason234b63a2007-03-13 10:46:10 -04001650 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001651 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001652 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001653 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001654 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001655
Chris Masone20d96d2007-03-22 12:13:20 -04001656 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001657 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001658 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001659 btrfs_memmove(root, node, node->ptrs + slot,
1660 node->ptrs + slot + 1,
1661 sizeof(struct btrfs_key_ptr) *
1662 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001663 }
Chris Mason7518a232007-03-12 12:01:18 -04001664 nritems--;
1665 btrfs_set_header_nritems(&node->header, nritems);
1666 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001667 struct btrfs_header *header = btrfs_buffer_header(root->node);
1668 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001669 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001670 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001671 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001672 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001673 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001674 if (wret)
1675 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001676 }
Chris Masond6025572007-03-30 14:27:56 -04001677 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001678 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001679}
1680
Chris Mason74123bd2007-02-02 11:05:29 -05001681/*
1682 * delete the item at the leaf level in path. If that empties
1683 * the leaf, remove it from the tree
1684 */
Chris Masone089f052007-03-16 16:20:31 -04001685int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1686 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001687{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001688 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001689 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001690 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001691 int doff;
1692 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001693 int ret = 0;
1694 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001695 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001696
Chris Masoneb60cea2007-02-02 09:18:22 -05001697 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001698 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001699 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001700 doff = btrfs_item_offset(leaf->items + slot);
1701 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001702 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001703
Chris Mason7518a232007-03-12 12:01:18 -04001704 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001705 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001706 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04001707 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1708 data_end + dsize,
1709 btrfs_leaf_data(leaf) + data_end,
1710 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001711 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001712 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001713 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1714 }
Chris Masond6025572007-03-30 14:27:56 -04001715 btrfs_memmove(root, leaf, leaf->items + slot,
1716 leaf->items + slot + 1,
1717 sizeof(struct btrfs_item) *
1718 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001719 }
Chris Mason7518a232007-03-12 12:01:18 -04001720 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1721 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001722 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001723 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001724 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001725 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05001726 } else {
Chris Masone089f052007-03-16 16:20:31 -04001727 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001728 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001729 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001730 if (wret)
1731 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001732 wret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001733 bh_blocknr(leaf_buf), 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001734 if (wret)
1735 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001736 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001737 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001738 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001739 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001740 wret = fixup_low_keys(trans, root, path,
1741 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001742 if (wret)
1743 ret = wret;
1744 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001745
Chris Mason74123bd2007-02-02 11:05:29 -05001746 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001747 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001748 /* push_leaf_left fixes the path.
1749 * make sure the path still points to our leaf
1750 * for possible call to del_ptr below
1751 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001752 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001753 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001754 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001755 if (wret < 0)
1756 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001757 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001758 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001759 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001760 if (wret < 0)
1761 ret = wret;
1762 }
Chris Mason7518a232007-03-12 12:01:18 -04001763 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -04001764 u64 blocknr = bh_blocknr(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001765 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04001766 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001767 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001768 if (wret)
1769 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001770 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001771 wret = btrfs_free_extent(trans, root, blocknr,
1772 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001773 if (wret)
1774 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001775 } else {
Chris Masond6025572007-03-30 14:27:56 -04001776 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001777 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001778 }
Chris Masond5719762007-03-23 10:01:08 -04001779 } else {
Chris Masond6025572007-03-30 14:27:56 -04001780 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001781 }
1782 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001783 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001784}
1785
Chris Mason97571fd2007-02-24 13:39:08 -05001786/*
1787 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001788 * returns 0 if it found something or 1 if there are no greater leaves.
1789 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001790 */
Chris Mason234b63a2007-03-13 10:46:10 -04001791int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001792{
1793 int slot;
1794 int level = 1;
1795 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04001796 struct buffer_head *c;
1797 struct btrfs_node *c_node;
1798 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001799
Chris Mason234b63a2007-03-13 10:46:10 -04001800 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001801 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001802 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001803 slot = path->slots[level] + 1;
1804 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001805 c_node = btrfs_buffer_node(c);
1806 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001807 level++;
1808 continue;
1809 }
Chris Masone20d96d2007-03-22 12:13:20 -04001810 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001811 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001812 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001813 next = read_tree_block(root, blocknr);
1814 break;
1815 }
1816 path->slots[level] = slot;
1817 while(1) {
1818 level--;
1819 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001820 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001821 path->nodes[level] = next;
1822 path->slots[level] = 0;
1823 if (!level)
1824 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001825 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04001826 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001827 }
1828 return 0;
1829}