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