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 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include "opt_hwpmc_hooks.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/disk.h> 53 #include <sys/fail.h> 54 #include <sys/fcntl.h> 55 #include <sys/file.h> 56 #include <sys/kdb.h> 57 #include <sys/ktr.h> 58 #include <sys/stat.h> 59 #include <sys/priv.h> 60 #include <sys/proc.h> 61 #include <sys/limits.h> 62 #include <sys/lock.h> 63 #include <sys/mman.h> 64 #include <sys/mount.h> 65 #include <sys/mutex.h> 66 #include <sys/namei.h> 67 #include <sys/vnode.h> 68 #include <sys/bio.h> 69 #include <sys/buf.h> 70 #include <sys/filio.h> 71 #include <sys/resourcevar.h> 72 #include <sys/rwlock.h> 73 #include <sys/sx.h> 74 #include <sys/sysctl.h> 75 #include <sys/ttycom.h> 76 #include <sys/conf.h> 77 #include <sys/syslog.h> 78 #include <sys/unistd.h> 79 #include <sys/user.h> 80 81 #include <security/audit/audit.h> 82 #include <security/mac/mac_framework.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_extern.h> 86 #include <vm/pmap.h> 87 #include <vm/vm_map.h> 88 #include <vm/vm_object.h> 89 #include <vm/vm_page.h> 90 #include <vm/vnode_pager.h> 91 92 #ifdef HWPMC_HOOKS 93 #include <sys/pmckern.h> 94 #endif 95 96 static fo_rdwr_t vn_read; 97 static fo_rdwr_t vn_write; 98 static fo_rdwr_t vn_io_fault; 99 static fo_truncate_t vn_truncate; 100 static fo_ioctl_t vn_ioctl; 101 static fo_poll_t vn_poll; 102 static fo_kqfilter_t vn_kqfilter; 103 static fo_stat_t vn_statfile; 104 static fo_close_t vn_closefile; 105 static fo_mmap_t vn_mmap; 106 107 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_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 123 }; 124 125 static const int io_hold_cnt = 16; 126 static int vn_io_fault_enable = 1; 127 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RW, 128 &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance"); 129 static int vn_io_fault_prefault = 0; 130 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_prefault, CTLFLAG_RW, 131 &vn_io_fault_prefault, 0, "Enable vn_io_fault prefaulting"); 132 static u_long vn_io_faults_cnt; 133 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD, 134 &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers"); 135 136 /* 137 * Returns true if vn_io_fault mode of handling the i/o request should 138 * be used. 139 */ 140 static bool 141 do_vn_io_fault(struct vnode *vp, struct uio *uio) 142 { 143 struct mount *mp; 144 145 return (uio->uio_segflg == UIO_USERSPACE && vp->v_type == VREG && 146 (mp = vp->v_mount) != NULL && 147 (mp->mnt_kern_flag & MNTK_NO_IOPF) != 0 && vn_io_fault_enable); 148 } 149 150 /* 151 * Structure used to pass arguments to vn_io_fault1(), to do either 152 * file- or vnode-based I/O calls. 153 */ 154 struct vn_io_fault_args { 155 enum { 156 VN_IO_FAULT_FOP, 157 VN_IO_FAULT_VOP 158 } kind; 159 struct ucred *cred; 160 int flags; 161 union { 162 struct fop_args_tag { 163 struct file *fp; 164 fo_rdwr_t *doio; 165 } fop_args; 166 struct vop_args_tag { 167 struct vnode *vp; 168 } vop_args; 169 } args; 170 }; 171 172 static int vn_io_fault1(struct vnode *vp, struct uio *uio, 173 struct vn_io_fault_args *args, struct thread *td); 174 175 int 176 vn_open(struct nameidata *ndp, int *flagp, int cmode, struct file *fp) 177 { 178 struct thread *td = ndp->ni_cnd.cn_thread; 179 180 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp)); 181 } 182 183 /* 184 * Common code for vnode open operations via a name lookup. 185 * Lookup the vnode and invoke VOP_CREATE if needed. 186 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 187 * 188 * Note that this does NOT free nameidata for the successful case, 189 * due to the NDINIT being done elsewhere. 190 */ 191 int 192 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags, 193 struct ucred *cred, struct file *fp) 194 { 195 struct vnode *vp; 196 struct mount *mp; 197 struct thread *td = ndp->ni_cnd.cn_thread; 198 struct vattr vat; 199 struct vattr *vap = &vat; 200 int fmode, error; 201 202 restart: 203 fmode = *flagp; 204 if ((fmode & (O_CREAT | O_EXCL | O_DIRECTORY)) == (O_CREAT | 205 O_EXCL | O_DIRECTORY)) 206 return (EINVAL); 207 else if ((fmode & (O_CREAT | O_DIRECTORY)) == O_CREAT) { 208 ndp->ni_cnd.cn_nameiop = CREATE; 209 /* 210 * Set NOCACHE to avoid flushing the cache when 211 * rolling in many files at once. 212 */ 213 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF | NOCACHE; 214 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 215 ndp->ni_cnd.cn_flags |= FOLLOW; 216 if ((fmode & O_BENEATH) != 0) 217 ndp->ni_cnd.cn_flags |= BENEATH; 218 if (!(vn_open_flags & VN_OPEN_NOAUDIT)) 219 ndp->ni_cnd.cn_flags |= AUDITVNODE1; 220 if (vn_open_flags & VN_OPEN_NOCAPCHECK) 221 ndp->ni_cnd.cn_flags |= NOCAPCHECK; 222 bwillwrite(); 223 if ((error = namei(ndp)) != 0) 224 return (error); 225 if (ndp->ni_vp == NULL) { 226 VATTR_NULL(vap); 227 vap->va_type = VREG; 228 vap->va_mode = cmode; 229 if (fmode & O_EXCL) 230 vap->va_vaflags |= VA_EXCLUSIVE; 231 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) { 232 NDFREE(ndp, NDF_ONLY_PNBUF); 233 vput(ndp->ni_dvp); 234 if ((error = vn_start_write(NULL, &mp, 235 V_XSLEEP | PCATCH)) != 0) 236 return (error); 237 goto restart; 238 } 239 if ((vn_open_flags & VN_OPEN_NAMECACHE) != 0) 240 ndp->ni_cnd.cn_flags |= MAKEENTRY; 241 #ifdef MAC 242 error = mac_vnode_check_create(cred, ndp->ni_dvp, 243 &ndp->ni_cnd, vap); 244 if (error == 0) 245 #endif 246 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 247 &ndp->ni_cnd, vap); 248 vput(ndp->ni_dvp); 249 vn_finished_write(mp); 250 if (error) { 251 NDFREE(ndp, NDF_ONLY_PNBUF); 252 return (error); 253 } 254 fmode &= ~O_TRUNC; 255 vp = ndp->ni_vp; 256 } else { 257 if (ndp->ni_dvp == ndp->ni_vp) 258 vrele(ndp->ni_dvp); 259 else 260 vput(ndp->ni_dvp); 261 ndp->ni_dvp = NULL; 262 vp = ndp->ni_vp; 263 if (fmode & O_EXCL) { 264 error = EEXIST; 265 goto bad; 266 } 267 fmode &= ~O_CREAT; 268 } 269 } else { 270 ndp->ni_cnd.cn_nameiop = LOOKUP; 271 ndp->ni_cnd.cn_flags = ISOPEN | 272 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF; 273 if (!(fmode & FWRITE)) 274 ndp->ni_cnd.cn_flags |= LOCKSHARED; 275 if ((fmode & O_BENEATH) != 0) 276 ndp->ni_cnd.cn_flags |= BENEATH; 277 if (!(vn_open_flags & VN_OPEN_NOAUDIT)) 278 ndp->ni_cnd.cn_flags |= AUDITVNODE1; 279 if (vn_open_flags & VN_OPEN_NOCAPCHECK) 280 ndp->ni_cnd.cn_flags |= NOCAPCHECK; 281 if ((error = namei(ndp)) != 0) 282 return (error); 283 vp = ndp->ni_vp; 284 } 285 error = vn_open_vnode(vp, fmode, cred, td, fp); 286 if (error) 287 goto bad; 288 *flagp = fmode; 289 return (0); 290 bad: 291 NDFREE(ndp, NDF_ONLY_PNBUF); 292 vput(vp); 293 *flagp = fmode; 294 ndp->ni_vp = NULL; 295 return (error); 296 } 297 298 static int 299 vn_open_vnode_advlock(struct vnode *vp, int fmode, struct file *fp) 300 { 301 struct flock lf; 302 int error, lock_flags, type; 303 304 ASSERT_VOP_LOCKED(vp, "vn_open_vnode_advlock"); 305 if ((fmode & (O_EXLOCK | O_SHLOCK)) == 0) 306 return (0); 307 KASSERT(fp != NULL, ("open with flock requires fp")); 308 if (fp->f_type != DTYPE_NONE && fp->f_type != DTYPE_VNODE) 309 return (EOPNOTSUPP); 310 311 lock_flags = VOP_ISLOCKED(vp); 312 VOP_UNLOCK(vp, 0); 313 314 lf.l_whence = SEEK_SET; 315 lf.l_start = 0; 316 lf.l_len = 0; 317 lf.l_type = (fmode & O_EXLOCK) != 0 ? F_WRLCK : F_RDLCK; 318 type = F_FLOCK; 319 if ((fmode & FNONBLOCK) == 0) 320 type |= F_WAIT; 321 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type); 322 if (error == 0) 323 fp->f_flag |= FHASLOCK; 324 325 vn_lock(vp, lock_flags | LK_RETRY); 326 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0) 327 error = ENOENT; 328 return (error); 329 } 330 331 /* 332 * Common code for vnode open operations once a vnode is located. 333 * Check permissions, and call the VOP_OPEN routine. 334 */ 335 int 336 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred, 337 struct thread *td, struct file *fp) 338 { 339 accmode_t accmode; 340 int error; 341 342 if (vp->v_type == VLNK) 343 return (EMLINK); 344 if (vp->v_type == VSOCK) 345 return (EOPNOTSUPP); 346 if (vp->v_type != VDIR && fmode & O_DIRECTORY) 347 return (ENOTDIR); 348 accmode = 0; 349 if (fmode & (FWRITE | O_TRUNC)) { 350 if (vp->v_type == VDIR) 351 return (EISDIR); 352 accmode |= VWRITE; 353 } 354 if (fmode & FREAD) 355 accmode |= VREAD; 356 if (fmode & FEXEC) 357 accmode |= VEXEC; 358 if ((fmode & O_APPEND) && (fmode & FWRITE)) 359 accmode |= VAPPEND; 360 #ifdef MAC 361 if (fmode & O_CREAT) 362 accmode |= VCREAT; 363 if (fmode & O_VERIFY) 364 accmode |= VVERIFY; 365 error = mac_vnode_check_open(cred, vp, accmode); 366 if (error) 367 return (error); 368 369 accmode &= ~(VCREAT | VVERIFY); 370 #endif 371 if ((fmode & O_CREAT) == 0 && accmode != 0) { 372 error = VOP_ACCESS(vp, accmode, cred, td); 373 if (error != 0) 374 return (error); 375 } 376 if (vp->v_type == VFIFO && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 377 vn_lock(vp, LK_UPGRADE | LK_RETRY); 378 error = VOP_OPEN(vp, fmode, cred, td, fp); 379 if (error != 0) 380 return (error); 381 382 error = vn_open_vnode_advlock(vp, fmode, fp); 383 if (error == 0 && (fmode & FWRITE) != 0) { 384 error = VOP_ADD_WRITECOUNT(vp, 1); 385 if (error == 0) { 386 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 387 __func__, vp, vp->v_writecount); 388 } 389 } 390 391 /* 392 * Error from advlock or VOP_ADD_WRITECOUNT() still requires 393 * calling VOP_CLOSE() to pair with earlier VOP_OPEN(). 394 * Arrange for that by having fdrop() to use vn_closefile(). 395 */ 396 if (error != 0) { 397 fp->f_flag |= FOPENFAILED; 398 fp->f_vnode = vp; 399 if (fp->f_ops == &badfileops) { 400 fp->f_type = DTYPE_VNODE; 401 fp->f_ops = &vnops; 402 } 403 vref(vp); 404 } 405 406 ASSERT_VOP_LOCKED(vp, "vn_open_vnode"); 407 return (error); 408 409 } 410 411 /* 412 * Check for write permissions on the specified vnode. 413 * Prototype text segments cannot be written. 414 * It is racy. 415 */ 416 int 417 vn_writechk(struct vnode *vp) 418 { 419 420 ASSERT_VOP_LOCKED(vp, "vn_writechk"); 421 /* 422 * If there's shared text associated with 423 * the vnode, try to free it up once. If 424 * we fail, we can't allow writing. 425 */ 426 if (VOP_IS_TEXT(vp)) 427 return (ETXTBSY); 428 429 return (0); 430 } 431 432 /* 433 * Vnode close call 434 */ 435 static int 436 vn_close1(struct vnode *vp, int flags, struct ucred *file_cred, 437 struct thread *td, bool keep_ref) 438 { 439 struct mount *mp; 440 int error, lock_flags; 441 442 if (vp->v_type != VFIFO && (flags & FWRITE) == 0 && 443 MNT_EXTENDED_SHARED(vp->v_mount)) 444 lock_flags = LK_SHARED; 445 else 446 lock_flags = LK_EXCLUSIVE; 447 448 vn_start_write(vp, &mp, V_WAIT); 449 vn_lock(vp, lock_flags | LK_RETRY); 450 AUDIT_ARG_VNODE1(vp); 451 if ((flags & (FWRITE | FOPENFAILED)) == FWRITE) { 452 VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 453 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 454 __func__, vp, vp->v_writecount); 455 } 456 error = VOP_CLOSE(vp, flags, file_cred, td); 457 if (keep_ref) 458 VOP_UNLOCK(vp, 0); 459 else 460 vput(vp); 461 vn_finished_write(mp); 462 return (error); 463 } 464 465 int 466 vn_close(struct vnode *vp, int flags, struct ucred *file_cred, 467 struct thread *td) 468 { 469 470 return (vn_close1(vp, flags, file_cred, td, false)); 471 } 472 473 /* 474 * Heuristic to detect sequential operation. 475 */ 476 static int 477 sequential_heuristic(struct uio *uio, struct file *fp) 478 { 479 480 ASSERT_VOP_LOCKED(fp->f_vnode, __func__); 481 if (fp->f_flag & FRDAHEAD) 482 return (fp->f_seqcount << IO_SEQSHIFT); 483 484 /* 485 * Offset 0 is handled specially. open() sets f_seqcount to 1 so 486 * that the first I/O is normally considered to be slightly 487 * sequential. Seeking to offset 0 doesn't change sequentiality 488 * unless previous seeks have reduced f_seqcount to 0, in which 489 * case offset 0 is not special. 490 */ 491 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) || 492 uio->uio_offset == fp->f_nextoff) { 493 /* 494 * f_seqcount is in units of fixed-size blocks so that it 495 * depends mainly on the amount of sequential I/O and not 496 * much on the number of sequential I/O's. The fixed size 497 * of 16384 is hard-coded here since it is (not quite) just 498 * a magic size that works well here. This size is more 499 * closely related to the best I/O size for real disks than 500 * to any block size used by software. 501 */ 502 fp->f_seqcount += lmin(IO_SEQMAX, 503 howmany(uio->uio_resid, 16384)); 504 return (fp->f_seqcount << IO_SEQSHIFT); 505 } 506 507 /* Not sequential. Quickly draw-down sequentiality. */ 508 if (fp->f_seqcount > 1) 509 fp->f_seqcount = 1; 510 else 511 fp->f_seqcount = 0; 512 return (0); 513 } 514 515 /* 516 * Package up an I/O request on a vnode into a uio and do it. 517 */ 518 int 519 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 520 enum uio_seg segflg, int ioflg, struct ucred *active_cred, 521 struct ucred *file_cred, ssize_t *aresid, struct thread *td) 522 { 523 struct uio auio; 524 struct iovec aiov; 525 struct mount *mp; 526 struct ucred *cred; 527 void *rl_cookie; 528 struct vn_io_fault_args args; 529 int error, lock_flags; 530 531 if (offset < 0 && vp->v_type != VCHR) 532 return (EINVAL); 533 auio.uio_iov = &aiov; 534 auio.uio_iovcnt = 1; 535 aiov.iov_base = base; 536 aiov.iov_len = len; 537 auio.uio_resid = len; 538 auio.uio_offset = offset; 539 auio.uio_segflg = segflg; 540 auio.uio_rw = rw; 541 auio.uio_td = td; 542 error = 0; 543 544 if ((ioflg & IO_NODELOCKED) == 0) { 545 if ((ioflg & IO_RANGELOCKED) == 0) { 546 if (rw == UIO_READ) { 547 rl_cookie = vn_rangelock_rlock(vp, offset, 548 offset + len); 549 } else { 550 rl_cookie = vn_rangelock_wlock(vp, offset, 551 offset + len); 552 } 553 } else 554 rl_cookie = NULL; 555 mp = NULL; 556 if (rw == UIO_WRITE) { 557 if (vp->v_type != VCHR && 558 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) 559 != 0) 560 goto out; 561 if (MNT_SHARED_WRITES(mp) || 562 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) 563 lock_flags = LK_SHARED; 564 else 565 lock_flags = LK_EXCLUSIVE; 566 } else 567 lock_flags = LK_SHARED; 568 vn_lock(vp, lock_flags | LK_RETRY); 569 } else 570 rl_cookie = NULL; 571 572 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 573 #ifdef MAC 574 if ((ioflg & IO_NOMACCHECK) == 0) { 575 if (rw == UIO_READ) 576 error = mac_vnode_check_read(active_cred, file_cred, 577 vp); 578 else 579 error = mac_vnode_check_write(active_cred, file_cred, 580 vp); 581 } 582 #endif 583 if (error == 0) { 584 if (file_cred != NULL) 585 cred = file_cred; 586 else 587 cred = active_cred; 588 if (do_vn_io_fault(vp, &auio)) { 589 args.kind = VN_IO_FAULT_VOP; 590 args.cred = cred; 591 args.flags = ioflg; 592 args.args.vop_args.vp = vp; 593 error = vn_io_fault1(vp, &auio, &args, td); 594 } else if (rw == UIO_READ) { 595 error = VOP_READ(vp, &auio, ioflg, cred); 596 } else /* if (rw == UIO_WRITE) */ { 597 error = VOP_WRITE(vp, &auio, ioflg, cred); 598 } 599 } 600 if (aresid) 601 *aresid = auio.uio_resid; 602 else 603 if (auio.uio_resid && error == 0) 604 error = EIO; 605 if ((ioflg & IO_NODELOCKED) == 0) { 606 VOP_UNLOCK(vp, 0); 607 if (mp != NULL) 608 vn_finished_write(mp); 609 } 610 out: 611 if (rl_cookie != NULL) 612 vn_rangelock_unlock(vp, rl_cookie); 613 return (error); 614 } 615 616 /* 617 * Package up an I/O request on a vnode into a uio and do it. The I/O 618 * request is split up into smaller chunks and we try to avoid saturating 619 * the buffer cache while potentially holding a vnode locked, so we 620 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield() 621 * to give other processes a chance to lock the vnode (either other processes 622 * core'ing the same binary, or unrelated processes scanning the directory). 623 */ 624 int 625 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, void *base, size_t len, 626 off_t offset, enum uio_seg segflg, int ioflg, struct ucred *active_cred, 627 struct ucred *file_cred, size_t *aresid, struct thread *td) 628 { 629 int error = 0; 630 ssize_t iaresid; 631 632 do { 633 int chunk; 634 635 /* 636 * Force `offset' to a multiple of MAXBSIZE except possibly 637 * for the first chunk, so that filesystems only need to 638 * write full blocks except possibly for the first and last 639 * chunks. 640 */ 641 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE; 642 643 if (chunk > len) 644 chunk = len; 645 if (rw != UIO_READ && vp->v_type == VREG) 646 bwillwrite(); 647 iaresid = 0; 648 error = vn_rdwr(rw, vp, base, chunk, offset, segflg, 649 ioflg, active_cred, file_cred, &iaresid, td); 650 len -= chunk; /* aresid calc already includes length */ 651 if (error) 652 break; 653 offset += chunk; 654 base = (char *)base + chunk; 655 kern_yield(PRI_USER); 656 } while (len); 657 if (aresid) 658 *aresid = len + iaresid; 659 return (error); 660 } 661 662 off_t 663 foffset_lock(struct file *fp, int flags) 664 { 665 struct mtx *mtxp; 666 off_t res; 667 668 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 669 670 #if OFF_MAX <= LONG_MAX 671 /* 672 * Caller only wants the current f_offset value. Assume that 673 * the long and shorter integer types reads are atomic. 674 */ 675 if ((flags & FOF_NOLOCK) != 0) 676 return (fp->f_offset); 677 #endif 678 679 /* 680 * According to McKusick the vn lock was protecting f_offset here. 681 * It is now protected by the FOFFSET_LOCKED flag. 682 */ 683 mtxp = mtx_pool_find(mtxpool_sleep, fp); 684 mtx_lock(mtxp); 685 if ((flags & FOF_NOLOCK) == 0) { 686 while (fp->f_vnread_flags & FOFFSET_LOCKED) { 687 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING; 688 msleep(&fp->f_vnread_flags, mtxp, PUSER -1, 689 "vofflock", 0); 690 } 691 fp->f_vnread_flags |= FOFFSET_LOCKED; 692 } 693 res = fp->f_offset; 694 mtx_unlock(mtxp); 695 return (res); 696 } 697 698 void 699 foffset_unlock(struct file *fp, off_t val, int flags) 700 { 701 struct mtx *mtxp; 702 703 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 704 705 #if OFF_MAX <= LONG_MAX 706 if ((flags & FOF_NOLOCK) != 0) { 707 if ((flags & FOF_NOUPDATE) == 0) 708 fp->f_offset = val; 709 if ((flags & FOF_NEXTOFF) != 0) 710 fp->f_nextoff = val; 711 return; 712 } 713 #endif 714 715 mtxp = mtx_pool_find(mtxpool_sleep, fp); 716 mtx_lock(mtxp); 717 if ((flags & FOF_NOUPDATE) == 0) 718 fp->f_offset = val; 719 if ((flags & FOF_NEXTOFF) != 0) 720 fp->f_nextoff = val; 721 if ((flags & FOF_NOLOCK) == 0) { 722 KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0, 723 ("Lost FOFFSET_LOCKED")); 724 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING) 725 wakeup(&fp->f_vnread_flags); 726 fp->f_vnread_flags = 0; 727 } 728 mtx_unlock(mtxp); 729 } 730 731 void 732 foffset_lock_uio(struct file *fp, struct uio *uio, int flags) 733 { 734 735 if ((flags & FOF_OFFSET) == 0) 736 uio->uio_offset = foffset_lock(fp, flags); 737 } 738 739 void 740 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags) 741 { 742 743 if ((flags & FOF_OFFSET) == 0) 744 foffset_unlock(fp, uio->uio_offset, flags); 745 } 746 747 static int 748 get_advice(struct file *fp, struct uio *uio) 749 { 750 struct mtx *mtxp; 751 int ret; 752 753 ret = POSIX_FADV_NORMAL; 754 if (fp->f_advice == NULL || fp->f_vnode->v_type != VREG) 755 return (ret); 756 757 mtxp = mtx_pool_find(mtxpool_sleep, fp); 758 mtx_lock(mtxp); 759 if (fp->f_advice != NULL && 760 uio->uio_offset >= fp->f_advice->fa_start && 761 uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end) 762 ret = fp->f_advice->fa_advice; 763 mtx_unlock(mtxp); 764 return (ret); 765 } 766 767 /* 768 * File table vnode read routine. 769 */ 770 static int 771 vn_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, 772 struct thread *td) 773 { 774 struct vnode *vp; 775 off_t orig_offset; 776 int error, ioflag; 777 int advice; 778 779 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 780 uio->uio_td, td)); 781 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 782 vp = fp->f_vnode; 783 ioflag = 0; 784 if (fp->f_flag & FNONBLOCK) 785 ioflag |= IO_NDELAY; 786 if (fp->f_flag & O_DIRECT) 787 ioflag |= IO_DIRECT; 788 advice = get_advice(fp, uio); 789 vn_lock(vp, LK_SHARED | LK_RETRY); 790 791 switch (advice) { 792 case POSIX_FADV_NORMAL: 793 case POSIX_FADV_SEQUENTIAL: 794 case POSIX_FADV_NOREUSE: 795 ioflag |= sequential_heuristic(uio, fp); 796 break; 797 case POSIX_FADV_RANDOM: 798 /* Disable read-ahead for random I/O. */ 799 break; 800 } 801 orig_offset = uio->uio_offset; 802 803 #ifdef MAC 804 error = mac_vnode_check_read(active_cred, fp->f_cred, vp); 805 if (error == 0) 806 #endif 807 error = VOP_READ(vp, uio, ioflag, fp->f_cred); 808 fp->f_nextoff = uio->uio_offset; 809 VOP_UNLOCK(vp, 0); 810 if (error == 0 && advice == POSIX_FADV_NOREUSE && 811 orig_offset != uio->uio_offset) 812 /* 813 * Use POSIX_FADV_DONTNEED to flush pages and buffers 814 * for the backing file after a POSIX_FADV_NOREUSE 815 * read(2). 816 */ 817 error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1, 818 POSIX_FADV_DONTNEED); 819 return (error); 820 } 821 822 /* 823 * File table vnode write routine. 824 */ 825 static int 826 vn_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, 827 struct thread *td) 828 { 829 struct vnode *vp; 830 struct mount *mp; 831 off_t orig_offset; 832 int error, ioflag, lock_flags; 833 int advice; 834 835 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 836 uio->uio_td, td)); 837 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 838 vp = fp->f_vnode; 839 if (vp->v_type == VREG) 840 bwillwrite(); 841 ioflag = IO_UNIT; 842 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 843 ioflag |= IO_APPEND; 844 if (fp->f_flag & FNONBLOCK) 845 ioflag |= IO_NDELAY; 846 if (fp->f_flag & O_DIRECT) 847 ioflag |= IO_DIRECT; 848 if ((fp->f_flag & O_FSYNC) || 849 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 850 ioflag |= IO_SYNC; 851 mp = NULL; 852 if (vp->v_type != VCHR && 853 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 854 goto unlock; 855 856 advice = get_advice(fp, uio); 857 858 if (MNT_SHARED_WRITES(mp) || 859 (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) { 860 lock_flags = LK_SHARED; 861 } else { 862 lock_flags = LK_EXCLUSIVE; 863 } 864 865 vn_lock(vp, lock_flags | LK_RETRY); 866 switch (advice) { 867 case POSIX_FADV_NORMAL: 868 case POSIX_FADV_SEQUENTIAL: 869 case POSIX_FADV_NOREUSE: 870 ioflag |= sequential_heuristic(uio, fp); 871 break; 872 case POSIX_FADV_RANDOM: 873 /* XXX: Is this correct? */ 874 break; 875 } 876 orig_offset = uio->uio_offset; 877 878 #ifdef MAC 879 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 880 if (error == 0) 881 #endif 882 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred); 883 fp->f_nextoff = uio->uio_offset; 884 VOP_UNLOCK(vp, 0); 885 if (vp->v_type != VCHR) 886 vn_finished_write(mp); 887 if (error == 0 && advice == POSIX_FADV_NOREUSE && 888 orig_offset != uio->uio_offset) 889 /* 890 * Use POSIX_FADV_DONTNEED to flush pages and buffers 891 * for the backing file after a POSIX_FADV_NOREUSE 892 * write(2). 893 */ 894 error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1, 895 POSIX_FADV_DONTNEED); 896 unlock: 897 return (error); 898 } 899 900 /* 901 * The vn_io_fault() is a wrapper around vn_read() and vn_write() to 902 * prevent the following deadlock: 903 * 904 * Assume that the thread A reads from the vnode vp1 into userspace 905 * buffer buf1 backed by the pages of vnode vp2. If a page in buf1 is 906 * currently not resident, then system ends up with the call chain 907 * vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] -> 908 * vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2) 909 * which establishes lock order vp1->vn_lock, then vp2->vn_lock. 910 * If, at the same time, thread B reads from vnode vp2 into buffer buf2 911 * backed by the pages of vnode vp1, and some page in buf2 is not 912 * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock. 913 * 914 * To prevent the lock order reversal and deadlock, vn_io_fault() does 915 * not allow page faults to happen during VOP_READ() or VOP_WRITE(). 916 * Instead, it first tries to do the whole range i/o with pagefaults 917 * disabled. If all pages in the i/o buffer are resident and mapped, 918 * VOP will succeed (ignoring the genuine filesystem errors). 919 * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do 920 * i/o in chunks, with all pages in the chunk prefaulted and held 921 * using vm_fault_quick_hold_pages(). 922 * 923 * Filesystems using this deadlock avoidance scheme should use the 924 * array of the held pages from uio, saved in the curthread->td_ma, 925 * instead of doing uiomove(). A helper function 926 * vn_io_fault_uiomove() converts uiomove request into 927 * uiomove_fromphys() over td_ma array. 928 * 929 * Since vnode locks do not cover the whole i/o anymore, rangelocks 930 * make the current i/o request atomic with respect to other i/os and 931 * truncations. 932 */ 933 934 /* 935 * Decode vn_io_fault_args and perform the corresponding i/o. 936 */ 937 static int 938 vn_io_fault_doio(struct vn_io_fault_args *args, struct uio *uio, 939 struct thread *td) 940 { 941 int error, save; 942 943 error = 0; 944 save = vm_fault_disable_pagefaults(); 945 switch (args->kind) { 946 case VN_IO_FAULT_FOP: 947 error = (args->args.fop_args.doio)(args->args.fop_args.fp, 948 uio, args->cred, args->flags, td); 949 break; 950 case VN_IO_FAULT_VOP: 951 if (uio->uio_rw == UIO_READ) { 952 error = VOP_READ(args->args.vop_args.vp, uio, 953 args->flags, args->cred); 954 } else if (uio->uio_rw == UIO_WRITE) { 955 error = VOP_WRITE(args->args.vop_args.vp, uio, 956 args->flags, args->cred); 957 } 958 break; 959 default: 960 panic("vn_io_fault_doio: unknown kind of io %d %d", 961 args->kind, uio->uio_rw); 962 } 963 vm_fault_enable_pagefaults(save); 964 return (error); 965 } 966 967 static int 968 vn_io_fault_touch(char *base, const struct uio *uio) 969 { 970 int r; 971 972 r = fubyte(base); 973 if (r == -1 || (uio->uio_rw == UIO_READ && subyte(base, r) == -1)) 974 return (EFAULT); 975 return (0); 976 } 977 978 static int 979 vn_io_fault_prefault_user(const struct uio *uio) 980 { 981 char *base; 982 const struct iovec *iov; 983 size_t len; 984 ssize_t resid; 985 int error, i; 986 987 KASSERT(uio->uio_segflg == UIO_USERSPACE, 988 ("vn_io_fault_prefault userspace")); 989 990 error = i = 0; 991 iov = uio->uio_iov; 992 resid = uio->uio_resid; 993 base = iov->iov_base; 994 len = iov->iov_len; 995 while (resid > 0) { 996 error = vn_io_fault_touch(base, uio); 997 if (error != 0) 998 break; 999 if (len < PAGE_SIZE) { 1000 if (len != 0) { 1001 error = vn_io_fault_touch(base + len - 1, uio); 1002 if (error != 0) 1003 break; 1004 resid -= len; 1005 } 1006 if (++i >= uio->uio_iovcnt) 1007 break; 1008 iov = uio->uio_iov + i; 1009 base = iov->iov_base; 1010 len = iov->iov_len; 1011 } else { 1012 len -= PAGE_SIZE; 1013 base += PAGE_SIZE; 1014 resid -= PAGE_SIZE; 1015 } 1016 } 1017 return (error); 1018 } 1019 1020 /* 1021 * Common code for vn_io_fault(), agnostic to the kind of i/o request. 1022 * Uses vn_io_fault_doio() to make the call to an actual i/o function. 1023 * Used from vn_rdwr() and vn_io_fault(), which encode the i/o request 1024 * into args and call vn_io_fault1() to handle faults during the user 1025 * mode buffer accesses. 1026 */ 1027 static int 1028 vn_io_fault1(struct vnode *vp, struct uio *uio, struct vn_io_fault_args *args, 1029 struct thread *td) 1030 { 1031 vm_page_t ma[io_hold_cnt + 2]; 1032 struct uio *uio_clone, short_uio; 1033 struct iovec short_iovec[1]; 1034 vm_page_t *prev_td_ma; 1035 vm_prot_t prot; 1036 vm_offset_t addr, end; 1037 size_t len, resid; 1038 ssize_t adv; 1039 int error, cnt, saveheld, prev_td_ma_cnt; 1040 1041 if (vn_io_fault_prefault) { 1042 error = vn_io_fault_prefault_user(uio); 1043 if (error != 0) 1044 return (error); /* Or ignore ? */ 1045 } 1046 1047 prot = uio->uio_rw == UIO_READ ? VM_PROT_WRITE : VM_PROT_READ; 1048 1049 /* 1050 * The UFS follows IO_UNIT directive and replays back both 1051 * uio_offset and uio_resid if an error is encountered during the 1052 * operation. But, since the iovec may be already advanced, 1053 * uio is still in an inconsistent state. 1054 * 1055 * Cache a copy of the original uio, which is advanced to the redo 1056 * point using UIO_NOCOPY below. 1057 */ 1058 uio_clone = cloneuio(uio); 1059 resid = uio->uio_resid; 1060 1061 short_uio.uio_segflg = UIO_USERSPACE; 1062 short_uio.uio_rw = uio->uio_rw; 1063 short_uio.uio_td = uio->uio_td; 1064 1065 error = vn_io_fault_doio(args, uio, td); 1066 if (error != EFAULT) 1067 goto out; 1068 1069 atomic_add_long(&vn_io_faults_cnt, 1); 1070 uio_clone->uio_segflg = UIO_NOCOPY; 1071 uiomove(NULL, resid - uio->uio_resid, uio_clone); 1072 uio_clone->uio_segflg = uio->uio_segflg; 1073 1074 saveheld = curthread_pflags_set(TDP_UIOHELD); 1075 prev_td_ma = td->td_ma; 1076 prev_td_ma_cnt = td->td_ma_cnt; 1077 1078 while (uio_clone->uio_resid != 0) { 1079 len = uio_clone->uio_iov->iov_len; 1080 if (len == 0) { 1081 KASSERT(uio_clone->uio_iovcnt >= 1, 1082 ("iovcnt underflow")); 1083 uio_clone->uio_iov++; 1084 uio_clone->uio_iovcnt--; 1085 continue; 1086 } 1087 if (len > io_hold_cnt * PAGE_SIZE) 1088 len = io_hold_cnt * PAGE_SIZE; 1089 addr = (uintptr_t)uio_clone->uio_iov->iov_base; 1090 end = round_page(addr + len); 1091 if (end < addr) { 1092 error = EFAULT; 1093 break; 1094 } 1095 cnt = atop(end - trunc_page(addr)); 1096 /* 1097 * A perfectly misaligned address and length could cause 1098 * both the start and the end of the chunk to use partial 1099 * page. +2 accounts for such a situation. 1100 */ 1101 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map, 1102 addr, len, prot, ma, io_hold_cnt + 2); 1103 if (cnt == -1) { 1104 error = EFAULT; 1105 break; 1106 } 1107 short_uio.uio_iov = &short_iovec[0]; 1108 short_iovec[0].iov_base = (void *)addr; 1109 short_uio.uio_iovcnt = 1; 1110 short_uio.uio_resid = short_iovec[0].iov_len = len; 1111 short_uio.uio_offset = uio_clone->uio_offset; 1112 td->td_ma = ma; 1113 td->td_ma_cnt = cnt; 1114 1115 error = vn_io_fault_doio(args, &short_uio, td); 1116 vm_page_unhold_pages(ma, cnt); 1117 adv = len - short_uio.uio_resid; 1118 1119 uio_clone->uio_iov->iov_base = 1120 (char *)uio_clone->uio_iov->iov_base + adv; 1121 uio_clone->uio_iov->iov_len -= adv; 1122 uio_clone->uio_resid -= adv; 1123 uio_clone->uio_offset += adv; 1124 1125 uio->uio_resid -= adv; 1126 uio->uio_offset += adv; 1127 1128 if (error != 0 || adv == 0) 1129 break; 1130 } 1131 td->td_ma = prev_td_ma; 1132 td->td_ma_cnt = prev_td_ma_cnt; 1133 curthread_pflags_restore(saveheld); 1134 out: 1135 free(uio_clone, M_IOV); 1136 return (error); 1137 } 1138 1139 static int 1140 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred, 1141 int flags, struct thread *td) 1142 { 1143 fo_rdwr_t *doio; 1144 struct vnode *vp; 1145 void *rl_cookie; 1146 struct vn_io_fault_args args; 1147 int error; 1148 1149 doio = uio->uio_rw == UIO_READ ? vn_read : vn_write; 1150 vp = fp->f_vnode; 1151 foffset_lock_uio(fp, uio, flags); 1152 if (do_vn_io_fault(vp, uio)) { 1153 args.kind = VN_IO_FAULT_FOP; 1154 args.args.fop_args.fp = fp; 1155 args.args.fop_args.doio = doio; 1156 args.cred = active_cred; 1157 args.flags = flags | FOF_OFFSET; 1158 if (uio->uio_rw == UIO_READ) { 1159 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset, 1160 uio->uio_offset + uio->uio_resid); 1161 } else if ((fp->f_flag & O_APPEND) != 0 || 1162 (flags & FOF_OFFSET) == 0) { 1163 /* For appenders, punt and lock the whole range. */ 1164 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1165 } else { 1166 rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset, 1167 uio->uio_offset + uio->uio_resid); 1168 } 1169 error = vn_io_fault1(vp, uio, &args, td); 1170 vn_rangelock_unlock(vp, rl_cookie); 1171 } else { 1172 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td); 1173 } 1174 foffset_unlock_uio(fp, uio, flags); 1175 return (error); 1176 } 1177 1178 /* 1179 * Helper function to perform the requested uiomove operation using 1180 * the held pages for io->uio_iov[0].iov_base buffer instead of 1181 * copyin/copyout. Access to the pages with uiomove_fromphys() 1182 * instead of iov_base prevents page faults that could occur due to 1183 * pmap_collect() invalidating the mapping created by 1184 * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or 1185 * object cleanup revoking the write access from page mappings. 1186 * 1187 * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove() 1188 * instead of plain uiomove(). 1189 */ 1190 int 1191 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio) 1192 { 1193 struct uio transp_uio; 1194 struct iovec transp_iov[1]; 1195 struct thread *td; 1196 size_t adv; 1197 int error, pgadv; 1198 1199 td = curthread; 1200 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1201 uio->uio_segflg != UIO_USERSPACE) 1202 return (uiomove(data, xfersize, uio)); 1203 1204 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1205 transp_iov[0].iov_base = data; 1206 transp_uio.uio_iov = &transp_iov[0]; 1207 transp_uio.uio_iovcnt = 1; 1208 if (xfersize > uio->uio_resid) 1209 xfersize = uio->uio_resid; 1210 transp_uio.uio_resid = transp_iov[0].iov_len = xfersize; 1211 transp_uio.uio_offset = 0; 1212 transp_uio.uio_segflg = UIO_SYSSPACE; 1213 /* 1214 * Since transp_iov points to data, and td_ma page array 1215 * corresponds to original uio->uio_iov, we need to invert the 1216 * direction of the i/o operation as passed to 1217 * uiomove_fromphys(). 1218 */ 1219 switch (uio->uio_rw) { 1220 case UIO_WRITE: 1221 transp_uio.uio_rw = UIO_READ; 1222 break; 1223 case UIO_READ: 1224 transp_uio.uio_rw = UIO_WRITE; 1225 break; 1226 } 1227 transp_uio.uio_td = uio->uio_td; 1228 error = uiomove_fromphys(td->td_ma, 1229 ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK, 1230 xfersize, &transp_uio); 1231 adv = xfersize - transp_uio.uio_resid; 1232 pgadv = 1233 (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) - 1234 (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT); 1235 td->td_ma += pgadv; 1236 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1237 pgadv)); 1238 td->td_ma_cnt -= pgadv; 1239 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv; 1240 uio->uio_iov->iov_len -= adv; 1241 uio->uio_resid -= adv; 1242 uio->uio_offset += adv; 1243 return (error); 1244 } 1245 1246 int 1247 vn_io_fault_pgmove(vm_page_t ma[], vm_offset_t offset, int xfersize, 1248 struct uio *uio) 1249 { 1250 struct thread *td; 1251 vm_offset_t iov_base; 1252 int cnt, pgadv; 1253 1254 td = curthread; 1255 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1256 uio->uio_segflg != UIO_USERSPACE) 1257 return (uiomove_fromphys(ma, offset, xfersize, uio)); 1258 1259 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1260 cnt = xfersize > uio->uio_resid ? uio->uio_resid : xfersize; 1261 iov_base = (vm_offset_t)uio->uio_iov->iov_base; 1262 switch (uio->uio_rw) { 1263 case UIO_WRITE: 1264 pmap_copy_pages(td->td_ma, iov_base & PAGE_MASK, ma, 1265 offset, cnt); 1266 break; 1267 case UIO_READ: 1268 pmap_copy_pages(ma, offset, td->td_ma, iov_base & PAGE_MASK, 1269 cnt); 1270 break; 1271 } 1272 pgadv = ((iov_base + cnt) >> PAGE_SHIFT) - (iov_base >> PAGE_SHIFT); 1273 td->td_ma += pgadv; 1274 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1275 pgadv)); 1276 td->td_ma_cnt -= pgadv; 1277 uio->uio_iov->iov_base = (char *)(iov_base + cnt); 1278 uio->uio_iov->iov_len -= cnt; 1279 uio->uio_resid -= cnt; 1280 uio->uio_offset += cnt; 1281 return (0); 1282 } 1283 1284 1285 /* 1286 * File table truncate routine. 1287 */ 1288 static int 1289 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred, 1290 struct thread *td) 1291 { 1292 struct mount *mp; 1293 struct vnode *vp; 1294 void *rl_cookie; 1295 int error; 1296 1297 vp = fp->f_vnode; 1298 1299 /* 1300 * Lock the whole range for truncation. Otherwise split i/o 1301 * might happen partly before and partly after the truncation. 1302 */ 1303 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1304 error = vn_start_write(vp, &mp, V_WAIT | PCATCH); 1305 if (error) 1306 goto out1; 1307 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1308 AUDIT_ARG_VNODE1(vp); 1309 if (vp->v_type == VDIR) { 1310 error = EISDIR; 1311 goto out; 1312 } 1313 #ifdef MAC 1314 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 1315 if (error) 1316 goto out; 1317 #endif 1318 error = vn_truncate_locked(vp, length, (fp->f_flag & O_FSYNC) != 0, 1319 fp->f_cred); 1320 out: 1321 VOP_UNLOCK(vp, 0); 1322 vn_finished_write(mp); 1323 out1: 1324 vn_rangelock_unlock(vp, rl_cookie); 1325 return (error); 1326 } 1327 1328 /* 1329 * Truncate a file that is already locked. 1330 */ 1331 int 1332 vn_truncate_locked(struct vnode *vp, off_t length, bool sync, 1333 struct ucred *cred) 1334 { 1335 struct vattr vattr; 1336 int error; 1337 1338 error = VOP_ADD_WRITECOUNT(vp, 1); 1339 if (error == 0) { 1340 VATTR_NULL(&vattr); 1341 vattr.va_size = length; 1342 if (sync) 1343 vattr.va_vaflags |= VA_SYNC; 1344 error = VOP_SETATTR(vp, &vattr, cred); 1345 VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 1346 } 1347 return (error); 1348 } 1349 1350 /* 1351 * File table vnode stat routine. 1352 */ 1353 static int 1354 vn_statfile(struct file *fp, struct stat *sb, struct ucred *active_cred, 1355 struct thread *td) 1356 { 1357 struct vnode *vp = fp->f_vnode; 1358 int error; 1359 1360 vn_lock(vp, LK_SHARED | LK_RETRY); 1361 error = vn_stat(vp, sb, active_cred, fp->f_cred, td); 1362 VOP_UNLOCK(vp, 0); 1363 1364 return (error); 1365 } 1366 1367 /* 1368 * Stat a vnode; implementation for the stat syscall 1369 */ 1370 int 1371 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *active_cred, 1372 struct ucred *file_cred, struct thread *td) 1373 { 1374 struct vattr vattr; 1375 struct vattr *vap; 1376 int error; 1377 u_short mode; 1378 1379 AUDIT_ARG_VNODE1(vp); 1380 #ifdef MAC 1381 error = mac_vnode_check_stat(active_cred, file_cred, vp); 1382 if (error) 1383 return (error); 1384 #endif 1385 1386 vap = &vattr; 1387 1388 /* 1389 * Initialize defaults for new and unusual fields, so that file 1390 * systems which don't support these fields don't need to know 1391 * about them. 1392 */ 1393 vap->va_birthtime.tv_sec = -1; 1394 vap->va_birthtime.tv_nsec = 0; 1395 vap->va_fsid = VNOVAL; 1396 vap->va_rdev = NODEV; 1397 1398 error = VOP_GETATTR(vp, vap, active_cred); 1399 if (error) 1400 return (error); 1401 1402 /* 1403 * Zero the spare stat fields 1404 */ 1405 bzero(sb, sizeof *sb); 1406 1407 /* 1408 * Copy from vattr table 1409 */ 1410 if (vap->va_fsid != VNOVAL) 1411 sb->st_dev = vap->va_fsid; 1412 else 1413 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 1414 sb->st_ino = vap->va_fileid; 1415 mode = vap->va_mode; 1416 switch (vap->va_type) { 1417 case VREG: 1418 mode |= S_IFREG; 1419 break; 1420 case VDIR: 1421 mode |= S_IFDIR; 1422 break; 1423 case VBLK: 1424 mode |= S_IFBLK; 1425 break; 1426 case VCHR: 1427 mode |= S_IFCHR; 1428 break; 1429 case VLNK: 1430 mode |= S_IFLNK; 1431 break; 1432 case VSOCK: 1433 mode |= S_IFSOCK; 1434 break; 1435 case VFIFO: 1436 mode |= S_IFIFO; 1437 break; 1438 default: 1439 return (EBADF); 1440 } 1441 sb->st_mode = mode; 1442 sb->st_nlink = vap->va_nlink; 1443 sb->st_uid = vap->va_uid; 1444 sb->st_gid = vap->va_gid; 1445 sb->st_rdev = vap->va_rdev; 1446 if (vap->va_size > OFF_MAX) 1447 return (EOVERFLOW); 1448 sb->st_size = vap->va_size; 1449 sb->st_atim = vap->va_atime; 1450 sb->st_mtim = vap->va_mtime; 1451 sb->st_ctim = vap->va_ctime; 1452 sb->st_birthtim = vap->va_birthtime; 1453 1454 /* 1455 * According to www.opengroup.org, the meaning of st_blksize is 1456 * "a filesystem-specific preferred I/O block size for this 1457 * object. In some filesystem types, this may vary from file 1458 * to file" 1459 * Use miminum/default of PAGE_SIZE (e.g. for VCHR). 1460 */ 1461 1462 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize); 1463 1464 sb->st_flags = vap->va_flags; 1465 if (priv_check(td, PRIV_VFS_GENERATION)) 1466 sb->st_gen = 0; 1467 else 1468 sb->st_gen = vap->va_gen; 1469 1470 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 1471 return (0); 1472 } 1473 1474 /* 1475 * File table vnode ioctl routine. 1476 */ 1477 static int 1478 vn_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, 1479 struct thread *td) 1480 { 1481 struct vattr vattr; 1482 struct vnode *vp; 1483 struct fiobmap2_arg *bmarg; 1484 int error; 1485 1486 vp = fp->f_vnode; 1487 switch (vp->v_type) { 1488 case VDIR: 1489 case VREG: 1490 switch (com) { 1491 case FIONREAD: 1492 vn_lock(vp, LK_SHARED | LK_RETRY); 1493 error = VOP_GETATTR(vp, &vattr, active_cred); 1494 VOP_UNLOCK(vp, 0); 1495 if (error == 0) 1496 *(int *)data = vattr.va_size - fp->f_offset; 1497 return (error); 1498 case FIOBMAP2: 1499 bmarg = (struct fiobmap2_arg *)data; 1500 vn_lock(vp, LK_SHARED | LK_RETRY); 1501 #ifdef MAC 1502 error = mac_vnode_check_read(active_cred, fp->f_cred, 1503 vp); 1504 if (error == 0) 1505 #endif 1506 error = VOP_BMAP(vp, bmarg->bn, NULL, 1507 &bmarg->bn, &bmarg->runp, &bmarg->runb); 1508 VOP_UNLOCK(vp, 0); 1509 return (error); 1510 case FIONBIO: 1511 case FIOASYNC: 1512 return (0); 1513 default: 1514 return (VOP_IOCTL(vp, com, data, fp->f_flag, 1515 active_cred, td)); 1516 } 1517 break; 1518 case VCHR: 1519 return (VOP_IOCTL(vp, com, data, fp->f_flag, 1520 active_cred, td)); 1521 default: 1522 return (ENOTTY); 1523 } 1524 } 1525 1526 /* 1527 * File table vnode poll routine. 1528 */ 1529 static int 1530 vn_poll(struct file *fp, int events, struct ucred *active_cred, 1531 struct thread *td) 1532 { 1533 struct vnode *vp; 1534 int error; 1535 1536 vp = fp->f_vnode; 1537 #ifdef MAC 1538 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1539 AUDIT_ARG_VNODE1(vp); 1540 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp); 1541 VOP_UNLOCK(vp, 0); 1542 if (!error) 1543 #endif 1544 1545 error = VOP_POLL(vp, events, fp->f_cred, td); 1546 return (error); 1547 } 1548 1549 /* 1550 * Acquire the requested lock and then check for validity. LK_RETRY 1551 * permits vn_lock to return doomed vnodes. 1552 */ 1553 int 1554 _vn_lock(struct vnode *vp, int flags, char *file, int line) 1555 { 1556 int error; 1557 1558 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 1559 ("vn_lock: no locktype")); 1560 VNASSERT(vp->v_holdcnt != 0, vp, ("vn_lock: zero hold count")); 1561 retry: 1562 error = VOP_LOCK1(vp, flags, file, line); 1563 flags &= ~LK_INTERLOCK; /* Interlock is always dropped. */ 1564 KASSERT((flags & LK_RETRY) == 0 || error == 0, 1565 ("vn_lock: error %d incompatible with flags %#x", error, flags)); 1566 1567 if ((flags & LK_RETRY) == 0) { 1568 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0) { 1569 VOP_UNLOCK(vp, 0); 1570 error = ENOENT; 1571 } 1572 } else if (error != 0) 1573 goto retry; 1574 return (error); 1575 } 1576 1577 /* 1578 * File table vnode close routine. 1579 */ 1580 static int 1581 vn_closefile(struct file *fp, struct thread *td) 1582 { 1583 struct vnode *vp; 1584 struct flock lf; 1585 int error; 1586 bool ref; 1587 1588 vp = fp->f_vnode; 1589 fp->f_ops = &badfileops; 1590 ref= (fp->f_flag & FHASLOCK) != 0 && fp->f_type == DTYPE_VNODE; 1591 1592 error = vn_close1(vp, fp->f_flag, fp->f_cred, td, ref); 1593 1594 if (__predict_false(ref)) { 1595 lf.l_whence = SEEK_SET; 1596 lf.l_start = 0; 1597 lf.l_len = 0; 1598 lf.l_type = F_UNLCK; 1599 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK); 1600 vrele(vp); 1601 } 1602 return (error); 1603 } 1604 1605 static bool 1606 vn_suspendable(struct mount *mp) 1607 { 1608 1609 return (mp->mnt_op->vfs_susp_clean != NULL); 1610 } 1611 1612 /* 1613 * Preparing to start a filesystem write operation. If the operation is 1614 * permitted, then we bump the count of operations in progress and 1615 * proceed. If a suspend request is in progress, we wait until the 1616 * suspension is over, and then proceed. 1617 */ 1618 static int 1619 vn_start_write_locked(struct mount *mp, int flags) 1620 { 1621 int error, mflags; 1622 1623 mtx_assert(MNT_MTX(mp), MA_OWNED); 1624 error = 0; 1625 1626 /* 1627 * Check on status of suspension. 1628 */ 1629 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 || 1630 mp->mnt_susp_owner != curthread) { 1631 mflags = ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0 ? 1632 (flags & PCATCH) : 0) | (PUSER - 1); 1633 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 1634 if (flags & V_NOWAIT) { 1635 error = EWOULDBLOCK; 1636 goto unlock; 1637 } 1638 error = msleep(&mp->mnt_flag, MNT_MTX(mp), mflags, 1639 "suspfs", 0); 1640 if (error) 1641 goto unlock; 1642 } 1643 } 1644 if (flags & V_XSLEEP) 1645 goto unlock; 1646 mp->mnt_writeopcount++; 1647 unlock: 1648 if (error != 0 || (flags & V_XSLEEP) != 0) 1649 MNT_REL(mp); 1650 MNT_IUNLOCK(mp); 1651 return (error); 1652 } 1653 1654 int 1655 vn_start_write(struct vnode *vp, struct mount **mpp, int flags) 1656 { 1657 struct mount *mp; 1658 int error; 1659 1660 KASSERT((flags & V_MNTREF) == 0 || (*mpp != NULL && vp == NULL), 1661 ("V_MNTREF requires mp")); 1662 1663 error = 0; 1664 /* 1665 * If a vnode is provided, get and return the mount point that 1666 * to which it will write. 1667 */ 1668 if (vp != NULL) { 1669 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 1670 *mpp = NULL; 1671 if (error != EOPNOTSUPP) 1672 return (error); 1673 return (0); 1674 } 1675 } 1676 if ((mp = *mpp) == NULL) 1677 return (0); 1678 1679 if (!vn_suspendable(mp)) { 1680 if (vp != NULL || (flags & V_MNTREF) != 0) 1681 vfs_rel(mp); 1682 return (0); 1683 } 1684 1685 /* 1686 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 1687 * a vfs_ref(). 1688 * As long as a vnode is not provided we need to acquire a 1689 * refcount for the provided mountpoint too, in order to 1690 * emulate a vfs_ref(). 1691 */ 1692 MNT_ILOCK(mp); 1693 if (vp == NULL && (flags & V_MNTREF) == 0) 1694 MNT_REF(mp); 1695 1696 return (vn_start_write_locked(mp, flags)); 1697 } 1698 1699 /* 1700 * Secondary suspension. Used by operations such as vop_inactive 1701 * routines that are needed by the higher level functions. These 1702 * are allowed to proceed until all the higher level functions have 1703 * completed (indicated by mnt_writeopcount dropping to zero). At that 1704 * time, these operations are halted until the suspension is over. 1705 */ 1706 int 1707 vn_start_secondary_write(struct vnode *vp, struct mount **mpp, int flags) 1708 { 1709 struct mount *mp; 1710 int error; 1711 1712 KASSERT((flags & V_MNTREF) == 0 || (*mpp != NULL && vp == NULL), 1713 ("V_MNTREF requires mp")); 1714 1715 retry: 1716 if (vp != NULL) { 1717 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 1718 *mpp = NULL; 1719 if (error != EOPNOTSUPP) 1720 return (error); 1721 return (0); 1722 } 1723 } 1724 /* 1725 * If we are not suspended or have not yet reached suspended 1726 * mode, then let the operation proceed. 1727 */ 1728 if ((mp = *mpp) == NULL) 1729 return (0); 1730 1731 if (!vn_suspendable(mp)) { 1732 if (vp != NULL || (flags & V_MNTREF) != 0) 1733 vfs_rel(mp); 1734 return (0); 1735 } 1736 1737 /* 1738 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 1739 * a vfs_ref(). 1740 * As long as a vnode is not provided we need to acquire a 1741 * refcount for the provided mountpoint too, in order to 1742 * emulate a vfs_ref(). 1743 */ 1744 MNT_ILOCK(mp); 1745 if (vp == NULL && (flags & V_MNTREF) == 0) 1746 MNT_REF(mp); 1747 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) { 1748 mp->mnt_secondary_writes++; 1749 mp->mnt_secondary_accwrites++; 1750 MNT_IUNLOCK(mp); 1751 return (0); 1752 } 1753 if (flags & V_NOWAIT) { 1754 MNT_REL(mp); 1755 MNT_IUNLOCK(mp); 1756 return (EWOULDBLOCK); 1757 } 1758 /* 1759 * Wait for the suspension to finish. 1760 */ 1761 error = msleep(&mp->mnt_flag, MNT_MTX(mp), (PUSER - 1) | PDROP | 1762 ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0 ? (flags & PCATCH) : 0), 1763 "suspfs", 0); 1764 vfs_rel(mp); 1765 if (error == 0) 1766 goto retry; 1767 return (error); 1768 } 1769 1770 /* 1771 * Filesystem write operation has completed. If we are suspending and this 1772 * operation is the last one, notify the suspender that the suspension is 1773 * now in effect. 1774 */ 1775 void 1776 vn_finished_write(struct mount *mp) 1777 { 1778 if (mp == NULL || !vn_suspendable(mp)) 1779 return; 1780 MNT_ILOCK(mp); 1781 MNT_REL(mp); 1782 mp->mnt_writeopcount--; 1783 if (mp->mnt_writeopcount < 0) 1784 panic("vn_finished_write: neg cnt"); 1785 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 1786 mp->mnt_writeopcount <= 0) 1787 wakeup(&mp->mnt_writeopcount); 1788 MNT_IUNLOCK(mp); 1789 } 1790 1791 1792 /* 1793 * Filesystem secondary write operation has completed. If we are 1794 * suspending and this operation is the last one, notify the suspender 1795 * that the suspension is now in effect. 1796 */ 1797 void 1798 vn_finished_secondary_write(struct mount *mp) 1799 { 1800 if (mp == NULL || !vn_suspendable(mp)) 1801 return; 1802 MNT_ILOCK(mp); 1803 MNT_REL(mp); 1804 mp->mnt_secondary_writes--; 1805 if (mp->mnt_secondary_writes < 0) 1806 panic("vn_finished_secondary_write: neg cnt"); 1807 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 1808 mp->mnt_secondary_writes <= 0) 1809 wakeup(&mp->mnt_secondary_writes); 1810 MNT_IUNLOCK(mp); 1811 } 1812 1813 1814 1815 /* 1816 * Request a filesystem to suspend write operations. 1817 */ 1818 int 1819 vfs_write_suspend(struct mount *mp, int flags) 1820 { 1821 int error; 1822 1823 MPASS(vn_suspendable(mp)); 1824 1825 MNT_ILOCK(mp); 1826 if (mp->mnt_susp_owner == curthread) { 1827 MNT_IUNLOCK(mp); 1828 return (EALREADY); 1829 } 1830 while (mp->mnt_kern_flag & MNTK_SUSPEND) 1831 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0); 1832 1833 /* 1834 * Unmount holds a write reference on the mount point. If we 1835 * own busy reference and drain for writers, we deadlock with 1836 * the reference draining in the unmount path. Callers of 1837 * vfs_write_suspend() must specify VS_SKIP_UNMOUNT if 1838 * vfs_busy() reference is owned and caller is not in the 1839 * unmount context. 1840 */ 1841 if ((flags & VS_SKIP_UNMOUNT) != 0 && 1842 (mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) { 1843 MNT_IUNLOCK(mp); 1844 return (EBUSY); 1845 } 1846 1847 mp->mnt_kern_flag |= MNTK_SUSPEND; 1848 mp->mnt_susp_owner = curthread; 1849 if (mp->mnt_writeopcount > 0) 1850 (void) msleep(&mp->mnt_writeopcount, 1851 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0); 1852 else 1853 MNT_IUNLOCK(mp); 1854 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0) 1855 vfs_write_resume(mp, 0); 1856 return (error); 1857 } 1858 1859 /* 1860 * Request a filesystem to resume write operations. 1861 */ 1862 void 1863 vfs_write_resume(struct mount *mp, int flags) 1864 { 1865 1866 MPASS(vn_suspendable(mp)); 1867 1868 MNT_ILOCK(mp); 1869 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 1870 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner")); 1871 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 | 1872 MNTK_SUSPENDED); 1873 mp->mnt_susp_owner = NULL; 1874 wakeup(&mp->mnt_writeopcount); 1875 wakeup(&mp->mnt_flag); 1876 curthread->td_pflags &= ~TDP_IGNSUSP; 1877 if ((flags & VR_START_WRITE) != 0) { 1878 MNT_REF(mp); 1879 mp->mnt_writeopcount++; 1880 } 1881 MNT_IUNLOCK(mp); 1882 if ((flags & VR_NO_SUSPCLR) == 0) 1883 VFS_SUSP_CLEAN(mp); 1884 } else if ((flags & VR_START_WRITE) != 0) { 1885 MNT_REF(mp); 1886 vn_start_write_locked(mp, 0); 1887 } else { 1888 MNT_IUNLOCK(mp); 1889 } 1890 } 1891 1892 /* 1893 * Helper loop around vfs_write_suspend() for filesystem unmount VFS 1894 * methods. 1895 */ 1896 int 1897 vfs_write_suspend_umnt(struct mount *mp) 1898 { 1899 int error; 1900 1901 MPASS(vn_suspendable(mp)); 1902 KASSERT((curthread->td_pflags & TDP_IGNSUSP) == 0, 1903 ("vfs_write_suspend_umnt: recursed")); 1904 1905 /* dounmount() already called vn_start_write(). */ 1906 for (;;) { 1907 vn_finished_write(mp); 1908 error = vfs_write_suspend(mp, 0); 1909 if (error != 0) { 1910 vn_start_write(NULL, &mp, V_WAIT); 1911 return (error); 1912 } 1913 MNT_ILOCK(mp); 1914 if ((mp->mnt_kern_flag & MNTK_SUSPENDED) != 0) 1915 break; 1916 MNT_IUNLOCK(mp); 1917 vn_start_write(NULL, &mp, V_WAIT); 1918 } 1919 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2); 1920 wakeup(&mp->mnt_flag); 1921 MNT_IUNLOCK(mp); 1922 curthread->td_pflags |= TDP_IGNSUSP; 1923 return (0); 1924 } 1925 1926 /* 1927 * Implement kqueues for files by translating it to vnode operation. 1928 */ 1929 static int 1930 vn_kqfilter(struct file *fp, struct knote *kn) 1931 { 1932 1933 return (VOP_KQFILTER(fp->f_vnode, kn)); 1934 } 1935 1936 /* 1937 * Simplified in-kernel wrapper calls for extended attribute access. 1938 * Both calls pass in a NULL credential, authorizing as "kernel" access. 1939 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 1940 */ 1941 int 1942 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 1943 const char *attrname, int *buflen, char *buf, struct thread *td) 1944 { 1945 struct uio auio; 1946 struct iovec iov; 1947 int error; 1948 1949 iov.iov_len = *buflen; 1950 iov.iov_base = buf; 1951 1952 auio.uio_iov = &iov; 1953 auio.uio_iovcnt = 1; 1954 auio.uio_rw = UIO_READ; 1955 auio.uio_segflg = UIO_SYSSPACE; 1956 auio.uio_td = td; 1957 auio.uio_offset = 0; 1958 auio.uio_resid = *buflen; 1959 1960 if ((ioflg & IO_NODELOCKED) == 0) 1961 vn_lock(vp, LK_SHARED | LK_RETRY); 1962 1963 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 1964 1965 /* authorize attribute retrieval as kernel */ 1966 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL, 1967 td); 1968 1969 if ((ioflg & IO_NODELOCKED) == 0) 1970 VOP_UNLOCK(vp, 0); 1971 1972 if (error == 0) { 1973 *buflen = *buflen - auio.uio_resid; 1974 } 1975 1976 return (error); 1977 } 1978 1979 /* 1980 * XXX failure mode if partially written? 1981 */ 1982 int 1983 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 1984 const char *attrname, int buflen, char *buf, struct thread *td) 1985 { 1986 struct uio auio; 1987 struct iovec iov; 1988 struct mount *mp; 1989 int error; 1990 1991 iov.iov_len = buflen; 1992 iov.iov_base = buf; 1993 1994 auio.uio_iov = &iov; 1995 auio.uio_iovcnt = 1; 1996 auio.uio_rw = UIO_WRITE; 1997 auio.uio_segflg = UIO_SYSSPACE; 1998 auio.uio_td = td; 1999 auio.uio_offset = 0; 2000 auio.uio_resid = buflen; 2001 2002 if ((ioflg & IO_NODELOCKED) == 0) { 2003 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 2004 return (error); 2005 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2006 } 2007 2008 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2009 2010 /* authorize attribute setting as kernel */ 2011 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td); 2012 2013 if ((ioflg & IO_NODELOCKED) == 0) { 2014 vn_finished_write(mp); 2015 VOP_UNLOCK(vp, 0); 2016 } 2017 2018 return (error); 2019 } 2020 2021 int 2022 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 2023 const char *attrname, struct thread *td) 2024 { 2025 struct mount *mp; 2026 int error; 2027 2028 if ((ioflg & IO_NODELOCKED) == 0) { 2029 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 2030 return (error); 2031 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2032 } 2033 2034 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2035 2036 /* authorize attribute removal as kernel */ 2037 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td); 2038 if (error == EOPNOTSUPP) 2039 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, 2040 NULL, td); 2041 2042 if ((ioflg & IO_NODELOCKED) == 0) { 2043 vn_finished_write(mp); 2044 VOP_UNLOCK(vp, 0); 2045 } 2046 2047 return (error); 2048 } 2049 2050 static int 2051 vn_get_ino_alloc_vget(struct mount *mp, void *arg, int lkflags, 2052 struct vnode **rvp) 2053 { 2054 2055 return (VFS_VGET(mp, *(ino_t *)arg, lkflags, rvp)); 2056 } 2057 2058 int 2059 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp) 2060 { 2061 2062 return (vn_vget_ino_gen(vp, vn_get_ino_alloc_vget, &ino, 2063 lkflags, rvp)); 2064 } 2065 2066 int 2067 vn_vget_ino_gen(struct vnode *vp, vn_get_ino_t alloc, void *alloc_arg, 2068 int lkflags, struct vnode **rvp) 2069 { 2070 struct mount *mp; 2071 int ltype, error; 2072 2073 ASSERT_VOP_LOCKED(vp, "vn_vget_ino_get"); 2074 mp = vp->v_mount; 2075 ltype = VOP_ISLOCKED(vp); 2076 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED, 2077 ("vn_vget_ino: vp not locked")); 2078 error = vfs_busy(mp, MBF_NOWAIT); 2079 if (error != 0) { 2080 vfs_ref(mp); 2081 VOP_UNLOCK(vp, 0); 2082 error = vfs_busy(mp, 0); 2083 vn_lock(vp, ltype | LK_RETRY); 2084 vfs_rel(mp); 2085 if (error != 0) 2086 return (ENOENT); 2087 if (vp->v_iflag & VI_DOOMED) { 2088 vfs_unbusy(mp); 2089 return (ENOENT); 2090 } 2091 } 2092 VOP_UNLOCK(vp, 0); 2093 error = alloc(mp, alloc_arg, lkflags, rvp); 2094 vfs_unbusy(mp); 2095 if (*rvp != vp) 2096 vn_lock(vp, ltype | LK_RETRY); 2097 if (vp->v_iflag & VI_DOOMED) { 2098 if (error == 0) { 2099 if (*rvp == vp) 2100 vunref(vp); 2101 else 2102 vput(*rvp); 2103 } 2104 error = ENOENT; 2105 } 2106 return (error); 2107 } 2108 2109 int 2110 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, 2111 struct thread *td) 2112 { 2113 2114 if (vp->v_type != VREG || td == NULL) 2115 return (0); 2116 if ((uoff_t)uio->uio_offset + uio->uio_resid > 2117 lim_cur(td, RLIMIT_FSIZE)) { 2118 PROC_LOCK(td->td_proc); 2119 kern_psignal(td->td_proc, SIGXFSZ); 2120 PROC_UNLOCK(td->td_proc); 2121 return (EFBIG); 2122 } 2123 return (0); 2124 } 2125 2126 int 2127 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 2128 struct thread *td) 2129 { 2130 struct vnode *vp; 2131 2132 vp = fp->f_vnode; 2133 #ifdef AUDIT 2134 vn_lock(vp, LK_SHARED | LK_RETRY); 2135 AUDIT_ARG_VNODE1(vp); 2136 VOP_UNLOCK(vp, 0); 2137 #endif 2138 return (setfmode(td, active_cred, vp, mode)); 2139 } 2140 2141 int 2142 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 2143 struct thread *td) 2144 { 2145 struct vnode *vp; 2146 2147 vp = fp->f_vnode; 2148 #ifdef AUDIT 2149 vn_lock(vp, LK_SHARED | LK_RETRY); 2150 AUDIT_ARG_VNODE1(vp); 2151 VOP_UNLOCK(vp, 0); 2152 #endif 2153 return (setfown(td, active_cred, vp, uid, gid)); 2154 } 2155 2156 void 2157 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end) 2158 { 2159 vm_object_t object; 2160 2161 if ((object = vp->v_object) == NULL) 2162 return; 2163 VM_OBJECT_WLOCK(object); 2164 vm_object_page_remove(object, start, end, 0); 2165 VM_OBJECT_WUNLOCK(object); 2166 } 2167 2168 int 2169 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred) 2170 { 2171 struct vattr va; 2172 daddr_t bn, bnp; 2173 uint64_t bsize; 2174 off_t noff; 2175 int error; 2176 2177 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA, 2178 ("Wrong command %lu", cmd)); 2179 2180 if (vn_lock(vp, LK_SHARED) != 0) 2181 return (EBADF); 2182 if (vp->v_type != VREG) { 2183 error = ENOTTY; 2184 goto unlock; 2185 } 2186 error = VOP_GETATTR(vp, &va, cred); 2187 if (error != 0) 2188 goto unlock; 2189 noff = *off; 2190 if (noff >= va.va_size) { 2191 error = ENXIO; 2192 goto unlock; 2193 } 2194 bsize = vp->v_mount->mnt_stat.f_iosize; 2195 for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize - 2196 noff % bsize) { 2197 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL); 2198 if (error == EOPNOTSUPP) { 2199 error = ENOTTY; 2200 goto unlock; 2201 } 2202 if ((bnp == -1 && cmd == FIOSEEKHOLE) || 2203 (bnp != -1 && cmd == FIOSEEKDATA)) { 2204 noff = bn * bsize; 2205 if (noff < *off) 2206 noff = *off; 2207 goto unlock; 2208 } 2209 } 2210 if (noff > va.va_size) 2211 noff = va.va_size; 2212 /* noff == va.va_size. There is an implicit hole at the end of file. */ 2213 if (cmd == FIOSEEKDATA) 2214 error = ENXIO; 2215 unlock: 2216 VOP_UNLOCK(vp, 0); 2217 if (error == 0) 2218 *off = noff; 2219 return (error); 2220 } 2221 2222 int 2223 vn_seek(struct file *fp, off_t offset, int whence, struct thread *td) 2224 { 2225 struct ucred *cred; 2226 struct vnode *vp; 2227 struct vattr vattr; 2228 off_t foffset, size; 2229 int error, noneg; 2230 2231 cred = td->td_ucred; 2232 vp = fp->f_vnode; 2233 foffset = foffset_lock(fp, 0); 2234 noneg = (vp->v_type != VCHR); 2235 error = 0; 2236 switch (whence) { 2237 case L_INCR: 2238 if (noneg && 2239 (foffset < 0 || 2240 (offset > 0 && foffset > OFF_MAX - offset))) { 2241 error = EOVERFLOW; 2242 break; 2243 } 2244 offset += foffset; 2245 break; 2246 case L_XTND: 2247 vn_lock(vp, LK_SHARED | LK_RETRY); 2248 error = VOP_GETATTR(vp, &vattr, cred); 2249 VOP_UNLOCK(vp, 0); 2250 if (error) 2251 break; 2252 2253 /* 2254 * If the file references a disk device, then fetch 2255 * the media size and use that to determine the ending 2256 * offset. 2257 */ 2258 if (vattr.va_size == 0 && vp->v_type == VCHR && 2259 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0) 2260 vattr.va_size = size; 2261 if (noneg && 2262 (vattr.va_size > OFF_MAX || 2263 (offset > 0 && vattr.va_size > OFF_MAX - offset))) { 2264 error = EOVERFLOW; 2265 break; 2266 } 2267 offset += vattr.va_size; 2268 break; 2269 case L_SET: 2270 break; 2271 case SEEK_DATA: 2272 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td); 2273 break; 2274 case SEEK_HOLE: 2275 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td); 2276 break; 2277 default: 2278 error = EINVAL; 2279 } 2280 if (error == 0 && noneg && offset < 0) 2281 error = EINVAL; 2282 if (error != 0) 2283 goto drop; 2284 VFS_KNOTE_UNLOCKED(vp, 0); 2285 td->td_uretoff.tdu_off = offset; 2286 drop: 2287 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0); 2288 return (error); 2289 } 2290 2291 int 2292 vn_utimes_perm(struct vnode *vp, struct vattr *vap, struct ucred *cred, 2293 struct thread *td) 2294 { 2295 int error; 2296 2297 /* 2298 * Grant permission if the caller is the owner of the file, or 2299 * the super-user, or has ACL_WRITE_ATTRIBUTES permission on 2300 * on the file. If the time pointer is null, then write 2301 * permission on the file is also sufficient. 2302 * 2303 * From NFSv4.1, draft 21, 6.2.1.3.1, Discussion of Mask Attributes: 2304 * A user having ACL_WRITE_DATA or ACL_WRITE_ATTRIBUTES 2305 * will be allowed to set the times [..] to the current 2306 * server time. 2307 */ 2308 error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred, td); 2309 if (error != 0 && (vap->va_vaflags & VA_UTIMES_NULL) != 0) 2310 error = VOP_ACCESS(vp, VWRITE, cred, td); 2311 return (error); 2312 } 2313 2314 int 2315 vn_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 2316 { 2317 struct vnode *vp; 2318 int error; 2319 2320 if (fp->f_type == DTYPE_FIFO) 2321 kif->kf_type = KF_TYPE_FIFO; 2322 else 2323 kif->kf_type = KF_TYPE_VNODE; 2324 vp = fp->f_vnode; 2325 vref(vp); 2326 FILEDESC_SUNLOCK(fdp); 2327 error = vn_fill_kinfo_vnode(vp, kif); 2328 vrele(vp); 2329 FILEDESC_SLOCK(fdp); 2330 return (error); 2331 } 2332 2333 static inline void 2334 vn_fill_junk(struct kinfo_file *kif) 2335 { 2336 size_t len, olen; 2337 2338 /* 2339 * Simulate vn_fullpath returning changing values for a given 2340 * vp during e.g. coredump. 2341 */ 2342 len = (arc4random() % (sizeof(kif->kf_path) - 2)) + 1; 2343 olen = strlen(kif->kf_path); 2344 if (len < olen) 2345 strcpy(&kif->kf_path[len - 1], "$"); 2346 else 2347 for (; olen < len; olen++) 2348 strcpy(&kif->kf_path[olen], "A"); 2349 } 2350 2351 int 2352 vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif) 2353 { 2354 struct vattr va; 2355 char *fullpath, *freepath; 2356 int error; 2357 2358 kif->kf_un.kf_file.kf_file_type = vntype_to_kinfo(vp->v_type); 2359 freepath = NULL; 2360 fullpath = "-"; 2361 error = vn_fullpath(curthread, vp, &fullpath, &freepath); 2362 if (error == 0) { 2363 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); 2364 } 2365 if (freepath != NULL) 2366 free(freepath, M_TEMP); 2367 2368 KFAIL_POINT_CODE(DEBUG_FP, fill_kinfo_vnode__random_path, 2369 vn_fill_junk(kif); 2370 ); 2371 2372 /* 2373 * Retrieve vnode attributes. 2374 */ 2375 va.va_fsid = VNOVAL; 2376 va.va_rdev = NODEV; 2377 vn_lock(vp, LK_SHARED | LK_RETRY); 2378 error = VOP_GETATTR(vp, &va, curthread->td_ucred); 2379 VOP_UNLOCK(vp, 0); 2380 if (error != 0) 2381 return (error); 2382 if (va.va_fsid != VNOVAL) 2383 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid; 2384 else 2385 kif->kf_un.kf_file.kf_file_fsid = 2386 vp->v_mount->mnt_stat.f_fsid.val[0]; 2387 kif->kf_un.kf_file.kf_file_fsid_freebsd11 = 2388 kif->kf_un.kf_file.kf_file_fsid; /* truncate */ 2389 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid; 2390 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode); 2391 kif->kf_un.kf_file.kf_file_size = va.va_size; 2392 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev; 2393 kif->kf_un.kf_file.kf_file_rdev_freebsd11 = 2394 kif->kf_un.kf_file.kf_file_rdev; /* truncate */ 2395 return (0); 2396 } 2397 2398 int 2399 vn_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size, 2400 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff, 2401 struct thread *td) 2402 { 2403 #ifdef HWPMC_HOOKS 2404 struct pmckern_map_in pkm; 2405 #endif 2406 struct mount *mp; 2407 struct vnode *vp; 2408 vm_object_t object; 2409 vm_prot_t maxprot; 2410 boolean_t writecounted; 2411 int error; 2412 2413 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \ 2414 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) 2415 /* 2416 * POSIX shared-memory objects are defined to have 2417 * kernel persistence, and are not defined to support 2418 * read(2)/write(2) -- or even open(2). Thus, we can 2419 * use MAP_ASYNC to trade on-disk coherence for speed. 2420 * The shm_open(3) library routine turns on the FPOSIXSHM 2421 * flag to request this behavior. 2422 */ 2423 if ((fp->f_flag & FPOSIXSHM) != 0) 2424 flags |= MAP_NOSYNC; 2425 #endif 2426 vp = fp->f_vnode; 2427 2428 /* 2429 * Ensure that file and memory protections are 2430 * compatible. Note that we only worry about 2431 * writability if mapping is shared; in this case, 2432 * current and max prot are dictated by the open file. 2433 * XXX use the vnode instead? Problem is: what 2434 * credentials do we use for determination? What if 2435 * proc does a setuid? 2436 */ 2437 mp = vp->v_mount; 2438 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) { 2439 maxprot = VM_PROT_NONE; 2440 if ((prot & VM_PROT_EXECUTE) != 0) 2441 return (EACCES); 2442 } else 2443 maxprot = VM_PROT_EXECUTE; 2444 if ((fp->f_flag & FREAD) != 0) 2445 maxprot |= VM_PROT_READ; 2446 else if ((prot & VM_PROT_READ) != 0) 2447 return (EACCES); 2448 2449 /* 2450 * If we are sharing potential changes via MAP_SHARED and we 2451 * are trying to get write permission although we opened it 2452 * without asking for it, bail out. 2453 */ 2454 if ((flags & MAP_SHARED) != 0) { 2455 if ((fp->f_flag & FWRITE) != 0) 2456 maxprot |= VM_PROT_WRITE; 2457 else if ((prot & VM_PROT_WRITE) != 0) 2458 return (EACCES); 2459 } else { 2460 maxprot |= VM_PROT_WRITE; 2461 cap_maxprot |= VM_PROT_WRITE; 2462 } 2463 maxprot &= cap_maxprot; 2464 2465 /* 2466 * For regular files and shared memory, POSIX requires that 2467 * the value of foff be a legitimate offset within the data 2468 * object. In particular, negative offsets are invalid. 2469 * Blocking negative offsets and overflows here avoids 2470 * possible wraparound or user-level access into reserved 2471 * ranges of the data object later. In contrast, POSIX does 2472 * not dictate how offsets are used by device drivers, so in 2473 * the case of a device mapping a negative offset is passed 2474 * on. 2475 */ 2476 if ( 2477 #ifdef _LP64 2478 size > OFF_MAX || 2479 #endif 2480 foff < 0 || foff > OFF_MAX - size) 2481 return (EINVAL); 2482 2483 writecounted = FALSE; 2484 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags, vp, 2485 &foff, &object, &writecounted); 2486 if (error != 0) 2487 return (error); 2488 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object, 2489 foff, writecounted, td); 2490 if (error != 0) { 2491 /* 2492 * If this mapping was accounted for in the vnode's 2493 * writecount, then undo that now. 2494 */ 2495 if (writecounted) 2496 vnode_pager_release_writecount(object, 0, size); 2497 vm_object_deallocate(object); 2498 } 2499 #ifdef HWPMC_HOOKS 2500 /* Inform hwpmc(4) if an executable is being mapped. */ 2501 if (PMC_HOOK_INSTALLED(PMC_FN_MMAP)) { 2502 if ((prot & VM_PROT_EXECUTE) != 0 && error == 0) { 2503 pkm.pm_file = vp; 2504 pkm.pm_address = (uintptr_t) *addr; 2505 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_MMAP, (void *) &pkm); 2506 } 2507 } 2508 #endif 2509 return (error); 2510 } 2511 2512 void 2513 vn_fsid(struct vnode *vp, struct vattr *va) 2514 { 2515 fsid_t *f; 2516 2517 f = &vp->v_mount->mnt_stat.f_fsid; 2518 va->va_fsid = (uint32_t)f->val[1]; 2519 va->va_fsid <<= sizeof(f->val[1]) * NBBY; 2520 va->va_fsid += (uint32_t)f->val[0]; 2521 } 2522 2523 int 2524 vn_fsync_buf(struct vnode *vp, int waitfor) 2525 { 2526 struct buf *bp, *nbp; 2527 struct bufobj *bo; 2528 struct mount *mp; 2529 int error, maxretry; 2530 2531 error = 0; 2532 maxretry = 10000; /* large, arbitrarily chosen */ 2533 mp = NULL; 2534 if (vp->v_type == VCHR) { 2535 VI_LOCK(vp); 2536 mp = vp->v_rdev->si_mountpt; 2537 VI_UNLOCK(vp); 2538 } 2539 bo = &vp->v_bufobj; 2540 BO_LOCK(bo); 2541 loop1: 2542 /* 2543 * MARK/SCAN initialization to avoid infinite loops. 2544 */ 2545 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 2546 bp->b_vflags &= ~BV_SCANNED; 2547 bp->b_error = 0; 2548 } 2549 2550 /* 2551 * Flush all dirty buffers associated with a vnode. 2552 */ 2553 loop2: 2554 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2555 if ((bp->b_vflags & BV_SCANNED) != 0) 2556 continue; 2557 bp->b_vflags |= BV_SCANNED; 2558 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 2559 if (waitfor != MNT_WAIT) 2560 continue; 2561 if (BUF_LOCK(bp, 2562 LK_EXCLUSIVE | LK_INTERLOCK | LK_SLEEPFAIL, 2563 BO_LOCKPTR(bo)) != 0) { 2564 BO_LOCK(bo); 2565 goto loop1; 2566 } 2567 BO_LOCK(bo); 2568 } 2569 BO_UNLOCK(bo); 2570 KASSERT(bp->b_bufobj == bo, 2571 ("bp %p wrong b_bufobj %p should be %p", 2572 bp, bp->b_bufobj, bo)); 2573 if ((bp->b_flags & B_DELWRI) == 0) 2574 panic("fsync: not dirty"); 2575 if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) { 2576 vfs_bio_awrite(bp); 2577 } else { 2578 bremfree(bp); 2579 bawrite(bp); 2580 } 2581 if (maxretry < 1000) 2582 pause("dirty", hz < 1000 ? 1 : hz / 1000); 2583 BO_LOCK(bo); 2584 goto loop2; 2585 } 2586 2587 /* 2588 * If synchronous the caller expects us to completely resolve all 2589 * dirty buffers in the system. Wait for in-progress I/O to 2590 * complete (which could include background bitmap writes), then 2591 * retry if dirty blocks still exist. 2592 */ 2593 if (waitfor == MNT_WAIT) { 2594 bufobj_wwait(bo, 0, 0); 2595 if (bo->bo_dirty.bv_cnt > 0) { 2596 /* 2597 * If we are unable to write any of these buffers 2598 * then we fail now rather than trying endlessly 2599 * to write them out. 2600 */ 2601 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 2602 if ((error = bp->b_error) != 0) 2603 break; 2604 if ((mp != NULL && mp->mnt_secondary_writes > 0) || 2605 (error == 0 && --maxretry >= 0)) 2606 goto loop1; 2607 if (error == 0) 2608 error = EAGAIN; 2609 } 2610 } 2611 BO_UNLOCK(bo); 2612 if (error != 0) 2613 vn_printf(vp, "fsync: giving up on dirty (error = %d) ", error); 2614 2615 return (error); 2616 } 2617