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