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