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

Cache object: 62823cb129e44238fed487424a672fa9


[ 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.