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 1550 mtx_lock(&Giant); 1551 last_work_seen = 0; 1552 syncer_final_iter = 0; 1553 syncer_state = SYNCER_RUNNING; 1554 starttime = time_second; 1555 1556 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 1557 SHUTDOWN_PRI_LAST); 1558 1559 for (;;) { 1560 mtx_lock(&sync_mtx); 1561 if (syncer_state == SYNCER_FINAL_DELAY && 1562 syncer_final_iter == 0) { 1563 mtx_unlock(&sync_mtx); 1564 kthread_suspend_check(td->td_proc); 1565 mtx_lock(&sync_mtx); 1566 } 1567 net_worklist_len = syncer_worklist_len - sync_vnode_count; 1568 if (syncer_state != SYNCER_RUNNING && starttime != time_second) 1569 printf("%d ", net_worklist_len); 1570 starttime = time_second; 1571 1572 /* 1573 * Push files whose dirty time has expired. Be careful 1574 * of interrupt race on slp queue. 1575 * 1576 * Skip over empty worklist slots when shutting down. 1577 */ 1578 do { 1579 slp = &syncer_workitem_pending[syncer_delayno]; 1580 syncer_delayno += 1; 1581 if (syncer_delayno == syncer_maxdelay) 1582 syncer_delayno = 0; 1583 next = &syncer_workitem_pending[syncer_delayno]; 1584 /* 1585 * If the worklist has wrapped since the 1586 * it was emptied of all but syncer vnodes, 1587 * switch to the FINAL_DELAY state and run 1588 * for one more second. 1589 */ 1590 if (syncer_state == SYNCER_SHUTTING_DOWN && 1591 net_worklist_len == 0 && 1592 last_work_seen == syncer_delayno) { 1593 syncer_state = SYNCER_FINAL_DELAY; 1594 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 1595 } 1596 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 1597 syncer_worklist_len > 0); 1598 1599 /* 1600 * Keep track of the last time there was anything 1601 * on the worklist other than syncer vnodes. 1602 * Return to the SHUTTING_DOWN state if any 1603 * new work appears. 1604 */ 1605 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 1606 last_work_seen = syncer_delayno; 1607 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 1608 syncer_state = SYNCER_SHUTTING_DOWN; 1609 while ((vp = LIST_FIRST(slp)) != NULL) { 1610 if (VOP_ISLOCKED(vp, NULL) != 0 || 1611 vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1612 LIST_REMOVE(vp, v_synclist); 1613 LIST_INSERT_HEAD(next, vp, v_synclist); 1614 continue; 1615 } 1616 if (VI_TRYLOCK(vp) == 0) { 1617 LIST_REMOVE(vp, v_synclist); 1618 LIST_INSERT_HEAD(next, vp, v_synclist); 1619 vn_finished_write(mp); 1620 continue; 1621 } 1622 /* 1623 * We use vhold in case the vnode does not 1624 * successfully sync. vhold prevents the vnode from 1625 * going away when we unlock the sync_mtx so that 1626 * we can acquire the vnode interlock. 1627 */ 1628 vholdl(vp); 1629 mtx_unlock(&sync_mtx); 1630 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td); 1631 (void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td); 1632 VOP_UNLOCK(vp, 0, td); 1633 vn_finished_write(mp); 1634 VI_LOCK(vp); 1635 if ((vp->v_iflag & VI_ONWORKLST) != 0) { 1636 /* 1637 * Put us back on the worklist. The worklist 1638 * routine will remove us from our current 1639 * position and then add us back in at a later 1640 * position. 1641 */ 1642 vn_syncer_add_to_worklist(vp, syncdelay); 1643 } 1644 vdropl(vp); 1645 VI_UNLOCK(vp); 1646 mtx_lock(&sync_mtx); 1647 } 1648 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 1649 syncer_final_iter--; 1650 mtx_unlock(&sync_mtx); 1651 1652 /* 1653 * Do soft update processing. 1654 */ 1655 if (softdep_process_worklist_hook != NULL) 1656 (*softdep_process_worklist_hook)(NULL); 1657 1658 /* 1659 * The variable rushjob allows the kernel to speed up the 1660 * processing of the filesystem syncer process. A rushjob 1661 * value of N tells the filesystem syncer to process the next 1662 * N seconds worth of work on its queue ASAP. Currently rushjob 1663 * is used by the soft update code to speed up the filesystem 1664 * syncer process when the incore state is getting so far 1665 * ahead of the disk that the kernel memory pool is being 1666 * threatened with exhaustion. 1667 */ 1668 mtx_lock(&sync_mtx); 1669 if (rushjob > 0) { 1670 rushjob -= 1; 1671 mtx_unlock(&sync_mtx); 1672 continue; 1673 } 1674 mtx_unlock(&sync_mtx); 1675 /* 1676 * Just sleep for a short period if time between 1677 * iterations when shutting down to allow some I/O 1678 * to happen. 1679 * 1680 * If it has taken us less than a second to process the 1681 * current work, then wait. Otherwise start right over 1682 * again. We can still lose time if any single round 1683 * takes more than two seconds, but it does not really 1684 * matter as we are just trying to generally pace the 1685 * filesystem activity. 1686 */ 1687 if (syncer_state != SYNCER_RUNNING) 1688 tsleep(&dummychan, PPAUSE, "syncfnl", 1689 hz / SYNCER_SHUTDOWN_SPEEDUP); 1690 else if (time_second == starttime) 1691 tsleep(&lbolt, PPAUSE, "syncer", 0); 1692 } 1693 } 1694 1695 /* 1696 * Request the syncer daemon to speed up its work. 1697 * We never push it to speed up more than half of its 1698 * normal turn time, otherwise it could take over the cpu. 1699 */ 1700 int 1701 speedup_syncer() 1702 { 1703 struct thread *td; 1704 int ret = 0; 1705 1706 td = FIRST_THREAD_IN_PROC(updateproc); 1707 sleepq_remove(td, &lbolt); 1708 mtx_lock(&sync_mtx); 1709 if (rushjob < syncdelay / 2) { 1710 rushjob += 1; 1711 stat_rush_requests += 1; 1712 ret = 1; 1713 } 1714 mtx_unlock(&sync_mtx); 1715 return (ret); 1716 } 1717 1718 /* 1719 * Tell the syncer to speed up its work and run though its work 1720 * list several times, then tell it to shut down. 1721 */ 1722 static void 1723 syncer_shutdown(void *arg, int howto) 1724 { 1725 struct thread *td; 1726 1727 td = FIRST_THREAD_IN_PROC(updateproc); 1728 sleepq_remove(td, &lbolt); 1729 mtx_lock(&sync_mtx); 1730 syncer_state = SYNCER_SHUTTING_DOWN; 1731 rushjob = 0; 1732 mtx_unlock(&sync_mtx); 1733 kproc_shutdown(arg, howto); 1734 } 1735 1736 /* 1737 * Associate a p-buffer with a vnode. 1738 * 1739 * Also sets B_PAGING flag to indicate that vnode is not fully associated 1740 * with the buffer. i.e. the bp has not been linked into the vnode or 1741 * ref-counted. 1742 */ 1743 void 1744 pbgetvp(vp, bp) 1745 register struct vnode *vp; 1746 register struct buf *bp; 1747 { 1748 1749 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 1750 1751 bp->b_vp = vp; 1752 bp->b_object = vp->v_object; 1753 bp->b_flags |= B_PAGING; 1754 bp->b_dev = vn_todev(vp); 1755 } 1756 1757 /* 1758 * Disassociate a p-buffer from a vnode. 1759 */ 1760 void 1761 pbrelvp(bp) 1762 register struct buf *bp; 1763 { 1764 1765 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 1766 1767 /* XXX REMOVE ME */ 1768 VI_LOCK(bp->b_vp); 1769 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) { 1770 panic( 1771 "relpbuf(): b_vp was probably reassignbuf()d %p %x", 1772 bp, 1773 (int)bp->b_flags 1774 ); 1775 } 1776 VI_UNLOCK(bp->b_vp); 1777 bp->b_vp = (struct vnode *) 0; 1778 bp->b_object = NULL; 1779 bp->b_flags &= ~B_PAGING; 1780 } 1781 1782 /* 1783 * Reassign a buffer from one vnode to another. 1784 * Used to assign file specific control information 1785 * (indirect blocks) to the vnode to which they belong. 1786 */ 1787 void 1788 reassignbuf(bp, newvp) 1789 register struct buf *bp; 1790 register struct vnode *newvp; 1791 { 1792 struct vnode *vp; 1793 int delay; 1794 1795 if (newvp == NULL) { 1796 printf("reassignbuf: NULL"); 1797 return; 1798 } 1799 vp = bp->b_vp; 1800 ++reassignbufcalls; 1801 1802 /* 1803 * B_PAGING flagged buffers cannot be reassigned because their vp 1804 * is not fully linked in. 1805 */ 1806 if (bp->b_flags & B_PAGING) 1807 panic("cannot reassign paging buffer"); 1808 1809 /* 1810 * Delete from old vnode list, if on one. 1811 */ 1812 VI_LOCK(vp); 1813 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 1814 buf_vlist_remove(bp); 1815 if (vp != newvp) { 1816 vdropl(bp->b_vp); 1817 bp->b_vp = NULL; /* for clarification */ 1818 } 1819 } 1820 if (vp != newvp) { 1821 VI_UNLOCK(vp); 1822 VI_LOCK(newvp); 1823 } 1824 /* 1825 * If dirty, put on list of dirty buffers; otherwise insert onto list 1826 * of clean buffers. 1827 */ 1828 if (bp->b_flags & B_DELWRI) { 1829 if ((newvp->v_iflag & VI_ONWORKLST) == 0) { 1830 switch (newvp->v_type) { 1831 case VDIR: 1832 delay = dirdelay; 1833 break; 1834 case VCHR: 1835 delay = metadelay; 1836 break; 1837 default: 1838 delay = filedelay; 1839 } 1840 vn_syncer_add_to_worklist(newvp, delay); 1841 } 1842 buf_vlist_add(bp, newvp, BX_VNDIRTY); 1843 } else { 1844 buf_vlist_add(bp, newvp, BX_VNCLEAN); 1845 1846 if ((newvp->v_iflag & VI_ONWORKLST) && 1847 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) { 1848 mtx_lock(&sync_mtx); 1849 LIST_REMOVE(newvp, v_synclist); 1850 syncer_worklist_len--; 1851 mtx_unlock(&sync_mtx); 1852 newvp->v_iflag &= ~VI_ONWORKLST; 1853 } 1854 } 1855 if (bp->b_vp != newvp) { 1856 bp->b_vp = newvp; 1857 vholdl(bp->b_vp); 1858 } 1859 VI_UNLOCK(newvp); 1860 } 1861 1862 /* 1863 * Create a vnode for a device. 1864 * Used for mounting the root filesystem. 1865 */ 1866 int 1867 bdevvp(dev, vpp) 1868 struct cdev *dev; 1869 struct vnode **vpp; 1870 { 1871 register struct vnode *vp; 1872 struct vnode *nvp; 1873 int error; 1874 1875 if (dev == NULL) { 1876 *vpp = NULLVP; 1877 return (ENXIO); 1878 } 1879 if (vfinddev(dev, vpp)) 1880 return (0); 1881 1882 error = getnewvnode("none", (struct mount *)0, spec_vnodeop_p, &nvp); 1883 if (error) { 1884 *vpp = NULLVP; 1885 return (error); 1886 } 1887 vp = nvp; 1888 vp->v_type = VCHR; 1889 vp->v_bsize = DEV_BSIZE; 1890 addalias(vp, dev); 1891 *vpp = vp; 1892 return (0); 1893 } 1894 1895 static void 1896 v_incr_usecount(struct vnode *vp, int delta) 1897 { 1898 1899 vp->v_usecount += delta; 1900 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 1901 mtx_lock(&spechash_mtx); 1902 vp->v_rdev->si_usecount += delta; 1903 mtx_unlock(&spechash_mtx); 1904 } 1905 } 1906 1907 /* 1908 * Add vnode to the alias list hung off the struct cdev *. 1909 * 1910 * The reason for this gunk is that multiple vnodes can reference 1911 * the same physical device, so checking vp->v_usecount to see 1912 * how many users there are is inadequate; the v_usecount for 1913 * the vnodes need to be accumulated. vcount() does that. 1914 */ 1915 struct vnode * 1916 addaliasu(nvp, nvp_rdev) 1917 struct vnode *nvp; 1918 dev_t nvp_rdev; 1919 { 1920 struct vnode *ovp; 1921 vop_t **ops; 1922 struct cdev *dev; 1923 1924 if (nvp->v_type == VBLK) 1925 return (nvp); 1926 if (nvp->v_type != VCHR) 1927 panic("addaliasu on non-special vnode"); 1928 dev = findcdev(nvp_rdev); 1929 if (dev == NULL) 1930 return (nvp); 1931 /* 1932 * Check to see if we have a bdevvp vnode with no associated 1933 * filesystem. If so, we want to associate the filesystem of 1934 * the new newly instigated vnode with the bdevvp vnode and 1935 * discard the newly created vnode rather than leaving the 1936 * bdevvp vnode lying around with no associated filesystem. 1937 */ 1938 if (vfinddev(dev, &ovp) == 0 || ovp->v_data != NULL) { 1939 addalias(nvp, dev); 1940 return (nvp); 1941 } 1942 /* 1943 * Discard unneeded vnode, but save its node specific data. 1944 * Note that if there is a lock, it is carried over in the 1945 * node specific data to the replacement vnode. 1946 */ 1947 vref(ovp); 1948 ovp->v_data = nvp->v_data; 1949 ovp->v_tag = nvp->v_tag; 1950 nvp->v_data = NULL; 1951 lockdestroy(ovp->v_vnlock); 1952 lockinit(ovp->v_vnlock, PVFS, nvp->v_vnlock->lk_wmesg, 1953 nvp->v_vnlock->lk_timo, nvp->v_vnlock->lk_flags & LK_EXTFLG_MASK); 1954 ops = ovp->v_op; 1955 ovp->v_op = nvp->v_op; 1956 if (VOP_ISLOCKED(nvp, curthread)) { 1957 VOP_UNLOCK(nvp, 0, curthread); 1958 vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread); 1959 } 1960 nvp->v_op = ops; 1961 delmntque(ovp); 1962 insmntque(ovp, nvp->v_mount); 1963 vrele(nvp); 1964 vgone(nvp); 1965 return (ovp); 1966 } 1967 1968 /* This is a local helper function that do the same as addaliasu, but for a 1969 * struct cdev *instead of an dev_t. */ 1970 static void 1971 addalias(nvp, dev) 1972 struct vnode *nvp; 1973 struct cdev *dev; 1974 { 1975 1976 KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode")); 1977 dev_ref(dev); 1978 nvp->v_rdev = dev; 1979 VI_LOCK(nvp); 1980 mtx_lock(&spechash_mtx); 1981 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext); 1982 dev->si_usecount += nvp->v_usecount; 1983 mtx_unlock(&spechash_mtx); 1984 VI_UNLOCK(nvp); 1985 } 1986 1987 /* 1988 * Grab a particular vnode from the free list, increment its 1989 * reference count and lock it. The vnode lock bit is set if the 1990 * vnode is being eliminated in vgone. The process is awakened 1991 * when the transition is completed, and an error returned to 1992 * indicate that the vnode is no longer usable (possibly having 1993 * been changed to a new filesystem type). 1994 */ 1995 int 1996 vget(vp, flags, td) 1997 register struct vnode *vp; 1998 int flags; 1999 struct thread *td; 2000 { 2001 int error; 2002 2003 /* 2004 * If the vnode is in the process of being cleaned out for 2005 * another use, we wait for the cleaning to finish and then 2006 * return failure. Cleaning is determined by checking that 2007 * the VI_XLOCK flag is set. 2008 */ 2009 if ((flags & LK_INTERLOCK) == 0) 2010 VI_LOCK(vp); 2011 if (vp->v_iflag & VI_XLOCK && vp->v_vxthread != curthread) { 2012 if ((flags & LK_NOWAIT) == 0) { 2013 vp->v_iflag |= VI_XWANT; 2014 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0); 2015 return (ENOENT); 2016 } 2017 VI_UNLOCK(vp); 2018 return (EBUSY); 2019 } 2020 2021 v_incr_usecount(vp, 1); 2022 2023 if (VSHOULDBUSY(vp)) 2024 vbusy(vp); 2025 if (flags & LK_TYPE_MASK) { 2026 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) { 2027 /* 2028 * must expand vrele here because we do not want 2029 * to call VOP_INACTIVE if the reference count 2030 * drops back to zero since it was never really 2031 * active. We must remove it from the free list 2032 * before sleeping so that multiple processes do 2033 * not try to recycle it. 2034 */ 2035 VI_LOCK(vp); 2036 v_incr_usecount(vp, -1); 2037 if (VSHOULDFREE(vp)) 2038 vfree(vp); 2039 else 2040 vlruvp(vp); 2041 VI_UNLOCK(vp); 2042 } 2043 return (error); 2044 } 2045 VI_UNLOCK(vp); 2046 return (0); 2047 } 2048 2049 /* 2050 * Increase the reference count of a vnode. 2051 */ 2052 void 2053 vref(struct vnode *vp) 2054 { 2055 2056 VI_LOCK(vp); 2057 v_incr_usecount(vp, 1); 2058 VI_UNLOCK(vp); 2059 } 2060 2061 /* 2062 * Return reference count of a vnode. 2063 * 2064 * The results of this call are only guaranteed when some mechanism other 2065 * than the VI lock is used to stop other processes from gaining references 2066 * to the vnode. This may be the case if the caller holds the only reference. 2067 * This is also useful when stale data is acceptable as race conditions may 2068 * be accounted for by some other means. 2069 */ 2070 int 2071 vrefcnt(struct vnode *vp) 2072 { 2073 int usecnt; 2074 2075 VI_LOCK(vp); 2076 usecnt = vp->v_usecount; 2077 VI_UNLOCK(vp); 2078 2079 return (usecnt); 2080 } 2081 2082 2083 /* 2084 * Vnode put/release. 2085 * If count drops to zero, call inactive routine and return to freelist. 2086 */ 2087 void 2088 vrele(vp) 2089 struct vnode *vp; 2090 { 2091 struct thread *td = curthread; /* XXX */ 2092 2093 GIANT_REQUIRED; 2094 2095 KASSERT(vp != NULL, ("vrele: null vp")); 2096 2097 VI_LOCK(vp); 2098 2099 /* Skip this v_writecount check if we're going to panic below. */ 2100 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2101 ("vrele: missed vn_close")); 2102 2103 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2104 vp->v_usecount == 1)) { 2105 v_incr_usecount(vp, -1); 2106 VI_UNLOCK(vp); 2107 2108 return; 2109 } 2110 2111 if (vp->v_usecount == 1) { 2112 v_incr_usecount(vp, -1); 2113 /* 2114 * We must call VOP_INACTIVE with the node locked. Mark 2115 * as VI_DOINGINACT to avoid recursion. 2116 */ 2117 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) { 2118 VI_LOCK(vp); 2119 vp->v_iflag |= VI_DOINGINACT; 2120 VI_UNLOCK(vp); 2121 VOP_INACTIVE(vp, td); 2122 VI_LOCK(vp); 2123 KASSERT(vp->v_iflag & VI_DOINGINACT, 2124 ("vrele: lost VI_DOINGINACT")); 2125 vp->v_iflag &= ~VI_DOINGINACT; 2126 } else 2127 VI_LOCK(vp); 2128 if (VSHOULDFREE(vp)) 2129 vfree(vp); 2130 else 2131 vlruvp(vp); 2132 VI_UNLOCK(vp); 2133 2134 } else { 2135 #ifdef DIAGNOSTIC 2136 vprint("vrele: negative ref count", vp); 2137 #endif 2138 VI_UNLOCK(vp); 2139 panic("vrele: negative ref cnt"); 2140 } 2141 } 2142 2143 /* 2144 * Release an already locked vnode. This give the same effects as 2145 * unlock+vrele(), but takes less time and avoids releasing and 2146 * re-aquiring the lock (as vrele() aquires the lock internally.) 2147 */ 2148 void 2149 vput(vp) 2150 struct vnode *vp; 2151 { 2152 struct thread *td = curthread; /* XXX */ 2153 2154 GIANT_REQUIRED; 2155 2156 KASSERT(vp != NULL, ("vput: null vp")); 2157 VI_LOCK(vp); 2158 /* Skip this v_writecount check if we're going to panic below. */ 2159 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2160 ("vput: missed vn_close")); 2161 2162 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2163 vp->v_usecount == 1)) { 2164 v_incr_usecount(vp, -1); 2165 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2166 return; 2167 } 2168 2169 if (vp->v_usecount == 1) { 2170 v_incr_usecount(vp, -1); 2171 /* 2172 * We must call VOP_INACTIVE with the node locked, so 2173 * we just need to release the vnode mutex. Mark as 2174 * as VI_DOINGINACT to avoid recursion. 2175 */ 2176 vp->v_iflag |= VI_DOINGINACT; 2177 VI_UNLOCK(vp); 2178 VOP_INACTIVE(vp, td); 2179 VI_LOCK(vp); 2180 KASSERT(vp->v_iflag & VI_DOINGINACT, 2181 ("vput: lost VI_DOINGINACT")); 2182 vp->v_iflag &= ~VI_DOINGINACT; 2183 if (VSHOULDFREE(vp)) 2184 vfree(vp); 2185 else 2186 vlruvp(vp); 2187 VI_UNLOCK(vp); 2188 2189 } else { 2190 #ifdef DIAGNOSTIC 2191 vprint("vput: negative ref count", vp); 2192 #endif 2193 panic("vput: negative ref cnt"); 2194 } 2195 } 2196 2197 /* 2198 * Somebody doesn't want the vnode recycled. 2199 */ 2200 void 2201 vhold(struct vnode *vp) 2202 { 2203 2204 VI_LOCK(vp); 2205 vholdl(vp); 2206 VI_UNLOCK(vp); 2207 } 2208 2209 void 2210 vholdl(vp) 2211 register struct vnode *vp; 2212 { 2213 2214 vp->v_holdcnt++; 2215 if (VSHOULDBUSY(vp)) 2216 vbusy(vp); 2217 } 2218 2219 /* 2220 * Note that there is one less who cares about this vnode. vdrop() is the 2221 * opposite of vhold(). 2222 */ 2223 void 2224 vdrop(struct vnode *vp) 2225 { 2226 2227 VI_LOCK(vp); 2228 vdropl(vp); 2229 VI_UNLOCK(vp); 2230 } 2231 2232 void 2233 vdropl(vp) 2234 register struct vnode *vp; 2235 { 2236 2237 if (vp->v_holdcnt <= 0) 2238 panic("vdrop: holdcnt"); 2239 vp->v_holdcnt--; 2240 if (VSHOULDFREE(vp)) 2241 vfree(vp); 2242 else 2243 vlruvp(vp); 2244 } 2245 2246 /* 2247 * Remove any vnodes in the vnode table belonging to mount point mp. 2248 * 2249 * If FORCECLOSE is not specified, there should not be any active ones, 2250 * return error if any are found (nb: this is a user error, not a 2251 * system error). If FORCECLOSE is specified, detach any active vnodes 2252 * that are found. 2253 * 2254 * If WRITECLOSE is set, only flush out regular file vnodes open for 2255 * writing. 2256 * 2257 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2258 * 2259 * `rootrefs' specifies the base reference count for the root vnode 2260 * of this filesystem. The root vnode is considered busy if its 2261 * v_usecount exceeds this value. On a successful return, vflush(, td) 2262 * will call vrele() on the root vnode exactly rootrefs times. 2263 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2264 * be zero. 2265 */ 2266 #ifdef DIAGNOSTIC 2267 static int busyprt = 0; /* print out busy vnodes */ 2268 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2269 #endif 2270 2271 int 2272 vflush(mp, rootrefs, flags, td) 2273 struct mount *mp; 2274 int rootrefs; 2275 int flags; 2276 struct thread *td; 2277 { 2278 struct vnode *vp, *nvp, *rootvp = NULL; 2279 struct vattr vattr; 2280 int busy = 0, error; 2281 2282 if (rootrefs > 0) { 2283 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2284 ("vflush: bad args")); 2285 /* 2286 * Get the filesystem root vnode. We can vput() it 2287 * immediately, since with rootrefs > 0, it won't go away. 2288 */ 2289 if ((error = VFS_ROOT(mp, &rootvp, td)) != 0) 2290 return (error); 2291 vput(rootvp); 2292 2293 } 2294 MNT_ILOCK(mp); 2295 loop: 2296 MNT_VNODE_FOREACH(vp, mp, nvp) { 2297 2298 VI_LOCK(vp); 2299 MNT_IUNLOCK(mp); 2300 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td); 2301 if (error) { 2302 MNT_ILOCK(mp); 2303 goto loop; 2304 } 2305 /* 2306 * Skip over a vnodes marked VV_SYSTEM. 2307 */ 2308 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2309 VOP_UNLOCK(vp, 0, td); 2310 MNT_ILOCK(mp); 2311 continue; 2312 } 2313 /* 2314 * If WRITECLOSE is set, flush out unlinked but still open 2315 * files (even if open only for reading) and regular file 2316 * vnodes open for writing. 2317 */ 2318 if (flags & WRITECLOSE) { 2319 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td); 2320 VI_LOCK(vp); 2321 2322 if ((vp->v_type == VNON || 2323 (error == 0 && vattr.va_nlink > 0)) && 2324 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2325 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2326 MNT_ILOCK(mp); 2327 continue; 2328 } 2329 } else 2330 VI_LOCK(vp); 2331 2332 VOP_UNLOCK(vp, 0, td); 2333 2334 /* 2335 * With v_usecount == 0, all we need to do is clear out the 2336 * vnode data structures and we are done. 2337 */ 2338 if (vp->v_usecount == 0) { 2339 vgonel(vp, td); 2340 MNT_ILOCK(mp); 2341 continue; 2342 } 2343 2344 /* 2345 * If FORCECLOSE is set, forcibly close the vnode. For block 2346 * or character devices, revert to an anonymous device. For 2347 * all other files, just kill them. 2348 */ 2349 if (flags & FORCECLOSE) { 2350 if (vp->v_type != VCHR) 2351 vgonel(vp, td); 2352 else 2353 vgonechrl(vp, td); 2354 MNT_ILOCK(mp); 2355 continue; 2356 } 2357 #ifdef DIAGNOSTIC 2358 if (busyprt) 2359 vprint("vflush: busy vnode", vp); 2360 #endif 2361 VI_UNLOCK(vp); 2362 MNT_ILOCK(mp); 2363 busy++; 2364 } 2365 MNT_IUNLOCK(mp); 2366 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2367 /* 2368 * If just the root vnode is busy, and if its refcount 2369 * is equal to `rootrefs', then go ahead and kill it. 2370 */ 2371 VI_LOCK(rootvp); 2372 KASSERT(busy > 0, ("vflush: not busy")); 2373 KASSERT(rootvp->v_usecount >= rootrefs, 2374 ("vflush: usecount %d < rootrefs %d", 2375 rootvp->v_usecount, rootrefs)); 2376 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2377 vgonel(rootvp, td); 2378 busy = 0; 2379 } else 2380 VI_UNLOCK(rootvp); 2381 } 2382 if (busy) 2383 return (EBUSY); 2384 for (; rootrefs > 0; rootrefs--) 2385 vrele(rootvp); 2386 return (0); 2387 } 2388 2389 /* 2390 * This moves a now (likely recyclable) vnode to the end of the 2391 * mountlist. XXX However, it is temporarily disabled until we 2392 * can clean up ffs_sync() and friends, which have loop restart 2393 * conditions which this code causes to operate O(N^2). 2394 */ 2395 static void 2396 vlruvp(struct vnode *vp) 2397 { 2398 #if 0 2399 struct mount *mp; 2400 2401 if ((mp = vp->v_mount) != NULL) { 2402 MNT_ILOCK(mp); 2403 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2404 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2405 MNT_IUNLOCK(mp); 2406 } 2407 #endif 2408 } 2409 2410 static void 2411 vx_lock(struct vnode *vp) 2412 { 2413 2414 ASSERT_VI_LOCKED(vp, "vx_lock"); 2415 2416 /* 2417 * Prevent the vnode from being recycled or brought into use while we 2418 * clean it out. 2419 */ 2420 if (vp->v_iflag & VI_XLOCK) 2421 panic("vclean: deadlock"); 2422 vp->v_iflag |= VI_XLOCK; 2423 vp->v_vxthread = curthread; 2424 } 2425 2426 static void 2427 vx_unlock(struct vnode *vp) 2428 { 2429 ASSERT_VI_LOCKED(vp, "vx_unlock"); 2430 vp->v_iflag &= ~VI_XLOCK; 2431 vp->v_vxthread = NULL; 2432 if (vp->v_iflag & VI_XWANT) { 2433 vp->v_iflag &= ~VI_XWANT; 2434 wakeup(vp); 2435 } 2436 } 2437 2438 /* 2439 * Disassociate the underlying filesystem from a vnode. 2440 */ 2441 static void 2442 vclean(vp, flags, td) 2443 struct vnode *vp; 2444 int flags; 2445 struct thread *td; 2446 { 2447 int active; 2448 2449 ASSERT_VI_LOCKED(vp, "vclean"); 2450 /* 2451 * Check to see if the vnode is in use. If so we have to reference it 2452 * before we clean it out so that its count cannot fall to zero and 2453 * generate a race against ourselves to recycle it. 2454 */ 2455 if ((active = vp->v_usecount)) 2456 v_incr_usecount(vp, 1); 2457 2458 /* 2459 * Even if the count is zero, the VOP_INACTIVE routine may still 2460 * have the object locked while it cleans it out. The VOP_LOCK 2461 * ensures that the VOP_INACTIVE routine is done with its work. 2462 * For active vnodes, it ensures that no other activity can 2463 * occur while the underlying object is being cleaned out. 2464 */ 2465 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 2466 2467 /* 2468 * Clean out any buffers associated with the vnode. 2469 * If the flush fails, just toss the buffers. 2470 */ 2471 if (flags & DOCLOSE) { 2472 struct buf *bp; 2473 bp = TAILQ_FIRST(&vp->v_dirtyblkhd); 2474 if (bp != NULL) 2475 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 2476 if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0) 2477 vinvalbuf(vp, 0, NOCRED, td, 0, 0); 2478 } 2479 2480 VOP_DESTROYVOBJECT(vp); 2481 2482 /* 2483 * Any other processes trying to obtain this lock must first 2484 * wait for VXLOCK to clear, then call the new lock operation. 2485 */ 2486 VOP_UNLOCK(vp, 0, td); 2487 2488 /* 2489 * If purging an active vnode, it must be closed and 2490 * deactivated before being reclaimed. Note that the 2491 * VOP_INACTIVE will unlock the vnode. 2492 */ 2493 if (active) { 2494 if (flags & DOCLOSE) 2495 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2496 VI_LOCK(vp); 2497 if ((vp->v_iflag & VI_DOINGINACT) == 0) { 2498 vp->v_iflag |= VI_DOINGINACT; 2499 VI_UNLOCK(vp); 2500 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0) 2501 panic("vclean: cannot relock."); 2502 VOP_INACTIVE(vp, td); 2503 VI_LOCK(vp); 2504 KASSERT(vp->v_iflag & VI_DOINGINACT, 2505 ("vclean: lost VI_DOINGINACT")); 2506 vp->v_iflag &= ~VI_DOINGINACT; 2507 } 2508 VI_UNLOCK(vp); 2509 } 2510 /* 2511 * Reclaim the vnode. 2512 */ 2513 if (VOP_RECLAIM(vp, td)) 2514 panic("vclean: cannot reclaim"); 2515 2516 if (active) { 2517 /* 2518 * Inline copy of vrele() since VOP_INACTIVE 2519 * has already been called. 2520 */ 2521 VI_LOCK(vp); 2522 v_incr_usecount(vp, -1); 2523 if (vp->v_usecount <= 0) { 2524 #ifdef INVARIANTS 2525 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 2526 vprint("vclean: bad ref count", vp); 2527 panic("vclean: ref cnt"); 2528 } 2529 #endif 2530 if (VSHOULDFREE(vp)) 2531 vfree(vp); 2532 } 2533 VI_UNLOCK(vp); 2534 } 2535 /* 2536 * Delete from old mount point vnode list. 2537 */ 2538 delmntque(vp); 2539 cache_purge(vp); 2540 VI_LOCK(vp); 2541 if (VSHOULDFREE(vp)) 2542 vfree(vp); 2543 2544 /* 2545 * Done with purge, reset to the standard lock and 2546 * notify sleepers of the grim news. 2547 */ 2548 vp->v_vnlock = &vp->v_lock; 2549 vp->v_op = dead_vnodeop_p; 2550 if (vp->v_pollinfo != NULL) 2551 vn_pollgone(vp); 2552 vp->v_tag = "none"; 2553 } 2554 2555 /* 2556 * Eliminate all activity associated with the requested vnode 2557 * and with all vnodes aliased to the requested vnode. 2558 */ 2559 int 2560 vop_revoke(ap) 2561 struct vop_revoke_args /* { 2562 struct vnode *a_vp; 2563 int a_flags; 2564 } */ *ap; 2565 { 2566 struct vnode *vp, *vq; 2567 struct cdev *dev; 2568 2569 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke")); 2570 vp = ap->a_vp; 2571 KASSERT((vp->v_type == VCHR), ("vop_revoke: not VCHR")); 2572 2573 VI_LOCK(vp); 2574 /* 2575 * If a vgone (or vclean) is already in progress, 2576 * wait until it is done and return. 2577 */ 2578 if (vp->v_iflag & VI_XLOCK) { 2579 vp->v_iflag |= VI_XWANT; 2580 msleep(vp, VI_MTX(vp), PINOD | PDROP, 2581 "vop_revokeall", 0); 2582 return (0); 2583 } 2584 VI_UNLOCK(vp); 2585 dev = vp->v_rdev; 2586 for (;;) { 2587 mtx_lock(&spechash_mtx); 2588 vq = SLIST_FIRST(&dev->si_hlist); 2589 mtx_unlock(&spechash_mtx); 2590 if (vq == NULL) 2591 break; 2592 vgone(vq); 2593 } 2594 return (0); 2595 } 2596 2597 /* 2598 * Recycle an unused vnode to the front of the free list. 2599 * Release the passed interlock if the vnode will be recycled. 2600 */ 2601 int 2602 vrecycle(vp, inter_lkp, td) 2603 struct vnode *vp; 2604 struct mtx *inter_lkp; 2605 struct thread *td; 2606 { 2607 2608 VI_LOCK(vp); 2609 if (vp->v_usecount == 0) { 2610 if (inter_lkp) { 2611 mtx_unlock(inter_lkp); 2612 } 2613 vgonel(vp, td); 2614 return (1); 2615 } 2616 VI_UNLOCK(vp); 2617 return (0); 2618 } 2619 2620 /* 2621 * Eliminate all activity associated with a vnode 2622 * in preparation for reuse. 2623 */ 2624 void 2625 vgone(vp) 2626 register struct vnode *vp; 2627 { 2628 struct thread *td = curthread; /* XXX */ 2629 2630 VI_LOCK(vp); 2631 vgonel(vp, td); 2632 } 2633 2634 /* 2635 * Disassociate a character device from the its underlying filesystem and 2636 * attach it to spec. This is for use when the chr device is still active 2637 * and the filesystem is going away. 2638 */ 2639 static void 2640 vgonechrl(struct vnode *vp, struct thread *td) 2641 { 2642 ASSERT_VI_LOCKED(vp, "vgonechrl"); 2643 vx_lock(vp); 2644 /* 2645 * This is a custom version of vclean() which does not tearm down 2646 * the bufs or vm objects held by this vnode. This allows filesystems 2647 * to continue using devices which were discovered via another 2648 * filesystem that has been unmounted. 2649 */ 2650 if (vp->v_usecount != 0) { 2651 v_incr_usecount(vp, 1); 2652 /* 2653 * Ensure that no other activity can occur while the 2654 * underlying object is being cleaned out. 2655 */ 2656 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 2657 /* 2658 * Any other processes trying to obtain this lock must first 2659 * wait for VXLOCK to clear, then call the new lock operation. 2660 */ 2661 VOP_UNLOCK(vp, 0, td); 2662 vp->v_vnlock = &vp->v_lock; 2663 vp->v_tag = "orphanchr"; 2664 vp->v_op = spec_vnodeop_p; 2665 delmntque(vp); 2666 cache_purge(vp); 2667 vrele(vp); 2668 VI_LOCK(vp); 2669 } else 2670 vclean(vp, 0, td); 2671 vp->v_op = spec_vnodeop_p; 2672 vx_unlock(vp); 2673 VI_UNLOCK(vp); 2674 } 2675 2676 /* 2677 * vgone, with the vp interlock held. 2678 */ 2679 void 2680 vgonel(vp, td) 2681 struct vnode *vp; 2682 struct thread *td; 2683 { 2684 /* 2685 * If a vgone (or vclean) is already in progress, 2686 * wait until it is done and return. 2687 */ 2688 ASSERT_VI_LOCKED(vp, "vgonel"); 2689 if (vp->v_iflag & VI_XLOCK) { 2690 vp->v_iflag |= VI_XWANT; 2691 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0); 2692 return; 2693 } 2694 vx_lock(vp); 2695 2696 /* 2697 * Clean out the filesystem specific data. 2698 */ 2699 vclean(vp, DOCLOSE, td); 2700 VI_UNLOCK(vp); 2701 2702 /* 2703 * If special device, remove it from special device alias list 2704 * if it is on one. 2705 */ 2706 VI_LOCK(vp); 2707 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 2708 mtx_lock(&spechash_mtx); 2709 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext); 2710 vp->v_rdev->si_usecount -= vp->v_usecount; 2711 mtx_unlock(&spechash_mtx); 2712 dev_rel(vp->v_rdev); 2713 vp->v_rdev = NULL; 2714 } 2715 2716 /* 2717 * If it is on the freelist and not already at the head, 2718 * move it to the head of the list. The test of the 2719 * VDOOMED flag and the reference count of zero is because 2720 * it will be removed from the free list by getnewvnode, 2721 * but will not have its reference count incremented until 2722 * after calling vgone. If the reference count were 2723 * incremented first, vgone would (incorrectly) try to 2724 * close the previous instance of the underlying object. 2725 */ 2726 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) { 2727 mtx_lock(&vnode_free_list_mtx); 2728 if (vp->v_iflag & VI_FREE) { 2729 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2730 } else { 2731 vp->v_iflag |= VI_FREE; 2732 freevnodes++; 2733 } 2734 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2735 mtx_unlock(&vnode_free_list_mtx); 2736 } 2737 2738 vp->v_type = VBAD; 2739 vx_unlock(vp); 2740 VI_UNLOCK(vp); 2741 } 2742 2743 /* 2744 * Lookup a vnode by device number. 2745 */ 2746 int 2747 vfinddev(dev, vpp) 2748 struct cdev *dev; 2749 struct vnode **vpp; 2750 { 2751 struct vnode *vp; 2752 2753 mtx_lock(&spechash_mtx); 2754 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 2755 *vpp = vp; 2756 mtx_unlock(&spechash_mtx); 2757 return (1); 2758 } 2759 mtx_unlock(&spechash_mtx); 2760 return (0); 2761 } 2762 2763 /* 2764 * Calculate the total number of references to a special device. 2765 */ 2766 int 2767 vcount(vp) 2768 struct vnode *vp; 2769 { 2770 int count; 2771 2772 mtx_lock(&spechash_mtx); 2773 count = vp->v_rdev->si_usecount; 2774 mtx_unlock(&spechash_mtx); 2775 return (count); 2776 } 2777 2778 /* 2779 * Same as above, but using the struct cdev *as argument 2780 */ 2781 int 2782 count_dev(dev) 2783 struct cdev *dev; 2784 { 2785 int count; 2786 2787 mtx_lock(&spechash_mtx); 2788 count = dev->si_usecount; 2789 mtx_unlock(&spechash_mtx); 2790 return(count); 2791 } 2792 2793 /* 2794 * Print out a description of a vnode. 2795 */ 2796 static char *typename[] = 2797 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2798 2799 void 2800 vprint(label, vp) 2801 char *label; 2802 struct vnode *vp; 2803 { 2804 char buf[96]; 2805 2806 if (label != NULL) 2807 printf("%s: %p: ", label, (void *)vp); 2808 else 2809 printf("%p: ", (void *)vp); 2810 printf("tag %s, type %s, usecount %d, writecount %d, refcount %d,", 2811 vp->v_tag, typename[vp->v_type], vp->v_usecount, 2812 vp->v_writecount, vp->v_holdcnt); 2813 buf[0] = '\0'; 2814 if (vp->v_vflag & VV_ROOT) 2815 strcat(buf, "|VV_ROOT"); 2816 if (vp->v_vflag & VV_TEXT) 2817 strcat(buf, "|VV_TEXT"); 2818 if (vp->v_vflag & VV_SYSTEM) 2819 strcat(buf, "|VV_SYSTEM"); 2820 if (vp->v_iflag & VI_XLOCK) 2821 strcat(buf, "|VI_XLOCK"); 2822 if (vp->v_iflag & VI_XWANT) 2823 strcat(buf, "|VI_XWANT"); 2824 if (vp->v_iflag & VI_BWAIT) 2825 strcat(buf, "|VI_BWAIT"); 2826 if (vp->v_iflag & VI_DOOMED) 2827 strcat(buf, "|VI_DOOMED"); 2828 if (vp->v_iflag & VI_FREE) 2829 strcat(buf, "|VI_FREE"); 2830 if (vp->v_vflag & VV_OBJBUF) 2831 strcat(buf, "|VV_OBJBUF"); 2832 if (buf[0] != '\0') 2833 printf(" flags (%s),", &buf[1]); 2834 lockmgr_printinfo(vp->v_vnlock); 2835 printf("\n"); 2836 if (vp->v_data != NULL) 2837 VOP_PRINT(vp); 2838 } 2839 2840 #ifdef DDB 2841 #include <ddb/ddb.h> 2842 /* 2843 * List all of the locked vnodes in the system. 2844 * Called when debugging the kernel. 2845 */ 2846 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2847 { 2848 struct mount *mp, *nmp; 2849 struct vnode *vp; 2850 2851 /* 2852 * Note: because this is DDB, we can't obey the locking semantics 2853 * for these structures, which means we could catch an inconsistent 2854 * state and dereference a nasty pointer. Not much to be done 2855 * about that. 2856 */ 2857 printf("Locked vnodes\n"); 2858 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2859 nmp = TAILQ_NEXT(mp, mnt_list); 2860 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2861 if (VOP_ISLOCKED(vp, NULL)) 2862 vprint(NULL, vp); 2863 } 2864 nmp = TAILQ_NEXT(mp, mnt_list); 2865 } 2866 } 2867 #endif 2868 2869 /* 2870 * Fill in a struct xvfsconf based on a struct vfsconf. 2871 */ 2872 static void 2873 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2874 { 2875 2876 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2877 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2878 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2879 xvfsp->vfc_flags = vfsp->vfc_flags; 2880 /* 2881 * These are unused in userland, we keep them 2882 * to not break binary compatibility. 2883 */ 2884 xvfsp->vfc_vfsops = NULL; 2885 xvfsp->vfc_next = NULL; 2886 } 2887 2888 /* 2889 * Top level filesystem related information gathering. 2890 */ 2891 static int 2892 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2893 { 2894 struct vfsconf *vfsp; 2895 struct xvfsconf *xvfsp; 2896 int cnt, error, i; 2897 2898 cnt = 0; 2899 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 2900 cnt++; 2901 xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK); 2902 /* 2903 * Handle the race that we will have here when struct vfsconf 2904 * will be locked down by using both cnt and checking vfc_next 2905 * against NULL to determine the end of the loop. The race will 2906 * happen because we will have to unlock before calling malloc(). 2907 * We are protected by Giant for now. 2908 */ 2909 i = 0; 2910 for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) { 2911 vfsconf2x(vfsp, xvfsp + i); 2912 i++; 2913 } 2914 error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i); 2915 free(xvfsp, M_TEMP); 2916 return (error); 2917 } 2918 2919 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2920 "S,xvfsconf", "List of all configured filesystems"); 2921 2922 #ifndef BURN_BRIDGES 2923 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2924 2925 static int 2926 vfs_sysctl(SYSCTL_HANDLER_ARGS) 2927 { 2928 int *name = (int *)arg1 - 1; /* XXX */ 2929 u_int namelen = arg2 + 1; /* XXX */ 2930 struct vfsconf *vfsp; 2931 struct xvfsconf xvfsp; 2932 2933 printf("WARNING: userland calling deprecated sysctl, " 2934 "please rebuild world\n"); 2935 2936 #if 1 || defined(COMPAT_PRELITE2) 2937 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2938 if (namelen == 1) 2939 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2940 #endif 2941 2942 switch (name[1]) { 2943 case VFS_MAXTYPENUM: 2944 if (namelen != 2) 2945 return (ENOTDIR); 2946 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2947 case VFS_CONF: 2948 if (namelen != 3) 2949 return (ENOTDIR); /* overloaded */ 2950 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 2951 if (vfsp->vfc_typenum == name[2]) 2952 break; 2953 if (vfsp == NULL) 2954 return (EOPNOTSUPP); 2955 vfsconf2x(vfsp, &xvfsp); 2956 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 2957 } 2958 return (EOPNOTSUPP); 2959 } 2960 2961 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl, 2962 "Generic filesystem"); 2963 2964 #if 1 || defined(COMPAT_PRELITE2) 2965 2966 static int 2967 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2968 { 2969 int error; 2970 struct vfsconf *vfsp; 2971 struct ovfsconf ovfs; 2972 2973 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 2974 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 2975 strcpy(ovfs.vfc_name, vfsp->vfc_name); 2976 ovfs.vfc_index = vfsp->vfc_typenum; 2977 ovfs.vfc_refcount = vfsp->vfc_refcount; 2978 ovfs.vfc_flags = vfsp->vfc_flags; 2979 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 2980 if (error) 2981 return error; 2982 } 2983 return 0; 2984 } 2985 2986 #endif /* 1 || COMPAT_PRELITE2 */ 2987 #endif /* !BURN_BRIDGES */ 2988 2989 #define KINFO_VNODESLOP 10 2990 #ifdef notyet 2991 /* 2992 * Dump vnode list (via sysctl). 2993 */ 2994 /* ARGSUSED */ 2995 static int 2996 sysctl_vnode(SYSCTL_HANDLER_ARGS) 2997 { 2998 struct xvnode *xvn; 2999 struct thread *td = req->td; 3000 struct mount *mp; 3001 struct vnode *vp; 3002 int error, len, n; 3003 3004 /* 3005 * Stale numvnodes access is not fatal here. 3006 */ 3007 req->lock = 0; 3008 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 3009 if (!req->oldptr) 3010 /* Make an estimate */ 3011 return (SYSCTL_OUT(req, 0, len)); 3012 3013 error = sysctl_wire_old_buffer(req, 0); 3014 if (error != 0) 3015 return (error); 3016 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 3017 n = 0; 3018 mtx_lock(&mountlist_mtx); 3019 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3020 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 3021 continue; 3022 MNT_ILOCK(mp); 3023 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3024 if (n == len) 3025 break; 3026 vref(vp); 3027 xvn[n].xv_size = sizeof *xvn; 3028 xvn[n].xv_vnode = vp; 3029 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3030 XV_COPY(usecount); 3031 XV_COPY(writecount); 3032 XV_COPY(holdcnt); 3033 XV_COPY(id); 3034 XV_COPY(mount); 3035 XV_COPY(numoutput); 3036 XV_COPY(type); 3037 #undef XV_COPY 3038 xvn[n].xv_flag = vp->v_vflag; 3039 3040 switch (vp->v_type) { 3041 case VREG: 3042 case VDIR: 3043 case VLNK: 3044 xvn[n].xv_dev = vp->v_cachedfs; 3045 xvn[n].xv_ino = vp->v_cachedid; 3046 break; 3047 case VBLK: 3048 case VCHR: 3049 if (vp->v_rdev == NULL) { 3050 vrele(vp); 3051 continue; 3052 } 3053 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3054 break; 3055 case VSOCK: 3056 xvn[n].xv_socket = vp->v_socket; 3057 break; 3058 case VFIFO: 3059 xvn[n].xv_fifo = vp->v_fifoinfo; 3060 break; 3061 case VNON: 3062 case VBAD: 3063 default: 3064 /* shouldn't happen? */ 3065 vrele(vp); 3066 continue; 3067 } 3068 vrele(vp); 3069 ++n; 3070 } 3071 MNT_IUNLOCK(mp); 3072 mtx_lock(&mountlist_mtx); 3073 vfs_unbusy(mp, td); 3074 if (n == len) 3075 break; 3076 } 3077 mtx_unlock(&mountlist_mtx); 3078 3079 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3080 free(xvn, M_TEMP); 3081 return (error); 3082 } 3083 3084 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 3085 0, 0, sysctl_vnode, "S,xvnode", ""); 3086 #endif 3087 3088 /* 3089 * Check to see if a filesystem is mounted on a block device. 3090 */ 3091 int 3092 vfs_mountedon(vp) 3093 struct vnode *vp; 3094 { 3095 3096 if (vp->v_rdev->si_mountpoint != NULL) 3097 return (EBUSY); 3098 return (0); 3099 } 3100 3101 /* 3102 * Unmount all filesystems. The list is traversed in reverse order 3103 * of mounting to avoid dependencies. 3104 */ 3105 void 3106 vfs_unmountall() 3107 { 3108 struct mount *mp; 3109 struct thread *td; 3110 int error; 3111 3112 if (curthread != NULL) 3113 td = curthread; 3114 else 3115 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */ 3116 /* 3117 * Since this only runs when rebooting, it is not interlocked. 3118 */ 3119 while(!TAILQ_EMPTY(&mountlist)) { 3120 mp = TAILQ_LAST(&mountlist, mntlist); 3121 error = dounmount(mp, MNT_FORCE, td); 3122 if (error) { 3123 TAILQ_REMOVE(&mountlist, mp, mnt_list); 3124 printf("unmount of %s failed (", 3125 mp->mnt_stat.f_mntonname); 3126 if (error == EBUSY) 3127 printf("BUSY)\n"); 3128 else 3129 printf("%d)\n", error); 3130 } else { 3131 /* The unmount has removed mp from the mountlist */ 3132 } 3133 } 3134 } 3135 3136 /* 3137 * perform msync on all vnodes under a mount point 3138 * the mount point must be locked. 3139 */ 3140 void 3141 vfs_msync(struct mount *mp, int flags) 3142 { 3143 struct vnode *vp, *nvp; 3144 struct vm_object *obj; 3145 int tries; 3146 3147 GIANT_REQUIRED; 3148 3149 tries = 5; 3150 MNT_ILOCK(mp); 3151 loop: 3152 TAILQ_FOREACH_SAFE(vp, &mp->mnt_nvnodelist, v_nmntvnodes, nvp) { 3153 if (vp->v_mount != mp) { 3154 if (--tries > 0) 3155 goto loop; 3156 break; 3157 } 3158 3159 VI_LOCK(vp); 3160 if (vp->v_iflag & VI_XLOCK) { 3161 VI_UNLOCK(vp); 3162 continue; 3163 } 3164 3165 if ((vp->v_iflag & VI_OBJDIRTY) && 3166 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 3167 MNT_IUNLOCK(mp); 3168 if (!vget(vp, 3169 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3170 curthread)) { 3171 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3172 vput(vp); 3173 MNT_ILOCK(mp); 3174 continue; 3175 } 3176 3177 if (VOP_GETVOBJECT(vp, &obj) == 0) { 3178 VM_OBJECT_LOCK(obj); 3179 vm_object_page_clean(obj, 0, 0, 3180 flags == MNT_WAIT ? 3181 OBJPC_SYNC : OBJPC_NOSYNC); 3182 VM_OBJECT_UNLOCK(obj); 3183 } 3184 vput(vp); 3185 } 3186 MNT_ILOCK(mp); 3187 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) { 3188 if (--tries > 0) 3189 goto loop; 3190 break; 3191 } 3192 } else 3193 VI_UNLOCK(vp); 3194 } 3195 MNT_IUNLOCK(mp); 3196 } 3197 3198 /* 3199 * Create the VM object needed for VMIO and mmap support. This 3200 * is done for all VREG files in the system. Some filesystems might 3201 * afford the additional metadata buffering capability of the 3202 * VMIO code by making the device node be VMIO mode also. 3203 * 3204 * vp must be locked when vfs_object_create is called. 3205 */ 3206 int 3207 vfs_object_create(vp, td, cred) 3208 struct vnode *vp; 3209 struct thread *td; 3210 struct ucred *cred; 3211 { 3212 3213 GIANT_REQUIRED; 3214 return (VOP_CREATEVOBJECT(vp, cred, td)); 3215 } 3216 3217 /* 3218 * Mark a vnode as free, putting it up for recycling. 3219 */ 3220 void 3221 vfree(vp) 3222 struct vnode *vp; 3223 { 3224 3225 ASSERT_VI_LOCKED(vp, "vfree"); 3226 mtx_lock(&vnode_free_list_mtx); 3227 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free")); 3228 if (vp->v_iflag & VI_AGE) { 3229 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 3230 } else { 3231 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 3232 } 3233 freevnodes++; 3234 mtx_unlock(&vnode_free_list_mtx); 3235 vp->v_iflag &= ~VI_AGE; 3236 vp->v_iflag |= VI_FREE; 3237 } 3238 3239 /* 3240 * Opposite of vfree() - mark a vnode as in use. 3241 */ 3242 void 3243 vbusy(vp) 3244 struct vnode *vp; 3245 { 3246 3247 ASSERT_VI_LOCKED(vp, "vbusy"); 3248 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free")); 3249 3250 mtx_lock(&vnode_free_list_mtx); 3251 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 3252 freevnodes--; 3253 mtx_unlock(&vnode_free_list_mtx); 3254 3255 vp->v_iflag &= ~(VI_FREE|VI_AGE); 3256 } 3257 3258 /* 3259 * Initalize per-vnode helper structure to hold poll-related state. 3260 */ 3261 void 3262 v_addpollinfo(struct vnode *vp) 3263 { 3264 3265 vp->v_pollinfo = uma_zalloc(vnodepoll_zone, M_WAITOK); 3266 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 3267 } 3268 3269 /* 3270 * Record a process's interest in events which might happen to 3271 * a vnode. Because poll uses the historic select-style interface 3272 * internally, this routine serves as both the ``check for any 3273 * pending events'' and the ``record my interest in future events'' 3274 * functions. (These are done together, while the lock is held, 3275 * to avoid race conditions.) 3276 */ 3277 int 3278 vn_pollrecord(vp, td, events) 3279 struct vnode *vp; 3280 struct thread *td; 3281 short events; 3282 { 3283 3284 if (vp->v_pollinfo == NULL) 3285 v_addpollinfo(vp); 3286 mtx_lock(&vp->v_pollinfo->vpi_lock); 3287 if (vp->v_pollinfo->vpi_revents & events) { 3288 /* 3289 * This leaves events we are not interested 3290 * in available for the other process which 3291 * which presumably had requested them 3292 * (otherwise they would never have been 3293 * recorded). 3294 */ 3295 events &= vp->v_pollinfo->vpi_revents; 3296 vp->v_pollinfo->vpi_revents &= ~events; 3297 3298 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3299 return events; 3300 } 3301 vp->v_pollinfo->vpi_events |= events; 3302 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3303 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3304 return 0; 3305 } 3306 3307 /* 3308 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 3309 * it is possible for us to miss an event due to race conditions, but 3310 * that condition is expected to be rare, so for the moment it is the 3311 * preferred interface. 3312 */ 3313 void 3314 vn_pollevent(vp, events) 3315 struct vnode *vp; 3316 short events; 3317 { 3318 3319 if (vp->v_pollinfo == NULL) 3320 v_addpollinfo(vp); 3321 mtx_lock(&vp->v_pollinfo->vpi_lock); 3322 if (vp->v_pollinfo->vpi_events & events) { 3323 /* 3324 * We clear vpi_events so that we don't 3325 * call selwakeup() twice if two events are 3326 * posted before the polling process(es) is 3327 * awakened. This also ensures that we take at 3328 * most one selwakeup() if the polling process 3329 * is no longer interested. However, it does 3330 * mean that only one event can be noticed at 3331 * a time. (Perhaps we should only clear those 3332 * event bits which we note?) XXX 3333 */ 3334 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */ 3335 vp->v_pollinfo->vpi_revents |= events; 3336 selwakeuppri(&vp->v_pollinfo->vpi_selinfo, PRIBIO); 3337 } 3338 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3339 } 3340 3341 /* 3342 * Wake up anyone polling on vp because it is being revoked. 3343 * This depends on dead_poll() returning POLLHUP for correct 3344 * behavior. 3345 */ 3346 void 3347 vn_pollgone(vp) 3348 struct vnode *vp; 3349 { 3350 3351 mtx_lock(&vp->v_pollinfo->vpi_lock); 3352 VN_KNOTE(vp, NOTE_REVOKE); 3353 if (vp->v_pollinfo->vpi_events) { 3354 vp->v_pollinfo->vpi_events = 0; 3355 selwakeuppri(&vp->v_pollinfo->vpi_selinfo, PRIBIO); 3356 } 3357 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3358 } 3359 3360 3361 3362 /* 3363 * Routine to create and manage a filesystem syncer vnode. 3364 */ 3365 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3366 static int sync_fsync(struct vop_fsync_args *); 3367 static int sync_inactive(struct vop_inactive_args *); 3368 static int sync_reclaim(struct vop_reclaim_args *); 3369 3370 static vop_t **sync_vnodeop_p; 3371 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 3372 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 3373 { &vop_close_desc, (vop_t *) sync_close }, /* close */ 3374 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */ 3375 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */ 3376 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */ 3377 { &vop_lock_desc, (vop_t *) vop_stdlock }, /* lock */ 3378 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, /* unlock */ 3379 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, /* islocked */ 3380 { NULL, NULL } 3381 }; 3382 static struct vnodeopv_desc sync_vnodeop_opv_desc = 3383 { &sync_vnodeop_p, sync_vnodeop_entries }; 3384 3385 VNODEOP_SET(sync_vnodeop_opv_desc); 3386 3387 /* 3388 * Create a new filesystem syncer vnode for the specified mount point. 3389 */ 3390 int 3391 vfs_allocate_syncvnode(mp) 3392 struct mount *mp; 3393 { 3394 struct vnode *vp; 3395 static long start, incr, next; 3396 int error; 3397 3398 /* Allocate a new vnode */ 3399 if ((error = getnewvnode("syncer", mp, sync_vnodeop_p, &vp)) != 0) { 3400 mp->mnt_syncer = NULL; 3401 return (error); 3402 } 3403 vp->v_type = VNON; 3404 /* 3405 * Place the vnode onto the syncer worklist. We attempt to 3406 * scatter them about on the list so that they will go off 3407 * at evenly distributed times even if all the filesystems 3408 * are mounted at once. 3409 */ 3410 next += incr; 3411 if (next == 0 || next > syncer_maxdelay) { 3412 start /= 2; 3413 incr /= 2; 3414 if (start == 0) { 3415 start = syncer_maxdelay / 2; 3416 incr = syncer_maxdelay; 3417 } 3418 next = start; 3419 } 3420 VI_LOCK(vp); 3421 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0); 3422 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 3423 mtx_lock(&sync_mtx); 3424 sync_vnode_count++; 3425 mtx_unlock(&sync_mtx); 3426 VI_UNLOCK(vp); 3427 mp->mnt_syncer = vp; 3428 return (0); 3429 } 3430 3431 /* 3432 * Do a lazy sync of the filesystem. 3433 */ 3434 static int 3435 sync_fsync(ap) 3436 struct vop_fsync_args /* { 3437 struct vnode *a_vp; 3438 struct ucred *a_cred; 3439 int a_waitfor; 3440 struct thread *a_td; 3441 } */ *ap; 3442 { 3443 struct vnode *syncvp = ap->a_vp; 3444 struct mount *mp = syncvp->v_mount; 3445 struct thread *td = ap->a_td; 3446 int error, asyncflag; 3447 3448 /* 3449 * We only need to do something if this is a lazy evaluation. 3450 */ 3451 if (ap->a_waitfor != MNT_LAZY) 3452 return (0); 3453 3454 /* 3455 * Move ourselves to the back of the sync list. 3456 */ 3457 VI_LOCK(syncvp); 3458 vn_syncer_add_to_worklist(syncvp, syncdelay); 3459 VI_UNLOCK(syncvp); 3460 3461 /* 3462 * Walk the list of vnodes pushing all that are dirty and 3463 * not already on the sync list. 3464 */ 3465 mtx_lock(&mountlist_mtx); 3466 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3467 mtx_unlock(&mountlist_mtx); 3468 return (0); 3469 } 3470 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3471 vfs_unbusy(mp, td); 3472 return (0); 3473 } 3474 asyncflag = mp->mnt_flag & MNT_ASYNC; 3475 mp->mnt_flag &= ~MNT_ASYNC; 3476 vfs_msync(mp, MNT_NOWAIT); 3477 error = VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td); 3478 if (asyncflag) 3479 mp->mnt_flag |= MNT_ASYNC; 3480 vn_finished_write(mp); 3481 vfs_unbusy(mp, td); 3482 return (error); 3483 } 3484 3485 /* 3486 * The syncer vnode is no referenced. 3487 */ 3488 static int 3489 sync_inactive(ap) 3490 struct vop_inactive_args /* { 3491 struct vnode *a_vp; 3492 struct thread *a_td; 3493 } */ *ap; 3494 { 3495 3496 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 3497 vgone(ap->a_vp); 3498 return (0); 3499 } 3500 3501 /* 3502 * The syncer vnode is no longer needed and is being decommissioned. 3503 * 3504 * Modifications to the worklist must be protected by sync_mtx. 3505 */ 3506 static int 3507 sync_reclaim(ap) 3508 struct vop_reclaim_args /* { 3509 struct vnode *a_vp; 3510 } */ *ap; 3511 { 3512 struct vnode *vp = ap->a_vp; 3513 3514 VI_LOCK(vp); 3515 vp->v_mount->mnt_syncer = NULL; 3516 if (vp->v_iflag & VI_ONWORKLST) { 3517 mtx_lock(&sync_mtx); 3518 LIST_REMOVE(vp, v_synclist); 3519 syncer_worklist_len--; 3520 sync_vnode_count--; 3521 mtx_unlock(&sync_mtx); 3522 vp->v_iflag &= ~VI_ONWORKLST; 3523 } 3524 VI_UNLOCK(vp); 3525 3526 return (0); 3527 } 3528 3529 /* 3530 * extract the struct cdev *from a VCHR 3531 */ 3532 struct cdev * 3533 vn_todev(vp) 3534 struct vnode *vp; 3535 { 3536 3537 if (vp->v_type != VCHR) 3538 return (NULL); 3539 return (vp->v_rdev); 3540 } 3541 3542 /* 3543 * Check if vnode represents a disk device 3544 */ 3545 int 3546 vn_isdisk(vp, errp) 3547 struct vnode *vp; 3548 int *errp; 3549 { 3550 int error; 3551 3552 error = 0; 3553 if (vp->v_type != VCHR) 3554 error = ENOTBLK; 3555 else if (vp->v_rdev == NULL) 3556 error = ENXIO; 3557 else if (!(devsw(vp->v_rdev)->d_flags & D_DISK)) 3558 error = ENOTBLK; 3559 if (errp != NULL) 3560 *errp = error; 3561 return (error == 0); 3562 } 3563 3564 /* 3565 * Free data allocated by namei(); see namei(9) for details. 3566 */ 3567 void 3568 NDFREE(ndp, flags) 3569 struct nameidata *ndp; 3570 const u_int flags; 3571 { 3572 3573 if (!(flags & NDF_NO_FREE_PNBUF) && 3574 (ndp->ni_cnd.cn_flags & HASBUF)) { 3575 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 3576 ndp->ni_cnd.cn_flags &= ~HASBUF; 3577 } 3578 if (!(flags & NDF_NO_DVP_UNLOCK) && 3579 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 3580 ndp->ni_dvp != ndp->ni_vp) 3581 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 3582 if (!(flags & NDF_NO_DVP_RELE) && 3583 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3584 vrele(ndp->ni_dvp); 3585 ndp->ni_dvp = NULL; 3586 } 3587 if (!(flags & NDF_NO_VP_UNLOCK) && 3588 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3589 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 3590 if (!(flags & NDF_NO_VP_RELE) && 3591 ndp->ni_vp) { 3592 vrele(ndp->ni_vp); 3593 ndp->ni_vp = NULL; 3594 } 3595 if (!(flags & NDF_NO_STARTDIR_RELE) && 3596 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3597 vrele(ndp->ni_startdir); 3598 ndp->ni_startdir = NULL; 3599 } 3600 } 3601 3602 /* 3603 * Common filesystem object access control check routine. Accepts a 3604 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3605 * and optional call-by-reference privused argument allowing vaccess() 3606 * to indicate to the caller whether privilege was used to satisfy the 3607 * request (obsoleted). Returns 0 on success, or an errno on failure. 3608 */ 3609 int 3610 vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3611 enum vtype type; 3612 mode_t file_mode; 3613 uid_t file_uid; 3614 gid_t file_gid; 3615 mode_t acc_mode; 3616 struct ucred *cred; 3617 int *privused; 3618 { 3619 mode_t dac_granted; 3620 #ifdef CAPABILITIES 3621 mode_t cap_granted; 3622 #endif 3623 3624 /* 3625 * Look for a normal, non-privileged way to access the file/directory 3626 * as requested. If it exists, go with that. 3627 */ 3628 3629 if (privused != NULL) 3630 *privused = 0; 3631 3632 dac_granted = 0; 3633 3634 /* Check the owner. */ 3635 if (cred->cr_uid == file_uid) { 3636 dac_granted |= VADMIN; 3637 if (file_mode & S_IXUSR) 3638 dac_granted |= VEXEC; 3639 if (file_mode & S_IRUSR) 3640 dac_granted |= VREAD; 3641 if (file_mode & S_IWUSR) 3642 dac_granted |= (VWRITE | VAPPEND); 3643 3644 if ((acc_mode & dac_granted) == acc_mode) 3645 return (0); 3646 3647 goto privcheck; 3648 } 3649 3650 /* Otherwise, check the groups (first match) */ 3651 if (groupmember(file_gid, cred)) { 3652 if (file_mode & S_IXGRP) 3653 dac_granted |= VEXEC; 3654 if (file_mode & S_IRGRP) 3655 dac_granted |= VREAD; 3656 if (file_mode & S_IWGRP) 3657 dac_granted |= (VWRITE | VAPPEND); 3658 3659 if ((acc_mode & dac_granted) == acc_mode) 3660 return (0); 3661 3662 goto privcheck; 3663 } 3664 3665 /* Otherwise, check everyone else. */ 3666 if (file_mode & S_IXOTH) 3667 dac_granted |= VEXEC; 3668 if (file_mode & S_IROTH) 3669 dac_granted |= VREAD; 3670 if (file_mode & S_IWOTH) 3671 dac_granted |= (VWRITE | VAPPEND); 3672 if ((acc_mode & dac_granted) == acc_mode) 3673 return (0); 3674 3675 privcheck: 3676 if (!suser_cred(cred, PRISON_ROOT)) { 3677 /* XXX audit: privilege used */ 3678 if (privused != NULL) 3679 *privused = 1; 3680 return (0); 3681 } 3682 3683 #ifdef CAPABILITIES 3684 /* 3685 * Build a capability mask to determine if the set of capabilities 3686 * satisfies the requirements when combined with the granted mask 3687 * from above. 3688 * For each capability, if the capability is required, bitwise 3689 * or the request type onto the cap_granted mask. 3690 */ 3691 cap_granted = 0; 3692 3693 if (type == VDIR) { 3694 /* 3695 * For directories, use CAP_DAC_READ_SEARCH to satisfy 3696 * VEXEC requests, instead of CAP_DAC_EXECUTE. 3697 */ 3698 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3699 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3700 cap_granted |= VEXEC; 3701 } else { 3702 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3703 !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT)) 3704 cap_granted |= VEXEC; 3705 } 3706 3707 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3708 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3709 cap_granted |= VREAD; 3710 3711 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3712 !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT)) 3713 cap_granted |= (VWRITE | VAPPEND); 3714 3715 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3716 !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT)) 3717 cap_granted |= VADMIN; 3718 3719 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3720 /* XXX audit: privilege used */ 3721 if (privused != NULL) 3722 *privused = 1; 3723 return (0); 3724 } 3725 #endif 3726 3727 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3728 } 3729 3730 /* 3731 * Credential check based on process requesting service, and per-attribute 3732 * permissions. 3733 */ 3734 int 3735 extattr_check_cred(struct vnode *vp, int attrnamespace, 3736 struct ucred *cred, struct thread *td, int access) 3737 { 3738 3739 /* 3740 * Kernel-invoked always succeeds. 3741 */ 3742 if (cred == NOCRED) 3743 return (0); 3744 3745 /* 3746 * Do not allow privileged processes in jail to directly 3747 * manipulate system attributes. 3748 * 3749 * XXX What capability should apply here? 3750 * Probably CAP_SYS_SETFFLAG. 3751 */ 3752 switch (attrnamespace) { 3753 case EXTATTR_NAMESPACE_SYSTEM: 3754 /* Potentially should be: return (EPERM); */ 3755 return (suser_cred(cred, 0)); 3756 case EXTATTR_NAMESPACE_USER: 3757 return (VOP_ACCESS(vp, access, cred, td)); 3758 default: 3759 return (EPERM); 3760 } 3761 } 3762 3763 #ifdef DEBUG_VFS_LOCKS 3764 /* 3765 * This only exists to supress warnings from unlocked specfs accesses. It is 3766 * no longer ok to have an unlocked VFS. 3767 */ 3768 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD) 3769 3770 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 3771 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 3772 int vfs_badlock_print = 1; /* Print lock violations. */ 3773 3774 static void 3775 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 3776 { 3777 3778 if (vfs_badlock_print) 3779 printf("%s: %p %s\n", str, (void *)vp, msg); 3780 if (vfs_badlock_ddb) 3781 kdb_enter("lock violation"); 3782 } 3783 3784 void 3785 assert_vi_locked(struct vnode *vp, const char *str) 3786 { 3787 3788 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 3789 vfs_badlock("interlock is not locked but should be", str, vp); 3790 } 3791 3792 void 3793 assert_vi_unlocked(struct vnode *vp, const char *str) 3794 { 3795 3796 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 3797 vfs_badlock("interlock is locked but should not be", str, vp); 3798 } 3799 3800 void 3801 assert_vop_locked(struct vnode *vp, const char *str) 3802 { 3803 3804 if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0) 3805 vfs_badlock("is not locked but should be", str, vp); 3806 } 3807 3808 void 3809 assert_vop_unlocked(struct vnode *vp, const char *str) 3810 { 3811 3812 if (vp && !IGNORE_LOCK(vp) && 3813 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) 3814 vfs_badlock("is locked but should not be", str, vp); 3815 } 3816 3817 #if 0 3818 void 3819 assert_vop_elocked(struct vnode *vp, const char *str) 3820 { 3821 3822 if (vp && !IGNORE_LOCK(vp) && 3823 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE) 3824 vfs_badlock("is not exclusive locked but should be", str, vp); 3825 } 3826 3827 void 3828 assert_vop_elocked_other(struct vnode *vp, const char *str) 3829 { 3830 3831 if (vp && !IGNORE_LOCK(vp) && 3832 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER) 3833 vfs_badlock("is not exclusive locked by another thread", 3834 str, vp); 3835 } 3836 3837 void 3838 assert_vop_slocked(struct vnode *vp, const char *str) 3839 { 3840 3841 if (vp && !IGNORE_LOCK(vp) && 3842 VOP_ISLOCKED(vp, curthread) != LK_SHARED) 3843 vfs_badlock("is not locked shared but should be", str, vp); 3844 } 3845 #endif /* 0 */ 3846 3847 void 3848 vop_rename_pre(void *ap) 3849 { 3850 struct vop_rename_args *a = ap; 3851 3852 if (a->a_tvp) 3853 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 3854 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 3855 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 3856 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 3857 3858 /* Check the source (from). */ 3859 if (a->a_tdvp != a->a_fdvp) 3860 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 3861 if (a->a_tvp != a->a_fvp) 3862 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked"); 3863 3864 /* Check the target. */ 3865 if (a->a_tvp) 3866 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 3867 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 3868 } 3869 3870 void 3871 vop_strategy_pre(void *ap) 3872 { 3873 struct vop_strategy_args *a; 3874 struct buf *bp; 3875 3876 a = ap; 3877 bp = a->a_bp; 3878 3879 /* 3880 * Cluster ops lock their component buffers but not the IO container. 3881 */ 3882 if ((bp->b_flags & B_CLUSTER) != 0) 3883 return; 3884 3885 if (BUF_REFCNT(bp) < 1) { 3886 if (vfs_badlock_print) 3887 printf( 3888 "VOP_STRATEGY: bp is not locked but should be\n"); 3889 if (vfs_badlock_ddb) 3890 kdb_enter("lock violation"); 3891 } 3892 } 3893 3894 void 3895 vop_lookup_pre(void *ap) 3896 { 3897 struct vop_lookup_args *a; 3898 struct vnode *dvp; 3899 3900 a = ap; 3901 dvp = a->a_dvp; 3902 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3903 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3904 } 3905 3906 void 3907 vop_lookup_post(void *ap, int rc) 3908 { 3909 struct vop_lookup_args *a; 3910 struct componentname *cnp; 3911 struct vnode *dvp; 3912 struct vnode *vp; 3913 int flags; 3914 3915 a = ap; 3916 dvp = a->a_dvp; 3917 cnp = a->a_cnp; 3918 vp = *(a->a_vpp); 3919 flags = cnp->cn_flags; 3920 3921 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3922 3923 /* 3924 * If this is the last path component for this lookup and LOCKPARENT 3925 * is set, OR if there is an error the directory has to be locked. 3926 */ 3927 if ((flags & LOCKPARENT) && (flags & ISLASTCN)) 3928 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)"); 3929 else if (rc != 0) 3930 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)"); 3931 else if (dvp != vp) 3932 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)"); 3933 if (flags & PDIRUNLOCK) 3934 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)"); 3935 } 3936 3937 void 3938 vop_lock_pre(void *ap) 3939 { 3940 struct vop_lock_args *a = ap; 3941 3942 if ((a->a_flags & LK_INTERLOCK) == 0) 3943 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3944 else 3945 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 3946 } 3947 3948 void 3949 vop_lock_post(void *ap, int rc) 3950 { 3951 struct vop_lock_args *a = ap; 3952 3953 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3954 if (rc == 0) 3955 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 3956 } 3957 3958 void 3959 vop_unlock_pre(void *ap) 3960 { 3961 struct vop_unlock_args *a = ap; 3962 3963 if (a->a_flags & LK_INTERLOCK) 3964 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 3965 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 3966 } 3967 3968 void 3969 vop_unlock_post(void *ap, int rc) 3970 { 3971 struct vop_unlock_args *a = ap; 3972 3973 if (a->a_flags & LK_INTERLOCK) 3974 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 3975 } 3976 #endif /* DEBUG_VFS_LOCKS */ 3977 3978 static struct klist fs_klist = SLIST_HEAD_INITIALIZER(&fs_klist); 3979 3980 void 3981 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused) 3982 { 3983 3984 KNOTE(&fs_klist, event); 3985 } 3986 3987 static int filt_fsattach(struct knote *kn); 3988 static void filt_fsdetach(struct knote *kn); 3989 static int filt_fsevent(struct knote *kn, long hint); 3990 3991 struct filterops fs_filtops = 3992 { 0, filt_fsattach, filt_fsdetach, filt_fsevent }; 3993 3994 static int 3995 filt_fsattach(struct knote *kn) 3996 { 3997 3998 kn->kn_flags |= EV_CLEAR; 3999 SLIST_INSERT_HEAD(&fs_klist, kn, kn_selnext); 4000 return (0); 4001 } 4002 4003 static void 4004 filt_fsdetach(struct knote *kn) 4005 { 4006 4007 SLIST_REMOVE(&fs_klist, kn, knote, kn_selnext); 4008 } 4009 4010 static int 4011 filt_fsevent(struct knote *kn, long hint) 4012 { 4013 4014 kn->kn_fflags |= hint; 4015 return (kn->kn_fflags != 0); 4016 } 4017 4018 static int 4019 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 4020 { 4021 struct vfsidctl vc; 4022 int error; 4023 struct mount *mp; 4024 4025 error = SYSCTL_IN(req, &vc, sizeof(vc)); 4026 if (error) 4027 return (error); 4028 if (vc.vc_vers != VFS_CTL_VERS1) 4029 return (EINVAL); 4030 mp = vfs_getvfs(&vc.vc_fsid); 4031 if (mp == NULL) 4032 return (ENOENT); 4033 /* ensure that a specific sysctl goes to the right filesystem. */ 4034 if (strcmp(vc.vc_fstypename, "*") != 0 && 4035 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 4036 return (EINVAL); 4037 } 4038 VCTLTOREQ(&vc, req); 4039 return (VFS_SYSCTL(mp, vc.vc_op, req)); 4040 } 4041 4042 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, 4043 NULL, 0, sysctl_vfs_ctl, "", "Sysctl by fsid"); 4044