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