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
);
96 * Make sure target_dir is fully connected to the dentry tree.
98 * It may already be, as the flag isn't always updated when connection happens.
101 reconnect_path(struct super_block
*sb
, struct dentry
*target_dir
)
103 char nbuf
[NAME_MAX
+1];
108 * It is possible that a confused file system might not let us complete
109 * the path to the root. For example, if get_parent returns a directory
110 * in which we cannot find a name for the child. While this implies a
111 * very sick filesystem we don't want it to cause knfsd to spin. Hence
112 * the noprogress counter. If we go through the loop 10 times (2 is
113 * probably enough) without getting anywhere, we just give up
115 while (target_dir
->d_flags
& DCACHE_DISCONNECTED
&& noprogress
++ < 10) {
116 struct dentry
*pd
= find_disconnected_root(target_dir
);
119 /* must have found a connected parent - great */
120 spin_lock(&pd
->d_lock
);
121 pd
->d_flags
&= ~DCACHE_DISCONNECTED
;
122 spin_unlock(&pd
->d_lock
);
124 } else if (pd
== sb
->s_root
) {
125 printk(KERN_ERR
"export: Eeek filesystem root is not connected, impossible\n");
126 spin_lock(&pd
->d_lock
);
127 pd
->d_flags
&= ~DCACHE_DISCONNECTED
;
128 spin_unlock(&pd
->d_lock
);
132 * We have hit the top of a disconnected path, try to
133 * find parent and connect.
135 * Racing with some other process renaming a directory
136 * isn't much of a problem here. If someone renames
137 * the directory, it will end up properly connected,
138 * which is what we want
140 * Getting the parent can't be supported generically,
141 * the locking is too icky.
143 * Instead we just return EACCES. If server reboots
144 * or inodes get flushed, you lose
146 struct dentry
*ppd
= ERR_PTR(-EACCES
);
149 mutex_lock(&pd
->d_inode
->i_mutex
);
150 if (sb
->s_export_op
->get_parent
)
151 ppd
= sb
->s_export_op
->get_parent(pd
);
152 mutex_unlock(&pd
->d_inode
->i_mutex
);
156 dprintk("%s: get_parent of %ld failed, err %d\n",
157 __FUNCTION__
, pd
->d_inode
->i_ino
, err
);
162 dprintk("%s: find name of %lu in %lu\n", __FUNCTION__
,
163 pd
->d_inode
->i_ino
, ppd
->d_inode
->i_ino
);
164 err
= exportfs_get_name(ppd
, nbuf
, pd
);
169 /* some race between get_parent and
170 * get_name? just try again
175 dprintk("%s: found name: %s\n", __FUNCTION__
, nbuf
);
176 mutex_lock(&ppd
->d_inode
->i_mutex
);
177 npd
= lookup_one_len(nbuf
, ppd
, strlen(nbuf
));
178 mutex_unlock(&ppd
->d_inode
->i_mutex
);
181 dprintk("%s: lookup failed: %d\n",
187 /* we didn't really want npd, we really wanted
188 * a side-effect of the lookup.
189 * hopefully, npd == pd, though it isn't really
190 * a problem if it isn't
195 printk("%s: npd != pd\n", __FUNCTION__
);
199 /* something went wrong, we have to give up */
207 if (target_dir
->d_flags
& DCACHE_DISCONNECTED
) {
208 /* something went wrong - oh-well */
218 * find_exported_dentry - helper routine to implement export_operations->decode_fh
219 * @sb: The &super_block identifying the filesystem
220 * @obj: An opaque identifier of the object to be found - passed to
222 * @parent: An optional opqaue identifier of the parent of the object.
223 * @acceptable: A function used to test possible &dentries to see if they are
225 * @context: A parameter to @acceptable so that it knows on what basis to
228 * find_exported_dentry is the central helper routine to enable file systems
229 * to provide the decode_fh() export_operation. It's main task is to take
230 * an &inode, find or create an appropriate &dentry structure, and possibly
231 * splice this into the dcache in the correct place.
233 * The decode_fh() operation provided by the filesystem should call
234 * find_exported_dentry() with the same parameters that it received except
235 * that instead of the file handle fragment, pointers to opaque identifiers
236 * for the object and optionally its parent are passed. The default decode_fh
237 * routine passes one pointer to the start of the filehandle fragment, and
238 * one 8 bytes into the fragment. It is expected that most filesystems will
239 * take this approach, though the offset to the parent identifier may well be
242 * find_exported_dentry() will call get_dentry to get an dentry pointer from
243 * the file system. If any &dentry in the d_alias list is acceptable, it will
244 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
245 * &dentry into the dcache using get_name() and get_parent() to find the
250 find_exported_dentry(struct super_block
*sb
, void *obj
, void *parent
,
251 int (*acceptable
)(void *context
, struct dentry
*de
),
254 struct dentry
*result
, *alias
;
258 * Attempt to find the inode.
260 result
= exportfs_get_dentry(sb
, obj
);
264 if (S_ISDIR(result
->d_inode
->i_mode
)) {
265 if (!(result
->d_flags
& DCACHE_DISCONNECTED
)) {
266 if (acceptable(context
, result
))
272 err
= reconnect_path(sb
, result
);
276 struct dentry
*target_dir
, *nresult
;
277 char nbuf
[NAME_MAX
+1];
279 alias
= find_acceptable_alias(result
, acceptable
, context
);
286 target_dir
= exportfs_get_dentry(sb
,parent
);
287 if (IS_ERR(target_dir
)) {
288 err
= PTR_ERR(target_dir
);
292 err
= reconnect_path(sb
, target_dir
);
299 * As we weren't after a directory, have one more step to go.
301 err
= exportfs_get_name(target_dir
, nbuf
, result
);
303 mutex_lock(&target_dir
->d_inode
->i_mutex
);
304 nresult
= lookup_one_len(nbuf
, target_dir
,
306 mutex_unlock(&target_dir
->d_inode
->i_mutex
);
307 if (!IS_ERR(nresult
)) {
308 if (nresult
->d_inode
) {
318 alias
= find_acceptable_alias(result
, acceptable
, context
);
322 /* drat - I just cannot find anything acceptable */
324 /* It might be justifiable to return ESTALE here,
325 * but the filehandle at-least looks reasonable good
326 * and it may just be a permission problem, so returning
329 return ERR_PTR(-EACCES
);
336 struct getdents_callback
{
337 char *name
; /* name that was found. It already points to a
338 buffer NAME_MAX+1 is size */
339 unsigned long ino
; /* the inum we are looking for */
340 int found
; /* inode matched? */
341 int sequence
; /* sequence counter */
345 * A rather strange filldir function to capture
346 * the name matching the specified inode number.
348 static int filldir_one(void * __buf
, const char * name
, int len
,
349 loff_t pos
, u64 ino
, unsigned int d_type
)
351 struct getdents_callback
*buf
= __buf
;
355 if (buf
->ino
== ino
) {
356 memcpy(buf
->name
, name
, len
);
357 buf
->name
[len
] = '\0';
365 * get_name - default export_operations->get_name function
366 * @dentry: the directory in which to find a name
367 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
368 * @child: the dentry for the child directory.
370 * calls readdir on the parent until it finds an entry with
371 * the same inode number as the child, and returns that.
373 static int get_name(struct dentry
*dentry
, char *name
,
374 struct dentry
*child
)
376 struct inode
*dir
= dentry
->d_inode
;
379 struct getdents_callback buffer
;
382 if (!dir
|| !S_ISDIR(dir
->i_mode
))
388 * Open the directory ...
390 file
= dentry_open(dget(dentry
), NULL
, O_RDONLY
);
391 error
= PTR_ERR(file
);
396 if (!file
->f_op
->readdir
)
400 buffer
.ino
= child
->d_inode
->i_ino
;
404 int old_seq
= buffer
.sequence
;
406 error
= vfs_readdir(file
, filldir_one
, &buffer
);
415 if (old_seq
== buffer
.sequence
)
426 * export_encode_fh - default export_operations->encode_fh function
427 * @dentry: the dentry to encode
428 * @fh: where to store the file handle fragment
429 * @max_len: maximum length to store there
430 * @connectable: whether to store parent information
432 * This default encode_fh function assumes that the 32 inode number
433 * is suitable for locating an inode, and that the generation number
434 * can be used to check that it is still valid. It places them in the
435 * filehandle fragment where export_decode_fh expects to find them.
437 static int export_encode_fh(struct dentry
*dentry
, __u32
*fh
, int *max_len
,
440 struct inode
* inode
= dentry
->d_inode
;
444 if (len
< 2 || (connectable
&& len
< 4))
448 fh
[0] = inode
->i_ino
;
449 fh
[1] = inode
->i_generation
;
450 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
451 struct inode
*parent
;
453 spin_lock(&dentry
->d_lock
);
454 parent
= dentry
->d_parent
->d_inode
;
455 fh
[2] = parent
->i_ino
;
456 fh
[3] = parent
->i_generation
;
457 spin_unlock(&dentry
->d_lock
);
467 * export_decode_fh - default export_operations->decode_fh function
468 * @sb: The superblock
469 * @fh: pointer to the file handle fragment
470 * @fh_len: length of file handle fragment
471 * @acceptable: function for testing acceptability of dentrys
472 * @context: context for @acceptable
474 * This is the default decode_fh() function.
475 * a fileid_type of 1 indicates that the filehandlefragment
476 * just contains an object identifier understood by get_dentry.
477 * a fileid_type of 2 says that there is also a directory
478 * identifier 8 bytes in to the filehandlefragement.
480 static struct dentry
*export_decode_fh(struct super_block
*sb
, __u32
*fh
, int fh_len
,
482 int (*acceptable
)(void *context
, struct dentry
*de
),
486 parent
[0] = parent
[1] = 0;
487 if (fh_len
< 2 || fileid_type
> 2)
489 if (fileid_type
== 2) {
490 if (fh_len
> 2) parent
[0] = fh
[2];
491 if (fh_len
> 3) parent
[1] = fh
[3];
493 return find_exported_dentry(sb
, fh
, parent
,
494 acceptable
, context
);
497 int exportfs_encode_fh(struct dentry
*dentry
, __u32
*fh
, int *max_len
,
500 struct export_operations
*nop
= dentry
->d_sb
->s_export_op
;
504 error
= nop
->encode_fh(dentry
, fh
, max_len
, connectable
);
506 error
= export_encode_fh(dentry
, fh
, max_len
, connectable
);
510 EXPORT_SYMBOL_GPL(exportfs_encode_fh
);
512 struct dentry
*exportfs_decode_fh(struct vfsmount
*mnt
, __u32
*fh
, int fh_len
,
513 int fileid_type
, int (*acceptable
)(void *, struct dentry
*),
516 struct export_operations
*nop
= mnt
->mnt_sb
->s_export_op
;
517 struct dentry
*result
;
519 if (nop
->decode_fh
) {
520 result
= nop
->decode_fh(mnt
->mnt_sb
, fh
, fh_len
, fileid_type
,
521 acceptable
, context
);
523 result
= export_decode_fh(mnt
->mnt_sb
, fh
, fh_len
, fileid_type
,
524 acceptable
, context
);
529 EXPORT_SYMBOL_GPL(exportfs_decode_fh
);
531 EXPORT_SYMBOL(find_exported_dentry
);
533 MODULE_LICENSE("GPL");