knfsd: exportfs: add procedural interface for NFSD
[linux-2.6] / fs / exportfs / expfs.c
1
2 #include <linux/exportfs.h>
3 #include <linux/fs.h>
4 #include <linux/file.h>
5 #include <linux/module.h>
6 #include <linux/mount.h>
7 #include <linux/namei.h>
8
9 struct export_operations export_op_default;
10
11 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
12
13 #define dprintk(fmt, args...) do{}while(0)
14
15 static struct dentry *
16 find_acceptable_alias(struct dentry *result,
17                 int (*acceptable)(void *context, struct dentry *dentry),
18                 void *context)
19 {
20         struct dentry *dentry, *toput = NULL;
21
22         spin_lock(&dcache_lock);
23         list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
24                 dget_locked(dentry);
25                 spin_unlock(&dcache_lock);
26                 if (toput)
27                         dput(toput);
28                 if (dentry != result && acceptable(context, dentry)) {
29                         dput(result);
30                         return dentry;
31                 }
32                 spin_lock(&dcache_lock);
33                 toput = dentry;
34         }
35         spin_unlock(&dcache_lock);
36
37         if (toput)
38                 dput(toput);
39         return NULL;
40 }
41
42 /**
43  * find_exported_dentry - helper routine to implement export_operations->decode_fh
44  * @sb:         The &super_block identifying the filesystem
45  * @obj:        An opaque identifier of the object to be found - passed to
46  *              get_inode
47  * @parent:     An optional opqaue identifier of the parent of the object.
48  * @acceptable: A function used to test possible &dentries to see if they are
49  *              acceptable
50  * @context:    A parameter to @acceptable so that it knows on what basis to
51  *              judge.
52  *
53  * find_exported_dentry is the central helper routine to enable file systems
54  * to provide the decode_fh() export_operation.  It's main task is to take
55  * an &inode, find or create an appropriate &dentry structure, and possibly
56  * splice this into the dcache in the correct place.
57  *
58  * The decode_fh() operation provided by the filesystem should call
59  * find_exported_dentry() with the same parameters that it received except
60  * that instead of the file handle fragment, pointers to opaque identifiers
61  * for the object and optionally its parent are passed.  The default decode_fh
62  * routine passes one pointer to the start of the filehandle fragment, and
63  * one 8 bytes into the fragment.  It is expected that most filesystems will
64  * take this approach, though the offset to the parent identifier may well be
65  * different.
66  *
67  * find_exported_dentry() will call get_dentry to get an dentry pointer from
68  * the file system.  If any &dentry in the d_alias list is acceptable, it will
69  * be returned.  Otherwise find_exported_dentry() will attempt to splice a new
70  * &dentry into the dcache using get_name() and get_parent() to find the
71  * appropriate place.
72  */
73
74 struct dentry *
75 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
76                      int (*acceptable)(void *context, struct dentry *de),
77                      void *context)
78 {
79         struct dentry *result = NULL;
80         struct dentry *target_dir;
81         int err;
82         struct export_operations *nops = sb->s_export_op;
83         struct dentry *alias;
84         int noprogress;
85         char nbuf[NAME_MAX+1];
86
87         /*
88          * Attempt to find the inode.
89          */
90         result = CALL(sb->s_export_op,get_dentry)(sb,obj);
91         err = -ESTALE;
92         if (result == NULL)
93                 goto err_out;
94         if (IS_ERR(result)) {
95                 err = PTR_ERR(result);
96                 goto err_out;
97         }
98         if (S_ISDIR(result->d_inode->i_mode) &&
99             (result->d_flags & DCACHE_DISCONNECTED)) {
100                 /* it is an unconnected directory, we must connect it */
101                 ;
102         } else {
103                 if (acceptable(context, result))
104                         return result;
105                 if (S_ISDIR(result->d_inode->i_mode)) {
106                         err = -EACCES;
107                         goto err_result;
108                 }
109
110                 alias = find_acceptable_alias(result, acceptable, context);
111                 if (alias)
112                         return alias;
113         }                       
114
115         /* It's a directory, or we are required to confirm the file's
116          * location in the tree based on the parent information
117          */
118         dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
119         if (S_ISDIR(result->d_inode->i_mode))
120                 target_dir = dget(result);
121         else {
122                 if (parent == NULL)
123                         goto err_result;
124
125                 target_dir = CALL(sb->s_export_op,get_dentry)(sb,parent);
126                 if (IS_ERR(target_dir))
127                         err = PTR_ERR(target_dir);
128                 if (target_dir == NULL || IS_ERR(target_dir))
129                         goto err_result;
130         }
131         /*
132          * Now we need to make sure that target_dir is properly connected.
133          * It may already be, as the flag isn't always updated when connection
134          * happens.
135          * So, we walk up parent links until we find a connected directory,
136          * or we run out of directories.  Then we find the parent, find
137          * the name of the child in that parent, and do a lookup.
138          * This should connect the child into the parent
139          * We then repeat.
140          */
141
142         /* it is possible that a confused file system might not let us complete 
143          * the path to the root.  For example, if get_parent returns a directory
144          * in which we cannot find a name for the child.  While this implies a
145          * very sick filesystem we don't want it to cause knfsd to spin.  Hence
146          * the noprogress counter.  If we go through the loop 10 times (2 is
147          * probably enough) without getting anywhere, we just give up
148          */
149         noprogress= 0;
150         while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
151                 struct dentry *pd = target_dir;
152
153                 dget(pd);
154                 spin_lock(&pd->d_lock);
155                 while (!IS_ROOT(pd) &&
156                                 (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
157                         struct dentry *parent = pd->d_parent;
158
159                         dget(parent);
160                         spin_unlock(&pd->d_lock);
161                         dput(pd);
162                         pd = parent;
163                         spin_lock(&pd->d_lock);
164                 }
165                 spin_unlock(&pd->d_lock);
166
167                 if (!IS_ROOT(pd)) {
168                         /* must have found a connected parent - great */
169                         spin_lock(&pd->d_lock);
170                         pd->d_flags &= ~DCACHE_DISCONNECTED;
171                         spin_unlock(&pd->d_lock);
172                         noprogress = 0;
173                 } else if (pd == sb->s_root) {
174                         printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
175                         spin_lock(&pd->d_lock);
176                         pd->d_flags &= ~DCACHE_DISCONNECTED;
177                         spin_unlock(&pd->d_lock);
178                         noprogress = 0;
179                 } else {
180                         /* we have hit the top of a disconnected path.  Try
181                          * to find parent and connect
182                          * note: racing with some other process renaming a
183                          * directory isn't much of a problem here.  If someone
184                          * renames the directory, it will end up properly
185                          * connected, which is what we want
186                          */
187                         struct dentry *ppd;
188                         struct dentry *npd;
189
190                         mutex_lock(&pd->d_inode->i_mutex);
191                         ppd = CALL(nops,get_parent)(pd);
192                         mutex_unlock(&pd->d_inode->i_mutex);
193
194                         if (IS_ERR(ppd)) {
195                                 err = PTR_ERR(ppd);
196                                 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
197                                         pd->d_inode->i_ino, err);
198                                 dput(pd);
199                                 break;
200                         }
201                         dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
202                         err = CALL(nops,get_name)(ppd, nbuf, pd);
203                         if (err) {
204                                 dput(ppd);
205                                 dput(pd);
206                                 if (err == -ENOENT)
207                                         /* some race between get_parent and
208                                          * get_name?  just try again
209                                          */
210                                         continue;
211                                 break;
212                         }
213                         dprintk("find_exported_dentry: found name: %s\n", nbuf);
214                         mutex_lock(&ppd->d_inode->i_mutex);
215                         npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
216                         mutex_unlock(&ppd->d_inode->i_mutex);
217                         if (IS_ERR(npd)) {
218                                 err = PTR_ERR(npd);
219                                 dprintk("find_exported_dentry: lookup failed: %d\n", err);
220                                 dput(ppd);
221                                 dput(pd);
222                                 break;
223                         }
224                         /* we didn't really want npd, we really wanted
225                          * a side-effect of the lookup.
226                          * hopefully, npd == pd, though it isn't really
227                          * a problem if it isn't
228                          */
229                         if (npd == pd)
230                                 noprogress = 0;
231                         else
232                                 printk("find_exported_dentry: npd != pd\n");
233                         dput(npd);
234                         dput(ppd);
235                         if (IS_ROOT(pd)) {
236                                 /* something went wrong, we have to give up */
237                                 dput(pd);
238                                 break;
239                         }
240                 }
241                 dput(pd);
242         }
243
244         if (target_dir->d_flags & DCACHE_DISCONNECTED) {
245                 /* something went wrong - oh-well */
246                 if (!err)
247                         err = -ESTALE;
248                 goto err_target;
249         }
250         /* if we weren't after a directory, have one more step to go */
251         if (result != target_dir) {
252                 struct dentry *nresult;
253                 err = CALL(nops,get_name)(target_dir, nbuf, result);
254                 if (!err) {
255                         mutex_lock(&target_dir->d_inode->i_mutex);
256                         nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
257                         mutex_unlock(&target_dir->d_inode->i_mutex);
258                         if (!IS_ERR(nresult)) {
259                                 if (nresult->d_inode) {
260                                         dput(result);
261                                         result = nresult;
262                                 } else
263                                         dput(nresult);
264                         }
265                 }
266         }
267         dput(target_dir);
268         /* now result is properly connected, it is our best bet */
269         if (acceptable(context, result))
270                 return result;
271
272         alias = find_acceptable_alias(result, acceptable, context);
273         if (alias)
274                 return alias;
275
276         /* drat - I just cannot find anything acceptable */
277         dput(result);
278         /* It might be justifiable to return ESTALE here,
279          * but the filehandle at-least looks reasonable good
280          * and it just be a permission problem, so returning
281          * -EACCESS is safer
282          */
283         return ERR_PTR(-EACCES);
284
285  err_target:
286         dput(target_dir);
287  err_result:
288         dput(result);
289  err_out:
290         return ERR_PTR(err);
291 }
292
293
294
295 static struct dentry *get_parent(struct dentry *child)
296 {
297         /* get_parent cannot be supported generically, the locking
298          * is too icky.
299          * instead, we just return EACCES.  If server reboots or inodes
300          * get flushed, you lose
301          */
302         return ERR_PTR(-EACCES);
303 }
304
305
306 struct getdents_callback {
307         char *name;             /* name that was found. It already points to a
308                                    buffer NAME_MAX+1 is size */
309         unsigned long ino;      /* the inum we are looking for */
310         int found;              /* inode matched? */
311         int sequence;           /* sequence counter */
312 };
313
314 /*
315  * A rather strange filldir function to capture
316  * the name matching the specified inode number.
317  */
318 static int filldir_one(void * __buf, const char * name, int len,
319                         loff_t pos, u64 ino, unsigned int d_type)
320 {
321         struct getdents_callback *buf = __buf;
322         int result = 0;
323
324         buf->sequence++;
325         if (buf->ino == ino) {
326                 memcpy(buf->name, name, len);
327                 buf->name[len] = '\0';
328                 buf->found = 1;
329                 result = -1;
330         }
331         return result;
332 }
333
334 /**
335  * get_name - default export_operations->get_name function
336  * @dentry: the directory in which to find a name
337  * @name:   a pointer to a %NAME_MAX+1 char buffer to store the name
338  * @child:  the dentry for the child directory.
339  *
340  * calls readdir on the parent until it finds an entry with
341  * the same inode number as the child, and returns that.
342  */
343 static int get_name(struct dentry *dentry, char *name,
344                         struct dentry *child)
345 {
346         struct inode *dir = dentry->d_inode;
347         int error;
348         struct file *file;
349         struct getdents_callback buffer;
350
351         error = -ENOTDIR;
352         if (!dir || !S_ISDIR(dir->i_mode))
353                 goto out;
354         error = -EINVAL;
355         if (!dir->i_fop)
356                 goto out;
357         /*
358          * Open the directory ...
359          */
360         file = dentry_open(dget(dentry), NULL, O_RDONLY);
361         error = PTR_ERR(file);
362         if (IS_ERR(file))
363                 goto out;
364
365         error = -EINVAL;
366         if (!file->f_op->readdir)
367                 goto out_close;
368
369         buffer.name = name;
370         buffer.ino = child->d_inode->i_ino;
371         buffer.found = 0;
372         buffer.sequence = 0;
373         while (1) {
374                 int old_seq = buffer.sequence;
375
376                 error = vfs_readdir(file, filldir_one, &buffer);
377
378                 if (error < 0)
379                         break;
380
381                 error = 0;
382                 if (buffer.found)
383                         break;
384                 error = -ENOENT;
385                 if (old_seq == buffer.sequence)
386                         break;
387         }
388
389 out_close:
390         fput(file);
391 out:
392         return error;
393 }
394
395 static struct dentry *get_dentry(struct super_block *sb, void *vobjp)
396 {
397         return ERR_PTR(-ESTALE);
398 }
399
400 /**
401  * export_encode_fh - default export_operations->encode_fh function
402  * @dentry:  the dentry to encode
403  * @fh:      where to store the file handle fragment
404  * @max_len: maximum length to store there
405  * @connectable: whether to store parent information
406  *
407  * This default encode_fh function assumes that the 32 inode number
408  * is suitable for locating an inode, and that the generation number
409  * can be used to check that it is still valid.  It places them in the
410  * filehandle fragment where export_decode_fh expects to find them.
411  */
412 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
413                    int connectable)
414 {
415         struct inode * inode = dentry->d_inode;
416         int len = *max_len;
417         int type = 1;
418         
419         if (len < 2 || (connectable && len < 4))
420                 return 255;
421
422         len = 2;
423         fh[0] = inode->i_ino;
424         fh[1] = inode->i_generation;
425         if (connectable && !S_ISDIR(inode->i_mode)) {
426                 struct inode *parent;
427
428                 spin_lock(&dentry->d_lock);
429                 parent = dentry->d_parent->d_inode;
430                 fh[2] = parent->i_ino;
431                 fh[3] = parent->i_generation;
432                 spin_unlock(&dentry->d_lock);
433                 len = 4;
434                 type = 2;
435         }
436         *max_len = len;
437         return type;
438 }
439
440
441 /**
442  * export_decode_fh - default export_operations->decode_fh function
443  * @sb:  The superblock
444  * @fh:  pointer to the file handle fragment
445  * @fh_len: length of file handle fragment
446  * @acceptable: function for testing acceptability of dentrys
447  * @context:   context for @acceptable
448  *
449  * This is the default decode_fh() function.
450  * a fileid_type of 1 indicates that the filehandlefragment
451  * just contains an object identifier understood by  get_dentry.
452  * a fileid_type of 2 says that there is also a directory
453  * identifier 8 bytes in to the filehandlefragement.
454  */
455 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
456                               int fileid_type,
457                          int (*acceptable)(void *context, struct dentry *de),
458                          void *context)
459 {
460         __u32 parent[2];
461         parent[0] = parent[1] = 0;
462         if (fh_len < 2 || fileid_type > 2)
463                 return NULL;
464         if (fileid_type == 2) {
465                 if (fh_len > 2) parent[0] = fh[2];
466                 if (fh_len > 3) parent[1] = fh[3];
467         }
468         return find_exported_dentry(sb, fh, parent,
469                                    acceptable, context);
470 }
471
472 int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
473                 int connectable)
474 {
475         struct export_operations *nop = dentry->d_sb->s_export_op;
476
477         return CALL(nop, encode_fh)(dentry, fh, max_len, connectable);
478 }
479 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
480
481 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
482                 int fileid_type, int (*acceptable)(void *, struct dentry *),
483                 void *context)
484 {
485         struct export_operations *nop = mnt->mnt_sb->s_export_op;
486
487         return CALL(nop, decode_fh)(mnt->mnt_sb, fh, fh_len, fileid_type,
488                         acceptable, context);
489 }
490 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
491
492 struct export_operations export_op_default = {
493         .decode_fh      = export_decode_fh,
494         .encode_fh      = export_encode_fh,
495
496         .get_name       = get_name,
497         .get_parent     = get_parent,
498         .get_dentry     = get_dentry,
499 };
500
501 EXPORT_SYMBOL(find_exported_dentry);
502
503 MODULE_LICENSE("GPL");