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