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