1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed 8 * to Berkeley by John Heidemann of the UCLA Ficus project. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bio.h> 39 #include <sys/buf.h> 40 #include <sys/conf.h> 41 #include <sys/event.h> 42 #include <sys/filio.h> 43 #include <sys/kernel.h> 44 #include <sys/limits.h> 45 #include <sys/lock.h> 46 #include <sys/lockf.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/rwlock.h> 51 #include <sys/fcntl.h> 52 #include <sys/unistd.h> 53 #include <sys/vnode.h> 54 #include <sys/dirent.h> 55 #include <sys/poll.h> 56 #include <sys/stat.h> 57 #include <security/audit/audit.h> 58 #include <sys/priv.h> 59 60 #include <security/mac/mac_framework.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_object.h> 64 #include <vm/vm_extern.h> 65 #include <vm/pmap.h> 66 #include <vm/vm_map.h> 67 #include <vm/vm_page.h> 68 #include <vm/vm_pager.h> 69 #include <vm/vnode_pager.h> 70 71 static int vop_nolookup(struct vop_lookup_args *); 72 static int vop_norename(struct vop_rename_args *); 73 static int vop_nostrategy(struct vop_strategy_args *); 74 static int dirent_exists(struct vnode *vp, const char *dirname, 75 struct thread *td); 76 77 static int vop_stdis_text(struct vop_is_text_args *ap); 78 static int vop_stdunset_text(struct vop_unset_text_args *ap); 79 static int vop_stdadd_writecount(struct vop_add_writecount_args *ap); 80 static int vop_stdcopy_file_range(struct vop_copy_file_range_args *ap); 81 static int vop_stdfdatasync(struct vop_fdatasync_args *ap); 82 static int vop_stdgetpages_async(struct vop_getpages_async_args *ap); 83 static int vop_stdread_pgcache(struct vop_read_pgcache_args *ap); 84 static int vop_stdstat(struct vop_stat_args *ap); 85 static int vop_stdvput_pair(struct vop_vput_pair_args *ap); 86 87 /* 88 * This vnode table stores what we want to do if the filesystem doesn't 89 * implement a particular VOP. 90 * 91 * If there is no specific entry here, we will return EOPNOTSUPP. 92 * 93 * Note that every filesystem has to implement either vop_access 94 * or vop_accessx; failing to do so will result in immediate crash 95 * due to stack overflow, as vop_stdaccess() calls vop_stdaccessx(), 96 * which calls vop_stdaccess() etc. 97 */ 98 99 struct vop_vector default_vnodeops = { 100 .vop_default = NULL, 101 .vop_bypass = VOP_EOPNOTSUPP, 102 103 .vop_access = vop_stdaccess, 104 .vop_accessx = vop_stdaccessx, 105 .vop_advise = vop_stdadvise, 106 .vop_advlock = vop_stdadvlock, 107 .vop_advlockasync = vop_stdadvlockasync, 108 .vop_advlockpurge = vop_stdadvlockpurge, 109 .vop_allocate = vop_stdallocate, 110 .vop_deallocate = vop_stddeallocate, 111 .vop_bmap = vop_stdbmap, 112 .vop_close = VOP_NULL, 113 .vop_fsync = VOP_NULL, 114 .vop_stat = vop_stdstat, 115 .vop_fdatasync = vop_stdfdatasync, 116 .vop_getpages = vop_stdgetpages, 117 .vop_getpages_async = vop_stdgetpages_async, 118 .vop_getwritemount = vop_stdgetwritemount, 119 .vop_inactive = VOP_NULL, 120 .vop_need_inactive = vop_stdneed_inactive, 121 .vop_ioctl = vop_stdioctl, 122 .vop_kqfilter = vop_stdkqfilter, 123 .vop_islocked = vop_stdislocked, 124 .vop_lock1 = vop_stdlock, 125 .vop_lookup = vop_nolookup, 126 .vop_open = VOP_NULL, 127 .vop_pathconf = VOP_EINVAL, 128 .vop_poll = vop_nopoll, 129 .vop_putpages = vop_stdputpages, 130 .vop_readlink = VOP_EINVAL, 131 .vop_read_pgcache = vop_stdread_pgcache, 132 .vop_rename = vop_norename, 133 .vop_revoke = VOP_PANIC, 134 .vop_strategy = vop_nostrategy, 135 .vop_unlock = vop_stdunlock, 136 .vop_vptocnp = vop_stdvptocnp, 137 .vop_vptofh = vop_stdvptofh, 138 .vop_unp_bind = vop_stdunp_bind, 139 .vop_unp_connect = vop_stdunp_connect, 140 .vop_unp_detach = vop_stdunp_detach, 141 .vop_is_text = vop_stdis_text, 142 .vop_set_text = vop_stdset_text, 143 .vop_unset_text = vop_stdunset_text, 144 .vop_add_writecount = vop_stdadd_writecount, 145 .vop_copy_file_range = vop_stdcopy_file_range, 146 .vop_vput_pair = vop_stdvput_pair, 147 }; 148 VFS_VOP_VECTOR_REGISTER(default_vnodeops); 149 150 /* 151 * Series of placeholder functions for various error returns for 152 * VOPs. 153 */ 154 155 int 156 vop_eopnotsupp(struct vop_generic_args *ap) 157 { 158 /* 159 printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name); 160 */ 161 162 return (EOPNOTSUPP); 163 } 164 165 int 166 vop_ebadf(struct vop_generic_args *ap) 167 { 168 169 return (EBADF); 170 } 171 172 int 173 vop_enotty(struct vop_generic_args *ap) 174 { 175 176 return (ENOTTY); 177 } 178 179 int 180 vop_einval(struct vop_generic_args *ap) 181 { 182 183 return (EINVAL); 184 } 185 186 int 187 vop_enoent(struct vop_generic_args *ap) 188 { 189 190 return (ENOENT); 191 } 192 193 int 194 vop_eagain(struct vop_generic_args *ap) 195 { 196 197 return (EAGAIN); 198 } 199 200 int 201 vop_null(struct vop_generic_args *ap) 202 { 203 204 return (0); 205 } 206 207 /* 208 * Helper function to panic on some bad VOPs in some filesystems. 209 */ 210 int 211 vop_panic(struct vop_generic_args *ap) 212 { 213 214 panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name); 215 } 216 217 /* 218 * vop_std<something> and vop_no<something> are default functions for use by 219 * filesystems that need the "default reasonable" implementation for a 220 * particular operation. 221 * 222 * The documentation for the operations they implement exists (if it exists) 223 * in the VOP_<SOMETHING>(9) manpage (all uppercase). 224 */ 225 226 /* 227 * Default vop for filesystems that do not support name lookup 228 */ 229 static int 230 vop_nolookup(struct vop_lookup_args *ap) 231 { 232 233 *ap->a_vpp = NULL; 234 return (ENOTDIR); 235 } 236 237 /* 238 * vop_norename: 239 * 240 * Handle unlock and reference counting for arguments of vop_rename 241 * for filesystems that do not implement rename operation. 242 */ 243 static int 244 vop_norename(struct vop_rename_args *ap) 245 { 246 247 vop_rename_fail(ap); 248 return (EOPNOTSUPP); 249 } 250 251 /* 252 * vop_nostrategy: 253 * 254 * Strategy routine for VFS devices that have none. 255 * 256 * BIO_ERROR and B_INVAL must be cleared prior to calling any strategy 257 * routine. Typically this is done for a BIO_READ strategy call. 258 * Typically B_INVAL is assumed to already be clear prior to a write 259 * and should not be cleared manually unless you just made the buffer 260 * invalid. BIO_ERROR should be cleared either way. 261 */ 262 263 static int 264 vop_nostrategy (struct vop_strategy_args *ap) 265 { 266 printf("No strategy for buffer at %p\n", ap->a_bp); 267 vn_printf(ap->a_vp, "vnode "); 268 ap->a_bp->b_ioflags |= BIO_ERROR; 269 ap->a_bp->b_error = EOPNOTSUPP; 270 bufdone(ap->a_bp); 271 return (EOPNOTSUPP); 272 } 273 274 /* 275 * Check if a named file exists in a given directory vnode 276 * 277 * Returns 0 if the file exists, ENOENT if it doesn't, or errors returned by 278 * vn_dir_next_dirent(). 279 */ 280 static int 281 dirent_exists(struct vnode *vp, const char *dirname, struct thread *td) 282 { 283 char *dirbuf; 284 int error, eofflag; 285 size_t dirbuflen, len; 286 off_t off; 287 struct dirent *dp; 288 struct vattr va; 289 290 ASSERT_VOP_LOCKED(vp, "vnode not locked"); 291 KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp)); 292 293 error = VOP_GETATTR(vp, &va, td->td_ucred); 294 if (error != 0) 295 return (error); 296 297 dirbuflen = MAX(DEV_BSIZE, GENERIC_MAXDIRSIZ); 298 if (dirbuflen < va.va_blocksize) 299 dirbuflen = va.va_blocksize; 300 dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK); 301 302 len = 0; 303 off = 0; 304 eofflag = 0; 305 306 for (;;) { 307 error = vn_dir_next_dirent(vp, td, dirbuf, dirbuflen, 308 &dp, &len, &off, &eofflag); 309 if (error != 0) 310 goto out; 311 312 if (len == 0) 313 break; 314 315 if (dp->d_type != DT_WHT && dp->d_fileno != 0 && 316 strcmp(dp->d_name, dirname) == 0) 317 goto out; 318 } 319 320 error = ENOENT; 321 322 out: 323 free(dirbuf, M_TEMP); 324 return (error); 325 } 326 327 int 328 vop_stdaccess(struct vop_access_args *ap) 329 { 330 331 KASSERT((ap->a_accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | 332 VAPPEND)) == 0, ("invalid bit in accmode")); 333 334 return (VOP_ACCESSX(ap->a_vp, ap->a_accmode, ap->a_cred, ap->a_td)); 335 } 336 337 int 338 vop_stdaccessx(struct vop_accessx_args *ap) 339 { 340 int error; 341 accmode_t accmode = ap->a_accmode; 342 343 error = vfs_unixify_accmode(&accmode); 344 if (error != 0) 345 return (error); 346 347 if (accmode == 0) 348 return (0); 349 350 return (VOP_ACCESS(ap->a_vp, accmode, ap->a_cred, ap->a_td)); 351 } 352 353 /* 354 * Advisory record locking support 355 */ 356 int 357 vop_stdadvlock(struct vop_advlock_args *ap) 358 { 359 struct vnode *vp; 360 struct mount *mp; 361 struct vattr vattr; 362 int error; 363 364 vp = ap->a_vp; 365 366 /* 367 * Provide atomicity of open(O_CREAT | O_EXCL | O_EXLOCK) for 368 * local filesystems. See vn_open_cred() for reciprocal part. 369 */ 370 mp = vp->v_mount; 371 if (mp != NULL && (mp->mnt_flag & MNT_LOCAL) != 0 && 372 ap->a_op == F_SETLK && (ap->a_flags & F_FIRSTOPEN) == 0) { 373 VI_LOCK(vp); 374 while ((vp->v_iflag & VI_FOPENING) != 0) 375 msleep(vp, VI_MTX(vp), PLOCK, "lockfo", 0); 376 VI_UNLOCK(vp); 377 } 378 379 if (ap->a_fl->l_whence == SEEK_END) { 380 /* 381 * The NFSv4 server must avoid doing a vn_lock() here, since it 382 * can deadlock the nfsd threads, due to a LOR. Fortunately 383 * the NFSv4 server always uses SEEK_SET and this code is 384 * only required for the SEEK_END case. 385 */ 386 vn_lock(vp, LK_SHARED | LK_RETRY); 387 error = VOP_GETATTR(vp, &vattr, curthread->td_ucred); 388 VOP_UNLOCK(vp); 389 if (error) 390 return (error); 391 } else 392 vattr.va_size = 0; 393 394 return (lf_advlock(ap, &(vp->v_lockf), vattr.va_size)); 395 } 396 397 int 398 vop_stdadvlockasync(struct vop_advlockasync_args *ap) 399 { 400 struct vnode *vp; 401 struct vattr vattr; 402 int error; 403 404 vp = ap->a_vp; 405 if (ap->a_fl->l_whence == SEEK_END) { 406 /* The size argument is only needed for SEEK_END. */ 407 vn_lock(vp, LK_SHARED | LK_RETRY); 408 error = VOP_GETATTR(vp, &vattr, curthread->td_ucred); 409 VOP_UNLOCK(vp); 410 if (error) 411 return (error); 412 } else 413 vattr.va_size = 0; 414 415 return (lf_advlockasync(ap, &(vp->v_lockf), vattr.va_size)); 416 } 417 418 int 419 vop_stdadvlockpurge(struct vop_advlockpurge_args *ap) 420 { 421 struct vnode *vp; 422 423 vp = ap->a_vp; 424 lf_purgelocks(vp, &vp->v_lockf); 425 return (0); 426 } 427 428 /* 429 * vop_stdpathconf: 430 * 431 * Standard implementation of POSIX pathconf, to get information about limits 432 * for a filesystem. 433 * Override per filesystem for the case where the filesystem has smaller 434 * limits. 435 */ 436 int 437 vop_stdpathconf(struct vop_pathconf_args *ap) 438 { 439 440 switch (ap->a_name) { 441 case _PC_ASYNC_IO: 442 *ap->a_retval = _POSIX_ASYNCHRONOUS_IO; 443 return (0); 444 case _PC_PATH_MAX: 445 *ap->a_retval = PATH_MAX; 446 return (0); 447 case _PC_ACL_EXTENDED: 448 case _PC_ACL_NFS4: 449 case _PC_CAP_PRESENT: 450 case _PC_DEALLOC_PRESENT: 451 case _PC_INF_PRESENT: 452 case _PC_MAC_PRESENT: 453 *ap->a_retval = 0; 454 return (0); 455 default: 456 return (EINVAL); 457 } 458 /* NOTREACHED */ 459 } 460 461 /* 462 * Standard lock, unlock and islocked functions. 463 */ 464 int 465 vop_stdlock(struct vop_lock1_args *ap) 466 { 467 struct vnode *vp = ap->a_vp; 468 struct mtx *ilk; 469 470 ilk = VI_MTX(vp); 471 return (lockmgr_lock_flags(vp->v_vnlock, ap->a_flags, 472 &ilk->lock_object, ap->a_file, ap->a_line)); 473 } 474 475 /* See above. */ 476 int 477 vop_stdunlock(struct vop_unlock_args *ap) 478 { 479 struct vnode *vp = ap->a_vp; 480 481 return (lockmgr_unlock(vp->v_vnlock)); 482 } 483 484 /* See above. */ 485 int 486 vop_stdislocked(struct vop_islocked_args *ap) 487 { 488 489 return (lockstatus(ap->a_vp->v_vnlock)); 490 } 491 492 /* 493 * Variants of the above set. 494 * 495 * Differences are: 496 * - shared locking disablement is not supported 497 * - v_vnlock pointer is not honored 498 */ 499 int 500 vop_lock(struct vop_lock1_args *ap) 501 { 502 struct vnode *vp = ap->a_vp; 503 int flags = ap->a_flags; 504 struct mtx *ilk; 505 506 MPASS(vp->v_vnlock == &vp->v_lock); 507 508 if (__predict_false((flags & ~(LK_TYPE_MASK | LK_NODDLKTREAT | LK_RETRY)) != 0)) 509 goto other; 510 511 switch (flags & LK_TYPE_MASK) { 512 case LK_SHARED: 513 return (lockmgr_slock(&vp->v_lock, flags, ap->a_file, ap->a_line)); 514 case LK_EXCLUSIVE: 515 return (lockmgr_xlock(&vp->v_lock, flags, ap->a_file, ap->a_line)); 516 } 517 other: 518 ilk = VI_MTX(vp); 519 return (lockmgr_lock_flags(&vp->v_lock, flags, 520 &ilk->lock_object, ap->a_file, ap->a_line)); 521 } 522 523 int 524 vop_unlock(struct vop_unlock_args *ap) 525 { 526 struct vnode *vp = ap->a_vp; 527 528 MPASS(vp->v_vnlock == &vp->v_lock); 529 530 return (lockmgr_unlock(&vp->v_lock)); 531 } 532 533 int 534 vop_islocked(struct vop_islocked_args *ap) 535 { 536 struct vnode *vp = ap->a_vp; 537 538 MPASS(vp->v_vnlock == &vp->v_lock); 539 540 return (lockstatus(&vp->v_lock)); 541 } 542 543 /* 544 * Return true for select/poll. 545 */ 546 int 547 vop_nopoll(struct vop_poll_args *ap) 548 { 549 550 if (ap->a_events & ~POLLSTANDARD) 551 return (POLLNVAL); 552 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 553 } 554 555 /* 556 * Implement poll for local filesystems that support it. 557 */ 558 int 559 vop_stdpoll(struct vop_poll_args *ap) 560 { 561 if (ap->a_events & ~POLLSTANDARD) 562 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events)); 563 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 564 } 565 566 /* 567 * Return our mount point, as we will take charge of the writes. 568 */ 569 int 570 vop_stdgetwritemount(struct vop_getwritemount_args *ap) 571 { 572 struct mount *mp; 573 struct vnode *vp; 574 575 /* 576 * Note that having a reference does not prevent forced unmount from 577 * setting ->v_mount to NULL after the lock gets released. This is of 578 * no consequence for typical consumers (most notably vn_start_write) 579 * since in this case the vnode is VIRF_DOOMED. Unmount might have 580 * progressed far enough that its completion is only delayed by the 581 * reference obtained here. The consumer only needs to concern itself 582 * with releasing it. 583 */ 584 vp = ap->a_vp; 585 mp = vfs_ref_from_vp(vp); 586 *(ap->a_mpp) = mp; 587 return (0); 588 } 589 590 /* 591 * If the file system doesn't implement VOP_BMAP, then return sensible defaults: 592 * - Return the vnode's bufobj instead of any underlying device's bufobj 593 * - Calculate the physical block number as if there were equal size 594 * consecutive blocks, but 595 * - Report no contiguous runs of blocks. 596 */ 597 int 598 vop_stdbmap(struct vop_bmap_args *ap) 599 { 600 601 if (ap->a_bop != NULL) 602 *ap->a_bop = &ap->a_vp->v_bufobj; 603 if (ap->a_bnp != NULL) 604 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize); 605 if (ap->a_runp != NULL) 606 *ap->a_runp = 0; 607 if (ap->a_runb != NULL) 608 *ap->a_runb = 0; 609 return (0); 610 } 611 612 int 613 vop_stdfsync(struct vop_fsync_args *ap) 614 { 615 616 return (vn_fsync_buf(ap->a_vp, ap->a_waitfor)); 617 } 618 619 static int 620 vop_stdfdatasync(struct vop_fdatasync_args *ap) 621 { 622 623 return (VOP_FSYNC(ap->a_vp, MNT_WAIT, ap->a_td)); 624 } 625 626 int 627 vop_stdfdatasync_buf(struct vop_fdatasync_args *ap) 628 { 629 630 return (vn_fsync_buf(ap->a_vp, MNT_WAIT)); 631 } 632 633 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */ 634 int 635 vop_stdgetpages(struct vop_getpages_args *ap) 636 { 637 638 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 639 ap->a_count, ap->a_rbehind, ap->a_rahead, NULL, NULL); 640 } 641 642 static int 643 vop_stdgetpages_async(struct vop_getpages_async_args *ap) 644 { 645 int error; 646 647 error = VOP_GETPAGES(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind, 648 ap->a_rahead); 649 if (ap->a_iodone != NULL) 650 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error); 651 return (error); 652 } 653 654 int 655 vop_stdkqfilter(struct vop_kqfilter_args *ap) 656 { 657 return vfs_kqfilter(ap); 658 } 659 660 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */ 661 int 662 vop_stdputpages(struct vop_putpages_args *ap) 663 { 664 665 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count, 666 ap->a_sync, ap->a_rtvals); 667 } 668 669 int 670 vop_stdvptofh(struct vop_vptofh_args *ap) 671 { 672 return (EOPNOTSUPP); 673 } 674 675 int 676 vop_stdvptocnp(struct vop_vptocnp_args *ap) 677 { 678 struct vnode *const vp = ap->a_vp; 679 struct vnode **const dvp = ap->a_vpp; 680 char *buf = ap->a_buf; 681 size_t *buflen = ap->a_buflen; 682 char *dirbuf; 683 int i = *buflen; 684 int error = 0, covered = 0; 685 int eofflag, flags, locked; 686 size_t dirbuflen, len; 687 off_t off; 688 ino_t fileno; 689 struct vattr va; 690 struct nameidata nd; 691 struct thread *const td = curthread; 692 struct ucred *const cred = td->td_ucred; 693 struct dirent *dp; 694 struct vnode *mvp; 695 696 if (vp->v_type != VDIR) 697 return (ENOENT); 698 699 error = VOP_GETATTR(vp, &va, cred); 700 if (error) 701 return (error); 702 703 VREF(vp); 704 locked = VOP_ISLOCKED(vp); 705 VOP_UNLOCK(vp); 706 NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE, 707 "..", vp); 708 flags = FREAD; 709 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL); 710 if (error) { 711 vn_lock(vp, locked | LK_RETRY); 712 return (error); 713 } 714 NDFREE_PNBUF(&nd); 715 716 mvp = *dvp = nd.ni_vp; 717 718 if (vp->v_mount != (*dvp)->v_mount && 719 ((*dvp)->v_vflag & VV_ROOT) && 720 ((*dvp)->v_mount->mnt_flag & MNT_UNION)) { 721 *dvp = (*dvp)->v_mount->mnt_vnodecovered; 722 VREF(mvp); 723 VOP_UNLOCK(mvp); 724 vn_close(mvp, FREAD, cred, td); 725 VREF(*dvp); 726 vn_lock(*dvp, LK_SHARED | LK_RETRY); 727 covered = 1; 728 } 729 730 fileno = va.va_fileid; 731 732 dirbuflen = MAX(DEV_BSIZE, GENERIC_MAXDIRSIZ); 733 if (dirbuflen < va.va_blocksize) 734 dirbuflen = va.va_blocksize; 735 dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK); 736 737 if ((*dvp)->v_type != VDIR) { 738 error = ENOENT; 739 goto out; 740 } 741 742 len = 0; 743 off = 0; 744 eofflag = 0; 745 746 for (;;) { 747 /* call VOP_READDIR of parent */ 748 error = vn_dir_next_dirent(*dvp, td, 749 dirbuf, dirbuflen, &dp, &len, &off, &eofflag); 750 if (error != 0) 751 goto out; 752 753 if (len == 0) { 754 error = ENOENT; 755 goto out; 756 } 757 758 if ((dp->d_type != DT_WHT) && 759 (dp->d_fileno == fileno)) { 760 if (covered) { 761 VOP_UNLOCK(*dvp); 762 vn_lock(mvp, LK_SHARED | LK_RETRY); 763 if (dirent_exists(mvp, dp->d_name, td) == 0) { 764 error = ENOENT; 765 VOP_UNLOCK(mvp); 766 vn_lock(*dvp, LK_SHARED | LK_RETRY); 767 goto out; 768 } 769 VOP_UNLOCK(mvp); 770 vn_lock(*dvp, LK_SHARED | LK_RETRY); 771 } 772 i -= dp->d_namlen; 773 774 if (i < 0) { 775 error = ENOMEM; 776 goto out; 777 } 778 if (dp->d_namlen == 1 && dp->d_name[0] == '.') { 779 error = ENOENT; 780 } else { 781 bcopy(dp->d_name, buf + i, dp->d_namlen); 782 error = 0; 783 } 784 goto out; 785 } 786 } 787 788 out: 789 free(dirbuf, M_TEMP); 790 if (!error) { 791 *buflen = i; 792 vref(*dvp); 793 } 794 if (covered) { 795 vput(*dvp); 796 vrele(mvp); 797 } else { 798 VOP_UNLOCK(mvp); 799 vn_close(mvp, FREAD, cred, td); 800 } 801 vn_lock(vp, locked | LK_RETRY); 802 return (error); 803 } 804 805 int 806 vop_stdallocate(struct vop_allocate_args *ap) 807 { 808 #ifdef __notyet__ 809 struct statfs *sfs; 810 off_t maxfilesize = 0; 811 #endif 812 struct iovec aiov; 813 struct vattr vattr, *vap; 814 struct uio auio; 815 off_t fsize, len, cur, offset; 816 uint8_t *buf; 817 struct thread *td; 818 struct vnode *vp; 819 size_t iosize; 820 int error; 821 822 buf = NULL; 823 error = 0; 824 td = curthread; 825 vap = &vattr; 826 vp = ap->a_vp; 827 len = *ap->a_len; 828 offset = *ap->a_offset; 829 830 error = VOP_GETATTR(vp, vap, ap->a_cred); 831 if (error != 0) 832 goto out; 833 fsize = vap->va_size; 834 iosize = vap->va_blocksize; 835 if (iosize == 0) 836 iosize = BLKDEV_IOSIZE; 837 if (iosize > maxphys) 838 iosize = maxphys; 839 buf = malloc(iosize, M_TEMP, M_WAITOK); 840 841 #ifdef __notyet__ 842 /* 843 * Check if the filesystem sets f_maxfilesize; if not use 844 * VOP_SETATTR to perform the check. 845 */ 846 sfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 847 error = VFS_STATFS(vp->v_mount, sfs, td); 848 if (error == 0) 849 maxfilesize = sfs->f_maxfilesize; 850 free(sfs, M_STATFS); 851 if (error != 0) 852 goto out; 853 if (maxfilesize) { 854 if (offset > maxfilesize || len > maxfilesize || 855 offset + len > maxfilesize) { 856 error = EFBIG; 857 goto out; 858 } 859 } else 860 #endif 861 if (offset + len > vap->va_size) { 862 /* 863 * Test offset + len against the filesystem's maxfilesize. 864 */ 865 VATTR_NULL(vap); 866 vap->va_size = offset + len; 867 error = VOP_SETATTR(vp, vap, ap->a_cred); 868 if (error != 0) 869 goto out; 870 VATTR_NULL(vap); 871 vap->va_size = fsize; 872 error = VOP_SETATTR(vp, vap, ap->a_cred); 873 if (error != 0) 874 goto out; 875 } 876 877 for (;;) { 878 /* 879 * Read and write back anything below the nominal file 880 * size. There's currently no way outside the filesystem 881 * to know whether this area is sparse or not. 882 */ 883 cur = iosize; 884 if ((offset % iosize) != 0) 885 cur -= (offset % iosize); 886 if (cur > len) 887 cur = len; 888 if (offset < fsize) { 889 aiov.iov_base = buf; 890 aiov.iov_len = cur; 891 auio.uio_iov = &aiov; 892 auio.uio_iovcnt = 1; 893 auio.uio_offset = offset; 894 auio.uio_resid = cur; 895 auio.uio_segflg = UIO_SYSSPACE; 896 auio.uio_rw = UIO_READ; 897 auio.uio_td = td; 898 error = VOP_READ(vp, &auio, ap->a_ioflag, ap->a_cred); 899 if (error != 0) 900 break; 901 if (auio.uio_resid > 0) { 902 bzero(buf + cur - auio.uio_resid, 903 auio.uio_resid); 904 } 905 } else { 906 bzero(buf, cur); 907 } 908 909 aiov.iov_base = buf; 910 aiov.iov_len = cur; 911 auio.uio_iov = &aiov; 912 auio.uio_iovcnt = 1; 913 auio.uio_offset = offset; 914 auio.uio_resid = cur; 915 auio.uio_segflg = UIO_SYSSPACE; 916 auio.uio_rw = UIO_WRITE; 917 auio.uio_td = td; 918 919 error = VOP_WRITE(vp, &auio, ap->a_ioflag, ap->a_cred); 920 if (error != 0) 921 break; 922 923 len -= cur; 924 offset += cur; 925 if (len == 0) 926 break; 927 if (should_yield()) 928 break; 929 } 930 931 out: 932 *ap->a_len = len; 933 *ap->a_offset = offset; 934 free(buf, M_TEMP); 935 return (error); 936 } 937 938 static int 939 vp_zerofill(struct vnode *vp, struct vattr *vap, off_t *offsetp, off_t *lenp, 940 int ioflag, struct ucred *cred) 941 { 942 int iosize; 943 int error = 0; 944 struct iovec aiov; 945 struct uio auio; 946 struct thread *td; 947 off_t offset, len; 948 949 iosize = vap->va_blocksize; 950 td = curthread; 951 offset = *offsetp; 952 len = *lenp; 953 954 if (iosize == 0) 955 iosize = BLKDEV_IOSIZE; 956 /* If va_blocksize is 512 bytes, iosize will be 4 kilobytes */ 957 iosize = min(iosize * 8, ZERO_REGION_SIZE); 958 959 while (len > 0) { 960 int xfersize = iosize; 961 if (offset % iosize != 0) 962 xfersize -= offset % iosize; 963 if (xfersize > len) 964 xfersize = len; 965 966 aiov.iov_base = __DECONST(void *, zero_region); 967 aiov.iov_len = xfersize; 968 auio.uio_iov = &aiov; 969 auio.uio_iovcnt = 1; 970 auio.uio_offset = offset; 971 auio.uio_resid = xfersize; 972 auio.uio_segflg = UIO_SYSSPACE; 973 auio.uio_rw = UIO_WRITE; 974 auio.uio_td = td; 975 976 error = VOP_WRITE(vp, &auio, ioflag, cred); 977 if (error != 0) { 978 len -= xfersize - auio.uio_resid; 979 offset += xfersize - auio.uio_resid; 980 break; 981 } 982 983 len -= xfersize; 984 offset += xfersize; 985 } 986 987 *offsetp = offset; 988 *lenp = len; 989 return (error); 990 } 991 992 int 993 vop_stddeallocate(struct vop_deallocate_args *ap) 994 { 995 struct vnode *vp; 996 off_t offset, len; 997 struct ucred *cred; 998 int error; 999 struct vattr va; 1000 off_t noff, xfersize, rem; 1001 1002 vp = ap->a_vp; 1003 offset = *ap->a_offset; 1004 cred = ap->a_cred; 1005 1006 error = VOP_GETATTR(vp, &va, cred); 1007 if (error) 1008 return (error); 1009 1010 len = omin((off_t)va.va_size - offset, *ap->a_len); 1011 while (len > 0) { 1012 noff = offset; 1013 error = vn_bmap_seekhole_locked(vp, FIOSEEKDATA, &noff, cred); 1014 if (error) { 1015 if (error != ENXIO) 1016 /* XXX: Is it okay to fallback further? */ 1017 goto out; 1018 1019 /* 1020 * No more data region to be filled 1021 */ 1022 offset += len; 1023 len = 0; 1024 error = 0; 1025 break; 1026 } 1027 KASSERT(noff >= offset, ("FIOSEEKDATA going backward")); 1028 if (noff != offset) { 1029 xfersize = omin(noff - offset, len); 1030 len -= xfersize; 1031 offset += xfersize; 1032 if (len == 0) 1033 break; 1034 } 1035 error = vn_bmap_seekhole_locked(vp, FIOSEEKHOLE, &noff, cred); 1036 if (error) 1037 goto out; 1038 1039 /* Fill zeroes */ 1040 xfersize = rem = omin(noff - offset, len); 1041 error = vp_zerofill(vp, &va, &offset, &rem, ap->a_ioflag, cred); 1042 if (error) { 1043 len -= xfersize - rem; 1044 goto out; 1045 } 1046 1047 len -= xfersize; 1048 if (should_yield()) 1049 break; 1050 } 1051 /* Handle the case when offset is beyond EOF */ 1052 if (len < 0) 1053 len = 0; 1054 out: 1055 *ap->a_offset = offset; 1056 *ap->a_len = len; 1057 return (error); 1058 } 1059 1060 int 1061 vop_stdadvise(struct vop_advise_args *ap) 1062 { 1063 struct vnode *vp; 1064 struct bufobj *bo; 1065 daddr_t startn, endn; 1066 off_t bstart, bend, start, end; 1067 int bsize, error; 1068 1069 vp = ap->a_vp; 1070 switch (ap->a_advice) { 1071 case POSIX_FADV_WILLNEED: 1072 /* 1073 * Do nothing for now. Filesystems should provide a 1074 * custom method which starts an asynchronous read of 1075 * the requested region. 1076 */ 1077 error = 0; 1078 break; 1079 case POSIX_FADV_DONTNEED: 1080 error = 0; 1081 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1082 if (VN_IS_DOOMED(vp)) { 1083 VOP_UNLOCK(vp); 1084 break; 1085 } 1086 1087 /* 1088 * Round to block boundaries (and later possibly further to 1089 * page boundaries). Applications cannot reasonably be aware 1090 * of the boundaries, and the rounding must be to expand at 1091 * both extremities to cover enough. It still doesn't cover 1092 * read-ahead. For partial blocks, this gives unnecessary 1093 * discarding of buffers but is efficient enough since the 1094 * pages usually remain in VMIO for some time. 1095 */ 1096 bsize = vp->v_bufobj.bo_bsize; 1097 bstart = rounddown(ap->a_start, bsize); 1098 bend = roundup(ap->a_end, bsize); 1099 1100 /* 1101 * Deactivate pages in the specified range from the backing VM 1102 * object. Pages that are resident in the buffer cache will 1103 * remain wired until their corresponding buffers are released 1104 * below. 1105 */ 1106 if (vp->v_object != NULL) { 1107 start = trunc_page(bstart); 1108 end = round_page(bend); 1109 VM_OBJECT_RLOCK(vp->v_object); 1110 vm_object_page_noreuse(vp->v_object, OFF_TO_IDX(start), 1111 OFF_TO_IDX(end)); 1112 VM_OBJECT_RUNLOCK(vp->v_object); 1113 } 1114 1115 bo = &vp->v_bufobj; 1116 BO_RLOCK(bo); 1117 startn = bstart / bsize; 1118 endn = bend / bsize; 1119 error = bnoreuselist(&bo->bo_clean, bo, startn, endn); 1120 if (error == 0) 1121 error = bnoreuselist(&bo->bo_dirty, bo, startn, endn); 1122 BO_RUNLOCK(bo); 1123 VOP_UNLOCK(vp); 1124 break; 1125 default: 1126 error = EINVAL; 1127 break; 1128 } 1129 return (error); 1130 } 1131 1132 int 1133 vop_stdunp_bind(struct vop_unp_bind_args *ap) 1134 { 1135 1136 ap->a_vp->v_unpcb = ap->a_unpcb; 1137 return (0); 1138 } 1139 1140 int 1141 vop_stdunp_connect(struct vop_unp_connect_args *ap) 1142 { 1143 1144 *ap->a_unpcb = ap->a_vp->v_unpcb; 1145 return (0); 1146 } 1147 1148 int 1149 vop_stdunp_detach(struct vop_unp_detach_args *ap) 1150 { 1151 1152 ap->a_vp->v_unpcb = NULL; 1153 return (0); 1154 } 1155 1156 static int 1157 vop_stdis_text(struct vop_is_text_args *ap) 1158 { 1159 1160 return (atomic_load_int(&ap->a_vp->v_writecount) < 0); 1161 } 1162 1163 int 1164 vop_stdset_text(struct vop_set_text_args *ap) 1165 { 1166 struct vnode *vp; 1167 int n; 1168 bool gotref; 1169 1170 vp = ap->a_vp; 1171 1172 n = atomic_load_int(&vp->v_writecount); 1173 for (;;) { 1174 if (__predict_false(n > 0)) { 1175 return (ETXTBSY); 1176 } 1177 1178 /* 1179 * Transition point, we may need to grab a reference on the vnode. 1180 * 1181 * Take the ref early As a safety measure against bogus calls 1182 * to vop_stdunset_text. 1183 */ 1184 if (n == 0) { 1185 gotref = false; 1186 if ((vn_irflag_read(vp) & VIRF_TEXT_REF) != 0) { 1187 vref(vp); 1188 gotref = true; 1189 } 1190 if (atomic_fcmpset_int(&vp->v_writecount, &n, -1)) { 1191 return (0); 1192 } 1193 if (gotref) { 1194 vunref(vp); 1195 } 1196 continue; 1197 } 1198 1199 MPASS(n < 0); 1200 if (atomic_fcmpset_int(&vp->v_writecount, &n, n - 1)) { 1201 return (0); 1202 } 1203 } 1204 __assert_unreachable(); 1205 } 1206 1207 static int 1208 vop_stdunset_text(struct vop_unset_text_args *ap) 1209 { 1210 struct vnode *vp; 1211 int n; 1212 1213 vp = ap->a_vp; 1214 1215 n = atomic_load_int(&vp->v_writecount); 1216 for (;;) { 1217 if (__predict_false(n >= 0)) { 1218 return (EINVAL); 1219 } 1220 1221 /* 1222 * Transition point, we may need to release a reference on the vnode. 1223 */ 1224 if (n == -1) { 1225 if (atomic_fcmpset_int(&vp->v_writecount, &n, 0)) { 1226 if ((vn_irflag_read(vp) & VIRF_TEXT_REF) != 0) { 1227 vunref(vp); 1228 } 1229 return (0); 1230 } 1231 continue; 1232 } 1233 1234 MPASS(n < -1); 1235 if (atomic_fcmpset_int(&vp->v_writecount, &n, n + 1)) { 1236 return (0); 1237 } 1238 } 1239 __assert_unreachable(); 1240 } 1241 1242 static int __always_inline 1243 vop_stdadd_writecount_impl(struct vop_add_writecount_args *ap, bool handle_msync) 1244 { 1245 struct vnode *vp; 1246 struct mount *mp __diagused; 1247 int n; 1248 1249 vp = ap->a_vp; 1250 1251 #ifdef INVARIANTS 1252 mp = vp->v_mount; 1253 if (mp != NULL) { 1254 if (handle_msync) { 1255 VNPASS((mp->mnt_kern_flag & MNTK_NOMSYNC) == 0, vp); 1256 } else { 1257 VNPASS((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0, vp); 1258 } 1259 } 1260 #endif 1261 1262 n = atomic_load_int(&vp->v_writecount); 1263 for (;;) { 1264 if (__predict_false(n < 0)) { 1265 return (ETXTBSY); 1266 } 1267 1268 VNASSERT(n + ap->a_inc >= 0, vp, 1269 ("neg writecount increment %d + %d = %d", n, ap->a_inc, 1270 n + ap->a_inc)); 1271 if (n == 0) { 1272 if (handle_msync) { 1273 vlazy(vp); 1274 } 1275 } 1276 1277 if (atomic_fcmpset_int(&vp->v_writecount, &n, n + ap->a_inc)) { 1278 return (0); 1279 } 1280 } 1281 __assert_unreachable(); 1282 } 1283 1284 int 1285 vop_stdadd_writecount(struct vop_add_writecount_args *ap) 1286 { 1287 1288 return (vop_stdadd_writecount_impl(ap, true)); 1289 } 1290 1291 int 1292 vop_stdadd_writecount_nomsync(struct vop_add_writecount_args *ap) 1293 { 1294 1295 return (vop_stdadd_writecount_impl(ap, false)); 1296 } 1297 1298 int 1299 vop_stdneed_inactive(struct vop_need_inactive_args *ap) 1300 { 1301 1302 return (1); 1303 } 1304 1305 int 1306 vop_stdioctl(struct vop_ioctl_args *ap) 1307 { 1308 struct vnode *vp; 1309 struct vattr va; 1310 off_t *offp; 1311 int error; 1312 1313 switch (ap->a_command) { 1314 case FIOSEEKDATA: 1315 case FIOSEEKHOLE: 1316 vp = ap->a_vp; 1317 error = vn_lock(vp, LK_SHARED); 1318 if (error != 0) 1319 return (EBADF); 1320 if (vp->v_type == VREG) 1321 error = VOP_GETATTR(vp, &va, ap->a_cred); 1322 else 1323 error = ENOTTY; 1324 if (error == 0) { 1325 offp = ap->a_data; 1326 if (*offp < 0 || *offp >= va.va_size) 1327 error = ENXIO; 1328 else if (ap->a_command == FIOSEEKHOLE) 1329 *offp = va.va_size; 1330 } 1331 VOP_UNLOCK(vp); 1332 break; 1333 default: 1334 error = ENOTTY; 1335 break; 1336 } 1337 return (error); 1338 } 1339 1340 /* 1341 * vfs default ops 1342 * used to fill the vfs function table to get reasonable default return values. 1343 */ 1344 int 1345 vfs_stdroot(struct mount *mp, int flags, struct vnode **vpp) 1346 { 1347 1348 return (EOPNOTSUPP); 1349 } 1350 1351 int 1352 vfs_stdstatfs(struct mount *mp, struct statfs *sbp) 1353 { 1354 1355 return (EOPNOTSUPP); 1356 } 1357 1358 int 1359 vfs_stdquotactl(struct mount *mp, int cmds, uid_t uid, void *arg, bool *mp_busy) 1360 { 1361 return (EOPNOTSUPP); 1362 } 1363 1364 int 1365 vfs_stdsync(struct mount *mp, int waitfor) 1366 { 1367 struct vnode *vp, *mvp; 1368 struct thread *td; 1369 int error, lockreq, allerror = 0; 1370 1371 td = curthread; 1372 lockreq = LK_EXCLUSIVE | LK_INTERLOCK; 1373 if (waitfor != MNT_WAIT) 1374 lockreq |= LK_NOWAIT; 1375 /* 1376 * Force stale buffer cache information to be flushed. 1377 */ 1378 loop: 1379 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 1380 if (vp->v_bufobj.bo_dirty.bv_cnt == 0) { 1381 VI_UNLOCK(vp); 1382 continue; 1383 } 1384 if ((error = vget(vp, lockreq)) != 0) { 1385 if (error == ENOENT) { 1386 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 1387 goto loop; 1388 } 1389 continue; 1390 } 1391 error = VOP_FSYNC(vp, waitfor, td); 1392 if (error) 1393 allerror = error; 1394 vput(vp); 1395 } 1396 return (allerror); 1397 } 1398 1399 int 1400 vfs_stdnosync(struct mount *mp, int waitfor) 1401 { 1402 1403 return (0); 1404 } 1405 1406 static int 1407 vop_stdcopy_file_range(struct vop_copy_file_range_args *ap) 1408 { 1409 int error; 1410 1411 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp, 1412 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, ap->a_incred, 1413 ap->a_outcred, ap->a_fsizetd); 1414 return (error); 1415 } 1416 1417 int 1418 vfs_stdvget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 1419 { 1420 1421 return (EOPNOTSUPP); 1422 } 1423 1424 int 1425 vfs_stdfhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp) 1426 { 1427 1428 return (EOPNOTSUPP); 1429 } 1430 1431 int 1432 vfs_stdinit(struct vfsconf *vfsp) 1433 { 1434 1435 return (0); 1436 } 1437 1438 int 1439 vfs_stduninit(struct vfsconf *vfsp) 1440 { 1441 1442 return(0); 1443 } 1444 1445 int 1446 vfs_stdextattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 1447 int attrnamespace, const char *attrname) 1448 { 1449 1450 if (filename_vp != NULL) 1451 VOP_UNLOCK(filename_vp); 1452 return (EOPNOTSUPP); 1453 } 1454 1455 int 1456 vfs_stdsysctl(struct mount *mp, fsctlop_t op, struct sysctl_req *req) 1457 { 1458 1459 return (EOPNOTSUPP); 1460 } 1461 1462 static vop_bypass_t * 1463 bp_by_off(struct vop_vector *vop, struct vop_generic_args *a) 1464 { 1465 1466 return (*(vop_bypass_t **)((char *)vop + a->a_desc->vdesc_vop_offset)); 1467 } 1468 1469 int 1470 vop_sigdefer(struct vop_vector *vop, struct vop_generic_args *a) 1471 { 1472 vop_bypass_t *bp; 1473 int prev_stops, rc; 1474 1475 bp = bp_by_off(vop, a); 1476 MPASS(bp != NULL); 1477 1478 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 1479 rc = bp(a); 1480 sigallowstop(prev_stops); 1481 return (rc); 1482 } 1483 1484 static int 1485 vop_stdstat(struct vop_stat_args *a) 1486 { 1487 struct vattr vattr; 1488 struct vattr *vap; 1489 struct vnode *vp; 1490 struct stat *sb; 1491 int error; 1492 u_short mode; 1493 1494 vp = a->a_vp; 1495 sb = a->a_sb; 1496 1497 error = vop_stat_helper_pre(a); 1498 if (error != 0) 1499 return (error); 1500 1501 vap = &vattr; 1502 1503 /* 1504 * Initialize defaults for new and unusual fields, so that file 1505 * systems which don't support these fields don't need to know 1506 * about them. 1507 */ 1508 vap->va_birthtime.tv_sec = -1; 1509 vap->va_birthtime.tv_nsec = 0; 1510 vap->va_fsid = VNOVAL; 1511 vap->va_gen = 0; 1512 vap->va_rdev = NODEV; 1513 1514 error = VOP_GETATTR(vp, vap, a->a_active_cred); 1515 if (error) 1516 goto out; 1517 1518 /* 1519 * Zero the spare stat fields 1520 */ 1521 bzero(sb, sizeof *sb); 1522 1523 /* 1524 * Copy from vattr table 1525 */ 1526 if (vap->va_fsid != VNOVAL) 1527 sb->st_dev = vap->va_fsid; 1528 else 1529 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 1530 sb->st_ino = vap->va_fileid; 1531 mode = vap->va_mode; 1532 switch (vap->va_type) { 1533 case VREG: 1534 mode |= S_IFREG; 1535 break; 1536 case VDIR: 1537 mode |= S_IFDIR; 1538 break; 1539 case VBLK: 1540 mode |= S_IFBLK; 1541 break; 1542 case VCHR: 1543 mode |= S_IFCHR; 1544 break; 1545 case VLNK: 1546 mode |= S_IFLNK; 1547 break; 1548 case VSOCK: 1549 mode |= S_IFSOCK; 1550 break; 1551 case VFIFO: 1552 mode |= S_IFIFO; 1553 break; 1554 default: 1555 error = EBADF; 1556 goto out; 1557 } 1558 sb->st_mode = mode; 1559 sb->st_nlink = vap->va_nlink; 1560 sb->st_uid = vap->va_uid; 1561 sb->st_gid = vap->va_gid; 1562 sb->st_rdev = vap->va_rdev; 1563 if (vap->va_size > OFF_MAX) { 1564 error = EOVERFLOW; 1565 goto out; 1566 } 1567 sb->st_size = vap->va_size; 1568 sb->st_atim.tv_sec = vap->va_atime.tv_sec; 1569 sb->st_atim.tv_nsec = vap->va_atime.tv_nsec; 1570 sb->st_mtim.tv_sec = vap->va_mtime.tv_sec; 1571 sb->st_mtim.tv_nsec = vap->va_mtime.tv_nsec; 1572 sb->st_ctim.tv_sec = vap->va_ctime.tv_sec; 1573 sb->st_ctim.tv_nsec = vap->va_ctime.tv_nsec; 1574 sb->st_birthtim.tv_sec = vap->va_birthtime.tv_sec; 1575 sb->st_birthtim.tv_nsec = vap->va_birthtime.tv_nsec; 1576 1577 /* 1578 * According to www.opengroup.org, the meaning of st_blksize is 1579 * "a filesystem-specific preferred I/O block size for this 1580 * object. In some filesystem types, this may vary from file 1581 * to file" 1582 * Use minimum/default of PAGE_SIZE (e.g. for VCHR). 1583 */ 1584 1585 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize); 1586 sb->st_flags = vap->va_flags; 1587 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 1588 sb->st_gen = vap->va_gen; 1589 out: 1590 return (vop_stat_helper_post(a, error)); 1591 } 1592 1593 static int 1594 vop_stdread_pgcache(struct vop_read_pgcache_args *ap __unused) 1595 { 1596 return (EJUSTRETURN); 1597 } 1598 1599 static int 1600 vop_stdvput_pair(struct vop_vput_pair_args *ap) 1601 { 1602 struct vnode *dvp, *vp, **vpp; 1603 1604 dvp = ap->a_dvp; 1605 vpp = ap->a_vpp; 1606 vput(dvp); 1607 if (vpp != NULL && ap->a_unlock_vp && (vp = *vpp) != NULL) 1608 vput(vp); 1609 return (0); 1610 } 1611