1 /* $NetBSD: vfs_subr.c,v 1.404 2010/06/06 08:01:31 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998, 2004, 2005, 2007, 2008 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, by Charles M. Hannum, and by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * (c) UNIX System Laboratories, Inc. 37 * All or some portions of this file are derived from material licensed 38 * to the University of California by American Telephone and Telegraph 39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 40 * the permission of UNIX System Laboratories, Inc. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94 67 */ 68 69 /* 70 * Note on v_usecount and locking: 71 * 72 * At nearly all points it is known that v_usecount could be zero, the 73 * vnode interlock will be held. 74 * 75 * To change v_usecount away from zero, the interlock must be held. To 76 * change from a non-zero value to zero, again the interlock must be 77 * held. 78 * 79 * There's a flag bit, VC_XLOCK, embedded in v_usecount. 80 * To raise v_usecount, if the VC_XLOCK bit is set in it, the interlock 81 * must be held. 82 * To modify the VC_XLOCK bit, the interlock must be held. 83 * We always keep the usecount (v_usecount & VC_MASK) non-zero while the 84 * VC_XLOCK bit is set. 85 * 86 * Unless the VC_XLOCK bit is set, changing the usecount from a non-zero 87 * value to a non-zero value can safely be done using atomic operations, 88 * without the interlock held. 89 * Even if the VC_XLOCK bit is set, decreasing the usecount to a non-zero 90 * value can be done using atomic operations, without the interlock held. 91 */ 92 93 #include <sys/cdefs.h> 94 __KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.404 2010/06/06 08:01:31 hannken Exp $"); 95 96 #include "opt_ddb.h" 97 #include "opt_compat_netbsd.h" 98 #include "opt_compat_43.h" 99 100 #include <sys/param.h> 101 #include <sys/systm.h> 102 #include <sys/conf.h> 103 #include <sys/dirent.h> 104 #include <sys/proc.h> 105 #include <sys/kernel.h> 106 #include <sys/mount.h> 107 #include <sys/fcntl.h> 108 #include <sys/vnode.h> 109 #include <sys/stat.h> 110 #include <sys/namei.h> 111 #include <sys/ucred.h> 112 #include <sys/buf.h> 113 #include <sys/errno.h> 114 #include <sys/kmem.h> 115 #include <sys/syscallargs.h> 116 #include <sys/device.h> 117 #include <sys/filedesc.h> 118 #include <sys/kauth.h> 119 #include <sys/atomic.h> 120 #include <sys/kthread.h> 121 #include <sys/wapbl.h> 122 #include <sys/module.h> 123 124 #include <miscfs/genfs/genfs.h> 125 #include <miscfs/specfs/specdev.h> 126 #include <miscfs/syncfs/syncfs.h> 127 128 #include <uvm/uvm.h> 129 #include <uvm/uvm_readahead.h> 130 #include <uvm/uvm_ddb.h> 131 132 #include <sys/sysctl.h> 133 134 const enum vtype iftovt_tab[16] = { 135 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 136 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 137 }; 138 const int vttoif_tab[9] = { 139 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 140 S_IFSOCK, S_IFIFO, S_IFMT, 141 }; 142 143 /* 144 * Insq/Remq for the vnode usage lists. 145 */ 146 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 147 #define bufremvn(bp) { \ 148 LIST_REMOVE(bp, b_vnbufs); \ 149 (bp)->b_vnbufs.le_next = NOLIST; \ 150 } 151 152 int doforce = 1; /* 1 => permit forcible unmounting */ 153 int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 154 155 static vnodelst_t vnode_free_list = TAILQ_HEAD_INITIALIZER(vnode_free_list); 156 static vnodelst_t vnode_hold_list = TAILQ_HEAD_INITIALIZER(vnode_hold_list); 157 static vnodelst_t vrele_list = TAILQ_HEAD_INITIALIZER(vrele_list); 158 159 struct mntlist mountlist = /* mounted filesystem list */ 160 CIRCLEQ_HEAD_INITIALIZER(mountlist); 161 162 u_int numvnodes; 163 static specificdata_domain_t mount_specificdata_domain; 164 165 static int vrele_pending; 166 static int vrele_gen; 167 static kmutex_t vrele_lock; 168 static kcondvar_t vrele_cv; 169 static lwp_t *vrele_lwp; 170 171 static uint64_t mountgen = 0; 172 static kmutex_t mountgen_lock; 173 174 kmutex_t mountlist_lock; 175 kmutex_t mntid_lock; 176 kmutex_t mntvnode_lock; 177 kmutex_t vnode_free_list_lock; 178 kmutex_t vfs_list_lock; 179 180 static pool_cache_t vnode_cache; 181 182 /* 183 * These define the root filesystem and device. 184 */ 185 struct vnode *rootvnode; 186 struct device *root_device; /* root device */ 187 188 /* 189 * Local declarations. 190 */ 191 192 static void vrele_thread(void *); 193 static void insmntque(vnode_t *, struct mount *); 194 static int getdevvp(dev_t, vnode_t **, enum vtype); 195 static vnode_t *getcleanvnode(void); 196 void vpanic(vnode_t *, const char *); 197 static void vfs_shutdown1(struct lwp *); 198 199 #ifdef DEBUG 200 void printlockedvnodes(void); 201 #endif 202 203 #ifdef DIAGNOSTIC 204 void 205 vpanic(vnode_t *vp, const char *msg) 206 { 207 208 vprint(NULL, vp); 209 panic("%s\n", msg); 210 } 211 #else 212 #define vpanic(vp, msg) /* nothing */ 213 #endif 214 215 void 216 vn_init1(void) 217 { 218 219 vnode_cache = pool_cache_init(sizeof(struct vnode), 0, 0, 0, "vnodepl", 220 NULL, IPL_NONE, NULL, NULL, NULL); 221 KASSERT(vnode_cache != NULL); 222 223 /* Create deferred release thread. */ 224 mutex_init(&vrele_lock, MUTEX_DEFAULT, IPL_NONE); 225 cv_init(&vrele_cv, "vrele"); 226 if (kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vrele_thread, 227 NULL, &vrele_lwp, "vrele")) 228 panic("fork vrele"); 229 } 230 231 /* 232 * Initialize the vnode management data structures. 233 */ 234 void 235 vntblinit(void) 236 { 237 238 mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE); 239 mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE); 240 mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE); 241 mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE); 242 mutex_init(&vnode_free_list_lock, MUTEX_DEFAULT, IPL_NONE); 243 mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE); 244 245 mount_specificdata_domain = specificdata_domain_create(); 246 247 /* Initialize the filesystem syncer. */ 248 vn_initialize_syncerd(); 249 vn_init1(); 250 } 251 252 int 253 vfs_drainvnodes(long target, struct lwp *l) 254 { 255 256 while (numvnodes > target) { 257 vnode_t *vp; 258 259 mutex_enter(&vnode_free_list_lock); 260 vp = getcleanvnode(); 261 if (vp == NULL) 262 return EBUSY; /* give up */ 263 ungetnewvnode(vp); 264 } 265 266 return 0; 267 } 268 269 /* 270 * Lookup a mount point by filesystem identifier. 271 * 272 * XXX Needs to add a reference to the mount point. 273 */ 274 struct mount * 275 vfs_getvfs(fsid_t *fsid) 276 { 277 struct mount *mp; 278 279 mutex_enter(&mountlist_lock); 280 CIRCLEQ_FOREACH(mp, &mountlist, mnt_list) { 281 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] && 282 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) { 283 mutex_exit(&mountlist_lock); 284 return (mp); 285 } 286 } 287 mutex_exit(&mountlist_lock); 288 return ((struct mount *)0); 289 } 290 291 /* 292 * Drop a reference to a mount structure, freeing if the last reference. 293 */ 294 void 295 vfs_destroy(struct mount *mp) 296 { 297 298 if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) { 299 return; 300 } 301 302 /* 303 * Nothing else has visibility of the mount: we can now 304 * free the data structures. 305 */ 306 KASSERT(mp->mnt_refcnt == 0); 307 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 308 rw_destroy(&mp->mnt_unmounting); 309 mutex_destroy(&mp->mnt_updating); 310 mutex_destroy(&mp->mnt_renamelock); 311 if (mp->mnt_op != NULL) { 312 vfs_delref(mp->mnt_op); 313 } 314 kmem_free(mp, sizeof(*mp)); 315 } 316 317 /* 318 * grab a vnode from freelist and clean it. 319 */ 320 vnode_t * 321 getcleanvnode(void) 322 { 323 vnode_t *vp; 324 vnodelst_t *listhd; 325 326 KASSERT(mutex_owned(&vnode_free_list_lock)); 327 328 retry: 329 listhd = &vnode_free_list; 330 try_nextlist: 331 TAILQ_FOREACH(vp, listhd, v_freelist) { 332 /* 333 * It's safe to test v_usecount and v_iflag 334 * without holding the interlock here, since 335 * these vnodes should never appear on the 336 * lists. 337 */ 338 if (vp->v_usecount != 0) { 339 vpanic(vp, "free vnode isn't"); 340 } 341 if ((vp->v_iflag & VI_CLEAN) != 0) { 342 vpanic(vp, "clean vnode on freelist"); 343 } 344 if (vp->v_freelisthd != listhd) { 345 printf("vnode sez %p, listhd %p\n", vp->v_freelisthd, listhd); 346 vpanic(vp, "list head mismatch"); 347 } 348 if (!mutex_tryenter(&vp->v_interlock)) 349 continue; 350 /* 351 * Our lwp might hold the underlying vnode 352 * locked, so don't try to reclaim a VI_LAYER 353 * node if it's locked. 354 */ 355 if ((vp->v_iflag & VI_XLOCK) == 0 && 356 ((vp->v_iflag & VI_LAYER) == 0 || VOP_ISLOCKED(vp) == 0)) { 357 break; 358 } 359 mutex_exit(&vp->v_interlock); 360 } 361 362 if (vp == NULL) { 363 if (listhd == &vnode_free_list) { 364 listhd = &vnode_hold_list; 365 goto try_nextlist; 366 } 367 mutex_exit(&vnode_free_list_lock); 368 return NULL; 369 } 370 371 /* Remove it from the freelist. */ 372 TAILQ_REMOVE(listhd, vp, v_freelist); 373 vp->v_freelisthd = NULL; 374 mutex_exit(&vnode_free_list_lock); 375 376 if (vp->v_usecount != 0) { 377 /* 378 * was referenced again before we got the interlock 379 * Don't return to freelist - the holder of the last 380 * reference will destroy it. 381 */ 382 mutex_exit(&vp->v_interlock); 383 mutex_enter(&vnode_free_list_lock); 384 goto retry; 385 } 386 387 /* 388 * The vnode is still associated with a file system, so we must 389 * clean it out before reusing it. We need to add a reference 390 * before doing this. If the vnode gains another reference while 391 * being cleaned out then we lose - retry. 392 */ 393 atomic_add_int(&vp->v_usecount, 1 + VC_XLOCK); 394 vclean(vp, DOCLOSE); 395 KASSERT(vp->v_usecount >= 1 + VC_XLOCK); 396 atomic_add_int(&vp->v_usecount, -VC_XLOCK); 397 if (vp->v_usecount == 1) { 398 /* We're about to dirty it. */ 399 vp->v_iflag &= ~VI_CLEAN; 400 mutex_exit(&vp->v_interlock); 401 if (vp->v_type == VBLK || vp->v_type == VCHR) { 402 spec_node_destroy(vp); 403 } 404 vp->v_type = VNON; 405 } else { 406 /* 407 * Don't return to freelist - the holder of the last 408 * reference will destroy it. 409 */ 410 vrelel(vp, 0); /* releases vp->v_interlock */ 411 mutex_enter(&vnode_free_list_lock); 412 goto retry; 413 } 414 415 if (vp->v_data != NULL || vp->v_uobj.uo_npages != 0 || 416 !TAILQ_EMPTY(&vp->v_uobj.memq)) { 417 vpanic(vp, "cleaned vnode isn't"); 418 } 419 if (vp->v_numoutput != 0) { 420 vpanic(vp, "clean vnode has pending I/O's"); 421 } 422 if ((vp->v_iflag & VI_ONWORKLST) != 0) { 423 vpanic(vp, "clean vnode on syncer list"); 424 } 425 426 return vp; 427 } 428 429 /* 430 * Mark a mount point as busy, and gain a new reference to it. Used to 431 * prevent the file system from being unmounted during critical sections. 432 * 433 * => The caller must hold a pre-existing reference to the mount. 434 * => Will fail if the file system is being unmounted, or is unmounted. 435 */ 436 int 437 vfs_busy(struct mount *mp, struct mount **nextp) 438 { 439 440 KASSERT(mp->mnt_refcnt > 0); 441 442 if (__predict_false(!rw_tryenter(&mp->mnt_unmounting, RW_READER))) { 443 if (nextp != NULL) { 444 KASSERT(mutex_owned(&mountlist_lock)); 445 *nextp = CIRCLEQ_NEXT(mp, mnt_list); 446 } 447 return EBUSY; 448 } 449 if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) { 450 rw_exit(&mp->mnt_unmounting); 451 if (nextp != NULL) { 452 KASSERT(mutex_owned(&mountlist_lock)); 453 *nextp = CIRCLEQ_NEXT(mp, mnt_list); 454 } 455 return ENOENT; 456 } 457 if (nextp != NULL) { 458 mutex_exit(&mountlist_lock); 459 } 460 atomic_inc_uint(&mp->mnt_refcnt); 461 return 0; 462 } 463 464 /* 465 * Unbusy a busy filesystem. 466 * 467 * => If keepref is true, preserve reference added by vfs_busy(). 468 * => If nextp != NULL, acquire mountlist_lock. 469 */ 470 void 471 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp) 472 { 473 474 KASSERT(mp->mnt_refcnt > 0); 475 476 if (nextp != NULL) { 477 mutex_enter(&mountlist_lock); 478 } 479 rw_exit(&mp->mnt_unmounting); 480 if (!keepref) { 481 vfs_destroy(mp); 482 } 483 if (nextp != NULL) { 484 KASSERT(mutex_owned(&mountlist_lock)); 485 *nextp = CIRCLEQ_NEXT(mp, mnt_list); 486 } 487 } 488 489 struct mount * 490 vfs_mountalloc(struct vfsops *vfsops, struct vnode *vp) 491 { 492 int error; 493 struct mount *mp; 494 495 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP); 496 if (mp == NULL) 497 return NULL; 498 499 mp->mnt_op = vfsops; 500 mp->mnt_refcnt = 1; 501 TAILQ_INIT(&mp->mnt_vnodelist); 502 rw_init(&mp->mnt_unmounting); 503 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE); 504 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE); 505 error = vfs_busy(mp, NULL); 506 KASSERT(error == 0); 507 mp->mnt_vnodecovered = vp; 508 mount_initspecific(mp); 509 510 mutex_enter(&mountgen_lock); 511 mp->mnt_gen = mountgen++; 512 mutex_exit(&mountgen_lock); 513 514 return mp; 515 } 516 517 /* 518 * Lookup a filesystem type, and if found allocate and initialize 519 * a mount structure for it. 520 * 521 * Devname is usually updated by mount(8) after booting. 522 */ 523 int 524 vfs_rootmountalloc(const char *fstypename, const char *devname, 525 struct mount **mpp) 526 { 527 struct vfsops *vfsp = NULL; 528 struct mount *mp; 529 530 mutex_enter(&vfs_list_lock); 531 LIST_FOREACH(vfsp, &vfs_list, vfs_list) 532 if (!strncmp(vfsp->vfs_name, fstypename, 533 sizeof(mp->mnt_stat.f_fstypename))) 534 break; 535 if (vfsp == NULL) { 536 mutex_exit(&vfs_list_lock); 537 return (ENODEV); 538 } 539 vfsp->vfs_refcount++; 540 mutex_exit(&vfs_list_lock); 541 542 if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL) 543 return ENOMEM; 544 mp->mnt_flag = MNT_RDONLY; 545 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, 546 sizeof(mp->mnt_stat.f_fstypename)); 547 mp->mnt_stat.f_mntonname[0] = '/'; 548 mp->mnt_stat.f_mntonname[1] = '\0'; 549 mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] = 550 '\0'; 551 (void)copystr(devname, mp->mnt_stat.f_mntfromname, 552 sizeof(mp->mnt_stat.f_mntfromname) - 1, 0); 553 *mpp = mp; 554 return (0); 555 } 556 557 /* 558 * Routines having to do with the management of the vnode table. 559 */ 560 extern int (**dead_vnodeop_p)(void *); 561 562 /* 563 * Return the next vnode from the free list. 564 */ 565 int 566 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *), 567 vnode_t **vpp) 568 { 569 struct uvm_object *uobj; 570 static int toggle; 571 vnode_t *vp; 572 int error = 0, tryalloc; 573 574 try_again: 575 if (mp != NULL) { 576 /* 577 * Mark filesystem busy while we're creating a 578 * vnode. If unmount is in progress, this will 579 * fail. 580 */ 581 error = vfs_busy(mp, NULL); 582 if (error) 583 return error; 584 } 585 586 /* 587 * We must choose whether to allocate a new vnode or recycle an 588 * existing one. The criterion for allocating a new one is that 589 * the total number of vnodes is less than the number desired or 590 * there are no vnodes on either free list. Generally we only 591 * want to recycle vnodes that have no buffers associated with 592 * them, so we look first on the vnode_free_list. If it is empty, 593 * we next consider vnodes with referencing buffers on the 594 * vnode_hold_list. The toggle ensures that half the time we 595 * will use a buffer from the vnode_hold_list, and half the time 596 * we will allocate a new one unless the list has grown to twice 597 * the desired size. We are reticent to recycle vnodes from the 598 * vnode_hold_list because we will lose the identity of all its 599 * referencing buffers. 600 */ 601 602 vp = NULL; 603 604 mutex_enter(&vnode_free_list_lock); 605 606 toggle ^= 1; 607 if (numvnodes > 2 * desiredvnodes) 608 toggle = 0; 609 610 tryalloc = numvnodes < desiredvnodes || 611 (TAILQ_FIRST(&vnode_free_list) == NULL && 612 (TAILQ_FIRST(&vnode_hold_list) == NULL || toggle)); 613 614 if (tryalloc) { 615 numvnodes++; 616 mutex_exit(&vnode_free_list_lock); 617 if ((vp = vnalloc(NULL)) == NULL) { 618 mutex_enter(&vnode_free_list_lock); 619 numvnodes--; 620 } else 621 vp->v_usecount = 1; 622 } 623 624 if (vp == NULL) { 625 vp = getcleanvnode(); 626 if (vp == NULL) { 627 if (mp != NULL) { 628 vfs_unbusy(mp, false, NULL); 629 } 630 if (tryalloc) { 631 printf("WARNING: unable to allocate new " 632 "vnode, retrying...\n"); 633 kpause("newvn", false, hz, NULL); 634 goto try_again; 635 } 636 tablefull("vnode", "increase kern.maxvnodes or NVNODE"); 637 *vpp = 0; 638 return (ENFILE); 639 } 640 vp->v_iflag = 0; 641 vp->v_vflag = 0; 642 vp->v_uflag = 0; 643 vp->v_socket = NULL; 644 } 645 646 KASSERT(vp->v_usecount == 1); 647 KASSERT(vp->v_freelisthd == NULL); 648 KASSERT(LIST_EMPTY(&vp->v_nclist)); 649 KASSERT(LIST_EMPTY(&vp->v_dnclist)); 650 651 vp->v_type = VNON; 652 vp->v_tag = tag; 653 vp->v_op = vops; 654 insmntque(vp, mp); 655 *vpp = vp; 656 vp->v_data = 0; 657 658 /* 659 * initialize uvm_object within vnode. 660 */ 661 662 uobj = &vp->v_uobj; 663 KASSERT(uobj->pgops == &uvm_vnodeops); 664 KASSERT(uobj->uo_npages == 0); 665 KASSERT(TAILQ_FIRST(&uobj->memq) == NULL); 666 vp->v_size = vp->v_writesize = VSIZENOTSET; 667 668 if (mp != NULL) { 669 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0) 670 vp->v_vflag |= VV_MPSAFE; 671 vfs_unbusy(mp, true, NULL); 672 } 673 674 return (0); 675 } 676 677 /* 678 * This is really just the reverse of getnewvnode(). Needed for 679 * VFS_VGET functions who may need to push back a vnode in case 680 * of a locking race. 681 */ 682 void 683 ungetnewvnode(vnode_t *vp) 684 { 685 686 KASSERT(vp->v_usecount == 1); 687 KASSERT(vp->v_data == NULL); 688 KASSERT(vp->v_freelisthd == NULL); 689 690 mutex_enter(&vp->v_interlock); 691 vp->v_iflag |= VI_CLEAN; 692 vrelel(vp, 0); 693 } 694 695 /* 696 * Allocate a new, uninitialized vnode. If 'mp' is non-NULL, this is a 697 * marker vnode and we are prepared to wait for the allocation. 698 */ 699 vnode_t * 700 vnalloc(struct mount *mp) 701 { 702 vnode_t *vp; 703 704 vp = pool_cache_get(vnode_cache, (mp != NULL ? PR_WAITOK : PR_NOWAIT)); 705 if (vp == NULL) { 706 return NULL; 707 } 708 709 memset(vp, 0, sizeof(*vp)); 710 UVM_OBJ_INIT(&vp->v_uobj, &uvm_vnodeops, 0); 711 cv_init(&vp->v_cv, "vnode"); 712 /* 713 * done by memset() above. 714 * LIST_INIT(&vp->v_nclist); 715 * LIST_INIT(&vp->v_dnclist); 716 */ 717 718 if (mp != NULL) { 719 vp->v_mount = mp; 720 vp->v_type = VBAD; 721 vp->v_iflag = VI_MARKER; 722 } else { 723 rw_init(&vp->v_lock.vl_lock); 724 } 725 726 return vp; 727 } 728 729 /* 730 * Free an unused, unreferenced vnode. 731 */ 732 void 733 vnfree(vnode_t *vp) 734 { 735 736 KASSERT(vp->v_usecount == 0); 737 738 if ((vp->v_iflag & VI_MARKER) == 0) { 739 rw_destroy(&vp->v_lock.vl_lock); 740 mutex_enter(&vnode_free_list_lock); 741 numvnodes--; 742 mutex_exit(&vnode_free_list_lock); 743 } 744 745 UVM_OBJ_DESTROY(&vp->v_uobj); 746 cv_destroy(&vp->v_cv); 747 pool_cache_put(vnode_cache, vp); 748 } 749 750 /* 751 * Remove a vnode from its freelist. 752 */ 753 static inline void 754 vremfree(vnode_t *vp) 755 { 756 757 KASSERT(mutex_owned(&vp->v_interlock)); 758 KASSERT(vp->v_usecount == 0); 759 760 /* 761 * Note that the reference count must not change until 762 * the vnode is removed. 763 */ 764 mutex_enter(&vnode_free_list_lock); 765 if (vp->v_holdcnt > 0) { 766 KASSERT(vp->v_freelisthd == &vnode_hold_list); 767 } else { 768 KASSERT(vp->v_freelisthd == &vnode_free_list); 769 } 770 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist); 771 vp->v_freelisthd = NULL; 772 mutex_exit(&vnode_free_list_lock); 773 } 774 775 /* 776 * Move a vnode from one mount queue to another. 777 */ 778 static void 779 insmntque(vnode_t *vp, struct mount *mp) 780 { 781 struct mount *omp; 782 783 #ifdef DIAGNOSTIC 784 if ((mp != NULL) && 785 (mp->mnt_iflag & IMNT_UNMOUNT) && 786 vp->v_tag != VT_VFS) { 787 panic("insmntque into dying filesystem"); 788 } 789 #endif 790 791 mutex_enter(&mntvnode_lock); 792 /* 793 * Delete from old mount point vnode list, if on one. 794 */ 795 if ((omp = vp->v_mount) != NULL) 796 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes); 797 /* 798 * Insert into list of vnodes for the new mount point, if 799 * available. The caller must take a reference on the mount 800 * structure and donate to the vnode. 801 */ 802 if ((vp->v_mount = mp) != NULL) 803 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes); 804 mutex_exit(&mntvnode_lock); 805 806 if (omp != NULL) { 807 /* Release reference to old mount. */ 808 vfs_destroy(omp); 809 } 810 } 811 812 /* 813 * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or 814 * recycled. 815 */ 816 void 817 vwait(vnode_t *vp, int flags) 818 { 819 820 KASSERT(mutex_owned(&vp->v_interlock)); 821 KASSERT(vp->v_usecount != 0); 822 823 while ((vp->v_iflag & flags) != 0) 824 cv_wait(&vp->v_cv, &vp->v_interlock); 825 } 826 827 /* 828 * Insert a marker vnode into a mount's vnode list, after the 829 * specified vnode. mntvnode_lock must be held. 830 */ 831 void 832 vmark(vnode_t *mvp, vnode_t *vp) 833 { 834 struct mount *mp; 835 836 mp = mvp->v_mount; 837 838 KASSERT(mutex_owned(&mntvnode_lock)); 839 KASSERT((mvp->v_iflag & VI_MARKER) != 0); 840 KASSERT(vp->v_mount == mp); 841 842 TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vp, mvp, v_mntvnodes); 843 } 844 845 /* 846 * Remove a marker vnode from a mount's vnode list, and return 847 * a pointer to the next vnode in the list. mntvnode_lock must 848 * be held. 849 */ 850 vnode_t * 851 vunmark(vnode_t *mvp) 852 { 853 vnode_t *vp; 854 struct mount *mp; 855 856 mp = mvp->v_mount; 857 858 KASSERT(mutex_owned(&mntvnode_lock)); 859 KASSERT((mvp->v_iflag & VI_MARKER) != 0); 860 861 vp = TAILQ_NEXT(mvp, v_mntvnodes); 862 TAILQ_REMOVE(&mp->mnt_vnodelist, mvp, v_mntvnodes); 863 864 KASSERT(vp == NULL || vp->v_mount == mp); 865 866 return vp; 867 } 868 869 /* 870 * Update outstanding I/O count and do wakeup if requested. 871 */ 872 void 873 vwakeup(struct buf *bp) 874 { 875 struct vnode *vp; 876 877 if ((vp = bp->b_vp) == NULL) 878 return; 879 880 KASSERT(bp->b_objlock == &vp->v_interlock); 881 KASSERT(mutex_owned(bp->b_objlock)); 882 883 if (--vp->v_numoutput < 0) 884 panic("vwakeup: neg numoutput, vp %p", vp); 885 if (vp->v_numoutput == 0) 886 cv_broadcast(&vp->v_cv); 887 } 888 889 /* 890 * Flush out and invalidate all buffers associated with a vnode. 891 * Called with the underlying vnode locked, which should prevent new dirty 892 * buffers from being queued. 893 */ 894 int 895 vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l, 896 bool catch, int slptimeo) 897 { 898 struct buf *bp, *nbp; 899 int error; 900 int flushflags = PGO_ALLPAGES | PGO_FREE | PGO_SYNCIO | 901 (flags & V_SAVE ? PGO_CLEANIT | PGO_RECLAIM : 0); 902 903 /* XXXUBC this doesn't look at flags or slp* */ 904 mutex_enter(&vp->v_interlock); 905 error = VOP_PUTPAGES(vp, 0, 0, flushflags); 906 if (error) { 907 return error; 908 } 909 910 if (flags & V_SAVE) { 911 error = VOP_FSYNC(vp, cred, FSYNC_WAIT|FSYNC_RECLAIM, 0, 0); 912 if (error) 913 return (error); 914 KASSERT(LIST_EMPTY(&vp->v_dirtyblkhd)); 915 } 916 917 mutex_enter(&bufcache_lock); 918 restart: 919 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 920 nbp = LIST_NEXT(bp, b_vnbufs); 921 error = bbusy(bp, catch, slptimeo, NULL); 922 if (error != 0) { 923 if (error == EPASSTHROUGH) 924 goto restart; 925 mutex_exit(&bufcache_lock); 926 return (error); 927 } 928 brelsel(bp, BC_INVAL | BC_VFLUSH); 929 } 930 931 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 932 nbp = LIST_NEXT(bp, b_vnbufs); 933 error = bbusy(bp, catch, slptimeo, NULL); 934 if (error != 0) { 935 if (error == EPASSTHROUGH) 936 goto restart; 937 mutex_exit(&bufcache_lock); 938 return (error); 939 } 940 /* 941 * XXX Since there are no node locks for NFS, I believe 942 * there is a slight chance that a delayed write will 943 * occur while sleeping just above, so check for it. 944 */ 945 if ((bp->b_oflags & BO_DELWRI) && (flags & V_SAVE)) { 946 #ifdef DEBUG 947 printf("buffer still DELWRI\n"); 948 #endif 949 bp->b_cflags |= BC_BUSY | BC_VFLUSH; 950 mutex_exit(&bufcache_lock); 951 VOP_BWRITE(bp); 952 mutex_enter(&bufcache_lock); 953 goto restart; 954 } 955 brelsel(bp, BC_INVAL | BC_VFLUSH); 956 } 957 958 #ifdef DIAGNOSTIC 959 if (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd)) 960 panic("vinvalbuf: flush failed, vp %p", vp); 961 #endif 962 963 mutex_exit(&bufcache_lock); 964 965 return (0); 966 } 967 968 /* 969 * Destroy any in core blocks past the truncation length. 970 * Called with the underlying vnode locked, which should prevent new dirty 971 * buffers from being queued. 972 */ 973 int 974 vtruncbuf(struct vnode *vp, daddr_t lbn, bool catch, int slptimeo) 975 { 976 struct buf *bp, *nbp; 977 int error; 978 voff_t off; 979 980 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift); 981 mutex_enter(&vp->v_interlock); 982 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO); 983 if (error) { 984 return error; 985 } 986 987 mutex_enter(&bufcache_lock); 988 restart: 989 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 990 nbp = LIST_NEXT(bp, b_vnbufs); 991 if (bp->b_lblkno < lbn) 992 continue; 993 error = bbusy(bp, catch, slptimeo, NULL); 994 if (error != 0) { 995 if (error == EPASSTHROUGH) 996 goto restart; 997 mutex_exit(&bufcache_lock); 998 return (error); 999 } 1000 brelsel(bp, BC_INVAL | BC_VFLUSH); 1001 } 1002 1003 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 1004 nbp = LIST_NEXT(bp, b_vnbufs); 1005 if (bp->b_lblkno < lbn) 1006 continue; 1007 error = bbusy(bp, catch, slptimeo, NULL); 1008 if (error != 0) { 1009 if (error == EPASSTHROUGH) 1010 goto restart; 1011 mutex_exit(&bufcache_lock); 1012 return (error); 1013 } 1014 brelsel(bp, BC_INVAL | BC_VFLUSH); 1015 } 1016 mutex_exit(&bufcache_lock); 1017 1018 return (0); 1019 } 1020 1021 /* 1022 * Flush all dirty buffers from a vnode. 1023 * Called with the underlying vnode locked, which should prevent new dirty 1024 * buffers from being queued. 1025 */ 1026 void 1027 vflushbuf(struct vnode *vp, int sync) 1028 { 1029 struct buf *bp, *nbp; 1030 int flags = PGO_CLEANIT | PGO_ALLPAGES | (sync ? PGO_SYNCIO : 0); 1031 bool dirty; 1032 1033 mutex_enter(&vp->v_interlock); 1034 (void) VOP_PUTPAGES(vp, 0, 0, flags); 1035 1036 loop: 1037 mutex_enter(&bufcache_lock); 1038 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1039 nbp = LIST_NEXT(bp, b_vnbufs); 1040 if ((bp->b_cflags & BC_BUSY)) 1041 continue; 1042 if ((bp->b_oflags & BO_DELWRI) == 0) 1043 panic("vflushbuf: not dirty, bp %p", bp); 1044 bp->b_cflags |= BC_BUSY | BC_VFLUSH; 1045 mutex_exit(&bufcache_lock); 1046 /* 1047 * Wait for I/O associated with indirect blocks to complete, 1048 * since there is no way to quickly wait for them below. 1049 */ 1050 if (bp->b_vp == vp || sync == 0) 1051 (void) bawrite(bp); 1052 else 1053 (void) bwrite(bp); 1054 goto loop; 1055 } 1056 mutex_exit(&bufcache_lock); 1057 1058 if (sync == 0) 1059 return; 1060 1061 mutex_enter(&vp->v_interlock); 1062 while (vp->v_numoutput != 0) 1063 cv_wait(&vp->v_cv, &vp->v_interlock); 1064 dirty = !LIST_EMPTY(&vp->v_dirtyblkhd); 1065 mutex_exit(&vp->v_interlock); 1066 1067 if (dirty) { 1068 vprint("vflushbuf: dirty", vp); 1069 goto loop; 1070 } 1071 } 1072 1073 /* 1074 * Create a vnode for a block device. 1075 * Used for root filesystem and swap areas. 1076 * Also used for memory file system special devices. 1077 */ 1078 int 1079 bdevvp(dev_t dev, vnode_t **vpp) 1080 { 1081 1082 return (getdevvp(dev, vpp, VBLK)); 1083 } 1084 1085 /* 1086 * Create a vnode for a character device. 1087 * Used for kernfs and some console handling. 1088 */ 1089 int 1090 cdevvp(dev_t dev, vnode_t **vpp) 1091 { 1092 1093 return (getdevvp(dev, vpp, VCHR)); 1094 } 1095 1096 /* 1097 * Associate a buffer with a vnode. There must already be a hold on 1098 * the vnode. 1099 */ 1100 void 1101 bgetvp(struct vnode *vp, struct buf *bp) 1102 { 1103 1104 KASSERT(bp->b_vp == NULL); 1105 KASSERT(bp->b_objlock == &buffer_lock); 1106 KASSERT(mutex_owned(&vp->v_interlock)); 1107 KASSERT(mutex_owned(&bufcache_lock)); 1108 KASSERT((bp->b_cflags & BC_BUSY) != 0); 1109 KASSERT(!cv_has_waiters(&bp->b_done)); 1110 1111 vholdl(vp); 1112 bp->b_vp = vp; 1113 if (vp->v_type == VBLK || vp->v_type == VCHR) 1114 bp->b_dev = vp->v_rdev; 1115 else 1116 bp->b_dev = NODEV; 1117 1118 /* 1119 * Insert onto list for new vnode. 1120 */ 1121 bufinsvn(bp, &vp->v_cleanblkhd); 1122 bp->b_objlock = &vp->v_interlock; 1123 } 1124 1125 /* 1126 * Disassociate a buffer from a vnode. 1127 */ 1128 void 1129 brelvp(struct buf *bp) 1130 { 1131 struct vnode *vp = bp->b_vp; 1132 1133 KASSERT(vp != NULL); 1134 KASSERT(bp->b_objlock == &vp->v_interlock); 1135 KASSERT(mutex_owned(&vp->v_interlock)); 1136 KASSERT(mutex_owned(&bufcache_lock)); 1137 KASSERT((bp->b_cflags & BC_BUSY) != 0); 1138 KASSERT(!cv_has_waiters(&bp->b_done)); 1139 1140 /* 1141 * Delete from old vnode list, if on one. 1142 */ 1143 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 1144 bufremvn(bp); 1145 1146 if (TAILQ_EMPTY(&vp->v_uobj.memq) && (vp->v_iflag & VI_ONWORKLST) && 1147 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) { 1148 vp->v_iflag &= ~VI_WRMAPDIRTY; 1149 vn_syncer_remove_from_worklist(vp); 1150 } 1151 1152 bp->b_objlock = &buffer_lock; 1153 bp->b_vp = NULL; 1154 holdrelel(vp); 1155 } 1156 1157 /* 1158 * Reassign a buffer from one vnode list to another. 1159 * The list reassignment must be within the same vnode. 1160 * Used to assign file specific control information 1161 * (indirect blocks) to the list to which they belong. 1162 */ 1163 void 1164 reassignbuf(struct buf *bp, struct vnode *vp) 1165 { 1166 struct buflists *listheadp; 1167 int delayx; 1168 1169 KASSERT(mutex_owned(&bufcache_lock)); 1170 KASSERT(bp->b_objlock == &vp->v_interlock); 1171 KASSERT(mutex_owned(&vp->v_interlock)); 1172 KASSERT((bp->b_cflags & BC_BUSY) != 0); 1173 1174 /* 1175 * Delete from old vnode list, if on one. 1176 */ 1177 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 1178 bufremvn(bp); 1179 1180 /* 1181 * If dirty, put on list of dirty buffers; 1182 * otherwise insert onto list of clean buffers. 1183 */ 1184 if ((bp->b_oflags & BO_DELWRI) == 0) { 1185 listheadp = &vp->v_cleanblkhd; 1186 if (TAILQ_EMPTY(&vp->v_uobj.memq) && 1187 (vp->v_iflag & VI_ONWORKLST) && 1188 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) { 1189 vp->v_iflag &= ~VI_WRMAPDIRTY; 1190 vn_syncer_remove_from_worklist(vp); 1191 } 1192 } else { 1193 listheadp = &vp->v_dirtyblkhd; 1194 if ((vp->v_iflag & VI_ONWORKLST) == 0) { 1195 switch (vp->v_type) { 1196 case VDIR: 1197 delayx = dirdelay; 1198 break; 1199 case VBLK: 1200 if (vp->v_specmountpoint != NULL) { 1201 delayx = metadelay; 1202 break; 1203 } 1204 /* fall through */ 1205 default: 1206 delayx = filedelay; 1207 break; 1208 } 1209 if (!vp->v_mount || 1210 (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 1211 vn_syncer_add_to_worklist(vp, delayx); 1212 } 1213 } 1214 bufinsvn(bp, listheadp); 1215 } 1216 1217 /* 1218 * Create a vnode for a device. 1219 * Used by bdevvp (block device) for root file system etc., 1220 * and by cdevvp (character device) for console and kernfs. 1221 */ 1222 static int 1223 getdevvp(dev_t dev, vnode_t **vpp, enum vtype type) 1224 { 1225 vnode_t *vp; 1226 vnode_t *nvp; 1227 int error; 1228 1229 if (dev == NODEV) { 1230 *vpp = NULL; 1231 return (0); 1232 } 1233 error = getnewvnode(VT_NON, NULL, spec_vnodeop_p, &nvp); 1234 if (error) { 1235 *vpp = NULL; 1236 return (error); 1237 } 1238 vp = nvp; 1239 vp->v_type = type; 1240 vp->v_vflag |= VV_MPSAFE; 1241 uvm_vnp_setsize(vp, 0); 1242 spec_node_init(vp, dev); 1243 *vpp = vp; 1244 return (0); 1245 } 1246 1247 /* 1248 * Try to gain a reference to a vnode, without acquiring its interlock. 1249 * The caller must hold a lock that will prevent the vnode from being 1250 * recycled or freed. 1251 */ 1252 bool 1253 vtryget(vnode_t *vp) 1254 { 1255 u_int use, next; 1256 1257 /* 1258 * If the vnode is being freed, don't make life any harder 1259 * for vclean() by adding another reference without waiting. 1260 * This is not strictly necessary, but we'll do it anyway. 1261 */ 1262 if (__predict_false((vp->v_iflag & (VI_XLOCK | VI_FREEING)) != 0)) { 1263 return false; 1264 } 1265 for (use = vp->v_usecount;; use = next) { 1266 if (use == 0 || __predict_false((use & VC_XLOCK) != 0)) { 1267 /* Need interlock held if first reference. */ 1268 return false; 1269 } 1270 next = atomic_cas_uint(&vp->v_usecount, use, use + 1); 1271 if (__predict_true(next == use)) { 1272 return true; 1273 } 1274 } 1275 } 1276 1277 /* 1278 * Grab a particular vnode from the free list, increment its 1279 * reference count and lock it. If the vnode lock bit is set the 1280 * vnode is being eliminated in vgone. In that case, we can not 1281 * grab the vnode, so the process is awakened when the transition is 1282 * completed, and an error returned to indicate that the vnode is no 1283 * longer usable (possibly having been changed to a new file system type). 1284 */ 1285 int 1286 vget(vnode_t *vp, int flags) 1287 { 1288 int error = 0; 1289 1290 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1291 1292 if ((flags & LK_INTERLOCK) == 0) 1293 mutex_enter(&vp->v_interlock); 1294 1295 /* 1296 * Before adding a reference, we must remove the vnode 1297 * from its freelist. 1298 */ 1299 if (vp->v_usecount == 0) { 1300 vremfree(vp); 1301 vp->v_usecount = 1; 1302 } else { 1303 atomic_inc_uint(&vp->v_usecount); 1304 } 1305 1306 /* 1307 * If the vnode is in the process of being cleaned out for 1308 * another use, we wait for the cleaning to finish and then 1309 * return failure. Cleaning is determined by checking if 1310 * the VI_XLOCK or VI_FREEING flags are set. 1311 */ 1312 if ((vp->v_iflag & (VI_XLOCK | VI_FREEING)) != 0) { 1313 if ((flags & LK_NOWAIT) != 0) { 1314 vrelel(vp, 0); 1315 return EBUSY; 1316 } 1317 vwait(vp, VI_XLOCK | VI_FREEING); 1318 vrelel(vp, 0); 1319 return ENOENT; 1320 } 1321 1322 if ((vp->v_iflag & VI_INACTNOW) != 0) { 1323 /* 1324 * if it's being desactived, wait for it to complete. 1325 * Make sure to not return a clean vnode. 1326 */ 1327 if ((flags & LK_NOWAIT) != 0) { 1328 vrelel(vp, 0); 1329 return EBUSY; 1330 } 1331 vwait(vp, VI_INACTNOW); 1332 if ((vp->v_iflag & VI_CLEAN) != 0) { 1333 vrelel(vp, 0); 1334 return ENOENT; 1335 } 1336 } 1337 1338 /* 1339 * Ok, we got it in good shape. Just locking left. 1340 */ 1341 KASSERT((vp->v_iflag & VI_CLEAN) == 0); 1342 if (flags & LK_TYPE_MASK) { 1343 error = vn_lock(vp, flags | LK_INTERLOCK); 1344 if (error != 0) { 1345 vrele(vp); 1346 } 1347 } else { 1348 mutex_exit(&vp->v_interlock); 1349 } 1350 return error; 1351 } 1352 1353 /* 1354 * vput(), just unlock and vrele() 1355 */ 1356 void 1357 vput(vnode_t *vp) 1358 { 1359 1360 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1361 1362 VOP_UNLOCK(vp, 0); 1363 vrele(vp); 1364 } 1365 1366 /* 1367 * Try to drop reference on a vnode. Abort if we are releasing the 1368 * last reference. Note: this _must_ succeed if not the last reference. 1369 */ 1370 static inline bool 1371 vtryrele(vnode_t *vp) 1372 { 1373 u_int use, next; 1374 1375 for (use = vp->v_usecount;; use = next) { 1376 if (use == 1) { 1377 return false; 1378 } 1379 KASSERT((use & VC_MASK) > 1); 1380 next = atomic_cas_uint(&vp->v_usecount, use, use - 1); 1381 if (__predict_true(next == use)) { 1382 return true; 1383 } 1384 } 1385 } 1386 1387 /* 1388 * Vnode release. If reference count drops to zero, call inactive 1389 * routine and either return to freelist or free to the pool. 1390 */ 1391 void 1392 vrelel(vnode_t *vp, int flags) 1393 { 1394 bool recycle, defer; 1395 int error; 1396 1397 KASSERT(mutex_owned(&vp->v_interlock)); 1398 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1399 KASSERT(vp->v_freelisthd == NULL); 1400 1401 if (__predict_false(vp->v_op == dead_vnodeop_p && 1402 (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) == 0)) { 1403 vpanic(vp, "dead but not clean"); 1404 } 1405 1406 /* 1407 * If not the last reference, just drop the reference count 1408 * and unlock. 1409 */ 1410 if (vtryrele(vp)) { 1411 vp->v_iflag |= VI_INACTREDO; 1412 mutex_exit(&vp->v_interlock); 1413 return; 1414 } 1415 if (vp->v_usecount <= 0 || vp->v_writecount != 0) { 1416 vpanic(vp, "vrelel: bad ref count"); 1417 } 1418 1419 KASSERT((vp->v_iflag & VI_XLOCK) == 0); 1420 1421 /* 1422 * If not clean, deactivate the vnode, but preserve 1423 * our reference across the call to VOP_INACTIVE(). 1424 */ 1425 retry: 1426 if ((vp->v_iflag & VI_CLEAN) == 0) { 1427 recycle = false; 1428 vp->v_iflag |= VI_INACTNOW; 1429 1430 /* 1431 * XXX This ugly block can be largely eliminated if 1432 * locking is pushed down into the file systems. 1433 * 1434 * Defer vnode release to vrele_thread if caller 1435 * requests it explicitly. 1436 */ 1437 if ((curlwp == uvm.pagedaemon_lwp) || 1438 (flags & VRELEL_ASYNC_RELE) != 0) { 1439 /* The pagedaemon can't wait around; defer. */ 1440 defer = true; 1441 } else if (curlwp == vrele_lwp) { 1442 /* 1443 * We have to try harder. But we can't sleep 1444 * with VI_INACTNOW as vget() may be waiting on it. 1445 */ 1446 vp->v_iflag &= ~(VI_INACTREDO|VI_INACTNOW); 1447 cv_broadcast(&vp->v_cv); 1448 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | 1449 LK_RETRY); 1450 if (error != 0) { 1451 /* XXX */ 1452 vpanic(vp, "vrele: unable to lock %p"); 1453 } 1454 mutex_enter(&vp->v_interlock); 1455 /* 1456 * if we did get another reference while 1457 * sleeping, don't try to inactivate it yet. 1458 */ 1459 if (__predict_false(vtryrele(vp))) { 1460 VOP_UNLOCK(vp, 0); 1461 mutex_exit(&vp->v_interlock); 1462 return; 1463 } 1464 vp->v_iflag |= VI_INACTNOW; 1465 mutex_exit(&vp->v_interlock); 1466 defer = false; 1467 } else if ((vp->v_iflag & VI_LAYER) != 0) { 1468 /* 1469 * Acquiring the stack's lock in vclean() even 1470 * for an honest vput/vrele is dangerous because 1471 * our caller may hold other vnode locks; defer. 1472 */ 1473 defer = true; 1474 } else { 1475 /* If we can't acquire the lock, then defer. */ 1476 vp->v_iflag &= ~VI_INACTREDO; 1477 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | 1478 LK_NOWAIT); 1479 if (error != 0) { 1480 defer = true; 1481 mutex_enter(&vp->v_interlock); 1482 } else { 1483 defer = false; 1484 } 1485 } 1486 1487 if (defer) { 1488 /* 1489 * Defer reclaim to the kthread; it's not safe to 1490 * clean it here. We donate it our last reference. 1491 */ 1492 KASSERT(mutex_owned(&vp->v_interlock)); 1493 KASSERT((vp->v_iflag & VI_INACTPEND) == 0); 1494 vp->v_iflag &= ~VI_INACTNOW; 1495 vp->v_iflag |= VI_INACTPEND; 1496 mutex_enter(&vrele_lock); 1497 TAILQ_INSERT_TAIL(&vrele_list, vp, v_freelist); 1498 if (++vrele_pending > (desiredvnodes >> 8)) 1499 cv_signal(&vrele_cv); 1500 mutex_exit(&vrele_lock); 1501 cv_broadcast(&vp->v_cv); 1502 mutex_exit(&vp->v_interlock); 1503 return; 1504 } 1505 1506 #ifdef DIAGNOSTIC 1507 if ((vp->v_type == VBLK || vp->v_type == VCHR) && 1508 vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) { 1509 vprint("vrelel: missing VOP_CLOSE()", vp); 1510 } 1511 #endif 1512 1513 /* 1514 * The vnode can gain another reference while being 1515 * deactivated. If VOP_INACTIVE() indicates that 1516 * the described file has been deleted, then recycle 1517 * the vnode irrespective of additional references. 1518 * Another thread may be waiting to re-use the on-disk 1519 * inode. 1520 * 1521 * Note that VOP_INACTIVE() will drop the vnode lock. 1522 */ 1523 VOP_INACTIVE(vp, &recycle); 1524 mutex_enter(&vp->v_interlock); 1525 vp->v_iflag &= ~VI_INACTNOW; 1526 cv_broadcast(&vp->v_cv); 1527 if (!recycle) { 1528 if (vtryrele(vp)) { 1529 mutex_exit(&vp->v_interlock); 1530 return; 1531 } 1532 1533 /* 1534 * If we grew another reference while 1535 * VOP_INACTIVE() was underway, retry. 1536 */ 1537 if ((vp->v_iflag & VI_INACTREDO) != 0) { 1538 goto retry; 1539 } 1540 } 1541 1542 /* Take care of space accounting. */ 1543 if (vp->v_iflag & VI_EXECMAP) { 1544 atomic_add_int(&uvmexp.execpages, 1545 -vp->v_uobj.uo_npages); 1546 atomic_add_int(&uvmexp.filepages, 1547 vp->v_uobj.uo_npages); 1548 } 1549 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP); 1550 vp->v_vflag &= ~VV_MAPPED; 1551 1552 /* 1553 * Recycle the vnode if the file is now unused (unlinked), 1554 * otherwise just free it. 1555 */ 1556 if (recycle) { 1557 vclean(vp, DOCLOSE); 1558 } 1559 KASSERT(vp->v_usecount > 0); 1560 } 1561 1562 if (atomic_dec_uint_nv(&vp->v_usecount) != 0) { 1563 /* Gained another reference while being reclaimed. */ 1564 mutex_exit(&vp->v_interlock); 1565 return; 1566 } 1567 1568 if ((vp->v_iflag & VI_CLEAN) != 0) { 1569 /* 1570 * It's clean so destroy it. It isn't referenced 1571 * anywhere since it has been reclaimed. 1572 */ 1573 KASSERT(vp->v_holdcnt == 0); 1574 KASSERT(vp->v_writecount == 0); 1575 mutex_exit(&vp->v_interlock); 1576 insmntque(vp, NULL); 1577 if (vp->v_type == VBLK || vp->v_type == VCHR) { 1578 spec_node_destroy(vp); 1579 } 1580 vnfree(vp); 1581 } else { 1582 /* 1583 * Otherwise, put it back onto the freelist. It 1584 * can't be destroyed while still associated with 1585 * a file system. 1586 */ 1587 mutex_enter(&vnode_free_list_lock); 1588 if (vp->v_holdcnt > 0) { 1589 vp->v_freelisthd = &vnode_hold_list; 1590 } else { 1591 vp->v_freelisthd = &vnode_free_list; 1592 } 1593 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist); 1594 mutex_exit(&vnode_free_list_lock); 1595 mutex_exit(&vp->v_interlock); 1596 } 1597 } 1598 1599 void 1600 vrele(vnode_t *vp) 1601 { 1602 1603 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1604 1605 if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) { 1606 return; 1607 } 1608 mutex_enter(&vp->v_interlock); 1609 vrelel(vp, 0); 1610 } 1611 1612 /* 1613 * Asynchronous vnode release, vnode is released in different context. 1614 */ 1615 void 1616 vrele_async(vnode_t *vp) 1617 { 1618 1619 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1620 1621 if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) { 1622 return; 1623 } 1624 1625 mutex_enter(&vp->v_interlock); 1626 vrelel(vp, VRELEL_ASYNC_RELE); 1627 } 1628 1629 static void 1630 vrele_thread(void *cookie) 1631 { 1632 vnode_t *vp; 1633 1634 for (;;) { 1635 mutex_enter(&vrele_lock); 1636 while (TAILQ_EMPTY(&vrele_list)) { 1637 vrele_gen++; 1638 cv_broadcast(&vrele_cv); 1639 cv_timedwait(&vrele_cv, &vrele_lock, hz); 1640 } 1641 vp = TAILQ_FIRST(&vrele_list); 1642 TAILQ_REMOVE(&vrele_list, vp, v_freelist); 1643 vrele_pending--; 1644 mutex_exit(&vrele_lock); 1645 1646 /* 1647 * If not the last reference, then ignore the vnode 1648 * and look for more work. 1649 */ 1650 mutex_enter(&vp->v_interlock); 1651 KASSERT((vp->v_iflag & VI_INACTPEND) != 0); 1652 vp->v_iflag &= ~VI_INACTPEND; 1653 vrelel(vp, 0); 1654 } 1655 } 1656 1657 /* 1658 * Page or buffer structure gets a reference. 1659 * Called with v_interlock held. 1660 */ 1661 void 1662 vholdl(vnode_t *vp) 1663 { 1664 1665 KASSERT(mutex_owned(&vp->v_interlock)); 1666 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1667 1668 if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) { 1669 mutex_enter(&vnode_free_list_lock); 1670 KASSERT(vp->v_freelisthd == &vnode_free_list); 1671 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist); 1672 vp->v_freelisthd = &vnode_hold_list; 1673 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist); 1674 mutex_exit(&vnode_free_list_lock); 1675 } 1676 } 1677 1678 /* 1679 * Page or buffer structure frees a reference. 1680 * Called with v_interlock held. 1681 */ 1682 void 1683 holdrelel(vnode_t *vp) 1684 { 1685 1686 KASSERT(mutex_owned(&vp->v_interlock)); 1687 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1688 1689 if (vp->v_holdcnt <= 0) { 1690 vpanic(vp, "holdrelel: holdcnt vp %p"); 1691 } 1692 1693 vp->v_holdcnt--; 1694 if (vp->v_holdcnt == 0 && vp->v_usecount == 0) { 1695 mutex_enter(&vnode_free_list_lock); 1696 KASSERT(vp->v_freelisthd == &vnode_hold_list); 1697 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist); 1698 vp->v_freelisthd = &vnode_free_list; 1699 TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist); 1700 mutex_exit(&vnode_free_list_lock); 1701 } 1702 } 1703 1704 /* 1705 * Vnode reference, where a reference is already held by some other 1706 * object (for example, a file structure). 1707 */ 1708 void 1709 vref(vnode_t *vp) 1710 { 1711 1712 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1713 KASSERT(vp->v_usecount != 0); 1714 1715 atomic_inc_uint(&vp->v_usecount); 1716 } 1717 1718 /* 1719 * Remove any vnodes in the vnode table belonging to mount point mp. 1720 * 1721 * If FORCECLOSE is not specified, there should not be any active ones, 1722 * return error if any are found (nb: this is a user error, not a 1723 * system error). If FORCECLOSE is specified, detach any active vnodes 1724 * that are found. 1725 * 1726 * If WRITECLOSE is set, only flush out regular file vnodes open for 1727 * writing. 1728 * 1729 * SKIPSYSTEM causes any vnodes marked V_SYSTEM to be skipped. 1730 */ 1731 #ifdef DEBUG 1732 int busyprt = 0; /* print out busy vnodes */ 1733 struct ctldebug debug1 = { "busyprt", &busyprt }; 1734 #endif 1735 1736 static vnode_t * 1737 vflushnext(vnode_t *mvp, int *when) 1738 { 1739 1740 if (hardclock_ticks > *when) { 1741 mutex_exit(&mntvnode_lock); 1742 yield(); 1743 mutex_enter(&mntvnode_lock); 1744 *when = hardclock_ticks + hz / 10; 1745 } 1746 1747 return vunmark(mvp); 1748 } 1749 1750 int 1751 vflush(struct mount *mp, vnode_t *skipvp, int flags) 1752 { 1753 vnode_t *vp, *mvp; 1754 int busy = 0, when = 0, gen; 1755 1756 /* 1757 * First, flush out any vnode references from vrele_list. 1758 */ 1759 mutex_enter(&vrele_lock); 1760 gen = vrele_gen; 1761 while (vrele_pending && gen == vrele_gen) { 1762 cv_broadcast(&vrele_cv); 1763 cv_wait(&vrele_cv, &vrele_lock); 1764 } 1765 mutex_exit(&vrele_lock); 1766 1767 /* Allocate a marker vnode. */ 1768 if ((mvp = vnalloc(mp)) == NULL) 1769 return (ENOMEM); 1770 1771 /* 1772 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone() 1773 * and vclean() are called 1774 */ 1775 mutex_enter(&mntvnode_lock); 1776 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp != NULL; 1777 vp = vflushnext(mvp, &when)) { 1778 vmark(mvp, vp); 1779 if (vp->v_mount != mp || vismarker(vp)) 1780 continue; 1781 /* 1782 * Skip over a selected vnode. 1783 */ 1784 if (vp == skipvp) 1785 continue; 1786 mutex_enter(&vp->v_interlock); 1787 /* 1788 * Ignore clean but still referenced vnodes. 1789 */ 1790 if ((vp->v_iflag & VI_CLEAN) != 0) { 1791 mutex_exit(&vp->v_interlock); 1792 continue; 1793 } 1794 /* 1795 * Skip over a vnodes marked VSYSTEM. 1796 */ 1797 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 1798 mutex_exit(&vp->v_interlock); 1799 continue; 1800 } 1801 /* 1802 * If WRITECLOSE is set, only flush out regular file 1803 * vnodes open for writing. 1804 */ 1805 if ((flags & WRITECLOSE) && 1806 (vp->v_writecount == 0 || vp->v_type != VREG)) { 1807 mutex_exit(&vp->v_interlock); 1808 continue; 1809 } 1810 /* 1811 * With v_usecount == 0, all we need to do is clear 1812 * out the vnode data structures and we are done. 1813 */ 1814 if (vp->v_usecount == 0) { 1815 mutex_exit(&mntvnode_lock); 1816 vremfree(vp); 1817 vp->v_usecount = 1; 1818 vclean(vp, DOCLOSE); 1819 vrelel(vp, 0); 1820 mutex_enter(&mntvnode_lock); 1821 continue; 1822 } 1823 /* 1824 * If FORCECLOSE is set, forcibly close the vnode. 1825 * For block or character devices, revert to an 1826 * anonymous device. For all other files, just 1827 * kill them. 1828 */ 1829 if (flags & FORCECLOSE) { 1830 mutex_exit(&mntvnode_lock); 1831 atomic_inc_uint(&vp->v_usecount); 1832 if (vp->v_type != VBLK && vp->v_type != VCHR) { 1833 vclean(vp, DOCLOSE); 1834 vrelel(vp, 0); 1835 } else { 1836 vclean(vp, 0); 1837 vp->v_op = spec_vnodeop_p; /* XXXSMP */ 1838 mutex_exit(&vp->v_interlock); 1839 /* 1840 * The vnode isn't clean, but still resides 1841 * on the mount list. Remove it. XXX This 1842 * is a bit dodgy. 1843 */ 1844 insmntque(vp, NULL); 1845 vrele(vp); 1846 } 1847 mutex_enter(&mntvnode_lock); 1848 continue; 1849 } 1850 #ifdef DEBUG 1851 if (busyprt) 1852 vprint("vflush: busy vnode", vp); 1853 #endif 1854 mutex_exit(&vp->v_interlock); 1855 busy++; 1856 } 1857 mutex_exit(&mntvnode_lock); 1858 vnfree(mvp); 1859 if (busy) 1860 return (EBUSY); 1861 return (0); 1862 } 1863 1864 /* 1865 * Disassociate the underlying file system from a vnode. 1866 * 1867 * Must be called with the interlock held, and will return with it held. 1868 */ 1869 void 1870 vclean(vnode_t *vp, int flags) 1871 { 1872 lwp_t *l = curlwp; 1873 bool recycle, active; 1874 int error; 1875 1876 KASSERT(mutex_owned(&vp->v_interlock)); 1877 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1878 KASSERT(vp->v_usecount != 0); 1879 1880 /* If cleaning is already in progress wait until done and return. */ 1881 if (vp->v_iflag & VI_XLOCK) { 1882 vwait(vp, VI_XLOCK); 1883 return; 1884 } 1885 1886 /* If already clean, nothing to do. */ 1887 if ((vp->v_iflag & VI_CLEAN) != 0) { 1888 return; 1889 } 1890 1891 /* 1892 * Prevent the vnode from being recycled or brought into use 1893 * while we clean it out. 1894 */ 1895 vp->v_iflag |= VI_XLOCK; 1896 if (vp->v_iflag & VI_EXECMAP) { 1897 atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages); 1898 atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages); 1899 } 1900 vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP); 1901 active = (vp->v_usecount > 1); 1902 1903 /* XXXAD should not lock vnode under layer */ 1904 VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK); 1905 1906 /* 1907 * Clean out any cached data associated with the vnode. 1908 * If purging an active vnode, it must be closed and 1909 * deactivated before being reclaimed. Note that the 1910 * VOP_INACTIVE will unlock the vnode. 1911 */ 1912 if (flags & DOCLOSE) { 1913 error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0); 1914 if (error != 0) { 1915 /* XXX, fix vn_start_write's grab of mp and use that. */ 1916 1917 if (wapbl_vphaswapbl(vp)) 1918 WAPBL_DISCARD(wapbl_vptomp(vp)); 1919 error = vinvalbuf(vp, 0, NOCRED, l, 0, 0); 1920 } 1921 KASSERT(error == 0); 1922 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0); 1923 if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) { 1924 spec_node_revoke(vp); 1925 } 1926 } 1927 if (active) { 1928 VOP_INACTIVE(vp, &recycle); 1929 } else { 1930 /* 1931 * Any other processes trying to obtain this lock must first 1932 * wait for VI_XLOCK to clear, then call the new lock operation. 1933 */ 1934 VOP_UNLOCK(vp, 0); 1935 } 1936 1937 /* Disassociate the underlying file system from the vnode. */ 1938 if (VOP_RECLAIM(vp)) { 1939 vpanic(vp, "vclean: cannot reclaim"); 1940 } 1941 1942 KASSERT(vp->v_uobj.uo_npages == 0); 1943 if (vp->v_type == VREG && vp->v_ractx != NULL) { 1944 uvm_ra_freectx(vp->v_ractx); 1945 vp->v_ractx = NULL; 1946 } 1947 cache_purge(vp); 1948 1949 /* Done with purge, notify sleepers of the grim news. */ 1950 mutex_enter(&vp->v_interlock); 1951 vp->v_op = dead_vnodeop_p; 1952 vp->v_tag = VT_NON; 1953 KNOTE(&vp->v_klist, NOTE_REVOKE); 1954 vp->v_iflag &= ~(VI_XLOCK | VI_FREEING); 1955 vp->v_vflag &= ~VV_LOCKSWORK; 1956 if ((flags & DOCLOSE) != 0) { 1957 vp->v_iflag |= VI_CLEAN; 1958 } 1959 cv_broadcast(&vp->v_cv); 1960 1961 KASSERT((vp->v_iflag & VI_ONWORKLST) == 0); 1962 } 1963 1964 /* 1965 * Recycle an unused vnode to the front of the free list. 1966 * Release the passed interlock if the vnode will be recycled. 1967 */ 1968 int 1969 vrecycle(vnode_t *vp, kmutex_t *inter_lkp, struct lwp *l) 1970 { 1971 1972 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1973 1974 mutex_enter(&vp->v_interlock); 1975 if (vp->v_usecount != 0) { 1976 mutex_exit(&vp->v_interlock); 1977 return (0); 1978 } 1979 if (inter_lkp) 1980 mutex_exit(inter_lkp); 1981 vremfree(vp); 1982 vp->v_usecount = 1; 1983 vclean(vp, DOCLOSE); 1984 vrelel(vp, 0); 1985 return (1); 1986 } 1987 1988 /* 1989 * Eliminate all activity associated with a vnode in preparation for 1990 * reuse. Drops a reference from the vnode. 1991 */ 1992 void 1993 vgone(vnode_t *vp) 1994 { 1995 1996 mutex_enter(&vp->v_interlock); 1997 vclean(vp, DOCLOSE); 1998 vrelel(vp, 0); 1999 } 2000 2001 /* 2002 * Lookup a vnode by device number. 2003 */ 2004 int 2005 vfinddev(dev_t dev, enum vtype type, vnode_t **vpp) 2006 { 2007 vnode_t *vp; 2008 int rc = 0; 2009 2010 mutex_enter(&device_lock); 2011 for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) { 2012 if (dev != vp->v_rdev || type != vp->v_type) 2013 continue; 2014 *vpp = vp; 2015 rc = 1; 2016 break; 2017 } 2018 mutex_exit(&device_lock); 2019 return (rc); 2020 } 2021 2022 /* 2023 * Revoke all the vnodes corresponding to the specified minor number 2024 * range (endpoints inclusive) of the specified major. 2025 */ 2026 void 2027 vdevgone(int maj, int minl, int minh, enum vtype type) 2028 { 2029 vnode_t *vp, **vpp; 2030 dev_t dev; 2031 int mn; 2032 2033 vp = NULL; /* XXX gcc */ 2034 2035 mutex_enter(&device_lock); 2036 for (mn = minl; mn <= minh; mn++) { 2037 dev = makedev(maj, mn); 2038 vpp = &specfs_hash[SPECHASH(dev)]; 2039 for (vp = *vpp; vp != NULL;) { 2040 mutex_enter(&vp->v_interlock); 2041 if ((vp->v_iflag & VI_CLEAN) != 0 || 2042 dev != vp->v_rdev || type != vp->v_type) { 2043 mutex_exit(&vp->v_interlock); 2044 vp = vp->v_specnext; 2045 continue; 2046 } 2047 mutex_exit(&device_lock); 2048 if (vget(vp, LK_INTERLOCK) == 0) { 2049 VOP_REVOKE(vp, REVOKEALL); 2050 vrele(vp); 2051 } 2052 mutex_enter(&device_lock); 2053 vp = *vpp; 2054 } 2055 } 2056 mutex_exit(&device_lock); 2057 } 2058 2059 /* 2060 * Eliminate all activity associated with the requested vnode 2061 * and with all vnodes aliased to the requested vnode. 2062 */ 2063 void 2064 vrevoke(vnode_t *vp) 2065 { 2066 vnode_t *vq, **vpp; 2067 enum vtype type; 2068 dev_t dev; 2069 2070 KASSERT(vp->v_usecount > 0); 2071 2072 mutex_enter(&vp->v_interlock); 2073 if ((vp->v_iflag & VI_CLEAN) != 0) { 2074 mutex_exit(&vp->v_interlock); 2075 return; 2076 } else if (vp->v_type != VBLK && vp->v_type != VCHR) { 2077 atomic_inc_uint(&vp->v_usecount); 2078 vclean(vp, DOCLOSE); 2079 vrelel(vp, 0); 2080 return; 2081 } else { 2082 dev = vp->v_rdev; 2083 type = vp->v_type; 2084 mutex_exit(&vp->v_interlock); 2085 } 2086 2087 vpp = &specfs_hash[SPECHASH(dev)]; 2088 mutex_enter(&device_lock); 2089 for (vq = *vpp; vq != NULL;) { 2090 /* If clean or being cleaned, then ignore it. */ 2091 mutex_enter(&vq->v_interlock); 2092 if ((vq->v_iflag & (VI_CLEAN | VI_XLOCK)) != 0 || 2093 vq->v_rdev != dev || vq->v_type != type) { 2094 mutex_exit(&vq->v_interlock); 2095 vq = vq->v_specnext; 2096 continue; 2097 } 2098 mutex_exit(&device_lock); 2099 if (vq->v_usecount == 0) { 2100 vremfree(vq); 2101 vq->v_usecount = 1; 2102 } else { 2103 atomic_inc_uint(&vq->v_usecount); 2104 } 2105 vclean(vq, DOCLOSE); 2106 vrelel(vq, 0); 2107 mutex_enter(&device_lock); 2108 vq = *vpp; 2109 } 2110 mutex_exit(&device_lock); 2111 } 2112 2113 /* 2114 * sysctl helper routine to return list of supported fstypes 2115 */ 2116 int 2117 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS) 2118 { 2119 char bf[sizeof(((struct statvfs *)NULL)->f_fstypename)]; 2120 char *where = oldp; 2121 struct vfsops *v; 2122 size_t needed, left, slen; 2123 int error, first; 2124 2125 if (newp != NULL) 2126 return (EPERM); 2127 if (namelen != 0) 2128 return (EINVAL); 2129 2130 first = 1; 2131 error = 0; 2132 needed = 0; 2133 left = *oldlenp; 2134 2135 sysctl_unlock(); 2136 mutex_enter(&vfs_list_lock); 2137 LIST_FOREACH(v, &vfs_list, vfs_list) { 2138 if (where == NULL) 2139 needed += strlen(v->vfs_name) + 1; 2140 else { 2141 memset(bf, 0, sizeof(bf)); 2142 if (first) { 2143 strncpy(bf, v->vfs_name, sizeof(bf)); 2144 first = 0; 2145 } else { 2146 bf[0] = ' '; 2147 strncpy(bf + 1, v->vfs_name, sizeof(bf) - 1); 2148 } 2149 bf[sizeof(bf)-1] = '\0'; 2150 slen = strlen(bf); 2151 if (left < slen + 1) 2152 break; 2153 v->vfs_refcount++; 2154 mutex_exit(&vfs_list_lock); 2155 /* +1 to copy out the trailing NUL byte */ 2156 error = copyout(bf, where, slen + 1); 2157 mutex_enter(&vfs_list_lock); 2158 v->vfs_refcount--; 2159 if (error) 2160 break; 2161 where += slen; 2162 needed += slen; 2163 left -= slen; 2164 } 2165 } 2166 mutex_exit(&vfs_list_lock); 2167 sysctl_relock(); 2168 *oldlenp = needed; 2169 return (error); 2170 } 2171 2172 2173 int kinfo_vdebug = 1; 2174 int kinfo_vgetfailed; 2175 #define KINFO_VNODESLOP 10 2176 /* 2177 * Dump vnode list (via sysctl). 2178 * Copyout address of vnode followed by vnode. 2179 */ 2180 /* ARGSUSED */ 2181 int 2182 sysctl_kern_vnode(SYSCTLFN_ARGS) 2183 { 2184 char *where = oldp; 2185 size_t *sizep = oldlenp; 2186 struct mount *mp, *nmp; 2187 vnode_t *vp, *mvp, vbuf; 2188 char *bp = where; 2189 char *ewhere; 2190 int error; 2191 2192 if (namelen != 0) 2193 return (EOPNOTSUPP); 2194 if (newp != NULL) 2195 return (EPERM); 2196 2197 #define VPTRSZ sizeof(vnode_t *) 2198 #define VNODESZ sizeof(vnode_t) 2199 if (where == NULL) { 2200 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ); 2201 return (0); 2202 } 2203 ewhere = where + *sizep; 2204 2205 sysctl_unlock(); 2206 mutex_enter(&mountlist_lock); 2207 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 2208 mp = nmp) { 2209 if (vfs_busy(mp, &nmp)) { 2210 continue; 2211 } 2212 /* Allocate a marker vnode. */ 2213 mvp = vnalloc(mp); 2214 /* Should never fail for mp != NULL */ 2215 KASSERT(mvp != NULL); 2216 mutex_enter(&mntvnode_lock); 2217 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; 2218 vp = vunmark(mvp)) { 2219 vmark(mvp, vp); 2220 /* 2221 * Check that the vp is still associated with 2222 * this filesystem. RACE: could have been 2223 * recycled onto the same filesystem. 2224 */ 2225 if (vp->v_mount != mp || vismarker(vp)) 2226 continue; 2227 if (bp + VPTRSZ + VNODESZ > ewhere) { 2228 (void)vunmark(mvp); 2229 mutex_exit(&mntvnode_lock); 2230 vnfree(mvp); 2231 vfs_unbusy(mp, false, NULL); 2232 sysctl_relock(); 2233 *sizep = bp - where; 2234 return (ENOMEM); 2235 } 2236 memcpy(&vbuf, vp, VNODESZ); 2237 mutex_exit(&mntvnode_lock); 2238 if ((error = copyout(&vp, bp, VPTRSZ)) || 2239 (error = copyout(&vbuf, bp + VPTRSZ, VNODESZ))) { 2240 mutex_enter(&mntvnode_lock); 2241 (void)vunmark(mvp); 2242 mutex_exit(&mntvnode_lock); 2243 vnfree(mvp); 2244 vfs_unbusy(mp, false, NULL); 2245 sysctl_relock(); 2246 return (error); 2247 } 2248 bp += VPTRSZ + VNODESZ; 2249 mutex_enter(&mntvnode_lock); 2250 } 2251 mutex_exit(&mntvnode_lock); 2252 vnfree(mvp); 2253 vfs_unbusy(mp, false, &nmp); 2254 } 2255 mutex_exit(&mountlist_lock); 2256 sysctl_relock(); 2257 2258 *sizep = bp - where; 2259 return (0); 2260 } 2261 2262 /* 2263 * Remove clean vnodes from a mountpoint's vnode list. 2264 */ 2265 void 2266 vfs_scrubvnlist(struct mount *mp) 2267 { 2268 vnode_t *vp, *nvp; 2269 2270 retry: 2271 mutex_enter(&mntvnode_lock); 2272 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) { 2273 nvp = TAILQ_NEXT(vp, v_mntvnodes); 2274 mutex_enter(&vp->v_interlock); 2275 if ((vp->v_iflag & VI_CLEAN) != 0) { 2276 TAILQ_REMOVE(&mp->mnt_vnodelist, vp, v_mntvnodes); 2277 vp->v_mount = NULL; 2278 mutex_exit(&mntvnode_lock); 2279 mutex_exit(&vp->v_interlock); 2280 vfs_destroy(mp); 2281 goto retry; 2282 } 2283 mutex_exit(&vp->v_interlock); 2284 } 2285 mutex_exit(&mntvnode_lock); 2286 } 2287 2288 /* 2289 * Check to see if a filesystem is mounted on a block device. 2290 */ 2291 int 2292 vfs_mountedon(vnode_t *vp) 2293 { 2294 vnode_t *vq; 2295 int error = 0; 2296 2297 if (vp->v_type != VBLK) 2298 return ENOTBLK; 2299 if (vp->v_specmountpoint != NULL) 2300 return (EBUSY); 2301 mutex_enter(&device_lock); 2302 for (vq = specfs_hash[SPECHASH(vp->v_rdev)]; vq != NULL; 2303 vq = vq->v_specnext) { 2304 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 2305 continue; 2306 if (vq->v_specmountpoint != NULL) { 2307 error = EBUSY; 2308 break; 2309 } 2310 } 2311 mutex_exit(&device_lock); 2312 return (error); 2313 } 2314 2315 /* 2316 * Unmount all file systems. 2317 * We traverse the list in reverse order under the assumption that doing so 2318 * will avoid needing to worry about dependencies. 2319 */ 2320 bool 2321 vfs_unmountall(struct lwp *l) 2322 { 2323 2324 printf("unmounting file systems..."); 2325 return vfs_unmountall1(l, true, true); 2326 } 2327 2328 static void 2329 vfs_unmount_print(struct mount *mp, const char *pfx) 2330 { 2331 2332 aprint_verbose("%sunmounted %s on %s type %s\n", pfx, 2333 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname, 2334 mp->mnt_stat.f_fstypename); 2335 } 2336 2337 bool 2338 vfs_unmount_forceone(struct lwp *l) 2339 { 2340 struct mount *mp, *nmp; 2341 int error; 2342 2343 nmp = NULL; 2344 2345 CIRCLEQ_FOREACH_REVERSE(mp, &mountlist, mnt_list) { 2346 if (nmp == NULL || mp->mnt_gen > nmp->mnt_gen) { 2347 nmp = mp; 2348 } 2349 } 2350 if (nmp == NULL) { 2351 return false; 2352 } 2353 2354 #ifdef DEBUG 2355 printf("\nforcefully unmounting %s (%s)...", 2356 nmp->mnt_stat.f_mntonname, nmp->mnt_stat.f_mntfromname); 2357 #endif 2358 atomic_inc_uint(&nmp->mnt_refcnt); 2359 if ((error = dounmount(nmp, MNT_FORCE, l)) == 0) { 2360 vfs_unmount_print(nmp, "forcefully "); 2361 return true; 2362 } else { 2363 vfs_destroy(nmp); 2364 } 2365 2366 #ifdef DEBUG 2367 printf("forceful unmount of %s failed with error %d\n", 2368 nmp->mnt_stat.f_mntonname, error); 2369 #endif 2370 2371 return false; 2372 } 2373 2374 bool 2375 vfs_unmountall1(struct lwp *l, bool force, bool verbose) 2376 { 2377 struct mount *mp, *nmp; 2378 bool any_error = false, progress = false; 2379 int error; 2380 2381 for (mp = CIRCLEQ_LAST(&mountlist); 2382 mp != (void *)&mountlist; 2383 mp = nmp) { 2384 nmp = CIRCLEQ_PREV(mp, mnt_list); 2385 #ifdef DEBUG 2386 printf("\nunmounting %p %s (%s)...", 2387 (void *)mp, mp->mnt_stat.f_mntonname, 2388 mp->mnt_stat.f_mntfromname); 2389 #endif 2390 atomic_inc_uint(&mp->mnt_refcnt); 2391 if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) { 2392 vfs_unmount_print(mp, ""); 2393 progress = true; 2394 } else { 2395 vfs_destroy(mp); 2396 if (verbose) { 2397 printf("unmount of %s failed with error %d\n", 2398 mp->mnt_stat.f_mntonname, error); 2399 } 2400 any_error = true; 2401 } 2402 } 2403 if (verbose) { 2404 printf(" done\n"); 2405 } 2406 if (any_error && verbose) { 2407 printf("WARNING: some file systems would not unmount\n"); 2408 } 2409 return progress; 2410 } 2411 2412 /* 2413 * Sync and unmount file systems before shutting down. 2414 */ 2415 void 2416 vfs_shutdown(void) 2417 { 2418 struct lwp *l; 2419 2420 /* XXX we're certainly not running in lwp0's context! */ 2421 l = (curlwp == NULL) ? &lwp0 : curlwp; 2422 2423 vfs_shutdown1(l); 2424 } 2425 2426 void 2427 vfs_sync_all(struct lwp *l) 2428 { 2429 printf("syncing disks... "); 2430 2431 /* remove user processes from run queue */ 2432 suspendsched(); 2433 (void) spl0(); 2434 2435 /* avoid coming back this way again if we panic. */ 2436 doing_shutdown = 1; 2437 2438 sys_sync(l, NULL, NULL); 2439 2440 /* Wait for sync to finish. */ 2441 if (buf_syncwait() != 0) { 2442 #if defined(DDB) && defined(DEBUG_HALT_BUSY) 2443 Debugger(); 2444 #endif 2445 printf("giving up\n"); 2446 return; 2447 } else 2448 printf("done\n"); 2449 } 2450 2451 static void 2452 vfs_shutdown1(struct lwp *l) 2453 { 2454 2455 vfs_sync_all(l); 2456 2457 /* 2458 * If we've panic'd, don't make the situation potentially 2459 * worse by unmounting the file systems. 2460 */ 2461 if (panicstr != NULL) 2462 return; 2463 2464 /* Release inodes held by texts before update. */ 2465 #ifdef notdef 2466 vnshutdown(); 2467 #endif 2468 /* Unmount file systems. */ 2469 vfs_unmountall(l); 2470 } 2471 2472 /* 2473 * Print a list of supported file system types (used by vfs_mountroot) 2474 */ 2475 static void 2476 vfs_print_fstypes(void) 2477 { 2478 struct vfsops *v; 2479 int cnt = 0; 2480 2481 mutex_enter(&vfs_list_lock); 2482 LIST_FOREACH(v, &vfs_list, vfs_list) 2483 ++cnt; 2484 mutex_exit(&vfs_list_lock); 2485 2486 if (cnt == 0) { 2487 printf("WARNING: No file system modules have been loaded.\n"); 2488 return; 2489 } 2490 2491 printf("Supported file systems:"); 2492 mutex_enter(&vfs_list_lock); 2493 LIST_FOREACH(v, &vfs_list, vfs_list) { 2494 printf(" %s", v->vfs_name); 2495 } 2496 mutex_exit(&vfs_list_lock); 2497 printf("\n"); 2498 } 2499 2500 /* 2501 * Mount the root file system. If the operator didn't specify a 2502 * file system to use, try all possible file systems until one 2503 * succeeds. 2504 */ 2505 int 2506 vfs_mountroot(void) 2507 { 2508 struct vfsops *v; 2509 int error = ENODEV; 2510 2511 if (root_device == NULL) 2512 panic("vfs_mountroot: root device unknown"); 2513 2514 switch (device_class(root_device)) { 2515 case DV_IFNET: 2516 if (rootdev != NODEV) 2517 panic("vfs_mountroot: rootdev set for DV_IFNET " 2518 "(0x%llx -> %llu,%llu)", 2519 (unsigned long long)rootdev, 2520 (unsigned long long)major(rootdev), 2521 (unsigned long long)minor(rootdev)); 2522 break; 2523 2524 case DV_DISK: 2525 if (rootdev == NODEV) 2526 panic("vfs_mountroot: rootdev not set for DV_DISK"); 2527 if (bdevvp(rootdev, &rootvp)) 2528 panic("vfs_mountroot: can't get vnode for rootdev"); 2529 error = VOP_OPEN(rootvp, FREAD, FSCRED); 2530 if (error) { 2531 printf("vfs_mountroot: can't open root device\n"); 2532 return (error); 2533 } 2534 break; 2535 2536 case DV_VIRTUAL: 2537 break; 2538 2539 default: 2540 printf("%s: inappropriate for root file system\n", 2541 device_xname(root_device)); 2542 return (ENODEV); 2543 } 2544 2545 /* 2546 * If user specified a root fs type, use it. Make sure the 2547 * specified type exists and has a mount_root() 2548 */ 2549 if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) { 2550 v = vfs_getopsbyname(rootfstype); 2551 error = EFTYPE; 2552 if (v != NULL) { 2553 if (v->vfs_mountroot != NULL) { 2554 error = (v->vfs_mountroot)(); 2555 } 2556 v->vfs_refcount--; 2557 } 2558 goto done; 2559 } 2560 2561 /* 2562 * Try each file system currently configured into the kernel. 2563 */ 2564 mutex_enter(&vfs_list_lock); 2565 LIST_FOREACH(v, &vfs_list, vfs_list) { 2566 if (v->vfs_mountroot == NULL) 2567 continue; 2568 #ifdef DEBUG 2569 aprint_normal("mountroot: trying %s...\n", v->vfs_name); 2570 #endif 2571 v->vfs_refcount++; 2572 mutex_exit(&vfs_list_lock); 2573 error = (*v->vfs_mountroot)(); 2574 mutex_enter(&vfs_list_lock); 2575 v->vfs_refcount--; 2576 if (!error) { 2577 aprint_normal("root file system type: %s\n", 2578 v->vfs_name); 2579 break; 2580 } 2581 } 2582 mutex_exit(&vfs_list_lock); 2583 2584 if (v == NULL) { 2585 vfs_print_fstypes(); 2586 printf("no file system for %s", device_xname(root_device)); 2587 if (device_class(root_device) == DV_DISK) 2588 printf(" (dev 0x%llx)", (unsigned long long)rootdev); 2589 printf("\n"); 2590 error = EFTYPE; 2591 } 2592 2593 done: 2594 if (error && device_class(root_device) == DV_DISK) { 2595 VOP_CLOSE(rootvp, FREAD, FSCRED); 2596 vrele(rootvp); 2597 } 2598 if (error == 0) { 2599 extern struct cwdinfo cwdi0; 2600 2601 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS; 2602 CIRCLEQ_FIRST(&mountlist)->mnt_op->vfs_refcount++; 2603 2604 /* 2605 * Get the vnode for '/'. Set cwdi0.cwdi_cdir to 2606 * reference it. 2607 */ 2608 error = VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode); 2609 if (error) 2610 panic("cannot find root vnode, error=%d", error); 2611 cwdi0.cwdi_cdir = rootvnode; 2612 vref(cwdi0.cwdi_cdir); 2613 VOP_UNLOCK(rootvnode, 0); 2614 cwdi0.cwdi_rdir = NULL; 2615 2616 /* 2617 * Now that root is mounted, we can fixup initproc's CWD 2618 * info. All other processes are kthreads, which merely 2619 * share proc0's CWD info. 2620 */ 2621 initproc->p_cwdi->cwdi_cdir = rootvnode; 2622 vref(initproc->p_cwdi->cwdi_cdir); 2623 initproc->p_cwdi->cwdi_rdir = NULL; 2624 /* 2625 * Enable loading of modules from the filesystem 2626 */ 2627 module_load_vfs_init(); 2628 2629 } 2630 return (error); 2631 } 2632 2633 /* 2634 * Get a new unique fsid 2635 */ 2636 void 2637 vfs_getnewfsid(struct mount *mp) 2638 { 2639 static u_short xxxfs_mntid; 2640 fsid_t tfsid; 2641 int mtype; 2642 2643 mutex_enter(&mntid_lock); 2644 mtype = makefstype(mp->mnt_op->vfs_name); 2645 mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0); 2646 mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype; 2647 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 2648 if (xxxfs_mntid == 0) 2649 ++xxxfs_mntid; 2650 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid); 2651 tfsid.__fsid_val[1] = mtype; 2652 if (!CIRCLEQ_EMPTY(&mountlist)) { 2653 while (vfs_getvfs(&tfsid)) { 2654 tfsid.__fsid_val[0]++; 2655 xxxfs_mntid++; 2656 } 2657 } 2658 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0]; 2659 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 2660 mutex_exit(&mntid_lock); 2661 } 2662 2663 /* 2664 * Make a 'unique' number from a mount type name. 2665 */ 2666 long 2667 makefstype(const char *type) 2668 { 2669 long rv; 2670 2671 for (rv = 0; *type; type++) { 2672 rv <<= 2; 2673 rv ^= *type; 2674 } 2675 return rv; 2676 } 2677 2678 /* 2679 * Set vnode attributes to VNOVAL 2680 */ 2681 void 2682 vattr_null(struct vattr *vap) 2683 { 2684 2685 memset(vap, 0, sizeof(*vap)); 2686 2687 vap->va_type = VNON; 2688 2689 /* 2690 * Assign individually so that it is safe even if size and 2691 * sign of each member are varied. 2692 */ 2693 vap->va_mode = VNOVAL; 2694 vap->va_nlink = VNOVAL; 2695 vap->va_uid = VNOVAL; 2696 vap->va_gid = VNOVAL; 2697 vap->va_fsid = VNOVAL; 2698 vap->va_fileid = VNOVAL; 2699 vap->va_size = VNOVAL; 2700 vap->va_blocksize = VNOVAL; 2701 vap->va_atime.tv_sec = 2702 vap->va_mtime.tv_sec = 2703 vap->va_ctime.tv_sec = 2704 vap->va_birthtime.tv_sec = VNOVAL; 2705 vap->va_atime.tv_nsec = 2706 vap->va_mtime.tv_nsec = 2707 vap->va_ctime.tv_nsec = 2708 vap->va_birthtime.tv_nsec = VNOVAL; 2709 vap->va_gen = VNOVAL; 2710 vap->va_flags = VNOVAL; 2711 vap->va_rdev = VNOVAL; 2712 vap->va_bytes = VNOVAL; 2713 } 2714 2715 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 2716 #define ARRAY_PRINT(idx, arr) \ 2717 ((unsigned int)(idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN") 2718 2719 const char * const vnode_tags[] = { VNODE_TAGS }; 2720 const char * const vnode_types[] = { VNODE_TYPES }; 2721 const char vnode_flagbits[] = VNODE_FLAGBITS; 2722 2723 /* 2724 * Print out a description of a vnode. 2725 */ 2726 void 2727 vprint(const char *label, struct vnode *vp) 2728 { 2729 struct vnlock *vl; 2730 char bf[96]; 2731 int flag; 2732 2733 vl = &vp->v_lock; 2734 flag = vp->v_iflag | vp->v_vflag | vp->v_uflag; 2735 snprintb(bf, sizeof(bf), vnode_flagbits, flag); 2736 2737 if (label != NULL) 2738 printf("%s: ", label); 2739 printf("vnode @ %p, flags (%s)\n\ttag %s(%d), type %s(%d), " 2740 "usecount %d, writecount %d, holdcount %d\n" 2741 "\tfreelisthd %p, mount %p, data %p lock %p recursecnt %d\n", 2742 vp, bf, ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag, 2743 ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type, 2744 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, 2745 vp->v_freelisthd, vp->v_mount, vp->v_data, vl, vl->vl_recursecnt); 2746 if (vp->v_data != NULL) { 2747 printf("\t"); 2748 VOP_PRINT(vp); 2749 } 2750 } 2751 2752 #ifdef DEBUG 2753 /* 2754 * List all of the locked vnodes in the system. 2755 * Called when debugging the kernel. 2756 */ 2757 void 2758 printlockedvnodes(void) 2759 { 2760 struct mount *mp, *nmp; 2761 struct vnode *vp; 2762 2763 printf("Locked vnodes\n"); 2764 mutex_enter(&mountlist_lock); 2765 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 2766 mp = nmp) { 2767 if (vfs_busy(mp, &nmp)) { 2768 continue; 2769 } 2770 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2771 if (VOP_ISLOCKED(vp)) 2772 vprint(NULL, vp); 2773 } 2774 mutex_enter(&mountlist_lock); 2775 vfs_unbusy(mp, false, &nmp); 2776 } 2777 mutex_exit(&mountlist_lock); 2778 } 2779 #endif 2780 2781 /* Deprecated. Kept for KPI compatibility. */ 2782 int 2783 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 2784 mode_t acc_mode, kauth_cred_t cred) 2785 { 2786 2787 #ifdef DIAGNOSTIC 2788 printf("vaccess: deprecated interface used.\n"); 2789 #endif /* DIAGNOSTIC */ 2790 2791 return genfs_can_access(type, file_mode, uid, gid, acc_mode, cred); 2792 } 2793 2794 /* 2795 * Given a file system name, look up the vfsops for that 2796 * file system, or return NULL if file system isn't present 2797 * in the kernel. 2798 */ 2799 struct vfsops * 2800 vfs_getopsbyname(const char *name) 2801 { 2802 struct vfsops *v; 2803 2804 mutex_enter(&vfs_list_lock); 2805 LIST_FOREACH(v, &vfs_list, vfs_list) { 2806 if (strcmp(v->vfs_name, name) == 0) 2807 break; 2808 } 2809 if (v != NULL) 2810 v->vfs_refcount++; 2811 mutex_exit(&vfs_list_lock); 2812 2813 return (v); 2814 } 2815 2816 void 2817 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp) 2818 { 2819 const struct statvfs *mbp; 2820 2821 if (sbp == (mbp = &mp->mnt_stat)) 2822 return; 2823 2824 (void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx)); 2825 sbp->f_fsid = mbp->f_fsid; 2826 sbp->f_owner = mbp->f_owner; 2827 sbp->f_flag = mbp->f_flag; 2828 sbp->f_syncwrites = mbp->f_syncwrites; 2829 sbp->f_asyncwrites = mbp->f_asyncwrites; 2830 sbp->f_syncreads = mbp->f_syncreads; 2831 sbp->f_asyncreads = mbp->f_asyncreads; 2832 (void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare)); 2833 (void)memcpy(sbp->f_fstypename, mbp->f_fstypename, 2834 sizeof(sbp->f_fstypename)); 2835 (void)memcpy(sbp->f_mntonname, mbp->f_mntonname, 2836 sizeof(sbp->f_mntonname)); 2837 (void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, 2838 sizeof(sbp->f_mntfromname)); 2839 sbp->f_namemax = mbp->f_namemax; 2840 } 2841 2842 int 2843 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom, 2844 const char *vfsname, struct mount *mp, struct lwp *l) 2845 { 2846 int error; 2847 size_t size; 2848 struct statvfs *sfs = &mp->mnt_stat; 2849 int (*fun)(const void *, void *, size_t, size_t *); 2850 2851 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsname, 2852 sizeof(mp->mnt_stat.f_fstypename)); 2853 2854 if (onp) { 2855 struct cwdinfo *cwdi = l->l_proc->p_cwdi; 2856 fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr; 2857 if (cwdi->cwdi_rdir != NULL) { 2858 size_t len; 2859 char *bp; 2860 char *path = PNBUF_GET(); 2861 2862 bp = path + MAXPATHLEN; 2863 *--bp = '\0'; 2864 rw_enter(&cwdi->cwdi_lock, RW_READER); 2865 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, 2866 path, MAXPATHLEN / 2, 0, l); 2867 rw_exit(&cwdi->cwdi_lock); 2868 if (error) { 2869 PNBUF_PUT(path); 2870 return error; 2871 } 2872 2873 len = strlen(bp); 2874 if (len > sizeof(sfs->f_mntonname) - 1) 2875 len = sizeof(sfs->f_mntonname) - 1; 2876 (void)strncpy(sfs->f_mntonname, bp, len); 2877 PNBUF_PUT(path); 2878 2879 if (len < sizeof(sfs->f_mntonname) - 1) { 2880 error = (*fun)(onp, &sfs->f_mntonname[len], 2881 sizeof(sfs->f_mntonname) - len - 1, &size); 2882 if (error) 2883 return error; 2884 size += len; 2885 } else { 2886 size = len; 2887 } 2888 } else { 2889 error = (*fun)(onp, &sfs->f_mntonname, 2890 sizeof(sfs->f_mntonname) - 1, &size); 2891 if (error) 2892 return error; 2893 } 2894 (void)memset(sfs->f_mntonname + size, 0, 2895 sizeof(sfs->f_mntonname) - size); 2896 } 2897 2898 if (fromp) { 2899 fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr; 2900 error = (*fun)(fromp, sfs->f_mntfromname, 2901 sizeof(sfs->f_mntfromname) - 1, &size); 2902 if (error) 2903 return error; 2904 (void)memset(sfs->f_mntfromname + size, 0, 2905 sizeof(sfs->f_mntfromname) - size); 2906 } 2907 return 0; 2908 } 2909 2910 void 2911 vfs_timestamp(struct timespec *ts) 2912 { 2913 2914 nanotime(ts); 2915 } 2916 2917 time_t rootfstime; /* recorded root fs time, if known */ 2918 void 2919 setrootfstime(time_t t) 2920 { 2921 rootfstime = t; 2922 } 2923 2924 /* 2925 * Sham lock manager for vnodes. This is a temporary measure. 2926 */ 2927 int 2928 vlockmgr(struct vnlock *vl, int flags) 2929 { 2930 2931 KASSERT((flags & ~(LK_CANRECURSE | LK_NOWAIT | LK_TYPE_MASK)) == 0); 2932 2933 switch (flags & LK_TYPE_MASK) { 2934 case LK_SHARED: 2935 if (rw_tryenter(&vl->vl_lock, RW_READER)) { 2936 return 0; 2937 } 2938 if ((flags & LK_NOWAIT) != 0) { 2939 return EBUSY; 2940 } 2941 rw_enter(&vl->vl_lock, RW_READER); 2942 return 0; 2943 2944 case LK_EXCLUSIVE: 2945 if (rw_tryenter(&vl->vl_lock, RW_WRITER)) { 2946 return 0; 2947 } 2948 if ((vl->vl_canrecurse || (flags & LK_CANRECURSE) != 0) && 2949 rw_write_held(&vl->vl_lock)) { 2950 vl->vl_recursecnt++; 2951 return 0; 2952 } 2953 if ((flags & LK_NOWAIT) != 0) { 2954 return EBUSY; 2955 } 2956 rw_enter(&vl->vl_lock, RW_WRITER); 2957 return 0; 2958 2959 case LK_RELEASE: 2960 if (vl->vl_recursecnt != 0) { 2961 KASSERT(rw_write_held(&vl->vl_lock)); 2962 vl->vl_recursecnt--; 2963 return 0; 2964 } 2965 rw_exit(&vl->vl_lock); 2966 return 0; 2967 2968 default: 2969 panic("vlockmgr: flags %x", flags); 2970 } 2971 } 2972 2973 int 2974 vlockstatus(struct vnlock *vl) 2975 { 2976 2977 if (rw_write_held(&vl->vl_lock)) { 2978 return LK_EXCLUSIVE; 2979 } 2980 if (rw_read_held(&vl->vl_lock)) { 2981 return LK_SHARED; 2982 } 2983 return 0; 2984 } 2985 2986 static const uint8_t vttodt_tab[9] = { 2987 DT_UNKNOWN, /* VNON */ 2988 DT_REG, /* VREG */ 2989 DT_DIR, /* VDIR */ 2990 DT_BLK, /* VBLK */ 2991 DT_CHR, /* VCHR */ 2992 DT_LNK, /* VLNK */ 2993 DT_SOCK, /* VSUCK */ 2994 DT_FIFO, /* VFIFO */ 2995 DT_UNKNOWN /* VBAD */ 2996 }; 2997 2998 uint8_t 2999 vtype2dt(enum vtype vt) 3000 { 3001 3002 CTASSERT(VBAD == __arraycount(vttodt_tab) - 1); 3003 return vttodt_tab[vt]; 3004 } 3005 3006 /* 3007 * mount_specific_key_create -- 3008 * Create a key for subsystem mount-specific data. 3009 */ 3010 int 3011 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 3012 { 3013 3014 return (specificdata_key_create(mount_specificdata_domain, keyp, dtor)); 3015 } 3016 3017 /* 3018 * mount_specific_key_delete -- 3019 * Delete a key for subsystem mount-specific data. 3020 */ 3021 void 3022 mount_specific_key_delete(specificdata_key_t key) 3023 { 3024 3025 specificdata_key_delete(mount_specificdata_domain, key); 3026 } 3027 3028 /* 3029 * mount_initspecific -- 3030 * Initialize a mount's specificdata container. 3031 */ 3032 void 3033 mount_initspecific(struct mount *mp) 3034 { 3035 int error; 3036 3037 error = specificdata_init(mount_specificdata_domain, 3038 &mp->mnt_specdataref); 3039 KASSERT(error == 0); 3040 } 3041 3042 /* 3043 * mount_finispecific -- 3044 * Finalize a mount's specificdata container. 3045 */ 3046 void 3047 mount_finispecific(struct mount *mp) 3048 { 3049 3050 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 3051 } 3052 3053 /* 3054 * mount_getspecific -- 3055 * Return mount-specific data corresponding to the specified key. 3056 */ 3057 void * 3058 mount_getspecific(struct mount *mp, specificdata_key_t key) 3059 { 3060 3061 return (specificdata_getspecific(mount_specificdata_domain, 3062 &mp->mnt_specdataref, key)); 3063 } 3064 3065 /* 3066 * mount_setspecific -- 3067 * Set mount-specific data corresponding to the specified key. 3068 */ 3069 void 3070 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data) 3071 { 3072 3073 specificdata_setspecific(mount_specificdata_domain, 3074 &mp->mnt_specdataref, key, data); 3075 } 3076 3077 int 3078 VFS_MOUNT(struct mount *mp, const char *a, void *b, size_t *c) 3079 { 3080 int error; 3081 3082 KERNEL_LOCK(1, NULL); 3083 error = (*(mp->mnt_op->vfs_mount))(mp, a, b, c); 3084 KERNEL_UNLOCK_ONE(NULL); 3085 3086 return error; 3087 } 3088 3089 int 3090 VFS_START(struct mount *mp, int a) 3091 { 3092 int error; 3093 3094 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3095 KERNEL_LOCK(1, NULL); 3096 } 3097 error = (*(mp->mnt_op->vfs_start))(mp, a); 3098 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3099 KERNEL_UNLOCK_ONE(NULL); 3100 } 3101 3102 return error; 3103 } 3104 3105 int 3106 VFS_UNMOUNT(struct mount *mp, int a) 3107 { 3108 int error; 3109 3110 KERNEL_LOCK(1, NULL); 3111 error = (*(mp->mnt_op->vfs_unmount))(mp, a); 3112 KERNEL_UNLOCK_ONE(NULL); 3113 3114 return error; 3115 } 3116 3117 int 3118 VFS_ROOT(struct mount *mp, struct vnode **a) 3119 { 3120 int error; 3121 3122 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3123 KERNEL_LOCK(1, NULL); 3124 } 3125 error = (*(mp->mnt_op->vfs_root))(mp, a); 3126 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3127 KERNEL_UNLOCK_ONE(NULL); 3128 } 3129 3130 return error; 3131 } 3132 3133 int 3134 VFS_QUOTACTL(struct mount *mp, int a, uid_t b, void *c) 3135 { 3136 int error; 3137 3138 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3139 KERNEL_LOCK(1, NULL); 3140 } 3141 error = (*(mp->mnt_op->vfs_quotactl))(mp, a, b, c); 3142 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3143 KERNEL_UNLOCK_ONE(NULL); 3144 } 3145 3146 return error; 3147 } 3148 3149 int 3150 VFS_STATVFS(struct mount *mp, struct statvfs *a) 3151 { 3152 int error; 3153 3154 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3155 KERNEL_LOCK(1, NULL); 3156 } 3157 error = (*(mp->mnt_op->vfs_statvfs))(mp, a); 3158 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3159 KERNEL_UNLOCK_ONE(NULL); 3160 } 3161 3162 return error; 3163 } 3164 3165 int 3166 VFS_SYNC(struct mount *mp, int a, struct kauth_cred *b) 3167 { 3168 int error; 3169 3170 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3171 KERNEL_LOCK(1, NULL); 3172 } 3173 error = (*(mp->mnt_op->vfs_sync))(mp, a, b); 3174 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3175 KERNEL_UNLOCK_ONE(NULL); 3176 } 3177 3178 return error; 3179 } 3180 3181 int 3182 VFS_FHTOVP(struct mount *mp, struct fid *a, struct vnode **b) 3183 { 3184 int error; 3185 3186 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3187 KERNEL_LOCK(1, NULL); 3188 } 3189 error = (*(mp->mnt_op->vfs_fhtovp))(mp, a, b); 3190 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3191 KERNEL_UNLOCK_ONE(NULL); 3192 } 3193 3194 return error; 3195 } 3196 3197 int 3198 VFS_VPTOFH(struct vnode *vp, struct fid *a, size_t *b) 3199 { 3200 int error; 3201 3202 if ((vp->v_vflag & VV_MPSAFE) == 0) { 3203 KERNEL_LOCK(1, NULL); 3204 } 3205 error = (*(vp->v_mount->mnt_op->vfs_vptofh))(vp, a, b); 3206 if ((vp->v_vflag & VV_MPSAFE) == 0) { 3207 KERNEL_UNLOCK_ONE(NULL); 3208 } 3209 3210 return error; 3211 } 3212 3213 int 3214 VFS_SNAPSHOT(struct mount *mp, struct vnode *a, struct timespec *b) 3215 { 3216 int error; 3217 3218 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3219 KERNEL_LOCK(1, NULL); 3220 } 3221 error = (*(mp->mnt_op->vfs_snapshot))(mp, a, b); 3222 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3223 KERNEL_UNLOCK_ONE(NULL); 3224 } 3225 3226 return error; 3227 } 3228 3229 int 3230 VFS_EXTATTRCTL(struct mount *mp, int a, struct vnode *b, int c, const char *d) 3231 { 3232 int error; 3233 3234 KERNEL_LOCK(1, NULL); /* XXXSMP check ffs */ 3235 error = (*(mp->mnt_op->vfs_extattrctl))(mp, a, b, c, d); 3236 KERNEL_UNLOCK_ONE(NULL); /* XXX */ 3237 3238 return error; 3239 } 3240 3241 int 3242 VFS_SUSPENDCTL(struct mount *mp, int a) 3243 { 3244 int error; 3245 3246 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3247 KERNEL_LOCK(1, NULL); 3248 } 3249 error = (*(mp->mnt_op->vfs_suspendctl))(mp, a); 3250 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3251 KERNEL_UNLOCK_ONE(NULL); 3252 } 3253 3254 return error; 3255 } 3256 3257 #if defined(DDB) || defined(DEBUGPRINT) 3258 static const char buf_flagbits[] = BUF_FLAGBITS; 3259 3260 void 3261 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...)) 3262 { 3263 char bf[1024]; 3264 3265 (*pr)(" vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" rawblkno 0x%" 3266 PRIx64 " dev 0x%x\n", 3267 bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_rawblkno, bp->b_dev); 3268 3269 snprintb(bf, sizeof(bf), 3270 buf_flagbits, bp->b_flags | bp->b_oflags | bp->b_cflags); 3271 (*pr)(" error %d flags 0x%s\n", bp->b_error, bf); 3272 3273 (*pr)(" bufsize 0x%lx bcount 0x%lx resid 0x%lx\n", 3274 bp->b_bufsize, bp->b_bcount, bp->b_resid); 3275 (*pr)(" data %p saveaddr %p\n", 3276 bp->b_data, bp->b_saveaddr); 3277 (*pr)(" iodone %p objlock %p\n", bp->b_iodone, bp->b_objlock); 3278 } 3279 3280 3281 void 3282 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...)) 3283 { 3284 char bf[256]; 3285 3286 uvm_object_printit(&vp->v_uobj, full, pr); 3287 snprintb(bf, sizeof(bf), 3288 vnode_flagbits, vp->v_iflag | vp->v_vflag | vp->v_uflag); 3289 (*pr)("\nVNODE flags %s\n", bf); 3290 (*pr)("mp %p numoutput %d size 0x%llx writesize 0x%llx\n", 3291 vp->v_mount, vp->v_numoutput, vp->v_size, vp->v_writesize); 3292 3293 (*pr)("data %p writecount %ld holdcnt %ld\n", 3294 vp->v_data, vp->v_writecount, vp->v_holdcnt); 3295 3296 (*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n", 3297 ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag, 3298 ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type, 3299 vp->v_mount, vp->v_mountedhere); 3300 3301 (*pr)("v_lock %p\n", &vp->v_lock); 3302 3303 if (full) { 3304 struct buf *bp; 3305 3306 (*pr)("clean bufs:\n"); 3307 LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) { 3308 (*pr)(" bp %p\n", bp); 3309 vfs_buf_print(bp, full, pr); 3310 } 3311 3312 (*pr)("dirty bufs:\n"); 3313 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 3314 (*pr)(" bp %p\n", bp); 3315 vfs_buf_print(bp, full, pr); 3316 } 3317 } 3318 } 3319 3320 void 3321 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...)) 3322 { 3323 char sbuf[256]; 3324 3325 (*pr)("vnodecovered = %p syncer = %p data = %p\n", 3326 mp->mnt_vnodecovered,mp->mnt_syncer,mp->mnt_data); 3327 3328 (*pr)("fs_bshift %d dev_bshift = %d\n", 3329 mp->mnt_fs_bshift,mp->mnt_dev_bshift); 3330 3331 snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_flag); 3332 (*pr)("flag = %s\n", sbuf); 3333 3334 snprintb(sbuf, sizeof(sbuf), __IMNT_FLAG_BITS, mp->mnt_iflag); 3335 (*pr)("iflag = %s\n", sbuf); 3336 3337 (*pr)("refcnt = %d unmounting @ %p updating @ %p\n", mp->mnt_refcnt, 3338 &mp->mnt_unmounting, &mp->mnt_updating); 3339 3340 (*pr)("statvfs cache:\n"); 3341 (*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize); 3342 (*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize); 3343 (*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize); 3344 3345 (*pr)("\tblocks = %"PRIu64"\n",mp->mnt_stat.f_blocks); 3346 (*pr)("\tbfree = %"PRIu64"\n",mp->mnt_stat.f_bfree); 3347 (*pr)("\tbavail = %"PRIu64"\n",mp->mnt_stat.f_bavail); 3348 (*pr)("\tbresvd = %"PRIu64"\n",mp->mnt_stat.f_bresvd); 3349 3350 (*pr)("\tfiles = %"PRIu64"\n",mp->mnt_stat.f_files); 3351 (*pr)("\tffree = %"PRIu64"\n",mp->mnt_stat.f_ffree); 3352 (*pr)("\tfavail = %"PRIu64"\n",mp->mnt_stat.f_favail); 3353 (*pr)("\tfresvd = %"PRIu64"\n",mp->mnt_stat.f_fresvd); 3354 3355 (*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n", 3356 mp->mnt_stat.f_fsidx.__fsid_val[0], 3357 mp->mnt_stat.f_fsidx.__fsid_val[1]); 3358 3359 (*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner); 3360 (*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax); 3361 3362 snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_stat.f_flag); 3363 3364 (*pr)("\tflag = %s\n",sbuf); 3365 (*pr)("\tsyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_syncwrites); 3366 (*pr)("\tasyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_asyncwrites); 3367 (*pr)("\tsyncreads = %" PRIu64 "\n",mp->mnt_stat.f_syncreads); 3368 (*pr)("\tasyncreads = %" PRIu64 "\n",mp->mnt_stat.f_asyncreads); 3369 (*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename); 3370 (*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname); 3371 (*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname); 3372 3373 { 3374 int cnt = 0; 3375 struct vnode *vp; 3376 (*pr)("locked vnodes ="); 3377 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 3378 if (VOP_ISLOCKED(vp)) { 3379 if ((++cnt % 6) == 0) { 3380 (*pr)(" %p,\n\t", vp); 3381 } else { 3382 (*pr)(" %p,", vp); 3383 } 3384 } 3385 } 3386 (*pr)("\n"); 3387 } 3388 3389 if (full) { 3390 int cnt = 0; 3391 struct vnode *vp; 3392 (*pr)("all vnodes ="); 3393 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 3394 if (!TAILQ_NEXT(vp, v_mntvnodes)) { 3395 (*pr)(" %p", vp); 3396 } else if ((++cnt % 6) == 0) { 3397 (*pr)(" %p,\n\t", vp); 3398 } else { 3399 (*pr)(" %p,", vp); 3400 } 3401 } 3402 (*pr)("\n", vp); 3403 } 3404 } 3405 #endif /* DDB || DEBUGPRINT */ 3406 3407 /* 3408 * Check if a device pointed to by vp is mounted. 3409 * 3410 * Returns: 3411 * EINVAL if it's not a disk 3412 * EBUSY if it's a disk and mounted 3413 * 0 if it's a disk and not mounted 3414 */ 3415 int 3416 rawdev_mounted(struct vnode *vp, struct vnode **bvpp) 3417 { 3418 struct vnode *bvp; 3419 dev_t dev; 3420 int d_type; 3421 3422 bvp = NULL; 3423 dev = vp->v_rdev; 3424 d_type = D_OTHER; 3425 3426 if (iskmemvp(vp)) 3427 return EINVAL; 3428 3429 switch (vp->v_type) { 3430 case VCHR: { 3431 const struct cdevsw *cdev; 3432 3433 cdev = cdevsw_lookup(dev); 3434 if (cdev != NULL) { 3435 dev_t blkdev; 3436 3437 blkdev = devsw_chr2blk(dev); 3438 if (blkdev != NODEV) { 3439 vfinddev(blkdev, VBLK, &bvp); 3440 if (bvp != NULL) 3441 d_type = (cdev->d_flag & D_TYPEMASK); 3442 } 3443 } 3444 3445 break; 3446 } 3447 3448 case VBLK: { 3449 const struct bdevsw *bdev; 3450 3451 bdev = bdevsw_lookup(dev); 3452 if (bdev != NULL) 3453 d_type = (bdev->d_flag & D_TYPEMASK); 3454 3455 bvp = vp; 3456 3457 break; 3458 } 3459 3460 default: 3461 break; 3462 } 3463 3464 if (d_type != D_DISK) 3465 return EINVAL; 3466 3467 if (bvpp != NULL) 3468 *bvpp = bvp; 3469 3470 /* 3471 * XXX: This is bogus. We should be failing the request 3472 * XXX: not only if this specific slice is mounted, but 3473 * XXX: if it's on a disk with any other mounted slice. 3474 */ 3475 if (vfs_mountedon(bvp)) 3476 return EBUSY; 3477 3478 return 0; 3479 } 3480