vfs_export.c revision 33936
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 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 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 39 * $Id: vfs_subr.c,v 1.134 1998/02/23 06:59:52 dyson Exp $ 40 */ 41 42/* 43 * External virtual filesystem routines 44 */ 45#include "opt_ddb.h" 46#include "opt_devfs.h" 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/kernel.h> 51#include <sys/proc.h> 52#include <sys/malloc.h> 53#include <sys/mount.h> 54#include <sys/vnode.h> 55#include <sys/stat.h> 56#include <sys/buf.h> 57#include <sys/poll.h> 58#include <sys/domain.h> 59#include <sys/dirent.h> 60#include <sys/vmmeter.h> 61 62#include <machine/limits.h> 63 64#include <vm/vm.h> 65#include <vm/vm_object.h> 66#include <vm/vm_extern.h> 67#include <vm/pmap.h> 68#include <vm/vm_map.h> 69#include <vm/vm_pager.h> 70#include <vm/vnode_pager.h> 71#include <vm/vm_zone.h> 72#include <sys/sysctl.h> 73 74#include <miscfs/specfs/specdev.h> 75 76static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); 77 78static void insmntque __P((struct vnode *vp, struct mount *mp)); 79#ifdef DDB 80static void printlockedvnodes __P((void)); 81#endif 82static void vclean __P((struct vnode *vp, int flags, struct proc *p)); 83static void vfree __P((struct vnode *)); 84static void vgonel __P((struct vnode *vp, struct proc *p)); 85static unsigned long numvnodes; 86SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, ""); 87 88enum vtype iftovt_tab[16] = { 89 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 90 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 91}; 92int vttoif_tab[9] = { 93 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 94 S_IFSOCK, S_IFIFO, S_IFMT, 95}; 96 97/* 98 * Insq/Remq for the vnode usage lists. 99 */ 100#define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 101#define bufremvn(bp) { \ 102 LIST_REMOVE(bp, b_vnbufs); \ 103 (bp)->b_vnbufs.le_next = NOLIST; \ 104} 105 106static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */ 107struct tobefreelist vnode_tobefree_list; /* vnode free list */ 108 109static u_long wantfreevnodes = 25; 110SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 111static u_long freevnodes = 0; 112SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, ""); 113 114int vfs_ioopt = 1; 115SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, ""); 116 117struct mntlist mountlist; /* mounted filesystem list */ 118struct simplelock mountlist_slock; 119static struct simplelock mntid_slock; 120struct simplelock mntvnode_slock; 121static struct simplelock vnode_free_list_slock; 122static struct simplelock spechash_slock; 123struct nfs_public nfs_pub; /* publicly exported FS */ 124static vm_zone_t vnode_zone; 125 126int desiredvnodes; 127SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, &desiredvnodes, 0, ""); 128 129static void vfs_free_addrlist __P((struct netexport *nep)); 130static int vfs_free_netcred __P((struct radix_node *rn, void *w)); 131static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep, 132 struct export_args *argp)); 133 134/* 135 * Initialize the vnode management data structures. 136 */ 137void 138vntblinit() 139{ 140 141 desiredvnodes = maxproc + cnt.v_page_count / 4; 142 simple_lock_init(&mntvnode_slock); 143 simple_lock_init(&mntid_slock); 144 simple_lock_init(&spechash_slock); 145 TAILQ_INIT(&vnode_free_list); 146 TAILQ_INIT(&vnode_tobefree_list); 147 simple_lock_init(&vnode_free_list_slock); 148 CIRCLEQ_INIT(&mountlist); 149 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5); 150} 151 152/* 153 * Mark a mount point as busy. Used to synchronize access and to delay 154 * unmounting. Interlock is not released on failure. 155 */ 156int 157vfs_busy(mp, flags, interlkp, p) 158 struct mount *mp; 159 int flags; 160 struct simplelock *interlkp; 161 struct proc *p; 162{ 163 int lkflags; 164 165 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 166 if (flags & LK_NOWAIT) 167 return (ENOENT); 168 mp->mnt_kern_flag |= MNTK_MWAIT; 169 if (interlkp) { 170 simple_unlock(interlkp); 171 } 172 /* 173 * Since all busy locks are shared except the exclusive 174 * lock granted when unmounting, the only place that a 175 * wakeup needs to be done is at the release of the 176 * exclusive lock at the end of dounmount. 177 */ 178 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0); 179 if (interlkp) { 180 simple_lock(interlkp); 181 } 182 return (ENOENT); 183 } 184 lkflags = LK_SHARED; 185 if (interlkp) 186 lkflags |= LK_INTERLOCK; 187 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p)) 188 panic("vfs_busy: unexpected lock failure"); 189 return (0); 190} 191 192/* 193 * Free a busy filesystem. 194 */ 195void 196vfs_unbusy(mp, p) 197 struct mount *mp; 198 struct proc *p; 199{ 200 201 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p); 202} 203 204/* 205 * Lookup a filesystem type, and if found allocate and initialize 206 * a mount structure for it. 207 * 208 * Devname is usually updated by mount(8) after booting. 209 */ 210int 211vfs_rootmountalloc(fstypename, devname, mpp) 212 char *fstypename; 213 char *devname; 214 struct mount **mpp; 215{ 216 struct proc *p = curproc; /* XXX */ 217 struct vfsconf *vfsp; 218 struct mount *mp; 219 220 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 221 if (!strcmp(vfsp->vfc_name, fstypename)) 222 break; 223 if (vfsp == NULL) 224 return (ENODEV); 225 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 226 bzero((char *)mp, (u_long)sizeof(struct mount)); 227 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 228 (void)vfs_busy(mp, LK_NOWAIT, 0, p); 229 LIST_INIT(&mp->mnt_vnodelist); 230 mp->mnt_vfc = vfsp; 231 mp->mnt_op = vfsp->vfc_vfsops; 232 mp->mnt_flag = MNT_RDONLY; 233 mp->mnt_vnodecovered = NULLVP; 234 vfsp->vfc_refcount++; 235 mp->mnt_stat.f_type = vfsp->vfc_typenum; 236 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 237 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 238 mp->mnt_stat.f_mntonname[0] = '/'; 239 mp->mnt_stat.f_mntonname[1] = 0; 240 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0); 241 *mpp = mp; 242 return (0); 243} 244 245/* 246 * Find an appropriate filesystem to use for the root. If a filesystem 247 * has not been preselected, walk through the list of known filesystems 248 * trying those that have mountroot routines, and try them until one 249 * works or we have tried them all. 250 */ 251#ifdef notdef /* XXX JH */ 252int 253lite2_vfs_mountroot() 254{ 255 struct vfsconf *vfsp; 256 extern int (*lite2_mountroot) __P((void)); 257 int error; 258 259 if (lite2_mountroot != NULL) 260 return ((*lite2_mountroot)()); 261 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 262 if (vfsp->vfc_mountroot == NULL) 263 continue; 264 if ((error = (*vfsp->vfc_mountroot)()) == 0) 265 return (0); 266 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error); 267 } 268 return (ENODEV); 269} 270#endif 271 272/* 273 * Lookup a mount point by filesystem identifier. 274 */ 275struct mount * 276vfs_getvfs(fsid) 277 fsid_t *fsid; 278{ 279 register struct mount *mp; 280 281 simple_lock(&mountlist_slock); 282 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; 283 mp = mp->mnt_list.cqe_next) { 284 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 285 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 286 simple_unlock(&mountlist_slock); 287 return (mp); 288 } 289 } 290 simple_unlock(&mountlist_slock); 291 return ((struct mount *) 0); 292} 293 294/* 295 * Get a new unique fsid 296 */ 297void 298vfs_getnewfsid(mp) 299 struct mount *mp; 300{ 301 static u_short xxxfs_mntid; 302 303 fsid_t tfsid; 304 int mtype; 305 306 simple_lock(&mntid_slock); 307 mtype = mp->mnt_vfc->vfc_typenum; 308 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0); 309 mp->mnt_stat.f_fsid.val[1] = mtype; 310 if (xxxfs_mntid == 0) 311 ++xxxfs_mntid; 312 tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid); 313 tfsid.val[1] = mtype; 314 if (mountlist.cqh_first != (void *)&mountlist) { 315 while (vfs_getvfs(&tfsid)) { 316 tfsid.val[0]++; 317 xxxfs_mntid++; 318 } 319 } 320 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 321 simple_unlock(&mntid_slock); 322} 323 324/* 325 * Set vnode attributes to VNOVAL 326 */ 327void 328vattr_null(vap) 329 register struct vattr *vap; 330{ 331 332 vap->va_type = VNON; 333 vap->va_size = VNOVAL; 334 vap->va_bytes = VNOVAL; 335 vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid = 336 vap->va_fsid = vap->va_fileid = 337 vap->va_blocksize = vap->va_rdev = 338 vap->va_atime.tv_sec = vap->va_atime.tv_nsec = 339 vap->va_mtime.tv_sec = vap->va_mtime.tv_nsec = 340 vap->va_ctime.tv_sec = vap->va_ctime.tv_nsec = 341 vap->va_flags = vap->va_gen = VNOVAL; 342 vap->va_vaflags = 0; 343} 344 345/* 346 * Routines having to do with the management of the vnode table. 347 */ 348extern vop_t **dead_vnodeop_p; 349 350/* 351 * Return the next vnode from the free list. 352 */ 353int 354getnewvnode(tag, mp, vops, vpp) 355 enum vtagtype tag; 356 struct mount *mp; 357 vop_t **vops; 358 struct vnode **vpp; 359{ 360 int s; 361 struct proc *p = curproc; /* XXX */ 362 struct vnode *vp, *tvp, *nvp; 363 vm_object_t object; 364 TAILQ_HEAD(freelst, vnode) vnode_tmp_list; 365 366 /* 367 * We take the least recently used vnode from the freelist 368 * if we can get it and it has no cached pages, and no 369 * namecache entries are relative to it. 370 * Otherwise we allocate a new vnode 371 */ 372 373 s = splbio(); 374 simple_lock(&vnode_free_list_slock); 375 TAILQ_INIT(&vnode_tmp_list); 376 377 for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) { 378 nvp = TAILQ_NEXT(vp, v_freelist); 379 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist); 380 if (vp->v_flag & VAGE) { 381 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 382 } else { 383 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 384 } 385 vp->v_flag &= ~(VTBFREE|VAGE); 386 vp->v_flag |= VFREE; 387 if (vp->v_usecount) 388 panic("tobe free vnode isn't"); 389 freevnodes++; 390 } 391 392 if (wantfreevnodes && freevnodes < wantfreevnodes) { 393 vp = NULL; 394 } else if (!wantfreevnodes && freevnodes <= desiredvnodes) { 395 /* 396 * XXX: this is only here to be backwards compatible 397 */ 398 vp = NULL; 399 } else { 400 for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) { 401 402 nvp = TAILQ_NEXT(vp, v_freelist); 403 404 if (!simple_lock_try(&vp->v_interlock)) 405 continue; 406 if (vp->v_usecount) 407 panic("free vnode isn't"); 408 409 object = vp->v_object; 410 if (object && (object->resident_page_count || object->ref_count)) { 411 printf("object inconsistant state: RPC: %d, RC: %d\n", 412 object->resident_page_count, object->ref_count); 413 /* Don't recycle if it's caching some pages */ 414 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 415 TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist); 416 continue; 417 } else if (LIST_FIRST(&vp->v_cache_src)) { 418 /* Don't recycle if active in the namecache */ 419 simple_unlock(&vp->v_interlock); 420 continue; 421 } else { 422 break; 423 } 424 } 425 } 426 427 for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) { 428 nvp = TAILQ_NEXT(tvp, v_freelist); 429 TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist); 430 TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist); 431 simple_unlock(&tvp->v_interlock); 432 } 433 434 if (vp) { 435 vp->v_flag |= VDOOMED; 436 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 437 freevnodes--; 438 simple_unlock(&vnode_free_list_slock); 439 cache_purge(vp); 440 vp->v_lease = NULL; 441 if (vp->v_type != VBAD) { 442 vgonel(vp, p); 443 } else { 444 simple_unlock(&vp->v_interlock); 445 } 446 447#ifdef DIAGNOSTIC 448 { 449 int s; 450 451 if (vp->v_data) 452 panic("cleaned vnode isn't"); 453 s = splbio(); 454 if (vp->v_numoutput) 455 panic("Clean vnode has pending I/O's"); 456 splx(s); 457 } 458#endif 459 vp->v_flag = 0; 460 vp->v_lastr = 0; 461 vp->v_lastw = 0; 462 vp->v_lasta = 0; 463 vp->v_cstart = 0; 464 vp->v_clen = 0; 465 vp->v_socket = 0; 466 vp->v_writecount = 0; /* XXX */ 467 vp->v_maxio = 0; 468 } else { 469 simple_unlock(&vnode_free_list_slock); 470 vp = (struct vnode *) zalloc(vnode_zone); 471 bzero((char *) vp, sizeof *vp); 472 simple_lock_init(&vp->v_interlock); 473 vp->v_dd = vp; 474 cache_purge(vp); 475 LIST_INIT(&vp->v_cache_src); 476 TAILQ_INIT(&vp->v_cache_dst); 477 numvnodes++; 478 } 479 480 vp->v_type = VNON; 481 vp->v_tag = tag; 482 vp->v_op = vops; 483 insmntque(vp, mp); 484 *vpp = vp; 485 vp->v_usecount = 1; 486 vp->v_data = 0; 487 splx(s); 488 489 vfs_object_create(vp, p, p->p_ucred, TRUE); 490 return (0); 491} 492 493/* 494 * Move a vnode from one mount queue to another. 495 */ 496static void 497insmntque(vp, mp) 498 register struct vnode *vp; 499 register struct mount *mp; 500{ 501 502 simple_lock(&mntvnode_slock); 503 /* 504 * Delete from old mount point vnode list, if on one. 505 */ 506 if (vp->v_mount != NULL) 507 LIST_REMOVE(vp, v_mntvnodes); 508 /* 509 * Insert into list of vnodes for the new mount point, if available. 510 */ 511 if ((vp->v_mount = mp) == NULL) { 512 simple_unlock(&mntvnode_slock); 513 return; 514 } 515 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes); 516 simple_unlock(&mntvnode_slock); 517} 518 519/* 520 * Update outstanding I/O count and do wakeup if requested. 521 */ 522void 523vwakeup(bp) 524 register struct buf *bp; 525{ 526 register struct vnode *vp; 527 528 bp->b_flags &= ~B_WRITEINPROG; 529 if ((vp = bp->b_vp)) { 530 vp->v_numoutput--; 531 if (vp->v_numoutput < 0) 532 panic("vwakeup: neg numoutput"); 533 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) { 534 vp->v_flag &= ~VBWAIT; 535 wakeup((caddr_t) &vp->v_numoutput); 536 } 537 } 538} 539 540/* 541 * Flush out and invalidate all buffers associated with a vnode. 542 * Called with the underlying object locked. 543 */ 544int 545vinvalbuf(vp, flags, cred, p, slpflag, slptimeo) 546 register struct vnode *vp; 547 int flags; 548 struct ucred *cred; 549 struct proc *p; 550 int slpflag, slptimeo; 551{ 552 register struct buf *bp; 553 struct buf *nbp, *blist; 554 int s, error; 555 vm_object_t object; 556 557 if (flags & V_SAVE) { 558 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p))) 559 return (error); 560 if (vp->v_dirtyblkhd.lh_first != NULL) 561 panic("vinvalbuf: dirty bufs"); 562 } 563 564 s = splbio(); 565 for (;;) { 566 if ((blist = vp->v_cleanblkhd.lh_first) && (flags & V_SAVEMETA)) 567 while (blist && blist->b_lblkno < 0) 568 blist = blist->b_vnbufs.le_next; 569 if (!blist && (blist = vp->v_dirtyblkhd.lh_first) && 570 (flags & V_SAVEMETA)) 571 while (blist && blist->b_lblkno < 0) 572 blist = blist->b_vnbufs.le_next; 573 if (!blist) 574 break; 575 576 for (bp = blist; bp; bp = nbp) { 577 nbp = bp->b_vnbufs.le_next; 578 if ((flags & V_SAVEMETA) && bp->b_lblkno < 0) 579 continue; 580 if (bp->b_flags & B_BUSY) { 581 bp->b_flags |= B_WANTED; 582 error = tsleep((caddr_t) bp, 583 slpflag | (PRIBIO + 1), "vinvalbuf", 584 slptimeo); 585 if (error) { 586 splx(s); 587 return (error); 588 } 589 break; 590 } 591 bremfree(bp); 592 bp->b_flags |= B_BUSY; 593 /* 594 * XXX Since there are no node locks for NFS, I 595 * believe there is a slight chance that a delayed 596 * write will occur while sleeping just above, so 597 * check for it. 598 */ 599 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 600 if (bp->b_vp == vp) { 601 if (bp->b_flags & B_CLUSTEROK) { 602 vfs_bio_awrite(bp); 603 } else { 604 bp->b_flags |= B_ASYNC; 605 VOP_BWRITE(bp); 606 } 607 } else { 608 (void) VOP_BWRITE(bp); 609 } 610 break; 611 } 612 bp->b_flags |= (B_INVAL|B_NOCACHE|B_RELBUF); 613 brelse(bp); 614 } 615 } 616 617 while (vp->v_numoutput > 0) { 618 vp->v_flag |= VBWAIT; 619 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0); 620 } 621 622 splx(s); 623 624 /* 625 * Destroy the copy in the VM cache, too. 626 */ 627 simple_lock(&vp->v_interlock); 628 object = vp->v_object; 629 if (object != NULL) { 630 if (flags & V_SAVEMETA) 631 vm_object_page_remove(object, 0, object->size, 632 (flags & V_SAVE) ? TRUE : FALSE); 633 else 634 vm_object_page_remove(object, 0, 0, 635 (flags & V_SAVE) ? TRUE : FALSE); 636 } 637 simple_unlock(&vp->v_interlock); 638 639 if (!(flags & V_SAVEMETA) && 640 (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first)) 641 panic("vinvalbuf: flush failed"); 642 return (0); 643} 644 645/* 646 * Associate a buffer with a vnode. 647 */ 648void 649bgetvp(vp, bp) 650 register struct vnode *vp; 651 register struct buf *bp; 652{ 653 int s; 654 655#if defined(DIAGNOSTIC) 656 if (bp->b_vp) 657 panic("bgetvp: not free"); 658#endif 659 vhold(vp); 660 bp->b_vp = vp; 661 if (vp->v_type == VBLK || vp->v_type == VCHR) 662 bp->b_dev = vp->v_rdev; 663 else 664 bp->b_dev = NODEV; 665 /* 666 * Insert onto list for new vnode. 667 */ 668 s = splbio(); 669 bufinsvn(bp, &vp->v_cleanblkhd); 670 splx(s); 671} 672 673/* 674 * Disassociate a buffer from a vnode. 675 */ 676void 677brelvp(bp) 678 register struct buf *bp; 679{ 680 struct vnode *vp; 681 int s; 682 683#if defined(DIAGNOSTIC) 684 if (bp->b_vp == (struct vnode *) 0) 685 panic("brelvp: NULL"); 686#endif 687 688 /* 689 * Delete from old vnode list, if on one. 690 */ 691 s = splbio(); 692 if (bp->b_vnbufs.le_next != NOLIST) 693 bufremvn(bp); 694 splx(s); 695 696 vp = bp->b_vp; 697 bp->b_vp = (struct vnode *) 0; 698 vdrop(vp); 699} 700 701/* 702 * Associate a p-buffer with a vnode. 703 */ 704void 705pbgetvp(vp, bp) 706 register struct vnode *vp; 707 register struct buf *bp; 708{ 709#if defined(DIAGNOSTIC) 710 if (bp->b_vp) 711 panic("pbgetvp: not free"); 712#endif 713 bp->b_vp = vp; 714 if (vp->v_type == VBLK || vp->v_type == VCHR) 715 bp->b_dev = vp->v_rdev; 716 else 717 bp->b_dev = NODEV; 718} 719 720/* 721 * Disassociate a p-buffer from a vnode. 722 */ 723void 724pbrelvp(bp) 725 register struct buf *bp; 726{ 727 728#if defined(DIAGNOSTIC) 729 if (bp->b_vp == (struct vnode *) 0) 730 panic("pbrelvp: NULL"); 731#endif 732 733 bp->b_vp = (struct vnode *) 0; 734} 735 736/* 737 * Reassign a buffer from one vnode to another. 738 * Used to assign file specific control information 739 * (indirect blocks) to the vnode to which they belong. 740 */ 741void 742reassignbuf(bp, newvp) 743 register struct buf *bp; 744 register struct vnode *newvp; 745{ 746 int s; 747 748 if (newvp == NULL) { 749 printf("reassignbuf: NULL"); 750 return; 751 } 752 753 s = splbio(); 754 /* 755 * Delete from old vnode list, if on one. 756 */ 757 if (bp->b_vnbufs.le_next != NOLIST) { 758 bufremvn(bp); 759 vdrop(bp->b_vp); 760 } 761 /* 762 * If dirty, put on list of dirty buffers; otherwise insert onto list 763 * of clean buffers. 764 */ 765 if (bp->b_flags & B_DELWRI) { 766 struct buf *tbp; 767 768 tbp = newvp->v_dirtyblkhd.lh_first; 769 if (!tbp || (tbp->b_lblkno > bp->b_lblkno)) { 770 bufinsvn(bp, &newvp->v_dirtyblkhd); 771 } else { 772 while (tbp->b_vnbufs.le_next && 773 (tbp->b_vnbufs.le_next->b_lblkno < bp->b_lblkno)) { 774 tbp = tbp->b_vnbufs.le_next; 775 } 776 LIST_INSERT_AFTER(tbp, bp, b_vnbufs); 777 } 778 } else { 779 bufinsvn(bp, &newvp->v_cleanblkhd); 780 } 781 bp->b_vp = newvp; 782 vhold(bp->b_vp); 783 splx(s); 784} 785 786#ifndef DEVFS_ROOT 787/* 788 * Create a vnode for a block device. 789 * Used for mounting the root file system. 790 */ 791int 792bdevvp(dev, vpp) 793 dev_t dev; 794 struct vnode **vpp; 795{ 796 register struct vnode *vp; 797 struct vnode *nvp; 798 int error; 799 800 if (dev == NODEV) 801 return (0); 802 error = getnewvnode(VT_NON, (struct mount *) 0, spec_vnodeop_p, &nvp); 803 if (error) { 804 *vpp = 0; 805 return (error); 806 } 807 vp = nvp; 808 vp->v_type = VBLK; 809 if ((nvp = checkalias(vp, dev, (struct mount *) 0))) { 810 vput(vp); 811 vp = nvp; 812 } 813 *vpp = vp; 814 return (0); 815} 816#endif /* !DEVFS_ROOT */ 817 818/* 819 * Check to see if the new vnode represents a special device 820 * for which we already have a vnode (either because of 821 * bdevvp() or because of a different vnode representing 822 * the same block device). If such an alias exists, deallocate 823 * the existing contents and return the aliased vnode. The 824 * caller is responsible for filling it with its new contents. 825 */ 826struct vnode * 827checkalias(nvp, nvp_rdev, mp) 828 register struct vnode *nvp; 829 dev_t nvp_rdev; 830 struct mount *mp; 831{ 832 struct proc *p = curproc; /* XXX */ 833 struct vnode *vp; 834 struct vnode **vpp; 835 836 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 837 return (NULLVP); 838 839 vpp = &speclisth[SPECHASH(nvp_rdev)]; 840loop: 841 simple_lock(&spechash_slock); 842 for (vp = *vpp; vp; vp = vp->v_specnext) { 843 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) 844 continue; 845 /* 846 * Alias, but not in use, so flush it out. 847 */ 848 simple_lock(&vp->v_interlock); 849 if (vp->v_usecount == 0) { 850 simple_unlock(&spechash_slock); 851 vgonel(vp, p); 852 goto loop; 853 } 854 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { 855 simple_unlock(&spechash_slock); 856 goto loop; 857 } 858 break; 859 } 860 if (vp == NULL || vp->v_tag != VT_NON) { 861 MALLOC(nvp->v_specinfo, struct specinfo *, 862 sizeof(struct specinfo), M_VNODE, M_WAITOK); 863 nvp->v_rdev = nvp_rdev; 864 nvp->v_hashchain = vpp; 865 nvp->v_specnext = *vpp; 866 nvp->v_specflags = 0; 867 simple_unlock(&spechash_slock); 868 *vpp = nvp; 869 if (vp != NULLVP) { 870 nvp->v_flag |= VALIASED; 871 vp->v_flag |= VALIASED; 872 vput(vp); 873 } 874 return (NULLVP); 875 } 876 simple_unlock(&spechash_slock); 877 VOP_UNLOCK(vp, 0, p); 878 simple_lock(&vp->v_interlock); 879 vclean(vp, 0, p); 880 vp->v_op = nvp->v_op; 881 vp->v_tag = nvp->v_tag; 882 nvp->v_type = VNON; 883 insmntque(vp, mp); 884 return (vp); 885} 886 887/* 888 * Grab a particular vnode from the free list, increment its 889 * reference count and lock it. The vnode lock bit is set the 890 * vnode is being eliminated in vgone. The process is awakened 891 * when the transition is completed, and an error returned to 892 * indicate that the vnode is no longer usable (possibly having 893 * been changed to a new file system type). 894 */ 895int 896vget(vp, flags, p) 897 register struct vnode *vp; 898 int flags; 899 struct proc *p; 900{ 901 int error; 902 903 /* 904 * If the vnode is in the process of being cleaned out for 905 * another use, we wait for the cleaning to finish and then 906 * return failure. Cleaning is determined by checking that 907 * the VXLOCK flag is set. 908 */ 909 if ((flags & LK_INTERLOCK) == 0) { 910 simple_lock(&vp->v_interlock); 911 } 912 if (vp->v_flag & VXLOCK) { 913 vp->v_flag |= VXWANT; 914 simple_unlock(&vp->v_interlock); 915 tsleep((caddr_t)vp, PINOD, "vget", 0); 916 return (ENOENT); 917 } 918 919 vp->v_usecount++; 920 921 if (VSHOULDBUSY(vp)) 922 vbusy(vp); 923 924 if (flags & LK_TYPE_MASK) { 925 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) { 926 /* 927 * must expand vrele here because we do not want 928 * to call VOP_INACTIVE if the reference count 929 * drops back to zero since it was never really 930 * active. We must remove it from the free list 931 * before sleeping so that multiple processes do 932 * not try to recycle it. 933 */ 934 simple_lock(&vp->v_interlock); 935 vp->v_usecount--; 936 if (VSHOULDFREE(vp)) 937 vfree(vp); 938 simple_unlock(&vp->v_interlock); 939 } 940 return (error); 941 } 942 simple_unlock(&vp->v_interlock); 943 return (0); 944} 945 946void 947vref(struct vnode *vp) 948{ 949 simple_lock(&vp->v_interlock); 950 vp->v_usecount++; 951 simple_unlock(&vp->v_interlock); 952} 953 954/* 955 * Vnode put/release. 956 * If count drops to zero, call inactive routine and return to freelist. 957 */ 958void 959vrele(vp) 960 struct vnode *vp; 961{ 962 struct proc *p = curproc; /* XXX */ 963 964#ifdef DIAGNOSTIC 965 if (vp == NULL) 966 panic("vrele: null vp"); 967#endif 968 simple_lock(&vp->v_interlock); 969 970 if (vp->v_usecount > 1) { 971 972 vp->v_usecount--; 973 simple_unlock(&vp->v_interlock); 974 975 return; 976 } 977 978 if (vp->v_usecount == 1) { 979 980 vp->v_usecount--; 981 982 if (VSHOULDFREE(vp)) 983 vfree(vp); 984 /* 985 * If we are doing a vput, the node is already locked, and we must 986 * call VOP_INACTIVE with the node locked. So, in the case of 987 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE. 988 */ 989 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) { 990 VOP_INACTIVE(vp, p); 991 } 992 993 } else { 994#ifdef DIAGNOSTIC 995 vprint("vrele: negative ref count", vp); 996 simple_unlock(&vp->v_interlock); 997#endif 998 panic("vrele: negative ref cnt"); 999 } 1000} 1001 1002void 1003vput(vp) 1004 struct vnode *vp; 1005{ 1006 struct proc *p = curproc; /* XXX */ 1007 1008#ifdef DIAGNOSTIC 1009 if (vp == NULL) 1010 panic("vput: null vp"); 1011#endif 1012 1013 simple_lock(&vp->v_interlock); 1014 1015 if (vp->v_usecount > 1) { 1016 1017 vp->v_usecount--; 1018 VOP_UNLOCK(vp, LK_INTERLOCK, p); 1019 return; 1020 1021 } 1022 1023 if (vp->v_usecount == 1) { 1024 1025 vp->v_usecount--; 1026 if (VSHOULDFREE(vp)) 1027 vfree(vp); 1028 /* 1029 * If we are doing a vput, the node is already locked, and we must 1030 * call VOP_INACTIVE with the node locked. So, in the case of 1031 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE. 1032 */ 1033 simple_unlock(&vp->v_interlock); 1034 VOP_INACTIVE(vp, p); 1035 1036 } else { 1037#ifdef DIAGNOSTIC 1038 vprint("vput: negative ref count", vp); 1039#endif 1040 panic("vput: negative ref cnt"); 1041 } 1042} 1043 1044/* 1045 * Somebody doesn't want the vnode recycled. 1046 */ 1047void 1048vhold(vp) 1049 register struct vnode *vp; 1050{ 1051 1052 simple_lock(&vp->v_interlock); 1053 vp->v_holdcnt++; 1054 if (VSHOULDBUSY(vp)) 1055 vbusy(vp); 1056 simple_unlock(&vp->v_interlock); 1057} 1058 1059/* 1060 * One less who cares about this vnode. 1061 */ 1062void 1063vdrop(vp) 1064 register struct vnode *vp; 1065{ 1066 1067 simple_lock(&vp->v_interlock); 1068 if (vp->v_holdcnt <= 0) 1069 panic("holdrele: holdcnt"); 1070 vp->v_holdcnt--; 1071 if (VSHOULDFREE(vp)) 1072 vfree(vp); 1073 simple_unlock(&vp->v_interlock); 1074} 1075 1076/* 1077 * Remove any vnodes in the vnode table belonging to mount point mp. 1078 * 1079 * If MNT_NOFORCE is specified, there should not be any active ones, 1080 * return error if any are found (nb: this is a user error, not a 1081 * system error). If MNT_FORCE is specified, detach any active vnodes 1082 * that are found. 1083 */ 1084#ifdef DIAGNOSTIC 1085static int busyprt = 0; /* print out busy vnodes */ 1086SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 1087#endif 1088 1089int 1090vflush(mp, skipvp, flags) 1091 struct mount *mp; 1092 struct vnode *skipvp; 1093 int flags; 1094{ 1095 struct proc *p = curproc; /* XXX */ 1096 struct vnode *vp, *nvp; 1097 int busy = 0; 1098 1099 simple_lock(&mntvnode_slock); 1100loop: 1101 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 1102 /* 1103 * Make sure this vnode wasn't reclaimed in getnewvnode(). 1104 * Start over if it has (it won't be on the list anymore). 1105 */ 1106 if (vp->v_mount != mp) 1107 goto loop; 1108 nvp = vp->v_mntvnodes.le_next; 1109 /* 1110 * Skip over a selected vnode. 1111 */ 1112 if (vp == skipvp) 1113 continue; 1114 1115 simple_lock(&vp->v_interlock); 1116 /* 1117 * Skip over a vnodes marked VSYSTEM. 1118 */ 1119 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 1120 simple_unlock(&vp->v_interlock); 1121 continue; 1122 } 1123 /* 1124 * If WRITECLOSE is set, only flush out regular file vnodes 1125 * open for writing. 1126 */ 1127 if ((flags & WRITECLOSE) && 1128 (vp->v_writecount == 0 || vp->v_type != VREG)) { 1129 simple_unlock(&vp->v_interlock); 1130 continue; 1131 } 1132 1133 /* 1134 * With v_usecount == 0, all we need to do is clear out the 1135 * vnode data structures and we are done. 1136 */ 1137 if (vp->v_usecount == 0) { 1138 simple_unlock(&mntvnode_slock); 1139 vgonel(vp, p); 1140 simple_lock(&mntvnode_slock); 1141 continue; 1142 } 1143 1144 /* 1145 * If FORCECLOSE is set, forcibly close the vnode. For block 1146 * or character devices, revert to an anonymous device. For 1147 * all other files, just kill them. 1148 */ 1149 if (flags & FORCECLOSE) { 1150 simple_unlock(&mntvnode_slock); 1151 if (vp->v_type != VBLK && vp->v_type != VCHR) { 1152 vgonel(vp, p); 1153 } else { 1154 vclean(vp, 0, p); 1155 vp->v_op = spec_vnodeop_p; 1156 insmntque(vp, (struct mount *) 0); 1157 } 1158 simple_lock(&mntvnode_slock); 1159 continue; 1160 } 1161#ifdef DIAGNOSTIC 1162 if (busyprt) 1163 vprint("vflush: busy vnode", vp); 1164#endif 1165 simple_unlock(&vp->v_interlock); 1166 busy++; 1167 } 1168 simple_unlock(&mntvnode_slock); 1169 if (busy) 1170 return (EBUSY); 1171 return (0); 1172} 1173 1174/* 1175 * Disassociate the underlying file system from a vnode. 1176 */ 1177static void 1178vclean(vp, flags, p) 1179 struct vnode *vp; 1180 int flags; 1181 struct proc *p; 1182{ 1183 int active; 1184 vm_object_t obj; 1185 1186 /* 1187 * Check to see if the vnode is in use. If so we have to reference it 1188 * before we clean it out so that its count cannot fall to zero and 1189 * generate a race against ourselves to recycle it. 1190 */ 1191 if ((active = vp->v_usecount)) 1192 vp->v_usecount++; 1193 1194 /* 1195 * Prevent the vnode from being recycled or brought into use while we 1196 * clean it out. 1197 */ 1198 if (vp->v_flag & VXLOCK) 1199 panic("vclean: deadlock"); 1200 vp->v_flag |= VXLOCK; 1201 /* 1202 * Even if the count is zero, the VOP_INACTIVE routine may still 1203 * have the object locked while it cleans it out. The VOP_LOCK 1204 * ensures that the VOP_INACTIVE routine is done with its work. 1205 * For active vnodes, it ensures that no other activity can 1206 * occur while the underlying object is being cleaned out. 1207 */ 1208 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p); 1209 1210 /* 1211 * Clean out any buffers associated with the vnode. 1212 */ 1213 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0); 1214 if (obj = vp->v_object) { 1215 if (obj->ref_count == 0) { 1216 /* 1217 * This is a normal way of shutting down the object/vnode 1218 * association. 1219 */ 1220 vm_object_terminate(obj); 1221 } else { 1222 /* 1223 * Woe to the process that tries to page now :-). 1224 */ 1225 vm_pager_deallocate(obj); 1226 } 1227 } 1228 1229 /* 1230 * If purging an active vnode, it must be closed and 1231 * deactivated before being reclaimed. Note that the 1232 * VOP_INACTIVE will unlock the vnode. 1233 */ 1234 if (active) { 1235 if (flags & DOCLOSE) 1236 VOP_CLOSE(vp, IO_NDELAY, NOCRED, p); 1237 VOP_INACTIVE(vp, p); 1238 } else { 1239 /* 1240 * Any other processes trying to obtain this lock must first 1241 * wait for VXLOCK to clear, then call the new lock operation. 1242 */ 1243 VOP_UNLOCK(vp, 0, p); 1244 } 1245 /* 1246 * Reclaim the vnode. 1247 */ 1248 if (VOP_RECLAIM(vp, p)) 1249 panic("vclean: cannot reclaim"); 1250 1251 if (active) 1252 vrele(vp); 1253 1254 cache_purge(vp); 1255 if (vp->v_vnlock) { 1256#if 0 /* This is the only place we have LK_DRAINED in the entire kernel ??? */ 1257#ifdef DIAGNOSTIC 1258 if ((vp->v_vnlock->lk_flags & LK_DRAINED) == 0) 1259 vprint("vclean: lock not drained", vp); 1260#endif 1261#endif 1262 FREE(vp->v_vnlock, M_VNODE); 1263 vp->v_vnlock = NULL; 1264 } 1265 1266 if (VSHOULDFREE(vp)) 1267 vfree(vp); 1268 1269 /* 1270 * Done with purge, notify sleepers of the grim news. 1271 */ 1272 vp->v_op = dead_vnodeop_p; 1273 vn_pollgone(vp); 1274 vp->v_tag = VT_NON; 1275 vp->v_flag &= ~VXLOCK; 1276 if (vp->v_flag & VXWANT) { 1277 vp->v_flag &= ~VXWANT; 1278 wakeup((caddr_t) vp); 1279 } 1280} 1281 1282/* 1283 * Eliminate all activity associated with the requested vnode 1284 * and with all vnodes aliased to the requested vnode. 1285 */ 1286int 1287vop_revoke(ap) 1288 struct vop_revoke_args /* { 1289 struct vnode *a_vp; 1290 int a_flags; 1291 } */ *ap; 1292{ 1293 struct vnode *vp, *vq; 1294 struct proc *p = curproc; /* XXX */ 1295 1296#ifdef DIAGNOSTIC 1297 if ((ap->a_flags & REVOKEALL) == 0) 1298 panic("vop_revoke"); 1299#endif 1300 1301 vp = ap->a_vp; 1302 simple_lock(&vp->v_interlock); 1303 1304 if (vp->v_flag & VALIASED) { 1305 /* 1306 * If a vgone (or vclean) is already in progress, 1307 * wait until it is done and return. 1308 */ 1309 if (vp->v_flag & VXLOCK) { 1310 vp->v_flag |= VXWANT; 1311 simple_unlock(&vp->v_interlock); 1312 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0); 1313 return (0); 1314 } 1315 /* 1316 * Ensure that vp will not be vgone'd while we 1317 * are eliminating its aliases. 1318 */ 1319 vp->v_flag |= VXLOCK; 1320 simple_unlock(&vp->v_interlock); 1321 while (vp->v_flag & VALIASED) { 1322 simple_lock(&spechash_slock); 1323 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1324 if (vq->v_rdev != vp->v_rdev || 1325 vq->v_type != vp->v_type || vp == vq) 1326 continue; 1327 simple_unlock(&spechash_slock); 1328 vgone(vq); 1329 break; 1330 } 1331 if (vq == NULLVP) { 1332 simple_unlock(&spechash_slock); 1333 } 1334 } 1335 /* 1336 * Remove the lock so that vgone below will 1337 * really eliminate the vnode after which time 1338 * vgone will awaken any sleepers. 1339 */ 1340 simple_lock(&vp->v_interlock); 1341 vp->v_flag &= ~VXLOCK; 1342 if (vp->v_flag & VXWANT) { 1343 vp->v_flag &= ~VXWANT; 1344 wakeup(vp); 1345 } 1346 } 1347 vgonel(vp, p); 1348 return (0); 1349} 1350 1351/* 1352 * Recycle an unused vnode to the front of the free list. 1353 * Release the passed interlock if the vnode will be recycled. 1354 */ 1355int 1356vrecycle(vp, inter_lkp, p) 1357 struct vnode *vp; 1358 struct simplelock *inter_lkp; 1359 struct proc *p; 1360{ 1361 1362 simple_lock(&vp->v_interlock); 1363 if (vp->v_usecount == 0) { 1364 if (inter_lkp) { 1365 simple_unlock(inter_lkp); 1366 } 1367 vgonel(vp, p); 1368 return (1); 1369 } 1370 simple_unlock(&vp->v_interlock); 1371 return (0); 1372} 1373 1374/* 1375 * Eliminate all activity associated with a vnode 1376 * in preparation for reuse. 1377 */ 1378void 1379vgone(vp) 1380 register struct vnode *vp; 1381{ 1382 struct proc *p = curproc; /* XXX */ 1383 1384 simple_lock(&vp->v_interlock); 1385 vgonel(vp, p); 1386} 1387 1388/* 1389 * vgone, with the vp interlock held. 1390 */ 1391static void 1392vgonel(vp, p) 1393 struct vnode *vp; 1394 struct proc *p; 1395{ 1396 int s; 1397 struct vnode *vq; 1398 struct vnode *vx; 1399 1400 /* 1401 * If a vgone (or vclean) is already in progress, 1402 * wait until it is done and return. 1403 */ 1404 if (vp->v_flag & VXLOCK) { 1405 vp->v_flag |= VXWANT; 1406 simple_unlock(&vp->v_interlock); 1407 tsleep((caddr_t)vp, PINOD, "vgone", 0); 1408 return; 1409 } 1410 1411 /* 1412 * Clean out the filesystem specific data. 1413 */ 1414 vclean(vp, DOCLOSE, p); 1415 simple_lock(&vp->v_interlock); 1416 1417 /* 1418 * Delete from old mount point vnode list, if on one. 1419 */ 1420 if (vp->v_mount != NULL) 1421 insmntque(vp, (struct mount *)0); 1422 /* 1423 * If special device, remove it from special device alias list 1424 * if it is on one. 1425 */ 1426 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) { 1427 simple_lock(&spechash_slock); 1428 if (*vp->v_hashchain == vp) { 1429 *vp->v_hashchain = vp->v_specnext; 1430 } else { 1431 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1432 if (vq->v_specnext != vp) 1433 continue; 1434 vq->v_specnext = vp->v_specnext; 1435 break; 1436 } 1437 if (vq == NULL) 1438 panic("missing bdev"); 1439 } 1440 if (vp->v_flag & VALIASED) { 1441 vx = NULL; 1442 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1443 if (vq->v_rdev != vp->v_rdev || 1444 vq->v_type != vp->v_type) 1445 continue; 1446 if (vx) 1447 break; 1448 vx = vq; 1449 } 1450 if (vx == NULL) 1451 panic("missing alias"); 1452 if (vq == NULL) 1453 vx->v_flag &= ~VALIASED; 1454 vp->v_flag &= ~VALIASED; 1455 } 1456 simple_unlock(&spechash_slock); 1457 FREE(vp->v_specinfo, M_VNODE); 1458 vp->v_specinfo = NULL; 1459 } 1460 1461 /* 1462 * If it is on the freelist and not already at the head, 1463 * move it to the head of the list. The test of the back 1464 * pointer and the reference count of zero is because 1465 * it will be removed from the free list by getnewvnode, 1466 * but will not have its reference count incremented until 1467 * after calling vgone. If the reference count were 1468 * incremented first, vgone would (incorrectly) try to 1469 * close the previous instance of the underlying object. 1470 */ 1471 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) { 1472 s = splbio(); 1473 simple_lock(&vnode_free_list_slock); 1474 if (vp->v_flag & VFREE) { 1475 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1476 } else if (vp->v_flag & VTBFREE) { 1477 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist); 1478 vp->v_flag &= ~VTBFREE; 1479 freevnodes++; 1480 } else 1481 freevnodes++; 1482 vp->v_flag |= VFREE; 1483 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1484 simple_unlock(&vnode_free_list_slock); 1485 splx(s); 1486 } 1487 1488 vp->v_type = VBAD; 1489 simple_unlock(&vp->v_interlock); 1490} 1491 1492/* 1493 * Lookup a vnode by device number. 1494 */ 1495int 1496vfinddev(dev, type, vpp) 1497 dev_t dev; 1498 enum vtype type; 1499 struct vnode **vpp; 1500{ 1501 register struct vnode *vp; 1502 int rc = 0; 1503 1504 simple_lock(&spechash_slock); 1505 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 1506 if (dev != vp->v_rdev || type != vp->v_type) 1507 continue; 1508 *vpp = vp; 1509 rc = 1; 1510 break; 1511 } 1512 simple_unlock(&spechash_slock); 1513 return (rc); 1514} 1515 1516/* 1517 * Calculate the total number of references to a special device. 1518 */ 1519int 1520vcount(vp) 1521 register struct vnode *vp; 1522{ 1523 struct vnode *vq, *vnext; 1524 int count; 1525 1526loop: 1527 if ((vp->v_flag & VALIASED) == 0) 1528 return (vp->v_usecount); 1529 simple_lock(&spechash_slock); 1530 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) { 1531 vnext = vq->v_specnext; 1532 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 1533 continue; 1534 /* 1535 * Alias, but not in use, so flush it out. 1536 */ 1537 if (vq->v_usecount == 0 && vq != vp) { 1538 simple_unlock(&spechash_slock); 1539 vgone(vq); 1540 goto loop; 1541 } 1542 count += vq->v_usecount; 1543 } 1544 simple_unlock(&spechash_slock); 1545 return (count); 1546} 1547/* 1548 * Print out a description of a vnode. 1549 */ 1550static char *typename[] = 1551{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 1552 1553void 1554vprint(label, vp) 1555 char *label; 1556 register struct vnode *vp; 1557{ 1558 char buf[64]; 1559 1560 if (label != NULL) 1561 printf("%s: %x: ", label, vp); 1562 else 1563 printf("%x: ", vp); 1564 printf("type %s, usecount %d, writecount %d, refcount %ld,", 1565 typename[vp->v_type], vp->v_usecount, vp->v_writecount, 1566 vp->v_holdcnt); 1567 buf[0] = '\0'; 1568 if (vp->v_flag & VROOT) 1569 strcat(buf, "|VROOT"); 1570 if (vp->v_flag & VTEXT) 1571 strcat(buf, "|VTEXT"); 1572 if (vp->v_flag & VSYSTEM) 1573 strcat(buf, "|VSYSTEM"); 1574 if (vp->v_flag & VXLOCK) 1575 strcat(buf, "|VXLOCK"); 1576 if (vp->v_flag & VXWANT) 1577 strcat(buf, "|VXWANT"); 1578 if (vp->v_flag & VBWAIT) 1579 strcat(buf, "|VBWAIT"); 1580 if (vp->v_flag & VALIASED) 1581 strcat(buf, "|VALIASED"); 1582 if (vp->v_flag & VDOOMED) 1583 strcat(buf, "|VDOOMED"); 1584 if (vp->v_flag & VFREE) 1585 strcat(buf, "|VFREE"); 1586 if (vp->v_flag & VOBJBUF) 1587 strcat(buf, "|VOBJBUF"); 1588 if (buf[0] != '\0') 1589 printf(" flags (%s)", &buf[1]); 1590 if (vp->v_data == NULL) { 1591 printf("\n"); 1592 } else { 1593 printf("\n\t"); 1594 VOP_PRINT(vp); 1595 } 1596} 1597 1598#ifdef DDB 1599/* 1600 * List all of the locked vnodes in the system. 1601 * Called when debugging the kernel. 1602 */ 1603static void 1604printlockedvnodes() 1605{ 1606 struct proc *p = curproc; /* XXX */ 1607 struct mount *mp, *nmp; 1608 struct vnode *vp; 1609 1610 printf("Locked vnodes\n"); 1611 simple_lock(&mountlist_slock); 1612 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { 1613 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) { 1614 nmp = mp->mnt_list.cqe_next; 1615 continue; 1616 } 1617 for (vp = mp->mnt_vnodelist.lh_first; 1618 vp != NULL; 1619 vp = vp->v_mntvnodes.le_next) { 1620 if (VOP_ISLOCKED(vp)) 1621 vprint((char *)0, vp); 1622 } 1623 simple_lock(&mountlist_slock); 1624 nmp = mp->mnt_list.cqe_next; 1625 vfs_unbusy(mp, p); 1626 } 1627 simple_unlock(&mountlist_slock); 1628} 1629#endif 1630 1631/* 1632 * Top level filesystem related information gathering. 1633 */ 1634static int sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS); 1635 1636static int 1637vfs_sysctl SYSCTL_HANDLER_ARGS 1638{ 1639 int *name = (int *)arg1 - 1; /* XXX */ 1640 u_int namelen = arg2 + 1; /* XXX */ 1641 struct vfsconf *vfsp; 1642 1643#ifndef NO_COMPAT_PRELITE2 1644 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 1645 if (namelen == 1) 1646 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 1647#endif 1648 1649#ifdef notyet 1650 /* all sysctl names at this level are at least name and field */ 1651 if (namelen < 2) 1652 return (ENOTDIR); /* overloaded */ 1653 if (name[0] != VFS_GENERIC) { 1654 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 1655 if (vfsp->vfc_typenum == name[0]) 1656 break; 1657 if (vfsp == NULL) 1658 return (EOPNOTSUPP); 1659 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1660 oldp, oldlenp, newp, newlen, p)); 1661 } 1662#endif 1663 switch (name[1]) { 1664 case VFS_MAXTYPENUM: 1665 if (namelen != 2) 1666 return (ENOTDIR); 1667 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 1668 case VFS_CONF: 1669 if (namelen != 3) 1670 return (ENOTDIR); /* overloaded */ 1671 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 1672 if (vfsp->vfc_typenum == name[2]) 1673 break; 1674 if (vfsp == NULL) 1675 return (EOPNOTSUPP); 1676 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); 1677 } 1678 return (EOPNOTSUPP); 1679} 1680 1681SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, 1682 "Generic filesystem"); 1683 1684#ifndef NO_COMPAT_PRELITE2 1685 1686static int 1687sysctl_ovfs_conf SYSCTL_HANDLER_ARGS 1688{ 1689 int error; 1690 struct vfsconf *vfsp; 1691 struct ovfsconf ovfs; 1692 1693 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 1694 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 1695 strcpy(ovfs.vfc_name, vfsp->vfc_name); 1696 ovfs.vfc_index = vfsp->vfc_typenum; 1697 ovfs.vfc_refcount = vfsp->vfc_refcount; 1698 ovfs.vfc_flags = vfsp->vfc_flags; 1699 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 1700 if (error) 1701 return error; 1702 } 1703 return 0; 1704} 1705 1706#endif /* !NO_COMPAT_PRELITE2 */ 1707 1708static volatile int kinfo_vdebug = 1; 1709 1710#if 0 1711#define KINFO_VNODESLOP 10 1712/* 1713 * Dump vnode list (via sysctl). 1714 * Copyout address of vnode followed by vnode. 1715 */ 1716/* ARGSUSED */ 1717static int 1718sysctl_vnode SYSCTL_HANDLER_ARGS 1719{ 1720 struct proc *p = curproc; /* XXX */ 1721 struct mount *mp, *nmp; 1722 struct vnode *nvp, *vp; 1723 int error; 1724 1725#define VPTRSZ sizeof (struct vnode *) 1726#define VNODESZ sizeof (struct vnode) 1727 1728 req->lock = 0; 1729 if (!req->oldptr) /* Make an estimate */ 1730 return (SYSCTL_OUT(req, 0, 1731 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ))); 1732 1733 simple_lock(&mountlist_slock); 1734 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { 1735 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) { 1736 nmp = mp->mnt_list.cqe_next; 1737 continue; 1738 } 1739again: 1740 simple_lock(&mntvnode_slock); 1741 for (vp = mp->mnt_vnodelist.lh_first; 1742 vp != NULL; 1743 vp = nvp) { 1744 /* 1745 * Check that the vp is still associated with 1746 * this filesystem. RACE: could have been 1747 * recycled onto the same filesystem. 1748 */ 1749 if (vp->v_mount != mp) { 1750 simple_unlock(&mntvnode_slock); 1751 if (kinfo_vdebug) 1752 printf("kinfo: vp changed\n"); 1753 goto again; 1754 } 1755 nvp = vp->v_mntvnodes.le_next; 1756 simple_unlock(&mntvnode_slock); 1757 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) || 1758 (error = SYSCTL_OUT(req, vp, VNODESZ))) 1759 return (error); 1760 simple_lock(&mntvnode_slock); 1761 } 1762 simple_unlock(&mntvnode_slock); 1763 simple_lock(&mountlist_slock); 1764 nmp = mp->mnt_list.cqe_next; 1765 vfs_unbusy(mp, p); 1766 } 1767 simple_unlock(&mountlist_slock); 1768 1769 return (0); 1770} 1771#endif 1772 1773/* 1774 * XXX 1775 * Exporting the vnode list on large systems causes them to crash. 1776 * Exporting the vnode list on medium systems causes sysctl to coredump. 1777 */ 1778#if 0 1779SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 1780 0, 0, sysctl_vnode, "S,vnode", ""); 1781#endif 1782 1783/* 1784 * Check to see if a filesystem is mounted on a block device. 1785 */ 1786int 1787vfs_mountedon(vp) 1788 struct vnode *vp; 1789{ 1790 struct vnode *vq; 1791 int error = 0; 1792 1793 if (vp->v_specflags & SI_MOUNTEDON) 1794 return (EBUSY); 1795 if (vp->v_flag & VALIASED) { 1796 simple_lock(&spechash_slock); 1797 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1798 if (vq->v_rdev != vp->v_rdev || 1799 vq->v_type != vp->v_type) 1800 continue; 1801 if (vq->v_specflags & SI_MOUNTEDON) { 1802 error = EBUSY; 1803 break; 1804 } 1805 } 1806 simple_unlock(&spechash_slock); 1807 } 1808 return (error); 1809} 1810 1811/* 1812 * Unmount all filesystems. The list is traversed in reverse order 1813 * of mounting to avoid dependencies. 1814 */ 1815void 1816vfs_unmountall() 1817{ 1818 struct mount *mp, *nmp; 1819 struct proc *p = initproc; /* XXX XXX should this be proc0? */ 1820 int error; 1821 1822 /* 1823 * Since this only runs when rebooting, it is not interlocked. 1824 */ 1825 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) { 1826 nmp = mp->mnt_list.cqe_prev; 1827 error = dounmount(mp, MNT_FORCE, p); 1828 if (error) { 1829 printf("unmount of %s failed (", 1830 mp->mnt_stat.f_mntonname); 1831 if (error == EBUSY) 1832 printf("BUSY)\n"); 1833 else 1834 printf("%d)\n", error); 1835 } 1836 } 1837} 1838 1839/* 1840 * Build hash lists of net addresses and hang them off the mount point. 1841 * Called by ufs_mount() to set up the lists of export addresses. 1842 */ 1843static int 1844vfs_hang_addrlist(mp, nep, argp) 1845 struct mount *mp; 1846 struct netexport *nep; 1847 struct export_args *argp; 1848{ 1849 register struct netcred *np; 1850 register struct radix_node_head *rnh; 1851 register int i; 1852 struct radix_node *rn; 1853 struct sockaddr *saddr, *smask = 0; 1854 struct domain *dom; 1855 int error; 1856 1857 if (argp->ex_addrlen == 0) { 1858 if (mp->mnt_flag & MNT_DEFEXPORTED) 1859 return (EPERM); 1860 np = &nep->ne_defexported; 1861 np->netc_exflags = argp->ex_flags; 1862 np->netc_anon = argp->ex_anon; 1863 np->netc_anon.cr_ref = 1; 1864 mp->mnt_flag |= MNT_DEFEXPORTED; 1865 return (0); 1866 } 1867 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1868 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK); 1869 bzero((caddr_t) np, i); 1870 saddr = (struct sockaddr *) (np + 1); 1871 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) 1872 goto out; 1873 if (saddr->sa_len > argp->ex_addrlen) 1874 saddr->sa_len = argp->ex_addrlen; 1875 if (argp->ex_masklen) { 1876 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen); 1877 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen); 1878 if (error) 1879 goto out; 1880 if (smask->sa_len > argp->ex_masklen) 1881 smask->sa_len = argp->ex_masklen; 1882 } 1883 i = saddr->sa_family; 1884 if ((rnh = nep->ne_rtable[i]) == 0) { 1885 /* 1886 * Seems silly to initialize every AF when most are not used, 1887 * do so on demand here 1888 */ 1889 for (dom = domains; dom; dom = dom->dom_next) 1890 if (dom->dom_family == i && dom->dom_rtattach) { 1891 dom->dom_rtattach((void **) &nep->ne_rtable[i], 1892 dom->dom_rtoffset); 1893 break; 1894 } 1895 if ((rnh = nep->ne_rtable[i]) == 0) { 1896 error = ENOBUFS; 1897 goto out; 1898 } 1899 } 1900 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh, 1901 np->netc_rnodes); 1902 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */ 1903 error = EPERM; 1904 goto out; 1905 } 1906 np->netc_exflags = argp->ex_flags; 1907 np->netc_anon = argp->ex_anon; 1908 np->netc_anon.cr_ref = 1; 1909 return (0); 1910out: 1911 free(np, M_NETADDR); 1912 return (error); 1913} 1914 1915/* ARGSUSED */ 1916static int 1917vfs_free_netcred(rn, w) 1918 struct radix_node *rn; 1919 void *w; 1920{ 1921 register struct radix_node_head *rnh = (struct radix_node_head *) w; 1922 1923 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 1924 free((caddr_t) rn, M_NETADDR); 1925 return (0); 1926} 1927 1928/* 1929 * Free the net address hash lists that are hanging off the mount points. 1930 */ 1931static void 1932vfs_free_addrlist(nep) 1933 struct netexport *nep; 1934{ 1935 register int i; 1936 register struct radix_node_head *rnh; 1937 1938 for (i = 0; i <= AF_MAX; i++) 1939 if ((rnh = nep->ne_rtable[i])) { 1940 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, 1941 (caddr_t) rnh); 1942 free((caddr_t) rnh, M_RTABLE); 1943 nep->ne_rtable[i] = 0; 1944 } 1945} 1946 1947int 1948vfs_export(mp, nep, argp) 1949 struct mount *mp; 1950 struct netexport *nep; 1951 struct export_args *argp; 1952{ 1953 int error; 1954 1955 if (argp->ex_flags & MNT_DELEXPORT) { 1956 if (mp->mnt_flag & MNT_EXPUBLIC) { 1957 vfs_setpublicfs(NULL, NULL, NULL); 1958 mp->mnt_flag &= ~MNT_EXPUBLIC; 1959 } 1960 vfs_free_addrlist(nep); 1961 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 1962 } 1963 if (argp->ex_flags & MNT_EXPORTED) { 1964 if (argp->ex_flags & MNT_EXPUBLIC) { 1965 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 1966 return (error); 1967 mp->mnt_flag |= MNT_EXPUBLIC; 1968 } 1969 if ((error = vfs_hang_addrlist(mp, nep, argp))) 1970 return (error); 1971 mp->mnt_flag |= MNT_EXPORTED; 1972 } 1973 return (0); 1974} 1975 1976 1977/* 1978 * Set the publicly exported filesystem (WebNFS). Currently, only 1979 * one public filesystem is possible in the spec (RFC 2054 and 2055) 1980 */ 1981int 1982vfs_setpublicfs(mp, nep, argp) 1983 struct mount *mp; 1984 struct netexport *nep; 1985 struct export_args *argp; 1986{ 1987 int error; 1988 struct vnode *rvp; 1989 char *cp; 1990 1991 /* 1992 * mp == NULL -> invalidate the current info, the FS is 1993 * no longer exported. May be called from either vfs_export 1994 * or unmount, so check if it hasn't already been done. 1995 */ 1996 if (mp == NULL) { 1997 if (nfs_pub.np_valid) { 1998 nfs_pub.np_valid = 0; 1999 if (nfs_pub.np_index != NULL) { 2000 FREE(nfs_pub.np_index, M_TEMP); 2001 nfs_pub.np_index = NULL; 2002 } 2003 } 2004 return (0); 2005 } 2006 2007 /* 2008 * Only one allowed at a time. 2009 */ 2010 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 2011 return (EBUSY); 2012 2013 /* 2014 * Get real filehandle for root of exported FS. 2015 */ 2016 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 2017 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 2018 2019 if ((error = VFS_ROOT(mp, &rvp))) 2020 return (error); 2021 2022 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 2023 return (error); 2024 2025 vput(rvp); 2026 2027 /* 2028 * If an indexfile was specified, pull it in. 2029 */ 2030 if (argp->ex_indexfile != NULL) { 2031 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP, 2032 M_WAITOK); 2033 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 2034 MAXNAMLEN, (size_t *)0); 2035 if (!error) { 2036 /* 2037 * Check for illegal filenames. 2038 */ 2039 for (cp = nfs_pub.np_index; *cp; cp++) { 2040 if (*cp == '/') { 2041 error = EINVAL; 2042 break; 2043 } 2044 } 2045 } 2046 if (error) { 2047 FREE(nfs_pub.np_index, M_TEMP); 2048 return (error); 2049 } 2050 } 2051 2052 nfs_pub.np_mount = mp; 2053 nfs_pub.np_valid = 1; 2054 return (0); 2055} 2056 2057struct netcred * 2058vfs_export_lookup(mp, nep, nam) 2059 register struct mount *mp; 2060 struct netexport *nep; 2061 struct sockaddr *nam; 2062{ 2063 register struct netcred *np; 2064 register struct radix_node_head *rnh; 2065 struct sockaddr *saddr; 2066 2067 np = NULL; 2068 if (mp->mnt_flag & MNT_EXPORTED) { 2069 /* 2070 * Lookup in the export list first. 2071 */ 2072 if (nam != NULL) { 2073 saddr = nam; 2074 rnh = nep->ne_rtable[saddr->sa_family]; 2075 if (rnh != NULL) { 2076 np = (struct netcred *) 2077 (*rnh->rnh_matchaddr)((caddr_t)saddr, 2078 rnh); 2079 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 2080 np = NULL; 2081 } 2082 } 2083 /* 2084 * If no address match, use the default if it exists. 2085 */ 2086 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 2087 np = &nep->ne_defexported; 2088 } 2089 return (np); 2090} 2091 2092/* 2093 * perform msync on all vnodes under a mount point 2094 * the mount point must be locked. 2095 */ 2096void 2097vfs_msync(struct mount *mp, int flags) { 2098 struct vnode *vp, *nvp; 2099 int anyio, tries; 2100 2101 tries = 5; 2102loop: 2103 anyio = 0; 2104 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 2105 2106 nvp = vp->v_mntvnodes.le_next; 2107 2108 if (vp->v_mount != mp) { 2109 goto loop; 2110 } 2111 2112 if ((vp->v_flag & VXLOCK) || 2113 (VOP_ISLOCKED(vp) && (flags != MNT_WAIT))) { 2114 continue; 2115 } 2116 2117 simple_lock(&vp->v_interlock); 2118 if (vp->v_object && 2119 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 2120 if (!vget(vp, 2121 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) { 2122 if (vp->v_object) { 2123 vm_object_page_clean(vp->v_object, 0, 0, TRUE); 2124 anyio = 1; 2125 } 2126 vput(vp); 2127 } 2128 } else { 2129 simple_unlock(&vp->v_interlock); 2130 } 2131 } 2132 if (anyio && (--tries > 0)) 2133 goto loop; 2134} 2135 2136/* 2137 * Create the VM object needed for VMIO and mmap support. This 2138 * is done for all VREG files in the system. Some filesystems might 2139 * afford the additional metadata buffering capability of the 2140 * VMIO code by making the device node be VMIO mode also. 2141 * 2142 * If !waslocked, must be called with interlock. 2143 */ 2144int 2145vfs_object_create(vp, p, cred, waslocked) 2146 struct vnode *vp; 2147 struct proc *p; 2148 struct ucred *cred; 2149 int waslocked; 2150{ 2151 struct vattr vat; 2152 vm_object_t object; 2153 int error = 0; 2154 2155 if ((vp->v_type != VREG) && (vp->v_type != VBLK)) { 2156 if (!waslocked) 2157 simple_unlock(&vp->v_interlock); 2158 return 0; 2159 } 2160 2161 if (!waslocked) 2162 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, p); 2163 2164retry: 2165 if ((object = vp->v_object) == NULL) { 2166 if (vp->v_type == VREG) { 2167 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0) 2168 goto retn; 2169 object = vnode_pager_alloc(vp, 2170 OFF_TO_IDX(round_page(vat.va_size)), 0, 0); 2171 } else if (major(vp->v_rdev) < nblkdev) { 2172 /* 2173 * This simply allocates the biggest object possible 2174 * for a VBLK vnode. This should be fixed, but doesn't 2175 * cause any problems (yet). 2176 */ 2177 object = vnode_pager_alloc(vp, INT_MAX, 0, 0); 2178 } 2179 object->ref_count--; 2180 vp->v_usecount--; 2181 } else { 2182 if (object->flags & OBJ_DEAD) { 2183 VOP_UNLOCK(vp, 0, p); 2184 tsleep(object, PVM, "vodead", 0); 2185 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 2186 goto retry; 2187 } 2188 } 2189 2190 if (vp->v_object) { 2191 vp->v_flag |= VOBJBUF; 2192 } 2193 2194retn: 2195 if (!waslocked) { 2196 simple_lock(&vp->v_interlock); 2197 VOP_UNLOCK(vp, LK_INTERLOCK, p); 2198 } 2199 2200 return error; 2201} 2202 2203static void 2204vfree(vp) 2205 struct vnode *vp; 2206{ 2207 int s; 2208 2209 s = splbio(); 2210 simple_lock(&vnode_free_list_slock); 2211 if (vp->v_flag & VTBFREE) { 2212 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist); 2213 vp->v_flag &= ~VTBFREE; 2214 } 2215 if (vp->v_flag & VAGE) { 2216 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2217 } else { 2218 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 2219 } 2220 freevnodes++; 2221 simple_unlock(&vnode_free_list_slock); 2222 vp->v_flag &= ~VAGE; 2223 vp->v_flag |= VFREE; 2224 splx(s); 2225} 2226 2227void 2228vbusy(vp) 2229 struct vnode *vp; 2230{ 2231 int s; 2232 2233 s = splbio(); 2234 simple_lock(&vnode_free_list_slock); 2235 if (vp->v_flag & VTBFREE) { 2236 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist); 2237 vp->v_flag &= ~VTBFREE; 2238 } else { 2239 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2240 freevnodes--; 2241 } 2242 simple_unlock(&vnode_free_list_slock); 2243 vp->v_flag &= ~(VFREE|VAGE); 2244 splx(s); 2245} 2246 2247/* 2248 * Record a process's interest in events which might happen to 2249 * a vnode. Because poll uses the historic select-style interface 2250 * internally, this routine serves as both the ``check for any 2251 * pending events'' and the ``record my interest in future events'' 2252 * functions. (These are done together, while the lock is held, 2253 * to avoid race conditions.) 2254 */ 2255int 2256vn_pollrecord(vp, p, events) 2257 struct vnode *vp; 2258 struct proc *p; 2259 short events; 2260{ 2261 simple_lock(&vp->v_pollinfo.vpi_lock); 2262 if (vp->v_pollinfo.vpi_revents & events) { 2263 /* 2264 * This leaves events we are not interested 2265 * in available for the other process which 2266 * which presumably had requested them 2267 * (otherwise they would never have been 2268 * recorded). 2269 */ 2270 events &= vp->v_pollinfo.vpi_revents; 2271 vp->v_pollinfo.vpi_revents &= ~events; 2272 2273 simple_unlock(&vp->v_pollinfo.vpi_lock); 2274 return events; 2275 } 2276 vp->v_pollinfo.vpi_events |= events; 2277 selrecord(p, &vp->v_pollinfo.vpi_selinfo); 2278 simple_unlock(&vp->v_pollinfo.vpi_lock); 2279 return 0; 2280} 2281 2282/* 2283 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 2284 * it is possible for us to miss an event due to race conditions, but 2285 * that condition is expected to be rare, so for the moment it is the 2286 * preferred interface. 2287 */ 2288void 2289vn_pollevent(vp, events) 2290 struct vnode *vp; 2291 short events; 2292{ 2293 simple_lock(&vp->v_pollinfo.vpi_lock); 2294 if (vp->v_pollinfo.vpi_events & events) { 2295 /* 2296 * We clear vpi_events so that we don't 2297 * call selwakeup() twice if two events are 2298 * posted before the polling process(es) is 2299 * awakened. This also ensures that we take at 2300 * most one selwakeup() if the polling process 2301 * is no longer interested. However, it does 2302 * mean that only one event can be noticed at 2303 * a time. (Perhaps we should only clear those 2304 * event bits which we note?) XXX 2305 */ 2306 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */ 2307 vp->v_pollinfo.vpi_revents |= events; 2308 selwakeup(&vp->v_pollinfo.vpi_selinfo); 2309 } 2310 simple_unlock(&vp->v_pollinfo.vpi_lock); 2311} 2312 2313/* 2314 * Wake up anyone polling on vp because it is being revoked. 2315 * This depends on dead_poll() returning POLLHUP for correct 2316 * behavior. 2317 */ 2318void 2319vn_pollgone(vp) 2320 struct vnode *vp; 2321{ 2322 simple_lock(&vp->v_pollinfo.vpi_lock); 2323 if (vp->v_pollinfo.vpi_events) { 2324 vp->v_pollinfo.vpi_events = 0; 2325 selwakeup(&vp->v_pollinfo.vpi_selinfo); 2326 } 2327 simple_unlock(&vp->v_pollinfo.vpi_lock); 2328} 2329