aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ctree.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-03-14 14:14:43 -0400
committerDavid Woodhouse <dwmw2@hera.kernel.org>2007-03-14 14:14:43 -0400
commit123abc88c9087b9c5605566ee3491aaef17fd837 (patch)
tree3a8e83ac7a095d2b875664ee54cf7e24e977999b /fs/btrfs/ctree.c
parent4beb1b8b75a86373f6020103ab840448d14c8880 (diff)
Btrfs: variable block size support
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/ctree.c')
-rw-r--r--fs/btrfs/ctree.c225
1 files changed, 111 insertions, 114 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index dc620186e501..ece8de7f38ef 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -47,7 +47,7 @@ static int btrfs_cow_block(struct btrfs_root *root,
47 return 0; 47 return 0;
48 } 48 }
49 cow = btrfs_alloc_free_block(root); 49 cow = btrfs_alloc_free_block(root);
50 memcpy(&cow->node, &buf->node, sizeof(buf->node)); 50 memcpy(&cow->node, &buf->node, root->blocksize);
51 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr); 51 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
52 *cow_ret = cow; 52 *cow_ret = cow;
53 btrfs_inc_ref(root, buf); 53 btrfs_inc_ref(root, buf);
@@ -72,11 +72,12 @@ static int btrfs_cow_block(struct btrfs_root *root,
72 * this returns the address of the start of the last item, 72 * this returns the address of the start of the last item,
73 * which is the stop of the leaf data stack 73 * which is the stop of the leaf data stack
74 */ 74 */
75static inline unsigned int leaf_data_end(struct btrfs_leaf *leaf) 75static inline unsigned int leaf_data_end(struct btrfs_root *root,
76 struct btrfs_leaf *leaf)
76{ 77{
77 u32 nr = btrfs_header_nritems(&leaf->header); 78 u32 nr = btrfs_header_nritems(&leaf->header);
78 if (nr == 0) 79 if (nr == 0)
79 return sizeof(leaf->data); 80 return BTRFS_LEAF_DATA_SIZE(root);
80 return btrfs_item_offset(leaf->items + nr - 1); 81 return btrfs_item_offset(leaf->items + nr - 1);
81} 82}
82 83
@@ -85,12 +86,12 @@ static inline unsigned int leaf_data_end(struct btrfs_leaf *leaf)
85 * the start of the leaf data. IOW, how much room 86 * the start of the leaf data. IOW, how much room
86 * the leaf has left for both items and data 87 * the leaf has left for both items and data
87 */ 88 */
88int btrfs_leaf_free_space(struct btrfs_leaf *leaf) 89int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
89{ 90{
90 int data_end = leaf_data_end(leaf); 91 int data_end = leaf_data_end(root, leaf);
91 int nritems = btrfs_header_nritems(&leaf->header); 92 int nritems = btrfs_header_nritems(&leaf->header);
92 char *items_end = (char *)(leaf->items + nritems + 1); 93 char *items_end = (char *)(leaf->items + nritems + 1);
93 return (char *)(leaf->data + data_end) - (char *)items_end; 94 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
94} 95}
95 96
96/* 97/*
@@ -117,7 +118,8 @@ static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
117 return 0; 118 return 0;
118} 119}
119 120
120static int check_node(struct btrfs_path *path, int level) 121static int check_node(struct btrfs_root *root, struct btrfs_path *path,
122 int level)
121{ 123{
122 int i; 124 int i;
123 struct btrfs_node *parent = NULL; 125 struct btrfs_node *parent = NULL;
@@ -131,22 +133,23 @@ static int check_node(struct btrfs_path *path, int level)
131 BUG_ON(nritems == 0); 133 BUG_ON(nritems == 0);
132 if (parent) { 134 if (parent) {
133 struct btrfs_disk_key *parent_key; 135 struct btrfs_disk_key *parent_key;
134 parent_key = &parent->keys[parent_slot]; 136 parent_key = &parent->ptrs[parent_slot].key;
135 BUG_ON(memcmp(parent_key, node->keys, 137 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
136 sizeof(struct btrfs_disk_key))); 138 sizeof(struct btrfs_disk_key)));
137 BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 139 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
138 btrfs_header_blocknr(&node->header)); 140 btrfs_header_blocknr(&node->header));
139 } 141 }
140 BUG_ON(nritems > NODEPTRS_PER_BLOCK); 142 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
141 for (i = 0; nritems > 1 && i < nritems - 2; i++) { 143 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
142 struct btrfs_key cpukey; 144 struct btrfs_key cpukey;
143 btrfs_disk_key_to_cpu(&cpukey, &node->keys[i + 1]); 145 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
144 BUG_ON(comp_keys(&node->keys[i], &cpukey) >= 0); 146 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
145 } 147 }
146 return 0; 148 return 0;
147} 149}
148 150
149static int check_leaf(struct btrfs_path *path, int level) 151static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
152 int level)
150{ 153{
151 int i; 154 int i;
152 struct btrfs_leaf *leaf = &path->nodes[level]->leaf; 155 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
@@ -157,14 +160,14 @@ static int check_leaf(struct btrfs_path *path, int level)
157 if (path->nodes[level + 1]) 160 if (path->nodes[level + 1])
158 parent = &path->nodes[level + 1]->node; 161 parent = &path->nodes[level + 1]->node;
159 parent_slot = path->slots[level + 1]; 162 parent_slot = path->slots[level + 1];
160 BUG_ON(btrfs_leaf_free_space(leaf) < 0); 163 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
161 164
162 if (nritems == 0) 165 if (nritems == 0)
163 return 0; 166 return 0;
164 167
165 if (parent) { 168 if (parent) {
166 struct btrfs_disk_key *parent_key; 169 struct btrfs_disk_key *parent_key;
167 parent_key = &parent->keys[parent_slot]; 170 parent_key = &parent->ptrs[parent_slot].key;
168 BUG_ON(memcmp(parent_key, &leaf->items[0].key, 171 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
169 sizeof(struct btrfs_disk_key))); 172 sizeof(struct btrfs_disk_key)));
170 BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 173 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
@@ -180,17 +183,18 @@ static int check_leaf(struct btrfs_path *path, int level)
180 if (i == 0) { 183 if (i == 0) {
181 BUG_ON(btrfs_item_offset(leaf->items + i) + 184 BUG_ON(btrfs_item_offset(leaf->items + i) +
182 btrfs_item_size(leaf->items + i) != 185 btrfs_item_size(leaf->items + i) !=
183 LEAF_DATA_SIZE); 186 BTRFS_LEAF_DATA_SIZE(root));
184 } 187 }
185 } 188 }
186 return 0; 189 return 0;
187} 190}
188 191
189static int check_block(struct btrfs_path *path, int level) 192static int check_block(struct btrfs_root *root, struct btrfs_path *path,
193 int level)
190{ 194{
191 if (level == 0) 195 if (level == 0)
192 return check_leaf(path, level); 196 return check_leaf(root, path, level);
193 return check_node(path, level); 197 return check_node(root, path, level);
194} 198}
195 199
196/* 200/*
@@ -242,8 +246,8 @@ static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
242 key, btrfs_header_nritems(&c->header), 246 key, btrfs_header_nritems(&c->header),
243 slot); 247 slot);
244 } else { 248 } else {
245 return generic_bin_search((void *)c->keys, 249 return generic_bin_search((void *)c->ptrs,
246 sizeof(struct btrfs_disk_key), 250 sizeof(struct btrfs_key_ptr),
247 key, btrfs_header_nritems(&c->header), 251 key, btrfs_header_nritems(&c->header),
248 slot); 252 slot);
249 } 253 }
@@ -311,7 +315,8 @@ static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
311 } 315 }
312 parent = &parent_buf->node; 316 parent = &parent_buf->node;
313 317
314 if (btrfs_header_nritems(&mid->header) > NODEPTRS_PER_BLOCK / 4) 318 if (btrfs_header_nritems(&mid->header) >
319 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
315 return 0; 320 return 0;
316 321
317 left_buf = read_node_slot(root, parent_buf, pslot - 1); 322 left_buf = read_node_slot(root, parent_buf, pslot - 1);
@@ -351,7 +356,8 @@ static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
351 if (wret) 356 if (wret)
352 ret = wret; 357 ret = wret;
353 } else { 358 } else {
354 memcpy(parent->keys + pslot + 1, right->keys, 359 memcpy(&parent->ptrs[pslot + 1].key,
360 &right->ptrs[0].key,
355 sizeof(struct btrfs_disk_key)); 361 sizeof(struct btrfs_disk_key));
356 BUG_ON(list_empty(&parent_buf->dirty)); 362 BUG_ON(list_empty(&parent_buf->dirty));
357 } 363 }
@@ -387,7 +393,7 @@ static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
387 ret = wret; 393 ret = wret;
388 } else { 394 } else {
389 /* update the parent key to reflect our changes */ 395 /* update the parent key to reflect our changes */
390 memcpy(parent->keys + pslot, mid->keys, 396 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
391 sizeof(struct btrfs_disk_key)); 397 sizeof(struct btrfs_disk_key));
392 BUG_ON(list_empty(&parent_buf->dirty)); 398 BUG_ON(list_empty(&parent_buf->dirty));
393 } 399 }
@@ -407,7 +413,7 @@ static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
407 } 413 }
408 } 414 }
409 /* double check we haven't messed things up */ 415 /* double check we haven't messed things up */
410 check_block(path, level); 416 check_block(root, path, level);
411 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node, 417 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
412 path->slots[level])) 418 path->slots[level]))
413 BUG(); 419 BUG();
@@ -456,7 +462,7 @@ again:
456 BUG_ON(!cow && ins_len); 462 BUG_ON(!cow && ins_len);
457 c = &b->node; 463 c = &b->node;
458 p->nodes[level] = b; 464 p->nodes[level] = b;
459 ret = check_block(p, level); 465 ret = check_block(root, p, level);
460 if (ret) 466 if (ret)
461 return -1; 467 return -1;
462 ret = bin_search(c, key, &slot); 468 ret = bin_search(c, key, &slot);
@@ -465,7 +471,7 @@ again:
465 slot -= 1; 471 slot -= 1;
466 p->slots[level] = slot; 472 p->slots[level] = slot;
467 if (ins_len > 0 && btrfs_header_nritems(&c->header) == 473 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
468 NODEPTRS_PER_BLOCK) { 474 BTRFS_NODEPTRS_PER_BLOCK(root)) {
469 int sret = split_node(root, p, level); 475 int sret = split_node(root, p, level);
470 BUG_ON(sret > 0); 476 BUG_ON(sret > 0);
471 if (sret) 477 if (sret)
@@ -488,7 +494,7 @@ again:
488 } else { 494 } else {
489 struct btrfs_leaf *l = (struct btrfs_leaf *)c; 495 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
490 p->slots[level] = slot; 496 p->slots[level] = slot;
491 if (ins_len > 0 && btrfs_leaf_free_space(l) < 497 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
492 sizeof(struct btrfs_item) + ins_len) { 498 sizeof(struct btrfs_item) + ins_len) {
493 int sret = split_leaf(root, p, ins_len); 499 int sret = split_leaf(root, p, ins_len);
494 BUG_ON(sret > 0); 500 BUG_ON(sret > 0);
@@ -525,7 +531,7 @@ static int fixup_low_keys(struct btrfs_root *root,
525 if (!path->nodes[i]) 531 if (!path->nodes[i])
526 break; 532 break;
527 t = &path->nodes[i]->node; 533 t = &path->nodes[i]->node;
528 memcpy(t->keys + tslot, key, sizeof(*key)); 534 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
529 BUG_ON(list_empty(&path->nodes[i]->dirty)); 535 BUG_ON(list_empty(&path->nodes[i]->dirty));
530 if (tslot != 0) 536 if (tslot != 0)
531 break; 537 break;
@@ -552,7 +558,7 @@ static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
552 558
553 src_nritems = btrfs_header_nritems(&src->header); 559 src_nritems = btrfs_header_nritems(&src->header);
554 dst_nritems = btrfs_header_nritems(&dst->header); 560 dst_nritems = btrfs_header_nritems(&dst->header);
555 push_items = NODEPTRS_PER_BLOCK - dst_nritems; 561 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
556 if (push_items <= 0) { 562 if (push_items <= 0) {
557 return 1; 563 return 1;
558 } 564 }
@@ -560,16 +566,12 @@ static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
560 if (src_nritems < push_items) 566 if (src_nritems < push_items)
561 push_items = src_nritems; 567 push_items = src_nritems;
562 568
563 memcpy(dst->keys + dst_nritems, src->keys, 569 memcpy(dst->ptrs + dst_nritems, src->ptrs,
564 push_items * sizeof(struct btrfs_disk_key)); 570 push_items * sizeof(struct btrfs_key_ptr));
565 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
566 push_items * sizeof(u64));
567 if (push_items < src_nritems) { 571 if (push_items < src_nritems) {
568 memmove(src->keys, src->keys + push_items, 572 memmove(src->ptrs, src->ptrs + push_items,
569 (src_nritems - push_items) * 573 (src_nritems - push_items) *
570 sizeof(struct btrfs_disk_key)); 574 sizeof(struct btrfs_key_ptr));
571 memmove(src->blockptrs, src->blockptrs + push_items,
572 (src_nritems - push_items) * sizeof(u64));
573 } 575 }
574 btrfs_set_header_nritems(&src->header, src_nritems - push_items); 576 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
575 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); 577 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
@@ -601,7 +603,7 @@ static int balance_node_right(struct btrfs_root *root,
601 603
602 src_nritems = btrfs_header_nritems(&src->header); 604 src_nritems = btrfs_header_nritems(&src->header);
603 dst_nritems = btrfs_header_nritems(&dst->header); 605 dst_nritems = btrfs_header_nritems(&dst->header);
604 push_items = NODEPTRS_PER_BLOCK - dst_nritems; 606 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
605 if (push_items <= 0) { 607 if (push_items <= 0) {
606 return 1; 608 return 1;
607 } 609 }
@@ -613,14 +615,10 @@ static int balance_node_right(struct btrfs_root *root,
613 if (max_push < push_items) 615 if (max_push < push_items)
614 push_items = max_push; 616 push_items = max_push;
615 617
616 memmove(dst->keys + push_items, dst->keys, 618 memmove(dst->ptrs + push_items, dst->ptrs,
617 dst_nritems * sizeof(struct btrfs_disk_key)); 619 dst_nritems * sizeof(struct btrfs_key_ptr));
618 memmove(dst->blockptrs + push_items, dst->blockptrs, 620 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
619 dst_nritems * sizeof(u64)); 621 push_items * sizeof(struct btrfs_key_ptr));
620 memcpy(dst->keys, src->keys + src_nritems - push_items,
621 push_items * sizeof(struct btrfs_disk_key));
622 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
623 push_items * sizeof(u64));
624 622
625 btrfs_set_header_nritems(&src->header, src_nritems - push_items); 623 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
626 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); 624 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
@@ -650,7 +648,7 @@ static int insert_new_root(struct btrfs_root *root,
650 648
651 t = btrfs_alloc_free_block(root); 649 t = btrfs_alloc_free_block(root);
652 c = &t->node; 650 c = &t->node;
653 memset(c, 0, sizeof(c)); 651 memset(c, 0, root->blocksize);
654 btrfs_set_header_nritems(&c->header, 1); 652 btrfs_set_header_nritems(&c->header, 1);
655 btrfs_set_header_level(&c->header, level); 653 btrfs_set_header_level(&c->header, level);
656 btrfs_set_header_blocknr(&c->header, t->blocknr); 654 btrfs_set_header_blocknr(&c->header, t->blocknr);
@@ -660,8 +658,8 @@ static int insert_new_root(struct btrfs_root *root,
660 if (btrfs_is_leaf(lower)) 658 if (btrfs_is_leaf(lower))
661 lower_key = &((struct btrfs_leaf *)lower)->items[0].key; 659 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
662 else 660 else
663 lower_key = lower->keys; 661 lower_key = &lower->ptrs[0].key;
664 memcpy(c->keys, lower_key, sizeof(struct btrfs_disk_key)); 662 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
665 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr); 663 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
666 /* the super has an extra ref to root->node */ 664 /* the super has an extra ref to root->node */
667 btrfs_block_release(root, root->node); 665 btrfs_block_release(root, root->node);
@@ -693,19 +691,15 @@ static int insert_ptr(struct btrfs_root *root,
693 nritems = btrfs_header_nritems(&lower->header); 691 nritems = btrfs_header_nritems(&lower->header);
694 if (slot > nritems) 692 if (slot > nritems)
695 BUG(); 693 BUG();
696 if (nritems == NODEPTRS_PER_BLOCK) 694 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
697 BUG(); 695 BUG();
698 if (slot != nritems) { 696 if (slot != nritems) {
699 memmove(lower->keys + slot + 1, lower->keys + slot, 697 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
700 (nritems - slot) * sizeof(struct btrfs_disk_key)); 698 (nritems - slot) * sizeof(struct btrfs_key_ptr));
701 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
702 (nritems - slot) * sizeof(u64));
703 } 699 }
704 memcpy(lower->keys + slot, key, sizeof(struct btrfs_disk_key)); 700 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
705 btrfs_set_node_blockptr(lower, slot, blocknr); 701 btrfs_set_node_blockptr(lower, slot, blocknr);
706 btrfs_set_header_nritems(&lower->header, nritems + 1); 702 btrfs_set_header_nritems(&lower->header, nritems + 1);
707 if (lower->keys[1].objectid == 0)
708 BUG();
709 BUG_ON(list_empty(&path->nodes[level]->dirty)); 703 BUG_ON(list_empty(&path->nodes[level]->dirty));
710 return 0; 704 return 0;
711} 705}
@@ -747,17 +741,16 @@ static int split_node(struct btrfs_root *root, struct btrfs_path *path,
747 btrfs_set_header_parentid(&split->header, 741 btrfs_set_header_parentid(&split->header,
748 btrfs_header_parentid(&root->node->node.header)); 742 btrfs_header_parentid(&root->node->node.header));
749 mid = (c_nritems + 1) / 2; 743 mid = (c_nritems + 1) / 2;
750 memcpy(split->keys, c->keys + mid, 744 memcpy(split->ptrs, c->ptrs + mid,
751 (c_nritems - mid) * sizeof(struct btrfs_disk_key)); 745 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
752 memcpy(split->blockptrs, c->blockptrs + mid,
753 (c_nritems - mid) * sizeof(u64));
754 btrfs_set_header_nritems(&split->header, c_nritems - mid); 746 btrfs_set_header_nritems(&split->header, c_nritems - mid);
755 btrfs_set_header_nritems(&c->header, mid); 747 btrfs_set_header_nritems(&c->header, mid);
756 ret = 0; 748 ret = 0;
757 749
758 BUG_ON(list_empty(&t->dirty)); 750 BUG_ON(list_empty(&t->dirty));
759 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr, 751 wret = insert_ptr(root, path, &split->ptrs[0].key,
760 path->slots[level + 1] + 1, level + 1); 752 split_buffer->blocknr, path->slots[level + 1] + 1,
753 level + 1);
761 if (wret) 754 if (wret)
762 ret = wret; 755 ret = wret;
763 756
@@ -825,7 +818,7 @@ static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
825 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node, 818 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
826 slot + 1)); 819 slot + 1));
827 right = &right_buf->leaf; 820 right = &right_buf->leaf;
828 free_space = btrfs_leaf_free_space(right); 821 free_space = btrfs_leaf_free_space(root, right);
829 if (free_space < data_size + sizeof(struct btrfs_item)) { 822 if (free_space < data_size + sizeof(struct btrfs_item)) {
830 btrfs_block_release(root, right_buf); 823 btrfs_block_release(root, right_buf);
831 return 1; 824 return 1;
@@ -833,7 +826,7 @@ static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
833 /* cow and double check */ 826 /* cow and double check */
834 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf); 827 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
835 right = &right_buf->leaf; 828 right = &right_buf->leaf;
836 free_space = btrfs_leaf_free_space(right); 829 free_space = btrfs_leaf_free_space(root, right);
837 if (free_space < data_size + sizeof(struct btrfs_item)) { 830 if (free_space < data_size + sizeof(struct btrfs_item)) {
838 btrfs_block_release(root, right_buf); 831 btrfs_block_release(root, right_buf);
839 return 1; 832 return 1;
@@ -857,15 +850,14 @@ static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
857 right_nritems = btrfs_header_nritems(&right->header); 850 right_nritems = btrfs_header_nritems(&right->header);
858 /* push left to right */ 851 /* push left to right */
859 push_space = btrfs_item_end(left->items + left_nritems - push_items); 852 push_space = btrfs_item_end(left->items + left_nritems - push_items);
860 push_space -= leaf_data_end(left); 853 push_space -= leaf_data_end(root, left);
861 /* make room in the right data area */ 854 /* make room in the right data area */
862 memmove(right->data + leaf_data_end(right) - push_space, 855 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
863 right->data + leaf_data_end(right), 856 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
864 LEAF_DATA_SIZE - leaf_data_end(right)); 857 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
865 /* copy from the left data area */ 858 /* copy from the left data area */
866 memcpy(right->data + LEAF_DATA_SIZE - push_space, 859 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
867 left->data + leaf_data_end(left), 860 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
868 push_space);
869 memmove(right->items + push_items, right->items, 861 memmove(right->items + push_items, right->items,
870 right_nritems * sizeof(struct btrfs_item)); 862 right_nritems * sizeof(struct btrfs_item));
871 /* copy the items from left to right */ 863 /* copy the items from left to right */
@@ -875,7 +867,7 @@ static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
875 /* update the item pointers */ 867 /* update the item pointers */
876 right_nritems += push_items; 868 right_nritems += push_items;
877 btrfs_set_header_nritems(&right->header, right_nritems); 869 btrfs_set_header_nritems(&right->header, right_nritems);
878 push_space = LEAF_DATA_SIZE; 870 push_space = BTRFS_LEAF_DATA_SIZE(root);
879 for (i = 0; i < right_nritems; i++) { 871 for (i = 0; i < right_nritems; i++) {
880 btrfs_set_item_offset(right->items + i, push_space - 872 btrfs_set_item_offset(right->items + i, push_space -
881 btrfs_item_size(right->items + i)); 873 btrfs_item_size(right->items + i));
@@ -886,7 +878,7 @@ static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
886 878
887 BUG_ON(list_empty(&left_buf->dirty)); 879 BUG_ON(list_empty(&left_buf->dirty));
888 BUG_ON(list_empty(&right_buf->dirty)); 880 BUG_ON(list_empty(&right_buf->dirty));
889 memcpy(upper->node.keys + slot + 1, 881 memcpy(&upper->node.ptrs[slot + 1].key,
890 &right->items[0].key, sizeof(struct btrfs_disk_key)); 882 &right->items[0].key, sizeof(struct btrfs_disk_key));
891 BUG_ON(list_empty(&upper->dirty)); 883 BUG_ON(list_empty(&upper->dirty));
892 884
@@ -932,7 +924,7 @@ static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
932 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node, 924 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
933 slot - 1)); 925 slot - 1));
934 left = &t->leaf; 926 left = &t->leaf;
935 free_space = btrfs_leaf_free_space(left); 927 free_space = btrfs_leaf_free_space(root, left);
936 if (free_space < data_size + sizeof(struct btrfs_item)) { 928 if (free_space < data_size + sizeof(struct btrfs_item)) {
937 btrfs_block_release(root, t); 929 btrfs_block_release(root, t);
938 return 1; 930 return 1;
@@ -941,7 +933,7 @@ static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
941 /* cow and double check */ 933 /* cow and double check */
942 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t); 934 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
943 left = &t->leaf; 935 left = &t->leaf;
944 free_space = btrfs_leaf_free_space(left); 936 free_space = btrfs_leaf_free_space(root, left);
945 if (free_space < data_size + sizeof(struct btrfs_item)) { 937 if (free_space < data_size + sizeof(struct btrfs_item)) {
946 btrfs_block_release(root, t); 938 btrfs_block_release(root, t);
947 return 1; 939 return 1;
@@ -964,17 +956,19 @@ static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
964 /* push data from right to left */ 956 /* push data from right to left */
965 memcpy(left->items + btrfs_header_nritems(&left->header), 957 memcpy(left->items + btrfs_header_nritems(&left->header),
966 right->items, push_items * sizeof(struct btrfs_item)); 958 right->items, push_items * sizeof(struct btrfs_item));
967 push_space = LEAF_DATA_SIZE - 959 push_space = BTRFS_LEAF_DATA_SIZE(root) -
968 btrfs_item_offset(right->items + push_items -1); 960 btrfs_item_offset(right->items + push_items -1);
969 memcpy(left->data + leaf_data_end(left) - push_space, 961 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
970 right->data + btrfs_item_offset(right->items + push_items - 1), 962 btrfs_leaf_data(right) +
963 btrfs_item_offset(right->items + push_items - 1),
971 push_space); 964 push_space);
972 old_left_nritems = btrfs_header_nritems(&left->header); 965 old_left_nritems = btrfs_header_nritems(&left->header);
973 BUG_ON(old_left_nritems < 0); 966 BUG_ON(old_left_nritems < 0);
974 967
975 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { 968 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
976 u16 ioff = btrfs_item_offset(left->items + i); 969 u32 ioff = btrfs_item_offset(left->items + i);
977 btrfs_set_item_offset(left->items + i, ioff - (LEAF_DATA_SIZE - 970 btrfs_set_item_offset(left->items + i, ioff -
971 (BTRFS_LEAF_DATA_SIZE(root) -
978 btrfs_item_offset(left->items + 972 btrfs_item_offset(left->items +
979 old_left_nritems - 1))); 973 old_left_nritems - 1)));
980 } 974 }
@@ -982,16 +976,17 @@ static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
982 976
983 /* fixup right node */ 977 /* fixup right node */
984 push_space = btrfs_item_offset(right->items + push_items - 1) - 978 push_space = btrfs_item_offset(right->items + push_items - 1) -
985 leaf_data_end(right); 979 leaf_data_end(root, right);
986 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data + 980 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
987 leaf_data_end(right), push_space); 981 push_space, btrfs_leaf_data(right) +
982 leaf_data_end(root, right), push_space);
988 memmove(right->items, right->items + push_items, 983 memmove(right->items, right->items + push_items,
989 (btrfs_header_nritems(&right->header) - push_items) * 984 (btrfs_header_nritems(&right->header) - push_items) *
990 sizeof(struct btrfs_item)); 985 sizeof(struct btrfs_item));
991 btrfs_set_header_nritems(&right->header, 986 btrfs_set_header_nritems(&right->header,
992 btrfs_header_nritems(&right->header) - 987 btrfs_header_nritems(&right->header) -
993 push_items); 988 push_items);
994 push_space = LEAF_DATA_SIZE; 989 push_space = BTRFS_LEAF_DATA_SIZE(root);
995 990
996 for (i = 0; i < btrfs_header_nritems(&right->header); i++) { 991 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
997 btrfs_set_item_offset(right->items + i, push_space - 992 btrfs_set_item_offset(right->items + i, push_space -
@@ -1051,12 +1046,12 @@ static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
1051 if (wret < 0) 1046 if (wret < 0)
1052 return wret; 1047 return wret;
1053 } 1048 }
1054
1055 l_buf = path->nodes[0]; 1049 l_buf = path->nodes[0];
1056 l = &l_buf->leaf; 1050 l = &l_buf->leaf;
1057 1051
1058 /* did the pushes work? */ 1052 /* did the pushes work? */
1059 if (btrfs_leaf_free_space(l) >= sizeof(struct btrfs_item) + data_size) 1053 if (btrfs_leaf_free_space(root, l) >=
1054 sizeof(struct btrfs_item) + data_size)
1060 return 0; 1055 return 0;
1061 1056
1062 if (!path->nodes[1]) { 1057 if (!path->nodes[1]) {
@@ -1071,16 +1066,16 @@ static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
1071 BUG_ON(!right_buffer); 1066 BUG_ON(!right_buffer);
1072 BUG_ON(mid == nritems); 1067 BUG_ON(mid == nritems);
1073 right = &right_buffer->leaf; 1068 right = &right_buffer->leaf;
1074 memset(right, 0, sizeof(*right)); 1069 memset(&right->header, 0, sizeof(right->header));
1075 if (mid <= slot) { 1070 if (mid <= slot) {
1076 /* FIXME, just alloc a new leaf here */ 1071 /* FIXME, just alloc a new leaf here */
1077 if (leaf_space_used(l, mid, nritems - mid) + space_needed > 1072 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1078 LEAF_DATA_SIZE) 1073 BTRFS_LEAF_DATA_SIZE(root))
1079 BUG(); 1074 BUG();
1080 } else { 1075 } else {
1081 /* FIXME, just alloc a new leaf here */ 1076 /* FIXME, just alloc a new leaf here */
1082 if (leaf_space_used(l, 0, mid + 1) + space_needed > 1077 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1083 LEAF_DATA_SIZE) 1078 BTRFS_LEAF_DATA_SIZE(root))
1084 BUG(); 1079 BUG();
1085 } 1080 }
1086 btrfs_set_header_nritems(&right->header, nritems - mid); 1081 btrfs_set_header_nritems(&right->header, nritems - mid);
@@ -1088,15 +1083,18 @@ static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
1088 btrfs_set_header_level(&right->header, 0); 1083 btrfs_set_header_level(&right->header, 0);
1089 btrfs_set_header_parentid(&right->header, 1084 btrfs_set_header_parentid(&right->header,
1090 btrfs_header_parentid(&root->node->node.header)); 1085 btrfs_header_parentid(&root->node->node.header));
1091 data_copy_size = btrfs_item_end(l->items + mid) - leaf_data_end(l); 1086 data_copy_size = btrfs_item_end(l->items + mid) -
1087 leaf_data_end(root, l);
1092 memcpy(right->items, l->items + mid, 1088 memcpy(right->items, l->items + mid,
1093 (nritems - mid) * sizeof(struct btrfs_item)); 1089 (nritems - mid) * sizeof(struct btrfs_item));
1094 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size, 1090 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1095 l->data + leaf_data_end(l), data_copy_size); 1091 data_copy_size, btrfs_leaf_data(l) +
1096 rt_data_off = LEAF_DATA_SIZE - btrfs_item_end(l->items + mid); 1092 leaf_data_end(root, l), data_copy_size);
1093 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1094 btrfs_item_end(l->items + mid);
1097 1095
1098 for (i = 0; i < btrfs_header_nritems(&right->header); i++) { 1096 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1099 u16 ioff = btrfs_item_offset(right->items + i); 1097 u32 ioff = btrfs_item_offset(right->items + i);
1100 btrfs_set_item_offset(right->items + i, ioff + rt_data_off); 1098 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1101 } 1099 }
1102 1100
@@ -1156,9 +1154,9 @@ int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
1156 leaf = &leaf_buf->leaf; 1154 leaf = &leaf_buf->leaf;
1157 1155
1158 nritems = btrfs_header_nritems(&leaf->header); 1156 nritems = btrfs_header_nritems(&leaf->header);
1159 data_end = leaf_data_end(leaf); 1157 data_end = leaf_data_end(root, leaf);
1160 1158
1161 if (btrfs_leaf_free_space(leaf) < 1159 if (btrfs_leaf_free_space(root, leaf) <
1162 sizeof(struct btrfs_item) + data_size) 1160 sizeof(struct btrfs_item) + data_size)
1163 BUG(); 1161 BUG();
1164 1162
@@ -1173,7 +1171,7 @@ int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
1173 */ 1171 */
1174 /* first correct the data pointers */ 1172 /* first correct the data pointers */
1175 for (i = slot; i < nritems; i++) { 1173 for (i = slot; i < nritems; i++) {
1176 u16 ioff = btrfs_item_offset(leaf->items + i); 1174 u32 ioff = btrfs_item_offset(leaf->items + i);
1177 btrfs_set_item_offset(leaf->items + i, 1175 btrfs_set_item_offset(leaf->items + i,
1178 ioff - data_size); 1176 ioff - data_size);
1179 } 1177 }
@@ -1183,7 +1181,8 @@ int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
1183 (nritems - slot) * sizeof(struct btrfs_item)); 1181 (nritems - slot) * sizeof(struct btrfs_item));
1184 1182
1185 /* shift the data */ 1183 /* shift the data */
1186 memmove(leaf->data + data_end - data_size, leaf->data + 1184 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1185 btrfs_leaf_data(leaf) +
1187 data_end, old_data - data_end); 1186 data_end, old_data - data_end);
1188 data_end = old_data; 1187 data_end = old_data;
1189 } 1188 }
@@ -1192,7 +1191,7 @@ int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
1192 sizeof(struct btrfs_disk_key)); 1191 sizeof(struct btrfs_disk_key));
1193 btrfs_set_item_offset(leaf->items + slot, data_end - data_size); 1192 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1194 btrfs_set_item_size(leaf->items + slot, data_size); 1193 btrfs_set_item_size(leaf->items + slot, data_size);
1195 memcpy(leaf->data + data_end - data_size, data, data_size); 1194 memcpy(btrfs_leaf_data(leaf) + data_end - data_size, data, data_size);
1196 btrfs_set_header_nritems(&leaf->header, nritems + 1); 1195 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1197 1196
1198 ret = 0; 1197 ret = 0;
@@ -1200,9 +1199,9 @@ int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
1200 ret = fixup_low_keys(root, &path, &disk_key, 1); 1199 ret = fixup_low_keys(root, &path, &disk_key, 1);
1201 1200
1202 BUG_ON(list_empty(&leaf_buf->dirty)); 1201 BUG_ON(list_empty(&leaf_buf->dirty));
1203 if (btrfs_leaf_free_space(leaf) < 0) 1202 if (btrfs_leaf_free_space(root, leaf) < 0)
1204 BUG(); 1203 BUG();
1205 check_leaf(&path, 0); 1204 check_leaf(root, &path, 0);
1206out: 1205out:
1207 btrfs_release_path(root, &path); 1206 btrfs_release_path(root, &path);
1208 return ret; 1207 return ret;
@@ -1227,11 +1226,8 @@ static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
1227 node = &parent->node; 1226 node = &parent->node;
1228 nritems = btrfs_header_nritems(&node->header); 1227 nritems = btrfs_header_nritems(&node->header);
1229 if (slot != nritems -1) { 1228 if (slot != nritems -1) {
1230 memmove(node->keys + slot, node->keys + slot + 1, 1229 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1231 sizeof(struct btrfs_disk_key) * (nritems - slot - 1)); 1230 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
1232 memmove(node->blockptrs + slot,
1233 node->blockptrs + slot + 1,
1234 sizeof(u64) * (nritems - slot - 1));
1235 } 1231 }
1236 nritems--; 1232 nritems--;
1237 btrfs_set_header_nritems(&node->header, nritems); 1233 btrfs_set_header_nritems(&node->header, nritems);
@@ -1240,7 +1236,8 @@ static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
1240 /* just turn the root into a leaf and break */ 1236 /* just turn the root into a leaf and break */
1241 btrfs_set_header_level(&root->node->node.header, 0); 1237 btrfs_set_header_level(&root->node->node.header, 0);
1242 } else if (slot == 0) { 1238 } else if (slot == 0) {
1243 wret = fixup_low_keys(root, path, node->keys, level + 1); 1239 wret = fixup_low_keys(root, path, &node->ptrs[0].key,
1240 level + 1);
1244 if (wret) 1241 if (wret)
1245 ret = wret; 1242 ret = wret;
1246 } 1243 }
@@ -1272,12 +1269,12 @@ int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
1272 1269
1273 if (slot != nritems - 1) { 1270 if (slot != nritems - 1) {
1274 int i; 1271 int i;
1275 int data_end = leaf_data_end(leaf); 1272 int data_end = leaf_data_end(root, leaf);
1276 memmove(leaf->data + data_end + dsize, 1273 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1277 leaf->data + data_end, 1274 btrfs_leaf_data(leaf) + data_end,
1278 doff - data_end); 1275 doff - data_end);
1279 for (i = slot + 1; i < nritems; i++) { 1276 for (i = slot + 1; i < nritems; i++) {
1280 u16 ioff = btrfs_item_offset(leaf->items + i); 1277 u32 ioff = btrfs_item_offset(leaf->items + i);
1281 btrfs_set_item_offset(leaf->items + i, ioff + dsize); 1278 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1282 } 1279 }
1283 memmove(leaf->items + slot, leaf->items + slot + 1, 1280 memmove(leaf->items + slot, leaf->items + slot + 1,
@@ -1311,7 +1308,7 @@ int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
1311 BUG_ON(list_empty(&leaf_buf->dirty)); 1308 BUG_ON(list_empty(&leaf_buf->dirty));
1312 1309
1313 /* delete the leaf if it is mostly empty */ 1310 /* delete the leaf if it is mostly empty */
1314 if (used < LEAF_DATA_SIZE / 3) { 1311 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1315 /* push_leaf_left fixes the path. 1312 /* push_leaf_left fixes the path.
1316 * make sure the path still points to our leaf 1313 * make sure the path still points to our leaf
1317 * for possible call to del_ptr below 1314 * for possible call to del_ptr below