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