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