v2.4.10.4 -> v2.4.10.5
[linux-flexiantxendom0-3.2.10.git] / fs / nfsd / vfs.c
1 #define MSNFS   /* HACK HACK */
2 /*
3  * linux/fs/nfsd/vfs.c
4  *
5  * File operations used by nfsd. Some of these have been ripped from
6  * other parts of the kernel because they weren't in ksyms.c, others
7  * are partial duplicates with added or changed functionality.
8  *
9  * Note that several functions dget() the dentry upon which they want
10  * to act, most notably those that create directory entries. Response
11  * dentry's are dput()'d if necessary in the release callback.
12  * So if you notice code paths that apparently fail to dput() the
13  * dentry, don't worry--they have been taken care of.
14  *
15  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
16  */
17
18 #include <linux/config.h>
19 #include <linux/version.h>
20 #include <linux/string.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/locks.h>
24 #include <linux/fs.h>
25 #include <linux/major.h>
26 #include <linux/ext2_fs.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stat.h>
29 #include <linux/fcntl.h>
30 #include <linux/net.h>
31 #include <linux/unistd.h>
32 #include <linux/slab.h>
33 #include <linux/in.h>
34 #define __NO_VERSION__
35 #include <linux/module.h>
36
37 #include <linux/sunrpc/svc.h>
38 #include <linux/nfsd/nfsd.h>
39 #ifdef CONFIG_NFSD_V3
40 #include <linux/nfs3.h>
41 #include <linux/nfsd/xdr3.h>
42 #endif /* CONFIG_NFSD_V3 */
43 #include <linux/nfsd/nfsfh.h>
44 #include <linux/quotaops.h>
45
46 #include <asm/uaccess.h>
47
48 #define NFSDDBG_FACILITY                NFSDDBG_FILEOP
49 #define NFSD_PARANOIA
50
51
52 /* We must ignore files (but only files) which might have mandatory
53  * locks on them because there is no way to know if the accesser has
54  * the lock.
55  */
56 #define IS_ISMNDLK(i)   (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
57
58 /*
59  * This is a cache of readahead params that help us choose the proper
60  * readahead strategy. Initially, we set all readahead parameters to 0
61  * and let the VFS handle things.
62  * If you increase the number of cached files very much, you'll need to
63  * add a hash table here.
64  */
65 struct raparms {
66         struct raparms          *p_next;
67         unsigned int            p_count;
68         ino_t                   p_ino;
69         dev_t                   p_dev;
70         unsigned long           p_reada,
71                                 p_ramax,
72                                 p_raend,
73                                 p_ralen,
74                                 p_rawin;
75 };
76
77 static struct raparms *         raparml;
78 static struct raparms *         raparm_cache;
79
80 /*
81  * Look up one component of a pathname.
82  * N.B. After this call _both_ fhp and resfh need an fh_put
83  *
84  * If the lookup would cross a mountpoint, and the mounted filesystem
85  * is exported to the client with NFSEXP_CROSSMNT, then the lookup is
86  * accepted as it stands and the mounted directory is
87  * returned. Otherwise the covered directory is returned.
88  * NOTE: this mountpoint crossing is not supported properly by all
89  *   clients and is explicitly disallowed for NFSv3
90  *      NeilBrown <neilb@cse.unsw.edu.au>
91  */
92 int
93 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
94                                         int len, struct svc_fh *resfh)
95 {
96         struct svc_export       *exp;
97         struct dentry           *dparent;
98         struct dentry           *dentry;
99         int                     err;
100
101         dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
102
103         /* Obtain dentry and export. */
104         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
105         if (err)
106                 goto out;
107
108         dparent = fhp->fh_dentry;
109         exp  = fhp->fh_export;
110
111         err = nfserr_acces;
112
113         /* Lookup the name, but don't follow links */
114         if (isdotent(name, len)) {
115                 if (len==1)
116                         dentry = dget(dparent);
117                 else  { /* must be ".." */
118                         /* checking mountpoint crossing is very different when stepping up */
119                         if (dparent == exp->ex_dentry) {
120                                 if (!EX_CROSSMNT(exp))
121                                         dentry = dget(dparent); /* .. == . just like at / */
122                                 else
123                                 {
124                                         struct svc_export *exp2 = NULL;
125                                         struct dentry *dp;
126                                         struct vfsmount *mnt = mntget(exp->ex_mnt);
127                                         dentry = dget(dparent);
128                                         while(follow_up(&mnt, &dentry))
129                                                 ;
130                                         dp = dget(dentry->d_parent);
131                                         dput(dentry);
132                                         dentry = dp;
133                                         for ( ; exp2 == NULL && dp->d_parent != dp;
134                                               dp=dp->d_parent)
135                                                 exp2 = exp_get(exp->ex_client, dp->d_inode->i_dev, dp->d_inode->i_ino);
136                                         if (exp2==NULL) {
137                                                 dput(dentry);
138                                                 dentry = dget(dparent);
139                                         } else {
140                                                 exp = exp2;
141                                         }
142                                         mntput(mnt);
143                                 }
144                         } else
145                                 dentry = dget(dparent->d_parent);
146                 }
147         } else {
148                 fh_lock(fhp);
149                 dentry = lookup_one_len(name, dparent, len);
150                 err = PTR_ERR(dentry);
151                 if (IS_ERR(dentry))
152                         goto out_nfserr;
153                 /*
154                  * check if we have crossed a mount point ...
155                  */
156                 if (d_mountpoint(dentry)) {
157                         struct svc_export *exp2 = NULL;
158                         struct vfsmount *mnt = mntget(exp->ex_mnt);
159                         struct dentry *mounts = dget(dentry);
160                         while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts))
161                                 ;
162                         exp2 = exp_get(rqstp->rq_client,
163                                        mounts->d_inode->i_dev,
164                                        mounts->d_inode->i_ino);
165                         if (exp2 && EX_CROSSMNT(exp2)) {
166                                 /* successfully crossed mount point */
167                                 exp = exp2;
168                                 dput(dentry);
169                                 dentry = mounts;
170                         } else
171                                 dput(mounts);
172                         mntput(mnt);
173                 }
174         }
175         /*
176          * Note: we compose the file handle now, but as the
177          * dentry may be negative, it may need to be updated.
178          */
179         err = fh_compose(resfh, exp, dentry, fhp);
180         if (!err && !dentry->d_inode)
181                 err = nfserr_noent;
182 out:
183         return err;
184
185 out_nfserr:
186         err = nfserrno(err);
187         goto out;
188 }
189
190 /*
191  * Set various file attributes.
192  * N.B. After this call fhp needs an fh_put
193  */
194 int
195 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
196              int check_guard, time_t guardtime)
197 {
198         struct dentry   *dentry;
199         struct inode    *inode;
200         int             accmode = MAY_SATTR;
201         int             ftype = 0;
202         int             imode;
203         int             err;
204         int             size_change = 0;
205
206         if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
207                 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
208         if (iap->ia_valid & ATTR_SIZE)
209                 ftype = S_IFREG;
210
211         /* Get inode */
212         err = fh_verify(rqstp, fhp, ftype, accmode);
213         if (err || !iap->ia_valid)
214                 goto out;
215
216         dentry = fhp->fh_dentry;
217         inode = dentry->d_inode;
218
219         err = inode_change_ok(inode, iap);
220         /* could be a "touch" (utimes) request where the user is not the owner but does
221          * have write permission. In this case the user should be allowed to set
222          * both times to the current time.  We could just assume any such SETATTR
223          * is intended to set the times to "now", but we do a couple of simple tests
224          * to increase our confidence.
225          */
226 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
227 #define MAX_TOUCH_TIME_ERROR (30*60)
228         if (err
229             && (iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
230             && iap->ia_mtime == iap->ia_atime
231             ) {
232             /* looks good.  now just make sure time is in the right ballpark.
233              * solaris, at least, doesn't seem to care what the time request is
234              */
235             time_t delta = iap->ia_atime - CURRENT_TIME;
236             if (delta<0) delta = -delta;
237             if (delta < MAX_TOUCH_TIME_ERROR) {
238                 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
239                  * this will cause notify_change to set these times to "now"
240                  */
241                 iap->ia_valid &= ~BOTH_TIME_SET;
242                 err = inode_change_ok(inode, iap);
243             }
244         }
245             
246         if (err)
247                 goto out_nfserr;
248
249         /* The size case is special. It changes the file as well as the attributes.  */
250         if (iap->ia_valid & ATTR_SIZE) {
251                 if (iap->ia_size < inode->i_size) {
252                         err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
253                         if (err)
254                                 goto out;
255                 }
256
257                 /*
258                  * If we are changing the size of the file, then
259                  * we need to break all leases.
260                  */
261                 err = get_lease(inode, FMODE_WRITE);
262                 if (err)
263                         goto out_nfserr;
264
265                 err = get_write_access(inode);
266                 if (err)
267                         goto out_nfserr;
268
269                 err = locks_verify_truncate(inode, NULL, iap->ia_size);
270                 if (err) {
271                         put_write_access(inode);
272                         goto out_nfserr;
273                 }
274                 DQUOT_INIT(inode);
275         }
276
277         imode = inode->i_mode;
278         if (iap->ia_valid & ATTR_MODE) {
279                 iap->ia_mode &= S_IALLUGO;
280                 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
281         }
282
283         /* Revoke setuid/setgid bit on chown/chgrp */
284         if ((iap->ia_valid & ATTR_UID) && (imode & S_ISUID)
285          && iap->ia_uid != inode->i_uid) {
286                 iap->ia_valid |= ATTR_MODE;
287                 iap->ia_mode = imode &= ~S_ISUID;
288         }
289         if ((iap->ia_valid & ATTR_GID) && (imode & S_ISGID)
290          && iap->ia_gid != inode->i_gid) {
291                 iap->ia_valid |= ATTR_MODE;
292                 iap->ia_mode = imode &= ~S_ISGID;
293         }
294
295         /* Change the attributes. */
296
297
298         iap->ia_valid |= ATTR_CTIME;
299
300         if (iap->ia_valid & ATTR_SIZE) {
301                 fh_lock(fhp);
302                 size_change = 1;
303         }
304         err = nfserr_notsync;
305         if (!check_guard || guardtime == inode->i_ctime) {
306                 err = notify_change(dentry, iap);
307                 err = nfserrno(err);
308         }
309         if (size_change) {
310                 fh_unlock(fhp);
311                 put_write_access(inode);
312         }
313         if (!err)
314                 if (EX_ISSYNC(fhp->fh_export))
315                         write_inode_now(inode, 1);
316 out:
317         return err;
318
319 out_nfserr:
320         err = nfserrno(err);
321         goto out;
322 }
323
324 #ifdef CONFIG_NFSD_V3
325 /*
326  * Check server access rights to a file system object
327  */
328 struct accessmap {
329         u32             access;
330         int             how;
331 };
332 static struct accessmap nfs3_regaccess[] = {
333     {   NFS3_ACCESS_READ,       MAY_READ                        },
334     {   NFS3_ACCESS_EXECUTE,    MAY_EXEC                        },
335     {   NFS3_ACCESS_MODIFY,     MAY_WRITE|MAY_TRUNC             },
336     {   NFS3_ACCESS_EXTEND,     MAY_WRITE                       },
337
338     {   0,                      0                               }
339 };
340
341 static struct accessmap nfs3_diraccess[] = {
342     {   NFS3_ACCESS_READ,       MAY_READ                        },
343     {   NFS3_ACCESS_LOOKUP,     MAY_EXEC                        },
344     {   NFS3_ACCESS_MODIFY,     MAY_EXEC|MAY_WRITE|MAY_TRUNC    },
345     {   NFS3_ACCESS_EXTEND,     MAY_EXEC|MAY_WRITE              },
346     {   NFS3_ACCESS_DELETE,     MAY_REMOVE                      },
347
348     {   0,                      0                               }
349 };
350
351 static struct accessmap nfs3_anyaccess[] = {
352         /* Some clients - Solaris 2.6 at least, make an access call
353          * to the server to check for access for things like /dev/null
354          * (which really, the server doesn't care about).  So
355          * We provide simple access checking for them, looking
356          * mainly at mode bits
357          */
358     {   NFS3_ACCESS_READ,       MAY_READ                        },
359     {   NFS3_ACCESS_EXECUTE,    MAY_EXEC                        },
360     {   NFS3_ACCESS_MODIFY,     MAY_WRITE                       },
361     {   NFS3_ACCESS_EXTEND,     MAY_WRITE                       },
362
363     {   0,                      0                               }
364 };
365
366 int
367 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access)
368 {
369         struct accessmap        *map;
370         struct svc_export       *export;
371         struct dentry           *dentry;
372         u32                     query, result = 0;
373         unsigned int            error;
374
375         error = fh_verify(rqstp, fhp, 0, MAY_NOP);
376         if (error)
377                 goto out;
378
379         export = fhp->fh_export;
380         dentry = fhp->fh_dentry;
381
382         if (S_ISREG(dentry->d_inode->i_mode))
383                 map = nfs3_regaccess;
384         else if (S_ISDIR(dentry->d_inode->i_mode))
385                 map = nfs3_diraccess;
386         else
387                 map = nfs3_anyaccess;
388
389
390         query = *access;
391         for  (; map->access; map++) {
392                 if (map->access & query) {
393                         unsigned int err2;
394                         err2 = nfsd_permission(export, dentry, map->how);
395                         switch (err2) {
396                         case nfs_ok:
397                                 result |= map->access;
398                                 break;
399                                 
400                         /* the following error codes just mean the access was not allowed,
401                          * rather than an error occurred */
402                         case nfserr_rofs:
403                         case nfserr_acces:
404                         case nfserr_perm:
405                                 /* simply don't "or" in the access bit. */
406                                 break;
407                         default:
408                                 error = err2;
409                                 goto out;
410                         }
411                 }
412         }
413         *access = result;
414
415  out:
416         return error;
417 }
418 #endif /* CONFIG_NFSD_V3 */
419
420
421
422 /*
423  * Open an existing file or directory.
424  * The access argument indicates the type of open (read/write/lock)
425  * N.B. After this call fhp needs an fh_put
426  */
427 int
428 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
429                         int access, struct file *filp)
430 {
431         struct dentry   *dentry;
432         struct inode    *inode;
433         int             err;
434
435         /* If we get here, then the client has already done an "open", and (hopefully)
436          * checked permission - so allow OWNER_OVERRIDE in case a chmod has now revoked
437          * permission */
438         err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
439         if (err)
440                 goto out;
441
442         dentry = fhp->fh_dentry;
443         inode = dentry->d_inode;
444
445         /* Disallow access to files with the append-only bit set or
446          * with mandatory locking enabled
447          */
448         err = nfserr_perm;
449         if (IS_APPEND(inode) || IS_ISMNDLK(inode))
450                 goto out;
451         if (!inode->i_fop)
452                 goto out;
453
454         /*
455          * Check to see if there are any leases on this file.
456          * This may block while leases are broken.
457          */
458         err = get_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
459         if (err)
460                 goto out_nfserr;
461
462         if ((access & MAY_WRITE) && (err = get_write_access(inode)) != 0)
463                 goto out_nfserr;
464
465         memset(filp, 0, sizeof(*filp));
466         filp->f_op    = fops_get(inode->i_fop);
467         atomic_set(&filp->f_count, 1);
468         filp->f_dentry = dentry;
469         filp->f_vfsmnt = fhp->fh_export->ex_mnt;
470         if (access & MAY_WRITE) {
471                 filp->f_flags = O_WRONLY|O_LARGEFILE;
472                 filp->f_mode  = FMODE_WRITE;
473                 DQUOT_INIT(inode);
474         } else {
475                 filp->f_flags = O_RDONLY|O_LARGEFILE;
476                 filp->f_mode  = FMODE_READ;
477         }
478
479         err = 0;
480         if (filp->f_op && filp->f_op->open) {
481                 err = filp->f_op->open(inode, filp);
482                 if (err) {
483                         fops_put(filp->f_op);
484                         if (access & MAY_WRITE)
485                                 put_write_access(inode);
486
487                         /* I nearly added put_filp() call here, but this filp
488                          * is really on callers stack frame. -DaveM
489                          */
490                         atomic_dec(&filp->f_count);
491                 }
492         }
493 out_nfserr:
494         if (err)
495                 err = nfserrno(err);
496 out:
497         return err;
498 }
499
500 /*
501  * Close a file.
502  */
503 void
504 nfsd_close(struct file *filp)
505 {
506         struct dentry   *dentry = filp->f_dentry;
507         struct inode    *inode = dentry->d_inode;
508
509         if (filp->f_op && filp->f_op->release)
510                 filp->f_op->release(inode, filp);
511         fops_put(filp->f_op);
512         if (filp->f_mode & FMODE_WRITE)
513                 put_write_access(inode);
514 }
515
516 /*
517  * Sync a file
518  * As this calls fsync (not fdatasync) there is no need for a write_inode
519  * after it.
520  */
521 void
522 nfsd_sync(struct file *filp)
523 {
524         dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
525         down(&filp->f_dentry->d_inode->i_sem);
526         filp->f_op->fsync(filp, filp->f_dentry, 0);
527         up(&filp->f_dentry->d_inode->i_sem);
528 }
529
530 void
531 nfsd_sync_dir(struct dentry *dp)
532 {
533         struct inode *inode = dp->d_inode;
534         int (*fsync) (struct file *, struct dentry *, int);
535         
536         if (inode->i_fop && (fsync = inode->i_fop->fsync)) {
537                 fsync(NULL, dp, 0);
538         }
539 }
540
541 /*
542  * Obtain the readahead parameters for the file
543  * specified by (dev, ino).
544  */
545 static inline struct raparms *
546 nfsd_get_raparms(dev_t dev, ino_t ino)
547 {
548         struct raparms  *ra, **rap, **frap = NULL;
549         int depth = 0;
550         
551         for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
552                 if (ra->p_ino == ino && ra->p_dev == dev)
553                         goto found;
554                 depth++;
555                 if (ra->p_count == 0)
556                         frap = rap;
557         }
558         depth = nfsdstats.ra_size*11/10;
559         if (!frap)
560                 return NULL;
561         rap = frap;
562         ra = *frap;
563         memset(ra, 0, sizeof(*ra));
564         ra->p_dev = dev;
565         ra->p_ino = ino;
566 found:
567         if (rap != &raparm_cache) {
568                 *rap = ra->p_next;
569                 ra->p_next   = raparm_cache;
570                 raparm_cache = ra;
571         }
572         ra->p_count++;
573         nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
574         return ra;
575 }
576
577 /*
578  * Read data from a file. count must contain the requested read count
579  * on entry. On return, *count contains the number of bytes actually read.
580  * N.B. After this call fhp needs an fh_put
581  */
582 int
583 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
584           char *buf, unsigned long *count)
585 {
586         struct raparms  *ra;
587         mm_segment_t    oldfs;
588         int             err;
589         struct file     file;
590
591         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
592         if (err)
593                 goto out;
594         err = nfserr_perm;
595         if (!file.f_op->read)
596                 goto out_close;
597 #ifdef MSNFS
598         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
599                 (!lock_may_read(file.f_dentry->d_inode, offset, *count)))
600                 goto out_close;
601 #endif
602
603         /* Get readahead parameters */
604         ra = nfsd_get_raparms(fhp->fh_export->ex_dev, fhp->fh_dentry->d_inode->i_ino);
605         if (ra) {
606                 file.f_reada = ra->p_reada;
607                 file.f_ramax = ra->p_ramax;
608                 file.f_raend = ra->p_raend;
609                 file.f_ralen = ra->p_ralen;
610                 file.f_rawin = ra->p_rawin;
611         }
612         file.f_pos = offset;
613
614         oldfs = get_fs(); set_fs(KERNEL_DS);
615         err = file.f_op->read(&file, buf, *count, &file.f_pos);
616         set_fs(oldfs);
617
618         /* Write back readahead params */
619         if (ra != NULL) {
620                 dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
621                         file.f_reada, file.f_ramax, file.f_raend,
622                         file.f_ralen, file.f_rawin);
623                 ra->p_reada = file.f_reada;
624                 ra->p_ramax = file.f_ramax;
625                 ra->p_raend = file.f_raend;
626                 ra->p_ralen = file.f_ralen;
627                 ra->p_rawin = file.f_rawin;
628                 ra->p_count -= 1;
629         }
630
631         if (err >= 0) {
632                 nfsdstats.io_read += err;
633                 *count = err;
634                 err = 0;
635         } else 
636                 err = nfserrno(err);
637 out_close:
638         nfsd_close(&file);
639 out:
640         return err;
641 }
642
643 /*
644  * Write data to a file.
645  * The stable flag requests synchronous writes.
646  * N.B. After this call fhp needs an fh_put
647  */
648 int
649 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
650                                 char *buf, unsigned long cnt, int *stablep)
651 {
652         struct svc_export       *exp;
653         struct file             file;
654         struct dentry           *dentry;
655         struct inode            *inode;
656         mm_segment_t            oldfs;
657         int                     err = 0;
658         int                     stable = *stablep;
659
660         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
661         if (err)
662                 goto out;
663         if (!cnt)
664                 goto out_close;
665         err = nfserr_perm;
666         if (!file.f_op->write)
667                 goto out_close;
668 #ifdef MSNFS
669         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
670                 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
671                 goto out_close;
672 #endif
673
674         dentry = file.f_dentry;
675         inode = dentry->d_inode;
676         exp   = fhp->fh_export;
677
678         /*
679          * Request sync writes if
680          *  -   the sync export option has been set, or
681          *  -   the client requested O_SYNC behavior (NFSv3 feature).
682          *  -   The file system doesn't support fsync().
683          * When gathered writes have been configured for this volume,
684          * flushing the data to disk is handled separately below.
685          */
686
687         if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
688                stable = 2;
689                *stablep = 2; /* FILE_SYNC */
690         }
691
692         if (!EX_ISSYNC(exp))
693                 stable = 0;
694         if (stable && !EX_WGATHER(exp))
695                 file.f_flags |= O_SYNC;
696
697         file.f_pos = offset;            /* set write offset */
698
699         /* Write the data. */
700         oldfs = get_fs(); set_fs(KERNEL_DS);
701         err = file.f_op->write(&file, buf, cnt, &file.f_pos);
702         if (err >= 0)
703                 nfsdstats.io_write += cnt;
704         set_fs(oldfs);
705
706         /* clear setuid/setgid flag after write */
707         if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
708                 struct iattr    ia;
709
710                 ia.ia_valid = ATTR_MODE;
711                 ia.ia_mode  = inode->i_mode & ~(S_ISUID | S_ISGID);
712                 notify_change(dentry, &ia);
713         }
714
715         if (err >= 0 && stable) {
716                 static unsigned long    last_ino;
717                 static kdev_t           last_dev = NODEV;
718
719                 /*
720                  * Gathered writes: If another process is currently
721                  * writing to the file, there's a high chance
722                  * this is another nfsd (triggered by a bulk write
723                  * from a client's biod). Rather than syncing the
724                  * file with each write request, we sleep for 10 msec.
725                  *
726                  * I don't know if this roughly approximates
727                  * C. Juszak's idea of gathered writes, but it's a
728                  * nice and simple solution (IMHO), and it seems to
729                  * work:-)
730                  */
731                 if (EX_WGATHER(exp)) {
732                         if (atomic_read(&inode->i_writecount) > 1
733                             || (last_ino == inode->i_ino && last_dev == inode->i_dev)) {
734                                 dprintk("nfsd: write defer %d\n", current->pid);
735                                 set_current_state(TASK_UNINTERRUPTIBLE);
736                                 schedule_timeout((HZ+99)/100);
737                                 current->state = TASK_RUNNING;
738                                 dprintk("nfsd: write resume %d\n", current->pid);
739                         }
740
741                         if (inode->i_state & I_DIRTY) {
742                                 dprintk("nfsd: write sync %d\n", current->pid);
743                                 nfsd_sync(&file);
744                         }
745 #if 0
746                         wake_up(&inode->i_wait);
747 #endif
748                 }
749                 last_ino = inode->i_ino;
750                 last_dev = inode->i_dev;
751         }
752
753         dprintk("nfsd: write complete err=%d\n", err);
754         if (err >= 0)
755                 err = 0;
756         else 
757                 err = nfserrno(err);
758 out_close:
759         nfsd_close(&file);
760 out:
761         return err;
762 }
763
764
765 #ifdef CONFIG_NFSD_V3
766 /*
767  * Commit all pending writes to stable storage.
768  * Strictly speaking, we could sync just the indicated file region here,
769  * but there's currently no way we can ask the VFS to do so.
770  *
771  * Unfortunately we cannot lock the file to make sure we return full WCC
772  * data to the client, as locking happens lower down in the filesystem.
773  */
774 int
775 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
776                off_t offset, unsigned long count)
777 {
778         struct file     file;
779         int             err;
780
781         if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
782                 return err;
783         if (EX_ISSYNC(fhp->fh_export)) {
784                 if (file.f_op && file.f_op->fsync) {
785                         nfsd_sync(&file);
786                 } else {
787                         err = nfserr_notsupp;
788                 }
789         }
790
791         nfsd_close(&file);
792         return err;
793 }
794 #endif /* CONFIG_NFSD_V3 */
795
796 /*
797  * Create a file (regular, directory, device, fifo); UNIX sockets 
798  * not yet implemented.
799  * If the response fh has been verified, the parent directory should
800  * already be locked. Note that the parent directory is left locked.
801  *
802  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
803  */
804 int
805 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
806                 char *fname, int flen, struct iattr *iap,
807                 int type, dev_t rdev, struct svc_fh *resfhp)
808 {
809         struct dentry   *dentry, *dchild;
810         struct inode    *dirp;
811         int             err;
812
813         err = nfserr_perm;
814         if (!flen)
815                 goto out;
816         err = nfserr_exist;
817         if (isdotent(fname, flen))
818                 goto out;
819
820         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
821         if (err)
822                 goto out;
823
824         dentry = fhp->fh_dentry;
825         dirp = dentry->d_inode;
826
827         err = nfserr_notdir;
828         if(!dirp->i_op || !dirp->i_op->lookup)
829                 goto out;
830         /*
831          * Check whether the response file handle has been verified yet.
832          * If it has, the parent directory should already be locked.
833          */
834         if (!resfhp->fh_dentry) {
835                 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
836                 fh_lock(fhp);
837                 dchild = lookup_one_len(fname, dentry, flen);
838                 err = PTR_ERR(dchild);
839                 if (IS_ERR(dchild))
840                         goto out_nfserr;
841                 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
842                 if (err)
843                         goto out;
844         } else {
845                 /* called from nfsd_proc_create */
846                 dchild = resfhp->fh_dentry;
847                 if (!fhp->fh_locked) {
848                         /* not actually possible */
849                         printk(KERN_ERR
850                                 "nfsd_create: parent %s/%s not locked!\n",
851                                 dentry->d_parent->d_name.name,
852                                 dentry->d_name.name);
853                         err = -EIO;
854                         goto out;
855                 }
856         }
857         /*
858          * Make sure the child dentry is still negative ...
859          */
860         err = nfserr_exist;
861         if (dchild->d_inode) {
862                 dprintk("nfsd_create: dentry %s/%s not negative!\n",
863                         dentry->d_name.name, dchild->d_name.name);
864                 goto out; 
865         }
866
867         if (!(iap->ia_valid & ATTR_MODE))
868                 iap->ia_mode = 0;
869         iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
870
871         /*
872          * Get the dir op function pointer.
873          */
874         err = nfserr_perm;
875         switch (type) {
876         case S_IFREG:
877                 err = vfs_create(dirp, dchild, iap->ia_mode);
878                 break;
879         case S_IFDIR:
880                 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
881                 break;
882         case S_IFCHR:
883         case S_IFBLK:
884         case S_IFIFO:
885         case S_IFSOCK:
886                 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
887                 break;
888         default:
889                 printk("nfsd: bad file type %o in nfsd_create\n", type);
890                 err = -EINVAL;
891         }
892         if (err < 0)
893                 goto out_nfserr;
894
895         if (EX_ISSYNC(fhp->fh_export)) {
896                 nfsd_sync_dir(dentry);
897                 write_inode_now(dchild->d_inode, 1);
898         }
899
900
901         /* Set file attributes. Mode has already been set and
902          * setting uid/gid works only for root. Irix appears to
903          * send along the gid when it tries to implement setgid
904          * directories via NFS.
905          */
906         err = 0;
907         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
908                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
909         /*
910          * Update the file handle to get the new inode info.
911          */
912         if (!err)
913                 err = fh_update(resfhp);
914 out:
915         return err;
916
917 out_nfserr:
918         err = nfserrno(err);
919         goto out;
920 }
921
922 #ifdef CONFIG_NFSD_V3
923 /*
924  * NFSv3 version of nfsd_create
925  */
926 int
927 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
928                 char *fname, int flen, struct iattr *iap,
929                 struct svc_fh *resfhp, int createmode, u32 *verifier)
930 {
931         struct dentry   *dentry, *dchild;
932         struct inode    *dirp;
933         int             err;
934         __u32           v_mtime=0, v_atime=0;
935         int             v_mode=0;
936
937         err = nfserr_perm;
938         if (!flen)
939                 goto out;
940         err = nfserr_exist;
941         if (isdotent(fname, flen))
942                 goto out;
943         if (!(iap->ia_valid & ATTR_MODE))
944                 iap->ia_mode = 0;
945         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
946         if (err)
947                 goto out;
948
949         dentry = fhp->fh_dentry;
950         dirp = dentry->d_inode;
951
952         /* Get all the sanity checks out of the way before
953          * we lock the parent. */
954         err = nfserr_notdir;
955         if(!dirp->i_op || !dirp->i_op->lookup)
956                 goto out;
957         fh_lock(fhp);
958
959         /*
960          * Compose the response file handle.
961          */
962         dchild = lookup_one_len(fname, dentry, flen);
963         err = PTR_ERR(dchild);
964         if (IS_ERR(dchild))
965                 goto out_nfserr;
966
967         err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
968         if (err)
969                 goto out;
970
971         if (createmode == NFS3_CREATE_EXCLUSIVE) {
972                 /* while the verifier would fit in mtime+atime,
973                  * solaris7 gets confused (bugid 4218508) if these have
974                  * the high bit set, so we use the mode as well
975                  */
976                 v_mtime = verifier[0]&0x7fffffff;
977                 v_atime = verifier[1]&0x7fffffff;
978                 v_mode  = S_IFREG
979                         | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
980                         | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
981                         ;
982         }
983         
984         if (dchild->d_inode) {
985                 err = 0;
986
987                 switch (createmode) {
988                 case NFS3_CREATE_UNCHECKED:
989                         if (! S_ISREG(dchild->d_inode->i_mode))
990                                 err = nfserr_exist;
991                         else {
992                                 iap->ia_valid &= ATTR_SIZE;
993                                 goto set_attr;
994                         }
995                         break;
996                 case NFS3_CREATE_EXCLUSIVE:
997                         if (   dchild->d_inode->i_mtime == v_mtime
998                             && dchild->d_inode->i_atime == v_atime
999                             && dchild->d_inode->i_mode  == v_mode
1000                             && dchild->d_inode->i_size  == 0 )
1001                                 break;
1002                          /* fallthru */
1003                 case NFS3_CREATE_GUARDED:
1004                         err = nfserr_exist;
1005                 }
1006                 goto out;
1007         }
1008
1009         err = vfs_create(dirp, dchild, iap->ia_mode);
1010         if (err < 0)
1011                 goto out_nfserr;
1012
1013         if (EX_ISSYNC(fhp->fh_export)) {
1014                 nfsd_sync_dir(dentry);
1015                 /* setattr will sync the child (or not) */
1016         }
1017
1018         /*
1019          * Update the filehandle to get the new inode info.
1020          */
1021         err = fh_update(resfhp);
1022         if (err)
1023                 goto out;
1024
1025         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1026                 /* Cram the verifier into atime/mtime/mode */
1027                 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1028                         | ATTR_MTIME_SET|ATTR_ATIME_SET
1029                         | ATTR_MODE;
1030                 iap->ia_mtime = v_mtime;
1031                 iap->ia_atime = v_atime;
1032                 iap->ia_mode  = v_mode;
1033         }
1034
1035         /* Set file attributes.
1036          * Mode has already been set but we might need to reset it
1037          * for CREATE_EXCLUSIVE
1038          * Irix appears to send along the gid when it tries to
1039          * implement setgid directories via NFS. Clear out all that cruft.
1040          */
1041  set_attr:
1042         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1043                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1044
1045  out:
1046         fh_unlock(fhp);
1047         return err;
1048  
1049  out_nfserr:
1050         err = nfserrno(err);
1051         goto out;
1052 }
1053 #endif /* CONFIG_NFSD_V3 */
1054
1055 /*
1056  * Read a symlink. On entry, *lenp must contain the maximum path length that
1057  * fits into the buffer. On return, it contains the true length.
1058  * N.B. After this call fhp needs an fh_put
1059  */
1060 int
1061 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1062 {
1063         struct dentry   *dentry;
1064         struct inode    *inode;
1065         mm_segment_t    oldfs;
1066         int             err;
1067
1068         err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1069         if (err)
1070                 goto out;
1071
1072         dentry = fhp->fh_dentry;
1073         inode = dentry->d_inode;
1074
1075         err = nfserr_inval;
1076         if (!inode->i_op || !inode->i_op->readlink)
1077                 goto out;
1078
1079         UPDATE_ATIME(inode);
1080         /* N.B. Why does this call need a get_fs()??
1081          * Remove the set_fs and watch the fireworks:-) --okir
1082          */
1083
1084         oldfs = get_fs(); set_fs(KERNEL_DS);
1085         err = inode->i_op->readlink(dentry, buf, *lenp);
1086         set_fs(oldfs);
1087
1088         if (err < 0)
1089                 goto out_nfserr;
1090         *lenp = err;
1091         err = 0;
1092 out:
1093         return err;
1094
1095 out_nfserr:
1096         err = nfserrno(err);
1097         goto out;
1098 }
1099
1100 /*
1101  * Create a symlink and look up its inode
1102  * N.B. After this call _both_ fhp and resfhp need an fh_put
1103  */
1104 int
1105 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1106                                 char *fname, int flen,
1107                                 char *path,  int plen,
1108                                 struct svc_fh *resfhp,
1109                                 struct iattr *iap)
1110 {
1111         struct dentry   *dentry, *dnew;
1112         int             err, cerr;
1113
1114         err = nfserr_noent;
1115         if (!flen || !plen)
1116                 goto out;
1117         err = nfserr_exist;
1118         if (isdotent(fname, flen))
1119                 goto out;
1120
1121         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1122         if (err)
1123                 goto out;
1124         fh_lock(fhp);
1125         dentry = fhp->fh_dentry;
1126         dnew = lookup_one_len(fname, dentry, flen);
1127         err = PTR_ERR(dnew);
1128         if (IS_ERR(dnew))
1129                 goto out_nfserr;
1130
1131         err = vfs_symlink(dentry->d_inode, dnew, path);
1132         if (!err) {
1133                 if (EX_ISSYNC(fhp->fh_export))
1134                         nfsd_sync_dir(dentry);
1135                 if (iap) {
1136                         iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1137                         if (iap->ia_valid) {
1138                                 iap->ia_valid |= ATTR_CTIME;
1139                                 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1140                                         | S_IFLNK;
1141                                 err = notify_change(dnew, iap);
1142                                 if (!err && EX_ISSYNC(fhp->fh_export))
1143                                         write_inode_now(dentry->d_inode, 1);
1144                        }
1145                 }
1146         } else
1147                 err = nfserrno(err);
1148         fh_unlock(fhp);
1149
1150         /* Compose the fh so the dentry will be freed ... */
1151         cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1152         if (err==0) err = cerr;
1153 out:
1154         return err;
1155
1156 out_nfserr:
1157         err = nfserrno(err);
1158         goto out;
1159 }
1160
1161 /*
1162  * Create a hardlink
1163  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1164  */
1165 int
1166 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1167                                 char *name, int len, struct svc_fh *tfhp)
1168 {
1169         struct dentry   *ddir, *dnew, *dold;
1170         struct inode    *dirp, *dest;
1171         int             err;
1172
1173         err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1174         if (err)
1175                 goto out;
1176         err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1177         if (err)
1178                 goto out;
1179
1180         err = nfserr_perm;
1181         if (!len)
1182                 goto out;
1183         err = nfserr_exist;
1184         if (isdotent(name, len))
1185                 goto out;
1186
1187         fh_lock(ffhp);
1188         ddir = ffhp->fh_dentry;
1189         dirp = ddir->d_inode;
1190
1191         dnew = lookup_one_len(name, ddir, len);
1192         err = PTR_ERR(dnew);
1193         if (IS_ERR(dnew))
1194                 goto out_nfserr;
1195
1196         dold = tfhp->fh_dentry;
1197         dest = dold->d_inode;
1198
1199         err = vfs_link(dold, dirp, dnew);
1200         if (!err) {
1201                 if (EX_ISSYNC(ffhp->fh_export)) {
1202                         nfsd_sync_dir(ddir);
1203                         write_inode_now(dest, 1);
1204                 }
1205         } else {
1206                 if (err == -EXDEV && rqstp->rq_vers == 2)
1207                         err = nfserr_acces;
1208                 else
1209                         err = nfserrno(err);
1210         }
1211
1212         fh_unlock(ffhp);
1213         dput(dnew);
1214 out:
1215         return err;
1216
1217 out_nfserr:
1218         err = nfserrno(err);
1219         goto out;
1220 }
1221
1222 /*
1223  * Rename a file
1224  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1225  */
1226 int
1227 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1228                             struct svc_fh *tfhp, char *tname, int tlen)
1229 {
1230         struct dentry   *fdentry, *tdentry, *odentry, *ndentry;
1231         struct inode    *fdir, *tdir;
1232         int             err;
1233
1234         err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1235         if (err)
1236                 goto out;
1237         err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1238         if (err)
1239                 goto out;
1240
1241         fdentry = ffhp->fh_dentry;
1242         fdir = fdentry->d_inode;
1243
1244         tdentry = tfhp->fh_dentry;
1245         tdir = tdentry->d_inode;
1246
1247         err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1248         if (fdir->i_dev != tdir->i_dev)
1249                 goto out;
1250
1251         err = nfserr_perm;
1252         if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1253                 goto out;
1254
1255         /* cannot use fh_lock as we need deadlock protective ordering
1256          * so do it by hand */
1257         double_down(&tdir->i_sem, &fdir->i_sem);
1258         ffhp->fh_locked = tfhp->fh_locked = 1;
1259         fill_pre_wcc(ffhp);
1260         fill_pre_wcc(tfhp);
1261
1262         odentry = lookup_one_len(fname, fdentry, flen);
1263         err = PTR_ERR(odentry);
1264         if (IS_ERR(odentry))
1265                 goto out_nfserr;
1266
1267         err = -ENOENT;
1268         if (!odentry->d_inode)
1269                 goto out_dput_old;
1270
1271         ndentry = lookup_one_len(tname, tdentry, tlen);
1272         err = PTR_ERR(ndentry);
1273         if (IS_ERR(ndentry))
1274                 goto out_dput_old;
1275
1276
1277 #ifdef MSNFS
1278         if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1279                 ((atomic_read(&odentry->d_count) > 1)
1280                  || (atomic_read(&ndentry->d_count) > 1))) {
1281                         err = nfserr_perm;
1282         } else
1283 #endif
1284         err = vfs_rename(fdir, odentry, tdir, ndentry);
1285         if (!err && EX_ISSYNC(tfhp->fh_export)) {
1286                 nfsd_sync_dir(tdentry);
1287                 nfsd_sync_dir(fdentry);
1288         }
1289         dput(ndentry);
1290
1291  out_dput_old:
1292         dput(odentry);
1293  out_nfserr:
1294         if (err)
1295                 err = nfserrno(err);
1296
1297         /* we cannot reply on fh_unlock on the two filehandles,
1298          * as that would do the wrong thing if the two directories
1299          * were the same, so again we do it by hand
1300          */
1301         fill_post_wcc(ffhp);
1302         fill_post_wcc(tfhp);
1303         double_up(&tdir->i_sem, &fdir->i_sem);
1304         ffhp->fh_locked = tfhp->fh_locked = 0;
1305         
1306 out:
1307         return err;
1308 }
1309
1310 /*
1311  * Unlink a file or directory
1312  * N.B. After this call fhp needs an fh_put
1313  */
1314 int
1315 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1316                                 char *fname, int flen)
1317 {
1318         struct dentry   *dentry, *rdentry;
1319         struct inode    *dirp;
1320         int             err;
1321
1322         err = nfserr_acces;
1323         if (!flen || isdotent(fname, flen))
1324                 goto out;
1325         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1326         if (err)
1327                 goto out;
1328
1329         fh_lock(fhp);
1330         dentry = fhp->fh_dentry;
1331         dirp = dentry->d_inode;
1332
1333         rdentry = lookup_one_len(fname, dentry, flen);
1334         err = PTR_ERR(rdentry);
1335         if (IS_ERR(rdentry))
1336                 goto out_nfserr;
1337
1338         if (!rdentry->d_inode) {
1339                 dput(rdentry);
1340                 err = nfserr_noent;
1341                 goto out;
1342         }
1343
1344         if (type != S_IFDIR) { /* It's UNLINK */
1345 #ifdef MSNFS
1346                 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1347                         (atomic_read(&rdentry->d_count) > 1)) {
1348                         err = nfserr_perm;
1349                 } else
1350 #endif
1351                 err = vfs_unlink(dirp, rdentry);
1352         } else { /* It's RMDIR */
1353                 err = vfs_rmdir(dirp, rdentry);
1354         }
1355
1356         dput(rdentry);
1357
1358         if (err)
1359                 goto out_nfserr;
1360         if (EX_ISSYNC(fhp->fh_export)) 
1361                 nfsd_sync_dir(dentry);
1362
1363 out:
1364         return err;
1365
1366 out_nfserr:
1367         err = nfserrno(err);
1368         goto out;
1369 }
1370
1371 /*
1372  * Read entries from a directory.
1373  * The verifier is an NFSv3 thing we ignore for now.
1374  */
1375 int
1376 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, 
1377              encode_dent_fn func, u32 *buffer, int *countp, u32 *verf)
1378 {
1379         struct inode    *inode;
1380         u32             *p;
1381         int             oldlen, eof, err;
1382         struct file     file;
1383         struct readdir_cd cd;
1384
1385         err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1386         if (err)
1387                 goto out;
1388         if (offset > ~(u32) 0)
1389                 goto out_close;
1390
1391         err = nfserr_notdir;
1392         if (!file.f_op->readdir)
1393                 goto out_close;
1394         file.f_pos = offset;
1395
1396         /* Set up the readdir context */
1397         memset(&cd, 0, sizeof(cd));
1398         cd.rqstp  = rqstp;
1399         cd.buffer = buffer;
1400         cd.buflen = *countp; /* count of words */
1401         cd.dirfh  = fhp;
1402
1403         /*
1404          * Read the directory entries. This silly loop is necessary because
1405          * readdir() is not guaranteed to fill up the entire buffer, but
1406          * may choose to do less.
1407          */
1408         inode = file.f_dentry->d_inode;
1409         down(&inode->i_sem);
1410         while (1) {
1411                 oldlen = cd.buflen;
1412
1413                 /*
1414                 dprintk("nfsd: f_op->readdir(%x/%ld @ %d) buflen = %d (%d)\n",
1415                         file.f_inode->i_dev, file.f_inode->i_ino,
1416                         (int) file.f_pos, (int) oldlen, (int) cd.buflen);
1417                  */
1418                 err = file.f_op->readdir(&file, &cd, (filldir_t) func);
1419                 if (err < 0)
1420                         goto out_nfserr;
1421                 if (oldlen == cd.buflen)
1422                         break;
1423                 if (cd.eob)
1424                         break;
1425         }
1426         up(&inode->i_sem);
1427
1428         /* If we didn't fill the buffer completely, we're at EOF */
1429         eof = !cd.eob;
1430
1431         if (cd.offset) {
1432                 if (rqstp->rq_vers == 3)
1433                         (void)xdr_encode_hyper(cd.offset, file.f_pos);
1434                 else
1435                         *cd.offset = htonl(file.f_pos);
1436         }
1437
1438         p = cd.buffer;
1439         *p++ = 0;                       /* no more entries */
1440         *p++ = htonl(eof);              /* end of directory */
1441         *countp = (caddr_t) p - (caddr_t) buffer;
1442
1443         dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1444                                 *countp, eof,
1445                                 cd.offset? ntohl(*cd.offset) : -1);
1446         err = 0;
1447 out_close:
1448         nfsd_close(&file);
1449 out:
1450         return err;
1451
1452 out_nfserr:
1453         up(&inode->i_sem);
1454         err = nfserrno(err);
1455         goto out_close;
1456 }
1457
1458 /*
1459  * Get file system stats
1460  * N.B. After this call fhp needs an fh_put
1461  */
1462 int
1463 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct statfs *stat)
1464 {
1465         int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1466         if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1467                 err = nfserr_io;
1468         return err;
1469 }
1470
1471 /*
1472  * Check for a user's access permissions to this inode.
1473  */
1474 int
1475 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1476 {
1477         struct inode    *inode = dentry->d_inode;
1478         int             err;
1479
1480         if (acc == MAY_NOP)
1481                 return 0;
1482 #if 0
1483         dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1484                 acc,
1485                 (acc & MAY_READ)?       " read"  : "",
1486                 (acc & MAY_WRITE)?      " write" : "",
1487                 (acc & MAY_EXEC)?       " exec"  : "",
1488                 (acc & MAY_SATTR)?      " sattr" : "",
1489                 (acc & MAY_TRUNC)?      " trunc" : "",
1490                 (acc & MAY_LOCK)?       " lock"  : "",
1491                 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1492                 inode->i_mode,
1493                 IS_IMMUTABLE(inode)?    " immut" : "",
1494                 IS_APPEND(inode)?       " append" : "",
1495                 IS_RDONLY(inode)?       " ro" : "");
1496         dprintk("      owner %d/%d user %d/%d\n",
1497                 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1498 #endif
1499
1500         /* only care about readonly exports for files and
1501          * directories. links don't have meaningful write access,
1502          * and all else is local to the client
1503          */
1504         if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) 
1505                 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1506                         if (EX_RDONLY(exp) || IS_RDONLY(inode))
1507                                 return nfserr_rofs;
1508                         if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1509                                 return nfserr_perm;
1510                 }
1511         if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1512                 return nfserr_perm;
1513
1514         if (acc & MAY_LOCK) {
1515                 /* If we cannot rely on authentication in NLM requests,
1516                  * just allow locks, otherwise require read permission, or
1517                  * ownership
1518                  */
1519                 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1520                         return 0;
1521                 else
1522                         acc = MAY_READ | MAY_OWNER_OVERRIDE;
1523         }
1524         /*
1525          * The file owner always gets access permission for accesses that
1526          * would normally be checked at open time. This is to make
1527          * file access work even when the client has done a fchmod(fd, 0).
1528          *
1529          * However, `cp foo bar' should fail nevertheless when bar is
1530          * readonly. A sensible way to do this might be to reject all
1531          * attempts to truncate a read-only file, because a creat() call
1532          * always implies file truncation.
1533          * ... but this isn't really fair.  A process may reasonably call
1534          * ftruncate on an open file descriptor on a file with perm 000.
1535          * We must trust the client to do permission checking - using "ACCESS"
1536          * with NFSv3.
1537          */
1538         if ((acc & MAY_OWNER_OVERRIDE) &&
1539             inode->i_uid == current->fsuid)
1540                 return 0;
1541
1542         acc &= ~ MAY_OWNER_OVERRIDE; /* This bit is no longer needed,
1543                                         and gets in the way later */
1544
1545         err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1546
1547         /* Allow read access to binaries even when mode 111 */
1548         if (err == -EACCES && S_ISREG(inode->i_mode) && acc == MAY_READ)
1549                 err = permission(inode, MAY_EXEC);
1550
1551         return err? nfserrno(err) : 0;
1552 }
1553
1554 void
1555 nfsd_racache_shutdown(void)
1556 {
1557         if (!raparm_cache)
1558                 return;
1559         dprintk("nfsd: freeing readahead buffers.\n");
1560         kfree(raparml);
1561         raparm_cache = raparml = NULL;
1562 }
1563 /*
1564  * Initialize readahead param cache
1565  */
1566 int
1567 nfsd_racache_init(int cache_size)
1568 {
1569         int     i;
1570
1571         if (raparm_cache)
1572                 return 0;
1573         raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1574
1575         if (raparml != NULL) {
1576                 dprintk("nfsd: allocating %d readahead buffers.\n",
1577                         cache_size);
1578                 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1579                 for (i = 0; i < cache_size - 1; i++) {
1580                         raparml[i].p_next = raparml + i + 1;
1581                 }
1582                 raparm_cache = raparml;
1583         } else {
1584                 printk(KERN_WARNING
1585                        "nfsd: Could not allocate memory read-ahead cache.\n");
1586                 return -ENOMEM;
1587         }
1588         nfsdstats.ra_size = cache_size;
1589         return 0;
1590 }