Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[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 #define dprintk(fmt, args...) do{}while(0)
10
11
12 static int get_name(struct dentry *dentry, char *name,
13                 struct dentry *child);
14
15
16 static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj)
17 {
18         struct dentry *result = ERR_PTR(-ESTALE);
19
20         if (sb->s_export_op->get_dentry) {
21                 result = sb->s_export_op->get_dentry(sb, obj);
22                 if (!result)
23                         result = ERR_PTR(-ESTALE);
24         }
25
26         return result;
27 }
28
29 static int exportfs_get_name(struct dentry *dir, char *name,
30                 struct dentry *child)
31 {
32         struct export_operations *nop = dir->d_sb->s_export_op;
33
34         if (nop->get_name)
35                 return nop->get_name(dir, name, child);
36         else
37                 return get_name(dir, name, child);
38 }
39
40 /*
41  * Check if the dentry or any of it's aliases is acceptable.
42  */
43 static struct dentry *
44 find_acceptable_alias(struct dentry *result,
45                 int (*acceptable)(void *context, struct dentry *dentry),
46                 void *context)
47 {
48         struct dentry *dentry, *toput = NULL;
49
50         if (acceptable(context, result))
51                 return result;
52
53         spin_lock(&dcache_lock);
54         list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
55                 dget_locked(dentry);
56                 spin_unlock(&dcache_lock);
57                 if (toput)
58                         dput(toput);
59                 if (dentry != result && acceptable(context, dentry)) {
60                         dput(result);
61                         return dentry;
62                 }
63                 spin_lock(&dcache_lock);
64                 toput = dentry;
65         }
66         spin_unlock(&dcache_lock);
67
68         if (toput)
69                 dput(toput);
70         return NULL;
71 }
72
73 /*
74  * Find root of a disconnected subtree and return a reference to it.
75  */
76 static struct dentry *
77 find_disconnected_root(struct dentry *dentry)
78 {
79         dget(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;
84                 dget(parent);
85                 spin_unlock(&dentry->d_lock);
86                 dput(dentry);
87                 dentry = parent;
88                 spin_lock(&dentry->d_lock);
89         }
90         spin_unlock(&dentry->d_lock);
91         return dentry;
92 }
93
94
95 /*
96  * Make sure target_dir is fully connected to the dentry tree.
97  *
98  * It may already be, as the flag isn't always updated when connection happens.
99  */
100 static int
101 reconnect_path(struct super_block *sb, struct dentry *target_dir)
102 {
103         char nbuf[NAME_MAX+1];
104         int noprogress = 0;
105         int err = -ESTALE;
106
107         /*
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
114          */
115         while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
116                 struct dentry *pd = find_disconnected_root(target_dir);
117
118                 if (!IS_ROOT(pd)) {
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);
123                         noprogress = 0;
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);
129                         noprogress = 0;
130                 } else {
131                         /*
132                          * We have hit the top of a disconnected path, try to
133                          * find parent and connect.
134                          *
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
139                          *
140                          * Getting the parent can't be supported generically,
141                          * the locking is too icky.
142                          *
143                          * Instead we just return EACCES.  If server reboots
144                          * or inodes get flushed, you lose
145                          */
146                         struct dentry *ppd = ERR_PTR(-EACCES);
147                         struct dentry *npd;
148
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);
153
154                         if (IS_ERR(ppd)) {
155                                 err = PTR_ERR(ppd);
156                                 dprintk("%s: get_parent of %ld failed, err %d\n",
157                                         __FUNCTION__, pd->d_inode->i_ino, err);
158                                 dput(pd);
159                                 break;
160                         }
161
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);
165                         if (err) {
166                                 dput(ppd);
167                                 dput(pd);
168                                 if (err == -ENOENT)
169                                         /* some race between get_parent and
170                                          * get_name?  just try again
171                                          */
172                                         continue;
173                                 break;
174                         }
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);
179                         if (IS_ERR(npd)) {
180                                 err = PTR_ERR(npd);
181                                 dprintk("%s: lookup failed: %d\n",
182                                         __FUNCTION__, err);
183                                 dput(ppd);
184                                 dput(pd);
185                                 break;
186                         }
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
191                          */
192                         if (npd == pd)
193                                 noprogress = 0;
194                         else
195                                 printk("%s: npd != pd\n", __FUNCTION__);
196                         dput(npd);
197                         dput(ppd);
198                         if (IS_ROOT(pd)) {
199                                 /* something went wrong, we have to give up */
200                                 dput(pd);
201                                 break;
202                         }
203                 }
204                 dput(pd);
205         }
206
207         if (target_dir->d_flags & DCACHE_DISCONNECTED) {
208                 /* something went wrong - oh-well */
209                 if (!err)
210                         err = -ESTALE;
211                 return err;
212         }
213
214         return 0;
215 }
216
217 /**
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
221  *              get_inode
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
224  *              acceptable
225  * @context:    A parameter to @acceptable so that it knows on what basis to
226  *              judge.
227  *
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.
232  *
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
240  * different.
241  *
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
246  * appropriate place.
247  */
248
249 struct dentry *
250 find_exported_dentry(struct super_block *sb, void *obj, void *parent,
251                      int (*acceptable)(void *context, struct dentry *de),
252                      void *context)
253 {
254         struct dentry *result, *alias;
255         int err = -ESTALE;
256
257         /*
258          * Attempt to find the inode.
259          */
260         result = exportfs_get_dentry(sb, obj);
261         if (IS_ERR(result))
262                 return result;
263
264         if (S_ISDIR(result->d_inode->i_mode)) {
265                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
266                         if (acceptable(context, result))
267                                 return result;
268                         err = -EACCES;
269                         goto err_result;
270                 }
271
272                 err = reconnect_path(sb, result);
273                 if (err)
274                         goto err_result;
275         } else {
276                 struct dentry *target_dir, *nresult;
277                 char nbuf[NAME_MAX+1];
278
279                 alias = find_acceptable_alias(result, acceptable, context);
280                 if (alias)
281                         return alias;
282
283                 if (parent == NULL)
284                         goto err_result;
285
286                 target_dir = exportfs_get_dentry(sb,parent);
287                 if (IS_ERR(target_dir)) {
288                         err = PTR_ERR(target_dir);
289                         goto err_result;
290                 }
291
292                 err = reconnect_path(sb, target_dir);
293                 if (err) {
294                         dput(target_dir);
295                         goto err_result;
296                 }
297
298                 /*
299                  * As we weren't after a directory, have one more step to go.
300                  */
301                 err = exportfs_get_name(target_dir, nbuf, result);
302                 if (!err) {
303                         mutex_lock(&target_dir->d_inode->i_mutex);
304                         nresult = lookup_one_len(nbuf, target_dir,
305                                                  strlen(nbuf));
306                         mutex_unlock(&target_dir->d_inode->i_mutex);
307                         if (!IS_ERR(nresult)) {
308                                 if (nresult->d_inode) {
309                                         dput(result);
310                                         result = nresult;
311                                 } else
312                                         dput(nresult);
313                         }
314                 }
315                 dput(target_dir);
316         }
317
318         alias = find_acceptable_alias(result, acceptable, context);
319         if (alias)
320                 return alias;
321
322         /* drat - I just cannot find anything acceptable */
323         dput(result);
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
327          * -EACCESS is safer
328          */
329         return ERR_PTR(-EACCES);
330
331  err_result:
332         dput(result);
333         return ERR_PTR(err);
334 }
335
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 */
342 };
343
344 /*
345  * A rather strange filldir function to capture
346  * the name matching the specified inode number.
347  */
348 static int filldir_one(void * __buf, const char * name, int len,
349                         loff_t pos, u64 ino, unsigned int d_type)
350 {
351         struct getdents_callback *buf = __buf;
352         int result = 0;
353
354         buf->sequence++;
355         if (buf->ino == ino) {
356                 memcpy(buf->name, name, len);
357                 buf->name[len] = '\0';
358                 buf->found = 1;
359                 result = -1;
360         }
361         return result;
362 }
363
364 /**
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.
369  *
370  * calls readdir on the parent until it finds an entry with
371  * the same inode number as the child, and returns that.
372  */
373 static int get_name(struct dentry *dentry, char *name,
374                         struct dentry *child)
375 {
376         struct inode *dir = dentry->d_inode;
377         int error;
378         struct file *file;
379         struct getdents_callback buffer;
380
381         error = -ENOTDIR;
382         if (!dir || !S_ISDIR(dir->i_mode))
383                 goto out;
384         error = -EINVAL;
385         if (!dir->i_fop)
386                 goto out;
387         /*
388          * Open the directory ...
389          */
390         file = dentry_open(dget(dentry), NULL, O_RDONLY);
391         error = PTR_ERR(file);
392         if (IS_ERR(file))
393                 goto out;
394
395         error = -EINVAL;
396         if (!file->f_op->readdir)
397                 goto out_close;
398
399         buffer.name = name;
400         buffer.ino = child->d_inode->i_ino;
401         buffer.found = 0;
402         buffer.sequence = 0;
403         while (1) {
404                 int old_seq = buffer.sequence;
405
406                 error = vfs_readdir(file, filldir_one, &buffer);
407
408                 if (error < 0)
409                         break;
410
411                 error = 0;
412                 if (buffer.found)
413                         break;
414                 error = -ENOENT;
415                 if (old_seq == buffer.sequence)
416                         break;
417         }
418
419 out_close:
420         fput(file);
421 out:
422         return error;
423 }
424
425 /**
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
431  *
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.
436  */
437 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
438                    int connectable)
439 {
440         struct inode * inode = dentry->d_inode;
441         int len = *max_len;
442         int type = 1;
443         
444         if (len < 2 || (connectable && len < 4))
445                 return 255;
446
447         len = 2;
448         fh[0] = inode->i_ino;
449         fh[1] = inode->i_generation;
450         if (connectable && !S_ISDIR(inode->i_mode)) {
451                 struct inode *parent;
452
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);
458                 len = 4;
459                 type = 2;
460         }
461         *max_len = len;
462         return type;
463 }
464
465
466 /**
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
473  *
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.
479  */
480 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
481                               int fileid_type,
482                          int (*acceptable)(void *context, struct dentry *de),
483                          void *context)
484 {
485         __u32 parent[2];
486         parent[0] = parent[1] = 0;
487         if (fh_len < 2 || fileid_type > 2)
488                 return NULL;
489         if (fileid_type == 2) {
490                 if (fh_len > 2) parent[0] = fh[2];
491                 if (fh_len > 3) parent[1] = fh[3];
492         }
493         return find_exported_dentry(sb, fh, parent,
494                                    acceptable, context);
495 }
496
497 int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
498                 int connectable)
499 {
500         struct export_operations *nop = dentry->d_sb->s_export_op;
501         int error;
502
503         if (nop->encode_fh)
504                 error = nop->encode_fh(dentry, fh, max_len, connectable);
505         else
506                 error = export_encode_fh(dentry, fh, max_len, connectable);
507
508         return error;
509 }
510 EXPORT_SYMBOL_GPL(exportfs_encode_fh);
511
512 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
513                 int fileid_type, int (*acceptable)(void *, struct dentry *),
514                 void *context)
515 {
516         struct export_operations *nop = mnt->mnt_sb->s_export_op;
517         struct dentry *result;
518
519         if (nop->decode_fh) {
520                 result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
521                         acceptable, context);
522         } else {
523                 result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
524                         acceptable, context);
525         }
526
527         return result;
528 }
529 EXPORT_SYMBOL_GPL(exportfs_decode_fh);
530
531 EXPORT_SYMBOL(find_exported_dentry);
532
533 MODULE_LICENSE("GPL");