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