The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_subr.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)vfs_subr.c  8.31 (Berkeley) 5/26/95
   35  */
   36 
   37 /*
   38  * External virtual filesystem routines
   39  */
   40 
   41 #include <sys/cdefs.h>
   42 __FBSDID("$FreeBSD: releng/9.1/sys/kern/vfs_subr.c 237839 2012-06-30 14:17:59Z avg $");
   43 
   44 #include "opt_ddb.h"
   45 #include "opt_watchdog.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/bio.h>
   50 #include <sys/buf.h>
   51 #include <sys/condvar.h>
   52 #include <sys/conf.h>
   53 #include <sys/dirent.h>
   54 #include <sys/event.h>
   55 #include <sys/eventhandler.h>
   56 #include <sys/extattr.h>
   57 #include <sys/file.h>
   58 #include <sys/fcntl.h>
   59 #include <sys/jail.h>
   60 #include <sys/kdb.h>
   61 #include <sys/kernel.h>
   62 #include <sys/kthread.h>
   63 #include <sys/lockf.h>
   64 #include <sys/malloc.h>
   65 #include <sys/mount.h>
   66 #include <sys/namei.h>
   67 #include <sys/priv.h>
   68 #include <sys/reboot.h>
   69 #include <sys/sched.h>
   70 #include <sys/sleepqueue.h>
   71 #include <sys/stat.h>
   72 #include <sys/sysctl.h>
   73 #include <sys/syslog.h>
   74 #include <sys/vmmeter.h>
   75 #include <sys/vnode.h>
   76 #include <sys/watchdog.h>
   77 
   78 #include <machine/stdarg.h>
   79 
   80 #include <security/mac/mac_framework.h>
   81 
   82 #include <vm/vm.h>
   83 #include <vm/vm_object.h>
   84 #include <vm/vm_extern.h>
   85 #include <vm/pmap.h>
   86 #include <vm/vm_map.h>
   87 #include <vm/vm_page.h>
   88 #include <vm/vm_kern.h>
   89 #include <vm/uma.h>
   90 
   91 #ifdef DDB
   92 #include <ddb/ddb.h>
   93 #endif
   94 
   95 #define WI_MPSAFEQ      0
   96 #define WI_GIANTQ       1
   97 
   98 static void     delmntque(struct vnode *vp);
   99 static int      flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
  100                     int slpflag, int slptimeo);
  101 static void     syncer_shutdown(void *arg, int howto);
  102 static int      vtryrecycle(struct vnode *vp);
  103 static void     v_incr_usecount(struct vnode *);
  104 static void     v_decr_usecount(struct vnode *);
  105 static void     v_decr_useonly(struct vnode *);
  106 static void     v_upgrade_usecount(struct vnode *);
  107 static void     vnlru_free(int);
  108 static void     vgonel(struct vnode *);
  109 static void     vfs_knllock(void *arg);
  110 static void     vfs_knlunlock(void *arg);
  111 static void     vfs_knl_assert_locked(void *arg);
  112 static void     vfs_knl_assert_unlocked(void *arg);
  113 static void     destroy_vpollinfo(struct vpollinfo *vi);
  114 
  115 /*
  116  * Number of vnodes in existence.  Increased whenever getnewvnode()
  117  * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
  118  */
  119 static unsigned long    numvnodes;
  120 
  121 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
  122     "Number of vnodes in existence");
  123 
  124 /*
  125  * Conversion tables for conversion from vnode types to inode formats
  126  * and back.
  127  */
  128 enum vtype iftovt_tab[16] = {
  129         VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
  130         VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
  131 };
  132 int vttoif_tab[10] = {
  133         0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
  134         S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
  135 };
  136 
  137 /*
  138  * List of vnodes that are ready for recycling.
  139  */
  140 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
  141 
  142 /*
  143  * Free vnode target.  Free vnodes may simply be files which have been stat'd
  144  * but not read.  This is somewhat common, and a small cache of such files
  145  * should be kept to avoid recreation costs.
  146  */
  147 static u_long wantfreevnodes;
  148 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
  149 /* Number of vnodes in the free list. */
  150 static u_long freevnodes;
  151 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
  152     "Number of vnodes in the free list");
  153 
  154 static int vlru_allow_cache_src;
  155 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
  156     &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
  157 
  158 /*
  159  * Various variables used for debugging the new implementation of
  160  * reassignbuf().
  161  * XXX these are probably of (very) limited utility now.
  162  */
  163 static int reassignbufcalls;
  164 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
  165     "Number of calls to reassignbuf");
  166 
  167 /*
  168  * Cache for the mount type id assigned to NFS.  This is used for
  169  * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
  170  */
  171 int     nfs_mount_type = -1;
  172 
  173 /* To keep more than one thread at a time from running vfs_getnewfsid */
  174 static struct mtx mntid_mtx;
  175 
  176 /*
  177  * Lock for any access to the following:
  178  *      vnode_free_list
  179  *      numvnodes
  180  *      freevnodes
  181  */
  182 static struct mtx vnode_free_list_mtx;
  183 
  184 /* Publicly exported FS */
  185 struct nfs_public nfs_pub;
  186 
  187 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
  188 static uma_zone_t vnode_zone;
  189 static uma_zone_t vnodepoll_zone;
  190 
  191 /*
  192  * The workitem queue.
  193  *
  194  * It is useful to delay writes of file data and filesystem metadata
  195  * for tens of seconds so that quickly created and deleted files need
  196  * not waste disk bandwidth being created and removed. To realize this,
  197  * we append vnodes to a "workitem" queue. When running with a soft
  198  * updates implementation, most pending metadata dependencies should
  199  * not wait for more than a few seconds. Thus, mounted on block devices
  200  * are delayed only about a half the time that file data is delayed.
  201  * Similarly, directory updates are more critical, so are only delayed
  202  * about a third the time that file data is delayed. Thus, there are
  203  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
  204  * one each second (driven off the filesystem syncer process). The
  205  * syncer_delayno variable indicates the next queue that is to be processed.
  206  * Items that need to be processed soon are placed in this queue:
  207  *
  208  *      syncer_workitem_pending[syncer_delayno]
  209  *
  210  * A delay of fifteen seconds is done by placing the request fifteen
  211  * entries later in the queue:
  212  *
  213  *      syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
  214  *
  215  */
  216 static int syncer_delayno;
  217 static long syncer_mask;
  218 LIST_HEAD(synclist, bufobj);
  219 static struct synclist *syncer_workitem_pending[2];
  220 /*
  221  * The sync_mtx protects:
  222  *      bo->bo_synclist
  223  *      sync_vnode_count
  224  *      syncer_delayno
  225  *      syncer_state
  226  *      syncer_workitem_pending
  227  *      syncer_worklist_len
  228  *      rushjob
  229  */
  230 static struct mtx sync_mtx;
  231 static struct cv sync_wakeup;
  232 
  233 #define SYNCER_MAXDELAY         32
  234 static int syncer_maxdelay = SYNCER_MAXDELAY;   /* maximum delay time */
  235 static int syncdelay = 30;              /* max time to delay syncing data */
  236 static int filedelay = 30;              /* time to delay syncing files */
  237 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
  238     "Time to delay syncing files (in seconds)");
  239 static int dirdelay = 29;               /* time to delay syncing directories */
  240 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
  241     "Time to delay syncing directories (in seconds)");
  242 static int metadelay = 28;              /* time to delay syncing metadata */
  243 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
  244     "Time to delay syncing metadata (in seconds)");
  245 static int rushjob;             /* number of slots to run ASAP */
  246 static int stat_rush_requests;  /* number of times I/O speeded up */
  247 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
  248     "Number of times I/O speeded up (rush requests)");
  249 
  250 /*
  251  * When shutting down the syncer, run it at four times normal speed.
  252  */
  253 #define SYNCER_SHUTDOWN_SPEEDUP         4
  254 static int sync_vnode_count;
  255 static int syncer_worklist_len;
  256 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
  257     syncer_state;
  258 
  259 /*
  260  * Number of vnodes we want to exist at any one time.  This is mostly used
  261  * to size hash tables in vnode-related code.  It is normally not used in
  262  * getnewvnode(), as wantfreevnodes is normally nonzero.)
  263  *
  264  * XXX desiredvnodes is historical cruft and should not exist.
  265  */
  266 int desiredvnodes;
  267 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
  268     &desiredvnodes, 0, "Maximum number of vnodes");
  269 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
  270     &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
  271 static int vnlru_nowhere;
  272 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
  273     &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
  274 
  275 /*
  276  * Macros to control when a vnode is freed and recycled.  All require
  277  * the vnode interlock.
  278  */
  279 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
  280 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
  281 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
  282 
  283 
  284 /*
  285  * Initialize the vnode management data structures.
  286  *
  287  * Reevaluate the following cap on the number of vnodes after the physical
  288  * memory size exceeds 512GB.  In the limit, as the physical memory size
  289  * grows, the ratio of physical pages to vnodes approaches sixteen to one.
  290  */
  291 #ifndef MAXVNODES_MAX
  292 #define MAXVNODES_MAX   (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
  293 #endif
  294 static void
  295 vntblinit(void *dummy __unused)
  296 {
  297         int physvnodes, virtvnodes;
  298 
  299         /*
  300          * Desiredvnodes is a function of the physical memory size and the
  301          * kernel's heap size.  Generally speaking, it scales with the
  302          * physical memory size.  The ratio of desiredvnodes to physical pages
  303          * is one to four until desiredvnodes exceeds 98,304.  Thereafter, the
  304          * marginal ratio of desiredvnodes to physical pages is one to
  305          * sixteen.  However, desiredvnodes is limited by the kernel's heap
  306          * size.  The memory required by desiredvnodes vnodes and vm objects
  307          * may not exceed one seventh of the kernel's heap size.
  308          */
  309         physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
  310             cnt.v_page_count) / 16;
  311         virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
  312             sizeof(struct vnode)));
  313         desiredvnodes = min(physvnodes, virtvnodes);
  314         if (desiredvnodes > MAXVNODES_MAX) {
  315                 if (bootverbose)
  316                         printf("Reducing kern.maxvnodes %d -> %d\n",
  317                             desiredvnodes, MAXVNODES_MAX);
  318                 desiredvnodes = MAXVNODES_MAX;
  319         }
  320         wantfreevnodes = desiredvnodes / 4;
  321         mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
  322         TAILQ_INIT(&vnode_free_list);
  323         mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
  324         vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
  325             NULL, NULL, UMA_ALIGN_PTR, 0);
  326         vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
  327             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
  328         /*
  329          * Initialize the filesystem syncer.
  330          */
  331         syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
  332             &syncer_mask);
  333         syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
  334             &syncer_mask);
  335         syncer_maxdelay = syncer_mask + 1;
  336         mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
  337         cv_init(&sync_wakeup, "syncer");
  338 }
  339 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
  340 
  341 
  342 /*
  343  * Mark a mount point as busy. Used to synchronize access and to delay
  344  * unmounting. Eventually, mountlist_mtx is not released on failure.
  345  *
  346  * vfs_busy() is a custom lock, it can block the caller.
  347  * vfs_busy() only sleeps if the unmount is active on the mount point.
  348  * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
  349  * vnode belonging to mp.
  350  *
  351  * Lookup uses vfs_busy() to traverse mount points.
  352  * root fs                      var fs
  353  * / vnode lock         A       / vnode lock (/var)             D
  354  * /var vnode lock      B       /log vnode lock(/var/log)       E
  355  * vfs_busy lock        C       vfs_busy lock                   F
  356  *
  357  * Within each file system, the lock order is C->A->B and F->D->E.
  358  *
  359  * When traversing across mounts, the system follows that lock order:
  360  *
  361  *        C->A->B
  362  *              |
  363  *              +->F->D->E
  364  *
  365  * The lookup() process for namei("/var") illustrates the process:
  366  *  VOP_LOOKUP() obtains B while A is held
  367  *  vfs_busy() obtains a shared lock on F while A and B are held
  368  *  vput() releases lock on B
  369  *  vput() releases lock on A
  370  *  VFS_ROOT() obtains lock on D while shared lock on F is held
  371  *  vfs_unbusy() releases shared lock on F
  372  *  vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
  373  *    Attempt to lock A (instead of vp_crossmp) while D is held would
  374  *    violate the global order, causing deadlocks.
  375  *
  376  * dounmount() locks B while F is drained.
  377  */
  378 int
  379 vfs_busy(struct mount *mp, int flags)
  380 {
  381 
  382         MPASS((flags & ~MBF_MASK) == 0);
  383         CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
  384 
  385         MNT_ILOCK(mp);
  386         MNT_REF(mp);
  387         /*
  388          * If mount point is currenly being unmounted, sleep until the
  389          * mount point fate is decided.  If thread doing the unmounting fails,
  390          * it will clear MNTK_UNMOUNT flag before waking us up, indicating
  391          * that this mount point has survived the unmount attempt and vfs_busy
  392          * should retry.  Otherwise the unmounter thread will set MNTK_REFEXPIRE
  393          * flag in addition to MNTK_UNMOUNT, indicating that mount point is
  394          * about to be really destroyed.  vfs_busy needs to release its
  395          * reference on the mount point in this case and return with ENOENT,
  396          * telling the caller that mount mount it tried to busy is no longer
  397          * valid.
  398          */
  399         while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
  400                 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
  401                         MNT_REL(mp);
  402                         MNT_IUNLOCK(mp);
  403                         CTR1(KTR_VFS, "%s: failed busying before sleeping",
  404                             __func__);
  405                         return (ENOENT);
  406                 }
  407                 if (flags & MBF_MNTLSTLOCK)
  408                         mtx_unlock(&mountlist_mtx);
  409                 mp->mnt_kern_flag |= MNTK_MWAIT;
  410                 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
  411                 if (flags & MBF_MNTLSTLOCK)
  412                         mtx_lock(&mountlist_mtx);
  413                 MNT_ILOCK(mp);
  414         }
  415         if (flags & MBF_MNTLSTLOCK)
  416                 mtx_unlock(&mountlist_mtx);
  417         mp->mnt_lockref++;
  418         MNT_IUNLOCK(mp);
  419         return (0);
  420 }
  421 
  422 /*
  423  * Free a busy filesystem.
  424  */
  425 void
  426 vfs_unbusy(struct mount *mp)
  427 {
  428 
  429         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
  430         MNT_ILOCK(mp);
  431         MNT_REL(mp);
  432         KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
  433         mp->mnt_lockref--;
  434         if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
  435                 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
  436                 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
  437                 mp->mnt_kern_flag &= ~MNTK_DRAINING;
  438                 wakeup(&mp->mnt_lockref);
  439         }
  440         MNT_IUNLOCK(mp);
  441 }
  442 
  443 /*
  444  * Lookup a mount point by filesystem identifier.
  445  */
  446 struct mount *
  447 vfs_getvfs(fsid_t *fsid)
  448 {
  449         struct mount *mp;
  450 
  451         CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
  452         mtx_lock(&mountlist_mtx);
  453         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
  454                 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
  455                     mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
  456                         vfs_ref(mp);
  457                         mtx_unlock(&mountlist_mtx);
  458                         return (mp);
  459                 }
  460         }
  461         mtx_unlock(&mountlist_mtx);
  462         CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
  463         return ((struct mount *) 0);
  464 }
  465 
  466 /*
  467  * Lookup a mount point by filesystem identifier, busying it before
  468  * returning.
  469  */
  470 struct mount *
  471 vfs_busyfs(fsid_t *fsid)
  472 {
  473         struct mount *mp;
  474         int error;
  475 
  476         CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
  477         mtx_lock(&mountlist_mtx);
  478         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
  479                 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
  480                     mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
  481                         error = vfs_busy(mp, MBF_MNTLSTLOCK);
  482                         if (error) {
  483                                 mtx_unlock(&mountlist_mtx);
  484                                 return (NULL);
  485                         }
  486                         return (mp);
  487                 }
  488         }
  489         CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
  490         mtx_unlock(&mountlist_mtx);
  491         return ((struct mount *) 0);
  492 }
  493 
  494 /*
  495  * Check if a user can access privileged mount options.
  496  */
  497 int
  498 vfs_suser(struct mount *mp, struct thread *td)
  499 {
  500         int error;
  501 
  502         /*
  503          * If the thread is jailed, but this is not a jail-friendly file
  504          * system, deny immediately.
  505          */
  506         if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
  507                 return (EPERM);
  508 
  509         /*
  510          * If the file system was mounted outside the jail of the calling
  511          * thread, deny immediately.
  512          */
  513         if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
  514                 return (EPERM);
  515 
  516         /*
  517          * If file system supports delegated administration, we don't check
  518          * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
  519          * by the file system itself.
  520          * If this is not the user that did original mount, we check for
  521          * the PRIV_VFS_MOUNT_OWNER privilege.
  522          */
  523         if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
  524             mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
  525                 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
  526                         return (error);
  527         }
  528         return (0);
  529 }
  530 
  531 /*
  532  * Get a new unique fsid.  Try to make its val[0] unique, since this value
  533  * will be used to create fake device numbers for stat().  Also try (but
  534  * not so hard) make its val[0] unique mod 2^16, since some emulators only
  535  * support 16-bit device numbers.  We end up with unique val[0]'s for the
  536  * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
  537  *
  538  * Keep in mind that several mounts may be running in parallel.  Starting
  539  * the search one past where the previous search terminated is both a
  540  * micro-optimization and a defense against returning the same fsid to
  541  * different mounts.
  542  */
  543 void
  544 vfs_getnewfsid(struct mount *mp)
  545 {
  546         static uint16_t mntid_base;
  547         struct mount *nmp;
  548         fsid_t tfsid;
  549         int mtype;
  550 
  551         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
  552         mtx_lock(&mntid_mtx);
  553         mtype = mp->mnt_vfc->vfc_typenum;
  554         tfsid.val[1] = mtype;
  555         mtype = (mtype & 0xFF) << 24;
  556         for (;;) {
  557                 tfsid.val[0] = makedev(255,
  558                     mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
  559                 mntid_base++;
  560                 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
  561                         break;
  562                 vfs_rel(nmp);
  563         }
  564         mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
  565         mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
  566         mtx_unlock(&mntid_mtx);
  567 }
  568 
  569 /*
  570  * Knob to control the precision of file timestamps:
  571  *
  572  *   0 = seconds only; nanoseconds zeroed.
  573  *   1 = seconds and nanoseconds, accurate within 1/HZ.
  574  *   2 = seconds and nanoseconds, truncated to microseconds.
  575  * >=3 = seconds and nanoseconds, maximum precision.
  576  */
  577 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
  578 
  579 static int timestamp_precision = TSP_SEC;
  580 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
  581     &timestamp_precision, 0, "File timestamp precision (0: seconds, "
  582     "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
  583     "3+: sec + ns (max. precision))");
  584 
  585 /*
  586  * Get a current timestamp.
  587  */
  588 void
  589 vfs_timestamp(struct timespec *tsp)
  590 {
  591         struct timeval tv;
  592 
  593         switch (timestamp_precision) {
  594         case TSP_SEC:
  595                 tsp->tv_sec = time_second;
  596                 tsp->tv_nsec = 0;
  597                 break;
  598         case TSP_HZ:
  599                 getnanotime(tsp);
  600                 break;
  601         case TSP_USEC:
  602                 microtime(&tv);
  603                 TIMEVAL_TO_TIMESPEC(&tv, tsp);
  604                 break;
  605         case TSP_NSEC:
  606         default:
  607                 nanotime(tsp);
  608                 break;
  609         }
  610 }
  611 
  612 /*
  613  * Set vnode attributes to VNOVAL
  614  */
  615 void
  616 vattr_null(struct vattr *vap)
  617 {
  618 
  619         vap->va_type = VNON;
  620         vap->va_size = VNOVAL;
  621         vap->va_bytes = VNOVAL;
  622         vap->va_mode = VNOVAL;
  623         vap->va_nlink = VNOVAL;
  624         vap->va_uid = VNOVAL;
  625         vap->va_gid = VNOVAL;
  626         vap->va_fsid = VNOVAL;
  627         vap->va_fileid = VNOVAL;
  628         vap->va_blocksize = VNOVAL;
  629         vap->va_rdev = VNOVAL;
  630         vap->va_atime.tv_sec = VNOVAL;
  631         vap->va_atime.tv_nsec = VNOVAL;
  632         vap->va_mtime.tv_sec = VNOVAL;
  633         vap->va_mtime.tv_nsec = VNOVAL;
  634         vap->va_ctime.tv_sec = VNOVAL;
  635         vap->va_ctime.tv_nsec = VNOVAL;
  636         vap->va_birthtime.tv_sec = VNOVAL;
  637         vap->va_birthtime.tv_nsec = VNOVAL;
  638         vap->va_flags = VNOVAL;
  639         vap->va_gen = VNOVAL;
  640         vap->va_vaflags = 0;
  641 }
  642 
  643 /*
  644  * This routine is called when we have too many vnodes.  It attempts
  645  * to free <count> vnodes and will potentially free vnodes that still
  646  * have VM backing store (VM backing store is typically the cause
  647  * of a vnode blowout so we want to do this).  Therefore, this operation
  648  * is not considered cheap.
  649  *
  650  * A number of conditions may prevent a vnode from being reclaimed.
  651  * the buffer cache may have references on the vnode, a directory
  652  * vnode may still have references due to the namei cache representing
  653  * underlying files, or the vnode may be in active use.   It is not
  654  * desireable to reuse such vnodes.  These conditions may cause the
  655  * number of vnodes to reach some minimum value regardless of what
  656  * you set kern.maxvnodes to.  Do not set kern.maxvnodes too low.
  657  */
  658 static int
  659 vlrureclaim(struct mount *mp)
  660 {
  661         struct vnode *vp;
  662         int done;
  663         int trigger;
  664         int usevnodes;
  665         int count;
  666 
  667         /*
  668          * Calculate the trigger point, don't allow user
  669          * screwups to blow us up.   This prevents us from
  670          * recycling vnodes with lots of resident pages.  We
  671          * aren't trying to free memory, we are trying to
  672          * free vnodes.
  673          */
  674         usevnodes = desiredvnodes;
  675         if (usevnodes <= 0)
  676                 usevnodes = 1;
  677         trigger = cnt.v_page_count * 2 / usevnodes;
  678         done = 0;
  679         vn_start_write(NULL, &mp, V_WAIT);
  680         MNT_ILOCK(mp);
  681         count = mp->mnt_nvnodelistsize / 10 + 1;
  682         while (count != 0) {
  683                 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
  684                 while (vp != NULL && vp->v_type == VMARKER)
  685                         vp = TAILQ_NEXT(vp, v_nmntvnodes);
  686                 if (vp == NULL)
  687                         break;
  688                 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
  689                 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
  690                 --count;
  691                 if (!VI_TRYLOCK(vp))
  692                         goto next_iter;
  693                 /*
  694                  * If it's been deconstructed already, it's still
  695                  * referenced, or it exceeds the trigger, skip it.
  696                  */
  697                 if (vp->v_usecount ||
  698                     (!vlru_allow_cache_src &&
  699                         !LIST_EMPTY(&(vp)->v_cache_src)) ||
  700                     (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
  701                     vp->v_object->resident_page_count > trigger)) {
  702                         VI_UNLOCK(vp);
  703                         goto next_iter;
  704                 }
  705                 MNT_IUNLOCK(mp);
  706                 vholdl(vp);
  707                 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
  708                         vdrop(vp);
  709                         goto next_iter_mntunlocked;
  710                 }
  711                 VI_LOCK(vp);
  712                 /*
  713                  * v_usecount may have been bumped after VOP_LOCK() dropped
  714                  * the vnode interlock and before it was locked again.
  715                  *
  716                  * It is not necessary to recheck VI_DOOMED because it can
  717                  * only be set by another thread that holds both the vnode
  718                  * lock and vnode interlock.  If another thread has the
  719                  * vnode lock before we get to VOP_LOCK() and obtains the
  720                  * vnode interlock after VOP_LOCK() drops the vnode
  721                  * interlock, the other thread will be unable to drop the
  722                  * vnode lock before our VOP_LOCK() call fails.
  723                  */
  724                 if (vp->v_usecount ||
  725                     (!vlru_allow_cache_src &&
  726                         !LIST_EMPTY(&(vp)->v_cache_src)) ||
  727                     (vp->v_object != NULL &&
  728                     vp->v_object->resident_page_count > trigger)) {
  729                         VOP_UNLOCK(vp, LK_INTERLOCK);
  730                         goto next_iter_mntunlocked;
  731                 }
  732                 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
  733                     ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
  734                 vgonel(vp);
  735                 VOP_UNLOCK(vp, 0);
  736                 vdropl(vp);
  737                 done++;
  738 next_iter_mntunlocked:
  739                 if (!should_yield())
  740                         goto relock_mnt;
  741                 goto yield;
  742 next_iter:
  743                 if (!should_yield())
  744                         continue;
  745                 MNT_IUNLOCK(mp);
  746 yield:
  747                 kern_yield(PRI_UNCHANGED);
  748 relock_mnt:
  749                 MNT_ILOCK(mp);
  750         }
  751         MNT_IUNLOCK(mp);
  752         vn_finished_write(mp);
  753         return done;
  754 }
  755 
  756 /*
  757  * Attempt to keep the free list at wantfreevnodes length.
  758  */
  759 static void
  760 vnlru_free(int count)
  761 {
  762         struct vnode *vp;
  763         int vfslocked;
  764 
  765         mtx_assert(&vnode_free_list_mtx, MA_OWNED);
  766         for (; count > 0; count--) {
  767                 vp = TAILQ_FIRST(&vnode_free_list);
  768                 /*
  769                  * The list can be modified while the free_list_mtx
  770                  * has been dropped and vp could be NULL here.
  771                  */
  772                 if (!vp)
  773                         break;
  774                 VNASSERT(vp->v_op != NULL, vp,
  775                     ("vnlru_free: vnode already reclaimed."));
  776                 KASSERT((vp->v_iflag & VI_FREE) != 0,
  777                     ("Removing vnode not on freelist"));
  778                 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
  779                     ("Mangling active vnode"));
  780                 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
  781                 /*
  782                  * Don't recycle if we can't get the interlock.
  783                  */
  784                 if (!VI_TRYLOCK(vp)) {
  785                         TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
  786                         continue;
  787                 }
  788                 VNASSERT(VCANRECYCLE(vp), vp,
  789                     ("vp inconsistent on freelist"));
  790                 freevnodes--;
  791                 vp->v_iflag &= ~VI_FREE;
  792                 vholdl(vp);
  793                 mtx_unlock(&vnode_free_list_mtx);
  794                 VI_UNLOCK(vp);
  795                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  796                 vtryrecycle(vp);
  797                 VFS_UNLOCK_GIANT(vfslocked);
  798                 /*
  799                  * If the recycled succeeded this vdrop will actually free
  800                  * the vnode.  If not it will simply place it back on
  801                  * the free list.
  802                  */
  803                 vdrop(vp);
  804                 mtx_lock(&vnode_free_list_mtx);
  805         }
  806 }
  807 /*
  808  * Attempt to recycle vnodes in a context that is always safe to block.
  809  * Calling vlrurecycle() from the bowels of filesystem code has some
  810  * interesting deadlock problems.
  811  */
  812 static struct proc *vnlruproc;
  813 static int vnlruproc_sig;
  814 
  815 static void
  816 vnlru_proc(void)
  817 {
  818         struct mount *mp, *nmp;
  819         int done, vfslocked;
  820         struct proc *p = vnlruproc;
  821 
  822         EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
  823             SHUTDOWN_PRI_FIRST);
  824 
  825         for (;;) {
  826                 kproc_suspend_check(p);
  827                 mtx_lock(&vnode_free_list_mtx);
  828                 if (freevnodes > wantfreevnodes)
  829                         vnlru_free(freevnodes - wantfreevnodes);
  830                 if (numvnodes <= desiredvnodes * 9 / 10) {
  831                         vnlruproc_sig = 0;
  832                         wakeup(&vnlruproc_sig);
  833                         msleep(vnlruproc, &vnode_free_list_mtx,
  834                             PVFS|PDROP, "vlruwt", hz);
  835                         continue;
  836                 }
  837                 mtx_unlock(&vnode_free_list_mtx);
  838                 done = 0;
  839                 mtx_lock(&mountlist_mtx);
  840                 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
  841                         if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
  842                                 nmp = TAILQ_NEXT(mp, mnt_list);
  843                                 continue;
  844                         }
  845                         vfslocked = VFS_LOCK_GIANT(mp);
  846                         done += vlrureclaim(mp);
  847                         VFS_UNLOCK_GIANT(vfslocked);
  848                         mtx_lock(&mountlist_mtx);
  849                         nmp = TAILQ_NEXT(mp, mnt_list);
  850                         vfs_unbusy(mp);
  851                 }
  852                 mtx_unlock(&mountlist_mtx);
  853                 if (done == 0) {
  854 #if 0
  855                         /* These messages are temporary debugging aids */
  856                         if (vnlru_nowhere < 5)
  857                                 printf("vnlru process getting nowhere..\n");
  858                         else if (vnlru_nowhere == 5)
  859                                 printf("vnlru process messages stopped.\n");
  860 #endif
  861                         vnlru_nowhere++;
  862                         tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
  863                 } else
  864                         kern_yield(PRI_UNCHANGED);
  865         }
  866 }
  867 
  868 static struct kproc_desc vnlru_kp = {
  869         "vnlru",
  870         vnlru_proc,
  871         &vnlruproc
  872 };
  873 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
  874     &vnlru_kp);
  875  
  876 /*
  877  * Routines having to do with the management of the vnode table.
  878  */
  879 
  880 /*
  881  * Try to recycle a freed vnode.  We abort if anyone picks up a reference
  882  * before we actually vgone().  This function must be called with the vnode
  883  * held to prevent the vnode from being returned to the free list midway
  884  * through vgone().
  885  */
  886 static int
  887 vtryrecycle(struct vnode *vp)
  888 {
  889         struct mount *vnmp;
  890 
  891         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
  892         VNASSERT(vp->v_holdcnt, vp,
  893             ("vtryrecycle: Recycling vp %p without a reference.", vp));
  894         /*
  895          * This vnode may found and locked via some other list, if so we
  896          * can't recycle it yet.
  897          */
  898         if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
  899                 CTR2(KTR_VFS,
  900                     "%s: impossible to recycle, vp %p lock is already held",
  901                     __func__, vp);
  902                 return (EWOULDBLOCK);
  903         }
  904         /*
  905          * Don't recycle if its filesystem is being suspended.
  906          */
  907         if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
  908                 VOP_UNLOCK(vp, 0);
  909                 CTR2(KTR_VFS,
  910                     "%s: impossible to recycle, cannot start the write for %p",
  911                     __func__, vp);
  912                 return (EBUSY);
  913         }
  914         /*
  915          * If we got this far, we need to acquire the interlock and see if
  916          * anyone picked up this vnode from another list.  If not, we will
  917          * mark it with DOOMED via vgonel() so that anyone who does find it
  918          * will skip over it.
  919          */
  920         VI_LOCK(vp);
  921         if (vp->v_usecount) {
  922                 VOP_UNLOCK(vp, LK_INTERLOCK);
  923                 vn_finished_write(vnmp);
  924                 CTR2(KTR_VFS,
  925                     "%s: impossible to recycle, %p is already referenced",
  926                     __func__, vp);
  927                 return (EBUSY);
  928         }
  929         if ((vp->v_iflag & VI_DOOMED) == 0)
  930                 vgonel(vp);
  931         VOP_UNLOCK(vp, LK_INTERLOCK);
  932         vn_finished_write(vnmp);
  933         return (0);
  934 }
  935 
  936 /*
  937  * Return the next vnode from the free list.
  938  */
  939 int
  940 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
  941     struct vnode **vpp)
  942 {
  943         struct vnode *vp = NULL;
  944         struct bufobj *bo;
  945 
  946         CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
  947         mtx_lock(&vnode_free_list_mtx);
  948         /*
  949          * Lend our context to reclaim vnodes if they've exceeded the max.
  950          */
  951         if (freevnodes > wantfreevnodes)
  952                 vnlru_free(1);
  953         /*
  954          * Wait for available vnodes.
  955          */
  956         if (numvnodes > desiredvnodes) {
  957                 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
  958                         /*
  959                          * File system is beeing suspended, we cannot risk a
  960                          * deadlock here, so allocate new vnode anyway.
  961                          */
  962                         if (freevnodes > wantfreevnodes)
  963                                 vnlru_free(freevnodes - wantfreevnodes);
  964                         goto alloc;
  965                 }
  966                 if (vnlruproc_sig == 0) {
  967                         vnlruproc_sig = 1;      /* avoid unnecessary wakeups */
  968                         wakeup(vnlruproc);
  969                 }
  970                 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
  971                     "vlruwk", hz);
  972 #if 0   /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
  973                 if (numvnodes > desiredvnodes) {
  974                         mtx_unlock(&vnode_free_list_mtx);
  975                         return (ENFILE);
  976                 }
  977 #endif
  978         }
  979 alloc:
  980         numvnodes++;
  981         mtx_unlock(&vnode_free_list_mtx);
  982         vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
  983         /*
  984          * Setup locks.
  985          */
  986         vp->v_vnlock = &vp->v_lock;
  987         mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
  988         /*
  989          * By default, don't allow shared locks unless filesystems
  990          * opt-in.
  991          */
  992         lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
  993         /*
  994          * Initialize bufobj.
  995          */
  996         bo = &vp->v_bufobj;
  997         bo->__bo_vnode = vp;
  998         mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
  999         bo->bo_ops = &buf_ops_bio;
 1000         bo->bo_private = vp;
 1001         TAILQ_INIT(&bo->bo_clean.bv_hd);
 1002         TAILQ_INIT(&bo->bo_dirty.bv_hd);
 1003         /*
 1004          * Initialize namecache.
 1005          */
 1006         LIST_INIT(&vp->v_cache_src);
 1007         TAILQ_INIT(&vp->v_cache_dst);
 1008         /*
 1009          * Finalize various vnode identity bits.
 1010          */
 1011         vp->v_type = VNON;
 1012         vp->v_tag = tag;
 1013         vp->v_op = vops;
 1014         v_incr_usecount(vp);
 1015         vp->v_data = 0;
 1016 #ifdef MAC
 1017         mac_vnode_init(vp);
 1018         if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
 1019                 mac_vnode_associate_singlelabel(mp, vp);
 1020         else if (mp == NULL && vops != &dead_vnodeops)
 1021                 printf("NULL mp in getnewvnode()\n");
 1022 #endif
 1023         if (mp != NULL) {
 1024                 bo->bo_bsize = mp->mnt_stat.f_iosize;
 1025                 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
 1026                         vp->v_vflag |= VV_NOKNOTE;
 1027         }
 1028 
 1029         *vpp = vp;
 1030         return (0);
 1031 }
 1032 
 1033 /*
 1034  * Delete from old mount point vnode list, if on one.
 1035  */
 1036 static void
 1037 delmntque(struct vnode *vp)
 1038 {
 1039         struct mount *mp;
 1040         int active;
 1041 
 1042         mp = vp->v_mount;
 1043         if (mp == NULL)
 1044                 return;
 1045         MNT_ILOCK(mp);
 1046         VI_LOCK(vp);
 1047         KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
 1048             ("Active vnode list size %d > Vnode list size %d",
 1049              mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
 1050         active = vp->v_iflag & VI_ACTIVE;
 1051         vp->v_iflag &= ~VI_ACTIVE;
 1052         if (active) {
 1053                 mtx_lock(&vnode_free_list_mtx);
 1054                 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
 1055                 mp->mnt_activevnodelistsize--;
 1056                 mtx_unlock(&vnode_free_list_mtx);
 1057         }
 1058         vp->v_mount = NULL;
 1059         VI_UNLOCK(vp);
 1060         VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
 1061                 ("bad mount point vnode list size"));
 1062         TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
 1063         mp->mnt_nvnodelistsize--;
 1064         MNT_REL(mp);
 1065         MNT_IUNLOCK(mp);
 1066 }
 1067 
 1068 static void
 1069 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
 1070 {
 1071 
 1072         vp->v_data = NULL;
 1073         vp->v_op = &dead_vnodeops;
 1074         /* XXX non mp-safe fs may still call insmntque with vnode
 1075            unlocked */
 1076         if (!VOP_ISLOCKED(vp))
 1077                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 1078         vgone(vp);
 1079         vput(vp);
 1080 }
 1081 
 1082 /*
 1083  * Insert into list of vnodes for the new mount point, if available.
 1084  */
 1085 int
 1086 insmntque1(struct vnode *vp, struct mount *mp,
 1087         void (*dtr)(struct vnode *, void *), void *dtr_arg)
 1088 {
 1089         int locked;
 1090 
 1091         KASSERT(vp->v_mount == NULL,
 1092                 ("insmntque: vnode already on per mount vnode list"));
 1093         VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
 1094 #ifdef DEBUG_VFS_LOCKS
 1095         if (!VFS_NEEDSGIANT(mp))
 1096                 ASSERT_VOP_ELOCKED(vp,
 1097                     "insmntque: mp-safe fs and non-locked vp");
 1098 #endif
 1099         /*
 1100          * We acquire the vnode interlock early to ensure that the
 1101          * vnode cannot be recycled by another process releasing a
 1102          * holdcnt on it before we get it on both the vnode list
 1103          * and the active vnode list. The mount mutex protects only
 1104          * manipulation of the vnode list and the vnode freelist
 1105          * mutex protects only manipulation of the active vnode list.
 1106          * Hence the need to hold the vnode interlock throughout.
 1107          */
 1108         MNT_ILOCK(mp);
 1109         VI_LOCK(vp);
 1110         if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
 1111             ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
 1112              mp->mnt_nvnodelistsize == 0)) {
 1113                 locked = VOP_ISLOCKED(vp);
 1114                 if (!locked || (locked == LK_EXCLUSIVE &&
 1115                      (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
 1116                         VI_UNLOCK(vp);
 1117                         MNT_IUNLOCK(mp);
 1118                         if (dtr != NULL)
 1119                                 dtr(vp, dtr_arg);
 1120                         return (EBUSY);
 1121                 }
 1122         }
 1123         vp->v_mount = mp;
 1124         MNT_REF(mp);
 1125         TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
 1126         VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
 1127                 ("neg mount point vnode list size"));
 1128         mp->mnt_nvnodelistsize++;
 1129         KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
 1130             ("Activating already active vnode"));
 1131         vp->v_iflag |= VI_ACTIVE;
 1132         mtx_lock(&vnode_free_list_mtx);
 1133         TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
 1134         mp->mnt_activevnodelistsize++;
 1135         mtx_unlock(&vnode_free_list_mtx);
 1136         VI_UNLOCK(vp);
 1137         MNT_IUNLOCK(mp);
 1138         return (0);
 1139 }
 1140 
 1141 int
 1142 insmntque(struct vnode *vp, struct mount *mp)
 1143 {
 1144 
 1145         return (insmntque1(vp, mp, insmntque_stddtr, NULL));
 1146 }
 1147 
 1148 /*
 1149  * Flush out and invalidate all buffers associated with a bufobj
 1150  * Called with the underlying object locked.
 1151  */
 1152 int
 1153 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
 1154 {
 1155         int error;
 1156 
 1157         BO_LOCK(bo);
 1158         if (flags & V_SAVE) {
 1159                 error = bufobj_wwait(bo, slpflag, slptimeo);
 1160                 if (error) {
 1161                         BO_UNLOCK(bo);
 1162                         return (error);
 1163                 }
 1164                 if (bo->bo_dirty.bv_cnt > 0) {
 1165                         BO_UNLOCK(bo);
 1166                         if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
 1167                                 return (error);
 1168                         /*
 1169                          * XXX We could save a lock/unlock if this was only
 1170                          * enabled under INVARIANTS
 1171                          */
 1172                         BO_LOCK(bo);
 1173                         if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
 1174                                 panic("vinvalbuf: dirty bufs");
 1175                 }
 1176         }
 1177         /*
 1178          * If you alter this loop please notice that interlock is dropped and
 1179          * reacquired in flushbuflist.  Special care is needed to ensure that
 1180          * no race conditions occur from this.
 1181          */
 1182         do {
 1183                 error = flushbuflist(&bo->bo_clean,
 1184                     flags, bo, slpflag, slptimeo);
 1185                 if (error == 0 && !(flags & V_CLEANONLY))
 1186                         error = flushbuflist(&bo->bo_dirty,
 1187                             flags, bo, slpflag, slptimeo);
 1188                 if (error != 0 && error != EAGAIN) {
 1189                         BO_UNLOCK(bo);
 1190                         return (error);
 1191                 }
 1192         } while (error != 0);
 1193 
 1194         /*
 1195          * Wait for I/O to complete.  XXX needs cleaning up.  The vnode can
 1196          * have write I/O in-progress but if there is a VM object then the
 1197          * VM object can also have read-I/O in-progress.
 1198          */
 1199         do {
 1200                 bufobj_wwait(bo, 0, 0);
 1201                 BO_UNLOCK(bo);
 1202                 if (bo->bo_object != NULL) {
 1203                         VM_OBJECT_LOCK(bo->bo_object);
 1204                         vm_object_pip_wait(bo->bo_object, "bovlbx");
 1205                         VM_OBJECT_UNLOCK(bo->bo_object);
 1206                 }
 1207                 BO_LOCK(bo);
 1208         } while (bo->bo_numoutput > 0);
 1209         BO_UNLOCK(bo);
 1210 
 1211         /*
 1212          * Destroy the copy in the VM cache, too.
 1213          */
 1214         if (bo->bo_object != NULL &&
 1215             (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
 1216                 VM_OBJECT_LOCK(bo->bo_object);
 1217                 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
 1218                     OBJPR_CLEANONLY : 0);
 1219                 VM_OBJECT_UNLOCK(bo->bo_object);
 1220         }
 1221 
 1222 #ifdef INVARIANTS
 1223         BO_LOCK(bo);
 1224         if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
 1225             (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
 1226                 panic("vinvalbuf: flush failed");
 1227         BO_UNLOCK(bo);
 1228 #endif
 1229         return (0);
 1230 }
 1231 
 1232 /*
 1233  * Flush out and invalidate all buffers associated with a vnode.
 1234  * Called with the underlying object locked.
 1235  */
 1236 int
 1237 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
 1238 {
 1239 
 1240         CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
 1241         ASSERT_VOP_LOCKED(vp, "vinvalbuf");
 1242         return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
 1243 }
 1244 
 1245 /*
 1246  * Flush out buffers on the specified list.
 1247  *
 1248  */
 1249 static int
 1250 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
 1251     int slptimeo)
 1252 {
 1253         struct buf *bp, *nbp;
 1254         int retval, error;
 1255         daddr_t lblkno;
 1256         b_xflags_t xflags;
 1257 
 1258         ASSERT_BO_LOCKED(bo);
 1259 
 1260         retval = 0;
 1261         TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
 1262                 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
 1263                     ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
 1264                         continue;
 1265                 }
 1266                 lblkno = 0;
 1267                 xflags = 0;
 1268                 if (nbp != NULL) {
 1269                         lblkno = nbp->b_lblkno;
 1270                         xflags = nbp->b_xflags &
 1271                                 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
 1272                 }
 1273                 retval = EAGAIN;
 1274                 error = BUF_TIMELOCK(bp,
 1275                     LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
 1276                     "flushbuf", slpflag, slptimeo);
 1277                 if (error) {
 1278                         BO_LOCK(bo);
 1279                         return (error != ENOLCK ? error : EAGAIN);
 1280                 }
 1281                 KASSERT(bp->b_bufobj == bo,
 1282                     ("bp %p wrong b_bufobj %p should be %p",
 1283                     bp, bp->b_bufobj, bo));
 1284                 if (bp->b_bufobj != bo) {       /* XXX: necessary ? */
 1285                         BUF_UNLOCK(bp);
 1286                         BO_LOCK(bo);
 1287                         return (EAGAIN);
 1288                 }
 1289                 /*
 1290                  * XXX Since there are no node locks for NFS, I
 1291                  * believe there is a slight chance that a delayed
 1292                  * write will occur while sleeping just above, so
 1293                  * check for it.
 1294                  */
 1295                 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
 1296                     (flags & V_SAVE)) {
 1297                         BO_LOCK(bo);
 1298                         bremfree(bp);
 1299                         BO_UNLOCK(bo);
 1300                         bp->b_flags |= B_ASYNC;
 1301                         bwrite(bp);
 1302                         BO_LOCK(bo);
 1303                         return (EAGAIN);        /* XXX: why not loop ? */
 1304                 }
 1305                 BO_LOCK(bo);
 1306                 bremfree(bp);
 1307                 BO_UNLOCK(bo);
 1308                 bp->b_flags |= (B_INVAL | B_RELBUF);
 1309                 bp->b_flags &= ~B_ASYNC;
 1310                 brelse(bp);
 1311                 BO_LOCK(bo);
 1312                 if (nbp != NULL &&
 1313                     (nbp->b_bufobj != bo ||
 1314                      nbp->b_lblkno != lblkno ||
 1315                      (nbp->b_xflags &
 1316                       (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
 1317                         break;                  /* nbp invalid */
 1318         }
 1319         return (retval);
 1320 }
 1321 
 1322 /*
 1323  * Truncate a file's buffer and pages to a specified length.  This
 1324  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
 1325  * sync activity.
 1326  */
 1327 int
 1328 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
 1329     off_t length, int blksize)
 1330 {
 1331         struct buf *bp, *nbp;
 1332         int anyfreed;
 1333         int trunclbn;
 1334         struct bufobj *bo;
 1335 
 1336         CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
 1337             vp, cred, blksize, (uintmax_t)length);
 1338 
 1339         /*
 1340          * Round up to the *next* lbn.
 1341          */
 1342         trunclbn = (length + blksize - 1) / blksize;
 1343 
 1344         ASSERT_VOP_LOCKED(vp, "vtruncbuf");
 1345 restart:
 1346         bo = &vp->v_bufobj;
 1347         BO_LOCK(bo);
 1348         anyfreed = 1;
 1349         for (;anyfreed;) {
 1350                 anyfreed = 0;
 1351                 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
 1352                         if (bp->b_lblkno < trunclbn)
 1353                                 continue;
 1354                         if (BUF_LOCK(bp,
 1355                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
 1356                             BO_MTX(bo)) == ENOLCK)
 1357                                 goto restart;
 1358 
 1359                         BO_LOCK(bo);
 1360                         bremfree(bp);
 1361                         BO_UNLOCK(bo);
 1362                         bp->b_flags |= (B_INVAL | B_RELBUF);
 1363                         bp->b_flags &= ~B_ASYNC;
 1364                         brelse(bp);
 1365                         anyfreed = 1;
 1366 
 1367                         BO_LOCK(bo);
 1368                         if (nbp != NULL &&
 1369                             (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
 1370                             (nbp->b_vp != vp) ||
 1371                             (nbp->b_flags & B_DELWRI))) {
 1372                                 BO_UNLOCK(bo);
 1373                                 goto restart;
 1374                         }
 1375                 }
 1376 
 1377                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
 1378                         if (bp->b_lblkno < trunclbn)
 1379                                 continue;
 1380                         if (BUF_LOCK(bp,
 1381                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
 1382                             BO_MTX(bo)) == ENOLCK)
 1383                                 goto restart;
 1384                         BO_LOCK(bo);
 1385                         bremfree(bp);
 1386                         BO_UNLOCK(bo);
 1387                         bp->b_flags |= (B_INVAL | B_RELBUF);
 1388                         bp->b_flags &= ~B_ASYNC;
 1389                         brelse(bp);
 1390                         anyfreed = 1;
 1391 
 1392                         BO_LOCK(bo);
 1393                         if (nbp != NULL &&
 1394                             (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
 1395                             (nbp->b_vp != vp) ||
 1396                             (nbp->b_flags & B_DELWRI) == 0)) {
 1397                                 BO_UNLOCK(bo);
 1398                                 goto restart;
 1399                         }
 1400                 }
 1401         }
 1402 
 1403         if (length > 0) {
 1404 restartsync:
 1405                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
 1406                         if (bp->b_lblkno > 0)
 1407                                 continue;
 1408                         /*
 1409                          * Since we hold the vnode lock this should only
 1410                          * fail if we're racing with the buf daemon.
 1411                          */
 1412                         if (BUF_LOCK(bp,
 1413                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
 1414                             BO_MTX(bo)) == ENOLCK) {
 1415                                 goto restart;
 1416                         }
 1417                         VNASSERT((bp->b_flags & B_DELWRI), vp,
 1418                             ("buf(%p) on dirty queue without DELWRI", bp));
 1419 
 1420                         BO_LOCK(bo);
 1421                         bremfree(bp);
 1422                         BO_UNLOCK(bo);
 1423                         bawrite(bp);
 1424                         BO_LOCK(bo);
 1425                         goto restartsync;
 1426                 }
 1427         }
 1428 
 1429         bufobj_wwait(bo, 0, 0);
 1430         BO_UNLOCK(bo);
 1431         vnode_pager_setsize(vp, length);
 1432 
 1433         return (0);
 1434 }
 1435 
 1436 /*
 1437  * buf_splay() - splay tree core for the clean/dirty list of buffers in
 1438  *               a vnode.
 1439  *
 1440  *      NOTE: We have to deal with the special case of a background bitmap
 1441  *      buffer, a situation where two buffers will have the same logical
 1442  *      block offset.  We want (1) only the foreground buffer to be accessed
 1443  *      in a lookup and (2) must differentiate between the foreground and
 1444  *      background buffer in the splay tree algorithm because the splay
 1445  *      tree cannot normally handle multiple entities with the same 'index'.
 1446  *      We accomplish this by adding differentiating flags to the splay tree's
 1447  *      numerical domain.
 1448  */
 1449 static
 1450 struct buf *
 1451 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
 1452 {
 1453         struct buf dummy;
 1454         struct buf *lefttreemax, *righttreemin, *y;
 1455 
 1456         if (root == NULL)
 1457                 return (NULL);
 1458         lefttreemax = righttreemin = &dummy;
 1459         for (;;) {
 1460                 if (lblkno < root->b_lblkno ||
 1461                     (lblkno == root->b_lblkno &&
 1462                     (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
 1463                         if ((y = root->b_left) == NULL)
 1464                                 break;
 1465                         if (lblkno < y->b_lblkno) {
 1466                                 /* Rotate right. */
 1467                                 root->b_left = y->b_right;
 1468                                 y->b_right = root;
 1469                                 root = y;
 1470                                 if ((y = root->b_left) == NULL)
 1471                                         break;
 1472                         }
 1473                         /* Link into the new root's right tree. */
 1474                         righttreemin->b_left = root;
 1475                         righttreemin = root;
 1476                 } else if (lblkno > root->b_lblkno ||
 1477                     (lblkno == root->b_lblkno &&
 1478                     (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
 1479                         if ((y = root->b_right) == NULL)
 1480                                 break;
 1481                         if (lblkno > y->b_lblkno) {
 1482                                 /* Rotate left. */
 1483                                 root->b_right = y->b_left;
 1484                                 y->b_left = root;
 1485                                 root = y;
 1486                                 if ((y = root->b_right) == NULL)
 1487                                         break;
 1488                         }
 1489                         /* Link into the new root's left tree. */
 1490                         lefttreemax->b_right = root;
 1491                         lefttreemax = root;
 1492                 } else {
 1493                         break;
 1494                 }
 1495                 root = y;
 1496         }
 1497         /* Assemble the new root. */
 1498         lefttreemax->b_right = root->b_left;
 1499         righttreemin->b_left = root->b_right;
 1500         root->b_left = dummy.b_right;
 1501         root->b_right = dummy.b_left;
 1502         return (root);
 1503 }
 1504 
 1505 static void
 1506 buf_vlist_remove(struct buf *bp)
 1507 {
 1508         struct buf *root;
 1509         struct bufv *bv;
 1510 
 1511         KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
 1512         ASSERT_BO_LOCKED(bp->b_bufobj);
 1513         KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
 1514             (BX_VNDIRTY|BX_VNCLEAN),
 1515             ("buf_vlist_remove: Buf %p is on two lists", bp));
 1516         if (bp->b_xflags & BX_VNDIRTY)
 1517                 bv = &bp->b_bufobj->bo_dirty;
 1518         else
 1519                 bv = &bp->b_bufobj->bo_clean;
 1520         if (bp != bv->bv_root) {
 1521                 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
 1522                 KASSERT(root == bp, ("splay lookup failed in remove"));
 1523         }
 1524         if (bp->b_left == NULL) {
 1525                 root = bp->b_right;
 1526         } else {
 1527                 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
 1528                 root->b_right = bp->b_right;
 1529         }
 1530         bv->bv_root = root;
 1531         TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
 1532         bv->bv_cnt--;
 1533         bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
 1534 }
 1535 
 1536 /*
 1537  * Add the buffer to the sorted clean or dirty block list using a
 1538  * splay tree algorithm.
 1539  *
 1540  * NOTE: xflags is passed as a constant, optimizing this inline function!
 1541  */
 1542 static void
 1543 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
 1544 {
 1545         struct buf *root;
 1546         struct bufv *bv;
 1547 
 1548         ASSERT_BO_LOCKED(bo);
 1549         KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
 1550             ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
 1551         bp->b_xflags |= xflags;
 1552         if (xflags & BX_VNDIRTY)
 1553                 bv = &bo->bo_dirty;
 1554         else
 1555                 bv = &bo->bo_clean;
 1556 
 1557         root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
 1558         if (root == NULL) {
 1559                 bp->b_left = NULL;
 1560                 bp->b_right = NULL;
 1561                 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
 1562         } else if (bp->b_lblkno < root->b_lblkno ||
 1563             (bp->b_lblkno == root->b_lblkno &&
 1564             (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
 1565                 bp->b_left = root->b_left;
 1566                 bp->b_right = root;
 1567                 root->b_left = NULL;
 1568                 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
 1569         } else {
 1570                 bp->b_right = root->b_right;
 1571                 bp->b_left = root;
 1572                 root->b_right = NULL;
 1573                 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
 1574         }
 1575         bv->bv_cnt++;
 1576         bv->bv_root = bp;
 1577 }
 1578 
 1579 /*
 1580  * Lookup a buffer using the splay tree.  Note that we specifically avoid
 1581  * shadow buffers used in background bitmap writes.
 1582  *
 1583  * This code isn't quite efficient as it could be because we are maintaining
 1584  * two sorted lists and do not know which list the block resides in.
 1585  *
 1586  * During a "make buildworld" the desired buffer is found at one of
 1587  * the roots more than 60% of the time.  Thus, checking both roots
 1588  * before performing either splay eliminates unnecessary splays on the
 1589  * first tree splayed.
 1590  */
 1591 struct buf *
 1592 gbincore(struct bufobj *bo, daddr_t lblkno)
 1593 {
 1594         struct buf *bp;
 1595 
 1596         ASSERT_BO_LOCKED(bo);
 1597         if ((bp = bo->bo_clean.bv_root) != NULL &&
 1598             bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
 1599                 return (bp);
 1600         if ((bp = bo->bo_dirty.bv_root) != NULL &&
 1601             bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
 1602                 return (bp);
 1603         if ((bp = bo->bo_clean.bv_root) != NULL) {
 1604                 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
 1605                 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
 1606                         return (bp);
 1607         }
 1608         if ((bp = bo->bo_dirty.bv_root) != NULL) {
 1609                 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
 1610                 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
 1611                         return (bp);
 1612         }
 1613         return (NULL);
 1614 }
 1615 
 1616 /*
 1617  * Associate a buffer with a vnode.
 1618  */
 1619 void
 1620 bgetvp(struct vnode *vp, struct buf *bp)
 1621 {
 1622         struct bufobj *bo;
 1623 
 1624         bo = &vp->v_bufobj;
 1625         ASSERT_BO_LOCKED(bo);
 1626         VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
 1627 
 1628         CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
 1629         VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
 1630             ("bgetvp: bp already attached! %p", bp));
 1631 
 1632         vhold(vp);
 1633         if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
 1634                 bp->b_flags |= B_NEEDSGIANT;
 1635         bp->b_vp = vp;
 1636         bp->b_bufobj = bo;
 1637         /*
 1638          * Insert onto list for new vnode.
 1639          */
 1640         buf_vlist_add(bp, bo, BX_VNCLEAN);
 1641 }
 1642 
 1643 /*
 1644  * Disassociate a buffer from a vnode.
 1645  */
 1646 void
 1647 brelvp(struct buf *bp)
 1648 {
 1649         struct bufobj *bo;
 1650         struct vnode *vp;
 1651 
 1652         CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
 1653         KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
 1654 
 1655         /*
 1656          * Delete from old vnode list, if on one.
 1657          */
 1658         vp = bp->b_vp;          /* XXX */
 1659         bo = bp->b_bufobj;
 1660         BO_LOCK(bo);
 1661         if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
 1662                 buf_vlist_remove(bp);
 1663         else
 1664                 panic("brelvp: Buffer %p not on queue.", bp);
 1665         if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
 1666                 bo->bo_flag &= ~BO_ONWORKLST;
 1667                 mtx_lock(&sync_mtx);
 1668                 LIST_REMOVE(bo, bo_synclist);
 1669                 syncer_worklist_len--;
 1670                 mtx_unlock(&sync_mtx);
 1671         }
 1672         bp->b_flags &= ~B_NEEDSGIANT;
 1673         bp->b_vp = NULL;
 1674         bp->b_bufobj = NULL;
 1675         BO_UNLOCK(bo);
 1676         vdrop(vp);
 1677 }
 1678 
 1679 /*
 1680  * Add an item to the syncer work queue.
 1681  */
 1682 static void
 1683 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
 1684 {
 1685         int queue, slot;
 1686 
 1687         ASSERT_BO_LOCKED(bo);
 1688 
 1689         mtx_lock(&sync_mtx);
 1690         if (bo->bo_flag & BO_ONWORKLST)
 1691                 LIST_REMOVE(bo, bo_synclist);
 1692         else {
 1693                 bo->bo_flag |= BO_ONWORKLST;
 1694                 syncer_worklist_len++;
 1695         }
 1696 
 1697         if (delay > syncer_maxdelay - 2)
 1698                 delay = syncer_maxdelay - 2;
 1699         slot = (syncer_delayno + delay) & syncer_mask;
 1700 
 1701         queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
 1702             WI_MPSAFEQ;
 1703         LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
 1704             bo_synclist);
 1705         mtx_unlock(&sync_mtx);
 1706 }
 1707 
 1708 static int
 1709 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
 1710 {
 1711         int error, len;
 1712 
 1713         mtx_lock(&sync_mtx);
 1714         len = syncer_worklist_len - sync_vnode_count;
 1715         mtx_unlock(&sync_mtx);
 1716         error = SYSCTL_OUT(req, &len, sizeof(len));
 1717         return (error);
 1718 }
 1719 
 1720 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
 1721     sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
 1722 
 1723 static struct proc *updateproc;
 1724 static void sched_sync(void);
 1725 static struct kproc_desc up_kp = {
 1726         "syncer",
 1727         sched_sync,
 1728         &updateproc
 1729 };
 1730 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
 1731 
 1732 static int
 1733 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
 1734 {
 1735         struct vnode *vp;
 1736         struct mount *mp;
 1737 
 1738         *bo = LIST_FIRST(slp);
 1739         if (*bo == NULL)
 1740                 return (0);
 1741         vp = (*bo)->__bo_vnode; /* XXX */
 1742         if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
 1743                 return (1);
 1744         /*
 1745          * We use vhold in case the vnode does not
 1746          * successfully sync.  vhold prevents the vnode from
 1747          * going away when we unlock the sync_mtx so that
 1748          * we can acquire the vnode interlock.
 1749          */
 1750         vholdl(vp);
 1751         mtx_unlock(&sync_mtx);
 1752         VI_UNLOCK(vp);
 1753         if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
 1754                 vdrop(vp);
 1755                 mtx_lock(&sync_mtx);
 1756                 return (*bo == LIST_FIRST(slp));
 1757         }
 1758         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 1759         (void) VOP_FSYNC(vp, MNT_LAZY, td);
 1760         VOP_UNLOCK(vp, 0);
 1761         vn_finished_write(mp);
 1762         BO_LOCK(*bo);
 1763         if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
 1764                 /*
 1765                  * Put us back on the worklist.  The worklist
 1766                  * routine will remove us from our current
 1767                  * position and then add us back in at a later
 1768                  * position.
 1769                  */
 1770                 vn_syncer_add_to_worklist(*bo, syncdelay);
 1771         }
 1772         BO_UNLOCK(*bo);
 1773         vdrop(vp);
 1774         mtx_lock(&sync_mtx);
 1775         return (0);
 1776 }
 1777 
 1778 /*
 1779  * System filesystem synchronizer daemon.
 1780  */
 1781 static void
 1782 sched_sync(void)
 1783 {
 1784         struct synclist *gnext, *next;
 1785         struct synclist *gslp, *slp;
 1786         struct bufobj *bo;
 1787         long starttime;
 1788         struct thread *td = curthread;
 1789         int last_work_seen;
 1790         int net_worklist_len;
 1791         int syncer_final_iter;
 1792         int first_printf;
 1793         int error;
 1794 
 1795         last_work_seen = 0;
 1796         syncer_final_iter = 0;
 1797         first_printf = 1;
 1798         syncer_state = SYNCER_RUNNING;
 1799         starttime = time_uptime;
 1800         td->td_pflags |= TDP_NORUNNINGBUF;
 1801 
 1802         EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
 1803             SHUTDOWN_PRI_LAST);
 1804 
 1805         mtx_lock(&sync_mtx);
 1806         for (;;) {
 1807                 if (syncer_state == SYNCER_FINAL_DELAY &&
 1808                     syncer_final_iter == 0) {
 1809                         mtx_unlock(&sync_mtx);
 1810                         kproc_suspend_check(td->td_proc);
 1811                         mtx_lock(&sync_mtx);
 1812                 }
 1813                 net_worklist_len = syncer_worklist_len - sync_vnode_count;
 1814                 if (syncer_state != SYNCER_RUNNING &&
 1815                     starttime != time_uptime) {
 1816                         if (first_printf) {
 1817                                 printf("\nSyncing disks, vnodes remaining...");
 1818                                 first_printf = 0;
 1819                         }
 1820                         printf("%d ", net_worklist_len);
 1821                 }
 1822                 starttime = time_uptime;
 1823 
 1824                 /*
 1825                  * Push files whose dirty time has expired.  Be careful
 1826                  * of interrupt race on slp queue.
 1827                  *
 1828                  * Skip over empty worklist slots when shutting down.
 1829                  */
 1830                 do {
 1831                         slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
 1832                         gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
 1833                         syncer_delayno += 1;
 1834                         if (syncer_delayno == syncer_maxdelay)
 1835                                 syncer_delayno = 0;
 1836                         next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
 1837                         gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
 1838                         /*
 1839                          * If the worklist has wrapped since the
 1840                          * it was emptied of all but syncer vnodes,
 1841                          * switch to the FINAL_DELAY state and run
 1842                          * for one more second.
 1843                          */
 1844                         if (syncer_state == SYNCER_SHUTTING_DOWN &&
 1845                             net_worklist_len == 0 &&
 1846                             last_work_seen == syncer_delayno) {
 1847                                 syncer_state = SYNCER_FINAL_DELAY;
 1848                                 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
 1849                         }
 1850                 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
 1851                     LIST_EMPTY(gslp) && syncer_worklist_len > 0);
 1852 
 1853                 /*
 1854                  * Keep track of the last time there was anything
 1855                  * on the worklist other than syncer vnodes.
 1856                  * Return to the SHUTTING_DOWN state if any
 1857                  * new work appears.
 1858                  */
 1859                 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
 1860                         last_work_seen = syncer_delayno;
 1861                 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
 1862                         syncer_state = SYNCER_SHUTTING_DOWN;
 1863                 while (!LIST_EMPTY(slp)) {
 1864                         error = sync_vnode(slp, &bo, td);
 1865                         if (error == 1) {
 1866                                 LIST_REMOVE(bo, bo_synclist);
 1867                                 LIST_INSERT_HEAD(next, bo, bo_synclist);
 1868                                 continue;
 1869                         }
 1870 
 1871                         if (first_printf == 0)
 1872                                 wdog_kern_pat(WD_LASTVAL);
 1873 
 1874                 }
 1875                 if (!LIST_EMPTY(gslp)) {
 1876                         mtx_unlock(&sync_mtx);
 1877                         mtx_lock(&Giant);
 1878                         mtx_lock(&sync_mtx);
 1879                         while (!LIST_EMPTY(gslp)) {
 1880                                 error = sync_vnode(gslp, &bo, td);
 1881                                 if (error == 1) {
 1882                                         LIST_REMOVE(bo, bo_synclist);
 1883                                         LIST_INSERT_HEAD(gnext, bo,
 1884                                             bo_synclist);
 1885                                         continue;
 1886                                 }
 1887                         }
 1888                         mtx_unlock(&Giant);
 1889                 }
 1890                 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
 1891                         syncer_final_iter--;
 1892                 /*
 1893                  * The variable rushjob allows the kernel to speed up the
 1894                  * processing of the filesystem syncer process. A rushjob
 1895                  * value of N tells the filesystem syncer to process the next
 1896                  * N seconds worth of work on its queue ASAP. Currently rushjob
 1897                  * is used by the soft update code to speed up the filesystem
 1898                  * syncer process when the incore state is getting so far
 1899                  * ahead of the disk that the kernel memory pool is being
 1900                  * threatened with exhaustion.
 1901                  */
 1902                 if (rushjob > 0) {
 1903                         rushjob -= 1;
 1904                         continue;
 1905                 }
 1906                 /*
 1907                  * Just sleep for a short period of time between
 1908                  * iterations when shutting down to allow some I/O
 1909                  * to happen.
 1910                  *
 1911                  * If it has taken us less than a second to process the
 1912                  * current work, then wait. Otherwise start right over
 1913                  * again. We can still lose time if any single round
 1914                  * takes more than two seconds, but it does not really
 1915                  * matter as we are just trying to generally pace the
 1916                  * filesystem activity.
 1917                  */
 1918                 if (syncer_state != SYNCER_RUNNING ||
 1919                     time_uptime == starttime) {
 1920                         thread_lock(td);
 1921                         sched_prio(td, PPAUSE);
 1922                         thread_unlock(td);
 1923                 }
 1924                 if (syncer_state != SYNCER_RUNNING)
 1925                         cv_timedwait(&sync_wakeup, &sync_mtx,
 1926                             hz / SYNCER_SHUTDOWN_SPEEDUP);
 1927                 else if (time_uptime == starttime)
 1928                         cv_timedwait(&sync_wakeup, &sync_mtx, hz);
 1929         }
 1930 }
 1931 
 1932 /*
 1933  * Request the syncer daemon to speed up its work.
 1934  * We never push it to speed up more than half of its
 1935  * normal turn time, otherwise it could take over the cpu.
 1936  */
 1937 int
 1938 speedup_syncer(void)
 1939 {
 1940         int ret = 0;
 1941 
 1942         mtx_lock(&sync_mtx);
 1943         if (rushjob < syncdelay / 2) {
 1944                 rushjob += 1;
 1945                 stat_rush_requests += 1;
 1946                 ret = 1;
 1947         }
 1948         mtx_unlock(&sync_mtx);
 1949         cv_broadcast(&sync_wakeup);
 1950         return (ret);
 1951 }
 1952 
 1953 /*
 1954  * Tell the syncer to speed up its work and run though its work
 1955  * list several times, then tell it to shut down.
 1956  */
 1957 static void
 1958 syncer_shutdown(void *arg, int howto)
 1959 {
 1960 
 1961         if (howto & RB_NOSYNC)
 1962                 return;
 1963         mtx_lock(&sync_mtx);
 1964         syncer_state = SYNCER_SHUTTING_DOWN;
 1965         rushjob = 0;
 1966         mtx_unlock(&sync_mtx);
 1967         cv_broadcast(&sync_wakeup);
 1968         kproc_shutdown(arg, howto);
 1969 }
 1970 
 1971 /*
 1972  * Reassign a buffer from one vnode to another.
 1973  * Used to assign file specific control information
 1974  * (indirect blocks) to the vnode to which they belong.
 1975  */
 1976 void
 1977 reassignbuf(struct buf *bp)
 1978 {
 1979         struct vnode *vp;
 1980         struct bufobj *bo;
 1981         int delay;
 1982 #ifdef INVARIANTS
 1983         struct bufv *bv;
 1984 #endif
 1985 
 1986         vp = bp->b_vp;
 1987         bo = bp->b_bufobj;
 1988         ++reassignbufcalls;
 1989 
 1990         CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
 1991             bp, bp->b_vp, bp->b_flags);
 1992         /*
 1993          * B_PAGING flagged buffers cannot be reassigned because their vp
 1994          * is not fully linked in.
 1995          */
 1996         if (bp->b_flags & B_PAGING)
 1997                 panic("cannot reassign paging buffer");
 1998 
 1999         /*
 2000          * Delete from old vnode list, if on one.
 2001          */
 2002         BO_LOCK(bo);
 2003         if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
 2004                 buf_vlist_remove(bp);
 2005         else
 2006                 panic("reassignbuf: Buffer %p not on queue.", bp);
 2007         /*
 2008          * If dirty, put on list of dirty buffers; otherwise insert onto list
 2009          * of clean buffers.
 2010          */
 2011         if (bp->b_flags & B_DELWRI) {
 2012                 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
 2013                         switch (vp->v_type) {
 2014                         case VDIR:
 2015                                 delay = dirdelay;
 2016                                 break;
 2017                         case VCHR:
 2018                                 delay = metadelay;
 2019                                 break;
 2020                         default:
 2021                                 delay = filedelay;
 2022                         }
 2023                         vn_syncer_add_to_worklist(bo, delay);
 2024                 }
 2025                 buf_vlist_add(bp, bo, BX_VNDIRTY);
 2026         } else {
 2027                 buf_vlist_add(bp, bo, BX_VNCLEAN);
 2028 
 2029                 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
 2030                         mtx_lock(&sync_mtx);
 2031                         LIST_REMOVE(bo, bo_synclist);
 2032                         syncer_worklist_len--;
 2033                         mtx_unlock(&sync_mtx);
 2034                         bo->bo_flag &= ~BO_ONWORKLST;
 2035                 }
 2036         }
 2037 #ifdef INVARIANTS
 2038         bv = &bo->bo_clean;
 2039         bp = TAILQ_FIRST(&bv->bv_hd);
 2040         KASSERT(bp == NULL || bp->b_bufobj == bo,
 2041             ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
 2042         bp = TAILQ_LAST(&bv->bv_hd, buflists);
 2043         KASSERT(bp == NULL || bp->b_bufobj == bo,
 2044             ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
 2045         bv = &bo->bo_dirty;
 2046         bp = TAILQ_FIRST(&bv->bv_hd);
 2047         KASSERT(bp == NULL || bp->b_bufobj == bo,
 2048             ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
 2049         bp = TAILQ_LAST(&bv->bv_hd, buflists);
 2050         KASSERT(bp == NULL || bp->b_bufobj == bo,
 2051             ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
 2052 #endif
 2053         BO_UNLOCK(bo);
 2054 }
 2055 
 2056 /*
 2057  * Increment the use and hold counts on the vnode, taking care to reference
 2058  * the driver's usecount if this is a chardev.  The vholdl() will remove
 2059  * the vnode from the free list if it is presently free.  Requires the
 2060  * vnode interlock and returns with it held.
 2061  */
 2062 static void
 2063 v_incr_usecount(struct vnode *vp)
 2064 {
 2065 
 2066         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2067         vp->v_usecount++;
 2068         if (vp->v_type == VCHR && vp->v_rdev != NULL) {
 2069                 dev_lock();
 2070                 vp->v_rdev->si_usecount++;
 2071                 dev_unlock();
 2072         }
 2073         vholdl(vp);
 2074 }
 2075 
 2076 /*
 2077  * Turn a holdcnt into a use+holdcnt such that only one call to
 2078  * v_decr_usecount is needed.
 2079  */
 2080 static void
 2081 v_upgrade_usecount(struct vnode *vp)
 2082 {
 2083 
 2084         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2085         vp->v_usecount++;
 2086         if (vp->v_type == VCHR && vp->v_rdev != NULL) {
 2087                 dev_lock();
 2088                 vp->v_rdev->si_usecount++;
 2089                 dev_unlock();
 2090         }
 2091 }
 2092 
 2093 /*
 2094  * Decrement the vnode use and hold count along with the driver's usecount
 2095  * if this is a chardev.  The vdropl() below releases the vnode interlock
 2096  * as it may free the vnode.
 2097  */
 2098 static void
 2099 v_decr_usecount(struct vnode *vp)
 2100 {
 2101 
 2102         ASSERT_VI_LOCKED(vp, __FUNCTION__);
 2103         VNASSERT(vp->v_usecount > 0, vp,
 2104             ("v_decr_usecount: negative usecount"));
 2105         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2106         vp->v_usecount--;
 2107         if (vp->v_type == VCHR && vp->v_rdev != NULL) {
 2108                 dev_lock();
 2109                 vp->v_rdev->si_usecount--;
 2110                 dev_unlock();
 2111         }
 2112         vdropl(vp);
 2113 }
 2114 
 2115 /*
 2116  * Decrement only the use count and driver use count.  This is intended to
 2117  * be paired with a follow on vdropl() to release the remaining hold count.
 2118  * In this way we may vgone() a vnode with a 0 usecount without risk of
 2119  * having it end up on a free list because the hold count is kept above 0.
 2120  */
 2121 static void
 2122 v_decr_useonly(struct vnode *vp)
 2123 {
 2124 
 2125         ASSERT_VI_LOCKED(vp, __FUNCTION__);
 2126         VNASSERT(vp->v_usecount > 0, vp,
 2127             ("v_decr_useonly: negative usecount"));
 2128         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2129         vp->v_usecount--;
 2130         if (vp->v_type == VCHR && vp->v_rdev != NULL) {
 2131                 dev_lock();
 2132                 vp->v_rdev->si_usecount--;
 2133                 dev_unlock();
 2134         }
 2135 }
 2136 
 2137 /*
 2138  * Grab a particular vnode from the free list, increment its
 2139  * reference count and lock it.  VI_DOOMED is set if the vnode
 2140  * is being destroyed.  Only callers who specify LK_RETRY will
 2141  * see doomed vnodes.  If inactive processing was delayed in
 2142  * vput try to do it here.
 2143  */
 2144 int
 2145 vget(struct vnode *vp, int flags, struct thread *td)
 2146 {
 2147         int error;
 2148 
 2149         error = 0;
 2150         VFS_ASSERT_GIANT(vp->v_mount);
 2151         VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
 2152             ("vget: invalid lock operation"));
 2153         CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
 2154 
 2155         if ((flags & LK_INTERLOCK) == 0)
 2156                 VI_LOCK(vp);
 2157         vholdl(vp);
 2158         if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
 2159                 vdrop(vp);
 2160                 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
 2161                     vp);
 2162                 return (error);
 2163         }
 2164         if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
 2165                 panic("vget: vn_lock failed to return ENOENT\n");
 2166         VI_LOCK(vp);
 2167         /* Upgrade our holdcnt to a usecount. */
 2168         v_upgrade_usecount(vp);
 2169         /*
 2170          * We don't guarantee that any particular close will
 2171          * trigger inactive processing so just make a best effort
 2172          * here at preventing a reference to a removed file.  If
 2173          * we don't succeed no harm is done.
 2174          */
 2175         if (vp->v_iflag & VI_OWEINACT) {
 2176                 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
 2177                     (flags & LK_NOWAIT) == 0)
 2178                         vinactive(vp, td);
 2179                 vp->v_iflag &= ~VI_OWEINACT;
 2180         }
 2181         VI_UNLOCK(vp);
 2182         return (0);
 2183 }
 2184 
 2185 /*
 2186  * Increase the reference count of a vnode.
 2187  */
 2188 void
 2189 vref(struct vnode *vp)
 2190 {
 2191 
 2192         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2193         VI_LOCK(vp);
 2194         v_incr_usecount(vp);
 2195         VI_UNLOCK(vp);
 2196 }
 2197 
 2198 /*
 2199  * Return reference count of a vnode.
 2200  *
 2201  * The results of this call are only guaranteed when some mechanism other
 2202  * than the VI lock is used to stop other processes from gaining references
 2203  * to the vnode.  This may be the case if the caller holds the only reference.
 2204  * This is also useful when stale data is acceptable as race conditions may
 2205  * be accounted for by some other means.
 2206  */
 2207 int
 2208 vrefcnt(struct vnode *vp)
 2209 {
 2210         int usecnt;
 2211 
 2212         VI_LOCK(vp);
 2213         usecnt = vp->v_usecount;
 2214         VI_UNLOCK(vp);
 2215 
 2216         return (usecnt);
 2217 }
 2218 
 2219 #define VPUTX_VRELE     1
 2220 #define VPUTX_VPUT      2
 2221 #define VPUTX_VUNREF    3
 2222 
 2223 static void
 2224 vputx(struct vnode *vp, int func)
 2225 {
 2226         int error;
 2227 
 2228         KASSERT(vp != NULL, ("vputx: null vp"));
 2229         if (func == VPUTX_VUNREF)
 2230                 ASSERT_VOP_LOCKED(vp, "vunref");
 2231         else if (func == VPUTX_VPUT)
 2232                 ASSERT_VOP_LOCKED(vp, "vput");
 2233         else
 2234                 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
 2235         VFS_ASSERT_GIANT(vp->v_mount);
 2236         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2237         VI_LOCK(vp);
 2238 
 2239         /* Skip this v_writecount check if we're going to panic below. */
 2240         VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
 2241             ("vputx: missed vn_close"));
 2242         error = 0;
 2243 
 2244         if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
 2245             vp->v_usecount == 1)) {
 2246                 if (func == VPUTX_VPUT)
 2247                         VOP_UNLOCK(vp, 0);
 2248                 v_decr_usecount(vp);
 2249                 return;
 2250         }
 2251 
 2252         if (vp->v_usecount != 1) {
 2253                 vprint("vputx: negative ref count", vp);
 2254                 panic("vputx: negative ref cnt");
 2255         }
 2256         CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
 2257         /*
 2258          * We want to hold the vnode until the inactive finishes to
 2259          * prevent vgone() races.  We drop the use count here and the
 2260          * hold count below when we're done.
 2261          */
 2262         v_decr_useonly(vp);
 2263         /*
 2264          * We must call VOP_INACTIVE with the node locked. Mark
 2265          * as VI_DOINGINACT to avoid recursion.
 2266          */
 2267         vp->v_iflag |= VI_OWEINACT;
 2268         switch (func) {
 2269         case VPUTX_VRELE:
 2270                 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
 2271                 VI_LOCK(vp);
 2272                 break;
 2273         case VPUTX_VPUT:
 2274                 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
 2275                         error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
 2276                             LK_NOWAIT);
 2277                         VI_LOCK(vp);
 2278                 }
 2279                 break;
 2280         case VPUTX_VUNREF:
 2281                 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
 2282                         error = EBUSY;
 2283                 break;
 2284         }
 2285         if (vp->v_usecount > 0)
 2286                 vp->v_iflag &= ~VI_OWEINACT;
 2287         if (error == 0) {
 2288                 if (vp->v_iflag & VI_OWEINACT)
 2289                         vinactive(vp, curthread);
 2290                 if (func != VPUTX_VUNREF)
 2291                         VOP_UNLOCK(vp, 0);
 2292         }
 2293         vdropl(vp);
 2294 }
 2295 
 2296 /*
 2297  * Vnode put/release.
 2298  * If count drops to zero, call inactive routine and return to freelist.
 2299  */
 2300 void
 2301 vrele(struct vnode *vp)
 2302 {
 2303 
 2304         vputx(vp, VPUTX_VRELE);
 2305 }
 2306 
 2307 /*
 2308  * Release an already locked vnode.  This give the same effects as
 2309  * unlock+vrele(), but takes less time and avoids releasing and
 2310  * re-aquiring the lock (as vrele() acquires the lock internally.)
 2311  */
 2312 void
 2313 vput(struct vnode *vp)
 2314 {
 2315 
 2316         vputx(vp, VPUTX_VPUT);
 2317 }
 2318 
 2319 /*
 2320  * Release an exclusively locked vnode. Do not unlock the vnode lock.
 2321  */
 2322 void
 2323 vunref(struct vnode *vp)
 2324 {
 2325 
 2326         vputx(vp, VPUTX_VUNREF);
 2327 }
 2328 
 2329 /*
 2330  * Somebody doesn't want the vnode recycled.
 2331  */
 2332 void
 2333 vhold(struct vnode *vp)
 2334 {
 2335 
 2336         VI_LOCK(vp);
 2337         vholdl(vp);
 2338         VI_UNLOCK(vp);
 2339 }
 2340 
 2341 /*
 2342  * Increase the hold count and activate if this is the first reference.
 2343  */
 2344 void
 2345 vholdl(struct vnode *vp)
 2346 {
 2347         struct mount *mp;
 2348 
 2349         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2350         vp->v_holdcnt++;
 2351         if (!VSHOULDBUSY(vp))
 2352                 return;
 2353         ASSERT_VI_LOCKED(vp, "vholdl");
 2354         VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
 2355         VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
 2356         /*
 2357          * Remove a vnode from the free list, mark it as in use,
 2358          * and put it on the active list.
 2359          */
 2360         mtx_lock(&vnode_free_list_mtx);
 2361         TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
 2362         freevnodes--;
 2363         vp->v_iflag &= ~(VI_FREE|VI_AGE);
 2364         KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
 2365             ("Activating already active vnode"));
 2366         vp->v_iflag |= VI_ACTIVE;
 2367         mp = vp->v_mount;
 2368         TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
 2369         mp->mnt_activevnodelistsize++;
 2370         mtx_unlock(&vnode_free_list_mtx);
 2371 }
 2372 
 2373 /*
 2374  * Note that there is one less who cares about this vnode.
 2375  * vdrop() is the opposite of vhold().
 2376  */
 2377 void
 2378 vdrop(struct vnode *vp)
 2379 {
 2380 
 2381         VI_LOCK(vp);
 2382         vdropl(vp);
 2383 }
 2384 
 2385 /*
 2386  * Drop the hold count of the vnode.  If this is the last reference to
 2387  * the vnode we place it on the free list unless it has been vgone'd
 2388  * (marked VI_DOOMED) in which case we will free it.
 2389  */
 2390 void
 2391 vdropl(struct vnode *vp)
 2392 {
 2393         struct bufobj *bo;
 2394         struct mount *mp;
 2395         int active;
 2396 
 2397         ASSERT_VI_LOCKED(vp, "vdropl");
 2398         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2399         if (vp->v_holdcnt <= 0)
 2400                 panic("vdrop: holdcnt %d", vp->v_holdcnt);
 2401         vp->v_holdcnt--;
 2402         if (vp->v_holdcnt > 0) {
 2403                 VI_UNLOCK(vp);
 2404                 return;
 2405         }
 2406         if ((vp->v_iflag & VI_DOOMED) == 0) {
 2407                 /*
 2408                  * Mark a vnode as free: remove it from its active list
 2409                  * and put it up for recycling on the freelist.
 2410                  */
 2411                 VNASSERT(vp->v_op != NULL, vp,
 2412                     ("vdropl: vnode already reclaimed."));
 2413                 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
 2414                     ("vnode already free"));
 2415                 VNASSERT(VSHOULDFREE(vp), vp,
 2416                     ("vdropl: freeing when we shouldn't"));
 2417                 active = vp->v_iflag & VI_ACTIVE;
 2418                 vp->v_iflag &= ~VI_ACTIVE;
 2419                 mp = vp->v_mount;
 2420                 mtx_lock(&vnode_free_list_mtx);
 2421                 if (active) {
 2422                         TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
 2423                             v_actfreelist);
 2424                         mp->mnt_activevnodelistsize--;
 2425                 }
 2426                 if (vp->v_iflag & VI_AGE) {
 2427                         TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
 2428                 } else {
 2429                         TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
 2430                 }
 2431                 freevnodes++;
 2432                 vp->v_iflag &= ~VI_AGE;
 2433                 vp->v_iflag |= VI_FREE;
 2434                 mtx_unlock(&vnode_free_list_mtx);
 2435                 VI_UNLOCK(vp);
 2436                 return;
 2437         }
 2438         /*
 2439          * The vnode has been marked for destruction, so free it.
 2440          */
 2441         CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
 2442         mtx_lock(&vnode_free_list_mtx);
 2443         numvnodes--;
 2444         mtx_unlock(&vnode_free_list_mtx);
 2445         bo = &vp->v_bufobj;
 2446         VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
 2447             ("cleaned vnode still on the free list."));
 2448         VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
 2449         VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
 2450         VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
 2451         VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
 2452         VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
 2453         VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
 2454         VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
 2455         VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
 2456         VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
 2457         VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
 2458         VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
 2459         VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
 2460         VI_UNLOCK(vp);
 2461 #ifdef MAC
 2462         mac_vnode_destroy(vp);
 2463 #endif
 2464         if (vp->v_pollinfo != NULL)
 2465                 destroy_vpollinfo(vp->v_pollinfo);
 2466 #ifdef INVARIANTS
 2467         /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
 2468         vp->v_op = NULL;
 2469 #endif
 2470         lockdestroy(vp->v_vnlock);
 2471         mtx_destroy(&vp->v_interlock);
 2472         mtx_destroy(BO_MTX(bo));
 2473         uma_zfree(vnode_zone, vp);
 2474 }
 2475 
 2476 /*
 2477  * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
 2478  * flags.  DOINGINACT prevents us from recursing in calls to vinactive.
 2479  * OWEINACT tracks whether a vnode missed a call to inactive due to a
 2480  * failed lock upgrade.
 2481  */
 2482 void
 2483 vinactive(struct vnode *vp, struct thread *td)
 2484 {
 2485         struct vm_object *obj;
 2486 
 2487         ASSERT_VOP_ELOCKED(vp, "vinactive");
 2488         ASSERT_VI_LOCKED(vp, "vinactive");
 2489         VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
 2490             ("vinactive: recursed on VI_DOINGINACT"));
 2491         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2492         vp->v_iflag |= VI_DOINGINACT;
 2493         vp->v_iflag &= ~VI_OWEINACT;
 2494         VI_UNLOCK(vp);
 2495         /*
 2496          * Before moving off the active list, we must be sure that any
 2497          * modified pages are on the vnode's dirty list since these will
 2498          * no longer be checked once the vnode is on the inactive list.
 2499          */
 2500         obj = vp->v_object;
 2501         if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
 2502                 VM_OBJECT_LOCK(obj);
 2503                 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
 2504                 VM_OBJECT_UNLOCK(obj);
 2505         }
 2506         VOP_INACTIVE(vp, td);
 2507         VI_LOCK(vp);
 2508         VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
 2509             ("vinactive: lost VI_DOINGINACT"));
 2510         vp->v_iflag &= ~VI_DOINGINACT;
 2511 }
 2512 
 2513 /*
 2514  * Remove any vnodes in the vnode table belonging to mount point mp.
 2515  *
 2516  * If FORCECLOSE is not specified, there should not be any active ones,
 2517  * return error if any are found (nb: this is a user error, not a
 2518  * system error). If FORCECLOSE is specified, detach any active vnodes
 2519  * that are found.
 2520  *
 2521  * If WRITECLOSE is set, only flush out regular file vnodes open for
 2522  * writing.
 2523  *
 2524  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
 2525  *
 2526  * `rootrefs' specifies the base reference count for the root vnode
 2527  * of this filesystem. The root vnode is considered busy if its
 2528  * v_usecount exceeds this value. On a successful return, vflush(, td)
 2529  * will call vrele() on the root vnode exactly rootrefs times.
 2530  * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
 2531  * be zero.
 2532  */
 2533 #ifdef DIAGNOSTIC
 2534 static int busyprt = 0;         /* print out busy vnodes */
 2535 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
 2536 #endif
 2537 
 2538 int
 2539 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
 2540 {
 2541         struct vnode *vp, *mvp, *rootvp = NULL;
 2542         struct vattr vattr;
 2543         int busy = 0, error;
 2544 
 2545         CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
 2546             rootrefs, flags);
 2547         if (rootrefs > 0) {
 2548                 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
 2549                     ("vflush: bad args"));
 2550                 /*
 2551                  * Get the filesystem root vnode. We can vput() it
 2552                  * immediately, since with rootrefs > 0, it won't go away.
 2553                  */
 2554                 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
 2555                         CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
 2556                             __func__, error);
 2557                         return (error);
 2558                 }
 2559                 vput(rootvp);
 2560         }
 2561 loop:
 2562         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
 2563                 vholdl(vp);
 2564                 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
 2565                 if (error) {
 2566                         vdrop(vp);
 2567                         MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 2568                         goto loop;
 2569                 }
 2570                 /*
 2571                  * Skip over a vnodes marked VV_SYSTEM.
 2572                  */
 2573                 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
 2574                         VOP_UNLOCK(vp, 0);
 2575                         vdrop(vp);
 2576                         continue;
 2577                 }
 2578                 /*
 2579                  * If WRITECLOSE is set, flush out unlinked but still open
 2580                  * files (even if open only for reading) and regular file
 2581                  * vnodes open for writing.
 2582                  */
 2583                 if (flags & WRITECLOSE) {
 2584                         if (vp->v_object != NULL) {
 2585                                 VM_OBJECT_LOCK(vp->v_object);
 2586                                 vm_object_page_clean(vp->v_object, 0, 0, 0);
 2587                                 VM_OBJECT_UNLOCK(vp->v_object);
 2588                         }
 2589                         error = VOP_FSYNC(vp, MNT_WAIT, td);
 2590                         if (error != 0) {
 2591                                 VOP_UNLOCK(vp, 0);
 2592                                 vdrop(vp);
 2593                                 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
 2594                                 return (error);
 2595                         }
 2596                         error = VOP_GETATTR(vp, &vattr, td->td_ucred);
 2597                         VI_LOCK(vp);
 2598 
 2599                         if ((vp->v_type == VNON ||
 2600                             (error == 0 && vattr.va_nlink > 0)) &&
 2601                             (vp->v_writecount == 0 || vp->v_type != VREG)) {
 2602                                 VOP_UNLOCK(vp, 0);
 2603                                 vdropl(vp);
 2604                                 continue;
 2605                         }
 2606                 } else
 2607                         VI_LOCK(vp);
 2608                 /*
 2609                  * With v_usecount == 0, all we need to do is clear out the
 2610                  * vnode data structures and we are done.
 2611                  *
 2612                  * If FORCECLOSE is set, forcibly close the vnode.
 2613                  */
 2614                 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
 2615                         VNASSERT(vp->v_usecount == 0 ||
 2616                             (vp->v_type != VCHR && vp->v_type != VBLK), vp,
 2617                             ("device VNODE %p is FORCECLOSED", vp));
 2618                         vgonel(vp);
 2619                 } else {
 2620                         busy++;
 2621 #ifdef DIAGNOSTIC
 2622                         if (busyprt)
 2623                                 vprint("vflush: busy vnode", vp);
 2624 #endif
 2625                 }
 2626                 VOP_UNLOCK(vp, 0);
 2627                 vdropl(vp);
 2628         }
 2629         if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
 2630                 /*
 2631                  * If just the root vnode is busy, and if its refcount
 2632                  * is equal to `rootrefs', then go ahead and kill it.
 2633                  */
 2634                 VI_LOCK(rootvp);
 2635                 KASSERT(busy > 0, ("vflush: not busy"));
 2636                 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
 2637                     ("vflush: usecount %d < rootrefs %d",
 2638                      rootvp->v_usecount, rootrefs));
 2639                 if (busy == 1 && rootvp->v_usecount == rootrefs) {
 2640                         VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
 2641                         vgone(rootvp);
 2642                         VOP_UNLOCK(rootvp, 0);
 2643                         busy = 0;
 2644                 } else
 2645                         VI_UNLOCK(rootvp);
 2646         }
 2647         if (busy) {
 2648                 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
 2649                     busy);
 2650                 return (EBUSY);
 2651         }
 2652         for (; rootrefs > 0; rootrefs--)
 2653                 vrele(rootvp);
 2654         return (0);
 2655 }
 2656 
 2657 /*
 2658  * Recycle an unused vnode to the front of the free list.
 2659  */
 2660 int
 2661 vrecycle(struct vnode *vp, struct thread *td)
 2662 {
 2663         int recycled;
 2664 
 2665         ASSERT_VOP_ELOCKED(vp, "vrecycle");
 2666         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2667         recycled = 0;
 2668         VI_LOCK(vp);
 2669         if (vp->v_usecount == 0) {
 2670                 recycled = 1;
 2671                 vgonel(vp);
 2672         }
 2673         VI_UNLOCK(vp);
 2674         return (recycled);
 2675 }
 2676 
 2677 /*
 2678  * Eliminate all activity associated with a vnode
 2679  * in preparation for reuse.
 2680  */
 2681 void
 2682 vgone(struct vnode *vp)
 2683 {
 2684         VI_LOCK(vp);
 2685         vgonel(vp);
 2686         VI_UNLOCK(vp);
 2687 }
 2688 
 2689 /*
 2690  * vgone, with the vp interlock held.
 2691  */
 2692 void
 2693 vgonel(struct vnode *vp)
 2694 {
 2695         struct thread *td;
 2696         int oweinact;
 2697         int active;
 2698         struct mount *mp;
 2699 
 2700         ASSERT_VOP_ELOCKED(vp, "vgonel");
 2701         ASSERT_VI_LOCKED(vp, "vgonel");
 2702         VNASSERT(vp->v_holdcnt, vp,
 2703             ("vgonel: vp %p has no reference.", vp));
 2704         CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
 2705         td = curthread;
 2706 
 2707         /*
 2708          * Don't vgonel if we're already doomed.
 2709          */
 2710         if (vp->v_iflag & VI_DOOMED)
 2711                 return;
 2712         vp->v_iflag |= VI_DOOMED;
 2713         /*
 2714          * Check to see if the vnode is in use.  If so, we have to call
 2715          * VOP_CLOSE() and VOP_INACTIVE().
 2716          */
 2717         active = vp->v_usecount;
 2718         oweinact = (vp->v_iflag & VI_OWEINACT);
 2719         VI_UNLOCK(vp);
 2720         /*
 2721          * Clean out any buffers associated with the vnode.
 2722          * If the flush fails, just toss the buffers.
 2723          */
 2724         mp = NULL;
 2725         if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
 2726                 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
 2727         if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
 2728                 vinvalbuf(vp, 0, 0, 0);
 2729 
 2730         /*
 2731          * If purging an active vnode, it must be closed and
 2732          * deactivated before being reclaimed.
 2733          */
 2734         if (active)
 2735                 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
 2736         if (oweinact || active) {
 2737                 VI_LOCK(vp);
 2738                 if ((vp->v_iflag & VI_DOINGINACT) == 0)
 2739                         vinactive(vp, td);
 2740                 VI_UNLOCK(vp);
 2741         }
 2742         if (vp->v_type == VSOCK)
 2743                 vfs_unp_reclaim(vp);
 2744         /*
 2745          * Reclaim the vnode.
 2746          */
 2747         if (VOP_RECLAIM(vp, td))
 2748                 panic("vgone: cannot reclaim");
 2749         if (mp != NULL)
 2750                 vn_finished_secondary_write(mp);
 2751         VNASSERT(vp->v_object == NULL, vp,
 2752             ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
 2753         /*
 2754          * Clear the advisory locks and wake up waiting threads.
 2755          */
 2756         (void)VOP_ADVLOCKPURGE(vp);
 2757         /*
 2758          * Delete from old mount point vnode list.
 2759          */
 2760         delmntque(vp);
 2761         cache_purge(vp);
 2762         /*
 2763          * Done with purge, reset to the standard lock and invalidate
 2764          * the vnode.
 2765          */
 2766         VI_LOCK(vp);
 2767         vp->v_vnlock = &vp->v_lock;
 2768         vp->v_op = &dead_vnodeops;
 2769         vp->v_tag = "none";
 2770         vp->v_type = VBAD;
 2771 }
 2772 
 2773 /*
 2774  * Calculate the total number of references to a special device.
 2775  */
 2776 int
 2777 vcount(struct vnode *vp)
 2778 {
 2779         int count;
 2780 
 2781         dev_lock();
 2782         count = vp->v_rdev->si_usecount;
 2783         dev_unlock();
 2784         return (count);
 2785 }
 2786 
 2787 /*
 2788  * Same as above, but using the struct cdev *as argument
 2789  */
 2790 int
 2791 count_dev(struct cdev *dev)
 2792 {
 2793         int count;
 2794 
 2795         dev_lock();
 2796         count = dev->si_usecount;
 2797         dev_unlock();
 2798         return(count);
 2799 }
 2800 
 2801 /*
 2802  * Print out a description of a vnode.
 2803  */
 2804 static char *typename[] =
 2805 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
 2806  "VMARKER"};
 2807 
 2808 void
 2809 vn_printf(struct vnode *vp, const char *fmt, ...)
 2810 {
 2811         va_list ap;
 2812         char buf[256], buf2[16];
 2813         u_long flags;
 2814 
 2815         va_start(ap, fmt);
 2816         vprintf(fmt, ap);
 2817         va_end(ap);
 2818         printf("%p: ", (void *)vp);
 2819         printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
 2820         printf("    usecount %d, writecount %d, refcount %d mountedhere %p\n",
 2821             vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
 2822         buf[0] = '\0';
 2823         buf[1] = '\0';
 2824         if (vp->v_vflag & VV_ROOT)
 2825                 strlcat(buf, "|VV_ROOT", sizeof(buf));
 2826         if (vp->v_vflag & VV_ISTTY)
 2827                 strlcat(buf, "|VV_ISTTY", sizeof(buf));
 2828         if (vp->v_vflag & VV_NOSYNC)
 2829                 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
 2830         if (vp->v_vflag & VV_CACHEDLABEL)
 2831                 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
 2832         if (vp->v_vflag & VV_TEXT)
 2833                 strlcat(buf, "|VV_TEXT", sizeof(buf));
 2834         if (vp->v_vflag & VV_COPYONWRITE)
 2835                 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
 2836         if (vp->v_vflag & VV_SYSTEM)
 2837                 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
 2838         if (vp->v_vflag & VV_PROCDEP)
 2839                 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
 2840         if (vp->v_vflag & VV_NOKNOTE)
 2841                 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
 2842         if (vp->v_vflag & VV_DELETED)
 2843                 strlcat(buf, "|VV_DELETED", sizeof(buf));
 2844         if (vp->v_vflag & VV_MD)
 2845                 strlcat(buf, "|VV_MD", sizeof(buf));
 2846         flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
 2847             VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
 2848             VV_NOKNOTE | VV_DELETED | VV_MD);
 2849         if (flags != 0) {
 2850                 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
 2851                 strlcat(buf, buf2, sizeof(buf));
 2852         }
 2853         if (vp->v_iflag & VI_MOUNT)
 2854                 strlcat(buf, "|VI_MOUNT", sizeof(buf));
 2855         if (vp->v_iflag & VI_AGE)
 2856                 strlcat(buf, "|VI_AGE", sizeof(buf));
 2857         if (vp->v_iflag & VI_DOOMED)
 2858                 strlcat(buf, "|VI_DOOMED", sizeof(buf));
 2859         if (vp->v_iflag & VI_FREE)
 2860                 strlcat(buf, "|VI_FREE", sizeof(buf));
 2861         if (vp->v_iflag & VI_DOINGINACT)
 2862                 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
 2863         if (vp->v_iflag & VI_OWEINACT)
 2864                 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
 2865         flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
 2866             VI_DOINGINACT | VI_OWEINACT);
 2867         if (flags != 0) {
 2868                 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
 2869                 strlcat(buf, buf2, sizeof(buf));
 2870         }
 2871         printf("    flags (%s)\n", buf + 1);
 2872         if (mtx_owned(VI_MTX(vp)))
 2873                 printf(" VI_LOCKed");
 2874         if (vp->v_object != NULL)
 2875                 printf("    v_object %p ref %d pages %d\n",
 2876                     vp->v_object, vp->v_object->ref_count,
 2877                     vp->v_object->resident_page_count);
 2878         printf("    ");
 2879         lockmgr_printinfo(vp->v_vnlock);
 2880         if (vp->v_data != NULL)
 2881                 VOP_PRINT(vp);
 2882 }
 2883 
 2884 #ifdef DDB
 2885 /*
 2886  * List all of the locked vnodes in the system.
 2887  * Called when debugging the kernel.
 2888  */
 2889 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
 2890 {
 2891         struct mount *mp, *nmp;
 2892         struct vnode *vp;
 2893 
 2894         /*
 2895          * Note: because this is DDB, we can't obey the locking semantics
 2896          * for these structures, which means we could catch an inconsistent
 2897          * state and dereference a nasty pointer.  Not much to be done
 2898          * about that.
 2899          */
 2900         db_printf("Locked vnodes\n");
 2901         for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
 2902                 nmp = TAILQ_NEXT(mp, mnt_list);
 2903                 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
 2904                         if (vp->v_type != VMARKER &&
 2905                             VOP_ISLOCKED(vp))
 2906                                 vprint("", vp);
 2907                 }
 2908                 nmp = TAILQ_NEXT(mp, mnt_list);
 2909         }
 2910 }
 2911 
 2912 /*
 2913  * Show details about the given vnode.
 2914  */
 2915 DB_SHOW_COMMAND(vnode, db_show_vnode)
 2916 {
 2917         struct vnode *vp;
 2918 
 2919         if (!have_addr)
 2920                 return;
 2921         vp = (struct vnode *)addr;
 2922         vn_printf(vp, "vnode ");
 2923 }
 2924 
 2925 /*
 2926  * Show details about the given mount point.
 2927  */
 2928 DB_SHOW_COMMAND(mount, db_show_mount)
 2929 {
 2930         struct mount *mp;
 2931         struct vfsopt *opt;
 2932         struct statfs *sp;
 2933         struct vnode *vp;
 2934         char buf[512];
 2935         uint64_t mflags;
 2936         u_int flags;
 2937 
 2938         if (!have_addr) {
 2939                 /* No address given, print short info about all mount points. */
 2940                 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
 2941                         db_printf("%p %s on %s (%s)\n", mp,
 2942                             mp->mnt_stat.f_mntfromname,
 2943                             mp->mnt_stat.f_mntonname,
 2944                             mp->mnt_stat.f_fstypename);
 2945                         if (db_pager_quit)
 2946                                 break;
 2947                 }
 2948                 db_printf("\nMore info: show mount <addr>\n");
 2949                 return;
 2950         }
 2951 
 2952         mp = (struct mount *)addr;
 2953         db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
 2954             mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
 2955 
 2956         buf[0] = '\0';
 2957         mflags = mp->mnt_flag;
 2958 #define MNT_FLAG(flag)  do {                                            \
 2959         if (mflags & (flag)) {                                          \
 2960                 if (buf[0] != '\0')                                     \
 2961                         strlcat(buf, ", ", sizeof(buf));                \
 2962                 strlcat(buf, (#flag) + 4, sizeof(buf));                 \
 2963                 mflags &= ~(flag);                                      \
 2964         }                                                               \
 2965 } while (0)
 2966         MNT_FLAG(MNT_RDONLY);
 2967         MNT_FLAG(MNT_SYNCHRONOUS);
 2968         MNT_FLAG(MNT_NOEXEC);
 2969         MNT_FLAG(MNT_NOSUID);
 2970         MNT_FLAG(MNT_UNION);
 2971         MNT_FLAG(MNT_ASYNC);
 2972         MNT_FLAG(MNT_SUIDDIR);
 2973         MNT_FLAG(MNT_SOFTDEP);
 2974         MNT_FLAG(MNT_SUJ);
 2975         MNT_FLAG(MNT_NOSYMFOLLOW);
 2976         MNT_FLAG(MNT_GJOURNAL);
 2977         MNT_FLAG(MNT_MULTILABEL);
 2978         MNT_FLAG(MNT_ACLS);
 2979         MNT_FLAG(MNT_NOATIME);
 2980         MNT_FLAG(MNT_NOCLUSTERR);
 2981         MNT_FLAG(MNT_NOCLUSTERW);
 2982         MNT_FLAG(MNT_NFS4ACLS);
 2983         MNT_FLAG(MNT_EXRDONLY);
 2984         MNT_FLAG(MNT_EXPORTED);
 2985         MNT_FLAG(MNT_DEFEXPORTED);
 2986         MNT_FLAG(MNT_EXPORTANON);
 2987         MNT_FLAG(MNT_EXKERB);
 2988         MNT_FLAG(MNT_EXPUBLIC);
 2989         MNT_FLAG(MNT_LOCAL);
 2990         MNT_FLAG(MNT_QUOTA);
 2991         MNT_FLAG(MNT_ROOTFS);
 2992         MNT_FLAG(MNT_USER);
 2993         MNT_FLAG(MNT_IGNORE);
 2994         MNT_FLAG(MNT_UPDATE);
 2995         MNT_FLAG(MNT_DELEXPORT);
 2996         MNT_FLAG(MNT_RELOAD);
 2997         MNT_FLAG(MNT_FORCE);
 2998         MNT_FLAG(MNT_SNAPSHOT);
 2999         MNT_FLAG(MNT_BYFSID);
 3000 #undef MNT_FLAG
 3001         if (mflags != 0) {
 3002                 if (buf[0] != '\0')
 3003                         strlcat(buf, ", ", sizeof(buf));
 3004                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 3005                     "0x%016jx", mflags);
 3006         }
 3007         db_printf("    mnt_flag = %s\n", buf);
 3008 
 3009         buf[0] = '\0';
 3010         flags = mp->mnt_kern_flag;
 3011 #define MNT_KERN_FLAG(flag)     do {                                    \
 3012         if (flags & (flag)) {                                           \
 3013                 if (buf[0] != '\0')                                     \
 3014                         strlcat(buf, ", ", sizeof(buf));                \
 3015                 strlcat(buf, (#flag) + 5, sizeof(buf));                 \
 3016                 flags &= ~(flag);                                       \
 3017         }                                                               \
 3018 } while (0)
 3019         MNT_KERN_FLAG(MNTK_UNMOUNTF);
 3020         MNT_KERN_FLAG(MNTK_ASYNC);
 3021         MNT_KERN_FLAG(MNTK_SOFTDEP);
 3022         MNT_KERN_FLAG(MNTK_NOINSMNTQ);
 3023         MNT_KERN_FLAG(MNTK_DRAINING);
 3024         MNT_KERN_FLAG(MNTK_REFEXPIRE);
 3025         MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
 3026         MNT_KERN_FLAG(MNTK_SHARED_WRITES);
 3027         MNT_KERN_FLAG(MNTK_NOASYNC);
 3028         MNT_KERN_FLAG(MNTK_UNMOUNT);
 3029         MNT_KERN_FLAG(MNTK_MWAIT);
 3030         MNT_KERN_FLAG(MNTK_SUSPEND);
 3031         MNT_KERN_FLAG(MNTK_SUSPEND2);
 3032         MNT_KERN_FLAG(MNTK_SUSPENDED);
 3033         MNT_KERN_FLAG(MNTK_MPSAFE);
 3034         MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
 3035         MNT_KERN_FLAG(MNTK_NOKNOTE);
 3036 #undef MNT_KERN_FLAG
 3037         if (flags != 0) {
 3038                 if (buf[0] != '\0')
 3039                         strlcat(buf, ", ", sizeof(buf));
 3040                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
 3041                     "0x%08x", flags);
 3042         }
 3043         db_printf("    mnt_kern_flag = %s\n", buf);
 3044 
 3045         db_printf("    mnt_opt = ");
 3046         opt = TAILQ_FIRST(mp->mnt_opt);
 3047         if (opt != NULL) {
 3048                 db_printf("%s", opt->name);
 3049                 opt = TAILQ_NEXT(opt, link);
 3050                 while (opt != NULL) {
 3051                         db_printf(", %s", opt->name);
 3052                         opt = TAILQ_NEXT(opt, link);
 3053                 }
 3054         }
 3055         db_printf("\n");
 3056 
 3057         sp = &mp->mnt_stat;
 3058         db_printf("    mnt_stat = { version=%u type=%u flags=0x%016jx "
 3059             "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
 3060             "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
 3061             "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
 3062             (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
 3063             (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
 3064             (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
 3065             (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
 3066             (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
 3067             (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
 3068             (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
 3069             (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
 3070 
 3071         db_printf("    mnt_cred = { uid=%u ruid=%u",
 3072             (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
 3073         if (jailed(mp->mnt_cred))
 3074                 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
 3075         db_printf(" }\n");
 3076         db_printf("    mnt_ref = %d\n", mp->mnt_ref);
 3077         db_printf("    mnt_gen = %d\n", mp->mnt_gen);
 3078         db_printf("    mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
 3079         db_printf("    mnt_activevnodelistsize = %d\n",
 3080             mp->mnt_activevnodelistsize);
 3081         db_printf("    mnt_writeopcount = %d\n", mp->mnt_writeopcount);
 3082         db_printf("    mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
 3083         db_printf("    mnt_iosize_max = %d\n", mp->mnt_iosize_max);
 3084         db_printf("    mnt_hashseed = %u\n", mp->mnt_hashseed);
 3085         db_printf("    mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
 3086         db_printf("    mnt_secondary_accwrites = %d\n",
 3087             mp->mnt_secondary_accwrites);
 3088         db_printf("    mnt_gjprovider = %s\n",
 3089             mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
 3090 
 3091         db_printf("\n\nList of active vnodes\n");
 3092         TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
 3093                 if (vp->v_type != VMARKER) {
 3094                         vn_printf(vp, "vnode ");
 3095                         if (db_pager_quit)
 3096                                 break;
 3097                 }
 3098         }
 3099         db_printf("\n\nList of inactive vnodes\n");
 3100         TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
 3101                 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
 3102                         vn_printf(vp, "vnode ");
 3103                         if (db_pager_quit)
 3104                                 break;
 3105                 }
 3106         }
 3107 }
 3108 #endif  /* DDB */
 3109 
 3110 /*
 3111  * Fill in a struct xvfsconf based on a struct vfsconf.
 3112  */
 3113 static void
 3114 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
 3115 {
 3116 
 3117         strcpy(xvfsp->vfc_name, vfsp->vfc_name);
 3118         xvfsp->vfc_typenum = vfsp->vfc_typenum;
 3119         xvfsp->vfc_refcount = vfsp->vfc_refcount;
 3120         xvfsp->vfc_flags = vfsp->vfc_flags;
 3121         /*
 3122          * These are unused in userland, we keep them
 3123          * to not break binary compatibility.
 3124          */
 3125         xvfsp->vfc_vfsops = NULL;
 3126         xvfsp->vfc_next = NULL;
 3127 }
 3128 
 3129 /*
 3130  * Top level filesystem related information gathering.
 3131  */
 3132 static int
 3133 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
 3134 {
 3135         struct vfsconf *vfsp;
 3136         struct xvfsconf xvfsp;
 3137         int error;
 3138 
 3139         error = 0;
 3140         TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
 3141                 bzero(&xvfsp, sizeof(xvfsp));
 3142                 vfsconf2x(vfsp, &xvfsp);
 3143                 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
 3144                 if (error)
 3145                         break;
 3146         }
 3147         return (error);
 3148 }
 3149 
 3150 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD,
 3151     NULL, 0, sysctl_vfs_conflist,
 3152     "S,xvfsconf", "List of all configured filesystems");
 3153 
 3154 #ifndef BURN_BRIDGES
 3155 static int      sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
 3156 
 3157 static int
 3158 vfs_sysctl(SYSCTL_HANDLER_ARGS)
 3159 {
 3160         int *name = (int *)arg1 - 1;    /* XXX */
 3161         u_int namelen = arg2 + 1;       /* XXX */
 3162         struct vfsconf *vfsp;
 3163         struct xvfsconf xvfsp;
 3164 
 3165         printf("WARNING: userland calling deprecated sysctl, "
 3166             "please rebuild world\n");
 3167 
 3168 #if 1 || defined(COMPAT_PRELITE2)
 3169         /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
 3170         if (namelen == 1)
 3171                 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
 3172 #endif
 3173 
 3174         switch (name[1]) {
 3175         case VFS_MAXTYPENUM:
 3176                 if (namelen != 2)
 3177                         return (ENOTDIR);
 3178                 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
 3179         case VFS_CONF:
 3180                 if (namelen != 3)
 3181                         return (ENOTDIR);       /* overloaded */
 3182                 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
 3183                         if (vfsp->vfc_typenum == name[2])
 3184                                 break;
 3185                 if (vfsp == NULL)
 3186                         return (EOPNOTSUPP);
 3187                 bzero(&xvfsp, sizeof(xvfsp));
 3188                 vfsconf2x(vfsp, &xvfsp);
 3189                 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
 3190         }
 3191         return (EOPNOTSUPP);
 3192 }
 3193 
 3194 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
 3195     vfs_sysctl, "Generic filesystem");
 3196 
 3197 #if 1 || defined(COMPAT_PRELITE2)
 3198 
 3199 static int
 3200 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
 3201 {
 3202         int error;
 3203         struct vfsconf *vfsp;
 3204         struct ovfsconf ovfs;
 3205 
 3206         TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
 3207                 bzero(&ovfs, sizeof(ovfs));
 3208                 ovfs.vfc_vfsops = vfsp->vfc_vfsops;     /* XXX used as flag */
 3209                 strcpy(ovfs.vfc_name, vfsp->vfc_name);
 3210                 ovfs.vfc_index = vfsp->vfc_typenum;
 3211                 ovfs.vfc_refcount = vfsp->vfc_refcount;
 3212                 ovfs.vfc_flags = vfsp->vfc_flags;
 3213                 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
 3214                 if (error)
 3215                         return error;
 3216         }
 3217         return 0;
 3218 }
 3219 
 3220 #endif /* 1 || COMPAT_PRELITE2 */
 3221 #endif /* !BURN_BRIDGES */
 3222 
 3223 #define KINFO_VNODESLOP         10
 3224 #ifdef notyet
 3225 /*
 3226  * Dump vnode list (via sysctl).
 3227  */
 3228 /* ARGSUSED */
 3229 static int
 3230 sysctl_vnode(SYSCTL_HANDLER_ARGS)
 3231 {
 3232         struct xvnode *xvn;
 3233         struct mount *mp;
 3234         struct vnode *vp;
 3235         int error, len, n;
 3236 
 3237         /*
 3238          * Stale numvnodes access is not fatal here.
 3239          */
 3240         req->lock = 0;
 3241         len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
 3242         if (!req->oldptr)
 3243                 /* Make an estimate */
 3244                 return (SYSCTL_OUT(req, 0, len));
 3245 
 3246         error = sysctl_wire_old_buffer(req, 0);
 3247         if (error != 0)
 3248                 return (error);
 3249         xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
 3250         n = 0;
 3251         mtx_lock(&mountlist_mtx);
 3252         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
 3253                 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
 3254                         continue;
 3255                 MNT_ILOCK(mp);
 3256                 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
 3257                         if (n == len)
 3258                                 break;
 3259                         vref(vp);
 3260                         xvn[n].xv_size = sizeof *xvn;
 3261                         xvn[n].xv_vnode = vp;
 3262                         xvn[n].xv_id = 0;       /* XXX compat */
 3263 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
 3264                         XV_COPY(usecount);
 3265                         XV_COPY(writecount);
 3266                         XV_COPY(holdcnt);
 3267                         XV_COPY(mount);
 3268                         XV_COPY(numoutput);
 3269                         XV_COPY(type);
 3270 #undef XV_COPY
 3271                         xvn[n].xv_flag = vp->v_vflag;
 3272 
 3273                         switch (vp->v_type) {
 3274                         case VREG:
 3275                         case VDIR:
 3276                         case VLNK:
 3277                                 break;
 3278                         case VBLK:
 3279                         case VCHR:
 3280                                 if (vp->v_rdev == NULL) {
 3281                                         vrele(vp);
 3282                                         continue;
 3283                                 }
 3284                                 xvn[n].xv_dev = dev2udev(vp->v_rdev);
 3285                                 break;
 3286                         case VSOCK:
 3287                                 xvn[n].xv_socket = vp->v_socket;
 3288                                 break;
 3289                         case VFIFO:
 3290                                 xvn[n].xv_fifo = vp->v_fifoinfo;
 3291                                 break;
 3292                         case VNON:
 3293                         case VBAD:
 3294                         default:
 3295                                 /* shouldn't happen? */
 3296                                 vrele(vp);
 3297                                 continue;
 3298                         }
 3299                         vrele(vp);
 3300                         ++n;
 3301                 }
 3302                 MNT_IUNLOCK(mp);
 3303                 mtx_lock(&mountlist_mtx);
 3304                 vfs_unbusy(mp);
 3305                 if (n == len)
 3306                         break;
 3307         }
 3308         mtx_unlock(&mountlist_mtx);
 3309 
 3310         error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
 3311         free(xvn, M_TEMP);
 3312         return (error);
 3313 }
 3314 
 3315 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
 3316     0, 0, sysctl_vnode, "S,xvnode", "");
 3317 #endif
 3318 
 3319 /*
 3320  * Unmount all filesystems. The list is traversed in reverse order
 3321  * of mounting to avoid dependencies.
 3322  */
 3323 void
 3324 vfs_unmountall(void)
 3325 {
 3326         struct mount *mp;
 3327         struct thread *td;
 3328         int error;
 3329 
 3330         KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
 3331         CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
 3332         td = curthread;
 3333 
 3334         /*
 3335          * Since this only runs when rebooting, it is not interlocked.
 3336          */
 3337         while(!TAILQ_EMPTY(&mountlist)) {
 3338                 mp = TAILQ_LAST(&mountlist, mntlist);
 3339                 error = dounmount(mp, MNT_FORCE, td);
 3340                 if (error) {
 3341                         TAILQ_REMOVE(&mountlist, mp, mnt_list);
 3342                         /*
 3343                          * XXX: Due to the way in which we mount the root
 3344                          * file system off of devfs, devfs will generate a
 3345                          * "busy" warning when we try to unmount it before
 3346                          * the root.  Don't print a warning as a result in
 3347                          * order to avoid false positive errors that may
 3348                          * cause needless upset.
 3349                          */
 3350                         if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
 3351                                 printf("unmount of %s failed (",
 3352                                     mp->mnt_stat.f_mntonname);
 3353                                 if (error == EBUSY)
 3354                                         printf("BUSY)\n");
 3355                                 else
 3356                                         printf("%d)\n", error);
 3357                         }
 3358                 } else {
 3359                         /* The unmount has removed mp from the mountlist */
 3360                 }
 3361         }
 3362 }
 3363 
 3364 /*
 3365  * perform msync on all vnodes under a mount point
 3366  * the mount point must be locked.
 3367  */
 3368 void
 3369 vfs_msync(struct mount *mp, int flags)
 3370 {
 3371         struct vnode *vp, *mvp;
 3372         struct vm_object *obj;
 3373 
 3374         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
 3375         MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
 3376                 obj = vp->v_object;
 3377                 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
 3378                     (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
 3379                         if (!vget(vp,
 3380                             LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
 3381                             curthread)) {
 3382                                 if (vp->v_vflag & VV_NOSYNC) {  /* unlinked */
 3383                                         vput(vp);
 3384                                         continue;
 3385                                 }
 3386 
 3387                                 obj = vp->v_object;
 3388                                 if (obj != NULL) {
 3389                                         VM_OBJECT_LOCK(obj);
 3390                                         vm_object_page_clean(obj, 0, 0,
 3391                                             flags == MNT_WAIT ?
 3392                                             OBJPC_SYNC : OBJPC_NOSYNC);
 3393                                         VM_OBJECT_UNLOCK(obj);
 3394                                 }
 3395                                 vput(vp);
 3396                         }
 3397                 } else
 3398                         VI_UNLOCK(vp);
 3399         }
 3400 }
 3401 
 3402 static void
 3403 destroy_vpollinfo(struct vpollinfo *vi)
 3404 {
 3405         seldrain(&vi->vpi_selinfo);
 3406         knlist_destroy(&vi->vpi_selinfo.si_note);
 3407         mtx_destroy(&vi->vpi_lock);
 3408         uma_zfree(vnodepoll_zone, vi);
 3409 }
 3410 
 3411 /*
 3412  * Initalize per-vnode helper structure to hold poll-related state.
 3413  */
 3414 void
 3415 v_addpollinfo(struct vnode *vp)
 3416 {
 3417         struct vpollinfo *vi;
 3418 
 3419         if (vp->v_pollinfo != NULL)
 3420                 return;
 3421         vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
 3422         mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
 3423         knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
 3424             vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
 3425         VI_LOCK(vp);
 3426         if (vp->v_pollinfo != NULL) {
 3427                 VI_UNLOCK(vp);
 3428                 destroy_vpollinfo(vi);
 3429                 return;
 3430         }
 3431         vp->v_pollinfo = vi;
 3432         VI_UNLOCK(vp);
 3433 }
 3434 
 3435 /*
 3436  * Record a process's interest in events which might happen to
 3437  * a vnode.  Because poll uses the historic select-style interface
 3438  * internally, this routine serves as both the ``check for any
 3439  * pending events'' and the ``record my interest in future events''
 3440  * functions.  (These are done together, while the lock is held,
 3441  * to avoid race conditions.)
 3442  */
 3443 int
 3444 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
 3445 {
 3446 
 3447         v_addpollinfo(vp);
 3448         mtx_lock(&vp->v_pollinfo->vpi_lock);
 3449         if (vp->v_pollinfo->vpi_revents & events) {
 3450                 /*
 3451                  * This leaves events we are not interested
 3452                  * in available for the other process which
 3453                  * which presumably had requested them
 3454                  * (otherwise they would never have been
 3455                  * recorded).
 3456                  */
 3457                 events &= vp->v_pollinfo->vpi_revents;
 3458                 vp->v_pollinfo->vpi_revents &= ~events;
 3459 
 3460                 mtx_unlock(&vp->v_pollinfo->vpi_lock);
 3461                 return (events);
 3462         }
 3463         vp->v_pollinfo->vpi_events |= events;
 3464         selrecord(td, &vp->v_pollinfo->vpi_selinfo);
 3465         mtx_unlock(&vp->v_pollinfo->vpi_lock);
 3466         return (0);
 3467 }
 3468 
 3469 /*
 3470  * Routine to create and manage a filesystem syncer vnode.
 3471  */
 3472 #define sync_close ((int (*)(struct  vop_close_args *))nullop)
 3473 static int      sync_fsync(struct  vop_fsync_args *);
 3474 static int      sync_inactive(struct  vop_inactive_args *);
 3475 static int      sync_reclaim(struct  vop_reclaim_args *);
 3476 
 3477 static struct vop_vector sync_vnodeops = {
 3478         .vop_bypass =   VOP_EOPNOTSUPP,
 3479         .vop_close =    sync_close,             /* close */
 3480         .vop_fsync =    sync_fsync,             /* fsync */
 3481         .vop_inactive = sync_inactive,  /* inactive */
 3482         .vop_reclaim =  sync_reclaim,   /* reclaim */
 3483         .vop_lock1 =    vop_stdlock,    /* lock */
 3484         .vop_unlock =   vop_stdunlock,  /* unlock */
 3485         .vop_islocked = vop_stdislocked,        /* islocked */
 3486 };
 3487 
 3488 /*
 3489  * Create a new filesystem syncer vnode for the specified mount point.
 3490  */
 3491 void
 3492 vfs_allocate_syncvnode(struct mount *mp)
 3493 {
 3494         struct vnode *vp;
 3495         struct bufobj *bo;
 3496         static long start, incr, next;
 3497         int error;
 3498 
 3499         /* Allocate a new vnode */
 3500         error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
 3501         if (error != 0)
 3502                 panic("vfs_allocate_syncvnode: getnewvnode() failed");
 3503         vp->v_type = VNON;
 3504         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 3505         vp->v_vflag |= VV_FORCEINSMQ;
 3506         error = insmntque(vp, mp);
 3507         if (error != 0)
 3508                 panic("vfs_allocate_syncvnode: insmntque() failed");
 3509         vp->v_vflag &= ~VV_FORCEINSMQ;
 3510         VOP_UNLOCK(vp, 0);
 3511         /*
 3512          * Place the vnode onto the syncer worklist. We attempt to
 3513          * scatter them about on the list so that they will go off
 3514          * at evenly distributed times even if all the filesystems
 3515          * are mounted at once.
 3516          */
 3517         next += incr;
 3518         if (next == 0 || next > syncer_maxdelay) {
 3519                 start /= 2;
 3520                 incr /= 2;
 3521                 if (start == 0) {
 3522                         start = syncer_maxdelay / 2;
 3523                         incr = syncer_maxdelay;
 3524                 }
 3525                 next = start;
 3526         }
 3527         bo = &vp->v_bufobj;
 3528         BO_LOCK(bo);
 3529         vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
 3530         /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
 3531         mtx_lock(&sync_mtx);
 3532         sync_vnode_count++;
 3533         if (mp->mnt_syncer == NULL) {
 3534                 mp->mnt_syncer = vp;
 3535                 vp = NULL;
 3536         }
 3537         mtx_unlock(&sync_mtx);
 3538         BO_UNLOCK(bo);
 3539         if (vp != NULL) {
 3540                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 3541                 vgone(vp);
 3542                 vput(vp);
 3543         }
 3544 }
 3545 
 3546 void
 3547 vfs_deallocate_syncvnode(struct mount *mp)
 3548 {
 3549         struct vnode *vp;
 3550 
 3551         mtx_lock(&sync_mtx);
 3552         vp = mp->mnt_syncer;
 3553         if (vp != NULL)
 3554                 mp->mnt_syncer = NULL;
 3555         mtx_unlock(&sync_mtx);
 3556         if (vp != NULL)
 3557                 vrele(vp);
 3558 }
 3559 
 3560 /*
 3561  * Do a lazy sync of the filesystem.
 3562  */
 3563 static int
 3564 sync_fsync(struct vop_fsync_args *ap)
 3565 {
 3566         struct vnode *syncvp = ap->a_vp;
 3567         struct mount *mp = syncvp->v_mount;
 3568         int error, save;
 3569         struct bufobj *bo;
 3570 
 3571         /*
 3572          * We only need to do something if this is a lazy evaluation.
 3573          */
 3574         if (ap->a_waitfor != MNT_LAZY)
 3575                 return (0);
 3576 
 3577         /*
 3578          * Move ourselves to the back of the sync list.
 3579          */
 3580         bo = &syncvp->v_bufobj;
 3581         BO_LOCK(bo);
 3582         vn_syncer_add_to_worklist(bo, syncdelay);
 3583         BO_UNLOCK(bo);
 3584 
 3585         /*
 3586          * Walk the list of vnodes pushing all that are dirty and
 3587          * not already on the sync list.
 3588          */
 3589         mtx_lock(&mountlist_mtx);
 3590         if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
 3591                 mtx_unlock(&mountlist_mtx);
 3592                 return (0);
 3593         }
 3594         if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
 3595                 vfs_unbusy(mp);
 3596                 return (0);
 3597         }
 3598         save = curthread_pflags_set(TDP_SYNCIO);
 3599         vfs_msync(mp, MNT_NOWAIT);
 3600         error = VFS_SYNC(mp, MNT_LAZY);
 3601         curthread_pflags_restore(save);
 3602         vn_finished_write(mp);
 3603         vfs_unbusy(mp);
 3604         return (error);
 3605 }
 3606 
 3607 /*
 3608  * The syncer vnode is no referenced.
 3609  */
 3610 static int
 3611 sync_inactive(struct vop_inactive_args *ap)
 3612 {
 3613 
 3614         vgone(ap->a_vp);
 3615         return (0);
 3616 }
 3617 
 3618 /*
 3619  * The syncer vnode is no longer needed and is being decommissioned.
 3620  *
 3621  * Modifications to the worklist must be protected by sync_mtx.
 3622  */
 3623 static int
 3624 sync_reclaim(struct vop_reclaim_args *ap)
 3625 {
 3626         struct vnode *vp = ap->a_vp;
 3627         struct bufobj *bo;
 3628 
 3629         bo = &vp->v_bufobj;
 3630         BO_LOCK(bo);
 3631         mtx_lock(&sync_mtx);
 3632         if (vp->v_mount->mnt_syncer == vp)
 3633                 vp->v_mount->mnt_syncer = NULL;
 3634         if (bo->bo_flag & BO_ONWORKLST) {
 3635                 LIST_REMOVE(bo, bo_synclist);
 3636                 syncer_worklist_len--;
 3637                 sync_vnode_count--;
 3638                 bo->bo_flag &= ~BO_ONWORKLST;
 3639         }
 3640         mtx_unlock(&sync_mtx);
 3641         BO_UNLOCK(bo);
 3642 
 3643         return (0);
 3644 }
 3645 
 3646 /*
 3647  * Check if vnode represents a disk device
 3648  */
 3649 int
 3650 vn_isdisk(struct vnode *vp, int *errp)
 3651 {
 3652         int error;
 3653 
 3654         error = 0;
 3655         dev_lock();
 3656         if (vp->v_type != VCHR)
 3657                 error = ENOTBLK;
 3658         else if (vp->v_rdev == NULL)
 3659                 error = ENXIO;
 3660         else if (vp->v_rdev->si_devsw == NULL)
 3661                 error = ENXIO;
 3662         else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
 3663                 error = ENOTBLK;
 3664         dev_unlock();
 3665         if (errp != NULL)
 3666                 *errp = error;
 3667         return (error == 0);
 3668 }
 3669 
 3670 /*
 3671  * Common filesystem object access control check routine.  Accepts a
 3672  * vnode's type, "mode", uid and gid, requested access mode, credentials,
 3673  * and optional call-by-reference privused argument allowing vaccess()
 3674  * to indicate to the caller whether privilege was used to satisfy the
 3675  * request (obsoleted).  Returns 0 on success, or an errno on failure.
 3676  */
 3677 int
 3678 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
 3679     accmode_t accmode, struct ucred *cred, int *privused)
 3680 {
 3681         accmode_t dac_granted;
 3682         accmode_t priv_granted;
 3683 
 3684         KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
 3685             ("invalid bit in accmode"));
 3686         KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
 3687             ("VAPPEND without VWRITE"));
 3688 
 3689         /*
 3690          * Look for a normal, non-privileged way to access the file/directory
 3691          * as requested.  If it exists, go with that.
 3692          */
 3693 
 3694         if (privused != NULL)
 3695                 *privused = 0;
 3696 
 3697         dac_granted = 0;
 3698 
 3699         /* Check the owner. */
 3700         if (cred->cr_uid == file_uid) {
 3701                 dac_granted |= VADMIN;
 3702                 if (file_mode & S_IXUSR)
 3703                         dac_granted |= VEXEC;
 3704                 if (file_mode & S_IRUSR)
 3705                         dac_granted |= VREAD;
 3706                 if (file_mode & S_IWUSR)
 3707                         dac_granted |= (VWRITE | VAPPEND);
 3708 
 3709                 if ((accmode & dac_granted) == accmode)
 3710                         return (0);
 3711 
 3712                 goto privcheck;
 3713         }
 3714 
 3715         /* Otherwise, check the groups (first match) */
 3716         if (groupmember(file_gid, cred)) {
 3717                 if (file_mode & S_IXGRP)
 3718                         dac_granted |= VEXEC;
 3719                 if (file_mode & S_IRGRP)
 3720                         dac_granted |= VREAD;
 3721                 if (file_mode & S_IWGRP)
 3722                         dac_granted |= (VWRITE | VAPPEND);
 3723 
 3724                 if ((accmode & dac_granted) == accmode)
 3725                         return (0);
 3726 
 3727                 goto privcheck;
 3728         }
 3729 
 3730         /* Otherwise, check everyone else. */
 3731         if (file_mode & S_IXOTH)
 3732                 dac_granted |= VEXEC;
 3733         if (file_mode & S_IROTH)
 3734                 dac_granted |= VREAD;
 3735         if (file_mode & S_IWOTH)
 3736                 dac_granted |= (VWRITE | VAPPEND);
 3737         if ((accmode & dac_granted) == accmode)
 3738                 return (0);
 3739 
 3740 privcheck:
 3741         /*
 3742          * Build a privilege mask to determine if the set of privileges
 3743          * satisfies the requirements when combined with the granted mask
 3744          * from above.  For each privilege, if the privilege is required,
 3745          * bitwise or the request type onto the priv_granted mask.
 3746          */
 3747         priv_granted = 0;
 3748 
 3749         if (type == VDIR) {
 3750                 /*
 3751                  * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
 3752                  * requests, instead of PRIV_VFS_EXEC.
 3753                  */
 3754                 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
 3755                     !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
 3756                         priv_granted |= VEXEC;
 3757         } else {
 3758                 /*
 3759                  * Ensure that at least one execute bit is on. Otherwise,
 3760                  * a privileged user will always succeed, and we don't want
 3761                  * this to happen unless the file really is executable.
 3762                  */
 3763                 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
 3764                     (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
 3765                     !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
 3766                         priv_granted |= VEXEC;
 3767         }
 3768 
 3769         if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
 3770             !priv_check_cred(cred, PRIV_VFS_READ, 0))
 3771                 priv_granted |= VREAD;
 3772 
 3773         if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
 3774             !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
 3775                 priv_granted |= (VWRITE | VAPPEND);
 3776 
 3777         if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
 3778             !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
 3779                 priv_granted |= VADMIN;
 3780 
 3781         if ((accmode & (priv_granted | dac_granted)) == accmode) {
 3782                 /* XXX audit: privilege used */
 3783                 if (privused != NULL)
 3784                         *privused = 1;
 3785                 return (0);
 3786         }
 3787 
 3788         return ((accmode & VADMIN) ? EPERM : EACCES);
 3789 }
 3790 
 3791 /*
 3792  * Credential check based on process requesting service, and per-attribute
 3793  * permissions.
 3794  */
 3795 int
 3796 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
 3797     struct thread *td, accmode_t accmode)
 3798 {
 3799 
 3800         /*
 3801          * Kernel-invoked always succeeds.
 3802          */
 3803         if (cred == NOCRED)
 3804                 return (0);
 3805 
 3806         /*
 3807          * Do not allow privileged processes in jail to directly manipulate
 3808          * system attributes.
 3809          */
 3810         switch (attrnamespace) {
 3811         case EXTATTR_NAMESPACE_SYSTEM:
 3812                 /* Potentially should be: return (EPERM); */
 3813                 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
 3814         case EXTATTR_NAMESPACE_USER:
 3815                 return (VOP_ACCESS(vp, accmode, cred, td));
 3816         default:
 3817                 return (EPERM);
 3818         }
 3819 }
 3820 
 3821 #ifdef DEBUG_VFS_LOCKS
 3822 /*
 3823  * This only exists to supress warnings from unlocked specfs accesses.  It is
 3824  * no longer ok to have an unlocked VFS.
 3825  */
 3826 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL ||            \
 3827         (vp)->v_type == VCHR || (vp)->v_type == VBAD)
 3828 
 3829 int vfs_badlock_ddb = 1;        /* Drop into debugger on violation. */
 3830 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
 3831     "Drop into debugger on lock violation");
 3832 
 3833 int vfs_badlock_mutex = 1;      /* Check for interlock across VOPs. */
 3834 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
 3835     0, "Check for interlock across VOPs");
 3836 
 3837 int vfs_badlock_print = 1;      /* Print lock violations. */
 3838 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
 3839     0, "Print lock violations");
 3840 
 3841 #ifdef KDB
 3842 int vfs_badlock_backtrace = 1;  /* Print backtrace at lock violations. */
 3843 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
 3844     &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
 3845 #endif
 3846 
 3847 static void
 3848 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
 3849 {
 3850 
 3851 #ifdef KDB
 3852         if (vfs_badlock_backtrace)
 3853                 kdb_backtrace();
 3854 #endif
 3855         if (vfs_badlock_print)
 3856                 printf("%s: %p %s\n", str, (void *)vp, msg);
 3857         if (vfs_badlock_ddb)
 3858                 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
 3859 }
 3860 
 3861 void
 3862 assert_vi_locked(struct vnode *vp, const char *str)
 3863 {
 3864 
 3865         if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
 3866                 vfs_badlock("interlock is not locked but should be", str, vp);
 3867 }
 3868 
 3869 void
 3870 assert_vi_unlocked(struct vnode *vp, const char *str)
 3871 {
 3872 
 3873         if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
 3874                 vfs_badlock("interlock is locked but should not be", str, vp);
 3875 }
 3876 
 3877 void
 3878 assert_vop_locked(struct vnode *vp, const char *str)
 3879 {
 3880 
 3881         if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
 3882                 vfs_badlock("is not locked but should be", str, vp);
 3883 }
 3884 
 3885 void
 3886 assert_vop_unlocked(struct vnode *vp, const char *str)
 3887 {
 3888 
 3889         if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
 3890                 vfs_badlock("is locked but should not be", str, vp);
 3891 }
 3892 
 3893 void
 3894 assert_vop_elocked(struct vnode *vp, const char *str)
 3895 {
 3896 
 3897         if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
 3898                 vfs_badlock("is not exclusive locked but should be", str, vp);
 3899 }
 3900 
 3901 #if 0
 3902 void
 3903 assert_vop_elocked_other(struct vnode *vp, const char *str)
 3904 {
 3905 
 3906         if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
 3907                 vfs_badlock("is not exclusive locked by another thread",
 3908                     str, vp);
 3909 }
 3910 
 3911 void
 3912 assert_vop_slocked(struct vnode *vp, const char *str)
 3913 {
 3914 
 3915         if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
 3916                 vfs_badlock("is not locked shared but should be", str, vp);
 3917 }
 3918 #endif /* 0 */
 3919 #endif /* DEBUG_VFS_LOCKS */
 3920 
 3921 void
 3922 vop_rename_fail(struct vop_rename_args *ap)
 3923 {
 3924 
 3925         if (ap->a_tvp != NULL)
 3926                 vput(ap->a_tvp);
 3927         if (ap->a_tdvp == ap->a_tvp)
 3928                 vrele(ap->a_tdvp);
 3929         else
 3930                 vput(ap->a_tdvp);
 3931         vrele(ap->a_fdvp);
 3932         vrele(ap->a_fvp);
 3933 }
 3934 
 3935 void
 3936 vop_rename_pre(void *ap)
 3937 {
 3938         struct vop_rename_args *a = ap;
 3939 
 3940 #ifdef DEBUG_VFS_LOCKS
 3941         if (a->a_tvp)
 3942                 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
 3943         ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
 3944         ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
 3945         ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
 3946 
 3947         /* Check the source (from). */
 3948         if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
 3949             (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
 3950                 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
 3951         if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
 3952                 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
 3953 
 3954         /* Check the target. */
 3955         if (a->a_tvp)
 3956                 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
 3957         ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
 3958 #endif
 3959         if (a->a_tdvp != a->a_fdvp)
 3960                 vhold(a->a_fdvp);
 3961         if (a->a_tvp != a->a_fvp)
 3962                 vhold(a->a_fvp);
 3963         vhold(a->a_tdvp);
 3964         if (a->a_tvp)
 3965                 vhold(a->a_tvp);
 3966 }
 3967 
 3968 void
 3969 vop_strategy_pre(void *ap)
 3970 {
 3971 #ifdef DEBUG_VFS_LOCKS
 3972         struct vop_strategy_args *a;
 3973         struct buf *bp;
 3974 
 3975         a = ap;
 3976         bp = a->a_bp;
 3977 
 3978         /*
 3979          * Cluster ops lock their component buffers but not the IO container.
 3980          */
 3981         if ((bp->b_flags & B_CLUSTER) != 0)
 3982                 return;
 3983 
 3984         if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
 3985                 if (vfs_badlock_print)
 3986                         printf(
 3987                             "VOP_STRATEGY: bp is not locked but should be\n");
 3988                 if (vfs_badlock_ddb)
 3989                         kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
 3990         }
 3991 #endif
 3992 }
 3993 
 3994 void
 3995 vop_lookup_pre(void *ap)
 3996 {
 3997 #ifdef DEBUG_VFS_LOCKS
 3998         struct vop_lookup_args *a;
 3999         struct vnode *dvp;
 4000 
 4001         a = ap;
 4002         dvp = a->a_dvp;
 4003         ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
 4004         ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
 4005 #endif
 4006 }
 4007 
 4008 void
 4009 vop_lookup_post(void *ap, int rc)
 4010 {
 4011 #ifdef DEBUG_VFS_LOCKS
 4012         struct vop_lookup_args *a;
 4013         struct vnode *dvp;
 4014         struct vnode *vp;
 4015 
 4016         a = ap;
 4017         dvp = a->a_dvp;
 4018         vp = *(a->a_vpp);
 4019 
 4020         ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
 4021         ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
 4022 
 4023         if (!rc)
 4024                 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
 4025 #endif
 4026 }
 4027 
 4028 void
 4029 vop_lock_pre(void *ap)
 4030 {
 4031 #ifdef DEBUG_VFS_LOCKS
 4032         struct vop_lock1_args *a = ap;
 4033 
 4034         if ((a->a_flags & LK_INTERLOCK) == 0)
 4035                 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
 4036         else
 4037                 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
 4038 #endif
 4039 }
 4040 
 4041 void
 4042 vop_lock_post(void *ap, int rc)
 4043 {
 4044 #ifdef DEBUG_VFS_LOCKS
 4045         struct vop_lock1_args *a = ap;
 4046 
 4047         ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
 4048         if (rc == 0)
 4049                 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
 4050 #endif
 4051 }
 4052 
 4053 void
 4054 vop_unlock_pre(void *ap)
 4055 {
 4056 #ifdef DEBUG_VFS_LOCKS
 4057         struct vop_unlock_args *a = ap;
 4058 
 4059         if (a->a_flags & LK_INTERLOCK)
 4060                 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
 4061         ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
 4062 #endif
 4063 }
 4064 
 4065 void
 4066 vop_unlock_post(void *ap, int rc)
 4067 {
 4068 #ifdef DEBUG_VFS_LOCKS
 4069         struct vop_unlock_args *a = ap;
 4070 
 4071         if (a->a_flags & LK_INTERLOCK)
 4072                 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
 4073 #endif
 4074 }
 4075 
 4076 void
 4077 vop_create_post(void *ap, int rc)
 4078 {
 4079         struct vop_create_args *a = ap;
 4080 
 4081         if (!rc)
 4082                 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
 4083 }
 4084 
 4085 void
 4086 vop_deleteextattr_post(void *ap, int rc)
 4087 {
 4088         struct vop_deleteextattr_args *a = ap;
 4089 
 4090         if (!rc)
 4091                 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
 4092 }
 4093 
 4094 void
 4095 vop_link_post(void *ap, int rc)
 4096 {
 4097         struct vop_link_args *a = ap;
 4098 
 4099         if (!rc) {
 4100                 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
 4101                 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
 4102         }
 4103 }
 4104 
 4105 void
 4106 vop_mkdir_post(void *ap, int rc)
 4107 {
 4108         struct vop_mkdir_args *a = ap;
 4109 
 4110         if (!rc)
 4111                 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
 4112 }
 4113 
 4114 void
 4115 vop_mknod_post(void *ap, int rc)
 4116 {
 4117         struct vop_mknod_args *a = ap;
 4118 
 4119         if (!rc)
 4120                 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
 4121 }
 4122 
 4123 void
 4124 vop_remove_post(void *ap, int rc)
 4125 {
 4126         struct vop_remove_args *a = ap;
 4127 
 4128         if (!rc) {
 4129                 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
 4130                 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
 4131         }
 4132 }
 4133 
 4134 void
 4135 vop_rename_post(void *ap, int rc)
 4136 {
 4137         struct vop_rename_args *a = ap;
 4138 
 4139         if (!rc) {
 4140                 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
 4141                 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
 4142                 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
 4143                 if (a->a_tvp)
 4144                         VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
 4145         }
 4146         if (a->a_tdvp != a->a_fdvp)
 4147                 vdrop(a->a_fdvp);
 4148         if (a->a_tvp != a->a_fvp)
 4149                 vdrop(a->a_fvp);
 4150         vdrop(a->a_tdvp);
 4151         if (a->a_tvp)
 4152                 vdrop(a->a_tvp);
 4153 }
 4154 
 4155 void
 4156 vop_rmdir_post(void *ap, int rc)
 4157 {
 4158         struct vop_rmdir_args *a = ap;
 4159 
 4160         if (!rc) {
 4161                 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
 4162                 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
 4163         }
 4164 }
 4165 
 4166 void
 4167 vop_setattr_post(void *ap, int rc)
 4168 {
 4169         struct vop_setattr_args *a = ap;
 4170 
 4171         if (!rc)
 4172                 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
 4173 }
 4174 
 4175 void
 4176 vop_setextattr_post(void *ap, int rc)
 4177 {
 4178         struct vop_setextattr_args *a = ap;
 4179 
 4180         if (!rc)
 4181                 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
 4182 }
 4183 
 4184 void
 4185 vop_symlink_post(void *ap, int rc)
 4186 {
 4187         struct vop_symlink_args *a = ap;
 4188 
 4189         if (!rc)
 4190                 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
 4191 }
 4192 
 4193 static struct knlist fs_knlist;
 4194 
 4195 static void
 4196 vfs_event_init(void *arg)
 4197 {
 4198         knlist_init_mtx(&fs_knlist, NULL);
 4199 }
 4200 /* XXX - correct order? */
 4201 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
 4202 
 4203 void
 4204 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
 4205 {
 4206 
 4207         KNOTE_UNLOCKED(&fs_knlist, event);
 4208 }
 4209 
 4210 static int      filt_fsattach(struct knote *kn);
 4211 static void     filt_fsdetach(struct knote *kn);
 4212 static int      filt_fsevent(struct knote *kn, long hint);
 4213 
 4214 struct filterops fs_filtops = {
 4215         .f_isfd = 0,
 4216         .f_attach = filt_fsattach,
 4217         .f_detach = filt_fsdetach,
 4218         .f_event = filt_fsevent
 4219 };
 4220 
 4221 static int
 4222 filt_fsattach(struct knote *kn)
 4223 {
 4224 
 4225         kn->kn_flags |= EV_CLEAR;
 4226         knlist_add(&fs_knlist, kn, 0);
 4227         return (0);
 4228 }
 4229 
 4230 static void
 4231 filt_fsdetach(struct knote *kn)
 4232 {
 4233 
 4234         knlist_remove(&fs_knlist, kn, 0);
 4235 }
 4236 
 4237 static int
 4238 filt_fsevent(struct knote *kn, long hint)
 4239 {
 4240 
 4241         kn->kn_fflags |= hint;
 4242         return (kn->kn_fflags != 0);
 4243 }
 4244 
 4245 static int
 4246 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
 4247 {
 4248         struct vfsidctl vc;
 4249         int error;
 4250         struct mount *mp;
 4251 
 4252         error = SYSCTL_IN(req, &vc, sizeof(vc));
 4253         if (error)
 4254                 return (error);
 4255         if (vc.vc_vers != VFS_CTL_VERS1)
 4256                 return (EINVAL);
 4257         mp = vfs_getvfs(&vc.vc_fsid);
 4258         if (mp == NULL)
 4259                 return (ENOENT);
 4260         /* ensure that a specific sysctl goes to the right filesystem. */
 4261         if (strcmp(vc.vc_fstypename, "*") != 0 &&
 4262             strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
 4263                 vfs_rel(mp);
 4264                 return (EINVAL);
 4265         }
 4266         VCTLTOREQ(&vc, req);
 4267         error = VFS_SYSCTL(mp, vc.vc_op, req);
 4268         vfs_rel(mp);
 4269         return (error);
 4270 }
 4271 
 4272 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
 4273     NULL, 0, sysctl_vfs_ctl, "",
 4274     "Sysctl by fsid");
 4275 
 4276 /*
 4277  * Function to initialize a va_filerev field sensibly.
 4278  * XXX: Wouldn't a random number make a lot more sense ??
 4279  */
 4280 u_quad_t
 4281 init_va_filerev(void)
 4282 {
 4283         struct bintime bt;
 4284 
 4285         getbinuptime(&bt);
 4286         return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
 4287 }
 4288 
 4289 static int      filt_vfsread(struct knote *kn, long hint);
 4290 static int      filt_vfswrite(struct knote *kn, long hint);
 4291 static int      filt_vfsvnode(struct knote *kn, long hint);
 4292 static void     filt_vfsdetach(struct knote *kn);
 4293 static struct filterops vfsread_filtops = {
 4294         .f_isfd = 1,
 4295         .f_detach = filt_vfsdetach,
 4296         .f_event = filt_vfsread
 4297 };
 4298 static struct filterops vfswrite_filtops = {
 4299         .f_isfd = 1,
 4300         .f_detach = filt_vfsdetach,
 4301         .f_event = filt_vfswrite
 4302 };
 4303 static struct filterops vfsvnode_filtops = {
 4304         .f_isfd = 1,
 4305         .f_detach = filt_vfsdetach,
 4306         .f_event = filt_vfsvnode
 4307 };
 4308 
 4309 static void
 4310 vfs_knllock(void *arg)
 4311 {
 4312         struct vnode *vp = arg;
 4313 
 4314         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 4315 }
 4316 
 4317 static void
 4318 vfs_knlunlock(void *arg)
 4319 {
 4320         struct vnode *vp = arg;
 4321 
 4322         VOP_UNLOCK(vp, 0);
 4323 }
 4324 
 4325 static void
 4326 vfs_knl_assert_locked(void *arg)
 4327 {
 4328 #ifdef DEBUG_VFS_LOCKS
 4329         struct vnode *vp = arg;
 4330 
 4331         ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
 4332 #endif
 4333 }
 4334 
 4335 static void
 4336 vfs_knl_assert_unlocked(void *arg)
 4337 {
 4338 #ifdef DEBUG_VFS_LOCKS
 4339         struct vnode *vp = arg;
 4340 
 4341         ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
 4342 #endif
 4343 }
 4344 
 4345 int
 4346 vfs_kqfilter(struct vop_kqfilter_args *ap)
 4347 {
 4348         struct vnode *vp = ap->a_vp;
 4349         struct knote *kn = ap->a_kn;
 4350         struct knlist *knl;
 4351 
 4352         switch (kn->kn_filter) {
 4353         case EVFILT_READ:
 4354                 kn->kn_fop = &vfsread_filtops;
 4355                 break;
 4356         case EVFILT_WRITE:
 4357                 kn->kn_fop = &vfswrite_filtops;
 4358                 break;
 4359         case EVFILT_VNODE:
 4360                 kn->kn_fop = &vfsvnode_filtops;
 4361                 break;
 4362         default:
 4363                 return (EINVAL);
 4364         }
 4365 
 4366         kn->kn_hook = (caddr_t)vp;
 4367 
 4368         v_addpollinfo(vp);
 4369         if (vp->v_pollinfo == NULL)
 4370                 return (ENOMEM);
 4371         knl = &vp->v_pollinfo->vpi_selinfo.si_note;
 4372         knlist_add(knl, kn, 0);
 4373 
 4374         return (0);
 4375 }
 4376 
 4377 /*
 4378  * Detach knote from vnode
 4379  */
 4380 static void
 4381 filt_vfsdetach(struct knote *kn)
 4382 {
 4383         struct vnode *vp = (struct vnode *)kn->kn_hook;
 4384 
 4385         KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
 4386         knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
 4387 }
 4388 
 4389 /*ARGSUSED*/
 4390 static int
 4391 filt_vfsread(struct knote *kn, long hint)
 4392 {
 4393         struct vnode *vp = (struct vnode *)kn->kn_hook;
 4394         struct vattr va;
 4395         int res;
 4396 
 4397         /*
 4398          * filesystem is gone, so set the EOF flag and schedule
 4399          * the knote for deletion.
 4400          */
 4401         if (hint == NOTE_REVOKE) {
 4402                 VI_LOCK(vp);
 4403                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
 4404                 VI_UNLOCK(vp);
 4405                 return (1);
 4406         }
 4407 
 4408         if (VOP_GETATTR(vp, &va, curthread->td_ucred))
 4409                 return (0);
 4410 
 4411         VI_LOCK(vp);
 4412         kn->kn_data = va.va_size - kn->kn_fp->f_offset;
 4413         res = (kn->kn_data != 0);
 4414         VI_UNLOCK(vp);
 4415         return (res);
 4416 }
 4417 
 4418 /*ARGSUSED*/
 4419 static int
 4420 filt_vfswrite(struct knote *kn, long hint)
 4421 {
 4422         struct vnode *vp = (struct vnode *)kn->kn_hook;
 4423 
 4424         VI_LOCK(vp);
 4425 
 4426         /*
 4427          * filesystem is gone, so set the EOF flag and schedule
 4428          * the knote for deletion.
 4429          */
 4430         if (hint == NOTE_REVOKE)
 4431                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
 4432 
 4433         kn->kn_data = 0;
 4434         VI_UNLOCK(vp);
 4435         return (1);
 4436 }
 4437 
 4438 static int
 4439 filt_vfsvnode(struct knote *kn, long hint)
 4440 {
 4441         struct vnode *vp = (struct vnode *)kn->kn_hook;
 4442         int res;
 4443 
 4444         VI_LOCK(vp);
 4445         if (kn->kn_sfflags & hint)
 4446                 kn->kn_fflags |= hint;
 4447         if (hint == NOTE_REVOKE) {
 4448                 kn->kn_flags |= EV_EOF;
 4449                 VI_UNLOCK(vp);
 4450                 return (1);
 4451         }
 4452         res = (kn->kn_fflags != 0);
 4453         VI_UNLOCK(vp);
 4454         return (res);
 4455 }
 4456 
 4457 int
 4458 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
 4459 {
 4460         int error;
 4461 
 4462         if (dp->d_reclen > ap->a_uio->uio_resid)
 4463                 return (ENAMETOOLONG);
 4464         error = uiomove(dp, dp->d_reclen, ap->a_uio);
 4465         if (error) {
 4466                 if (ap->a_ncookies != NULL) {
 4467                         if (ap->a_cookies != NULL)
 4468                                 free(ap->a_cookies, M_TEMP);
 4469                         ap->a_cookies = NULL;
 4470                         *ap->a_ncookies = 0;
 4471                 }
 4472                 return (error);
 4473         }
 4474         if (ap->a_ncookies == NULL)
 4475                 return (0);
 4476 
 4477         KASSERT(ap->a_cookies,
 4478             ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
 4479 
 4480         *ap->a_cookies = realloc(*ap->a_cookies,
 4481             (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
 4482         (*ap->a_cookies)[*ap->a_ncookies] = off;
 4483         return (0);
 4484 }
 4485 
 4486 /*
 4487  * Mark for update the access time of the file if the filesystem
 4488  * supports VOP_MARKATIME.  This functionality is used by execve and
 4489  * mmap, so we want to avoid the I/O implied by directly setting
 4490  * va_atime for the sake of efficiency.
 4491  */
 4492 void
 4493 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
 4494 {
 4495         struct mount *mp;
 4496 
 4497         mp = vp->v_mount;
 4498         VFS_ASSERT_GIANT(mp);
 4499         ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
 4500         if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
 4501                 (void)VOP_MARKATIME(vp);
 4502 }
 4503 
 4504 /*
 4505  * The purpose of this routine is to remove granularity from accmode_t,
 4506  * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
 4507  * VADMIN and VAPPEND.
 4508  *
 4509  * If it returns 0, the caller is supposed to continue with the usual
 4510  * access checks using 'accmode' as modified by this routine.  If it
 4511  * returns nonzero value, the caller is supposed to return that value
 4512  * as errno.
 4513  *
 4514  * Note that after this routine runs, accmode may be zero.
 4515  */
 4516 int
 4517 vfs_unixify_accmode(accmode_t *accmode)
 4518 {
 4519         /*
 4520          * There is no way to specify explicit "deny" rule using
 4521          * file mode or POSIX.1e ACLs.
 4522          */
 4523         if (*accmode & VEXPLICIT_DENY) {
 4524                 *accmode = 0;
 4525                 return (0);
 4526         }
 4527 
 4528         /*
 4529          * None of these can be translated into usual access bits.
 4530          * Also, the common case for NFSv4 ACLs is to not contain
 4531          * either of these bits. Caller should check for VWRITE
 4532          * on the containing directory instead.
 4533          */
 4534         if (*accmode & (VDELETE_CHILD | VDELETE))
 4535                 return (EPERM);
 4536 
 4537         if (*accmode & VADMIN_PERMS) {
 4538                 *accmode &= ~VADMIN_PERMS;
 4539                 *accmode |= VADMIN;
 4540         }
 4541 
 4542         /*
 4543          * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
 4544          * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
 4545          */
 4546         *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
 4547 
 4548         return (0);
 4549 }
 4550 
 4551 /*
 4552  * These are helper functions for filesystems to traverse all
 4553  * their vnodes.  See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
 4554  *
 4555  * This interface replaces MNT_VNODE_FOREACH.
 4556  */
 4557 
 4558 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
 4559 
 4560 struct vnode *
 4561 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
 4562 {
 4563         struct vnode *vp;
 4564 
 4565         if (should_yield())
 4566                 kern_yield(PRI_UNCHANGED);
 4567         MNT_ILOCK(mp);
 4568         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 4569         vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
 4570         while (vp != NULL && (vp->v_type == VMARKER ||
 4571             (vp->v_iflag & VI_DOOMED) != 0))
 4572                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 4573 
 4574         /* Check if we are done */
 4575         if (vp == NULL) {
 4576                 __mnt_vnode_markerfree_all(mvp, mp);
 4577                 /* MNT_IUNLOCK(mp); -- done in above function */
 4578                 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
 4579                 return (NULL);
 4580         }
 4581         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
 4582         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
 4583         VI_LOCK(vp);
 4584         MNT_IUNLOCK(mp);
 4585         return (vp);
 4586 }
 4587 
 4588 struct vnode *
 4589 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
 4590 {
 4591         struct vnode *vp;
 4592 
 4593         *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
 4594         MNT_ILOCK(mp);
 4595         MNT_REF(mp);
 4596         (*mvp)->v_type = VMARKER;
 4597 
 4598         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
 4599         while (vp != NULL && (vp->v_type == VMARKER ||
 4600             (vp->v_iflag & VI_DOOMED) != 0))
 4601                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
 4602 
 4603         /* Check if we are done */
 4604         if (vp == NULL) {
 4605                 MNT_REL(mp);
 4606                 MNT_IUNLOCK(mp);
 4607                 free(*mvp, M_VNODE_MARKER);
 4608                 *mvp = NULL;
 4609                 return (NULL);
 4610         }
 4611         (*mvp)->v_mount = mp;
 4612         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
 4613         VI_LOCK(vp);
 4614         MNT_IUNLOCK(mp);
 4615         return (vp);
 4616 }
 4617 
 4618 
 4619 void
 4620 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
 4621 {
 4622 
 4623         if (*mvp == NULL) {
 4624                 MNT_IUNLOCK(mp);
 4625                 return;
 4626         }
 4627 
 4628         mtx_assert(MNT_MTX(mp), MA_OWNED);
 4629 
 4630         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 4631         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
 4632         MNT_REL(mp);
 4633         MNT_IUNLOCK(mp);
 4634         free(*mvp, M_VNODE_MARKER);
 4635         *mvp = NULL;
 4636 }
 4637 
 4638 /*
 4639  * These are helper functions for filesystems to traverse their
 4640  * active vnodes.  See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
 4641  */
 4642 struct vnode *
 4643 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
 4644 {
 4645         struct vnode *vp, *nvp;
 4646 
 4647         if (should_yield())
 4648                 kern_yield(PRI_UNCHANGED);
 4649         MNT_ILOCK(mp);
 4650         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 4651         vp = TAILQ_NEXT(*mvp, v_actfreelist);
 4652         while (vp != NULL) {
 4653                 VI_LOCK(vp);
 4654                 if (vp->v_mount == mp && vp->v_type != VMARKER &&
 4655                     (vp->v_iflag & VI_DOOMED) == 0)
 4656                         break;
 4657                 nvp = TAILQ_NEXT(vp, v_actfreelist);
 4658                 VI_UNLOCK(vp);
 4659                 vp = nvp;
 4660         }
 4661 
 4662         /* Check if we are done */
 4663         if (vp == NULL) {
 4664                 __mnt_vnode_markerfree_active(mvp, mp);
 4665                 /* MNT_IUNLOCK(mp); -- done in above function */
 4666                 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
 4667                 return (NULL);
 4668         }
 4669         mtx_lock(&vnode_free_list_mtx);
 4670         TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
 4671         TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
 4672         mtx_unlock(&vnode_free_list_mtx);
 4673         MNT_IUNLOCK(mp);
 4674         return (vp);
 4675 }
 4676 
 4677 struct vnode *
 4678 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
 4679 {
 4680         struct vnode *vp, *nvp;
 4681 
 4682         *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
 4683         MNT_ILOCK(mp);
 4684         MNT_REF(mp);
 4685         (*mvp)->v_type = VMARKER;
 4686 
 4687         vp = TAILQ_NEXT(*mvp, v_actfreelist);
 4688         while (vp != NULL) {
 4689                 VI_LOCK(vp);
 4690                 if (vp->v_mount == mp && vp->v_type != VMARKER &&
 4691                     (vp->v_iflag & VI_DOOMED) == 0)
 4692                         break;
 4693                 nvp = TAILQ_NEXT(vp, v_actfreelist);
 4694                 VI_UNLOCK(vp);
 4695                 vp = nvp;
 4696         }
 4697 
 4698         /* Check if we are done */
 4699         if (vp == NULL) {
 4700                 MNT_REL(mp);
 4701                 MNT_IUNLOCK(mp);
 4702                 free(*mvp, M_VNODE_MARKER);
 4703                 *mvp = NULL;
 4704                 return (NULL);
 4705         }
 4706         (*mvp)->v_mount = mp;
 4707         mtx_lock(&vnode_free_list_mtx);
 4708         TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
 4709         mtx_unlock(&vnode_free_list_mtx);
 4710         MNT_IUNLOCK(mp);
 4711         return (vp);
 4712 }
 4713 
 4714 void
 4715 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
 4716 {
 4717 
 4718         if (*mvp == NULL) {
 4719                 MNT_IUNLOCK(mp);
 4720                 return;
 4721         }
 4722 
 4723         mtx_assert(MNT_MTX(mp), MA_OWNED);
 4724 
 4725         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
 4726         mtx_lock(&vnode_free_list_mtx);
 4727         TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
 4728         mtx_unlock(&vnode_free_list_mtx);
 4729         MNT_REL(mp);
 4730         MNT_IUNLOCK(mp);
 4731         free(*mvp, M_VNODE_MARKER);
 4732         *mvp = NULL;
 4733 }

Cache object: 8a178be5bf359ccbbc0071a72f7c6e62


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.