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