root-tree.c 2.0 KB
Newer Older
1
#include <linux/module.h>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#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)
{
	struct btrfs_path path;
	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;

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

40 41 42
int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, struct btrfs_key *key, struct btrfs_root_item
		      *item)
43 44 45 46 47 48 49
{
	struct btrfs_path path;
	struct btrfs_leaf *l;
	int ret;
	int slot;

	btrfs_init_path(&path);
50
	ret = btrfs_search_slot(trans, root, key, &path, 0, 1);
51 52 53 54 55
	if (ret < 0)
		goto out;
	BUG_ON(ret != 0);
	l = &path.nodes[0]->leaf;
	slot = path.slots[0];
C
Chris Mason 已提交
56
	memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
57 58 59 60 61 62
		sizeof(*item));
out:
	btrfs_release_path(root, &path);
	return ret;
}

63 64 65
int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
		      *root, struct btrfs_key *key, struct btrfs_root_item
		      *item)
66 67
{
	int ret;
68
	ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
69 70 71 72
	BUG_ON(ret);
	return ret;
}

73 74
int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
		   struct btrfs_key *key)
75 76 77 78 79
{
	struct btrfs_path path;
	int ret;

	btrfs_init_path(&path);
80
	ret = btrfs_search_slot(trans, root, key, &path, -1, 1);
81 82 83
	if (ret < 0)
		goto out;
	BUG_ON(ret != 0);
84
	ret = btrfs_del_item(trans, root, &path);
85 86 87 88
out:
	btrfs_release_path(root, &path);
	return ret;
}