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.14 (Berkeley) 01/09/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 57 TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */ 58 struct mntlist mountlist; /* mounted filesystem list */ 59 60 /* 61 * Initialize the vnode management data structures. 62 */ 63 vntblinit() 64 { 65 66 TAILQ_INIT(&vnode_free_list); 67 TAILQ_INIT(&mountlist); 68 } 69 70 /* 71 * Lock a filesystem. 72 * Used to prevent access to it while mounting and unmounting. 73 */ 74 vfs_lock(mp) 75 register struct mount *mp; 76 { 77 78 while(mp->mnt_flag & MNT_MLOCK) { 79 mp->mnt_flag |= MNT_MWAIT; 80 sleep((caddr_t)mp, PVFS); 81 } 82 mp->mnt_flag |= MNT_MLOCK; 83 return (0); 84 } 85 86 /* 87 * Unlock a locked filesystem. 88 * Panic if filesystem is not locked. 89 */ 90 void 91 vfs_unlock(mp) 92 register struct mount *mp; 93 { 94 95 if ((mp->mnt_flag & MNT_MLOCK) == 0) 96 panic("vfs_unlock: not locked"); 97 mp->mnt_flag &= ~MNT_MLOCK; 98 if (mp->mnt_flag & MNT_MWAIT) { 99 mp->mnt_flag &= ~MNT_MWAIT; 100 wakeup((caddr_t)mp); 101 } 102 } 103 104 /* 105 * Mark a mount point as busy. 106 * Used to synchronize access and to delay unmounting. 107 */ 108 vfs_busy(mp) 109 register struct mount *mp; 110 { 111 112 while(mp->mnt_flag & MNT_MPBUSY) { 113 mp->mnt_flag |= MNT_MPWANT; 114 sleep((caddr_t)&mp->mnt_flag, PVFS); 115 } 116 if (mp->mnt_flag & MNT_UNMOUNT) 117 return (1); 118 mp->mnt_flag |= MNT_MPBUSY; 119 return (0); 120 } 121 122 /* 123 * Free a busy filesystem. 124 * Panic if filesystem is not busy. 125 */ 126 vfs_unbusy(mp) 127 register struct mount *mp; 128 { 129 130 if ((mp->mnt_flag & MNT_MPBUSY) == 0) 131 panic("vfs_unbusy: not busy"); 132 mp->mnt_flag &= ~MNT_MPBUSY; 133 if (mp->mnt_flag & MNT_MPWANT) { 134 mp->mnt_flag &= ~MNT_MPWANT; 135 wakeup((caddr_t)&mp->mnt_flag); 136 } 137 } 138 139 /* 140 * Lookup a mount point by filesystem identifier. 141 */ 142 struct mount * 143 getvfs(fsid) 144 fsid_t *fsid; 145 { 146 register struct mount *mp; 147 148 for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) { 149 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 150 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) 151 return (mp); 152 } 153 return ((struct mount *)0); 154 } 155 156 /* 157 * Get a new unique fsid 158 */ 159 void 160 getnewfsid(mp, mtype) 161 struct mount *mp; 162 int mtype; 163 { 164 static u_short xxxfs_mntid; 165 166 fsid_t tfsid; 167 168 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0); 169 mp->mnt_stat.f_fsid.val[1] = mtype; 170 if (xxxfs_mntid == 0) 171 ++xxxfs_mntid; 172 tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid); 173 tfsid.val[1] = mtype; 174 if (mountlist.tqh_first != NULL) { 175 while (getvfs(&tfsid)) { 176 tfsid.val[0]++; 177 xxxfs_mntid++; 178 } 179 } 180 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 181 } 182 183 /* 184 * Set vnode attributes to VNOVAL 185 */ 186 void vattr_null(vap) 187 register struct vattr *vap; 188 { 189 190 vap->va_type = VNON; 191 vap->va_size = vap->va_bytes = VNOVAL; 192 vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid = 193 vap->va_fsid = vap->va_fileid = 194 vap->va_blocksize = vap->va_rdev = 195 vap->va_atime.ts_sec = vap->va_atime.ts_nsec = 196 vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec = 197 vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec = 198 vap->va_flags = vap->va_gen = VNOVAL; 199 vap->va_vaflags = 0; 200 } 201 202 /* 203 * Routines having to do with the management of the vnode table. 204 */ 205 extern int (**dead_vnodeop_p)(); 206 extern void vclean(); 207 long numvnodes; 208 extern struct vattr va_null; 209 210 /* 211 * Return the next vnode from the free list. 212 */ 213 getnewvnode(tag, mp, vops, vpp) 214 enum vtagtype tag; 215 struct mount *mp; 216 int (**vops)(); 217 struct vnode **vpp; 218 { 219 register struct vnode *vp; 220 int s; 221 222 if ((vnode_free_list.tqh_first == NULL && 223 numvnodes < 2 * desiredvnodes) || 224 numvnodes < desiredvnodes) { 225 vp = (struct vnode *)malloc((u_long)sizeof *vp, 226 M_VNODE, M_WAITOK); 227 bzero((char *)vp, sizeof *vp); 228 numvnodes++; 229 } else { 230 if ((vp = vnode_free_list.tqh_first) == NULL) { 231 tablefull("vnode"); 232 *vpp = 0; 233 return (ENFILE); 234 } 235 if (vp->v_usecount) 236 panic("free vnode isn't"); 237 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 238 /* see comment on why 0xdeadb is set at end of vgone (below) */ 239 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb; 240 vp->v_lease = NULL; 241 if (vp->v_type != VBAD) 242 vgone(vp); 243 #ifdef DIAGNOSTIC 244 if (vp->v_data) 245 panic("cleaned vnode isn't"); 246 s = splbio(); 247 if (vp->v_numoutput) 248 panic("Clean vnode has pending I/O's"); 249 splx(s); 250 #endif 251 vp->v_flag = 0; 252 vp->v_lastr = 0; 253 vp->v_ralen = 0; 254 vp->v_maxra = 0; 255 vp->v_lastw = 0; 256 vp->v_lasta = 0; 257 vp->v_cstart = 0; 258 vp->v_clen = 0; 259 vp->v_socket = 0; 260 } 261 vp->v_type = VNON; 262 cache_purge(vp); 263 vp->v_tag = tag; 264 vp->v_op = vops; 265 insmntque(vp, mp); 266 *vpp = vp; 267 vp->v_usecount = 1; 268 vp->v_data = 0; 269 return (0); 270 } 271 272 /* 273 * Move a vnode from one mount queue to another. 274 */ 275 insmntque(vp, mp) 276 register struct vnode *vp; 277 register struct mount *mp; 278 { 279 280 /* 281 * Delete from old mount point vnode list, if on one. 282 */ 283 if (vp->v_mount != NULL) 284 LIST_REMOVE(vp, v_mntvnodes); 285 /* 286 * Insert into list of vnodes for the new mount point, if available. 287 */ 288 if ((vp->v_mount = mp) == NULL) 289 return; 290 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes); 291 } 292 293 /* 294 * Update outstanding I/O count and do wakeup if requested. 295 */ 296 vwakeup(bp) 297 register struct buf *bp; 298 { 299 register struct vnode *vp; 300 301 bp->b_flags &= ~B_WRITEINPROG; 302 if (vp = bp->b_vp) { 303 vp->v_numoutput--; 304 if (vp->v_numoutput < 0) 305 panic("vwakeup: neg numoutput"); 306 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) { 307 if (vp->v_numoutput < 0) 308 panic("vwakeup: neg numoutput"); 309 vp->v_flag &= ~VBWAIT; 310 wakeup((caddr_t)&vp->v_numoutput); 311 } 312 } 313 } 314 315 /* 316 * Flush out and invalidate all buffers associated with a vnode. 317 * Called with the underlying object locked. 318 */ 319 int 320 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo) 321 register struct vnode *vp; 322 int flags; 323 struct ucred *cred; 324 struct proc *p; 325 int slpflag, slptimeo; 326 { 327 register struct buf *bp; 328 struct buf *nbp, *blist; 329 int s, error; 330 331 if (flags & V_SAVE) { 332 if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) 333 return (error); 334 if (vp->v_dirtyblkhd.lh_first != NULL) 335 panic("vinvalbuf: dirty bufs"); 336 } 337 for (;;) { 338 if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA) 339 while (blist && blist->b_lblkno < 0) 340 blist = blist->b_vnbufs.le_next; 341 if (!blist && (blist = vp->v_dirtyblkhd.lh_first) && 342 (flags & V_SAVEMETA)) 343 while (blist && blist->b_lblkno < 0) 344 blist = blist->b_vnbufs.le_next; 345 if (!blist) 346 break; 347 348 for (bp = blist; bp; bp = nbp) { 349 nbp = bp->b_vnbufs.le_next; 350 if (flags & V_SAVEMETA && bp->b_lblkno < 0) 351 continue; 352 s = splbio(); 353 if (bp->b_flags & B_BUSY) { 354 bp->b_flags |= B_WANTED; 355 error = tsleep((caddr_t)bp, 356 slpflag | (PRIBIO + 1), "vinvalbuf", 357 slptimeo); 358 splx(s); 359 if (error) 360 return (error); 361 break; 362 } 363 bremfree(bp); 364 bp->b_flags |= B_BUSY; 365 splx(s); 366 /* 367 * XXX Since there are no node locks for NFS, I believe 368 * there is a slight chance that a delayed write will 369 * occur while sleeping just above, so check for it. 370 */ 371 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 372 (void) VOP_BWRITE(bp); 373 break; 374 } 375 bp->b_flags |= B_INVAL; 376 brelse(bp); 377 } 378 } 379 if (!(flags & V_SAVEMETA) && 380 (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first)) 381 panic("vinvalbuf: flush failed"); 382 return (0); 383 } 384 385 /* 386 * Associate a buffer with a vnode. 387 */ 388 bgetvp(vp, bp) 389 register struct vnode *vp; 390 register struct buf *bp; 391 { 392 393 if (bp->b_vp) 394 panic("bgetvp: not free"); 395 VHOLD(vp); 396 bp->b_vp = vp; 397 if (vp->v_type == VBLK || vp->v_type == VCHR) 398 bp->b_dev = vp->v_rdev; 399 else 400 bp->b_dev = NODEV; 401 /* 402 * Insert onto list for new vnode. 403 */ 404 bufinsvn(bp, &vp->v_cleanblkhd); 405 } 406 407 /* 408 * Disassociate a buffer from a vnode. 409 */ 410 brelvp(bp) 411 register struct buf *bp; 412 { 413 struct vnode *vp; 414 415 if (bp->b_vp == (struct vnode *) 0) 416 panic("brelvp: NULL"); 417 /* 418 * Delete from old vnode list, if on one. 419 */ 420 if (bp->b_vnbufs.le_next != NOLIST) 421 bufremvn(bp); 422 vp = bp->b_vp; 423 bp->b_vp = (struct vnode *) 0; 424 HOLDRELE(vp); 425 } 426 427 /* 428 * Reassign a buffer from one vnode to another. 429 * Used to assign file specific control information 430 * (indirect blocks) to the vnode to which they belong. 431 */ 432 reassignbuf(bp, newvp) 433 register struct buf *bp; 434 register struct vnode *newvp; 435 { 436 register struct buflists *listheadp; 437 438 if (newvp == NULL) { 439 printf("reassignbuf: NULL"); 440 return; 441 } 442 /* 443 * Delete from old vnode list, if on one. 444 */ 445 if (bp->b_vnbufs.le_next != NOLIST) 446 bufremvn(bp); 447 /* 448 * If dirty, put on list of dirty buffers; 449 * otherwise insert onto list of clean buffers. 450 */ 451 if (bp->b_flags & B_DELWRI) 452 listheadp = &newvp->v_dirtyblkhd; 453 else 454 listheadp = &newvp->v_cleanblkhd; 455 bufinsvn(bp, listheadp); 456 } 457 458 /* 459 * Create a vnode for a block device. 460 * Used for root filesystem, argdev, and swap areas. 461 * Also used for memory file system special devices. 462 */ 463 bdevvp(dev, vpp) 464 dev_t dev; 465 struct vnode **vpp; 466 { 467 register struct vnode *vp; 468 struct vnode *nvp; 469 int error; 470 471 if (dev == NODEV) 472 return (0); 473 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp); 474 if (error) { 475 *vpp = 0; 476 return (error); 477 } 478 vp = nvp; 479 vp->v_type = VBLK; 480 if (nvp = checkalias(vp, dev, (struct mount *)0)) { 481 vput(vp); 482 vp = nvp; 483 } 484 *vpp = vp; 485 return (0); 486 } 487 488 /* 489 * Check to see if the new vnode represents a special device 490 * for which we already have a vnode (either because of 491 * bdevvp() or because of a different vnode representing 492 * the same block device). If such an alias exists, deallocate 493 * the existing contents and return the aliased vnode. The 494 * caller is responsible for filling it with its new contents. 495 */ 496 struct vnode * 497 checkalias(nvp, nvp_rdev, mp) 498 register struct vnode *nvp; 499 dev_t nvp_rdev; 500 struct mount *mp; 501 { 502 register struct vnode *vp; 503 struct vnode **vpp; 504 505 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 506 return (NULLVP); 507 508 vpp = &speclisth[SPECHASH(nvp_rdev)]; 509 loop: 510 for (vp = *vpp; vp; vp = vp->v_specnext) { 511 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) 512 continue; 513 /* 514 * Alias, but not in use, so flush it out. 515 */ 516 if (vp->v_usecount == 0) { 517 vgone(vp); 518 goto loop; 519 } 520 if (vget(vp, 1)) 521 goto loop; 522 break; 523 } 524 if (vp == NULL || vp->v_tag != VT_NON) { 525 MALLOC(nvp->v_specinfo, struct specinfo *, 526 sizeof(struct specinfo), M_VNODE, M_WAITOK); 527 nvp->v_rdev = nvp_rdev; 528 nvp->v_hashchain = vpp; 529 nvp->v_specnext = *vpp; 530 nvp->v_specflags = 0; 531 *vpp = nvp; 532 if (vp != NULL) { 533 nvp->v_flag |= VALIASED; 534 vp->v_flag |= VALIASED; 535 vput(vp); 536 } 537 return (NULLVP); 538 } 539 VOP_UNLOCK(vp); 540 vclean(vp, 0); 541 vp->v_op = nvp->v_op; 542 vp->v_tag = nvp->v_tag; 543 nvp->v_type = VNON; 544 insmntque(vp, mp); 545 return (vp); 546 } 547 548 /* 549 * Grab a particular vnode from the free list, increment its 550 * reference count and lock it. The vnode lock bit is set the 551 * vnode is being eliminated in vgone. The process is awakened 552 * when the transition is completed, and an error returned to 553 * indicate that the vnode is no longer usable (possibly having 554 * been changed to a new file system type). 555 */ 556 vget(vp, lockflag) 557 register struct vnode *vp; 558 int lockflag; 559 { 560 561 /* 562 * If the vnode is in the process of being cleaned out for 563 * another use, we wait for the cleaning to finish and then 564 * return failure. Cleaning is determined either by checking 565 * that the VXLOCK flag is set, or that the use count is 566 * zero with the back pointer set to show that it has been 567 * removed from the free list by getnewvnode. The VXLOCK 568 * flag may not have been set yet because vclean is blocked in 569 * the VOP_LOCK call waiting for the VOP_INACTIVE to complete. 570 */ 571 if ((vp->v_flag & VXLOCK) || 572 (vp->v_usecount == 0 && 573 vp->v_freelist.tqe_prev == (struct vnode **)0xdeadb)) { 574 vp->v_flag |= VXWANT; 575 sleep((caddr_t)vp, PINOD); 576 return (1); 577 } 578 if (vp->v_usecount == 0) 579 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 580 vp->v_usecount++; 581 if (lockflag) 582 VOP_LOCK(vp); 583 return (0); 584 } 585 586 /* 587 * Vnode reference, just increment the count 588 */ 589 void vref(vp) 590 struct vnode *vp; 591 { 592 593 if (vp->v_usecount <= 0) 594 panic("vref used where vget required"); 595 vp->v_usecount++; 596 } 597 598 /* 599 * vput(), just unlock and vrele() 600 */ 601 void vput(vp) 602 register struct vnode *vp; 603 { 604 605 VOP_UNLOCK(vp); 606 vrele(vp); 607 } 608 609 /* 610 * Vnode release. 611 * If count drops to zero, call inactive routine and return to freelist. 612 */ 613 void vrele(vp) 614 register struct vnode *vp; 615 { 616 617 #ifdef DIAGNOSTIC 618 if (vp == NULL) 619 panic("vrele: null vp"); 620 #endif 621 vp->v_usecount--; 622 if (vp->v_usecount > 0) 623 return; 624 #ifdef DIAGNOSTIC 625 if (vp->v_usecount != 0 || vp->v_writecount != 0) { 626 vprint("vrele: bad ref count", vp); 627 panic("vrele: ref cnt"); 628 } 629 #endif 630 /* 631 * insert at tail of LRU list 632 */ 633 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 634 VOP_INACTIVE(vp); 635 } 636 637 /* 638 * Page or buffer structure gets a reference. 639 */ 640 void vhold(vp) 641 register struct vnode *vp; 642 { 643 644 vp->v_holdcnt++; 645 } 646 647 /* 648 * Page or buffer structure frees a reference. 649 */ 650 void holdrele(vp) 651 register struct vnode *vp; 652 { 653 654 if (vp->v_holdcnt <= 0) 655 panic("holdrele: holdcnt"); 656 vp->v_holdcnt--; 657 } 658 659 /* 660 * Remove any vnodes in the vnode table belonging to mount point mp. 661 * 662 * If MNT_NOFORCE is specified, there should not be any active ones, 663 * return error if any are found (nb: this is a user error, not a 664 * system error). If MNT_FORCE is specified, detach any active vnodes 665 * that are found. 666 */ 667 #ifdef DIAGNOSTIC 668 int busyprt = 0; /* print out busy vnodes */ 669 struct ctldebug debug1 = { "busyprt", &busyprt }; 670 #endif 671 672 vflush(mp, skipvp, flags) 673 struct mount *mp; 674 struct vnode *skipvp; 675 int flags; 676 { 677 register struct vnode *vp, *nvp; 678 int busy = 0; 679 680 if ((mp->mnt_flag & MNT_MPBUSY) == 0) 681 panic("vflush: not busy"); 682 loop: 683 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) { 684 if (vp->v_mount != mp) 685 goto loop; 686 nvp = vp->v_mntvnodes.le_next; 687 /* 688 * Skip over a selected vnode. 689 */ 690 if (vp == skipvp) 691 continue; 692 /* 693 * Skip over a vnodes marked VSYSTEM. 694 */ 695 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) 696 continue; 697 /* 698 * If WRITECLOSE is set, only flush out regular file 699 * vnodes open for writing. 700 */ 701 if ((flags & WRITECLOSE) && 702 (vp->v_writecount == 0 || vp->v_type != VREG)) 703 continue; 704 /* 705 * With v_usecount == 0, all we need to do is clear 706 * out the vnode data structures and we are done. 707 */ 708 if (vp->v_usecount == 0) { 709 vgone(vp); 710 continue; 711 } 712 /* 713 * If FORCECLOSE is set, forcibly close the vnode. 714 * For block or character devices, revert to an 715 * anonymous device. For all other files, just kill them. 716 */ 717 if (flags & FORCECLOSE) { 718 if (vp->v_type != VBLK && vp->v_type != VCHR) { 719 vgone(vp); 720 } else { 721 vclean(vp, 0); 722 vp->v_op = spec_vnodeop_p; 723 insmntque(vp, (struct mount *)0); 724 } 725 continue; 726 } 727 #ifdef DIAGNOSTIC 728 if (busyprt) 729 vprint("vflush: busy vnode", vp); 730 #endif 731 busy++; 732 } 733 if (busy) 734 return (EBUSY); 735 return (0); 736 } 737 738 /* 739 * Disassociate the underlying file system from a vnode. 740 */ 741 void 742 vclean(vp, flags) 743 register struct vnode *vp; 744 int flags; 745 { 746 int active; 747 748 /* 749 * Check to see if the vnode is in use. 750 * If so we have to reference it before we clean it out 751 * so that its count cannot fall to zero and generate a 752 * race against ourselves to recycle it. 753 */ 754 if (active = vp->v_usecount) 755 VREF(vp); 756 /* 757 * Even if the count is zero, the VOP_INACTIVE routine may still 758 * have the object locked while it cleans it out. The VOP_LOCK 759 * ensures that the VOP_INACTIVE routine is done with its work. 760 * For active vnodes, it ensures that no other activity can 761 * occur while the underlying object is being cleaned out. 762 */ 763 VOP_LOCK(vp); 764 /* 765 * Prevent the vnode from being recycled or 766 * brought into use while we clean it out. 767 */ 768 if (vp->v_flag & VXLOCK) 769 panic("vclean: deadlock"); 770 vp->v_flag |= VXLOCK; 771 /* 772 * Clean out any buffers associated with the vnode. 773 */ 774 if (flags & DOCLOSE) 775 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 776 /* 777 * Any other processes trying to obtain this lock must first 778 * wait for VXLOCK to clear, then call the new lock operation. 779 */ 780 VOP_UNLOCK(vp); 781 /* 782 * If purging an active vnode, it must be closed and 783 * deactivated before being reclaimed. 784 */ 785 if (active) { 786 if (flags & DOCLOSE) 787 VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL); 788 VOP_INACTIVE(vp); 789 } 790 /* 791 * Reclaim the vnode. 792 */ 793 if (VOP_RECLAIM(vp)) 794 panic("vclean: cannot reclaim"); 795 if (active) 796 vrele(vp); 797 798 /* 799 * Done with purge, notify sleepers of the grim news. 800 */ 801 vp->v_op = dead_vnodeop_p; 802 vp->v_tag = VT_NON; 803 vp->v_flag &= ~VXLOCK; 804 if (vp->v_flag & VXWANT) { 805 vp->v_flag &= ~VXWANT; 806 wakeup((caddr_t)vp); 807 } 808 } 809 810 /* 811 * Eliminate all activity associated with the requested vnode 812 * and with all vnodes aliased to the requested vnode. 813 */ 814 void vgoneall(vp) 815 register struct vnode *vp; 816 { 817 register struct vnode *vq; 818 819 if (vp->v_flag & VALIASED) { 820 /* 821 * If a vgone (or vclean) is already in progress, 822 * wait until it is done and return. 823 */ 824 if (vp->v_flag & VXLOCK) { 825 vp->v_flag |= VXWANT; 826 sleep((caddr_t)vp, PINOD); 827 return; 828 } 829 /* 830 * Ensure that vp will not be vgone'd while we 831 * are eliminating its aliases. 832 */ 833 vp->v_flag |= VXLOCK; 834 while (vp->v_flag & VALIASED) { 835 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 836 if (vq->v_rdev != vp->v_rdev || 837 vq->v_type != vp->v_type || vp == vq) 838 continue; 839 vgone(vq); 840 break; 841 } 842 } 843 /* 844 * Remove the lock so that vgone below will 845 * really eliminate the vnode after which time 846 * vgone will awaken any sleepers. 847 */ 848 vp->v_flag &= ~VXLOCK; 849 } 850 vgone(vp); 851 } 852 853 /* 854 * Eliminate all activity associated with a vnode 855 * in preparation for reuse. 856 */ 857 void vgone(vp) 858 register struct vnode *vp; 859 { 860 register struct vnode *vq; 861 struct vnode *vx; 862 863 /* 864 * If a vgone (or vclean) is already in progress, 865 * wait until it is done and return. 866 */ 867 if (vp->v_flag & VXLOCK) { 868 vp->v_flag |= VXWANT; 869 sleep((caddr_t)vp, PINOD); 870 return; 871 } 872 /* 873 * Clean out the filesystem specific data. 874 */ 875 vclean(vp, DOCLOSE); 876 /* 877 * Delete from old mount point vnode list, if on one. 878 */ 879 if (vp->v_mount != NULL) { 880 LIST_REMOVE(vp, v_mntvnodes); 881 vp->v_mount = NULL; 882 } 883 /* 884 * If special device, remove it from special device alias list. 885 */ 886 if (vp->v_type == VBLK || vp->v_type == VCHR) { 887 if (*vp->v_hashchain == vp) { 888 *vp->v_hashchain = vp->v_specnext; 889 } else { 890 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 891 if (vq->v_specnext != vp) 892 continue; 893 vq->v_specnext = vp->v_specnext; 894 break; 895 } 896 if (vq == NULL) 897 panic("missing bdev"); 898 } 899 if (vp->v_flag & VALIASED) { 900 vx = NULL; 901 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 902 if (vq->v_rdev != vp->v_rdev || 903 vq->v_type != vp->v_type) 904 continue; 905 if (vx) 906 break; 907 vx = vq; 908 } 909 if (vx == NULL) 910 panic("missing alias"); 911 if (vq == NULL) 912 vx->v_flag &= ~VALIASED; 913 vp->v_flag &= ~VALIASED; 914 } 915 FREE(vp->v_specinfo, M_VNODE); 916 vp->v_specinfo = NULL; 917 } 918 /* 919 * If it is on the freelist and not already at the head, 920 * move it to the head of the list. The test of the back 921 * pointer and the reference count of zero is because 922 * it will be removed from the free list by getnewvnode, 923 * but will not have its reference count incremented until 924 * after calling vgone. If the reference count were 925 * incremented first, vgone would (incorrectly) try to 926 * close the previous instance of the underlying object. 927 * So, the back pointer is explicitly set to `0xdeadb' in 928 * getnewvnode after removing it from the freelist to ensure 929 * that we do not try to move it here. 930 */ 931 if (vp->v_usecount == 0 && 932 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb && 933 vnode_free_list.tqh_first != vp) { 934 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 935 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 936 } 937 vp->v_type = VBAD; 938 } 939 940 /* 941 * Lookup a vnode by device number. 942 */ 943 vfinddev(dev, type, vpp) 944 dev_t dev; 945 enum vtype type; 946 struct vnode **vpp; 947 { 948 register struct vnode *vp; 949 950 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 951 if (dev != vp->v_rdev || type != vp->v_type) 952 continue; 953 *vpp = vp; 954 return (1); 955 } 956 return (0); 957 } 958 959 /* 960 * Calculate the total number of references to a special device. 961 */ 962 vcount(vp) 963 register struct vnode *vp; 964 { 965 register struct vnode *vq, *vnext; 966 int count; 967 968 loop: 969 if ((vp->v_flag & VALIASED) == 0) 970 return (vp->v_usecount); 971 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) { 972 vnext = vq->v_specnext; 973 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 974 continue; 975 /* 976 * Alias, but not in use, so flush it out. 977 */ 978 if (vq->v_usecount == 0 && vq != vp) { 979 vgone(vq); 980 goto loop; 981 } 982 count += vq->v_usecount; 983 } 984 return (count); 985 } 986 987 /* 988 * Print out a description of a vnode. 989 */ 990 static char *typename[] = 991 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" }; 992 993 void 994 vprint(label, vp) 995 char *label; 996 register struct vnode *vp; 997 { 998 char buf[64]; 999 1000 if (label != NULL) 1001 printf("%s: ", label); 1002 printf("type %s, usecount %d, writecount %d, refcount %d,", 1003 typename[vp->v_type], vp->v_usecount, vp->v_writecount, 1004 vp->v_holdcnt); 1005 buf[0] = '\0'; 1006 if (vp->v_flag & VROOT) 1007 strcat(buf, "|VROOT"); 1008 if (vp->v_flag & VTEXT) 1009 strcat(buf, "|VTEXT"); 1010 if (vp->v_flag & VSYSTEM) 1011 strcat(buf, "|VSYSTEM"); 1012 if (vp->v_flag & VXLOCK) 1013 strcat(buf, "|VXLOCK"); 1014 if (vp->v_flag & VXWANT) 1015 strcat(buf, "|VXWANT"); 1016 if (vp->v_flag & VBWAIT) 1017 strcat(buf, "|VBWAIT"); 1018 if (vp->v_flag & VALIASED) 1019 strcat(buf, "|VALIASED"); 1020 if (buf[0] != '\0') 1021 printf(" flags (%s)", &buf[1]); 1022 if (vp->v_data == NULL) { 1023 printf("\n"); 1024 } else { 1025 printf("\n\t"); 1026 VOP_PRINT(vp); 1027 } 1028 } 1029 1030 #ifdef DEBUG 1031 /* 1032 * List all of the locked vnodes in the system. 1033 * Called when debugging the kernel. 1034 */ 1035 printlockedvnodes() 1036 { 1037 register struct mount *mp; 1038 register struct vnode *vp; 1039 1040 printf("Locked vnodes\n"); 1041 for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) { 1042 for (vp = mp->mnt_vnodelist.lh_first; 1043 vp != NULL; 1044 vp = vp->v_mntvnodes.le_next) 1045 if (VOP_ISLOCKED(vp)) 1046 vprint((char *)0, vp); 1047 } 1048 } 1049 #endif 1050 1051 int kinfo_vdebug = 1; 1052 int kinfo_vgetfailed; 1053 #define KINFO_VNODESLOP 10 1054 /* 1055 * Dump vnode list (via sysctl). 1056 * Copyout address of vnode followed by vnode. 1057 */ 1058 /* ARGSUSED */ 1059 sysctl_vnode(where, sizep) 1060 char *where; 1061 size_t *sizep; 1062 { 1063 register struct mount *mp, *nmp; 1064 struct vnode *vp; 1065 register char *bp = where, *savebp; 1066 char *ewhere; 1067 int error; 1068 1069 #define VPTRSZ sizeof (struct vnode *) 1070 #define VNODESZ sizeof (struct vnode) 1071 if (where == NULL) { 1072 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ); 1073 return (0); 1074 } 1075 ewhere = where + *sizep; 1076 1077 for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) { 1078 nmp = mp->mnt_list.tqe_next; 1079 if (vfs_busy(mp)) 1080 continue; 1081 savebp = bp; 1082 again: 1083 for (vp = mp->mnt_vnodelist.lh_first; 1084 vp != NULL; 1085 vp = vp->v_mntvnodes.le_next) { 1086 /* 1087 * Check that the vp is still associated with 1088 * this filesystem. RACE: could have been 1089 * recycled onto the same filesystem. 1090 */ 1091 if (vp->v_mount != mp) { 1092 if (kinfo_vdebug) 1093 printf("kinfo: vp changed\n"); 1094 bp = savebp; 1095 goto again; 1096 } 1097 if (bp + VPTRSZ + VNODESZ > ewhere) { 1098 *sizep = bp - where; 1099 return (ENOMEM); 1100 } 1101 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) || 1102 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ))) 1103 return (error); 1104 bp += VPTRSZ + VNODESZ; 1105 } 1106 vfs_unbusy(mp); 1107 } 1108 1109 *sizep = bp - where; 1110 return (0); 1111 } 1112 1113 /* 1114 * Check to see if a filesystem is mounted on a block device. 1115 */ 1116 int 1117 vfs_mountedon(vp) 1118 register struct vnode *vp; 1119 { 1120 register struct vnode *vq; 1121 1122 if (vp->v_specflags & SI_MOUNTEDON) 1123 return (EBUSY); 1124 if (vp->v_flag & VALIASED) { 1125 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 1126 if (vq->v_rdev != vp->v_rdev || 1127 vq->v_type != vp->v_type) 1128 continue; 1129 if (vq->v_specflags & SI_MOUNTEDON) 1130 return (EBUSY); 1131 } 1132 } 1133 return (0); 1134 } 1135 1136 /* 1137 * Build hash lists of net addresses and hang them off the mount point. 1138 * Called by ufs_mount() to set up the lists of export addresses. 1139 */ 1140 static int 1141 vfs_hang_addrlist(mp, nep, argp) 1142 struct mount *mp; 1143 struct netexport *nep; 1144 struct export_args *argp; 1145 { 1146 register struct netcred *np; 1147 register struct radix_node_head *rnh; 1148 register int i; 1149 struct radix_node *rn; 1150 struct sockaddr *saddr, *smask = 0; 1151 struct domain *dom; 1152 int error; 1153 1154 if (argp->ex_addrlen == 0) { 1155 if (mp->mnt_flag & MNT_DEFEXPORTED) 1156 return (EPERM); 1157 np = &nep->ne_defexported; 1158 np->netc_exflags = argp->ex_flags; 1159 np->netc_anon = argp->ex_anon; 1160 np->netc_anon.cr_ref = 1; 1161 mp->mnt_flag |= MNT_DEFEXPORTED; 1162 return (0); 1163 } 1164 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 1165 np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK); 1166 bzero((caddr_t)np, i); 1167 saddr = (struct sockaddr *)(np + 1); 1168 if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen)) 1169 goto out; 1170 if (saddr->sa_len > argp->ex_addrlen) 1171 saddr->sa_len = argp->ex_addrlen; 1172 if (argp->ex_masklen) { 1173 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 1174 error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen); 1175 if (error) 1176 goto out; 1177 if (smask->sa_len > argp->ex_masklen) 1178 smask->sa_len = argp->ex_masklen; 1179 } 1180 i = saddr->sa_family; 1181 if ((rnh = nep->ne_rtable[i]) == 0) { 1182 /* 1183 * Seems silly to initialize every AF when most are not 1184 * used, do so on demand here 1185 */ 1186 for (dom = domains; dom; dom = dom->dom_next) 1187 if (dom->dom_family == i && dom->dom_rtattach) { 1188 dom->dom_rtattach((void **)&nep->ne_rtable[i], 1189 dom->dom_rtoffset); 1190 break; 1191 } 1192 if ((rnh = nep->ne_rtable[i]) == 0) { 1193 error = ENOBUFS; 1194 goto out; 1195 } 1196 } 1197 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, 1198 np->netc_rnodes); 1199 if (rn == 0 || np != (struct netcred *)rn) { /* already exists */ 1200 error = EPERM; 1201 goto out; 1202 } 1203 np->netc_exflags = argp->ex_flags; 1204 np->netc_anon = argp->ex_anon; 1205 np->netc_anon.cr_ref = 1; 1206 return (0); 1207 out: 1208 free(np, M_NETADDR); 1209 return (error); 1210 } 1211 1212 /* ARGSUSED */ 1213 static int 1214 vfs_free_netcred(rn, w) 1215 struct radix_node *rn; 1216 caddr_t w; 1217 { 1218 register struct radix_node_head *rnh = (struct radix_node_head *)w; 1219 1220 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh); 1221 free((caddr_t)rn, M_NETADDR); 1222 return (0); 1223 } 1224 1225 /* 1226 * Free the net address hash lists that are hanging off the mount points. 1227 */ 1228 static void 1229 vfs_free_addrlist(nep) 1230 struct netexport *nep; 1231 { 1232 register int i; 1233 register struct radix_node_head *rnh; 1234 1235 for (i = 0; i <= AF_MAX; i++) 1236 if (rnh = nep->ne_rtable[i]) { 1237 (*rnh->rnh_walktree)(rnh, vfs_free_netcred, 1238 (caddr_t)rnh); 1239 free((caddr_t)rnh, M_RTABLE); 1240 nep->ne_rtable[i] = 0; 1241 } 1242 } 1243 1244 int 1245 vfs_export(mp, nep, argp) 1246 struct mount *mp; 1247 struct netexport *nep; 1248 struct export_args *argp; 1249 { 1250 int error; 1251 1252 if (argp->ex_flags & MNT_DELEXPORT) { 1253 vfs_free_addrlist(nep); 1254 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 1255 } 1256 if (argp->ex_flags & MNT_EXPORTED) { 1257 if (error = vfs_hang_addrlist(mp, nep, argp)) 1258 return (error); 1259 mp->mnt_flag |= MNT_EXPORTED; 1260 } 1261 return (0); 1262 } 1263 1264 struct netcred * 1265 vfs_export_lookup(mp, nep, nam) 1266 register struct mount *mp; 1267 struct netexport *nep; 1268 struct mbuf *nam; 1269 { 1270 register struct netcred *np; 1271 register struct radix_node_head *rnh; 1272 struct sockaddr *saddr; 1273 1274 np = NULL; 1275 if (mp->mnt_flag & MNT_EXPORTED) { 1276 /* 1277 * Lookup in the export list first. 1278 */ 1279 if (nam != NULL) { 1280 saddr = mtod(nam, struct sockaddr *); 1281 rnh = nep->ne_rtable[saddr->sa_family]; 1282 if (rnh != NULL) { 1283 np = (struct netcred *) 1284 (*rnh->rnh_matchaddr)((caddr_t)saddr, 1285 rnh); 1286 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 1287 np = NULL; 1288 } 1289 } 1290 /* 1291 * If no address match, use the default if it exists. 1292 */ 1293 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 1294 np = &nep->ne_defexported; 1295 } 1296 return (np); 1297 } 1298