blob: 9786963b07e5be25914a15c2d863da2d13700d33 [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
Chris Masond3977122009-01-05 21:25:51 -050010#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
11 parent_objectid) / 4)
12#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
13 parent_root_objectid) / 4)
14#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
Balaji Raobe6e8dc2008-07-21 02:01:56 +053015
16static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
17 int connectable)
18{
19 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
20 struct inode *inode = dentry->d_inode;
21 int len = *max_len;
22 int type;
23
24 if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
25 (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
26 return 255;
27
28 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
29 type = FILEID_BTRFS_WITHOUT_PARENT;
30
Yan, Zheng76dda932009-09-21 16:00:26 -040031 fid->objectid = inode->i_ino;
Balaji Raobe6e8dc2008-07-21 02:01:56 +053032 fid->root_objectid = BTRFS_I(inode)->root->objectid;
33 fid->gen = inode->i_generation;
34
35 if (connectable && !S_ISDIR(inode->i_mode)) {
36 struct inode *parent;
37 u64 parent_root_id;
38
39 spin_lock(&dentry->d_lock);
40
41 parent = dentry->d_parent->d_inode;
42 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
43 fid->parent_gen = parent->i_generation;
44 parent_root_id = BTRFS_I(parent)->root->objectid;
45
46 spin_unlock(&dentry->d_lock);
47
48 if (parent_root_id != fid->root_objectid) {
49 fid->parent_root_objectid = parent_root_id;
50 len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
51 type = FILEID_BTRFS_WITH_PARENT_ROOT;
52 } else {
53 len = BTRFS_FID_SIZE_CONNECTABLE;
54 type = FILEID_BTRFS_WITH_PARENT;
55 }
56 }
57
58 *max_len = len;
59 return type;
60}
61
62static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
Yan, Zheng76dda932009-09-21 16:00:26 -040063 u64 root_objectid, u32 generation,
64 int check_generation)
Balaji Raobe6e8dc2008-07-21 02:01:56 +053065{
Yan, Zheng76dda932009-09-21 16:00:26 -040066 struct btrfs_fs_info *fs_info = btrfs_sb(sb)->fs_info;
Balaji Raobe6e8dc2008-07-21 02:01:56 +053067 struct btrfs_root *root;
68 struct inode *inode;
Balaji Raobe6e8dc2008-07-21 02:01:56 +053069 struct btrfs_key key;
Yan, Zheng76dda932009-09-21 16:00:26 -040070 int index;
71 int err = 0;
72
73 if (objectid < BTRFS_FIRST_FREE_OBJECTID)
74 return ERR_PTR(-ESTALE);
Balaji Raobe6e8dc2008-07-21 02:01:56 +053075
David Woodhouse2d4d9fbd2008-08-19 22:20:17 +010076 key.objectid = root_objectid;
77 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
78 key.offset = (u64)-1;
79
Yan, Zheng76dda932009-09-21 16:00:26 -040080 index = srcu_read_lock(&fs_info->subvol_srcu);
81
82 root = btrfs_read_fs_root_no_name(fs_info, &key);
83 if (IS_ERR(root)) {
84 err = PTR_ERR(root);
85 goto fail;
86 }
87
88 if (btrfs_root_refs(&root->root_item) == 0) {
89 err = -ENOENT;
90 goto fail;
91 }
David Woodhouse2d4d9fbd2008-08-19 22:20:17 +010092
Balaji Raobe6e8dc2008-07-21 02:01:56 +053093 key.objectid = objectid;
94 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
95 key.offset = 0;
96
Josef Bacik73f73412009-12-04 17:38:27 +000097 inode = btrfs_iget(sb, &key, root, NULL);
Yan, Zheng76dda932009-09-21 16:00:26 -040098 if (IS_ERR(inode)) {
99 err = PTR_ERR(inode);
100 goto fail;
101 }
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530102
Yan, Zheng76dda932009-09-21 16:00:26 -0400103 srcu_read_unlock(&fs_info->subvol_srcu, index);
104
105 if (check_generation && generation != inode->i_generation) {
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530106 iput(inode);
107 return ERR_PTR(-ESTALE);
108 }
109
Al Viroaf53d292010-12-20 10:56:06 -0500110 return d_obtain_alias(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -0400111fail:
112 srcu_read_unlock(&fs_info->subvol_srcu, index);
113 return ERR_PTR(err);
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530114}
115
116static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
117 int fh_len, int fh_type)
118{
119 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
120 u64 objectid, root_objectid;
121 u32 generation;
122
123 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
124 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
125 return NULL;
126 root_objectid = fid->root_objectid;
127 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
128 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
129 return NULL;
130 root_objectid = fid->parent_root_objectid;
131 } else
132 return NULL;
133
134 objectid = fid->parent_objectid;
135 generation = fid->parent_gen;
136
Yan, Zheng76dda932009-09-21 16:00:26 -0400137 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530138}
139
140static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
141 int fh_len, int fh_type)
142{
143 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
144 u64 objectid, root_objectid;
145 u32 generation;
146
147 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
148 fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
149 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
150 fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
151 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
152 fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
153 return NULL;
154
155 objectid = fid->objectid;
156 root_objectid = fid->root_objectid;
157 generation = fid->gen;
158
Yan, Zheng76dda932009-09-21 16:00:26 -0400159 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530160}
161
162static struct dentry *btrfs_get_parent(struct dentry *child)
163{
164 struct inode *dir = child->d_inode;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530165 struct btrfs_root *root = BTRFS_I(dir)->root;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530166 struct btrfs_path *path;
167 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400168 struct btrfs_root_ref *ref;
169 struct btrfs_key key;
170 struct btrfs_key found_key;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530171 int ret;
172
173 path = btrfs_alloc_path();
174
Yan, Zheng76dda932009-09-21 16:00:26 -0400175 if (dir->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
176 key.objectid = root->root_key.objectid;
177 key.type = BTRFS_ROOT_BACKREF_KEY;
178 key.offset = (u64)-1;
179 root = root->fs_info->tree_root;
180 } else {
181 key.objectid = dir->i_ino;
182 key.type = BTRFS_INODE_REF_KEY;
183 key.offset = (u64)-1;
184 }
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530185
David Woodhouse87acb4e2008-08-18 22:50:22 +0100186 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -0400187 if (ret < 0)
188 goto fail;
189
190 BUG_ON(ret == 0);
191 if (path->slots[0] == 0) {
192 ret = -ENOENT;
193 goto fail;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530194 }
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530195
Yan, Zheng76dda932009-09-21 16:00:26 -0400196 path->slots[0]--;
197 leaf = path->nodes[0];
198
199 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
200 if (found_key.objectid != key.objectid || found_key.type != key.type) {
201 ret = -ENOENT;
202 goto fail;
203 }
204
205 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
206 ref = btrfs_item_ptr(leaf, path->slots[0],
207 struct btrfs_root_ref);
208 key.objectid = btrfs_root_ref_dirid(leaf, ref);
209 } else {
210 key.objectid = found_key.offset;
211 }
David Woodhouse87acb4e2008-08-18 22:50:22 +0100212 btrfs_free_path(path);
213
Yan, Zheng76dda932009-09-21 16:00:26 -0400214 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
215 return btrfs_get_dentry(root->fs_info->sb, key.objectid,
216 found_key.offset, 0, 0);
217 }
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530218
Yan, Zheng76dda932009-09-21 16:00:26 -0400219 key.type = BTRFS_INODE_ITEM_KEY;
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530220 key.offset = 0;
Al Viroaf53d292010-12-20 10:56:06 -0500221 return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
Yan, Zheng76dda932009-09-21 16:00:26 -0400222fail:
223 btrfs_free_path(path);
224 return ERR_PTR(ret);
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530225}
226
Josef Bacik2ede0da2010-11-17 18:54:54 +0000227static int btrfs_get_name(struct dentry *parent, char *name,
228 struct dentry *child)
229{
230 struct inode *inode = child->d_inode;
231 struct inode *dir = parent->d_inode;
232 struct btrfs_path *path;
233 struct btrfs_root *root = BTRFS_I(dir)->root;
234 struct btrfs_inode_ref *iref;
235 struct btrfs_root_ref *rref;
236 struct extent_buffer *leaf;
237 unsigned long name_ptr;
238 struct btrfs_key key;
239 int name_len;
240 int ret;
241
242 if (!dir || !inode)
243 return -EINVAL;
244
245 if (!S_ISDIR(dir->i_mode))
246 return -EINVAL;
247
248 path = btrfs_alloc_path();
249 if (!path)
250 return -ENOMEM;
251 path->leave_spinning = 1;
252
253 if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
254 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
255 key.type = BTRFS_ROOT_BACKREF_KEY;
256 key.offset = (u64)-1;
257 root = root->fs_info->tree_root;
258 } else {
259 key.objectid = inode->i_ino;
260 key.offset = dir->i_ino;
261 key.type = BTRFS_INODE_REF_KEY;
262 }
263
264 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
265 if (ret < 0) {
266 btrfs_free_path(path);
267 return ret;
268 } else if (ret > 0) {
269 if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
270 path->slots[0]--;
271 } else {
272 btrfs_free_path(path);
273 return -ENOENT;
274 }
275 }
276 leaf = path->nodes[0];
277
278 if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
279 rref = btrfs_item_ptr(leaf, path->slots[0],
280 struct btrfs_root_ref);
281 name_ptr = (unsigned long)(rref + 1);
282 name_len = btrfs_root_ref_name_len(leaf, rref);
283 } else {
284 iref = btrfs_item_ptr(leaf, path->slots[0],
285 struct btrfs_inode_ref);
286 name_ptr = (unsigned long)(iref + 1);
287 name_len = btrfs_inode_ref_name_len(leaf, iref);
288 }
289
290 read_extent_buffer(leaf, name, name_ptr, name_len);
291 btrfs_free_path(path);
292
293 /*
294 * have to add the null termination to make sure that reconnect_path
295 * gets the right len for strlen
296 */
297 name[name_len] = '\0';
298
299 return 0;
300}
301
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530302const struct export_operations btrfs_export_ops = {
303 .encode_fh = btrfs_encode_fh,
304 .fh_to_dentry = btrfs_fh_to_dentry,
305 .fh_to_parent = btrfs_fh_to_parent,
306 .get_parent = btrfs_get_parent,
Josef Bacik2ede0da2010-11-17 18:54:54 +0000307 .get_name = btrfs_get_name,
Balaji Raobe6e8dc2008-07-21 02:01:56 +0530308};