root-tree.c 2.7 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
	int ret;
C
Chris Mason 已提交
86 87
	u32 refs;
	struct btrfs_root_item *ri;
88

89 90 91 92
	path = btrfs_alloc_path();
	BUG_ON(!path);
	btrfs_init_path(path);
	ret = btrfs_search_slot(trans, root, key, path, -1, 1);
93 94 95
	if (ret < 0)
		goto out;
	BUG_ON(ret != 0);
C
Chris Mason 已提交
96 97 98 99 100 101 102 103 104 105 106 107
	ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
			    path->slots[0], struct btrfs_root_item);

	refs = btrfs_root_refs(ri);
	BUG_ON(refs == 0);
	if (refs == 1) {
		ret = btrfs_del_item(trans, root, path);
	} else {
		btrfs_set_root_refs(ri, refs - 1);
printk("ref now %u root %Lu %Lu %u\n", refs -1, key->objectid, key->offset, key->flags);
		mark_buffer_dirty(path->nodes[0]);
	}
108
out:
109 110
	btrfs_release_path(root, path);
	btrfs_free_path(path);
111 112
	return ret;
}