3 #include <linux/file.h>
4 #include <linux/module.h>
5 #include <linux/smp_lock.h>
6 #include <linux/namei.h>
8 struct export_operations export_op_default
;
10 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
12 #define dprintk(fmt, args...) do{}while(0)
15 * find_exported_dentry - helper routine to implement export_operations->decode_fh
16 * @sb: The &super_block identifying the filesystem
17 * @obj: An opaque identifier of the object to be found - passed to
19 * @parent: An optional opqaue identifier of the parent of the object.
20 * @acceptable: A function used to test possible &dentries to see if they are
22 * @context: A parameter to @acceptable so that it knows on what basis to
25 * find_exported_dentry is the central helper routine to enable file systems
26 * to provide the decode_fh() export_operation. It's main task is to take
27 * an &inode, find or create an appropriate &dentry structure, and possibly
28 * splice this into the dcache in the correct place.
30 * The decode_fh() operation provided by the filesystem should call
31 * find_exported_dentry() with the same parameters that it received except
32 * that instead of the file handle fragment, pointers to opaque identifiers
33 * for the object and optionally its parent are passed. The default decode_fh
34 * routine passes one pointer to the start of the filehandle fragment, and
35 * one 8 bytes into the fragment. It is expected that most filesystems will
36 * take this approach, though the offset to the parent identifier may well be
39 * find_exported_dentry() will call get_dentry to get an dentry pointer from
40 * the file system. If any &dentry in the d_alias list is acceptable, it will
41 * be returned. Otherwise find_exported_dentry() will attempt to splice a new
42 * &dentry into the dcache using get_name() and get_parent() to find the
47 find_exported_dentry(struct super_block
*sb
, void *obj
, void *parent
,
48 int (*acceptable
)(void *context
, struct dentry
*de
),
51 struct dentry
*result
= NULL
;
52 struct dentry
*target_dir
;
54 struct export_operations
*nops
= sb
->s_export_op
;
55 struct list_head
*le
, *head
;
56 struct dentry
*toput
= NULL
;
61 * Attempt to find the inode.
63 result
= CALL(sb
->s_export_op
,get_dentry
)(sb
,obj
);
68 err
= PTR_ERR(result
);
71 if (S_ISDIR(result
->d_inode
->i_mode
) &&
72 (result
->d_flags
& DCACHE_DISCONNECTED
)) {
73 /* it is an unconnected directory, we must connect it */
76 if (acceptable(context
, result
))
78 if (S_ISDIR(result
->d_inode
->i_mode
)) {
79 /* there is no other dentry, so fail */
82 /* try any other aliases */
83 spin_lock(&dcache_lock
);
84 head
= &result
->d_inode
->i_dentry
;
85 list_for_each(le
, head
) {
86 struct dentry
*dentry
= list_entry(le
, struct dentry
, d_alias
);
88 spin_unlock(&dcache_lock
);
92 if (dentry
!= result
&&
93 acceptable(context
, dentry
)) {
97 spin_lock(&dcache_lock
);
100 spin_unlock(&dcache_lock
);
105 /* It's a directory, or we are required to confirm the file's
106 * location in the tree based on the parent information
108 dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb
->s_id
,*(int*)obj
);
109 if (S_ISDIR(result
->d_inode
->i_mode
))
110 target_dir
= dget(result
);
115 target_dir
= CALL(sb
->s_export_op
,get_dentry
)(sb
,parent
);
116 if (IS_ERR(target_dir
))
117 err
= PTR_ERR(target_dir
);
118 if (target_dir
== NULL
|| IS_ERR(target_dir
))
122 * Now we need to make sure that target_dir is properly connected.
123 * It may already be, as the flag isn't always updated when connection
125 * So, we walk up parent links until we find a connected directory,
126 * or we run out of directories. Then we find the parent, find
127 * the name of the child in that parent, and do a lookup.
128 * This should connect the child into the parent
132 /* it is possible that a confused file system might not let us complete
133 * the path to the root. For example, if get_parent returns a directory
134 * in which we cannot find a name for the child. While this implies a
135 * very sick filesystem we don't want it to cause knfsd to spin. Hence
136 * the noprogress counter. If we go through the loop 10 times (2 is
137 * probably enough) without getting anywhere, we just give up
140 while (target_dir
->d_flags
& DCACHE_DISCONNECTED
&& noprogress
++ < 10) {
141 struct dentry
*pd
= target_dir
;
144 spin_lock(&pd
->d_lock
);
145 while (!IS_ROOT(pd
) &&
146 (pd
->d_parent
->d_flags
&DCACHE_DISCONNECTED
)) {
147 struct dentry
*parent
= pd
->d_parent
;
150 spin_unlock(&pd
->d_lock
);
153 spin_lock(&pd
->d_lock
);
155 spin_unlock(&pd
->d_lock
);
158 /* must have found a connected parent - great */
159 spin_lock(&pd
->d_lock
);
160 pd
->d_flags
&= ~DCACHE_DISCONNECTED
;
161 spin_unlock(&pd
->d_lock
);
163 } else if (pd
== sb
->s_root
) {
164 printk(KERN_ERR
"export: Eeek filesystem root is not connected, impossible\n");
165 spin_lock(&pd
->d_lock
);
166 pd
->d_flags
&= ~DCACHE_DISCONNECTED
;
167 spin_unlock(&pd
->d_lock
);
170 /* we have hit the top of a disconnected path. Try
171 * to find parent and connect
172 * note: racing with some other process renaming a
173 * directory isn't much of a problem here. If someone
174 * renames the directory, it will end up properly
175 * connected, which is what we want
179 char nbuf
[NAME_MAX
+1];
181 down(&pd
->d_inode
->i_sem
);
182 ppd
= CALL(nops
,get_parent
)(pd
);
183 up(&pd
->d_inode
->i_sem
);
187 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
188 pd
->d_inode
->i_ino
, err
);
192 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd
->d_inode
->i_ino
, ppd
->d_inode
->i_ino
);
193 err
= CALL(nops
,get_name
)(ppd
, nbuf
, pd
);
198 /* some race between get_parent and
199 * get_name? just try again
204 dprintk("find_exported_dentry: found name: %s\n", nbuf
);
205 down(&ppd
->d_inode
->i_sem
);
206 npd
= lookup_one_len(nbuf
, ppd
, strlen(nbuf
));
207 up(&ppd
->d_inode
->i_sem
);
210 dprintk("find_exported_dentry: lookup failed: %d\n", err
);
215 /* we didn't really want npd, we really wanted
216 * a side-effect of the lookup.
217 * hopefully, npd == pd, though it isn't really
218 * a problem if it isn't
223 printk("find_exported_dentry: npd != pd\n");
227 /* something went wrong, we have to give up */
235 if (target_dir
->d_flags
& DCACHE_DISCONNECTED
) {
236 /* something went wrong - oh-well */
241 /* if we weren't after a directory, have one more step to go */
242 if (result
!= target_dir
) {
243 struct dentry
*nresult
;
244 char nbuf
[NAME_MAX
+1];
245 err
= CALL(nops
,get_name
)(target_dir
, nbuf
, result
);
247 down(&target_dir
->d_inode
->i_sem
);
248 nresult
= lookup_one_len(nbuf
, target_dir
, strlen(nbuf
));
249 up(&target_dir
->d_inode
->i_sem
);
250 if (!IS_ERR(nresult
)) {
251 if (nresult
->d_inode
) {
260 /* now result is properly connected, it is our best bet */
261 if (acceptable(context
, result
))
263 /* one last try of the aliases.. */
264 spin_lock(&dcache_lock
);
266 head
= &result
->d_inode
->i_dentry
;
267 list_for_each(le
, head
) {
268 struct dentry
*dentry
= list_entry(le
, struct dentry
, d_alias
);
270 spin_unlock(&dcache_lock
);
271 if (toput
) dput(toput
);
272 if (dentry
!= result
&&
273 acceptable(context
, dentry
)) {
277 spin_lock(&dcache_lock
);
280 spin_unlock(&dcache_lock
);
284 /* drat - I just cannot find anything acceptable */
286 /* It might be justifiable to return ESTALE here,
287 * but the filehandle at-least looks reasonable good
288 * and it just be a permission problem, so returning
291 return ERR_PTR(-EACCES
);
303 static struct dentry
*get_parent(struct dentry
*child
)
305 /* get_parent cannot be supported generically, the locking
307 * instead, we just return EACCES. If server reboots or inodes
308 * get flushed, you lose
310 return ERR_PTR(-EACCES
);
314 struct getdents_callback
{
315 char *name
; /* name that was found. It already points to a
316 buffer NAME_MAX+1 is size */
317 unsigned long ino
; /* the inum we are looking for */
318 int found
; /* inode matched? */
319 int sequence
; /* sequence counter */
323 * A rather strange filldir function to capture
324 * the name matching the specified inode number.
326 static int filldir_one(void * __buf
, const char * name
, int len
,
327 loff_t pos
, ino_t ino
, unsigned int d_type
)
329 struct getdents_callback
*buf
= __buf
;
333 if (buf
->ino
== ino
) {
334 memcpy(buf
->name
, name
, len
);
335 buf
->name
[len
] = '\0';
343 * get_name - default export_operations->get_name function
344 * @dentry: the directory in which to find a name
345 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
346 * @child: the dentry for the child directory.
348 * calls readdir on the parent until it finds an entry with
349 * the same inode number as the child, and returns that.
351 static int get_name(struct dentry
*dentry
, char *name
,
352 struct dentry
*child
)
354 struct inode
*dir
= dentry
->d_inode
;
357 struct getdents_callback buffer
;
360 if (!dir
|| !S_ISDIR(dir
->i_mode
))
366 * Open the directory ...
368 file
= dentry_open(dget(dentry
), NULL
, O_RDONLY
);
369 error
= PTR_ERR(file
);
374 if (!file
->f_op
->readdir
)
378 buffer
.ino
= child
->d_inode
->i_ino
;
382 int old_seq
= buffer
.sequence
;
384 error
= vfs_readdir(file
, filldir_one
, &buffer
);
393 if (old_seq
== buffer
.sequence
)
404 static struct dentry
*export_iget(struct super_block
*sb
, unsigned long ino
, __u32 generation
)
407 /* iget isn't really right if the inode is currently unallocated!!
408 * This should really all be done inside each filesystem
410 * ext2fs' read_inode has been strengthed to return a bad_inode if
411 * the inode had been deleted.
413 * Currently we don't know the generation for parent directory, so
414 * a generation of 0 means "accept any"
417 struct dentry
*result
;
419 return ERR_PTR(-ESTALE
);
420 inode
= iget(sb
, ino
);
422 return ERR_PTR(-ENOMEM
);
423 if (is_bad_inode(inode
)
424 || (generation
&& inode
->i_generation
!= generation
)
426 /* we didn't find the right inode.. */
427 dprintk("fh_verify: Inode %lu, Bad count: %d %d or version %u %u\n",
429 inode
->i_nlink
, atomic_read(&inode
->i_count
),
434 return ERR_PTR(-ESTALE
);
436 /* now to find a dentry.
437 * If possible, get a well-connected one
439 result
= d_alloc_anon(inode
);
442 return ERR_PTR(-ENOMEM
);
448 static struct dentry
*get_object(struct super_block
*sb
, void *vobjp
)
451 unsigned long ino
= objp
[0];
452 __u32 generation
= objp
[1];
454 return export_iget(sb
, ino
, generation
);
459 * export_encode_fh - default export_operations->encode_fh function
460 * @dentry: the dentry to encode
461 * @fh: where to store the file handle fragment
462 * @max_len: maximum length to store there
463 * @connectable: whether to store parent information
465 * This default encode_fh function assumes that the 32 inode number
466 * is suitable for locating an inode, and that the generation number
467 * can be used to check that it is still valid. It places them in the
468 * filehandle fragment where export_decode_fh expects to find them.
470 static int export_encode_fh(struct dentry
*dentry
, __u32
*fh
, int *max_len
,
473 struct inode
* inode
= dentry
->d_inode
;
477 if (len
< 2 || (connectable
&& len
< 4))
481 fh
[0] = inode
->i_ino
;
482 fh
[1] = inode
->i_generation
;
483 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
484 struct inode
*parent
;
486 spin_lock(&dentry
->d_lock
);
487 parent
= dentry
->d_parent
->d_inode
;
488 fh
[2] = parent
->i_ino
;
489 fh
[3] = parent
->i_generation
;
490 spin_unlock(&dentry
->d_lock
);
500 * export_decode_fh - default export_operations->decode_fh function
501 * @sb: The superblock
502 * @fh: pointer to the file handle fragment
503 * @fh_len: length of file handle fragment
504 * @acceptable: function for testing acceptability of dentrys
505 * @context: context for @acceptable
507 * This is the default decode_fh() function.
508 * a fileid_type of 1 indicates that the filehandlefragment
509 * just contains an object identifier understood by get_dentry.
510 * a fileid_type of 2 says that there is also a directory
511 * identifier 8 bytes in to the filehandlefragement.
513 static struct dentry
*export_decode_fh(struct super_block
*sb
, __u32
*fh
, int fh_len
,
515 int (*acceptable
)(void *context
, struct dentry
*de
),
519 parent
[0] = parent
[1] = 0;
520 if (fh_len
< 2 || fileid_type
> 2)
522 if (fileid_type
== 2) {
523 if (fh_len
> 2) parent
[0] = fh
[2];
524 if (fh_len
> 3) parent
[1] = fh
[3];
526 return find_exported_dentry(sb
, fh
, parent
,
527 acceptable
, context
);
530 struct export_operations export_op_default
= {
531 .decode_fh
= export_decode_fh
,
532 .encode_fh
= export_encode_fh
,
534 .get_name
= get_name
,
535 .get_parent
= get_parent
,
536 .get_dentry
= get_object
,
539 EXPORT_SYMBOL(export_op_default
);
540 EXPORT_SYMBOL(find_exported_dentry
);
542 MODULE_LICENSE("GPL");