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