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