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