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