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