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