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);
40 static struct dentry *
41 find_acceptable_alias(struct dentry *result,
42 int (*acceptable)(void *context, struct dentry *dentry),
45 struct dentry *dentry, *toput = NULL;
47 spin_lock(&dcache_lock);
48 list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
50 spin_unlock(&dcache_lock);
53 if (dentry != result && acceptable(context, dentry)) {
57 spin_lock(&dcache_lock);
60 spin_unlock(&dcache_lock);
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
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
75 * @context: A parameter to @acceptable so that it knows on what basis to
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.
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
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
100 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
101 int (*acceptable)(void *context, struct dentry *de),
104 struct dentry *result = NULL;
105 struct dentry *target_dir;
107 struct export_operations *nops = sb->s_export_op;
108 struct dentry *alias;
110 char nbuf[NAME_MAX+1];
113 * Attempt to find the inode.
115 result = exportfs_get_dentry(sb, obj);
119 if (S_ISDIR(result->d_inode->i_mode)) {
120 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
121 if (acceptable(context, result))
127 target_dir = dget(result);
129 if (acceptable(context, result))
132 alias = find_acceptable_alias(result, acceptable, context);
139 target_dir = exportfs_get_dentry(sb,parent);
140 if (IS_ERR(target_dir)) {
141 err = PTR_ERR(target_dir);
147 * Now we need to make sure that target_dir is properly connected.
148 * It may already be, as the flag isn't always updated when connection
150 * So, we walk up parent links until we find a connected directory,
151 * or we run out of directories. Then we find the parent, find
152 * the name of the child in that parent, and do a lookup.
153 * This should connect the child into the parent
157 /* it is possible that a confused file system might not let us complete
158 * the path to the root. For example, if get_parent returns a directory
159 * in which we cannot find a name for the child. While this implies a
160 * very sick filesystem we don't want it to cause knfsd to spin. Hence
161 * the noprogress counter. If we go through the loop 10 times (2 is
162 * probably enough) without getting anywhere, we just give up
165 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
166 struct dentry *pd = target_dir;
169 spin_lock(&pd->d_lock);
170 while (!IS_ROOT(pd) &&
171 (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
172 struct dentry *parent = pd->d_parent;
175 spin_unlock(&pd->d_lock);
178 spin_lock(&pd->d_lock);
180 spin_unlock(&pd->d_lock);
183 /* must have found a connected parent - great */
184 spin_lock(&pd->d_lock);
185 pd->d_flags &= ~DCACHE_DISCONNECTED;
186 spin_unlock(&pd->d_lock);
188 } else if (pd == sb->s_root) {
189 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
190 spin_lock(&pd->d_lock);
191 pd->d_flags &= ~DCACHE_DISCONNECTED;
192 spin_unlock(&pd->d_lock);
196 * We have hit the top of a disconnected path, try to
197 * find parent and connect.
199 * Racing with some other process renaming a directory
200 * isn't much of a problem here. If someone renames
201 * the directory, it will end up properly connected,
202 * which is what we want
204 * Getting the parent can't be supported generically,
205 * the locking is too icky.
207 * Instead we just return EACCES. If server reboots
208 * or inodes get flushed, you lose
210 struct dentry *ppd = ERR_PTR(-EACCES);
213 mutex_lock(&pd->d_inode->i_mutex);
214 if (nops->get_parent)
215 ppd = nops->get_parent(pd);
216 mutex_unlock(&pd->d_inode->i_mutex);
220 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
221 pd->d_inode->i_ino, err);
225 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
226 err = exportfs_get_name(ppd, nbuf, pd);
231 /* some race between get_parent and
232 * get_name? just try again
237 dprintk("find_exported_dentry: found name: %s\n", nbuf);
238 mutex_lock(&ppd->d_inode->i_mutex);
239 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
240 mutex_unlock(&ppd->d_inode->i_mutex);
243 dprintk("find_exported_dentry: lookup failed: %d\n", err);
248 /* we didn't really want npd, we really wanted
249 * a side-effect of the lookup.
250 * hopefully, npd == pd, though it isn't really
251 * a problem if it isn't
256 printk("find_exported_dentry: npd != pd\n");
260 /* something went wrong, we have to give up */
268 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
269 /* something went wrong - oh-well */
274 /* if we weren't after a directory, have one more step to go */
275 if (result != target_dir) {
276 struct dentry *nresult;
277 err = exportfs_get_name(target_dir, nbuf, result);
279 mutex_lock(&target_dir->d_inode->i_mutex);
280 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
281 mutex_unlock(&target_dir->d_inode->i_mutex);
282 if (!IS_ERR(nresult)) {
283 if (nresult->d_inode) {
292 /* now result is properly connected, it is our best bet */
293 if (acceptable(context, result))
296 alias = find_acceptable_alias(result, acceptable, context);
300 /* drat - I just cannot find anything acceptable */
302 /* It might be justifiable to return ESTALE here,
303 * but the filehandle at-least looks reasonable good
304 * and it just be a permission problem, so returning
307 return ERR_PTR(-EACCES);
316 struct getdents_callback {
317 char *name; /* name that was found. It already points to a
318 buffer NAME_MAX+1 is size */
319 unsigned long ino; /* the inum we are looking for */
320 int found; /* inode matched? */
321 int sequence; /* sequence counter */
325 * A rather strange filldir function to capture
326 * the name matching the specified inode number.
328 static int filldir_one(void * __buf, const char * name, int len,
329 loff_t pos, u64 ino, unsigned int d_type)
331 struct getdents_callback *buf = __buf;
335 if (buf->ino == ino) {
336 memcpy(buf->name, name, len);
337 buf->name[len] = '\0';
345 * get_name - default export_operations->get_name function
346 * @dentry: the directory in which to find a name
347 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
348 * @child: the dentry for the child directory.
350 * calls readdir on the parent until it finds an entry with
351 * the same inode number as the child, and returns that.
353 static int get_name(struct dentry *dentry, char *name,
354 struct dentry *child)
356 struct inode *dir = dentry->d_inode;
359 struct getdents_callback buffer;
362 if (!dir || !S_ISDIR(dir->i_mode))
368 * Open the directory ...
370 file = dentry_open(dget(dentry), NULL, O_RDONLY);
371 error = PTR_ERR(file);
376 if (!file->f_op->readdir)
380 buffer.ino = child->d_inode->i_ino;
384 int old_seq = buffer.sequence;
386 error = vfs_readdir(file, filldir_one, &buffer);
395 if (old_seq == buffer.sequence)
406 * export_encode_fh - default export_operations->encode_fh function
407 * @dentry: the dentry to encode
408 * @fh: where to store the file handle fragment
409 * @max_len: maximum length to store there
410 * @connectable: whether to store parent information
412 * This default encode_fh function assumes that the 32 inode number
413 * is suitable for locating an inode, and that the generation number
414 * can be used to check that it is still valid. It places them in the
415 * filehandle fragment where export_decode_fh expects to find them.
417 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
420 struct inode * inode = dentry->d_inode;
424 if (len < 2 || (connectable && len < 4))
428 fh[0] = inode->i_ino;
429 fh[1] = inode->i_generation;
430 if (connectable && !S_ISDIR(inode->i_mode)) {
431 struct inode *parent;
433 spin_lock(&dentry->d_lock);
434 parent = dentry->d_parent->d_inode;
435 fh[2] = parent->i_ino;
436 fh[3] = parent->i_generation;
437 spin_unlock(&dentry->d_lock);
447 * export_decode_fh - default export_operations->decode_fh function
448 * @sb: The superblock
449 * @fh: pointer to the file handle fragment
450 * @fh_len: length of file handle fragment
451 * @acceptable: function for testing acceptability of dentrys
452 * @context: context for @acceptable
454 * This is the default decode_fh() function.
455 * a fileid_type of 1 indicates that the filehandlefragment
456 * just contains an object identifier understood by get_dentry.
457 * a fileid_type of 2 says that there is also a directory
458 * identifier 8 bytes in to the filehandlefragement.
460 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
462 int (*acceptable)(void *context, struct dentry *de),
466 parent[0] = parent[1] = 0;
467 if (fh_len < 2 || fileid_type > 2)
469 if (fileid_type == 2) {
470 if (fh_len > 2) parent[0] = fh[2];
471 if (fh_len > 3) parent[1] = fh[3];
473 return find_exported_dentry(sb, fh, parent,
474 acceptable, context);
477 int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
480 struct export_operations *nop = dentry->d_sb->s_export_op;
484 error = nop->encode_fh(dentry, fh, max_len, connectable);
486 error = export_encode_fh(dentry, fh, max_len, connectable);
490 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
492 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
493 int fileid_type, int (*acceptable)(void *, struct dentry *),
496 struct export_operations *nop = mnt->mnt_sb->s_export_op;
497 struct dentry *result;
499 if (nop->decode_fh) {
500 result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
501 acceptable, context);
503 result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
504 acceptable, context);
509 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
511 EXPORT_SYMBOL(find_exported_dentry);
513 MODULE_LICENSE("GPL");