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