blob: 813011aad7003990a7bed1e703c4c150f0675025 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
Christoph Hellwiga5694252007-07-17 04:04:28 -07002#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/fs.h>
4#include <linux/file.h>
5#include <linux/module.h>
Christoph Hellwigd37065c2007-07-17 04:04:30 -07006#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/namei.h>
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#define dprintk(fmt, args...) do{}while(0)
10
Christoph Hellwig10f11c32007-07-17 04:04:31 -070011
12static int get_name(struct dentry *dentry, char *name,
13 struct dentry *child);
14
15
16static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj)
17{
18 struct dentry *result = ERR_PTR(-ESTALE);
19
20 if (sb->s_export_op->get_dentry) {
21 result = sb->s_export_op->get_dentry(sb, obj);
22 if (!result)
23 result = ERR_PTR(-ESTALE);
24 }
25
26 return result;
27}
28
29static int exportfs_get_name(struct dentry *dir, char *name,
30 struct dentry *child)
31{
32 struct export_operations *nop = dir->d_sb->s_export_op;
33
34 if (nop->get_name)
35 return nop->get_name(dir, name, child);
36 else
37 return get_name(dir, name, child);
38}
39
Christoph Hellwigfb66a192007-07-17 04:04:32 -070040/*
41 * Check if the dentry or any of it's aliases is acceptable.
42 */
Christoph Hellwige2f99012006-01-18 17:43:52 -080043static struct dentry *
44find_acceptable_alias(struct dentry *result,
45 int (*acceptable)(void *context, struct dentry *dentry),
46 void *context)
47{
48 struct dentry *dentry, *toput = NULL;
49
Christoph Hellwigfb66a192007-07-17 04:04:32 -070050 if (acceptable(context, result))
51 return result;
52
Christoph Hellwige2f99012006-01-18 17:43:52 -080053 spin_lock(&dcache_lock);
54 list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
55 dget_locked(dentry);
56 spin_unlock(&dcache_lock);
57 if (toput)
58 dput(toput);
59 if (dentry != result && acceptable(context, dentry)) {
60 dput(result);
61 return dentry;
62 }
63 spin_lock(&dcache_lock);
64 toput = dentry;
65 }
66 spin_unlock(&dcache_lock);
67
68 if (toput)
69 dput(toput);
70 return NULL;
71}
72
Christoph Hellwigdd90b502007-07-17 04:04:32 -070073/*
74 * Find root of a disconnected subtree and return a reference to it.
75 */
76static struct dentry *
77find_disconnected_root(struct dentry *dentry)
78{
79 dget(dentry);
80 spin_lock(&dentry->d_lock);
81 while (!IS_ROOT(dentry) &&
82 (dentry->d_parent->d_flags & DCACHE_DISCONNECTED)) {
83 struct dentry *parent = dentry->d_parent;
84 dget(parent);
85 spin_unlock(&dentry->d_lock);
86 dput(dentry);
87 dentry = parent;
88 spin_lock(&dentry->d_lock);
89 }
90 spin_unlock(&dentry->d_lock);
91 return dentry;
92}
93
Christoph Hellwig019ab802007-07-17 04:04:33 -070094
95/*
96 * Make sure target_dir is fully connected to the dentry tree.
97 *
98 * It may already be, as the flag isn't always updated when connection happens.
99 */
100static int
101reconnect_path(struct super_block *sb, struct dentry *target_dir)
102{
103 char nbuf[NAME_MAX+1];
104 int noprogress = 0;
105 int err = -ESTALE;
106
107 /*
108 * It is possible that a confused file system might not let us complete
109 * the path to the root. For example, if get_parent returns a directory
110 * in which we cannot find a name for the child. While this implies a
111 * very sick filesystem we don't want it to cause knfsd to spin. Hence
112 * the noprogress counter. If we go through the loop 10 times (2 is
113 * probably enough) without getting anywhere, we just give up
114 */
115 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
116 struct dentry *pd = find_disconnected_root(target_dir);
117
118 if (!IS_ROOT(pd)) {
119 /* must have found a connected parent - great */
120 spin_lock(&pd->d_lock);
121 pd->d_flags &= ~DCACHE_DISCONNECTED;
122 spin_unlock(&pd->d_lock);
123 noprogress = 0;
124 } else if (pd == sb->s_root) {
125 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
126 spin_lock(&pd->d_lock);
127 pd->d_flags &= ~DCACHE_DISCONNECTED;
128 spin_unlock(&pd->d_lock);
129 noprogress = 0;
130 } else {
131 /*
132 * We have hit the top of a disconnected path, try to
133 * find parent and connect.
134 *
135 * Racing with some other process renaming a directory
136 * isn't much of a problem here. If someone renames
137 * the directory, it will end up properly connected,
138 * which is what we want
139 *
140 * Getting the parent can't be supported generically,
141 * the locking is too icky.
142 *
143 * Instead we just return EACCES. If server reboots
144 * or inodes get flushed, you lose
145 */
146 struct dentry *ppd = ERR_PTR(-EACCES);
147 struct dentry *npd;
148
149 mutex_lock(&pd->d_inode->i_mutex);
150 if (sb->s_export_op->get_parent)
151 ppd = sb->s_export_op->get_parent(pd);
152 mutex_unlock(&pd->d_inode->i_mutex);
153
154 if (IS_ERR(ppd)) {
155 err = PTR_ERR(ppd);
156 dprintk("%s: get_parent of %ld failed, err %d\n",
157 __FUNCTION__, pd->d_inode->i_ino, err);
158 dput(pd);
159 break;
160 }
161
162 dprintk("%s: find name of %lu in %lu\n", __FUNCTION__,
163 pd->d_inode->i_ino, ppd->d_inode->i_ino);
164 err = exportfs_get_name(ppd, nbuf, pd);
165 if (err) {
166 dput(ppd);
167 dput(pd);
168 if (err == -ENOENT)
169 /* some race between get_parent and
170 * get_name? just try again
171 */
172 continue;
173 break;
174 }
175 dprintk("%s: found name: %s\n", __FUNCTION__, nbuf);
176 mutex_lock(&ppd->d_inode->i_mutex);
177 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
178 mutex_unlock(&ppd->d_inode->i_mutex);
179 if (IS_ERR(npd)) {
180 err = PTR_ERR(npd);
181 dprintk("%s: lookup failed: %d\n",
182 __FUNCTION__, err);
183 dput(ppd);
184 dput(pd);
185 break;
186 }
187 /* we didn't really want npd, we really wanted
188 * a side-effect of the lookup.
189 * hopefully, npd == pd, though it isn't really
190 * a problem if it isn't
191 */
192 if (npd == pd)
193 noprogress = 0;
194 else
195 printk("%s: npd != pd\n", __FUNCTION__);
196 dput(npd);
197 dput(ppd);
198 if (IS_ROOT(pd)) {
199 /* something went wrong, we have to give up */
200 dput(pd);
201 break;
202 }
203 }
204 dput(pd);
205 }
206
207 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
208 /* something went wrong - oh-well */
209 if (!err)
210 err = -ESTALE;
211 return err;
212 }
213
214 return 0;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/**
218 * find_exported_dentry - helper routine to implement export_operations->decode_fh
219 * @sb: The &super_block identifying the filesystem
220 * @obj: An opaque identifier of the object to be found - passed to
221 * get_inode
222 * @parent: An optional opqaue identifier of the parent of the object.
223 * @acceptable: A function used to test possible &dentries to see if they are
224 * acceptable
225 * @context: A parameter to @acceptable so that it knows on what basis to
226 * judge.
227 *
228 * find_exported_dentry is the central helper routine to enable file systems
229 * to provide the decode_fh() export_operation. It's main task is to take
230 * an &inode, find or create an appropriate &dentry structure, and possibly
231 * splice this into the dcache in the correct place.
232 *
233 * The decode_fh() operation provided by the filesystem should call
234 * find_exported_dentry() with the same parameters that it received except
235 * that instead of the file handle fragment, pointers to opaque identifiers
236 * for the object and optionally its parent are passed. The default decode_fh
237 * routine passes one pointer to the start of the filehandle fragment, and
238 * one 8 bytes into the fragment. It is expected that most filesystems will
239 * take this approach, though the offset to the parent identifier may well be
240 * different.
241 *
242 * find_exported_dentry() will call get_dentry to get an dentry pointer from
243 * the file system. If any &dentry in the d_alias list is acceptable, it will
244 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
245 * &dentry into the dcache using get_name() and get_parent() to find the
246 * appropriate place.
247 */
248
249struct dentry *
250find_exported_dentry(struct super_block *sb, void *obj, void *parent,
251 int (*acceptable)(void *context, struct dentry *de),
252 void *context)
253{
Christoph Hellwig019ab802007-07-17 04:04:33 -0700254 struct dentry *result, *alias;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700255 int err = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /*
258 * Attempt to find the inode.
259 */
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700260 result = exportfs_get_dentry(sb, obj);
261 if (IS_ERR(result))
262 return result;
263
Christoph Hellwigd7dd6182007-07-17 04:04:31 -0700264 if (S_ISDIR(result->d_inode->i_mode)) {
265 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
266 if (acceptable(context, result))
267 return result;
Peter Staubach8c7b3892006-05-20 14:59:56 -0700268 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 goto err_result;
270 }
Christoph Hellwige2f99012006-01-18 17:43:52 -0800271
Christoph Hellwig019ab802007-07-17 04:04:33 -0700272 err = reconnect_path(sb, result);
273 if (err)
274 goto err_result;
Christoph Hellwigd7dd6182007-07-17 04:04:31 -0700275 } else {
Christoph Hellwig019ab802007-07-17 04:04:33 -0700276 struct dentry *target_dir, *nresult;
277 char nbuf[NAME_MAX+1];
278
Christoph Hellwige2f99012006-01-18 17:43:52 -0800279 alias = find_acceptable_alias(result, acceptable, context);
280 if (alias)
281 return alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (parent == NULL)
284 goto err_result;
285
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700286 target_dir = exportfs_get_dentry(sb,parent);
287 if (IS_ERR(target_dir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 err = PTR_ERR(target_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto err_result;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700290 }
Christoph Hellwigd7dd6182007-07-17 04:04:31 -0700291
Christoph Hellwig019ab802007-07-17 04:04:33 -0700292 err = reconnect_path(sb, target_dir);
293 if (err) {
294 dput(target_dir);
295 goto err_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Christoph Hellwig019ab802007-07-17 04:04:33 -0700298 /*
299 * As we weren't after a directory, have one more step to go.
300 */
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700301 err = exportfs_get_name(target_dir, nbuf, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!err) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800303 mutex_lock(&target_dir->d_inode->i_mutex);
Christoph Hellwig019ab802007-07-17 04:04:33 -0700304 nresult = lookup_one_len(nbuf, target_dir,
305 strlen(nbuf));
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800306 mutex_unlock(&target_dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!IS_ERR(nresult)) {
308 if (nresult->d_inode) {
309 dput(result);
310 result = nresult;
311 } else
312 dput(nresult);
313 }
314 }
Christoph Hellwig019ab802007-07-17 04:04:33 -0700315 dput(target_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Christoph Hellwige2f99012006-01-18 17:43:52 -0800317
318 alias = find_acceptable_alias(result, acceptable, context);
319 if (alias)
320 return alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 /* drat - I just cannot find anything acceptable */
323 dput(result);
324 /* It might be justifiable to return ESTALE here,
325 * but the filehandle at-least looks reasonable good
Christoph Hellwig019ab802007-07-17 04:04:33 -0700326 * and it may just be a permission problem, so returning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * -EACCESS is safer
328 */
329 return ERR_PTR(-EACCES);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 err_result:
332 dput(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return ERR_PTR(err);
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336struct getdents_callback {
337 char *name; /* name that was found. It already points to a
338 buffer NAME_MAX+1 is size */
339 unsigned long ino; /* the inum we are looking for */
340 int found; /* inode matched? */
341 int sequence; /* sequence counter */
342};
343
344/*
345 * A rather strange filldir function to capture
346 * the name matching the specified inode number.
347 */
348static int filldir_one(void * __buf, const char * name, int len,
David Howellsafefdbb2006-10-03 01:13:46 -0700349 loff_t pos, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct getdents_callback *buf = __buf;
352 int result = 0;
353
354 buf->sequence++;
355 if (buf->ino == ino) {
356 memcpy(buf->name, name, len);
357 buf->name[len] = '\0';
358 buf->found = 1;
359 result = -1;
360 }
361 return result;
362}
363
364/**
365 * get_name - default export_operations->get_name function
366 * @dentry: the directory in which to find a name
367 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
368 * @child: the dentry for the child directory.
369 *
370 * calls readdir on the parent until it finds an entry with
371 * the same inode number as the child, and returns that.
372 */
373static int get_name(struct dentry *dentry, char *name,
374 struct dentry *child)
375{
376 struct inode *dir = dentry->d_inode;
377 int error;
378 struct file *file;
379 struct getdents_callback buffer;
380
381 error = -ENOTDIR;
382 if (!dir || !S_ISDIR(dir->i_mode))
383 goto out;
384 error = -EINVAL;
385 if (!dir->i_fop)
386 goto out;
387 /*
388 * Open the directory ...
389 */
390 file = dentry_open(dget(dentry), NULL, O_RDONLY);
391 error = PTR_ERR(file);
392 if (IS_ERR(file))
393 goto out;
394
395 error = -EINVAL;
396 if (!file->f_op->readdir)
397 goto out_close;
398
399 buffer.name = name;
400 buffer.ino = child->d_inode->i_ino;
401 buffer.found = 0;
402 buffer.sequence = 0;
403 while (1) {
404 int old_seq = buffer.sequence;
405
406 error = vfs_readdir(file, filldir_one, &buffer);
407
408 if (error < 0)
409 break;
410
411 error = 0;
412 if (buffer.found)
413 break;
414 error = -ENOENT;
415 if (old_seq == buffer.sequence)
416 break;
417 }
418
419out_close:
420 fput(file);
421out:
422 return error;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/**
426 * export_encode_fh - default export_operations->encode_fh function
427 * @dentry: the dentry to encode
428 * @fh: where to store the file handle fragment
429 * @max_len: maximum length to store there
430 * @connectable: whether to store parent information
431 *
432 * This default encode_fh function assumes that the 32 inode number
433 * is suitable for locating an inode, and that the generation number
434 * can be used to check that it is still valid. It places them in the
435 * filehandle fragment where export_decode_fh expects to find them.
436 */
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700437static int export_encode_fh(struct dentry *dentry, struct fid *fid,
438 int *max_len, int connectable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct inode * inode = dentry->d_inode;
441 int len = *max_len;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700442 int type = FILEID_INO32_GEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (len < 2 || (connectable && len < 4))
445 return 255;
446
447 len = 2;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700448 fid->i32.ino = inode->i_ino;
449 fid->i32.gen = inode->i_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (connectable && !S_ISDIR(inode->i_mode)) {
451 struct inode *parent;
452
453 spin_lock(&dentry->d_lock);
454 parent = dentry->d_parent->d_inode;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700455 fid->i32.parent_ino = parent->i_ino;
456 fid->i32.parent_gen = parent->i_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 spin_unlock(&dentry->d_lock);
458 len = 4;
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700459 type = FILEID_INO32_GEN_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 *max_len = len;
462 return type;
463}
464
465
466/**
467 * export_decode_fh - default export_operations->decode_fh function
468 * @sb: The superblock
469 * @fh: pointer to the file handle fragment
470 * @fh_len: length of file handle fragment
471 * @acceptable: function for testing acceptability of dentrys
472 * @context: context for @acceptable
473 *
474 * This is the default decode_fh() function.
475 * a fileid_type of 1 indicates that the filehandlefragment
476 * just contains an object identifier understood by get_dentry.
477 * a fileid_type of 2 says that there is also a directory
478 * identifier 8 bytes in to the filehandlefragement.
479 */
480static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
481 int fileid_type,
482 int (*acceptable)(void *context, struct dentry *de),
483 void *context)
484{
485 __u32 parent[2];
486 parent[0] = parent[1] = 0;
487 if (fh_len < 2 || fileid_type > 2)
488 return NULL;
489 if (fileid_type == 2) {
490 if (fh_len > 2) parent[0] = fh[2];
491 if (fh_len > 3) parent[1] = fh[3];
492 }
493 return find_exported_dentry(sb, fh, parent,
494 acceptable, context);
495}
496
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700497int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700498 int connectable)
499{
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700500 struct export_operations *nop = dentry->d_sb->s_export_op;
501 int error;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700502
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700503 if (nop->encode_fh)
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700504 error = nop->encode_fh(dentry, fid->raw, max_len, connectable);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700505 else
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700506 error = export_encode_fh(dentry, fid, max_len, connectable);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700507
508 return error;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700509}
510EXPORT_SYMBOL_GPL(exportfs_encode_fh);
511
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700512struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
513 int fh_len, int fileid_type,
514 int (*acceptable)(void *, struct dentry *), void *context)
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700515{
516 struct export_operations *nop = mnt->mnt_sb->s_export_op;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700517 struct dentry *result;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700518
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700519 if (nop->decode_fh) {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700520 result = nop->decode_fh(mnt->mnt_sb, fid->raw, fh_len,
521 fileid_type, acceptable, context);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700522 } else {
Christoph Hellwig6e91ea22007-10-21 16:42:03 -0700523 result = export_decode_fh(mnt->mnt_sb, fid->raw, fh_len,
524 fileid_type, acceptable, context);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700525 }
526
527 return result;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700528}
529EXPORT_SYMBOL_GPL(exportfs_decode_fh);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531EXPORT_SYMBOL(find_exported_dentry);
532
533MODULE_LICENSE("GPL");