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