Commit | Line | Data |
---|---|---|
2e635a27 | 1 | #include <linux/module.h> |
3768f368 CM |
2 | #include "ctree.h" |
3 | #include "disk-io.h" | |
4 | #include "print-tree.h" | |
5 | ||
6 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, | |
7 | struct btrfs_root_item *item, struct btrfs_key *key) | |
8 | { | |
5caf2a00 | 9 | struct btrfs_path *path; |
3768f368 CM |
10 | struct btrfs_key search_key; |
11 | struct btrfs_leaf *l; | |
12 | int ret; | |
13 | int slot; | |
14 | ||
15 | search_key.objectid = objectid; | |
16 | search_key.flags = (u32)-1; | |
17 | search_key.offset = (u32)-1; | |
18 | ||
5caf2a00 CM |
19 | path = btrfs_alloc_path(); |
20 | BUG_ON(!path); | |
21 | btrfs_init_path(path); | |
22 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
3768f368 CM |
23 | if (ret < 0) |
24 | goto out; | |
25 | BUG_ON(ret == 0); | |
5caf2a00 CM |
26 | l = btrfs_buffer_leaf(path->nodes[0]); |
27 | BUG_ON(path->slots[0] == 0); | |
28 | slot = path->slots[0] - 1; | |
62e2749e | 29 | if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) { |
3768f368 CM |
30 | ret = 1; |
31 | goto out; | |
32 | } | |
123abc88 | 33 | memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), |
3768f368 CM |
34 | sizeof(*item)); |
35 | btrfs_disk_key_to_cpu(key, &l->items[slot].key); | |
3768f368 CM |
36 | ret = 0; |
37 | out: | |
5caf2a00 CM |
38 | btrfs_release_path(root, path); |
39 | btrfs_free_path(path); | |
3768f368 CM |
40 | return ret; |
41 | } | |
42 | ||
e089f05c CM |
43 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
44 | *root, struct btrfs_key *key, struct btrfs_root_item | |
45 | *item) | |
3768f368 | 46 | { |
5caf2a00 | 47 | struct btrfs_path *path; |
3768f368 CM |
48 | struct btrfs_leaf *l; |
49 | int ret; | |
50 | int slot; | |
d6025579 | 51 | struct btrfs_root_item *update_item; |
3768f368 | 52 | |
5caf2a00 CM |
53 | path = btrfs_alloc_path(); |
54 | BUG_ON(!path); | |
55 | btrfs_init_path(path); | |
56 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); | |
3768f368 CM |
57 | if (ret < 0) |
58 | goto out; | |
59 | BUG_ON(ret != 0); | |
5caf2a00 CM |
60 | l = btrfs_buffer_leaf(path->nodes[0]); |
61 | slot = path->slots[0]; | |
d6025579 CM |
62 | update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item); |
63 | btrfs_memcpy(root, l, update_item, item, sizeof(*item)); | |
5caf2a00 | 64 | btrfs_mark_buffer_dirty(path->nodes[0]); |
3768f368 | 65 | out: |
5caf2a00 CM |
66 | btrfs_release_path(root, path); |
67 | btrfs_free_path(path); | |
3768f368 CM |
68 | return ret; |
69 | } | |
70 | ||
e089f05c CM |
71 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
72 | *root, struct btrfs_key *key, struct btrfs_root_item | |
73 | *item) | |
3768f368 CM |
74 | { |
75 | int ret; | |
e089f05c | 76 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
3768f368 CM |
77 | BUG_ON(ret); |
78 | return ret; | |
79 | } | |
80 | ||
e089f05c CM |
81 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
82 | struct btrfs_key *key) | |
3768f368 | 83 | { |
5caf2a00 | 84 | struct btrfs_path *path; |
3768f368 CM |
85 | int ret; |
86 | ||
5caf2a00 CM |
87 | path = btrfs_alloc_path(); |
88 | BUG_ON(!path); | |
89 | btrfs_init_path(path); | |
90 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); | |
3768f368 CM |
91 | if (ret < 0) |
92 | goto out; | |
93 | BUG_ON(ret != 0); | |
5caf2a00 | 94 | ret = btrfs_del_item(trans, root, path); |
3768f368 | 95 | out: |
5caf2a00 CM |
96 | btrfs_release_path(root, path); |
97 | btrfs_free_path(path); | |
3768f368 CM |
98 | return ret; |
99 | } |