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 case _PC_HAS_HIDDENSYSTEM: 460 *ap->a_retval = 0; 461 return (0); 462 default: 463 return (EINVAL); 464 } 465 /* NOTREACHED */ 466 } 467 468 /* 469 * Standard lock, unlock and islocked functions. 470 */ 471 int 472 vop_stdlock(struct vop_lock1_args *ap) 473 { 474 struct vnode *vp = ap->a_vp; 475 struct mtx *ilk; 476 477 ilk = VI_MTX(vp); 478 return (lockmgr_lock_flags(vp->v_vnlock, ap->a_flags, 479 &ilk->lock_object, ap->a_file, ap->a_line)); 480 } 481 482 /* See above. */ 483 int 484 vop_stdunlock(struct vop_unlock_args *ap) 485 { 486 struct vnode *vp = ap->a_vp; 487 488 return (lockmgr_unlock(vp->v_vnlock)); 489 } 490 491 /* See above. */ 492 int 493 vop_stdislocked(struct vop_islocked_args *ap) 494 { 495 496 return (lockstatus(ap->a_vp->v_vnlock)); 497 } 498 499 /* 500 * Variants of the above set. 501 * 502 * Differences are: 503 * - shared locking disablement is not supported 504 * - v_vnlock pointer is not honored 505 */ 506 int 507 vop_lock(struct vop_lock1_args *ap) 508 { 509 struct vnode *vp = ap->a_vp; 510 int flags = ap->a_flags; 511 struct mtx *ilk; 512 513 MPASS(vp->v_vnlock == &vp->v_lock); 514 515 if (__predict_false((flags & ~(LK_TYPE_MASK | LK_NODDLKTREAT | LK_RETRY)) != 0)) 516 goto other; 517 518 switch (flags & LK_TYPE_MASK) { 519 case LK_SHARED: 520 return (lockmgr_slock(&vp->v_lock, flags, ap->a_file, ap->a_line)); 521 case LK_EXCLUSIVE: 522 return (lockmgr_xlock(&vp->v_lock, flags, ap->a_file, ap->a_line)); 523 } 524 other: 525 ilk = VI_MTX(vp); 526 return (lockmgr_lock_flags(&vp->v_lock, flags, 527 &ilk->lock_object, ap->a_file, ap->a_line)); 528 } 529 530 int 531 vop_unlock(struct vop_unlock_args *ap) 532 { 533 struct vnode *vp = ap->a_vp; 534 535 MPASS(vp->v_vnlock == &vp->v_lock); 536 537 return (lockmgr_unlock(&vp->v_lock)); 538 } 539 540 int 541 vop_islocked(struct vop_islocked_args *ap) 542 { 543 struct vnode *vp = ap->a_vp; 544 545 MPASS(vp->v_vnlock == &vp->v_lock); 546 547 return (lockstatus(&vp->v_lock)); 548 } 549 550 /* 551 * Return true for select/poll. 552 */ 553 int 554 vop_nopoll(struct vop_poll_args *ap) 555 { 556 557 if (ap->a_events & ~POLLSTANDARD) 558 return (POLLNVAL); 559 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 560 } 561 562 /* 563 * Implement poll for local filesystems that support it. 564 */ 565 int 566 vop_stdpoll(struct vop_poll_args *ap) 567 { 568 if (ap->a_events & ~POLLSTANDARD) 569 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events)); 570 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 571 } 572 573 /* 574 * Return our mount point, as we will take charge of the writes. 575 */ 576 int 577 vop_stdgetwritemount(struct vop_getwritemount_args *ap) 578 { 579 struct mount *mp; 580 struct vnode *vp; 581 582 /* 583 * Note that having a reference does not prevent forced unmount from 584 * setting ->v_mount to NULL after the lock gets released. This is of 585 * no consequence for typical consumers (most notably vn_start_write) 586 * since in this case the vnode is VIRF_DOOMED. Unmount might have 587 * progressed far enough that its completion is only delayed by the 588 * reference obtained here. The consumer only needs to concern itself 589 * with releasing it. 590 */ 591 vp = ap->a_vp; 592 mp = vfs_ref_from_vp(vp); 593 *(ap->a_mpp) = mp; 594 return (0); 595 } 596 597 /* 598 * If the file system doesn't implement VOP_BMAP, then return sensible defaults: 599 * - Return the vnode's bufobj instead of any underlying device's bufobj 600 * - Calculate the physical block number as if there were equal size 601 * consecutive blocks, but 602 * - Report no contiguous runs of blocks. 603 */ 604 int 605 vop_stdbmap(struct vop_bmap_args *ap) 606 { 607 608 if (ap->a_bop != NULL) 609 *ap->a_bop = &ap->a_vp->v_bufobj; 610 if (ap->a_bnp != NULL) 611 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize); 612 if (ap->a_runp != NULL) 613 *ap->a_runp = 0; 614 if (ap->a_runb != NULL) 615 *ap->a_runb = 0; 616 return (0); 617 } 618 619 int 620 vop_stdfsync(struct vop_fsync_args *ap) 621 { 622 623 return (vn_fsync_buf(ap->a_vp, ap->a_waitfor)); 624 } 625 626 static int 627 vop_stdfdatasync(struct vop_fdatasync_args *ap) 628 { 629 630 return (VOP_FSYNC(ap->a_vp, MNT_WAIT, ap->a_td)); 631 } 632 633 int 634 vop_stdfdatasync_buf(struct vop_fdatasync_args *ap) 635 { 636 637 return (vn_fsync_buf(ap->a_vp, MNT_WAIT)); 638 } 639 640 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */ 641 int 642 vop_stdgetpages(struct vop_getpages_args *ap) 643 { 644 645 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 646 ap->a_count, ap->a_rbehind, ap->a_rahead, NULL, NULL); 647 } 648 649 static int 650 vop_stdgetpages_async(struct vop_getpages_async_args *ap) 651 { 652 int error; 653 654 error = VOP_GETPAGES(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind, 655 ap->a_rahead); 656 if (ap->a_iodone != NULL) 657 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error); 658 return (error); 659 } 660 661 int 662 vop_stdkqfilter(struct vop_kqfilter_args *ap) 663 { 664 return vfs_kqfilter(ap); 665 } 666 667 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */ 668 int 669 vop_stdputpages(struct vop_putpages_args *ap) 670 { 671 672 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count, 673 ap->a_sync, ap->a_rtvals); 674 } 675 676 int 677 vop_stdvptofh(struct vop_vptofh_args *ap) 678 { 679 return (EOPNOTSUPP); 680 } 681 682 int 683 vop_stdvptocnp(struct vop_vptocnp_args *ap) 684 { 685 struct vnode *const vp = ap->a_vp; 686 struct vnode **const dvp = ap->a_vpp; 687 char *buf = ap->a_buf; 688 size_t *buflen = ap->a_buflen; 689 char *dirbuf; 690 int i = *buflen; 691 int error = 0, covered = 0; 692 int eofflag, flags, locked; 693 size_t dirbuflen, len; 694 off_t off; 695 ino_t fileno; 696 struct vattr va; 697 struct nameidata nd; 698 struct thread *const td = curthread; 699 struct ucred *const cred = td->td_ucred; 700 struct dirent *dp; 701 struct vnode *mvp; 702 703 if (vp->v_type != VDIR) 704 return (ENOENT); 705 706 error = VOP_GETATTR(vp, &va, cred); 707 if (error) 708 return (error); 709 710 VREF(vp); 711 locked = VOP_ISLOCKED(vp); 712 VOP_UNLOCK(vp); 713 NDINIT_ATVP(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE, 714 "..", vp); 715 flags = FREAD; 716 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_NOAUDIT, cred, NULL); 717 if (error) { 718 vn_lock(vp, locked | LK_RETRY); 719 return (error); 720 } 721 NDFREE_PNBUF(&nd); 722 723 mvp = *dvp = nd.ni_vp; 724 725 if (vp->v_mount != (*dvp)->v_mount && 726 ((*dvp)->v_vflag & VV_ROOT) && 727 ((*dvp)->v_mount->mnt_flag & MNT_UNION)) { 728 *dvp = (*dvp)->v_mount->mnt_vnodecovered; 729 VREF(mvp); 730 VOP_UNLOCK(mvp); 731 vn_close(mvp, FREAD, cred, td); 732 VREF(*dvp); 733 vn_lock(*dvp, LK_SHARED | LK_RETRY); 734 covered = 1; 735 } 736 737 fileno = va.va_fileid; 738 739 dirbuflen = MAX(DEV_BSIZE, GENERIC_MAXDIRSIZ); 740 if (dirbuflen < va.va_blocksize) 741 dirbuflen = va.va_blocksize; 742 dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK); 743 744 if ((*dvp)->v_type != VDIR) { 745 error = ENOENT; 746 goto out; 747 } 748 749 len = 0; 750 off = 0; 751 eofflag = 0; 752 753 for (;;) { 754 /* call VOP_READDIR of parent */ 755 error = vn_dir_next_dirent(*dvp, td, 756 dirbuf, dirbuflen, &dp, &len, &off, &eofflag); 757 if (error != 0) 758 goto out; 759 760 if (len == 0) { 761 error = ENOENT; 762 goto out; 763 } 764 765 if ((dp->d_type != DT_WHT) && 766 (dp->d_fileno == fileno)) { 767 if (covered) { 768 VOP_UNLOCK(*dvp); 769 vn_lock(mvp, LK_SHARED | LK_RETRY); 770 if (dirent_exists(mvp, dp->d_name, td) == 0) { 771 error = ENOENT; 772 VOP_UNLOCK(mvp); 773 vn_lock(*dvp, LK_SHARED | LK_RETRY); 774 goto out; 775 } 776 VOP_UNLOCK(mvp); 777 vn_lock(*dvp, LK_SHARED | LK_RETRY); 778 } 779 i -= dp->d_namlen; 780 781 if (i < 0) { 782 error = ENOMEM; 783 goto out; 784 } 785 if (dp->d_namlen == 1 && dp->d_name[0] == '.') { 786 error = ENOENT; 787 } else { 788 bcopy(dp->d_name, buf + i, dp->d_namlen); 789 error = 0; 790 } 791 goto out; 792 } 793 } 794 795 out: 796 free(dirbuf, M_TEMP); 797 if (!error) { 798 *buflen = i; 799 vref(*dvp); 800 } 801 if (covered) { 802 vput(*dvp); 803 vrele(mvp); 804 } else { 805 VOP_UNLOCK(mvp); 806 vn_close(mvp, FREAD, cred, td); 807 } 808 vn_lock(vp, locked | LK_RETRY); 809 return (error); 810 } 811 812 int 813 vop_stdallocate(struct vop_allocate_args *ap) 814 { 815 #ifdef __notyet__ 816 struct statfs *sfs; 817 off_t maxfilesize = 0; 818 #endif 819 struct iovec aiov; 820 struct vattr vattr, *vap; 821 struct uio auio; 822 off_t fsize, len, cur, offset; 823 uint8_t *buf; 824 struct thread *td; 825 struct vnode *vp; 826 size_t iosize; 827 int error; 828 829 buf = NULL; 830 error = 0; 831 td = curthread; 832 vap = &vattr; 833 vp = ap->a_vp; 834 len = *ap->a_len; 835 offset = *ap->a_offset; 836 837 error = VOP_GETATTR(vp, vap, ap->a_cred); 838 if (error != 0) 839 goto out; 840 fsize = vap->va_size; 841 iosize = vap->va_blocksize; 842 if (iosize == 0) 843 iosize = BLKDEV_IOSIZE; 844 if (iosize > maxphys) 845 iosize = maxphys; 846 buf = malloc(iosize, M_TEMP, M_WAITOK); 847 848 #ifdef __notyet__ 849 /* 850 * Check if the filesystem sets f_maxfilesize; if not use 851 * VOP_SETATTR to perform the check. 852 */ 853 sfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 854 error = VFS_STATFS(vp->v_mount, sfs, td); 855 if (error == 0) 856 maxfilesize = sfs->f_maxfilesize; 857 free(sfs, M_STATFS); 858 if (error != 0) 859 goto out; 860 if (maxfilesize) { 861 if (offset > maxfilesize || len > maxfilesize || 862 offset + len > maxfilesize) { 863 error = EFBIG; 864 goto out; 865 } 866 } else 867 #endif 868 if (offset + len > vap->va_size) { 869 /* 870 * Test offset + len against the filesystem's maxfilesize. 871 */ 872 VATTR_NULL(vap); 873 vap->va_size = offset + len; 874 error = VOP_SETATTR(vp, vap, ap->a_cred); 875 if (error != 0) 876 goto out; 877 VATTR_NULL(vap); 878 vap->va_size = fsize; 879 error = VOP_SETATTR(vp, vap, ap->a_cred); 880 if (error != 0) 881 goto out; 882 } 883 884 for (;;) { 885 /* 886 * Read and write back anything below the nominal file 887 * size. There's currently no way outside the filesystem 888 * to know whether this area is sparse or not. 889 */ 890 cur = iosize; 891 if ((offset % iosize) != 0) 892 cur -= (offset % iosize); 893 if (cur > len) 894 cur = len; 895 if (offset < fsize) { 896 aiov.iov_base = buf; 897 aiov.iov_len = cur; 898 auio.uio_iov = &aiov; 899 auio.uio_iovcnt = 1; 900 auio.uio_offset = offset; 901 auio.uio_resid = cur; 902 auio.uio_segflg = UIO_SYSSPACE; 903 auio.uio_rw = UIO_READ; 904 auio.uio_td = td; 905 error = VOP_READ(vp, &auio, ap->a_ioflag, ap->a_cred); 906 if (error != 0) 907 break; 908 if (auio.uio_resid > 0) { 909 bzero(buf + cur - auio.uio_resid, 910 auio.uio_resid); 911 } 912 } else { 913 bzero(buf, cur); 914 } 915 916 aiov.iov_base = buf; 917 aiov.iov_len = cur; 918 auio.uio_iov = &aiov; 919 auio.uio_iovcnt = 1; 920 auio.uio_offset = offset; 921 auio.uio_resid = cur; 922 auio.uio_segflg = UIO_SYSSPACE; 923 auio.uio_rw = UIO_WRITE; 924 auio.uio_td = td; 925 926 error = VOP_WRITE(vp, &auio, ap->a_ioflag, ap->a_cred); 927 if (error != 0) 928 break; 929 930 len -= cur; 931 offset += cur; 932 if (len == 0) 933 break; 934 if (should_yield()) 935 break; 936 } 937 938 out: 939 *ap->a_len = len; 940 *ap->a_offset = offset; 941 free(buf, M_TEMP); 942 return (error); 943 } 944 945 static int 946 vp_zerofill(struct vnode *vp, struct vattr *vap, off_t *offsetp, off_t *lenp, 947 int ioflag, struct ucred *cred) 948 { 949 int iosize; 950 int error = 0; 951 struct iovec aiov; 952 struct uio auio; 953 struct thread *td; 954 off_t offset, len; 955 956 iosize = vap->va_blocksize; 957 td = curthread; 958 offset = *offsetp; 959 len = *lenp; 960 961 if (iosize == 0) 962 iosize = BLKDEV_IOSIZE; 963 /* If va_blocksize is 512 bytes, iosize will be 4 kilobytes */ 964 iosize = min(iosize * 8, ZERO_REGION_SIZE); 965 966 while (len > 0) { 967 int xfersize = iosize; 968 if (offset % iosize != 0) 969 xfersize -= offset % iosize; 970 if (xfersize > len) 971 xfersize = len; 972 973 aiov.iov_base = __DECONST(void *, zero_region); 974 aiov.iov_len = xfersize; 975 auio.uio_iov = &aiov; 976 auio.uio_iovcnt = 1; 977 auio.uio_offset = offset; 978 auio.uio_resid = xfersize; 979 auio.uio_segflg = UIO_SYSSPACE; 980 auio.uio_rw = UIO_WRITE; 981 auio.uio_td = td; 982 983 error = VOP_WRITE(vp, &auio, ioflag, cred); 984 if (error != 0) { 985 len -= xfersize - auio.uio_resid; 986 offset += xfersize - auio.uio_resid; 987 break; 988 } 989 990 len -= xfersize; 991 offset += xfersize; 992 } 993 994 *offsetp = offset; 995 *lenp = len; 996 return (error); 997 } 998 999 int 1000 vop_stddeallocate(struct vop_deallocate_args *ap) 1001 { 1002 struct vnode *vp; 1003 off_t offset, len; 1004 struct ucred *cred; 1005 int error; 1006 struct vattr va; 1007 off_t noff, xfersize, rem; 1008 1009 vp = ap->a_vp; 1010 offset = *ap->a_offset; 1011 cred = ap->a_cred; 1012 1013 error = VOP_GETATTR(vp, &va, cred); 1014 if (error) 1015 return (error); 1016 1017 len = omin((off_t)va.va_size - offset, *ap->a_len); 1018 while (len > 0) { 1019 noff = offset; 1020 error = vn_bmap_seekhole_locked(vp, FIOSEEKDATA, &noff, cred); 1021 if (error) { 1022 if (error != ENXIO) 1023 /* XXX: Is it okay to fallback further? */ 1024 goto out; 1025 1026 /* 1027 * No more data region to be filled 1028 */ 1029 offset += len; 1030 len = 0; 1031 error = 0; 1032 break; 1033 } 1034 KASSERT(noff >= offset, ("FIOSEEKDATA going backward")); 1035 if (noff != offset) { 1036 xfersize = omin(noff - offset, len); 1037 len -= xfersize; 1038 offset += xfersize; 1039 if (len == 0) 1040 break; 1041 } 1042 error = vn_bmap_seekhole_locked(vp, FIOSEEKHOLE, &noff, cred); 1043 if (error) 1044 goto out; 1045 1046 /* Fill zeroes */ 1047 xfersize = rem = omin(noff - offset, len); 1048 error = vp_zerofill(vp, &va, &offset, &rem, ap->a_ioflag, cred); 1049 if (error) { 1050 len -= xfersize - rem; 1051 goto out; 1052 } 1053 1054 len -= xfersize; 1055 if (should_yield()) 1056 break; 1057 } 1058 /* Handle the case when offset is beyond EOF */ 1059 if (len < 0) 1060 len = 0; 1061 out: 1062 *ap->a_offset = offset; 1063 *ap->a_len = len; 1064 return (error); 1065 } 1066 1067 int 1068 vop_stdadvise(struct vop_advise_args *ap) 1069 { 1070 struct vnode *vp; 1071 struct bufobj *bo; 1072 uintmax_t bstart, bend; 1073 daddr_t startn, endn; 1074 int bsize, error; 1075 1076 vp = ap->a_vp; 1077 switch (ap->a_advice) { 1078 case POSIX_FADV_WILLNEED: 1079 /* 1080 * Do nothing for now. Filesystems should provide a 1081 * custom method which starts an asynchronous read of 1082 * the requested region. 1083 */ 1084 error = 0; 1085 break; 1086 case POSIX_FADV_DONTNEED: 1087 error = 0; 1088 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1089 if (VN_IS_DOOMED(vp)) { 1090 VOP_UNLOCK(vp); 1091 break; 1092 } 1093 1094 /* 1095 * Round to block boundaries (and later possibly further to 1096 * page boundaries). Applications cannot reasonably be aware 1097 * of the boundaries, and the rounding must be to expand at 1098 * both extremities to cover enough. It still doesn't cover 1099 * read-ahead. For partial blocks, this gives unnecessary 1100 * discarding of buffers but is efficient enough since the 1101 * pages usually remain in VMIO for some time. 1102 */ 1103 bsize = vp->v_bufobj.bo_bsize; 1104 bstart = rounddown(ap->a_start, bsize); 1105 bend = ap->a_end; 1106 bend = roundup(bend, bsize); 1107 1108 /* 1109 * Deactivate pages in the specified range from the backing VM 1110 * object. Pages that are resident in the buffer cache will 1111 * remain wired until their corresponding buffers are released 1112 * below. 1113 */ 1114 if (vp->v_object != NULL) { 1115 VM_OBJECT_RLOCK(vp->v_object); 1116 vm_object_page_noreuse(vp->v_object, 1117 OFF_TO_IDX(trunc_page(bstart)), 1118 OFF_TO_IDX(round_page(bend))); 1119 VM_OBJECT_RUNLOCK(vp->v_object); 1120 } 1121 1122 bo = &vp->v_bufobj; 1123 startn = bstart / bsize; 1124 endn = bend / bsize; 1125 BO_RLOCK(bo); 1126 error = bnoreuselist(&bo->bo_clean, bo, startn, endn); 1127 if (error == 0) 1128 error = bnoreuselist(&bo->bo_dirty, bo, startn, endn); 1129 BO_RUNLOCK(bo); 1130 VOP_UNLOCK(vp); 1131 break; 1132 default: 1133 error = EINVAL; 1134 break; 1135 } 1136 return (error); 1137 } 1138 1139 int 1140 vop_stdunp_bind(struct vop_unp_bind_args *ap) 1141 { 1142 1143 ap->a_vp->v_unpcb = ap->a_unpcb; 1144 return (0); 1145 } 1146 1147 int 1148 vop_stdunp_connect(struct vop_unp_connect_args *ap) 1149 { 1150 1151 *ap->a_unpcb = ap->a_vp->v_unpcb; 1152 return (0); 1153 } 1154 1155 int 1156 vop_stdunp_detach(struct vop_unp_detach_args *ap) 1157 { 1158 1159 ap->a_vp->v_unpcb = NULL; 1160 return (0); 1161 } 1162 1163 static int 1164 vop_stdis_text(struct vop_is_text_args *ap) 1165 { 1166 1167 return ((int)atomic_load_int(&ap->a_vp->v_writecount) < 0); 1168 } 1169 1170 int 1171 vop_stdset_text(struct vop_set_text_args *ap) 1172 { 1173 struct vnode *vp; 1174 int n; 1175 bool gotref; 1176 1177 vp = ap->a_vp; 1178 1179 n = atomic_load_int(&vp->v_writecount); 1180 for (;;) { 1181 if (__predict_false(n > 0)) { 1182 return (ETXTBSY); 1183 } 1184 1185 /* 1186 * Transition point, we may need to grab a reference on the vnode. 1187 * 1188 * Take the ref early As a safety measure against bogus calls 1189 * to vop_stdunset_text. 1190 */ 1191 if (n == 0) { 1192 gotref = false; 1193 if ((vn_irflag_read(vp) & VIRF_TEXT_REF) != 0) { 1194 vref(vp); 1195 gotref = true; 1196 } 1197 if (atomic_fcmpset_int(&vp->v_writecount, &n, -1)) { 1198 return (0); 1199 } 1200 if (gotref) { 1201 vunref(vp); 1202 } 1203 continue; 1204 } 1205 1206 MPASS(n < 0); 1207 if (atomic_fcmpset_int(&vp->v_writecount, &n, n - 1)) { 1208 return (0); 1209 } 1210 } 1211 __assert_unreachable(); 1212 } 1213 1214 static int 1215 vop_stdunset_text(struct vop_unset_text_args *ap) 1216 { 1217 struct vnode *vp; 1218 int n; 1219 1220 vp = ap->a_vp; 1221 1222 n = atomic_load_int(&vp->v_writecount); 1223 for (;;) { 1224 if (__predict_false(n >= 0)) { 1225 return (EINVAL); 1226 } 1227 1228 /* 1229 * Transition point, we may need to release a reference on the vnode. 1230 */ 1231 if (n == -1) { 1232 if (atomic_fcmpset_int(&vp->v_writecount, &n, 0)) { 1233 if ((vn_irflag_read(vp) & VIRF_TEXT_REF) != 0) { 1234 vunref(vp); 1235 } 1236 return (0); 1237 } 1238 continue; 1239 } 1240 1241 MPASS(n < -1); 1242 if (atomic_fcmpset_int(&vp->v_writecount, &n, n + 1)) { 1243 return (0); 1244 } 1245 } 1246 __assert_unreachable(); 1247 } 1248 1249 static __always_inline int 1250 vop_stdadd_writecount_impl(struct vop_add_writecount_args *ap, bool handle_msync) 1251 { 1252 struct vnode *vp; 1253 struct mount *mp __diagused; 1254 int n; 1255 1256 vp = ap->a_vp; 1257 1258 #ifdef INVARIANTS 1259 mp = vp->v_mount; 1260 if (mp != NULL) { 1261 if (handle_msync) { 1262 VNPASS((mp->mnt_kern_flag & MNTK_NOMSYNC) == 0, vp); 1263 } else { 1264 VNPASS((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0, vp); 1265 } 1266 } 1267 #endif 1268 1269 n = atomic_load_int(&vp->v_writecount); 1270 for (;;) { 1271 if (__predict_false(n < 0)) { 1272 return (ETXTBSY); 1273 } 1274 1275 VNASSERT(n + ap->a_inc >= 0, vp, 1276 ("neg writecount increment %d + %d = %d", n, ap->a_inc, 1277 n + ap->a_inc)); 1278 if (n == 0) { 1279 if (handle_msync) { 1280 vlazy(vp); 1281 } 1282 } 1283 1284 if (atomic_fcmpset_int(&vp->v_writecount, &n, n + ap->a_inc)) { 1285 return (0); 1286 } 1287 } 1288 __assert_unreachable(); 1289 } 1290 1291 int 1292 vop_stdadd_writecount(struct vop_add_writecount_args *ap) 1293 { 1294 1295 return (vop_stdadd_writecount_impl(ap, true)); 1296 } 1297 1298 int 1299 vop_stdadd_writecount_nomsync(struct vop_add_writecount_args *ap) 1300 { 1301 1302 return (vop_stdadd_writecount_impl(ap, false)); 1303 } 1304 1305 int 1306 vop_stdneed_inactive(struct vop_need_inactive_args *ap) 1307 { 1308 1309 return (1); 1310 } 1311 1312 int 1313 vop_stdinotify(struct vop_inotify_args *ap) 1314 { 1315 vn_inotify(ap->a_vp, ap->a_dvp, ap->a_cnp, ap->a_event, ap->a_cookie); 1316 return (0); 1317 } 1318 1319 int 1320 vop_stdinotify_add_watch(struct vop_inotify_add_watch_args *ap) 1321 { 1322 return (vn_inotify_add_watch(ap->a_vp, ap->a_sc, ap->a_mask, 1323 ap->a_wdp, ap->a_td)); 1324 } 1325 1326 int 1327 vop_stdioctl(struct vop_ioctl_args *ap) 1328 { 1329 struct vnode *vp; 1330 struct vattr va; 1331 off_t *offp; 1332 int error; 1333 1334 switch (ap->a_command) { 1335 case FIOSEEKDATA: 1336 case FIOSEEKHOLE: 1337 vp = ap->a_vp; 1338 error = vn_lock(vp, LK_SHARED); 1339 if (error != 0) 1340 return (EBADF); 1341 if (vp->v_type == VREG) 1342 error = VOP_GETATTR(vp, &va, ap->a_cred); 1343 else 1344 error = ENOTTY; 1345 if (error == 0) { 1346 offp = ap->a_data; 1347 if (*offp < 0 || *offp >= va.va_size) 1348 error = ENXIO; 1349 else if (ap->a_command == FIOSEEKHOLE) 1350 *offp = va.va_size; 1351 } 1352 VOP_UNLOCK(vp); 1353 break; 1354 default: 1355 error = ENOTTY; 1356 break; 1357 } 1358 return (error); 1359 } 1360 1361 /* 1362 * vfs default ops 1363 * used to fill the vfs function table to get reasonable default return values. 1364 */ 1365 int 1366 vfs_stdroot(struct mount *mp, int flags, struct vnode **vpp) 1367 { 1368 1369 return (EOPNOTSUPP); 1370 } 1371 1372 int 1373 vfs_stdstatfs(struct mount *mp, struct statfs *sbp) 1374 { 1375 1376 return (EOPNOTSUPP); 1377 } 1378 1379 int 1380 vfs_stdquotactl(struct mount *mp, int cmds, uid_t uid, void *arg, bool *mp_busy) 1381 { 1382 return (EOPNOTSUPP); 1383 } 1384 1385 int 1386 vfs_stdsync(struct mount *mp, int waitfor) 1387 { 1388 struct vnode *vp, *mvp; 1389 struct thread *td; 1390 int error, lockreq, allerror = 0; 1391 1392 td = curthread; 1393 lockreq = LK_EXCLUSIVE | LK_INTERLOCK; 1394 if (waitfor != MNT_WAIT) 1395 lockreq |= LK_NOWAIT; 1396 /* 1397 * Force stale buffer cache information to be flushed. 1398 */ 1399 loop: 1400 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 1401 if (vp->v_bufobj.bo_dirty.bv_cnt == 0) { 1402 VI_UNLOCK(vp); 1403 continue; 1404 } 1405 if ((error = vget(vp, lockreq)) != 0) { 1406 if (error == ENOENT) { 1407 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 1408 goto loop; 1409 } 1410 continue; 1411 } 1412 error = VOP_FSYNC(vp, waitfor, td); 1413 if (error) 1414 allerror = error; 1415 vput(vp); 1416 } 1417 return (allerror); 1418 } 1419 1420 int 1421 vfs_stdnosync(struct mount *mp, int waitfor) 1422 { 1423 1424 return (0); 1425 } 1426 1427 static int 1428 vop_stdcopy_file_range(struct vop_copy_file_range_args *ap) 1429 { 1430 int error; 1431 1432 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp, 1433 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, ap->a_incred, 1434 ap->a_outcred, ap->a_fsizetd); 1435 return (error); 1436 } 1437 1438 int 1439 vfs_stdvget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 1440 { 1441 1442 return (EOPNOTSUPP); 1443 } 1444 1445 int 1446 vfs_stdfhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp) 1447 { 1448 1449 return (EOPNOTSUPP); 1450 } 1451 1452 int 1453 vfs_stdinit(struct vfsconf *vfsp) 1454 { 1455 1456 return (0); 1457 } 1458 1459 int 1460 vfs_stduninit(struct vfsconf *vfsp) 1461 { 1462 1463 return(0); 1464 } 1465 1466 int 1467 vfs_stdextattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 1468 int attrnamespace, const char *attrname) 1469 { 1470 1471 if (filename_vp != NULL) 1472 VOP_UNLOCK(filename_vp); 1473 return (EOPNOTSUPP); 1474 } 1475 1476 int 1477 vfs_stdsysctl(struct mount *mp, fsctlop_t op, struct sysctl_req *req) 1478 { 1479 1480 return (EOPNOTSUPP); 1481 } 1482 1483 static vop_bypass_t * 1484 bp_by_off(struct vop_vector *vop, struct vop_generic_args *a) 1485 { 1486 1487 return (*(vop_bypass_t **)((char *)vop + a->a_desc->vdesc_vop_offset)); 1488 } 1489 1490 int 1491 vop_sigdefer(struct vop_vector *vop, struct vop_generic_args *a) 1492 { 1493 vop_bypass_t *bp; 1494 int prev_stops, rc; 1495 1496 bp = bp_by_off(vop, a); 1497 MPASS(bp != NULL); 1498 1499 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 1500 rc = bp(a); 1501 sigallowstop(prev_stops); 1502 return (rc); 1503 } 1504 1505 static int 1506 vop_stdstat(struct vop_stat_args *a) 1507 { 1508 struct vattr vattr; 1509 struct vattr *vap; 1510 struct vnode *vp; 1511 struct stat *sb; 1512 int error; 1513 u_short mode; 1514 1515 vp = a->a_vp; 1516 sb = a->a_sb; 1517 1518 error = vop_stat_helper_pre(a); 1519 if (error != 0) 1520 return (error); 1521 1522 vap = &vattr; 1523 1524 /* 1525 * Initialize defaults for new and unusual fields, so that file 1526 * systems which don't support these fields don't need to know 1527 * about them. 1528 */ 1529 vap->va_birthtime.tv_sec = -1; 1530 vap->va_birthtime.tv_nsec = 0; 1531 vap->va_fsid = VNOVAL; 1532 vap->va_gen = 0; 1533 vap->va_rdev = NODEV; 1534 vap->va_filerev = 0; 1535 vap->va_bsdflags = 0; 1536 1537 error = VOP_GETATTR(vp, vap, a->a_active_cred); 1538 if (error) 1539 goto out; 1540 1541 /* 1542 * Zero the spare stat fields 1543 */ 1544 bzero(sb, sizeof *sb); 1545 1546 /* 1547 * Copy from vattr table 1548 */ 1549 if (vap->va_fsid != VNOVAL) 1550 sb->st_dev = vap->va_fsid; 1551 else 1552 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0]; 1553 sb->st_ino = vap->va_fileid; 1554 mode = vap->va_mode; 1555 switch (vap->va_type) { 1556 case VREG: 1557 mode |= S_IFREG; 1558 break; 1559 case VDIR: 1560 mode |= S_IFDIR; 1561 break; 1562 case VBLK: 1563 mode |= S_IFBLK; 1564 break; 1565 case VCHR: 1566 mode |= S_IFCHR; 1567 break; 1568 case VLNK: 1569 mode |= S_IFLNK; 1570 break; 1571 case VSOCK: 1572 mode |= S_IFSOCK; 1573 break; 1574 case VFIFO: 1575 mode |= S_IFIFO; 1576 break; 1577 default: 1578 error = EBADF; 1579 goto out; 1580 } 1581 sb->st_mode = mode; 1582 sb->st_nlink = vap->va_nlink; 1583 sb->st_uid = vap->va_uid; 1584 sb->st_gid = vap->va_gid; 1585 sb->st_rdev = vap->va_rdev; 1586 if (vap->va_size > OFF_MAX) { 1587 error = EOVERFLOW; 1588 goto out; 1589 } 1590 sb->st_size = vap->va_size; 1591 sb->st_atim.tv_sec = vap->va_atime.tv_sec; 1592 sb->st_atim.tv_nsec = vap->va_atime.tv_nsec; 1593 sb->st_mtim.tv_sec = vap->va_mtime.tv_sec; 1594 sb->st_mtim.tv_nsec = vap->va_mtime.tv_nsec; 1595 sb->st_ctim.tv_sec = vap->va_ctime.tv_sec; 1596 sb->st_ctim.tv_nsec = vap->va_ctime.tv_nsec; 1597 sb->st_birthtim.tv_sec = vap->va_birthtime.tv_sec; 1598 sb->st_birthtim.tv_nsec = vap->va_birthtime.tv_nsec; 1599 1600 /* 1601 * According to www.opengroup.org, the meaning of st_blksize is 1602 * "a filesystem-specific preferred I/O block size for this 1603 * object. In some filesystem types, this may vary from file 1604 * to file" 1605 * Use minimum/default of PAGE_SIZE (e.g. for VCHR). 1606 */ 1607 1608 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize); 1609 sb->st_flags = vap->va_flags; 1610 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 1611 sb->st_gen = vap->va_gen; 1612 sb->st_filerev = vap->va_filerev; 1613 sb->st_bsdflags = vap->va_bsdflags; 1614 out: 1615 return (vop_stat_helper_post(a, error)); 1616 } 1617 1618 static int 1619 vop_stdread_pgcache(struct vop_read_pgcache_args *ap __unused) 1620 { 1621 return (EJUSTRETURN); 1622 } 1623 1624 static int 1625 vop_stdvput_pair(struct vop_vput_pair_args *ap) 1626 { 1627 struct vnode *dvp, *vp, **vpp; 1628 1629 dvp = ap->a_dvp; 1630 vpp = ap->a_vpp; 1631 vput(dvp); 1632 if (vpp != NULL && ap->a_unlock_vp && (vp = *vpp) != NULL) 1633 vput(vp); 1634 return (0); 1635 } 1636 1637 static int 1638 vop_stdgetlowvnode(struct vop_getlowvnode_args *ap) 1639 { 1640 vref(ap->a_vp); 1641 *ap->a_vplp = ap->a_vp; 1642 return (0); 1643 } 1644