2a888e97e1a28fcac229850de717a65d345873ad
[platform/upstream/btrfs-progs.git] / dir-item.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 "hash.h"
8
9 int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
10                           u64 dir, u64 objectid, u8 type)
11 {
12         int ret = 0;
13         struct btrfs_path path;
14         struct btrfs_dir_item *dir_item;
15         char *name_ptr;
16         struct btrfs_key key;
17         u32 data_size;
18
19         key.objectid = dir;
20         key.flags = 0;
21         ret = btrfs_name_hash(name, name_len, &key.offset);
22         BUG_ON(ret);
23         btrfs_init_path(&path);
24         data_size = sizeof(*dir_item) + name_len;
25         ret = btrfs_insert_empty_item(root, &path, &key, data_size);
26         if (ret)
27                 goto out;
28
29         dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
30                                   struct btrfs_dir_item);
31         btrfs_set_dir_objectid(dir_item, objectid);
32         btrfs_set_dir_type(dir_item, type);
33         btrfs_set_dir_flags(dir_item, 0);
34         name_ptr = (char *)(dir_item + 1);
35         memcpy(name_ptr, name, name_len);
36 out:
37         btrfs_release_path(root, &path);
38         return ret;
39 }
40
41 int btrfs_del_dir_item(struct btrfs_root *root, u64 dir, char *name,
42                        int name_len)
43 {
44         int ret = 0;
45         struct btrfs_path path;
46         struct btrfs_key key;
47
48         key.objectid = dir;
49         key.flags = 0;
50         ret = btrfs_name_hash(name, name_len, &key.offset);
51         BUG_ON(ret);
52         btrfs_init_path(&path);
53         ret = btrfs_search_slot(root, &key, &path, 0, 1);
54         if (ret)
55                 goto out;
56         ret = btrfs_del_item(root, &path);
57 out:
58         btrfs_release_path(root, &path);
59         return ret;
60 }
61
62 int btrfs_lookup_dir_item(struct btrfs_root *root, u64 dir, char *name,
63                           int name_len, u64 *objectid)
64 {
65         int ret = 0;
66         struct btrfs_path path;
67         struct btrfs_dir_item *dir_item;
68         char *name_ptr;
69         struct btrfs_key key;
70         u32 item_len;
71         struct btrfs_item *item;
72
73         key.objectid = dir;
74         key.flags = 0;
75         ret = btrfs_name_hash(name, name_len, &key.offset);
76         BUG_ON(ret);
77         btrfs_init_path(&path);
78         ret = btrfs_search_slot(root, &key, &path, 0, 0);
79         if (ret)
80                 goto out;
81
82         dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
83                                   struct btrfs_dir_item);
84
85         item = path.nodes[0]->leaf.items + path.slots[0];
86         item_len = btrfs_item_size(item);
87         if (item_len != name_len + sizeof(struct btrfs_dir_item)) {
88                 BUG();
89                 ret = 1;
90                 goto out;
91         }
92         name_ptr = (char *)(dir_item + 1);
93         if (memcmp(name_ptr, name, name_len)) {
94                 BUG();
95                 ret = 1;
96                 goto out;
97         }
98         *objectid = btrfs_dir_objectid(dir_item);
99 out:
100         btrfs_release_path(root, &path);
101         return ret;
102 }