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