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