Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.309
      1 /*	$NetBSD: ffs_vfsops.c,v 1.309 2015/02/13 17:13:20 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.309 2015/02/13 17:13:20 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 || fs_bsize < MINBSIZE)
    916 		return 0;
    917 	if (!powerof2(fs_bsize))
    918 		return 0;
    919 
    920 	/* Check the size of frag blocks */
    921 	if (!powerof2(fs->fs_fsize))
    922 		return 0;
    923 
    924 	if (fs->fs_size == 0)
    925 		return 0;
    926 	if (fs->fs_cssize == 0)
    927 		return 0;
    928 
    929 	/* Block size cannot be smaller than fragment size */
    930 	if (fs_bsize < fs->fs_fsize)
    931 		return 0;
    932 
    933 	return 1;
    934 }
    935 
    936 /*
    937  * Common code for mount and mountroot
    938  */
    939 int
    940 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    941 {
    942 	struct ufsmount *ump = NULL;
    943 	struct buf *bp = NULL;
    944 	struct fs *fs = NULL;
    945 	dev_t dev;
    946 	struct dkwedge_info dkw;
    947 	void *space;
    948 	daddr_t sblockloc = 0;
    949 	int blks, fstype = 0;
    950 	int error, i, bsize, ronly, bset = 0;
    951 #ifdef FFS_EI
    952 	int needswap = 0;		/* keep gcc happy */
    953 #endif
    954 	int32_t *lp;
    955 	kauth_cred_t cred;
    956 	u_int32_t fs_sbsize = 8192;	/* keep gcc happy*/
    957 	u_int32_t allocsbsize;
    958 
    959 	dev = devvp->v_rdev;
    960 	cred = l ? l->l_cred : NOCRED;
    961 
    962 	/* Flush out any old buffers remaining from a previous use. */
    963 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    964 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    965 	VOP_UNLOCK(devvp);
    966 	if (error) {
    967 		DPRINTF(("%s: vinvalbuf %d\n", __func__, error));
    968 		return error;
    969 	}
    970 
    971 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    972 
    973 	error = fstrans_mount(mp);
    974 	if (error) {
    975 		DPRINTF(("%s: fstrans_mount %d\n", __func__, error));
    976 		return error;
    977 	}
    978 
    979 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
    980 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
    981 	error = ffs_snapshot_init(ump);
    982 	if (error) {
    983 		DPRINTF(("%s: ffs_snapshot_init %d\n", __func__, error));
    984 		goto out;
    985 	}
    986 	ump->um_ops = &ffs_ufsops;
    987 
    988 #ifdef WAPBL
    989  sbagain:
    990 #endif
    991 	/*
    992 	 * Try reading the superblock in each of its possible locations.
    993 	 */
    994 	for (i = 0; ; i++) {
    995 		daddr_t fsblockloc;
    996 		int32_t fs_bsize;
    997 
    998 		if (bp != NULL) {
    999 			brelse(bp, BC_NOCACHE);
   1000 			bp = NULL;
   1001 		}
   1002 		if (sblock_try[i] == -1) {
   1003 			DPRINTF(("%s: sblock_try\n", __func__));
   1004 			error = EINVAL;
   1005 			fs = NULL;
   1006 			goto out;
   1007 		}
   1008 
   1009 		error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE,
   1010 		    cred, 0, &bp);
   1011 		if (error) {
   1012 			DPRINTF(("%s: bread@0x%x %d\n", __func__,
   1013 			    sblock_try[i] / DEV_BSIZE, error));
   1014 			fs = NULL;
   1015 			goto out;
   1016 		}
   1017 		fs = (struct fs*)bp->b_data;
   1018 
   1019 		fsblockloc = sblockloc = sblock_try[i];
   1020 		DPRINTF(("%s: fs_magic 0x%x\n", __func__, fs->fs_magic));
   1021 		if (fs->fs_magic == FS_UFS1_MAGIC) {
   1022 			fs_sbsize = fs->fs_sbsize;
   1023 			fstype = UFS1;
   1024 			fs_bsize = fs->fs_bsize;
   1025 #ifdef FFS_EI
   1026 			needswap = 0;
   1027 		} else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) {
   1028 			fs_sbsize = bswap32(fs->fs_sbsize);
   1029 			fstype = UFS1;
   1030 			fs_bsize = bswap32(fs->fs_bsize);
   1031 			needswap = 1;
   1032 #endif
   1033 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
   1034 			fs_sbsize = fs->fs_sbsize;
   1035 			fstype = UFS2;
   1036 			fs_bsize = fs->fs_bsize;
   1037 #ifdef FFS_EI
   1038 			needswap = 0;
   1039 		} else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
   1040 			fs_sbsize = bswap32(fs->fs_sbsize);
   1041 			fstype = UFS2;
   1042 			fs_bsize = bswap32(fs->fs_bsize);
   1043 			needswap = 1;
   1044 #endif
   1045 		} else
   1046 			continue;
   1047 
   1048 		/* fs->fs_sblockloc isn't defined for old filesystems */
   1049 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
   1050 			if (sblockloc == SBLOCK_UFS2)
   1051 				/*
   1052 				 * This is likely to be the first alternate
   1053 				 * in a filesystem with 64k blocks.
   1054 				 * Don't use it.
   1055 				 */
   1056 				continue;
   1057 			fsblockloc = sblockloc;
   1058 		} else {
   1059 			fsblockloc = fs->fs_sblockloc;
   1060 #ifdef FFS_EI
   1061 			if (needswap)
   1062 				fsblockloc = bswap64(fsblockloc);
   1063 #endif
   1064 		}
   1065 
   1066 		/* Check we haven't found an alternate superblock */
   1067 		if (fsblockloc != sblockloc)
   1068 			continue;
   1069 
   1070 		if (!ffs_superblock_validate(fs, fs_sbsize, fs_bsize))
   1071 			continue;
   1072 
   1073 		/* Ok seems to be a good superblock */
   1074 		break;
   1075 	}
   1076 
   1077 	fs = kmem_alloc((u_long)fs_sbsize, KM_SLEEP);
   1078 	memcpy(fs, bp->b_data, fs_sbsize);
   1079 	ump->um_fs = fs;
   1080 
   1081 #ifdef FFS_EI
   1082 	if (needswap) {
   1083 		ffs_sb_swap((struct fs*)bp->b_data, fs);
   1084 		fs->fs_flags |= FS_SWAPPED;
   1085 	} else
   1086 #endif
   1087 		fs->fs_flags &= ~FS_SWAPPED;
   1088 
   1089 #ifdef WAPBL
   1090 	if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
   1091 		error = ffs_wapbl_replay_start(mp, fs, devvp);
   1092 		if (error && (mp->mnt_flag & MNT_FORCE) == 0) {
   1093 			DPRINTF(("%s: ffs_wapbl_replay_start %d\n", __func__,
   1094 			    error));
   1095 			goto out;
   1096 		}
   1097 		if (!error) {
   1098 			if (!ronly) {
   1099 				/* XXX fsmnt may be stale. */
   1100 				printf("%s: replaying log to disk\n",
   1101 				    fs->fs_fsmnt);
   1102 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
   1103 				    devvp);
   1104 				if (error) {
   1105 					DPRINTF(("%s: wapbl_replay_write %d\n",
   1106 					    __func__, error));
   1107 					goto out;
   1108 				}
   1109 				wapbl_replay_stop(mp->mnt_wapbl_replay);
   1110 				fs->fs_clean = FS_WASCLEAN;
   1111 			} else {
   1112 				/* XXX fsmnt may be stale */
   1113 				printf("%s: replaying log to memory\n",
   1114 				    fs->fs_fsmnt);
   1115 			}
   1116 
   1117 			/* Force a re-read of the superblock */
   1118 			brelse(bp, BC_INVAL);
   1119 			bp = NULL;
   1120 			kmem_free(fs, fs_sbsize);
   1121 			fs = NULL;
   1122 			goto sbagain;
   1123 		}
   1124 	}
   1125 #else /* !WAPBL */
   1126 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
   1127 		error = EPERM;
   1128 		DPRINTF(("%s: no force %d\n", __func__, error));
   1129 		goto out;
   1130 	}
   1131 #endif /* !WAPBL */
   1132 
   1133 	ffs_oldfscompat_read(fs, ump, sblockloc);
   1134 	ump->um_maxfilesize = fs->fs_maxfilesize;
   1135 
   1136 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
   1137 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
   1138 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
   1139 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1140 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1141 			error = EINVAL;
   1142 			DPRINTF(("%s: no force %d\n", __func__, error));
   1143 			goto out;
   1144 		}
   1145 	}
   1146 
   1147 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1148 		fs->fs_pendingblocks = 0;
   1149 		fs->fs_pendinginodes = 0;
   1150 	}
   1151 
   1152 	ump->um_fstype = fstype;
   1153 	if (fs->fs_sbsize < SBLOCKSIZE)
   1154 		brelse(bp, BC_INVAL);
   1155 	else
   1156 		brelse(bp, 0);
   1157 	bp = NULL;
   1158 
   1159 	/* First check to see if this is tagged as an Apple UFS filesystem
   1160 	 * in the disklabel
   1161 	 */
   1162 	if (getdiskinfo(devvp, &dkw) == 0 &&
   1163 	    strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
   1164 		ump->um_flags |= UFS_ISAPPLEUFS;
   1165 #ifdef APPLE_UFS
   1166 	else {
   1167 		/* Manually look for an apple ufs label, and if a valid one
   1168 		 * is found, then treat it like an Apple UFS filesystem anyway
   1169 		 */
   1170 		error = bread(devvp,
   1171 		    (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
   1172 		    APPLEUFS_LABEL_SIZE, cred, 0, &bp);
   1173 		if (error) {
   1174 			DPRINTF(("%s: apple bread@0x%jx %d\n", __func__,
   1175 			    (intmax_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
   1176 			    error));
   1177 			goto out;
   1178 		}
   1179 		error = ffs_appleufs_validate(fs->fs_fsmnt,
   1180 		    (struct appleufslabel *)bp->b_data, NULL);
   1181 		if (error == 0)
   1182 			ump->um_flags |= UFS_ISAPPLEUFS;
   1183 		brelse(bp, 0);
   1184 		bp = NULL;
   1185 	}
   1186 #else
   1187 	if (ump->um_flags & UFS_ISAPPLEUFS) {
   1188 		DPRINTF(("%s: bad apple\n", __func__));
   1189 		error = EINVAL;
   1190 		goto out;
   1191 	}
   1192 #endif
   1193 
   1194 #if 0
   1195 /*
   1196  * XXX This code changes the behaviour of mounting dirty filesystems, to
   1197  * XXX require "mount -f ..." to mount them.  This doesn't match what
   1198  * XXX mount(8) describes and is disabled for now.
   1199  */
   1200 	/*
   1201 	 * If the file system is not clean, don't allow it to be mounted
   1202 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
   1203 	 * for the root file system.)
   1204 	 */
   1205 	if (fs->fs_flags & FS_DOWAPBL) {
   1206 		/*
   1207 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
   1208 		 * bit is set, although there's a window in unmount where it
   1209 		 * could be FS_ISCLEAN
   1210 		 */
   1211 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
   1212 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
   1213 			error = EPERM;
   1214 			goto out;
   1215 		}
   1216 	} else
   1217 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
   1218 		    (mp->mnt_flag & MNT_FORCE) == 0) {
   1219 			error = EPERM;
   1220 			goto out;
   1221 		}
   1222 #endif
   1223 
   1224 	/*
   1225 	 * Verify that we can access the last block in the fs
   1226 	 * if we're mounting read/write.
   1227 	 */
   1228 
   1229 	if (!ronly) {
   1230 		error = bread(devvp, FFS_FSBTODB(fs, fs->fs_size - 1),
   1231 		    fs->fs_fsize, cred, 0, &bp);
   1232 		if (error) {
   1233 			DPRINTF(("%s: bread@0x%jx %d\n", __func__,
   1234 			    (intmax_t)FFS_FSBTODB(fs, fs->fs_size - 1),
   1235 			    error));
   1236 			bset = BC_INVAL;
   1237 			goto out;
   1238 		}
   1239 		if (bp->b_bcount != fs->fs_fsize) {
   1240 			DPRINTF(("%s: bcount %x != fsize %x\n", __func__,
   1241 			    bp->b_bcount, fs->fs_fsize));
   1242 			error = EINVAL;
   1243 		}
   1244 		brelse(bp, BC_INVAL);
   1245 		bp = NULL;
   1246 	}
   1247 
   1248 	fs->fs_ronly = ronly;
   1249 	/* Don't bump fs_clean if we're replaying journal */
   1250 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN))) {
   1251 		if (ronly == 0) {
   1252 			fs->fs_clean <<= 1;
   1253 			fs->fs_fmod = 1;
   1254 		}
   1255 	}
   1256 
   1257 	bsize = fs->fs_cssize;
   1258 	blks = howmany(bsize, fs->fs_fsize);
   1259 	if (fs->fs_contigsumsize > 0)
   1260 		bsize += fs->fs_ncg * sizeof(int32_t);
   1261 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1262 	allocsbsize = bsize;
   1263 	space = kmem_alloc((u_long)allocsbsize, KM_SLEEP);
   1264 	fs->fs_csp = space;
   1265 
   1266 	for (i = 0; i < blks; i += fs->fs_frag) {
   1267 		bsize = fs->fs_bsize;
   1268 		if (i + fs->fs_frag > blks)
   1269 			bsize = (blks - i) * fs->fs_fsize;
   1270 		error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize,
   1271 			      cred, 0, &bp);
   1272 		if (error) {
   1273 			DPRINTF(("%s: bread@0x%jx %d\n", __func__,
   1274 			    (intmax_t)FFS_FSBTODB(fs, fs->fs_csaddr + i),
   1275 			    error));
   1276 			goto out1;
   1277 		}
   1278 #ifdef FFS_EI
   1279 		if (needswap)
   1280 			ffs_csum_swap((struct csum *)bp->b_data,
   1281 				(struct csum *)space, bsize);
   1282 		else
   1283 #endif
   1284 			memcpy(space, bp->b_data, (u_int)bsize);
   1285 
   1286 		space = (char *)space + bsize;
   1287 		brelse(bp, 0);
   1288 		bp = NULL;
   1289 	}
   1290 	if (fs->fs_contigsumsize > 0) {
   1291 		fs->fs_maxcluster = lp = space;
   1292 		for (i = 0; i < fs->fs_ncg; i++)
   1293 			*lp++ = fs->fs_contigsumsize;
   1294 		space = lp;
   1295 	}
   1296 	bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1297 	fs->fs_contigdirs = space;
   1298 	space = (char *)space + bsize;
   1299 	memset(fs->fs_contigdirs, 0, bsize);
   1300 		/* Compatibility for old filesystems - XXX */
   1301 	if (fs->fs_avgfilesize <= 0)
   1302 		fs->fs_avgfilesize = AVFILESIZ;
   1303 	if (fs->fs_avgfpdir <= 0)
   1304 		fs->fs_avgfpdir = AFPDIR;
   1305 	fs->fs_active = NULL;
   1306 	mp->mnt_data = ump;
   1307 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1308 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
   1309 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1310 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
   1311 	if (UFS_MPISAPPLEUFS(ump)) {
   1312 		/* NeXT used to keep short symlinks in the inode even
   1313 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
   1314 		 * is probably -1, but we still need to be able to identify
   1315 		 * short symlinks.
   1316 		 */
   1317 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
   1318 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
   1319 		mp->mnt_iflag |= IMNT_DTYPE;
   1320 	} else {
   1321 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
   1322 		ump->um_dirblksiz = UFS_DIRBLKSIZ;
   1323 		if (ump->um_maxsymlinklen > 0)
   1324 			mp->mnt_iflag |= IMNT_DTYPE;
   1325 		else
   1326 			mp->mnt_iflag &= ~IMNT_DTYPE;
   1327 	}
   1328 	mp->mnt_fs_bshift = fs->fs_bshift;
   1329 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
   1330 	mp->mnt_flag |= MNT_LOCAL;
   1331 	mp->mnt_iflag |= IMNT_MPSAFE;
   1332 #ifdef FFS_EI
   1333 	if (needswap)
   1334 		ump->um_flags |= UFS_NEEDSWAP;
   1335 #endif
   1336 	ump->um_mountp = mp;
   1337 	ump->um_dev = dev;
   1338 	ump->um_devvp = devvp;
   1339 	ump->um_nindir = fs->fs_nindir;
   1340 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
   1341 	ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
   1342 	ump->um_seqinc = fs->fs_frag;
   1343 	for (i = 0; i < MAXQUOTAS; i++)
   1344 		ump->um_quotas[i] = NULLVP;
   1345 	spec_node_setmountedfs(devvp, mp);
   1346 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1347 		ffs_snapshot_mount(mp);
   1348 #ifdef WAPBL
   1349 	if (!ronly) {
   1350 		KDASSERT(fs->fs_ronly == 0);
   1351 		/*
   1352 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
   1353 		 * needs to create a new log file in-filesystem.
   1354 		 */
   1355 		error = ffs_statvfs(mp, &mp->mnt_stat);
   1356 		if (error) {
   1357 			DPRINTF(("%s: ffs_statvfs %d\n", __func__, error));
   1358 			goto out1;
   1359 		}
   1360 
   1361 		error = ffs_wapbl_start(mp);
   1362 		if (error) {
   1363 			DPRINTF(("%s: ffs_wapbl_start %d\n", __func__, error));
   1364 			goto out1;
   1365 		}
   1366 	}
   1367 #endif /* WAPBL */
   1368 	if (ronly == 0) {
   1369 #ifdef QUOTA2
   1370 		error = ffs_quota2_mount(mp);
   1371 		if (error) {
   1372 			DPRINTF(("%s: ffs_quota2_mount %d\n", __func__, error));
   1373 			goto out1;
   1374 		}
   1375 #else
   1376 		if (fs->fs_flags & FS_DOQUOTA2) {
   1377 			ump->um_flags |= UFS_QUOTA2;
   1378 			uprintf("%s: options QUOTA2 not enabled%s\n",
   1379 			    mp->mnt_stat.f_mntonname,
   1380 			    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1381 			if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1382 				error = EINVAL;
   1383 				DPRINTF(("%s: quota disabled %d\n", __func__,
   1384 				    error));
   1385 				goto out1;
   1386 			}
   1387 		}
   1388 #endif
   1389 	 }
   1390 
   1391 	if (mp->mnt_flag & MNT_DISCARD)
   1392 		ump->um_discarddata = ffs_discard_init(devvp, fs);
   1393 
   1394 	return (0);
   1395 out1:
   1396 	kmem_free(fs->fs_csp, allocsbsize);
   1397 out:
   1398 #ifdef WAPBL
   1399 	if (mp->mnt_wapbl_replay) {
   1400 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1401 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1402 		mp->mnt_wapbl_replay = 0;
   1403 	}
   1404 #endif
   1405 
   1406 	fstrans_unmount(mp);
   1407 	if (fs)
   1408 		kmem_free(fs, fs->fs_sbsize);
   1409 	spec_node_setmountedfs(devvp, NULL);
   1410 	if (bp)
   1411 		brelse(bp, bset);
   1412 	if (ump) {
   1413 		if (ump->um_oldfscompat)
   1414 			kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
   1415 		mutex_destroy(&ump->um_lock);
   1416 		kmem_free(ump, sizeof(*ump));
   1417 		mp->mnt_data = NULL;
   1418 	}
   1419 	return (error);
   1420 }
   1421 
   1422 /*
   1423  * Sanity checks for loading old filesystem superblocks.
   1424  * See ffs_oldfscompat_write below for unwound actions.
   1425  *
   1426  * XXX - Parts get retired eventually.
   1427  * Unfortunately new bits get added.
   1428  */
   1429 static void
   1430 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1431 {
   1432 	off_t maxfilesize;
   1433 	int32_t *extrasave;
   1434 
   1435 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1436 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1437 		return;
   1438 
   1439 	if (!ump->um_oldfscompat)
   1440 		ump->um_oldfscompat = kmem_alloc(512 + 3*sizeof(int32_t),
   1441 		    KM_SLEEP);
   1442 
   1443 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1444 	extrasave = ump->um_oldfscompat;
   1445 	extrasave += 512/sizeof(int32_t);
   1446 	extrasave[0] = fs->fs_old_npsect;
   1447 	extrasave[1] = fs->fs_old_interleave;
   1448 	extrasave[2] = fs->fs_old_trackskew;
   1449 
   1450 	/* These fields will be overwritten by their
   1451 	 * original values in fs_oldfscompat_write, so it is harmless
   1452 	 * to modify them here.
   1453 	 */
   1454 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1455 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1456 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1457 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1458 
   1459 	fs->fs_maxbsize = fs->fs_bsize;
   1460 	fs->fs_time = fs->fs_old_time;
   1461 	fs->fs_size = fs->fs_old_size;
   1462 	fs->fs_dsize = fs->fs_old_dsize;
   1463 	fs->fs_csaddr = fs->fs_old_csaddr;
   1464 	fs->fs_sblockloc = sblockloc;
   1465 
   1466 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1467 
   1468 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1469 		fs->fs_old_nrpos = 8;
   1470 		fs->fs_old_npsect = fs->fs_old_nsect;
   1471 		fs->fs_old_interleave = 1;
   1472 		fs->fs_old_trackskew = 0;
   1473 	}
   1474 
   1475 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1476 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1477 		fs->fs_qbmask = ~fs->fs_bmask;
   1478 		fs->fs_qfmask = ~fs->fs_fmask;
   1479 	}
   1480 
   1481 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1482 	if (fs->fs_maxfilesize > maxfilesize)
   1483 		fs->fs_maxfilesize = maxfilesize;
   1484 
   1485 	/* Compatibility for old filesystems */
   1486 	if (fs->fs_avgfilesize <= 0)
   1487 		fs->fs_avgfilesize = AVFILESIZ;
   1488 	if (fs->fs_avgfpdir <= 0)
   1489 		fs->fs_avgfpdir = AFPDIR;
   1490 
   1491 #if 0
   1492 	if (bigcgs) {
   1493 		fs->fs_save_cgsize = fs->fs_cgsize;
   1494 		fs->fs_cgsize = fs->fs_bsize;
   1495 	}
   1496 #endif
   1497 }
   1498 
   1499 /*
   1500  * Unwinding superblock updates for old filesystems.
   1501  * See ffs_oldfscompat_read above for details.
   1502  *
   1503  * XXX - Parts get retired eventually.
   1504  * Unfortunately new bits get added.
   1505  */
   1506 static void
   1507 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1508 {
   1509 	int32_t *extrasave;
   1510 
   1511 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1512 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1513 		return;
   1514 
   1515 	fs->fs_old_time = fs->fs_time;
   1516 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1517 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1518 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1519 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1520 	fs->fs_old_flags = fs->fs_flags;
   1521 
   1522 #if 0
   1523 	if (bigcgs) {
   1524 		fs->fs_cgsize = fs->fs_save_cgsize;
   1525 	}
   1526 #endif
   1527 
   1528 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1529 	extrasave = ump->um_oldfscompat;
   1530 	extrasave += 512/sizeof(int32_t);
   1531 	fs->fs_old_npsect = extrasave[0];
   1532 	fs->fs_old_interleave = extrasave[1];
   1533 	fs->fs_old_trackskew = extrasave[2];
   1534 
   1535 }
   1536 
   1537 /*
   1538  * unmount vfs operation
   1539  */
   1540 int
   1541 ffs_unmount(struct mount *mp, int mntflags)
   1542 {
   1543 	struct lwp *l = curlwp;
   1544 	struct ufsmount *ump = VFSTOUFS(mp);
   1545 	struct fs *fs = ump->um_fs;
   1546 	int error, flags;
   1547 	u_int32_t bsize;
   1548 #ifdef WAPBL
   1549 	extern int doforce;
   1550 #endif
   1551 
   1552 	if (ump->um_discarddata) {
   1553 		ffs_discard_finish(ump->um_discarddata, mntflags);
   1554 		ump->um_discarddata = NULL;
   1555 	}
   1556 
   1557 	flags = 0;
   1558 	if (mntflags & MNT_FORCE)
   1559 		flags |= FORCECLOSE;
   1560 	if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1561 		return (error);
   1562 	error = UFS_WAPBL_BEGIN(mp);
   1563 	if (error == 0)
   1564 		if (fs->fs_ronly == 0 &&
   1565 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1566 		    fs->fs_clean & FS_WASCLEAN) {
   1567 			fs->fs_clean = FS_ISCLEAN;
   1568 			fs->fs_fmod = 0;
   1569 			(void) ffs_sbupdate(ump, MNT_WAIT);
   1570 		}
   1571 	if (error == 0)
   1572 		UFS_WAPBL_END(mp);
   1573 #ifdef WAPBL
   1574 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
   1575 	if (mp->mnt_wapbl_replay) {
   1576 		KDASSERT(fs->fs_ronly);
   1577 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1578 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1579 		mp->mnt_wapbl_replay = 0;
   1580 	}
   1581 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
   1582 	if (error) {
   1583 		return error;
   1584 	}
   1585 #endif /* WAPBL */
   1586 
   1587 	if (ump->um_devvp->v_type != VBAD)
   1588 		spec_node_setmountedfs(ump->um_devvp, NULL);
   1589 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1590 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
   1591 		NOCRED);
   1592 	vput(ump->um_devvp);
   1593 
   1594 	bsize = fs->fs_cssize;
   1595 	if (fs->fs_contigsumsize > 0)
   1596 		bsize += fs->fs_ncg * sizeof(int32_t);
   1597 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1598 	kmem_free(fs->fs_csp, bsize);
   1599 
   1600 	kmem_free(fs, fs->fs_sbsize);
   1601 	if (ump->um_oldfscompat != NULL)
   1602 		kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t));
   1603 	mutex_destroy(&ump->um_lock);
   1604 	ffs_snapshot_fini(ump);
   1605 	kmem_free(ump, sizeof(*ump));
   1606 	mp->mnt_data = NULL;
   1607 	mp->mnt_flag &= ~MNT_LOCAL;
   1608 	fstrans_unmount(mp);
   1609 	return (0);
   1610 }
   1611 
   1612 /*
   1613  * Flush out all the files in a filesystem.
   1614  */
   1615 int
   1616 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1617 {
   1618 	extern int doforce;
   1619 	struct ufsmount *ump;
   1620 	int error;
   1621 
   1622 	if (!doforce)
   1623 		flags &= ~FORCECLOSE;
   1624 	ump = VFSTOUFS(mp);
   1625 #ifdef QUOTA
   1626 	if ((error = quota1_umount(mp, flags)) != 0)
   1627 		return (error);
   1628 #endif
   1629 #ifdef QUOTA2
   1630 	if ((error = quota2_umount(mp, flags)) != 0)
   1631 		return (error);
   1632 #endif
   1633 #ifdef UFS_EXTATTR
   1634 	if (ump->um_fstype == UFS1) {
   1635 		if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
   1636 			ufs_extattr_stop(mp, l);
   1637 		if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)
   1638 			ufs_extattr_uepm_destroy(&ump->um_extattr);
   1639 		mp->mnt_flag &= ~MNT_EXTATTR;
   1640 	}
   1641 #endif
   1642 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1643 		return (error);
   1644 	ffs_snapshot_unmount(mp);
   1645 	/*
   1646 	 * Flush all the files.
   1647 	 */
   1648 	error = vflush(mp, NULLVP, flags);
   1649 	if (error)
   1650 		return (error);
   1651 	/*
   1652 	 * Flush filesystem metadata.
   1653 	 */
   1654 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1655 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
   1656 	VOP_UNLOCK(ump->um_devvp);
   1657 	if (flags & FORCECLOSE) /* XXXDBJ */
   1658 		error = 0;
   1659 
   1660 #ifdef WAPBL
   1661 	if (error)
   1662 		return error;
   1663 	if (mp->mnt_wapbl) {
   1664 		error = wapbl_flush(mp->mnt_wapbl, 1);
   1665 		if (flags & FORCECLOSE)
   1666 			error = 0;
   1667 	}
   1668 #endif
   1669 
   1670 	return (error);
   1671 }
   1672 
   1673 /*
   1674  * Get file system statistics.
   1675  */
   1676 int
   1677 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
   1678 {
   1679 	struct ufsmount *ump;
   1680 	struct fs *fs;
   1681 
   1682 	ump = VFSTOUFS(mp);
   1683 	fs = ump->um_fs;
   1684 	mutex_enter(&ump->um_lock);
   1685 	sbp->f_bsize = fs->fs_bsize;
   1686 	sbp->f_frsize = fs->fs_fsize;
   1687 	sbp->f_iosize = fs->fs_bsize;
   1688 	sbp->f_blocks = fs->fs_dsize;
   1689 	sbp->f_bfree = ffs_blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1690 	    fs->fs_cstotal.cs_nffree + FFS_DBTOFSB(fs, fs->fs_pendingblocks);
   1691 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1692 	    fs->fs_minfree) / (u_int64_t) 100;
   1693 	if (sbp->f_bfree > sbp->f_bresvd)
   1694 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1695 	else
   1696 		sbp->f_bavail = 0;
   1697 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO;
   1698 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1699 	sbp->f_favail = sbp->f_ffree;
   1700 	sbp->f_fresvd = 0;
   1701 	mutex_exit(&ump->um_lock);
   1702 	copy_statvfs_info(sbp, mp);
   1703 
   1704 	return (0);
   1705 }
   1706 
   1707 struct ffs_sync_ctx {
   1708 	int waitfor;
   1709 	bool is_suspending;
   1710 };
   1711 
   1712 static bool
   1713 ffs_sync_selector(void *cl, struct vnode *vp)
   1714 {
   1715 	struct ffs_sync_ctx *c = cl;
   1716 	struct inode *ip;
   1717 
   1718 	ip = VTOI(vp);
   1719 	/*
   1720 	 * Skip the vnode/inode if inaccessible.
   1721 	 */
   1722 	if (ip == NULL || vp->v_type == VNON)
   1723 		return false;
   1724 
   1725 	/*
   1726 	 * We deliberately update inode times here.  This will
   1727 	 * prevent a massive queue of updates accumulating, only
   1728 	 * to be handled by a call to unmount.
   1729 	 *
   1730 	 * XXX It would be better to have the syncer trickle these
   1731 	 * out.  Adjustment needed to allow registering vnodes for
   1732 	 * sync when the vnode is clean, but the inode dirty.  Or
   1733 	 * have ufs itself trickle out inode updates.
   1734 	 *
   1735 	 * If doing a lazy sync, we don't care about metadata or
   1736 	 * data updates, because they are handled by each vnode's
   1737 	 * synclist entry.  In this case we are only interested in
   1738 	 * writing back modified inodes.
   1739 	 */
   1740 	if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
   1741 	    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1742 	    (c->waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1743 	    UVM_OBJ_IS_CLEAN(&vp->v_uobj))))
   1744 		return false;
   1745 
   1746 	if (vp->v_type == VBLK && c->is_suspending)
   1747 		return false;
   1748 
   1749 	return true;
   1750 }
   1751 
   1752 /*
   1753  * Go through the disk queues to initiate sandbagged IO;
   1754  * go through the inodes to write those that have been modified;
   1755  * initiate the writing of the super block if it has been modified.
   1756  *
   1757  * Note: we are always called with the filesystem marked `MPBUSY'.
   1758  */
   1759 int
   1760 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1761 {
   1762 	struct vnode *vp;
   1763 	struct ufsmount *ump = VFSTOUFS(mp);
   1764 	struct fs *fs;
   1765 	struct vnode_iterator *marker;
   1766 	int error, allerror = 0;
   1767 	bool is_suspending;
   1768 	struct ffs_sync_ctx ctx;
   1769 
   1770 	fs = ump->um_fs;
   1771 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1772 		printf("fs = %s\n", fs->fs_fsmnt);
   1773 		panic("update: rofs mod");
   1774 	}
   1775 
   1776 	fstrans_start(mp, FSTRANS_SHARED);
   1777 	is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
   1778 	/*
   1779 	 * Write back each (modified) inode.
   1780 	 */
   1781 	vfs_vnode_iterator_init(mp, &marker);
   1782 
   1783 	ctx.waitfor = waitfor;
   1784 	ctx.is_suspending = is_suspending;
   1785 	while ((vp = vfs_vnode_iterator_next(marker, ffs_sync_selector, &ctx)))
   1786 	{
   1787 		error = vn_lock(vp, LK_EXCLUSIVE);
   1788 		if (error) {
   1789 			vrele(vp);
   1790 			continue;
   1791 		}
   1792 		if (waitfor == MNT_LAZY) {
   1793 			error = UFS_WAPBL_BEGIN(vp->v_mount);
   1794 			if (!error) {
   1795 				error = ffs_update(vp, NULL, NULL,
   1796 				    UPDATE_CLOSE);
   1797 				UFS_WAPBL_END(vp->v_mount);
   1798 			}
   1799 		} else {
   1800 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
   1801 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
   1802 		}
   1803 		if (error)
   1804 			allerror = error;
   1805 		vput(vp);
   1806 	}
   1807 	vfs_vnode_iterator_destroy(marker);
   1808 
   1809 	/*
   1810 	 * Force stale file system control information to be flushed.
   1811 	 */
   1812 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1813 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1814 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1815 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1816 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
   1817 		    0, 0)) != 0)
   1818 			allerror = error;
   1819 		VOP_UNLOCK(ump->um_devvp);
   1820 	}
   1821 #if defined(QUOTA) || defined(QUOTA2)
   1822 	qsync(mp);
   1823 #endif
   1824 	/*
   1825 	 * Write back modified superblock.
   1826 	 */
   1827 	if (fs->fs_fmod != 0) {
   1828 		fs->fs_fmod = 0;
   1829 		fs->fs_time = time_second;
   1830 		error = UFS_WAPBL_BEGIN(mp);
   1831 		if (error)
   1832 			allerror = error;
   1833 		else {
   1834 			if ((error = ffs_cgupdate(ump, waitfor)))
   1835 				allerror = error;
   1836 			UFS_WAPBL_END(mp);
   1837 		}
   1838 	}
   1839 
   1840 #ifdef WAPBL
   1841 	if (mp->mnt_wapbl) {
   1842 		error = wapbl_flush(mp->mnt_wapbl, 0);
   1843 		if (error)
   1844 			allerror = error;
   1845 	}
   1846 #endif
   1847 
   1848 	fstrans_done(mp);
   1849 	return (allerror);
   1850 }
   1851 
   1852 /*
   1853  * Read an inode from disk and initialize this vnode / inode pair.
   1854  * Caller assures no other thread will try to load this inode.
   1855  */
   1856 int
   1857 ffs_loadvnode(struct mount *mp, struct vnode *vp,
   1858     const void *key, size_t key_len, const void **new_key)
   1859 {
   1860 	ino_t ino;
   1861 	struct fs *fs;
   1862 	struct inode *ip;
   1863 	struct ufsmount *ump;
   1864 	struct buf *bp;
   1865 	dev_t dev;
   1866 	int error;
   1867 
   1868 	KASSERT(key_len == sizeof(ino));
   1869 	memcpy(&ino, key, key_len);
   1870 	ump = VFSTOUFS(mp);
   1871 	dev = ump->um_dev;
   1872 	fs = ump->um_fs;
   1873 
   1874 	/* Read in the disk contents for the inode. */
   1875 	error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)),
   1876 		      (int)fs->fs_bsize, NOCRED, 0, &bp);
   1877 	if (error)
   1878 		return error;
   1879 
   1880 	/* Allocate and initialize inode. */
   1881 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
   1882 	memset(ip, 0, sizeof(struct inode));
   1883 	vp->v_tag = VT_UFS;
   1884 	vp->v_op = ffs_vnodeop_p;
   1885 	vp->v_vflag |= VV_LOCKSWORK;
   1886 	vp->v_data = ip;
   1887 	ip->i_vnode = vp;
   1888 	ip->i_ump = ump;
   1889 	ip->i_fs = fs;
   1890 	ip->i_dev = dev;
   1891 	ip->i_number = ino;
   1892 #if defined(QUOTA) || defined(QUOTA2)
   1893 	ufsquota_init(ip);
   1894 #endif
   1895 
   1896 	/* Initialize genfs node. */
   1897 	genfs_node_init(vp, &ffs_genfsops);
   1898 
   1899 	if (ip->i_ump->um_fstype == UFS1)
   1900 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
   1901 		    PR_WAITOK);
   1902 	else
   1903 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
   1904 		    PR_WAITOK);
   1905 	ffs_load_inode(bp, ip, fs, ino);
   1906 	brelse(bp, 0);
   1907 
   1908 	/* Initialize the vnode from the inode. */
   1909 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1910 
   1911 	/* Finish inode initialization.  */
   1912 	ip->i_devvp = ump->um_devvp;
   1913 	vref(ip->i_devvp);
   1914 
   1915 	/*
   1916 	 * Ensure that uid and gid are correct. This is a temporary
   1917 	 * fix until fsck has been changed to do the update.
   1918 	 */
   1919 
   1920 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1921 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1922 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1923 	}							/* XXX */
   1924 	uvm_vnp_setsize(vp, ip->i_size);
   1925 	*new_key = &ip->i_number;
   1926 	return 0;
   1927 }
   1928 
   1929 /*
   1930  * File handle to vnode
   1931  *
   1932  * Have to be really careful about stale file handles:
   1933  * - check that the inode number is valid
   1934  * - call ffs_vget() to get the locked inode
   1935  * - check for an unallocated inode (i_mode == 0)
   1936  * - check that the given client host has export rights and return
   1937  *   those rights via. exflagsp and credanonp
   1938  */
   1939 int
   1940 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1941 {
   1942 	struct ufid ufh;
   1943 	struct fs *fs;
   1944 
   1945 	if (fhp->fid_len != sizeof(struct ufid))
   1946 		return EINVAL;
   1947 
   1948 	memcpy(&ufh, fhp, sizeof(ufh));
   1949 	fs = VFSTOUFS(mp)->um_fs;
   1950 	if (ufh.ufid_ino < UFS_ROOTINO ||
   1951 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1952 		return (ESTALE);
   1953 	return (ufs_fhtovp(mp, &ufh, vpp));
   1954 }
   1955 
   1956 /*
   1957  * Vnode pointer to File handle
   1958  */
   1959 /* ARGSUSED */
   1960 int
   1961 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1962 {
   1963 	struct inode *ip;
   1964 	struct ufid ufh;
   1965 
   1966 	if (*fh_size < sizeof(struct ufid)) {
   1967 		*fh_size = sizeof(struct ufid);
   1968 		return E2BIG;
   1969 	}
   1970 	ip = VTOI(vp);
   1971 	*fh_size = sizeof(struct ufid);
   1972 	memset(&ufh, 0, sizeof(ufh));
   1973 	ufh.ufid_len = sizeof(struct ufid);
   1974 	ufh.ufid_ino = ip->i_number;
   1975 	ufh.ufid_gen = ip->i_gen;
   1976 	memcpy(fhp, &ufh, sizeof(ufh));
   1977 	return (0);
   1978 }
   1979 
   1980 void
   1981 ffs_init(void)
   1982 {
   1983 	if (ffs_initcount++ > 0)
   1984 		return;
   1985 
   1986 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
   1987 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
   1988 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
   1989 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
   1990 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
   1991 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
   1992 	ufs_init();
   1993 }
   1994 
   1995 void
   1996 ffs_reinit(void)
   1997 {
   1998 
   1999 	ufs_reinit();
   2000 }
   2001 
   2002 void
   2003 ffs_done(void)
   2004 {
   2005 	if (--ffs_initcount > 0)
   2006 		return;
   2007 
   2008 	ufs_done();
   2009 	pool_cache_destroy(ffs_dinode2_cache);
   2010 	pool_cache_destroy(ffs_dinode1_cache);
   2011 	pool_cache_destroy(ffs_inode_cache);
   2012 }
   2013 
   2014 /*
   2015  * Write a superblock and associated information back to disk.
   2016  */
   2017 int
   2018 ffs_sbupdate(struct ufsmount *mp, int waitfor)
   2019 {
   2020 	struct fs *fs = mp->um_fs;
   2021 	struct buf *bp;
   2022 	int error = 0;
   2023 	u_int32_t saveflag;
   2024 
   2025 	error = ffs_getblk(mp->um_devvp,
   2026 	    fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
   2027 	    fs->fs_sbsize, false, &bp);
   2028 	if (error)
   2029 		return error;
   2030 	saveflag = fs->fs_flags & FS_INTERNAL;
   2031 	fs->fs_flags &= ~FS_INTERNAL;
   2032 
   2033 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   2034 
   2035 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   2036 #ifdef FFS_EI
   2037 	if (mp->um_flags & UFS_NEEDSWAP)
   2038 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   2039 #endif
   2040 	fs->fs_flags |= saveflag;
   2041 
   2042 	if (waitfor == MNT_WAIT)
   2043 		error = bwrite(bp);
   2044 	else
   2045 		bawrite(bp);
   2046 	return (error);
   2047 }
   2048 
   2049 int
   2050 ffs_cgupdate(struct ufsmount *mp, int waitfor)
   2051 {
   2052 	struct fs *fs = mp->um_fs;
   2053 	struct buf *bp;
   2054 	int blks;
   2055 	void *space;
   2056 	int i, size, error = 0, allerror = 0;
   2057 
   2058 	allerror = ffs_sbupdate(mp, waitfor);
   2059 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   2060 	space = fs->fs_csp;
   2061 	for (i = 0; i < blks; i += fs->fs_frag) {
   2062 		size = fs->fs_bsize;
   2063 		if (i + fs->fs_frag > blks)
   2064 			size = (blks - i) * fs->fs_fsize;
   2065 		error = ffs_getblk(mp->um_devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i),
   2066 		    FFS_NOBLK, size, false, &bp);
   2067 		if (error)
   2068 			break;
   2069 #ifdef FFS_EI
   2070 		if (mp->um_flags & UFS_NEEDSWAP)
   2071 			ffs_csum_swap((struct csum*)space,
   2072 			    (struct csum*)bp->b_data, size);
   2073 		else
   2074 #endif
   2075 			memcpy(bp->b_data, space, (u_int)size);
   2076 		space = (char *)space + size;
   2077 		if (waitfor == MNT_WAIT)
   2078 			error = bwrite(bp);
   2079 		else
   2080 			bawrite(bp);
   2081 	}
   2082 	if (!allerror && error)
   2083 		allerror = error;
   2084 	return (allerror);
   2085 }
   2086 
   2087 int
   2088 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2089     int attrnamespace, const char *attrname)
   2090 {
   2091 #ifdef UFS_EXTATTR
   2092 	/*
   2093 	 * File-backed extended attributes are only supported on UFS1.
   2094 	 * UFS2 has native extended attributes.
   2095 	 */
   2096 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   2097 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
   2098 #endif
   2099 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
   2100 }
   2101 
   2102 int
   2103 ffs_suspendctl(struct mount *mp, int cmd)
   2104 {
   2105 	int error;
   2106 	struct lwp *l = curlwp;
   2107 
   2108 	switch (cmd) {
   2109 	case SUSPEND_SUSPEND:
   2110 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   2111 			return error;
   2112 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
   2113 		if (error == 0)
   2114 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   2115 #ifdef WAPBL
   2116 		if (error == 0 && mp->mnt_wapbl)
   2117 			error = wapbl_flush(mp->mnt_wapbl, 1);
   2118 #endif
   2119 		if (error != 0) {
   2120 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   2121 			return error;
   2122 		}
   2123 		return 0;
   2124 
   2125 	case SUSPEND_RESUME:
   2126 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   2127 
   2128 	default:
   2129 		return EINVAL;
   2130 	}
   2131 }
   2132 
   2133 /*
   2134  * Synch vnode for a mounted file system.
   2135  */
   2136 static int
   2137 ffs_vfs_fsync(vnode_t *vp, int flags)
   2138 {
   2139 	int error, i, pflags;
   2140 #ifdef WAPBL
   2141 	struct mount *mp;
   2142 #endif
   2143 
   2144 	KASSERT(vp->v_type == VBLK);
   2145 	KASSERT(spec_node_getmountedfs(vp) != NULL);
   2146 
   2147 	/*
   2148 	 * Flush all dirty data associated with the vnode.
   2149 	 */
   2150 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
   2151 	if ((flags & FSYNC_WAIT) != 0)
   2152 		pflags |= PGO_SYNCIO;
   2153 	mutex_enter(vp->v_interlock);
   2154 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
   2155 	if (error)
   2156 		return error;
   2157 
   2158 #ifdef WAPBL
   2159 	mp = spec_node_getmountedfs(vp);
   2160 	if (mp && mp->mnt_wapbl) {
   2161 		/*
   2162 		 * Don't bother writing out metadata if the syncer is
   2163 		 * making the request.  We will let the sync vnode
   2164 		 * write it out in a single burst through a call to
   2165 		 * VFS_SYNC().
   2166 		 */
   2167 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
   2168 			return 0;
   2169 
   2170 		/*
   2171 		 * Don't flush the log if the vnode being flushed
   2172 		 * contains no dirty buffers that could be in the log.
   2173 		 */
   2174 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2175 			error = wapbl_flush(mp->mnt_wapbl, 0);
   2176 			if (error)
   2177 				return error;
   2178 		}
   2179 
   2180 		if ((flags & FSYNC_WAIT) != 0) {
   2181 			mutex_enter(vp->v_interlock);
   2182 			while (vp->v_numoutput)
   2183 				cv_wait(&vp->v_cv, vp->v_interlock);
   2184 			mutex_exit(vp->v_interlock);
   2185 		}
   2186 
   2187 		return 0;
   2188 	}
   2189 #endif /* WAPBL */
   2190 
   2191 	error = vflushbuf(vp, flags);
   2192 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
   2193 		i = 1;
   2194 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
   2195 		    kauth_cred_get());
   2196 	}
   2197 
   2198 	return error;
   2199 }
   2200