1 /*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 35 */ 36 37 /* 38 * External virtual filesystem routines 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_ddb.h" 45 #include "opt_mac.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/bio.h> 50 #include <sys/buf.h> 51 #include <sys/conf.h> 52 #include <sys/dirent.h> 53 #include <sys/event.h> 54 #include <sys/eventhandler.h> 55 #include <sys/extattr.h> 56 #include <sys/file.h> 57 #include <sys/fcntl.h> 58 #include <sys/kdb.h> 59 #include <sys/kernel.h> 60 #include <sys/kthread.h> 61 #include <sys/malloc.h> 62 #include <sys/mount.h> 63 #include <sys/namei.h> 64 #include <sys/priv.h> 65 #include <sys/reboot.h> 66 #include <sys/sleepqueue.h> 67 #include <sys/stat.h> 68 #include <sys/sysctl.h> 69 #include <sys/syslog.h> 70 #include <sys/vmmeter.h> 71 #include <sys/vnode.h> 72 73 #include <machine/stdarg.h> 74 75 #include <security/mac/mac_framework.h> 76 77 #include <vm/vm.h> 78 #include <vm/vm_object.h> 79 #include <vm/vm_extern.h> 80 #include <vm/pmap.h> 81 #include <vm/vm_map.h> 82 #include <vm/vm_page.h> 83 #include <vm/vm_kern.h> 84 #include <vm/uma.h> 85 86 #ifdef DDB 87 #include <ddb/ddb.h> 88 #endif 89 90 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure"); 91 92 static void delmntque(struct vnode *vp); 93 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, 94 int slpflag, int slptimeo); 95 static void syncer_shutdown(void *arg, int howto); 96 static int vtryrecycle(struct vnode *vp); 97 static void vbusy(struct vnode *vp); 98 static void 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 = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 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 bo->bo_bsize = mp->mnt_stat.f_iosize; 946 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0) 947 vp->v_vflag |= VV_NOKNOTE; 948 } 949 950 CTR2(KTR_VFS, "getnewvnode: mp %p vp %p", mp, vp); 951 *vpp = vp; 952 return (0); 953 } 954 955 /* 956 * Delete from old mount point vnode list, if on one. 957 */ 958 static void 959 delmntque(struct vnode *vp) 960 { 961 struct mount *mp; 962 963 mp = vp->v_mount; 964 if (mp == NULL) 965 return; 966 MNT_ILOCK(mp); 967 vp->v_mount = NULL; 968 VNASSERT(mp->mnt_nvnodelistsize > 0, vp, 969 ("bad mount point vnode list size")); 970 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 971 mp->mnt_nvnodelistsize--; 972 MNT_REL(mp); 973 MNT_IUNLOCK(mp); 974 } 975 976 static void 977 insmntque_stddtr(struct vnode *vp, void *dtr_arg) 978 { 979 struct thread *td; 980 981 td = curthread; /* XXX ? */ 982 vp->v_data = NULL; 983 vp->v_op = &dead_vnodeops; 984 /* XXX non mp-safe fs may still call insmntque with vnode 985 unlocked */ 986 if (!VOP_ISLOCKED(vp, td)) 987 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 988 vgone(vp); 989 vput(vp); 990 } 991 992 /* 993 * Insert into list of vnodes for the new mount point, if available. 994 */ 995 int 996 insmntque1(struct vnode *vp, struct mount *mp, 997 void (*dtr)(struct vnode *, void *), void *dtr_arg) 998 { 999 1000 KASSERT(vp->v_mount == NULL, 1001 ("insmntque: vnode already on per mount vnode list")); 1002 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)")); 1003 MNT_ILOCK(mp); 1004 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 && 1005 mp->mnt_nvnodelistsize == 0) { 1006 MNT_IUNLOCK(mp); 1007 if (dtr != NULL) 1008 dtr(vp, dtr_arg); 1009 return (EBUSY); 1010 } 1011 vp->v_mount = mp; 1012 MNT_REF(mp); 1013 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1014 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp, 1015 ("neg mount point vnode list size")); 1016 mp->mnt_nvnodelistsize++; 1017 MNT_IUNLOCK(mp); 1018 return (0); 1019 } 1020 1021 int 1022 insmntque(struct vnode *vp, struct mount *mp) 1023 { 1024 1025 return (insmntque1(vp, mp, insmntque_stddtr, NULL)); 1026 } 1027 1028 /* 1029 * Flush out and invalidate all buffers associated with a bufobj 1030 * Called with the underlying object locked. 1031 */ 1032 int 1033 bufobj_invalbuf(struct bufobj *bo, int flags, struct thread *td, int slpflag, 1034 int slptimeo) 1035 { 1036 int error; 1037 1038 BO_LOCK(bo); 1039 if (flags & V_SAVE) { 1040 error = bufobj_wwait(bo, slpflag, slptimeo); 1041 if (error) { 1042 BO_UNLOCK(bo); 1043 return (error); 1044 } 1045 if (bo->bo_dirty.bv_cnt > 0) { 1046 BO_UNLOCK(bo); 1047 if ((error = BO_SYNC(bo, MNT_WAIT, td)) != 0) 1048 return (error); 1049 /* 1050 * XXX We could save a lock/unlock if this was only 1051 * enabled under INVARIANTS 1052 */ 1053 BO_LOCK(bo); 1054 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0) 1055 panic("vinvalbuf: dirty bufs"); 1056 } 1057 } 1058 /* 1059 * If you alter this loop please notice that interlock is dropped and 1060 * reacquired in flushbuflist. Special care is needed to ensure that 1061 * no race conditions occur from this. 1062 */ 1063 do { 1064 error = flushbuflist(&bo->bo_clean, 1065 flags, bo, slpflag, slptimeo); 1066 if (error == 0) 1067 error = flushbuflist(&bo->bo_dirty, 1068 flags, bo, slpflag, slptimeo); 1069 if (error != 0 && error != EAGAIN) { 1070 BO_UNLOCK(bo); 1071 return (error); 1072 } 1073 } while (error != 0); 1074 1075 /* 1076 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1077 * have write I/O in-progress but if there is a VM object then the 1078 * VM object can also have read-I/O in-progress. 1079 */ 1080 do { 1081 bufobj_wwait(bo, 0, 0); 1082 BO_UNLOCK(bo); 1083 if (bo->bo_object != NULL) { 1084 VM_OBJECT_LOCK(bo->bo_object); 1085 vm_object_pip_wait(bo->bo_object, "bovlbx"); 1086 VM_OBJECT_UNLOCK(bo->bo_object); 1087 } 1088 BO_LOCK(bo); 1089 } while (bo->bo_numoutput > 0); 1090 BO_UNLOCK(bo); 1091 1092 /* 1093 * Destroy the copy in the VM cache, too. 1094 */ 1095 if (bo->bo_object != NULL) { 1096 VM_OBJECT_LOCK(bo->bo_object); 1097 vm_object_page_remove(bo->bo_object, 0, 0, 1098 (flags & V_SAVE) ? TRUE : FALSE); 1099 VM_OBJECT_UNLOCK(bo->bo_object); 1100 } 1101 1102 #ifdef INVARIANTS 1103 BO_LOCK(bo); 1104 if ((flags & (V_ALT | V_NORMAL)) == 0 && 1105 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0)) 1106 panic("vinvalbuf: flush failed"); 1107 BO_UNLOCK(bo); 1108 #endif 1109 return (0); 1110 } 1111 1112 /* 1113 * Flush out and invalidate all buffers associated with a vnode. 1114 * Called with the underlying object locked. 1115 */ 1116 int 1117 vinvalbuf(struct vnode *vp, int flags, struct thread *td, int slpflag, 1118 int slptimeo) 1119 { 1120 1121 CTR2(KTR_VFS, "vinvalbuf vp %p flags %d", vp, flags); 1122 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1123 return (bufobj_invalbuf(&vp->v_bufobj, flags, td, slpflag, slptimeo)); 1124 } 1125 1126 /* 1127 * Flush out buffers on the specified list. 1128 * 1129 */ 1130 static int 1131 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag, 1132 int slptimeo) 1133 { 1134 struct buf *bp, *nbp; 1135 int retval, error; 1136 daddr_t lblkno; 1137 b_xflags_t xflags; 1138 1139 ASSERT_BO_LOCKED(bo); 1140 1141 retval = 0; 1142 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) { 1143 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1144 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 1145 continue; 1146 } 1147 lblkno = 0; 1148 xflags = 0; 1149 if (nbp != NULL) { 1150 lblkno = nbp->b_lblkno; 1151 xflags = nbp->b_xflags & 1152 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN); 1153 } 1154 retval = EAGAIN; 1155 error = BUF_TIMELOCK(bp, 1156 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo), 1157 "flushbuf", slpflag, slptimeo); 1158 if (error) { 1159 BO_LOCK(bo); 1160 return (error != ENOLCK ? error : EAGAIN); 1161 } 1162 KASSERT(bp->b_bufobj == bo, 1163 ("bp %p wrong b_bufobj %p should be %p", 1164 bp, bp->b_bufobj, bo)); 1165 if (bp->b_bufobj != bo) { /* XXX: necessary ? */ 1166 BUF_UNLOCK(bp); 1167 BO_LOCK(bo); 1168 return (EAGAIN); 1169 } 1170 /* 1171 * XXX Since there are no node locks for NFS, I 1172 * believe there is a slight chance that a delayed 1173 * write will occur while sleeping just above, so 1174 * check for it. 1175 */ 1176 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1177 (flags & V_SAVE)) { 1178 bremfree(bp); 1179 bp->b_flags |= B_ASYNC; 1180 bwrite(bp); 1181 BO_LOCK(bo); 1182 return (EAGAIN); /* XXX: why not loop ? */ 1183 } 1184 bremfree(bp); 1185 bp->b_flags |= (B_INVAL | B_RELBUF); 1186 bp->b_flags &= ~B_ASYNC; 1187 brelse(bp); 1188 BO_LOCK(bo); 1189 if (nbp != NULL && 1190 (nbp->b_bufobj != bo || 1191 nbp->b_lblkno != lblkno || 1192 (nbp->b_xflags & 1193 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags)) 1194 break; /* nbp invalid */ 1195 } 1196 return (retval); 1197 } 1198 1199 /* 1200 * Truncate a file's buffer and pages to a specified length. This 1201 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1202 * sync activity. 1203 */ 1204 int 1205 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td, 1206 off_t length, int blksize) 1207 { 1208 struct buf *bp, *nbp; 1209 int anyfreed; 1210 int trunclbn; 1211 struct bufobj *bo; 1212 1213 CTR2(KTR_VFS, "vtruncbuf vp %p length %jd", vp, length); 1214 /* 1215 * Round up to the *next* lbn. 1216 */ 1217 trunclbn = (length + blksize - 1) / blksize; 1218 1219 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1220 restart: 1221 VI_LOCK(vp); 1222 bo = &vp->v_bufobj; 1223 anyfreed = 1; 1224 for (;anyfreed;) { 1225 anyfreed = 0; 1226 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) { 1227 if (bp->b_lblkno < trunclbn) 1228 continue; 1229 if (BUF_LOCK(bp, 1230 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1231 VI_MTX(vp)) == ENOLCK) 1232 goto restart; 1233 1234 bremfree(bp); 1235 bp->b_flags |= (B_INVAL | B_RELBUF); 1236 bp->b_flags &= ~B_ASYNC; 1237 brelse(bp); 1238 anyfreed = 1; 1239 1240 if (nbp != NULL && 1241 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1242 (nbp->b_vp != vp) || 1243 (nbp->b_flags & B_DELWRI))) { 1244 goto restart; 1245 } 1246 VI_LOCK(vp); 1247 } 1248 1249 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1250 if (bp->b_lblkno < trunclbn) 1251 continue; 1252 if (BUF_LOCK(bp, 1253 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1254 VI_MTX(vp)) == ENOLCK) 1255 goto restart; 1256 bremfree(bp); 1257 bp->b_flags |= (B_INVAL | B_RELBUF); 1258 bp->b_flags &= ~B_ASYNC; 1259 brelse(bp); 1260 anyfreed = 1; 1261 if (nbp != NULL && 1262 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1263 (nbp->b_vp != vp) || 1264 (nbp->b_flags & B_DELWRI) == 0)) { 1265 goto restart; 1266 } 1267 VI_LOCK(vp); 1268 } 1269 } 1270 1271 if (length > 0) { 1272 restartsync: 1273 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 1274 if (bp->b_lblkno > 0) 1275 continue; 1276 /* 1277 * Since we hold the vnode lock this should only 1278 * fail if we're racing with the buf daemon. 1279 */ 1280 if (BUF_LOCK(bp, 1281 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 1282 VI_MTX(vp)) == ENOLCK) { 1283 goto restart; 1284 } 1285 VNASSERT((bp->b_flags & B_DELWRI), vp, 1286 ("buf(%p) on dirty queue without DELWRI", bp)); 1287 1288 bremfree(bp); 1289 bawrite(bp); 1290 VI_LOCK(vp); 1291 goto restartsync; 1292 } 1293 } 1294 1295 bufobj_wwait(bo, 0, 0); 1296 VI_UNLOCK(vp); 1297 vnode_pager_setsize(vp, length); 1298 1299 return (0); 1300 } 1301 1302 /* 1303 * buf_splay() - splay tree core for the clean/dirty list of buffers in 1304 * a vnode. 1305 * 1306 * NOTE: We have to deal with the special case of a background bitmap 1307 * buffer, a situation where two buffers will have the same logical 1308 * block offset. We want (1) only the foreground buffer to be accessed 1309 * in a lookup and (2) must differentiate between the foreground and 1310 * background buffer in the splay tree algorithm because the splay 1311 * tree cannot normally handle multiple entities with the same 'index'. 1312 * We accomplish this by adding differentiating flags to the splay tree's 1313 * numerical domain. 1314 */ 1315 static 1316 struct buf * 1317 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 1318 { 1319 struct buf dummy; 1320 struct buf *lefttreemax, *righttreemin, *y; 1321 1322 if (root == NULL) 1323 return (NULL); 1324 lefttreemax = righttreemin = &dummy; 1325 for (;;) { 1326 if (lblkno < root->b_lblkno || 1327 (lblkno == root->b_lblkno && 1328 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1329 if ((y = root->b_left) == NULL) 1330 break; 1331 if (lblkno < y->b_lblkno) { 1332 /* Rotate right. */ 1333 root->b_left = y->b_right; 1334 y->b_right = root; 1335 root = y; 1336 if ((y = root->b_left) == NULL) 1337 break; 1338 } 1339 /* Link into the new root's right tree. */ 1340 righttreemin->b_left = root; 1341 righttreemin = root; 1342 } else if (lblkno > root->b_lblkno || 1343 (lblkno == root->b_lblkno && 1344 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 1345 if ((y = root->b_right) == NULL) 1346 break; 1347 if (lblkno > y->b_lblkno) { 1348 /* Rotate left. */ 1349 root->b_right = y->b_left; 1350 y->b_left = root; 1351 root = y; 1352 if ((y = root->b_right) == NULL) 1353 break; 1354 } 1355 /* Link into the new root's left tree. */ 1356 lefttreemax->b_right = root; 1357 lefttreemax = root; 1358 } else { 1359 break; 1360 } 1361 root = y; 1362 } 1363 /* Assemble the new root. */ 1364 lefttreemax->b_right = root->b_left; 1365 righttreemin->b_left = root->b_right; 1366 root->b_left = dummy.b_right; 1367 root->b_right = dummy.b_left; 1368 return (root); 1369 } 1370 1371 static void 1372 buf_vlist_remove(struct buf *bp) 1373 { 1374 struct buf *root; 1375 struct bufv *bv; 1376 1377 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp)); 1378 ASSERT_BO_LOCKED(bp->b_bufobj); 1379 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) != 1380 (BX_VNDIRTY|BX_VNCLEAN), 1381 ("buf_vlist_remove: Buf %p is on two lists", bp)); 1382 if (bp->b_xflags & BX_VNDIRTY) 1383 bv = &bp->b_bufobj->bo_dirty; 1384 else 1385 bv = &bp->b_bufobj->bo_clean; 1386 if (bp != bv->bv_root) { 1387 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root); 1388 KASSERT(root == bp, ("splay lookup failed in remove")); 1389 } 1390 if (bp->b_left == NULL) { 1391 root = bp->b_right; 1392 } else { 1393 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1394 root->b_right = bp->b_right; 1395 } 1396 bv->bv_root = root; 1397 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs); 1398 bv->bv_cnt--; 1399 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1400 } 1401 1402 /* 1403 * Add the buffer to the sorted clean or dirty block list using a 1404 * splay tree algorithm. 1405 * 1406 * NOTE: xflags is passed as a constant, optimizing this inline function! 1407 */ 1408 static void 1409 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags) 1410 { 1411 struct buf *root; 1412 struct bufv *bv; 1413 1414 ASSERT_BO_LOCKED(bo); 1415 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1416 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags)); 1417 bp->b_xflags |= xflags; 1418 if (xflags & BX_VNDIRTY) 1419 bv = &bo->bo_dirty; 1420 else 1421 bv = &bo->bo_clean; 1422 1423 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root); 1424 if (root == NULL) { 1425 bp->b_left = NULL; 1426 bp->b_right = NULL; 1427 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs); 1428 } else if (bp->b_lblkno < root->b_lblkno || 1429 (bp->b_lblkno == root->b_lblkno && 1430 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1431 bp->b_left = root->b_left; 1432 bp->b_right = root; 1433 root->b_left = NULL; 1434 TAILQ_INSERT_BEFORE(root, bp, b_bobufs); 1435 } else { 1436 bp->b_right = root->b_right; 1437 bp->b_left = root; 1438 root->b_right = NULL; 1439 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs); 1440 } 1441 bv->bv_cnt++; 1442 bv->bv_root = bp; 1443 } 1444 1445 /* 1446 * Lookup a buffer using the splay tree. Note that we specifically avoid 1447 * shadow buffers used in background bitmap writes. 1448 * 1449 * This code isn't quite efficient as it could be because we are maintaining 1450 * two sorted lists and do not know which list the block resides in. 1451 * 1452 * During a "make buildworld" the desired buffer is found at one of 1453 * the roots more than 60% of the time. Thus, checking both roots 1454 * before performing either splay eliminates unnecessary splays on the 1455 * first tree splayed. 1456 */ 1457 struct buf * 1458 gbincore(struct bufobj *bo, daddr_t lblkno) 1459 { 1460 struct buf *bp; 1461 1462 ASSERT_BO_LOCKED(bo); 1463 if ((bp = bo->bo_clean.bv_root) != NULL && 1464 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1465 return (bp); 1466 if ((bp = bo->bo_dirty.bv_root) != NULL && 1467 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1468 return (bp); 1469 if ((bp = bo->bo_clean.bv_root) != NULL) { 1470 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp); 1471 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1472 return (bp); 1473 } 1474 if ((bp = bo->bo_dirty.bv_root) != NULL) { 1475 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp); 1476 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1477 return (bp); 1478 } 1479 return (NULL); 1480 } 1481 1482 /* 1483 * Associate a buffer with a vnode. 1484 */ 1485 void 1486 bgetvp(struct vnode *vp, struct buf *bp) 1487 { 1488 1489 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free")); 1490 1491 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags); 1492 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp, 1493 ("bgetvp: bp already attached! %p", bp)); 1494 1495 ASSERT_VI_LOCKED(vp, "bgetvp"); 1496 vholdl(vp); 1497 if (VFS_NEEDSGIANT(vp->v_mount) || 1498 vp->v_bufobj.bo_flag & BO_NEEDSGIANT) 1499 bp->b_flags |= B_NEEDSGIANT; 1500 bp->b_vp = vp; 1501 bp->b_bufobj = &vp->v_bufobj; 1502 /* 1503 * Insert onto list for new vnode. 1504 */ 1505 buf_vlist_add(bp, &vp->v_bufobj, BX_VNCLEAN); 1506 } 1507 1508 /* 1509 * Disassociate a buffer from a vnode. 1510 */ 1511 void 1512 brelvp(struct buf *bp) 1513 { 1514 struct bufobj *bo; 1515 struct vnode *vp; 1516 1517 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags); 1518 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1519 1520 /* 1521 * Delete from old vnode list, if on one. 1522 */ 1523 vp = bp->b_vp; /* XXX */ 1524 bo = bp->b_bufobj; 1525 BO_LOCK(bo); 1526 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1527 buf_vlist_remove(bp); 1528 else 1529 panic("brelvp: Buffer %p not on queue.", bp); 1530 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 1531 bo->bo_flag &= ~BO_ONWORKLST; 1532 mtx_lock(&sync_mtx); 1533 LIST_REMOVE(bo, bo_synclist); 1534 syncer_worklist_len--; 1535 mtx_unlock(&sync_mtx); 1536 } 1537 bp->b_flags &= ~B_NEEDSGIANT; 1538 bp->b_vp = NULL; 1539 bp->b_bufobj = NULL; 1540 vdropl(vp); 1541 } 1542 1543 /* 1544 * Add an item to the syncer work queue. 1545 */ 1546 static void 1547 vn_syncer_add_to_worklist(struct bufobj *bo, int delay) 1548 { 1549 int slot; 1550 1551 ASSERT_BO_LOCKED(bo); 1552 1553 mtx_lock(&sync_mtx); 1554 if (bo->bo_flag & BO_ONWORKLST) 1555 LIST_REMOVE(bo, bo_synclist); 1556 else { 1557 bo->bo_flag |= BO_ONWORKLST; 1558 syncer_worklist_len++; 1559 } 1560 1561 if (delay > syncer_maxdelay - 2) 1562 delay = syncer_maxdelay - 2; 1563 slot = (syncer_delayno + delay) & syncer_mask; 1564 1565 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist); 1566 mtx_unlock(&sync_mtx); 1567 } 1568 1569 static int 1570 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS) 1571 { 1572 int error, len; 1573 1574 mtx_lock(&sync_mtx); 1575 len = syncer_worklist_len - sync_vnode_count; 1576 mtx_unlock(&sync_mtx); 1577 error = SYSCTL_OUT(req, &len, sizeof(len)); 1578 return (error); 1579 } 1580 1581 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 1582 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length"); 1583 1584 static struct proc *updateproc; 1585 static void sched_sync(void); 1586 static struct kproc_desc up_kp = { 1587 "syncer", 1588 sched_sync, 1589 &updateproc 1590 }; 1591 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 1592 1593 static int 1594 sync_vnode(struct bufobj *bo, struct thread *td) 1595 { 1596 struct vnode *vp; 1597 struct mount *mp; 1598 1599 vp = bo->__bo_vnode; /* XXX */ 1600 if (VOP_ISLOCKED(vp, NULL) != 0) 1601 return (1); 1602 if (VI_TRYLOCK(vp) == 0) 1603 return (1); 1604 /* 1605 * We use vhold in case the vnode does not 1606 * successfully sync. vhold prevents the vnode from 1607 * going away when we unlock the sync_mtx so that 1608 * we can acquire the vnode interlock. 1609 */ 1610 vholdl(vp); 1611 mtx_unlock(&sync_mtx); 1612 VI_UNLOCK(vp); 1613 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) { 1614 vdrop(vp); 1615 mtx_lock(&sync_mtx); 1616 return (1); 1617 } 1618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1619 (void) VOP_FSYNC(vp, MNT_LAZY, td); 1620 VOP_UNLOCK(vp, 0, td); 1621 vn_finished_write(mp); 1622 VI_LOCK(vp); 1623 if ((bo->bo_flag & BO_ONWORKLST) != 0) { 1624 /* 1625 * Put us back on the worklist. The worklist 1626 * routine will remove us from our current 1627 * position and then add us back in at a later 1628 * position. 1629 */ 1630 vn_syncer_add_to_worklist(bo, syncdelay); 1631 } 1632 vdropl(vp); 1633 mtx_lock(&sync_mtx); 1634 return (0); 1635 } 1636 1637 /* 1638 * System filesystem synchronizer daemon. 1639 */ 1640 static void 1641 sched_sync(void) 1642 { 1643 struct synclist *next; 1644 struct synclist *slp; 1645 struct bufobj *bo; 1646 long starttime; 1647 struct thread *td = FIRST_THREAD_IN_PROC(updateproc); 1648 static int dummychan; 1649 int last_work_seen; 1650 int net_worklist_len; 1651 int syncer_final_iter; 1652 int first_printf; 1653 int error; 1654 1655 mtx_lock(&Giant); 1656 last_work_seen = 0; 1657 syncer_final_iter = 0; 1658 first_printf = 1; 1659 syncer_state = SYNCER_RUNNING; 1660 starttime = time_uptime; 1661 td->td_pflags |= TDP_NORUNNINGBUF; 1662 1663 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc, 1664 SHUTDOWN_PRI_LAST); 1665 1666 mtx_lock(&sync_mtx); 1667 for (;;) { 1668 if (syncer_state == SYNCER_FINAL_DELAY && 1669 syncer_final_iter == 0) { 1670 mtx_unlock(&sync_mtx); 1671 kthread_suspend_check(td->td_proc); 1672 mtx_lock(&sync_mtx); 1673 } 1674 net_worklist_len = syncer_worklist_len - sync_vnode_count; 1675 if (syncer_state != SYNCER_RUNNING && 1676 starttime != time_uptime) { 1677 if (first_printf) { 1678 printf("\nSyncing disks, vnodes remaining..."); 1679 first_printf = 0; 1680 } 1681 printf("%d ", net_worklist_len); 1682 } 1683 starttime = time_uptime; 1684 1685 /* 1686 * Push files whose dirty time has expired. Be careful 1687 * of interrupt race on slp queue. 1688 * 1689 * Skip over empty worklist slots when shutting down. 1690 */ 1691 do { 1692 slp = &syncer_workitem_pending[syncer_delayno]; 1693 syncer_delayno += 1; 1694 if (syncer_delayno == syncer_maxdelay) 1695 syncer_delayno = 0; 1696 next = &syncer_workitem_pending[syncer_delayno]; 1697 /* 1698 * If the worklist has wrapped since the 1699 * it was emptied of all but syncer vnodes, 1700 * switch to the FINAL_DELAY state and run 1701 * for one more second. 1702 */ 1703 if (syncer_state == SYNCER_SHUTTING_DOWN && 1704 net_worklist_len == 0 && 1705 last_work_seen == syncer_delayno) { 1706 syncer_state = SYNCER_FINAL_DELAY; 1707 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP; 1708 } 1709 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) && 1710 syncer_worklist_len > 0); 1711 1712 /* 1713 * Keep track of the last time there was anything 1714 * on the worklist other than syncer vnodes. 1715 * Return to the SHUTTING_DOWN state if any 1716 * new work appears. 1717 */ 1718 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING) 1719 last_work_seen = syncer_delayno; 1720 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY) 1721 syncer_state = SYNCER_SHUTTING_DOWN; 1722 while ((bo = LIST_FIRST(slp)) != NULL) { 1723 error = sync_vnode(bo, td); 1724 if (error == 1) { 1725 LIST_REMOVE(bo, bo_synclist); 1726 LIST_INSERT_HEAD(next, bo, bo_synclist); 1727 continue; 1728 } 1729 } 1730 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0) 1731 syncer_final_iter--; 1732 /* 1733 * The variable rushjob allows the kernel to speed up the 1734 * processing of the filesystem syncer process. A rushjob 1735 * value of N tells the filesystem syncer to process the next 1736 * N seconds worth of work on its queue ASAP. Currently rushjob 1737 * is used by the soft update code to speed up the filesystem 1738 * syncer process when the incore state is getting so far 1739 * ahead of the disk that the kernel memory pool is being 1740 * threatened with exhaustion. 1741 */ 1742 if (rushjob > 0) { 1743 rushjob -= 1; 1744 continue; 1745 } 1746 /* 1747 * Just sleep for a short period of time between 1748 * iterations when shutting down to allow some I/O 1749 * to happen. 1750 * 1751 * If it has taken us less than a second to process the 1752 * current work, then wait. Otherwise start right over 1753 * again. We can still lose time if any single round 1754 * takes more than two seconds, but it does not really 1755 * matter as we are just trying to generally pace the 1756 * filesystem activity. 1757 */ 1758 if (syncer_state != SYNCER_RUNNING) 1759 msleep(&dummychan, &sync_mtx, PPAUSE, "syncfnl", 1760 hz / SYNCER_SHUTDOWN_SPEEDUP); 1761 else if (time_uptime == starttime) 1762 msleep(&lbolt, &sync_mtx, PPAUSE, "syncer", 0); 1763 } 1764 } 1765 1766 /* 1767 * Request the syncer daemon to speed up its work. 1768 * We never push it to speed up more than half of its 1769 * normal turn time, otherwise it could take over the cpu. 1770 */ 1771 int 1772 speedup_syncer(void) 1773 { 1774 struct thread *td; 1775 int ret = 0; 1776 1777 td = FIRST_THREAD_IN_PROC(updateproc); 1778 sleepq_remove(td, &lbolt); 1779 mtx_lock(&sync_mtx); 1780 if (rushjob < syncdelay / 2) { 1781 rushjob += 1; 1782 stat_rush_requests += 1; 1783 ret = 1; 1784 } 1785 mtx_unlock(&sync_mtx); 1786 return (ret); 1787 } 1788 1789 /* 1790 * Tell the syncer to speed up its work and run though its work 1791 * list several times, then tell it to shut down. 1792 */ 1793 static void 1794 syncer_shutdown(void *arg, int howto) 1795 { 1796 struct thread *td; 1797 1798 if (howto & RB_NOSYNC) 1799 return; 1800 td = FIRST_THREAD_IN_PROC(updateproc); 1801 sleepq_remove(td, &lbolt); 1802 mtx_lock(&sync_mtx); 1803 syncer_state = SYNCER_SHUTTING_DOWN; 1804 rushjob = 0; 1805 mtx_unlock(&sync_mtx); 1806 kproc_shutdown(arg, howto); 1807 } 1808 1809 /* 1810 * Reassign a buffer from one vnode to another. 1811 * Used to assign file specific control information 1812 * (indirect blocks) to the vnode to which they belong. 1813 */ 1814 void 1815 reassignbuf(struct buf *bp) 1816 { 1817 struct vnode *vp; 1818 struct bufobj *bo; 1819 int delay; 1820 #ifdef INVARIANTS 1821 struct bufv *bv; 1822 #endif 1823 1824 vp = bp->b_vp; 1825 bo = bp->b_bufobj; 1826 ++reassignbufcalls; 1827 1828 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X", 1829 bp, bp->b_vp, bp->b_flags); 1830 /* 1831 * B_PAGING flagged buffers cannot be reassigned because their vp 1832 * is not fully linked in. 1833 */ 1834 if (bp->b_flags & B_PAGING) 1835 panic("cannot reassign paging buffer"); 1836 1837 /* 1838 * Delete from old vnode list, if on one. 1839 */ 1840 VI_LOCK(vp); 1841 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1842 buf_vlist_remove(bp); 1843 else 1844 panic("reassignbuf: Buffer %p not on queue.", bp); 1845 /* 1846 * If dirty, put on list of dirty buffers; otherwise insert onto list 1847 * of clean buffers. 1848 */ 1849 if (bp->b_flags & B_DELWRI) { 1850 if ((bo->bo_flag & BO_ONWORKLST) == 0) { 1851 switch (vp->v_type) { 1852 case VDIR: 1853 delay = dirdelay; 1854 break; 1855 case VCHR: 1856 delay = metadelay; 1857 break; 1858 default: 1859 delay = filedelay; 1860 } 1861 vn_syncer_add_to_worklist(bo, delay); 1862 } 1863 buf_vlist_add(bp, bo, BX_VNDIRTY); 1864 } else { 1865 buf_vlist_add(bp, bo, BX_VNCLEAN); 1866 1867 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) { 1868 mtx_lock(&sync_mtx); 1869 LIST_REMOVE(bo, bo_synclist); 1870 syncer_worklist_len--; 1871 mtx_unlock(&sync_mtx); 1872 bo->bo_flag &= ~BO_ONWORKLST; 1873 } 1874 } 1875 #ifdef INVARIANTS 1876 bv = &bo->bo_clean; 1877 bp = TAILQ_FIRST(&bv->bv_hd); 1878 KASSERT(bp == NULL || bp->b_bufobj == bo, 1879 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1880 bp = TAILQ_LAST(&bv->bv_hd, buflists); 1881 KASSERT(bp == NULL || bp->b_bufobj == bo, 1882 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1883 bv = &bo->bo_dirty; 1884 bp = TAILQ_FIRST(&bv->bv_hd); 1885 KASSERT(bp == NULL || bp->b_bufobj == bo, 1886 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1887 bp = TAILQ_LAST(&bv->bv_hd, buflists); 1888 KASSERT(bp == NULL || bp->b_bufobj == bo, 1889 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo)); 1890 #endif 1891 VI_UNLOCK(vp); 1892 } 1893 1894 /* 1895 * Increment the use and hold counts on the vnode, taking care to reference 1896 * the driver's usecount if this is a chardev. The vholdl() will remove 1897 * the vnode from the free list if it is presently free. Requires the 1898 * vnode interlock and returns with it held. 1899 */ 1900 static void 1901 v_incr_usecount(struct vnode *vp) 1902 { 1903 1904 CTR3(KTR_VFS, "v_incr_usecount: vp %p holdcnt %d usecount %d\n", 1905 vp, vp->v_holdcnt, vp->v_usecount); 1906 vp->v_usecount++; 1907 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 1908 dev_lock(); 1909 vp->v_rdev->si_usecount++; 1910 dev_unlock(); 1911 } 1912 vholdl(vp); 1913 } 1914 1915 /* 1916 * Turn a holdcnt into a use+holdcnt such that only one call to 1917 * v_decr_usecount is needed. 1918 */ 1919 static void 1920 v_upgrade_usecount(struct vnode *vp) 1921 { 1922 1923 CTR3(KTR_VFS, "v_upgrade_usecount: vp %p holdcnt %d usecount %d\n", 1924 vp, vp->v_holdcnt, vp->v_usecount); 1925 vp->v_usecount++; 1926 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 1927 dev_lock(); 1928 vp->v_rdev->si_usecount++; 1929 dev_unlock(); 1930 } 1931 } 1932 1933 /* 1934 * Decrement the vnode use and hold count along with the driver's usecount 1935 * if this is a chardev. The vdropl() below releases the vnode interlock 1936 * as it may free the vnode. 1937 */ 1938 static void 1939 v_decr_usecount(struct vnode *vp) 1940 { 1941 1942 CTR3(KTR_VFS, "v_decr_usecount: vp %p holdcnt %d usecount %d\n", 1943 vp, vp->v_holdcnt, vp->v_usecount); 1944 ASSERT_VI_LOCKED(vp, __FUNCTION__); 1945 VNASSERT(vp->v_usecount > 0, vp, 1946 ("v_decr_usecount: negative usecount")); 1947 vp->v_usecount--; 1948 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 1949 dev_lock(); 1950 vp->v_rdev->si_usecount--; 1951 dev_unlock(); 1952 } 1953 vdropl(vp); 1954 } 1955 1956 /* 1957 * Decrement only the use count and driver use count. This is intended to 1958 * be paired with a follow on vdropl() to release the remaining hold count. 1959 * In this way we may vgone() a vnode with a 0 usecount without risk of 1960 * having it end up on a free list because the hold count is kept above 0. 1961 */ 1962 static void 1963 v_decr_useonly(struct vnode *vp) 1964 { 1965 1966 CTR3(KTR_VFS, "v_decr_useonly: vp %p holdcnt %d usecount %d\n", 1967 vp, vp->v_holdcnt, vp->v_usecount); 1968 ASSERT_VI_LOCKED(vp, __FUNCTION__); 1969 VNASSERT(vp->v_usecount > 0, vp, 1970 ("v_decr_useonly: negative usecount")); 1971 vp->v_usecount--; 1972 if (vp->v_type == VCHR && vp->v_rdev != NULL) { 1973 dev_lock(); 1974 vp->v_rdev->si_usecount--; 1975 dev_unlock(); 1976 } 1977 } 1978 1979 /* 1980 * Grab a particular vnode from the free list, increment its 1981 * reference count and lock it. The vnode lock bit is set if the 1982 * vnode is being eliminated in vgone. The process is awakened 1983 * when the transition is completed, and an error returned to 1984 * indicate that the vnode is no longer usable (possibly having 1985 * been changed to a new filesystem type). 1986 */ 1987 int 1988 vget(struct vnode *vp, int flags, struct thread *td) 1989 { 1990 int oweinact; 1991 int oldflags; 1992 int error; 1993 1994 error = 0; 1995 oldflags = flags; 1996 oweinact = 0; 1997 VFS_ASSERT_GIANT(vp->v_mount); 1998 if ((flags & LK_INTERLOCK) == 0) 1999 VI_LOCK(vp); 2000 /* 2001 * If the inactive call was deferred because vput() was called 2002 * with a shared lock, we have to do it here before another thread 2003 * gets a reference to data that should be dead. 2004 */ 2005 if (vp->v_iflag & VI_OWEINACT) { 2006 if (flags & LK_NOWAIT) { 2007 VI_UNLOCK(vp); 2008 return (EBUSY); 2009 } 2010 flags &= ~LK_TYPE_MASK; 2011 flags |= LK_EXCLUSIVE; 2012 oweinact = 1; 2013 } 2014 vholdl(vp); 2015 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) { 2016 vdrop(vp); 2017 return (error); 2018 } 2019 VI_LOCK(vp); 2020 /* Upgrade our holdcnt to a usecount. */ 2021 v_upgrade_usecount(vp); 2022 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0) 2023 panic("vget: vn_lock failed to return ENOENT\n"); 2024 if (oweinact) { 2025 if (vp->v_iflag & VI_OWEINACT) 2026 vinactive(vp, td); 2027 VI_UNLOCK(vp); 2028 if ((oldflags & LK_TYPE_MASK) == 0) 2029 VOP_UNLOCK(vp, 0, td); 2030 } else 2031 VI_UNLOCK(vp); 2032 return (0); 2033 } 2034 2035 /* 2036 * Increase the reference count of a vnode. 2037 */ 2038 void 2039 vref(struct vnode *vp) 2040 { 2041 2042 VI_LOCK(vp); 2043 v_incr_usecount(vp); 2044 VI_UNLOCK(vp); 2045 } 2046 2047 /* 2048 * Return reference count of a vnode. 2049 * 2050 * The results of this call are only guaranteed when some mechanism other 2051 * than the VI lock is used to stop other processes from gaining references 2052 * to the vnode. This may be the case if the caller holds the only reference. 2053 * This is also useful when stale data is acceptable as race conditions may 2054 * be accounted for by some other means. 2055 */ 2056 int 2057 vrefcnt(struct vnode *vp) 2058 { 2059 int usecnt; 2060 2061 VI_LOCK(vp); 2062 usecnt = vp->v_usecount; 2063 VI_UNLOCK(vp); 2064 2065 return (usecnt); 2066 } 2067 2068 2069 /* 2070 * Vnode put/release. 2071 * If count drops to zero, call inactive routine and return to freelist. 2072 */ 2073 void 2074 vrele(struct vnode *vp) 2075 { 2076 struct thread *td = curthread; /* XXX */ 2077 2078 KASSERT(vp != NULL, ("vrele: null vp")); 2079 VFS_ASSERT_GIANT(vp->v_mount); 2080 2081 VI_LOCK(vp); 2082 2083 /* Skip this v_writecount check if we're going to panic below. */ 2084 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp, 2085 ("vrele: missed vn_close")); 2086 2087 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2088 vp->v_usecount == 1)) { 2089 v_decr_usecount(vp); 2090 return; 2091 } 2092 if (vp->v_usecount != 1) { 2093 #ifdef DIAGNOSTIC 2094 vprint("vrele: negative ref count", vp); 2095 #endif 2096 VI_UNLOCK(vp); 2097 panic("vrele: negative ref cnt"); 2098 } 2099 /* 2100 * We want to hold the vnode until the inactive finishes to 2101 * prevent vgone() races. We drop the use count here and the 2102 * hold count below when we're done. 2103 */ 2104 v_decr_useonly(vp); 2105 /* 2106 * We must call VOP_INACTIVE with the node locked. Mark 2107 * as VI_DOINGINACT to avoid recursion. 2108 */ 2109 vp->v_iflag |= VI_OWEINACT; 2110 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) { 2111 VI_LOCK(vp); 2112 if (vp->v_usecount > 0) 2113 vp->v_iflag &= ~VI_OWEINACT; 2114 if (vp->v_iflag & VI_OWEINACT) 2115 vinactive(vp, td); 2116 VOP_UNLOCK(vp, 0, td); 2117 } else { 2118 VI_LOCK(vp); 2119 if (vp->v_usecount > 0) 2120 vp->v_iflag &= ~VI_OWEINACT; 2121 } 2122 vdropl(vp); 2123 } 2124 2125 /* 2126 * Release an already locked vnode. This give the same effects as 2127 * unlock+vrele(), but takes less time and avoids releasing and 2128 * re-aquiring the lock (as vrele() aquires the lock internally.) 2129 */ 2130 void 2131 vput(struct vnode *vp) 2132 { 2133 struct thread *td = curthread; /* XXX */ 2134 int error; 2135 2136 KASSERT(vp != NULL, ("vput: null vp")); 2137 ASSERT_VOP_LOCKED(vp, "vput"); 2138 VFS_ASSERT_GIANT(vp->v_mount); 2139 VI_LOCK(vp); 2140 /* Skip this v_writecount check if we're going to panic below. */ 2141 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp, 2142 ("vput: missed vn_close")); 2143 error = 0; 2144 2145 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) && 2146 vp->v_usecount == 1)) { 2147 VOP_UNLOCK(vp, 0, td); 2148 v_decr_usecount(vp); 2149 return; 2150 } 2151 2152 if (vp->v_usecount != 1) { 2153 #ifdef DIAGNOSTIC 2154 vprint("vput: negative ref count", vp); 2155 #endif 2156 panic("vput: negative ref cnt"); 2157 } 2158 /* 2159 * We want to hold the vnode until the inactive finishes to 2160 * prevent vgone() races. We drop the use count here and the 2161 * hold count below when we're done. 2162 */ 2163 v_decr_useonly(vp); 2164 vp->v_iflag |= VI_OWEINACT; 2165 if (VOP_ISLOCKED(vp, NULL) != LK_EXCLUSIVE) { 2166 error = VOP_LOCK(vp, LK_EXCLUPGRADE|LK_INTERLOCK|LK_NOWAIT, td); 2167 VI_LOCK(vp); 2168 if (error) { 2169 if (vp->v_usecount > 0) 2170 vp->v_iflag &= ~VI_OWEINACT; 2171 goto done; 2172 } 2173 } 2174 if (vp->v_usecount > 0) 2175 vp->v_iflag &= ~VI_OWEINACT; 2176 if (vp->v_iflag & VI_OWEINACT) 2177 vinactive(vp, td); 2178 VOP_UNLOCK(vp, 0, td); 2179 done: 2180 vdropl(vp); 2181 } 2182 2183 /* 2184 * Somebody doesn't want the vnode recycled. 2185 */ 2186 void 2187 vhold(struct vnode *vp) 2188 { 2189 2190 VI_LOCK(vp); 2191 vholdl(vp); 2192 VI_UNLOCK(vp); 2193 } 2194 2195 void 2196 vholdl(struct vnode *vp) 2197 { 2198 2199 vp->v_holdcnt++; 2200 if (VSHOULDBUSY(vp)) 2201 vbusy(vp); 2202 } 2203 2204 /* 2205 * Note that there is one less who cares about this vnode. vdrop() is the 2206 * opposite of vhold(). 2207 */ 2208 void 2209 vdrop(struct vnode *vp) 2210 { 2211 2212 VI_LOCK(vp); 2213 vdropl(vp); 2214 } 2215 2216 /* 2217 * Drop the hold count of the vnode. If this is the last reference to 2218 * the vnode we will free it if it has been vgone'd otherwise it is 2219 * placed on the free list. 2220 */ 2221 static void 2222 vdropl(struct vnode *vp) 2223 { 2224 2225 if (vp->v_holdcnt <= 0) 2226 panic("vdrop: holdcnt %d", vp->v_holdcnt); 2227 vp->v_holdcnt--; 2228 if (vp->v_holdcnt == 0) { 2229 if (vp->v_iflag & VI_DOOMED) { 2230 vdestroy(vp); 2231 return; 2232 } else 2233 vfree(vp); 2234 } 2235 VI_UNLOCK(vp); 2236 } 2237 2238 /* 2239 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT 2240 * flags. DOINGINACT prevents us from recursing in calls to vinactive. 2241 * OWEINACT tracks whether a vnode missed a call to inactive due to a 2242 * failed lock upgrade. 2243 */ 2244 static void 2245 vinactive(struct vnode *vp, struct thread *td) 2246 { 2247 2248 ASSERT_VOP_LOCKED(vp, "vinactive"); 2249 ASSERT_VI_LOCKED(vp, "vinactive"); 2250 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp, 2251 ("vinactive: recursed on VI_DOINGINACT")); 2252 vp->v_iflag |= VI_DOINGINACT; 2253 vp->v_iflag &= ~VI_OWEINACT; 2254 VI_UNLOCK(vp); 2255 VOP_INACTIVE(vp, td); 2256 VI_LOCK(vp); 2257 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp, 2258 ("vinactive: lost VI_DOINGINACT")); 2259 vp->v_iflag &= ~VI_DOINGINACT; 2260 } 2261 2262 /* 2263 * Remove any vnodes in the vnode table belonging to mount point mp. 2264 * 2265 * If FORCECLOSE is not specified, there should not be any active ones, 2266 * return error if any are found (nb: this is a user error, not a 2267 * system error). If FORCECLOSE is specified, detach any active vnodes 2268 * that are found. 2269 * 2270 * If WRITECLOSE is set, only flush out regular file vnodes open for 2271 * writing. 2272 * 2273 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2274 * 2275 * `rootrefs' specifies the base reference count for the root vnode 2276 * of this filesystem. The root vnode is considered busy if its 2277 * v_usecount exceeds this value. On a successful return, vflush(, td) 2278 * will call vrele() on the root vnode exactly rootrefs times. 2279 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2280 * be zero. 2281 */ 2282 #ifdef DIAGNOSTIC 2283 static int busyprt = 0; /* print out busy vnodes */ 2284 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2285 #endif 2286 2287 int 2288 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td) 2289 { 2290 struct vnode *vp, *mvp, *rootvp = NULL; 2291 struct vattr vattr; 2292 int busy = 0, error; 2293 2294 CTR1(KTR_VFS, "vflush: mp %p", mp); 2295 if (rootrefs > 0) { 2296 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2297 ("vflush: bad args")); 2298 /* 2299 * Get the filesystem root vnode. We can vput() it 2300 * immediately, since with rootrefs > 0, it won't go away. 2301 */ 2302 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp, td)) != 0) 2303 return (error); 2304 vput(rootvp); 2305 2306 } 2307 MNT_ILOCK(mp); 2308 loop: 2309 MNT_VNODE_FOREACH(vp, mp, mvp) { 2310 2311 VI_LOCK(vp); 2312 vholdl(vp); 2313 MNT_IUNLOCK(mp); 2314 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td); 2315 if (error) { 2316 vdrop(vp); 2317 MNT_ILOCK(mp); 2318 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp); 2319 goto loop; 2320 } 2321 /* 2322 * Skip over a vnodes marked VV_SYSTEM. 2323 */ 2324 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2325 VOP_UNLOCK(vp, 0, td); 2326 vdrop(vp); 2327 MNT_ILOCK(mp); 2328 continue; 2329 } 2330 /* 2331 * If WRITECLOSE is set, flush out unlinked but still open 2332 * files (even if open only for reading) and regular file 2333 * vnodes open for writing. 2334 */ 2335 if (flags & WRITECLOSE) { 2336 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td); 2337 VI_LOCK(vp); 2338 2339 if ((vp->v_type == VNON || 2340 (error == 0 && vattr.va_nlink > 0)) && 2341 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2342 VOP_UNLOCK(vp, 0, td); 2343 vdropl(vp); 2344 MNT_ILOCK(mp); 2345 continue; 2346 } 2347 } else 2348 VI_LOCK(vp); 2349 /* 2350 * With v_usecount == 0, all we need to do is clear out the 2351 * vnode data structures and we are done. 2352 * 2353 * If FORCECLOSE is set, forcibly close the vnode. 2354 */ 2355 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) { 2356 VNASSERT(vp->v_usecount == 0 || 2357 (vp->v_type != VCHR && vp->v_type != VBLK), vp, 2358 ("device VNODE %p is FORCECLOSED", vp)); 2359 vgonel(vp); 2360 } else { 2361 busy++; 2362 #ifdef DIAGNOSTIC 2363 if (busyprt) 2364 vprint("vflush: busy vnode", vp); 2365 #endif 2366 } 2367 VOP_UNLOCK(vp, 0, td); 2368 vdropl(vp); 2369 MNT_ILOCK(mp); 2370 } 2371 MNT_IUNLOCK(mp); 2372 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2373 /* 2374 * If just the root vnode is busy, and if its refcount 2375 * is equal to `rootrefs', then go ahead and kill it. 2376 */ 2377 VI_LOCK(rootvp); 2378 KASSERT(busy > 0, ("vflush: not busy")); 2379 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp, 2380 ("vflush: usecount %d < rootrefs %d", 2381 rootvp->v_usecount, rootrefs)); 2382 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2383 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK, td); 2384 vgone(rootvp); 2385 VOP_UNLOCK(rootvp, 0, td); 2386 busy = 0; 2387 } else 2388 VI_UNLOCK(rootvp); 2389 } 2390 if (busy) 2391 return (EBUSY); 2392 for (; rootrefs > 0; rootrefs--) 2393 vrele(rootvp); 2394 return (0); 2395 } 2396 2397 /* 2398 * Recycle an unused vnode to the front of the free list. 2399 */ 2400 int 2401 vrecycle(struct vnode *vp, struct thread *td) 2402 { 2403 int recycled; 2404 2405 ASSERT_VOP_LOCKED(vp, "vrecycle"); 2406 recycled = 0; 2407 VI_LOCK(vp); 2408 if (vp->v_usecount == 0) { 2409 recycled = 1; 2410 vgonel(vp); 2411 } 2412 VI_UNLOCK(vp); 2413 return (recycled); 2414 } 2415 2416 /* 2417 * Eliminate all activity associated with a vnode 2418 * in preparation for reuse. 2419 */ 2420 void 2421 vgone(struct vnode *vp) 2422 { 2423 VI_LOCK(vp); 2424 vgonel(vp); 2425 VI_UNLOCK(vp); 2426 } 2427 2428 /* 2429 * vgone, with the vp interlock held. 2430 */ 2431 void 2432 vgonel(struct vnode *vp) 2433 { 2434 struct thread *td; 2435 int oweinact; 2436 int active; 2437 struct mount *mp; 2438 2439 CTR1(KTR_VFS, "vgonel: vp %p", vp); 2440 ASSERT_VOP_LOCKED(vp, "vgonel"); 2441 ASSERT_VI_LOCKED(vp, "vgonel"); 2442 VNASSERT(vp->v_holdcnt, vp, 2443 ("vgonel: vp %p has no reference.", vp)); 2444 td = curthread; 2445 2446 /* 2447 * Don't vgonel if we're already doomed. 2448 */ 2449 if (vp->v_iflag & VI_DOOMED) 2450 return; 2451 vp->v_iflag |= VI_DOOMED; 2452 /* 2453 * Check to see if the vnode is in use. If so, we have to call 2454 * VOP_CLOSE() and VOP_INACTIVE(). 2455 */ 2456 active = vp->v_usecount; 2457 oweinact = (vp->v_iflag & VI_OWEINACT); 2458 VI_UNLOCK(vp); 2459 /* 2460 * Clean out any buffers associated with the vnode. 2461 * If the flush fails, just toss the buffers. 2462 */ 2463 mp = NULL; 2464 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd)) 2465 (void) vn_start_secondary_write(vp, &mp, V_WAIT); 2466 if (vinvalbuf(vp, V_SAVE, td, 0, 0) != 0) 2467 vinvalbuf(vp, 0, td, 0, 0); 2468 2469 /* 2470 * If purging an active vnode, it must be closed and 2471 * deactivated before being reclaimed. 2472 */ 2473 if (active) 2474 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2475 if (oweinact || active) { 2476 VI_LOCK(vp); 2477 if ((vp->v_iflag & VI_DOINGINACT) == 0) 2478 vinactive(vp, td); 2479 VI_UNLOCK(vp); 2480 } 2481 /* 2482 * Reclaim the vnode. 2483 */ 2484 if (VOP_RECLAIM(vp, td)) 2485 panic("vgone: cannot reclaim"); 2486 if (mp != NULL) 2487 vn_finished_secondary_write(mp); 2488 VNASSERT(vp->v_object == NULL, vp, 2489 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag)); 2490 /* 2491 * Delete from old mount point vnode list. 2492 */ 2493 delmntque(vp); 2494 cache_purge(vp); 2495 /* 2496 * Done with purge, reset to the standard lock and invalidate 2497 * the vnode. 2498 */ 2499 VI_LOCK(vp); 2500 vp->v_vnlock = &vp->v_lock; 2501 vp->v_op = &dead_vnodeops; 2502 vp->v_tag = "none"; 2503 vp->v_type = VBAD; 2504 } 2505 2506 /* 2507 * Calculate the total number of references to a special device. 2508 */ 2509 int 2510 vcount(struct vnode *vp) 2511 { 2512 int count; 2513 2514 dev_lock(); 2515 count = vp->v_rdev->si_usecount; 2516 dev_unlock(); 2517 return (count); 2518 } 2519 2520 /* 2521 * Same as above, but using the struct cdev *as argument 2522 */ 2523 int 2524 count_dev(struct cdev *dev) 2525 { 2526 int count; 2527 2528 dev_lock(); 2529 count = dev->si_usecount; 2530 dev_unlock(); 2531 return(count); 2532 } 2533 2534 /* 2535 * Print out a description of a vnode. 2536 */ 2537 static char *typename[] = 2538 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD", 2539 "VMARKER"}; 2540 2541 void 2542 vn_printf(struct vnode *vp, const char *fmt, ...) 2543 { 2544 va_list ap; 2545 char buf[96]; 2546 2547 va_start(ap, fmt); 2548 vprintf(fmt, ap); 2549 va_end(ap); 2550 printf("%p: ", (void *)vp); 2551 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]); 2552 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n", 2553 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere); 2554 buf[0] = '\0'; 2555 buf[1] = '\0'; 2556 if (vp->v_vflag & VV_ROOT) 2557 strcat(buf, "|VV_ROOT"); 2558 if (vp->v_vflag & VV_TEXT) 2559 strcat(buf, "|VV_TEXT"); 2560 if (vp->v_vflag & VV_SYSTEM) 2561 strcat(buf, "|VV_SYSTEM"); 2562 if (vp->v_vflag & VV_DELETED) 2563 strcat(buf, "|VV_DELETED"); 2564 if (vp->v_iflag & VI_DOOMED) 2565 strcat(buf, "|VI_DOOMED"); 2566 if (vp->v_iflag & VI_FREE) 2567 strcat(buf, "|VI_FREE"); 2568 printf(" flags (%s)\n", buf + 1); 2569 if (mtx_owned(VI_MTX(vp))) 2570 printf(" VI_LOCKed"); 2571 if (vp->v_object != NULL) 2572 printf(" v_object %p ref %d pages %d\n", 2573 vp->v_object, vp->v_object->ref_count, 2574 vp->v_object->resident_page_count); 2575 printf(" "); 2576 lockmgr_printinfo(vp->v_vnlock); 2577 printf("\n"); 2578 if (vp->v_data != NULL) 2579 VOP_PRINT(vp); 2580 } 2581 2582 #ifdef DDB 2583 /* 2584 * List all of the locked vnodes in the system. 2585 * Called when debugging the kernel. 2586 */ 2587 DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2588 { 2589 struct mount *mp, *nmp; 2590 struct vnode *vp; 2591 2592 /* 2593 * Note: because this is DDB, we can't obey the locking semantics 2594 * for these structures, which means we could catch an inconsistent 2595 * state and dereference a nasty pointer. Not much to be done 2596 * about that. 2597 */ 2598 printf("Locked vnodes\n"); 2599 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2600 nmp = TAILQ_NEXT(mp, mnt_list); 2601 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2602 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp, NULL)) 2603 vprint("", vp); 2604 } 2605 nmp = TAILQ_NEXT(mp, mnt_list); 2606 } 2607 } 2608 2609 /* 2610 * Show details about the given vnode. 2611 */ 2612 DB_SHOW_COMMAND(vnode, db_show_vnode) 2613 { 2614 struct vnode *vp; 2615 2616 if (!have_addr) 2617 return; 2618 vp = (struct vnode *)addr; 2619 vn_printf(vp, "vnode "); 2620 } 2621 #endif /* DDB */ 2622 2623 /* 2624 * Fill in a struct xvfsconf based on a struct vfsconf. 2625 */ 2626 static void 2627 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2628 { 2629 2630 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2631 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2632 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2633 xvfsp->vfc_flags = vfsp->vfc_flags; 2634 /* 2635 * These are unused in userland, we keep them 2636 * to not break binary compatibility. 2637 */ 2638 xvfsp->vfc_vfsops = NULL; 2639 xvfsp->vfc_next = NULL; 2640 } 2641 2642 /* 2643 * Top level filesystem related information gathering. 2644 */ 2645 static int 2646 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2647 { 2648 struct vfsconf *vfsp; 2649 struct xvfsconf xvfsp; 2650 int error; 2651 2652 error = 0; 2653 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 2654 bzero(&xvfsp, sizeof(xvfsp)); 2655 vfsconf2x(vfsp, &xvfsp); 2656 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp); 2657 if (error) 2658 break; 2659 } 2660 return (error); 2661 } 2662 2663 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2664 "S,xvfsconf", "List of all configured filesystems"); 2665 2666 #ifndef BURN_BRIDGES 2667 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2668 2669 static int 2670 vfs_sysctl(SYSCTL_HANDLER_ARGS) 2671 { 2672 int *name = (int *)arg1 - 1; /* XXX */ 2673 u_int namelen = arg2 + 1; /* XXX */ 2674 struct vfsconf *vfsp; 2675 struct xvfsconf xvfsp; 2676 2677 printf("WARNING: userland calling deprecated sysctl, " 2678 "please rebuild world\n"); 2679 2680 #if 1 || defined(COMPAT_PRELITE2) 2681 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2682 if (namelen == 1) 2683 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2684 #endif 2685 2686 switch (name[1]) { 2687 case VFS_MAXTYPENUM: 2688 if (namelen != 2) 2689 return (ENOTDIR); 2690 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2691 case VFS_CONF: 2692 if (namelen != 3) 2693 return (ENOTDIR); /* overloaded */ 2694 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 2695 if (vfsp->vfc_typenum == name[2]) 2696 break; 2697 if (vfsp == NULL) 2698 return (EOPNOTSUPP); 2699 bzero(&xvfsp, sizeof(xvfsp)); 2700 vfsconf2x(vfsp, &xvfsp); 2701 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 2702 } 2703 return (EOPNOTSUPP); 2704 } 2705 2706 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, 2707 vfs_sysctl, "Generic filesystem"); 2708 2709 #if 1 || defined(COMPAT_PRELITE2) 2710 2711 static int 2712 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2713 { 2714 int error; 2715 struct vfsconf *vfsp; 2716 struct ovfsconf ovfs; 2717 2718 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 2719 bzero(&ovfs, sizeof(ovfs)); 2720 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 2721 strcpy(ovfs.vfc_name, vfsp->vfc_name); 2722 ovfs.vfc_index = vfsp->vfc_typenum; 2723 ovfs.vfc_refcount = vfsp->vfc_refcount; 2724 ovfs.vfc_flags = vfsp->vfc_flags; 2725 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 2726 if (error) 2727 return error; 2728 } 2729 return 0; 2730 } 2731 2732 #endif /* 1 || COMPAT_PRELITE2 */ 2733 #endif /* !BURN_BRIDGES */ 2734 2735 #define KINFO_VNODESLOP 10 2736 #ifdef notyet 2737 /* 2738 * Dump vnode list (via sysctl). 2739 */ 2740 /* ARGSUSED */ 2741 static int 2742 sysctl_vnode(SYSCTL_HANDLER_ARGS) 2743 { 2744 struct xvnode *xvn; 2745 struct thread *td = req->td; 2746 struct mount *mp; 2747 struct vnode *vp; 2748 int error, len, n; 2749 2750 /* 2751 * Stale numvnodes access is not fatal here. 2752 */ 2753 req->lock = 0; 2754 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 2755 if (!req->oldptr) 2756 /* Make an estimate */ 2757 return (SYSCTL_OUT(req, 0, len)); 2758 2759 error = sysctl_wire_old_buffer(req, 0); 2760 if (error != 0) 2761 return (error); 2762 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 2763 n = 0; 2764 mtx_lock(&mountlist_mtx); 2765 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2766 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 2767 continue; 2768 MNT_ILOCK(mp); 2769 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2770 if (n == len) 2771 break; 2772 vref(vp); 2773 xvn[n].xv_size = sizeof *xvn; 2774 xvn[n].xv_vnode = vp; 2775 xvn[n].xv_id = 0; /* XXX compat */ 2776 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 2777 XV_COPY(usecount); 2778 XV_COPY(writecount); 2779 XV_COPY(holdcnt); 2780 XV_COPY(mount); 2781 XV_COPY(numoutput); 2782 XV_COPY(type); 2783 #undef XV_COPY 2784 xvn[n].xv_flag = vp->v_vflag; 2785 2786 switch (vp->v_type) { 2787 case VREG: 2788 case VDIR: 2789 case VLNK: 2790 break; 2791 case VBLK: 2792 case VCHR: 2793 if (vp->v_rdev == NULL) { 2794 vrele(vp); 2795 continue; 2796 } 2797 xvn[n].xv_dev = dev2udev(vp->v_rdev); 2798 break; 2799 case VSOCK: 2800 xvn[n].xv_socket = vp->v_socket; 2801 break; 2802 case VFIFO: 2803 xvn[n].xv_fifo = vp->v_fifoinfo; 2804 break; 2805 case VNON: 2806 case VBAD: 2807 default: 2808 /* shouldn't happen? */ 2809 vrele(vp); 2810 continue; 2811 } 2812 vrele(vp); 2813 ++n; 2814 } 2815 MNT_IUNLOCK(mp); 2816 mtx_lock(&mountlist_mtx); 2817 vfs_unbusy(mp, td); 2818 if (n == len) 2819 break; 2820 } 2821 mtx_unlock(&mountlist_mtx); 2822 2823 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 2824 free(xvn, M_TEMP); 2825 return (error); 2826 } 2827 2828 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 2829 0, 0, sysctl_vnode, "S,xvnode", ""); 2830 #endif 2831 2832 /* 2833 * Unmount all filesystems. The list is traversed in reverse order 2834 * of mounting to avoid dependencies. 2835 */ 2836 void 2837 vfs_unmountall(void) 2838 { 2839 struct mount *mp; 2840 struct thread *td; 2841 int error; 2842 2843 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread")); 2844 td = curthread; 2845 /* 2846 * Since this only runs when rebooting, it is not interlocked. 2847 */ 2848 while(!TAILQ_EMPTY(&mountlist)) { 2849 mp = TAILQ_LAST(&mountlist, mntlist); 2850 error = dounmount(mp, MNT_FORCE, td); 2851 if (error) { 2852 TAILQ_REMOVE(&mountlist, mp, mnt_list); 2853 /* 2854 * XXX: Due to the way in which we mount the root 2855 * file system off of devfs, devfs will generate a 2856 * "busy" warning when we try to unmount it before 2857 * the root. Don't print a warning as a result in 2858 * order to avoid false positive errors that may 2859 * cause needless upset. 2860 */ 2861 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) { 2862 printf("unmount of %s failed (", 2863 mp->mnt_stat.f_mntonname); 2864 if (error == EBUSY) 2865 printf("BUSY)\n"); 2866 else 2867 printf("%d)\n", error); 2868 } 2869 } else { 2870 /* The unmount has removed mp from the mountlist */ 2871 } 2872 } 2873 } 2874 2875 /* 2876 * perform msync on all vnodes under a mount point 2877 * the mount point must be locked. 2878 */ 2879 void 2880 vfs_msync(struct mount *mp, int flags) 2881 { 2882 struct vnode *vp, *mvp; 2883 struct vm_object *obj; 2884 2885 MNT_ILOCK(mp); 2886 MNT_VNODE_FOREACH(vp, mp, mvp) { 2887 VI_LOCK(vp); 2888 if ((vp->v_iflag & VI_OBJDIRTY) && 2889 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 2890 MNT_IUNLOCK(mp); 2891 if (!vget(vp, 2892 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 2893 curthread)) { 2894 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 2895 vput(vp); 2896 MNT_ILOCK(mp); 2897 continue; 2898 } 2899 2900 obj = vp->v_object; 2901 if (obj != NULL) { 2902 VM_OBJECT_LOCK(obj); 2903 vm_object_page_clean(obj, 0, 0, 2904 flags == MNT_WAIT ? 2905 OBJPC_SYNC : OBJPC_NOSYNC); 2906 VM_OBJECT_UNLOCK(obj); 2907 } 2908 vput(vp); 2909 } 2910 MNT_ILOCK(mp); 2911 } else 2912 VI_UNLOCK(vp); 2913 } 2914 MNT_IUNLOCK(mp); 2915 } 2916 2917 /* 2918 * Mark a vnode as free, putting it up for recycling. 2919 */ 2920 static void 2921 vfree(struct vnode *vp) 2922 { 2923 2924 CTR1(KTR_VFS, "vfree vp %p", vp); 2925 ASSERT_VI_LOCKED(vp, "vfree"); 2926 mtx_lock(&vnode_free_list_mtx); 2927 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed.")); 2928 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free")); 2929 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't")); 2930 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp, 2931 ("vfree: Freeing doomed vnode")); 2932 if (vp->v_iflag & VI_AGE) { 2933 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2934 } else { 2935 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 2936 } 2937 freevnodes++; 2938 vp->v_iflag &= ~VI_AGE; 2939 vp->v_iflag |= VI_FREE; 2940 mtx_unlock(&vnode_free_list_mtx); 2941 } 2942 2943 /* 2944 * Opposite of vfree() - mark a vnode as in use. 2945 */ 2946 static void 2947 vbusy(struct vnode *vp) 2948 { 2949 CTR1(KTR_VFS, "vbusy vp %p", vp); 2950 ASSERT_VI_LOCKED(vp, "vbusy"); 2951 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free")); 2952 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed.")); 2953 2954 mtx_lock(&vnode_free_list_mtx); 2955 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2956 freevnodes--; 2957 vp->v_iflag &= ~(VI_FREE|VI_AGE); 2958 mtx_unlock(&vnode_free_list_mtx); 2959 } 2960 2961 /* 2962 * Initalize per-vnode helper structure to hold poll-related state. 2963 */ 2964 void 2965 v_addpollinfo(struct vnode *vp) 2966 { 2967 struct vpollinfo *vi; 2968 2969 vi = uma_zalloc(vnodepoll_zone, M_WAITOK); 2970 if (vp->v_pollinfo != NULL) { 2971 uma_zfree(vnodepoll_zone, vi); 2972 return; 2973 } 2974 vp->v_pollinfo = vi; 2975 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF); 2976 knlist_init(&vp->v_pollinfo->vpi_selinfo.si_note, vp, vfs_knllock, 2977 vfs_knlunlock, vfs_knllocked); 2978 } 2979 2980 /* 2981 * Record a process's interest in events which might happen to 2982 * a vnode. Because poll uses the historic select-style interface 2983 * internally, this routine serves as both the ``check for any 2984 * pending events'' and the ``record my interest in future events'' 2985 * functions. (These are done together, while the lock is held, 2986 * to avoid race conditions.) 2987 */ 2988 int 2989 vn_pollrecord(struct vnode *vp, struct thread *td, int events) 2990 { 2991 2992 if (vp->v_pollinfo == NULL) 2993 v_addpollinfo(vp); 2994 mtx_lock(&vp->v_pollinfo->vpi_lock); 2995 if (vp->v_pollinfo->vpi_revents & events) { 2996 /* 2997 * This leaves events we are not interested 2998 * in available for the other process which 2999 * which presumably had requested them 3000 * (otherwise they would never have been 3001 * recorded). 3002 */ 3003 events &= vp->v_pollinfo->vpi_revents; 3004 vp->v_pollinfo->vpi_revents &= ~events; 3005 3006 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3007 return events; 3008 } 3009 vp->v_pollinfo->vpi_events |= events; 3010 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3011 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3012 return 0; 3013 } 3014 3015 /* 3016 * Routine to create and manage a filesystem syncer vnode. 3017 */ 3018 #define sync_close ((int (*)(struct vop_close_args *))nullop) 3019 static int sync_fsync(struct vop_fsync_args *); 3020 static int sync_inactive(struct vop_inactive_args *); 3021 static int sync_reclaim(struct vop_reclaim_args *); 3022 3023 static struct vop_vector sync_vnodeops = { 3024 .vop_bypass = VOP_EOPNOTSUPP, 3025 .vop_close = sync_close, /* close */ 3026 .vop_fsync = sync_fsync, /* fsync */ 3027 .vop_inactive = sync_inactive, /* inactive */ 3028 .vop_reclaim = sync_reclaim, /* reclaim */ 3029 ._vop_lock = vop_stdlock, /* lock */ 3030 .vop_unlock = vop_stdunlock, /* unlock */ 3031 .vop_islocked = vop_stdislocked, /* islocked */ 3032 }; 3033 3034 /* 3035 * Create a new filesystem syncer vnode for the specified mount point. 3036 */ 3037 int 3038 vfs_allocate_syncvnode(struct mount *mp) 3039 { 3040 struct vnode *vp; 3041 static long start, incr, next; 3042 int error; 3043 3044 /* Allocate a new vnode */ 3045 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) { 3046 mp->mnt_syncer = NULL; 3047 return (error); 3048 } 3049 vp->v_type = VNON; 3050 error = insmntque(vp, mp); 3051 if (error != 0) 3052 panic("vfs_allocate_syncvnode: insmntque failed"); 3053 /* 3054 * Place the vnode onto the syncer worklist. We attempt to 3055 * scatter them about on the list so that they will go off 3056 * at evenly distributed times even if all the filesystems 3057 * are mounted at once. 3058 */ 3059 next += incr; 3060 if (next == 0 || next > syncer_maxdelay) { 3061 start /= 2; 3062 incr /= 2; 3063 if (start == 0) { 3064 start = syncer_maxdelay / 2; 3065 incr = syncer_maxdelay; 3066 } 3067 next = start; 3068 } 3069 VI_LOCK(vp); 3070 vn_syncer_add_to_worklist(&vp->v_bufobj, 3071 syncdelay > 0 ? next % syncdelay : 0); 3072 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ 3073 mtx_lock(&sync_mtx); 3074 sync_vnode_count++; 3075 mtx_unlock(&sync_mtx); 3076 VI_UNLOCK(vp); 3077 mp->mnt_syncer = vp; 3078 return (0); 3079 } 3080 3081 /* 3082 * Do a lazy sync of the filesystem. 3083 */ 3084 static int 3085 sync_fsync(struct vop_fsync_args *ap) 3086 { 3087 struct vnode *syncvp = ap->a_vp; 3088 struct mount *mp = syncvp->v_mount; 3089 struct thread *td = ap->a_td; 3090 int error; 3091 struct bufobj *bo; 3092 3093 /* 3094 * We only need to do something if this is a lazy evaluation. 3095 */ 3096 if (ap->a_waitfor != MNT_LAZY) 3097 return (0); 3098 3099 /* 3100 * Move ourselves to the back of the sync list. 3101 */ 3102 bo = &syncvp->v_bufobj; 3103 BO_LOCK(bo); 3104 vn_syncer_add_to_worklist(bo, syncdelay); 3105 BO_UNLOCK(bo); 3106 3107 /* 3108 * Walk the list of vnodes pushing all that are dirty and 3109 * not already on the sync list. 3110 */ 3111 mtx_lock(&mountlist_mtx); 3112 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3113 mtx_unlock(&mountlist_mtx); 3114 return (0); 3115 } 3116 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3117 vfs_unbusy(mp, td); 3118 return (0); 3119 } 3120 MNT_ILOCK(mp); 3121 mp->mnt_noasync++; 3122 mp->mnt_kern_flag &= ~MNTK_ASYNC; 3123 MNT_IUNLOCK(mp); 3124 vfs_msync(mp, MNT_NOWAIT); 3125 error = VFS_SYNC(mp, MNT_LAZY, td); 3126 MNT_ILOCK(mp); 3127 mp->mnt_noasync--; 3128 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 3129 mp->mnt_kern_flag |= MNTK_ASYNC; 3130 MNT_IUNLOCK(mp); 3131 vn_finished_write(mp); 3132 vfs_unbusy(mp, td); 3133 return (error); 3134 } 3135 3136 /* 3137 * The syncer vnode is no referenced. 3138 */ 3139 static int 3140 sync_inactive(struct vop_inactive_args *ap) 3141 { 3142 3143 vgone(ap->a_vp); 3144 return (0); 3145 } 3146 3147 /* 3148 * The syncer vnode is no longer needed and is being decommissioned. 3149 * 3150 * Modifications to the worklist must be protected by sync_mtx. 3151 */ 3152 static int 3153 sync_reclaim(struct vop_reclaim_args *ap) 3154 { 3155 struct vnode *vp = ap->a_vp; 3156 struct bufobj *bo; 3157 3158 VI_LOCK(vp); 3159 bo = &vp->v_bufobj; 3160 vp->v_mount->mnt_syncer = NULL; 3161 if (bo->bo_flag & BO_ONWORKLST) { 3162 mtx_lock(&sync_mtx); 3163 LIST_REMOVE(bo, bo_synclist); 3164 syncer_worklist_len--; 3165 sync_vnode_count--; 3166 mtx_unlock(&sync_mtx); 3167 bo->bo_flag &= ~BO_ONWORKLST; 3168 } 3169 VI_UNLOCK(vp); 3170 3171 return (0); 3172 } 3173 3174 /* 3175 * Check if vnode represents a disk device 3176 */ 3177 int 3178 vn_isdisk(struct vnode *vp, int *errp) 3179 { 3180 int error; 3181 3182 error = 0; 3183 dev_lock(); 3184 if (vp->v_type != VCHR) 3185 error = ENOTBLK; 3186 else if (vp->v_rdev == NULL) 3187 error = ENXIO; 3188 else if (vp->v_rdev->si_devsw == NULL) 3189 error = ENXIO; 3190 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK)) 3191 error = ENOTBLK; 3192 dev_unlock(); 3193 if (errp != NULL) 3194 *errp = error; 3195 return (error == 0); 3196 } 3197 3198 /* 3199 * Common filesystem object access control check routine. Accepts a 3200 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3201 * and optional call-by-reference privused argument allowing vaccess() 3202 * to indicate to the caller whether privilege was used to satisfy the 3203 * request (obsoleted). Returns 0 on success, or an errno on failure. 3204 * 3205 * The ifdef'd CAPABILITIES version is here for reference, but is not 3206 * actually used. 3207 */ 3208 int 3209 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid, 3210 mode_t acc_mode, struct ucred *cred, int *privused) 3211 { 3212 mode_t dac_granted; 3213 mode_t priv_granted; 3214 3215 /* 3216 * Look for a normal, non-privileged way to access the file/directory 3217 * as requested. If it exists, go with that. 3218 */ 3219 3220 if (privused != NULL) 3221 *privused = 0; 3222 3223 dac_granted = 0; 3224 3225 /* Check the owner. */ 3226 if (cred->cr_uid == file_uid) { 3227 dac_granted |= VADMIN; 3228 if (file_mode & S_IXUSR) 3229 dac_granted |= VEXEC; 3230 if (file_mode & S_IRUSR) 3231 dac_granted |= VREAD; 3232 if (file_mode & S_IWUSR) 3233 dac_granted |= (VWRITE | VAPPEND); 3234 3235 if ((acc_mode & dac_granted) == acc_mode) 3236 return (0); 3237 3238 goto privcheck; 3239 } 3240 3241 /* Otherwise, check the groups (first match) */ 3242 if (groupmember(file_gid, cred)) { 3243 if (file_mode & S_IXGRP) 3244 dac_granted |= VEXEC; 3245 if (file_mode & S_IRGRP) 3246 dac_granted |= VREAD; 3247 if (file_mode & S_IWGRP) 3248 dac_granted |= (VWRITE | VAPPEND); 3249 3250 if ((acc_mode & dac_granted) == acc_mode) 3251 return (0); 3252 3253 goto privcheck; 3254 } 3255 3256 /* Otherwise, check everyone else. */ 3257 if (file_mode & S_IXOTH) 3258 dac_granted |= VEXEC; 3259 if (file_mode & S_IROTH) 3260 dac_granted |= VREAD; 3261 if (file_mode & S_IWOTH) 3262 dac_granted |= (VWRITE | VAPPEND); 3263 if ((acc_mode & dac_granted) == acc_mode) 3264 return (0); 3265 3266 privcheck: 3267 /* 3268 * Build a privilege mask to determine if the set of privileges 3269 * satisfies the requirements when combined with the granted mask 3270 * from above. For each privilege, if the privilege is required, 3271 * bitwise or the request type onto the priv_granted mask. 3272 */ 3273 priv_granted = 0; 3274 3275 if (type == VDIR) { 3276 /* 3277 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC 3278 * requests, instead of PRIV_VFS_EXEC. 3279 */ 3280 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3281 !priv_check_cred(cred, PRIV_VFS_LOOKUP, SUSER_ALLOWJAIL)) 3282 priv_granted |= VEXEC; 3283 } else { 3284 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3285 !priv_check_cred(cred, PRIV_VFS_EXEC, SUSER_ALLOWJAIL)) 3286 priv_granted |= VEXEC; 3287 } 3288 3289 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3290 !priv_check_cred(cred, PRIV_VFS_READ, SUSER_ALLOWJAIL)) 3291 priv_granted |= VREAD; 3292 3293 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3294 !priv_check_cred(cred, PRIV_VFS_WRITE, SUSER_ALLOWJAIL)) 3295 priv_granted |= (VWRITE | VAPPEND); 3296 3297 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3298 !priv_check_cred(cred, PRIV_VFS_ADMIN, SUSER_ALLOWJAIL)) 3299 priv_granted |= VADMIN; 3300 3301 if ((acc_mode & (priv_granted | dac_granted)) == acc_mode) { 3302 /* XXX audit: privilege used */ 3303 if (privused != NULL) 3304 *privused = 1; 3305 return (0); 3306 } 3307 3308 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3309 } 3310 3311 /* 3312 * Credential check based on process requesting service, and per-attribute 3313 * permissions. 3314 */ 3315 int 3316 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred, 3317 struct thread *td, int access) 3318 { 3319 3320 /* 3321 * Kernel-invoked always succeeds. 3322 */ 3323 if (cred == NOCRED) 3324 return (0); 3325 3326 /* 3327 * Do not allow privileged processes in jail to directly manipulate 3328 * system attributes. 3329 */ 3330 switch (attrnamespace) { 3331 case EXTATTR_NAMESPACE_SYSTEM: 3332 /* Potentially should be: return (EPERM); */ 3333 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0)); 3334 case EXTATTR_NAMESPACE_USER: 3335 return (VOP_ACCESS(vp, access, cred, td)); 3336 default: 3337 return (EPERM); 3338 } 3339 } 3340 3341 #ifdef DEBUG_VFS_LOCKS 3342 /* 3343 * This only exists to supress warnings from unlocked specfs accesses. It is 3344 * no longer ok to have an unlocked VFS. 3345 */ 3346 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD) 3347 3348 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */ 3349 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, ""); 3350 3351 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */ 3352 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, ""); 3353 3354 int vfs_badlock_print = 1; /* Print lock violations. */ 3355 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, ""); 3356 3357 #ifdef KDB 3358 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */ 3359 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, ""); 3360 #endif 3361 3362 static void 3363 vfs_badlock(const char *msg, const char *str, struct vnode *vp) 3364 { 3365 3366 #ifdef KDB 3367 if (vfs_badlock_backtrace) 3368 kdb_backtrace(); 3369 #endif 3370 if (vfs_badlock_print) 3371 printf("%s: %p %s\n", str, (void *)vp, msg); 3372 if (vfs_badlock_ddb) 3373 kdb_enter("lock violation"); 3374 } 3375 3376 void 3377 assert_vi_locked(struct vnode *vp, const char *str) 3378 { 3379 3380 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp))) 3381 vfs_badlock("interlock is not locked but should be", str, vp); 3382 } 3383 3384 void 3385 assert_vi_unlocked(struct vnode *vp, const char *str) 3386 { 3387 3388 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp))) 3389 vfs_badlock("interlock is locked but should not be", str, vp); 3390 } 3391 3392 void 3393 assert_vop_locked(struct vnode *vp, const char *str) 3394 { 3395 3396 if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0) 3397 vfs_badlock("is not locked but should be", str, vp); 3398 } 3399 3400 void 3401 assert_vop_unlocked(struct vnode *vp, const char *str) 3402 { 3403 3404 if (vp && !IGNORE_LOCK(vp) && 3405 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) 3406 vfs_badlock("is locked but should not be", str, vp); 3407 } 3408 3409 void 3410 assert_vop_elocked(struct vnode *vp, const char *str) 3411 { 3412 3413 if (vp && !IGNORE_LOCK(vp) && 3414 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE) 3415 vfs_badlock("is not exclusive locked but should be", str, vp); 3416 } 3417 3418 #if 0 3419 void 3420 assert_vop_elocked_other(struct vnode *vp, const char *str) 3421 { 3422 3423 if (vp && !IGNORE_LOCK(vp) && 3424 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER) 3425 vfs_badlock("is not exclusive locked by another thread", 3426 str, vp); 3427 } 3428 3429 void 3430 assert_vop_slocked(struct vnode *vp, const char *str) 3431 { 3432 3433 if (vp && !IGNORE_LOCK(vp) && 3434 VOP_ISLOCKED(vp, curthread) != LK_SHARED) 3435 vfs_badlock("is not locked shared but should be", str, vp); 3436 } 3437 #endif /* 0 */ 3438 #endif /* DEBUG_VFS_LOCKS */ 3439 3440 void 3441 vop_rename_pre(void *ap) 3442 { 3443 struct vop_rename_args *a = ap; 3444 3445 #ifdef DEBUG_VFS_LOCKS 3446 if (a->a_tvp) 3447 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME"); 3448 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME"); 3449 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME"); 3450 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME"); 3451 3452 /* Check the source (from). */ 3453 if (a->a_tdvp != a->a_fdvp && a->a_tvp != a->a_fdvp) 3454 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked"); 3455 if (a->a_tvp != a->a_fvp) 3456 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked"); 3457 3458 /* Check the target. */ 3459 if (a->a_tvp) 3460 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked"); 3461 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked"); 3462 #endif 3463 if (a->a_tdvp != a->a_fdvp) 3464 vhold(a->a_fdvp); 3465 if (a->a_tvp != a->a_fvp) 3466 vhold(a->a_fvp); 3467 vhold(a->a_tdvp); 3468 if (a->a_tvp) 3469 vhold(a->a_tvp); 3470 } 3471 3472 void 3473 vop_strategy_pre(void *ap) 3474 { 3475 #ifdef DEBUG_VFS_LOCKS 3476 struct vop_strategy_args *a; 3477 struct buf *bp; 3478 3479 a = ap; 3480 bp = a->a_bp; 3481 3482 /* 3483 * Cluster ops lock their component buffers but not the IO container. 3484 */ 3485 if ((bp->b_flags & B_CLUSTER) != 0) 3486 return; 3487 3488 if (BUF_REFCNT(bp) < 1) { 3489 if (vfs_badlock_print) 3490 printf( 3491 "VOP_STRATEGY: bp is not locked but should be\n"); 3492 if (vfs_badlock_ddb) 3493 kdb_enter("lock violation"); 3494 } 3495 #endif 3496 } 3497 3498 void 3499 vop_lookup_pre(void *ap) 3500 { 3501 #ifdef DEBUG_VFS_LOCKS 3502 struct vop_lookup_args *a; 3503 struct vnode *dvp; 3504 3505 a = ap; 3506 dvp = a->a_dvp; 3507 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3508 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3509 #endif 3510 } 3511 3512 void 3513 vop_lookup_post(void *ap, int rc) 3514 { 3515 #ifdef DEBUG_VFS_LOCKS 3516 struct vop_lookup_args *a; 3517 struct vnode *dvp; 3518 struct vnode *vp; 3519 3520 a = ap; 3521 dvp = a->a_dvp; 3522 vp = *(a->a_vpp); 3523 3524 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP"); 3525 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP"); 3526 3527 if (!rc) 3528 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)"); 3529 #endif 3530 } 3531 3532 void 3533 vop_lock_pre(void *ap) 3534 { 3535 #ifdef DEBUG_VFS_LOCKS 3536 struct _vop_lock_args *a = ap; 3537 3538 if ((a->a_flags & LK_INTERLOCK) == 0) 3539 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3540 else 3541 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK"); 3542 #endif 3543 } 3544 3545 void 3546 vop_lock_post(void *ap, int rc) 3547 { 3548 #ifdef DEBUG_VFS_LOCKS 3549 struct _vop_lock_args *a = ap; 3550 3551 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK"); 3552 if (rc == 0) 3553 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK"); 3554 #endif 3555 } 3556 3557 void 3558 vop_unlock_pre(void *ap) 3559 { 3560 #ifdef DEBUG_VFS_LOCKS 3561 struct vop_unlock_args *a = ap; 3562 3563 if (a->a_flags & LK_INTERLOCK) 3564 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK"); 3565 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK"); 3566 #endif 3567 } 3568 3569 void 3570 vop_unlock_post(void *ap, int rc) 3571 { 3572 #ifdef DEBUG_VFS_LOCKS 3573 struct vop_unlock_args *a = ap; 3574 3575 if (a->a_flags & LK_INTERLOCK) 3576 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK"); 3577 #endif 3578 } 3579 3580 void 3581 vop_create_post(void *ap, int rc) 3582 { 3583 struct vop_create_args *a = ap; 3584 3585 if (!rc) 3586 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3587 } 3588 3589 void 3590 vop_link_post(void *ap, int rc) 3591 { 3592 struct vop_link_args *a = ap; 3593 3594 if (!rc) { 3595 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK); 3596 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE); 3597 } 3598 } 3599 3600 void 3601 vop_mkdir_post(void *ap, int rc) 3602 { 3603 struct vop_mkdir_args *a = ap; 3604 3605 if (!rc) 3606 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 3607 } 3608 3609 void 3610 vop_mknod_post(void *ap, int rc) 3611 { 3612 struct vop_mknod_args *a = ap; 3613 3614 if (!rc) 3615 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3616 } 3617 3618 void 3619 vop_remove_post(void *ap, int rc) 3620 { 3621 struct vop_remove_args *a = ap; 3622 3623 if (!rc) { 3624 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3625 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 3626 } 3627 } 3628 3629 void 3630 vop_rename_post(void *ap, int rc) 3631 { 3632 struct vop_rename_args *a = ap; 3633 3634 if (!rc) { 3635 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE); 3636 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE); 3637 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME); 3638 if (a->a_tvp) 3639 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE); 3640 } 3641 if (a->a_tdvp != a->a_fdvp) 3642 vdrop(a->a_fdvp); 3643 if (a->a_tvp != a->a_fvp) 3644 vdrop(a->a_fvp); 3645 vdrop(a->a_tdvp); 3646 if (a->a_tvp) 3647 vdrop(a->a_tvp); 3648 } 3649 3650 void 3651 vop_rmdir_post(void *ap, int rc) 3652 { 3653 struct vop_rmdir_args *a = ap; 3654 3655 if (!rc) { 3656 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK); 3657 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE); 3658 } 3659 } 3660 3661 void 3662 vop_setattr_post(void *ap, int rc) 3663 { 3664 struct vop_setattr_args *a = ap; 3665 3666 if (!rc) 3667 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB); 3668 } 3669 3670 void 3671 vop_symlink_post(void *ap, int rc) 3672 { 3673 struct vop_symlink_args *a = ap; 3674 3675 if (!rc) 3676 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE); 3677 } 3678 3679 static struct knlist fs_knlist; 3680 3681 static void 3682 vfs_event_init(void *arg) 3683 { 3684 knlist_init(&fs_knlist, NULL, NULL, NULL, NULL); 3685 } 3686 /* XXX - correct order? */ 3687 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL); 3688 3689 void 3690 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused) 3691 { 3692 3693 KNOTE_UNLOCKED(&fs_knlist, event); 3694 } 3695 3696 static int filt_fsattach(struct knote *kn); 3697 static void filt_fsdetach(struct knote *kn); 3698 static int filt_fsevent(struct knote *kn, long hint); 3699 3700 struct filterops fs_filtops = 3701 { 0, filt_fsattach, filt_fsdetach, filt_fsevent }; 3702 3703 static int 3704 filt_fsattach(struct knote *kn) 3705 { 3706 3707 kn->kn_flags |= EV_CLEAR; 3708 knlist_add(&fs_knlist, kn, 0); 3709 return (0); 3710 } 3711 3712 static void 3713 filt_fsdetach(struct knote *kn) 3714 { 3715 3716 knlist_remove(&fs_knlist, kn, 0); 3717 } 3718 3719 static int 3720 filt_fsevent(struct knote *kn, long hint) 3721 { 3722 3723 kn->kn_fflags |= hint; 3724 return (kn->kn_fflags != 0); 3725 } 3726 3727 static int 3728 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS) 3729 { 3730 struct vfsidctl vc; 3731 int error; 3732 struct mount *mp; 3733 3734 error = SYSCTL_IN(req, &vc, sizeof(vc)); 3735 if (error) 3736 return (error); 3737 if (vc.vc_vers != VFS_CTL_VERS1) 3738 return (EINVAL); 3739 mp = vfs_getvfs(&vc.vc_fsid); 3740 if (mp == NULL) 3741 return (ENOENT); 3742 /* ensure that a specific sysctl goes to the right filesystem. */ 3743 if (strcmp(vc.vc_fstypename, "*") != 0 && 3744 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) { 3745 vfs_rel(mp); 3746 return (EINVAL); 3747 } 3748 VCTLTOREQ(&vc, req); 3749 error = VFS_SYSCTL(mp, vc.vc_op, req); 3750 vfs_rel(mp); 3751 return (error); 3752 } 3753 3754 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, 3755 NULL, 0, sysctl_vfs_ctl, "", "Sysctl by fsid"); 3756 3757 /* 3758 * Function to initialize a va_filerev field sensibly. 3759 * XXX: Wouldn't a random number make a lot more sense ?? 3760 */ 3761 u_quad_t 3762 init_va_filerev(void) 3763 { 3764 struct bintime bt; 3765 3766 getbinuptime(&bt); 3767 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL)); 3768 } 3769 3770 static int filt_vfsread(struct knote *kn, long hint); 3771 static int filt_vfswrite(struct knote *kn, long hint); 3772 static int filt_vfsvnode(struct knote *kn, long hint); 3773 static void filt_vfsdetach(struct knote *kn); 3774 static struct filterops vfsread_filtops = 3775 { 1, NULL, filt_vfsdetach, filt_vfsread }; 3776 static struct filterops vfswrite_filtops = 3777 { 1, NULL, filt_vfsdetach, filt_vfswrite }; 3778 static struct filterops vfsvnode_filtops = 3779 { 1, NULL, filt_vfsdetach, filt_vfsvnode }; 3780 3781 static void 3782 vfs_knllock(void *arg) 3783 { 3784 struct vnode *vp = arg; 3785 3786 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread); 3787 } 3788 3789 static void 3790 vfs_knlunlock(void *arg) 3791 { 3792 struct vnode *vp = arg; 3793 3794 VOP_UNLOCK(vp, 0, curthread); 3795 } 3796 3797 static int 3798 vfs_knllocked(void *arg) 3799 { 3800 struct vnode *vp = arg; 3801 3802 return (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE); 3803 } 3804 3805 int 3806 vfs_kqfilter(struct vop_kqfilter_args *ap) 3807 { 3808 struct vnode *vp = ap->a_vp; 3809 struct knote *kn = ap->a_kn; 3810 struct knlist *knl; 3811 3812 switch (kn->kn_filter) { 3813 case EVFILT_READ: 3814 kn->kn_fop = &vfsread_filtops; 3815 break; 3816 case EVFILT_WRITE: 3817 kn->kn_fop = &vfswrite_filtops; 3818 break; 3819 case EVFILT_VNODE: 3820 kn->kn_fop = &vfsvnode_filtops; 3821 break; 3822 default: 3823 return (EINVAL); 3824 } 3825 3826 kn->kn_hook = (caddr_t)vp; 3827 3828 if (vp->v_pollinfo == NULL) 3829 v_addpollinfo(vp); 3830 if (vp->v_pollinfo == NULL) 3831 return (ENOMEM); 3832 knl = &vp->v_pollinfo->vpi_selinfo.si_note; 3833 knlist_add(knl, kn, 0); 3834 3835 return (0); 3836 } 3837 3838 /* 3839 * Detach knote from vnode 3840 */ 3841 static void 3842 filt_vfsdetach(struct knote *kn) 3843 { 3844 struct vnode *vp = (struct vnode *)kn->kn_hook; 3845 3846 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo")); 3847 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0); 3848 } 3849 3850 /*ARGSUSED*/ 3851 static int 3852 filt_vfsread(struct knote *kn, long hint) 3853 { 3854 struct vnode *vp = (struct vnode *)kn->kn_hook; 3855 struct vattr va; 3856 3857 /* 3858 * filesystem is gone, so set the EOF flag and schedule 3859 * the knote for deletion. 3860 */ 3861 if (hint == NOTE_REVOKE) { 3862 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 3863 return (1); 3864 } 3865 3866 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread)) 3867 return (0); 3868 3869 kn->kn_data = va.va_size - kn->kn_fp->f_offset; 3870 return (kn->kn_data != 0); 3871 } 3872 3873 /*ARGSUSED*/ 3874 static int 3875 filt_vfswrite(struct knote *kn, long hint) 3876 { 3877 /* 3878 * filesystem is gone, so set the EOF flag and schedule 3879 * the knote for deletion. 3880 */ 3881 if (hint == NOTE_REVOKE) 3882 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 3883 3884 kn->kn_data = 0; 3885 return (1); 3886 } 3887 3888 static int 3889 filt_vfsvnode(struct knote *kn, long hint) 3890 { 3891 if (kn->kn_sfflags & hint) 3892 kn->kn_fflags |= hint; 3893 if (hint == NOTE_REVOKE) { 3894 kn->kn_flags |= EV_EOF; 3895 return (1); 3896 } 3897 return (kn->kn_fflags != 0); 3898 } 3899 3900 int 3901 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off) 3902 { 3903 int error; 3904 3905 if (dp->d_reclen > ap->a_uio->uio_resid) 3906 return (ENAMETOOLONG); 3907 error = uiomove(dp, dp->d_reclen, ap->a_uio); 3908 if (error) { 3909 if (ap->a_ncookies != NULL) { 3910 if (ap->a_cookies != NULL) 3911 free(ap->a_cookies, M_TEMP); 3912 ap->a_cookies = NULL; 3913 *ap->a_ncookies = 0; 3914 } 3915 return (error); 3916 } 3917 if (ap->a_ncookies == NULL) 3918 return (0); 3919 3920 KASSERT(ap->a_cookies, 3921 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!")); 3922 3923 *ap->a_cookies = realloc(*ap->a_cookies, 3924 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO); 3925 (*ap->a_cookies)[*ap->a_ncookies] = off; 3926 return (0); 3927 } 3928 3929 /* 3930 * Mark for update the access time of the file if the filesystem 3931 * supports VA_MARK_ATIME. This functionality is used by execve 3932 * and mmap, so we want to avoid the synchronous I/O implied by 3933 * directly setting va_atime for the sake of efficiency. 3934 */ 3935 void 3936 vfs_mark_atime(struct vnode *vp, struct thread *td) 3937 { 3938 struct vattr atimeattr; 3939 3940 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) { 3941 VATTR_NULL(&atimeattr); 3942 atimeattr.va_vaflags |= VA_MARK_ATIME; 3943 (void)VOP_SETATTR(vp, &atimeattr, td->td_ucred, td); 3944 } 3945 } 3946