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