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