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