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