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