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