1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/fcntl.h> 43 #include <sys/file.h> 44 #include <sys/kdb.h> 45 #include <sys/stat.h> 46 #include <sys/priv.h> 47 #include <sys/proc.h> 48 #include <sys/limits.h> 49 #include <sys/lock.h> 50 #include <sys/mount.h> 51 #include <sys/mutex.h> 52 #include <sys/namei.h> 53 #include <sys/vnode.h> 54 #include <sys/bio.h> 55 #include <sys/buf.h> 56 #include <sys/filio.h> 57 #include <sys/resourcevar.h> 58 #include <sys/sx.h> 59 #include <sys/sysctl.h> 60 #include <sys/ttycom.h> 61 #include <sys/conf.h> 62 #include <sys/syslog.h> 63 #include <sys/unistd.h> 64 65 #include <security/audit/audit.h> 66 #include <security/mac/mac_framework.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_extern.h> 70 #include <vm/pmap.h> 71 #include <vm/vm_map.h> 72 #include <vm/vm_object.h> 73 #include <vm/vm_page.h> 74 75 static fo_rdwr_t vn_read; 76 static fo_rdwr_t vn_write; 77 static fo_rdwr_t vn_io_fault; 78 static fo_truncate_t vn_truncate; 79 static fo_ioctl_t vn_ioctl; 80 static fo_poll_t vn_poll; 81 static fo_kqfilter_t vn_kqfilter; 82 static fo_stat_t vn_statfile; 83 static fo_close_t vn_closefile; 84 85 struct fileops vnops = { 86 .fo_read = vn_io_fault, 87 .fo_write = vn_io_fault, 88 .fo_truncate = vn_truncate, 89 .fo_ioctl = vn_ioctl, 90 .fo_poll = vn_poll, 91 .fo_kqfilter = vn_kqfilter, 92 .fo_stat = vn_statfile, 93 .fo_close = vn_closefile, 94 .fo_chmod = vn_chmod, 95 .fo_chown = vn_chown, 96 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 97 }; 98 99 int 100 vn_open(ndp, flagp, cmode, fp) 101 struct nameidata *ndp; 102 int *flagp, cmode; 103 struct file *fp; 104 { 105 struct thread *td = ndp->ni_cnd.cn_thread; 106 107 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp)); 108 } 109 110 /* 111 * Common code for vnode open operations via a name lookup. 112 * Lookup the vnode and invoke VOP_CREATE if needed. 113 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 114 * 115 * Note that this does NOT free nameidata for the successful case, 116 * due to the NDINIT being done elsewhere. 117 */ 118 int 119 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags, 120 struct ucred *cred, struct file *fp) 121 { 122 struct vnode *vp; 123 struct mount *mp; 124 struct thread *td = ndp->ni_cnd.cn_thread; 125 struct vattr vat; 126 struct vattr *vap = &vat; 127 int fmode, error; 128 129 restart: 130 fmode = *flagp; 131 if (fmode & O_CREAT) { 132 ndp->ni_cnd.cn_nameiop = CREATE; 133 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF; 134 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 135 ndp->ni_cnd.cn_flags |= FOLLOW; 136 if (!(vn_open_flags & VN_OPEN_NOAUDIT)) 137 ndp->ni_cnd.cn_flags |= AUDITVNODE1; 138 if (vn_open_flags & VN_OPEN_NOCAPCHECK) 139 ndp->ni_cnd.cn_flags |= NOCAPCHECK; 140 bwillwrite(); 141 if ((error = namei(ndp)) != 0) 142 return (error); 143 if (ndp->ni_vp == NULL) { 144 VATTR_NULL(vap); 145 vap->va_type = VREG; 146 vap->va_mode = cmode; 147 if (fmode & O_EXCL) 148 vap->va_vaflags |= VA_EXCLUSIVE; 149 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) { 150 NDFREE(ndp, NDF_ONLY_PNBUF); 151 vput(ndp->ni_dvp); 152 if ((error = vn_start_write(NULL, &mp, 153 V_XSLEEP | PCATCH)) != 0) 154 return (error); 155 goto restart; 156 } 157 #ifdef MAC 158 error = mac_vnode_check_create(cred, ndp->ni_dvp, 159 &ndp->ni_cnd, vap); 160 if (error == 0) 161 #endif 162 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 163 &ndp->ni_cnd, vap); 164 vput(ndp->ni_dvp); 165 vn_finished_write(mp); 166 if (error) { 167 NDFREE(ndp, NDF_ONLY_PNBUF); 168 return (error); 169 } 170 fmode &= ~O_TRUNC; 171 vp = ndp->ni_vp; 172 } else { 173 if (ndp->ni_dvp == ndp->ni_vp) 174 vrele(ndp->ni_dvp); 175 else 176 vput(ndp->ni_dvp); 177 ndp->ni_dvp = NULL; 178 vp = ndp->ni_vp; 179 if (fmode & O_EXCL) { 180 error = EEXIST; 181 goto bad; 182 } 183 fmode &= ~O_CREAT; 184 } 185 } else { 186 ndp->ni_cnd.cn_nameiop = LOOKUP; 187 ndp->ni_cnd.cn_flags = ISOPEN | 188 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 189 if (!(fmode & FWRITE)) 190 ndp->ni_cnd.cn_flags |= LOCKSHARED; 191 if (!(vn_open_flags & VN_OPEN_NOAUDIT)) 192 ndp->ni_cnd.cn_flags |= AUDITVNODE1; 193 if (vn_open_flags & VN_OPEN_NOCAPCHECK) 194 ndp->ni_cnd.cn_flags |= NOCAPCHECK; 195 if ((error = namei(ndp)) != 0) 196 return (error); 197 vp = ndp->ni_vp; 198 } 199 error = vn_open_vnode(vp, fmode, cred, td, fp); 200 if (error) 201 goto bad; 202 *flagp = fmode; 203 return (0); 204 bad: 205 NDFREE(ndp, NDF_ONLY_PNBUF); 206 vput(vp); 207 *flagp = fmode; 208 ndp->ni_vp = NULL; 209 return (error); 210 } 211 212 /* 213 * Common code for vnode open operations once a vnode is located. 214 * Check permissions, and call the VOP_OPEN routine. 215 */ 216 int 217 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred, 218 struct thread *td, struct file *fp) 219 { 220 struct mount *mp; 221 accmode_t accmode; 222 struct flock lf; 223 int error, have_flock, lock_flags, type; 224 225 if (vp->v_type == VLNK) 226 return (EMLINK); 227 if (vp->v_type == VSOCK) 228 return (EOPNOTSUPP); 229 if (vp->v_type != VDIR && fmode & O_DIRECTORY) 230 return (ENOTDIR); 231 accmode = 0; 232 if (fmode & (FWRITE | O_TRUNC)) { 233 if (vp->v_type == VDIR) 234 return (EISDIR); 235 accmode |= VWRITE; 236 } 237 if (fmode & FREAD) 238 accmode |= VREAD; 239 if (fmode & FEXEC) 240 accmode |= VEXEC; 241 if ((fmode & O_APPEND) && (fmode & FWRITE)) 242 accmode |= VAPPEND; 243 #ifdef MAC 244 error = mac_vnode_check_open(cred, vp, accmode); 245 if (error) 246 return (error); 247 #endif 248 if ((fmode & O_CREAT) == 0) { 249 if (accmode & VWRITE) { 250 error = vn_writechk(vp); 251 if (error) 252 return (error); 253 } 254 if (accmode) { 255 error = VOP_ACCESS(vp, accmode, cred, td); 256 if (error) 257 return (error); 258 } 259 } 260 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0) 261 return (error); 262 263 if (fmode & (O_EXLOCK | O_SHLOCK)) { 264 KASSERT(fp != NULL, ("open with flock requires fp")); 265 lock_flags = VOP_ISLOCKED(vp); 266 VOP_UNLOCK(vp, 0); 267 lf.l_whence = SEEK_SET; 268 lf.l_start = 0; 269 lf.l_len = 0; 270 if (fmode & O_EXLOCK) 271 lf.l_type = F_WRLCK; 272 else 273 lf.l_type = F_RDLCK; 274 type = F_FLOCK; 275 if ((fmode & FNONBLOCK) == 0) 276 type |= F_WAIT; 277 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type); 278 have_flock = (error == 0); 279 vn_lock(vp, lock_flags | LK_RETRY); 280 if (error == 0 && vp->v_iflag & VI_DOOMED) 281 error = ENOENT; 282 /* 283 * Another thread might have used this vnode as an 284 * executable while the vnode lock was dropped. 285 * Ensure the vnode is still able to be opened for 286 * writing after the lock has been obtained. 287 */ 288 if (error == 0 && accmode & VWRITE) 289 error = vn_writechk(vp); 290 if (error) { 291 VOP_UNLOCK(vp, 0); 292 if (have_flock) { 293 lf.l_whence = SEEK_SET; 294 lf.l_start = 0; 295 lf.l_len = 0; 296 lf.l_type = F_UNLCK; 297 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, 298 F_FLOCK); 299 } 300 vn_start_write(vp, &mp, V_WAIT); 301 vn_lock(vp, lock_flags | LK_RETRY); 302 (void)VOP_CLOSE(vp, fmode, cred, td); 303 vn_finished_write(mp); 304 return (error); 305 } 306 fp->f_flag |= FHASLOCK; 307 } 308 if (fmode & FWRITE) { 309 VOP_ADD_WRITECOUNT(vp, 1); 310 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 311 __func__, vp, vp->v_writecount); 312 } 313 ASSERT_VOP_LOCKED(vp, "vn_open_vnode"); 314 return (0); 315 } 316 317 /* 318 * Check for write permissions on the specified vnode. 319 * Prototype text segments cannot be written. 320 */ 321 int 322 vn_writechk(vp) 323 register struct vnode *vp; 324 { 325 326 ASSERT_VOP_LOCKED(vp, "vn_writechk"); 327 /* 328 * If there's shared text associated with 329 * the vnode, try to free it up once. If 330 * we fail, we can't allow writing. 331 */ 332 if (VOP_IS_TEXT(vp)) 333 return (ETXTBSY); 334 335 return (0); 336 } 337 338 /* 339 * Vnode close call 340 */ 341 int 342 vn_close(vp, flags, file_cred, td) 343 register struct vnode *vp; 344 int flags; 345 struct ucred *file_cred; 346 struct thread *td; 347 { 348 struct mount *mp; 349 int error, lock_flags; 350 351 if (!(flags & FWRITE) && vp->v_mount != NULL && 352 vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED) 353 lock_flags = LK_SHARED; 354 else 355 lock_flags = LK_EXCLUSIVE; 356 357 vn_start_write(vp, &mp, V_WAIT); 358 vn_lock(vp, lock_flags | LK_RETRY); 359 if (flags & FWRITE) { 360 VNASSERT(vp->v_writecount > 0, vp, 361 ("vn_close: negative writecount")); 362 VOP_ADD_WRITECOUNT(vp, -1); 363 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 364 __func__, vp, vp->v_writecount); 365 } 366 error = VOP_CLOSE(vp, flags, file_cred, td); 367 vput(vp); 368 vn_finished_write(mp); 369 return (error); 370 } 371 372 /* 373 * Heuristic to detect sequential operation. 374 */ 375 static int 376 sequential_heuristic(struct uio *uio, struct file *fp) 377 { 378 379 if (atomic_load_acq_int(&(fp->f_flag)) & FRDAHEAD) 380 return (fp->f_seqcount << IO_SEQSHIFT); 381 382 /* 383 * Offset 0 is handled specially. open() sets f_seqcount to 1 so 384 * that the first I/O is normally considered to be slightly 385 * sequential. Seeking to offset 0 doesn't change sequentiality 386 * unless previous seeks have reduced f_seqcount to 0, in which 387 * case offset 0 is not special. 388 */ 389 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 390 uio->uio_offset == fp->f_nextoff) { 391 /* 392 * f_seqcount is in units of fixed-size blocks so that it 393 * depends mainly on the amount of sequential I/O and not 394 * much on the number of sequential I/O's. The fixed size 395 * of 16384 is hard-coded here since it is (not quite) just 396 * a magic size that works well here. This size is more 397 * closely related to the best I/O size for real disks than 398 * to any block size used by software. 399 */ 400 fp->f_seqcount += howmany(uio->uio_resid, 16384); 401 if (fp->f_seqcount > IO_SEQMAX) 402 fp->f_seqcount = IO_SEQMAX; 403 return (fp->f_seqcount << IO_SEQSHIFT); 404 } 405 406 /* Not sequential. Quickly draw-down sequentiality. */ 407 if (fp->f_seqcount > 1) 408 fp->f_seqcount = 1; 409 else 410 fp->f_seqcount = 0; 411 return (0); 412 } 413 414 /* 415 * Package up an I/O request on a vnode into a uio and do it. 416 */ 417 int 418 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 419 enum uio_seg segflg, int ioflg, struct ucred *active_cred, 420 struct ucred *file_cred, ssize_t *aresid, struct thread *td) 421 { 422 struct uio auio; 423 struct iovec aiov; 424 struct mount *mp; 425 struct ucred *cred; 426 void *rl_cookie; 427 int error, lock_flags; 428 429 auio.uio_iov = &aiov; 430 auio.uio_iovcnt = 1; 431 aiov.iov_base = base; 432 aiov.iov_len = len; 433 auio.uio_resid = len; 434 auio.uio_offset = offset; 435 auio.uio_segflg = segflg; 436 auio.uio_rw = rw; 437 auio.uio_td = td; 438 error = 0; 439 440 if ((ioflg & IO_NODELOCKED) == 0) { 441 if (rw == UIO_READ) { 442 rl_cookie = vn_rangelock_rlock(vp, offset, 443 offset + len); 444 } else { 445 rl_cookie = vn_rangelock_wlock(vp, offset, 446 offset + len); 447 } 448 mp = NULL; 449 if (rw == UIO_WRITE) { 450 if (vp->v_type != VCHR && 451 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) 452 != 0) 453 goto out; 454 if (MNT_SHARED_WRITES(mp) || 455 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) 456 lock_flags = LK_SHARED; 457 else 458 lock_flags = LK_EXCLUSIVE; 459 } else 460 lock_flags = LK_SHARED; 461 vn_lock(vp, lock_flags | LK_RETRY); 462 } else 463 rl_cookie = NULL; 464 465 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 466 #ifdef MAC 467 if ((ioflg & IO_NOMACCHECK) == 0) { 468 if (rw == UIO_READ) 469 error = mac_vnode_check_read(active_cred, file_cred, 470 vp); 471 else 472 error = mac_vnode_check_write(active_cred, file_cred, 473 vp); 474 } 475 #endif 476 if (error == 0) { 477 if (file_cred != NULL) 478 cred = file_cred; 479 else 480 cred = active_cred; 481 if (rw == UIO_READ) 482 error = VOP_READ(vp, &auio, ioflg, cred); 483 else 484 error = VOP_WRITE(vp, &auio, ioflg, cred); 485 } 486 if (aresid) 487 *aresid = auio.uio_resid; 488 else 489 if (auio.uio_resid && error == 0) 490 error = EIO; 491 if ((ioflg & IO_NODELOCKED) == 0) { 492 VOP_UNLOCK(vp, 0); 493 if (mp != NULL) 494 vn_finished_write(mp); 495 } 496 out: 497 if (rl_cookie != NULL) 498 vn_rangelock_unlock(vp, rl_cookie); 499 return (error); 500 } 501 502 /* 503 * Package up an I/O request on a vnode into a uio and do it. The I/O 504 * request is split up into smaller chunks and we try to avoid saturating 505 * the buffer cache while potentially holding a vnode locked, so we 506 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield() 507 * to give other processes a chance to lock the vnode (either other processes 508 * core'ing the same binary, or unrelated processes scanning the directory). 509 */ 510 int 511 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred, 512 file_cred, aresid, td) 513 enum uio_rw rw; 514 struct vnode *vp; 515 void *base; 516 size_t len; 517 off_t offset; 518 enum uio_seg segflg; 519 int ioflg; 520 struct ucred *active_cred; 521 struct ucred *file_cred; 522 size_t *aresid; 523 struct thread *td; 524 { 525 int error = 0; 526 ssize_t iaresid; 527 528 do { 529 int chunk; 530 531 /* 532 * Force `offset' to a multiple of MAXBSIZE except possibly 533 * for the first chunk, so that filesystems only need to 534 * write full blocks except possibly for the first and last 535 * chunks. 536 */ 537 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE; 538 539 if (chunk > len) 540 chunk = len; 541 if (rw != UIO_READ && vp->v_type == VREG) 542 bwillwrite(); 543 iaresid = 0; 544 error = vn_rdwr(rw, vp, base, chunk, offset, segflg, 545 ioflg, active_cred, file_cred, &iaresid, td); 546 len -= chunk; /* aresid calc already includes length */ 547 if (error) 548 break; 549 offset += chunk; 550 base = (char *)base + chunk; 551 kern_yield(PRI_USER); 552 } while (len); 553 if (aresid) 554 *aresid = len + iaresid; 555 return (error); 556 } 557 558 off_t 559 foffset_lock(struct file *fp, int flags) 560 { 561 struct mtx *mtxp; 562 off_t res; 563 564 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 565 566 #if OFF_MAX <= LONG_MAX 567 /* 568 * Caller only wants the current f_offset value. Assume that 569 * the long and shorter integer types reads are atomic. 570 */ 571 if ((flags & FOF_NOLOCK) != 0) 572 return (fp->f_offset); 573 #endif 574 575 /* 576 * According to McKusick the vn lock was protecting f_offset here. 577 * It is now protected by the FOFFSET_LOCKED flag. 578 */ 579 mtxp = mtx_pool_find(mtxpool_sleep, fp); 580 mtx_lock(mtxp); 581 if ((flags & FOF_NOLOCK) == 0) { 582 while (fp->f_vnread_flags & FOFFSET_LOCKED) { 583 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING; 584 msleep(&fp->f_vnread_flags, mtxp, PUSER -1, 585 "vofflock", 0); 586 } 587 fp->f_vnread_flags |= FOFFSET_LOCKED; 588 } 589 res = fp->f_offset; 590 mtx_unlock(mtxp); 591 return (res); 592 } 593 594 void 595 foffset_unlock(struct file *fp, off_t val, int flags) 596 { 597 struct mtx *mtxp; 598 599 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 600 601 #if OFF_MAX <= LONG_MAX 602 if ((flags & FOF_NOLOCK) != 0) { 603 if ((flags & FOF_NOUPDATE) == 0) 604 fp->f_offset = val; 605 if ((flags & FOF_NEXTOFF) != 0) 606 fp->f_nextoff = val; 607 return; 608 } 609 #endif 610 611 mtxp = mtx_pool_find(mtxpool_sleep, fp); 612 mtx_lock(mtxp); 613 if ((flags & FOF_NOUPDATE) == 0) 614 fp->f_offset = val; 615 if ((flags & FOF_NEXTOFF) != 0) 616 fp->f_nextoff = val; 617 if ((flags & FOF_NOLOCK) == 0) { 618 KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0, 619 ("Lost FOFFSET_LOCKED")); 620 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING) 621 wakeup(&fp->f_vnread_flags); 622 fp->f_vnread_flags = 0; 623 } 624 mtx_unlock(mtxp); 625 } 626 627 void 628 foffset_lock_uio(struct file *fp, struct uio *uio, int flags) 629 { 630 631 if ((flags & FOF_OFFSET) == 0) 632 uio->uio_offset = foffset_lock(fp, flags); 633 } 634 635 void 636 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags) 637 { 638 639 if ((flags & FOF_OFFSET) == 0) 640 foffset_unlock(fp, uio->uio_offset, flags); 641 } 642 643 static int 644 get_advice(struct file *fp, struct uio *uio) 645 { 646 struct mtx *mtxp; 647 int ret; 648 649 ret = POSIX_FADV_NORMAL; 650 if (fp->f_advice == NULL) 651 return (ret); 652 653 mtxp = mtx_pool_find(mtxpool_sleep, fp); 654 mtx_lock(mtxp); 655 if (uio->uio_offset >= fp->f_advice->fa_start && 656 uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end) 657 ret = fp->f_advice->fa_advice; 658 mtx_unlock(mtxp); 659 return (ret); 660 } 661 662 /* 663 * File table vnode read routine. 664 */ 665 static int 666 vn_read(fp, uio, active_cred, flags, td) 667 struct file *fp; 668 struct uio *uio; 669 struct ucred *active_cred; 670 int flags; 671 struct thread *td; 672 { 673 struct vnode *vp; 674 struct mtx *mtxp; 675 int error, ioflag; 676 int advice; 677 off_t offset, start, end; 678 679 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 680 uio->uio_td, td)); 681 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 682 vp = fp->f_vnode; 683 ioflag = 0; 684 if (fp->f_flag & FNONBLOCK) 685 ioflag |= IO_NDELAY; 686 if (fp->f_flag & O_DIRECT) 687 ioflag |= IO_DIRECT; 688 advice = get_advice(fp, uio); 689 vn_lock(vp, LK_SHARED | LK_RETRY); 690 691 switch (advice) { 692 case POSIX_FADV_NORMAL: 693 case POSIX_FADV_SEQUENTIAL: 694 case POSIX_FADV_NOREUSE: 695 ioflag |= sequential_heuristic(uio, fp); 696 break; 697 case POSIX_FADV_RANDOM: 698 /* Disable read-ahead for random I/O. */ 699 break; 700 } 701 offset = uio->uio_offset; 702 703 #ifdef MAC 704 error = mac_vnode_check_read(active_cred, fp->f_cred, vp); 705 if (error == 0) 706 #endif 707 error = VOP_READ(vp, uio, ioflag, fp->f_cred); 708 fp->f_nextoff = uio->uio_offset; 709 VOP_UNLOCK(vp, 0); 710 if (error == 0 && advice == POSIX_FADV_NOREUSE && 711 offset != uio->uio_offset) { 712 /* 713 * Use POSIX_FADV_DONTNEED to flush clean pages and 714 * buffers for the backing file after a 715 * POSIX_FADV_NOREUSE read(2). To optimize the common 716 * case of using POSIX_FADV_NOREUSE with sequential 717 * access, track the previous implicit DONTNEED 718 * request and grow this request to include the 719 * current read(2) in addition to the previous 720 * DONTNEED. With purely sequential access this will 721 * cause the DONTNEED requests to continously grow to 722 * cover all of the previously read regions of the 723 * file. This allows filesystem blocks that are 724 * accessed by multiple calls to read(2) to be flushed 725 * once the last read(2) finishes. 726 */ 727 start = offset; 728 end = uio->uio_offset - 1; 729 mtxp = mtx_pool_find(mtxpool_sleep, fp); 730 mtx_lock(mtxp); 731 if (fp->f_advice != NULL && 732 fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) { 733 if (start != 0 && fp->f_advice->fa_prevend + 1 == start) 734 start = fp->f_advice->fa_prevstart; 735 else if (fp->f_advice->fa_prevstart != 0 && 736 fp->f_advice->fa_prevstart == end + 1) 737 end = fp->f_advice->fa_prevend; 738 fp->f_advice->fa_prevstart = start; 739 fp->f_advice->fa_prevend = end; 740 } 741 mtx_unlock(mtxp); 742 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED); 743 } 744 return (error); 745 } 746 747 /* 748 * File table vnode write routine. 749 */ 750 static int 751 vn_write(fp, uio, active_cred, flags, td) 752 struct file *fp; 753 struct uio *uio; 754 struct ucred *active_cred; 755 int flags; 756 struct thread *td; 757 { 758 struct vnode *vp; 759 struct mount *mp; 760 struct mtx *mtxp; 761 int error, ioflag, lock_flags; 762 int advice; 763 off_t offset, start, end; 764 765 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 766 uio->uio_td, td)); 767 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 768 vp = fp->f_vnode; 769 if (vp->v_type == VREG) 770 bwillwrite(); 771 ioflag = IO_UNIT; 772 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 773 ioflag |= IO_APPEND; 774 if (fp->f_flag & FNONBLOCK) 775 ioflag |= IO_NDELAY; 776 if (fp->f_flag & O_DIRECT) 777 ioflag |= IO_DIRECT; 778 if ((fp->f_flag & O_FSYNC) || 779 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 780 ioflag |= IO_SYNC; 781 mp = NULL; 782 if (vp->v_type != VCHR && 783 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 784 goto unlock; 785 786 advice = get_advice(fp, uio); 787 788 if (MNT_SHARED_WRITES(mp) || 789 (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) { 790 lock_flags = LK_SHARED; 791 } else { 792 lock_flags = LK_EXCLUSIVE; 793 } 794 795 vn_lock(vp, lock_flags | LK_RETRY); 796 switch (advice) { 797 case POSIX_FADV_NORMAL: 798 case POSIX_FADV_SEQUENTIAL: 799 case POSIX_FADV_NOREUSE: 800 ioflag |= sequential_heuristic(uio, fp); 801 break; 802 case POSIX_FADV_RANDOM: 803 /* XXX: Is this correct? */ 804 break; 805 } 806 offset = uio->uio_offset; 807 808 #ifdef MAC 809 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 810 if (error == 0) 811 #endif 812 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred); 813 fp->f_nextoff = uio->uio_offset; 814 VOP_UNLOCK(vp, 0); 815 if (vp->v_type != VCHR) 816 vn_finished_write(mp); 817 if (error == 0 && advice == POSIX_FADV_NOREUSE && 818 offset != uio->uio_offset) { 819 /* 820 * Use POSIX_FADV_DONTNEED to flush clean pages and 821 * buffers for the backing file after a 822 * POSIX_FADV_NOREUSE write(2). To optimize the 823 * common case of using POSIX_FADV_NOREUSE with 824 * sequential access, track the previous implicit 825 * DONTNEED request and grow this request to include 826 * the current write(2) in addition to the previous 827 * DONTNEED. With purely sequential access this will 828 * cause the DONTNEED requests to continously grow to 829 * cover all of the previously written regions of the 830 * file. 831 * 832 * Note that the blocks just written are almost 833 * certainly still dirty, so this only works when 834 * VOP_ADVISE() calls from subsequent writes push out 835 * the data written by this write(2) once the backing 836 * buffers are clean. However, as compared to forcing 837 * IO_DIRECT, this gives much saner behavior. Write 838 * clustering is still allowed, and clean pages are 839 * merely moved to the cache page queue rather than 840 * outright thrown away. This means a subsequent 841 * read(2) can still avoid hitting the disk if the 842 * pages have not been reclaimed. 843 * 844 * This does make POSIX_FADV_NOREUSE largely useless 845 * with non-sequential access. However, sequential 846 * access is the more common use case and the flag is 847 * merely advisory. 848 */ 849 start = offset; 850 end = uio->uio_offset - 1; 851 mtxp = mtx_pool_find(mtxpool_sleep, fp); 852 mtx_lock(mtxp); 853 if (fp->f_advice != NULL && 854 fp->f_advice->fa_advice == POSIX_FADV_NOREUSE) { 855 if (start != 0 && fp->f_advice->fa_prevend + 1 == start) 856 start = fp->f_advice->fa_prevstart; 857 else if (fp->f_advice->fa_prevstart != 0 && 858 fp->f_advice->fa_prevstart == end + 1) 859 end = fp->f_advice->fa_prevend; 860 fp->f_advice->fa_prevstart = start; 861 fp->f_advice->fa_prevend = end; 862 } 863 mtx_unlock(mtxp); 864 error = VOP_ADVISE(vp, start, end, POSIX_FADV_DONTNEED); 865 } 866 867 unlock: 868 return (error); 869 } 870 871 static const int io_hold_cnt = 16; 872 static int vn_io_fault_enable = 1; 873 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW, 874 &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance"); 875 static unsigned long vn_io_faults_cnt; 876 SYSCTL_LONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD, 877 &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers"); 878 879 /* 880 * The vn_io_fault() is a wrapper around vn_read() and vn_write() to 881 * prevent the following deadlock: 882 * 883 * Assume that the thread A reads from the vnode vp1 into userspace 884 * buffer buf1 backed by the pages of vnode vp2. If a page in buf1 is 885 * currently not resident, then system ends up with the call chain 886 * vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] -> 887 * vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2) 888 * which establishes lock order vp1->vn_lock, then vp2->vn_lock. 889 * If, at the same time, thread B reads from vnode vp2 into buffer buf2 890 * backed by the pages of vnode vp1, and some page in buf2 is not 891 * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock. 892 * 893 * To prevent the lock order reversal and deadlock, vn_io_fault() does 894 * not allow page faults to happen during VOP_READ() or VOP_WRITE(). 895 * Instead, it first tries to do the whole range i/o with pagefaults 896 * disabled. If all pages in the i/o buffer are resident and mapped, 897 * VOP will succeed (ignoring the genuine filesystem errors). 898 * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do 899 * i/o in chunks, with all pages in the chunk prefaulted and held 900 * using vm_fault_quick_hold_pages(). 901 * 902 * Filesystems using this deadlock avoidance scheme should use the 903 * array of the held pages from uio, saved in the curthread->td_ma, 904 * instead of doing uiomove(). A helper function 905 * vn_io_fault_uiomove() converts uiomove request into 906 * uiomove_fromphys() over td_ma array. 907 * 908 * Since vnode locks do not cover the whole i/o anymore, rangelocks 909 * make the current i/o request atomic with respect to other i/os and 910 * truncations. 911 */ 912 static int 913 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred, 914 int flags, struct thread *td) 915 { 916 vm_page_t ma[io_hold_cnt + 2]; 917 struct uio *uio_clone, short_uio; 918 struct iovec short_iovec[1]; 919 fo_rdwr_t *doio; 920 struct vnode *vp; 921 void *rl_cookie; 922 struct mount *mp; 923 vm_page_t *prev_td_ma; 924 int cnt, error, save, saveheld, prev_td_ma_cnt; 925 vm_offset_t addr, end; 926 vm_prot_t prot; 927 size_t len, resid; 928 ssize_t adv; 929 930 if (uio->uio_rw == UIO_READ) 931 doio = vn_read; 932 else 933 doio = vn_write; 934 vp = fp->f_vnode; 935 foffset_lock_uio(fp, uio, flags); 936 937 if (uio->uio_segflg != UIO_USERSPACE || vp->v_type != VREG || 938 ((mp = vp->v_mount) != NULL && 939 (mp->mnt_kern_flag & MNTK_NO_IOPF) == 0) || 940 !vn_io_fault_enable) { 941 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td); 942 goto out_last; 943 } 944 945 /* 946 * The UFS follows IO_UNIT directive and replays back both 947 * uio_offset and uio_resid if an error is encountered during the 948 * operation. But, since the iovec may be already advanced, 949 * uio is still in an inconsistent state. 950 * 951 * Cache a copy of the original uio, which is advanced to the redo 952 * point using UIO_NOCOPY below. 953 */ 954 uio_clone = cloneuio(uio); 955 resid = uio->uio_resid; 956 957 short_uio.uio_segflg = UIO_USERSPACE; 958 short_uio.uio_rw = uio->uio_rw; 959 short_uio.uio_td = uio->uio_td; 960 961 if (uio->uio_rw == UIO_READ) { 962 prot = VM_PROT_WRITE; 963 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset, 964 uio->uio_offset + uio->uio_resid); 965 } else { 966 prot = VM_PROT_READ; 967 if ((fp->f_flag & O_APPEND) != 0 || (flags & FOF_OFFSET) == 0) 968 /* For appenders, punt and lock the whole range. */ 969 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 970 else 971 rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset, 972 uio->uio_offset + uio->uio_resid); 973 } 974 975 save = vm_fault_disable_pagefaults(); 976 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td); 977 if (error != EFAULT) 978 goto out; 979 980 atomic_add_long(&vn_io_faults_cnt, 1); 981 uio_clone->uio_segflg = UIO_NOCOPY; 982 uiomove(NULL, resid - uio->uio_resid, uio_clone); 983 uio_clone->uio_segflg = uio->uio_segflg; 984 985 saveheld = curthread_pflags_set(TDP_UIOHELD); 986 prev_td_ma = td->td_ma; 987 prev_td_ma_cnt = td->td_ma_cnt; 988 989 while (uio_clone->uio_resid != 0) { 990 len = uio_clone->uio_iov->iov_len; 991 if (len == 0) { 992 KASSERT(uio_clone->uio_iovcnt >= 1, 993 ("iovcnt underflow")); 994 uio_clone->uio_iov++; 995 uio_clone->uio_iovcnt--; 996 continue; 997 } 998 999 addr = (vm_offset_t)uio_clone->uio_iov->iov_base; 1000 end = round_page(addr + len); 1001 cnt = howmany(end - trunc_page(addr), PAGE_SIZE); 1002 /* 1003 * A perfectly misaligned address and length could cause 1004 * both the start and the end of the chunk to use partial 1005 * page. +2 accounts for such a situation. 1006 */ 1007 if (cnt > io_hold_cnt + 2) { 1008 len = io_hold_cnt * PAGE_SIZE; 1009 KASSERT(howmany(round_page(addr + len) - 1010 trunc_page(addr), PAGE_SIZE) <= io_hold_cnt + 2, 1011 ("cnt overflow")); 1012 } 1013 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map, 1014 addr, len, prot, ma, io_hold_cnt + 2); 1015 if (cnt == -1) { 1016 error = EFAULT; 1017 break; 1018 } 1019 short_uio.uio_iov = &short_iovec[0]; 1020 short_iovec[0].iov_base = (void *)addr; 1021 short_uio.uio_iovcnt = 1; 1022 short_uio.uio_resid = short_iovec[0].iov_len = len; 1023 short_uio.uio_offset = uio_clone->uio_offset; 1024 td->td_ma = ma; 1025 td->td_ma_cnt = cnt; 1026 1027 error = doio(fp, &short_uio, active_cred, flags | FOF_OFFSET, 1028 td); 1029 vm_page_unhold_pages(ma, cnt); 1030 adv = len - short_uio.uio_resid; 1031 1032 uio_clone->uio_iov->iov_base = 1033 (char *)uio_clone->uio_iov->iov_base + adv; 1034 uio_clone->uio_iov->iov_len -= adv; 1035 uio_clone->uio_resid -= adv; 1036 uio_clone->uio_offset += adv; 1037 1038 uio->uio_resid -= adv; 1039 uio->uio_offset += adv; 1040 1041 if (error != 0 || adv == 0) 1042 break; 1043 } 1044 td->td_ma = prev_td_ma; 1045 td->td_ma_cnt = prev_td_ma_cnt; 1046 curthread_pflags_restore(saveheld); 1047 out: 1048 vm_fault_enable_pagefaults(save); 1049 vn_rangelock_unlock(vp, rl_cookie); 1050 free(uio_clone, M_IOV); 1051 out_last: 1052 foffset_unlock_uio(fp, uio, flags); 1053 return (error); 1054 } 1055 1056 /* 1057 * Helper function to perform the requested uiomove operation using 1058 * the held pages for io->uio_iov[0].iov_base buffer instead of 1059 * copyin/copyout. Access to the pages with uiomove_fromphys() 1060 * instead of iov_base prevents page faults that could occur due to 1061 * pmap_collect() invalidating the mapping created by 1062 * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or 1063 * object cleanup revoking the write access from page mappings. 1064 * 1065 * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove() 1066 * instead of plain uiomove(). 1067 */ 1068 int 1069 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio) 1070 { 1071 struct uio transp_uio; 1072 struct iovec transp_iov[1]; 1073 struct thread *td; 1074 size_t adv; 1075 int error, pgadv; 1076 1077 td = curthread; 1078 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1079 uio->uio_segflg != UIO_USERSPACE) 1080 return (uiomove(data, xfersize, uio)); 1081 1082 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1083 transp_iov[0].iov_base = data; 1084 transp_uio.uio_iov = &transp_iov[0]; 1085 transp_uio.uio_iovcnt = 1; 1086 if (xfersize > uio->uio_resid) 1087 xfersize = uio->uio_resid; 1088 transp_uio.uio_resid = transp_iov[0].iov_len = xfersize; 1089 transp_uio.uio_offset = 0; 1090 transp_uio.uio_segflg = UIO_SYSSPACE; 1091 /* 1092 * Since transp_iov points to data, and td_ma page array 1093 * corresponds to original uio->uio_iov, we need to invert the 1094 * direction of the i/o operation as passed to 1095 * uiomove_fromphys(). 1096 */ 1097 switch (uio->uio_rw) { 1098 case UIO_WRITE: 1099 transp_uio.uio_rw = UIO_READ; 1100 break; 1101 case UIO_READ: 1102 transp_uio.uio_rw = UIO_WRITE; 1103 break; 1104 } 1105 transp_uio.uio_td = uio->uio_td; 1106 error = uiomove_fromphys(td->td_ma, 1107 ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK, 1108 xfersize, &transp_uio); 1109 adv = xfersize - transp_uio.uio_resid; 1110 pgadv = 1111 (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) - 1112 (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT); 1113 td->td_ma += pgadv; 1114 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1115 pgadv)); 1116 td->td_ma_cnt -= pgadv; 1117 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv; 1118 uio->uio_iov->iov_len -= adv; 1119 uio->uio_resid -= adv; 1120 uio->uio_offset += adv; 1121 return (error); 1122 } 1123 1124 /* 1125 * File table truncate routine. 1126 */ 1127 static int 1128 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred, 1129 struct thread *td) 1130 { 1131 struct vattr vattr; 1132 struct mount *mp; 1133 struct vnode *vp; 1134 void *rl_cookie; 1135 int error; 1136 1137 vp = fp->f_vnode; 1138 1139 /* 1140 * Lock the whole range for truncation. Otherwise split i/o 1141 * might happen partly before and partly after the truncation. 1142 */ 1143 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1144 error = vn_start_write(vp, &mp, V_WAIT | PCATCH); 1145 if (error) 1146 goto out1; 1147 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1148 if (vp->v_type == VDIR) { 1149 error = EISDIR; 1150 goto out; 1151 } 1152 #ifdef MAC 1153 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 1154 if (error) 1155 goto out; 1156 #endif 1157 error = vn_writechk(vp); 1158 if (error == 0) { 1159 VATTR_NULL(&vattr); 1160 vattr.va_size = length; 1161 error = VOP_SETATTR(vp, &vattr, fp->f_cred); 1162 } 1163 out: 1164 VOP_UNLOCK(vp, 0); 1165 vn_finished_write(mp); 1166 out1: 1167 vn_rangelock_unlock(vp, rl_cookie); 1168 return (error); 1169 } 1170 1171 /* 1172 * File table vnode stat routine. 1173 */ 1174 static int 1175 vn_statfile(fp, sb, active_cred, td) 1176 struct file *fp; 1177 struct stat *sb; 1178 struct ucred *active_cred; 1179 struct thread *td; 1180 { 1181 struct vnode *vp = fp->f_vnode; 1182 int error; 1183 1184 vn_lock(vp, LK_SHARED | LK_RETRY); 1185 error = vn_stat(vp, sb, active_cred, fp->f_cred, td); 1186 VOP_UNLOCK(vp, 0); 1187 1188 return (error); 1189 } 1190 1191 /* 1192 * Stat a vnode; implementation for the stat syscall 1193 */ 1194 int 1195 vn_stat(vp, sb, active_cred, file_cred, td) 1196 struct vnode *vp; 1197 register struct stat *sb; 1198 struct ucred *active_cred; 1199 struct ucred *file_cred; 1200 struct thread *td; 1201 { 1202 struct vattr vattr; 1203 register struct vattr *vap; 1204 int error; 1205 u_short mode; 1206 1207 #ifdef MAC 1208 error = mac_vnode_check_stat(active_cred, file_cred, vp); 1209 if (error) 1210 return (error); 1211 #endif 1212 1213 vap = &vattr; 1214 1215 /* 1216 * Initialize defaults for new and unusual fields, so that file 1217 * systems which don't support these fields don't need to know 1218 * about them. 1219 */ 1220 vap->va_birthtime.tv_sec = -1; 1221 vap->va_birthtime.tv_nsec = 0; 1222 vap->va_fsid = VNOVAL; 1223 vap->va_rdev = NODEV; 1224 1225 error = VOP_GETATTR(vp, vap, active_cred); 1226 if (error) 1227 return (error); 1228 1229 /* 1230 * Zero the spare stat fields 1231 */ 1232 bzero(sb, sizeof *sb); 1233 1234 /* 1235 * Copy from vattr table 1236 */ 1237 if (vap->va_fsid != VNOVAL) 1238 sb->st_dev = vap->va_fsid; 1239 else 1240 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 1241 sb->st_ino = vap->va_fileid; 1242 mode = vap->va_mode; 1243 switch (vap->va_type) { 1244 case VREG: 1245 mode |= S_IFREG; 1246 break; 1247 case VDIR: 1248 mode |= S_IFDIR; 1249 break; 1250 case VBLK: 1251 mode |= S_IFBLK; 1252 break; 1253 case VCHR: 1254 mode |= S_IFCHR; 1255 break; 1256 case VLNK: 1257 mode |= S_IFLNK; 1258 break; 1259 case VSOCK: 1260 mode |= S_IFSOCK; 1261 break; 1262 case VFIFO: 1263 mode |= S_IFIFO; 1264 break; 1265 default: 1266 return (EBADF); 1267 }; 1268 sb->st_mode = mode; 1269 sb->st_nlink = vap->va_nlink; 1270 sb->st_uid = vap->va_uid; 1271 sb->st_gid = vap->va_gid; 1272 sb->st_rdev = vap->va_rdev; 1273 if (vap->va_size > OFF_MAX) 1274 return (EOVERFLOW); 1275 sb->st_size = vap->va_size; 1276 sb->st_atim = vap->va_atime; 1277 sb->st_mtim = vap->va_mtime; 1278 sb->st_ctim = vap->va_ctime; 1279 sb->st_birthtim = vap->va_birthtime; 1280 1281 /* 1282 * According to www.opengroup.org, the meaning of st_blksize is 1283 * "a filesystem-specific preferred I/O block size for this 1284 * object. In some filesystem types, this may vary from file 1285 * to file" 1286 * Use miminum/default of PAGE_SIZE (e.g. for VCHR). 1287 */ 1288 1289 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize); 1290 1291 sb->st_flags = vap->va_flags; 1292 if (priv_check(td, PRIV_VFS_GENERATION)) 1293 sb->st_gen = 0; 1294 else 1295 sb->st_gen = vap->va_gen; 1296 1297 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 1298 return (0); 1299 } 1300 1301 /* 1302 * File table vnode ioctl routine. 1303 */ 1304 static int 1305 vn_ioctl(fp, com, data, active_cred, td) 1306 struct file *fp; 1307 u_long com; 1308 void *data; 1309 struct ucred *active_cred; 1310 struct thread *td; 1311 { 1312 struct vnode *vp = fp->f_vnode; 1313 struct vattr vattr; 1314 int error; 1315 1316 error = ENOTTY; 1317 switch (vp->v_type) { 1318 case VREG: 1319 case VDIR: 1320 if (com == FIONREAD) { 1321 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1322 error = VOP_GETATTR(vp, &vattr, active_cred); 1323 VOP_UNLOCK(vp, 0); 1324 if (!error) 1325 *(int *)data = vattr.va_size - fp->f_offset; 1326 } 1327 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 1328 error = 0; 1329 else 1330 error = VOP_IOCTL(vp, com, data, fp->f_flag, 1331 active_cred, td); 1332 break; 1333 1334 default: 1335 break; 1336 } 1337 return (error); 1338 } 1339 1340 /* 1341 * File table vnode poll routine. 1342 */ 1343 static int 1344 vn_poll(fp, events, active_cred, td) 1345 struct file *fp; 1346 int events; 1347 struct ucred *active_cred; 1348 struct thread *td; 1349 { 1350 struct vnode *vp; 1351 int error; 1352 1353 vp = fp->f_vnode; 1354 #ifdef MAC 1355 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1356 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp); 1357 VOP_UNLOCK(vp, 0); 1358 if (!error) 1359 #endif 1360 1361 error = VOP_POLL(vp, events, fp->f_cred, td); 1362 return (error); 1363 } 1364 1365 /* 1366 * Acquire the requested lock and then check for validity. LK_RETRY 1367 * permits vn_lock to return doomed vnodes. 1368 */ 1369 int 1370 _vn_lock(struct vnode *vp, int flags, char *file, int line) 1371 { 1372 int error; 1373 1374 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 1375 ("vn_lock called with no locktype.")); 1376 do { 1377 #ifdef DEBUG_VFS_LOCKS 1378 KASSERT(vp->v_holdcnt != 0, 1379 ("vn_lock %p: zero hold count", vp)); 1380 #endif 1381 error = VOP_LOCK1(vp, flags, file, line); 1382 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */ 1383 KASSERT((flags & LK_RETRY) == 0 || error == 0, 1384 ("LK_RETRY set with incompatible flags (0x%x) or an error occured (%d)", 1385 flags, error)); 1386 /* 1387 * Callers specify LK_RETRY if they wish to get dead vnodes. 1388 * If RETRY is not set, we return ENOENT instead. 1389 */ 1390 if (error == 0 && vp->v_iflag & VI_DOOMED && 1391 (flags & LK_RETRY) == 0) { 1392 VOP_UNLOCK(vp, 0); 1393 error = ENOENT; 1394 break; 1395 } 1396 } while (flags & LK_RETRY && error != 0); 1397 return (error); 1398 } 1399 1400 /* 1401 * File table vnode close routine. 1402 */ 1403 static int 1404 vn_closefile(fp, td) 1405 struct file *fp; 1406 struct thread *td; 1407 { 1408 struct vnode *vp; 1409 struct flock lf; 1410 int error; 1411 1412 vp = fp->f_vnode; 1413 fp->f_ops = &badfileops; 1414 1415 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) 1416 vref(vp); 1417 1418 error = vn_close(vp, fp->f_flag, fp->f_cred, td); 1419 1420 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) { 1421 lf.l_whence = SEEK_SET; 1422 lf.l_start = 0; 1423 lf.l_len = 0; 1424 lf.l_type = F_UNLCK; 1425 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK); 1426 vrele(vp); 1427 } 1428 return (error); 1429 } 1430 1431 /* 1432 * Preparing to start a filesystem write operation. If the operation is 1433 * permitted, then we bump the count of operations in progress and 1434 * proceed. If a suspend request is in progress, we wait until the 1435 * suspension is over, and then proceed. 1436 */ 1437 static int 1438 vn_start_write_locked(struct mount *mp, int flags) 1439 { 1440 int error; 1441 1442 mtx_assert(MNT_MTX(mp), MA_OWNED); 1443 error = 0; 1444 1445 /* 1446 * Check on status of suspension. 1447 */ 1448 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 || 1449 mp->mnt_susp_owner != curthread) { 1450 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 1451 if (flags & V_NOWAIT) { 1452 error = EWOULDBLOCK; 1453 goto unlock; 1454 } 1455 error = msleep(&mp->mnt_flag, MNT_MTX(mp), 1456 (PUSER - 1) | (flags & PCATCH), "suspfs", 0); 1457 if (error) 1458 goto unlock; 1459 } 1460 } 1461 if (flags & V_XSLEEP) 1462 goto unlock; 1463 mp->mnt_writeopcount++; 1464 unlock: 1465 if (error != 0 || (flags & V_XSLEEP) != 0) 1466 MNT_REL(mp); 1467 MNT_IUNLOCK(mp); 1468 return (error); 1469 } 1470 1471 int 1472 vn_start_write(vp, mpp, flags) 1473 struct vnode *vp; 1474 struct mount **mpp; 1475 int flags; 1476 { 1477 struct mount *mp; 1478 int error; 1479 1480 error = 0; 1481 /* 1482 * If a vnode is provided, get and return the mount point that 1483 * to which it will write. 1484 */ 1485 if (vp != NULL) { 1486 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 1487 *mpp = NULL; 1488 if (error != EOPNOTSUPP) 1489 return (error); 1490 return (0); 1491 } 1492 } 1493 if ((mp = *mpp) == NULL) 1494 return (0); 1495 1496 /* 1497 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 1498 * a vfs_ref(). 1499 * As long as a vnode is not provided we need to acquire a 1500 * refcount for the provided mountpoint too, in order to 1501 * emulate a vfs_ref(). 1502 */ 1503 MNT_ILOCK(mp); 1504 if (vp == NULL) 1505 MNT_REF(mp); 1506 1507 return (vn_start_write_locked(mp, flags)); 1508 } 1509 1510 /* 1511 * Secondary suspension. Used by operations such as vop_inactive 1512 * routines that are needed by the higher level functions. These 1513 * are allowed to proceed until all the higher level functions have 1514 * completed (indicated by mnt_writeopcount dropping to zero). At that 1515 * time, these operations are halted until the suspension is over. 1516 */ 1517 int 1518 vn_start_secondary_write(vp, mpp, flags) 1519 struct vnode *vp; 1520 struct mount **mpp; 1521 int flags; 1522 { 1523 struct mount *mp; 1524 int error; 1525 1526 retry: 1527 if (vp != NULL) { 1528 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 1529 *mpp = NULL; 1530 if (error != EOPNOTSUPP) 1531 return (error); 1532 return (0); 1533 } 1534 } 1535 /* 1536 * If we are not suspended or have not yet reached suspended 1537 * mode, then let the operation proceed. 1538 */ 1539 if ((mp = *mpp) == NULL) 1540 return (0); 1541 1542 /* 1543 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 1544 * a vfs_ref(). 1545 * As long as a vnode is not provided we need to acquire a 1546 * refcount for the provided mountpoint too, in order to 1547 * emulate a vfs_ref(). 1548 */ 1549 MNT_ILOCK(mp); 1550 if (vp == NULL) 1551 MNT_REF(mp); 1552 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) { 1553 mp->mnt_secondary_writes++; 1554 mp->mnt_secondary_accwrites++; 1555 MNT_IUNLOCK(mp); 1556 return (0); 1557 } 1558 if (flags & V_NOWAIT) { 1559 MNT_REL(mp); 1560 MNT_IUNLOCK(mp); 1561 return (EWOULDBLOCK); 1562 } 1563 /* 1564 * Wait for the suspension to finish. 1565 */ 1566 error = msleep(&mp->mnt_flag, MNT_MTX(mp), 1567 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0); 1568 vfs_rel(mp); 1569 if (error == 0) 1570 goto retry; 1571 return (error); 1572 } 1573 1574 /* 1575 * Filesystem write operation has completed. If we are suspending and this 1576 * operation is the last one, notify the suspender that the suspension is 1577 * now in effect. 1578 */ 1579 void 1580 vn_finished_write(mp) 1581 struct mount *mp; 1582 { 1583 if (mp == NULL) 1584 return; 1585 MNT_ILOCK(mp); 1586 MNT_REL(mp); 1587 mp->mnt_writeopcount--; 1588 if (mp->mnt_writeopcount < 0) 1589 panic("vn_finished_write: neg cnt"); 1590 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 1591 mp->mnt_writeopcount <= 0) 1592 wakeup(&mp->mnt_writeopcount); 1593 MNT_IUNLOCK(mp); 1594 } 1595 1596 1597 /* 1598 * Filesystem secondary write operation has completed. If we are 1599 * suspending and this operation is the last one, notify the suspender 1600 * that the suspension is now in effect. 1601 */ 1602 void 1603 vn_finished_secondary_write(mp) 1604 struct mount *mp; 1605 { 1606 if (mp == NULL) 1607 return; 1608 MNT_ILOCK(mp); 1609 MNT_REL(mp); 1610 mp->mnt_secondary_writes--; 1611 if (mp->mnt_secondary_writes < 0) 1612 panic("vn_finished_secondary_write: neg cnt"); 1613 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 1614 mp->mnt_secondary_writes <= 0) 1615 wakeup(&mp->mnt_secondary_writes); 1616 MNT_IUNLOCK(mp); 1617 } 1618 1619 1620 1621 /* 1622 * Request a filesystem to suspend write operations. 1623 */ 1624 int 1625 vfs_write_suspend(mp) 1626 struct mount *mp; 1627 { 1628 int error; 1629 1630 MNT_ILOCK(mp); 1631 if (mp->mnt_susp_owner == curthread) { 1632 MNT_IUNLOCK(mp); 1633 return (EALREADY); 1634 } 1635 while (mp->mnt_kern_flag & MNTK_SUSPEND) 1636 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0); 1637 mp->mnt_kern_flag |= MNTK_SUSPEND; 1638 mp->mnt_susp_owner = curthread; 1639 if (mp->mnt_writeopcount > 0) 1640 (void) msleep(&mp->mnt_writeopcount, 1641 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0); 1642 else 1643 MNT_IUNLOCK(mp); 1644 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0) 1645 vfs_write_resume(mp, 0); 1646 return (error); 1647 } 1648 1649 /* 1650 * Request a filesystem to resume write operations. 1651 */ 1652 void 1653 vfs_write_resume(struct mount *mp, int flags) 1654 { 1655 1656 MNT_ILOCK(mp); 1657 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 1658 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner")); 1659 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 | 1660 MNTK_SUSPENDED); 1661 mp->mnt_susp_owner = NULL; 1662 wakeup(&mp->mnt_writeopcount); 1663 wakeup(&mp->mnt_flag); 1664 curthread->td_pflags &= ~TDP_IGNSUSP; 1665 if ((flags & VR_START_WRITE) != 0) { 1666 MNT_REF(mp); 1667 mp->mnt_writeopcount++; 1668 } 1669 MNT_IUNLOCK(mp); 1670 if ((flags & VR_NO_SUSPCLR) == 0) 1671 VFS_SUSP_CLEAN(mp); 1672 } else if ((flags & VR_START_WRITE) != 0) { 1673 MNT_REF(mp); 1674 vn_start_write_locked(mp, 0); 1675 } else { 1676 MNT_IUNLOCK(mp); 1677 } 1678 } 1679 1680 /* 1681 * Implement kqueues for files by translating it to vnode operation. 1682 */ 1683 static int 1684 vn_kqfilter(struct file *fp, struct knote *kn) 1685 { 1686 1687 return (VOP_KQFILTER(fp->f_vnode, kn)); 1688 } 1689 1690 /* 1691 * Simplified in-kernel wrapper calls for extended attribute access. 1692 * Both calls pass in a NULL credential, authorizing as "kernel" access. 1693 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 1694 */ 1695 int 1696 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 1697 const char *attrname, int *buflen, char *buf, struct thread *td) 1698 { 1699 struct uio auio; 1700 struct iovec iov; 1701 int error; 1702 1703 iov.iov_len = *buflen; 1704 iov.iov_base = buf; 1705 1706 auio.uio_iov = &iov; 1707 auio.uio_iovcnt = 1; 1708 auio.uio_rw = UIO_READ; 1709 auio.uio_segflg = UIO_SYSSPACE; 1710 auio.uio_td = td; 1711 auio.uio_offset = 0; 1712 auio.uio_resid = *buflen; 1713 1714 if ((ioflg & IO_NODELOCKED) == 0) 1715 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1716 1717 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 1718 1719 /* authorize attribute retrieval as kernel */ 1720 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL, 1721 td); 1722 1723 if ((ioflg & IO_NODELOCKED) == 0) 1724 VOP_UNLOCK(vp, 0); 1725 1726 if (error == 0) { 1727 *buflen = *buflen - auio.uio_resid; 1728 } 1729 1730 return (error); 1731 } 1732 1733 /* 1734 * XXX failure mode if partially written? 1735 */ 1736 int 1737 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 1738 const char *attrname, int buflen, char *buf, struct thread *td) 1739 { 1740 struct uio auio; 1741 struct iovec iov; 1742 struct mount *mp; 1743 int error; 1744 1745 iov.iov_len = buflen; 1746 iov.iov_base = buf; 1747 1748 auio.uio_iov = &iov; 1749 auio.uio_iovcnt = 1; 1750 auio.uio_rw = UIO_WRITE; 1751 auio.uio_segflg = UIO_SYSSPACE; 1752 auio.uio_td = td; 1753 auio.uio_offset = 0; 1754 auio.uio_resid = buflen; 1755 1756 if ((ioflg & IO_NODELOCKED) == 0) { 1757 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 1758 return (error); 1759 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1760 } 1761 1762 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 1763 1764 /* authorize attribute setting as kernel */ 1765 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td); 1766 1767 if ((ioflg & IO_NODELOCKED) == 0) { 1768 vn_finished_write(mp); 1769 VOP_UNLOCK(vp, 0); 1770 } 1771 1772 return (error); 1773 } 1774 1775 int 1776 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 1777 const char *attrname, struct thread *td) 1778 { 1779 struct mount *mp; 1780 int error; 1781 1782 if ((ioflg & IO_NODELOCKED) == 0) { 1783 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 1784 return (error); 1785 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1786 } 1787 1788 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 1789 1790 /* authorize attribute removal as kernel */ 1791 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td); 1792 if (error == EOPNOTSUPP) 1793 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, 1794 NULL, td); 1795 1796 if ((ioflg & IO_NODELOCKED) == 0) { 1797 vn_finished_write(mp); 1798 VOP_UNLOCK(vp, 0); 1799 } 1800 1801 return (error); 1802 } 1803 1804 int 1805 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp) 1806 { 1807 struct mount *mp; 1808 int ltype, error; 1809 1810 mp = vp->v_mount; 1811 ltype = VOP_ISLOCKED(vp); 1812 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED, 1813 ("vn_vget_ino: vp not locked")); 1814 error = vfs_busy(mp, MBF_NOWAIT); 1815 if (error != 0) { 1816 vfs_ref(mp); 1817 VOP_UNLOCK(vp, 0); 1818 error = vfs_busy(mp, 0); 1819 vn_lock(vp, ltype | LK_RETRY); 1820 vfs_rel(mp); 1821 if (error != 0) 1822 return (ENOENT); 1823 if (vp->v_iflag & VI_DOOMED) { 1824 vfs_unbusy(mp); 1825 return (ENOENT); 1826 } 1827 } 1828 VOP_UNLOCK(vp, 0); 1829 error = VFS_VGET(mp, ino, lkflags, rvp); 1830 vfs_unbusy(mp); 1831 vn_lock(vp, ltype | LK_RETRY); 1832 if (vp->v_iflag & VI_DOOMED) { 1833 if (error == 0) 1834 vput(*rvp); 1835 error = ENOENT; 1836 } 1837 return (error); 1838 } 1839 1840 int 1841 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, 1842 const struct thread *td) 1843 { 1844 1845 if (vp->v_type != VREG || td == NULL) 1846 return (0); 1847 PROC_LOCK(td->td_proc); 1848 if ((uoff_t)uio->uio_offset + uio->uio_resid > 1849 lim_cur(td->td_proc, RLIMIT_FSIZE)) { 1850 kern_psignal(td->td_proc, SIGXFSZ); 1851 PROC_UNLOCK(td->td_proc); 1852 return (EFBIG); 1853 } 1854 PROC_UNLOCK(td->td_proc); 1855 return (0); 1856 } 1857 1858 int 1859 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 1860 struct thread *td) 1861 { 1862 struct vnode *vp; 1863 int error; 1864 1865 vp = fp->f_vnode; 1866 #ifdef AUDIT 1867 vn_lock(vp, LK_SHARED | LK_RETRY); 1868 AUDIT_ARG_VNODE1(vp); 1869 VOP_UNLOCK(vp, 0); 1870 #endif 1871 error = setfmode(td, active_cred, vp, mode); 1872 return (error); 1873 } 1874 1875 int 1876 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 1877 struct thread *td) 1878 { 1879 struct vnode *vp; 1880 int error; 1881 1882 vp = fp->f_vnode; 1883 #ifdef AUDIT 1884 vn_lock(vp, LK_SHARED | LK_RETRY); 1885 AUDIT_ARG_VNODE1(vp); 1886 VOP_UNLOCK(vp, 0); 1887 #endif 1888 error = setfown(td, active_cred, vp, uid, gid); 1889 return (error); 1890 } 1891 1892 void 1893 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end) 1894 { 1895 vm_object_t object; 1896 1897 if ((object = vp->v_object) == NULL) 1898 return; 1899 VM_OBJECT_LOCK(object); 1900 vm_object_page_remove(object, start, end, 0); 1901 VM_OBJECT_UNLOCK(object); 1902 } 1903 1904 int 1905 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred) 1906 { 1907 struct vattr va; 1908 daddr_t bn, bnp; 1909 uint64_t bsize; 1910 off_t noff; 1911 int error; 1912 1913 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA, 1914 ("Wrong command %lu", cmd)); 1915 1916 if (vn_lock(vp, LK_SHARED) != 0) 1917 return (EBADF); 1918 if (vp->v_type != VREG) { 1919 error = ENOTTY; 1920 goto unlock; 1921 } 1922 error = VOP_GETATTR(vp, &va, cred); 1923 if (error != 0) 1924 goto unlock; 1925 noff = *off; 1926 if (noff >= va.va_size) { 1927 error = ENXIO; 1928 goto unlock; 1929 } 1930 bsize = vp->v_mount->mnt_stat.f_iosize; 1931 for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize) { 1932 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL); 1933 if (error == EOPNOTSUPP) { 1934 error = ENOTTY; 1935 goto unlock; 1936 } 1937 if ((bnp == -1 && cmd == FIOSEEKHOLE) || 1938 (bnp != -1 && cmd == FIOSEEKDATA)) { 1939 noff = bn * bsize; 1940 if (noff < *off) 1941 noff = *off; 1942 goto unlock; 1943 } 1944 } 1945 if (noff > va.va_size) 1946 noff = va.va_size; 1947 /* noff == va.va_size. There is an implicit hole at the end of file. */ 1948 if (cmd == FIOSEEKDATA) 1949 error = ENXIO; 1950 unlock: 1951 VOP_UNLOCK(vp, 0); 1952 if (error == 0) 1953 *off = noff; 1954 return (error); 1955 } 1956