1 /* $NetBSD: vfs_subr.c,v 1.355 2008/07/31 05:38:05 simonb 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.355 2008/07/31 05:38:05 simonb 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/proc.h> 93 #include <sys/kernel.h> 94 #include <sys/mount.h> 95 #include <sys/fcntl.h> 96 #include <sys/vnode.h> 97 #include <sys/stat.h> 98 #include <sys/namei.h> 99 #include <sys/ucred.h> 100 #include <sys/buf.h> 101 #include <sys/errno.h> 102 #include <sys/malloc.h> 103 #include <sys/syscallargs.h> 104 #include <sys/device.h> 105 #include <sys/filedesc.h> 106 #include <sys/kauth.h> 107 #include <sys/atomic.h> 108 #include <sys/kthread.h> 109 #include <sys/wapbl.h> 110 111 #include <miscfs/specfs/specdev.h> 112 #include <miscfs/syncfs/syncfs.h> 113 114 #include <uvm/uvm.h> 115 #include <uvm/uvm_readahead.h> 116 #include <uvm/uvm_ddb.h> 117 118 #include <sys/sysctl.h> 119 120 const enum vtype iftovt_tab[16] = { 121 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 122 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 123 }; 124 const int vttoif_tab[9] = { 125 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 126 S_IFSOCK, S_IFIFO, S_IFMT, 127 }; 128 129 /* 130 * Insq/Remq for the vnode usage lists. 131 */ 132 #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) 133 #define bufremvn(bp) { \ 134 LIST_REMOVE(bp, b_vnbufs); \ 135 (bp)->b_vnbufs.le_next = NOLIST; \ 136 } 137 138 int doforce = 1; /* 1 => permit forcible unmounting */ 139 int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 140 141 extern int dovfsusermount; /* 1 => permit any user to mount filesystems */ 142 extern int vfs_magiclinks; /* 1 => expand "magic" symlinks */ 143 144 static vnodelst_t vnode_free_list = TAILQ_HEAD_INITIALIZER(vnode_free_list); 145 static vnodelst_t vnode_hold_list = TAILQ_HEAD_INITIALIZER(vnode_hold_list); 146 static vnodelst_t vrele_list = TAILQ_HEAD_INITIALIZER(vrele_list); 147 148 struct mntlist mountlist = /* mounted filesystem list */ 149 CIRCLEQ_HEAD_INITIALIZER(mountlist); 150 151 u_int numvnodes; 152 static specificdata_domain_t mount_specificdata_domain; 153 154 static int vrele_pending; 155 static int vrele_gen; 156 static kmutex_t vrele_lock; 157 static kcondvar_t vrele_cv; 158 static lwp_t *vrele_lwp; 159 160 kmutex_t mountlist_lock; 161 kmutex_t mntid_lock; 162 kmutex_t mntvnode_lock; 163 kmutex_t vnode_free_list_lock; 164 kmutex_t specfs_lock; 165 kmutex_t vfs_list_lock; 166 167 static pool_cache_t vnode_cache; 168 169 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 170 171 /* 172 * These define the root filesystem and device. 173 */ 174 struct vnode *rootvnode; 175 struct device *root_device; /* root device */ 176 177 /* 178 * Local declarations. 179 */ 180 181 static void vrele_thread(void *); 182 static void insmntque(vnode_t *, struct mount *); 183 static int getdevvp(dev_t, vnode_t **, enum vtype); 184 static vnode_t *getcleanvnode(void);; 185 void vpanic(vnode_t *, const char *); 186 187 #ifdef DEBUG 188 void printlockedvnodes(void); 189 #endif 190 191 #ifdef DIAGNOSTIC 192 void 193 vpanic(vnode_t *vp, const char *msg) 194 { 195 196 vprint(NULL, vp); 197 panic("%s\n", msg); 198 } 199 #else 200 #define vpanic(vp, msg) /* nothing */ 201 #endif 202 203 void 204 vn_init1(void) 205 { 206 207 vnode_cache = pool_cache_init(sizeof(struct vnode), 0, 0, 0, "vnodepl", 208 NULL, IPL_NONE, NULL, NULL, NULL); 209 KASSERT(vnode_cache != NULL); 210 211 /* Create deferred release thread. */ 212 mutex_init(&vrele_lock, MUTEX_DEFAULT, IPL_NONE); 213 cv_init(&vrele_cv, "vrele"); 214 if (kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vrele_thread, 215 NULL, &vrele_lwp, "vrele")) 216 panic("fork vrele"); 217 } 218 219 /* 220 * Initialize the vnode management data structures. 221 */ 222 void 223 vntblinit(void) 224 { 225 226 mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE); 227 mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE); 228 mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE); 229 mutex_init(&vnode_free_list_lock, MUTEX_DEFAULT, IPL_NONE); 230 mutex_init(&specfs_lock, MUTEX_DEFAULT, IPL_NONE); 231 mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE); 232 233 mount_specificdata_domain = specificdata_domain_create(); 234 235 /* Initialize the filesystem syncer. */ 236 vn_initialize_syncerd(); 237 vn_init1(); 238 } 239 240 int 241 vfs_drainvnodes(long target, struct lwp *l) 242 { 243 244 while (numvnodes > target) { 245 vnode_t *vp; 246 247 mutex_enter(&vnode_free_list_lock); 248 vp = getcleanvnode(); 249 if (vp == NULL) 250 return EBUSY; /* give up */ 251 ungetnewvnode(vp); 252 } 253 254 return 0; 255 } 256 257 /* 258 * Lookup a mount point by filesystem identifier. 259 * 260 * XXX Needs to add a reference to the mount point. 261 */ 262 struct mount * 263 vfs_getvfs(fsid_t *fsid) 264 { 265 struct mount *mp; 266 267 mutex_enter(&mountlist_lock); 268 CIRCLEQ_FOREACH(mp, &mountlist, mnt_list) { 269 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] && 270 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) { 271 mutex_exit(&mountlist_lock); 272 return (mp); 273 } 274 } 275 mutex_exit(&mountlist_lock); 276 return ((struct mount *)0); 277 } 278 279 /* 280 * Drop a reference to a mount structure, freeing if the last reference. 281 */ 282 void 283 vfs_destroy(struct mount *mp) 284 { 285 286 if (__predict_true(atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) { 287 return; 288 } 289 290 /* 291 * Nothing else has visibility of the mount: we can now 292 * free the data structures. 293 */ 294 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 295 rw_destroy(&mp->mnt_unmounting); 296 mutex_destroy(&mp->mnt_updating); 297 mutex_destroy(&mp->mnt_renamelock); 298 if (mp->mnt_op != NULL) { 299 vfs_delref(mp->mnt_op); 300 } 301 kmem_free(mp, sizeof(*mp)); 302 } 303 304 /* 305 * grab a vnode from freelist and clean it. 306 */ 307 vnode_t * 308 getcleanvnode(void) 309 { 310 vnode_t *vp; 311 vnodelst_t *listhd; 312 313 KASSERT(mutex_owned(&vnode_free_list_lock)); 314 315 retry: 316 listhd = &vnode_free_list; 317 try_nextlist: 318 TAILQ_FOREACH(vp, listhd, v_freelist) { 319 /* 320 * It's safe to test v_usecount and v_iflag 321 * without holding the interlock here, since 322 * these vnodes should never appear on the 323 * lists. 324 */ 325 if (vp->v_usecount != 0) { 326 vpanic(vp, "free vnode isn't"); 327 } 328 if ((vp->v_iflag & VI_CLEAN) != 0) { 329 vpanic(vp, "clean vnode on freelist"); 330 } 331 if (vp->v_freelisthd != listhd) { 332 printf("vnode sez %p, listhd %p\n", vp->v_freelisthd, listhd); 333 vpanic(vp, "list head mismatch"); 334 } 335 if (!mutex_tryenter(&vp->v_interlock)) 336 continue; 337 /* 338 * Our lwp might hold the underlying vnode 339 * locked, so don't try to reclaim a VI_LAYER 340 * node if it's locked. 341 */ 342 if ((vp->v_iflag & VI_XLOCK) == 0 && 343 ((vp->v_iflag & VI_LAYER) == 0 || VOP_ISLOCKED(vp) == 0)) { 344 break; 345 } 346 mutex_exit(&vp->v_interlock); 347 } 348 349 if (vp == NULL) { 350 if (listhd == &vnode_free_list) { 351 listhd = &vnode_hold_list; 352 goto try_nextlist; 353 } 354 mutex_exit(&vnode_free_list_lock); 355 return NULL; 356 } 357 358 /* Remove it from the freelist. */ 359 TAILQ_REMOVE(listhd, vp, v_freelist); 360 vp->v_freelisthd = NULL; 361 mutex_exit(&vnode_free_list_lock); 362 363 /* 364 * The vnode is still associated with a file system, so we must 365 * clean it out before reusing it. We need to add a reference 366 * before doing this. If the vnode gains another reference while 367 * being cleaned out then we lose - retry. 368 */ 369 atomic_inc_uint(&vp->v_usecount); 370 vclean(vp, DOCLOSE); 371 if (vp->v_usecount == 1) { 372 /* We're about to dirty it. */ 373 vp->v_iflag &= ~VI_CLEAN; 374 mutex_exit(&vp->v_interlock); 375 if (vp->v_type == VBLK || vp->v_type == VCHR) { 376 spec_node_destroy(vp); 377 } 378 vp->v_type = VNON; 379 } else { 380 /* 381 * Don't return to freelist - the holder of the last 382 * reference will destroy it. 383 */ 384 vrelel(vp, 0); /* releases vp->v_interlock */ 385 mutex_enter(&vnode_free_list_lock); 386 goto retry; 387 } 388 389 if (vp->v_data != NULL || vp->v_uobj.uo_npages != 0 || 390 !TAILQ_EMPTY(&vp->v_uobj.memq)) { 391 vpanic(vp, "cleaned vnode isn't"); 392 } 393 if (vp->v_numoutput != 0) { 394 vpanic(vp, "clean vnode has pending I/O's"); 395 } 396 if ((vp->v_iflag & VI_ONWORKLST) != 0) { 397 vpanic(vp, "clean vnode on syncer list"); 398 } 399 400 return vp; 401 } 402 403 /* 404 * Mark a mount point as busy, and gain a new reference to it. Used to 405 * prevent the file system from being unmounted during critical sections. 406 * 407 * => The caller must hold a pre-existing reference to the mount. 408 * => Will fail if the file system is being unmounted, or is unmounted. 409 */ 410 int 411 vfs_busy(struct mount *mp, struct mount **nextp) 412 { 413 414 KASSERT(mp->mnt_refcnt > 0); 415 416 if (__predict_false(!rw_tryenter(&mp->mnt_unmounting, RW_READER))) { 417 if (nextp != NULL) { 418 KASSERT(mutex_owned(&mountlist_lock)); 419 *nextp = CIRCLEQ_NEXT(mp, mnt_list); 420 } 421 return EBUSY; 422 } 423 if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) { 424 rw_exit(&mp->mnt_unmounting); 425 if (nextp != NULL) { 426 KASSERT(mutex_owned(&mountlist_lock)); 427 *nextp = CIRCLEQ_NEXT(mp, mnt_list); 428 } 429 return ENOENT; 430 } 431 if (nextp != NULL) { 432 mutex_exit(&mountlist_lock); 433 } 434 atomic_inc_uint(&mp->mnt_refcnt); 435 return 0; 436 } 437 438 /* 439 * Unbusy a busy filesystem. 440 * 441 * => If keepref is true, preserve reference added by vfs_busy(). 442 * => If nextp != NULL, acquire mountlist_lock. 443 */ 444 void 445 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp) 446 { 447 448 KASSERT(mp->mnt_refcnt > 0); 449 450 if (nextp != NULL) { 451 mutex_enter(&mountlist_lock); 452 } 453 rw_exit(&mp->mnt_unmounting); 454 if (!keepref) { 455 vfs_destroy(mp); 456 } 457 if (nextp != NULL) { 458 KASSERT(mutex_owned(&mountlist_lock)); 459 *nextp = CIRCLEQ_NEXT(mp, mnt_list); 460 } 461 } 462 463 /* 464 * Lookup a filesystem type, and if found allocate and initialize 465 * a mount structure for it. 466 * 467 * Devname is usually updated by mount(8) after booting. 468 */ 469 int 470 vfs_rootmountalloc(const char *fstypename, const char *devname, 471 struct mount **mpp) 472 { 473 struct vfsops *vfsp = NULL; 474 struct mount *mp; 475 476 mutex_enter(&vfs_list_lock); 477 LIST_FOREACH(vfsp, &vfs_list, vfs_list) 478 if (!strncmp(vfsp->vfs_name, fstypename, 479 sizeof(mp->mnt_stat.f_fstypename))) 480 break; 481 if (vfsp == NULL) { 482 mutex_exit(&vfs_list_lock); 483 return (ENODEV); 484 } 485 vfsp->vfs_refcount++; 486 mutex_exit(&vfs_list_lock); 487 488 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP); 489 if (mp == NULL) 490 return ENOMEM; 491 mp->mnt_refcnt = 1; 492 rw_init(&mp->mnt_unmounting); 493 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE); 494 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE); 495 (void)vfs_busy(mp, NULL); 496 TAILQ_INIT(&mp->mnt_vnodelist); 497 mp->mnt_op = vfsp; 498 mp->mnt_flag = MNT_RDONLY; 499 mp->mnt_vnodecovered = NULL; 500 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, 501 sizeof(mp->mnt_stat.f_fstypename)); 502 mp->mnt_stat.f_mntonname[0] = '/'; 503 mp->mnt_stat.f_mntonname[1] = '\0'; 504 mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] = 505 '\0'; 506 (void)copystr(devname, mp->mnt_stat.f_mntfromname, 507 sizeof(mp->mnt_stat.f_mntfromname) - 1, 0); 508 mount_initspecific(mp); 509 *mpp = mp; 510 return (0); 511 } 512 513 /* 514 * Routines having to do with the management of the vnode table. 515 */ 516 extern int (**dead_vnodeop_p)(void *); 517 518 /* 519 * Return the next vnode from the free list. 520 */ 521 int 522 getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *), 523 vnode_t **vpp) 524 { 525 struct uvm_object *uobj; 526 static int toggle; 527 vnode_t *vp; 528 int error = 0, tryalloc; 529 530 try_again: 531 if (mp != NULL) { 532 /* 533 * Mark filesystem busy while we're creating a 534 * vnode. If unmount is in progress, this will 535 * fail. 536 */ 537 error = vfs_busy(mp, NULL); 538 if (error) 539 return error; 540 } 541 542 /* 543 * We must choose whether to allocate a new vnode or recycle an 544 * existing one. The criterion for allocating a new one is that 545 * the total number of vnodes is less than the number desired or 546 * there are no vnodes on either free list. Generally we only 547 * want to recycle vnodes that have no buffers associated with 548 * them, so we look first on the vnode_free_list. If it is empty, 549 * we next consider vnodes with referencing buffers on the 550 * vnode_hold_list. The toggle ensures that half the time we 551 * will use a buffer from the vnode_hold_list, and half the time 552 * we will allocate a new one unless the list has grown to twice 553 * the desired size. We are reticent to recycle vnodes from the 554 * vnode_hold_list because we will lose the identity of all its 555 * referencing buffers. 556 */ 557 558 vp = NULL; 559 560 mutex_enter(&vnode_free_list_lock); 561 562 toggle ^= 1; 563 if (numvnodes > 2 * desiredvnodes) 564 toggle = 0; 565 566 tryalloc = numvnodes < desiredvnodes || 567 (TAILQ_FIRST(&vnode_free_list) == NULL && 568 (TAILQ_FIRST(&vnode_hold_list) == NULL || toggle)); 569 570 if (tryalloc) { 571 numvnodes++; 572 mutex_exit(&vnode_free_list_lock); 573 if ((vp = vnalloc(NULL)) == NULL) { 574 mutex_enter(&vnode_free_list_lock); 575 numvnodes--; 576 } else 577 vp->v_usecount = 1; 578 } 579 580 if (vp == NULL) { 581 vp = getcleanvnode(); 582 if (vp == NULL) { 583 if (mp != NULL) { 584 vfs_unbusy(mp, false, NULL); 585 } 586 if (tryalloc) { 587 printf("WARNING: unable to allocate new " 588 "vnode, retrying...\n"); 589 kpause("newvn", false, hz, NULL); 590 goto try_again; 591 } 592 tablefull("vnode", "increase kern.maxvnodes or NVNODE"); 593 *vpp = 0; 594 return (ENFILE); 595 } 596 vp->v_iflag = 0; 597 vp->v_vflag = 0; 598 vp->v_uflag = 0; 599 vp->v_socket = NULL; 600 } 601 602 KASSERT(vp->v_usecount == 1); 603 KASSERT(vp->v_freelisthd == NULL); 604 KASSERT(LIST_EMPTY(&vp->v_nclist)); 605 KASSERT(LIST_EMPTY(&vp->v_dnclist)); 606 607 vp->v_type = VNON; 608 vp->v_vnlock = &vp->v_lock; 609 vp->v_tag = tag; 610 vp->v_op = vops; 611 insmntque(vp, mp); 612 *vpp = vp; 613 vp->v_data = 0; 614 615 /* 616 * initialize uvm_object within vnode. 617 */ 618 619 uobj = &vp->v_uobj; 620 KASSERT(uobj->pgops == &uvm_vnodeops); 621 KASSERT(uobj->uo_npages == 0); 622 KASSERT(TAILQ_FIRST(&uobj->memq) == NULL); 623 vp->v_size = vp->v_writesize = VSIZENOTSET; 624 625 if (mp != NULL) { 626 if ((mp->mnt_iflag & IMNT_MPSAFE) != 0) 627 vp->v_vflag |= VV_MPSAFE; 628 vfs_unbusy(mp, true, NULL); 629 } 630 631 return (0); 632 } 633 634 /* 635 * This is really just the reverse of getnewvnode(). Needed for 636 * VFS_VGET functions who may need to push back a vnode in case 637 * of a locking race. 638 */ 639 void 640 ungetnewvnode(vnode_t *vp) 641 { 642 643 KASSERT(vp->v_usecount == 1); 644 KASSERT(vp->v_data == NULL); 645 KASSERT(vp->v_freelisthd == NULL); 646 647 mutex_enter(&vp->v_interlock); 648 vp->v_iflag |= VI_CLEAN; 649 vrelel(vp, 0); 650 } 651 652 /* 653 * Allocate a new, uninitialized vnode. If 'mp' is non-NULL, this is a 654 * marker vnode and we are prepared to wait for the allocation. 655 */ 656 vnode_t * 657 vnalloc(struct mount *mp) 658 { 659 vnode_t *vp; 660 661 vp = pool_cache_get(vnode_cache, (mp != NULL ? PR_WAITOK : PR_NOWAIT)); 662 if (vp == NULL) { 663 return NULL; 664 } 665 666 memset(vp, 0, sizeof(*vp)); 667 UVM_OBJ_INIT(&vp->v_uobj, &uvm_vnodeops, 0); 668 cv_init(&vp->v_cv, "vnode"); 669 /* 670 * done by memset() above. 671 * LIST_INIT(&vp->v_nclist); 672 * LIST_INIT(&vp->v_dnclist); 673 */ 674 675 if (mp != NULL) { 676 vp->v_mount = mp; 677 vp->v_type = VBAD; 678 vp->v_iflag = VI_MARKER; 679 } else { 680 rw_init(&vp->v_lock.vl_lock); 681 } 682 683 return vp; 684 } 685 686 /* 687 * Free an unused, unreferenced vnode. 688 */ 689 void 690 vnfree(vnode_t *vp) 691 { 692 693 KASSERT(vp->v_usecount == 0); 694 695 if ((vp->v_iflag & VI_MARKER) == 0) { 696 rw_destroy(&vp->v_lock.vl_lock); 697 mutex_enter(&vnode_free_list_lock); 698 numvnodes--; 699 mutex_exit(&vnode_free_list_lock); 700 } 701 702 UVM_OBJ_DESTROY(&vp->v_uobj); 703 cv_destroy(&vp->v_cv); 704 pool_cache_put(vnode_cache, vp); 705 } 706 707 /* 708 * Remove a vnode from its freelist. 709 */ 710 static inline void 711 vremfree(vnode_t *vp) 712 { 713 714 KASSERT(mutex_owned(&vp->v_interlock)); 715 KASSERT(vp->v_usecount == 0); 716 717 /* 718 * Note that the reference count must not change until 719 * the vnode is removed. 720 */ 721 mutex_enter(&vnode_free_list_lock); 722 if (vp->v_holdcnt > 0) { 723 KASSERT(vp->v_freelisthd == &vnode_hold_list); 724 } else { 725 KASSERT(vp->v_freelisthd == &vnode_free_list); 726 } 727 TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist); 728 vp->v_freelisthd = NULL; 729 mutex_exit(&vnode_free_list_lock); 730 } 731 732 /* 733 * Move a vnode from one mount queue to another. 734 */ 735 static void 736 insmntque(vnode_t *vp, struct mount *mp) 737 { 738 struct mount *omp; 739 740 #ifdef DIAGNOSTIC 741 if ((mp != NULL) && 742 (mp->mnt_iflag & IMNT_UNMOUNT) && 743 !(mp->mnt_flag & MNT_SOFTDEP) && 744 vp->v_tag != VT_VFS) { 745 panic("insmntque into dying filesystem"); 746 } 747 #endif 748 749 mutex_enter(&mntvnode_lock); 750 /* 751 * Delete from old mount point vnode list, if on one. 752 */ 753 if ((omp = vp->v_mount) != NULL) 754 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes); 755 /* 756 * Insert into list of vnodes for the new mount point, if 757 * available. The caller must take a reference on the mount 758 * structure and donate to the vnode. 759 */ 760 if ((vp->v_mount = mp) != NULL) 761 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes); 762 mutex_exit(&mntvnode_lock); 763 764 if (omp != NULL) { 765 /* Release reference to old mount. */ 766 vfs_destroy(omp); 767 } 768 } 769 770 /* 771 * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or 772 * recycled. 773 */ 774 void 775 vwait(vnode_t *vp, int flags) 776 { 777 778 KASSERT(mutex_owned(&vp->v_interlock)); 779 KASSERT(vp->v_usecount != 0); 780 781 while ((vp->v_iflag & flags) != 0) 782 cv_wait(&vp->v_cv, &vp->v_interlock); 783 } 784 785 /* 786 * Insert a marker vnode into a mount's vnode list, after the 787 * specified vnode. mntvnode_lock must be held. 788 */ 789 void 790 vmark(vnode_t *mvp, vnode_t *vp) 791 { 792 struct mount *mp; 793 794 mp = mvp->v_mount; 795 796 KASSERT(mutex_owned(&mntvnode_lock)); 797 KASSERT((mvp->v_iflag & VI_MARKER) != 0); 798 KASSERT(vp->v_mount == mp); 799 800 TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vp, mvp, v_mntvnodes); 801 } 802 803 /* 804 * Remove a marker vnode from a mount's vnode list, and return 805 * a pointer to the next vnode in the list. mntvnode_lock must 806 * be held. 807 */ 808 vnode_t * 809 vunmark(vnode_t *mvp) 810 { 811 vnode_t *vp; 812 struct mount *mp; 813 814 mp = mvp->v_mount; 815 816 KASSERT(mutex_owned(&mntvnode_lock)); 817 KASSERT((mvp->v_iflag & VI_MARKER) != 0); 818 819 vp = TAILQ_NEXT(mvp, v_mntvnodes); 820 TAILQ_REMOVE(&mp->mnt_vnodelist, mvp, v_mntvnodes); 821 822 KASSERT(vp == NULL || vp->v_mount == mp); 823 824 return vp; 825 } 826 827 /* 828 * Update outstanding I/O count and do wakeup if requested. 829 */ 830 void 831 vwakeup(struct buf *bp) 832 { 833 struct vnode *vp; 834 835 if ((vp = bp->b_vp) == NULL) 836 return; 837 838 KASSERT(bp->b_objlock == &vp->v_interlock); 839 KASSERT(mutex_owned(bp->b_objlock)); 840 841 if (--vp->v_numoutput < 0) 842 panic("vwakeup: neg numoutput, vp %p", vp); 843 if (vp->v_numoutput == 0) 844 cv_broadcast(&vp->v_cv); 845 } 846 847 /* 848 * Flush out and invalidate all buffers associated with a vnode. 849 * Called with the underlying vnode locked, which should prevent new dirty 850 * buffers from being queued. 851 */ 852 int 853 vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l, 854 bool catch, int slptimeo) 855 { 856 struct buf *bp, *nbp; 857 int error; 858 int flushflags = PGO_ALLPAGES | PGO_FREE | PGO_SYNCIO | 859 (flags & V_SAVE ? PGO_CLEANIT | PGO_RECLAIM : 0); 860 861 /* XXXUBC this doesn't look at flags or slp* */ 862 mutex_enter(&vp->v_interlock); 863 error = VOP_PUTPAGES(vp, 0, 0, flushflags); 864 if (error) { 865 return error; 866 } 867 868 if (flags & V_SAVE) { 869 error = VOP_FSYNC(vp, cred, FSYNC_WAIT|FSYNC_RECLAIM, 0, 0); 870 if (error) 871 return (error); 872 KASSERT(LIST_EMPTY(&vp->v_dirtyblkhd)); 873 } 874 875 mutex_enter(&bufcache_lock); 876 restart: 877 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 878 nbp = LIST_NEXT(bp, b_vnbufs); 879 error = bbusy(bp, catch, slptimeo, NULL); 880 if (error != 0) { 881 if (error == EPASSTHROUGH) 882 goto restart; 883 mutex_exit(&bufcache_lock); 884 return (error); 885 } 886 brelsel(bp, BC_INVAL | BC_VFLUSH); 887 } 888 889 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 890 nbp = LIST_NEXT(bp, b_vnbufs); 891 error = bbusy(bp, catch, slptimeo, NULL); 892 if (error != 0) { 893 if (error == EPASSTHROUGH) 894 goto restart; 895 mutex_exit(&bufcache_lock); 896 return (error); 897 } 898 /* 899 * XXX Since there are no node locks for NFS, I believe 900 * there is a slight chance that a delayed write will 901 * occur while sleeping just above, so check for it. 902 */ 903 if ((bp->b_oflags & BO_DELWRI) && (flags & V_SAVE)) { 904 #ifdef DEBUG 905 printf("buffer still DELWRI\n"); 906 #endif 907 bp->b_cflags |= BC_BUSY | BC_VFLUSH; 908 mutex_exit(&bufcache_lock); 909 VOP_BWRITE(bp); 910 mutex_enter(&bufcache_lock); 911 goto restart; 912 } 913 brelsel(bp, BC_INVAL | BC_VFLUSH); 914 } 915 916 #ifdef DIAGNOSTIC 917 if (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd)) 918 panic("vinvalbuf: flush failed, vp %p", vp); 919 #endif 920 921 mutex_exit(&bufcache_lock); 922 923 return (0); 924 } 925 926 /* 927 * Destroy any in core blocks past the truncation length. 928 * Called with the underlying vnode locked, which should prevent new dirty 929 * buffers from being queued. 930 */ 931 int 932 vtruncbuf(struct vnode *vp, daddr_t lbn, bool catch, int slptimeo) 933 { 934 struct buf *bp, *nbp; 935 int error; 936 voff_t off; 937 938 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift); 939 mutex_enter(&vp->v_interlock); 940 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO); 941 if (error) { 942 return error; 943 } 944 945 mutex_enter(&bufcache_lock); 946 restart: 947 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 948 nbp = LIST_NEXT(bp, b_vnbufs); 949 if (bp->b_lblkno < lbn) 950 continue; 951 error = bbusy(bp, catch, slptimeo, NULL); 952 if (error != 0) { 953 if (error == EPASSTHROUGH) 954 goto restart; 955 mutex_exit(&bufcache_lock); 956 return (error); 957 } 958 brelsel(bp, BC_INVAL | BC_VFLUSH); 959 } 960 961 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 962 nbp = LIST_NEXT(bp, b_vnbufs); 963 if (bp->b_lblkno < lbn) 964 continue; 965 error = bbusy(bp, catch, slptimeo, NULL); 966 if (error != 0) { 967 if (error == EPASSTHROUGH) 968 goto restart; 969 mutex_exit(&bufcache_lock); 970 return (error); 971 } 972 brelsel(bp, BC_INVAL | BC_VFLUSH); 973 } 974 mutex_exit(&bufcache_lock); 975 976 return (0); 977 } 978 979 /* 980 * Flush all dirty buffers from a vnode. 981 * Called with the underlying vnode locked, which should prevent new dirty 982 * buffers from being queued. 983 */ 984 void 985 vflushbuf(struct vnode *vp, int sync) 986 { 987 struct buf *bp, *nbp; 988 int flags = PGO_CLEANIT | PGO_ALLPAGES | (sync ? PGO_SYNCIO : 0); 989 bool dirty; 990 991 mutex_enter(&vp->v_interlock); 992 (void) VOP_PUTPAGES(vp, 0, 0, flags); 993 994 loop: 995 mutex_enter(&bufcache_lock); 996 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 997 nbp = LIST_NEXT(bp, b_vnbufs); 998 if ((bp->b_cflags & BC_BUSY)) 999 continue; 1000 if ((bp->b_oflags & BO_DELWRI) == 0) 1001 panic("vflushbuf: not dirty, bp %p", bp); 1002 bp->b_cflags |= BC_BUSY | BC_VFLUSH; 1003 mutex_exit(&bufcache_lock); 1004 /* 1005 * Wait for I/O associated with indirect blocks to complete, 1006 * since there is no way to quickly wait for them below. 1007 */ 1008 if (bp->b_vp == vp || sync == 0) 1009 (void) bawrite(bp); 1010 else 1011 (void) bwrite(bp); 1012 goto loop; 1013 } 1014 mutex_exit(&bufcache_lock); 1015 1016 if (sync == 0) 1017 return; 1018 1019 mutex_enter(&vp->v_interlock); 1020 while (vp->v_numoutput != 0) 1021 cv_wait(&vp->v_cv, &vp->v_interlock); 1022 dirty = !LIST_EMPTY(&vp->v_dirtyblkhd); 1023 mutex_exit(&vp->v_interlock); 1024 1025 if (dirty) { 1026 vprint("vflushbuf: dirty", vp); 1027 goto loop; 1028 } 1029 } 1030 1031 /* 1032 * Create a vnode for a block device. 1033 * Used for root filesystem and swap areas. 1034 * Also used for memory file system special devices. 1035 */ 1036 int 1037 bdevvp(dev_t dev, vnode_t **vpp) 1038 { 1039 1040 return (getdevvp(dev, vpp, VBLK)); 1041 } 1042 1043 /* 1044 * Create a vnode for a character device. 1045 * Used for kernfs and some console handling. 1046 */ 1047 int 1048 cdevvp(dev_t dev, vnode_t **vpp) 1049 { 1050 1051 return (getdevvp(dev, vpp, VCHR)); 1052 } 1053 1054 /* 1055 * Associate a buffer with a vnode. There must already be a hold on 1056 * the vnode. 1057 */ 1058 void 1059 bgetvp(struct vnode *vp, struct buf *bp) 1060 { 1061 1062 KASSERT(bp->b_vp == NULL); 1063 KASSERT(bp->b_objlock == &buffer_lock); 1064 KASSERT(mutex_owned(&vp->v_interlock)); 1065 KASSERT(mutex_owned(&bufcache_lock)); 1066 KASSERT((bp->b_cflags & BC_BUSY) != 0); 1067 KASSERT(!cv_has_waiters(&bp->b_done)); 1068 1069 vholdl(vp); 1070 bp->b_vp = vp; 1071 if (vp->v_type == VBLK || vp->v_type == VCHR) 1072 bp->b_dev = vp->v_rdev; 1073 else 1074 bp->b_dev = NODEV; 1075 1076 /* 1077 * Insert onto list for new vnode. 1078 */ 1079 bufinsvn(bp, &vp->v_cleanblkhd); 1080 bp->b_objlock = &vp->v_interlock; 1081 } 1082 1083 /* 1084 * Disassociate a buffer from a vnode. 1085 */ 1086 void 1087 brelvp(struct buf *bp) 1088 { 1089 struct vnode *vp = bp->b_vp; 1090 1091 KASSERT(vp != NULL); 1092 KASSERT(bp->b_objlock == &vp->v_interlock); 1093 KASSERT(mutex_owned(&vp->v_interlock)); 1094 KASSERT(mutex_owned(&bufcache_lock)); 1095 KASSERT((bp->b_cflags & BC_BUSY) != 0); 1096 KASSERT(!cv_has_waiters(&bp->b_done)); 1097 1098 /* 1099 * Delete from old vnode list, if on one. 1100 */ 1101 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 1102 bufremvn(bp); 1103 1104 if (TAILQ_EMPTY(&vp->v_uobj.memq) && (vp->v_iflag & VI_ONWORKLST) && 1105 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) { 1106 vp->v_iflag &= ~VI_WRMAPDIRTY; 1107 vn_syncer_remove_from_worklist(vp); 1108 } 1109 1110 bp->b_objlock = &buffer_lock; 1111 bp->b_vp = NULL; 1112 holdrelel(vp); 1113 } 1114 1115 /* 1116 * Reassign a buffer from one vnode list to another. 1117 * The list reassignment must be within the same vnode. 1118 * Used to assign file specific control information 1119 * (indirect blocks) to the list to which they belong. 1120 */ 1121 void 1122 reassignbuf(struct buf *bp, struct vnode *vp) 1123 { 1124 struct buflists *listheadp; 1125 int delayx; 1126 1127 KASSERT(mutex_owned(&bufcache_lock)); 1128 KASSERT(bp->b_objlock == &vp->v_interlock); 1129 KASSERT(mutex_owned(&vp->v_interlock)); 1130 KASSERT((bp->b_cflags & BC_BUSY) != 0); 1131 1132 /* 1133 * Delete from old vnode list, if on one. 1134 */ 1135 if (LIST_NEXT(bp, b_vnbufs) != NOLIST) 1136 bufremvn(bp); 1137 1138 /* 1139 * If dirty, put on list of dirty buffers; 1140 * otherwise insert onto list of clean buffers. 1141 */ 1142 if ((bp->b_oflags & BO_DELWRI) == 0) { 1143 listheadp = &vp->v_cleanblkhd; 1144 if (TAILQ_EMPTY(&vp->v_uobj.memq) && 1145 (vp->v_iflag & VI_ONWORKLST) && 1146 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) { 1147 vp->v_iflag &= ~VI_WRMAPDIRTY; 1148 vn_syncer_remove_from_worklist(vp); 1149 } 1150 } else { 1151 listheadp = &vp->v_dirtyblkhd; 1152 if ((vp->v_iflag & VI_ONWORKLST) == 0) { 1153 switch (vp->v_type) { 1154 case VDIR: 1155 delayx = dirdelay; 1156 break; 1157 case VBLK: 1158 if (vp->v_specmountpoint != NULL) { 1159 delayx = metadelay; 1160 break; 1161 } 1162 /* fall through */ 1163 default: 1164 delayx = filedelay; 1165 break; 1166 } 1167 if (!vp->v_mount || 1168 (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) 1169 vn_syncer_add_to_worklist(vp, delayx); 1170 } 1171 } 1172 bufinsvn(bp, listheadp); 1173 } 1174 1175 /* 1176 * Create a vnode for a device. 1177 * Used by bdevvp (block device) for root file system etc., 1178 * and by cdevvp (character device) for console and kernfs. 1179 */ 1180 static int 1181 getdevvp(dev_t dev, vnode_t **vpp, enum vtype type) 1182 { 1183 vnode_t *vp; 1184 vnode_t *nvp; 1185 int error; 1186 1187 if (dev == NODEV) { 1188 *vpp = NULL; 1189 return (0); 1190 } 1191 error = getnewvnode(VT_NON, NULL, spec_vnodeop_p, &nvp); 1192 if (error) { 1193 *vpp = NULL; 1194 return (error); 1195 } 1196 vp = nvp; 1197 vp->v_type = type; 1198 vp->v_vflag |= VV_MPSAFE; 1199 uvm_vnp_setsize(vp, 0); 1200 spec_node_init(vp, dev); 1201 *vpp = vp; 1202 return (0); 1203 } 1204 1205 /* 1206 * Try to gain a reference to a vnode, without acquiring its interlock. 1207 * The caller must hold a lock that will prevent the vnode from being 1208 * recycled or freed. 1209 */ 1210 bool 1211 vtryget(vnode_t *vp) 1212 { 1213 u_int use, next; 1214 1215 /* 1216 * If the vnode is being freed, don't make life any harder 1217 * for vclean() by adding another reference without waiting. 1218 * This is not strictly necessary, but we'll do it anyway. 1219 */ 1220 if (__predict_false((vp->v_iflag & (VI_XLOCK | VI_FREEING)) != 0)) { 1221 return false; 1222 } 1223 for (use = vp->v_usecount;; use = next) { 1224 if (use == 0) { 1225 /* Need interlock held if first reference. */ 1226 return false; 1227 } 1228 next = atomic_cas_uint(&vp->v_usecount, use, use + 1); 1229 if (__predict_true(next == use)) { 1230 return true; 1231 } 1232 } 1233 } 1234 1235 /* 1236 * Grab a particular vnode from the free list, increment its 1237 * reference count and lock it. If the vnode lock bit is set the 1238 * vnode is being eliminated in vgone. In that case, we can not 1239 * grab the vnode, so the process is awakened when the transition is 1240 * completed, and an error returned to indicate that the vnode is no 1241 * longer usable (possibly having been changed to a new file system type). 1242 */ 1243 int 1244 vget(vnode_t *vp, int flags) 1245 { 1246 int error; 1247 1248 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1249 1250 if ((flags & LK_INTERLOCK) == 0) 1251 mutex_enter(&vp->v_interlock); 1252 1253 /* 1254 * Before adding a reference, we must remove the vnode 1255 * from its freelist. 1256 */ 1257 if (vp->v_usecount == 0) { 1258 vremfree(vp); 1259 vp->v_usecount = 1; 1260 } else { 1261 atomic_inc_uint(&vp->v_usecount); 1262 } 1263 1264 /* 1265 * If the vnode is in the process of being cleaned out for 1266 * another use, we wait for the cleaning to finish and then 1267 * return failure. Cleaning is determined by checking if 1268 * the VI_XLOCK or VI_FREEING flags are set. 1269 */ 1270 if ((vp->v_iflag & (VI_XLOCK | VI_FREEING)) != 0) { 1271 if ((flags & LK_NOWAIT) != 0) { 1272 vrelel(vp, 0); 1273 return EBUSY; 1274 } 1275 vwait(vp, VI_XLOCK | VI_FREEING); 1276 vrelel(vp, 0); 1277 return ENOENT; 1278 } 1279 if (flags & LK_TYPE_MASK) { 1280 error = vn_lock(vp, flags | LK_INTERLOCK); 1281 if (error != 0) { 1282 vrele(vp); 1283 } 1284 return error; 1285 } 1286 mutex_exit(&vp->v_interlock); 1287 return 0; 1288 } 1289 1290 /* 1291 * vput(), just unlock and vrele() 1292 */ 1293 void 1294 vput(vnode_t *vp) 1295 { 1296 1297 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1298 1299 VOP_UNLOCK(vp, 0); 1300 vrele(vp); 1301 } 1302 1303 /* 1304 * Try to drop reference on a vnode. Abort if we are releasing the 1305 * last reference. 1306 */ 1307 static inline bool 1308 vtryrele(vnode_t *vp) 1309 { 1310 u_int use, next; 1311 1312 for (use = vp->v_usecount;; use = next) { 1313 if (use == 1) { 1314 return false; 1315 } 1316 next = atomic_cas_uint(&vp->v_usecount, use, use - 1); 1317 if (__predict_true(next == use)) { 1318 return true; 1319 } 1320 } 1321 } 1322 1323 /* 1324 * Vnode release. If reference count drops to zero, call inactive 1325 * routine and either return to freelist or free to the pool. 1326 */ 1327 void 1328 vrelel(vnode_t *vp, int flags) 1329 { 1330 bool recycle, defer; 1331 int error; 1332 1333 KASSERT(mutex_owned(&vp->v_interlock)); 1334 KASSERT((vp->v_iflag & VI_MARKER) == 0); 1335 KASSERT(vp->v_freelisthd == NULL); 1336 1337 if (vp->v_op == dead_vnodeop_p && (vp->v_iflag & VI_CLEAN) == 0) { 1338 vpanic(vp, "dead but not clean"); 1339 } 1340 1341 /* 1342 * If not the last reference, just drop the reference count 1343 * and unlock. 1344 */ 1345 if (vtryrele(vp)) { 1346 vp->v_iflag |= VI_INACTREDO; 1347 mutex_exit(&vp->v_interlock); 1348 return; 1349 } 1350 if (vp->v_usecount <= 0 || vp->v_writecount != 0) { 1351 vpanic(vp, "vput: bad ref count"); 1352 } 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 vp->v_op = dead_vnodeop_p; 1845 vp->v_tag = VT_NON; 1846 mutex_enter(&vp->v_interlock); 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(&specfs_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(&specfs_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(&specfs_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(&specfs_lock); 1943 if (vget(vp, LK_INTERLOCK) == 0) { 1944 VOP_REVOKE(vp, REVOKEALL); 1945 vrele(vp); 1946 } 1947 mutex_enter(&specfs_lock); 1948 vp = *vpp; 1949 } 1950 } 1951 mutex_exit(&specfs_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(&specfs_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(&specfs_lock); 1968 return (count); 1969 } 1970 mutex_exit(&vp->v_interlock); 1971 count = vp->v_specnode->sn_dev->sd_opencnt; 1972 mutex_exit(&specfs_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(&specfs_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(&specfs_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(&specfs_lock); 2020 vq = *vpp; 2021 } 2022 mutex_exit(&specfs_lock); 2023 } 2024 2025 /* 2026 * sysctl helper routine to return list of supported fstypes 2027 */ 2028 static 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 * Top level filesystem related information gathering. 2086 */ 2087 SYSCTL_SETUP(sysctl_vfs_setup, "sysctl vfs subtree setup") 2088 { 2089 sysctl_createv(clog, 0, NULL, NULL, 2090 CTLFLAG_PERMANENT, 2091 CTLTYPE_NODE, "vfs", NULL, 2092 NULL, 0, NULL, 0, 2093 CTL_VFS, CTL_EOL); 2094 sysctl_createv(clog, 0, NULL, NULL, 2095 CTLFLAG_PERMANENT, 2096 CTLTYPE_NODE, "generic", 2097 SYSCTL_DESCR("Non-specific vfs related information"), 2098 NULL, 0, NULL, 0, 2099 CTL_VFS, VFS_GENERIC, CTL_EOL); 2100 sysctl_createv(clog, 0, NULL, NULL, 2101 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2102 CTLTYPE_INT, "usermount", 2103 SYSCTL_DESCR("Whether unprivileged users may mount " 2104 "filesystems"), 2105 NULL, 0, &dovfsusermount, 0, 2106 CTL_VFS, VFS_GENERIC, VFS_USERMOUNT, CTL_EOL); 2107 sysctl_createv(clog, 0, NULL, NULL, 2108 CTLFLAG_PERMANENT, 2109 CTLTYPE_STRING, "fstypes", 2110 SYSCTL_DESCR("List of file systems present"), 2111 sysctl_vfs_generic_fstypes, 0, NULL, 0, 2112 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL); 2113 sysctl_createv(clog, 0, NULL, NULL, 2114 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2115 CTLTYPE_INT, "magiclinks", 2116 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"), 2117 NULL, 0, &vfs_magiclinks, 0, 2118 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL); 2119 } 2120 2121 2122 int kinfo_vdebug = 1; 2123 int kinfo_vgetfailed; 2124 #define KINFO_VNODESLOP 10 2125 /* 2126 * Dump vnode list (via sysctl). 2127 * Copyout address of vnode followed by vnode. 2128 */ 2129 /* ARGSUSED */ 2130 int 2131 sysctl_kern_vnode(SYSCTLFN_ARGS) 2132 { 2133 char *where = oldp; 2134 size_t *sizep = oldlenp; 2135 struct mount *mp, *nmp; 2136 vnode_t *vp, *mvp, vbuf; 2137 char *bp = where, *savebp; 2138 char *ewhere; 2139 int error; 2140 2141 if (namelen != 0) 2142 return (EOPNOTSUPP); 2143 if (newp != NULL) 2144 return (EPERM); 2145 2146 #define VPTRSZ sizeof(vnode_t *) 2147 #define VNODESZ sizeof(vnode_t) 2148 if (where == NULL) { 2149 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ); 2150 return (0); 2151 } 2152 ewhere = where + *sizep; 2153 2154 sysctl_unlock(); 2155 mutex_enter(&mountlist_lock); 2156 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 2157 mp = nmp) { 2158 if (vfs_busy(mp, &nmp)) { 2159 continue; 2160 } 2161 savebp = bp; 2162 /* Allocate a marker vnode. */ 2163 if ((mvp = vnalloc(mp)) == NULL) { 2164 sysctl_relock(); 2165 return (ENOMEM); 2166 } 2167 mutex_enter(&mntvnode_lock); 2168 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) { 2169 vmark(mvp, vp); 2170 /* 2171 * Check that the vp is still associated with 2172 * this filesystem. RACE: could have been 2173 * recycled onto the same filesystem. 2174 */ 2175 if (vp->v_mount != mp || vismarker(vp)) 2176 continue; 2177 if (bp + VPTRSZ + VNODESZ > ewhere) { 2178 (void)vunmark(mvp); 2179 mutex_exit(&mntvnode_lock); 2180 vnfree(mvp); 2181 sysctl_relock(); 2182 *sizep = bp - where; 2183 return (ENOMEM); 2184 } 2185 memcpy(&vbuf, vp, VNODESZ); 2186 mutex_exit(&mntvnode_lock); 2187 if ((error = copyout(vp, bp, VPTRSZ)) || 2188 (error = copyout(&vbuf, bp + VPTRSZ, VNODESZ))) { 2189 mutex_enter(&mntvnode_lock); 2190 (void)vunmark(mvp); 2191 mutex_exit(&mntvnode_lock); 2192 vnfree(mvp); 2193 sysctl_relock(); 2194 return (error); 2195 } 2196 bp += VPTRSZ + VNODESZ; 2197 mutex_enter(&mntvnode_lock); 2198 } 2199 mutex_exit(&mntvnode_lock); 2200 vnfree(mvp); 2201 vfs_unbusy(mp, false, &nmp); 2202 } 2203 mutex_exit(&mountlist_lock); 2204 sysctl_relock(); 2205 2206 *sizep = bp - where; 2207 return (0); 2208 } 2209 2210 /* 2211 * Remove clean vnodes from a mountpoint's vnode list. 2212 */ 2213 void 2214 vfs_scrubvnlist(struct mount *mp) 2215 { 2216 vnode_t *vp, *nvp; 2217 2218 retry: 2219 mutex_enter(&mntvnode_lock); 2220 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) { 2221 nvp = TAILQ_NEXT(vp, v_mntvnodes); 2222 mutex_enter(&vp->v_interlock); 2223 if ((vp->v_iflag & VI_CLEAN) != 0) { 2224 TAILQ_REMOVE(&mp->mnt_vnodelist, vp, v_mntvnodes); 2225 vp->v_mount = NULL; 2226 mutex_exit(&mntvnode_lock); 2227 mutex_exit(&vp->v_interlock); 2228 vfs_destroy(mp); 2229 goto retry; 2230 } 2231 mutex_exit(&vp->v_interlock); 2232 } 2233 mutex_exit(&mntvnode_lock); 2234 } 2235 2236 /* 2237 * Check to see if a filesystem is mounted on a block device. 2238 */ 2239 int 2240 vfs_mountedon(vnode_t *vp) 2241 { 2242 vnode_t *vq; 2243 int error = 0; 2244 2245 if (vp->v_type != VBLK) 2246 return ENOTBLK; 2247 if (vp->v_specmountpoint != NULL) 2248 return (EBUSY); 2249 mutex_enter(&specfs_lock); 2250 for (vq = specfs_hash[SPECHASH(vp->v_rdev)]; vq != NULL; 2251 vq = vq->v_specnext) { 2252 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type) 2253 continue; 2254 if (vq->v_specmountpoint != NULL) { 2255 error = EBUSY; 2256 break; 2257 } 2258 } 2259 mutex_exit(&specfs_lock); 2260 return (error); 2261 } 2262 2263 /* 2264 * Unmount all file systems. 2265 * We traverse the list in reverse order under the assumption that doing so 2266 * will avoid needing to worry about dependencies. 2267 */ 2268 void 2269 vfs_unmountall(struct lwp *l) 2270 { 2271 struct mount *mp, *nmp; 2272 int allerror, error; 2273 2274 printf("unmounting file systems..."); 2275 for (allerror = 0, mp = CIRCLEQ_LAST(&mountlist); 2276 !CIRCLEQ_EMPTY(&mountlist); 2277 mp = nmp) { 2278 nmp = CIRCLEQ_PREV(mp, mnt_list); 2279 #ifdef DEBUG 2280 printf("\nunmounting %s (%s)...", 2281 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 2282 #endif 2283 atomic_inc_uint(&mp->mnt_refcnt); 2284 if ((error = dounmount(mp, MNT_FORCE, l)) != 0) { 2285 printf("unmount of %s failed with error %d\n", 2286 mp->mnt_stat.f_mntonname, error); 2287 allerror = 1; 2288 } 2289 } 2290 printf(" done\n"); 2291 if (allerror) 2292 printf("WARNING: some file systems would not unmount\n"); 2293 } 2294 2295 /* 2296 * Sync and unmount file systems before shutting down. 2297 */ 2298 void 2299 vfs_shutdown(void) 2300 { 2301 struct lwp *l; 2302 2303 /* XXX we're certainly not running in lwp0's context! */ 2304 l = curlwp; 2305 if (l == NULL) 2306 l = &lwp0; 2307 2308 printf("syncing disks... "); 2309 2310 /* remove user processes from run queue */ 2311 suspendsched(); 2312 (void) spl0(); 2313 2314 /* avoid coming back this way again if we panic. */ 2315 doing_shutdown = 1; 2316 2317 sys_sync(l, NULL, NULL); 2318 2319 /* Wait for sync to finish. */ 2320 if (buf_syncwait() != 0) { 2321 #if defined(DDB) && defined(DEBUG_HALT_BUSY) 2322 Debugger(); 2323 #endif 2324 printf("giving up\n"); 2325 return; 2326 } else 2327 printf("done\n"); 2328 2329 /* 2330 * If we've panic'd, don't make the situation potentially 2331 * worse by unmounting the file systems. 2332 */ 2333 if (panicstr != NULL) 2334 return; 2335 2336 /* Release inodes held by texts before update. */ 2337 #ifdef notdef 2338 vnshutdown(); 2339 #endif 2340 /* Unmount file systems. */ 2341 vfs_unmountall(l); 2342 } 2343 2344 /* 2345 * Mount the root file system. If the operator didn't specify a 2346 * file system to use, try all possible file systems until one 2347 * succeeds. 2348 */ 2349 int 2350 vfs_mountroot(void) 2351 { 2352 struct vfsops *v; 2353 int error = ENODEV; 2354 2355 if (root_device == NULL) 2356 panic("vfs_mountroot: root device unknown"); 2357 2358 switch (device_class(root_device)) { 2359 case DV_IFNET: 2360 if (rootdev != NODEV) 2361 panic("vfs_mountroot: rootdev set for DV_IFNET " 2362 "(0x%08x -> %d,%d)", rootdev, 2363 major(rootdev), minor(rootdev)); 2364 break; 2365 2366 case DV_DISK: 2367 if (rootdev == NODEV) 2368 panic("vfs_mountroot: rootdev not set for DV_DISK"); 2369 if (bdevvp(rootdev, &rootvp)) 2370 panic("vfs_mountroot: can't get vnode for rootdev"); 2371 error = VOP_OPEN(rootvp, FREAD, FSCRED); 2372 if (error) { 2373 printf("vfs_mountroot: can't open root device\n"); 2374 return (error); 2375 } 2376 break; 2377 2378 default: 2379 printf("%s: inappropriate for root file system\n", 2380 device_xname(root_device)); 2381 return (ENODEV); 2382 } 2383 2384 /* 2385 * If user specified a file system, use it. 2386 */ 2387 if (mountroot != NULL) { 2388 error = (*mountroot)(); 2389 goto done; 2390 } 2391 2392 /* 2393 * Try each file system currently configured into the kernel. 2394 */ 2395 mutex_enter(&vfs_list_lock); 2396 LIST_FOREACH(v, &vfs_list, vfs_list) { 2397 if (v->vfs_mountroot == NULL) 2398 continue; 2399 #ifdef DEBUG 2400 aprint_normal("mountroot: trying %s...\n", v->vfs_name); 2401 #endif 2402 v->vfs_refcount++; 2403 mutex_exit(&vfs_list_lock); 2404 error = (*v->vfs_mountroot)(); 2405 mutex_enter(&vfs_list_lock); 2406 v->vfs_refcount--; 2407 if (!error) { 2408 aprint_normal("root file system type: %s\n", 2409 v->vfs_name); 2410 break; 2411 } 2412 } 2413 mutex_exit(&vfs_list_lock); 2414 2415 if (v == NULL) { 2416 printf("no file system for %s", device_xname(root_device)); 2417 if (device_class(root_device) == DV_DISK) 2418 printf(" (dev 0x%x)", rootdev); 2419 printf("\n"); 2420 error = EFTYPE; 2421 } 2422 2423 done: 2424 if (error && device_class(root_device) == DV_DISK) { 2425 VOP_CLOSE(rootvp, FREAD, FSCRED); 2426 vrele(rootvp); 2427 } 2428 return (error); 2429 } 2430 2431 /* 2432 * Get a new unique fsid 2433 */ 2434 void 2435 vfs_getnewfsid(struct mount *mp) 2436 { 2437 static u_short xxxfs_mntid; 2438 fsid_t tfsid; 2439 int mtype; 2440 2441 mutex_enter(&mntid_lock); 2442 mtype = makefstype(mp->mnt_op->vfs_name); 2443 mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0); 2444 mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype; 2445 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 2446 if (xxxfs_mntid == 0) 2447 ++xxxfs_mntid; 2448 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid); 2449 tfsid.__fsid_val[1] = mtype; 2450 if (!CIRCLEQ_EMPTY(&mountlist)) { 2451 while (vfs_getvfs(&tfsid)) { 2452 tfsid.__fsid_val[0]++; 2453 xxxfs_mntid++; 2454 } 2455 } 2456 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0]; 2457 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 2458 mutex_exit(&mntid_lock); 2459 } 2460 2461 /* 2462 * Make a 'unique' number from a mount type name. 2463 */ 2464 long 2465 makefstype(const char *type) 2466 { 2467 long rv; 2468 2469 for (rv = 0; *type; type++) { 2470 rv <<= 2; 2471 rv ^= *type; 2472 } 2473 return rv; 2474 } 2475 2476 /* 2477 * Set vnode attributes to VNOVAL 2478 */ 2479 void 2480 vattr_null(struct vattr *vap) 2481 { 2482 2483 vap->va_type = VNON; 2484 2485 /* 2486 * Assign individually so that it is safe even if size and 2487 * sign of each member are varied. 2488 */ 2489 vap->va_mode = VNOVAL; 2490 vap->va_nlink = VNOVAL; 2491 vap->va_uid = VNOVAL; 2492 vap->va_gid = VNOVAL; 2493 vap->va_fsid = VNOVAL; 2494 vap->va_fileid = VNOVAL; 2495 vap->va_size = VNOVAL; 2496 vap->va_blocksize = VNOVAL; 2497 vap->va_atime.tv_sec = 2498 vap->va_mtime.tv_sec = 2499 vap->va_ctime.tv_sec = 2500 vap->va_birthtime.tv_sec = VNOVAL; 2501 vap->va_atime.tv_nsec = 2502 vap->va_mtime.tv_nsec = 2503 vap->va_ctime.tv_nsec = 2504 vap->va_birthtime.tv_nsec = VNOVAL; 2505 vap->va_gen = VNOVAL; 2506 vap->va_flags = VNOVAL; 2507 vap->va_rdev = VNOVAL; 2508 vap->va_bytes = VNOVAL; 2509 vap->va_vaflags = 0; 2510 } 2511 2512 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 2513 #define ARRAY_PRINT(idx, arr) \ 2514 ((idx) > 0 && (idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN") 2515 2516 const char * const vnode_tags[] = { VNODE_TAGS }; 2517 const char * const vnode_types[] = { VNODE_TYPES }; 2518 const char vnode_flagbits[] = VNODE_FLAGBITS; 2519 2520 /* 2521 * Print out a description of a vnode. 2522 */ 2523 void 2524 vprint(const char *label, struct vnode *vp) 2525 { 2526 struct vnlock *vl; 2527 char bf[96]; 2528 int flag; 2529 2530 vl = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock); 2531 flag = vp->v_iflag | vp->v_vflag | vp->v_uflag; 2532 bitmask_snprintf(flag, vnode_flagbits, bf, sizeof(bf)); 2533 2534 if (label != NULL) 2535 printf("%s: ", label); 2536 printf("vnode @ %p, flags (%s)\n\ttag %s(%d), type %s(%d), " 2537 "usecount %d, writecount %d, holdcount %d\n" 2538 "\tfreelisthd %p, mount %p, data %p lock %p recursecnt %d\n", 2539 vp, bf, ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag, 2540 ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type, 2541 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, 2542 vp->v_freelisthd, vp->v_mount, vp->v_data, vl, vl->vl_recursecnt); 2543 if (vp->v_data != NULL) { 2544 printf("\t"); 2545 VOP_PRINT(vp); 2546 } 2547 } 2548 2549 #ifdef DEBUG 2550 /* 2551 * List all of the locked vnodes in the system. 2552 * Called when debugging the kernel. 2553 */ 2554 void 2555 printlockedvnodes(void) 2556 { 2557 struct mount *mp, *nmp; 2558 struct vnode *vp; 2559 2560 printf("Locked vnodes\n"); 2561 mutex_enter(&mountlist_lock); 2562 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 2563 mp = nmp) { 2564 if (vfs_busy(mp, &nmp)) { 2565 continue; 2566 } 2567 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2568 if (VOP_ISLOCKED(vp)) 2569 vprint(NULL, vp); 2570 } 2571 mutex_enter(&mountlist_lock); 2572 vfs_unbusy(mp, false, &nmp); 2573 } 2574 mutex_exit(&mountlist_lock); 2575 } 2576 #endif 2577 2578 /* 2579 * Do the usual access checking. 2580 * file_mode, uid and gid are from the vnode in question, 2581 * while acc_mode and cred are from the VOP_ACCESS parameter list 2582 */ 2583 int 2584 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid, 2585 mode_t acc_mode, kauth_cred_t cred) 2586 { 2587 mode_t mask; 2588 int error, ismember; 2589 2590 /* 2591 * Super-user always gets read/write access, but execute access depends 2592 * on at least one execute bit being set. 2593 */ 2594 if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0) { 2595 if ((acc_mode & VEXEC) && type != VDIR && 2596 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) 2597 return (EACCES); 2598 return (0); 2599 } 2600 2601 mask = 0; 2602 2603 /* Otherwise, check the owner. */ 2604 if (kauth_cred_geteuid(cred) == uid) { 2605 if (acc_mode & VEXEC) 2606 mask |= S_IXUSR; 2607 if (acc_mode & VREAD) 2608 mask |= S_IRUSR; 2609 if (acc_mode & VWRITE) 2610 mask |= S_IWUSR; 2611 return ((file_mode & mask) == mask ? 0 : EACCES); 2612 } 2613 2614 /* Otherwise, check the groups. */ 2615 error = kauth_cred_ismember_gid(cred, gid, &ismember); 2616 if (error) 2617 return (error); 2618 if (kauth_cred_getegid(cred) == gid || ismember) { 2619 if (acc_mode & VEXEC) 2620 mask |= S_IXGRP; 2621 if (acc_mode & VREAD) 2622 mask |= S_IRGRP; 2623 if (acc_mode & VWRITE) 2624 mask |= S_IWGRP; 2625 return ((file_mode & mask) == mask ? 0 : EACCES); 2626 } 2627 2628 /* Otherwise, check everyone else. */ 2629 if (acc_mode & VEXEC) 2630 mask |= S_IXOTH; 2631 if (acc_mode & VREAD) 2632 mask |= S_IROTH; 2633 if (acc_mode & VWRITE) 2634 mask |= S_IWOTH; 2635 return ((file_mode & mask) == mask ? 0 : EACCES); 2636 } 2637 2638 /* 2639 * Given a file system name, look up the vfsops for that 2640 * file system, or return NULL if file system isn't present 2641 * in the kernel. 2642 */ 2643 struct vfsops * 2644 vfs_getopsbyname(const char *name) 2645 { 2646 struct vfsops *v; 2647 2648 mutex_enter(&vfs_list_lock); 2649 LIST_FOREACH(v, &vfs_list, vfs_list) { 2650 if (strcmp(v->vfs_name, name) == 0) 2651 break; 2652 } 2653 if (v != NULL) 2654 v->vfs_refcount++; 2655 mutex_exit(&vfs_list_lock); 2656 2657 return (v); 2658 } 2659 2660 void 2661 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp) 2662 { 2663 const struct statvfs *mbp; 2664 2665 if (sbp == (mbp = &mp->mnt_stat)) 2666 return; 2667 2668 (void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx)); 2669 sbp->f_fsid = mbp->f_fsid; 2670 sbp->f_owner = mbp->f_owner; 2671 sbp->f_flag = mbp->f_flag; 2672 sbp->f_syncwrites = mbp->f_syncwrites; 2673 sbp->f_asyncwrites = mbp->f_asyncwrites; 2674 sbp->f_syncreads = mbp->f_syncreads; 2675 sbp->f_asyncreads = mbp->f_asyncreads; 2676 (void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare)); 2677 (void)memcpy(sbp->f_fstypename, mbp->f_fstypename, 2678 sizeof(sbp->f_fstypename)); 2679 (void)memcpy(sbp->f_mntonname, mbp->f_mntonname, 2680 sizeof(sbp->f_mntonname)); 2681 (void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, 2682 sizeof(sbp->f_mntfromname)); 2683 sbp->f_namemax = mbp->f_namemax; 2684 } 2685 2686 int 2687 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom, 2688 const char *vfsname, struct mount *mp, struct lwp *l) 2689 { 2690 int error; 2691 size_t size; 2692 struct statvfs *sfs = &mp->mnt_stat; 2693 int (*fun)(const void *, void *, size_t, size_t *); 2694 2695 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsname, 2696 sizeof(mp->mnt_stat.f_fstypename)); 2697 2698 if (onp) { 2699 struct cwdinfo *cwdi = l->l_proc->p_cwdi; 2700 fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr; 2701 if (cwdi->cwdi_rdir != NULL) { 2702 size_t len; 2703 char *bp; 2704 char *path = PNBUF_GET(); 2705 2706 bp = path + MAXPATHLEN; 2707 *--bp = '\0'; 2708 rw_enter(&cwdi->cwdi_lock, RW_READER); 2709 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, 2710 path, MAXPATHLEN / 2, 0, l); 2711 rw_exit(&cwdi->cwdi_lock); 2712 if (error) { 2713 PNBUF_PUT(path); 2714 return error; 2715 } 2716 2717 len = strlen(bp); 2718 if (len > sizeof(sfs->f_mntonname) - 1) 2719 len = sizeof(sfs->f_mntonname) - 1; 2720 (void)strncpy(sfs->f_mntonname, bp, len); 2721 PNBUF_PUT(path); 2722 2723 if (len < sizeof(sfs->f_mntonname) - 1) { 2724 error = (*fun)(onp, &sfs->f_mntonname[len], 2725 sizeof(sfs->f_mntonname) - len - 1, &size); 2726 if (error) 2727 return error; 2728 size += len; 2729 } else { 2730 size = len; 2731 } 2732 } else { 2733 error = (*fun)(onp, &sfs->f_mntonname, 2734 sizeof(sfs->f_mntonname) - 1, &size); 2735 if (error) 2736 return error; 2737 } 2738 (void)memset(sfs->f_mntonname + size, 0, 2739 sizeof(sfs->f_mntonname) - size); 2740 } 2741 2742 if (fromp) { 2743 fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr; 2744 error = (*fun)(fromp, sfs->f_mntfromname, 2745 sizeof(sfs->f_mntfromname) - 1, &size); 2746 if (error) 2747 return error; 2748 (void)memset(sfs->f_mntfromname + size, 0, 2749 sizeof(sfs->f_mntfromname) - size); 2750 } 2751 return 0; 2752 } 2753 2754 void 2755 vfs_timestamp(struct timespec *ts) 2756 { 2757 2758 nanotime(ts); 2759 } 2760 2761 time_t rootfstime; /* recorded root fs time, if known */ 2762 void 2763 setrootfstime(time_t t) 2764 { 2765 rootfstime = t; 2766 } 2767 2768 /* 2769 * Sham lock manager for vnodes. This is a temporary measure. 2770 */ 2771 int 2772 vlockmgr(struct vnlock *vl, int flags) 2773 { 2774 2775 KASSERT((flags & ~(LK_CANRECURSE | LK_NOWAIT | LK_TYPE_MASK)) == 0); 2776 2777 switch (flags & LK_TYPE_MASK) { 2778 case LK_SHARED: 2779 if (rw_tryenter(&vl->vl_lock, RW_READER)) { 2780 return 0; 2781 } 2782 if ((flags & LK_NOWAIT) != 0) { 2783 return EBUSY; 2784 } 2785 rw_enter(&vl->vl_lock, RW_READER); 2786 return 0; 2787 2788 case LK_EXCLUSIVE: 2789 if (rw_tryenter(&vl->vl_lock, RW_WRITER)) { 2790 return 0; 2791 } 2792 if ((vl->vl_canrecurse || (flags & LK_CANRECURSE) != 0) && 2793 rw_write_held(&vl->vl_lock)) { 2794 vl->vl_recursecnt++; 2795 return 0; 2796 } 2797 if ((flags & LK_NOWAIT) != 0) { 2798 return EBUSY; 2799 } 2800 rw_enter(&vl->vl_lock, RW_WRITER); 2801 return 0; 2802 2803 case LK_RELEASE: 2804 if (vl->vl_recursecnt != 0) { 2805 KASSERT(rw_write_held(&vl->vl_lock)); 2806 vl->vl_recursecnt--; 2807 return 0; 2808 } 2809 rw_exit(&vl->vl_lock); 2810 return 0; 2811 2812 default: 2813 panic("vlockmgr: flags %x", flags); 2814 } 2815 } 2816 2817 int 2818 vlockstatus(struct vnlock *vl) 2819 { 2820 2821 if (rw_write_held(&vl->vl_lock)) { 2822 return LK_EXCLUSIVE; 2823 } 2824 if (rw_read_held(&vl->vl_lock)) { 2825 return LK_SHARED; 2826 } 2827 return 0; 2828 } 2829 2830 /* 2831 * mount_specific_key_create -- 2832 * Create a key for subsystem mount-specific data. 2833 */ 2834 int 2835 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 2836 { 2837 2838 return (specificdata_key_create(mount_specificdata_domain, keyp, dtor)); 2839 } 2840 2841 /* 2842 * mount_specific_key_delete -- 2843 * Delete a key for subsystem mount-specific data. 2844 */ 2845 void 2846 mount_specific_key_delete(specificdata_key_t key) 2847 { 2848 2849 specificdata_key_delete(mount_specificdata_domain, key); 2850 } 2851 2852 /* 2853 * mount_initspecific -- 2854 * Initialize a mount's specificdata container. 2855 */ 2856 void 2857 mount_initspecific(struct mount *mp) 2858 { 2859 int error; 2860 2861 error = specificdata_init(mount_specificdata_domain, 2862 &mp->mnt_specdataref); 2863 KASSERT(error == 0); 2864 } 2865 2866 /* 2867 * mount_finispecific -- 2868 * Finalize a mount's specificdata container. 2869 */ 2870 void 2871 mount_finispecific(struct mount *mp) 2872 { 2873 2874 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 2875 } 2876 2877 /* 2878 * mount_getspecific -- 2879 * Return mount-specific data corresponding to the specified key. 2880 */ 2881 void * 2882 mount_getspecific(struct mount *mp, specificdata_key_t key) 2883 { 2884 2885 return (specificdata_getspecific(mount_specificdata_domain, 2886 &mp->mnt_specdataref, key)); 2887 } 2888 2889 /* 2890 * mount_setspecific -- 2891 * Set mount-specific data corresponding to the specified key. 2892 */ 2893 void 2894 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data) 2895 { 2896 2897 specificdata_setspecific(mount_specificdata_domain, 2898 &mp->mnt_specdataref, key, data); 2899 } 2900 2901 int 2902 VFS_MOUNT(struct mount *mp, const char *a, void *b, size_t *c) 2903 { 2904 int error; 2905 2906 KERNEL_LOCK(1, NULL); 2907 error = (*(mp->mnt_op->vfs_mount))(mp, a, b, c); 2908 KERNEL_UNLOCK_ONE(NULL); 2909 2910 return error; 2911 } 2912 2913 int 2914 VFS_START(struct mount *mp, int a) 2915 { 2916 int error; 2917 2918 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2919 KERNEL_LOCK(1, NULL); 2920 } 2921 error = (*(mp->mnt_op->vfs_start))(mp, a); 2922 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2923 KERNEL_UNLOCK_ONE(NULL); 2924 } 2925 2926 return error; 2927 } 2928 2929 int 2930 VFS_UNMOUNT(struct mount *mp, int a) 2931 { 2932 int error; 2933 2934 KERNEL_LOCK(1, NULL); 2935 error = (*(mp->mnt_op->vfs_unmount))(mp, a); 2936 KERNEL_UNLOCK_ONE(NULL); 2937 2938 return error; 2939 } 2940 2941 int 2942 VFS_ROOT(struct mount *mp, struct vnode **a) 2943 { 2944 int error; 2945 2946 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2947 KERNEL_LOCK(1, NULL); 2948 } 2949 error = (*(mp->mnt_op->vfs_root))(mp, a); 2950 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2951 KERNEL_UNLOCK_ONE(NULL); 2952 } 2953 2954 return error; 2955 } 2956 2957 int 2958 VFS_QUOTACTL(struct mount *mp, int a, uid_t b, void *c) 2959 { 2960 int error; 2961 2962 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2963 KERNEL_LOCK(1, NULL); 2964 } 2965 error = (*(mp->mnt_op->vfs_quotactl))(mp, a, b, c); 2966 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2967 KERNEL_UNLOCK_ONE(NULL); 2968 } 2969 2970 return error; 2971 } 2972 2973 int 2974 VFS_STATVFS(struct mount *mp, struct statvfs *a) 2975 { 2976 int error; 2977 2978 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2979 KERNEL_LOCK(1, NULL); 2980 } 2981 error = (*(mp->mnt_op->vfs_statvfs))(mp, a); 2982 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2983 KERNEL_UNLOCK_ONE(NULL); 2984 } 2985 2986 return error; 2987 } 2988 2989 int 2990 VFS_SYNC(struct mount *mp, int a, struct kauth_cred *b) 2991 { 2992 int error; 2993 2994 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2995 KERNEL_LOCK(1, NULL); 2996 } 2997 error = (*(mp->mnt_op->vfs_sync))(mp, a, b); 2998 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 2999 KERNEL_UNLOCK_ONE(NULL); 3000 } 3001 3002 return error; 3003 } 3004 3005 int 3006 VFS_FHTOVP(struct mount *mp, struct fid *a, struct vnode **b) 3007 { 3008 int error; 3009 3010 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3011 KERNEL_LOCK(1, NULL); 3012 } 3013 error = (*(mp->mnt_op->vfs_fhtovp))(mp, a, b); 3014 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3015 KERNEL_UNLOCK_ONE(NULL); 3016 } 3017 3018 return error; 3019 } 3020 3021 int 3022 VFS_VPTOFH(struct vnode *vp, struct fid *a, size_t *b) 3023 { 3024 int error; 3025 3026 if ((vp->v_vflag & VV_MPSAFE) == 0) { 3027 KERNEL_LOCK(1, NULL); 3028 } 3029 error = (*(vp->v_mount->mnt_op->vfs_vptofh))(vp, a, b); 3030 if ((vp->v_vflag & VV_MPSAFE) == 0) { 3031 KERNEL_UNLOCK_ONE(NULL); 3032 } 3033 3034 return error; 3035 } 3036 3037 int 3038 VFS_SNAPSHOT(struct mount *mp, struct vnode *a, struct timespec *b) 3039 { 3040 int error; 3041 3042 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3043 KERNEL_LOCK(1, NULL); 3044 } 3045 error = (*(mp->mnt_op->vfs_snapshot))(mp, a, b); 3046 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3047 KERNEL_UNLOCK_ONE(NULL); 3048 } 3049 3050 return error; 3051 } 3052 3053 int 3054 VFS_EXTATTRCTL(struct mount *mp, int a, struct vnode *b, int c, const char *d) 3055 { 3056 int error; 3057 3058 KERNEL_LOCK(1, NULL); /* XXXSMP check ffs */ 3059 error = (*(mp->mnt_op->vfs_extattrctl))(mp, a, b, c, d); 3060 KERNEL_UNLOCK_ONE(NULL); /* XXX */ 3061 3062 return error; 3063 } 3064 3065 int 3066 VFS_SUSPENDCTL(struct mount *mp, int a) 3067 { 3068 int error; 3069 3070 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3071 KERNEL_LOCK(1, NULL); 3072 } 3073 error = (*(mp->mnt_op->vfs_suspendctl))(mp, a); 3074 if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) { 3075 KERNEL_UNLOCK_ONE(NULL); 3076 } 3077 3078 return error; 3079 } 3080 3081 #ifdef DDB 3082 static const char buf_flagbits[] = BUF_FLAGBITS; 3083 3084 void 3085 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...)) 3086 { 3087 char bf[1024]; 3088 3089 (*pr)(" vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" rawblkno 0x%" 3090 PRIx64 " dev 0x%x\n", 3091 bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_rawblkno, bp->b_dev); 3092 3093 bitmask_snprintf(bp->b_flags | bp->b_oflags | bp->b_cflags, 3094 buf_flagbits, bf, sizeof(bf)); 3095 (*pr)(" error %d flags 0x%s\n", bp->b_error, bf); 3096 3097 (*pr)(" bufsize 0x%lx bcount 0x%lx resid 0x%lx\n", 3098 bp->b_bufsize, bp->b_bcount, bp->b_resid); 3099 (*pr)(" data %p saveaddr %p dep %p\n", 3100 bp->b_data, bp->b_saveaddr, LIST_FIRST(&bp->b_dep)); 3101 (*pr)(" iodone %p objlock %p\n", bp->b_iodone, bp->b_objlock); 3102 } 3103 3104 3105 void 3106 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...)) 3107 { 3108 char bf[256]; 3109 3110 uvm_object_printit(&vp->v_uobj, full, pr); 3111 bitmask_snprintf(vp->v_iflag | vp->v_vflag | vp->v_uflag, 3112 vnode_flagbits, bf, sizeof(bf)); 3113 (*pr)("\nVNODE flags %s\n", bf); 3114 (*pr)("mp %p numoutput %d size 0x%llx writesize 0x%llx\n", 3115 vp->v_mount, vp->v_numoutput, vp->v_size, vp->v_writesize); 3116 3117 (*pr)("data %p writecount %ld holdcnt %ld\n", 3118 vp->v_data, vp->v_writecount, vp->v_holdcnt); 3119 3120 (*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n", 3121 ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag, 3122 ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type, 3123 vp->v_mount, vp->v_mountedhere); 3124 3125 (*pr)("v_lock %p v_vnlock %p\n", &vp->v_lock, vp->v_vnlock); 3126 3127 if (full) { 3128 struct buf *bp; 3129 3130 (*pr)("clean bufs:\n"); 3131 LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) { 3132 (*pr)(" bp %p\n", bp); 3133 vfs_buf_print(bp, full, pr); 3134 } 3135 3136 (*pr)("dirty bufs:\n"); 3137 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) { 3138 (*pr)(" bp %p\n", bp); 3139 vfs_buf_print(bp, full, pr); 3140 } 3141 } 3142 } 3143 3144 void 3145 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...)) 3146 { 3147 char sbuf[256]; 3148 3149 (*pr)("vnodecovered = %p syncer = %p data = %p\n", 3150 mp->mnt_vnodecovered,mp->mnt_syncer,mp->mnt_data); 3151 3152 (*pr)("fs_bshift %d dev_bshift = %d\n", 3153 mp->mnt_fs_bshift,mp->mnt_dev_bshift); 3154 3155 bitmask_snprintf(mp->mnt_flag, __MNT_FLAG_BITS, sbuf, sizeof(sbuf)); 3156 (*pr)("flag = %s\n", sbuf); 3157 3158 bitmask_snprintf(mp->mnt_iflag, __IMNT_FLAG_BITS, sbuf, sizeof(sbuf)); 3159 (*pr)("iflag = %s\n", sbuf); 3160 3161 (*pr)("refcnt = %d unmounting @ %p updating @ %p\n", mp->mnt_refcnt, 3162 &mp->mnt_unmounting, &mp->mnt_updating); 3163 3164 (*pr)("statvfs cache:\n"); 3165 (*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize); 3166 (*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize); 3167 (*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize); 3168 3169 (*pr)("\tblocks = %"PRIu64"\n",mp->mnt_stat.f_blocks); 3170 (*pr)("\tbfree = %"PRIu64"\n",mp->mnt_stat.f_bfree); 3171 (*pr)("\tbavail = %"PRIu64"\n",mp->mnt_stat.f_bavail); 3172 (*pr)("\tbresvd = %"PRIu64"\n",mp->mnt_stat.f_bresvd); 3173 3174 (*pr)("\tfiles = %"PRIu64"\n",mp->mnt_stat.f_files); 3175 (*pr)("\tffree = %"PRIu64"\n",mp->mnt_stat.f_ffree); 3176 (*pr)("\tfavail = %"PRIu64"\n",mp->mnt_stat.f_favail); 3177 (*pr)("\tfresvd = %"PRIu64"\n",mp->mnt_stat.f_fresvd); 3178 3179 (*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n", 3180 mp->mnt_stat.f_fsidx.__fsid_val[0], 3181 mp->mnt_stat.f_fsidx.__fsid_val[1]); 3182 3183 (*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner); 3184 (*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax); 3185 3186 bitmask_snprintf(mp->mnt_stat.f_flag, __MNT_FLAG_BITS, sbuf, 3187 sizeof(sbuf)); 3188 (*pr)("\tflag = %s\n",sbuf); 3189 (*pr)("\tsyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_syncwrites); 3190 (*pr)("\tasyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_asyncwrites); 3191 (*pr)("\tsyncreads = %" PRIu64 "\n",mp->mnt_stat.f_syncreads); 3192 (*pr)("\tasyncreads = %" PRIu64 "\n",mp->mnt_stat.f_asyncreads); 3193 (*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename); 3194 (*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname); 3195 (*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname); 3196 3197 { 3198 int cnt = 0; 3199 struct vnode *vp; 3200 (*pr)("locked vnodes ="); 3201 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 3202 if (VOP_ISLOCKED(vp)) { 3203 if ((++cnt % 6) == 0) { 3204 (*pr)(" %p,\n\t", vp); 3205 } else { 3206 (*pr)(" %p,", vp); 3207 } 3208 } 3209 } 3210 (*pr)("\n"); 3211 } 3212 3213 if (full) { 3214 int cnt = 0; 3215 struct vnode *vp; 3216 (*pr)("all vnodes ="); 3217 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 3218 if (!TAILQ_NEXT(vp, v_mntvnodes)) { 3219 (*pr)(" %p", vp); 3220 } else if ((++cnt % 6) == 0) { 3221 (*pr)(" %p,\n\t", vp); 3222 } else { 3223 (*pr)(" %p,", vp); 3224 } 3225 } 3226 (*pr)("\n", vp); 3227 } 3228 } 3229 #endif /* DDB */ 3230