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 vp->v_op = &devfs_specops; 2401 delmntque(vp); 2402 cache_purge(vp); 2403 vrele(vp); 2404 VI_LOCK(vp); 2405 } else 2406 vclean(vp, 0, td); 2407 vp->v_op = &devfs_specops; 2408 vx_unlock(vp); 2409 VI_UNLOCK(vp); 2410 } 2411 2412 /* 2413 * vgone, with the vp interlock held. 2414 */ 2415 void 2416 vgonel(vp, td) 2417 struct vnode *vp; 2418 struct thread *td; 2419 { 2420 /* 2421 * If a vgone (or vclean) is already in progress, 2422 * wait until it is done and return. 2423 */ 2424 ASSERT_VI_LOCKED(vp, "vgonel"); 2425 if (vp->v_iflag & VI_XLOCK) { 2426 vp->v_iflag |= VI_XWANT; 2427 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0); 2428 return; 2429 } 2430 vx_lock(vp); 2431 2432 /* 2433 * Clean out the filesystem specific data. 2434 */ 2435 vclean(vp, DOCLOSE, td); 2436 VI_UNLOCK(vp); 2437 2438 /* 2439 * If special device, remove it from special device alias list 2440 * if it is on one. 2441 */ 2442 VI_LOCK(vp); 2443 if (vp->v_type == VCHR && vp->v_rdev != NULL) 2444 dev_rel(vp); 2445 2446 /* 2447 * If it is on the freelist and not already at the head, 2448 * move it to the head of the list. The test of the 2449 * VDOOMED flag and the reference count of zero is because 2450 * it will be removed from the free list by getnewvnode, 2451 * but will not have its reference count incremented until 2452 * after calling vgone. If the reference count were 2453 * incremented first, vgone would (incorrectly) try to 2454 * close the previous instance of the underlying object. 2455 */ 2456 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) { 2457 mtx_lock(&vnode_free_list_mtx); 2458 if (vp->v_iflag & VI_FREE) { 2459 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2460 } else { 2461 vp->v_iflag |= VI_FREE; 2462 freevnodes++; 2463 } 2464 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2465 mtx_unlock(&vnode_free_list_mtx); 2466 } 2467 2468 vp->v_type = VBAD; 2469 vx_unlock(vp); 2470 VI_UNLOCK(vp); 2471 } 2472 2473 /* 2474 * Lookup a vnode by device number. 2475 */ 2476 int 2477 vfinddev(dev, vpp) 2478 struct cdev *dev; 2479 struct vnode **vpp; 2480 { 2481 struct vnode *vp; 2482 2483 dev_lock(); 2484 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 2485 *vpp = vp; 2486 dev_unlock(); 2487 return (1); 2488 } 2489 dev_unlock(); 2490 return (0); 2491 } 2492 2493 /* 2494 * Calculate the total number of references to a special device. 2495 */ 2496 int 2497 vcount(vp) 2498 struct vnode *vp; 2499 { 2500 int count; 2501 2502 dev_lock(); 2503 count = vp->v_rdev->si_usecount; 2504 dev_unlock(); 2505 return (count); 2506 } 2507 2508 /* 2509 * Same as above, but using the struct cdev *as argument 2510 */ 2511 int 2512 count_dev(dev) 2513 struct cdev *dev; 2514 { 2515 int count; 2516 2517 dev_lock(); 2518 count = dev->si_usecount; 2519 dev_unlock(); 2520 return(count); 2521 } 2522 2523 /* 2524 * Print out a description of a vnode. 2525 */ 2526 static char *typename[] = 2527 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2528 2529 void 2530 vprint(label, vp) 2531 char *label; 2532 struct vnode *vp; 2533 { 2534 char buf[96]; 2535 2536 if (label != NULL) 2537 printf("%s: %p: ", label, (void *)vp); 2538 else 2539 printf("%p: ", (void *)vp); 2540 printf("tag %s, type %s\n ", vp->v_tag, typename[vp->v_type]); 2541 printf("usecount %d, writecount %d, refcount %d mountedhere %p\n", 2542 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere); 2543 buf[0] = '\0'; 2544 if (vp->v_vflag & VV_ROOT) 2545 strcat(buf, "|VV_ROOT"); 2546 if (vp->v_vflag & VV_TEXT) 2547 strcat(buf, "|VV_TEXT"); 2548 if (vp->v_vflag & VV_SYSTEM) 2549 strcat(buf, "|VV_SYSTEM"); 2550 if (vp->v_iflag & VI_XLOCK) 2551 strcat(buf, "|VI_XLOCK"); 2552 if (vp->v_iflag & VI_XWANT) 2553 strcat(buf, "|VI_XWANT"); 2554 if (vp->v_iflag & VI_DOOMED) 2555 strcat(buf, "|VI_DOOMED"); 2556 if (vp->v_iflag & VI_FREE) 2557 strcat(buf, "|VI_FREE"); 2558 if (vp->v_vflag & VV_OBJBUF) 2559 strcat(buf, "|VV_OBJBUF"); 2560 if (buf[0] != '\0') 2561 printf(" flags (%s)", &buf[1]); 2562 if (mtx_owned(VI_MTX(vp))) 2563 printf(" VI_LOCKed"); 2564 printf("\n "); 2565 lockmgr_printinfo(vp->v_vnlock); 2566 printf("\n"); 2567 if (vp->v_data != NULL) 2568 VOP_PRINT(vp); 2569 } 2570 2571 #ifdef DDB 2572 #include <ddb/ddb.h> 2573 /* 2574 * List all of the locked vnodes in the system. 2575 * Called when debugging the kernel. 2576 */ 2577 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2578 { 2579 struct mount *mp, *nmp; 2580 struct vnode *vp; 2581 2582 /* 2583 * Note: because this is DDB, we can't obey the locking semantics 2584 * for these structures, which means we could catch an inconsistent 2585 * state and dereference a nasty pointer. Not much to be done 2586 * about that. 2587 */ 2588 printf("Locked vnodes\n"); 2589 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2590 nmp = TAILQ_NEXT(mp, mnt_list); 2591 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2592 if (VOP_ISLOCKED(vp, NULL)) 2593 vprint(NULL, vp); 2594 } 2595 nmp = TAILQ_NEXT(mp, mnt_list); 2596 } 2597 } 2598 #endif 2599 2600 /* 2601 * Fill in a struct xvfsconf based on a struct vfsconf. 2602 */ 2603 static void 2604 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2605 { 2606 2607 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2608 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2609 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2610 xvfsp->vfc_flags = vfsp->vfc_flags; 2611 /* 2612 * These are unused in userland, we keep them 2613 * to not break binary compatibility. 2614 */ 2615 xvfsp->vfc_vfsops = NULL; 2616 xvfsp->vfc_next = NULL; 2617 } 2618 2619 /* 2620 * Top level filesystem related information gathering. 2621 */ 2622 static int 2623 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2624 { 2625 struct vfsconf *vfsp; 2626 struct xvfsconf xvfsp; 2627 int error; 2628 2629 error = 0; 2630 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 2631 vfsconf2x(vfsp, &xvfsp); 2632 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp); 2633 if (error) 2634 break; 2635 } 2636 return (error); 2637 } 2638 2639 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2640 "S,xvfsconf", "List of all configured filesystems"); 2641 2642 #ifndef BURN_BRIDGES 2643 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2644 2645 static int 2646 vfs_sysctl(SYSCTL_HANDLER_ARGS) 2647 { 2648 int *name = (int *)arg1 - 1; /* XXX */ 2649 u_int namelen = arg2 + 1; /* XXX */ 2650 struct vfsconf *vfsp; 2651 struct xvfsconf xvfsp; 2652 2653 printf("WARNING: userland calling deprecated sysctl, " 2654 "please rebuild world\n"); 2655 2656 #if 1 || defined(COMPAT_PRELITE2) 2657 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2658 if (namelen == 1) 2659 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2660 #endif 2661 2662 switch (name[1]) { 2663 case VFS_MAXTYPENUM: 2664 if (namelen != 2) 2665 return (ENOTDIR); 2666 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2667 case VFS_CONF: 2668 if (namelen != 3) 2669 return (ENOTDIR); /* overloaded */ 2670 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 2671 if (vfsp->vfc_typenum == name[2]) 2672 break; 2673 if (vfsp == NULL) 2674 return (EOPNOTSUPP); 2675 vfsconf2x(vfsp, &xvfsp); 2676 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 2677 } 2678 return (EOPNOTSUPP); 2679 } 2680 2681 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl, 2682 "Generic filesystem"); 2683 2684 #if 1 || defined(COMPAT_PRELITE2) 2685 2686 static int 2687 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2688 { 2689 int error; 2690 struct vfsconf *vfsp; 2691 struct ovfsconf ovfs; 2692 2693 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 2694 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 2695 strcpy(ovfs.vfc_name, vfsp->vfc_name); 2696 ovfs.vfc_index = vfsp->vfc_typenum; 2697 ovfs.vfc_refcount = vfsp->vfc_refcount; 2698 ovfs.vfc_flags = vfsp->vfc_flags; 2699 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 2700 if (error) 2701 return error; 2702 } 2703 return 0; 2704 } 2705 2706 #endif /* 1 || COMPAT_PRELITE2 */ 2707 #endif /* !BURN_BRIDGES */ 2708 2709 #define KINFO_VNODESLOP 10 2710 #ifdef notyet 2711 /* 2712 * Dump vnode list (via sysctl). 2713 */ 2714 /* ARGSUSED */ 2715 static int 2716 sysctl_vnode(SYSCTL_HANDLER_ARGS) 2717 { 2718 struct xvnode *xvn; 2719 struct thread *td = req->td; 2720 struct mount *mp; 2721 struct vnode *vp; 2722 int error, len, n; 2723 2724 /* 2725 * Stale numvnodes access is not fatal here. 2726 */ 2727 req->lock = 0; 2728 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 2729 if (!req->oldptr) 2730 /* Make an estimate */ 2731 return (SYSCTL_OUT(req, 0, len)); 2732 2733 error = sysctl_wire_old_buffer(req, 0); 2734 if (error != 0) 2735 return (error); 2736 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 2737 n = 0; 2738 mtx_lock(&mountlist_mtx); 2739 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2740 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 2741 continue; 2742 MNT_ILOCK(mp); 2743 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2744 if (n == len) 2745 break; 2746 vref(vp); 2747 xvn[n].xv_size = sizeof *xvn; 2748 xvn[n].xv_vnode = vp; 2749 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 2750 XV_COPY(usecount); 2751 XV_COPY(writecount); 2752 XV_COPY(holdcnt); 2753 XV_COPY(id); 2754 XV_COPY(mount); 2755 XV_COPY(numoutput); 2756 XV_COPY(type); 2757 #undef XV_COPY 2758 xvn[n].xv_flag = vp->v_vflag; 2759 2760 switch (vp->v_type) { 2761 case VREG: 2762 case VDIR: 2763 case VLNK: 2764 break; 2765 case VBLK: 2766 case VCHR: 2767 if (vp->v_rdev == NULL) { 2768 vrele(vp); 2769 continue; 2770 } 2771 xvn[n].xv_dev = dev2udev(vp->v_rdev); 2772 break; 2773 case VSOCK: 2774 xvn[n].xv_socket = vp->v_socket; 2775 break; 2776 case VFIFO: 2777 xvn[n].xv_fifo = vp->v_fifoinfo; 2778 break; 2779 case VNON: 2780 case VBAD: 2781 default: 2782 /* shouldn't happen? */ 2783 vrele(vp); 2784 continue; 2785 } 2786 vrele(vp); 2787 ++n; 2788 } 2789 MNT_IUNLOCK(mp); 2790 mtx_lock(&mountlist_mtx); 2791 vfs_unbusy(mp, td); 2792 if (n == len) 2793 break; 2794 } 2795 mtx_unlock(&mountlist_mtx); 2796 2797 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 2798 free(xvn, M_TEMP); 2799 return (error); 2800 } 2801 2802 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 2803 0, 0, sysctl_vnode, "S,xvnode", ""); 2804 #endif 2805 2806 /* 2807 * Unmount all filesystems. The list is traversed in reverse order 2808 * of mounting to avoid dependencies. 2809 */ 2810 void 2811 vfs_unmountall() 2812 { 2813 struct mount *mp; 2814 struct thread *td; 2815 int error; 2816 2817 if (curthread != NULL) 2818 td = curthread; 2819 else 2820 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */ 2821 /* 2822 * Since this only runs when rebooting, it is not interlocked. 2823 */ 2824 while(!TAILQ_EMPTY(&mountlist)) { 2825 mp = TAILQ_LAST(&mountlist, mntlist); 2826 error = dounmount(mp, MNT_FORCE, td); 2827 if (error) { 2828 TAILQ_REMOVE(&mountlist, mp, mnt_list); 2829 printf("unmount of %s failed (", 2830 mp->mnt_stat.f_mntonname); 2831 if (error == EBUSY) 2832 printf("BUSY)\n"); 2833 else 2834 printf("%d)\n", error); 2835 } else { 2836 /* The unmount has removed mp from the mountlist */ 2837 } 2838 } 2839 } 2840 2841 /* 2842 * perform msync on all vnodes under a mount point 2843 * the mount point must be locked. 2844 */ 2845 void 2846 vfs_msync(struct mount *mp, int flags) 2847 { 2848 struct vnode *vp, *nvp; 2849 struct vm_object *obj; 2850 int tries; 2851 2852 GIANT_REQUIRED; 2853 2854 tries = 5; 2855 MNT_ILOCK(mp); 2856 loop: 2857 TAILQ_FOREACH_SAFE(vp, &mp->mnt_nvnodelist, v_nmntvnodes, nvp) { 2858 if (vp->v_mount != mp) { 2859 if (--tries > 0) 2860 goto loop; 2861 break; 2862 } 2863 2864 VI_LOCK(vp); 2865 if (vp->v_iflag & VI_XLOCK) { 2866 VI_UNLOCK(vp); 2867 continue; 2868 } 2869 2870 if ((vp->v_iflag & VI_OBJDIRTY) && 2871 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 2872 MNT_IUNLOCK(mp); 2873 if (!vget(vp, 2874 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 2875 curthread)) { 2876 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 2877 vput(vp); 2878 MNT_ILOCK(mp); 2879 continue; 2880 } 2881 2882 if (VOP_GETVOBJECT(vp, &obj) == 0) { 2883 VM_OBJECT_LOCK(obj); 2884 vm_object_page_clean(obj, 0, 0, 2885 flags == MNT_WAIT ? 2886 OBJPC_SYNC : OBJPC_NOSYNC); 2887 VM_OBJECT_UNLOCK(obj); 2888 } 2889 vput(vp); 2890 } 2891 MNT_ILOCK(mp); 2892 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) { 2893 if (--tries > 0) 2894 goto loop; 2895 break; 2896 } 2897 } else 2898 VI_UNLOCK(vp); 2899 } 2900 MNT_IUNLOCK(mp); 2901 } 2902 2903 /* 2904 * Create the VM object needed for VMIO and mmap support. This 2905 * is done for all VREG files in the system. Some filesystems might 2906 * afford the additional metadata buffering capability of the 2907 * VMIO code by making the device node be VMIO mode also. 2908 * 2909 * vp must be locked when vfs_object_create is called. 2910 */ 2911 int 2912 vfs_object_create(struct vnode *vp, struct thread *td, struct ucred *cred) 2913 { 2914 2915 GIANT_REQUIRED; 2916 return (VOP_CREATEVOBJECT(vp, cred, td)); 2917 } 2918 2919 /* 2920 * Mark a vnode as free, putting it up for recycling. 2921 */ 2922 void 2923 vfree(struct vnode *vp) 2924 { 2925 2926 ASSERT_VI_LOCKED(vp, "vfree"); 2927 mtx_lock(&vnode_free_list_mtx); 2928 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free")); 2929 if (vp->v_iflag & VI_AGE) { 2930 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2931 } else { 2932 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 2933 } 2934 freevnodes++; 2935 mtx_unlock(&vnode_free_list_mtx); 2936 vp->v_iflag &= ~VI_AGE; 2937 vp->v_iflag |= VI_FREE; 2938 } 2939 2940 /* 2941 * Opposite of vfree() - mark a vnode as in use. 2942 */ 2943 void 2944 vbusy(struct vnode *vp) 2945 { 2946 2947 ASSERT_VI_LOCKED(vp, "vbusy"); 2948 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free")); 2949 2950 mtx_lock(&vnode_free_list_mtx); 2951 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2952 freevnodes--; 2953 mtx_unlock(&vnode_free_list_mtx); 2954 2955 vp->v_iflag &= ~(VI_FREE|VI_AGE); 2956 } 2957 2958 /* 2959 * Initalize per-vnode helper structure to hold poll-related state. 2960 */ 2961 void 2962 v_addpollinfo(struct vnode *vp) 2963 { 2964 struct vpollinfo *vi; 2965 2966 vi = uma_zalloc(vnodepoll_zone, M_WAITOK); 2967 if (vp->v_pollinfo != NULL) { 2968 uma_zfree(vnodepoll_zone, vi); 2969 return; 2970 } 2971 vp->v_pollinfo = vi; 2972 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 2973 knlist_init(&vp->v_pollinfo->vpi_selinfo.si_note, 2974 &vp->v_pollinfo->vpi_lock); 2975 } 2976 2977 /* 2978 * Record a process's interest in events which might happen to 2979 * a vnode. Because poll uses the historic select-style interface 2980 * internally, this routine serves as both the ``check for any 2981 * pending events'' and the ``record my interest in future events'' 2982 * functions. (These are done together, while the lock is held, 2983 * to avoid race conditions.) 2984 */ 2985 int 2986 vn_pollrecord(vp, td, events) 2987 struct vnode *vp; 2988 struct thread *td; 2989 short events; 2990 { 2991 2992 if (vp->v_pollinfo == NULL) 2993 v_addpollinfo(vp); 2994 mtx_lock(&vp->v_pollinfo->vpi_lock); 2995 if (vp->v_pollinfo->vpi_revents & events) { 2996 /* 2997 * This leaves events we are not interested 2998 * in available for the other process which 2999 * which presumably had requested them 3000 * (otherwise they would never have been 3001 * recorded). 3002 */ 3003 events &= vp->v_pollinfo->vpi_revents; 3004 vp->v_pollinfo->vpi_revents &= ~events; 3005 3006 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3007 return events; 3008 } 3009 vp->v_pollinfo->vpi_events |= events; 3010 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3011 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3012 return 0; 3013 } 3014 3015 /* 3016 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 3017 * it is possible for us to miss an event due to race conditions, but 3018 * that condition is expected to be rare, so for the moment it is the 3019 * preferred interface. 3020 */ 3021 void 3022 vn_pollevent(vp, events) 3023 struct vnode *vp; 3024 short events; 3025 { 3026 3027 if (vp->v_pollinfo == NULL) 3028 v_addpollinfo(vp); 3029 mtx_lock(&vp->v_pollinfo->vpi_lock); 3030 if (vp->v_pollinfo->vpi_events & events) { 3031 /* 3032 * We clear vpi_events so that we don't 3033 * call selwakeup() twice if two events are 3034 * posted before the polling process(es) is 3035 * awakened. This also ensures that we take at 3036 * most one selwakeup() if the polling process 3037 * is no longer interested. However, it does 3038 * mean that only one event can be noticed at 3039 * a time. (Perhaps we should only clear those 3040 * event bits which we note?) XXX 3041 */ 3042 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */ 3043 vp->v_pollinfo->vpi_revents |= events; 3044 selwakeuppri(&vp->v_pollinfo->vpi_selinfo, PRIBIO); 3045 } 3046 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3047 } 3048 3049 /* 3050 * Wake up anyone polling on vp because it is being revoked. 3051 * This depends on dead_poll() returning POLLHUP for correct 3052 * behavior. 3053 */ 3054 void 3055 vn_pollgone(vp) 3056 struct vnode *vp; 3057 { 3058 3059 mtx_lock(&vp->v_pollinfo->vpi_lock); 3060 VN_KNOTE_LOCKED(vp, NOTE_REVOKE); 3061 if (vp->v_pollinfo->vpi_events) { 3062 vp->v_pollinfo->vpi_events = 0; 3063 selwakeuppri(&vp->v_pollinfo->vpi_selinfo, PRIBIO); 3064 } 3065 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3066 } 3067 3068 3069 3070 /* 3071 * Routine to create and manage a filesystem syncer vnode. 3072 */ 3073 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3074 static int sync_fsync(struct vop_fsync_args *); 3075 static int sync_inactive(struct vop_inactive_args *); 3076 static int sync_reclaim(struct vop_reclaim_args *); 3077 3078 static struct vop_vector sync_vnodeops = { 3079 .vop_bypass = VOP_EOPNOTSUPP, 3080 .vop_close = sync_close, /* close */ 3081 .vop_fsync = sync_fsync, /* fsync */ 3082 .vop_inactive = sync_inactive, /* inactive */ 3083 .vop_reclaim = sync_reclaim, /* reclaim */ 3084 .vop_lock = vop_stdlock, /* lock */ 3085 .vop_unlock = vop_stdunlock, /* unlock */ 3086 .vop_islocked = vop_stdislocked, /* islocked */ 3087 }; 3088 3089 /* 3090 * Create a new filesystem syncer vnode for the specified mount point. 3091 */ 3092 int 3093 vfs_allocate_syncvnode(mp) 3094 struct mount *mp; 3095 { 3096 struct vnode *vp; 3097 static long start, incr, next; 3098 int error; 3099 3100 /* Allocate a new vnode */ 3101 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) { 3102 mp->mnt_syncer = NULL; 3103 return (error); 3104 } 3105 vp->v_type = VNON; 3106 /* 3107 * Place the vnode onto the syncer worklist. We attempt to 3108 * scatter them about on the list so that they will go off 3109 * at evenly distributed times even if all the filesystems 3110 * are mounted at once. 3111 */ 3112 next += incr; 3113 if (next == 0 || next > syncer_maxdelay) { 3114 start /= 2; 3115 incr /= 2; 3116 if (start == 0) { 3117 start = syncer_maxdelay / 2; 3118 incr = syncer_maxdelay; 3119 } 3120 next = start; 3121 } 3122 VI_LOCK(vp); 3123 vn_syncer_add_to_worklist(&vp->v_bufobj, 3124 syncdelay > 0 ? next % syncdelay : 0); 3125 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 3126 mtx_lock(&sync_mtx); 3127 sync_vnode_count++; 3128 mtx_unlock(&sync_mtx); 3129 VI_UNLOCK(vp); 3130 mp->mnt_syncer = vp; 3131 return (0); 3132 } 3133 3134 /* 3135 * Do a lazy sync of the filesystem. 3136 */ 3137 static int 3138 sync_fsync(ap) 3139 struct vop_fsync_args /* { 3140 struct vnode *a_vp; 3141 struct ucred *a_cred; 3142 int a_waitfor; 3143 struct thread *a_td; 3144 } */ *ap; 3145 { 3146 struct vnode *syncvp = ap->a_vp; 3147 struct mount *mp = syncvp->v_mount; 3148 struct thread *td = ap->a_td; 3149 int error, asyncflag; 3150 struct bufobj *bo; 3151 3152 /* 3153 * We only need to do something if this is a lazy evaluation. 3154 */ 3155 if (ap->a_waitfor != MNT_LAZY) 3156 return (0); 3157 3158 /* 3159 * Move ourselves to the back of the sync list. 3160 */ 3161 bo = &syncvp->v_bufobj; 3162 BO_LOCK(bo); 3163 vn_syncer_add_to_worklist(bo, syncdelay); 3164 BO_UNLOCK(bo); 3165 3166 /* 3167 * Walk the list of vnodes pushing all that are dirty and 3168 * not already on the sync list. 3169 */ 3170 mtx_lock(&mountlist_mtx); 3171 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3172 mtx_unlock(&mountlist_mtx); 3173 return (0); 3174 } 3175 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3176 vfs_unbusy(mp, td); 3177 return (0); 3178 } 3179 asyncflag = mp->mnt_flag & MNT_ASYNC; 3180 mp->mnt_flag &= ~MNT_ASYNC; 3181 vfs_msync(mp, MNT_NOWAIT); 3182 error = VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td); 3183 if (asyncflag) 3184 mp->mnt_flag |= MNT_ASYNC; 3185 vn_finished_write(mp); 3186 vfs_unbusy(mp, td); 3187 return (error); 3188 } 3189 3190 /* 3191 * The syncer vnode is no referenced. 3192 */ 3193 static int 3194 sync_inactive(ap) 3195 struct vop_inactive_args /* { 3196 struct vnode *a_vp; 3197 struct thread *a_td; 3198 } */ *ap; 3199 { 3200 3201 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 3202 vgone(ap->a_vp); 3203 return (0); 3204 } 3205 3206 /* 3207 * The syncer vnode is no longer needed and is being decommissioned. 3208 * 3209 * Modifications to the worklist must be protected by sync_mtx. 3210 */ 3211 static int 3212 sync_reclaim(ap) 3213 struct vop_reclaim_args /* { 3214 struct vnode *a_vp; 3215 } */ *ap; 3216 { 3217 struct vnode *vp = ap->a_vp; 3218 struct bufobj *bo; 3219 3220 VI_LOCK(vp); 3221 bo = &vp->v_bufobj; 3222 vp->v_mount->mnt_syncer = NULL; 3223 if (bo->bo_flag & BO_ONWORKLST) { 3224 mtx_lock(&sync_mtx); 3225 LIST_REMOVE(bo, bo_synclist); 3226 syncer_worklist_len--; 3227 sync_vnode_count--; 3228 mtx_unlock(&sync_mtx); 3229 bo->bo_flag &= ~BO_ONWORKLST; 3230 } 3231 VI_UNLOCK(vp); 3232 3233 return (0); 3234 } 3235 3236 /* 3237 * Check if vnode represents a disk device 3238 */ 3239 int 3240 vn_isdisk(vp, errp) 3241 struct vnode *vp; 3242 int *errp; 3243 { 3244 int error; 3245 3246 error = 0; 3247 dev_lock(); 3248 if (vp->v_type != VCHR) 3249 error = ENOTBLK; 3250 else if (vp->v_rdev == NULL) 3251 error = ENXIO; 3252 else if (vp->v_rdev->si_devsw == NULL) 3253 error = ENXIO; 3254 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 3255 error = ENOTBLK; 3256 dev_unlock(); 3257 if (errp != NULL) 3258 *errp = error; 3259 return (error == 0); 3260 } 3261 3262 /* 3263 * Free data allocated by namei(); see namei(9) for details. 3264 */ 3265 void 3266 NDFREE(ndp, flags) 3267 struct nameidata *ndp; 3268 const u_int flags; 3269 { 3270 3271 if (!(flags & NDF_NO_FREE_PNBUF) && 3272 (ndp->ni_cnd.cn_flags & HASBUF)) { 3273 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 3274 ndp->ni_cnd.cn_flags &= ~HASBUF; 3275 } 3276 if (!(flags & NDF_NO_DVP_UNLOCK) && 3277 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 3278 ndp->ni_dvp != ndp->ni_vp) 3279 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 3280 if (!(flags & NDF_NO_DVP_RELE) && 3281 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3282 vrele(ndp->ni_dvp); 3283 ndp->ni_dvp = NULL; 3284 } 3285 if (!(flags & NDF_NO_VP_UNLOCK) && 3286 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3287 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 3288 if (!(flags & NDF_NO_VP_RELE) && 3289 ndp->ni_vp) { 3290 vrele(ndp->ni_vp); 3291 ndp->ni_vp = NULL; 3292 } 3293 if (!(flags & NDF_NO_STARTDIR_RELE) && 3294 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3295 vrele(ndp->ni_startdir); 3296 ndp->ni_startdir = NULL; 3297 } 3298 } 3299 3300 /* 3301 * Common filesystem object access control check routine. Accepts a 3302 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3303 * and optional call-by-reference privused argument allowing vaccess() 3304 * to indicate to the caller whether privilege was used to satisfy the 3305 * request (obsoleted). Returns 0 on success, or an errno on failure. 3306 */ 3307 int 3308 vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3309 enum vtype type; 3310 mode_t file_mode; 3311 uid_t file_uid; 3312 gid_t file_gid; 3313 mode_t acc_mode; 3314 struct ucred *cred; 3315 int *privused; 3316 { 3317 mode_t dac_granted; 3318 #ifdef CAPABILITIES 3319 mode_t cap_granted; 3320 #endif 3321 3322 /* 3323 * Look for a normal, non-privileged way to access the file/directory 3324 * as requested. If it exists, go with that. 3325 */ 3326 3327 if (privused != NULL) 3328 *privused = 0; 3329 3330 dac_granted = 0; 3331 3332 /* Check the owner. */ 3333 if (cred->cr_uid == file_uid) { 3334 dac_granted |= VADMIN; 3335 if (file_mode & S_IXUSR) 3336 dac_granted |= VEXEC; 3337 if (file_mode & S_IRUSR) 3338 dac_granted |= VREAD; 3339 if (file_mode & S_IWUSR) 3340 dac_granted |= (VWRITE | VAPPEND); 3341 3342 if ((acc_mode & dac_granted) == acc_mode) 3343 return (0); 3344 3345 goto privcheck; 3346 } 3347 3348 /* Otherwise, check the groups (first match) */ 3349 if (groupmember(file_gid, cred)) { 3350 if (file_mode & S_IXGRP) 3351 dac_granted |= VEXEC; 3352 if (file_mode & S_IRGRP) 3353 dac_granted |= VREAD; 3354 if (file_mode & S_IWGRP) 3355 dac_granted |= (VWRITE | VAPPEND); 3356 3357 if ((acc_mode & dac_granted) == acc_mode) 3358 return (0); 3359 3360 goto privcheck; 3361 } 3362 3363 /* Otherwise, check everyone else. */ 3364 if (file_mode & S_IXOTH) 3365 dac_granted |= VEXEC; 3366 if (file_mode & S_IROTH) 3367 dac_granted |= VREAD; 3368 if (file_mode & S_IWOTH) 3369 dac_granted |= (VWRITE | VAPPEND); 3370 if ((acc_mode & dac_granted) == acc_mode) 3371 return (0); 3372 3373 privcheck: 3374 if (!suser_cred(cred, SUSER_ALLOWJAIL)) { 3375 /* XXX audit: privilege used */ 3376 if (privused != NULL) 3377 *privused = 1; 3378 return (0); 3379 } 3380 3381 #ifdef CAPABILITIES 3382 /* 3383 * Build a capability mask to determine if the set of capabilities 3384 * satisfies the requirements when combined with the granted mask 3385 * from above. 3386 * For each capability, if the capability is required, bitwise 3387 * or the request type onto the cap_granted mask. 3388 */ 3389 cap_granted = 0; 3390 3391 if (type == VDIR) { 3392 /* 3393 * For directories, use CAP_DAC_READ_SEARCH to satisfy 3394 * VEXEC requests, instead of CAP_DAC_EXECUTE. 3395 */ 3396 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3397 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, SUSER_ALLOWJAIL)) 3398 cap_granted |= VEXEC; 3399 } else { 3400 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3401 !cap_check(cred, NULL, CAP_DAC_EXECUTE, SUSER_ALLOWJAIL)) 3402 cap_granted |= VEXEC; 3403 } 3404 3405 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3406 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, SUSER_ALLOWJAIL)) 3407 cap_granted |= VREAD; 3408 3409 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3410 !cap_check(cred, NULL, CAP_DAC_WRITE, SUSER_ALLOWJAIL)) 3411 cap_granted |= (VWRITE | VAPPEND); 3412 3413 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3414 !cap_check(cred, NULL, CAP_FOWNER, SUSER_ALLOWJAIL)) 3415 cap_granted |= VADMIN; 3416 3417 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3418 /* XXX audit: privilege used */ 3419 if (privused != NULL) 3420 *privused = 1; 3421 return (0); 3422 } 3423 #endif 3424 3425 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3426 } 3427 3428 /* 3429 * Credential check based on process requesting service, and per-attribute 3430 * permissions. 3431 */ 3432 int 3433 extattr_check_cred(struct vnode *vp, int attrnamespace, 3434 struct ucred *cred, struct thread *td, int access) 3435 { 3436 3437 /* 3438 * Kernel-invoked always succeeds. 3439 */ 3440 if (cred == NOCRED) 3441 return (0); 3442 3443 /* 3444 * Do not allow privileged processes in jail to directly 3445 * manipulate system attributes. 3446 * 3447 * XXX What capability should apply here? 3448 * Probably CAP_SYS_SETFFLAG. 3449 */ 3450 switch (attrnamespace) { 3451 case EXTATTR_NAMESPACE_SYSTEM: 3452 /* Potentially should be: return (EPERM); */ 3453 return (suser_cred(cred, 0)); 3454 case EXTATTR_NAMESPACE_USER: 3455 return (VOP_ACCESS(vp, access, cred, td)); 3456 default: 3457 return (EPERM); 3458 } 3459 } 3460 3461 #ifdef DEBUG_VFS_LOCKS 3462 /* 3463 * This only exists to supress warnings from unlocked specfs accesses. It is 3464 * no longer ok to have an unlocked VFS. 3465 */ 3466 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD) 3467 3468 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 3469 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, ""); 3470 3471 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 3472 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, ""); 3473 3474 int vfs_badlock_print = 1; /* Print lock violations. */ 3475 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, ""); 3476 3477 #ifdef KDB 3478 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 3479 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, ""); 3480 #endif 3481 3482 static void 3483 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 3484 { 3485 3486 #ifdef KDB 3487 if (vfs_badlock_backtrace) 3488 kdb_backtrace(); 3489 #endif 3490 if (vfs_badlock_print) 3491 printf("%s: %p %s\n", str, (void *)vp, msg); 3492 if (vfs_badlock_ddb) 3493 kdb_enter("lock violation"); 3494 } 3495 3496 void 3497 assert_vi_locked(struct vnode *vp, const char *str) 3498 { 3499 3500 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 3501 vfs_badlock("interlock is not locked but should be", str, vp); 3502 } 3503 3504 void 3505 assert_vi_unlocked(struct vnode *vp, const char *str) 3506 { 3507 3508 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 3509 vfs_badlock("interlock is locked but should not be", str, vp); 3510 } 3511 3512 void 3513 assert_vop_locked(struct vnode *vp, const char *str) 3514 { 3515 3516 if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0) 3517 vfs_badlock("is not locked but should be", str, vp); 3518 } 3519 3520 void 3521 assert_vop_unlocked(struct vnode *vp, const char *str) 3522 { 3523 3524 if (vp && !IGNORE_LOCK(vp) && 3525 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) 3526 vfs_badlock("is locked but should not be", str, vp); 3527 } 3528 3529 #if 0 3530 void 3531 assert_vop_elocked(struct vnode *vp, const char *str) 3532 { 3533 3534 if (vp && !IGNORE_LOCK(vp) && 3535 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE) 3536 vfs_badlock("is not exclusive locked but should be", str, vp); 3537 } 3538 3539 void 3540 assert_vop_elocked_other(struct vnode *vp, const char *str) 3541 { 3542 3543 if (vp && !IGNORE_LOCK(vp) && 3544 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER) 3545 vfs_badlock("is not exclusive locked by another thread", 3546 str, vp); 3547 } 3548 3549 void 3550 assert_vop_slocked(struct vnode *vp, const char *str) 3551 { 3552 3553 if (vp && !IGNORE_LOCK(vp) && 3554 VOP_ISLOCKED(vp, curthread) != LK_SHARED) 3555 vfs_badlock("is not locked shared but should be", str, vp); 3556 } 3557 #endif /* 0 */ 3558 3559 void 3560 vop_rename_pre(void *ap) 3561 { 3562 struct vop_rename_args *a = ap; 3563 3564 if (a->a_tvp) 3565 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 3566 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 3567 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 3568 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 3569 3570 /* Check the source (from). */ 3571 if (a->a_tdvp != a->a_fdvp) 3572 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 3573 if (a->a_tvp != a->a_fvp) 3574 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked"); 3575 3576 /* Check the target. */ 3577 if (a->a_tvp) 3578 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 3579 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 3580 } 3581 3582 void 3583 vop_strategy_pre(void *ap) 3584 { 3585 struct vop_strategy_args *a; 3586 struct buf *bp; 3587 3588 a = ap; 3589 bp = a->a_bp; 3590 3591 /* 3592 * Cluster ops lock their component buffers but not the IO container. 3593 */ 3594 if ((bp->b_flags & B_CLUSTER) != 0) 3595 return; 3596 3597 if (BUF_REFCNT(bp) < 1) { 3598 if (vfs_badlock_print) 3599 printf( 3600 "VOP_STRATEGY: bp is not locked but should be\n"); 3601 if (vfs_badlock_ddb) 3602 kdb_enter("lock violation"); 3603 } 3604 } 3605 3606 void 3607 vop_lookup_pre(void *ap) 3608 { 3609 struct vop_lookup_args *a; 3610 struct vnode *dvp; 3611 3612 a = ap; 3613 dvp = a->a_dvp; 3614 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3615 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3616 } 3617 3618 void 3619 vop_lookup_post(void *ap, int rc) 3620 { 3621 struct vop_lookup_args *a; 3622 struct componentname *cnp; 3623 struct vnode *dvp; 3624 struct vnode *vp; 3625 int flags; 3626 3627 a = ap; 3628 dvp = a->a_dvp; 3629 cnp = a->a_cnp; 3630 vp = *(a->a_vpp); 3631 flags = cnp->cn_flags; 3632 3633 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3634 3635 /* 3636 * If this is the last path component for this lookup and LOCKPARENT 3637 * is set, OR if there is an error the directory has to be locked. 3638 */ 3639 if ((flags & LOCKPARENT) && (flags & ISLASTCN)) 3640 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (LOCKPARENT)"); 3641 else if (rc != 0) 3642 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP (error)"); 3643 else if (dvp != vp) 3644 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (dvp)"); 3645 if (flags & PDIRUNLOCK) 3646 ASSERT_VOP_UNLOCKED(dvp, "VOP_LOOKUP (PDIRUNLOCK)"); 3647 } 3648 3649 void 3650 vop_lock_pre(void *ap) 3651 { 3652 struct vop_lock_args *a = ap; 3653 3654 if ((a->a_flags & LK_INTERLOCK) == 0) 3655 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3656 else 3657 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 3658 } 3659 3660 void 3661 vop_lock_post(void *ap, int rc) 3662 { 3663 struct vop_lock_args *a = ap; 3664 3665 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3666 if (rc == 0) 3667 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 3668 } 3669 3670 void 3671 vop_unlock_pre(void *ap) 3672 { 3673 struct vop_unlock_args *a = ap; 3674 3675 if (a->a_flags & LK_INTERLOCK) 3676 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 3677 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 3678 } 3679 3680 void 3681 vop_unlock_post(void *ap, int rc) 3682 { 3683 struct vop_unlock_args *a = ap; 3684 3685 if (a->a_flags & LK_INTERLOCK) 3686 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 3687 } 3688 #endif /* DEBUG_VFS_LOCKS */ 3689 3690 static struct knlist fs_knlist; 3691 3692 static void 3693 vfs_event_init(void *arg) 3694 { 3695 knlist_init(&fs_knlist, NULL); 3696 } 3697 /* XXX - correct order? */ 3698 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 3699 3700 void 3701 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused) 3702 { 3703 3704 KNOTE_UNLOCKED(&fs_knlist, event); 3705 } 3706 3707 static int filt_fsattach(struct knote *kn); 3708 static void filt_fsdetach(struct knote *kn); 3709 static int filt_fsevent(struct knote *kn, long hint); 3710 3711 struct filterops fs_filtops = 3712 { 0, filt_fsattach, filt_fsdetach, filt_fsevent }; 3713 3714 static int 3715 filt_fsattach(struct knote *kn) 3716 { 3717 3718 kn->kn_flags |= EV_CLEAR; 3719 knlist_add(&fs_knlist, kn, 0); 3720 return (0); 3721 } 3722 3723 static void 3724 filt_fsdetach(struct knote *kn) 3725 { 3726 3727 knlist_remove(&fs_knlist, kn, 0); 3728 } 3729 3730 static int 3731 filt_fsevent(struct knote *kn, long hint) 3732 { 3733 3734 kn->kn_fflags |= hint; 3735 return (kn->kn_fflags != 0); 3736 } 3737 3738 static int 3739 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 3740 { 3741 struct vfsidctl vc; 3742 int error; 3743 struct mount *mp; 3744 3745 error = SYSCTL_IN(req, &vc, sizeof(vc)); 3746 if (error) 3747 return (error); 3748 if (vc.vc_vers != VFS_CTL_VERS1) 3749 return (EINVAL); 3750 mp = vfs_getvfs(&vc.vc_fsid); 3751 if (mp == NULL) 3752 return (ENOENT); 3753 /* ensure that a specific sysctl goes to the right filesystem. */ 3754 if (strcmp(vc.vc_fstypename, "*") != 0 && 3755 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 3756 return (EINVAL); 3757 } 3758 VCTLTOREQ(&vc, req); 3759 return (VFS_SYSCTL(mp, vc.vc_op, req)); 3760 } 3761 3762 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, 3763 NULL, 0, sysctl_vfs_ctl, "", "Sysctl by fsid"); 3764 3765 /* 3766 * Function to initialize a va_filerev field sensibly. 3767 * XXX: Wouldn't a random number make a lot more sense ?? 3768 */ 3769 u_quad_t 3770 init_va_filerev(void) 3771 { 3772 struct bintime bt; 3773 3774 getbinuptime(&bt); 3775 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 3776 } 3777