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