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