1 /* $NetBSD: vfs_subr.c,v 1.267 2006/06/23 14:13:02 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998, 2004, 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Charles M. Hannum. 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. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /* 43 * Copyright (c) 1989, 1993 44 * The Regents of the University of California. All rights reserved. 45 * (c) UNIX System Laboratories, Inc. 46 * All or some portions of this file are derived from material licensed 47 * to the University of California by American Telephone and Telegraph 48 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 49 * the permission of UNIX System Laboratories, Inc. 50 * 51 * Redistribution and use in source and binary forms, with or without 52 * modification, are permitted provided that the following conditions 53 * are met: 54 * 1. Redistributions of source code must retain the above copyright 55 * notice, this list of conditions and the following disclaimer. 56 * 2. Redistributions in binary form must reproduce the above copyright 57 * notice, this list of conditions and the following disclaimer in the 58 * documentation and/or other materials provided with the distribution. 59 * 3. Neither the name of the University nor the names of its contributors 60 * may be used to endorse or promote products derived from this software 61 * without specific prior written permission. 62 * 63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 73 * SUCH DAMAGE. 74 * 75 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94 76 */ 77 78 /* 79 * External virtual filesystem routines 80 */ 81 82 #include <sys/cdefs.h> 83 __KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.267 2006/06/23 14:13:02 yamt Exp $"); 84 85 #include "opt_inet.h" 86 #include "opt_ddb.h" 87 #include "opt_compat_netbsd.h" 88 #include "opt_compat_43.h" 89 90 #include <sys/param.h> 91 #include <sys/systm.h> 92 #include <sys/proc.h> 93 #include <sys/kernel.h> 94 #include <sys/mount.h> 95 #include <sys/fcntl.h> 96 #include <sys/vnode.h> 97 #include <sys/stat.h> 98 #include <sys/namei.h> 99 #include <sys/ucred.h> 100 #include <sys/buf.h> 101 #include <sys/errno.h> 102 #include <sys/malloc.h> 103 #include <sys/domain.h> 104 #include <sys/mbuf.h> 105 #include <sys/sa.h> 106 #include <sys/syscallargs.h> 107 #include <sys/device.h> 108 #include <sys/filedesc.h> 109 #include <sys/kauth.h> 110 111 #include <miscfs/specfs/specdev.h> 112 #include <miscfs/genfs/genfs.h> 113 #include <miscfs/syncfs/syncfs.h> 114 115 #include <uvm/uvm.h> 116 #include <uvm/uvm_readahead.h> 117 #include <uvm/uvm_ddb.h> 118 119 #include <sys/sysctl.h> 120 121 const enum vtype iftovt_tab[16] = { 122 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 123 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 124 }; 125 const int vttoif_tab[9] = { 126 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 127 S_IFSOCK, S_IFIFO, S_IFMT, 128 }; 129 130 int doforce = 1; /* 1 => permit forcible unmounting */ 131 int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 132 133 extern int dovfsusermount; /* 1 => permit any user to mount filesystems */ 134 extern int vfs_magiclinks; /* 1 => expand "magic" symlinks */ 135 136 /* 137 * Insq/Remq for the vnode usage lists. 138 */ 139 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 140 #define bufremvn(bp) { \ 141 LIST_REMOVE(bp, b_vnbufs); \ 142 (bp)->b_vnbufs.le_next = NOLIST; \ 143 } 144 /* TAILQ_HEAD(freelst, vnode) vnode_free_list = vnode free list (in vnode.h) */ 145 struct freelst vnode_free_list = TAILQ_HEAD_INITIALIZER(vnode_free_list); 146 struct freelst vnode_hold_list = TAILQ_HEAD_INITIALIZER(vnode_hold_list); 147 148 struct mntlist mountlist = /* mounted filesystem list */ 149 CIRCLEQ_HEAD_INITIALIZER(mountlist); 150 struct vfs_list_head vfs_list = /* vfs list */ 151 LIST_HEAD_INITIALIZER(vfs_list); 152 153 struct simplelock mountlist_slock = SIMPLELOCK_INITIALIZER; 154 static struct simplelock mntid_slock = SIMPLELOCK_INITIALIZER; 155 struct simplelock mntvnode_slock = SIMPLELOCK_INITIALIZER; 156 struct simplelock vnode_free_list_slock = SIMPLELOCK_INITIALIZER; 157 struct simplelock spechash_slock = SIMPLELOCK_INITIALIZER; 158 159 /* XXX - gross; single global lock to protect v_numoutput */ 160 struct simplelock global_v_numoutput_slock = SIMPLELOCK_INITIALIZER; 161 162 /* 163 * These define the root filesystem and device. 164 */ 165 struct mount *rootfs; 166 struct vnode *rootvnode; 167 struct device *root_device; /* root device */ 168 169 POOL_INIT(vnode_pool, sizeof(struct vnode), 0, 0, 0, "vnodepl", 170 &pool_allocator_nointr); 171 172 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 173 174 /* 175 * Local declarations. 176 */ 177 static void insmntque(struct vnode *, struct mount *); 178 static int getdevvp(dev_t, struct vnode **, enum vtype); 179 static void vclean(struct vnode *, int, struct lwp *); 180 static struct vnode *getcleanvnode(struct lwp *); 181 182 #ifdef DEBUG 183 void printlockedvnodes(void); 184 #endif 185 186 /* 187 * Initialize the vnode management data structures. 188 */ 189 void 190 vntblinit(void) 191 { 192 193 /* 194 * Initialize the filesystem syncer. 195 */ 196 vn_initialize_syncerd(); 197 } 198 199 int 200 vfs_drainvnodes(long target, struct lwp *l) 201 { 202 203 simple_lock(&vnode_free_list_slock); 204 while (numvnodes > target) { 205 struct vnode *vp; 206 207 vp = getcleanvnode(l); 208 if (vp == NULL) 209 return EBUSY; /* give up */ 210 pool_put(&vnode_pool, vp); 211 simple_lock(&vnode_free_list_slock); 212 numvnodes--; 213 } 214 simple_unlock(&vnode_free_list_slock); 215 216 return 0; 217 } 218 219 /* 220 * grab a vnode from freelist and clean it. 221 */ 222 struct vnode * 223 getcleanvnode(struct lwp *l) 224 { 225 struct vnode *vp; 226 struct mount *mp; 227 struct freelst *listhd; 228 229 LOCK_ASSERT(simple_lock_held(&vnode_free_list_slock)); 230 231 listhd = &vnode_free_list; 232 try_nextlist: 233 TAILQ_FOREACH(vp, listhd, v_freelist) { 234 if (!simple_lock_try(&vp->v_interlock)) 235 continue; 236 /* 237 * as our lwp might hold the underlying vnode locked, 238 * don't try to reclaim the VLAYER vnode if it's locked. 239 */ 240 if ((vp->v_flag & VXLOCK) == 0 && 241 ((vp->v_flag & VLAYER) == 0 || VOP_ISLOCKED(vp) == 0)) { 242 if (vn_start_write(vp, &mp, V_NOWAIT) == 0) 243 break; 244 } 245 mp = NULL; 246 simple_unlock(&vp->v_interlock); 247 } 248 249 if (vp == NULLVP) { 250 if (listhd == &vnode_free_list) { 251 listhd = &vnode_hold_list; 252 goto try_nextlist; 253 } 254 simple_unlock(&vnode_free_list_slock); 255 return NULLVP; 256 } 257 258 if (vp->v_usecount) 259 panic("free vnode isn't, vp %p", vp); 260 TAILQ_REMOVE(listhd, vp, v_freelist); 261 /* see comment on why 0xdeadb is set at end of vgone (below) */ 262 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb; 263 simple_unlock(&vnode_free_list_slock); 264 vp->v_lease = NULL; 265 266 if (vp->v_type != VBAD) 267 vgonel(vp, l); 268 else 269 simple_unlock(&vp->v_interlock); 270 vn_finished_write(mp, 0); 271 #ifdef DIAGNOSTIC 272 if (vp->v_data || vp->v_uobj.uo_npages || 273 TAILQ_FIRST(&vp->v_uobj.memq)) 274 panic("cleaned vnode isn't, vp %p", vp); 275 if (vp->v_numoutput) 276 panic("clean vnode has pending I/O's, vp %p", vp); 277 #endif 278 KASSERT((vp->v_flag & VONWORKLST) == 0); 279 280 return vp; 281 } 282 283 /* 284 * Mark a mount point as busy. Used to synchronize access and to delay 285 * unmounting. Interlock is not released on failure. 286 */ 287 int 288 vfs_busy(struct mount *mp, int flags, struct simplelock *interlkp) 289 { 290 int lkflags; 291 292 while (mp->mnt_iflag & IMNT_UNMOUNT) { 293 int gone, n; 294 295 if (flags & LK_NOWAIT) 296 return (ENOENT); 297 if ((flags & LK_RECURSEFAIL) && mp->mnt_unmounter != NULL 298 && mp->mnt_unmounter == curlwp) 299 return (EDEADLK); 300 if (interlkp) 301 simple_unlock(interlkp); 302 /* 303 * Since all busy locks are shared except the exclusive 304 * lock granted when unmounting, the only place that a 305 * wakeup needs to be done is at the release of the 306 * exclusive lock at the end of dounmount. 307 */ 308 simple_lock(&mp->mnt_slock); 309 mp->mnt_wcnt++; 310 ltsleep((caddr_t)mp, PVFS, "vfs_busy", 0, &mp->mnt_slock); 311 n = --mp->mnt_wcnt; 312 simple_unlock(&mp->mnt_slock); 313 gone = mp->mnt_iflag & IMNT_GONE; 314 315 if (n == 0) 316 wakeup(&mp->mnt_wcnt); 317 if (interlkp) 318 simple_lock(interlkp); 319 if (gone) 320 return (ENOENT); 321 } 322 lkflags = LK_SHARED; 323 if (interlkp) 324 lkflags |= LK_INTERLOCK; 325 if (lockmgr(&mp->mnt_lock, lkflags, interlkp)) 326 panic("vfs_busy: unexpected lock failure"); 327 return (0); 328 } 329 330 /* 331 * Free a busy filesystem. 332 */ 333 void 334 vfs_unbusy(struct mount *mp) 335 { 336 337 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL); 338 } 339 340 /* 341 * Lookup a filesystem type, and if found allocate and initialize 342 * a mount structure for it. 343 * 344 * Devname is usually updated by mount(8) after booting. 345 */ 346 int 347 vfs_rootmountalloc(const char *fstypename, const char *devname, 348 struct mount **mpp) 349 { 350 struct vfsops *vfsp = NULL; 351 struct mount *mp; 352 353 LIST_FOREACH(vfsp, &vfs_list, vfs_list) 354 if (!strncmp(vfsp->vfs_name, fstypename, MFSNAMELEN)) 355 break; 356 357 if (vfsp == NULL) 358 return (ENODEV); 359 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 360 memset((char *)mp, 0, (u_long)sizeof(struct mount)); 361 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 362 simple_lock_init(&mp->mnt_slock); 363 (void)vfs_busy(mp, LK_NOWAIT, 0); 364 LIST_INIT(&mp->mnt_vnodelist); 365 mp->mnt_op = vfsp; 366 mp->mnt_flag = MNT_RDONLY; 367 mp->mnt_vnodecovered = NULLVP; 368 mp->mnt_leaf = mp; 369 vfsp->vfs_refcount++; 370 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, MFSNAMELEN); 371 mp->mnt_stat.f_mntonname[0] = '/'; 372 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0); 373 *mpp = mp; 374 return (0); 375 } 376 377 /* 378 * Lookup a mount point by filesystem identifier. 379 */ 380 struct mount * 381 vfs_getvfs(fsid_t *fsid) 382 { 383 struct mount *mp; 384 385 simple_lock(&mountlist_slock); 386 CIRCLEQ_FOREACH(mp, &mountlist, mnt_list) { 387 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] && 388 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) { 389 simple_unlock(&mountlist_slock); 390 return (mp); 391 } 392 } 393 simple_unlock(&mountlist_slock); 394 return ((struct mount *)0); 395 } 396 397 /* 398 * Get a new unique fsid 399 */ 400 void 401 vfs_getnewfsid(struct mount *mp) 402 { 403 static u_short xxxfs_mntid; 404 fsid_t tfsid; 405 int mtype; 406 407 simple_lock(&mntid_slock); 408 mtype = makefstype(mp->mnt_op->vfs_name); 409 mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0); 410 mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype; 411 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 412 if (xxxfs_mntid == 0) 413 ++xxxfs_mntid; 414 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid); 415 tfsid.__fsid_val[1] = mtype; 416 if (!CIRCLEQ_EMPTY(&mountlist)) { 417 while (vfs_getvfs(&tfsid)) { 418 tfsid.__fsid_val[0]++; 419 xxxfs_mntid++; 420 } 421 } 422 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0]; 423 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 424 simple_unlock(&mntid_slock); 425 } 426 427 /* 428 * Make a 'unique' number from a mount type name. 429 */ 430 long 431 makefstype(const char *type) 432 { 433 long rv; 434 435 for (rv = 0; *type; type++) { 436 rv <<= 2; 437 rv ^= *type; 438 } 439 return rv; 440 } 441 442 443 /* 444 * Set vnode attributes to VNOVAL 445 */ 446 void 447 vattr_null(struct vattr *vap) 448 { 449 450 vap->va_type = VNON; 451 452 /* 453 * Assign individually so that it is safe even if size and 454 * sign of each member are varied. 455 */ 456 vap->va_mode = VNOVAL; 457 vap->va_nlink = VNOVAL; 458 vap->va_uid = VNOVAL; 459 vap->va_gid = VNOVAL; 460 vap->va_fsid = VNOVAL; 461 vap->va_fileid = VNOVAL; 462 vap->va_size = VNOVAL; 463 vap->va_blocksize = VNOVAL; 464 vap->va_atime.tv_sec = 465 vap->va_mtime.tv_sec = 466 vap->va_ctime.tv_sec = 467 vap->va_birthtime.tv_sec = VNOVAL; 468 vap->va_atime.tv_nsec = 469 vap->va_mtime.tv_nsec = 470 vap->va_ctime.tv_nsec = 471 vap->va_birthtime.tv_nsec = VNOVAL; 472 vap->va_gen = VNOVAL; 473 vap->va_flags = VNOVAL; 474 vap->va_rdev = VNOVAL; 475 vap->va_bytes = VNOVAL; 476 vap->va_vaflags = 0; 477 } 478 479 /* 480 * Routines having to do with the management of the vnode table. 481 */ 482 extern int (**dead_vnodeop_p)(void *); 483 long numvnodes; 484 485 /* 486 * Return the next vnode from the free list. 487 */ 488 int 489 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *), 490 struct vnode **vpp) 491 { 492 extern struct uvm_pagerops uvm_vnodeops; 493 struct uvm_object *uobj; 494 struct lwp *l = curlwp; /* XXX */ 495 static int toggle; 496 struct vnode *vp; 497 int error = 0, tryalloc; 498 499 try_again: 500 if (mp) { 501 /* 502 * Mark filesystem busy while we're creating a vnode. 503 * If unmount is in progress, this will wait; if the 504 * unmount succeeds (only if umount -f), this will 505 * return an error. If the unmount fails, we'll keep 506 * going afterwards. 507 * (This puts the per-mount vnode list logically under 508 * the protection of the vfs_busy lock). 509 */ 510 error = vfs_busy(mp, LK_RECURSEFAIL, 0); 511 if (error && error != EDEADLK) 512 return error; 513 } 514 515 /* 516 * We must choose whether to allocate a new vnode or recycle an 517 * existing one. The criterion for allocating a new one is that 518 * the total number of vnodes is less than the number desired or 519 * there are no vnodes on either free list. Generally we only 520 * want to recycle vnodes that have no buffers associated with 521 * them, so we look first on the vnode_free_list. If it is empty, 522 * we next consider vnodes with referencing buffers on the 523 * vnode_hold_list. The toggle ensures that half the time we 524 * will use a buffer from the vnode_hold_list, and half the time 525 * we will allocate a new one unless the list has grown to twice 526 * the desired size. We are reticent to recycle vnodes from the 527 * vnode_hold_list because we will lose the identity of all its 528 * referencing buffers. 529 */ 530 531 vp = NULL; 532 533 simple_lock(&vnode_free_list_slock); 534 535 toggle ^= 1; 536 if (numvnodes > 2 * desiredvnodes) 537 toggle = 0; 538 539 tryalloc = numvnodes < desiredvnodes || 540 (TAILQ_FIRST(&vnode_free_list) == NULL && 541 (TAILQ_FIRST(&vnode_hold_list) == NULL || toggle)); 542 543 if (tryalloc && 544 (vp = pool_get(&vnode_pool, PR_NOWAIT)) != NULL) { 545 numvnodes++; 546 simple_unlock(&vnode_free_list_slock); 547 memset(vp, 0, sizeof(*vp)); 548 UVM_OBJ_INIT(&vp->v_uobj, &uvm_vnodeops, 1); 549 /* 550 * done by memset() above. 551 * LIST_INIT(&vp->v_nclist); 552 * LIST_INIT(&vp->v_dnclist); 553 */ 554 } else { 555 vp = getcleanvnode(l); 556 /* 557 * Unless this is a bad time of the month, at most 558 * the first NCPUS items on the free list are 559 * locked, so this is close enough to being empty. 560 */ 561 if (vp == NULLVP) { 562 if (mp && error != EDEADLK) 563 vfs_unbusy(mp); 564 if (tryalloc) { 565 printf("WARNING: unable to allocate new " 566 "vnode, retrying...\n"); 567 (void) tsleep(&lbolt, PRIBIO, "newvn", hz); 568 goto try_again; 569 } 570 tablefull("vnode", "increase kern.maxvnodes or NVNODE"); 571 *vpp = 0; 572 return (ENFILE); 573 } 574 vp->v_usecount = 1; 575 vp->v_flag = 0; 576 vp->v_socket = NULL; 577 } 578 vp->v_type = VNON; 579 vp->v_vnlock = &vp->v_lock; 580 lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0); 581 KASSERT(LIST_EMPTY(&vp->v_nclist)); 582 KASSERT(LIST_EMPTY(&vp->v_dnclist)); 583 vp->v_tag = tag; 584 vp->v_op = vops; 585 insmntque(vp, mp); 586 *vpp = vp; 587 vp->v_data = 0; 588 simple_lock_init(&vp->v_interlock); 589 590 /* 591 * initialize uvm_object within vnode. 592 */ 593 594 uobj = &vp->v_uobj; 595 KASSERT(uobj->pgops == &uvm_vnodeops); 596 KASSERT(uobj->uo_npages == 0); 597 KASSERT(TAILQ_FIRST(&uobj->memq) == NULL); 598 vp->v_size = VSIZENOTSET; 599 600 if (mp && error != EDEADLK) 601 vfs_unbusy(mp); 602 return (0); 603 } 604 605 /* 606 * This is really just the reverse of getnewvnode(). Needed for 607 * VFS_VGET functions who may need to push back a vnode in case 608 * of a locking race. 609 */ 610 void 611 ungetnewvnode(struct vnode *vp) 612 { 613 #ifdef DIAGNOSTIC 614 if (vp->v_usecount != 1) 615 panic("ungetnewvnode: busy vnode"); 616 #endif 617 vp->v_usecount--; 618 insmntque(vp, NULL); 619 vp->v_type = VBAD; 620 621 simple_lock(&vp->v_interlock); 622 /* 623 * Insert at head of LRU list 624 */ 625 simple_lock(&vnode_free_list_slock); 626 if (vp->v_holdcnt > 0) 627 TAILQ_INSERT_HEAD(&vnode_hold_list, vp, v_freelist); 628 else 629 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 630 simple_unlock(&vnode_free_list_slock); 631 simple_unlock(&vp->v_interlock); 632 } 633 634 /* 635 * Move a vnode from one mount queue to another. 636 */ 637 static void 638 insmntque(struct vnode *vp, struct mount *mp) 639 { 640 641 #ifdef DIAGNOSTIC 642 if ((mp != NULL) && 643 (mp->mnt_iflag & IMNT_UNMOUNT) && 644 !(mp->mnt_flag & MNT_SOFTDEP) && 645 vp->v_tag != VT_VFS) { 646 panic("insmntque into dying filesystem"); 647 } 648 #endif 649 650 simple_lock(&mntvnode_slock); 651 /* 652 * Delete from old mount point vnode list, if on one. 653 */ 654 if (vp->v_mount != NULL) 655 LIST_REMOVE(vp, v_mntvnodes); 656 /* 657 * Insert into list of vnodes for the new mount point, if available. 658 */ 659 if ((vp->v_mount = mp) != NULL) 660 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes); 661 simple_unlock(&mntvnode_slock); 662 } 663 664 /* 665 * Update outstanding I/O count and do wakeup if requested. 666 */ 667 void 668 vwakeup(struct buf *bp) 669 { 670 struct vnode *vp; 671 672 if ((vp = bp->b_vp) != NULL) { 673 /* XXX global lock hack 674 * can't use v_interlock here since this is called 675 * in interrupt context from biodone(). 676 */ 677 simple_lock(&global_v_numoutput_slock); 678 if (--vp->v_numoutput < 0) 679 panic("vwakeup: neg numoutput, vp %p", vp); 680 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) { 681 vp->v_flag &= ~VBWAIT; 682 wakeup((caddr_t)&vp->v_numoutput); 683 } 684 simple_unlock(&global_v_numoutput_slock); 685 } 686 } 687 688 /* 689 * Flush out and invalidate all buffers associated with a vnode. 690 * Called with the underlying vnode locked, which should prevent new dirty 691 * buffers from being queued. 692 */ 693 int 694 vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l, 695 int slpflag, int slptimeo) 696 { 697 struct buf *bp, *nbp; 698 int s, error; 699 int flushflags = PGO_ALLPAGES | PGO_FREE | PGO_SYNCIO | 700 (flags & V_SAVE ? PGO_CLEANIT : 0); 701 702 /* XXXUBC this doesn't look at flags or slp* */ 703 simple_lock(&vp->v_interlock); 704 error = VOP_PUTPAGES(vp, 0, 0, flushflags); 705 if (error) { 706 return error; 707 } 708 709 if (flags & V_SAVE) { 710 error = VOP_FSYNC(vp, cred, FSYNC_WAIT|FSYNC_RECLAIM, 0, 0, l); 711 if (error) 712 return (error); 713 #ifdef DIAGNOSTIC 714 s = splbio(); 715 if (vp->v_numoutput > 0 || !LIST_EMPTY(&vp->v_dirtyblkhd)) 716 panic("vinvalbuf: dirty bufs, vp %p", vp); 717 splx(s); 718 #endif 719 } 720 721 s = splbio(); 722 723 restart: 724 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 725 nbp = LIST_NEXT(bp, b_vnbufs); 726 simple_lock(&bp->b_interlock); 727 if (bp->b_flags & B_BUSY) { 728 bp->b_flags |= B_WANTED; 729 error = ltsleep((caddr_t)bp, 730 slpflag | (PRIBIO + 1) | PNORELOCK, 731 "vinvalbuf", slptimeo, &bp->b_interlock); 732 if (error) { 733 splx(s); 734 return (error); 735 } 736 goto restart; 737 } 738 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH; 739 simple_unlock(&bp->b_interlock); 740 brelse(bp); 741 } 742 743 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 744 nbp = LIST_NEXT(bp, b_vnbufs); 745 simple_lock(&bp->b_interlock); 746 if (bp->b_flags & B_BUSY) { 747 bp->b_flags |= B_WANTED; 748 error = ltsleep((caddr_t)bp, 749 slpflag | (PRIBIO + 1) | PNORELOCK, 750 "vinvalbuf", slptimeo, &bp->b_interlock); 751 if (error) { 752 splx(s); 753 return (error); 754 } 755 goto restart; 756 } 757 /* 758 * XXX Since there are no node locks for NFS, I believe 759 * there is a slight chance that a delayed write will 760 * occur while sleeping just above, so check for it. 761 */ 762 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) { 763 #ifdef DEBUG 764 printf("buffer still DELWRI\n"); 765 #endif 766 bp->b_flags |= B_BUSY | B_VFLUSH; 767 simple_unlock(&bp->b_interlock); 768 VOP_BWRITE(bp); 769 goto restart; 770 } 771 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH; 772 simple_unlock(&bp->b_interlock); 773 brelse(bp); 774 } 775 776 #ifdef DIAGNOSTIC 777 if (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd)) 778 panic("vinvalbuf: flush failed, vp %p", vp); 779 #endif 780 781 splx(s); 782 783 return (0); 784 } 785 786 /* 787 * Destroy any in core blocks past the truncation length. 788 * Called with the underlying vnode locked, which should prevent new dirty 789 * buffers from being queued. 790 */ 791 int 792 vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo) 793 { 794 struct buf *bp, *nbp; 795 int s, error; 796 voff_t off; 797 798 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift); 799 simple_lock(&vp->v_interlock); 800 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO); 801 if (error) { 802 return error; 803 } 804 805 s = splbio(); 806 807 restart: 808 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 809 nbp = LIST_NEXT(bp, b_vnbufs); 810 if (bp->b_lblkno < lbn) 811 continue; 812 simple_lock(&bp->b_interlock); 813 if (bp->b_flags & B_BUSY) { 814 bp->b_flags |= B_WANTED; 815 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK, 816 "vtruncbuf", slptimeo, &bp->b_interlock); 817 if (error) { 818 splx(s); 819 return (error); 820 } 821 goto restart; 822 } 823 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH; 824 simple_unlock(&bp->b_interlock); 825 brelse(bp); 826 } 827 828 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 829 nbp = LIST_NEXT(bp, b_vnbufs); 830 if (bp->b_lblkno < lbn) 831 continue; 832 simple_lock(&bp->b_interlock); 833 if (bp->b_flags & B_BUSY) { 834 bp->b_flags |= B_WANTED; 835 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK, 836 "vtruncbuf", slptimeo, &bp->b_interlock); 837 if (error) { 838 splx(s); 839 return (error); 840 } 841 goto restart; 842 } 843 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH; 844 simple_unlock(&bp->b_interlock); 845 brelse(bp); 846 } 847 848 splx(s); 849 850 return (0); 851 } 852 853 void 854 vflushbuf(struct vnode *vp, int sync) 855 { 856 struct buf *bp, *nbp; 857 int flags = PGO_CLEANIT | PGO_ALLPAGES | (sync ? PGO_SYNCIO : 0); 858 int s; 859 860 simple_lock(&vp->v_interlock); 861 (void) VOP_PUTPAGES(vp, 0, 0, flags); 862 863 loop: 864 s = splbio(); 865 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 866 nbp = LIST_NEXT(bp, b_vnbufs); 867 simple_lock(&bp->b_interlock); 868 if ((bp->b_flags & B_BUSY)) { 869 simple_unlock(&bp->b_interlock); 870 continue; 871 } 872 if ((bp->b_flags & B_DELWRI) == 0) 873 panic("vflushbuf: not dirty, bp %p", bp); 874 bp->b_flags |= B_BUSY | B_VFLUSH; 875 simple_unlock(&bp->b_interlock); 876 splx(s); 877 /* 878 * Wait for I/O associated with indirect blocks to complete, 879 * since there is no way to quickly wait for them below. 880 */ 881 if (bp->b_vp == vp || sync == 0) 882 (void) bawrite(bp); 883 else 884 (void) bwrite(bp); 885 goto loop; 886 } 887 if (sync == 0) { 888 splx(s); 889 return; 890 } 891 simple_lock(&global_v_numoutput_slock); 892 while (vp->v_numoutput) { 893 vp->v_flag |= VBWAIT; 894 ltsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "vflushbuf", 0, 895 &global_v_numoutput_slock); 896 } 897 simple_unlock(&global_v_numoutput_slock); 898 splx(s); 899 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) { 900 vprint("vflushbuf: dirty", vp); 901 goto loop; 902 } 903 } 904 905 /* 906 * Associate a buffer with a vnode. 907 */ 908 void 909 bgetvp(struct vnode *vp, struct buf *bp) 910 { 911 int s; 912 913 if (bp->b_vp) 914 panic("bgetvp: not free, bp %p", bp); 915 VHOLD(vp); 916 s = splbio(); 917 bp->b_vp = vp; 918 if (vp->v_type == VBLK || vp->v_type == VCHR) 919 bp->b_dev = vp->v_rdev; 920 else 921 bp->b_dev = NODEV; 922 /* 923 * Insert onto list for new vnode. 924 */ 925 bufinsvn(bp, &vp->v_cleanblkhd); 926 splx(s); 927 } 928 929 /* 930 * Disassociate a buffer from a vnode. 931 */ 932 void 933 brelvp(struct buf *bp) 934 { 935 struct vnode *vp; 936 int s; 937 938 if (bp->b_vp == NULL) 939 panic("brelvp: vp NULL, bp %p", bp); 940 941 s = splbio(); 942 vp = bp->b_vp; 943 /* 944 * Delete from old vnode list, if on one. 945 */ 946 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 947 bufremvn(bp); 948 949 if (TAILQ_EMPTY(&vp->v_uobj.memq) && (vp->v_flag & VONWORKLST) && 950 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) { 951 vp->v_flag &= ~(VWRITEMAPDIRTY|VONWORKLST); 952 LIST_REMOVE(vp, v_synclist); 953 } 954 955 bp->b_vp = NULL; 956 HOLDRELE(vp); 957 splx(s); 958 } 959 960 /* 961 * Reassign a buffer from one vnode to another. 962 * Used to assign file specific control information 963 * (indirect blocks) to the vnode to which they belong. 964 * 965 * This function must be called at splbio(). 966 */ 967 void 968 reassignbuf(struct buf *bp, struct vnode *newvp) 969 { 970 struct buflists *listheadp; 971 int delayx; 972 973 /* 974 * Delete from old vnode list, if on one. 975 */ 976 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 977 bufremvn(bp); 978 /* 979 * If dirty, put on list of dirty buffers; 980 * otherwise insert onto list of clean buffers. 981 */ 982 if ((bp->b_flags & B_DELWRI) == 0) { 983 listheadp = &newvp->v_cleanblkhd; 984 if (TAILQ_EMPTY(&newvp->v_uobj.memq) && 985 (newvp->v_flag & VONWORKLST) && 986 LIST_FIRST(&newvp->v_dirtyblkhd) == NULL) { 987 newvp->v_flag &= ~(VWRITEMAPDIRTY|VONWORKLST); 988 LIST_REMOVE(newvp, v_synclist); 989 } 990 } else { 991 listheadp = &newvp->v_dirtyblkhd; 992 if ((newvp->v_flag & VONWORKLST) == 0) { 993 switch (newvp->v_type) { 994 case VDIR: 995 delayx = dirdelay; 996 break; 997 case VBLK: 998 if (newvp->v_specmountpoint != NULL) { 999 delayx = metadelay; 1000 break; 1001 } 1002 /* fall through */ 1003 default: 1004 delayx = filedelay; 1005 break; 1006 } 1007 if (!newvp->v_mount || 1008 (newvp->v_mount->mnt_flag & MNT_ASYNC) == 0) 1009 vn_syncer_add_to_worklist(newvp, delayx); 1010 } 1011 } 1012 bufinsvn(bp, listheadp); 1013 } 1014 1015 /* 1016 * Create a vnode for a block device. 1017 * Used for root filesystem and swap areas. 1018 * Also used for memory file system special devices. 1019 */ 1020 int 1021 bdevvp(dev_t dev, struct vnode **vpp) 1022 { 1023 1024 return (getdevvp(dev, vpp, VBLK)); 1025 } 1026 1027 /* 1028 * Create a vnode for a character device. 1029 * Used for kernfs and some console handling. 1030 */ 1031 int 1032 cdevvp(dev_t dev, struct vnode **vpp) 1033 { 1034 1035 return (getdevvp(dev, vpp, VCHR)); 1036 } 1037 1038 /* 1039 * Create a vnode for a device. 1040 * Used by bdevvp (block device) for root file system etc., 1041 * and by cdevvp (character device) for console and kernfs. 1042 */ 1043 static int 1044 getdevvp(dev_t dev, struct vnode **vpp, enum vtype type) 1045 { 1046 struct vnode *vp; 1047 struct vnode *nvp; 1048 int error; 1049 1050 if (dev == NODEV) { 1051 *vpp = NULLVP; 1052 return (0); 1053 } 1054 error = getnewvnode(VT_NON, NULL, spec_vnodeop_p, &nvp); 1055 if (error) { 1056 *vpp = NULLVP; 1057 return (error); 1058 } 1059 vp = nvp; 1060 vp->v_type = type; 1061 if ((nvp = checkalias(vp, dev, NULL)) != 0) { 1062 vput(vp); 1063 vp = nvp; 1064 } 1065 *vpp = vp; 1066 return (0); 1067 } 1068 1069 /* 1070 * Check to see if the new vnode represents a special device 1071 * for which we already have a vnode (either because of 1072 * bdevvp() or because of a different vnode representing 1073 * the same block device). If such an alias exists, deallocate 1074 * the existing contents and return the aliased vnode. The 1075 * caller is responsible for filling it with its new contents. 1076 */ 1077 struct vnode * 1078 checkalias(struct vnode *nvp, dev_t nvp_rdev, struct mount *mp) 1079 { 1080 struct lwp *l = curlwp; /* XXX */ 1081 struct vnode *vp; 1082 struct vnode **vpp; 1083 1084 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 1085 return (NULLVP); 1086 1087 vpp = &speclisth[SPECHASH(nvp_rdev)]; 1088 loop: 1089 simple_lock(&spechash_slock); 1090 for (vp = *vpp; vp; vp = vp->v_specnext) { 1091 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type) 1092 continue; 1093 /* 1094 * Alias, but not in use, so flush it out. 1095 */ 1096 simple_lock(&vp->v_interlock); 1097 simple_unlock(&spechash_slock); 1098 if (vp->v_usecount == 0) { 1099 vgonel(vp, l); 1100 goto loop; 1101 } 1102 /* 1103 * What we're interested to know here is if someone else has 1104 * removed this vnode from the device hash list while we were 1105 * waiting. This can only happen if vclean() did it, and 1106 * this requires the vnode to be locked. 1107 */ 1108 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) 1109 goto loop; 1110 if (vp->v_specinfo == NULL) { 1111 vput(vp); 1112 goto loop; 1113 } 1114 simple_lock(&spechash_slock); 1115 break; 1116 } 1117 if (vp == NULL || vp->v_tag != VT_NON || vp->v_type != VBLK) { 1118 MALLOC(nvp->v_specinfo, struct specinfo *, 1119 sizeof(struct specinfo), M_VNODE, M_NOWAIT); 1120 /* XXX Erg. */ 1121 if (nvp->v_specinfo == NULL) { 1122 simple_unlock(&spechash_slock); 1123 uvm_wait("checkalias"); 1124 goto loop; 1125 } 1126 1127 nvp->v_rdev = nvp_rdev; 1128 nvp->v_hashchain = vpp; 1129 nvp->v_specnext = *vpp; 1130 nvp->v_specmountpoint = NULL; 1131 simple_unlock(&spechash_slock); 1132 nvp->v_speclockf = NULL; 1133 simple_lock_init(&nvp->v_spec_cow_slock); 1134 SLIST_INIT(&nvp->v_spec_cow_head); 1135 nvp->v_spec_cow_req = 0; 1136 nvp->v_spec_cow_count = 0; 1137 1138 *vpp = nvp; 1139 if (vp != NULLVP) { 1140 nvp->v_flag |= VALIASED; 1141 vp->v_flag |= VALIASED; 1142 vput(vp); 1143 } 1144 return (NULLVP); 1145 } 1146 simple_unlock(&spechash_slock); 1147 VOP_UNLOCK(vp, 0); 1148 simple_lock(&vp->v_interlock); 1149 vclean(vp, 0, l); 1150 vp->v_op = nvp->v_op; 1151 vp->v_tag = nvp->v_tag; 1152 vp->v_vnlock = &vp->v_lock; 1153 lockinit(vp->v_vnlock, PVFS, "vnlock", 0, 0); 1154 nvp->v_type = VNON; 1155 insmntque(vp, mp); 1156 return (vp); 1157 } 1158 1159 /* 1160 * Grab a particular vnode from the free list, increment its 1161 * reference count and lock it. If the vnode lock bit is set the 1162 * vnode is being eliminated in vgone. In that case, we can not 1163 * grab the vnode, so the process is awakened when the transition is 1164 * completed, and an error returned to indicate that the vnode is no 1165 * longer usable (possibly having been changed to a new file system type). 1166 */ 1167 int 1168 vget(struct vnode *vp, int flags) 1169 { 1170 int error; 1171 1172 /* 1173 * If the vnode is in the process of being cleaned out for 1174 * another use, we wait for the cleaning to finish and then 1175 * return failure. Cleaning is determined by checking that 1176 * the VXLOCK flag is set. 1177 */ 1178 1179 if ((flags & LK_INTERLOCK) == 0) 1180 simple_lock(&vp->v_interlock); 1181 if ((vp->v_flag & (VXLOCK | VFREEING)) != 0) { 1182 if (flags & LK_NOWAIT) { 1183 simple_unlock(&vp->v_interlock); 1184 return EBUSY; 1185 } 1186 vp->v_flag |= VXWANT; 1187 ltsleep(vp, PINOD|PNORELOCK, "vget", 0, &vp->v_interlock); 1188 return (ENOENT); 1189 } 1190 if (vp->v_usecount == 0) { 1191 simple_lock(&vnode_free_list_slock); 1192 if (vp->v_holdcnt > 0) 1193 TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist); 1194 else 1195 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1196 simple_unlock(&vnode_free_list_slock); 1197 } 1198 vp->v_usecount++; 1199 #ifdef DIAGNOSTIC 1200 if (vp->v_usecount == 0) { 1201 vprint("vget", vp); 1202 panic("vget: usecount overflow, vp %p", vp); 1203 } 1204 #endif 1205 if (flags & LK_TYPE_MASK) { 1206 if ((error = vn_lock(vp, flags | LK_INTERLOCK))) { 1207 vrele(vp); 1208 } 1209 return (error); 1210 } 1211 simple_unlock(&vp->v_interlock); 1212 return (0); 1213 } 1214 1215 /* 1216 * vput(), just unlock and vrele() 1217 */ 1218 void 1219 vput(struct vnode *vp) 1220 { 1221 struct lwp *l = curlwp; /* XXX */ 1222 1223 #ifdef DIAGNOSTIC 1224 if (vp == NULL) 1225 panic("vput: null vp"); 1226 #endif 1227 simple_lock(&vp->v_interlock); 1228 vp->v_usecount--; 1229 if (vp->v_usecount > 0) { 1230 simple_unlock(&vp->v_interlock); 1231 VOP_UNLOCK(vp, 0); 1232 return; 1233 } 1234 #ifdef DIAGNOSTIC 1235 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 1236 vprint("vput: bad ref count", vp); 1237 panic("vput: ref cnt"); 1238 } 1239 #endif 1240 /* 1241 * Insert at tail of LRU list. 1242 */ 1243 simple_lock(&vnode_free_list_slock); 1244 if (vp->v_holdcnt > 0) 1245 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist); 1246 else 1247 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 1248 simple_unlock(&vnode_free_list_slock); 1249 if (vp->v_flag & VEXECMAP) { 1250 uvmexp.execpages -= vp->v_uobj.uo_npages; 1251 uvmexp.filepages += vp->v_uobj.uo_npages; 1252 } 1253 vp->v_flag &= ~(VTEXT|VEXECMAP|VWRITEMAP); 1254 simple_unlock(&vp->v_interlock); 1255 VOP_INACTIVE(vp, l); 1256 } 1257 1258 /* 1259 * Vnode release. 1260 * If count drops to zero, call inactive routine and return to freelist. 1261 */ 1262 void 1263 vrele(struct vnode *vp) 1264 { 1265 struct lwp *l = curlwp; /* XXX */ 1266 1267 #ifdef DIAGNOSTIC 1268 if (vp == NULL) 1269 panic("vrele: null vp"); 1270 #endif 1271 simple_lock(&vp->v_interlock); 1272 vp->v_usecount--; 1273 if (vp->v_usecount > 0) { 1274 simple_unlock(&vp->v_interlock); 1275 return; 1276 } 1277 #ifdef DIAGNOSTIC 1278 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 1279 vprint("vrele: bad ref count", vp); 1280 panic("vrele: ref cnt vp %p", vp); 1281 } 1282 #endif 1283 /* 1284 * Insert at tail of LRU list. 1285 */ 1286 simple_lock(&vnode_free_list_slock); 1287 if (vp->v_holdcnt > 0) 1288 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist); 1289 else 1290 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 1291 simple_unlock(&vnode_free_list_slock); 1292 if (vp->v_flag & VEXECMAP) { 1293 uvmexp.execpages -= vp->v_uobj.uo_npages; 1294 uvmexp.filepages += vp->v_uobj.uo_npages; 1295 } 1296 vp->v_flag &= ~(VTEXT|VEXECMAP|VWRITEMAP); 1297 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) 1298 VOP_INACTIVE(vp, l); 1299 } 1300 1301 /* 1302 * Page or buffer structure gets a reference. 1303 * Called with v_interlock held. 1304 */ 1305 void 1306 vholdl(struct vnode *vp) 1307 { 1308 1309 /* 1310 * If it is on the freelist and the hold count is currently 1311 * zero, move it to the hold list. The test of the back 1312 * pointer and the use reference count of zero is because 1313 * it will be removed from a free list by getnewvnode, 1314 * but will not have its reference count incremented until 1315 * after calling vgone. If the reference count were 1316 * incremented first, vgone would (incorrectly) try to 1317 * close the previous instance of the underlying object. 1318 * So, the back pointer is explicitly set to `0xdeadb' in 1319 * getnewvnode after removing it from a freelist to ensure 1320 * that we do not try to move it here. 1321 */ 1322 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb) && 1323 vp->v_holdcnt == 0 && vp->v_usecount == 0) { 1324 simple_lock(&vnode_free_list_slock); 1325 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1326 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist); 1327 simple_unlock(&vnode_free_list_slock); 1328 } 1329 vp->v_holdcnt++; 1330 } 1331 1332 /* 1333 * Page or buffer structure frees a reference. 1334 * Called with v_interlock held. 1335 */ 1336 void 1337 holdrelel(struct vnode *vp) 1338 { 1339 1340 if (vp->v_holdcnt <= 0) 1341 panic("holdrelel: holdcnt vp %p", vp); 1342 vp->v_holdcnt--; 1343 1344 /* 1345 * If it is on the holdlist and the hold count drops to 1346 * zero, move it to the free list. The test of the back 1347 * pointer and the use reference count of zero is because 1348 * it will be removed from a free list by getnewvnode, 1349 * but will not have its reference count incremented until 1350 * after calling vgone. If the reference count were 1351 * incremented first, vgone would (incorrectly) try to 1352 * close the previous instance of the underlying object. 1353 * So, the back pointer is explicitly set to `0xdeadb' in 1354 * getnewvnode after removing it from a freelist to ensure 1355 * that we do not try to move it here. 1356 */ 1357 1358 if ((vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb) && 1359 vp->v_holdcnt == 0 && vp->v_usecount == 0) { 1360 simple_lock(&vnode_free_list_slock); 1361 TAILQ_REMOVE(&vnode_hold_list, vp, v_freelist); 1362 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 1363 simple_unlock(&vnode_free_list_slock); 1364 } 1365 } 1366 1367 /* 1368 * Vnode reference. 1369 */ 1370 void 1371 vref(struct vnode *vp) 1372 { 1373 1374 simple_lock(&vp->v_interlock); 1375 if (vp->v_usecount <= 0) 1376 panic("vref used where vget required, vp %p", vp); 1377 vp->v_usecount++; 1378 #ifdef DIAGNOSTIC 1379 if (vp->v_usecount == 0) { 1380 vprint("vref", vp); 1381 panic("vref: usecount overflow, vp %p", vp); 1382 } 1383 #endif 1384 simple_unlock(&vp->v_interlock); 1385 } 1386 1387 /* 1388 * Remove any vnodes in the vnode table belonging to mount point mp. 1389 * 1390 * If FORCECLOSE is not specified, there should not be any active ones, 1391 * return error if any are found (nb: this is a user error, not a 1392 * system error). If FORCECLOSE is specified, detach any active vnodes 1393 * that are found. 1394 * 1395 * If WRITECLOSE is set, only flush out regular file vnodes open for 1396 * writing. 1397 * 1398 * SKIPSYSTEM causes any vnodes marked V_SYSTEM to be skipped. 1399 */ 1400 #ifdef DEBUG 1401 int busyprt = 0; /* print out busy vnodes */ 1402 struct ctldebug debug1 = { "busyprt", &busyprt }; 1403 #endif 1404 1405 int 1406 vflush(struct mount *mp, struct vnode *skipvp, int flags) 1407 { 1408 struct lwp *l = curlwp; /* XXX */ 1409 struct vnode *vp, *nvp; 1410 int busy = 0; 1411 1412 simple_lock(&mntvnode_slock); 1413 loop: 1414 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) { 1415 if (vp->v_mount != mp) 1416 goto loop; 1417 nvp = LIST_NEXT(vp, v_mntvnodes); 1418 /* 1419 * Skip over a selected vnode. 1420 */ 1421 if (vp == skipvp) 1422 continue; 1423 simple_lock(&vp->v_interlock); 1424 /* 1425 * Skip over a vnodes marked VSYSTEM. 1426 */ 1427 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 1428 simple_unlock(&vp->v_interlock); 1429 continue; 1430 } 1431 /* 1432 * If WRITECLOSE is set, only flush out regular file 1433 * vnodes open for writing. 1434 */ 1435 if ((flags & WRITECLOSE) && 1436 (vp->v_writecount == 0 || vp->v_type != VREG)) { 1437 simple_unlock(&vp->v_interlock); 1438 continue; 1439 } 1440 /* 1441 * With v_usecount == 0, all we need to do is clear 1442 * out the vnode data structures and we are done. 1443 */ 1444 if (vp->v_usecount == 0) { 1445 simple_unlock(&mntvnode_slock); 1446 vgonel(vp, l); 1447 simple_lock(&mntvnode_slock); 1448 continue; 1449 } 1450 /* 1451 * If FORCECLOSE is set, forcibly close the vnode. 1452 * For block or character devices, revert to an 1453 * anonymous device. For all other files, just kill them. 1454 */ 1455 if (flags & FORCECLOSE) { 1456 simple_unlock(&mntvnode_slock); 1457 if (vp->v_type != VBLK && vp->v_type != VCHR) { 1458 vgonel(vp, l); 1459 } else { 1460 vclean(vp, 0, l); 1461 vp->v_op = spec_vnodeop_p; 1462 insmntque(vp, (struct mount *)0); 1463 } 1464 simple_lock(&mntvnode_slock); 1465 continue; 1466 } 1467 #ifdef DEBUG 1468 if (busyprt) 1469 vprint("vflush: busy vnode", vp); 1470 #endif 1471 simple_unlock(&vp->v_interlock); 1472 busy++; 1473 } 1474 simple_unlock(&mntvnode_slock); 1475 if (busy) 1476 return (EBUSY); 1477 return (0); 1478 } 1479 1480 /* 1481 * Disassociate the underlying file system from a vnode. 1482 */ 1483 static void 1484 vclean(struct vnode *vp, int flags, struct lwp *l) 1485 { 1486 struct mount *mp; 1487 int active; 1488 1489 LOCK_ASSERT(simple_lock_held(&vp->v_interlock)); 1490 1491 /* 1492 * Check to see if the vnode is in use. 1493 * If so we have to reference it before we clean it out 1494 * so that its count cannot fall to zero and generate a 1495 * race against ourselves to recycle it. 1496 */ 1497 1498 if ((active = vp->v_usecount) != 0) { 1499 vp->v_usecount++; 1500 #ifdef DIAGNOSTIC 1501 if (vp->v_usecount == 0) { 1502 vprint("vclean", vp); 1503 panic("vclean: usecount overflow"); 1504 } 1505 #endif 1506 } 1507 1508 /* 1509 * Prevent the vnode from being recycled or 1510 * brought into use while we clean it out. 1511 */ 1512 if (vp->v_flag & VXLOCK) 1513 panic("vclean: deadlock, vp %p", vp); 1514 vp->v_flag |= VXLOCK; 1515 if (vp->v_flag & VEXECMAP) { 1516 uvmexp.execpages -= vp->v_uobj.uo_npages; 1517 uvmexp.filepages += vp->v_uobj.uo_npages; 1518 } 1519 vp->v_flag &= ~(VTEXT|VEXECMAP); 1520 1521 /* 1522 * Even if the count is zero, the VOP_INACTIVE routine may still 1523 * have the object locked while it cleans it out. The VOP_LOCK 1524 * ensures that the VOP_INACTIVE routine is done with its work. 1525 * For active vnodes, it ensures that no other activity can 1526 * occur while the underlying object is being cleaned out. 1527 */ 1528 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK); 1529 1530 /* 1531 * Clean out any cached data associated with the vnode. 1532 * If special device, remove it from special device alias list. 1533 * if it is on one. 1534 */ 1535 if (flags & DOCLOSE) { 1536 int error; 1537 struct vnode *vq, *vx; 1538 1539 vn_start_write(vp, &mp, V_WAIT | V_LOWER); 1540 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0); 1541 vn_finished_write(mp, V_LOWER); 1542 if (error) 1543 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0); 1544 KASSERT(error == 0); 1545 KASSERT((vp->v_flag & VONWORKLST) == 0); 1546 1547 if (active) 1548 VOP_CLOSE(vp, FNONBLOCK, NOCRED, NULL); 1549 1550 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 1551 vp->v_specinfo != 0) { 1552 simple_lock(&spechash_slock); 1553 if (vp->v_hashchain != NULL) { 1554 if (*vp->v_hashchain == vp) { 1555 *vp->v_hashchain = vp->v_specnext; 1556 } else { 1557 for (vq = *vp->v_hashchain; vq; 1558 vq = vq->v_specnext) { 1559 if (vq->v_specnext != vp) 1560 continue; 1561 vq->v_specnext = vp->v_specnext; 1562 break; 1563 } 1564 if (vq == NULL) 1565 panic("missing bdev"); 1566 } 1567 if (vp->v_flag & VALIASED) { 1568 vx = NULL; 1569 for (vq = *vp->v_hashchain; vq; 1570 vq = vq->v_specnext) { 1571 if (vq->v_rdev != vp->v_rdev || 1572 vq->v_type != vp->v_type) 1573 continue; 1574 if (vx) 1575 break; 1576 vx = vq; 1577 } 1578 if (vx == NULL) 1579 panic("missing alias"); 1580 if (vq == NULL) 1581 vx->v_flag &= ~VALIASED; 1582 vp->v_flag &= ~VALIASED; 1583 } 1584 } 1585 simple_unlock(&spechash_slock); 1586 FREE(vp->v_specinfo, M_VNODE); 1587 vp->v_specinfo = NULL; 1588 } 1589 } 1590 LOCK_ASSERT(!simple_lock_held(&vp->v_interlock)); 1591 1592 /* 1593 * If purging an active vnode, it must be closed and 1594 * deactivated before being reclaimed. Note that the 1595 * VOP_INACTIVE will unlock the vnode. 1596 */ 1597 if (active) { 1598 VOP_INACTIVE(vp, l); 1599 } else { 1600 /* 1601 * Any other processes trying to obtain this lock must first 1602 * wait for VXLOCK to clear, then call the new lock operation. 1603 */ 1604 VOP_UNLOCK(vp, 0); 1605 } 1606 /* 1607 * Reclaim the vnode. 1608 */ 1609 if (VOP_RECLAIM(vp, l)) 1610 panic("vclean: cannot reclaim, vp %p", vp); 1611 if (active) { 1612 /* 1613 * Inline copy of vrele() since VOP_INACTIVE 1614 * has already been called. 1615 */ 1616 simple_lock(&vp->v_interlock); 1617 if (--vp->v_usecount <= 0) { 1618 #ifdef DIAGNOSTIC 1619 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 1620 vprint("vclean: bad ref count", vp); 1621 panic("vclean: ref cnt"); 1622 } 1623 #endif 1624 /* 1625 * Insert at tail of LRU list. 1626 */ 1627 1628 simple_unlock(&vp->v_interlock); 1629 simple_lock(&vnode_free_list_slock); 1630 #ifdef DIAGNOSTIC 1631 if (vp->v_holdcnt > 0) 1632 panic("vclean: not clean, vp %p", vp); 1633 #endif 1634 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 1635 simple_unlock(&vnode_free_list_slock); 1636 } else 1637 simple_unlock(&vp->v_interlock); 1638 } 1639 1640 KASSERT(vp->v_uobj.uo_npages == 0); 1641 if (vp->v_type == VREG && vp->v_ractx != NULL) { 1642 uvm_ra_freectx(vp->v_ractx); 1643 vp->v_ractx = NULL; 1644 } 1645 cache_purge(vp); 1646 1647 /* 1648 * Done with purge, notify sleepers of the grim news. 1649 */ 1650 vp->v_op = dead_vnodeop_p; 1651 vp->v_tag = VT_NON; 1652 simple_lock(&vp->v_interlock); 1653 VN_KNOTE(vp, NOTE_REVOKE); /* FreeBSD has this in vn_pollgone() */ 1654 vp->v_flag &= ~(VXLOCK|VLOCKSWORK); 1655 if (vp->v_flag & VXWANT) { 1656 vp->v_flag &= ~VXWANT; 1657 simple_unlock(&vp->v_interlock); 1658 wakeup((caddr_t)vp); 1659 } else 1660 simple_unlock(&vp->v_interlock); 1661 } 1662 1663 /* 1664 * Recycle an unused vnode to the front of the free list. 1665 * Release the passed interlock if the vnode will be recycled. 1666 */ 1667 int 1668 vrecycle(struct vnode *vp, struct simplelock *inter_lkp, struct lwp *l) 1669 { 1670 1671 simple_lock(&vp->v_interlock); 1672 if (vp->v_usecount == 0) { 1673 if (inter_lkp) 1674 simple_unlock(inter_lkp); 1675 vgonel(vp, l); 1676 return (1); 1677 } 1678 simple_unlock(&vp->v_interlock); 1679 return (0); 1680 } 1681 1682 /* 1683 * Eliminate all activity associated with a vnode 1684 * in preparation for reuse. 1685 */ 1686 void 1687 vgone(struct vnode *vp) 1688 { 1689 struct lwp *l = curlwp; /* XXX */ 1690 1691 simple_lock(&vp->v_interlock); 1692 vgonel(vp, l); 1693 } 1694 1695 /* 1696 * vgone, with the vp interlock held. 1697 */ 1698 void 1699 vgonel(struct vnode *vp, struct lwp *l) 1700 { 1701 1702 LOCK_ASSERT(simple_lock_held(&vp->v_interlock)); 1703 1704 /* 1705 * If a vgone (or vclean) is already in progress, 1706 * wait until it is done and return. 1707 */ 1708 1709 if (vp->v_flag & VXLOCK) { 1710 vp->v_flag |= VXWANT; 1711 ltsleep(vp, PINOD | PNORELOCK, "vgone", 0, &vp->v_interlock); 1712 return; 1713 } 1714 1715 /* 1716 * Clean out the filesystem specific data. 1717 */ 1718 1719 vclean(vp, DOCLOSE, l); 1720 KASSERT((vp->v_flag & VONWORKLST) == 0); 1721 1722 /* 1723 * Delete from old mount point vnode list, if on one. 1724 */ 1725 1726 if (vp->v_mount != NULL) 1727 insmntque(vp, (struct mount *)0); 1728 1729 /* 1730 * The test of the back pointer and the reference count of 1731 * zero is because it will be removed from the free list by 1732 * getcleanvnode, but will not have its reference count 1733 * incremented until after calling vgone. If the reference 1734 * count were incremented first, vgone would (incorrectly) 1735 * try to close the previous instance of the underlying object. 1736 * So, the back pointer is explicitly set to `0xdeadb' in 1737 * getnewvnode after removing it from the freelist to ensure 1738 * that we do not try to move it here. 1739 */ 1740 1741 vp->v_type = VBAD; 1742 if (vp->v_usecount == 0) { 1743 boolean_t dofree; 1744 1745 simple_lock(&vnode_free_list_slock); 1746 if (vp->v_holdcnt > 0) 1747 panic("vgonel: not clean, vp %p", vp); 1748 /* 1749 * if it isn't on the freelist, we're called by getcleanvnode 1750 * and vnode is being re-used. otherwise, we'll free it. 1751 */ 1752 dofree = vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb; 1753 if (dofree) { 1754 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1755 numvnodes--; 1756 } 1757 simple_unlock(&vnode_free_list_slock); 1758 if (dofree) 1759 pool_put(&vnode_pool, vp); 1760 } 1761 } 1762 1763 /* 1764 * Lookup a vnode by device number. 1765 */ 1766 int 1767 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp) 1768 { 1769 struct vnode *vp; 1770 int rc = 0; 1771 1772 simple_lock(&spechash_slock); 1773 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 1774 if (dev != vp->v_rdev || type != vp->v_type) 1775 continue; 1776 *vpp = vp; 1777 rc = 1; 1778 break; 1779 } 1780 simple_unlock(&spechash_slock); 1781 return (rc); 1782 } 1783 1784 /* 1785 * Revoke all the vnodes corresponding to the specified minor number 1786 * range (endpoints inclusive) of the specified major. 1787 */ 1788 void 1789 vdevgone(int maj, int minl, int minh, enum vtype type) 1790 { 1791 struct vnode *vp; 1792 int mn; 1793 1794 for (mn = minl; mn <= minh; mn++) 1795 if (vfinddev(makedev(maj, mn), type, &vp)) 1796 VOP_REVOKE(vp, REVOKEALL); 1797 } 1798 1799 /* 1800 * Calculate the total number of references to a special device. 1801 */ 1802 int 1803 vcount(struct vnode *vp) 1804 { 1805 struct vnode *vq, *vnext; 1806 int count; 1807 1808 loop: 1809 if ((vp->v_flag & VALIASED) == 0) 1810 return (vp->v_usecount); 1811 simple_lock(&spechash_slock); 1812 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) { 1813 vnext = vq->v_specnext; 1814 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 1815 continue; 1816 /* 1817 * Alias, but not in use, so flush it out. 1818 */ 1819 if (vq->v_usecount == 0 && vq != vp && 1820 (vq->v_flag & VXLOCK) == 0) { 1821 simple_unlock(&spechash_slock); 1822 vgone(vq); 1823 goto loop; 1824 } 1825 count += vq->v_usecount; 1826 } 1827 simple_unlock(&spechash_slock); 1828 return (count); 1829 } 1830 1831 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 1832 #define ARRAY_PRINT(idx, arr) \ 1833 ((idx) > 0 && (idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN") 1834 1835 const char * const vnode_tags[] = { VNODE_TAGS }; 1836 const char * const vnode_types[] = { VNODE_TYPES }; 1837 const char vnode_flagbits[] = VNODE_FLAGBITS; 1838 1839 /* 1840 * Print out a description of a vnode. 1841 */ 1842 void 1843 vprint(const char *label, struct vnode *vp) 1844 { 1845 char bf[96]; 1846 1847 if (label != NULL) 1848 printf("%s: ", label); 1849 printf("tag %s(%d) type %s(%d), usecount %d, writecount %ld, " 1850 "refcount %ld,", ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag, 1851 ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type, 1852 vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 1853 bitmask_snprintf(vp->v_flag, vnode_flagbits, bf, sizeof(bf)); 1854 if (bf[0] != '\0') 1855 printf(" flags (%s)", &bf[1]); 1856 if (vp->v_data == NULL) { 1857 printf("\n"); 1858 } else { 1859 printf("\n\t"); 1860 VOP_PRINT(vp); 1861 } 1862 } 1863 1864 #ifdef DEBUG 1865 /* 1866 * List all of the locked vnodes in the system. 1867 * Called when debugging the kernel. 1868 */ 1869 void 1870 printlockedvnodes(void) 1871 { 1872 struct mount *mp, *nmp; 1873 struct vnode *vp; 1874 1875 printf("Locked vnodes\n"); 1876 simple_lock(&mountlist_slock); 1877 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 1878 mp = nmp) { 1879 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 1880 nmp = CIRCLEQ_NEXT(mp, mnt_list); 1881 continue; 1882 } 1883 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 1884 if (VOP_ISLOCKED(vp)) 1885 vprint(NULL, vp); 1886 } 1887 simple_lock(&mountlist_slock); 1888 nmp = CIRCLEQ_NEXT(mp, mnt_list); 1889 vfs_unbusy(mp); 1890 } 1891 simple_unlock(&mountlist_slock); 1892 } 1893 #endif 1894 1895 /* 1896 * sysctl helper routine to return list of supported fstypes 1897 */ 1898 static int 1899 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS) 1900 { 1901 char bf[MFSNAMELEN]; 1902 char *where = oldp; 1903 struct vfsops *v; 1904 size_t needed, left, slen; 1905 int error, first; 1906 1907 if (newp != NULL) 1908 return (EPERM); 1909 if (namelen != 0) 1910 return (EINVAL); 1911 1912 first = 1; 1913 error = 0; 1914 needed = 0; 1915 left = *oldlenp; 1916 1917 LIST_FOREACH(v, &vfs_list, vfs_list) { 1918 if (where == NULL) 1919 needed += strlen(v->vfs_name) + 1; 1920 else { 1921 memset(bf, 0, sizeof(bf)); 1922 if (first) { 1923 strncpy(bf, v->vfs_name, sizeof(bf)); 1924 first = 0; 1925 } else { 1926 bf[0] = ' '; 1927 strncpy(bf + 1, v->vfs_name, sizeof(bf) - 1); 1928 } 1929 bf[sizeof(bf)-1] = '\0'; 1930 slen = strlen(bf); 1931 if (left < slen + 1) 1932 break; 1933 /* +1 to copy out the trailing NUL byte */ 1934 error = copyout(bf, where, slen + 1); 1935 if (error) 1936 break; 1937 where += slen; 1938 needed += slen; 1939 left -= slen; 1940 } 1941 } 1942 *oldlenp = needed; 1943 return (error); 1944 } 1945 1946 /* 1947 * Top level filesystem related information gathering. 1948 */ 1949 SYSCTL_SETUP(sysctl_vfs_setup, "sysctl vfs subtree setup") 1950 { 1951 sysctl_createv(clog, 0, NULL, NULL, 1952 CTLFLAG_PERMANENT, 1953 CTLTYPE_NODE, "vfs", NULL, 1954 NULL, 0, NULL, 0, 1955 CTL_VFS, CTL_EOL); 1956 sysctl_createv(clog, 0, NULL, NULL, 1957 CTLFLAG_PERMANENT, 1958 CTLTYPE_NODE, "generic", 1959 SYSCTL_DESCR("Non-specific vfs related information"), 1960 NULL, 0, NULL, 0, 1961 CTL_VFS, VFS_GENERIC, CTL_EOL); 1962 sysctl_createv(clog, 0, NULL, NULL, 1963 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1964 CTLTYPE_INT, "usermount", 1965 SYSCTL_DESCR("Whether unprivileged users may mount " 1966 "filesystems"), 1967 NULL, 0, &dovfsusermount, 0, 1968 CTL_VFS, VFS_GENERIC, VFS_USERMOUNT, CTL_EOL); 1969 sysctl_createv(clog, 0, NULL, NULL, 1970 CTLFLAG_PERMANENT, 1971 CTLTYPE_STRING, "fstypes", 1972 SYSCTL_DESCR("List of file systems present"), 1973 sysctl_vfs_generic_fstypes, 0, NULL, 0, 1974 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL); 1975 sysctl_createv(clog, 0, NULL, NULL, 1976 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1977 CTLTYPE_INT, "magiclinks", 1978 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"), 1979 NULL, 0, &vfs_magiclinks, 0, 1980 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL); 1981 } 1982 1983 1984 int kinfo_vdebug = 1; 1985 int kinfo_vgetfailed; 1986 #define KINFO_VNODESLOP 10 1987 /* 1988 * Dump vnode list (via sysctl). 1989 * Copyout address of vnode followed by vnode. 1990 */ 1991 /* ARGSUSED */ 1992 int 1993 sysctl_kern_vnode(SYSCTLFN_ARGS) 1994 { 1995 char *where = oldp; 1996 size_t *sizep = oldlenp; 1997 struct mount *mp, *nmp; 1998 struct vnode *nvp, *vp; 1999 char *bp = where, *savebp; 2000 char *ewhere; 2001 int error; 2002 2003 if (namelen != 0) 2004 return (EOPNOTSUPP); 2005 if (newp != NULL) 2006 return (EPERM); 2007 2008 #define VPTRSZ sizeof(struct vnode *) 2009 #define VNODESZ sizeof(struct vnode) 2010 if (where == NULL) { 2011 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ); 2012 return (0); 2013 } 2014 ewhere = where + *sizep; 2015 2016 simple_lock(&mountlist_slock); 2017 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 2018 mp = nmp) { 2019 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 2020 nmp = CIRCLEQ_NEXT(mp, mnt_list); 2021 continue; 2022 } 2023 savebp = bp; 2024 again: 2025 simple_lock(&mntvnode_slock); 2026 for (vp = LIST_FIRST(&mp->mnt_vnodelist); 2027 vp != NULL; 2028 vp = nvp) { 2029 /* 2030 * Check that the vp is still associated with 2031 * this filesystem. RACE: could have been 2032 * recycled onto the same filesystem. 2033 */ 2034 if (vp->v_mount != mp) { 2035 simple_unlock(&mntvnode_slock); 2036 if (kinfo_vdebug) 2037 printf("kinfo: vp changed\n"); 2038 bp = savebp; 2039 goto again; 2040 } 2041 nvp = LIST_NEXT(vp, v_mntvnodes); 2042 if (bp + VPTRSZ + VNODESZ > ewhere) { 2043 simple_unlock(&mntvnode_slock); 2044 *sizep = bp - where; 2045 return (ENOMEM); 2046 } 2047 simple_unlock(&mntvnode_slock); 2048 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) || 2049 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ))) 2050 return (error); 2051 bp += VPTRSZ + VNODESZ; 2052 simple_lock(&mntvnode_slock); 2053 } 2054 simple_unlock(&mntvnode_slock); 2055 simple_lock(&mountlist_slock); 2056 nmp = CIRCLEQ_NEXT(mp, mnt_list); 2057 vfs_unbusy(mp); 2058 } 2059 simple_unlock(&mountlist_slock); 2060 2061 *sizep = bp - where; 2062 return (0); 2063 } 2064 2065 /* 2066 * Check to see if a filesystem is mounted on a block device. 2067 */ 2068 int 2069 vfs_mountedon(struct vnode *vp) 2070 { 2071 struct vnode *vq; 2072 int error = 0; 2073 2074 if (vp->v_type != VBLK) 2075 return ENOTBLK; 2076 if (vp->v_specmountpoint != NULL) 2077 return (EBUSY); 2078 if (vp->v_flag & VALIASED) { 2079 simple_lock(&spechash_slock); 2080 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) { 2081 if (vq->v_rdev != vp->v_rdev || 2082 vq->v_type != vp->v_type) 2083 continue; 2084 if (vq->v_specmountpoint != NULL) { 2085 error = EBUSY; 2086 break; 2087 } 2088 } 2089 simple_unlock(&spechash_slock); 2090 } 2091 return (error); 2092 } 2093 2094 /* 2095 * Do the usual access checking. 2096 * file_mode, uid and gid are from the vnode in question, 2097 * while acc_mode and cred are from the VOP_ACCESS parameter list 2098 */ 2099 int 2100 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 2101 mode_t acc_mode, kauth_cred_t cred) 2102 { 2103 mode_t mask; 2104 int error, ismember; 2105 2106 /* 2107 * Super-user always gets read/write access, but execute access depends 2108 * on at least one execute bit being set. 2109 */ 2110 if (kauth_cred_geteuid(cred) == 0) { 2111 if ((acc_mode & VEXEC) && type != VDIR && 2112 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 2113 return (EACCES); 2114 return (0); 2115 } 2116 2117 mask = 0; 2118 2119 /* Otherwise, check the owner. */ 2120 if (kauth_cred_geteuid(cred) == uid) { 2121 if (acc_mode & VEXEC) 2122 mask |= S_IXUSR; 2123 if (acc_mode & VREAD) 2124 mask |= S_IRUSR; 2125 if (acc_mode & VWRITE) 2126 mask |= S_IWUSR; 2127 return ((file_mode & mask) == mask ? 0 : EACCES); 2128 } 2129 2130 /* Otherwise, check the groups. */ 2131 error = kauth_cred_ismember_gid(cred, gid, &ismember); 2132 if (error) 2133 return (error); 2134 if (kauth_cred_getegid(cred) == gid || ismember) { 2135 if (acc_mode & VEXEC) 2136 mask |= S_IXGRP; 2137 if (acc_mode & VREAD) 2138 mask |= S_IRGRP; 2139 if (acc_mode & VWRITE) 2140 mask |= S_IWGRP; 2141 return ((file_mode & mask) == mask ? 0 : EACCES); 2142 } 2143 2144 /* Otherwise, check everyone else. */ 2145 if (acc_mode & VEXEC) 2146 mask |= S_IXOTH; 2147 if (acc_mode & VREAD) 2148 mask |= S_IROTH; 2149 if (acc_mode & VWRITE) 2150 mask |= S_IWOTH; 2151 return ((file_mode & mask) == mask ? 0 : EACCES); 2152 } 2153 2154 /* 2155 * Unmount all file systems. 2156 * We traverse the list in reverse order under the assumption that doing so 2157 * will avoid needing to worry about dependencies. 2158 */ 2159 void 2160 vfs_unmountall(struct lwp *l) 2161 { 2162 struct mount *mp, *nmp; 2163 int allerror, error; 2164 2165 printf("unmounting file systems..."); 2166 for (allerror = 0, 2167 mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) { 2168 nmp = mp->mnt_list.cqe_prev; 2169 #ifdef DEBUG 2170 printf("\nunmounting %s (%s)...", 2171 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 2172 #endif 2173 /* 2174 * XXX Freeze syncer. Must do this before locking the 2175 * mount point. See dounmount() for details. 2176 */ 2177 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL); 2178 if (vfs_busy(mp, 0, 0)) { 2179 lockmgr(&syncer_lock, LK_RELEASE, NULL); 2180 continue; 2181 } 2182 if ((error = dounmount(mp, MNT_FORCE, l)) != 0) { 2183 printf("unmount of %s failed with error %d\n", 2184 mp->mnt_stat.f_mntonname, error); 2185 allerror = 1; 2186 } 2187 } 2188 printf(" done\n"); 2189 if (allerror) 2190 printf("WARNING: some file systems would not unmount\n"); 2191 } 2192 2193 extern struct simplelock bqueue_slock; /* XXX */ 2194 2195 /* 2196 * Sync and unmount file systems before shutting down. 2197 */ 2198 void 2199 vfs_shutdown(void) 2200 { 2201 struct lwp *l; 2202 2203 /* XXX we're certainly not running in lwp0's context! */ 2204 l = curlwp; 2205 if (l == NULL) 2206 l = &lwp0; 2207 2208 printf("syncing disks... "); 2209 2210 /* remove user process from run queue */ 2211 suspendsched(); 2212 (void) spl0(); 2213 2214 /* avoid coming back this way again if we panic. */ 2215 doing_shutdown = 1; 2216 2217 sys_sync(l, NULL, NULL); 2218 2219 /* Wait for sync to finish. */ 2220 if (buf_syncwait() != 0) { 2221 #if defined(DDB) && defined(DEBUG_HALT_BUSY) 2222 Debugger(); 2223 #endif 2224 printf("giving up\n"); 2225 return; 2226 } else 2227 printf("done\n"); 2228 2229 /* 2230 * If we've panic'd, don't make the situation potentially 2231 * worse by unmounting the file systems. 2232 */ 2233 if (panicstr != NULL) 2234 return; 2235 2236 /* Release inodes held by texts before update. */ 2237 #ifdef notdef 2238 vnshutdown(); 2239 #endif 2240 /* Unmount file systems. */ 2241 vfs_unmountall(l); 2242 } 2243 2244 /* 2245 * Mount the root file system. If the operator didn't specify a 2246 * file system to use, try all possible file systems until one 2247 * succeeds. 2248 */ 2249 int 2250 vfs_mountroot(void) 2251 { 2252 struct vfsops *v; 2253 int error = ENODEV; 2254 2255 if (root_device == NULL) 2256 panic("vfs_mountroot: root device unknown"); 2257 2258 switch (device_class(root_device)) { 2259 case DV_IFNET: 2260 if (rootdev != NODEV) 2261 panic("vfs_mountroot: rootdev set for DV_IFNET " 2262 "(0x%08x -> %d,%d)", rootdev, 2263 major(rootdev), minor(rootdev)); 2264 break; 2265 2266 case DV_DISK: 2267 if (rootdev == NODEV) 2268 panic("vfs_mountroot: rootdev not set for DV_DISK"); 2269 if (bdevvp(rootdev, &rootvp)) 2270 panic("vfs_mountroot: can't get vnode for rootdev"); 2271 error = VOP_OPEN(rootvp, FREAD, FSCRED, curlwp); 2272 if (error) { 2273 printf("vfs_mountroot: can't open root device\n"); 2274 return (error); 2275 } 2276 break; 2277 2278 default: 2279 printf("%s: inappropriate for root file system\n", 2280 root_device->dv_xname); 2281 return (ENODEV); 2282 } 2283 2284 /* 2285 * If user specified a file system, use it. 2286 */ 2287 if (mountroot != NULL) { 2288 error = (*mountroot)(); 2289 goto done; 2290 } 2291 2292 /* 2293 * Try each file system currently configured into the kernel. 2294 */ 2295 LIST_FOREACH(v, &vfs_list, vfs_list) { 2296 if (v->vfs_mountroot == NULL) 2297 continue; 2298 #ifdef DEBUG 2299 aprint_normal("mountroot: trying %s...\n", v->vfs_name); 2300 #endif 2301 error = (*v->vfs_mountroot)(); 2302 if (!error) { 2303 aprint_normal("root file system type: %s\n", 2304 v->vfs_name); 2305 break; 2306 } 2307 } 2308 2309 if (v == NULL) { 2310 printf("no file system for %s", root_device->dv_xname); 2311 if (device_class(root_device) == DV_DISK) 2312 printf(" (dev 0x%x)", rootdev); 2313 printf("\n"); 2314 error = EFTYPE; 2315 } 2316 2317 done: 2318 if (error && device_class(root_device) == DV_DISK) { 2319 VOP_CLOSE(rootvp, FREAD, FSCRED, curlwp); 2320 vrele(rootvp); 2321 } 2322 return (error); 2323 } 2324 2325 /* 2326 * Given a file system name, look up the vfsops for that 2327 * file system, or return NULL if file system isn't present 2328 * in the kernel. 2329 */ 2330 struct vfsops * 2331 vfs_getopsbyname(const char *name) 2332 { 2333 struct vfsops *v; 2334 2335 LIST_FOREACH(v, &vfs_list, vfs_list) { 2336 if (strcmp(v->vfs_name, name) == 0) 2337 break; 2338 } 2339 2340 return (v); 2341 } 2342 2343 /* 2344 * Establish a file system and initialize it. 2345 */ 2346 int 2347 vfs_attach(struct vfsops *vfs) 2348 { 2349 struct vfsops *v; 2350 int error = 0; 2351 2352 2353 /* 2354 * Make sure this file system doesn't already exist. 2355 */ 2356 LIST_FOREACH(v, &vfs_list, vfs_list) { 2357 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) { 2358 error = EEXIST; 2359 goto out; 2360 } 2361 } 2362 2363 /* 2364 * Initialize the vnode operations for this file system. 2365 */ 2366 vfs_opv_init(vfs->vfs_opv_descs); 2367 2368 /* 2369 * Now initialize the file system itself. 2370 */ 2371 (*vfs->vfs_init)(); 2372 2373 /* 2374 * ...and link it into the kernel's list. 2375 */ 2376 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list); 2377 2378 /* 2379 * Sanity: make sure the reference count is 0. 2380 */ 2381 vfs->vfs_refcount = 0; 2382 2383 out: 2384 return (error); 2385 } 2386 2387 /* 2388 * Remove a file system from the kernel. 2389 */ 2390 int 2391 vfs_detach(struct vfsops *vfs) 2392 { 2393 struct vfsops *v; 2394 2395 /* 2396 * Make sure no one is using the filesystem. 2397 */ 2398 if (vfs->vfs_refcount != 0) 2399 return (EBUSY); 2400 2401 /* 2402 * ...and remove it from the kernel's list. 2403 */ 2404 LIST_FOREACH(v, &vfs_list, vfs_list) { 2405 if (v == vfs) { 2406 LIST_REMOVE(v, vfs_list); 2407 break; 2408 } 2409 } 2410 2411 if (v == NULL) 2412 return (ESRCH); 2413 2414 /* 2415 * Now run the file system-specific cleanups. 2416 */ 2417 (*vfs->vfs_done)(); 2418 2419 /* 2420 * Free the vnode operations vector. 2421 */ 2422 vfs_opv_free(vfs->vfs_opv_descs); 2423 return (0); 2424 } 2425 2426 void 2427 vfs_reinit(void) 2428 { 2429 struct vfsops *vfs; 2430 2431 LIST_FOREACH(vfs, &vfs_list, vfs_list) { 2432 if (vfs->vfs_reinit) { 2433 (*vfs->vfs_reinit)(); 2434 } 2435 } 2436 } 2437 2438 /* 2439 * Request a filesystem to suspend write operations. 2440 */ 2441 int 2442 vfs_write_suspend(struct mount *mp, int slpflag, int slptimeo) 2443 { 2444 struct lwp *l = curlwp; /* XXX */ 2445 int error; 2446 2447 while ((mp->mnt_iflag & IMNT_SUSPEND)) { 2448 if (slptimeo < 0) 2449 return EWOULDBLOCK; 2450 error = tsleep(&mp->mnt_flag, slpflag, "suspwt1", slptimeo); 2451 if (error) 2452 return error; 2453 } 2454 mp->mnt_iflag |= IMNT_SUSPEND; 2455 2456 simple_lock(&mp->mnt_slock); 2457 if (mp->mnt_writeopcountupper > 0) 2458 ltsleep(&mp->mnt_writeopcountupper, PUSER - 1, "suspwt", 2459 0, &mp->mnt_slock); 2460 simple_unlock(&mp->mnt_slock); 2461 2462 error = VFS_SYNC(mp, MNT_WAIT, l->l_proc->p_cred, l); 2463 if (error) { 2464 vfs_write_resume(mp); 2465 return error; 2466 } 2467 mp->mnt_iflag |= IMNT_SUSPENDLOW; 2468 2469 simple_lock(&mp->mnt_slock); 2470 if (mp->mnt_writeopcountlower > 0) 2471 ltsleep(&mp->mnt_writeopcountlower, PUSER - 1, "suspwt", 2472 0, &mp->mnt_slock); 2473 mp->mnt_iflag |= IMNT_SUSPENDED; 2474 simple_unlock(&mp->mnt_slock); 2475 2476 return 0; 2477 } 2478 2479 /* 2480 * Request a filesystem to resume write operations. 2481 */ 2482 void 2483 vfs_write_resume(struct mount *mp) 2484 { 2485 2486 if ((mp->mnt_iflag & IMNT_SUSPEND) == 0) 2487 return; 2488 mp->mnt_iflag &= ~(IMNT_SUSPEND | IMNT_SUSPENDLOW | IMNT_SUSPENDED); 2489 wakeup(&mp->mnt_flag); 2490 } 2491 2492 void 2493 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp) 2494 { 2495 const struct statvfs *mbp; 2496 2497 if (sbp == (mbp = &mp->mnt_stat)) 2498 return; 2499 2500 (void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx)); 2501 sbp->f_fsid = mbp->f_fsid; 2502 sbp->f_owner = mbp->f_owner; 2503 sbp->f_flag = mbp->f_flag; 2504 sbp->f_syncwrites = mbp->f_syncwrites; 2505 sbp->f_asyncwrites = mbp->f_asyncwrites; 2506 sbp->f_syncreads = mbp->f_syncreads; 2507 sbp->f_asyncreads = mbp->f_asyncreads; 2508 (void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare)); 2509 (void)memcpy(sbp->f_fstypename, mbp->f_fstypename, 2510 sizeof(sbp->f_fstypename)); 2511 (void)memcpy(sbp->f_mntonname, mbp->f_mntonname, 2512 sizeof(sbp->f_mntonname)); 2513 (void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, 2514 sizeof(sbp->f_mntfromname)); 2515 sbp->f_namemax = mbp->f_namemax; 2516 } 2517 2518 int 2519 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom, 2520 struct mount *mp, struct lwp *l) 2521 { 2522 int error; 2523 size_t size; 2524 struct statvfs *sfs = &mp->mnt_stat; 2525 int (*fun)(const void *, void *, size_t, size_t *); 2526 2527 (void)strncpy(mp->mnt_stat.f_fstypename, mp->mnt_op->vfs_name, 2528 sizeof(mp->mnt_stat.f_fstypename)); 2529 2530 if (onp) { 2531 struct cwdinfo *cwdi = l->l_proc->p_cwdi; 2532 fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr; 2533 if (cwdi->cwdi_rdir != NULL) { 2534 size_t len; 2535 char *bp; 2536 char *path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 2537 2538 if (!path) /* XXX can't happen with M_WAITOK */ 2539 return ENOMEM; 2540 2541 bp = path + MAXPATHLEN; 2542 *--bp = '\0'; 2543 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, 2544 path, MAXPATHLEN / 2, 0, l); 2545 if (error) { 2546 free(path, M_TEMP); 2547 return error; 2548 } 2549 2550 len = strlen(bp); 2551 if (len > sizeof(sfs->f_mntonname) - 1) 2552 len = sizeof(sfs->f_mntonname) - 1; 2553 (void)strncpy(sfs->f_mntonname, bp, len); 2554 free(path, M_TEMP); 2555 2556 if (len < sizeof(sfs->f_mntonname) - 1) { 2557 error = (*fun)(onp, &sfs->f_mntonname[len], 2558 sizeof(sfs->f_mntonname) - len - 1, &size); 2559 if (error) 2560 return error; 2561 size += len; 2562 } else { 2563 size = len; 2564 } 2565 } else { 2566 error = (*fun)(onp, &sfs->f_mntonname, 2567 sizeof(sfs->f_mntonname) - 1, &size); 2568 if (error) 2569 return error; 2570 } 2571 (void)memset(sfs->f_mntonname + size, 0, 2572 sizeof(sfs->f_mntonname) - size); 2573 } 2574 2575 if (fromp) { 2576 fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr; 2577 error = (*fun)(fromp, sfs->f_mntfromname, 2578 sizeof(sfs->f_mntfromname) - 1, &size); 2579 if (error) 2580 return error; 2581 (void)memset(sfs->f_mntfromname + size, 0, 2582 sizeof(sfs->f_mntfromname) - size); 2583 } 2584 return 0; 2585 } 2586 2587 void 2588 vfs_timestamp(struct timespec *ts) 2589 { 2590 2591 nanotime(ts); 2592 } 2593 2594 #ifdef DDB 2595 static const char buf_flagbits[] = BUF_FLAGBITS; 2596 2597 void 2598 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...)) 2599 { 2600 char bf[1024]; 2601 2602 (*pr)(" vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" rawblkno 0x%" 2603 PRIx64 " dev 0x%x\n", 2604 bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_rawblkno, bp->b_dev); 2605 2606 bitmask_snprintf(bp->b_flags, buf_flagbits, bf, sizeof(bf)); 2607 (*pr)(" error %d flags 0x%s\n", bp->b_error, bf); 2608 2609 (*pr)(" bufsize 0x%lx bcount 0x%lx resid 0x%lx\n", 2610 bp->b_bufsize, bp->b_bcount, bp->b_resid); 2611 (*pr)(" data %p saveaddr %p dep %p\n", 2612 bp->b_data, bp->b_saveaddr, LIST_FIRST(&bp->b_dep)); 2613 (*pr)(" iodone %p\n", bp->b_iodone); 2614 } 2615 2616 2617 void 2618 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...)) 2619 { 2620 char bf[256]; 2621 2622 uvm_object_printit(&vp->v_uobj, full, pr); 2623 bitmask_snprintf(vp->v_flag, vnode_flagbits, bf, sizeof(bf)); 2624 (*pr)("\nVNODE flags %s\n", bf); 2625 (*pr)("mp %p numoutput %d size 0x%llx\n", 2626 vp->v_mount, vp->v_numoutput, vp->v_size); 2627 2628 (*pr)("data %p usecount %d writecount %ld holdcnt %ld numoutput %d\n", 2629 vp->v_data, vp->v_usecount, vp->v_writecount, 2630 vp->v_holdcnt, vp->v_numoutput); 2631 2632 (*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n", 2633 ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag, 2634 ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type, 2635 vp->v_mount, vp->v_mountedhere); 2636 2637 if (full) { 2638 struct buf *bp; 2639 2640 (*pr)("clean bufs:\n"); 2641 LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) { 2642 (*pr)(" bp %p\n", bp); 2643 vfs_buf_print(bp, full, pr); 2644 } 2645 2646 (*pr)("dirty bufs:\n"); 2647 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 2648 (*pr)(" bp %p\n", bp); 2649 vfs_buf_print(bp, full, pr); 2650 } 2651 } 2652 } 2653 2654 void 2655 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...)) 2656 { 2657 char sbuf[256]; 2658 2659 (*pr)("vnodecovered = %p syncer = %p data = %p\n", 2660 mp->mnt_vnodecovered,mp->mnt_syncer,mp->mnt_data); 2661 2662 (*pr)("fs_bshift %d dev_bshift = %d\n", 2663 mp->mnt_fs_bshift,mp->mnt_dev_bshift); 2664 2665 bitmask_snprintf(mp->mnt_flag, __MNT_FLAG_BITS, sbuf, sizeof(sbuf)); 2666 (*pr)("flag = %s\n", sbuf); 2667 2668 bitmask_snprintf(mp->mnt_iflag, __IMNT_FLAG_BITS, sbuf, sizeof(sbuf)); 2669 (*pr)("iflag = %s\n", sbuf); 2670 2671 /* XXX use lockmgr_printinfo */ 2672 if (mp->mnt_lock.lk_sharecount) 2673 (*pr)(" lock type %s: SHARED (count %d)", mp->mnt_lock.lk_wmesg, 2674 mp->mnt_lock.lk_sharecount); 2675 else if (mp->mnt_lock.lk_flags & LK_HAVE_EXCL) { 2676 (*pr)(" lock type %s: EXCL (count %d) by ", 2677 mp->mnt_lock.lk_wmesg, mp->mnt_lock.lk_exclusivecount); 2678 if (mp->mnt_lock.lk_flags & LK_SPIN) 2679 (*pr)("processor %lu", mp->mnt_lock.lk_cpu); 2680 else 2681 (*pr)("pid %d.%d", mp->mnt_lock.lk_lockholder, 2682 mp->mnt_lock.lk_locklwp); 2683 } else 2684 (*pr)(" not locked"); 2685 if ((mp->mnt_lock.lk_flags & LK_SPIN) == 0 && mp->mnt_lock.lk_waitcount > 0) 2686 (*pr)(" with %d pending", mp->mnt_lock.lk_waitcount); 2687 2688 (*pr)("\n"); 2689 2690 if (mp->mnt_unmounter) { 2691 (*pr)("unmounter pid = %d ",mp->mnt_unmounter->l_proc); 2692 } 2693 (*pr)("wcnt = %d, writeopcountupper = %d, writeopcountupper = %d\n", 2694 mp->mnt_wcnt,mp->mnt_writeopcountupper,mp->mnt_writeopcountlower); 2695 2696 (*pr)("statvfs cache:\n"); 2697 (*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize); 2698 (*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize); 2699 (*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize); 2700 2701 (*pr)("\tblocks = "PRIu64"\n",mp->mnt_stat.f_blocks); 2702 (*pr)("\tbfree = "PRIu64"\n",mp->mnt_stat.f_bfree); 2703 (*pr)("\tbavail = "PRIu64"\n",mp->mnt_stat.f_bavail); 2704 (*pr)("\tbresvd = "PRIu64"\n",mp->mnt_stat.f_bresvd); 2705 2706 (*pr)("\tfiles = "PRIu64"\n",mp->mnt_stat.f_files); 2707 (*pr)("\tffree = "PRIu64"\n",mp->mnt_stat.f_ffree); 2708 (*pr)("\tfavail = "PRIu64"\n",mp->mnt_stat.f_favail); 2709 (*pr)("\tfresvd = "PRIu64"\n",mp->mnt_stat.f_fresvd); 2710 2711 (*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n", 2712 mp->mnt_stat.f_fsidx.__fsid_val[0], 2713 mp->mnt_stat.f_fsidx.__fsid_val[1]); 2714 2715 (*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner); 2716 (*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax); 2717 2718 bitmask_snprintf(mp->mnt_stat.f_flag, __MNT_FLAG_BITS, sbuf, 2719 sizeof(sbuf)); 2720 (*pr)("\tflag = %s\n",sbuf); 2721 (*pr)("\tsyncwrites = " PRIu64 "\n",mp->mnt_stat.f_syncwrites); 2722 (*pr)("\tasyncwrites = " PRIu64 "\n",mp->mnt_stat.f_asyncwrites); 2723 (*pr)("\tsyncreads = " PRIu64 "\n",mp->mnt_stat.f_syncreads); 2724 (*pr)("\tasyncreads = " PRIu64 "\n",mp->mnt_stat.f_asyncreads); 2725 (*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename); 2726 (*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname); 2727 (*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname); 2728 2729 { 2730 int cnt = 0; 2731 struct vnode *vp; 2732 (*pr)("locked vnodes ="); 2733 /* XXX would take mountlist lock, except ddb may not have context */ 2734 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2735 if (VOP_ISLOCKED(vp)) { 2736 if ((++cnt % 6) == 0) { 2737 (*pr)(" %p,\n\t", vp); 2738 } else { 2739 (*pr)(" %p,", vp); 2740 } 2741 } 2742 } 2743 (*pr)("\n"); 2744 } 2745 2746 if (full) { 2747 int cnt = 0; 2748 struct vnode *vp; 2749 (*pr)("all vnodes ="); 2750 /* XXX would take mountlist lock, except ddb may not have context */ 2751 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2752 if (!LIST_NEXT(vp, v_mntvnodes)) { 2753 (*pr)(" %p", vp); 2754 } else if ((++cnt % 6) == 0) { 2755 (*pr)(" %p,\n\t", vp); 2756 } else { 2757 (*pr)(" %p,", vp); 2758 } 2759 } 2760 (*pr)("\n", vp); 2761 } 2762 } 2763 #endif /* DDB */ 2764