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