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