patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / exportfs / expfs.c
1
2 #include <linux/fs.h>
3 #include <linux/module.h>
4 #include <linux/smp_lock.h>
5 #include <linux/namei.h>
6
7 struct export_operations export_op_default;
8
9 #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
10
11 #define dprintk(fmt, args...) do{}while(0)
12
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.
23  *
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.
28  *
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.
37  *
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.
43  */
44
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)
49 {
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;
57
58
59         /*
60          * Attempt to find the inode.
61          */
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;
69         }
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 */
73                 ;
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;
80                 }
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;
95                         }
96                         spin_lock(&dcache_lock);
97                         toput = dentry;
98                 }
99                 spin_unlock(&dcache_lock);
100                 if (toput)
101                         dput(toput);
102         }                       
103
104         /* It's a directory, or we are required to confirm the file's
105          * location in the tree based on the parent information
106          */
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;
113
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;
119         }
120         /*
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.
129          */
130
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
137          */
138         noprogress= 0;
139         while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
140                 struct dentry *pd = target_dir;
141
142                 dget(pd);
143                 spin_lock(&pd->d_lock);
144                 while (!IS_ROOT(pd) &&
145                                 (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
146                         struct dentry *parent = pd->d_parent;
147
148                         dget(parent);
149                         spin_unlock(&pd->d_lock);
150                         dput(pd);
151                         pd = parent;
152                         spin_lock(&pd->d_lock);
153                 }
154                 spin_unlock(&pd->d_lock);
155
156                 if (!IS_ROOT(pd)) {
157                         /* must have found a connected parent - great */
158                         spin_lock(&pd->d_lock);
159                         pd->d_flags &= ~DCACHE_DISCONNECTED;
160                         spin_unlock(&pd->d_lock);
161                         noprogress = 0;
162                 } else if (pd == sb->s_root) {
163                         printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
164                         spin_lock(&pd->d_lock);
165                         pd->d_flags &= ~DCACHE_DISCONNECTED;
166                         spin_unlock(&pd->d_lock);
167                         noprogress = 0;
168                 } else {
169                         /* we have hit the top of a disconnected path.  Try
170                          * to find parent and connect
171                          * note: racing with some other process renaming a
172                          * directory isn't much of a problem here.  If someone
173                          * renames the directory, it will end up properly
174                          * connected, which is what we want
175                          */
176                         struct dentry *ppd;
177                         struct dentry *npd;
178                         char nbuf[NAME_MAX+1];
179
180                         down(&pd->d_inode->i_sem);
181                         ppd = CALL(nops,get_parent)(pd);
182                         up(&pd->d_inode->i_sem);
183
184                         if (IS_ERR(ppd)) {
185                                 err = PTR_ERR(ppd);
186                                 dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
187                                         pd->d_inode->i_ino, err);
188                                 dput(pd);
189                                 break;
190                         }
191                         dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
192                         err = CALL(nops,get_name)(ppd, nbuf, pd);
193                         if (err) {
194                                 dput(ppd);
195                                 dput(pd);
196                                 if (err == -ENOENT)
197                                         /* some race between get_parent and
198                                          * get_name?  just try again
199                                          */
200                                         continue;
201                                 break;
202                         }
203                         dprintk("find_exported_dentry: found name: %s\n", nbuf);
204                         down(&ppd->d_inode->i_sem);
205                         npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
206                         up(&ppd->d_inode->i_sem);
207                         if (IS_ERR(npd)) {
208                                 err = PTR_ERR(npd);
209                                 dprintk("find_exported_dentry: lookup failed: %d\n", err);
210                                 dput(ppd);
211                                 dput(pd);
212                                 break;
213                         }
214                         /* we didn't really want npd, we really wanted
215                          * a side-effect of the lookup.
216                          * hopefully, npd == pd, though it isn't really
217                          * a problem if it isn't
218                          */
219                         if (npd == pd)
220                                 noprogress = 0;
221                         else
222                                 printk("find_exported_dentry: npd != pd\n");
223                         dput(npd);
224                         dput(ppd);
225                         if (IS_ROOT(pd)) {
226                                 /* something went wrong, we have to give up */
227                                 dput(pd);
228                                 break;
229                         }
230                 }
231                 dput(pd);
232         }
233
234         if (target_dir->d_flags & DCACHE_DISCONNECTED) {
235                 /* something went wrong - oh-well */
236                 if (!err)
237                         err = -ESTALE;
238                 goto err_target;
239         }
240         /* if we weren't after a directory, have one more step to go */
241         if (result != target_dir) {
242                 struct dentry *nresult;
243                 char nbuf[NAME_MAX+1];
244                 err = CALL(nops,get_name)(target_dir, nbuf, result);
245                 if (!err) {
246                         down(&target_dir->d_inode->i_sem);
247                         nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
248                         up(&target_dir->d_inode->i_sem);
249                         if (!IS_ERR(nresult)) {
250                                 if (nresult->d_inode) {
251                                         dput(result);
252                                         result = nresult;
253                                 } else
254                                         dput(nresult);
255                         }
256                 }
257         }
258         dput(target_dir);
259         /* now result is properly connected, it is our best bet */
260         if (acceptable(context, result))
261                 return result;
262         /* one last try of the aliases.. */
263         spin_lock(&dcache_lock);
264         toput = NULL;
265         head = &result->d_inode->i_dentry;
266         list_for_each(le, head) {
267                 struct dentry *dentry = list_entry(le, struct dentry, d_alias);
268                 dget_locked(dentry);
269                 spin_unlock(&dcache_lock);
270                 if (toput) dput(toput);
271                 if (dentry != result &&
272                     acceptable(context, dentry)) {
273                         dput(result);
274                         return dentry;
275                 }
276                 spin_lock(&dcache_lock);
277                 toput = dentry;
278         }
279         spin_unlock(&dcache_lock);
280         if (toput)
281                 dput(toput);
282
283         /* drat - I just cannot find anything acceptable */
284         dput(result);
285         return ERR_PTR(-ESTALE);
286
287  err_target:
288         dput(target_dir);
289  err_result:
290         dput(result);
291  err_out:
292         return ERR_PTR(err);
293 }
294
295
296
297 static struct dentry *get_parent(struct dentry *child)
298 {
299         /* get_parent cannot be supported generically, the locking
300          * is too icky.
301          * instead, we just return EACCES.  If server reboots or inodes
302          * get flushed, you lose
303          */
304         return ERR_PTR(-EACCES);
305 }
306
307
308 struct getdents_callback {
309         char *name;             /* name that was found. It already points to a
310                                    buffer NAME_MAX+1 is size */
311         unsigned long ino;      /* the inum we are looking for */
312         int found;              /* inode matched? */
313         int sequence;           /* sequence counter */
314 };
315
316 /*
317  * A rather strange filldir function to capture
318  * the name matching the specified inode number.
319  */
320 static int filldir_one(void * __buf, const char * name, int len,
321                         loff_t pos, ino_t ino, unsigned int d_type)
322 {
323         struct getdents_callback *buf = __buf;
324         int result = 0;
325
326         buf->sequence++;
327         if (buf->ino == ino) {
328                 memcpy(buf->name, name, len);
329                 buf->name[len] = '\0';
330                 buf->found = 1;
331                 result = -1;
332         }
333         return result;
334 }
335
336 /**
337  * get_name - default export_operations->get_name function
338  * @dentry: the directory in which to find a name
339  * @name:   a pointer to a %NAME_MAX+1 char buffer to store the name
340  * @child:  the dentry for the child directory.
341  *
342  * calls readdir on the parent until it finds an entry with
343  * the same inode number as the child, and returns that.
344  */
345 static int get_name(struct dentry *dentry, char *name,
346                         struct dentry *child)
347 {
348         struct inode *dir = dentry->d_inode;
349         int error;
350         struct file file;
351         struct getdents_callback buffer;
352
353         error = -ENOTDIR;
354         if (!dir || !S_ISDIR(dir->i_mode))
355                 goto out;
356         error = -EINVAL;
357         if (!dir->i_fop)
358                 goto out;
359         /*
360          * Open the directory ...
361          */
362         error = open_private_file(&file, dentry, O_RDONLY);
363         if (error)
364                 goto out;
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         close_private_file(&file);
391 out:
392         return error;
393 }
394
395
396 static struct dentry *export_iget(struct super_block *sb, unsigned long ino, __u32 generation)
397 {
398
399         /* iget isn't really right if the inode is currently unallocated!!
400          * This should really all be done inside each filesystem
401          *
402          * ext2fs' read_inode has been strengthed to return a bad_inode if
403          * the inode had been deleted.
404          *
405          * Currently we don't know the generation for parent directory, so
406          * a generation of 0 means "accept any"
407          */
408         struct inode *inode;
409         struct dentry *result;
410         if (ino == 0)
411                 return ERR_PTR(-ESTALE);
412         inode = iget(sb, ino);
413         if (inode == NULL)
414                 return ERR_PTR(-ENOMEM);
415         if (is_bad_inode(inode)
416             || (generation && inode->i_generation != generation)
417                 ) {
418                 /* we didn't find the right inode.. */
419                 dprintk("fh_verify: Inode %lu, Bad count: %d %d or version  %u %u\n",
420                         inode->i_ino,
421                         inode->i_nlink, atomic_read(&inode->i_count),
422                         inode->i_generation,
423                         generation);
424
425                 iput(inode);
426                 return ERR_PTR(-ESTALE);
427         }
428         /* now to find a dentry.
429          * If possible, get a well-connected one
430          */
431         result = d_alloc_anon(inode);
432         if (!result) {
433                 iput(inode);
434                 return ERR_PTR(-ENOMEM);
435         }
436         return result;
437 }
438
439
440 static struct dentry *get_object(struct super_block *sb, void *vobjp)
441 {
442         __u32 *objp = vobjp;
443         unsigned long ino = objp[0];
444         __u32 generation = objp[1];
445
446         return export_iget(sb, ino, generation);
447 }
448
449
450 /**
451  * export_encode_fh - default export_operations->encode_fh function
452  * @dentry:  the dentry to encode
453  * @fh:      where to store the file handle fragment
454  * @max_len: maximum length to store there
455  * @connectable: whether to store parent information
456  *
457  * This default encode_fh function assumes that the 32 inode number
458  * is suitable for locating an inode, and that the generation number
459  * can be used to check that it is still valid.  It places them in the
460  * filehandle fragment where export_decode_fh expects to find them.
461  */
462 static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
463                    int connectable)
464 {
465         struct inode * inode = dentry->d_inode;
466         int len = *max_len;
467         int type = 1;
468         
469         if (len < 2 || (connectable && len < 4))
470                 return 255;
471
472         len = 2;
473         fh[0] = inode->i_ino;
474         fh[1] = inode->i_generation;
475         if (connectable && !S_ISDIR(inode->i_mode)) {
476                 struct inode *parent;
477
478                 spin_lock(&dentry->d_lock);
479                 parent = dentry->d_parent->d_inode;
480                 fh[2] = parent->i_ino;
481                 fh[3] = parent->i_generation;
482                 spin_unlock(&dentry->d_lock);
483                 len = 4;
484                 type = 2;
485         }
486         *max_len = len;
487         return type;
488 }
489
490
491 /**
492  * export_decode_fh - default export_operations->decode_fh function
493  * @sb:  The superblock
494  * @fh:  pointer to the file handle fragment
495  * @fh_len: length of file handle fragment
496  * @acceptable: function for testing acceptability of dentrys
497  * @context:   context for @acceptable
498  *
499  * This is the default decode_fh() function.
500  * a fileid_type of 1 indicates that the filehandlefragment
501  * just contains an object identifier understood by  get_dentry.
502  * a fileid_type of 2 says that there is also a directory
503  * identifier 8 bytes in to the filehandlefragement.
504  */
505 static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
506                               int fileid_type,
507                          int (*acceptable)(void *context, struct dentry *de),
508                          void *context)
509 {
510         __u32 parent[2];
511         parent[0] = parent[1] = 0;
512         if (fh_len < 2 || fileid_type > 2)
513                 return NULL;
514         if (fileid_type == 2) {
515                 if (fh_len > 2) parent[0] = fh[2];
516                 if (fh_len > 3) parent[1] = fh[3];
517         }
518         return find_exported_dentry(sb, fh, parent,
519                                    acceptable, context);
520 }
521
522 struct export_operations export_op_default = {
523         .decode_fh      = export_decode_fh,
524         .encode_fh      = export_encode_fh,
525
526         .get_name       = get_name,
527         .get_parent     = get_parent,
528         .get_dentry     = get_object,
529 };
530
531 EXPORT_SYMBOL(export_op_default);
532 EXPORT_SYMBOL(find_exported_dentry);
533
534 MODULE_LICENSE("GPL");