Commit e089f05c authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: transaction handles everywhere

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 88fd146c
CC=gcc
CFLAGS = -g -Wall
CFLAGS = -g -Wall -Werror
headers = radix-tree.h ctree.h disk-io.h kerncompat.h print-tree.h list.h
objects = ctree.o disk-io.o radix-tree.o mkfs.o extent-tree.o print-tree.o \
root-tree.o dir-item.o hash.o file-item.o inode-item.o
......@@ -14,7 +14,7 @@ check=sparse $(CHECKFLAGS)
$(check) $<
$(CC) $(CFLAGS) -c $<
all: tester debug-tree quick-test dir-test
all: tester debug-tree quick-test dir-test tags
debug-tree: $(objects) debug-tree.o
gcc $(CFLAGS) -o debug-tree $(objects) debug-tree.o
......
......@@ -8,6 +8,8 @@
* Add block mapping tree (simple dm layer)
* Add simple tree locking (semaphore per tree)
* Make allocator smarter
* Only pin blocks allocated in this transaction
* Add transaction handles
* Do actual block accounting
* Check compat and incompat flags on the inode
* Port into the kernel
......
This diff is collapsed.
......@@ -4,6 +4,8 @@
#include "list.h"
#include "kerncompat.h"
struct btrfs_trans_handle;
#define BTRFS_MAGIC "_BtRfS_M"
#define BTRFS_ROOT_TREE_OBJECTID 1
......@@ -200,6 +202,7 @@ struct btrfs_root {
struct btrfs_root_item root_item;
struct btrfs_key root_key;
u32 blocksize;
struct btrfs_trans_handle *running_transaction;
};
/* the lower bits in the key flags defines the item type */
......@@ -656,34 +659,46 @@ static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
((type *)(btrfs_leaf_data(leaf) + \
btrfs_item_offset((leaf)->items + (slot))))
struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks,
int pin);
int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_path *p, int ins_len, int cow);
struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_buffer *buf);
int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 blocknr, u64 num_blocks, int pin);
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);
void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
void btrfs_init_path(struct btrfs_path *p);
int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
void *data, u32 data_size);
int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
struct btrfs_key *cpu_key, u32 data_size);
int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_path *path);
int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, void *data, 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);
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_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
int btrfs_finish_extent_commit(struct btrfs_root *root);
int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_root_item *item);
int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_root_item *item);
int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
struct btrfs_root_item *item, struct btrfs_key *key);
int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
u64 dir, u64 objectid, u8 type);
int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
u64 dir, char *name, int name_len, int mod);
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);
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
*root, struct btrfs_key *key, struct btrfs_root_item
*item);
int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, struct btrfs_root_item
*item);
int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
btrfs_root_item *item, struct btrfs_key *key);
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, char *name, int name_len, u64 dir, u64
objectid, u8 type);
int 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);
int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
char *name, int name_len);
#endif
......@@ -5,6 +5,7 @@
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
int main(int ac, char **av) {
struct btrfs_super_block super;
......
......@@ -5,9 +5,11 @@
#include "ctree.h"
#include "disk-io.h"
#include "hash.h"
#include "transaction.h"
int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
u64 dir, u64 objectid, u8 type)
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, char *name, int name_len, u64 dir, u64
objectid, u8 type)
{
int ret = 0;
struct btrfs_path path;
......@@ -23,7 +25,7 @@ int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
BUG_ON(ret);
btrfs_init_path(&path);
data_size = sizeof(*dir_item) + name_len;
ret = btrfs_insert_empty_item(root, &path, &key, data_size);
ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
if (ret)
goto out;
......@@ -40,8 +42,9 @@ int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
return ret;
}
int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
u64 dir, char *name, int name_len, int mod)
int 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)
{
int ret;
struct btrfs_key key;
......@@ -53,12 +56,13 @@ int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
ret = btrfs_name_hash(name, name_len, &key.offset);
BUG_ON(ret);
ret = btrfs_search_slot(root, &key, path, ins_len, cow);
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
return ret;
}
int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
char *name, int name_len)
int btrfs_match_dir_item_name(struct btrfs_root *root,
struct btrfs_path *path, char
*name, int name_len)
{
struct btrfs_dir_item *dir_item;
char *name_ptr;
......
......@@ -8,6 +8,7 @@
#include "disk-io.h"
#include "print-tree.h"
#include "hash.h"
#include "transaction.h"
int keep_running = 1;
struct btrfs_super_block super;
......@@ -38,7 +39,8 @@ static int find_num(struct radix_tree_root *root, unsigned long *num_ret,
return 0;
}
static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
int ret;
char buf[128];
......@@ -48,8 +50,8 @@ static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
find_num(radix, &oid, 0);
sprintf(buf, "str-%lu", oid);
ret = btrfs_insert_dir_item(root, buf, strlen(buf), dir_oid, file_oid,
1);
ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
file_oid, 1);
if (ret)
goto error;
......@@ -68,7 +70,8 @@ static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
* check
*/
btrfs_init_path(&path);
ret = btrfs_lookup_dir_item(root, &path, dir_oid, buf, strlen(buf), 0);
ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
strlen(buf), 0);
if (ret)
goto fatal_release;
if (!btrfs_match_dir_item_name(root, &path, buf, strlen(buf))) {
......@@ -96,7 +99,8 @@ static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
return -1;
}
static int insert_dup(struct btrfs_root *root, struct radix_tree_root *radix)
static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
int ret;
char buf[128];
......@@ -107,8 +111,8 @@ static int insert_dup(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
sprintf(buf, "str-%lu", oid);
ret = btrfs_insert_dir_item(root, buf, strlen(buf), dir_oid, file_oid,
1);
ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
file_oid, 1);
if (ret != -EEXIST) {
printf("insert on %s gave us %d\n", buf, ret);
return 1;
......@@ -116,7 +120,8 @@ static int insert_dup(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
}
static int del_one(struct btrfs_root *root, struct radix_tree_root *radix)
static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
int ret;
char buf[128];
......@@ -129,10 +134,11 @@ static int del_one(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
sprintf(buf, "str-%lu", oid);
btrfs_init_path(&path);
ret = btrfs_lookup_dir_item(root, &path, dir_oid, buf, strlen(buf), -1);
ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
strlen(buf), -1);
if (ret)
goto out_release;
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
if (ret)
goto out_release;
btrfs_release_path(root, &path);
......@@ -149,7 +155,8 @@ static int del_one(struct btrfs_root *root, struct radix_tree_root *radix)
return -1;
}
static int lookup_item(struct btrfs_root *root, struct radix_tree_root *radix)
static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
char buf[128];
......@@ -161,7 +168,8 @@ static int lookup_item(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
sprintf(buf, "str-%lu", oid);
btrfs_init_path(&path);
ret = btrfs_lookup_dir_item(root, &path, dir_oid, buf, strlen(buf), 0);
ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
strlen(buf), 0);
btrfs_release_path(root, &path);
if (ret) {
printf("unable to find key %lu\n", oid);
......@@ -170,7 +178,8 @@ static int lookup_item(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
}
static int lookup_enoent(struct btrfs_root *root, struct radix_tree_root *radix)
static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
char buf[128];
......@@ -182,7 +191,8 @@ static int lookup_enoent(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
sprintf(buf, "str-%lu", oid);
btrfs_init_path(&path);
ret = btrfs_lookup_dir_item(root, &path, dir_oid, buf, strlen(buf), 0);
ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
strlen(buf), 0);
btrfs_release_path(root, &path);
if (!ret) {
printf("able to find key that should not exist %lu\n", oid);
......@@ -191,8 +201,8 @@ static int lookup_enoent(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
}
static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
int nr)
static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix, int nr)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -211,7 +221,7 @@ static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
key.objectid = dir_oid;
while(nr-- >= 0) {
btrfs_init_path(&path);
ret = btrfs_search_slot(root, &key, &path, -1, 1);
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
if (ret < 0) {
btrfs_release_path(root, &path);
return ret;
......@@ -231,7 +241,7 @@ static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
BUG_ON(found_len > 128);
buf[found_len] = '\0';
found = atoi(buf + 4);
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
count++;
if (ret) {
fprintf(stderr,
......@@ -252,19 +262,19 @@ static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
return -1;
}
static int fill_tree(struct btrfs_root *root, struct radix_tree_root *radix,
int count)
static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix, int count)
{
int i;
int ret = 0;
for (i = 0; i < count; i++) {
ret = ins_one(root, radix);
ret = ins_one(trans, root, radix);
if (ret) {
fprintf(stderr, "fill failed\n");
goto out;
}
if (i % 1000 == 0) {
ret = btrfs_commit_transaction(root, &super);
ret = btrfs_commit_transaction(trans, root, &super);
if (ret) {
fprintf(stderr, "fill commit failed\n");
return ret;
......@@ -280,7 +290,8 @@ static int fill_tree(struct btrfs_root *root, struct radix_tree_root *radix,
return ret;
}
static int bulk_op(struct btrfs_root *root, struct radix_tree_root *radix)
static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
int ret;
int nr = rand() % 5000;
......@@ -289,17 +300,18 @@ static int bulk_op(struct btrfs_root *root, struct radix_tree_root *radix)
/* do the bulk op much less frequently */
if (run_nr++ % 100)
return 0;
ret = empty_tree(root, radix, nr);
ret = empty_tree(trans, root, radix, nr);
if (ret)
return ret;
ret = fill_tree(root, radix, nr);
ret = fill_tree(trans, root, radix, nr);
if (ret)
return ret;
return 0;
}
int (*ops[])(struct btrfs_root *root, struct radix_tree_root *radix) =
int (*ops[])(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct
radix_tree_root *radix) =
{ ins_one, insert_dup, del_one, lookup_item,
lookup_enoent, bulk_op };
......@@ -330,11 +342,13 @@ int main(int ac, char **av)
int init_fill_count = 800000;
int err = 0;
int initial_only = 0;
struct btrfs_trans_handle *trans;
radix_tree_init();
printf("removing old tree\n");
unlink("dbfile");
root = open_ctree("dbfile", &super);
trans = btrfs_start_transaction(root, 1);
signal(SIGTERM, sigstopper);
signal(SIGINT, sigstopper);
......@@ -353,7 +367,7 @@ int main(int ac, char **av)
}
}
printf("initial fill\n");
ret = fill_tree(root, &radix, init_fill_count);
ret = fill_tree(trans, root, &radix, init_fill_count);
printf("starting run\n");
if (ret) {
err = ret;
......@@ -377,7 +391,7 @@ int main(int ac, char **av)
root = open_ctree("dbfile", &super);
}
while(count--) {
ret = ops[op](root, &radix);
ret = ops[op](trans, root, &radix);
if (ret) {
fprintf(stderr, "op %d failed %d:%d\n",
op, i, iterations);
......
......@@ -9,6 +9,7 @@
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
static int allocated_blocks = 0;
int cache_max = 10000;
......@@ -107,7 +108,8 @@ struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
return buf;
}
int dirty_tree_block(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)
{
if (!list_empty(&buf->dirty))
return 0;
......@@ -116,7 +118,8 @@ int dirty_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
return 0;
}
int clean_tree_block(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)
{
if (!list_empty(&buf->dirty)) {
list_del_init(&buf->dirty);
......@@ -125,7 +128,8 @@ int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
return 0;
}
int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_buffer *buf)
{
u64 blocknr = buf->blocknr;
loff_t offset = blocknr * root->blocksize;
......@@ -139,7 +143,8 @@ int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
return 0;
}
static int __commit_transaction(struct btrfs_root *root)
static int __commit_transaction(struct btrfs_trans_handle *trans, struct
btrfs_root *root)
{
struct btrfs_buffer *b;
int ret = 0;
......@@ -147,7 +152,7 @@ static int __commit_transaction(struct btrfs_root *root)
while(!list_empty(&root->trans)) {
b = list_entry(root->trans.next, struct btrfs_buffer, dirty);
list_del_init(&b->dirty);
wret = write_tree_block(root, b);
wret = write_tree_block(trans, root, b);
if (wret)
ret = wret;
btrfs_block_release(root, b);
......@@ -155,8 +160,9 @@ static int __commit_transaction(struct btrfs_root *root)
return ret;
}
static int commit_extent_and_tree_roots(struct btrfs_root *tree_root,
struct btrfs_root *extent_root)
static int commit_extent_and_tree_roots(struct btrfs_trans_handle *trans,
struct btrfs_root *tree_root, struct
btrfs_root *extent_root)
{
int ret;
u64 old_extent_block;
......@@ -167,24 +173,24 @@ static int commit_extent_and_tree_roots(struct btrfs_root *tree_root,
break;
btrfs_set_root_blocknr(&extent_root->root_item,
extent_root->node->blocknr);
ret = btrfs_update_root(tree_root,
ret = btrfs_update_root(trans, tree_root,
&extent_root->root_key,
&extent_root->root_item);
BUG_ON(ret);
}
__commit_transaction(extent_root);
__commit_transaction(tree_root);
__commit_transaction(trans, extent_root);
__commit_transaction(trans, tree_root);
return 0;
}
int btrfs_commit_transaction(struct btrfs_root *root,
struct btrfs_super_block *s)
int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
btrfs_root *root, struct btrfs_super_block *s)
{
int ret = 0;
struct btrfs_buffer *snap = root->commit_root;
struct btrfs_key snap_key;
ret = __commit_transaction(root);
ret = __commit_transaction(trans, root);
BUG_ON(ret);
if (root->commit_root == root->node)
......@@ -194,23 +200,24 @@ int btrfs_commit_transaction(struct btrfs_root *root,
root->root_key.offset++;
btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
ret = btrfs_insert_root(root->tree_root, &root->root_key,
ret = btrfs_insert_root(trans, root->tree_root, &root->root_key,
&root->root_item);
BUG_ON(ret);
ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root);
ret = commit_extent_and_tree_roots(trans, root->tree_root,
root->extent_root);
BUG_ON(ret);
write_ctree_super(root, s);
btrfs_finish_extent_commit(root->extent_root);
btrfs_finish_extent_commit(root->tree_root);
write_ctree_super(trans, root, s);
btrfs_finish_extent_commit(trans, root->extent_root);
btrfs_finish_extent_commit(trans, root->tree_root);
root->commit_root = root->node;
root->node->count++;
ret = btrfs_drop_snapshot(root, snap);
ret = btrfs_drop_snapshot(trans, root, snap);
BUG_ON(ret);
ret = btrfs_del_root(root->tree_root, &snap_key);
ret = btrfs_del_root(trans, root->tree_root, &snap_key);
BUG_ON(ret);
return ret;
......@@ -312,7 +319,8 @@ struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
return root;
}
int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s)
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->tree_root->node->blocknr);
......@@ -338,10 +346,14 @@ static int drop_cache(struct btrfs_root *root)
int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
{
int ret;
btrfs_commit_transaction(root, s);
ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root);
struct btrfs_trans_handle *trans;
trans = root->running_transaction;
btrfs_commit_transaction(trans, root, s);
ret = commit_extent_and_tree_roots(trans, root->tree_root,
root->extent_root);
BUG_ON(ret);
write_ctree_super(root, s);
write_ctree_super(trans, root, s);
drop_cache(root->extent_root);
drop_cache(root->tree_root);
drop_cache(root);
......
......@@ -15,15 +15,19 @@ struct btrfs_buffer {
struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr);
struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr);
int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf);
int dirty_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf);
int clean_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf);
int btrfs_commit_transaction(struct btrfs_root *root,
struct btrfs_super_block *s);
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);
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_root *root, struct btrfs_super_block *s);
int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_super_block *s);
int mkfs(int fd, u64 num_blocks, u32 blocksize);
#define BTRFS_SUPER_INFO_OFFSET (16 * 1024)
......
This diff is collapsed.
......@@ -4,9 +4,11 @@
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
int btrfs_insert_inode(struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *inode_item)
int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
*root, u64 objectid, struct btrfs_inode_item
*inode_item)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -17,13 +19,14 @@ int btrfs_insert_inode(struct btrfs_root *root, u64 objectid,
key.offset = 0;
btrfs_init_path(&path);
ret = btrfs_insert_item(root, &key, inode_item, sizeof(*inode_item));
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_root *root, struct btrfs_path *path,
u64 objectid, int mod)
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;
......@@ -33,5 +36,5 @@ int btrfs_lookup_inode(struct btrfs_root *root, struct btrfs_path *path,
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
key.offset = 0;
return btrfs_search_slot(root, &key, path, ins_len, cow);
return btrfs_search_slot(trans, root, &key, path, ins_len, cow);
}
#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);
#endif
......@@ -5,6 +5,7 @@
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
/* for testing only */
int next_key(int i, int max_key) {
......@@ -25,10 +26,12 @@ int main(int ac, char **av) {
struct btrfs_path path;
struct btrfs_super_block super;
struct btrfs_root *root;
struct btrfs_trans_handle *trans;
radix_tree_init();
root = open_ctree("dbfile", &super);
trans = btrfs_start_transaction(root, 1);
srand(55);
ins.flags = 0;
btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
......@@ -41,12 +44,12 @@ int main(int ac, char **av) {
fprintf(stderr, "insert %d:%d\n", num, i);
ins.objectid = num;
ins.offset = 0;
ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
if (!ret)
tree_size++;
free(buf);
if (i == run_size - 5) {
btrfs_commit_transaction(root, &super);
btrfs_commit_transaction(trans, root, &super);
}
}
......@@ -61,7 +64,7 @@ int main(int ac, char **av) {
btrfs_init_path(&path);
if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i);
ret = btrfs_search_slot(root, &ins, &path, 0, 0);
ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
if (ret) {
btrfs_print_tree(root, root->node);
printf("unable to find %d\n", num);
......@@ -83,11 +86,11 @@ int main(int ac, char **av) {
num = next_key(i, max_key);
ins.objectid = num;
btrfs_init_path(&path);
ret = btrfs_search_slot(root, &ins, &path, -1, 1);
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
if (!ret) {
if (i % 10000 == 0)
fprintf(stderr, "del %d:%d\n", num, i);
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
if (ret != 0)
BUG();
tree_size--;
......@@ -104,7 +107,7 @@ int main(int ac, char **av) {
ins.objectid = num;
if (i % 10000 == 0)
fprintf(stderr, "insert %d:%d\n", num, i);
ret = btrfs_insert_item(root, &ins, buf, strlen(buf));
ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
if (!ret)
tree_size++;
free(buf);
......@@ -119,7 +122,7 @@ int main(int ac, char **av) {
btrfs_init_path(&path);
if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i);
ret = btrfs_search_slot(root, &ins, &path, 0, 0);
ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
if (ret) {
btrfs_print_tree(root, root->node);
printf("unable to find %d\n", num);
......@@ -134,7 +137,7 @@ int main(int ac, char **av) {
int slot;
ins.objectid = (u64)-1;
btrfs_init_path(&path);
ret = btrfs_search_slot(root, &ins, &path, -1, 1);
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
if (ret == 0)
BUG();
......@@ -150,7 +153,7 @@ int main(int ac, char **av) {
btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
if (tree_size % 10000 == 0)
printf("big del %d:%d\n", tree_size, i);
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
if (ret != 0) {
printf("del_item returned %d\n", ret);
BUG();
......@@ -165,7 +168,7 @@ int main(int ac, char **av) {
printf("map before commit\n");
btrfs_print_tree(root->extent_root, root->extent_root->node);
*/
btrfs_commit_transaction(root, &super);
btrfs_commit_transaction(trans, root, &super);
printf("tree size is now %d\n", tree_size);
printf("root %p commit root %p\n", root->node, root->commit_root);
printf("map tree\n");
......
......@@ -6,6 +6,7 @@
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
int keep_running = 1;
struct btrfs_super_block super;
......@@ -37,7 +38,8 @@ static int setup_key(struct radix_tree_root *root, struct btrfs_key *key,
return 0;
}
static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -47,7 +49,7 @@ static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
btrfs_init_path(&path);
ret = setup_key(radix, &key, 0);
sprintf(buf, "str-%Lu\n", key.objectid);
ret = btrfs_insert_item(root, &key, buf, strlen(buf));
ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
if (ret)
goto error;
oid = (unsigned long)key.objectid;
......@@ -62,7 +64,8 @@ static int ins_one(struct btrfs_root *root, struct radix_tree_root *radix)
return -1;
}
static int insert_dup(struct btrfs_root *root, struct radix_tree_root *radix)
static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -73,7 +76,7 @@ static int insert_dup(struct btrfs_root *root, struct radix_tree_root *radix)
if (ret < 0)
return 0;
sprintf(buf, "str-%Lu\n", key.objectid);
ret = btrfs_insert_item(root, &key, buf, strlen(buf));
ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
if (ret != -EEXIST) {
printf("insert on %Lu gave us %d\n", key.objectid, ret);
return 1;
......@@ -81,7 +84,8 @@ static int insert_dup(struct btrfs_root *root, struct radix_tree_root *radix)
return 0;
}
static int del_one(struct btrfs_root *root, struct radix_tree_root *radix)
static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -91,10 +95,10 @@ static int del_one(struct btrfs_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
ret = btrfs_search_slot(root, &key, &path, -1, 1);
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
if (ret)
goto error;
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
btrfs_release_path(root, &path);
if (ret != 0)
goto error;
......@@ -107,7 +111,8 @@ static int del_one(struct btrfs_root *root, struct radix_tree_root *radix)
return -1;
}
static int lookup_item(struct btrfs_root *root, struct radix_tree_root *radix)
static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -116,7 +121,7 @@ static int lookup_item(struct btrfs_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
ret = btrfs_search_slot(root, &key, &path, 0, 1);
ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
btrfs_release_path(root, &path);
if (ret)
goto error;
......@@ -126,7 +131,8 @@ static int lookup_item(struct btrfs_root *root, struct radix_tree_root *radix)
return -1;
}
static int lookup_enoent(struct btrfs_root *root, struct radix_tree_root *radix)
static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -135,7 +141,7 @@ static int lookup_enoent(struct btrfs_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 0);
if (ret < 0)
return ret;
ret = btrfs_search_slot(root, &key, &path, 0, 0);
ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
btrfs_release_path(root, &path);
if (ret <= 0)
goto error;
......@@ -145,8 +151,8 @@ static int lookup_enoent(struct btrfs_root *root, struct radix_tree_root *radix)
return -1;
}
static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
int nr)
static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix, int nr)
{
struct btrfs_path path;
struct btrfs_key key;
......@@ -162,7 +168,7 @@ static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
key.objectid = (unsigned long)-1;
while(nr-- >= 0) {
btrfs_init_path(&path);
ret = btrfs_search_slot(root, &key, &path, -1, 1);
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
if (ret < 0) {
btrfs_release_path(root, &path);
return ret;
......@@ -177,7 +183,7 @@ static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
slot = path.slots[0];
found = btrfs_disk_key_objectid(
&path.nodes[0]->leaf.items[slot].key);
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
count++;
if (ret) {
fprintf(stderr,
......@@ -198,19 +204,19 @@ static int empty_tree(struct btrfs_root *root, struct radix_tree_root *radix,
return -1;
}
static int fill_tree(struct btrfs_root *root, struct radix_tree_root *radix,
int count)
static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix, int count)
{
int i;
int ret = 0;
for (i = 0; i < count; i++) {
ret = ins_one(root, radix);
ret = ins_one(trans, root, radix);
if (ret) {
fprintf(stderr, "fill failed\n");
goto out;
}
if (i % 1000 == 0) {
ret = btrfs_commit_transaction(root, &super);
ret = btrfs_commit_transaction(trans, root, &super);
if (ret) {
fprintf(stderr, "fill commit failed\n");
return ret;
......@@ -226,7 +232,8 @@ static int fill_tree(struct btrfs_root *root, struct radix_tree_root *radix,
return ret;
}
static int bulk_op(struct btrfs_root *root, struct radix_tree_root *radix)
static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
int ret;
int nr = rand() % 5000;
......@@ -235,17 +242,18 @@ static int bulk_op(struct btrfs_root *root, struct radix_tree_root *radix)
/* do the bulk op much less frequently */
if (run_nr++ % 100)
return 0;
ret = empty_tree(root, radix, nr);
ret = empty_tree(trans, root, radix, nr);
if (ret)
return ret;
ret = fill_tree(root, radix, nr);
ret = fill_tree(trans, root, radix, nr);
if (ret)
return ret;
return 0;
}
int (*ops[])(struct btrfs_root *root, struct radix_tree_root *radix) =
int (*ops[])(struct btrfs_trans_handle *,
struct btrfs_root *root, struct radix_tree_root *radix) =
{ ins_one, insert_dup, del_one, lookup_item,
lookup_enoent, bulk_op };
......@@ -264,7 +272,7 @@ static int fill_radix(struct btrfs_root *root, struct radix_tree_root *radix)
key.objectid = (unsigned long)-1;
while(1) {
btrfs_init_path(&path);
ret = btrfs_search_slot(root, &key, &path, 0, 0);
ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
if (ret < 0) {
btrfs_release_path(root, &path);
return ret;
......@@ -325,6 +333,7 @@ int main(int ac, char **av)
int init_fill_count = 800000;
int err = 0;
int initial_only = 0;
struct btrfs_trans_handle *trans;
radix_tree_init();
root = open_ctree("dbfile", &super);
fill_radix(root, &radix);
......@@ -346,7 +355,8 @@ int main(int ac, char **av)
}
}
printf("initial fill\n");
ret = fill_tree(root, &radix, init_fill_count);
trans = btrfs_start_transaction(root, 1);
ret = fill_tree(trans, root, &radix, init_fill_count);
printf("starting run\n");
if (ret) {
err = ret;
......@@ -370,7 +380,7 @@ int main(int ac, char **av)
root = open_ctree("dbfile", &super);
}
while(count--) {
ret = ops[op](root, &radix);
ret = ops[op](trans, root, &radix);
if (ret) {
fprintf(stderr, "op %d failed %d:%d\n",
op, i, iterations);
......
......@@ -20,7 +20,7 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
search_key.offset = (u32)-1;
btrfs_init_path(&path);
ret = btrfs_search_slot(root, &search_key, &path, 0, 0);
ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0);
if (ret < 0)
goto out;
BUG_ON(ret == 0);
......@@ -40,8 +40,9 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
return ret;
}
int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_root_item *item)
int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, struct btrfs_root_item
*item)
{
struct btrfs_path path;
struct btrfs_leaf *l;
......@@ -49,7 +50,7 @@ int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
int slot;
btrfs_init_path(&path);
ret = btrfs_search_slot(root, key, &path, 0, 1);
ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
if (ret < 0)
goto out;
BUG_ON(ret != 0);
......@@ -62,26 +63,28 @@ int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
return ret;
}
int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
struct btrfs_root_item *item)
int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_key *key, struct btrfs_root_item
*item)
{
int ret;
ret = btrfs_insert_item(root, key, item, sizeof(*item));
ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
BUG_ON(ret);
return ret;
}
int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key)
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(root, key, &path, -1, 1);
ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
if (ret < 0)
goto out;
BUG_ON(ret != 0);
ret = btrfs_del_item(root, &path);
ret = btrfs_del_item(trans, root, &path);
out:
btrfs_release_path(root, &path);
return ret;
......
#ifndef __TRANSACTION__
#define __TRANSACTION__
struct btrfs_trans_handle {
u64 transid;
unsigned long blocks_reserved;
unsigned long blocks_used;
};
static inline struct btrfs_trans_handle *
btrfs_start_transaction(struct btrfs_root *root, int num_blocks)
{
struct btrfs_trans_handle *h = malloc(sizeof(*h));
h->transid = root->root_key.offset;
h->blocks_reserved = num_blocks;
h->blocks_used = 0;
return h;
}
static inline void btrfs_free_transaction(struct btrfs_root *root,
struct btrfs_trans_handle *handle)
{
memset(handle, 0, sizeof(*handle));
free(handle);
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment