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 struct export_operations export_op_default;
11 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
13 #define dprintk(fmt, args...) do{}while(0)
15 static struct dentry *
16 find_acceptable_alias(struct dentry *result,
17 int (*acceptable)(void *context, struct dentry *dentry),
20 struct dentry *dentry, *toput = NULL;
22 spin_lock(&dcache_lock);
23 list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
25 spin_unlock(&dcache_lock);
28 if (dentry != result && acceptable(context, dentry)) {
32 spin_lock(&dcache_lock);
35 spin_unlock(&dcache_lock);
43 * find_exported_dentry - helper routine to implement export_operations->decode_fh
44 * @sb: The &super_block identifying the filesystem
45 * @obj: An opaque identifier of the object to be found - passed to
47 * @parent: An optional opqaue identifier of the parent of the object.
48 * @acceptable: A function used to test possible &dentries to see if they are
50 * @context: A parameter to @acceptable so that it knows on what basis to
53 * find_exported_dentry is the central helper routine to enable file systems
54 * to provide the decode_fh() export_operation. It's main task is to take
55 * an &inode, find or create an appropriate &dentry structure, and possibly
56 * splice this into the dcache in the correct place.
58 * The decode_fh() operation provided by the filesystem should call
59 * find_exported_dentry() with the same parameters that it received except
60 * that instead of the file handle fragment, pointers to opaque identifiers
61 * for the object and optionally its parent are passed. The default decode_fh
62 * routine passes one pointer to the start of the filehandle fragment, and
63 * one 8 bytes into the fragment. It is expected that most filesystems will
64 * take this approach, though the offset to the parent identifier may well be
67 * find_exported_dentry() will call get_dentry to get an dentry pointer from
68 * the file system. If any &dentry in the d_alias list is acceptable, it will
69 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
70 * &dentry into the dcache using get_name() and get_parent() to find the
75 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
76 int (*acceptable)(void *context, struct dentry *de),
79 struct dentry *result = NULL;
80 struct dentry *target_dir;
82 struct export_operations *nops = sb->s_export_op;
85 char nbuf[NAME_MAX+1];
88 * Attempt to find the inode.
90 result = CALL(sb->s_export_op,get_dentry)(sb,obj);
95 err = PTR_ERR(result);
98 if (S_ISDIR(result->d_inode->i_mode) &&
99 (result->d_flags & DCACHE_DISCONNECTED)) {
100 /* it is an unconnected directory, we must connect it */
103 if (acceptable(context, result))
105 if (S_ISDIR(result->d_inode->i_mode)) {
110 alias = find_acceptable_alias(result, acceptable, context);
115 /* It's a directory, or we are required to confirm the file's
116 * location in the tree based on the parent information
118 dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
119 if (S_ISDIR(result->d_inode->i_mode))
120 target_dir = dget(result);
125 target_dir = CALL(sb->s_export_op,get_dentry)(sb,parent);
126 if (IS_ERR(target_dir))
127 err = PTR_ERR(target_dir);
128 if (target_dir == NULL || IS_ERR(target_dir))
132 * Now we need to make sure that target_dir is properly connected.
133 * It may already be, as the flag isn't always updated when connection
135 * So, we walk up parent links until we find a connected directory,
136 * or we run out of directories. Then we find the parent, find
137 * the name of the child in that parent, and do a lookup.
138 * This should connect the child into the parent
142 /* it is possible that a confused file system might not let us complete
143 * the path to the root. For example, if get_parent returns a directory
144 * in which we cannot find a name for the child. While this implies a
145 * very sick filesystem we don't want it to cause knfsd to spin. Hence
146 * the noprogress counter. If we go through the loop 10 times (2 is
147 * probably enough) without getting anywhere, we just give up
150 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
151 struct dentry *pd = target_dir;
154 spin_lock(&pd->d_lock);
155 while (!IS_ROOT(pd) &&
156 (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
157 struct dentry *parent = pd->d_parent;
160 spin_unlock(&pd->d_lock);
163 spin_lock(&pd->d_lock);
165 spin_unlock(&pd->d_lock);
168 /* must have found a connected parent - great */
169 spin_lock(&pd->d_lock);
170 pd->d_flags &= ~DCACHE_DISCONNECTED;
171 spin_unlock(&pd->d_lock);
173 } else if (pd == sb->s_root) {
174 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
175 spin_lock(&pd->d_lock);
176 pd->d_flags &= ~DCACHE_DISCONNECTED;
177 spin_unlock(&pd->d_lock);
180 /* we have hit the top of a disconnected path. Try
181 * to find parent and connect
182 * note: racing with some other process renaming a
183 * directory isn't much of a problem here. If someone
184 * renames the directory, it will end up properly
185 * connected, which is what we want
190 mutex_lock(&pd->d_inode->i_mutex);
191 ppd = CALL(nops,get_parent)(pd);
192 mutex_unlock(&pd->d_inode->i_mutex);
196 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
197 pd->d_inode->i_ino, err);
201 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
202 err = CALL(nops,get_name)(ppd, nbuf, pd);
207 /* some race between get_parent and
208 * get_name? just try again
213 dprintk("find_exported_dentry: found name: %s\n", nbuf);
214 mutex_lock(&ppd->d_inode->i_mutex);
215 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
216 mutex_unlock(&ppd->d_inode->i_mutex);
219 dprintk("find_exported_dentry: lookup failed: %d\n", err);
224 /* we didn't really want npd, we really wanted
225 * a side-effect of the lookup.
226 * hopefully, npd == pd, though it isn't really
227 * a problem if it isn't
232 printk("find_exported_dentry: npd != pd\n");
236 /* something went wrong, we have to give up */
244 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
245 /* something went wrong - oh-well */
250 /* if we weren't after a directory, have one more step to go */
251 if (result != target_dir) {
252 struct dentry *nresult;
253 err = CALL(nops,get_name)(target_dir, nbuf, result);
255 mutex_lock(&target_dir->d_inode->i_mutex);
256 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
257 mutex_unlock(&target_dir->d_inode->i_mutex);
258 if (!IS_ERR(nresult)) {
259 if (nresult->d_inode) {
268 /* now result is properly connected, it is our best bet */
269 if (acceptable(context, result))
272 alias = find_acceptable_alias(result, acceptable, context);
276 /* drat - I just cannot find anything acceptable */
278 /* It might be justifiable to return ESTALE here,
279 * but the filehandle at-least looks reasonable good
280 * and it just be a permission problem, so returning
283 return ERR_PTR(-EACCES);
295 static struct dentry *get_parent(struct dentry *child)
297 /* get_parent cannot be supported generically, the locking
299 * instead, we just return EACCES. If server reboots or inodes
300 * get flushed, you lose
302 return ERR_PTR(-EACCES);
306 struct getdents_callback {
307 char *name; /* name that was found. It already points to a
308 buffer NAME_MAX+1 is size */
309 unsigned long ino; /* the inum we are looking for */
310 int found; /* inode matched? */
311 int sequence; /* sequence counter */
315 * A rather strange filldir function to capture
316 * the name matching the specified inode number.
318 static int filldir_one(void * __buf, const char * name, int len,
319 loff_t pos, u64 ino, unsigned int d_type)
321 struct getdents_callback *buf = __buf;
325 if (buf->ino == ino) {
326 memcpy(buf->name, name, len);
327 buf->name[len] = '\0';
335 * get_name - default export_operations->get_name function
336 * @dentry: the directory in which to find a name
337 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
338 * @child: the dentry for the child directory.
340 * calls readdir on the parent until it finds an entry with
341 * the same inode number as the child, and returns that.
343 static int get_name(struct dentry *dentry, char *name,
344 struct dentry *child)
346 struct inode *dir = dentry->d_inode;
349 struct getdents_callback buffer;
352 if (!dir || !S_ISDIR(dir->i_mode))
358 * Open the directory ...
360 file = dentry_open(dget(dentry), NULL, O_RDONLY);
361 error = PTR_ERR(file);
366 if (!file->f_op->readdir)
370 buffer.ino = child->d_inode->i_ino;
374 int old_seq = buffer.sequence;
376 error = vfs_readdir(file, filldir_one, &buffer);
385 if (old_seq == buffer.sequence)
395 static struct dentry *get_dentry(struct super_block *sb, void *vobjp)
397 return ERR_PTR(-ESTALE);
401 * export_encode_fh - default export_operations->encode_fh function
402 * @dentry: the dentry to encode
403 * @fh: where to store the file handle fragment
404 * @max_len: maximum length to store there
405 * @connectable: whether to store parent information
407 * This default encode_fh function assumes that the 32 inode number
408 * is suitable for locating an inode, and that the generation number
409 * can be used to check that it is still valid. It places them in the
410 * filehandle fragment where export_decode_fh expects to find them.
412 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
415 struct inode * inode = dentry->d_inode;
419 if (len < 2 || (connectable && len < 4))
423 fh[0] = inode->i_ino;
424 fh[1] = inode->i_generation;
425 if (connectable && !S_ISDIR(inode->i_mode)) {
426 struct inode *parent;
428 spin_lock(&dentry->d_lock);
429 parent = dentry->d_parent->d_inode;
430 fh[2] = parent->i_ino;
431 fh[3] = parent->i_generation;
432 spin_unlock(&dentry->d_lock);
442 * export_decode_fh - default export_operations->decode_fh function
443 * @sb: The superblock
444 * @fh: pointer to the file handle fragment
445 * @fh_len: length of file handle fragment
446 * @acceptable: function for testing acceptability of dentrys
447 * @context: context for @acceptable
449 * This is the default decode_fh() function.
450 * a fileid_type of 1 indicates that the filehandlefragment
451 * just contains an object identifier understood by get_dentry.
452 * a fileid_type of 2 says that there is also a directory
453 * identifier 8 bytes in to the filehandlefragement.
455 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
457 int (*acceptable)(void *context, struct dentry *de),
461 parent[0] = parent[1] = 0;
462 if (fh_len < 2 || fileid_type > 2)
464 if (fileid_type == 2) {
465 if (fh_len > 2) parent[0] = fh[2];
466 if (fh_len > 3) parent[1] = fh[3];
468 return find_exported_dentry(sb, fh, parent,
469 acceptable, context);
472 int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
475 struct export_operations *nop = dentry->d_sb->s_export_op;
477 return CALL(nop, encode_fh)(dentry, fh, max_len, connectable);
479 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
481 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
482 int fileid_type, int (*acceptable)(void *, struct dentry *),
485 struct export_operations *nop = mnt->mnt_sb->s_export_op;
487 return CALL(nop, decode_fh)(mnt->mnt_sb, fh, fh_len, fileid_type,
488 acceptable, context);
490 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
492 struct export_operations export_op_default = {
493 .decode_fh = export_decode_fh,
494 .encode_fh = export_encode_fh,
496 .get_name = get_name,
497 .get_parent = get_parent,
498 .get_dentry = get_dentry,
501 EXPORT_SYMBOL(find_exported_dentry);
503 MODULE_LICENSE("GPL");