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