xref: /netbsd-src/sys/kern/vfs_subr.c (revision 37afb7eb6895c833050f8bfb1d1bb2f99f332539)
1 /*	$NetBSD: vfs_subr.c,v 1.447 2015/06/23 10:42:34 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 2004, 2005, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, by Charles M. Hannum, by Andrew Doran,
10  * by Marshall Kirk McKusick and Greg Ganger at the University of Michigan.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1989, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  * (c) UNIX System Laboratories, Inc.
38  * All or some portions of this file are derived from material licensed
39  * to the University of California by American Telephone and Telegraph
40  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41  * the permission of UNIX System Laboratories, Inc.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: vfs_subr.c,v 1.447 2015/06/23 10:42:34 hannken Exp $");
72 
73 #include "opt_ddb.h"
74 #include "opt_compat_netbsd.h"
75 #include "opt_compat_43.h"
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/conf.h>
80 #include <sys/dirent.h>
81 #include <sys/filedesc.h>
82 #include <sys/kernel.h>
83 #include <sys/mount.h>
84 #include <sys/vnode.h>
85 #include <sys/stat.h>
86 #include <sys/sysctl.h>
87 #include <sys/namei.h>
88 #include <sys/buf.h>
89 #include <sys/errno.h>
90 #include <sys/kmem.h>
91 #include <sys/syscallargs.h>
92 #include <sys/kauth.h>
93 #include <sys/module.h>
94 
95 #include <miscfs/genfs/genfs.h>
96 #include <miscfs/specfs/specdev.h>
97 #include <uvm/uvm_ddb.h>
98 
99 const enum vtype iftovt_tab[16] = {
100 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
101 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
102 };
103 const int	vttoif_tab[9] = {
104 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
105 	S_IFSOCK, S_IFIFO, S_IFMT,
106 };
107 
108 /*
109  * Insq/Remq for the vnode usage lists.
110  */
111 #define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
112 #define	bufremvn(bp) {							\
113 	LIST_REMOVE(bp, b_vnbufs);					\
114 	(bp)->b_vnbufs.le_next = NOLIST;				\
115 }
116 
117 int doforce = 1;		/* 1 => permit forcible unmounting */
118 int prtactive = 0;		/* 1 => print out reclaim of active vnodes */
119 
120 extern struct mount *dead_rootmount;
121 
122 /*
123  * Local declarations.
124  */
125 
126 static void vn_initialize_syncerd(void);
127 
128 /*
129  * Initialize the vnode management data structures.
130  */
131 void
132 vntblinit(void)
133 {
134 
135 	vn_initialize_syncerd();
136 	vfs_mount_sysinit();
137 	vfs_vnode_sysinit();
138 }
139 
140 /*
141  * Flush out and invalidate all buffers associated with a vnode.
142  * Called with the underlying vnode locked, which should prevent new dirty
143  * buffers from being queued.
144  */
145 int
146 vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l,
147 	  bool catch_p, int slptimeo)
148 {
149 	struct buf *bp, *nbp;
150 	int error;
151 	int flushflags = PGO_ALLPAGES | PGO_FREE | PGO_SYNCIO |
152 	    (flags & V_SAVE ? PGO_CLEANIT | PGO_RECLAIM : 0);
153 
154 	/* XXXUBC this doesn't look at flags or slp* */
155 	mutex_enter(vp->v_interlock);
156 	error = VOP_PUTPAGES(vp, 0, 0, flushflags);
157 	if (error) {
158 		return error;
159 	}
160 
161 	if (flags & V_SAVE) {
162 		error = VOP_FSYNC(vp, cred, FSYNC_WAIT|FSYNC_RECLAIM, 0, 0);
163 		if (error)
164 		        return (error);
165 		KASSERT(LIST_EMPTY(&vp->v_dirtyblkhd));
166 	}
167 
168 	mutex_enter(&bufcache_lock);
169 restart:
170 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
171 		KASSERT(bp->b_vp == vp);
172 		nbp = LIST_NEXT(bp, b_vnbufs);
173 		error = bbusy(bp, catch_p, slptimeo, NULL);
174 		if (error != 0) {
175 			if (error == EPASSTHROUGH)
176 				goto restart;
177 			mutex_exit(&bufcache_lock);
178 			return (error);
179 		}
180 		brelsel(bp, BC_INVAL | BC_VFLUSH);
181 	}
182 
183 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
184 		KASSERT(bp->b_vp == vp);
185 		nbp = LIST_NEXT(bp, b_vnbufs);
186 		error = bbusy(bp, catch_p, slptimeo, NULL);
187 		if (error != 0) {
188 			if (error == EPASSTHROUGH)
189 				goto restart;
190 			mutex_exit(&bufcache_lock);
191 			return (error);
192 		}
193 		/*
194 		 * XXX Since there are no node locks for NFS, I believe
195 		 * there is a slight chance that a delayed write will
196 		 * occur while sleeping just above, so check for it.
197 		 */
198 		if ((bp->b_oflags & BO_DELWRI) && (flags & V_SAVE)) {
199 #ifdef DEBUG
200 			printf("buffer still DELWRI\n");
201 #endif
202 			bp->b_cflags |= BC_BUSY | BC_VFLUSH;
203 			mutex_exit(&bufcache_lock);
204 			VOP_BWRITE(bp->b_vp, bp);
205 			mutex_enter(&bufcache_lock);
206 			goto restart;
207 		}
208 		brelsel(bp, BC_INVAL | BC_VFLUSH);
209 	}
210 
211 #ifdef DIAGNOSTIC
212 	if (!LIST_EMPTY(&vp->v_cleanblkhd) || !LIST_EMPTY(&vp->v_dirtyblkhd))
213 		panic("vinvalbuf: flush failed, vp %p", vp);
214 #endif
215 
216 	mutex_exit(&bufcache_lock);
217 
218 	return (0);
219 }
220 
221 /*
222  * Destroy any in core blocks past the truncation length.
223  * Called with the underlying vnode locked, which should prevent new dirty
224  * buffers from being queued.
225  */
226 int
227 vtruncbuf(struct vnode *vp, daddr_t lbn, bool catch_p, int slptimeo)
228 {
229 	struct buf *bp, *nbp;
230 	int error;
231 	voff_t off;
232 
233 	off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift);
234 	mutex_enter(vp->v_interlock);
235 	error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO);
236 	if (error) {
237 		return error;
238 	}
239 
240 	mutex_enter(&bufcache_lock);
241 restart:
242 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
243 		KASSERT(bp->b_vp == vp);
244 		nbp = LIST_NEXT(bp, b_vnbufs);
245 		if (bp->b_lblkno < lbn)
246 			continue;
247 		error = bbusy(bp, catch_p, slptimeo, NULL);
248 		if (error != 0) {
249 			if (error == EPASSTHROUGH)
250 				goto restart;
251 			mutex_exit(&bufcache_lock);
252 			return (error);
253 		}
254 		brelsel(bp, BC_INVAL | BC_VFLUSH);
255 	}
256 
257 	for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
258 		KASSERT(bp->b_vp == vp);
259 		nbp = LIST_NEXT(bp, b_vnbufs);
260 		if (bp->b_lblkno < lbn)
261 			continue;
262 		error = bbusy(bp, catch_p, slptimeo, NULL);
263 		if (error != 0) {
264 			if (error == EPASSTHROUGH)
265 				goto restart;
266 			mutex_exit(&bufcache_lock);
267 			return (error);
268 		}
269 		brelsel(bp, BC_INVAL | BC_VFLUSH);
270 	}
271 	mutex_exit(&bufcache_lock);
272 
273 	return (0);
274 }
275 
276 /*
277  * Flush all dirty buffers from a vnode.
278  * Called with the underlying vnode locked, which should prevent new dirty
279  * buffers from being queued.
280  */
281 int
282 vflushbuf(struct vnode *vp, int flags)
283 {
284 	struct buf *bp, *nbp;
285 	int error, pflags;
286 	bool dirty, sync;
287 
288 	sync = (flags & FSYNC_WAIT) != 0;
289 	pflags = PGO_CLEANIT | PGO_ALLPAGES |
290 		(sync ? PGO_SYNCIO : 0) |
291 		((flags & FSYNC_LAZY) ? PGO_LAZY : 0);
292 	mutex_enter(vp->v_interlock);
293 	(void) VOP_PUTPAGES(vp, 0, 0, pflags);
294 
295 loop:
296 	mutex_enter(&bufcache_lock);
297 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
298 		KASSERT(bp->b_vp == vp);
299 		nbp = LIST_NEXT(bp, b_vnbufs);
300 		if ((bp->b_cflags & BC_BUSY))
301 			continue;
302 		if ((bp->b_oflags & BO_DELWRI) == 0)
303 			panic("vflushbuf: not dirty, bp %p", bp);
304 		bp->b_cflags |= BC_BUSY | BC_VFLUSH;
305 		mutex_exit(&bufcache_lock);
306 		/*
307 		 * Wait for I/O associated with indirect blocks to complete,
308 		 * since there is no way to quickly wait for them below.
309 		 */
310 		if (bp->b_vp == vp || !sync)
311 			(void) bawrite(bp);
312 		else {
313 			error = bwrite(bp);
314 			if (error)
315 				return error;
316 		}
317 		goto loop;
318 	}
319 	mutex_exit(&bufcache_lock);
320 
321 	if (!sync)
322 		return 0;
323 
324 	mutex_enter(vp->v_interlock);
325 	while (vp->v_numoutput != 0)
326 		cv_wait(&vp->v_cv, vp->v_interlock);
327 	dirty = !LIST_EMPTY(&vp->v_dirtyblkhd);
328 	mutex_exit(vp->v_interlock);
329 
330 	if (dirty) {
331 		vprint("vflushbuf: dirty", vp);
332 		goto loop;
333 	}
334 
335 	return 0;
336 }
337 
338 /*
339  * Create a vnode for a block device.
340  * Used for root filesystem and swap areas.
341  * Also used for memory file system special devices.
342  */
343 int
344 bdevvp(dev_t dev, vnode_t **vpp)
345 {
346 	struct vattr va;
347 
348 	vattr_null(&va);
349 	va.va_type = VBLK;
350 	va.va_rdev = dev;
351 
352 	return vcache_new(dead_rootmount, NULL, &va, NOCRED, vpp);
353 }
354 
355 /*
356  * Create a vnode for a character device.
357  * Used for kernfs and some console handling.
358  */
359 int
360 cdevvp(dev_t dev, vnode_t **vpp)
361 {
362 	struct vattr va;
363 
364 	vattr_null(&va);
365 	va.va_type = VCHR;
366 	va.va_rdev = dev;
367 
368 	return vcache_new(dead_rootmount, NULL, &va, NOCRED, vpp);
369 }
370 
371 /*
372  * Associate a buffer with a vnode.  There must already be a hold on
373  * the vnode.
374  */
375 void
376 bgetvp(struct vnode *vp, struct buf *bp)
377 {
378 
379 	KASSERT(bp->b_vp == NULL);
380 	KASSERT(bp->b_objlock == &buffer_lock);
381 	KASSERT(mutex_owned(vp->v_interlock));
382 	KASSERT(mutex_owned(&bufcache_lock));
383 	KASSERT((bp->b_cflags & BC_BUSY) != 0);
384 	KASSERT(!cv_has_waiters(&bp->b_done));
385 
386 	vholdl(vp);
387 	bp->b_vp = vp;
388 	if (vp->v_type == VBLK || vp->v_type == VCHR)
389 		bp->b_dev = vp->v_rdev;
390 	else
391 		bp->b_dev = NODEV;
392 
393 	/*
394 	 * Insert onto list for new vnode.
395 	 */
396 	bufinsvn(bp, &vp->v_cleanblkhd);
397 	bp->b_objlock = vp->v_interlock;
398 }
399 
400 /*
401  * Disassociate a buffer from a vnode.
402  */
403 void
404 brelvp(struct buf *bp)
405 {
406 	struct vnode *vp = bp->b_vp;
407 
408 	KASSERT(vp != NULL);
409 	KASSERT(bp->b_objlock == vp->v_interlock);
410 	KASSERT(mutex_owned(vp->v_interlock));
411 	KASSERT(mutex_owned(&bufcache_lock));
412 	KASSERT((bp->b_cflags & BC_BUSY) != 0);
413 	KASSERT(!cv_has_waiters(&bp->b_done));
414 
415 	/*
416 	 * Delete from old vnode list, if on one.
417 	 */
418 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
419 		bufremvn(bp);
420 
421 	if (vp->v_uobj.uo_npages == 0 && (vp->v_iflag & VI_ONWORKLST) &&
422 	    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
423 		vp->v_iflag &= ~VI_WRMAPDIRTY;
424 		vn_syncer_remove_from_worklist(vp);
425 	}
426 
427 	bp->b_objlock = &buffer_lock;
428 	bp->b_vp = NULL;
429 	holdrelel(vp);
430 }
431 
432 /*
433  * Reassign a buffer from one vnode list to another.
434  * The list reassignment must be within the same vnode.
435  * Used to assign file specific control information
436  * (indirect blocks) to the list to which they belong.
437  */
438 void
439 reassignbuf(struct buf *bp, struct vnode *vp)
440 {
441 	struct buflists *listheadp;
442 	int delayx;
443 
444 	KASSERT(mutex_owned(&bufcache_lock));
445 	KASSERT(bp->b_objlock == vp->v_interlock);
446 	KASSERT(mutex_owned(vp->v_interlock));
447 	KASSERT((bp->b_cflags & BC_BUSY) != 0);
448 
449 	/*
450 	 * Delete from old vnode list, if on one.
451 	 */
452 	if (LIST_NEXT(bp, b_vnbufs) != NOLIST)
453 		bufremvn(bp);
454 
455 	/*
456 	 * If dirty, put on list of dirty buffers;
457 	 * otherwise insert onto list of clean buffers.
458 	 */
459 	if ((bp->b_oflags & BO_DELWRI) == 0) {
460 		listheadp = &vp->v_cleanblkhd;
461 		if (vp->v_uobj.uo_npages == 0 &&
462 		    (vp->v_iflag & VI_ONWORKLST) &&
463 		    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
464 			vp->v_iflag &= ~VI_WRMAPDIRTY;
465 			vn_syncer_remove_from_worklist(vp);
466 		}
467 	} else {
468 		listheadp = &vp->v_dirtyblkhd;
469 		if ((vp->v_iflag & VI_ONWORKLST) == 0) {
470 			switch (vp->v_type) {
471 			case VDIR:
472 				delayx = dirdelay;
473 				break;
474 			case VBLK:
475 				if (spec_node_getmountedfs(vp) != NULL) {
476 					delayx = metadelay;
477 					break;
478 				}
479 				/* fall through */
480 			default:
481 				delayx = filedelay;
482 				break;
483 			}
484 			if (!vp->v_mount ||
485 			    (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
486 				vn_syncer_add_to_worklist(vp, delayx);
487 		}
488 	}
489 	bufinsvn(bp, listheadp);
490 }
491 
492 /*
493  * Lookup a vnode by device number and return it referenced.
494  */
495 int
496 vfinddev(dev_t dev, enum vtype type, vnode_t **vpp)
497 {
498 
499 	return (spec_node_lookup_by_dev(type, dev, vpp) == 0);
500 }
501 
502 /*
503  * Revoke all the vnodes corresponding to the specified minor number
504  * range (endpoints inclusive) of the specified major.
505  */
506 void
507 vdevgone(int maj, int minl, int minh, enum vtype type)
508 {
509 	vnode_t *vp;
510 	dev_t dev;
511 	int mn;
512 
513 	for (mn = minl; mn <= minh; mn++) {
514 		dev = makedev(maj, mn);
515 		while (spec_node_lookup_by_dev(type, dev, &vp) == 0) {
516 			VOP_REVOKE(vp, REVOKEALL);
517 			vrele(vp);
518 		}
519 	}
520 }
521 
522 /*
523  * The filesystem synchronizer mechanism - syncer.
524  *
525  * It is useful to delay writes of file data and filesystem metadata for
526  * a certain amount of time so that quickly created and deleted files need
527  * not waste disk bandwidth being created and removed.  To implement this,
528  * vnodes are appended to a "workitem" queue.
529  *
530  * Most pending metadata should not wait for more than ten seconds.  Thus,
531  * mounted on block devices are delayed only about a half the time that file
532  * data is delayed.  Similarly, directory updates are more critical, so are
533  * only delayed about a third the time that file data is delayed.
534  *
535  * There are SYNCER_MAXDELAY queues that are processed in a round-robin
536  * manner at a rate of one each second (driven off the filesystem syner
537  * thread). The syncer_delayno variable indicates the next queue that is
538  * to be processed.  Items that need to be processed soon are placed in
539  * this queue:
540  *
541  *	syncer_workitem_pending[syncer_delayno]
542  *
543  * A delay of e.g. fifteen seconds is done by placing the request fifteen
544  * entries later in the queue:
545  *
546  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
547  *
548  * Flag VI_ONWORKLST indicates that vnode is added into the queue.
549  */
550 
551 #define SYNCER_MAXDELAY		32
552 
553 typedef TAILQ_HEAD(synclist, vnode) synclist_t;
554 
555 static void	vn_syncer_add1(struct vnode *, int);
556 static void	sysctl_vfs_syncfs_setup(struct sysctllog **);
557 
558 /*
559  * Defines and variables for the syncer process.
560  */
561 int syncer_maxdelay = SYNCER_MAXDELAY;	/* maximum delay time */
562 time_t syncdelay = 30;			/* max time to delay syncing data */
563 time_t filedelay = 30;			/* time to delay syncing files */
564 time_t dirdelay  = 15;			/* time to delay syncing directories */
565 time_t metadelay = 10;			/* time to delay syncing metadata */
566 time_t lockdelay = 1;			/* time to delay if locking fails */
567 
568 kmutex_t		syncer_mutex;	/* used to freeze syncer, long term */
569 static kmutex_t		syncer_data_lock; /* short term lock on data structs */
570 
571 static int		syncer_delayno = 0;
572 static long		syncer_last;
573 static synclist_t *	syncer_workitem_pending;
574 
575 static void
576 vn_initialize_syncerd(void)
577 {
578 	int i;
579 
580 	syncer_last = SYNCER_MAXDELAY + 2;
581 
582 	sysctl_vfs_syncfs_setup(NULL);
583 
584 	syncer_workitem_pending =
585 	    kmem_alloc(syncer_last * sizeof (struct synclist), KM_SLEEP);
586 
587 	for (i = 0; i < syncer_last; i++)
588 		TAILQ_INIT(&syncer_workitem_pending[i]);
589 
590 	mutex_init(&syncer_mutex, MUTEX_DEFAULT, IPL_NONE);
591 	mutex_init(&syncer_data_lock, MUTEX_DEFAULT, IPL_NONE);
592 }
593 
594 /*
595  * Return delay factor appropriate for the given file system.   For
596  * WAPBL we use the sync vnode to burst out metadata updates: sync
597  * those file systems more frequently.
598  */
599 static inline int
600 sync_delay(struct mount *mp)
601 {
602 
603 	return mp->mnt_wapbl != NULL ? metadelay : syncdelay;
604 }
605 
606 /*
607  * Compute the next slot index from delay.
608  */
609 static inline int
610 sync_delay_slot(int delayx)
611 {
612 
613 	if (delayx > syncer_maxdelay - 2)
614 		delayx = syncer_maxdelay - 2;
615 	return (syncer_delayno + delayx) % syncer_last;
616 }
617 
618 /*
619  * Add an item to the syncer work queue.
620  */
621 static void
622 vn_syncer_add1(struct vnode *vp, int delayx)
623 {
624 	synclist_t *slp;
625 
626 	KASSERT(mutex_owned(&syncer_data_lock));
627 
628 	if (vp->v_iflag & VI_ONWORKLST) {
629 		/*
630 		 * Remove in order to adjust the position of the vnode.
631 		 * Note: called from sched_sync(), which will not hold
632 		 * interlock, therefore we cannot modify v_iflag here.
633 		 */
634 		slp = &syncer_workitem_pending[vp->v_synclist_slot];
635 		TAILQ_REMOVE(slp, vp, v_synclist);
636 	} else {
637 		KASSERT(mutex_owned(vp->v_interlock));
638 		vp->v_iflag |= VI_ONWORKLST;
639 	}
640 
641 	vp->v_synclist_slot = sync_delay_slot(delayx);
642 
643 	slp = &syncer_workitem_pending[vp->v_synclist_slot];
644 	TAILQ_INSERT_TAIL(slp, vp, v_synclist);
645 }
646 
647 void
648 vn_syncer_add_to_worklist(struct vnode *vp, int delayx)
649 {
650 
651 	KASSERT(mutex_owned(vp->v_interlock));
652 
653 	mutex_enter(&syncer_data_lock);
654 	vn_syncer_add1(vp, delayx);
655 	mutex_exit(&syncer_data_lock);
656 }
657 
658 /*
659  * Remove an item from the syncer work queue.
660  */
661 void
662 vn_syncer_remove_from_worklist(struct vnode *vp)
663 {
664 	synclist_t *slp;
665 
666 	KASSERT(mutex_owned(vp->v_interlock));
667 
668 	mutex_enter(&syncer_data_lock);
669 	if (vp->v_iflag & VI_ONWORKLST) {
670 		vp->v_iflag &= ~VI_ONWORKLST;
671 		slp = &syncer_workitem_pending[vp->v_synclist_slot];
672 		TAILQ_REMOVE(slp, vp, v_synclist);
673 	}
674 	mutex_exit(&syncer_data_lock);
675 }
676 
677 /*
678  * Add this mount point to the syncer.
679  */
680 void
681 vfs_syncer_add_to_worklist(struct mount *mp)
682 {
683 	static int start, incr, next;
684 	int vdelay;
685 
686 	KASSERT(mutex_owned(&mp->mnt_updating));
687 	KASSERT((mp->mnt_iflag & IMNT_ONWORKLIST) == 0);
688 
689 	/*
690 	 * We attempt to scatter the mount points on the list
691 	 * so that they will go off at evenly distributed times
692 	 * even if all the filesystems are mounted at once.
693 	 */
694 
695 	next += incr;
696 	if (next == 0 || next > syncer_maxdelay) {
697 		start /= 2;
698 		incr /= 2;
699 		if (start == 0) {
700 			start = syncer_maxdelay / 2;
701 			incr = syncer_maxdelay;
702 		}
703 		next = start;
704 	}
705 	mp->mnt_iflag |= IMNT_ONWORKLIST;
706 	vdelay = sync_delay(mp);
707 	mp->mnt_synclist_slot = vdelay > 0 ? next % vdelay : 0;
708 }
709 
710 /*
711  * Remove the mount point from the syncer.
712  */
713 void
714 vfs_syncer_remove_from_worklist(struct mount *mp)
715 {
716 
717 	KASSERT(mutex_owned(&mp->mnt_updating));
718 	KASSERT((mp->mnt_iflag & IMNT_ONWORKLIST) != 0);
719 
720 	mp->mnt_iflag &= ~IMNT_ONWORKLIST;
721 }
722 
723 /*
724  * Try lazy sync, return true on success.
725  */
726 static bool
727 lazy_sync_vnode(struct vnode *vp)
728 {
729 	bool synced;
730 
731 	KASSERT(mutex_owned(&syncer_data_lock));
732 
733 	synced = false;
734 	/* We are locking in the wrong direction. */
735 	if (mutex_tryenter(vp->v_interlock)) {
736 		mutex_exit(&syncer_data_lock);
737 		if (vget(vp, LK_NOWAIT, false /* !wait */) == 0) {
738 			if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
739 				synced = true;
740 				(void) VOP_FSYNC(vp, curlwp->l_cred,
741 				    FSYNC_LAZY, 0, 0);
742 				vput(vp);
743 			} else
744 				vrele(vp);
745 		}
746 		mutex_enter(&syncer_data_lock);
747 	}
748 	return synced;
749 }
750 
751 /*
752  * System filesystem synchronizer daemon.
753  */
754 void
755 sched_sync(void *arg)
756 {
757 	synclist_t *slp;
758 	struct vnode *vp;
759 	struct mount *mp, *nmp;
760 	time_t starttime;
761 	bool synced;
762 
763 	for (;;) {
764 		mutex_enter(&syncer_mutex);
765 
766 		starttime = time_second;
767 
768 		/*
769 		 * Sync mounts whose dirty time has expired.
770 		 */
771 		mutex_enter(&mountlist_lock);
772 		for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
773 			if ((mp->mnt_iflag & IMNT_ONWORKLIST) == 0 ||
774 			    mp->mnt_synclist_slot != syncer_delayno) {
775 				nmp = TAILQ_NEXT(mp, mnt_list);
776 				continue;
777 			}
778 			mp->mnt_synclist_slot = sync_delay_slot(sync_delay(mp));
779 			if (vfs_busy(mp, &nmp))
780 				continue;
781 			VFS_SYNC(mp, MNT_LAZY, curlwp->l_cred);
782 			vfs_unbusy(mp, false, &nmp);
783 		}
784 		mutex_exit(&mountlist_lock);
785 
786 		mutex_enter(&syncer_data_lock);
787 
788 		/*
789 		 * Push files whose dirty time has expired.
790 		 */
791 		slp = &syncer_workitem_pending[syncer_delayno];
792 		syncer_delayno += 1;
793 		if (syncer_delayno >= syncer_last)
794 			syncer_delayno = 0;
795 
796 		while ((vp = TAILQ_FIRST(slp)) != NULL) {
797 			synced = lazy_sync_vnode(vp);
798 
799 			/*
800 			 * XXX The vnode may have been recycled, in which
801 			 * case it may have a new identity.
802 			 */
803 			if (TAILQ_FIRST(slp) == vp) {
804 				/*
805 				 * Put us back on the worklist.  The worklist
806 				 * routine will remove us from our current
807 				 * position and then add us back in at a later
808 				 * position.
809 				 *
810 				 * Try again sooner rather than later if
811 				 * we were unable to lock the vnode.  Lock
812 				 * failure should not prevent us from doing
813 				 * the sync "soon".
814 				 *
815 				 * If we locked it yet arrive here, it's
816 				 * likely that lazy sync is in progress and
817 				 * so the vnode still has dirty metadata.
818 				 * syncdelay is mainly to get this vnode out
819 				 * of the way so we do not consider it again
820 				 * "soon" in this loop, so the delay time is
821 				 * not critical as long as it is not "soon".
822 				 * While write-back strategy is the file
823 				 * system's domain, we expect write-back to
824 				 * occur no later than syncdelay seconds
825 				 * into the future.
826 				 */
827 				vn_syncer_add1(vp,
828 				    synced ? syncdelay : lockdelay);
829 			}
830 		}
831 		mutex_exit(&syncer_mutex);
832 
833 		/*
834 		 * If it has taken us less than a second to process the
835 		 * current work, then wait.  Otherwise start right over
836 		 * again.  We can still lose time if any single round
837 		 * takes more than two seconds, but it does not really
838 		 * matter as we are just trying to generally pace the
839 		 * filesystem activity.
840 		 */
841 		if (time_second == starttime) {
842 			kpause("syncer", false, hz, &syncer_data_lock);
843 		}
844 		mutex_exit(&syncer_data_lock);
845 	}
846 }
847 
848 static void
849 sysctl_vfs_syncfs_setup(struct sysctllog **clog)
850 {
851 	const struct sysctlnode *rnode, *cnode;
852 
853 	sysctl_createv(clog, 0, NULL, &rnode,
854 			CTLFLAG_PERMANENT,
855 			CTLTYPE_NODE, "sync",
856 			SYSCTL_DESCR("syncer options"),
857 			NULL, 0, NULL, 0,
858 			CTL_VFS, CTL_CREATE, CTL_EOL);
859 
860 	sysctl_createv(clog, 0, &rnode, &cnode,
861 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
862 			CTLTYPE_QUAD, "delay",
863 			SYSCTL_DESCR("max time to delay syncing data"),
864 			NULL, 0, &syncdelay, 0,
865 			CTL_CREATE, CTL_EOL);
866 
867 	sysctl_createv(clog, 0, &rnode, &cnode,
868 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
869 			CTLTYPE_QUAD, "filedelay",
870 			SYSCTL_DESCR("time to delay syncing files"),
871 			NULL, 0, &filedelay, 0,
872 			CTL_CREATE, CTL_EOL);
873 
874 	sysctl_createv(clog, 0, &rnode, &cnode,
875 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
876 			CTLTYPE_QUAD, "dirdelay",
877 			SYSCTL_DESCR("time to delay syncing directories"),
878 			NULL, 0, &dirdelay, 0,
879 			CTL_CREATE, CTL_EOL);
880 
881 	sysctl_createv(clog, 0, &rnode, &cnode,
882 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
883 			CTLTYPE_QUAD, "metadelay",
884 			SYSCTL_DESCR("time to delay syncing metadata"),
885 			NULL, 0, &metadelay, 0,
886 			CTL_CREATE, CTL_EOL);
887 }
888 
889 /*
890  * sysctl helper routine to return list of supported fstypes
891  */
892 int
893 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS)
894 {
895 	char bf[sizeof(((struct statvfs *)NULL)->f_fstypename)];
896 	char *where = oldp;
897 	struct vfsops *v;
898 	size_t needed, left, slen;
899 	int error, first;
900 
901 	if (newp != NULL)
902 		return (EPERM);
903 	if (namelen != 0)
904 		return (EINVAL);
905 
906 	first = 1;
907 	error = 0;
908 	needed = 0;
909 	left = *oldlenp;
910 
911 	sysctl_unlock();
912 	mutex_enter(&vfs_list_lock);
913 	LIST_FOREACH(v, &vfs_list, vfs_list) {
914 		if (where == NULL)
915 			needed += strlen(v->vfs_name) + 1;
916 		else {
917 			memset(bf, 0, sizeof(bf));
918 			if (first) {
919 				strncpy(bf, v->vfs_name, sizeof(bf));
920 				first = 0;
921 			} else {
922 				bf[0] = ' ';
923 				strncpy(bf + 1, v->vfs_name, sizeof(bf) - 1);
924 			}
925 			bf[sizeof(bf)-1] = '\0';
926 			slen = strlen(bf);
927 			if (left < slen + 1)
928 				break;
929 			v->vfs_refcount++;
930 			mutex_exit(&vfs_list_lock);
931 			/* +1 to copy out the trailing NUL byte */
932 			error = copyout(bf, where, slen + 1);
933 			mutex_enter(&vfs_list_lock);
934 			v->vfs_refcount--;
935 			if (error)
936 				break;
937 			where += slen;
938 			needed += slen;
939 			left -= slen;
940 		}
941 	}
942 	mutex_exit(&vfs_list_lock);
943 	sysctl_relock();
944 	*oldlenp = needed;
945 	return (error);
946 }
947 
948 int kinfo_vdebug = 1;
949 int kinfo_vgetfailed;
950 
951 #define KINFO_VNODESLOP	10
952 
953 /*
954  * Dump vnode list (via sysctl).
955  * Copyout address of vnode followed by vnode.
956  */
957 int
958 sysctl_kern_vnode(SYSCTLFN_ARGS)
959 {
960 	char *where = oldp;
961 	size_t *sizep = oldlenp;
962 	struct mount *mp, *nmp;
963 	vnode_t *vp, vbuf;
964 	struct vnode_iterator *marker;
965 	char *bp = where;
966 	char *ewhere;
967 	int error;
968 
969 	if (namelen != 0)
970 		return (EOPNOTSUPP);
971 	if (newp != NULL)
972 		return (EPERM);
973 
974 #define VPTRSZ	sizeof(vnode_t *)
975 #define VNODESZ	sizeof(vnode_t)
976 	if (where == NULL) {
977 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
978 		return (0);
979 	}
980 	ewhere = where + *sizep;
981 
982 	sysctl_unlock();
983 	mutex_enter(&mountlist_lock);
984 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
985 		if (vfs_busy(mp, &nmp)) {
986 			continue;
987 		}
988 		vfs_vnode_iterator_init(mp, &marker);
989 		while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
990 			if (bp + VPTRSZ + VNODESZ > ewhere) {
991 				vrele(vp);
992 				vfs_vnode_iterator_destroy(marker);
993 				vfs_unbusy(mp, false, NULL);
994 				sysctl_relock();
995 				*sizep = bp - where;
996 				return (ENOMEM);
997 			}
998 			memcpy(&vbuf, vp, VNODESZ);
999 			if ((error = copyout(&vp, bp, VPTRSZ)) ||
1000 			    (error = copyout(&vbuf, bp + VPTRSZ, VNODESZ))) {
1001 				vrele(vp);
1002 				vfs_vnode_iterator_destroy(marker);
1003 				vfs_unbusy(mp, false, NULL);
1004 				sysctl_relock();
1005 				return (error);
1006 			}
1007 			vrele(vp);
1008 			bp += VPTRSZ + VNODESZ;
1009 		}
1010 		vfs_vnode_iterator_destroy(marker);
1011 		vfs_unbusy(mp, false, &nmp);
1012 	}
1013 	mutex_exit(&mountlist_lock);
1014 	sysctl_relock();
1015 
1016 	*sizep = bp - where;
1017 	return (0);
1018 }
1019 
1020 /*
1021  * Set vnode attributes to VNOVAL
1022  */
1023 void
1024 vattr_null(struct vattr *vap)
1025 {
1026 
1027 	memset(vap, 0, sizeof(*vap));
1028 
1029 	vap->va_type = VNON;
1030 
1031 	/*
1032 	 * Assign individually so that it is safe even if size and
1033 	 * sign of each member are varied.
1034 	 */
1035 	vap->va_mode = VNOVAL;
1036 	vap->va_nlink = VNOVAL;
1037 	vap->va_uid = VNOVAL;
1038 	vap->va_gid = VNOVAL;
1039 	vap->va_fsid = VNOVAL;
1040 	vap->va_fileid = VNOVAL;
1041 	vap->va_size = VNOVAL;
1042 	vap->va_blocksize = VNOVAL;
1043 	vap->va_atime.tv_sec =
1044 	    vap->va_mtime.tv_sec =
1045 	    vap->va_ctime.tv_sec =
1046 	    vap->va_birthtime.tv_sec = VNOVAL;
1047 	vap->va_atime.tv_nsec =
1048 	    vap->va_mtime.tv_nsec =
1049 	    vap->va_ctime.tv_nsec =
1050 	    vap->va_birthtime.tv_nsec = VNOVAL;
1051 	vap->va_gen = VNOVAL;
1052 	vap->va_flags = VNOVAL;
1053 	vap->va_rdev = VNOVAL;
1054 	vap->va_bytes = VNOVAL;
1055 }
1056 
1057 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
1058 #define ARRAY_PRINT(idx, arr) \
1059     ((unsigned int)(idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN")
1060 
1061 const char * const vnode_tags[] = { VNODE_TAGS };
1062 const char * const vnode_types[] = { VNODE_TYPES };
1063 const char vnode_flagbits[] = VNODE_FLAGBITS;
1064 
1065 /*
1066  * Print out a description of a vnode.
1067  */
1068 void
1069 vprint(const char *label, struct vnode *vp)
1070 {
1071 	char bf[96];
1072 	int flag;
1073 
1074 	flag = vp->v_iflag | vp->v_vflag | vp->v_uflag;
1075 	snprintb(bf, sizeof(bf), vnode_flagbits, flag);
1076 
1077 	if (label != NULL)
1078 		printf("%s: ", label);
1079 	printf("vnode @ %p, flags (%s)\n\ttag %s(%d), type %s(%d), "
1080 	    "usecount %d, writecount %d, holdcount %d\n"
1081 	    "\tfreelisthd %p, mount %p, data %p lock %p\n",
1082 	    vp, bf, ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
1083 	    ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
1084 	    vp->v_usecount, vp->v_writecount, vp->v_holdcnt,
1085 	    vp->v_freelisthd, vp->v_mount, vp->v_data, &vp->v_lock);
1086 	if (vp->v_data != NULL) {
1087 		printf("\t");
1088 		VOP_PRINT(vp);
1089 	}
1090 }
1091 
1092 /* Deprecated. Kept for KPI compatibility. */
1093 int
1094 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1095     mode_t acc_mode, kauth_cred_t cred)
1096 {
1097 
1098 #ifdef DIAGNOSTIC
1099 	printf("vaccess: deprecated interface used.\n");
1100 #endif /* DIAGNOSTIC */
1101 
1102 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(acc_mode,
1103 	    type, file_mode), NULL /* This may panic. */, NULL,
1104 	    genfs_can_access(type, file_mode, uid, gid, acc_mode, cred));
1105 }
1106 
1107 /*
1108  * Given a file system name, look up the vfsops for that
1109  * file system, or return NULL if file system isn't present
1110  * in the kernel.
1111  */
1112 struct vfsops *
1113 vfs_getopsbyname(const char *name)
1114 {
1115 	struct vfsops *v;
1116 
1117 	mutex_enter(&vfs_list_lock);
1118 	LIST_FOREACH(v, &vfs_list, vfs_list) {
1119 		if (strcmp(v->vfs_name, name) == 0)
1120 			break;
1121 	}
1122 	if (v != NULL)
1123 		v->vfs_refcount++;
1124 	mutex_exit(&vfs_list_lock);
1125 
1126 	return (v);
1127 }
1128 
1129 void
1130 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp)
1131 {
1132 	const struct statvfs *mbp;
1133 
1134 	if (sbp == (mbp = &mp->mnt_stat))
1135 		return;
1136 
1137 	(void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx));
1138 	sbp->f_fsid = mbp->f_fsid;
1139 	sbp->f_owner = mbp->f_owner;
1140 	sbp->f_flag = mbp->f_flag;
1141 	sbp->f_syncwrites = mbp->f_syncwrites;
1142 	sbp->f_asyncwrites = mbp->f_asyncwrites;
1143 	sbp->f_syncreads = mbp->f_syncreads;
1144 	sbp->f_asyncreads = mbp->f_asyncreads;
1145 	(void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare));
1146 	(void)memcpy(sbp->f_fstypename, mbp->f_fstypename,
1147 	    sizeof(sbp->f_fstypename));
1148 	(void)memcpy(sbp->f_mntonname, mbp->f_mntonname,
1149 	    sizeof(sbp->f_mntonname));
1150 	(void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname,
1151 	    sizeof(sbp->f_mntfromname));
1152 	sbp->f_namemax = mbp->f_namemax;
1153 }
1154 
1155 int
1156 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom,
1157     const char *vfsname, struct mount *mp, struct lwp *l)
1158 {
1159 	int error;
1160 	size_t size;
1161 	struct statvfs *sfs = &mp->mnt_stat;
1162 	int (*fun)(const void *, void *, size_t, size_t *);
1163 
1164 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsname,
1165 	    sizeof(mp->mnt_stat.f_fstypename));
1166 
1167 	if (onp) {
1168 		struct cwdinfo *cwdi = l->l_proc->p_cwdi;
1169 		fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr;
1170 		if (cwdi->cwdi_rdir != NULL) {
1171 			size_t len;
1172 			char *bp;
1173 			char *path = PNBUF_GET();
1174 
1175 			bp = path + MAXPATHLEN;
1176 			*--bp = '\0';
1177 			rw_enter(&cwdi->cwdi_lock, RW_READER);
1178 			error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
1179 			    path, MAXPATHLEN / 2, 0, l);
1180 			rw_exit(&cwdi->cwdi_lock);
1181 			if (error) {
1182 				PNBUF_PUT(path);
1183 				return error;
1184 			}
1185 
1186 			len = strlen(bp);
1187 			if (len > sizeof(sfs->f_mntonname) - 1)
1188 				len = sizeof(sfs->f_mntonname) - 1;
1189 			(void)strncpy(sfs->f_mntonname, bp, len);
1190 			PNBUF_PUT(path);
1191 
1192 			if (len < sizeof(sfs->f_mntonname) - 1) {
1193 				error = (*fun)(onp, &sfs->f_mntonname[len],
1194 				    sizeof(sfs->f_mntonname) - len - 1, &size);
1195 				if (error)
1196 					return error;
1197 				size += len;
1198 			} else {
1199 				size = len;
1200 			}
1201 		} else {
1202 			error = (*fun)(onp, &sfs->f_mntonname,
1203 			    sizeof(sfs->f_mntonname) - 1, &size);
1204 			if (error)
1205 				return error;
1206 		}
1207 		(void)memset(sfs->f_mntonname + size, 0,
1208 		    sizeof(sfs->f_mntonname) - size);
1209 	}
1210 
1211 	if (fromp) {
1212 		fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr;
1213 		error = (*fun)(fromp, sfs->f_mntfromname,
1214 		    sizeof(sfs->f_mntfromname) - 1, &size);
1215 		if (error)
1216 			return error;
1217 		(void)memset(sfs->f_mntfromname + size, 0,
1218 		    sizeof(sfs->f_mntfromname) - size);
1219 	}
1220 	return 0;
1221 }
1222 
1223 void
1224 vfs_timestamp(struct timespec *ts)
1225 {
1226 
1227 	nanotime(ts);
1228 }
1229 
1230 time_t	rootfstime;			/* recorded root fs time, if known */
1231 void
1232 setrootfstime(time_t t)
1233 {
1234 	rootfstime = t;
1235 }
1236 
1237 static const uint8_t vttodt_tab[ ] = {
1238 	[VNON]	=	DT_UNKNOWN,
1239 	[VREG]	=	DT_REG,
1240 	[VDIR]	=	DT_DIR,
1241 	[VBLK]	=	DT_BLK,
1242 	[VCHR]	=	DT_CHR,
1243 	[VLNK]	=	DT_LNK,
1244 	[VSOCK]	=	DT_SOCK,
1245 	[VFIFO]	=	DT_FIFO,
1246 	[VBAD]	=	DT_UNKNOWN
1247 };
1248 
1249 uint8_t
1250 vtype2dt(enum vtype vt)
1251 {
1252 
1253 	CTASSERT(VBAD == __arraycount(vttodt_tab) - 1);
1254 	return vttodt_tab[vt];
1255 }
1256 
1257 int
1258 VFS_MOUNT(struct mount *mp, const char *a, void *b, size_t *c)
1259 {
1260 	int error;
1261 
1262 	KERNEL_LOCK(1, NULL);
1263 	error = (*(mp->mnt_op->vfs_mount))(mp, a, b, c);
1264 	KERNEL_UNLOCK_ONE(NULL);
1265 
1266 	return error;
1267 }
1268 
1269 int
1270 VFS_START(struct mount *mp, int a)
1271 {
1272 	int error;
1273 
1274 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1275 		KERNEL_LOCK(1, NULL);
1276 	}
1277 	error = (*(mp->mnt_op->vfs_start))(mp, a);
1278 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1279 		KERNEL_UNLOCK_ONE(NULL);
1280 	}
1281 
1282 	return error;
1283 }
1284 
1285 int
1286 VFS_UNMOUNT(struct mount *mp, int a)
1287 {
1288 	int error;
1289 
1290 	KERNEL_LOCK(1, NULL);
1291 	error = (*(mp->mnt_op->vfs_unmount))(mp, a);
1292 	KERNEL_UNLOCK_ONE(NULL);
1293 
1294 	return error;
1295 }
1296 
1297 int
1298 VFS_ROOT(struct mount *mp, struct vnode **a)
1299 {
1300 	int error;
1301 
1302 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1303 		KERNEL_LOCK(1, NULL);
1304 	}
1305 	error = (*(mp->mnt_op->vfs_root))(mp, a);
1306 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1307 		KERNEL_UNLOCK_ONE(NULL);
1308 	}
1309 
1310 	return error;
1311 }
1312 
1313 int
1314 VFS_QUOTACTL(struct mount *mp, struct quotactl_args *args)
1315 {
1316 	int error;
1317 
1318 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1319 		KERNEL_LOCK(1, NULL);
1320 	}
1321 	error = (*(mp->mnt_op->vfs_quotactl))(mp, args);
1322 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1323 		KERNEL_UNLOCK_ONE(NULL);
1324 	}
1325 
1326 	return error;
1327 }
1328 
1329 int
1330 VFS_STATVFS(struct mount *mp, struct statvfs *a)
1331 {
1332 	int error;
1333 
1334 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1335 		KERNEL_LOCK(1, NULL);
1336 	}
1337 	error = (*(mp->mnt_op->vfs_statvfs))(mp, a);
1338 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1339 		KERNEL_UNLOCK_ONE(NULL);
1340 	}
1341 
1342 	return error;
1343 }
1344 
1345 int
1346 VFS_SYNC(struct mount *mp, int a, struct kauth_cred *b)
1347 {
1348 	int error;
1349 
1350 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1351 		KERNEL_LOCK(1, NULL);
1352 	}
1353 	error = (*(mp->mnt_op->vfs_sync))(mp, a, b);
1354 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1355 		KERNEL_UNLOCK_ONE(NULL);
1356 	}
1357 
1358 	return error;
1359 }
1360 
1361 int
1362 VFS_FHTOVP(struct mount *mp, struct fid *a, struct vnode **b)
1363 {
1364 	int error;
1365 
1366 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1367 		KERNEL_LOCK(1, NULL);
1368 	}
1369 	error = (*(mp->mnt_op->vfs_fhtovp))(mp, a, b);
1370 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1371 		KERNEL_UNLOCK_ONE(NULL);
1372 	}
1373 
1374 	return error;
1375 }
1376 
1377 int
1378 VFS_VPTOFH(struct vnode *vp, struct fid *a, size_t *b)
1379 {
1380 	int error;
1381 
1382 	if ((vp->v_vflag & VV_MPSAFE) == 0) {
1383 		KERNEL_LOCK(1, NULL);
1384 	}
1385 	error = (*(vp->v_mount->mnt_op->vfs_vptofh))(vp, a, b);
1386 	if ((vp->v_vflag & VV_MPSAFE) == 0) {
1387 		KERNEL_UNLOCK_ONE(NULL);
1388 	}
1389 
1390 	return error;
1391 }
1392 
1393 int
1394 VFS_SNAPSHOT(struct mount *mp, struct vnode *a, struct timespec *b)
1395 {
1396 	int error;
1397 
1398 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1399 		KERNEL_LOCK(1, NULL);
1400 	}
1401 	error = (*(mp->mnt_op->vfs_snapshot))(mp, a, b);
1402 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1403 		KERNEL_UNLOCK_ONE(NULL);
1404 	}
1405 
1406 	return error;
1407 }
1408 
1409 int
1410 VFS_EXTATTRCTL(struct mount *mp, int a, struct vnode *b, int c, const char *d)
1411 {
1412 	int error;
1413 
1414 	KERNEL_LOCK(1, NULL);		/* XXXSMP check ffs */
1415 	error = (*(mp->mnt_op->vfs_extattrctl))(mp, a, b, c, d);
1416 	KERNEL_UNLOCK_ONE(NULL);	/* XXX */
1417 
1418 	return error;
1419 }
1420 
1421 int
1422 VFS_SUSPENDCTL(struct mount *mp, int a)
1423 {
1424 	int error;
1425 
1426 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1427 		KERNEL_LOCK(1, NULL);
1428 	}
1429 	error = (*(mp->mnt_op->vfs_suspendctl))(mp, a);
1430 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
1431 		KERNEL_UNLOCK_ONE(NULL);
1432 	}
1433 
1434 	return error;
1435 }
1436 
1437 #if defined(DDB) || defined(DEBUGPRINT)
1438 static const char buf_flagbits[] = BUF_FLAGBITS;
1439 
1440 void
1441 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...))
1442 {
1443 	char bf[1024];
1444 
1445 	(*pr)("  vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" rawblkno 0x%"
1446 	    PRIx64 " dev 0x%x\n",
1447 	    bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_rawblkno, bp->b_dev);
1448 
1449 	snprintb(bf, sizeof(bf),
1450 	    buf_flagbits, bp->b_flags | bp->b_oflags | bp->b_cflags);
1451 	(*pr)("  error %d flags 0x%s\n", bp->b_error, bf);
1452 
1453 	(*pr)("  bufsize 0x%lx bcount 0x%lx resid 0x%lx\n",
1454 		  bp->b_bufsize, bp->b_bcount, bp->b_resid);
1455 	(*pr)("  data %p saveaddr %p\n",
1456 		  bp->b_data, bp->b_saveaddr);
1457 	(*pr)("  iodone %p objlock %p\n", bp->b_iodone, bp->b_objlock);
1458 }
1459 
1460 void
1461 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...))
1462 {
1463 	char bf[256];
1464 
1465 	uvm_object_printit(&vp->v_uobj, full, pr);
1466 	snprintb(bf, sizeof(bf),
1467 	    vnode_flagbits, vp->v_iflag | vp->v_vflag | vp->v_uflag);
1468 	(*pr)("\nVNODE flags %s\n", bf);
1469 	(*pr)("mp %p numoutput %d size 0x%llx writesize 0x%llx\n",
1470 	      vp->v_mount, vp->v_numoutput, vp->v_size, vp->v_writesize);
1471 
1472 	(*pr)("data %p writecount %ld holdcnt %ld\n",
1473 	      vp->v_data, vp->v_writecount, vp->v_holdcnt);
1474 
1475 	(*pr)("tag %s(%d) type %s(%d) mount %p typedata %p\n",
1476 	      ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
1477 	      ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
1478 	      vp->v_mount, vp->v_mountedhere);
1479 
1480 	(*pr)("v_lock %p\n", &vp->v_lock);
1481 
1482 	if (full) {
1483 		struct buf *bp;
1484 
1485 		(*pr)("clean bufs:\n");
1486 		LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
1487 			(*pr)(" bp %p\n", bp);
1488 			vfs_buf_print(bp, full, pr);
1489 		}
1490 
1491 		(*pr)("dirty bufs:\n");
1492 		LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
1493 			(*pr)(" bp %p\n", bp);
1494 			vfs_buf_print(bp, full, pr);
1495 		}
1496 	}
1497 }
1498 
1499 void
1500 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...))
1501 {
1502 	char sbuf[256];
1503 
1504 	(*pr)("vnodecovered = %p data = %p\n",
1505 			mp->mnt_vnodecovered,mp->mnt_data);
1506 
1507 	(*pr)("fs_bshift %d dev_bshift = %d\n",
1508 			mp->mnt_fs_bshift,mp->mnt_dev_bshift);
1509 
1510 	snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_flag);
1511 	(*pr)("flag = %s\n", sbuf);
1512 
1513 	snprintb(sbuf, sizeof(sbuf), __IMNT_FLAG_BITS, mp->mnt_iflag);
1514 	(*pr)("iflag = %s\n", sbuf);
1515 
1516 	(*pr)("refcnt = %d unmounting @ %p updating @ %p\n", mp->mnt_refcnt,
1517 	    &mp->mnt_unmounting, &mp->mnt_updating);
1518 
1519 	(*pr)("statvfs cache:\n");
1520 	(*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize);
1521 	(*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize);
1522 	(*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize);
1523 
1524 	(*pr)("\tblocks = %"PRIu64"\n",mp->mnt_stat.f_blocks);
1525 	(*pr)("\tbfree = %"PRIu64"\n",mp->mnt_stat.f_bfree);
1526 	(*pr)("\tbavail = %"PRIu64"\n",mp->mnt_stat.f_bavail);
1527 	(*pr)("\tbresvd = %"PRIu64"\n",mp->mnt_stat.f_bresvd);
1528 
1529 	(*pr)("\tfiles = %"PRIu64"\n",mp->mnt_stat.f_files);
1530 	(*pr)("\tffree = %"PRIu64"\n",mp->mnt_stat.f_ffree);
1531 	(*pr)("\tfavail = %"PRIu64"\n",mp->mnt_stat.f_favail);
1532 	(*pr)("\tfresvd = %"PRIu64"\n",mp->mnt_stat.f_fresvd);
1533 
1534 	(*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n",
1535 			mp->mnt_stat.f_fsidx.__fsid_val[0],
1536 			mp->mnt_stat.f_fsidx.__fsid_val[1]);
1537 
1538 	(*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner);
1539 	(*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax);
1540 
1541 	snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_stat.f_flag);
1542 
1543 	(*pr)("\tflag = %s\n",sbuf);
1544 	(*pr)("\tsyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_syncwrites);
1545 	(*pr)("\tasyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_asyncwrites);
1546 	(*pr)("\tsyncreads = %" PRIu64 "\n",mp->mnt_stat.f_syncreads);
1547 	(*pr)("\tasyncreads = %" PRIu64 "\n",mp->mnt_stat.f_asyncreads);
1548 	(*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename);
1549 	(*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname);
1550 	(*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname);
1551 
1552 	{
1553 		int cnt = 0;
1554 		struct vnode *vp;
1555 		(*pr)("locked vnodes =");
1556 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1557 			if (VOP_ISLOCKED(vp)) {
1558 				if ((++cnt % 6) == 0) {
1559 					(*pr)(" %p,\n\t", vp);
1560 				} else {
1561 					(*pr)(" %p,", vp);
1562 				}
1563 			}
1564 		}
1565 		(*pr)("\n");
1566 	}
1567 
1568 	if (full) {
1569 		int cnt = 0;
1570 		struct vnode *vp;
1571 		(*pr)("all vnodes =");
1572 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1573 			if (!TAILQ_NEXT(vp, v_mntvnodes)) {
1574 				(*pr)(" %p", vp);
1575 			} else if ((++cnt % 6) == 0) {
1576 				(*pr)(" %p,\n\t", vp);
1577 			} else {
1578 				(*pr)(" %p,", vp);
1579 			}
1580 		}
1581 		(*pr)("\n", vp);
1582 	}
1583 }
1584 
1585 /*
1586  * List all of the locked vnodes in the system.
1587  */
1588 void printlockedvnodes(void);
1589 
1590 void
1591 printlockedvnodes(void)
1592 {
1593 	struct mount *mp, *nmp;
1594 	struct vnode *vp;
1595 
1596 	printf("Locked vnodes\n");
1597 	mutex_enter(&mountlist_lock);
1598 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1599 		if (vfs_busy(mp, &nmp)) {
1600 			continue;
1601 		}
1602 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1603 			if (VOP_ISLOCKED(vp))
1604 				vprint(NULL, vp);
1605 		}
1606 		mutex_enter(&mountlist_lock);
1607 		vfs_unbusy(mp, false, &nmp);
1608 	}
1609 	mutex_exit(&mountlist_lock);
1610 }
1611 
1612 #endif /* DDB || DEBUGPRINT */
1613