1 /* $NetBSD: vfs_subr.c,v 1.95 1998/11/15 18:38:11 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1989, 1993 42 * The Regents of the University of California. All rights reserved. 43 * (c) UNIX System Laboratories, Inc. 44 * All or some portions of this file are derived from material licensed 45 * to the University of California by American Telephone and Telegraph 46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 47 * the permission of UNIX System Laboratories, Inc. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 * 77 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94 78 */ 79 80 /* 81 * External virtual filesystem routines 82 */ 83 84 #include "opt_compat_netbsd.h" 85 #include "opt_uvm.h" 86 87 #include <sys/param.h> 88 #include <sys/systm.h> 89 #include <sys/proc.h> 90 #include <sys/mount.h> 91 #include <sys/time.h> 92 #include <sys/fcntl.h> 93 #include <sys/vnode.h> 94 #include <sys/stat.h> 95 #include <sys/namei.h> 96 #include <sys/ucred.h> 97 #include <sys/buf.h> 98 #include <sys/errno.h> 99 #include <sys/malloc.h> 100 #include <sys/domain.h> 101 #include <sys/mbuf.h> 102 #include <sys/syscallargs.h> 103 #include <sys/device.h> 104 #include <sys/dirent.h> 105 106 #include <vm/vm.h> 107 #include <sys/sysctl.h> 108 109 #include <miscfs/specfs/specdev.h> 110 111 #if defined(UVM) 112 #include <uvm/uvm_extern.h> 113 #endif 114 115 enum vtype iftovt_tab[16] = { 116 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 117 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 118 }; 119 int vttoif_tab[9] = { 120 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 121 S_IFSOCK, S_IFIFO, S_IFMT, 122 }; 123 124 int doforce = 1; /* 1 => permit forcible unmounting */ 125 int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 126 127 /* 128 * Insq/Remq for the vnode usage lists. 129 */ 130 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 131 #define bufremvn(bp) { \ 132 LIST_REMOVE(bp, b_vnbufs); \ 133 (bp)->b_vnbufs.le_next = NOLIST; \ 134 } 135 TAILQ_HEAD(freelst, vnode) vnode_free_list = /* vnode free list */ 136 TAILQ_HEAD_INITIALIZER(vnode_free_list); 137 struct mntlist mountlist = /* mounted filesystem list */ 138 CIRCLEQ_HEAD_INITIALIZER(mountlist); 139 struct vfs_list_head vfs_list = /* vfs list */ 140 LIST_HEAD_INITIALIZER(vfs_list); 141 142 struct nfs_public nfs_pub; /* publicly exported FS */ 143 144 struct simplelock mountlist_slock; 145 static struct simplelock mntid_slock; 146 struct simplelock mntvnode_slock; 147 struct simplelock vnode_free_list_slock; 148 struct simplelock spechash_slock; 149 150 /* 151 * These define the root filesystem and device. 152 */ 153 struct mount *rootfs; 154 struct vnode *rootvnode; 155 struct device *root_device; /* root device */ 156 157 struct pool vnode_pool; /* memory pool for vnodes */ 158 159 /* 160 * Local declarations. 161 */ 162 void insmntque __P((struct vnode *, struct mount *)); 163 int getdevvp __P((dev_t, struct vnode **, enum vtype)); 164 void vgoneall __P((struct vnode *)); 165 166 static int vfs_hang_addrlist __P((struct mount *, struct netexport *, 167 struct export_args *)); 168 static int vfs_free_netcred __P((struct radix_node *, void *)); 169 static void vfs_free_addrlist __P((struct netexport *)); 170 171 #ifdef DEBUG 172 void printlockedvnodes __P((void)); 173 #endif 174 175 /* 176 * Initialize the vnode management data structures. 177 */ 178 void 179 vntblinit() 180 { 181 182 simple_lock_init(&mntvnode_slock); 183 simple_lock_init(&mntid_slock); 184 simple_lock_init(&spechash_slock); 185 simple_lock_init(&vnode_free_list_slock); 186 187 pool_init(&vnode_pool, sizeof(struct vnode), 0, 0, 0, "vnodepl", 188 0, pool_page_alloc_nointr, pool_page_free_nointr, M_VNODE); 189 } 190 191 /* 192 * Mark a mount point as busy. Used to synchronize access and to delay 193 * unmounting. Interlock is not released on failure. 194 */ 195 int 196 vfs_busy(mp, flags, interlkp) 197 struct mount *mp; 198 int flags; 199 struct simplelock *interlkp; 200 { 201 int lkflags; 202 203 if (mp->mnt_flag & MNT_UNMOUNT) { 204 if (flags & LK_NOWAIT) 205 return (ENOENT); 206 mp->mnt_flag |= MNT_MWAIT; 207 if (interlkp) 208 simple_unlock(interlkp); 209 /* 210 * Since all busy locks are shared except the exclusive 211 * lock granted when unmounting, the only place that a 212 * wakeup needs to be done is at the release of the 213 * exclusive lock at the end of dounmount. 214 */ 215 sleep((caddr_t)mp, PVFS); 216 if (interlkp) 217 simple_lock(interlkp); 218 return (ENOENT); 219 } 220 lkflags = LK_SHARED; 221 if (interlkp) 222 lkflags |= LK_INTERLOCK; 223 if (lockmgr(&mp->mnt_lock, lkflags, interlkp)) 224 panic("vfs_busy: unexpected lock failure"); 225 return (0); 226 } 227 228 /* 229 * Free a busy filesystem. 230 */ 231 void 232 vfs_unbusy(mp) 233 struct mount *mp; 234 { 235 236 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL); 237 } 238 239 /* 240 * Lookup a filesystem type, and if found allocate and initialize 241 * a mount structure for it. 242 * 243 * Devname is usually updated by mount(8) after booting. 244 */ 245 int 246 vfs_rootmountalloc(fstypename, devname, mpp) 247 char *fstypename; 248 char *devname; 249 struct mount **mpp; 250 { 251 struct vfsops *vfsp = NULL; 252 struct mount *mp; 253 254 for (vfsp = LIST_FIRST(&vfs_list); vfsp != NULL; 255 vfsp = LIST_NEXT(vfsp, vfs_list)) 256 if (!strncmp(vfsp->vfs_name, fstypename, MFSNAMELEN)) 257 break; 258 259 if (vfsp == NULL) 260 return (ENODEV); 261 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 262 memset((char *)mp, 0, (u_long)sizeof(struct mount)); 263 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 264 (void)vfs_busy(mp, LK_NOWAIT, 0); 265 LIST_INIT(&mp->mnt_vnodelist); 266 mp->mnt_op = vfsp; 267 mp->mnt_flag = MNT_RDONLY; 268 mp->mnt_vnodecovered = NULLVP; 269 vfsp->vfs_refcount++; 270 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, MFSNAMELEN); 271 mp->mnt_stat.f_mntonname[0] = '/'; 272 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0); 273 *mpp = mp; 274 return (0); 275 } 276 277 /* 278 * Lookup a mount point by filesystem identifier. 279 */ 280 struct mount * 281 vfs_getvfs(fsid) 282 fsid_t *fsid; 283 { 284 register struct mount *mp; 285 286 simple_lock(&mountlist_slock); 287 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; 288 mp = mp->mnt_list.cqe_next) { 289 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 290 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 291 simple_unlock(&mountlist_slock); 292 return (mp); 293 } 294 } 295 simple_unlock(&mountlist_slock); 296 return ((struct mount *)0); 297 } 298 299 /* 300 * Get a new unique fsid 301 */ 302 void 303 vfs_getnewfsid(mp, fstypename) 304 struct mount *mp; 305 char *fstypename; 306 { 307 static u_short xxxfs_mntid; 308 fsid_t tfsid; 309 int mtype; 310 311 simple_lock(&mntid_slock); 312 mtype = makefstype(fstypename); 313 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0); 314 mp->mnt_stat.f_fsid.val[1] = mtype; 315 if (xxxfs_mntid == 0) 316 ++xxxfs_mntid; 317 tfsid.val[0] = makedev((nblkdev + mtype) & 0xff, xxxfs_mntid); 318 tfsid.val[1] = mtype; 319 if (mountlist.cqh_first != (void *)&mountlist) { 320 while (vfs_getvfs(&tfsid)) { 321 tfsid.val[0]++; 322 xxxfs_mntid++; 323 } 324 } 325 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 326 simple_unlock(&mntid_slock); 327 } 328 329 /* 330 * Make a 'unique' number from a mount type name. 331 */ 332 long 333 makefstype(type) 334 char *type; 335 { 336 long rv; 337 338 for (rv = 0; *type; type++) { 339 rv <<= 2; 340 rv ^= *type; 341 } 342 return rv; 343 } 344 345 346 /* 347 * Set vnode attributes to VNOVAL 348 */ 349 void 350 vattr_null(vap) 351 register struct vattr *vap; 352 { 353 354 vap->va_type = VNON; 355 356 /* 357 * Assign individually so that it is safe even if size and 358 * sign of each member are varied. 359 */ 360 vap->va_mode = VNOVAL; 361 vap->va_nlink = VNOVAL; 362 vap->va_uid = VNOVAL; 363 vap->va_gid = VNOVAL; 364 vap->va_fsid = VNOVAL; 365 vap->va_fileid = VNOVAL; 366 vap->va_size = VNOVAL; 367 vap->va_blocksize = VNOVAL; 368 vap->va_atime.tv_sec = 369 vap->va_mtime.tv_sec = 370 vap->va_ctime.tv_sec = VNOVAL; 371 vap->va_atime.tv_nsec = 372 vap->va_mtime.tv_nsec = 373 vap->va_ctime.tv_nsec = VNOVAL; 374 vap->va_gen = VNOVAL; 375 vap->va_flags = VNOVAL; 376 vap->va_rdev = VNOVAL; 377 vap->va_bytes = VNOVAL; 378 vap->va_vaflags = 0; 379 } 380 381 /* 382 * Routines having to do with the management of the vnode table. 383 */ 384 extern int (**dead_vnodeop_p) __P((void *)); 385 long numvnodes; 386 387 /* 388 * Return the next vnode from the free list. 389 */ 390 int 391 getnewvnode(tag, mp, vops, vpp) 392 enum vtagtype tag; 393 struct mount *mp; 394 int (**vops) __P((void *)); 395 struct vnode **vpp; 396 { 397 struct proc *p = curproc; /* XXX */ 398 struct vnode *vp; 399 #ifdef DIAGNOSTIC 400 int s; 401 #endif 402 403 simple_lock(&vnode_free_list_slock); 404 if ((vnode_free_list.tqh_first == NULL && 405 numvnodes < 2 * desiredvnodes) || 406 numvnodes < desiredvnodes) { 407 simple_unlock(&vnode_free_list_slock); 408 vp = pool_get(&vnode_pool, PR_WAITOK); 409 memset((char *)vp, 0, sizeof(*vp)); 410 numvnodes++; 411 } else { 412 for (vp = vnode_free_list.tqh_first; 413 vp != NULLVP; vp = vp->v_freelist.tqe_next) { 414 if (simple_lock_try(&vp->v_interlock)) 415 break; 416 } 417 /* 418 * Unless this is a bad time of the month, at most 419 * the first NCPUS items on the free list are 420 * locked, so this is close enough to being empty. 421 */ 422 if (vp == NULLVP) { 423 simple_unlock(&vnode_free_list_slock); 424 tablefull("vnode"); 425 *vpp = 0; 426 return (ENFILE); 427 } 428 if (vp->v_usecount) 429 panic("free vnode isn't"); 430 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 431 /* see comment on why 0xdeadb is set at end of vgone (below) */ 432 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb; 433 simple_unlock(&vnode_free_list_slock); 434 vp->v_lease = NULL; 435 if (vp->v_type != VBAD) 436 vgonel(vp, p); 437 else 438 simple_unlock(&vp->v_interlock); 439 #ifdef DIAGNOSTIC 440 if (vp->v_data) 441 panic("cleaned vnode isn't"); 442 s = splbio(); 443 if (vp->v_numoutput) 444 panic("Clean vnode has pending I/O's"); 445 splx(s); 446 #endif 447 vp->v_flag = 0; 448 vp->v_lastr = 0; 449 vp->v_ralen = 0; 450 vp->v_maxra = 0; 451 vp->v_lastw = 0; 452 vp->v_lasta = 0; 453 vp->v_cstart = 0; 454 vp->v_clen = 0; 455 vp->v_socket = 0; 456 } 457 vp->v_type = VNON; 458 cache_purge(vp); 459 vp->v_tag = tag; 460 vp->v_op = vops; 461 insmntque(vp, mp); 462 *vpp = vp; 463 vp->v_usecount = 1; 464 vp->v_data = 0; 465 #ifdef UVM 466 simple_lock_init(&vp->v_uvm.u_obj.vmobjlock); 467 #endif 468 return (0); 469 } 470 471 /* 472 * Move a vnode from one mount queue to another. 473 */ 474 void 475 insmntque(vp, mp) 476 register struct vnode *vp; 477 register struct mount *mp; 478 { 479 480 simple_lock(&mntvnode_slock); 481 /* 482 * Delete from old mount point vnode list, if on one. 483 */ 484 if (vp->v_mount != NULL) 485 LIST_REMOVE(vp, v_mntvnodes); 486 /* 487 * Insert into list of vnodes for the new mount point, if available. 488 */ 489 if ((vp->v_mount = mp) != NULL) 490 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes); 491 simple_unlock(&mntvnode_slock); 492 } 493 494 /* 495 * Update outstanding I/O count and do wakeup if requested. 496 */ 497 void 498 vwakeup(bp) 499 register struct buf *bp; 500 { 501 register struct vnode *vp; 502 503 bp->b_flags &= ~B_WRITEINPROG; 504 if ((vp = bp->b_vp) != NULL) { 505 if (--vp->v_numoutput < 0) 506 panic("vwakeup: neg numoutput"); 507 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) { 508 vp->v_flag &= ~VBWAIT; 509 wakeup((caddr_t)&vp->v_numoutput); 510 } 511 } 512 } 513 514 /* 515 * Flush out and invalidate all buffers associated with a vnode. 516 * Called with the underlying object locked. 517 */ 518 int 519 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo) 520 register struct vnode *vp; 521 int flags; 522 struct ucred *cred; 523 struct proc *p; 524 int slpflag, slptimeo; 525 { 526 register struct buf *bp; 527 struct buf *nbp, *blist; 528 int s, error; 529 530 if (flags & V_SAVE) { 531 if ((error = VOP_FSYNC(vp, cred, FSYNC_WAIT, p)) != 0) 532 return (error); 533 if (vp->v_dirtyblkhd.lh_first != NULL) 534 panic("vinvalbuf: dirty bufs"); 535 } 536 for (;;) { 537 if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA) 538 while (blist && blist->b_lblkno < 0) 539 blist = blist->b_vnbufs.le_next; 540 if (!blist && (blist = vp->v_dirtyblkhd.lh_first) && 541 (flags & V_SAVEMETA)) 542 while (blist && blist->b_lblkno < 0) 543 blist = blist->b_vnbufs.le_next; 544 if (!blist) 545 break; 546 547 for (bp = blist; bp; bp = nbp) { 548 nbp = bp->b_vnbufs.le_next; 549 if (flags & V_SAVEMETA && bp->b_lblkno < 0) 550 continue; 551 s = splbio(); 552 if (bp->b_flags & B_BUSY) { 553 bp->b_flags |= B_WANTED; 554 error = tsleep((caddr_t)bp, 555 slpflag | (PRIBIO + 1), "vinvalbuf", 556 slptimeo); 557 splx(s); 558 if (error) 559 return (error); 560 break; 561 } 562 bp->b_flags |= B_BUSY | B_VFLUSH; 563 splx(s); 564 /* 565 * XXX Since there are no node locks for NFS, I believe 566 * there is a slight chance that a delayed write will 567 * occur while sleeping just above, so check for it. 568 */ 569 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 570 (void) VOP_BWRITE(bp); 571 break; 572 } 573 bp->b_flags |= B_INVAL; 574 brelse(bp); 575 } 576 } 577 if (!(flags & V_SAVEMETA) && 578 (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first)) 579 panic("vinvalbuf: flush failed"); 580 return (0); 581 } 582 583 void 584 vflushbuf(vp, sync) 585 register struct vnode *vp; 586 int sync; 587 { 588 register struct buf *bp, *nbp; 589 int s; 590 591 loop: 592 s = splbio(); 593 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) { 594 nbp = bp->b_vnbufs.le_next; 595 if ((bp->b_flags & B_BUSY)) 596 continue; 597 if ((bp->b_flags & B_DELWRI) == 0) 598 panic("vflushbuf: not dirty"); 599 bp->b_flags |= B_BUSY | B_VFLUSH; 600 splx(s); 601 /* 602 * Wait for I/O associated with indirect blocks to complete, 603 * since there is no way to quickly wait for them below. 604 */ 605 if (bp->b_vp == vp || sync == 0) 606 (void) bawrite(bp); 607 else 608 (void) bwrite(bp); 609 goto loop; 610 } 611 if (sync == 0) { 612 splx(s); 613 return; 614 } 615 while (vp->v_numoutput) { 616 vp->v_flag |= VBWAIT; 617 tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "vflushbuf", 0); 618 } 619 splx(s); 620 if (vp->v_dirtyblkhd.lh_first != NULL) { 621 vprint("vflushbuf: dirty", vp); 622 goto loop; 623 } 624 } 625 626 /* 627 * Associate a buffer with a vnode. 628 */ 629 void 630 bgetvp(vp, bp) 631 register struct vnode *vp; 632 register struct buf *bp; 633 { 634 635 if (bp->b_vp) 636 panic("bgetvp: not free"); 637 VHOLD(vp); 638 bp->b_vp = vp; 639 if (vp->v_type == VBLK || vp->v_type == VCHR) 640 bp->b_dev = vp->v_rdev; 641 else 642 bp->b_dev = NODEV; 643 /* 644 * Insert onto list for new vnode. 645 */ 646 bufinsvn(bp, &vp->v_cleanblkhd); 647 } 648 649 /* 650 * Disassociate a buffer from a vnode. 651 */ 652 void 653 brelvp(bp) 654 register struct buf *bp; 655 { 656 struct vnode *vp; 657 658 if (bp->b_vp == (struct vnode *) 0) 659 panic("brelvp: NULL"); 660 /* 661 * Delete from old vnode list, if on one. 662 */ 663 if (bp->b_vnbufs.le_next != NOLIST) 664 bufremvn(bp); 665 vp = bp->b_vp; 666 bp->b_vp = (struct vnode *) 0; 667 HOLDRELE(vp); 668 } 669 670 /* 671 * Reassign a buffer from one vnode to another. 672 * Used to assign file specific control information 673 * (indirect blocks) to the vnode to which they belong. 674 */ 675 void 676 reassignbuf(bp, newvp) 677 register struct buf *bp; 678 register struct vnode *newvp; 679 { 680 register struct buflists *listheadp; 681 682 if (newvp == NULL) { 683 printf("reassignbuf: NULL"); 684 return; 685 } 686 /* 687 * Delete from old vnode list, if on one. 688 */ 689 if (bp->b_vnbufs.le_next != NOLIST) 690 bufremvn(bp); 691 /* 692 * If dirty, put on list of dirty buffers; 693 * otherwise insert onto list of clean buffers. 694 */ 695 if (bp->b_flags & B_DELWRI) 696 listheadp = &newvp->v_dirtyblkhd; 697 else 698 listheadp = &newvp->v_cleanblkhd; 699 bufinsvn(bp, listheadp); 700 } 701 702 /* 703 * Create a vnode for a block device. 704 * Used for root filesystem and swap areas. 705 * Also used for memory file system special devices. 706 */ 707 int 708 bdevvp(dev, vpp) 709 dev_t dev; 710 struct vnode **vpp; 711 { 712 713 return (getdevvp(dev, vpp, VBLK)); 714 } 715 716 /* 717 * Create a vnode for a character device. 718 * Used for kernfs and some console handling. 719 */ 720 int 721 cdevvp(dev, vpp) 722 dev_t dev; 723 struct vnode **vpp; 724 { 725 726 return (getdevvp(dev, vpp, VCHR)); 727 } 728 729 /* 730 * Create a vnode for a device. 731 * Used by bdevvp (block device) for root file system etc., 732 * and by cdevvp (character device) for console and kernfs. 733 */ 734 int 735 getdevvp(dev, vpp, type) 736 dev_t dev; 737 struct vnode **vpp; 738 enum vtype type; 739 { 740 register struct vnode *vp; 741 struct vnode *nvp; 742 int error; 743 744 if (dev == NODEV) { 745 *vpp = NULLVP; 746 return (0); 747 } 748 error = getnewvnode(VT_NON, NULL, spec_vnodeop_p, &nvp); 749 if (error) { 750 *vpp = NULLVP; 751 return (error); 752 } 753 vp = nvp; 754 vp->v_type = type; 755 if ((nvp = checkalias(vp, dev, NULL)) != 0) { 756 vput(vp); 757 vp = nvp; 758 } 759 *vpp = vp; 760 return (0); 761 } 762 763 /* 764 * Check to see if the new vnode represents a special device 765 * for which we already have a vnode (either because of 766 * bdevvp() or because of a different vnode representing 767 * the same block device). If such an alias exists, deallocate 768 * the existing contents and return the aliased vnode. The 769 * caller is responsible for filling it with its new contents. 770 */ 771 struct vnode * 772 checkalias(nvp, nvp_rdev, mp) 773 register struct vnode *nvp; 774 dev_t nvp_rdev; 775 struct mount *mp; 776 { 777 struct proc *p = curproc; /* XXX */ 778 register struct vnode *vp; 779 struct vnode **vpp; 780 781 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 782 return (NULLVP); 783 784 vpp = &speclisth[SPECHASH(nvp_rdev)]; 785 loop: 786 simple_lock(&spechash_slock); 787 for (vp = *vpp; vp; vp = vp->v_specnext) { 788 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) 789 continue; 790 /* 791 * Alias, but not in use, so flush it out. 792 */ 793 simple_lock(&vp->v_interlock); 794 if (vp->v_usecount == 0) { 795 simple_unlock(&spechash_slock); 796 vgonel(vp, p); 797 goto loop; 798 } 799 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) { 800 simple_unlock(&spechash_slock); 801 goto loop; 802 } 803 break; 804 } 805 if (vp == NULL || vp->v_tag != VT_NON || vp->v_type != VBLK) { 806 MALLOC(nvp->v_specinfo, struct specinfo *, 807 sizeof(struct specinfo), M_VNODE, M_WAITOK); 808 nvp->v_rdev = nvp_rdev; 809 nvp->v_hashchain = vpp; 810 nvp->v_specnext = *vpp; 811 nvp->v_specflags = 0; 812 simple_unlock(&spechash_slock); 813 nvp->v_speclockf = NULL; 814 *vpp = nvp; 815 if (vp != NULLVP) { 816 nvp->v_flag |= VALIASED; 817 vp->v_flag |= VALIASED; 818 vput(vp); 819 } 820 return (NULLVP); 821 } 822 simple_unlock(&spechash_slock); 823 VOP_UNLOCK(vp, 0); 824 simple_lock(&vp->v_interlock); 825 vclean(vp, 0, p); 826 vp->v_op = nvp->v_op; 827 vp->v_tag = nvp->v_tag; 828 nvp->v_type = VNON; 829 insmntque(vp, mp); 830 return (vp); 831 } 832 833 /* 834 * Grab a particular vnode from the free list, increment its 835 * reference count and lock it. If the vnode lock bit is set the 836 * vnode is being eliminated in vgone. In that case, we can not 837 * grab the vnode, so the process is awakened when the transition is 838 * completed, and an error returned to indicate that the vnode is no 839 * longer usable (possibly having been changed to a new file system type). 840 */ 841 int 842 vget(vp, flags) 843 struct vnode *vp; 844 int flags; 845 { 846 int error; 847 848 /* 849 * If the vnode is in the process of being cleaned out for 850 * another use, we wait for the cleaning to finish and then 851 * return failure. Cleaning is determined by checking that 852 * the VXLOCK flag is set. 853 */ 854 if ((flags & LK_INTERLOCK) == 0) 855 simple_lock(&vp->v_interlock); 856 if (vp->v_flag & VXLOCK) { 857 vp->v_flag |= VXWANT; 858 simple_unlock(&vp->v_interlock); 859 tsleep((caddr_t)vp, PINOD, "vget", 0); 860 return (ENOENT); 861 } 862 if (vp->v_usecount == 0) { 863 simple_lock(&vnode_free_list_slock); 864 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 865 simple_unlock(&vnode_free_list_slock); 866 } 867 vp->v_usecount++; 868 if (flags & LK_TYPE_MASK) { 869 if ((error = vn_lock(vp, flags | LK_INTERLOCK))) 870 vrele(vp); 871 return (error); 872 } 873 simple_unlock(&vp->v_interlock); 874 return (0); 875 } 876 877 /* 878 * vput(), just unlock and vrele() 879 */ 880 void 881 vput(vp) 882 struct vnode *vp; 883 { 884 struct proc *p = curproc; /* XXX */ 885 886 #ifdef DIGANOSTIC 887 if (vp == NULL) 888 panic("vput: null vp"); 889 #endif 890 simple_lock(&vp->v_interlock); 891 vp->v_usecount--; 892 if (vp->v_usecount > 0) { 893 simple_unlock(&vp->v_interlock); 894 VOP_UNLOCK(vp, 0); 895 return; 896 } 897 #ifdef DIAGNOSTIC 898 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 899 vprint("vput: bad ref count", vp); 900 panic("vput: ref cnt"); 901 } 902 #endif 903 /* 904 * Insert at tail of LRU list. 905 */ 906 simple_lock(&vnode_free_list_slock); 907 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 908 simple_unlock(&vnode_free_list_slock); 909 simple_unlock(&vp->v_interlock); 910 VOP_INACTIVE(vp, p); 911 } 912 913 /* 914 * Vnode release. 915 * If count drops to zero, call inactive routine and return to freelist. 916 */ 917 void 918 vrele(vp) 919 struct vnode *vp; 920 { 921 struct proc *p = curproc; /* XXX */ 922 923 #ifdef DIAGNOSTIC 924 if (vp == NULL) 925 panic("vrele: null vp"); 926 #endif 927 simple_lock(&vp->v_interlock); 928 vp->v_usecount--; 929 if (vp->v_usecount > 0) { 930 simple_unlock(&vp->v_interlock); 931 return; 932 } 933 #ifdef DIAGNOSTIC 934 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 935 vprint("vrele: bad ref count", vp); 936 panic("vrele: ref cnt"); 937 } 938 #endif 939 /* 940 * Insert at tail of LRU list. 941 */ 942 simple_lock(&vnode_free_list_slock); 943 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 944 simple_unlock(&vnode_free_list_slock); 945 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) 946 VOP_INACTIVE(vp, p); 947 } 948 949 #ifdef DIAGNOSTIC 950 /* 951 * Page or buffer structure gets a reference. 952 */ 953 void 954 vhold(vp) 955 register struct vnode *vp; 956 { 957 958 simple_lock(&vp->v_interlock); 959 vp->v_holdcnt++; 960 simple_unlock(&vp->v_interlock); 961 } 962 963 /* 964 * Page or buffer structure frees a reference. 965 */ 966 void 967 holdrele(vp) 968 register struct vnode *vp; 969 { 970 971 simple_lock(&vp->v_interlock); 972 if (vp->v_holdcnt <= 0) 973 panic("holdrele: holdcnt"); 974 vp->v_holdcnt--; 975 simple_unlock(&vp->v_interlock); 976 } 977 978 /* 979 * Vnode reference. 980 */ 981 void 982 vref(vp) 983 struct vnode *vp; 984 { 985 986 simple_lock(&vp->v_interlock); 987 if (vp->v_usecount <= 0) 988 panic("vref used where vget required"); 989 vp->v_usecount++; 990 simple_unlock(&vp->v_interlock); 991 } 992 #endif /* DIAGNOSTIC */ 993 994 /* 995 * Remove any vnodes in the vnode table belonging to mount point mp. 996 * 997 * If MNT_NOFORCE is specified, there should not be any active ones, 998 * return error if any are found (nb: this is a user error, not a 999 * system error). If MNT_FORCE is specified, detach any active vnodes 1000 * that are found. 1001 */ 1002 #ifdef DEBUG 1003 int busyprt = 0; /* print out busy vnodes */ 1004 struct ctldebug debug1 = { "busyprt", &busyprt }; 1005 #endif 1006 1007 int 1008 vflush(mp, skipvp, flags) 1009 struct mount *mp; 1010 struct vnode *skipvp; 1011 int flags; 1012 { 1013 struct proc *p = curproc; /* XXX */ 1014 register struct vnode *vp, *nvp; 1015 int busy = 0; 1016 1017 simple_lock(&mntvnode_slock); 1018 loop: 1019 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 1020 if (vp->v_mount != mp) 1021 goto loop; 1022 nvp = vp->v_mntvnodes.le_next; 1023 /* 1024 * Skip over a selected vnode. 1025 */ 1026 if (vp == skipvp) 1027 continue; 1028 simple_lock(&vp->v_interlock); 1029 /* 1030 * Skip over a vnodes marked VSYSTEM. 1031 */ 1032 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 1033 simple_unlock(&vp->v_interlock); 1034 continue; 1035 } 1036 /* 1037 * If WRITECLOSE is set, only flush out regular file 1038 * vnodes open for writing. 1039 */ 1040 if ((flags & WRITECLOSE) && 1041 (vp->v_writecount == 0 || vp->v_type != VREG)) { 1042 simple_unlock(&vp->v_interlock); 1043 continue; 1044 } 1045 /* 1046 * With v_usecount == 0, all we need to do is clear 1047 * out the vnode data structures and we are done. 1048 */ 1049 if (vp->v_usecount == 0) { 1050 simple_unlock(&mntvnode_slock); 1051 vgonel(vp, p); 1052 simple_lock(&mntvnode_slock); 1053 continue; 1054 } 1055 /* 1056 * If FORCECLOSE is set, forcibly close the vnode. 1057 * For block or character devices, revert to an 1058 * anonymous device. For all other files, just kill them. 1059 */ 1060 if (flags & FORCECLOSE) { 1061 simple_unlock(&mntvnode_slock); 1062 if (vp->v_type != VBLK && vp->v_type != VCHR) { 1063 vgonel(vp, p); 1064 } else { 1065 vclean(vp, 0, p); 1066 vp->v_op = spec_vnodeop_p; 1067 insmntque(vp, (struct mount *)0); 1068 } 1069 simple_lock(&mntvnode_slock); 1070 continue; 1071 } 1072 #ifdef DEBUG 1073 if (busyprt) 1074 vprint("vflush: busy vnode", vp); 1075 #endif 1076 simple_unlock(&vp->v_interlock); 1077 busy++; 1078 } 1079 simple_unlock(&mntvnode_slock); 1080 if (busy) 1081 return (EBUSY); 1082 return (0); 1083 } 1084 1085 /* 1086 * Disassociate the underlying file system from a vnode. 1087 */ 1088 void 1089 vclean(vp, flags, p) 1090 register struct vnode *vp; 1091 int flags; 1092 struct proc *p; 1093 { 1094 int active; 1095 1096 /* 1097 * Check to see if the vnode is in use. 1098 * If so we have to reference it before we clean it out 1099 * so that its count cannot fall to zero and generate a 1100 * race against ourselves to recycle it. 1101 */ 1102 if ((active = vp->v_usecount) != 0) 1103 /* We have the vnode interlock. */ 1104 vp->v_usecount++; 1105 1106 /* 1107 * Prevent the vnode from being recycled or 1108 * brought into use while we clean it out. 1109 */ 1110 if (vp->v_flag & VXLOCK) 1111 panic("vclean: deadlock"); 1112 vp->v_flag |= VXLOCK; 1113 #ifdef UVM 1114 /* 1115 * clean out any VM data associated with the vnode. 1116 */ 1117 uvm_vnp_terminate(vp); 1118 #endif 1119 /* 1120 * Even if the count is zero, the VOP_INACTIVE routine may still 1121 * have the object locked while it cleans it out. The VOP_LOCK 1122 * ensures that the VOP_INACTIVE routine is done with its work. 1123 * For active vnodes, it ensures that no other activity can 1124 * occur while the underlying object is being cleaned out. 1125 */ 1126 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK); 1127 1128 /* 1129 * Clean out any buffers associated with the vnode. 1130 */ 1131 if (flags & DOCLOSE) 1132 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0); 1133 1134 /* 1135 * If purging an active vnode, it must be closed and 1136 * deactivated before being reclaimed. Note that the 1137 * VOP_INACTIVE will unlock the vnode. 1138 */ 1139 if (active) { 1140 if (flags & DOCLOSE) 1141 VOP_CLOSE(vp, FNONBLOCK, NOCRED, NULL); 1142 VOP_INACTIVE(vp, p); 1143 } else { 1144 /* 1145 * Any other processes trying to obtain this lock must first 1146 * wait for VXLOCK to clear, then call the new lock operation. 1147 */ 1148 VOP_UNLOCK(vp, 0); 1149 } 1150 /* 1151 * Reclaim the vnode. 1152 */ 1153 if (VOP_RECLAIM(vp, p)) 1154 panic("vclean: cannot reclaim"); 1155 1156 if (active) { 1157 /* 1158 * Inline copy of vrele() since VOP_INACTIVE 1159 * has already been called. 1160 */ 1161 simple_lock(&vp->v_interlock); 1162 if (--vp->v_usecount <= 0) { 1163 #ifdef DIAGNOSTIC 1164 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 1165 vprint("vclean: bad ref count", vp); 1166 panic("vclean: ref cnt"); 1167 } 1168 #endif 1169 /* 1170 * Insert at tail of LRU list. 1171 */ 1172 simple_lock(&vnode_free_list_slock); 1173 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 1174 simple_unlock(&vnode_free_list_slock); 1175 } 1176 simple_unlock(&vp->v_interlock); 1177 } 1178 1179 cache_purge(vp); 1180 if (vp->v_vnlock) { 1181 if ((vp->v_vnlock->lk_flags & LK_DRAINED) == 0) 1182 vprint("vclean: lock not drained", vp); 1183 FREE(vp->v_vnlock, M_VNODE); 1184 vp->v_vnlock = NULL; 1185 } 1186 1187 /* 1188 * Done with purge, notify sleepers of the grim news. 1189 */ 1190 vp->v_op = dead_vnodeop_p; 1191 vp->v_tag = VT_NON; 1192 vp->v_flag &= ~VXLOCK; 1193 if (vp->v_flag & VXWANT) { 1194 vp->v_flag &= ~VXWANT; 1195 wakeup((caddr_t)vp); 1196 } 1197 } 1198 1199 /* 1200 * Recycle an unused vnode to the front of the free list. 1201 * Release the passed interlock if the vnode will be recycled. 1202 */ 1203 int 1204 vrecycle(vp, inter_lkp, p) 1205 struct vnode *vp; 1206 struct simplelock *inter_lkp; 1207 struct proc *p; 1208 { 1209 1210 simple_lock(&vp->v_interlock); 1211 if (vp->v_usecount == 0) { 1212 if (inter_lkp) 1213 simple_unlock(inter_lkp); 1214 vgonel(vp, p); 1215 return (1); 1216 } 1217 simple_unlock(&vp->v_interlock); 1218 return (0); 1219 } 1220 1221 /* 1222 * Eliminate all activity associated with a vnode 1223 * in preparation for reuse. 1224 */ 1225 void 1226 vgone(vp) 1227 struct vnode *vp; 1228 { 1229 struct proc *p = curproc; /* XXX */ 1230 1231 simple_lock(&vp->v_interlock); 1232 vgonel(vp, p); 1233 } 1234 1235 /* 1236 * vgone, with the vp interlock held. 1237 */ 1238 void 1239 vgonel(vp, p) 1240 register struct vnode *vp; 1241 struct proc *p; 1242 { 1243 struct vnode *vq; 1244 struct vnode *vx; 1245 1246 /* 1247 * If a vgone (or vclean) is already in progress, 1248 * wait until it is done and return. 1249 */ 1250 if (vp->v_flag & VXLOCK) { 1251 vp->v_flag |= VXWANT; 1252 simple_unlock(&vp->v_interlock); 1253 tsleep((caddr_t)vp, PINOD, "vgone", 0); 1254 return; 1255 } 1256 /* 1257 * Clean out the filesystem specific data. 1258 */ 1259 vclean(vp, DOCLOSE, p); 1260 /* 1261 * Delete from old mount point vnode list, if on one. 1262 */ 1263 if (vp->v_mount != NULL) 1264 insmntque(vp, (struct mount *)0); 1265 /* 1266 * If special device, remove it from special device alias list. 1267 * if it is on one. 1268 */ 1269 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) { 1270 simple_lock(&spechash_slock); 1271 if (*vp->v_hashchain == vp) { 1272 *vp->v_hashchain = vp->v_specnext; 1273 } else { 1274 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1275 if (vq->v_specnext != vp) 1276 continue; 1277 vq->v_specnext = vp->v_specnext; 1278 break; 1279 } 1280 if (vq == NULL) 1281 panic("missing bdev"); 1282 } 1283 if (vp->v_flag & VALIASED) { 1284 vx = NULL; 1285 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1286 if (vq->v_rdev != vp->v_rdev || 1287 vq->v_type != vp->v_type) 1288 continue; 1289 if (vx) 1290 break; 1291 vx = vq; 1292 } 1293 if (vx == NULL) 1294 panic("missing alias"); 1295 if (vq == NULL) 1296 vx->v_flag &= ~VALIASED; 1297 vp->v_flag &= ~VALIASED; 1298 } 1299 simple_unlock(&spechash_slock); 1300 FREE(vp->v_specinfo, M_VNODE); 1301 vp->v_specinfo = NULL; 1302 } 1303 /* 1304 * If it is on the freelist and not already at the head, 1305 * move it to the head of the list. The test of the back 1306 * pointer and the reference count of zero is because 1307 * it will be removed from the free list by getnewvnode, 1308 * but will not have its reference count incremented until 1309 * after calling vgone. If the reference count were 1310 * incremented first, vgone would (incorrectly) try to 1311 * close the previous instance of the underlying object. 1312 * So, the back pointer is explicitly set to `0xdeadb' in 1313 * getnewvnode after removing it from the freelist to ensure 1314 * that we do not try to move it here. 1315 */ 1316 if (vp->v_usecount == 0) { 1317 simple_lock(&vnode_free_list_slock); 1318 if (vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb && 1319 vnode_free_list.tqh_first != vp) { 1320 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1321 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1322 } 1323 simple_unlock(&vnode_free_list_slock); 1324 } 1325 vp->v_type = VBAD; 1326 } 1327 1328 /* 1329 * Lookup a vnode by device number. 1330 */ 1331 int 1332 vfinddev(dev, type, vpp) 1333 dev_t dev; 1334 enum vtype type; 1335 struct vnode **vpp; 1336 { 1337 struct vnode *vp; 1338 int rc = 0; 1339 1340 simple_lock(&spechash_slock); 1341 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 1342 if (dev != vp->v_rdev || type != vp->v_type) 1343 continue; 1344 *vpp = vp; 1345 rc = 1; 1346 break; 1347 } 1348 simple_unlock(&spechash_slock); 1349 return (rc); 1350 } 1351 1352 /* 1353 * Calculate the total number of references to a special device. 1354 */ 1355 int 1356 vcount(vp) 1357 register struct vnode *vp; 1358 { 1359 register struct vnode *vq, *vnext; 1360 int count; 1361 1362 loop: 1363 if ((vp->v_flag & VALIASED) == 0) 1364 return (vp->v_usecount); 1365 simple_lock(&spechash_slock); 1366 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) { 1367 vnext = vq->v_specnext; 1368 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 1369 continue; 1370 /* 1371 * Alias, but not in use, so flush it out. 1372 */ 1373 if (vq->v_usecount == 0 && vq != vp) { 1374 simple_unlock(&spechash_slock); 1375 vgone(vq); 1376 goto loop; 1377 } 1378 count += vq->v_usecount; 1379 } 1380 simple_unlock(&spechash_slock); 1381 return (count); 1382 } 1383 1384 /* 1385 * Print out a description of a vnode. 1386 */ 1387 static char *typename[] = 1388 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" }; 1389 1390 void 1391 vprint(label, vp) 1392 char *label; 1393 register struct vnode *vp; 1394 { 1395 char buf[64]; 1396 1397 if (label != NULL) 1398 printf("%s: ", label); 1399 printf("type %s, usecount %d, writecount %d, refcount %ld,", 1400 typename[vp->v_type], vp->v_usecount, vp->v_writecount, 1401 vp->v_holdcnt); 1402 buf[0] = '\0'; 1403 if (vp->v_flag & VROOT) 1404 strcat(buf, "|VROOT"); 1405 if (vp->v_flag & VTEXT) 1406 strcat(buf, "|VTEXT"); 1407 if (vp->v_flag & VSYSTEM) 1408 strcat(buf, "|VSYSTEM"); 1409 if (vp->v_flag & VXLOCK) 1410 strcat(buf, "|VXLOCK"); 1411 if (vp->v_flag & VXWANT) 1412 strcat(buf, "|VXWANT"); 1413 if (vp->v_flag & VBWAIT) 1414 strcat(buf, "|VBWAIT"); 1415 if (vp->v_flag & VALIASED) 1416 strcat(buf, "|VALIASED"); 1417 if (buf[0] != '\0') 1418 printf(" flags (%s)", &buf[1]); 1419 if (vp->v_data == NULL) { 1420 printf("\n"); 1421 } else { 1422 printf("\n\t"); 1423 VOP_PRINT(vp); 1424 } 1425 } 1426 1427 #ifdef DEBUG 1428 /* 1429 * List all of the locked vnodes in the system. 1430 * Called when debugging the kernel. 1431 */ 1432 void 1433 printlockedvnodes() 1434 { 1435 struct mount *mp, *nmp; 1436 struct vnode *vp; 1437 1438 printf("Locked vnodes\n"); 1439 simple_lock(&mountlist_slock); 1440 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { 1441 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 1442 nmp = mp->mnt_list.cqe_next; 1443 continue; 1444 } 1445 for (vp = mp->mnt_vnodelist.lh_first; 1446 vp != NULL; 1447 vp = vp->v_mntvnodes.le_next) { 1448 if (VOP_ISLOCKED(vp)) 1449 vprint((char *)0, vp); 1450 } 1451 simple_lock(&mountlist_slock); 1452 nmp = mp->mnt_list.cqe_next; 1453 vfs_unbusy(mp); 1454 } 1455 simple_unlock(&mountlist_slock); 1456 } 1457 #endif 1458 1459 extern const char *mountcompatnames[]; 1460 extern const int nmountcompatnames; 1461 1462 /* 1463 * Top level filesystem related information gathering. 1464 */ 1465 int 1466 vfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 1467 int *name; 1468 u_int namelen; 1469 void *oldp; 1470 size_t *oldlenp; 1471 void *newp; 1472 size_t newlen; 1473 struct proc *p; 1474 { 1475 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44) 1476 struct vfsconf vfc; 1477 #endif 1478 struct vfsops *vfsp; 1479 1480 /* all sysctl names at this level are at least name and field */ 1481 if (namelen < 2) 1482 return (ENOTDIR); /* overloaded */ 1483 1484 /* Not generic: goes to file system. */ 1485 if (name[0] != VFS_GENERIC) { 1486 if (name[0] >= nmountcompatnames || name[0] < 0 || 1487 mountcompatnames[name[0]] == NULL) 1488 return (EOPNOTSUPP); 1489 vfsp = vfs_getopsbyname(mountcompatnames[name[0]]); 1490 if (vfsp == NULL || vfsp->vfs_sysctl == NULL) 1491 return (EOPNOTSUPP); 1492 return ((*vfsp->vfs_sysctl)(&name[1], namelen - 1, 1493 oldp, oldlenp, newp, newlen, p)); 1494 } 1495 1496 /* The rest are generic vfs sysctls. */ 1497 switch (name[1]) { 1498 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44) 1499 case VFS_MAXTYPENUM: 1500 /* 1501 * Provided for 4.4BSD-Lite2 compatibility. 1502 */ 1503 return (sysctl_rdint(oldp, oldlenp, newp, nmountcompatnames)); 1504 case VFS_CONF: 1505 /* 1506 * Special: a node, next is a file system name. 1507 * Provided for 4.4BSD-Lite2 compatibility. 1508 */ 1509 if (namelen < 3) 1510 return (ENOTDIR); /* overloaded */ 1511 if (name[2] >= nmountcompatnames || name[2] < 0 || 1512 mountcompatnames[name[2]] == NULL) 1513 return (EOPNOTSUPP); 1514 vfsp = vfs_getopsbyname(mountcompatnames[name[2]]); 1515 if (vfsp == NULL) 1516 return (EOPNOTSUPP); 1517 vfc.vfc_vfsops = vfsp; 1518 strncpy(vfc.vfc_name, vfsp->vfs_name, MFSNAMELEN); 1519 vfc.vfc_typenum = name[2]; 1520 vfc.vfc_refcount = vfsp->vfs_refcount; 1521 vfc.vfc_flags = 0; 1522 vfc.vfc_mountroot = vfsp->vfs_mountroot; 1523 vfc.vfc_next = NULL; 1524 return (sysctl_rdstruct(oldp, oldlenp, newp, &vfc, 1525 sizeof(struct vfsconf))); 1526 #endif 1527 default: 1528 break; 1529 } 1530 return (EOPNOTSUPP); 1531 } 1532 1533 int kinfo_vdebug = 1; 1534 int kinfo_vgetfailed; 1535 #define KINFO_VNODESLOP 10 1536 /* 1537 * Dump vnode list (via sysctl). 1538 * Copyout address of vnode followed by vnode. 1539 */ 1540 /* ARGSUSED */ 1541 int 1542 sysctl_vnode(where, sizep, p) 1543 char *where; 1544 size_t *sizep; 1545 struct proc *p; 1546 { 1547 struct mount *mp, *nmp; 1548 struct vnode *nvp, *vp; 1549 char *bp = where, *savebp; 1550 char *ewhere; 1551 int error; 1552 1553 #define VPTRSZ sizeof(struct vnode *) 1554 #define VNODESZ sizeof(struct vnode) 1555 if (where == NULL) { 1556 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ); 1557 return (0); 1558 } 1559 ewhere = where + *sizep; 1560 1561 simple_lock(&mountlist_slock); 1562 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { 1563 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 1564 nmp = mp->mnt_list.cqe_next; 1565 continue; 1566 } 1567 savebp = bp; 1568 again: 1569 simple_lock(&mntvnode_slock); 1570 for (vp = mp->mnt_vnodelist.lh_first; 1571 vp != NULL; 1572 vp = nvp) { 1573 /* 1574 * Check that the vp is still associated with 1575 * this filesystem. RACE: could have been 1576 * recycled onto the same filesystem. 1577 */ 1578 if (vp->v_mount != mp) { 1579 simple_unlock(&mntvnode_slock); 1580 if (kinfo_vdebug) 1581 printf("kinfo: vp changed\n"); 1582 bp = savebp; 1583 goto again; 1584 } 1585 nvp = vp->v_mntvnodes.le_next; 1586 if (bp + VPTRSZ + VNODESZ > ewhere) { 1587 simple_unlock(&mntvnode_slock); 1588 *sizep = bp - where; 1589 return (ENOMEM); 1590 } 1591 simple_unlock(&mntvnode_slock); 1592 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) || 1593 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ))) 1594 return (error); 1595 bp += VPTRSZ + VNODESZ; 1596 simple_lock(&mntvnode_slock); 1597 } 1598 simple_unlock(&mntvnode_slock); 1599 simple_lock(&mountlist_slock); 1600 nmp = mp->mnt_list.cqe_next; 1601 vfs_unbusy(mp); 1602 } 1603 simple_unlock(&mountlist_slock); 1604 1605 *sizep = bp - where; 1606 return (0); 1607 } 1608 1609 /* 1610 * Check to see if a filesystem is mounted on a block device. 1611 */ 1612 int 1613 vfs_mountedon(vp) 1614 struct vnode *vp; 1615 { 1616 struct vnode *vq; 1617 int error = 0; 1618 1619 if (vp->v_specflags & SI_MOUNTEDON) 1620 return (EBUSY); 1621 if (vp->v_flag & VALIASED) { 1622 simple_lock(&spechash_slock); 1623 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1624 if (vq->v_rdev != vp->v_rdev || 1625 vq->v_type != vp->v_type) 1626 continue; 1627 if (vq->v_specflags & SI_MOUNTEDON) { 1628 error = EBUSY; 1629 break; 1630 } 1631 } 1632 simple_unlock(&spechash_slock); 1633 } 1634 return (error); 1635 } 1636 1637 /* 1638 * Build hash lists of net addresses and hang them off the mount point. 1639 * Called by ufs_mount() to set up the lists of export addresses. 1640 */ 1641 static int 1642 vfs_hang_addrlist(mp, nep, argp) 1643 struct mount *mp; 1644 struct netexport *nep; 1645 struct export_args *argp; 1646 { 1647 register struct netcred *np, *enp; 1648 register struct radix_node_head *rnh; 1649 register int i; 1650 struct radix_node *rn; 1651 struct sockaddr *saddr, *smask = 0; 1652 struct domain *dom; 1653 int error; 1654 1655 if (argp->ex_addrlen == 0) { 1656 if (mp->mnt_flag & MNT_DEFEXPORTED) 1657 return (EPERM); 1658 np = &nep->ne_defexported; 1659 np->netc_exflags = argp->ex_flags; 1660 np->netc_anon = argp->ex_anon; 1661 np->netc_anon.cr_ref = 1; 1662 mp->mnt_flag |= MNT_DEFEXPORTED; 1663 return (0); 1664 } 1665 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1666 np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK); 1667 memset((caddr_t)np, 0, i); 1668 saddr = (struct sockaddr *)(np + 1); 1669 error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen); 1670 if (error) 1671 goto out; 1672 if (saddr->sa_len > argp->ex_addrlen) 1673 saddr->sa_len = argp->ex_addrlen; 1674 if (argp->ex_masklen) { 1675 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 1676 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen); 1677 if (error) 1678 goto out; 1679 if (smask->sa_len > argp->ex_masklen) 1680 smask->sa_len = argp->ex_masklen; 1681 } 1682 i = saddr->sa_family; 1683 if ((rnh = nep->ne_rtable[i]) == 0) { 1684 /* 1685 * Seems silly to initialize every AF when most are not 1686 * used, do so on demand here 1687 */ 1688 for (dom = domains; dom; dom = dom->dom_next) 1689 if (dom->dom_family == i && dom->dom_rtattach) { 1690 dom->dom_rtattach((void **)&nep->ne_rtable[i], 1691 dom->dom_rtoffset); 1692 break; 1693 } 1694 if ((rnh = nep->ne_rtable[i]) == 0) { 1695 error = ENOBUFS; 1696 goto out; 1697 } 1698 } 1699 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, 1700 np->netc_rnodes); 1701 if (rn == 0 || np != (struct netcred *)rn) { /* already exists */ 1702 if (rn == 0) { 1703 enp = (struct netcred *)(*rnh->rnh_lookup)(saddr, 1704 smask, rnh); 1705 if (enp == 0) { 1706 error = EPERM; 1707 goto out; 1708 } 1709 } else 1710 enp = (struct netcred *)rn; 1711 1712 if (enp->netc_exflags != argp->ex_flags || 1713 enp->netc_anon.cr_uid != argp->ex_anon.cr_uid || 1714 enp->netc_anon.cr_gid != argp->ex_anon.cr_gid || 1715 enp->netc_anon.cr_ngroups != argp->ex_anon.cr_ngroups || 1716 memcmp(&enp->netc_anon.cr_groups, &argp->ex_anon.cr_groups, 1717 enp->netc_anon.cr_ngroups)) 1718 error = EPERM; 1719 else 1720 error = 0; 1721 goto out; 1722 } 1723 np->netc_exflags = argp->ex_flags; 1724 np->netc_anon = argp->ex_anon; 1725 np->netc_anon.cr_ref = 1; 1726 return (0); 1727 out: 1728 free(np, M_NETADDR); 1729 return (error); 1730 } 1731 1732 /* ARGSUSED */ 1733 static int 1734 vfs_free_netcred(rn, w) 1735 struct radix_node *rn; 1736 void *w; 1737 { 1738 register struct radix_node_head *rnh = (struct radix_node_head *)w; 1739 1740 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh); 1741 free((caddr_t)rn, M_NETADDR); 1742 return (0); 1743 } 1744 1745 /* 1746 * Free the net address hash lists that are hanging off the mount points. 1747 */ 1748 static void 1749 vfs_free_addrlist(nep) 1750 struct netexport *nep; 1751 { 1752 register int i; 1753 register struct radix_node_head *rnh; 1754 1755 for (i = 0; i <= AF_MAX; i++) 1756 if ((rnh = nep->ne_rtable[i]) != NULL) { 1757 (*rnh->rnh_walktree)(rnh, vfs_free_netcred, rnh); 1758 free((caddr_t)rnh, M_RTABLE); 1759 nep->ne_rtable[i] = 0; 1760 } 1761 } 1762 1763 int 1764 vfs_export(mp, nep, argp) 1765 struct mount *mp; 1766 struct netexport *nep; 1767 struct export_args *argp; 1768 { 1769 int error; 1770 1771 if (argp->ex_flags & MNT_DELEXPORT) { 1772 if (mp->mnt_flag & MNT_EXPUBLIC) { 1773 vfs_setpublicfs(NULL, NULL, NULL); 1774 mp->mnt_flag &= ~MNT_EXPUBLIC; 1775 } 1776 vfs_free_addrlist(nep); 1777 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 1778 } 1779 if (argp->ex_flags & MNT_EXPORTED) { 1780 if (argp->ex_flags & MNT_EXPUBLIC) { 1781 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 1782 return (error); 1783 mp->mnt_flag |= MNT_EXPUBLIC; 1784 } 1785 if ((error = vfs_hang_addrlist(mp, nep, argp)) != 0) 1786 return (error); 1787 mp->mnt_flag |= MNT_EXPORTED; 1788 } 1789 return (0); 1790 } 1791 1792 /* 1793 * Set the publicly exported filesystem (WebNFS). Currently, only 1794 * one public filesystem is possible in the spec (RFC 2054 and 2055) 1795 */ 1796 int 1797 vfs_setpublicfs(mp, nep, argp) 1798 struct mount *mp; 1799 struct netexport *nep; 1800 struct export_args *argp; 1801 { 1802 int error; 1803 struct vnode *rvp; 1804 char *cp; 1805 1806 /* 1807 * mp == NULL -> invalidate the current info, the FS is 1808 * no longer exported. May be called from either vfs_export 1809 * or unmount, so check if it hasn't already been done. 1810 */ 1811 if (mp == NULL) { 1812 if (nfs_pub.np_valid) { 1813 nfs_pub.np_valid = 0; 1814 if (nfs_pub.np_index != NULL) { 1815 FREE(nfs_pub.np_index, M_TEMP); 1816 nfs_pub.np_index = NULL; 1817 } 1818 } 1819 return (0); 1820 } 1821 1822 /* 1823 * Only one allowed at a time. 1824 */ 1825 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 1826 return (EBUSY); 1827 1828 /* 1829 * Get real filehandle for root of exported FS. 1830 */ 1831 memset((caddr_t)&nfs_pub.np_handle, 0, sizeof(nfs_pub.np_handle)); 1832 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 1833 1834 if ((error = VFS_ROOT(mp, &rvp))) 1835 return (error); 1836 1837 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 1838 return (error); 1839 1840 vput(rvp); 1841 1842 /* 1843 * If an indexfile was specified, pull it in. 1844 */ 1845 if (argp->ex_indexfile != NULL) { 1846 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP, 1847 M_WAITOK); 1848 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 1849 MAXNAMLEN, (size_t *)0); 1850 if (!error) { 1851 /* 1852 * Check for illegal filenames. 1853 */ 1854 for (cp = nfs_pub.np_index; *cp; cp++) { 1855 if (*cp == '/') { 1856 error = EINVAL; 1857 break; 1858 } 1859 } 1860 } 1861 if (error) { 1862 FREE(nfs_pub.np_index, M_TEMP); 1863 return (error); 1864 } 1865 } 1866 1867 nfs_pub.np_mount = mp; 1868 nfs_pub.np_valid = 1; 1869 return (0); 1870 } 1871 1872 struct netcred * 1873 vfs_export_lookup(mp, nep, nam) 1874 register struct mount *mp; 1875 struct netexport *nep; 1876 struct mbuf *nam; 1877 { 1878 register struct netcred *np; 1879 register struct radix_node_head *rnh; 1880 struct sockaddr *saddr; 1881 1882 np = NULL; 1883 if (mp->mnt_flag & MNT_EXPORTED) { 1884 /* 1885 * Lookup in the export list first. 1886 */ 1887 if (nam != NULL) { 1888 saddr = mtod(nam, struct sockaddr *); 1889 rnh = nep->ne_rtable[saddr->sa_family]; 1890 if (rnh != NULL) { 1891 np = (struct netcred *) 1892 (*rnh->rnh_matchaddr)((caddr_t)saddr, 1893 rnh); 1894 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 1895 np = NULL; 1896 } 1897 } 1898 /* 1899 * If no address match, use the default if it exists. 1900 */ 1901 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 1902 np = &nep->ne_defexported; 1903 } 1904 return (np); 1905 } 1906 1907 /* 1908 * Do the usual access checking. 1909 * file_mode, uid and gid are from the vnode in question, 1910 * while acc_mode and cred are from the VOP_ACCESS parameter list 1911 */ 1912 int 1913 vaccess(type, file_mode, uid, gid, acc_mode, cred) 1914 enum vtype type; 1915 mode_t file_mode; 1916 uid_t uid; 1917 gid_t gid; 1918 mode_t acc_mode; 1919 struct ucred *cred; 1920 { 1921 mode_t mask; 1922 1923 /* 1924 * Super-user always gets read/write access, but execute access depends 1925 * on at least one execute bit being set. 1926 */ 1927 if (cred->cr_uid == 0) { 1928 if ((acc_mode & VEXEC) && type != VDIR && 1929 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 1930 return (EACCES); 1931 return (0); 1932 } 1933 1934 mask = 0; 1935 1936 /* Otherwise, check the owner. */ 1937 if (cred->cr_uid == uid) { 1938 if (acc_mode & VEXEC) 1939 mask |= S_IXUSR; 1940 if (acc_mode & VREAD) 1941 mask |= S_IRUSR; 1942 if (acc_mode & VWRITE) 1943 mask |= S_IWUSR; 1944 return ((file_mode & mask) == mask ? 0 : EACCES); 1945 } 1946 1947 /* Otherwise, check the groups. */ 1948 if (cred->cr_gid == gid || groupmember(gid, cred)) { 1949 if (acc_mode & VEXEC) 1950 mask |= S_IXGRP; 1951 if (acc_mode & VREAD) 1952 mask |= S_IRGRP; 1953 if (acc_mode & VWRITE) 1954 mask |= S_IWGRP; 1955 return ((file_mode & mask) == mask ? 0 : EACCES); 1956 } 1957 1958 /* Otherwise, check everyone else. */ 1959 if (acc_mode & VEXEC) 1960 mask |= S_IXOTH; 1961 if (acc_mode & VREAD) 1962 mask |= S_IROTH; 1963 if (acc_mode & VWRITE) 1964 mask |= S_IWOTH; 1965 return ((file_mode & mask) == mask ? 0 : EACCES); 1966 } 1967 1968 /* 1969 * Unmount all file systems. 1970 * We traverse the list in reverse order under the assumption that doing so 1971 * will avoid needing to worry about dependencies. 1972 */ 1973 void 1974 vfs_unmountall() 1975 { 1976 register struct mount *mp, *nmp; 1977 int allerror, error; 1978 struct proc *p = curproc; /* XXX */ 1979 1980 /* 1981 * Unmounting a file system blocks the requesting process. 1982 * However, it's possible for this routine to be called when 1983 * curproc is NULL (e.g. panic situation, or via the debugger). 1984 * If we get stuck in this situation, just abort, since any 1985 * attempts to sleep will fault. 1986 */ 1987 if (p == NULL) { 1988 printf("vfs_unmountall: no context, aborting\n"); 1989 return; 1990 } 1991 1992 for (allerror = 0, 1993 mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) { 1994 nmp = mp->mnt_list.cqe_prev; 1995 #ifdef DEBUG 1996 printf("unmounting %s (%s)...\n", 1997 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 1998 #endif 1999 if (vfs_busy(mp, 0, 0)) 2000 continue; 2001 if ((error = dounmount(mp, MNT_FORCE, p)) != 0) { 2002 printf("unmount of %s failed with error %d\n", 2003 mp->mnt_stat.f_mntonname, error); 2004 allerror = 1; 2005 } 2006 } 2007 if (allerror) 2008 printf("WARNING: some file systems would not unmount\n"); 2009 } 2010 2011 /* 2012 * Sync and unmount file systems before shutting down. 2013 */ 2014 void 2015 vfs_shutdown() 2016 { 2017 register struct buf *bp; 2018 int iter, nbusy; 2019 2020 printf("syncing disks... "); 2021 2022 /* XXX Should suspend scheduling. */ 2023 (void) spl0(); 2024 2025 sys_sync(&proc0, (void *)0, (register_t *)0); 2026 2027 /* Wait for sync to finish. */ 2028 for (iter = 0; iter < 20; iter++) { 2029 nbusy = 0; 2030 for (bp = &buf[nbuf]; --bp >= buf; ) 2031 if ((bp->b_flags & (B_BUSY|B_INVAL)) == B_BUSY) 2032 nbusy++; 2033 if (nbusy == 0) 2034 break; 2035 printf("%d ", nbusy); 2036 DELAY(40000 * iter); 2037 } 2038 if (nbusy) { 2039 printf("giving up\n"); 2040 return; 2041 } else 2042 printf("done\n"); 2043 2044 /* 2045 * If we've panic'd, don't make the situation potentially 2046 * worse by unmounting the file systems. 2047 */ 2048 if (panicstr != NULL) 2049 return; 2050 2051 /* Release inodes held by texts before update. */ 2052 #if !defined(UVM) 2053 vnode_pager_umount(NULL); 2054 #endif 2055 #ifdef notdef 2056 vnshutdown(); 2057 #endif 2058 /* Unmount file systems. */ 2059 vfs_unmountall(); 2060 } 2061 2062 /* 2063 * Mount the root file system. If the operator didn't specify a 2064 * file system to use, try all possible file systems until one 2065 * succeeds. 2066 */ 2067 int 2068 vfs_mountroot() 2069 { 2070 extern int (*mountroot) __P((void)); 2071 struct vfsops *v; 2072 2073 if (root_device == NULL) 2074 panic("vfs_mountroot: root device unknown"); 2075 2076 switch (root_device->dv_class) { 2077 case DV_IFNET: 2078 if (rootdev != NODEV) 2079 panic("vfs_mountroot: rootdev set for DV_IFNET"); 2080 break; 2081 2082 case DV_DISK: 2083 if (rootdev == NODEV) 2084 panic("vfs_mountroot: rootdev not set for DV_DISK"); 2085 break; 2086 2087 default: 2088 printf("%s: inappropriate for root file system\n", 2089 root_device->dv_xname); 2090 return (ENODEV); 2091 } 2092 2093 /* 2094 * If user specified a file system, use it. 2095 */ 2096 if (mountroot != NULL) 2097 return ((*mountroot)()); 2098 2099 /* 2100 * Try each file system currently configured into the kernel. 2101 */ 2102 for (v = LIST_FIRST(&vfs_list); v != NULL; v = LIST_NEXT(v, vfs_list)) { 2103 if (v->vfs_mountroot == NULL) 2104 continue; 2105 #ifdef DEBUG 2106 printf("mountroot: trying %s...\n", v->vfs_name); 2107 #endif 2108 if ((*v->vfs_mountroot)() == 0) { 2109 printf("root file system type: %s\n", v->vfs_name); 2110 break; 2111 } 2112 } 2113 2114 if (v == NULL) { 2115 printf("no file system for %s", root_device->dv_xname); 2116 if (root_device->dv_class == DV_DISK) 2117 printf(" (dev 0x%x)", rootdev); 2118 printf("\n"); 2119 return (EFTYPE); 2120 } 2121 return (0); 2122 } 2123 2124 /* 2125 * Given a file system name, look up the vfsops for that 2126 * file system, or return NULL if file system isn't present 2127 * in the kernel. 2128 */ 2129 struct vfsops * 2130 vfs_getopsbyname(name) 2131 const char *name; 2132 { 2133 struct vfsops *v; 2134 2135 for (v = LIST_FIRST(&vfs_list); v != NULL; v = LIST_NEXT(v, vfs_list)) { 2136 if (strcmp(v->vfs_name, name) == 0) 2137 break; 2138 } 2139 2140 return (v); 2141 } 2142 2143 /* 2144 * Establish a file system and initialize it. 2145 */ 2146 int 2147 vfs_attach(vfs) 2148 struct vfsops *vfs; 2149 { 2150 struct vfsops *v; 2151 int error = 0; 2152 2153 2154 /* 2155 * Make sure this file system doesn't already exist. 2156 */ 2157 for (v = LIST_FIRST(&vfs_list); v != NULL; v = LIST_NEXT(v, vfs_list)) { 2158 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) { 2159 error = EEXIST; 2160 goto out; 2161 } 2162 } 2163 2164 /* 2165 * Initialize the vnode operations for this file system. 2166 */ 2167 vfs_opv_init(vfs->vfs_opv_descs); 2168 2169 /* 2170 * Now initialize the file system itself. 2171 */ 2172 (*vfs->vfs_init)(); 2173 2174 /* 2175 * ...and link it into the kernel's list. 2176 */ 2177 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list); 2178 2179 /* 2180 * Sanity: make sure the reference count is 0. 2181 */ 2182 vfs->vfs_refcount = 0; 2183 2184 out: 2185 return (error); 2186 } 2187 2188 /* 2189 * Remove a file system from the kernel. 2190 */ 2191 int 2192 vfs_detach(vfs) 2193 struct vfsops *vfs; 2194 { 2195 struct vfsops *v; 2196 2197 /* 2198 * Make sure no one is using the filesystem. 2199 */ 2200 if (vfs->vfs_refcount != 0) 2201 return (EBUSY); 2202 2203 /* 2204 * ...and remove it from the kernel's list. 2205 */ 2206 for (v = LIST_FIRST(&vfs_list); v != NULL; v = LIST_NEXT(v, vfs_list)) { 2207 if (v == vfs) { 2208 LIST_REMOVE(v, vfs_list); 2209 break; 2210 } 2211 } 2212 2213 if (v == NULL) 2214 return (ESRCH); 2215 2216 /* 2217 * Free the vnode operations vector. 2218 */ 2219 vfs_opv_free(vfs->vfs_opv_descs); 2220 return (0); 2221 } 2222