1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed 6 * to Berkeley by John Heidemann of the UCLA Ficus project. 7 * 8 * Source: * @(#)i405_init.c 2.10 92/04/27 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/bio.h> 45 #include <sys/buf.h> 46 #include <sys/conf.h> 47 #include <sys/kernel.h> 48 #include <sys/limits.h> 49 #include <sys/lock.h> 50 #include <sys/malloc.h> 51 #include <sys/mount.h> 52 #include <sys/mutex.h> 53 #include <sys/unistd.h> 54 #include <sys/vnode.h> 55 #include <sys/poll.h> 56 57 #include <vm/vm.h> 58 #include <vm/vm_object.h> 59 #include <vm/vm_extern.h> 60 #include <vm/pmap.h> 61 #include <vm/vm_map.h> 62 #include <vm/vm_page.h> 63 #include <vm/vm_pager.h> 64 #include <vm/vnode_pager.h> 65 66 static int vop_nolookup(struct vop_lookup_args *); 67 static int vop_nostrategy(struct vop_strategy_args *); 68 69 /* 70 * This vnode table stores what we want to do if the filesystem doesn't 71 * implement a particular VOP. 72 * 73 * If there is no specific entry here, we will return EOPNOTSUPP. 74 * 75 */ 76 77 vop_t **default_vnodeop_p; 78 static struct vnodeopv_entry_desc default_vnodeop_entries[] = { 79 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 80 { &vop_advlock_desc, (vop_t *) vop_einval }, 81 { &vop_bmap_desc, (vop_t *) vop_stdbmap }, 82 { &vop_close_desc, (vop_t *) vop_null }, 83 { &vop_createvobject_desc, (vop_t *) vop_stdcreatevobject }, 84 { &vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject }, 85 { &vop_fsync_desc, (vop_t *) vop_null }, 86 { &vop_getpages_desc, (vop_t *) vop_stdgetpages }, 87 { &vop_getvobject_desc, (vop_t *) vop_stdgetvobject }, 88 { &vop_inactive_desc, (vop_t *) vop_stdinactive }, 89 { &vop_ioctl_desc, (vop_t *) vop_enotty }, 90 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, 91 { &vop_lease_desc, (vop_t *) vop_null }, 92 { &vop_lock_desc, (vop_t *) vop_stdlock }, 93 { &vop_lookup_desc, (vop_t *) vop_nolookup }, 94 { &vop_open_desc, (vop_t *) vop_null }, 95 { &vop_pathconf_desc, (vop_t *) vop_einval }, 96 { &vop_poll_desc, (vop_t *) vop_nopoll }, 97 { &vop_putpages_desc, (vop_t *) vop_stdputpages }, 98 { &vop_readlink_desc, (vop_t *) vop_einval }, 99 { &vop_revoke_desc, (vop_t *) vop_revoke }, 100 { &vop_specstrategy_desc, (vop_t *) vop_panic }, 101 { &vop_strategy_desc, (vop_t *) vop_nostrategy }, 102 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, 103 { NULL, NULL } 104 }; 105 106 static struct vnodeopv_desc default_vnodeop_opv_desc = 107 { &default_vnodeop_p, default_vnodeop_entries }; 108 109 VNODEOP_SET(default_vnodeop_opv_desc); 110 111 /* 112 * Series of placeholder functions for various error returns for 113 * VOPs. 114 */ 115 116 int 117 vop_eopnotsupp(struct vop_generic_args *ap) 118 { 119 /* 120 printf("vop_notsupp[%s]\n", ap->a_desc->vdesc_name); 121 */ 122 123 return (EOPNOTSUPP); 124 } 125 126 int 127 vop_ebadf(struct vop_generic_args *ap) 128 { 129 130 return (EBADF); 131 } 132 133 int 134 vop_enotty(struct vop_generic_args *ap) 135 { 136 137 return (ENOTTY); 138 } 139 140 int 141 vop_einval(struct vop_generic_args *ap) 142 { 143 144 return (EINVAL); 145 } 146 147 int 148 vop_null(struct vop_generic_args *ap) 149 { 150 151 return (0); 152 } 153 154 /* 155 * Used to make a defined VOP fall back to the default VOP. 156 */ 157 int 158 vop_defaultop(struct vop_generic_args *ap) 159 { 160 161 return (VOCALL(default_vnodeop_p, ap->a_desc->vdesc_offset, ap)); 162 } 163 164 /* 165 * Helper function to panic on some bad VOPs in some filesystems. 166 */ 167 int 168 vop_panic(struct vop_generic_args *ap) 169 { 170 171 panic("filesystem goof: vop_panic[%s]", ap->a_desc->vdesc_name); 172 } 173 174 /* 175 * vop_std<something> and vop_no<something> are default functions for use by 176 * filesystems that need the "default reasonable" implementation for a 177 * particular operation. 178 * 179 * The documentation for the operations they implement exists (if it exists) 180 * in the VOP_<SOMETHING>(9) manpage (all uppercase). 181 */ 182 183 /* 184 * Default vop for filesystems that do not support name lookup 185 */ 186 static int 187 vop_nolookup(ap) 188 struct vop_lookup_args /* { 189 struct vnode *a_dvp; 190 struct vnode **a_vpp; 191 struct componentname *a_cnp; 192 } */ *ap; 193 { 194 195 *ap->a_vpp = NULL; 196 return (ENOTDIR); 197 } 198 199 /* 200 * vop_nostrategy: 201 * 202 * Strategy routine for VFS devices that have none. 203 * 204 * BIO_ERROR and B_INVAL must be cleared prior to calling any strategy 205 * routine. Typically this is done for a BIO_READ strategy call. 206 * Typically B_INVAL is assumed to already be clear prior to a write 207 * and should not be cleared manually unless you just made the buffer 208 * invalid. BIO_ERROR should be cleared either way. 209 */ 210 211 static int 212 vop_nostrategy (struct vop_strategy_args *ap) 213 { 214 KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)", 215 __func__, ap->a_vp, ap->a_bp->b_vp)); 216 printf("No strategy for buffer at %p\n", ap->a_bp); 217 vprint("vnode", ap->a_vp); 218 vprint("device vnode", ap->a_bp->b_vp); 219 ap->a_bp->b_ioflags |= BIO_ERROR; 220 ap->a_bp->b_error = EOPNOTSUPP; 221 bufdone(ap->a_bp); 222 return (EOPNOTSUPP); 223 } 224 225 /* 226 * vop_stdpathconf: 227 * 228 * Standard implementation of POSIX pathconf, to get information about limits 229 * for a filesystem. 230 * Override per filesystem for the case where the filesystem has smaller 231 * limits. 232 */ 233 int 234 vop_stdpathconf(ap) 235 struct vop_pathconf_args /* { 236 struct vnode *a_vp; 237 int a_name; 238 int *a_retval; 239 } */ *ap; 240 { 241 242 switch (ap->a_name) { 243 case _PC_LINK_MAX: 244 *ap->a_retval = LINK_MAX; 245 return (0); 246 case _PC_MAX_CANON: 247 *ap->a_retval = MAX_CANON; 248 return (0); 249 case _PC_MAX_INPUT: 250 *ap->a_retval = MAX_INPUT; 251 return (0); 252 case _PC_PIPE_BUF: 253 *ap->a_retval = PIPE_BUF; 254 return (0); 255 case _PC_CHOWN_RESTRICTED: 256 *ap->a_retval = 1; 257 return (0); 258 case _PC_VDISABLE: 259 *ap->a_retval = _POSIX_VDISABLE; 260 return (0); 261 default: 262 return (EINVAL); 263 } 264 /* NOTREACHED */ 265 } 266 267 /* 268 * Standard lock, unlock and islocked functions. 269 */ 270 int 271 vop_stdlock(ap) 272 struct vop_lock_args /* { 273 struct vnode *a_vp; 274 int a_flags; 275 struct thread *a_td; 276 } */ *ap; 277 { 278 struct vnode *vp = ap->a_vp; 279 280 #ifndef DEBUG_LOCKS 281 return (lockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp), ap->a_td)); 282 #else 283 return (debuglockmgr(vp->v_vnlock, ap->a_flags, VI_MTX(vp), 284 ap->a_td, "vop_stdlock", vp->filename, vp->line)); 285 #endif 286 } 287 288 /* See above. */ 289 int 290 vop_stdunlock(ap) 291 struct vop_unlock_args /* { 292 struct vnode *a_vp; 293 int a_flags; 294 struct thread *a_td; 295 } */ *ap; 296 { 297 struct vnode *vp = ap->a_vp; 298 299 return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE, VI_MTX(vp), 300 ap->a_td)); 301 } 302 303 /* See above. */ 304 int 305 vop_stdislocked(ap) 306 struct vop_islocked_args /* { 307 struct vnode *a_vp; 308 struct thread *a_td; 309 } */ *ap; 310 { 311 312 return (lockstatus(ap->a_vp->v_vnlock, ap->a_td)); 313 } 314 315 /* Mark the vnode inactive */ 316 int 317 vop_stdinactive(ap) 318 struct vop_inactive_args /* { 319 struct vnode *a_vp; 320 struct thread *a_td; 321 } */ *ap; 322 { 323 324 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 325 return (0); 326 } 327 328 /* 329 * Return true for select/poll. 330 */ 331 int 332 vop_nopoll(ap) 333 struct vop_poll_args /* { 334 struct vnode *a_vp; 335 int a_events; 336 struct ucred *a_cred; 337 struct thread *a_td; 338 } */ *ap; 339 { 340 /* 341 * Return true for read/write. If the user asked for something 342 * special, return POLLNVAL, so that clients have a way of 343 * determining reliably whether or not the extended 344 * functionality is present without hard-coding knowledge 345 * of specific filesystem implementations. 346 */ 347 if (ap->a_events & ~POLLSTANDARD) 348 return (POLLNVAL); 349 350 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 351 } 352 353 /* 354 * Implement poll for local filesystems that support it. 355 */ 356 int 357 vop_stdpoll(ap) 358 struct vop_poll_args /* { 359 struct vnode *a_vp; 360 int a_events; 361 struct ucred *a_cred; 362 struct thread *a_td; 363 } */ *ap; 364 { 365 if (ap->a_events & ~POLLSTANDARD) 366 return (vn_pollrecord(ap->a_vp, ap->a_td, ap->a_events)); 367 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 368 } 369 370 /* 371 * Stubs to use when there is no locking to be done on the underlying object. 372 * A minimal shared lock is necessary to ensure that the underlying object 373 * is not revoked while an operation is in progress. So, an active shared 374 * count is maintained in an auxillary vnode lock structure. 375 */ 376 int 377 vop_sharedlock(ap) 378 struct vop_lock_args /* { 379 struct vnode *a_vp; 380 int a_flags; 381 struct thread *a_td; 382 } */ *ap; 383 { 384 /* 385 * This code cannot be used until all the non-locking filesystems 386 * (notably NFS) are converted to properly lock and release nodes. 387 * Also, certain vnode operations change the locking state within 388 * the operation (create, mknod, remove, link, rename, mkdir, rmdir, 389 * and symlink). Ideally these operations should not change the 390 * lock state, but should be changed to let the caller of the 391 * function unlock them. Otherwise all intermediate vnode layers 392 * (such as union, umapfs, etc) must catch these functions to do 393 * the necessary locking at their layer. Note that the inactive 394 * and lookup operations also change their lock state, but this 395 * cannot be avoided, so these two operations will always need 396 * to be handled in intermediate layers. 397 */ 398 struct vnode *vp = ap->a_vp; 399 int vnflags, flags = ap->a_flags; 400 401 switch (flags & LK_TYPE_MASK) { 402 case LK_DRAIN: 403 vnflags = LK_DRAIN; 404 break; 405 case LK_EXCLUSIVE: 406 #ifdef DEBUG_VFS_LOCKS 407 /* 408 * Normally, we use shared locks here, but that confuses 409 * the locking assertions. 410 */ 411 vnflags = LK_EXCLUSIVE; 412 break; 413 #endif 414 case LK_SHARED: 415 vnflags = LK_SHARED; 416 break; 417 case LK_UPGRADE: 418 case LK_EXCLUPGRADE: 419 case LK_DOWNGRADE: 420 return (0); 421 case LK_RELEASE: 422 default: 423 panic("vop_sharedlock: bad operation %d", flags & LK_TYPE_MASK); 424 } 425 vnflags |= flags & (LK_INTERLOCK | LK_EXTFLG_MASK); 426 #ifndef DEBUG_LOCKS 427 return (lockmgr(vp->v_vnlock, vnflags, VI_MTX(vp), ap->a_td)); 428 #else 429 return (debuglockmgr(vp->v_vnlock, vnflags, VI_MTX(vp), ap->a_td, 430 "vop_sharedlock", vp->filename, vp->line)); 431 #endif 432 } 433 434 /* 435 * Stubs to use when there is no locking to be done on the underlying object. 436 * A minimal shared lock is necessary to ensure that the underlying object 437 * is not revoked while an operation is in progress. So, an active shared 438 * count is maintained in an auxillary vnode lock structure. 439 */ 440 int 441 vop_nolock(ap) 442 struct vop_lock_args /* { 443 struct vnode *a_vp; 444 int a_flags; 445 struct thread *a_td; 446 } */ *ap; 447 { 448 #ifdef notyet 449 /* 450 * This code cannot be used until all the non-locking filesystems 451 * (notably NFS) are converted to properly lock and release nodes. 452 * Also, certain vnode operations change the locking state within 453 * the operation (create, mknod, remove, link, rename, mkdir, rmdir, 454 * and symlink). Ideally these operations should not change the 455 * lock state, but should be changed to let the caller of the 456 * function unlock them. Otherwise all intermediate vnode layers 457 * (such as union, umapfs, etc) must catch these functions to do 458 * the necessary locking at their layer. Note that the inactive 459 * and lookup operations also change their lock state, but this 460 * cannot be avoided, so these two operations will always need 461 * to be handled in intermediate layers. 462 */ 463 struct vnode *vp = ap->a_vp; 464 int vnflags, flags = ap->a_flags; 465 466 switch (flags & LK_TYPE_MASK) { 467 case LK_DRAIN: 468 vnflags = LK_DRAIN; 469 break; 470 case LK_EXCLUSIVE: 471 case LK_SHARED: 472 vnflags = LK_SHARED; 473 break; 474 case LK_UPGRADE: 475 case LK_EXCLUPGRADE: 476 case LK_DOWNGRADE: 477 return (0); 478 case LK_RELEASE: 479 default: 480 panic("vop_nolock: bad operation %d", flags & LK_TYPE_MASK); 481 } 482 vnflags |= flags & (LK_INTERLOCK | LK_EXTFLG_MASK); 483 return(lockmgr(vp->v_vnlock, vnflags, VI_MTX(vp), ap->a_td)); 484 #else /* for now */ 485 /* 486 * Since we are not using the lock manager, we must clear 487 * the interlock here. 488 */ 489 if (ap->a_flags & LK_INTERLOCK) 490 VI_UNLOCK(ap->a_vp); 491 return (0); 492 #endif 493 } 494 495 /* 496 * Do the inverse of vop_nolock, handling the interlock in a compatible way. 497 */ 498 int 499 vop_nounlock(ap) 500 struct vop_unlock_args /* { 501 struct vnode *a_vp; 502 int a_flags; 503 struct thread *a_td; 504 } */ *ap; 505 { 506 507 /* 508 * Since we are not using the lock manager, we must clear 509 * the interlock here. 510 */ 511 if (ap->a_flags & LK_INTERLOCK) 512 VI_UNLOCK(ap->a_vp); 513 return (0); 514 } 515 516 /* 517 * Return whether or not the node is in use. 518 */ 519 int 520 vop_noislocked(ap) 521 struct vop_islocked_args /* { 522 struct vnode *a_vp; 523 struct thread *a_td; 524 } */ *ap; 525 { 526 527 return (0); 528 } 529 530 /* 531 * Return our mount point, as we will take charge of the writes. 532 */ 533 int 534 vop_stdgetwritemount(ap) 535 struct vop_getwritemount_args /* { 536 struct vnode *a_vp; 537 struct mount **a_mpp; 538 } */ *ap; 539 { 540 541 *(ap->a_mpp) = ap->a_vp->v_mount; 542 return (0); 543 } 544 545 /* Create the VM system backing object for this vnode */ 546 int 547 vop_stdcreatevobject(ap) 548 struct vop_createvobject_args /* { 549 struct vnode *vp; 550 struct ucred *cred; 551 struct thread *td; 552 } */ *ap; 553 { 554 struct vnode *vp = ap->a_vp; 555 struct ucred *cred = ap->a_cred; 556 struct thread *td = ap->a_td; 557 struct vattr vat; 558 vm_object_t object; 559 int error = 0; 560 561 GIANT_REQUIRED; 562 563 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE) 564 return (0); 565 566 retry: 567 if ((object = vp->v_object) == NULL) { 568 if (vp->v_type == VREG || vp->v_type == VDIR) { 569 if ((error = VOP_GETATTR(vp, &vat, cred, td)) != 0) 570 goto retn; 571 object = vnode_pager_alloc(vp, vat.va_size, 0, 0); 572 } else if (devsw(vp->v_rdev) != NULL) { 573 /* 574 * This simply allocates the biggest object possible 575 * for a disk vnode. This should be fixed, but doesn't 576 * cause any problems (yet). 577 */ 578 object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0); 579 } else { 580 goto retn; 581 } 582 /* 583 * Dereference the reference we just created. This assumes 584 * that the object is associated with the vp. 585 */ 586 VM_OBJECT_LOCK(object); 587 object->ref_count--; 588 VM_OBJECT_UNLOCK(object); 589 vrele(vp); 590 } else { 591 VM_OBJECT_LOCK(object); 592 if (object->flags & OBJ_DEAD) { 593 VOP_UNLOCK(vp, 0, td); 594 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, 595 "vodead", 0); 596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 597 goto retry; 598 } 599 VM_OBJECT_UNLOCK(object); 600 } 601 602 KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object")); 603 vp->v_vflag |= VV_OBJBUF; 604 605 retn: 606 return (error); 607 } 608 609 /* Destroy the VM system object associated with this vnode */ 610 int 611 vop_stddestroyvobject(ap) 612 struct vop_destroyvobject_args /* { 613 struct vnode *vp; 614 } */ *ap; 615 { 616 struct vnode *vp = ap->a_vp; 617 vm_object_t obj = vp->v_object; 618 619 GIANT_REQUIRED; 620 621 if (obj == NULL) 622 return (0); 623 VM_OBJECT_LOCK(obj); 624 if (obj->ref_count == 0) { 625 /* 626 * vclean() may be called twice. The first time 627 * removes the primary reference to the object, 628 * the second time goes one further and is a 629 * special-case to terminate the object. 630 * 631 * don't double-terminate the object 632 */ 633 if ((obj->flags & OBJ_DEAD) == 0) 634 vm_object_terminate(obj); 635 else 636 VM_OBJECT_UNLOCK(obj); 637 } else { 638 /* 639 * Woe to the process that tries to page now :-). 640 */ 641 vm_pager_deallocate(obj); 642 VM_OBJECT_UNLOCK(obj); 643 } 644 return (0); 645 } 646 647 /* 648 * Return the underlying VM object. This routine may be called with or 649 * without the vnode interlock held. If called without, the returned 650 * object is not guarenteed to be valid. The syncer typically gets the 651 * object without holding the interlock in order to quickly test whether 652 * it might be dirty before going heavy-weight. vm_object's use zalloc 653 * and thus stable-storage, so this is safe. 654 */ 655 int 656 vop_stdgetvobject(ap) 657 struct vop_getvobject_args /* { 658 struct vnode *vp; 659 struct vm_object **objpp; 660 } */ *ap; 661 { 662 struct vnode *vp = ap->a_vp; 663 struct vm_object **objpp = ap->a_objpp; 664 665 if (objpp) 666 *objpp = vp->v_object; 667 return (vp->v_object ? 0 : EINVAL); 668 } 669 670 /* XXX Needs good comment and VOP_BMAP(9) manpage */ 671 int 672 vop_stdbmap(ap) 673 struct vop_bmap_args /* { 674 struct vnode *a_vp; 675 daddr_t a_bn; 676 struct vnode **a_vpp; 677 daddr_t *a_bnp; 678 int *a_runp; 679 int *a_runb; 680 } */ *ap; 681 { 682 683 if (ap->a_vpp != NULL) 684 *ap->a_vpp = ap->a_vp; 685 if (ap->a_bnp != NULL) 686 *ap->a_bnp = ap->a_bn * btodb(ap->a_vp->v_mount->mnt_stat.f_iosize); 687 if (ap->a_runp != NULL) 688 *ap->a_runp = 0; 689 if (ap->a_runb != NULL) 690 *ap->a_runb = 0; 691 return (0); 692 } 693 694 int 695 vop_stdfsync(ap) 696 struct vop_fsync_args /* { 697 struct vnode *a_vp; 698 struct ucred *a_cred; 699 int a_waitfor; 700 struct thread *a_td; 701 } */ *ap; 702 { 703 struct vnode *vp = ap->a_vp; 704 struct buf *bp; 705 struct buf *nbp; 706 int s, error = 0; 707 int maxretry = 100; /* large, arbitrarily chosen */ 708 709 VI_LOCK(vp); 710 loop1: 711 /* 712 * MARK/SCAN initialization to avoid infinite loops. 713 */ 714 s = splbio(); 715 TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 716 bp->b_vflags &= ~BV_SCANNED; 717 bp->b_error = 0; 718 } 719 splx(s); 720 721 /* 722 * Flush all dirty buffers associated with a block device. 723 */ 724 loop2: 725 s = splbio(); 726 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp != NULL; bp = nbp) { 727 nbp = TAILQ_NEXT(bp, b_vnbufs); 728 if ((bp->b_vflags & BV_SCANNED) != 0) 729 continue; 730 bp->b_vflags |= BV_SCANNED; 731 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 732 continue; 733 VI_UNLOCK(vp); 734 if ((bp->b_flags & B_DELWRI) == 0) 735 panic("fsync: not dirty"); 736 if ((vp->v_vflag & VV_OBJBUF) && (bp->b_flags & B_CLUSTEROK)) { 737 vfs_bio_awrite(bp); 738 splx(s); 739 } else { 740 bremfree(bp); 741 splx(s); 742 bawrite(bp); 743 } 744 VI_LOCK(vp); 745 goto loop2; 746 } 747 748 /* 749 * If synchronous the caller expects us to completely resolve all 750 * dirty buffers in the system. Wait for in-progress I/O to 751 * complete (which could include background bitmap writes), then 752 * retry if dirty blocks still exist. 753 */ 754 if (ap->a_waitfor == MNT_WAIT) { 755 while (vp->v_numoutput) { 756 vp->v_iflag |= VI_BWAIT; 757 msleep((caddr_t)&vp->v_numoutput, VI_MTX(vp), 758 PRIBIO + 1, "fsync", 0); 759 } 760 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 761 /* 762 * If we are unable to write any of these buffers 763 * then we fail now rather than trying endlessly 764 * to write them out. 765 */ 766 TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) 767 if ((error = bp->b_error) == 0) 768 continue; 769 if (error == 0 && --maxretry >= 0) { 770 splx(s); 771 goto loop1; 772 } 773 vprint("fsync: giving up on dirty", vp); 774 error = EAGAIN; 775 } 776 } 777 VI_UNLOCK(vp); 778 splx(s); 779 780 return (error); 781 } 782 783 /* XXX Needs good comment and more info in the manpage (VOP_GETPAGES(9)). */ 784 int 785 vop_stdgetpages(ap) 786 struct vop_getpages_args /* { 787 struct vnode *a_vp; 788 vm_page_t *a_m; 789 int a_count; 790 int a_reqpage; 791 vm_ooffset_t a_offset; 792 } */ *ap; 793 { 794 795 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 796 ap->a_count, ap->a_reqpage); 797 } 798 799 /* XXX Needs good comment and more info in the manpage (VOP_PUTPAGES(9)). */ 800 int 801 vop_stdputpages(ap) 802 struct vop_putpages_args /* { 803 struct vnode *a_vp; 804 vm_page_t *a_m; 805 int a_count; 806 int a_sync; 807 int *a_rtvals; 808 vm_ooffset_t a_offset; 809 } */ *ap; 810 { 811 812 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count, 813 ap->a_sync, ap->a_rtvals); 814 } 815 816 /* 817 * vfs default ops 818 * used to fill the vfs function table to get reasonable default return values. 819 */ 820 int 821 vfs_stdroot (mp, vpp) 822 struct mount *mp; 823 struct vnode **vpp; 824 { 825 return (EOPNOTSUPP); 826 } 827 828 int 829 vfs_stdstatfs (mp, sbp, td) 830 struct mount *mp; 831 struct statfs *sbp; 832 struct thread *td; 833 { 834 return (EOPNOTSUPP); 835 } 836 837 int 838 vfs_stdvptofh (vp, fhp) 839 struct vnode *vp; 840 struct fid *fhp; 841 { 842 return (EOPNOTSUPP); 843 } 844 845 int 846 vfs_stdstart (mp, flags, td) 847 struct mount *mp; 848 int flags; 849 struct thread *td; 850 { 851 return (0); 852 } 853 854 int 855 vfs_stdquotactl (mp, cmds, uid, arg, td) 856 struct mount *mp; 857 int cmds; 858 uid_t uid; 859 caddr_t arg; 860 struct thread *td; 861 { 862 return (EOPNOTSUPP); 863 } 864 865 int 866 vfs_stdsync(mp, waitfor, cred, td) 867 struct mount *mp; 868 int waitfor; 869 struct ucred *cred; 870 struct thread *td; 871 { 872 struct vnode *vp, *nvp; 873 int error, lockreq, allerror = 0; 874 875 lockreq = LK_EXCLUSIVE | LK_INTERLOCK; 876 if (waitfor != MNT_WAIT) 877 lockreq |= LK_NOWAIT; 878 /* 879 * Force stale buffer cache information to be flushed. 880 */ 881 mtx_lock(&mntvnode_mtx); 882 loop: 883 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 884 /* 885 * If the vnode that we are about to sync is no longer 886 * associated with this mount point, start over. 887 */ 888 if (vp->v_mount != mp) 889 goto loop; 890 891 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 892 893 VI_LOCK(vp); 894 if (TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 895 VI_UNLOCK(vp); 896 continue; 897 } 898 mtx_unlock(&mntvnode_mtx); 899 900 if ((error = vget(vp, lockreq, td)) != 0) { 901 if (error == ENOENT) 902 goto loop; 903 continue; 904 } 905 error = VOP_FSYNC(vp, cred, waitfor, td); 906 if (error) 907 allerror = error; 908 909 mtx_lock(&mntvnode_mtx); 910 if (nvp != TAILQ_NEXT(vp, v_nmntvnodes)) { 911 vput(vp); 912 goto loop; 913 } 914 vput(vp); 915 } 916 mtx_unlock(&mntvnode_mtx); 917 return (allerror); 918 } 919 920 int 921 vfs_stdnosync (mp, waitfor, cred, td) 922 struct mount *mp; 923 int waitfor; 924 struct ucred *cred; 925 struct thread *td; 926 { 927 return (0); 928 } 929 930 int 931 vfs_stdvget (mp, ino, flags, vpp) 932 struct mount *mp; 933 ino_t ino; 934 int flags; 935 struct vnode **vpp; 936 { 937 return (EOPNOTSUPP); 938 } 939 940 int 941 vfs_stdfhtovp (mp, fhp, vpp) 942 struct mount *mp; 943 struct fid *fhp; 944 struct vnode **vpp; 945 { 946 return (EOPNOTSUPP); 947 } 948 949 int 950 vfs_stdinit (vfsp) 951 struct vfsconf *vfsp; 952 { 953 return (0); 954 } 955 956 int 957 vfs_stduninit (vfsp) 958 struct vfsconf *vfsp; 959 { 960 return(0); 961 } 962 963 int 964 vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace, attrname, td) 965 struct mount *mp; 966 int cmd; 967 struct vnode *filename_vp; 968 int attrnamespace; 969 const char *attrname; 970 struct thread *td; 971 { 972 if (filename_vp != NULL) 973 VOP_UNLOCK(filename_vp, 0, td); 974 return(EOPNOTSUPP); 975 } 976 977 /* end of vfs default ops */ 978