Replace extern inline with static inline.
[linux-2.6/linux-mips.git] / fs / exportfs / expfs.c
blobaae953bb957290ef7d5d1142544331d9a6e2de4d
2 #include <linux/fs.h>
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)
13 /**
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
17 * get_inode
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
20 * acceptable
21 * @context: A parameter to @acceptable so that it knows on what basis to
22 * judge.
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
36 * different.
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
42 * appropriate place.
45 struct dentry *
46 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
47 int (*acceptable)(void *context, struct dentry *de),
48 void *context)
50 struct dentry *result = NULL;
51 struct dentry *target_dir;
52 int err;
53 struct export_operations *nops = sb->s_export_op;
54 struct list_head *le, *head;
55 struct dentry *toput = NULL;
56 int noprogress;
60 * Attempt to find the inode.
62 result = CALL(sb->s_export_op,get_dentry)(sb,obj);
63 err = -ESTALE;
64 if (result == NULL)
65 goto err_out;
66 if (IS_ERR(result)) {
67 err = PTR_ERR(result);
68 goto err_out;
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 */
74 } else {
75 if (acceptable(context, result))
76 return result;
77 if (S_ISDIR(result->d_inode->i_mode)) {
78 /* there is no other dentry, so fail */
79 goto err_result;
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);
86 dget_locked(dentry);
87 spin_unlock(&dcache_lock);
88 if (toput)
89 dput(toput);
90 toput = NULL;
91 if (dentry != result &&
92 acceptable(context, dentry)) {
93 dput(result);
94 return dentry;
96 spin_lock(&dcache_lock);
97 toput = dentry;
99 spin_unlock(&dcache_lock);
100 if (toput)
101 dput(toput);
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);
110 else {
111 if (parent == NULL)
112 goto err_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))
118 goto err_result;
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
123 * happens.
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
128 * We then repeat.
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
138 lock_kernel();
139 noprogress= 0;
140 while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
141 struct dentry *pd = target_dir;
143 dget(pd);
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;
149 dget(parent);
150 spin_unlock(&pd->d_lock);
151 dput(pd);
152 pd = parent;
153 spin_lock(&pd->d_lock);
155 spin_unlock(&pd->d_lock);
157 if (!IS_ROOT(pd)) {
158 /* must have found a connected parent - great */
159 pd->d_flags &= ~DCACHE_DISCONNECTED;
160 noprogress = 0;
161 } else if (pd == sb->s_root) {
162 printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
163 pd->d_flags &= ~DCACHE_DISCONNECTED;
164 noprogress = 0;
165 } else {
166 /* we have hit the top of a disconnected path. Try
167 * to find parent and connect
168 * note: racing with some other process renaming a
169 * directory isn't much of a problem here. If someone
170 * renames the directory, it will end up properly
171 * connected, which is what we want
173 struct dentry *ppd;
174 struct dentry *npd;
175 char nbuf[NAME_MAX+1];
177 down(&pd->d_inode->i_sem);
178 ppd = CALL(nops,get_parent)(pd);
179 up(&pd->d_inode->i_sem);
181 if (IS_ERR(ppd)) {
182 err = PTR_ERR(ppd);
183 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
184 pd->d_inode->i_ino, err);
185 dput(pd);
186 break;
188 dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
189 err = CALL(nops,get_name)(ppd, nbuf, pd);
190 if (err) {
191 dput(ppd);
192 dput(pd);
193 if (err == -ENOENT)
194 /* some race between get_parent and
195 * get_name? just try again
197 continue;
198 break;
200 dprintk("find_exported_dentry: found name: %s\n", nbuf);
201 down(&ppd->d_inode->i_sem);
202 npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
203 up(&ppd->d_inode->i_sem);
204 if (IS_ERR(npd)) {
205 err = PTR_ERR(npd);
206 dprintk("find_exported_dentry: lookup failed: %d\n", err);
207 dput(ppd);
208 dput(pd);
209 break;
211 /* we didn't really want npd, we really wanted
212 * a side-effect of the lookup.
213 * hopefully, npd == pd, though it isn't really
214 * a problem if it isn't
216 if (npd == pd)
217 noprogress = 0;
218 else
219 printk("find_exported_dentry: npd != pd\n");
220 dput(npd);
221 dput(ppd);
222 if (IS_ROOT(pd)) {
223 /* something went wrong, we have to give up */
224 dput(pd);
225 break;
228 dput(pd);
231 if (target_dir->d_flags & DCACHE_DISCONNECTED) {
232 /* something went wrong - oh-well */
233 if (!err)
234 err = -ESTALE;
235 unlock_kernel();
236 goto err_target;
238 /* if we weren't after a directory, have one more step to go */
239 if (result != target_dir) {
240 struct dentry *nresult;
241 char nbuf[NAME_MAX+1];
242 err = CALL(nops,get_name)(target_dir, nbuf, result);
243 if (!err) {
244 down(&target_dir->d_inode->i_sem);
245 nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
246 up(&target_dir->d_inode->i_sem);
247 if (!IS_ERR(nresult)) {
248 if (nresult->d_inode) {
249 dput(result);
250 result = nresult;
251 } else
252 dput(nresult);
256 dput(target_dir);
257 unlock_kernel();
258 /* now result is properly connected, it is our best bet */
259 if (acceptable(context, result))
260 return result;
261 /* one last try of the aliases.. */
262 spin_lock(&dcache_lock);
263 toput = NULL;
264 head = &result->d_inode->i_dentry;
265 list_for_each(le, head) {
266 struct dentry *dentry = list_entry(le, struct dentry, d_alias);
267 dget_locked(dentry);
268 spin_unlock(&dcache_lock);
269 if (toput) dput(toput);
270 if (dentry != result &&
271 acceptable(context, dentry)) {
272 dput(result);
273 return dentry;
275 spin_lock(&dcache_lock);
276 toput = dentry;
278 spin_unlock(&dcache_lock);
279 if (toput)
280 dput(toput);
282 /* drat - I just cannot find anything acceptable */
283 dput(result);
284 return ERR_PTR(-ESTALE);
286 err_target:
287 dput(target_dir);
288 err_result:
289 dput(result);
290 err_out:
291 return ERR_PTR(err);
296 static struct dentry *get_parent(struct dentry *child)
298 /* get_parent cannot be supported generically, the locking
299 * is too icky.
300 * instead, we just return EACCES. If server reboots or inodes
301 * get flushed, you lose
303 return ERR_PTR(-EACCES);
307 struct getdents_callback {
308 char *name; /* name that was found. It already points to a
309 buffer NAME_MAX+1 is size */
310 unsigned long ino; /* the inum we are looking for */
311 int found; /* inode matched? */
312 int sequence; /* sequence counter */
316 * A rather strange filldir function to capture
317 * the name matching the specified inode number.
319 static int filldir_one(void * __buf, const char * name, int len,
320 loff_t pos, ino_t ino, unsigned int d_type)
322 struct getdents_callback *buf = __buf;
323 int result = 0;
325 buf->sequence++;
326 if (buf->ino == ino) {
327 memcpy(buf->name, name, len);
328 buf->name[len] = '\0';
329 buf->found = 1;
330 result = -1;
332 return result;
336 * get_name - default export_operations->get_name function
337 * @dentry: the directory in which to find a name
338 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
339 * @child: the dentry for the child directory.
341 * calls readdir on the parent until it finds an entry with
342 * the same inode number as the child, and returns that.
344 static int get_name(struct dentry *dentry, char *name,
345 struct dentry *child)
347 struct inode *dir = dentry->d_inode;
348 int error;
349 struct file file;
350 struct getdents_callback buffer;
352 error = -ENOTDIR;
353 if (!dir || !S_ISDIR(dir->i_mode))
354 goto out;
355 error = -EINVAL;
356 if (!dir->i_fop)
357 goto out;
359 * Open the directory ...
361 error = open_private_file(&file, dentry, O_RDONLY);
362 if (error)
363 goto out;
364 error = -EINVAL;
365 if (!file.f_op->readdir)
366 goto out_close;
368 buffer.name = name;
369 buffer.ino = child->d_inode->i_ino;
370 buffer.found = 0;
371 buffer.sequence = 0;
372 while (1) {
373 int old_seq = buffer.sequence;
375 error = vfs_readdir(&file, filldir_one, &buffer);
377 if (error < 0)
378 break;
380 error = 0;
381 if (buffer.found)
382 break;
383 error = -ENOENT;
384 if (old_seq == buffer.sequence)
385 break;
388 out_close:
389 close_private_file(&file);
390 out:
391 return error;
395 static struct dentry *export_iget(struct super_block *sb, unsigned long ino, __u32 generation)
398 /* iget isn't really right if the inode is currently unallocated!!
399 * This should really all be done inside each filesystem
401 * ext2fs' read_inode has been strengthed to return a bad_inode if
402 * the inode had been deleted.
404 * Currently we don't know the generation for parent directory, so
405 * a generation of 0 means "accept any"
407 struct inode *inode;
408 struct dentry *result;
409 if (ino == 0)
410 return ERR_PTR(-ESTALE);
411 inode = iget(sb, ino);
412 if (inode == NULL)
413 return ERR_PTR(-ENOMEM);
414 if (is_bad_inode(inode)
415 || (generation && inode->i_generation != generation)
417 /* we didn't find the right inode.. */
418 dprintk("fh_verify: Inode %lu, Bad count: %d %d or version %u %u\n",
419 inode->i_ino,
420 inode->i_nlink, atomic_read(&inode->i_count),
421 inode->i_generation,
422 generation);
424 iput(inode);
425 return ERR_PTR(-ESTALE);
427 /* now to find a dentry.
428 * If possible, get a well-connected one
430 result = d_alloc_anon(inode);
431 if (!result) {
432 iput(inode);
433 return ERR_PTR(-ENOMEM);
435 return result;
439 static struct dentry *get_object(struct super_block *sb, void *vobjp)
441 __u32 *objp = vobjp;
442 unsigned long ino = objp[0];
443 __u32 generation = objp[1];
445 return export_iget(sb, ino, generation);
450 * export_encode_fh - default export_operations->encode_fh function
451 * @dentry: the dentry to encode
452 * @fh: where to store the file handle fragment
453 * @max_len: maximum length to store there
454 * @connectable: whether to store parent information
456 * This default encode_fh function assumes that the 32 inode number
457 * is suitable for locating an inode, and that the generation number
458 * can be used to check that it is still valid. It places them in the
459 * filehandle fragment where export_decode_fh expects to find them.
461 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
462 int connectable)
464 struct inode * inode = dentry->d_inode;
465 int len = *max_len;
466 int type = 1;
468 if (len < 2 || (connectable && len < 4))
469 return 255;
471 len = 2;
472 fh[0] = inode->i_ino;
473 fh[1] = inode->i_generation;
474 if (connectable && !S_ISDIR(inode->i_mode)) {
475 struct inode *parent;
477 spin_lock(&dentry->d_lock);
478 parent = dentry->d_parent->d_inode;
479 fh[2] = parent->i_ino;
480 fh[3] = parent->i_generation;
481 spin_unlock(&dentry->d_lock);
482 len = 4;
483 type = 2;
485 *max_len = len;
486 return type;
491 * export_decode_fh - default export_operations->decode_fh function
492 * @sb: The superblock
493 * @fh: pointer to the file handle fragment
494 * @fh_len: length of file handle fragment
495 * @acceptable: function for testing acceptability of dentrys
496 * @context: context for @acceptable
498 * This is the default decode_fh() function.
499 * a fileid_type of 1 indicates that the filehandlefragment
500 * just contains an object identifier understood by get_dentry.
501 * a fileid_type of 2 says that there is also a directory
502 * identifier 8 bytes in to the filehandlefragement.
504 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
505 int fileid_type,
506 int (*acceptable)(void *context, struct dentry *de),
507 void *context)
509 __u32 parent[2];
510 parent[0] = parent[1] = 0;
511 if (fh_len < 2 || fileid_type > 2)
512 return NULL;
513 if (fileid_type == 2) {
514 if (fh_len > 2) parent[0] = fh[2];
515 if (fh_len > 3) parent[1] = fh[3];
517 return find_exported_dentry(sb, fh, parent,
518 acceptable, context);
521 struct export_operations export_op_default = {
522 .decode_fh = export_decode_fh,
523 .encode_fh = export_encode_fh,
525 .get_name = get_name,
526 .get_parent = get_parent,
527 .get_dentry = get_object,
530 EXPORT_SYMBOL(export_op_default);
531 EXPORT_SYMBOL(find_exported_dentry);
533 MODULE_LICENSE("GPL");