root-tree.c 2.4 KB
Newer Older
1
#include <linux/module.h>
2 3 4 5 6 7 8
#include "ctree.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)
{
9
	struct btrfs_path *path;
10 11 12 13 14 15 16 17 18
	struct btrfs_key search_key;
	struct btrfs_leaf *l;
	int ret;
	int slot;

	search_key.objectid = objectid;
	search_key.flags = (u32)-1;
	search_key.offset = (u32)-1;

19 20 21 22
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
23 24 25
	if (ret < 0)
		goto out;
	BUG_ON(ret == 0);
26 27 28
	l = btrfs_buffer_leaf(path->nodes[0]);
	BUG_ON(path->slots[0] == 0);
	slot = path->slots[0] - 1;
29
	if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
30 31 32
		ret = 1;
		goto out;
	}
C
Chris Mason 已提交
33
	memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
34 35 36 37
		sizeof(*item));
	btrfs_disk_key_to_cpu(key, &l->items[slot].key);
	ret = 0;
out:
38 39
	btrfs_release_path(root, path);
	btrfs_free_path(path);
40 41 42
	return ret;
}

43 44 45
int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, struct btrfs_key *key, struct btrfs_root_item
		      *item)
46
{
47
	struct btrfs_path *path;
48 49 50
	struct btrfs_leaf *l;
	int ret;
	int slot;
C
Chris Mason 已提交
51
	struct btrfs_root_item *update_item;
52

53 54 55 56
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
	ret = btrfs_search_slot(trans, root, key, path, 0, 1);
57 58 59
	if (ret < 0)
		goto out;
	BUG_ON(ret != 0);
60 61
	l = btrfs_buffer_leaf(path->nodes[0]);
	slot = path->slots[0];
C
Chris Mason 已提交
62 63
	update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
	btrfs_memcpy(root, l, update_item, item, sizeof(*item));
64
	btrfs_mark_buffer_dirty(path->nodes[0]);
65
out:
66 67
	btrfs_release_path(root, path);
	btrfs_free_path(path);
68 69 70
	return ret;
}

71 72 73
int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, struct btrfs_key *key, struct btrfs_root_item
		      *item)
74 75
{
	int ret;
76
	ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
77 78 79 80
	BUG_ON(ret);
	return ret;
}

81 82
int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
		   struct btrfs_key *key)
83
{
84
	struct btrfs_path *path;
85 86
	int ret;

87 88 89 90
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
91 92 93
	if (ret < 0)
		goto out;
	BUG_ON(ret != 0);
94
	ret = btrfs_del_item(trans, root, path);
95
out:
96 97
	btrfs_release_path(root, path);
	btrfs_free_path(path);
98 99
	return ret;
}