]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/btrfs/ctree.c
Btrfs: initial move to kernel module land
[mirror_ubuntu-jammy-kernel.git] / fs / btrfs / ctree.c
CommitLineData
2e635a27 1#include <linux/module.h>
eb60ceac
CM
2#include "ctree.h"
3#include "disk-io.h"
9a8dd150 4
e089f05c
CM
5static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
6 *root, struct btrfs_path *path, int level);
7static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
8 *root, struct btrfs_path *path, int data_size);
9static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
10 *root, struct btrfs_buffer *dst, struct btrfs_buffer
11 *src);
12static int balance_node_right(struct btrfs_trans_handle *trans, struct
13 btrfs_root *root, struct btrfs_buffer *dst_buf,
234b63a0 14 struct btrfs_buffer *src_buf);
e089f05c
CM
15static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
16 struct btrfs_path *path, int level, int slot);
d97e63b6 17
234b63a0 18inline void btrfs_init_path(struct btrfs_path *p)
be0e5c09
CM
19{
20 memset(p, 0, sizeof(*p));
21}
22
234b63a0 23void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
eb60ceac
CM
24{
25 int i;
234b63a0 26 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
eb60ceac
CM
27 if (!p->nodes[i])
28 break;
234b63a0 29 btrfs_block_release(root, p->nodes[i]);
eb60ceac 30 }
aa5d6bed 31 memset(p, 0, sizeof(*p));
eb60ceac
CM
32}
33
e089f05c
CM
34static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
35 *root, struct btrfs_buffer *buf, struct btrfs_buffer
36 *parent, int parent_slot, struct btrfs_buffer
37 **cow_ret)
02217ed2 38{
234b63a0 39 struct btrfs_buffer *cow;
02217ed2
CM
40
41 if (!list_empty(&buf->dirty)) {
42 *cow_ret = buf;
43 return 0;
44 }
e089f05c 45 cow = btrfs_alloc_free_block(trans, root);
123abc88 46 memcpy(&cow->node, &buf->node, root->blocksize);
7518a238 47 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
02217ed2 48 *cow_ret = cow;
e089f05c 49 btrfs_inc_ref(trans, root, buf);
02217ed2
CM
50 if (buf == root->node) {
51 root->node = cow;
52 cow->count++;
a28ec197 53 if (buf != root->commit_root)
e089f05c 54 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
234b63a0 55 btrfs_block_release(root, buf);
02217ed2 56 } else {
1d4f8a0c
CM
57 btrfs_set_node_blockptr(&parent->node, parent_slot,
58 cow->blocknr);
02217ed2 59 BUG_ON(list_empty(&parent->dirty));
e089f05c 60 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
02217ed2 61 }
234b63a0 62 btrfs_block_release(root, buf);
02217ed2
CM
63 return 0;
64}
65
74123bd7
CM
66/*
67 * The leaf data grows from end-to-front in the node.
68 * this returns the address of the start of the last item,
69 * which is the stop of the leaf data stack
70 */
123abc88
CM
71static inline unsigned int leaf_data_end(struct btrfs_root *root,
72 struct btrfs_leaf *leaf)
be0e5c09 73{
7518a238 74 u32 nr = btrfs_header_nritems(&leaf->header);
be0e5c09 75 if (nr == 0)
123abc88 76 return BTRFS_LEAF_DATA_SIZE(root);
0783fcfc 77 return btrfs_item_offset(leaf->items + nr - 1);
be0e5c09
CM
78}
79
74123bd7
CM
80/*
81 * The space between the end of the leaf items and
82 * the start of the leaf data. IOW, how much room
83 * the leaf has left for both items and data
84 */
123abc88 85int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
be0e5c09 86{
123abc88 87 int data_end = leaf_data_end(root, leaf);
7518a238 88 int nritems = btrfs_header_nritems(&leaf->header);
be0e5c09 89 char *items_end = (char *)(leaf->items + nritems + 1);
123abc88 90 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
be0e5c09
CM
91}
92
74123bd7
CM
93/*
94 * compare two keys in a memcmp fashion
95 */
9aca1d51 96static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
be0e5c09 97{
e2fa7227
CM
98 struct btrfs_key k1;
99
100 btrfs_disk_key_to_cpu(&k1, disk);
101
102 if (k1.objectid > k2->objectid)
be0e5c09 103 return 1;
e2fa7227 104 if (k1.objectid < k2->objectid)
be0e5c09 105 return -1;
62e2749e
CM
106 if (k1.flags > k2->flags)
107 return 1;
108 if (k1.flags < k2->flags)
109 return -1;
a8a2ee0c
CM
110 if (k1.offset > k2->offset)
111 return 1;
112 if (k1.offset < k2->offset)
113 return -1;
be0e5c09
CM
114 return 0;
115}
74123bd7 116
123abc88
CM
117static int check_node(struct btrfs_root *root, struct btrfs_path *path,
118 int level)
aa5d6bed
CM
119{
120 int i;
234b63a0
CM
121 struct btrfs_node *parent = NULL;
122 struct btrfs_node *node = &path->nodes[level]->node;
aa5d6bed 123 int parent_slot;
7518a238 124 u32 nritems = btrfs_header_nritems(&node->header);
aa5d6bed
CM
125
126 if (path->nodes[level + 1])
127 parent = &path->nodes[level + 1]->node;
128 parent_slot = path->slots[level + 1];
7518a238
CM
129 BUG_ON(nritems == 0);
130 if (parent) {
e2fa7227 131 struct btrfs_disk_key *parent_key;
123abc88
CM
132 parent_key = &parent->ptrs[parent_slot].key;
133 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
e2fa7227 134 sizeof(struct btrfs_disk_key)));
1d4f8a0c 135 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
7518a238 136 btrfs_header_blocknr(&node->header));
aa5d6bed 137 }
123abc88 138 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
7518a238 139 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
e2fa7227 140 struct btrfs_key cpukey;
123abc88
CM
141 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
142 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
aa5d6bed
CM
143 }
144 return 0;
145}
146
123abc88
CM
147static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
148 int level)
aa5d6bed
CM
149{
150 int i;
234b63a0
CM
151 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
152 struct btrfs_node *parent = NULL;
aa5d6bed 153 int parent_slot;
7518a238 154 u32 nritems = btrfs_header_nritems(&leaf->header);
aa5d6bed
CM
155
156 if (path->nodes[level + 1])
157 parent = &path->nodes[level + 1]->node;
158 parent_slot = path->slots[level + 1];
123abc88 159 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
7518a238
CM
160
161 if (nritems == 0)
162 return 0;
163
164 if (parent) {
e2fa7227 165 struct btrfs_disk_key *parent_key;
123abc88 166 parent_key = &parent->ptrs[parent_slot].key;
aa5d6bed 167 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
e2fa7227 168 sizeof(struct btrfs_disk_key)));
1d4f8a0c 169 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
7518a238 170 btrfs_header_blocknr(&leaf->header));
aa5d6bed 171 }
7518a238 172 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
e2fa7227
CM
173 struct btrfs_key cpukey;
174 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
aa5d6bed 175 BUG_ON(comp_keys(&leaf->items[i].key,
e2fa7227 176 &cpukey) >= 0);
0783fcfc
CM
177 BUG_ON(btrfs_item_offset(leaf->items + i) !=
178 btrfs_item_end(leaf->items + i + 1));
aa5d6bed 179 if (i == 0) {
0783fcfc
CM
180 BUG_ON(btrfs_item_offset(leaf->items + i) +
181 btrfs_item_size(leaf->items + i) !=
123abc88 182 BTRFS_LEAF_DATA_SIZE(root));
aa5d6bed
CM
183 }
184 }
aa5d6bed
CM
185 return 0;
186}
187
123abc88
CM
188static int check_block(struct btrfs_root *root, struct btrfs_path *path,
189 int level)
aa5d6bed
CM
190{
191 if (level == 0)
123abc88
CM
192 return check_leaf(root, path, level);
193 return check_node(root, path, level);
aa5d6bed
CM
194}
195
74123bd7
CM
196/*
197 * search for key in the array p. items p are item_size apart
198 * and there are 'max' items in p
199 * the slot in the array is returned via slot, and it points to
200 * the place where you would insert key if it is not found in
201 * the array.
202 *
203 * slot may point to max if the key is bigger than all of the keys
204 */
9aca1d51 205static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
be0e5c09
CM
206 int max, int *slot)
207{
208 int low = 0;
209 int high = max;
210 int mid;
211 int ret;
e2fa7227 212 struct btrfs_disk_key *tmp;
be0e5c09
CM
213
214 while(low < high) {
215 mid = (low + high) / 2;
e2fa7227 216 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
be0e5c09
CM
217 ret = comp_keys(tmp, key);
218
219 if (ret < 0)
220 low = mid + 1;
221 else if (ret > 0)
222 high = mid;
223 else {
224 *slot = mid;
225 return 0;
226 }
227 }
228 *slot = low;
229 return 1;
230}
231
97571fd0
CM
232/*
233 * simple bin_search frontend that does the right thing for
234 * leaves vs nodes
235 */
9aca1d51 236static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
be0e5c09 237{
7518a238 238 if (btrfs_is_leaf(c)) {
234b63a0 239 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
0783fcfc
CM
240 return generic_bin_search((void *)l->items,
241 sizeof(struct btrfs_item),
7518a238
CM
242 key, btrfs_header_nritems(&c->header),
243 slot);
be0e5c09 244 } else {
123abc88
CM
245 return generic_bin_search((void *)c->ptrs,
246 sizeof(struct btrfs_key_ptr),
7518a238
CM
247 key, btrfs_header_nritems(&c->header),
248 slot);
be0e5c09
CM
249 }
250 return -1;
251}
252
9aca1d51 253static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
234b63a0 254 struct btrfs_buffer *parent_buf,
bb803951
CM
255 int slot)
256{
234b63a0 257 struct btrfs_node *node = &parent_buf->node;
bb803951
CM
258 if (slot < 0)
259 return NULL;
7518a238 260 if (slot >= btrfs_header_nritems(&node->header))
bb803951 261 return NULL;
1d4f8a0c 262 return read_tree_block(root, btrfs_node_blockptr(node, slot));
bb803951
CM
263}
264
e089f05c
CM
265static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
266 *root, struct btrfs_path *path, int level)
bb803951 267{
234b63a0
CM
268 struct btrfs_buffer *right_buf;
269 struct btrfs_buffer *mid_buf;
270 struct btrfs_buffer *left_buf;
271 struct btrfs_buffer *parent_buf = NULL;
272 struct btrfs_node *right = NULL;
273 struct btrfs_node *mid;
274 struct btrfs_node *left = NULL;
275 struct btrfs_node *parent = NULL;
bb803951
CM
276 int ret = 0;
277 int wret;
278 int pslot;
bb803951 279 int orig_slot = path->slots[level];
79f95c82 280 u64 orig_ptr;
bb803951
CM
281
282 if (level == 0)
283 return 0;
284
285 mid_buf = path->nodes[level];
286 mid = &mid_buf->node;
1d4f8a0c 287 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
79f95c82 288
234b63a0 289 if (level < BTRFS_MAX_LEVEL - 1)
bb803951
CM
290 parent_buf = path->nodes[level + 1];
291 pslot = path->slots[level + 1];
292
40689478
CM
293 /*
294 * deal with the case where there is only one pointer in the root
295 * by promoting the node below to a root
296 */
bb803951 297 if (!parent_buf) {
234b63a0 298 struct btrfs_buffer *child;
bb803951
CM
299 u64 blocknr = mid_buf->blocknr;
300
7518a238 301 if (btrfs_header_nritems(&mid->header) != 1)
bb803951
CM
302 return 0;
303
304 /* promote the child to a root */
305 child = read_node_slot(root, mid_buf, 0);
306 BUG_ON(!child);
307 root->node = child;
308 path->nodes[level] = NULL;
309 /* once for the path */
234b63a0 310 btrfs_block_release(root, mid_buf);
bb803951 311 /* once for the root ptr */
234b63a0 312 btrfs_block_release(root, mid_buf);
e089f05c
CM
313 clean_tree_block(trans, root, mid_buf);
314 return btrfs_free_extent(trans, root, blocknr, 1, 1);
bb803951
CM
315 }
316 parent = &parent_buf->node;
317
123abc88
CM
318 if (btrfs_header_nritems(&mid->header) >
319 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
bb803951
CM
320 return 0;
321
bb803951
CM
322 left_buf = read_node_slot(root, parent_buf, pslot - 1);
323 right_buf = read_node_slot(root, parent_buf, pslot + 1);
79f95c82
CM
324
325 /* first, try to make some room in the middle buffer */
bb803951 326 if (left_buf) {
e089f05c
CM
327 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
328 &left_buf);
bb803951 329 left = &left_buf->node;
7518a238 330 orig_slot += btrfs_header_nritems(&left->header);
e089f05c 331 wret = push_node_left(trans, root, left_buf, mid_buf);
79f95c82
CM
332 if (wret < 0)
333 ret = wret;
bb803951 334 }
79f95c82
CM
335
336 /*
337 * then try to empty the right most buffer into the middle
338 */
bb803951 339 if (right_buf) {
e089f05c
CM
340 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
341 &right_buf);
79f95c82 342 right = &right_buf->node;
e089f05c 343 wret = push_node_left(trans, root, mid_buf, right_buf);
79f95c82
CM
344 if (wret < 0)
345 ret = wret;
7518a238 346 if (btrfs_header_nritems(&right->header) == 0) {
bb803951 347 u64 blocknr = right_buf->blocknr;
234b63a0 348 btrfs_block_release(root, right_buf);
e089f05c 349 clean_tree_block(trans, root, right_buf);
bb803951
CM
350 right_buf = NULL;
351 right = NULL;
e089f05c
CM
352 wret = del_ptr(trans, root, path, level + 1, pslot +
353 1);
bb803951
CM
354 if (wret)
355 ret = wret;
e089f05c 356 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
bb803951
CM
357 if (wret)
358 ret = wret;
359 } else {
123abc88
CM
360 memcpy(&parent->ptrs[pslot + 1].key,
361 &right->ptrs[0].key,
e2fa7227 362 sizeof(struct btrfs_disk_key));
02217ed2 363 BUG_ON(list_empty(&parent_buf->dirty));
bb803951
CM
364 }
365 }
7518a238 366 if (btrfs_header_nritems(&mid->header) == 1) {
79f95c82
CM
367 /*
368 * we're not allowed to leave a node with one item in the
369 * tree during a delete. A deletion from lower in the tree
370 * could try to delete the only pointer in this node.
371 * So, pull some keys from the left.
372 * There has to be a left pointer at this point because
373 * otherwise we would have pulled some pointers from the
374 * right
375 */
376 BUG_ON(!left_buf);
e089f05c 377 wret = balance_node_right(trans, root, mid_buf, left_buf);
79f95c82
CM
378 if (wret < 0)
379 ret = wret;
380 BUG_ON(wret == 1);
381 }
7518a238 382 if (btrfs_header_nritems(&mid->header) == 0) {
79f95c82 383 /* we've managed to empty the middle node, drop it */
bb803951 384 u64 blocknr = mid_buf->blocknr;
234b63a0 385 btrfs_block_release(root, mid_buf);
e089f05c 386 clean_tree_block(trans, root, mid_buf);
bb803951
CM
387 mid_buf = NULL;
388 mid = NULL;
e089f05c 389 wret = del_ptr(trans, root, path, level + 1, pslot);
bb803951
CM
390 if (wret)
391 ret = wret;
e089f05c 392 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
bb803951
CM
393 if (wret)
394 ret = wret;
79f95c82
CM
395 } else {
396 /* update the parent key to reflect our changes */
123abc88 397 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
e2fa7227 398 sizeof(struct btrfs_disk_key));
02217ed2 399 BUG_ON(list_empty(&parent_buf->dirty));
79f95c82 400 }
bb803951 401
79f95c82 402 /* update the path */
bb803951 403 if (left_buf) {
7518a238 404 if (btrfs_header_nritems(&left->header) > orig_slot) {
bb803951
CM
405 left_buf->count++; // released below
406 path->nodes[level] = left_buf;
407 path->slots[level + 1] -= 1;
408 path->slots[level] = orig_slot;
409 if (mid_buf)
234b63a0 410 btrfs_block_release(root, mid_buf);
bb803951 411 } else {
7518a238 412 orig_slot -= btrfs_header_nritems(&left->header);
bb803951
CM
413 path->slots[level] = orig_slot;
414 }
415 }
79f95c82 416 /* double check we haven't messed things up */
123abc88 417 check_block(root, path, level);
1d4f8a0c
CM
418 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
419 path->slots[level]))
79f95c82 420 BUG();
bb803951
CM
421
422 if (right_buf)
234b63a0 423 btrfs_block_release(root, right_buf);
bb803951 424 if (left_buf)
234b63a0 425 btrfs_block_release(root, left_buf);
bb803951
CM
426 return ret;
427}
428
74123bd7
CM
429/*
430 * look for key in the tree. path is filled in with nodes along the way
431 * if key is found, we return zero and you can find the item in the leaf
432 * level of the path (level 0)
433 *
434 * If the key isn't found, the path points to the slot where it should
aa5d6bed
CM
435 * be inserted, and 1 is returned. If there are other errors during the
436 * search a negative error number is returned.
97571fd0
CM
437 *
438 * if ins_len > 0, nodes and leaves will be split as we walk down the
439 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
440 * possible)
74123bd7 441 */
e089f05c
CM
442int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
443 *root, struct btrfs_key *key, struct btrfs_path *p, int
444 ins_len, int cow)
be0e5c09 445{
234b63a0
CM
446 struct btrfs_buffer *b;
447 struct btrfs_buffer *cow_buf;
448 struct btrfs_node *c;
be0e5c09
CM
449 int slot;
450 int ret;
451 int level;
5c680ed6 452
bb803951
CM
453again:
454 b = root->node;
eb60ceac
CM
455 b->count++;
456 while (b) {
7518a238 457 level = btrfs_header_level(&b->node.header);
02217ed2
CM
458 if (cow) {
459 int wret;
e089f05c
CM
460 wret = btrfs_cow_block(trans, root, b, p->nodes[level +
461 1], p->slots[level + 1],
462 &cow_buf);
02217ed2
CM
463 b = cow_buf;
464 }
465 BUG_ON(!cow && ins_len);
eb60ceac 466 c = &b->node;
eb60ceac 467 p->nodes[level] = b;
123abc88 468 ret = check_block(root, p, level);
aa5d6bed
CM
469 if (ret)
470 return -1;
be0e5c09 471 ret = bin_search(c, key, &slot);
7518a238 472 if (!btrfs_is_leaf(c)) {
be0e5c09
CM
473 if (ret && slot > 0)
474 slot -= 1;
475 p->slots[level] = slot;
7518a238 476 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
123abc88 477 BTRFS_NODEPTRS_PER_BLOCK(root)) {
e089f05c 478 int sret = split_node(trans, root, p, level);
5c680ed6
CM
479 BUG_ON(sret > 0);
480 if (sret)
481 return sret;
482 b = p->nodes[level];
483 c = &b->node;
484 slot = p->slots[level];
bb803951 485 } else if (ins_len < 0) {
e089f05c
CM
486 int sret = balance_level(trans, root, p,
487 level);
bb803951
CM
488 if (sret)
489 return sret;
490 b = p->nodes[level];
491 if (!b)
492 goto again;
493 c = &b->node;
494 slot = p->slots[level];
7518a238 495 BUG_ON(btrfs_header_nritems(&c->header) == 1);
5c680ed6 496 }
1d4f8a0c 497 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
be0e5c09 498 } else {
234b63a0 499 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
be0e5c09 500 p->slots[level] = slot;
123abc88 501 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
0783fcfc 502 sizeof(struct btrfs_item) + ins_len) {
e089f05c 503 int sret = split_leaf(trans, root, p, ins_len);
5c680ed6
CM
504 BUG_ON(sret > 0);
505 if (sret)
506 return sret;
507 }
bb803951 508 BUG_ON(root->node->count == 1);
be0e5c09
CM
509 return ret;
510 }
511 }
bb803951 512 BUG_ON(root->node->count == 1);
aa5d6bed 513 return 1;
be0e5c09
CM
514}
515
74123bd7
CM
516/*
517 * adjust the pointers going up the tree, starting at level
518 * making sure the right key of each node is points to 'key'.
519 * This is used after shifting pointers to the left, so it stops
520 * fixing up pointers when a given leaf/node is not in slot 0 of the
521 * higher levels
aa5d6bed
CM
522 *
523 * If this fails to write a tree block, it returns -1, but continues
524 * fixing up the blocks in ram so the tree is consistent.
74123bd7 525 */
e089f05c
CM
526static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
527 *root, struct btrfs_path *path, struct btrfs_disk_key
528 *key, int level)
be0e5c09
CM
529{
530 int i;
aa5d6bed 531 int ret = 0;
234b63a0
CM
532 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
533 struct btrfs_node *t;
be0e5c09 534 int tslot = path->slots[i];
eb60ceac 535 if (!path->nodes[i])
be0e5c09 536 break;
eb60ceac 537 t = &path->nodes[i]->node;
123abc88 538 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
02217ed2 539 BUG_ON(list_empty(&path->nodes[i]->dirty));
be0e5c09
CM
540 if (tslot != 0)
541 break;
542 }
aa5d6bed 543 return ret;
be0e5c09
CM
544}
545
74123bd7
CM
546/*
547 * try to push data from one node into the next node left in the
79f95c82 548 * tree.
aa5d6bed
CM
549 *
550 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
551 * error, and > 0 if there was no room in the left hand block.
74123bd7 552 */
e089f05c
CM
553static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
554 *root, struct btrfs_buffer *dst_buf, struct
555 btrfs_buffer *src_buf)
be0e5c09 556{
234b63a0
CM
557 struct btrfs_node *src = &src_buf->node;
558 struct btrfs_node *dst = &dst_buf->node;
be0e5c09 559 int push_items = 0;
bb803951
CM
560 int src_nritems;
561 int dst_nritems;
aa5d6bed 562 int ret = 0;
be0e5c09 563
7518a238
CM
564 src_nritems = btrfs_header_nritems(&src->header);
565 dst_nritems = btrfs_header_nritems(&dst->header);
123abc88 566 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
eb60ceac 567 if (push_items <= 0) {
be0e5c09 568 return 1;
eb60ceac 569 }
be0e5c09 570
bb803951 571 if (src_nritems < push_items)
79f95c82
CM
572 push_items = src_nritems;
573
123abc88
CM
574 memcpy(dst->ptrs + dst_nritems, src->ptrs,
575 push_items * sizeof(struct btrfs_key_ptr));
bb803951 576 if (push_items < src_nritems) {
123abc88 577 memmove(src->ptrs, src->ptrs + push_items,
e2fa7227 578 (src_nritems - push_items) *
123abc88 579 sizeof(struct btrfs_key_ptr));
bb803951 580 }
7518a238
CM
581 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
582 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
02217ed2
CM
583 BUG_ON(list_empty(&src_buf->dirty));
584 BUG_ON(list_empty(&dst_buf->dirty));
79f95c82
CM
585 return ret;
586}
587
588/*
589 * try to push data from one node into the next node right in the
590 * tree.
591 *
592 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
593 * error, and > 0 if there was no room in the right hand block.
594 *
595 * this will only push up to 1/2 the contents of the left node over
596 */
e089f05c
CM
597static int balance_node_right(struct btrfs_trans_handle *trans, struct
598 btrfs_root *root, struct btrfs_buffer *dst_buf,
234b63a0 599 struct btrfs_buffer *src_buf)
79f95c82 600{
234b63a0
CM
601 struct btrfs_node *src = &src_buf->node;
602 struct btrfs_node *dst = &dst_buf->node;
79f95c82
CM
603 int push_items = 0;
604 int max_push;
605 int src_nritems;
606 int dst_nritems;
607 int ret = 0;
79f95c82 608
7518a238
CM
609 src_nritems = btrfs_header_nritems(&src->header);
610 dst_nritems = btrfs_header_nritems(&dst->header);
123abc88 611 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
79f95c82
CM
612 if (push_items <= 0) {
613 return 1;
614 }
615
616 max_push = src_nritems / 2 + 1;
617 /* don't try to empty the node */
618 if (max_push > src_nritems)
619 return 1;
620 if (max_push < push_items)
621 push_items = max_push;
622
123abc88
CM
623 memmove(dst->ptrs + push_items, dst->ptrs,
624 dst_nritems * sizeof(struct btrfs_key_ptr));
625 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
626 push_items * sizeof(struct btrfs_key_ptr));
79f95c82 627
7518a238
CM
628 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
629 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
79f95c82 630
02217ed2
CM
631 BUG_ON(list_empty(&src_buf->dirty));
632 BUG_ON(list_empty(&dst_buf->dirty));
aa5d6bed 633 return ret;
be0e5c09
CM
634}
635
97571fd0
CM
636/*
637 * helper function to insert a new root level in the tree.
638 * A new node is allocated, and a single item is inserted to
639 * point to the existing root
aa5d6bed
CM
640 *
641 * returns zero on success or < 0 on failure.
97571fd0 642 */
e089f05c
CM
643static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
644 *root, struct btrfs_path *path, int level)
5c680ed6 645{
234b63a0
CM
646 struct btrfs_buffer *t;
647 struct btrfs_node *lower;
648 struct btrfs_node *c;
e2fa7227 649 struct btrfs_disk_key *lower_key;
5c680ed6
CM
650
651 BUG_ON(path->nodes[level]);
652 BUG_ON(path->nodes[level-1] != root->node);
653
e089f05c 654 t = btrfs_alloc_free_block(trans, root);
5c680ed6 655 c = &t->node;
123abc88 656 memset(c, 0, root->blocksize);
7518a238
CM
657 btrfs_set_header_nritems(&c->header, 1);
658 btrfs_set_header_level(&c->header, level);
659 btrfs_set_header_blocknr(&c->header, t->blocknr);
660 btrfs_set_header_parentid(&c->header,
661 btrfs_header_parentid(&root->node->node.header));
5c680ed6 662 lower = &path->nodes[level-1]->node;
7518a238 663 if (btrfs_is_leaf(lower))
234b63a0 664 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
5c680ed6 665 else
123abc88
CM
666 lower_key = &lower->ptrs[0].key;
667 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
1d4f8a0c 668 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
5c680ed6 669 /* the super has an extra ref to root->node */
234b63a0 670 btrfs_block_release(root, root->node);
5c680ed6
CM
671 root->node = t;
672 t->count++;
5c680ed6
CM
673 path->nodes[level] = t;
674 path->slots[level] = 0;
675 return 0;
676}
677
74123bd7
CM
678/*
679 * worker function to insert a single pointer in a node.
680 * the node should have enough room for the pointer already
97571fd0 681 *
74123bd7
CM
682 * slot and level indicate where you want the key to go, and
683 * blocknr is the block the key points to.
aa5d6bed
CM
684 *
685 * returns zero on success and < 0 on any error
74123bd7 686 */
e089f05c
CM
687static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
688 *root, struct btrfs_path *path, struct btrfs_disk_key
689 *key, u64 blocknr, int slot, int level)
74123bd7 690{
234b63a0 691 struct btrfs_node *lower;
74123bd7 692 int nritems;
5c680ed6
CM
693
694 BUG_ON(!path->nodes[level]);
74123bd7 695 lower = &path->nodes[level]->node;
7518a238 696 nritems = btrfs_header_nritems(&lower->header);
74123bd7
CM
697 if (slot > nritems)
698 BUG();
123abc88 699 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
74123bd7
CM
700 BUG();
701 if (slot != nritems) {
123abc88
CM
702 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
703 (nritems - slot) * sizeof(struct btrfs_key_ptr));
74123bd7 704 }
123abc88 705 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
1d4f8a0c 706 btrfs_set_node_blockptr(lower, slot, blocknr);
7518a238 707 btrfs_set_header_nritems(&lower->header, nritems + 1);
02217ed2 708 BUG_ON(list_empty(&path->nodes[level]->dirty));
74123bd7
CM
709 return 0;
710}
711
97571fd0
CM
712/*
713 * split the node at the specified level in path in two.
714 * The path is corrected to point to the appropriate node after the split
715 *
716 * Before splitting this tries to make some room in the node by pushing
717 * left and right, if either one works, it returns right away.
aa5d6bed
CM
718 *
719 * returns 0 on success and < 0 on failure
97571fd0 720 */
e089f05c
CM
721static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
722 *root, struct btrfs_path *path, int level)
be0e5c09 723{
234b63a0
CM
724 struct btrfs_buffer *t;
725 struct btrfs_node *c;
726 struct btrfs_buffer *split_buffer;
727 struct btrfs_node *split;
be0e5c09 728 int mid;
5c680ed6 729 int ret;
aa5d6bed 730 int wret;
7518a238 731 u32 c_nritems;
eb60ceac 732
5c680ed6
CM
733 t = path->nodes[level];
734 c = &t->node;
735 if (t == root->node) {
736 /* trying to split the root, lets make a new one */
e089f05c 737 ret = insert_new_root(trans, root, path, level + 1);
5c680ed6
CM
738 if (ret)
739 return ret;
be0e5c09 740 }
7518a238 741 c_nritems = btrfs_header_nritems(&c->header);
e089f05c 742 split_buffer = btrfs_alloc_free_block(trans, root);
5c680ed6 743 split = &split_buffer->node;
7518a238
CM
744 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
745 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
746 btrfs_set_header_parentid(&split->header,
747 btrfs_header_parentid(&root->node->node.header));
748 mid = (c_nritems + 1) / 2;
123abc88
CM
749 memcpy(split->ptrs, c->ptrs + mid,
750 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
7518a238
CM
751 btrfs_set_header_nritems(&split->header, c_nritems - mid);
752 btrfs_set_header_nritems(&c->header, mid);
aa5d6bed
CM
753 ret = 0;
754
02217ed2 755 BUG_ON(list_empty(&t->dirty));
e089f05c 756 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
123abc88
CM
757 split_buffer->blocknr, path->slots[level + 1] + 1,
758 level + 1);
aa5d6bed
CM
759 if (wret)
760 ret = wret;
761
5de08d7d 762 if (path->slots[level] >= mid) {
5c680ed6 763 path->slots[level] -= mid;
234b63a0 764 btrfs_block_release(root, t);
5c680ed6
CM
765 path->nodes[level] = split_buffer;
766 path->slots[level + 1] += 1;
767 } else {
234b63a0 768 btrfs_block_release(root, split_buffer);
be0e5c09 769 }
aa5d6bed 770 return ret;
be0e5c09
CM
771}
772
74123bd7
CM
773/*
774 * how many bytes are required to store the items in a leaf. start
775 * and nr indicate which items in the leaf to check. This totals up the
776 * space used both by the item structs and the item data
777 */
234b63a0 778static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
be0e5c09
CM
779{
780 int data_len;
781 int end = start + nr - 1;
782
783 if (!nr)
784 return 0;
0783fcfc
CM
785 data_len = btrfs_item_end(l->items + start);
786 data_len = data_len - btrfs_item_offset(l->items + end);
787 data_len += sizeof(struct btrfs_item) * nr;
be0e5c09
CM
788 return data_len;
789}
790
00ec4c51
CM
791/*
792 * push some data in the path leaf to the right, trying to free up at
793 * least data_size bytes. returns zero if the push worked, nonzero otherwise
aa5d6bed
CM
794 *
795 * returns 1 if the push failed because the other node didn't have enough
796 * room, 0 if everything worked out and < 0 if there were major errors.
00ec4c51 797 */
e089f05c
CM
798static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
799 *root, struct btrfs_path *path, int data_size)
00ec4c51 800{
234b63a0
CM
801 struct btrfs_buffer *left_buf = path->nodes[0];
802 struct btrfs_leaf *left = &left_buf->leaf;
803 struct btrfs_leaf *right;
804 struct btrfs_buffer *right_buf;
805 struct btrfs_buffer *upper;
00ec4c51
CM
806 int slot;
807 int i;
808 int free_space;
809 int push_space = 0;
810 int push_items = 0;
0783fcfc 811 struct btrfs_item *item;
7518a238
CM
812 u32 left_nritems;
813 u32 right_nritems;
00ec4c51
CM
814
815 slot = path->slots[1];
816 if (!path->nodes[1]) {
817 return 1;
818 }
819 upper = path->nodes[1];
7518a238 820 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
00ec4c51
CM
821 return 1;
822 }
1d4f8a0c
CM
823 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
824 slot + 1));
00ec4c51 825 right = &right_buf->leaf;
123abc88 826 free_space = btrfs_leaf_free_space(root, right);
0783fcfc 827 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 828 btrfs_block_release(root, right_buf);
00ec4c51
CM
829 return 1;
830 }
02217ed2 831 /* cow and double check */
e089f05c 832 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
02217ed2 833 right = &right_buf->leaf;
123abc88 834 free_space = btrfs_leaf_free_space(root, right);
0783fcfc 835 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 836 btrfs_block_release(root, right_buf);
02217ed2
CM
837 return 1;
838 }
839
7518a238
CM
840 left_nritems = btrfs_header_nritems(&left->header);
841 for (i = left_nritems - 1; i >= 0; i--) {
00ec4c51
CM
842 item = left->items + i;
843 if (path->slots[0] == i)
844 push_space += data_size + sizeof(*item);
0783fcfc
CM
845 if (btrfs_item_size(item) + sizeof(*item) + push_space >
846 free_space)
00ec4c51
CM
847 break;
848 push_items++;
0783fcfc 849 push_space += btrfs_item_size(item) + sizeof(*item);
00ec4c51
CM
850 }
851 if (push_items == 0) {
234b63a0 852 btrfs_block_release(root, right_buf);
00ec4c51
CM
853 return 1;
854 }
7518a238 855 right_nritems = btrfs_header_nritems(&right->header);
00ec4c51 856 /* push left to right */
0783fcfc 857 push_space = btrfs_item_end(left->items + left_nritems - push_items);
123abc88 858 push_space -= leaf_data_end(root, left);
00ec4c51 859 /* make room in the right data area */
123abc88
CM
860 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
861 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
862 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
00ec4c51 863 /* copy from the left data area */
123abc88
CM
864 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
865 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
00ec4c51 866 memmove(right->items + push_items, right->items,
0783fcfc 867 right_nritems * sizeof(struct btrfs_item));
00ec4c51 868 /* copy the items from left to right */
7518a238 869 memcpy(right->items, left->items + left_nritems - push_items,
0783fcfc 870 push_items * sizeof(struct btrfs_item));
00ec4c51
CM
871
872 /* update the item pointers */
7518a238
CM
873 right_nritems += push_items;
874 btrfs_set_header_nritems(&right->header, right_nritems);
123abc88 875 push_space = BTRFS_LEAF_DATA_SIZE(root);
7518a238 876 for (i = 0; i < right_nritems; i++) {
0783fcfc
CM
877 btrfs_set_item_offset(right->items + i, push_space -
878 btrfs_item_size(right->items + i));
879 push_space = btrfs_item_offset(right->items + i);
00ec4c51 880 }
7518a238
CM
881 left_nritems -= push_items;
882 btrfs_set_header_nritems(&left->header, left_nritems);
00ec4c51 883
02217ed2
CM
884 BUG_ON(list_empty(&left_buf->dirty));
885 BUG_ON(list_empty(&right_buf->dirty));
123abc88 886 memcpy(&upper->node.ptrs[slot + 1].key,
e2fa7227 887 &right->items[0].key, sizeof(struct btrfs_disk_key));
02217ed2
CM
888 BUG_ON(list_empty(&upper->dirty));
889
00ec4c51 890 /* then fixup the leaf pointer in the path */
7518a238
CM
891 if (path->slots[0] >= left_nritems) {
892 path->slots[0] -= left_nritems;
234b63a0 893 btrfs_block_release(root, path->nodes[0]);
00ec4c51
CM
894 path->nodes[0] = right_buf;
895 path->slots[1] += 1;
896 } else {
234b63a0 897 btrfs_block_release(root, right_buf);
00ec4c51
CM
898 }
899 return 0;
900}
74123bd7
CM
901/*
902 * push some data in the path leaf to the left, trying to free up at
903 * least data_size bytes. returns zero if the push worked, nonzero otherwise
904 */
e089f05c
CM
905static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
906 *root, struct btrfs_path *path, int data_size)
be0e5c09 907{
234b63a0
CM
908 struct btrfs_buffer *right_buf = path->nodes[0];
909 struct btrfs_leaf *right = &right_buf->leaf;
910 struct btrfs_buffer *t;
911 struct btrfs_leaf *left;
be0e5c09
CM
912 int slot;
913 int i;
914 int free_space;
915 int push_space = 0;
916 int push_items = 0;
0783fcfc 917 struct btrfs_item *item;
7518a238 918 u32 old_left_nritems;
aa5d6bed
CM
919 int ret = 0;
920 int wret;
be0e5c09
CM
921
922 slot = path->slots[1];
923 if (slot == 0) {
924 return 1;
925 }
926 if (!path->nodes[1]) {
927 return 1;
928 }
1d4f8a0c
CM
929 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
930 slot - 1));
eb60ceac 931 left = &t->leaf;
123abc88 932 free_space = btrfs_leaf_free_space(root, left);
0783fcfc 933 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 934 btrfs_block_release(root, t);
be0e5c09
CM
935 return 1;
936 }
02217ed2
CM
937
938 /* cow and double check */
e089f05c 939 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
02217ed2 940 left = &t->leaf;
123abc88 941 free_space = btrfs_leaf_free_space(root, left);
0783fcfc 942 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 943 btrfs_block_release(root, t);
02217ed2
CM
944 return 1;
945 }
946
7518a238 947 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
be0e5c09
CM
948 item = right->items + i;
949 if (path->slots[0] == i)
950 push_space += data_size + sizeof(*item);
0783fcfc
CM
951 if (btrfs_item_size(item) + sizeof(*item) + push_space >
952 free_space)
be0e5c09
CM
953 break;
954 push_items++;
0783fcfc 955 push_space += btrfs_item_size(item) + sizeof(*item);
be0e5c09
CM
956 }
957 if (push_items == 0) {
234b63a0 958 btrfs_block_release(root, t);
be0e5c09
CM
959 return 1;
960 }
961 /* push data from right to left */
7518a238 962 memcpy(left->items + btrfs_header_nritems(&left->header),
0783fcfc 963 right->items, push_items * sizeof(struct btrfs_item));
123abc88 964 push_space = BTRFS_LEAF_DATA_SIZE(root) -
0783fcfc 965 btrfs_item_offset(right->items + push_items -1);
123abc88
CM
966 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
967 btrfs_leaf_data(right) +
968 btrfs_item_offset(right->items + push_items - 1),
be0e5c09 969 push_space);
7518a238 970 old_left_nritems = btrfs_header_nritems(&left->header);
eb60ceac
CM
971 BUG_ON(old_left_nritems < 0);
972
0783fcfc 973 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
123abc88
CM
974 u32 ioff = btrfs_item_offset(left->items + i);
975 btrfs_set_item_offset(left->items + i, ioff -
976 (BTRFS_LEAF_DATA_SIZE(root) -
0783fcfc
CM
977 btrfs_item_offset(left->items +
978 old_left_nritems - 1)));
be0e5c09 979 }
7518a238 980 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
be0e5c09
CM
981
982 /* fixup right node */
0783fcfc 983 push_space = btrfs_item_offset(right->items + push_items - 1) -
123abc88
CM
984 leaf_data_end(root, right);
985 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
986 push_space, btrfs_leaf_data(right) +
987 leaf_data_end(root, right), push_space);
be0e5c09 988 memmove(right->items, right->items + push_items,
7518a238 989 (btrfs_header_nritems(&right->header) - push_items) *
0783fcfc 990 sizeof(struct btrfs_item));
7518a238
CM
991 btrfs_set_header_nritems(&right->header,
992 btrfs_header_nritems(&right->header) -
993 push_items);
123abc88 994 push_space = BTRFS_LEAF_DATA_SIZE(root);
eb60ceac 995
7518a238 996 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
0783fcfc
CM
997 btrfs_set_item_offset(right->items + i, push_space -
998 btrfs_item_size(right->items + i));
999 push_space = btrfs_item_offset(right->items + i);
be0e5c09 1000 }
eb60ceac 1001
02217ed2
CM
1002 BUG_ON(list_empty(&t->dirty));
1003 BUG_ON(list_empty(&right_buf->dirty));
eb60ceac 1004
e089f05c 1005 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
aa5d6bed
CM
1006 if (wret)
1007 ret = wret;
be0e5c09
CM
1008
1009 /* then fixup the leaf pointer in the path */
1010 if (path->slots[0] < push_items) {
1011 path->slots[0] += old_left_nritems;
234b63a0 1012 btrfs_block_release(root, path->nodes[0]);
eb60ceac 1013 path->nodes[0] = t;
be0e5c09
CM
1014 path->slots[1] -= 1;
1015 } else {
234b63a0 1016 btrfs_block_release(root, t);
be0e5c09
CM
1017 path->slots[0] -= push_items;
1018 }
eb60ceac 1019 BUG_ON(path->slots[0] < 0);
aa5d6bed 1020 return ret;
be0e5c09
CM
1021}
1022
74123bd7
CM
1023/*
1024 * split the path's leaf in two, making sure there is at least data_size
1025 * available for the resulting leaf level of the path.
aa5d6bed
CM
1026 *
1027 * returns 0 if all went well and < 0 on failure.
74123bd7 1028 */
e089f05c
CM
1029static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1030 *root, struct btrfs_path *path, int data_size)
be0e5c09 1031{
234b63a0
CM
1032 struct btrfs_buffer *l_buf;
1033 struct btrfs_leaf *l;
7518a238 1034 u32 nritems;
eb60ceac
CM
1035 int mid;
1036 int slot;
234b63a0
CM
1037 struct btrfs_leaf *right;
1038 struct btrfs_buffer *right_buffer;
0783fcfc 1039 int space_needed = data_size + sizeof(struct btrfs_item);
be0e5c09
CM
1040 int data_copy_size;
1041 int rt_data_off;
1042 int i;
1043 int ret;
aa5d6bed
CM
1044 int wret;
1045
40689478 1046 /* first try to make some room by pushing left and right */
e089f05c 1047 wret = push_leaf_left(trans, root, path, data_size);
eaee50e8
CM
1048 if (wret < 0)
1049 return wret;
1050 if (wret) {
e089f05c 1051 wret = push_leaf_right(trans, root, path, data_size);
eaee50e8
CM
1052 if (wret < 0)
1053 return wret;
1054 }
aa5d6bed
CM
1055 l_buf = path->nodes[0];
1056 l = &l_buf->leaf;
1057
1058 /* did the pushes work? */
123abc88
CM
1059 if (btrfs_leaf_free_space(root, l) >=
1060 sizeof(struct btrfs_item) + data_size)
aa5d6bed
CM
1061 return 0;
1062
5c680ed6 1063 if (!path->nodes[1]) {
e089f05c 1064 ret = insert_new_root(trans, root, path, 1);
5c680ed6
CM
1065 if (ret)
1066 return ret;
1067 }
eb60ceac 1068 slot = path->slots[0];
7518a238 1069 nritems = btrfs_header_nritems(&l->header);
eb60ceac 1070 mid = (nritems + 1)/ 2;
e089f05c 1071 right_buffer = btrfs_alloc_free_block(trans, root);
eb60ceac
CM
1072 BUG_ON(!right_buffer);
1073 BUG_ON(mid == nritems);
1074 right = &right_buffer->leaf;
123abc88 1075 memset(&right->header, 0, sizeof(right->header));
be0e5c09 1076 if (mid <= slot) {
97571fd0 1077 /* FIXME, just alloc a new leaf here */
be0e5c09 1078 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
123abc88 1079 BTRFS_LEAF_DATA_SIZE(root))
be0e5c09
CM
1080 BUG();
1081 } else {
97571fd0 1082 /* FIXME, just alloc a new leaf here */
be0e5c09 1083 if (leaf_space_used(l, 0, mid + 1) + space_needed >
123abc88 1084 BTRFS_LEAF_DATA_SIZE(root))
be0e5c09
CM
1085 BUG();
1086 }
7518a238
CM
1087 btrfs_set_header_nritems(&right->header, nritems - mid);
1088 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1089 btrfs_set_header_level(&right->header, 0);
1090 btrfs_set_header_parentid(&right->header,
1091 btrfs_header_parentid(&root->node->node.header));
123abc88
CM
1092 data_copy_size = btrfs_item_end(l->items + mid) -
1093 leaf_data_end(root, l);
be0e5c09 1094 memcpy(right->items, l->items + mid,
0783fcfc 1095 (nritems - mid) * sizeof(struct btrfs_item));
123abc88
CM
1096 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1097 data_copy_size, btrfs_leaf_data(l) +
1098 leaf_data_end(root, l), data_copy_size);
1099 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1100 btrfs_item_end(l->items + mid);
74123bd7 1101
0783fcfc 1102 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
123abc88 1103 u32 ioff = btrfs_item_offset(right->items + i);
0783fcfc
CM
1104 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1105 }
74123bd7 1106
7518a238 1107 btrfs_set_header_nritems(&l->header, mid);
aa5d6bed 1108 ret = 0;
e089f05c 1109 wret = insert_ptr(trans, root, path, &right->items[0].key,
5c680ed6 1110 right_buffer->blocknr, path->slots[1] + 1, 1);
aa5d6bed
CM
1111 if (wret)
1112 ret = wret;
02217ed2
CM
1113 BUG_ON(list_empty(&right_buffer->dirty));
1114 BUG_ON(list_empty(&l_buf->dirty));
eb60ceac 1115 BUG_ON(path->slots[0] != slot);
be0e5c09 1116 if (mid <= slot) {
234b63a0 1117 btrfs_block_release(root, path->nodes[0]);
eb60ceac 1118 path->nodes[0] = right_buffer;
be0e5c09
CM
1119 path->slots[0] -= mid;
1120 path->slots[1] += 1;
eb60ceac 1121 } else
234b63a0 1122 btrfs_block_release(root, right_buffer);
eb60ceac 1123 BUG_ON(path->slots[0] < 0);
be0e5c09
CM
1124 return ret;
1125}
1126
74123bd7
CM
1127/*
1128 * Given a key and some data, insert an item into the tree.
1129 * This does all the path init required, making room in the tree if needed.
1130 */
e089f05c
CM
1131int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1132 *root, struct btrfs_path *path, struct btrfs_key
1133 *cpu_key, u32 data_size)
be0e5c09 1134{
aa5d6bed 1135 int ret = 0;
be0e5c09 1136 int slot;
eb60ceac 1137 int slot_orig;
234b63a0
CM
1138 struct btrfs_leaf *leaf;
1139 struct btrfs_buffer *leaf_buf;
7518a238 1140 u32 nritems;
be0e5c09 1141 unsigned int data_end;
e2fa7227
CM
1142 struct btrfs_disk_key disk_key;
1143
1144 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
be0e5c09 1145
74123bd7 1146 /* create a root if there isn't one */
5c680ed6 1147 if (!root->node)
cfaa7295 1148 BUG();
e089f05c 1149 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
eb60ceac 1150 if (ret == 0) {
62e2749e 1151 btrfs_release_path(root, path);
f0930a37 1152 return -EEXIST;
aa5d6bed 1153 }
ed2ff2cb
CM
1154 if (ret < 0)
1155 goto out;
be0e5c09 1156
62e2749e
CM
1157 slot_orig = path->slots[0];
1158 leaf_buf = path->nodes[0];
eb60ceac 1159 leaf = &leaf_buf->leaf;
74123bd7 1160
7518a238 1161 nritems = btrfs_header_nritems(&leaf->header);
123abc88 1162 data_end = leaf_data_end(root, leaf);
eb60ceac 1163
123abc88 1164 if (btrfs_leaf_free_space(root, leaf) <
234b63a0 1165 sizeof(struct btrfs_item) + data_size)
be0e5c09
CM
1166 BUG();
1167
62e2749e 1168 slot = path->slots[0];
eb60ceac 1169 BUG_ON(slot < 0);
be0e5c09
CM
1170 if (slot != nritems) {
1171 int i;
0783fcfc 1172 unsigned int old_data = btrfs_item_end(leaf->items + slot);
be0e5c09
CM
1173
1174 /*
1175 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1176 */
1177 /* first correct the data pointers */
0783fcfc 1178 for (i = slot; i < nritems; i++) {
123abc88 1179 u32 ioff = btrfs_item_offset(leaf->items + i);
0783fcfc
CM
1180 btrfs_set_item_offset(leaf->items + i,
1181 ioff - data_size);
1182 }
be0e5c09
CM
1183
1184 /* shift the items */
1185 memmove(leaf->items + slot + 1, leaf->items + slot,
0783fcfc 1186 (nritems - slot) * sizeof(struct btrfs_item));
be0e5c09
CM
1187
1188 /* shift the data */
123abc88
CM
1189 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1190 btrfs_leaf_data(leaf) +
be0e5c09
CM
1191 data_end, old_data - data_end);
1192 data_end = old_data;
1193 }
62e2749e 1194 /* setup the item for the new data */
e2fa7227
CM
1195 memcpy(&leaf->items[slot].key, &disk_key,
1196 sizeof(struct btrfs_disk_key));
0783fcfc
CM
1197 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1198 btrfs_set_item_size(leaf->items + slot, data_size);
7518a238 1199 btrfs_set_header_nritems(&leaf->header, nritems + 1);
aa5d6bed
CM
1200
1201 ret = 0;
8e19f2cd 1202 if (slot == 0)
e089f05c 1203 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
aa5d6bed 1204
02217ed2 1205 BUG_ON(list_empty(&leaf_buf->dirty));
123abc88 1206 if (btrfs_leaf_free_space(root, leaf) < 0)
be0e5c09 1207 BUG();
62e2749e 1208 check_leaf(root, path, 0);
ed2ff2cb 1209out:
62e2749e
CM
1210 return ret;
1211}
1212
1213/*
1214 * Given a key and some data, insert an item into the tree.
1215 * This does all the path init required, making room in the tree if needed.
1216 */
e089f05c
CM
1217int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1218 *root, struct btrfs_key *cpu_key, void *data, u32
1219 data_size)
62e2749e
CM
1220{
1221 int ret = 0;
1222 struct btrfs_path path;
1223 u8 *ptr;
1224
1225 btrfs_init_path(&path);
e089f05c 1226 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
62e2749e
CM
1227 if (!ret) {
1228 ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
1229 memcpy(ptr, data, data_size);
1230 }
234b63a0 1231 btrfs_release_path(root, &path);
aa5d6bed 1232 return ret;
be0e5c09
CM
1233}
1234
74123bd7 1235/*
5de08d7d 1236 * delete the pointer from a given node.
74123bd7
CM
1237 *
1238 * If the delete empties a node, the node is removed from the tree,
1239 * continuing all the way the root if required. The root is converted into
1240 * a leaf if all the nodes are emptied.
1241 */
e089f05c
CM
1242static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1243 struct btrfs_path *path, int level, int slot)
be0e5c09 1244{
234b63a0
CM
1245 struct btrfs_node *node;
1246 struct btrfs_buffer *parent = path->nodes[level];
7518a238 1247 u32 nritems;
aa5d6bed 1248 int ret = 0;
bb803951 1249 int wret;
be0e5c09 1250
bb803951 1251 node = &parent->node;
7518a238 1252 nritems = btrfs_header_nritems(&node->header);
bb803951 1253 if (slot != nritems -1) {
123abc88
CM
1254 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1255 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
bb803951 1256 }
7518a238
CM
1257 nritems--;
1258 btrfs_set_header_nritems(&node->header, nritems);
1259 if (nritems == 0 && parent == root->node) {
1260 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
bb803951 1261 /* just turn the root into a leaf and break */
7518a238 1262 btrfs_set_header_level(&root->node->node.header, 0);
bb803951 1263 } else if (slot == 0) {
e089f05c 1264 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
123abc88 1265 level + 1);
0f70abe2
CM
1266 if (wret)
1267 ret = wret;
be0e5c09 1268 }
02217ed2 1269 BUG_ON(list_empty(&parent->dirty));
aa5d6bed 1270 return ret;
be0e5c09
CM
1271}
1272
74123bd7
CM
1273/*
1274 * delete the item at the leaf level in path. If that empties
1275 * the leaf, remove it from the tree
1276 */
e089f05c
CM
1277int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1278 struct btrfs_path *path)
be0e5c09 1279{
be0e5c09 1280 int slot;
234b63a0
CM
1281 struct btrfs_leaf *leaf;
1282 struct btrfs_buffer *leaf_buf;
be0e5c09
CM
1283 int doff;
1284 int dsize;
aa5d6bed
CM
1285 int ret = 0;
1286 int wret;
7518a238 1287 u32 nritems;
be0e5c09 1288
eb60ceac
CM
1289 leaf_buf = path->nodes[0];
1290 leaf = &leaf_buf->leaf;
4920c9ac 1291 slot = path->slots[0];
0783fcfc
CM
1292 doff = btrfs_item_offset(leaf->items + slot);
1293 dsize = btrfs_item_size(leaf->items + slot);
7518a238 1294 nritems = btrfs_header_nritems(&leaf->header);
be0e5c09 1295
7518a238 1296 if (slot != nritems - 1) {
be0e5c09 1297 int i;
123abc88
CM
1298 int data_end = leaf_data_end(root, leaf);
1299 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1300 btrfs_leaf_data(leaf) + data_end,
be0e5c09 1301 doff - data_end);
0783fcfc 1302 for (i = slot + 1; i < nritems; i++) {
123abc88 1303 u32 ioff = btrfs_item_offset(leaf->items + i);
0783fcfc
CM
1304 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1305 }
be0e5c09 1306 memmove(leaf->items + slot, leaf->items + slot + 1,
0783fcfc 1307 sizeof(struct btrfs_item) *
7518a238 1308 (nritems - slot - 1));
be0e5c09 1309 }
7518a238
CM
1310 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1311 nritems--;
74123bd7 1312 /* delete the leaf if we've emptied it */
7518a238 1313 if (nritems == 0) {
eb60ceac 1314 if (leaf_buf == root->node) {
7518a238 1315 btrfs_set_header_level(&leaf->header, 0);
02217ed2 1316 BUG_ON(list_empty(&leaf_buf->dirty));
9a8dd150 1317 } else {
e089f05c
CM
1318 clean_tree_block(trans, root, leaf_buf);
1319 wret = del_ptr(trans, root, path, 1, path->slots[1]);
aa5d6bed
CM
1320 if (wret)
1321 ret = wret;
e089f05c
CM
1322 wret = btrfs_free_extent(trans, root,
1323 leaf_buf->blocknr, 1, 1);
0f70abe2
CM
1324 if (wret)
1325 ret = wret;
9a8dd150 1326 }
be0e5c09 1327 } else {
7518a238 1328 int used = leaf_space_used(leaf, 0, nritems);
aa5d6bed 1329 if (slot == 0) {
e089f05c
CM
1330 wret = fixup_low_keys(trans, root, path,
1331 &leaf->items[0].key, 1);
aa5d6bed
CM
1332 if (wret)
1333 ret = wret;
1334 }
02217ed2 1335 BUG_ON(list_empty(&leaf_buf->dirty));
aa5d6bed 1336
74123bd7 1337 /* delete the leaf if it is mostly empty */
123abc88 1338 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
be0e5c09
CM
1339 /* push_leaf_left fixes the path.
1340 * make sure the path still points to our leaf
1341 * for possible call to del_ptr below
1342 */
4920c9ac 1343 slot = path->slots[1];
eb60ceac 1344 leaf_buf->count++;
e089f05c 1345 wret = push_leaf_left(trans, root, path, 1);
aa5d6bed
CM
1346 if (wret < 0)
1347 ret = wret;
f0930a37 1348 if (path->nodes[0] == leaf_buf &&
7518a238 1349 btrfs_header_nritems(&leaf->header)) {
e089f05c 1350 wret = push_leaf_right(trans, root, path, 1);
aa5d6bed
CM
1351 if (wret < 0)
1352 ret = wret;
1353 }
7518a238 1354 if (btrfs_header_nritems(&leaf->header) == 0) {
5de08d7d 1355 u64 blocknr = leaf_buf->blocknr;
e089f05c
CM
1356 clean_tree_block(trans, root, leaf_buf);
1357 wret = del_ptr(trans, root, path, 1, slot);
aa5d6bed
CM
1358 if (wret)
1359 ret = wret;
234b63a0 1360 btrfs_block_release(root, leaf_buf);
e089f05c
CM
1361 wret = btrfs_free_extent(trans, root, blocknr,
1362 1, 1);
0f70abe2
CM
1363 if (wret)
1364 ret = wret;
5de08d7d 1365 } else {
234b63a0 1366 btrfs_block_release(root, leaf_buf);
be0e5c09
CM
1367 }
1368 }
1369 }
aa5d6bed 1370 return ret;
be0e5c09
CM
1371}
1372
97571fd0
CM
1373/*
1374 * walk up the tree as far as required to find the next leaf.
0f70abe2
CM
1375 * returns 0 if it found something or 1 if there are no greater leaves.
1376 * returns < 0 on io errors.
97571fd0 1377 */
234b63a0 1378int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
d97e63b6
CM
1379{
1380 int slot;
1381 int level = 1;
1382 u64 blocknr;
234b63a0
CM
1383 struct btrfs_buffer *c;
1384 struct btrfs_buffer *next = NULL;
d97e63b6 1385
234b63a0 1386 while(level < BTRFS_MAX_LEVEL) {
d97e63b6 1387 if (!path->nodes[level])
0f70abe2 1388 return 1;
d97e63b6
CM
1389 slot = path->slots[level] + 1;
1390 c = path->nodes[level];
7518a238 1391 if (slot >= btrfs_header_nritems(&c->node.header)) {
d97e63b6
CM
1392 level++;
1393 continue;
1394 }
1d4f8a0c 1395 blocknr = btrfs_node_blockptr(&c->node, slot);
cfaa7295 1396 if (next)
234b63a0 1397 btrfs_block_release(root, next);
d97e63b6
CM
1398 next = read_tree_block(root, blocknr);
1399 break;
1400 }
1401 path->slots[level] = slot;
1402 while(1) {
1403 level--;
1404 c = path->nodes[level];
234b63a0 1405 btrfs_block_release(root, c);
d97e63b6
CM
1406 path->nodes[level] = next;
1407 path->slots[level] = 0;
1408 if (!level)
1409 break;
1d4f8a0c
CM
1410 next = read_tree_block(root,
1411 btrfs_node_blockptr(&next->node, 0));
d97e63b6
CM
1412 }
1413 return 0;
1414}