Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.239.2.3.4.1
      1 /*	$NetBSD: ffs_vfsops.c,v 1.239.2.3.4.1 2010/04/21 00:28:26 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wasabi Systems, Inc, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1989, 1991, 1993, 1994
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.239.2.3.4.1 2010/04/21 00:28:26 matt Exp $");
     65 
     66 #if defined(_KERNEL_OPT)
     67 #include "opt_ffs.h"
     68 #include "opt_quota.h"
     69 #include "opt_softdep.h"
     70 #include "opt_wapbl.h"
     71 #endif
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/namei.h>
     76 #include <sys/proc.h>
     77 #include <sys/kernel.h>
     78 #include <sys/vnode.h>
     79 #include <sys/socket.h>
     80 #include <sys/mount.h>
     81 #include <sys/buf.h>
     82 #include <sys/device.h>
     83 #include <sys/mbuf.h>
     84 #include <sys/file.h>
     85 #include <sys/disklabel.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/errno.h>
     88 #include <sys/malloc.h>
     89 #include <sys/pool.h>
     90 #include <sys/lock.h>
     91 #include <sys/sysctl.h>
     92 #include <sys/conf.h>
     93 #include <sys/kauth.h>
     94 #include <sys/wapbl.h>
     95 #include <sys/fstrans.h>
     96 #include <sys/module.h>
     97 
     98 #include <miscfs/genfs/genfs.h>
     99 #include <miscfs/specfs/specdev.h>
    100 
    101 #include <ufs/ufs/quota.h>
    102 #include <ufs/ufs/ufsmount.h>
    103 #include <ufs/ufs/inode.h>
    104 #include <ufs/ufs/dir.h>
    105 #include <ufs/ufs/ufs_extern.h>
    106 #include <ufs/ufs/ufs_bswap.h>
    107 #include <ufs/ufs/ufs_wapbl.h>
    108 
    109 #include <ufs/ffs/fs.h>
    110 #include <ufs/ffs/ffs_extern.h>
    111 
    112 MODULE(MODULE_CLASS_VFS, ffs, NULL);
    113 
    114 static int	ffs_vfs_fsync(vnode_t *, int);
    115 
    116 static struct sysctllog *ffs_sysctl_log;
    117 
    118 /* how many times ffs_init() was called */
    119 int ffs_initcount = 0;
    120 
    121 extern kmutex_t ufs_hashlock;
    122 
    123 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
    124 extern const struct vnodeopv_desc ffs_specop_opv_desc;
    125 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
    126 
    127 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
    128 	&ffs_vnodeop_opv_desc,
    129 	&ffs_specop_opv_desc,
    130 	&ffs_fifoop_opv_desc,
    131 	NULL,
    132 };
    133 
    134 struct vfsops ffs_vfsops = {
    135 	MOUNT_FFS,
    136 	sizeof (struct ufs_args),
    137 	ffs_mount,
    138 	ufs_start,
    139 	ffs_unmount,
    140 	ufs_root,
    141 	ufs_quotactl,
    142 	ffs_statvfs,
    143 	ffs_sync,
    144 	ffs_vget,
    145 	ffs_fhtovp,
    146 	ffs_vptofh,
    147 	ffs_init,
    148 	ffs_reinit,
    149 	ffs_done,
    150 	ffs_mountroot,
    151 	ffs_snapshot,
    152 	ffs_extattrctl,
    153 	ffs_suspendctl,
    154 	genfs_renamelock_enter,
    155 	genfs_renamelock_exit,
    156 	ffs_vfs_fsync,
    157 	ffs_vnodeopv_descs,
    158 	0,
    159 	{ NULL, NULL },
    160 };
    161 
    162 static const struct genfs_ops ffs_genfsops = {
    163 	.gop_size = ffs_gop_size,
    164 	.gop_alloc = ufs_gop_alloc,
    165 	.gop_write = genfs_gop_write,
    166 	.gop_markupdate = ufs_gop_markupdate,
    167 };
    168 
    169 static const struct ufs_ops ffs_ufsops = {
    170 	.uo_itimes = ffs_itimes,
    171 	.uo_update = ffs_update,
    172 	.uo_truncate = ffs_truncate,
    173 	.uo_valloc = ffs_valloc,
    174 	.uo_vfree = ffs_vfree,
    175 	.uo_balloc = ffs_balloc,
    176 };
    177 
    178 static int
    179 ffs_modcmd(modcmd_t cmd, void *arg)
    180 {
    181 	int error;
    182 
    183 #if 0
    184 	extern int doasyncfree;
    185 #endif
    186 	extern int ffs_log_changeopt;
    187 
    188 	switch (cmd) {
    189 	case MODULE_CMD_INIT:
    190 		error = vfs_attach(&ffs_vfsops);
    191 		if (error != 0)
    192 			break;
    193 
    194 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    195 			       CTLFLAG_PERMANENT,
    196 			       CTLTYPE_NODE, "vfs", NULL,
    197 			       NULL, 0, NULL, 0,
    198 			       CTL_VFS, CTL_EOL);
    199 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    200 			       CTLFLAG_PERMANENT,
    201 			       CTLTYPE_NODE, "ffs",
    202 			       SYSCTL_DESCR("Berkeley Fast File System"),
    203 			       NULL, 0, NULL, 0,
    204 			       CTL_VFS, 1, CTL_EOL);
    205 
    206 		/*
    207 		 * @@@ should we even bother with these first three?
    208 		 */
    209 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    210 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    211 			       CTLTYPE_INT, "doclusterread", NULL,
    212 			       sysctl_notavail, 0, NULL, 0,
    213 			       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
    214 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    215 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    216 			       CTLTYPE_INT, "doclusterwrite", NULL,
    217 			       sysctl_notavail, 0, NULL, 0,
    218 			       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
    219 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    220 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    221 			       CTLTYPE_INT, "doreallocblks", NULL,
    222 			       sysctl_notavail, 0, NULL, 0,
    223 			       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
    224 #if 0
    225 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    226 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    227 			       CTLTYPE_INT, "doasyncfree",
    228 			       SYSCTL_DESCR("Release dirty blocks asynchronously"),
    229 			       NULL, 0, &doasyncfree, 0,
    230 			       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
    231 #endif
    232 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    233 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    234 			       CTLTYPE_INT, "log_changeopt",
    235 			       SYSCTL_DESCR("Log changes in optimization strategy"),
    236 			       NULL, 0, &ffs_log_changeopt, 0,
    237 			       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
    238 		break;
    239 	case MODULE_CMD_FINI:
    240 		error = vfs_detach(&ffs_vfsops);
    241 		if (error != 0)
    242 			break;
    243 		sysctl_teardown(&ffs_sysctl_log);
    244 		break;
    245 	default:
    246 		error = ENOTTY;
    247 		break;
    248 	}
    249 
    250 	return (error);
    251 }
    252 
    253 pool_cache_t ffs_inode_cache;
    254 pool_cache_t ffs_dinode1_cache;
    255 pool_cache_t ffs_dinode2_cache;
    256 
    257 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
    258 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    259 
    260 /*
    261  * Called by main() when ffs is going to be mounted as root.
    262  */
    263 
    264 int
    265 ffs_mountroot(void)
    266 {
    267 	struct fs *fs;
    268 	struct mount *mp;
    269 	struct lwp *l = curlwp;			/* XXX */
    270 	struct ufsmount *ump;
    271 	int error;
    272 
    273 	if (device_class(root_device) != DV_DISK)
    274 		return (ENODEV);
    275 
    276 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    277 		vrele(rootvp);
    278 		return (error);
    279 	}
    280 
    281 	/*
    282 	 * We always need to be able to mount the root file system.
    283 	 */
    284 	mp->mnt_flag |= MNT_FORCE;
    285 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    286 		vfs_unbusy(mp, false, NULL);
    287 		vfs_destroy(mp);
    288 		return (error);
    289 	}
    290 	mp->mnt_flag &= ~MNT_FORCE;
    291 	mutex_enter(&mountlist_lock);
    292 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    293 	mutex_exit(&mountlist_lock);
    294 	ump = VFSTOUFS(mp);
    295 	fs = ump->um_fs;
    296 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    297 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    298 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    299 	vfs_unbusy(mp, false, NULL);
    300 	setrootfstime((time_t)fs->fs_time);
    301 	return (0);
    302 }
    303 
    304 /*
    305  * VFS Operations.
    306  *
    307  * mount system call
    308  */
    309 int
    310 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    311 {
    312 	struct lwp *l = curlwp;
    313 	struct nameidata nd;
    314 	struct vnode *vp, *devvp = NULL;
    315 	struct ufs_args *args = data;
    316 	struct ufsmount *ump = NULL;
    317 	struct fs *fs;
    318 	int error = 0, flags, update;
    319 	mode_t accessmode;
    320 
    321 	if (*data_len < sizeof *args)
    322 		return EINVAL;
    323 
    324 	if (mp->mnt_flag & MNT_GETARGS) {
    325 		ump = VFSTOUFS(mp);
    326 		if (ump == NULL)
    327 			return EIO;
    328 		args->fspec = NULL;
    329 		*data_len = sizeof *args;
    330 		return 0;
    331 	}
    332 
    333 #if !defined(SOFTDEP)
    334 	mp->mnt_flag &= ~MNT_SOFTDEP;
    335 #endif
    336 
    337 	update = mp->mnt_flag & MNT_UPDATE;
    338 
    339 	/* Check arguments */
    340 	if (args->fspec != NULL) {
    341 		/*
    342 		 * Look up the name and verify that it's sane.
    343 		 */
    344 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
    345 		if ((error = namei(&nd)) != 0)
    346 			return (error);
    347 		devvp = nd.ni_vp;
    348 
    349 		if (!update) {
    350 			/*
    351 			 * Be sure this is a valid block device
    352 			 */
    353 			if (devvp->v_type != VBLK)
    354 				error = ENOTBLK;
    355 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    356 				error = ENXIO;
    357 		} else {
    358 			/*
    359 			 * Be sure we're still naming the same device
    360 			 * used for our initial mount
    361 			 */
    362 			ump = VFSTOUFS(mp);
    363 			if (devvp != ump->um_devvp) {
    364 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    365 					error = EINVAL;
    366 				else {
    367 					vrele(devvp);
    368 					devvp = ump->um_devvp;
    369 					vref(devvp);
    370 				}
    371 			}
    372 		}
    373 	} else {
    374 		if (!update) {
    375 			/* New mounts must have a filename for the device */
    376 			return (EINVAL);
    377 		} else {
    378 			/* Use the extant mount */
    379 			ump = VFSTOUFS(mp);
    380 			devvp = ump->um_devvp;
    381 			vref(devvp);
    382 		}
    383 	}
    384 
    385 	/*
    386 	 * Mark the device and any existing vnodes as involved in
    387 	 * softdep processing.
    388 	 */
    389 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
    390 		devvp->v_uflag |= VU_SOFTDEP;
    391 		mutex_enter(&mntvnode_lock);
    392 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
    393 			if (vp->v_mount != mp || vismarker(vp))
    394 				continue;
    395 			vp->v_uflag |= VU_SOFTDEP;
    396 		}
    397 		mutex_exit(&mntvnode_lock);
    398 	}
    399 
    400 	/*
    401 	 * If mount by non-root, then verify that user has necessary
    402 	 * permissions on the device.
    403 	 */
    404 	if (error == 0 && kauth_authorize_generic(l->l_cred,
    405 	    KAUTH_GENERIC_ISSUSER, NULL) != 0) {
    406 		accessmode = VREAD;
    407 		if (update ?
    408 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    409 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    410 			accessmode |= VWRITE;
    411 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    412 		error = VOP_ACCESS(devvp, accessmode, l->l_cred);
    413 		VOP_UNLOCK(devvp, 0);
    414 	}
    415 
    416 	if (error) {
    417 		vrele(devvp);
    418 		return (error);
    419 	}
    420 
    421 #ifdef WAPBL
    422 	/*
    423 	 * WAPBL can only be enabled on a r/w mount
    424 	 * that does not use softdep.
    425 	 */
    426 	if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
    427 		mp->mnt_flag &= ~MNT_LOG;
    428 	}
    429 	if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_LOG)) ==
    430 			(MNT_SOFTDEP | MNT_LOG)) {
    431 		printf("%s fs is journalled, ignoring soft update mode\n",
    432 			VFSTOUFS(mp)->um_fs->fs_fsmnt);
    433 		mp->mnt_flag &= ~MNT_SOFTDEP;
    434 	}
    435 #else /* !WAPBL */
    436 	mp->mnt_flag &= ~MNT_LOG;
    437 #endif /* !WAPBL */
    438 
    439 	if (!update) {
    440 		int xflags;
    441 
    442 		if (mp->mnt_flag & MNT_RDONLY)
    443 			xflags = FREAD;
    444 		else
    445 			xflags = FREAD | FWRITE;
    446 		error = VOP_OPEN(devvp, xflags, FSCRED);
    447 		if (error)
    448 			goto fail;
    449 		error = ffs_mountfs(devvp, mp, l);
    450 		if (error) {
    451 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    452 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    453 			VOP_UNLOCK(devvp, 0);
    454 			goto fail;
    455 		}
    456 
    457 		ump = VFSTOUFS(mp);
    458 		fs = ump->um_fs;
    459 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    460 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    461 			printf("%s fs uses soft updates, "
    462 			    "ignoring async mode\n",
    463 			    fs->fs_fsmnt);
    464 			mp->mnt_flag &= ~MNT_ASYNC;
    465 		}
    466 	} else {
    467 		/*
    468 		 * Update the mount.
    469 		 */
    470 
    471 		/*
    472 		 * The initial mount got a reference on this
    473 		 * device, so drop the one obtained via
    474 		 * namei(), above.
    475 		 */
    476 		vrele(devvp);
    477 
    478 		ump = VFSTOUFS(mp);
    479 		fs = ump->um_fs;
    480 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    481 			/*
    482 			 * Changing from r/w to r/o
    483 			 */
    484 			flags = WRITECLOSE;
    485 			if (mp->mnt_flag & MNT_FORCE)
    486 				flags |= FORCECLOSE;
    487 			if (mp->mnt_flag & MNT_SOFTDEP)
    488 				error = softdep_flushfiles(mp, flags, l);
    489 			else
    490 				error = ffs_flushfiles(mp, flags, l);
    491 			if (fs->fs_pendingblocks != 0 ||
    492 			    fs->fs_pendinginodes != 0) {
    493 				printf("%s: update error: blocks %" PRId64
    494 				       " files %d\n",
    495 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    496 				    fs->fs_pendinginodes);
    497 				fs->fs_pendingblocks = 0;
    498 				fs->fs_pendinginodes = 0;
    499 			}
    500 			if (error == 0)
    501 				error = UFS_WAPBL_BEGIN(mp);
    502 			if (error == 0 &&
    503 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    504 			    fs->fs_clean & FS_WASCLEAN) {
    505 				if (mp->mnt_flag & MNT_SOFTDEP)
    506 					fs->fs_flags &= ~FS_DOSOFTDEP;
    507 				fs->fs_clean = FS_ISCLEAN;
    508 				(void) ffs_sbupdate(ump, MNT_WAIT);
    509 			}
    510 			if (error == 0)
    511 				UFS_WAPBL_END(mp);
    512 			if (error)
    513 				return (error);
    514 		}
    515 
    516 #ifdef WAPBL
    517 		if ((mp->mnt_flag & MNT_LOG) == 0) {
    518 			error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
    519 			if (error)
    520 				return error;
    521 		}
    522 #endif /* WAPBL */
    523 
    524 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    525 			/*
    526 			 * Finish change from r/w to r/o
    527 			 */
    528 			fs->fs_ronly = 1;
    529 			fs->fs_fmod = 0;
    530 		}
    531 
    532 		/*
    533 		 * Flush soft dependencies if disabling it via an update
    534 		 * mount. This may leave some items to be processed,
    535 		 * so don't do this yet XXX.
    536 		 */
    537 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    538 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    539 #ifdef notyet
    540 			flags = WRITECLOSE;
    541 			if (mp->mnt_flag & MNT_FORCE)
    542 				flags |= FORCECLOSE;
    543 			error = softdep_flushfiles(mp, flags, l);
    544 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    545 				fs->fs_flags &= ~FS_DOSOFTDEP;
    546 				(void) ffs_sbupdate(ump, MNT_WAIT);
    547 #elif defined(SOFTDEP)
    548 			mp->mnt_flag |= MNT_SOFTDEP;
    549 #endif
    550 		}
    551 
    552 		/*
    553 		 * When upgrading to a softdep mount, we must first flush
    554 		 * all vnodes. (not done yet -- see above)
    555 		 */
    556 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    557 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    558 #ifdef notyet
    559 			flags = WRITECLOSE;
    560 			if (mp->mnt_flag & MNT_FORCE)
    561 				flags |= FORCECLOSE;
    562 			error = ffs_flushfiles(mp, flags, l);
    563 #else
    564 			mp->mnt_flag &= ~MNT_SOFTDEP;
    565 #endif
    566 		}
    567 
    568 		if (mp->mnt_flag & MNT_RELOAD) {
    569 			error = ffs_reload(mp, l->l_cred, l);
    570 			if (error)
    571 				return (error);
    572 		}
    573 
    574 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    575 			/*
    576 			 * Changing from read-only to read/write
    577 			 */
    578 			fs->fs_ronly = 0;
    579 			fs->fs_clean <<= 1;
    580 			fs->fs_fmod = 1;
    581 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    582 				error = softdep_mount(devvp, mp, fs,
    583 				    l->l_cred);
    584 				if (error)
    585 					return (error);
    586 			}
    587 #ifdef WAPBL
    588 			if (fs->fs_flags & FS_DOWAPBL) {
    589 				printf("%s: replaying log to disk\n",
    590 				    fs->fs_fsmnt);
    591 				KDASSERT(mp->mnt_wapbl_replay);
    592 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
    593 							   devvp);
    594 				if (error) {
    595 					return error;
    596 				}
    597 				wapbl_replay_stop(mp->mnt_wapbl_replay);
    598 				fs->fs_clean = FS_WASCLEAN;
    599 			}
    600 #endif /* WAPBL */
    601 			if (fs->fs_snapinum[0] != 0)
    602 				ffs_snapshot_mount(mp);
    603 		}
    604 
    605 #ifdef WAPBL
    606 		error = ffs_wapbl_start(mp);
    607 		if (error)
    608 			return error;
    609 #endif /* WAPBL */
    610 
    611 		if (args->fspec == NULL)
    612 			return EINVAL;
    613 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    614 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    615 			printf("%s fs uses soft updates, ignoring async mode\n",
    616 			    fs->fs_fsmnt);
    617 			mp->mnt_flag &= ~MNT_ASYNC;
    618 		}
    619 	}
    620 
    621 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    622 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    623 	if (error == 0)
    624 		(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    625 		    sizeof(fs->fs_fsmnt));
    626 	if (mp->mnt_flag & MNT_SOFTDEP)
    627 		fs->fs_flags |= FS_DOSOFTDEP;
    628 	else
    629 		fs->fs_flags &= ~FS_DOSOFTDEP;
    630 	if (fs->fs_fmod != 0) {	/* XXX */
    631 		int err;
    632 
    633 		fs->fs_fmod = 0;
    634 		if (fs->fs_clean & FS_WASCLEAN)
    635 			fs->fs_time = time_second;
    636 		else {
    637 			printf("%s: file system not clean (fs_clean=%#x); "
    638 			    "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
    639 			    fs->fs_clean);
    640 			printf("%s: lost blocks %" PRId64 " files %d\n",
    641 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    642 			    fs->fs_pendinginodes);
    643 		}
    644 		err = UFS_WAPBL_BEGIN(mp);
    645 		if (err == 0) {
    646 			(void) ffs_cgupdate(ump, MNT_WAIT);
    647 			UFS_WAPBL_END(mp);
    648 		}
    649 	}
    650 	return (error);
    651 
    652 fail:
    653 	vrele(devvp);
    654 	return (error);
    655 }
    656 
    657 /*
    658  * Reload all incore data for a filesystem (used after running fsck on
    659  * the root filesystem and finding things to fix). The filesystem must
    660  * be mounted read-only.
    661  *
    662  * Things to do to update the mount:
    663  *	1) invalidate all cached meta-data.
    664  *	2) re-read superblock from disk.
    665  *	3) re-read summary information from disk.
    666  *	4) invalidate all inactive vnodes.
    667  *	5) invalidate all cached file data.
    668  *	6) re-read inode data for all active vnodes.
    669  */
    670 int
    671 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    672 {
    673 	struct vnode *vp, *mvp, *devvp;
    674 	struct inode *ip;
    675 	void *space;
    676 	struct buf *bp;
    677 	struct fs *fs, *newfs;
    678 	struct partinfo dpart;
    679 	int i, blks, size, error;
    680 	int32_t *lp;
    681 	struct ufsmount *ump;
    682 	daddr_t sblockloc;
    683 
    684 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    685 		return (EINVAL);
    686 
    687 	ump = VFSTOUFS(mp);
    688 	/*
    689 	 * Step 1: invalidate all cached meta-data.
    690 	 */
    691 	devvp = ump->um_devvp;
    692 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    693 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    694 	VOP_UNLOCK(devvp, 0);
    695 	if (error)
    696 		panic("ffs_reload: dirty1");
    697 	/*
    698 	 * Step 2: re-read superblock from disk.
    699 	 */
    700 	fs = ump->um_fs;
    701 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED) != 0)
    702 		size = DEV_BSIZE;
    703 	else
    704 		size = dpart.disklab->d_secsize;
    705 	/* XXX we don't handle possibility that superblock moved. */
    706 	error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
    707 		      NOCRED, 0, &bp);
    708 	if (error) {
    709 		brelse(bp, 0);
    710 		return (error);
    711 	}
    712 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    713 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    714 #ifdef FFS_EI
    715 	if (ump->um_flags & UFS_NEEDSWAP) {
    716 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    717 		fs->fs_flags |= FS_SWAPPED;
    718 	} else
    719 #endif
    720 		fs->fs_flags &= ~FS_SWAPPED;
    721 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
    722 	     newfs->fs_magic != FS_UFS2_MAGIC)||
    723 	     newfs->fs_bsize > MAXBSIZE ||
    724 	     newfs->fs_bsize < sizeof(struct fs)) {
    725 		brelse(bp, 0);
    726 		free(newfs, M_UFSMNT);
    727 		return (EIO);		/* XXX needs translation */
    728 	}
    729 	/* Store off old fs_sblockloc for fs_oldfscompat_read. */
    730 	sblockloc = fs->fs_sblockloc;
    731 	/*
    732 	 * Copy pointer fields back into superblock before copying in	XXX
    733 	 * new superblock. These should really be in the ufsmount.	XXX
    734 	 * Note that important parameters (eg fs_ncg) are unchanged.
    735 	 */
    736 	newfs->fs_csp = fs->fs_csp;
    737 	newfs->fs_maxcluster = fs->fs_maxcluster;
    738 	newfs->fs_contigdirs = fs->fs_contigdirs;
    739 	newfs->fs_ronly = fs->fs_ronly;
    740 	newfs->fs_active = fs->fs_active;
    741 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    742 	brelse(bp, 0);
    743 	free(newfs, M_UFSMNT);
    744 
    745 	/* Recheck for apple UFS filesystem */
    746 	ump->um_flags &= ~UFS_ISAPPLEUFS;
    747 	/* First check to see if this is tagged as an Apple UFS filesystem
    748 	 * in the disklabel
    749 	 */
    750 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
    751 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    752 		ump->um_flags |= UFS_ISAPPLEUFS;
    753 	}
    754 #ifdef APPLE_UFS
    755 	else {
    756 		/* Manually look for an apple ufs label, and if a valid one
    757 		 * is found, then treat it like an Apple UFS filesystem anyway
    758 		 */
    759 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    760 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
    761 		if (error) {
    762 			brelse(bp, 0);
    763 			return (error);
    764 		}
    765 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    766 			(struct appleufslabel *)bp->b_data, NULL);
    767 		if (error == 0)
    768 			ump->um_flags |= UFS_ISAPPLEUFS;
    769 		brelse(bp, 0);
    770 		bp = NULL;
    771 	}
    772 #else
    773 	if (ump->um_flags & UFS_ISAPPLEUFS)
    774 		return (EIO);
    775 #endif
    776 
    777 	if (UFS_MPISAPPLEUFS(ump)) {
    778 		/* see comment about NeXT below */
    779 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    780 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
    781 		mp->mnt_iflag |= IMNT_DTYPE;
    782 	} else {
    783 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
    784 		ump->um_dirblksiz = DIRBLKSIZ;
    785 		if (ump->um_maxsymlinklen > 0)
    786 			mp->mnt_iflag |= IMNT_DTYPE;
    787 		else
    788 			mp->mnt_iflag &= ~IMNT_DTYPE;
    789 	}
    790 	ffs_oldfscompat_read(fs, ump, sblockloc);
    791 	mutex_enter(&ump->um_lock);
    792 	ump->um_maxfilesize = fs->fs_maxfilesize;
    793 
    794 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
    795 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
    796 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
    797 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
    798 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
    799 			mutex_exit(&ump->um_lock);
    800 			return (EINVAL);
    801 		}
    802 	}
    803 
    804 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    805 		fs->fs_pendingblocks = 0;
    806 		fs->fs_pendinginodes = 0;
    807 	}
    808 	mutex_exit(&ump->um_lock);
    809 
    810 	ffs_statvfs(mp, &mp->mnt_stat);
    811 	/*
    812 	 * Step 3: re-read summary information from disk.
    813 	 */
    814 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    815 	space = fs->fs_csp;
    816 	for (i = 0; i < blks; i += fs->fs_frag) {
    817 		size = fs->fs_bsize;
    818 		if (i + fs->fs_frag > blks)
    819 			size = (blks - i) * fs->fs_fsize;
    820 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    821 			      NOCRED, 0, &bp);
    822 		if (error) {
    823 			brelse(bp, 0);
    824 			return (error);
    825 		}
    826 #ifdef FFS_EI
    827 		if (UFS_FSNEEDSWAP(fs))
    828 			ffs_csum_swap((struct csum *)bp->b_data,
    829 			    (struct csum *)space, size);
    830 		else
    831 #endif
    832 			memcpy(space, bp->b_data, (size_t)size);
    833 		space = (char *)space + size;
    834 		brelse(bp, 0);
    835 	}
    836 	if ((fs->fs_flags & FS_DOSOFTDEP))
    837 		softdep_mount(devvp, mp, fs, cred);
    838 	if (fs->fs_snapinum[0] != 0)
    839 		ffs_snapshot_mount(mp);
    840 	/*
    841 	 * We no longer know anything about clusters per cylinder group.
    842 	 */
    843 	if (fs->fs_contigsumsize > 0) {
    844 		lp = fs->fs_maxcluster;
    845 		for (i = 0; i < fs->fs_ncg; i++)
    846 			*lp++ = fs->fs_contigsumsize;
    847 	}
    848 
    849 	/* Allocate a marker vnode. */
    850 	if ((mvp = vnalloc(mp)) == NULL)
    851 		return ENOMEM;
    852 	/*
    853 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    854 	 * and vclean() can be called indirectly
    855 	 */
    856 	mutex_enter(&mntvnode_lock);
    857  loop:
    858 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    859 		vmark(mvp, vp);
    860 		if (vp->v_mount != mp || vismarker(vp))
    861 			continue;
    862 		/*
    863 		 * Step 4: invalidate all inactive vnodes.
    864 		 */
    865 		if (vrecycle(vp, &mntvnode_lock, l)) {
    866 			mutex_enter(&mntvnode_lock);
    867 			(void)vunmark(mvp);
    868 			goto loop;
    869 		}
    870 		/*
    871 		 * Step 5: invalidate all cached file data.
    872 		 */
    873 		mutex_enter(&vp->v_interlock);
    874 		mutex_exit(&mntvnode_lock);
    875 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
    876 			(void)vunmark(mvp);
    877 			goto loop;
    878 		}
    879 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    880 			panic("ffs_reload: dirty2");
    881 		/*
    882 		 * Step 6: re-read inode data for all active vnodes.
    883 		 */
    884 		ip = VTOI(vp);
    885 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    886 			      (int)fs->fs_bsize, NOCRED, 0, &bp);
    887 		if (error) {
    888 			brelse(bp, 0);
    889 			vput(vp);
    890 			(void)vunmark(mvp);
    891 			break;
    892 		}
    893 		ffs_load_inode(bp, ip, fs, ip->i_number);
    894 		ip->i_ffs_effnlink = ip->i_nlink;
    895 		brelse(bp, 0);
    896 		vput(vp);
    897 		mutex_enter(&mntvnode_lock);
    898 	}
    899 	mutex_exit(&mntvnode_lock);
    900 	vnfree(mvp);
    901 	return (error);
    902 }
    903 
    904 /*
    905  * Possible superblock locations ordered from most to least likely.
    906  */
    907 static const int sblock_try[] = SBLOCKSEARCH;
    908 
    909 /*
    910  * Common code for mount and mountroot
    911  */
    912 int
    913 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    914 {
    915 	struct ufsmount *ump;
    916 	struct buf *bp;
    917 	struct fs *fs;
    918 	dev_t dev;
    919 	struct partinfo dpart;
    920 	void *space;
    921 	daddr_t sblockloc, fsblockloc;
    922 	int blks, fstype;
    923 	int error, i, size, ronly, bset = 0;
    924 #ifdef FFS_EI
    925 	int needswap = 0;		/* keep gcc happy */
    926 #endif
    927 	int32_t *lp;
    928 	kauth_cred_t cred;
    929 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
    930 
    931 	dev = devvp->v_rdev;
    932 	cred = l ? l->l_cred : NOCRED;
    933 
    934 	/* Flush out any old buffers remaining from a previous use. */
    935 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    936 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    937 	VOP_UNLOCK(devvp, 0);
    938 	if (error)
    939 		return (error);
    940 
    941 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    942 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) != 0)
    943 		size = DEV_BSIZE;
    944 	else
    945 		size = dpart.disklab->d_secsize;
    946 
    947 	bp = NULL;
    948 	ump = NULL;
    949 	fs = NULL;
    950 	sblockloc = 0;
    951 	fstype = 0;
    952 
    953 	error = fstrans_mount(mp);
    954 	if (error)
    955 		return error;
    956 
    957 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    958 	memset(ump, 0, sizeof *ump);
    959 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
    960 	error = ffs_snapshot_init(ump);
    961 	if (error)
    962 		goto out;
    963 	ump->um_ops = &ffs_ufsops;
    964 
    965 #ifdef WAPBL
    966  sbagain:
    967 #endif
    968 	/*
    969 	 * Try reading the superblock in each of its possible locations.
    970 	 */
    971 	for (i = 0; ; i++) {
    972 		if (bp != NULL) {
    973 			brelse(bp, BC_NOCACHE);
    974 			bp = NULL;
    975 		}
    976 		if (sblock_try[i] == -1) {
    977 			error = EINVAL;
    978 			fs = NULL;
    979 			goto out;
    980 		}
    981 		error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
    982 			      0, &bp);
    983 		if (error) {
    984 			fs = NULL;
    985 			goto out;
    986 		}
    987 		fs = (struct fs*)bp->b_data;
    988 		fsblockloc = sblockloc = sblock_try[i];
    989 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    990 			sbsize = fs->fs_sbsize;
    991 			fstype = UFS1;
    992 #ifdef FFS_EI
    993 			needswap = 0;
    994 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    995 			sbsize = bswap32(fs->fs_sbsize);
    996 			fstype = UFS1;
    997 			needswap = 1;
    998 #endif
    999 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
   1000 			sbsize = fs->fs_sbsize;
   1001 			fstype = UFS2;
   1002 #ifdef FFS_EI
   1003 			needswap = 0;
   1004 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
   1005 			sbsize = bswap32(fs->fs_sbsize);
   1006 			fstype = UFS2;
   1007 			needswap = 1;
   1008 #endif
   1009 		} else
   1010 			continue;
   1011 
   1012 
   1013 		/* fs->fs_sblockloc isn't defined for old filesystems */
   1014 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
   1015 			if (sblockloc == SBLOCK_UFS2)
   1016 				/*
   1017 				 * This is likely to be the first alternate
   1018 				 * in a filesystem with 64k blocks.
   1019 				 * Don't use it.
   1020 				 */
   1021 				continue;
   1022 			fsblockloc = sblockloc;
   1023 		} else {
   1024 			fsblockloc = fs->fs_sblockloc;
   1025 #ifdef FFS_EI
   1026 			if (needswap)
   1027 				fsblockloc = bswap64(fsblockloc);
   1028 #endif
   1029 		}
   1030 
   1031 		/* Check we haven't found an alternate superblock */
   1032 		if (fsblockloc != sblockloc)
   1033 			continue;
   1034 
   1035 		/* Validate size of superblock */
   1036 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
   1037 			continue;
   1038 
   1039 		/* Ok seems to be a good superblock */
   1040 		break;
   1041 	}
   1042 
   1043 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
   1044 	memcpy(fs, bp->b_data, sbsize);
   1045 	ump->um_fs = fs;
   1046 
   1047 #ifdef FFS_EI
   1048 	if (needswap) {
   1049 		ffs_sb_swap((struct fs*)bp->b_data, fs);
   1050 		fs->fs_flags |= FS_SWAPPED;
   1051 	} else
   1052 #endif
   1053 		fs->fs_flags &= ~FS_SWAPPED;
   1054 
   1055 #ifdef WAPBL
   1056 	if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
   1057 		error = ffs_wapbl_replay_start(mp, fs, devvp);
   1058 		if (error && (mp->mnt_flag & MNT_FORCE) == 0)
   1059 			goto out;
   1060 		if (!error) {
   1061 			if (!ronly) {
   1062 				/* XXX fsmnt may be stale. */
   1063 				printf("%s: replaying log to disk\n",
   1064 				    fs->fs_fsmnt);
   1065 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
   1066 				    devvp);
   1067 				if (error)
   1068 					goto out;
   1069 				wapbl_replay_stop(mp->mnt_wapbl_replay);
   1070 				fs->fs_clean = FS_WASCLEAN;
   1071 			} else {
   1072 				/* XXX fsmnt may be stale */
   1073 				printf("%s: replaying log to memory\n",
   1074 				    fs->fs_fsmnt);
   1075 			}
   1076 
   1077 			/* Force a re-read of the superblock */
   1078 			brelse(bp, BC_INVAL);
   1079 			bp = NULL;
   1080 			free(fs, M_UFSMNT);
   1081 			fs = NULL;
   1082 			goto sbagain;
   1083 		}
   1084 	}
   1085 #else /* !WAPBL */
   1086 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
   1087 		error = EPERM;
   1088 		goto out;
   1089 	}
   1090 #endif /* !WAPBL */
   1091 
   1092 	ffs_oldfscompat_read(fs, ump, sblockloc);
   1093 	ump->um_maxfilesize = fs->fs_maxfilesize;
   1094 
   1095 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
   1096 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
   1097 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
   1098 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1099 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1100 			error = EINVAL;
   1101 			goto out;
   1102 		}
   1103 	}
   1104 
   1105 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1106 		fs->fs_pendingblocks = 0;
   1107 		fs->fs_pendinginodes = 0;
   1108 	}
   1109 
   1110 	ump->um_fstype = fstype;
   1111 	if (fs->fs_sbsize < SBLOCKSIZE)
   1112 		brelse(bp, BC_INVAL);
   1113 	else
   1114 		brelse(bp, 0);
   1115 	bp = NULL;
   1116 
   1117 	/* First check to see if this is tagged as an Apple UFS filesystem
   1118 	 * in the disklabel
   1119 	 */
   1120 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
   1121 		(dpart.part->p_fstype == FS_APPLEUFS)) {
   1122 		ump->um_flags |= UFS_ISAPPLEUFS;
   1123 	}
   1124 #ifdef APPLE_UFS
   1125 	else {
   1126 		/* Manually look for an apple ufs label, and if a valid one
   1127 		 * is found, then treat it like an Apple UFS filesystem anyway
   1128 		 */
   1129 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
   1130 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
   1131 		if (error)
   1132 			goto out;
   1133 		error = ffs_appleufs_validate(fs->fs_fsmnt,
   1134 			(struct appleufslabel *)bp->b_data, NULL);
   1135 		if (error == 0) {
   1136 			ump->um_flags |= UFS_ISAPPLEUFS;
   1137 		}
   1138 		brelse(bp, 0);
   1139 		bp = NULL;
   1140 	}
   1141 #else
   1142 	if (ump->um_flags & UFS_ISAPPLEUFS) {
   1143 		error = EINVAL;
   1144 		goto out;
   1145 	}
   1146 #endif
   1147 
   1148 #if 0
   1149 /*
   1150  * XXX This code changes the behaviour of mounting dirty filesystems, to
   1151  * XXX require "mount -f ..." to mount them.  This doesn't match what
   1152  * XXX mount(8) describes and is disabled for now.
   1153  */
   1154 	/*
   1155 	 * If the file system is not clean, don't allow it to be mounted
   1156 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
   1157 	 * for the root file system.)
   1158 	 */
   1159 	if (fs->fs_flags & FS_DOWAPBL) {
   1160 		/*
   1161 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
   1162 		 * bit is set, although there's a window in unmount where it
   1163 		 * could be FS_ISCLEAN
   1164 		 */
   1165 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
   1166 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
   1167 			error = EPERM;
   1168 			goto out;
   1169 		}
   1170 	} else
   1171 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
   1172 		    (mp->mnt_flag & MNT_FORCE) == 0) {
   1173 			error = EPERM;
   1174 			goto out;
   1175 		}
   1176 #endif
   1177 
   1178 	/*
   1179 	 * verify that we can access the last block in the fs
   1180 	 * if we're mounting read/write.
   1181 	 */
   1182 
   1183 	if (!ronly) {
   1184 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
   1185 		    cred, 0, &bp);
   1186 		if (bp->b_bcount != fs->fs_fsize)
   1187 			error = EINVAL;
   1188 		if (error) {
   1189 			bset = BC_INVAL;
   1190 			goto out;
   1191 		}
   1192 		brelse(bp, BC_INVAL);
   1193 		bp = NULL;
   1194 	}
   1195 
   1196 	fs->fs_ronly = ronly;
   1197 	/* Don't bump fs_clean if we're replaying journal */
   1198 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
   1199 		if (ronly == 0) {
   1200 			fs->fs_clean <<= 1;
   1201 			fs->fs_fmod = 1;
   1202 		}
   1203 	size = fs->fs_cssize;
   1204 	blks = howmany(size, fs->fs_fsize);
   1205 	if (fs->fs_contigsumsize > 0)
   1206 		size += fs->fs_ncg * sizeof(int32_t);
   1207 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1208 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
   1209 	fs->fs_csp = space;
   1210 	for (i = 0; i < blks; i += fs->fs_frag) {
   1211 		size = fs->fs_bsize;
   1212 		if (i + fs->fs_frag > blks)
   1213 			size = (blks - i) * fs->fs_fsize;
   1214 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
   1215 			      cred, 0, &bp);
   1216 		if (error) {
   1217 			free(fs->fs_csp, M_UFSMNT);
   1218 			goto out;
   1219 		}
   1220 #ifdef FFS_EI
   1221 		if (needswap)
   1222 			ffs_csum_swap((struct csum *)bp->b_data,
   1223 				(struct csum *)space, size);
   1224 		else
   1225 #endif
   1226 			memcpy(space, bp->b_data, (u_int)size);
   1227 
   1228 		space = (char *)space + size;
   1229 		brelse(bp, 0);
   1230 		bp = NULL;
   1231 	}
   1232 	if (fs->fs_contigsumsize > 0) {
   1233 		fs->fs_maxcluster = lp = space;
   1234 		for (i = 0; i < fs->fs_ncg; i++)
   1235 			*lp++ = fs->fs_contigsumsize;
   1236 		space = lp;
   1237 	}
   1238 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1239 	fs->fs_contigdirs = space;
   1240 	space = (char *)space + size;
   1241 	memset(fs->fs_contigdirs, 0, size);
   1242 		/* Compatibility for old filesystems - XXX */
   1243 	if (fs->fs_avgfilesize <= 0)
   1244 		fs->fs_avgfilesize = AVFILESIZ;
   1245 	if (fs->fs_avgfpdir <= 0)
   1246 		fs->fs_avgfpdir = AFPDIR;
   1247 	fs->fs_active = NULL;
   1248 	mp->mnt_data = ump;
   1249 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1250 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
   1251 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1252 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
   1253 	if (UFS_MPISAPPLEUFS(ump)) {
   1254 		/* NeXT used to keep short symlinks in the inode even
   1255 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
   1256 		 * is probably -1, but we still need to be able to identify
   1257 		 * short symlinks.
   1258 		 */
   1259 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
   1260 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
   1261 		mp->mnt_iflag |= IMNT_DTYPE;
   1262 	} else {
   1263 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
   1264 		ump->um_dirblksiz = DIRBLKSIZ;
   1265 		if (ump->um_maxsymlinklen > 0)
   1266 			mp->mnt_iflag |= IMNT_DTYPE;
   1267 		else
   1268 			mp->mnt_iflag &= ~IMNT_DTYPE;
   1269 	}
   1270 	mp->mnt_fs_bshift = fs->fs_bshift;
   1271 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
   1272 	mp->mnt_flag |= MNT_LOCAL;
   1273 	mp->mnt_iflag |= IMNT_MPSAFE;
   1274 #ifdef FFS_EI
   1275 	if (needswap)
   1276 		ump->um_flags |= UFS_NEEDSWAP;
   1277 #endif
   1278 	ump->um_mountp = mp;
   1279 	ump->um_dev = dev;
   1280 	ump->um_devvp = devvp;
   1281 	ump->um_nindir = fs->fs_nindir;
   1282 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
   1283 	ump->um_bptrtodb = fs->fs_fsbtodb;
   1284 	ump->um_seqinc = fs->fs_frag;
   1285 	for (i = 0; i < MAXQUOTAS; i++)
   1286 		ump->um_quotas[i] = NULLVP;
   1287 	devvp->v_specmountpoint = mp;
   1288 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
   1289 		error = softdep_mount(devvp, mp, fs, cred);
   1290 		if (error) {
   1291 			free(fs->fs_csp, M_UFSMNT);
   1292 			goto out;
   1293 		}
   1294 	}
   1295 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1296 		ffs_snapshot_mount(mp);
   1297 
   1298 #ifdef WAPBL
   1299 	if (!ronly) {
   1300 		KDASSERT(fs->fs_ronly == 0);
   1301 		/*
   1302 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
   1303 		 * needs to create a new log file in-filesystem.
   1304 		 */
   1305 		ffs_statvfs(mp, &mp->mnt_stat);
   1306 
   1307 		error = ffs_wapbl_start(mp);
   1308 		if (error) {
   1309 			free(fs->fs_csp, M_UFSMNT);
   1310 			goto out;
   1311 		}
   1312 	}
   1313 #endif /* WAPBL */
   1314 #ifdef UFS_EXTATTR
   1315 	/*
   1316 	 * Initialize file-backed extended attributes on UFS1 file
   1317 	 * systems.
   1318 	 */
   1319 	if (ump->um_fstype == UFS1) {
   1320 		ufs_extattr_uepm_init(&ump->um_extattr);
   1321 #ifdef UFS_EXTATTR_AUTOSTART
   1322 		/*
   1323 		 * XXX Just ignore errors.  Not clear that we should
   1324 		 * XXX fail the mount in this case.
   1325 		 */
   1326 		(void) ufs_extattr_autostart(mp, l);
   1327 #endif
   1328 	}
   1329 #endif /* UFS_EXTATTR */
   1330 	return (0);
   1331 out:
   1332 #ifdef WAPBL
   1333 	if (mp->mnt_wapbl_replay) {
   1334 		if (wapbl_replay_isopen(mp->mnt_wapbl_replay))
   1335 			wapbl_replay_stop(mp->mnt_wapbl_replay);
   1336 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1337 		mp->mnt_wapbl_replay = 0;
   1338 	}
   1339 #endif
   1340 
   1341 	fstrans_unmount(mp);
   1342 	if (fs)
   1343 		free(fs, M_UFSMNT);
   1344 	devvp->v_specmountpoint = NULL;
   1345 	if (bp)
   1346 		brelse(bp, bset);
   1347 	if (ump) {
   1348 		if (ump->um_oldfscompat)
   1349 			free(ump->um_oldfscompat, M_UFSMNT);
   1350 		mutex_destroy(&ump->um_lock);
   1351 		free(ump, M_UFSMNT);
   1352 		mp->mnt_data = NULL;
   1353 	}
   1354 	return (error);
   1355 }
   1356 
   1357 /*
   1358  * Sanity checks for loading old filesystem superblocks.
   1359  * See ffs_oldfscompat_write below for unwound actions.
   1360  *
   1361  * XXX - Parts get retired eventually.
   1362  * Unfortunately new bits get added.
   1363  */
   1364 static void
   1365 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1366 {
   1367 	off_t maxfilesize;
   1368 	int32_t *extrasave;
   1369 
   1370 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1371 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1372 		return;
   1373 
   1374 	if (!ump->um_oldfscompat)
   1375 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
   1376 		    M_UFSMNT, M_WAITOK);
   1377 
   1378 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1379 	extrasave = ump->um_oldfscompat;
   1380 	extrasave += 512/sizeof(int32_t);
   1381 	extrasave[0] = fs->fs_old_npsect;
   1382 	extrasave[1] = fs->fs_old_interleave;
   1383 	extrasave[2] = fs->fs_old_trackskew;
   1384 
   1385 	/* These fields will be overwritten by their
   1386 	 * original values in fs_oldfscompat_write, so it is harmless
   1387 	 * to modify them here.
   1388 	 */
   1389 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1390 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1391 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1392 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1393 
   1394 	fs->fs_maxbsize = fs->fs_bsize;
   1395 	fs->fs_time = fs->fs_old_time;
   1396 	fs->fs_size = fs->fs_old_size;
   1397 	fs->fs_dsize = fs->fs_old_dsize;
   1398 	fs->fs_csaddr = fs->fs_old_csaddr;
   1399 	fs->fs_sblockloc = sblockloc;
   1400 
   1401 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1402 
   1403 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1404 		fs->fs_old_nrpos = 8;
   1405 		fs->fs_old_npsect = fs->fs_old_nsect;
   1406 		fs->fs_old_interleave = 1;
   1407 		fs->fs_old_trackskew = 0;
   1408 	}
   1409 
   1410 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1411 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1412 		fs->fs_qbmask = ~fs->fs_bmask;
   1413 		fs->fs_qfmask = ~fs->fs_fmask;
   1414 	}
   1415 
   1416 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1417 	if (fs->fs_maxfilesize > maxfilesize)
   1418 		fs->fs_maxfilesize = maxfilesize;
   1419 
   1420 	/* Compatibility for old filesystems */
   1421 	if (fs->fs_avgfilesize <= 0)
   1422 		fs->fs_avgfilesize = AVFILESIZ;
   1423 	if (fs->fs_avgfpdir <= 0)
   1424 		fs->fs_avgfpdir = AFPDIR;
   1425 
   1426 #if 0
   1427 	if (bigcgs) {
   1428 		fs->fs_save_cgsize = fs->fs_cgsize;
   1429 		fs->fs_cgsize = fs->fs_bsize;
   1430 	}
   1431 #endif
   1432 }
   1433 
   1434 /*
   1435  * Unwinding superblock updates for old filesystems.
   1436  * See ffs_oldfscompat_read above for details.
   1437  *
   1438  * XXX - Parts get retired eventually.
   1439  * Unfortunately new bits get added.
   1440  */
   1441 static void
   1442 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1443 {
   1444 	int32_t *extrasave;
   1445 
   1446 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1447 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1448 		return;
   1449 
   1450 	fs->fs_old_time = fs->fs_time;
   1451 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1452 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1453 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1454 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1455 	fs->fs_old_flags = fs->fs_flags;
   1456 
   1457 #if 0
   1458 	if (bigcgs) {
   1459 		fs->fs_cgsize = fs->fs_save_cgsize;
   1460 	}
   1461 #endif
   1462 
   1463 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1464 	extrasave = ump->um_oldfscompat;
   1465 	extrasave += 512/sizeof(int32_t);
   1466 	fs->fs_old_npsect = extrasave[0];
   1467 	fs->fs_old_interleave = extrasave[1];
   1468 	fs->fs_old_trackskew = extrasave[2];
   1469 
   1470 }
   1471 
   1472 /*
   1473  * unmount system call
   1474  */
   1475 int
   1476 ffs_unmount(struct mount *mp, int mntflags)
   1477 {
   1478 	struct lwp *l = curlwp;
   1479 	struct ufsmount *ump = VFSTOUFS(mp);
   1480 	struct fs *fs = ump->um_fs;
   1481 	int error, flags, penderr;
   1482 #ifdef WAPBL
   1483 	extern int doforce;
   1484 #endif
   1485 
   1486 	penderr = 0;
   1487 	flags = 0;
   1488 	if (mntflags & MNT_FORCE)
   1489 		flags |= FORCECLOSE;
   1490 #ifdef UFS_EXTATTR
   1491 	if (ump->um_fstype == UFS1) {
   1492 		ufs_extattr_stop(mp, l);
   1493 		ufs_extattr_uepm_destroy(&ump->um_extattr);
   1494 	}
   1495 #endif /* UFS_EXTATTR */
   1496 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1497 		if ((error = softdep_flushfiles(mp, flags, l)) != 0)
   1498 			return (error);
   1499 	} else {
   1500 		if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1501 			return (error);
   1502 	}
   1503 	mutex_enter(&ump->um_lock);
   1504 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1505 		printf("%s: unmount pending error: blocks %" PRId64
   1506 		       " files %d\n",
   1507 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1508 		fs->fs_pendingblocks = 0;
   1509 		fs->fs_pendinginodes = 0;
   1510 		penderr = 1;
   1511 	}
   1512 	mutex_exit(&ump->um_lock);
   1513 	error = UFS_WAPBL_BEGIN(mp);
   1514 	if (error == 0)
   1515 		if (fs->fs_ronly == 0 &&
   1516 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1517 		    fs->fs_clean & FS_WASCLEAN) {
   1518 			/*
   1519 			 * XXXX don't mark fs clean in the case of softdep
   1520 			 * pending block errors, until they are fixed.
   1521 			 */
   1522 			if (penderr == 0) {
   1523 				if (mp->mnt_flag & MNT_SOFTDEP)
   1524 					fs->fs_flags &= ~FS_DOSOFTDEP;
   1525 				fs->fs_clean = FS_ISCLEAN;
   1526 			}
   1527 			fs->fs_fmod = 0;
   1528 			(void) ffs_sbupdate(ump, MNT_WAIT);
   1529 		}
   1530 	if (error == 0)
   1531 		UFS_WAPBL_END(mp);
   1532 #ifdef WAPBL
   1533 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
   1534 	if (mp->mnt_wapbl_replay) {
   1535 		KDASSERT(fs->fs_ronly);
   1536 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1537 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1538 		mp->mnt_wapbl_replay = 0;
   1539 	}
   1540 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
   1541 	if (error) {
   1542 		return error;
   1543 	}
   1544 #endif /* WAPBL */
   1545 	if (ump->um_devvp->v_type != VBAD)
   1546 		ump->um_devvp->v_specmountpoint = NULL;
   1547 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1548 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
   1549 		NOCRED);
   1550 	vput(ump->um_devvp);
   1551 	free(fs->fs_csp, M_UFSMNT);
   1552 	free(fs, M_UFSMNT);
   1553 	if (ump->um_oldfscompat != NULL)
   1554 		free(ump->um_oldfscompat, M_UFSMNT);
   1555 	softdep_unmount(mp);
   1556 	mutex_destroy(&ump->um_lock);
   1557 	ffs_snapshot_fini(ump);
   1558 	free(ump, M_UFSMNT);
   1559 	mp->mnt_data = NULL;
   1560 	mp->mnt_flag &= ~MNT_LOCAL;
   1561 	fstrans_unmount(mp);
   1562 	return (0);
   1563 }
   1564 
   1565 /*
   1566  * Flush out all the files in a filesystem.
   1567  */
   1568 int
   1569 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1570 {
   1571 	extern int doforce;
   1572 	struct ufsmount *ump;
   1573 	int error;
   1574 
   1575 	if (!doforce)
   1576 		flags &= ~FORCECLOSE;
   1577 	ump = VFSTOUFS(mp);
   1578 #ifdef QUOTA
   1579 	if (mp->mnt_flag & MNT_QUOTA) {
   1580 		int i;
   1581 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
   1582 			return (error);
   1583 		for (i = 0; i < MAXQUOTAS; i++) {
   1584 			if (ump->um_quotas[i] == NULLVP)
   1585 				continue;
   1586 			quotaoff(l, mp, i);
   1587 		}
   1588 		/*
   1589 		 * Here we fall through to vflush again to ensure
   1590 		 * that we have gotten rid of all the system vnodes.
   1591 		 */
   1592 	}
   1593 #endif
   1594 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1595 		return (error);
   1596 	ffs_snapshot_unmount(mp);
   1597 	/*
   1598 	 * Flush all the files.
   1599 	 */
   1600 	error = vflush(mp, NULLVP, flags);
   1601 	if (error)
   1602 		return (error);
   1603 	/*
   1604 	 * Flush filesystem metadata.
   1605 	 */
   1606 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1607 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
   1608 	VOP_UNLOCK(ump->um_devvp, 0);
   1609 	if (flags & FORCECLOSE) /* XXXDBJ */
   1610 		error = 0;
   1611 
   1612 #ifdef WAPBL
   1613 	if (error)
   1614 		return error;
   1615 	if (mp->mnt_wapbl) {
   1616 		error = wapbl_flush(mp->mnt_wapbl, 1);
   1617 		if (flags & FORCECLOSE)
   1618 			error = 0;
   1619 	}
   1620 #endif
   1621 
   1622 	return (error);
   1623 }
   1624 
   1625 /*
   1626  * Get file system statistics.
   1627  */
   1628 int
   1629 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
   1630 {
   1631 	struct ufsmount *ump;
   1632 	struct fs *fs;
   1633 
   1634 	ump = VFSTOUFS(mp);
   1635 	fs = ump->um_fs;
   1636 	mutex_enter(&ump->um_lock);
   1637 	sbp->f_bsize = fs->fs_bsize;
   1638 	sbp->f_frsize = fs->fs_fsize;
   1639 	sbp->f_iosize = fs->fs_bsize;
   1640 	sbp->f_blocks = fs->fs_dsize;
   1641 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1642 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1643 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1644 	    fs->fs_minfree) / (u_int64_t) 100;
   1645 	if (sbp->f_bfree > sbp->f_bresvd)
   1646 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1647 	else
   1648 		sbp->f_bavail = 0;
   1649 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1650 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1651 	sbp->f_favail = sbp->f_ffree;
   1652 	sbp->f_fresvd = 0;
   1653 	mutex_exit(&ump->um_lock);
   1654 	copy_statvfs_info(sbp, mp);
   1655 
   1656 	return (0);
   1657 }
   1658 
   1659 /*
   1660  * Go through the disk queues to initiate sandbagged IO;
   1661  * go through the inodes to write those that have been modified;
   1662  * initiate the writing of the super block if it has been modified.
   1663  *
   1664  * Note: we are always called with the filesystem marked `MPBUSY'.
   1665  */
   1666 int
   1667 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1668 {
   1669 	struct lwp *l = curlwp;
   1670 	struct vnode *vp, *mvp, *nvp;
   1671 	struct inode *ip;
   1672 	struct ufsmount *ump = VFSTOUFS(mp);
   1673 	struct fs *fs;
   1674 	int error, count, allerror = 0;
   1675 
   1676 	fs = ump->um_fs;
   1677 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1678 		printf("fs = %s\n", fs->fs_fsmnt);
   1679 		panic("update: rofs mod");
   1680 	}
   1681 
   1682 	/* Allocate a marker vnode. */
   1683 	if ((mvp = vnalloc(mp)) == NULL)
   1684 		return (ENOMEM);
   1685 
   1686 	fstrans_start(mp, FSTRANS_SHARED);
   1687 	/*
   1688 	 * Write back each (modified) inode.
   1689 	 */
   1690 	mutex_enter(&mntvnode_lock);
   1691 loop:
   1692 	/*
   1693 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
   1694 	 * and vclean() can be called indirectly
   1695 	 */
   1696 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
   1697 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
   1698 		/*
   1699 		 * If the vnode that we are about to sync is no longer
   1700 		 * associated with this mount point, start over.
   1701 		 */
   1702 		if (vp->v_mount != mp)
   1703 			goto loop;
   1704 		/*
   1705 		 * Don't interfere with concurrent scans of this FS.
   1706 		 */
   1707 		if (vismarker(vp))
   1708 			continue;
   1709 		mutex_enter(&vp->v_interlock);
   1710 		ip = VTOI(vp);
   1711 
   1712 		/*
   1713 		 * Skip the vnode/inode if inaccessible.
   1714 		 */
   1715 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
   1716 		    vp->v_type == VNON) {
   1717 			mutex_exit(&vp->v_interlock);
   1718 			continue;
   1719 		}
   1720 
   1721 		/*
   1722 		 * We deliberately update inode times here.  This will
   1723 		 * prevent a massive queue of updates accumulating, only
   1724 		 * to be handled by a call to unmount.
   1725 		 *
   1726 		 * XXX It would be better to have the syncer trickle these
   1727 		 * out.  Adjustment needed to allow registering vnodes for
   1728 		 * sync when the vnode is clean, but the inode dirty.  Or
   1729 		 * have ufs itself trickle out inode updates.
   1730 		 *
   1731 		 * If doing a lazy sync, we don't care about metadata or
   1732 		 * data updates, because they are handled by each vnode's
   1733 		 * synclist entry.  In this case we are only interested in
   1734 		 * writing back modified inodes.
   1735 		 */
   1736 		if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
   1737 		    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1738 		    (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1739 		    UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
   1740 			mutex_exit(&vp->v_interlock);
   1741 			continue;
   1742 		}
   1743 		if (vp->v_type == VBLK &&
   1744 		    fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
   1745 			mutex_exit(&vp->v_interlock);
   1746 			continue;
   1747 		}
   1748 		vmark(mvp, vp);
   1749 		mutex_exit(&mntvnode_lock);
   1750 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1751 		if (error) {
   1752 			mutex_enter(&mntvnode_lock);
   1753 			nvp = vunmark(mvp);
   1754 			if (error == ENOENT) {
   1755 				goto loop;
   1756 			}
   1757 			continue;
   1758 		}
   1759 		if (waitfor == MNT_LAZY) {
   1760 			error = UFS_WAPBL_BEGIN(vp->v_mount);
   1761 			if (!error) {
   1762 				error = ffs_update(vp, NULL, NULL,
   1763 				    UPDATE_CLOSE);
   1764 				UFS_WAPBL_END(vp->v_mount);
   1765 			}
   1766 		} else {
   1767 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
   1768 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
   1769 		}
   1770 		if (error)
   1771 			allerror = error;
   1772 		vput(vp);
   1773 		mutex_enter(&mntvnode_lock);
   1774 		nvp = vunmark(mvp);
   1775 	}
   1776 	mutex_exit(&mntvnode_lock);
   1777 	/*
   1778 	 * Force stale file system control information to be flushed.
   1779 	 */
   1780 	if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
   1781 		if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
   1782 			allerror = error;
   1783 		/* Flushed work items may create new vnodes to clean */
   1784 		if (allerror == 0 && count) {
   1785 			mutex_enter(&mntvnode_lock);
   1786 			goto loop;
   1787 		}
   1788 	}
   1789 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1790 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1791 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1792 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1793 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
   1794 		    0, 0)) != 0)
   1795 			allerror = error;
   1796 		VOP_UNLOCK(ump->um_devvp, 0);
   1797 		if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
   1798 			mutex_enter(&mntvnode_lock);
   1799 			goto loop;
   1800 		}
   1801 	}
   1802 #ifdef QUOTA
   1803 	qsync(mp);
   1804 #endif
   1805 	/*
   1806 	 * Write back modified superblock.
   1807 	 */
   1808 	if (fs->fs_fmod != 0) {
   1809 		fs->fs_fmod = 0;
   1810 		fs->fs_time = time_second;
   1811 		error = UFS_WAPBL_BEGIN(mp);
   1812 		if (error)
   1813 			allerror = error;
   1814 		else {
   1815 			if ((error = ffs_cgupdate(ump, waitfor)))
   1816 				allerror = error;
   1817 			UFS_WAPBL_END(mp);
   1818 		}
   1819 	}
   1820 
   1821 #ifdef WAPBL
   1822 	if (mp->mnt_wapbl) {
   1823 		error = wapbl_flush(mp->mnt_wapbl, 0);
   1824 		if (error)
   1825 			allerror = error;
   1826 	}
   1827 #endif
   1828 
   1829 	fstrans_done(mp);
   1830 	vnfree(mvp);
   1831 	return (allerror);
   1832 }
   1833 
   1834 /*
   1835  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1836  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1837  * return the inode locked.  Detection and handling of mount points must be
   1838  * done by the calling routine.
   1839  */
   1840 int
   1841 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1842 {
   1843 	struct fs *fs;
   1844 	struct inode *ip;
   1845 	struct ufsmount *ump;
   1846 	struct buf *bp;
   1847 	struct vnode *vp;
   1848 	dev_t dev;
   1849 	int error;
   1850 
   1851 	ump = VFSTOUFS(mp);
   1852 	dev = ump->um_dev;
   1853 
   1854  retry:
   1855 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1856 		return (0);
   1857 
   1858 	/* Allocate a new vnode/inode. */
   1859 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1860 		*vpp = NULL;
   1861 		return (error);
   1862 	}
   1863 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
   1864 
   1865 	/*
   1866 	 * If someone beat us to it, put back the freshly allocated
   1867 	 * vnode/inode pair and retry.
   1868 	 */
   1869 	mutex_enter(&ufs_hashlock);
   1870 	if (ufs_ihashget(dev, ino, 0) != NULL) {
   1871 		mutex_exit(&ufs_hashlock);
   1872 		ungetnewvnode(vp);
   1873 		pool_cache_put(ffs_inode_cache, ip);
   1874 		goto retry;
   1875 	}
   1876 
   1877 	vp->v_vflag |= VV_LOCKSWORK;
   1878 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0)
   1879 		vp->v_uflag |= VU_SOFTDEP;
   1880 
   1881 	/*
   1882 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1883 	 * XXX create another pool for MFS inodes?
   1884 	 */
   1885 
   1886 	memset(ip, 0, sizeof(struct inode));
   1887 	vp->v_data = ip;
   1888 	ip->i_vnode = vp;
   1889 	ip->i_ump = ump;
   1890 	ip->i_fs = fs = ump->um_fs;
   1891 	ip->i_dev = dev;
   1892 	ip->i_number = ino;
   1893 	LIST_INIT(&ip->i_pcbufhd);
   1894 #ifdef QUOTA
   1895 	ufsquota_init(ip);
   1896 #endif
   1897 
   1898 	/*
   1899 	 * Initialize genfs node, we might proceed to destroy it in
   1900 	 * error branches.
   1901 	 */
   1902 	genfs_node_init(vp, &ffs_genfsops);
   1903 
   1904 	/*
   1905 	 * Put it onto its hash chain and lock it so that other requests for
   1906 	 * this inode will block if they arrive while we are sleeping waiting
   1907 	 * for old data structures to be purged or for the contents of the
   1908 	 * disk portion of this inode to be read.
   1909 	 */
   1910 
   1911 	ufs_ihashins(ip);
   1912 	mutex_exit(&ufs_hashlock);
   1913 
   1914 	/* Read in the disk contents for the inode, copy into the inode. */
   1915 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1916 		      (int)fs->fs_bsize, NOCRED, 0, &bp);
   1917 	if (error) {
   1918 
   1919 		/*
   1920 		 * The inode does not contain anything useful, so it would
   1921 		 * be misleading to leave it on its hash chain. With mode
   1922 		 * still zero, it will be unlinked and returned to the free
   1923 		 * list by vput().
   1924 		 */
   1925 
   1926 		vput(vp);
   1927 		brelse(bp, 0);
   1928 		*vpp = NULL;
   1929 		return (error);
   1930 	}
   1931 	if (ip->i_ump->um_fstype == UFS1)
   1932 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
   1933 		    PR_WAITOK);
   1934 	else
   1935 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
   1936 		    PR_WAITOK);
   1937 	ffs_load_inode(bp, ip, fs, ino);
   1938 	if (DOINGSOFTDEP(vp))
   1939 		softdep_load_inodeblock(ip);
   1940 	else
   1941 		ip->i_ffs_effnlink = ip->i_nlink;
   1942 	brelse(bp, 0);
   1943 
   1944 	/*
   1945 	 * Initialize the vnode from the inode, check for aliases.
   1946 	 * Note that the underlying vnode may have changed.
   1947 	 */
   1948 
   1949 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1950 
   1951 	/*
   1952 	 * Finish inode initialization now that aliasing has been resolved.
   1953 	 */
   1954 
   1955 	ip->i_devvp = ump->um_devvp;
   1956 	VREF(ip->i_devvp);
   1957 
   1958 	/*
   1959 	 * Ensure that uid and gid are correct. This is a temporary
   1960 	 * fix until fsck has been changed to do the update.
   1961 	 */
   1962 
   1963 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1964 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1965 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1966 	}							/* XXX */
   1967 	uvm_vnp_setsize(vp, ip->i_size);
   1968 	*vpp = vp;
   1969 	return (0);
   1970 }
   1971 
   1972 /*
   1973  * File handle to vnode
   1974  *
   1975  * Have to be really careful about stale file handles:
   1976  * - check that the inode number is valid
   1977  * - call ffs_vget() to get the locked inode
   1978  * - check for an unallocated inode (i_mode == 0)
   1979  * - check that the given client host has export rights and return
   1980  *   those rights via. exflagsp and credanonp
   1981  */
   1982 int
   1983 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1984 {
   1985 	struct ufid ufh;
   1986 	struct fs *fs;
   1987 
   1988 	if (fhp->fid_len != sizeof(struct ufid))
   1989 		return EINVAL;
   1990 
   1991 	memcpy(&ufh, fhp, sizeof(ufh));
   1992 	fs = VFSTOUFS(mp)->um_fs;
   1993 	if (ufh.ufid_ino < ROOTINO ||
   1994 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1995 		return (ESTALE);
   1996 	return (ufs_fhtovp(mp, &ufh, vpp));
   1997 }
   1998 
   1999 /*
   2000  * Vnode pointer to File handle
   2001  */
   2002 /* ARGSUSED */
   2003 int
   2004 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   2005 {
   2006 	struct inode *ip;
   2007 	struct ufid ufh;
   2008 
   2009 	if (*fh_size < sizeof(struct ufid)) {
   2010 		*fh_size = sizeof(struct ufid);
   2011 		return E2BIG;
   2012 	}
   2013 	ip = VTOI(vp);
   2014 	*fh_size = sizeof(struct ufid);
   2015 	memset(&ufh, 0, sizeof(ufh));
   2016 	ufh.ufid_len = sizeof(struct ufid);
   2017 	ufh.ufid_ino = ip->i_number;
   2018 	ufh.ufid_gen = ip->i_gen;
   2019 	memcpy(fhp, &ufh, sizeof(ufh));
   2020 	return (0);
   2021 }
   2022 
   2023 void
   2024 ffs_init(void)
   2025 {
   2026 	if (ffs_initcount++ > 0)
   2027 		return;
   2028 
   2029 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
   2030 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
   2031 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
   2032 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
   2033 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
   2034 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
   2035 	softdep_initialize();
   2036 	ufs_init();
   2037 }
   2038 
   2039 void
   2040 ffs_reinit(void)
   2041 {
   2042 	softdep_reinitialize();
   2043 	ufs_reinit();
   2044 }
   2045 
   2046 void
   2047 ffs_done(void)
   2048 {
   2049 	if (--ffs_initcount > 0)
   2050 		return;
   2051 
   2052 	/* XXX softdep cleanup ? */
   2053 	ufs_done();
   2054 	pool_cache_destroy(ffs_dinode2_cache);
   2055 	pool_cache_destroy(ffs_dinode1_cache);
   2056 	pool_cache_destroy(ffs_inode_cache);
   2057 }
   2058 
   2059 /*
   2060  * Write a superblock and associated information back to disk.
   2061  */
   2062 int
   2063 ffs_sbupdate(struct ufsmount *mp, int waitfor)
   2064 {
   2065 	struct fs *fs = mp->um_fs;
   2066 	struct buf *bp;
   2067 	int error = 0;
   2068 	u_int32_t saveflag;
   2069 
   2070 	error = ffs_getblk(mp->um_devvp,
   2071 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb), FFS_NOBLK,
   2072 	    fs->fs_sbsize, false, &bp);
   2073 	if (error)
   2074 		return error;
   2075 	saveflag = fs->fs_flags & FS_INTERNAL;
   2076 	fs->fs_flags &= ~FS_INTERNAL;
   2077 
   2078 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   2079 
   2080 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   2081 #ifdef FFS_EI
   2082 	if (mp->um_flags & UFS_NEEDSWAP)
   2083 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   2084 #endif
   2085 	fs->fs_flags |= saveflag;
   2086 
   2087 	if (waitfor == MNT_WAIT)
   2088 		error = bwrite(bp);
   2089 	else
   2090 		bawrite(bp);
   2091 	return (error);
   2092 }
   2093 
   2094 int
   2095 ffs_cgupdate(struct ufsmount *mp, int waitfor)
   2096 {
   2097 	struct fs *fs = mp->um_fs;
   2098 	struct buf *bp;
   2099 	int blks;
   2100 	void *space;
   2101 	int i, size, error = 0, allerror = 0;
   2102 
   2103 	allerror = ffs_sbupdate(mp, waitfor);
   2104 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   2105 	space = fs->fs_csp;
   2106 	for (i = 0; i < blks; i += fs->fs_frag) {
   2107 		size = fs->fs_bsize;
   2108 		if (i + fs->fs_frag > blks)
   2109 			size = (blks - i) * fs->fs_fsize;
   2110 		error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   2111 		    FFS_NOBLK, size, false, &bp);
   2112 		if (error)
   2113 			break;
   2114 #ifdef FFS_EI
   2115 		if (mp->um_flags & UFS_NEEDSWAP)
   2116 			ffs_csum_swap((struct csum*)space,
   2117 			    (struct csum*)bp->b_data, size);
   2118 		else
   2119 #endif
   2120 			memcpy(bp->b_data, space, (u_int)size);
   2121 		space = (char *)space + size;
   2122 		if (waitfor == MNT_WAIT)
   2123 			error = bwrite(bp);
   2124 		else
   2125 			bawrite(bp);
   2126 	}
   2127 	if (!allerror && error)
   2128 		allerror = error;
   2129 	return (allerror);
   2130 }
   2131 
   2132 int
   2133 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2134     int attrnamespace, const char *attrname)
   2135 {
   2136 #ifdef UFS_EXTATTR
   2137 	/*
   2138 	 * File-backed extended attributes are only supported on UFS1.
   2139 	 * UFS2 has native extended attributes.
   2140 	 */
   2141 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   2142 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
   2143 #endif
   2144 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
   2145 }
   2146 
   2147 int
   2148 ffs_suspendctl(struct mount *mp, int cmd)
   2149 {
   2150 	int error;
   2151 	struct lwp *l = curlwp;
   2152 
   2153 	switch (cmd) {
   2154 	case SUSPEND_SUSPEND:
   2155 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   2156 			return error;
   2157 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
   2158 		if (error == 0)
   2159 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   2160 #ifdef WAPBL
   2161 		if (error == 0 && mp->mnt_wapbl)
   2162 			error = wapbl_flush(mp->mnt_wapbl, 1);
   2163 #endif
   2164 		if (error != 0) {
   2165 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   2166 			return error;
   2167 		}
   2168 		return 0;
   2169 
   2170 	case SUSPEND_RESUME:
   2171 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   2172 
   2173 	default:
   2174 		return EINVAL;
   2175 	}
   2176 }
   2177 
   2178 /*
   2179  * Synch vnode for a mounted file system.  This is called for foreign
   2180  * vnodes, i.e. non-ffs.
   2181  */
   2182 static int
   2183 ffs_vfs_fsync(vnode_t *vp, int flags)
   2184 {
   2185 	int error, passes, skipmeta, i, pflags;
   2186 	buf_t *bp, *nbp;
   2187 	struct mount *mp;
   2188 
   2189 	KASSERT(vp->v_type == VBLK);
   2190 	KASSERT(vp->v_specmountpoint != NULL);
   2191 
   2192 	mp = vp->v_specmountpoint;
   2193 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0)
   2194 		softdep_fsync_mountdev(vp);
   2195 
   2196 	/*
   2197 	 * Flush all dirty data associated with the vnode.
   2198 	 */
   2199 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
   2200 	if ((flags & FSYNC_WAIT) != 0)
   2201 		pflags |= PGO_SYNCIO;
   2202 	mutex_enter(&vp->v_interlock);
   2203 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
   2204 	if (error)
   2205 		return error;
   2206 
   2207 #ifdef WAPBL
   2208 	if (mp && mp->mnt_wapbl) {
   2209 		/*
   2210 		 * Don't bother writing out metadata if the syncer is
   2211 		 * making the request.  We will let the sync vnode
   2212 		 * write it out in a single burst through a call to
   2213 		 * VFS_SYNC().
   2214 		 */
   2215 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
   2216 			return 0;
   2217 
   2218 		/*
   2219 		 * Don't flush the log if the vnode being flushed
   2220 		 * contains no dirty buffers that could be in the log.
   2221 		 */
   2222 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2223 			error = wapbl_flush(mp->mnt_wapbl, 0);
   2224 			if (error)
   2225 				return error;
   2226 		}
   2227 
   2228 		if ((flags & FSYNC_WAIT) != 0) {
   2229 			mutex_enter(&vp->v_interlock);
   2230 			while (vp->v_numoutput)
   2231 				cv_wait(&vp->v_cv, &vp->v_interlock);
   2232 			mutex_exit(&vp->v_interlock);
   2233 		}
   2234 
   2235 		return 0;
   2236 	}
   2237 #endif /* WAPBL */
   2238 
   2239 	/*
   2240 	 * Write out metadata for non-logging file systems.  This block can
   2241 	 * be simplified once softdep goes.
   2242 	 */
   2243 	passes = NIADDR + 1;
   2244 	skipmeta = 0;
   2245 	if (flags & FSYNC_WAIT)
   2246 		skipmeta = 1;
   2247 
   2248 loop:
   2249 	mutex_enter(&bufcache_lock);
   2250 	LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
   2251 		bp->b_cflags &= ~BC_SCANNED;
   2252 	}
   2253 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
   2254 		nbp = LIST_NEXT(bp, b_vnbufs);
   2255 		if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
   2256 			continue;
   2257 		if ((bp->b_oflags & BO_DELWRI) == 0)
   2258 			panic("ffs_fsync: not dirty");
   2259 		if (skipmeta && bp->b_lblkno < 0)
   2260 			continue;
   2261 		bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
   2262 		mutex_exit(&bufcache_lock);
   2263 		/*
   2264 		 * On our final pass through, do all I/O synchronously
   2265 		 * so that we can find out if our flush is failing
   2266 		 * because of write errors.
   2267 		 */
   2268 		if (passes > 0 || !(flags & FSYNC_WAIT))
   2269 			(void) bawrite(bp);
   2270 		else if ((error = bwrite(bp)) != 0)
   2271 			return (error);
   2272 		/*
   2273 		 * Since we unlocked during the I/O, we need
   2274 		 * to start from a known point.
   2275 		 */
   2276 		mutex_enter(&bufcache_lock);
   2277 		nbp = LIST_FIRST(&vp->v_dirtyblkhd);
   2278 	}
   2279 	mutex_exit(&bufcache_lock);
   2280 	if (skipmeta) {
   2281 		skipmeta = 0;
   2282 		goto loop;
   2283 	}
   2284 
   2285 	if ((flags & FSYNC_WAIT) != 0) {
   2286 		mutex_enter(&vp->v_interlock);
   2287 		while (vp->v_numoutput) {
   2288 			cv_wait(&vp->v_cv, &vp->v_interlock);
   2289 		}
   2290 		mutex_exit(&vp->v_interlock);
   2291 
   2292 		/*
   2293 		 * Ensure that any filesystem metadata associated
   2294 		 * with the vnode has been written.
   2295 		 */
   2296 		if ((error = softdep_sync_metadata(vp)) != 0)
   2297 			return (error);
   2298 
   2299 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2300 			/*
   2301 			* Block devices associated with filesystems may
   2302 			* have new I/O requests posted for them even if
   2303 			* the vnode is locked, so no amount of trying will
   2304 			* get them clean. Thus we give block devices a
   2305 			* good effort, then just give up. For all other file
   2306 			* types, go around and try again until it is clean.
   2307 			*/
   2308 			if (passes > 0) {
   2309 				passes--;
   2310 				goto loop;
   2311 			}
   2312 #ifdef DIAGNOSTIC
   2313 			if (vp->v_type != VBLK)
   2314 				vprint("ffs_fsync: dirty", vp);
   2315 #endif
   2316 		}
   2317 	}
   2318 
   2319 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
   2320 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
   2321 		    kauth_cred_get());
   2322 	}
   2323 
   2324 	return error;
   2325 }
   2326