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