blob: 2b357a6d2407833d81b5419ab8d4aac5a113eca8 [file] [log] [blame]
Balaji Raobe6e8dc2008-07-21 02:01:56 +05301#include <linux/fs.h>
2#include <linux/types.h>
3#include "ctree.h"
4#include "disk-io.h"
5#include "btrfs_inode.h"
6#include "print-tree.h"
7#include "export.h"
8#include "compat.h"
9
10#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
11#define FILEID_BTRFS_WITHOUT_PARENT 0x4d
12#define FILEID_BTRFS_WITH_PARENT 0x4e
13#define FILEID_BTRFS_WITH_PARENT_ROOT 0x4f
14#endif
15
16#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, parent_objectid)/4)
17#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, parent_root_objectid)/4)
18#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid)/4)
19
20static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
21 int connectable)
22{
23 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
24 struct inode *inode = dentry->d_inode;
25 int len = *max_len;
26 int type;
27
28 if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
29 (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
30 return 255;
31
32 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
33 type = FILEID_BTRFS_WITHOUT_PARENT;
34
35 fid->objectid = BTRFS_I(inode)->location.objectid;
36 fid->root_objectid = BTRFS_I(inode)->root->objectid;
37 fid->gen = inode->i_generation;
38
39 if (connectable && !S_ISDIR(inode->i_mode)) {
40 struct inode *parent;
41 u64 parent_root_id;
42
43 spin_lock(&dentry->d_lock);
44
45 parent = dentry->d_parent->d_inode;
46 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
47 fid->parent_gen = parent->i_generation;
48 parent_root_id = BTRFS_I(parent)->root->objectid;
49
50 spin_unlock(&dentry->d_lock);
51
52 if (parent_root_id != fid->root_objectid) {
53 fid->parent_root_objectid = parent_root_id;
54 len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
55 type = FILEID_BTRFS_WITH_PARENT_ROOT;
56 } else {
57 len = BTRFS_FID_SIZE_CONNECTABLE;
58 type = FILEID_BTRFS_WITH_PARENT;
59 }
60 }
61
62 *max_len = len;
63 return type;
64}
65
66static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
67 u64 root_objectid, u32 generation)
68{
69 struct btrfs_root *root;
70 struct inode *inode;
Balaji Raobe6e8dc2008-07-21 02:01:56 +053071 struct btrfs_key key;
72
David Woodhouse2d4d9fbd2008-08-19 22:20:17 +010073 key.objectid = root_objectid;
74 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
75 key.offset = (u64)-1;
76
77 root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
78 if (IS_ERR(root))
79 return ERR_CAST(root);
80
Balaji Raobe6e8dc2008-07-21 02:01:56 +053081 key.objectid = objectid;
82 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
83 key.offset = 0;
84
Balaji Raobe6e8dc2008-07-21 02:01:56 +053085 inode = btrfs_iget(sb, &key, root, NULL);
86 if (IS_ERR(inode))
87 return (void *)inode;
88
89 if (generation != inode->i_generation) {
90 iput(inode);
91 return ERR_PTR(-ESTALE);
92 }
93
Christoph Hellwig50ec8912008-09-05 16:43:20 -040094 return d_obtain_alias(inode);
Balaji Raobe6e8dc2008-07-21 02:01:56 +053095}
96
97static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
98 int fh_len, int fh_type)
99{
100 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
101 u64 objectid, root_objectid;
102 u32 generation;
103
104 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
105 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
106 return NULL;
107 root_objectid = fid->root_objectid;
108 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
109 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
110 return NULL;
111 root_objectid = fid->parent_root_objectid;
112 } else
113 return NULL;
114
115 objectid = fid->parent_objectid;
116 generation = fid->parent_gen;
117
118 return btrfs_get_dentry(sb, objectid, root_objectid, generation);
119}
120
121static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
122 int fh_len, int fh_type)
123{
124 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
125 u64 objectid, root_objectid;
126 u32 generation;
127
128 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
129 fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
130 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
131 fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
132 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
133 fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
134 return NULL;
135
136 objectid = fid->objectid;
137 root_objectid = fid->root_objectid;
138 generation = fid->gen;
139
140 return btrfs_get_dentry(sb, objectid, root_objectid, generation);
141}
142
143static struct dentry *btrfs_get_parent(struct dentry *child)
144{
145 struct inode *dir = child->d_inode;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530146 struct btrfs_root *root = BTRFS_I(dir)->root;
147 struct btrfs_key key;
148 struct btrfs_path *path;
149 struct extent_buffer *leaf;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530150 int slot;
151 u64 objectid;
152 int ret;
153
154 path = btrfs_alloc_path();
155
156 key.objectid = dir->i_ino;
157 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
David Woodhouse87acb4e2008-08-18 22:50:22 +0100158 key.offset = (u64)-1;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530159
David Woodhouse87acb4e2008-08-18 22:50:22 +0100160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
David Woodhoused54a8392008-08-19 22:33:04 +0100161 if (ret < 0) {
162 /* Error */
163 btrfs_free_path(path);
164 return ERR_PTR(ret);
165 }
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530166 leaf = path->nodes[0];
167 slot = path->slots[0];
David Woodhoused54a8392008-08-19 22:33:04 +0100168 if (ret) {
169 /* btrfs_search_slot() returns the slot where we'd want to
170 insert a backref for parent inode #0xFFFFFFFFFFFFFFFF.
171 The _real_ backref, telling us what the parent inode
172 _actually_ is, will be in the slot _before_ the one
173 that btrfs_search_slot() returns. */
174 if (!slot) {
175 /* Unless there is _no_ key in the tree before... */
176 btrfs_free_path(path);
177 return ERR_PTR(-EIO);
178 }
179 slot--;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530180 }
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530181
182 btrfs_item_key_to_cpu(leaf, &key, slot);
David Woodhouse87acb4e2008-08-18 22:50:22 +0100183 btrfs_free_path(path);
184
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530185 if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
David Woodhoused54a8392008-08-19 22:33:04 +0100186 return ERR_PTR(-EINVAL);
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530187
188 objectid = key.offset;
189
David Woodhouse2d4d9fbd2008-08-19 22:20:17 +0100190 /* If we are already at the root of a subvol, return the real root */
191 if (objectid == dir->i_ino)
192 return dget(dir->i_sb->s_root);
193
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530194 /* Build a new key for the inode item */
195 key.objectid = objectid;
196 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
197 key.offset = 0;
198
Christoph Hellwig50ec8912008-09-05 16:43:20 -0400199 return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530200}
201
202const struct export_operations btrfs_export_ops = {
203 .encode_fh = btrfs_encode_fh,
204 .fh_to_dentry = btrfs_fh_to_dentry,
205 .fh_to_parent = btrfs_fh_to_parent,
206 .get_parent = btrfs_get_parent,
207};