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