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