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