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