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