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