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