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/prng.h> 74 #include <sys/sx.h> 75 #include <sys/sleepqueue.h> 76 #include <sys/sysctl.h> 77 #include <sys/ttycom.h> 78 #include <sys/conf.h> 79 #include <sys/syslog.h> 80 #include <sys/unistd.h> 81 #include <sys/user.h> 82 83 #include <security/audit/audit.h> 84 #include <security/mac/mac_framework.h> 85 86 #include <vm/vm.h> 87 #include <vm/vm_extern.h> 88 #include <vm/pmap.h> 89 #include <vm/vm_map.h> 90 #include <vm/vm_object.h> 91 #include <vm/vm_page.h> 92 #include <vm/vm_pager.h> 93 94 #ifdef HWPMC_HOOKS 95 #include <sys/pmckern.h> 96 #endif 97 98 static fo_rdwr_t vn_read; 99 static fo_rdwr_t vn_write; 100 static fo_rdwr_t vn_io_fault; 101 static fo_truncate_t vn_truncate; 102 static fo_ioctl_t vn_ioctl; 103 static fo_poll_t vn_poll; 104 static fo_kqfilter_t vn_kqfilter; 105 static fo_stat_t vn_statfile; 106 static fo_close_t vn_closefile; 107 static fo_mmap_t vn_mmap; 108 static fo_fallocate_t vn_fallocate; 109 110 struct fileops vnops = { 111 .fo_read = vn_io_fault, 112 .fo_write = vn_io_fault, 113 .fo_truncate = vn_truncate, 114 .fo_ioctl = vn_ioctl, 115 .fo_poll = vn_poll, 116 .fo_kqfilter = vn_kqfilter, 117 .fo_stat = vn_statfile, 118 .fo_close = vn_closefile, 119 .fo_chmod = vn_chmod, 120 .fo_chown = vn_chown, 121 .fo_sendfile = vn_sendfile, 122 .fo_seek = vn_seek, 123 .fo_fill_kinfo = vn_fill_kinfo, 124 .fo_mmap = vn_mmap, 125 .fo_fallocate = vn_fallocate, 126 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE 127 }; 128 129 const u_int io_hold_cnt = 16; 130 static int vn_io_fault_enable = 1; 131 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_enable, CTLFLAG_RWTUN, 132 &vn_io_fault_enable, 0, "Enable vn_io_fault lock avoidance"); 133 static int vn_io_fault_prefault = 0; 134 SYSCTL_INT(_debug, OID_AUTO, vn_io_fault_prefault, CTLFLAG_RWTUN, 135 &vn_io_fault_prefault, 0, "Enable vn_io_fault prefaulting"); 136 static int vn_io_pgcache_read_enable = 1; 137 SYSCTL_INT(_debug, OID_AUTO, vn_io_pgcache_read_enable, CTLFLAG_RWTUN, 138 &vn_io_pgcache_read_enable, 0, 139 "Enable copying from page cache for reads, avoiding fs"); 140 static u_long vn_io_faults_cnt; 141 SYSCTL_ULONG(_debug, OID_AUTO, vn_io_faults, CTLFLAG_RD, 142 &vn_io_faults_cnt, 0, "Count of vn_io_fault lock avoidance triggers"); 143 144 static int vfs_allow_read_dir = 0; 145 SYSCTL_INT(_security_bsd, OID_AUTO, allow_read_dir, CTLFLAG_RW, 146 &vfs_allow_read_dir, 0, 147 "Enable read(2) of directory by root for filesystems that support it"); 148 149 /* 150 * Returns true if vn_io_fault mode of handling the i/o request should 151 * be used. 152 */ 153 static bool 154 do_vn_io_fault(struct vnode *vp, struct uio *uio) 155 { 156 struct mount *mp; 157 158 return (uio->uio_segflg == UIO_USERSPACE && vp->v_type == VREG && 159 (mp = vp->v_mount) != NULL && 160 (mp->mnt_kern_flag & MNTK_NO_IOPF) != 0 && vn_io_fault_enable); 161 } 162 163 /* 164 * Structure used to pass arguments to vn_io_fault1(), to do either 165 * file- or vnode-based I/O calls. 166 */ 167 struct vn_io_fault_args { 168 enum { 169 VN_IO_FAULT_FOP, 170 VN_IO_FAULT_VOP 171 } kind; 172 struct ucred *cred; 173 int flags; 174 union { 175 struct fop_args_tag { 176 struct file *fp; 177 fo_rdwr_t *doio; 178 } fop_args; 179 struct vop_args_tag { 180 struct vnode *vp; 181 } vop_args; 182 } args; 183 }; 184 185 static int vn_io_fault1(struct vnode *vp, struct uio *uio, 186 struct vn_io_fault_args *args, struct thread *td); 187 188 int 189 vn_open(struct nameidata *ndp, int *flagp, int cmode, struct file *fp) 190 { 191 struct thread *td = ndp->ni_cnd.cn_thread; 192 193 return (vn_open_cred(ndp, flagp, cmode, 0, td->td_ucred, fp)); 194 } 195 196 static uint64_t 197 open2nameif(int fmode, u_int vn_open_flags) 198 { 199 uint64_t res; 200 201 res = ISOPEN | LOCKLEAF; 202 if ((fmode & O_RESOLVE_BENEATH) != 0) 203 res |= RBENEATH; 204 if ((vn_open_flags & VN_OPEN_NOAUDIT) == 0) 205 res |= AUDITVNODE1; 206 if ((vn_open_flags & VN_OPEN_NOCAPCHECK) != 0) 207 res |= NOCAPCHECK; 208 return (res); 209 } 210 211 /* 212 * Common code for vnode open operations via a name lookup. 213 * Lookup the vnode and invoke VOP_CREATE if needed. 214 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 215 * 216 * Note that this does NOT free nameidata for the successful case, 217 * due to the NDINIT being done elsewhere. 218 */ 219 int 220 vn_open_cred(struct nameidata *ndp, int *flagp, int cmode, u_int vn_open_flags, 221 struct ucred *cred, struct file *fp) 222 { 223 struct vnode *vp; 224 struct mount *mp; 225 struct thread *td = ndp->ni_cnd.cn_thread; 226 struct vattr vat; 227 struct vattr *vap = &vat; 228 int fmode, error; 229 bool first_open; 230 231 restart: 232 first_open = false; 233 fmode = *flagp; 234 if ((fmode & (O_CREAT | O_EXCL | O_DIRECTORY)) == (O_CREAT | 235 O_EXCL | O_DIRECTORY)) 236 return (EINVAL); 237 else if ((fmode & (O_CREAT | O_DIRECTORY)) == O_CREAT) { 238 ndp->ni_cnd.cn_nameiop = CREATE; 239 ndp->ni_cnd.cn_flags = open2nameif(fmode, vn_open_flags); 240 /* 241 * Set NOCACHE to avoid flushing the cache when 242 * rolling in many files at once. 243 * 244 * Set NC_KEEPPOSENTRY to keep positive entries if they already 245 * exist despite NOCACHE. 246 */ 247 ndp->ni_cnd.cn_flags |= LOCKPARENT | NOCACHE | NC_KEEPPOSENTRY; 248 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0) 249 ndp->ni_cnd.cn_flags |= FOLLOW; 250 if ((vn_open_flags & VN_OPEN_INVFS) == 0) 251 bwillwrite(); 252 if ((error = namei(ndp)) != 0) 253 return (error); 254 if (ndp->ni_vp == NULL) { 255 VATTR_NULL(vap); 256 vap->va_type = VREG; 257 vap->va_mode = cmode; 258 if (fmode & O_EXCL) 259 vap->va_vaflags |= VA_EXCLUSIVE; 260 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) { 261 NDFREE(ndp, NDF_ONLY_PNBUF); 262 vput(ndp->ni_dvp); 263 if ((error = vn_start_write(NULL, &mp, 264 V_XSLEEP | PCATCH)) != 0) 265 return (error); 266 NDREINIT(ndp); 267 goto restart; 268 } 269 if ((vn_open_flags & VN_OPEN_NAMECACHE) != 0) 270 ndp->ni_cnd.cn_flags |= MAKEENTRY; 271 #ifdef MAC 272 error = mac_vnode_check_create(cred, ndp->ni_dvp, 273 &ndp->ni_cnd, vap); 274 if (error == 0) 275 #endif 276 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 277 &ndp->ni_cnd, vap); 278 vp = ndp->ni_vp; 279 if (error == 0 && (fmode & O_EXCL) != 0 && 280 (fmode & (O_EXLOCK | O_SHLOCK)) != 0) { 281 VI_LOCK(vp); 282 vp->v_iflag |= VI_FOPENING; 283 VI_UNLOCK(vp); 284 first_open = true; 285 } 286 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &vp : NULL, 287 false); 288 vn_finished_write(mp); 289 if (error) { 290 NDFREE(ndp, NDF_ONLY_PNBUF); 291 if (error == ERELOOKUP) { 292 NDREINIT(ndp); 293 goto restart; 294 } 295 return (error); 296 } 297 fmode &= ~O_TRUNC; 298 } else { 299 if (ndp->ni_dvp == ndp->ni_vp) 300 vrele(ndp->ni_dvp); 301 else 302 vput(ndp->ni_dvp); 303 ndp->ni_dvp = NULL; 304 vp = ndp->ni_vp; 305 if (fmode & O_EXCL) { 306 error = EEXIST; 307 goto bad; 308 } 309 if (vp->v_type == VDIR) { 310 error = EISDIR; 311 goto bad; 312 } 313 fmode &= ~O_CREAT; 314 } 315 } else { 316 ndp->ni_cnd.cn_nameiop = LOOKUP; 317 ndp->ni_cnd.cn_flags = open2nameif(fmode, vn_open_flags); 318 ndp->ni_cnd.cn_flags |= (fmode & O_NOFOLLOW) != 0 ? NOFOLLOW : 319 FOLLOW; 320 if ((fmode & FWRITE) == 0) 321 ndp->ni_cnd.cn_flags |= LOCKSHARED; 322 if ((error = namei(ndp)) != 0) 323 return (error); 324 vp = ndp->ni_vp; 325 } 326 error = vn_open_vnode(vp, fmode, cred, td, fp); 327 if (first_open) { 328 VI_LOCK(vp); 329 vp->v_iflag &= ~VI_FOPENING; 330 wakeup(vp); 331 VI_UNLOCK(vp); 332 } 333 if (error) 334 goto bad; 335 *flagp = fmode; 336 return (0); 337 bad: 338 NDFREE(ndp, NDF_ONLY_PNBUF); 339 vput(vp); 340 *flagp = fmode; 341 ndp->ni_vp = NULL; 342 return (error); 343 } 344 345 static int 346 vn_open_vnode_advlock(struct vnode *vp, int fmode, struct file *fp) 347 { 348 struct flock lf; 349 int error, lock_flags, type; 350 351 ASSERT_VOP_LOCKED(vp, "vn_open_vnode_advlock"); 352 if ((fmode & (O_EXLOCK | O_SHLOCK)) == 0) 353 return (0); 354 KASSERT(fp != NULL, ("open with flock requires fp")); 355 if (fp->f_type != DTYPE_NONE && fp->f_type != DTYPE_VNODE) 356 return (EOPNOTSUPP); 357 358 lock_flags = VOP_ISLOCKED(vp); 359 VOP_UNLOCK(vp); 360 361 lf.l_whence = SEEK_SET; 362 lf.l_start = 0; 363 lf.l_len = 0; 364 lf.l_type = (fmode & O_EXLOCK) != 0 ? F_WRLCK : F_RDLCK; 365 type = F_FLOCK; 366 if ((fmode & FNONBLOCK) == 0) 367 type |= F_WAIT; 368 if ((fmode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 369 type |= F_FIRSTOPEN; 370 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type); 371 if (error == 0) 372 fp->f_flag |= FHASLOCK; 373 374 vn_lock(vp, lock_flags | LK_RETRY); 375 return (error); 376 } 377 378 /* 379 * Common code for vnode open operations once a vnode is located. 380 * Check permissions, and call the VOP_OPEN routine. 381 */ 382 int 383 vn_open_vnode(struct vnode *vp, int fmode, struct ucred *cred, 384 struct thread *td, struct file *fp) 385 { 386 accmode_t accmode; 387 int error; 388 389 if (vp->v_type == VLNK) 390 return (EMLINK); 391 if (vp->v_type == VSOCK) 392 return (EOPNOTSUPP); 393 if (vp->v_type != VDIR && fmode & O_DIRECTORY) 394 return (ENOTDIR); 395 accmode = 0; 396 if (fmode & (FWRITE | O_TRUNC)) { 397 if (vp->v_type == VDIR) 398 return (EISDIR); 399 accmode |= VWRITE; 400 } 401 if (fmode & FREAD) 402 accmode |= VREAD; 403 if (fmode & FEXEC) 404 accmode |= VEXEC; 405 if ((fmode & O_APPEND) && (fmode & FWRITE)) 406 accmode |= VAPPEND; 407 #ifdef MAC 408 if (fmode & O_CREAT) 409 accmode |= VCREAT; 410 if (fmode & O_VERIFY) 411 accmode |= VVERIFY; 412 error = mac_vnode_check_open(cred, vp, accmode); 413 if (error) 414 return (error); 415 416 accmode &= ~(VCREAT | VVERIFY); 417 #endif 418 if ((fmode & O_CREAT) == 0 && accmode != 0) { 419 error = VOP_ACCESS(vp, accmode, cred, td); 420 if (error != 0) 421 return (error); 422 } 423 if (vp->v_type == VFIFO && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) 424 vn_lock(vp, LK_UPGRADE | LK_RETRY); 425 error = VOP_OPEN(vp, fmode, cred, td, fp); 426 if (error != 0) 427 return (error); 428 429 error = vn_open_vnode_advlock(vp, fmode, fp); 430 if (error == 0 && (fmode & FWRITE) != 0) { 431 error = VOP_ADD_WRITECOUNT(vp, 1); 432 if (error == 0) { 433 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", 434 __func__, vp, vp->v_writecount); 435 } 436 } 437 438 /* 439 * Error from advlock or VOP_ADD_WRITECOUNT() still requires 440 * calling VOP_CLOSE() to pair with earlier VOP_OPEN(). 441 * Arrange for that by having fdrop() to use vn_closefile(). 442 */ 443 if (error != 0) { 444 fp->f_flag |= FOPENFAILED; 445 fp->f_vnode = vp; 446 if (fp->f_ops == &badfileops) { 447 fp->f_type = DTYPE_VNODE; 448 fp->f_ops = &vnops; 449 } 450 vref(vp); 451 } 452 453 ASSERT_VOP_LOCKED(vp, "vn_open_vnode"); 454 return (error); 455 456 } 457 458 /* 459 * Check for write permissions on the specified vnode. 460 * Prototype text segments cannot be written. 461 * It is racy. 462 */ 463 int 464 vn_writechk(struct vnode *vp) 465 { 466 467 ASSERT_VOP_LOCKED(vp, "vn_writechk"); 468 /* 469 * If there's shared text associated with 470 * the vnode, try to free it up once. If 471 * we fail, we can't allow writing. 472 */ 473 if (VOP_IS_TEXT(vp)) 474 return (ETXTBSY); 475 476 return (0); 477 } 478 479 /* 480 * Vnode close call 481 */ 482 static int 483 vn_close1(struct vnode *vp, int flags, struct ucred *file_cred, 484 struct thread *td, bool keep_ref) 485 { 486 struct mount *mp; 487 int error, lock_flags; 488 489 if (vp->v_type != VFIFO && (flags & FWRITE) == 0 && 490 MNT_EXTENDED_SHARED(vp->v_mount)) 491 lock_flags = LK_SHARED; 492 else 493 lock_flags = LK_EXCLUSIVE; 494 495 vn_start_write(vp, &mp, V_WAIT); 496 vn_lock(vp, lock_flags | LK_RETRY); 497 AUDIT_ARG_VNODE1(vp); 498 if ((flags & (FWRITE | FOPENFAILED)) == FWRITE) { 499 VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 500 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", 501 __func__, vp, vp->v_writecount); 502 } 503 error = VOP_CLOSE(vp, flags, file_cred, td); 504 if (keep_ref) 505 VOP_UNLOCK(vp); 506 else 507 vput(vp); 508 vn_finished_write(mp); 509 return (error); 510 } 511 512 int 513 vn_close(struct vnode *vp, int flags, struct ucred *file_cred, 514 struct thread *td) 515 { 516 517 return (vn_close1(vp, flags, file_cred, td, false)); 518 } 519 520 /* 521 * Heuristic to detect sequential operation. 522 */ 523 static int 524 sequential_heuristic(struct uio *uio, struct file *fp) 525 { 526 enum uio_rw rw; 527 528 ASSERT_VOP_LOCKED(fp->f_vnode, __func__); 529 530 rw = uio->uio_rw; 531 if (fp->f_flag & FRDAHEAD) 532 return (fp->f_seqcount[rw] << IO_SEQSHIFT); 533 534 /* 535 * Offset 0 is handled specially. open() sets f_seqcount to 1 so 536 * that the first I/O is normally considered to be slightly 537 * sequential. Seeking to offset 0 doesn't change sequentiality 538 * unless previous seeks have reduced f_seqcount to 0, in which 539 * case offset 0 is not special. 540 */ 541 if ((uio->uio_offset == 0 && fp->f_seqcount[rw] > 0) || 542 uio->uio_offset == fp->f_nextoff[rw]) { 543 /* 544 * f_seqcount is in units of fixed-size blocks so that it 545 * depends mainly on the amount of sequential I/O and not 546 * much on the number of sequential I/O's. The fixed size 547 * of 16384 is hard-coded here since it is (not quite) just 548 * a magic size that works well here. This size is more 549 * closely related to the best I/O size for real disks than 550 * to any block size used by software. 551 */ 552 if (uio->uio_resid >= IO_SEQMAX * 16384) 553 fp->f_seqcount[rw] = IO_SEQMAX; 554 else { 555 fp->f_seqcount[rw] += howmany(uio->uio_resid, 16384); 556 if (fp->f_seqcount[rw] > IO_SEQMAX) 557 fp->f_seqcount[rw] = IO_SEQMAX; 558 } 559 return (fp->f_seqcount[rw] << IO_SEQSHIFT); 560 } 561 562 /* Not sequential. Quickly draw-down sequentiality. */ 563 if (fp->f_seqcount[rw] > 1) 564 fp->f_seqcount[rw] = 1; 565 else 566 fp->f_seqcount[rw] = 0; 567 return (0); 568 } 569 570 /* 571 * Package up an I/O request on a vnode into a uio and do it. 572 */ 573 int 574 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset, 575 enum uio_seg segflg, int ioflg, struct ucred *active_cred, 576 struct ucred *file_cred, ssize_t *aresid, struct thread *td) 577 { 578 struct uio auio; 579 struct iovec aiov; 580 struct mount *mp; 581 struct ucred *cred; 582 void *rl_cookie; 583 struct vn_io_fault_args args; 584 int error, lock_flags; 585 586 if (offset < 0 && vp->v_type != VCHR) 587 return (EINVAL); 588 auio.uio_iov = &aiov; 589 auio.uio_iovcnt = 1; 590 aiov.iov_base = base; 591 aiov.iov_len = len; 592 auio.uio_resid = len; 593 auio.uio_offset = offset; 594 auio.uio_segflg = segflg; 595 auio.uio_rw = rw; 596 auio.uio_td = td; 597 error = 0; 598 599 if ((ioflg & IO_NODELOCKED) == 0) { 600 if ((ioflg & IO_RANGELOCKED) == 0) { 601 if (rw == UIO_READ) { 602 rl_cookie = vn_rangelock_rlock(vp, offset, 603 offset + len); 604 } else if ((ioflg & IO_APPEND) != 0) { 605 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 606 } else { 607 rl_cookie = vn_rangelock_wlock(vp, offset, 608 offset + len); 609 } 610 } else 611 rl_cookie = NULL; 612 mp = NULL; 613 if (rw == UIO_WRITE) { 614 if (vp->v_type != VCHR && 615 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) 616 != 0) 617 goto out; 618 if (MNT_SHARED_WRITES(mp) || 619 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) 620 lock_flags = LK_SHARED; 621 else 622 lock_flags = LK_EXCLUSIVE; 623 } else 624 lock_flags = LK_SHARED; 625 vn_lock(vp, lock_flags | LK_RETRY); 626 } else 627 rl_cookie = NULL; 628 629 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 630 #ifdef MAC 631 if ((ioflg & IO_NOMACCHECK) == 0) { 632 if (rw == UIO_READ) 633 error = mac_vnode_check_read(active_cred, file_cred, 634 vp); 635 else 636 error = mac_vnode_check_write(active_cred, file_cred, 637 vp); 638 } 639 #endif 640 if (error == 0) { 641 if (file_cred != NULL) 642 cred = file_cred; 643 else 644 cred = active_cred; 645 if (do_vn_io_fault(vp, &auio)) { 646 args.kind = VN_IO_FAULT_VOP; 647 args.cred = cred; 648 args.flags = ioflg; 649 args.args.vop_args.vp = vp; 650 error = vn_io_fault1(vp, &auio, &args, td); 651 } else if (rw == UIO_READ) { 652 error = VOP_READ(vp, &auio, ioflg, cred); 653 } else /* if (rw == UIO_WRITE) */ { 654 error = VOP_WRITE(vp, &auio, ioflg, cred); 655 } 656 } 657 if (aresid) 658 *aresid = auio.uio_resid; 659 else 660 if (auio.uio_resid && error == 0) 661 error = EIO; 662 if ((ioflg & IO_NODELOCKED) == 0) { 663 VOP_UNLOCK(vp); 664 if (mp != NULL) 665 vn_finished_write(mp); 666 } 667 out: 668 if (rl_cookie != NULL) 669 vn_rangelock_unlock(vp, rl_cookie); 670 return (error); 671 } 672 673 /* 674 * Package up an I/O request on a vnode into a uio and do it. The I/O 675 * request is split up into smaller chunks and we try to avoid saturating 676 * the buffer cache while potentially holding a vnode locked, so we 677 * check bwillwrite() before calling vn_rdwr(). We also call kern_yield() 678 * to give other processes a chance to lock the vnode (either other processes 679 * core'ing the same binary, or unrelated processes scanning the directory). 680 */ 681 int 682 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, void *base, size_t len, 683 off_t offset, enum uio_seg segflg, int ioflg, struct ucred *active_cred, 684 struct ucred *file_cred, size_t *aresid, struct thread *td) 685 { 686 int error = 0; 687 ssize_t iaresid; 688 689 do { 690 int chunk; 691 692 /* 693 * Force `offset' to a multiple of MAXBSIZE except possibly 694 * for the first chunk, so that filesystems only need to 695 * write full blocks except possibly for the first and last 696 * chunks. 697 */ 698 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE; 699 700 if (chunk > len) 701 chunk = len; 702 if (rw != UIO_READ && vp->v_type == VREG) 703 bwillwrite(); 704 iaresid = 0; 705 error = vn_rdwr(rw, vp, base, chunk, offset, segflg, 706 ioflg, active_cred, file_cred, &iaresid, td); 707 len -= chunk; /* aresid calc already includes length */ 708 if (error) 709 break; 710 offset += chunk; 711 base = (char *)base + chunk; 712 kern_yield(PRI_USER); 713 } while (len); 714 if (aresid) 715 *aresid = len + iaresid; 716 return (error); 717 } 718 719 #if OFF_MAX <= LONG_MAX 720 off_t 721 foffset_lock(struct file *fp, int flags) 722 { 723 volatile short *flagsp; 724 off_t res; 725 short state; 726 727 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 728 729 if ((flags & FOF_NOLOCK) != 0) 730 return (atomic_load_long(&fp->f_offset)); 731 732 /* 733 * According to McKusick the vn lock was protecting f_offset here. 734 * It is now protected by the FOFFSET_LOCKED flag. 735 */ 736 flagsp = &fp->f_vnread_flags; 737 if (atomic_cmpset_acq_16(flagsp, 0, FOFFSET_LOCKED)) 738 return (atomic_load_long(&fp->f_offset)); 739 740 sleepq_lock(&fp->f_vnread_flags); 741 state = atomic_load_16(flagsp); 742 for (;;) { 743 if ((state & FOFFSET_LOCKED) == 0) { 744 if (!atomic_fcmpset_acq_16(flagsp, &state, 745 FOFFSET_LOCKED)) 746 continue; 747 break; 748 } 749 if ((state & FOFFSET_LOCK_WAITING) == 0) { 750 if (!atomic_fcmpset_acq_16(flagsp, &state, 751 state | FOFFSET_LOCK_WAITING)) 752 continue; 753 } 754 DROP_GIANT(); 755 sleepq_add(&fp->f_vnread_flags, NULL, "vofflock", 0, 0); 756 sleepq_wait(&fp->f_vnread_flags, PUSER -1); 757 PICKUP_GIANT(); 758 sleepq_lock(&fp->f_vnread_flags); 759 state = atomic_load_16(flagsp); 760 } 761 res = atomic_load_long(&fp->f_offset); 762 sleepq_release(&fp->f_vnread_flags); 763 return (res); 764 } 765 766 void 767 foffset_unlock(struct file *fp, off_t val, int flags) 768 { 769 volatile short *flagsp; 770 short state; 771 772 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 773 774 if ((flags & FOF_NOUPDATE) == 0) 775 atomic_store_long(&fp->f_offset, val); 776 if ((flags & FOF_NEXTOFF_R) != 0) 777 fp->f_nextoff[UIO_READ] = val; 778 if ((flags & FOF_NEXTOFF_W) != 0) 779 fp->f_nextoff[UIO_WRITE] = val; 780 781 if ((flags & FOF_NOLOCK) != 0) 782 return; 783 784 flagsp = &fp->f_vnread_flags; 785 state = atomic_load_16(flagsp); 786 if ((state & FOFFSET_LOCK_WAITING) == 0 && 787 atomic_cmpset_rel_16(flagsp, state, 0)) 788 return; 789 790 sleepq_lock(&fp->f_vnread_flags); 791 MPASS((fp->f_vnread_flags & FOFFSET_LOCKED) != 0); 792 MPASS((fp->f_vnread_flags & FOFFSET_LOCK_WAITING) != 0); 793 fp->f_vnread_flags = 0; 794 sleepq_broadcast(&fp->f_vnread_flags, SLEEPQ_SLEEP, 0, 0); 795 sleepq_release(&fp->f_vnread_flags); 796 } 797 #else 798 off_t 799 foffset_lock(struct file *fp, int flags) 800 { 801 struct mtx *mtxp; 802 off_t res; 803 804 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 805 806 mtxp = mtx_pool_find(mtxpool_sleep, fp); 807 mtx_lock(mtxp); 808 if ((flags & FOF_NOLOCK) == 0) { 809 while (fp->f_vnread_flags & FOFFSET_LOCKED) { 810 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING; 811 msleep(&fp->f_vnread_flags, mtxp, PUSER -1, 812 "vofflock", 0); 813 } 814 fp->f_vnread_flags |= FOFFSET_LOCKED; 815 } 816 res = fp->f_offset; 817 mtx_unlock(mtxp); 818 return (res); 819 } 820 821 void 822 foffset_unlock(struct file *fp, off_t val, int flags) 823 { 824 struct mtx *mtxp; 825 826 KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed")); 827 828 mtxp = mtx_pool_find(mtxpool_sleep, fp); 829 mtx_lock(mtxp); 830 if ((flags & FOF_NOUPDATE) == 0) 831 fp->f_offset = val; 832 if ((flags & FOF_NEXTOFF_R) != 0) 833 fp->f_nextoff[UIO_READ] = val; 834 if ((flags & FOF_NEXTOFF_W) != 0) 835 fp->f_nextoff[UIO_WRITE] = val; 836 if ((flags & FOF_NOLOCK) == 0) { 837 KASSERT((fp->f_vnread_flags & FOFFSET_LOCKED) != 0, 838 ("Lost FOFFSET_LOCKED")); 839 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING) 840 wakeup(&fp->f_vnread_flags); 841 fp->f_vnread_flags = 0; 842 } 843 mtx_unlock(mtxp); 844 } 845 #endif 846 847 void 848 foffset_lock_uio(struct file *fp, struct uio *uio, int flags) 849 { 850 851 if ((flags & FOF_OFFSET) == 0) 852 uio->uio_offset = foffset_lock(fp, flags); 853 } 854 855 void 856 foffset_unlock_uio(struct file *fp, struct uio *uio, int flags) 857 { 858 859 if ((flags & FOF_OFFSET) == 0) 860 foffset_unlock(fp, uio->uio_offset, flags); 861 } 862 863 static int 864 get_advice(struct file *fp, struct uio *uio) 865 { 866 struct mtx *mtxp; 867 int ret; 868 869 ret = POSIX_FADV_NORMAL; 870 if (fp->f_advice == NULL || fp->f_vnode->v_type != VREG) 871 return (ret); 872 873 mtxp = mtx_pool_find(mtxpool_sleep, fp); 874 mtx_lock(mtxp); 875 if (fp->f_advice != NULL && 876 uio->uio_offset >= fp->f_advice->fa_start && 877 uio->uio_offset + uio->uio_resid <= fp->f_advice->fa_end) 878 ret = fp->f_advice->fa_advice; 879 mtx_unlock(mtxp); 880 return (ret); 881 } 882 883 int 884 vn_read_from_obj(struct vnode *vp, struct uio *uio) 885 { 886 vm_object_t obj; 887 vm_page_t ma[io_hold_cnt + 2]; 888 off_t off, vsz; 889 ssize_t resid; 890 int error, i, j; 891 892 MPASS(uio->uio_resid <= ptoa(io_hold_cnt + 2)); 893 obj = atomic_load_ptr(&vp->v_object); 894 if (obj == NULL) 895 return (EJUSTRETURN); 896 897 /* 898 * Depends on type stability of vm_objects. 899 */ 900 vm_object_pip_add(obj, 1); 901 if ((obj->flags & OBJ_DEAD) != 0) { 902 /* 903 * Note that object might be already reused from the 904 * vnode, and the OBJ_DEAD flag cleared. This is fine, 905 * we recheck for DOOMED vnode state after all pages 906 * are busied, and retract then. 907 * 908 * But we check for OBJ_DEAD to ensure that we do not 909 * busy pages while vm_object_terminate_pages() 910 * processes the queue. 911 */ 912 error = EJUSTRETURN; 913 goto out_pip; 914 } 915 916 resid = uio->uio_resid; 917 off = uio->uio_offset; 918 for (i = 0; resid > 0; i++) { 919 MPASS(i < io_hold_cnt + 2); 920 ma[i] = vm_page_grab_unlocked(obj, atop(off), 921 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY | 922 VM_ALLOC_NOWAIT); 923 if (ma[i] == NULL) 924 break; 925 926 /* 927 * Skip invalid pages. Valid mask can be partial only 928 * at EOF, and we clip later. 929 */ 930 if (vm_page_none_valid(ma[i])) { 931 vm_page_sunbusy(ma[i]); 932 break; 933 } 934 935 resid -= PAGE_SIZE; 936 off += PAGE_SIZE; 937 } 938 if (i == 0) { 939 error = EJUSTRETURN; 940 goto out_pip; 941 } 942 943 /* 944 * Check VIRF_DOOMED after we busied our pages. Since 945 * vgonel() terminates the vnode' vm_object, it cannot 946 * process past pages busied by us. 947 */ 948 if (VN_IS_DOOMED(vp)) { 949 error = EJUSTRETURN; 950 goto out; 951 } 952 953 resid = PAGE_SIZE - (uio->uio_offset & PAGE_MASK) + ptoa(i - 1); 954 if (resid > uio->uio_resid) 955 resid = uio->uio_resid; 956 957 /* 958 * Unlocked read of vnp_size is safe because truncation cannot 959 * pass busied page. But we load vnp_size into a local 960 * variable so that possible concurrent extension does not 961 * break calculation. 962 */ 963 #if defined(__powerpc__) && !defined(__powerpc64__) 964 vsz = obj->un_pager.vnp.vnp_size; 965 #else 966 vsz = atomic_load_64(&obj->un_pager.vnp.vnp_size); 967 #endif 968 if (uio->uio_offset >= vsz) { 969 error = EJUSTRETURN; 970 goto out; 971 } 972 if (uio->uio_offset + resid > vsz) 973 resid = vsz - uio->uio_offset; 974 975 error = vn_io_fault_pgmove(ma, uio->uio_offset & PAGE_MASK, resid, uio); 976 977 out: 978 for (j = 0; j < i; j++) { 979 if (error == 0) 980 vm_page_reference(ma[j]); 981 vm_page_sunbusy(ma[j]); 982 } 983 out_pip: 984 vm_object_pip_wakeup(obj); 985 if (error != 0) 986 return (error); 987 return (uio->uio_resid == 0 ? 0 : EJUSTRETURN); 988 } 989 990 /* 991 * File table vnode read routine. 992 */ 993 static int 994 vn_read(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, 995 struct thread *td) 996 { 997 struct vnode *vp; 998 off_t orig_offset; 999 int error, ioflag; 1000 int advice; 1001 1002 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 1003 uio->uio_td, td)); 1004 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 1005 vp = fp->f_vnode; 1006 ioflag = 0; 1007 if (fp->f_flag & FNONBLOCK) 1008 ioflag |= IO_NDELAY; 1009 if (fp->f_flag & O_DIRECT) 1010 ioflag |= IO_DIRECT; 1011 1012 /* 1013 * Try to read from page cache. VIRF_DOOMED check is racy but 1014 * allows us to avoid unneeded work outright. 1015 */ 1016 if (vn_io_pgcache_read_enable && !mac_vnode_check_read_enabled() && 1017 (vn_irflag_read(vp) & (VIRF_DOOMED | VIRF_PGREAD)) == VIRF_PGREAD) { 1018 error = VOP_READ_PGCACHE(vp, uio, ioflag, fp->f_cred); 1019 if (error == 0) { 1020 fp->f_nextoff[UIO_READ] = uio->uio_offset; 1021 return (0); 1022 } 1023 if (error != EJUSTRETURN) 1024 return (error); 1025 } 1026 1027 advice = get_advice(fp, uio); 1028 vn_lock(vp, LK_SHARED | LK_RETRY); 1029 1030 switch (advice) { 1031 case POSIX_FADV_NORMAL: 1032 case POSIX_FADV_SEQUENTIAL: 1033 case POSIX_FADV_NOREUSE: 1034 ioflag |= sequential_heuristic(uio, fp); 1035 break; 1036 case POSIX_FADV_RANDOM: 1037 /* Disable read-ahead for random I/O. */ 1038 break; 1039 } 1040 orig_offset = uio->uio_offset; 1041 1042 #ifdef MAC 1043 error = mac_vnode_check_read(active_cred, fp->f_cred, vp); 1044 if (error == 0) 1045 #endif 1046 error = VOP_READ(vp, uio, ioflag, fp->f_cred); 1047 fp->f_nextoff[UIO_READ] = uio->uio_offset; 1048 VOP_UNLOCK(vp); 1049 if (error == 0 && advice == POSIX_FADV_NOREUSE && 1050 orig_offset != uio->uio_offset) 1051 /* 1052 * Use POSIX_FADV_DONTNEED to flush pages and buffers 1053 * for the backing file after a POSIX_FADV_NOREUSE 1054 * read(2). 1055 */ 1056 error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1, 1057 POSIX_FADV_DONTNEED); 1058 return (error); 1059 } 1060 1061 /* 1062 * File table vnode write routine. 1063 */ 1064 static int 1065 vn_write(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, 1066 struct thread *td) 1067 { 1068 struct vnode *vp; 1069 struct mount *mp; 1070 off_t orig_offset; 1071 int error, ioflag, lock_flags; 1072 int advice; 1073 1074 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", 1075 uio->uio_td, td)); 1076 KASSERT(flags & FOF_OFFSET, ("No FOF_OFFSET")); 1077 vp = fp->f_vnode; 1078 if (vp->v_type == VREG) 1079 bwillwrite(); 1080 ioflag = IO_UNIT; 1081 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 1082 ioflag |= IO_APPEND; 1083 if (fp->f_flag & FNONBLOCK) 1084 ioflag |= IO_NDELAY; 1085 if (fp->f_flag & O_DIRECT) 1086 ioflag |= IO_DIRECT; 1087 if ((fp->f_flag & O_FSYNC) || 1088 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) 1089 ioflag |= IO_SYNC; 1090 /* 1091 * For O_DSYNC we set both IO_SYNC and IO_DATASYNC, so that VOP_WRITE() 1092 * implementations that don't understand IO_DATASYNC fall back to full 1093 * O_SYNC behavior. 1094 */ 1095 if (fp->f_flag & O_DSYNC) 1096 ioflag |= IO_SYNC | IO_DATASYNC; 1097 mp = NULL; 1098 if (vp->v_type != VCHR && 1099 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 1100 goto unlock; 1101 1102 advice = get_advice(fp, uio); 1103 1104 if (MNT_SHARED_WRITES(mp) || 1105 (mp == NULL && MNT_SHARED_WRITES(vp->v_mount))) { 1106 lock_flags = LK_SHARED; 1107 } else { 1108 lock_flags = LK_EXCLUSIVE; 1109 } 1110 1111 vn_lock(vp, lock_flags | LK_RETRY); 1112 switch (advice) { 1113 case POSIX_FADV_NORMAL: 1114 case POSIX_FADV_SEQUENTIAL: 1115 case POSIX_FADV_NOREUSE: 1116 ioflag |= sequential_heuristic(uio, fp); 1117 break; 1118 case POSIX_FADV_RANDOM: 1119 /* XXX: Is this correct? */ 1120 break; 1121 } 1122 orig_offset = uio->uio_offset; 1123 1124 #ifdef MAC 1125 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 1126 if (error == 0) 1127 #endif 1128 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred); 1129 fp->f_nextoff[UIO_WRITE] = uio->uio_offset; 1130 VOP_UNLOCK(vp); 1131 if (vp->v_type != VCHR) 1132 vn_finished_write(mp); 1133 if (error == 0 && advice == POSIX_FADV_NOREUSE && 1134 orig_offset != uio->uio_offset) 1135 /* 1136 * Use POSIX_FADV_DONTNEED to flush pages and buffers 1137 * for the backing file after a POSIX_FADV_NOREUSE 1138 * write(2). 1139 */ 1140 error = VOP_ADVISE(vp, orig_offset, uio->uio_offset - 1, 1141 POSIX_FADV_DONTNEED); 1142 unlock: 1143 return (error); 1144 } 1145 1146 /* 1147 * The vn_io_fault() is a wrapper around vn_read() and vn_write() to 1148 * prevent the following deadlock: 1149 * 1150 * Assume that the thread A reads from the vnode vp1 into userspace 1151 * buffer buf1 backed by the pages of vnode vp2. If a page in buf1 is 1152 * currently not resident, then system ends up with the call chain 1153 * vn_read() -> VOP_READ(vp1) -> uiomove() -> [Page Fault] -> 1154 * vm_fault(buf1) -> vnode_pager_getpages(vp2) -> VOP_GETPAGES(vp2) 1155 * which establishes lock order vp1->vn_lock, then vp2->vn_lock. 1156 * If, at the same time, thread B reads from vnode vp2 into buffer buf2 1157 * backed by the pages of vnode vp1, and some page in buf2 is not 1158 * resident, we get a reversed order vp2->vn_lock, then vp1->vn_lock. 1159 * 1160 * To prevent the lock order reversal and deadlock, vn_io_fault() does 1161 * not allow page faults to happen during VOP_READ() or VOP_WRITE(). 1162 * Instead, it first tries to do the whole range i/o with pagefaults 1163 * disabled. If all pages in the i/o buffer are resident and mapped, 1164 * VOP will succeed (ignoring the genuine filesystem errors). 1165 * Otherwise, we get back EFAULT, and vn_io_fault() falls back to do 1166 * i/o in chunks, with all pages in the chunk prefaulted and held 1167 * using vm_fault_quick_hold_pages(). 1168 * 1169 * Filesystems using this deadlock avoidance scheme should use the 1170 * array of the held pages from uio, saved in the curthread->td_ma, 1171 * instead of doing uiomove(). A helper function 1172 * vn_io_fault_uiomove() converts uiomove request into 1173 * uiomove_fromphys() over td_ma array. 1174 * 1175 * Since vnode locks do not cover the whole i/o anymore, rangelocks 1176 * make the current i/o request atomic with respect to other i/os and 1177 * truncations. 1178 */ 1179 1180 /* 1181 * Decode vn_io_fault_args and perform the corresponding i/o. 1182 */ 1183 static int 1184 vn_io_fault_doio(struct vn_io_fault_args *args, struct uio *uio, 1185 struct thread *td) 1186 { 1187 int error, save; 1188 1189 error = 0; 1190 save = vm_fault_disable_pagefaults(); 1191 switch (args->kind) { 1192 case VN_IO_FAULT_FOP: 1193 error = (args->args.fop_args.doio)(args->args.fop_args.fp, 1194 uio, args->cred, args->flags, td); 1195 break; 1196 case VN_IO_FAULT_VOP: 1197 if (uio->uio_rw == UIO_READ) { 1198 error = VOP_READ(args->args.vop_args.vp, uio, 1199 args->flags, args->cred); 1200 } else if (uio->uio_rw == UIO_WRITE) { 1201 error = VOP_WRITE(args->args.vop_args.vp, uio, 1202 args->flags, args->cred); 1203 } 1204 break; 1205 default: 1206 panic("vn_io_fault_doio: unknown kind of io %d %d", 1207 args->kind, uio->uio_rw); 1208 } 1209 vm_fault_enable_pagefaults(save); 1210 return (error); 1211 } 1212 1213 static int 1214 vn_io_fault_touch(char *base, const struct uio *uio) 1215 { 1216 int r; 1217 1218 r = fubyte(base); 1219 if (r == -1 || (uio->uio_rw == UIO_READ && subyte(base, r) == -1)) 1220 return (EFAULT); 1221 return (0); 1222 } 1223 1224 static int 1225 vn_io_fault_prefault_user(const struct uio *uio) 1226 { 1227 char *base; 1228 const struct iovec *iov; 1229 size_t len; 1230 ssize_t resid; 1231 int error, i; 1232 1233 KASSERT(uio->uio_segflg == UIO_USERSPACE, 1234 ("vn_io_fault_prefault userspace")); 1235 1236 error = i = 0; 1237 iov = uio->uio_iov; 1238 resid = uio->uio_resid; 1239 base = iov->iov_base; 1240 len = iov->iov_len; 1241 while (resid > 0) { 1242 error = vn_io_fault_touch(base, uio); 1243 if (error != 0) 1244 break; 1245 if (len < PAGE_SIZE) { 1246 if (len != 0) { 1247 error = vn_io_fault_touch(base + len - 1, uio); 1248 if (error != 0) 1249 break; 1250 resid -= len; 1251 } 1252 if (++i >= uio->uio_iovcnt) 1253 break; 1254 iov = uio->uio_iov + i; 1255 base = iov->iov_base; 1256 len = iov->iov_len; 1257 } else { 1258 len -= PAGE_SIZE; 1259 base += PAGE_SIZE; 1260 resid -= PAGE_SIZE; 1261 } 1262 } 1263 return (error); 1264 } 1265 1266 /* 1267 * Common code for vn_io_fault(), agnostic to the kind of i/o request. 1268 * Uses vn_io_fault_doio() to make the call to an actual i/o function. 1269 * Used from vn_rdwr() and vn_io_fault(), which encode the i/o request 1270 * into args and call vn_io_fault1() to handle faults during the user 1271 * mode buffer accesses. 1272 */ 1273 static int 1274 vn_io_fault1(struct vnode *vp, struct uio *uio, struct vn_io_fault_args *args, 1275 struct thread *td) 1276 { 1277 vm_page_t ma[io_hold_cnt + 2]; 1278 struct uio *uio_clone, short_uio; 1279 struct iovec short_iovec[1]; 1280 vm_page_t *prev_td_ma; 1281 vm_prot_t prot; 1282 vm_offset_t addr, end; 1283 size_t len, resid; 1284 ssize_t adv; 1285 int error, cnt, saveheld, prev_td_ma_cnt; 1286 1287 if (vn_io_fault_prefault) { 1288 error = vn_io_fault_prefault_user(uio); 1289 if (error != 0) 1290 return (error); /* Or ignore ? */ 1291 } 1292 1293 prot = uio->uio_rw == UIO_READ ? VM_PROT_WRITE : VM_PROT_READ; 1294 1295 /* 1296 * The UFS follows IO_UNIT directive and replays back both 1297 * uio_offset and uio_resid if an error is encountered during the 1298 * operation. But, since the iovec may be already advanced, 1299 * uio is still in an inconsistent state. 1300 * 1301 * Cache a copy of the original uio, which is advanced to the redo 1302 * point using UIO_NOCOPY below. 1303 */ 1304 uio_clone = cloneuio(uio); 1305 resid = uio->uio_resid; 1306 1307 short_uio.uio_segflg = UIO_USERSPACE; 1308 short_uio.uio_rw = uio->uio_rw; 1309 short_uio.uio_td = uio->uio_td; 1310 1311 error = vn_io_fault_doio(args, uio, td); 1312 if (error != EFAULT) 1313 goto out; 1314 1315 atomic_add_long(&vn_io_faults_cnt, 1); 1316 uio_clone->uio_segflg = UIO_NOCOPY; 1317 uiomove(NULL, resid - uio->uio_resid, uio_clone); 1318 uio_clone->uio_segflg = uio->uio_segflg; 1319 1320 saveheld = curthread_pflags_set(TDP_UIOHELD); 1321 prev_td_ma = td->td_ma; 1322 prev_td_ma_cnt = td->td_ma_cnt; 1323 1324 while (uio_clone->uio_resid != 0) { 1325 len = uio_clone->uio_iov->iov_len; 1326 if (len == 0) { 1327 KASSERT(uio_clone->uio_iovcnt >= 1, 1328 ("iovcnt underflow")); 1329 uio_clone->uio_iov++; 1330 uio_clone->uio_iovcnt--; 1331 continue; 1332 } 1333 if (len > ptoa(io_hold_cnt)) 1334 len = ptoa(io_hold_cnt); 1335 addr = (uintptr_t)uio_clone->uio_iov->iov_base; 1336 end = round_page(addr + len); 1337 if (end < addr) { 1338 error = EFAULT; 1339 break; 1340 } 1341 cnt = atop(end - trunc_page(addr)); 1342 /* 1343 * A perfectly misaligned address and length could cause 1344 * both the start and the end of the chunk to use partial 1345 * page. +2 accounts for such a situation. 1346 */ 1347 cnt = vm_fault_quick_hold_pages(&td->td_proc->p_vmspace->vm_map, 1348 addr, len, prot, ma, io_hold_cnt + 2); 1349 if (cnt == -1) { 1350 error = EFAULT; 1351 break; 1352 } 1353 short_uio.uio_iov = &short_iovec[0]; 1354 short_iovec[0].iov_base = (void *)addr; 1355 short_uio.uio_iovcnt = 1; 1356 short_uio.uio_resid = short_iovec[0].iov_len = len; 1357 short_uio.uio_offset = uio_clone->uio_offset; 1358 td->td_ma = ma; 1359 td->td_ma_cnt = cnt; 1360 1361 error = vn_io_fault_doio(args, &short_uio, td); 1362 vm_page_unhold_pages(ma, cnt); 1363 adv = len - short_uio.uio_resid; 1364 1365 uio_clone->uio_iov->iov_base = 1366 (char *)uio_clone->uio_iov->iov_base + adv; 1367 uio_clone->uio_iov->iov_len -= adv; 1368 uio_clone->uio_resid -= adv; 1369 uio_clone->uio_offset += adv; 1370 1371 uio->uio_resid -= adv; 1372 uio->uio_offset += adv; 1373 1374 if (error != 0 || adv == 0) 1375 break; 1376 } 1377 td->td_ma = prev_td_ma; 1378 td->td_ma_cnt = prev_td_ma_cnt; 1379 curthread_pflags_restore(saveheld); 1380 out: 1381 free(uio_clone, M_IOV); 1382 return (error); 1383 } 1384 1385 static int 1386 vn_io_fault(struct file *fp, struct uio *uio, struct ucred *active_cred, 1387 int flags, struct thread *td) 1388 { 1389 fo_rdwr_t *doio; 1390 struct vnode *vp; 1391 void *rl_cookie; 1392 struct vn_io_fault_args args; 1393 int error; 1394 1395 doio = uio->uio_rw == UIO_READ ? vn_read : vn_write; 1396 vp = fp->f_vnode; 1397 1398 /* 1399 * The ability to read(2) on a directory has historically been 1400 * allowed for all users, but this can and has been the source of 1401 * at least one security issue in the past. As such, it is now hidden 1402 * away behind a sysctl for those that actually need it to use it, and 1403 * restricted to root when it's turned on to make it relatively safe to 1404 * leave on for longer sessions of need. 1405 */ 1406 if (vp->v_type == VDIR) { 1407 KASSERT(uio->uio_rw == UIO_READ, 1408 ("illegal write attempted on a directory")); 1409 if (!vfs_allow_read_dir) 1410 return (EISDIR); 1411 if ((error = priv_check(td, PRIV_VFS_READ_DIR)) != 0) 1412 return (EISDIR); 1413 } 1414 1415 foffset_lock_uio(fp, uio, flags); 1416 if (do_vn_io_fault(vp, uio)) { 1417 args.kind = VN_IO_FAULT_FOP; 1418 args.args.fop_args.fp = fp; 1419 args.args.fop_args.doio = doio; 1420 args.cred = active_cred; 1421 args.flags = flags | FOF_OFFSET; 1422 if (uio->uio_rw == UIO_READ) { 1423 rl_cookie = vn_rangelock_rlock(vp, uio->uio_offset, 1424 uio->uio_offset + uio->uio_resid); 1425 } else if ((fp->f_flag & O_APPEND) != 0 || 1426 (flags & FOF_OFFSET) == 0) { 1427 /* For appenders, punt and lock the whole range. */ 1428 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1429 } else { 1430 rl_cookie = vn_rangelock_wlock(vp, uio->uio_offset, 1431 uio->uio_offset + uio->uio_resid); 1432 } 1433 error = vn_io_fault1(vp, uio, &args, td); 1434 vn_rangelock_unlock(vp, rl_cookie); 1435 } else { 1436 error = doio(fp, uio, active_cred, flags | FOF_OFFSET, td); 1437 } 1438 foffset_unlock_uio(fp, uio, flags); 1439 return (error); 1440 } 1441 1442 /* 1443 * Helper function to perform the requested uiomove operation using 1444 * the held pages for io->uio_iov[0].iov_base buffer instead of 1445 * copyin/copyout. Access to the pages with uiomove_fromphys() 1446 * instead of iov_base prevents page faults that could occur due to 1447 * pmap_collect() invalidating the mapping created by 1448 * vm_fault_quick_hold_pages(), or pageout daemon, page laundry or 1449 * object cleanup revoking the write access from page mappings. 1450 * 1451 * Filesystems specified MNTK_NO_IOPF shall use vn_io_fault_uiomove() 1452 * instead of plain uiomove(). 1453 */ 1454 int 1455 vn_io_fault_uiomove(char *data, int xfersize, struct uio *uio) 1456 { 1457 struct uio transp_uio; 1458 struct iovec transp_iov[1]; 1459 struct thread *td; 1460 size_t adv; 1461 int error, pgadv; 1462 1463 td = curthread; 1464 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1465 uio->uio_segflg != UIO_USERSPACE) 1466 return (uiomove(data, xfersize, uio)); 1467 1468 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1469 transp_iov[0].iov_base = data; 1470 transp_uio.uio_iov = &transp_iov[0]; 1471 transp_uio.uio_iovcnt = 1; 1472 if (xfersize > uio->uio_resid) 1473 xfersize = uio->uio_resid; 1474 transp_uio.uio_resid = transp_iov[0].iov_len = xfersize; 1475 transp_uio.uio_offset = 0; 1476 transp_uio.uio_segflg = UIO_SYSSPACE; 1477 /* 1478 * Since transp_iov points to data, and td_ma page array 1479 * corresponds to original uio->uio_iov, we need to invert the 1480 * direction of the i/o operation as passed to 1481 * uiomove_fromphys(). 1482 */ 1483 switch (uio->uio_rw) { 1484 case UIO_WRITE: 1485 transp_uio.uio_rw = UIO_READ; 1486 break; 1487 case UIO_READ: 1488 transp_uio.uio_rw = UIO_WRITE; 1489 break; 1490 } 1491 transp_uio.uio_td = uio->uio_td; 1492 error = uiomove_fromphys(td->td_ma, 1493 ((vm_offset_t)uio->uio_iov->iov_base) & PAGE_MASK, 1494 xfersize, &transp_uio); 1495 adv = xfersize - transp_uio.uio_resid; 1496 pgadv = 1497 (((vm_offset_t)uio->uio_iov->iov_base + adv) >> PAGE_SHIFT) - 1498 (((vm_offset_t)uio->uio_iov->iov_base) >> PAGE_SHIFT); 1499 td->td_ma += pgadv; 1500 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1501 pgadv)); 1502 td->td_ma_cnt -= pgadv; 1503 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + adv; 1504 uio->uio_iov->iov_len -= adv; 1505 uio->uio_resid -= adv; 1506 uio->uio_offset += adv; 1507 return (error); 1508 } 1509 1510 int 1511 vn_io_fault_pgmove(vm_page_t ma[], vm_offset_t offset, int xfersize, 1512 struct uio *uio) 1513 { 1514 struct thread *td; 1515 vm_offset_t iov_base; 1516 int cnt, pgadv; 1517 1518 td = curthread; 1519 if ((td->td_pflags & TDP_UIOHELD) == 0 || 1520 uio->uio_segflg != UIO_USERSPACE) 1521 return (uiomove_fromphys(ma, offset, xfersize, uio)); 1522 1523 KASSERT(uio->uio_iovcnt == 1, ("uio_iovcnt %d", uio->uio_iovcnt)); 1524 cnt = xfersize > uio->uio_resid ? uio->uio_resid : xfersize; 1525 iov_base = (vm_offset_t)uio->uio_iov->iov_base; 1526 switch (uio->uio_rw) { 1527 case UIO_WRITE: 1528 pmap_copy_pages(td->td_ma, iov_base & PAGE_MASK, ma, 1529 offset, cnt); 1530 break; 1531 case UIO_READ: 1532 pmap_copy_pages(ma, offset, td->td_ma, iov_base & PAGE_MASK, 1533 cnt); 1534 break; 1535 } 1536 pgadv = ((iov_base + cnt) >> PAGE_SHIFT) - (iov_base >> PAGE_SHIFT); 1537 td->td_ma += pgadv; 1538 KASSERT(td->td_ma_cnt >= pgadv, ("consumed pages %d %d", td->td_ma_cnt, 1539 pgadv)); 1540 td->td_ma_cnt -= pgadv; 1541 uio->uio_iov->iov_base = (char *)(iov_base + cnt); 1542 uio->uio_iov->iov_len -= cnt; 1543 uio->uio_resid -= cnt; 1544 uio->uio_offset += cnt; 1545 return (0); 1546 } 1547 1548 /* 1549 * File table truncate routine. 1550 */ 1551 static int 1552 vn_truncate(struct file *fp, off_t length, struct ucred *active_cred, 1553 struct thread *td) 1554 { 1555 struct mount *mp; 1556 struct vnode *vp; 1557 void *rl_cookie; 1558 int error; 1559 1560 vp = fp->f_vnode; 1561 1562 retry: 1563 /* 1564 * Lock the whole range for truncation. Otherwise split i/o 1565 * might happen partly before and partly after the truncation. 1566 */ 1567 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 1568 error = vn_start_write(vp, &mp, V_WAIT | PCATCH); 1569 if (error) 1570 goto out1; 1571 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1572 AUDIT_ARG_VNODE1(vp); 1573 if (vp->v_type == VDIR) { 1574 error = EISDIR; 1575 goto out; 1576 } 1577 #ifdef MAC 1578 error = mac_vnode_check_write(active_cred, fp->f_cred, vp); 1579 if (error) 1580 goto out; 1581 #endif 1582 error = vn_truncate_locked(vp, length, (fp->f_flag & O_FSYNC) != 0, 1583 fp->f_cred); 1584 out: 1585 VOP_UNLOCK(vp); 1586 vn_finished_write(mp); 1587 out1: 1588 vn_rangelock_unlock(vp, rl_cookie); 1589 if (error == ERELOOKUP) 1590 goto retry; 1591 return (error); 1592 } 1593 1594 /* 1595 * Truncate a file that is already locked. 1596 */ 1597 int 1598 vn_truncate_locked(struct vnode *vp, off_t length, bool sync, 1599 struct ucred *cred) 1600 { 1601 struct vattr vattr; 1602 int error; 1603 1604 error = VOP_ADD_WRITECOUNT(vp, 1); 1605 if (error == 0) { 1606 VATTR_NULL(&vattr); 1607 vattr.va_size = length; 1608 if (sync) 1609 vattr.va_vaflags |= VA_SYNC; 1610 error = VOP_SETATTR(vp, &vattr, cred); 1611 VOP_ADD_WRITECOUNT_CHECKED(vp, -1); 1612 } 1613 return (error); 1614 } 1615 1616 /* 1617 * File table vnode stat routine. 1618 */ 1619 static int 1620 vn_statfile(struct file *fp, struct stat *sb, struct ucred *active_cred, 1621 struct thread *td) 1622 { 1623 struct vnode *vp = fp->f_vnode; 1624 int error; 1625 1626 vn_lock(vp, LK_SHARED | LK_RETRY); 1627 error = VOP_STAT(vp, sb, active_cred, fp->f_cred, td); 1628 VOP_UNLOCK(vp); 1629 1630 return (error); 1631 } 1632 1633 /* 1634 * File table vnode ioctl routine. 1635 */ 1636 static int 1637 vn_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, 1638 struct thread *td) 1639 { 1640 struct vattr vattr; 1641 struct vnode *vp; 1642 struct fiobmap2_arg *bmarg; 1643 int error; 1644 1645 vp = fp->f_vnode; 1646 switch (vp->v_type) { 1647 case VDIR: 1648 case VREG: 1649 switch (com) { 1650 case FIONREAD: 1651 vn_lock(vp, LK_SHARED | LK_RETRY); 1652 error = VOP_GETATTR(vp, &vattr, active_cred); 1653 VOP_UNLOCK(vp); 1654 if (error == 0) 1655 *(int *)data = vattr.va_size - fp->f_offset; 1656 return (error); 1657 case FIOBMAP2: 1658 bmarg = (struct fiobmap2_arg *)data; 1659 vn_lock(vp, LK_SHARED | LK_RETRY); 1660 #ifdef MAC 1661 error = mac_vnode_check_read(active_cred, fp->f_cred, 1662 vp); 1663 if (error == 0) 1664 #endif 1665 error = VOP_BMAP(vp, bmarg->bn, NULL, 1666 &bmarg->bn, &bmarg->runp, &bmarg->runb); 1667 VOP_UNLOCK(vp); 1668 return (error); 1669 case FIONBIO: 1670 case FIOASYNC: 1671 return (0); 1672 default: 1673 return (VOP_IOCTL(vp, com, data, fp->f_flag, 1674 active_cred, td)); 1675 } 1676 break; 1677 case VCHR: 1678 return (VOP_IOCTL(vp, com, data, fp->f_flag, 1679 active_cred, td)); 1680 default: 1681 return (ENOTTY); 1682 } 1683 } 1684 1685 /* 1686 * File table vnode poll routine. 1687 */ 1688 static int 1689 vn_poll(struct file *fp, int events, struct ucred *active_cred, 1690 struct thread *td) 1691 { 1692 struct vnode *vp; 1693 int error; 1694 1695 vp = fp->f_vnode; 1696 #if defined(MAC) || defined(AUDIT) 1697 if (AUDITING_TD(td) || mac_vnode_check_poll_enabled()) { 1698 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1699 AUDIT_ARG_VNODE1(vp); 1700 error = mac_vnode_check_poll(active_cred, fp->f_cred, vp); 1701 VOP_UNLOCK(vp); 1702 if (error != 0) 1703 return (error); 1704 } 1705 #endif 1706 error = VOP_POLL(vp, events, fp->f_cred, td); 1707 return (error); 1708 } 1709 1710 /* 1711 * Acquire the requested lock and then check for validity. LK_RETRY 1712 * permits vn_lock to return doomed vnodes. 1713 */ 1714 static int __noinline 1715 _vn_lock_fallback(struct vnode *vp, int flags, const char *file, int line, 1716 int error) 1717 { 1718 1719 KASSERT((flags & LK_RETRY) == 0 || error == 0, 1720 ("vn_lock: error %d incompatible with flags %#x", error, flags)); 1721 1722 if (error == 0) 1723 VNASSERT(VN_IS_DOOMED(vp), vp, ("vnode not doomed")); 1724 1725 if ((flags & LK_RETRY) == 0) { 1726 if (error == 0) { 1727 VOP_UNLOCK(vp); 1728 error = ENOENT; 1729 } 1730 return (error); 1731 } 1732 1733 /* 1734 * LK_RETRY case. 1735 * 1736 * Nothing to do if we got the lock. 1737 */ 1738 if (error == 0) 1739 return (0); 1740 1741 /* 1742 * Interlock was dropped by the call in _vn_lock. 1743 */ 1744 flags &= ~LK_INTERLOCK; 1745 do { 1746 error = VOP_LOCK1(vp, flags, file, line); 1747 } while (error != 0); 1748 return (0); 1749 } 1750 1751 int 1752 _vn_lock(struct vnode *vp, int flags, const char *file, int line) 1753 { 1754 int error; 1755 1756 VNASSERT((flags & LK_TYPE_MASK) != 0, vp, 1757 ("vn_lock: no locktype (%d passed)", flags)); 1758 VNPASS(vp->v_holdcnt > 0, vp); 1759 error = VOP_LOCK1(vp, flags, file, line); 1760 if (__predict_false(error != 0 || VN_IS_DOOMED(vp))) 1761 return (_vn_lock_fallback(vp, flags, file, line, error)); 1762 return (0); 1763 } 1764 1765 /* 1766 * File table vnode close routine. 1767 */ 1768 static int 1769 vn_closefile(struct file *fp, struct thread *td) 1770 { 1771 struct vnode *vp; 1772 struct flock lf; 1773 int error; 1774 bool ref; 1775 1776 vp = fp->f_vnode; 1777 fp->f_ops = &badfileops; 1778 ref= (fp->f_flag & FHASLOCK) != 0 && fp->f_type == DTYPE_VNODE; 1779 1780 error = vn_close1(vp, fp->f_flag, fp->f_cred, td, ref); 1781 1782 if (__predict_false(ref)) { 1783 lf.l_whence = SEEK_SET; 1784 lf.l_start = 0; 1785 lf.l_len = 0; 1786 lf.l_type = F_UNLCK; 1787 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK); 1788 vrele(vp); 1789 } 1790 return (error); 1791 } 1792 1793 /* 1794 * Preparing to start a filesystem write operation. If the operation is 1795 * permitted, then we bump the count of operations in progress and 1796 * proceed. If a suspend request is in progress, we wait until the 1797 * suspension is over, and then proceed. 1798 */ 1799 static int 1800 vn_start_write_refed(struct mount *mp, int flags, bool mplocked) 1801 { 1802 struct mount_pcpu *mpcpu; 1803 int error, mflags; 1804 1805 if (__predict_true(!mplocked) && (flags & V_XSLEEP) == 0 && 1806 vfs_op_thread_enter(mp, mpcpu)) { 1807 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) == 0); 1808 vfs_mp_count_add_pcpu(mpcpu, writeopcount, 1); 1809 vfs_op_thread_exit(mp, mpcpu); 1810 return (0); 1811 } 1812 1813 if (mplocked) 1814 mtx_assert(MNT_MTX(mp), MA_OWNED); 1815 else 1816 MNT_ILOCK(mp); 1817 1818 error = 0; 1819 1820 /* 1821 * Check on status of suspension. 1822 */ 1823 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 || 1824 mp->mnt_susp_owner != curthread) { 1825 mflags = ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0 ? 1826 (flags & PCATCH) : 0) | (PUSER - 1); 1827 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 1828 if (flags & V_NOWAIT) { 1829 error = EWOULDBLOCK; 1830 goto unlock; 1831 } 1832 error = msleep(&mp->mnt_flag, MNT_MTX(mp), mflags, 1833 "suspfs", 0); 1834 if (error) 1835 goto unlock; 1836 } 1837 } 1838 if (flags & V_XSLEEP) 1839 goto unlock; 1840 mp->mnt_writeopcount++; 1841 unlock: 1842 if (error != 0 || (flags & V_XSLEEP) != 0) 1843 MNT_REL(mp); 1844 MNT_IUNLOCK(mp); 1845 return (error); 1846 } 1847 1848 int 1849 vn_start_write(struct vnode *vp, struct mount **mpp, int flags) 1850 { 1851 struct mount *mp; 1852 int error; 1853 1854 KASSERT((flags & V_MNTREF) == 0 || (*mpp != NULL && vp == NULL), 1855 ("V_MNTREF requires mp")); 1856 1857 error = 0; 1858 /* 1859 * If a vnode is provided, get and return the mount point that 1860 * to which it will write. 1861 */ 1862 if (vp != NULL) { 1863 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 1864 *mpp = NULL; 1865 if (error != EOPNOTSUPP) 1866 return (error); 1867 return (0); 1868 } 1869 } 1870 if ((mp = *mpp) == NULL) 1871 return (0); 1872 1873 /* 1874 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 1875 * a vfs_ref(). 1876 * As long as a vnode is not provided we need to acquire a 1877 * refcount for the provided mountpoint too, in order to 1878 * emulate a vfs_ref(). 1879 */ 1880 if (vp == NULL && (flags & V_MNTREF) == 0) 1881 vfs_ref(mp); 1882 1883 return (vn_start_write_refed(mp, flags, false)); 1884 } 1885 1886 /* 1887 * Secondary suspension. Used by operations such as vop_inactive 1888 * routines that are needed by the higher level functions. These 1889 * are allowed to proceed until all the higher level functions have 1890 * completed (indicated by mnt_writeopcount dropping to zero). At that 1891 * time, these operations are halted until the suspension is over. 1892 */ 1893 int 1894 vn_start_secondary_write(struct vnode *vp, struct mount **mpp, int flags) 1895 { 1896 struct mount *mp; 1897 int error; 1898 1899 KASSERT((flags & V_MNTREF) == 0 || (*mpp != NULL && vp == NULL), 1900 ("V_MNTREF requires mp")); 1901 1902 retry: 1903 if (vp != NULL) { 1904 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) { 1905 *mpp = NULL; 1906 if (error != EOPNOTSUPP) 1907 return (error); 1908 return (0); 1909 } 1910 } 1911 /* 1912 * If we are not suspended or have not yet reached suspended 1913 * mode, then let the operation proceed. 1914 */ 1915 if ((mp = *mpp) == NULL) 1916 return (0); 1917 1918 /* 1919 * VOP_GETWRITEMOUNT() returns with the mp refcount held through 1920 * a vfs_ref(). 1921 * As long as a vnode is not provided we need to acquire a 1922 * refcount for the provided mountpoint too, in order to 1923 * emulate a vfs_ref(). 1924 */ 1925 MNT_ILOCK(mp); 1926 if (vp == NULL && (flags & V_MNTREF) == 0) 1927 MNT_REF(mp); 1928 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) { 1929 mp->mnt_secondary_writes++; 1930 mp->mnt_secondary_accwrites++; 1931 MNT_IUNLOCK(mp); 1932 return (0); 1933 } 1934 if (flags & V_NOWAIT) { 1935 MNT_REL(mp); 1936 MNT_IUNLOCK(mp); 1937 return (EWOULDBLOCK); 1938 } 1939 /* 1940 * Wait for the suspension to finish. 1941 */ 1942 error = msleep(&mp->mnt_flag, MNT_MTX(mp), (PUSER - 1) | PDROP | 1943 ((mp->mnt_vfc->vfc_flags & VFCF_SBDRY) != 0 ? (flags & PCATCH) : 0), 1944 "suspfs", 0); 1945 vfs_rel(mp); 1946 if (error == 0) 1947 goto retry; 1948 return (error); 1949 } 1950 1951 /* 1952 * Filesystem write operation has completed. If we are suspending and this 1953 * operation is the last one, notify the suspender that the suspension is 1954 * now in effect. 1955 */ 1956 void 1957 vn_finished_write(struct mount *mp) 1958 { 1959 struct mount_pcpu *mpcpu; 1960 int c; 1961 1962 if (mp == NULL) 1963 return; 1964 1965 if (vfs_op_thread_enter(mp, mpcpu)) { 1966 vfs_mp_count_sub_pcpu(mpcpu, writeopcount, 1); 1967 vfs_mp_count_sub_pcpu(mpcpu, ref, 1); 1968 vfs_op_thread_exit(mp, mpcpu); 1969 return; 1970 } 1971 1972 MNT_ILOCK(mp); 1973 vfs_assert_mount_counters(mp); 1974 MNT_REL(mp); 1975 c = --mp->mnt_writeopcount; 1976 if (mp->mnt_vfs_ops == 0) { 1977 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) == 0); 1978 MNT_IUNLOCK(mp); 1979 return; 1980 } 1981 if (c < 0) 1982 vfs_dump_mount_counters(mp); 1983 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && c == 0) 1984 wakeup(&mp->mnt_writeopcount); 1985 MNT_IUNLOCK(mp); 1986 } 1987 1988 /* 1989 * Filesystem secondary write operation has completed. If we are 1990 * suspending and this operation is the last one, notify the suspender 1991 * that the suspension is now in effect. 1992 */ 1993 void 1994 vn_finished_secondary_write(struct mount *mp) 1995 { 1996 if (mp == NULL) 1997 return; 1998 MNT_ILOCK(mp); 1999 MNT_REL(mp); 2000 mp->mnt_secondary_writes--; 2001 if (mp->mnt_secondary_writes < 0) 2002 panic("vn_finished_secondary_write: neg cnt"); 2003 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 && 2004 mp->mnt_secondary_writes <= 0) 2005 wakeup(&mp->mnt_secondary_writes); 2006 MNT_IUNLOCK(mp); 2007 } 2008 2009 /* 2010 * Request a filesystem to suspend write operations. 2011 */ 2012 int 2013 vfs_write_suspend(struct mount *mp, int flags) 2014 { 2015 int error; 2016 2017 vfs_op_enter(mp); 2018 2019 MNT_ILOCK(mp); 2020 vfs_assert_mount_counters(mp); 2021 if (mp->mnt_susp_owner == curthread) { 2022 vfs_op_exit_locked(mp); 2023 MNT_IUNLOCK(mp); 2024 return (EALREADY); 2025 } 2026 while (mp->mnt_kern_flag & MNTK_SUSPEND) 2027 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0); 2028 2029 /* 2030 * Unmount holds a write reference on the mount point. If we 2031 * own busy reference and drain for writers, we deadlock with 2032 * the reference draining in the unmount path. Callers of 2033 * vfs_write_suspend() must specify VS_SKIP_UNMOUNT if 2034 * vfs_busy() reference is owned and caller is not in the 2035 * unmount context. 2036 */ 2037 if ((flags & VS_SKIP_UNMOUNT) != 0 && 2038 (mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) { 2039 vfs_op_exit_locked(mp); 2040 MNT_IUNLOCK(mp); 2041 return (EBUSY); 2042 } 2043 2044 mp->mnt_kern_flag |= MNTK_SUSPEND; 2045 mp->mnt_susp_owner = curthread; 2046 if (mp->mnt_writeopcount > 0) 2047 (void) msleep(&mp->mnt_writeopcount, 2048 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0); 2049 else 2050 MNT_IUNLOCK(mp); 2051 if ((error = VFS_SYNC(mp, MNT_SUSPEND)) != 0) { 2052 vfs_write_resume(mp, 0); 2053 /* vfs_write_resume does vfs_op_exit() for us */ 2054 } 2055 return (error); 2056 } 2057 2058 /* 2059 * Request a filesystem to resume write operations. 2060 */ 2061 void 2062 vfs_write_resume(struct mount *mp, int flags) 2063 { 2064 2065 MNT_ILOCK(mp); 2066 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 2067 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner")); 2068 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 | 2069 MNTK_SUSPENDED); 2070 mp->mnt_susp_owner = NULL; 2071 wakeup(&mp->mnt_writeopcount); 2072 wakeup(&mp->mnt_flag); 2073 curthread->td_pflags &= ~TDP_IGNSUSP; 2074 if ((flags & VR_START_WRITE) != 0) { 2075 MNT_REF(mp); 2076 mp->mnt_writeopcount++; 2077 } 2078 MNT_IUNLOCK(mp); 2079 if ((flags & VR_NO_SUSPCLR) == 0) 2080 VFS_SUSP_CLEAN(mp); 2081 vfs_op_exit(mp); 2082 } else if ((flags & VR_START_WRITE) != 0) { 2083 MNT_REF(mp); 2084 vn_start_write_refed(mp, 0, true); 2085 } else { 2086 MNT_IUNLOCK(mp); 2087 } 2088 } 2089 2090 /* 2091 * Helper loop around vfs_write_suspend() for filesystem unmount VFS 2092 * methods. 2093 */ 2094 int 2095 vfs_write_suspend_umnt(struct mount *mp) 2096 { 2097 int error; 2098 2099 KASSERT((curthread->td_pflags & TDP_IGNSUSP) == 0, 2100 ("vfs_write_suspend_umnt: recursed")); 2101 2102 /* dounmount() already called vn_start_write(). */ 2103 for (;;) { 2104 vn_finished_write(mp); 2105 error = vfs_write_suspend(mp, 0); 2106 if (error != 0) { 2107 vn_start_write(NULL, &mp, V_WAIT); 2108 return (error); 2109 } 2110 MNT_ILOCK(mp); 2111 if ((mp->mnt_kern_flag & MNTK_SUSPENDED) != 0) 2112 break; 2113 MNT_IUNLOCK(mp); 2114 vn_start_write(NULL, &mp, V_WAIT); 2115 } 2116 mp->mnt_kern_flag &= ~(MNTK_SUSPENDED | MNTK_SUSPEND2); 2117 wakeup(&mp->mnt_flag); 2118 MNT_IUNLOCK(mp); 2119 curthread->td_pflags |= TDP_IGNSUSP; 2120 return (0); 2121 } 2122 2123 /* 2124 * Implement kqueues for files by translating it to vnode operation. 2125 */ 2126 static int 2127 vn_kqfilter(struct file *fp, struct knote *kn) 2128 { 2129 2130 return (VOP_KQFILTER(fp->f_vnode, kn)); 2131 } 2132 2133 /* 2134 * Simplified in-kernel wrapper calls for extended attribute access. 2135 * Both calls pass in a NULL credential, authorizing as "kernel" access. 2136 * Set IO_NODELOCKED in ioflg if the vnode is already locked. 2137 */ 2138 int 2139 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace, 2140 const char *attrname, int *buflen, char *buf, struct thread *td) 2141 { 2142 struct uio auio; 2143 struct iovec iov; 2144 int error; 2145 2146 iov.iov_len = *buflen; 2147 iov.iov_base = buf; 2148 2149 auio.uio_iov = &iov; 2150 auio.uio_iovcnt = 1; 2151 auio.uio_rw = UIO_READ; 2152 auio.uio_segflg = UIO_SYSSPACE; 2153 auio.uio_td = td; 2154 auio.uio_offset = 0; 2155 auio.uio_resid = *buflen; 2156 2157 if ((ioflg & IO_NODELOCKED) == 0) 2158 vn_lock(vp, LK_SHARED | LK_RETRY); 2159 2160 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2161 2162 /* authorize attribute retrieval as kernel */ 2163 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL, 2164 td); 2165 2166 if ((ioflg & IO_NODELOCKED) == 0) 2167 VOP_UNLOCK(vp); 2168 2169 if (error == 0) { 2170 *buflen = *buflen - auio.uio_resid; 2171 } 2172 2173 return (error); 2174 } 2175 2176 /* 2177 * XXX failure mode if partially written? 2178 */ 2179 int 2180 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace, 2181 const char *attrname, int buflen, char *buf, struct thread *td) 2182 { 2183 struct uio auio; 2184 struct iovec iov; 2185 struct mount *mp; 2186 int error; 2187 2188 iov.iov_len = buflen; 2189 iov.iov_base = buf; 2190 2191 auio.uio_iov = &iov; 2192 auio.uio_iovcnt = 1; 2193 auio.uio_rw = UIO_WRITE; 2194 auio.uio_segflg = UIO_SYSSPACE; 2195 auio.uio_td = td; 2196 auio.uio_offset = 0; 2197 auio.uio_resid = buflen; 2198 2199 if ((ioflg & IO_NODELOCKED) == 0) { 2200 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 2201 return (error); 2202 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2203 } 2204 2205 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2206 2207 /* authorize attribute setting as kernel */ 2208 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td); 2209 2210 if ((ioflg & IO_NODELOCKED) == 0) { 2211 vn_finished_write(mp); 2212 VOP_UNLOCK(vp); 2213 } 2214 2215 return (error); 2216 } 2217 2218 int 2219 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace, 2220 const char *attrname, struct thread *td) 2221 { 2222 struct mount *mp; 2223 int error; 2224 2225 if ((ioflg & IO_NODELOCKED) == 0) { 2226 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0) 2227 return (error); 2228 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2229 } 2230 2231 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held"); 2232 2233 /* authorize attribute removal as kernel */ 2234 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td); 2235 if (error == EOPNOTSUPP) 2236 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, 2237 NULL, td); 2238 2239 if ((ioflg & IO_NODELOCKED) == 0) { 2240 vn_finished_write(mp); 2241 VOP_UNLOCK(vp); 2242 } 2243 2244 return (error); 2245 } 2246 2247 static int 2248 vn_get_ino_alloc_vget(struct mount *mp, void *arg, int lkflags, 2249 struct vnode **rvp) 2250 { 2251 2252 return (VFS_VGET(mp, *(ino_t *)arg, lkflags, rvp)); 2253 } 2254 2255 int 2256 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp) 2257 { 2258 2259 return (vn_vget_ino_gen(vp, vn_get_ino_alloc_vget, &ino, 2260 lkflags, rvp)); 2261 } 2262 2263 int 2264 vn_vget_ino_gen(struct vnode *vp, vn_get_ino_t alloc, void *alloc_arg, 2265 int lkflags, struct vnode **rvp) 2266 { 2267 struct mount *mp; 2268 int ltype, error; 2269 2270 ASSERT_VOP_LOCKED(vp, "vn_vget_ino_get"); 2271 mp = vp->v_mount; 2272 ltype = VOP_ISLOCKED(vp); 2273 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED, 2274 ("vn_vget_ino: vp not locked")); 2275 error = vfs_busy(mp, MBF_NOWAIT); 2276 if (error != 0) { 2277 vfs_ref(mp); 2278 VOP_UNLOCK(vp); 2279 error = vfs_busy(mp, 0); 2280 vn_lock(vp, ltype | LK_RETRY); 2281 vfs_rel(mp); 2282 if (error != 0) 2283 return (ENOENT); 2284 if (VN_IS_DOOMED(vp)) { 2285 vfs_unbusy(mp); 2286 return (ENOENT); 2287 } 2288 } 2289 VOP_UNLOCK(vp); 2290 error = alloc(mp, alloc_arg, lkflags, rvp); 2291 vfs_unbusy(mp); 2292 if (error != 0 || *rvp != vp) 2293 vn_lock(vp, ltype | LK_RETRY); 2294 if (VN_IS_DOOMED(vp)) { 2295 if (error == 0) { 2296 if (*rvp == vp) 2297 vunref(vp); 2298 else 2299 vput(*rvp); 2300 } 2301 error = ENOENT; 2302 } 2303 return (error); 2304 } 2305 2306 int 2307 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio, 2308 struct thread *td) 2309 { 2310 2311 if (vp->v_type != VREG || td == NULL) 2312 return (0); 2313 if ((uoff_t)uio->uio_offset + uio->uio_resid > 2314 lim_cur(td, RLIMIT_FSIZE)) { 2315 PROC_LOCK(td->td_proc); 2316 kern_psignal(td->td_proc, SIGXFSZ); 2317 PROC_UNLOCK(td->td_proc); 2318 return (EFBIG); 2319 } 2320 return (0); 2321 } 2322 2323 int 2324 vn_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 2325 struct thread *td) 2326 { 2327 struct vnode *vp; 2328 2329 vp = fp->f_vnode; 2330 #ifdef AUDIT 2331 vn_lock(vp, LK_SHARED | LK_RETRY); 2332 AUDIT_ARG_VNODE1(vp); 2333 VOP_UNLOCK(vp); 2334 #endif 2335 return (setfmode(td, active_cred, vp, mode)); 2336 } 2337 2338 int 2339 vn_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 2340 struct thread *td) 2341 { 2342 struct vnode *vp; 2343 2344 vp = fp->f_vnode; 2345 #ifdef AUDIT 2346 vn_lock(vp, LK_SHARED | LK_RETRY); 2347 AUDIT_ARG_VNODE1(vp); 2348 VOP_UNLOCK(vp); 2349 #endif 2350 return (setfown(td, active_cred, vp, uid, gid)); 2351 } 2352 2353 void 2354 vn_pages_remove(struct vnode *vp, vm_pindex_t start, vm_pindex_t end) 2355 { 2356 vm_object_t object; 2357 2358 if ((object = vp->v_object) == NULL) 2359 return; 2360 VM_OBJECT_WLOCK(object); 2361 vm_object_page_remove(object, start, end, 0); 2362 VM_OBJECT_WUNLOCK(object); 2363 } 2364 2365 int 2366 vn_bmap_seekhole(struct vnode *vp, u_long cmd, off_t *off, struct ucred *cred) 2367 { 2368 struct vattr va; 2369 daddr_t bn, bnp; 2370 uint64_t bsize; 2371 off_t noff; 2372 int error; 2373 2374 KASSERT(cmd == FIOSEEKHOLE || cmd == FIOSEEKDATA, 2375 ("Wrong command %lu", cmd)); 2376 2377 if (vn_lock(vp, LK_SHARED) != 0) 2378 return (EBADF); 2379 if (vp->v_type != VREG) { 2380 error = ENOTTY; 2381 goto unlock; 2382 } 2383 error = VOP_GETATTR(vp, &va, cred); 2384 if (error != 0) 2385 goto unlock; 2386 noff = *off; 2387 if (noff >= va.va_size) { 2388 error = ENXIO; 2389 goto unlock; 2390 } 2391 bsize = vp->v_mount->mnt_stat.f_iosize; 2392 for (bn = noff / bsize; noff < va.va_size; bn++, noff += bsize - 2393 noff % bsize) { 2394 error = VOP_BMAP(vp, bn, NULL, &bnp, NULL, NULL); 2395 if (error == EOPNOTSUPP) { 2396 error = ENOTTY; 2397 goto unlock; 2398 } 2399 if ((bnp == -1 && cmd == FIOSEEKHOLE) || 2400 (bnp != -1 && cmd == FIOSEEKDATA)) { 2401 noff = bn * bsize; 2402 if (noff < *off) 2403 noff = *off; 2404 goto unlock; 2405 } 2406 } 2407 if (noff > va.va_size) 2408 noff = va.va_size; 2409 /* noff == va.va_size. There is an implicit hole at the end of file. */ 2410 if (cmd == FIOSEEKDATA) 2411 error = ENXIO; 2412 unlock: 2413 VOP_UNLOCK(vp); 2414 if (error == 0) 2415 *off = noff; 2416 return (error); 2417 } 2418 2419 int 2420 vn_seek(struct file *fp, off_t offset, int whence, struct thread *td) 2421 { 2422 struct ucred *cred; 2423 struct vnode *vp; 2424 struct vattr vattr; 2425 off_t foffset, size; 2426 int error, noneg; 2427 2428 cred = td->td_ucred; 2429 vp = fp->f_vnode; 2430 foffset = foffset_lock(fp, 0); 2431 noneg = (vp->v_type != VCHR); 2432 error = 0; 2433 switch (whence) { 2434 case L_INCR: 2435 if (noneg && 2436 (foffset < 0 || 2437 (offset > 0 && foffset > OFF_MAX - offset))) { 2438 error = EOVERFLOW; 2439 break; 2440 } 2441 offset += foffset; 2442 break; 2443 case L_XTND: 2444 vn_lock(vp, LK_SHARED | LK_RETRY); 2445 error = VOP_GETATTR(vp, &vattr, cred); 2446 VOP_UNLOCK(vp); 2447 if (error) 2448 break; 2449 2450 /* 2451 * If the file references a disk device, then fetch 2452 * the media size and use that to determine the ending 2453 * offset. 2454 */ 2455 if (vattr.va_size == 0 && vp->v_type == VCHR && 2456 fo_ioctl(fp, DIOCGMEDIASIZE, &size, cred, td) == 0) 2457 vattr.va_size = size; 2458 if (noneg && 2459 (vattr.va_size > OFF_MAX || 2460 (offset > 0 && vattr.va_size > OFF_MAX - offset))) { 2461 error = EOVERFLOW; 2462 break; 2463 } 2464 offset += vattr.va_size; 2465 break; 2466 case L_SET: 2467 break; 2468 case SEEK_DATA: 2469 error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td); 2470 if (error == ENOTTY) 2471 error = EINVAL; 2472 break; 2473 case SEEK_HOLE: 2474 error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td); 2475 if (error == ENOTTY) 2476 error = EINVAL; 2477 break; 2478 default: 2479 error = EINVAL; 2480 } 2481 if (error == 0 && noneg && offset < 0) 2482 error = EINVAL; 2483 if (error != 0) 2484 goto drop; 2485 VFS_KNOTE_UNLOCKED(vp, 0); 2486 td->td_uretoff.tdu_off = offset; 2487 drop: 2488 foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0); 2489 return (error); 2490 } 2491 2492 int 2493 vn_utimes_perm(struct vnode *vp, struct vattr *vap, struct ucred *cred, 2494 struct thread *td) 2495 { 2496 int error; 2497 2498 /* 2499 * Grant permission if the caller is the owner of the file, or 2500 * the super-user, or has ACL_WRITE_ATTRIBUTES permission on 2501 * on the file. If the time pointer is null, then write 2502 * permission on the file is also sufficient. 2503 * 2504 * From NFSv4.1, draft 21, 6.2.1.3.1, Discussion of Mask Attributes: 2505 * A user having ACL_WRITE_DATA or ACL_WRITE_ATTRIBUTES 2506 * will be allowed to set the times [..] to the current 2507 * server time. 2508 */ 2509 error = VOP_ACCESSX(vp, VWRITE_ATTRIBUTES, cred, td); 2510 if (error != 0 && (vap->va_vaflags & VA_UTIMES_NULL) != 0) 2511 error = VOP_ACCESS(vp, VWRITE, cred, td); 2512 return (error); 2513 } 2514 2515 int 2516 vn_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 2517 { 2518 struct vnode *vp; 2519 int error; 2520 2521 if (fp->f_type == DTYPE_FIFO) 2522 kif->kf_type = KF_TYPE_FIFO; 2523 else 2524 kif->kf_type = KF_TYPE_VNODE; 2525 vp = fp->f_vnode; 2526 vref(vp); 2527 FILEDESC_SUNLOCK(fdp); 2528 error = vn_fill_kinfo_vnode(vp, kif); 2529 vrele(vp); 2530 FILEDESC_SLOCK(fdp); 2531 return (error); 2532 } 2533 2534 static inline void 2535 vn_fill_junk(struct kinfo_file *kif) 2536 { 2537 size_t len, olen; 2538 2539 /* 2540 * Simulate vn_fullpath returning changing values for a given 2541 * vp during e.g. coredump. 2542 */ 2543 len = (arc4random() % (sizeof(kif->kf_path) - 2)) + 1; 2544 olen = strlen(kif->kf_path); 2545 if (len < olen) 2546 strcpy(&kif->kf_path[len - 1], "$"); 2547 else 2548 for (; olen < len; olen++) 2549 strcpy(&kif->kf_path[olen], "A"); 2550 } 2551 2552 int 2553 vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif) 2554 { 2555 struct vattr va; 2556 char *fullpath, *freepath; 2557 int error; 2558 2559 kif->kf_un.kf_file.kf_file_type = vntype_to_kinfo(vp->v_type); 2560 freepath = NULL; 2561 fullpath = "-"; 2562 error = vn_fullpath(vp, &fullpath, &freepath); 2563 if (error == 0) { 2564 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); 2565 } 2566 if (freepath != NULL) 2567 free(freepath, M_TEMP); 2568 2569 KFAIL_POINT_CODE(DEBUG_FP, fill_kinfo_vnode__random_path, 2570 vn_fill_junk(kif); 2571 ); 2572 2573 /* 2574 * Retrieve vnode attributes. 2575 */ 2576 va.va_fsid = VNOVAL; 2577 va.va_rdev = NODEV; 2578 vn_lock(vp, LK_SHARED | LK_RETRY); 2579 error = VOP_GETATTR(vp, &va, curthread->td_ucred); 2580 VOP_UNLOCK(vp); 2581 if (error != 0) 2582 return (error); 2583 if (va.va_fsid != VNOVAL) 2584 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid; 2585 else 2586 kif->kf_un.kf_file.kf_file_fsid = 2587 vp->v_mount->mnt_stat.f_fsid.val[0]; 2588 kif->kf_un.kf_file.kf_file_fsid_freebsd11 = 2589 kif->kf_un.kf_file.kf_file_fsid; /* truncate */ 2590 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid; 2591 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode); 2592 kif->kf_un.kf_file.kf_file_size = va.va_size; 2593 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev; 2594 kif->kf_un.kf_file.kf_file_rdev_freebsd11 = 2595 kif->kf_un.kf_file.kf_file_rdev; /* truncate */ 2596 return (0); 2597 } 2598 2599 int 2600 vn_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size, 2601 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff, 2602 struct thread *td) 2603 { 2604 #ifdef HWPMC_HOOKS 2605 struct pmckern_map_in pkm; 2606 #endif 2607 struct mount *mp; 2608 struct vnode *vp; 2609 vm_object_t object; 2610 vm_prot_t maxprot; 2611 boolean_t writecounted; 2612 int error; 2613 2614 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \ 2615 defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) 2616 /* 2617 * POSIX shared-memory objects are defined to have 2618 * kernel persistence, and are not defined to support 2619 * read(2)/write(2) -- or even open(2). Thus, we can 2620 * use MAP_ASYNC to trade on-disk coherence for speed. 2621 * The shm_open(3) library routine turns on the FPOSIXSHM 2622 * flag to request this behavior. 2623 */ 2624 if ((fp->f_flag & FPOSIXSHM) != 0) 2625 flags |= MAP_NOSYNC; 2626 #endif 2627 vp = fp->f_vnode; 2628 2629 /* 2630 * Ensure that file and memory protections are 2631 * compatible. Note that we only worry about 2632 * writability if mapping is shared; in this case, 2633 * current and max prot are dictated by the open file. 2634 * XXX use the vnode instead? Problem is: what 2635 * credentials do we use for determination? What if 2636 * proc does a setuid? 2637 */ 2638 mp = vp->v_mount; 2639 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) { 2640 maxprot = VM_PROT_NONE; 2641 if ((prot & VM_PROT_EXECUTE) != 0) 2642 return (EACCES); 2643 } else 2644 maxprot = VM_PROT_EXECUTE; 2645 if ((fp->f_flag & FREAD) != 0) 2646 maxprot |= VM_PROT_READ; 2647 else if ((prot & VM_PROT_READ) != 0) 2648 return (EACCES); 2649 2650 /* 2651 * If we are sharing potential changes via MAP_SHARED and we 2652 * are trying to get write permission although we opened it 2653 * without asking for it, bail out. 2654 */ 2655 if ((flags & MAP_SHARED) != 0) { 2656 if ((fp->f_flag & FWRITE) != 0) 2657 maxprot |= VM_PROT_WRITE; 2658 else if ((prot & VM_PROT_WRITE) != 0) 2659 return (EACCES); 2660 } else { 2661 maxprot |= VM_PROT_WRITE; 2662 cap_maxprot |= VM_PROT_WRITE; 2663 } 2664 maxprot &= cap_maxprot; 2665 2666 /* 2667 * For regular files and shared memory, POSIX requires that 2668 * the value of foff be a legitimate offset within the data 2669 * object. In particular, negative offsets are invalid. 2670 * Blocking negative offsets and overflows here avoids 2671 * possible wraparound or user-level access into reserved 2672 * ranges of the data object later. In contrast, POSIX does 2673 * not dictate how offsets are used by device drivers, so in 2674 * the case of a device mapping a negative offset is passed 2675 * on. 2676 */ 2677 if ( 2678 #ifdef _LP64 2679 size > OFF_MAX || 2680 #endif 2681 foff > OFF_MAX - size) 2682 return (EINVAL); 2683 2684 writecounted = FALSE; 2685 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags, vp, 2686 &foff, &object, &writecounted); 2687 if (error != 0) 2688 return (error); 2689 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object, 2690 foff, writecounted, td); 2691 if (error != 0) { 2692 /* 2693 * If this mapping was accounted for in the vnode's 2694 * writecount, then undo that now. 2695 */ 2696 if (writecounted) 2697 vm_pager_release_writecount(object, 0, size); 2698 vm_object_deallocate(object); 2699 } 2700 #ifdef HWPMC_HOOKS 2701 /* Inform hwpmc(4) if an executable is being mapped. */ 2702 if (PMC_HOOK_INSTALLED(PMC_FN_MMAP)) { 2703 if ((prot & VM_PROT_EXECUTE) != 0 && error == 0) { 2704 pkm.pm_file = vp; 2705 pkm.pm_address = (uintptr_t) *addr; 2706 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_MMAP, (void *) &pkm); 2707 } 2708 } 2709 #endif 2710 return (error); 2711 } 2712 2713 void 2714 vn_fsid(struct vnode *vp, struct vattr *va) 2715 { 2716 fsid_t *f; 2717 2718 f = &vp->v_mount->mnt_stat.f_fsid; 2719 va->va_fsid = (uint32_t)f->val[1]; 2720 va->va_fsid <<= sizeof(f->val[1]) * NBBY; 2721 va->va_fsid += (uint32_t)f->val[0]; 2722 } 2723 2724 int 2725 vn_fsync_buf(struct vnode *vp, int waitfor) 2726 { 2727 struct buf *bp, *nbp; 2728 struct bufobj *bo; 2729 struct mount *mp; 2730 int error, maxretry; 2731 2732 error = 0; 2733 maxretry = 10000; /* large, arbitrarily chosen */ 2734 mp = NULL; 2735 if (vp->v_type == VCHR) { 2736 VI_LOCK(vp); 2737 mp = vp->v_rdev->si_mountpt; 2738 VI_UNLOCK(vp); 2739 } 2740 bo = &vp->v_bufobj; 2741 BO_LOCK(bo); 2742 loop1: 2743 /* 2744 * MARK/SCAN initialization to avoid infinite loops. 2745 */ 2746 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 2747 bp->b_vflags &= ~BV_SCANNED; 2748 bp->b_error = 0; 2749 } 2750 2751 /* 2752 * Flush all dirty buffers associated with a vnode. 2753 */ 2754 loop2: 2755 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 2756 if ((bp->b_vflags & BV_SCANNED) != 0) 2757 continue; 2758 bp->b_vflags |= BV_SCANNED; 2759 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) { 2760 if (waitfor != MNT_WAIT) 2761 continue; 2762 if (BUF_LOCK(bp, 2763 LK_EXCLUSIVE | LK_INTERLOCK | LK_SLEEPFAIL, 2764 BO_LOCKPTR(bo)) != 0) { 2765 BO_LOCK(bo); 2766 goto loop1; 2767 } 2768 BO_LOCK(bo); 2769 } 2770 BO_UNLOCK(bo); 2771 KASSERT(bp->b_bufobj == bo, 2772 ("bp %p wrong b_bufobj %p should be %p", 2773 bp, bp->b_bufobj, bo)); 2774 if ((bp->b_flags & B_DELWRI) == 0) 2775 panic("fsync: not dirty"); 2776 if ((vp->v_object != NULL) && (bp->b_flags & B_CLUSTEROK)) { 2777 vfs_bio_awrite(bp); 2778 } else { 2779 bremfree(bp); 2780 bawrite(bp); 2781 } 2782 if (maxretry < 1000) 2783 pause("dirty", hz < 1000 ? 1 : hz / 1000); 2784 BO_LOCK(bo); 2785 goto loop2; 2786 } 2787 2788 /* 2789 * If synchronous the caller expects us to completely resolve all 2790 * dirty buffers in the system. Wait for in-progress I/O to 2791 * complete (which could include background bitmap writes), then 2792 * retry if dirty blocks still exist. 2793 */ 2794 if (waitfor == MNT_WAIT) { 2795 bufobj_wwait(bo, 0, 0); 2796 if (bo->bo_dirty.bv_cnt > 0) { 2797 /* 2798 * If we are unable to write any of these buffers 2799 * then we fail now rather than trying endlessly 2800 * to write them out. 2801 */ 2802 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 2803 if ((error = bp->b_error) != 0) 2804 break; 2805 if ((mp != NULL && mp->mnt_secondary_writes > 0) || 2806 (error == 0 && --maxretry >= 0)) 2807 goto loop1; 2808 if (error == 0) 2809 error = EAGAIN; 2810 } 2811 } 2812 BO_UNLOCK(bo); 2813 if (error != 0) 2814 vn_printf(vp, "fsync: giving up on dirty (error = %d) ", error); 2815 2816 return (error); 2817 } 2818 2819 /* 2820 * Copies a byte range from invp to outvp. Calls VOP_COPY_FILE_RANGE() 2821 * or vn_generic_copy_file_range() after rangelocking the byte ranges, 2822 * to do the actual copy. 2823 * vn_generic_copy_file_range() is factored out, so it can be called 2824 * from a VOP_COPY_FILE_RANGE() call as well, but handles vnodes from 2825 * different file systems. 2826 */ 2827 int 2828 vn_copy_file_range(struct vnode *invp, off_t *inoffp, struct vnode *outvp, 2829 off_t *outoffp, size_t *lenp, unsigned int flags, struct ucred *incred, 2830 struct ucred *outcred, struct thread *fsize_td) 2831 { 2832 int error; 2833 size_t len; 2834 uint64_t uval; 2835 2836 len = *lenp; 2837 *lenp = 0; /* For error returns. */ 2838 error = 0; 2839 2840 /* Do some sanity checks on the arguments. */ 2841 if (invp->v_type == VDIR || outvp->v_type == VDIR) 2842 error = EISDIR; 2843 else if (*inoffp < 0 || *outoffp < 0 || 2844 invp->v_type != VREG || outvp->v_type != VREG) 2845 error = EINVAL; 2846 if (error != 0) 2847 goto out; 2848 2849 /* Ensure offset + len does not wrap around. */ 2850 uval = *inoffp; 2851 uval += len; 2852 if (uval > INT64_MAX) 2853 len = INT64_MAX - *inoffp; 2854 uval = *outoffp; 2855 uval += len; 2856 if (uval > INT64_MAX) 2857 len = INT64_MAX - *outoffp; 2858 if (len == 0) 2859 goto out; 2860 2861 /* 2862 * If the two vnode are for the same file system, call 2863 * VOP_COPY_FILE_RANGE(), otherwise call vn_generic_copy_file_range() 2864 * which can handle copies across multiple file systems. 2865 */ 2866 *lenp = len; 2867 if (invp->v_mount == outvp->v_mount) 2868 error = VOP_COPY_FILE_RANGE(invp, inoffp, outvp, outoffp, 2869 lenp, flags, incred, outcred, fsize_td); 2870 else 2871 error = vn_generic_copy_file_range(invp, inoffp, outvp, 2872 outoffp, lenp, flags, incred, outcred, fsize_td); 2873 out: 2874 return (error); 2875 } 2876 2877 /* 2878 * Test len bytes of data starting at dat for all bytes == 0. 2879 * Return true if all bytes are zero, false otherwise. 2880 * Expects dat to be well aligned. 2881 */ 2882 static bool 2883 mem_iszero(void *dat, int len) 2884 { 2885 int i; 2886 const u_int *p; 2887 const char *cp; 2888 2889 for (p = dat; len > 0; len -= sizeof(*p), p++) { 2890 if (len >= sizeof(*p)) { 2891 if (*p != 0) 2892 return (false); 2893 } else { 2894 cp = (const char *)p; 2895 for (i = 0; i < len; i++, cp++) 2896 if (*cp != '\0') 2897 return (false); 2898 } 2899 } 2900 return (true); 2901 } 2902 2903 /* 2904 * Look for a hole in the output file and, if found, adjust *outoffp 2905 * and *xferp to skip past the hole. 2906 * *xferp is the entire hole length to be written and xfer2 is how many bytes 2907 * to be written as 0's upon return. 2908 */ 2909 static off_t 2910 vn_skip_hole(struct vnode *outvp, off_t xfer2, off_t *outoffp, off_t *xferp, 2911 off_t *dataoffp, off_t *holeoffp, struct ucred *cred) 2912 { 2913 int error; 2914 off_t delta; 2915 2916 if (*holeoffp == 0 || *holeoffp <= *outoffp) { 2917 *dataoffp = *outoffp; 2918 error = VOP_IOCTL(outvp, FIOSEEKDATA, dataoffp, 0, cred, 2919 curthread); 2920 if (error == 0) { 2921 *holeoffp = *dataoffp; 2922 error = VOP_IOCTL(outvp, FIOSEEKHOLE, holeoffp, 0, cred, 2923 curthread); 2924 } 2925 if (error != 0 || *holeoffp == *dataoffp) { 2926 /* 2927 * Since outvp is unlocked, it may be possible for 2928 * another thread to do a truncate(), lseek(), write() 2929 * creating a hole at startoff between the above 2930 * VOP_IOCTL() calls, if the other thread does not do 2931 * rangelocking. 2932 * If that happens, *holeoffp == *dataoffp and finding 2933 * the hole has failed, so disable vn_skip_hole(). 2934 */ 2935 *holeoffp = -1; /* Disable use of vn_skip_hole(). */ 2936 return (xfer2); 2937 } 2938 KASSERT(*dataoffp >= *outoffp, 2939 ("vn_skip_hole: dataoff=%jd < outoff=%jd", 2940 (intmax_t)*dataoffp, (intmax_t)*outoffp)); 2941 KASSERT(*holeoffp > *dataoffp, 2942 ("vn_skip_hole: holeoff=%jd <= dataoff=%jd", 2943 (intmax_t)*holeoffp, (intmax_t)*dataoffp)); 2944 } 2945 2946 /* 2947 * If there is a hole before the data starts, advance *outoffp and 2948 * *xferp past the hole. 2949 */ 2950 if (*dataoffp > *outoffp) { 2951 delta = *dataoffp - *outoffp; 2952 if (delta >= *xferp) { 2953 /* Entire *xferp is a hole. */ 2954 *outoffp += *xferp; 2955 *xferp = 0; 2956 return (0); 2957 } 2958 *xferp -= delta; 2959 *outoffp += delta; 2960 xfer2 = MIN(xfer2, *xferp); 2961 } 2962 2963 /* 2964 * If a hole starts before the end of this xfer2, reduce this xfer2 so 2965 * that the write ends at the start of the hole. 2966 * *holeoffp should always be greater than *outoffp, but for the 2967 * non-INVARIANTS case, check this to make sure xfer2 remains a sane 2968 * value. 2969 */ 2970 if (*holeoffp > *outoffp && *holeoffp < *outoffp + xfer2) 2971 xfer2 = *holeoffp - *outoffp; 2972 return (xfer2); 2973 } 2974 2975 /* 2976 * Write an xfer sized chunk to outvp in blksize blocks from dat. 2977 * dat is a maximum of blksize in length and can be written repeatedly in 2978 * the chunk. 2979 * If growfile == true, just grow the file via vn_truncate_locked() instead 2980 * of doing actual writes. 2981 * If checkhole == true, a hole is being punched, so skip over any hole 2982 * already in the output file. 2983 */ 2984 static int 2985 vn_write_outvp(struct vnode *outvp, char *dat, off_t outoff, off_t xfer, 2986 u_long blksize, bool growfile, bool checkhole, struct ucred *cred) 2987 { 2988 struct mount *mp; 2989 off_t dataoff, holeoff, xfer2; 2990 int error, lckf; 2991 2992 /* 2993 * Loop around doing writes of blksize until write has been completed. 2994 * Lock/unlock on each loop iteration so that a bwillwrite() can be 2995 * done for each iteration, since the xfer argument can be very 2996 * large if there is a large hole to punch in the output file. 2997 */ 2998 error = 0; 2999 holeoff = 0; 3000 do { 3001 xfer2 = MIN(xfer, blksize); 3002 if (checkhole) { 3003 /* 3004 * Punching a hole. Skip writing if there is 3005 * already a hole in the output file. 3006 */ 3007 xfer2 = vn_skip_hole(outvp, xfer2, &outoff, &xfer, 3008 &dataoff, &holeoff, cred); 3009 if (xfer == 0) 3010 break; 3011 if (holeoff < 0) 3012 checkhole = false; 3013 KASSERT(xfer2 > 0, ("vn_write_outvp: xfer2=%jd", 3014 (intmax_t)xfer2)); 3015 } 3016 bwillwrite(); 3017 mp = NULL; 3018 error = vn_start_write(outvp, &mp, V_WAIT); 3019 if (error != 0) 3020 break; 3021 if (growfile) { 3022 error = vn_lock(outvp, LK_EXCLUSIVE); 3023 if (error == 0) { 3024 error = vn_truncate_locked(outvp, outoff + xfer, 3025 false, cred); 3026 VOP_UNLOCK(outvp); 3027 } 3028 } else { 3029 if (MNT_SHARED_WRITES(mp)) 3030 lckf = LK_SHARED; 3031 else 3032 lckf = LK_EXCLUSIVE; 3033 error = vn_lock(outvp, lckf); 3034 if (error == 0) { 3035 error = vn_rdwr(UIO_WRITE, outvp, dat, xfer2, 3036 outoff, UIO_SYSSPACE, IO_NODELOCKED, 3037 curthread->td_ucred, cred, NULL, curthread); 3038 outoff += xfer2; 3039 xfer -= xfer2; 3040 VOP_UNLOCK(outvp); 3041 } 3042 } 3043 if (mp != NULL) 3044 vn_finished_write(mp); 3045 } while (!growfile && xfer > 0 && error == 0); 3046 return (error); 3047 } 3048 3049 /* 3050 * Copy a byte range of one file to another. This function can handle the 3051 * case where invp and outvp are on different file systems. 3052 * It can also be called by a VOP_COPY_FILE_RANGE() to do the work, if there 3053 * is no better file system specific way to do it. 3054 */ 3055 int 3056 vn_generic_copy_file_range(struct vnode *invp, off_t *inoffp, 3057 struct vnode *outvp, off_t *outoffp, size_t *lenp, unsigned int flags, 3058 struct ucred *incred, struct ucred *outcred, struct thread *fsize_td) 3059 { 3060 struct vattr va; 3061 struct mount *mp; 3062 struct uio io; 3063 off_t startoff, endoff, xfer, xfer2; 3064 u_long blksize; 3065 int error, interrupted; 3066 bool cantseek, readzeros, eof, lastblock; 3067 ssize_t aresid; 3068 size_t copylen, len, rem, savlen; 3069 char *dat; 3070 long holein, holeout; 3071 3072 holein = holeout = 0; 3073 savlen = len = *lenp; 3074 error = 0; 3075 interrupted = 0; 3076 dat = NULL; 3077 3078 error = vn_lock(invp, LK_SHARED); 3079 if (error != 0) 3080 goto out; 3081 if (VOP_PATHCONF(invp, _PC_MIN_HOLE_SIZE, &holein) != 0) 3082 holein = 0; 3083 VOP_UNLOCK(invp); 3084 3085 mp = NULL; 3086 error = vn_start_write(outvp, &mp, V_WAIT); 3087 if (error == 0) 3088 error = vn_lock(outvp, LK_EXCLUSIVE); 3089 if (error == 0) { 3090 /* 3091 * If fsize_td != NULL, do a vn_rlimit_fsize() call, 3092 * now that outvp is locked. 3093 */ 3094 if (fsize_td != NULL) { 3095 io.uio_offset = *outoffp; 3096 io.uio_resid = len; 3097 error = vn_rlimit_fsize(outvp, &io, fsize_td); 3098 if (error != 0) 3099 error = EFBIG; 3100 } 3101 if (VOP_PATHCONF(outvp, _PC_MIN_HOLE_SIZE, &holeout) != 0) 3102 holeout = 0; 3103 /* 3104 * Holes that are past EOF do not need to be written as a block 3105 * of zero bytes. So, truncate the output file as far as 3106 * possible and then use va.va_size to decide if writing 0 3107 * bytes is necessary in the loop below. 3108 */ 3109 if (error == 0) 3110 error = VOP_GETATTR(outvp, &va, outcred); 3111 if (error == 0 && va.va_size > *outoffp && va.va_size <= 3112 *outoffp + len) { 3113 #ifdef MAC 3114 error = mac_vnode_check_write(curthread->td_ucred, 3115 outcred, outvp); 3116 if (error == 0) 3117 #endif 3118 error = vn_truncate_locked(outvp, *outoffp, 3119 false, outcred); 3120 if (error == 0) 3121 va.va_size = *outoffp; 3122 } 3123 VOP_UNLOCK(outvp); 3124 } 3125 if (mp != NULL) 3126 vn_finished_write(mp); 3127 if (error != 0) 3128 goto out; 3129 3130 /* 3131 * Set the blksize to the larger of the hole sizes for invp and outvp. 3132 * If hole sizes aren't available, set the blksize to the larger 3133 * f_iosize of invp and outvp. 3134 * This code expects the hole sizes and f_iosizes to be powers of 2. 3135 * This value is clipped at 4Kbytes and 1Mbyte. 3136 */ 3137 blksize = MAX(holein, holeout); 3138 3139 /* Clip len to end at an exact multiple of hole size. */ 3140 if (blksize > 1) { 3141 rem = *inoffp % blksize; 3142 if (rem > 0) 3143 rem = blksize - rem; 3144 if (len > rem && len - rem > blksize) 3145 len = savlen = rounddown(len - rem, blksize) + rem; 3146 } 3147 3148 if (blksize <= 1) 3149 blksize = MAX(invp->v_mount->mnt_stat.f_iosize, 3150 outvp->v_mount->mnt_stat.f_iosize); 3151 if (blksize < 4096) 3152 blksize = 4096; 3153 else if (blksize > 1024 * 1024) 3154 blksize = 1024 * 1024; 3155 dat = malloc(blksize, M_TEMP, M_WAITOK); 3156 3157 /* 3158 * If VOP_IOCTL(FIOSEEKHOLE) works for invp, use it and FIOSEEKDATA 3159 * to find holes. Otherwise, just scan the read block for all 0s 3160 * in the inner loop where the data copying is done. 3161 * Note that some file systems such as NFSv3, NFSv4.0 and NFSv4.1 may 3162 * support holes on the server, but do not support FIOSEEKHOLE. 3163 */ 3164 eof = false; 3165 while (len > 0 && error == 0 && !eof && interrupted == 0) { 3166 endoff = 0; /* To shut up compilers. */ 3167 cantseek = true; 3168 startoff = *inoffp; 3169 copylen = len; 3170 3171 /* 3172 * Find the next data area. If there is just a hole to EOF, 3173 * FIOSEEKDATA should fail and then we drop down into the 3174 * inner loop and create the hole on the outvp file. 3175 * (I do not know if any file system will report a hole to 3176 * EOF via FIOSEEKHOLE, but I am pretty sure FIOSEEKDATA 3177 * will fail for those file systems.) 3178 * 3179 * For input files that don't support FIOSEEKDATA/FIOSEEKHOLE, 3180 * the code just falls through to the inner copy loop. 3181 */ 3182 error = EINVAL; 3183 if (holein > 0) 3184 error = VOP_IOCTL(invp, FIOSEEKDATA, &startoff, 0, 3185 incred, curthread); 3186 if (error == 0) { 3187 endoff = startoff; 3188 error = VOP_IOCTL(invp, FIOSEEKHOLE, &endoff, 0, 3189 incred, curthread); 3190 /* 3191 * Since invp is unlocked, it may be possible for 3192 * another thread to do a truncate(), lseek(), write() 3193 * creating a hole at startoff between the above 3194 * VOP_IOCTL() calls, if the other thread does not do 3195 * rangelocking. 3196 * If that happens, startoff == endoff and finding 3197 * the hole has failed, so set an error. 3198 */ 3199 if (error == 0 && startoff == endoff) 3200 error = EINVAL; /* Any error. Reset to 0. */ 3201 } 3202 if (error == 0) { 3203 if (startoff > *inoffp) { 3204 /* Found hole before data block. */ 3205 xfer = MIN(startoff - *inoffp, len); 3206 if (*outoffp < va.va_size) { 3207 /* Must write 0s to punch hole. */ 3208 xfer2 = MIN(va.va_size - *outoffp, 3209 xfer); 3210 memset(dat, 0, MIN(xfer2, blksize)); 3211 error = vn_write_outvp(outvp, dat, 3212 *outoffp, xfer2, blksize, false, 3213 holeout > 0, outcred); 3214 } 3215 3216 if (error == 0 && *outoffp + xfer > 3217 va.va_size && xfer == len) 3218 /* Grow last block. */ 3219 error = vn_write_outvp(outvp, dat, 3220 *outoffp, xfer, blksize, true, 3221 false, outcred); 3222 if (error == 0) { 3223 *inoffp += xfer; 3224 *outoffp += xfer; 3225 len -= xfer; 3226 if (len < savlen) 3227 interrupted = sig_intr(); 3228 } 3229 } 3230 copylen = MIN(len, endoff - startoff); 3231 cantseek = false; 3232 } else { 3233 cantseek = true; 3234 startoff = *inoffp; 3235 copylen = len; 3236 error = 0; 3237 } 3238 3239 xfer = blksize; 3240 if (cantseek) { 3241 /* 3242 * Set first xfer to end at a block boundary, so that 3243 * holes are more likely detected in the loop below via 3244 * the for all bytes 0 method. 3245 */ 3246 xfer -= (*inoffp % blksize); 3247 } 3248 /* Loop copying the data block. */ 3249 while (copylen > 0 && error == 0 && !eof && interrupted == 0) { 3250 if (copylen < xfer) 3251 xfer = copylen; 3252 error = vn_lock(invp, LK_SHARED); 3253 if (error != 0) 3254 goto out; 3255 error = vn_rdwr(UIO_READ, invp, dat, xfer, 3256 startoff, UIO_SYSSPACE, IO_NODELOCKED, 3257 curthread->td_ucred, incred, &aresid, 3258 curthread); 3259 VOP_UNLOCK(invp); 3260 lastblock = false; 3261 if (error == 0 && aresid > 0) { 3262 /* Stop the copy at EOF on the input file. */ 3263 xfer -= aresid; 3264 eof = true; 3265 lastblock = true; 3266 } 3267 if (error == 0) { 3268 /* 3269 * Skip the write for holes past the initial EOF 3270 * of the output file, unless this is the last 3271 * write of the output file at EOF. 3272 */ 3273 readzeros = cantseek ? mem_iszero(dat, xfer) : 3274 false; 3275 if (xfer == len) 3276 lastblock = true; 3277 if (!cantseek || *outoffp < va.va_size || 3278 lastblock || !readzeros) 3279 error = vn_write_outvp(outvp, dat, 3280 *outoffp, xfer, blksize, 3281 readzeros && lastblock && 3282 *outoffp >= va.va_size, false, 3283 outcred); 3284 if (error == 0) { 3285 *inoffp += xfer; 3286 startoff += xfer; 3287 *outoffp += xfer; 3288 copylen -= xfer; 3289 len -= xfer; 3290 if (len < savlen) 3291 interrupted = sig_intr(); 3292 } 3293 } 3294 xfer = blksize; 3295 } 3296 } 3297 out: 3298 *lenp = savlen - len; 3299 free(dat, M_TEMP); 3300 return (error); 3301 } 3302 3303 static int 3304 vn_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td) 3305 { 3306 struct mount *mp; 3307 struct vnode *vp; 3308 off_t olen, ooffset; 3309 int error; 3310 #ifdef AUDIT 3311 int audited_vnode1 = 0; 3312 #endif 3313 3314 vp = fp->f_vnode; 3315 if (vp->v_type != VREG) 3316 return (ENODEV); 3317 3318 /* Allocating blocks may take a long time, so iterate. */ 3319 for (;;) { 3320 olen = len; 3321 ooffset = offset; 3322 3323 bwillwrite(); 3324 mp = NULL; 3325 error = vn_start_write(vp, &mp, V_WAIT | PCATCH); 3326 if (error != 0) 3327 break; 3328 error = vn_lock(vp, LK_EXCLUSIVE); 3329 if (error != 0) { 3330 vn_finished_write(mp); 3331 break; 3332 } 3333 #ifdef AUDIT 3334 if (!audited_vnode1) { 3335 AUDIT_ARG_VNODE1(vp); 3336 audited_vnode1 = 1; 3337 } 3338 #endif 3339 #ifdef MAC 3340 error = mac_vnode_check_write(td->td_ucred, fp->f_cred, vp); 3341 if (error == 0) 3342 #endif 3343 error = VOP_ALLOCATE(vp, &offset, &len); 3344 VOP_UNLOCK(vp); 3345 vn_finished_write(mp); 3346 3347 if (olen + ooffset != offset + len) { 3348 panic("offset + len changed from %jx/%jx to %jx/%jx", 3349 ooffset, olen, offset, len); 3350 } 3351 if (error != 0 || len == 0) 3352 break; 3353 KASSERT(olen > len, ("Iteration did not make progress?")); 3354 maybe_yield(); 3355 } 3356 3357 return (error); 3358 } 3359 3360 static u_long vn_lock_pair_pause_cnt; 3361 SYSCTL_ULONG(_debug, OID_AUTO, vn_lock_pair_pause, CTLFLAG_RD, 3362 &vn_lock_pair_pause_cnt, 0, 3363 "Count of vn_lock_pair deadlocks"); 3364 3365 u_int vn_lock_pair_pause_max; 3366 SYSCTL_UINT(_debug, OID_AUTO, vn_lock_pair_pause_max, CTLFLAG_RW, 3367 &vn_lock_pair_pause_max, 0, 3368 "Max ticks for vn_lock_pair deadlock avoidance sleep"); 3369 3370 static void 3371 vn_lock_pair_pause(const char *wmesg) 3372 { 3373 atomic_add_long(&vn_lock_pair_pause_cnt, 1); 3374 pause(wmesg, prng32_bounded(vn_lock_pair_pause_max)); 3375 } 3376 3377 /* 3378 * Lock pair of vnodes vp1, vp2, avoiding lock order reversal. 3379 * vp1_locked indicates whether vp1 is exclusively locked; if not, vp1 3380 * must be unlocked. Same for vp2 and vp2_locked. One of the vnodes 3381 * can be NULL. 3382 * 3383 * The function returns with both vnodes exclusively locked, and 3384 * guarantees that it does not create lock order reversal with other 3385 * threads during its execution. Both vnodes could be unlocked 3386 * temporary (and reclaimed). 3387 */ 3388 void 3389 vn_lock_pair(struct vnode *vp1, bool vp1_locked, struct vnode *vp2, 3390 bool vp2_locked) 3391 { 3392 int error; 3393 3394 if (vp1 == NULL && vp2 == NULL) 3395 return; 3396 if (vp1 != NULL) { 3397 if (vp1_locked) 3398 ASSERT_VOP_ELOCKED(vp1, "vp1"); 3399 else 3400 ASSERT_VOP_UNLOCKED(vp1, "vp1"); 3401 } else { 3402 vp1_locked = true; 3403 } 3404 if (vp2 != NULL) { 3405 if (vp2_locked) 3406 ASSERT_VOP_ELOCKED(vp2, "vp2"); 3407 else 3408 ASSERT_VOP_UNLOCKED(vp2, "vp2"); 3409 } else { 3410 vp2_locked = true; 3411 } 3412 if (!vp1_locked && !vp2_locked) { 3413 vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY); 3414 vp1_locked = true; 3415 } 3416 3417 for (;;) { 3418 if (vp1_locked && vp2_locked) 3419 break; 3420 if (vp1_locked && vp2 != NULL) { 3421 if (vp1 != NULL) { 3422 error = VOP_LOCK1(vp2, LK_EXCLUSIVE | LK_NOWAIT, 3423 __FILE__, __LINE__); 3424 if (error == 0) 3425 break; 3426 VOP_UNLOCK(vp1); 3427 vp1_locked = false; 3428 vn_lock_pair_pause("vlp1"); 3429 } 3430 vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); 3431 vp2_locked = true; 3432 } 3433 if (vp2_locked && vp1 != NULL) { 3434 if (vp2 != NULL) { 3435 error = VOP_LOCK1(vp1, LK_EXCLUSIVE | LK_NOWAIT, 3436 __FILE__, __LINE__); 3437 if (error == 0) 3438 break; 3439 VOP_UNLOCK(vp2); 3440 vp2_locked = false; 3441 vn_lock_pair_pause("vlp2"); 3442 } 3443 vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY); 3444 vp1_locked = true; 3445 } 3446 } 3447 if (vp1 != NULL) 3448 ASSERT_VOP_ELOCKED(vp1, "vp1 ret"); 3449 if (vp2 != NULL) 3450 ASSERT_VOP_ELOCKED(vp2, "vp2 ret"); 3451 } 3452