1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org> 13 * Copyright (c) 2013, 2014 The FreeBSD Foundation 14 * 15 * Portions of this software were developed by Konstantin Belousov 16 * under sponsorship from the FreeBSD Foundation. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 */ 42 43 #include "opt_hwpmc_hooks.h" 44 #include "opt_hwt_hooks.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/buf.h> 49 #include <sys/disk.h> 50 #include <sys/dirent.h> 51 #include <sys/fail.h> 52 #include <sys/fcntl.h> 53 #include <sys/file.h> 54 #include <sys/filio.h> 55 #include <sys/inotify.h> 56 #include <sys/ktr.h> 57 #include <sys/ktrace.h> 58 #include <sys/limits.h> 59 #include <sys/lock.h> 60 #include <sys/mman.h> 61 #include <sys/mount.h> 62 #include <sys/mutex.h> 63 #include <sys/namei.h> 64 #include <sys/priv.h> 65 #include <sys/prng.h> 66 #include <sys/proc.h> 67 #include <sys/rwlock.h> 68 #include <sys/sleepqueue.h> 69 #include <sys/stat.h> 70 #include <sys/sysctl.h> 71 #include <sys/unistd.h> 72 #include <sys/user.h> 73 #include <sys/vnode.h> 74 75 #include <security/audit/audit.h> 76 #include <security/mac/mac_framework.h> 77 78 #include <vm/vm.h> 79 #include <vm/vm_extern.h> 80 #include <vm/pmap.h> 81 #include <vm/vm_map.h> 82 #include <vm/vm_object.h> 83 #include <vm/vm_page.h> 84 #include <vm/vm_pager.h> 85 #include <vm/vnode_pager.h> 86 87 #ifdef HWPMC_HOOKS 88 #include <sys/pmckern.h> 89 #endif 90 91 #ifdef HWT_HOOKS 92 #include <dev/hwt/hwt_hook.h> 93 #endif 94 95 static fo_rdwr_t vn_read; 96 static fo_rdwr_t vn_write; 97 static fo_rdwr_t vn_io_fault; 98 static fo_truncate_t vn_truncate; 99 static fo_ioctl_t vn_ioctl; 100 static fo_poll_t vn_poll; 101 static fo_kqfilter_t vn_kqfilter; 102 static fo_close_t vn_closefile; 103 static fo_mmap_t vn_mmap; 104 static fo_fallocate_t vn_fallocate; 105 static fo_fspacectl_t vn_fspacectl; 106 107 const struct fileops vnops = { 108 .fo_read = vn_io_fault, 109 .fo_write = vn_io_fault, 110 .fo_truncate = vn_truncate, 111 .fo_ioctl = vn_ioctl, 112 .fo_poll = vn_poll, 113 .fo_kqfilter = vn_kqfilter, 114 .fo_stat = vn_statfile, 115 .fo_close = vn_closefile, 116 .fo_chmod = vn_chmod, 117 .fo_chown = vn_chown, 118 .fo_sendfile = vn_sendfile, 119 .fo_seek = vn_seek, 120 .fo_fill_kinfo = vn_fill_kinfo, 121 .fo_mmap = vn_mmap, 122 .fo_fallocate = vn_fallocate, 123 .fo_fspacectl = vn_fspacectl, 124 .fo_cmp = vn_cmp, 125 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 126 }; 127 128 const u_int io_hold_cnt = 16; 129 static int vn_io_fault_enable = 1; 130 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RWTUN, 131 &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance"); 132 static int vn_io_fault_prefault = 0; 133 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_prefault, CTLFLAG_RWTUN, 134 &vn_io_fault_prefault, 0, "Enable vn_io_fault prefaulting"); 135 static int vn_io_pgcache_read_enable = 1; 136 SYSCTL_INT(_debug, OID_AUTO, vn_io_pgcache_read_enable, CTLFLAG_RWTUN, 137 &vn_io_pgcache_read_enable, 0, 138 "Enable copying from page cache for reads, avoiding fs"); 139 static u_long vn_io_faults_cnt; 140 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD, 141 &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers"); 142 143 static int vfs_allow_read_dir = 0; 144 SYSCTL_INT(_security_bsd, OID_AUTO, allow_read_dir, CTLFLAG_RW, 145 &vfs_allow_read_dir, 0, 146 "Enable read(2) of directory by root for filesystems that support it"); 147 148 /* 149 * Returns true if vn_io_fault mode of handling the i/o request should 150 * be used. 151 */ 152 static bool 153 do_vn_io_fault(struct vnode *vp, struct uio *uio) 154 { 155 struct mount *mp; 156 157 return (uio->uio_segflg == UIO_USERSPACE && vp->v_type == VREG && 158 (mp = vp->v_mount) != NULL && 159 (mp->mnt_kern_flag & MNTK_NO_IOPF) != 0 && vn_io_fault_enable); 160 } 161 162 /* 163 * Structure used to pass arguments to vn_io_fault1(), to do either 164 * file- or vnode-based I/O calls. 165 */ 166 struct vn_io_fault_args { 167 enum { 168 VN_IO_FAULT_FOP, 169 VN_IO_FAULT_VOP 170 } kind; 171 struct ucred *cred; 172 int flags; 173 union { 174 struct fop_args_tag { 175 struct file *fp; 176 fo_rdwr_t *doio; 177 } fop_args; 178 struct vop_args_tag { 179 struct vnode *vp; 180 } vop_args; 181 } args; 182 }; 183 184 static int vn_io_fault1(struct vnode *vp, struct uio *uio, 185 struct vn_io_fault_args *args, struct thread *td); 186 187 int 188 vn_open(struct nameidata *ndp, int *flagp, int cmode, struct file *fp) 189 { 190 struct thread *td = curthread; 191 192 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp)); 193 } 194 195 static uint64_t 196 open2nameif(int fmode, u_int vn_open_flags, uint64_t cn_flags) 197 { 198 uint64_t res; 199 200 res = ISOPEN | LOCKLEAF | cn_flags; 201 if ((fmode & O_RESOLVE_BENEATH) != 0) 202 res |= RBENEATH; 203 if ((fmode & O_EMPTY_PATH) != 0) 204 res |= EMPTYPATH; 205 if ((fmode & FREAD) != 0) 206 res |= OPENREAD; 207 if ((fmode & FWRITE) != 0) 208 res |= OPENWRITE; 209 if ((fmode & O_NAMEDATTR) != 0) 210 res |= OPENNAMED | CREATENAMED; 211 if ((fmode & O_NOFOLLOW) != 0) 212 res &= ~FOLLOW; 213 if ((vn_open_flags & VN_OPEN_NOAUDIT) == 0) 214 res |= AUDITVNODE1; 215 else 216 res &= ~AUDITVNODE1; 217 if ((vn_open_flags & VN_OPEN_NOCAPCHECK) != 0) 218 res |= NOCAPCHECK; 219 if ((vn_open_flags & VN_OPEN_WANTIOCTLCAPS) != 0) 220 res |= WANTIOCTLCAPS; 221 222 return (res); 223 } 224 225 /* 226 * For the O_NAMEDATTR case, check for a valid use of it. 227 */ 228 static int 229 vfs_check_namedattr(struct vnode *vp) 230 { 231 int error; 232 short irflag; 233 234 error = 0; 235 irflag = vn_irflag_read(vp); 236 if ((vp->v_mount->mnt_flag & MNT_NAMEDATTR) == 0 || 237 ((irflag & VIRF_NAMEDATTR) != 0 && vp->v_type != VREG)) 238 error = EINVAL; 239 else if ((irflag & (VIRF_NAMEDDIR | VIRF_NAMEDATTR)) == 0) 240 error = ENOATTR; 241 return (error); 242 } 243 244 /* 245 * Common code for vnode open operations via a name lookup. 246 * Lookup the vnode and invoke VOP_CREATE if needed. 247 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 248 * 249 * Note that this does NOT free nameidata for the successful case, 250 * due to the NDINIT being done elsewhere. 251 */ 252 int 253 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags, 254 struct ucred *cred, struct file *fp) 255 { 256 struct vnode *vp; 257 struct mount *mp; 258 struct vattr vat; 259 struct vattr *vap = &vat; 260 int fmode, error; 261 bool first_open; 262 263 restart: 264 first_open = false; 265 fmode = *flagp; 266 if ((fmode & (O_CREAT | O_EXCL | O_DIRECTORY)) == (O_CREAT | 267 O_EXCL | O_DIRECTORY) || 268 (fmode & (O_CREAT | O_EMPTY_PATH)) == (O_CREAT | O_EMPTY_PATH)) 269 return (EINVAL); 270 else if ((fmode & (O_CREAT | O_DIRECTORY)) == O_CREAT) { 271 ndp->ni_cnd.cn_nameiop = CREATE; 272 ndp->ni_cnd.cn_flags = open2nameif(fmode, vn_open_flags, 273 ndp->ni_cnd.cn_flags); 274 275 /* 276 * Set NOCACHE to avoid flushing the cache when 277 * rolling in many files at once. 278 * 279 * Set NC_KEEPPOSENTRY to keep positive entries if they already 280 * exist despite NOCACHE. 281 */ 282 ndp->ni_cnd.cn_flags |= LOCKPARENT | NOCACHE | NC_KEEPPOSENTRY; 283 if ((fmode & O_EXCL) != 0) 284 ndp->ni_cnd.cn_flags &= ~FOLLOW; 285 if ((vn_open_flags & VN_OPEN_INVFS) == 0) 286 bwillwrite(); 287 if ((error = namei(ndp)) != 0) 288 return (error); 289 if (ndp->ni_vp == NULL) { 290 if ((fmode & O_NAMEDATTR) != 0 && 291 (ndp->ni_dvp->v_mount->mnt_flag & MNT_NAMEDATTR) == 292 0) { 293 error = EINVAL; 294 vp = ndp->ni_dvp; 295 ndp->ni_dvp = NULL; 296 goto bad; 297 } 298 VATTR_NULL(vap); 299 vap->va_type = VREG; 300 vap->va_mode = cmode; 301 if (fmode & O_EXCL) 302 vap->va_vaflags |= VA_EXCLUSIVE; 303 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) { 304 NDFREE_PNBUF(ndp); 305 vput(ndp->ni_dvp); 306 if ((error = vn_start_write(NULL, &mp, 307 V_XSLEEP | V_PCATCH)) != 0) 308 return (error); 309 NDREINIT(ndp); 310 goto restart; 311 } 312 if ((vn_open_flags & VN_OPEN_NAMECACHE) != 0 || 313 (vn_irflag_read(ndp->ni_dvp) & VIRF_INOTIFY) != 0) 314 ndp->ni_cnd.cn_flags |= MAKEENTRY; 315 #ifdef MAC 316 error = mac_vnode_check_create(cred, ndp->ni_dvp, 317 &ndp->ni_cnd, vap); 318 if (error == 0) 319 #endif 320 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 321 &ndp->ni_cnd, vap); 322 vp = ndp->ni_vp; 323 if (error == 0 && (fmode & O_EXCL) != 0 && 324 (fmode & (O_EXLOCK | O_SHLOCK)) != 0) { 325 VI_LOCK(vp); 326 vp->v_iflag |= VI_FOPENING; 327 VI_UNLOCK(vp); 328 first_open = true; 329 } 330 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &vp : NULL, 331 false); 332 vn_finished_write(mp); 333 if (error) { 334 NDFREE_PNBUF(ndp); 335 if (error == ERELOOKUP) { 336 NDREINIT(ndp); 337 goto restart; 338 } 339 return (error); 340 } 341 fmode &= ~O_TRUNC; 342 } else { 343 if (ndp->ni_dvp == ndp->ni_vp) 344 vrele(ndp->ni_dvp); 345 else 346 vput(ndp->ni_dvp); 347 ndp->ni_dvp = NULL; 348 vp = ndp->ni_vp; 349 if (fmode & O_EXCL) { 350 error = EEXIST; 351 goto bad; 352 } 353 if ((fmode & O_NAMEDATTR) != 0) { 354 error = vfs_check_namedattr(vp); 355 if (error != 0) 356 goto bad; 357 } else if (vp->v_type == VDIR) { 358 error = EISDIR; 359 goto bad; 360 } 361 fmode &= ~O_CREAT; 362 } 363 } else { 364 ndp->ni_cnd.cn_nameiop = LOOKUP; 365 ndp->ni_cnd.cn_flags = open2nameif(fmode, vn_open_flags, 366 ndp->ni_cnd.cn_flags); 367 if ((fmode & FWRITE) == 0) 368 ndp->ni_cnd.cn_flags |= LOCKSHARED; 369 if ((error = namei(ndp)) != 0) 370 return (error); 371 vp = ndp->ni_vp; 372 if ((fmode & O_NAMEDATTR) != 0) { 373 error = vfs_check_namedattr(vp); 374 if (error != 0) 375 goto bad; 376 } 377 } 378 error = vn_open_vnode(vp, fmode, cred, curthread, fp); 379 if (first_open) { 380 VI_LOCK(vp); 381 vp->v_iflag &= ~VI_FOPENING; 382 wakeup(vp); 383 VI_UNLOCK(vp); 384 } 385 if (error) 386 goto bad; 387 *flagp = fmode; 388 return (0); 389 bad: 390 NDFREE_PNBUF(ndp); 391 vput(vp); 392 *flagp = fmode; 393 ndp->ni_vp = NULL; 394 return (error); 395 } 396 397 static int 398 vn_open_vnode_advlock(struct vnode *vp, int fmode, struct file *fp) 399 { 400 struct flock lf; 401 int error, lock_flags, type; 402 403 ASSERT_VOP_LOCKED(vp, "vn_open_vnode_advlock"); 404 if ((fmode & (O_EXLOCK | O_SHLOCK)) == 0) 405 return (0); 406 KASSERT(fp != NULL, ("open with flock requires fp")); 407 if (fp->f_type != DTYPE_NONE && fp->f_type != DTYPE_VNODE) 408 return (EOPNOTSUPP); 409 410 lock_flags = VOP_ISLOCKED(vp); 411 VOP_UNLOCK(vp); 412 413 lf.l_whence = SEEK_SET; 414 lf.l_start = 0; 415 lf.l_len = 0; 416 lf.l_type = (fmode & O_EXLOCK) != 0 ? F_WRLCK : F_RDLCK; 417 type = F_FLOCK; 418 if ((fmode & FNONBLOCK) == 0) 419 type |= F_WAIT; 420 if ((fmode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 421 type |= F_FIRSTOPEN; 422 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type); 423 if (error == 0) 424 fp->f_flag |= FHASLOCK; 425 426 vn_lock(vp, lock_flags | LK_RETRY); 427 return (error); 428 } 429 430 /* 431 * Common code for vnode open operations once a vnode is located. 432 * Check permissions, and call the VOP_OPEN routine. 433 */ 434 int 435 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred, 436 struct thread *td, struct file *fp) 437 { 438 accmode_t accmode; 439 int error; 440 441 KASSERT((fmode & O_PATH) == 0 || (fmode & O_ACCMODE) == 0, 442 ("%s: O_PATH and O_ACCMODE are mutually exclusive", __func__)); 443 444 if (vp->v_type == VLNK) { 445 if ((fmode & O_PATH) == 0 || (fmode & FEXEC) != 0) 446 return (EMLINK); 447 } 448 if (vp->v_type != VDIR && fmode & O_DIRECTORY) 449 return (ENOTDIR); 450 451 accmode = 0; 452 if ((fmode & O_PATH) == 0) { 453 if (vp->v_type == VSOCK) 454 return (EOPNOTSUPP); 455 if ((fmode & (FWRITE | O_TRUNC)) != 0) { 456 if (vp->v_type == VDIR) 457 return (EISDIR); 458 accmode |= VWRITE; 459 } 460 if ((fmode & FREAD) != 0) 461 accmode |= VREAD; 462 if ((fmode & O_APPEND) && (fmode & FWRITE)) 463 accmode |= VAPPEND; 464 #ifdef MAC 465 if ((fmode & O_CREAT) != 0) 466 accmode |= VCREAT; 467 #endif 468 } 469 if ((fmode & FEXEC) != 0) 470 accmode |= VEXEC; 471 #ifdef MAC 472 if ((fmode & O_VERIFY) != 0) 473 accmode |= VVERIFY; 474 error = mac_vnode_check_open(cred, vp, accmode); 475 if (error != 0) 476 return (error); 477 478 accmode &= ~(VCREAT | VVERIFY); 479 #endif 480 if ((fmode & O_CREAT) == 0 && accmode != 0) { 481 error = VOP_ACCESS(vp, accmode, cred, td); 482 if (error != 0) 483 return (error); 484 } 485 if ((fmode & O_PATH) != 0) { 486 if (vp->v_type != VFIFO && vp->v_type != VSOCK && 487 VOP_ACCESS(vp, VREAD, cred, td) == 0) 488 fp->f_flag |= FKQALLOWED; 489 INOTIFY(vp, IN_OPEN); 490 return (0); 491 } 492 493 if (vp->v_type == VFIFO && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 494 vn_lock(vp, LK_UPGRADE | LK_RETRY); 495 error = VOP_OPEN(vp, fmode, cred, td, fp); 496 if (error != 0) 497 return (error); 498 499 error = vn_open_vnode_advlock(vp, fmode, fp); 500 if (error == 0 && (fmode & FWRITE) != 0) { 501 error = VOP_ADD_WRITECOUNT(vp, 1); 502 if (error == 0) { 503 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 504 __func__, vp, vp->v_writecount); 505 } 506 } 507 508 /* 509 * Error from advlock or VOP_ADD_WRITECOUNT() still requires 510 * calling VOP_CLOSE() to pair with earlier VOP_OPEN(). 511 */ 512 if (error != 0) { 513 if (fp != NULL) { 514 /* 515 * Arrange the call by having fdrop() to use 516 * vn_closefile(). This is to satisfy 517 * filesystems like devfs or tmpfs, which 518 * override fo_close(). 519 */ 520 fp->f_flag |= FOPENFAILED; 521 fp->f_vnode = vp; 522 if (fp->f_ops == &badfileops) { 523 fp->f_type = DTYPE_VNODE; 524 fp->f_ops = &vnops; 525 } 526 vref(vp); 527 } else { 528 /* 529 * If there is no fp, due to kernel-mode open, 530 * we can call VOP_CLOSE() now. 531 */ 532 if ((vp->v_type == VFIFO || 533 !MNT_EXTENDED_SHARED(vp->v_mount)) && 534 VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 535 vn_lock(vp, LK_UPGRADE | LK_RETRY); 536 (void)VOP_CLOSE(vp, fmode & (FREAD | FWRITE | FEXEC), 537 cred, td); 538 } 539 } 540 541 ASSERT_VOP_LOCKED(vp, "vn_open_vnode"); 542 return (error); 543 544 } 545 546 /* 547 * Check for write permissions on the specified vnode. 548 * Prototype text segments cannot be written. 549 * It is racy. 550 */ 551 int 552 vn_writechk(struct vnode *vp) 553 { 554 555 ASSERT_VOP_LOCKED(vp, "vn_writechk"); 556 /* 557 * If there's shared text associated with 558 * the vnode, try to free it up once. If 559 * we fail, we can't allow writing. 560 */ 561 if (VOP_IS_TEXT(vp)) 562 return (ETXTBSY); 563 564 return (0); 565 } 566 567 /* 568 * Vnode close call 569 */ 570 static int 571 vn_close1(struct vnode *vp, int flags, struct ucred *file_cred, 572 struct thread *td, bool keep_ref) 573 { 574 struct mount *mp; 575 int error, lock_flags; 576 577 lock_flags = vp->v_type != VFIFO && MNT_EXTENDED_SHARED(vp->v_mount) ? 578 LK_SHARED : LK_EXCLUSIVE; 579 580 vn_start_write(vp, &mp, V_WAIT); 581 vn_lock(vp, lock_flags | LK_RETRY); 582 AUDIT_ARG_VNODE1(vp); 583 if ((flags & (FWRITE | FOPENFAILED)) == FWRITE) { 584 VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 585 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 586 __func__, vp, vp->v_writecount); 587 } 588 error = VOP_CLOSE(vp, flags, file_cred, td); 589 if (keep_ref) 590 VOP_UNLOCK(vp); 591 else 592 vput(vp); 593 vn_finished_write(mp); 594 return (error); 595 } 596 597 int 598 vn_close(struct vnode *vp, int flags, struct ucred *file_cred, 599 struct thread *td) 600 { 601 602 return (vn_close1(vp, flags, file_cred, td, false)); 603 } 604 605 /* 606 * Heuristic to detect sequential operation. 607 */ 608 static int 609 sequential_heuristic(struct uio *uio, struct file *fp) 610 { 611 enum uio_rw rw; 612 613 ASSERT_VOP_LOCKED(fp->f_vnode, __func__); 614 615 rw = uio->uio_rw; 616 if (fp->f_flag & FRDAHEAD) 617 return (fp->f_seqcount[rw] << IO_SEQSHIFT); 618 619 /* 620 * Offset 0 is handled specially. open() sets f_seqcount to 1 so 621 * that the first I/O is normally considered to be slightly 622 * sequential. Seeking to offset 0 doesn't change sequentiality 623 * unless previous seeks have reduced f_seqcount to 0, in which 624 * case offset 0 is not special. 625 */ 626 if ((uio->uio_offset == 0 && fp->f_seqcount[rw] > 0) || 627 uio->uio_offset == fp->f_nextoff[rw]) { 628 /* 629 * f_seqcount is in units of fixed-size blocks so that it 630 * depends mainly on the amount of sequential I/O and not 631 * much on the number of sequential I/O's. The fixed size 632 * of 16384 is hard-coded here since it is (not quite) just 633 * a magic size that works well here. This size is more 634 * closely related to the best I/O size for real disks than 635 * to any block size used by software. 636 */ 637 if (uio->uio_resid >= IO_SEQMAX * 16384) 638 fp->f_seqcount[rw] = IO_SEQMAX; 639 else { 640 fp->f_seqcount[rw] += howmany(uio->uio_resid, 16384); 641 if (fp->f_seqcount[rw] > IO_SEQMAX) 642 fp->f_seqcount[rw] = IO_SEQMAX; 643 } 644 return (fp->f_seqcount[rw] << IO_SEQSHIFT); 645 } 646 647 /* Not sequential. Quickly draw-down sequentiality. */ 648 if (fp->f_seqcount[rw] > 1) 649 fp->f_seqcount[rw] = 1; 650 else 651 fp->f_seqcount[rw] = 0; 652 return (0); 653 } 654 655 /* 656 * Package up an I/O request on a vnode into a uio and do it. 657 */ 658 int 659 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 660 enum uio_seg segflg, int ioflg, struct ucred *active_cred, 661 struct ucred *file_cred, ssize_t *aresid, struct thread *td) 662 { 663 struct uio auio; 664 struct iovec aiov; 665 struct mount *mp; 666 struct ucred *cred; 667 void *rl_cookie; 668 struct vn_io_fault_args args; 669 int error, lock_flags; 670 671 if (offset < 0 && vp->v_type != VCHR) 672 return (EINVAL); 673 auio.uio_iov = &aiov; 674 auio.uio_iovcnt = 1; 675 aiov.iov_base = base; 676 aiov.iov_len = len; 677 auio.uio_resid = len; 678 auio.uio_offset = offset; 679 auio.uio_segflg = segflg; 680 auio.uio_rw = rw; 681 auio.uio_td = td; 682 error = 0; 683 684 if ((ioflg & IO_NODELOCKED) == 0) { 685 if ((ioflg & IO_RANGELOCKED) == 0) { 686 if (rw == UIO_READ) { 687 rl_cookie = vn_rangelock_rlock(vp, offset, 688 offset + len); 689 } else if ((ioflg & IO_APPEND) != 0) { 690 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 691 } else { 692 rl_cookie = vn_rangelock_wlock(vp, offset, 693 offset + len); 694 } 695 } else 696 rl_cookie = NULL; 697 mp = NULL; 698 if (rw == UIO_WRITE) { 699 if (vp->v_type != VCHR && 700 (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) 701 != 0) 702 goto out; 703 lock_flags = vn_lktype_write(mp, vp); 704 } else 705 lock_flags = LK_SHARED; 706 vn_lock(vp, lock_flags | LK_RETRY); 707 } else 708 rl_cookie = NULL; 709 710 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 711 #ifdef MAC 712 if ((ioflg & IO_NOMACCHECK) == 0) { 713 if (rw == UIO_READ) 714 error = mac_vnode_check_read(active_cred, file_cred, 715 vp); 716 else 717 error = mac_vnode_check_write(active_cred, file_cred, 718 vp); 719 } 720 #endif 721 if (error == 0) { 722 if (file_cred != NULL) 723 cred = file_cred; 724 else 725 cred = active_cred; 726 if (do_vn_io_fault(vp, &auio)) { 727 args.kind = VN_IO_FAULT_VOP; 728 args.cred = cred; 729 args.flags = ioflg; 730 args.args.vop_args.vp = vp; 731 error = vn_io_fault1(vp, &auio, &args, td); 732 } else if (rw == UIO_READ) { 733 error = VOP_READ(vp, &auio, ioflg, cred); 734 } else /* if (rw == UIO_WRITE) */ { 735 error = VOP_WRITE(vp, &auio, ioflg, cred); 736 } 737 } 738 if (aresid) 739 *aresid = auio.uio_resid; 740 else 741 if (auio.uio_resid && error == 0) 742 error = EIO; 743 if ((ioflg & IO_NODELOCKED) == 0) { 744 VOP_UNLOCK(vp); 745 if (mp != NULL) 746 vn_finished_write(mp); 747 } 748 out: 749 if (rl_cookie != NULL) 750 vn_rangelock_unlock(vp, rl_cookie); 751 return (error); 752 } 753 754 /* 755 * Package up an I/O request on a vnode into a uio and do it. The I/O 756 * request is split up into smaller chunks and we try to avoid saturating 757 * the buffer cache while potentially holding a vnode locked, so we 758 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield() 759 * to give other processes a chance to lock the vnode (either other processes 760 * core'ing the same binary, or unrelated processes scanning the directory). 761 */ 762 int 763 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, void *base, size_t len, 764 off_t offset, enum uio_seg segflg, int ioflg, struct ucred *active_cred, 765 struct ucred *file_cred, size_t *aresid, struct thread *td) 766 { 767 int error = 0; 768 ssize_t iaresid; 769 770 do { 771 int chunk; 772 773 /* 774 * Force `offset' to a multiple of MAXBSIZE except possibly 775 * for the first chunk, so that filesystems only need to 776 * write full blocks except possibly for the first and last 777 * chunks. 778 */ 779 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE; 780 781 if (chunk > len) 782 chunk = len; 783 if (rw != UIO_READ && vp->v_type == VREG) 784 bwillwrite(); 785 iaresid = 0; 786 error = vn_rdwr(rw, vp, base, chunk, offset, segflg, 787 ioflg, active_cred, file_cred, &iaresid, td); 788 len -= chunk; /* aresid calc already includes length */ 789 if (error) 790 break; 791 offset += chunk; 792 base = (char *)base + chunk; 793 kern_yield(PRI_USER); 794 } while (len); 795 if (aresid) 796 *aresid = len + iaresid; 797 return (error); 798 } 799 800 #if OFF_MAX <= LONG_MAX 801 static void 802 file_v_lock(struct file *fp, short lock_bit, short lock_wait_bit) 803 { 804 short *flagsp; 805 short state; 806 807 flagsp = &fp->f_vflags; 808 state = atomic_load_16(flagsp); 809 for (;;) { 810 if ((state & lock_bit) != 0) 811 break; 812 if (atomic_fcmpset_acq_16(flagsp, &state, state | lock_bit)) 813 return; 814 } 815 816 sleepq_lock(flagsp); 817 state = atomic_load_16(flagsp); 818 for (;;) { 819 if ((state & lock_bit) == 0) { 820 if (!atomic_fcmpset_acq_16(flagsp, &state, 821 state | lock_bit)) 822 continue; 823 break; 824 } 825 if ((state & lock_wait_bit) == 0) { 826 if (!atomic_fcmpset_acq_16(flagsp, &state, 827 state | lock_wait_bit)) 828 continue; 829 } 830 DROP_GIANT(); 831 sleepq_add(flagsp, NULL, "vofflock", 0, 0); 832 sleepq_wait(flagsp, PRI_MAX_KERN); 833 PICKUP_GIANT(); 834 sleepq_lock(flagsp); 835 state = atomic_load_16(flagsp); 836 } 837 sleepq_release(flagsp); 838 } 839 840 static void 841 file_v_unlock(struct file *fp, short lock_bit, short lock_wait_bit) 842 { 843 short *flagsp; 844 short state; 845 846 flagsp = &fp->f_vflags; 847 state = atomic_load_16(flagsp); 848 for (;;) { 849 if ((state & lock_wait_bit) != 0) 850 break; 851 if (atomic_fcmpset_rel_16(flagsp, &state, state & ~lock_bit)) 852 return; 853 } 854 855 sleepq_lock(flagsp); 856 MPASS((*flagsp & lock_bit) != 0); 857 MPASS((*flagsp & lock_wait_bit) != 0); 858 atomic_clear_16(flagsp, lock_bit | lock_wait_bit); 859 sleepq_broadcast(flagsp, SLEEPQ_SLEEP, 0, 0); 860 sleepq_release(flagsp); 861 } 862 863 off_t 864 foffset_lock(struct file *fp, int flags) 865 { 866 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 867 868 if ((flags & FOF_NOLOCK) == 0) { 869 file_v_lock(fp, FILE_V_FOFFSET_LOCKED, 870 FILE_V_FOFFSET_LOCK_WAITING); 871 } 872 873 return (atomic_load_long(&fp->f_offset)); 874 } 875 876 void 877 foffset_unlock(struct file *fp, off_t val, int flags) 878 { 879 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 880 881 if ((flags & FOF_NOUPDATE) == 0) 882 atomic_store_long(&fp->f_offset, val); 883 if ((flags & FOF_NEXTOFF_R) != 0) 884 fp->f_nextoff[UIO_READ] = val; 885 if ((flags & FOF_NEXTOFF_W) != 0) 886 fp->f_nextoff[UIO_WRITE] = val; 887 888 if ((flags & FOF_NOLOCK) == 0) { 889 file_v_unlock(fp, FILE_V_FOFFSET_LOCKED, 890 FILE_V_FOFFSET_LOCK_WAITING); 891 } 892 } 893 894 static off_t 895 foffset_read(struct file *fp) 896 { 897 898 return (atomic_load_long(&fp->f_offset)); 899 } 900 901 void 902 fsetfl_lock(struct file *fp) 903 { 904 file_v_lock(fp, FILE_V_SETFL_LOCKED, FILE_V_SETFL_LOCK_WAITING); 905 } 906 907 void 908 fsetfl_unlock(struct file *fp) 909 { 910 file_v_unlock(fp, FILE_V_SETFL_LOCKED, FILE_V_SETFL_LOCK_WAITING); 911 } 912 913 #else /* OFF_MAX <= LONG_MAX */ 914 915 static void 916 file_v_lock_mtxp(struct file *fp, struct mtx *mtxp, short lock_bit, 917 short lock_wait_bit) 918 { 919 mtx_assert(mtxp, MA_OWNED); 920 921 while ((fp->f_vflags & lock_bit) != 0) { 922 fp->f_vflags |= lock_wait_bit; 923 msleep(&fp->f_vflags, mtxp, PRI_MAX_KERN, 924 "vofflock", 0); 925 } 926 fp->f_vflags |= lock_bit; 927 } 928 929 static void 930 file_v_unlock_mtxp(struct file *fp, struct mtx *mtxp, short lock_bit, 931 short lock_wait_bit) 932 { 933 mtx_assert(mtxp, MA_OWNED); 934 935 KASSERT((fp->f_vflags & lock_bit) != 0, ("Lost lock_bit")); 936 if ((fp->f_vflags & lock_wait_bit) != 0) 937 wakeup(&fp->f_vflags); 938 fp->f_vflags &= ~(lock_bit | lock_wait_bit); 939 } 940 941 off_t 942 foffset_lock(struct file *fp, int flags) 943 { 944 struct mtx *mtxp; 945 off_t res; 946 947 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 948 949 mtxp = mtx_pool_find(mtxpool_sleep, fp); 950 mtx_lock(mtxp); 951 if ((flags & FOF_NOLOCK) == 0) { 952 file_v_lock_mtxp(fp, mtxp, FILE_V_FOFFSET_LOCKED, 953 FILE_V_FOFFSET_LOCK_WAITING); 954 } 955 res = fp->f_offset; 956 mtx_unlock(mtxp); 957 return (res); 958 } 959 960 void 961 foffset_unlock(struct file *fp, off_t val, int flags) 962 { 963 struct mtx *mtxp; 964 965 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 966 967 mtxp = mtx_pool_find(mtxpool_sleep, fp); 968 mtx_lock(mtxp); 969 if ((flags & FOF_NOUPDATE) == 0) 970 fp->f_offset = val; 971 if ((flags & FOF_NEXTOFF_R) != 0) 972 fp->f_nextoff[UIO_READ] = val; 973 if ((flags & FOF_NEXTOFF_W) != 0) 974 fp->f_nextoff[UIO_WRITE] = val; 975 if ((flags & FOF_NOLOCK) == 0) { 976 file_v_unlock_mtxp(fp, mtxp, FILE_V_FOFFSET_LOCKED, 977 FILE_V_FOFFSET_LOCK_WAITING); 978 } 979 mtx_unlock(mtxp); 980 } 981 982 static off_t 983 foffset_read(struct file *fp) 984 { 985 986 return (foffset_lock(fp, FOF_NOLOCK)); 987 } 988 989 void 990 fsetfl_lock(struct file *fp) 991 { 992 struct mtx *mtxp; 993 994 mtxp = mtx_pool_find(mtxpool_sleep, fp); 995 mtx_lock(mtxp); 996 file_v_lock_mtxp(fp, mtxp, FILE_V_SETFL_LOCKED, 997 FILE_V_SETFL_LOCK_WAITING); 998 mtx_unlock(mtxp); 999 } 1000 1001 void 1002 fsetfl_unlock(struct file *fp) 1003 { 1004 struct mtx *mtxp; 1005 1006 mtxp = mtx_pool_find(mtxpool_sleep, fp); 1007 mtx_lock(mtxp); 1008 file_v_unlock_mtxp(fp, mtxp, FILE_V_SETFL_LOCKED, 1009 FILE_V_SETFL_LOCK_WAITING); 1010 mtx_unlock(mtxp); 1011 } 1012 #endif 1013 1014 void 1015 foffset_lock_pair(struct file *fp1, off_t *off1p, struct file *fp2, off_t *off2p, 1016 int flags) 1017 { 1018 KASSERT(fp1 != fp2, ("foffset_lock_pair: fp1 == fp2")); 1019 1020 /* Lock in a consistent order to avoid deadlock. */ 1021 if ((uintptr_t)fp1 > (uintptr_t)fp2) { 1022 struct file *tmpfp; 1023 off_t *tmpoffp; 1024 1025 tmpfp = fp1, fp1 = fp2, fp2 = tmpfp; 1026 tmpoffp = off1p, off1p = off2p, off2p = tmpoffp; 1027 } 1028 if (fp1 != NULL) 1029 *off1p = foffset_lock(fp1, flags); 1030 if (fp2 != NULL) 1031 *off2p = foffset_lock(fp2, flags); 1032 } 1033 1034 void 1035 foffset_lock_uio(struct file *fp, struct uio *uio, int flags) 1036 { 1037 1038 if ((flags & FOF_OFFSET) == 0) 1039 uio->uio_offset = foffset_lock(fp, flags); 1040 } 1041 1042 void 1043 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags) 1044 { 1045 1046 if ((flags & FOF_OFFSET) == 0) 1047 foffset_unlock(fp, uio->uio_offset, flags); 1048 } 1049 1050 static int 1051 get_advice(struct file *fp, struct uio *uio) 1052 { 1053 struct mtx *mtxp; 1054 int ret; 1055 1056 ret = POSIX_FADV_NORMAL; 1057 if (fp->f_advice == NULL || fp->f_vnode->v_type != VREG) 1058 return (ret); 1059 1060 mtxp = mtx_pool_find(mtxpool_sleep, fp); 1061 mtx_lock(mtxp); 1062 if (fp->f_advice != NULL && 1063 uio->uio_offset >= fp->f_advice->fa_start && 1064 uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end) 1065 ret = fp->f_advice->fa_advice; 1066 mtx_unlock(mtxp); 1067 return (ret); 1068 } 1069 1070 static int 1071 get_write_ioflag(struct file *fp) 1072 { 1073 int ioflag; 1074 struct mount *mp; 1075 struct vnode *vp; 1076 1077 ioflag = 0; 1078 vp = fp->f_vnode; 1079 mp = atomic_load_ptr(&vp->v_mount); 1080 1081 if ((fp->f_flag & O_DIRECT) != 0) 1082 ioflag |= IO_DIRECT; 1083 1084 if ((fp->f_flag & O_FSYNC) != 0 || 1085 (mp != NULL && (mp->mnt_flag & MNT_SYNCHRONOUS) != 0)) 1086 ioflag |= IO_SYNC; 1087 1088 /* 1089 * For O_DSYNC we set both IO_SYNC and IO_DATASYNC, so that VOP_WRITE() 1090 * or VOP_DEALLOCATE() implementations that don't understand IO_DATASYNC 1091 * fall back to full O_SYNC behavior. 1092 */ 1093 if ((fp->f_flag & O_DSYNC) != 0) 1094 ioflag |= IO_SYNC | IO_DATASYNC; 1095 1096 return (ioflag); 1097 } 1098 1099 int 1100 vn_read_from_obj(struct vnode *vp, struct uio *uio) 1101 { 1102 vm_object_t obj; 1103 vm_page_t ma[io_hold_cnt + 2]; 1104 off_t off, vsz; 1105 ssize_t resid; 1106 int error, i, j; 1107 1108 MPASS(uio->uio_resid <= ptoa(io_hold_cnt + 2)); 1109 obj = atomic_load_ptr(&vp->v_object); 1110 if (obj == NULL) 1111 return (EJUSTRETURN); 1112 1113 /* 1114 * Depends on type stability of vm_objects. 1115 */ 1116 vm_object_pip_add(obj, 1); 1117 if ((obj->flags & OBJ_DEAD) != 0) { 1118 /* 1119 * Note that object might be already reused from the 1120 * vnode, and the OBJ_DEAD flag cleared. This is fine, 1121 * we recheck for DOOMED vnode state after all pages 1122 * are busied, and retract then. 1123 * 1124 * But we check for OBJ_DEAD to ensure that we do not 1125 * busy pages while vm_object_terminate_pages() 1126 * processes the queue. 1127 */ 1128 error = EJUSTRETURN; 1129 goto out_pip; 1130 } 1131 1132 resid = uio->uio_resid; 1133 off = uio->uio_offset; 1134 for (i = 0; resid > 0; i++) { 1135 MPASS(i < io_hold_cnt + 2); 1136 ma[i] = vm_page_grab_unlocked(obj, atop(off), 1137 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY | 1138 VM_ALLOC_NOWAIT); 1139 if (ma[i] == NULL) 1140 break; 1141 1142 /* 1143 * Skip invalid pages. Valid mask can be partial only 1144 * at EOF, and we clip later. 1145 */ 1146 if (vm_page_none_valid(ma[i])) { 1147 vm_page_sunbusy(ma[i]); 1148 break; 1149 } 1150 1151 resid -= PAGE_SIZE; 1152 off += PAGE_SIZE; 1153 } 1154 if (i == 0) { 1155 error = EJUSTRETURN; 1156 goto out_pip; 1157 } 1158 1159 /* 1160 * Check VIRF_DOOMED after we busied our pages. Since 1161 * vgonel() terminates the vnode' vm_object, it cannot 1162 * process past pages busied by us. 1163 */ 1164 if (VN_IS_DOOMED(vp)) { 1165 error = EJUSTRETURN; 1166 goto out; 1167 } 1168 1169 resid = PAGE_SIZE - (uio->uio_offset & PAGE_MASK) + ptoa(i - 1); 1170 if (resid > uio->uio_resid) 1171 resid = uio->uio_resid; 1172 1173 /* 1174 * Unlocked read of vnp_size is safe because truncation cannot 1175 * pass busied page. But we load vnp_size into a local 1176 * variable so that possible concurrent extension does not 1177 * break calculation. 1178 */ 1179 #if defined(__powerpc__) && !defined(__powerpc64__) 1180 vsz = obj->un_pager.vnp.vnp_size; 1181 #else 1182 vsz = atomic_load_64(&obj->un_pager.vnp.vnp_size); 1183 #endif 1184 if (uio->uio_offset >= vsz) { 1185 error = EJUSTRETURN; 1186 goto out; 1187 } 1188 if (uio->uio_offset + resid > vsz) 1189 resid = vsz - uio->uio_offset; 1190 1191 error = vn_io_fault_pgmove(ma, uio->uio_offset & PAGE_MASK, resid, uio); 1192 1193 out: 1194 for (j = 0; j < i; j++) { 1195 if (error == 0) 1196 vm_page_reference(ma[j]); 1197 vm_page_sunbusy(ma[j]); 1198 } 1199 out_pip: 1200 vm_object_pip_wakeup(obj); 1201 if (error != 0) 1202 return (error); 1203 return (uio->uio_resid == 0 ? 0 : EJUSTRETURN); 1204 } 1205 1206 /* 1207 * File table vnode read routine. 1208 */ 1209 static int 1210 vn_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, 1211 struct thread *td) 1212 { 1213 struct vnode *vp; 1214 off_t orig_offset; 1215 int error, ioflag; 1216 int advice; 1217 1218 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 1219 uio->uio_td, td)); 1220 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 1221 vp = fp->f_vnode; 1222 ioflag = 0; 1223 if (fp->f_flag & FNONBLOCK) 1224 ioflag |= IO_NDELAY; 1225 if (fp->f_flag & O_DIRECT) 1226 ioflag |= IO_DIRECT; 1227 1228 /* 1229 * Try to read from page cache. VIRF_DOOMED check is racy but 1230 * allows us to avoid unneeded work outright. 1231 */ 1232 if (vn_io_pgcache_read_enable && !mac_vnode_check_read_enabled() && 1233 (vn_irflag_read(vp) & (VIRF_DOOMED | VIRF_PGREAD)) == VIRF_PGREAD) { 1234 error = VOP_READ_PGCACHE(vp, uio, ioflag, fp->f_cred); 1235 if (error == 0) { 1236 fp->f_nextoff[UIO_READ] = uio->uio_offset; 1237 return (0); 1238 } 1239 if (error != EJUSTRETURN) 1240 return (error); 1241 } 1242 1243 advice = get_advice(fp, uio); 1244 vn_lock(vp, LK_SHARED | LK_RETRY); 1245 1246 switch (advice) { 1247 case POSIX_FADV_NORMAL: 1248 case POSIX_FADV_SEQUENTIAL: 1249 case POSIX_FADV_NOREUSE: 1250 ioflag |= sequential_heuristic(uio, fp); 1251 break; 1252 case POSIX_FADV_RANDOM: 1253 /* Disable read-ahead for random I/O. */ 1254 break; 1255 } 1256 orig_offset = uio->uio_offset; 1257 1258 #ifdef MAC 1259 error = mac_vnode_check_read(active_cred, fp->f_cred, vp); 1260 if (error == 0) 1261 #endif 1262 error = VOP_READ(vp, uio, ioflag, fp->f_cred); 1263 fp->f_nextoff[UIO_READ] = uio->uio_offset; 1264 VOP_UNLOCK(vp); 1265 if (error == 0 && advice == POSIX_FADV_NOREUSE && 1266 orig_offset != uio->uio_offset) 1267 /* 1268 * Use POSIX_FADV_DONTNEED to flush pages and buffers 1269 * for the backing file after a POSIX_FADV_NOREUSE 1270 * read(2). 1271 */ 1272 error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1, 1273 POSIX_FADV_DONTNEED); 1274 return (error); 1275 } 1276 1277 /* 1278 * File table vnode write routine. 1279 */ 1280 static int 1281 vn_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, 1282 struct thread *td) 1283 { 1284 struct vnode *vp; 1285 struct mount *mp; 1286 off_t orig_offset; 1287 int error, ioflag; 1288 int advice; 1289 bool need_finished_write; 1290 1291 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 1292 uio->uio_td, td)); 1293 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 1294 vp = fp->f_vnode; 1295 if (vp->v_type == VREG) 1296 bwillwrite(); 1297 ioflag = IO_UNIT; 1298 if (vp->v_type == VREG && (fp->f_flag & O_APPEND) != 0) 1299 ioflag |= IO_APPEND; 1300 if ((fp->f_flag & FNONBLOCK) != 0) 1301 ioflag |= IO_NDELAY; 1302 ioflag |= get_write_ioflag(fp); 1303 1304 mp = NULL; 1305 need_finished_write = false; 1306 if (vp->v_type != VCHR) { 1307 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH); 1308 if (error != 0) 1309 goto unlock; 1310 need_finished_write = true; 1311 } 1312 1313 advice = get_advice(fp, uio); 1314 1315 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY); 1316 switch (advice) { 1317 case POSIX_FADV_NORMAL: 1318 case POSIX_FADV_SEQUENTIAL: 1319 case POSIX_FADV_NOREUSE: 1320 ioflag |= sequential_heuristic(uio, fp); 1321 break; 1322 case POSIX_FADV_RANDOM: 1323 /* XXX: Is this correct? */ 1324 break; 1325 } 1326 orig_offset = uio->uio_offset; 1327 1328 #ifdef MAC 1329 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 1330 if (error == 0) 1331 #endif 1332 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred); 1333 fp->f_nextoff[UIO_WRITE] = uio->uio_offset; 1334 VOP_UNLOCK(vp); 1335 if (need_finished_write) 1336 vn_finished_write(mp); 1337 if (error == 0 && advice == POSIX_FADV_NOREUSE && 1338 orig_offset != uio->uio_offset) 1339 /* 1340 * Use POSIX_FADV_DONTNEED to flush pages and buffers 1341 * for the backing file after a POSIX_FADV_NOREUSE 1342 * write(2). 1343 */ 1344 error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1, 1345 POSIX_FADV_DONTNEED); 1346 unlock: 1347 return (error); 1348 } 1349 1350 /* 1351 * The vn_io_fault() is a wrapper around vn_read() and vn_write() to 1352 * prevent the following deadlock: 1353 * 1354 * Assume that the thread A reads from the vnode vp1 into userspace 1355 * buffer buf1 backed by the pages of vnode vp2. If a page in buf1 is 1356 * currently not resident, then system ends up with the call chain 1357 * vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] -> 1358 * vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2) 1359 * which establishes lock order vp1->vn_lock, then vp2->vn_lock. 1360 * If, at the same time, thread B reads from vnode vp2 into buffer buf2 1361 * backed by the pages of vnode vp1, and some page in buf2 is not 1362 * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock. 1363 * 1364 * To prevent the lock order reversal and deadlock, vn_io_fault() does 1365 * not allow page faults to happen during VOP_READ() or VOP_WRITE(). 1366 * Instead, it first tries to do the whole range i/o with pagefaults 1367 * disabled. If all pages in the i/o buffer are resident and mapped, 1368 * VOP will succeed (ignoring the genuine filesystem errors). 1369 * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do 1370 * i/o in chunks, with all pages in the chunk prefaulted and held 1371 * using vm_fault_quick_hold_pages(). 1372 * 1373 * Filesystems using this deadlock avoidance scheme should use the 1374 * array of the held pages from uio, saved in the curthread->td_ma, 1375 * instead of doing uiomove(). A helper function 1376 * vn_io_fault_uiomove() converts uiomove request into 1377 * uiomove_fromphys() over td_ma array. 1378 * 1379 * Since vnode locks do not cover the whole i/o anymore, rangelocks 1380 * make the current i/o request atomic with respect to other i/os and 1381 * truncations. 1382 */ 1383 1384 /* 1385 * Decode vn_io_fault_args and perform the corresponding i/o. 1386 */ 1387 static int 1388 vn_io_fault_doio(struct vn_io_fault_args *args, struct uio *uio, 1389 struct thread *td) 1390 { 1391 int error, save; 1392 1393 error = 0; 1394 save = vm_fault_disable_pagefaults(); 1395 switch (args->kind) { 1396 case VN_IO_FAULT_FOP: 1397 error = (args->args.fop_args.doio)(args->args.fop_args.fp, 1398 uio, args->cred, args->flags, td); 1399 break; 1400 case VN_IO_FAULT_VOP: 1401 switch (uio->uio_rw) { 1402 case UIO_READ: 1403 error = VOP_READ(args->args.vop_args.vp, uio, 1404 args->flags, args->cred); 1405 break; 1406 case UIO_WRITE: 1407 error = VOP_WRITE(args->args.vop_args.vp, uio, 1408 args->flags, args->cred); 1409 break; 1410 } 1411 break; 1412 default: 1413 panic("vn_io_fault_doio: unknown kind of io %d %d", 1414 args->kind, uio->uio_rw); 1415 } 1416 vm_fault_enable_pagefaults(save); 1417 return (error); 1418 } 1419 1420 static int 1421 vn_io_fault_touch(char *base, const struct uio *uio) 1422 { 1423 int r; 1424 1425 r = fubyte(base); 1426 if (r == -1 || (uio->uio_rw == UIO_READ && subyte(base, r) == -1)) 1427 return (EFAULT); 1428 return (0); 1429 } 1430 1431 static int 1432 vn_io_fault_prefault_user(const struct uio *uio) 1433 { 1434 char *base; 1435 const struct iovec *iov; 1436 size_t len; 1437 ssize_t resid; 1438 int error, i; 1439 1440 KASSERT(uio->uio_segflg == UIO_USERSPACE, 1441 ("vn_io_fault_prefault userspace")); 1442 1443 error = i = 0; 1444 iov = uio->uio_iov; 1445 resid = uio->uio_resid; 1446 base = iov->iov_base; 1447 len = iov->iov_len; 1448 while (resid > 0) { 1449 error = vn_io_fault_touch(base, uio); 1450 if (error != 0) 1451 break; 1452 if (len < PAGE_SIZE) { 1453 if (len != 0) { 1454 error = vn_io_fault_touch(base + len - 1, uio); 1455 if (error != 0) 1456 break; 1457 resid -= len; 1458 } 1459 if (++i >= uio->uio_iovcnt) 1460 break; 1461 iov = uio->uio_iov + i; 1462 base = iov->iov_base; 1463 len = iov->iov_len; 1464 } else { 1465 len -= PAGE_SIZE; 1466 base += PAGE_SIZE; 1467 resid -= PAGE_SIZE; 1468 } 1469 } 1470 return (error); 1471 } 1472 1473 /* 1474 * Common code for vn_io_fault(), agnostic to the kind of i/o request. 1475 * Uses vn_io_fault_doio() to make the call to an actual i/o function. 1476 * Used from vn_rdwr() and vn_io_fault(), which encode the i/o request 1477 * into args and call vn_io_fault1() to handle faults during the user 1478 * mode buffer accesses. 1479 */ 1480 static int 1481 vn_io_fault1(struct vnode *vp, struct uio *uio, struct vn_io_fault_args *args, 1482 struct thread *td) 1483 { 1484 vm_page_t ma[io_hold_cnt + 2]; 1485 struct uio *uio_clone, short_uio; 1486 struct iovec short_iovec[1]; 1487 vm_page_t *prev_td_ma; 1488 vm_prot_t prot; 1489 vm_offset_t addr, end; 1490 size_t len, resid; 1491 ssize_t adv; 1492 int error, cnt, saveheld, prev_td_ma_cnt; 1493 1494 if (vn_io_fault_prefault) { 1495 error = vn_io_fault_prefault_user(uio); 1496 if (error != 0) 1497 return (error); /* Or ignore ? */ 1498 } 1499 1500 prot = uio->uio_rw == UIO_READ ? VM_PROT_WRITE : VM_PROT_READ; 1501 1502 /* 1503 * The UFS follows IO_UNIT directive and replays back both 1504 * uio_offset and uio_resid if an error is encountered during the 1505 * operation. But, since the iovec may be already advanced, 1506 * uio is still in an inconsistent state. 1507 * 1508 * Cache a copy of the original uio, which is advanced to the redo 1509 * point using UIO_NOCOPY below. 1510 */ 1511 uio_clone = cloneuio(uio); 1512 resid = uio->uio_resid; 1513 1514 short_uio.uio_segflg = UIO_USERSPACE; 1515 short_uio.uio_rw = uio->uio_rw; 1516 short_uio.uio_td = uio->uio_td; 1517 1518 error = vn_io_fault_doio(args, uio, td); 1519 if (error != EFAULT) 1520 goto out; 1521 1522 atomic_add_long(&vn_io_faults_cnt, 1); 1523 uio_clone->uio_segflg = UIO_NOCOPY; 1524 uiomove(NULL, resid - uio->uio_resid, uio_clone); 1525 uio_clone->uio_segflg = uio->uio_segflg; 1526 1527 saveheld = curthread_pflags_set(TDP_UIOHELD); 1528 prev_td_ma = td->td_ma; 1529 prev_td_ma_cnt = td->td_ma_cnt; 1530 1531 while (uio_clone->uio_resid != 0) { 1532 len = uio_clone->uio_iov->iov_len; 1533 if (len == 0) { 1534 KASSERT(uio_clone->uio_iovcnt >= 1, 1535 ("iovcnt underflow")); 1536 uio_clone->uio_iov++; 1537 uio_clone->uio_iovcnt--; 1538 continue; 1539 } 1540 if (len > ptoa(io_hold_cnt)) 1541 len = ptoa(io_hold_cnt); 1542 addr = (uintptr_t)uio_clone->uio_iov->iov_base; 1543 end = round_page(addr + len); 1544 if (end < addr) { 1545 error = EFAULT; 1546 break; 1547 } 1548 /* 1549 * A perfectly misaligned address and length could cause 1550 * both the start and the end of the chunk to use partial 1551 * page. +2 accounts for such a situation. 1552 */ 1553 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map, 1554 addr, len, prot, ma, io_hold_cnt + 2); 1555 if (cnt == -1) { 1556 error = EFAULT; 1557 break; 1558 } 1559 short_uio.uio_iov = &short_iovec[0]; 1560 short_iovec[0].iov_base = (void *)addr; 1561 short_uio.uio_iovcnt = 1; 1562 short_uio.uio_resid = short_iovec[0].iov_len = len; 1563 short_uio.uio_offset = uio_clone->uio_offset; 1564 td->td_ma = ma; 1565 td->td_ma_cnt = cnt; 1566 1567 error = vn_io_fault_doio(args, &short_uio, td); 1568 vm_page_unhold_pages(ma, cnt); 1569 adv = len - short_uio.uio_resid; 1570 1571 uio_clone->uio_iov->iov_base = 1572 (char *)uio_clone->uio_iov->iov_base + adv; 1573 uio_clone->uio_iov->iov_len -= adv; 1574 uio_clone->uio_resid -= adv; 1575 uio_clone->uio_offset += adv; 1576 1577 uio->uio_resid -= adv; 1578 uio->uio_offset += adv; 1579 1580 if (error != 0 || adv == 0) 1581 break; 1582 } 1583 td->td_ma = prev_td_ma; 1584 td->td_ma_cnt = prev_td_ma_cnt; 1585 curthread_pflags_restore(saveheld); 1586 out: 1587 freeuio(uio_clone); 1588 return (error); 1589 } 1590 1591 static int 1592 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred, 1593 int flags, struct thread *td) 1594 { 1595 fo_rdwr_t *doio; 1596 struct vnode *vp; 1597 void *rl_cookie; 1598 struct vn_io_fault_args args; 1599 int error; 1600 bool do_io_fault, do_rangelock; 1601 1602 doio = uio->uio_rw == UIO_READ ? vn_read : vn_write; 1603 vp = fp->f_vnode; 1604 1605 /* 1606 * The ability to read(2) on a directory has historically been 1607 * allowed for all users, but this can and has been the source of 1608 * at least one security issue in the past. As such, it is now hidden 1609 * away behind a sysctl for those that actually need it to use it, and 1610 * restricted to root when it's turned on to make it relatively safe to 1611 * leave on for longer sessions of need. 1612 */ 1613 if (vp->v_type == VDIR) { 1614 KASSERT(uio->uio_rw == UIO_READ, 1615 ("illegal write attempted on a directory")); 1616 if (!vfs_allow_read_dir) 1617 return (EISDIR); 1618 if ((error = priv_check(td, PRIV_VFS_READ_DIR)) != 0) 1619 return (EISDIR); 1620 } 1621 1622 do_io_fault = do_vn_io_fault(vp, uio); 1623 do_rangelock = do_io_fault || (vn_irflag_read(vp) & VIRF_PGREAD) != 0; 1624 foffset_lock_uio(fp, uio, flags); 1625 if (do_rangelock) { 1626 if (uio->uio_rw == UIO_READ) { 1627 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset, 1628 uio->uio_offset + uio->uio_resid); 1629 } else if ((fp->f_flag & O_APPEND) != 0 || 1630 (flags & FOF_OFFSET) == 0) { 1631 /* For appenders, punt and lock the whole range. */ 1632 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1633 } else { 1634 rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset, 1635 uio->uio_offset + uio->uio_resid); 1636 } 1637 } 1638 if (do_io_fault) { 1639 args.kind = VN_IO_FAULT_FOP; 1640 args.args.fop_args.fp = fp; 1641 args.args.fop_args.doio = doio; 1642 args.cred = active_cred; 1643 args.flags = flags | FOF_OFFSET; 1644 error = vn_io_fault1(vp, uio, &args, td); 1645 } else { 1646 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td); 1647 } 1648 if (do_rangelock) 1649 vn_rangelock_unlock(vp, rl_cookie); 1650 foffset_unlock_uio(fp, uio, flags); 1651 return (error); 1652 } 1653 1654 /* 1655 * Helper function to perform the requested uiomove operation using 1656 * the held pages for io->uio_iov[0].iov_base buffer instead of 1657 * copyin/copyout. Access to the pages with uiomove_fromphys() 1658 * instead of iov_base prevents page faults that could occur due to 1659 * pmap_collect() invalidating the mapping created by 1660 * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or 1661 * object cleanup revoking the write access from page mappings. 1662 * 1663 * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove() 1664 * instead of plain uiomove(). 1665 */ 1666 int 1667 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio) 1668 { 1669 struct uio transp_uio; 1670 struct iovec transp_iov[1]; 1671 struct thread *td; 1672 size_t adv; 1673 int error, pgadv; 1674 1675 td = curthread; 1676 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1677 uio->uio_segflg != UIO_USERSPACE) 1678 return (uiomove(data, xfersize, uio)); 1679 1680 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1681 transp_iov[0].iov_base = data; 1682 transp_uio.uio_iov = &transp_iov[0]; 1683 transp_uio.uio_iovcnt = 1; 1684 if (xfersize > uio->uio_resid) 1685 xfersize = uio->uio_resid; 1686 transp_uio.uio_resid = transp_iov[0].iov_len = xfersize; 1687 transp_uio.uio_offset = 0; 1688 transp_uio.uio_segflg = UIO_SYSSPACE; 1689 /* 1690 * Since transp_iov points to data, and td_ma page array 1691 * corresponds to original uio->uio_iov, we need to invert the 1692 * direction of the i/o operation as passed to 1693 * uiomove_fromphys(). 1694 */ 1695 switch (uio->uio_rw) { 1696 case UIO_WRITE: 1697 transp_uio.uio_rw = UIO_READ; 1698 break; 1699 case UIO_READ: 1700 transp_uio.uio_rw = UIO_WRITE; 1701 break; 1702 } 1703 transp_uio.uio_td = uio->uio_td; 1704 error = uiomove_fromphys(td->td_ma, 1705 ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK, 1706 xfersize, &transp_uio); 1707 adv = xfersize - transp_uio.uio_resid; 1708 pgadv = 1709 (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) - 1710 (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT); 1711 td->td_ma += pgadv; 1712 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1713 pgadv)); 1714 td->td_ma_cnt -= pgadv; 1715 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv; 1716 uio->uio_iov->iov_len -= adv; 1717 uio->uio_resid -= adv; 1718 uio->uio_offset += adv; 1719 return (error); 1720 } 1721 1722 int 1723 vn_io_fault_pgmove(vm_page_t ma[], vm_offset_t offset, int xfersize, 1724 struct uio *uio) 1725 { 1726 struct thread *td; 1727 vm_offset_t iov_base; 1728 int cnt, pgadv; 1729 1730 td = curthread; 1731 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1732 uio->uio_segflg != UIO_USERSPACE) 1733 return (uiomove_fromphys(ma, offset, xfersize, uio)); 1734 1735 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1736 cnt = xfersize > uio->uio_resid ? uio->uio_resid : xfersize; 1737 iov_base = (vm_offset_t)uio->uio_iov->iov_base; 1738 switch (uio->uio_rw) { 1739 case UIO_WRITE: 1740 pmap_copy_pages(td->td_ma, iov_base & PAGE_MASK, ma, 1741 offset, cnt); 1742 break; 1743 case UIO_READ: 1744 pmap_copy_pages(ma, offset, td->td_ma, iov_base & PAGE_MASK, 1745 cnt); 1746 break; 1747 } 1748 pgadv = ((iov_base + cnt) >> PAGE_SHIFT) - (iov_base >> PAGE_SHIFT); 1749 td->td_ma += pgadv; 1750 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1751 pgadv)); 1752 td->td_ma_cnt -= pgadv; 1753 uio->uio_iov->iov_base = (char *)(iov_base + cnt); 1754 uio->uio_iov->iov_len -= cnt; 1755 uio->uio_resid -= cnt; 1756 uio->uio_offset += cnt; 1757 return (0); 1758 } 1759 1760 /* 1761 * File table truncate routine. 1762 */ 1763 static int 1764 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred, 1765 struct thread *td) 1766 { 1767 struct mount *mp; 1768 struct vnode *vp; 1769 void *rl_cookie; 1770 int error; 1771 1772 vp = fp->f_vnode; 1773 1774 retry: 1775 /* 1776 * Lock the whole range for truncation. Otherwise split i/o 1777 * might happen partly before and partly after the truncation. 1778 */ 1779 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1780 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH); 1781 if (error) 1782 goto out1; 1783 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1784 AUDIT_ARG_VNODE1(vp); 1785 if (vp->v_type == VDIR) { 1786 error = EISDIR; 1787 goto out; 1788 } 1789 #ifdef MAC 1790 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 1791 if (error) 1792 goto out; 1793 #endif 1794 error = vn_truncate_locked(vp, length, (fp->f_flag & O_FSYNC) != 0, 1795 fp->f_cred); 1796 out: 1797 VOP_UNLOCK(vp); 1798 vn_finished_write(mp); 1799 out1: 1800 vn_rangelock_unlock(vp, rl_cookie); 1801 if (error == ERELOOKUP) 1802 goto retry; 1803 return (error); 1804 } 1805 1806 /* 1807 * Truncate a file that is already locked. 1808 */ 1809 int 1810 vn_truncate_locked(struct vnode *vp, off_t length, bool sync, 1811 struct ucred *cred) 1812 { 1813 struct vattr vattr; 1814 int error; 1815 1816 error = VOP_ADD_WRITECOUNT(vp, 1); 1817 if (error == 0) { 1818 VATTR_NULL(&vattr); 1819 vattr.va_size = length; 1820 if (sync) 1821 vattr.va_vaflags |= VA_SYNC; 1822 error = VOP_SETATTR(vp, &vattr, cred); 1823 VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 1824 if (error == 0) 1825 INOTIFY(vp, IN_MODIFY); 1826 } 1827 return (error); 1828 } 1829 1830 /* 1831 * File table vnode stat routine. 1832 */ 1833 int 1834 vn_statfile(struct file *fp, struct stat *sb, struct ucred *active_cred) 1835 { 1836 struct vnode *vp = fp->f_vnode; 1837 int error; 1838 1839 vn_lock(vp, LK_SHARED | LK_RETRY); 1840 error = VOP_STAT(vp, sb, active_cred, fp->f_cred); 1841 VOP_UNLOCK(vp); 1842 1843 return (error); 1844 } 1845 1846 /* 1847 * File table vnode ioctl routine. 1848 */ 1849 static int 1850 vn_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, 1851 struct thread *td) 1852 { 1853 struct vnode *vp; 1854 struct fiobmap2_arg *bmarg; 1855 off_t size; 1856 int error; 1857 1858 vp = fp->f_vnode; 1859 switch (vp->v_type) { 1860 case VDIR: 1861 case VREG: 1862 switch (com) { 1863 case FIONREAD: 1864 error = vn_getsize(vp, &size, active_cred); 1865 if (error == 0) 1866 *(int *)data = size - fp->f_offset; 1867 return (error); 1868 case FIOBMAP2: 1869 bmarg = (struct fiobmap2_arg *)data; 1870 vn_lock(vp, LK_SHARED | LK_RETRY); 1871 #ifdef MAC 1872 error = mac_vnode_check_read(active_cred, fp->f_cred, 1873 vp); 1874 if (error == 0) 1875 #endif 1876 error = VOP_BMAP(vp, bmarg->bn, NULL, 1877 &bmarg->bn, &bmarg->runp, &bmarg->runb); 1878 VOP_UNLOCK(vp); 1879 return (error); 1880 case FIONBIO: 1881 case FIOASYNC: 1882 return (0); 1883 default: 1884 return (VOP_IOCTL(vp, com, data, fp->f_flag, 1885 active_cred, td)); 1886 } 1887 break; 1888 case VCHR: 1889 return (VOP_IOCTL(vp, com, data, fp->f_flag, 1890 active_cred, td)); 1891 default: 1892 return (ENOTTY); 1893 } 1894 } 1895 1896 /* 1897 * File table vnode poll routine. 1898 */ 1899 static int 1900 vn_poll(struct file *fp, int events, struct ucred *active_cred, 1901 struct thread *td) 1902 { 1903 struct vnode *vp; 1904 int error; 1905 1906 vp = fp->f_vnode; 1907 #if defined(MAC) || defined(AUDIT) 1908 if (AUDITING_TD(td) || mac_vnode_check_poll_enabled()) { 1909 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1910 AUDIT_ARG_VNODE1(vp); 1911 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp); 1912 VOP_UNLOCK(vp); 1913 if (error != 0) 1914 return (error); 1915 } 1916 #endif 1917 error = VOP_POLL(vp, events, fp->f_cred, td); 1918 return (error); 1919 } 1920 1921 /* 1922 * Acquire the requested lock and then check for validity. LK_RETRY 1923 * permits vn_lock to return doomed vnodes. 1924 */ 1925 static int __noinline 1926 _vn_lock_fallback(struct vnode *vp, int flags, const char *file, int line, 1927 int error) 1928 { 1929 1930 KASSERT((flags & LK_RETRY) == 0 || error == 0, 1931 ("vn_lock: error %d incompatible with flags %#x", error, flags)); 1932 1933 if (error == 0) 1934 VNASSERT(VN_IS_DOOMED(vp), vp, ("vnode not doomed")); 1935 1936 if ((flags & LK_RETRY) == 0) { 1937 if (error == 0) { 1938 VOP_UNLOCK(vp); 1939 error = ENOENT; 1940 } 1941 return (error); 1942 } 1943 1944 /* 1945 * LK_RETRY case. 1946 * 1947 * Nothing to do if we got the lock. 1948 */ 1949 if (error == 0) 1950 return (0); 1951 1952 /* 1953 * Interlock was dropped by the call in _vn_lock. 1954 */ 1955 flags &= ~LK_INTERLOCK; 1956 do { 1957 error = VOP_LOCK1(vp, flags, file, line); 1958 } while (error != 0); 1959 return (0); 1960 } 1961 1962 static int 1963 vn_lock_delayed_setsize(struct vnode *vp, int flags, const char *file, int line) 1964 { 1965 struct vop_lock1_args ap; 1966 int error, lktype; 1967 bool onfault; 1968 1969 ASSERT_VOP_LOCKED(vp, "vn_lock_delayed_setsize"); 1970 lktype = flags & LK_TYPE_MASK; 1971 if (vp->v_op == &dead_vnodeops) 1972 return (0); 1973 VI_LOCK(vp); 1974 if ((vp->v_iflag & VI_DELAYED_SETSIZE) == 0 || (lktype != LK_SHARED && 1975 lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE && 1976 lktype != LK_TRYUPGRADE)) { 1977 VI_UNLOCK(vp); 1978 return (0); 1979 } 1980 onfault = (flags & LK_EATTR_MASK) == LK_NOWAIT && 1981 (flags & LK_INIT_MASK) == LK_CANRECURSE && 1982 (lktype == LK_SHARED || lktype == LK_EXCLUSIVE); 1983 if (onfault && vp->v_vnlock->lk_recurse == 0) { 1984 /* 1985 * Force retry in vm_fault(), to make the lock request 1986 * sleepable, which allows us to piggy-back the 1987 * sleepable call to vnode_pager_setsize(). 1988 */ 1989 VI_UNLOCK(vp); 1990 VOP_UNLOCK(vp); 1991 return (EBUSY); 1992 } 1993 if ((flags & LK_NOWAIT) != 0 || 1994 (lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) { 1995 VI_UNLOCK(vp); 1996 return (0); 1997 } 1998 if (lktype == LK_SHARED) { 1999 VOP_UNLOCK(vp); 2000 ap.a_gen.a_desc = &vop_lock1_desc; 2001 ap.a_vp = vp; 2002 ap.a_flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE | 2003 LK_INTERLOCK; 2004 ap.a_file = file; 2005 ap.a_line = line; 2006 error = VOP_LOCK1_APV(&default_vnodeops, &ap); 2007 if (error != 0 || vp->v_op == &dead_vnodeops) 2008 return (error); 2009 if (vp->v_data == NULL) 2010 goto downgrade; 2011 VI_LOCK(vp); 2012 if ((vp->v_iflag & VI_DELAYED_SETSIZE) == 0) { 2013 VI_UNLOCK(vp); 2014 goto downgrade; 2015 } 2016 } 2017 vn_clear_delayed_setsize_locked(vp); 2018 VI_UNLOCK(vp); 2019 VOP_DELAYED_SETSIZE(vp); 2020 downgrade: 2021 if (lktype == LK_SHARED) { 2022 ap.a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK); 2023 ap.a_flags |= LK_DOWNGRADE; 2024 (void)VOP_LOCK1_APV(&default_vnodeops, &ap); 2025 } 2026 return (0); 2027 } 2028 2029 int 2030 _vn_lock(struct vnode *vp, int flags, const char *file, int line) 2031 { 2032 int error; 2033 2034 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 2035 ("vn_lock: no locktype (%d passed)", flags)); 2036 VNPASS(vp->v_holdcnt > 0, vp); 2037 error = VOP_LOCK1(vp, flags, file, line); 2038 if (__predict_false(error != 0 || VN_IS_DOOMED(vp))) 2039 error = _vn_lock_fallback(vp, flags, file, line, error); 2040 if (error != 0 || __predict_true((atomic_load_short(&vp->v_iflag) & 2041 VI_DELAYED_SETSIZE) == 0)) 2042 return (error); 2043 return (vn_lock_delayed_setsize(vp, flags, file, line)); 2044 } 2045 2046 /* 2047 * File table vnode close routine. 2048 */ 2049 static int 2050 vn_closefile(struct file *fp, struct thread *td) 2051 { 2052 struct vnode *vp; 2053 struct flock lf; 2054 int error; 2055 bool ref; 2056 2057 vp = fp->f_vnode; 2058 fp->f_ops = &badfileops; 2059 ref = (fp->f_flag & FHASLOCK) != 0; 2060 2061 error = vn_close1(vp, fp->f_flag, fp->f_cred, td, ref); 2062 2063 if (__predict_false(ref)) { 2064 lf.l_whence = SEEK_SET; 2065 lf.l_start = 0; 2066 lf.l_len = 0; 2067 lf.l_type = F_UNLCK; 2068 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK); 2069 vrele(vp); 2070 } 2071 return (error); 2072 } 2073 2074 /* 2075 * Preparing to start a filesystem write operation. If the operation is 2076 * permitted, then we bump the count of operations in progress and 2077 * proceed. If a suspend request is in progress, we wait until the 2078 * suspension is over, and then proceed. 2079 */ 2080 static int 2081 vn_start_write_refed(struct mount *mp, int flags, bool mplocked) 2082 { 2083 struct mount_pcpu *mpcpu; 2084 int error, mflags; 2085 2086 if (__predict_true(!mplocked) && (flags & V_XSLEEP) == 0 && 2087 vfs_op_thread_enter(mp, mpcpu)) { 2088 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) == 0); 2089 vfs_mp_count_add_pcpu(mpcpu, writeopcount, 1); 2090 vfs_op_thread_exit(mp, mpcpu); 2091 return (0); 2092 } 2093 2094 if (mplocked) 2095 mtx_assert(MNT_MTX(mp), MA_OWNED); 2096 else 2097 MNT_ILOCK(mp); 2098 2099 error = 0; 2100 2101 /* 2102 * Check on status of suspension. 2103 */ 2104 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 || 2105 mp->mnt_susp_owner != curthread) { 2106 mflags = 0; 2107 if ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0) { 2108 if (flags & V_PCATCH) 2109 mflags |= PCATCH; 2110 } 2111 mflags |= PRI_MAX_KERN; 2112 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 2113 if ((flags & V_NOWAIT) != 0) { 2114 error = EWOULDBLOCK; 2115 goto unlock; 2116 } 2117 error = msleep(&mp->mnt_flag, MNT_MTX(mp), mflags, 2118 "suspfs", 0); 2119 if (error != 0) 2120 goto unlock; 2121 } 2122 } 2123 if ((flags & V_XSLEEP) != 0) 2124 goto unlock; 2125 mp->mnt_writeopcount++; 2126 unlock: 2127 if (error != 0 || (flags & V_XSLEEP) != 0) 2128 MNT_REL(mp); 2129 MNT_IUNLOCK(mp); 2130 return (error); 2131 } 2132 2133 int 2134 vn_start_write(struct vnode *vp, struct mount **mpp, int flags) 2135 { 2136 struct mount *mp; 2137 int error; 2138 2139 KASSERT((flags & ~V_VALID_FLAGS) == 0, 2140 ("%s: invalid flags passed %d\n", __func__, flags)); 2141 2142 error = 0; 2143 /* 2144 * If a vnode is provided, get and return the mount point that 2145 * to which it will write. 2146 */ 2147 if (vp != NULL) { 2148 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 2149 *mpp = NULL; 2150 if (error != EOPNOTSUPP) 2151 return (error); 2152 return (0); 2153 } 2154 } 2155 if ((mp = *mpp) == NULL) 2156 return (0); 2157 2158 /* 2159 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 2160 * a vfs_ref(). 2161 * As long as a vnode is not provided we need to acquire a 2162 * refcount for the provided mountpoint too, in order to 2163 * emulate a vfs_ref(). 2164 */ 2165 if (vp == NULL) 2166 vfs_ref(mp); 2167 2168 error = vn_start_write_refed(mp, flags, false); 2169 if (error != 0 && (flags & V_NOWAIT) == 0) 2170 *mpp = NULL; 2171 return (error); 2172 } 2173 2174 /* 2175 * Secondary suspension. Used by operations such as vop_inactive 2176 * routines that are needed by the higher level functions. These 2177 * are allowed to proceed until all the higher level functions have 2178 * completed (indicated by mnt_writeopcount dropping to zero). At that 2179 * time, these operations are halted until the suspension is over. 2180 */ 2181 int 2182 vn_start_secondary_write(struct vnode *vp, struct mount **mpp, int flags) 2183 { 2184 struct mount *mp; 2185 int error, mflags; 2186 2187 KASSERT((flags & (~V_VALID_FLAGS | V_XSLEEP)) == 0, 2188 ("%s: invalid flags passed %d\n", __func__, flags)); 2189 2190 retry: 2191 if (vp != NULL) { 2192 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 2193 *mpp = NULL; 2194 if (error != EOPNOTSUPP) 2195 return (error); 2196 return (0); 2197 } 2198 } 2199 /* 2200 * If we are not suspended or have not yet reached suspended 2201 * mode, then let the operation proceed. 2202 */ 2203 if ((mp = *mpp) == NULL) 2204 return (0); 2205 2206 /* 2207 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 2208 * a vfs_ref(). 2209 * As long as a vnode is not provided we need to acquire a 2210 * refcount for the provided mountpoint too, in order to 2211 * emulate a vfs_ref(). 2212 */ 2213 MNT_ILOCK(mp); 2214 if (vp == NULL) 2215 MNT_REF(mp); 2216 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) { 2217 mp->mnt_secondary_writes++; 2218 mp->mnt_secondary_accwrites++; 2219 MNT_IUNLOCK(mp); 2220 return (0); 2221 } 2222 if ((flags & V_NOWAIT) != 0) { 2223 MNT_REL(mp); 2224 MNT_IUNLOCK(mp); 2225 *mpp = NULL; 2226 return (EWOULDBLOCK); 2227 } 2228 /* 2229 * Wait for the suspension to finish. 2230 */ 2231 mflags = 0; 2232 if ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0) { 2233 if ((flags & V_PCATCH) != 0) 2234 mflags |= PCATCH; 2235 } 2236 mflags |= PRI_MAX_KERN | PDROP; 2237 error = msleep(&mp->mnt_flag, MNT_MTX(mp), mflags, "suspfs", 0); 2238 vfs_rel(mp); 2239 if (error == 0) 2240 goto retry; 2241 *mpp = NULL; 2242 return (error); 2243 } 2244 2245 /* 2246 * Filesystem write operation has completed. If we are suspending and this 2247 * operation is the last one, notify the suspender that the suspension is 2248 * now in effect. 2249 */ 2250 void 2251 vn_finished_write(struct mount *mp) 2252 { 2253 struct mount_pcpu *mpcpu; 2254 int c; 2255 2256 if (mp == NULL) 2257 return; 2258 2259 if (vfs_op_thread_enter(mp, mpcpu)) { 2260 vfs_mp_count_sub_pcpu(mpcpu, writeopcount, 1); 2261 vfs_mp_count_sub_pcpu(mpcpu, ref, 1); 2262 vfs_op_thread_exit(mp, mpcpu); 2263 return; 2264 } 2265 2266 MNT_ILOCK(mp); 2267 vfs_assert_mount_counters(mp); 2268 MNT_REL(mp); 2269 c = --mp->mnt_writeopcount; 2270 if (mp->mnt_vfs_ops == 0) { 2271 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) == 0); 2272 MNT_IUNLOCK(mp); 2273 return; 2274 } 2275 if (c < 0) 2276 vfs_dump_mount_counters(mp); 2277 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && c == 0) 2278 wakeup(&mp->mnt_writeopcount); 2279 MNT_IUNLOCK(mp); 2280 } 2281 2282 /* 2283 * Filesystem secondary write operation has completed. If we are 2284 * suspending and this operation is the last one, notify the suspender 2285 * that the suspension is now in effect. 2286 */ 2287 void 2288 vn_finished_secondary_write(struct mount *mp) 2289 { 2290 if (mp == NULL) 2291 return; 2292 MNT_ILOCK(mp); 2293 MNT_REL(mp); 2294 mp->mnt_secondary_writes--; 2295 if (mp->mnt_secondary_writes < 0) 2296 panic("vn_finished_secondary_write: neg cnt"); 2297 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 2298 mp->mnt_secondary_writes <= 0) 2299 wakeup(&mp->mnt_secondary_writes); 2300 MNT_IUNLOCK(mp); 2301 } 2302 2303 /* 2304 * Request a filesystem to suspend write operations. 2305 */ 2306 int 2307 vfs_write_suspend(struct mount *mp, int flags) 2308 { 2309 int error; 2310 2311 vfs_op_enter(mp); 2312 2313 MNT_ILOCK(mp); 2314 vfs_assert_mount_counters(mp); 2315 if (mp->mnt_susp_owner == curthread) { 2316 vfs_op_exit_locked(mp); 2317 MNT_IUNLOCK(mp); 2318 return (EALREADY); 2319 } 2320 while (mp->mnt_kern_flag & MNTK_SUSPEND) 2321 msleep(&mp->mnt_flag, MNT_MTX(mp), PRI_MAX_KERN, "wsuspfs", 0); 2322 2323 /* 2324 * Unmount holds a write reference on the mount point. If we 2325 * own busy reference and drain for writers, we deadlock with 2326 * the reference draining in the unmount path. Callers of 2327 * vfs_write_suspend() must specify VS_SKIP_UNMOUNT if 2328 * vfs_busy() reference is owned and caller is not in the 2329 * unmount context. 2330 */ 2331 if ((flags & VS_SKIP_UNMOUNT) != 0 && 2332 (mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) { 2333 vfs_op_exit_locked(mp); 2334 MNT_IUNLOCK(mp); 2335 return (EBUSY); 2336 } 2337 2338 mp->mnt_kern_flag |= MNTK_SUSPEND; 2339 mp->mnt_susp_owner = curthread; 2340 if (mp->mnt_writeopcount > 0) 2341 (void) msleep(&mp->mnt_writeopcount, 2342 MNT_MTX(mp), PRI_MAX_KERN | PDROP, "suspwt", 0); 2343 else 2344 MNT_IUNLOCK(mp); 2345 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0) { 2346 vfs_write_resume(mp, 0); 2347 /* vfs_write_resume does vfs_op_exit() for us */ 2348 } 2349 return (error); 2350 } 2351 2352 /* 2353 * Request a filesystem to resume write operations. 2354 */ 2355 void 2356 vfs_write_resume(struct mount *mp, int flags) 2357 { 2358 2359 MNT_ILOCK(mp); 2360 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 2361 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner")); 2362 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 | 2363 MNTK_SUSPENDED); 2364 mp->mnt_susp_owner = NULL; 2365 wakeup(&mp->mnt_writeopcount); 2366 wakeup(&mp->mnt_flag); 2367 curthread->td_pflags &= ~TDP_IGNSUSP; 2368 if ((flags & VR_START_WRITE) != 0) { 2369 MNT_REF(mp); 2370 mp->mnt_writeopcount++; 2371 } 2372 MNT_IUNLOCK(mp); 2373 if ((flags & VR_NO_SUSPCLR) == 0) 2374 VFS_SUSP_CLEAN(mp); 2375 vfs_op_exit(mp); 2376 } else if ((flags & VR_START_WRITE) != 0) { 2377 MNT_REF(mp); 2378 vn_start_write_refed(mp, 0, true); 2379 } else { 2380 MNT_IUNLOCK(mp); 2381 } 2382 } 2383 2384 /* 2385 * Helper loop around vfs_write_suspend() for filesystem unmount VFS 2386 * methods. 2387 */ 2388 int 2389 vfs_write_suspend_umnt(struct mount *mp) 2390 { 2391 int error; 2392 2393 KASSERT((curthread->td_pflags & TDP_IGNSUSP) == 0, 2394 ("vfs_write_suspend_umnt: recursed")); 2395 2396 /* dounmount() already called vn_start_write(). */ 2397 for (;;) { 2398 vn_finished_write(mp); 2399 error = vfs_write_suspend(mp, 0); 2400 if (error != 0) { 2401 vn_start_write(NULL, &mp, V_WAIT); 2402 return (error); 2403 } 2404 MNT_ILOCK(mp); 2405 if ((mp->mnt_kern_flag & MNTK_SUSPENDED) != 0) 2406 break; 2407 MNT_IUNLOCK(mp); 2408 vn_start_write(NULL, &mp, V_WAIT); 2409 } 2410 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2); 2411 wakeup(&mp->mnt_flag); 2412 MNT_IUNLOCK(mp); 2413 curthread->td_pflags |= TDP_IGNSUSP; 2414 return (0); 2415 } 2416 2417 /* 2418 * Implement kqueues for files by translating it to vnode operation. 2419 */ 2420 static int 2421 vn_kqfilter(struct file *fp, struct knote *kn) 2422 { 2423 2424 return (VOP_KQFILTER(fp->f_vnode, kn)); 2425 } 2426 2427 int 2428 vn_kqfilter_opath(struct file *fp, struct knote *kn) 2429 { 2430 if ((fp->f_flag & FKQALLOWED) == 0) 2431 return (EBADF); 2432 return (vn_kqfilter(fp, kn)); 2433 } 2434 2435 /* 2436 * Simplified in-kernel wrapper calls for extended attribute access. 2437 * Both calls pass in a NULL credential, authorizing as "kernel" access. 2438 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 2439 */ 2440 int 2441 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 2442 const char *attrname, int *buflen, char *buf, struct thread *td) 2443 { 2444 struct uio auio; 2445 struct iovec iov; 2446 int error; 2447 2448 iov.iov_len = *buflen; 2449 iov.iov_base = buf; 2450 2451 auio.uio_iov = &iov; 2452 auio.uio_iovcnt = 1; 2453 auio.uio_rw = UIO_READ; 2454 auio.uio_segflg = UIO_SYSSPACE; 2455 auio.uio_td = td; 2456 auio.uio_offset = 0; 2457 auio.uio_resid = *buflen; 2458 2459 if ((ioflg & IO_NODELOCKED) == 0) 2460 vn_lock(vp, LK_SHARED | LK_RETRY); 2461 2462 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2463 2464 /* authorize attribute retrieval as kernel */ 2465 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL, 2466 td); 2467 2468 if ((ioflg & IO_NODELOCKED) == 0) 2469 VOP_UNLOCK(vp); 2470 2471 if (error == 0) { 2472 *buflen = *buflen - auio.uio_resid; 2473 } 2474 2475 return (error); 2476 } 2477 2478 /* 2479 * XXX failure mode if partially written? 2480 */ 2481 int 2482 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 2483 const char *attrname, int buflen, char *buf, struct thread *td) 2484 { 2485 struct uio auio; 2486 struct iovec iov; 2487 struct mount *mp; 2488 int error; 2489 2490 iov.iov_len = buflen; 2491 iov.iov_base = buf; 2492 2493 auio.uio_iov = &iov; 2494 auio.uio_iovcnt = 1; 2495 auio.uio_rw = UIO_WRITE; 2496 auio.uio_segflg = UIO_SYSSPACE; 2497 auio.uio_td = td; 2498 auio.uio_offset = 0; 2499 auio.uio_resid = buflen; 2500 2501 if ((ioflg & IO_NODELOCKED) == 0) { 2502 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 2503 return (error); 2504 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2505 } 2506 2507 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2508 2509 /* authorize attribute setting as kernel */ 2510 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td); 2511 2512 if ((ioflg & IO_NODELOCKED) == 0) { 2513 vn_finished_write(mp); 2514 VOP_UNLOCK(vp); 2515 } 2516 2517 return (error); 2518 } 2519 2520 int 2521 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 2522 const char *attrname, struct thread *td) 2523 { 2524 struct mount *mp; 2525 int error; 2526 2527 if ((ioflg & IO_NODELOCKED) == 0) { 2528 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 2529 return (error); 2530 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2531 } 2532 2533 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2534 2535 /* authorize attribute removal as kernel */ 2536 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td); 2537 if (error == EOPNOTSUPP) 2538 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, 2539 NULL, td); 2540 2541 if ((ioflg & IO_NODELOCKED) == 0) { 2542 vn_finished_write(mp); 2543 VOP_UNLOCK(vp); 2544 } 2545 2546 return (error); 2547 } 2548 2549 static int 2550 vn_get_ino_alloc_vget(struct mount *mp, void *arg, int lkflags, 2551 struct vnode **rvp) 2552 { 2553 2554 return (VFS_VGET(mp, *(ino_t *)arg, lkflags, rvp)); 2555 } 2556 2557 int 2558 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp) 2559 { 2560 2561 return (vn_vget_ino_gen(vp, vn_get_ino_alloc_vget, &ino, 2562 lkflags, rvp)); 2563 } 2564 2565 int 2566 vn_vget_ino_gen(struct vnode *vp, vn_get_ino_t alloc, void *alloc_arg, 2567 int lkflags, struct vnode **rvp) 2568 { 2569 struct mount *mp; 2570 int ltype, error; 2571 2572 ASSERT_VOP_LOCKED(vp, "vn_vget_ino_get"); 2573 mp = vp->v_mount; 2574 ltype = VOP_ISLOCKED(vp); 2575 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED, 2576 ("vn_vget_ino: vp not locked")); 2577 error = vfs_busy(mp, MBF_NOWAIT); 2578 if (error != 0) { 2579 vfs_ref(mp); 2580 VOP_UNLOCK(vp); 2581 error = vfs_busy(mp, 0); 2582 vn_lock(vp, ltype | LK_RETRY); 2583 vfs_rel(mp); 2584 if (error != 0) 2585 return (ENOENT); 2586 if (VN_IS_DOOMED(vp)) { 2587 vfs_unbusy(mp); 2588 return (ENOENT); 2589 } 2590 } 2591 VOP_UNLOCK(vp); 2592 error = alloc(mp, alloc_arg, lkflags, rvp); 2593 vfs_unbusy(mp); 2594 if (error != 0 || *rvp != vp) 2595 vn_lock(vp, ltype | LK_RETRY); 2596 if (VN_IS_DOOMED(vp)) { 2597 if (error == 0) { 2598 if (*rvp == vp) 2599 vunref(vp); 2600 else 2601 vput(*rvp); 2602 } 2603 error = ENOENT; 2604 } 2605 return (error); 2606 } 2607 2608 static void 2609 vn_send_sigxfsz(struct proc *p) 2610 { 2611 PROC_LOCK(p); 2612 kern_psignal(p, SIGXFSZ); 2613 PROC_UNLOCK(p); 2614 } 2615 2616 int 2617 vn_rlimit_trunc(u_quad_t size, struct thread *td) 2618 { 2619 if (size <= lim_cur(td, RLIMIT_FSIZE)) 2620 return (0); 2621 vn_send_sigxfsz(td->td_proc); 2622 return (EFBIG); 2623 } 2624 2625 static int 2626 vn_rlimit_fsizex1(const struct vnode *vp, struct uio *uio, off_t maxfsz, 2627 bool adj, struct thread *td) 2628 { 2629 off_t lim; 2630 bool ktr_write; 2631 2632 if (vp->v_type != VREG) 2633 return (0); 2634 2635 /* 2636 * Handle file system maximum file size. 2637 */ 2638 if (maxfsz != 0 && uio->uio_offset + uio->uio_resid > maxfsz) { 2639 if (!adj || uio->uio_offset >= maxfsz) 2640 return (EFBIG); 2641 uio->uio_resid = maxfsz - uio->uio_offset; 2642 } 2643 2644 /* 2645 * This is kernel write (e.g. vnode_pager) or accounting 2646 * write, ignore limit. 2647 */ 2648 if (td == NULL || (td->td_pflags2 & TDP2_ACCT) != 0) 2649 return (0); 2650 2651 /* 2652 * Calculate file size limit. 2653 */ 2654 ktr_write = (td->td_pflags & TDP_INKTRACE) != 0; 2655 lim = __predict_false(ktr_write) ? td->td_ktr_io_lim : 2656 lim_cur(td, RLIMIT_FSIZE); 2657 2658 /* 2659 * Is the limit reached? 2660 */ 2661 if (__predict_true((uoff_t)uio->uio_offset + uio->uio_resid <= lim)) 2662 return (0); 2663 2664 /* 2665 * Prepared filesystems can handle writes truncated to the 2666 * file size limit. 2667 */ 2668 if (adj && (uoff_t)uio->uio_offset < lim) { 2669 uio->uio_resid = lim - (uoff_t)uio->uio_offset; 2670 return (0); 2671 } 2672 2673 if (!ktr_write || ktr_filesize_limit_signal) 2674 vn_send_sigxfsz(td->td_proc); 2675 return (EFBIG); 2676 } 2677 2678 /* 2679 * Helper for VOP_WRITE() implementations, the common code to 2680 * handle maximum supported file size on the filesystem, and 2681 * RLIMIT_FSIZE, except for special writes from accounting subsystem 2682 * and ktrace. 2683 * 2684 * For maximum file size (maxfsz argument): 2685 * - return EFBIG if uio_offset is beyond it 2686 * - otherwise, clamp uio_resid if write would extend file beyond maxfsz. 2687 * 2688 * For RLIMIT_FSIZE: 2689 * - return EFBIG and send SIGXFSZ if uio_offset is beyond the limit 2690 * - otherwise, clamp uio_resid if write would extend file beyond limit. 2691 * 2692 * If clamping occured, the adjustment for uio_resid is stored in 2693 * *resid_adj, to be re-applied by vn_rlimit_fsizex_res() on return 2694 * from the VOP. 2695 */ 2696 int 2697 vn_rlimit_fsizex(const struct vnode *vp, struct uio *uio, off_t maxfsz, 2698 ssize_t *resid_adj, struct thread *td) 2699 { 2700 ssize_t resid_orig; 2701 int error; 2702 bool adj; 2703 2704 resid_orig = uio->uio_resid; 2705 adj = resid_adj != NULL; 2706 error = vn_rlimit_fsizex1(vp, uio, maxfsz, adj, td); 2707 if (adj) 2708 *resid_adj = resid_orig - uio->uio_resid; 2709 return (error); 2710 } 2711 2712 void 2713 vn_rlimit_fsizex_res(struct uio *uio, ssize_t resid_adj) 2714 { 2715 uio->uio_resid += resid_adj; 2716 } 2717 2718 int 2719 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, 2720 struct thread *td) 2721 { 2722 return (vn_rlimit_fsizex(vp, __DECONST(struct uio *, uio), 0, NULL, 2723 td)); 2724 } 2725 2726 int 2727 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 2728 struct thread *td) 2729 { 2730 struct vnode *vp; 2731 2732 vp = fp->f_vnode; 2733 #ifdef AUDIT 2734 vn_lock(vp, LK_SHARED | LK_RETRY); 2735 AUDIT_ARG_VNODE1(vp); 2736 VOP_UNLOCK(vp); 2737 #endif 2738 return (setfmode(td, active_cred, vp, mode)); 2739 } 2740 2741 int 2742 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 2743 struct thread *td) 2744 { 2745 struct vnode *vp; 2746 2747 vp = fp->f_vnode; 2748 #ifdef AUDIT 2749 vn_lock(vp, LK_SHARED | LK_RETRY); 2750 AUDIT_ARG_VNODE1(vp); 2751 VOP_UNLOCK(vp); 2752 #endif 2753 return (setfown(td, active_cred, vp, uid, gid)); 2754 } 2755 2756 /* 2757 * Remove pages in the range ["start", "end") from the vnode's VM object. If 2758 * "end" is 0, then the range extends to the end of the object. 2759 */ 2760 void 2761 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end) 2762 { 2763 vm_object_t object; 2764 2765 if ((object = vp->v_object) == NULL) 2766 return; 2767 VM_OBJECT_WLOCK(object); 2768 vm_object_page_remove(object, start, end, 0); 2769 VM_OBJECT_WUNLOCK(object); 2770 } 2771 2772 /* 2773 * Like vn_pages_remove(), but skips invalid pages, which by definition are not 2774 * mapped into any process' address space. Filesystems may use this in 2775 * preference to vn_pages_remove() to avoid blocking on pages busied in 2776 * preparation for a VOP_GETPAGES. 2777 */ 2778 void 2779 vn_pages_remove_valid(struct vnode *vp, vm_pindex_t start, vm_pindex_t end) 2780 { 2781 vm_object_t object; 2782 2783 if ((object = vp->v_object) == NULL) 2784 return; 2785 VM_OBJECT_WLOCK(object); 2786 vm_object_page_remove(object, start, end, OBJPR_VALIDONLY); 2787 VM_OBJECT_WUNLOCK(object); 2788 } 2789 2790 int 2791 vn_bmap_seekhole_locked(struct vnode *vp, u_long cmd, off_t *off, 2792 struct ucred *cred) 2793 { 2794 off_t size; 2795 daddr_t bn, bnp; 2796 uint64_t bsize; 2797 off_t noff; 2798 int error; 2799 2800 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA, 2801 ("%s: Wrong command %lu", __func__, cmd)); 2802 ASSERT_VOP_ELOCKED(vp, "vn_bmap_seekhole_locked"); 2803 2804 if (vp->v_type != VREG) { 2805 error = ENOTTY; 2806 goto out; 2807 } 2808 error = vn_getsize_locked(vp, &size, cred); 2809 if (error != 0) 2810 goto out; 2811 noff = *off; 2812 if (noff < 0 || noff >= size) { 2813 error = ENXIO; 2814 goto out; 2815 } 2816 2817 /* See the comment in ufs_bmap_seekdata(). */ 2818 vnode_pager_clean_sync(vp); 2819 2820 bsize = vp->v_mount->mnt_stat.f_iosize; 2821 for (bn = noff / bsize; noff < size; bn++, noff += bsize - 2822 noff % bsize) { 2823 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL); 2824 if (error == EOPNOTSUPP) { 2825 error = ENOTTY; 2826 goto out; 2827 } 2828 if ((bnp == -1 && cmd == FIOSEEKHOLE) || 2829 (bnp != -1 && cmd == FIOSEEKDATA)) { 2830 noff = bn * bsize; 2831 if (noff < *off) 2832 noff = *off; 2833 goto out; 2834 } 2835 } 2836 if (noff > size) 2837 noff = size; 2838 /* noff == size. There is an implicit hole at the end of file. */ 2839 if (cmd == FIOSEEKDATA) 2840 error = ENXIO; 2841 out: 2842 if (error == 0) 2843 *off = noff; 2844 return (error); 2845 } 2846 2847 int 2848 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred) 2849 { 2850 int error; 2851 2852 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA, 2853 ("%s: Wrong command %lu", __func__, cmd)); 2854 2855 if (vn_lock(vp, LK_EXCLUSIVE) != 0) 2856 return (EBADF); 2857 error = vn_bmap_seekhole_locked(vp, cmd, off, cred); 2858 VOP_UNLOCK(vp); 2859 return (error); 2860 } 2861 2862 int 2863 vn_seek(struct file *fp, off_t offset, int whence, struct thread *td) 2864 { 2865 struct ucred *cred; 2866 struct vnode *vp; 2867 off_t foffset, fsize, size; 2868 int error, noneg; 2869 2870 cred = td->td_ucred; 2871 vp = fp->f_vnode; 2872 noneg = (vp->v_type != VCHR); 2873 /* 2874 * Try to dodge locking for common case of querying the offset. 2875 */ 2876 if (whence == L_INCR && offset == 0) { 2877 foffset = foffset_read(fp); 2878 if (__predict_false(foffset < 0 && noneg)) { 2879 return (EOVERFLOW); 2880 } 2881 td->td_uretoff.tdu_off = foffset; 2882 return (0); 2883 } 2884 foffset = foffset_lock(fp, 0); 2885 error = 0; 2886 switch (whence) { 2887 case L_INCR: 2888 if (noneg && 2889 (foffset < 0 || 2890 (offset > 0 && foffset > OFF_MAX - offset))) { 2891 error = EOVERFLOW; 2892 break; 2893 } 2894 offset += foffset; 2895 break; 2896 case L_XTND: 2897 error = vn_getsize(vp, &fsize, cred); 2898 if (error != 0) 2899 break; 2900 2901 /* 2902 * If the file references a disk device, then fetch 2903 * the media size and use that to determine the ending 2904 * offset. 2905 */ 2906 if (fsize == 0 && vp->v_type == VCHR && 2907 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0) 2908 fsize = size; 2909 if (noneg && offset > 0 && fsize > OFF_MAX - offset) { 2910 error = EOVERFLOW; 2911 break; 2912 } 2913 offset += fsize; 2914 break; 2915 case L_SET: 2916 break; 2917 case SEEK_DATA: 2918 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td); 2919 if (error == ENOTTY) 2920 error = EINVAL; 2921 break; 2922 case SEEK_HOLE: 2923 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td); 2924 if (error == ENOTTY) 2925 error = EINVAL; 2926 break; 2927 default: 2928 error = EINVAL; 2929 } 2930 if (error == 0 && noneg && offset < 0) 2931 error = EINVAL; 2932 if (error != 0) 2933 goto drop; 2934 VFS_KNOTE_UNLOCKED(vp, 0); 2935 td->td_uretoff.tdu_off = offset; 2936 drop: 2937 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0); 2938 return (error); 2939 } 2940 2941 int 2942 vn_utimes_perm(struct vnode *vp, struct vattr *vap, struct ucred *cred, 2943 struct thread *td) 2944 { 2945 int error; 2946 2947 /* 2948 * Grant permission if the caller is the owner of the file, or 2949 * the super-user, or has ACL_WRITE_ATTRIBUTES permission on 2950 * on the file. If the time pointer is null, then write 2951 * permission on the file is also sufficient. 2952 * 2953 * From NFSv4.1, draft 21, 6.2.1.3.1, Discussion of Mask Attributes: 2954 * A user having ACL_WRITE_DATA or ACL_WRITE_ATTRIBUTES 2955 * will be allowed to set the times [..] to the current 2956 * server time. 2957 */ 2958 error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred, td); 2959 if (error != 0 && (vap->va_vaflags & VA_UTIMES_NULL) != 0) 2960 error = VOP_ACCESS(vp, VWRITE, cred, td); 2961 return (error); 2962 } 2963 2964 int 2965 vn_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 2966 { 2967 struct vnode *vp; 2968 int error; 2969 2970 if (fp->f_type == DTYPE_FIFO) 2971 kif->kf_type = KF_TYPE_FIFO; 2972 else 2973 kif->kf_type = KF_TYPE_VNODE; 2974 vp = fp->f_vnode; 2975 vref(vp); 2976 FILEDESC_SUNLOCK(fdp); 2977 error = vn_fill_kinfo_vnode(vp, kif); 2978 vrele(vp); 2979 FILEDESC_SLOCK(fdp); 2980 return (error); 2981 } 2982 2983 static inline void 2984 vn_fill_junk(struct kinfo_file *kif) 2985 { 2986 size_t len, olen; 2987 2988 /* 2989 * Simulate vn_fullpath returning changing values for a given 2990 * vp during e.g. coredump. 2991 */ 2992 len = (arc4random() % (sizeof(kif->kf_path) - 2)) + 1; 2993 olen = strlen(kif->kf_path); 2994 if (len < olen) 2995 strcpy(&kif->kf_path[len - 1], "$"); 2996 else 2997 for (; olen < len; olen++) 2998 strcpy(&kif->kf_path[olen], "A"); 2999 } 3000 3001 int 3002 vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif) 3003 { 3004 struct vattr va; 3005 char *fullpath, *freepath; 3006 int error; 3007 3008 kif->kf_un.kf_file.kf_file_type = vntype_to_kinfo(vp->v_type); 3009 freepath = NULL; 3010 fullpath = "-"; 3011 error = vn_fullpath(vp, &fullpath, &freepath); 3012 if (error == 0) { 3013 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); 3014 } 3015 if (freepath != NULL) 3016 free(freepath, M_TEMP); 3017 3018 KFAIL_POINT_CODE(DEBUG_FP, fill_kinfo_vnode__random_path, 3019 vn_fill_junk(kif); 3020 ); 3021 3022 /* 3023 * Retrieve vnode attributes. 3024 */ 3025 va.va_fsid = VNOVAL; 3026 va.va_rdev = NODEV; 3027 vn_lock(vp, LK_SHARED | LK_RETRY); 3028 error = VOP_GETATTR(vp, &va, curthread->td_ucred); 3029 VOP_UNLOCK(vp); 3030 if (error != 0) 3031 return (error); 3032 if (va.va_fsid != VNOVAL) 3033 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid; 3034 else 3035 kif->kf_un.kf_file.kf_file_fsid = 3036 vp->v_mount->mnt_stat.f_fsid.val[0]; 3037 kif->kf_un.kf_file.kf_file_fsid_freebsd11 = 3038 kif->kf_un.kf_file.kf_file_fsid; /* truncate */ 3039 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid; 3040 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode); 3041 kif->kf_un.kf_file.kf_file_size = va.va_size; 3042 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev; 3043 kif->kf_un.kf_file.kf_file_rdev_freebsd11 = 3044 kif->kf_un.kf_file.kf_file_rdev; /* truncate */ 3045 kif->kf_un.kf_file.kf_file_nlink = va.va_nlink; 3046 return (0); 3047 } 3048 3049 int 3050 vn_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size, 3051 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff, 3052 struct thread *td) 3053 { 3054 #ifdef HWPMC_HOOKS 3055 struct pmckern_map_in pkm; 3056 #endif 3057 struct mount *mp; 3058 struct vnode *vp; 3059 vm_object_t object; 3060 vm_prot_t maxprot; 3061 boolean_t writecounted; 3062 int error; 3063 3064 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \ 3065 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) 3066 /* 3067 * POSIX shared-memory objects are defined to have 3068 * kernel persistence, and are not defined to support 3069 * read(2)/write(2) -- or even open(2). Thus, we can 3070 * use MAP_ASYNC to trade on-disk coherence for speed. 3071 * The shm_open(3) library routine turns on the FPOSIXSHM 3072 * flag to request this behavior. 3073 */ 3074 if ((fp->f_flag & FPOSIXSHM) != 0) 3075 flags |= MAP_NOSYNC; 3076 #endif 3077 vp = fp->f_vnode; 3078 3079 /* 3080 * Ensure that file and memory protections are 3081 * compatible. Note that we only worry about 3082 * writability if mapping is shared; in this case, 3083 * current and max prot are dictated by the open file. 3084 * XXX use the vnode instead? Problem is: what 3085 * credentials do we use for determination? What if 3086 * proc does a setuid? 3087 */ 3088 mp = vp->v_mount; 3089 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) { 3090 maxprot = VM_PROT_NONE; 3091 if ((prot & VM_PROT_EXECUTE) != 0) 3092 return (EACCES); 3093 } else 3094 maxprot = VM_PROT_EXECUTE; 3095 if ((fp->f_flag & FREAD) != 0) 3096 maxprot |= VM_PROT_READ; 3097 else if ((prot & VM_PROT_READ) != 0) 3098 return (EACCES); 3099 3100 /* 3101 * If we are sharing potential changes via MAP_SHARED and we 3102 * are trying to get write permission although we opened it 3103 * without asking for it, bail out. 3104 */ 3105 if ((flags & MAP_SHARED) != 0) { 3106 if ((fp->f_flag & FWRITE) != 0) 3107 maxprot |= VM_PROT_WRITE; 3108 else if ((prot & VM_PROT_WRITE) != 0) 3109 return (EACCES); 3110 } else { 3111 maxprot |= VM_PROT_WRITE; 3112 cap_maxprot |= VM_PROT_WRITE; 3113 } 3114 maxprot &= cap_maxprot; 3115 3116 /* 3117 * For regular files and shared memory, POSIX requires that 3118 * the value of foff be a legitimate offset within the data 3119 * object. In particular, negative offsets are invalid. 3120 * Blocking negative offsets and overflows here avoids 3121 * possible wraparound or user-level access into reserved 3122 * ranges of the data object later. In contrast, POSIX does 3123 * not dictate how offsets are used by device drivers, so in 3124 * the case of a device mapping a negative offset is passed 3125 * on. 3126 */ 3127 if ( 3128 #ifdef _LP64 3129 size > OFF_MAX || 3130 #endif 3131 foff > OFF_MAX - size) 3132 return (EINVAL); 3133 3134 writecounted = FALSE; 3135 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags, vp, 3136 &foff, &object, &writecounted); 3137 if (error != 0) 3138 return (error); 3139 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object, 3140 foff, writecounted, td); 3141 if (error != 0) { 3142 /* 3143 * If this mapping was accounted for in the vnode's 3144 * writecount, then undo that now. 3145 */ 3146 if (writecounted) 3147 vm_pager_release_writecount(object, 0, size); 3148 vm_object_deallocate(object); 3149 } 3150 #ifdef HWPMC_HOOKS 3151 /* Inform hwpmc(4) if an executable is being mapped. */ 3152 if (PMC_HOOK_INSTALLED(PMC_FN_MMAP)) { 3153 if ((prot & VM_PROT_EXECUTE) != 0 && error == 0) { 3154 pkm.pm_file = vp; 3155 pkm.pm_address = (uintptr_t) *addr; 3156 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_MMAP, (void *) &pkm); 3157 } 3158 } 3159 #endif 3160 3161 #ifdef HWT_HOOKS 3162 if (HWT_HOOK_INSTALLED && (prot & VM_PROT_EXECUTE) != 0 && 3163 error == 0) { 3164 struct hwt_record_entry ent; 3165 char *fullpath; 3166 char *freepath; 3167 3168 if (vn_fullpath(vp, &fullpath, &freepath) == 0) { 3169 ent.fullpath = fullpath; 3170 ent.addr = (uintptr_t) *addr; 3171 ent.record_type = HWT_RECORD_MMAP; 3172 HWT_CALL_HOOK(td, HWT_MMAP, &ent); 3173 free(freepath, M_TEMP); 3174 } 3175 } 3176 #endif 3177 3178 return (error); 3179 } 3180 3181 void 3182 vn_fsid(struct vnode *vp, struct vattr *va) 3183 { 3184 fsid_t *f; 3185 3186 f = &vp->v_mount->mnt_stat.f_fsid; 3187 va->va_fsid = (uint32_t)f->val[1]; 3188 va->va_fsid <<= sizeof(f->val[1]) * NBBY; 3189 va->va_fsid += (uint32_t)f->val[0]; 3190 } 3191 3192 int 3193 vn_fsync_buf(struct vnode *vp, int waitfor) 3194 { 3195 struct buf *bp, *nbp; 3196 struct bufobj *bo; 3197 struct mount *mp; 3198 int error, maxretry; 3199 3200 error = 0; 3201 maxretry = 10000; /* large, arbitrarily chosen */ 3202 mp = NULL; 3203 if (vp->v_type == VCHR) { 3204 VI_LOCK(vp); 3205 mp = vp->v_rdev->si_mountpt; 3206 VI_UNLOCK(vp); 3207 } 3208 bo = &vp->v_bufobj; 3209 BO_LOCK(bo); 3210 loop1: 3211 /* 3212 * MARK/SCAN initialization to avoid infinite loops. 3213 */ 3214 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 3215 bp->b_vflags &= ~BV_SCANNED; 3216 bp->b_error = 0; 3217 } 3218 3219 /* 3220 * Flush all dirty buffers associated with a vnode. 3221 */ 3222 loop2: 3223 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 3224 if ((bp->b_vflags & BV_SCANNED) != 0) 3225 continue; 3226 bp->b_vflags |= BV_SCANNED; 3227 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 3228 if (waitfor != MNT_WAIT) 3229 continue; 3230 if (BUF_LOCK(bp, 3231 LK_EXCLUSIVE | LK_INTERLOCK | LK_SLEEPFAIL, 3232 BO_LOCKPTR(bo)) != 0) { 3233 BO_LOCK(bo); 3234 goto loop1; 3235 } 3236 BO_LOCK(bo); 3237 } 3238 BO_UNLOCK(bo); 3239 KASSERT(bp->b_bufobj == bo, 3240 ("bp %p wrong b_bufobj %p should be %p", 3241 bp, bp->b_bufobj, bo)); 3242 if ((bp->b_flags & B_DELWRI) == 0) 3243 panic("fsync: not dirty"); 3244 if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) { 3245 vfs_bio_awrite(bp); 3246 } else { 3247 bremfree(bp); 3248 bawrite(bp); 3249 } 3250 if (maxretry < 1000) 3251 pause("dirty", hz < 1000 ? 1 : hz / 1000); 3252 BO_LOCK(bo); 3253 goto loop2; 3254 } 3255 3256 /* 3257 * If synchronous the caller expects us to completely resolve all 3258 * dirty buffers in the system. Wait for in-progress I/O to 3259 * complete (which could include background bitmap writes), then 3260 * retry if dirty blocks still exist. 3261 */ 3262 if (waitfor == MNT_WAIT) { 3263 bufobj_wwait(bo, 0, 0); 3264 if (bo->bo_dirty.bv_cnt > 0) { 3265 /* 3266 * If we are unable to write any of these buffers 3267 * then we fail now rather than trying endlessly 3268 * to write them out. 3269 */ 3270 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 3271 if ((error = bp->b_error) != 0) 3272 break; 3273 if ((mp != NULL && mp->mnt_secondary_writes > 0) || 3274 (error == 0 && --maxretry >= 0)) 3275 goto loop1; 3276 if (error == 0) 3277 error = EAGAIN; 3278 } 3279 } 3280 BO_UNLOCK(bo); 3281 if (error != 0) 3282 vn_printf(vp, "fsync: giving up on dirty (error = %d) ", error); 3283 3284 return (error); 3285 } 3286 3287 /* 3288 * Copies a byte range from invp to outvp. Calls VOP_COPY_FILE_RANGE() 3289 * or vn_generic_copy_file_range() after rangelocking the byte ranges, 3290 * to do the actual copy. 3291 * vn_generic_copy_file_range() is factored out, so it can be called 3292 * from a VOP_COPY_FILE_RANGE() call as well, but handles vnodes from 3293 * different file systems. 3294 */ 3295 int 3296 vn_copy_file_range(struct vnode *invp, off_t *inoffp, struct vnode *outvp, 3297 off_t *outoffp, size_t *lenp, unsigned int flags, struct ucred *incred, 3298 struct ucred *outcred, struct thread *fsize_td) 3299 { 3300 struct mount *inmp, *outmp; 3301 struct vnode *invpl, *outvpl; 3302 int error; 3303 size_t len; 3304 uint64_t uval; 3305 3306 invpl = outvpl = NULL; 3307 len = *lenp; 3308 *lenp = 0; /* For error returns. */ 3309 error = 0; 3310 3311 /* Do some sanity checks on the arguments. */ 3312 if (invp->v_type == VDIR || outvp->v_type == VDIR) 3313 error = EISDIR; 3314 else if (*inoffp < 0 || *outoffp < 0 || 3315 invp->v_type != VREG || outvp->v_type != VREG) 3316 error = EINVAL; 3317 if (error != 0) 3318 goto out; 3319 3320 /* Ensure offset + len does not wrap around. */ 3321 uval = *inoffp; 3322 uval += len; 3323 if (uval > INT64_MAX) 3324 len = INT64_MAX - *inoffp; 3325 uval = *outoffp; 3326 uval += len; 3327 if (uval > INT64_MAX) 3328 len = INT64_MAX - *outoffp; 3329 if (len == 0) 3330 goto out; 3331 3332 error = VOP_GETLOWVNODE(invp, &invpl, FREAD); 3333 if (error != 0) 3334 goto out; 3335 error = VOP_GETLOWVNODE(outvp, &outvpl, FWRITE); 3336 if (error != 0) 3337 goto out1; 3338 3339 inmp = invpl->v_mount; 3340 outmp = outvpl->v_mount; 3341 if (inmp == NULL || outmp == NULL) 3342 goto out2; 3343 3344 for (;;) { 3345 error = vfs_busy(inmp, 0); 3346 if (error != 0) 3347 goto out2; 3348 if (inmp == outmp) 3349 break; 3350 error = vfs_busy(outmp, MBF_NOWAIT); 3351 if (error != 0) { 3352 vfs_unbusy(inmp); 3353 error = vfs_busy(outmp, 0); 3354 if (error == 0) { 3355 vfs_unbusy(outmp); 3356 continue; 3357 } 3358 goto out2; 3359 } 3360 break; 3361 } 3362 3363 /* 3364 * If the two vnodes are for the same file system type, call 3365 * VOP_COPY_FILE_RANGE(), otherwise call vn_generic_copy_file_range() 3366 * which can handle copies across multiple file system types. 3367 */ 3368 *lenp = len; 3369 if (inmp == outmp || inmp->mnt_vfc == outmp->mnt_vfc) 3370 error = VOP_COPY_FILE_RANGE(invpl, inoffp, outvpl, outoffp, 3371 lenp, flags, incred, outcred, fsize_td); 3372 else 3373 error = ENOSYS; 3374 if (error == ENOSYS) 3375 error = vn_generic_copy_file_range(invpl, inoffp, outvpl, 3376 outoffp, lenp, flags, incred, outcred, fsize_td); 3377 vfs_unbusy(outmp); 3378 if (inmp != outmp) 3379 vfs_unbusy(inmp); 3380 out2: 3381 if (outvpl != NULL) 3382 vrele(outvpl); 3383 out1: 3384 if (invpl != NULL) 3385 vrele(invpl); 3386 out: 3387 return (error); 3388 } 3389 3390 /* 3391 * Test len bytes of data starting at dat for all bytes == 0. 3392 * Return true if all bytes are zero, false otherwise. 3393 * Expects dat to be well aligned. 3394 */ 3395 static bool 3396 mem_iszero(void *dat, int len) 3397 { 3398 int i; 3399 const u_int *p; 3400 const char *cp; 3401 3402 for (p = dat; len > 0; len -= sizeof(*p), p++) { 3403 if (len >= sizeof(*p)) { 3404 if (*p != 0) 3405 return (false); 3406 } else { 3407 cp = (const char *)p; 3408 for (i = 0; i < len; i++, cp++) 3409 if (*cp != '\0') 3410 return (false); 3411 } 3412 } 3413 return (true); 3414 } 3415 3416 /* 3417 * Look for a hole in the output file and, if found, adjust *outoffp 3418 * and *xferp to skip past the hole. 3419 * *xferp is the entire hole length to be written and xfer2 is how many bytes 3420 * to be written as 0's upon return. 3421 */ 3422 static off_t 3423 vn_skip_hole(struct vnode *outvp, off_t xfer2, off_t *outoffp, off_t *xferp, 3424 off_t *dataoffp, off_t *holeoffp, struct ucred *cred) 3425 { 3426 int error; 3427 off_t delta; 3428 3429 if (*holeoffp == 0 || *holeoffp <= *outoffp) { 3430 *dataoffp = *outoffp; 3431 error = VOP_IOCTL(outvp, FIOSEEKDATA, dataoffp, 0, cred, 3432 curthread); 3433 if (error == 0) { 3434 *holeoffp = *dataoffp; 3435 error = VOP_IOCTL(outvp, FIOSEEKHOLE, holeoffp, 0, cred, 3436 curthread); 3437 } 3438 if (error != 0 || *holeoffp == *dataoffp) { 3439 /* 3440 * Since outvp is unlocked, it may be possible for 3441 * another thread to do a truncate(), lseek(), write() 3442 * creating a hole at startoff between the above 3443 * VOP_IOCTL() calls, if the other thread does not do 3444 * rangelocking. 3445 * If that happens, *holeoffp == *dataoffp and finding 3446 * the hole has failed, so disable vn_skip_hole(). 3447 */ 3448 *holeoffp = -1; /* Disable use of vn_skip_hole(). */ 3449 return (xfer2); 3450 } 3451 KASSERT(*dataoffp >= *outoffp, 3452 ("vn_skip_hole: dataoff=%jd < outoff=%jd", 3453 (intmax_t)*dataoffp, (intmax_t)*outoffp)); 3454 KASSERT(*holeoffp > *dataoffp, 3455 ("vn_skip_hole: holeoff=%jd <= dataoff=%jd", 3456 (intmax_t)*holeoffp, (intmax_t)*dataoffp)); 3457 } 3458 3459 /* 3460 * If there is a hole before the data starts, advance *outoffp and 3461 * *xferp past the hole. 3462 */ 3463 if (*dataoffp > *outoffp) { 3464 delta = *dataoffp - *outoffp; 3465 if (delta >= *xferp) { 3466 /* Entire *xferp is a hole. */ 3467 *outoffp += *xferp; 3468 *xferp = 0; 3469 return (0); 3470 } 3471 *xferp -= delta; 3472 *outoffp += delta; 3473 xfer2 = MIN(xfer2, *xferp); 3474 } 3475 3476 /* 3477 * If a hole starts before the end of this xfer2, reduce this xfer2 so 3478 * that the write ends at the start of the hole. 3479 * *holeoffp should always be greater than *outoffp, but for the 3480 * non-INVARIANTS case, check this to make sure xfer2 remains a sane 3481 * value. 3482 */ 3483 if (*holeoffp > *outoffp && *holeoffp < *outoffp + xfer2) 3484 xfer2 = *holeoffp - *outoffp; 3485 return (xfer2); 3486 } 3487 3488 /* 3489 * Write an xfer sized chunk to outvp in blksize blocks from dat. 3490 * dat is a maximum of blksize in length and can be written repeatedly in 3491 * the chunk. 3492 * If growfile == true, just grow the file via vn_truncate_locked() instead 3493 * of doing actual writes. 3494 * If checkhole == true, a hole is being punched, so skip over any hole 3495 * already in the output file. 3496 */ 3497 static int 3498 vn_write_outvp(struct vnode *outvp, char *dat, off_t outoff, off_t xfer, 3499 u_long blksize, bool growfile, bool checkhole, struct ucred *cred) 3500 { 3501 struct mount *mp; 3502 off_t dataoff, holeoff, xfer2; 3503 int error; 3504 3505 /* 3506 * Loop around doing writes of blksize until write has been completed. 3507 * Lock/unlock on each loop iteration so that a bwillwrite() can be 3508 * done for each iteration, since the xfer argument can be very 3509 * large if there is a large hole to punch in the output file. 3510 */ 3511 error = 0; 3512 holeoff = 0; 3513 do { 3514 xfer2 = MIN(xfer, blksize); 3515 if (checkhole) { 3516 /* 3517 * Punching a hole. Skip writing if there is 3518 * already a hole in the output file. 3519 */ 3520 xfer2 = vn_skip_hole(outvp, xfer2, &outoff, &xfer, 3521 &dataoff, &holeoff, cred); 3522 if (xfer == 0) 3523 break; 3524 if (holeoff < 0) 3525 checkhole = false; 3526 KASSERT(xfer2 > 0, ("vn_write_outvp: xfer2=%jd", 3527 (intmax_t)xfer2)); 3528 } 3529 bwillwrite(); 3530 mp = NULL; 3531 error = vn_start_write(outvp, &mp, V_WAIT); 3532 if (error != 0) 3533 break; 3534 if (growfile) { 3535 error = vn_lock(outvp, LK_EXCLUSIVE); 3536 if (error == 0) { 3537 error = vn_truncate_locked(outvp, outoff + xfer, 3538 false, cred); 3539 VOP_UNLOCK(outvp); 3540 } 3541 } else { 3542 error = vn_lock(outvp, vn_lktype_write(mp, outvp)); 3543 if (error == 0) { 3544 error = vn_rdwr(UIO_WRITE, outvp, dat, xfer2, 3545 outoff, UIO_SYSSPACE, IO_NODELOCKED, 3546 curthread->td_ucred, cred, NULL, curthread); 3547 outoff += xfer2; 3548 xfer -= xfer2; 3549 VOP_UNLOCK(outvp); 3550 } 3551 } 3552 if (mp != NULL) 3553 vn_finished_write(mp); 3554 } while (!growfile && xfer > 0 && error == 0); 3555 return (error); 3556 } 3557 3558 /* 3559 * Copy a byte range of one file to another. This function can handle the 3560 * case where invp and outvp are on different file systems. 3561 * It can also be called by a VOP_COPY_FILE_RANGE() to do the work, if there 3562 * is no better file system specific way to do it. 3563 */ 3564 int 3565 vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp, 3566 struct vnode *outvp, off_t *outoffp, size_t *lenp, unsigned int flags, 3567 struct ucred *incred, struct ucred *outcred, struct thread *fsize_td) 3568 { 3569 struct vattr inva; 3570 struct mount *mp; 3571 off_t startoff, endoff, xfer, xfer2; 3572 u_long blksize; 3573 int error, interrupted; 3574 bool cantseek, readzeros, eof, first, lastblock, holetoeof, sparse; 3575 ssize_t aresid, r = 0; 3576 size_t copylen, len, savlen; 3577 off_t outsize; 3578 char *dat; 3579 long holein, holeout; 3580 struct timespec curts, endts; 3581 3582 holein = holeout = 0; 3583 savlen = len = *lenp; 3584 error = 0; 3585 interrupted = 0; 3586 dat = NULL; 3587 3588 if ((flags & COPY_FILE_RANGE_CLONE) != 0) { 3589 error = EOPNOTSUPP; 3590 goto out; 3591 } 3592 3593 error = vn_lock(invp, LK_SHARED); 3594 if (error != 0) 3595 goto out; 3596 if (VOP_PATHCONF(invp, _PC_MIN_HOLE_SIZE, &holein) != 0) 3597 holein = 0; 3598 error = VOP_GETATTR(invp, &inva, incred); 3599 if (error == 0 && inva.va_size > OFF_MAX) 3600 error = EFBIG; 3601 VOP_UNLOCK(invp); 3602 if (error != 0) 3603 goto out; 3604 3605 /* 3606 * Use va_bytes >= va_size as a hint that the file does not have 3607 * sufficient holes to justify the overhead of doing FIOSEEKHOLE. 3608 * This hint does not work well for file systems doing compression 3609 * and may fail when allocations for extended attributes increases 3610 * the value of va_bytes to >= va_size. 3611 */ 3612 sparse = true; 3613 if (holein != 0 && inva.va_bytes >= inva.va_size) { 3614 holein = 0; 3615 sparse = false; 3616 } 3617 3618 mp = NULL; 3619 error = vn_start_write(outvp, &mp, V_WAIT); 3620 if (error == 0) 3621 error = vn_lock(outvp, LK_EXCLUSIVE); 3622 if (error == 0) { 3623 /* 3624 * If fsize_td != NULL, do a vn_rlimit_fsizex() call, 3625 * now that outvp is locked. 3626 */ 3627 if (fsize_td != NULL) { 3628 struct uio io; 3629 3630 io.uio_offset = *outoffp; 3631 io.uio_resid = len; 3632 error = vn_rlimit_fsizex(outvp, &io, 0, &r, fsize_td); 3633 len = savlen = io.uio_resid; 3634 /* 3635 * No need to call vn_rlimit_fsizex_res before return, 3636 * since the uio is local. 3637 */ 3638 } 3639 if (VOP_PATHCONF(outvp, _PC_MIN_HOLE_SIZE, &holeout) != 0) 3640 holeout = 0; 3641 /* 3642 * Holes that are past EOF do not need to be written as a block 3643 * of zero bytes. So, truncate the output file as far as 3644 * possible and then use size to decide if writing 0 3645 * bytes is necessary in the loop below. 3646 */ 3647 if (error == 0) 3648 error = vn_getsize_locked(outvp, &outsize, outcred); 3649 if (error == 0 && outsize > *outoffp && 3650 *outoffp <= OFF_MAX - len && outsize <= *outoffp + len && 3651 *inoffp < inva.va_size && 3652 *outoffp <= OFF_MAX - (inva.va_size - *inoffp) && 3653 outsize <= *outoffp + (inva.va_size - *inoffp)) { 3654 #ifdef MAC 3655 error = mac_vnode_check_write(curthread->td_ucred, 3656 outcred, outvp); 3657 if (error == 0) 3658 #endif 3659 error = vn_truncate_locked(outvp, *outoffp, 3660 false, outcred); 3661 if (error == 0) 3662 outsize = *outoffp; 3663 } 3664 VOP_UNLOCK(outvp); 3665 } 3666 if (mp != NULL) 3667 vn_finished_write(mp); 3668 if (error != 0) 3669 goto out; 3670 3671 if (sparse && holein == 0 && holeout > 0) { 3672 /* 3673 * For this special case, the input data will be scanned 3674 * for blocks of all 0 bytes. For these blocks, the 3675 * write can be skipped for the output file to create 3676 * an unallocated region. 3677 * Therefore, use the appropriate size for the output file. 3678 */ 3679 blksize = holeout; 3680 if (blksize <= 512) { 3681 /* 3682 * Use f_iosize, since ZFS reports a _PC_MIN_HOLE_SIZE 3683 * of 512, although it actually only creates 3684 * unallocated regions for blocks >= f_iosize. 3685 */ 3686 blksize = outvp->v_mount->mnt_stat.f_iosize; 3687 } 3688 } else { 3689 /* 3690 * Use the larger of the two f_iosize values. If they are 3691 * not the same size, one will normally be an exact multiple of 3692 * the other, since they are both likely to be a power of 2. 3693 */ 3694 blksize = MAX(invp->v_mount->mnt_stat.f_iosize, 3695 outvp->v_mount->mnt_stat.f_iosize); 3696 } 3697 3698 /* Clip to sane limits. */ 3699 if (blksize < 4096) 3700 blksize = 4096; 3701 else if (blksize > maxphys) 3702 blksize = maxphys; 3703 dat = malloc(blksize, M_TEMP, M_WAITOK); 3704 3705 /* 3706 * If VOP_IOCTL(FIOSEEKHOLE) works for invp, use it and FIOSEEKDATA 3707 * to find holes. Otherwise, just scan the read block for all 0s 3708 * in the inner loop where the data copying is done. 3709 * Note that some file systems such as NFSv3, NFSv4.0 and NFSv4.1 may 3710 * support holes on the server, but do not support FIOSEEKHOLE. 3711 * The kernel flag COPY_FILE_RANGE_TIMEO1SEC is used to indicate 3712 * that this function should return after 1second with a partial 3713 * completion. 3714 */ 3715 if ((flags & COPY_FILE_RANGE_TIMEO1SEC) != 0) { 3716 getnanouptime(&endts); 3717 endts.tv_sec++; 3718 } else 3719 timespecclear(&endts); 3720 first = true; 3721 holetoeof = eof = false; 3722 while (len > 0 && error == 0 && !eof && interrupted == 0) { 3723 endoff = 0; /* To shut up compilers. */ 3724 cantseek = true; 3725 startoff = *inoffp; 3726 copylen = len; 3727 3728 /* 3729 * Find the next data area. If there is just a hole to EOF, 3730 * FIOSEEKDATA should fail with ENXIO. 3731 * (I do not know if any file system will report a hole to 3732 * EOF via FIOSEEKHOLE, but I am pretty sure FIOSEEKDATA 3733 * will fail for those file systems.) 3734 * 3735 * For input files that don't support FIOSEEKDATA/FIOSEEKHOLE, 3736 * the code just falls through to the inner copy loop. 3737 */ 3738 error = EINVAL; 3739 if (holein > 0) { 3740 error = VOP_IOCTL(invp, FIOSEEKDATA, &startoff, 0, 3741 incred, curthread); 3742 if (error == ENXIO) { 3743 startoff = endoff = inva.va_size; 3744 eof = holetoeof = true; 3745 error = 0; 3746 } 3747 } 3748 if (error == 0 && !holetoeof) { 3749 endoff = startoff; 3750 error = VOP_IOCTL(invp, FIOSEEKHOLE, &endoff, 0, 3751 incred, curthread); 3752 /* 3753 * Since invp is unlocked, it may be possible for 3754 * another thread to do a truncate(), lseek(), write() 3755 * creating a hole at startoff between the above 3756 * VOP_IOCTL() calls, if the other thread does not do 3757 * rangelocking. 3758 * If that happens, startoff == endoff and finding 3759 * the hole has failed, so set an error. 3760 */ 3761 if (error == 0 && startoff == endoff) 3762 error = EINVAL; /* Any error. Reset to 0. */ 3763 } 3764 if (error == 0) { 3765 if (startoff > *inoffp) { 3766 /* Found hole before data block. */ 3767 xfer = MIN(startoff - *inoffp, len); 3768 if (*outoffp < outsize) { 3769 /* Must write 0s to punch hole. */ 3770 xfer2 = MIN(outsize - *outoffp, 3771 xfer); 3772 memset(dat, 0, MIN(xfer2, blksize)); 3773 error = vn_write_outvp(outvp, dat, 3774 *outoffp, xfer2, blksize, false, 3775 holeout > 0, outcred); 3776 } 3777 3778 if (error == 0 && *outoffp + xfer > 3779 outsize && (xfer == len || holetoeof)) { 3780 /* Grow output file (hole at end). */ 3781 error = vn_write_outvp(outvp, dat, 3782 *outoffp, xfer, blksize, true, 3783 false, outcred); 3784 } 3785 if (error == 0) { 3786 *inoffp += xfer; 3787 *outoffp += xfer; 3788 len -= xfer; 3789 if (len < savlen) { 3790 interrupted = sig_intr(); 3791 if (timespecisset(&endts) && 3792 interrupted == 0) { 3793 getnanouptime(&curts); 3794 if (timespeccmp(&curts, 3795 &endts, >=)) 3796 interrupted = 3797 EINTR; 3798 } 3799 } 3800 } 3801 } 3802 copylen = MIN(len, endoff - startoff); 3803 cantseek = false; 3804 } else { 3805 cantseek = true; 3806 if (!sparse) 3807 cantseek = false; 3808 startoff = *inoffp; 3809 copylen = len; 3810 error = 0; 3811 } 3812 3813 xfer = blksize; 3814 if (cantseek) { 3815 /* 3816 * Set first xfer to end at a block boundary, so that 3817 * holes are more likely detected in the loop below via 3818 * the for all bytes 0 method. 3819 */ 3820 xfer -= (*inoffp % blksize); 3821 } 3822 3823 /* 3824 * Loop copying the data block. If this was our first attempt 3825 * to copy anything, allow a zero-length block so that the VOPs 3826 * get a chance to update metadata, specifically the atime. 3827 */ 3828 while (error == 0 && ((copylen > 0 && !eof) || first) && 3829 interrupted == 0) { 3830 if (copylen < xfer) 3831 xfer = copylen; 3832 first = false; 3833 error = vn_lock(invp, LK_SHARED); 3834 if (error != 0) 3835 goto out; 3836 error = vn_rdwr(UIO_READ, invp, dat, xfer, 3837 startoff, UIO_SYSSPACE, IO_NODELOCKED, 3838 curthread->td_ucred, incred, &aresid, 3839 curthread); 3840 VOP_UNLOCK(invp); 3841 lastblock = false; 3842 if (error == 0 && (xfer == 0 || aresid > 0)) { 3843 /* Stop the copy at EOF on the input file. */ 3844 xfer -= aresid; 3845 eof = true; 3846 lastblock = true; 3847 } 3848 if (error == 0) { 3849 /* 3850 * Skip the write for holes past the initial EOF 3851 * of the output file, unless this is the last 3852 * write of the output file at EOF. 3853 */ 3854 readzeros = cantseek ? mem_iszero(dat, xfer) : 3855 false; 3856 if (xfer == len) 3857 lastblock = true; 3858 if (!cantseek || *outoffp < outsize || 3859 lastblock || !readzeros) 3860 error = vn_write_outvp(outvp, dat, 3861 *outoffp, xfer, blksize, 3862 readzeros && lastblock && 3863 *outoffp >= outsize, false, 3864 outcred); 3865 if (error == 0) { 3866 *inoffp += xfer; 3867 startoff += xfer; 3868 *outoffp += xfer; 3869 copylen -= xfer; 3870 len -= xfer; 3871 if (len < savlen) { 3872 interrupted = sig_intr(); 3873 if (timespecisset(&endts) && 3874 interrupted == 0) { 3875 getnanouptime(&curts); 3876 if (timespeccmp(&curts, 3877 &endts, >=)) 3878 interrupted = 3879 EINTR; 3880 } 3881 } 3882 } 3883 } 3884 xfer = blksize; 3885 } 3886 } 3887 out: 3888 *lenp = savlen - len; 3889 free(dat, M_TEMP); 3890 return (error); 3891 } 3892 3893 static int 3894 vn_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td) 3895 { 3896 struct mount *mp; 3897 struct vnode *vp; 3898 off_t olen, ooffset; 3899 int error; 3900 #ifdef AUDIT 3901 int audited_vnode1 = 0; 3902 #endif 3903 3904 vp = fp->f_vnode; 3905 if (vp->v_type != VREG) 3906 return (ENODEV); 3907 3908 /* Allocating blocks may take a long time, so iterate. */ 3909 for (;;) { 3910 olen = len; 3911 ooffset = offset; 3912 3913 bwillwrite(); 3914 mp = NULL; 3915 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH); 3916 if (error != 0) 3917 break; 3918 error = vn_lock(vp, LK_EXCLUSIVE); 3919 if (error != 0) { 3920 vn_finished_write(mp); 3921 break; 3922 } 3923 #ifdef AUDIT 3924 if (!audited_vnode1) { 3925 AUDIT_ARG_VNODE1(vp); 3926 audited_vnode1 = 1; 3927 } 3928 #endif 3929 #ifdef MAC 3930 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp); 3931 if (error == 0) 3932 #endif 3933 error = VOP_ALLOCATE(vp, &offset, &len, 0, 3934 td->td_ucred); 3935 VOP_UNLOCK(vp); 3936 vn_finished_write(mp); 3937 3938 if (olen + ooffset != offset + len) { 3939 panic("offset + len changed from %jx/%jx to %jx/%jx", 3940 ooffset, olen, offset, len); 3941 } 3942 if (error != 0 || len == 0) 3943 break; 3944 KASSERT(olen > len, ("Iteration did not make progress?")); 3945 maybe_yield(); 3946 } 3947 3948 return (error); 3949 } 3950 3951 static int 3952 vn_deallocate_impl(struct vnode *vp, off_t *offset, off_t *length, int flags, 3953 int ioflag, struct ucred *cred, struct ucred *active_cred, 3954 struct ucred *file_cred) 3955 { 3956 struct mount *mp; 3957 void *rl_cookie; 3958 off_t off, len; 3959 int error; 3960 #ifdef AUDIT 3961 bool audited_vnode1 = false; 3962 #endif 3963 3964 rl_cookie = NULL; 3965 error = 0; 3966 mp = NULL; 3967 off = *offset; 3968 len = *length; 3969 3970 if ((ioflag & (IO_NODELOCKED | IO_RANGELOCKED)) == 0) 3971 rl_cookie = vn_rangelock_wlock(vp, off, off + len); 3972 while (len > 0 && error == 0) { 3973 /* 3974 * Try to deallocate the longest range in one pass. 3975 * In case a pass takes too long to be executed, it returns 3976 * partial result. The residue will be proceeded in the next 3977 * pass. 3978 */ 3979 3980 if ((ioflag & IO_NODELOCKED) == 0) { 3981 bwillwrite(); 3982 if ((error = vn_start_write(vp, &mp, 3983 V_WAIT | V_PCATCH)) != 0) 3984 goto out; 3985 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY); 3986 } 3987 #ifdef AUDIT 3988 if (!audited_vnode1) { 3989 AUDIT_ARG_VNODE1(vp); 3990 audited_vnode1 = true; 3991 } 3992 #endif 3993 3994 #ifdef MAC 3995 if ((ioflag & IO_NOMACCHECK) == 0) 3996 error = mac_vnode_check_write(active_cred, file_cred, 3997 vp); 3998 #endif 3999 if (error == 0) 4000 error = VOP_DEALLOCATE(vp, &off, &len, flags, ioflag, 4001 cred); 4002 4003 if ((ioflag & IO_NODELOCKED) == 0) { 4004 VOP_UNLOCK(vp); 4005 if (mp != NULL) { 4006 vn_finished_write(mp); 4007 mp = NULL; 4008 } 4009 } 4010 if (error == 0 && len != 0) 4011 maybe_yield(); 4012 } 4013 out: 4014 if (rl_cookie != NULL) 4015 vn_rangelock_unlock(vp, rl_cookie); 4016 *offset = off; 4017 *length = len; 4018 return (error); 4019 } 4020 4021 /* 4022 * This function is supposed to be used in the situations where the deallocation 4023 * is not triggered by a user request. 4024 */ 4025 int 4026 vn_deallocate(struct vnode *vp, off_t *offset, off_t *length, int flags, 4027 int ioflag, struct ucred *active_cred, struct ucred *file_cred) 4028 { 4029 struct ucred *cred; 4030 4031 if (*offset < 0 || *length <= 0 || *length > OFF_MAX - *offset || 4032 flags != 0) 4033 return (EINVAL); 4034 if (vp->v_type != VREG) 4035 return (ENODEV); 4036 4037 cred = file_cred != NOCRED ? file_cred : active_cred; 4038 return (vn_deallocate_impl(vp, offset, length, flags, ioflag, cred, 4039 active_cred, file_cred)); 4040 } 4041 4042 static int 4043 vn_fspacectl(struct file *fp, int cmd, off_t *offset, off_t *length, int flags, 4044 struct ucred *active_cred, struct thread *td) 4045 { 4046 int error; 4047 struct vnode *vp; 4048 int ioflag; 4049 4050 KASSERT(cmd == SPACECTL_DEALLOC, ("vn_fspacectl: Invalid cmd")); 4051 KASSERT((flags & ~SPACECTL_F_SUPPORTED) == 0, 4052 ("vn_fspacectl: non-zero flags")); 4053 KASSERT(*offset >= 0 && *length > 0 && *length <= OFF_MAX - *offset, 4054 ("vn_fspacectl: offset/length overflow or underflow")); 4055 vp = fp->f_vnode; 4056 4057 if (vp->v_type != VREG) 4058 return (ENODEV); 4059 4060 ioflag = get_write_ioflag(fp); 4061 4062 switch (cmd) { 4063 case SPACECTL_DEALLOC: 4064 error = vn_deallocate_impl(vp, offset, length, flags, ioflag, 4065 active_cred, active_cred, fp->f_cred); 4066 break; 4067 default: 4068 panic("vn_fspacectl: unknown cmd %d", cmd); 4069 } 4070 4071 return (error); 4072 } 4073 4074 /* 4075 * Keep this assert as long as sizeof(struct dirent) is used as the maximum 4076 * entry size. 4077 */ 4078 _Static_assert(_GENERIC_MAXDIRSIZ == sizeof(struct dirent), 4079 "'struct dirent' size must be a multiple of its alignment " 4080 "(see _GENERIC_DIRLEN())"); 4081 4082 /* 4083 * Returns successive directory entries through some caller's provided buffer. 4084 * 4085 * This function automatically refills the provided buffer with calls to 4086 * VOP_READDIR() (after MAC permission checks). 4087 * 4088 * 'td' is used for credentials and passed to uiomove(). 'dirbuf' is the 4089 * caller's buffer to fill and 'dirbuflen' its allocated size. 'dirbuf' must 4090 * be properly aligned to access 'struct dirent' structures and 'dirbuflen' 4091 * must be greater than GENERIC_MAXDIRSIZ to avoid VOP_READDIR() returning 4092 * EINVAL (the latter is not a strong guarantee (yet); but EINVAL will always 4093 * be returned if this requirement is not verified). '*dpp' points to the 4094 * current directory entry in the buffer and '*len' contains the remaining 4095 * valid bytes in 'dirbuf' after 'dpp' (including the pointed entry). 4096 * 4097 * At first call (or when restarting the read), '*len' must have been set to 0, 4098 * '*off' to 0 (or any valid start offset) and '*eofflag' to 0. There are no 4099 * more entries as soon as '*len' is 0 after a call that returned 0. Calling 4100 * again this function after such a condition is considered an error and EINVAL 4101 * will be returned. Other possible error codes are those of VOP_READDIR(), 4102 * EINTEGRITY if the returned entries do not pass coherency tests, or EINVAL 4103 * (bad call). All errors are unrecoverable, i.e., the state ('*len', '*off' 4104 * and '*eofflag') must be re-initialized before a subsequent call. On error 4105 * or at end of directory, '*dpp' is reset to NULL. 4106 * 4107 * '*len', '*off' and '*eofflag' are internal state the caller should not 4108 * tamper with except as explained above. '*off' is the next directory offset 4109 * to read from to refill the buffer. '*eofflag' is set to 0 or 1 by the last 4110 * internal call to VOP_READDIR() that returned without error, indicating 4111 * whether it reached the end of the directory, and to 2 by this function after 4112 * all entries have been read. 4113 */ 4114 int 4115 vn_dir_next_dirent(struct vnode *vp, struct thread *td, 4116 char *dirbuf, size_t dirbuflen, 4117 struct dirent **dpp, size_t *len, off_t *off, int *eofflag) 4118 { 4119 struct dirent *dp = NULL; 4120 int reclen; 4121 int error; 4122 struct uio uio; 4123 struct iovec iov; 4124 4125 ASSERT_VOP_LOCKED(vp, "vnode not locked"); 4126 VNASSERT(vp->v_type == VDIR, vp, ("vnode is not a directory")); 4127 MPASS2((uintptr_t)dirbuf < (uintptr_t)dirbuf + dirbuflen, 4128 "Address space overflow"); 4129 4130 if (__predict_false(dirbuflen < GENERIC_MAXDIRSIZ)) { 4131 /* Don't take any chances in this case */ 4132 error = EINVAL; 4133 goto out; 4134 } 4135 4136 if (*len != 0) { 4137 dp = *dpp; 4138 4139 /* 4140 * The caller continued to call us after an error (we set dp to 4141 * NULL in a previous iteration). Bail out right now. 4142 */ 4143 if (__predict_false(dp == NULL)) 4144 return (EINVAL); 4145 4146 MPASS(*len <= dirbuflen); 4147 MPASS2((uintptr_t)dirbuf <= (uintptr_t)dp && 4148 (uintptr_t)dp + *len <= (uintptr_t)dirbuf + dirbuflen, 4149 "Filled range not inside buffer"); 4150 4151 reclen = dp->d_reclen; 4152 if (reclen >= *len) { 4153 /* End of buffer reached */ 4154 *len = 0; 4155 } else { 4156 dp = (struct dirent *)((char *)dp + reclen); 4157 *len -= reclen; 4158 } 4159 } 4160 4161 if (*len == 0) { 4162 dp = NULL; 4163 4164 /* Have to refill. */ 4165 switch (*eofflag) { 4166 case 0: 4167 break; 4168 4169 case 1: 4170 /* Nothing more to read. */ 4171 *eofflag = 2; /* Remember the caller reached EOF. */ 4172 goto success; 4173 4174 default: 4175 /* The caller didn't test for EOF. */ 4176 error = EINVAL; 4177 goto out; 4178 } 4179 4180 iov.iov_base = dirbuf; 4181 iov.iov_len = dirbuflen; 4182 4183 uio.uio_iov = &iov; 4184 uio.uio_iovcnt = 1; 4185 uio.uio_offset = *off; 4186 uio.uio_resid = dirbuflen; 4187 uio.uio_segflg = UIO_SYSSPACE; 4188 uio.uio_rw = UIO_READ; 4189 uio.uio_td = td; 4190 4191 #ifdef MAC 4192 error = mac_vnode_check_readdir(td->td_ucred, vp); 4193 if (error == 0) 4194 #endif 4195 error = VOP_READDIR(vp, &uio, td->td_ucred, eofflag, 4196 NULL, NULL); 4197 if (error != 0) 4198 goto out; 4199 4200 *len = dirbuflen - uio.uio_resid; 4201 *off = uio.uio_offset; 4202 4203 if (*len == 0) { 4204 /* Sanity check on INVARIANTS. */ 4205 MPASS(*eofflag != 0); 4206 *eofflag = 1; 4207 goto success; 4208 } 4209 4210 /* 4211 * Normalize the flag returned by VOP_READDIR(), since we use 2 4212 * as a sentinel value. 4213 */ 4214 if (*eofflag != 0) 4215 *eofflag = 1; 4216 4217 dp = (struct dirent *)dirbuf; 4218 } 4219 4220 if (__predict_false(*len < GENERIC_MINDIRSIZ || 4221 dp->d_reclen < GENERIC_MINDIRSIZ)) { 4222 error = EINTEGRITY; 4223 dp = NULL; 4224 goto out; 4225 } 4226 4227 success: 4228 error = 0; 4229 out: 4230 *dpp = dp; 4231 return (error); 4232 } 4233 4234 /* 4235 * Checks whether a directory is empty or not. 4236 * 4237 * If the directory is empty, returns 0, and if it is not, ENOTEMPTY. Other 4238 * values are genuine errors preventing the check. 4239 */ 4240 int 4241 vn_dir_check_empty(struct vnode *vp) 4242 { 4243 struct thread *const td = curthread; 4244 char *dirbuf; 4245 size_t dirbuflen, len; 4246 off_t off; 4247 int eofflag, error; 4248 struct dirent *dp; 4249 struct vattr va; 4250 4251 ASSERT_VOP_LOCKED(vp, "vfs_emptydir"); 4252 VNPASS(vp->v_type == VDIR, vp); 4253 4254 error = VOP_GETATTR(vp, &va, td->td_ucred); 4255 if (error != 0) 4256 return (error); 4257 4258 dirbuflen = max(DEV_BSIZE, GENERIC_MAXDIRSIZ); 4259 if (dirbuflen < va.va_blocksize) 4260 dirbuflen = va.va_blocksize; 4261 dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK); 4262 4263 len = 0; 4264 off = 0; 4265 eofflag = 0; 4266 4267 for (;;) { 4268 error = vn_dir_next_dirent(vp, td, dirbuf, dirbuflen, 4269 &dp, &len, &off, &eofflag); 4270 if (error != 0) 4271 goto end; 4272 4273 if (len == 0) { 4274 /* EOF */ 4275 error = 0; 4276 goto end; 4277 } 4278 4279 /* 4280 * Skip whiteouts. Unionfs operates on filesystems only and 4281 * not on hierarchies, so these whiteouts would be shadowed on 4282 * the system hierarchy but not for a union using the 4283 * filesystem of their directories as the upper layer. 4284 * Additionally, unionfs currently transparently exposes 4285 * union-specific metadata of its upper layer, meaning that 4286 * whiteouts can be seen through the union view in empty 4287 * directories. Taking into account these whiteouts would then 4288 * prevent mounting another filesystem on such effectively 4289 * empty directories. 4290 */ 4291 if (dp->d_type == DT_WHT) 4292 continue; 4293 4294 /* 4295 * Any file in the directory which is not '.' or '..' indicates 4296 * the directory is not empty. 4297 */ 4298 switch (dp->d_namlen) { 4299 case 2: 4300 if (dp->d_name[1] != '.') { 4301 /* Can't be '..' (nor '.') */ 4302 error = ENOTEMPTY; 4303 goto end; 4304 } 4305 /* FALLTHROUGH */ 4306 case 1: 4307 if (dp->d_name[0] != '.') { 4308 /* Can't be '..' nor '.' */ 4309 error = ENOTEMPTY; 4310 goto end; 4311 } 4312 break; 4313 4314 default: 4315 error = ENOTEMPTY; 4316 goto end; 4317 } 4318 } 4319 4320 end: 4321 free(dirbuf, M_TEMP); 4322 return (error); 4323 } 4324 4325 4326 static u_long vn_lock_pair_pause_cnt; 4327 SYSCTL_ULONG(_debug, OID_AUTO, vn_lock_pair_pause, CTLFLAG_RD, 4328 &vn_lock_pair_pause_cnt, 0, 4329 "Count of vn_lock_pair deadlocks"); 4330 4331 u_int vn_lock_pair_pause_max; 4332 SYSCTL_UINT(_debug, OID_AUTO, vn_lock_pair_pause_max, CTLFLAG_RW, 4333 &vn_lock_pair_pause_max, 0, 4334 "Max ticks for vn_lock_pair deadlock avoidance sleep"); 4335 4336 static void 4337 vn_lock_pair_pause(const char *wmesg) 4338 { 4339 atomic_add_long(&vn_lock_pair_pause_cnt, 1); 4340 pause(wmesg, prng32_bounded(vn_lock_pair_pause_max)); 4341 } 4342 4343 /* 4344 * Lock pair of (possibly same) vnodes vp1, vp2, avoiding lock order 4345 * reversal. vp1_locked indicates whether vp1 is locked; if not, vp1 4346 * must be unlocked. Same for vp2 and vp2_locked. One of the vnodes 4347 * can be NULL. 4348 * 4349 * The function returns with both vnodes exclusively or shared locked, 4350 * according to corresponding lkflags, and guarantees that it does not 4351 * create lock order reversal with other threads during its execution. 4352 * Both vnodes could be unlocked temporary (and reclaimed). 4353 * 4354 * If requesting shared locking, locked vnode lock must not be recursed. 4355 * 4356 * Only one of LK_SHARED and LK_EXCLUSIVE must be specified. 4357 * LK_NODDLKTREAT can be optionally passed. 4358 * 4359 * If vp1 == vp2, only one, most exclusive, lock is obtained on it. 4360 */ 4361 void 4362 vn_lock_pair(struct vnode *vp1, bool vp1_locked, int lkflags1, 4363 struct vnode *vp2, bool vp2_locked, int lkflags2) 4364 { 4365 int error, locked1; 4366 4367 MPASS((((lkflags1 & LK_SHARED) != 0) ^ ((lkflags1 & LK_EXCLUSIVE) != 0)) || 4368 (vp1 == NULL && lkflags1 == 0)); 4369 MPASS((lkflags1 & ~(LK_SHARED | LK_EXCLUSIVE | LK_NODDLKTREAT)) == 0); 4370 MPASS((((lkflags2 & LK_SHARED) != 0) ^ ((lkflags2 & LK_EXCLUSIVE) != 0)) || 4371 (vp2 == NULL && lkflags2 == 0)); 4372 MPASS((lkflags2 & ~(LK_SHARED | LK_EXCLUSIVE | LK_NODDLKTREAT)) == 0); 4373 4374 if (vp1 == NULL && vp2 == NULL) 4375 return; 4376 4377 if (vp1 == vp2) { 4378 MPASS(vp1_locked == vp2_locked); 4379 4380 /* Select the most exclusive mode for lock. */ 4381 if ((lkflags1 & LK_TYPE_MASK) != (lkflags2 & LK_TYPE_MASK)) 4382 lkflags1 = (lkflags1 & ~LK_SHARED) | LK_EXCLUSIVE; 4383 4384 if (vp1_locked) { 4385 ASSERT_VOP_LOCKED(vp1, "vp1"); 4386 4387 /* No need to relock if any lock is exclusive. */ 4388 if ((vp1->v_vnlock->lock_object.lo_flags & 4389 LK_NOSHARE) != 0) 4390 return; 4391 4392 locked1 = VOP_ISLOCKED(vp1); 4393 if (((lkflags1 & LK_SHARED) != 0 && 4394 locked1 != LK_EXCLUSIVE) || 4395 ((lkflags1 & LK_EXCLUSIVE) != 0 && 4396 locked1 == LK_EXCLUSIVE)) 4397 return; 4398 VOP_UNLOCK(vp1); 4399 } 4400 4401 ASSERT_VOP_UNLOCKED(vp1, "vp1"); 4402 vn_lock(vp1, lkflags1 | LK_RETRY); 4403 return; 4404 } 4405 4406 if (vp1 != NULL) { 4407 if ((lkflags1 & LK_SHARED) != 0 && 4408 (vp1->v_vnlock->lock_object.lo_flags & LK_NOSHARE) != 0) 4409 lkflags1 = (lkflags1 & ~LK_SHARED) | LK_EXCLUSIVE; 4410 if (vp1_locked && VOP_ISLOCKED(vp1) != LK_EXCLUSIVE) { 4411 ASSERT_VOP_LOCKED(vp1, "vp1"); 4412 if ((lkflags1 & LK_EXCLUSIVE) != 0) { 4413 VOP_UNLOCK(vp1); 4414 ASSERT_VOP_UNLOCKED(vp1, 4415 "vp1 shared recursed"); 4416 vp1_locked = false; 4417 } 4418 } else if (!vp1_locked) 4419 ASSERT_VOP_UNLOCKED(vp1, "vp1"); 4420 } else { 4421 vp1_locked = true; 4422 } 4423 4424 if (vp2 != NULL) { 4425 if ((lkflags2 & LK_SHARED) != 0 && 4426 (vp2->v_vnlock->lock_object.lo_flags & LK_NOSHARE) != 0) 4427 lkflags2 = (lkflags2 & ~LK_SHARED) | LK_EXCLUSIVE; 4428 if (vp2_locked && VOP_ISLOCKED(vp2) != LK_EXCLUSIVE) { 4429 ASSERT_VOP_LOCKED(vp2, "vp2"); 4430 if ((lkflags2 & LK_EXCLUSIVE) != 0) { 4431 VOP_UNLOCK(vp2); 4432 ASSERT_VOP_UNLOCKED(vp2, 4433 "vp2 shared recursed"); 4434 vp2_locked = false; 4435 } 4436 } else if (!vp2_locked) 4437 ASSERT_VOP_UNLOCKED(vp2, "vp2"); 4438 } else { 4439 vp2_locked = true; 4440 } 4441 4442 if (!vp1_locked && !vp2_locked) { 4443 vn_lock(vp1, lkflags1 | LK_RETRY); 4444 vp1_locked = true; 4445 } 4446 4447 while (!vp1_locked || !vp2_locked) { 4448 if (vp1_locked && vp2 != NULL) { 4449 if (vp1 != NULL) { 4450 error = VOP_LOCK1(vp2, lkflags2 | LK_NOWAIT, 4451 __FILE__, __LINE__); 4452 if (error == 0) 4453 break; 4454 VOP_UNLOCK(vp1); 4455 vp1_locked = false; 4456 vn_lock_pair_pause("vlp1"); 4457 } 4458 vn_lock(vp2, lkflags2 | LK_RETRY); 4459 vp2_locked = true; 4460 } 4461 if (vp2_locked && vp1 != NULL) { 4462 if (vp2 != NULL) { 4463 error = VOP_LOCK1(vp1, lkflags1 | LK_NOWAIT, 4464 __FILE__, __LINE__); 4465 if (error == 0) 4466 break; 4467 VOP_UNLOCK(vp2); 4468 vp2_locked = false; 4469 vn_lock_pair_pause("vlp2"); 4470 } 4471 vn_lock(vp1, lkflags1 | LK_RETRY); 4472 vp1_locked = true; 4473 } 4474 } 4475 if (vp1 != NULL) { 4476 if (lkflags1 == LK_EXCLUSIVE) 4477 ASSERT_VOP_ELOCKED(vp1, "vp1 ret"); 4478 else 4479 ASSERT_VOP_LOCKED(vp1, "vp1 ret"); 4480 } 4481 if (vp2 != NULL) { 4482 if (lkflags2 == LK_EXCLUSIVE) 4483 ASSERT_VOP_ELOCKED(vp2, "vp2 ret"); 4484 else 4485 ASSERT_VOP_LOCKED(vp2, "vp2 ret"); 4486 } 4487 } 4488 4489 int 4490 vn_lktype_write(struct mount *mp, struct vnode *vp) 4491 { 4492 if (MNT_SHARED_WRITES(mp) || 4493 (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) 4494 return (LK_SHARED); 4495 return (LK_EXCLUSIVE); 4496 } 4497 4498 int 4499 vn_cmp(struct file *fp1, struct file *fp2, struct thread *td) 4500 { 4501 if (fp2->f_type != DTYPE_VNODE) 4502 return (3); 4503 return (kcmp_cmp((uintptr_t)fp1->f_vnode, (uintptr_t)fp2->f_vnode)); 4504 } 4505