Use a chunk of the key flags to record the item type.
[platform/upstream/btrfs-progs.git] / root-tree.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "kerncompat.h"
4 #include "radix-tree.h"
5 #include "ctree.h"
6 #include "disk-io.h"
7 #include "print-tree.h"
8
9 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
10                         struct btrfs_root_item *item, struct btrfs_key *key)
11 {
12         struct btrfs_path path;
13         struct btrfs_key search_key;
14         struct btrfs_leaf *l;
15         int ret;
16         int slot;
17
18         search_key.objectid = objectid;
19         search_key.flags = (u32)-1;
20         search_key.offset = (u32)-1;
21
22         btrfs_init_path(&path);
23         ret = btrfs_search_slot(root, &search_key, &path, 0, 0);
24         if (ret < 0)
25                 goto out;
26         BUG_ON(ret == 0);
27         l = &path.nodes[0]->leaf;
28         BUG_ON(path.slots[0] == 0);
29         slot = path.slots[0] - 1;
30         if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
31                 ret = 1;
32                 goto out;
33         }
34         memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
35                 sizeof(*item));
36         btrfs_disk_key_to_cpu(key, &l->items[slot].key);
37         btrfs_release_path(root, &path);
38         ret = 0;
39 out:
40         return ret;
41 }
42
43 int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
44                       struct btrfs_root_item *item)
45 {
46         struct btrfs_path path;
47         struct btrfs_leaf *l;
48         int ret;
49         int slot;
50
51         btrfs_init_path(&path);
52         ret = btrfs_search_slot(root, key, &path, 0, 1);
53         if (ret < 0)
54                 goto out;
55         BUG_ON(ret != 0);
56         l = &path.nodes[0]->leaf;
57         slot = path.slots[0];
58         memcpy(btrfs_item_ptr(l, slot, struct btrfs_root_item), item,
59                 sizeof(*item));
60 out:
61         btrfs_release_path(root, &path);
62         return ret;
63 }
64
65 int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
66                       struct btrfs_root_item *item)
67 {
68         int ret;
69         ret = btrfs_insert_item(root, key, item, sizeof(*item));
70         BUG_ON(ret);
71         return ret;
72 }
73
74 int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key)
75 {
76         struct btrfs_path path;
77         int ret;
78
79         btrfs_init_path(&path);
80         ret = btrfs_search_slot(root, key, &path, -1, 1);
81         if (ret < 0)
82                 goto out;
83         BUG_ON(ret != 0);
84         ret = btrfs_del_item(root, &path);
85 out:
86         btrfs_release_path(root, &path);
87         return ret;
88 }