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