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