blob: 489019ac04b8db1c587679cacf109665cfbc5847 [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 Masonbe0e5c02007-01-26 15:51:26 -050079 return leaf->items[nr-1].offset;
80}
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 Masonaa5d6be2007-02-28 16:35:06 -0500177 BUG_ON(leaf->items[i].offset != leaf->items[i + 1].offset +
178 leaf->items[i + 1].size);
179 if (i == 0) {
180 BUG_ON(leaf->items[i].offset + leaf->items[i].size !=
181 LEAF_DATA_SIZE);
182 }
183 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500184 return 0;
185}
186
187int check_block(struct ctree_path *path, int level)
188{
189 if (level == 0)
190 return check_leaf(path, level);
191 return check_node(path, level);
192}
193
Chris Mason74123bd2007-02-02 11:05:29 -0500194/*
195 * search for key in the array p. items p are item_size apart
196 * and there are 'max' items in p
197 * the slot in the array is returned via slot, and it points to
198 * the place where you would insert key if it is not found in
199 * the array.
200 *
201 * slot may point to max if the key is bigger than all of the keys
202 */
Chris Masone2fa7222007-03-12 16:22:34 -0400203int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500204 int max, int *slot)
205{
206 int low = 0;
207 int high = max;
208 int mid;
209 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400210 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500211
212 while(low < high) {
213 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400214 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500215 ret = comp_keys(tmp, key);
216
217 if (ret < 0)
218 low = mid + 1;
219 else if (ret > 0)
220 high = mid;
221 else {
222 *slot = mid;
223 return 0;
224 }
225 }
226 *slot = low;
227 return 1;
228}
229
Chris Mason97571fd2007-02-24 13:39:08 -0500230/*
231 * simple bin_search frontend that does the right thing for
232 * leaves vs nodes
233 */
Chris Masone2fa7222007-03-12 16:22:34 -0400234int bin_search(struct node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500235{
Chris Mason7518a232007-03-12 12:01:18 -0400236 if (btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500237 struct leaf *l = (struct leaf *)c;
238 return generic_bin_search((void *)l->items, sizeof(struct item),
Chris Mason7518a232007-03-12 12:01:18 -0400239 key, btrfs_header_nritems(&c->header),
240 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500241 } else {
Chris Masone2fa7222007-03-12 16:22:34 -0400242 return generic_bin_search((void *)c->keys,
243 sizeof(struct btrfs_disk_key),
Chris Mason7518a232007-03-12 12:01:18 -0400244 key, btrfs_header_nritems(&c->header),
245 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500246 }
247 return -1;
248}
249
Chris Masonbb803952007-03-01 12:04:21 -0500250struct tree_buffer *read_node_slot(struct ctree_root *root,
251 struct tree_buffer *parent_buf,
252 int slot)
253{
254 struct node *node = &parent_buf->node;
255 if (slot < 0)
256 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400257 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500258 return NULL;
259 return read_tree_block(root, node->blockptrs[slot]);
260}
261
262static int balance_level(struct ctree_root *root, struct ctree_path *path,
263 int level)
264{
265 struct tree_buffer *right_buf;
266 struct tree_buffer *mid_buf;
267 struct tree_buffer *left_buf;
268 struct tree_buffer *parent_buf = NULL;
269 struct node *right = NULL;
270 struct node *mid;
271 struct node *left = NULL;
272 struct node *parent = NULL;
273 int ret = 0;
274 int wret;
275 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500276 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500277 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500278
279 if (level == 0)
280 return 0;
281
282 mid_buf = path->nodes[level];
283 mid = &mid_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500284 orig_ptr = mid->blockptrs[orig_slot];
285
Chris Masonbb803952007-03-01 12:04:21 -0500286 if (level < MAX_LEVEL - 1)
287 parent_buf = path->nodes[level + 1];
288 pslot = path->slots[level + 1];
289
290 if (!parent_buf) {
291 struct tree_buffer *child;
292 u64 blocknr = mid_buf->blocknr;
293
Chris Mason7518a232007-03-12 12:01:18 -0400294 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500295 return 0;
296
297 /* promote the child to a root */
298 child = read_node_slot(root, mid_buf, 0);
299 BUG_ON(!child);
300 root->node = child;
301 path->nodes[level] = NULL;
302 /* once for the path */
303 tree_block_release(root, mid_buf);
304 /* once for the root ptr */
305 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500306 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500307 return free_extent(root, blocknr, 1);
308 }
309 parent = &parent_buf->node;
310
Chris Mason7518a232007-03-12 12:01:18 -0400311 if (btrfs_header_nritems(&mid->header) > NODEPTRS_PER_BLOCK / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500312 return 0;
313
Chris Masonbb803952007-03-01 12:04:21 -0500314 left_buf = read_node_slot(root, parent_buf, pslot - 1);
315 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500316
317 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500318 if (left_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500319 btrfs_cow_block(root, left_buf, parent_buf,
320 pslot - 1, &left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500321 left = &left_buf->node;
Chris Mason7518a232007-03-12 12:01:18 -0400322 orig_slot += btrfs_header_nritems(&left->header);
Chris Mason79f95c82007-03-01 15:16:26 -0500323 wret = push_node_left(root, left_buf, mid_buf);
324 if (wret < 0)
325 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500326 }
Chris Mason79f95c82007-03-01 15:16:26 -0500327
328 /*
329 * then try to empty the right most buffer into the middle
330 */
Chris Masonbb803952007-03-01 12:04:21 -0500331 if (right_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500332 btrfs_cow_block(root, right_buf, parent_buf,
333 pslot + 1, &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500334 right = &right_buf->node;
335 wret = push_node_left(root, mid_buf, right_buf);
336 if (wret < 0)
337 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400338 if (btrfs_header_nritems(&right->header) == 0) {
Chris Masonbb803952007-03-01 12:04:21 -0500339 u64 blocknr = right_buf->blocknr;
340 tree_block_release(root, right_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500341 clean_tree_block(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500342 right_buf = NULL;
343 right = NULL;
344 wret = del_ptr(root, path, level + 1, pslot + 1);
345 if (wret)
346 ret = wret;
347 wret = free_extent(root, blocknr, 1);
348 if (wret)
349 ret = wret;
350 } else {
351 memcpy(parent->keys + pslot + 1, right->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400352 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500353 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500354 }
355 }
Chris Mason7518a232007-03-12 12:01:18 -0400356 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500357 /*
358 * we're not allowed to leave a node with one item in the
359 * tree during a delete. A deletion from lower in the tree
360 * could try to delete the only pointer in this node.
361 * So, pull some keys from the left.
362 * There has to be a left pointer at this point because
363 * otherwise we would have pulled some pointers from the
364 * right
365 */
366 BUG_ON(!left_buf);
367 wret = balance_node_right(root, mid_buf, left_buf);
368 if (wret < 0)
369 ret = wret;
370 BUG_ON(wret == 1);
371 }
Chris Mason7518a232007-03-12 12:01:18 -0400372 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500373 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500374 u64 blocknr = mid_buf->blocknr;
375 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500376 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500377 mid_buf = NULL;
378 mid = NULL;
379 wret = del_ptr(root, path, level + 1, pslot);
380 if (wret)
381 ret = wret;
382 wret = free_extent(root, blocknr, 1);
383 if (wret)
384 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500385 } else {
386 /* update the parent key to reflect our changes */
Chris Masone2fa7222007-03-12 16:22:34 -0400387 memcpy(parent->keys + pslot, mid->keys,
388 sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500389 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500390 }
Chris Masonbb803952007-03-01 12:04:21 -0500391
Chris Mason79f95c82007-03-01 15:16:26 -0500392 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500393 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400394 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500395 left_buf->count++; // released below
396 path->nodes[level] = left_buf;
397 path->slots[level + 1] -= 1;
398 path->slots[level] = orig_slot;
399 if (mid_buf)
400 tree_block_release(root, mid_buf);
401 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400402 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500403 path->slots[level] = orig_slot;
404 }
405 }
Chris Mason79f95c82007-03-01 15:16:26 -0500406 /* double check we haven't messed things up */
407 check_block(path, level);
408 if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]])
409 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500410
411 if (right_buf)
412 tree_block_release(root, right_buf);
413 if (left_buf)
414 tree_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500415 return ret;
416}
417
Chris Mason74123bd2007-02-02 11:05:29 -0500418/*
419 * look for key in the tree. path is filled in with nodes along the way
420 * if key is found, we return zero and you can find the item in the leaf
421 * level of the path (level 0)
422 *
423 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500424 * be inserted, and 1 is returned. If there are other errors during the
425 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500426 *
427 * if ins_len > 0, nodes and leaves will be split as we walk down the
428 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
429 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500430 */
Chris Masone2fa7222007-03-12 16:22:34 -0400431int search_slot(struct ctree_root *root, struct btrfs_key *key,
Chris Mason02217ed2007-03-02 16:08:05 -0500432 struct ctree_path *p, int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500433{
Chris Masonbb803952007-03-01 12:04:21 -0500434 struct tree_buffer *b;
Chris Mason02217ed2007-03-02 16:08:05 -0500435 struct tree_buffer *cow_buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500436 struct node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500437 int slot;
438 int ret;
439 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500440
Chris Masonbb803952007-03-01 12:04:21 -0500441again:
442 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500443 b->count++;
444 while (b) {
Chris Mason7518a232007-03-12 12:01:18 -0400445 level = btrfs_header_level(&b->node.header);
Chris Mason02217ed2007-03-02 16:08:05 -0500446 if (cow) {
447 int wret;
448 wret = btrfs_cow_block(root, b, p->nodes[level + 1],
449 p->slots[level + 1], &cow_buf);
450 b = cow_buf;
451 }
452 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500453 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500454 p->nodes[level] = b;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500455 ret = check_block(p, level);
456 if (ret)
457 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500458 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400459 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500460 if (ret && slot > 0)
461 slot -= 1;
462 p->slots[level] = slot;
Chris Mason7518a232007-03-12 12:01:18 -0400463 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
464 NODEPTRS_PER_BLOCK) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500465 int sret = split_node(root, p, level);
466 BUG_ON(sret > 0);
467 if (sret)
468 return sret;
469 b = p->nodes[level];
470 c = &b->node;
471 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500472 } else if (ins_len < 0) {
473 int sret = balance_level(root, p, level);
474 if (sret)
475 return sret;
476 b = p->nodes[level];
477 if (!b)
478 goto again;
479 c = &b->node;
480 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400481 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500482 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500483 b = read_tree_block(root, c->blockptrs[slot]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500484 } else {
Chris Mason5c680ed2007-02-22 11:39:13 -0500485 struct leaf *l = (struct leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500486 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500487 if (ins_len > 0 && leaf_free_space(l) <
488 sizeof(struct item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500489 int sret = split_leaf(root, p, ins_len);
490 BUG_ON(sret > 0);
491 if (sret)
492 return sret;
493 }
Chris Masonbb803952007-03-01 12:04:21 -0500494 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500495 return ret;
496 }
497 }
Chris Masonbb803952007-03-01 12:04:21 -0500498 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500499 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500500}
501
Chris Mason74123bd2007-02-02 11:05:29 -0500502/*
503 * adjust the pointers going up the tree, starting at level
504 * making sure the right key of each node is points to 'key'.
505 * This is used after shifting pointers to the left, so it stops
506 * fixing up pointers when a given leaf/node is not in slot 0 of the
507 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500508 *
509 * If this fails to write a tree block, it returns -1, but continues
510 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500511 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500512static int fixup_low_keys(struct ctree_root *root,
Chris Masone2fa7222007-03-12 16:22:34 -0400513 struct ctree_path *path, struct btrfs_disk_key *key,
Chris Masoneb60cea2007-02-02 09:18:22 -0500514 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500515{
516 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500517 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500518 for (i = level; i < MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500519 struct node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500520 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500521 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500522 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500523 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500524 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500525 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500526 if (tslot != 0)
527 break;
528 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500529 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500530}
531
Chris Mason74123bd2007-02-02 11:05:29 -0500532/*
533 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500534 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500535 *
536 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
537 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500538 */
Chris Masonbb803952007-03-01 12:04:21 -0500539static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
540 struct tree_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500541{
Chris Masonbb803952007-03-01 12:04:21 -0500542 struct node *src = &src_buf->node;
543 struct node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500544 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500545 int src_nritems;
546 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500547 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500548
Chris Mason7518a232007-03-12 12:01:18 -0400549 src_nritems = btrfs_header_nritems(&src->header);
550 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Masonbb803952007-03-01 12:04:21 -0500551 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500552 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500553 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500554 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500555
556 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500557 push_items = src_nritems;
558
Chris Masonbb803952007-03-01 12:04:21 -0500559 memcpy(dst->keys + dst_nritems, src->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400560 push_items * sizeof(struct btrfs_disk_key));
Chris Masonbb803952007-03-01 12:04:21 -0500561 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500562 push_items * sizeof(u64));
Chris Masonbb803952007-03-01 12:04:21 -0500563 if (push_items < src_nritems) {
564 memmove(src->keys, src->keys + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400565 (src_nritems - push_items) *
566 sizeof(struct btrfs_disk_key));
Chris Masonbb803952007-03-01 12:04:21 -0500567 memmove(src->blockptrs, src->blockptrs + push_items,
568 (src_nritems - push_items) * sizeof(u64));
569 }
Chris Mason7518a232007-03-12 12:01:18 -0400570 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
571 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason02217ed2007-03-02 16:08:05 -0500572 BUG_ON(list_empty(&src_buf->dirty));
573 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500574 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500575}
576
Chris Mason97571fd2007-02-24 13:39:08 -0500577/*
Chris Mason79f95c82007-03-01 15:16:26 -0500578 * try to push data from one node into the next node right in the
579 * tree.
580 *
581 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
582 * error, and > 0 if there was no room in the right hand block.
583 *
584 * this will only push up to 1/2 the contents of the left node over
585 */
586static int balance_node_right(struct ctree_root *root,
587 struct tree_buffer *dst_buf,
588 struct tree_buffer *src_buf)
589{
590 struct node *src = &src_buf->node;
591 struct node *dst = &dst_buf->node;
592 int push_items = 0;
593 int max_push;
594 int src_nritems;
595 int dst_nritems;
596 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500597
Chris Mason7518a232007-03-12 12:01:18 -0400598 src_nritems = btrfs_header_nritems(&src->header);
599 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason79f95c82007-03-01 15:16:26 -0500600 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
601 if (push_items <= 0) {
602 return 1;
603 }
604
605 max_push = src_nritems / 2 + 1;
606 /* don't try to empty the node */
607 if (max_push > src_nritems)
608 return 1;
609 if (max_push < push_items)
610 push_items = max_push;
611
612 memmove(dst->keys + push_items, dst->keys,
Chris Masone2fa7222007-03-12 16:22:34 -0400613 dst_nritems * sizeof(struct btrfs_disk_key));
Chris Mason79f95c82007-03-01 15:16:26 -0500614 memmove(dst->blockptrs + push_items, dst->blockptrs,
615 dst_nritems * sizeof(u64));
616 memcpy(dst->keys, src->keys + src_nritems - push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400617 push_items * sizeof(struct btrfs_disk_key));
Chris Mason79f95c82007-03-01 15:16:26 -0500618 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
619 push_items * sizeof(u64));
620
Chris Mason7518a232007-03-12 12:01:18 -0400621 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
622 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -0500623
Chris Mason02217ed2007-03-02 16:08:05 -0500624 BUG_ON(list_empty(&src_buf->dirty));
625 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500626 return ret;
627}
628
629/*
Chris Mason97571fd2007-02-24 13:39:08 -0500630 * helper function to insert a new root level in the tree.
631 * A new node is allocated, and a single item is inserted to
632 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500633 *
634 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500635 */
Chris Mason5de08d72007-02-24 06:24:44 -0500636static int insert_new_root(struct ctree_root *root,
637 struct ctree_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500638{
639 struct tree_buffer *t;
640 struct node *lower;
641 struct node *c;
Chris Masone2fa7222007-03-12 16:22:34 -0400642 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -0500643
644 BUG_ON(path->nodes[level]);
645 BUG_ON(path->nodes[level-1] != root->node);
646
647 t = alloc_free_block(root);
648 c = &t->node;
649 memset(c, 0, sizeof(c));
Chris Mason7518a232007-03-12 12:01:18 -0400650 btrfs_set_header_nritems(&c->header, 1);
651 btrfs_set_header_level(&c->header, level);
652 btrfs_set_header_blocknr(&c->header, t->blocknr);
653 btrfs_set_header_parentid(&c->header,
654 btrfs_header_parentid(&root->node->node.header));
Chris Mason5c680ed2007-02-22 11:39:13 -0500655 lower = &path->nodes[level-1]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400656 if (btrfs_is_leaf(lower))
Chris Mason5c680ed2007-02-22 11:39:13 -0500657 lower_key = &((struct leaf *)lower)->items[0].key;
658 else
659 lower_key = lower->keys;
Chris Masone2fa7222007-03-12 16:22:34 -0400660 memcpy(c->keys, lower_key, sizeof(struct btrfs_disk_key));
Chris Mason5c680ed2007-02-22 11:39:13 -0500661 c->blockptrs[0] = path->nodes[level-1]->blocknr;
662 /* the super has an extra ref to root->node */
663 tree_block_release(root, root->node);
664 root->node = t;
665 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500666 path->nodes[level] = t;
667 path->slots[level] = 0;
668 return 0;
669}
670
Chris Mason74123bd2007-02-02 11:05:29 -0500671/*
672 * worker function to insert a single pointer in a node.
673 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500674 *
Chris Mason74123bd2007-02-02 11:05:29 -0500675 * slot and level indicate where you want the key to go, and
676 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500677 *
678 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500679 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500680static int insert_ptr(struct ctree_root *root,
Chris Masone2fa7222007-03-12 16:22:34 -0400681 struct ctree_path *path, struct btrfs_disk_key *key,
Chris Mason74123bd2007-02-02 11:05:29 -0500682 u64 blocknr, int slot, int level)
683{
Chris Mason74123bd2007-02-02 11:05:29 -0500684 struct node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500685 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500686
687 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500688 lower = &path->nodes[level]->node;
Chris Mason7518a232007-03-12 12:01:18 -0400689 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -0500690 if (slot > nritems)
691 BUG();
692 if (nritems == NODEPTRS_PER_BLOCK)
693 BUG();
694 if (slot != nritems) {
695 memmove(lower->keys + slot + 1, lower->keys + slot,
Chris Masone2fa7222007-03-12 16:22:34 -0400696 (nritems - slot) * sizeof(struct btrfs_disk_key));
Chris Mason74123bd2007-02-02 11:05:29 -0500697 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
698 (nritems - slot) * sizeof(u64));
699 }
Chris Masone2fa7222007-03-12 16:22:34 -0400700 memcpy(lower->keys + slot, key, sizeof(struct btrfs_disk_key));
Chris Mason74123bd2007-02-02 11:05:29 -0500701 lower->blockptrs[slot] = blocknr;
Chris Mason7518a232007-03-12 12:01:18 -0400702 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Mason74123bd2007-02-02 11:05:29 -0500703 if (lower->keys[1].objectid == 0)
704 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -0500705 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500706 return 0;
707}
708
Chris Mason97571fd2007-02-24 13:39:08 -0500709/*
710 * split the node at the specified level in path in two.
711 * The path is corrected to point to the appropriate node after the split
712 *
713 * Before splitting this tries to make some room in the node by pushing
714 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500715 *
716 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500717 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500718static int split_node(struct ctree_root *root, struct ctree_path *path,
719 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500720{
Chris Mason5c680ed2007-02-22 11:39:13 -0500721 struct tree_buffer *t;
722 struct node *c;
723 struct tree_buffer *split_buffer;
724 struct node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500725 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500726 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500727 int wret;
Chris Mason7518a232007-03-12 12:01:18 -0400728 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500729
Chris Mason5c680ed2007-02-22 11:39:13 -0500730 t = path->nodes[level];
731 c = &t->node;
732 if (t == root->node) {
733 /* trying to split the root, lets make a new one */
734 ret = insert_new_root(root, path, level + 1);
735 if (ret)
736 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500737 }
Chris Mason7518a232007-03-12 12:01:18 -0400738 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason5c680ed2007-02-22 11:39:13 -0500739 split_buffer = alloc_free_block(root);
740 split = &split_buffer->node;
Chris Mason7518a232007-03-12 12:01:18 -0400741 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
742 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
743 btrfs_set_header_parentid(&split->header,
744 btrfs_header_parentid(&root->node->node.header));
745 mid = (c_nritems + 1) / 2;
Chris Mason5c680ed2007-02-22 11:39:13 -0500746 memcpy(split->keys, c->keys + mid,
Chris Masone2fa7222007-03-12 16:22:34 -0400747 (c_nritems - mid) * sizeof(struct btrfs_disk_key));
Chris Mason5c680ed2007-02-22 11:39:13 -0500748 memcpy(split->blockptrs, c->blockptrs + mid,
Chris Mason7518a232007-03-12 12:01:18 -0400749 (c_nritems - mid) * sizeof(u64));
750 btrfs_set_header_nritems(&split->header, c_nritems - mid);
751 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500752 ret = 0;
753
Chris Mason02217ed2007-03-02 16:08:05 -0500754 BUG_ON(list_empty(&t->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500755 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
756 path->slots[level + 1] + 1, level + 1);
757 if (wret)
758 ret = wret;
759
Chris Mason5de08d72007-02-24 06:24:44 -0500760 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500761 path->slots[level] -= mid;
762 tree_block_release(root, t);
763 path->nodes[level] = split_buffer;
764 path->slots[level + 1] += 1;
765 } else {
766 tree_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500767 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500768 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500769}
770
Chris Mason74123bd2007-02-02 11:05:29 -0500771/*
772 * how many bytes are required to store the items in a leaf. start
773 * and nr indicate which items in the leaf to check. This totals up the
774 * space used both by the item structs and the item data
775 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500776static int leaf_space_used(struct leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777{
778 int data_len;
779 int end = start + nr - 1;
780
781 if (!nr)
782 return 0;
783 data_len = l->items[start].offset + l->items[start].size;
784 data_len = data_len - l->items[end].offset;
785 data_len += sizeof(struct item) * nr;
786 return data_len;
787}
788
Chris Mason74123bd2007-02-02 11:05:29 -0500789/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500790 * push some data in the path leaf to the right, trying to free up at
791 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500792 *
793 * returns 1 if the push failed because the other node didn't have enough
794 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500795 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500796static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
797 int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500798{
799 struct tree_buffer *left_buf = path->nodes[0];
800 struct leaf *left = &left_buf->leaf;
801 struct leaf *right;
802 struct tree_buffer *right_buf;
803 struct tree_buffer *upper;
804 int slot;
805 int i;
806 int free_space;
807 int push_space = 0;
808 int push_items = 0;
809 struct item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400810 u32 left_nritems;
811 u32 right_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500812
813 slot = path->slots[1];
814 if (!path->nodes[1]) {
815 return 1;
816 }
817 upper = path->nodes[1];
Chris Mason7518a232007-03-12 12:01:18 -0400818 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500819 return 1;
820 }
821 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
822 right = &right_buf->leaf;
823 free_space = leaf_free_space(right);
824 if (free_space < data_size + sizeof(struct item)) {
825 tree_block_release(root, right_buf);
826 return 1;
827 }
Chris Mason02217ed2007-03-02 16:08:05 -0500828 /* cow and double check */
829 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
830 right = &right_buf->leaf;
831 free_space = leaf_free_space(right);
832 if (free_space < data_size + sizeof(struct item)) {
833 tree_block_release(root, right_buf);
834 return 1;
835 }
836
Chris Mason7518a232007-03-12 12:01:18 -0400837 left_nritems = btrfs_header_nritems(&left->header);
838 for (i = left_nritems - 1; i >= 0; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500839 item = left->items + i;
840 if (path->slots[0] == i)
841 push_space += data_size + sizeof(*item);
842 if (item->size + sizeof(*item) + push_space > free_space)
843 break;
844 push_items++;
845 push_space += item->size + sizeof(*item);
846 }
847 if (push_items == 0) {
848 tree_block_release(root, right_buf);
849 return 1;
850 }
Chris Mason7518a232007-03-12 12:01:18 -0400851 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -0500852 /* push left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400853 push_space = left->items[left_nritems - push_items].offset +
854 left->items[left_nritems - push_items].size;
Chris Mason00ec4c52007-02-24 12:47:20 -0500855 push_space -= leaf_data_end(left);
856 /* make room in the right data area */
857 memmove(right->data + leaf_data_end(right) - push_space,
858 right->data + leaf_data_end(right),
859 LEAF_DATA_SIZE - leaf_data_end(right));
860 /* copy from the left data area */
861 memcpy(right->data + LEAF_DATA_SIZE - push_space,
862 left->data + leaf_data_end(left),
863 push_space);
864 memmove(right->items + push_items, right->items,
Chris Mason7518a232007-03-12 12:01:18 -0400865 right_nritems * sizeof(struct item));
Chris Mason00ec4c52007-02-24 12:47:20 -0500866 /* copy the items from left to right */
Chris Mason7518a232007-03-12 12:01:18 -0400867 memcpy(right->items, left->items + left_nritems - push_items,
Chris Mason00ec4c52007-02-24 12:47:20 -0500868 push_items * sizeof(struct item));
869
870 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -0400871 right_nritems += push_items;
872 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500873 push_space = LEAF_DATA_SIZE;
Chris Mason7518a232007-03-12 12:01:18 -0400874 for (i = 0; i < right_nritems; i++) {
Chris Mason00ec4c52007-02-24 12:47:20 -0500875 right->items[i].offset = push_space - right->items[i].size;
876 push_space = right->items[i].offset;
877 }
Chris Mason7518a232007-03-12 12:01:18 -0400878 left_nritems -= push_items;
879 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -0500880
Chris Mason02217ed2007-03-02 16:08:05 -0500881 BUG_ON(list_empty(&left_buf->dirty));
882 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason00ec4c52007-02-24 12:47:20 -0500883 memcpy(upper->node.keys + slot + 1,
Chris Masone2fa7222007-03-12 16:22:34 -0400884 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Mason02217ed2007-03-02 16:08:05 -0500885 BUG_ON(list_empty(&upper->dirty));
886
Chris Mason00ec4c52007-02-24 12:47:20 -0500887 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -0400888 if (path->slots[0] >= left_nritems) {
889 path->slots[0] -= left_nritems;
Chris Mason00ec4c52007-02-24 12:47:20 -0500890 tree_block_release(root, path->nodes[0]);
891 path->nodes[0] = right_buf;
892 path->slots[1] += 1;
893 } else {
894 tree_block_release(root, right_buf);
895 }
896 return 0;
897}
898/*
Chris Mason74123bd2007-02-02 11:05:29 -0500899 * push some data in the path leaf to the left, trying to free up at
900 * least data_size bytes. returns zero if the push worked, nonzero otherwise
901 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500902static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
903 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500904{
Chris Masoneb60cea2007-02-02 09:18:22 -0500905 struct tree_buffer *right_buf = path->nodes[0];
906 struct leaf *right = &right_buf->leaf;
907 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500908 struct leaf *left;
909 int slot;
910 int i;
911 int free_space;
912 int push_space = 0;
913 int push_items = 0;
914 struct item *item;
Chris Mason7518a232007-03-12 12:01:18 -0400915 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500916 int ret = 0;
917 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500918
919 slot = path->slots[1];
920 if (slot == 0) {
921 return 1;
922 }
923 if (!path->nodes[1]) {
924 return 1;
925 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500926 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
927 left = &t->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500928 free_space = leaf_free_space(left);
929 if (free_space < data_size + sizeof(struct item)) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500930 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500931 return 1;
932 }
Chris Mason02217ed2007-03-02 16:08:05 -0500933
934 /* cow and double check */
935 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
936 left = &t->leaf;
937 free_space = leaf_free_space(left);
938 if (free_space < data_size + sizeof(struct item)) {
939 tree_block_release(root, t);
940 return 1;
941 }
942
Chris Mason7518a232007-03-12 12:01:18 -0400943 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500944 item = right->items + i;
945 if (path->slots[0] == i)
946 push_space += data_size + sizeof(*item);
947 if (item->size + sizeof(*item) + push_space > free_space)
948 break;
949 push_items++;
950 push_space += item->size + sizeof(*item);
951 }
952 if (push_items == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500953 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500954 return 1;
955 }
956 /* push data from right to left */
Chris Mason7518a232007-03-12 12:01:18 -0400957 memcpy(left->items + btrfs_header_nritems(&left->header),
Chris Masonbe0e5c02007-01-26 15:51:26 -0500958 right->items, push_items * sizeof(struct item));
959 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
960 memcpy(left->data + leaf_data_end(left) - push_space,
961 right->data + right->items[push_items - 1].offset,
962 push_space);
Chris Mason7518a232007-03-12 12:01:18 -0400963 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500964 BUG_ON(old_left_nritems < 0);
965
Chris Masonbe0e5c02007-01-26 15:51:26 -0500966 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
967 left->items[i].offset -= LEAF_DATA_SIZE -
968 left->items[old_left_nritems -1].offset;
969 }
Chris Mason7518a232007-03-12 12:01:18 -0400970 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500971
972 /* fixup right node */
973 push_space = right->items[push_items-1].offset - leaf_data_end(right);
974 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
975 leaf_data_end(right), push_space);
976 memmove(right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -0400977 (btrfs_header_nritems(&right->header) - push_items) *
978 sizeof(struct item));
979 btrfs_set_header_nritems(&right->header,
980 btrfs_header_nritems(&right->header) -
981 push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500982 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500983
Chris Mason7518a232007-03-12 12:01:18 -0400984 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500985 right->items[i].offset = push_space - right->items[i].size;
986 push_space = right->items[i].offset;
987 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500988
Chris Mason02217ed2007-03-02 16:08:05 -0500989 BUG_ON(list_empty(&t->dirty));
990 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500991
Chris Masonaa5d6be2007-02-28 16:35:06 -0500992 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
993 if (wret)
994 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500995
996 /* then fixup the leaf pointer in the path */
997 if (path->slots[0] < push_items) {
998 path->slots[0] += old_left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500999 tree_block_release(root, path->nodes[0]);
1000 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001001 path->slots[1] -= 1;
1002 } else {
Chris Masoneb60cea2007-02-02 09:18:22 -05001003 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001004 path->slots[0] -= push_items;
1005 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001006 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001007 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001008}
1009
Chris Mason74123bd2007-02-02 11:05:29 -05001010/*
1011 * split the path's leaf in two, making sure there is at least data_size
1012 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001013 *
1014 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001015 */
Chris Masonaa5d6be2007-02-28 16:35:06 -05001016static int split_leaf(struct ctree_root *root, struct ctree_path *path,
1017 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001018{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001019 struct tree_buffer *l_buf;
1020 struct leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001021 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001022 int mid;
1023 int slot;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001024 struct leaf *right;
Chris Masoneb60cea2007-02-02 09:18:22 -05001025 struct tree_buffer *right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001026 int space_needed = data_size + sizeof(struct item);
1027 int data_copy_size;
1028 int rt_data_off;
1029 int i;
1030 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001031 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001032
Chris Masonaa5d6be2007-02-28 16:35:06 -05001033 l_buf = path->nodes[0];
1034 l = &l_buf->leaf;
1035
1036 /* did the pushes work? */
1037 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
1038 return 0;
1039
Chris Mason5c680ed2007-02-22 11:39:13 -05001040 if (!path->nodes[1]) {
1041 ret = insert_new_root(root, path, 1);
1042 if (ret)
1043 return ret;
1044 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001045 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001046 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001047 mid = (nritems + 1)/ 2;
Chris Masoneb60cea2007-02-02 09:18:22 -05001048 right_buffer = alloc_free_block(root);
1049 BUG_ON(!right_buffer);
1050 BUG_ON(mid == nritems);
1051 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001052 memset(right, 0, sizeof(*right));
1053 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001054 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001055 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1056 LEAF_DATA_SIZE)
1057 BUG();
1058 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001059 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001060 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1061 LEAF_DATA_SIZE)
1062 BUG();
1063 }
Chris Mason7518a232007-03-12 12:01:18 -04001064 btrfs_set_header_nritems(&right->header, nritems - mid);
1065 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1066 btrfs_set_header_level(&right->header, 0);
1067 btrfs_set_header_parentid(&right->header,
1068 btrfs_header_parentid(&root->node->node.header));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001069 data_copy_size = l->items[mid].offset + l->items[mid].size -
1070 leaf_data_end(l);
1071 memcpy(right->items, l->items + mid,
1072 (nritems - mid) * sizeof(struct item));
1073 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1074 l->data + leaf_data_end(l), data_copy_size);
1075 rt_data_off = LEAF_DATA_SIZE -
1076 (l->items[mid].offset + l->items[mid].size);
Chris Mason74123bd2007-02-02 11:05:29 -05001077
Chris Mason7518a232007-03-12 12:01:18 -04001078 for (i = 0; i < btrfs_header_nritems(&right->header); i++)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001079 right->items[i].offset += rt_data_off;
Chris Mason74123bd2007-02-02 11:05:29 -05001080
Chris Mason7518a232007-03-12 12:01:18 -04001081 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001082 ret = 0;
1083 wret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001084 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001085 if (wret)
1086 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001087 BUG_ON(list_empty(&right_buffer->dirty));
1088 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001089 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001090 if (mid <= slot) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001091 tree_block_release(root, path->nodes[0]);
1092 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001093 path->slots[0] -= mid;
1094 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001095 } else
1096 tree_block_release(root, right_buffer);
1097 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001098 return ret;
1099}
1100
Chris Mason74123bd2007-02-02 11:05:29 -05001101/*
1102 * Given a key and some data, insert an item into the tree.
1103 * This does all the path init required, making room in the tree if needed.
1104 */
Chris Masone2fa7222007-03-12 16:22:34 -04001105int insert_item(struct ctree_root *root, struct btrfs_key *cpu_key,
Chris Masonbe0e5c02007-01-26 15:51:26 -05001106 void *data, int data_size)
1107{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001108 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001109 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001110 int slot_orig;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001111 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001112 struct tree_buffer *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001113 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001114 unsigned int data_end;
1115 struct ctree_path path;
Chris Masone2fa7222007-03-12 16:22:34 -04001116 struct btrfs_disk_key disk_key;
1117
1118 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001119
Chris Mason74123bd2007-02-02 11:05:29 -05001120 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001121 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001122 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -05001123 init_path(&path);
Chris Masone2fa7222007-03-12 16:22:34 -04001124 ret = search_slot(root, cpu_key, &path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001125 if (ret == 0) {
1126 release_path(root, &path);
Chris Masonf0930a32007-03-02 09:47:58 -05001127 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001128 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001129 if (ret < 0)
1130 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001131
Chris Masoneb60cea2007-02-02 09:18:22 -05001132 slot_orig = path.slots[0];
1133 leaf_buf = path.nodes[0];
1134 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001135
Chris Mason7518a232007-03-12 12:01:18 -04001136 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001137 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001138
Chris Masonbe0e5c02007-01-26 15:51:26 -05001139 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
1140 BUG();
1141
1142 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001143 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001144 if (slot != nritems) {
1145 int i;
1146 unsigned int old_data = leaf->items[slot].offset +
1147 leaf->items[slot].size;
1148
1149 /*
1150 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1151 */
1152 /* first correct the data pointers */
1153 for (i = slot; i < nritems; i++)
1154 leaf->items[i].offset -= data_size;
1155
1156 /* shift the items */
1157 memmove(leaf->items + slot + 1, leaf->items + slot,
1158 (nritems - slot) * sizeof(struct item));
1159
1160 /* shift the data */
1161 memmove(leaf->data + data_end - data_size, leaf->data +
1162 data_end, old_data - data_end);
1163 data_end = old_data;
1164 }
Chris Mason74123bd2007-02-02 11:05:29 -05001165 /* copy the new data in */
Chris Masone2fa7222007-03-12 16:22:34 -04001166 memcpy(&leaf->items[slot].key, &disk_key,
1167 sizeof(struct btrfs_disk_key));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001168 leaf->items[slot].offset = data_end - data_size;
1169 leaf->items[slot].size = data_size;
1170 memcpy(leaf->data + data_end - data_size, data, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001171 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001172
1173 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001174 if (slot == 0)
Chris Masone2fa7222007-03-12 16:22:34 -04001175 ret = fixup_low_keys(root, &path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001176
Chris Mason02217ed2007-03-02 16:08:05 -05001177 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001178 if (leaf_free_space(leaf) < 0)
1179 BUG();
Chris Masonbb803952007-03-01 12:04:21 -05001180 check_leaf(&path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001181out:
Chris Masoneb60cea2007-02-02 09:18:22 -05001182 release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001183 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001184}
1185
Chris Mason74123bd2007-02-02 11:05:29 -05001186/*
Chris Mason5de08d72007-02-24 06:24:44 -05001187 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001188 *
1189 * If the delete empties a node, the node is removed from the tree,
1190 * continuing all the way the root if required. The root is converted into
1191 * a leaf if all the nodes are emptied.
1192 */
Chris Masonbb803952007-03-01 12:04:21 -05001193static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
1194 int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001195{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001196 struct node *node;
Chris Masonbb803952007-03-01 12:04:21 -05001197 struct tree_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001198 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001199 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001200 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201
Chris Masonbb803952007-03-01 12:04:21 -05001202 node = &parent->node;
Chris Mason7518a232007-03-12 12:01:18 -04001203 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001204 if (slot != nritems -1) {
1205 memmove(node->keys + slot, node->keys + slot + 1,
Chris Masone2fa7222007-03-12 16:22:34 -04001206 sizeof(struct btrfs_disk_key) * (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001207 memmove(node->blockptrs + slot,
1208 node->blockptrs + slot + 1,
1209 sizeof(u64) * (nritems - slot - 1));
1210 }
Chris Mason7518a232007-03-12 12:01:18 -04001211 nritems--;
1212 btrfs_set_header_nritems(&node->header, nritems);
1213 if (nritems == 0 && parent == root->node) {
1214 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001215 /* just turn the root into a leaf and break */
Chris Mason7518a232007-03-12 12:01:18 -04001216 btrfs_set_header_level(&root->node->node.header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001217 } else if (slot == 0) {
1218 wret = fixup_low_keys(root, path, node->keys, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001219 if (wret)
1220 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001221 }
Chris Mason02217ed2007-03-02 16:08:05 -05001222 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001223 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001224}
1225
Chris Mason74123bd2007-02-02 11:05:29 -05001226/*
1227 * delete the item at the leaf level in path. If that empties
1228 * the leaf, remove it from the tree
1229 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001230int del_item(struct ctree_root *root, struct ctree_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001231{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001232 int slot;
1233 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001234 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001235 int doff;
1236 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001237 int ret = 0;
1238 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001239 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001240
Chris Masoneb60cea2007-02-02 09:18:22 -05001241 leaf_buf = path->nodes[0];
1242 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001243 slot = path->slots[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05001244 doff = leaf->items[slot].offset;
1245 dsize = leaf->items[slot].size;
Chris Mason7518a232007-03-12 12:01:18 -04001246 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001247
Chris Mason7518a232007-03-12 12:01:18 -04001248 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001249 int i;
1250 int data_end = leaf_data_end(leaf);
1251 memmove(leaf->data + data_end + dsize,
1252 leaf->data + data_end,
1253 doff - data_end);
Chris Mason7518a232007-03-12 12:01:18 -04001254 for (i = slot + 1; i < nritems; i++)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001255 leaf->items[i].offset += dsize;
1256 memmove(leaf->items + slot, leaf->items + slot + 1,
1257 sizeof(struct item) *
Chris Mason7518a232007-03-12 12:01:18 -04001258 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259 }
Chris Mason7518a232007-03-12 12:01:18 -04001260 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1261 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05001262 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04001263 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001264 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04001265 btrfs_set_header_level(&leaf->header, 0);
Chris Mason02217ed2007-03-02 16:08:05 -05001266 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001267 } else {
Chris Masoned2ff2c2007-03-01 18:59:40 -05001268 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001269 wret = del_ptr(root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001270 if (wret)
1271 ret = wret;
Chris Mason0f70abe2007-02-28 16:46:22 -05001272 wret = free_extent(root, leaf_buf->blocknr, 1);
1273 if (wret)
1274 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001275 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001276 } else {
Chris Mason7518a232007-03-12 12:01:18 -04001277 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001278 if (slot == 0) {
1279 wret = fixup_low_keys(root, path,
1280 &leaf->items[0].key, 1);
1281 if (wret)
1282 ret = wret;
1283 }
Chris Mason02217ed2007-03-02 16:08:05 -05001284 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001285
Chris Mason74123bd2007-02-02 11:05:29 -05001286 /* delete the leaf if it is mostly empty */
Chris Mason5de08d72007-02-24 06:24:44 -05001287 if (used < LEAF_DATA_SIZE / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001288 /* push_leaf_left fixes the path.
1289 * make sure the path still points to our leaf
1290 * for possible call to del_ptr below
1291 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001292 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001293 leaf_buf->count++;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001294 wret = push_leaf_left(root, path, 1);
1295 if (wret < 0)
1296 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001297 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04001298 btrfs_header_nritems(&leaf->header)) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001299 wret = push_leaf_right(root, path, 1);
1300 if (wret < 0)
1301 ret = wret;
1302 }
Chris Mason7518a232007-03-12 12:01:18 -04001303 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001304 u64 blocknr = leaf_buf->blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001305 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001306 wret = del_ptr(root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001307 if (wret)
1308 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001309 tree_block_release(root, leaf_buf);
Chris Mason0f70abe2007-02-28 16:46:22 -05001310 wret = free_extent(root, blocknr, 1);
1311 if (wret)
1312 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001313 } else {
1314 tree_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001315 }
1316 }
1317 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001318 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001319}
1320
Chris Mason97571fd2007-02-24 13:39:08 -05001321/*
1322 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001323 * returns 0 if it found something or 1 if there are no greater leaves.
1324 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001325 */
Chris Masond97e63b2007-02-20 16:40:44 -05001326int next_leaf(struct ctree_root *root, struct ctree_path *path)
1327{
1328 int slot;
1329 int level = 1;
1330 u64 blocknr;
1331 struct tree_buffer *c;
Chris Masoncfaa7292007-02-21 17:04:57 -05001332 struct tree_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001333
1334 while(level < MAX_LEVEL) {
1335 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001336 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001337 slot = path->slots[level] + 1;
1338 c = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001339 if (slot >= btrfs_header_nritems(&c->node.header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05001340 level++;
1341 continue;
1342 }
1343 blocknr = c->node.blockptrs[slot];
Chris Masoncfaa7292007-02-21 17:04:57 -05001344 if (next)
1345 tree_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001346 next = read_tree_block(root, blocknr);
1347 break;
1348 }
1349 path->slots[level] = slot;
1350 while(1) {
1351 level--;
1352 c = path->nodes[level];
1353 tree_block_release(root, c);
1354 path->nodes[level] = next;
1355 path->slots[level] = 0;
1356 if (!level)
1357 break;
1358 next = read_tree_block(root, next->node.blockptrs[0]);
1359 }
1360 return 0;
1361}
1362
Chris Mason02217ed2007-03-02 16:08:05 -05001363