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