diff -r 59fc7ac63e46 Makefile --- a/Makefile Fri Dec 21 16:25:35 2007 -0500 +++ b/Makefile Fri Jan 04 19:19:49 2008 +0800 @@ -3,7 +3,7 @@ CFLAGS = -g -Werror CFLAGS = -g -Werror objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ root-tree.o dir-item.o hash.o file-item.o inode-item.o \ - inode-map.o crc32c.o rbtree.o extent-cache.o \ + inode-map.o crc32c.o rbtree.o extent-cache.o extent_map.o \ # CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \ -Wuninitialized -Wshadow -Wundef @@ -35,8 +35,8 @@ btrfsck: $(objects) btrfsck.o bit-radix. btrfsck: $(objects) btrfsck.o bit-radix.o gcc $(CFLAGS) -o btrfsck btrfsck.o $(objects) bit-radix.o $(LDFLAGS) -mkfs.btrfs: $(objects) mkfs.o - gcc $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o -luuid $(LDFLAGS) +mkfs.btrfs: $(objects) mkfs.o utils.o + gcc $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o utils.o -luuid $(LDFLAGS) debug-tree: $(objects) debug-tree.o gcc $(CFLAGS) -o debug-tree $(objects) debug-tree.o -luuid $(LDFLAGS) diff -r 59fc7ac63e46 btrfsck.c --- a/btrfsck.c Fri Dec 21 16:25:35 2007 -0500 +++ b/btrfsck.c Fri Jan 04 19:19:49 2008 +0800 @@ -63,22 +63,24 @@ struct block_info { static int check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key, - struct btrfs_node *node) + struct extent_buffer *buf) { int i; - u32 nritems = btrfs_header_nritems(&node->header); + struct btrfs_key cpukey; + struct btrfs_disk_key key; + u32 nritems = btrfs_header_nritems(buf); if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root)) return 1; if (parent_key->type) { - if (memcmp(parent_key, &node->ptrs[0].key, - sizeof(struct btrfs_disk_key))) + btrfs_node_key(buf, &key, 0); + if (memcmp(parent_key, &key, sizeof(key))) return 1; } for (i = 0; nritems > 1 && i < nritems - 2; i++) { - struct btrfs_key cpukey; - btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key); - if (btrfs_comp_keys(&node->ptrs[i].key, &cpukey) >= 0) + btrfs_node_key(buf, &key, i); + btrfs_node_key_to_cpu(buf, &cpukey, i + 1); + if (btrfs_comp_keys(&key, &cpukey) >= 0) return 1; } return 0; @@ -86,44 +88,44 @@ static int check_node(struct btrfs_root static int check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key, - struct btrfs_leaf *leaf) + struct extent_buffer *buf) { int i; - u32 nritems = btrfs_header_nritems(&leaf->header); - - if (btrfs_header_level(&leaf->header) != 0) { + struct btrfs_key cpukey; + struct btrfs_disk_key key; + u32 nritems = btrfs_header_nritems(buf); + + if (btrfs_header_level(buf) != 0) { fprintf(stderr, "leaf is not a leaf %llu\n", - (unsigned long long)btrfs_header_bytenr(&leaf->header)); + (unsigned long long)btrfs_header_bytenr(buf)); return 1; } - if (btrfs_leaf_free_space(root, leaf) < 0) { + if (btrfs_leaf_free_space(root, buf) < 0) { fprintf(stderr, "leaf free space incorrect %llu %d\n", - (unsigned long long)btrfs_header_bytenr(&leaf->header), - btrfs_leaf_free_space(root, leaf)); + (unsigned long long)btrfs_header_bytenr(buf), + btrfs_leaf_free_space(root, buf)); return 1; } if (nritems == 0) return 0; - if (parent_key->type && memcmp(parent_key, &leaf->items[0].key, - sizeof(struct btrfs_disk_key))) { + btrfs_item_key(buf, &key, 0); + if (parent_key->type && memcmp(parent_key, &key, sizeof(key))) { fprintf(stderr, "leaf parent key incorrect %llu\n", - (unsigned long long)btrfs_header_bytenr(&leaf->header)); + (unsigned long long)btrfs_header_bytenr(buf)); return 1; } for (i = 0; nritems > 1 && i < nritems - 2; i++) { - struct btrfs_key cpukey; - btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); - if (btrfs_comp_keys(&leaf->items[i].key, - &cpukey) >= 0) + btrfs_item_key(buf, &key, i); + btrfs_item_key_to_cpu(buf, &cpukey, i + 1); + if (btrfs_comp_keys(&key, &cpukey) >= 0) return 1; - if (btrfs_item_offset(leaf->items + i) != - btrfs_item_end(leaf->items + i + 1)) + if (btrfs_item_offset_nr(buf, i) != + btrfs_item_end_nr(buf, i + 1)) return 1; if (i == 0) { - if (btrfs_item_offset(leaf->items + i) + - btrfs_item_size(leaf->items + i) != + if (btrfs_item_end_nr(buf, i) != BTRFS_LEAF_DATA_SIZE(root)) return 1; } @@ -206,20 +208,20 @@ static int maybe_free_extent_rec(struct static int check_block(struct btrfs_root *root, struct cache_tree *extent_cache, - struct btrfs_buffer *buf) + struct extent_buffer *buf) { struct extent_record *rec; struct cache_extent *cache; int ret = 1; - cache = find_cache_extent(extent_cache, buf->bytenr, buf->size); + cache = find_cache_extent(extent_cache, buf->start, buf->len); if (!cache) return 1; rec = container_of(cache, struct extent_record, cache); - if (btrfs_is_leaf(&buf->node)) { - ret = check_leaf(root, &rec->parent_key, &buf->leaf); + if (btrfs_is_leaf(buf)) { + ret = check_leaf(root, &rec->parent_key, buf); } else { - ret = check_node(root, &rec->parent_key, &buf->node); + ret = check_node(root, &rec->parent_key, buf); } rec->checked = 1; if (!ret) @@ -470,7 +472,7 @@ static int pick_next_pending(struct cach } return ret; } -static struct btrfs_buffer reada_buf; +static struct extent_buffer reada_buf; static int run_next_block(struct btrfs_root *root, struct block_info *bits, @@ -482,16 +484,14 @@ static int run_next_block(struct btrfs_r struct cache_tree *nodes, struct cache_tree *extent_cache) { - struct btrfs_buffer *buf; + struct extent_buffer *buf; u64 bytenr; u32 size; int ret; int i; int nritems; struct btrfs_extent_ref *ref; - struct btrfs_leaf *leaf; - struct btrfs_node *node; - struct btrfs_disk_key *disk_key; + struct btrfs_disk_key disk_key; struct cache_extent *cache; int reada_bits; @@ -534,41 +534,41 @@ static int run_next_block(struct btrfs_r } buf = read_tree_block(root, bytenr, size); - nritems = btrfs_header_nritems(&buf->node.header); + nritems = btrfs_header_nritems(buf); ret = check_block(root, extent_cache, buf); if (ret) { fprintf(stderr, "bad block %llu\n", (unsigned long long)bytenr); } - if (btrfs_is_leaf(&buf->node)) { - leaf = &buf->leaf; - btree_space_waste += btrfs_leaf_free_space(root, leaf); + if (btrfs_is_leaf(buf)) { + btree_space_waste += btrfs_leaf_free_space(root, buf); for (i = 0; i < nritems; i++) { struct btrfs_file_extent_item *fi; - disk_key = &leaf->items[i].key; - if (btrfs_disk_key_type(disk_key) == + btrfs_item_key(buf, &disk_key, i); + if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_ITEM_KEY) { struct btrfs_key found; struct btrfs_extent_item *ei; - btrfs_disk_key_to_cpu(&found, disk_key); - ei = btrfs_item_ptr(leaf, i, + btrfs_disk_key_to_cpu(&found, &disk_key); + ei = btrfs_item_ptr(buf, i, struct btrfs_extent_item); add_extent_rec(extent_cache, NULL, 0, found.objectid, found.offset, - btrfs_extent_refs(ei), 0, 0); + btrfs_extent_refs(buf, ei), + 0, 0); continue; } - if (btrfs_disk_key_type(disk_key) == + if (btrfs_disk_key_type(&disk_key) == BTRFS_CSUM_ITEM_KEY) { total_csum_bytes += - btrfs_item_size(leaf->items + i); + btrfs_item_size_nr(buf, i); continue; } - if (btrfs_disk_key_type(disk_key) == + if (btrfs_disk_key_type(&disk_key) == BTRFS_BLOCK_GROUP_ITEM_KEY) { struct btrfs_block_group_item *bi; - bi = btrfs_item_ptr(leaf, i, + bi = btrfs_item_ptr(buf, i, struct btrfs_block_group_item); #if 0 fprintf(stderr,"block group %Lu %Lu used %Lu ", @@ -579,64 +579,64 @@ static int run_next_block(struct btrfs_r #endif continue; } - if (btrfs_disk_key_type(disk_key) == + if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_REF_KEY) { - ref = btrfs_item_ptr(leaf, i, + ref = btrfs_item_ptr(buf, i, struct btrfs_extent_ref); add_backref(extent_cache, - btrfs_disk_key_objectid(disk_key), - btrfs_ref_root(ref), - btrfs_ref_generation(ref), - btrfs_ref_objectid(ref), - btrfs_ref_offset(ref), 0); + btrfs_disk_key_objectid(&disk_key), + btrfs_ref_root(buf, ref), + btrfs_ref_generation(buf, ref), + btrfs_ref_objectid(buf, ref), + btrfs_ref_offset(buf, ref), 0); continue; } - if (btrfs_disk_key_type(disk_key) != + if (btrfs_disk_key_type(&disk_key) != BTRFS_EXTENT_DATA_KEY) continue; - fi = btrfs_item_ptr(leaf, i, + fi = btrfs_item_ptr(buf, i, struct btrfs_file_extent_item); - if (btrfs_file_extent_type(fi) != + if (btrfs_file_extent_type(buf, fi) != BTRFS_FILE_EXTENT_REG) continue; - if (btrfs_file_extent_disk_bytenr(fi) == 0) + if (btrfs_file_extent_disk_bytenr(buf, fi) == 0) continue; data_bytes_allocated += - btrfs_file_extent_disk_num_bytes(fi); + btrfs_file_extent_disk_num_bytes(buf, fi); data_bytes_referenced += - btrfs_file_extent_num_bytes(fi); + btrfs_file_extent_num_bytes(buf, fi); ret = add_extent_rec(extent_cache, NULL, bytenr, - btrfs_file_extent_disk_bytenr(fi), - btrfs_file_extent_disk_num_bytes(fi), + btrfs_file_extent_disk_bytenr(buf, fi), + btrfs_file_extent_disk_num_bytes(buf, fi), 0, 1, 1); add_backref(extent_cache, - btrfs_file_extent_disk_bytenr(fi), - btrfs_header_owner(&leaf->header), - btrfs_header_generation(&leaf->header), - btrfs_disk_key_objectid(disk_key), - btrfs_disk_key_offset(disk_key), 1); + btrfs_file_extent_disk_bytenr(buf, fi), + btrfs_header_owner(buf), + btrfs_header_generation(buf), + btrfs_disk_key_objectid(&disk_key), + btrfs_disk_key_offset(&disk_key), 1); BUG_ON(ret); } } else { int level; - node = &buf->node; - level = btrfs_header_level(&node->header); + level = btrfs_header_level(buf); for (i = 0; i < nritems; i++) { - u64 ptr = btrfs_node_blockptr(node, i); + u64 ptr = btrfs_node_blockptr(buf, i); u32 size = btrfs_level_size(root, level - 1); + btrfs_node_key(buf, &disk_key, i); ret = add_extent_rec(extent_cache, - &node->ptrs[i].key, + &disk_key, bytenr, ptr, size, 0, 1, 0); BUG_ON(ret); add_backref(extent_cache, ptr, - btrfs_header_owner(&node->header), - btrfs_header_generation(&node->header), + btrfs_header_owner(buf), + btrfs_header_generation(buf), level - 1, - btrfs_disk_key_objectid(&node->ptrs[i].key), 1); + btrfs_disk_key_objectid(&disk_key), 1); if (level > 1) { add_pending(nodes, seen, ptr, size); @@ -647,12 +647,12 @@ static int run_next_block(struct btrfs_r btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) - nritems) * sizeof(struct btrfs_key_ptr); } - total_btree_bytes += buf->size; - btrfs_block_release(root, buf); + total_btree_bytes += buf->len; + free_extent_buffer(buf); return 0; } -static int add_root_to_pending(struct btrfs_buffer *buf, +static int add_root_to_pending(struct extent_buffer *buf, struct block_info *bits, int bits_nr, struct cache_tree *extent_cache, @@ -661,16 +661,16 @@ static int add_root_to_pending(struct bt struct cache_tree *reada, struct cache_tree *nodes, u64 root_objectid) { - if (btrfs_header_level(&buf->node.header) > 0) - add_pending(nodes, seen, buf->bytenr, buf->size); + if (btrfs_header_level(buf) > 0) + add_pending(nodes, seen, buf->start, buf->len); else - add_pending(pending, seen, buf->bytenr, buf->size); - add_extent_rec(extent_cache, NULL, 0, buf->bytenr, buf->size, + add_pending(pending, seen, buf->start, buf->len); + add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len, 0, 1, 0); - add_backref(extent_cache, buf->bytenr, root_objectid, - btrfs_header_generation(&buf->node.header), - btrfs_header_level(&buf->node.header), 0, 1); + add_backref(extent_cache, buf->start, root_objectid, + btrfs_header_generation(buf), + btrfs_header_level(buf), 0, 1); return 0; } @@ -710,7 +710,6 @@ int check_extent_refs(struct btrfs_root } int main(int ac, char **av) { - struct btrfs_super_block super; struct btrfs_root *root; struct cache_tree extent_cache; struct cache_tree seen; @@ -724,9 +723,9 @@ int main(int ac, char **av) { u64 last = 0; struct block_info *bits; int bits_nr; - struct btrfs_leaf *leaf; + struct extent_buffer *leaf; int slot; - struct btrfs_root_item *ri; + struct btrfs_root_item ri; radix_tree_init(); cache_tree_init(&extent_cache); @@ -735,7 +734,7 @@ int main(int ac, char **av) { cache_tree_init(&nodes); cache_tree_init(&reada); - root = open_ctree(av[1], &super); + root = open_ctree(av[1], 0); bits_nr = 1024; bits = malloc(bits_nr * sizeof(struct block_info)); @@ -756,30 +755,30 @@ int main(int ac, char **av) { &key, &path, 0, 0); BUG_ON(ret < 0); while(1) { - leaf = &path.nodes[0]->leaf; + leaf = path.nodes[0]; slot = path.slots[0]; - if (slot >= btrfs_header_nritems(&leaf->header)) { + if (slot >= btrfs_header_nritems(path.nodes[0])) { ret = btrfs_next_leaf(root, &path); if (ret != 0) break; - leaf = &path.nodes[0]->leaf; + leaf = path.nodes[0]; slot = path.slots[0]; } - btrfs_disk_key_to_cpu(&found_key, - &leaf->items[path.slots[0]].key); + btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]); if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) { - struct btrfs_buffer *buf; - - ri = btrfs_item_ptr(leaf, path.slots[0], - struct btrfs_root_item); + unsigned long offset; + struct extent_buffer *buf; + + offset = btrfs_item_ptr_offset(leaf, path.slots[0]); + read_extent_buffer(leaf, &ri, offset, sizeof(ri)); buf = read_tree_block(root->fs_info->tree_root, - btrfs_root_bytenr(ri), + btrfs_root_bytenr(&ri), btrfs_level_size(root, - btrfs_root_level(ri))); + btrfs_root_level(&ri))); add_root_to_pending(buf, bits, bits_nr, &extent_cache, &pending, &seen, &reada, &nodes, found_key.objectid); - btrfs_block_release(root->fs_info->tree_root, buf); + free_extent_buffer(buf); } path.slots[0]++; } @@ -791,7 +790,7 @@ int main(int ac, char **av) { break; } ret = check_extent_refs(root, &extent_cache); - close_ctree(root, &super); + close_ctree(root); printf("found %llu bytes used err is %d\n", (unsigned long long)bytes_used, ret); printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes); diff -r 59fc7ac63e46 ctree.c --- a/ctree.c Fri Dec 21 16:25:35 2007 -0500 +++ b/ctree.c Fri Jan 04 19:19:49 2008 +0800 @@ -16,9 +16,7 @@ * Boston, MA 021110-1307, USA. */ -#include -#include -#include "kerncompat.h" +#include #include "ctree.h" #include "disk-io.h" #include "transaction.h" @@ -29,18 +27,36 @@ static int split_leaf(struct btrfs_trans static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_key *ins_key, struct btrfs_path *path, int data_size, int extend); -static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_buffer *dst, struct btrfs_buffer - *src); -static int balance_node_right(struct btrfs_trans_handle *trans, struct - btrfs_root *root, struct btrfs_buffer *dst_buf, - struct btrfs_buffer *src_buf); +static int push_node_left(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct extent_buffer *dst, + struct extent_buffer *src); +static int balance_node_right(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_buffer *dst_buf, + struct extent_buffer *src_buf); static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, int level, int slot); inline void btrfs_init_path(struct btrfs_path *p) { memset(p, 0, sizeof(*p)); +} + +struct btrfs_path *btrfs_alloc_path(void) +{ + struct btrfs_path *path; + path = kmalloc(sizeof(struct btrfs_path), GFP_NOFS); + if (path) { + btrfs_init_path(path); + path->reada = 0; + } + return path; +} + +void btrfs_free_path(struct btrfs_path *p) +{ + btrfs_release_path(NULL, p); + kfree(p); } void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) @@ -49,100 +65,196 @@ void btrfs_release_path(struct btrfs_roo for (i = 0; i < BTRFS_MAX_LEVEL; i++) { if (!p->nodes[i]) break; - btrfs_block_release(root, p->nodes[i]); + free_extent_buffer(p->nodes[i]); } memset(p, 0, sizeof(*p)); } -int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_buffer *buf, struct btrfs_buffer - *parent, int parent_slot, struct btrfs_buffer - **cow_ret) -{ - struct btrfs_buffer *cow; + +int btrfs_copy_root(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_buffer *buf, + struct extent_buffer **cow_ret, u64 new_root_objectid) +{ + struct extent_buffer *cow; + u32 nritems; + int ret = 0; + int level; + struct btrfs_key first_key; + struct btrfs_root *new_root; + + new_root = kmalloc(sizeof(*new_root), GFP_NOFS); + if (!new_root) + return -ENOMEM; + + memcpy(new_root, root, sizeof(*new_root)); + new_root->root_key.objectid = new_root_objectid; + + WARN_ON(root->ref_cows && trans->transid != + root->fs_info->running_transaction->transid); + WARN_ON(root->ref_cows && trans->transid != root->last_trans); + + level = btrfs_header_level(buf); + nritems = btrfs_header_nritems(buf); + if (nritems) { + if (level == 0) + btrfs_item_key_to_cpu(buf, &first_key, 0); + else + btrfs_node_key_to_cpu(buf, &first_key, 0); + } else { + first_key.objectid = 0; + } + cow = __btrfs_alloc_free_block(trans, new_root, buf->len, + new_root_objectid, + trans->transid, first_key.objectid, + level, buf->start, 0); + if (IS_ERR(cow)) { + kfree(new_root); + return PTR_ERR(cow); + } + + copy_extent_buffer(cow, buf, 0, 0, cow->len); + btrfs_set_header_bytenr(cow, cow->start); + btrfs_set_header_generation(cow, trans->transid); + btrfs_set_header_owner(cow, new_root_objectid); + + WARN_ON(btrfs_header_generation(buf) > trans->transid); + ret = btrfs_inc_ref(trans, new_root, buf); + kfree(new_root); + + if (ret) + return ret; + + btrfs_mark_buffer_dirty(cow); + *cow_ret = cow; + return 0; +} + +int __btrfs_cow_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_buffer *buf, + struct extent_buffer *parent, int parent_slot, + struct extent_buffer **cow_ret, + u64 search_start, u64 empty_size) +{ u64 root_gen; - - if (!list_empty(&buf->dirty)) { + struct extent_buffer *cow; + u32 nritems; + int ret = 0; + int different_trans = 0; + int level; + struct btrfs_key first_key; + + if (root->ref_cows) { + root_gen = trans->transid; + } else { + root_gen = 0; + } + + WARN_ON(root->ref_cows && trans->transid != + root->fs_info->running_transaction->transid); + WARN_ON(root->ref_cows && trans->transid != root->last_trans); + + level = btrfs_header_level(buf); + nritems = btrfs_header_nritems(buf); + if (nritems) { + if (level == 0) + btrfs_item_key_to_cpu(buf, &first_key, 0); + else + btrfs_node_key_to_cpu(buf, &first_key, 0); + } else { + first_key.objectid = 0; + } + cow = __btrfs_alloc_free_block(trans, root, buf->len, + root->root_key.objectid, + root_gen, first_key.objectid, level, + search_start, empty_size); + if (IS_ERR(cow)) + return PTR_ERR(cow); + + copy_extent_buffer(cow, buf, 0, 0, cow->len); + btrfs_set_header_bytenr(cow, cow->start); + btrfs_set_header_generation(cow, trans->transid); + btrfs_set_header_owner(cow, root->root_key.objectid); + + WARN_ON(btrfs_header_generation(buf) > trans->transid); + if (btrfs_header_generation(buf) != trans->transid) { + different_trans = 1; + ret = btrfs_inc_ref(trans, root, buf); + if (ret) + return ret; + } else { + clean_tree_block(trans, root, buf); + } + + if (buf == root->node) { + root_gen = btrfs_header_generation(buf); + root->node = cow; + extent_buffer_get(cow); + if (buf != root->commit_root) { + btrfs_free_extent(trans, root, buf->start, + buf->len, root->root_key.objectid, + root_gen, 0, 0, 1); + } + free_extent_buffer(buf); + } else { + root_gen = btrfs_header_generation(parent); + btrfs_set_node_blockptr(parent, parent_slot, + cow->start); + WARN_ON(trans->transid == 0); + btrfs_set_node_ptr_generation(parent, parent_slot, + trans->transid); + btrfs_mark_buffer_dirty(parent); + WARN_ON(btrfs_header_generation(parent) != trans->transid); + btrfs_free_extent(trans, root, buf->start, buf->len, + btrfs_header_owner(parent), root_gen, + 0, 0, 1); + } + free_extent_buffer(buf); + btrfs_mark_buffer_dirty(cow); + *cow_ret = cow; + return 0; +} + +int btrfs_cow_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct extent_buffer *buf, + struct extent_buffer *parent, int parent_slot, + struct extent_buffer **cow_ret) +{ + u64 search_start; + int ret; + /* + if (trans->transaction != root->fs_info->running_transaction) { + printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, + root->fs_info->running_transaction->transid); + WARN_ON(1); + } + */ + if (trans->transid != root->fs_info->generation) { + printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, + root->fs_info->generation); + WARN_ON(1); + } + if (btrfs_header_generation(buf) == trans->transid) { *cow_ret = buf; return 0; } - cow = btrfs_alloc_free_block(trans, root, buf->size); - memcpy(&cow->node, &buf->node, buf->size); - btrfs_set_header_bytenr(&cow->node.header, cow->bytenr); - btrfs_set_header_generation(&cow->node.header, trans->transid); - btrfs_set_header_owner(&cow->node.header, root->root_key.objectid); - *cow_ret = cow; - btrfs_inc_ref(trans, root, buf); - if (buf == root->node) { - root_gen = btrfs_header_generation(&buf->node.header); - root->node = cow; - cow->count++; - if (buf != root->commit_root) - btrfs_free_extent(trans, root, buf->bytenr, - buf->size, root->root_key.objectid, - root_gen, 0, 0, 1); - btrfs_block_release(root, buf); - } else { - root_gen = btrfs_header_generation(&parent->node.header); - btrfs_set_node_blockptr(&parent->node, parent_slot, - cow->bytenr); - btrfs_set_node_ptr_generation(&parent->node, parent_slot, - trans->transid); - BUG_ON(list_empty(&parent->dirty)); - btrfs_free_extent(trans, root, buf->bytenr, buf->size, - root->root_key.objectid, root_gen, 0, 0, 1); - } - btrfs_block_release(root, buf); + + search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1); + ret = __btrfs_cow_block(trans, root, buf, parent, + parent_slot, cow_ret, search_start, 0); + return ret; +} + +/* +static int close_blocks(u64 blocknr, u64 other, u32 blocksize) +{ + if (blocknr < other && other - (blocknr + blocksize) < 32768) + return 1; + if (blocknr > other && blocknr - (other + blocksize) < 32768) + return 1; return 0; } - -/* - * 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 - */ -static inline unsigned int leaf_data_end(struct btrfs_root *root, - struct btrfs_leaf *leaf) -{ - u32 nr = btrfs_header_nritems(&leaf->header); - if (nr == 0) - return BTRFS_LEAF_DATA_SIZE(root); - return btrfs_item_offset(leaf->items + nr - 1); -} - -/* - * 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 - */ -static int leaf_space_used(struct btrfs_leaf *l, int start, int nr) -{ - int data_len; - int nritems = btrfs_header_nritems(&l->header); - int end; - - if (nritems < start + nr) - end = nritems - 1; - else - end = start + nr - 1; - - if (!nr) - return 0; - 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; - return data_len; -} - -/* - * 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 - */ -int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) -{ - int nritems = btrfs_header_nritems(&leaf->header); - return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); -} +*/ /* * compare two keys in a memcmp fashion @@ -168,32 +280,183 @@ int btrfs_comp_keys(struct btrfs_disk_ke return 0; } + +#if 0 +int btrfs_realloc_node(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct extent_buffer *parent, + int start_slot, int cache_only, u64 *last_ret, + struct btrfs_key *progress) +{ + struct extent_buffer *cur; + struct extent_buffer *tmp; + u64 blocknr; + u64 search_start = *last_ret; + u64 last_block = 0; + u64 other; + u32 parent_nritems; + int end_slot; + int i; + int err = 0; + int parent_level; + int uptodate; + u32 blocksize; + int progress_passed = 0; + struct btrfs_disk_key disk_key; + + parent_level = btrfs_header_level(parent); + if (cache_only && parent_level != 1) + return 0; + + if (trans->transaction != root->fs_info->running_transaction) { + printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, + root->fs_info->running_transaction->transid); + WARN_ON(1); + } + if (trans->transid != root->fs_info->generation) { + printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid, + root->fs_info->generation); + WARN_ON(1); + } + + parent_nritems = btrfs_header_nritems(parent); + blocksize = btrfs_level_size(root, parent_level - 1); + end_slot = parent_nritems; + + if (parent_nritems == 1) + return 0; + + for (i = start_slot; i < end_slot; i++) { + int close = 1; + + if (!parent->map_token) { + map_extent_buffer(parent, + btrfs_node_key_ptr_offset(i), + sizeof(struct btrfs_key_ptr), + &parent->map_token, &parent->kaddr, + &parent->map_start, &parent->map_len, + KM_USER1); + } + btrfs_node_key(parent, &disk_key, i); + if (!progress_passed && comp_keys(&disk_key, progress) < 0) + continue; + + progress_passed = 1; + blocknr = btrfs_node_blockptr(parent, i); + if (last_block == 0) + last_block = blocknr; + + if (i > 0) { + other = btrfs_node_blockptr(parent, i - 1); + close = close_blocks(blocknr, other, blocksize); + } + if (close && i < end_slot - 2) { + other = btrfs_node_blockptr(parent, i + 1); + close = close_blocks(blocknr, other, blocksize); + } + if (close) { + last_block = blocknr; + continue; + } + if (parent->map_token) { + unmap_extent_buffer(parent, parent->map_token, + KM_USER1); + parent->map_token = NULL; + } + + cur = btrfs_find_tree_block(root, blocknr, blocksize); + if (cur) + uptodate = btrfs_buffer_uptodate(cur); + else + uptodate = 0; + if (!cur || !uptodate) { + if (cache_only) { + free_extent_buffer(cur); + continue; + } + if (!cur) { + cur = read_tree_block(root, blocknr, + blocksize); + } else if (!uptodate) { + btrfs_read_buffer(cur); + } + } + if (search_start == 0) + search_start = last_block; + + err = __btrfs_cow_block(trans, root, cur, parent, i, + &tmp, search_start, + min(16 * blocksize, + (end_slot - i) * blocksize)); + if (err) { + free_extent_buffer(cur); + break; + } + search_start = tmp->start; + last_block = tmp->start; + *last_ret = search_start; + if (parent_level == 1) + btrfs_clear_buffer_defrag(tmp); + free_extent_buffer(tmp); + } + if (parent->map_token) { + unmap_extent_buffer(parent, parent->map_token, + KM_USER1); + parent->map_token = NULL; + } + return err; +} +#endif + +/* + * 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 + */ +static inline unsigned int leaf_data_end(struct btrfs_root *root, + struct extent_buffer *leaf) +{ + u32 nr = btrfs_header_nritems(leaf); + if (nr == 0) + return BTRFS_LEAF_DATA_SIZE(root); + return btrfs_item_offset_nr(leaf, nr - 1); +} + static int check_node(struct btrfs_root *root, struct btrfs_path *path, int level) { - int i; - struct btrfs_node *parent = NULL; - struct btrfs_node *node = &path->nodes[level]->node; + struct extent_buffer *parent = NULL; + struct extent_buffer *node = path->nodes[level]; + struct btrfs_disk_key parent_key; + struct btrfs_disk_key node_key; int parent_slot; - u32 nritems = btrfs_header_nritems(&node->header); + int slot; + struct btrfs_key cpukey; + u32 nritems = btrfs_header_nritems(node); if (path->nodes[level + 1]) - parent = &path->nodes[level + 1]->node; - parent_slot = path->slots[level + 1]; + parent = path->nodes[level + 1]; + + slot = path->slots[level]; BUG_ON(nritems == 0); if (parent) { - struct btrfs_disk_key *parent_key; - parent_key = &parent->ptrs[parent_slot].key; - BUG_ON(memcmp(parent_key, &node->ptrs[0].key, + parent_slot = path->slots[level + 1]; + btrfs_node_key(parent, &parent_key, parent_slot); + btrfs_node_key(node, &node_key, 0); + BUG_ON(memcmp(&parent_key, &node_key, sizeof(struct btrfs_disk_key))); BUG_ON(btrfs_node_blockptr(parent, parent_slot) != - btrfs_header_bytenr(&node->header)); + btrfs_header_bytenr(node)); } BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); - for (i = 0; nritems > 1 && i < nritems - 2; i++) { - struct btrfs_key cpukey; - btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key); - BUG_ON(btrfs_comp_keys(&node->ptrs[i].key, &cpukey) >= 0); + if (slot != 0) { + btrfs_node_key_to_cpu(node, &cpukey, slot - 1); + btrfs_node_key(node, &node_key, slot); + BUG_ON(btrfs_comp_keys(&node_key, &cpukey) <= 0); + } + if (slot < nritems - 1) { + btrfs_node_key_to_cpu(node, &cpukey, slot + 1); + btrfs_node_key(node, &node_key, slot); + BUG_ON(btrfs_comp_keys(&node_key, &cpukey) >= 0); } return 0; } @@ -201,73 +464,141 @@ static int check_leaf(struct btrfs_root static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, int level) { - int i; - struct btrfs_leaf *leaf = &path->nodes[level]->leaf; - struct btrfs_node *parent = NULL; + struct extent_buffer *leaf = path->nodes[level]; + struct extent_buffer *parent = NULL; int parent_slot; - u32 nritems = btrfs_header_nritems(&leaf->header); + struct btrfs_key cpukey; + struct btrfs_disk_key parent_key; + struct btrfs_disk_key leaf_key; + int slot = path->slots[0]; + + u32 nritems = btrfs_header_nritems(leaf); if (path->nodes[level + 1]) - parent = &path->nodes[level + 1]->node; - parent_slot = path->slots[level + 1]; - BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); + parent = path->nodes[level + 1]; if (nritems == 0) return 0; if (parent) { - struct btrfs_disk_key *parent_key; - parent_key = &parent->ptrs[parent_slot].key; - BUG_ON(memcmp(parent_key, &leaf->items[0].key, + parent_slot = path->slots[level + 1]; + btrfs_node_key(parent, &parent_key, parent_slot); + btrfs_item_key(leaf, &leaf_key, 0); + + BUG_ON(memcmp(&parent_key, &leaf_key, sizeof(struct btrfs_disk_key))); BUG_ON(btrfs_node_blockptr(parent, parent_slot) != - btrfs_header_bytenr(&leaf->header)); - } + btrfs_header_bytenr(leaf)); + } +#if 0 for (i = 0; nritems > 1 && i < nritems - 2; i++) { - struct btrfs_key cpukey; - btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); - BUG_ON(btrfs_comp_keys(&leaf->items[i].key, - &cpukey) >= 0); - BUG_ON(btrfs_item_offset(leaf->items + i) != - btrfs_item_end(leaf->items + i + 1)); + btrfs_item_key_to_cpu(leaf, &cpukey, i + 1); + btrfs_item_key(leaf, &leaf_key, i); + if (comp_keys(&leaf_key, &cpukey) >= 0) { + btrfs_print_leaf(root, leaf); + printk("slot %d offset bad key\n", i); + BUG_ON(1); + } + if (btrfs_item_offset_nr(leaf, i) != + btrfs_item_end_nr(leaf, i + 1)) { + btrfs_print_leaf(root, leaf); + printk("slot %d offset bad\n", i); + BUG_ON(1); + } if (i == 0) { - BUG_ON(btrfs_item_offset(leaf->items + i) + - btrfs_item_size(leaf->items + i) != - BTRFS_LEAF_DATA_SIZE(root)); - } - } + if (btrfs_item_offset_nr(leaf, i) + + btrfs_item_size_nr(leaf, i) != + BTRFS_LEAF_DATA_SIZE(root)) { + btrfs_print_leaf(root, leaf); + printk("slot %d first offset bad\n", i); + BUG_ON(1); + } + } + } + if (nritems > 0) { + if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) { + btrfs_print_leaf(root, leaf); + printk("slot %d bad size \n", nritems - 1); + BUG_ON(1); + } + } +#endif + if (slot != 0 && slot < nritems - 1) { + btrfs_item_key(leaf, &leaf_key, slot); + btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); + if (btrfs_comp_keys(&leaf_key, &cpukey) <= 0) { + btrfs_print_leaf(root, leaf); + printk("slot %d offset bad key\n", slot); + BUG_ON(1); + } + if (btrfs_item_offset_nr(leaf, slot - 1) != + btrfs_item_end_nr(leaf, slot)) { + btrfs_print_leaf(root, leaf); + printk("slot %d offset bad\n", slot); + BUG_ON(1); + } + } + if (slot < nritems - 1) { + btrfs_item_key(leaf, &leaf_key, slot); + btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); + BUG_ON(btrfs_comp_keys(&leaf_key, &cpukey) >= 0); + if (btrfs_item_offset_nr(leaf, slot) != + btrfs_item_end_nr(leaf, slot + 1)) { + btrfs_print_leaf(root, leaf); + printk("slot %d offset bad\n", slot); + BUG_ON(1); + } + } + BUG_ON(btrfs_item_offset_nr(leaf, 0) + + btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); return 0; } -static int check_block(struct btrfs_root *root, struct btrfs_path *path, - int level) -{ +static int noinline check_block(struct btrfs_root *root, + struct btrfs_path *path, int level) +{ + return 0; +#if 0 + struct extent_buffer *buf = path->nodes[level]; + + if (memcmp_extent_buffer(buf, root->fs_info->fsid, + (unsigned long)btrfs_header_fsid(buf), + BTRFS_FSID_SIZE)) { + printk("warning bad block %Lu\n", buf->start); + return 1; + } +#endif if (level == 0) return check_leaf(root, path, level); return check_node(root, path, level); } /* - * search for key in the array p. items p are item_size apart - * and there are 'max' items in p + * search for key in the extent_buffer. The items start at offset p, + * and they are item_size apart. 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 */ -static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, - int max, int *slot) +static int generic_bin_search(struct extent_buffer *eb, unsigned long p, + int item_size, struct btrfs_key *key, + int max, int *slot) { int low = 0; int high = max; int mid; int ret; + unsigned long offset; struct btrfs_disk_key *tmp; while(low < high) { mid = (low + high) / 2; - tmp = (struct btrfs_disk_key *)(p + mid * item_size); + offset = p + mid * item_size; + + tmp = (struct btrfs_disk_key *)(eb->data + offset); ret = btrfs_comp_keys(tmp, key); if (ret < 0) @@ -287,148 +618,159 @@ static int generic_bin_search(char *p, i * simple bin_search frontend that does the right thing for * leaves vs nodes */ -static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot) -{ - if (btrfs_is_leaf(c)) { - struct btrfs_leaf *l = (struct btrfs_leaf *)c; - return generic_bin_search((void *)l->items, +static int bin_search(struct extent_buffer *eb, struct btrfs_key *key, + int level, int *slot) +{ + if (level == 0) { + return generic_bin_search(eb, + offsetof(struct btrfs_leaf, items), sizeof(struct btrfs_item), - key, btrfs_header_nritems(&c->header), + key, btrfs_header_nritems(eb), slot); } else { - return generic_bin_search((void *)c->ptrs, + return generic_bin_search(eb, + offsetof(struct btrfs_node, ptrs), sizeof(struct btrfs_key_ptr), - key, btrfs_header_nritems(&c->header), + key, btrfs_header_nritems(eb), slot); } return -1; } -static struct btrfs_buffer *read_node_slot(struct btrfs_root *root, - struct btrfs_buffer *parent_buf, - int slot) -{ - struct btrfs_node *node = &parent_buf->node; - int level = btrfs_header_level(&node->header); +static struct extent_buffer *read_node_slot(struct btrfs_root *root, + struct extent_buffer *parent, int slot) +{ if (slot < 0) return NULL; - if (slot >= btrfs_header_nritems(&node->header)) + if (slot >= btrfs_header_nritems(parent)) return NULL; - return read_tree_block(root, btrfs_node_blockptr(node, slot), - btrfs_level_size(root, level - 1)); -} - -static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, int level) -{ - 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; + return read_tree_block(root, btrfs_node_blockptr(parent, slot), + btrfs_level_size(root, btrfs_header_level(parent) - 1)); +} + +static int balance_level(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, int level) +{ + struct extent_buffer *right = NULL; + struct extent_buffer *mid; + struct extent_buffer *left = NULL; + struct extent_buffer *parent = NULL; int ret = 0; int wret; int pslot; int orig_slot = path->slots[level]; + int err_on_enospc = 0; u64 orig_ptr; if (level == 0) return 0; - mid_buf = path->nodes[level]; - mid = &mid_buf->node; + mid = path->nodes[level]; + WARN_ON(btrfs_header_generation(mid) != trans->transid); + orig_ptr = btrfs_node_blockptr(mid, orig_slot); if (level < BTRFS_MAX_LEVEL - 1) - parent_buf = path->nodes[level + 1]; + parent = path->nodes[level + 1]; pslot = path->slots[level + 1]; /* * deal with the case where there is only one pointer in the root * by promoting the node below to a root */ - if (!parent_buf) { - struct btrfs_buffer *child; - u64 bytenr = mid_buf->bytenr; - - if (btrfs_header_nritems(&mid->header) != 1) + if (!parent) { + struct extent_buffer *child; + + if (btrfs_header_nritems(mid) != 1) return 0; /* promote the child to a root */ - child = read_node_slot(root, mid_buf, 0); + child = read_node_slot(root, mid, 0); BUG_ON(!child); root->node = child; path->nodes[level] = NULL; + clean_tree_block(trans, root, mid); + wait_on_tree_block_writeback(root, mid); /* once for the path */ - btrfs_block_release(root, mid_buf); + free_extent_buffer(mid); + ret = btrfs_free_extent(trans, root, mid->start, mid->len, + root->root_key.objectid, + btrfs_header_generation(mid), 0, 0, 1); /* once for the root ptr */ - btrfs_block_release(root, mid_buf); - clean_tree_block(trans, root, mid_buf); - return btrfs_free_extent(trans, root, bytenr, root->nodesize, - root->root_key.objectid, - btrfs_header_generation(&mid->header), - 0, 0, 1); - } - parent = &parent_buf->node; - - if (btrfs_header_nritems(&mid->header) > + free_extent_buffer(mid); + return ret; + } + if (btrfs_header_nritems(mid) > BTRFS_NODEPTRS_PER_BLOCK(root) / 4) return 0; - left_buf = read_node_slot(root, parent_buf, pslot - 1); - right_buf = read_node_slot(root, parent_buf, pslot + 1); + if (btrfs_header_nritems(mid) < 2) + err_on_enospc = 1; + + left = read_node_slot(root, parent, pslot - 1); + if (left) { + wret = btrfs_cow_block(trans, root, left, + parent, pslot - 1, &left); + if (wret) { + ret = wret; + goto enospc; + } + } + right = read_node_slot(root, parent, pslot + 1); + if (right) { + wret = btrfs_cow_block(trans, root, right, + parent, pslot + 1, &right); + if (wret) { + ret = wret; + goto enospc; + } + } /* first, try to make some room in the middle buffer */ - if (left_buf) { - btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1, - &left_buf); - left = &left_buf->node; - orig_slot += btrfs_header_nritems(&left->header); - wret = push_node_left(trans, root, left_buf, mid_buf); + if (left) { + orig_slot += btrfs_header_nritems(left); + wret = push_node_left(trans, root, left, mid); if (wret < 0) ret = wret; + if (btrfs_header_nritems(mid) < 2) + err_on_enospc = 1; } /* * then try to empty the right most buffer into the middle */ - if (right_buf) { - btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1, - &right_buf); - right = &right_buf->node; - wret = push_node_left(trans, root, mid_buf, right_buf); - if (wret < 0) + if (right) { + wret = push_node_left(trans, root, mid, right); + if (wret < 0 && wret != -ENOSPC) ret = wret; - if (btrfs_header_nritems(&right->header) == 0) { - u64 generation; - u64 bytenr = right_buf->bytenr; - - generation = btrfs_header_generation(&parent->header); - btrfs_block_release(root, right_buf); - clean_tree_block(trans, root, right_buf); - right_buf = NULL; + if (btrfs_header_nritems(right) == 0) { + u64 bytenr = right->start; + u64 generation = btrfs_header_generation(parent); + u32 blocksize = right->len; + + clean_tree_block(trans, root, right); + wait_on_tree_block_writeback(root, right); + free_extent_buffer(right); right = NULL; wret = del_ptr(trans, root, path, level + 1, pslot + 1); if (wret) ret = wret; wret = btrfs_free_extent(trans, root, bytenr, - root->nodesize, - root->root_key.objectid, + blocksize, + btrfs_header_owner(parent), generation, 0, 0, 1); if (wret) ret = wret; } else { - memcpy(&parent->ptrs[pslot + 1].key, - &right->ptrs[0].key, - sizeof(struct btrfs_disk_key)); - BUG_ON(list_empty(&parent_buf->dirty)); - } - } - if (btrfs_header_nritems(&mid->header) == 1) { + struct btrfs_disk_key right_key; + btrfs_node_key(right, &right_key, 0); + btrfs_set_node_key(parent, &right_key, pslot + 1); + btrfs_mark_buffer_dirty(parent); + } + } + if (btrfs_header_nritems(mid) == 1) { /* * we're not allowed to leave a node with one item in the * tree during a delete. A deletion from lower in the tree @@ -438,75 +780,75 @@ static int balance_level(struct btrfs_tr * otherwise we would have pulled some pointers from the * right */ - BUG_ON(!left_buf); - wret = balance_node_right(trans, root, mid_buf, left_buf); - if (wret < 0) + BUG_ON(!left); + wret = balance_node_right(trans, root, mid, left); + if (wret < 0) { ret = wret; + goto enospc; + } BUG_ON(wret == 1); } - if (btrfs_header_nritems(&mid->header) == 0) { + if (btrfs_header_nritems(mid) == 0) { /* we've managed to empty the middle node, drop it */ - u64 bytenr = mid_buf->bytenr; - u64 generation; - - generation = btrfs_header_generation(&parent->header); - btrfs_block_release(root, mid_buf); - clean_tree_block(trans, root, mid_buf); - mid_buf = NULL; + u64 root_gen = btrfs_header_generation(parent); + u64 bytenr = mid->start; + u32 blocksize = mid->len; + clean_tree_block(trans, root, mid); + wait_on_tree_block_writeback(root, mid); + free_extent_buffer(mid); mid = NULL; wret = del_ptr(trans, root, path, level + 1, pslot); if (wret) ret = wret; - wret = btrfs_free_extent(trans, root, bytenr, root->nodesize, - root->root_key.objectid, - generation, 0, 0, 1); + wret = btrfs_free_extent(trans, root, bytenr, blocksize, + btrfs_header_owner(parent), + root_gen, 0, 0, 1); if (wret) ret = wret; } else { /* update the parent key to reflect our changes */ - memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key, - sizeof(struct btrfs_disk_key)); - BUG_ON(list_empty(&parent_buf->dirty)); + struct btrfs_disk_key mid_key; + btrfs_node_key(mid, &mid_key, 0); + btrfs_set_node_key(parent, &mid_key, pslot); + btrfs_mark_buffer_dirty(parent); } /* update the path */ - if (left_buf) { - if (btrfs_header_nritems(&left->header) > orig_slot) { - left_buf->count++; // released below - path->nodes[level] = left_buf; + if (left) { + if (btrfs_header_nritems(left) > orig_slot) { + extent_buffer_get(left); + path->nodes[level] = left; path->slots[level + 1] -= 1; path->slots[level] = orig_slot; - if (mid_buf) - btrfs_block_release(root, mid_buf); + if (mid) + free_extent_buffer(mid); } else { - orig_slot -= btrfs_header_nritems(&left->header); + orig_slot -= btrfs_header_nritems(left); path->slots[level] = orig_slot; } } /* double check we haven't messed things up */ check_block(root, path, level); - if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node, - path->slots[level])) + if (orig_ptr != + btrfs_node_blockptr(path->nodes[level], path->slots[level])) BUG(); - - if (right_buf) - btrfs_block_release(root, right_buf); - if (left_buf) - btrfs_block_release(root, left_buf); +enospc: + if (right) + free_extent_buffer(right); + if (left) + free_extent_buffer(left); return ret; } -static int push_nodes_for_insert(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_path *path, int level) -{ - struct btrfs_node *right; - struct btrfs_node *mid; - struct btrfs_node *left; - struct btrfs_node *parent; - struct btrfs_buffer *right_buf; - struct btrfs_buffer *mid_buf; - struct btrfs_buffer *left_buf; - struct btrfs_buffer *parent_buf = NULL; + +/* returns zero if the push worked, non-zero otherwise */ +static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, int level) +{ + struct extent_buffer *right = NULL; + struct extent_buffer *mid; + struct extent_buffer *left = NULL; + struct extent_buffer *parent = NULL; int ret = 0; int wret; int pslot; @@ -516,106 +858,176 @@ static int push_nodes_for_insert(struct if (level == 0) return 1; - mid_buf = path->nodes[level]; - mid = &mid_buf->node; + mid = path->nodes[level]; + WARN_ON(btrfs_header_generation(mid) != trans->transid); orig_ptr = btrfs_node_blockptr(mid, orig_slot); if (level < BTRFS_MAX_LEVEL - 1) - parent_buf = path->nodes[level + 1]; + parent = path->nodes[level + 1]; pslot = path->slots[level + 1]; - if (!parent_buf) + if (!parent) return 1; - parent = &parent_buf->node; - - left_buf = read_node_slot(root, parent_buf, pslot - 1); - left = &left_buf->node; + + left = read_node_slot(root, parent, pslot - 1); /* first, try to make some room in the middle buffer */ - if (left_buf) { + if (left) { u32 left_nr; - left_nr = btrfs_header_nritems(&left->header); + left_nr = btrfs_header_nritems(left); if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { wret = 1; } else { - ret = btrfs_cow_block(trans, root, left_buf, - parent_buf, pslot - 1, - &left_buf); - left = &left_buf->node; + ret = btrfs_cow_block(trans, root, left, parent, + pslot - 1, &left); if (ret) wret = 1; else { wret = push_node_left(trans, root, - left_buf, mid_buf); + left, mid); } } if (wret < 0) ret = wret; if (wret == 0) { + struct btrfs_disk_key disk_key; orig_slot += left_nr; - memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key, - sizeof(struct btrfs_disk_key)); - BUG_ON(list_empty(&parent_buf->dirty)); - if (btrfs_header_nritems(&left->header) > orig_slot) { - path->nodes[level] = left_buf; + btrfs_node_key(mid, &disk_key, 0); + btrfs_set_node_key(parent, &disk_key, pslot); + btrfs_mark_buffer_dirty(parent); + if (btrfs_header_nritems(left) > orig_slot) { + path->nodes[level] = left; path->slots[level + 1] -= 1; path->slots[level] = orig_slot; - btrfs_block_release(root, mid_buf); + free_extent_buffer(mid); } else { orig_slot -= - btrfs_header_nritems(&left->header); + btrfs_header_nritems(left); path->slots[level] = orig_slot; - btrfs_block_release(root, left_buf); + free_extent_buffer(left); } return 0; } - btrfs_block_release(root, left_buf); - } - - right_buf = read_node_slot(root, parent_buf, pslot + 1); - right = &right_buf->node; + free_extent_buffer(left); + } + right= read_node_slot(root, parent, pslot + 1); /* * then try to empty the right most buffer into the middle */ - if (right_buf) { + if (right) { u32 right_nr; - right_nr = btrfs_header_nritems(&right->header); + right_nr = btrfs_header_nritems(right); if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { wret = 1; } else { - ret = btrfs_cow_block(trans, root, right_buf, - parent_buf, pslot + 1, - &right_buf); - right = &right_buf->node; + ret = btrfs_cow_block(trans, root, right, + parent, pslot + 1, + &right); if (ret) wret = 1; else { wret = balance_node_right(trans, root, - right_buf, mid_buf); + right, mid); } } if (wret < 0) ret = wret; if (wret == 0) { - memcpy(&parent->ptrs[pslot + 1].key, - &right->ptrs[0].key, - sizeof(struct btrfs_disk_key)); - BUG_ON(list_empty(&parent_buf->dirty)); - if (btrfs_header_nritems(&mid->header) <= orig_slot) { - path->nodes[level] = right_buf; + struct btrfs_disk_key disk_key; + + btrfs_node_key(right, &disk_key, 0); + btrfs_set_node_key(parent, &disk_key, pslot + 1); + btrfs_mark_buffer_dirty(parent); + + if (btrfs_header_nritems(mid) <= orig_slot) { + path->nodes[level] = right; path->slots[level + 1] += 1; path->slots[level] = orig_slot - - btrfs_header_nritems(&mid->header); - btrfs_block_release(root, mid_buf); + btrfs_header_nritems(mid); + free_extent_buffer(mid); } else { - btrfs_block_release(root, right_buf); + free_extent_buffer(right); } return 0; } - btrfs_block_release(root, right_buf); + free_extent_buffer(right); } return 1; +} + +/* + * readahead one full node of leaves + */ +static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path, + int level, int slot, u64 objectid) +{ + struct extent_buffer *node; + struct btrfs_disk_key disk_key; + u32 nritems; + u64 search; + u64 lowest_read; + u64 highest_read; + u64 nread = 0; + int direction = path->reada; + struct extent_buffer *eb; + u32 nr; + u32 blocksize; + u32 nscan = 0; + + if (level != 1) + return; + + if (!path->nodes[level]) + return; + + node = path->nodes[level]; + search = btrfs_node_blockptr(node, slot); + blocksize = btrfs_level_size(root, level - 1); + eb = btrfs_find_tree_block(root, search, blocksize); + if (eb) { + free_extent_buffer(eb); + return; + } + + highest_read = search; + lowest_read = search; + + nritems = btrfs_header_nritems(node); + nr = slot; + while(1) { + if (direction < 0) { + if (nr == 0) + break; + nr--; + } else if (direction > 0) { + nr++; + if (nr >= nritems) + break; + } + if (path->reada < 0 && objectid) { + btrfs_node_key(node, &disk_key, nr); + if (btrfs_disk_key_objectid(&disk_key) != objectid) + break; + } + search = btrfs_node_blockptr(node, nr); + if ((search >= lowest_read && search <= highest_read) || + (search < lowest_read && lowest_read - search <= 32768) || + (search > highest_read && search - highest_read <= 32768)) { + readahead_tree_block(root, search, blocksize); + nread += blocksize; + } + nscan++; + if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32)) + break; + if(nread > (1024 * 1024) || nscan > 128) + break; + + if (search < lowest_read) + lowest_read = search; + if (search > highest_read) + highest_read = search; + } } /* @@ -635,47 +1047,57 @@ int btrfs_search_slot(struct btrfs_trans *root, struct btrfs_key *key, struct btrfs_path *p, int ins_len, int cow) { - struct btrfs_buffer *b; - struct btrfs_node *c; + struct extent_buffer *b; + u64 bytenr; + u64 ptr_gen; int slot; int ret; int level; - + int should_reada = p->reada; + u8 lowest_level = 0; + + lowest_level = p->lowest_level; + WARN_ON(lowest_level && ins_len); + WARN_ON(p->nodes[0] != NULL); + /* + WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); + */ again: b = root->node; - b->count++; + extent_buffer_get(b); while (b) { - level = btrfs_header_level(&b->node.header); + level = btrfs_header_level(b); if (cow) { int wret; wret = btrfs_cow_block(trans, root, b, - p->nodes[level + 1], - p->slots[level + 1], - &b); + p->nodes[level + 1], + p->slots[level + 1], + &b); if (wret) { - btrfs_block_release(root, b); + free_extent_buffer(b); return wret; } } BUG_ON(!cow && ins_len); - c = &b->node; + if (level != btrfs_header_level(b)) + WARN_ON(1); + level = btrfs_header_level(b); p->nodes[level] = b; ret = check_block(root, p, level); if (ret) return -1; - ret = bin_search(c, key, &slot); - if (!btrfs_is_leaf(c)) { + ret = bin_search(b, key, level, &slot); + if (level != 0) { if (ret && slot > 0) slot -= 1; p->slots[level] = slot; - if (ins_len > 0 && btrfs_header_nritems(&c->header) >= + if (ins_len > 0 && btrfs_header_nritems(b) >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { int sret = split_node(trans, root, p, level); BUG_ON(sret > 0); if (sret) return sret; b = p->nodes[level]; - c = &b->node; slot = p->slots[level]; } else if (ins_len < 0) { int sret = balance_level(trans, root, p, @@ -687,17 +1109,29 @@ again: btrfs_release_path(NULL, p); goto again; } - c = &b->node; slot = p->slots[level]; - BUG_ON(btrfs_header_nritems(&c->header) == 1); + BUG_ON(btrfs_header_nritems(b) == 1); } - b = read_tree_block(root, - btrfs_node_blockptr(c, slot), + /* this is only true while dropping a snapshot */ + if (level == lowest_level) + break; + bytenr = btrfs_node_blockptr(b, slot); + ptr_gen = btrfs_node_ptr_generation(b, slot); + if (should_reada) + reada_for_search(root, p, level, slot, + key->objectid); + b = read_tree_block(root, bytenr, btrfs_level_size(root, level - 1)); + if (ptr_gen != btrfs_header_generation(b)) { + printk("block %llu bad gen wanted %llu " + "found %llu\n", + (unsigned long long)b->start, + (unsigned long long)ptr_gen, + (unsigned long long)btrfs_header_generation(b)); + } } else { - struct btrfs_leaf *l = (struct btrfs_leaf *)c; p->slots[level] = slot; - if (ins_len > 0 && btrfs_leaf_free_space(root, l) < + if (ins_len > 0 && btrfs_leaf_free_space(root, b) < sizeof(struct btrfs_item) + ins_len) { int sret = split_leaf(trans, root, key, p, ins_len, ret == 0); @@ -705,11 +1139,9 @@ again: if (sret) return sret; } - BUG_ON(root->node->count == 1); return ret; } } - BUG_ON(root->node->count == 1); return 1; } @@ -723,20 +1155,21 @@ again: * If this fails to write a tree block, it returns -1, but continues * fixing up the blocks in ram so the tree is consistent. */ -static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, struct btrfs_disk_key - *key, int level) +static int fixup_low_keys(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct btrfs_path *path, + struct btrfs_disk_key *key, int level) { int i; int ret = 0; + struct extent_buffer *t; + for (i = level; i < BTRFS_MAX_LEVEL; i++) { - struct btrfs_node *t; int tslot = path->slots[i]; if (!path->nodes[i]) break; - t = &path->nodes[i]->node; - memcpy(&t->ptrs[tslot].key, key, sizeof(*key)); - BUG_ON(list_empty(&path->nodes[i]->dirty)); + t = path->nodes[i]; + btrfs_set_node_key(t, key, tslot); + btrfs_mark_buffer_dirty(path->nodes[i]); if (tslot != 0) break; } @@ -750,20 +1183,21 @@ static int fixup_low_keys(struct btrfs_t * 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. */ -static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_buffer *dst_buf, struct - btrfs_buffer *src_buf) -{ - struct btrfs_node *src = &src_buf->node; - struct btrfs_node *dst = &dst_buf->node; +static int push_node_left(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct extent_buffer *dst, + struct extent_buffer *src) +{ int push_items = 0; int src_nritems; int dst_nritems; int ret = 0; - src_nritems = btrfs_header_nritems(&src->header); - dst_nritems = btrfs_header_nritems(&dst->header); + src_nritems = btrfs_header_nritems(src); + dst_nritems = btrfs_header_nritems(dst); push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; + WARN_ON(btrfs_header_generation(src) != trans->transid); + WARN_ON(btrfs_header_generation(dst) != trans->transid); + if (push_items <= 0) { return 1; } @@ -771,17 +1205,21 @@ static int push_node_left(struct btrfs_t if (src_nritems < push_items) push_items = src_nritems; - memcpy(dst->ptrs + dst_nritems, src->ptrs, - push_items * sizeof(struct btrfs_key_ptr)); + copy_extent_buffer(dst, src, + btrfs_node_key_ptr_offset(dst_nritems), + btrfs_node_key_ptr_offset(0), + push_items * sizeof(struct btrfs_key_ptr)); + if (push_items < src_nritems) { - memmove(src->ptrs, src->ptrs + push_items, - (src_nritems - push_items) * - sizeof(struct btrfs_key_ptr)); - } - btrfs_set_header_nritems(&src->header, src_nritems - push_items); - btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); - BUG_ON(list_empty(&src_buf->dirty)); - BUG_ON(list_empty(&dst_buf->dirty)); + memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), + btrfs_node_key_ptr_offset(push_items), + (src_nritems - push_items) * + sizeof(struct btrfs_key_ptr)); + } + btrfs_set_header_nritems(src, src_nritems - push_items); + btrfs_set_header_nritems(dst, dst_nritems + push_items); + btrfs_mark_buffer_dirty(src); + btrfs_mark_buffer_dirty(dst); return ret; } @@ -794,41 +1232,49 @@ static int push_node_left(struct btrfs_t * * this will only push up to 1/2 the contents of the left node over */ -static int balance_node_right(struct btrfs_trans_handle *trans, struct - btrfs_root *root, struct btrfs_buffer *dst_buf, - struct btrfs_buffer *src_buf) -{ - struct btrfs_node *src = &src_buf->node; - struct btrfs_node *dst = &dst_buf->node; +static int balance_node_right(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_buffer *dst, + struct extent_buffer *src) +{ int push_items = 0; int max_push; int src_nritems; int dst_nritems; int ret = 0; - src_nritems = btrfs_header_nritems(&src->header); - dst_nritems = btrfs_header_nritems(&dst->header); + WARN_ON(btrfs_header_generation(src) != trans->transid); + WARN_ON(btrfs_header_generation(dst) != trans->transid); + + src_nritems = btrfs_header_nritems(src); + dst_nritems = btrfs_header_nritems(dst); push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; - if (push_items <= 0) { + 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; - 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)); - - btrfs_set_header_nritems(&src->header, src_nritems - push_items); - btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); - - BUG_ON(list_empty(&src_buf->dirty)); - BUG_ON(list_empty(&dst_buf->dirty)); + memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), + btrfs_node_key_ptr_offset(0), + (dst_nritems) * + sizeof(struct btrfs_key_ptr)); + + copy_extent_buffer(dst, src, + btrfs_node_key_ptr_offset(0), + btrfs_node_key_ptr_offset(src_nritems - push_items), + push_items * sizeof(struct btrfs_key_ptr)); + + btrfs_set_header_nritems(src, src_nritems - push_items); + btrfs_set_header_nritems(dst, dst_nritems + push_items); + + btrfs_mark_buffer_dirty(src); + btrfs_mark_buffer_dirty(dst); return ret; } @@ -839,43 +1285,73 @@ static int balance_node_right(struct btr * * returns zero on success or < 0 on failure. */ -static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, int level) -{ - struct btrfs_buffer *t; - struct btrfs_node *lower; - struct btrfs_node *c; - struct btrfs_disk_key *lower_key; +static int noinline insert_new_root(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, int level) +{ + u64 root_gen; + u64 lower_gen; + struct extent_buffer *lower; + struct extent_buffer *c; + struct btrfs_disk_key lower_key; BUG_ON(path->nodes[level]); BUG_ON(path->nodes[level-1] != root->node); - t = btrfs_alloc_free_block(trans, root, root->nodesize); - c = &t->node; - memset(&c->header, 0, sizeof(c->header)); - btrfs_set_header_nritems(&c->header, 1); - btrfs_set_header_level(&c->header, level); - btrfs_set_header_bytenr(&c->header, t->bytenr); - btrfs_set_header_generation(&c->header, trans->transid); - btrfs_set_header_owner(&c->header, root->root_key.objectid); - memcpy(c->header.fsid, root->fs_info->disk_super->fsid, - sizeof(c->header.fsid)); - lower = &path->nodes[level-1]->node; - - if (btrfs_is_leaf(lower)) - lower_key = &((struct btrfs_leaf *)lower)->items[0].key; + + if (root->ref_cows) + root_gen = trans->transid; else - lower_key = &lower->ptrs[0].key; - memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key)); - btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->bytenr); - BUG_ON(list_empty(&t->dirty)); - btrfs_set_node_ptr_generation(c, 0, - btrfs_header_generation(&path->nodes[level - 1]->node.header)); + root_gen = 0; + + lower = path->nodes[level-1]; + if (level == 1) + btrfs_item_key(lower, &lower_key, 0); + else + btrfs_node_key(lower, &lower_key, 0); + + c = __btrfs_alloc_free_block(trans, root, root->nodesize, + root->root_key.objectid, + root_gen, lower_key.objectid, level, + root->node->start, 0); + if (IS_ERR(c)) + return PTR_ERR(c); + memset_extent_buffer(c, 0, 0, root->nodesize); + btrfs_set_header_nritems(c, 1); + btrfs_set_header_level(c, level); + btrfs_set_header_bytenr(c, c->start); + btrfs_set_header_generation(c, trans->transid); + btrfs_set_header_owner(c, root->root_key.objectid); + + write_extent_buffer(c, root->fs_info->fsid, + (unsigned long)btrfs_header_fsid(c), + BTRFS_FSID_SIZE); + btrfs_set_node_key(c, &lower_key, 0); + btrfs_set_node_blockptr(c, 0, lower->start); + lower_gen = btrfs_header_generation(lower); + WARN_ON(lower_gen == 0); + + btrfs_set_node_ptr_generation(c, 0, lower_gen); + + btrfs_mark_buffer_dirty(c); + /* the super has an extra ref to root->node */ - btrfs_block_release(root, root->node); - root->node = t; - t->count++; - path->nodes[level] = t; + free_extent_buffer(root->node); + root->node = c; + extent_buffer_get(c); + path->nodes[level] = c; path->slots[level] = 0; + + if (root->ref_cows && lower_gen != trans->transid) { + struct btrfs_path *back_path = btrfs_alloc_path(); + int ret; + ret = btrfs_insert_extent_backref(trans, + root->fs_info->extent_root, + path, lower->start, + root->root_key.objectid, + trans->transid, 0, 0); + BUG_ON(ret); + btrfs_free_path(back_path); + } return 0; } @@ -884,7 +1360,7 @@ static int insert_new_root(struct btrfs_ * the node should have enough room for the pointer already * * slot and level indicate where you want the key to go, and - * bytenr is the block the key points to. + * blocknr is the block the key points to. * * returns zero on success and < 0 on any error */ @@ -892,25 +1368,28 @@ static int insert_ptr(struct btrfs_trans *root, struct btrfs_path *path, struct btrfs_disk_key *key, u64 bytenr, int slot, int level) { - struct btrfs_node *lower; + struct extent_buffer *lower; int nritems; BUG_ON(!path->nodes[level]); - lower = &path->nodes[level]->node; - nritems = btrfs_header_nritems(&lower->header); + lower = path->nodes[level]; + nritems = btrfs_header_nritems(lower); if (slot > nritems) BUG(); if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) BUG(); if (slot != nritems) { - memmove(lower->ptrs + slot + 1, lower->ptrs + slot, - (nritems - slot) * sizeof(struct btrfs_key_ptr)); - } - memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key)); + memmove_extent_buffer(lower, + btrfs_node_key_ptr_offset(slot + 1), + btrfs_node_key_ptr_offset(slot), + (nritems - slot) * sizeof(struct btrfs_key_ptr)); + } + btrfs_set_node_key(lower, key, slot); btrfs_set_node_blockptr(lower, slot, bytenr); + WARN_ON(trans->transid == 0); btrfs_set_node_ptr_generation(lower, slot, trans->transid); - btrfs_set_header_nritems(&lower->header, nritems + 1); - BUG_ON(list_empty(&path->nodes[level]->dirty)); + btrfs_set_header_nritems(lower, nritems + 1); + btrfs_mark_buffer_dirty(lower); return 0; } @@ -926,63 +1405,121 @@ static int split_node(struct btrfs_trans static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, int level) { - struct btrfs_buffer *t; - struct btrfs_node *c; - struct btrfs_buffer *split_buffer; - struct btrfs_node *split; + u64 root_gen; + struct extent_buffer *c; + struct extent_buffer *split; + struct btrfs_disk_key disk_key; int mid; int ret; int wret; u32 c_nritems; - t = path->nodes[level]; - c = &t->node; - if (t == root->node) { + c = path->nodes[level]; + WARN_ON(btrfs_header_generation(c) != trans->transid); + if (c == root->node) { /* trying to split the root, lets make a new one */ ret = insert_new_root(trans, root, path, level + 1); if (ret) return ret; } else { ret = push_nodes_for_insert(trans, root, path, level); - t = path->nodes[level]; - c = &t->node; - if (!ret && btrfs_header_nritems(&c->header) < + c = path->nodes[level]; + if (!ret && btrfs_header_nritems(c) < BTRFS_NODEPTRS_PER_BLOCK(root) - 1) return 0; if (ret < 0) return ret; } - c_nritems = btrfs_header_nritems(&c->header); - split_buffer = btrfs_alloc_free_block(trans, root, root->nodesize); - split = &split_buffer->node; - btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); - btrfs_set_header_level(&split->header, btrfs_header_level(&c->header)); - btrfs_set_header_bytenr(&split->header, split_buffer->bytenr); - btrfs_set_header_generation(&split->header, trans->transid); - btrfs_set_header_owner(&split->header, root->root_key.objectid); - memcpy(split->header.fsid, root->fs_info->disk_super->fsid, - sizeof(split->header.fsid)); + + c_nritems = btrfs_header_nritems(c); + if (root->ref_cows) + root_gen = trans->transid; + else + root_gen = 0; + + btrfs_node_key(c, &disk_key, 0); + split = __btrfs_alloc_free_block(trans, root, root->nodesize, + root->root_key.objectid, + root_gen, + btrfs_disk_key_objectid(&disk_key), + level, c->start, 0); + if (IS_ERR(split)) + return PTR_ERR(split); + + btrfs_set_header_flags(split, btrfs_header_flags(c)); + btrfs_set_header_level(split, btrfs_header_level(c)); + btrfs_set_header_bytenr(split, split->start); + btrfs_set_header_generation(split, trans->transid); + btrfs_set_header_owner(split, root->root_key.objectid); + write_extent_buffer(split, root->fs_info->fsid, + (unsigned long)btrfs_header_fsid(split), + BTRFS_FSID_SIZE); + mid = (c_nritems + 1) / 2; - memcpy(split->ptrs, c->ptrs + mid, - (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); - btrfs_set_header_nritems(&split->header, c_nritems - mid); - btrfs_set_header_nritems(&c->header, mid); + + copy_extent_buffer(split, c, + btrfs_node_key_ptr_offset(0), + btrfs_node_key_ptr_offset(mid), + (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); + btrfs_set_header_nritems(split, c_nritems - mid); + btrfs_set_header_nritems(c, mid); ret = 0; - BUG_ON(list_empty(&t->dirty)); - wret = insert_ptr(trans, root, path, &split->ptrs[0].key, - split_buffer->bytenr, path->slots[level + 1] + 1, + btrfs_mark_buffer_dirty(c); + btrfs_mark_buffer_dirty(split); + + btrfs_node_key(split, &disk_key, 0); + wret = insert_ptr(trans, root, path, &disk_key, split->start, + path->slots[level + 1] + 1, level + 1); if (wret) ret = wret; if (path->slots[level] >= mid) { path->slots[level] -= mid; - btrfs_block_release(root, t); - path->nodes[level] = split_buffer; + free_extent_buffer(c); + path->nodes[level] = split; path->slots[level + 1] += 1; } else { - btrfs_block_release(root, split_buffer); + free_extent_buffer(split); + } + return ret; +} + +/* + * 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 + */ +static int leaf_space_used(struct extent_buffer *l, int start, int nr) +{ + int data_len; + int nritems = btrfs_header_nritems(l); + int end = min(nritems, start + nr) - 1; + + if (!nr) + return 0; + data_len = btrfs_item_end_nr(l, start); + data_len = data_len - btrfs_item_offset_nr(l, end); + data_len += sizeof(struct btrfs_item) * nr; + WARN_ON(data_len < 0); + return data_len; +} + +/* + * 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 + */ +int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf) +{ + int nritems = btrfs_header_nritems(leaf); + int ret; + ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); + if (ret < 0) { + printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n", + ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root), + leaf_space_used(leaf, 0, nritems), nritems); } return ret; } @@ -998,11 +1535,10 @@ static int push_leaf_right(struct btrfs_ *root, struct btrfs_path *path, int data_size, int empty) { - 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; + struct extent_buffer *left = path->nodes[0]; + struct extent_buffer *right; + struct extent_buffer *upper; + struct btrfs_disk_key disk_key; int slot; u32 i; int free_space; @@ -1012,34 +1548,42 @@ static int push_leaf_right(struct btrfs_ u32 left_nritems; u32 nr; u32 right_nritems; + u32 data_end; + u32 this_item_size; + int ret; + slot = path->slots[1]; if (!path->nodes[1]) { return 1; } upper = path->nodes[1]; - if (slot >= btrfs_header_nritems(&upper->node.header) - 1) { + if (slot >= btrfs_header_nritems(upper) - 1) return 1; - } - right_buf = read_tree_block(root, - btrfs_node_blockptr(&upper->node, slot + 1), - root->leafsize); - right = &right_buf->leaf; + + right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1), + root->leafsize); free_space = btrfs_leaf_free_space(root, right); if (free_space < data_size + sizeof(struct btrfs_item)) { - btrfs_block_release(root, right_buf); + free_extent_buffer(right); return 1; } + /* cow and double check */ - btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf); - right = &right_buf->leaf; + ret = btrfs_cow_block(trans, root, right, upper, + slot + 1, &right); + if (ret) { + free_extent_buffer(right); + return 1; + } free_space = btrfs_leaf_free_space(root, right); if (free_space < data_size + sizeof(struct btrfs_item)) { - btrfs_block_release(root, right_buf); + free_extent_buffer(right); return 1; } - left_nritems = btrfs_header_nritems(&left->header); + + left_nritems = btrfs_header_nritems(left); if (left_nritems == 0) { - btrfs_block_release(root, right_buf); + free_extent_buffer(right); return 1; } @@ -1050,65 +1594,86 @@ static int push_leaf_right(struct btrfs_ i = left_nritems - 1; while (i >= nr) { - item = left->items + i; + item = btrfs_item_nr(left, i); + if (path->slots[0] == i) push_space += data_size + sizeof(*item); - if (btrfs_item_size(item) + sizeof(*item) + push_space > - free_space) + + this_item_size = btrfs_item_size(left, item); + if (this_item_size + sizeof(*item) + push_space > free_space) break; push_items++; - push_space += btrfs_item_size(item) + sizeof(*item); + push_space += this_item_size + sizeof(*item); if (i == 0) break; i--; } + if (push_items == 0) { - btrfs_block_release(root, right_buf); + free_extent_buffer(right); return 1; } - right_nritems = btrfs_header_nritems(&right->header); + + if (!empty && push_items == left_nritems) + WARN_ON(1); + /* push left to right */ - push_space = btrfs_item_end(left->items + left_nritems - push_items); + right_nritems = btrfs_header_nritems(right); + + push_space = btrfs_item_end_nr(left, left_nritems - push_items); push_space -= leaf_data_end(root, left); + /* make room in the right data area */ - 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)); + data_end = leaf_data_end(root, right); + memmove_extent_buffer(right, + btrfs_leaf_data(right) + data_end - push_space, + btrfs_leaf_data(right) + data_end, + BTRFS_LEAF_DATA_SIZE(root) - data_end); + /* copy from the left data area */ - memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space, - btrfs_leaf_data(left) + leaf_data_end(root, left), push_space); - memmove(right->items + push_items, right->items, - right_nritems * sizeof(struct btrfs_item)); + copy_extent_buffer(right, left, btrfs_leaf_data(right) + + BTRFS_LEAF_DATA_SIZE(root) - push_space, + btrfs_leaf_data(left) + leaf_data_end(root, left), + push_space); + + memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), + btrfs_item_nr_offset(0), + right_nritems * sizeof(struct btrfs_item)); + /* copy the items from left to right */ - memcpy(right->items, left->items + left_nritems - push_items, - push_items * sizeof(struct btrfs_item)); + copy_extent_buffer(right, left, btrfs_item_nr_offset(0), + btrfs_item_nr_offset(left_nritems - push_items), + push_items * sizeof(struct btrfs_item)); /* update the item pointers */ right_nritems += push_items; - btrfs_set_header_nritems(&right->header, right_nritems); + btrfs_set_header_nritems(right, right_nritems); push_space = BTRFS_LEAF_DATA_SIZE(root); for (i = 0; i < right_nritems; i++) { - btrfs_set_item_offset(right->items + i, push_space - - btrfs_item_size(right->items + i)); - push_space = btrfs_item_offset(right->items + i); - } + item = btrfs_item_nr(right, i); + push_space -= btrfs_item_size(right, item); + btrfs_set_item_offset(right, item, push_space); + } + left_nritems -= push_items; - btrfs_set_header_nritems(&left->header, left_nritems); - - BUG_ON(list_empty(&left_buf->dirty)); - BUG_ON(list_empty(&right_buf->dirty)); - memcpy(&upper->node.ptrs[slot + 1].key, - &right->items[0].key, sizeof(struct btrfs_disk_key)); - BUG_ON(list_empty(&upper->dirty)); + btrfs_set_header_nritems(left, left_nritems); + + if (left_nritems) + btrfs_mark_buffer_dirty(left); + btrfs_mark_buffer_dirty(right); + + btrfs_item_key(right, &disk_key, 0); + btrfs_set_node_key(upper, &disk_key, slot + 1); + btrfs_mark_buffer_dirty(upper); /* then fixup the leaf pointer in the path */ if (path->slots[0] >= left_nritems) { path->slots[0] -= left_nritems; - btrfs_block_release(root, path->nodes[0]); - path->nodes[0] = right_buf; + free_extent_buffer(path->nodes[0]); + path->nodes[0] = right; path->slots[1] += 1; } else { - btrfs_block_release(root, right_buf); + free_extent_buffer(right); } return 0; } @@ -1120,10 +1685,9 @@ static int push_leaf_left(struct btrfs_t *root, struct btrfs_path *path, int data_size, int empty) { - struct btrfs_buffer *right_buf = path->nodes[0]; - struct btrfs_leaf *right = &right_buf->leaf; - struct btrfs_buffer *t; - struct btrfs_leaf *left; + struct btrfs_disk_key disk_key; + struct extent_buffer *right = path->nodes[0]; + struct extent_buffer *left; int slot; int i; int free_space; @@ -1135,110 +1699,142 @@ static int push_leaf_left(struct btrfs_t u32 nr; int ret = 0; int wret; + u32 this_item_size; + u32 old_left_item_size; + slot = path->slots[1]; - if (slot == 0) { + if (slot == 0) return 1; - } - if (!path->nodes[1]) { + if (!path->nodes[1]) return 1; - } - right_nritems = btrfs_header_nritems(&right->header); + + right_nritems = btrfs_header_nritems(right); if (right_nritems == 0) { return 1; } - t = read_tree_block(root, - btrfs_node_blockptr(&path->nodes[1]->node, slot - 1), - root->leafsize); - left = &t->leaf; + left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1], + slot - 1), root->leafsize); free_space = btrfs_leaf_free_space(root, left); if (free_space < data_size + sizeof(struct btrfs_item)) { - btrfs_block_release(root, t); + free_extent_buffer(left); return 1; } /* cow and double check */ - btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t); - left = &t->leaf; + ret = btrfs_cow_block(trans, root, left, + path->nodes[1], slot - 1, &left); + if (ret) { + /* we hit -ENOSPC, but it isn't fatal here */ + free_extent_buffer(left); + return 1; + } + free_space = btrfs_leaf_free_space(root, left); if (free_space < data_size + sizeof(struct btrfs_item)) { - btrfs_block_release(root, t); + free_extent_buffer(left); return 1; } + if (empty) nr = right_nritems; else nr = right_nritems - 1; for (i = 0; i < nr; i++) { - item = right->items + i; + item = btrfs_item_nr(right, i); + if (path->slots[0] == i) push_space += data_size + sizeof(*item); - if (btrfs_item_size(item) + sizeof(*item) + push_space > - free_space) + + this_item_size = btrfs_item_size(right, item); + if (this_item_size + sizeof(*item) + push_space > free_space) break; + push_items++; - push_space += btrfs_item_size(item) + sizeof(*item); - } + push_space += this_item_size + sizeof(*item); + } + if (push_items == 0) { - btrfs_block_release(root, t); + free_extent_buffer(left); return 1; } + if (!empty && push_items == btrfs_header_nritems(right)) + WARN_ON(1); + /* push data from right to left */ - memcpy(left->items + btrfs_header_nritems(&left->header), - right->items, push_items * sizeof(struct btrfs_item)); + copy_extent_buffer(left, right, + btrfs_item_nr_offset(btrfs_header_nritems(left)), + btrfs_item_nr_offset(0), + push_items * sizeof(struct btrfs_item)); + push_space = BTRFS_LEAF_DATA_SIZE(root) - - btrfs_item_offset(right->items + push_items -1); - memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space, - btrfs_leaf_data(right) + - btrfs_item_offset(right->items + push_items - 1), - push_space); - old_left_nritems = btrfs_header_nritems(&left->header); + btrfs_item_offset_nr(right, push_items -1); + + copy_extent_buffer(left, right, btrfs_leaf_data(left) + + leaf_data_end(root, left) - push_space, + btrfs_leaf_data(right) + + btrfs_item_offset_nr(right, push_items - 1), + push_space); + old_left_nritems = btrfs_header_nritems(left); BUG_ON(old_left_nritems < 0); + old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { - u32 ioff = btrfs_item_offset(left->items + i); - btrfs_set_item_offset(left->items + i, ioff - - (BTRFS_LEAF_DATA_SIZE(root) - - btrfs_item_offset(left->items + - old_left_nritems - 1))); - } - btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); + u32 ioff; + + item = btrfs_item_nr(left, i); + ioff = btrfs_item_offset(left, item); + btrfs_set_item_offset(left, item, + ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size)); + } + btrfs_set_header_nritems(left, old_left_nritems + push_items); + /* fixup right node */ + if (push_items > right_nritems) { + printk("push items %d nr %u\n", push_items, right_nritems); + WARN_ON(1); + } + if (push_items < right_nritems) { - push_space = btrfs_item_offset(right->items + push_items - 1) - - 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); - memmove(right->items, right->items + push_items, - (right_nritems - push_items) * - sizeof(struct btrfs_item)); + push_space = btrfs_item_offset_nr(right, push_items - 1) - + leaf_data_end(root, right); + memmove_extent_buffer(right, btrfs_leaf_data(right) + + BTRFS_LEAF_DATA_SIZE(root) - push_space, + btrfs_leaf_data(right) + + leaf_data_end(root, right), push_space); + + memmove_extent_buffer(right, btrfs_item_nr_offset(0), + btrfs_item_nr_offset(push_items), + (btrfs_header_nritems(right) - push_items) * + sizeof(struct btrfs_item)); } right_nritems -= push_items; - btrfs_set_header_nritems(&right->header, right_nritems); + btrfs_set_header_nritems(right, right_nritems); push_space = BTRFS_LEAF_DATA_SIZE(root); for (i = 0; i < right_nritems; i++) { - btrfs_set_item_offset(right->items + i, push_space - - btrfs_item_size(right->items + i)); - push_space = btrfs_item_offset(right->items + i); - } - - BUG_ON(list_empty(&t->dirty)); - BUG_ON(list_empty(&right_buf->dirty)); - - wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1); + item = btrfs_item_nr(right, i); + push_space = push_space - btrfs_item_size(right, item); + btrfs_set_item_offset(right, item, push_space); + } + + btrfs_mark_buffer_dirty(left); + if (right_nritems) + btrfs_mark_buffer_dirty(right); + + btrfs_item_key(right, &disk_key, 0); + wret = fixup_low_keys(trans, root, path, &disk_key, 1); if (wret) ret = wret; /* then fixup the leaf pointer in the path */ if (path->slots[0] < push_items) { path->slots[0] += old_left_nritems; - btrfs_block_release(root, path->nodes[0]); - path->nodes[0] = t; + free_extent_buffer(path->nodes[0]); + path->nodes[0] = left; path->slots[1] -= 1; } else { - btrfs_block_release(root, t); + free_extent_buffer(left); path->slots[0] -= push_items; } BUG_ON(path->slots[0] < 0); @@ -1255,13 +1851,12 @@ static int split_leaf(struct btrfs_trans *root, struct btrfs_key *ins_key, struct btrfs_path *path, int data_size, int extend) { - struct btrfs_buffer *l_buf; - struct btrfs_leaf *l; + u64 root_gen; + struct extent_buffer *l; u32 nritems; int mid; int slot; - struct btrfs_leaf *right; - struct btrfs_buffer *right_buffer; + struct extent_buffer *right; int space_needed = data_size + sizeof(struct btrfs_item); int data_copy_size; int rt_data_off; @@ -1274,6 +1869,12 @@ static int split_leaf(struct btrfs_trans if (extend) space_needed = data_size; + + if (root->ref_cows) + root_gen = trans->transid; + else + root_gen = 0; + /* first try to make some room by pushing left and right */ if (ins_key->type != BTRFS_DIR_ITEM_KEY) { wret = push_leaf_right(trans, root, path, data_size, 0); @@ -1285,13 +1886,13 @@ static int split_leaf(struct btrfs_trans if (wret < 0) return wret; } - l_buf = path->nodes[0]; - l = &l_buf->leaf; + l = path->nodes[0]; /* did the pushes work? */ if (btrfs_leaf_free_space(root, l) >= space_needed) return 0; } + if (!path->nodes[1]) { ret = insert_new_root(trans, root, path, 1); if (ret) @@ -1299,35 +1900,42 @@ static int split_leaf(struct btrfs_trans } again: double_split = 0; - l_buf = path->nodes[0]; - l = &l_buf->leaf; + l = path->nodes[0]; slot = path->slots[0]; - nritems = btrfs_header_nritems(&l->header); + nritems = btrfs_header_nritems(l); mid = (nritems + 1)/ 2; - right_buffer = btrfs_alloc_free_block(trans, root, root->leafsize); - right = &right_buffer->leaf; - memset(&right->header, 0, sizeof(right->header)); - btrfs_set_header_bytenr(&right->header, right_buffer->bytenr); - btrfs_set_header_level(&right->header, 0); - btrfs_set_header_owner(&right->header, root->root_key.objectid); - btrfs_set_header_generation(&right->header, trans->transid); - memcpy(right->header.fsid, root->fs_info->disk_super->fsid, - sizeof(right->header.fsid)); + btrfs_item_key(l, &disk_key, 0); + + right = __btrfs_alloc_free_block(trans, root, root->leafsize, + root->root_key.objectid, + root_gen, disk_key.objectid, 0, + l->start, 0); + if (IS_ERR(right)) + return PTR_ERR(right); + + memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header)); + btrfs_set_header_bytenr(right, right->start); + btrfs_set_header_generation(right, trans->transid); + btrfs_set_header_owner(right, root->root_key.objectid); + btrfs_set_header_level(right, 0); + write_extent_buffer(right, root->fs_info->fsid, + (unsigned long)btrfs_header_fsid(right), + BTRFS_FSID_SIZE); if (mid <= slot) { if (nritems == 1 || leaf_space_used(l, mid, nritems - mid) + space_needed > BTRFS_LEAF_DATA_SIZE(root)) { if (slot >= nritems) { btrfs_cpu_key_to_disk(&disk_key, ins_key); - btrfs_set_header_nritems(&right->header, 0); + btrfs_set_header_nritems(right, 0); wret = insert_ptr(trans, root, path, - &disk_key, right_buffer->bytenr, + &disk_key, right->start, path->slots[1] + 1, 1); if (wret) ret = wret; - btrfs_block_release(root, path->nodes[0]); - path->nodes[0] = right_buffer; + free_extent_buffer(path->nodes[0]); + path->nodes[0] = right; path->slots[0] = 0; path->slots[1] += 1; return ret; @@ -1340,19 +1948,19 @@ again: } } } else { - if (leaf_space_used(l, 0, mid) + space_needed > + if (leaf_space_used(l, 0, mid + 1) + space_needed > BTRFS_LEAF_DATA_SIZE(root)) { if (!extend && slot == 0) { btrfs_cpu_key_to_disk(&disk_key, ins_key); - btrfs_set_header_nritems(&right->header, 0); + btrfs_set_header_nritems(right, 0); wret = insert_ptr(trans, root, path, &disk_key, - right_buffer->bytenr, + right->start, path->slots[1], 1); if (wret) ret = wret; - btrfs_block_release(root, path->nodes[0]); - path->nodes[0] = right_buffer; + free_extent_buffer(path->nodes[0]); + path->nodes[0] = right; path->slots[0] = 0; if (path->slots[1] == 0) { wret = fixup_low_keys(trans, root, @@ -1374,40 +1982,49 @@ again: } } nritems = nritems - mid; - btrfs_set_header_nritems(&right->header, nritems); - data_copy_size = btrfs_item_end(l->items + mid) - - leaf_data_end(root, l); - memcpy(right->items, l->items + mid, - nritems * sizeof(struct btrfs_item)); - 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); + btrfs_set_header_nritems(right, nritems); + data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); + + copy_extent_buffer(right, l, btrfs_item_nr_offset(0), + btrfs_item_nr_offset(mid), + nritems * sizeof(struct btrfs_item)); + + copy_extent_buffer(right, l, + 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); + btrfs_item_end_nr(l, mid); + for (i = 0; i < nritems; i++) { - u32 ioff = btrfs_item_offset(right->items + i); - btrfs_set_item_offset(right->items + i, ioff + rt_data_off); - } - - btrfs_set_header_nritems(&l->header, mid); + struct btrfs_item *item = btrfs_item_nr(right, i); + u32 ioff = btrfs_item_offset(right, item); + btrfs_set_item_offset(right, item, ioff + rt_data_off); + } + + btrfs_set_header_nritems(l, mid); ret = 0; - wret = insert_ptr(trans, root, path, &right->items[0].key, - right_buffer->bytenr, path->slots[1] + 1, 1); + btrfs_item_key(right, &disk_key, 0); + wret = insert_ptr(trans, root, path, &disk_key, right->start, + path->slots[1] + 1, 1); if (wret) ret = wret; - BUG_ON(list_empty(&right_buffer->dirty)); - BUG_ON(list_empty(&l_buf->dirty)); + btrfs_mark_buffer_dirty(right); + btrfs_mark_buffer_dirty(l); BUG_ON(path->slots[0] != slot); + if (mid <= slot) { - btrfs_block_release(root, path->nodes[0]); - path->nodes[0] = right_buffer; + free_extent_buffer(path->nodes[0]); + path->nodes[0] = right; path->slots[0] -= mid; path->slots[1] += 1; } else - btrfs_block_release(root, right_buffer); + free_extent_buffer(right); BUG_ON(path->slots[0] < 0); + if (double_split) { BUG_ON(num_doubles != 0); num_doubles++; @@ -1415,19 +2032,185 @@ again: } return ret; } + +int btrfs_truncate_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, + u32 new_size, int from_end) +{ + int ret = 0; + int slot; + int slot_orig; + struct extent_buffer *leaf; + struct btrfs_item *item; + u32 nritems; + unsigned int data_end; + unsigned int old_data_start; + unsigned int old_size; + unsigned int size_diff; + int i; + + slot_orig = path->slots[0]; + leaf = path->nodes[0]; + slot = path->slots[0]; + + old_size = btrfs_item_size_nr(leaf, slot); + if (old_size == new_size) + return 0; + + nritems = btrfs_header_nritems(leaf); + data_end = leaf_data_end(root, leaf); + + old_data_start = btrfs_item_offset_nr(leaf, slot); + + size_diff = old_size - new_size; + + BUG_ON(slot < 0); + BUG_ON(slot >= nritems); + + /* + * item0..itemN ... dataN.offset..dataN.size .. data0.size + */ + /* first correct the data pointers */ + for (i = slot; i < nritems; i++) { + u32 ioff; + item = btrfs_item_nr(leaf, i); + ioff = btrfs_item_offset(leaf, item); + btrfs_set_item_offset(leaf, item, ioff + size_diff); + } + + /* shift the data */ + if (from_end) { + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + + data_end + size_diff, btrfs_leaf_data(leaf) + + data_end, old_data_start + new_size - data_end); + } else { + struct btrfs_disk_key disk_key; + u64 offset; + + btrfs_item_key(leaf, &disk_key, slot); + + if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { + unsigned long ptr; + struct btrfs_file_extent_item *fi; + + fi = btrfs_item_ptr(leaf, slot, + struct btrfs_file_extent_item); + fi = (struct btrfs_file_extent_item *)( + (unsigned long)fi - size_diff); + + if (btrfs_file_extent_type(leaf, fi) == + BTRFS_FILE_EXTENT_INLINE) { + ptr = btrfs_item_ptr_offset(leaf, slot); + memmove_extent_buffer(leaf, ptr, + (unsigned long)fi, + offsetof(struct btrfs_file_extent_item, + disk_bytenr)); + } + } + + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + + data_end + size_diff, btrfs_leaf_data(leaf) + + data_end, old_data_start - data_end); + + offset = btrfs_disk_key_offset(&disk_key); + btrfs_set_disk_key_offset(&disk_key, offset + size_diff); + btrfs_set_item_key(leaf, &disk_key, slot); + if (slot == 0) + fixup_low_keys(trans, root, path, &disk_key, 1); + } + + item = btrfs_item_nr(leaf, slot); + btrfs_set_item_size(leaf, item, new_size); + btrfs_mark_buffer_dirty(leaf); + + ret = 0; + if (btrfs_leaf_free_space(root, leaf) < 0) { + btrfs_print_leaf(root, leaf); + BUG(); + } + return ret; +} + +int btrfs_extend_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct btrfs_path *path, + u32 data_size) +{ + int ret = 0; + int slot; + int slot_orig; + struct extent_buffer *leaf; + struct btrfs_item *item; + u32 nritems; + unsigned int data_end; + unsigned int old_data; + unsigned int old_size; + int i; + + slot_orig = path->slots[0]; + leaf = path->nodes[0]; + + nritems = btrfs_header_nritems(leaf); + data_end = leaf_data_end(root, leaf); + + if (btrfs_leaf_free_space(root, leaf) < data_size) { + btrfs_print_leaf(root, leaf); + BUG(); + } + slot = path->slots[0]; + old_data = btrfs_item_end_nr(leaf, slot); + + BUG_ON(slot < 0); + if (slot >= nritems) { + btrfs_print_leaf(root, leaf); + printk("slot %d too large, nritems %d\n", slot, nritems); + BUG_ON(1); + } + + /* + * item0..itemN ... dataN.offset..dataN.size .. data0.size + */ + /* first correct the data pointers */ + for (i = slot; i < nritems; i++) { + u32 ioff; + item = btrfs_item_nr(leaf, i); + ioff = btrfs_item_offset(leaf, item); + btrfs_set_item_offset(leaf, item, ioff - data_size); + } + + /* shift the data */ + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + + data_end - data_size, btrfs_leaf_data(leaf) + + data_end, old_data - data_end); + + data_end = old_data; + old_size = btrfs_item_size_nr(leaf, slot); + item = btrfs_item_nr(leaf, slot); + btrfs_set_item_size(leaf, item, old_size + data_size); + btrfs_mark_buffer_dirty(leaf); + + ret = 0; + if (btrfs_leaf_free_space(root, leaf) < 0) { + btrfs_print_leaf(root, leaf); + BUG(); + } + 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_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, struct btrfs_key - *cpu_key, u32 data_size) -{ +int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, + struct btrfs_key *cpu_key, u32 data_size) +{ + struct extent_buffer *leaf; + struct btrfs_item *item; int ret = 0; int slot; int slot_orig; - struct btrfs_leaf *leaf; - struct btrfs_buffer *leaf_buf; u32 nritems; unsigned int data_end; struct btrfs_disk_key disk_key; @@ -1437,6 +2220,7 @@ int btrfs_insert_empty_item(struct btrfs /* create a root if there isn't one */ if (!root->node) BUG(); + ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1); if (ret == 0) { return -EEXIST; @@ -1445,57 +2229,72 @@ int btrfs_insert_empty_item(struct btrfs goto out; slot_orig = path->slots[0]; - leaf_buf = path->nodes[0]; - leaf = &leaf_buf->leaf; - - nritems = btrfs_header_nritems(&leaf->header); + leaf = path->nodes[0]; + + nritems = btrfs_header_nritems(leaf); data_end = leaf_data_end(root, leaf); if (btrfs_leaf_free_space(root, leaf) < - sizeof(struct btrfs_item) + data_size) + sizeof(struct btrfs_item) + data_size) { + btrfs_print_leaf(root, leaf); + printk("not enough freespace need %u have %d\n", + data_size, btrfs_leaf_free_space(root, leaf)); BUG(); + } slot = path->slots[0]; BUG_ON(slot < 0); + if (slot != nritems) { int i; - unsigned int old_data = btrfs_item_end(leaf->items + slot); - + unsigned int old_data = btrfs_item_end_nr(leaf, slot); + + if (old_data < data_end) { + btrfs_print_leaf(root, leaf); + printk("slot %d old_data %d data_end %d\n", + slot, old_data, data_end); + BUG_ON(1); + } /* * item0..itemN ... dataN.offset..dataN.size .. data0.size */ /* first correct the data pointers */ for (i = slot; i < nritems; i++) { - u32 ioff = btrfs_item_offset(leaf->items + i); - btrfs_set_item_offset(leaf->items + i, - ioff - data_size); + u32 ioff; + + item = btrfs_item_nr(leaf, i); + ioff = btrfs_item_offset(leaf, item); + btrfs_set_item_offset(leaf, item, ioff - data_size); } /* shift the items */ - memmove(leaf->items + slot + 1, leaf->items + slot, - (nritems - slot) * sizeof(struct btrfs_item)); + memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), + btrfs_item_nr_offset(slot), + (nritems - slot) * sizeof(struct btrfs_item)); /* shift the data */ - memmove(btrfs_leaf_data(leaf) + data_end - data_size, - btrfs_leaf_data(leaf) + - data_end, old_data - data_end); + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + + data_end - data_size, btrfs_leaf_data(leaf) + + data_end, old_data - data_end); data_end = old_data; } + /* setup the item for the new data */ - memcpy(&leaf->items[slot].key, &disk_key, - sizeof(struct btrfs_disk_key)); - btrfs_set_item_offset(leaf->items + slot, data_end - data_size); - btrfs_set_item_size(leaf->items + slot, data_size); - btrfs_set_header_nritems(&leaf->header, nritems + 1); + btrfs_set_item_key(leaf, &disk_key, slot); + item = btrfs_item_nr(leaf, slot); + btrfs_set_item_offset(leaf, item, data_end - data_size); + btrfs_set_item_size(leaf, item, data_size); + btrfs_set_header_nritems(leaf, nritems + 1); + btrfs_mark_buffer_dirty(leaf); ret = 0; if (slot == 0) ret = fixup_low_keys(trans, root, path, &disk_key, 1); - BUG_ON(list_empty(&leaf_buf->dirty)); - if (btrfs_leaf_free_space(root, leaf) < 0) + if (btrfs_leaf_free_space(root, leaf) < 0) { + btrfs_print_leaf(root, leaf); BUG(); - check_leaf(root, path, 0); + } out: return ret; } @@ -1509,16 +2308,20 @@ int btrfs_insert_item(struct btrfs_trans data_size) { int ret = 0; - struct btrfs_path path; - u8 *ptr; - - btrfs_init_path(&path); - ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size); + struct btrfs_path *path; + struct extent_buffer *leaf; + unsigned long ptr; + + path = btrfs_alloc_path(); + BUG_ON(!path); + ret = btrfs_insert_empty_item(trans, 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); - } - btrfs_release_path(root, &path); + leaf = path->nodes[0]; + ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); + write_extent_buffer(leaf, data, ptr, data_size); + btrfs_mark_buffer_dirty(leaf); + } + btrfs_free_path(path); return ret; } @@ -1532,31 +2335,34 @@ static int del_ptr(struct btrfs_trans_ha static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, int level, int slot) { - struct btrfs_node *node; - struct btrfs_buffer *parent = path->nodes[level]; + struct extent_buffer *parent = path->nodes[level]; u32 nritems; int ret = 0; int wret; - node = &parent->node; - nritems = btrfs_header_nritems(&node->header); + nritems = btrfs_header_nritems(parent); if (slot != nritems -1) { - memmove(node->ptrs + slot, node->ptrs + slot + 1, - sizeof(struct btrfs_key_ptr) * (nritems - slot - 1)); + memmove_extent_buffer(parent, + btrfs_node_key_ptr_offset(slot), + btrfs_node_key_ptr_offset(slot + 1), + sizeof(struct btrfs_key_ptr) * + (nritems - slot - 1)); } nritems--; - btrfs_set_header_nritems(&node->header, nritems); + btrfs_set_header_nritems(parent, nritems); if (nritems == 0 && parent == root->node) { - BUG_ON(btrfs_header_level(&root->node->node.header) != 1); + BUG_ON(btrfs_header_level(root->node) != 1); /* just turn the root into a leaf and break */ - btrfs_set_header_level(&root->node->node.header, 0); + btrfs_set_header_level(root->node, 0); } else if (slot == 0) { - wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key, - level + 1); + struct btrfs_disk_key disk_key; + + btrfs_node_key(parent, &disk_key, 0); + wret = fixup_low_keys(trans, root, path, &disk_key, level + 1); if (wret) ret = wret; } - BUG_ON(list_empty(&parent->dirty)); + btrfs_mark_buffer_dirty(parent); return ret; } @@ -1568,66 +2374,74 @@ int btrfs_del_item(struct btrfs_trans_ha struct btrfs_path *path) { int slot; - struct btrfs_leaf *leaf; - struct btrfs_buffer *leaf_buf; + struct extent_buffer *leaf; + struct btrfs_item *item; int doff; int dsize; int ret = 0; int wret; u32 nritems; - leaf_buf = path->nodes[0]; - leaf = &leaf_buf->leaf; + leaf = path->nodes[0]; slot = path->slots[0]; - doff = btrfs_item_offset(leaf->items + slot); - dsize = btrfs_item_size(leaf->items + slot); - nritems = btrfs_header_nritems(&leaf->header); + doff = btrfs_item_offset_nr(leaf, slot); + dsize = btrfs_item_size_nr(leaf, slot); + nritems = btrfs_header_nritems(leaf); if (slot != nritems - 1) { int i; int data_end = leaf_data_end(root, leaf); - memmove(btrfs_leaf_data(leaf) + data_end + dsize, - btrfs_leaf_data(leaf) + data_end, - doff - data_end); + + memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + + data_end + dsize, + btrfs_leaf_data(leaf) + data_end, + doff - data_end); + for (i = slot + 1; i < nritems; i++) { - u32 ioff = btrfs_item_offset(leaf->items + i); - btrfs_set_item_offset(leaf->items + i, ioff + dsize); - } - memmove(leaf->items + slot, leaf->items + slot + 1, - sizeof(struct btrfs_item) * - (nritems - slot - 1)); - } - btrfs_set_header_nritems(&leaf->header, nritems - 1); + u32 ioff; + + item = btrfs_item_nr(leaf, i); + ioff = btrfs_item_offset(leaf, item); + btrfs_set_item_offset(leaf, item, ioff + dsize); + } + + memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), + btrfs_item_nr_offset(slot + 1), + sizeof(struct btrfs_item) * + (nritems - slot - 1)); + } + btrfs_set_header_nritems(leaf, nritems - 1); nritems--; + /* delete the leaf if we've emptied it */ if (nritems == 0) { - if (leaf_buf == root->node) { - btrfs_set_header_level(&leaf->header, 0); - BUG_ON(list_empty(&leaf_buf->dirty)); + if (leaf == root->node) { + btrfs_set_header_level(leaf, 0); } else { - u64 generation = - btrfs_header_generation(&path->nodes[1]->node.header); - - clean_tree_block(trans, root, leaf_buf); + u64 root_gen = btrfs_header_generation(path->nodes[1]); + clean_tree_block(trans, root, leaf); + wait_on_tree_block_writeback(root, leaf); wret = del_ptr(trans, root, path, 1, path->slots[1]); if (wret) ret = wret; - wret = btrfs_free_extent(trans, root, leaf_buf->bytenr, - leaf_buf->size, - root->root_key.objectid, - generation, 0, 0, 1); + wret = btrfs_free_extent(trans, root, + leaf->start, leaf->len, + btrfs_header_owner(path->nodes[1]), + root_gen, 0, 0, 1); if (wret) ret = wret; } } else { int used = leaf_space_used(leaf, 0, nritems); if (slot == 0) { + struct btrfs_disk_key disk_key; + + btrfs_item_key(leaf, &disk_key, 0); wret = fixup_low_keys(trans, root, path, - &leaf->items[0].key, 1); + &disk_key, 1); if (wret) ret = wret; } - BUG_ON(list_empty(&leaf_buf->dirty)); /* delete the leaf if it is mostly empty */ if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { @@ -1636,186 +2450,103 @@ int btrfs_del_item(struct btrfs_trans_ha * for possible call to del_ptr below */ slot = path->slots[1]; - leaf_buf->count++; + extent_buffer_get(leaf); + wret = push_leaf_right(trans, root, path, 1, 1); - if (wret < 0) + if (wret < 0 && wret != -ENOSPC) ret = wret; - if (path->nodes[0] == leaf_buf && - btrfs_header_nritems(&leaf->header)) { + + if (path->nodes[0] == leaf && + btrfs_header_nritems(leaf)) { wret = push_leaf_left(trans, root, path, 1, 1); - if (wret < 0) + if (wret < 0 && wret != -ENOSPC) ret = wret; } - if (btrfs_header_nritems(&leaf->header) == 0) { - u64 bytenr = leaf_buf->bytenr; - struct btrfs_buffer *parent = path->nodes[1]; - u64 generation = - btrfs_header_generation(&parent->node.header); - - clean_tree_block(trans, root, leaf_buf); + + if (btrfs_header_nritems(leaf) == 0) { + u64 root_gen; + u64 bytenr = leaf->start; + u32 blocksize = leaf->len; + + root_gen = btrfs_header_generation( + path->nodes[1]); + + clean_tree_block(trans, root, leaf); + wait_on_tree_block_writeback(root, leaf); + wret = del_ptr(trans, root, path, 1, slot); if (wret) ret = wret; + + free_extent_buffer(leaf); wret = btrfs_free_extent(trans, root, bytenr, - leaf_buf->size, - root->root_key.objectid, - generation, 0, 0, 1); - btrfs_block_release(root, leaf_buf); + blocksize, + btrfs_header_owner(path->nodes[1]), + root_gen, 0, 0, 1); if (wret) ret = wret; } else { - btrfs_block_release(root, leaf_buf); + btrfs_mark_buffer_dirty(leaf); + free_extent_buffer(leaf); } + } else { + btrfs_mark_buffer_dirty(leaf); } } return ret; } -int btrfs_truncate_item(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_path *path, - u32 new_size, int from_end) -{ - int ret = 0; + +/* + * walk up the tree as far as required to find the previous leaf. + * returns 0 if it found something or 1 if there are no lesser leaves. + * returns < 0 on io errors. + */ +int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) +{ + u64 bytenr; int slot; - int slot_orig; - struct btrfs_leaf *leaf; - struct btrfs_item *item; - u32 nritems; - unsigned int data_end; - unsigned int old_data_start; - unsigned int old_size; - unsigned int size_diff; - int i; - - slot_orig = path->slots[0]; - leaf = &path->nodes[0]->leaf; - slot = path->slots[0]; - - old_size = btrfs_item_size(leaf->items + slot); - if (old_size == new_size) - return 0; - - nritems = btrfs_header_nritems(&leaf->header); - data_end = leaf_data_end(root, leaf); - - old_data_start = btrfs_item_offset(leaf->items + slot); - - size_diff = old_size - new_size; - - BUG_ON(slot < 0); - BUG_ON(slot >= nritems); - - /* - * item0..itemN ... dataN.offset..dataN.size .. data0.size - */ - /* first correct the data pointers */ - for (i = slot; i < nritems; i++) { - u32 ioff; - item = leaf->items + i; - ioff = btrfs_item_offset(item); - btrfs_set_item_offset(item, ioff + size_diff); - } - - /* shift the data */ - if (from_end) { - memmove(btrfs_leaf_data(leaf) + data_end + size_diff, - btrfs_leaf_data(leaf) + data_end, - old_data_start + new_size - data_end); - } else { - struct btrfs_disk_key *disk_key; - u64 offset; - - disk_key = &leaf->items[slot].key; - if (btrfs_disk_key_type(disk_key) == BTRFS_EXTENT_DATA_KEY) { - char *ptr; - struct btrfs_file_extent_item *fi; - - fi = btrfs_item_ptr(leaf, slot, - struct btrfs_file_extent_item); - fi = (struct btrfs_file_extent_item *)( - (unsigned long)fi - size_diff); - - if (btrfs_file_extent_type(fi) == - BTRFS_FILE_EXTENT_INLINE) { - ptr = btrfs_item_ptr(leaf, slot, char); - memmove(ptr, (char *)fi, - offsetof(struct btrfs_file_extent_item, - disk_bytenr)); - } - } - - memmove(btrfs_leaf_data(leaf) + data_end + size_diff, - btrfs_leaf_data(leaf) + data_end, - old_data_start - data_end); - - offset = btrfs_disk_key_offset(disk_key); - btrfs_set_disk_key_offset(disk_key, offset + size_diff); - if (slot == 0) - fixup_low_keys(trans, root, path, disk_key, 1); - } - - item = leaf->items + slot; - btrfs_set_item_size(item, new_size); - BUG_ON(list_empty(&path->nodes[0]->dirty)); - - ret = 0; - if (btrfs_leaf_free_space(root, leaf) < 0) { - btrfs_print_leaf(root, leaf); - BUG(); - } - return ret; -} - -int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, u32 data_size) -{ - int ret = 0; - int slot; - int slot_orig; - struct btrfs_leaf *leaf; - struct btrfs_buffer *leaf_buf; - u32 nritems; - unsigned int data_end; - unsigned int old_data; - unsigned int old_size; - int i; - - slot_orig = path->slots[0]; - leaf_buf = path->nodes[0]; - leaf = &leaf_buf->leaf; - - nritems = btrfs_header_nritems(&leaf->header); - data_end = leaf_data_end(root, leaf); - - if (btrfs_leaf_free_space(root, leaf) < data_size) - BUG(); - slot = path->slots[0]; - old_data = btrfs_item_end(leaf->items + slot); - - BUG_ON(slot < 0); - BUG_ON(slot >= nritems); - - /* - * item0..itemN ... dataN.offset..dataN.size .. data0.size - */ - /* first correct the data pointers */ - for (i = slot; i < nritems; i++) { - u32 ioff = btrfs_item_offset(leaf->items + i); - btrfs_set_item_offset(leaf->items + i, - ioff - data_size); - } - /* shift the data */ - memmove(btrfs_leaf_data(leaf) + data_end - data_size, - btrfs_leaf_data(leaf) + data_end, old_data - data_end); - data_end = old_data; - old_size = btrfs_item_size(leaf->items + slot); - btrfs_set_item_size(leaf->items + slot, old_size + data_size); - - ret = 0; - if (btrfs_leaf_free_space(root, leaf) < 0) - BUG(); - check_leaf(root, path, 0); - return ret; + int level = 1; + struct extent_buffer *c; + struct extent_buffer *next = NULL; + + while(level < BTRFS_MAX_LEVEL) { + if (!path->nodes[level]) + return 1; + + slot = path->slots[level]; + c = path->nodes[level]; + if (slot == 0) { + level++; + if (level == BTRFS_MAX_LEVEL) + return 1; + continue; + } + slot--; + + bytenr = btrfs_node_blockptr(c, slot); + if (next) + free_extent_buffer(next); + + next = read_tree_block(root, bytenr, + btrfs_level_size(root, level - 1)); + break; + } + path->slots[level] = slot; + while(1) { + level--; + c = path->nodes[level]; + free_extent_buffer(c); + slot = btrfs_header_nritems(next); + if (slot != 0) + slot--; + path->nodes[level] = next; + path->slots[level] = slot; + if (!level) + break; + next = read_tree_block(root, btrfs_node_blockptr(next, slot), + btrfs_level_size(root, level - 1)); + } + return 0; } /* @@ -1828,38 +2559,46 @@ int btrfs_next_leaf(struct btrfs_root *r int slot; int level = 1; u64 bytenr; - struct btrfs_buffer *c; - struct btrfs_buffer *next = NULL; + struct extent_buffer *c; + struct extent_buffer *next = NULL; while(level < BTRFS_MAX_LEVEL) { if (!path->nodes[level]) return 1; + slot = path->slots[level] + 1; c = path->nodes[level]; - if (slot >= btrfs_header_nritems(&c->node.header)) { + if (slot >= btrfs_header_nritems(c)) { level++; + if (level == BTRFS_MAX_LEVEL) + return 1; continue; } - bytenr = btrfs_node_blockptr(&c->node, slot); + + bytenr = btrfs_node_blockptr(c, slot); if (next) - btrfs_block_release(root, next); + free_extent_buffer(next); + + if (path->reada) + reada_for_search(root, path, level, slot, 0); + next = read_tree_block(root, bytenr, - btrfs_level_size(root, level - 1)); + btrfs_level_size(root, level -1)); break; } path->slots[level] = slot; while(1) { level--; c = path->nodes[level]; - btrfs_block_release(root, c); + free_extent_buffer(c); path->nodes[level] = next; path->slots[level] = 0; if (!level) break; - next = read_tree_block(root, - btrfs_node_blockptr(&next->node, 0), + if (path->reada) + reada_for_search(root, path, level, 0, 0); + next = read_tree_block(root, btrfs_node_blockptr(next, 0), btrfs_level_size(root, level - 1)); } - check_leaf(root, path, 0); return 0; } diff -r 59fc7ac63e46 ctree.h --- a/ctree.h Fri Dec 21 16:25:35 2007 -0500 +++ b/ctree.h Fri Jan 04 19:19:50 2008 +0800 @@ -23,10 +23,13 @@ #include "kerncompat.h" #include "radix-tree.h" #include "extent-cache.h" +#include "extent_map.h" + struct btrfs_root; struct btrfs_trans_handle; #define BTRFS_MAGIC "_B2RfS_M" +#define BTRFS_MAX_LEVEL 8 #define BTRFS_ROOT_TREE_OBJECTID 1ULL #define BTRFS_EXTENT_TREE_OBJECTID 2ULL #define BTRFS_FS_TREE_OBJECTID 3ULL @@ -43,6 +46,7 @@ struct btrfs_trans_handle; #define BTRFS_CSUM_SIZE 32 /* four bytes for CRC32 */ #define BTRFS_CRC32_SIZE 4 +#define BTRFS_EMPTY_DIR_SIZE 0 #define BTRFS_FT_UNKNOWN 0 #define BTRFS_FT_REG_FILE 1 @@ -81,12 +85,13 @@ struct btrfs_key { u64 offset; } __attribute__ ((__packed__)); +#define BTRFS_FSID_SIZE 16 /* * every tree block (leaf or node) starts with this header. */ struct btrfs_header { u8 csum[BTRFS_CSUM_SIZE]; - u8 fsid[16]; /* FS specific uuid */ + u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */ __le64 bytenr; /* which block this node is supposed to live in */ __le64 generation; __le64 owner; @@ -95,16 +100,14 @@ struct btrfs_header { u8 level; } __attribute__ ((__packed__)); -#define BTRFS_MAX_LEVEL 8 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \ sizeof(struct btrfs_header)) / \ - sizeof(struct btrfs_key_ptr)) + sizeof(struct btrfs_key_ptr)) #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header)) #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize)) #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \ sizeof(struct btrfs_item) - \ sizeof(struct btrfs_file_extent_item)) -struct btrfs_buffer; /* * the super block basically lists the main trees of the FS * it currently lacks any block count etc etc @@ -112,7 +115,7 @@ struct btrfs_super_block { struct btrfs_super_block { u8 csum[BTRFS_CSUM_SIZE]; /* the first 3 fields must match struct btrfs_header */ - u8 fsid[16]; /* FS specific uuid */ + u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */ __le64 bytenr; /* this block number */ __le64 magic; __le64 generation; @@ -173,8 +176,10 @@ struct btrfs_node { * used while walking the tree. */ struct btrfs_path { - struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL]; + struct extent_buffer *nodes[BTRFS_MAX_LEVEL]; int slots[BTRFS_MAX_LEVEL]; + int reada; + int lowest_level; }; /* @@ -222,11 +227,6 @@ struct btrfs_inode_item { struct btrfs_inode_timespec ctime; struct btrfs_inode_timespec mtime; struct btrfs_inode_timespec otime; -} __attribute__ ((__packed__)); - -/* inline data is just a blob of bytes */ -struct btrfs_inline_data_item { - u8 data; } __attribute__ ((__packed__)); struct btrfs_dir_item { @@ -276,15 +276,15 @@ struct btrfs_file_extent_item { } __attribute__ ((__packed__)); struct btrfs_csum_item { - u8 csum[BTRFS_CSUM_SIZE]; + u8 csum; } __attribute__ ((__packed__)); /* tag for the radix tree of block groups in ram */ -#define BTRFS_BLOCK_GROUP_DIRTY 0 #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024) - #define BTRFS_BLOCK_GROUP_DATA 1 +#define BTRFS_BLOCK_GROUP_MIXED 2 + struct btrfs_block_group_item { __le64 used; u8 flags; @@ -294,28 +294,34 @@ struct btrfs_block_group_cache { struct cache_extent cache; struct btrfs_key key; struct btrfs_block_group_item item; - int dirty; + int data; + int cached; + u64 pinned; }; struct btrfs_fs_info { + u8 fsid[BTRFS_FSID_SIZE]; struct btrfs_root *fs_root; struct btrfs_root *extent_root; struct btrfs_root *tree_root; - struct btrfs_key last_insert; - struct cache_tree extent_cache; - struct cache_tree block_group_cache; - struct cache_tree pending_tree; - struct cache_tree pinned_tree; - struct cache_tree del_pending; - struct list_head trans; - struct list_head cache; - u64 last_inode_alloc; - u64 last_inode_alloc_dirid; + + struct extent_map_tree extent_cache; + struct extent_map_tree free_space_cache; + struct extent_map_tree block_group_cache; + struct extent_map_tree pending_tree; + struct extent_map_tree pinned_extents; + struct extent_map_tree del_pending; + struct extent_map_tree pending_del; + struct extent_map_tree extent_ins; + u64 generation; - int cache_size; + u64 last_trans_committed; + struct btrfs_trans_handle *running_transaction; + struct btrfs_super_block super_copy; + struct extent_buffer *sb_buffer; + struct mutex fs_mutex; int fp; - struct btrfs_trans_handle *running_transaction; - struct btrfs_super_block *disk_super; + u64 total_pinned; }; /* @@ -323,11 +329,13 @@ struct btrfs_fs_info { * and for the extent tree extent_root root. */ struct btrfs_root { - struct btrfs_buffer *node; - struct btrfs_buffer *commit_root; + struct extent_buffer *node; + struct extent_buffer *commit_root; struct btrfs_root_item root_item; struct btrfs_key root_key; struct btrfs_fs_info *fs_info; + u64 objectid; + u64 last_trans; /* data allocations are done in sectorsize units */ u32 sectorsize; @@ -342,14 +350,11 @@ struct btrfs_root { u32 stripesize; int ref_cows; + u32 type; + u64 highest_inode; + u64 last_inode_alloc; }; - -/* the lower bits in the key flags defines the item type */ -#define BTRFS_KEY_TYPE_MAX 256 -#define BTRFS_KEY_TYPE_SHIFT 24 -#define BTRFS_KEY_TYPE_MASK (((u32)BTRFS_KEY_TYPE_MAX - 1) << \ - BTRFS_KEY_TYPE_SHIFT) /* * inode items have the data typically returned from stat and store other @@ -402,6 +407,50 @@ struct btrfs_root { * data in the FS */ #define BTRFS_STRING_ITEM_KEY 253 + + +#define read_eb_member(eb, ptr, type, member, result) ( \ + read_extent_buffer(eb, (char *)(result), \ + ((unsigned long)(ptr)) + \ + offsetof(type, member), \ + sizeof(((type *)0)->member))) + +#define write_eb_member(eb, ptr, type, member, result) ( \ + write_extent_buffer(eb, (char *)(result), \ + ((unsigned long)(ptr)) + \ + offsetof(type, member), \ + sizeof(((type *)0)->member))) + +#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \ +static inline u##bits btrfs_##name(struct extent_buffer *eb) \ +{ \ + struct btrfs_header *h = (struct btrfs_header *)eb->data; \ + return le##bits##_to_cpu(h->member); \ +} \ +static inline void btrfs_set_##name(struct extent_buffer *eb, \ + u##bits val) \ +{ \ + struct btrfs_header *h = (struct btrfs_header *)eb->data; \ + h->member = cpu_to_le##bits(val); \ +} + +#define BTRFS_SETGET_FUNCS(name, type, member, bits) \ +static inline u##bits btrfs_##name(struct extent_buffer *eb, \ + type *s) \ +{ \ + unsigned long offset = (unsigned long)s + \ + offsetof(type, member); \ + __le##bits *tmp = (__le##bits *)(eb->data + offset); \ + return le##bits##_to_cpu(*tmp); \ +} \ +static inline void btrfs_set_##name(struct extent_buffer *eb, \ + type *s, u##bits val) \ +{ \ + unsigned long offset = (unsigned long)s + \ + offsetof(type, member); \ + __le##bits *tmp = (__le##bits *)(eb->data + offset); \ + *tmp = cpu_to_le##bits(val); \ +} #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \ static inline u##bits btrfs_##name(type *s) \ @@ -413,94 +462,290 @@ static inline void btrfs_set_##name(type s->member = cpu_to_le##bits(val); \ } +/* struct btrfs_block_group_item */ BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item, used, 64); - -BTRFS_SETGET_STACK_FUNCS(inode_generation, struct btrfs_inode_item, +BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item, + used, 64); + +/* struct btrfs_inode_ref */ +BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16); + +/* struct btrfs_inode_item */ +BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64); +BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64); +BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64); +BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64); +BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32); +BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32); +BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32); +BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32); +BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32); +BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16); +BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item, + compat_flags, 16); + +BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, + struct btrfs_inode_item, generation, 64); +BTRFS_SETGET_STACK_FUNCS(stack_inode_size, + struct btrfs_inode_item, size, 64); +BTRFS_SETGET_STACK_FUNCS(stack_inode_nblocks, + struct btrfs_inode_item, nblocks, 64); +BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, + struct btrfs_inode_item, block_group, 64); +BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, + struct btrfs_inode_item, nlink, 32); +BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, + struct btrfs_inode_item, uid, 32); +BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, + struct btrfs_inode_item, gid, 32); +BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, + struct btrfs_inode_item, mode, 32); +BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, + struct btrfs_inode_item, rdev, 32); +BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, + struct btrfs_inode_item, flags, 16); +BTRFS_SETGET_STACK_FUNCS(stack_inode_compat_flags, + struct btrfs_inode_item, compat_flags, 16); + +static inline struct btrfs_inode_timespec * +btrfs_inode_atime(struct btrfs_inode_item *inode_item) +{ + unsigned long ptr = (unsigned long)inode_item; + ptr += offsetof(struct btrfs_inode_item, atime); + return (struct btrfs_inode_timespec *)ptr; +} + +static inline struct btrfs_inode_timespec * +btrfs_inode_mtime(struct btrfs_inode_item *inode_item) +{ + unsigned long ptr = (unsigned long)inode_item; + ptr += offsetof(struct btrfs_inode_item, mtime); + return (struct btrfs_inode_timespec *)ptr; +} + +static inline struct btrfs_inode_timespec * +btrfs_inode_ctime(struct btrfs_inode_item *inode_item) +{ + unsigned long ptr = (unsigned long)inode_item; + ptr += offsetof(struct btrfs_inode_item, ctime); + return (struct btrfs_inode_timespec *)ptr; +} + +static inline struct btrfs_inode_timespec * +btrfs_inode_otime(struct btrfs_inode_item *inode_item) +{ + unsigned long ptr = (unsigned long)inode_item; + ptr += offsetof(struct btrfs_inode_item, otime); + return (struct btrfs_inode_timespec *)ptr; +} + +BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_inode_timespec, sec, 64); +BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_inode_timespec, nsec, 32); +BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_inode_timespec, + sec, 64); +BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_inode_timespec, + nsec, 32); + +/* struct btrfs_extent_item */ +BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32); + +/* struct btrfs_extent_ref */ +BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64); +BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64); +BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64); +BTRFS_SETGET_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64); + +BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64); +BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref, generation, 64); - -BTRFS_SETGET_STACK_FUNCS(inode_size, struct btrfs_inode_item, size, 64); -BTRFS_SETGET_STACK_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64); -BTRFS_SETGET_STACK_FUNCS(inode_block_group, struct btrfs_inode_item, - block_group, 64); -BTRFS_SETGET_STACK_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32); -BTRFS_SETGET_STACK_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32); -BTRFS_SETGET_STACK_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32); -BTRFS_SETGET_STACK_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32); -BTRFS_SETGET_STACK_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32); -BTRFS_SETGET_STACK_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16); -BTRFS_SETGET_STACK_FUNCS(inode_compat_flags, struct btrfs_inode_item, - compat_flags, 16); - -BTRFS_SETGET_STACK_FUNCS(timpsec_sec, struct btrfs_inode_timespec, sec, 64); -BTRFS_SETGET_STACK_FUNCS(timpsec_nsec, struct btrfs_inode_timespec, nsec, 32); -BTRFS_SETGET_STACK_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32); - -BTRFS_SETGET_STACK_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, - name_len, 16); - - -BTRFS_SETGET_STACK_FUNCS(ref_root, struct btrfs_extent_ref, root, 64); -BTRFS_SETGET_STACK_FUNCS(ref_generation, struct btrfs_extent_ref, - generation, 64); -BTRFS_SETGET_STACK_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64); -BTRFS_SETGET_STACK_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64); - -static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr) -{ - return le64_to_cpu(n->ptrs[nr].blockptr); -} - -static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr, - u64 val) -{ - n->ptrs[nr].blockptr = cpu_to_le64(val); -} - -static inline u64 btrfs_node_ptr_generation(struct btrfs_node *n, int nr) -{ - return le64_to_cpu(n->ptrs[nr].generation); -} - -static inline void btrfs_set_node_ptr_generation(struct btrfs_node *n, int nr, - u64 val) -{ - n->ptrs[nr].generation = cpu_to_le64(val); -} - -BTRFS_SETGET_STACK_FUNCS(item_offset, struct btrfs_item, offset, 32); - -static inline u32 btrfs_item_end(struct btrfs_item *item) -{ - return le32_to_cpu(item->offset) + le32_to_cpu(item->size); -} - -BTRFS_SETGET_STACK_FUNCS(item_size, struct btrfs_item, size, 32); -BTRFS_SETGET_STACK_FUNCS(dir_type, struct btrfs_dir_item, type, 8); -BTRFS_SETGET_STACK_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16); -BTRFS_SETGET_STACK_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16); - -static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, - struct btrfs_disk_key *disk) -{ - cpu->offset = le64_to_cpu(disk->offset); - cpu->type = le32_to_cpu(disk->type); - cpu->objectid = le64_to_cpu(disk->objectid); -} - -static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, - struct btrfs_key *cpu) -{ - disk->offset = cpu_to_le64(cpu->offset); - disk->type = cpu_to_le32(cpu->type); - disk->objectid = cpu_to_le64(cpu->objectid); -} - +BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref, + objectid, 64); +BTRFS_SETGET_STACK_FUNCS(stack_ref_offset, struct btrfs_extent_ref, offset, 64); + +BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item, + refs, 32); + +/* struct btrfs_node */ +BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64); +BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64); + +static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr) +{ + unsigned long ptr; + ptr = offsetof(struct btrfs_node, ptrs) + + sizeof(struct btrfs_key_ptr) * nr; + return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr); +} + +static inline void btrfs_set_node_blockptr(struct extent_buffer *eb, + int nr, u64 val) +{ + unsigned long ptr; + ptr = offsetof(struct btrfs_node, ptrs) + + sizeof(struct btrfs_key_ptr) * nr; + btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val); +} + +static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr) +{ + unsigned long ptr; + ptr = offsetof(struct btrfs_node, ptrs) + + sizeof(struct btrfs_key_ptr) * nr; + return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr); +} + +static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb, + int nr, u64 val) +{ + unsigned long ptr; + ptr = offsetof(struct btrfs_node, ptrs) + + sizeof(struct btrfs_key_ptr) * nr; + btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val); +} + +static inline unsigned long btrfs_node_key_ptr_offset(int nr) +{ + return offsetof(struct btrfs_node, ptrs) + + sizeof(struct btrfs_key_ptr) * nr; +} + +static inline void btrfs_node_key(struct extent_buffer *eb, + struct btrfs_disk_key *disk_key, int nr) +{ + unsigned long ptr; + ptr = btrfs_node_key_ptr_offset(nr); + read_eb_member(eb, (struct btrfs_key_ptr *)ptr, + struct btrfs_key_ptr, key, disk_key); +} + +static inline void btrfs_set_node_key(struct extent_buffer *eb, + struct btrfs_disk_key *disk_key, int nr) +{ + unsigned long ptr; + ptr = btrfs_node_key_ptr_offset(nr); + write_eb_member(eb, (struct btrfs_key_ptr *)ptr, + struct btrfs_key_ptr, key, disk_key); +} + +/* struct btrfs_item */ +BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32); +BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32); + +static inline unsigned long btrfs_item_nr_offset(int nr) +{ + return offsetof(struct btrfs_leaf, items) + + sizeof(struct btrfs_item) * nr; +} + +static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb, + int nr) +{ + return (struct btrfs_item *)btrfs_item_nr_offset(nr); +} + +static inline u32 btrfs_item_end(struct extent_buffer *eb, + struct btrfs_item *item) +{ + return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item); +} + +static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr) +{ + return btrfs_item_end(eb, btrfs_item_nr(eb, nr)); +} + +static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr) +{ + return btrfs_item_offset(eb, btrfs_item_nr(eb, nr)); +} + +static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr) +{ + return btrfs_item_size(eb, btrfs_item_nr(eb, nr)); +} + +static inline void btrfs_item_key(struct extent_buffer *eb, + struct btrfs_disk_key *disk_key, int nr) +{ + struct btrfs_item *item = btrfs_item_nr(eb, nr); + read_eb_member(eb, item, struct btrfs_item, key, disk_key); +} + +static inline void btrfs_set_item_key(struct extent_buffer *eb, + struct btrfs_disk_key *disk_key, int nr) +{ + struct btrfs_item *item = btrfs_item_nr(eb, nr); + write_eb_member(eb, item, struct btrfs_item, key, disk_key); +} + +/* struct btrfs_dir_item */ +BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16); +BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8); +BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16); + +static inline void btrfs_dir_item_key(struct extent_buffer *eb, + struct btrfs_dir_item *item, + struct btrfs_disk_key *key) +{ + read_eb_member(eb, item, struct btrfs_dir_item, location, key); +} + +static inline void btrfs_set_dir_item_key(struct extent_buffer *eb, + struct btrfs_dir_item *item, + struct btrfs_disk_key *key) +{ + write_eb_member(eb, item, struct btrfs_dir_item, location, key); +} + +/* struct btrfs_disk_key */ BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key, objectid, 64); BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64); BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8); +static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, + struct btrfs_disk_key *disk) +{ + cpu->offset = le64_to_cpu(disk->offset); + cpu->type = disk->type; + cpu->objectid = le64_to_cpu(disk->objectid); +} + +static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, + struct btrfs_key *cpu) +{ + disk->offset = cpu_to_le64(cpu->offset); + disk->type = cpu->type; + disk->objectid = cpu_to_le64(cpu->objectid); +} + +static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb, + struct btrfs_key *key, int nr) +{ + struct btrfs_disk_key disk_key; + btrfs_node_key(eb, &disk_key, nr); + btrfs_disk_key_to_cpu(key, &disk_key); +} + +static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb, + struct btrfs_key *key, int nr) +{ + struct btrfs_disk_key disk_key; + btrfs_item_key(eb, &disk_key, nr); + btrfs_disk_key_to_cpu(key, &disk_key); +} + +static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb, + struct btrfs_dir_item *item, + struct btrfs_key *key) +{ + struct btrfs_disk_key disk_key; + btrfs_dir_item_key(eb, item, &disk_key); + btrfs_disk_key_to_cpu(key, &disk_key); +} + + static inline u8 btrfs_key_type(struct btrfs_key *key) { return key->type; @@ -511,44 +756,73 @@ static inline void btrfs_set_key_type(st key->type = val; } -BTRFS_SETGET_STACK_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64); -BTRFS_SETGET_STACK_FUNCS(header_generation, struct btrfs_header, - generation, 64); -BTRFS_SETGET_STACK_FUNCS(header_owner, struct btrfs_header, owner, 64); -BTRFS_SETGET_STACK_FUNCS(header_nritems, struct btrfs_header, nritems, 32); -BTRFS_SETGET_STACK_FUNCS(header_flags, struct btrfs_header, flags, 16); - -static inline int btrfs_header_level(struct btrfs_header *h) -{ - return h->level; -} - -static inline void btrfs_set_header_level(struct btrfs_header *h, int level) -{ - BUG_ON(level > BTRFS_MAX_LEVEL); - h->level = level; -} - -static inline int btrfs_is_leaf(struct btrfs_node *n) -{ - return (btrfs_header_level(&n->header) == 0); -} +/* struct btrfs_header */ +BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64); +BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header, + generation, 64); +BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64); +BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32); +BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 16); +BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8); + +static inline u8 *btrfs_header_fsid(struct extent_buffer *eb) +{ + unsigned long ptr = offsetof(struct btrfs_header, fsid); + return (u8 *)ptr; +} + +static inline u8 *btrfs_super_fsid(struct extent_buffer *eb) +{ + unsigned long ptr = offsetof(struct btrfs_super_block, fsid); + return (u8 *)ptr; +} + +static inline u8 *btrfs_header_csum(struct extent_buffer *eb) +{ + unsigned long ptr = offsetof(struct btrfs_header, csum); + return (u8 *)ptr; +} + +static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb) +{ + return NULL; +} + +static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb) +{ + return NULL; +} + +static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb) +{ + return NULL; +} + +static inline int btrfs_is_leaf(struct extent_buffer *eb) +{ + return (btrfs_header_level(eb) == 0); +} + +/* struct btrfs_root_item */ +BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32); +BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64); +BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8); BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64); -BTRFS_SETGET_STACK_FUNCS(root_byte_limit, struct btrfs_root_item, - byte_limit, 64); BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8); BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64); BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32); BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32); -BTRFS_SETGET_STACK_FUNCS(root_bytes_used, struct btrfs_root_item, - bytes_used, 64); +BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64); +BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64); + +/* struct btrfs_super_block */ BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64); BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block, generation, 64); +BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64); BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block, root_level, 8); -BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64); BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block, total_bytes, 64); BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block, @@ -564,73 +838,139 @@ BTRFS_SETGET_STACK_FUNCS(super_root_dir, BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block, root_dir_objectid, 64); -static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l) -{ - return (u8 *)l->items; -} - -BTRFS_SETGET_STACK_FUNCS(file_extent_type, struct btrfs_file_extent_item, - type, 8); - -static inline char *btrfs_file_extent_inline_start(struct +static inline unsigned long btrfs_leaf_data(struct extent_buffer *l) +{ + return offsetof(struct btrfs_leaf, items); +} + +/* struct btrfs_file_extent_item */ +BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8); + +static inline unsigned long btrfs_file_extent_inline_start(struct btrfs_file_extent_item *e) { - return (char *)(&e->disk_bytenr); + unsigned long offset = (unsigned long)e; + offset += offsetof(struct btrfs_file_extent_item, disk_bytenr); + return offset; } static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize) { - return (unsigned long)(&((struct - btrfs_file_extent_item *)NULL)->disk_bytenr) + datasize; -} - -static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e) -{ - struct btrfs_file_extent_item *fe = NULL; - return btrfs_item_size(e) - (unsigned long)(&fe->disk_bytenr); -} - -BTRFS_SETGET_STACK_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item, - disk_bytenr, 64); -BTRFS_SETGET_STACK_FUNCS(file_extent_generation, struct btrfs_file_extent_item, - generation, 64); -BTRFS_SETGET_STACK_FUNCS(file_extent_disk_num_bytes, - struct btrfs_file_extent_item, disk_num_bytes, 64); -BTRFS_SETGET_STACK_FUNCS(file_extent_offset, struct btrfs_file_extent_item, - offset, 64); -BTRFS_SETGET_STACK_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item, - num_bytes, 64); + return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize; +} + +static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb, + struct btrfs_item *e) +{ + unsigned long offset; + offset = offsetof(struct btrfs_file_extent_item, disk_bytenr); + return btrfs_item_size(eb, e) - offset; +} + +BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item, + disk_bytenr, 64); +BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item, + generation, 64); +BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item, + disk_num_bytes, 64); +BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item, + offset, 64); +BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item, + num_bytes, 64); + +static inline u32 btrfs_level_size(struct btrfs_root *root, int level) { + if (level == 0) + return root->leafsize; + return root->nodesize; +} /* helper function to cast into the data area of the leaf. */ #define btrfs_item_ptr(leaf, slot, type) \ ((type *)(btrfs_leaf_data(leaf) + \ - btrfs_item_offset((leaf)->items + (slot)))) + btrfs_item_offset_nr(leaf, slot))) + #define btrfs_item_ptr_offset(leaf, slot) \ ((unsigned long)(btrfs_leaf_data(leaf) + \ btrfs_item_offset_nr(leaf, slot))) -static inline u32 btrfs_level_size(struct btrfs_root *root, int level) -{ - if (level == 0) - return root->leafsize; - return root->nodesize; -} -int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2); -struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - u32 blocksize); -int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf); +/* extent-tree.c */ +u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, + struct btrfs_path *count_path, + u64 first_extent); +int btrfs_extent_post_op(struct btrfs_trans_handle *trans, + struct btrfs_root *root); +int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy); +struct btrfs_block_group_cache *btrfs_lookup_block_group(struct + btrfs_fs_info *info, + u64 bytenr); +struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, + struct btrfs_block_group_cache + *hint, u64 search_start, + int data, int owner); int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 owner_objectid); +struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u32 size, + u64 root_objectid, + u64 hint, u64 empty_size); +struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u32 blocksize, + u64 root_objectid, + u64 ref_generation, + u64 first_objectid, + int level, + u64 hint, + u64 empty_size); +int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 new_size); +int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size); +int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 bytenr, + u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset); +int btrfs_alloc_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u64 num_bytes, u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset, + u64 empty_size, u64 hint_byte, + u64 search_end, struct btrfs_key *ins, int data); +int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, + struct extent_buffer *buf); int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 bytenr, u64 num_bytes, - u64 root_objectid, u64 root_generation, - u64 owner, u64 owner_offset, int pin); + u64 root_objectid, u64 ref_generation, + u64 owner_objectid, u64 owner_offset, int pin); +int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_map_tree *unpin); +int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u64 bytenr, u64 num_bytes, + u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset); +int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, + struct btrfs_root *root); +int btrfs_free_block_groups(struct btrfs_fs_info *info); +int btrfs_read_block_groups(struct btrfs_root *root); +int btrfs_make_block_groups(struct btrfs_trans_handle *trans, + struct btrfs_root *root); +u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset); +int btrfs_update_block_group(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 bytenr, u64 num, + int alloc, int mark_free, int data); +/* ctree.c */ +int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2); int btrfs_cow_block(struct btrfs_trans_handle *trans, - struct btrfs_root *root, struct btrfs_buffer *buf, - struct btrfs_buffer *parent, int parent_slot, - struct btrfs_buffer **cow_ret); + struct btrfs_root *root, struct extent_buffer *buf, + struct extent_buffer *parent, int parent_slot, + struct extent_buffer **cow_ret); +int btrfs_copy_root(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_buffer *buf, + struct extent_buffer **cow_ret, u64 new_root_objectid); int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, u32 data_size); int btrfs_truncate_item(struct btrfs_trans_handle *trans, @@ -640,7 +980,13 @@ int btrfs_search_slot(struct btrfs_trans int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_key *key, struct btrfs_path *p, int ins_len, int cow); +int btrfs_realloc_node(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct extent_buffer *parent, + int start_slot, int cache_only, u64 *last_ret, + struct btrfs_key *progress); void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); +struct btrfs_path *btrfs_alloc_path(void); +void btrfs_free_path(struct btrfs_path *p); void btrfs_init_path(struct btrfs_path *p); int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path); @@ -650,11 +996,13 @@ int btrfs_insert_empty_item(struct btrfs *root, struct btrfs_path *path, struct btrfs_key *cpu_key, u32 data_size); int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); -int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf); +int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path); +int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf); int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_buffer *snap); -int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct - btrfs_root *root); + *root); + + +/* root-item.c */ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_key *key); int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root @@ -665,12 +1013,23 @@ int btrfs_update_root(struct btrfs_trans *item); int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct btrfs_root_item *item, struct btrfs_key *key); +int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, + struct btrfs_root *latest_root); +/* dir-item.c */ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root - *root, char *name, int name_len, u64 dir, + *root, const char *name, int name_len, u64 dir, struct btrfs_key *location, u8 type); struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, - struct btrfs_root *root, struct btrfs_path *path, - u64 dir, char *name, int name_len, int mod); + struct btrfs_root *root, + struct btrfs_path *path, u64 dir, + const char *name, int name_len, + int mod); +struct btrfs_dir_item * +btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 dir, + u64 objectid, const char *name, int name_len, + int mod); struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path, const char *name, int name_len); @@ -678,38 +1037,64 @@ int btrfs_delete_one_dir_name(struct btr struct btrfs_root *root, struct btrfs_path *path, struct btrfs_dir_item *di); +int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, const char *name, + u16 name_len, const void *data, u16 data_len, + u64 dir); +struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 dir, + const char *name, u16 name_len, + int mod); +/* inode-map.c */ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, struct btrfs_root *fs_root, u64 dirid, u64 *objectid); +int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid); + +/* inode-item.c */ +int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + const char *name, int name_len, + u64 inode_objectid, u64 ref_objectid); +int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + const char *name, int name_len, + u64 inode_objectid, u64 ref_objectid); +int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 objectid); int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 objectid, struct btrfs_inode_item *inode_item); int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, u64 objectid, int mod); -int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, - struct btrfs_root *root); -int btrfs_free_block_groups(struct btrfs_fs_info *info); -int btrfs_read_block_groups(struct btrfs_root *root); -int btrfs_insert_block_group(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_key *key, - struct btrfs_block_group_item *bi); + *root, struct btrfs_path *path, + struct btrfs_key *location, int mod); + /* file-item.c */ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 objectid, u64 pos, u64 offset, - u64 disk_num_bytes, u64 num_bytes); -int btrfs_insert_inline_file_extent(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid, - u64 offset, char *buffer, size_t size); -int btrfs_lookup_csum(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, u64 objectid, - u64 offset, int cow, struct btrfs_csum_item **item_ret); -int btrfs_csum_file_block(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_inode_item *inode, - u64 objectid, u64 offset, char *data, size_t len); -int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - const char *name, int name_len, - u64 inode_objectid, u64 ref_objectid); + u64 disk_num_bytes, + u64 num_bytes); +int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid, + u64 offset, char *buffer, size_t size); +int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 objectid, + u64 bytenr, int mod); +int btrfs_csum_file_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_inode_item *inode, + u64 objectid, u64 offset, + char *data, size_t len); +struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, + u64 objectid, u64 offset, + int cow); +int btrfs_csum_truncate(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct btrfs_path *path, + u64 isize); #endif diff -r 59fc7ac63e46 debug-tree.c --- a/debug-tree.c Fri Dec 21 16:25:35 2007 -0500 +++ b/debug-tree.c Fri Jan 04 19:19:50 2008 +0800 @@ -27,12 +27,11 @@ #include "transaction.h" int main(int ac, char **av) { - struct btrfs_super_block super; struct btrfs_root *root; struct btrfs_path path; struct btrfs_key key; - struct btrfs_root_item *ri; - struct btrfs_leaf *leaf; + struct btrfs_root_item ri; + struct extent_buffer *leaf; struct btrfs_key found_key; char uuidbuf[37]; int ret; @@ -43,7 +42,7 @@ int main(int ac, char **av) { exit(1); } radix_tree_init(); - root = open_ctree(av[1], &super); + root = open_ctree(av[1], 0); if (!root) { fprintf(stderr, "unable to open %s\n", av[1]); exit(1); @@ -59,23 +58,23 @@ int main(int ac, char **av) { &key, &path, 0, 0); BUG_ON(ret < 0); while(1) { - leaf = &path.nodes[0]->leaf; + leaf = path.nodes[0]; slot = path.slots[0]; - if (slot >= btrfs_header_nritems(&leaf->header)) { + if (slot >= btrfs_header_nritems(leaf)) { ret = btrfs_next_leaf(root, &path); if (ret != 0) break; - leaf = &path.nodes[0]->leaf; + leaf = path.nodes[0]; slot = path.slots[0]; } - btrfs_disk_key_to_cpu(&found_key, - &leaf->items[path.slots[0]].key); + btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]); if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) { - struct btrfs_buffer *buf; - ri = btrfs_item_ptr(leaf, path.slots[0], - struct btrfs_root_item); + unsigned long offset; + struct extent_buffer *buf; + offset = btrfs_item_ptr_offset(leaf, slot); + read_extent_buffer(leaf, &ri, offset, sizeof(ri)); buf = read_tree_block(root->fs_info->tree_root, - btrfs_root_bytenr(ri), + btrfs_root_bytenr(&ri), root->leafsize); switch(found_key.objectid) { case BTRFS_ROOT_TREE_OBJECTID: @@ -95,11 +94,11 @@ int main(int ac, char **av) { } btrfs_release_path(root, &path); printf("total bytes %llu\n", - (unsigned long long)btrfs_super_total_bytes(&super)); + (unsigned long long)btrfs_super_total_bytes(&root->fs_info->super_copy)); printf("bytes used %llu\n", - (unsigned long long)btrfs_super_bytes_used(&super)); + (unsigned long long)btrfs_super_bytes_used(&root->fs_info->super_copy)); uuidbuf[36] = '\0'; - uuid_unparse(super.fsid, uuidbuf); + uuid_unparse(root->fs_info->super_copy.fsid, uuidbuf); printf("uuid %s\n", uuidbuf); return 0; } diff -r 59fc7ac63e46 dir-item.c --- a/dir-item.c Fri Dec 21 16:25:35 2007 -0500 +++ b/dir-item.c Fri Jan 04 19:19:50 2008 +0800 @@ -16,17 +16,13 @@ * Boston, MA 021110-1307, USA. */ -#include -#include -#include "kerncompat.h" -#include "radix-tree.h" #include "ctree.h" #include "disk-io.h" #include "hash.h" #include "transaction.h" -static struct btrfs_dir_item *insert_with_overflow(struct - btrfs_trans_handle *trans, +static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle + *trans, struct btrfs_root *root, struct btrfs_path *path, struct btrfs_key *cpu_key, @@ -37,110 +33,183 @@ static struct btrfs_dir_item *insert_wit int ret; char *ptr; struct btrfs_item *item; - struct btrfs_leaf *leaf; + struct extent_buffer *leaf; + ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); if (ret == -EEXIST) { struct btrfs_dir_item *di; di = btrfs_match_dir_item_name(root, path, name, name_len); if (di) - return NULL; + return ERR_PTR(-EEXIST); ret = btrfs_extend_item(trans, root, path, data_size); - } - BUG_ON(ret > 0); - if (ret) - return NULL; - leaf = &path->nodes[0]->leaf; - item = leaf->items + path->slots[0]; + WARN_ON(ret > 0); + } + if (ret < 0) + return ERR_PTR(ret); + WARN_ON(ret > 0); + leaf = path->nodes[0]; + item = btrfs_item_nr(leaf, path->slots[0]); ptr = btrfs_item_ptr(leaf, path->slots[0], char); - BUG_ON(data_size > btrfs_item_size(item)); - ptr += btrfs_item_size(item) - data_size; + BUG_ON(data_size > btrfs_item_size(leaf, item)); + ptr += btrfs_item_size(leaf, item) - data_size; return (struct btrfs_dir_item *)ptr; } +int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, const char *name, + u16 name_len, const void *data, u16 data_len, + u64 dir) +{ + int ret = 0; + struct btrfs_path *path; + struct btrfs_dir_item *dir_item; + unsigned long name_ptr, data_ptr; + struct btrfs_key key, location; + struct btrfs_disk_key disk_key; + struct extent_buffer *leaf; + u32 data_size; + + key.objectid = dir; + btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); + ret = btrfs_name_hash(name, name_len, &key.offset); + BUG_ON(ret); + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + data_size = sizeof(*dir_item) + name_len + data_len; + dir_item = insert_with_overflow(trans, root, path, &key, data_size, + name, name_len); + /* + * FIXME: at some point we should handle xattr's that are larger than + * what we can fit in our leaf. We set location to NULL b/c we arent + * pointing at anything else, that will change if we store the xattr + * data in a separate inode. + */ + BUG_ON(IS_ERR(dir_item)); + memset(&location, 0, sizeof(location)); + + leaf = path->nodes[0]; + btrfs_cpu_key_to_disk(&disk_key, &location); + btrfs_set_dir_item_key(leaf, dir_item, &disk_key); + btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR); + btrfs_set_dir_name_len(leaf, dir_item, name_len); + btrfs_set_dir_data_len(leaf, dir_item, data_len); + name_ptr = (unsigned long)(dir_item + 1); + data_ptr = (unsigned long)((char *)name_ptr + name_len); + + write_extent_buffer(leaf, name, name_ptr, name_len); + write_extent_buffer(leaf, data, data_ptr, data_len); + btrfs_mark_buffer_dirty(path->nodes[0]); + + btrfs_free_path(path); + return ret; +} + int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root - *root, char *name, int name_len, u64 dir, + *root, const char *name, int name_len, u64 dir, struct btrfs_key *location, u8 type) { int ret = 0; - struct btrfs_path path; + int ret2 = 0; + struct btrfs_path *path; struct btrfs_dir_item *dir_item; - char *name_ptr; - struct btrfs_key key; + struct extent_buffer *leaf; + unsigned long name_ptr; + struct btrfs_key key; + struct btrfs_disk_key disk_key; u32 data_size; key.objectid = dir; btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); - if (name_len == 1 && *name == '.') - key.offset = 1; - else if (name_len == 2 && name[0] == '.' && name[1] == '.') - key.offset = 2; - else - ret = btrfs_name_hash(name, name_len, &key.offset); - BUG_ON(ret); - btrfs_init_path(&path); + ret = btrfs_name_hash(name, name_len, &key.offset); + BUG_ON(ret); + path = btrfs_alloc_path(); data_size = sizeof(*dir_item) + name_len; - dir_item = insert_with_overflow(trans, root, &path, &key, data_size, + dir_item = insert_with_overflow(trans, root, path, &key, data_size, name, name_len); - if (!dir_item) { - ret = -1; + if (IS_ERR(dir_item)) { + ret = PTR_ERR(dir_item); + if (ret == -EEXIST) + goto second_insert; goto out; } - btrfs_cpu_key_to_disk(&dir_item->location, location); - btrfs_set_dir_type(dir_item, type); - btrfs_set_dir_name_len(dir_item, name_len); - btrfs_set_dir_data_len(dir_item, 0); - name_ptr = (char *)(dir_item + 1); - memcpy(name_ptr, name, name_len); - + + leaf = path->nodes[0]; + btrfs_cpu_key_to_disk(&disk_key, location); + btrfs_set_dir_item_key(leaf, dir_item, &disk_key); + btrfs_set_dir_type(leaf, dir_item, type); + btrfs_set_dir_data_len(leaf, dir_item, 0); + btrfs_set_dir_name_len(leaf, dir_item, name_len); + name_ptr = (unsigned long)(dir_item + 1); + + write_extent_buffer(leaf, name, name_ptr, name_len); + btrfs_mark_buffer_dirty(leaf); + +second_insert: /* FIXME, use some real flag for selecting the extra index */ - if (root == root->fs_info->tree_root) + if (root == root->fs_info->tree_root) { + ret = 0; goto out; - - btrfs_release_path(root, &path); + } + btrfs_release_path(root, path); + btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); key.offset = location->objectid; - dir_item = insert_with_overflow(trans, root, &path, &key, data_size, + dir_item = insert_with_overflow(trans, root, path, &key, data_size, name, name_len); - if (!dir_item) { - ret = -1; + if (IS_ERR(dir_item)) { + ret2 = PTR_ERR(dir_item); goto out; } - btrfs_cpu_key_to_disk(&dir_item->location, location); - btrfs_set_dir_type(dir_item, type); - btrfs_set_dir_name_len(dir_item, name_len); - btrfs_set_dir_data_len(dir_item, 0); - name_ptr = (char *)(dir_item + 1); - memcpy(name_ptr, name, name_len); + leaf = path->nodes[0]; + btrfs_cpu_key_to_disk(&disk_key, location); + btrfs_set_dir_item_key(leaf, dir_item, &disk_key); + btrfs_set_dir_type(leaf, dir_item, type); + btrfs_set_dir_data_len(leaf, dir_item, 0); + btrfs_set_dir_name_len(leaf, dir_item, name_len); + name_ptr = (unsigned long)(dir_item + 1); + write_extent_buffer(leaf, name, name_ptr, name_len); + btrfs_mark_buffer_dirty(leaf); out: - btrfs_release_path(root, &path); - return ret; -} + btrfs_free_path(path); + if (ret) + return ret; + if (ret2) + return ret2; + return 0; +} + struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_path *path, u64 dir, - char *name, int name_len, int mod) + struct btrfs_root *root, + struct btrfs_path *path, u64 dir, + const char *name, int name_len, + int mod) { int ret; struct btrfs_key key; int ins_len = mod < 0 ? -1 : 0; int cow = mod != 0; struct btrfs_key found_key; - struct btrfs_leaf *leaf; + struct extent_buffer *leaf; + key.objectid = dir; btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); - ret = btrfs_name_hash(name, name_len, &key.offset); - BUG_ON(ret); + + ret = btrfs_name_hash(name, name_len, &key.offset); + BUG_ON(ret); + ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); if (ret < 0) - return NULL; + return ERR_PTR(ret); if (ret > 0) { if (path->slots[0] == 0) return NULL; path->slots[0]--; } - leaf = &path->nodes[0]->leaf; - btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); + leaf = path->nodes[0]; + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); if (found_key.objectid != dir || btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY || @@ -150,27 +219,89 @@ struct btrfs_dir_item *btrfs_lookup_dir_ return btrfs_match_dir_item_name(root, path, name, name_len); } +struct btrfs_dir_item * +btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 dir, + u64 objectid, const char *name, int name_len, + int mod) +{ + int ret; + struct btrfs_key key; + int ins_len = mod < 0 ? -1 : 0; + int cow = mod != 0; + + key.objectid = dir; + btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); + key.offset = objectid; + + ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); + if (ret < 0) + return ERR_PTR(ret); + if (ret > 0) + return ERR_PTR(-ENOENT); + return btrfs_match_dir_item_name(root, path, name, name_len); +} + +struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 dir, + const char *name, u16 name_len, + int mod) +{ + int ret; + struct btrfs_key key; + int ins_len = mod < 0 ? -1 : 0; + int cow = mod != 0; + struct btrfs_key found_key; + struct extent_buffer *leaf; + + key.objectid = dir; + btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); + ret = btrfs_name_hash(name, name_len, &key.offset); + BUG_ON(ret); + ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); + if (ret < 0) + return ERR_PTR(ret); + if (ret > 0) { + if (path->slots[0] == 0) + return NULL; + path->slots[0]--; + } + + leaf = path->nodes[0]; + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); + + if (found_key.objectid != dir || + btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY || + found_key.offset != key.offset) + return NULL; + + return btrfs_match_dir_item_name(root, path, name, name_len); +} + struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path, const char *name, int name_len) { + struct btrfs_dir_item *dir_item; + unsigned long name_ptr; + u32 total_len; u32 cur = 0; u32 this_len; - u32 total_len; - char *name_ptr; - struct btrfs_leaf *leaf; - struct btrfs_dir_item *dir_item; - - leaf = &path->nodes[0]->leaf; + struct extent_buffer *leaf; + + leaf = path->nodes[0]; dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); - total_len = btrfs_item_size(leaf->items + path->slots[0]); + total_len = btrfs_item_size_nr(leaf, path->slots[0]); while(cur < total_len) { - this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item) + - btrfs_dir_data_len(dir_item); - name_ptr = (char *)(dir_item + 1); - - if (btrfs_dir_name_len(dir_item) == name_len && - memcmp(name, name_ptr, name_len) == 0) + this_len = sizeof(*dir_item) + + btrfs_dir_name_len(leaf, dir_item) + + btrfs_dir_data_len(leaf, dir_item); + name_ptr = (unsigned long)(dir_item + 1); + + if (btrfs_dir_name_len(leaf, dir_item) == name_len && + memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) return dir_item; cur += this_len; @@ -186,24 +317,28 @@ int btrfs_delete_one_dir_name(struct btr struct btrfs_dir_item *di) { - struct btrfs_leaf *leaf; + struct extent_buffer *leaf; u32 sub_item_len; u32 item_len; int ret = 0; - leaf = &path->nodes[0]->leaf; - sub_item_len = sizeof(*di) + btrfs_dir_name_len(di) + - btrfs_dir_data_len(di); - item_len = btrfs_item_size(leaf->items + path->slots[0]); + leaf = path->nodes[0]; + sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) + + btrfs_dir_data_len(leaf, di); + item_len = btrfs_item_size_nr(leaf, path->slots[0]); if (sub_item_len == item_len) { ret = btrfs_del_item(trans, root, path); } else { - char *ptr = (char *)di; - char *start = btrfs_item_ptr(leaf, path->slots[0], char); - memmove(ptr, ptr + sub_item_len, + /* MARKER */ + unsigned long ptr = (unsigned long)di; + unsigned long start; + + start = btrfs_item_ptr_offset(leaf, path->slots[0]); + memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, item_len - (ptr + sub_item_len - start)); ret = btrfs_truncate_item(trans, root, path, item_len - sub_item_len, 1); } return 0; } + diff -r 59fc7ac63e46 disk-io.c --- a/disk-io.c Fri Dec 21 16:25:35 2007 -0500 +++ b/disk-io.c Fri Jan 04 19:19:50 2008 +0800 @@ -31,215 +31,130 @@ #include "transaction.h" #include "crc32c.h" -static u64 allocated_bytes = 0; -int cache_max = 10000; - -int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh, - u64 logical) -{ - bh->fd = root->fs_info->fp; - bh->dev_bytenr = logical; - return 0; -} - -static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf) -{ - if (buf->bytenr != btrfs_header_bytenr(&buf->node.header)) +int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *buf, + u64 logical) +{ + buf->fd = root->fs_info->fp; + buf->dev_bytenr = logical; + return 0; +} + +static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) +{ + if (buf->start != btrfs_header_bytenr(buf)) BUG(); - if (memcmp(root->fs_info->disk_super->fsid, buf->node.header.fsid, - sizeof(buf->node.header.fsid))) + + if (memcmp_extent_buffer(buf, root->fs_info->fsid, + (unsigned long)btrfs_header_fsid(buf), + BTRFS_FSID_SIZE)) BUG(); return 0; } -static int free_some_buffers(struct btrfs_root *root) -{ - struct list_head *node, *next; - struct btrfs_buffer *b; - if (root->fs_info->cache_size < cache_max) - return 0; - list_for_each_safe(node, next, &root->fs_info->cache) { - b = list_entry(node, struct btrfs_buffer, cache); - if (b->count == 1) { - BUG_ON(!list_empty(&b->dirty)); - list_del_init(&b->cache); - btrfs_block_release(root, b); - if (root->fs_info->cache_size < cache_max) - break; +u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) +{ + return crc32c(seed, data, len); +} + +void btrfs_csum_final(u32 crc, char *result) +{ + *(__le32 *)result = ~cpu_to_le32(crc); +} + +static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, + int verify) +{ + char result[BTRFS_CRC32_SIZE]; + u32 len; + u32 crc = ~(u32)0; + + len = buf->len - BTRFS_CSUM_SIZE; + crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len); + btrfs_csum_final(crc, result); + + if (verify) { + if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { + printk("checksum verify failed on %llu\n", buf->start); + return 1; } - } - return 0; -} - -struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 bytenr, - u32 blocksize) -{ - struct btrfs_buffer *buf; - int ret; - - buf = malloc(sizeof(struct btrfs_buffer) + blocksize); - if (!buf) - return buf; - allocated_bytes += blocksize; - - buf->bytenr = bytenr; - buf->count = 2; - buf->size = blocksize; - buf->cache_node.start = bytenr; - buf->cache_node.size = blocksize; - - INIT_LIST_HEAD(&buf->dirty); - free_some_buffers(root); - - ret = insert_existing_cache_extent(&root->fs_info->extent_cache, - &buf->cache_node); - - list_add_tail(&buf->cache, &root->fs_info->cache); - root->fs_info->cache_size += blocksize; - if (ret) { - free(buf); + } else { + write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); + } + return 0; +} + +struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, + u64 bytenr, u32 blocksize) +{ + return find_extent_buffer(&root->fs_info->extent_cache, + bytenr, blocksize); +} + +struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, + u64 bytenr, u32 blocksize) +{ + return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr, + blocksize); +} + +int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) +{ + return 0; +} + +struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, + u32 blocksize) +{ + int ret; + struct extent_buffer *eb; + + eb = btrfs_find_create_tree_block(root, bytenr, blocksize); + if (!eb) return NULL; - } - return buf; -} - -struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 bytenr, - u32 blocksize) -{ - struct btrfs_buffer *buf; - struct cache_extent *cache; - - cache = find_cache_extent(&root->fs_info->extent_cache, - bytenr, blocksize); - if (cache) { - buf = container_of(cache, struct btrfs_buffer, cache_node); - buf->count++; - } else { - buf = alloc_tree_block(root, bytenr, blocksize); - if (!buf) { - BUG(); + if (!btrfs_buffer_uptodate(eb)) { + btrfs_map_bh_to_logical(root, eb, eb->start); + ret = read_extent_from_disk(eb); + if (ret) { + free_extent_buffer(eb); return NULL; } - } - return buf; -} - -struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, - u32 blocksize) -{ - struct btrfs_buffer *buf; - int ret; - struct cache_extent *cache; - - cache = find_cache_extent(&root->fs_info->extent_cache, - bytenr, blocksize); - if (cache) { - buf = container_of(cache, struct btrfs_buffer, cache_node); - buf->count++; - if (check_tree_block(root, buf)) - BUG(); - } else { - buf = alloc_tree_block(root, bytenr, blocksize); - if (!buf) - return NULL; - btrfs_map_bh_to_logical(root, buf, bytenr); - ret = pread(buf->fd, &buf->node, blocksize, - buf->dev_bytenr); - if (ret != blocksize) { - free(buf); - return NULL; - } - if (check_tree_block(root, buf)) - BUG(); - } - return buf; -} - -int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf) -{ - if (!list_empty(&buf->dirty)) - return 0; - list_add_tail(&buf->dirty, &root->fs_info->trans); - buf->count++; - if (check_tree_block(root, buf)) + btrfs_set_buffer_uptodate(eb); + } + return eb; +} + +int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, + struct extent_buffer *eb) +{ + if (check_tree_block(root, eb)) BUG(); - return 0; -} - -int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf) -{ - if (!list_empty(&buf->dirty)) { - list_del_init(&buf->dirty); - btrfs_block_release(root, buf); - } - return 0; -} - -int btrfs_csum_node(struct btrfs_root *root, struct btrfs_node *node) -{ - u32 crc = ~(u32)0; - size_t len = btrfs_level_size(root, btrfs_header_level(&node->header)) - - BTRFS_CSUM_SIZE; - - crc = crc32c(crc, (char *)(node) + BTRFS_CSUM_SIZE, len); - crc = ~cpu_to_le32(crc); - memcpy(node->header.csum, &crc, BTRFS_CRC32_SIZE); - return 0; -} - -int btrfs_csum_super(struct btrfs_root *root, struct btrfs_super_block *super) -{ - u32 crc = ~(u32)0; - char block[512]; - size_t len = 512 - BTRFS_CSUM_SIZE; - - memset(block, 0, 512); - memcpy(block, super, sizeof(*super)); - - crc = crc32c(crc, block + BTRFS_CSUM_SIZE, len); - crc = ~cpu_to_le32(crc); - memcpy(super->csum, &crc, BTRFS_CRC32_SIZE); - return 0; -} - -int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf) -{ - int ret; - - if (buf->bytenr != btrfs_header_bytenr(&buf->node.header)) + if (!btrfs_buffer_uptodate(eb)) BUG(); - btrfs_map_bh_to_logical(root, buf, buf->bytenr); - if (check_tree_block(root, buf)) - BUG(); - - btrfs_csum_node(root, &buf->node); - - ret = pwrite(buf->fd, &buf->node, buf->size, - buf->dev_bytenr); - if (ret != buf->size) - return ret; - return 0; -} - -static int __commit_transaction(struct btrfs_trans_handle *trans, struct - btrfs_root *root) -{ - struct btrfs_buffer *b; - int ret = 0; - int wret; - while(!list_empty(&root->fs_info->trans)) { - b = list_entry(root->fs_info->trans.next, struct btrfs_buffer, - dirty); - list_del_init(&b->dirty); - wret = write_tree_block(trans, root, b); - if (wret) - ret = wret; - btrfs_block_release(root, b); - } - return ret; + btrfs_map_bh_to_logical(root, eb, eb->start); + csum_tree_block(root, eb, 0); + return write_extent_to_disk(eb); +} + +static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, + u32 stripesize, struct btrfs_root *root, + struct btrfs_fs_info *fs_info, u64 objectid) +{ + root->node = NULL; + root->commit_root = NULL; + root->sectorsize = sectorsize; + root->nodesize = nodesize; + root->leafsize = leafsize; + root->stripesize = stripesize; + root->ref_cows = 0; + root->fs_info = fs_info; + root->objectid = objectid; + root->last_trans = 0; + root->highest_inode = 0; + root->last_inode_alloc = 0; + memset(&root->root_key, 0, sizeof(root->root_key)); + memset(&root->root_item, 0, sizeof(root->root_item)); + root->root_key.objectid = objectid; + return 0; } static int commit_tree_roots(struct btrfs_trans_handle *trans, @@ -253,12 +168,12 @@ static int commit_tree_roots(struct btrf btrfs_write_dirty_block_groups(trans, fs_info->extent_root); while(1) { old_extent_bytenr = btrfs_root_bytenr(&extent_root->root_item); - if (old_extent_bytenr == extent_root->node->bytenr) + if (old_extent_bytenr == extent_root->node->start) break; btrfs_set_root_bytenr(&extent_root->root_item, - extent_root->node->bytenr); + extent_root->node->start); extent_root->root_item.level = - btrfs_header_level(&extent_root->node->node.header); + btrfs_header_level(extent_root->node); ret = btrfs_update_root(trans, tree_root, &extent_root->root_key, &extent_root->root_item); @@ -268,96 +183,187 @@ static int commit_tree_roots(struct btrf return 0; } -int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct - btrfs_root *root, struct btrfs_super_block *s) +static int __commit_transaction(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + u64 start; + u64 end; + struct extent_buffer *eb; + struct extent_map_tree *tree = &root->fs_info->extent_cache; + int ret; + + while(1) { + ret = find_first_extent_bit(tree, 0, &start, &end, + EXTENT_DIRTY); + if (ret) + break; + while(start <= end) { + eb = find_first_extent_buffer(tree, start); + BUG_ON(!eb || eb->start != start); + ret = write_tree_block(trans, root, eb); + BUG_ON(ret); + start += eb->len; + clear_extent_buffer_dirty(eb); + free_extent_buffer(eb); + } + } + return 0; +} + +int btrfs_commit_transaction(struct btrfs_trans_handle *trans, + struct btrfs_root *root) { int ret = 0; - struct btrfs_buffer *snap = root->commit_root; - struct btrfs_key snap_key; + struct btrfs_root *new_root = NULL; + struct btrfs_fs_info *fs_info = root->fs_info; if (root->commit_root == root->node) - return 0; - - memcpy(&snap_key, &root->root_key, sizeof(snap_key)); + goto commit_tree; + + new_root = malloc(sizeof(*new_root)); + if (!new_root) + return -ENOMEM; + memcpy(new_root, root, sizeof(*new_root)); + new_root->node = root->commit_root; + root->commit_root = NULL; + root->root_key.offset = trans->transid; - - btrfs_set_root_bytenr(&root->root_item, root->node->bytenr); - root->root_item.level = - btrfs_header_level(&root->node->node.header); - ret = btrfs_insert_root(trans, root->fs_info->tree_root, + btrfs_set_root_bytenr(&root->root_item, root->node->start); + root->root_item.level = btrfs_header_level(root->node); + ret = btrfs_insert_root(trans, fs_info->tree_root, &root->root_key, &root->root_item); BUG_ON(ret); - ret = commit_tree_roots(trans, root->fs_info); - BUG_ON(ret); - + btrfs_set_root_refs(&new_root->root_item, 0); + ret = btrfs_update_root(trans, root->fs_info->tree_root, + &new_root->root_key, &new_root->root_item); + BUG_ON(ret); + + ret = commit_tree_roots(trans, fs_info); + BUG_ON(ret); ret = __commit_transaction(trans, root); BUG_ON(ret); - - write_ctree_super(trans, root, s); - btrfs_finish_extent_commit(trans, root->fs_info->extent_root); - btrfs_finish_extent_commit(trans, root->fs_info->tree_root); - - root->commit_root = root->node; - root->node->count++; - ret = btrfs_drop_snapshot(trans, root, snap); - BUG_ON(ret); - ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key); - BUG_ON(ret); + write_ctree_super(trans, root); + btrfs_finish_extent_commit(trans, fs_info->extent_root, + &fs_info->pinned_extents); btrfs_free_transaction(root, trans); - return ret; -} - -static int __setup_root(struct btrfs_super_block *super, - struct btrfs_root *root, - struct btrfs_fs_info *fs_info, - u64 objectid, int fp) -{ - root->node = NULL; + fs_info->running_transaction = NULL; + + trans = btrfs_start_transaction(root, 1); + ret = btrfs_drop_snapshot(trans, new_root); + BUG_ON(ret); + ret = btrfs_del_root(trans, fs_info->tree_root, &new_root->root_key); + BUG_ON(ret); +commit_tree: + ret = commit_tree_roots(trans, fs_info); + BUG_ON(ret); + ret = __commit_transaction(trans, root); + BUG_ON(ret); + write_ctree_super(trans, root); + btrfs_finish_extent_commit(trans, fs_info->extent_root, + &fs_info->pinned_extents); + btrfs_free_transaction(root, trans); + free_extent_buffer(root->commit_root); root->commit_root = NULL; - root->sectorsize = btrfs_super_sectorsize(super); - root->nodesize = btrfs_super_nodesize(super); - root->leafsize = btrfs_super_leafsize(super); - root->stripesize = btrfs_super_stripesize(super); - root->ref_cows = 0; - root->fs_info = fs_info; - memset(&root->root_key, 0, sizeof(root->root_key)); - memset(&root->root_item, 0, sizeof(root->root_item)); - root->root_key.objectid = objectid; - return 0; -} - -struct btrfs_buffer *read_root_block(struct btrfs_root *root, u64 bytenr, - u8 level) -{ - struct btrfs_buffer *node; - u32 size = btrfs_level_size(root, level); - - node = read_tree_block(root, bytenr, size); - BUG_ON(!node); - return node; -} - -static int find_and_setup_root(struct btrfs_super_block *super, - struct btrfs_root *tree_root, + fs_info->running_transaction = NULL; + if (new_root) { + free_extent_buffer(new_root->node); + free(new_root); + } + return 0; +} + +static int find_and_setup_root(struct btrfs_root *tree_root, struct btrfs_fs_info *fs_info, - u64 objectid, - struct btrfs_root *root, int fp) -{ - int ret; - - __setup_root(super, root, fs_info, objectid, fp); + u64 objectid, struct btrfs_root *root) +{ + int ret; + u32 blocksize; + + __setup_root(tree_root->nodesize, tree_root->leafsize, + tree_root->sectorsize, tree_root->stripesize, + root, fs_info, objectid); ret = btrfs_find_last_root(tree_root, objectid, &root->root_item, &root->root_key); BUG_ON(ret); - root->node = read_root_block(root, - btrfs_root_bytenr(&root->root_item), - root->root_item.level); + + blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); + root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), + blocksize); BUG_ON(!root->node); return 0; } -struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super) +int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) +{ + if (root->node) + free_extent_buffer(root->node); + if (root->commit_root) + free_extent_buffer(root->commit_root); + + free(root); + return 0; +} + +struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, + struct btrfs_key *location) +{ + struct btrfs_root *root; + struct btrfs_root *tree_root = fs_info->tree_root; + struct btrfs_path *path; + struct extent_buffer *l; + u32 blocksize; + int ret = 0; + + root = malloc(sizeof(*root)); + if (!root) + return ERR_PTR(-ENOMEM); + memset(root, 0, sizeof(*root)); + if (location->offset == (u64)-1) { + ret = find_and_setup_root(tree_root, fs_info, + location->objectid, root); + if (ret) { + free(root); + return ERR_PTR(ret); + } + goto insert; + } + + __setup_root(tree_root->nodesize, tree_root->leafsize, + tree_root->sectorsize, tree_root->stripesize, + root, fs_info, location->objectid); + + path = btrfs_alloc_path(); + BUG_ON(!path); + ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); + if (ret != 0) { + if (ret > 0) + ret = -ENOENT; + goto out; + } + l = path->nodes[0]; + read_extent_buffer(l, &root->root_item, + btrfs_item_ptr_offset(l, path->slots[0]), + sizeof(root->root_item)); + memcpy(&root->root_key, location, sizeof(*location)); + ret = 0; +out: + btrfs_release_path(root, path); + btrfs_free_path(path); + if (ret) { + free(root); + return ERR_PTR(ret); + } + blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); + root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), + blocksize); + BUG_ON(!root->node); +insert: + root->ref_cows = 1; + return root; +} + +struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr) { int fp; @@ -365,145 +371,165 @@ struct btrfs_root *open_ctree(char *file if (fp < 0) { return NULL; } - return open_ctree_fd(fp, super); -} - -struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super) -{ + return open_ctree_fd(fp, sb_bytenr); +} + +struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr) +{ + u32 sectorsize; + u32 nodesize; + u32 leafsize; + u32 blocksize; + u32 stripesize; struct btrfs_root *root = malloc(sizeof(struct btrfs_root)); + struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root)); struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root)); - struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root)); struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info)); int ret; - - INIT_LIST_HEAD(&fs_info->trans); - INIT_LIST_HEAD(&fs_info->cache); - cache_tree_init(&fs_info->extent_cache); - cache_tree_init(&fs_info->pending_tree); - cache_tree_init(&fs_info->pinned_tree); - cache_tree_init(&fs_info->del_pending); - cache_tree_init(&fs_info->block_group_cache); - fs_info->cache_size = 0; + struct btrfs_super_block *disk_super; + + if (sb_bytenr == 0) + sb_bytenr = BTRFS_SUPER_INFO_OFFSET; + fs_info->fp = fp; fs_info->running_transaction = NULL; fs_info->fs_root = root; fs_info->tree_root = tree_root; fs_info->extent_root = extent_root; - fs_info->last_inode_alloc = 0; - fs_info->last_inode_alloc_dirid = 0; - fs_info->disk_super = super; - memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); - - ret = pread(fp, super, sizeof(struct btrfs_super_block), - BTRFS_SUPER_INFO_OFFSET); - if (ret == 0 || btrfs_super_root(super) == 0) { - BUG(); - return NULL; - } - BUG_ON(ret < 0); - - __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp); - tree_root->node = read_root_block(tree_root, btrfs_super_root(super), - btrfs_super_root_level(super)); + + extent_map_tree_init(&fs_info->extent_cache); + extent_map_tree_init(&fs_info->free_space_cache); + extent_map_tree_init(&fs_info->pending_tree); + extent_map_tree_init(&fs_info->pinned_extents); + extent_map_tree_init(&fs_info->del_pending); + extent_map_tree_init(&fs_info->block_group_cache); + + mutex_init(&fs_info->fs_mutex); + + __setup_root(512, 512, 512, 512, tree_root, + fs_info, BTRFS_ROOT_TREE_OBJECTID); + + fs_info->sb_buffer = read_tree_block(tree_root, sb_bytenr, 512); + BUG_ON(!fs_info->sb_buffer); + read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, + sizeof(fs_info->super_copy)); + read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, + (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), + BTRFS_FSID_SIZE); + disk_super = &fs_info->super_copy; + + nodesize = btrfs_super_nodesize(disk_super); + leafsize = btrfs_super_leafsize(disk_super); + sectorsize = btrfs_super_sectorsize(disk_super); + stripesize = btrfs_super_stripesize(disk_super); + tree_root->nodesize = nodesize; + tree_root->leafsize = leafsize; + tree_root->sectorsize = sectorsize; + tree_root->stripesize = stripesize; + + blocksize = btrfs_level_size(tree_root, + btrfs_super_root_level(disk_super)); + tree_root->node = read_tree_block(tree_root, + btrfs_super_root(disk_super), + blocksize); BUG_ON(!tree_root->node); - - ret = find_and_setup_root(super, tree_root, fs_info, - BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp); - BUG_ON(ret); - - ret = find_and_setup_root(super, tree_root, fs_info, - BTRFS_FS_TREE_OBJECTID, root, fp); - BUG_ON(ret); - - root->commit_root = root->node; - root->node->count++; + ret = find_and_setup_root(tree_root, fs_info, + BTRFS_EXTENT_TREE_OBJECTID, extent_root); + BUG_ON(ret); + ret = find_and_setup_root(tree_root, fs_info, + BTRFS_FS_TREE_OBJECTID, root); + BUG_ON(ret); root->ref_cows = 1; - root->fs_info->generation = btrfs_super_generation(super) + 1; + fs_info->generation = btrfs_super_generation(disk_super) + 1; btrfs_read_block_groups(root); return root; } -int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_super_block *s) -{ - int ret; - btrfs_set_super_root(s, root->fs_info->tree_root->node->bytenr); - btrfs_set_super_generation(s, trans->transid); - btrfs_set_super_root_level(s, - btrfs_header_level(&root->fs_info->tree_root->node->node.header)); - btrfs_csum_super(root, s); - - ret = pwrite(root->fs_info->fp, s, sizeof(*s), - BTRFS_SUPER_INFO_OFFSET); - if (ret != sizeof(*s)) { +int write_ctree_super(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + int ret; + struct btrfs_root *tree_root = root->fs_info->tree_root; + btrfs_set_super_generation(&root->fs_info->super_copy, + trans->transid); + btrfs_set_super_root(&root->fs_info->super_copy, + tree_root->node->start); + btrfs_set_super_root_level(&root->fs_info->super_copy, + btrfs_header_level(tree_root->node)); + write_extent_buffer(root->fs_info->sb_buffer, + &root->fs_info->super_copy, 0, + sizeof(root->fs_info->super_copy)); + ret = write_tree_block(trans, root, root->fs_info->sb_buffer); + if (ret) fprintf(stderr, "failed to write new super block err %d\n", ret); - return ret; - } - return 0; -} - -static int drop_cache(struct btrfs_root *root) -{ - while(!list_empty(&root->fs_info->cache)) { - struct btrfs_buffer *b = list_entry(root->fs_info->cache.next, - struct btrfs_buffer, - cache); - list_del_init(&b->cache); - btrfs_block_release(root, b); - } - return 0; -} - -int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s) + return ret; +} + +int close_ctree(struct btrfs_root *root) { int ret; struct btrfs_trans_handle *trans; + struct btrfs_fs_info *fs_info = root->fs_info; + trans = btrfs_start_transaction(root, 1); - btrfs_commit_transaction(trans, root, s); + btrfs_commit_transaction(trans, root); trans = btrfs_start_transaction(root, 1); ret = commit_tree_roots(trans, root->fs_info); BUG_ON(ret); ret = __commit_transaction(trans, root); BUG_ON(ret); - write_ctree_super(trans, root, s); + write_ctree_super(trans, root); btrfs_free_transaction(root, trans); - drop_cache(root); - BUG_ON(!list_empty(&root->fs_info->trans)); btrfs_free_block_groups(root->fs_info); close(root->fs_info->fp); if (root->node) - btrfs_block_release(root, root->node); + free_extent_buffer(root->node); if (root->fs_info->extent_root->node) - btrfs_block_release(root->fs_info->extent_root, - root->fs_info->extent_root->node); + free_extent_buffer(root->fs_info->extent_root->node); if (root->fs_info->tree_root->node) - btrfs_block_release(root->fs_info->tree_root, - root->fs_info->tree_root->node); - btrfs_block_release(root, root->commit_root); - free(root); - printf("on close %llu blocks are allocated\n", - (unsigned long long)allocated_bytes); - return 0; -} - -void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf) -{ - buf->count--; - if (buf->count < 0) - BUG(); - if (buf->count == 0) { - BUG_ON(!list_empty(&buf->cache)); - BUG_ON(!list_empty(&buf->dirty)); - - remove_cache_extent(&root->fs_info->extent_cache, - &buf->cache_node); - BUG_ON(allocated_bytes == 0); - allocated_bytes -= buf->size; - BUG_ON(root->fs_info->cache_size == 0); - root->fs_info->cache_size -= buf->size; - - memset(buf, 0, sizeof(*buf)); - free(buf); - } -} - + free_extent_buffer(root->fs_info->tree_root->node); + free_extent_buffer(root->commit_root); + free_extent_buffer(root->fs_info->sb_buffer); + + extent_map_tree_cleanup(&fs_info->extent_cache); + extent_map_tree_cleanup(&fs_info->free_space_cache); + extent_map_tree_cleanup(&fs_info->pending_tree); + extent_map_tree_cleanup(&fs_info->pinned_extents); + extent_map_tree_cleanup(&fs_info->del_pending); + extent_map_tree_cleanup(&fs_info->block_group_cache); + + free(fs_info->tree_root); + free(fs_info->extent_root); + free(fs_info->fs_root); + free(fs_info); + + return 0; +} + +int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, + struct extent_buffer *eb) +{ + return clear_extent_buffer_dirty(eb); +} + +int wait_on_tree_block_writeback(struct btrfs_root *root, + struct extent_buffer *eb) +{ + return 0; +} + +void btrfs_mark_buffer_dirty(struct extent_buffer *eb) +{ + set_extent_buffer_dirty(eb); +} + +int btrfs_buffer_uptodate(struct extent_buffer *eb) +{ + return extent_buffer_uptodate(eb); +} + +int btrfs_set_buffer_uptodate(struct extent_buffer *eb) +{ + return set_extent_buffer_uptodate(eb); +} + diff -r 59fc7ac63e46 disk-io.h --- a/disk-io.h Fri Dec 21 16:25:35 2007 -0500 +++ b/disk-io.h Fri Jan 04 19:19:50 2008 +0800 @@ -18,46 +18,36 @@ #ifndef __DISKIO__ #define __DISKIO__ -#include "extent-cache.h" -#include "list.h" -struct btrfs_buffer { - struct cache_extent cache_node; - u64 bytenr; - u64 dev_bytenr; - u32 size; - int count; - int fd; - struct list_head dirty; - struct list_head cache; - union { - struct btrfs_node node; - struct btrfs_leaf leaf; - }; -}; - -struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, - u32 blocksize); -struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 bytenr, - u32 blocksize); -int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf); -int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf); -int clean_tree_block(struct btrfs_trans_handle *trans, - struct btrfs_root *root, struct btrfs_buffer *buf); -int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_super_block *s); -struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *s); -struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super); -int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s); -void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf); -int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_super_block *s); -int btrfs_map_bh_to_logical(struct btrfs_root *root, struct btrfs_buffer *bh, - u64 logical); -int btrfs_csum_super(struct btrfs_root *root, struct btrfs_super_block *super); -int btrfs_csum_node(struct btrfs_root *root, struct btrfs_node *node); #define BTRFS_SUPER_INFO_OFFSET (16 * 1024) +struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, + u32 blocksize); +int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize); +struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, + u64 bytenr, u32 blocksize); +int clean_tree_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct extent_buffer *buf); +struct btrfs_root *open_ctree(char *filename, u64 sb_bytenr); +struct btrfs_root *open_ctree_fd(int fp, u64 sb_bytenr); +int close_ctree(struct btrfs_root *root); +int write_ctree_super(struct btrfs_trans_handle *trans, + struct btrfs_root *root); +int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *bh, + u64 logical); +struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, + u64 bytenr, u32 blocksize); +struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, + struct btrfs_key *location); +int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root); +void btrfs_mark_buffer_dirty(struct extent_buffer *buf); +int btrfs_buffer_uptodate(struct extent_buffer *buf); +int btrfs_set_buffer_uptodate(struct extent_buffer *buf); +int wait_on_tree_block_writeback(struct btrfs_root *root, + struct extent_buffer *buf); +u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len); +void btrfs_csum_final(u32 crc, char *result); + +int btrfs_commit_transaction(struct btrfs_trans_handle *trans, + struct btrfs_root *root); #endif diff -r 59fc7ac63e46 extent-tree.c --- a/extent-tree.c Fri Dec 21 16:25:35 2007 -0500 +++ b/extent-tree.c Fri Jan 04 19:19:50 2008 +0800 @@ -26,10 +26,337 @@ #include "transaction.h" #include "crc32c.h" +#define BLOCK_GROUP_DATA EXTENT_WRITEBACK +#define BLOCK_GROUP_METADATA EXTENT_UPTODATE +#define BLOCK_GROUP_DIRTY EXTENT_DIRTY + static int finish_current_insert(struct btrfs_trans_handle *trans, struct btrfs_root *extent_root); -static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root - *extent_root); +static int del_pending_extents(struct btrfs_trans_handle *trans, struct + btrfs_root *extent_root); + +static int cache_block_group(struct btrfs_root *root, + struct btrfs_block_group_cache *block_group) +{ + struct btrfs_path *path; + int ret; + struct btrfs_key key; + struct extent_buffer *leaf; + struct extent_map_tree *free_space_cache; + int slot; + u64 last = 0; + u64 hole_size; + u64 first_free; + int found = 0; + + if (!block_group) + return 0; + + root = root->fs_info->extent_root; + free_space_cache = &root->fs_info->free_space_cache; + + if (block_group->cached) + return 0; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + path->reada = 2; + first_free = block_group->key.objectid; + key.objectid = block_group->key.objectid; + key.offset = 0; + + btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + + if (ret < 0) + return ret; + + if (ret && path->slots[0] > 0) + path->slots[0]--; + + while(1) { + leaf = path->nodes[0]; + slot = path->slots[0]; + if (slot >= btrfs_header_nritems(leaf)) { + ret = btrfs_next_leaf(root, path); + if (ret < 0) + goto err; + if (ret == 0) { + continue; + } else { + break; + } + } + + btrfs_item_key_to_cpu(leaf, &key, slot); + if (key.objectid < block_group->key.objectid) { + if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY && + key.objectid + key.offset > first_free) + first_free = key.objectid + key.offset; + goto next; + } + + if (key.objectid >= block_group->key.objectid + + block_group->key.offset) { + break; + } + + if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { + if (!found) { + last = first_free; + found = 1; + } + if (key.objectid > last) { + hole_size = key.objectid - last; + set_extent_dirty(free_space_cache, last, + last + hole_size - 1, + GFP_NOFS); + } + last = key.objectid + key.offset; + } +next: + path->slots[0]++; + } + + if (!found) + last = first_free; + if (block_group->key.objectid + + block_group->key.offset > last) { + hole_size = block_group->key.objectid + + block_group->key.offset - last; + set_extent_dirty(free_space_cache, last, + last + hole_size - 1, GFP_NOFS); + } + block_group->cached = 1; +err: + btrfs_free_path(path); + return 0; +} + +struct btrfs_block_group_cache *btrfs_lookup_block_group(struct + btrfs_fs_info *info, + u64 bytenr) +{ + struct extent_map_tree *block_group_cache; + struct btrfs_block_group_cache *block_group = NULL; + u64 ptr; + u64 start; + u64 end; + int ret; + + block_group_cache = &info->block_group_cache; + ret = find_first_extent_bit(block_group_cache, + bytenr, &start, &end, + BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA); + if (ret) { + return NULL; + } + ret = get_state_private(block_group_cache, start, &ptr); + if (ret) + return NULL; + + block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr; + if (block_group->key.objectid <= bytenr && bytenr < + block_group->key.objectid + block_group->key.offset) + return block_group; + return NULL; +} + +static u64 noinline find_search_start(struct btrfs_root *root, + struct btrfs_block_group_cache **cache_ret, + u64 search_start, int num, + int data, int full_scan) +{ + int ret; + struct btrfs_block_group_cache *cache = *cache_ret; + u64 last; + u64 start = 0; + u64 end = 0; + u64 cache_miss = 0; + int wrapped = 0; + + if (!cache) { + goto out; + } +again: + ret = cache_block_group(root, cache); + if (ret) + goto out; + + last = max(search_start, cache->key.objectid); + + while(1) { + ret = find_first_extent_bit(&root->fs_info->free_space_cache, + last, &start, &end, EXTENT_DIRTY); + if (ret) { + if (!cache_miss) + cache_miss = last; + goto new_group; + } + + start = max(last, start); + last = end + 1; + if (last - start < num) { + if (last == cache->key.objectid + cache->key.offset) + cache_miss = start; + continue; + } + if (data != BTRFS_BLOCK_GROUP_MIXED && + start + num > cache->key.objectid + cache->key.offset) + goto new_group; + return start; + } +out: + cache = btrfs_lookup_block_group(root->fs_info, search_start); + if (!cache) { + printk("Unable to find block group for %Lu\n", + search_start); + WARN_ON(1); + return search_start; + } + return search_start; + +new_group: + last = cache->key.objectid + cache->key.offset; +wrapped: + cache = btrfs_lookup_block_group(root->fs_info, last); + if (!cache) { +no_cache: + if (!wrapped) { + wrapped = 1; + last = search_start; + data = BTRFS_BLOCK_GROUP_MIXED; + goto wrapped; + } + goto out; + } + if (cache_miss && !cache->cached) { + cache_block_group(root, cache); + last = cache_miss; + cache = btrfs_lookup_block_group(root->fs_info, last); + } + cache = btrfs_find_block_group(root, cache, last, data, 0); + if (!cache) + goto no_cache; + *cache_ret = cache; + cache_miss = 0; + goto again; +} + +static u64 div_factor(u64 num, int factor) +{ + if (factor == 10) + return num; + num *= factor; + num /= 10; + return num; +} + +struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, + struct btrfs_block_group_cache + *hint, u64 search_start, + int data, int owner) +{ + struct btrfs_block_group_cache *cache; + struct extent_map_tree *block_group_cache; + struct btrfs_block_group_cache *found_group = NULL; + struct btrfs_fs_info *info = root->fs_info; + u64 used; + u64 last = 0; + u64 hint_last; + u64 start; + u64 end; + u64 free_check; + u64 ptr; + int bit; + int ret; + int full_search = 0; + int factor = 8; + int data_swap = 0; + + block_group_cache = &info->block_group_cache; + + if (!owner) + factor = 8; + + if (data == BTRFS_BLOCK_GROUP_MIXED) { + bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; + factor = 10; + } else if (data) + bit = BLOCK_GROUP_DATA; + else + bit = BLOCK_GROUP_METADATA; + + if (search_start) { + struct btrfs_block_group_cache *shint; + shint = btrfs_lookup_block_group(info, search_start); + if (shint && (shint->data == data || + shint->data == BTRFS_BLOCK_GROUP_MIXED)) { + used = btrfs_block_group_used(&shint->item); + if (used + shint->pinned < + div_factor(shint->key.offset, factor)) { + return shint; + } + } + } + if (hint && (hint->data == data || + hint->data == BTRFS_BLOCK_GROUP_MIXED)) { + used = btrfs_block_group_used(&hint->item); + if (used + hint->pinned < + div_factor(hint->key.offset, factor)) { + return hint; + } + last = hint->key.objectid + hint->key.offset; + hint_last = last; + } else { + if (hint) + hint_last = max(hint->key.objectid, search_start); + else + hint_last = search_start; + + last = hint_last; + } +again: + while(1) { + ret = find_first_extent_bit(block_group_cache, last, + &start, &end, bit); + if (ret) + break; + + ret = get_state_private(block_group_cache, start, &ptr); + if (ret) + break; + + cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; + last = cache->key.objectid + cache->key.offset; + used = btrfs_block_group_used(&cache->item); + + if (full_search) + free_check = cache->key.offset; + else + free_check = div_factor(cache->key.offset, factor); + if (used + cache->pinned < free_check) { + found_group = cache; + goto found; + } + cond_resched(); + } + if (!full_search) { + last = search_start; + full_search = 1; + goto again; + } + if (!data_swap) { + data_swap = 1; + bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; + last = search_start; + goto again; + } +found: + return found_group; +} static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, u64 owner, u64 owner_offset) @@ -52,32 +379,42 @@ static u64 hash_extent_ref(u64 root_obje return ((u64)high_crc << 32) | (u64)low_crc; } -static int match_extent_ref(struct btrfs_extent_ref *disk_ref, +static int match_extent_ref(struct extent_buffer *leaf, + struct btrfs_extent_ref *disk_ref, struct btrfs_extent_ref *cpu_ref) { - int ret = memcmp(cpu_ref, disk_ref, sizeof(*cpu_ref)); + int ret; + int len; + + if (cpu_ref->objectid) + len = sizeof(*cpu_ref); + else + len = 2 * sizeof(u64); + ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, + len); return ret == 0; } -static int lookup_extent_backref(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_path *path, u64 bytenr, - u64 root_objectid, u64 ref_generation, - u64 owner, u64 owner_offset, int del) +static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 bytenr, + u64 root_objectid, + u64 ref_generation, u64 owner, + u64 owner_offset, int del) { u64 hash; struct btrfs_key key; struct btrfs_key found_key; struct btrfs_extent_ref ref; - struct btrfs_buffer *leaf; + struct extent_buffer *leaf; struct btrfs_extent_ref *disk_ref; int ret; int ret2; - btrfs_set_ref_root(&ref, root_objectid); - btrfs_set_ref_generation(&ref, ref_generation); - btrfs_set_ref_objectid(&ref, owner); - btrfs_set_ref_offset(&ref, owner_offset); + btrfs_set_stack_ref_root(&ref, root_objectid); + btrfs_set_stack_ref_generation(&ref, ref_generation); + btrfs_set_stack_ref_objectid(&ref, owner); + btrfs_set_stack_ref_offset(&ref, owner_offset); hash = hash_extent_ref(root_objectid, ref_generation, owner, owner_offset); @@ -92,15 +429,14 @@ static int lookup_extent_backref(struct goto out; leaf = path->nodes[0]; if (ret != 0) { - u32 nritems = btrfs_header_nritems(&leaf->node.header); + u32 nritems = btrfs_header_nritems(leaf); if (path->slots[0] >= nritems) { ret2 = btrfs_next_leaf(root, path); if (ret2) goto out; leaf = path->nodes[0]; } - btrfs_disk_key_to_cpu(&found_key, - &leaf->leaf.items[path->slots[0]].key); + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); if (found_key.objectid != bytenr || found_key.type != BTRFS_EXTENT_REF_KEY) goto out; @@ -110,15 +446,14 @@ static int lookup_extent_backref(struct continue; } } - disk_ref = btrfs_item_ptr(&path->nodes[0]->leaf, + disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_ref); - if (match_extent_ref(disk_ref, &ref)) { + if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { ret = 0; goto out; } - btrfs_disk_key_to_cpu(&found_key, - &leaf->leaf.items[path->slots[0]].key); + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); key.offset = found_key.offset + 1; btrfs_release_path(root, path); } @@ -126,7 +461,95 @@ out: return ret; } -static int insert_extent_backref(struct btrfs_trans_handle *trans, +/* + * Back reference rules. Back refs have three main goals: + * + * 1) differentiate between all holders of references to an extent so that + * when a reference is dropped we can make sure it was a valid reference + * before freeing the extent. + * + * 2) Provide enough information to quickly find the holders of an extent + * if we notice a given block is corrupted or bad. + * + * 3) Make it easy to migrate blocks for FS shrinking or storage pool + * maintenance. This is actually the same as #2, but with a slightly + * different use case. + * + * File extents can be referenced by: + * + * - multiple snapshots, subvolumes, or different generations in one subvol + * - different files inside a single subvolume (in theory, not implemented yet) + * - different offsets inside a file (bookend extents in file.c) + * + * The extent ref structure has fields for: + * + * - Objectid of the subvolume root + * - Generation number of the tree holding the reference + * - objectid of the file holding the reference + * - offset in the file corresponding to the key holding the reference + * + * When a file extent is allocated the fields are filled in: + * (root_key.objectid, trans->transid, inode objectid, offset in file) + * + * When a leaf is cow'd new references are added for every file extent found + * in the leaf. It looks the same as the create case, but trans->transid + * will be different when the block is cow'd. + * + * (root_key.objectid, trans->transid, inode objectid, offset in file) + * + * When a file extent is removed either during snapshot deletion or file + * truncation, the corresponding back reference is found + * by searching for: + * + * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), + * inode objectid, offset in file) + * + * Btree extents can be referenced by: + * + * - Different subvolumes + * - Different generations of the same subvolume + * + * Storing sufficient information for a full reverse mapping of a btree + * block would require storing the lowest key of the block in the backref, + * and it would require updating that lowest key either before write out or + * every time it changed. Instead, the objectid of the lowest key is stored + * along with the level of the tree block. This provides a hint + * about where in the btree the block can be found. Searches through the + * btree only need to look for a pointer to that block, so they stop one + * level higher than the level recorded in the backref. + * + * Some btrees do not do reference counting on their extents. These + * include the extent tree and the tree of tree roots. Backrefs for these + * trees always have a generation of zero. + * + * When a tree block is created, back references are inserted: + * + * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) + * + * When a tree block is cow'd in a reference counted root, + * new back references are added for all the blocks it points to. + * These are of the form (trans->transid will have increased since creation): + * + * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) + * + * Because the lowest_key_objectid and the level are just hints + * they are not used when backrefs are deleted. When a backref is deleted: + * + * if backref was for a tree root: + * root_objectid = root->root_key.objectid + * else + * root_objectid = btrfs_header_owner(parent) + * + * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) + * + * Back Reference Key hashing: + * + * Back references have four fields, each 64 bits long. Unfortunately, + * This is hashed into a single 64 bit number and placed into the key offset. + * The key objectid corresponds to the first byte in the extent, and the + * key type is set to BTRFS_EXTENT_REF_KEY + */ +int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, u64 bytenr, u64 root_objectid, u64 ref_generation, @@ -138,10 +561,10 @@ static int insert_extent_backref(struct struct btrfs_extent_ref *disk_ref; int ret; - btrfs_set_ref_root(&ref, root_objectid); - btrfs_set_ref_generation(&ref, ref_generation); - btrfs_set_ref_objectid(&ref, owner); - btrfs_set_ref_offset(&ref, owner_offset); + btrfs_set_stack_ref_root(&ref, root_objectid); + btrfs_set_stack_ref_generation(&ref, ref_generation); + btrfs_set_stack_ref_objectid(&ref, owner); + btrfs_set_stack_ref_offset(&ref, owner_offset); hash = hash_extent_ref(root_objectid, ref_generation, owner, owner_offset); @@ -151,121 +574,317 @@ static int insert_extent_backref(struct ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); while (ret == -EEXIST) { - disk_ref = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0], + disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_ref); - if (match_extent_ref(disk_ref, &ref)) + if (match_extent_ref(path->nodes[0], disk_ref, &ref)) goto out; key.offset++; + btrfs_release_path(root, path); ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); } if (ret) goto out; - disk_ref = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0], + disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_ref); - memcpy(disk_ref, &ref, sizeof(ref)); - dirty_tree_block(trans, root, path->nodes[0]); + write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, + sizeof(ref)); + btrfs_mark_buffer_dirty(path->nodes[0]); out: btrfs_release_path(root, path); return ret; } -static int inc_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root - *root, u64 bytenr, u32 blocksize, - u64 root_objectid, u64 ref_generation, - u64 owner, u64 owner_offset) -{ - struct btrfs_path path; +int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u64 bytenr, u64 num_bytes, + u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset) +{ + struct btrfs_path *path; int ret; struct btrfs_key key; - struct btrfs_leaf *l; + struct extent_buffer *l; struct btrfs_extent_item *item; u32 refs; - btrfs_init_path(&path); + WARN_ON(num_bytes < root->sectorsize); + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + key.objectid = bytenr; btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); - key.offset = blocksize; - ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path, + key.offset = num_bytes; + ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, 0, 1); - if (ret != 0) + if (ret < 0) + return ret; + if (ret != 0) { BUG(); + } BUG_ON(ret != 0); - l = &path.nodes[0]->leaf; - item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item); - refs = btrfs_extent_refs(item); - btrfs_set_extent_refs(item, refs + 1); - - BUG_ON(list_empty(&path.nodes[0]->dirty)); - btrfs_release_path(root->fs_info->extent_root, &path); - - ret = insert_extent_backref(trans, root->fs_info->extent_root, &path, - bytenr, root_objectid, ref_generation, - owner, owner_offset); + l = path->nodes[0]; + item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); + refs = btrfs_extent_refs(l, item); + btrfs_set_extent_refs(l, item, refs + 1); + btrfs_mark_buffer_dirty(path->nodes[0]); + + btrfs_release_path(root->fs_info->extent_root, path); + + ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, + path, bytenr, root_objectid, + ref_generation, owner, owner_offset); BUG_ON(ret); - finish_current_insert(trans, root->fs_info->extent_root); - run_pending(trans, root->fs_info->extent_root); + del_pending_extents(trans, root->fs_info->extent_root); + + btrfs_free_path(path); return 0; } -static int lookup_block_ref(struct btrfs_trans_handle *trans, struct btrfs_root - *root, u64 bytenr, u32 blocksize, u32 *refs) -{ - struct btrfs_path path; +int btrfs_extent_post_op(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + finish_current_insert(trans, root->fs_info->extent_root); + del_pending_extents(trans, root->fs_info->extent_root); + return 0; +} + +static int lookup_extent_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 bytenr, + u64 num_bytes, u32 *refs) +{ + struct btrfs_path *path; int ret; struct btrfs_key key; - struct btrfs_leaf *l; + struct extent_buffer *l; struct btrfs_extent_item *item; - btrfs_init_path(&path); - + WARN_ON(num_bytes < root->sectorsize); + path = btrfs_alloc_path(); key.objectid = bytenr; - key.offset = blocksize; + key.offset = num_bytes; btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); - ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, &path, + ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, 0, 0); - if (ret != 0) + if (ret < 0) + goto out; + if (ret != 0) { + btrfs_print_leaf(root, path->nodes[0]); + printk("failed to find block number %Lu\n", bytenr); BUG(); - l = &path.nodes[0]->leaf; - item = btrfs_item_ptr(l, path.slots[0], struct btrfs_extent_item); - *refs = btrfs_extent_refs(item); - btrfs_release_path(root->fs_info->extent_root, &path); + } + l = path->nodes[0]; + item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); + *refs = btrfs_extent_refs(l, item); +out: + btrfs_free_path(path); return 0; } +u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, + struct btrfs_path *count_path, + u64 first_extent) +{ + struct btrfs_root *extent_root = root->fs_info->extent_root; + struct btrfs_path *path; + u64 bytenr; + u64 found_objectid; + u64 root_objectid = root->root_key.objectid; + u32 total_count = 0; + u32 cur_count; + u32 refs; + u32 nritems; + int ret; + struct btrfs_key key; + struct btrfs_key found_key; + struct extent_buffer *l; + struct btrfs_extent_item *item; + struct btrfs_extent_ref *ref_item; + int level = -1; + + path = btrfs_alloc_path(); +again: + if (level == -1) + bytenr = first_extent; + else + bytenr = count_path->nodes[level]->start; + + cur_count = 0; + key.objectid = bytenr; + key.offset = 0; + + btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); + ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); + if (ret < 0) + goto out; + BUG_ON(ret == 0); + + l = path->nodes[0]; + btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); + + if (found_key.objectid != bytenr || + found_key.type != BTRFS_EXTENT_ITEM_KEY) { + goto out; + } + + item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); + refs = btrfs_extent_refs(l, item); + while (1) { + nritems = btrfs_header_nritems(l); + if (path->slots[0] >= nritems) { + ret = btrfs_next_leaf(extent_root, path); + if (ret == 0) + continue; + break; + } + btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); + if (found_key.objectid != bytenr) + break; + if (found_key.type != BTRFS_EXTENT_REF_KEY) { + path->slots[0]++; + continue; + } + + cur_count++; + ref_item = btrfs_item_ptr(l, path->slots[0], + struct btrfs_extent_ref); + found_objectid = btrfs_ref_root(l, ref_item); + + if (found_objectid != root_objectid) { + total_count = 2; + goto out; + } + total_count = 1; + path->slots[0]++; + } + if (cur_count == 0) { + total_count = 0; + goto out; + } + if (level >= 0 && root->node == count_path->nodes[level]) + goto out; + level++; + btrfs_release_path(root, path); + goto again; + +out: + btrfs_free_path(path); + return total_count; + +} + +int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 owner_objectid) +{ + u64 generation; + u64 key_objectid; + u64 level; + u32 nritems; + struct btrfs_disk_key disk_key; + + level = btrfs_header_level(root->node); + generation = trans->transid; + nritems = btrfs_header_nritems(root->node); + if (nritems > 0) { + if (level == 0) + btrfs_item_key(root->node, &disk_key, 0); + else + btrfs_node_key(root->node, &disk_key, 0); + key_objectid = btrfs_disk_key_objectid(&disk_key); + } else { + key_objectid = 0; + } + return btrfs_inc_extent_ref(trans, root, root->node->start, + root->node->len, owner_objectid, + generation, level, key_objectid); +} + int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, - struct btrfs_buffer *buf) + struct extent_buffer *buf) { u64 bytenr; - u32 blocksize; + u32 nritems; + struct btrfs_key key; + struct btrfs_file_extent_item *fi; int i; int level; + int ret; + int faili; if (!root->ref_cows) return 0; - level = btrfs_header_level(&buf->node.header) - 1; - blocksize = btrfs_level_size(root, level); - - if (btrfs_is_leaf(&buf->node)) - return 0; - - for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) { - bytenr = btrfs_node_blockptr(&buf->node, i); - inc_block_ref(trans, root, bytenr, blocksize, - root->root_key.objectid, trans->transid, 0, 0); - } - + level = btrfs_header_level(buf); + nritems = btrfs_header_nritems(buf); + for (i = 0; i < nritems; i++) { + if (level == 0) { + u64 disk_bytenr; + btrfs_item_key_to_cpu(buf, &key, i); + if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) + continue; + fi = btrfs_item_ptr(buf, i, + struct btrfs_file_extent_item); + if (btrfs_file_extent_type(buf, fi) == + BTRFS_FILE_EXTENT_INLINE) + continue; + disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); + if (disk_bytenr == 0) + continue; + ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, + btrfs_file_extent_disk_num_bytes(buf, fi), + root->root_key.objectid, trans->transid, + key.objectid, key.offset); + if (ret) { + faili = i; + goto fail; + } + } else { + bytenr = btrfs_node_blockptr(buf, i); + btrfs_node_key_to_cpu(buf, &key, i); + ret = btrfs_inc_extent_ref(trans, root, bytenr, + btrfs_level_size(root, level - 1), + root->root_key.objectid, + trans->transid, + level - 1, key.objectid); + if (ret) { + faili = i; + goto fail; + } + } + } return 0; -} - -int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 owner_objectid) -{ - return inc_block_ref(trans, root, root->node->bytenr, - root->node->size, owner_objectid, - trans->transid, 0, 0); +fail: + WARN_ON(1); +#if 0 + for (i =0; i < faili; i++) { + if (level == 0) { + u64 disk_bytenr; + btrfs_item_key_to_cpu(buf, &key, i); + if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) + continue; + fi = btrfs_item_ptr(buf, i, + struct btrfs_file_extent_item); + if (btrfs_file_extent_type(buf, fi) == + BTRFS_FILE_EXTENT_INLINE) + continue; + disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); + if (disk_bytenr == 0) + continue; + err = btrfs_free_extent(trans, root, disk_bytenr, + btrfs_file_extent_disk_num_bytes(buf, + fi), 0); + BUG_ON(err); + } else { + bytenr = btrfs_node_blockptr(buf, i); + err = btrfs_free_extent(trans, root, bytenr, + btrfs_level_size(root, level - 1), 0); + BUG_ON(err); + } + } +#endif + return ret; } static int write_one_cache_group(struct btrfs_trans_handle *trans, @@ -276,18 +895,22 @@ static int write_one_cache_group(struct int ret; int pending_ret; struct btrfs_root *extent_root = root->fs_info->extent_root; - struct btrfs_block_group_item *bi; - - ret = btrfs_search_slot(trans, root->fs_info->extent_root, - &cache->key, path, 0, 1); + unsigned long bi; + struct extent_buffer *leaf; + + ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); + if (ret < 0) + goto fail; BUG_ON(ret); - bi = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0], - struct btrfs_block_group_item); - memcpy(bi, &cache->item, sizeof(*bi)); - dirty_tree_block(trans, extent_root, path->nodes[0]); + + leaf = path->nodes[0]; + bi = btrfs_item_ptr_offset(leaf, path->slots[0]); + write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); + btrfs_mark_buffer_dirty(leaf); btrfs_release_path(extent_root, path); - finish_current_insert(trans, root); - pending_ret = run_pending(trans, root); +fail: + finish_current_insert(trans, extent_root); + pending_ret = del_pending_extents(trans, extent_root); if (ret) return ret; if (pending_ret) @@ -297,134 +920,280 @@ static int write_one_cache_group(struct } int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, - struct btrfs_root *root) -{ - struct btrfs_block_group_cache *bg; - struct cache_extent *cache; + struct btrfs_root *root) +{ + struct extent_map_tree *block_group_cache; + struct btrfs_block_group_cache *cache; + int ret; int err = 0; int werr = 0; - struct cache_tree *bg_cache = &root->fs_info->block_group_cache; - struct btrfs_path path; - btrfs_init_path(&path); - u64 start = 0; + struct btrfs_path *path; + u64 last = 0; + u64 start; + u64 end; + u64 ptr; + + block_group_cache = &root->fs_info->block_group_cache; + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; while(1) { - cache = find_first_cache_extent(bg_cache, start); - if (!cache) + ret = find_first_extent_bit(block_group_cache, last, + &start, &end, BLOCK_GROUP_DIRTY); + if (ret) break; - bg = container_of(cache, struct btrfs_block_group_cache, - cache); - start = cache->start + cache->size; - if (bg->dirty) { - err = write_one_cache_group(trans, root, - &path, bg); - if (err) - werr = err; - } - bg->dirty = 0; - } + + last = end + 1; + ret = get_state_private(block_group_cache, start, &ptr); + if (ret) + break; + + cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; + err = write_one_cache_group(trans, root, + path, cache); + /* + * if we fail to write the cache group, we want + * to keep it marked dirty in hopes that a later + * write will work + */ + if (err) { + werr = err; + continue; + } + clear_extent_bits(block_group_cache, start, end, + BLOCK_GROUP_DIRTY, GFP_NOFS); + } + btrfs_free_path(path); return werr; } static int update_block_group(struct btrfs_trans_handle *trans, struct btrfs_root *root, - u64 bytenr, u64 num, int alloc) -{ - struct btrfs_block_group_cache *bg; - struct cache_extent *cache; + u64 bytenr, u64 num_bytes, int alloc, + int mark_free, int data) +{ + struct btrfs_block_group_cache *cache; struct btrfs_fs_info *info = root->fs_info; - u64 total = num; + u64 total = num_bytes; u64 old_val; u64 byte_in_group; + u64 start; + u64 end; while(total) { - cache = find_first_cache_extent(&info->block_group_cache, - bytenr); - if (!cache) + cache = btrfs_lookup_block_group(info, bytenr); + if (!cache) { return -1; - bg = container_of(cache, struct btrfs_block_group_cache, - cache); - bg->dirty = 1; - byte_in_group = bytenr - bg->key.objectid; - old_val = btrfs_block_group_used(&bg->item); - if (total > bg->key.offset - byte_in_group) - num = bg->key.offset - byte_in_group; - else - num = total; - total -= num; - bytenr += num; - if (alloc) - old_val += num; - else - old_val -= num; - btrfs_set_block_group_used(&bg->item, old_val); + } + byte_in_group = bytenr - cache->key.objectid; + WARN_ON(byte_in_group > cache->key.offset); + start = cache->key.objectid; + end = start + cache->key.offset - 1; + set_extent_bits(&info->block_group_cache, start, end, + BLOCK_GROUP_DIRTY, GFP_NOFS); + + old_val = btrfs_block_group_used(&cache->item); + num_bytes = min(total, cache->key.offset - byte_in_group); + if (alloc) { + if (cache->data != data && + old_val < (cache->key.offset >> 1)) { + int bit_to_clear; + int bit_to_set; + cache->data = data; + if (data) { + bit_to_clear = BLOCK_GROUP_METADATA; + bit_to_set = BLOCK_GROUP_DATA; + cache->item.flags &= + ~BTRFS_BLOCK_GROUP_MIXED; + cache->item.flags |= + BTRFS_BLOCK_GROUP_DATA; + } else { + bit_to_clear = BLOCK_GROUP_DATA; + bit_to_set = BLOCK_GROUP_METADATA; + cache->item.flags &= + ~BTRFS_BLOCK_GROUP_MIXED; + cache->item.flags &= + ~BTRFS_BLOCK_GROUP_DATA; + } + clear_extent_bits(&info->block_group_cache, + start, end, bit_to_clear, + GFP_NOFS); + set_extent_bits(&info->block_group_cache, + start, end, bit_to_set, + GFP_NOFS); + } else if (cache->data != data && + cache->data != BTRFS_BLOCK_GROUP_MIXED) { + cache->data = BTRFS_BLOCK_GROUP_MIXED; + set_extent_bits(&info->block_group_cache, + start, end, + BLOCK_GROUP_DATA | + BLOCK_GROUP_METADATA, + GFP_NOFS); + } + old_val += num_bytes; + } else { + old_val -= num_bytes; + if (mark_free) { + set_extent_dirty(&info->free_space_cache, + bytenr, bytenr + num_bytes - 1, + GFP_NOFS); + } + } + btrfs_set_block_group_used(&cache->item, old_val); + total -= num_bytes; + bytenr += num_bytes; } return 0; } - -int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct - btrfs_root *root) -{ - u64 first = 0; - struct cache_extent *pe; - struct cache_extent *next; - - pe = find_first_cache_extent(&root->fs_info->pinned_tree, 0); - if (pe) - first = pe->start; - while(pe) { - next = next_cache_extent(pe); - remove_cache_extent(&root->fs_info->pinned_tree, pe); - free_cache_extent(pe); - pe = next; - } - root->fs_info->last_insert.objectid = first; - root->fs_info->last_insert.offset = 0; +static int update_pinned_extents(struct btrfs_root *root, + u64 bytenr, u64 num, int pin) +{ + u64 len; + struct btrfs_block_group_cache *cache; + struct btrfs_fs_info *fs_info = root->fs_info; + + if (pin) { + set_extent_dirty(&fs_info->pinned_extents, + bytenr, bytenr + num - 1, GFP_NOFS); + } else { + clear_extent_dirty(&fs_info->pinned_extents, + bytenr, bytenr + num - 1, GFP_NOFS); + } + while (num > 0) { + cache = btrfs_lookup_block_group(fs_info, bytenr); + WARN_ON(!cache); + len = min(num, cache->key.offset - + (bytenr - cache->key.objectid)); + if (pin) { + cache->pinned += len; + fs_info->total_pinned += len; + } else { + cache->pinned -= len; + fs_info->total_pinned -= len; + } + bytenr += len; + num -= len; + } return 0; } -static int finish_current_insert(struct btrfs_trans_handle *trans, struct - btrfs_root *extent_root) -{ +int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy) +{ + u64 last = 0; + u64 start; + u64 end; + struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents; + int ret; + + while(1) { + ret = find_first_extent_bit(pinned_extents, last, + &start, &end, EXTENT_DIRTY); + if (ret) + break; + set_extent_dirty(copy, start, end, GFP_NOFS); + last = end + 1; + } + return 0; +} + +int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_map_tree *unpin) +{ + u64 start; + u64 end; + int ret; + struct extent_map_tree *free_space_cache; + free_space_cache = &root->fs_info->free_space_cache; + + while(1) { + ret = find_first_extent_bit(unpin, 0, &start, &end, + EXTENT_DIRTY); + if (ret) + break; + update_pinned_extents(root, start, end + 1 - start, 0); + clear_extent_dirty(unpin, start, end, GFP_NOFS); + set_extent_dirty(free_space_cache, start, end, GFP_NOFS); + } + return 0; +} + +static int finish_current_insert(struct btrfs_trans_handle *trans, + struct btrfs_root *extent_root) +{ + u64 start; + u64 end; + struct btrfs_fs_info *info = extent_root->fs_info; + struct extent_buffer *eb; + struct btrfs_path *path; struct btrfs_key ins; + struct btrfs_disk_key first; struct btrfs_extent_item extent_item; int ret; - struct btrfs_fs_info *info = extent_root->fs_info; - struct cache_extent *pe; - struct cache_extent *next; - struct cache_tree *pending_tree = &info->pending_tree; - struct btrfs_path path; - - btrfs_init_path(&path); - btrfs_set_extent_refs(&extent_item, 1); - ins.offset = 1; + int level; + int err = 0; + + btrfs_set_stack_extent_refs(&extent_item, 1); btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); - pe = find_first_cache_extent(pending_tree, 0); - while(pe) { - ins.offset = pe->size; - ins.objectid = pe->start; - - remove_cache_extent(pending_tree, pe); - next = next_cache_extent(pe); - if (!next) - next = find_first_cache_extent(pending_tree, 0); - - free_cache_extent(pe); - pe = next; - - ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item, - sizeof(extent_item)); - if (ret) { - btrfs_print_tree(extent_root, extent_root->node); - } - BUG_ON(ret); - - ret = insert_extent_backref(trans, extent_root, &path, - ins.objectid, - extent_root->root_key.objectid, - 0, 0, 0); - BUG_ON(ret); - } + path = btrfs_alloc_path(); + + while(1) { + ret = find_first_extent_bit(&info->extent_ins, 0, &start, + &end, EXTENT_LOCKED); + if (ret) + break; + + ins.objectid = start; + ins.offset = end + 1 - start; + err = btrfs_insert_item(trans, extent_root, &ins, + &extent_item, sizeof(extent_item)); + clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, + GFP_NOFS); + eb = read_tree_block(extent_root, ins.objectid, ins.offset); + level = btrfs_header_level(eb); + if (level == 0) { + btrfs_item_key(eb, &first, 0); + } else { + btrfs_node_key(eb, &first, 0); + } + err = btrfs_insert_extent_backref(trans, extent_root, path, + start, extent_root->root_key.objectid, + 0, level, + btrfs_disk_key_objectid(&first)); + BUG_ON(err); + free_extent_buffer(eb); + } + btrfs_free_path(path); + return 0; +} + +static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, + int pending) +{ + int err = 0; + struct extent_buffer *buf; + + if (!pending) { + buf = btrfs_find_tree_block(root, bytenr, num_bytes); + if (buf) { + if (btrfs_buffer_uptodate(buf)) { + u64 transid = + root->fs_info->running_transaction->transid; + if (btrfs_header_generation(buf) == transid) { + free_extent_buffer(buf); + return 1; + } + } + free_extent_buffer(buf); + } + update_pinned_extents(root, bytenr, num_bytes, 1); + } else { + set_extent_bits(&root->fs_info->pending_del, + bytenr, bytenr + num_bytes - 1, + EXTENT_LOCKED, GFP_NOFS); + } + BUG_ON(err < 0); return 0; } @@ -434,12 +1203,14 @@ static int __free_extent(struct btrfs_tr static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 bytenr, u64 num_bytes, u64 root_objectid, u64 ref_generation, - u64 owner_objectid, u64 owner_offset, int pin) -{ - struct btrfs_path path; + u64 owner_objectid, u64 owner_offset, int pin, + int mark_free) +{ + struct btrfs_path *path; struct btrfs_key key; struct btrfs_fs_info *info = root->fs_info; struct btrfs_root *extent_root = info->extent_root; + struct extent_buffer *leaf; int ret; struct btrfs_extent_item *ei; u32 refs; @@ -448,57 +1219,69 @@ static int __free_extent(struct btrfs_tr btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); key.offset = num_bytes; - btrfs_init_path(&path); - - ret = lookup_extent_backref(trans, extent_root, &path, + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + ret = lookup_extent_backref(trans, extent_root, path, bytenr, root_objectid, ref_generation, owner_objectid, owner_offset, 1); if (ret == 0) { - ret = btrfs_del_item(trans, extent_root, &path); + ret = btrfs_del_item(trans, extent_root, path); } else { - // FIXME deal with missing references here - } - - btrfs_release_path(extent_root, &path); - - ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1); - if (ret) { - btrfs_print_tree(extent_root, extent_root->node); - printf("failed to find %llu\n", - (unsigned long long)key.objectid); - BUG(); - } - ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], + btrfs_print_leaf(extent_root, path->nodes[0]); + WARN_ON(1); + printk("Unable to find ref byte nr %Lu root %Lu " + " gen %Lu owner %Lu offset %Lu\n", bytenr, + root_objectid, ref_generation, owner_objectid, + owner_offset); + } + btrfs_release_path(extent_root, path); + ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); + if (ret < 0) + return ret; + BUG_ON(ret); + + leaf = path->nodes[0]; + ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); - BUG_ON(ei->refs == 0); - refs = btrfs_extent_refs(ei) - 1; - btrfs_set_extent_refs(ei, refs); + refs = btrfs_extent_refs(leaf, ei); + BUG_ON(refs == 0); + refs -= 1; + btrfs_set_extent_refs(leaf, ei, refs); + btrfs_mark_buffer_dirty(leaf); + if (refs == 0) { - u64 super_bytes_used, root_bytes_used; + u64 super_used; + u64 root_used; + if (pin) { - int err; - err = insert_cache_extent(&info->pinned_tree, - bytenr, num_bytes); - BUG_ON(err); - } - super_bytes_used = btrfs_super_bytes_used(info->disk_super); - btrfs_set_super_bytes_used(info->disk_super, - super_bytes_used - num_bytes); - root_bytes_used = btrfs_root_bytes_used(&root->root_item); - btrfs_set_root_bytes_used(&root->root_item, - root_bytes_used - num_bytes); - - ret = btrfs_del_item(trans, extent_root, &path); - if (!pin && extent_root->fs_info->last_insert.objectid > - bytenr) - extent_root->fs_info->last_insert.objectid = bytenr; - if (ret) - BUG(); - ret = update_block_group(trans, root, bytenr, num_bytes, 0); + ret = pin_down_bytes(root, bytenr, num_bytes, 0); + if (ret > 0) + mark_free = 1; + BUG_ON(ret < 0); + } + + /* block accounting for super block */ + super_used = btrfs_super_bytes_used(&info->super_copy); + btrfs_set_super_bytes_used(&info->super_copy, + super_used - num_bytes); + + /* block accounting for root item */ + root_used = btrfs_root_used(&root->root_item); + btrfs_set_root_used(&root->root_item, + root_used - num_bytes); + + ret = btrfs_del_item(trans, extent_root, path); + if (ret) { + return ret; + } + ret = update_block_group(trans, root, bytenr, num_bytes, 0, + mark_free, 0); BUG_ON(ret); } - btrfs_release_path(extent_root, &path); + btrfs_free_path(path); finish_current_insert(trans, extent_root); return ret; } @@ -511,60 +1294,57 @@ static int del_pending_extents(struct bt btrfs_root *extent_root) { int ret; - struct cache_extent *pe; - struct cache_extent *next; - struct cache_tree *del_pending = &extent_root->fs_info->del_pending; - - pe = find_first_cache_extent(del_pending, 0); - while(pe) { - remove_cache_extent(del_pending, pe); + int err = 0; + u64 start; + u64 end; + struct extent_map_tree *pending_del; + struct extent_map_tree *pinned_extents; + + pending_del = &extent_root->fs_info->pending_del; + pinned_extents = &extent_root->fs_info->pinned_extents; + + while(1) { + ret = find_first_extent_bit(pending_del, 0, &start, &end, + EXTENT_LOCKED); + if (ret) + break; + update_pinned_extents(extent_root, start, end + 1 - start, 1); + clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, + GFP_NOFS); ret = __free_extent(trans, extent_root, - pe->start, pe->size, - extent_root->root_key.objectid, - 0, 0, 0, 1); - BUG_ON(ret); - next = next_cache_extent(pe); - if (!next) - next = find_first_cache_extent(del_pending, 0); - free_cache_extent(pe); - pe = next; - } - return 0; -} - -static int run_pending(struct btrfs_trans_handle *trans, struct btrfs_root - *extent_root) -{ - del_pending_extents(trans, extent_root); - return 0; -} - + start, end + 1 - start, + extent_root->root_key.objectid, + 0, 0, 0, 0, 0); + if (ret) + err = ret; + } + return err; +} /* * remove an extent from the root, returns 0 on success */ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 bytenr, u64 num_bytes, - u64 root_objectid, u64 root_generation, + u64 root_objectid, u64 ref_generation, u64 owner_objectid, u64 owner_offset, int pin) { struct btrfs_root *extent_root = root->fs_info->extent_root; int pending_ret; int ret; + WARN_ON(num_bytes < root->sectorsize); if (!root->ref_cows) - root_generation = 0; + ref_generation = 0; if (root == extent_root) { - ret = insert_cache_extent(&root->fs_info->del_pending, - bytenr, num_bytes); - BUG_ON(ret); + pin_down_bytes(root, bytenr, num_bytes, 1); return 0; } - ret = __free_extent(trans, root, bytenr, num_bytes, - root_objectid, root_generation, owner_objectid, - owner_offset, pin); - pending_ret = run_pending(trans, root->fs_info->extent_root); + ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, + ref_generation, owner_objectid, owner_offset, + pin, pin == 0); + pending_ret = del_pending_extents(trans, root->fs_info->extent_root); return ret ? ret : pending_ret; } @@ -583,105 +1363,245 @@ static u64 stripe_align(struct btrfs_roo * ins->offset == number of blocks * Any available blocks before search_start are skipped. */ -static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root - *orig_root, u64 total_needed, u64 search_start, - u64 search_end, struct btrfs_key *ins) -{ - struct btrfs_path path; +static int noinline find_free_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *orig_root, + u64 num_bytes, u64 empty_size, + u64 search_start, u64 search_end, + u64 hint_byte, struct btrfs_key *ins, + u64 exclude_start, u64 exclude_nr, + int data) +{ + struct btrfs_path *path; struct btrfs_key key; - int ret; u64 hole_size = 0; + u64 aligned; + int ret; int slot = 0; u64 last_byte = 0; - u64 aligned; + u64 orig_search_start = search_start; int start_found; - struct btrfs_leaf *l; + struct extent_buffer *l; struct btrfs_root * root = orig_root->fs_info->extent_root; - - if (root->fs_info->last_insert.objectid > search_start) - search_start = root->fs_info->last_insert.objectid; - + struct btrfs_fs_info *info = root->fs_info; + u64 total_needed = num_bytes; + int level; + struct btrfs_block_group_cache *block_group; + int full_scan = 0; + int wrapped = 0; + u64 cached_start; + + WARN_ON(num_bytes < root->sectorsize); + btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); + + level = btrfs_header_level(root->node); + + if (num_bytes >= 32 * 1024 * 1024 && hint_byte) { + data = BTRFS_BLOCK_GROUP_MIXED; + } + + if (search_end == (u64)-1) + search_end = btrfs_super_total_bytes(&info->super_copy); + if (hint_byte) { + block_group = btrfs_lookup_block_group(info, hint_byte); + if (!block_group) + hint_byte = search_start; + block_group = btrfs_find_block_group(root, block_group, + hint_byte, data, 1); + } else { + block_group = btrfs_find_block_group(root, + trans->block_group, + search_start, data, 1); + } + + total_needed += empty_size; + path = btrfs_alloc_path(); +check_failed: + if (!block_group) { + block_group = btrfs_lookup_block_group(info, search_start); + if (!block_group) + block_group = btrfs_lookup_block_group(info, + orig_search_start); + } + search_start = find_search_start(root, &block_group, search_start, + total_needed, data, full_scan); search_start = stripe_align(root, search_start); - btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); - -check_failed: - btrfs_init_path(&path); + cached_start = search_start; + btrfs_init_path(path); ins->objectid = search_start; ins->offset = 0; start_found = 0; - ret = btrfs_search_slot(trans, root, ins, &path, 0, 0); + path->reada = 2; + + ret = btrfs_search_slot(trans, root, ins, path, 0, 0); if (ret < 0) goto error; - if (path.slots[0] > 0) - path.slots[0]--; - + if (path->slots[0] > 0) { + path->slots[0]--; + } + + l = path->nodes[0]; + btrfs_item_key_to_cpu(l, &key, path->slots[0]); + + /* + * walk backwards to find the first extent item key + */ + while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) { + if (path->slots[0] == 0) { + ret = btrfs_prev_leaf(root, path); + if (ret != 0) { + ret = btrfs_search_slot(trans, root, ins, + path, 0, 0); + if (ret < 0) + goto error; + if (path->slots[0] > 0) + path->slots[0]--; + break; + } + } else { + path->slots[0]--; + } + l = path->nodes[0]; + btrfs_item_key_to_cpu(l, &key, path->slots[0]); + } while (1) { - l = &path.nodes[0]->leaf; - slot = path.slots[0]; - if (slot >= btrfs_header_nritems(&l->header)) { - ret = btrfs_next_leaf(root, &path); + l = path->nodes[0]; + slot = path->slots[0]; + if (slot >= btrfs_header_nritems(l)) { + ret = btrfs_next_leaf(root, path); if (ret == 0) continue; if (ret < 0) goto error; + + search_start = max(search_start, + block_group->key.objectid); if (!start_found) { aligned = stripe_align(root, search_start); ins->objectid = aligned; - ins->offset = (u64)-1 - aligned; + if (aligned >= search_end) { + ret = -ENOSPC; + goto error; + } + ins->offset = search_end - aligned; start_found = 1; goto check_pending; } ins->objectid = stripe_align(root, last_byte > search_start ? last_byte : search_start); - ins->offset = (u64)-1 - ins->objectid; + if (search_end <= ins->objectid) { + ret = -ENOSPC; + goto error; + } + ins->offset = search_end - ins->objectid; + BUG_ON(ins->objectid >= search_end); goto check_pending; } - btrfs_disk_key_to_cpu(&key, &l->items[slot].key); - if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) + btrfs_item_key_to_cpu(l, &key, slot); + + if (key.objectid >= search_start && key.objectid > last_byte && + start_found) { + if (last_byte < search_start) + last_byte = search_start; + aligned = stripe_align(root, last_byte); + hole_size = key.objectid - aligned; + if (key.objectid > aligned && hole_size >= num_bytes) { + ins->objectid = aligned; + ins->offset = hole_size; + goto check_pending; + } + } + if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) { + if (!start_found && btrfs_key_type(&key) == + BTRFS_BLOCK_GROUP_ITEM_KEY) { + last_byte = key.objectid; + start_found = 1; + } goto next; - if (key.objectid >= search_start) { - if (start_found) { - if (last_byte < search_start) - last_byte = search_start; - aligned = stripe_align(root, last_byte); - hole_size = key.objectid - aligned; - if (key.objectid > aligned && - hole_size > total_needed) { - ins->objectid = aligned; - ins->offset = hole_size; - goto check_pending; - } - } - } + } + + start_found = 1; last_byte = key.objectid + key.offset; + + if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED && + last_byte >= block_group->key.objectid + + block_group->key.offset) { + btrfs_release_path(root, path); + search_start = block_group->key.objectid + + block_group->key.offset; + goto new_group; + } next: - path.slots[0]++; - } - // FIXME -ENOSPC + path->slots[0]++; + cond_resched(); + } check_pending: /* we have to make sure we didn't find an extent that has already * been allocated by the map tree or the original allocation */ - btrfs_release_path(root, &path); + btrfs_release_path(root, path); BUG_ON(ins->objectid < search_start); - if (find_cache_extent(&root->fs_info->pinned_tree, - ins->objectid, total_needed)) { - search_start = ins->objectid + total_needed; - goto check_failed; - } - if (find_cache_extent(&root->fs_info->pending_tree, - ins->objectid, total_needed)) { - search_start = ins->objectid + total_needed; - goto check_failed; - } - root->fs_info->last_insert.objectid = ins->objectid; - ins->offset = total_needed; + + if (ins->objectid + num_bytes >= search_end) + goto enospc; + if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED && + ins->objectid + num_bytes > block_group-> + key.objectid + block_group->key.offset) { + search_start = block_group->key.objectid + + block_group->key.offset; + goto new_group; + } + if (test_range_bit(&info->extent_ins, ins->objectid, + ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) { + search_start = ins->objectid + num_bytes; + goto new_group; + } + if (test_range_bit(&info->pinned_extents, ins->objectid, + ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) { + search_start = ins->objectid + num_bytes; + goto new_group; + } + if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && + ins->objectid < exclude_start + exclude_nr)) { + search_start = exclude_start + exclude_nr; + goto new_group; + } + if (!data) { + block_group = btrfs_lookup_block_group(info, ins->objectid); + if (block_group) + trans->block_group = block_group; + } + ins->offset = num_bytes; + btrfs_free_path(path); return 0; + +new_group: + if (search_start + num_bytes >= search_end) { +enospc: + search_start = orig_search_start; + if (full_scan) { + ret = -ENOSPC; + goto error; + } + if (wrapped) { + if (!full_scan) + total_needed -= empty_size; + full_scan = 1; + data = BTRFS_BLOCK_GROUP_MIXED; + } else + wrapped = 1; + } + block_group = btrfs_lookup_block_group(info, search_start); + cond_resched(); + block_group = btrfs_find_block_group(root, block_group, + search_start, data, 0); + goto check_failed; + error: - btrfs_release_path(root, &path); + btrfs_release_path(root, path); + btrfs_free_path(path); return ret; } /* @@ -691,160 +1611,352 @@ error: * * returns 0 if everything worked, non-zero otherwise. */ -static int alloc_extent(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 num_bytes, - u64 root_objectid, u64 ref_generation, u64 owner, - u64 owner_offset, u64 search_start, - u64 search_end, struct btrfs_key *ins) +int btrfs_alloc_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u64 num_bytes, u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset, + u64 empty_size, u64 hint_byte, + u64 search_end, struct btrfs_key *ins, int data) { int ret; int pending_ret; - u64 super_bytes_used, root_bytes_used; + u64 super_used, root_used; + u64 search_start = 0; + /* + u64 new_hint; + */ struct btrfs_fs_info *info = root->fs_info; struct btrfs_root *extent_root = info->extent_root; struct btrfs_extent_item extent_item; - struct btrfs_path path; - - btrfs_init_path(&path); - - btrfs_set_extent_refs(&extent_item, 1); - - ret = find_free_extent(trans, root, num_bytes, search_start, - search_end, ins); + struct btrfs_path *path; + + btrfs_set_stack_extent_refs(&extent_item, 1); + + /* + new_hint = max(hint_byte, root->fs_info->alloc_start); + if (new_hint < btrfs_super_total_bytes(&info->super_copy)) + hint_byte = new_hint; + */ + + WARN_ON(num_bytes < root->sectorsize); + ret = find_free_extent(trans, root, num_bytes, empty_size, + search_start, search_end, hint_byte, ins, + trans->alloc_exclude_start, + trans->alloc_exclude_nr, data); + BUG_ON(ret); if (ret) return ret; - super_bytes_used = btrfs_super_bytes_used(info->disk_super); - btrfs_set_super_bytes_used(info->disk_super, super_bytes_used + - num_bytes); - root_bytes_used = btrfs_root_bytes_used(&root->root_item); - btrfs_set_root_bytes_used(&root->root_item, root_bytes_used + - num_bytes); + /* block accounting for super block */ + super_used = btrfs_super_bytes_used(&info->super_copy); + btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); + + /* block accounting for root item */ + root_used = btrfs_root_used(&root->root_item); + btrfs_set_root_used(&root->root_item, root_used + num_bytes); + + clear_extent_dirty(&root->fs_info->free_space_cache, + ins->objectid, ins->objectid + ins->offset - 1, + GFP_NOFS); + if (root == extent_root) { - ret = insert_cache_extent(&root->fs_info->pending_tree, - ins->objectid, ins->offset); - BUG_ON(ret); + set_extent_bits(&root->fs_info->extent_ins, ins->objectid, + ins->objectid + ins->offset - 1, + EXTENT_LOCKED, GFP_NOFS); + WARN_ON(data == 1); goto update_block; } + + WARN_ON(trans->alloc_exclude_nr); + trans->alloc_exclude_start = ins->objectid; + trans->alloc_exclude_nr = ins->offset; ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, sizeof(extent_item)); + trans->alloc_exclude_start = 0; + trans->alloc_exclude_nr = 0; BUG_ON(ret); - ret = insert_extent_backref(trans, extent_root, &path, ins->objectid, - root_objectid, ref_generation, - owner, owner_offset); + + path = btrfs_alloc_path(); + BUG_ON(!path); + ret = btrfs_insert_extent_backref(trans, extent_root, path, + ins->objectid, root_objectid, + ref_generation, owner, owner_offset); + BUG_ON(ret); - + btrfs_free_path(path); finish_current_insert(trans, extent_root); - pending_ret = run_pending(trans, extent_root); - if (ret) + pending_ret = del_pending_extents(trans, extent_root); + + if (ret) { return ret; - if (pending_ret) + } + if (pending_ret) { return pending_ret; + } + update_block: - ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); + ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0, + data); BUG_ON(ret); return 0; } + /* * helper function to allocate a block for a given tree * returns the tree buffer or NULL. */ -struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - u32 blocksize) +struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u32 blocksize, + u64 root_objectid, u64 hint, + u64 empty_size) { u64 ref_generation; - struct btrfs_key ins; - int ret; - struct btrfs_buffer *buf; if (root->ref_cows) ref_generation = trans->transid; else ref_generation = 0; - ret = alloc_extent(trans, root, blocksize, - root->root_key.objectid, ref_generation, - 0, 0, 0, (u64)-1, &ins); + + return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid, + ref_generation, 0, 0, hint, empty_size); +} + +/* + * helper function to allocate a block for a given tree + * returns the tree buffer or NULL. + */ +struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u32 blocksize, + u64 root_objectid, + u64 ref_generation, + u64 first_objectid, + int level, + u64 hint, + u64 empty_size) +{ + struct btrfs_key ins; + int ret; + struct extent_buffer *buf; + + ret = btrfs_alloc_extent(trans, root, blocksize, + root_objectid, ref_generation, + level, first_objectid, empty_size, hint, + (u64)-1, &ins, 0); if (ret) { - BUG(); - return NULL; - } - buf = find_tree_block(root, ins.objectid, blocksize); - btrfs_set_header_generation(&buf->node.header, trans->transid); - btrfs_set_header_bytenr(&buf->node.header, buf->bytenr); - memcpy(buf->node.header.fsid, root->fs_info->disk_super->fsid, - sizeof(buf->node.header.fsid)); - dirty_tree_block(trans, root, buf); + BUG_ON(ret > 0); + return ERR_PTR(ret); + } + buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize); + if (!buf) { + btrfs_free_extent(trans, root, ins.objectid, blocksize, + root->root_key.objectid, ref_generation, + 0, 0, 0); + return ERR_PTR(-ENOMEM); + } + btrfs_set_buffer_uptodate(buf); + /* + set_extent_dirty(&trans->transaction->dirty_pages, buf->start, + buf->start + buf->len - 1, GFP_NOFS); + set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree, + buf->start, buf->start + buf->len - 1, + EXTENT_CSUM, GFP_NOFS); + buf->flags |= EXTENT_CSUM; + btrfs_set_buffer_defrag(buf); + */ + trans->blocks_used++; return buf; - +} + +static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct extent_buffer *leaf) +{ + u64 leaf_owner; + u64 leaf_generation; + struct btrfs_key key; + struct btrfs_file_extent_item *fi; + int i; + int nritems; + int ret; + + BUG_ON(!btrfs_is_leaf(leaf)); + nritems = btrfs_header_nritems(leaf); + leaf_owner = btrfs_header_owner(leaf); + leaf_generation = btrfs_header_generation(leaf); + + for (i = 0; i < nritems; i++) { + u64 disk_bytenr; + + btrfs_item_key_to_cpu(leaf, &key, i); + if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) + continue; + fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); + if (btrfs_file_extent_type(leaf, fi) == + BTRFS_FILE_EXTENT_INLINE) + continue; + /* + * FIXME make sure to insert a trans record that + * repeats the snapshot del on crash + */ + disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); + if (disk_bytenr == 0) + continue; + ret = btrfs_free_extent(trans, root, disk_bytenr, + btrfs_file_extent_disk_num_bytes(leaf, fi), + leaf_owner, leaf_generation, + key.objectid, key.offset, 0); + BUG_ON(ret); + } + return 0; +} + +static void noinline reada_walk_down(struct btrfs_root *root, + struct extent_buffer *node) +{ + int i; + u32 nritems; + u64 bytenr; + int ret; + u32 refs; + int level; + u32 blocksize; + + nritems = btrfs_header_nritems(node); + level = btrfs_header_level(node); + for (i = 0; i < nritems; i++) { + bytenr = btrfs_node_blockptr(node, i); + blocksize = btrfs_level_size(root, level - 1); + ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs); + BUG_ON(ret); + if (refs != 1) + continue; + mutex_unlock(&root->fs_info->fs_mutex); + ret = readahead_tree_block(root, bytenr, blocksize); + cond_resched(); + mutex_lock(&root->fs_info->fs_mutex); + if (ret) + break; + } } /* * helper function for drop_snapshot, this walks down the tree dropping ref * counts as it goes. */ -static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, int *level) -{ - struct btrfs_buffer *next; - struct btrfs_buffer *cur; - struct btrfs_buffer *parent; +static int noinline walk_down_tree(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, int *level) +{ u64 root_owner; u64 root_gen; u64 bytenr; + struct extent_buffer *next; + struct extent_buffer *cur; + struct extent_buffer *parent; + u32 blocksize; int ret; u32 refs; - ret = lookup_block_ref(trans, root, path->nodes[*level]->bytenr, - btrfs_level_size(root, *level), &refs); + WARN_ON(*level < 0); + WARN_ON(*level >= BTRFS_MAX_LEVEL); + ret = lookup_extent_ref(trans, root, + path->nodes[*level]->start, + path->nodes[*level]->len, &refs); BUG_ON(ret); if (refs > 1) goto out; + /* * walk down to the last node level and free all the leaves */ - while(*level > 0) { - u32 size = btrfs_level_size(root, *level - 1); - + while(*level >= 0) { + WARN_ON(*level < 0); + WARN_ON(*level >= BTRFS_MAX_LEVEL); cur = path->nodes[*level]; + + if (*level > 0 && path->slots[*level] == 0) + reada_walk_down(root, cur); + + if (btrfs_header_level(cur) != *level) + WARN_ON(1); + if (path->slots[*level] >= - btrfs_header_nritems(&cur->node.header)) + btrfs_header_nritems(cur)) break; - bytenr = btrfs_node_blockptr(&cur->node, path->slots[*level]); - ret = lookup_block_ref(trans, root, bytenr, size, &refs); - if (refs != 1 || *level == 1) { + if (*level == 0) { + ret = drop_leaf_ref(trans, root, cur); + BUG_ON(ret); + break; + } + bytenr = btrfs_node_blockptr(cur, path->slots[*level]); + blocksize = btrfs_level_size(root, *level - 1); + ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs); + BUG_ON(ret); + if (refs != 1) { parent = path->nodes[*level]; - root_owner = btrfs_header_owner(&parent->node.header); - root_gen = - btrfs_header_generation(&parent->node.header); + root_owner = btrfs_header_owner(parent); + root_gen = btrfs_header_generation(parent); path->slots[*level]++; - ret = btrfs_free_extent(trans, root, bytenr, size, - root_owner, root_gen, 0, 0, 1); + ret = btrfs_free_extent(trans, root, bytenr, + blocksize, root_owner, + root_gen, 0, 0, 1); BUG_ON(ret); continue; } - BUG_ON(ret); - next = read_tree_block(root, bytenr, size); + next = btrfs_find_tree_block(root, bytenr, blocksize); + if (!next || !btrfs_buffer_uptodate(next)) { + free_extent_buffer(next); + mutex_unlock(&root->fs_info->fs_mutex); + next = read_tree_block(root, bytenr, blocksize); + mutex_lock(&root->fs_info->fs_mutex); + + /* we dropped the lock, check one more time */ + ret = lookup_extent_ref(trans, root, bytenr, + blocksize, &refs); + BUG_ON(ret); + if (refs != 1) { + parent = path->nodes[*level]; + root_owner = btrfs_header_owner(parent); + root_gen = btrfs_header_generation(parent); + + path->slots[*level]++; + free_extent_buffer(next); + ret = btrfs_free_extent(trans, root, bytenr, + blocksize, + root_owner, + root_gen, 0, 0, 1); + BUG_ON(ret); + continue; + } + } + WARN_ON(*level <= 0); if (path->nodes[*level-1]) - btrfs_block_release(root, path->nodes[*level-1]); + free_extent_buffer(path->nodes[*level-1]); path->nodes[*level-1] = next; - *level = btrfs_header_level(&next->node.header); + *level = btrfs_header_level(next); path->slots[*level] = 0; } out: - if (*level == BTRFS_MAX_LEVEL - 1 || !path->nodes[*level + 1]) + WARN_ON(*level < 0); + WARN_ON(*level >= BTRFS_MAX_LEVEL); + + if (path->nodes[*level] == root->node) { + root_owner = root->root_key.objectid; parent = path->nodes[*level]; - else + } else { parent = path->nodes[*level + 1]; - - root_owner = btrfs_header_owner(&parent->node.header); - root_gen = btrfs_header_generation(&parent->node.header); - ret = btrfs_free_extent(trans, root, path->nodes[*level]->bytenr, - btrfs_level_size(root, *level), + root_owner = btrfs_header_owner(parent); + } + + root_gen = btrfs_header_generation(parent); + ret = btrfs_free_extent(trans, root, path->nodes[*level]->start, + path->nodes[*level]->len, root_owner, root_gen, 0, 0, 1); - btrfs_block_release(root, path->nodes[*level]); + free_extent_buffer(path->nodes[*level]); path->nodes[*level] = NULL; *level += 1; BUG_ON(ret); @@ -856,39 +1968,50 @@ out: * to find the first node higher up where we haven't yet gone through * all the slots */ -static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, int *level) -{ +static int noinline walk_up_tree(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, int *level) +{ + u64 root_owner; + u64 root_gen; + struct btrfs_root_item *root_item = &root->root_item; int i; int slot; int ret; - u64 root_owner; - u64 root_gen; - struct btrfs_buffer *parent; + for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { slot = path->slots[i]; - if (slot < - btrfs_header_nritems(&path->nodes[i]->node.header)- 1) { + if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { + struct extent_buffer *node; + struct btrfs_disk_key disk_key; + node = path->nodes[i]; path->slots[i]++; *level = i; + WARN_ON(*level == 0); + btrfs_node_key(node, &disk_key, path->slots[i]); + memcpy(&root_item->drop_progress, + &disk_key, sizeof(disk_key)); + root_item->drop_level = i; return 0; } else { - if (path->nodes[*level] == root->node) - parent = path->nodes[*level]; - else - parent = path->nodes[*level + 1]; - - root_owner = btrfs_header_owner(&parent->node.header); - root_gen = - btrfs_header_generation(&parent->node.header); + if (path->nodes[*level] == root->node) { + root_owner = root->root_key.objectid; + root_gen = + btrfs_header_generation(path->nodes[*level]); + } else { + struct extent_buffer *node; + node = path->nodes[*level + 1]; + root_owner = btrfs_header_owner(node); + root_gen = btrfs_header_generation(node); + } ret = btrfs_free_extent(trans, root, - path->nodes[*level]->bytenr, - btrfs_level_size(root, *level), - root_owner, root_gen, 0, 0, 1); - btrfs_block_release(root, path->nodes[*level]); + path->nodes[*level]->start, + path->nodes[*level]->len, + root_owner, root_gen, 0, 0, 1); + BUG_ON(ret); + free_extent_buffer(path->nodes[*level]); path->nodes[*level] = NULL; *level = i + 1; - BUG_ON(ret); } } return 1; @@ -900,128 +2023,266 @@ static int walk_up_tree(struct btrfs_tra * decremented. */ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_buffer *snap) + *root) { int ret = 0; int wret; int level; - struct btrfs_path path; + struct btrfs_path *path; int i; int orig_level; - - btrfs_init_path(&path); - - level = btrfs_header_level(&snap->node.header); + struct btrfs_root_item *root_item = &root->root_item; + + path = btrfs_alloc_path(); + BUG_ON(!path); + + level = btrfs_header_level(root->node); orig_level = level; - path.nodes[level] = snap; - path.slots[level] = 0; + if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { + path->nodes[level] = root->node; + extent_buffer_get(root->node); + path->slots[level] = 0; + } else { + struct btrfs_key key; + struct btrfs_disk_key found_key; + struct extent_buffer *node; + + btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); + level = root_item->drop_level; + path->lowest_level = level; + wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + if (wret < 0) { + ret = wret; + goto out; + } + node = path->nodes[level]; + btrfs_node_key(node, &found_key, path->slots[level]); + WARN_ON(memcmp(&found_key, &root_item->drop_progress, + sizeof(found_key))); + } while(1) { - wret = walk_down_tree(trans, root, &path, &level); - if (wret > 0) - break; + wret = walk_down_tree(trans, root, path, &level); if (wret < 0) ret = wret; - - wret = walk_up_tree(trans, root, &path, &level); - if (wret > 0) + if (wret != 0) break; + + wret = walk_up_tree(trans, root, path, &level); if (wret < 0) ret = wret; + if (wret != 0) + break; + /* + ret = -EAGAIN; + break; + */ } for (i = 0; i <= orig_level; i++) { - if (path.nodes[i]) { - btrfs_block_release(root, path.nodes[i]); - } - } + if (path->nodes[i]) { + free_extent_buffer(path->nodes[i]); + path->nodes[i] = NULL; + } + } +out: + btrfs_free_path(path); return ret; } int btrfs_free_block_groups(struct btrfs_fs_info *info) { - struct btrfs_block_group_cache *bg; - struct cache_extent *cache; - + u64 start; + u64 end; + u64 ptr; + int ret; while(1) { - cache = find_first_cache_extent(&info->block_group_cache, 0); - if (!cache) + ret = find_first_extent_bit(&info->block_group_cache, 0, + &start, &end, (unsigned int)-1); + if (ret) break; - bg = container_of(cache, struct btrfs_block_group_cache, - cache); - remove_cache_extent(&info->block_group_cache, cache); - free(bg); + ret = get_state_private(&info->block_group_cache, start, &ptr); + if (!ret) + kfree((void *)(unsigned long)ptr); + clear_extent_bits(&info->block_group_cache, start, + end, (unsigned int)-1, GFP_NOFS); + } + while(1) { + ret = find_first_extent_bit(&info->free_space_cache, 0, + &start, &end, EXTENT_DIRTY); + if (ret) + break; + clear_extent_dirty(&info->free_space_cache, start, + end, GFP_NOFS); } return 0; } int btrfs_read_block_groups(struct btrfs_root *root) { - struct btrfs_path path; + struct btrfs_path *path; int ret; int err = 0; - struct btrfs_block_group_item *bi; - struct btrfs_block_group_cache *bg; - struct cache_tree *bg_cache; + int bit; + struct btrfs_block_group_cache *cache; + struct btrfs_fs_info *info = root->fs_info; + struct extent_map_tree *block_group_cache; struct btrfs_key key; struct btrfs_key found_key; - struct btrfs_leaf *leaf; - u64 group_size = BTRFS_BLOCK_GROUP_SIZE; - - root = root->fs_info->extent_root; - bg_cache = &root->fs_info->block_group_cache; + struct extent_buffer *leaf; + + block_group_cache = &info->block_group_cache; + + root = info->extent_root; key.objectid = 0; - key.offset = group_size; + key.offset = BTRFS_BLOCK_GROUP_SIZE; btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); - btrfs_init_path(&path); + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; while(1) { - ret = btrfs_search_slot(NULL, root->fs_info->extent_root, - &key, &path, 0, 0); + ret = btrfs_search_slot(NULL, info->extent_root, + &key, path, 0, 0); if (ret != 0) { err = ret; break; } - leaf = &path.nodes[0]->leaf; - btrfs_disk_key_to_cpu(&found_key, - &leaf->items[path.slots[0]].key); - bg = malloc(sizeof(*bg)); - if (!bg) { + leaf = path->nodes[0]; + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); + cache = kmalloc(sizeof(*cache), GFP_NOFS); + if (!cache) { err = -1; break; } - bi = btrfs_item_ptr(leaf, path.slots[0], - struct btrfs_block_group_item); - memcpy(&bg->item, bi, sizeof(*bi)); - memcpy(&bg->key, &found_key, sizeof(found_key)); + + read_extent_buffer(leaf, &cache->item, + btrfs_item_ptr_offset(leaf, path->slots[0]), + sizeof(cache->item)); + memcpy(&cache->key, &found_key, sizeof(found_key)); + cache->cached = 0; + cache->pinned = 0; key.objectid = found_key.objectid + found_key.offset; - btrfs_release_path(root, &path); - bg->cache.start = found_key.objectid; - bg->cache.size = found_key.offset; - bg->dirty = 0; - ret = insert_existing_cache_extent(bg_cache, &bg->cache); - BUG_ON(ret); + btrfs_release_path(root, path); + + if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) { + bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; + cache->data = BTRFS_BLOCK_GROUP_MIXED; + } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) { + bit = BLOCK_GROUP_DATA; + cache->data = BTRFS_BLOCK_GROUP_DATA; + } else { + bit = BLOCK_GROUP_METADATA; + cache->data = 0; + } + + /* use EXTENT_LOCKED to prevent merging */ + set_extent_bits(block_group_cache, found_key.objectid, + found_key.objectid + found_key.offset - 1, + bit | EXTENT_LOCKED, GFP_NOFS); + set_state_private(block_group_cache, found_key.objectid, + (unsigned long)cache); + if (key.objectid >= - btrfs_super_total_bytes(root->fs_info->disk_super)) + btrfs_super_total_bytes(&info->super_copy)) break; } - btrfs_release_path(root, &path); + + btrfs_free_path(path); return 0; } -int btrfs_insert_block_group(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_key *key, - struct btrfs_block_group_item *bi) +static int btrfs_insert_block_group(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_key *key, + struct btrfs_block_group_item *bi) { int ret; int pending_ret; - - root = root->fs_info->extent_root; - ret = btrfs_insert_item(trans, root, key, bi, sizeof(*bi)); - finish_current_insert(trans, root); - pending_ret = run_pending(trans, root); + struct btrfs_root *extent_root; + + extent_root = root->fs_info->extent_root; + ret = btrfs_insert_item(trans, extent_root, key, bi, sizeof(*bi)); + finish_current_insert(trans, extent_root); + pending_ret = del_pending_extents(trans, extent_root); if (ret) return ret; if (pending_ret) return pending_ret; - return ret; -} + return 0; +} + +int btrfs_make_block_groups(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + u64 group_size; + u64 bytes_used; + u64 total_bytes; + u64 cur_start; + u64 nr = 0; + int ret; + int bit; + struct btrfs_root *extent_root; + struct btrfs_block_group_cache *cache; + struct extent_map_tree *block_group_cache; + + extent_root = root->fs_info->extent_root; + block_group_cache = &root->fs_info->block_group_cache; + group_size = BTRFS_BLOCK_GROUP_SIZE; + bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy); + total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); + + cur_start = 0; + while (cur_start < total_bytes) { + cache = malloc(sizeof(*cache)); + BUG_ON(!cache); + cache->key.objectid = cur_start; + cache->key.offset = group_size; + btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); + memset(&cache->item, 0, sizeof(cache->item)); + if (nr == 0) + btrfs_set_block_group_used(&cache->item, bytes_used); + if (nr++ % 3) { + bit = BLOCK_GROUP_DATA; + cache->data = 1; + cache->item.flags |= BTRFS_BLOCK_GROUP_DATA; + } else { + bit = BLOCK_GROUP_METADATA; + cache->data = 0; + } + + set_extent_bits(block_group_cache, cur_start, + cur_start + group_size - 1, + bit | EXTENT_LOCKED, GFP_NOFS); + set_state_private(block_group_cache, cur_start, + (unsigned long)cache); + cur_start += group_size; + } + /* then insert all the items */ + cur_start = 0; + while(cur_start < total_bytes) { + cache = btrfs_lookup_block_group(root->fs_info, cur_start); + BUG_ON(!cache); + ret = btrfs_insert_block_group(trans, root, &cache->key, + &cache->item); + BUG_ON(ret); + cur_start += group_size; + } + return 0; +} + +u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation, + u64 owner, u64 owner_offset) +{ + return hash_extent_ref(root_objectid, ref_generation, + owner, owner_offset); +} + +int btrfs_update_block_group(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + u64 bytenr, u64 num_bytes, int alloc, + int mark_free, int data) +{ + return update_block_group(trans, root, bytenr, num_bytes, + alloc, mark_free, data); +} + diff -r 59fc7ac63e46 file-item.c --- a/file-item.c Fri Dec 21 16:25:35 2007 -0500 +++ b/file-item.c Fri Jan 04 19:19:50 2008 +0800 @@ -23,6 +23,7 @@ #include "ctree.h" #include "disk-io.h" #include "transaction.h" +#include "print-tree.h" #include "crc32c.h" #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ @@ -35,86 +36,169 @@ int btrfs_create_file(struct btrfs_trans } int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - u64 objectid, u64 pos, - u64 offset, u64 disk_num_bytes, - u64 num_bytes) + struct btrfs_root *root, + u64 objectid, u64 pos, u64 offset, + u64 disk_num_bytes, u64 num_bytes) { int ret = 0; struct btrfs_file_extent_item *item; struct btrfs_key file_key; - struct btrfs_path path; - struct btrfs_leaf *leaf; - - - btrfs_init_path(&path); + struct btrfs_path *path; + struct extent_buffer *leaf; + + path = btrfs_alloc_path(); + BUG_ON(!path); file_key.objectid = objectid; file_key.offset = pos; btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); - ret = btrfs_insert_empty_item(trans, root, &path, &file_key, + ret = btrfs_insert_empty_item(trans, root, path, &file_key, sizeof(*item)); if (ret < 0) goto out; BUG_ON(ret); - leaf = &path.nodes[0]->leaf; - item = btrfs_item_ptr(leaf, path.slots[0], + leaf = path->nodes[0]; + item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); - btrfs_set_file_extent_disk_bytenr(item, offset); - btrfs_set_file_extent_disk_num_bytes(item, disk_num_bytes); - btrfs_set_file_extent_offset(item, 0); - btrfs_set_file_extent_num_bytes(item, num_bytes); - btrfs_set_file_extent_generation(item, trans->transid); - btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG); + btrfs_set_file_extent_disk_bytenr(leaf, item, offset); + btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); + btrfs_set_file_extent_offset(leaf, item, 0); + btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); + btrfs_set_file_extent_generation(leaf, item, trans->transid); + btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); + btrfs_mark_buffer_dirty(leaf); out: - btrfs_release_path(root, &path); + btrfs_free_path(path); return ret; } -int btrfs_insert_inline_file_extent(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid, - u64 offset, char *buffer, size_t size) -{ - int ret; - char *ptr; +int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid, + u64 offset, char *buffer, size_t size) +{ + struct btrfs_key key; + struct btrfs_path *path; + struct extent_buffer *leaf; + unsigned long ptr; + struct btrfs_file_extent_item *ei; u32 datasize; - struct btrfs_key key; - struct btrfs_path path; - struct btrfs_leaf *leaf; - struct btrfs_file_extent_item *ei; - - btrfs_init_path(&path); + int err = 0; + int ret; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + key.objectid = objectid; key.offset = offset; btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); - datasize = btrfs_file_extent_calc_inline_size(size); - ret = btrfs_insert_empty_item(trans, root, &path, &key, - datasize); - BUG_ON(ret); - leaf = &path.nodes[0]->leaf; - ei = btrfs_item_ptr(leaf, path.slots[0], - struct btrfs_file_extent_item); - btrfs_set_file_extent_generation(ei, trans->transid); - btrfs_set_file_extent_type(ei, BTRFS_FILE_EXTENT_INLINE); - ptr = btrfs_file_extent_inline_start(ei); - memcpy(ptr, buffer, size); - btrfs_release_path(root, &path); - return 0; -} - -int btrfs_lookup_csum(struct btrfs_trans_handle *trans, - struct btrfs_root *root, - struct btrfs_path *path, - u64 objectid, u64 offset, int cow, - struct btrfs_csum_item **item_ret) -{ - int ret; - int slot; + ret = btrfs_search_slot(trans, root, &key, path, 0, 1); + if (ret < 0) { + err = ret; + goto fail; + } + if (ret == 1) { + struct btrfs_key found_key; + + if (path->slots[0] == 0) + goto insert; + + path->slots[0]--; + leaf = path->nodes[0]; + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); + + if (found_key.objectid != objectid) + goto insert; + + if (found_key.type != BTRFS_EXTENT_DATA_KEY) + goto insert; + ei = btrfs_item_ptr(leaf, path->slots[0], + struct btrfs_file_extent_item); + + if (btrfs_file_extent_type(leaf, ei) != + BTRFS_FILE_EXTENT_INLINE) { + goto insert; + } + btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); + ret = 0; + } + if (ret == 0) { + u32 found_size; + u64 found_end; + + leaf = path->nodes[0]; + ei = btrfs_item_ptr(leaf, path->slots[0], + struct btrfs_file_extent_item); + + if (btrfs_file_extent_type(leaf, ei) != + BTRFS_FILE_EXTENT_INLINE) { + err = ret; + btrfs_print_leaf(root, leaf); + printk("found wasn't inline offset %llu inode %llu\n", + offset, objectid); + goto fail; + } + found_size = btrfs_file_extent_inline_len(leaf, + btrfs_item_nr(leaf, path->slots[0])); + found_end = key.offset + found_size; + + if (found_end < offset + size) { + btrfs_release_path(root, path); + ret = btrfs_search_slot(trans, root, &key, path, + offset + size - found_end, 1); + BUG_ON(ret != 0); + + ret = btrfs_extend_item(trans, root, path, + offset + size - found_end); + if (ret) { + err = ret; + goto fail; + } + leaf = path->nodes[0]; + ei = btrfs_item_ptr(leaf, path->slots[0], + struct btrfs_file_extent_item); + } + if (found_end < offset) { + ptr = btrfs_file_extent_inline_start(ei) + found_size; + memset_extent_buffer(leaf, 0, ptr, offset - found_end); + } + } else { +insert: + btrfs_release_path(root, path); + datasize = offset + size - key.offset; + datasize = btrfs_file_extent_calc_inline_size(datasize); + ret = btrfs_insert_empty_item(trans, root, path, &key, + datasize); + if (ret) { + err = ret; + printk("got bad ret %d\n", ret); + goto fail; + } + leaf = path->nodes[0]; + ei = btrfs_item_ptr(leaf, path->slots[0], + struct btrfs_file_extent_item); + btrfs_set_file_extent_generation(leaf, ei, trans->transid); + btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); + } + ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset; + write_extent_buffer(leaf, buffer, ptr, size); + btrfs_mark_buffer_dirty(leaf); +fail: + btrfs_free_path(path); + return err; +} + +struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, + u64 objectid, u64 offset, int cow) +{ + int ret; struct btrfs_key file_key; struct btrfs_key found_key; struct btrfs_csum_item *item; - struct btrfs_leaf *leaf; + struct extent_buffer *leaf; u64 csum_offset = 0; int csums_in_item; @@ -124,35 +208,51 @@ int btrfs_lookup_csum(struct btrfs_trans ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); if (ret < 0) goto fail; - leaf = &path->nodes[0]->leaf; + leaf = path->nodes[0]; if (ret > 0) { + ret = 1; if (path->slots[0] == 0) goto fail; path->slots[0]--; - - slot = path->slots[0]; - btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key); + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || found_key.objectid != objectid) { goto fail; } - csum_offset = (offset - found_key.offset) / root->sectorsize; - csums_in_item = btrfs_item_size(&leaf->items[slot]); + csum_offset = (offset - found_key.offset) >> root->sectorsize; + csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); csums_in_item /= BTRFS_CRC32_SIZE; if (csum_offset >= csums_in_item) { ret = -EFBIG; goto fail; } - ret = 0; } item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); item = (struct btrfs_csum_item *)((unsigned char *)item + csum_offset * BTRFS_CRC32_SIZE); - *item_ret = item; + return item; fail: if (ret > 0) ret = -ENOENT; + return ERR_PTR(ret); +} + + +int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 objectid, + u64 offset, int mod) +{ + int ret; + struct btrfs_key file_key; + int ins_len = mod < 0 ? -1 : 0; + int cow = mod != 0; + + file_key.objectid = objectid; + file_key.offset = offset; + btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); + ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); return ret; } @@ -163,56 +263,53 @@ int btrfs_csum_file_block(struct btrfs_t char *data, size_t len) { int ret; - int slot; struct btrfs_key file_key; struct btrfs_key found_key; u64 next_offset = (u64)-1; int found_next = 0; - struct btrfs_path path; + struct btrfs_path *path; struct btrfs_csum_item *item; - struct btrfs_leaf *leaf = NULL; + struct extent_buffer *leaf = NULL; u64 csum_offset; u32 csum_result = ~(u32)0; u32 nritems; u32 ins_size; - btrfs_init_path(&path); + path = btrfs_alloc_path(); + BUG_ON(!path); file_key.objectid = objectid; file_key.offset = offset; btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); - ret = btrfs_lookup_csum(trans, root, &path, objectid, - offset, 1, &item); - if (!ret) { - leaf = &path.nodes[0]->leaf; + item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); + if (!IS_ERR(item)) { + leaf = path->nodes[0]; goto found; } - if (ret != -EFBIG && ret != -ENOENT) - goto fail; - leaf = &path.nodes[0]->leaf; + ret = PTR_ERR(item); if (ret == -EFBIG) { u32 item_size; - slot = path.slots[0]; /* we found one, but it isn't big enough yet */ - item_size = btrfs_item_size(&leaf->items[slot]); + leaf = path->nodes[0]; + item_size = btrfs_item_size_nr(leaf, path->slots[0]); if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { /* already at max size, make a new one */ goto insert; } } else { - slot = path.slots[0] + 1; + int slot = path->slots[0] + 1; /* we didn't find a csum item, insert one */ - nritems = btrfs_header_nritems(&leaf->header); - if (path.slots[0] >= nritems - 1) { - ret = btrfs_next_leaf(root, &path); + nritems = btrfs_header_nritems(path->nodes[0]); + if (path->slots[0] >= nritems - 1) { + ret = btrfs_next_leaf(root, path); if (ret == 1) found_next = 1; if (ret != 0) goto insert; slot = 0; } - btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key); + btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); if (found_key.objectid != objectid || found_key.type != BTRFS_CSUM_ITEM_KEY) { found_next = 1; @@ -227,68 +324,98 @@ int btrfs_csum_file_block(struct btrfs_t * at this point, we know the tree has an item, but it isn't big * enough yet to put our csum in. Grow it */ - btrfs_release_path(root, &path); - ret = btrfs_search_slot(trans, root, &file_key, &path, + btrfs_release_path(root, path); + ret = btrfs_search_slot(trans, root, &file_key, path, BTRFS_CRC32_SIZE, 1); if (ret < 0) goto fail; - BUG_ON(ret == 0); - if (path.slots[0] == 0) { + if (ret == 0) { + BUG(); + } + if (path->slots[0] == 0) { goto insert; } - path.slots[0]--; - slot = path.slots[0]; - leaf = &path.nodes[0]->leaf; - btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key); + path->slots[0]--; + leaf = path->nodes[0]; + btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); csum_offset = (offset - found_key.offset) / root->sectorsize; if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || found_key.objectid != objectid || csum_offset >= MAX_CSUM_ITEMS(root)) { goto insert; } - if (csum_offset >= btrfs_item_size(&leaf->items[slot]) / + if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / BTRFS_CRC32_SIZE) { u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; - diff = diff - btrfs_item_size(&leaf->items[slot]); + diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); if (diff != BTRFS_CRC32_SIZE) goto insert; - ret = btrfs_extend_item(trans, root, &path, diff); + ret = btrfs_extend_item(trans, root, path, diff); BUG_ON(ret); goto csum; } insert: - btrfs_release_path(root, &path); + btrfs_release_path(root, path); csum_offset = 0; if (found_next) { - u64 tmp; - if (next_offset > btrfs_inode_size(inode)) - next_offset = btrfs_inode_size(inode); - tmp = next_offset - offset + root->sectorsize - 1; + u64 tmp = min(btrfs_stack_inode_size(inode), next_offset); + tmp -= offset & ~((u64)root->sectorsize -1); tmp /= root->sectorsize; - if (tmp > MAX_CSUM_ITEMS(root)) - tmp = MAX_CSUM_ITEMS(root); + tmp = max((u64)1, tmp); + tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root)); ins_size = BTRFS_CRC32_SIZE * tmp; } else { ins_size = BTRFS_CRC32_SIZE; } - ret = btrfs_insert_empty_item(trans, root, &path, &file_key, + ret = btrfs_insert_empty_item(trans, root, path, &file_key, ins_size); if (ret < 0) goto fail; - BUG_ON(ret != 0); + if (ret != 0) { + WARN_ON(1); + goto fail; + } csum: - slot = path.slots[0]; - leaf = &path.nodes[0]->leaf; - item = btrfs_item_ptr(leaf, slot, struct btrfs_csum_item); + leaf = path->nodes[0]; + item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); + ret = 0; item = (struct btrfs_csum_item *)((unsigned char *)item + csum_offset * BTRFS_CRC32_SIZE); found: - csum_result = crc32c(csum_result, data, len); - csum_result = ~cpu_to_le32(csum_result); - memcpy(item, &csum_result, BTRFS_CRC32_SIZE); - ret = 0; + csum_result = btrfs_csum_data(root, data, csum_result, len); + btrfs_csum_final(csum_result, (char *)&csum_result); + write_extent_buffer(leaf, &csum_result, (unsigned long)item, + BTRFS_CRC32_SIZE); + btrfs_mark_buffer_dirty(path->nodes[0]); fail: - btrfs_release_path(root, &path); + btrfs_release_path(root, path); + btrfs_free_path(path); return ret; } + +int btrfs_csum_truncate(struct btrfs_trans_handle *trans, + struct btrfs_root *root, struct btrfs_path *path, + u64 isize) +{ + struct btrfs_key key; + struct extent_buffer *leaf = path->nodes[0]; + int slot = path->slots[0]; + int ret; + u32 new_item_size; + u64 new_item_span; + u64 blocks; + + btrfs_item_key_to_cpu(leaf, &key, slot); + if (isize <= key.offset) + return 0; + new_item_span = isize - key.offset; + blocks = (new_item_span + root->sectorsize - 1) / root->sectorsize; + new_item_size = blocks * BTRFS_CRC32_SIZE; + if (new_item_size >= btrfs_item_size_nr(leaf, slot)) + return 0; + ret = btrfs_truncate_item(trans, root, path, new_item_size, 1); + BUG_ON(ret); + return ret; +} + diff -r 59fc7ac63e46 inode-item.c --- a/inode-item.c Fri Dec 21 16:25:35 2007 -0500 +++ b/inode-item.c Fri Jan 04 19:19:50 2008 +0800 @@ -16,23 +16,102 @@ * Boston, MA 021110-1307, USA. */ -#include -#include -#include "kerncompat.h" -#include "radix-tree.h" #include "ctree.h" #include "disk-io.h" #include "transaction.h" + +int find_name_in_backref(struct btrfs_path *path, const char * name, + int name_len, struct btrfs_inode_ref **ref_ret) +{ + struct extent_buffer *leaf; + struct btrfs_inode_ref *ref; + unsigned long ptr; + unsigned long name_ptr; + u32 item_size; + u32 cur_offset = 0; + int len; + + leaf = path->nodes[0]; + item_size = btrfs_item_size_nr(leaf, path->slots[0]); + ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); + while (cur_offset < item_size) { + ref = (struct btrfs_inode_ref *)(ptr + cur_offset); + len = btrfs_inode_ref_name_len(leaf, ref); + name_ptr = (unsigned long)(ref + 1); + cur_offset += len + sizeof(*ref); + if (len != name_len) + continue; + if (memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) { + *ref_ret = ref; + return 1; + } + } + return 0; +} + +int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + const char *name, int name_len, + u64 inode_objectid, u64 ref_objectid) +{ + struct btrfs_path *path; + struct btrfs_key key; + struct btrfs_inode_ref *ref; + struct extent_buffer *leaf; + unsigned long ptr; + unsigned long item_start; + u32 item_size; + u32 sub_item_len; + int ret; + int del_len = name_len + sizeof(*ref); + + key.objectid = inode_objectid; + key.offset = ref_objectid; + btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + ret = btrfs_search_slot(trans, root, &key, path, -1, 1); + if (ret > 0) { + ret = -ENOENT; + goto out; + } else if (ret < 0) { + goto out; + } + if (!find_name_in_backref(path, name, name_len, &ref)) { + ret = -ENOENT; + goto out; + } + leaf = path->nodes[0]; + item_size = btrfs_item_size_nr(leaf, path->slots[0]); + if (del_len == item_size) { + ret = btrfs_del_item(trans, root, path); + goto out; + } + ptr = (unsigned long)ref; + sub_item_len = name_len + sizeof(*ref); + item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); + memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, + item_size - (ptr + sub_item_len - item_start)); + ret = btrfs_truncate_item(trans, root, path, + item_size - sub_item_len, 1); + BUG_ON(ret); +out: + btrfs_free_path(path); + return ret; +} int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, const char *name, int name_len, u64 inode_objectid, u64 ref_objectid) { - struct btrfs_path path; - struct btrfs_key key; - struct btrfs_inode_ref *ref; - char *ptr; + struct btrfs_path *path; + struct btrfs_key key; + struct btrfs_inode_ref *ref; + unsigned long ptr; int ret; int ins_len = name_len + sizeof(*ref); @@ -40,11 +119,13 @@ int btrfs_insert_inode_ref(struct btrfs_ key.offset = ref_objectid; btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); - btrfs_init_path(&path); - ret = btrfs_insert_empty_item(trans, root, &path, &key, + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + ret = btrfs_insert_empty_item(trans, root, path, &key, ins_len); if (ret == -EEXIST) { -#if 0 u32 old_size; if (find_name_in_backref(path, name, name_len, &ref)) @@ -59,21 +140,62 @@ int btrfs_insert_inode_ref(struct btrfs_ btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); ptr = (unsigned long)(ref + 1); ret = 0; -#endif - goto out; } else if (ret < 0) { goto out; } else { - ref = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], + ref = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_inode_ref); - btrfs_set_inode_ref_name_len(ref, name_len); - ptr = (char *)(ref + 1); - } - memcpy(ptr, name, name_len); - dirty_tree_block(trans, root, path.nodes[0]); + btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); + ptr = (unsigned long)(ref + 1); + } + write_extent_buffer(path->nodes[0], name, ptr, name_len); + btrfs_mark_buffer_dirty(path->nodes[0]); out: - btrfs_release_path(root, &path); + btrfs_free_path(path); + return ret; +} + +int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct btrfs_path *path, u64 objectid) +{ + struct btrfs_key key; + int ret; + key.objectid = objectid; + btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); + key.offset = 0; + + ret = btrfs_insert_empty_item(trans, root, path, &key, + sizeof(struct btrfs_inode_item)); + if (ret == 0 && objectid > root->highest_inode) + root->highest_inode = objectid; + return ret; +} + +int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root + *root, struct btrfs_path *path, + struct btrfs_key *location, int mod) +{ + int ins_len = mod < 0 ? -1 : 0; + int cow = mod != 0; + int ret; + int slot; + struct extent_buffer *leaf; + struct btrfs_key found_key; + + ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); + if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY && + location->offset == (u64)-1 && path->slots[0] != 0) { + slot = path->slots[0] - 1; + leaf = path->nodes[0]; + btrfs_item_key_to_cpu(leaf, &found_key, slot); + if (found_key.objectid == location->objectid && + btrfs_key_type(&found_key) == btrfs_key_type(location)) { + path->slots[0]--; + return 0; + } + } return ret; } @@ -81,29 +203,14 @@ int btrfs_insert_inode(struct btrfs_tran *root, u64 objectid, struct btrfs_inode_item *inode_item) { - struct btrfs_path path; - struct btrfs_key key; - int ret; + int ret; + struct btrfs_key key; + key.objectid = objectid; - btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); + key.type = BTRFS_INODE_ITEM_KEY; key.offset = 0; - btrfs_init_path(&path); ret = btrfs_insert_item(trans, root, &key, inode_item, sizeof(*inode_item)); - btrfs_release_path(root, &path); - return ret; -} - -int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root - *root, struct btrfs_path *path, u64 objectid, int mod) -{ - struct btrfs_key key; - int ins_len = mod < 0 ? -1 : 0; - int cow = mod != 0; - - key.objectid = objectid; - btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); - key.offset = 0; - return btrfs_search_slot(trans, root, &key, path, ins_len, cow); -} + return ret; +} diff -r 59fc7ac63e46 inode-map.c --- a/inode-map.c Fri Dec 21 16:25:35 2007 -0500 +++ b/inode-map.c Fri Jan 04 19:19:50 2008 +0800 @@ -16,13 +16,41 @@ * Boston, MA 021110-1307, USA. */ -#include -#include -#include "kerncompat.h" -#include "radix-tree.h" #include "ctree.h" #include "disk-io.h" #include "transaction.h" + +int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) +{ + struct btrfs_path *path; + int ret; + struct extent_buffer *l; + struct btrfs_key search_key; + struct btrfs_key found_key; + int slot; + + path = btrfs_alloc_path(); + BUG_ON(!path); + + search_key.objectid = (u64)-1; + search_key.offset = (u64)-1; + ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); + if (ret < 0) + goto error; + BUG_ON(ret == 0); + if (path->slots[0] > 0) { + slot = path->slots[0] - 1; + l = path->nodes[0]; + btrfs_item_key_to_cpu(l, &found_key, slot); + *objectid = found_key.objectid; + } else { + *objectid = BTRFS_FIRST_FREE_OBJECTID; + } + ret = 0; +error: + btrfs_free_path(path); + return ret; +} /* * walks the btree of allocated inodes and find a hole. @@ -31,40 +59,38 @@ int btrfs_find_free_objectid(struct btrf struct btrfs_root *root, u64 dirid, u64 *objectid) { - struct btrfs_path path; + struct btrfs_path *path; struct btrfs_key key; int ret; u64 hole_size = 0; int slot = 0; u64 last_ino = 0; int start_found; - struct btrfs_leaf *l; + struct extent_buffer *l; struct btrfs_key search_key; u64 search_start = dirid; - if (root->fs_info->last_inode_alloc_dirid == dirid) - search_start = root->fs_info->last_inode_alloc; - - if (search_start < BTRFS_FIRST_FREE_OBJECTID) - search_start = BTRFS_FIRST_FREE_OBJECTID; + path = btrfs_alloc_path(); + BUG_ON(!path); + search_start = root->last_inode_alloc; + search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID); search_key.objectid = search_start; - search_key.type = 0; search_key.offset = 0; - btrfs_init_path(&path); + btrfs_init_path(path); start_found = 0; - ret = btrfs_search_slot(trans, root, &search_key, &path, 0, 0); + ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0); if (ret < 0) goto error; - if (path.slots[0] > 0) - path.slots[0]--; + if (path->slots[0] > 0) + path->slots[0]--; while (1) { - l = &path.nodes[0]->leaf; - slot = path.slots[0]; - if (slot >= btrfs_header_nritems(&l->header)) { - ret = btrfs_next_leaf(root, &path); + l = path->nodes[0]; + slot = path->slots[0]; + if (slot >= btrfs_header_nritems(l)) { + ret = btrfs_next_leaf(root, path); if (ret == 0) continue; if (ret < 0) @@ -78,7 +104,7 @@ int btrfs_find_free_objectid(struct btrf last_ino : search_start; goto found; } - btrfs_disk_key_to_cpu(&key, &l->items[slot].key); + btrfs_item_key_to_cpu(l, &key, slot); if (key.objectid >= search_start) { if (start_found) { if (last_ino < search_start) @@ -92,16 +118,17 @@ int btrfs_find_free_objectid(struct btrf } start_found = 1; last_ino = key.objectid + 1; - path.slots[0]++; + path->slots[0]++; } // FIXME -ENOSPC found: - root->fs_info->last_inode_alloc = *objectid; - root->fs_info->last_inode_alloc_dirid = dirid; - btrfs_release_path(root, &path); + root->last_inode_alloc = *objectid; + btrfs_release_path(root, path); + btrfs_free_path(path); BUG_ON(*objectid < search_start); return 0; error: - btrfs_release_path(root, &path); + btrfs_release_path(root, path); + btrfs_free_path(path); return ret; } diff -r 59fc7ac63e46 kerncompat.h --- a/kerncompat.h Fri Dec 21 16:25:35 2007 -0500 +++ b/kerncompat.h Fri Jan 04 19:19:50 2008 +0800 @@ -67,10 +67,36 @@ struct vm_area_struct { unsigned long vm_end; struct vma_shared shared; }; + struct page { unsigned long index; }; +struct mutex { + unsigned long lock; +}; + +#define mutex_init(m) \ +do { \ + (m)->lock = 1; \ +} while (0) + +static inline void mutex_lock(struct mutex *m) +{ + m->lock--; +} + +static inline void mutex_unlock(struct mutex *m) +{ + m->lock++; +} + +static inline int mutex_is_locked(struct mutex *m) +{ + return (m->lock != 1); +} + +#define cond_resched() do { } while (0) #define preempt_enable() do { } while (0) #define preempt_disable() do { } while (0) @@ -112,7 +138,61 @@ static inline int test_bit(int nr, const return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); } +/* + * error pointer + */ +#define MAX_ERRNO 4095 +#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO) + +static inline void *ERR_PTR(long error) +{ + return (void *) error; +} + +static inline long PTR_ERR(const void *ptr) +{ + return (long) ptr; +} + +static inline long IS_ERR(const void *ptr) +{ + return IS_ERR_VALUE((unsigned long)ptr); +} + +/* + * max/min macro + */ +#define min(x,y) ({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + (void) (&_x == &_y); \ + _x < _y ? _x : _y; }) + +#define max(x,y) ({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + (void) (&_x == &_y); \ + _x > _y ? _x : _y; }) + +#define min_t(type,x,y) \ + ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) +#define max_t(type,x,y) \ + ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) + +/* + * printk + */ +#define printk(fmt, args...) fprintf(stderr, fmt, ##args) +#define KERN_CRIT "" + +/* + * kmalloc/kfree + */ +#define kmalloc(x, y) malloc(x) +#define kfree(x) free(x) + #define BUG_ON(c) do { if (c) abort(); } while (0) +#define WARN_ON(c) do { if (c) abort(); } while (0) #undef offsetof #ifdef __compiler_offsetof @@ -162,3 +242,7 @@ typedef u64 __bitwise __be64; #define le16_to_cpu(x) ((__force u16)(__le16)(x)) #endif #endif + +#ifndef noinline +#define noinline +#endif diff -r 59fc7ac63e46 mkfs.c --- a/mkfs.c Fri Dec 21 16:25:35 2007 -0500 +++ b/mkfs.c Fri Jan 04 19:19:50 2008 +0800 @@ -34,6 +34,7 @@ #include "ctree.h" #include "disk-io.h" #include "transaction.h" +#include "utils.h" #ifdef __CHECKER__ #define BLKGETSIZE64 0 @@ -66,133 +67,32 @@ static u64 parse_size(char *s) return atol(s) * mult; } -static int __make_root_dir(struct btrfs_trans_handle *trans, - struct btrfs_root *root, u64 objectid) -{ - int ret; - char buf[8]; - struct btrfs_key inode_map; - struct btrfs_inode_item inode_item; - - buf[0] = '.'; - buf[1] = '.'; - - inode_map.objectid = objectid; - btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY); - inode_map.offset = 0; - - memset(&inode_item, 0, sizeof(inode_item)); - btrfs_set_inode_generation(&inode_item, root->fs_info->generation); - btrfs_set_inode_size(&inode_item, 0); - btrfs_set_inode_nlink(&inode_item, 1); - btrfs_set_inode_nblocks(&inode_item, 0); - btrfs_set_inode_mode(&inode_item, S_IFDIR | 0555); - - if (root->fs_info->tree_root == root) - btrfs_set_super_root_dir(root->fs_info->disk_super, objectid); - - ret = btrfs_insert_inode(trans, root, objectid, &inode_item); - if (ret) - goto error; - - ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid); - if (ret) - goto error; - btrfs_set_root_dirid(&root->root_item, objectid); - ret = 0; -error: - return ret; -} - -static int make_block_groups(struct btrfs_trans_handle *trans, - struct btrfs_root *root) -{ - u64 group_size; - u64 total_bytes; - u64 cur_start; - int ret; - u64 nr = 0; - struct btrfs_block_group_cache *cache; - struct cache_tree *bg_cache = &root->fs_info->block_group_cache; - - root = root->fs_info->extent_root; - - /* first we bootstrap the things into cache */ - group_size = BTRFS_BLOCK_GROUP_SIZE; - cache = malloc(sizeof(*cache)); - cache->key.objectid = 0; - cache->key.offset = group_size; - cache->cache.start = 0; - cache->cache.size = group_size; - btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); - - memset(&cache->item, 0, sizeof(cache->item)); - btrfs_set_block_group_used(&cache->item, - btrfs_super_bytes_used(root->fs_info->disk_super)); - ret = insert_existing_cache_extent(bg_cache, &cache->cache); - BUG_ON(ret); - - total_bytes = btrfs_super_total_bytes(root->fs_info->disk_super); - cur_start = group_size; - while(cur_start < total_bytes) { - cache = malloc(sizeof(*cache)); - cache->key.objectid = cur_start; - cache->key.offset = group_size; - cache->cache.start = cur_start; - cache->cache.size = group_size; - btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); - memset(&cache->item, 0, sizeof(cache->item)); - if (nr % 3) - cache->item.flags |= BTRFS_BLOCK_GROUP_DATA; - - ret = insert_existing_cache_extent(bg_cache, &cache->cache); - BUG_ON(ret); - cur_start += group_size; - nr++; - } - /* then insert all the items */ - cur_start = 0; - while(cur_start < total_bytes) { - struct cache_extent *ce; - ce = find_first_cache_extent(bg_cache, cur_start); - BUG_ON(!ce); - cache = container_of(ce, struct btrfs_block_group_cache, - cache); - ret = btrfs_insert_block_group(trans, root, &cache->key, - &cache->item); - BUG_ON(ret); - cur_start += group_size; - } - return 0; -} - static int make_root_dir(int fd) { struct btrfs_root *root; - struct btrfs_super_block super; struct btrfs_trans_handle *trans; + struct btrfs_key location; int ret; - struct btrfs_key location; - - root = open_ctree_fd(fd, &super); + + root = open_ctree_fd(fd, 0); if (!root) { fprintf(stderr, "ctree init failed\n"); return -1; } trans = btrfs_start_transaction(root, 1); - ret = make_block_groups(trans, root); - ret = __make_root_dir(trans, root->fs_info->tree_root, + ret = btrfs_make_block_groups(trans, root); + ret = btrfs_make_root_dir(trans, root->fs_info->tree_root, BTRFS_ROOT_TREE_DIR_OBJECTID); if (ret) goto err; - ret = __make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID); + ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID); if (ret) goto err; memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location)); location.offset = (u64)-1; ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, "default", 7, - btrfs_super_root_dir(root->fs_info->disk_super), + btrfs_super_root_dir(&root->fs_info->super_copy), &location, BTRFS_FT_DIR); if (ret) goto err; @@ -203,152 +103,10 @@ static int make_root_dir(int fd) { if (ret) goto err; - btrfs_commit_transaction(trans, root, root->fs_info->disk_super); - ret = close_ctree(root, &super); + btrfs_commit_transaction(trans, root); + ret = close_ctree(root); err: return ret; -} - -int mkfs(int fd, char *pathname, u64 num_bytes, u32 nodesize, u32 leafsize, - u32 sectorsize, u32 stripesize) -{ - struct btrfs_super_block super; - struct btrfs_leaf *empty_leaf; - struct btrfs_root_item root_item; - struct btrfs_item item; - struct btrfs_extent_item extent_item; - struct btrfs_inode_item *inode_item; - char *block; - int ret; - u32 itemoff; - u32 start_block = BTRFS_SUPER_INFO_OFFSET; - u32 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize; - - btrfs_set_super_generation(&super, 1); - btrfs_set_super_bytenr(&super, start_block); - btrfs_set_super_root_level(&super, 0); - btrfs_set_super_root(&super, first_free); - strcpy((char *)(&super.magic), BTRFS_MAGIC); - -printf("blocksize is %d\n", leafsize); - btrfs_set_super_sectorsize(&super, sectorsize); - btrfs_set_super_leafsize(&super, leafsize); - btrfs_set_super_nodesize(&super, nodesize); - btrfs_set_super_stripesize(&super, stripesize); - - num_bytes = (num_bytes / sectorsize) * sectorsize; - btrfs_set_super_total_bytes(&super, num_bytes); - btrfs_set_super_bytes_used(&super, start_block + 3 * leafsize + - sectorsize); - uuid_generate(super.fsid); - - block = malloc(sectorsize); - memset(block, 0, sectorsize); - BUG_ON(sizeof(super) > sectorsize); - memcpy(block, &super, sizeof(super)); - ret = pwrite(fd, block, sectorsize, BTRFS_SUPER_INFO_OFFSET); - BUG_ON(ret != sectorsize); - - /* create the tree of root objects */ - empty_leaf = malloc(leafsize); - memset(empty_leaf, 0, leafsize); - btrfs_set_header_bytenr(&empty_leaf->header, first_free); - btrfs_set_header_nritems(&empty_leaf->header, 2); - btrfs_set_header_generation(&empty_leaf->header, 1); - btrfs_set_header_owner(&empty_leaf->header, BTRFS_ROOT_TREE_OBJECTID); - memcpy(empty_leaf->header.fsid, super.fsid, - sizeof(empty_leaf->header.fsid)); - - /* create the items for the root tree */ - inode_item = &root_item.inode; - memset(inode_item, 0, sizeof(*inode_item)); - btrfs_set_inode_generation(inode_item, 1); - btrfs_set_inode_size(inode_item, 3); - btrfs_set_inode_nlink(inode_item, 1); - btrfs_set_inode_nblocks(inode_item, 1); - btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); - - // memset(&root_item, 0, sizeof(root_item)); - btrfs_set_root_dirid(&root_item, 0); - btrfs_set_root_refs(&root_item, 1); - btrfs_set_disk_key_offset(&item.key, 0); - btrfs_set_item_size(&item, sizeof(root_item)); - btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY); - - itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item); - btrfs_set_root_bytenr(&root_item, first_free + leafsize); - root_item.level = 0; - btrfs_set_item_offset(&item, itemoff); - btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID); - memcpy(empty_leaf->items, &item, sizeof(item)); - memcpy(btrfs_leaf_data(empty_leaf) + itemoff, - &root_item, sizeof(root_item)); - - btrfs_set_root_bytenr(&root_item, first_free + leafsize * 2); - btrfs_set_root_bytes_used(&root_item, 1); - itemoff = itemoff - sizeof(root_item); - btrfs_set_item_offset(&item, itemoff); - btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID); - memcpy(empty_leaf->items + 1, &item, sizeof(item)); - memcpy(btrfs_leaf_data(empty_leaf) + itemoff, - &root_item, sizeof(root_item)); - ret = pwrite(fd, empty_leaf, leafsize, first_free); - - /* create the items for the extent tree */ - btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize); - btrfs_set_header_nritems(&empty_leaf->header, 4); - - /* item1, reserve blocks 0-16 */ - btrfs_set_disk_key_objectid(&item.key, 0); - btrfs_set_disk_key_offset(&item.key, first_free); - btrfs_set_disk_key_type(&item.key, 0); - btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY); - itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - - sizeof(struct btrfs_extent_item); - btrfs_set_item_offset(&item, itemoff); - btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item)); - btrfs_set_extent_refs(&extent_item, 1); - memcpy(empty_leaf->items, &item, sizeof(item)); - memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item), - &extent_item, btrfs_item_size(&item)); - - /* item2, give block 17 to the root */ - btrfs_set_disk_key_objectid(&item.key, first_free); - btrfs_set_disk_key_offset(&item.key, leafsize); - itemoff = itemoff - sizeof(struct btrfs_extent_item); - btrfs_set_item_offset(&item, itemoff); - memcpy(empty_leaf->items + 1, &item, sizeof(item)); - memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item), - &extent_item, btrfs_item_size(&item)); - - /* item3, give block 18 to the extent root */ - btrfs_set_disk_key_objectid(&item.key, first_free + leafsize); - btrfs_set_disk_key_offset(&item.key, leafsize); - itemoff = itemoff - sizeof(struct btrfs_extent_item); - btrfs_set_item_offset(&item, itemoff); - memcpy(empty_leaf->items + 2, &item, sizeof(item)); - memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item), - &extent_item, btrfs_item_size(&item)); - - /* item4, give block 19 to the FS root */ - btrfs_set_disk_key_objectid(&item.key, first_free + leafsize * 2); - btrfs_set_disk_key_offset(&item.key, leafsize); - itemoff = itemoff - sizeof(struct btrfs_extent_item); - btrfs_set_item_offset(&item, itemoff); - memcpy(empty_leaf->items + 3, &item, sizeof(item)); - memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item), - &extent_item, btrfs_item_size(&item)); - ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize); - if (ret != leafsize) - return -1; - - /* finally create the FS root */ - btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize * 2); - btrfs_set_header_nritems(&empty_leaf->header, 0); - ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize * 2); - if (ret != leafsize) - return -1; - return 0; } u64 device_size(int fd, struct stat *st) @@ -384,8 +142,8 @@ int main(int ac, char **av) u32 sectorsize = 4096; u32 nodesize = 16 * 1024; u32 stripesize = 4096; + u64 blocks[4]; char *buf = malloc(sectorsize); - char *realpath_name; while(1) { int c; @@ -459,9 +217,10 @@ int main(int ac, char **av) exit(1); } } - realpath_name = realpath(file, NULL); - ret = mkfs(fd, realpath_name, block_count, nodesize, leafsize, - sectorsize, stripesize); + for (i = 0; i < 4; i++) + blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i; + ret = make_btrfs(fd, blocks, block_count, nodesize, leafsize, + sectorsize, stripesize); if (ret) { fprintf(stderr, "error during mkfs %d\n", ret); exit(1); diff -r 59fc7ac63e46 print-tree.c --- a/print-tree.c Fri Dec 21 16:25:35 2007 -0500 +++ b/print-tree.c Fri Jan 04 19:19:50 2008 +0800 @@ -23,50 +23,62 @@ #include "ctree.h" #include "disk-io.h" -static int print_dir_item(struct btrfs_item *item, +static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item, struct btrfs_dir_item *di) { u32 total; u32 cur = 0; u32 len; - total = btrfs_item_size(item); + u32 name_len; + u32 data_len; + char namebuf[BTRFS_NAME_LEN]; + struct btrfs_disk_key location; + + total = btrfs_item_size(eb, item); while(cur < total) { + btrfs_dir_item_key(eb, di, &location); printf("\t\tdir index %llu type %u\n", - (unsigned long long)btrfs_disk_key_objectid(&di->location), - btrfs_dir_type(di)); - printf("\t\tname: %.*s\n", - btrfs_dir_name_len(di),(char *)(di + 1)); - if (btrfs_dir_data_len(di)) - printf("\t\tdata: %.*s\n", btrfs_dir_data_len(di), - (char *)((char *)(di + 1) + btrfs_dir_name_len(di))); - len = sizeof(*di) + btrfs_dir_name_len(di) + btrfs_dir_data_len(di); + (unsigned long long)btrfs_disk_key_objectid(&location), + btrfs_dir_type(eb, di)); + name_len = btrfs_dir_name_len(eb, di); + data_len = btrfs_dir_data_len(eb, di); + len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf); + read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len); + printf("\t\tnamelen %u datalen %u name: %.*s\n", + name_len, data_len, len, namebuf); + len = sizeof(*di) + name_len + data_len; di = (struct btrfs_dir_item *)((char *)di + len); cur += len; } return 0; } -static int print_inode_ref_item(struct btrfs_item *item, +static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item, struct btrfs_inode_ref *ref) { u32 total; u32 cur = 0; u32 len; - total = btrfs_item_size(item); + u32 name_len; + char namebuf[BTRFS_NAME_LEN]; + total = btrfs_item_size(eb, item); while(cur < total) { - len = btrfs_inode_ref_name_len(ref); - printf("\t\tinode ref name: %.*s\n", len, (char *)(ref + 1)); - len += sizeof(*ref); + name_len = btrfs_inode_ref_name_len(eb, ref); + len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf); + read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len); + printf("\t\tinode ref namelen %u name: %.*s\n", + name_len, len, namebuf); + len = sizeof(*ref) + name_len; ref = (struct btrfs_inode_ref *)((char *)ref + len); cur += len; } return 0; } -void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l) +void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l) { int i; - u32 nr = btrfs_header_nritems(&l->header); + char *str; struct btrfs_item *item; struct btrfs_extent_item *ei; struct btrfs_root_item *ri; @@ -77,163 +89,167 @@ void btrfs_print_leaf(struct btrfs_root struct btrfs_block_group_item *bi; struct btrfs_extent_ref *ref; struct btrfs_inode_ref *iref; + struct btrfs_disk_key disk_key; + struct btrfs_root_item root_item; + struct btrfs_block_group_item bg_item; + u32 nr = btrfs_header_nritems(l); u32 type; printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n", - (unsigned long long)btrfs_header_bytenr(&l->header), nr, + (unsigned long long)btrfs_header_bytenr(l), nr, btrfs_leaf_free_space(root, l), - (unsigned long long)btrfs_header_generation(&l->header), - (unsigned long long)btrfs_header_owner(&l->header)); + (unsigned long long)btrfs_header_generation(l), + (unsigned long long)btrfs_header_owner(l)); fflush(stdout); for (i = 0 ; i < nr ; i++) { - item = l->items + i; - type = btrfs_disk_key_type(&item->key); + item = btrfs_item_nr(l, i); + btrfs_item_key(l, &disk_key, i); + type = btrfs_disk_key_type(&disk_key); printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n", i, - (unsigned long long)btrfs_disk_key_objectid(&item->key), - btrfs_disk_key_type(&item->key), - (unsigned long long)btrfs_disk_key_offset(&item->key), - btrfs_item_offset(item), - btrfs_item_size(item)); + (unsigned long long)btrfs_disk_key_objectid(&disk_key), + btrfs_disk_key_type(&disk_key), + (unsigned long long)btrfs_disk_key_offset(&disk_key), + btrfs_item_offset(l, item), + btrfs_item_size(l, item)); switch (type) { case BTRFS_INODE_ITEM_KEY: ii = btrfs_item_ptr(l, i, struct btrfs_inode_item); printf("\t\tinode generation %llu size %llu block group %llu mode %o links %u\n", - (unsigned long long)btrfs_inode_generation(ii), - (unsigned long long)btrfs_inode_size(ii), - (unsigned long long)btrfs_inode_block_group(ii), - btrfs_inode_mode(ii), - btrfs_inode_nlink(ii)); + (unsigned long long)btrfs_inode_generation(l, ii), + (unsigned long long)btrfs_inode_size(l, ii), + (unsigned long long)btrfs_inode_block_group(l,ii), + btrfs_inode_mode(l, ii), + btrfs_inode_nlink(l, ii)); break; case BTRFS_INODE_REF_KEY: iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref); - print_inode_ref_item(l->items + i, iref); + print_inode_ref_item(l, item, iref); break; case BTRFS_DIR_ITEM_KEY: + case BTRFS_DIR_INDEX_KEY: + case BTRFS_XATTR_ITEM_KEY: di = btrfs_item_ptr(l, i, struct btrfs_dir_item); - print_dir_item(l->items + i, di); - break; - case BTRFS_XATTR_ITEM_KEY: - case BTRFS_DIR_INDEX_KEY: - di = btrfs_item_ptr(l, i, struct btrfs_dir_item); - print_dir_item(l->items + i, di); + print_dir_item(l, item, di); break; case BTRFS_ROOT_ITEM_KEY: ri = btrfs_item_ptr(l, i, struct btrfs_root_item); + read_extent_buffer(l, &root_item, (unsigned long)ri, sizeof(root_item)); printf("\t\troot data bytenr %llu level %d dirid %llu refs %u\n", - (unsigned long long)btrfs_root_bytenr(ri), - ri->level, - (unsigned long long)btrfs_root_dirid(ri), - btrfs_root_refs(ri)); - if (1 || btrfs_root_refs(ri) == 0) { + (unsigned long long)btrfs_root_bytenr(&root_item), + btrfs_root_level(&root_item), + (unsigned long long)btrfs_root_dirid(&root_item), + btrfs_root_refs(&root_item)); + if (1 || btrfs_root_refs(&root_item) == 0) { struct btrfs_key drop_key; btrfs_disk_key_to_cpu(&drop_key, - &ri->drop_progress); + &root_item.drop_progress); printf("\t\tdrop key %Lu %x %Lu level %d\n", (unsigned long long)drop_key.objectid, drop_key.type, (unsigned long long)drop_key.offset, - ri->drop_level); + root_item.drop_level); } break; case BTRFS_EXTENT_ITEM_KEY: ei = btrfs_item_ptr(l, i, struct btrfs_extent_item); printf("\t\textent data refs %u\n", - btrfs_extent_refs(ei)); + btrfs_extent_refs(l, ei)); break; case BTRFS_EXTENT_REF_KEY: ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref); printf("\t\textent back ref root %llu gen %llu " "owner %llu offset %llu\n", - (unsigned long long)btrfs_ref_root(ref), - (unsigned long long)btrfs_ref_generation(ref), - (unsigned long long)btrfs_ref_objectid(ref), - (unsigned long long)btrfs_ref_offset(ref)); + (unsigned long long)btrfs_ref_root(l, ref), + (unsigned long long)btrfs_ref_generation(l, ref), + (unsigned long long)btrfs_ref_objectid(l, ref), + (unsigned long long)btrfs_ref_offset(l, ref)); break; case BTRFS_CSUM_ITEM_KEY: - ci = btrfs_item_ptr(l, i, - struct btrfs_csum_item); + ci = btrfs_item_ptr(l, i, struct btrfs_csum_item); printf("\t\tcsum item\n"); break; case BTRFS_EXTENT_DATA_KEY: fi = btrfs_item_ptr(l, i, struct btrfs_file_extent_item); - if (btrfs_file_extent_type(fi) == + if (btrfs_file_extent_type(l, fi) == BTRFS_FILE_EXTENT_INLINE) { printf("\t\tinline extent data size %u\n", - btrfs_file_extent_inline_len(l->items + i)); + btrfs_file_extent_inline_len(l, item)); break; } printf("\t\textent data disk byte %llu nr %llu\n", - (unsigned long long)btrfs_file_extent_disk_bytenr(fi), - (unsigned long long)btrfs_file_extent_disk_num_bytes(fi)); + (unsigned long long)btrfs_file_extent_disk_bytenr(l, fi), + (unsigned long long)btrfs_file_extent_disk_num_bytes(l, fi)); printf("\t\textent data offset %llu nr %llu\n", - (unsigned long long)btrfs_file_extent_offset(fi), - (unsigned long long)btrfs_file_extent_num_bytes(fi)); + (unsigned long long)btrfs_file_extent_offset(l, fi), + (unsigned long long)btrfs_file_extent_num_bytes(l, fi)); break; case BTRFS_BLOCK_GROUP_ITEM_KEY: bi = btrfs_item_ptr(l, i, struct btrfs_block_group_item); + read_extent_buffer(l, &bg_item, (unsigned long)bi, + sizeof(bg_item)); printf("\t\tblock group used %llu flags %x\n", - (unsigned long long)btrfs_block_group_used(bi), - bi->flags); + (unsigned long long)btrfs_block_group_used(&bg_item), + bg_item.flags); break; case BTRFS_STRING_ITEM_KEY: - printf("\t\titem data %.*s\n", btrfs_item_size(item), - btrfs_leaf_data(l) + btrfs_item_offset(item)); + /* dirty, but it's simple */ + str = l->data + btrfs_item_ptr_offset(l, i); + printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str); break; }; fflush(stdout); } } -void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t) +void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb) { int i; u32 nr; - struct btrfs_node *c; u32 size; - - if (!t) + struct btrfs_key key; + + if (!eb) return; - c = &t->node; - nr = btrfs_header_nritems(&c->header); - if (btrfs_is_leaf(c)) { - btrfs_print_leaf(root, (struct btrfs_leaf *)c); + nr = btrfs_header_nritems(eb); + if (btrfs_is_leaf(eb)) { + btrfs_print_leaf(root, eb); return; } printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n", - (unsigned long long)t->bytenr, - btrfs_header_level(&c->header), nr, + (unsigned long long)eb->start, + btrfs_header_level(eb), nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr, - (unsigned long long)btrfs_header_generation(&c->header), - (unsigned long long)btrfs_header_owner(&c->header)); + (unsigned long long)btrfs_header_generation(eb), + (unsigned long long)btrfs_header_owner(eb)); fflush(stdout); - size = btrfs_level_size(root, btrfs_header_level(&c->header) - 1); + size = btrfs_level_size(root, btrfs_header_level(eb) - 1); for (i = 0; i < nr; i++) { - u64 blocknr = btrfs_node_blockptr(c, i); + u64 blocknr = btrfs_node_blockptr(eb, i); + btrfs_item_key_to_cpu(eb, &key, i); printf("\tkey %d (%llu %x %llu) block %llu (%llu) gen %llu\n", i, - (unsigned long long)c->ptrs[i].key.objectid, - c->ptrs[i].key.type, - (unsigned long long)c->ptrs[i].key.offset, + (unsigned long long)key.objectid, + key.type, + (unsigned long long)key.offset, (unsigned long long)blocknr, (unsigned long long)blocknr / size, - (unsigned long long)btrfs_node_ptr_generation(c, i)); + (unsigned long long)btrfs_node_ptr_generation(eb, i)); fflush(stdout); } for (i = 0; i < nr; i++) { - struct btrfs_buffer *next_buf = read_tree_block(root, - btrfs_node_blockptr(c, i), - size); - struct btrfs_node *next = &next_buf->node; + struct extent_buffer *next = read_tree_block(root, + btrfs_node_blockptr(eb, i), + size); if (btrfs_is_leaf(next) && - btrfs_header_level(&c->header) != 1) + btrfs_header_level(eb) != 1) BUG(); - if (btrfs_header_level(&next->header) != - btrfs_header_level(&c->header) - 1) + if (btrfs_header_level(next) != + btrfs_header_level(eb) - 1) BUG(); - btrfs_print_tree(root, next_buf); - btrfs_block_release(root, next_buf); - } -} - + btrfs_print_tree(root, next); + free_extent_buffer(next); + } +} + diff -r 59fc7ac63e46 print-tree.h --- a/print-tree.h Fri Dec 21 16:25:35 2007 -0500 +++ b/print-tree.h Fri Jan 04 19:19:50 2008 +0800 @@ -18,6 +18,6 @@ #ifndef __PRINT_TREE_ #define __PRINT_TREE_ -void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l); -void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t); +void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l); +void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *t); #endif diff -r 59fc7ac63e46 root-tree.c --- a/root-tree.c Fri Dec 21 16:25:35 2007 -0500 +++ b/root-tree.c Fri Jan 04 19:19:50 2008 +0800 @@ -16,20 +16,18 @@ * Boston, MA 021110-1307, USA. */ -#include -#include -#include "kerncompat.h" -#include "radix-tree.h" #include "ctree.h" +#include "transaction.h" #include "disk-io.h" #include "print-tree.h" int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct btrfs_root_item *item, struct btrfs_key *key) { - struct btrfs_path path; + struct btrfs_path *path; struct btrfs_key search_key; - struct btrfs_leaf *l; + struct btrfs_key found_key; + struct extent_buffer *l; int ret; int slot; @@ -37,24 +35,28 @@ int btrfs_find_last_root(struct btrfs_ro search_key.type = (u8)-1; search_key.offset = (u64)-1; - btrfs_init_path(&path); - ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0); - if (ret < 0) - goto out; + path = btrfs_alloc_path(); + BUG_ON(!path); + ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); + if (ret < 0) + goto out; + BUG_ON(ret == 0); - l = &path.nodes[0]->leaf; - BUG_ON(path.slots[0] == 0); - slot = path.slots[0] - 1; - if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) { + l = path->nodes[0]; + BUG_ON(path->slots[0] == 0); + slot = path->slots[0] - 1; + btrfs_item_key_to_cpu(l, &found_key, slot); + if (found_key.objectid != objectid) { ret = 1; goto out; } - memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), - sizeof(*item)); - btrfs_disk_key_to_cpu(key, &l->items[slot].key); - btrfs_release_path(root, &path); + read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot), + sizeof(*item)); + memcpy(key, &found_key, sizeof(found_key)); ret = 0; out: + btrfs_release_path(root, path); + btrfs_free_path(path); return ret; } @@ -62,22 +64,26 @@ int btrfs_update_root(struct btrfs_trans *root, struct btrfs_key *key, struct btrfs_root_item *item) { - struct btrfs_path path; - struct btrfs_leaf *l; - int ret; - int slot; - - btrfs_init_path(&path); - ret = btrfs_search_slot(trans, root, key, &path, 0, 1); + struct btrfs_path *path; + struct extent_buffer *l; + int ret; + int slot; + unsigned long ptr; + + path = btrfs_alloc_path(); + BUG_ON(!path); + ret = btrfs_search_slot(trans, root, key, path, 0, 1); if (ret < 0) goto out; BUG_ON(ret != 0); - l = &path.nodes[0]->leaf; - slot = path.slots[0]; - memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item, - sizeof(*item)); -out: - btrfs_release_path(root, &path); + l = path->nodes[0]; + slot = path->slots[0]; + ptr = btrfs_item_ptr_offset(l, slot); + write_extent_buffer(l, item, ptr, sizeof(*item)); + btrfs_mark_buffer_dirty(path->nodes[0]); +out: + btrfs_release_path(root, path); + btrfs_free_path(path); return ret; } @@ -87,23 +93,108 @@ int btrfs_insert_root(struct btrfs_trans { int ret; ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); - BUG_ON(ret); - return ret; -} + return ret; +} + +#if 0 +int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, + struct btrfs_root *latest) +{ + struct btrfs_root *dead_root; + struct btrfs_item *item; + struct btrfs_root_item *ri; + struct btrfs_key key; + struct btrfs_path *path; + int ret; + u32 nritems; + struct extent_buffer *leaf; + int slot; + + key.objectid = objectid; + btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); + key.offset = 0; + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + if (ret < 0) + goto err; + while(1) { + leaf = path->nodes[0]; + nritems = btrfs_header_nritems(leaf); + slot = path->slots[0]; + if (slot >= nritems) { + ret = btrfs_next_leaf(root, path); + if (ret) + break; + leaf = path->nodes[0]; + nritems = btrfs_header_nritems(leaf); + slot = path->slots[0]; + } + item = btrfs_item_nr(leaf, slot); + btrfs_item_key_to_cpu(leaf, &key, slot); + if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) + goto next; + + if (key.objectid < objectid) + goto next; + + if (key.objectid > objectid) + break; + + ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); + if (btrfs_disk_root_refs(leaf, ri) != 0) + goto next; + + dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key); + if (IS_ERR(dead_root)) { + ret = PTR_ERR(dead_root); + goto err; + } + + ret = btrfs_add_dead_root(dead_root, latest, + &root->fs_info->dead_roots); + if (ret) + goto err; +next: + slot++; + path->slots[0]++; + } + ret = 0; +err: + btrfs_free_path(path); + return ret; +} +#endif int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_key *key) { - struct btrfs_path path; - int ret; - - btrfs_init_path(&path); - ret = btrfs_search_slot(trans, root, key, &path, -1, 1); - if (ret < 0) - goto out; + struct btrfs_path *path; + int ret; + u32 refs; + struct btrfs_root_item *ri; + struct extent_buffer *leaf; + + path = btrfs_alloc_path(); + BUG_ON(!path); + ret = btrfs_search_slot(trans, root, key, path, -1, 1); + if (ret < 0) + goto out; + if (ret) { +btrfs_print_leaf(root, path->nodes[0]); +printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset); + + } BUG_ON(ret != 0); - ret = btrfs_del_item(trans, root, &path); -out: - btrfs_release_path(root, &path); - return ret; -} + leaf = path->nodes[0]; + ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); + + refs = btrfs_disk_root_refs(leaf, ri); + BUG_ON(refs != 0); + ret = btrfs_del_item(trans, root, path); +out: + btrfs_release_path(root, path); + btrfs_free_path(path); + return ret; +} diff -r 59fc7ac63e46 transaction.h --- a/transaction.h Fri Dec 21 16:25:35 2007 -0500 +++ b/transaction.h Fri Jan 04 19:19:50 2008 +0800 @@ -21,8 +21,11 @@ struct btrfs_trans_handle { u64 transid; + u64 alloc_exclude_start; + u64 alloc_exclude_nr; unsigned long blocks_reserved; unsigned long blocks_used; + struct btrfs_block_group_cache *block_group; }; static inline struct btrfs_trans_handle * @@ -30,11 +33,20 @@ btrfs_start_transaction(struct btrfs_roo { struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_trans_handle *h = malloc(sizeof(*h)); + + BUG_ON(root->commit_root); + BUG_ON(fs_info->running_transaction); fs_info->running_transaction = h; fs_info->generation++; h->transid = fs_info->generation; + h->alloc_exclude_start = 0; + h->alloc_exclude_nr = 0; h->blocks_reserved = num_blocks; h->blocks_used = 0; + h->block_group = NULL; + root->last_trans = h->transid; + root->commit_root = root->node; + extent_buffer_get(root->node); return h; } diff -r 59fc7ac63e46 utils.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.c Fri Jan 04 19:19:50 2008 +0800 @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2007 Oracle. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#define _XOPEN_SOURCE 600 +#define __USE_XOPEN2K +#include +#include +#include +#include +#include +#include +#include +#include "kerncompat.h" +#include "radix-tree.h" +#include "ctree.h" +#include "disk-io.h" +#include "transaction.h" +#include "crc32c.h" +#include "utils.h" + +static u64 reference_root_table[4] = { + [0] = 0, + [1] = BTRFS_ROOT_TREE_OBJECTID, + [2] = BTRFS_EXTENT_TREE_OBJECTID, + [3] = BTRFS_FS_TREE_OBJECTID, +}; + +int make_btrfs(int fd, u64 blocks[4], u64 num_bytes, u32 nodesize, + u32 leafsize, u32 sectorsize, u32 stripesize) +{ + struct btrfs_super_block super; + struct extent_buffer *buf; + struct btrfs_root_item root_item; + struct btrfs_disk_key disk_key; + struct btrfs_extent_ref *extent_ref; + struct btrfs_extent_item *extent_item; + struct btrfs_inode_item *inode_item; + int i; + int ret; + u32 itemoff; + u32 nritems = 0; + u64 hash; + u64 first_free; + u64 ref_gen; + u64 ref_root; + + first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1; + first_free &= ~((u64)sectorsize - 1); + + num_bytes = (num_bytes / sectorsize) * sectorsize; + uuid_generate(super.fsid); + btrfs_set_super_bytenr(&super, blocks[0]); + strcpy((char *)(&super.magic), BTRFS_MAGIC); + btrfs_set_super_generation(&super, 1); + btrfs_set_super_root(&super, blocks[1]); + btrfs_set_super_total_bytes(&super, num_bytes); + btrfs_set_super_bytes_used(&super, first_free + 3 * leafsize); + btrfs_set_super_root_dir(&super, 0); + btrfs_set_super_sectorsize(&super, sectorsize); + btrfs_set_super_leafsize(&super, leafsize); + btrfs_set_super_nodesize(&super, nodesize); + btrfs_set_super_stripesize(&super, stripesize); + btrfs_set_super_root_level(&super, 0); + + buf = malloc(sizeof(*buf) + max(sectorsize, leafsize)); + + BUG_ON(sizeof(super) > sectorsize); + memset(buf->data, 0, sectorsize); + memcpy(buf->data, &super, sizeof(super)); + ret = pwrite(fd, buf->data, sectorsize, blocks[0]); + BUG_ON(ret != sectorsize); + + /* create the tree of root objects */ + memset(buf->data, 0, leafsize); + btrfs_set_header_bytenr(buf, blocks[1]); + btrfs_set_header_nritems(buf, 2); + btrfs_set_header_generation(buf, 1); + btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID); + write_extent_buffer(buf, super.fsid, (unsigned long) + btrfs_header_fsid(buf), BTRFS_FSID_SIZE); + + /* create the items for the root tree */ + memset(&root_item, 0, sizeof(root_item)); + inode_item = &root_item.inode; + btrfs_set_stack_inode_generation(inode_item, 1); + btrfs_set_stack_inode_size(inode_item, 3); + btrfs_set_stack_inode_nlink(inode_item, 1); + btrfs_set_stack_inode_nblocks(inode_item, 1); + btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); + btrfs_set_root_refs(&root_item, 1); + btrfs_set_root_used(&root_item, leafsize); + + memset(&disk_key, 0, sizeof(disk_key)); + btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY); + btrfs_set_disk_key_offset(&disk_key, 0); + + itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item); + btrfs_set_root_bytenr(&root_item, blocks[2]); + btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID); + btrfs_set_item_key(buf, &disk_key, 0); + btrfs_set_item_offset(buf, btrfs_item_nr(buf, 0), itemoff); + btrfs_set_item_size(buf, btrfs_item_nr(buf, 0), sizeof(root_item)); + write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf, 0), + sizeof(root_item)); + + itemoff = itemoff - sizeof(root_item); + btrfs_set_root_bytenr(&root_item, blocks[3]); + btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID); + btrfs_set_item_key(buf, &disk_key, 1); + btrfs_set_item_offset(buf, btrfs_item_nr(buf, 1), itemoff); + btrfs_set_item_size(buf, btrfs_item_nr(buf, 1), sizeof(root_item)); + write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf, 1), + sizeof(root_item)); + ret = pwrite(fd, buf->data, leafsize, blocks[1]); + BUG_ON(ret != leafsize); + + /* create the items for the extent tree */ + itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - + sizeof(struct btrfs_extent_item); + btrfs_set_disk_key_objectid(&disk_key, 0); + btrfs_set_disk_key_offset(&disk_key, first_free); + btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY); + btrfs_set_item_key(buf, &disk_key, nritems); + btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), itemoff); + btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), + sizeof(struct btrfs_extent_item)); + extent_item = btrfs_item_ptr(buf, nritems, struct btrfs_extent_item); + btrfs_set_extent_refs(buf, extent_item, 1); + nritems++; + + for (i = 0; i < 4; i++) { + if (blocks[i] < first_free) { + BUG_ON(i > 0); + continue; + } + /* create extent item */ + itemoff = itemoff - sizeof(struct btrfs_extent_item); + btrfs_set_disk_key_objectid(&disk_key, blocks[i]); + btrfs_set_disk_key_offset(&disk_key, leafsize); + btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_ITEM_KEY); + btrfs_set_item_key(buf, &disk_key, nritems); + btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), + itemoff); + btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), + sizeof(struct btrfs_extent_item)); + extent_item = btrfs_item_ptr(buf, nritems, + struct btrfs_extent_item); + btrfs_set_extent_refs(buf, extent_item, 1); + nritems++; + + /* create extent ref */ + ref_root = reference_root_table[i]; + if (ref_root == BTRFS_FS_TREE_OBJECTID) + ref_gen = 1; + else + ref_gen = 0; + + hash = btrfs_hash_extent_ref(ref_root, ref_gen, 0, 0); + itemoff = itemoff - sizeof(struct btrfs_extent_ref); + btrfs_set_disk_key_objectid(&disk_key, blocks[i]); + btrfs_set_disk_key_offset(&disk_key, hash); + btrfs_set_disk_key_type(&disk_key, BTRFS_EXTENT_REF_KEY); + btrfs_set_item_key(buf, &disk_key, nritems); + btrfs_set_item_offset(buf, btrfs_item_nr(buf, nritems), + itemoff); + btrfs_set_item_size(buf, btrfs_item_nr(buf, nritems), + sizeof(struct btrfs_extent_ref)); + extent_ref = btrfs_item_ptr(buf, nritems, + struct btrfs_extent_ref); + btrfs_set_ref_root(buf, extent_ref, ref_root); + btrfs_set_ref_generation(buf, extent_ref, ref_gen); + btrfs_set_ref_objectid(buf, extent_ref, 0); + btrfs_set_ref_offset(buf, extent_ref, 0); + nritems++; + } + btrfs_set_header_bytenr(buf, blocks[2]); + btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID); + btrfs_set_header_nritems(buf, nritems); + ret = pwrite(fd, buf->data, leafsize, blocks[2]); + BUG_ON(ret != leafsize); + + /* finally create the FS root */ + btrfs_set_header_bytenr(buf, blocks[3]); + btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID); + btrfs_set_header_nritems(buf, 0); + ret = pwrite(fd, buf->data, leafsize, blocks[3]); + BUG_ON(ret != leafsize); + + free(buf); + return 0; +} + +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid) +{ + int ret; + struct btrfs_inode_item inode_item; + + memset(&inode_item, 0, sizeof(inode_item)); + btrfs_set_stack_inode_generation(&inode_item, trans->transid); + btrfs_set_stack_inode_size(&inode_item, 0); + btrfs_set_stack_inode_nlink(&inode_item, 1); + btrfs_set_stack_inode_nblocks(&inode_item, 1); + btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0555); + + if (root->fs_info->tree_root == root) + btrfs_set_super_root_dir(&root->fs_info->super_copy, objectid); + + ret = btrfs_insert_inode(trans, root, objectid, &inode_item); + if (ret) + goto error; + + ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid); + if (ret) + goto error; + + btrfs_set_root_dirid(&root->root_item, objectid); + ret = 0; +error: + return ret; +} + diff -r 59fc7ac63e46 utils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.h Fri Jan 04 19:19:50 2008 +0800 @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2007 Oracle. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#ifndef __UTILS__ +#define __UTILS__ +int make_btrfs(int fd, u64 new_blocks[4], u64 num_bytes, u32 nodesize, + u32 leafsize, u32 sectorsize, u32 stripesize); +int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid); +#endif +