a252cac5a6f32dceaa8b169a6e2d4508fb8b4cdc
[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)
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         memset(&ra->p_ra, 0, sizeof(ra->p_ra));
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         if (ra)
666                 file.f_ra = ra->p_ra;
667
668         if (file.f_op->sendfile) {
669                 svc_pushback_unused_pages(rqstp);
670                 err = file.f_op->sendfile(&file, &offset, *count,
671                                                  nfsd_read_actor, rqstp);
672         } else {
673                 oldfs = get_fs();
674                 set_fs(KERNEL_DS);
675                 err = vfs_readv(&file, vec, vlen, &offset);
676                 set_fs(oldfs);
677         }
678
679         /* Write back readahead params */
680         if (ra)
681                 ra->p_ra = file.f_ra;
682
683         if (err >= 0) {
684                 nfsdstats.io_read += err;
685                 *count = err;
686                 err = 0;
687                 dnotify_parent(file.f_dentry, DN_ACCESS);
688         } else 
689                 err = nfserrno(err);
690 out_close:
691         nfsd_close(&file);
692 out:
693         return err;
694 }
695
696 /*
697  * Write data to a file.
698  * The stable flag requests synchronous writes.
699  * N.B. After this call fhp needs an fh_put
700  */
701 int
702 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
703                                 struct iovec *vec, int vlen,
704                                 unsigned long cnt, int *stablep)
705 {
706         struct svc_export       *exp;
707         struct file             file;
708         struct dentry           *dentry;
709         struct inode            *inode;
710         mm_segment_t            oldfs;
711         int                     err = 0;
712         int                     stable = *stablep;
713
714         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
715         if (err)
716                 goto out;
717         if (!cnt)
718                 goto out_close;
719         err = nfserr_perm;
720
721 #ifdef MSNFS
722         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
723                 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
724                 goto out_close;
725 #endif
726
727         dentry = file.f_dentry;
728         inode = dentry->d_inode;
729         exp   = fhp->fh_export;
730
731         /*
732          * Request sync writes if
733          *  -   the sync export option has been set, or
734          *  -   the client requested O_SYNC behavior (NFSv3 feature).
735          *  -   The file system doesn't support fsync().
736          * When gathered writes have been configured for this volume,
737          * flushing the data to disk is handled separately below.
738          */
739
740         if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
741                stable = 2;
742                *stablep = 2; /* FILE_SYNC */
743         }
744
745         if (!EX_ISSYNC(exp))
746                 stable = 0;
747         if (stable && !EX_WGATHER(exp))
748                 file.f_flags |= O_SYNC;
749
750         /* Write the data. */
751         oldfs = get_fs(); set_fs(KERNEL_DS);
752         err = vfs_writev(&file, vec, vlen, &offset);
753         set_fs(oldfs);
754         if (err >= 0) {
755                 nfsdstats.io_write += cnt;
756                 dnotify_parent(file.f_dentry, DN_MODIFY);
757         }
758
759         /* clear setuid/setgid flag after write */
760         if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
761                 struct iattr    ia;
762                 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
763
764                 down(&inode->i_sem);
765                 notify_change(dentry, &ia);
766                 up(&inode->i_sem);
767         }
768
769         if (err >= 0 && stable) {
770                 static ino_t    last_ino;
771                 static dev_t    last_dev;
772
773                 /*
774                  * Gathered writes: If another process is currently
775                  * writing to the file, there's a high chance
776                  * this is another nfsd (triggered by a bulk write
777                  * from a client's biod). Rather than syncing the
778                  * file with each write request, we sleep for 10 msec.
779                  *
780                  * I don't know if this roughly approximates
781                  * C. Juszak's idea of gathered writes, but it's a
782                  * nice and simple solution (IMHO), and it seems to
783                  * work:-)
784                  */
785                 if (EX_WGATHER(exp)) {
786                         if (atomic_read(&inode->i_writecount) > 1
787                             || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
788                                 dprintk("nfsd: write defer %d\n", current->pid);
789                                 set_current_state(TASK_UNINTERRUPTIBLE);
790                                 schedule_timeout((HZ+99)/100);
791                                 dprintk("nfsd: write resume %d\n", current->pid);
792                         }
793
794                         if (inode->i_state & I_DIRTY) {
795                                 dprintk("nfsd: write sync %d\n", current->pid);
796                                 nfsd_sync(&file);
797                         }
798 #if 0
799                         wake_up(&inode->i_wait);
800 #endif
801                 }
802                 last_ino = inode->i_ino;
803                 last_dev = inode->i_sb->s_dev;
804         }
805
806         dprintk("nfsd: write complete err=%d\n", err);
807         if (err >= 0)
808                 err = 0;
809         else 
810                 err = nfserrno(err);
811 out_close:
812         nfsd_close(&file);
813 out:
814         return err;
815 }
816
817
818 #ifdef CONFIG_NFSD_V3
819 /*
820  * Commit all pending writes to stable storage.
821  * Strictly speaking, we could sync just the indicated file region here,
822  * but there's currently no way we can ask the VFS to do so.
823  *
824  * Unfortunately we cannot lock the file to make sure we return full WCC
825  * data to the client, as locking happens lower down in the filesystem.
826  */
827 int
828 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
829                loff_t offset, unsigned long count)
830 {
831         struct file     file;
832         int             err;
833
834         if ((u64)count > ~(u64)offset)
835                 return nfserr_inval;
836
837         if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
838                 return err;
839         if (EX_ISSYNC(fhp->fh_export)) {
840                 if (file.f_op && file.f_op->fsync) {
841                         nfsd_sync(&file);
842                 } else {
843                         err = nfserr_notsupp;
844                 }
845         }
846
847         nfsd_close(&file);
848         return err;
849 }
850 #endif /* CONFIG_NFSD_V3 */
851
852 /*
853  * Create a file (regular, directory, device, fifo); UNIX sockets 
854  * not yet implemented.
855  * If the response fh has been verified, the parent directory should
856  * already be locked. Note that the parent directory is left locked.
857  *
858  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
859  */
860 int
861 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
862                 char *fname, int flen, struct iattr *iap,
863                 int type, dev_t rdev, struct svc_fh *resfhp)
864 {
865         struct dentry   *dentry, *dchild = NULL;
866         struct inode    *dirp;
867         int             err;
868
869         err = nfserr_perm;
870         if (!flen)
871                 goto out;
872         err = nfserr_exist;
873         if (isdotent(fname, flen))
874                 goto out;
875
876         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
877         if (err)
878                 goto out;
879
880         dentry = fhp->fh_dentry;
881         dirp = dentry->d_inode;
882
883         err = nfserr_notdir;
884         if(!dirp->i_op || !dirp->i_op->lookup)
885                 goto out;
886         /*
887          * Check whether the response file handle has been verified yet.
888          * If it has, the parent directory should already be locked.
889          */
890         if (!resfhp->fh_dentry) {
891                 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
892                 fh_lock(fhp);
893                 dchild = lookup_one_len(fname, dentry, flen);
894                 err = PTR_ERR(dchild);
895                 if (IS_ERR(dchild))
896                         goto out_nfserr;
897                 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
898                 if (err)
899                         goto out;
900         } else {
901                 /* called from nfsd_proc_create */
902                 dchild = dget(resfhp->fh_dentry);
903                 if (!fhp->fh_locked) {
904                         /* not actually possible */
905                         printk(KERN_ERR
906                                 "nfsd_create: parent %s/%s not locked!\n",
907                                 dentry->d_parent->d_name.name,
908                                 dentry->d_name.name);
909                         err = -EIO;
910                         goto out;
911                 }
912         }
913         /*
914          * Make sure the child dentry is still negative ...
915          */
916         err = nfserr_exist;
917         if (dchild->d_inode) {
918                 dprintk("nfsd_create: dentry %s/%s not negative!\n",
919                         dentry->d_name.name, dchild->d_name.name);
920                 goto out; 
921         }
922
923         if (!(iap->ia_valid & ATTR_MODE))
924                 iap->ia_mode = 0;
925         iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
926
927         /*
928          * Get the dir op function pointer.
929          */
930         err = nfserr_perm;
931         switch (type) {
932         case S_IFREG:
933                 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
934                 break;
935         case S_IFDIR:
936                 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
937                 break;
938         case S_IFCHR:
939         case S_IFBLK:
940         case S_IFIFO:
941         case S_IFSOCK:
942                 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
943                 break;
944         default:
945                 printk("nfsd: bad file type %o in nfsd_create\n", type);
946                 err = -EINVAL;
947         }
948         if (err < 0)
949                 goto out_nfserr;
950
951         if (EX_ISSYNC(fhp->fh_export)) {
952                 nfsd_sync_dir(dentry);
953                 write_inode_now(dchild->d_inode, 1);
954         }
955
956
957         /* Set file attributes. Mode has already been set and
958          * setting uid/gid works only for root. Irix appears to
959          * send along the gid when it tries to implement setgid
960          * directories via NFS.
961          */
962         err = 0;
963         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
964                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
965         /*
966          * Update the file handle to get the new inode info.
967          */
968         if (!err)
969                 err = fh_update(resfhp);
970 out:
971         if (dchild && !IS_ERR(dchild))
972                 dput(dchild);
973         return err;
974
975 out_nfserr:
976         err = nfserrno(err);
977         goto out;
978 }
979
980 #ifdef CONFIG_NFSD_V3
981 /*
982  * NFSv3 version of nfsd_create
983  */
984 int
985 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
986                 char *fname, int flen, struct iattr *iap,
987                 struct svc_fh *resfhp, int createmode, u32 *verifier,
988                 int *truncp)
989 {
990         struct dentry   *dentry, *dchild = NULL;
991         struct inode    *dirp;
992         int             err;
993         __u32           v_mtime=0, v_atime=0;
994         int             v_mode=0;
995
996         err = nfserr_perm;
997         if (!flen)
998                 goto out;
999         err = nfserr_exist;
1000         if (isdotent(fname, flen))
1001                 goto out;
1002         if (!(iap->ia_valid & ATTR_MODE))
1003                 iap->ia_mode = 0;
1004         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1005         if (err)
1006                 goto out;
1007
1008         dentry = fhp->fh_dentry;
1009         dirp = dentry->d_inode;
1010
1011         /* Get all the sanity checks out of the way before
1012          * we lock the parent. */
1013         err = nfserr_notdir;
1014         if(!dirp->i_op || !dirp->i_op->lookup)
1015                 goto out;
1016         fh_lock(fhp);
1017
1018         /*
1019          * Compose the response file handle.
1020          */
1021         dchild = lookup_one_len(fname, dentry, flen);
1022         err = PTR_ERR(dchild);
1023         if (IS_ERR(dchild))
1024                 goto out_nfserr;
1025
1026         err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1027         if (err)
1028                 goto out;
1029
1030         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1031                 /* while the verifier would fit in mtime+atime,
1032                  * solaris7 gets confused (bugid 4218508) if these have
1033                  * the high bit set, so we use the mode as well
1034                  */
1035                 v_mtime = verifier[0]&0x7fffffff;
1036                 v_atime = verifier[1]&0x7fffffff;
1037                 v_mode  = S_IFREG
1038                         | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1039                         | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1040                         ;
1041         }
1042         
1043         if (dchild->d_inode) {
1044                 err = 0;
1045
1046                 switch (createmode) {
1047                 case NFS3_CREATE_UNCHECKED:
1048                         if (! S_ISREG(dchild->d_inode->i_mode))
1049                                 err = nfserr_exist;
1050                         else if (truncp) {
1051                                 /* in nfsv4, we need to treat this case a little
1052                                  * differently.  we don't want to truncate the
1053                                  * file now; this would be wrong if the OPEN
1054                                  * fails for some other reason.  furthermore,
1055                                  * if the size is nonzero, we should ignore it
1056                                  * according to spec!
1057                                  */
1058                                 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1059                         }
1060                         else {
1061                                 iap->ia_valid &= ATTR_SIZE;
1062                                 goto set_attr;
1063                         }
1064                         break;
1065                 case NFS3_CREATE_EXCLUSIVE:
1066                         if (   dchild->d_inode->i_mtime.tv_sec == v_mtime
1067                             && dchild->d_inode->i_atime.tv_sec == v_atime
1068                             && dchild->d_inode->i_mode  == v_mode
1069                             && dchild->d_inode->i_size  == 0 )
1070                                 break;
1071                          /* fallthru */
1072                 case NFS3_CREATE_GUARDED:
1073                         err = nfserr_exist;
1074                 }
1075                 goto out;
1076         }
1077
1078         err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1079         if (err < 0)
1080                 goto out_nfserr;
1081
1082         if (EX_ISSYNC(fhp->fh_export)) {
1083                 nfsd_sync_dir(dentry);
1084                 /* setattr will sync the child (or not) */
1085         }
1086
1087         /*
1088          * Update the filehandle to get the new inode info.
1089          */
1090         err = fh_update(resfhp);
1091         if (err)
1092                 goto out;
1093
1094         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1095                 /* Cram the verifier into atime/mtime/mode */
1096                 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1097                         | ATTR_MTIME_SET|ATTR_ATIME_SET
1098                         | ATTR_MODE;
1099                 /* XXX someone who knows this better please fix it for nsec */ 
1100                 iap->ia_mtime.tv_sec = v_mtime;
1101                 iap->ia_atime.tv_sec = v_atime;
1102                 iap->ia_mtime.tv_nsec = 0;
1103                 iap->ia_atime.tv_nsec = 0;
1104                 iap->ia_mode  = v_mode;
1105         }
1106
1107         /* Set file attributes.
1108          * Mode has already been set but we might need to reset it
1109          * for CREATE_EXCLUSIVE
1110          * Irix appears to send along the gid when it tries to
1111          * implement setgid directories via NFS. Clear out all that cruft.
1112          */
1113  set_attr:
1114         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1115                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1116
1117  out:
1118         fh_unlock(fhp);
1119         if (dchild && !IS_ERR(dchild))
1120                 dput(dchild);
1121         return err;
1122  
1123  out_nfserr:
1124         err = nfserrno(err);
1125         goto out;
1126 }
1127 #endif /* CONFIG_NFSD_V3 */
1128
1129 /*
1130  * Read a symlink. On entry, *lenp must contain the maximum path length that
1131  * fits into the buffer. On return, it contains the true length.
1132  * N.B. After this call fhp needs an fh_put
1133  */
1134 int
1135 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1136 {
1137         struct dentry   *dentry;
1138         struct inode    *inode;
1139         mm_segment_t    oldfs;
1140         int             err;
1141
1142         err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1143         if (err)
1144                 goto out;
1145
1146         dentry = fhp->fh_dentry;
1147         inode = dentry->d_inode;
1148
1149         err = nfserr_inval;
1150         if (!inode->i_op || !inode->i_op->readlink)
1151                 goto out;
1152
1153         touch_atime(fhp->fh_export->ex_mnt, dentry);
1154         /* N.B. Why does this call need a get_fs()??
1155          * Remove the set_fs and watch the fireworks:-) --okir
1156          */
1157
1158         oldfs = get_fs(); set_fs(KERNEL_DS);
1159         err = inode->i_op->readlink(dentry, buf, *lenp);
1160         set_fs(oldfs);
1161
1162         if (err < 0)
1163                 goto out_nfserr;
1164         *lenp = err;
1165         err = 0;
1166 out:
1167         return err;
1168
1169 out_nfserr:
1170         err = nfserrno(err);
1171         goto out;
1172 }
1173
1174 /*
1175  * Create a symlink and look up its inode
1176  * N.B. After this call _both_ fhp and resfhp need an fh_put
1177  */
1178 int
1179 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1180                                 char *fname, int flen,
1181                                 char *path,  int plen,
1182                                 struct svc_fh *resfhp,
1183                                 struct iattr *iap)
1184 {
1185         struct dentry   *dentry, *dnew;
1186         int             err, cerr;
1187         umode_t         mode;
1188
1189         err = nfserr_noent;
1190         if (!flen || !plen)
1191                 goto out;
1192         err = nfserr_exist;
1193         if (isdotent(fname, flen))
1194                 goto out;
1195
1196         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1197         if (err)
1198                 goto out;
1199         fh_lock(fhp);
1200         dentry = fhp->fh_dentry;
1201         dnew = lookup_one_len(fname, dentry, flen);
1202         err = PTR_ERR(dnew);
1203         if (IS_ERR(dnew))
1204                 goto out_nfserr;
1205
1206         mode = S_IALLUGO;
1207         /* Only the MODE ATTRibute is even vaguely meaningful */
1208         if (iap && (iap->ia_valid & ATTR_MODE))
1209                 mode = iap->ia_mode & S_IALLUGO;
1210
1211         if (unlikely(path[plen] != 0)) {
1212                 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1213                 if (path_alloced == NULL)
1214                         err = -ENOMEM;
1215                 else {
1216                         strncpy(path_alloced, path, plen);
1217                         path_alloced[plen] = 0;
1218                         err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
1219                         kfree(path_alloced);
1220                 }
1221         } else
1222                 err = vfs_symlink(dentry->d_inode, dnew, path, mode);
1223
1224         if (!err) {
1225                 if (EX_ISSYNC(fhp->fh_export))
1226                         nfsd_sync_dir(dentry);
1227         } else
1228                 err = nfserrno(err);
1229         fh_unlock(fhp);
1230
1231         cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1232         dput(dnew);
1233         if (err==0) err = cerr;
1234 out:
1235         return err;
1236
1237 out_nfserr:
1238         err = nfserrno(err);
1239         goto out;
1240 }
1241
1242 /*
1243  * Create a hardlink
1244  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1245  */
1246 int
1247 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1248                                 char *name, int len, struct svc_fh *tfhp)
1249 {
1250         struct dentry   *ddir, *dnew, *dold;
1251         struct inode    *dirp, *dest;
1252         int             err;
1253
1254         err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1255         if (err)
1256                 goto out;
1257         err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1258         if (err)
1259                 goto out;
1260
1261         err = nfserr_perm;
1262         if (!len)
1263                 goto out;
1264         err = nfserr_exist;
1265         if (isdotent(name, len))
1266                 goto out;
1267
1268         fh_lock(ffhp);
1269         ddir = ffhp->fh_dentry;
1270         dirp = ddir->d_inode;
1271
1272         dnew = lookup_one_len(name, ddir, len);
1273         err = PTR_ERR(dnew);
1274         if (IS_ERR(dnew))
1275                 goto out_nfserr;
1276
1277         dold = tfhp->fh_dentry;
1278         dest = dold->d_inode;
1279
1280         err = vfs_link(dold, dirp, dnew);
1281         if (!err) {
1282                 if (EX_ISSYNC(ffhp->fh_export)) {
1283                         nfsd_sync_dir(ddir);
1284                         write_inode_now(dest, 1);
1285                 }
1286         } else {
1287                 if (err == -EXDEV && rqstp->rq_vers == 2)
1288                         err = nfserr_acces;
1289                 else
1290                         err = nfserrno(err);
1291         }
1292
1293         fh_unlock(ffhp);
1294         dput(dnew);
1295 out:
1296         return err;
1297
1298 out_nfserr:
1299         err = nfserrno(err);
1300         goto out;
1301 }
1302
1303 /*
1304  * Rename a file
1305  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1306  */
1307 int
1308 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1309                             struct svc_fh *tfhp, char *tname, int tlen)
1310 {
1311         struct dentry   *fdentry, *tdentry, *odentry, *ndentry, *trap;
1312         struct inode    *fdir, *tdir;
1313         int             err;
1314
1315         err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1316         if (err)
1317                 goto out;
1318         err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1319         if (err)
1320                 goto out;
1321
1322         fdentry = ffhp->fh_dentry;
1323         fdir = fdentry->d_inode;
1324
1325         tdentry = tfhp->fh_dentry;
1326         tdir = tdentry->d_inode;
1327
1328         err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1329         if (fdir->i_sb != tdir->i_sb)
1330                 goto out;
1331
1332         err = nfserr_perm;
1333         if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1334                 goto out;
1335
1336         /* cannot use fh_lock as we need deadlock protective ordering
1337          * so do it by hand */
1338         trap = lock_rename(tdentry, fdentry);
1339         ffhp->fh_locked = tfhp->fh_locked = 1;
1340         fill_pre_wcc(ffhp);
1341         fill_pre_wcc(tfhp);
1342
1343         odentry = lookup_one_len(fname, fdentry, flen);
1344         err = PTR_ERR(odentry);
1345         if (IS_ERR(odentry))
1346                 goto out_nfserr;
1347
1348         err = -ENOENT;
1349         if (!odentry->d_inode)
1350                 goto out_dput_old;
1351         err = -EINVAL;
1352         if (odentry == trap)
1353                 goto out_dput_old;
1354
1355         ndentry = lookup_one_len(tname, tdentry, tlen);
1356         err = PTR_ERR(ndentry);
1357         if (IS_ERR(ndentry))
1358                 goto out_dput_old;
1359         err = -ENOTEMPTY;
1360         if (ndentry == trap)
1361                 goto out_dput_new;
1362
1363 #ifdef MSNFS
1364         if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1365                 ((atomic_read(&odentry->d_count) > 1)
1366                  || (atomic_read(&ndentry->d_count) > 1))) {
1367                         err = nfserr_perm;
1368         } else
1369 #endif
1370         err = vfs_rename(fdir, odentry, tdir, ndentry);
1371         if (!err && EX_ISSYNC(tfhp->fh_export)) {
1372                 nfsd_sync_dir(tdentry);
1373                 nfsd_sync_dir(fdentry);
1374         }
1375
1376  out_dput_new:
1377         dput(ndentry);
1378  out_dput_old:
1379         dput(odentry);
1380  out_nfserr:
1381         if (err)
1382                 err = nfserrno(err);
1383
1384         /* we cannot reply on fh_unlock on the two filehandles,
1385          * as that would do the wrong thing if the two directories
1386          * were the same, so again we do it by hand
1387          */
1388         fill_post_wcc(ffhp);
1389         fill_post_wcc(tfhp);
1390         unlock_rename(tdentry, fdentry);
1391         ffhp->fh_locked = tfhp->fh_locked = 0;
1392
1393 out:
1394         return err;
1395 }
1396
1397 /*
1398  * Unlink a file or directory
1399  * N.B. After this call fhp needs an fh_put
1400  */
1401 int
1402 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1403                                 char *fname, int flen)
1404 {
1405         struct dentry   *dentry, *rdentry;
1406         struct inode    *dirp;
1407         int             err;
1408
1409         err = nfserr_acces;
1410         if (!flen || isdotent(fname, flen))
1411                 goto out;
1412         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1413         if (err)
1414                 goto out;
1415
1416         fh_lock(fhp);
1417         dentry = fhp->fh_dentry;
1418         dirp = dentry->d_inode;
1419
1420         rdentry = lookup_one_len(fname, dentry, flen);
1421         err = PTR_ERR(rdentry);
1422         if (IS_ERR(rdentry))
1423                 goto out_nfserr;
1424
1425         if (!rdentry->d_inode) {
1426                 dput(rdentry);
1427                 err = nfserr_noent;
1428                 goto out;
1429         }
1430
1431         if (!type)
1432                 type = rdentry->d_inode->i_mode & S_IFMT;
1433
1434         if (type != S_IFDIR) { /* It's UNLINK */
1435 #ifdef MSNFS
1436                 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1437                         (atomic_read(&rdentry->d_count) > 1)) {
1438                         err = nfserr_perm;
1439                 } else
1440 #endif
1441                 err = vfs_unlink(dirp, rdentry);
1442         } else { /* It's RMDIR */
1443                 err = vfs_rmdir(dirp, rdentry);
1444         }
1445
1446         dput(rdentry);
1447
1448         if (err)
1449                 goto out_nfserr;
1450         if (EX_ISSYNC(fhp->fh_export)) 
1451                 nfsd_sync_dir(dentry);
1452
1453 out:
1454         return err;
1455
1456 out_nfserr:
1457         err = nfserrno(err);
1458         goto out;
1459 }
1460
1461 /*
1462  * Read entries from a directory.
1463  * The  NFSv3/4 verifier we ignore for now.
1464  */
1465 int
1466 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, 
1467              struct readdir_cd *cdp, encode_dent_fn func)
1468 {
1469         int             err;
1470         struct file     file;
1471         loff_t          offset = *offsetp;
1472
1473         err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1474         if (err)
1475                 goto out;
1476         if (offset > ~(u32) 0)
1477                 goto out_close;
1478
1479         file.f_pos = offset;
1480
1481         /*
1482          * Read the directory entries. This silly loop is necessary because
1483          * readdir() is not guaranteed to fill up the entire buffer, but
1484          * may choose to do less.
1485          */
1486
1487         do {
1488                 cdp->err = nfserr_eof; /* will be cleared on successful read */
1489                 err = vfs_readdir(&file, (filldir_t) func, cdp);
1490         } while (err >=0 && cdp->err == nfs_ok);
1491         if (err)
1492                 err = nfserrno(err);
1493         else
1494                 err = cdp->err;
1495         *offsetp = file.f_pos;
1496
1497         if (err == nfserr_eof || err == nfserr_toosmall)
1498                 err = nfs_ok; /* can still be found in ->err */
1499 out_close:
1500         nfsd_close(&file);
1501 out:
1502         return err;
1503 }
1504
1505 /*
1506  * Get file system stats
1507  * N.B. After this call fhp needs an fh_put
1508  */
1509 int
1510 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
1511 {
1512         int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1513         if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1514                 err = nfserr_io;
1515         return err;
1516 }
1517
1518 /*
1519  * Check for a user's access permissions to this inode.
1520  */
1521 int
1522 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1523 {
1524         struct inode    *inode = dentry->d_inode;
1525         int             err;
1526
1527         if (acc == MAY_NOP)
1528                 return 0;
1529 #if 0
1530         dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1531                 acc,
1532                 (acc & MAY_READ)?       " read"  : "",
1533                 (acc & MAY_WRITE)?      " write" : "",
1534                 (acc & MAY_EXEC)?       " exec"  : "",
1535                 (acc & MAY_SATTR)?      " sattr" : "",
1536                 (acc & MAY_TRUNC)?      " trunc" : "",
1537                 (acc & MAY_LOCK)?       " lock"  : "",
1538                 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1539                 inode->i_mode,
1540                 IS_IMMUTABLE(inode)?    " immut" : "",
1541                 IS_APPEND(inode)?       " append" : "",
1542                 IS_RDONLY(inode)?       " ro" : "");
1543         dprintk("      owner %d/%d user %d/%d\n",
1544                 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1545 #endif
1546
1547         /* Normally we reject any write/sattr etc access on a read-only file
1548          * system.  But if it is IRIX doing check on write-access for a 
1549          * device special file, we ignore rofs.
1550          */
1551         if (!(acc & MAY_LOCAL_ACCESS))
1552                 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1553                         if (EX_RDONLY(exp) || IS_RDONLY(inode))
1554                                 return nfserr_rofs;
1555                         if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1556                                 return nfserr_perm;
1557                 }
1558         if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1559                 return nfserr_perm;
1560
1561         if (acc & MAY_LOCK) {
1562                 /* If we cannot rely on authentication in NLM requests,
1563                  * just allow locks, otherwise require read permission, or
1564                  * ownership
1565                  */
1566                 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1567                         return 0;
1568                 else
1569                         acc = MAY_READ | MAY_OWNER_OVERRIDE;
1570         }
1571         /*
1572          * The file owner always gets access permission for accesses that
1573          * would normally be checked at open time. This is to make
1574          * file access work even when the client has done a fchmod(fd, 0).
1575          *
1576          * However, `cp foo bar' should fail nevertheless when bar is
1577          * readonly. A sensible way to do this might be to reject all
1578          * attempts to truncate a read-only file, because a creat() call
1579          * always implies file truncation.
1580          * ... but this isn't really fair.  A process may reasonably call
1581          * ftruncate on an open file descriptor on a file with perm 000.
1582          * We must trust the client to do permission checking - using "ACCESS"
1583          * with NFSv3.
1584          */
1585         if ((acc & MAY_OWNER_OVERRIDE) &&
1586             inode->i_uid == current->fsuid)
1587                 return 0;
1588
1589         err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
1590
1591         /* Allow read access to binaries even when mode 111 */
1592         if (err == -EACCES && S_ISREG(inode->i_mode) &&
1593             acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1594                 err = permission(inode, MAY_EXEC, NULL);
1595
1596         return err? nfserrno(err) : 0;
1597 }
1598
1599 void
1600 nfsd_racache_shutdown(void)
1601 {
1602         if (!raparm_cache)
1603                 return;
1604         dprintk("nfsd: freeing readahead buffers.\n");
1605         kfree(raparml);
1606         raparm_cache = raparml = NULL;
1607 }
1608 /*
1609  * Initialize readahead param cache
1610  */
1611 int
1612 nfsd_racache_init(int cache_size)
1613 {
1614         int     i;
1615
1616         if (raparm_cache)
1617                 return 0;
1618         raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1619
1620         if (raparml != NULL) {
1621                 dprintk("nfsd: allocating %d readahead buffers.\n",
1622                         cache_size);
1623                 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1624                 for (i = 0; i < cache_size - 1; i++) {
1625                         raparml[i].p_next = raparml + i + 1;
1626                 }
1627                 raparm_cache = raparml;
1628         } else {
1629                 printk(KERN_WARNING
1630                        "nfsd: Could not allocate memory read-ahead cache.\n");
1631                 return -ENOMEM;
1632         }
1633         nfsdstats.ra_size = cache_size;
1634         return 0;
1635 }