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