Create macros to generation set/get funcs for on disk structures
[platform/upstream/btrfs-progs.git] / inode-item.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26
27 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
28                            struct btrfs_root *root,
29                            const char *name, int name_len,
30                            u64 inode_objectid, u64 ref_objectid)
31 {
32         struct btrfs_path path;
33         struct btrfs_key key;
34         struct btrfs_inode_ref *ref;
35         char *ptr;
36         int ret;
37         int ins_len = name_len + sizeof(*ref);
38
39         key.objectid = inode_objectid;
40         key.offset = ref_objectid;
41         btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
42
43         btrfs_init_path(&path);
44         ret = btrfs_insert_empty_item(trans, root, &path, &key,
45                                       ins_len);
46         if (ret == -EEXIST) {
47 #if 0
48                 u32 old_size;
49
50                 if (find_name_in_backref(path, name, name_len, &ref))
51                         goto out;
52
53                 old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
54                 ret = btrfs_extend_item(trans, root, path, ins_len);
55                 BUG_ON(ret);
56                 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
57                                      struct btrfs_inode_ref);
58                 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
59                 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
60                 ptr = (unsigned long)(ref + 1);
61                 ret = 0;
62 #endif
63                 goto out;
64         } else if (ret < 0) {
65                 goto out;
66         } else {
67                 ref = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
68                                      struct btrfs_inode_ref);
69                 btrfs_set_inode_ref_name_len(ref, name_len);
70                 ptr = (char *)(ref + 1);
71         }
72         memcpy(ptr, name, name_len);
73         dirty_tree_block(trans, root, path.nodes[0]);
74
75 out:
76         btrfs_release_path(root, &path);
77         return ret;
78 }
79
80 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
81                        *root, u64 objectid, struct btrfs_inode_item
82                        *inode_item)
83 {
84         struct btrfs_path path;
85         struct btrfs_key key;
86         int ret;
87         key.objectid = objectid;
88         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
89         key.offset = 0;
90
91         btrfs_init_path(&path);
92         ret = btrfs_insert_item(trans, root, &key, inode_item,
93                                 sizeof(*inode_item));
94         btrfs_release_path(root, &path);
95         return ret;
96 }
97
98 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
99                        *root, struct btrfs_path *path, u64 objectid, int mod)
100 {
101         struct btrfs_key key;
102         int ins_len = mod < 0 ? -1 : 0;
103         int cow = mod != 0;
104
105         key.objectid = objectid;
106         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
107         key.offset = 0;
108         return btrfs_search_slot(trans, root, &key, path, ins_len, cow);
109 }