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