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