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