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