nfsd4: reshuffle lease-setting code to allow reuse
[safe/jmp/linux-2.6] / fs / nfsd / vfs.c
1 #define MSNFS   /* HACK HACK */
2 /*
3  * File operations used by nfsd. Some of these have been ripped from
4  * other parts of the kernel because they weren't exported, others
5  * are partial duplicates with added or changed functionality.
6  *
7  * Note that several functions dget() the dentry upon which they want
8  * to act, most notably those that create directory entries. Response
9  * dentry's are dput()'d if necessary in the release callback.
10  * So if you notice code paths that apparently fail to dput() the
11  * dentry, don't worry--they have been taken care of.
12  *
13  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
14  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
15  */
16
17 #include <linux/fs.h>
18 #include <linux/file.h>
19 #include <linux/splice.h>
20 #include <linux/fcntl.h>
21 #include <linux/namei.h>
22 #include <linux/delay.h>
23 #include <linux/quotaops.h>
24 #include <linux/fsnotify.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/xattr.h>
27 #include <linux/jhash.h>
28 #include <linux/ima.h>
29 #include <asm/uaccess.h>
30 #include <linux/exportfs.h>
31 #include <linux/writeback.h>
32
33 #ifdef CONFIG_NFSD_V3
34 #include "xdr3.h"
35 #endif /* CONFIG_NFSD_V3 */
36
37 #ifdef CONFIG_NFSD_V4
38 #include <linux/nfs4_acl.h>
39 #include <linux/nfsd_idmap.h>
40 #endif /* CONFIG_NFSD_V4 */
41
42 #include "nfsd.h"
43 #include "vfs.h"
44
45 #define NFSDDBG_FACILITY                NFSDDBG_FILEOP
46
47
48 /*
49  * This is a cache of readahead params that help us choose the proper
50  * readahead strategy. Initially, we set all readahead parameters to 0
51  * and let the VFS handle things.
52  * If you increase the number of cached files very much, you'll need to
53  * add a hash table here.
54  */
55 struct raparms {
56         struct raparms          *p_next;
57         unsigned int            p_count;
58         ino_t                   p_ino;
59         dev_t                   p_dev;
60         int                     p_set;
61         struct file_ra_state    p_ra;
62         unsigned int            p_hindex;
63 };
64
65 struct raparm_hbucket {
66         struct raparms          *pb_head;
67         spinlock_t              pb_lock;
68 } ____cacheline_aligned_in_smp;
69
70 #define RAPARM_HASH_BITS        4
71 #define RAPARM_HASH_SIZE        (1<<RAPARM_HASH_BITS)
72 #define RAPARM_HASH_MASK        (RAPARM_HASH_SIZE-1)
73 static struct raparm_hbucket    raparm_hash[RAPARM_HASH_SIZE];
74
75 /* 
76  * Called from nfsd_lookup and encode_dirent. Check if we have crossed 
77  * a mount point.
78  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
79  *  or nfs_ok having possibly changed *dpp and *expp
80  */
81 int
82 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, 
83                         struct svc_export **expp)
84 {
85         struct svc_export *exp = *expp, *exp2 = NULL;
86         struct dentry *dentry = *dpp;
87         struct path path = {.mnt = mntget(exp->ex_path.mnt),
88                             .dentry = dget(dentry)};
89         int err = 0;
90
91         while (d_mountpoint(path.dentry) && follow_down(&path))
92                 ;
93
94         exp2 = rqst_exp_get_by_name(rqstp, &path);
95         if (IS_ERR(exp2)) {
96                 err = PTR_ERR(exp2);
97                 /*
98                  * We normally allow NFS clients to continue
99                  * "underneath" a mountpoint that is not exported.
100                  * The exception is V4ROOT, where no traversal is ever
101                  * allowed without an explicit export of the new
102                  * directory.
103                  */
104                 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
105                         err = 0;
106                 path_put(&path);
107                 goto out;
108         }
109         if (nfsd_v4client(rqstp) ||
110                 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
111                 /* successfully crossed mount point */
112                 /*
113                  * This is subtle: path.dentry is *not* on path.mnt
114                  * at this point.  The only reason we are safe is that
115                  * original mnt is pinned down by exp, so we should
116                  * put path *before* putting exp
117                  */
118                 *dpp = path.dentry;
119                 path.dentry = dentry;
120                 *expp = exp2;
121                 exp2 = exp;
122         }
123         path_put(&path);
124         exp_put(exp2);
125 out:
126         return err;
127 }
128
129 static void follow_to_parent(struct path *path)
130 {
131         struct dentry *dp;
132
133         while (path->dentry == path->mnt->mnt_root && follow_up(path))
134                 ;
135         dp = dget_parent(path->dentry);
136         dput(path->dentry);
137         path->dentry = dp;
138 }
139
140 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
141 {
142         struct svc_export *exp2;
143         struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
144                             .dentry = dget(dparent)};
145
146         follow_to_parent(&path);
147
148         exp2 = rqst_exp_parent(rqstp, &path);
149         if (PTR_ERR(exp2) == -ENOENT) {
150                 *dentryp = dget(dparent);
151         } else if (IS_ERR(exp2)) {
152                 path_put(&path);
153                 return PTR_ERR(exp2);
154         } else {
155                 *dentryp = dget(path.dentry);
156                 exp_put(*exp);
157                 *exp = exp2;
158         }
159         path_put(&path);
160         return 0;
161 }
162
163 /*
164  * For nfsd purposes, we treat V4ROOT exports as though there was an
165  * export at *every* directory.
166  */
167 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
168 {
169         if (d_mountpoint(dentry))
170                 return 1;
171         if (!(exp->ex_flags & NFSEXP_V4ROOT))
172                 return 0;
173         return dentry->d_inode != NULL;
174 }
175
176 __be32
177 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
178                    const char *name, unsigned int len,
179                    struct svc_export **exp_ret, struct dentry **dentry_ret)
180 {
181         struct svc_export       *exp;
182         struct dentry           *dparent;
183         struct dentry           *dentry;
184         __be32                  err;
185         int                     host_err;
186
187         dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
188
189         /* Obtain dentry and export. */
190         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
191         if (err)
192                 return err;
193
194         dparent = fhp->fh_dentry;
195         exp  = fhp->fh_export;
196         exp_get(exp);
197
198         /* Lookup the name, but don't follow links */
199         if (isdotent(name, len)) {
200                 if (len==1)
201                         dentry = dget(dparent);
202                 else if (dparent != exp->ex_path.dentry)
203                         dentry = dget_parent(dparent);
204                 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
205                         dentry = dget(dparent); /* .. == . just like at / */
206                 else {
207                         /* checking mountpoint crossing is very different when stepping up */
208                         host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
209                         if (host_err)
210                                 goto out_nfserr;
211                 }
212         } else {
213                 fh_lock(fhp);
214                 dentry = lookup_one_len(name, dparent, len);
215                 host_err = PTR_ERR(dentry);
216                 if (IS_ERR(dentry))
217                         goto out_nfserr;
218                 /*
219                  * check if we have crossed a mount point ...
220                  */
221                 if (nfsd_mountpoint(dentry, exp)) {
222                         if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
223                                 dput(dentry);
224                                 goto out_nfserr;
225                         }
226                 }
227         }
228         *dentry_ret = dentry;
229         *exp_ret = exp;
230         return 0;
231
232 out_nfserr:
233         exp_put(exp);
234         return nfserrno(host_err);
235 }
236
237 /*
238  * Look up one component of a pathname.
239  * N.B. After this call _both_ fhp and resfh need an fh_put
240  *
241  * If the lookup would cross a mountpoint, and the mounted filesystem
242  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
243  * accepted as it stands and the mounted directory is
244  * returned. Otherwise the covered directory is returned.
245  * NOTE: this mountpoint crossing is not supported properly by all
246  *   clients and is explicitly disallowed for NFSv3
247  *      NeilBrown <neilb@cse.unsw.edu.au>
248  */
249 __be32
250 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
251                                 unsigned int len, struct svc_fh *resfh)
252 {
253         struct svc_export       *exp;
254         struct dentry           *dentry;
255         __be32 err;
256
257         err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
258         if (err)
259                 return err;
260         err = check_nfsd_access(exp, rqstp);
261         if (err)
262                 goto out;
263         /*
264          * Note: we compose the file handle now, but as the
265          * dentry may be negative, it may need to be updated.
266          */
267         err = fh_compose(resfh, exp, dentry, fhp);
268         if (!err && !dentry->d_inode)
269                 err = nfserr_noent;
270 out:
271         dput(dentry);
272         exp_put(exp);
273         return err;
274 }
275
276 /*
277  * Commit metadata changes to stable storage.
278  */
279 static int
280 commit_metadata(struct svc_fh *fhp)
281 {
282         struct inode *inode = fhp->fh_dentry->d_inode;
283         const struct export_operations *export_ops = inode->i_sb->s_export_op;
284         int error = 0;
285
286         if (!EX_ISSYNC(fhp->fh_export))
287                 return 0;
288
289         if (export_ops->commit_metadata) {
290                 error = export_ops->commit_metadata(inode);
291         } else {
292                 struct writeback_control wbc = {
293                         .sync_mode = WB_SYNC_ALL,
294                         .nr_to_write = 0, /* metadata only */
295                 };
296
297                 error = sync_inode(inode, &wbc);
298         }
299
300         return error;
301 }
302
303 /*
304  * Set various file attributes.
305  * N.B. After this call fhp needs an fh_put
306  */
307 __be32
308 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
309              int check_guard, time_t guardtime)
310 {
311         struct dentry   *dentry;
312         struct inode    *inode;
313         int             accmode = NFSD_MAY_SATTR;
314         int             ftype = 0;
315         __be32          err;
316         int             host_err;
317         int             size_change = 0;
318
319         if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
320                 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
321         if (iap->ia_valid & ATTR_SIZE)
322                 ftype = S_IFREG;
323
324         /* Get inode */
325         err = fh_verify(rqstp, fhp, ftype, accmode);
326         if (err)
327                 goto out;
328
329         dentry = fhp->fh_dentry;
330         inode = dentry->d_inode;
331
332         /* Ignore any mode updates on symlinks */
333         if (S_ISLNK(inode->i_mode))
334                 iap->ia_valid &= ~ATTR_MODE;
335
336         if (!iap->ia_valid)
337                 goto out;
338
339         /*
340          * NFSv2 does not differentiate between "set-[ac]time-to-now"
341          * which only requires access, and "set-[ac]time-to-X" which
342          * requires ownership.
343          * So if it looks like it might be "set both to the same time which
344          * is close to now", and if inode_change_ok fails, then we
345          * convert to "set to now" instead of "set to explicit time"
346          *
347          * We only call inode_change_ok as the last test as technically
348          * it is not an interface that we should be using.  It is only
349          * valid if the filesystem does not define it's own i_op->setattr.
350          */
351 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
352 #define MAX_TOUCH_TIME_ERROR (30*60)
353         if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
354             iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
355                 /*
356                  * Looks probable.
357                  *
358                  * Now just make sure time is in the right ballpark.
359                  * Solaris, at least, doesn't seem to care what the time
360                  * request is.  We require it be within 30 minutes of now.
361                  */
362                 time_t delta = iap->ia_atime.tv_sec - get_seconds();
363                 if (delta < 0)
364                         delta = -delta;
365                 if (delta < MAX_TOUCH_TIME_ERROR &&
366                     inode_change_ok(inode, iap) != 0) {
367                         /*
368                          * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
369                          * This will cause notify_change to set these times
370                          * to "now"
371                          */
372                         iap->ia_valid &= ~BOTH_TIME_SET;
373                 }
374         }
375             
376         /*
377          * The size case is special.
378          * It changes the file as well as the attributes.
379          */
380         if (iap->ia_valid & ATTR_SIZE) {
381                 if (iap->ia_size < inode->i_size) {
382                         err = nfsd_permission(rqstp, fhp->fh_export, dentry,
383                                         NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
384                         if (err)
385                                 goto out;
386                 }
387
388                 /*
389                  * If we are changing the size of the file, then
390                  * we need to break all leases.
391                  */
392                 host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
393                 if (host_err == -EWOULDBLOCK)
394                         host_err = -ETIMEDOUT;
395                 if (host_err) /* ENOMEM or EWOULDBLOCK */
396                         goto out_nfserr;
397
398                 host_err = get_write_access(inode);
399                 if (host_err)
400                         goto out_nfserr;
401
402                 size_change = 1;
403                 host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
404                 if (host_err) {
405                         put_write_access(inode);
406                         goto out_nfserr;
407                 }
408                 vfs_dq_init(inode);
409         }
410
411         /* sanitize the mode change */
412         if (iap->ia_valid & ATTR_MODE) {
413                 iap->ia_mode &= S_IALLUGO;
414                 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
415         }
416
417         /* Revoke setuid/setgid on chown */
418         if (!S_ISDIR(inode->i_mode) &&
419             (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
420              ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
421                 iap->ia_valid |= ATTR_KILL_PRIV;
422                 if (iap->ia_valid & ATTR_MODE) {
423                         /* we're setting mode too, just clear the s*id bits */
424                         iap->ia_mode &= ~S_ISUID;
425                         if (iap->ia_mode & S_IXGRP)
426                                 iap->ia_mode &= ~S_ISGID;
427                 } else {
428                         /* set ATTR_KILL_* bits and let VFS handle it */
429                         iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
430                 }
431         }
432
433         /* Change the attributes. */
434
435         iap->ia_valid |= ATTR_CTIME;
436
437         err = nfserr_notsync;
438         if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
439                 fh_lock(fhp);
440                 host_err = notify_change(dentry, iap);
441                 err = nfserrno(host_err);
442                 fh_unlock(fhp);
443         }
444         if (size_change)
445                 put_write_access(inode);
446         if (!err)
447                 if (EX_ISSYNC(fhp->fh_export))
448                         write_inode_now(inode, 1);
449 out:
450         return err;
451
452 out_nfserr:
453         err = nfserrno(host_err);
454         goto out;
455 }
456
457 #if defined(CONFIG_NFSD_V2_ACL) || \
458     defined(CONFIG_NFSD_V3_ACL) || \
459     defined(CONFIG_NFSD_V4)
460 static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
461 {
462         ssize_t buflen;
463         ssize_t ret;
464
465         buflen = vfs_getxattr(dentry, key, NULL, 0);
466         if (buflen <= 0)
467                 return buflen;
468
469         *buf = kmalloc(buflen, GFP_KERNEL);
470         if (!*buf)
471                 return -ENOMEM;
472
473         ret = vfs_getxattr(dentry, key, *buf, buflen);
474         if (ret < 0)
475                 kfree(*buf);
476         return ret;
477 }
478 #endif
479
480 #if defined(CONFIG_NFSD_V4)
481 static int
482 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
483 {
484         int len;
485         size_t buflen;
486         char *buf = NULL;
487         int error = 0;
488
489         buflen = posix_acl_xattr_size(pacl->a_count);
490         buf = kmalloc(buflen, GFP_KERNEL);
491         error = -ENOMEM;
492         if (buf == NULL)
493                 goto out;
494
495         len = posix_acl_to_xattr(pacl, buf, buflen);
496         if (len < 0) {
497                 error = len;
498                 goto out;
499         }
500
501         error = vfs_setxattr(dentry, key, buf, len, 0);
502 out:
503         kfree(buf);
504         return error;
505 }
506
507 __be32
508 nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
509     struct nfs4_acl *acl)
510 {
511         __be32 error;
512         int host_error;
513         struct dentry *dentry;
514         struct inode *inode;
515         struct posix_acl *pacl = NULL, *dpacl = NULL;
516         unsigned int flags = 0;
517
518         /* Get inode */
519         error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
520         if (error)
521                 return error;
522
523         dentry = fhp->fh_dentry;
524         inode = dentry->d_inode;
525         if (S_ISDIR(inode->i_mode))
526                 flags = NFS4_ACL_DIR;
527
528         host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
529         if (host_error == -EINVAL) {
530                 return nfserr_attrnotsupp;
531         } else if (host_error < 0)
532                 goto out_nfserr;
533
534         host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
535         if (host_error < 0)
536                 goto out_release;
537
538         if (S_ISDIR(inode->i_mode))
539                 host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
540
541 out_release:
542         posix_acl_release(pacl);
543         posix_acl_release(dpacl);
544 out_nfserr:
545         if (host_error == -EOPNOTSUPP)
546                 return nfserr_attrnotsupp;
547         else
548                 return nfserrno(host_error);
549 }
550
551 static struct posix_acl *
552 _get_posix_acl(struct dentry *dentry, char *key)
553 {
554         void *buf = NULL;
555         struct posix_acl *pacl = NULL;
556         int buflen;
557
558         buflen = nfsd_getxattr(dentry, key, &buf);
559         if (!buflen)
560                 buflen = -ENODATA;
561         if (buflen <= 0)
562                 return ERR_PTR(buflen);
563
564         pacl = posix_acl_from_xattr(buf, buflen);
565         kfree(buf);
566         return pacl;
567 }
568
569 int
570 nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
571 {
572         struct inode *inode = dentry->d_inode;
573         int error = 0;
574         struct posix_acl *pacl = NULL, *dpacl = NULL;
575         unsigned int flags = 0;
576
577         pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
578         if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
579                 pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
580         if (IS_ERR(pacl)) {
581                 error = PTR_ERR(pacl);
582                 pacl = NULL;
583                 goto out;
584         }
585
586         if (S_ISDIR(inode->i_mode)) {
587                 dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
588                 if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
589                         dpacl = NULL;
590                 else if (IS_ERR(dpacl)) {
591                         error = PTR_ERR(dpacl);
592                         dpacl = NULL;
593                         goto out;
594                 }
595                 flags = NFS4_ACL_DIR;
596         }
597
598         *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
599         if (IS_ERR(*acl)) {
600                 error = PTR_ERR(*acl);
601                 *acl = NULL;
602         }
603  out:
604         posix_acl_release(pacl);
605         posix_acl_release(dpacl);
606         return error;
607 }
608
609 #endif /* defined(CONFIG_NFS_V4) */
610
611 #ifdef CONFIG_NFSD_V3
612 /*
613  * Check server access rights to a file system object
614  */
615 struct accessmap {
616         u32             access;
617         int             how;
618 };
619 static struct accessmap nfs3_regaccess[] = {
620     {   NFS3_ACCESS_READ,       NFSD_MAY_READ                   },
621     {   NFS3_ACCESS_EXECUTE,    NFSD_MAY_EXEC                   },
622     {   NFS3_ACCESS_MODIFY,     NFSD_MAY_WRITE|NFSD_MAY_TRUNC   },
623     {   NFS3_ACCESS_EXTEND,     NFSD_MAY_WRITE                  },
624
625     {   0,                      0                               }
626 };
627
628 static struct accessmap nfs3_diraccess[] = {
629     {   NFS3_ACCESS_READ,       NFSD_MAY_READ                   },
630     {   NFS3_ACCESS_LOOKUP,     NFSD_MAY_EXEC                   },
631     {   NFS3_ACCESS_MODIFY,     NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
632     {   NFS3_ACCESS_EXTEND,     NFSD_MAY_EXEC|NFSD_MAY_WRITE    },
633     {   NFS3_ACCESS_DELETE,     NFSD_MAY_REMOVE                 },
634
635     {   0,                      0                               }
636 };
637
638 static struct accessmap nfs3_anyaccess[] = {
639         /* Some clients - Solaris 2.6 at least, make an access call
640          * to the server to check for access for things like /dev/null
641          * (which really, the server doesn't care about).  So
642          * We provide simple access checking for them, looking
643          * mainly at mode bits, and we make sure to ignore read-only
644          * filesystem checks
645          */
646     {   NFS3_ACCESS_READ,       NFSD_MAY_READ                   },
647     {   NFS3_ACCESS_EXECUTE,    NFSD_MAY_EXEC                   },
648     {   NFS3_ACCESS_MODIFY,     NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS    },
649     {   NFS3_ACCESS_EXTEND,     NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS    },
650
651     {   0,                      0                               }
652 };
653
654 __be32
655 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
656 {
657         struct accessmap        *map;
658         struct svc_export       *export;
659         struct dentry           *dentry;
660         u32                     query, result = 0, sresult = 0;
661         __be32                  error;
662
663         error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
664         if (error)
665                 goto out;
666
667         export = fhp->fh_export;
668         dentry = fhp->fh_dentry;
669
670         if (S_ISREG(dentry->d_inode->i_mode))
671                 map = nfs3_regaccess;
672         else if (S_ISDIR(dentry->d_inode->i_mode))
673                 map = nfs3_diraccess;
674         else
675                 map = nfs3_anyaccess;
676
677
678         query = *access;
679         for  (; map->access; map++) {
680                 if (map->access & query) {
681                         __be32 err2;
682
683                         sresult |= map->access;
684
685                         err2 = nfsd_permission(rqstp, export, dentry, map->how);
686                         switch (err2) {
687                         case nfs_ok:
688                                 result |= map->access;
689                                 break;
690                                 
691                         /* the following error codes just mean the access was not allowed,
692                          * rather than an error occurred */
693                         case nfserr_rofs:
694                         case nfserr_acces:
695                         case nfserr_perm:
696                                 /* simply don't "or" in the access bit. */
697                                 break;
698                         default:
699                                 error = err2;
700                                 goto out;
701                         }
702                 }
703         }
704         *access = result;
705         if (supported)
706                 *supported = sresult;
707
708  out:
709         return error;
710 }
711 #endif /* CONFIG_NFSD_V3 */
712
713
714
715 /*
716  * Open an existing file or directory.
717  * The access argument indicates the type of open (read/write/lock)
718  * N.B. After this call fhp needs an fh_put
719  */
720 __be32
721 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
722                         int access, struct file **filp)
723 {
724         struct dentry   *dentry;
725         struct inode    *inode;
726         int             flags = O_RDONLY|O_LARGEFILE;
727         __be32          err;
728         int             host_err;
729
730         validate_process_creds();
731
732         /*
733          * If we get here, then the client has already done an "open",
734          * and (hopefully) checked permission - so allow OWNER_OVERRIDE
735          * in case a chmod has now revoked permission.
736          */
737         err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
738         if (err)
739                 goto out;
740
741         dentry = fhp->fh_dentry;
742         inode = dentry->d_inode;
743
744         /* Disallow write access to files with the append-only bit set
745          * or any access when mandatory locking enabled
746          */
747         err = nfserr_perm;
748         if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
749                 goto out;
750         /*
751          * We must ignore files (but only files) which might have mandatory
752          * locks on them because there is no way to know if the accesser has
753          * the lock.
754          */
755         if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
756                 goto out;
757
758         if (!inode->i_fop)
759                 goto out;
760
761         /*
762          * Check to see if there are any leases on this file.
763          * This may block while leases are broken.
764          */
765         host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? FMODE_WRITE : 0));
766         if (host_err == -EWOULDBLOCK)
767                 host_err = -ETIMEDOUT;
768         if (host_err) /* NOMEM or WOULDBLOCK */
769                 goto out_nfserr;
770
771         if (access & NFSD_MAY_WRITE) {
772                 if (access & NFSD_MAY_READ)
773                         flags = O_RDWR|O_LARGEFILE;
774                 else
775                         flags = O_WRONLY|O_LARGEFILE;
776
777                 vfs_dq_init(inode);
778         }
779         *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
780                             flags, current_cred());
781         if (IS_ERR(*filp))
782                 host_err = PTR_ERR(*filp);
783         else
784                 host_err = ima_file_check(*filp, access);
785 out_nfserr:
786         err = nfserrno(host_err);
787 out:
788         validate_process_creds();
789         return err;
790 }
791
792 /*
793  * Close a file.
794  */
795 void
796 nfsd_close(struct file *filp)
797 {
798         fput(filp);
799 }
800
801 /*
802  * Obtain the readahead parameters for the file
803  * specified by (dev, ino).
804  */
805
806 static inline struct raparms *
807 nfsd_get_raparms(dev_t dev, ino_t ino)
808 {
809         struct raparms  *ra, **rap, **frap = NULL;
810         int depth = 0;
811         unsigned int hash;
812         struct raparm_hbucket *rab;
813
814         hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
815         rab = &raparm_hash[hash];
816
817         spin_lock(&rab->pb_lock);
818         for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
819                 if (ra->p_ino == ino && ra->p_dev == dev)
820                         goto found;
821                 depth++;
822                 if (ra->p_count == 0)
823                         frap = rap;
824         }
825         depth = nfsdstats.ra_size*11/10;
826         if (!frap) {    
827                 spin_unlock(&rab->pb_lock);
828                 return NULL;
829         }
830         rap = frap;
831         ra = *frap;
832         ra->p_dev = dev;
833         ra->p_ino = ino;
834         ra->p_set = 0;
835         ra->p_hindex = hash;
836 found:
837         if (rap != &rab->pb_head) {
838                 *rap = ra->p_next;
839                 ra->p_next   = rab->pb_head;
840                 rab->pb_head = ra;
841         }
842         ra->p_count++;
843         nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
844         spin_unlock(&rab->pb_lock);
845         return ra;
846 }
847
848 /*
849  * Grab and keep cached pages associated with a file in the svc_rqst
850  * so that they can be passed to the network sendmsg/sendpage routines
851  * directly. They will be released after the sending has completed.
852  */
853 static int
854 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
855                   struct splice_desc *sd)
856 {
857         struct svc_rqst *rqstp = sd->u.data;
858         struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
859         struct page *page = buf->page;
860         size_t size;
861         int ret;
862
863         ret = buf->ops->confirm(pipe, buf);
864         if (unlikely(ret))
865                 return ret;
866
867         size = sd->len;
868
869         if (rqstp->rq_res.page_len == 0) {
870                 get_page(page);
871                 put_page(*pp);
872                 *pp = page;
873                 rqstp->rq_resused++;
874                 rqstp->rq_res.page_base = buf->offset;
875                 rqstp->rq_res.page_len = size;
876         } else if (page != pp[-1]) {
877                 get_page(page);
878                 if (*pp)
879                         put_page(*pp);
880                 *pp = page;
881                 rqstp->rq_resused++;
882                 rqstp->rq_res.page_len += size;
883         } else
884                 rqstp->rq_res.page_len += size;
885
886         return size;
887 }
888
889 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
890                                     struct splice_desc *sd)
891 {
892         return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
893 }
894
895 static inline int svc_msnfs(struct svc_fh *ffhp)
896 {
897 #ifdef MSNFS
898         return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
899 #else
900         return 0;
901 #endif
902 }
903
904 static __be32
905 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
906               loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
907 {
908         struct inode *inode;
909         struct raparms  *ra;
910         mm_segment_t    oldfs;
911         __be32          err;
912         int             host_err;
913
914         err = nfserr_perm;
915         inode = file->f_path.dentry->d_inode;
916
917         if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
918                 goto out;
919
920         /* Get readahead parameters */
921         ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
922
923         if (ra && ra->p_set)
924                 file->f_ra = ra->p_ra;
925
926         if (file->f_op->splice_read && rqstp->rq_splice_ok) {
927                 struct splice_desc sd = {
928                         .len            = 0,
929                         .total_len      = *count,
930                         .pos            = offset,
931                         .u.data         = rqstp,
932                 };
933
934                 rqstp->rq_resused = 1;
935                 host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
936         } else {
937                 oldfs = get_fs();
938                 set_fs(KERNEL_DS);
939                 host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
940                 set_fs(oldfs);
941         }
942
943         /* Write back readahead params */
944         if (ra) {
945                 struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
946                 spin_lock(&rab->pb_lock);
947                 ra->p_ra = file->f_ra;
948                 ra->p_set = 1;
949                 ra->p_count--;
950                 spin_unlock(&rab->pb_lock);
951         }
952
953         if (host_err >= 0) {
954                 nfsdstats.io_read += host_err;
955                 *count = host_err;
956                 err = 0;
957                 fsnotify_access(file->f_path.dentry);
958         } else 
959                 err = nfserrno(host_err);
960 out:
961         return err;
962 }
963
964 static void kill_suid(struct dentry *dentry)
965 {
966         struct iattr    ia;
967         ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
968
969         mutex_lock(&dentry->d_inode->i_mutex);
970         notify_change(dentry, &ia);
971         mutex_unlock(&dentry->d_inode->i_mutex);
972 }
973
974 /*
975  * Gathered writes: If another process is currently writing to the file,
976  * there's a high chance this is another nfsd (triggered by a bulk write
977  * from a client's biod). Rather than syncing the file with each write
978  * request, we sleep for 10 msec.
979  *
980  * I don't know if this roughly approximates C. Juszak's idea of
981  * gathered writes, but it's a nice and simple solution (IMHO), and it
982  * seems to work:-)
983  *
984  * Note: we do this only in the NFSv2 case, since v3 and higher have a
985  * better tool (separate unstable writes and commits) for solving this
986  * problem.
987  */
988 static int wait_for_concurrent_writes(struct file *file)
989 {
990         struct inode *inode = file->f_path.dentry->d_inode;
991         static ino_t last_ino;
992         static dev_t last_dev;
993         int err = 0;
994
995         if (atomic_read(&inode->i_writecount) > 1
996             || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
997                 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
998                 msleep(10);
999                 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
1000         }
1001
1002         if (inode->i_state & I_DIRTY) {
1003                 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
1004                 err = vfs_fsync(file, file->f_path.dentry, 0);
1005         }
1006         last_ino = inode->i_ino;
1007         last_dev = inode->i_sb->s_dev;
1008         return err;
1009 }
1010
1011 static __be32
1012 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1013                                 loff_t offset, struct kvec *vec, int vlen,
1014                                 unsigned long *cnt, int *stablep)
1015 {
1016         struct svc_export       *exp;
1017         struct dentry           *dentry;
1018         struct inode            *inode;
1019         mm_segment_t            oldfs;
1020         __be32                  err = 0;
1021         int                     host_err;
1022         int                     stable = *stablep;
1023         int                     use_wgather;
1024
1025 #ifdef MSNFS
1026         err = nfserr_perm;
1027
1028         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1029                 (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
1030                 goto out;
1031 #endif
1032
1033         dentry = file->f_path.dentry;
1034         inode = dentry->d_inode;
1035         exp   = fhp->fh_export;
1036
1037         /*
1038          * Request sync writes if
1039          *  -   the sync export option has been set, or
1040          *  -   the client requested O_SYNC behavior (NFSv3 feature).
1041          *  -   The file system doesn't support fsync().
1042          * When NFSv2 gathered writes have been configured for this volume,
1043          * flushing the data to disk is handled separately below.
1044          */
1045         use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
1046
1047         if (!file->f_op->fsync) {/* COMMIT3 cannot work */
1048                stable = 2;
1049                *stablep = 2; /* FILE_SYNC */
1050         }
1051
1052         if (!EX_ISSYNC(exp))
1053                 stable = 0;
1054         if (stable && !use_wgather) {
1055                 spin_lock(&file->f_lock);
1056                 file->f_flags |= O_SYNC;
1057                 spin_unlock(&file->f_lock);
1058         }
1059
1060         /* Write the data. */
1061         oldfs = get_fs(); set_fs(KERNEL_DS);
1062         host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
1063         set_fs(oldfs);
1064         if (host_err < 0)
1065                 goto out_nfserr;
1066         *cnt = host_err;
1067         nfsdstats.io_write += host_err;
1068         fsnotify_modify(file->f_path.dentry);
1069
1070         /* clear setuid/setgid flag after write */
1071         if (inode->i_mode & (S_ISUID | S_ISGID))
1072                 kill_suid(dentry);
1073
1074         if (stable && use_wgather)
1075                 host_err = wait_for_concurrent_writes(file);
1076
1077 out_nfserr:
1078         dprintk("nfsd: write complete host_err=%d\n", host_err);
1079         if (host_err >= 0)
1080                 err = 0;
1081         else
1082                 err = nfserrno(host_err);
1083 out:
1084         return err;
1085 }
1086
1087 /*
1088  * Read data from a file. count must contain the requested read count
1089  * on entry. On return, *count contains the number of bytes actually read.
1090  * N.B. After this call fhp needs an fh_put
1091  */
1092 __be32
1093 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1094                 loff_t offset, struct kvec *vec, int vlen,
1095                 unsigned long *count)
1096 {
1097         __be32          err;
1098
1099         if (file) {
1100                 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1101                                 NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
1102                 if (err)
1103                         goto out;
1104                 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1105         } else {
1106                 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1107                 if (err)
1108                         goto out;
1109                 err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
1110                 nfsd_close(file);
1111         }
1112 out:
1113         return err;
1114 }
1115
1116 /*
1117  * Write data to a file.
1118  * The stable flag requests synchronous writes.
1119  * N.B. After this call fhp needs an fh_put
1120  */
1121 __be32
1122 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1123                 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1124                 int *stablep)
1125 {
1126         __be32                  err = 0;
1127
1128         if (file) {
1129                 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1130                                 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1131                 if (err)
1132                         goto out;
1133                 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1134                                 stablep);
1135         } else {
1136                 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1137                 if (err)
1138                         goto out;
1139
1140                 if (cnt)
1141                         err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1142                                              cnt, stablep);
1143                 nfsd_close(file);
1144         }
1145 out:
1146         return err;
1147 }
1148
1149 #ifdef CONFIG_NFSD_V3
1150 /*
1151  * Commit all pending writes to stable storage.
1152  *
1153  * Note: we only guarantee that data that lies within the range specified
1154  * by the 'offset' and 'count' parameters will be synced.
1155  *
1156  * Unfortunately we cannot lock the file to make sure we return full WCC
1157  * data to the client, as locking happens lower down in the filesystem.
1158  */
1159 __be32
1160 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1161                loff_t offset, unsigned long count)
1162 {
1163         struct file     *file;
1164         loff_t          end = LLONG_MAX;
1165         __be32          err = nfserr_inval;
1166
1167         if (offset < 0)
1168                 goto out;
1169         if (count != 0) {
1170                 end = offset + (loff_t)count - 1;
1171                 if (end < offset)
1172                         goto out;
1173         }
1174
1175         err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1176         if (err)
1177                 goto out;
1178         if (EX_ISSYNC(fhp->fh_export)) {
1179                 int err2 = vfs_fsync_range(file, file->f_path.dentry,
1180                                 offset, end, 0);
1181
1182                 if (err2 != -EINVAL)
1183                         err = nfserrno(err2);
1184                 else
1185                         err = nfserr_notsupp;
1186         }
1187
1188         nfsd_close(file);
1189 out:
1190         return err;
1191 }
1192 #endif /* CONFIG_NFSD_V3 */
1193
1194 static __be32
1195 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1196                         struct iattr *iap)
1197 {
1198         /*
1199          * Mode has already been set earlier in create:
1200          */
1201         iap->ia_valid &= ~ATTR_MODE;
1202         /*
1203          * Setting uid/gid works only for root.  Irix appears to
1204          * send along the gid on create when it tries to implement
1205          * setgid directories via NFS:
1206          */
1207         if (current_fsuid() != 0)
1208                 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1209         if (iap->ia_valid)
1210                 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1211         return 0;
1212 }
1213
1214 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1215  * setting size to 0 may fail for some specific file systems by the permission
1216  * checking which requires WRITE permission but the mode is 000.
1217  * we ignore the resizing(to 0) on the just new created file, since the size is
1218  * 0 after file created.
1219  *
1220  * call this only after vfs_create() is called.
1221  * */
1222 static void
1223 nfsd_check_ignore_resizing(struct iattr *iap)
1224 {
1225         if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1226                 iap->ia_valid &= ~ATTR_SIZE;
1227 }
1228
1229 /*
1230  * Create a file (regular, directory, device, fifo); UNIX sockets 
1231  * not yet implemented.
1232  * If the response fh has been verified, the parent directory should
1233  * already be locked. Note that the parent directory is left locked.
1234  *
1235  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1236  */
1237 __be32
1238 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1239                 char *fname, int flen, struct iattr *iap,
1240                 int type, dev_t rdev, struct svc_fh *resfhp)
1241 {
1242         struct dentry   *dentry, *dchild = NULL;
1243         struct inode    *dirp;
1244         __be32          err;
1245         __be32          err2;
1246         int             host_err;
1247
1248         err = nfserr_perm;
1249         if (!flen)
1250                 goto out;
1251         err = nfserr_exist;
1252         if (isdotent(fname, flen))
1253                 goto out;
1254
1255         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1256         if (err)
1257                 goto out;
1258
1259         dentry = fhp->fh_dentry;
1260         dirp = dentry->d_inode;
1261
1262         err = nfserr_notdir;
1263         if (!dirp->i_op->lookup)
1264                 goto out;
1265         /*
1266          * Check whether the response file handle has been verified yet.
1267          * If it has, the parent directory should already be locked.
1268          */
1269         if (!resfhp->fh_dentry) {
1270                 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1271                 fh_lock_nested(fhp, I_MUTEX_PARENT);
1272                 dchild = lookup_one_len(fname, dentry, flen);
1273                 host_err = PTR_ERR(dchild);
1274                 if (IS_ERR(dchild))
1275                         goto out_nfserr;
1276                 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1277                 if (err)
1278                         goto out;
1279         } else {
1280                 /* called from nfsd_proc_create */
1281                 dchild = dget(resfhp->fh_dentry);
1282                 if (!fhp->fh_locked) {
1283                         /* not actually possible */
1284                         printk(KERN_ERR
1285                                 "nfsd_create: parent %s/%s not locked!\n",
1286                                 dentry->d_parent->d_name.name,
1287                                 dentry->d_name.name);
1288                         err = nfserr_io;
1289                         goto out;
1290                 }
1291         }
1292         /*
1293          * Make sure the child dentry is still negative ...
1294          */
1295         err = nfserr_exist;
1296         if (dchild->d_inode) {
1297                 dprintk("nfsd_create: dentry %s/%s not negative!\n",
1298                         dentry->d_name.name, dchild->d_name.name);
1299                 goto out; 
1300         }
1301
1302         if (!(iap->ia_valid & ATTR_MODE))
1303                 iap->ia_mode = 0;
1304         iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1305
1306         err = nfserr_inval;
1307         if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1308                 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1309                        type);
1310                 goto out;
1311         }
1312
1313         host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1314         if (host_err)
1315                 goto out_nfserr;
1316
1317         /*
1318          * Get the dir op function pointer.
1319          */
1320         err = 0;
1321         switch (type) {
1322         case S_IFREG:
1323                 host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1324                 if (!host_err)
1325                         nfsd_check_ignore_resizing(iap);
1326                 break;
1327         case S_IFDIR:
1328                 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1329                 break;
1330         case S_IFCHR:
1331         case S_IFBLK:
1332         case S_IFIFO:
1333         case S_IFSOCK:
1334                 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1335                 break;
1336         }
1337         if (host_err < 0) {
1338                 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1339                 goto out_nfserr;
1340         }
1341
1342         err = nfsd_create_setattr(rqstp, resfhp, iap);
1343
1344         /*
1345          * nfsd_setattr already committed the child.  Transactional filesystems
1346          * had a chance to commit changes for both parent and child
1347          * simultaneously making the following commit_metadata a noop.
1348          */
1349         err2 = nfserrno(commit_metadata(fhp));
1350         if (err2)
1351                 err = err2;
1352         mnt_drop_write(fhp->fh_export->ex_path.mnt);
1353         /*
1354          * Update the file handle to get the new inode info.
1355          */
1356         if (!err)
1357                 err = fh_update(resfhp);
1358 out:
1359         if (dchild && !IS_ERR(dchild))
1360                 dput(dchild);
1361         return err;
1362
1363 out_nfserr:
1364         err = nfserrno(host_err);
1365         goto out;
1366 }
1367
1368 #ifdef CONFIG_NFSD_V3
1369 /*
1370  * NFSv3 version of nfsd_create
1371  */
1372 __be32
1373 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
1374                 char *fname, int flen, struct iattr *iap,
1375                 struct svc_fh *resfhp, int createmode, u32 *verifier,
1376                 int *truncp, int *created)
1377 {
1378         struct dentry   *dentry, *dchild = NULL;
1379         struct inode    *dirp;
1380         __be32          err;
1381         int             host_err;
1382         __u32           v_mtime=0, v_atime=0;
1383
1384         err = nfserr_perm;
1385         if (!flen)
1386                 goto out;
1387         err = nfserr_exist;
1388         if (isdotent(fname, flen))
1389                 goto out;
1390         if (!(iap->ia_valid & ATTR_MODE))
1391                 iap->ia_mode = 0;
1392         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1393         if (err)
1394                 goto out;
1395
1396         dentry = fhp->fh_dentry;
1397         dirp = dentry->d_inode;
1398
1399         /* Get all the sanity checks out of the way before
1400          * we lock the parent. */
1401         err = nfserr_notdir;
1402         if (!dirp->i_op->lookup)
1403                 goto out;
1404         fh_lock_nested(fhp, I_MUTEX_PARENT);
1405
1406         /*
1407          * Compose the response file handle.
1408          */
1409         dchild = lookup_one_len(fname, dentry, flen);
1410         host_err = PTR_ERR(dchild);
1411         if (IS_ERR(dchild))
1412                 goto out_nfserr;
1413
1414         err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1415         if (err)
1416                 goto out;
1417
1418         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1419                 /* solaris7 gets confused (bugid 4218508) if these have
1420                  * the high bit set, so just clear the high bits. If this is
1421                  * ever changed to use different attrs for storing the
1422                  * verifier, then do_open_lookup() will also need to be fixed
1423                  * accordingly.
1424                  */
1425                 v_mtime = verifier[0]&0x7fffffff;
1426                 v_atime = verifier[1]&0x7fffffff;
1427         }
1428         
1429         host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1430         if (host_err)
1431                 goto out_nfserr;
1432         if (dchild->d_inode) {
1433                 err = 0;
1434
1435                 switch (createmode) {
1436                 case NFS3_CREATE_UNCHECKED:
1437                         if (! S_ISREG(dchild->d_inode->i_mode))
1438                                 err = nfserr_exist;
1439                         else if (truncp) {
1440                                 /* in nfsv4, we need to treat this case a little
1441                                  * differently.  we don't want to truncate the
1442                                  * file now; this would be wrong if the OPEN
1443                                  * fails for some other reason.  furthermore,
1444                                  * if the size is nonzero, we should ignore it
1445                                  * according to spec!
1446                                  */
1447                                 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1448                         }
1449                         else {
1450                                 iap->ia_valid &= ATTR_SIZE;
1451                                 goto set_attr;
1452                         }
1453                         break;
1454                 case NFS3_CREATE_EXCLUSIVE:
1455                         if (   dchild->d_inode->i_mtime.tv_sec == v_mtime
1456                             && dchild->d_inode->i_atime.tv_sec == v_atime
1457                             && dchild->d_inode->i_size  == 0 )
1458                                 break;
1459                          /* fallthru */
1460                 case NFS3_CREATE_GUARDED:
1461                         err = nfserr_exist;
1462                 }
1463                 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1464                 goto out;
1465         }
1466
1467         host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1468         if (host_err < 0) {
1469                 mnt_drop_write(fhp->fh_export->ex_path.mnt);
1470                 goto out_nfserr;
1471         }
1472         if (created)
1473                 *created = 1;
1474
1475         nfsd_check_ignore_resizing(iap);
1476
1477         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1478                 /* Cram the verifier into atime/mtime */
1479                 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1480                         | ATTR_MTIME_SET|ATTR_ATIME_SET;
1481                 /* XXX someone who knows this better please fix it for nsec */ 
1482                 iap->ia_mtime.tv_sec = v_mtime;
1483                 iap->ia_atime.tv_sec = v_atime;
1484                 iap->ia_mtime.tv_nsec = 0;
1485                 iap->ia_atime.tv_nsec = 0;
1486         }
1487
1488  set_attr:
1489         err = nfsd_create_setattr(rqstp, resfhp, iap);
1490
1491         /*
1492          * nfsd_setattr already committed the child (and possibly also the parent).
1493          */
1494         if (!err)
1495                 err = nfserrno(commit_metadata(fhp));
1496
1497         mnt_drop_write(fhp->fh_export->ex_path.mnt);
1498         /*
1499          * Update the filehandle to get the new inode info.
1500          */
1501         if (!err)
1502                 err = fh_update(resfhp);
1503
1504  out:
1505         fh_unlock(fhp);
1506         if (dchild && !IS_ERR(dchild))
1507                 dput(dchild);
1508         return err;
1509  
1510  out_nfserr:
1511         err = nfserrno(host_err);
1512         goto out;
1513 }
1514 #endif /* CONFIG_NFSD_V3 */
1515
1516 /*
1517  * Read a symlink. On entry, *lenp must contain the maximum path length that
1518  * fits into the buffer. On return, it contains the true length.
1519  * N.B. After this call fhp needs an fh_put
1520  */
1521 __be32
1522 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1523 {
1524         struct dentry   *dentry;
1525         struct inode    *inode;
1526         mm_segment_t    oldfs;
1527         __be32          err;
1528         int             host_err;
1529
1530         err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1531         if (err)
1532                 goto out;
1533
1534         dentry = fhp->fh_dentry;
1535         inode = dentry->d_inode;
1536
1537         err = nfserr_inval;
1538         if (!inode->i_op->readlink)
1539                 goto out;
1540
1541         touch_atime(fhp->fh_export->ex_path.mnt, dentry);
1542         /* N.B. Why does this call need a get_fs()??
1543          * Remove the set_fs and watch the fireworks:-) --okir
1544          */
1545
1546         oldfs = get_fs(); set_fs(KERNEL_DS);
1547         host_err = inode->i_op->readlink(dentry, buf, *lenp);
1548         set_fs(oldfs);
1549
1550         if (host_err < 0)
1551                 goto out_nfserr;
1552         *lenp = host_err;
1553         err = 0;
1554 out:
1555         return err;
1556
1557 out_nfserr:
1558         err = nfserrno(host_err);
1559         goto out;
1560 }
1561
1562 /*
1563  * Create a symlink and look up its inode
1564  * N.B. After this call _both_ fhp and resfhp need an fh_put
1565  */
1566 __be32
1567 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1568                                 char *fname, int flen,
1569                                 char *path,  int plen,
1570                                 struct svc_fh *resfhp,
1571                                 struct iattr *iap)
1572 {
1573         struct dentry   *dentry, *dnew;
1574         __be32          err, cerr;
1575         int             host_err;
1576
1577         err = nfserr_noent;
1578         if (!flen || !plen)
1579                 goto out;
1580         err = nfserr_exist;
1581         if (isdotent(fname, flen))
1582                 goto out;
1583
1584         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1585         if (err)
1586                 goto out;
1587         fh_lock(fhp);
1588         dentry = fhp->fh_dentry;
1589         dnew = lookup_one_len(fname, dentry, flen);
1590         host_err = PTR_ERR(dnew);
1591         if (IS_ERR(dnew))
1592                 goto out_nfserr;
1593
1594         host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1595         if (host_err)
1596                 goto out_nfserr;
1597
1598         if (unlikely(path[plen] != 0)) {
1599                 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1600                 if (path_alloced == NULL)
1601                         host_err = -ENOMEM;
1602                 else {
1603                         strncpy(path_alloced, path, plen);
1604                         path_alloced[plen] = 0;
1605                         host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1606                         kfree(path_alloced);
1607                 }
1608         } else
1609                 host_err = vfs_symlink(dentry->d_inode, dnew, path);
1610         err = nfserrno(host_err);
1611         if (!err)
1612                 err = nfserrno(commit_metadata(fhp));
1613         fh_unlock(fhp);
1614
1615         mnt_drop_write(fhp->fh_export->ex_path.mnt);
1616
1617         cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1618         dput(dnew);
1619         if (err==0) err = cerr;
1620 out:
1621         return err;
1622
1623 out_nfserr:
1624         err = nfserrno(host_err);
1625         goto out;
1626 }
1627
1628 /*
1629  * Create a hardlink
1630  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1631  */
1632 __be32
1633 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1634                                 char *name, int len, struct svc_fh *tfhp)
1635 {
1636         struct dentry   *ddir, *dnew, *dold;
1637         struct inode    *dirp, *dest;
1638         __be32          err;
1639         int             host_err;
1640
1641         err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1642         if (err)
1643                 goto out;
1644         err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
1645         if (err)
1646                 goto out;
1647
1648         err = nfserr_perm;
1649         if (!len)
1650                 goto out;
1651         err = nfserr_exist;
1652         if (isdotent(name, len))
1653                 goto out;
1654
1655         fh_lock_nested(ffhp, I_MUTEX_PARENT);
1656         ddir = ffhp->fh_dentry;
1657         dirp = ddir->d_inode;
1658
1659         dnew = lookup_one_len(name, ddir, len);
1660         host_err = PTR_ERR(dnew);
1661         if (IS_ERR(dnew))
1662                 goto out_nfserr;
1663
1664         dold = tfhp->fh_dentry;
1665         dest = dold->d_inode;
1666
1667         host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
1668         if (host_err) {
1669                 err = nfserrno(host_err);
1670                 goto out_dput;
1671         }
1672         host_err = vfs_link(dold, dirp, dnew);
1673         if (!host_err) {
1674                 err = nfserrno(commit_metadata(ffhp));
1675                 if (!err)
1676                         err = nfserrno(commit_metadata(tfhp));
1677         } else {
1678                 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1679                         err = nfserr_acces;
1680                 else
1681                         err = nfserrno(host_err);
1682         }
1683         mnt_drop_write(tfhp->fh_export->ex_path.mnt);
1684 out_dput:
1685         dput(dnew);
1686 out_unlock:
1687         fh_unlock(ffhp);
1688 out:
1689         return err;
1690
1691 out_nfserr:
1692         err = nfserrno(host_err);
1693         goto out_unlock;
1694 }
1695
1696 /*
1697  * Rename a file
1698  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1699  */
1700 __be32
1701 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1702                             struct svc_fh *tfhp, char *tname, int tlen)
1703 {
1704         struct dentry   *fdentry, *tdentry, *odentry, *ndentry, *trap;
1705         struct inode    *fdir, *tdir;
1706         __be32          err;
1707         int             host_err;
1708
1709         err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1710         if (err)
1711                 goto out;
1712         err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1713         if (err)
1714                 goto out;
1715
1716         fdentry = ffhp->fh_dentry;
1717         fdir = fdentry->d_inode;
1718
1719         tdentry = tfhp->fh_dentry;
1720         tdir = tdentry->d_inode;
1721
1722         err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1723         if (ffhp->fh_export != tfhp->fh_export)
1724                 goto out;
1725
1726         err = nfserr_perm;
1727         if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1728                 goto out;
1729
1730         /* cannot use fh_lock as we need deadlock protective ordering
1731          * so do it by hand */
1732         trap = lock_rename(tdentry, fdentry);
1733         ffhp->fh_locked = tfhp->fh_locked = 1;
1734         fill_pre_wcc(ffhp);
1735         fill_pre_wcc(tfhp);
1736
1737         odentry = lookup_one_len(fname, fdentry, flen);
1738         host_err = PTR_ERR(odentry);
1739         if (IS_ERR(odentry))
1740                 goto out_nfserr;
1741
1742         host_err = -ENOENT;
1743         if (!odentry->d_inode)
1744                 goto out_dput_old;
1745         host_err = -EINVAL;
1746         if (odentry == trap)
1747                 goto out_dput_old;
1748
1749         ndentry = lookup_one_len(tname, tdentry, tlen);
1750         host_err = PTR_ERR(ndentry);
1751         if (IS_ERR(ndentry))
1752                 goto out_dput_old;
1753         host_err = -ENOTEMPTY;
1754         if (ndentry == trap)
1755                 goto out_dput_new;
1756
1757         if (svc_msnfs(ffhp) &&
1758                 ((atomic_read(&odentry->d_count) > 1)
1759                  || (atomic_read(&ndentry->d_count) > 1))) {
1760                         host_err = -EPERM;
1761                         goto out_dput_new;
1762         }
1763
1764         host_err = -EXDEV;
1765         if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1766                 goto out_dput_new;
1767         host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
1768         if (host_err)
1769                 goto out_dput_new;
1770
1771         host_err = vfs_rename(fdir, odentry, tdir, ndentry);
1772         if (!host_err) {
1773                 host_err = commit_metadata(tfhp);
1774                 if (!host_err)
1775                         host_err = commit_metadata(ffhp);
1776         }
1777
1778         mnt_drop_write(ffhp->fh_export->ex_path.mnt);
1779
1780  out_dput_new:
1781         dput(ndentry);
1782  out_dput_old:
1783         dput(odentry);
1784  out_nfserr:
1785         err = nfserrno(host_err);
1786
1787         /* we cannot reply on fh_unlock on the two filehandles,
1788          * as that would do the wrong thing if the two directories
1789          * were the same, so again we do it by hand
1790          */
1791         fill_post_wcc(ffhp);
1792         fill_post_wcc(tfhp);
1793         unlock_rename(tdentry, fdentry);
1794         ffhp->fh_locked = tfhp->fh_locked = 0;
1795
1796 out:
1797         return err;
1798 }
1799
1800 /*
1801  * Unlink a file or directory
1802  * N.B. After this call fhp needs an fh_put
1803  */
1804 __be32
1805 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1806                                 char *fname, int flen)
1807 {
1808         struct dentry   *dentry, *rdentry;
1809         struct inode    *dirp;
1810         __be32          err;
1811         int             host_err;
1812
1813         err = nfserr_acces;
1814         if (!flen || isdotent(fname, flen))
1815                 goto out;
1816         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1817         if (err)
1818                 goto out;
1819
1820         fh_lock_nested(fhp, I_MUTEX_PARENT);
1821         dentry = fhp->fh_dentry;
1822         dirp = dentry->d_inode;
1823
1824         rdentry = lookup_one_len(fname, dentry, flen);
1825         host_err = PTR_ERR(rdentry);
1826         if (IS_ERR(rdentry))
1827                 goto out_nfserr;
1828
1829         if (!rdentry->d_inode) {
1830                 dput(rdentry);
1831                 err = nfserr_noent;
1832                 goto out;
1833         }
1834
1835         if (!type)
1836                 type = rdentry->d_inode->i_mode & S_IFMT;
1837
1838         host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
1839         if (host_err)
1840                 goto out_nfserr;
1841
1842         if (type != S_IFDIR) { /* It's UNLINK */
1843 #ifdef MSNFS
1844                 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1845                         (atomic_read(&rdentry->d_count) > 1)) {
1846                         host_err = -EPERM;
1847                 } else
1848 #endif
1849                 host_err = vfs_unlink(dirp, rdentry);
1850         } else { /* It's RMDIR */
1851                 host_err = vfs_rmdir(dirp, rdentry);
1852         }
1853
1854         dput(rdentry);
1855
1856         if (!host_err)
1857                 host_err = commit_metadata(fhp);
1858
1859         mnt_drop_write(fhp->fh_export->ex_path.mnt);
1860 out_nfserr:
1861         err = nfserrno(host_err);
1862 out:
1863         return err;
1864 }
1865
1866 /*
1867  * We do this buffering because we must not call back into the file
1868  * system's ->lookup() method from the filldir callback. That may well
1869  * deadlock a number of file systems.
1870  *
1871  * This is based heavily on the implementation of same in XFS.
1872  */
1873 struct buffered_dirent {
1874         u64             ino;
1875         loff_t          offset;
1876         int             namlen;
1877         unsigned int    d_type;
1878         char            name[];
1879 };
1880
1881 struct readdir_data {
1882         char            *dirent;
1883         size_t          used;
1884         int             full;
1885 };
1886
1887 static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
1888                                  loff_t offset, u64 ino, unsigned int d_type)
1889 {
1890         struct readdir_data *buf = __buf;
1891         struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1892         unsigned int reclen;
1893
1894         reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1895         if (buf->used + reclen > PAGE_SIZE) {
1896                 buf->full = 1;
1897                 return -EINVAL;
1898         }
1899
1900         de->namlen = namlen;
1901         de->offset = offset;
1902         de->ino = ino;
1903         de->d_type = d_type;
1904         memcpy(de->name, name, namlen);
1905         buf->used += reclen;
1906
1907         return 0;
1908 }
1909
1910 static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
1911                                     struct readdir_cd *cdp, loff_t *offsetp)
1912 {
1913         struct readdir_data buf;
1914         struct buffered_dirent *de;
1915         int host_err;
1916         int size;
1917         loff_t offset;
1918
1919         buf.dirent = (void *)__get_free_page(GFP_KERNEL);
1920         if (!buf.dirent)
1921                 return nfserrno(-ENOMEM);
1922
1923         offset = *offsetp;
1924
1925         while (1) {
1926                 struct inode *dir_inode = file->f_path.dentry->d_inode;
1927                 unsigned int reclen;
1928
1929                 cdp->err = nfserr_eof; /* will be cleared on successful read */
1930                 buf.used = 0;
1931                 buf.full = 0;
1932
1933                 host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
1934                 if (buf.full)
1935                         host_err = 0;
1936
1937                 if (host_err < 0)
1938                         break;
1939
1940                 size = buf.used;
1941
1942                 if (!size)
1943                         break;
1944
1945                 /*
1946                  * Various filldir functions may end up calling back into
1947                  * lookup_one_len() and the file system's ->lookup() method.
1948                  * These expect i_mutex to be held, as it would within readdir.
1949                  */
1950                 host_err = mutex_lock_killable(&dir_inode->i_mutex);
1951                 if (host_err)
1952                         break;
1953
1954                 de = (struct buffered_dirent *)buf.dirent;
1955                 while (size > 0) {
1956                         offset = de->offset;
1957
1958                         if (func(cdp, de->name, de->namlen, de->offset,
1959                                  de->ino, de->d_type))
1960                                 break;
1961
1962                         if (cdp->err != nfs_ok)
1963                                 break;
1964
1965                         reclen = ALIGN(sizeof(*de) + de->namlen,
1966                                        sizeof(u64));
1967                         size -= reclen;
1968                         de = (struct buffered_dirent *)((char *)de + reclen);
1969                 }
1970                 mutex_unlock(&dir_inode->i_mutex);
1971                 if (size > 0) /* We bailed out early */
1972                         break;
1973
1974                 offset = vfs_llseek(file, 0, SEEK_CUR);
1975         }
1976
1977         free_page((unsigned long)(buf.dirent));
1978
1979         if (host_err)
1980                 return nfserrno(host_err);
1981
1982         *offsetp = offset;
1983         return cdp->err;
1984 }
1985
1986 /*
1987  * Read entries from a directory.
1988  * The  NFSv3/4 verifier we ignore for now.
1989  */
1990 __be32
1991 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, 
1992              struct readdir_cd *cdp, filldir_t func)
1993 {
1994         __be32          err;
1995         struct file     *file;
1996         loff_t          offset = *offsetp;
1997
1998         err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
1999         if (err)
2000                 goto out;
2001
2002         offset = vfs_llseek(file, offset, 0);
2003         if (offset < 0) {
2004                 err = nfserrno((int)offset);
2005                 goto out_close;
2006         }
2007
2008         err = nfsd_buffered_readdir(file, func, cdp, offsetp);
2009
2010         if (err == nfserr_eof || err == nfserr_toosmall)
2011                 err = nfs_ok; /* can still be found in ->err */
2012 out_close:
2013         nfsd_close(file);
2014 out:
2015         return err;
2016 }
2017
2018 /*
2019  * Get file system stats
2020  * N.B. After this call fhp needs an fh_put
2021  */
2022 __be32
2023 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
2024 {
2025         __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
2026         if (!err && vfs_statfs(fhp->fh_dentry,stat))
2027                 err = nfserr_io;
2028         return err;
2029 }
2030
2031 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
2032 {
2033         return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
2034 }
2035
2036 /*
2037  * Check for a user's access permissions to this inode.
2038  */
2039 __be32
2040 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
2041                                         struct dentry *dentry, int acc)
2042 {
2043         struct inode    *inode = dentry->d_inode;
2044         struct path     path;
2045         int             err;
2046
2047         if (acc == NFSD_MAY_NOP)
2048                 return 0;
2049 #if 0
2050         dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
2051                 acc,
2052                 (acc & NFSD_MAY_READ)?  " read"  : "",
2053                 (acc & NFSD_MAY_WRITE)? " write" : "",
2054                 (acc & NFSD_MAY_EXEC)?  " exec"  : "",
2055                 (acc & NFSD_MAY_SATTR)? " sattr" : "",
2056                 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
2057                 (acc & NFSD_MAY_LOCK)?  " lock"  : "",
2058                 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
2059                 inode->i_mode,
2060                 IS_IMMUTABLE(inode)?    " immut" : "",
2061                 IS_APPEND(inode)?       " append" : "",
2062                 __mnt_is_readonly(exp->ex_path.mnt)?    " ro" : "");
2063         dprintk("      owner %d/%d user %d/%d\n",
2064                 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
2065 #endif
2066
2067         /* Normally we reject any write/sattr etc access on a read-only file
2068          * system.  But if it is IRIX doing check on write-access for a 
2069          * device special file, we ignore rofs.
2070          */
2071         if (!(acc & NFSD_MAY_LOCAL_ACCESS))
2072                 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
2073                         if (exp_rdonly(rqstp, exp) ||
2074                             __mnt_is_readonly(exp->ex_path.mnt))
2075                                 return nfserr_rofs;
2076                         if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
2077                                 return nfserr_perm;
2078                 }
2079         if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
2080                 return nfserr_perm;
2081
2082         if (acc & NFSD_MAY_LOCK) {
2083                 /* If we cannot rely on authentication in NLM requests,
2084                  * just allow locks, otherwise require read permission, or
2085                  * ownership
2086                  */
2087                 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
2088                         return 0;
2089                 else
2090                         acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
2091         }
2092         /*
2093          * The file owner always gets access permission for accesses that
2094          * would normally be checked at open time. This is to make
2095          * file access work even when the client has done a fchmod(fd, 0).
2096          *
2097          * However, `cp foo bar' should fail nevertheless when bar is
2098          * readonly. A sensible way to do this might be to reject all
2099          * attempts to truncate a read-only file, because a creat() call
2100          * always implies file truncation.
2101          * ... but this isn't really fair.  A process may reasonably call
2102          * ftruncate on an open file descriptor on a file with perm 000.
2103          * We must trust the client to do permission checking - using "ACCESS"
2104          * with NFSv3.
2105          */
2106         if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2107             inode->i_uid == current_fsuid())
2108                 return 0;
2109
2110         /* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2111         err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2112
2113         /* Allow read access to binaries even when mode 111 */
2114         if (err == -EACCES && S_ISREG(inode->i_mode) &&
2115             acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
2116                 err = inode_permission(inode, MAY_EXEC);
2117         if (err)
2118                 goto nfsd_out;
2119
2120         /* Do integrity (permission) checking now, but defer incrementing
2121          * IMA counts to the actual file open.
2122          */
2123         path.mnt = exp->ex_path.mnt;
2124         path.dentry = dentry;
2125 nfsd_out:
2126         return err? nfserrno(err) : 0;
2127 }
2128
2129 void
2130 nfsd_racache_shutdown(void)
2131 {
2132         struct raparms *raparm, *last_raparm;
2133         unsigned int i;
2134
2135         dprintk("nfsd: freeing readahead buffers.\n");
2136
2137         for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2138                 raparm = raparm_hash[i].pb_head;
2139                 while(raparm) {
2140                         last_raparm = raparm;
2141                         raparm = raparm->p_next;
2142                         kfree(last_raparm);
2143                 }
2144                 raparm_hash[i].pb_head = NULL;
2145         }
2146 }
2147 /*
2148  * Initialize readahead param cache
2149  */
2150 int
2151 nfsd_racache_init(int cache_size)
2152 {
2153         int     i;
2154         int     j = 0;
2155         int     nperbucket;
2156         struct raparms **raparm = NULL;
2157
2158
2159         if (raparm_hash[0].pb_head)
2160                 return 0;
2161         nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2162         if (nperbucket < 2)
2163                 nperbucket = 2;
2164         cache_size = nperbucket * RAPARM_HASH_SIZE;
2165
2166         dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2167
2168         for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2169                 spin_lock_init(&raparm_hash[i].pb_lock);
2170
2171                 raparm = &raparm_hash[i].pb_head;
2172                 for (j = 0; j < nperbucket; j++) {
2173                         *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2174                         if (!*raparm)
2175                                 goto out_nomem;
2176                         raparm = &(*raparm)->p_next;
2177                 }
2178                 *raparm = NULL;
2179         }
2180
2181         nfsdstats.ra_size = cache_size;
2182         return 0;
2183
2184 out_nomem:
2185         dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2186         nfsd_racache_shutdown();
2187         return -ENOMEM;
2188 }
2189
2190 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
2191 struct posix_acl *
2192 nfsd_get_posix_acl(struct svc_fh *fhp, int type)
2193 {
2194         struct inode *inode = fhp->fh_dentry->d_inode;
2195         char *name;
2196         void *value = NULL;
2197         ssize_t size;
2198         struct posix_acl *acl;
2199
2200         if (!IS_POSIXACL(inode))
2201                 return ERR_PTR(-EOPNOTSUPP);
2202
2203         switch (type) {
2204         case ACL_TYPE_ACCESS:
2205                 name = POSIX_ACL_XATTR_ACCESS;
2206                 break;
2207         case ACL_TYPE_DEFAULT:
2208                 name = POSIX_ACL_XATTR_DEFAULT;
2209                 break;
2210         default:
2211                 return ERR_PTR(-EOPNOTSUPP);
2212         }
2213
2214         size = nfsd_getxattr(fhp->fh_dentry, name, &value);
2215         if (size < 0)
2216                 return ERR_PTR(size);
2217
2218         acl = posix_acl_from_xattr(value, size);
2219         kfree(value);
2220         return acl;
2221 }
2222
2223 int
2224 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
2225 {
2226         struct inode *inode = fhp->fh_dentry->d_inode;
2227         char *name;
2228         void *value = NULL;
2229         size_t size;
2230         int error;
2231
2232         if (!IS_POSIXACL(inode) ||
2233             !inode->i_op->setxattr || !inode->i_op->removexattr)
2234                 return -EOPNOTSUPP;
2235         switch(type) {
2236                 case ACL_TYPE_ACCESS:
2237                         name = POSIX_ACL_XATTR_ACCESS;
2238                         break;
2239                 case ACL_TYPE_DEFAULT:
2240                         name = POSIX_ACL_XATTR_DEFAULT;
2241                         break;
2242                 default:
2243                         return -EOPNOTSUPP;
2244         }
2245
2246         if (acl && acl->a_count) {
2247                 size = posix_acl_xattr_size(acl->a_count);
2248                 value = kmalloc(size, GFP_KERNEL);
2249                 if (!value)
2250                         return -ENOMEM;
2251                 error = posix_acl_to_xattr(acl, value, size);
2252                 if (error < 0)
2253                         goto getout;
2254                 size = error;
2255         } else
2256                 size = 0;
2257
2258         error = mnt_want_write(fhp->fh_export->ex_path.mnt);
2259         if (error)
2260                 goto getout;
2261         if (size)
2262                 error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
2263         else {
2264                 if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
2265                         error = 0;
2266                 else {
2267                         error = vfs_removexattr(fhp->fh_dentry, name);
2268                         if (error == -ENODATA)
2269                                 error = 0;
2270                 }
2271         }
2272         mnt_drop_write(fhp->fh_export->ex_path.mnt);
2273
2274 getout:
2275         kfree(value);
2276         return error;
2277 }
2278 #endif  /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */