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

Cache object: 85e6ef4e59102fd0e389d04e1c3fd1b3


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