ctree.c 38.8 KB
Newer Older
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
4 5 6
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
C
Chris Mason 已提交
7
#include "print-tree.h"
8

C
Chris Mason 已提交
9
static int split_node(struct btrfs_root *root, struct btrfs_path *path,
C
Chris Mason 已提交
10
		      int level);
C
Chris Mason 已提交
11
static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
C
Chris Mason 已提交
12
		      int data_size);
C
Chris Mason 已提交
13 14 15 16 17 18
static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst,
			  struct btrfs_buffer *src);
static int balance_node_right(struct btrfs_root *root,
			      struct btrfs_buffer *dst_buf,
			      struct btrfs_buffer *src_buf);
static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
19
		   int slot);
20

C
Chris Mason 已提交
21
inline void btrfs_init_path(struct btrfs_path *p)
22 23 24 25
{
	memset(p, 0, sizeof(*p));
}

C
Chris Mason 已提交
26
void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
27 28
{
	int i;
C
Chris Mason 已提交
29
	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
30 31
		if (!p->nodes[i])
			break;
C
Chris Mason 已提交
32
		btrfs_block_release(root, p->nodes[i]);
33
	}
C
Chris Mason 已提交
34
	memset(p, 0, sizeof(*p));
35 36
}

C
Chris Mason 已提交
37
static int btrfs_cow_block(struct btrfs_root *root,
C
Chris Mason 已提交
38 39
		    struct btrfs_buffer *buf,
		    struct btrfs_buffer *parent,
C
Chris Mason 已提交
40
		    int parent_slot,
C
Chris Mason 已提交
41
		    struct btrfs_buffer **cow_ret)
C
Chris Mason 已提交
42
{
C
Chris Mason 已提交
43
	struct btrfs_buffer *cow;
C
Chris Mason 已提交
44 45 46 47 48

	if (!list_empty(&buf->dirty)) {
		*cow_ret = buf;
		return 0;
	}
C
Chris Mason 已提交
49
	cow = btrfs_alloc_free_block(root);
C
Chris Mason 已提交
50
	memcpy(&cow->node, &buf->node, root->blocksize);
51
	btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
C
Chris Mason 已提交
52
	*cow_ret = cow;
53
	btrfs_inc_ref(root, buf);
C
Chris Mason 已提交
54 55 56
	if (buf == root->node) {
		root->node = cow;
		cow->count++;
57
		if (buf != root->commit_root)
C
Chris Mason 已提交
58 59
			btrfs_free_extent(root, buf->blocknr, 1);
		btrfs_block_release(root, buf);
C
Chris Mason 已提交
60
	} else {
61 62
		btrfs_set_node_blockptr(&parent->node, parent_slot,
					cow->blocknr);
C
Chris Mason 已提交
63
		BUG_ON(list_empty(&parent->dirty));
C
Chris Mason 已提交
64
		btrfs_free_extent(root, buf->blocknr, 1);
C
Chris Mason 已提交
65
	}
C
Chris Mason 已提交
66
	btrfs_block_release(root, buf);
C
Chris Mason 已提交
67 68 69
	return 0;
}

C
Chris Mason 已提交
70 71 72 73 74
/*
 * The leaf data grows from end-to-front in the node.
 * this returns the address of the start of the last item,
 * which is the stop of the leaf data stack
 */
C
Chris Mason 已提交
75 76
static inline unsigned int leaf_data_end(struct btrfs_root *root,
					 struct btrfs_leaf *leaf)
77
{
78
	u32 nr = btrfs_header_nritems(&leaf->header);
79
	if (nr == 0)
C
Chris Mason 已提交
80
		return BTRFS_LEAF_DATA_SIZE(root);
C
Chris Mason 已提交
81
	return btrfs_item_offset(leaf->items + nr - 1);
82 83
}

C
Chris Mason 已提交
84 85 86 87 88
/*
 * The space between the end of the leaf items and
 * the start of the leaf data.  IOW, how much room
 * the leaf has left for both items and data
 */
C
Chris Mason 已提交
89
int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
90
{
C
Chris Mason 已提交
91
	int data_end = leaf_data_end(root, leaf);
92
	int nritems = btrfs_header_nritems(&leaf->header);
93
	char *items_end = (char *)(leaf->items + nritems + 1);
C
Chris Mason 已提交
94
	return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
95 96
}

C
Chris Mason 已提交
97 98 99
/*
 * compare two keys in a memcmp fashion
 */
C
Chris Mason 已提交
100
static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
101
{
C
Chris Mason 已提交
102 103 104 105 106
	struct btrfs_key k1;

	btrfs_disk_key_to_cpu(&k1, disk);

	if (k1.objectid > k2->objectid)
107
		return 1;
C
Chris Mason 已提交
108
	if (k1.objectid < k2->objectid)
109
		return -1;
C
Chris Mason 已提交
110
	if (k1.offset > k2->offset)
111
		return 1;
C
Chris Mason 已提交
112
	if (k1.offset < k2->offset)
113
		return -1;
114 115 116 117
	if (k1.flags > k2->flags)
		return 1;
	if (k1.flags < k2->flags)
		return -1;
118 119
	return 0;
}
C
Chris Mason 已提交
120

C
Chris Mason 已提交
121 122
static int check_node(struct btrfs_root *root, struct btrfs_path *path,
		      int level)
C
Chris Mason 已提交
123 124
{
	int i;
C
Chris Mason 已提交
125 126
	struct btrfs_node *parent = NULL;
	struct btrfs_node *node = &path->nodes[level]->node;
C
Chris Mason 已提交
127
	int parent_slot;
128
	u32 nritems = btrfs_header_nritems(&node->header);
C
Chris Mason 已提交
129 130 131 132

	if (path->nodes[level + 1])
		parent = &path->nodes[level + 1]->node;
	parent_slot = path->slots[level + 1];
133 134
	BUG_ON(nritems == 0);
	if (parent) {
C
Chris Mason 已提交
135
		struct btrfs_disk_key *parent_key;
C
Chris Mason 已提交
136 137
		parent_key = &parent->ptrs[parent_slot].key;
		BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
C
Chris Mason 已提交
138
			      sizeof(struct btrfs_disk_key)));
139
		BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
140
		       btrfs_header_blocknr(&node->header));
C
Chris Mason 已提交
141
	}
C
Chris Mason 已提交
142
	BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
143
	for (i = 0; nritems > 1 && i < nritems - 2; i++) {
C
Chris Mason 已提交
144
		struct btrfs_key cpukey;
C
Chris Mason 已提交
145 146
		btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
		BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
C
Chris Mason 已提交
147 148 149 150
	}
	return 0;
}

C
Chris Mason 已提交
151 152
static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
		      int level)
C
Chris Mason 已提交
153 154
{
	int i;
C
Chris Mason 已提交
155 156
	struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
	struct btrfs_node *parent = NULL;
C
Chris Mason 已提交
157
	int parent_slot;
158
	u32 nritems = btrfs_header_nritems(&leaf->header);
C
Chris Mason 已提交
159 160 161 162

	if (path->nodes[level + 1])
		parent = &path->nodes[level + 1]->node;
	parent_slot = path->slots[level + 1];
C
Chris Mason 已提交
163
	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
164 165 166 167 168

	if (nritems == 0)
		return 0;

	if (parent) {
C
Chris Mason 已提交
169
		struct btrfs_disk_key *parent_key;
C
Chris Mason 已提交
170
		parent_key = &parent->ptrs[parent_slot].key;
C
Chris Mason 已提交
171
		BUG_ON(memcmp(parent_key, &leaf->items[0].key,
C
Chris Mason 已提交
172
		       sizeof(struct btrfs_disk_key)));
173
		BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
174
		       btrfs_header_blocknr(&leaf->header));
C
Chris Mason 已提交
175
	}
176
	for (i = 0; nritems > 1 && i < nritems - 2; i++) {
C
Chris Mason 已提交
177 178
		struct btrfs_key cpukey;
		btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
C
Chris Mason 已提交
179
		BUG_ON(comp_keys(&leaf->items[i].key,
C
Chris Mason 已提交
180
		                 &cpukey) >= 0);
C
Chris Mason 已提交
181 182
		BUG_ON(btrfs_item_offset(leaf->items + i) !=
			btrfs_item_end(leaf->items + i + 1));
C
Chris Mason 已提交
183
		if (i == 0) {
C
Chris Mason 已提交
184 185
			BUG_ON(btrfs_item_offset(leaf->items + i) +
			       btrfs_item_size(leaf->items + i) !=
C
Chris Mason 已提交
186
			       BTRFS_LEAF_DATA_SIZE(root));
C
Chris Mason 已提交
187 188 189 190 191
		}
	}
	return 0;
}

C
Chris Mason 已提交
192 193
static int check_block(struct btrfs_root *root, struct btrfs_path *path,
			int level)
C
Chris Mason 已提交
194 195
{
	if (level == 0)
C
Chris Mason 已提交
196 197
		return check_leaf(root, path, level);
	return check_node(root, path, level);
C
Chris Mason 已提交
198 199
}

C
Chris Mason 已提交
200 201 202 203 204 205 206 207 208
/*
 * search for key in the array p.  items p are item_size apart
 * and there are 'max' items in p
 * the slot in the array is returned via slot, and it points to
 * the place where you would insert key if it is not found in
 * the array.
 *
 * slot may point to max if the key is bigger than all of the keys
 */
C
Chris Mason 已提交
209
static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
210 211 212 213 214 215
		       int max, int *slot)
{
	int low = 0;
	int high = max;
	int mid;
	int ret;
C
Chris Mason 已提交
216
	struct btrfs_disk_key *tmp;
217 218 219

	while(low < high) {
		mid = (low + high) / 2;
C
Chris Mason 已提交
220
		tmp = (struct btrfs_disk_key *)(p + mid * item_size);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
		ret = comp_keys(tmp, key);

		if (ret < 0)
			low = mid + 1;
		else if (ret > 0)
			high = mid;
		else {
			*slot = mid;
			return 0;
		}
	}
	*slot = low;
	return 1;
}

C
Chris Mason 已提交
236 237 238 239
/*
 * simple bin_search frontend that does the right thing for
 * leaves vs nodes
 */
C
Chris Mason 已提交
240
static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
241
{
242
	if (btrfs_is_leaf(c)) {
C
Chris Mason 已提交
243
		struct btrfs_leaf *l = (struct btrfs_leaf *)c;
C
Chris Mason 已提交
244 245
		return generic_bin_search((void *)l->items,
					  sizeof(struct btrfs_item),
246 247
					  key, btrfs_header_nritems(&c->header),
					  slot);
248
	} else {
C
Chris Mason 已提交
249 250
		return generic_bin_search((void *)c->ptrs,
					  sizeof(struct btrfs_key_ptr),
251 252
					  key, btrfs_header_nritems(&c->header),
					  slot);
253 254 255 256
	}
	return -1;
}

C
Chris Mason 已提交
257
static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
C
Chris Mason 已提交
258
				   struct btrfs_buffer *parent_buf,
259 260
				   int slot)
{
C
Chris Mason 已提交
261
	struct btrfs_node *node = &parent_buf->node;
262 263
	if (slot < 0)
		return NULL;
264
	if (slot >= btrfs_header_nritems(&node->header))
265
		return NULL;
266
	return read_tree_block(root, btrfs_node_blockptr(node, slot));
267 268
}

C
Chris Mason 已提交
269
static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
270 271
			int level)
{
C
Chris Mason 已提交
272 273 274 275 276 277 278 279
	struct btrfs_buffer *right_buf;
	struct btrfs_buffer *mid_buf;
	struct btrfs_buffer *left_buf;
	struct btrfs_buffer *parent_buf = NULL;
	struct btrfs_node *right = NULL;
	struct btrfs_node *mid;
	struct btrfs_node *left = NULL;
	struct btrfs_node *parent = NULL;
280 281 282 283
	int ret = 0;
	int wret;
	int pslot;
	int orig_slot = path->slots[level];
284
	u64 orig_ptr;
285 286 287 288 289 290

	if (level == 0)
		return 0;

	mid_buf = path->nodes[level];
	mid = &mid_buf->node;
291
	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
292

C
Chris Mason 已提交
293
	if (level < BTRFS_MAX_LEVEL - 1)
294 295 296 297
		parent_buf = path->nodes[level + 1];
	pslot = path->slots[level + 1];

	if (!parent_buf) {
C
Chris Mason 已提交
298
		struct btrfs_buffer *child;
299 300
		u64 blocknr = mid_buf->blocknr;

301
		if (btrfs_header_nritems(&mid->header) != 1)
302 303 304 305 306 307 308 309
			return 0;

		/* promote the child to a root */
		child = read_node_slot(root, mid_buf, 0);
		BUG_ON(!child);
		root->node = child;
		path->nodes[level] = NULL;
		/* once for the path */
C
Chris Mason 已提交
310
		btrfs_block_release(root, mid_buf);
311
		/* once for the root ptr */
C
Chris Mason 已提交
312
		btrfs_block_release(root, mid_buf);
313
		clean_tree_block(root, mid_buf);
C
Chris Mason 已提交
314
		return btrfs_free_extent(root, blocknr, 1);
315 316 317
	}
	parent = &parent_buf->node;

C
Chris Mason 已提交
318 319
	if (btrfs_header_nritems(&mid->header) >
	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
320 321 322 323
		return 0;

	left_buf = read_node_slot(root, parent_buf, pslot - 1);
	right_buf = read_node_slot(root, parent_buf, pslot + 1);
324 325

	/* first, try to make some room in the middle buffer */
326
	if (left_buf) {
C
Chris Mason 已提交
327 328
		btrfs_cow_block(root, left_buf, parent_buf,
				pslot - 1, &left_buf);
329
		left = &left_buf->node;
330
		orig_slot += btrfs_header_nritems(&left->header);
331 332 333
		wret = push_node_left(root, left_buf, mid_buf);
		if (wret < 0)
			ret = wret;
334
	}
335 336 337 338

	/*
	 * then try to empty the right most buffer into the middle
	 */
339
	if (right_buf) {
C
Chris Mason 已提交
340 341
		btrfs_cow_block(root, right_buf, parent_buf,
				pslot + 1, &right_buf);
342 343 344 345
		right = &right_buf->node;
		wret = push_node_left(root, mid_buf, right_buf);
		if (wret < 0)
			ret = wret;
346
		if (btrfs_header_nritems(&right->header) == 0) {
347
			u64 blocknr = right_buf->blocknr;
C
Chris Mason 已提交
348
			btrfs_block_release(root, right_buf);
349
			clean_tree_block(root, right_buf);
350 351 352 353 354
			right_buf = NULL;
			right = NULL;
			wret = del_ptr(root, path, level + 1, pslot + 1);
			if (wret)
				ret = wret;
C
Chris Mason 已提交
355
			wret = btrfs_free_extent(root, blocknr, 1);
356 357 358
			if (wret)
				ret = wret;
		} else {
C
Chris Mason 已提交
359 360
			memcpy(&parent->ptrs[pslot + 1].key,
				&right->ptrs[0].key,
C
Chris Mason 已提交
361
				sizeof(struct btrfs_disk_key));
C
Chris Mason 已提交
362
			BUG_ON(list_empty(&parent_buf->dirty));
363 364
		}
	}
365
	if (btrfs_header_nritems(&mid->header) == 1) {
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		/*
		 * we're not allowed to leave a node with one item in the
		 * tree during a delete.  A deletion from lower in the tree
		 * could try to delete the only pointer in this node.
		 * So, pull some keys from the left.
		 * There has to be a left pointer at this point because
		 * otherwise we would have pulled some pointers from the
		 * right
		 */
		BUG_ON(!left_buf);
		wret = balance_node_right(root, mid_buf, left_buf);
		if (wret < 0)
			ret = wret;
		BUG_ON(wret == 1);
	}
381
	if (btrfs_header_nritems(&mid->header) == 0) {
382
		/* we've managed to empty the middle node, drop it */
383
		u64 blocknr = mid_buf->blocknr;
C
Chris Mason 已提交
384
		btrfs_block_release(root, mid_buf);
385
		clean_tree_block(root, mid_buf);
386 387 388 389 390
		mid_buf = NULL;
		mid = NULL;
		wret = del_ptr(root, path, level + 1, pslot);
		if (wret)
			ret = wret;
C
Chris Mason 已提交
391
		wret = btrfs_free_extent(root, blocknr, 1);
392 393
		if (wret)
			ret = wret;
394 395
	} else {
		/* update the parent key to reflect our changes */
C
Chris Mason 已提交
396
		memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
C
Chris Mason 已提交
397
		       sizeof(struct btrfs_disk_key));
C
Chris Mason 已提交
398
		BUG_ON(list_empty(&parent_buf->dirty));
399
	}
400

401
	/* update the path */
402
	if (left_buf) {
403
		if (btrfs_header_nritems(&left->header) > orig_slot) {
404 405 406 407 408
			left_buf->count++; // released below
			path->nodes[level] = left_buf;
			path->slots[level + 1] -= 1;
			path->slots[level] = orig_slot;
			if (mid_buf)
C
Chris Mason 已提交
409
				btrfs_block_release(root, mid_buf);
410
		} else {
411
			orig_slot -= btrfs_header_nritems(&left->header);
412 413 414
			path->slots[level] = orig_slot;
		}
	}
415
	/* double check we haven't messed things up */
C
Chris Mason 已提交
416
	check_block(root, path, level);
417 418
	if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
					    path->slots[level]))
419
		BUG();
420 421

	if (right_buf)
C
Chris Mason 已提交
422
		btrfs_block_release(root, right_buf);
423
	if (left_buf)
C
Chris Mason 已提交
424
		btrfs_block_release(root, left_buf);
425 426 427
	return ret;
}

C
Chris Mason 已提交
428 429 430 431 432 433
/*
 * look for key in the tree.  path is filled in with nodes along the way
 * if key is found, we return zero and you can find the item in the leaf
 * level of the path (level 0)
 *
 * If the key isn't found, the path points to the slot where it should
C
Chris Mason 已提交
434 435
 * be inserted, and 1 is returned.  If there are other errors during the
 * search a negative error number is returned.
C
Chris Mason 已提交
436 437 438 439
 *
 * if ins_len > 0, nodes and leaves will be split as we walk down the
 * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
 * possible)
C
Chris Mason 已提交
440
 */
C
Chris Mason 已提交
441 442
int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
		struct btrfs_path *p, int ins_len, int cow)
443
{
C
Chris Mason 已提交
444 445 446
	struct btrfs_buffer *b;
	struct btrfs_buffer *cow_buf;
	struct btrfs_node *c;
447 448 449
	int slot;
	int ret;
	int level;
C
Chris Mason 已提交
450

451 452
again:
	b = root->node;
453 454
	b->count++;
	while (b) {
455
		level = btrfs_header_level(&b->node.header);
C
Chris Mason 已提交
456 457 458 459 460 461 462
		if (cow) {
			int wret;
			wret = btrfs_cow_block(root, b, p->nodes[level + 1],
					       p->slots[level + 1], &cow_buf);
			b = cow_buf;
		}
		BUG_ON(!cow && ins_len);
463 464
		c = &b->node;
		p->nodes[level] = b;
C
Chris Mason 已提交
465
		ret = check_block(root, p, level);
C
Chris Mason 已提交
466 467
		if (ret)
			return -1;
468
		ret = bin_search(c, key, &slot);
469
		if (!btrfs_is_leaf(c)) {
470 471 472
			if (ret && slot > 0)
				slot -= 1;
			p->slots[level] = slot;
473
			if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
C
Chris Mason 已提交
474
			    BTRFS_NODEPTRS_PER_BLOCK(root)) {
C
Chris Mason 已提交
475 476 477 478 479 480 481
				int sret = split_node(root, p, level);
				BUG_ON(sret > 0);
				if (sret)
					return sret;
				b = p->nodes[level];
				c = &b->node;
				slot = p->slots[level];
482 483 484 485 486 487 488 489 490
			} else if (ins_len < 0) {
				int sret = balance_level(root, p, level);
				if (sret)
					return sret;
				b = p->nodes[level];
				if (!b)
					goto again;
				c = &b->node;
				slot = p->slots[level];
491
				BUG_ON(btrfs_header_nritems(&c->header) == 1);
C
Chris Mason 已提交
492
			}
493
			b = read_tree_block(root, btrfs_node_blockptr(c, slot));
494
		} else {
C
Chris Mason 已提交
495
			struct btrfs_leaf *l = (struct btrfs_leaf *)c;
496
			p->slots[level] = slot;
C
Chris Mason 已提交
497
			if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
C
Chris Mason 已提交
498
			    sizeof(struct btrfs_item) + ins_len) {
C
Chris Mason 已提交
499 500 501 502 503
				int sret = split_leaf(root, p, ins_len);
				BUG_ON(sret > 0);
				if (sret)
					return sret;
			}
504
			BUG_ON(root->node->count == 1);
505 506 507
			return ret;
		}
	}
508
	BUG_ON(root->node->count == 1);
C
Chris Mason 已提交
509
	return 1;
510 511
}

C
Chris Mason 已提交
512 513 514 515 516 517
/*
 * adjust the pointers going up the tree, starting at level
 * making sure the right key of each node is points to 'key'.
 * This is used after shifting pointers to the left, so it stops
 * fixing up pointers when a given leaf/node is not in slot 0 of the
 * higher levels
C
Chris Mason 已提交
518 519 520
 *
 * If this fails to write a tree block, it returns -1, but continues
 * fixing up the blocks in ram so the tree is consistent.
C
Chris Mason 已提交
521
 */
C
Chris Mason 已提交
522 523
static int fixup_low_keys(struct btrfs_root *root,
			   struct btrfs_path *path, struct btrfs_disk_key *key,
524
			   int level)
525 526
{
	int i;
C
Chris Mason 已提交
527
	int ret = 0;
C
Chris Mason 已提交
528 529
	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
		struct btrfs_node *t;
530
		int tslot = path->slots[i];
531
		if (!path->nodes[i])
532
			break;
533
		t = &path->nodes[i]->node;
C
Chris Mason 已提交
534
		memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
C
Chris Mason 已提交
535
		BUG_ON(list_empty(&path->nodes[i]->dirty));
536 537 538
		if (tslot != 0)
			break;
	}
C
Chris Mason 已提交
539
	return ret;
540 541
}

C
Chris Mason 已提交
542 543
/*
 * try to push data from one node into the next node left in the
544
 * tree.
C
Chris Mason 已提交
545 546 547
 *
 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
 * error, and > 0 if there was no room in the left hand block.
C
Chris Mason 已提交
548
 */
C
Chris Mason 已提交
549 550
static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
			  struct btrfs_buffer *src_buf)
551
{
C
Chris Mason 已提交
552 553
	struct btrfs_node *src = &src_buf->node;
	struct btrfs_node *dst = &dst_buf->node;
554
	int push_items = 0;
555 556
	int src_nritems;
	int dst_nritems;
C
Chris Mason 已提交
557
	int ret = 0;
558

559 560
	src_nritems = btrfs_header_nritems(&src->header);
	dst_nritems = btrfs_header_nritems(&dst->header);
C
Chris Mason 已提交
561
	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
562
	if (push_items <= 0) {
563
		return 1;
564
	}
565

566
	if (src_nritems < push_items)
567 568
		push_items = src_nritems;

C
Chris Mason 已提交
569 570
	memcpy(dst->ptrs + dst_nritems, src->ptrs,
		push_items * sizeof(struct btrfs_key_ptr));
571
	if (push_items < src_nritems) {
C
Chris Mason 已提交
572
		memmove(src->ptrs, src->ptrs + push_items,
C
Chris Mason 已提交
573
			(src_nritems - push_items) *
C
Chris Mason 已提交
574
			sizeof(struct btrfs_key_ptr));
575
	}
576 577
	btrfs_set_header_nritems(&src->header, src_nritems - push_items);
	btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
C
Chris Mason 已提交
578 579
	BUG_ON(list_empty(&src_buf->dirty));
	BUG_ON(list_empty(&dst_buf->dirty));
580 581 582 583 584 585 586 587 588 589 590 591
	return ret;
}

/*
 * try to push data from one node into the next node right in the
 * tree.
 *
 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
 * error, and > 0 if there was no room in the right hand block.
 *
 * this will  only push up to 1/2 the contents of the left node over
 */
C
Chris Mason 已提交
592 593 594
static int balance_node_right(struct btrfs_root *root,
			      struct btrfs_buffer *dst_buf,
			      struct btrfs_buffer *src_buf)
595
{
C
Chris Mason 已提交
596 597
	struct btrfs_node *src = &src_buf->node;
	struct btrfs_node *dst = &dst_buf->node;
598 599 600 601 602 603
	int push_items = 0;
	int max_push;
	int src_nritems;
	int dst_nritems;
	int ret = 0;

604 605
	src_nritems = btrfs_header_nritems(&src->header);
	dst_nritems = btrfs_header_nritems(&dst->header);
C
Chris Mason 已提交
606
	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
607 608 609 610 611 612 613 614 615 616 617
	if (push_items <= 0) {
		return 1;
	}

	max_push = src_nritems / 2 + 1;
	/* don't try to empty the node */
	if (max_push > src_nritems)
		return 1;
	if (max_push < push_items)
		push_items = max_push;

C
Chris Mason 已提交
618 619 620 621
	memmove(dst->ptrs + push_items, dst->ptrs,
		dst_nritems * sizeof(struct btrfs_key_ptr));
	memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
		push_items * sizeof(struct btrfs_key_ptr));
622

623 624
	btrfs_set_header_nritems(&src->header, src_nritems - push_items);
	btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
625

C
Chris Mason 已提交
626 627
	BUG_ON(list_empty(&src_buf->dirty));
	BUG_ON(list_empty(&dst_buf->dirty));
C
Chris Mason 已提交
628
	return ret;
629 630
}

C
Chris Mason 已提交
631 632 633 634
/*
 * helper function to insert a new root level in the tree.
 * A new node is allocated, and a single item is inserted to
 * point to the existing root
C
Chris Mason 已提交
635 636
 *
 * returns zero on success or < 0 on failure.
C
Chris Mason 已提交
637
 */
C
Chris Mason 已提交
638 639
static int insert_new_root(struct btrfs_root *root,
			   struct btrfs_path *path, int level)
C
Chris Mason 已提交
640
{
C
Chris Mason 已提交
641 642 643
	struct btrfs_buffer *t;
	struct btrfs_node *lower;
	struct btrfs_node *c;
C
Chris Mason 已提交
644
	struct btrfs_disk_key *lower_key;
C
Chris Mason 已提交
645 646 647 648

	BUG_ON(path->nodes[level]);
	BUG_ON(path->nodes[level-1] != root->node);

C
Chris Mason 已提交
649
	t = btrfs_alloc_free_block(root);
C
Chris Mason 已提交
650
	c = &t->node;
C
Chris Mason 已提交
651
	memset(c, 0, root->blocksize);
652 653 654 655 656
	btrfs_set_header_nritems(&c->header, 1);
	btrfs_set_header_level(&c->header, level);
	btrfs_set_header_blocknr(&c->header, t->blocknr);
	btrfs_set_header_parentid(&c->header,
	                       btrfs_header_parentid(&root->node->node.header));
C
Chris Mason 已提交
657
	lower = &path->nodes[level-1]->node;
658
	if (btrfs_is_leaf(lower))
C
Chris Mason 已提交
659
		lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
C
Chris Mason 已提交
660
	else
C
Chris Mason 已提交
661 662
		lower_key = &lower->ptrs[0].key;
	memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
663
	btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
C
Chris Mason 已提交
664
	/* the super has an extra ref to root->node */
C
Chris Mason 已提交
665
	btrfs_block_release(root, root->node);
C
Chris Mason 已提交
666 667 668 669 670 671 672
	root->node = t;
	t->count++;
	path->nodes[level] = t;
	path->slots[level] = 0;
	return 0;
}

C
Chris Mason 已提交
673 674 675
/*
 * worker function to insert a single pointer in a node.
 * the node should have enough room for the pointer already
C
Chris Mason 已提交
676
 *
C
Chris Mason 已提交
677 678
 * slot and level indicate where you want the key to go, and
 * blocknr is the block the key points to.
C
Chris Mason 已提交
679 680
 *
 * returns zero on success and < 0 on any error
C
Chris Mason 已提交
681
 */
C
Chris Mason 已提交
682 683
static int insert_ptr(struct btrfs_root *root,
		struct btrfs_path *path, struct btrfs_disk_key *key,
C
Chris Mason 已提交
684 685
		u64 blocknr, int slot, int level)
{
C
Chris Mason 已提交
686
	struct btrfs_node *lower;
C
Chris Mason 已提交
687
	int nritems;
C
Chris Mason 已提交
688 689

	BUG_ON(!path->nodes[level]);
C
Chris Mason 已提交
690
	lower = &path->nodes[level]->node;
691
	nritems = btrfs_header_nritems(&lower->header);
C
Chris Mason 已提交
692 693
	if (slot > nritems)
		BUG();
C
Chris Mason 已提交
694
	if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
C
Chris Mason 已提交
695 696
		BUG();
	if (slot != nritems) {
C
Chris Mason 已提交
697 698
		memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
			(nritems - slot) * sizeof(struct btrfs_key_ptr));
C
Chris Mason 已提交
699
	}
C
Chris Mason 已提交
700
	memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
701
	btrfs_set_node_blockptr(lower, slot, blocknr);
702
	btrfs_set_header_nritems(&lower->header, nritems + 1);
C
Chris Mason 已提交
703
	BUG_ON(list_empty(&path->nodes[level]->dirty));
C
Chris Mason 已提交
704 705 706
	return 0;
}

C
Chris Mason 已提交
707 708 709 710 711 712
/*
 * split the node at the specified level in path in two.
 * The path is corrected to point to the appropriate node after the split
 *
 * Before splitting this tries to make some room in the node by pushing
 * left and right, if either one works, it returns right away.
C
Chris Mason 已提交
713 714
 *
 * returns 0 on success and < 0 on failure
C
Chris Mason 已提交
715
 */
C
Chris Mason 已提交
716
static int split_node(struct btrfs_root *root, struct btrfs_path *path,
C
Chris Mason 已提交
717
		      int level)
718
{
C
Chris Mason 已提交
719 720 721 722
	struct btrfs_buffer *t;
	struct btrfs_node *c;
	struct btrfs_buffer *split_buffer;
	struct btrfs_node *split;
723
	int mid;
C
Chris Mason 已提交
724
	int ret;
C
Chris Mason 已提交
725
	int wret;
726
	u32 c_nritems;
727

C
Chris Mason 已提交
728 729 730 731 732 733 734
	t = path->nodes[level];
	c = &t->node;
	if (t == root->node) {
		/* trying to split the root, lets make a new one */
		ret = insert_new_root(root, path, level + 1);
		if (ret)
			return ret;
735
	}
736
	c_nritems = btrfs_header_nritems(&c->header);
C
Chris Mason 已提交
737
	split_buffer = btrfs_alloc_free_block(root);
C
Chris Mason 已提交
738
	split = &split_buffer->node;
739 740 741 742 743
	btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
	btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
	btrfs_set_header_parentid(&split->header,
	                       btrfs_header_parentid(&root->node->node.header));
	mid = (c_nritems + 1) / 2;
C
Chris Mason 已提交
744 745
	memcpy(split->ptrs, c->ptrs + mid,
		(c_nritems - mid) * sizeof(struct btrfs_key_ptr));
746 747
	btrfs_set_header_nritems(&split->header, c_nritems - mid);
	btrfs_set_header_nritems(&c->header, mid);
C
Chris Mason 已提交
748 749
	ret = 0;

C
Chris Mason 已提交
750
	BUG_ON(list_empty(&t->dirty));
C
Chris Mason 已提交
751 752 753
	wret = insert_ptr(root, path, &split->ptrs[0].key,
			  split_buffer->blocknr, path->slots[level + 1] + 1,
			  level + 1);
C
Chris Mason 已提交
754 755 756
	if (wret)
		ret = wret;

C
Chris Mason 已提交
757
	if (path->slots[level] >= mid) {
C
Chris Mason 已提交
758
		path->slots[level] -= mid;
C
Chris Mason 已提交
759
		btrfs_block_release(root, t);
C
Chris Mason 已提交
760 761 762
		path->nodes[level] = split_buffer;
		path->slots[level + 1] += 1;
	} else {
C
Chris Mason 已提交
763
		btrfs_block_release(root, split_buffer);
764
	}
C
Chris Mason 已提交
765
	return ret;
766 767
}

C
Chris Mason 已提交
768 769 770 771 772
/*
 * how many bytes are required to store the items in a leaf.  start
 * and nr indicate which items in the leaf to check.  This totals up the
 * space used both by the item structs and the item data
 */
C
Chris Mason 已提交
773
static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
774 775 776 777 778 779
{
	int data_len;
	int end = start + nr - 1;

	if (!nr)
		return 0;
C
Chris Mason 已提交
780 781 782
	data_len = btrfs_item_end(l->items + start);
	data_len = data_len - btrfs_item_offset(l->items + end);
	data_len += sizeof(struct btrfs_item) * nr;
783 784 785
	return data_len;
}

C
Chris Mason 已提交
786 787 788
/*
 * push some data in the path leaf to the right, trying to free up at
 * least data_size bytes.  returns zero if the push worked, nonzero otherwise
C
Chris Mason 已提交
789 790 791
 *
 * returns 1 if the push failed because the other node didn't have enough
 * room, 0 if everything worked out and < 0 if there were major errors.
C
Chris Mason 已提交
792
 */
C
Chris Mason 已提交
793
static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
C
Chris Mason 已提交
794
			   int data_size)
C
Chris Mason 已提交
795
{
C
Chris Mason 已提交
796 797 798 799 800
	struct btrfs_buffer *left_buf = path->nodes[0];
	struct btrfs_leaf *left = &left_buf->leaf;
	struct btrfs_leaf *right;
	struct btrfs_buffer *right_buf;
	struct btrfs_buffer *upper;
C
Chris Mason 已提交
801 802 803 804 805
	int slot;
	int i;
	int free_space;
	int push_space = 0;
	int push_items = 0;
C
Chris Mason 已提交
806
	struct btrfs_item *item;
807 808
	u32 left_nritems;
	u32 right_nritems;
C
Chris Mason 已提交
809 810 811 812 813 814

	slot = path->slots[1];
	if (!path->nodes[1]) {
		return 1;
	}
	upper = path->nodes[1];
815
	if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
C
Chris Mason 已提交
816 817
		return 1;
	}
818 819
	right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
							      slot + 1));
C
Chris Mason 已提交
820
	right = &right_buf->leaf;
C
Chris Mason 已提交
821
	free_space = btrfs_leaf_free_space(root, right);
C
Chris Mason 已提交
822
	if (free_space < data_size + sizeof(struct btrfs_item)) {
C
Chris Mason 已提交
823
		btrfs_block_release(root, right_buf);
C
Chris Mason 已提交
824 825
		return 1;
	}
C
Chris Mason 已提交
826 827 828
	/* cow and double check */
	btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
	right = &right_buf->leaf;
C
Chris Mason 已提交
829
	free_space = btrfs_leaf_free_space(root, right);
C
Chris Mason 已提交
830
	if (free_space < data_size + sizeof(struct btrfs_item)) {
C
Chris Mason 已提交
831
		btrfs_block_release(root, right_buf);
C
Chris Mason 已提交
832 833 834
		return 1;
	}

835 836
	left_nritems = btrfs_header_nritems(&left->header);
	for (i = left_nritems - 1; i >= 0; i--) {
C
Chris Mason 已提交
837 838 839
		item = left->items + i;
		if (path->slots[0] == i)
			push_space += data_size + sizeof(*item);
C
Chris Mason 已提交
840 841
		if (btrfs_item_size(item) + sizeof(*item) + push_space >
		    free_space)
C
Chris Mason 已提交
842 843
			break;
		push_items++;
C
Chris Mason 已提交
844
		push_space += btrfs_item_size(item) + sizeof(*item);
C
Chris Mason 已提交
845 846
	}
	if (push_items == 0) {
C
Chris Mason 已提交
847
		btrfs_block_release(root, right_buf);
C
Chris Mason 已提交
848 849
		return 1;
	}
850
	right_nritems = btrfs_header_nritems(&right->header);
C
Chris Mason 已提交
851
	/* push left to right */
C
Chris Mason 已提交
852
	push_space = btrfs_item_end(left->items + left_nritems - push_items);
C
Chris Mason 已提交
853
	push_space -= leaf_data_end(root, left);
C
Chris Mason 已提交
854
	/* make room in the right data area */
C
Chris Mason 已提交
855 856 857
	memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
		push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
		BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
C
Chris Mason 已提交
858
	/* copy from the left data area */
C
Chris Mason 已提交
859 860
	memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
		btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
C
Chris Mason 已提交
861
	memmove(right->items + push_items, right->items,
C
Chris Mason 已提交
862
		right_nritems * sizeof(struct btrfs_item));
C
Chris Mason 已提交
863
	/* copy the items from left to right */
864
	memcpy(right->items, left->items + left_nritems - push_items,
C
Chris Mason 已提交
865
		push_items * sizeof(struct btrfs_item));
C
Chris Mason 已提交
866 867

	/* update the item pointers */
868 869
	right_nritems += push_items;
	btrfs_set_header_nritems(&right->header, right_nritems);
C
Chris Mason 已提交
870
	push_space = BTRFS_LEAF_DATA_SIZE(root);
871
	for (i = 0; i < right_nritems; i++) {
C
Chris Mason 已提交
872 873 874
		btrfs_set_item_offset(right->items + i, push_space -
				      btrfs_item_size(right->items + i));
		push_space = btrfs_item_offset(right->items + i);
C
Chris Mason 已提交
875
	}
876 877
	left_nritems -= push_items;
	btrfs_set_header_nritems(&left->header, left_nritems);
C
Chris Mason 已提交
878

C
Chris Mason 已提交
879 880
	BUG_ON(list_empty(&left_buf->dirty));
	BUG_ON(list_empty(&right_buf->dirty));
C
Chris Mason 已提交
881
	memcpy(&upper->node.ptrs[slot + 1].key,
C
Chris Mason 已提交
882
		&right->items[0].key, sizeof(struct btrfs_disk_key));
C
Chris Mason 已提交
883 884
	BUG_ON(list_empty(&upper->dirty));

C
Chris Mason 已提交
885
	/* then fixup the leaf pointer in the path */
886 887
	if (path->slots[0] >= left_nritems) {
		path->slots[0] -= left_nritems;
C
Chris Mason 已提交
888
		btrfs_block_release(root, path->nodes[0]);
C
Chris Mason 已提交
889 890 891
		path->nodes[0] = right_buf;
		path->slots[1] += 1;
	} else {
C
Chris Mason 已提交
892
		btrfs_block_release(root, right_buf);
C
Chris Mason 已提交
893 894 895
	}
	return 0;
}
C
Chris Mason 已提交
896 897 898 899
/*
 * push some data in the path leaf to the left, trying to free up at
 * least data_size bytes.  returns zero if the push worked, nonzero otherwise
 */
C
Chris Mason 已提交
900
static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
C
Chris Mason 已提交
901
			  int data_size)
902
{
C
Chris Mason 已提交
903 904 905 906
	struct btrfs_buffer *right_buf = path->nodes[0];
	struct btrfs_leaf *right = &right_buf->leaf;
	struct btrfs_buffer *t;
	struct btrfs_leaf *left;
907 908 909 910 911
	int slot;
	int i;
	int free_space;
	int push_space = 0;
	int push_items = 0;
C
Chris Mason 已提交
912
	struct btrfs_item *item;
913
	u32 old_left_nritems;
C
Chris Mason 已提交
914 915
	int ret = 0;
	int wret;
916 917 918 919 920 921 922 923

	slot = path->slots[1];
	if (slot == 0) {
		return 1;
	}
	if (!path->nodes[1]) {
		return 1;
	}
924 925
	t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
						      slot - 1));
926
	left = &t->leaf;
C
Chris Mason 已提交
927
	free_space = btrfs_leaf_free_space(root, left);
C
Chris Mason 已提交
928
	if (free_space < data_size + sizeof(struct btrfs_item)) {
C
Chris Mason 已提交
929
		btrfs_block_release(root, t);
930 931
		return 1;
	}
C
Chris Mason 已提交
932 933 934 935

	/* cow and double check */
	btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
	left = &t->leaf;
C
Chris Mason 已提交
936
	free_space = btrfs_leaf_free_space(root, left);
C
Chris Mason 已提交
937
	if (free_space < data_size + sizeof(struct btrfs_item)) {
C
Chris Mason 已提交
938
		btrfs_block_release(root, t);
C
Chris Mason 已提交
939 940 941
		return 1;
	}

942
	for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
943 944 945
		item = right->items + i;
		if (path->slots[0] == i)
			push_space += data_size + sizeof(*item);
C
Chris Mason 已提交
946 947
		if (btrfs_item_size(item) + sizeof(*item) + push_space >
		    free_space)
948 949
			break;
		push_items++;
C
Chris Mason 已提交
950
		push_space += btrfs_item_size(item) + sizeof(*item);
951 952
	}
	if (push_items == 0) {
C
Chris Mason 已提交
953
		btrfs_block_release(root, t);
954 955 956
		return 1;
	}
	/* push data from right to left */
957
	memcpy(left->items + btrfs_header_nritems(&left->header),
C
Chris Mason 已提交
958
		right->items, push_items * sizeof(struct btrfs_item));
C
Chris Mason 已提交
959
	push_space = BTRFS_LEAF_DATA_SIZE(root) -
C
Chris Mason 已提交
960
		     btrfs_item_offset(right->items + push_items -1);
C
Chris Mason 已提交
961 962 963
	memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
		btrfs_leaf_data(right) +
		btrfs_item_offset(right->items + push_items - 1),
964
		push_space);
965
	old_left_nritems = btrfs_header_nritems(&left->header);
966 967
	BUG_ON(old_left_nritems < 0);

C
Chris Mason 已提交
968
	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
C
Chris Mason 已提交
969 970 971
		u32 ioff = btrfs_item_offset(left->items + i);
		btrfs_set_item_offset(left->items + i, ioff -
				     (BTRFS_LEAF_DATA_SIZE(root) -
C
Chris Mason 已提交
972 973
				      btrfs_item_offset(left->items +
						        old_left_nritems - 1)));
974
	}
975
	btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
976 977

	/* fixup right node */
C
Chris Mason 已提交
978
	push_space = btrfs_item_offset(right->items + push_items - 1) -
C
Chris Mason 已提交
979 980 981 982
		     leaf_data_end(root, right);
	memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
		push_space, btrfs_leaf_data(right) +
		leaf_data_end(root, right), push_space);
983
	memmove(right->items, right->items + push_items,
984
		(btrfs_header_nritems(&right->header) - push_items) *
C
Chris Mason 已提交
985
		sizeof(struct btrfs_item));
986 987 988
	btrfs_set_header_nritems(&right->header,
				 btrfs_header_nritems(&right->header) -
				 push_items);
C
Chris Mason 已提交
989
	push_space = BTRFS_LEAF_DATA_SIZE(root);
990

991
	for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
C
Chris Mason 已提交
992 993 994
		btrfs_set_item_offset(right->items + i, push_space -
				      btrfs_item_size(right->items + i));
		push_space = btrfs_item_offset(right->items + i);
995
	}
996

C
Chris Mason 已提交
997 998
	BUG_ON(list_empty(&t->dirty));
	BUG_ON(list_empty(&right_buf->dirty));
999

C
Chris Mason 已提交
1000 1001 1002
	wret = fixup_low_keys(root, path, &right->items[0].key, 1);
	if (wret)
		ret = wret;
1003 1004 1005 1006

	/* then fixup the leaf pointer in the path */
	if (path->slots[0] < push_items) {
		path->slots[0] += old_left_nritems;
C
Chris Mason 已提交
1007
		btrfs_block_release(root, path->nodes[0]);
1008
		path->nodes[0] = t;
1009 1010
		path->slots[1] -= 1;
	} else {
C
Chris Mason 已提交
1011
		btrfs_block_release(root, t);
1012 1013
		path->slots[0] -= push_items;
	}
1014
	BUG_ON(path->slots[0] < 0);
C
Chris Mason 已提交
1015
	return ret;
1016 1017
}

C
Chris Mason 已提交
1018 1019 1020
/*
 * split the path's leaf in two, making sure there is at least data_size
 * available for the resulting leaf level of the path.
C
Chris Mason 已提交
1021 1022
 *
 * returns 0 if all went well and < 0 on failure.
C
Chris Mason 已提交
1023
 */
C
Chris Mason 已提交
1024
static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
C
Chris Mason 已提交
1025
		      int data_size)
1026
{
C
Chris Mason 已提交
1027 1028
	struct btrfs_buffer *l_buf;
	struct btrfs_leaf *l;
1029
	u32 nritems;
1030 1031
	int mid;
	int slot;
C
Chris Mason 已提交
1032 1033
	struct btrfs_leaf *right;
	struct btrfs_buffer *right_buffer;
C
Chris Mason 已提交
1034
	int space_needed = data_size + sizeof(struct btrfs_item);
1035 1036 1037 1038
	int data_copy_size;
	int rt_data_off;
	int i;
	int ret;
C
Chris Mason 已提交
1039 1040
	int wret;

C
Chris Mason 已提交
1041 1042 1043 1044 1045 1046 1047 1048
	wret = push_leaf_left(root, path, data_size);
	if (wret < 0)
		return wret;
	if (wret) {
		wret = push_leaf_right(root, path, data_size);
		if (wret < 0)
			return wret;
	}
C
Chris Mason 已提交
1049 1050 1051 1052
	l_buf = path->nodes[0];
	l = &l_buf->leaf;

	/* did the pushes work? */
C
Chris Mason 已提交
1053 1054
	if (btrfs_leaf_free_space(root, l) >=
	    sizeof(struct btrfs_item) + data_size)
C
Chris Mason 已提交
1055 1056
		return 0;

C
Chris Mason 已提交
1057 1058 1059 1060 1061
	if (!path->nodes[1]) {
		ret = insert_new_root(root, path, 1);
		if (ret)
			return ret;
	}
1062
	slot = path->slots[0];
1063
	nritems = btrfs_header_nritems(&l->header);
1064
	mid = (nritems + 1)/ 2;
C
Chris Mason 已提交
1065
	right_buffer = btrfs_alloc_free_block(root);
1066 1067 1068
	BUG_ON(!right_buffer);
	BUG_ON(mid == nritems);
	right = &right_buffer->leaf;
C
Chris Mason 已提交
1069
	memset(&right->header, 0, sizeof(right->header));
1070
	if (mid <= slot) {
C
Chris Mason 已提交
1071
		/* FIXME, just alloc a new leaf here */
1072
		if (leaf_space_used(l, mid, nritems - mid) + space_needed >
C
Chris Mason 已提交
1073
			BTRFS_LEAF_DATA_SIZE(root))
1074 1075
			BUG();
	} else {
C
Chris Mason 已提交
1076
		/* FIXME, just alloc a new leaf here */
1077
		if (leaf_space_used(l, 0, mid + 1) + space_needed >
C
Chris Mason 已提交
1078
			BTRFS_LEAF_DATA_SIZE(root))
1079 1080
			BUG();
	}
1081 1082 1083 1084 1085
	btrfs_set_header_nritems(&right->header, nritems - mid);
	btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
	btrfs_set_header_level(&right->header, 0);
	btrfs_set_header_parentid(&right->header,
	                       btrfs_header_parentid(&root->node->node.header));
C
Chris Mason 已提交
1086 1087
	data_copy_size = btrfs_item_end(l->items + mid) -
			 leaf_data_end(root, l);
1088
	memcpy(right->items, l->items + mid,
C
Chris Mason 已提交
1089
	       (nritems - mid) * sizeof(struct btrfs_item));
C
Chris Mason 已提交
1090 1091 1092 1093 1094
	memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
		data_copy_size, btrfs_leaf_data(l) +
		leaf_data_end(root, l), data_copy_size);
	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
		      btrfs_item_end(l->items + mid);
C
Chris Mason 已提交
1095

C
Chris Mason 已提交
1096
	for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
C
Chris Mason 已提交
1097
		u32 ioff = btrfs_item_offset(right->items + i);
C
Chris Mason 已提交
1098 1099
		btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
	}
C
Chris Mason 已提交
1100

1101
	btrfs_set_header_nritems(&l->header, mid);
C
Chris Mason 已提交
1102 1103
	ret = 0;
	wret = insert_ptr(root, path, &right->items[0].key,
C
Chris Mason 已提交
1104
			  right_buffer->blocknr, path->slots[1] + 1, 1);
C
Chris Mason 已提交
1105 1106
	if (wret)
		ret = wret;
C
Chris Mason 已提交
1107 1108
	BUG_ON(list_empty(&right_buffer->dirty));
	BUG_ON(list_empty(&l_buf->dirty));
1109
	BUG_ON(path->slots[0] != slot);
1110
	if (mid <= slot) {
C
Chris Mason 已提交
1111
		btrfs_block_release(root, path->nodes[0]);
1112
		path->nodes[0] = right_buffer;
1113 1114
		path->slots[0] -= mid;
		path->slots[1] += 1;
1115
	} else
C
Chris Mason 已提交
1116
		btrfs_block_release(root, right_buffer);
1117
	BUG_ON(path->slots[0] < 0);
1118 1119 1120
	return ret;
}

C
Chris Mason 已提交
1121 1122 1123 1124
/*
 * Given a key and some data, insert an item into the tree.
 * This does all the path init required, making room in the tree if needed.
 */
1125 1126
int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
			    struct btrfs_key *cpu_key, u32 data_size)
1127
{
C
Chris Mason 已提交
1128
	int ret = 0;
1129
	int slot;
1130
	int slot_orig;
C
Chris Mason 已提交
1131 1132
	struct btrfs_leaf *leaf;
	struct btrfs_buffer *leaf_buf;
1133
	u32 nritems;
1134
	unsigned int data_end;
C
Chris Mason 已提交
1135 1136 1137
	struct btrfs_disk_key disk_key;

	btrfs_cpu_key_to_disk(&disk_key, cpu_key);
1138

C
Chris Mason 已提交
1139
	/* create a root if there isn't one */
C
Chris Mason 已提交
1140
	if (!root->node)
C
Chris Mason 已提交
1141
		BUG();
1142
	ret = btrfs_search_slot(root, cpu_key, path, data_size, 1);
1143
	if (ret == 0) {
1144
		btrfs_release_path(root, path);
1145
		return -EEXIST;
C
Chris Mason 已提交
1146
	}
1147 1148
	if (ret < 0)
		goto out;
1149

1150 1151
	slot_orig = path->slots[0];
	leaf_buf = path->nodes[0];
1152
	leaf = &leaf_buf->leaf;
C
Chris Mason 已提交
1153

1154
	nritems = btrfs_header_nritems(&leaf->header);
C
Chris Mason 已提交
1155
	data_end = leaf_data_end(root, leaf);
1156

C
Chris Mason 已提交
1157
	if (btrfs_leaf_free_space(root, leaf) <
C
Chris Mason 已提交
1158
	    sizeof(struct btrfs_item) + data_size)
1159 1160
		BUG();

1161
	slot = path->slots[0];
1162
	BUG_ON(slot < 0);
1163 1164
	if (slot != nritems) {
		int i;
C
Chris Mason 已提交
1165
		unsigned int old_data = btrfs_item_end(leaf->items + slot);
1166 1167 1168 1169 1170

		/*
		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
		 */
		/* first correct the data pointers */
C
Chris Mason 已提交
1171
		for (i = slot; i < nritems; i++) {
C
Chris Mason 已提交
1172
			u32 ioff = btrfs_item_offset(leaf->items + i);
C
Chris Mason 已提交
1173 1174 1175
			btrfs_set_item_offset(leaf->items + i,
					      ioff - data_size);
		}
1176 1177 1178

		/* shift the items */
		memmove(leaf->items + slot + 1, leaf->items + slot,
C
Chris Mason 已提交
1179
		        (nritems - slot) * sizeof(struct btrfs_item));
1180 1181

		/* shift the data */
C
Chris Mason 已提交
1182 1183
		memmove(btrfs_leaf_data(leaf) + data_end - data_size,
			btrfs_leaf_data(leaf) +
1184 1185 1186
		        data_end, old_data - data_end);
		data_end = old_data;
	}
1187
	/* setup the item for the new data */
C
Chris Mason 已提交
1188 1189
	memcpy(&leaf->items[slot].key, &disk_key,
		sizeof(struct btrfs_disk_key));
C
Chris Mason 已提交
1190 1191
	btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
	btrfs_set_item_size(leaf->items + slot, data_size);
1192
	btrfs_set_header_nritems(&leaf->header, nritems + 1);
C
Chris Mason 已提交
1193 1194

	ret = 0;
1195
	if (slot == 0)
1196
		ret = fixup_low_keys(root, path, &disk_key, 1);
C
Chris Mason 已提交
1197

C
Chris Mason 已提交
1198
	BUG_ON(list_empty(&leaf_buf->dirty));
C
Chris Mason 已提交
1199
	if (btrfs_leaf_free_space(root, leaf) < 0)
1200
		BUG();
1201
	check_leaf(root, path, 0);
1202
out:
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	return ret;
}

/*
 * Given a key and some data, insert an item into the tree.
 * This does all the path init required, making room in the tree if needed.
 */
int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
			  void *data, u32 data_size)
{
	int ret = 0;
	struct btrfs_path path;
	u8 *ptr;

	btrfs_init_path(&path);
	ret = btrfs_insert_empty_item(root, &path, cpu_key, data_size);
	if (!ret) {
		ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
		memcpy(ptr, data, data_size);
	}
C
Chris Mason 已提交
1223
	btrfs_release_path(root, &path);
C
Chris Mason 已提交
1224
	return ret;
1225 1226
}

C
Chris Mason 已提交
1227
/*
C
Chris Mason 已提交
1228
 * delete the pointer from a given node.
C
Chris Mason 已提交
1229 1230 1231 1232 1233
 *
 * If the delete empties a node, the node is removed from the tree,
 * continuing all the way the root if required.  The root is converted into
 * a leaf if all the nodes are emptied.
 */
C
Chris Mason 已提交
1234
static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
1235
		   int slot)
1236
{
C
Chris Mason 已提交
1237 1238
	struct btrfs_node *node;
	struct btrfs_buffer *parent = path->nodes[level];
1239
	u32 nritems;
C
Chris Mason 已提交
1240
	int ret = 0;
1241
	int wret;
1242

1243
	node = &parent->node;
1244
	nritems = btrfs_header_nritems(&node->header);
1245
	if (slot != nritems -1) {
C
Chris Mason 已提交
1246 1247
		memmove(node->ptrs + slot, node->ptrs + slot + 1,
			sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
1248
	}
1249 1250 1251 1252
	nritems--;
	btrfs_set_header_nritems(&node->header, nritems);
	if (nritems == 0 && parent == root->node) {
		BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
1253
		/* just turn the root into a leaf and break */
1254
		btrfs_set_header_level(&root->node->node.header, 0);
1255
	} else if (slot == 0) {
C
Chris Mason 已提交
1256 1257
		wret = fixup_low_keys(root, path, &node->ptrs[0].key,
				      level + 1);
C
Chris Mason 已提交
1258 1259
		if (wret)
			ret = wret;
1260
	}
C
Chris Mason 已提交
1261
	BUG_ON(list_empty(&parent->dirty));
C
Chris Mason 已提交
1262
	return ret;
1263 1264
}

C
Chris Mason 已提交
1265 1266 1267 1268
/*
 * delete the item at the leaf level in path.  If that empties
 * the leaf, remove it from the tree
 */
C
Chris Mason 已提交
1269
int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
1270 1271
{
	int slot;
C
Chris Mason 已提交
1272 1273
	struct btrfs_leaf *leaf;
	struct btrfs_buffer *leaf_buf;
1274 1275
	int doff;
	int dsize;
C
Chris Mason 已提交
1276 1277
	int ret = 0;
	int wret;
1278
	u32 nritems;
1279

1280 1281
	leaf_buf = path->nodes[0];
	leaf = &leaf_buf->leaf;
1282
	slot = path->slots[0];
C
Chris Mason 已提交
1283 1284
	doff = btrfs_item_offset(leaf->items + slot);
	dsize = btrfs_item_size(leaf->items + slot);
1285
	nritems = btrfs_header_nritems(&leaf->header);
1286

1287
	if (slot != nritems - 1) {
1288
		int i;
C
Chris Mason 已提交
1289 1290 1291
		int data_end = leaf_data_end(root, leaf);
		memmove(btrfs_leaf_data(leaf) + data_end + dsize,
			btrfs_leaf_data(leaf) + data_end,
1292
			doff - data_end);
C
Chris Mason 已提交
1293
		for (i = slot + 1; i < nritems; i++) {
C
Chris Mason 已提交
1294
			u32 ioff = btrfs_item_offset(leaf->items + i);
C
Chris Mason 已提交
1295 1296
			btrfs_set_item_offset(leaf->items + i, ioff + dsize);
		}
1297
		memmove(leaf->items + slot, leaf->items + slot + 1,
C
Chris Mason 已提交
1298
			sizeof(struct btrfs_item) *
1299
			(nritems - slot - 1));
1300
	}
1301 1302
	btrfs_set_header_nritems(&leaf->header, nritems - 1);
	nritems--;
C
Chris Mason 已提交
1303
	/* delete the leaf if we've emptied it */
1304
	if (nritems == 0) {
1305
		if (leaf_buf == root->node) {
1306
			btrfs_set_header_level(&leaf->header, 0);
C
Chris Mason 已提交
1307
			BUG_ON(list_empty(&leaf_buf->dirty));
1308
		} else {
1309
			clean_tree_block(root, leaf_buf);
1310
			wret = del_ptr(root, path, 1, path->slots[1]);
C
Chris Mason 已提交
1311 1312
			if (wret)
				ret = wret;
C
Chris Mason 已提交
1313
			wret = btrfs_free_extent(root, leaf_buf->blocknr, 1);
C
Chris Mason 已提交
1314 1315
			if (wret)
				ret = wret;
1316
		}
1317
	} else {
1318
		int used = leaf_space_used(leaf, 0, nritems);
C
Chris Mason 已提交
1319 1320 1321 1322 1323 1324
		if (slot == 0) {
			wret = fixup_low_keys(root, path,
						   &leaf->items[0].key, 1);
			if (wret)
				ret = wret;
		}
C
Chris Mason 已提交
1325
		BUG_ON(list_empty(&leaf_buf->dirty));
C
Chris Mason 已提交
1326

C
Chris Mason 已提交
1327
		/* delete the leaf if it is mostly empty */
C
Chris Mason 已提交
1328
		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1329 1330 1331 1332
			/* push_leaf_left fixes the path.
			 * make sure the path still points to our leaf
			 * for possible call to del_ptr below
			 */
1333
			slot = path->slots[1];
1334
			leaf_buf->count++;
C
Chris Mason 已提交
1335 1336 1337
			wret = push_leaf_left(root, path, 1);
			if (wret < 0)
				ret = wret;
1338
			if (path->nodes[0] == leaf_buf &&
1339
			    btrfs_header_nritems(&leaf->header)) {
C
Chris Mason 已提交
1340 1341 1342 1343
				wret = push_leaf_right(root, path, 1);
				if (wret < 0)
					ret = wret;
			}
1344
			if (btrfs_header_nritems(&leaf->header) == 0) {
C
Chris Mason 已提交
1345
				u64 blocknr = leaf_buf->blocknr;
1346
				clean_tree_block(root, leaf_buf);
1347
				wret = del_ptr(root, path, 1, slot);
C
Chris Mason 已提交
1348 1349
				if (wret)
					ret = wret;
C
Chris Mason 已提交
1350 1351
				btrfs_block_release(root, leaf_buf);
				wret = btrfs_free_extent(root, blocknr, 1);
C
Chris Mason 已提交
1352 1353
				if (wret)
					ret = wret;
C
Chris Mason 已提交
1354
			} else {
C
Chris Mason 已提交
1355
				btrfs_block_release(root, leaf_buf);
1356 1357 1358
			}
		}
	}
C
Chris Mason 已提交
1359
	return ret;
1360 1361
}

C
Chris Mason 已提交
1362 1363
/*
 * walk up the tree as far as required to find the next leaf.
C
Chris Mason 已提交
1364 1365
 * returns 0 if it found something or 1 if there are no greater leaves.
 * returns < 0 on io errors.
C
Chris Mason 已提交
1366
 */
C
Chris Mason 已提交
1367
int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
1368 1369 1370 1371
{
	int slot;
	int level = 1;
	u64 blocknr;
C
Chris Mason 已提交
1372 1373
	struct btrfs_buffer *c;
	struct btrfs_buffer *next = NULL;
1374

C
Chris Mason 已提交
1375
	while(level < BTRFS_MAX_LEVEL) {
1376
		if (!path->nodes[level])
C
Chris Mason 已提交
1377
			return 1;
1378 1379
		slot = path->slots[level] + 1;
		c = path->nodes[level];
1380
		if (slot >= btrfs_header_nritems(&c->node.header)) {
1381 1382 1383
			level++;
			continue;
		}
1384
		blocknr = btrfs_node_blockptr(&c->node, slot);
C
Chris Mason 已提交
1385
		if (next)
C
Chris Mason 已提交
1386
			btrfs_block_release(root, next);
1387 1388 1389 1390 1391 1392 1393
		next = read_tree_block(root, blocknr);
		break;
	}
	path->slots[level] = slot;
	while(1) {
		level--;
		c = path->nodes[level];
C
Chris Mason 已提交
1394
		btrfs_block_release(root, c);
1395 1396 1397 1398
		path->nodes[level] = next;
		path->slots[level] = 0;
		if (!level)
			break;
1399 1400
		next = read_tree_block(root,
				       btrfs_node_blockptr(&next->node, 0));
1401 1402 1403 1404
	}
	return 0;
}

C
Chris Mason 已提交
1405