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