root-tree.c 2.1 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
	if (ret < 0)
		goto out;
	BUG_ON(ret == 0);
C
Chris Mason 已提交
24
	l = btrfs_buffer_leaf(path.nodes[0]);
25 26
	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
	if (ret < 0)
		goto out;
	BUG_ON(ret != 0);
C
Chris Mason 已提交
54
	l = btrfs_buffer_leaf(path.nodes[0]);
55
	slot = path.slots[0];
C
Chris Mason 已提交
56
	memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
57
		sizeof(*item));
58
	mark_buffer_dirty(path.nodes[0]);
59 60 61 62 63
out:
	btrfs_release_path(root, &path);
	return ret;
}

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

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

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