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