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 * %sccs.include.redist.c% 11 * 12 * @(#)vfs_subr.c 8.25 (Berkeley) 05/17/95 13 */ 14 15 /* 16 * External virtual filesystem routines 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/proc.h> 22 #include <sys/mount.h> 23 #include <sys/time.h> 24 #include <sys/vnode.h> 25 #include <sys/stat.h> 26 #include <sys/namei.h> 27 #include <sys/ucred.h> 28 #include <sys/buf.h> 29 #include <sys/errno.h> 30 #include <sys/malloc.h> 31 #include <sys/domain.h> 32 #include <sys/mbuf.h> 33 34 #include <vm/vm.h> 35 #include <sys/sysctl.h> 36 37 #include <miscfs/specfs/specdev.h> 38 39 enum vtype iftovt_tab[16] = { 40 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 41 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 42 }; 43 int vttoif_tab[9] = { 44 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 45 S_IFSOCK, S_IFIFO, S_IFMT, 46 }; 47 48 /* 49 * Insq/Remq for the vnode usage lists. 50 */ 51 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 52 #define bufremvn(bp) { \ 53 LIST_REMOVE(bp, b_vnbufs); \ 54 (bp)->b_vnbufs.le_next = NOLIST; \ 55 } 56 TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */ 57 struct mntlist mountlist; /* mounted filesystem list */ 58 static struct simplelock mntid_slock; 59 struct simplelock mntvnode_slock; 60 static struct simplelock spechash_slock; 61 static struct simplelock vnode_free_list_slock; 62 63 /* 64 * Initialize the vnode management data structures. 65 */ 66 void 67 vntblinit() 68 { 69 70 simple_lock_init(&mntvnode_slock); 71 simple_lock_init(&mntid_slock); 72 simple_lock_init(&spechash_slock); 73 TAILQ_INIT(&vnode_free_list); 74 simple_lock_init(&vnode_free_list_slock); 75 CIRCLEQ_INIT(&mountlist); 76 } 77 78 /* 79 * Lock a filesystem. 80 * Used to prevent access to it while mounting and unmounting. 81 */ 82 int 83 vfs_lock(mp) 84 register struct mount *mp; 85 { 86 87 while (mp->mnt_flag & MNT_MLOCK) { 88 mp->mnt_flag |= MNT_MWAIT; 89 tsleep((caddr_t)mp, PVFS, "vfslock", 0); 90 } 91 mp->mnt_flag |= MNT_MLOCK; 92 return (0); 93 } 94 95 /* 96 * Unlock a locked filesystem. 97 * Panic if filesystem is not locked. 98 */ 99 void 100 vfs_unlock(mp) 101 register struct mount *mp; 102 { 103 104 if ((mp->mnt_flag & MNT_MLOCK) == 0) 105 panic("vfs_unlock: not locked"); 106 mp->mnt_flag &= ~MNT_MLOCK; 107 if (mp->mnt_flag & MNT_MWAIT) { 108 mp->mnt_flag &= ~MNT_MWAIT; 109 wakeup((caddr_t)mp); 110 } 111 } 112 113 /* 114 * Mark a mount point as busy. 115 * Used to synchronize access and to delay unmounting. 116 */ 117 int 118 vfs_busy(mp) 119 register struct mount *mp; 120 { 121 122 while (mp->mnt_flag & MNT_MPBUSY) { 123 mp->mnt_flag |= MNT_MPWANT; 124 tsleep((caddr_t)&mp->mnt_flag, PVFS, "vfsbusy", 0); 125 } 126 if (mp->mnt_flag & MNT_UNMOUNT) 127 return (1); 128 mp->mnt_flag |= MNT_MPBUSY; 129 return (0); 130 } 131 132 /* 133 * Free a busy filesystem. 134 * Panic if filesystem is not busy. 135 */ 136 void 137 vfs_unbusy(mp) 138 register struct mount *mp; 139 { 140 141 if ((mp->mnt_flag & MNT_MPBUSY) == 0) 142 panic("vfs_unbusy: not busy"); 143 mp->mnt_flag &= ~MNT_MPBUSY; 144 if (mp->mnt_flag & MNT_MPWANT) { 145 mp->mnt_flag &= ~MNT_MPWANT; 146 wakeup((caddr_t)&mp->mnt_flag); 147 } 148 } 149 150 /* 151 * Lookup a filesystem type, and if found allocate and initialize 152 * a mount structure for it. 153 * 154 * Devname is usually updated by mount(8) after booting. 155 */ 156 int 157 vfs_rootmountalloc(fstypename, devname, mpp) 158 char *fstypename; 159 char *devname; 160 struct mount **mpp; 161 { 162 struct vfsconf *vfsp; 163 struct mount *mp; 164 165 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 166 if (!strcmp(vfsp->vfc_name, fstypename)) 167 break; 168 if (vfsp == NULL) 169 return (ENODEV); 170 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 171 bzero((char *)mp, (u_long)sizeof(struct mount)); 172 LIST_INIT(&mp->mnt_vnodelist); 173 mp->mnt_vfc = vfsp; 174 mp->mnt_op = vfsp->vfc_vfsops; 175 mp->mnt_flag = MNT_RDONLY; 176 mp->mnt_vnodecovered = NULLVP; 177 vfsp->vfc_refcount++; 178 mp->mnt_stat.f_type = vfsp->vfc_typenum; 179 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 180 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 181 mp->mnt_stat.f_mntonname[0] = '/'; 182 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0); 183 *mpp = mp; 184 return (0); 185 } 186 187 /* 188 * Find an appropriate filesystem to use for the root. If a filesystem 189 * has not been preselected, walk through the list of known filesystems 190 * trying those that have mountroot routines, and try them until one 191 * works or we have tried them all. 192 */ 193 int 194 vfs_mountroot() 195 { 196 struct vfsconf *vfsp; 197 extern int (*mountroot)(void); 198 int error; 199 200 if (mountroot != NULL) 201 return ((*vfsp->vfc_mountroot)()); 202 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 203 if (vfsp->vfc_mountroot == NULL) 204 continue; 205 if ((error = (*vfsp->vfc_mountroot)()) == 0) 206 return (0); 207 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error); 208 } 209 return (ENODEV); 210 } 211 212 /* 213 * Lookup a mount point by filesystem identifier. 214 */ 215 struct mount * 216 vfs_getvfs(fsid) 217 fsid_t *fsid; 218 { 219 register struct mount *mp; 220 221 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; 222 mp = mp->mnt_list.cqe_next) { 223 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 224 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) 225 return (mp); 226 } 227 return ((struct mount *)0); 228 } 229 230 /* 231 * Get a new unique fsid 232 */ 233 void 234 vfs_getnewfsid(mp) 235 struct mount *mp; 236 { 237 static u_short xxxfs_mntid; 238 239 fsid_t tfsid; 240 int mtype; 241 242 simple_lock(&mntid_slock); 243 mtype = mp->mnt_vfc->vfc_typenum; 244 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0); 245 mp->mnt_stat.f_fsid.val[1] = mtype; 246 if (xxxfs_mntid == 0) 247 ++xxxfs_mntid; 248 tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid); 249 tfsid.val[1] = mtype; 250 if (mountlist.cqh_first != (void *)&mountlist) { 251 while (vfs_getvfs(&tfsid)) { 252 tfsid.val[0]++; 253 xxxfs_mntid++; 254 } 255 } 256 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 257 simple_unlock(&mntid_slock); 258 } 259 260 /* 261 * Set vnode attributes to VNOVAL 262 */ 263 void 264 vattr_null(vap) 265 register struct vattr *vap; 266 { 267 268 vap->va_type = VNON; 269 vap->va_size = vap->va_bytes = VNOVAL; 270 vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid = 271 vap->va_fsid = vap->va_fileid = 272 vap->va_blocksize = vap->va_rdev = 273 vap->va_atime.ts_sec = vap->va_atime.ts_nsec = 274 vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec = 275 vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec = 276 vap->va_flags = vap->va_gen = VNOVAL; 277 vap->va_vaflags = 0; 278 } 279 280 /* 281 * Routines having to do with the management of the vnode table. 282 */ 283 extern int (**dead_vnodeop_p)(); 284 static void vclean __P((struct vnode *vp, int flag, struct proc *p)); 285 extern void vgonel __P((struct vnode *vp, struct proc *p)); 286 long numvnodes; 287 extern struct vattr va_null; 288 289 /* 290 * Return the next vnode from the free list. 291 */ 292 int 293 getnewvnode(tag, mp, vops, vpp) 294 enum vtagtype tag; 295 struct mount *mp; 296 int (**vops)(); 297 struct vnode **vpp; 298 { 299 struct proc *p = curproc; /* XXX */ 300 struct vnode *vp; 301 int s; 302 int cnt; 303 304 top: 305 simple_lock(&vnode_free_list_slock); 306 if ((vnode_free_list.tqh_first == NULL && 307 numvnodes < 2 * desiredvnodes) || 308 numvnodes < desiredvnodes) { 309 simple_unlock(&vnode_free_list_slock); 310 vp = (struct vnode *)malloc((u_long)sizeof *vp, 311 M_VNODE, M_WAITOK); 312 bzero((char *)vp, sizeof *vp); 313 numvnodes++; 314 } else { 315 for (vp = vnode_free_list.tqh_first; 316 vp != NULLVP; vp = vp->v_freelist.tqe_next) { 317 if (simple_lock_try(&vp->v_interlock)) 318 break; 319 } 320 /* 321 * Unless this is a bad time of the month, at most 322 * the first NCPUS items on the free list are 323 * locked, so this is close enough to being empty. 324 */ 325 if (vp == NULLVP) { 326 simple_unlock(&vnode_free_list_slock); 327 tablefull("vnode"); 328 *vpp = 0; 329 return (ENFILE); 330 } 331 if (vp->v_usecount) 332 panic("free vnode isn't"); 333 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 334 /* see comment on why 0xdeadb is set at end of vgone (below) */ 335 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb; 336 simple_unlock(&vnode_free_list_slock); 337 vp->v_lease = NULL; 338 if (vp->v_type != VBAD) 339 vgonel(vp, p); 340 else 341 simple_unlock(&vp->v_interlock); 342 #ifdef DIAGNOSTIC 343 if (vp->v_data) 344 panic("cleaned vnode isn't"); 345 s = splbio(); 346 if (vp->v_numoutput) 347 panic("Clean vnode has pending I/O's"); 348 splx(s); 349 #endif 350 vp->v_flag = 0; 351 vp->v_lastr = 0; 352 vp->v_ralen = 0; 353 vp->v_maxra = 0; 354 vp->v_lastw = 0; 355 vp->v_lasta = 0; 356 vp->v_cstart = 0; 357 vp->v_clen = 0; 358 vp->v_socket = 0; 359 } 360 vp->v_type = VNON; 361 cache_purge(vp); 362 vp->v_tag = tag; 363 vp->v_op = vops; 364 insmntque(vp, mp); 365 *vpp = vp; 366 vp->v_usecount = 1; 367 vp->v_data = 0; 368 return (0); 369 } 370 371 /* 372 * Move a vnode from one mount queue to another. 373 */ 374 void 375 insmntque(vp, mp) 376 struct vnode *vp; 377 struct mount *mp; 378 { 379 380 simple_lock(&mntvnode_slock); 381 /* 382 * Delete from old mount point vnode list, if on one. 383 */ 384 if (vp->v_mount != NULL) 385 LIST_REMOVE(vp, v_mntvnodes); 386 /* 387 * Insert into list of vnodes for the new mount point, if available. 388 */ 389 if ((vp->v_mount = mp) != NULL) 390 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes); 391 simple_unlock(&mntvnode_slock); 392 } 393 394 /* 395 * Update outstanding I/O count and do wakeup if requested. 396 */ 397 void 398 vwakeup(bp) 399 register struct buf *bp; 400 { 401 register struct vnode *vp; 402 403 bp->b_flags &= ~B_WRITEINPROG; 404 if (vp = bp->b_vp) { 405 if (--vp->v_numoutput < 0) 406 panic("vwakeup: neg numoutput"); 407 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) { 408 if (vp->v_numoutput < 0) 409 panic("vwakeup: neg numoutput 2"); 410 vp->v_flag &= ~VBWAIT; 411 wakeup((caddr_t)&vp->v_numoutput); 412 } 413 } 414 } 415 416 /* 417 * Flush out and invalidate all buffers associated with a vnode. 418 * Called with the underlying object locked. 419 */ 420 int 421 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo) 422 register struct vnode *vp; 423 int flags; 424 struct ucred *cred; 425 struct proc *p; 426 int slpflag, slptimeo; 427 { 428 register struct buf *bp; 429 struct buf *nbp, *blist; 430 int s, error; 431 432 if (flags & V_SAVE) { 433 if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) 434 return (error); 435 if (vp->v_dirtyblkhd.lh_first != NULL) 436 panic("vinvalbuf: dirty bufs"); 437 } 438 for (;;) { 439 if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA) 440 while (blist && blist->b_lblkno < 0) 441 blist = blist->b_vnbufs.le_next; 442 if (!blist && (blist = vp->v_dirtyblkhd.lh_first) && 443 (flags & V_SAVEMETA)) 444 while (blist && blist->b_lblkno < 0) 445 blist = blist->b_vnbufs.le_next; 446 if (!blist) 447 break; 448 449 for (bp = blist; bp; bp = nbp) { 450 nbp = bp->b_vnbufs.le_next; 451 if (flags & V_SAVEMETA && bp->b_lblkno < 0) 452 continue; 453 s = splbio(); 454 if (bp->b_flags & B_BUSY) { 455 bp->b_flags |= B_WANTED; 456 error = tsleep((caddr_t)bp, 457 slpflag | (PRIBIO + 1), "vinvalbuf", 458 slptimeo); 459 splx(s); 460 if (error) 461 return (error); 462 break; 463 } 464 bremfree(bp); 465 bp->b_flags |= B_BUSY; 466 splx(s); 467 /* 468 * XXX Since there are no node locks for NFS, I believe 469 * there is a slight chance that a delayed write will 470 * occur while sleeping just above, so check for it. 471 */ 472 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 473 (void) VOP_BWRITE(bp); 474 break; 475 } 476 bp->b_flags |= B_INVAL; 477 brelse(bp); 478 } 479 } 480 if (!(flags & V_SAVEMETA) && 481 (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first)) 482 panic("vinvalbuf: flush failed"); 483 return (0); 484 } 485 486 /* 487 * Associate a buffer with a vnode. 488 */ 489 void 490 bgetvp(vp, bp) 491 register struct vnode *vp; 492 register struct buf *bp; 493 { 494 495 if (bp->b_vp) 496 panic("bgetvp: not free"); 497 VHOLD(vp); 498 bp->b_vp = vp; 499 if (vp->v_type == VBLK || vp->v_type == VCHR) 500 bp->b_dev = vp->v_rdev; 501 else 502 bp->b_dev = NODEV; 503 /* 504 * Insert onto list for new vnode. 505 */ 506 bufinsvn(bp, &vp->v_cleanblkhd); 507 } 508 509 /* 510 * Disassociate a buffer from a vnode. 511 */ 512 void 513 brelvp(bp) 514 register struct buf *bp; 515 { 516 struct vnode *vp; 517 518 if (bp->b_vp == (struct vnode *) 0) 519 panic("brelvp: NULL"); 520 /* 521 * Delete from old vnode list, if on one. 522 */ 523 if (bp->b_vnbufs.le_next != NOLIST) 524 bufremvn(bp); 525 vp = bp->b_vp; 526 bp->b_vp = (struct vnode *) 0; 527 HOLDRELE(vp); 528 } 529 530 /* 531 * Reassign a buffer from one vnode to another. 532 * Used to assign file specific control information 533 * (indirect blocks) to the vnode to which they belong. 534 */ 535 void 536 reassignbuf(bp, newvp) 537 register struct buf *bp; 538 register struct vnode *newvp; 539 { 540 register struct buflists *listheadp; 541 542 if (newvp == NULL) { 543 printf("reassignbuf: NULL"); 544 return; 545 } 546 /* 547 * Delete from old vnode list, if on one. 548 */ 549 if (bp->b_vnbufs.le_next != NOLIST) 550 bufremvn(bp); 551 /* 552 * If dirty, put on list of dirty buffers; 553 * otherwise insert onto list of clean buffers. 554 */ 555 if (bp->b_flags & B_DELWRI) 556 listheadp = &newvp->v_dirtyblkhd; 557 else 558 listheadp = &newvp->v_cleanblkhd; 559 bufinsvn(bp, listheadp); 560 } 561 562 /* 563 * Create a vnode for a block device. 564 * Used for root filesystem, argdev, and swap areas. 565 * Also used for memory file system special devices. 566 */ 567 int 568 bdevvp(dev, vpp) 569 dev_t dev; 570 struct vnode **vpp; 571 { 572 register struct vnode *vp; 573 struct vnode *nvp; 574 int error; 575 576 if (dev == NODEV) { 577 *vpp = NULLVP; 578 return (ENODEV); 579 } 580 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp); 581 if (error) { 582 *vpp = NULLVP; 583 return (error); 584 } 585 vp = nvp; 586 vp->v_type = VBLK; 587 if (nvp = checkalias(vp, dev, (struct mount *)0)) { 588 vput(vp); 589 vp = nvp; 590 } 591 *vpp = vp; 592 return (0); 593 } 594 595 /* 596 * Check to see if the new vnode represents a special device 597 * for which we already have a vnode (either because of 598 * bdevvp() or because of a different vnode representing 599 * the same block device). If such an alias exists, deallocate 600 * the existing contents and return the aliased vnode. The 601 * caller is responsible for filling it with its new contents. 602 */ 603 struct vnode * 604 checkalias(nvp, nvp_rdev, mp) 605 register struct vnode *nvp; 606 dev_t nvp_rdev; 607 struct mount *mp; 608 { 609 struct proc *p = curproc; /* XXX */ 610 struct vnode *vp; 611 struct vnode **vpp; 612 613 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 614 return (NULLVP); 615 616 vpp = &speclisth[SPECHASH(nvp_rdev)]; 617 loop: 618 simple_lock(&spechash_slock); 619 for (vp = *vpp; vp; vp = vp->v_specnext) { 620 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) 621 continue; 622 /* 623 * Alias, but not in use, so flush it out. 624 */ 625 simple_lock(&vp->v_interlock); 626 if (vp->v_usecount == 0) { 627 simple_unlock(&spechash_slock); 628 vgonel(vp, p); 629 goto loop; 630 } 631 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) { 632 simple_unlock(&spechash_slock); 633 goto loop; 634 } 635 break; 636 } 637 if (vp == NULL || vp->v_tag != VT_NON) { 638 MALLOC(nvp->v_specinfo, struct specinfo *, 639 sizeof(struct specinfo), M_VNODE, M_WAITOK); 640 nvp->v_rdev = nvp_rdev; 641 nvp->v_hashchain = vpp; 642 nvp->v_specnext = *vpp; 643 nvp->v_specflags = 0; 644 simple_unlock(&spechash_slock); 645 *vpp = nvp; 646 if (vp != NULLVP) { 647 nvp->v_flag |= VALIASED; 648 vp->v_flag |= VALIASED; 649 vput(vp); 650 } 651 return (NULLVP); 652 } 653 simple_unlock(&spechash_slock); 654 VOP_UNLOCK(vp, 0, p); 655 simple_lock(&vp->v_interlock); 656 vclean(vp, 0, p); 657 vp->v_op = nvp->v_op; 658 vp->v_tag = nvp->v_tag; 659 nvp->v_type = VNON; 660 insmntque(vp, mp); 661 return (vp); 662 } 663 664 /* 665 * Grab a particular vnode from the free list, increment its 666 * reference count and lock it. The vnode lock bit is set the 667 * vnode is being eliminated in vgone. The process is awakened 668 * when the transition is completed, and an error returned to 669 * indicate that the vnode is no longer usable (possibly having 670 * been changed to a new file system type). 671 */ 672 int 673 vget(vp, flags, p) 674 struct vnode *vp; 675 int flags; 676 struct proc *p; 677 { 678 679 /* 680 * If the vnode is in the process of being cleaned out for 681 * another use, we wait for the cleaning to finish and then 682 * return failure. Cleaning is determined by checking that 683 * the VXLOCK flag is set. 684 */ 685 if ((flags & LK_INTERLOCK) == 0) 686 simple_lock(&vp->v_interlock); 687 if (vp->v_flag & VXLOCK) { 688 vp->v_flag |= VXWANT; 689 simple_unlock(&vp->v_interlock); 690 tsleep((caddr_t)vp, PINOD, "vget", 0); 691 return (ENOENT); 692 } 693 if (vp->v_usecount == 0) { 694 simple_lock(&vnode_free_list_slock); 695 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 696 simple_unlock(&vnode_free_list_slock); 697 } 698 vp->v_usecount++; 699 if (flags & LK_TYPE_MASK) 700 return (vn_lock(vp, flags | LK_INTERLOCK, p)); 701 simple_unlock(&vp->v_interlock); 702 return (0); 703 } 704 705 /* 706 * Stubs to use when there is no locking to be done on the underlying object. 707 * 708 * Getting a lock just clears the interlock if necessary. 709 */ 710 int 711 vop_nolock(ap) 712 struct vop_lock_args /* { 713 struct vnode *a_vp; 714 int a_flags; 715 struct proc *a_p; 716 } */ *ap; 717 { 718 struct vnode *vp = ap->a_vp; 719 720 /* 721 * Since we are not using the lock manager, we must clear 722 * the interlock here. 723 */ 724 if (ap->a_flags & LK_INTERLOCK) 725 simple_unlock(&vp->v_interlock); 726 return (0); 727 } 728 729 /* 730 * Unlock has nothing to do. 731 */ 732 int 733 vop_nounlock(ap) 734 struct vop_unlock_args /* { 735 struct vnode *a_vp; 736 int a_flags; 737 struct proc *a_p; 738 } */ *ap; 739 { 740 741 return (0); 742 } 743 744 /* 745 * Nothing is ever locked. 746 */ 747 int 748 vop_noislocked(ap) 749 struct vop_islocked_args /* { 750 struct vnode *a_vp; 751 } */ *ap; 752 { 753 754 return (0); 755 } 756 757 /* 758 * Vnode reference. 759 */ 760 void 761 vref(vp) 762 struct vnode *vp; 763 { 764 765 simple_lock(&vp->v_interlock); 766 if (vp->v_usecount <= 0) 767 panic("vref used where vget required"); 768 vp->v_usecount++; 769 simple_unlock(&vp->v_interlock); 770 } 771 772 /* 773 * vput(), just unlock and vrele() 774 */ 775 void 776 vput(vp) 777 struct vnode *vp; 778 { 779 struct proc *p = curproc; /* XXX */ 780 781 #ifdef DIGANOSTIC 782 if (vp == NULL) 783 panic("vput: null vp"); 784 #endif 785 simple_lock(&vp->v_interlock); 786 vp->v_usecount--; 787 if (vp->v_usecount > 0) { 788 simple_unlock(&vp->v_interlock); 789 VOP_UNLOCK(vp, 0, p); 790 return; 791 } 792 #ifdef DIAGNOSTIC 793 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 794 vprint("vput: bad ref count", vp); 795 panic("vput: ref cnt"); 796 } 797 #endif 798 /* 799 * insert at tail of LRU list 800 */ 801 simple_lock(&vnode_free_list_slock); 802 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 803 simple_unlock(&vnode_free_list_slock); 804 simple_unlock(&vp->v_interlock); 805 VOP_INACTIVE(vp, p); 806 } 807 808 /* 809 * Vnode release. 810 * If count drops to zero, call inactive routine and return to freelist. 811 */ 812 void 813 vrele(vp) 814 struct vnode *vp; 815 { 816 struct proc *p = curproc; /* XXX */ 817 818 #ifdef DIAGNOSTIC 819 if (vp == NULL) 820 panic("vrele: null vp"); 821 #endif 822 simple_lock(&vp->v_interlock); 823 vp->v_usecount--; 824 if (vp->v_usecount > 0) { 825 simple_unlock(&vp->v_interlock); 826 return; 827 } 828 #ifdef DIAGNOSTIC 829 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 830 vprint("vrele: bad ref count", vp); 831 panic("vrele: ref cnt"); 832 } 833 #endif 834 /* 835 * insert at tail of LRU list 836 */ 837 simple_lock(&vnode_free_list_slock); 838 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 839 simple_unlock(&vnode_free_list_slock); 840 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) 841 VOP_INACTIVE(vp, p); 842 } 843 844 #ifdef DIAGNOSTIC 845 /* 846 * Page or buffer structure gets a reference. 847 */ 848 void 849 vhold(vp) 850 register struct vnode *vp; 851 { 852 853 simple_lock(&vp->v_interlock); 854 vp->v_holdcnt++; 855 simple_unlock(&vp->v_interlock); 856 } 857 858 /* 859 * Page or buffer structure frees a reference. 860 */ 861 void 862 holdrele(vp) 863 register struct vnode *vp; 864 { 865 866 simple_lock(&vp->v_interlock); 867 if (vp->v_holdcnt <= 0) 868 panic("holdrele: holdcnt"); 869 vp->v_holdcnt--; 870 simple_unlock(&vp->v_interlock); 871 } 872 #endif /* DIAGNOSTIC */ 873 874 /* 875 * Remove any vnodes in the vnode table belonging to mount point mp. 876 * 877 * If MNT_NOFORCE is specified, there should not be any active ones, 878 * return error if any are found (nb: this is a user error, not a 879 * system error). If MNT_FORCE is specified, detach any active vnodes 880 * that are found. 881 */ 882 #ifdef DIAGNOSTIC 883 int busyprt = 0; /* print out busy vnodes */ 884 struct ctldebug debug1 = { "busyprt", &busyprt }; 885 #endif 886 887 int 888 vflush(mp, skipvp, flags) 889 struct mount *mp; 890 struct vnode *skipvp; 891 int flags; 892 { 893 struct proc *p = curproc; /* XXX */ 894 struct vnode *vp, *nvp; 895 int busy = 0; 896 897 #ifdef DIAGNOSTIC 898 if ((mp->mnt_flag & MNT_MPBUSY) == 0) 899 panic("vflush: not busy"); 900 #endif 901 902 simple_lock(&mntvnode_slock); 903 loop: 904 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 905 if (vp->v_mount != mp) 906 goto loop; 907 nvp = vp->v_mntvnodes.le_next; 908 /* 909 * Skip over a selected vnode. 910 */ 911 if (vp == skipvp) 912 continue; 913 914 simple_lock(&vp->v_interlock); 915 /* 916 * Skip over a vnodes marked VSYSTEM. 917 */ 918 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 919 simple_unlock(&vp->v_interlock); 920 continue; 921 } 922 /* 923 * If WRITECLOSE is set, only flush out regular file 924 * vnodes open for writing. 925 */ 926 if ((flags & WRITECLOSE) && 927 (vp->v_writecount == 0 || vp->v_type != VREG)) { 928 simple_unlock(&vp->v_interlock); 929 continue; 930 } 931 /* 932 * With v_usecount == 0, all we need to do is clear 933 * out the vnode data structures and we are done. 934 */ 935 if (vp->v_usecount == 0) { 936 simple_unlock(&mntvnode_slock); 937 vgonel(vp, p); 938 simple_lock(&mntvnode_slock); 939 continue; 940 } 941 /* 942 * If FORCECLOSE is set, forcibly close the vnode. 943 * For block or character devices, revert to an 944 * anonymous device. For all other files, just kill them. 945 */ 946 if (flags & FORCECLOSE) { 947 simple_unlock(&mntvnode_slock); 948 if (vp->v_type != VBLK && vp->v_type != VCHR) { 949 vgonel(vp, p); 950 } else { 951 vclean(vp, 0, p); 952 vp->v_op = spec_vnodeop_p; 953 insmntque(vp, (struct mount *)0); 954 } 955 simple_lock(&mntvnode_slock); 956 continue; 957 } 958 #ifdef DIAGNOSTIC 959 if (busyprt) 960 vprint("vflush: busy vnode", vp); 961 #endif 962 simple_unlock(&vp->v_interlock); 963 busy++; 964 } 965 simple_unlock(&mntvnode_slock); 966 if (busy) 967 return (EBUSY); 968 return (0); 969 } 970 971 /* 972 * Disassociate the underlying file system from a vnode. 973 * The vnode interlock is held on entry. 974 */ 975 static void 976 vclean(vp, flags, p) 977 struct vnode *vp; 978 int flags; 979 struct proc *p; 980 { 981 int active; 982 983 /* 984 * Check to see if the vnode is in use. 985 * If so we have to reference it before we clean it out 986 * so that its count cannot fall to zero and generate a 987 * race against ourselves to recycle it. 988 */ 989 if (active = vp->v_usecount) 990 vp->v_usecount++; 991 /* 992 * Prevent the vnode from being recycled or 993 * brought into use while we clean it out. 994 */ 995 if (vp->v_flag & VXLOCK) 996 panic("vclean: deadlock"); 997 vp->v_flag |= VXLOCK; 998 /* 999 * Even if the count is zero, the VOP_INACTIVE routine may still 1000 * have the object locked while it cleans it out. The VOP_LOCK 1001 * ensures that the VOP_INACTIVE routine is done with its work. 1002 * For active vnodes, it ensures that no other activity can 1003 * occur while the underlying object is being cleaned out. 1004 */ 1005 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p); 1006 /* 1007 * Clean out any buffers associated with the vnode. 1008 */ 1009 if (flags & DOCLOSE) 1010 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 1011 /* 1012 * If purging an active vnode, it must be closed and 1013 * deactivated before being reclaimed. Note that the 1014 * VOP_INACTIVE will unlock the vnode. 1015 */ 1016 if (active) { 1017 if (flags & DOCLOSE) 1018 VOP_CLOSE(vp, IO_NDELAY, NOCRED, p); 1019 VOP_INACTIVE(vp, p); 1020 } else { 1021 /* 1022 * Any other processes trying to obtain this lock must first 1023 * wait for VXLOCK to clear, then call the new lock operation. 1024 */ 1025 VOP_UNLOCK(vp, 0, p); 1026 } 1027 /* 1028 * Reclaim the vnode. 1029 */ 1030 if (VOP_RECLAIM(vp, p)) 1031 panic("vclean: cannot reclaim"); 1032 if (active) 1033 vrele(vp); 1034 cache_purge(vp); 1035 1036 /* 1037 * Done with purge, notify sleepers of the grim news. 1038 */ 1039 vp->v_op = dead_vnodeop_p; 1040 vp->v_tag = VT_NON; 1041 vp->v_flag &= ~VXLOCK; 1042 if (vp->v_flag & VXWANT) { 1043 vp->v_flag &= ~VXWANT; 1044 wakeup((caddr_t)vp); 1045 } 1046 } 1047 1048 /* 1049 * Eliminate all activity associated with the requested vnode 1050 * and with all vnodes aliased to the requested vnode. 1051 */ 1052 int 1053 vop_revoke(ap) 1054 struct vop_revoke_args /* { 1055 struct vnode *a_vp; 1056 int a_flags; 1057 } */ *ap; 1058 { 1059 struct vnode *vp, *vq; 1060 struct proc *p = curproc; /* XXX */ 1061 1062 #ifdef DIAGNOSTIC 1063 if ((ap->a_flags & REVOKEALL) == 0) 1064 panic("vop_revoke"); 1065 #endif 1066 1067 vp = ap->a_vp; 1068 simple_lock(&vp->v_interlock); 1069 1070 if (vp->v_flag & VALIASED) { 1071 /* 1072 * If a vgone (or vclean) is already in progress, 1073 * wait until it is done and return. 1074 */ 1075 if (vp->v_flag & VXLOCK) { 1076 vp->v_flag |= VXWANT; 1077 simple_unlock(&vp->v_interlock); 1078 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0); 1079 return (0); 1080 } 1081 /* 1082 * Ensure that vp will not be vgone'd while we 1083 * are eliminating its aliases. 1084 */ 1085 vp->v_flag |= VXLOCK; 1086 simple_unlock(&vp->v_interlock); 1087 while (vp->v_flag & VALIASED) { 1088 simple_lock(&spechash_slock); 1089 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1090 if (vq->v_rdev != vp->v_rdev || 1091 vq->v_type != vp->v_type || vp == vq) 1092 continue; 1093 simple_unlock(&spechash_slock); 1094 vgone(vq); 1095 break; 1096 } 1097 if (vq == NULLVP) 1098 simple_unlock(&spechash_slock); 1099 } 1100 /* 1101 * Remove the lock so that vgone below will 1102 * really eliminate the vnode after which time 1103 * vgone will awaken any sleepers. 1104 */ 1105 simple_lock(&vp->v_interlock); 1106 vp->v_flag &= ~VXLOCK; 1107 } 1108 vgonel(vp, p); 1109 return (0); 1110 } 1111 1112 /* 1113 * Recycle an unused vnode to the front of the free list. 1114 * Release the passed interlock if the vnode will be recycled. 1115 */ 1116 int 1117 vrecycle(vp, inter_lkp, p) 1118 struct vnode *vp; 1119 struct simplelock *inter_lkp; 1120 struct proc *p; 1121 { 1122 1123 simple_lock(&vp->v_interlock); 1124 if (vp->v_usecount == 0) { 1125 if (inter_lkp) 1126 simple_unlock(inter_lkp); 1127 vgonel(vp, p); 1128 return (1); 1129 } 1130 simple_unlock(&vp->v_interlock); 1131 return (0); 1132 } 1133 1134 /* 1135 * Eliminate all activity associated with a vnode 1136 * in preparation for reuse. 1137 */ 1138 void 1139 vgone(vp) 1140 struct vnode *vp; 1141 { 1142 struct proc *p = curproc; /* XXX */ 1143 1144 simple_lock(&vp->v_interlock); 1145 vgonel(vp, p); 1146 } 1147 1148 /* 1149 * vgone, with the vp interlock held. 1150 */ 1151 void 1152 vgonel(vp, p) 1153 struct vnode *vp; 1154 struct proc *p; 1155 { 1156 struct vnode *vq; 1157 struct vnode *vx; 1158 1159 /* 1160 * If a vgone (or vclean) is already in progress, 1161 * wait until it is done and return. 1162 */ 1163 if (vp->v_flag & VXLOCK) { 1164 vp->v_flag |= VXWANT; 1165 simple_unlock(&vp->v_interlock); 1166 tsleep((caddr_t)vp, PINOD, "vgone", 0); 1167 return; 1168 } 1169 /* 1170 * Clean out the filesystem specific data. 1171 */ 1172 vclean(vp, DOCLOSE, p); 1173 /* 1174 * Delete from old mount point vnode list, if on one. 1175 */ 1176 if (vp->v_mount != NULL) 1177 insmntque(vp, (struct mount *)0); 1178 /* 1179 * If special device, remove it from special device alias list 1180 * if it is on one. 1181 */ 1182 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) { 1183 simple_lock(&spechash_slock); 1184 if (*vp->v_hashchain == vp) { 1185 *vp->v_hashchain = vp->v_specnext; 1186 } else { 1187 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1188 if (vq->v_specnext != vp) 1189 continue; 1190 vq->v_specnext = vp->v_specnext; 1191 break; 1192 } 1193 if (vq == NULL) 1194 panic("missing bdev"); 1195 } 1196 if (vp->v_flag & VALIASED) { 1197 vx = NULL; 1198 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1199 if (vq->v_rdev != vp->v_rdev || 1200 vq->v_type != vp->v_type) 1201 continue; 1202 if (vx) 1203 break; 1204 vx = vq; 1205 } 1206 if (vx == NULL) 1207 panic("missing alias"); 1208 if (vq == NULL) 1209 vx->v_flag &= ~VALIASED; 1210 vp->v_flag &= ~VALIASED; 1211 } 1212 simple_unlock(&spechash_slock); 1213 FREE(vp->v_specinfo, M_VNODE); 1214 vp->v_specinfo = NULL; 1215 } 1216 /* 1217 * If it is on the freelist and not already at the head, 1218 * move it to the head of the list. The test of the back 1219 * pointer and the reference count of zero is because 1220 * it will be removed from the free list by getnewvnode, 1221 * but will not have its reference count incremented until 1222 * after calling vgone. If the reference count were 1223 * incremented first, vgone would (incorrectly) try to 1224 * close the previous instance of the underlying object. 1225 * So, the back pointer is explicitly set to `0xdeadb' in 1226 * getnewvnode after removing it from the freelist to ensure 1227 * that we do not try to move it here. 1228 */ 1229 if (vp->v_usecount == 0) { 1230 simple_lock(&vnode_free_list_slock); 1231 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb) && 1232 vnode_free_list.tqh_first != vp) { 1233 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1234 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1235 } 1236 simple_unlock(&vnode_free_list_slock); 1237 } 1238 vp->v_type = VBAD; 1239 } 1240 1241 /* 1242 * Lookup a vnode by device number. 1243 */ 1244 int 1245 vfinddev(dev, type, vpp) 1246 dev_t dev; 1247 enum vtype type; 1248 struct vnode **vpp; 1249 { 1250 struct vnode *vp; 1251 int rc = 0; 1252 1253 simple_lock(&spechash_slock); 1254 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 1255 if (dev != vp->v_rdev || type != vp->v_type) 1256 continue; 1257 *vpp = vp; 1258 rc = 1; 1259 break; 1260 } 1261 simple_unlock(&spechash_slock); 1262 return (rc); 1263 } 1264 1265 /* 1266 * Calculate the total number of references to a special device. 1267 */ 1268 int 1269 vcount(vp) 1270 struct vnode *vp; 1271 { 1272 struct vnode *vq, *vnext; 1273 int count; 1274 1275 loop: 1276 if ((vp->v_flag & VALIASED) == 0) 1277 return (vp->v_usecount); 1278 simple_lock(&spechash_slock); 1279 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) { 1280 vnext = vq->v_specnext; 1281 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 1282 continue; 1283 /* 1284 * Alias, but not in use, so flush it out. 1285 */ 1286 if (vq->v_usecount == 0 && vq != vp) { 1287 simple_unlock(&spechash_slock); 1288 vgone(vq); 1289 goto loop; 1290 } 1291 count += vq->v_usecount; 1292 } 1293 simple_unlock(&spechash_slock); 1294 return (count); 1295 } 1296 1297 /* 1298 * Print out a description of a vnode. 1299 */ 1300 static char *typename[] = 1301 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" }; 1302 1303 void 1304 vprint(label, vp) 1305 char *label; 1306 register struct vnode *vp; 1307 { 1308 char buf[64]; 1309 1310 if (label != NULL) 1311 printf("%s: ", label); 1312 printf("type %s, usecount %d, writecount %d, refcount %d,", 1313 typename[vp->v_type], vp->v_usecount, vp->v_writecount, 1314 vp->v_holdcnt); 1315 buf[0] = '\0'; 1316 if (vp->v_flag & VROOT) 1317 strcat(buf, "|VROOT"); 1318 if (vp->v_flag & VTEXT) 1319 strcat(buf, "|VTEXT"); 1320 if (vp->v_flag & VSYSTEM) 1321 strcat(buf, "|VSYSTEM"); 1322 if (vp->v_flag & VXLOCK) 1323 strcat(buf, "|VXLOCK"); 1324 if (vp->v_flag & VXWANT) 1325 strcat(buf, "|VXWANT"); 1326 if (vp->v_flag & VBWAIT) 1327 strcat(buf, "|VBWAIT"); 1328 if (vp->v_flag & VALIASED) 1329 strcat(buf, "|VALIASED"); 1330 if (buf[0] != '\0') 1331 printf(" flags (%s)", &buf[1]); 1332 if (vp->v_data == NULL) { 1333 printf("\n"); 1334 } else { 1335 printf("\n\t"); 1336 VOP_PRINT(vp); 1337 } 1338 } 1339 1340 #ifdef DEBUG 1341 /* 1342 * List all of the locked vnodes in the system. 1343 * Called when debugging the kernel. 1344 */ 1345 void 1346 printlockedvnodes() 1347 { 1348 register struct mount *mp; 1349 register struct vnode *vp; 1350 1351 printf("Locked vnodes\n"); 1352 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; 1353 mp = mp->mnt_list.cqe_next) { 1354 for (vp = mp->mnt_vnodelist.lh_first; 1355 vp != NULL; 1356 vp = vp->v_mntvnodes.le_next) { 1357 if (VOP_ISLOCKED(vp)) 1358 vprint((char *)0, vp); 1359 } 1360 } 1361 } 1362 #endif 1363 1364 /* 1365 * Top level filesystem related information gathering. 1366 */ 1367 int 1368 vfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 1369 int *name; 1370 u_int namelen; 1371 void *oldp; 1372 size_t *oldlenp; 1373 void *newp; 1374 size_t newlen; 1375 struct proc *p; 1376 { 1377 struct ctldebug *cdp; 1378 struct vfsconf *vfsp; 1379 1380 /* all sysctl names at this level are at least name and field */ 1381 if (namelen < 2) 1382 return (ENOTDIR); /* overloaded */ 1383 if (name[0] != VFS_GENERIC) { 1384 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 1385 if (vfsp->vfc_typenum == name[0]) 1386 break; 1387 if (vfsp == NULL) 1388 return (EOPNOTSUPP); 1389 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 1390 oldp, oldlenp, newp, newlen, p)); 1391 } 1392 switch (name[1]) { 1393 case VFS_MAXTYPENUM: 1394 return (sysctl_rdint(oldp, oldlenp, newp, maxvfsconf)); 1395 case VFS_CONF: 1396 if (namelen < 3) 1397 return (ENOTDIR); /* overloaded */ 1398 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 1399 if (vfsp->vfc_typenum == name[2]) 1400 break; 1401 if (vfsp == NULL) 1402 return (EOPNOTSUPP); 1403 return (sysctl_rdstruct(oldp, oldlenp, newp, vfsp, 1404 sizeof(struct vfsconf))); 1405 } 1406 return (EOPNOTSUPP); 1407 } 1408 1409 int kinfo_vdebug = 1; 1410 int kinfo_vgetfailed; 1411 #define KINFO_VNODESLOP 10 1412 /* 1413 * Dump vnode list (via sysctl). 1414 * Copyout address of vnode followed by vnode. 1415 */ 1416 /* ARGSUSED */ 1417 int 1418 sysctl_vnode(where, sizep) 1419 char *where; 1420 size_t *sizep; 1421 { 1422 register struct mount *mp, *nmp; 1423 struct vnode *nvp, *vp; 1424 register char *bp = where, *savebp; 1425 char *ewhere; 1426 int error; 1427 1428 #define VPTRSZ sizeof (struct vnode *) 1429 #define VNODESZ sizeof (struct vnode) 1430 if (where == NULL) { 1431 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ); 1432 return (0); 1433 } 1434 ewhere = where + *sizep; 1435 1436 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { 1437 nmp = mp->mnt_list.cqe_next; 1438 if (vfs_busy(mp)) 1439 continue; 1440 savebp = bp; 1441 again: 1442 simple_lock(&mntvnode_slock); 1443 for (vp = mp->mnt_vnodelist.lh_first; 1444 vp != NULL; 1445 vp = nvp) { 1446 /* 1447 * Check that the vp is still associated with 1448 * this filesystem. RACE: could have been 1449 * recycled onto the same filesystem. 1450 */ 1451 if (vp->v_mount != mp) { 1452 simple_unlock(&mntvnode_slock); 1453 if (kinfo_vdebug) 1454 printf("kinfo: vp changed\n"); 1455 bp = savebp; 1456 goto again; 1457 } 1458 nvp = vp->v_mntvnodes.le_next; 1459 if (bp + VPTRSZ + VNODESZ > ewhere) { 1460 simple_unlock(&mntvnode_slock); 1461 *sizep = bp - where; 1462 return (ENOMEM); 1463 } 1464 simple_unlock(&mntvnode_slock); 1465 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) || 1466 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ))) 1467 return (error); 1468 bp += VPTRSZ + VNODESZ; 1469 simple_lock(&mntvnode_slock); 1470 } 1471 simple_unlock(&mntvnode_slock); 1472 vfs_unbusy(mp); 1473 } 1474 1475 *sizep = bp - where; 1476 return (0); 1477 } 1478 1479 /* 1480 * Check to see if a filesystem is mounted on a block device. 1481 */ 1482 int 1483 vfs_mountedon(vp) 1484 struct vnode *vp; 1485 { 1486 struct vnode *vq; 1487 int error = 0; 1488 1489 if (vp->v_specflags & SI_MOUNTEDON) 1490 return (EBUSY); 1491 if (vp->v_flag & VALIASED) { 1492 simple_lock(&spechash_slock); 1493 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1494 if (vq->v_rdev != vp->v_rdev || 1495 vq->v_type != vp->v_type) 1496 continue; 1497 if (vq->v_specflags & SI_MOUNTEDON) { 1498 error = EBUSY; 1499 break; 1500 } 1501 } 1502 simple_unlock(&spechash_slock); 1503 } 1504 return (error); 1505 } 1506 1507 /* 1508 * Unmount all filesystems. The list is traversed in reverse order 1509 * of mounting to avoid dependencies. 1510 */ 1511 void 1512 vfs_unmountall() 1513 { 1514 struct mount *mp, *nmp; 1515 1516 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) { 1517 nmp = mp->mnt_list.cqe_prev; 1518 (void) dounmount(mp, MNT_FORCE, &proc0); 1519 } 1520 } 1521 1522 /* 1523 * Build hash lists of net addresses and hang them off the mount point. 1524 * Called by ufs_mount() to set up the lists of export addresses. 1525 */ 1526 static int 1527 vfs_hang_addrlist(mp, nep, argp) 1528 struct mount *mp; 1529 struct netexport *nep; 1530 struct export_args *argp; 1531 { 1532 register struct netcred *np; 1533 register struct radix_node_head *rnh; 1534 register int i; 1535 struct radix_node *rn; 1536 struct sockaddr *saddr, *smask = 0; 1537 struct domain *dom; 1538 int error; 1539 1540 if (argp->ex_addrlen == 0) { 1541 if (mp->mnt_flag & MNT_DEFEXPORTED) 1542 return (EPERM); 1543 np = &nep->ne_defexported; 1544 np->netc_exflags = argp->ex_flags; 1545 np->netc_anon = argp->ex_anon; 1546 np->netc_anon.cr_ref = 1; 1547 mp->mnt_flag |= MNT_DEFEXPORTED; 1548 return (0); 1549 } 1550 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1551 np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK); 1552 bzero((caddr_t)np, i); 1553 saddr = (struct sockaddr *)(np + 1); 1554 if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen)) 1555 goto out; 1556 if (saddr->sa_len > argp->ex_addrlen) 1557 saddr->sa_len = argp->ex_addrlen; 1558 if (argp->ex_masklen) { 1559 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 1560 error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen); 1561 if (error) 1562 goto out; 1563 if (smask->sa_len > argp->ex_masklen) 1564 smask->sa_len = argp->ex_masklen; 1565 } 1566 i = saddr->sa_family; 1567 if ((rnh = nep->ne_rtable[i]) == 0) { 1568 /* 1569 * Seems silly to initialize every AF when most are not 1570 * used, do so on demand here 1571 */ 1572 for (dom = domains; dom; dom = dom->dom_next) 1573 if (dom->dom_family == i && dom->dom_rtattach) { 1574 dom->dom_rtattach((void **)&nep->ne_rtable[i], 1575 dom->dom_rtoffset); 1576 break; 1577 } 1578 if ((rnh = nep->ne_rtable[i]) == 0) { 1579 error = ENOBUFS; 1580 goto out; 1581 } 1582 } 1583 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, 1584 np->netc_rnodes); 1585 if (rn == 0) { 1586 /* 1587 * One of the reasons that rnh_addaddr may fail is that 1588 * the entry already exists. To check for this case, we 1589 * look up the entry to see if it is there. If so, we 1590 * do not need to make a new entry but do return success. 1591 */ 1592 free(np, M_NETADDR); 1593 rn = (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh); 1594 if (rn != 0 && (rn->rn_flags & RNF_ROOT) == 0 && 1595 ((struct netcred *)rn)->netc_exflags == argp->ex_flags && 1596 !bcmp((caddr_t)&((struct netcred *)rn)->netc_anon, 1597 (caddr_t)&argp->ex_anon, sizeof(struct ucred))) 1598 return (0); 1599 return (EPERM); 1600 } 1601 np->netc_exflags = argp->ex_flags; 1602 np->netc_anon = argp->ex_anon; 1603 np->netc_anon.cr_ref = 1; 1604 return (0); 1605 out: 1606 free(np, M_NETADDR); 1607 return (error); 1608 } 1609 1610 /* ARGSUSED */ 1611 static int 1612 vfs_free_netcred(rn, w) 1613 struct radix_node *rn; 1614 caddr_t w; 1615 { 1616 register struct radix_node_head *rnh = (struct radix_node_head *)w; 1617 1618 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh); 1619 free((caddr_t)rn, M_NETADDR); 1620 return (0); 1621 } 1622 1623 /* 1624 * Free the net address hash lists that are hanging off the mount points. 1625 */ 1626 static void 1627 vfs_free_addrlist(nep) 1628 struct netexport *nep; 1629 { 1630 register int i; 1631 register struct radix_node_head *rnh; 1632 1633 for (i = 0; i <= AF_MAX; i++) 1634 if (rnh = nep->ne_rtable[i]) { 1635 (*rnh->rnh_walktree)(rnh, vfs_free_netcred, 1636 (caddr_t)rnh); 1637 free((caddr_t)rnh, M_RTABLE); 1638 nep->ne_rtable[i] = 0; 1639 } 1640 } 1641 1642 int 1643 vfs_export(mp, nep, argp) 1644 struct mount *mp; 1645 struct netexport *nep; 1646 struct export_args *argp; 1647 { 1648 int error; 1649 1650 if (argp->ex_flags & MNT_DELEXPORT) { 1651 vfs_free_addrlist(nep); 1652 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 1653 } 1654 if (argp->ex_flags & MNT_EXPORTED) { 1655 if (error = vfs_hang_addrlist(mp, nep, argp)) 1656 return (error); 1657 mp->mnt_flag |= MNT_EXPORTED; 1658 } 1659 return (0); 1660 } 1661 1662 struct netcred * 1663 vfs_export_lookup(mp, nep, nam) 1664 register struct mount *mp; 1665 struct netexport *nep; 1666 struct mbuf *nam; 1667 { 1668 register struct netcred *np; 1669 register struct radix_node_head *rnh; 1670 struct sockaddr *saddr; 1671 1672 np = NULL; 1673 if (mp->mnt_flag & MNT_EXPORTED) { 1674 /* 1675 * Lookup in the export list first. 1676 */ 1677 if (nam != NULL) { 1678 saddr = mtod(nam, struct sockaddr *); 1679 rnh = nep->ne_rtable[saddr->sa_family]; 1680 if (rnh != NULL) { 1681 np = (struct netcred *) 1682 (*rnh->rnh_matchaddr)((caddr_t)saddr, 1683 rnh); 1684 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 1685 np = NULL; 1686 } 1687 } 1688 /* 1689 * If no address match, use the default if it exists. 1690 */ 1691 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 1692 np = &nep->ne_defexported; 1693 } 1694 return (np); 1695 } 1696