3 #include <linux/module.h>
4 #include <linux/smp_lock.h>
5 #include <linux/namei.h>
7 struct export_operations export_op_default;
9 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
11 #define dprintk(fmt, args...) do{}while(0)
14 * find_exported_dentry - helper routine to implement export_operations->decode_fh
15 * @sb: The &super_block identifying the filesystem
16 * @obj: An opaque identifier of the object to be found - passed to
18 * @parent: An optional opqaue identifier of the parent of the object.
19 * @acceptable: A function used to test possible &dentries to see if they are
21 * @context: A parameter to @acceptable so that it knows on what basis to
24 * find_exported_dentry is the central helper routine to enable file systems
25 * to provide the decode_fh() export_operation. It's main task is to take
26 * an &inode, find or create an appropriate &dentry structure, and possibly
27 * splice this into the dcache in the correct place.
29 * The decode_fh() operation provided by the filesystem should call
30 * find_exported_dentry() with the same parameters that it received except
31 * that instead of the file handle fragment, pointers to opaque identifiers
32 * for the object and optionally its parent are passed. The default decode_fh
33 * routine passes one pointer to the start of the filehandle fragment, and
34 * one 8 bytes into the fragment. It is expected that most filesystems will
35 * take this approach, though the offset to the parent identifier may well be
38 * find_exported_dentry() will call get_dentry to get an dentry pointer from
39 * the file system. If any &dentry in the d_alias list is acceptable, it will
40 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
41 * &dentry into the dcache using get_name() and get_parent() to find the
46 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
47 int (*acceptable)(void *context, struct dentry *de),
50 struct dentry *result = NULL;
51 struct dentry *target_dir;
53 struct export_operations *nops = sb->s_export_op;
54 struct list_head *le, *head;
55 struct dentry *toput = NULL;
60 * Attempt to find the inode.
62 result = CALL(sb->s_export_op,get_dentry)(sb,obj);
67 err = PTR_ERR(result);
70 if (S_ISDIR(result->d_inode->i_mode) &&
71 (result->d_flags & DCACHE_DISCONNECTED)) {
72 /* it is an unconnected directory, we must connect it */
75 if (acceptable(context, result))
77 if (S_ISDIR(result->d_inode->i_mode)) {
78 /* there is no other dentry, so fail */
81 /* try any other aliases */
82 spin_lock(&dcache_lock);
83 head = &result->d_inode->i_dentry;
84 list_for_each(le, head) {
85 struct dentry *dentry = list_entry(le, struct dentry, d_alias);
87 spin_unlock(&dcache_lock);
91 if (dentry != result &&
92 acceptable(context, dentry)) {
96 spin_lock(&dcache_lock);
99 spin_unlock(&dcache_lock);
104 /* It's a directory, or we are required to confirm the file's
105 * location in the tree based on the parent information
107 dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
108 if (S_ISDIR(result->d_inode->i_mode))
109 target_dir = dget(result);
114 target_dir = CALL(sb->s_export_op,get_dentry)(sb,parent);
115 if (IS_ERR(target_dir))
116 err = PTR_ERR(target_dir);
117 if (target_dir == NULL || IS_ERR(target_dir))
121 * Now we need to make sure that target_dir is properly connected.
122 * It may already be, as the flag isn't always updated when connection
124 * So, we walk up parent links until we find a connected directory,
125 * or we run out of directories. Then we find the parent, find
126 * the name of the child in that parent, and do a lookup.
127 * This should connect the child into the parent
131 /* it is possible that a confused file system might not let us complete
132 * the path to the root. For example, if get_parent returns a directory
133 * in which we cannot find a name for the child. While this implies a
134 * very sick filesystem we don't want it to cause knfsd to spin. Hence
135 * the noprogress counter. If we go through the loop 10 times (2 is
136 * probably enough) without getting anywhere, we just give up
139 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
140 struct dentry *pd = target_dir;
143 spin_lock(&pd->d_lock);
144 while (!IS_ROOT(pd) &&
145 (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
146 struct dentry *parent = pd->d_parent;
149 spin_unlock(&pd->d_lock);
152 spin_lock(&pd->d_lock);
154 spin_unlock(&pd->d_lock);
157 /* must have found a connected parent - great */
158 pd->d_flags &= ~DCACHE_DISCONNECTED;
160 } else if (pd == sb->s_root) {
161 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
162 pd->d_flags &= ~DCACHE_DISCONNECTED;
165 /* we have hit the top of a disconnected path. Try
166 * to find parent and connect
167 * note: racing with some other process renaming a
168 * directory isn't much of a problem here. If someone
169 * renames the directory, it will end up properly
170 * connected, which is what we want
174 char nbuf[NAME_MAX+1];
176 down(&pd->d_inode->i_sem);
177 ppd = CALL(nops,get_parent)(pd);
178 up(&pd->d_inode->i_sem);
182 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
183 pd->d_inode->i_ino, err);
187 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
188 err = CALL(nops,get_name)(ppd, nbuf, pd);
193 /* some race between get_parent and
194 * get_name? just try again
199 dprintk("find_exported_dentry: found name: %s\n", nbuf);
200 down(&ppd->d_inode->i_sem);
201 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
202 up(&ppd->d_inode->i_sem);
205 dprintk("find_exported_dentry: lookup failed: %d\n", err);
210 /* we didn't really want npd, we really wanted
211 * a side-effect of the lookup.
212 * hopefully, npd == pd, though it isn't really
213 * a problem if it isn't
218 printk("find_exported_dentry: npd != pd\n");
222 /* something went wrong, we have to give up */
230 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
231 /* something went wrong - oh-well */
236 /* if we weren't after a directory, have one more step to go */
237 if (result != target_dir) {
238 struct dentry *nresult;
239 char nbuf[NAME_MAX+1];
240 err = CALL(nops,get_name)(target_dir, nbuf, result);
242 down(&target_dir->d_inode->i_sem);
243 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
244 up(&target_dir->d_inode->i_sem);
245 if (!IS_ERR(nresult)) {
246 if (nresult->d_inode) {
255 /* now result is properly connected, it is our best bet */
256 if (acceptable(context, result))
258 /* one last try of the aliases.. */
259 spin_lock(&dcache_lock);
261 head = &result->d_inode->i_dentry;
262 list_for_each(le, head) {
263 struct dentry *dentry = list_entry(le, struct dentry, d_alias);
265 spin_unlock(&dcache_lock);
266 if (toput) dput(toput);
267 if (dentry != result &&
268 acceptable(context, dentry)) {
272 spin_lock(&dcache_lock);
275 spin_unlock(&dcache_lock);
279 /* drat - I just cannot find anything acceptable */
281 return ERR_PTR(-ESTALE);
293 static struct dentry *get_parent(struct dentry *child)
295 /* get_parent cannot be supported generically, the locking
297 * instead, we just return EACCES. If server reboots or inodes
298 * get flushed, you lose
300 return ERR_PTR(-EACCES);
304 struct getdents_callback {
305 char *name; /* name that was found. It already points to a
306 buffer NAME_MAX+1 is size */
307 unsigned long ino; /* the inum we are looking for */
308 int found; /* inode matched? */
309 int sequence; /* sequence counter */
313 * A rather strange filldir function to capture
314 * the name matching the specified inode number.
316 static int filldir_one(void * __buf, const char * name, int len,
317 loff_t pos, ino_t ino, unsigned int d_type)
319 struct getdents_callback *buf = __buf;
323 if (buf->ino == ino) {
324 memcpy(buf->name, name, len);
325 buf->name[len] = '\0';
333 * get_name - default export_operations->get_name function
334 * @dentry: the directory in which to find a name
335 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
336 * @child: the dentry for the child directory.
338 * calls readdir on the parent until it finds an entry with
339 * the same inode number as the child, and returns that.
341 static int get_name(struct dentry *dentry, char *name,
342 struct dentry *child)
344 struct inode *dir = dentry->d_inode;
347 struct getdents_callback buffer;
350 if (!dir || !S_ISDIR(dir->i_mode))
356 * Open the directory ...
358 error = open_private_file(&file, dentry, O_RDONLY);
362 if (!file.f_op->readdir)
366 buffer.ino = child->d_inode->i_ino;
370 int old_seq = buffer.sequence;
372 error = vfs_readdir(&file, filldir_one, &buffer);
381 if (old_seq == buffer.sequence)
386 close_private_file(&file);
392 static struct dentry *export_iget(struct super_block *sb, unsigned long ino, __u32 generation)
395 /* iget isn't really right if the inode is currently unallocated!!
396 * This should really all be done inside each filesystem
398 * ext2fs' read_inode has been strengthed to return a bad_inode if
399 * the inode had been deleted.
401 * Currently we don't know the generation for parent directory, so
402 * a generation of 0 means "accept any"
405 struct dentry *result;
407 return ERR_PTR(-ESTALE);
408 inode = iget(sb, ino);
410 return ERR_PTR(-ENOMEM);
411 if (is_bad_inode(inode)
412 || (generation && inode->i_generation != generation)
414 /* we didn't find the right inode.. */
415 dprintk("fh_verify: Inode %lu, Bad count: %d %d or version %u %u\n",
417 inode->i_nlink, atomic_read(&inode->i_count),
422 return ERR_PTR(-ESTALE);
424 /* now to find a dentry.
425 * If possible, get a well-connected one
427 result = d_alloc_anon(inode);
430 return ERR_PTR(-ENOMEM);
436 static struct dentry *get_object(struct super_block *sb, void *vobjp)
439 unsigned long ino = objp[0];
440 __u32 generation = objp[1];
442 return export_iget(sb, ino, generation);
447 * export_encode_fh - default export_operations->encode_fh function
448 * @dentry: the dentry to encode
449 * @fh: where to store the file handle fragment
450 * @max_len: maximum length to store there
451 * @connectable: whether to store parent information
453 * This default encode_fh function assumes that the 32 inode number
454 * is suitable for locating an inode, and that the generation number
455 * can be used to check that it is still valid. It places them in the
456 * filehandle fragment where export_decode_fh expects to find them.
458 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
461 struct inode * inode = dentry->d_inode;
465 if (len < 2 || (connectable && len < 4))
469 fh[0] = inode->i_ino;
470 fh[1] = inode->i_generation;
471 if (connectable && !S_ISDIR(inode->i_mode)) {
472 struct inode *parent;
474 spin_lock(&dentry->d_lock);
475 parent = dentry->d_parent->d_inode;
476 fh[2] = parent->i_ino;
477 fh[3] = parent->i_generation;
478 spin_unlock(&dentry->d_lock);
488 * export_decode_fh - default export_operations->decode_fh function
489 * @sb: The superblock
490 * @fh: pointer to the file handle fragment
491 * @fh_len: length of file handle fragment
492 * @acceptable: function for testing acceptability of dentrys
493 * @context: context for @acceptable
495 * This is the default decode_fh() function.
496 * a fileid_type of 1 indicates that the filehandlefragment
497 * just contains an object identifier understood by get_dentry.
498 * a fileid_type of 2 says that there is also a directory
499 * identifier 8 bytes in to the filehandlefragement.
501 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
503 int (*acceptable)(void *context, struct dentry *de),
507 parent[0] = parent[1] = 0;
508 if (fh_len < 2 || fileid_type > 2)
510 if (fileid_type == 2) {
511 if (fh_len > 2) parent[0] = fh[2];
512 if (fh_len > 3) parent[1] = fh[3];
514 return find_exported_dentry(sb, fh, parent,
515 acceptable, context);
518 struct export_operations export_op_default = {
519 .decode_fh = export_decode_fh,
520 .encode_fh = export_encode_fh,
522 .get_name = get_name,
523 .get_parent = get_parent,
524 .get_dentry = get_object,
527 EXPORT_SYMBOL(export_op_default);
528 EXPORT_SYMBOL(find_exported_dentry);
530 MODULE_LICENSE("GPL");