vfs_subr.c revision 102210
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 39 * $FreeBSD: head/sys/kern/vfs_subr.c 102210 2002-08-21 06:19:29Z jeff $ 40 */ 41 42/* 43 * External virtual filesystem routines 44 */ 45#include "opt_ddb.h" 46#include "opt_mac.h" 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/bio.h> 51#include <sys/buf.h> 52#include <sys/conf.h> 53#include <sys/eventhandler.h> 54#include <sys/fcntl.h> 55#include <sys/kernel.h> 56#include <sys/kthread.h> 57#include <sys/mac.h> 58#include <sys/malloc.h> 59#include <sys/mount.h> 60#include <sys/namei.h> 61#include <sys/stat.h> 62#include <sys/sysctl.h> 63#include <sys/syslog.h> 64#include <sys/vmmeter.h> 65#include <sys/vnode.h> 66 67#include <vm/vm.h> 68#include <vm/vm_object.h> 69#include <vm/vm_extern.h> 70#include <vm/pmap.h> 71#include <vm/vm_map.h> 72#include <vm/vm_page.h> 73#include <vm/uma.h> 74 75static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); 76 77static void addalias(struct vnode *vp, dev_t nvp_rdev); 78static void insmntque(struct vnode *vp, struct mount *mp); 79static void vclean(struct vnode *vp, int flags, struct thread *td); 80static void vlruvp(struct vnode *vp); 81static int flushbuflist(struct buf *blist, int flags, struct vnode *vp, 82 int slpflag, int slptimeo, int *errorp); 83static int vcanrecycle(struct vnode *vp); 84 85 86/* 87 * Number of vnodes in existence. Increased whenever getnewvnode() 88 * allocates a new vnode, never decreased. 89 */ 90static unsigned long numvnodes; 91 92SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, ""); 93 94/* 95 * Conversion tables for conversion from vnode types to inode formats 96 * and back. 97 */ 98enum vtype iftovt_tab[16] = { 99 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 100 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 101}; 102int vttoif_tab[9] = { 103 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 104 S_IFSOCK, S_IFIFO, S_IFMT, 105}; 106 107/* 108 * List of vnodes that are ready for recycling. 109 */ 110static TAILQ_HEAD(freelst, vnode) vnode_free_list; 111 112/* 113 * Minimum number of free vnodes. If there are fewer than this free vnodes, 114 * getnewvnode() will return a newly allocated vnode. 115 */ 116static u_long wantfreevnodes = 25; 117SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 118/* Number of vnodes in the free list. */ 119static u_long freevnodes; 120SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, ""); 121 122/* 123 * Various variables used for debugging the new implementation of 124 * reassignbuf(). 125 * XXX these are probably of (very) limited utility now. 126 */ 127static int reassignbufcalls; 128SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, ""); 129static int nameileafonly; 130SYSCTL_INT(_vfs, OID_AUTO, nameileafonly, CTLFLAG_RW, &nameileafonly, 0, ""); 131 132#ifdef ENABLE_VFS_IOOPT 133/* See NOTES for a description of this setting. */ 134int vfs_ioopt; 135SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, ""); 136#endif 137 138/* 139 * Cache for the mount type id assigned to NFS. This is used for 140 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c. 141 */ 142int nfs_mount_type = -1; 143 144/* To keep more than one thread at a time from running vfs_getnewfsid */ 145static struct mtx mntid_mtx; 146 147/* 148 * Lock for any access to the following: 149 * vnode_free_list 150 * numvnodes 151 * freevnodes 152 */ 153static struct mtx vnode_free_list_mtx; 154 155/* 156 * For any iteration/modification of dev->si_hlist (linked through 157 * v_specnext) 158 */ 159static struct mtx spechash_mtx; 160 161/* Publicly exported FS */ 162struct nfs_public nfs_pub; 163 164/* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 165static uma_zone_t vnode_zone; 166static uma_zone_t vnodepoll_zone; 167 168/* Set to 1 to print out reclaim of active vnodes */ 169int prtactive; 170 171/* 172 * The workitem queue. 173 * 174 * It is useful to delay writes of file data and filesystem metadata 175 * for tens of seconds so that quickly created and deleted files need 176 * not waste disk bandwidth being created and removed. To realize this, 177 * we append vnodes to a "workitem" queue. When running with a soft 178 * updates implementation, most pending metadata dependencies should 179 * not wait for more than a few seconds. Thus, mounted on block devices 180 * are delayed only about a half the time that file data is delayed. 181 * Similarly, directory updates are more critical, so are only delayed 182 * about a third the time that file data is delayed. Thus, there are 183 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 184 * one each second (driven off the filesystem syncer process). The 185 * syncer_delayno variable indicates the next queue that is to be processed. 186 * Items that need to be processed soon are placed in this queue: 187 * 188 * syncer_workitem_pending[syncer_delayno] 189 * 190 * A delay of fifteen seconds is done by placing the request fifteen 191 * entries later in the queue: 192 * 193 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 194 * 195 */ 196static int syncer_delayno; 197static long syncer_mask; 198LIST_HEAD(synclist, vnode); 199static struct synclist *syncer_workitem_pending; 200 201#define SYNCER_MAXDELAY 32 202static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 203static int syncdelay = 30; /* max time to delay syncing data */ 204static int filedelay = 30; /* time to delay syncing files */ 205SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, ""); 206static int dirdelay = 29; /* time to delay syncing directories */ 207SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, ""); 208static int metadelay = 28; /* time to delay syncing metadata */ 209SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, ""); 210static int rushjob; /* number of slots to run ASAP */ 211static int stat_rush_requests; /* number of times I/O speeded up */ 212SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, ""); 213 214/* 215 * Number of vnodes we want to exist at any one time. This is mostly used 216 * to size hash tables in vnode-related code. It is normally not used in 217 * getnewvnode(), as wantfreevnodes is normally nonzero.) 218 * 219 * XXX desiredvnodes is historical cruft and should not exist. 220 */ 221int desiredvnodes; 222SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 223 &desiredvnodes, 0, "Maximum number of vnodes"); 224static int minvnodes; 225SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW, 226 &minvnodes, 0, "Minimum number of vnodes"); 227static int vnlru_nowhere; 228SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW, &vnlru_nowhere, 0, 229 "Number of times the vnlru process ran without success"); 230 231/* Hook for calling soft updates */ 232int (*softdep_process_worklist_hook)(struct mount *); 233 234#ifdef DEBUG_VFS_LOCKS 235/* Print lock violations */ 236int vfs_badlock_print = 1; 237 238/* Panic on violation */ 239int vfs_badlock_panic = 1; 240 241/* Check for interlock across VOPs */ 242int vfs_badlock_mutex = 0; 243 244void 245vop_rename_pre(void *ap) 246{ 247 struct vop_rename_args *a = ap; 248 249 if (a->a_tvp) 250 ASSERT_VI_UNLOCKED(a->a_tvp); 251 ASSERT_VI_UNLOCKED(a->a_tdvp); 252 ASSERT_VI_UNLOCKED(a->a_fvp); 253 ASSERT_VI_UNLOCKED(a->a_fdvp); 254 255 /* Check the source (from) */ 256 if (a->a_tdvp != a->a_fdvp) 257 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked.\n"); 258 if (a->a_tvp != a->a_fvp) 259 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked.\n"); 260 261 /* Check the target */ 262 if (a->a_tvp) 263 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked.\n"); 264 265 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked.\n"); 266} 267 268void 269vop_strategy_pre(void *ap) 270{ 271 struct vop_strategy_args *a = ap; 272 struct buf *bp; 273 274 bp = a->a_bp; 275 276 /* 277 * Cluster ops lock their component buffers but not the IO container. 278 */ 279 if ((bp->b_flags & B_CLUSTER) != 0) 280 return; 281 282 if (BUF_REFCNT(bp) < 1) { 283 if (vfs_badlock_print) 284 printf("VOP_STRATEGY: bp is not locked but should be.\n"); 285 if (vfs_badlock_panic) 286 Debugger("Lock violation.\n"); 287 } 288} 289 290void 291vop_lookup_pre(void *ap) 292{ 293 struct vop_lookup_args *a = ap; 294 struct vnode *dvp; 295 296 dvp = a->a_dvp; 297 298 ASSERT_VI_UNLOCKED(dvp); 299 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 300} 301 302void 303vop_lookup_post(void *ap, int rc) 304{ 305 struct vop_lookup_args *a = ap; 306 struct componentname *cnp; 307 struct vnode *dvp; 308 struct vnode *vp; 309 int flags; 310 311 dvp = a->a_dvp; 312 cnp = a->a_cnp; 313 vp = *(a->a_vpp); 314 flags = cnp->cn_flags; 315 316 317 ASSERT_VI_UNLOCKED(dvp); 318 /* 319 * If this is the last path component for this lookup and LOCPARENT 320 * is set, OR if there is an error the directory has to be locked. 321 */ 322 if ((flags & LOCKPARENT) && (flags & ISLASTCN)) 323 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)"); 324 else if (rc != 0) 325 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)"); 326 else if (dvp != vp) 327 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)"); 328 329 if (flags & PDIRUNLOCK) 330 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)"); 331 332 if (rc == 0) { 333 ASSERT_VI_UNLOCKED(vp); 334 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (vpp)"); 335 } 336} 337 338void 339vop_unlock_pre(void *ap) 340{ 341 struct vop_unlock_args *a = ap; 342 343 if ((a->a_flags & LK_INTERLOCK) == 0) 344 ASSERT_VI_UNLOCKED(a->a_vp); 345 else 346 ASSERT_VI_LOCKED(a->a_vp); 347} 348 349void 350vop_unlock_post(void *ap, int rc) 351{ 352 struct vop_unlock_args *a = ap; 353 354 ASSERT_VI_UNLOCKED(a->a_vp); 355} 356 357void 358vop_lock_pre(void *ap) 359{ 360 struct vop_lock_args *a = ap; 361 362 if ((a->a_flags & LK_INTERLOCK) == 0) 363 ASSERT_VI_UNLOCKED(a->a_vp); 364 else 365 ASSERT_VI_LOCKED(a->a_vp); 366} 367 368void 369vop_lock_post(void *ap, int rc) 370{ 371 struct vop_lock_args *a = ap; 372 373 ASSERT_VI_UNLOCKED(a->a_vp); 374 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 375} 376 377#endif /* DEBUG_VFS_LOCKS */ 378 379void 380v_addpollinfo(struct vnode *vp) 381{ 382 vp->v_pollinfo = uma_zalloc(vnodepoll_zone, M_WAITOK); 383 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 384} 385 386/* 387 * Initialize the vnode management data structures. 388 */ 389static void 390vntblinit(void *dummy __unused) 391{ 392 393 desiredvnodes = maxproc + cnt.v_page_count / 4; 394 minvnodes = desiredvnodes / 4; 395 mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF); 396 mtx_init(&mntvnode_mtx, "mntvnode", NULL, MTX_DEF); 397 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF); 398 mtx_init(&spechash_mtx, "spechash", NULL, MTX_DEF); 399 TAILQ_INIT(&vnode_free_list); 400 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF); 401 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL, 402 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 403 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo), 404 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 405 /* 406 * Initialize the filesystem syncer. 407 */ 408 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 409 &syncer_mask); 410 syncer_maxdelay = syncer_mask + 1; 411} 412SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL) 413 414 415/* 416 * Mark a mount point as busy. Used to synchronize access and to delay 417 * unmounting. Interlock is not released on failure. 418 */ 419int 420vfs_busy(mp, flags, interlkp, td) 421 struct mount *mp; 422 int flags; 423 struct mtx *interlkp; 424 struct thread *td; 425{ 426 int lkflags; 427 428 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 429 if (flags & LK_NOWAIT) 430 return (ENOENT); 431 mp->mnt_kern_flag |= MNTK_MWAIT; 432 /* 433 * Since all busy locks are shared except the exclusive 434 * lock granted when unmounting, the only place that a 435 * wakeup needs to be done is at the release of the 436 * exclusive lock at the end of dounmount. 437 */ 438 msleep(mp, interlkp, PVFS, "vfs_busy", 0); 439 return (ENOENT); 440 } 441 lkflags = LK_SHARED | LK_NOPAUSE; 442 if (interlkp) 443 lkflags |= LK_INTERLOCK; 444 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, td)) 445 panic("vfs_busy: unexpected lock failure"); 446 return (0); 447} 448 449/* 450 * Free a busy filesystem. 451 */ 452void 453vfs_unbusy(mp, td) 454 struct mount *mp; 455 struct thread *td; 456{ 457 458 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 459} 460 461/* 462 * Lookup a mount point by filesystem identifier. 463 */ 464struct mount * 465vfs_getvfs(fsid) 466 fsid_t *fsid; 467{ 468 register struct mount *mp; 469 470 mtx_lock(&mountlist_mtx); 471 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 472 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 473 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 474 mtx_unlock(&mountlist_mtx); 475 return (mp); 476 } 477 } 478 mtx_unlock(&mountlist_mtx); 479 return ((struct mount *) 0); 480} 481 482/* 483 * Get a new unique fsid. Try to make its val[0] unique, since this value 484 * will be used to create fake device numbers for stat(). Also try (but 485 * not so hard) make its val[0] unique mod 2^16, since some emulators only 486 * support 16-bit device numbers. We end up with unique val[0]'s for the 487 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 488 * 489 * Keep in mind that several mounts may be running in parallel. Starting 490 * the search one past where the previous search terminated is both a 491 * micro-optimization and a defense against returning the same fsid to 492 * different mounts. 493 */ 494void 495vfs_getnewfsid(mp) 496 struct mount *mp; 497{ 498 static u_int16_t mntid_base; 499 fsid_t tfsid; 500 int mtype; 501 502 mtx_lock(&mntid_mtx); 503 mtype = mp->mnt_vfc->vfc_typenum; 504 tfsid.val[1] = mtype; 505 mtype = (mtype & 0xFF) << 24; 506 for (;;) { 507 tfsid.val[0] = makeudev(255, 508 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 509 mntid_base++; 510 if (vfs_getvfs(&tfsid) == NULL) 511 break; 512 } 513 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 514 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 515 mtx_unlock(&mntid_mtx); 516} 517 518/* 519 * Knob to control the precision of file timestamps: 520 * 521 * 0 = seconds only; nanoseconds zeroed. 522 * 1 = seconds and nanoseconds, accurate within 1/HZ. 523 * 2 = seconds and nanoseconds, truncated to microseconds. 524 * >=3 = seconds and nanoseconds, maximum precision. 525 */ 526enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 527 528static int timestamp_precision = TSP_SEC; 529SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 530 ×tamp_precision, 0, ""); 531 532/* 533 * Get a current timestamp. 534 */ 535void 536vfs_timestamp(tsp) 537 struct timespec *tsp; 538{ 539 struct timeval tv; 540 541 switch (timestamp_precision) { 542 case TSP_SEC: 543 tsp->tv_sec = time_second; 544 tsp->tv_nsec = 0; 545 break; 546 case TSP_HZ: 547 getnanotime(tsp); 548 break; 549 case TSP_USEC: 550 microtime(&tv); 551 TIMEVAL_TO_TIMESPEC(&tv, tsp); 552 break; 553 case TSP_NSEC: 554 default: 555 nanotime(tsp); 556 break; 557 } 558} 559 560/* 561 * Set vnode attributes to VNOVAL 562 */ 563void 564vattr_null(vap) 565 register struct vattr *vap; 566{ 567 568 vap->va_type = VNON; 569 vap->va_size = VNOVAL; 570 vap->va_bytes = VNOVAL; 571 vap->va_mode = VNOVAL; 572 vap->va_nlink = VNOVAL; 573 vap->va_uid = VNOVAL; 574 vap->va_gid = VNOVAL; 575 vap->va_fsid = VNOVAL; 576 vap->va_fileid = VNOVAL; 577 vap->va_blocksize = VNOVAL; 578 vap->va_rdev = VNOVAL; 579 vap->va_atime.tv_sec = VNOVAL; 580 vap->va_atime.tv_nsec = VNOVAL; 581 vap->va_mtime.tv_sec = VNOVAL; 582 vap->va_mtime.tv_nsec = VNOVAL; 583 vap->va_ctime.tv_sec = VNOVAL; 584 vap->va_ctime.tv_nsec = VNOVAL; 585 vap->va_birthtime.tv_sec = VNOVAL; 586 vap->va_birthtime.tv_nsec = VNOVAL; 587 vap->va_flags = VNOVAL; 588 vap->va_gen = VNOVAL; 589 vap->va_vaflags = 0; 590} 591 592/* 593 * This routine is called when we have too many vnodes. It attempts 594 * to free <count> vnodes and will potentially free vnodes that still 595 * have VM backing store (VM backing store is typically the cause 596 * of a vnode blowout so we want to do this). Therefore, this operation 597 * is not considered cheap. 598 * 599 * A number of conditions may prevent a vnode from being reclaimed. 600 * the buffer cache may have references on the vnode, a directory 601 * vnode may still have references due to the namei cache representing 602 * underlying files, or the vnode may be in active use. It is not 603 * desireable to reuse such vnodes. These conditions may cause the 604 * number of vnodes to reach some minimum value regardless of what 605 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low. 606 */ 607static int 608vlrureclaim(struct mount *mp, int count) 609{ 610 struct vnode *vp; 611 int done; 612 int trigger; 613 int usevnodes; 614 615 /* 616 * Calculate the trigger point, don't allow user 617 * screwups to blow us up. This prevents us from 618 * recycling vnodes with lots of resident pages. We 619 * aren't trying to free memory, we are trying to 620 * free vnodes. 621 */ 622 usevnodes = desiredvnodes; 623 if (usevnodes <= 0) 624 usevnodes = 1; 625 trigger = cnt.v_page_count * 2 / usevnodes; 626 627 done = 0; 628 mtx_lock(&mntvnode_mtx); 629 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) { 630 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 631 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 632 633 if (vp->v_type != VNON && 634 vp->v_type != VBAD && 635 VI_TRYLOCK(vp)) { 636 if (VMIGHTFREE(vp) && /* critical path opt */ 637 (vp->v_object == NULL || 638 vp->v_object->resident_page_count < trigger)) { 639 mtx_unlock(&mntvnode_mtx); 640 vgonel(vp, curthread); 641 done++; 642 mtx_lock(&mntvnode_mtx); 643 } else 644 VI_UNLOCK(vp); 645 } 646 --count; 647 } 648 mtx_unlock(&mntvnode_mtx); 649 return done; 650} 651 652/* 653 * Attempt to recycle vnodes in a context that is always safe to block. 654 * Calling vlrurecycle() from the bowels of filesystem code has some 655 * interesting deadlock problems. 656 */ 657static struct proc *vnlruproc; 658static int vnlruproc_sig; 659 660static void 661vnlru_proc(void) 662{ 663 struct mount *mp, *nmp; 664 int s; 665 int done; 666 struct proc *p = vnlruproc; 667 struct thread *td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */ 668 669 mtx_lock(&Giant); 670 671 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p, 672 SHUTDOWN_PRI_FIRST); 673 674 s = splbio(); 675 for (;;) { 676 kthread_suspend_check(p); 677 mtx_lock(&vnode_free_list_mtx); 678 if (numvnodes - freevnodes <= desiredvnodes * 9 / 10) { 679 mtx_unlock(&vnode_free_list_mtx); 680 vnlruproc_sig = 0; 681 tsleep(vnlruproc, PVFS, "vlruwt", 0); 682 continue; 683 } 684 mtx_unlock(&vnode_free_list_mtx); 685 done = 0; 686 mtx_lock(&mountlist_mtx); 687 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 688 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) { 689 nmp = TAILQ_NEXT(mp, mnt_list); 690 continue; 691 } 692 done += vlrureclaim(mp, 10); 693 mtx_lock(&mountlist_mtx); 694 nmp = TAILQ_NEXT(mp, mnt_list); 695 vfs_unbusy(mp, td); 696 } 697 mtx_unlock(&mountlist_mtx); 698 if (done == 0) { 699#if 0 700 /* These messages are temporary debugging aids */ 701 if (vnlru_nowhere < 5) 702 printf("vnlru process getting nowhere..\n"); 703 else if (vnlru_nowhere == 5) 704 printf("vnlru process messages stopped.\n"); 705#endif 706 vnlru_nowhere++; 707 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 708 } 709 } 710 splx(s); 711} 712 713static struct kproc_desc vnlru_kp = { 714 "vnlru", 715 vnlru_proc, 716 &vnlruproc 717}; 718SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp) 719 720 721/* 722 * Routines having to do with the management of the vnode table. 723 */ 724 725/* 726 * Check to see if a free vnode can be recycled. If it can, return it locked 727 * with the vn lock, but not interlock. Otherwise indicate the error. 728 */ 729static int 730vcanrecycle(struct vnode *vp) 731{ 732 struct thread *td = curthread; 733 vm_object_t object; 734 int error; 735 736 /* Don't recycle if we can't get the interlock */ 737 if (!mtx_trylock(&vp->v_interlock)) 738 return (EWOULDBLOCK); 739 740 /* We should be able to immediately acquire this */ 741 /* XXX This looks like it should panic if it fails */ 742 if (vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td) != 0) 743 return (EWOULDBLOCK); 744 /* 745 * Don't recycle if we still have cached pages. 746 */ 747 if (VOP_GETVOBJECT(vp, &object) == 0 && 748 (object->resident_page_count || 749 object->ref_count)) { 750 VOP_UNLOCK(vp, 0, td); 751 error = EBUSY; 752 goto done; 753 } 754 if (LIST_FIRST(&vp->v_cache_src)) { 755 /* 756 * note: nameileafonly sysctl is temporary, 757 * for debugging only, and will eventually be 758 * removed. 759 */ 760 if (nameileafonly > 0) { 761 /* 762 * Do not reuse namei-cached directory 763 * vnodes that have cached 764 * subdirectories. 765 */ 766 if (cache_leaf_test(vp) < 0) { 767 error = EISDIR; 768 goto done; 769 } 770 } else if (nameileafonly < 0 || 771 vmiodirenable == 0) { 772 /* 773 * Do not reuse namei-cached directory 774 * vnodes if nameileafonly is -1 or 775 * if VMIO backing for directories is 776 * turned off (otherwise we reuse them 777 * too quickly). 778 */ 779 error = EBUSY; 780 goto done; 781 } 782 } 783 return (0); 784done: 785 VOP_UNLOCK(vp, 0, td); 786 return (error); 787} 788 789/* 790 * Return the next vnode from the free list. 791 */ 792int 793getnewvnode(tag, mp, vops, vpp) 794 enum vtagtype tag; 795 struct mount *mp; 796 vop_t **vops; 797 struct vnode **vpp; 798{ 799 int s; 800 struct thread *td = curthread; /* XXX */ 801 struct vnode *vp = NULL; 802 struct mount *vnmp; 803 804 s = splbio(); 805 mtx_lock(&vnode_free_list_mtx); 806 807 /* 808 * Try to reuse vnodes if we hit the max. This situation only 809 * occurs in certain large-memory (2G+) situations. We cannot 810 * attempt to directly reclaim vnodes due to nasty recursion 811 * problems. 812 */ 813 if (vnlruproc_sig == 0 && numvnodes - freevnodes > desiredvnodes) { 814 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 815 wakeup(vnlruproc); 816 } 817 818 /* 819 * Attempt to reuse a vnode already on the free list, allocating 820 * a new vnode if we can't find one or if we have not reached a 821 * good minimum for good LRU performance. 822 */ 823 824 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) { 825 int error; 826 int count; 827 828 for (count = 0; count < freevnodes; count++) { 829 vp = TAILQ_FIRST(&vnode_free_list); 830 831 KASSERT(vp->v_usecount == 0, 832 ("getnewvnode: free vnode isn't")); 833 834 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 835 /* 836 * We have to drop the free list mtx to avoid lock 837 * order reversals with interlock. 838 */ 839 mtx_unlock(&vnode_free_list_mtx); 840 error = vcanrecycle(vp); 841 /* 842 * Skip over it if its filesystem is being suspended. 843 */ 844 if (error == 0 && 845 vn_start_write(vp, &vnmp, V_NOWAIT) != 0) 846 error = EBUSY; 847 848 mtx_lock(&vnode_free_list_mtx); 849 if (error != 0) 850 TAILQ_INSERT_TAIL(&vnode_free_list, vp, 851 v_freelist); 852 else 853 break; 854 } 855 } 856 /* 857 * Unlocked access to this vp is ok because we are assured that there 858 * are no other references to it. 859 */ 860 if (vp) { 861 freevnodes--; 862 mtx_unlock(&vnode_free_list_mtx); 863 864 vp->v_iflag |= VI_DOOMED; 865 vp->v_iflag &= ~VI_FREE; 866 cache_purge(vp); 867 if (vp->v_type != VBAD) { 868 VOP_UNLOCK(vp, 0, td); 869 vgone(vp); 870 } else { 871 VOP_UNLOCK(vp, 0, td); 872 } 873 vn_finished_write(vnmp); 874 875#ifdef INVARIANTS 876 { 877 if (vp->v_data) 878 panic("cleaned vnode isn't"); 879 VI_LOCK(vp); 880 if (vp->v_numoutput) 881 panic("Clean vnode has pending I/O's"); 882 if (vp->v_writecount != 0) 883 panic("Non-zero write count"); 884 VI_UNLOCK(vp); 885 } 886#endif 887 if (vp->v_pollinfo) { 888 mtx_destroy(&vp->v_pollinfo->vpi_lock); 889 uma_zfree(vnodepoll_zone, vp->v_pollinfo); 890 } 891 vp->v_pollinfo = NULL; 892#ifdef MAC 893 mac_destroy_vnode(vp); 894#endif 895 vp->v_iflag = 0; 896 vp->v_vflag = 0; 897 vp->v_lastw = 0; 898 vp->v_lasta = 0; 899 vp->v_cstart = 0; 900 vp->v_clen = 0; 901 vp->v_socket = 0; 902 KASSERT(vp->v_cleanblkroot == NULL, ("cleanblkroot not NULL")); 903 KASSERT(vp->v_dirtyblkroot == NULL, ("dirtyblkroot not NULL")); 904 } else { 905 numvnodes++; 906 mtx_unlock(&vnode_free_list_mtx); 907 908 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO); 909 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 910 vp->v_dd = vp; 911 cache_purge(vp); 912 LIST_INIT(&vp->v_cache_src); 913 TAILQ_INIT(&vp->v_cache_dst); 914 } 915 916 TAILQ_INIT(&vp->v_cleanblkhd); 917 TAILQ_INIT(&vp->v_dirtyblkhd); 918 vp->v_type = VNON; 919 vp->v_tag = tag; 920 vp->v_op = vops; 921 lockinit(&vp->v_lock, PVFS, "vnlock", VLKTIMEOUT, LK_NOPAUSE); 922#ifdef MAC 923 mac_init_vnode(vp); 924#endif 925 insmntque(vp, mp); 926 *vpp = vp; 927 vp->v_usecount = 1; 928 vp->v_data = 0; 929 vp->v_cachedid = -1; 930 931 splx(s); 932 933#if 0 934 mp_fixme("This code does not lock access to numvnodes && freevnodes."); 935 vnodeallocs++; 936 if (vnodeallocs % vnoderecycleperiod == 0 && 937 freevnodes < vnoderecycleminfreevn && 938 vnoderecyclemintotalvn < numvnodes) { 939 /* Recycle vnodes. */ 940 cache_purgeleafdirs(vnoderecyclenumber); 941 } 942#endif 943 944 return (0); 945} 946 947/* 948 * Move a vnode from one mount queue to another. 949 */ 950static void 951insmntque(vp, mp) 952 register struct vnode *vp; 953 register struct mount *mp; 954{ 955 956 mtx_lock(&mntvnode_mtx); 957 /* 958 * Delete from old mount point vnode list, if on one. 959 */ 960 if (vp->v_mount != NULL) 961 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes); 962 /* 963 * Insert into list of vnodes for the new mount point, if available. 964 */ 965 if ((vp->v_mount = mp) == NULL) { 966 mtx_unlock(&mntvnode_mtx); 967 return; 968 } 969 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 970 mtx_unlock(&mntvnode_mtx); 971} 972 973/* 974 * Update outstanding I/O count and do wakeup if requested. 975 */ 976void 977vwakeup(bp) 978 register struct buf *bp; 979{ 980 register struct vnode *vp; 981 982 bp->b_flags &= ~B_WRITEINPROG; 983 if ((vp = bp->b_vp)) { 984 VI_LOCK(vp); 985 vp->v_numoutput--; 986 if (vp->v_numoutput < 0) 987 panic("vwakeup: neg numoutput"); 988 if ((vp->v_numoutput == 0) && (vp->v_iflag & VI_BWAIT)) { 989 vp->v_iflag &= ~VI_BWAIT; 990 wakeup(&vp->v_numoutput); 991 } 992 VI_UNLOCK(vp); 993 } 994} 995 996/* 997 * Flush out and invalidate all buffers associated with a vnode. 998 * Called with the underlying object locked. 999 */ 1000int 1001vinvalbuf(vp, flags, cred, td, slpflag, slptimeo) 1002 struct vnode *vp; 1003 int flags; 1004 struct ucred *cred; 1005 struct thread *td; 1006 int slpflag, slptimeo; 1007{ 1008 struct buf *blist; 1009 int s, error; 1010 vm_object_t object; 1011 1012 GIANT_REQUIRED; 1013 1014 if (flags & V_SAVE) { 1015 s = splbio(); 1016 VI_LOCK(vp); 1017 while (vp->v_numoutput) { 1018 vp->v_iflag |= VI_BWAIT; 1019 error = msleep(&vp->v_numoutput, VI_MTX(vp), 1020 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo); 1021 if (error) { 1022 VI_UNLOCK(vp); 1023 splx(s); 1024 return (error); 1025 } 1026 } 1027 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 1028 splx(s); 1029 VI_UNLOCK(vp); 1030 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0) 1031 return (error); 1032 /* 1033 * XXX We could save a lock/unlock if this was only 1034 * enabled under INVARIANTS 1035 */ 1036 VI_LOCK(vp); 1037 s = splbio(); 1038 if (vp->v_numoutput > 0 || 1039 !TAILQ_EMPTY(&vp->v_dirtyblkhd)) 1040 panic("vinvalbuf: dirty bufs"); 1041 } 1042 VI_UNLOCK(vp); 1043 splx(s); 1044 } 1045 s = splbio(); 1046 for (error = 0;;) { 1047 if ((blist = TAILQ_FIRST(&vp->v_cleanblkhd)) != 0 && 1048 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 1049 if (error) 1050 break; 1051 continue; 1052 } 1053 if ((blist = TAILQ_FIRST(&vp->v_dirtyblkhd)) != 0 && 1054 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 1055 if (error) 1056 break; 1057 continue; 1058 } 1059 break; 1060 } 1061 if (error) { 1062 splx(s); 1063 return (error); 1064 } 1065 1066 /* 1067 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1068 * have write I/O in-progress but if there is a VM object then the 1069 * VM object can also have read-I/O in-progress. 1070 */ 1071 VI_LOCK(vp); 1072 do { 1073 while (vp->v_numoutput > 0) { 1074 vp->v_iflag |= VI_BWAIT; 1075 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vnvlbv", 0); 1076 } 1077 VI_UNLOCK(vp); 1078 if (VOP_GETVOBJECT(vp, &object) == 0) { 1079 while (object->paging_in_progress) 1080 vm_object_pip_sleep(object, "vnvlbx"); 1081 } 1082 VI_LOCK(vp); 1083 } while (vp->v_numoutput > 0); 1084 VI_UNLOCK(vp); 1085 1086 splx(s); 1087 1088 /* 1089 * Destroy the copy in the VM cache, too. 1090 */ 1091 if (VOP_GETVOBJECT(vp, &object) == 0) { 1092 vm_object_page_remove(object, 0, 0, 1093 (flags & V_SAVE) ? TRUE : FALSE); 1094 } 1095 1096 if ((flags & (V_ALT | V_NORMAL)) == 0 && 1097 (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || 1098 !TAILQ_EMPTY(&vp->v_cleanblkhd))) 1099 panic("vinvalbuf: flush failed"); 1100 return (0); 1101} 1102 1103/* 1104 * Flush out buffers on the specified list. 1105 */ 1106static int 1107flushbuflist(blist, flags, vp, slpflag, slptimeo, errorp) 1108 struct buf *blist; 1109 int flags; 1110 struct vnode *vp; 1111 int slpflag, slptimeo; 1112 int *errorp; 1113{ 1114 struct buf *bp, *nbp; 1115 int found, error; 1116 1117 for (found = 0, bp = blist; bp; bp = nbp) { 1118 nbp = TAILQ_NEXT(bp, b_vnbufs); 1119 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1120 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) 1121 continue; 1122 found += 1; 1123 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1124 error = BUF_TIMELOCK(bp, 1125 LK_EXCLUSIVE | LK_SLEEPFAIL, 1126 "flushbuf", slpflag, slptimeo); 1127 if (error != ENOLCK) 1128 *errorp = error; 1129 return (found); 1130 } 1131 /* 1132 * XXX Since there are no node locks for NFS, I 1133 * believe there is a slight chance that a delayed 1134 * write will occur while sleeping just above, so 1135 * check for it. Note that vfs_bio_awrite expects 1136 * buffers to reside on a queue, while BUF_WRITE and 1137 * brelse do not. 1138 */ 1139 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1140 (flags & V_SAVE)) { 1141 1142 if (bp->b_vp == vp) { 1143 if (bp->b_flags & B_CLUSTEROK) { 1144 BUF_UNLOCK(bp); 1145 vfs_bio_awrite(bp); 1146 } else { 1147 bremfree(bp); 1148 bp->b_flags |= B_ASYNC; 1149 BUF_WRITE(bp); 1150 } 1151 } else { 1152 bremfree(bp); 1153 (void) BUF_WRITE(bp); 1154 } 1155 return (found); 1156 } 1157 bremfree(bp); 1158 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 1159 bp->b_flags &= ~B_ASYNC; 1160 brelse(bp); 1161 } 1162 return (found); 1163} 1164 1165/* 1166 * Truncate a file's buffer and pages to a specified length. This 1167 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1168 * sync activity. 1169 */ 1170int 1171vtruncbuf(vp, cred, td, length, blksize) 1172 register struct vnode *vp; 1173 struct ucred *cred; 1174 struct thread *td; 1175 off_t length; 1176 int blksize; 1177{ 1178 register struct buf *bp; 1179 struct buf *nbp; 1180 int s, anyfreed; 1181 int trunclbn; 1182 1183 /* 1184 * Round up to the *next* lbn. 1185 */ 1186 trunclbn = (length + blksize - 1) / blksize; 1187 1188 s = splbio(); 1189restart: 1190 anyfreed = 1; 1191 for (;anyfreed;) { 1192 anyfreed = 0; 1193 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 1194 nbp = TAILQ_NEXT(bp, b_vnbufs); 1195 if (bp->b_lblkno >= trunclbn) { 1196 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1197 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 1198 goto restart; 1199 } else { 1200 bremfree(bp); 1201 bp->b_flags |= (B_INVAL | B_RELBUF); 1202 bp->b_flags &= ~B_ASYNC; 1203 brelse(bp); 1204 anyfreed = 1; 1205 } 1206 if (nbp && 1207 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1208 (nbp->b_vp != vp) || 1209 (nbp->b_flags & B_DELWRI))) { 1210 goto restart; 1211 } 1212 } 1213 } 1214 1215 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1216 nbp = TAILQ_NEXT(bp, b_vnbufs); 1217 if (bp->b_lblkno >= trunclbn) { 1218 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1219 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 1220 goto restart; 1221 } else { 1222 bremfree(bp); 1223 bp->b_flags |= (B_INVAL | B_RELBUF); 1224 bp->b_flags &= ~B_ASYNC; 1225 brelse(bp); 1226 anyfreed = 1; 1227 } 1228 if (nbp && 1229 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1230 (nbp->b_vp != vp) || 1231 (nbp->b_flags & B_DELWRI) == 0)) { 1232 goto restart; 1233 } 1234 } 1235 } 1236 } 1237 1238 if (length > 0) { 1239restartsync: 1240 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1241 nbp = TAILQ_NEXT(bp, b_vnbufs); 1242 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) { 1243 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1244 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 1245 goto restart; 1246 } else { 1247 bremfree(bp); 1248 if (bp->b_vp == vp) { 1249 bp->b_flags |= B_ASYNC; 1250 } else { 1251 bp->b_flags &= ~B_ASYNC; 1252 } 1253 BUF_WRITE(bp); 1254 } 1255 goto restartsync; 1256 } 1257 1258 } 1259 } 1260 1261 VI_LOCK(vp); 1262 while (vp->v_numoutput > 0) { 1263 vp->v_iflag |= VI_BWAIT; 1264 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vbtrunc", 0); 1265 } 1266 VI_UNLOCK(vp); 1267 splx(s); 1268 1269 vnode_pager_setsize(vp, length); 1270 1271 return (0); 1272} 1273 1274/* 1275 * buf_splay() - splay tree core for the clean/dirty list of buffers in 1276 * a vnode. 1277 * 1278 * NOTE: We have to deal with the special case of a background bitmap 1279 * buffer, a situation where two buffers will have the same logical 1280 * block offset. We want (1) only the foreground buffer to be accessed 1281 * in a lookup and (2) must differentiate between the foreground and 1282 * background buffer in the splay tree algorithm because the splay 1283 * tree cannot normally handle multiple entities with the same 'index'. 1284 * We accomplish this by adding differentiating flags to the splay tree's 1285 * numerical domain. 1286 */ 1287static 1288struct buf * 1289buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 1290{ 1291 struct buf dummy; 1292 struct buf *lefttreemax, *righttreemin, *y; 1293 1294 if (root == NULL) 1295 return (NULL); 1296 lefttreemax = righttreemin = &dummy; 1297 for (;;) { 1298 if (lblkno < root->b_lblkno || 1299 (lblkno == root->b_lblkno && 1300 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1301 if ((y = root->b_left) == NULL) 1302 break; 1303 if (lblkno < y->b_lblkno) { 1304 /* Rotate right. */ 1305 root->b_left = y->b_right; 1306 y->b_right = root; 1307 root = y; 1308 if ((y = root->b_left) == NULL) 1309 break; 1310 } 1311 /* Link into the new root's right tree. */ 1312 righttreemin->b_left = root; 1313 righttreemin = root; 1314 } else if (lblkno > root->b_lblkno || 1315 (lblkno == root->b_lblkno && 1316 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 1317 if ((y = root->b_right) == NULL) 1318 break; 1319 if (lblkno > y->b_lblkno) { 1320 /* Rotate left. */ 1321 root->b_right = y->b_left; 1322 y->b_left = root; 1323 root = y; 1324 if ((y = root->b_right) == NULL) 1325 break; 1326 } 1327 /* Link into the new root's left tree. */ 1328 lefttreemax->b_right = root; 1329 lefttreemax = root; 1330 } else { 1331 break; 1332 } 1333 root = y; 1334 } 1335 /* Assemble the new root. */ 1336 lefttreemax->b_right = root->b_left; 1337 righttreemin->b_left = root->b_right; 1338 root->b_left = dummy.b_right; 1339 root->b_right = dummy.b_left; 1340 return (root); 1341} 1342 1343static 1344void 1345buf_vlist_remove(struct buf *bp) 1346{ 1347 struct vnode *vp = bp->b_vp; 1348 struct buf *root; 1349 1350 if (bp->b_xflags & BX_VNDIRTY) { 1351 if (bp != vp->v_dirtyblkroot) { 1352 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 1353 KASSERT(root == bp, ("splay lookup failed during dirty remove")); 1354 } 1355 if (bp->b_left == NULL) { 1356 root = bp->b_right; 1357 } else { 1358 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1359 root->b_right = bp->b_right; 1360 } 1361 vp->v_dirtyblkroot = root; 1362 TAILQ_REMOVE(&vp->v_dirtyblkhd, bp, b_vnbufs); 1363 } else { 1364 /* KASSERT(bp->b_xflags & BX_VNCLEAN, ("bp wasn't clean")); */ 1365 if (bp != vp->v_cleanblkroot) { 1366 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 1367 KASSERT(root == bp, ("splay lookup failed during clean remove")); 1368 } 1369 if (bp->b_left == NULL) { 1370 root = bp->b_right; 1371 } else { 1372 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1373 root->b_right = bp->b_right; 1374 } 1375 vp->v_cleanblkroot = root; 1376 TAILQ_REMOVE(&vp->v_cleanblkhd, bp, b_vnbufs); 1377 } 1378 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1379} 1380 1381/* 1382 * Add the buffer to the sorted clean or dirty block list using a 1383 * splay tree algorithm. 1384 * 1385 * NOTE: xflags is passed as a constant, optimizing this inline function! 1386 */ 1387static 1388void 1389buf_vlist_add(struct buf *bp, struct vnode *vp, b_xflags_t xflags) 1390{ 1391 struct buf *root; 1392 1393 bp->b_xflags |= xflags; 1394 if (xflags & BX_VNDIRTY) { 1395 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 1396 if (root == NULL) { 1397 bp->b_left = NULL; 1398 bp->b_right = NULL; 1399 TAILQ_INSERT_TAIL(&vp->v_dirtyblkhd, bp, b_vnbufs); 1400 } else if (bp->b_lblkno < root->b_lblkno || 1401 (bp->b_lblkno == root->b_lblkno && 1402 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1403 bp->b_left = root->b_left; 1404 bp->b_right = root; 1405 root->b_left = NULL; 1406 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 1407 } else { 1408 bp->b_right = root->b_right; 1409 bp->b_left = root; 1410 root->b_right = NULL; 1411 TAILQ_INSERT_AFTER(&vp->v_dirtyblkhd, 1412 root, bp, b_vnbufs); 1413 } 1414 vp->v_dirtyblkroot = bp; 1415 } else { 1416 /* KASSERT(xflags & BX_VNCLEAN, ("xflags not clean")); */ 1417 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 1418 if (root == NULL) { 1419 bp->b_left = NULL; 1420 bp->b_right = NULL; 1421 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs); 1422 } else if (bp->b_lblkno < root->b_lblkno || 1423 (bp->b_lblkno == root->b_lblkno && 1424 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1425 bp->b_left = root->b_left; 1426 bp->b_right = root; 1427 root->b_left = NULL; 1428 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 1429 } else { 1430 bp->b_right = root->b_right; 1431 bp->b_left = root; 1432 root->b_right = NULL; 1433 TAILQ_INSERT_AFTER(&vp->v_cleanblkhd, 1434 root, bp, b_vnbufs); 1435 } 1436 vp->v_cleanblkroot = bp; 1437 } 1438} 1439 1440#ifndef USE_BUFHASH 1441 1442/* 1443 * Lookup a buffer using the splay tree. Note that we specifically avoid 1444 * shadow buffers used in background bitmap writes. 1445 * 1446 * This code isn't quite efficient as it could be because we are maintaining 1447 * two sorted lists and do not know which list the block resides in. 1448 */ 1449struct buf * 1450gbincore(struct vnode *vp, daddr_t lblkno) 1451{ 1452 struct buf *bp; 1453 1454 GIANT_REQUIRED; 1455 1456 bp = vp->v_cleanblkroot = buf_splay(lblkno, 0, vp->v_cleanblkroot); 1457 if (bp && bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1458 return(bp); 1459 bp = vp->v_dirtyblkroot = buf_splay(lblkno, 0, vp->v_dirtyblkroot); 1460 if (bp && bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1461 return(bp); 1462 return(NULL); 1463} 1464 1465#endif 1466 1467/* 1468 * Associate a buffer with a vnode. 1469 */ 1470void 1471bgetvp(vp, bp) 1472 register struct vnode *vp; 1473 register struct buf *bp; 1474{ 1475 int s; 1476 1477 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 1478 1479 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1480 ("bgetvp: bp already attached! %p", bp)); 1481 1482 vhold(vp); 1483 bp->b_vp = vp; 1484 bp->b_dev = vn_todev(vp); 1485 /* 1486 * Insert onto list for new vnode. 1487 */ 1488 s = splbio(); 1489 buf_vlist_add(bp, vp, BX_VNCLEAN); 1490 splx(s); 1491} 1492 1493/* 1494 * Disassociate a buffer from a vnode. 1495 */ 1496void 1497brelvp(bp) 1498 register struct buf *bp; 1499{ 1500 struct vnode *vp; 1501 int s; 1502 1503 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1504 1505 /* 1506 * Delete from old vnode list, if on one. 1507 */ 1508 vp = bp->b_vp; 1509 s = splbio(); 1510 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1511 buf_vlist_remove(bp); 1512 VI_LOCK(vp); 1513 if ((vp->v_iflag & VI_ONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 1514 vp->v_iflag &= ~VI_ONWORKLST; 1515 LIST_REMOVE(vp, v_synclist); 1516 } 1517 VI_UNLOCK(vp); 1518 splx(s); 1519 bp->b_vp = (struct vnode *) 0; 1520 vdrop(vp); 1521 if (bp->b_object) 1522 bp->b_object = NULL; 1523} 1524 1525/* 1526 * Add an item to the syncer work queue. 1527 */ 1528static void 1529vn_syncer_add_to_worklist(struct vnode *vp, int delay) 1530{ 1531 int s, slot; 1532 1533 s = splbio(); 1534 ASSERT_VI_LOCKED(vp); 1535 1536 if (vp->v_iflag & VI_ONWORKLST) 1537 LIST_REMOVE(vp, v_synclist); 1538 else 1539 vp->v_iflag |= VI_ONWORKLST; 1540 1541 if (delay > syncer_maxdelay - 2) 1542 delay = syncer_maxdelay - 2; 1543 slot = (syncer_delayno + delay) & syncer_mask; 1544 1545 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 1546 1547 splx(s); 1548} 1549 1550struct proc *updateproc; 1551static void sched_sync(void); 1552static struct kproc_desc up_kp = { 1553 "syncer", 1554 sched_sync, 1555 &updateproc 1556}; 1557SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 1558 1559/* 1560 * System filesystem synchronizer daemon. 1561 */ 1562void 1563sched_sync(void) 1564{ 1565 struct synclist *slp; 1566 struct vnode *vp; 1567 struct mount *mp; 1568 long starttime; 1569 int s; 1570 struct thread *td = FIRST_THREAD_IN_PROC(updateproc); /* XXXKSE */ 1571 1572 mtx_lock(&Giant); 1573 1574 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, td->td_proc, 1575 SHUTDOWN_PRI_LAST); 1576 1577 for (;;) { 1578 kthread_suspend_check(td->td_proc); 1579 1580 starttime = time_second; 1581 1582 /* 1583 * Push files whose dirty time has expired. Be careful 1584 * of interrupt race on slp queue. 1585 */ 1586 s = splbio(); 1587 slp = &syncer_workitem_pending[syncer_delayno]; 1588 syncer_delayno += 1; 1589 if (syncer_delayno == syncer_maxdelay) 1590 syncer_delayno = 0; 1591 splx(s); 1592 1593 while ((vp = LIST_FIRST(slp)) != NULL) { 1594 if (VOP_ISLOCKED(vp, NULL) == 0 && 1595 vn_start_write(vp, &mp, V_NOWAIT) == 0) { 1596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1597 (void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td); 1598 VOP_UNLOCK(vp, 0, td); 1599 vn_finished_write(mp); 1600 } 1601 s = splbio(); 1602 if (LIST_FIRST(slp) == vp) { 1603 /* 1604 * Note: v_tag VT_VFS vps can remain on the 1605 * worklist too with no dirty blocks, but 1606 * since sync_fsync() moves it to a different 1607 * slot we are safe. 1608 */ 1609 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) && 1610 !vn_isdisk(vp, NULL)) 1611 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag); 1612 /* 1613 * Put us back on the worklist. The worklist 1614 * routine will remove us from our current 1615 * position and then add us back in at a later 1616 * position. 1617 */ 1618 VI_LOCK(vp); 1619 vn_syncer_add_to_worklist(vp, syncdelay); 1620 VI_UNLOCK(vp); 1621 } 1622 splx(s); 1623 } 1624 1625 /* 1626 * Do soft update processing. 1627 */ 1628 if (softdep_process_worklist_hook != NULL) 1629 (*softdep_process_worklist_hook)(NULL); 1630 1631 /* 1632 * The variable rushjob allows the kernel to speed up the 1633 * processing of the filesystem syncer process. A rushjob 1634 * value of N tells the filesystem syncer to process the next 1635 * N seconds worth of work on its queue ASAP. Currently rushjob 1636 * is used by the soft update code to speed up the filesystem 1637 * syncer process when the incore state is getting so far 1638 * ahead of the disk that the kernel memory pool is being 1639 * threatened with exhaustion. 1640 */ 1641 if (rushjob > 0) { 1642 rushjob -= 1; 1643 continue; 1644 } 1645 /* 1646 * If it has taken us less than a second to process the 1647 * current work, then wait. Otherwise start right over 1648 * again. We can still lose time if any single round 1649 * takes more than two seconds, but it does not really 1650 * matter as we are just trying to generally pace the 1651 * filesystem activity. 1652 */ 1653 if (time_second == starttime) 1654 tsleep(&lbolt, PPAUSE, "syncer", 0); 1655 } 1656} 1657 1658/* 1659 * Request the syncer daemon to speed up its work. 1660 * We never push it to speed up more than half of its 1661 * normal turn time, otherwise it could take over the cpu. 1662 * XXXKSE only one update? 1663 */ 1664int 1665speedup_syncer() 1666{ 1667 1668 mtx_lock_spin(&sched_lock); 1669 if (FIRST_THREAD_IN_PROC(updateproc)->td_wchan == &lbolt) /* XXXKSE */ 1670 setrunnable(FIRST_THREAD_IN_PROC(updateproc)); 1671 mtx_unlock_spin(&sched_lock); 1672 if (rushjob < syncdelay / 2) { 1673 rushjob += 1; 1674 stat_rush_requests += 1; 1675 return (1); 1676 } 1677 return(0); 1678} 1679 1680/* 1681 * Associate a p-buffer with a vnode. 1682 * 1683 * Also sets B_PAGING flag to indicate that vnode is not fully associated 1684 * with the buffer. i.e. the bp has not been linked into the vnode or 1685 * ref-counted. 1686 */ 1687void 1688pbgetvp(vp, bp) 1689 register struct vnode *vp; 1690 register struct buf *bp; 1691{ 1692 1693 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 1694 1695 bp->b_vp = vp; 1696 bp->b_flags |= B_PAGING; 1697 bp->b_dev = vn_todev(vp); 1698} 1699 1700/* 1701 * Disassociate a p-buffer from a vnode. 1702 */ 1703void 1704pbrelvp(bp) 1705 register struct buf *bp; 1706{ 1707 1708 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 1709 1710 /* XXX REMOVE ME */ 1711 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) { 1712 panic( 1713 "relpbuf(): b_vp was probably reassignbuf()d %p %x", 1714 bp, 1715 (int)bp->b_flags 1716 ); 1717 } 1718 bp->b_vp = (struct vnode *) 0; 1719 bp->b_flags &= ~B_PAGING; 1720} 1721 1722/* 1723 * Reassign a buffer from one vnode to another. 1724 * Used to assign file specific control information 1725 * (indirect blocks) to the vnode to which they belong. 1726 */ 1727void 1728reassignbuf(bp, newvp) 1729 register struct buf *bp; 1730 register struct vnode *newvp; 1731{ 1732 int delay; 1733 int s; 1734 1735 if (newvp == NULL) { 1736 printf("reassignbuf: NULL"); 1737 return; 1738 } 1739 ++reassignbufcalls; 1740 1741 /* 1742 * B_PAGING flagged buffers cannot be reassigned because their vp 1743 * is not fully linked in. 1744 */ 1745 if (bp->b_flags & B_PAGING) 1746 panic("cannot reassign paging buffer"); 1747 1748 s = splbio(); 1749 /* 1750 * Delete from old vnode list, if on one. 1751 */ 1752 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 1753 buf_vlist_remove(bp); 1754 if (bp->b_vp != newvp) { 1755 vdrop(bp->b_vp); 1756 bp->b_vp = NULL; /* for clarification */ 1757 } 1758 } 1759 /* 1760 * If dirty, put on list of dirty buffers; otherwise insert onto list 1761 * of clean buffers. 1762 */ 1763 if (bp->b_flags & B_DELWRI) { 1764 VI_LOCK(newvp); 1765 if ((newvp->v_iflag & VI_ONWORKLST) == 0) { 1766 switch (newvp->v_type) { 1767 case VDIR: 1768 delay = dirdelay; 1769 break; 1770 case VCHR: 1771 if (newvp->v_rdev->si_mountpoint != NULL) { 1772 delay = metadelay; 1773 break; 1774 } 1775 /* fall through */ 1776 default: 1777 delay = filedelay; 1778 } 1779 vn_syncer_add_to_worklist(newvp, delay); 1780 } 1781 VI_UNLOCK(newvp); 1782 buf_vlist_add(bp, newvp, BX_VNDIRTY); 1783 } else { 1784 buf_vlist_add(bp, newvp, BX_VNCLEAN); 1785 1786 VI_LOCK(newvp); 1787 if ((newvp->v_iflag & VI_ONWORKLST) && 1788 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) { 1789 newvp->v_iflag &= ~VI_ONWORKLST; 1790 LIST_REMOVE(newvp, v_synclist); 1791 } 1792 VI_UNLOCK(newvp); 1793 } 1794 if (bp->b_vp != newvp) { 1795 bp->b_vp = newvp; 1796 vhold(bp->b_vp); 1797 } 1798 splx(s); 1799} 1800 1801/* 1802 * Create a vnode for a device. 1803 * Used for mounting the root filesystem. 1804 */ 1805int 1806bdevvp(dev, vpp) 1807 dev_t dev; 1808 struct vnode **vpp; 1809{ 1810 register struct vnode *vp; 1811 struct vnode *nvp; 1812 int error; 1813 1814 if (dev == NODEV) { 1815 *vpp = NULLVP; 1816 return (ENXIO); 1817 } 1818 if (vfinddev(dev, VCHR, vpp)) 1819 return (0); 1820 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp); 1821 if (error) { 1822 *vpp = NULLVP; 1823 return (error); 1824 } 1825 vp = nvp; 1826 vp->v_type = VCHR; 1827 addalias(vp, dev); 1828 *vpp = vp; 1829 return (0); 1830} 1831 1832/* 1833 * Add vnode to the alias list hung off the dev_t. 1834 * 1835 * The reason for this gunk is that multiple vnodes can reference 1836 * the same physical device, so checking vp->v_usecount to see 1837 * how many users there are is inadequate; the v_usecount for 1838 * the vnodes need to be accumulated. vcount() does that. 1839 */ 1840struct vnode * 1841addaliasu(nvp, nvp_rdev) 1842 struct vnode *nvp; 1843 udev_t nvp_rdev; 1844{ 1845 struct vnode *ovp; 1846 vop_t **ops; 1847 dev_t dev; 1848 1849 if (nvp->v_type == VBLK) 1850 return (nvp); 1851 if (nvp->v_type != VCHR) 1852 panic("addaliasu on non-special vnode"); 1853 dev = udev2dev(nvp_rdev, 0); 1854 /* 1855 * Check to see if we have a bdevvp vnode with no associated 1856 * filesystem. If so, we want to associate the filesystem of 1857 * the new newly instigated vnode with the bdevvp vnode and 1858 * discard the newly created vnode rather than leaving the 1859 * bdevvp vnode lying around with no associated filesystem. 1860 */ 1861 if (vfinddev(dev, nvp->v_type, &ovp) == 0 || ovp->v_data != NULL) { 1862 addalias(nvp, dev); 1863 return (nvp); 1864 } 1865 /* 1866 * Discard unneeded vnode, but save its node specific data. 1867 * Note that if there is a lock, it is carried over in the 1868 * node specific data to the replacement vnode. 1869 */ 1870 vref(ovp); 1871 ovp->v_data = nvp->v_data; 1872 ovp->v_tag = nvp->v_tag; 1873 nvp->v_data = NULL; 1874 lockinit(&ovp->v_lock, PVFS, nvp->v_lock.lk_wmesg, 1875 nvp->v_lock.lk_timo, nvp->v_lock.lk_flags & LK_EXTFLG_MASK); 1876 if (nvp->v_vnlock) 1877 ovp->v_vnlock = &ovp->v_lock; 1878 ops = ovp->v_op; 1879 ovp->v_op = nvp->v_op; 1880 if (VOP_ISLOCKED(nvp, curthread)) { 1881 VOP_UNLOCK(nvp, 0, curthread); 1882 vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread); 1883 } 1884 nvp->v_op = ops; 1885 insmntque(ovp, nvp->v_mount); 1886 vrele(nvp); 1887 vgone(nvp); 1888 return (ovp); 1889} 1890 1891/* This is a local helper function that do the same as addaliasu, but for a 1892 * dev_t instead of an udev_t. */ 1893static void 1894addalias(nvp, dev) 1895 struct vnode *nvp; 1896 dev_t dev; 1897{ 1898 1899 KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode")); 1900 nvp->v_rdev = dev; 1901 mtx_lock(&spechash_mtx); 1902 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext); 1903 mtx_unlock(&spechash_mtx); 1904} 1905 1906/* 1907 * Grab a particular vnode from the free list, increment its 1908 * reference count and lock it. The vnode lock bit is set if the 1909 * vnode is being eliminated in vgone. The process is awakened 1910 * when the transition is completed, and an error returned to 1911 * indicate that the vnode is no longer usable (possibly having 1912 * been changed to a new filesystem type). 1913 */ 1914int 1915vget(vp, flags, td) 1916 register struct vnode *vp; 1917 int flags; 1918 struct thread *td; 1919{ 1920 int error; 1921 1922 /* 1923 * If the vnode is in the process of being cleaned out for 1924 * another use, we wait for the cleaning to finish and then 1925 * return failure. Cleaning is determined by checking that 1926 * the VI_XLOCK flag is set. 1927 */ 1928 if ((flags & LK_INTERLOCK) == 0) 1929 VI_LOCK(vp); 1930 if (vp->v_iflag & VI_XLOCK) { 1931 if (vp->v_vxproc == curthread) { 1932#if 0 1933 /* this can now occur in normal operation */ 1934 log(LOG_INFO, "VXLOCK interlock avoided\n"); 1935#endif 1936 } else { 1937 vp->v_iflag |= VI_XWANT; 1938 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0); 1939 return (ENOENT); 1940 } 1941 } 1942 1943 vp->v_usecount++; 1944 1945 if (VSHOULDBUSY(vp)) 1946 vbusy(vp); 1947 if (flags & LK_TYPE_MASK) { 1948 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) { 1949 /* 1950 * must expand vrele here because we do not want 1951 * to call VOP_INACTIVE if the reference count 1952 * drops back to zero since it was never really 1953 * active. We must remove it from the free list 1954 * before sleeping so that multiple processes do 1955 * not try to recycle it. 1956 */ 1957 VI_LOCK(vp); 1958 vp->v_usecount--; 1959 if (VSHOULDFREE(vp)) 1960 vfree(vp); 1961 else 1962 vlruvp(vp); 1963 VI_UNLOCK(vp); 1964 } 1965 return (error); 1966 } 1967 VI_UNLOCK(vp); 1968 return (0); 1969} 1970 1971/* 1972 * Increase the reference count of a vnode. 1973 */ 1974void 1975vref(struct vnode *vp) 1976{ 1977 mtx_lock(&vp->v_interlock); 1978 vp->v_usecount++; 1979 mtx_unlock(&vp->v_interlock); 1980} 1981 1982/* 1983 * Vnode put/release. 1984 * If count drops to zero, call inactive routine and return to freelist. 1985 */ 1986void 1987vrele(vp) 1988 struct vnode *vp; 1989{ 1990 struct thread *td = curthread; /* XXX */ 1991 1992 KASSERT(vp != NULL, ("vrele: null vp")); 1993 1994 VI_LOCK(vp); 1995 1996 /* Skip this v_writecount check if we're going to panic below. */ 1997 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 1998 ("vrele: missed vn_close")); 1999 2000 if (vp->v_usecount > 1) { 2001 2002 vp->v_usecount--; 2003 VI_UNLOCK(vp); 2004 2005 return; 2006 } 2007 2008 if (vp->v_usecount == 1) { 2009 vp->v_usecount--; 2010 /* 2011 * We must call VOP_INACTIVE with the node locked. 2012 * If we are doing a vput, the node is already locked, 2013 * but, in the case of vrele, we must explicitly lock 2014 * the vnode before calling VOP_INACTIVE. 2015 */ 2016 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) 2017 VOP_INACTIVE(vp, td); 2018 VI_LOCK(vp); 2019 if (VSHOULDFREE(vp)) 2020 vfree(vp); 2021 else 2022 vlruvp(vp); 2023 VI_UNLOCK(vp); 2024 2025 } else { 2026#ifdef DIAGNOSTIC 2027 vprint("vrele: negative ref count", vp); 2028 VI_UNLOCK(vp); 2029#endif 2030 panic("vrele: negative ref cnt"); 2031 } 2032} 2033 2034/* 2035 * Release an already locked vnode. This give the same effects as 2036 * unlock+vrele(), but takes less time and avoids releasing and 2037 * re-aquiring the lock (as vrele() aquires the lock internally.) 2038 */ 2039void 2040vput(vp) 2041 struct vnode *vp; 2042{ 2043 struct thread *td = curthread; /* XXX */ 2044 2045 GIANT_REQUIRED; 2046 2047 KASSERT(vp != NULL, ("vput: null vp")); 2048 mtx_lock(&vp->v_interlock); 2049 /* Skip this v_writecount check if we're going to panic below. */ 2050 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2051 ("vput: missed vn_close")); 2052 2053 if (vp->v_usecount > 1) { 2054 vp->v_usecount--; 2055 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2056 return; 2057 } 2058 2059 if (vp->v_usecount == 1) { 2060 vp->v_usecount--; 2061 /* 2062 * We must call VOP_INACTIVE with the node locked. 2063 * If we are doing a vput, the node is already locked, 2064 * so we just need to release the vnode mutex. 2065 */ 2066 VI_UNLOCK(vp); 2067 VOP_INACTIVE(vp, td); 2068 VI_LOCK(vp); 2069 if (VSHOULDFREE(vp)) 2070 vfree(vp); 2071 else 2072 vlruvp(vp); 2073 VI_UNLOCK(vp); 2074 2075 } else { 2076#ifdef DIAGNOSTIC 2077 vprint("vput: negative ref count", vp); 2078#endif 2079 panic("vput: negative ref cnt"); 2080 } 2081} 2082 2083/* 2084 * Somebody doesn't want the vnode recycled. 2085 */ 2086void 2087vhold(vp) 2088 register struct vnode *vp; 2089{ 2090 int s; 2091 2092 s = splbio(); 2093 vp->v_holdcnt++; 2094 VI_LOCK(vp); 2095 if (VSHOULDBUSY(vp)) 2096 vbusy(vp); 2097 VI_UNLOCK(vp); 2098 splx(s); 2099} 2100 2101/* 2102 * Note that there is one less who cares about this vnode. vdrop() is the 2103 * opposite of vhold(). 2104 */ 2105void 2106vdrop(vp) 2107 register struct vnode *vp; 2108{ 2109 int s; 2110 2111 s = splbio(); 2112 if (vp->v_holdcnt <= 0) 2113 panic("vdrop: holdcnt"); 2114 vp->v_holdcnt--; 2115 VI_LOCK(vp); 2116 if (VSHOULDFREE(vp)) 2117 vfree(vp); 2118 else 2119 vlruvp(vp); 2120 VI_UNLOCK(vp); 2121 splx(s); 2122} 2123 2124/* 2125 * Remove any vnodes in the vnode table belonging to mount point mp. 2126 * 2127 * If FORCECLOSE is not specified, there should not be any active ones, 2128 * return error if any are found (nb: this is a user error, not a 2129 * system error). If FORCECLOSE is specified, detach any active vnodes 2130 * that are found. 2131 * 2132 * If WRITECLOSE is set, only flush out regular file vnodes open for 2133 * writing. 2134 * 2135 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2136 * 2137 * `rootrefs' specifies the base reference count for the root vnode 2138 * of this filesystem. The root vnode is considered busy if its 2139 * v_usecount exceeds this value. On a successful return, vflush() 2140 * will call vrele() on the root vnode exactly rootrefs times. 2141 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2142 * be zero. 2143 */ 2144#ifdef DIAGNOSTIC 2145static int busyprt = 0; /* print out busy vnodes */ 2146SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2147#endif 2148 2149int 2150vflush(mp, rootrefs, flags) 2151 struct mount *mp; 2152 int rootrefs; 2153 int flags; 2154{ 2155 struct thread *td = curthread; /* XXX */ 2156 struct vnode *vp, *nvp, *rootvp = NULL; 2157 struct vattr vattr; 2158 int busy = 0, error; 2159 2160 if (rootrefs > 0) { 2161 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2162 ("vflush: bad args")); 2163 /* 2164 * Get the filesystem root vnode. We can vput() it 2165 * immediately, since with rootrefs > 0, it won't go away. 2166 */ 2167 if ((error = VFS_ROOT(mp, &rootvp)) != 0) 2168 return (error); 2169 vput(rootvp); 2170 2171 } 2172 mtx_lock(&mntvnode_mtx); 2173loop: 2174 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) { 2175 /* 2176 * Make sure this vnode wasn't reclaimed in getnewvnode(). 2177 * Start over if it has (it won't be on the list anymore). 2178 */ 2179 if (vp->v_mount != mp) 2180 goto loop; 2181 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 2182 2183 mtx_unlock(&mntvnode_mtx); 2184 VI_LOCK(vp); 2185 /* 2186 * Skip over a vnodes marked VV_SYSTEM. 2187 */ 2188 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2189 VI_UNLOCK(vp); 2190 mtx_lock(&mntvnode_mtx); 2191 continue; 2192 } 2193 /* 2194 * If WRITECLOSE is set, flush out unlinked but still open 2195 * files (even if open only for reading) and regular file 2196 * vnodes open for writing. 2197 */ 2198 mp_fixme("Getattr called with interlock held!"); 2199 if ((flags & WRITECLOSE) && 2200 (vp->v_type == VNON || 2201 (VOP_GETATTR(vp, &vattr, td->td_ucred, td) == 0 && 2202 vattr.va_nlink > 0)) && 2203 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2204 mtx_unlock(&vp->v_interlock); 2205 mtx_lock(&mntvnode_mtx); 2206 continue; 2207 } 2208 2209 /* 2210 * With v_usecount == 0, all we need to do is clear out the 2211 * vnode data structures and we are done. 2212 */ 2213 if (vp->v_usecount == 0) { 2214 vgonel(vp, td); 2215 mtx_lock(&mntvnode_mtx); 2216 continue; 2217 } 2218 2219 /* 2220 * If FORCECLOSE is set, forcibly close the vnode. For block 2221 * or character devices, revert to an anonymous device. For 2222 * all other files, just kill them. 2223 */ 2224 if (flags & FORCECLOSE) { 2225 if (vp->v_type != VCHR) { 2226 vgonel(vp, td); 2227 } else { 2228 vclean(vp, 0, td); 2229 VI_UNLOCK(vp); 2230 vp->v_op = spec_vnodeop_p; 2231 insmntque(vp, (struct mount *) 0); 2232 } 2233 mtx_lock(&mntvnode_mtx); 2234 continue; 2235 } 2236#ifdef DIAGNOSTIC 2237 if (busyprt) 2238 vprint("vflush: busy vnode", vp); 2239#endif 2240 VI_UNLOCK(vp); 2241 mtx_lock(&mntvnode_mtx); 2242 busy++; 2243 } 2244 mtx_unlock(&mntvnode_mtx); 2245 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2246 /* 2247 * If just the root vnode is busy, and if its refcount 2248 * is equal to `rootrefs', then go ahead and kill it. 2249 */ 2250 mtx_lock(&rootvp->v_interlock); 2251 KASSERT(busy > 0, ("vflush: not busy")); 2252 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs")); 2253 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2254 vgonel(rootvp, td); 2255 busy = 0; 2256 } else 2257 mtx_unlock(&rootvp->v_interlock); 2258 } 2259 if (busy) 2260 return (EBUSY); 2261 for (; rootrefs > 0; rootrefs--) 2262 vrele(rootvp); 2263 return (0); 2264} 2265 2266/* 2267 * This moves a now (likely recyclable) vnode to the end of the 2268 * mountlist. XXX However, it is temporarily disabled until we 2269 * can clean up ffs_sync() and friends, which have loop restart 2270 * conditions which this code causes to operate O(N^2). 2271 */ 2272static void 2273vlruvp(struct vnode *vp) 2274{ 2275#if 0 2276 struct mount *mp; 2277 2278 if ((mp = vp->v_mount) != NULL) { 2279 mtx_lock(&mntvnode_mtx); 2280 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2281 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2282 mtx_unlock(&mntvnode_mtx); 2283 } 2284#endif 2285} 2286 2287/* 2288 * Disassociate the underlying filesystem from a vnode. 2289 */ 2290static void 2291vclean(vp, flags, td) 2292 struct vnode *vp; 2293 int flags; 2294 struct thread *td; 2295{ 2296 int active; 2297 2298 ASSERT_VI_LOCKED(vp); 2299 /* 2300 * Check to see if the vnode is in use. If so we have to reference it 2301 * before we clean it out so that its count cannot fall to zero and 2302 * generate a race against ourselves to recycle it. 2303 */ 2304 if ((active = vp->v_usecount)) 2305 vp->v_usecount++; 2306 2307 /* 2308 * Prevent the vnode from being recycled or brought into use while we 2309 * clean it out. 2310 */ 2311 if (vp->v_iflag & VI_XLOCK) 2312 panic("vclean: deadlock"); 2313 vp->v_iflag |= VI_XLOCK; 2314 vp->v_vxproc = curthread; 2315 /* 2316 * Even if the count is zero, the VOP_INACTIVE routine may still 2317 * have the object locked while it cleans it out. The VOP_LOCK 2318 * ensures that the VOP_INACTIVE routine is done with its work. 2319 * For active vnodes, it ensures that no other activity can 2320 * occur while the underlying object is being cleaned out. 2321 */ 2322 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 2323 2324 /* 2325 * Clean out any buffers associated with the vnode. 2326 * If the flush fails, just toss the buffers. 2327 */ 2328 if (flags & DOCLOSE) { 2329 if (TAILQ_FIRST(&vp->v_dirtyblkhd) != NULL) 2330 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 2331 if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0) 2332 vinvalbuf(vp, 0, NOCRED, td, 0, 0); 2333 } 2334 2335 VOP_DESTROYVOBJECT(vp); 2336 2337 /* 2338 * Any other processes trying to obtain this lock must first 2339 * wait for VXLOCK to clear, then call the new lock operation. 2340 */ 2341 VOP_UNLOCK(vp, 0, td); 2342 2343 /* 2344 * If purging an active vnode, it must be closed and 2345 * deactivated before being reclaimed. Note that the 2346 * VOP_INACTIVE will unlock the vnode. 2347 */ 2348 if (active) { 2349 if (flags & DOCLOSE) 2350 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2351 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0) 2352 panic("vclean: cannot relock."); 2353 VOP_INACTIVE(vp, td); 2354 } 2355 2356 /* 2357 * Reclaim the vnode. 2358 */ 2359 if (VOP_RECLAIM(vp, td)) 2360 panic("vclean: cannot reclaim"); 2361 2362 if (active) { 2363 /* 2364 * Inline copy of vrele() since VOP_INACTIVE 2365 * has already been called. 2366 */ 2367 VI_LOCK(vp); 2368 if (--vp->v_usecount <= 0) { 2369#ifdef DIAGNOSTIC 2370 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 2371 vprint("vclean: bad ref count", vp); 2372 panic("vclean: ref cnt"); 2373 } 2374#endif 2375 vfree(vp); 2376 } 2377 VI_UNLOCK(vp); 2378 } 2379 2380 cache_purge(vp); 2381 vp->v_vnlock = NULL; 2382 lockdestroy(&vp->v_lock); 2383 2384 VI_LOCK(vp); 2385 if (VSHOULDFREE(vp)) 2386 vfree(vp); 2387 2388 /* 2389 * Done with purge, notify sleepers of the grim news. 2390 */ 2391 vp->v_op = dead_vnodeop_p; 2392 if (vp->v_pollinfo != NULL) 2393 vn_pollgone(vp); 2394 vp->v_tag = VT_NON; 2395 vp->v_iflag &= ~VI_XLOCK; 2396 vp->v_vxproc = NULL; 2397 if (vp->v_iflag & VI_XWANT) { 2398 vp->v_iflag &= ~VI_XWANT; 2399 wakeup(vp); 2400 } 2401} 2402 2403/* 2404 * Eliminate all activity associated with the requested vnode 2405 * and with all vnodes aliased to the requested vnode. 2406 */ 2407int 2408vop_revoke(ap) 2409 struct vop_revoke_args /* { 2410 struct vnode *a_vp; 2411 int a_flags; 2412 } */ *ap; 2413{ 2414 struct vnode *vp, *vq; 2415 dev_t dev; 2416 2417 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke")); 2418 2419 vp = ap->a_vp; 2420 VI_LOCK(vp); 2421 /* 2422 * If a vgone (or vclean) is already in progress, 2423 * wait until it is done and return. 2424 */ 2425 if (vp->v_iflag & VI_XLOCK) { 2426 vp->v_iflag |= VI_XWANT; 2427 msleep(vp, VI_MTX(vp), PINOD | PDROP, 2428 "vop_revokeall", 0); 2429 VI_UNLOCK(vp); 2430 return (0); 2431 } 2432 VI_UNLOCK(vp); 2433 dev = vp->v_rdev; 2434 for (;;) { 2435 mtx_lock(&spechash_mtx); 2436 vq = SLIST_FIRST(&dev->si_hlist); 2437 mtx_unlock(&spechash_mtx); 2438 if (!vq) 2439 break; 2440 vgone(vq); 2441 } 2442 return (0); 2443} 2444 2445/* 2446 * Recycle an unused vnode to the front of the free list. 2447 * Release the passed interlock if the vnode will be recycled. 2448 */ 2449int 2450vrecycle(vp, inter_lkp, td) 2451 struct vnode *vp; 2452 struct mtx *inter_lkp; 2453 struct thread *td; 2454{ 2455 2456 mtx_lock(&vp->v_interlock); 2457 if (vp->v_usecount == 0) { 2458 if (inter_lkp) { 2459 mtx_unlock(inter_lkp); 2460 } 2461 vgonel(vp, td); 2462 return (1); 2463 } 2464 mtx_unlock(&vp->v_interlock); 2465 return (0); 2466} 2467 2468/* 2469 * Eliminate all activity associated with a vnode 2470 * in preparation for reuse. 2471 */ 2472void 2473vgone(vp) 2474 register struct vnode *vp; 2475{ 2476 struct thread *td = curthread; /* XXX */ 2477 2478 VI_LOCK(vp); 2479 vgonel(vp, td); 2480} 2481 2482/* 2483 * vgone, with the vp interlock held. 2484 */ 2485void 2486vgonel(vp, td) 2487 struct vnode *vp; 2488 struct thread *td; 2489{ 2490 int s; 2491 2492 /* 2493 * If a vgone (or vclean) is already in progress, 2494 * wait until it is done and return. 2495 */ 2496 ASSERT_VI_LOCKED(vp); 2497 if (vp->v_iflag & VI_XLOCK) { 2498 vp->v_iflag |= VI_XWANT; 2499 VI_UNLOCK(vp); 2500 tsleep(vp, PINOD | PDROP, "vgone", 0); 2501 return; 2502 } 2503 2504 /* 2505 * Clean out the filesystem specific data. 2506 */ 2507 vclean(vp, DOCLOSE, td); 2508 VI_UNLOCK(vp); 2509 2510 /* 2511 * Delete from old mount point vnode list, if on one. 2512 */ 2513 if (vp->v_mount != NULL) 2514 insmntque(vp, (struct mount *)0); 2515 /* 2516 * If special device, remove it from special device alias list 2517 * if it is on one. 2518 */ 2519 if (vp->v_type == VCHR && vp->v_rdev != NULL && vp->v_rdev != NODEV) { 2520 mtx_lock(&spechash_mtx); 2521 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext); 2522 freedev(vp->v_rdev); 2523 mtx_unlock(&spechash_mtx); 2524 vp->v_rdev = NULL; 2525 } 2526 2527 /* 2528 * If it is on the freelist and not already at the head, 2529 * move it to the head of the list. The test of the 2530 * VDOOMED flag and the reference count of zero is because 2531 * it will be removed from the free list by getnewvnode, 2532 * but will not have its reference count incremented until 2533 * after calling vgone. If the reference count were 2534 * incremented first, vgone would (incorrectly) try to 2535 * close the previous instance of the underlying object. 2536 */ 2537 VI_LOCK(vp); 2538 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) { 2539 s = splbio(); 2540 mtx_lock(&vnode_free_list_mtx); 2541 if (vp->v_iflag & VI_FREE) { 2542 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2543 } else { 2544 vp->v_iflag |= VI_FREE; 2545 freevnodes++; 2546 } 2547 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2548 mtx_unlock(&vnode_free_list_mtx); 2549 splx(s); 2550 } 2551 2552 vp->v_type = VBAD; 2553 VI_UNLOCK(vp); 2554} 2555 2556/* 2557 * Lookup a vnode by device number. 2558 */ 2559int 2560vfinddev(dev, type, vpp) 2561 dev_t dev; 2562 enum vtype type; 2563 struct vnode **vpp; 2564{ 2565 struct vnode *vp; 2566 2567 mtx_lock(&spechash_mtx); 2568 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 2569 if (type == vp->v_type) { 2570 *vpp = vp; 2571 mtx_unlock(&spechash_mtx); 2572 return (1); 2573 } 2574 } 2575 mtx_unlock(&spechash_mtx); 2576 return (0); 2577} 2578 2579/* 2580 * Calculate the total number of references to a special device. 2581 */ 2582int 2583vcount(vp) 2584 struct vnode *vp; 2585{ 2586 struct vnode *vq; 2587 int count; 2588 2589 count = 0; 2590 mtx_lock(&spechash_mtx); 2591 SLIST_FOREACH(vq, &vp->v_rdev->si_hlist, v_specnext) 2592 count += vq->v_usecount; 2593 mtx_unlock(&spechash_mtx); 2594 return (count); 2595} 2596 2597/* 2598 * Same as above, but using the dev_t as argument 2599 */ 2600int 2601count_dev(dev) 2602 dev_t dev; 2603{ 2604 struct vnode *vp; 2605 2606 vp = SLIST_FIRST(&dev->si_hlist); 2607 if (vp == NULL) 2608 return (0); 2609 return(vcount(vp)); 2610} 2611 2612/* 2613 * Print out a description of a vnode. 2614 */ 2615static char *typename[] = 2616{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2617 2618void 2619vprint(label, vp) 2620 char *label; 2621 struct vnode *vp; 2622{ 2623 char buf[96]; 2624 2625 if (label != NULL) 2626 printf("%s: %p: ", label, (void *)vp); 2627 else 2628 printf("%p: ", (void *)vp); 2629 printf("type %s, usecount %d, writecount %d, refcount %d,", 2630 typename[vp->v_type], vp->v_usecount, vp->v_writecount, 2631 vp->v_holdcnt); 2632 buf[0] = '\0'; 2633 if (vp->v_vflag & VV_ROOT) 2634 strcat(buf, "|VV_ROOT"); 2635 if (vp->v_vflag & VV_TEXT) 2636 strcat(buf, "|VV_TEXT"); 2637 if (vp->v_vflag & VV_SYSTEM) 2638 strcat(buf, "|VV_SYSTEM"); 2639 if (vp->v_iflag & VI_XLOCK) 2640 strcat(buf, "|VI_XLOCK"); 2641 if (vp->v_iflag & VI_XWANT) 2642 strcat(buf, "|VI_XWANT"); 2643 if (vp->v_iflag & VI_BWAIT) 2644 strcat(buf, "|VI_BWAIT"); 2645 if (vp->v_iflag & VI_DOOMED) 2646 strcat(buf, "|VI_DOOMED"); 2647 if (vp->v_iflag & VI_FREE) 2648 strcat(buf, "|VI_FREE"); 2649 if (vp->v_vflag & VV_OBJBUF) 2650 strcat(buf, "|VV_OBJBUF"); 2651 if (buf[0] != '\0') 2652 printf(" flags (%s)", &buf[1]); 2653 if (vp->v_data == NULL) { 2654 printf("\n"); 2655 } else { 2656 printf("\n\t"); 2657 VOP_PRINT(vp); 2658 } 2659} 2660 2661#ifdef DDB 2662#include <ddb/ddb.h> 2663/* 2664 * List all of the locked vnodes in the system. 2665 * Called when debugging the kernel. 2666 */ 2667DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2668{ 2669 struct thread *td = curthread; /* XXX */ 2670 struct mount *mp, *nmp; 2671 struct vnode *vp; 2672 2673 printf("Locked vnodes\n"); 2674 mtx_lock(&mountlist_mtx); 2675 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2676 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) { 2677 nmp = TAILQ_NEXT(mp, mnt_list); 2678 continue; 2679 } 2680 mtx_lock(&mntvnode_mtx); 2681 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2682 if (VOP_ISLOCKED(vp, NULL)) 2683 vprint((char *)0, vp); 2684 } 2685 mtx_unlock(&mntvnode_mtx); 2686 mtx_lock(&mountlist_mtx); 2687 nmp = TAILQ_NEXT(mp, mnt_list); 2688 vfs_unbusy(mp, td); 2689 } 2690 mtx_unlock(&mountlist_mtx); 2691} 2692#endif 2693 2694/* 2695 * Fill in a struct xvfsconf based on a struct vfsconf. 2696 */ 2697static void 2698vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2699{ 2700 2701 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2702 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2703 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2704 xvfsp->vfc_flags = vfsp->vfc_flags; 2705 /* 2706 * These are unused in userland, we keep them 2707 * to not break binary compatibility. 2708 */ 2709 xvfsp->vfc_vfsops = NULL; 2710 xvfsp->vfc_next = NULL; 2711} 2712 2713static int 2714sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2715{ 2716 struct vfsconf *vfsp; 2717 struct xvfsconf *xvfsp; 2718 int cnt, error, i; 2719 2720 cnt = 0; 2721 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 2722 cnt++; 2723 xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK); 2724 /* 2725 * Handle the race that we will have here when struct vfsconf 2726 * will be locked down by using both cnt and checking vfc_next 2727 * against NULL to determine the end of the loop. The race will 2728 * happen because we will have to unlock before calling malloc(). 2729 * We are protected by Giant for now. 2730 */ 2731 i = 0; 2732 for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) { 2733 vfsconf2x(vfsp, xvfsp + i); 2734 i++; 2735 } 2736 error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i); 2737 free(xvfsp, M_TEMP); 2738 return (error); 2739} 2740 2741SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2742 "S,xvfsconf", "List of all configured filesystems"); 2743 2744/* 2745 * Top level filesystem related information gathering. 2746 */ 2747static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2748 2749static int 2750vfs_sysctl(SYSCTL_HANDLER_ARGS) 2751{ 2752 int *name = (int *)arg1 - 1; /* XXX */ 2753 u_int namelen = arg2 + 1; /* XXX */ 2754 struct vfsconf *vfsp; 2755 struct xvfsconf xvfsp; 2756 2757 printf("WARNING: userland calling deprecated sysctl, " 2758 "please rebuild world\n"); 2759 2760#if 1 || defined(COMPAT_PRELITE2) 2761 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2762 if (namelen == 1) 2763 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2764#endif 2765 2766 switch (name[1]) { 2767 case VFS_MAXTYPENUM: 2768 if (namelen != 2) 2769 return (ENOTDIR); 2770 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2771 case VFS_CONF: 2772 if (namelen != 3) 2773 return (ENOTDIR); /* overloaded */ 2774 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 2775 if (vfsp->vfc_typenum == name[2]) 2776 break; 2777 if (vfsp == NULL) 2778 return (EOPNOTSUPP); 2779 vfsconf2x(vfsp, &xvfsp); 2780 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 2781 } 2782 return (EOPNOTSUPP); 2783} 2784 2785SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl, 2786 "Generic filesystem"); 2787 2788#if 1 || defined(COMPAT_PRELITE2) 2789 2790static int 2791sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2792{ 2793 int error; 2794 struct vfsconf *vfsp; 2795 struct ovfsconf ovfs; 2796 2797 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 2798 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 2799 strcpy(ovfs.vfc_name, vfsp->vfc_name); 2800 ovfs.vfc_index = vfsp->vfc_typenum; 2801 ovfs.vfc_refcount = vfsp->vfc_refcount; 2802 ovfs.vfc_flags = vfsp->vfc_flags; 2803 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 2804 if (error) 2805 return error; 2806 } 2807 return 0; 2808} 2809 2810#endif /* 1 || COMPAT_PRELITE2 */ 2811 2812#define KINFO_VNODESLOP 10 2813/* 2814 * Dump vnode list (via sysctl). 2815 */ 2816/* ARGSUSED */ 2817static int 2818sysctl_vnode(SYSCTL_HANDLER_ARGS) 2819{ 2820 struct xvnode *xvn; 2821 struct thread *td = req->td; 2822 struct mount *mp; 2823 struct vnode *vp; 2824 int error, len, n; 2825 2826 /* 2827 * Stale numvnodes access is not fatal here. 2828 */ 2829 req->lock = 0; 2830 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 2831 if (!req->oldptr) 2832 /* Make an estimate */ 2833 return (SYSCTL_OUT(req, 0, len)); 2834 2835 sysctl_wire_old_buffer(req, 0); 2836 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 2837 n = 0; 2838 mtx_lock(&mountlist_mtx); 2839 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2840 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 2841 continue; 2842 mtx_lock(&mntvnode_mtx); 2843 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2844 if (n == len) 2845 break; 2846 vref(vp); 2847 xvn[n].xv_size = sizeof *xvn; 2848 xvn[n].xv_vnode = vp; 2849#define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 2850 XV_COPY(usecount); 2851 XV_COPY(writecount); 2852 XV_COPY(holdcnt); 2853 XV_COPY(id); 2854 XV_COPY(mount); 2855 XV_COPY(numoutput); 2856 XV_COPY(type); 2857#undef XV_COPY 2858 xvn[n].xv_flag = vp->v_vflag; 2859 2860 switch (vp->v_type) { 2861 case VREG: 2862 case VDIR: 2863 case VLNK: 2864 xvn[n].xv_dev = vp->v_cachedfs; 2865 xvn[n].xv_ino = vp->v_cachedid; 2866 break; 2867 case VBLK: 2868 case VCHR: 2869 if (vp->v_rdev == NULL) { 2870 vrele(vp); 2871 continue; 2872 } 2873 xvn[n].xv_dev = dev2udev(vp->v_rdev); 2874 break; 2875 case VSOCK: 2876 xvn[n].xv_socket = vp->v_socket; 2877 break; 2878 case VFIFO: 2879 xvn[n].xv_fifo = vp->v_fifoinfo; 2880 break; 2881 case VNON: 2882 case VBAD: 2883 default: 2884 /* shouldn't happen? */ 2885 vrele(vp); 2886 continue; 2887 } 2888 vrele(vp); 2889 ++n; 2890 } 2891 mtx_unlock(&mntvnode_mtx); 2892 mtx_lock(&mountlist_mtx); 2893 vfs_unbusy(mp, td); 2894 if (n == len) 2895 break; 2896 } 2897 mtx_unlock(&mountlist_mtx); 2898 2899 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 2900 free(xvn, M_TEMP); 2901 return (error); 2902} 2903 2904SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 2905 0, 0, sysctl_vnode, "S,xvnode", ""); 2906 2907/* 2908 * Check to see if a filesystem is mounted on a block device. 2909 */ 2910int 2911vfs_mountedon(vp) 2912 struct vnode *vp; 2913{ 2914 2915 if (vp->v_rdev->si_mountpoint != NULL) 2916 return (EBUSY); 2917 return (0); 2918} 2919 2920/* 2921 * Unmount all filesystems. The list is traversed in reverse order 2922 * of mounting to avoid dependencies. 2923 */ 2924void 2925vfs_unmountall() 2926{ 2927 struct mount *mp; 2928 struct thread *td; 2929 int error; 2930 2931 if (curthread != NULL) 2932 td = curthread; 2933 else 2934 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */ 2935 /* 2936 * Since this only runs when rebooting, it is not interlocked. 2937 */ 2938 while(!TAILQ_EMPTY(&mountlist)) { 2939 mp = TAILQ_LAST(&mountlist, mntlist); 2940 error = dounmount(mp, MNT_FORCE, td); 2941 if (error) { 2942 TAILQ_REMOVE(&mountlist, mp, mnt_list); 2943 printf("unmount of %s failed (", 2944 mp->mnt_stat.f_mntonname); 2945 if (error == EBUSY) 2946 printf("BUSY)\n"); 2947 else 2948 printf("%d)\n", error); 2949 } else { 2950 /* The unmount has removed mp from the mountlist */ 2951 } 2952 } 2953} 2954 2955/* 2956 * perform msync on all vnodes under a mount point 2957 * the mount point must be locked. 2958 */ 2959void 2960vfs_msync(struct mount *mp, int flags) 2961{ 2962 struct vnode *vp, *nvp; 2963 struct vm_object *obj; 2964 int tries; 2965 2966 GIANT_REQUIRED; 2967 2968 tries = 5; 2969 mtx_lock(&mntvnode_mtx); 2970loop: 2971 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 2972 if (vp->v_mount != mp) { 2973 if (--tries > 0) 2974 goto loop; 2975 break; 2976 } 2977 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 2978 2979 mp_fixme("What locks do we need here?"); 2980 if (vp->v_iflag & VI_XLOCK) /* XXX: what if MNT_WAIT? */ 2981 continue; 2982 2983 if (vp->v_vflag & VV_NOSYNC) /* unlinked, skip it */ 2984 continue; 2985 2986 if ((vp->v_iflag & VI_OBJDIRTY) && 2987 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 2988 mtx_unlock(&mntvnode_mtx); 2989 if (!vget(vp, 2990 LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curthread)) { 2991 if (VOP_GETVOBJECT(vp, &obj) == 0) { 2992 vm_object_page_clean(obj, 0, 0, 2993 flags == MNT_WAIT ? 2994 OBJPC_SYNC : OBJPC_NOSYNC); 2995 } 2996 vput(vp); 2997 } 2998 mtx_lock(&mntvnode_mtx); 2999 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) { 3000 if (--tries > 0) 3001 goto loop; 3002 break; 3003 } 3004 } 3005 } 3006 mtx_unlock(&mntvnode_mtx); 3007} 3008 3009/* 3010 * Create the VM object needed for VMIO and mmap support. This 3011 * is done for all VREG files in the system. Some filesystems might 3012 * afford the additional metadata buffering capability of the 3013 * VMIO code by making the device node be VMIO mode also. 3014 * 3015 * vp must be locked when vfs_object_create is called. 3016 */ 3017int 3018vfs_object_create(vp, td, cred) 3019 struct vnode *vp; 3020 struct thread *td; 3021 struct ucred *cred; 3022{ 3023 GIANT_REQUIRED; 3024 return (VOP_CREATEVOBJECT(vp, cred, td)); 3025} 3026 3027/* 3028 * Mark a vnode as free, putting it up for recycling. 3029 */ 3030void 3031vfree(vp) 3032 struct vnode *vp; 3033{ 3034 int s; 3035 3036 ASSERT_VI_LOCKED(vp); 3037 s = splbio(); 3038 mtx_lock(&vnode_free_list_mtx); 3039 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free")); 3040 if (vp->v_iflag & VI_AGE) { 3041 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 3042 } else { 3043 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 3044 } 3045 freevnodes++; 3046 mtx_unlock(&vnode_free_list_mtx); 3047 vp->v_iflag &= ~VI_AGE; 3048 vp->v_iflag |= VI_FREE; 3049 splx(s); 3050} 3051 3052/* 3053 * Opposite of vfree() - mark a vnode as in use. 3054 */ 3055void 3056vbusy(vp) 3057 struct vnode *vp; 3058{ 3059 int s; 3060 3061 s = splbio(); 3062 ASSERT_VI_LOCKED(vp); 3063 mtx_lock(&vnode_free_list_mtx); 3064 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free")); 3065 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 3066 freevnodes--; 3067 mtx_unlock(&vnode_free_list_mtx); 3068 vp->v_iflag &= ~(VI_FREE|VI_AGE); 3069 splx(s); 3070} 3071 3072/* 3073 * Record a process's interest in events which might happen to 3074 * a vnode. Because poll uses the historic select-style interface 3075 * internally, this routine serves as both the ``check for any 3076 * pending events'' and the ``record my interest in future events'' 3077 * functions. (These are done together, while the lock is held, 3078 * to avoid race conditions.) 3079 */ 3080int 3081vn_pollrecord(vp, td, events) 3082 struct vnode *vp; 3083 struct thread *td; 3084 short events; 3085{ 3086 3087 if (vp->v_pollinfo == NULL) 3088 v_addpollinfo(vp); 3089 mtx_lock(&vp->v_pollinfo->vpi_lock); 3090 if (vp->v_pollinfo->vpi_revents & events) { 3091 /* 3092 * This leaves events we are not interested 3093 * in available for the other process which 3094 * which presumably had requested them 3095 * (otherwise they would never have been 3096 * recorded). 3097 */ 3098 events &= vp->v_pollinfo->vpi_revents; 3099 vp->v_pollinfo->vpi_revents &= ~events; 3100 3101 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3102 return events; 3103 } 3104 vp->v_pollinfo->vpi_events |= events; 3105 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3106 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3107 return 0; 3108} 3109 3110/* 3111 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 3112 * it is possible for us to miss an event due to race conditions, but 3113 * that condition is expected to be rare, so for the moment it is the 3114 * preferred interface. 3115 */ 3116void 3117vn_pollevent(vp, events) 3118 struct vnode *vp; 3119 short events; 3120{ 3121 3122 if (vp->v_pollinfo == NULL) 3123 v_addpollinfo(vp); 3124 mtx_lock(&vp->v_pollinfo->vpi_lock); 3125 if (vp->v_pollinfo->vpi_events & events) { 3126 /* 3127 * We clear vpi_events so that we don't 3128 * call selwakeup() twice if two events are 3129 * posted before the polling process(es) is 3130 * awakened. This also ensures that we take at 3131 * most one selwakeup() if the polling process 3132 * is no longer interested. However, it does 3133 * mean that only one event can be noticed at 3134 * a time. (Perhaps we should only clear those 3135 * event bits which we note?) XXX 3136 */ 3137 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */ 3138 vp->v_pollinfo->vpi_revents |= events; 3139 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3140 } 3141 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3142} 3143 3144/* 3145 * Wake up anyone polling on vp because it is being revoked. 3146 * This depends on dead_poll() returning POLLHUP for correct 3147 * behavior. 3148 */ 3149void 3150vn_pollgone(vp) 3151 struct vnode *vp; 3152{ 3153 3154 mtx_lock(&vp->v_pollinfo->vpi_lock); 3155 VN_KNOTE(vp, NOTE_REVOKE); 3156 if (vp->v_pollinfo->vpi_events) { 3157 vp->v_pollinfo->vpi_events = 0; 3158 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3159 } 3160 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3161} 3162 3163 3164 3165/* 3166 * Routine to create and manage a filesystem syncer vnode. 3167 */ 3168#define sync_close ((int (*)(struct vop_close_args *))nullop) 3169static int sync_fsync(struct vop_fsync_args *); 3170static int sync_inactive(struct vop_inactive_args *); 3171static int sync_reclaim(struct vop_reclaim_args *); 3172static int sync_print(struct vop_print_args *); 3173 3174static vop_t **sync_vnodeop_p; 3175static struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 3176 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 3177 { &vop_close_desc, (vop_t *) sync_close }, /* close */ 3178 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */ 3179 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */ 3180 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */ 3181 { &vop_lock_desc, (vop_t *) vop_stdlock }, /* lock */ 3182 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, /* unlock */ 3183 { &vop_print_desc, (vop_t *) sync_print }, /* print */ 3184 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, /* islocked */ 3185 { NULL, NULL } 3186}; 3187static struct vnodeopv_desc sync_vnodeop_opv_desc = 3188 { &sync_vnodeop_p, sync_vnodeop_entries }; 3189 3190VNODEOP_SET(sync_vnodeop_opv_desc); 3191 3192/* 3193 * Create a new filesystem syncer vnode for the specified mount point. 3194 */ 3195int 3196vfs_allocate_syncvnode(mp) 3197 struct mount *mp; 3198{ 3199 struct vnode *vp; 3200 static long start, incr, next; 3201 int error; 3202 3203 /* Allocate a new vnode */ 3204 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) { 3205 mp->mnt_syncer = NULL; 3206 return (error); 3207 } 3208 vp->v_type = VNON; 3209 /* 3210 * Place the vnode onto the syncer worklist. We attempt to 3211 * scatter them about on the list so that they will go off 3212 * at evenly distributed times even if all the filesystems 3213 * are mounted at once. 3214 */ 3215 next += incr; 3216 if (next == 0 || next > syncer_maxdelay) { 3217 start /= 2; 3218 incr /= 2; 3219 if (start == 0) { 3220 start = syncer_maxdelay / 2; 3221 incr = syncer_maxdelay; 3222 } 3223 next = start; 3224 } 3225 VI_LOCK(vp); 3226 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0); 3227 VI_UNLOCK(vp); 3228 mp->mnt_syncer = vp; 3229 return (0); 3230} 3231 3232/* 3233 * Do a lazy sync of the filesystem. 3234 */ 3235static int 3236sync_fsync(ap) 3237 struct vop_fsync_args /* { 3238 struct vnode *a_vp; 3239 struct ucred *a_cred; 3240 int a_waitfor; 3241 struct thread *a_td; 3242 } */ *ap; 3243{ 3244 struct vnode *syncvp = ap->a_vp; 3245 struct mount *mp = syncvp->v_mount; 3246 struct thread *td = ap->a_td; 3247 int asyncflag; 3248 3249 /* 3250 * We only need to do something if this is a lazy evaluation. 3251 */ 3252 if (ap->a_waitfor != MNT_LAZY) 3253 return (0); 3254 3255 /* 3256 * Move ourselves to the back of the sync list. 3257 */ 3258 VI_LOCK(syncvp); 3259 vn_syncer_add_to_worklist(syncvp, syncdelay); 3260 VI_UNLOCK(syncvp); 3261 3262 /* 3263 * Walk the list of vnodes pushing all that are dirty and 3264 * not already on the sync list. 3265 */ 3266 mtx_lock(&mountlist_mtx); 3267 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3268 mtx_unlock(&mountlist_mtx); 3269 return (0); 3270 } 3271 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3272 vfs_unbusy(mp, td); 3273 return (0); 3274 } 3275 asyncflag = mp->mnt_flag & MNT_ASYNC; 3276 mp->mnt_flag &= ~MNT_ASYNC; 3277 vfs_msync(mp, MNT_NOWAIT); 3278 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td); 3279 if (asyncflag) 3280 mp->mnt_flag |= MNT_ASYNC; 3281 vn_finished_write(mp); 3282 vfs_unbusy(mp, td); 3283 return (0); 3284} 3285 3286/* 3287 * The syncer vnode is no referenced. 3288 */ 3289static int 3290sync_inactive(ap) 3291 struct vop_inactive_args /* { 3292 struct vnode *a_vp; 3293 struct thread *a_td; 3294 } */ *ap; 3295{ 3296 3297 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 3298 vgone(ap->a_vp); 3299 return (0); 3300} 3301 3302/* 3303 * The syncer vnode is no longer needed and is being decommissioned. 3304 * 3305 * Modifications to the worklist must be protected at splbio(). 3306 */ 3307static int 3308sync_reclaim(ap) 3309 struct vop_reclaim_args /* { 3310 struct vnode *a_vp; 3311 } */ *ap; 3312{ 3313 struct vnode *vp = ap->a_vp; 3314 int s; 3315 3316 s = splbio(); 3317 vp->v_mount->mnt_syncer = NULL; 3318 VI_LOCK(vp); 3319 if (vp->v_iflag & VI_ONWORKLST) { 3320 LIST_REMOVE(vp, v_synclist); 3321 vp->v_iflag &= ~VI_ONWORKLST; 3322 } 3323 VI_UNLOCK(vp); 3324 splx(s); 3325 3326 return (0); 3327} 3328 3329/* 3330 * Print out a syncer vnode. 3331 */ 3332static int 3333sync_print(ap) 3334 struct vop_print_args /* { 3335 struct vnode *a_vp; 3336 } */ *ap; 3337{ 3338 struct vnode *vp = ap->a_vp; 3339 3340 printf("syncer vnode"); 3341 if (vp->v_vnlock != NULL) 3342 lockmgr_printinfo(vp->v_vnlock); 3343 printf("\n"); 3344 return (0); 3345} 3346 3347/* 3348 * extract the dev_t from a VCHR 3349 */ 3350dev_t 3351vn_todev(vp) 3352 struct vnode *vp; 3353{ 3354 if (vp->v_type != VCHR) 3355 return (NODEV); 3356 return (vp->v_rdev); 3357} 3358 3359/* 3360 * Check if vnode represents a disk device 3361 */ 3362int 3363vn_isdisk(vp, errp) 3364 struct vnode *vp; 3365 int *errp; 3366{ 3367 struct cdevsw *cdevsw; 3368 3369 if (vp->v_type != VCHR) { 3370 if (errp != NULL) 3371 *errp = ENOTBLK; 3372 return (0); 3373 } 3374 if (vp->v_rdev == NULL) { 3375 if (errp != NULL) 3376 *errp = ENXIO; 3377 return (0); 3378 } 3379 cdevsw = devsw(vp->v_rdev); 3380 if (cdevsw == NULL) { 3381 if (errp != NULL) 3382 *errp = ENXIO; 3383 return (0); 3384 } 3385 if (!(cdevsw->d_flags & D_DISK)) { 3386 if (errp != NULL) 3387 *errp = ENOTBLK; 3388 return (0); 3389 } 3390 if (errp != NULL) 3391 *errp = 0; 3392 return (1); 3393} 3394 3395/* 3396 * Free data allocated by namei(); see namei(9) for details. 3397 */ 3398void 3399NDFREE(ndp, flags) 3400 struct nameidata *ndp; 3401 const uint flags; 3402{ 3403 if (!(flags & NDF_NO_FREE_PNBUF) && 3404 (ndp->ni_cnd.cn_flags & HASBUF)) { 3405 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 3406 ndp->ni_cnd.cn_flags &= ~HASBUF; 3407 } 3408 if (!(flags & NDF_NO_DVP_UNLOCK) && 3409 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 3410 ndp->ni_dvp != ndp->ni_vp) 3411 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 3412 if (!(flags & NDF_NO_DVP_RELE) && 3413 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3414 vrele(ndp->ni_dvp); 3415 ndp->ni_dvp = NULL; 3416 } 3417 if (!(flags & NDF_NO_VP_UNLOCK) && 3418 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3419 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 3420 if (!(flags & NDF_NO_VP_RELE) && 3421 ndp->ni_vp) { 3422 vrele(ndp->ni_vp); 3423 ndp->ni_vp = NULL; 3424 } 3425 if (!(flags & NDF_NO_STARTDIR_RELE) && 3426 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3427 vrele(ndp->ni_startdir); 3428 ndp->ni_startdir = NULL; 3429 } 3430} 3431 3432/* 3433 * Common filesystem object access control check routine. Accepts a 3434 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3435 * and optional call-by-reference privused argument allowing vaccess() 3436 * to indicate to the caller whether privilege was used to satisfy the 3437 * request (obsoleted). Returns 0 on success, or an errno on failure. 3438 */ 3439int 3440vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3441 enum vtype type; 3442 mode_t file_mode; 3443 uid_t file_uid; 3444 gid_t file_gid; 3445 mode_t acc_mode; 3446 struct ucred *cred; 3447 int *privused; 3448{ 3449 mode_t dac_granted; 3450#ifdef CAPABILITIES 3451 mode_t cap_granted; 3452#endif 3453 3454 /* 3455 * Look for a normal, non-privileged way to access the file/directory 3456 * as requested. If it exists, go with that. 3457 */ 3458 3459 if (privused != NULL) 3460 *privused = 0; 3461 3462 dac_granted = 0; 3463 3464 /* Check the owner. */ 3465 if (cred->cr_uid == file_uid) { 3466 dac_granted |= VADMIN; 3467 if (file_mode & S_IXUSR) 3468 dac_granted |= VEXEC; 3469 if (file_mode & S_IRUSR) 3470 dac_granted |= VREAD; 3471 if (file_mode & S_IWUSR) 3472 dac_granted |= (VWRITE | VAPPEND); 3473 3474 if ((acc_mode & dac_granted) == acc_mode) 3475 return (0); 3476 3477 goto privcheck; 3478 } 3479 3480 /* Otherwise, check the groups (first match) */ 3481 if (groupmember(file_gid, cred)) { 3482 if (file_mode & S_IXGRP) 3483 dac_granted |= VEXEC; 3484 if (file_mode & S_IRGRP) 3485 dac_granted |= VREAD; 3486 if (file_mode & S_IWGRP) 3487 dac_granted |= (VWRITE | VAPPEND); 3488 3489 if ((acc_mode & dac_granted) == acc_mode) 3490 return (0); 3491 3492 goto privcheck; 3493 } 3494 3495 /* Otherwise, check everyone else. */ 3496 if (file_mode & S_IXOTH) 3497 dac_granted |= VEXEC; 3498 if (file_mode & S_IROTH) 3499 dac_granted |= VREAD; 3500 if (file_mode & S_IWOTH) 3501 dac_granted |= (VWRITE | VAPPEND); 3502 if ((acc_mode & dac_granted) == acc_mode) 3503 return (0); 3504 3505privcheck: 3506 if (!suser_cred(cred, PRISON_ROOT)) { 3507 /* XXX audit: privilege used */ 3508 if (privused != NULL) 3509 *privused = 1; 3510 return (0); 3511 } 3512 3513#ifdef CAPABILITIES 3514 /* 3515 * Build a capability mask to determine if the set of capabilities 3516 * satisfies the requirements when combined with the granted mask 3517 * from above. 3518 * For each capability, if the capability is required, bitwise 3519 * or the request type onto the cap_granted mask. 3520 */ 3521 cap_granted = 0; 3522 3523 if (type == VDIR) { 3524 /* 3525 * For directories, use CAP_DAC_READ_SEARCH to satisfy 3526 * VEXEC requests, instead of CAP_DAC_EXECUTE. 3527 */ 3528 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3529 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3530 cap_granted |= VEXEC; 3531 } else { 3532 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3533 !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT)) 3534 cap_granted |= VEXEC; 3535 } 3536 3537 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3538 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3539 cap_granted |= VREAD; 3540 3541 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3542 !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT)) 3543 cap_granted |= (VWRITE | VAPPEND); 3544 3545 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3546 !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT)) 3547 cap_granted |= VADMIN; 3548 3549 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3550 /* XXX audit: privilege used */ 3551 if (privused != NULL) 3552 *privused = 1; 3553 return (0); 3554 } 3555#endif 3556 3557 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3558} 3559