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