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