blob: 1e6f556514d60a0c7a97c3173a3699e4e44a8dd3 [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 Hellwige2f99012006-01-18 17:43:52 -080040static struct dentry *
41find_acceptable_alias(struct dentry *result,
42 int (*acceptable)(void *context, struct dentry *dentry),
43 void *context)
44{
45 struct dentry *dentry, *toput = NULL;
46
47 spin_lock(&dcache_lock);
48 list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
49 dget_locked(dentry);
50 spin_unlock(&dcache_lock);
51 if (toput)
52 dput(toput);
53 if (dentry != result && acceptable(context, dentry)) {
54 dput(result);
55 return dentry;
56 }
57 spin_lock(&dcache_lock);
58 toput = dentry;
59 }
60 spin_unlock(&dcache_lock);
61
62 if (toput)
63 dput(toput);
64 return NULL;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/**
68 * find_exported_dentry - helper routine to implement export_operations->decode_fh
69 * @sb: The &super_block identifying the filesystem
70 * @obj: An opaque identifier of the object to be found - passed to
71 * get_inode
72 * @parent: An optional opqaue identifier of the parent of the object.
73 * @acceptable: A function used to test possible &dentries to see if they are
74 * acceptable
75 * @context: A parameter to @acceptable so that it knows on what basis to
76 * judge.
77 *
78 * find_exported_dentry is the central helper routine to enable file systems
79 * to provide the decode_fh() export_operation. It's main task is to take
80 * an &inode, find or create an appropriate &dentry structure, and possibly
81 * splice this into the dcache in the correct place.
82 *
83 * The decode_fh() operation provided by the filesystem should call
84 * find_exported_dentry() with the same parameters that it received except
85 * that instead of the file handle fragment, pointers to opaque identifiers
86 * for the object and optionally its parent are passed. The default decode_fh
87 * routine passes one pointer to the start of the filehandle fragment, and
88 * one 8 bytes into the fragment. It is expected that most filesystems will
89 * take this approach, though the offset to the parent identifier may well be
90 * different.
91 *
92 * find_exported_dentry() will call get_dentry to get an dentry pointer from
93 * the file system. If any &dentry in the d_alias list is acceptable, it will
94 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
95 * &dentry into the dcache using get_name() and get_parent() to find the
96 * appropriate place.
97 */
98
99struct dentry *
100find_exported_dentry(struct super_block *sb, void *obj, void *parent,
101 int (*acceptable)(void *context, struct dentry *de),
102 void *context)
103{
104 struct dentry *result = NULL;
105 struct dentry *target_dir;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700106 int err = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct export_operations *nops = sb->s_export_op;
Christoph Hellwige2f99012006-01-18 17:43:52 -0800108 struct dentry *alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int noprogress;
110 char nbuf[NAME_MAX+1];
111
112 /*
113 * Attempt to find the inode.
114 */
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700115 result = exportfs_get_dentry(sb, obj);
116 if (IS_ERR(result))
117 return result;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (S_ISDIR(result->d_inode->i_mode) &&
120 (result->d_flags & DCACHE_DISCONNECTED)) {
121 /* it is an unconnected directory, we must connect it */
122 ;
123 } else {
124 if (acceptable(context, result))
125 return result;
126 if (S_ISDIR(result->d_inode->i_mode)) {
Peter Staubach8c7b3892006-05-20 14:59:56 -0700127 err = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto err_result;
129 }
Christoph Hellwige2f99012006-01-18 17:43:52 -0800130
131 alias = find_acceptable_alias(result, acceptable, context);
132 if (alias)
133 return alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 /* It's a directory, or we are required to confirm the file's
137 * location in the tree based on the parent information
138 */
139 dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
140 if (S_ISDIR(result->d_inode->i_mode))
141 target_dir = dget(result);
142 else {
143 if (parent == NULL)
144 goto err_result;
145
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700146 target_dir = exportfs_get_dentry(sb,parent);
147 if (IS_ERR(target_dir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 err = PTR_ERR(target_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto err_result;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 /*
153 * Now we need to make sure that target_dir is properly connected.
154 * It may already be, as the flag isn't always updated when connection
155 * happens.
156 * So, we walk up parent links until we find a connected directory,
157 * or we run out of directories. Then we find the parent, find
158 * the name of the child in that parent, and do a lookup.
159 * This should connect the child into the parent
160 * We then repeat.
161 */
162
163 /* it is possible that a confused file system might not let us complete
164 * the path to the root. For example, if get_parent returns a directory
165 * in which we cannot find a name for the child. While this implies a
166 * very sick filesystem we don't want it to cause knfsd to spin. Hence
167 * the noprogress counter. If we go through the loop 10 times (2 is
168 * probably enough) without getting anywhere, we just give up
169 */
170 noprogress= 0;
171 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
172 struct dentry *pd = target_dir;
173
174 dget(pd);
175 spin_lock(&pd->d_lock);
176 while (!IS_ROOT(pd) &&
177 (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
178 struct dentry *parent = pd->d_parent;
179
180 dget(parent);
181 spin_unlock(&pd->d_lock);
182 dput(pd);
183 pd = parent;
184 spin_lock(&pd->d_lock);
185 }
186 spin_unlock(&pd->d_lock);
187
188 if (!IS_ROOT(pd)) {
189 /* must have found a connected parent - great */
190 spin_lock(&pd->d_lock);
191 pd->d_flags &= ~DCACHE_DISCONNECTED;
192 spin_unlock(&pd->d_lock);
193 noprogress = 0;
194 } else if (pd == sb->s_root) {
195 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
196 spin_lock(&pd->d_lock);
197 pd->d_flags &= ~DCACHE_DISCONNECTED;
198 spin_unlock(&pd->d_lock);
199 noprogress = 0;
200 } else {
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700201 /*
202 * We have hit the top of a disconnected path, try to
203 * find parent and connect.
204 *
205 * Racing with some other process renaming a directory
206 * isn't much of a problem here. If someone renames
207 * the directory, it will end up properly connected,
208 * which is what we want
209 *
210 * Getting the parent can't be supported generically,
211 * the locking is too icky.
212 *
213 * Instead we just return EACCES. If server reboots
214 * or inodes get flushed, you lose
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700216 struct dentry *ppd = ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct dentry *npd;
218
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800219 mutex_lock(&pd->d_inode->i_mutex);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700220 if (nops->get_parent)
221 ppd = nops->get_parent(pd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800222 mutex_unlock(&pd->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (IS_ERR(ppd)) {
225 err = PTR_ERR(ppd);
226 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
227 pd->d_inode->i_ino, err);
228 dput(pd);
229 break;
230 }
231 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700232 err = exportfs_get_name(ppd, nbuf, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (err) {
234 dput(ppd);
235 dput(pd);
236 if (err == -ENOENT)
237 /* some race between get_parent and
238 * get_name? just try again
239 */
240 continue;
241 break;
242 }
243 dprintk("find_exported_dentry: found name: %s\n", nbuf);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800244 mutex_lock(&ppd->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800246 mutex_unlock(&ppd->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (IS_ERR(npd)) {
248 err = PTR_ERR(npd);
249 dprintk("find_exported_dentry: lookup failed: %d\n", err);
250 dput(ppd);
251 dput(pd);
252 break;
253 }
254 /* we didn't really want npd, we really wanted
255 * a side-effect of the lookup.
256 * hopefully, npd == pd, though it isn't really
257 * a problem if it isn't
258 */
259 if (npd == pd)
260 noprogress = 0;
261 else
262 printk("find_exported_dentry: npd != pd\n");
263 dput(npd);
264 dput(ppd);
265 if (IS_ROOT(pd)) {
266 /* something went wrong, we have to give up */
267 dput(pd);
268 break;
269 }
270 }
271 dput(pd);
272 }
273
274 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
275 /* something went wrong - oh-well */
276 if (!err)
277 err = -ESTALE;
278 goto err_target;
279 }
280 /* if we weren't after a directory, have one more step to go */
281 if (result != target_dir) {
282 struct dentry *nresult;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700283 err = exportfs_get_name(target_dir, nbuf, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!err) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800285 mutex_lock(&target_dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800287 mutex_unlock(&target_dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!IS_ERR(nresult)) {
289 if (nresult->d_inode) {
290 dput(result);
291 result = nresult;
292 } else
293 dput(nresult);
294 }
295 }
296 }
297 dput(target_dir);
298 /* now result is properly connected, it is our best bet */
299 if (acceptable(context, result))
300 return result;
Christoph Hellwige2f99012006-01-18 17:43:52 -0800301
302 alias = find_acceptable_alias(result, acceptable, context);
303 if (alias)
304 return alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* drat - I just cannot find anything acceptable */
307 dput(result);
308 /* It might be justifiable to return ESTALE here,
309 * but the filehandle at-least looks reasonable good
310 * and it just be a permission problem, so returning
311 * -EACCESS is safer
312 */
313 return ERR_PTR(-EACCES);
314
315 err_target:
316 dput(target_dir);
317 err_result:
318 dput(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return ERR_PTR(err);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322struct getdents_callback {
323 char *name; /* name that was found. It already points to a
324 buffer NAME_MAX+1 is size */
325 unsigned long ino; /* the inum we are looking for */
326 int found; /* inode matched? */
327 int sequence; /* sequence counter */
328};
329
330/*
331 * A rather strange filldir function to capture
332 * the name matching the specified inode number.
333 */
334static int filldir_one(void * __buf, const char * name, int len,
David Howellsafefdbb2006-10-03 01:13:46 -0700335 loff_t pos, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct getdents_callback *buf = __buf;
338 int result = 0;
339
340 buf->sequence++;
341 if (buf->ino == ino) {
342 memcpy(buf->name, name, len);
343 buf->name[len] = '\0';
344 buf->found = 1;
345 result = -1;
346 }
347 return result;
348}
349
350/**
351 * get_name - default export_operations->get_name function
352 * @dentry: the directory in which to find a name
353 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
354 * @child: the dentry for the child directory.
355 *
356 * calls readdir on the parent until it finds an entry with
357 * the same inode number as the child, and returns that.
358 */
359static int get_name(struct dentry *dentry, char *name,
360 struct dentry *child)
361{
362 struct inode *dir = dentry->d_inode;
363 int error;
364 struct file *file;
365 struct getdents_callback buffer;
366
367 error = -ENOTDIR;
368 if (!dir || !S_ISDIR(dir->i_mode))
369 goto out;
370 error = -EINVAL;
371 if (!dir->i_fop)
372 goto out;
373 /*
374 * Open the directory ...
375 */
376 file = dentry_open(dget(dentry), NULL, O_RDONLY);
377 error = PTR_ERR(file);
378 if (IS_ERR(file))
379 goto out;
380
381 error = -EINVAL;
382 if (!file->f_op->readdir)
383 goto out_close;
384
385 buffer.name = name;
386 buffer.ino = child->d_inode->i_ino;
387 buffer.found = 0;
388 buffer.sequence = 0;
389 while (1) {
390 int old_seq = buffer.sequence;
391
392 error = vfs_readdir(file, filldir_one, &buffer);
393
394 if (error < 0)
395 break;
396
397 error = 0;
398 if (buffer.found)
399 break;
400 error = -ENOENT;
401 if (old_seq == buffer.sequence)
402 break;
403 }
404
405out_close:
406 fput(file);
407out:
408 return error;
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/**
412 * export_encode_fh - default export_operations->encode_fh function
413 * @dentry: the dentry to encode
414 * @fh: where to store the file handle fragment
415 * @max_len: maximum length to store there
416 * @connectable: whether to store parent information
417 *
418 * This default encode_fh function assumes that the 32 inode number
419 * is suitable for locating an inode, and that the generation number
420 * can be used to check that it is still valid. It places them in the
421 * filehandle fragment where export_decode_fh expects to find them.
422 */
423static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
424 int connectable)
425{
426 struct inode * inode = dentry->d_inode;
427 int len = *max_len;
428 int type = 1;
429
430 if (len < 2 || (connectable && len < 4))
431 return 255;
432
433 len = 2;
434 fh[0] = inode->i_ino;
435 fh[1] = inode->i_generation;
436 if (connectable && !S_ISDIR(inode->i_mode)) {
437 struct inode *parent;
438
439 spin_lock(&dentry->d_lock);
440 parent = dentry->d_parent->d_inode;
441 fh[2] = parent->i_ino;
442 fh[3] = parent->i_generation;
443 spin_unlock(&dentry->d_lock);
444 len = 4;
445 type = 2;
446 }
447 *max_len = len;
448 return type;
449}
450
451
452/**
453 * export_decode_fh - default export_operations->decode_fh function
454 * @sb: The superblock
455 * @fh: pointer to the file handle fragment
456 * @fh_len: length of file handle fragment
457 * @acceptable: function for testing acceptability of dentrys
458 * @context: context for @acceptable
459 *
460 * This is the default decode_fh() function.
461 * a fileid_type of 1 indicates that the filehandlefragment
462 * just contains an object identifier understood by get_dentry.
463 * a fileid_type of 2 says that there is also a directory
464 * identifier 8 bytes in to the filehandlefragement.
465 */
466static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
467 int fileid_type,
468 int (*acceptable)(void *context, struct dentry *de),
469 void *context)
470{
471 __u32 parent[2];
472 parent[0] = parent[1] = 0;
473 if (fh_len < 2 || fileid_type > 2)
474 return NULL;
475 if (fileid_type == 2) {
476 if (fh_len > 2) parent[0] = fh[2];
477 if (fh_len > 3) parent[1] = fh[3];
478 }
479 return find_exported_dentry(sb, fh, parent,
480 acceptable, context);
481}
482
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700483int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
484 int connectable)
485{
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700486 struct export_operations *nop = dentry->d_sb->s_export_op;
487 int error;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700488
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700489 if (nop->encode_fh)
490 error = nop->encode_fh(dentry, fh, max_len, connectable);
491 else
492 error = export_encode_fh(dentry, fh, max_len, connectable);
493
494 return error;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700495}
496EXPORT_SYMBOL_GPL(exportfs_encode_fh);
497
498struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
499 int fileid_type, int (*acceptable)(void *, struct dentry *),
500 void *context)
501{
502 struct export_operations *nop = mnt->mnt_sb->s_export_op;
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700503 struct dentry *result;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700504
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700505 if (nop->decode_fh) {
506 result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700507 acceptable, context);
Christoph Hellwig10f11c32007-07-17 04:04:31 -0700508 } else {
509 result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
510 acceptable, context);
511 }
512
513 return result;
Christoph Hellwigd37065c2007-07-17 04:04:30 -0700514}
515EXPORT_SYMBOL_GPL(exportfs_decode_fh);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517EXPORT_SYMBOL(find_exported_dentry);
518
519MODULE_LICENSE("GPL");