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