ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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                 goto out;
145
146         dparent = fhp->fh_dentry;
147         exp  = fhp->fh_export;
148
149         err = nfserr_acces;
150
151         /* Lookup the name, but don't follow links */
152         if (isdotent(name, len)) {
153                 if (len==1)
154                         dentry = dget(dparent);
155                 else if (dparent != exp->ex_dentry) {
156                         dentry = dget_parent(dparent);
157                 } else  if (!EX_NOHIDE(exp))
158                         dentry = dget(dparent); /* .. == . just like at / */
159                 else {
160                         /* checking mountpoint crossing is very different when stepping up */
161                         struct svc_export *exp2 = NULL;
162                         struct dentry *dp;
163                         struct vfsmount *mnt = mntget(exp->ex_mnt);
164                         dentry = dget(dparent);
165                         while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
166                                 ;
167                         dp = dget_parent(dentry);
168                         dput(dentry);
169                         dentry = dp;
170
171                         exp2 = exp_parent(exp->ex_client, mnt, dentry,
172                                           &rqstp->rq_chandle);
173                         if (IS_ERR(exp2)) {
174                                 err = PTR_ERR(exp2);
175                                 dput(dentry);
176                                 mntput(mnt);
177                                 goto out_nfserr;
178                         }
179                         if (!exp2) {
180                                 dput(dentry);
181                                 dentry = dget(dparent);
182                         } else {
183                                 exp_put(exp);
184                                 exp = exp2;
185                         }
186                         mntput(mnt);
187                 }
188         } else {
189                 fh_lock(fhp);
190                 dentry = lookup_one_len(name, dparent, len);
191                 err = PTR_ERR(dentry);
192                 if (IS_ERR(dentry))
193                         goto out_nfserr;
194                 /*
195                  * check if we have crossed a mount point ...
196                  */
197                 if (d_mountpoint(dentry)) {
198                         if ((err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
199                                 dput(dentry);
200                                 goto out_nfserr;
201                         }
202                 }
203         }
204         /*
205          * Note: we compose the file handle now, but as the
206          * dentry may be negative, it may need to be updated.
207          */
208         err = fh_compose(resfh, exp, dentry, fhp);
209         if (!err && !dentry->d_inode)
210                 err = nfserr_noent;
211 out:
212         return err;
213
214 out_nfserr:
215         err = nfserrno(err);
216         goto out;
217 }
218
219 /*
220  * Set various file attributes.
221  * N.B. After this call fhp needs an fh_put
222  */
223 int
224 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
225              int check_guard, time_t guardtime)
226 {
227         struct dentry   *dentry;
228         struct inode    *inode;
229         int             accmode = MAY_SATTR;
230         int             ftype = 0;
231         int             imode;
232         int             err;
233         int             size_change = 0;
234
235         if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
236                 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
237         if (iap->ia_valid & ATTR_SIZE)
238                 ftype = S_IFREG;
239
240         /* Get inode */
241         err = fh_verify(rqstp, fhp, ftype, accmode);
242         if (err || !iap->ia_valid)
243                 goto out;
244
245         dentry = fhp->fh_dentry;
246         inode = dentry->d_inode;
247
248         /* NFSv2 does not differentiate between "set-[ac]time-to-now"
249          * which only requires access, and "set-[ac]time-to-X" which
250          * requires ownership.
251          * So if it looks like it might be "set both to the same time which
252          * is close to now", and if inode_change_ok fails, then we
253          * convert to "set to now" instead of "set to explicit time"
254          *
255          * We only call inode_change_ok as the last test as technically
256          * it is not an interface that we should be using.  It is only
257          * valid if the filesystem does not define it's own i_op->setattr.
258          */
259 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
260 #define MAX_TOUCH_TIME_ERROR (30*60)
261         if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
262             && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
263             ) {
264             /* Looks probable.  Now just make sure time is in the right ballpark.
265              * Solaris, at least, doesn't seem to care what the time request is.
266              * We require it be within 30 minutes of now.
267              */
268             time_t delta = iap->ia_atime.tv_sec - get_seconds();
269             if (delta<0) delta = -delta;
270             if (delta < MAX_TOUCH_TIME_ERROR &&
271                 inode_change_ok(inode, iap) != 0) {
272                 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
273                  * this will cause notify_change to set these times to "now"
274                  */
275                 iap->ia_valid &= ~BOTH_TIME_SET;
276             }
277         }
278             
279         /* The size case is special. It changes the file as well as the attributes.  */
280         if (iap->ia_valid & ATTR_SIZE) {
281                 if (iap->ia_size < inode->i_size) {
282                         err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
283                         if (err)
284                                 goto out;
285                 }
286
287                 /*
288                  * If we are changing the size of the file, then
289                  * we need to break all leases.
290                  */
291                 err = break_lease(inode, FMODE_WRITE);
292                 if (err)
293                         goto out_nfserr;
294
295                 err = get_write_access(inode);
296                 if (err)
297                         goto out_nfserr;
298
299                 size_change = 1;
300                 err = locks_verify_truncate(inode, NULL, iap->ia_size);
301                 if (err) {
302                         put_write_access(inode);
303                         goto out_nfserr;
304                 }
305                 DQUOT_INIT(inode);
306         }
307
308         imode = inode->i_mode;
309         if (iap->ia_valid & ATTR_MODE) {
310                 iap->ia_mode &= S_IALLUGO;
311                 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
312         }
313
314         /* Revoke setuid/setgid bit on chown/chgrp */
315         if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
316                 iap->ia_valid |= ATTR_KILL_SUID;
317         if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
318                 iap->ia_valid |= ATTR_KILL_SGID;
319
320         /* Change the attributes. */
321
322         iap->ia_valid |= ATTR_CTIME;
323
324         err = nfserr_notsync;
325         if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
326                 fh_lock(fhp);
327                 err = notify_change(dentry, iap);
328                 err = nfserrno(err);
329                 fh_unlock(fhp);
330         }
331         if (size_change)
332                 put_write_access(inode);
333         if (!err)
334                 if (EX_ISSYNC(fhp->fh_export))
335                         write_inode_now(inode, 1);
336 out:
337         return err;
338
339 out_nfserr:
340         err = nfserrno(err);
341         goto out;
342 }
343
344 #ifdef CONFIG_NFSD_V3
345 /*
346  * Check server access rights to a file system object
347  */
348 struct accessmap {
349         u32             access;
350         int             how;
351 };
352 static struct accessmap nfs3_regaccess[] = {
353     {   NFS3_ACCESS_READ,       MAY_READ                        },
354     {   NFS3_ACCESS_EXECUTE,    MAY_EXEC                        },
355     {   NFS3_ACCESS_MODIFY,     MAY_WRITE|MAY_TRUNC             },
356     {   NFS3_ACCESS_EXTEND,     MAY_WRITE                       },
357
358     {   0,                      0                               }
359 };
360
361 static struct accessmap nfs3_diraccess[] = {
362     {   NFS3_ACCESS_READ,       MAY_READ                        },
363     {   NFS3_ACCESS_LOOKUP,     MAY_EXEC                        },
364     {   NFS3_ACCESS_MODIFY,     MAY_EXEC|MAY_WRITE|MAY_TRUNC    },
365     {   NFS3_ACCESS_EXTEND,     MAY_EXEC|MAY_WRITE              },
366     {   NFS3_ACCESS_DELETE,     MAY_REMOVE                      },
367
368     {   0,                      0                               }
369 };
370
371 static struct accessmap nfs3_anyaccess[] = {
372         /* Some clients - Solaris 2.6 at least, make an access call
373          * to the server to check for access for things like /dev/null
374          * (which really, the server doesn't care about).  So
375          * We provide simple access checking for them, looking
376          * mainly at mode bits
377          */
378     {   NFS3_ACCESS_READ,       MAY_READ                        },
379     {   NFS3_ACCESS_EXECUTE,    MAY_EXEC                        },
380     {   NFS3_ACCESS_MODIFY,     MAY_WRITE                       },
381     {   NFS3_ACCESS_EXTEND,     MAY_WRITE                       },
382
383     {   0,                      0                               }
384 };
385
386 int
387 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
388 {
389         struct accessmap        *map;
390         struct svc_export       *export;
391         struct dentry           *dentry;
392         u32                     query, result = 0, sresult = 0;
393         unsigned int            error;
394
395         error = fh_verify(rqstp, fhp, 0, MAY_NOP);
396         if (error)
397                 goto out;
398
399         export = fhp->fh_export;
400         dentry = fhp->fh_dentry;
401
402         if (S_ISREG(dentry->d_inode->i_mode))
403                 map = nfs3_regaccess;
404         else if (S_ISDIR(dentry->d_inode->i_mode))
405                 map = nfs3_diraccess;
406         else
407                 map = nfs3_anyaccess;
408
409
410         query = *access;
411         for  (; map->access; map++) {
412                 if (map->access & query) {
413                         unsigned int err2;
414
415                         sresult |= map->access;
416
417                         err2 = nfsd_permission(export, dentry, map->how);
418                         switch (err2) {
419                         case nfs_ok:
420                                 result |= map->access;
421                                 break;
422                                 
423                         /* the following error codes just mean the access was not allowed,
424                          * rather than an error occurred */
425                         case nfserr_rofs:
426                         case nfserr_acces:
427                         case nfserr_perm:
428                                 /* simply don't "or" in the access bit. */
429                                 break;
430                         default:
431                                 error = err2;
432                                 goto out;
433                         }
434                 }
435         }
436         *access = result;
437         if (supported)
438                 *supported = sresult;
439
440  out:
441         return error;
442 }
443 #endif /* CONFIG_NFSD_V3 */
444
445
446
447 /*
448  * Open an existing file or directory.
449  * The access argument indicates the type of open (read/write/lock)
450  * N.B. After this call fhp needs an fh_put
451  */
452 int
453 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
454                         int access, struct file *filp)
455 {
456         struct dentry   *dentry;
457         struct inode    *inode;
458         int             flags = O_RDONLY|O_LARGEFILE, err;
459
460         /*
461          * If we get here, then the client has already done an "open",
462          * and (hopefully) checked permission - so allow OWNER_OVERRIDE
463          * in case a chmod has now revoked permission.
464          */
465         err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
466         if (err)
467                 goto out;
468
469         dentry = fhp->fh_dentry;
470         inode = dentry->d_inode;
471
472         /* Disallow access to files with the append-only bit set or
473          * with mandatory locking enabled
474          */
475         err = nfserr_perm;
476         if (IS_APPEND(inode) || IS_ISMNDLK(inode))
477                 goto out;
478         if (!inode->i_fop)
479                 goto out;
480
481         /*
482          * Check to see if there are any leases on this file.
483          * This may block while leases are broken.
484          */
485         err = break_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
486         if (err)
487                 goto out_nfserr;
488
489         if (access & MAY_WRITE) {
490                 err = get_write_access(inode);
491                 if (err)
492                         goto out_nfserr;
493
494                 flags = O_WRONLY|O_LARGEFILE;
495
496                 DQUOT_INIT(inode);
497         }
498
499         err = open_private_file(filp, dentry, flags);
500         if (!err) {
501                 filp->f_vfsmnt = fhp->fh_export->ex_mnt;
502         } else if (access & MAY_WRITE)
503                 put_write_access(inode);
504
505 out_nfserr:
506         if (err)
507                 err = nfserrno(err);
508 out:
509         return err;
510 }
511
512 /*
513  * Close a file.
514  */
515 void
516 nfsd_close(struct file *filp)
517 {
518         struct dentry   *dentry = filp->f_dentry;
519         struct inode    *inode = dentry->d_inode;
520
521         close_private_file(filp);
522         if (filp->f_mode & FMODE_WRITE)
523                 put_write_access(inode);
524 }
525
526 /*
527  * Sync a file
528  * As this calls fsync (not fdatasync) there is no need for a write_inode
529  * after it.
530  */
531 inline void nfsd_dosync(struct file *filp, struct dentry *dp, 
532                         struct file_operations *fop)
533 {
534         struct inode *inode = dp->d_inode;
535         int (*fsync) (struct file *, struct dentry *, int);
536
537         filemap_fdatawrite(inode->i_mapping);
538         if (fop && (fsync = fop->fsync))
539                 fsync(filp, dp, 0);
540         filemap_fdatawait(inode->i_mapping);
541 }
542         
543
544 void
545 nfsd_sync(struct file *filp)
546 {
547         struct inode *inode = filp->f_dentry->d_inode;
548         dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
549         down(&inode->i_sem);
550         nfsd_dosync(filp, filp->f_dentry, filp->f_op);
551         up(&inode->i_sem);
552 }
553
554 void
555 nfsd_sync_dir(struct dentry *dp)
556 {
557         nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
558 }
559
560 /*
561  * Obtain the readahead parameters for the file
562  * specified by (dev, ino).
563  */
564 static spinlock_t ra_lock = SPIN_LOCK_UNLOCKED;
565
566 static inline struct raparms *
567 nfsd_get_raparms(dev_t dev, ino_t ino)
568 {
569         struct raparms  *ra, **rap, **frap = NULL;
570         int depth = 0;
571
572         spin_lock(&ra_lock);
573         for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
574                 if (ra->p_ino == ino && ra->p_dev == dev)
575                         goto found;
576                 depth++;
577                 if (ra->p_count == 0)
578                         frap = rap;
579         }
580         depth = nfsdstats.ra_size*11/10;
581         if (!frap) {    
582                 spin_unlock(&ra_lock);
583                 return NULL;
584         }
585         rap = frap;
586         ra = *frap;
587         ra->p_dev = dev;
588         ra->p_ino = ino;
589         memset(&ra->p_ra, 0, sizeof(ra->p_ra));
590 found:
591         if (rap != &raparm_cache) {
592                 *rap = ra->p_next;
593                 ra->p_next   = raparm_cache;
594                 raparm_cache = ra;
595         }
596         ra->p_count++;
597         nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
598         spin_unlock(&ra_lock);
599         return ra;
600 }
601
602 /*
603  * Grab and keep cached pages assosiated with a file in the svc_rqst
604  * so that they can be passed to the netowork sendmsg/sendpage routines
605  * directrly. They will be released after the sending has completed.
606  */
607 static int
608 nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size)
609 {
610         unsigned long count = desc->count;
611         struct svc_rqst *rqstp = (struct svc_rqst *)desc->buf;
612
613         if (size > count)
614                 size = count;
615
616         if (rqstp->rq_res.page_len == 0) {
617                 get_page(page);
618                 rqstp->rq_respages[rqstp->rq_resused++] = page;
619                 rqstp->rq_res.page_base = offset;
620                 rqstp->rq_res.page_len = size;
621         } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) {
622                 get_page(page);
623                 rqstp->rq_respages[rqstp->rq_resused++] = page;
624                 rqstp->rq_res.page_len += size;
625         } else {
626                 rqstp->rq_res.page_len += size;
627         }
628
629         desc->count = count - size;
630         desc->written += size;
631         return size;
632 }
633
634 /*
635  * Read data from a file. count must contain the requested read count
636  * on entry. On return, *count contains the number of bytes actually read.
637  * N.B. After this call fhp needs an fh_put
638  */
639 int
640 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
641           struct iovec *vec, int vlen, unsigned long *count)
642 {
643         struct raparms  *ra;
644         mm_segment_t    oldfs;
645         int             err;
646         struct file     file;
647         struct inode    *inode;
648
649         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
650         if (err)
651                 goto out;
652         err = nfserr_perm;
653         inode = file.f_dentry->d_inode;
654 #ifdef MSNFS
655         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
656                 (!lock_may_read(inode, offset, *count)))
657                 goto out_close;
658 #endif
659
660         /* Get readahead parameters */
661         ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
662         if (ra)
663                 file.f_ra = ra->p_ra;
664
665         if (file.f_op->sendfile) {
666                 svc_pushback_unused_pages(rqstp);
667                 err = file.f_op->sendfile(&file, &offset, *count,
668                                                  nfsd_read_actor, rqstp);
669         } else {
670                 oldfs = get_fs();
671                 set_fs(KERNEL_DS);
672                 err = vfs_readv(&file, vec, vlen, &offset);
673                 set_fs(oldfs);
674         }
675
676         /* Write back readahead params */
677         if (ra)
678                 ra->p_ra = file.f_ra;
679
680         if (err >= 0) {
681                 nfsdstats.io_read += err;
682                 *count = err;
683                 err = 0;
684                 dnotify_parent(file.f_dentry, DN_ACCESS);
685         } else 
686                 err = nfserrno(err);
687 out_close:
688         nfsd_close(&file);
689 out:
690         return err;
691 }
692
693 /*
694  * Write data to a file.
695  * The stable flag requests synchronous writes.
696  * N.B. After this call fhp needs an fh_put
697  */
698 int
699 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
700                                 struct iovec *vec, int vlen,
701                                 unsigned long cnt, int *stablep)
702 {
703         struct svc_export       *exp;
704         struct file             file;
705         struct dentry           *dentry;
706         struct inode            *inode;
707         mm_segment_t            oldfs;
708         int                     err = 0;
709         int                     stable = *stablep;
710
711         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
712         if (err)
713                 goto out;
714         if (!cnt)
715                 goto out_close;
716         err = nfserr_perm;
717
718 #ifdef MSNFS
719         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
720                 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
721                 goto out_close;
722 #endif
723
724         dentry = file.f_dentry;
725         inode = dentry->d_inode;
726         exp   = fhp->fh_export;
727
728         /*
729          * Request sync writes if
730          *  -   the sync export option has been set, or
731          *  -   the client requested O_SYNC behavior (NFSv3 feature).
732          *  -   The file system doesn't support fsync().
733          * When gathered writes have been configured for this volume,
734          * flushing the data to disk is handled separately below.
735          */
736
737         if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
738                stable = 2;
739                *stablep = 2; /* FILE_SYNC */
740         }
741
742         if (!EX_ISSYNC(exp))
743                 stable = 0;
744         if (stable && !EX_WGATHER(exp))
745                 file.f_flags |= O_SYNC;
746
747         /* Write the data. */
748         oldfs = get_fs(); set_fs(KERNEL_DS);
749         err = vfs_writev(&file, vec, vlen, &offset);
750         set_fs(oldfs);
751         if (err >= 0) {
752                 nfsdstats.io_write += cnt;
753                 dnotify_parent(file.f_dentry, DN_MODIFY);
754         }
755
756         /* clear setuid/setgid flag after write */
757         if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
758                 struct iattr    ia;
759                 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
760
761                 down(&inode->i_sem);
762                 notify_change(dentry, &ia);
763                 up(&inode->i_sem);
764         }
765
766         if (err >= 0 && stable) {
767                 static ino_t    last_ino;
768                 static dev_t    last_dev;
769
770                 /*
771                  * Gathered writes: If another process is currently
772                  * writing to the file, there's a high chance
773                  * this is another nfsd (triggered by a bulk write
774                  * from a client's biod). Rather than syncing the
775                  * file with each write request, we sleep for 10 msec.
776                  *
777                  * I don't know if this roughly approximates
778                  * C. Juszak's idea of gathered writes, but it's a
779                  * nice and simple solution (IMHO), and it seems to
780                  * work:-)
781                  */
782                 if (EX_WGATHER(exp)) {
783                         if (atomic_read(&inode->i_writecount) > 1
784                             || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
785                                 dprintk("nfsd: write defer %d\n", current->pid);
786                                 set_current_state(TASK_UNINTERRUPTIBLE);
787                                 schedule_timeout((HZ+99)/100);
788                                 dprintk("nfsd: write resume %d\n", current->pid);
789                         }
790
791                         if (inode->i_state & I_DIRTY) {
792                                 dprintk("nfsd: write sync %d\n", current->pid);
793                                 nfsd_sync(&file);
794                         }
795 #if 0
796                         wake_up(&inode->i_wait);
797 #endif
798                 }
799                 last_ino = inode->i_ino;
800                 last_dev = inode->i_sb->s_dev;
801         }
802
803         dprintk("nfsd: write complete err=%d\n", err);
804         if (err >= 0)
805                 err = 0;
806         else 
807                 err = nfserrno(err);
808 out_close:
809         nfsd_close(&file);
810 out:
811         return err;
812 }
813
814
815 #ifdef CONFIG_NFSD_V3
816 /*
817  * Commit all pending writes to stable storage.
818  * Strictly speaking, we could sync just the indicated file region here,
819  * but there's currently no way we can ask the VFS to do so.
820  *
821  * Unfortunately we cannot lock the file to make sure we return full WCC
822  * data to the client, as locking happens lower down in the filesystem.
823  */
824 int
825 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
826                loff_t offset, unsigned long count)
827 {
828         struct file     file;
829         int             err;
830
831         if ((u64)count > ~(u64)offset)
832                 return nfserr_inval;
833
834         if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
835                 return err;
836         if (EX_ISSYNC(fhp->fh_export)) {
837                 if (file.f_op && file.f_op->fsync) {
838                         nfsd_sync(&file);
839                 } else {
840                         err = nfserr_notsupp;
841                 }
842         }
843
844         nfsd_close(&file);
845         return err;
846 }
847 #endif /* CONFIG_NFSD_V3 */
848
849 /*
850  * Create a file (regular, directory, device, fifo); UNIX sockets 
851  * not yet implemented.
852  * If the response fh has been verified, the parent directory should
853  * already be locked. Note that the parent directory is left locked.
854  *
855  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
856  */
857 int
858 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
859                 char *fname, int flen, struct iattr *iap,
860                 int type, dev_t rdev, struct svc_fh *resfhp)
861 {
862         struct dentry   *dentry, *dchild;
863         struct inode    *dirp;
864         int             err;
865
866         err = nfserr_perm;
867         if (!flen)
868                 goto out;
869         err = nfserr_exist;
870         if (isdotent(fname, flen))
871                 goto out;
872
873         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
874         if (err)
875                 goto out;
876
877         dentry = fhp->fh_dentry;
878         dirp = dentry->d_inode;
879
880         err = nfserr_notdir;
881         if(!dirp->i_op || !dirp->i_op->lookup)
882                 goto out;
883         /*
884          * Check whether the response file handle has been verified yet.
885          * If it has, the parent directory should already be locked.
886          */
887         if (!resfhp->fh_dentry) {
888                 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
889                 fh_lock(fhp);
890                 dchild = lookup_one_len(fname, dentry, flen);
891                 err = PTR_ERR(dchild);
892                 if (IS_ERR(dchild))
893                         goto out_nfserr;
894                 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
895                 if (err)
896                         goto out;
897         } else {
898                 /* called from nfsd_proc_create */
899                 dchild = resfhp->fh_dentry;
900                 if (!fhp->fh_locked) {
901                         /* not actually possible */
902                         printk(KERN_ERR
903                                 "nfsd_create: parent %s/%s not locked!\n",
904                                 dentry->d_parent->d_name.name,
905                                 dentry->d_name.name);
906                         err = -EIO;
907                         goto out;
908                 }
909         }
910         /*
911          * Make sure the child dentry is still negative ...
912          */
913         err = nfserr_exist;
914         if (dchild->d_inode) {
915                 dprintk("nfsd_create: dentry %s/%s not negative!\n",
916                         dentry->d_name.name, dchild->d_name.name);
917                 goto out; 
918         }
919
920         if (!(iap->ia_valid & ATTR_MODE))
921                 iap->ia_mode = 0;
922         iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
923
924         /*
925          * Get the dir op function pointer.
926          */
927         err = nfserr_perm;
928         switch (type) {
929         case S_IFREG:
930                 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
931                 break;
932         case S_IFDIR:
933                 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
934                 break;
935         case S_IFCHR:
936         case S_IFBLK:
937         case S_IFIFO:
938         case S_IFSOCK:
939                 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
940                 break;
941         default:
942                 printk("nfsd: bad file type %o in nfsd_create\n", type);
943                 err = -EINVAL;
944         }
945         if (err < 0)
946                 goto out_nfserr;
947
948         if (EX_ISSYNC(fhp->fh_export)) {
949                 nfsd_sync_dir(dentry);
950                 write_inode_now(dchild->d_inode, 1);
951         }
952
953
954         /* Set file attributes. Mode has already been set and
955          * setting uid/gid works only for root. Irix appears to
956          * send along the gid when it tries to implement setgid
957          * directories via NFS.
958          */
959         err = 0;
960         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
961                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
962         /*
963          * Update the file handle to get the new inode info.
964          */
965         if (!err)
966                 err = fh_update(resfhp);
967 out:
968         return err;
969
970 out_nfserr:
971         err = nfserrno(err);
972         goto out;
973 }
974
975 #ifdef CONFIG_NFSD_V3
976 /*
977  * NFSv3 version of nfsd_create
978  */
979 int
980 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
981                 char *fname, int flen, struct iattr *iap,
982                 struct svc_fh *resfhp, int createmode, u32 *verifier,
983                 int *truncp)
984 {
985         struct dentry   *dentry, *dchild;
986         struct inode    *dirp;
987         int             err;
988         __u32           v_mtime=0, v_atime=0;
989         int             v_mode=0;
990
991         err = nfserr_perm;
992         if (!flen)
993                 goto out;
994         err = nfserr_exist;
995         if (isdotent(fname, flen))
996                 goto out;
997         if (!(iap->ia_valid & ATTR_MODE))
998                 iap->ia_mode = 0;
999         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1000         if (err)
1001                 goto out;
1002
1003         dentry = fhp->fh_dentry;
1004         dirp = dentry->d_inode;
1005
1006         /* Get all the sanity checks out of the way before
1007          * we lock the parent. */
1008         err = nfserr_notdir;
1009         if(!dirp->i_op || !dirp->i_op->lookup)
1010                 goto out;
1011         fh_lock(fhp);
1012
1013         /*
1014          * Compose the response file handle.
1015          */
1016         dchild = lookup_one_len(fname, dentry, flen);
1017         err = PTR_ERR(dchild);
1018         if (IS_ERR(dchild))
1019                 goto out_nfserr;
1020
1021         err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1022         if (err)
1023                 goto out;
1024
1025         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1026                 /* while the verifier would fit in mtime+atime,
1027                  * solaris7 gets confused (bugid 4218508) if these have
1028                  * the high bit set, so we use the mode as well
1029                  */
1030                 v_mtime = verifier[0]&0x7fffffff;
1031                 v_atime = verifier[1]&0x7fffffff;
1032                 v_mode  = S_IFREG
1033                         | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1034                         | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1035                         ;
1036         }
1037         
1038         if (dchild->d_inode) {
1039                 err = 0;
1040
1041                 switch (createmode) {
1042                 case NFS3_CREATE_UNCHECKED:
1043                         if (! S_ISREG(dchild->d_inode->i_mode))
1044                                 err = nfserr_exist;
1045                         else if (truncp) {
1046                                 /* in nfsv4, we need to treat this case a little
1047                                  * differently.  we don't want to truncate the
1048                                  * file now; this would be wrong if the OPEN
1049                                  * fails for some other reason.  furthermore,
1050                                  * if the size is nonzero, we should ignore it
1051                                  * according to spec!
1052                                  */
1053                                 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1054                         }
1055                         else {
1056                                 iap->ia_valid &= ATTR_SIZE;
1057                                 goto set_attr;
1058                         }
1059                         break;
1060                 case NFS3_CREATE_EXCLUSIVE:
1061                         if (   dchild->d_inode->i_mtime.tv_sec == v_mtime
1062                             && dchild->d_inode->i_atime.tv_sec == v_atime
1063                             && dchild->d_inode->i_mode  == v_mode
1064                             && dchild->d_inode->i_size  == 0 )
1065                                 break;
1066                          /* fallthru */
1067                 case NFS3_CREATE_GUARDED:
1068                         err = nfserr_exist;
1069                 }
1070                 goto out;
1071         }
1072
1073         err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1074         if (err < 0)
1075                 goto out_nfserr;
1076
1077         if (EX_ISSYNC(fhp->fh_export)) {
1078                 nfsd_sync_dir(dentry);
1079                 /* setattr will sync the child (or not) */
1080         }
1081
1082         /*
1083          * Update the filehandle to get the new inode info.
1084          */
1085         err = fh_update(resfhp);
1086         if (err)
1087                 goto out;
1088
1089         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1090                 /* Cram the verifier into atime/mtime/mode */
1091                 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1092                         | ATTR_MTIME_SET|ATTR_ATIME_SET
1093                         | ATTR_MODE;
1094                 /* XXX someone who knows this better please fix it for nsec */ 
1095                 iap->ia_mtime.tv_sec = v_mtime;
1096                 iap->ia_atime.tv_sec = v_atime;
1097                 iap->ia_mtime.tv_nsec = 0;
1098                 iap->ia_atime.tv_nsec = 0;
1099                 iap->ia_mode  = v_mode;
1100         }
1101
1102         /* Set file attributes.
1103          * Mode has already been set but we might need to reset it
1104          * for CREATE_EXCLUSIVE
1105          * Irix appears to send along the gid when it tries to
1106          * implement setgid directories via NFS. Clear out all that cruft.
1107          */
1108  set_attr:
1109         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1110                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1111
1112  out:
1113         fh_unlock(fhp);
1114         return err;
1115  
1116  out_nfserr:
1117         err = nfserrno(err);
1118         goto out;
1119 }
1120 #endif /* CONFIG_NFSD_V3 */
1121
1122 /*
1123  * Read a symlink. On entry, *lenp must contain the maximum path length that
1124  * fits into the buffer. On return, it contains the true length.
1125  * N.B. After this call fhp needs an fh_put
1126  */
1127 int
1128 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1129 {
1130         struct dentry   *dentry;
1131         struct inode    *inode;
1132         mm_segment_t    oldfs;
1133         int             err;
1134
1135         err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1136         if (err)
1137                 goto out;
1138
1139         dentry = fhp->fh_dentry;
1140         inode = dentry->d_inode;
1141
1142         err = nfserr_inval;
1143         if (!inode->i_op || !inode->i_op->readlink)
1144                 goto out;
1145
1146         touch_atime(fhp->fh_export->ex_mnt, dentry);
1147         /* N.B. Why does this call need a get_fs()??
1148          * Remove the set_fs and watch the fireworks:-) --okir
1149          */
1150
1151         oldfs = get_fs(); set_fs(KERNEL_DS);
1152         err = inode->i_op->readlink(dentry, buf, *lenp);
1153         set_fs(oldfs);
1154
1155         if (err < 0)
1156                 goto out_nfserr;
1157         *lenp = err;
1158         err = 0;
1159 out:
1160         return err;
1161
1162 out_nfserr:
1163         err = nfserrno(err);
1164         goto out;
1165 }
1166
1167 /*
1168  * Create a symlink and look up its inode
1169  * N.B. After this call _both_ fhp and resfhp need an fh_put
1170  */
1171 int
1172 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1173                                 char *fname, int flen,
1174                                 char *path,  int plen,
1175                                 struct svc_fh *resfhp,
1176                                 struct iattr *iap)
1177 {
1178         struct dentry   *dentry, *dnew;
1179         int             err, cerr;
1180
1181         err = nfserr_noent;
1182         if (!flen || !plen)
1183                 goto out;
1184         err = nfserr_exist;
1185         if (isdotent(fname, flen))
1186                 goto out;
1187
1188         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1189         if (err)
1190                 goto out;
1191         fh_lock(fhp);
1192         dentry = fhp->fh_dentry;
1193         dnew = lookup_one_len(fname, dentry, flen);
1194         err = PTR_ERR(dnew);
1195         if (IS_ERR(dnew))
1196                 goto out_nfserr;
1197
1198         if (unlikely(path[plen] != 0)) {
1199                 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1200                 if (path_alloced == NULL)
1201                         err = -ENOMEM;
1202                 else {
1203                         strncpy(path_alloced, path, plen);
1204                         path_alloced[plen] = 0;
1205                         err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1206                         kfree(path_alloced);
1207                 }
1208         } else
1209                 err = vfs_symlink(dentry->d_inode, dnew, path);
1210
1211         if (!err) {
1212                 if (EX_ISSYNC(fhp->fh_export))
1213                         nfsd_sync_dir(dentry);
1214                 if (iap) {
1215                         iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1216                         if (iap->ia_valid) {
1217                                 iap->ia_valid |= ATTR_CTIME;
1218                                 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1219                                         | S_IFLNK;
1220                                 err = notify_change(dnew, iap);
1221                                 if (err)
1222                                         err = nfserrno(err);
1223                                 else if (EX_ISSYNC(fhp->fh_export))
1224                                         write_inode_now(dentry->d_inode, 1);
1225                        }
1226                 }
1227         } else
1228                 err = nfserrno(err);
1229         fh_unlock(fhp);
1230
1231         /* Compose the fh so the dentry will be freed ... */
1232         cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
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 }