2 #include <linux/exportfs.h>
4 #include <linux/file.h>
5 #include <linux/module.h>
6 #include <linux/mount.h>
7 #include <linux/namei.h>
9 #define dprintk(fmt, args...) do{}while(0)
12 static int get_name(struct dentry *dentry, char *name,
13 struct dentry *child);
16 static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj)
18 struct dentry *result = ERR_PTR(-ESTALE);
20 if (sb->s_export_op->get_dentry) {
21 result = sb->s_export_op->get_dentry(sb, obj);
23 result = ERR_PTR(-ESTALE);
29 static int exportfs_get_name(struct dentry *dir, char *name,
32 struct export_operations *nop = dir->d_sb->s_export_op;
35 return nop->get_name(dir, name, child);
37 return get_name(dir, name, child);
41 * Check if the dentry or any of it's aliases is acceptable.
43 static struct dentry *
44 find_acceptable_alias(struct dentry *result,
45 int (*acceptable)(void *context, struct dentry *dentry),
48 struct dentry *dentry, *toput = NULL;
50 if (acceptable(context, result))
53 spin_lock(&dcache_lock);
54 list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
56 spin_unlock(&dcache_lock);
59 if (dentry != result && acceptable(context, dentry)) {
63 spin_lock(&dcache_lock);
66 spin_unlock(&dcache_lock);
74 * Find root of a disconnected subtree and return a reference to it.
76 static struct dentry *
77 find_disconnected_root(struct dentry *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;
85 spin_unlock(&dentry->d_lock);
88 spin_lock(&dentry->d_lock);
90 spin_unlock(&dentry->d_lock);
95 * find_exported_dentry - helper routine to implement export_operations->decode_fh
96 * @sb: The &super_block identifying the filesystem
97 * @obj: An opaque identifier of the object to be found - passed to
99 * @parent: An optional opqaue identifier of the parent of the object.
100 * @acceptable: A function used to test possible &dentries to see if they are
102 * @context: A parameter to @acceptable so that it knows on what basis to
105 * find_exported_dentry is the central helper routine to enable file systems
106 * to provide the decode_fh() export_operation. It's main task is to take
107 * an &inode, find or create an appropriate &dentry structure, and possibly
108 * splice this into the dcache in the correct place.
110 * The decode_fh() operation provided by the filesystem should call
111 * find_exported_dentry() with the same parameters that it received except
112 * that instead of the file handle fragment, pointers to opaque identifiers
113 * for the object and optionally its parent are passed. The default decode_fh
114 * routine passes one pointer to the start of the filehandle fragment, and
115 * one 8 bytes into the fragment. It is expected that most filesystems will
116 * take this approach, though the offset to the parent identifier may well be
119 * find_exported_dentry() will call get_dentry to get an dentry pointer from
120 * the file system. If any &dentry in the d_alias list is acceptable, it will
121 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
122 * &dentry into the dcache using get_name() and get_parent() to find the
127 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
128 int (*acceptable)(void *context, struct dentry *de),
131 struct dentry *result = NULL;
132 struct dentry *target_dir;
134 struct export_operations *nops = sb->s_export_op;
135 struct dentry *alias;
137 char nbuf[NAME_MAX+1];
140 * Attempt to find the inode.
142 result = exportfs_get_dentry(sb, obj);
146 if (S_ISDIR(result->d_inode->i_mode)) {
147 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
148 if (acceptable(context, result))
154 target_dir = dget(result);
156 alias = find_acceptable_alias(result, acceptable, context);
163 target_dir = exportfs_get_dentry(sb,parent);
164 if (IS_ERR(target_dir)) {
165 err = PTR_ERR(target_dir);
171 * Now we need to make sure that target_dir is properly connected.
172 * It may already be, as the flag isn't always updated when connection
174 * So, we walk up parent links until we find a connected directory,
175 * or we run out of directories. Then we find the parent, find
176 * the name of the child in that parent, and do a lookup.
177 * This should connect the child into the parent
181 /* it is possible that a confused file system might not let us complete
182 * the path to the root. For example, if get_parent returns a directory
183 * in which we cannot find a name for the child. While this implies a
184 * very sick filesystem we don't want it to cause knfsd to spin. Hence
185 * the noprogress counter. If we go through the loop 10 times (2 is
186 * probably enough) without getting anywhere, we just give up
189 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
190 struct dentry *pd = find_disconnected_root(target_dir);
193 /* must have found a connected parent - great */
194 spin_lock(&pd->d_lock);
195 pd->d_flags &= ~DCACHE_DISCONNECTED;
196 spin_unlock(&pd->d_lock);
198 } else if (pd == sb->s_root) {
199 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
200 spin_lock(&pd->d_lock);
201 pd->d_flags &= ~DCACHE_DISCONNECTED;
202 spin_unlock(&pd->d_lock);
206 * We have hit the top of a disconnected path, try to
207 * find parent and connect.
209 * Racing with some other process renaming a directory
210 * isn't much of a problem here. If someone renames
211 * the directory, it will end up properly connected,
212 * which is what we want
214 * Getting the parent can't be supported generically,
215 * the locking is too icky.
217 * Instead we just return EACCES. If server reboots
218 * or inodes get flushed, you lose
220 struct dentry *ppd = ERR_PTR(-EACCES);
223 mutex_lock(&pd->d_inode->i_mutex);
224 if (nops->get_parent)
225 ppd = nops->get_parent(pd);
226 mutex_unlock(&pd->d_inode->i_mutex);
230 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
231 pd->d_inode->i_ino, err);
235 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
236 err = exportfs_get_name(ppd, nbuf, pd);
241 /* some race between get_parent and
242 * get_name? just try again
247 dprintk("find_exported_dentry: found name: %s\n", nbuf);
248 mutex_lock(&ppd->d_inode->i_mutex);
249 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
250 mutex_unlock(&ppd->d_inode->i_mutex);
253 dprintk("find_exported_dentry: lookup failed: %d\n", err);
258 /* we didn't really want npd, we really wanted
259 * a side-effect of the lookup.
260 * hopefully, npd == pd, though it isn't really
261 * a problem if it isn't
266 printk("find_exported_dentry: npd != pd\n");
270 /* something went wrong, we have to give up */
278 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
279 /* something went wrong - oh-well */
284 /* if we weren't after a directory, have one more step to go */
285 if (result != target_dir) {
286 struct dentry *nresult;
287 err = exportfs_get_name(target_dir, nbuf, result);
289 mutex_lock(&target_dir->d_inode->i_mutex);
290 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
291 mutex_unlock(&target_dir->d_inode->i_mutex);
292 if (!IS_ERR(nresult)) {
293 if (nresult->d_inode) {
303 alias = find_acceptable_alias(result, acceptable, context);
307 /* drat - I just cannot find anything acceptable */
309 /* It might be justifiable to return ESTALE here,
310 * but the filehandle at-least looks reasonable good
311 * and it just be a permission problem, so returning
314 return ERR_PTR(-EACCES);
323 struct getdents_callback {
324 char *name; /* name that was found. It already points to a
325 buffer NAME_MAX+1 is size */
326 unsigned long ino; /* the inum we are looking for */
327 int found; /* inode matched? */
328 int sequence; /* sequence counter */
332 * A rather strange filldir function to capture
333 * the name matching the specified inode number.
335 static int filldir_one(void * __buf, const char * name, int len,
336 loff_t pos, u64 ino, unsigned int d_type)
338 struct getdents_callback *buf = __buf;
342 if (buf->ino == ino) {
343 memcpy(buf->name, name, len);
344 buf->name[len] = '\0';
352 * get_name - default export_operations->get_name function
353 * @dentry: the directory in which to find a name
354 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
355 * @child: the dentry for the child directory.
357 * calls readdir on the parent until it finds an entry with
358 * the same inode number as the child, and returns that.
360 static int get_name(struct dentry *dentry, char *name,
361 struct dentry *child)
363 struct inode *dir = dentry->d_inode;
366 struct getdents_callback buffer;
369 if (!dir || !S_ISDIR(dir->i_mode))
375 * Open the directory ...
377 file = dentry_open(dget(dentry), NULL, O_RDONLY);
378 error = PTR_ERR(file);
383 if (!file->f_op->readdir)
387 buffer.ino = child->d_inode->i_ino;
391 int old_seq = buffer.sequence;
393 error = vfs_readdir(file, filldir_one, &buffer);
402 if (old_seq == buffer.sequence)
413 * export_encode_fh - default export_operations->encode_fh function
414 * @dentry: the dentry to encode
415 * @fh: where to store the file handle fragment
416 * @max_len: maximum length to store there
417 * @connectable: whether to store parent information
419 * This default encode_fh function assumes that the 32 inode number
420 * is suitable for locating an inode, and that the generation number
421 * can be used to check that it is still valid. It places them in the
422 * filehandle fragment where export_decode_fh expects to find them.
424 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
427 struct inode * inode = dentry->d_inode;
431 if (len < 2 || (connectable && len < 4))
435 fh[0] = inode->i_ino;
436 fh[1] = inode->i_generation;
437 if (connectable && !S_ISDIR(inode->i_mode)) {
438 struct inode *parent;
440 spin_lock(&dentry->d_lock);
441 parent = dentry->d_parent->d_inode;
442 fh[2] = parent->i_ino;
443 fh[3] = parent->i_generation;
444 spin_unlock(&dentry->d_lock);
454 * export_decode_fh - default export_operations->decode_fh function
455 * @sb: The superblock
456 * @fh: pointer to the file handle fragment
457 * @fh_len: length of file handle fragment
458 * @acceptable: function for testing acceptability of dentrys
459 * @context: context for @acceptable
461 * This is the default decode_fh() function.
462 * a fileid_type of 1 indicates that the filehandlefragment
463 * just contains an object identifier understood by get_dentry.
464 * a fileid_type of 2 says that there is also a directory
465 * identifier 8 bytes in to the filehandlefragement.
467 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
469 int (*acceptable)(void *context, struct dentry *de),
473 parent[0] = parent[1] = 0;
474 if (fh_len < 2 || fileid_type > 2)
476 if (fileid_type == 2) {
477 if (fh_len > 2) parent[0] = fh[2];
478 if (fh_len > 3) parent[1] = fh[3];
480 return find_exported_dentry(sb, fh, parent,
481 acceptable, context);
484 int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
487 struct export_operations *nop = dentry->d_sb->s_export_op;
491 error = nop->encode_fh(dentry, fh, max_len, connectable);
493 error = export_encode_fh(dentry, fh, max_len, connectable);
497 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
499 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
500 int fileid_type, int (*acceptable)(void *, struct dentry *),
503 struct export_operations *nop = mnt->mnt_sb->s_export_op;
504 struct dentry *result;
506 if (nop->decode_fh) {
507 result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
508 acceptable, context);
510 result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
511 acceptable, context);
516 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
518 EXPORT_SYMBOL(find_exported_dentry);
520 MODULE_LICENSE("GPL");