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