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