vfs_subr.c revision 105077
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 105077 2002-10-14 03:20:36Z mckusick $ 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; 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 tsleep(vnlruproc, PVFS, "vlruwt", 0); 756 continue; 757 } 758 mtx_unlock(&vnode_free_list_mtx); 759 done = 0; 760 mtx_lock(&mountlist_mtx); 761 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 762 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) { 763 nmp = TAILQ_NEXT(mp, mnt_list); 764 continue; 765 } 766 done += vlrureclaim(mp, 10); 767 mtx_lock(&mountlist_mtx); 768 nmp = TAILQ_NEXT(mp, mnt_list); 769 vfs_unbusy(mp, td); 770 } 771 mtx_unlock(&mountlist_mtx); 772 if (done == 0) { 773#if 0 774 /* These messages are temporary debugging aids */ 775 if (vnlru_nowhere < 5) 776 printf("vnlru process getting nowhere..\n"); 777 else if (vnlru_nowhere == 5) 778 printf("vnlru process messages stopped.\n"); 779#endif 780 vnlru_nowhere++; 781 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3); 782 } 783 } 784 splx(s); 785} 786 787static struct kproc_desc vnlru_kp = { 788 "vnlru", 789 vnlru_proc, 790 &vnlruproc 791}; 792SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp) 793 794 795/* 796 * Routines having to do with the management of the vnode table. 797 */ 798 799/* 800 * Check to see if a free vnode can be recycled. If it can, 801 * return it locked with the vn lock, but not interlock. Also 802 * get the vn_start_write lock. Otherwise indicate the error. 803 */ 804static int 805vcanrecycle(struct vnode *vp, struct mount **vnmpp) 806{ 807 struct thread *td = curthread; 808 vm_object_t object; 809 int error; 810 811 /* Don't recycle if we can't get the interlock */ 812 if (!VI_TRYLOCK(vp)) 813 return (EWOULDBLOCK); 814 815 /* We should be able to immediately acquire this */ 816 /* XXX This looks like it should panic if it fails */ 817 if (vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td) != 0) { 818 if (VOP_ISLOCKED(vp, td)) 819 panic("vcanrecycle: locked vnode"); 820 return (EWOULDBLOCK); 821 } 822 823 /* 824 * Don't recycle if its filesystem is being suspended. 825 */ 826 if (vn_start_write(vp, vnmpp, V_NOWAIT) != 0) { 827 error = EBUSY; 828 goto done; 829 } 830 831 /* 832 * Don't recycle if we still have cached pages. 833 */ 834 if (VOP_GETVOBJECT(vp, &object) == 0 && 835 (object->resident_page_count || 836 object->ref_count)) { 837 error = EBUSY; 838 goto done; 839 } 840 if (LIST_FIRST(&vp->v_cache_src)) { 841 /* 842 * note: nameileafonly sysctl is temporary, 843 * for debugging only, and will eventually be 844 * removed. 845 */ 846 if (nameileafonly > 0) { 847 /* 848 * Do not reuse namei-cached directory 849 * vnodes that have cached 850 * subdirectories. 851 */ 852 if (cache_leaf_test(vp) < 0) { 853 error = EISDIR; 854 goto done; 855 } 856 } else if (nameileafonly < 0 || 857 vmiodirenable == 0) { 858 /* 859 * Do not reuse namei-cached directory 860 * vnodes if nameileafonly is -1 or 861 * if VMIO backing for directories is 862 * turned off (otherwise we reuse them 863 * too quickly). 864 */ 865 error = EBUSY; 866 goto done; 867 } 868 } 869 return (0); 870done: 871 VOP_UNLOCK(vp, 0, td); 872 return (error); 873} 874 875/* 876 * Return the next vnode from the free list. 877 */ 878int 879getnewvnode(tag, mp, vops, vpp) 880 const char *tag; 881 struct mount *mp; 882 vop_t **vops; 883 struct vnode **vpp; 884{ 885 int s; 886 struct thread *td = curthread; /* XXX */ 887 struct vnode *vp = NULL; 888 struct mount *vnmp; 889 890 s = splbio(); 891 mtx_lock(&vnode_free_list_mtx); 892 893 /* 894 * Try to reuse vnodes if we hit the max. This situation only 895 * occurs in certain large-memory (2G+) situations. We cannot 896 * attempt to directly reclaim vnodes due to nasty recursion 897 * problems. 898 */ 899 if (vnlruproc_sig == 0 && numvnodes - freevnodes > desiredvnodes) { 900 vnlruproc_sig = 1; /* avoid unnecessary wakeups */ 901 wakeup(vnlruproc); 902 } 903 904 /* 905 * Attempt to reuse a vnode already on the free list, allocating 906 * a new vnode if we can't find one or if we have not reached a 907 * good minimum for good LRU performance. 908 */ 909 910 if (freevnodes >= wantfreevnodes && numvnodes >= minvnodes) { 911 int error; 912 int count; 913 914 for (count = 0; count < freevnodes; count++) { 915 vp = TAILQ_FIRST(&vnode_free_list); 916 917 KASSERT(vp->v_usecount == 0, 918 ("getnewvnode: free vnode isn't")); 919 920 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 921 /* 922 * We have to drop the free list mtx to avoid lock 923 * order reversals with interlock. 924 */ 925 mtx_unlock(&vnode_free_list_mtx); 926 error = vcanrecycle(vp, &vnmp); 927 mtx_lock(&vnode_free_list_mtx); 928 if (error != 0) 929 TAILQ_INSERT_TAIL(&vnode_free_list, vp, 930 v_freelist); 931 else 932 break; 933 } 934 } 935 if (vp) { 936 freevnodes--; 937 mtx_unlock(&vnode_free_list_mtx); 938 939 cache_purge(vp); 940 VI_LOCK(vp); 941 vp->v_iflag |= VI_DOOMED; 942 vp->v_iflag &= ~VI_FREE; 943 if (vp->v_type != VBAD) { 944 VOP_UNLOCK(vp, 0, td); 945 vgonel(vp, td); 946 VI_LOCK(vp); 947 } else { 948 VOP_UNLOCK(vp, 0, td); 949 } 950 vn_finished_write(vnmp); 951 952#ifdef INVARIANTS 953 { 954 if (vp->v_data) 955 panic("cleaned vnode isn't"); 956 if (vp->v_numoutput) 957 panic("Clean vnode has pending I/O's"); 958 if (vp->v_writecount != 0) 959 panic("Non-zero write count"); 960 } 961#endif 962 if (vp->v_pollinfo) { 963 mtx_destroy(&vp->v_pollinfo->vpi_lock); 964 uma_zfree(vnodepoll_zone, vp->v_pollinfo); 965 } 966 vp->v_pollinfo = NULL; 967#ifdef MAC 968 mac_destroy_vnode(vp); 969#endif 970 vp->v_iflag = 0; 971 vp->v_vflag = 0; 972 vp->v_lastw = 0; 973 vp->v_lasta = 0; 974 vp->v_cstart = 0; 975 vp->v_clen = 0; 976 vp->v_socket = 0; 977 lockdestroy(vp->v_vnlock); 978 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE); 979 KASSERT(vp->v_cleanblkroot == NULL, ("cleanblkroot not NULL")); 980 KASSERT(vp->v_dirtyblkroot == NULL, ("dirtyblkroot not NULL")); 981 } else { 982 numvnodes++; 983 mtx_unlock(&vnode_free_list_mtx); 984 985 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO); 986 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF); 987 VI_LOCK(vp); 988 vp->v_dd = vp; 989 vp->v_vnlock = &vp->v_lock; 990 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOPAUSE); 991 cache_purge(vp); 992 LIST_INIT(&vp->v_cache_src); 993 TAILQ_INIT(&vp->v_cache_dst); 994 } 995 996 TAILQ_INIT(&vp->v_cleanblkhd); 997 TAILQ_INIT(&vp->v_dirtyblkhd); 998 vp->v_type = VNON; 999 vp->v_tag = tag; 1000 vp->v_op = vops; 1001 *vpp = vp; 1002 vp->v_usecount = 1; 1003 vp->v_data = 0; 1004 vp->v_cachedid = -1; 1005 VI_UNLOCK(vp); 1006#ifdef MAC 1007 mac_init_vnode(vp); 1008#endif 1009 insmntque(vp, mp); 1010 1011 return (0); 1012} 1013 1014/* 1015 * Move a vnode from one mount queue to another. 1016 */ 1017static void 1018insmntque(vp, mp) 1019 register struct vnode *vp; 1020 register struct mount *mp; 1021{ 1022 1023 mtx_lock(&mntvnode_mtx); 1024 /* 1025 * Delete from old mount point vnode list, if on one. 1026 */ 1027 if (vp->v_mount != NULL) 1028 TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes); 1029 /* 1030 * Insert into list of vnodes for the new mount point, if available. 1031 */ 1032 if ((vp->v_mount = mp) == NULL) { 1033 mtx_unlock(&mntvnode_mtx); 1034 return; 1035 } 1036 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 1037 mtx_unlock(&mntvnode_mtx); 1038} 1039 1040/* 1041 * Update outstanding I/O count and do wakeup if requested. 1042 */ 1043void 1044vwakeup(bp) 1045 register struct buf *bp; 1046{ 1047 register struct vnode *vp; 1048 1049 bp->b_flags &= ~B_WRITEINPROG; 1050 if ((vp = bp->b_vp)) { 1051 VI_LOCK(vp); 1052 vp->v_numoutput--; 1053 if (vp->v_numoutput < 0) 1054 panic("vwakeup: neg numoutput"); 1055 if ((vp->v_numoutput == 0) && (vp->v_iflag & VI_BWAIT)) { 1056 vp->v_iflag &= ~VI_BWAIT; 1057 wakeup(&vp->v_numoutput); 1058 } 1059 VI_UNLOCK(vp); 1060 } 1061} 1062 1063/* 1064 * Flush out and invalidate all buffers associated with a vnode. 1065 * Called with the underlying object locked. 1066 */ 1067int 1068vinvalbuf(vp, flags, cred, td, slpflag, slptimeo) 1069 struct vnode *vp; 1070 int flags; 1071 struct ucred *cred; 1072 struct thread *td; 1073 int slpflag, slptimeo; 1074{ 1075 struct buf *blist; 1076 int s, error; 1077 vm_object_t object; 1078 1079 GIANT_REQUIRED; 1080 1081 ASSERT_VOP_LOCKED(vp, "vinvalbuf"); 1082 1083 VI_LOCK(vp); 1084 if (flags & V_SAVE) { 1085 s = splbio(); 1086 while (vp->v_numoutput) { 1087 vp->v_iflag |= VI_BWAIT; 1088 error = msleep(&vp->v_numoutput, VI_MTX(vp), 1089 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo); 1090 if (error) { 1091 VI_UNLOCK(vp); 1092 splx(s); 1093 return (error); 1094 } 1095 } 1096 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 1097 splx(s); 1098 VI_UNLOCK(vp); 1099 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, td)) != 0) 1100 return (error); 1101 /* 1102 * XXX We could save a lock/unlock if this was only 1103 * enabled under INVARIANTS 1104 */ 1105 VI_LOCK(vp); 1106 s = splbio(); 1107 if (vp->v_numoutput > 0 || 1108 !TAILQ_EMPTY(&vp->v_dirtyblkhd)) 1109 panic("vinvalbuf: dirty bufs"); 1110 } 1111 splx(s); 1112 } 1113 s = splbio(); 1114 /* 1115 * If you alter this loop please notice that interlock is dropped and 1116 * reacquired in flushbuflist. Special care is needed to ensure that 1117 * no race conditions occur from this. 1118 */ 1119 for (error = 0;;) { 1120 if ((blist = TAILQ_FIRST(&vp->v_cleanblkhd)) != 0 && 1121 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 1122 if (error) 1123 break; 1124 continue; 1125 } 1126 if ((blist = TAILQ_FIRST(&vp->v_dirtyblkhd)) != 0 && 1127 flushbuflist(blist, flags, vp, slpflag, slptimeo, &error)) { 1128 if (error) 1129 break; 1130 continue; 1131 } 1132 break; 1133 } 1134 if (error) { 1135 splx(s); 1136 VI_UNLOCK(vp); 1137 return (error); 1138 } 1139 1140 /* 1141 * Wait for I/O to complete. XXX needs cleaning up. The vnode can 1142 * have write I/O in-progress but if there is a VM object then the 1143 * VM object can also have read-I/O in-progress. 1144 */ 1145 do { 1146 while (vp->v_numoutput > 0) { 1147 vp->v_iflag |= VI_BWAIT; 1148 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vnvlbv", 0); 1149 } 1150 VI_UNLOCK(vp); 1151 if (VOP_GETVOBJECT(vp, &object) == 0) { 1152 while (object->paging_in_progress) 1153 vm_object_pip_sleep(object, "vnvlbx"); 1154 } 1155 VI_LOCK(vp); 1156 } while (vp->v_numoutput > 0); 1157 VI_UNLOCK(vp); 1158 1159 splx(s); 1160 1161 /* 1162 * Destroy the copy in the VM cache, too. 1163 */ 1164 if (VOP_GETVOBJECT(vp, &object) == 0) { 1165 vm_object_page_remove(object, 0, 0, 1166 (flags & V_SAVE) ? TRUE : FALSE); 1167 } 1168 1169#ifdef INVARIANTS 1170 VI_LOCK(vp); 1171 if ((flags & (V_ALT | V_NORMAL)) == 0 && 1172 (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || 1173 !TAILQ_EMPTY(&vp->v_cleanblkhd))) 1174 panic("vinvalbuf: flush failed"); 1175 VI_UNLOCK(vp); 1176#endif 1177 return (0); 1178} 1179 1180/* 1181 * Flush out buffers on the specified list. 1182 * 1183 */ 1184static int 1185flushbuflist(blist, flags, vp, slpflag, slptimeo, errorp) 1186 struct buf *blist; 1187 int flags; 1188 struct vnode *vp; 1189 int slpflag, slptimeo; 1190 int *errorp; 1191{ 1192 struct buf *bp, *nbp; 1193 int found, error; 1194 1195 ASSERT_VI_LOCKED(vp, "flushbuflist"); 1196 1197 for (found = 0, bp = blist; bp; bp = nbp) { 1198 nbp = TAILQ_NEXT(bp, b_vnbufs); 1199 VI_UNLOCK(vp); 1200 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) || 1201 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) { 1202 VI_LOCK(vp); 1203 continue; 1204 } 1205 found += 1; 1206 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1207 error = BUF_TIMELOCK(bp, 1208 LK_EXCLUSIVE | LK_SLEEPFAIL, 1209 "flushbuf", slpflag, slptimeo); 1210 if (error != ENOLCK) 1211 *errorp = error; 1212 goto done; 1213 } 1214 /* 1215 * XXX Since there are no node locks for NFS, I 1216 * believe there is a slight chance that a delayed 1217 * write will occur while sleeping just above, so 1218 * check for it. Note that vfs_bio_awrite expects 1219 * buffers to reside on a queue, while BUF_WRITE and 1220 * brelse do not. 1221 */ 1222 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 1223 (flags & V_SAVE)) { 1224 1225 if (bp->b_vp == vp) { 1226 if (bp->b_flags & B_CLUSTEROK) { 1227 BUF_UNLOCK(bp); 1228 vfs_bio_awrite(bp); 1229 } else { 1230 bremfree(bp); 1231 bp->b_flags |= B_ASYNC; 1232 BUF_WRITE(bp); 1233 } 1234 } else { 1235 bremfree(bp); 1236 (void) BUF_WRITE(bp); 1237 } 1238 goto done; 1239 } 1240 bremfree(bp); 1241 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 1242 bp->b_flags &= ~B_ASYNC; 1243 brelse(bp); 1244 VI_LOCK(vp); 1245 } 1246 return (found); 1247done: 1248 VI_LOCK(vp); 1249 return (found); 1250} 1251 1252/* 1253 * Truncate a file's buffer and pages to a specified length. This 1254 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 1255 * sync activity. 1256 */ 1257int 1258vtruncbuf(vp, cred, td, length, blksize) 1259 register struct vnode *vp; 1260 struct ucred *cred; 1261 struct thread *td; 1262 off_t length; 1263 int blksize; 1264{ 1265 register struct buf *bp; 1266 struct buf *nbp; 1267 int s, anyfreed; 1268 int trunclbn; 1269 1270 /* 1271 * Round up to the *next* lbn. 1272 */ 1273 trunclbn = (length + blksize - 1) / blksize; 1274 1275 s = splbio(); 1276 ASSERT_VOP_LOCKED(vp, "vtruncbuf"); 1277restart: 1278 VI_LOCK(vp); 1279 anyfreed = 1; 1280 for (;anyfreed;) { 1281 anyfreed = 0; 1282 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 1283 nbp = TAILQ_NEXT(bp, b_vnbufs); 1284 VI_UNLOCK(vp); 1285 if (bp->b_lblkno >= trunclbn) { 1286 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1287 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 1288 goto restart; 1289 } else { 1290 bremfree(bp); 1291 bp->b_flags |= (B_INVAL | B_RELBUF); 1292 bp->b_flags &= ~B_ASYNC; 1293 brelse(bp); 1294 anyfreed = 1; 1295 } 1296 if (nbp && 1297 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 1298 (nbp->b_vp != vp) || 1299 (nbp->b_flags & B_DELWRI))) { 1300 goto restart; 1301 } 1302 } 1303 VI_LOCK(vp); 1304 } 1305 1306 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1307 nbp = TAILQ_NEXT(bp, b_vnbufs); 1308 VI_UNLOCK(vp); 1309 if (bp->b_lblkno >= trunclbn) { 1310 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1311 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 1312 goto restart; 1313 } else { 1314 bremfree(bp); 1315 bp->b_flags |= (B_INVAL | B_RELBUF); 1316 bp->b_flags &= ~B_ASYNC; 1317 brelse(bp); 1318 anyfreed = 1; 1319 } 1320 if (nbp && 1321 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 1322 (nbp->b_vp != vp) || 1323 (nbp->b_flags & B_DELWRI) == 0)) { 1324 goto restart; 1325 } 1326 } 1327 VI_LOCK(vp); 1328 } 1329 } 1330 1331 if (length > 0) { 1332restartsync: 1333 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 1334 nbp = TAILQ_NEXT(bp, b_vnbufs); 1335 VI_UNLOCK(vp); 1336 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) { 1337 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 1338 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 1339 goto restart; 1340 } else { 1341 bremfree(bp); 1342 if (bp->b_vp == vp) { 1343 bp->b_flags |= B_ASYNC; 1344 } else { 1345 bp->b_flags &= ~B_ASYNC; 1346 } 1347 BUF_WRITE(bp); 1348 } 1349 VI_LOCK(vp); 1350 goto restartsync; 1351 } 1352 VI_LOCK(vp); 1353 } 1354 } 1355 1356 while (vp->v_numoutput > 0) { 1357 vp->v_iflag |= VI_BWAIT; 1358 msleep(&vp->v_numoutput, VI_MTX(vp), PVM, "vbtrunc", 0); 1359 } 1360 VI_UNLOCK(vp); 1361 splx(s); 1362 1363 vnode_pager_setsize(vp, length); 1364 1365 return (0); 1366} 1367 1368/* 1369 * buf_splay() - splay tree core for the clean/dirty list of buffers in 1370 * a vnode. 1371 * 1372 * NOTE: We have to deal with the special case of a background bitmap 1373 * buffer, a situation where two buffers will have the same logical 1374 * block offset. We want (1) only the foreground buffer to be accessed 1375 * in a lookup and (2) must differentiate between the foreground and 1376 * background buffer in the splay tree algorithm because the splay 1377 * tree cannot normally handle multiple entities with the same 'index'. 1378 * We accomplish this by adding differentiating flags to the splay tree's 1379 * numerical domain. 1380 */ 1381static 1382struct buf * 1383buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root) 1384{ 1385 struct buf dummy; 1386 struct buf *lefttreemax, *righttreemin, *y; 1387 1388 if (root == NULL) 1389 return (NULL); 1390 lefttreemax = righttreemin = &dummy; 1391 for (;;) { 1392 if (lblkno < root->b_lblkno || 1393 (lblkno == root->b_lblkno && 1394 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1395 if ((y = root->b_left) == NULL) 1396 break; 1397 if (lblkno < y->b_lblkno) { 1398 /* Rotate right. */ 1399 root->b_left = y->b_right; 1400 y->b_right = root; 1401 root = y; 1402 if ((y = root->b_left) == NULL) 1403 break; 1404 } 1405 /* Link into the new root's right tree. */ 1406 righttreemin->b_left = root; 1407 righttreemin = root; 1408 } else if (lblkno > root->b_lblkno || 1409 (lblkno == root->b_lblkno && 1410 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) { 1411 if ((y = root->b_right) == NULL) 1412 break; 1413 if (lblkno > y->b_lblkno) { 1414 /* Rotate left. */ 1415 root->b_right = y->b_left; 1416 y->b_left = root; 1417 root = y; 1418 if ((y = root->b_right) == NULL) 1419 break; 1420 } 1421 /* Link into the new root's left tree. */ 1422 lefttreemax->b_right = root; 1423 lefttreemax = root; 1424 } else { 1425 break; 1426 } 1427 root = y; 1428 } 1429 /* Assemble the new root. */ 1430 lefttreemax->b_right = root->b_left; 1431 righttreemin->b_left = root->b_right; 1432 root->b_left = dummy.b_right; 1433 root->b_right = dummy.b_left; 1434 return (root); 1435} 1436 1437static 1438void 1439buf_vlist_remove(struct buf *bp) 1440{ 1441 struct vnode *vp = bp->b_vp; 1442 struct buf *root; 1443 1444 ASSERT_VI_LOCKED(vp, "buf_vlist_remove"); 1445 if (bp->b_xflags & BX_VNDIRTY) { 1446 if (bp != vp->v_dirtyblkroot) { 1447 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 1448 KASSERT(root == bp, ("splay lookup failed during dirty remove")); 1449 } 1450 if (bp->b_left == NULL) { 1451 root = bp->b_right; 1452 } else { 1453 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1454 root->b_right = bp->b_right; 1455 } 1456 vp->v_dirtyblkroot = root; 1457 TAILQ_REMOVE(&vp->v_dirtyblkhd, bp, b_vnbufs); 1458 } else { 1459 /* KASSERT(bp->b_xflags & BX_VNCLEAN, ("bp wasn't clean")); */ 1460 if (bp != vp->v_cleanblkroot) { 1461 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 1462 KASSERT(root == bp, ("splay lookup failed during clean remove")); 1463 } 1464 if (bp->b_left == NULL) { 1465 root = bp->b_right; 1466 } else { 1467 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left); 1468 root->b_right = bp->b_right; 1469 } 1470 vp->v_cleanblkroot = root; 1471 TAILQ_REMOVE(&vp->v_cleanblkhd, bp, b_vnbufs); 1472 } 1473 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1474} 1475 1476/* 1477 * Add the buffer to the sorted clean or dirty block list using a 1478 * splay tree algorithm. 1479 * 1480 * NOTE: xflags is passed as a constant, optimizing this inline function! 1481 */ 1482static 1483void 1484buf_vlist_add(struct buf *bp, struct vnode *vp, b_xflags_t xflags) 1485{ 1486 struct buf *root; 1487 1488 ASSERT_VI_LOCKED(vp, "buf_vlist_add"); 1489 bp->b_xflags |= xflags; 1490 if (xflags & BX_VNDIRTY) { 1491 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_dirtyblkroot); 1492 if (root == NULL) { 1493 bp->b_left = NULL; 1494 bp->b_right = NULL; 1495 TAILQ_INSERT_TAIL(&vp->v_dirtyblkhd, bp, b_vnbufs); 1496 } else if (bp->b_lblkno < root->b_lblkno || 1497 (bp->b_lblkno == root->b_lblkno && 1498 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1499 bp->b_left = root->b_left; 1500 bp->b_right = root; 1501 root->b_left = NULL; 1502 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 1503 } else { 1504 bp->b_right = root->b_right; 1505 bp->b_left = root; 1506 root->b_right = NULL; 1507 TAILQ_INSERT_AFTER(&vp->v_dirtyblkhd, 1508 root, bp, b_vnbufs); 1509 } 1510 vp->v_dirtyblkroot = bp; 1511 } else { 1512 /* KASSERT(xflags & BX_VNCLEAN, ("xflags not clean")); */ 1513 root = buf_splay(bp->b_lblkno, bp->b_xflags, vp->v_cleanblkroot); 1514 if (root == NULL) { 1515 bp->b_left = NULL; 1516 bp->b_right = NULL; 1517 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs); 1518 } else if (bp->b_lblkno < root->b_lblkno || 1519 (bp->b_lblkno == root->b_lblkno && 1520 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) { 1521 bp->b_left = root->b_left; 1522 bp->b_right = root; 1523 root->b_left = NULL; 1524 TAILQ_INSERT_BEFORE(root, bp, b_vnbufs); 1525 } else { 1526 bp->b_right = root->b_right; 1527 bp->b_left = root; 1528 root->b_right = NULL; 1529 TAILQ_INSERT_AFTER(&vp->v_cleanblkhd, 1530 root, bp, b_vnbufs); 1531 } 1532 vp->v_cleanblkroot = bp; 1533 } 1534} 1535 1536#ifndef USE_BUFHASH 1537 1538/* 1539 * Lookup a buffer using the splay tree. Note that we specifically avoid 1540 * shadow buffers used in background bitmap writes. 1541 * 1542 * This code isn't quite efficient as it could be because we are maintaining 1543 * two sorted lists and do not know which list the block resides in. 1544 */ 1545struct buf * 1546gbincore(struct vnode *vp, daddr_t lblkno) 1547{ 1548 struct buf *bp; 1549 1550 GIANT_REQUIRED; 1551 1552 ASSERT_VI_LOCKED(vp, "gbincore"); 1553 bp = vp->v_cleanblkroot = buf_splay(lblkno, 0, vp->v_cleanblkroot); 1554 if (bp && bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1555 return(bp); 1556 bp = vp->v_dirtyblkroot = buf_splay(lblkno, 0, vp->v_dirtyblkroot); 1557 if (bp && bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER)) 1558 return(bp); 1559 return(NULL); 1560} 1561 1562#endif 1563 1564/* 1565 * Associate a buffer with a vnode. 1566 */ 1567void 1568bgetvp(vp, bp) 1569 register struct vnode *vp; 1570 register struct buf *bp; 1571{ 1572 int s; 1573 1574 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 1575 1576 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, 1577 ("bgetvp: bp already attached! %p", bp)); 1578 1579 VI_LOCK(vp); 1580 vholdl(vp); 1581 bp->b_vp = vp; 1582 bp->b_dev = vn_todev(vp); 1583 /* 1584 * Insert onto list for new vnode. 1585 */ 1586 s = splbio(); 1587 buf_vlist_add(bp, vp, BX_VNCLEAN); 1588 splx(s); 1589 VI_UNLOCK(vp); 1590} 1591 1592/* 1593 * Disassociate a buffer from a vnode. 1594 */ 1595void 1596brelvp(bp) 1597 register struct buf *bp; 1598{ 1599 struct vnode *vp; 1600 int s; 1601 1602 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 1603 1604 /* 1605 * Delete from old vnode list, if on one. 1606 */ 1607 vp = bp->b_vp; 1608 s = splbio(); 1609 VI_LOCK(vp); 1610 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) 1611 buf_vlist_remove(bp); 1612 if ((vp->v_iflag & VI_ONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 1613 vp->v_iflag &= ~VI_ONWORKLST; 1614 mtx_lock(&sync_mtx); 1615 LIST_REMOVE(vp, v_synclist); 1616 mtx_unlock(&sync_mtx); 1617 } 1618 vdropl(vp); 1619 VI_UNLOCK(vp); 1620 bp->b_vp = (struct vnode *) 0; 1621 if (bp->b_object) 1622 bp->b_object = NULL; 1623 splx(s); 1624} 1625 1626/* 1627 * Add an item to the syncer work queue. 1628 */ 1629static void 1630vn_syncer_add_to_worklist(struct vnode *vp, int delay) 1631{ 1632 int s, slot; 1633 1634 s = splbio(); 1635 ASSERT_VI_LOCKED(vp, "vn_syncer_add_to_worklist"); 1636 1637 mtx_lock(&sync_mtx); 1638 if (vp->v_iflag & VI_ONWORKLST) 1639 LIST_REMOVE(vp, v_synclist); 1640 else 1641 vp->v_iflag |= VI_ONWORKLST; 1642 1643 if (delay > syncer_maxdelay - 2) 1644 delay = syncer_maxdelay - 2; 1645 slot = (syncer_delayno + delay) & syncer_mask; 1646 1647 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 1648 mtx_unlock(&sync_mtx); 1649 1650 splx(s); 1651} 1652 1653struct proc *updateproc; 1654static void sched_sync(void); 1655static struct kproc_desc up_kp = { 1656 "syncer", 1657 sched_sync, 1658 &updateproc 1659}; 1660SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 1661 1662/* 1663 * System filesystem synchronizer daemon. 1664 */ 1665static void 1666sched_sync(void) 1667{ 1668 struct synclist *slp; 1669 struct vnode *vp; 1670 struct mount *mp; 1671 long starttime; 1672 int s; 1673 struct thread *td = FIRST_THREAD_IN_PROC(updateproc); /* XXXKSE */ 1674 1675 mtx_lock(&Giant); 1676 1677 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, td->td_proc, 1678 SHUTDOWN_PRI_LAST); 1679 1680 for (;;) { 1681 kthread_suspend_check(td->td_proc); 1682 1683 starttime = time_second; 1684 1685 /* 1686 * Push files whose dirty time has expired. Be careful 1687 * of interrupt race on slp queue. 1688 */ 1689 s = splbio(); 1690 mtx_lock(&sync_mtx); 1691 slp = &syncer_workitem_pending[syncer_delayno]; 1692 syncer_delayno += 1; 1693 if (syncer_delayno == syncer_maxdelay) 1694 syncer_delayno = 0; 1695 splx(s); 1696 1697 while ((vp = LIST_FIRST(slp)) != NULL) { 1698 mtx_unlock(&sync_mtx); 1699 if (VOP_ISLOCKED(vp, NULL) == 0 && 1700 vn_start_write(vp, &mp, V_NOWAIT) == 0) { 1701 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1702 (void) VOP_FSYNC(vp, td->td_ucred, MNT_LAZY, td); 1703 VOP_UNLOCK(vp, 0, td); 1704 vn_finished_write(mp); 1705 } 1706 s = splbio(); 1707 mtx_lock(&sync_mtx); 1708 if (LIST_FIRST(slp) == vp) { 1709 mtx_unlock(&sync_mtx); 1710 /* 1711 * Note: VFS vnodes can remain on the 1712 * worklist too with no dirty blocks, but 1713 * since sync_fsync() moves it to a different 1714 * slot we are safe. 1715 */ 1716 VI_LOCK(vp); 1717 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) && 1718 !vn_isdisk(vp, NULL)) { 1719 panic("sched_sync: fsync failed " 1720 "vp %p tag %s", vp, vp->v_tag); 1721 } 1722 /* 1723 * Put us back on the worklist. The worklist 1724 * routine will remove us from our current 1725 * position and then add us back in at a later 1726 * position. 1727 */ 1728 vn_syncer_add_to_worklist(vp, syncdelay); 1729 VI_UNLOCK(vp); 1730 mtx_lock(&sync_mtx); 1731 } 1732 splx(s); 1733 } 1734 mtx_unlock(&sync_mtx); 1735 1736 /* 1737 * Do soft update processing. 1738 */ 1739 if (softdep_process_worklist_hook != NULL) 1740 (*softdep_process_worklist_hook)(NULL); 1741 1742 /* 1743 * The variable rushjob allows the kernel to speed up the 1744 * processing of the filesystem syncer process. A rushjob 1745 * value of N tells the filesystem syncer to process the next 1746 * N seconds worth of work on its queue ASAP. Currently rushjob 1747 * is used by the soft update code to speed up the filesystem 1748 * syncer process when the incore state is getting so far 1749 * ahead of the disk that the kernel memory pool is being 1750 * threatened with exhaustion. 1751 */ 1752 mtx_lock(&sync_mtx); 1753 if (rushjob > 0) { 1754 rushjob -= 1; 1755 mtx_unlock(&sync_mtx); 1756 continue; 1757 } 1758 mtx_unlock(&sync_mtx); 1759 /* 1760 * If it has taken us less than a second to process the 1761 * current work, then wait. Otherwise start right over 1762 * again. We can still lose time if any single round 1763 * takes more than two seconds, but it does not really 1764 * matter as we are just trying to generally pace the 1765 * filesystem activity. 1766 */ 1767 if (time_second == starttime) 1768 tsleep(&lbolt, PPAUSE, "syncer", 0); 1769 } 1770} 1771 1772/* 1773 * Request the syncer daemon to speed up its work. 1774 * We never push it to speed up more than half of its 1775 * normal turn time, otherwise it could take over the cpu. 1776 * XXXKSE only one update? 1777 */ 1778int 1779speedup_syncer() 1780{ 1781 struct thread *td; 1782 int ret = 0; 1783 1784 td = FIRST_THREAD_IN_PROC(updateproc); 1785 mtx_lock_spin(&sched_lock); 1786 if (td->td_wchan == &lbolt) { 1787 unsleep(td); 1788 TD_CLR_SLEEPING(td); 1789 setrunnable(td); 1790 } 1791 mtx_unlock_spin(&sched_lock); 1792 mtx_lock(&sync_mtx); 1793 if (rushjob < syncdelay / 2) { 1794 rushjob += 1; 1795 stat_rush_requests += 1; 1796 ret = 1; 1797 } 1798 mtx_unlock(&sync_mtx); 1799 return (ret); 1800} 1801 1802/* 1803 * Associate a p-buffer with a vnode. 1804 * 1805 * Also sets B_PAGING flag to indicate that vnode is not fully associated 1806 * with the buffer. i.e. the bp has not been linked into the vnode or 1807 * ref-counted. 1808 */ 1809void 1810pbgetvp(vp, bp) 1811 register struct vnode *vp; 1812 register struct buf *bp; 1813{ 1814 1815 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 1816 1817 bp->b_vp = vp; 1818 bp->b_flags |= B_PAGING; 1819 bp->b_dev = vn_todev(vp); 1820} 1821 1822/* 1823 * Disassociate a p-buffer from a vnode. 1824 */ 1825void 1826pbrelvp(bp) 1827 register struct buf *bp; 1828{ 1829 1830 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 1831 1832 /* XXX REMOVE ME */ 1833 VI_LOCK(bp->b_vp); 1834 if (TAILQ_NEXT(bp, b_vnbufs) != NULL) { 1835 panic( 1836 "relpbuf(): b_vp was probably reassignbuf()d %p %x", 1837 bp, 1838 (int)bp->b_flags 1839 ); 1840 } 1841 VI_UNLOCK(bp->b_vp); 1842 bp->b_vp = (struct vnode *) 0; 1843 bp->b_flags &= ~B_PAGING; 1844} 1845 1846/* 1847 * Reassign a buffer from one vnode to another. 1848 * Used to assign file specific control information 1849 * (indirect blocks) to the vnode to which they belong. 1850 */ 1851void 1852reassignbuf(bp, newvp) 1853 register struct buf *bp; 1854 register struct vnode *newvp; 1855{ 1856 int delay; 1857 int s; 1858 1859 if (newvp == NULL) { 1860 printf("reassignbuf: NULL"); 1861 return; 1862 } 1863 ++reassignbufcalls; 1864 1865 /* 1866 * B_PAGING flagged buffers cannot be reassigned because their vp 1867 * is not fully linked in. 1868 */ 1869 if (bp->b_flags & B_PAGING) 1870 panic("cannot reassign paging buffer"); 1871 1872 s = splbio(); 1873 /* 1874 * Delete from old vnode list, if on one. 1875 */ 1876 VI_LOCK(bp->b_vp); 1877 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 1878 buf_vlist_remove(bp); 1879 if (bp->b_vp != newvp) { 1880 vdropl(bp->b_vp); 1881 bp->b_vp = NULL; /* for clarification */ 1882 } 1883 } 1884 VI_UNLOCK(bp->b_vp); 1885 /* 1886 * If dirty, put on list of dirty buffers; otherwise insert onto list 1887 * of clean buffers. 1888 */ 1889 VI_LOCK(newvp); 1890 if (bp->b_flags & B_DELWRI) { 1891 if ((newvp->v_iflag & VI_ONWORKLST) == 0) { 1892 switch (newvp->v_type) { 1893 case VDIR: 1894 delay = dirdelay; 1895 break; 1896 case VCHR: 1897 if (newvp->v_rdev->si_mountpoint != NULL) { 1898 delay = metadelay; 1899 break; 1900 } 1901 /* FALLTHROUGH */ 1902 default: 1903 delay = filedelay; 1904 } 1905 vn_syncer_add_to_worklist(newvp, delay); 1906 } 1907 buf_vlist_add(bp, newvp, BX_VNDIRTY); 1908 } else { 1909 buf_vlist_add(bp, newvp, BX_VNCLEAN); 1910 1911 if ((newvp->v_iflag & VI_ONWORKLST) && 1912 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) { 1913 mtx_lock(&sync_mtx); 1914 LIST_REMOVE(newvp, v_synclist); 1915 mtx_unlock(&sync_mtx); 1916 newvp->v_iflag &= ~VI_ONWORKLST; 1917 } 1918 } 1919 if (bp->b_vp != newvp) { 1920 bp->b_vp = newvp; 1921 vholdl(bp->b_vp); 1922 } 1923 VI_UNLOCK(newvp); 1924 splx(s); 1925} 1926 1927/* 1928 * Create a vnode for a device. 1929 * Used for mounting the root filesystem. 1930 */ 1931int 1932bdevvp(dev, vpp) 1933 dev_t dev; 1934 struct vnode **vpp; 1935{ 1936 register struct vnode *vp; 1937 struct vnode *nvp; 1938 int error; 1939 1940 if (dev == NODEV) { 1941 *vpp = NULLVP; 1942 return (ENXIO); 1943 } 1944 if (vfinddev(dev, VCHR, vpp)) 1945 return (0); 1946 error = getnewvnode("none", (struct mount *)0, spec_vnodeop_p, &nvp); 1947 if (error) { 1948 *vpp = NULLVP; 1949 return (error); 1950 } 1951 vp = nvp; 1952 vp->v_type = VCHR; 1953 addalias(vp, dev); 1954 *vpp = vp; 1955 return (0); 1956} 1957 1958/* 1959 * Add vnode to the alias list hung off the dev_t. 1960 * 1961 * The reason for this gunk is that multiple vnodes can reference 1962 * the same physical device, so checking vp->v_usecount to see 1963 * how many users there are is inadequate; the v_usecount for 1964 * the vnodes need to be accumulated. vcount() does that. 1965 */ 1966struct vnode * 1967addaliasu(nvp, nvp_rdev) 1968 struct vnode *nvp; 1969 udev_t nvp_rdev; 1970{ 1971 struct vnode *ovp; 1972 vop_t **ops; 1973 dev_t dev; 1974 1975 if (nvp->v_type == VBLK) 1976 return (nvp); 1977 if (nvp->v_type != VCHR) 1978 panic("addaliasu on non-special vnode"); 1979 dev = udev2dev(nvp_rdev, 0); 1980 /* 1981 * Check to see if we have a bdevvp vnode with no associated 1982 * filesystem. If so, we want to associate the filesystem of 1983 * the new newly instigated vnode with the bdevvp vnode and 1984 * discard the newly created vnode rather than leaving the 1985 * bdevvp vnode lying around with no associated filesystem. 1986 */ 1987 if (vfinddev(dev, nvp->v_type, &ovp) == 0 || ovp->v_data != NULL) { 1988 addalias(nvp, dev); 1989 return (nvp); 1990 } 1991 /* 1992 * Discard unneeded vnode, but save its node specific data. 1993 * Note that if there is a lock, it is carried over in the 1994 * node specific data to the replacement vnode. 1995 */ 1996 vref(ovp); 1997 ovp->v_data = nvp->v_data; 1998 ovp->v_tag = nvp->v_tag; 1999 nvp->v_data = NULL; 2000 lockdestroy(ovp->v_vnlock); 2001 lockinit(ovp->v_vnlock, PVFS, nvp->v_vnlock->lk_wmesg, 2002 nvp->v_vnlock->lk_timo, nvp->v_vnlock->lk_flags & LK_EXTFLG_MASK); 2003 ops = ovp->v_op; 2004 ovp->v_op = nvp->v_op; 2005 if (VOP_ISLOCKED(nvp, curthread)) { 2006 VOP_UNLOCK(nvp, 0, curthread); 2007 vn_lock(ovp, LK_EXCLUSIVE | LK_RETRY, curthread); 2008 } 2009 nvp->v_op = ops; 2010 insmntque(ovp, nvp->v_mount); 2011 vrele(nvp); 2012 vgone(nvp); 2013 return (ovp); 2014} 2015 2016/* This is a local helper function that do the same as addaliasu, but for a 2017 * dev_t instead of an udev_t. */ 2018static void 2019addalias(nvp, dev) 2020 struct vnode *nvp; 2021 dev_t dev; 2022{ 2023 2024 KASSERT(nvp->v_type == VCHR, ("addalias on non-special vnode")); 2025 nvp->v_rdev = dev; 2026 mtx_lock(&spechash_mtx); 2027 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext); 2028 mtx_unlock(&spechash_mtx); 2029} 2030 2031/* 2032 * Grab a particular vnode from the free list, increment its 2033 * reference count and lock it. The vnode lock bit is set if the 2034 * vnode is being eliminated in vgone. The process is awakened 2035 * when the transition is completed, and an error returned to 2036 * indicate that the vnode is no longer usable (possibly having 2037 * been changed to a new filesystem type). 2038 */ 2039int 2040vget(vp, flags, td) 2041 register struct vnode *vp; 2042 int flags; 2043 struct thread *td; 2044{ 2045 int error; 2046 2047 /* 2048 * If the vnode is in the process of being cleaned out for 2049 * another use, we wait for the cleaning to finish and then 2050 * return failure. Cleaning is determined by checking that 2051 * the VI_XLOCK flag is set. 2052 */ 2053 if ((flags & LK_INTERLOCK) == 0) 2054 VI_LOCK(vp); 2055 if (vp->v_iflag & VI_XLOCK && vp->v_vxproc != curthread) { 2056 vp->v_iflag |= VI_XWANT; 2057 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vget", 0); 2058 return (ENOENT); 2059 } 2060 2061 vp->v_usecount++; 2062 2063 if (VSHOULDBUSY(vp)) 2064 vbusy(vp); 2065 if (flags & LK_TYPE_MASK) { 2066 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) { 2067 /* 2068 * must expand vrele here because we do not want 2069 * to call VOP_INACTIVE if the reference count 2070 * drops back to zero since it was never really 2071 * active. We must remove it from the free list 2072 * before sleeping so that multiple processes do 2073 * not try to recycle it. 2074 */ 2075 VI_LOCK(vp); 2076 vp->v_usecount--; 2077 if (VSHOULDFREE(vp)) 2078 vfree(vp); 2079 else 2080 vlruvp(vp); 2081 VI_UNLOCK(vp); 2082 } 2083 return (error); 2084 } 2085 VI_UNLOCK(vp); 2086 return (0); 2087} 2088 2089/* 2090 * Increase the reference count of a vnode. 2091 */ 2092void 2093vref(struct vnode *vp) 2094{ 2095 VI_LOCK(vp); 2096 vp->v_usecount++; 2097 VI_UNLOCK(vp); 2098} 2099 2100/* 2101 * Return reference count of a vnode. 2102 * 2103 * The results of this call are only guaranteed when some mechanism other 2104 * than the VI lock is used to stop other processes from gaining references 2105 * to the vnode. This may be the case if the caller holds the only reference. 2106 * This is also useful when stale data is acceptable as race conditions may 2107 * be accounted for by some other means. 2108 */ 2109int 2110vrefcnt(struct vnode *vp) 2111{ 2112 int usecnt; 2113 2114 VI_LOCK(vp); 2115 usecnt = vp->v_usecount; 2116 VI_UNLOCK(vp); 2117 2118 return (usecnt); 2119} 2120 2121 2122/* 2123 * Vnode put/release. 2124 * If count drops to zero, call inactive routine and return to freelist. 2125 */ 2126void 2127vrele(vp) 2128 struct vnode *vp; 2129{ 2130 struct thread *td = curthread; /* XXX */ 2131 2132 KASSERT(vp != NULL, ("vrele: null vp")); 2133 2134 VI_LOCK(vp); 2135 2136 /* Skip this v_writecount check if we're going to panic below. */ 2137 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2138 ("vrele: missed vn_close")); 2139 2140 if (vp->v_usecount > 1) { 2141 2142 vp->v_usecount--; 2143 VI_UNLOCK(vp); 2144 2145 return; 2146 } 2147 2148 if (vp->v_usecount == 1) { 2149 vp->v_usecount--; 2150 /* 2151 * We must call VOP_INACTIVE with the node locked. 2152 * If we are doing a vput, the node is already locked, 2153 * but, in the case of vrele, we must explicitly lock 2154 * the vnode before calling VOP_INACTIVE. 2155 */ 2156 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) 2157 VOP_INACTIVE(vp, td); 2158 VI_LOCK(vp); 2159 if (VSHOULDFREE(vp)) 2160 vfree(vp); 2161 else 2162 vlruvp(vp); 2163 VI_UNLOCK(vp); 2164 2165 } else { 2166#ifdef DIAGNOSTIC 2167 vprint("vrele: negative ref count", vp); 2168#endif 2169 VI_UNLOCK(vp); 2170 panic("vrele: negative ref cnt"); 2171 } 2172} 2173 2174/* 2175 * Release an already locked vnode. This give the same effects as 2176 * unlock+vrele(), but takes less time and avoids releasing and 2177 * re-aquiring the lock (as vrele() aquires the lock internally.) 2178 */ 2179void 2180vput(vp) 2181 struct vnode *vp; 2182{ 2183 struct thread *td = curthread; /* XXX */ 2184 2185 GIANT_REQUIRED; 2186 2187 KASSERT(vp != NULL, ("vput: null vp")); 2188 VI_LOCK(vp); 2189 /* Skip this v_writecount check if we're going to panic below. */ 2190 KASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, 2191 ("vput: missed vn_close")); 2192 2193 if (vp->v_usecount > 1) { 2194 vp->v_usecount--; 2195 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2196 return; 2197 } 2198 2199 if (vp->v_usecount == 1) { 2200 vp->v_usecount--; 2201 /* 2202 * We must call VOP_INACTIVE with the node locked. 2203 * If we are doing a vput, the node is already locked, 2204 * so we just need to release the vnode mutex. 2205 */ 2206 VI_UNLOCK(vp); 2207 VOP_INACTIVE(vp, td); 2208 VI_LOCK(vp); 2209 if (VSHOULDFREE(vp)) 2210 vfree(vp); 2211 else 2212 vlruvp(vp); 2213 VI_UNLOCK(vp); 2214 2215 } else { 2216#ifdef DIAGNOSTIC 2217 vprint("vput: negative ref count", vp); 2218#endif 2219 panic("vput: negative ref cnt"); 2220 } 2221} 2222 2223/* 2224 * Somebody doesn't want the vnode recycled. 2225 */ 2226void 2227vhold(struct vnode *vp) 2228{ 2229 VI_LOCK(vp); 2230 vholdl(vp); 2231 VI_UNLOCK(vp); 2232} 2233 2234void 2235vholdl(vp) 2236 register struct vnode *vp; 2237{ 2238 int s; 2239 2240 s = splbio(); 2241 vp->v_holdcnt++; 2242 if (VSHOULDBUSY(vp)) 2243 vbusy(vp); 2244 splx(s); 2245} 2246 2247/* 2248 * Note that there is one less who cares about this vnode. vdrop() is the 2249 * opposite of vhold(). 2250 */ 2251void 2252vdrop(struct vnode *vp) 2253{ 2254 VI_LOCK(vp); 2255 vdropl(vp); 2256 VI_UNLOCK(vp); 2257} 2258 2259void 2260vdropl(vp) 2261 register struct vnode *vp; 2262{ 2263 int s; 2264 2265 s = splbio(); 2266 if (vp->v_holdcnt <= 0) 2267 panic("vdrop: holdcnt"); 2268 vp->v_holdcnt--; 2269 if (VSHOULDFREE(vp)) 2270 vfree(vp); 2271 else 2272 vlruvp(vp); 2273 splx(s); 2274} 2275 2276/* 2277 * Remove any vnodes in the vnode table belonging to mount point mp. 2278 * 2279 * If FORCECLOSE is not specified, there should not be any active ones, 2280 * return error if any are found (nb: this is a user error, not a 2281 * system error). If FORCECLOSE is specified, detach any active vnodes 2282 * that are found. 2283 * 2284 * If WRITECLOSE is set, only flush out regular file vnodes open for 2285 * writing. 2286 * 2287 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 2288 * 2289 * `rootrefs' specifies the base reference count for the root vnode 2290 * of this filesystem. The root vnode is considered busy if its 2291 * v_usecount exceeds this value. On a successful return, vflush() 2292 * will call vrele() on the root vnode exactly rootrefs times. 2293 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must 2294 * be zero. 2295 */ 2296#ifdef DIAGNOSTIC 2297static int busyprt = 0; /* print out busy vnodes */ 2298SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 2299#endif 2300 2301int 2302vflush(mp, rootrefs, flags) 2303 struct mount *mp; 2304 int rootrefs; 2305 int flags; 2306{ 2307 struct thread *td = curthread; /* XXX */ 2308 struct vnode *vp, *nvp, *rootvp = NULL; 2309 struct vattr vattr; 2310 int busy = 0, error; 2311 2312 if (rootrefs > 0) { 2313 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0, 2314 ("vflush: bad args")); 2315 /* 2316 * Get the filesystem root vnode. We can vput() it 2317 * immediately, since with rootrefs > 0, it won't go away. 2318 */ 2319 if ((error = VFS_ROOT(mp, &rootvp)) != 0) 2320 return (error); 2321 vput(rootvp); 2322 2323 } 2324 mtx_lock(&mntvnode_mtx); 2325loop: 2326 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) { 2327 /* 2328 * Make sure this vnode wasn't reclaimed in getnewvnode(). 2329 * Start over if it has (it won't be on the list anymore). 2330 */ 2331 if (vp->v_mount != mp) 2332 goto loop; 2333 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 2334 2335 VI_LOCK(vp); 2336 mtx_unlock(&mntvnode_mtx); 2337 vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, td); 2338 /* 2339 * Skip over a vnodes marked VV_SYSTEM. 2340 */ 2341 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 2342 VOP_UNLOCK(vp, 0, td); 2343 mtx_lock(&mntvnode_mtx); 2344 continue; 2345 } 2346 /* 2347 * If WRITECLOSE is set, flush out unlinked but still open 2348 * files (even if open only for reading) and regular file 2349 * vnodes open for writing. 2350 */ 2351 if (flags & WRITECLOSE) { 2352 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td); 2353 VI_LOCK(vp); 2354 2355 if ((vp->v_type == VNON || 2356 (error == 0 && vattr.va_nlink > 0)) && 2357 (vp->v_writecount == 0 || vp->v_type != VREG)) { 2358 VOP_UNLOCK(vp, LK_INTERLOCK, td); 2359 mtx_lock(&mntvnode_mtx); 2360 continue; 2361 } 2362 } else 2363 VI_LOCK(vp); 2364 2365 VOP_UNLOCK(vp, 0, td); 2366 2367 /* 2368 * With v_usecount == 0, all we need to do is clear out the 2369 * vnode data structures and we are done. 2370 */ 2371 if (vp->v_usecount == 0) { 2372 vgonel(vp, td); 2373 mtx_lock(&mntvnode_mtx); 2374 continue; 2375 } 2376 2377 /* 2378 * If FORCECLOSE is set, forcibly close the vnode. For block 2379 * or character devices, revert to an anonymous device. For 2380 * all other files, just kill them. 2381 */ 2382 if (flags & FORCECLOSE) { 2383 if (vp->v_type != VCHR) { 2384 vgonel(vp, td); 2385 } else { 2386 vclean(vp, 0, td); 2387 VI_UNLOCK(vp); 2388 vp->v_op = spec_vnodeop_p; 2389 insmntque(vp, (struct mount *) 0); 2390 } 2391 mtx_lock(&mntvnode_mtx); 2392 continue; 2393 } 2394#ifdef DIAGNOSTIC 2395 if (busyprt) 2396 vprint("vflush: busy vnode", vp); 2397#endif 2398 VI_UNLOCK(vp); 2399 mtx_lock(&mntvnode_mtx); 2400 busy++; 2401 } 2402 mtx_unlock(&mntvnode_mtx); 2403 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) { 2404 /* 2405 * If just the root vnode is busy, and if its refcount 2406 * is equal to `rootrefs', then go ahead and kill it. 2407 */ 2408 VI_LOCK(rootvp); 2409 KASSERT(busy > 0, ("vflush: not busy")); 2410 KASSERT(rootvp->v_usecount >= rootrefs, ("vflush: rootrefs")); 2411 if (busy == 1 && rootvp->v_usecount == rootrefs) { 2412 vgonel(rootvp, td); 2413 busy = 0; 2414 } else 2415 VI_UNLOCK(rootvp); 2416 } 2417 if (busy) 2418 return (EBUSY); 2419 for (; rootrefs > 0; rootrefs--) 2420 vrele(rootvp); 2421 return (0); 2422} 2423 2424/* 2425 * This moves a now (likely recyclable) vnode to the end of the 2426 * mountlist. XXX However, it is temporarily disabled until we 2427 * can clean up ffs_sync() and friends, which have loop restart 2428 * conditions which this code causes to operate O(N^2). 2429 */ 2430static void 2431vlruvp(struct vnode *vp) 2432{ 2433#if 0 2434 struct mount *mp; 2435 2436 if ((mp = vp->v_mount) != NULL) { 2437 mtx_lock(&mntvnode_mtx); 2438 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2439 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes); 2440 mtx_unlock(&mntvnode_mtx); 2441 } 2442#endif 2443} 2444 2445/* 2446 * Disassociate the underlying filesystem from a vnode. 2447 */ 2448static void 2449vclean(vp, flags, td) 2450 struct vnode *vp; 2451 int flags; 2452 struct thread *td; 2453{ 2454 int active; 2455 2456 ASSERT_VI_LOCKED(vp, "vclean"); 2457 /* 2458 * Check to see if the vnode is in use. If so we have to reference it 2459 * before we clean it out so that its count cannot fall to zero and 2460 * generate a race against ourselves to recycle it. 2461 */ 2462 if ((active = vp->v_usecount)) 2463 vp->v_usecount++; 2464 2465 /* 2466 * Prevent the vnode from being recycled or brought into use while we 2467 * clean it out. 2468 */ 2469 if (vp->v_iflag & VI_XLOCK) 2470 panic("vclean: deadlock"); 2471 vp->v_iflag |= VI_XLOCK; 2472 vp->v_vxproc = curthread; 2473 /* 2474 * Even if the count is zero, the VOP_INACTIVE routine may still 2475 * have the object locked while it cleans it out. The VOP_LOCK 2476 * ensures that the VOP_INACTIVE routine is done with its work. 2477 * For active vnodes, it ensures that no other activity can 2478 * occur while the underlying object is being cleaned out. 2479 */ 2480 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, td); 2481 2482 /* 2483 * Clean out any buffers associated with the vnode. 2484 * If the flush fails, just toss the buffers. 2485 */ 2486 if (flags & DOCLOSE) { 2487 struct buf *bp; 2488 VI_LOCK(vp); 2489 bp = TAILQ_FIRST(&vp->v_dirtyblkhd); 2490 VI_UNLOCK(vp); 2491 if (bp != NULL) 2492 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 2493 if (vinvalbuf(vp, V_SAVE, NOCRED, td, 0, 0) != 0) 2494 vinvalbuf(vp, 0, NOCRED, td, 0, 0); 2495 } 2496 2497 VOP_DESTROYVOBJECT(vp); 2498 2499 /* 2500 * Any other processes trying to obtain this lock must first 2501 * wait for VXLOCK to clear, then call the new lock operation. 2502 */ 2503 VOP_UNLOCK(vp, 0, td); 2504 2505 /* 2506 * If purging an active vnode, it must be closed and 2507 * deactivated before being reclaimed. Note that the 2508 * VOP_INACTIVE will unlock the vnode. 2509 */ 2510 if (active) { 2511 if (flags & DOCLOSE) 2512 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td); 2513 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0) 2514 panic("vclean: cannot relock."); 2515 VOP_INACTIVE(vp, td); 2516 } 2517 2518 /* 2519 * Reclaim the vnode. 2520 */ 2521 if (VOP_RECLAIM(vp, td)) 2522 panic("vclean: cannot reclaim"); 2523 2524 if (active) { 2525 /* 2526 * Inline copy of vrele() since VOP_INACTIVE 2527 * has already been called. 2528 */ 2529 VI_LOCK(vp); 2530 if (--vp->v_usecount <= 0) { 2531#ifdef DIAGNOSTIC 2532 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 2533 vprint("vclean: bad ref count", vp); 2534 panic("vclean: ref cnt"); 2535 } 2536#endif 2537 vfree(vp); 2538 } 2539 VI_UNLOCK(vp); 2540 } 2541 2542 cache_purge(vp); 2543 VI_LOCK(vp); 2544 if (VSHOULDFREE(vp)) 2545 vfree(vp); 2546 2547 /* 2548 * Done with purge, notify sleepers of the grim news. 2549 */ 2550 vp->v_op = dead_vnodeop_p; 2551 if (vp->v_pollinfo != NULL) 2552 vn_pollgone(vp); 2553 vp->v_tag = "none"; 2554 vp->v_iflag &= ~VI_XLOCK; 2555 vp->v_vxproc = NULL; 2556 if (vp->v_iflag & VI_XWANT) { 2557 vp->v_iflag &= ~VI_XWANT; 2558 wakeup(vp); 2559 } 2560} 2561 2562/* 2563 * Eliminate all activity associated with the requested vnode 2564 * and with all vnodes aliased to the requested vnode. 2565 */ 2566int 2567vop_revoke(ap) 2568 struct vop_revoke_args /* { 2569 struct vnode *a_vp; 2570 int a_flags; 2571 } */ *ap; 2572{ 2573 struct vnode *vp, *vq; 2574 dev_t dev; 2575 2576 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke")); 2577 2578 vp = ap->a_vp; 2579 VI_LOCK(vp); 2580 /* 2581 * If a vgone (or vclean) is already in progress, 2582 * wait until it is done and return. 2583 */ 2584 if (vp->v_iflag & VI_XLOCK) { 2585 vp->v_iflag |= VI_XWANT; 2586 msleep(vp, VI_MTX(vp), PINOD | PDROP, 2587 "vop_revokeall", 0); 2588 return (0); 2589 } 2590 VI_UNLOCK(vp); 2591 dev = vp->v_rdev; 2592 for (;;) { 2593 mtx_lock(&spechash_mtx); 2594 vq = SLIST_FIRST(&dev->si_hlist); 2595 mtx_unlock(&spechash_mtx); 2596 if (!vq) 2597 break; 2598 vgone(vq); 2599 } 2600 return (0); 2601} 2602 2603/* 2604 * Recycle an unused vnode to the front of the free list. 2605 * Release the passed interlock if the vnode will be recycled. 2606 */ 2607int 2608vrecycle(vp, inter_lkp, td) 2609 struct vnode *vp; 2610 struct mtx *inter_lkp; 2611 struct thread *td; 2612{ 2613 2614 VI_LOCK(vp); 2615 if (vp->v_usecount == 0) { 2616 if (inter_lkp) { 2617 mtx_unlock(inter_lkp); 2618 } 2619 vgonel(vp, td); 2620 return (1); 2621 } 2622 VI_UNLOCK(vp); 2623 return (0); 2624} 2625 2626/* 2627 * Eliminate all activity associated with a vnode 2628 * in preparation for reuse. 2629 */ 2630void 2631vgone(vp) 2632 register struct vnode *vp; 2633{ 2634 struct thread *td = curthread; /* XXX */ 2635 2636 VI_LOCK(vp); 2637 vgonel(vp, td); 2638} 2639 2640/* 2641 * vgone, with the vp interlock held. 2642 */ 2643void 2644vgonel(vp, td) 2645 struct vnode *vp; 2646 struct thread *td; 2647{ 2648 int s; 2649 2650 /* 2651 * If a vgone (or vclean) is already in progress, 2652 * wait until it is done and return. 2653 */ 2654 ASSERT_VI_LOCKED(vp, "vgonel"); 2655 if (vp->v_iflag & VI_XLOCK) { 2656 vp->v_iflag |= VI_XWANT; 2657 msleep(vp, VI_MTX(vp), PINOD | PDROP, "vgone", 0); 2658 return; 2659 } 2660 2661 /* 2662 * Clean out the filesystem specific data. 2663 */ 2664 vclean(vp, DOCLOSE, td); 2665 VI_UNLOCK(vp); 2666 2667 /* 2668 * Delete from old mount point vnode list, if on one. 2669 */ 2670 if (vp->v_mount != NULL) 2671 insmntque(vp, (struct mount *)0); 2672 /* 2673 * If special device, remove it from special device alias list 2674 * if it is on one. 2675 */ 2676 if (vp->v_type == VCHR && vp->v_rdev != NULL && vp->v_rdev != NODEV) { 2677 mtx_lock(&spechash_mtx); 2678 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext); 2679 freedev(vp->v_rdev); 2680 mtx_unlock(&spechash_mtx); 2681 vp->v_rdev = NULL; 2682 } 2683 2684 /* 2685 * If it is on the freelist and not already at the head, 2686 * move it to the head of the list. The test of the 2687 * VDOOMED flag and the reference count of zero is because 2688 * it will be removed from the free list by getnewvnode, 2689 * but will not have its reference count incremented until 2690 * after calling vgone. If the reference count were 2691 * incremented first, vgone would (incorrectly) try to 2692 * close the previous instance of the underlying object. 2693 */ 2694 VI_LOCK(vp); 2695 if (vp->v_usecount == 0 && !(vp->v_iflag & VI_DOOMED)) { 2696 s = splbio(); 2697 mtx_lock(&vnode_free_list_mtx); 2698 if (vp->v_iflag & VI_FREE) { 2699 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2700 } else { 2701 vp->v_iflag |= VI_FREE; 2702 freevnodes++; 2703 } 2704 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2705 mtx_unlock(&vnode_free_list_mtx); 2706 splx(s); 2707 } 2708 2709 vp->v_type = VBAD; 2710 VI_UNLOCK(vp); 2711} 2712 2713/* 2714 * Lookup a vnode by device number. 2715 */ 2716int 2717vfinddev(dev, type, vpp) 2718 dev_t dev; 2719 enum vtype type; 2720 struct vnode **vpp; 2721{ 2722 struct vnode *vp; 2723 2724 mtx_lock(&spechash_mtx); 2725 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 2726 if (type == vp->v_type) { 2727 *vpp = vp; 2728 mtx_unlock(&spechash_mtx); 2729 return (1); 2730 } 2731 } 2732 mtx_unlock(&spechash_mtx); 2733 return (0); 2734} 2735 2736/* 2737 * Calculate the total number of references to a special device. 2738 */ 2739int 2740vcount(vp) 2741 struct vnode *vp; 2742{ 2743 struct vnode *vq; 2744 int count; 2745 2746 count = 0; 2747 mtx_lock(&spechash_mtx); 2748 SLIST_FOREACH(vq, &vp->v_rdev->si_hlist, v_specnext) { 2749 if (vq != vp) 2750 VI_LOCK(vq); 2751 count += vq->v_usecount; 2752 if (vq != vp) 2753 VI_UNLOCK(vq); 2754 } 2755 mtx_unlock(&spechash_mtx); 2756 return (count); 2757} 2758 2759/* 2760 * Same as above, but using the dev_t as argument 2761 */ 2762int 2763count_dev(dev) 2764 dev_t dev; 2765{ 2766 struct vnode *vp; 2767 2768 vp = SLIST_FIRST(&dev->si_hlist); 2769 if (vp == NULL) 2770 return (0); 2771 return(vcount(vp)); 2772} 2773 2774/* 2775 * Print out a description of a vnode. 2776 */ 2777static char *typename[] = 2778{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2779 2780void 2781vprint(label, vp) 2782 char *label; 2783 struct vnode *vp; 2784{ 2785 char buf[96]; 2786 2787 if (label != NULL) 2788 printf("%s: %p: ", label, (void *)vp); 2789 else 2790 printf("%p: ", (void *)vp); 2791 printf("tag %s, type %s, usecount %d, writecount %d, refcount %d,", 2792 vp->v_tag, typename[vp->v_type], vp->v_usecount, 2793 vp->v_writecount, vp->v_holdcnt); 2794 buf[0] = '\0'; 2795 if (vp->v_vflag & VV_ROOT) 2796 strcat(buf, "|VV_ROOT"); 2797 if (vp->v_vflag & VV_TEXT) 2798 strcat(buf, "|VV_TEXT"); 2799 if (vp->v_vflag & VV_SYSTEM) 2800 strcat(buf, "|VV_SYSTEM"); 2801 if (vp->v_iflag & VI_XLOCK) 2802 strcat(buf, "|VI_XLOCK"); 2803 if (vp->v_iflag & VI_XWANT) 2804 strcat(buf, "|VI_XWANT"); 2805 if (vp->v_iflag & VI_BWAIT) 2806 strcat(buf, "|VI_BWAIT"); 2807 if (vp->v_iflag & VI_DOOMED) 2808 strcat(buf, "|VI_DOOMED"); 2809 if (vp->v_iflag & VI_FREE) 2810 strcat(buf, "|VI_FREE"); 2811 if (vp->v_vflag & VV_OBJBUF) 2812 strcat(buf, "|VV_OBJBUF"); 2813 if (buf[0] != '\0') 2814 printf(" flags (%s),", &buf[1]); 2815 lockmgr_printinfo(vp->v_vnlock); 2816 printf("\n"); 2817 if (vp->v_data != NULL) { 2818 printf("\t"); 2819 VOP_PRINT(vp); 2820 } 2821} 2822 2823#ifdef DDB 2824#include <ddb/ddb.h> 2825/* 2826 * List all of the locked vnodes in the system. 2827 * Called when debugging the kernel. 2828 */ 2829DB_SHOW_COMMAND(lockedvnods, lockedvnodes) 2830{ 2831 struct thread *td = curthread; /* XXX */ 2832 struct mount *mp, *nmp; 2833 struct vnode *vp; 2834 2835 printf("Locked vnodes\n"); 2836 mtx_lock(&mountlist_mtx); 2837 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2838 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) { 2839 nmp = TAILQ_NEXT(mp, mnt_list); 2840 continue; 2841 } 2842 mtx_lock(&mntvnode_mtx); 2843 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 2844 if (VOP_ISLOCKED(vp, NULL)) 2845 vprint((char *)0, vp); 2846 } 2847 mtx_unlock(&mntvnode_mtx); 2848 mtx_lock(&mountlist_mtx); 2849 nmp = TAILQ_NEXT(mp, mnt_list); 2850 vfs_unbusy(mp, td); 2851 } 2852 mtx_unlock(&mountlist_mtx); 2853} 2854#endif 2855 2856/* 2857 * Fill in a struct xvfsconf based on a struct vfsconf. 2858 */ 2859static void 2860vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp) 2861{ 2862 2863 strcpy(xvfsp->vfc_name, vfsp->vfc_name); 2864 xvfsp->vfc_typenum = vfsp->vfc_typenum; 2865 xvfsp->vfc_refcount = vfsp->vfc_refcount; 2866 xvfsp->vfc_flags = vfsp->vfc_flags; 2867 /* 2868 * These are unused in userland, we keep them 2869 * to not break binary compatibility. 2870 */ 2871 xvfsp->vfc_vfsops = NULL; 2872 xvfsp->vfc_next = NULL; 2873} 2874 2875static int 2876sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS) 2877{ 2878 struct vfsconf *vfsp; 2879 struct xvfsconf *xvfsp; 2880 int cnt, error, i; 2881 2882 cnt = 0; 2883 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next) 2884 cnt++; 2885 xvfsp = malloc(sizeof(struct xvfsconf) * cnt, M_TEMP, M_WAITOK); 2886 /* 2887 * Handle the race that we will have here when struct vfsconf 2888 * will be locked down by using both cnt and checking vfc_next 2889 * against NULL to determine the end of the loop. The race will 2890 * happen because we will have to unlock before calling malloc(). 2891 * We are protected by Giant for now. 2892 */ 2893 i = 0; 2894 for (vfsp = vfsconf; vfsp != NULL && i < cnt; vfsp = vfsp->vfc_next) { 2895 vfsconf2x(vfsp, xvfsp + i); 2896 i++; 2897 } 2898 error = SYSCTL_OUT(req, xvfsp, sizeof(struct xvfsconf) * i); 2899 free(xvfsp, M_TEMP); 2900 return (error); 2901} 2902 2903SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist, 2904 "S,xvfsconf", "List of all configured filesystems"); 2905 2906/* 2907 * Top level filesystem related information gathering. 2908 */ 2909static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS); 2910 2911static int 2912vfs_sysctl(SYSCTL_HANDLER_ARGS) 2913{ 2914 int *name = (int *)arg1 - 1; /* XXX */ 2915 u_int namelen = arg2 + 1; /* XXX */ 2916 struct vfsconf *vfsp; 2917 struct xvfsconf xvfsp; 2918 2919 printf("WARNING: userland calling deprecated sysctl, " 2920 "please rebuild world\n"); 2921 2922#if 1 || defined(COMPAT_PRELITE2) 2923 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2924 if (namelen == 1) 2925 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2926#endif 2927 2928 switch (name[1]) { 2929 case VFS_MAXTYPENUM: 2930 if (namelen != 2) 2931 return (ENOTDIR); 2932 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2933 case VFS_CONF: 2934 if (namelen != 3) 2935 return (ENOTDIR); /* overloaded */ 2936 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 2937 if (vfsp->vfc_typenum == name[2]) 2938 break; 2939 if (vfsp == NULL) 2940 return (EOPNOTSUPP); 2941 vfsconf2x(vfsp, &xvfsp); 2942 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp))); 2943 } 2944 return (EOPNOTSUPP); 2945} 2946 2947SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP, vfs_sysctl, 2948 "Generic filesystem"); 2949 2950#if 1 || defined(COMPAT_PRELITE2) 2951 2952static int 2953sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2954{ 2955 int error; 2956 struct vfsconf *vfsp; 2957 struct ovfsconf ovfs; 2958 2959 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 2960 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 2961 strcpy(ovfs.vfc_name, vfsp->vfc_name); 2962 ovfs.vfc_index = vfsp->vfc_typenum; 2963 ovfs.vfc_refcount = vfsp->vfc_refcount; 2964 ovfs.vfc_flags = vfsp->vfc_flags; 2965 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 2966 if (error) 2967 return error; 2968 } 2969 return 0; 2970} 2971 2972#endif /* 1 || COMPAT_PRELITE2 */ 2973 2974#define KINFO_VNODESLOP 10 2975/* 2976 * Dump vnode list (via sysctl). 2977 */ 2978/* ARGSUSED */ 2979static int 2980sysctl_vnode(SYSCTL_HANDLER_ARGS) 2981{ 2982 struct xvnode *xvn; 2983 struct thread *td = req->td; 2984 struct mount *mp; 2985 struct vnode *vp; 2986 int error, len, n; 2987 2988 /* 2989 * Stale numvnodes access is not fatal here. 2990 */ 2991 req->lock = 0; 2992 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn; 2993 if (!req->oldptr) 2994 /* Make an estimate */ 2995 return (SYSCTL_OUT(req, 0, len)); 2996 2997 sysctl_wire_old_buffer(req, 0); 2998 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK); 2999 n = 0; 3000 mtx_lock(&mountlist_mtx); 3001 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3002 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) 3003 continue; 3004 mtx_lock(&mntvnode_mtx); 3005 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) { 3006 if (n == len) 3007 break; 3008 vref(vp); 3009 xvn[n].xv_size = sizeof *xvn; 3010 xvn[n].xv_vnode = vp; 3011#define XV_COPY(field) xvn[n].xv_##field = vp->v_##field 3012 XV_COPY(usecount); 3013 XV_COPY(writecount); 3014 XV_COPY(holdcnt); 3015 XV_COPY(id); 3016 XV_COPY(mount); 3017 XV_COPY(numoutput); 3018 XV_COPY(type); 3019#undef XV_COPY 3020 xvn[n].xv_flag = vp->v_vflag; 3021 3022 switch (vp->v_type) { 3023 case VREG: 3024 case VDIR: 3025 case VLNK: 3026 xvn[n].xv_dev = vp->v_cachedfs; 3027 xvn[n].xv_ino = vp->v_cachedid; 3028 break; 3029 case VBLK: 3030 case VCHR: 3031 if (vp->v_rdev == NULL) { 3032 vrele(vp); 3033 continue; 3034 } 3035 xvn[n].xv_dev = dev2udev(vp->v_rdev); 3036 break; 3037 case VSOCK: 3038 xvn[n].xv_socket = vp->v_socket; 3039 break; 3040 case VFIFO: 3041 xvn[n].xv_fifo = vp->v_fifoinfo; 3042 break; 3043 case VNON: 3044 case VBAD: 3045 default: 3046 /* shouldn't happen? */ 3047 vrele(vp); 3048 continue; 3049 } 3050 vrele(vp); 3051 ++n; 3052 } 3053 mtx_unlock(&mntvnode_mtx); 3054 mtx_lock(&mountlist_mtx); 3055 vfs_unbusy(mp, td); 3056 if (n == len) 3057 break; 3058 } 3059 mtx_unlock(&mountlist_mtx); 3060 3061 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn); 3062 free(xvn, M_TEMP); 3063 return (error); 3064} 3065 3066SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 3067 0, 0, sysctl_vnode, "S,xvnode", ""); 3068 3069/* 3070 * Check to see if a filesystem is mounted on a block device. 3071 */ 3072int 3073vfs_mountedon(vp) 3074 struct vnode *vp; 3075{ 3076 3077 if (vp->v_rdev->si_mountpoint != NULL) 3078 return (EBUSY); 3079 return (0); 3080} 3081 3082/* 3083 * Unmount all filesystems. The list is traversed in reverse order 3084 * of mounting to avoid dependencies. 3085 */ 3086void 3087vfs_unmountall() 3088{ 3089 struct mount *mp; 3090 struct thread *td; 3091 int error; 3092 3093 if (curthread != NULL) 3094 td = curthread; 3095 else 3096 td = FIRST_THREAD_IN_PROC(initproc); /* XXX XXX proc0? */ 3097 /* 3098 * Since this only runs when rebooting, it is not interlocked. 3099 */ 3100 while(!TAILQ_EMPTY(&mountlist)) { 3101 mp = TAILQ_LAST(&mountlist, mntlist); 3102 error = dounmount(mp, MNT_FORCE, td); 3103 if (error) { 3104 TAILQ_REMOVE(&mountlist, mp, mnt_list); 3105 printf("unmount of %s failed (", 3106 mp->mnt_stat.f_mntonname); 3107 if (error == EBUSY) 3108 printf("BUSY)\n"); 3109 else 3110 printf("%d)\n", error); 3111 } else { 3112 /* The unmount has removed mp from the mountlist */ 3113 } 3114 } 3115} 3116 3117/* 3118 * perform msync on all vnodes under a mount point 3119 * the mount point must be locked. 3120 */ 3121void 3122vfs_msync(struct mount *mp, int flags) 3123{ 3124 struct vnode *vp, *nvp; 3125 struct vm_object *obj; 3126 int tries; 3127 3128 GIANT_REQUIRED; 3129 3130 tries = 5; 3131 mtx_lock(&mntvnode_mtx); 3132loop: 3133 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) { 3134 if (vp->v_mount != mp) { 3135 if (--tries > 0) 3136 goto loop; 3137 break; 3138 } 3139 nvp = TAILQ_NEXT(vp, v_nmntvnodes); 3140 3141 VI_LOCK(vp); 3142 if (vp->v_iflag & VI_XLOCK) { /* XXX: what if MNT_WAIT? */ 3143 VI_UNLOCK(vp); 3144 continue; 3145 } 3146 3147 if ((vp->v_iflag & VI_OBJDIRTY) && 3148 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) { 3149 mtx_unlock(&mntvnode_mtx); 3150 if (!vget(vp, 3151 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, 3152 curthread)) { 3153 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */ 3154 vput(vp); 3155 mtx_lock(&mntvnode_mtx); 3156 continue; 3157 } 3158 3159 if (VOP_GETVOBJECT(vp, &obj) == 0) { 3160 vm_object_page_clean(obj, 0, 0, 3161 flags == MNT_WAIT ? 3162 OBJPC_SYNC : OBJPC_NOSYNC); 3163 } 3164 vput(vp); 3165 } 3166 mtx_lock(&mntvnode_mtx); 3167 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) { 3168 if (--tries > 0) 3169 goto loop; 3170 break; 3171 } 3172 } else 3173 VI_UNLOCK(vp); 3174 } 3175 mtx_unlock(&mntvnode_mtx); 3176} 3177 3178/* 3179 * Create the VM object needed for VMIO and mmap support. This 3180 * is done for all VREG files in the system. Some filesystems might 3181 * afford the additional metadata buffering capability of the 3182 * VMIO code by making the device node be VMIO mode also. 3183 * 3184 * vp must be locked when vfs_object_create is called. 3185 */ 3186int 3187vfs_object_create(vp, td, cred) 3188 struct vnode *vp; 3189 struct thread *td; 3190 struct ucred *cred; 3191{ 3192 GIANT_REQUIRED; 3193 return (VOP_CREATEVOBJECT(vp, cred, td)); 3194} 3195 3196/* 3197 * Mark a vnode as free, putting it up for recycling. 3198 */ 3199void 3200vfree(vp) 3201 struct vnode *vp; 3202{ 3203 int s; 3204 3205 ASSERT_VI_LOCKED(vp, "vfree"); 3206 s = splbio(); 3207 mtx_lock(&vnode_free_list_mtx); 3208 KASSERT((vp->v_iflag & VI_FREE) == 0, ("vnode already free")); 3209 if (vp->v_iflag & VI_AGE) { 3210 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 3211 } else { 3212 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 3213 } 3214 freevnodes++; 3215 mtx_unlock(&vnode_free_list_mtx); 3216 vp->v_iflag &= ~VI_AGE; 3217 vp->v_iflag |= VI_FREE; 3218 splx(s); 3219} 3220 3221/* 3222 * Opposite of vfree() - mark a vnode as in use. 3223 */ 3224void 3225vbusy(vp) 3226 struct vnode *vp; 3227{ 3228 int s; 3229 3230 s = splbio(); 3231 ASSERT_VI_LOCKED(vp, "vbusy"); 3232 KASSERT((vp->v_iflag & VI_FREE) != 0, ("vnode not free")); 3233 3234 mtx_lock(&vnode_free_list_mtx); 3235 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 3236 freevnodes--; 3237 mtx_unlock(&vnode_free_list_mtx); 3238 3239 vp->v_iflag &= ~(VI_FREE|VI_AGE); 3240 splx(s); 3241} 3242 3243/* 3244 * Record a process's interest in events which might happen to 3245 * a vnode. Because poll uses the historic select-style interface 3246 * internally, this routine serves as both the ``check for any 3247 * pending events'' and the ``record my interest in future events'' 3248 * functions. (These are done together, while the lock is held, 3249 * to avoid race conditions.) 3250 */ 3251int 3252vn_pollrecord(vp, td, events) 3253 struct vnode *vp; 3254 struct thread *td; 3255 short events; 3256{ 3257 3258 if (vp->v_pollinfo == NULL) 3259 v_addpollinfo(vp); 3260 mtx_lock(&vp->v_pollinfo->vpi_lock); 3261 if (vp->v_pollinfo->vpi_revents & events) { 3262 /* 3263 * This leaves events we are not interested 3264 * in available for the other process which 3265 * which presumably had requested them 3266 * (otherwise they would never have been 3267 * recorded). 3268 */ 3269 events &= vp->v_pollinfo->vpi_revents; 3270 vp->v_pollinfo->vpi_revents &= ~events; 3271 3272 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3273 return events; 3274 } 3275 vp->v_pollinfo->vpi_events |= events; 3276 selrecord(td, &vp->v_pollinfo->vpi_selinfo); 3277 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3278 return 0; 3279} 3280 3281/* 3282 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 3283 * it is possible for us to miss an event due to race conditions, but 3284 * that condition is expected to be rare, so for the moment it is the 3285 * preferred interface. 3286 */ 3287void 3288vn_pollevent(vp, events) 3289 struct vnode *vp; 3290 short events; 3291{ 3292 3293 if (vp->v_pollinfo == NULL) 3294 v_addpollinfo(vp); 3295 mtx_lock(&vp->v_pollinfo->vpi_lock); 3296 if (vp->v_pollinfo->vpi_events & events) { 3297 /* 3298 * We clear vpi_events so that we don't 3299 * call selwakeup() twice if two events are 3300 * posted before the polling process(es) is 3301 * awakened. This also ensures that we take at 3302 * most one selwakeup() if the polling process 3303 * is no longer interested. However, it does 3304 * mean that only one event can be noticed at 3305 * a time. (Perhaps we should only clear those 3306 * event bits which we note?) XXX 3307 */ 3308 vp->v_pollinfo->vpi_events = 0; /* &= ~events ??? */ 3309 vp->v_pollinfo->vpi_revents |= events; 3310 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3311 } 3312 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3313} 3314 3315/* 3316 * Wake up anyone polling on vp because it is being revoked. 3317 * This depends on dead_poll() returning POLLHUP for correct 3318 * behavior. 3319 */ 3320void 3321vn_pollgone(vp) 3322 struct vnode *vp; 3323{ 3324 3325 mtx_lock(&vp->v_pollinfo->vpi_lock); 3326 VN_KNOTE(vp, NOTE_REVOKE); 3327 if (vp->v_pollinfo->vpi_events) { 3328 vp->v_pollinfo->vpi_events = 0; 3329 selwakeup(&vp->v_pollinfo->vpi_selinfo); 3330 } 3331 mtx_unlock(&vp->v_pollinfo->vpi_lock); 3332} 3333 3334 3335 3336/* 3337 * Routine to create and manage a filesystem syncer vnode. 3338 */ 3339#define sync_close ((int (*)(struct vop_close_args *))nullop) 3340static int sync_fsync(struct vop_fsync_args *); 3341static int sync_inactive(struct vop_inactive_args *); 3342static int sync_reclaim(struct vop_reclaim_args *); 3343static int sync_print(struct vop_print_args *); 3344 3345static vop_t **sync_vnodeop_p; 3346static struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 3347 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 3348 { &vop_close_desc, (vop_t *) sync_close }, /* close */ 3349 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */ 3350 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */ 3351 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */ 3352 { &vop_lock_desc, (vop_t *) vop_stdlock }, /* lock */ 3353 { &vop_unlock_desc, (vop_t *) vop_stdunlock }, /* unlock */ 3354 { &vop_print_desc, (vop_t *) sync_print }, /* print */ 3355 { &vop_islocked_desc, (vop_t *) vop_stdislocked }, /* islocked */ 3356 { NULL, NULL } 3357}; 3358static struct vnodeopv_desc sync_vnodeop_opv_desc = 3359 { &sync_vnodeop_p, sync_vnodeop_entries }; 3360 3361VNODEOP_SET(sync_vnodeop_opv_desc); 3362 3363/* 3364 * Create a new filesystem syncer vnode for the specified mount point. 3365 */ 3366int 3367vfs_allocate_syncvnode(mp) 3368 struct mount *mp; 3369{ 3370 struct vnode *vp; 3371 static long start, incr, next; 3372 int error; 3373 3374 /* Allocate a new vnode */ 3375 if ((error = getnewvnode("vfs", mp, sync_vnodeop_p, &vp)) != 0) { 3376 mp->mnt_syncer = NULL; 3377 return (error); 3378 } 3379 vp->v_type = VNON; 3380 /* 3381 * Place the vnode onto the syncer worklist. We attempt to 3382 * scatter them about on the list so that they will go off 3383 * at evenly distributed times even if all the filesystems 3384 * are mounted at once. 3385 */ 3386 next += incr; 3387 if (next == 0 || next > syncer_maxdelay) { 3388 start /= 2; 3389 incr /= 2; 3390 if (start == 0) { 3391 start = syncer_maxdelay / 2; 3392 incr = syncer_maxdelay; 3393 } 3394 next = start; 3395 } 3396 VI_LOCK(vp); 3397 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0); 3398 VI_UNLOCK(vp); 3399 mp->mnt_syncer = vp; 3400 return (0); 3401} 3402 3403/* 3404 * Do a lazy sync of the filesystem. 3405 */ 3406static int 3407sync_fsync(ap) 3408 struct vop_fsync_args /* { 3409 struct vnode *a_vp; 3410 struct ucred *a_cred; 3411 int a_waitfor; 3412 struct thread *a_td; 3413 } */ *ap; 3414{ 3415 struct vnode *syncvp = ap->a_vp; 3416 struct mount *mp = syncvp->v_mount; 3417 struct thread *td = ap->a_td; 3418 int asyncflag; 3419 3420 /* 3421 * We only need to do something if this is a lazy evaluation. 3422 */ 3423 if (ap->a_waitfor != MNT_LAZY) 3424 return (0); 3425 3426 /* 3427 * Move ourselves to the back of the sync list. 3428 */ 3429 VI_LOCK(syncvp); 3430 vn_syncer_add_to_worklist(syncvp, syncdelay); 3431 VI_UNLOCK(syncvp); 3432 3433 /* 3434 * Walk the list of vnodes pushing all that are dirty and 3435 * not already on the sync list. 3436 */ 3437 mtx_lock(&mountlist_mtx); 3438 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) { 3439 mtx_unlock(&mountlist_mtx); 3440 return (0); 3441 } 3442 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 3443 vfs_unbusy(mp, td); 3444 return (0); 3445 } 3446 asyncflag = mp->mnt_flag & MNT_ASYNC; 3447 mp->mnt_flag &= ~MNT_ASYNC; 3448 vfs_msync(mp, MNT_NOWAIT); 3449 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, td); 3450 if (asyncflag) 3451 mp->mnt_flag |= MNT_ASYNC; 3452 vn_finished_write(mp); 3453 vfs_unbusy(mp, td); 3454 return (0); 3455} 3456 3457/* 3458 * The syncer vnode is no referenced. 3459 */ 3460static int 3461sync_inactive(ap) 3462 struct vop_inactive_args /* { 3463 struct vnode *a_vp; 3464 struct thread *a_td; 3465 } */ *ap; 3466{ 3467 3468 VOP_UNLOCK(ap->a_vp, 0, ap->a_td); 3469 vgone(ap->a_vp); 3470 return (0); 3471} 3472 3473/* 3474 * The syncer vnode is no longer needed and is being decommissioned. 3475 * 3476 * Modifications to the worklist must be protected at splbio(). 3477 */ 3478static int 3479sync_reclaim(ap) 3480 struct vop_reclaim_args /* { 3481 struct vnode *a_vp; 3482 } */ *ap; 3483{ 3484 struct vnode *vp = ap->a_vp; 3485 int s; 3486 3487 s = splbio(); 3488 vp->v_mount->mnt_syncer = NULL; 3489 VI_LOCK(vp); 3490 if (vp->v_iflag & VI_ONWORKLST) { 3491 mtx_lock(&sync_mtx); 3492 LIST_REMOVE(vp, v_synclist); 3493 mtx_unlock(&sync_mtx); 3494 vp->v_iflag &= ~VI_ONWORKLST; 3495 } 3496 VI_UNLOCK(vp); 3497 splx(s); 3498 3499 return (0); 3500} 3501 3502/* 3503 * Print out a syncer vnode. 3504 */ 3505static int 3506sync_print(ap) 3507 struct vop_print_args /* { 3508 struct vnode *a_vp; 3509 } */ *ap; 3510{ 3511 struct vnode *vp = ap->a_vp; 3512 3513 printf("syncer vnode"); 3514 if (vp->v_vnlock != NULL) 3515 lockmgr_printinfo(vp->v_vnlock); 3516 printf("\n"); 3517 return (0); 3518} 3519 3520/* 3521 * extract the dev_t from a VCHR 3522 */ 3523dev_t 3524vn_todev(vp) 3525 struct vnode *vp; 3526{ 3527 if (vp->v_type != VCHR) 3528 return (NODEV); 3529 return (vp->v_rdev); 3530} 3531 3532/* 3533 * Check if vnode represents a disk device 3534 */ 3535int 3536vn_isdisk(vp, errp) 3537 struct vnode *vp; 3538 int *errp; 3539{ 3540 struct cdevsw *cdevsw; 3541 3542 if (vp->v_type != VCHR) { 3543 if (errp != NULL) 3544 *errp = ENOTBLK; 3545 return (0); 3546 } 3547 if (vp->v_rdev == NULL) { 3548 if (errp != NULL) 3549 *errp = ENXIO; 3550 return (0); 3551 } 3552 cdevsw = devsw(vp->v_rdev); 3553 if (cdevsw == NULL) { 3554 if (errp != NULL) 3555 *errp = ENXIO; 3556 return (0); 3557 } 3558 if (!(cdevsw->d_flags & D_DISK)) { 3559 if (errp != NULL) 3560 *errp = ENOTBLK; 3561 return (0); 3562 } 3563 if (errp != NULL) 3564 *errp = 0; 3565 return (1); 3566} 3567 3568/* 3569 * Free data allocated by namei(); see namei(9) for details. 3570 */ 3571void 3572NDFREE(ndp, flags) 3573 struct nameidata *ndp; 3574 const uint flags; 3575{ 3576 if (!(flags & NDF_NO_FREE_PNBUF) && 3577 (ndp->ni_cnd.cn_flags & HASBUF)) { 3578 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 3579 ndp->ni_cnd.cn_flags &= ~HASBUF; 3580 } 3581 if (!(flags & NDF_NO_DVP_UNLOCK) && 3582 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 3583 ndp->ni_dvp != ndp->ni_vp) 3584 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread); 3585 if (!(flags & NDF_NO_DVP_RELE) && 3586 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3587 vrele(ndp->ni_dvp); 3588 ndp->ni_dvp = NULL; 3589 } 3590 if (!(flags & NDF_NO_VP_UNLOCK) && 3591 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3592 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread); 3593 if (!(flags & NDF_NO_VP_RELE) && 3594 ndp->ni_vp) { 3595 vrele(ndp->ni_vp); 3596 ndp->ni_vp = NULL; 3597 } 3598 if (!(flags & NDF_NO_STARTDIR_RELE) && 3599 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3600 vrele(ndp->ni_startdir); 3601 ndp->ni_startdir = NULL; 3602 } 3603} 3604 3605/* 3606 * Common filesystem object access control check routine. Accepts a 3607 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3608 * and optional call-by-reference privused argument allowing vaccess() 3609 * to indicate to the caller whether privilege was used to satisfy the 3610 * request (obsoleted). Returns 0 on success, or an errno on failure. 3611 */ 3612int 3613vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3614 enum vtype type; 3615 mode_t file_mode; 3616 uid_t file_uid; 3617 gid_t file_gid; 3618 mode_t acc_mode; 3619 struct ucred *cred; 3620 int *privused; 3621{ 3622 mode_t dac_granted; 3623#ifdef CAPABILITIES 3624 mode_t cap_granted; 3625#endif 3626 3627 /* 3628 * Look for a normal, non-privileged way to access the file/directory 3629 * as requested. If it exists, go with that. 3630 */ 3631 3632 if (privused != NULL) 3633 *privused = 0; 3634 3635 dac_granted = 0; 3636 3637 /* Check the owner. */ 3638 if (cred->cr_uid == file_uid) { 3639 dac_granted |= VADMIN; 3640 if (file_mode & S_IXUSR) 3641 dac_granted |= VEXEC; 3642 if (file_mode & S_IRUSR) 3643 dac_granted |= VREAD; 3644 if (file_mode & S_IWUSR) 3645 dac_granted |= (VWRITE | VAPPEND); 3646 3647 if ((acc_mode & dac_granted) == acc_mode) 3648 return (0); 3649 3650 goto privcheck; 3651 } 3652 3653 /* Otherwise, check the groups (first match) */ 3654 if (groupmember(file_gid, cred)) { 3655 if (file_mode & S_IXGRP) 3656 dac_granted |= VEXEC; 3657 if (file_mode & S_IRGRP) 3658 dac_granted |= VREAD; 3659 if (file_mode & S_IWGRP) 3660 dac_granted |= (VWRITE | VAPPEND); 3661 3662 if ((acc_mode & dac_granted) == acc_mode) 3663 return (0); 3664 3665 goto privcheck; 3666 } 3667 3668 /* Otherwise, check everyone else. */ 3669 if (file_mode & S_IXOTH) 3670 dac_granted |= VEXEC; 3671 if (file_mode & S_IROTH) 3672 dac_granted |= VREAD; 3673 if (file_mode & S_IWOTH) 3674 dac_granted |= (VWRITE | VAPPEND); 3675 if ((acc_mode & dac_granted) == acc_mode) 3676 return (0); 3677 3678privcheck: 3679 if (!suser_cred(cred, PRISON_ROOT)) { 3680 /* XXX audit: privilege used */ 3681 if (privused != NULL) 3682 *privused = 1; 3683 return (0); 3684 } 3685 3686#ifdef CAPABILITIES 3687 /* 3688 * Build a capability mask to determine if the set of capabilities 3689 * satisfies the requirements when combined with the granted mask 3690 * from above. 3691 * For each capability, if the capability is required, bitwise 3692 * or the request type onto the cap_granted mask. 3693 */ 3694 cap_granted = 0; 3695 3696 if (type == VDIR) { 3697 /* 3698 * For directories, use CAP_DAC_READ_SEARCH to satisfy 3699 * VEXEC requests, instead of CAP_DAC_EXECUTE. 3700 */ 3701 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3702 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3703 cap_granted |= VEXEC; 3704 } else { 3705 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3706 !cap_check(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT)) 3707 cap_granted |= VEXEC; 3708 } 3709 3710 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3711 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3712 cap_granted |= VREAD; 3713 3714 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3715 !cap_check(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT)) 3716 cap_granted |= (VWRITE | VAPPEND); 3717 3718 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3719 !cap_check(cred, NULL, CAP_FOWNER, PRISON_ROOT)) 3720 cap_granted |= VADMIN; 3721 3722 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3723 /* XXX audit: privilege used */ 3724 if (privused != NULL) 3725 *privused = 1; 3726 return (0); 3727 } 3728#endif 3729 3730 return ((acc_mode & VADMIN) ? EPERM : EACCES); 3731} 3732 3733/* 3734 * Credential check based on process requesting service, and per-attribute 3735 * permissions. 3736 */ 3737int 3738extattr_check_cred(struct vnode *vp, int attrnamespace, 3739 struct ucred *cred, struct thread *td, int access) 3740{ 3741 3742 /* 3743 * Kernel-invoked always succeeds. 3744 */ 3745 if (cred == NOCRED) 3746 return (0); 3747 3748 /* 3749 * Do not allow privileged processes in jail to directly 3750 * manipulate system attributes. 3751 * 3752 * XXX What capability should apply here? 3753 * Probably CAP_SYS_SETFFLAG. 3754 */ 3755 switch (attrnamespace) { 3756 case EXTATTR_NAMESPACE_SYSTEM: 3757 /* Potentially should be: return (EPERM); */ 3758 return (suser_cred(cred, 0)); 3759 case EXTATTR_NAMESPACE_USER: 3760 return (VOP_ACCESS(vp, access, cred, td)); 3761 default: 3762 return (EPERM); 3763 } 3764} 3765