blob: 32922643b5bc9ec1bca0e9e96bf60f367a359f93 [file] [log] [blame]
Chris Masonbe0e5c02007-01-26 15:51:26 -05001#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05004#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
Chris Mason5de08d72007-02-24 06:24:44 -05007#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05008
Chris Masone089f052007-03-16 16:20:31 -04009static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
10 *root, struct btrfs_path *path, int level);
11static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
12 *root, struct btrfs_path *path, int data_size);
13static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
14 *root, struct btrfs_buffer *dst, struct btrfs_buffer
15 *src);
16static int balance_node_right(struct btrfs_trans_handle *trans, struct
17 btrfs_root *root, struct btrfs_buffer *dst_buf,
Chris Mason234b63a2007-03-13 10:46:10 -040018 struct btrfs_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040019static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
20 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050021
Chris Mason234b63a2007-03-13 10:46:10 -040022inline void btrfs_init_path(struct btrfs_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050023{
24 memset(p, 0, sizeof(*p));
25}
26
Chris Mason234b63a2007-03-13 10:46:10 -040027void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050028{
29 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040030 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050031 if (!p->nodes[i])
32 break;
Chris Mason234b63a2007-03-13 10:46:10 -040033 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050034 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050035 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050036}
37
Chris Masone089f052007-03-16 16:20:31 -040038static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
39 *root, struct btrfs_buffer *buf, struct btrfs_buffer
40 *parent, int parent_slot, struct btrfs_buffer
41 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -050042{
Chris Mason234b63a2007-03-13 10:46:10 -040043 struct btrfs_buffer *cow;
Chris Mason02217ed2007-03-02 16:08:05 -050044
45 if (!list_empty(&buf->dirty)) {
46 *cow_ret = buf;
47 return 0;
48 }
Chris Masone089f052007-03-16 16:20:31 -040049 cow = btrfs_alloc_free_block(trans, root);
Chris Mason123abc82007-03-14 14:14:43 -040050 memcpy(&cow->node, &buf->node, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -040051 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050052 *cow_ret = cow;
Chris Masone089f052007-03-16 16:20:31 -040053 btrfs_inc_ref(trans, root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050054 if (buf == root->node) {
55 root->node = cow;
56 cow->count++;
Chris Masona28ec192007-03-06 20:08:01 -050057 if (buf != root->commit_root)
Chris Masone089f052007-03-16 16:20:31 -040058 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -040059 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050060 } else {
Chris Mason1d4f8a02007-03-13 09:28:32 -040061 btrfs_set_node_blockptr(&parent->node, parent_slot,
62 cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050063 BUG_ON(list_empty(&parent->dirty));
Chris Masone089f052007-03-16 16:20:31 -040064 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050065 }
Chris Mason234b63a2007-03-13 10:46:10 -040066 btrfs_block_release(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050067 return 0;
68}
69
Chris Mason74123bd2007-02-02 11:05:29 -050070/*
71 * The leaf data grows from end-to-front in the node.
72 * this returns the address of the start of the last item,
73 * which is the stop of the leaf data stack
74 */
Chris Mason123abc82007-03-14 14:14:43 -040075static inline unsigned int leaf_data_end(struct btrfs_root *root,
76 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050077{
Chris Mason7518a232007-03-12 12:01:18 -040078 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050079 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -040080 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -040081 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050082}
83
Chris Mason74123bd2007-02-02 11:05:29 -050084/*
85 * The space between the end of the leaf items and
86 * the start of the leaf data. IOW, how much room
87 * the leaf has left for both items and data
88 */
Chris Mason123abc82007-03-14 14:14:43 -040089int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050090{
Chris Mason123abc82007-03-14 14:14:43 -040091 int data_end = leaf_data_end(root, leaf);
Chris Mason7518a232007-03-12 12:01:18 -040092 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050093 char *items_end = (char *)(leaf->items + nritems + 1);
Chris Mason123abc82007-03-14 14:14:43 -040094 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
Chris Masonbe0e5c02007-01-26 15:51:26 -050095}
96
Chris Mason74123bd2007-02-02 11:05:29 -050097/*
98 * compare two keys in a memcmp fashion
99 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400100static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500101{
Chris Masone2fa7222007-03-12 16:22:34 -0400102 struct btrfs_key k1;
103
104 btrfs_disk_key_to_cpu(&k1, disk);
105
106 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500107 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400108 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500109 return -1;
Chris Mason62e27492007-03-15 12:56:47 -0400110 if (k1.flags > k2->flags)
111 return 1;
112 if (k1.flags < k2->flags)
113 return -1;
Chris Masona8a2ee02007-03-16 08:46:49 -0400114 if (k1.offset > k2->offset)
115 return 1;
116 if (k1.offset < k2->offset)
117 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500118 return 0;
119}
Chris Mason74123bd2007-02-02 11:05:29 -0500120
Chris Mason123abc82007-03-14 14:14:43 -0400121static int check_node(struct btrfs_root *root, struct btrfs_path *path,
122 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500123{
124 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400125 struct btrfs_node *parent = NULL;
126 struct btrfs_node *node = &path->nodes[level]->node;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500127 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400128 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500129
130 if (path->nodes[level + 1])
131 parent = &path->nodes[level + 1]->node;
132 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400133 BUG_ON(nritems == 0);
134 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400135 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400136 parent_key = &parent->ptrs[parent_slot].key;
137 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400138 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400139 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400140 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500141 }
Chris Mason123abc82007-03-14 14:14:43 -0400142 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason7518a232007-03-12 12:01:18 -0400143 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400144 struct btrfs_key cpukey;
Chris Mason123abc82007-03-14 14:14:43 -0400145 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
146 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500147 }
148 return 0;
149}
150
Chris Mason123abc82007-03-14 14:14:43 -0400151static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
152 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500153{
154 int i;
Chris Mason234b63a2007-03-13 10:46:10 -0400155 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
156 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500157 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400158 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500159
160 if (path->nodes[level + 1])
161 parent = &path->nodes[level + 1]->node;
162 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400163 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400164
165 if (nritems == 0)
166 return 0;
167
168 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400169 struct btrfs_disk_key *parent_key;
Chris Mason123abc82007-03-14 14:14:43 -0400170 parent_key = &parent->ptrs[parent_slot].key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500171 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400172 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400173 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400174 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500175 }
Chris Mason7518a232007-03-12 12:01:18 -0400176 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400177 struct btrfs_key cpukey;
178 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500179 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400180 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400181 BUG_ON(btrfs_item_offset(leaf->items + i) !=
182 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500183 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400184 BUG_ON(btrfs_item_offset(leaf->items + i) +
185 btrfs_item_size(leaf->items + i) !=
Chris Mason123abc82007-03-14 14:14:43 -0400186 BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500187 }
188 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500189 return 0;
190}
191
Chris Mason123abc82007-03-14 14:14:43 -0400192static int check_block(struct btrfs_root *root, struct btrfs_path *path,
193 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500194{
195 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400196 return check_leaf(root, path, level);
197 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500198}
199
Chris Mason74123bd2007-02-02 11:05:29 -0500200/*
201 * search for key in the array p. items p are item_size apart
202 * and there are 'max' items in p
203 * the slot in the array is returned via slot, and it points to
204 * the place where you would insert key if it is not found in
205 * the array.
206 *
207 * slot may point to max if the key is bigger than all of the keys
208 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400209static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500210 int max, int *slot)
211{
212 int low = 0;
213 int high = max;
214 int mid;
215 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400216 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500217
218 while(low < high) {
219 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400220 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500221 ret = comp_keys(tmp, key);
222
223 if (ret < 0)
224 low = mid + 1;
225 else if (ret > 0)
226 high = mid;
227 else {
228 *slot = mid;
229 return 0;
230 }
231 }
232 *slot = low;
233 return 1;
234}
235
Chris Mason97571fd2007-02-24 13:39:08 -0500236/*
237 * simple bin_search frontend that does the right thing for
238 * leaves vs nodes
239 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400240static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500241{
Chris Mason7518a232007-03-12 12:01:18 -0400242 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400243 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400244 return generic_bin_search((void *)l->items,
245 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400246 key, btrfs_header_nritems(&c->header),
247 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500248 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400249 return generic_bin_search((void *)c->ptrs,
250 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400251 key, btrfs_header_nritems(&c->header),
252 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500253 }
254 return -1;
255}
256
Chris Mason9aca1d52007-03-13 11:09:37 -0400257static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason234b63a2007-03-13 10:46:10 -0400258 struct btrfs_buffer *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500259 int slot)
260{
Chris Mason234b63a2007-03-13 10:46:10 -0400261 struct btrfs_node *node = &parent_buf->node;
Chris Masonbb803952007-03-01 12:04:21 -0500262 if (slot < 0)
263 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400264 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500265 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400266 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500267}
268
Chris Masone089f052007-03-16 16:20:31 -0400269static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
270 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500271{
Chris Mason234b63a2007-03-13 10:46:10 -0400272 struct btrfs_buffer *right_buf;
273 struct btrfs_buffer *mid_buf;
274 struct btrfs_buffer *left_buf;
275 struct btrfs_buffer *parent_buf = NULL;
276 struct btrfs_node *right = NULL;
277 struct btrfs_node *mid;
278 struct btrfs_node *left = NULL;
279 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500280 int ret = 0;
281 int wret;
282 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500283 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500284 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500285
286 if (level == 0)
287 return 0;
288
289 mid_buf = path->nodes[level];
290 mid = &mid_buf->node;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400291 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500292
Chris Mason234b63a2007-03-13 10:46:10 -0400293 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500294 parent_buf = path->nodes[level + 1];
295 pslot = path->slots[level + 1];
296
Chris Mason40689472007-03-17 14:29:23 -0400297 /*
298 * deal with the case where there is only one pointer in the root
299 * by promoting the node below to a root
300 */
Chris Masonbb803952007-03-01 12:04:21 -0500301 if (!parent_buf) {
Chris Mason234b63a2007-03-13 10:46:10 -0400302 struct btrfs_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500303 u64 blocknr = mid_buf->blocknr;
304
Chris Mason7518a232007-03-12 12:01:18 -0400305 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500306 return 0;
307
308 /* promote the child to a root */
309 child = read_node_slot(root, mid_buf, 0);
310 BUG_ON(!child);
311 root->node = child;
312 path->nodes[level] = NULL;
313 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400314 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500315 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400316 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400317 clean_tree_block(trans, root, mid_buf);
318 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500319 }
320 parent = &parent_buf->node;
321
Chris Mason123abc82007-03-14 14:14:43 -0400322 if (btrfs_header_nritems(&mid->header) >
323 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500324 return 0;
325
Chris Masonbb803952007-03-01 12:04:21 -0500326 left_buf = read_node_slot(root, parent_buf, pslot - 1);
327 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500328
329 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500330 if (left_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400331 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
332 &left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500333 left = &left_buf->node;
Chris Mason7518a232007-03-12 12:01:18 -0400334 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400335 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500336 if (wret < 0)
337 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500338 }
Chris Mason79f95c82007-03-01 15:16:26 -0500339
340 /*
341 * then try to empty the right most buffer into the middle
342 */
Chris Masonbb803952007-03-01 12:04:21 -0500343 if (right_buf) {
Chris Masone089f052007-03-16 16:20:31 -0400344 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
345 &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500346 right = &right_buf->node;
Chris Masone089f052007-03-16 16:20:31 -0400347 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500348 if (wret < 0)
349 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400350 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masonbb803952007-03-01 12:04:21 -0500351 u64 blocknr = right_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400352 btrfs_block_release(root, right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400353 clean_tree_block(trans, root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500354 right_buf = NULL;
355 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400356 wret = del_ptr(trans, root, path, level + 1, pslot +
357 1);
Chris Masonbb803952007-03-01 12:04:21 -0500358 if (wret)
359 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400360 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500361 if (wret)
362 ret = wret;
363 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400364 memcpy(&parent->ptrs[pslot + 1].key,
365 &right->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400366 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500367 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500368 }
369 }
Chris Mason7518a232007-03-12 12:01:18 -0400370 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500371 /*
372 * we're not allowed to leave a node with one item in the
373 * tree during a delete. A deletion from lower in the tree
374 * could try to delete the only pointer in this node.
375 * So, pull some keys from the left.
376 * There has to be a left pointer at this point because
377 * otherwise we would have pulled some pointers from the
378 * right
379 */
380 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400381 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500382 if (wret < 0)
383 ret = wret;
384 BUG_ON(wret == 1);
385 }
Chris Mason7518a232007-03-12 12:01:18 -0400386 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500387 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500388 u64 blocknr = mid_buf->blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -0400389 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400390 clean_tree_block(trans, root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500391 mid_buf = NULL;
392 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400393 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500394 if (wret)
395 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400396 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500397 if (wret)
398 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500399 } else {
400 /* update the parent key to reflect our changes */
Chris Mason123abc82007-03-14 14:14:43 -0400401 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400402 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500403 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500404 }
Chris Masonbb803952007-03-01 12:04:21 -0500405
Chris Mason79f95c82007-03-01 15:16:26 -0500406 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500407 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400408 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500409 left_buf->count++; // released below
410 path->nodes[level] = left_buf;
411 path->slots[level + 1] -= 1;
412 path->slots[level] = orig_slot;
413 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400414 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500415 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400416 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500417 path->slots[level] = orig_slot;
418 }
419 }
Chris Mason79f95c82007-03-01 15:16:26 -0500420 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400421 check_block(root, path, level);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400422 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
423 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500424 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500425
426 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400427 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500428 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400429 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500430 return ret;
431}
432
Chris Mason74123bd2007-02-02 11:05:29 -0500433/*
434 * look for key in the tree. path is filled in with nodes along the way
435 * if key is found, we return zero and you can find the item in the leaf
436 * level of the path (level 0)
437 *
438 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500439 * be inserted, and 1 is returned. If there are other errors during the
440 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500441 *
442 * if ins_len > 0, nodes and leaves will be split as we walk down the
443 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
444 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500445 */
Chris Masone089f052007-03-16 16:20:31 -0400446int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
447 *root, struct btrfs_key *key, struct btrfs_path *p, int
448 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500449{
Chris Mason234b63a2007-03-13 10:46:10 -0400450 struct btrfs_buffer *b;
451 struct btrfs_buffer *cow_buf;
452 struct btrfs_node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500453 int slot;
454 int ret;
455 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500456
Chris Masonbb803952007-03-01 12:04:21 -0500457again:
458 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500459 b->count++;
460 while (b) {
Chris Mason7518a232007-03-12 12:01:18 -0400461 level = btrfs_header_level(&b->node.header);
Chris Mason02217ed2007-03-02 16:08:05 -0500462 if (cow) {
463 int wret;
Chris Masone089f052007-03-16 16:20:31 -0400464 wret = btrfs_cow_block(trans, root, b, p->nodes[level +
465 1], p->slots[level + 1],
466 &cow_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500467 b = cow_buf;
468 }
469 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500470 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500471 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400472 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500473 if (ret)
474 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500475 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400476 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500477 if (ret && slot > 0)
478 slot -= 1;
479 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400480 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
Chris Mason123abc82007-03-14 14:14:43 -0400481 BTRFS_NODEPTRS_PER_BLOCK(root)) {
Chris Masone089f052007-03-16 16:20:31 -0400482 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500483 BUG_ON(sret > 0);
484 if (sret)
485 return sret;
486 b = p->nodes[level];
487 c = &b->node;
488 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500489 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400490 int sret = balance_level(trans, root, p,
491 level);
Chris Masonbb803952007-03-01 12:04:21 -0500492 if (sret)
493 return sret;
494 b = p->nodes[level];
495 if (!b)
496 goto again;
497 c = &b->node;
498 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400499 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500500 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400501 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500502 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400503 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500504 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400505 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400506 sizeof(struct btrfs_item) + ins_len) {
Chris Masone089f052007-03-16 16:20:31 -0400507 int sret = split_leaf(trans, root, p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500508 BUG_ON(sret > 0);
509 if (sret)
510 return sret;
511 }
Chris Masonbb803952007-03-01 12:04:21 -0500512 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500513 return ret;
514 }
515 }
Chris Masonbb803952007-03-01 12:04:21 -0500516 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500517 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500518}
519
Chris Mason74123bd2007-02-02 11:05:29 -0500520/*
521 * adjust the pointers going up the tree, starting at level
522 * making sure the right key of each node is points to 'key'.
523 * This is used after shifting pointers to the left, so it stops
524 * fixing up pointers when a given leaf/node is not in slot 0 of the
525 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500526 *
527 * If this fails to write a tree block, it returns -1, but continues
528 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500529 */
Chris Masone089f052007-03-16 16:20:31 -0400530static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
531 *root, struct btrfs_path *path, struct btrfs_disk_key
532 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500533{
534 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500535 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400536 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
537 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500538 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500539 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500540 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500541 t = &path->nodes[i]->node;
Chris Mason123abc82007-03-14 14:14:43 -0400542 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500543 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500544 if (tslot != 0)
545 break;
546 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500547 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500548}
549
Chris Mason74123bd2007-02-02 11:05:29 -0500550/*
551 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500552 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500553 *
554 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
555 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500556 */
Chris Masone089f052007-03-16 16:20:31 -0400557static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
558 *root, struct btrfs_buffer *dst_buf, struct
559 btrfs_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500560{
Chris Mason234b63a2007-03-13 10:46:10 -0400561 struct btrfs_node *src = &src_buf->node;
562 struct btrfs_node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500564 int src_nritems;
565 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500566 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500567
Chris Mason7518a232007-03-12 12:01:18 -0400568 src_nritems = btrfs_header_nritems(&src->header);
569 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400570 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500571 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500572 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500573 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500574
575 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500576 push_items = src_nritems;
577
Chris Mason123abc82007-03-14 14:14:43 -0400578 memcpy(dst->ptrs + dst_nritems, src->ptrs,
579 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500580 if (push_items < src_nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400581 memmove(src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400582 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400583 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500584 }
Chris Mason7518a232007-03-12 12:01:18 -0400585 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
586 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason02217ed2007-03-02 16:08:05 -0500587 BUG_ON(list_empty(&src_buf->dirty));
588 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500589 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500590}
591
Chris Mason97571fd2007-02-24 13:39:08 -0500592/*
Chris Mason79f95c82007-03-01 15:16:26 -0500593 * try to push data from one node into the next node right in the
594 * tree.
595 *
596 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
597 * error, and > 0 if there was no room in the right hand block.
598 *
599 * this will only push up to 1/2 the contents of the left node over
600 */
Chris Masone089f052007-03-16 16:20:31 -0400601static int balance_node_right(struct btrfs_trans_handle *trans, struct
602 btrfs_root *root, struct btrfs_buffer *dst_buf,
Chris Mason234b63a2007-03-13 10:46:10 -0400603 struct btrfs_buffer *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -0500604{
Chris Mason234b63a2007-03-13 10:46:10 -0400605 struct btrfs_node *src = &src_buf->node;
606 struct btrfs_node *dst = &dst_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500607 int push_items = 0;
608 int max_push;
609 int src_nritems;
610 int dst_nritems;
611 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500612
Chris Mason7518a232007-03-12 12:01:18 -0400613 src_nritems = btrfs_header_nritems(&src->header);
614 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400615 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500616 if (push_items <= 0) {
617 return 1;
618 }
619
620 max_push = src_nritems / 2 + 1;
621 /* don't try to empty the node */
622 if (max_push > src_nritems)
623 return 1;
624 if (max_push < push_items)
625 push_items = max_push;
626
Chris Mason123abc82007-03-14 14:14:43 -0400627 memmove(dst->ptrs + push_items, dst->ptrs,
628 dst_nritems * sizeof(struct btrfs_key_ptr));
629 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
630 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -0500631
Chris Mason7518a232007-03-12 12:01:18 -0400632 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
633 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500634
Chris Mason02217ed2007-03-02 16:08:05 -0500635 BUG_ON(list_empty(&src_buf->dirty));
636 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500637 return ret;
638}
639
640/*
Chris Mason97571fd2007-02-24 13:39:08 -0500641 * helper function to insert a new root level in the tree.
642 * A new node is allocated, and a single item is inserted to
643 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500644 *
645 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500646 */
Chris Masone089f052007-03-16 16:20:31 -0400647static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
648 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500649{
Chris Mason234b63a2007-03-13 10:46:10 -0400650 struct btrfs_buffer *t;
651 struct btrfs_node *lower;
652 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400653 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500654
655 BUG_ON(path->nodes[level]);
656 BUG_ON(path->nodes[level-1] != root->node);
657
Chris Masone089f052007-03-16 16:20:31 -0400658 t = btrfs_alloc_free_block(trans, root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500659 c = &t->node;
Chris Mason123abc82007-03-14 14:14:43 -0400660 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -0400661 btrfs_set_header_nritems(&c->header, 1);
662 btrfs_set_header_level(&c->header, level);
663 btrfs_set_header_blocknr(&c->header, t->blocknr);
664 btrfs_set_header_parentid(&c->header,
665 btrfs_header_parentid(&root->node->node.header));
Chris Mason5c680ed2007-02-22 11:39:13 -0500666 lower = &path->nodes[level-1]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400667 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -0400668 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500669 else
Chris Mason123abc82007-03-14 14:14:43 -0400670 lower_key = &lower->ptrs[0].key;
671 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400672 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
Chris Mason5c680ed2007-02-22 11:39:13 -0500673 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -0400674 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -0500675 root->node = t;
676 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500677 path->nodes[level] = t;
678 path->slots[level] = 0;
679 return 0;
680}
681
Chris Mason74123bd2007-02-02 11:05:29 -0500682/*
683 * worker function to insert a single pointer in a node.
684 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500685 *
Chris Mason74123bd2007-02-02 11:05:29 -0500686 * slot and level indicate where you want the key to go, and
687 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500688 *
689 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500690 */
Chris Masone089f052007-03-16 16:20:31 -0400691static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
692 *root, struct btrfs_path *path, struct btrfs_disk_key
693 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -0500694{
Chris Mason234b63a2007-03-13 10:46:10 -0400695 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500696 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500697
698 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500699 lower = &path->nodes[level]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400700 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500701 if (slot > nritems)
702 BUG();
Chris Mason123abc82007-03-14 14:14:43 -0400703 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -0500704 BUG();
705 if (slot != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -0400706 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
707 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -0500708 }
Chris Mason123abc82007-03-14 14:14:43 -0400709 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400710 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -0400711 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Mason02217ed2007-03-02 16:08:05 -0500712 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500713 return 0;
714}
715
Chris Mason97571fd2007-02-24 13:39:08 -0500716/*
717 * split the node at the specified level in path in two.
718 * The path is corrected to point to the appropriate node after the split
719 *
720 * Before splitting this tries to make some room in the node by pushing
721 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500722 *
723 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500724 */
Chris Masone089f052007-03-16 16:20:31 -0400725static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
726 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727{
Chris Mason234b63a2007-03-13 10:46:10 -0400728 struct btrfs_buffer *t;
729 struct btrfs_node *c;
730 struct btrfs_buffer *split_buffer;
731 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500732 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500733 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500734 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400735 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500736
Chris Mason5c680ed2007-02-22 11:39:13 -0500737 t = path->nodes[level];
738 c = &t->node;
739 if (t == root->node) {
740 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -0400741 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500742 if (ret)
743 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500744 }
Chris Mason7518a232007-03-12 12:01:18 -0400745 c_nritems = btrfs_header_nritems(&c->header);
Chris Masone089f052007-03-16 16:20:31 -0400746 split_buffer = btrfs_alloc_free_block(trans, root);
Chris Mason5c680ed2007-02-22 11:39:13 -0500747 split = &split_buffer->node;
Chris Mason7518a232007-03-12 12:01:18 -0400748 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
749 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
750 btrfs_set_header_parentid(&split->header,
751 btrfs_header_parentid(&root->node->node.header));
752 mid = (c_nritems + 1) / 2;
Chris Mason123abc82007-03-14 14:14:43 -0400753 memcpy(split->ptrs, c->ptrs + mid,
754 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -0400755 btrfs_set_header_nritems(&split->header, c_nritems - mid);
756 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500757 ret = 0;
758
Chris Mason02217ed2007-03-02 16:08:05 -0500759 BUG_ON(list_empty(&t->dirty));
Chris Masone089f052007-03-16 16:20:31 -0400760 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -0400761 split_buffer->blocknr, path->slots[level + 1] + 1,
762 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500763 if (wret)
764 ret = wret;
765
Chris Mason5de08d72007-02-24 06:24:44 -0500766 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500767 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -0400768 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500769 path->nodes[level] = split_buffer;
770 path->slots[level + 1] += 1;
771 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400772 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500773 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500774 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775}
776
Chris Mason74123bd2007-02-02 11:05:29 -0500777/*
778 * how many bytes are required to store the items in a leaf. start
779 * and nr indicate which items in the leaf to check. This totals up the
780 * space used both by the item structs and the item data
781 */
Chris Mason234b63a2007-03-13 10:46:10 -0400782static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500783{
784 int data_len;
785 int end = start + nr - 1;
786
787 if (!nr)
788 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400789 data_len = btrfs_item_end(l->items + start);
790 data_len = data_len - btrfs_item_offset(l->items + end);
791 data_len += sizeof(struct btrfs_item) * nr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500792 return data_len;
793}
794
Chris Mason74123bd2007-02-02 11:05:29 -0500795/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500796 * push some data in the path leaf to the right, trying to free up at
797 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500798 *
799 * returns 1 if the push failed because the other node didn't have enough
800 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500801 */
Chris Masone089f052007-03-16 16:20:31 -0400802static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
803 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500804{
Chris Mason234b63a2007-03-13 10:46:10 -0400805 struct btrfs_buffer *left_buf = path->nodes[0];
806 struct btrfs_leaf *left = &left_buf->leaf;
807 struct btrfs_leaf *right;
808 struct btrfs_buffer *right_buf;
809 struct btrfs_buffer *upper;
Chris Mason00ec4c52007-02-24 12:47:20 -0500810 int slot;
811 int i;
812 int free_space;
813 int push_space = 0;
814 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400815 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400816 u32 left_nritems;
817 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500818
819 slot = path->slots[1];
820 if (!path->nodes[1]) {
821 return 1;
822 }
823 upper = path->nodes[1];
Chris Mason7518a232007-03-12 12:01:18 -0400824 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500825 return 1;
826 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400827 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
828 slot + 1));
Chris Mason00ec4c52007-02-24 12:47:20 -0500829 right = &right_buf->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400830 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400831 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400832 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500833 return 1;
834 }
Chris Mason02217ed2007-03-02 16:08:05 -0500835 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400836 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500837 right = &right_buf->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400838 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400839 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400840 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -0500841 return 1;
842 }
843
Chris Mason7518a232007-03-12 12:01:18 -0400844 left_nritems = btrfs_header_nritems(&left->header);
845 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500846 item = left->items + i;
847 if (path->slots[0] == i)
848 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400849 if (btrfs_item_size(item) + sizeof(*item) + push_space >
850 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500851 break;
852 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400853 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500854 }
855 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400856 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500857 return 1;
858 }
Chris Mason7518a232007-03-12 12:01:18 -0400859 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500860 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400861 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400862 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -0500863 /* make room in the right data area */
Chris Mason123abc82007-03-14 14:14:43 -0400864 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
865 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
866 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -0500867 /* copy from the left data area */
Chris Mason123abc82007-03-14 14:14:43 -0400868 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
869 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
Chris Mason00ec4c52007-02-24 12:47:20 -0500870 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400871 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500872 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400873 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400874 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500875
876 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400877 right_nritems += push_items;
878 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -0400879 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -0400880 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400881 btrfs_set_item_offset(right->items + i, push_space -
882 btrfs_item_size(right->items + i));
883 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -0500884 }
Chris Mason7518a232007-03-12 12:01:18 -0400885 left_nritems -= push_items;
886 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500887
Chris Mason02217ed2007-03-02 16:08:05 -0500888 BUG_ON(list_empty(&left_buf->dirty));
889 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason123abc82007-03-14 14:14:43 -0400890 memcpy(&upper->node.ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400891 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500892 BUG_ON(list_empty(&upper->dirty));
893
Chris Mason00ec4c52007-02-24 12:47:20 -0500894 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400895 if (path->slots[0] >= left_nritems) {
896 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -0400897 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -0500898 path->nodes[0] = right_buf;
899 path->slots[1] += 1;
900 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400901 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500902 }
903 return 0;
904}
905/*
Chris Mason74123bd2007-02-02 11:05:29 -0500906 * push some data in the path leaf to the left, trying to free up at
907 * least data_size bytes. returns zero if the push worked, nonzero otherwise
908 */
Chris Masone089f052007-03-16 16:20:31 -0400909static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
910 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500911{
Chris Mason234b63a2007-03-13 10:46:10 -0400912 struct btrfs_buffer *right_buf = path->nodes[0];
913 struct btrfs_leaf *right = &right_buf->leaf;
914 struct btrfs_buffer *t;
915 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500916 int slot;
917 int i;
918 int free_space;
919 int push_space = 0;
920 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400921 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400922 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500923 int ret = 0;
924 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500925
926 slot = path->slots[1];
927 if (slot == 0) {
928 return 1;
929 }
930 if (!path->nodes[1]) {
931 return 1;
932 }
Chris Mason1d4f8a02007-03-13 09:28:32 -0400933 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
934 slot - 1));
Chris Masoneb60cea2007-02-02 09:18:22 -0500935 left = &t->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400936 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400937 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400938 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500939 return 1;
940 }
Chris Mason02217ed2007-03-02 16:08:05 -0500941
942 /* cow and double check */
Chris Masone089f052007-03-16 16:20:31 -0400943 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
Chris Mason02217ed2007-03-02 16:08:05 -0500944 left = &t->leaf;
Chris Mason123abc82007-03-14 14:14:43 -0400945 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400946 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400947 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -0500948 return 1;
949 }
950
Chris Mason7518a232007-03-12 12:01:18 -0400951 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500952 item = right->items + i;
953 if (path->slots[0] == i)
954 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400955 if (btrfs_item_size(item) + sizeof(*item) + push_space >
956 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500957 break;
958 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400959 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500960 }
961 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -0400962 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500963 return 1;
964 }
965 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400966 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400967 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -0400968 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400969 btrfs_item_offset(right->items + push_items -1);
Chris Mason123abc82007-03-14 14:14:43 -0400970 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
971 btrfs_leaf_data(right) +
972 btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500973 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400974 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500975 BUG_ON(old_left_nritems < 0);
976
Chris Mason0783fcf2007-03-12 20:12:07 -0400977 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -0400978 u32 ioff = btrfs_item_offset(left->items + i);
979 btrfs_set_item_offset(left->items + i, ioff -
980 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -0400981 btrfs_item_offset(left->items +
982 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500983 }
Chris Mason7518a232007-03-12 12:01:18 -0400984 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500985
986 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400987 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -0400988 leaf_data_end(root, right);
989 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
990 push_space, btrfs_leaf_data(right) +
991 leaf_data_end(root, right), push_space);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500992 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400993 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -0400994 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -0400995 btrfs_set_header_nritems(&right->header,
996 btrfs_header_nritems(&right->header) -
997 push_items);
Chris Mason123abc82007-03-14 14:14:43 -0400998 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -0500999
Chris Mason7518a232007-03-12 12:01:18 -04001000 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001001 btrfs_set_item_offset(right->items + i, push_space -
1002 btrfs_item_size(right->items + i));
1003 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001004 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001005
Chris Mason02217ed2007-03-02 16:08:05 -05001006 BUG_ON(list_empty(&t->dirty));
1007 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001008
Chris Masone089f052007-03-16 16:20:31 -04001009 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001010 if (wret)
1011 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001012
1013 /* then fixup the leaf pointer in the path */
1014 if (path->slots[0] < push_items) {
1015 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001016 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001017 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001018 path->slots[1] -= 1;
1019 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001020 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001021 path->slots[0] -= push_items;
1022 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001023 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001024 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001025}
1026
Chris Mason74123bd2007-02-02 11:05:29 -05001027/*
1028 * split the path's leaf in two, making sure there is at least data_size
1029 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001030 *
1031 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001032 */
Chris Masone089f052007-03-16 16:20:31 -04001033static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1034 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001035{
Chris Mason234b63a2007-03-13 10:46:10 -04001036 struct btrfs_buffer *l_buf;
1037 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001038 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001039 int mid;
1040 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001041 struct btrfs_leaf *right;
1042 struct btrfs_buffer *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001043 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001044 int data_copy_size;
1045 int rt_data_off;
1046 int i;
1047 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001048 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001049
Chris Mason40689472007-03-17 14:29:23 -04001050 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001051 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001052 if (wret < 0)
1053 return wret;
1054 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001055 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001056 if (wret < 0)
1057 return wret;
1058 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001059 l_buf = path->nodes[0];
1060 l = &l_buf->leaf;
1061
1062 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001063 if (btrfs_leaf_free_space(root, l) >=
1064 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001065 return 0;
1066
Chris Mason5c680ed2007-02-22 11:39:13 -05001067 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001068 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001069 if (ret)
1070 return ret;
1071 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001072 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001073 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001074 mid = (nritems + 1)/ 2;
Chris Masone089f052007-03-16 16:20:31 -04001075 right_buffer = btrfs_alloc_free_block(trans, root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001076 BUG_ON(!right_buffer);
1077 BUG_ON(mid == nritems);
1078 right = &right_buffer->leaf;
Chris Mason123abc82007-03-14 14:14:43 -04001079 memset(&right->header, 0, sizeof(right->header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001080 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001081 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001082 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001083 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001084 BUG();
1085 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001086 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001087 if (leaf_space_used(l, 0, mid + 1) + space_needed >
Chris Mason123abc82007-03-14 14:14:43 -04001088 BTRFS_LEAF_DATA_SIZE(root))
Chris Masonbe0e5c02007-01-26 15:51:26 -05001089 BUG();
1090 }
Chris Mason7518a232007-03-12 12:01:18 -04001091 btrfs_set_header_nritems(&right->header, nritems - mid);
1092 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1093 btrfs_set_header_level(&right->header, 0);
1094 btrfs_set_header_parentid(&right->header,
1095 btrfs_header_parentid(&root->node->node.header));
Chris Mason123abc82007-03-14 14:14:43 -04001096 data_copy_size = btrfs_item_end(l->items + mid) -
1097 leaf_data_end(root, l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001098 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001099 (nritems - mid) * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001100 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1101 data_copy_size, btrfs_leaf_data(l) +
1102 leaf_data_end(root, l), data_copy_size);
1103 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1104 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001105
Chris Mason0783fcf2007-03-12 20:12:07 -04001106 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001107 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001108 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1109 }
Chris Mason74123bd2007-02-02 11:05:29 -05001110
Chris Mason7518a232007-03-12 12:01:18 -04001111 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001112 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001113 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001114 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001115 if (wret)
1116 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001117 BUG_ON(list_empty(&right_buffer->dirty));
1118 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001119 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001120 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001121 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001122 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001123 path->slots[0] -= mid;
1124 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001125 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001126 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001127 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001128 return ret;
1129}
1130
Chris Mason74123bd2007-02-02 11:05:29 -05001131/*
1132 * Given a key and some data, insert an item into the tree.
1133 * This does all the path init required, making room in the tree if needed.
1134 */
Chris Masone089f052007-03-16 16:20:31 -04001135int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1136 *root, struct btrfs_path *path, struct btrfs_key
1137 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001138{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001139 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001140 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001141 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001142 struct btrfs_leaf *leaf;
1143 struct btrfs_buffer *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001144 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001145 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001146 struct btrfs_disk_key disk_key;
1147
1148 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001149
Chris Mason74123bd2007-02-02 11:05:29 -05001150 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001151 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001152 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001153 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001154 if (ret == 0) {
Chris Mason62e27492007-03-15 12:56:47 -04001155 btrfs_release_path(root, path);
Chris Masonf0930a32007-03-02 09:47:58 -05001156 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001157 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001158 if (ret < 0)
1159 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001160
Chris Mason62e27492007-03-15 12:56:47 -04001161 slot_orig = path->slots[0];
1162 leaf_buf = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001163 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001164
Chris Mason7518a232007-03-12 12:01:18 -04001165 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001166 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001167
Chris Mason123abc82007-03-14 14:14:43 -04001168 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason234b63a2007-03-13 10:46:10 -04001169 sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001170 BUG();
1171
Chris Mason62e27492007-03-15 12:56:47 -04001172 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001173 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001174 if (slot != nritems) {
1175 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001176 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001177
1178 /*
1179 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1180 */
1181 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001182 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001183 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001184 btrfs_set_item_offset(leaf->items + i,
1185 ioff - data_size);
1186 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001187
1188 /* shift the items */
1189 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001190 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001191
1192 /* shift the data */
Chris Mason123abc82007-03-14 14:14:43 -04001193 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1194 btrfs_leaf_data(leaf) +
Chris Masonbe0e5c02007-01-26 15:51:26 -05001195 data_end, old_data - data_end);
1196 data_end = old_data;
1197 }
Chris Mason62e27492007-03-15 12:56:47 -04001198 /* setup the item for the new data */
Chris Masone2fa7222007-03-12 16:22:34 -04001199 memcpy(&leaf->items[slot].key, &disk_key,
1200 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001201 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1202 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001203 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001204
1205 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001206 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001207 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001208
Chris Mason02217ed2007-03-02 16:08:05 -05001209 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason123abc82007-03-14 14:14:43 -04001210 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001211 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001212 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001213out:
Chris Mason62e27492007-03-15 12:56:47 -04001214 return ret;
1215}
1216
1217/*
1218 * Given a key and some data, insert an item into the tree.
1219 * This does all the path init required, making room in the tree if needed.
1220 */
Chris Masone089f052007-03-16 16:20:31 -04001221int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1222 *root, struct btrfs_key *cpu_key, void *data, u32
1223 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001224{
1225 int ret = 0;
1226 struct btrfs_path path;
1227 u8 *ptr;
1228
1229 btrfs_init_path(&path);
Chris Masone089f052007-03-16 16:20:31 -04001230 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001231 if (!ret) {
1232 ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
1233 memcpy(ptr, data, data_size);
1234 }
Chris Mason234b63a2007-03-13 10:46:10 -04001235 btrfs_release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001236 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001237}
1238
Chris Mason74123bd2007-02-02 11:05:29 -05001239/*
Chris Mason5de08d72007-02-24 06:24:44 -05001240 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001241 *
1242 * If the delete empties a node, the node is removed from the tree,
1243 * continuing all the way the root if required. The root is converted into
1244 * a leaf if all the nodes are emptied.
1245 */
Chris Masone089f052007-03-16 16:20:31 -04001246static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1247 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001248{
Chris Mason234b63a2007-03-13 10:46:10 -04001249 struct btrfs_node *node;
1250 struct btrfs_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001251 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001252 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001253 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001254
Chris Masonbb803952007-03-01 12:04:21 -05001255 node = &parent->node;
Chris Mason7518a232007-03-12 12:01:18 -04001256 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001257 if (slot != nritems -1) {
Chris Mason123abc82007-03-14 14:14:43 -04001258 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1259 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001260 }
Chris Mason7518a232007-03-12 12:01:18 -04001261 nritems--;
1262 btrfs_set_header_nritems(&node->header, nritems);
1263 if (nritems == 0 && parent == root->node) {
1264 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001265 /* just turn the root into a leaf and break */
Chris Mason7518a232007-03-12 12:01:18 -04001266 btrfs_set_header_level(&root->node->node.header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001267 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001268 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001269 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001270 if (wret)
1271 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001272 }
Chris Mason02217ed2007-03-02 16:08:05 -05001273 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001274 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001275}
1276
Chris Mason74123bd2007-02-02 11:05:29 -05001277/*
1278 * delete the item at the leaf level in path. If that empties
1279 * the leaf, remove it from the tree
1280 */
Chris Masone089f052007-03-16 16:20:31 -04001281int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1282 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001283{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001284 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001285 struct btrfs_leaf *leaf;
1286 struct btrfs_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001287 int doff;
1288 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001289 int ret = 0;
1290 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001291 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001292
Chris Masoneb60cea2007-02-02 09:18:22 -05001293 leaf_buf = path->nodes[0];
1294 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001295 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001296 doff = btrfs_item_offset(leaf->items + slot);
1297 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001298 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001299
Chris Mason7518a232007-03-12 12:01:18 -04001300 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001301 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001302 int data_end = leaf_data_end(root, leaf);
1303 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1304 btrfs_leaf_data(leaf) + data_end,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001305 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001306 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001307 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001308 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1309 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001310 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001311 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001312 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001313 }
Chris Mason7518a232007-03-12 12:01:18 -04001314 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1315 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001316 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001317 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001318 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001319 btrfs_set_header_level(&leaf->header, 0);
Chris Mason02217ed2007-03-02 16:08:05 -05001320 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001321 } else {
Chris Masone089f052007-03-16 16:20:31 -04001322 clean_tree_block(trans, root, leaf_buf);
1323 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001324 if (wret)
1325 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04001326 wret = btrfs_free_extent(trans, root,
1327 leaf_buf->blocknr, 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001328 if (wret)
1329 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001330 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001331 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001332 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001333 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001334 wret = fixup_low_keys(trans, root, path,
1335 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001336 if (wret)
1337 ret = wret;
1338 }
Chris Mason02217ed2007-03-02 16:08:05 -05001339 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001340
Chris Mason74123bd2007-02-02 11:05:29 -05001341 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04001342 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001343 /* push_leaf_left fixes the path.
1344 * make sure the path still points to our leaf
1345 * for possible call to del_ptr below
1346 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001347 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001348 leaf_buf->count++;
Chris Masone089f052007-03-16 16:20:31 -04001349 wret = push_leaf_left(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001350 if (wret < 0)
1351 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001352 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001353 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04001354 wret = push_leaf_right(trans, root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001355 if (wret < 0)
1356 ret = wret;
1357 }
Chris Mason7518a232007-03-12 12:01:18 -04001358 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001359 u64 blocknr = leaf_buf->blocknr;
Chris Masone089f052007-03-16 16:20:31 -04001360 clean_tree_block(trans, root, leaf_buf);
1361 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001362 if (wret)
1363 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04001364 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04001365 wret = btrfs_free_extent(trans, root, blocknr,
1366 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001367 if (wret)
1368 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001369 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001370 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001371 }
1372 }
1373 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001374 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001375}
1376
Chris Mason97571fd2007-02-24 13:39:08 -05001377/*
1378 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001379 * returns 0 if it found something or 1 if there are no greater leaves.
1380 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001381 */
Chris Mason234b63a2007-03-13 10:46:10 -04001382int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05001383{
1384 int slot;
1385 int level = 1;
1386 u64 blocknr;
Chris Mason234b63a2007-03-13 10:46:10 -04001387 struct btrfs_buffer *c;
1388 struct btrfs_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001389
Chris Mason234b63a2007-03-13 10:46:10 -04001390 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05001391 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001392 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001393 slot = path->slots[level] + 1;
1394 c = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001395 if (slot >= btrfs_header_nritems(&c->node.header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001396 level++;
1397 continue;
1398 }
Chris Mason1d4f8a02007-03-13 09:28:32 -04001399 blocknr = btrfs_node_blockptr(&c->node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05001400 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04001401 btrfs_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001402 next = read_tree_block(root, blocknr);
1403 break;
1404 }
1405 path->slots[level] = slot;
1406 while(1) {
1407 level--;
1408 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04001409 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05001410 path->nodes[level] = next;
1411 path->slots[level] = 0;
1412 if (!level)
1413 break;
Chris Mason1d4f8a02007-03-13 09:28:32 -04001414 next = read_tree_block(root,
1415 btrfs_node_blockptr(&next->node, 0));
Chris Masond97e63b2007-02-20 16:40:44 -05001416 }
1417 return 0;
1418}