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