Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: vfs_subr.c,v 1.459 2017/01/11 14:52:02 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.459 2017/01/11 14:52:02 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 	synclist_t *slp;
    763 	struct vnode *vp;
    764 	struct mount *mp, *nmp;
    765 	time_t starttime;
    766 	bool synced;
    767 
    768 	for (;;) {
    769 		mutex_enter(&syncer_mutex);
    770 
    771 		starttime = time_second;
    772 
    773 		/*
    774 		 * Sync mounts whose dirty time has expired.
    775 		 */
    776 		mutex_enter(&mountlist_lock);
    777 		for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
    778 			if ((mp->mnt_iflag & IMNT_ONWORKLIST) == 0 ||
    779 			    mp->mnt_synclist_slot != syncer_delayno) {
    780 				nmp = TAILQ_NEXT(mp, mnt_list);
    781 				continue;
    782 			}
    783 			mp->mnt_synclist_slot = sync_delay_slot(sync_delay(mp));
    784 			if (vfs_busy(mp, &nmp))
    785 				continue;
    786 			if (fstrans_start_nowait(mp, FSTRANS_SHARED) == 0) {
    787 				VFS_SYNC(mp, MNT_LAZY, curlwp->l_cred);
    788 				fstrans_done(mp);
    789 			}
    790 			vfs_unbusy(mp, false, &nmp);
    791 		}
    792 		mutex_exit(&mountlist_lock);
    793 
    794 		mutex_enter(&syncer_data_lock);
    795 
    796 		/*
    797 		 * Push files whose dirty time has expired.
    798 		 */
    799 		slp = &syncer_workitem_pending[syncer_delayno];
    800 		syncer_delayno += 1;
    801 		if (syncer_delayno >= syncer_last)
    802 			syncer_delayno = 0;
    803 
    804 		while ((vp = VIMPL_TO_VNODE(TAILQ_FIRST(slp))) != NULL) {
    805 			synced = lazy_sync_vnode(vp);
    806 
    807 			/*
    808 			 * XXX The vnode may have been recycled, in which
    809 			 * case it may have a new identity.
    810 			 */
    811 			if (VIMPL_TO_VNODE(TAILQ_FIRST(slp)) == vp) {
    812 				/*
    813 				 * Put us back on the worklist.  The worklist
    814 				 * routine will remove us from our current
    815 				 * position and then add us back in at a later
    816 				 * position.
    817 				 *
    818 				 * Try again sooner rather than later if
    819 				 * we were unable to lock the vnode.  Lock
    820 				 * failure should not prevent us from doing
    821 				 * the sync "soon".
    822 				 *
    823 				 * If we locked it yet arrive here, it's
    824 				 * likely that lazy sync is in progress and
    825 				 * so the vnode still has dirty metadata.
    826 				 * syncdelay is mainly to get this vnode out
    827 				 * of the way so we do not consider it again
    828 				 * "soon" in this loop, so the delay time is
    829 				 * not critical as long as it is not "soon".
    830 				 * While write-back strategy is the file
    831 				 * system's domain, we expect write-back to
    832 				 * occur no later than syncdelay seconds
    833 				 * into the future.
    834 				 */
    835 				vn_syncer_add1(vp,
    836 				    synced ? syncdelay : lockdelay);
    837 			}
    838 		}
    839 		mutex_exit(&syncer_mutex);
    840 
    841 		/*
    842 		 * If it has taken us less than a second to process the
    843 		 * current work, then wait.  Otherwise start right over
    844 		 * again.  We can still lose time if any single round
    845 		 * takes more than two seconds, but it does not really
    846 		 * matter as we are just trying to generally pace the
    847 		 * filesystem activity.
    848 		 */
    849 		if (time_second == starttime) {
    850 			kpause("syncer", false, hz, &syncer_data_lock);
    851 		}
    852 		mutex_exit(&syncer_data_lock);
    853 	}
    854 }
    855 
    856 static void
    857 sysctl_vfs_syncfs_setup(struct sysctllog **clog)
    858 {
    859 	const struct sysctlnode *rnode, *cnode;
    860 
    861 	sysctl_createv(clog, 0, NULL, &rnode,
    862 			CTLFLAG_PERMANENT,
    863 			CTLTYPE_NODE, "sync",
    864 			SYSCTL_DESCR("syncer options"),
    865 			NULL, 0, NULL, 0,
    866 			CTL_VFS, CTL_CREATE, CTL_EOL);
    867 
    868 	sysctl_createv(clog, 0, &rnode, &cnode,
    869 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    870 			CTLTYPE_QUAD, "delay",
    871 			SYSCTL_DESCR("max time to delay syncing data"),
    872 			NULL, 0, &syncdelay, 0,
    873 			CTL_CREATE, CTL_EOL);
    874 
    875 	sysctl_createv(clog, 0, &rnode, &cnode,
    876 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    877 			CTLTYPE_QUAD, "filedelay",
    878 			SYSCTL_DESCR("time to delay syncing files"),
    879 			NULL, 0, &filedelay, 0,
    880 			CTL_CREATE, CTL_EOL);
    881 
    882 	sysctl_createv(clog, 0, &rnode, &cnode,
    883 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    884 			CTLTYPE_QUAD, "dirdelay",
    885 			SYSCTL_DESCR("time to delay syncing directories"),
    886 			NULL, 0, &dirdelay, 0,
    887 			CTL_CREATE, CTL_EOL);
    888 
    889 	sysctl_createv(clog, 0, &rnode, &cnode,
    890 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    891 			CTLTYPE_QUAD, "metadelay",
    892 			SYSCTL_DESCR("time to delay syncing metadata"),
    893 			NULL, 0, &metadelay, 0,
    894 			CTL_CREATE, CTL_EOL);
    895 }
    896 
    897 /*
    898  * sysctl helper routine to return list of supported fstypes
    899  */
    900 int
    901 sysctl_vfs_generic_fstypes(SYSCTLFN_ARGS)
    902 {
    903 	char bf[sizeof(((struct statvfs *)NULL)->f_fstypename)];
    904 	char *where = oldp;
    905 	struct vfsops *v;
    906 	size_t needed, left, slen;
    907 	int error, first;
    908 
    909 	if (newp != NULL)
    910 		return (EPERM);
    911 	if (namelen != 0)
    912 		return (EINVAL);
    913 
    914 	first = 1;
    915 	error = 0;
    916 	needed = 0;
    917 	left = *oldlenp;
    918 
    919 	sysctl_unlock();
    920 	mutex_enter(&vfs_list_lock);
    921 	LIST_FOREACH(v, &vfs_list, vfs_list) {
    922 		if (where == NULL)
    923 			needed += strlen(v->vfs_name) + 1;
    924 		else {
    925 			memset(bf, 0, sizeof(bf));
    926 			if (first) {
    927 				strncpy(bf, v->vfs_name, sizeof(bf));
    928 				first = 0;
    929 			} else {
    930 				bf[0] = ' ';
    931 				strncpy(bf + 1, v->vfs_name, sizeof(bf) - 1);
    932 			}
    933 			bf[sizeof(bf)-1] = '\0';
    934 			slen = strlen(bf);
    935 			if (left < slen + 1)
    936 				break;
    937 			v->vfs_refcount++;
    938 			mutex_exit(&vfs_list_lock);
    939 			/* +1 to copy out the trailing NUL byte */
    940 			error = copyout(bf, where, slen + 1);
    941 			mutex_enter(&vfs_list_lock);
    942 			v->vfs_refcount--;
    943 			if (error)
    944 				break;
    945 			where += slen;
    946 			needed += slen;
    947 			left -= slen;
    948 		}
    949 	}
    950 	mutex_exit(&vfs_list_lock);
    951 	sysctl_relock();
    952 	*oldlenp = needed;
    953 	return (error);
    954 }
    955 
    956 int kinfo_vdebug = 1;
    957 int kinfo_vgetfailed;
    958 
    959 #define KINFO_VNODESLOP	10
    960 
    961 /*
    962  * Dump vnode list (via sysctl).
    963  * Copyout address of vnode followed by vnode.
    964  */
    965 int
    966 sysctl_kern_vnode(SYSCTLFN_ARGS)
    967 {
    968 	char *where = oldp;
    969 	size_t *sizep = oldlenp;
    970 	struct mount *mp, *nmp;
    971 	vnode_t *vp, vbuf;
    972 	struct vnode_iterator *marker;
    973 	char *bp = where;
    974 	char *ewhere;
    975 	int error;
    976 
    977 	if (namelen != 0)
    978 		return (EOPNOTSUPP);
    979 	if (newp != NULL)
    980 		return (EPERM);
    981 
    982 #define VPTRSZ	sizeof(vnode_t *)
    983 #define VNODESZ	sizeof(vnode_t)
    984 	if (where == NULL) {
    985 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
    986 		return (0);
    987 	}
    988 	ewhere = where + *sizep;
    989 
    990 	sysctl_unlock();
    991 	mutex_enter(&mountlist_lock);
    992 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
    993 		if (vfs_busy(mp, &nmp)) {
    994 			continue;
    995 		}
    996 		vfs_vnode_iterator_init(mp, &marker);
    997 		while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
    998 			if (bp + VPTRSZ + VNODESZ > ewhere) {
    999 				vrele(vp);
   1000 				vfs_vnode_iterator_destroy(marker);
   1001 				vfs_unbusy(mp, false, NULL);
   1002 				sysctl_relock();
   1003 				*sizep = bp - where;
   1004 				return (ENOMEM);
   1005 			}
   1006 			memcpy(&vbuf, vp, VNODESZ);
   1007 			if ((error = copyout(&vp, bp, VPTRSZ)) ||
   1008 			    (error = copyout(&vbuf, bp + VPTRSZ, VNODESZ))) {
   1009 				vrele(vp);
   1010 				vfs_vnode_iterator_destroy(marker);
   1011 				vfs_unbusy(mp, false, NULL);
   1012 				sysctl_relock();
   1013 				return (error);
   1014 			}
   1015 			vrele(vp);
   1016 			bp += VPTRSZ + VNODESZ;
   1017 		}
   1018 		vfs_vnode_iterator_destroy(marker);
   1019 		vfs_unbusy(mp, false, &nmp);
   1020 	}
   1021 	mutex_exit(&mountlist_lock);
   1022 	sysctl_relock();
   1023 
   1024 	*sizep = bp - where;
   1025 	return (0);
   1026 }
   1027 
   1028 /*
   1029  * Set vnode attributes to VNOVAL
   1030  */
   1031 void
   1032 vattr_null(struct vattr *vap)
   1033 {
   1034 
   1035 	memset(vap, 0, sizeof(*vap));
   1036 
   1037 	vap->va_type = VNON;
   1038 
   1039 	/*
   1040 	 * Assign individually so that it is safe even if size and
   1041 	 * sign of each member are varied.
   1042 	 */
   1043 	vap->va_mode = VNOVAL;
   1044 	vap->va_nlink = VNOVAL;
   1045 	vap->va_uid = VNOVAL;
   1046 	vap->va_gid = VNOVAL;
   1047 	vap->va_fsid = VNOVAL;
   1048 	vap->va_fileid = VNOVAL;
   1049 	vap->va_size = VNOVAL;
   1050 	vap->va_blocksize = VNOVAL;
   1051 	vap->va_atime.tv_sec =
   1052 	    vap->va_mtime.tv_sec =
   1053 	    vap->va_ctime.tv_sec =
   1054 	    vap->va_birthtime.tv_sec = VNOVAL;
   1055 	vap->va_atime.tv_nsec =
   1056 	    vap->va_mtime.tv_nsec =
   1057 	    vap->va_ctime.tv_nsec =
   1058 	    vap->va_birthtime.tv_nsec = VNOVAL;
   1059 	vap->va_gen = VNOVAL;
   1060 	vap->va_flags = VNOVAL;
   1061 	vap->va_rdev = VNOVAL;
   1062 	vap->va_bytes = VNOVAL;
   1063 }
   1064 
   1065 /*
   1066  * Vnode state to string.
   1067  */
   1068 const char *
   1069 vstate_name(enum vnode_state state)
   1070 {
   1071 
   1072 	switch (state) {
   1073 	case VS_MARKER:
   1074 		return "MARKER";
   1075 	case VS_LOADING:
   1076 		return "LOADING";
   1077 	case VS_ACTIVE:
   1078 		return "ACTIVE";
   1079 	case VS_BLOCKED:
   1080 		return "BLOCKED";
   1081 	case VS_RECLAIMING:
   1082 		return "RECLAIMING";
   1083 	case VS_RECLAIMED:
   1084 		return "RECLAIMED";
   1085 	default:
   1086 		return "ILLEGAL";
   1087 	}
   1088 }
   1089 
   1090 /*
   1091  * Print a description of a vnode (common part).
   1092  */
   1093 static void
   1094 vprint_common(struct vnode *vp, const char *prefix,
   1095     void (*pr)(const char *, ...) __printflike(1, 2))
   1096 {
   1097 	int n;
   1098 	char bf[96];
   1099 	const uint8_t *cp;
   1100 	vnode_impl_t *vip;
   1101 	const char * const vnode_tags[] = { VNODE_TAGS };
   1102 	const char * const vnode_types[] = { VNODE_TYPES };
   1103 	const char vnode_flagbits[] = VNODE_FLAGBITS;
   1104 
   1105 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
   1106 #define ARRAY_PRINT(idx, arr) \
   1107     ((unsigned int)(idx) < ARRAY_SIZE(arr) ? (arr)[(idx)] : "UNKNOWN")
   1108 
   1109 	vip = VNODE_TO_VIMPL(vp);
   1110 
   1111 	snprintb(bf, sizeof(bf),
   1112 	    vnode_flagbits, vp->v_iflag | vp->v_vflag | vp->v_uflag);
   1113 
   1114 	(*pr)("vnode %p flags %s\n", vp, bf);
   1115 	(*pr)("%stag %s(%d) type %s(%d) mount %p typedata %p\n", prefix,
   1116 	    ARRAY_PRINT(vp->v_tag, vnode_tags), vp->v_tag,
   1117 	    ARRAY_PRINT(vp->v_type, vnode_types), vp->v_type,
   1118 	    vp->v_mount, vp->v_mountedhere);
   1119 	(*pr)("%susecount %d writecount %d holdcount %d\n", prefix,
   1120 	    vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
   1121 	(*pr)("%ssize %" PRIx64 " writesize %" PRIx64 " numoutput %d\n",
   1122 	    prefix, vp->v_size, vp->v_writesize, vp->v_numoutput);
   1123 	(*pr)("%sdata %p lock %p\n", prefix, vp->v_data, &vip->vi_lock);
   1124 
   1125 	(*pr)("%sstate %s key(%p %zd)", prefix, vstate_name(vip->vi_state),
   1126 	    vip->vi_key.vk_mount, vip->vi_key.vk_key_len);
   1127 	n = vip->vi_key.vk_key_len;
   1128 	cp = vip->vi_key.vk_key;
   1129 	while (n-- > 0)
   1130 		(*pr)(" %02x", *cp++);
   1131 	(*pr)("\n");
   1132 	(*pr)("%slrulisthd %p\n", prefix, vip->vi_lrulisthd);
   1133 
   1134 #undef ARRAY_PRINT
   1135 #undef ARRAY_SIZE
   1136 }
   1137 
   1138 /*
   1139  * Print out a description of a vnode.
   1140  */
   1141 void
   1142 vprint(const char *label, struct vnode *vp)
   1143 {
   1144 
   1145 	if (label != NULL)
   1146 		printf("%s: ", label);
   1147 	vprint_common(vp, "\t", printf);
   1148 	if (vp->v_data != NULL) {
   1149 		printf("\t");
   1150 		VOP_PRINT(vp);
   1151 	}
   1152 }
   1153 
   1154 /* Deprecated. Kept for KPI compatibility. */
   1155 int
   1156 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
   1157     mode_t acc_mode, kauth_cred_t cred)
   1158 {
   1159 
   1160 #ifdef DIAGNOSTIC
   1161 	printf("vaccess: deprecated interface used.\n");
   1162 #endif /* DIAGNOSTIC */
   1163 
   1164 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(acc_mode,
   1165 	    type, file_mode), NULL /* This may panic. */, NULL,
   1166 	    genfs_can_access(type, file_mode, uid, gid, acc_mode, cred));
   1167 }
   1168 
   1169 /*
   1170  * Given a file system name, look up the vfsops for that
   1171  * file system, or return NULL if file system isn't present
   1172  * in the kernel.
   1173  */
   1174 struct vfsops *
   1175 vfs_getopsbyname(const char *name)
   1176 {
   1177 	struct vfsops *v;
   1178 
   1179 	mutex_enter(&vfs_list_lock);
   1180 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1181 		if (strcmp(v->vfs_name, name) == 0)
   1182 			break;
   1183 	}
   1184 	if (v != NULL)
   1185 		v->vfs_refcount++;
   1186 	mutex_exit(&vfs_list_lock);
   1187 
   1188 	return (v);
   1189 }
   1190 
   1191 void
   1192 copy_statvfs_info(struct statvfs *sbp, const struct mount *mp)
   1193 {
   1194 	const struct statvfs *mbp;
   1195 
   1196 	if (sbp == (mbp = &mp->mnt_stat))
   1197 		return;
   1198 
   1199 	(void)memcpy(&sbp->f_fsidx, &mbp->f_fsidx, sizeof(sbp->f_fsidx));
   1200 	sbp->f_fsid = mbp->f_fsid;
   1201 	sbp->f_owner = mbp->f_owner;
   1202 	sbp->f_flag = mbp->f_flag;
   1203 	sbp->f_syncwrites = mbp->f_syncwrites;
   1204 	sbp->f_asyncwrites = mbp->f_asyncwrites;
   1205 	sbp->f_syncreads = mbp->f_syncreads;
   1206 	sbp->f_asyncreads = mbp->f_asyncreads;
   1207 	(void)memcpy(sbp->f_spare, mbp->f_spare, sizeof(mbp->f_spare));
   1208 	(void)memcpy(sbp->f_fstypename, mbp->f_fstypename,
   1209 	    sizeof(sbp->f_fstypename));
   1210 	(void)memcpy(sbp->f_mntonname, mbp->f_mntonname,
   1211 	    sizeof(sbp->f_mntonname));
   1212 	(void)memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname,
   1213 	    sizeof(sbp->f_mntfromname));
   1214 	sbp->f_namemax = mbp->f_namemax;
   1215 }
   1216 
   1217 int
   1218 set_statvfs_info(const char *onp, int ukon, const char *fromp, int ukfrom,
   1219     const char *vfsname, struct mount *mp, struct lwp *l)
   1220 {
   1221 	int error;
   1222 	size_t size;
   1223 	struct statvfs *sfs = &mp->mnt_stat;
   1224 	int (*fun)(const void *, void *, size_t, size_t *);
   1225 
   1226 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsname,
   1227 	    sizeof(mp->mnt_stat.f_fstypename));
   1228 
   1229 	if (onp) {
   1230 		struct cwdinfo *cwdi = l->l_proc->p_cwdi;
   1231 		fun = (ukon == UIO_SYSSPACE) ? copystr : copyinstr;
   1232 		if (cwdi->cwdi_rdir != NULL) {
   1233 			size_t len;
   1234 			char *bp;
   1235 			char *path = PNBUF_GET();
   1236 
   1237 			bp = path + MAXPATHLEN;
   1238 			*--bp = '\0';
   1239 			rw_enter(&cwdi->cwdi_lock, RW_READER);
   1240 			error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp,
   1241 			    path, MAXPATHLEN / 2, 0, l);
   1242 			rw_exit(&cwdi->cwdi_lock);
   1243 			if (error) {
   1244 				PNBUF_PUT(path);
   1245 				return error;
   1246 			}
   1247 
   1248 			len = strlen(bp);
   1249 			if (len > sizeof(sfs->f_mntonname) - 1)
   1250 				len = sizeof(sfs->f_mntonname) - 1;
   1251 			(void)strncpy(sfs->f_mntonname, bp, len);
   1252 			PNBUF_PUT(path);
   1253 
   1254 			if (len < sizeof(sfs->f_mntonname) - 1) {
   1255 				error = (*fun)(onp, &sfs->f_mntonname[len],
   1256 				    sizeof(sfs->f_mntonname) - len - 1, &size);
   1257 				if (error)
   1258 					return error;
   1259 				size += len;
   1260 			} else {
   1261 				size = len;
   1262 			}
   1263 		} else {
   1264 			error = (*fun)(onp, &sfs->f_mntonname,
   1265 			    sizeof(sfs->f_mntonname) - 1, &size);
   1266 			if (error)
   1267 				return error;
   1268 		}
   1269 		(void)memset(sfs->f_mntonname + size, 0,
   1270 		    sizeof(sfs->f_mntonname) - size);
   1271 	}
   1272 
   1273 	if (fromp) {
   1274 		fun = (ukfrom == UIO_SYSSPACE) ? copystr : copyinstr;
   1275 		error = (*fun)(fromp, sfs->f_mntfromname,
   1276 		    sizeof(sfs->f_mntfromname) - 1, &size);
   1277 		if (error)
   1278 			return error;
   1279 		(void)memset(sfs->f_mntfromname + size, 0,
   1280 		    sizeof(sfs->f_mntfromname) - size);
   1281 	}
   1282 	return 0;
   1283 }
   1284 
   1285 void
   1286 vfs_timestamp(struct timespec *ts)
   1287 {
   1288 
   1289 	nanotime(ts);
   1290 }
   1291 
   1292 time_t	rootfstime;			/* recorded root fs time, if known */
   1293 void
   1294 setrootfstime(time_t t)
   1295 {
   1296 	rootfstime = t;
   1297 }
   1298 
   1299 static const uint8_t vttodt_tab[ ] = {
   1300 	[VNON]	=	DT_UNKNOWN,
   1301 	[VREG]	=	DT_REG,
   1302 	[VDIR]	=	DT_DIR,
   1303 	[VBLK]	=	DT_BLK,
   1304 	[VCHR]	=	DT_CHR,
   1305 	[VLNK]	=	DT_LNK,
   1306 	[VSOCK]	=	DT_SOCK,
   1307 	[VFIFO]	=	DT_FIFO,
   1308 	[VBAD]	=	DT_UNKNOWN
   1309 };
   1310 
   1311 uint8_t
   1312 vtype2dt(enum vtype vt)
   1313 {
   1314 
   1315 	CTASSERT(VBAD == __arraycount(vttodt_tab) - 1);
   1316 	return vttodt_tab[vt];
   1317 }
   1318 
   1319 int
   1320 VFS_MOUNT(struct mount *mp, const char *a, void *b, size_t *c)
   1321 {
   1322 	int error;
   1323 
   1324 	KERNEL_LOCK(1, NULL);
   1325 	error = (*(mp->mnt_op->vfs_mount))(mp, a, b, c);
   1326 	KERNEL_UNLOCK_ONE(NULL);
   1327 
   1328 	return error;
   1329 }
   1330 
   1331 int
   1332 VFS_START(struct mount *mp, int a)
   1333 {
   1334 	int error;
   1335 
   1336 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1337 		KERNEL_LOCK(1, NULL);
   1338 	}
   1339 	error = (*(mp->mnt_op->vfs_start))(mp, a);
   1340 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1341 		KERNEL_UNLOCK_ONE(NULL);
   1342 	}
   1343 
   1344 	return error;
   1345 }
   1346 
   1347 int
   1348 VFS_UNMOUNT(struct mount *mp, int a)
   1349 {
   1350 	int error;
   1351 
   1352 	KERNEL_LOCK(1, NULL);
   1353 	error = (*(mp->mnt_op->vfs_unmount))(mp, a);
   1354 	KERNEL_UNLOCK_ONE(NULL);
   1355 
   1356 	return error;
   1357 }
   1358 
   1359 int
   1360 VFS_ROOT(struct mount *mp, struct vnode **a)
   1361 {
   1362 	int error;
   1363 
   1364 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1365 		KERNEL_LOCK(1, NULL);
   1366 	}
   1367 	error = (*(mp->mnt_op->vfs_root))(mp, a);
   1368 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1369 		KERNEL_UNLOCK_ONE(NULL);
   1370 	}
   1371 
   1372 	return error;
   1373 }
   1374 
   1375 int
   1376 VFS_QUOTACTL(struct mount *mp, struct quotactl_args *args)
   1377 {
   1378 	int error;
   1379 
   1380 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1381 		KERNEL_LOCK(1, NULL);
   1382 	}
   1383 	error = (*(mp->mnt_op->vfs_quotactl))(mp, args);
   1384 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1385 		KERNEL_UNLOCK_ONE(NULL);
   1386 	}
   1387 
   1388 	return error;
   1389 }
   1390 
   1391 int
   1392 VFS_STATVFS(struct mount *mp, struct statvfs *a)
   1393 {
   1394 	int error;
   1395 
   1396 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1397 		KERNEL_LOCK(1, NULL);
   1398 	}
   1399 	error = (*(mp->mnt_op->vfs_statvfs))(mp, a);
   1400 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1401 		KERNEL_UNLOCK_ONE(NULL);
   1402 	}
   1403 
   1404 	return error;
   1405 }
   1406 
   1407 int
   1408 VFS_SYNC(struct mount *mp, int a, struct kauth_cred *b)
   1409 {
   1410 	int error;
   1411 
   1412 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1413 		KERNEL_LOCK(1, NULL);
   1414 	}
   1415 	error = (*(mp->mnt_op->vfs_sync))(mp, a, b);
   1416 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1417 		KERNEL_UNLOCK_ONE(NULL);
   1418 	}
   1419 
   1420 	return error;
   1421 }
   1422 
   1423 int
   1424 VFS_FHTOVP(struct mount *mp, struct fid *a, struct vnode **b)
   1425 {
   1426 	int error;
   1427 
   1428 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1429 		KERNEL_LOCK(1, NULL);
   1430 	}
   1431 	error = (*(mp->mnt_op->vfs_fhtovp))(mp, a, b);
   1432 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1433 		KERNEL_UNLOCK_ONE(NULL);
   1434 	}
   1435 
   1436 	return error;
   1437 }
   1438 
   1439 int
   1440 VFS_VPTOFH(struct vnode *vp, struct fid *a, size_t *b)
   1441 {
   1442 	int error;
   1443 
   1444 	if ((vp->v_vflag & VV_MPSAFE) == 0) {
   1445 		KERNEL_LOCK(1, NULL);
   1446 	}
   1447 	error = (*(vp->v_mount->mnt_op->vfs_vptofh))(vp, a, b);
   1448 	if ((vp->v_vflag & VV_MPSAFE) == 0) {
   1449 		KERNEL_UNLOCK_ONE(NULL);
   1450 	}
   1451 
   1452 	return error;
   1453 }
   1454 
   1455 int
   1456 VFS_SNAPSHOT(struct mount *mp, struct vnode *a, struct timespec *b)
   1457 {
   1458 	int error;
   1459 
   1460 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1461 		KERNEL_LOCK(1, NULL);
   1462 	}
   1463 	error = (*(mp->mnt_op->vfs_snapshot))(mp, a, b);
   1464 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1465 		KERNEL_UNLOCK_ONE(NULL);
   1466 	}
   1467 
   1468 	return error;
   1469 }
   1470 
   1471 int
   1472 VFS_EXTATTRCTL(struct mount *mp, int a, struct vnode *b, int c, const char *d)
   1473 {
   1474 	int error;
   1475 
   1476 	KERNEL_LOCK(1, NULL);		/* XXXSMP check ffs */
   1477 	error = (*(mp->mnt_op->vfs_extattrctl))(mp, a, b, c, d);
   1478 	KERNEL_UNLOCK_ONE(NULL);	/* XXX */
   1479 
   1480 	return error;
   1481 }
   1482 
   1483 int
   1484 VFS_SUSPENDCTL(struct mount *mp, int a)
   1485 {
   1486 	int error;
   1487 
   1488 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1489 		KERNEL_LOCK(1, NULL);
   1490 	}
   1491 	error = (*(mp->mnt_op->vfs_suspendctl))(mp, a);
   1492 	if ((mp->mnt_iflag & IMNT_MPSAFE) == 0) {
   1493 		KERNEL_UNLOCK_ONE(NULL);
   1494 	}
   1495 
   1496 	return error;
   1497 }
   1498 
   1499 #if defined(DDB) || defined(DEBUGPRINT)
   1500 static const char buf_flagbits[] = BUF_FLAGBITS;
   1501 
   1502 void
   1503 vfs_buf_print(struct buf *bp, int full, void (*pr)(const char *, ...))
   1504 {
   1505 	char bf[1024];
   1506 
   1507 	(*pr)("  vp %p lblkno 0x%"PRIx64" blkno 0x%"PRIx64" rawblkno 0x%"
   1508 	    PRIx64 " dev 0x%x\n",
   1509 	    bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_rawblkno, bp->b_dev);
   1510 
   1511 	snprintb(bf, sizeof(bf),
   1512 	    buf_flagbits, bp->b_flags | bp->b_oflags | bp->b_cflags);
   1513 	(*pr)("  error %d flags 0x%s\n", bp->b_error, bf);
   1514 
   1515 	(*pr)("  bufsize 0x%lx bcount 0x%lx resid 0x%lx\n",
   1516 		  bp->b_bufsize, bp->b_bcount, bp->b_resid);
   1517 	(*pr)("  data %p saveaddr %p\n",
   1518 		  bp->b_data, bp->b_saveaddr);
   1519 	(*pr)("  iodone %p objlock %p\n", bp->b_iodone, bp->b_objlock);
   1520 }
   1521 
   1522 void
   1523 vfs_vnode_print(struct vnode *vp, int full, void (*pr)(const char *, ...))
   1524 {
   1525 
   1526 	uvm_object_printit(&vp->v_uobj, full, pr);
   1527 	(*pr)("\n");
   1528 	vprint_common(vp, "", printf);
   1529 	if (full) {
   1530 		struct buf *bp;
   1531 
   1532 		(*pr)("clean bufs:\n");
   1533 		LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
   1534 			(*pr)(" bp %p\n", bp);
   1535 			vfs_buf_print(bp, full, pr);
   1536 		}
   1537 
   1538 		(*pr)("dirty bufs:\n");
   1539 		LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
   1540 			(*pr)(" bp %p\n", bp);
   1541 			vfs_buf_print(bp, full, pr);
   1542 		}
   1543 	}
   1544 }
   1545 
   1546 void
   1547 vfs_vnode_lock_print(void *vlock, int full, void (*pr)(const char *, ...))
   1548 {
   1549 	struct mount *mp;
   1550 	vnode_impl_t *vip;
   1551 
   1552 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
   1553 		TAILQ_FOREACH(vip, &mp->mnt_vnodelist, vi_mntvnodes) {
   1554 			if (&vip->vi_lock != vlock)
   1555 				continue;
   1556 			vfs_vnode_print(VIMPL_TO_VNODE(vip), full, pr);
   1557 		}
   1558 	}
   1559 }
   1560 
   1561 void
   1562 vfs_mount_print(struct mount *mp, int full, void (*pr)(const char *, ...))
   1563 {
   1564 	char sbuf[256];
   1565 
   1566 	(*pr)("vnodecovered = %p data = %p\n",
   1567 			mp->mnt_vnodecovered,mp->mnt_data);
   1568 
   1569 	(*pr)("fs_bshift %d dev_bshift = %d\n",
   1570 			mp->mnt_fs_bshift,mp->mnt_dev_bshift);
   1571 
   1572 	snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_flag);
   1573 	(*pr)("flag = %s\n", sbuf);
   1574 
   1575 	snprintb(sbuf, sizeof(sbuf), __IMNT_FLAG_BITS, mp->mnt_iflag);
   1576 	(*pr)("iflag = %s\n", sbuf);
   1577 
   1578 	(*pr)("refcnt = %d unmounting @ %p updating @ %p\n", mp->mnt_refcnt,
   1579 	    &mp->mnt_unmounting, &mp->mnt_updating);
   1580 
   1581 	(*pr)("statvfs cache:\n");
   1582 	(*pr)("\tbsize = %lu\n",mp->mnt_stat.f_bsize);
   1583 	(*pr)("\tfrsize = %lu\n",mp->mnt_stat.f_frsize);
   1584 	(*pr)("\tiosize = %lu\n",mp->mnt_stat.f_iosize);
   1585 
   1586 	(*pr)("\tblocks = %"PRIu64"\n",mp->mnt_stat.f_blocks);
   1587 	(*pr)("\tbfree = %"PRIu64"\n",mp->mnt_stat.f_bfree);
   1588 	(*pr)("\tbavail = %"PRIu64"\n",mp->mnt_stat.f_bavail);
   1589 	(*pr)("\tbresvd = %"PRIu64"\n",mp->mnt_stat.f_bresvd);
   1590 
   1591 	(*pr)("\tfiles = %"PRIu64"\n",mp->mnt_stat.f_files);
   1592 	(*pr)("\tffree = %"PRIu64"\n",mp->mnt_stat.f_ffree);
   1593 	(*pr)("\tfavail = %"PRIu64"\n",mp->mnt_stat.f_favail);
   1594 	(*pr)("\tfresvd = %"PRIu64"\n",mp->mnt_stat.f_fresvd);
   1595 
   1596 	(*pr)("\tf_fsidx = { 0x%"PRIx32", 0x%"PRIx32" }\n",
   1597 			mp->mnt_stat.f_fsidx.__fsid_val[0],
   1598 			mp->mnt_stat.f_fsidx.__fsid_val[1]);
   1599 
   1600 	(*pr)("\towner = %"PRIu32"\n",mp->mnt_stat.f_owner);
   1601 	(*pr)("\tnamemax = %lu\n",mp->mnt_stat.f_namemax);
   1602 
   1603 	snprintb(sbuf, sizeof(sbuf), __MNT_FLAG_BITS, mp->mnt_stat.f_flag);
   1604 
   1605 	(*pr)("\tflag = %s\n",sbuf);
   1606 	(*pr)("\tsyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_syncwrites);
   1607 	(*pr)("\tasyncwrites = %" PRIu64 "\n",mp->mnt_stat.f_asyncwrites);
   1608 	(*pr)("\tsyncreads = %" PRIu64 "\n",mp->mnt_stat.f_syncreads);
   1609 	(*pr)("\tasyncreads = %" PRIu64 "\n",mp->mnt_stat.f_asyncreads);
   1610 	(*pr)("\tfstypename = %s\n",mp->mnt_stat.f_fstypename);
   1611 	(*pr)("\tmntonname = %s\n",mp->mnt_stat.f_mntonname);
   1612 	(*pr)("\tmntfromname = %s\n",mp->mnt_stat.f_mntfromname);
   1613 
   1614 	{
   1615 		int cnt = 0;
   1616 		vnode_t *vp;
   1617 		vnode_impl_t *vip;
   1618 		(*pr)("locked vnodes =");
   1619 		TAILQ_FOREACH(vip, &mp->mnt_vnodelist, vi_mntvnodes) {
   1620 			vp = VIMPL_TO_VNODE(vip);
   1621 			if (VOP_ISLOCKED(vp)) {
   1622 				if ((++cnt % 6) == 0) {
   1623 					(*pr)(" %p,\n\t", vp);
   1624 				} else {
   1625 					(*pr)(" %p,", vp);
   1626 				}
   1627 			}
   1628 		}
   1629 		(*pr)("\n");
   1630 	}
   1631 
   1632 	if (full) {
   1633 		int cnt = 0;
   1634 		vnode_t *vp;
   1635 		vnode_impl_t *vip;
   1636 		(*pr)("all vnodes =");
   1637 		TAILQ_FOREACH(vip, &mp->mnt_vnodelist, vi_mntvnodes) {
   1638 			vp = VIMPL_TO_VNODE(vip);
   1639 			if (!TAILQ_NEXT(vip, vi_mntvnodes)) {
   1640 				(*pr)(" %p", vp);
   1641 			} else if ((++cnt % 6) == 0) {
   1642 				(*pr)(" %p,\n\t", vp);
   1643 			} else {
   1644 				(*pr)(" %p,", vp);
   1645 			}
   1646 		}
   1647 		(*pr)("\n");
   1648 	}
   1649 }
   1650 
   1651 /*
   1652  * List all of the locked vnodes in the system.
   1653  */
   1654 void printlockedvnodes(void);
   1655 
   1656 void
   1657 printlockedvnodes(void)
   1658 {
   1659 	struct mount *mp, *nmp;
   1660 	vnode_t *vp;
   1661 	vnode_impl_t *vip;
   1662 
   1663 	printf("Locked vnodes\n");
   1664 	mutex_enter(&mountlist_lock);
   1665 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
   1666 		if (vfs_busy(mp, &nmp)) {
   1667 			continue;
   1668 		}
   1669 		TAILQ_FOREACH(vip, &mp->mnt_vnodelist, vi_mntvnodes) {
   1670 			vp = VIMPL_TO_VNODE(vip);
   1671 			if (VOP_ISLOCKED(vp))
   1672 				vprint(NULL, vp);
   1673 		}
   1674 		mutex_enter(&mountlist_lock);
   1675 		vfs_unbusy(mp, false, &nmp);
   1676 	}
   1677 	mutex_exit(&mountlist_lock);
   1678 }
   1679 
   1680 #endif /* DDB || DEBUGPRINT */
   1681