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