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