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