blob: 105556470055cac0cba206a0d989b322ebb05a4a [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 Masonaa5d6be2007-02-28 16:35:06 -05009static int split_node(struct ctree_root *root, struct ctree_path *path,
10 int level);
11static int split_leaf(struct ctree_root *root, struct ctree_path *path,
12 int data_size);
Chris Masonbb803952007-03-01 12:04:21 -050013static int push_node_left(struct ctree_root *root, struct tree_buffer *dst,
14 struct tree_buffer *src);
Chris Mason79f95c82007-03-01 15:16:26 -050015static int balance_node_right(struct ctree_root *root,
16 struct tree_buffer *dst_buf,
17 struct tree_buffer *src_buf);
Chris Masonbb803952007-03-01 12:04:21 -050018static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
19 int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050020
Chris Mason5de08d72007-02-24 06:24:44 -050021inline void init_path(struct ctree_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050022{
23 memset(p, 0, sizeof(*p));
24}
25
Chris Mason5de08d72007-02-24 06:24:44 -050026void release_path(struct ctree_root *root, struct ctree_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050027{
28 int i;
29 for (i = 0; i < MAX_LEVEL; i++) {
30 if (!p->nodes[i])
31 break;
32 tree_block_release(root, p->nodes[i]);
33 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050034 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050035}
36
Chris Mason02217ed2007-03-02 16:08:05 -050037int btrfs_cow_block(struct ctree_root *root,
38 struct tree_buffer *buf,
39 struct tree_buffer *parent,
40 int parent_slot,
41 struct tree_buffer **cow_ret)
42{
43 struct tree_buffer *cow;
44
45 if (!list_empty(&buf->dirty)) {
46 *cow_ret = buf;
47 return 0;
48 }
49 cow = alloc_free_block(root);
50 memcpy(&cow->node, &buf->node, sizeof(buf->node));
Chris Mason7518a232007-03-12 12:01:18 -040051 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
Chris Mason02217ed2007-03-02 16:08:05 -050052 *cow_ret = cow;
Chris Masona28ec192007-03-06 20:08:01 -050053 btrfs_inc_ref(root, buf);
Chris Mason02217ed2007-03-02 16:08:05 -050054 if (buf == root->node) {
55 root->node = cow;
56 cow->count++;
Chris Masona28ec192007-03-06 20:08:01 -050057 if (buf != root->commit_root)
58 free_extent(root, buf->blocknr, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050059 tree_block_release(root, buf);
60 } else {
61 parent->node.blockptrs[parent_slot] = cow->blocknr;
62 BUG_ON(list_empty(&parent->dirty));
Chris Masona28ec192007-03-06 20:08:01 -050063 free_extent(root, buf->blocknr, 1);
Chris Mason02217ed2007-03-02 16:08:05 -050064 }
65 tree_block_release(root, buf);
66 return 0;
67}
68
Chris Mason74123bd2007-02-02 11:05:29 -050069/*
70 * The leaf data grows from end-to-front in the node.
71 * this returns the address of the start of the last item,
72 * which is the stop of the leaf data stack
73 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050074static inline unsigned int leaf_data_end(struct leaf *leaf)
75{
Chris Mason7518a232007-03-12 12:01:18 -040076 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050077 if (nr == 0)
Chris Masond97e63b2007-02-20 16:40:44 -050078 return sizeof(leaf->data);
Chris Mason0783fcf2007-03-12 20:12:07 -040079 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -050080}
81
Chris Mason74123bd2007-02-02 11:05:29 -050082/*
83 * The space between the end of the leaf items and
84 * the start of the leaf data. IOW, how much room
85 * the leaf has left for both items and data
86 */
Chris Mason5de08d72007-02-24 06:24:44 -050087int leaf_free_space(struct leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050088{
89 int data_end = leaf_data_end(leaf);
Chris Mason7518a232007-03-12 12:01:18 -040090 int nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -050091 char *items_end = (char *)(leaf->items + nritems + 1);
92 return (char *)(leaf->data + data_end) - (char *)items_end;
93}
94
Chris Mason74123bd2007-02-02 11:05:29 -050095/*
96 * compare two keys in a memcmp fashion
97 */
Chris Masone2fa7222007-03-12 16:22:34 -040098int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -050099{
Chris Masone2fa7222007-03-12 16:22:34 -0400100 struct btrfs_key k1;
101
102 btrfs_disk_key_to_cpu(&k1, disk);
103
104 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500105 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400106 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.flags > k2->flags)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500109 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400110 if (k1.flags < k2->flags)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500111 return -1;
Chris Masone2fa7222007-03-12 16:22:34 -0400112 if (k1.offset > k2->offset)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500113 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400114 if (k1.offset < k2->offset)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500115 return -1;
116 return 0;
117}
Chris Mason74123bd2007-02-02 11:05:29 -0500118
Chris Masonaa5d6be2007-02-28 16:35:06 -0500119int check_node(struct ctree_path *path, int level)
120{
121 int i;
122 struct node *parent = NULL;
123 struct node *node = &path->nodes[level]->node;
124 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400125 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500126
127 if (path->nodes[level + 1])
128 parent = &path->nodes[level + 1]->node;
129 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400130 BUG_ON(nritems == 0);
131 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400132 struct btrfs_disk_key *parent_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500133 parent_key = &parent->keys[parent_slot];
Chris Masone2fa7222007-03-12 16:22:34 -0400134 BUG_ON(memcmp(parent_key, node->keys,
135 sizeof(struct btrfs_disk_key)));
Chris Mason7518a232007-03-12 12:01:18 -0400136 BUG_ON(parent->blockptrs[parent_slot] !=
137 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500138 }
Chris Mason7518a232007-03-12 12:01:18 -0400139 BUG_ON(nritems > NODEPTRS_PER_BLOCK);
140 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400141 struct btrfs_key cpukey;
142 btrfs_disk_key_to_cpu(&cpukey, &node->keys[i + 1]);
143 BUG_ON(comp_keys(&node->keys[i], &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500144 }
145 return 0;
146}
147
148int check_leaf(struct ctree_path *path, int level)
149{
150 int i;
151 struct leaf *leaf = &path->nodes[level]->leaf;
152 struct node *parent = NULL;
153 int parent_slot;
Chris Mason7518a232007-03-12 12:01:18 -0400154 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500155
156 if (path->nodes[level + 1])
157 parent = &path->nodes[level + 1]->node;
158 parent_slot = path->slots[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400159 BUG_ON(leaf_free_space(leaf) < 0);
160
161 if (nritems == 0)
162 return 0;
163
164 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400165 struct btrfs_disk_key *parent_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500166 parent_key = &parent->keys[parent_slot];
167 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400168 sizeof(struct btrfs_disk_key)));
Chris Mason7518a232007-03-12 12:01:18 -0400169 BUG_ON(parent->blockptrs[parent_slot] !=
170 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500171 }
Chris Mason7518a232007-03-12 12:01:18 -0400172 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
Chris Masone2fa7222007-03-12 16:22:34 -0400173 struct btrfs_key cpukey;
174 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500175 BUG_ON(comp_keys(&leaf->items[i].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400176 &cpukey) >= 0);
Chris Mason0783fcf2007-03-12 20:12:07 -0400177 BUG_ON(btrfs_item_offset(leaf->items + i) !=
178 btrfs_item_end(leaf->items + i + 1));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500179 if (i == 0) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400180 BUG_ON(btrfs_item_offset(leaf->items + i) +
181 btrfs_item_size(leaf->items + i) !=
182 LEAF_DATA_SIZE);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500183 }
184 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500185 return 0;
186}
187
188int check_block(struct ctree_path *path, int level)
189{
190 if (level == 0)
191 return check_leaf(path, level);
192 return check_node(path, level);
193}
194
Chris Mason74123bd2007-02-02 11:05:29 -0500195/*
196 * search for key in the array p. items p are item_size apart
197 * and there are 'max' items in p
198 * the slot in the array is returned via slot, and it points to
199 * the place where you would insert key if it is not found in
200 * the array.
201 *
202 * slot may point to max if the key is bigger than all of the keys
203 */
Chris Masone2fa7222007-03-12 16:22:34 -0400204int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500205 int max, int *slot)
206{
207 int low = 0;
208 int high = max;
209 int mid;
210 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400211 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500212
213 while(low < high) {
214 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400215 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500216 ret = comp_keys(tmp, key);
217
218 if (ret < 0)
219 low = mid + 1;
220 else if (ret > 0)
221 high = mid;
222 else {
223 *slot = mid;
224 return 0;
225 }
226 }
227 *slot = low;
228 return 1;
229}
230
Chris Mason97571fd2007-02-24 13:39:08 -0500231/*
232 * simple bin_search frontend that does the right thing for
233 * leaves vs nodes
234 */
Chris Masone2fa7222007-03-12 16:22:34 -0400235int bin_search(struct node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500236{
Chris Mason7518a232007-03-12 12:01:18 -0400237 if (btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500238 struct leaf *l = (struct leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400239 return generic_bin_search((void *)l->items,
240 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400241 key, btrfs_header_nritems(&c->header),
242 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500243 } else {
Chris Masone2fa7222007-03-12 16:22:34 -0400244 return generic_bin_search((void *)c->keys,
245 sizeof(struct btrfs_disk_key),
Chris Mason7518a232007-03-12 12:01:18 -0400246 key, btrfs_header_nritems(&c->header),
247 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500248 }
249 return -1;
250}
251
Chris Masonbb803952007-03-01 12:04:21 -0500252struct tree_buffer *read_node_slot(struct ctree_root *root,
253 struct tree_buffer *parent_buf,
254 int slot)
255{
256 struct node *node = &parent_buf->node;
257 if (slot < 0)
258 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400259 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500260 return NULL;
261 return read_tree_block(root, node->blockptrs[slot]);
262}
263
264static int balance_level(struct ctree_root *root, struct ctree_path *path,
265 int level)
266{
267 struct tree_buffer *right_buf;
268 struct tree_buffer *mid_buf;
269 struct tree_buffer *left_buf;
270 struct tree_buffer *parent_buf = NULL;
271 struct node *right = NULL;
272 struct node *mid;
273 struct node *left = NULL;
274 struct node *parent = NULL;
275 int ret = 0;
276 int wret;
277 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500278 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500279 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500280
281 if (level == 0)
282 return 0;
283
284 mid_buf = path->nodes[level];
285 mid = &mid_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500286 orig_ptr = mid->blockptrs[orig_slot];
287
Chris Masonbb803952007-03-01 12:04:21 -0500288 if (level < MAX_LEVEL - 1)
289 parent_buf = path->nodes[level + 1];
290 pslot = path->slots[level + 1];
291
292 if (!parent_buf) {
293 struct tree_buffer *child;
294 u64 blocknr = mid_buf->blocknr;
295
Chris Mason7518a232007-03-12 12:01:18 -0400296 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500297 return 0;
298
299 /* promote the child to a root */
300 child = read_node_slot(root, mid_buf, 0);
301 BUG_ON(!child);
302 root->node = child;
303 path->nodes[level] = NULL;
304 /* once for the path */
305 tree_block_release(root, mid_buf);
306 /* once for the root ptr */
307 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500308 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500309 return free_extent(root, blocknr, 1);
310 }
311 parent = &parent_buf->node;
312
Chris Mason7518a232007-03-12 12:01:18 -0400313 if (btrfs_header_nritems(&mid->header) > NODEPTRS_PER_BLOCK / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500314 return 0;
315
Chris Masonbb803952007-03-01 12:04:21 -0500316 left_buf = read_node_slot(root, parent_buf, pslot - 1);
317 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500318
319 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500320 if (left_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500321 btrfs_cow_block(root, left_buf, parent_buf,
322 pslot - 1, &left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500323 left = &left_buf->node;
Chris Mason7518a232007-03-12 12:01:18 -0400324 orig_slot += btrfs_header_nritems(&left->header);
Chris Mason79f95c82007-03-01 15:16:26 -0500325 wret = push_node_left(root, left_buf, mid_buf);
326 if (wret < 0)
327 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500328 }
Chris Mason79f95c82007-03-01 15:16:26 -0500329
330 /*
331 * then try to empty the right most buffer into the middle
332 */
Chris Masonbb803952007-03-01 12:04:21 -0500333 if (right_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500334 btrfs_cow_block(root, right_buf, parent_buf,
335 pslot + 1, &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500336 right = &right_buf->node;
337 wret = push_node_left(root, mid_buf, right_buf);
338 if (wret < 0)
339 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400340 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masonbb803952007-03-01 12:04:21 -0500341 u64 blocknr = right_buf->blocknr;
342 tree_block_release(root, right_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500343 clean_tree_block(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500344 right_buf = NULL;
345 right = NULL;
346 wret = del_ptr(root, path, level + 1, pslot + 1);
347 if (wret)
348 ret = wret;
349 wret = free_extent(root, blocknr, 1);
350 if (wret)
351 ret = wret;
352 } else {
353 memcpy(parent->keys + pslot + 1, right->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400354 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500355 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500356 }
357 }
Chris Mason7518a232007-03-12 12:01:18 -0400358 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500359 /*
360 * we're not allowed to leave a node with one item in the
361 * tree during a delete. A deletion from lower in the tree
362 * could try to delete the only pointer in this node.
363 * So, pull some keys from the left.
364 * There has to be a left pointer at this point because
365 * otherwise we would have pulled some pointers from the
366 * right
367 */
368 BUG_ON(!left_buf);
369 wret = balance_node_right(root, mid_buf, left_buf);
370 if (wret < 0)
371 ret = wret;
372 BUG_ON(wret == 1);
373 }
Chris Mason7518a232007-03-12 12:01:18 -0400374 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500375 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500376 u64 blocknr = mid_buf->blocknr;
377 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500378 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500379 mid_buf = NULL;
380 mid = NULL;
381 wret = del_ptr(root, path, level + 1, pslot);
382 if (wret)
383 ret = wret;
384 wret = free_extent(root, blocknr, 1);
385 if (wret)
386 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500387 } else {
388 /* update the parent key to reflect our changes */
Chris Masone2fa7222007-03-12 16:22:34 -0400389 memcpy(parent->keys + pslot, mid->keys,
390 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500391 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500392 }
Chris Masonbb803952007-03-01 12:04:21 -0500393
Chris Mason79f95c82007-03-01 15:16:26 -0500394 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500395 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400396 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500397 left_buf->count++; // released below
398 path->nodes[level] = left_buf;
399 path->slots[level + 1] -= 1;
400 path->slots[level] = orig_slot;
401 if (mid_buf)
402 tree_block_release(root, mid_buf);
403 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400404 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500405 path->slots[level] = orig_slot;
406 }
407 }
Chris Mason79f95c82007-03-01 15:16:26 -0500408 /* double check we haven't messed things up */
409 check_block(path, level);
410 if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]])
411 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500412
413 if (right_buf)
414 tree_block_release(root, right_buf);
415 if (left_buf)
416 tree_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500417 return ret;
418}
419
Chris Mason74123bd2007-02-02 11:05:29 -0500420/*
421 * look for key in the tree. path is filled in with nodes along the way
422 * if key is found, we return zero and you can find the item in the leaf
423 * level of the path (level 0)
424 *
425 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500426 * be inserted, and 1 is returned. If there are other errors during the
427 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500428 *
429 * if ins_len > 0, nodes and leaves will be split as we walk down the
430 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
431 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500432 */
Chris Masone2fa7222007-03-12 16:22:34 -0400433int search_slot(struct ctree_root *root, struct btrfs_key *key,
Chris Mason02217ed2007-03-02 16:08:05 -0500434 struct ctree_path *p, int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500435{
Chris Masonbb803952007-03-01 12:04:21 -0500436 struct tree_buffer *b;
Chris Mason02217ed2007-03-02 16:08:05 -0500437 struct tree_buffer *cow_buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500438 struct node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500439 int slot;
440 int ret;
441 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500442
Chris Masonbb803952007-03-01 12:04:21 -0500443again:
444 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500445 b->count++;
446 while (b) {
Chris Mason7518a232007-03-12 12:01:18 -0400447 level = btrfs_header_level(&b->node.header);
Chris Mason02217ed2007-03-02 16:08:05 -0500448 if (cow) {
449 int wret;
450 wret = btrfs_cow_block(root, b, p->nodes[level + 1],
451 p->slots[level + 1], &cow_buf);
452 b = cow_buf;
453 }
454 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500455 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500456 p->nodes[level] = b;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500457 ret = check_block(p, level);
458 if (ret)
459 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500460 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400461 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500462 if (ret && slot > 0)
463 slot -= 1;
464 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400465 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
466 NODEPTRS_PER_BLOCK) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500467 int sret = split_node(root, p, level);
468 BUG_ON(sret > 0);
469 if (sret)
470 return sret;
471 b = p->nodes[level];
472 c = &b->node;
473 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500474 } else if (ins_len < 0) {
475 int sret = balance_level(root, p, level);
476 if (sret)
477 return sret;
478 b = p->nodes[level];
479 if (!b)
480 goto again;
481 c = &b->node;
482 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400483 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500484 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500485 b = read_tree_block(root, c->blockptrs[slot]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500486 } else {
Chris Mason5c680ed2007-02-22 11:39:13 -0500487 struct leaf *l = (struct leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500488 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500489 if (ins_len > 0 && leaf_free_space(l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400490 sizeof(struct btrfs_item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500491 int sret = split_leaf(root, p, ins_len);
492 BUG_ON(sret > 0);
493 if (sret)
494 return sret;
495 }
Chris Masonbb803952007-03-01 12:04:21 -0500496 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500497 return ret;
498 }
499 }
Chris Masonbb803952007-03-01 12:04:21 -0500500 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500501 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500502}
503
Chris Mason74123bd2007-02-02 11:05:29 -0500504/*
505 * adjust the pointers going up the tree, starting at level
506 * making sure the right key of each node is points to 'key'.
507 * This is used after shifting pointers to the left, so it stops
508 * fixing up pointers when a given leaf/node is not in slot 0 of the
509 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500510 *
511 * If this fails to write a tree block, it returns -1, but continues
512 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500513 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500514static int fixup_low_keys(struct ctree_root *root,
Chris Masone2fa7222007-03-12 16:22:34 -0400515 struct ctree_path *path, struct btrfs_disk_key *key,
Chris Masoneb60cea2007-02-02 09:18:22 -0500516 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500517{
518 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500519 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500520 for (i = level; i < MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500521 struct node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500522 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500523 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500524 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500525 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500526 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500527 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500528 if (tslot != 0)
529 break;
530 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500531 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500532}
533
Chris Mason74123bd2007-02-02 11:05:29 -0500534/*
535 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500536 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500537 *
538 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
539 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500540 */
Chris Masonbb803952007-03-01 12:04:21 -0500541static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
542 struct tree_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500543{
Chris Masonbb803952007-03-01 12:04:21 -0500544 struct node *src = &src_buf->node;
545 struct node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500546 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500547 int src_nritems;
548 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500549 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500550
Chris Mason7518a232007-03-12 12:01:18 -0400551 src_nritems = btrfs_header_nritems(&src->header);
552 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Masonbb803952007-03-01 12:04:21 -0500553 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500554 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500555 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500556 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500557
558 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500559 push_items = src_nritems;
560
Chris Masonbb803952007-03-01 12:04:21 -0500561 memcpy(dst->keys + dst_nritems, src->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400562 push_items * sizeof(struct btrfs_disk_key));
Chris Masonbb803952007-03-01 12:04:21 -0500563 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500564 push_items * sizeof(u64));
Chris Masonbb803952007-03-01 12:04:21 -0500565 if (push_items < src_nritems) {
566 memmove(src->keys, src->keys + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400567 (src_nritems - push_items) *
568 sizeof(struct btrfs_disk_key));
Chris Masonbb803952007-03-01 12:04:21 -0500569 memmove(src->blockptrs, src->blockptrs + push_items,
570 (src_nritems - push_items) * sizeof(u64));
571 }
Chris Mason7518a232007-03-12 12:01:18 -0400572 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
573 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason02217ed2007-03-02 16:08:05 -0500574 BUG_ON(list_empty(&src_buf->dirty));
575 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500576 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500577}
578
Chris Mason97571fd2007-02-24 13:39:08 -0500579/*
Chris Mason79f95c82007-03-01 15:16:26 -0500580 * try to push data from one node into the next node right in the
581 * tree.
582 *
583 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
584 * error, and > 0 if there was no room in the right hand block.
585 *
586 * this will only push up to 1/2 the contents of the left node over
587 */
588static int balance_node_right(struct ctree_root *root,
589 struct tree_buffer *dst_buf,
590 struct tree_buffer *src_buf)
591{
592 struct node *src = &src_buf->node;
593 struct node *dst = &dst_buf->node;
594 int push_items = 0;
595 int max_push;
596 int src_nritems;
597 int dst_nritems;
598 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500599
Chris Mason7518a232007-03-12 12:01:18 -0400600 src_nritems = btrfs_header_nritems(&src->header);
601 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason79f95c82007-03-01 15:16:26 -0500602 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
603 if (push_items <= 0) {
604 return 1;
605 }
606
607 max_push = src_nritems / 2 + 1;
608 /* don't try to empty the node */
609 if (max_push > src_nritems)
610 return 1;
611 if (max_push < push_items)
612 push_items = max_push;
613
614 memmove(dst->keys + push_items, dst->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400615 dst_nritems * sizeof(struct btrfs_disk_key));
Chris Mason79f95c82007-03-01 15:16:26 -0500616 memmove(dst->blockptrs + push_items, dst->blockptrs,
617 dst_nritems * sizeof(u64));
618 memcpy(dst->keys, src->keys + src_nritems - push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400619 push_items * sizeof(struct btrfs_disk_key));
Chris Mason79f95c82007-03-01 15:16:26 -0500620 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
621 push_items * sizeof(u64));
622
Chris Mason7518a232007-03-12 12:01:18 -0400623 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
624 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500625
Chris Mason02217ed2007-03-02 16:08:05 -0500626 BUG_ON(list_empty(&src_buf->dirty));
627 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500628 return ret;
629}
630
631/*
Chris Mason97571fd2007-02-24 13:39:08 -0500632 * helper function to insert a new root level in the tree.
633 * A new node is allocated, and a single item is inserted to
634 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500635 *
636 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500637 */
Chris Mason5de08d72007-02-24 06:24:44 -0500638static int insert_new_root(struct ctree_root *root,
639 struct ctree_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500640{
641 struct tree_buffer *t;
642 struct node *lower;
643 struct node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400644 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500645
646 BUG_ON(path->nodes[level]);
647 BUG_ON(path->nodes[level-1] != root->node);
648
649 t = alloc_free_block(root);
650 c = &t->node;
651 memset(c, 0, sizeof(c));
Chris Mason7518a232007-03-12 12:01:18 -0400652 btrfs_set_header_nritems(&c->header, 1);
653 btrfs_set_header_level(&c->header, level);
654 btrfs_set_header_blocknr(&c->header, t->blocknr);
655 btrfs_set_header_parentid(&c->header,
656 btrfs_header_parentid(&root->node->node.header));
Chris Mason5c680ed2007-02-22 11:39:13 -0500657 lower = &path->nodes[level-1]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400658 if (btrfs_is_leaf(lower))
Chris Mason5c680ed2007-02-22 11:39:13 -0500659 lower_key = &((struct leaf *)lower)->items[0].key;
660 else
661 lower_key = lower->keys;
Chris Masone2fa7222007-03-12 16:22:34 -0400662 memcpy(c->keys, lower_key, sizeof(struct btrfs_disk_key));
Chris Mason5c680ed2007-02-22 11:39:13 -0500663 c->blockptrs[0] = path->nodes[level-1]->blocknr;
664 /* the super has an extra ref to root->node */
665 tree_block_release(root, root->node);
666 root->node = t;
667 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500668 path->nodes[level] = t;
669 path->slots[level] = 0;
670 return 0;
671}
672
Chris Mason74123bd2007-02-02 11:05:29 -0500673/*
674 * worker function to insert a single pointer in a node.
675 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500676 *
Chris Mason74123bd2007-02-02 11:05:29 -0500677 * slot and level indicate where you want the key to go, and
678 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500679 *
680 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500681 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500682static int insert_ptr(struct ctree_root *root,
Chris Masone2fa7222007-03-12 16:22:34 -0400683 struct ctree_path *path, struct btrfs_disk_key *key,
Chris Mason74123bd2007-02-02 11:05:29 -0500684 u64 blocknr, int slot, int level)
685{
Chris Mason74123bd2007-02-02 11:05:29 -0500686 struct node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500687 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500688
689 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500690 lower = &path->nodes[level]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400691 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500692 if (slot > nritems)
693 BUG();
694 if (nritems == NODEPTRS_PER_BLOCK)
695 BUG();
696 if (slot != nritems) {
697 memmove(lower->keys + slot + 1, lower->keys + slot,
Chris Masone2fa7222007-03-12 16:22:34 -0400698 (nritems - slot) * sizeof(struct btrfs_disk_key));
Chris Mason74123bd2007-02-02 11:05:29 -0500699 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
700 (nritems - slot) * sizeof(u64));
701 }
Chris Masone2fa7222007-03-12 16:22:34 -0400702 memcpy(lower->keys + slot, key, sizeof(struct btrfs_disk_key));
Chris Mason74123bd2007-02-02 11:05:29 -0500703 lower->blockptrs[slot] = blocknr;
Chris Mason7518a232007-03-12 12:01:18 -0400704 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Mason74123bd2007-02-02 11:05:29 -0500705 if (lower->keys[1].objectid == 0)
706 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -0500707 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500708 return 0;
709}
710
Chris Mason97571fd2007-02-24 13:39:08 -0500711/*
712 * split the node at the specified level in path in two.
713 * The path is corrected to point to the appropriate node after the split
714 *
715 * Before splitting this tries to make some room in the node by pushing
716 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500717 *
718 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500719 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500720static int split_node(struct ctree_root *root, struct ctree_path *path,
721 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500722{
Chris Mason5c680ed2007-02-22 11:39:13 -0500723 struct tree_buffer *t;
724 struct node *c;
725 struct tree_buffer *split_buffer;
726 struct node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500728 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500729 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400730 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731
Chris Mason5c680ed2007-02-22 11:39:13 -0500732 t = path->nodes[level];
733 c = &t->node;
734 if (t == root->node) {
735 /* trying to split the root, lets make a new one */
736 ret = insert_new_root(root, path, level + 1);
737 if (ret)
738 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500739 }
Chris Mason7518a232007-03-12 12:01:18 -0400740 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason5c680ed2007-02-22 11:39:13 -0500741 split_buffer = alloc_free_block(root);
742 split = &split_buffer->node;
Chris Mason7518a232007-03-12 12:01:18 -0400743 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
744 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
745 btrfs_set_header_parentid(&split->header,
746 btrfs_header_parentid(&root->node->node.header));
747 mid = (c_nritems + 1) / 2;
Chris Mason5c680ed2007-02-22 11:39:13 -0500748 memcpy(split->keys, c->keys + mid,
Chris Masone2fa7222007-03-12 16:22:34 -0400749 (c_nritems - mid) * sizeof(struct btrfs_disk_key));
Chris Mason5c680ed2007-02-22 11:39:13 -0500750 memcpy(split->blockptrs, c->blockptrs + mid,
Chris Mason7518a232007-03-12 12:01:18 -0400751 (c_nritems - mid) * sizeof(u64));
752 btrfs_set_header_nritems(&split->header, c_nritems - mid);
753 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500754 ret = 0;
755
Chris Mason02217ed2007-03-02 16:08:05 -0500756 BUG_ON(list_empty(&t->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500757 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
758 path->slots[level + 1] + 1, level + 1);
759 if (wret)
760 ret = wret;
761
Chris Mason5de08d72007-02-24 06:24:44 -0500762 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500763 path->slots[level] -= mid;
764 tree_block_release(root, t);
765 path->nodes[level] = split_buffer;
766 path->slots[level + 1] += 1;
767 } else {
768 tree_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500769 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500770 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500771}
772
Chris Mason74123bd2007-02-02 11:05:29 -0500773/*
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 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500778static int leaf_space_used(struct leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500779{
780 int data_len;
781 int end = start + nr - 1;
782
783 if (!nr)
784 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400785 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;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500788 return data_len;
789}
790
Chris Mason74123bd2007-02-02 11:05:29 -0500791/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500792 * 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
Chris Masonaa5d6be2007-02-28 16:35:06 -0500794 *
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.
Chris Mason00ec4c52007-02-24 12:47:20 -0500797 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500798static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
799 int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500800{
801 struct tree_buffer *left_buf = path->nodes[0];
802 struct leaf *left = &left_buf->leaf;
803 struct leaf *right;
804 struct tree_buffer *right_buf;
805 struct tree_buffer *upper;
806 int slot;
807 int i;
808 int free_space;
809 int push_space = 0;
810 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400811 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400812 u32 left_nritems;
813 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500814
815 slot = path->slots[1];
816 if (!path->nodes[1]) {
817 return 1;
818 }
819 upper = path->nodes[1];
Chris Mason7518a232007-03-12 12:01:18 -0400820 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500821 return 1;
822 }
823 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
824 right = &right_buf->leaf;
825 free_space = leaf_free_space(right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400826 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500827 tree_block_release(root, right_buf);
828 return 1;
829 }
Chris Mason02217ed2007-03-02 16:08:05 -0500830 /* cow and double check */
831 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
832 right = &right_buf->leaf;
833 free_space = leaf_free_space(right);
Chris Mason0783fcf2007-03-12 20:12:07 -0400834 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500835 tree_block_release(root, right_buf);
836 return 1;
837 }
838
Chris Mason7518a232007-03-12 12:01:18 -0400839 left_nritems = btrfs_header_nritems(&left->header);
840 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500841 item = left->items + i;
842 if (path->slots[0] == i)
843 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400844 if (btrfs_item_size(item) + sizeof(*item) + push_space >
845 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -0500846 break;
847 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400848 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -0500849 }
850 if (push_items == 0) {
851 tree_block_release(root, right_buf);
852 return 1;
853 }
Chris Mason7518a232007-03-12 12:01:18 -0400854 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500855 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -0400856 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason00ec4c52007-02-24 12:47:20 -0500857 push_space -= leaf_data_end(left);
858 /* make room in the right data area */
859 memmove(right->data + leaf_data_end(right) - push_space,
860 right->data + leaf_data_end(right),
861 LEAF_DATA_SIZE - leaf_data_end(right));
862 /* copy from the left data area */
863 memcpy(right->data + LEAF_DATA_SIZE - push_space,
864 left->data + leaf_data_end(left),
865 push_space);
866 memmove(right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400867 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500868 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400869 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason0783fcf2007-03-12 20:12:07 -0400870 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500871
872 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400873 right_nritems += push_items;
874 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500875 push_space = LEAF_DATA_SIZE;
Chris Mason7518a232007-03-12 12:01:18 -0400876 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400877 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);
Chris Mason00ec4c52007-02-24 12:47:20 -0500880 }
Chris Mason7518a232007-03-12 12:01:18 -0400881 left_nritems -= push_items;
882 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500883
Chris Mason02217ed2007-03-02 16:08:05 -0500884 BUG_ON(list_empty(&left_buf->dirty));
885 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason00ec4c52007-02-24 12:47:20 -0500886 memcpy(upper->node.keys + slot + 1,
Chris Masone2fa7222007-03-12 16:22:34 -0400887 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500888 BUG_ON(list_empty(&upper->dirty));
889
Chris Mason00ec4c52007-02-24 12:47:20 -0500890 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400891 if (path->slots[0] >= left_nritems) {
892 path->slots[0] -= left_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500893 tree_block_release(root, path->nodes[0]);
894 path->nodes[0] = right_buf;
895 path->slots[1] += 1;
896 } else {
897 tree_block_release(root, right_buf);
898 }
899 return 0;
900}
901/*
Chris Mason74123bd2007-02-02 11:05:29 -0500902 * 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 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500905static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
906 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500907{
Chris Masoneb60cea2007-02-02 09:18:22 -0500908 struct tree_buffer *right_buf = path->nodes[0];
909 struct leaf *right = &right_buf->leaf;
910 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500911 struct leaf *left;
912 int slot;
913 int i;
914 int free_space;
915 int push_space = 0;
916 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -0400917 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400918 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500919 int ret = 0;
920 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500921
922 slot = path->slots[1];
923 if (slot == 0) {
924 return 1;
925 }
926 if (!path->nodes[1]) {
927 return 1;
928 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500929 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
930 left = &t->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500931 free_space = leaf_free_space(left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400932 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500933 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500934 return 1;
935 }
Chris Mason02217ed2007-03-02 16:08:05 -0500936
937 /* cow and double check */
938 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
939 left = &t->leaf;
940 free_space = leaf_free_space(left);
Chris Mason0783fcf2007-03-12 20:12:07 -0400941 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500942 tree_block_release(root, t);
943 return 1;
944 }
945
Chris Mason7518a232007-03-12 12:01:18 -0400946 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500947 item = right->items + i;
948 if (path->slots[0] == i)
949 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -0400950 if (btrfs_item_size(item) + sizeof(*item) + push_space >
951 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500952 break;
953 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -0400954 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500955 }
956 if (push_items == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500957 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500958 return 1;
959 }
960 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400961 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Mason0783fcf2007-03-12 20:12:07 -0400962 right->items, push_items * sizeof(struct btrfs_item));
963 push_space = LEAF_DATA_SIZE -
964 btrfs_item_offset(right->items + push_items -1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500965 memcpy(left->data + leaf_data_end(left) - push_space,
Chris Mason0783fcf2007-03-12 20:12:07 -0400966 right->data + btrfs_item_offset(right->items + push_items - 1),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500967 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400968 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500969 BUG_ON(old_left_nritems < 0);
970
Chris Mason0783fcf2007-03-12 20:12:07 -0400971 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
972 u16 ioff = btrfs_item_offset(left->items + i);
973 btrfs_set_item_offset(left->items + i, ioff - (LEAF_DATA_SIZE -
974 btrfs_item_offset(left->items +
975 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500976 }
Chris Mason7518a232007-03-12 12:01:18 -0400977 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500978
979 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -0400980 push_space = btrfs_item_offset(right->items + push_items - 1) -
981 leaf_data_end(right);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500982 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
983 leaf_data_end(right), push_space);
984 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400985 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -0400986 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -0400987 btrfs_set_header_nritems(&right->header,
988 btrfs_header_nritems(&right->header) -
989 push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500990 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500991
Chris Mason7518a232007-03-12 12:01:18 -0400992 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -0400993 btrfs_set_item_offset(right->items + i, push_space -
994 btrfs_item_size(right->items + i));
995 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500996 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500997
Chris Mason02217ed2007-03-02 16:08:05 -0500998 BUG_ON(list_empty(&t->dirty));
999 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001000
Chris Masonaa5d6be2007-02-28 16:35:06 -05001001 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
1002 if (wret)
1003 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001004
1005 /* then fixup the leaf pointer in the path */
1006 if (path->slots[0] < push_items) {
1007 path->slots[0] += old_left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001008 tree_block_release(root, path->nodes[0]);
1009 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001010 path->slots[1] -= 1;
1011 } else {
Chris Masoneb60cea2007-02-02 09:18:22 -05001012 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001013 path->slots[0] -= push_items;
1014 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001015 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001016 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001017}
1018
Chris Mason74123bd2007-02-02 11:05:29 -05001019/*
1020 * split the path's leaf in two, making sure there is at least data_size
1021 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001022 *
1023 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001024 */
Chris Masonaa5d6be2007-02-28 16:35:06 -05001025static int split_leaf(struct ctree_root *root, struct ctree_path *path,
1026 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001027{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001028 struct tree_buffer *l_buf;
1029 struct leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001030 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001031 int mid;
1032 int slot;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001033 struct leaf *right;
Chris Masoneb60cea2007-02-02 09:18:22 -05001034 struct tree_buffer *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001035 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001036 int data_copy_size;
1037 int rt_data_off;
1038 int i;
1039 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001040 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001041
Chris Masonaa5d6be2007-02-28 16:35:06 -05001042 l_buf = path->nodes[0];
1043 l = &l_buf->leaf;
1044
1045 /* did the pushes work? */
Chris Mason0783fcf2007-03-12 20:12:07 -04001046 if (leaf_free_space(l) >= sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001047 return 0;
1048
Chris Mason5c680ed2007-02-22 11:39:13 -05001049 if (!path->nodes[1]) {
1050 ret = insert_new_root(root, path, 1);
1051 if (ret)
1052 return ret;
1053 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001054 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001055 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001056 mid = (nritems + 1)/ 2;
Chris Masoneb60cea2007-02-02 09:18:22 -05001057 right_buffer = alloc_free_block(root);
1058 BUG_ON(!right_buffer);
1059 BUG_ON(mid == nritems);
1060 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001061 memset(right, 0, sizeof(*right));
1062 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001063 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001064 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1065 LEAF_DATA_SIZE)
1066 BUG();
1067 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001068 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001069 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1070 LEAF_DATA_SIZE)
1071 BUG();
1072 }
Chris Mason7518a232007-03-12 12:01:18 -04001073 btrfs_set_header_nritems(&right->header, nritems - mid);
1074 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1075 btrfs_set_header_level(&right->header, 0);
1076 btrfs_set_header_parentid(&right->header,
1077 btrfs_header_parentid(&root->node->node.header));
Chris Mason0783fcf2007-03-12 20:12:07 -04001078 data_copy_size = btrfs_item_end(l->items + mid) - leaf_data_end(l);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001079 memcpy(right->items, l->items + mid,
Chris Mason0783fcf2007-03-12 20:12:07 -04001080 (nritems - mid) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001081 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1082 l->data + leaf_data_end(l), data_copy_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04001083 rt_data_off = LEAF_DATA_SIZE - btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001084
Chris Mason0783fcf2007-03-12 20:12:07 -04001085 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1086 u16 ioff = btrfs_item_offset(right->items + i);
1087 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1088 }
Chris Mason74123bd2007-02-02 11:05:29 -05001089
Chris Mason7518a232007-03-12 12:01:18 -04001090 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001091 ret = 0;
1092 wret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001093 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001094 if (wret)
1095 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001096 BUG_ON(list_empty(&right_buffer->dirty));
1097 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001098 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099 if (mid <= slot) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001100 tree_block_release(root, path->nodes[0]);
1101 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001102 path->slots[0] -= mid;
1103 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001104 } else
1105 tree_block_release(root, right_buffer);
1106 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001107 return ret;
1108}
1109
Chris Mason74123bd2007-02-02 11:05:29 -05001110/*
1111 * Given a key and some data, insert an item into the tree.
1112 * This does all the path init required, making room in the tree if needed.
1113 */
Chris Masone2fa7222007-03-12 16:22:34 -04001114int insert_item(struct ctree_root *root, struct btrfs_key *cpu_key,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001115 void *data, int data_size)
1116{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001117 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001118 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001119 int slot_orig;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001120 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001121 struct tree_buffer *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001122 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001123 unsigned int data_end;
1124 struct ctree_path path;
Chris Masone2fa7222007-03-12 16:22:34 -04001125 struct btrfs_disk_key disk_key;
1126
1127 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001128
Chris Mason74123bd2007-02-02 11:05:29 -05001129 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001130 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001131 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -05001132 init_path(&path);
Chris Masone2fa7222007-03-12 16:22:34 -04001133 ret = search_slot(root, cpu_key, &path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001134 if (ret == 0) {
1135 release_path(root, &path);
Chris Masonf0930a32007-03-02 09:47:58 -05001136 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001137 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001138 if (ret < 0)
1139 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001140
Chris Masoneb60cea2007-02-02 09:18:22 -05001141 slot_orig = path.slots[0];
1142 leaf_buf = path.nodes[0];
1143 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001144
Chris Mason7518a232007-03-12 12:01:18 -04001145 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001146 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001147
Chris Mason0783fcf2007-03-12 20:12:07 -04001148 if (leaf_free_space(leaf) < sizeof(struct btrfs_item) + data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001149 BUG();
1150
1151 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001152 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001153 if (slot != nritems) {
1154 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001155 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001156
1157 /*
1158 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1159 */
1160 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001161 for (i = slot; i < nritems; i++) {
1162 u16 ioff = btrfs_item_offset(leaf->items + i);
1163 btrfs_set_item_offset(leaf->items + i,
1164 ioff - data_size);
1165 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001166
1167 /* shift the items */
1168 memmove(leaf->items + slot + 1, leaf->items + slot,
Chris Mason0783fcf2007-03-12 20:12:07 -04001169 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001170
1171 /* shift the data */
1172 memmove(leaf->data + data_end - data_size, leaf->data +
1173 data_end, old_data - data_end);
1174 data_end = old_data;
1175 }
Chris Mason74123bd2007-02-02 11:05:29 -05001176 /* copy the new data in */
Chris Masone2fa7222007-03-12 16:22:34 -04001177 memcpy(&leaf->items[slot].key, &disk_key,
1178 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001179 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1180 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001181 memcpy(leaf->data + data_end - data_size, data, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001182 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001183
1184 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001185 if (slot == 0)
Chris Masone2fa7222007-03-12 16:22:34 -04001186 ret = fixup_low_keys(root, &path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001187
Chris Mason02217ed2007-03-02 16:08:05 -05001188 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001189 if (leaf_free_space(leaf) < 0)
1190 BUG();
Chris Masonbb803952007-03-01 12:04:21 -05001191 check_leaf(&path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001192out:
Chris Masoneb60cea2007-02-02 09:18:22 -05001193 release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001194 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001195}
1196
Chris Mason74123bd2007-02-02 11:05:29 -05001197/*
Chris Mason5de08d72007-02-24 06:24:44 -05001198 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001199 *
1200 * If the delete empties a node, the node is removed from the tree,
1201 * continuing all the way the root if required. The root is converted into
1202 * a leaf if all the nodes are emptied.
1203 */
Chris Masonbb803952007-03-01 12:04:21 -05001204static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
1205 int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001206{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001207 struct node *node;
Chris Masonbb803952007-03-01 12:04:21 -05001208 struct tree_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001209 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001210 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001211 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001212
Chris Masonbb803952007-03-01 12:04:21 -05001213 node = &parent->node;
Chris Mason7518a232007-03-12 12:01:18 -04001214 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001215 if (slot != nritems -1) {
1216 memmove(node->keys + slot, node->keys + slot + 1,
Chris Masone2fa7222007-03-12 16:22:34 -04001217 sizeof(struct btrfs_disk_key) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001218 memmove(node->blockptrs + slot,
1219 node->blockptrs + slot + 1,
1220 sizeof(u64) * (nritems - slot - 1));
1221 }
Chris Mason7518a232007-03-12 12:01:18 -04001222 nritems--;
1223 btrfs_set_header_nritems(&node->header, nritems);
1224 if (nritems == 0 && parent == root->node) {
1225 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001226 /* just turn the root into a leaf and break */
Chris Mason7518a232007-03-12 12:01:18 -04001227 btrfs_set_header_level(&root->node->node.header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001228 } else if (slot == 0) {
1229 wret = fixup_low_keys(root, path, node->keys, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001230 if (wret)
1231 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001232 }
Chris Mason02217ed2007-03-02 16:08:05 -05001233 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001234 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001235}
1236
Chris Mason74123bd2007-02-02 11:05:29 -05001237/*
1238 * delete the item at the leaf level in path. If that empties
1239 * the leaf, remove it from the tree
1240 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001241int del_item(struct ctree_root *root, struct ctree_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001242{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001243 int slot;
1244 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001245 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001246 int doff;
1247 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001248 int ret = 0;
1249 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001250 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001251
Chris Masoneb60cea2007-02-02 09:18:22 -05001252 leaf_buf = path->nodes[0];
1253 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001254 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001255 doff = btrfs_item_offset(leaf->items + slot);
1256 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001257 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001258
Chris Mason7518a232007-03-12 12:01:18 -04001259 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001260 int i;
1261 int data_end = leaf_data_end(leaf);
1262 memmove(leaf->data + data_end + dsize,
1263 leaf->data + data_end,
1264 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001265 for (i = slot + 1; i < nritems; i++) {
1266 u16 ioff = btrfs_item_offset(leaf->items + i);
1267 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1268 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001269 memmove(leaf->items + slot, leaf->items + slot + 1,
Chris Mason0783fcf2007-03-12 20:12:07 -04001270 sizeof(struct btrfs_item) *
Chris Mason7518a232007-03-12 12:01:18 -04001271 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001272 }
Chris Mason7518a232007-03-12 12:01:18 -04001273 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1274 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001275 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001276 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001277 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001278 btrfs_set_header_level(&leaf->header, 0);
Chris Mason02217ed2007-03-02 16:08:05 -05001279 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001280 } else {
Chris Masoned2ff2c2007-03-01 18:59:40 -05001281 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001282 wret = del_ptr(root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001283 if (wret)
1284 ret = wret;
Chris Mason0f70abe2007-02-28 16:46:22 -05001285 wret = free_extent(root, leaf_buf->blocknr, 1);
1286 if (wret)
1287 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001288 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001289 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001290 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001291 if (slot == 0) {
1292 wret = fixup_low_keys(root, path,
1293 &leaf->items[0].key, 1);
1294 if (wret)
1295 ret = wret;
1296 }
Chris Mason02217ed2007-03-02 16:08:05 -05001297 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001298
Chris Mason74123bd2007-02-02 11:05:29 -05001299 /* delete the leaf if it is mostly empty */
Chris Mason5de08d72007-02-24 06:24:44 -05001300 if (used < LEAF_DATA_SIZE / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001301 /* push_leaf_left fixes the path.
1302 * make sure the path still points to our leaf
1303 * for possible call to del_ptr below
1304 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001305 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001306 leaf_buf->count++;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001307 wret = push_leaf_left(root, path, 1);
1308 if (wret < 0)
1309 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001310 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001311 btrfs_header_nritems(&leaf->header)) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001312 wret = push_leaf_right(root, path, 1);
1313 if (wret < 0)
1314 ret = wret;
1315 }
Chris Mason7518a232007-03-12 12:01:18 -04001316 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001317 u64 blocknr = leaf_buf->blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001318 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001319 wret = del_ptr(root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001320 if (wret)
1321 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001322 tree_block_release(root, leaf_buf);
Chris Mason0f70abe2007-02-28 16:46:22 -05001323 wret = free_extent(root, blocknr, 1);
1324 if (wret)
1325 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001326 } else {
1327 tree_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001328 }
1329 }
1330 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001331 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001332}
1333
Chris Mason97571fd2007-02-24 13:39:08 -05001334/*
1335 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001336 * returns 0 if it found something or 1 if there are no greater leaves.
1337 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001338 */
Chris Masond97e63b2007-02-20 16:40:44 -05001339int next_leaf(struct ctree_root *root, struct ctree_path *path)
1340{
1341 int slot;
1342 int level = 1;
1343 u64 blocknr;
1344 struct tree_buffer *c;
Chris Masoncfaa7292007-02-21 17:04:57 -05001345 struct tree_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001346
1347 while(level < MAX_LEVEL) {
1348 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001349 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001350 slot = path->slots[level] + 1;
1351 c = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001352 if (slot >= btrfs_header_nritems(&c->node.header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001353 level++;
1354 continue;
1355 }
1356 blocknr = c->node.blockptrs[slot];
Chris Masoncfaa7292007-02-21 17:04:57 -05001357 if (next)
1358 tree_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001359 next = read_tree_block(root, blocknr);
1360 break;
1361 }
1362 path->slots[level] = slot;
1363 while(1) {
1364 level--;
1365 c = path->nodes[level];
1366 tree_block_release(root, c);
1367 path->nodes[level] = next;
1368 path->slots[level] = 0;
1369 if (!level)
1370 break;
1371 next = read_tree_block(root, next->node.blockptrs[0]);
1372 }
1373 return 0;
1374}
1375
Chris Mason02217ed2007-03-02 16:08:05 -05001376