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 int exportfs_get_name(struct dentry
*dir
, char *name
,
19 struct export_operations
*nop
= dir
->d_sb
->s_export_op
;
22 return nop
->get_name(dir
, name
, child
);
24 return get_name(dir
, name
, child
);
28 * Check if the dentry or any of it's aliases is acceptable.
30 static struct dentry
*
31 find_acceptable_alias(struct dentry
*result
,
32 int (*acceptable
)(void *context
, struct dentry
*dentry
),
35 struct dentry
*dentry
, *toput
= NULL
;
37 if (acceptable(context
, result
))
40 spin_lock(&dcache_lock
);
41 list_for_each_entry(dentry
, &result
->d_inode
->i_dentry
, d_alias
) {
43 spin_unlock(&dcache_lock
);
46 if (dentry
!= result
&& acceptable(context
, dentry
)) {
50 spin_lock(&dcache_lock
);
53 spin_unlock(&dcache_lock
);
61 * Find root of a disconnected subtree and return a reference to it.
63 static struct dentry
*
64 find_disconnected_root(struct dentry
*dentry
)
67 spin_lock(&dentry
->d_lock
);
68 while (!IS_ROOT(dentry
) &&
69 (dentry
->d_parent
->d_flags
& DCACHE_DISCONNECTED
)) {
70 struct dentry
*parent
= dentry
->d_parent
;
72 spin_unlock(&dentry
->d_lock
);
75 spin_lock(&dentry
->d_lock
);
77 spin_unlock(&dentry
->d_lock
);
83 * Make sure target_dir is fully connected to the dentry tree.
85 * It may already be, as the flag isn't always updated when connection happens.
88 reconnect_path(struct super_block
*sb
, struct dentry
*target_dir
)
90 char nbuf
[NAME_MAX
+1];
95 * It is possible that a confused file system might not let us complete
96 * the path to the root. For example, if get_parent returns a directory
97 * in which we cannot find a name for the child. While this implies a
98 * very sick filesystem we don't want it to cause knfsd to spin. Hence
99 * the noprogress counter. If we go through the loop 10 times (2 is
100 * probably enough) without getting anywhere, we just give up
102 while (target_dir
->d_flags
& DCACHE_DISCONNECTED
&& noprogress
++ < 10) {
103 struct dentry
*pd
= find_disconnected_root(target_dir
);
106 /* must have found a connected parent - great */
107 spin_lock(&pd
->d_lock
);
108 pd
->d_flags
&= ~DCACHE_DISCONNECTED
;
109 spin_unlock(&pd
->d_lock
);
111 } else if (pd
== sb
->s_root
) {
112 printk(KERN_ERR
"export: Eeek filesystem root is not connected, impossible\n");
113 spin_lock(&pd
->d_lock
);
114 pd
->d_flags
&= ~DCACHE_DISCONNECTED
;
115 spin_unlock(&pd
->d_lock
);
119 * We have hit the top of a disconnected path, try to
120 * find parent and connect.
122 * Racing with some other process renaming a directory
123 * isn't much of a problem here. If someone renames
124 * the directory, it will end up properly connected,
125 * which is what we want
127 * Getting the parent can't be supported generically,
128 * the locking is too icky.
130 * Instead we just return EACCES. If server reboots
131 * or inodes get flushed, you lose
133 struct dentry
*ppd
= ERR_PTR(-EACCES
);
136 mutex_lock(&pd
->d_inode
->i_mutex
);
137 if (sb
->s_export_op
->get_parent
)
138 ppd
= sb
->s_export_op
->get_parent(pd
);
139 mutex_unlock(&pd
->d_inode
->i_mutex
);
143 dprintk("%s: get_parent of %ld failed, err %d\n",
144 __FUNCTION__
, pd
->d_inode
->i_ino
, err
);
149 dprintk("%s: find name of %lu in %lu\n", __FUNCTION__
,
150 pd
->d_inode
->i_ino
, ppd
->d_inode
->i_ino
);
151 err
= exportfs_get_name(ppd
, nbuf
, pd
);
156 /* some race between get_parent and
157 * get_name? just try again
162 dprintk("%s: found name: %s\n", __FUNCTION__
, nbuf
);
163 mutex_lock(&ppd
->d_inode
->i_mutex
);
164 npd
= lookup_one_len(nbuf
, ppd
, strlen(nbuf
));
165 mutex_unlock(&ppd
->d_inode
->i_mutex
);
168 dprintk("%s: lookup failed: %d\n",
174 /* we didn't really want npd, we really wanted
175 * a side-effect of the lookup.
176 * hopefully, npd == pd, though it isn't really
177 * a problem if it isn't
182 printk("%s: npd != pd\n", __FUNCTION__
);
186 /* something went wrong, we have to give up */
194 if (target_dir
->d_flags
& DCACHE_DISCONNECTED
) {
195 /* something went wrong - oh-well */
204 struct getdents_callback
{
205 char *name
; /* name that was found. It already points to a
206 buffer NAME_MAX+1 is size */
207 unsigned long ino
; /* the inum we are looking for */
208 int found
; /* inode matched? */
209 int sequence
; /* sequence counter */
213 * A rather strange filldir function to capture
214 * the name matching the specified inode number.
216 static int filldir_one(void * __buf
, const char * name
, int len
,
217 loff_t pos
, u64 ino
, unsigned int d_type
)
219 struct getdents_callback
*buf
= __buf
;
223 if (buf
->ino
== ino
) {
224 memcpy(buf
->name
, name
, len
);
225 buf
->name
[len
] = '\0';
233 * get_name - default export_operations->get_name function
234 * @dentry: the directory in which to find a name
235 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
236 * @child: the dentry for the child directory.
238 * calls readdir on the parent until it finds an entry with
239 * the same inode number as the child, and returns that.
241 static int get_name(struct dentry
*dentry
, char *name
,
242 struct dentry
*child
)
244 struct inode
*dir
= dentry
->d_inode
;
247 struct getdents_callback buffer
;
250 if (!dir
|| !S_ISDIR(dir
->i_mode
))
256 * Open the directory ...
258 file
= dentry_open(dget(dentry
), NULL
, O_RDONLY
);
259 error
= PTR_ERR(file
);
264 if (!file
->f_op
->readdir
)
268 buffer
.ino
= child
->d_inode
->i_ino
;
272 int old_seq
= buffer
.sequence
;
274 error
= vfs_readdir(file
, filldir_one
, &buffer
);
283 if (old_seq
== buffer
.sequence
)
294 * export_encode_fh - default export_operations->encode_fh function
295 * @dentry: the dentry to encode
296 * @fh: where to store the file handle fragment
297 * @max_len: maximum length to store there
298 * @connectable: whether to store parent information
300 * This default encode_fh function assumes that the 32 inode number
301 * is suitable for locating an inode, and that the generation number
302 * can be used to check that it is still valid. It places them in the
303 * filehandle fragment where export_decode_fh expects to find them.
305 static int export_encode_fh(struct dentry
*dentry
, struct fid
*fid
,
306 int *max_len
, int connectable
)
308 struct inode
* inode
= dentry
->d_inode
;
310 int type
= FILEID_INO32_GEN
;
312 if (len
< 2 || (connectable
&& len
< 4))
316 fid
->i32
.ino
= inode
->i_ino
;
317 fid
->i32
.gen
= inode
->i_generation
;
318 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
319 struct inode
*parent
;
321 spin_lock(&dentry
->d_lock
);
322 parent
= dentry
->d_parent
->d_inode
;
323 fid
->i32
.parent_ino
= parent
->i_ino
;
324 fid
->i32
.parent_gen
= parent
->i_generation
;
325 spin_unlock(&dentry
->d_lock
);
327 type
= FILEID_INO32_GEN_PARENT
;
333 int exportfs_encode_fh(struct dentry
*dentry
, struct fid
*fid
, int *max_len
,
336 struct export_operations
*nop
= dentry
->d_sb
->s_export_op
;
340 error
= nop
->encode_fh(dentry
, fid
->raw
, max_len
, connectable
);
342 error
= export_encode_fh(dentry
, fid
, max_len
, connectable
);
346 EXPORT_SYMBOL_GPL(exportfs_encode_fh
);
348 struct dentry
*exportfs_decode_fh(struct vfsmount
*mnt
, struct fid
*fid
,
349 int fh_len
, int fileid_type
,
350 int (*acceptable
)(void *, struct dentry
*), void *context
)
352 struct export_operations
*nop
= mnt
->mnt_sb
->s_export_op
;
353 struct dentry
*result
, *alias
;
357 * Try to get any dentry for the given file handle from the filesystem.
359 result
= nop
->fh_to_dentry(mnt
->mnt_sb
, fid
, fh_len
, fileid_type
);
361 result
= ERR_PTR(-ESTALE
);
365 if (S_ISDIR(result
->d_inode
->i_mode
)) {
367 * This request is for a directory.
369 * On the positive side there is only one dentry for each
370 * directory inode. On the negative side this implies that we
371 * to ensure our dentry is connected all the way up to the
374 if (result
->d_flags
& DCACHE_DISCONNECTED
) {
375 err
= reconnect_path(mnt
->mnt_sb
, result
);
380 if (!acceptable(context
, result
)) {
388 * It's not a directory. Life is a little more complicated.
390 struct dentry
*target_dir
, *nresult
;
391 char nbuf
[NAME_MAX
+1];
394 * See if either the dentry we just got from the filesystem
395 * or any alias for it is acceptable. This is always true
396 * if this filesystem is exported without the subtreecheck
397 * option. If the filesystem is exported with the subtree
398 * check option there's a fair chance we need to look at
399 * the parent directory in the file handle and make sure
400 * it's connected to the filesystem root.
402 alias
= find_acceptable_alias(result
, acceptable
, context
);
407 * Try to extract a dentry for the parent directory from the
408 * file handle. If this fails we'll have to give up.
411 if (!nop
->fh_to_parent
)
414 target_dir
= nop
->fh_to_parent(mnt
->mnt_sb
, fid
,
415 fh_len
, fileid_type
);
418 err
= PTR_ERR(target_dir
);
419 if (IS_ERR(target_dir
))
423 * And as usual we need to make sure the parent directory is
424 * connected to the filesystem root. The VFS really doesn't
425 * like disconnected directories..
427 err
= reconnect_path(mnt
->mnt_sb
, target_dir
);
434 * Now that we've got both a well-connected parent and a
435 * dentry for the inode we're after, make sure that our
436 * inode is actually connected to the parent.
438 err
= exportfs_get_name(target_dir
, nbuf
, result
);
440 mutex_lock(&target_dir
->d_inode
->i_mutex
);
441 nresult
= lookup_one_len(nbuf
, target_dir
,
443 mutex_unlock(&target_dir
->d_inode
->i_mutex
);
444 if (!IS_ERR(nresult
)) {
445 if (nresult
->d_inode
) {
454 * At this point we are done with the parent, but it's pinned
455 * by the child dentry anyway.
460 * And finally make sure the dentry is actually acceptable
463 alias
= find_acceptable_alias(result
, acceptable
, context
);
476 EXPORT_SYMBOL_GPL(exportfs_decode_fh
);
478 MODULE_LICENSE("GPL");