Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.257.2.6
      1 /*	$NetBSD: ffs_vfsops.c,v 1.257.2.6 2010/05/28 09:14:55 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.6 2010/05/28 09:14:55 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, 0);
    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, 0);
    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, 0);
    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 | LK_INTERLOCK)) {
    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 
    833 	dev = devvp->v_rdev;
    834 	cred = l ? l->l_cred : NOCRED;
    835 
    836 	/* Flush out any old buffers remaining from a previous use. */
    837 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    838 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    839 	VOP_UNLOCK(devvp, 0);
    840 	if (error)
    841 		return (error);
    842 
    843 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    844 
    845 	bp = NULL;
    846 	ump = NULL;
    847 	fs = NULL;
    848 	sblockloc = 0;
    849 	fstype = 0;
    850 
    851 	error = fstrans_mount(mp);
    852 	if (error)
    853 		return error;
    854 
    855 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    856 	memset(ump, 0, sizeof *ump);
    857 	mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE);
    858 	error = ffs_snapshot_init(ump);
    859 	if (error)
    860 		goto out;
    861 	ump->um_ops = &ffs_ufsops;
    862 
    863 #ifdef WAPBL
    864  sbagain:
    865 #endif
    866 	/*
    867 	 * Try reading the superblock in each of its possible locations.
    868 	 */
    869 	for (i = 0; ; i++) {
    870 		if (bp != NULL) {
    871 			brelse(bp, BC_NOCACHE);
    872 			bp = NULL;
    873 		}
    874 		if (sblock_try[i] == -1) {
    875 			error = EINVAL;
    876 			fs = NULL;
    877 			goto out;
    878 		}
    879 		error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, cred,
    880 			      0, &bp);
    881 		if (error) {
    882 			fs = NULL;
    883 			goto out;
    884 		}
    885 		fs = (struct fs*)bp->b_data;
    886 		fsblockloc = sblockloc = sblock_try[i];
    887 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    888 			sbsize = fs->fs_sbsize;
    889 			fstype = UFS1;
    890 #ifdef FFS_EI
    891 			needswap = 0;
    892 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    893 			sbsize = bswap32(fs->fs_sbsize);
    894 			fstype = UFS1;
    895 			needswap = 1;
    896 #endif
    897 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    898 			sbsize = fs->fs_sbsize;
    899 			fstype = UFS2;
    900 #ifdef FFS_EI
    901 			needswap = 0;
    902 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    903 			sbsize = bswap32(fs->fs_sbsize);
    904 			fstype = UFS2;
    905 			needswap = 1;
    906 #endif
    907 		} else
    908 			continue;
    909 
    910 
    911 		/* fs->fs_sblockloc isn't defined for old filesystems */
    912 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
    913 			if (sblockloc == SBLOCK_UFS2)
    914 				/*
    915 				 * This is likely to be the first alternate
    916 				 * in a filesystem with 64k blocks.
    917 				 * Don't use it.
    918 				 */
    919 				continue;
    920 			fsblockloc = sblockloc;
    921 		} else {
    922 			fsblockloc = fs->fs_sblockloc;
    923 #ifdef FFS_EI
    924 			if (needswap)
    925 				fsblockloc = bswap64(fsblockloc);
    926 #endif
    927 		}
    928 
    929 		/* Check we haven't found an alternate superblock */
    930 		if (fsblockloc != sblockloc)
    931 			continue;
    932 
    933 		/* Validate size of superblock */
    934 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
    935 			continue;
    936 
    937 		/* Ok seems to be a good superblock */
    938 		break;
    939 	}
    940 
    941 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    942 	memcpy(fs, bp->b_data, sbsize);
    943 	ump->um_fs = fs;
    944 
    945 #ifdef FFS_EI
    946 	if (needswap) {
    947 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    948 		fs->fs_flags |= FS_SWAPPED;
    949 	} else
    950 #endif
    951 		fs->fs_flags &= ~FS_SWAPPED;
    952 
    953 #ifdef WAPBL
    954 	if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) {
    955 		error = ffs_wapbl_replay_start(mp, fs, devvp);
    956 		if (error && (mp->mnt_flag & MNT_FORCE) == 0)
    957 			goto out;
    958 		if (!error) {
    959 			if (!ronly) {
    960 				/* XXX fsmnt may be stale. */
    961 				printf("%s: replaying log to disk\n",
    962 				    fs->fs_fsmnt);
    963 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
    964 				    devvp);
    965 				if (error)
    966 					goto out;
    967 				wapbl_replay_stop(mp->mnt_wapbl_replay);
    968 				fs->fs_clean = FS_WASCLEAN;
    969 			} else {
    970 				/* XXX fsmnt may be stale */
    971 				printf("%s: replaying log to memory\n",
    972 				    fs->fs_fsmnt);
    973 			}
    974 
    975 			/* Force a re-read of the superblock */
    976 			brelse(bp, BC_INVAL);
    977 			bp = NULL;
    978 			free(fs, M_UFSMNT);
    979 			fs = NULL;
    980 			goto sbagain;
    981 		}
    982 	}
    983 #else /* !WAPBL */
    984 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
    985 		error = EPERM;
    986 		goto out;
    987 	}
    988 #endif /* !WAPBL */
    989 
    990 	ffs_oldfscompat_read(fs, ump, sblockloc);
    991 	ump->um_maxfilesize = fs->fs_maxfilesize;
    992 
    993 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
    994 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
    995 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
    996 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
    997 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
    998 			error = EINVAL;
    999 			goto out;
   1000 		}
   1001 	}
   1002 
   1003 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1004 		fs->fs_pendingblocks = 0;
   1005 		fs->fs_pendinginodes = 0;
   1006 	}
   1007 
   1008 	ump->um_fstype = fstype;
   1009 	if (fs->fs_sbsize < SBLOCKSIZE)
   1010 		brelse(bp, BC_INVAL);
   1011 	else
   1012 		brelse(bp, 0);
   1013 	bp = NULL;
   1014 
   1015 	/* First check to see if this is tagged as an Apple UFS filesystem
   1016 	 * in the disklabel
   1017 	 */
   1018 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
   1019 		(dpart.part->p_fstype == FS_APPLEUFS)) {
   1020 		ump->um_flags |= UFS_ISAPPLEUFS;
   1021 	}
   1022 #ifdef APPLE_UFS
   1023 	else {
   1024 		/* Manually look for an apple ufs label, and if a valid one
   1025 		 * is found, then treat it like an Apple UFS filesystem anyway
   1026 		 */
   1027 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / DEV_BSIZE),
   1028 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
   1029 		if (error)
   1030 			goto out;
   1031 		error = ffs_appleufs_validate(fs->fs_fsmnt,
   1032 			(struct appleufslabel *)bp->b_data, NULL);
   1033 		if (error == 0) {
   1034 			ump->um_flags |= UFS_ISAPPLEUFS;
   1035 		}
   1036 		brelse(bp, 0);
   1037 		bp = NULL;
   1038 	}
   1039 #else
   1040 	if (ump->um_flags & UFS_ISAPPLEUFS) {
   1041 		error = EINVAL;
   1042 		goto out;
   1043 	}
   1044 #endif
   1045 
   1046 #if 0
   1047 /*
   1048  * XXX This code changes the behaviour of mounting dirty filesystems, to
   1049  * XXX require "mount -f ..." to mount them.  This doesn't match what
   1050  * XXX mount(8) describes and is disabled for now.
   1051  */
   1052 	/*
   1053 	 * If the file system is not clean, don't allow it to be mounted
   1054 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
   1055 	 * for the root file system.)
   1056 	 */
   1057 	if (fs->fs_flags & FS_DOWAPBL) {
   1058 		/*
   1059 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
   1060 		 * bit is set, although there's a window in unmount where it
   1061 		 * could be FS_ISCLEAN
   1062 		 */
   1063 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
   1064 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
   1065 			error = EPERM;
   1066 			goto out;
   1067 		}
   1068 	} else
   1069 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
   1070 		    (mp->mnt_flag & MNT_FORCE) == 0) {
   1071 			error = EPERM;
   1072 			goto out;
   1073 		}
   1074 #endif
   1075 
   1076 	/*
   1077 	 * verify that we can access the last block in the fs
   1078 	 * if we're mounting read/write.
   1079 	 */
   1080 
   1081 	if (!ronly) {
   1082 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
   1083 		    cred, 0, &bp);
   1084 		if (bp->b_bcount != fs->fs_fsize)
   1085 			error = EINVAL;
   1086 		if (error) {
   1087 			bset = BC_INVAL;
   1088 			goto out;
   1089 		}
   1090 		brelse(bp, BC_INVAL);
   1091 		bp = NULL;
   1092 	}
   1093 
   1094 	fs->fs_ronly = ronly;
   1095 	/* Don't bump fs_clean if we're replaying journal */
   1096 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
   1097 		if (ronly == 0) {
   1098 			fs->fs_clean <<= 1;
   1099 			fs->fs_fmod = 1;
   1100 		}
   1101 	bsize = fs->fs_cssize;
   1102 	blks = howmany(bsize, fs->fs_fsize);
   1103 	if (fs->fs_contigsumsize > 0)
   1104 		bsize += fs->fs_ncg * sizeof(int32_t);
   1105 	bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1106 	space = malloc((u_long)bsize, M_UFSMNT, M_WAITOK);
   1107 	fs->fs_csp = space;
   1108 	for (i = 0; i < blks; i += fs->fs_frag) {
   1109 		bsize = fs->fs_bsize;
   1110 		if (i + fs->fs_frag > blks)
   1111 			bsize = (blks - i) * fs->fs_fsize;
   1112 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), bsize,
   1113 			      cred, 0, &bp);
   1114 		if (error) {
   1115 			free(fs->fs_csp, M_UFSMNT);
   1116 			goto out;
   1117 		}
   1118 #ifdef FFS_EI
   1119 		if (needswap)
   1120 			ffs_csum_swap((struct csum *)bp->b_data,
   1121 				(struct csum *)space, bsize);
   1122 		else
   1123 #endif
   1124 			memcpy(space, bp->b_data, (u_int)bsize);
   1125 
   1126 		space = (char *)space + bsize;
   1127 		brelse(bp, 0);
   1128 		bp = NULL;
   1129 	}
   1130 	if (fs->fs_contigsumsize > 0) {
   1131 		fs->fs_maxcluster = lp = space;
   1132 		for (i = 0; i < fs->fs_ncg; i++)
   1133 			*lp++ = fs->fs_contigsumsize;
   1134 		space = lp;
   1135 	}
   1136 	bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1137 	fs->fs_contigdirs = space;
   1138 	space = (char *)space + bsize;
   1139 	memset(fs->fs_contigdirs, 0, bsize);
   1140 		/* Compatibility for old filesystems - XXX */
   1141 	if (fs->fs_avgfilesize <= 0)
   1142 		fs->fs_avgfilesize = AVFILESIZ;
   1143 	if (fs->fs_avgfpdir <= 0)
   1144 		fs->fs_avgfpdir = AFPDIR;
   1145 	fs->fs_active = NULL;
   1146 	mp->mnt_data = ump;
   1147 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1148 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
   1149 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1150 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
   1151 	if (UFS_MPISAPPLEUFS(ump)) {
   1152 		/* NeXT used to keep short symlinks in the inode even
   1153 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
   1154 		 * is probably -1, but we still need to be able to identify
   1155 		 * short symlinks.
   1156 		 */
   1157 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
   1158 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
   1159 		mp->mnt_iflag |= IMNT_DTYPE;
   1160 	} else {
   1161 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
   1162 		ump->um_dirblksiz = DIRBLKSIZ;
   1163 		if (ump->um_maxsymlinklen > 0)
   1164 			mp->mnt_iflag |= IMNT_DTYPE;
   1165 		else
   1166 			mp->mnt_iflag &= ~IMNT_DTYPE;
   1167 	}
   1168 	mp->mnt_fs_bshift = fs->fs_bshift;
   1169 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
   1170 	mp->mnt_flag |= MNT_LOCAL;
   1171 	mp->mnt_iflag |= IMNT_MPSAFE;
   1172 #ifdef XIP
   1173 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
   1174 		printf("XIP needs read-only mount\n");
   1175 	else {
   1176 		void *physseg = NULL;
   1177 
   1178 		if ((VOP_IOCTL(devvp, DIOCGPHYSADDR, &physseg, FREAD,
   1179 		    cred) == 0) &&
   1180 		    physseg != NULL) {
   1181 			mp->mnt_iflag |= IMNT_XIP;
   1182 			devvp->v_physseg = physseg;
   1183 		} else
   1184 			printf("device doesn't support DIOCGPHYSADDR\n");
   1185 	}
   1186 #endif
   1187 #ifdef FFS_EI
   1188 	if (needswap)
   1189 		ump->um_flags |= UFS_NEEDSWAP;
   1190 #endif
   1191 	ump->um_mountp = mp;
   1192 	ump->um_dev = dev;
   1193 	ump->um_devvp = devvp;
   1194 	ump->um_nindir = fs->fs_nindir;
   1195 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
   1196 	ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT;
   1197 	ump->um_seqinc = fs->fs_frag;
   1198 	for (i = 0; i < MAXQUOTAS; i++)
   1199 		ump->um_quotas[i] = NULLVP;
   1200 	devvp->v_specmountpoint = mp;
   1201 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1202 		ffs_snapshot_mount(mp);
   1203 
   1204 #ifdef WAPBL
   1205 	if (!ronly) {
   1206 		KDASSERT(fs->fs_ronly == 0);
   1207 		/*
   1208 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
   1209 		 * needs to create a new log file in-filesystem.
   1210 		 */
   1211 		ffs_statvfs(mp, &mp->mnt_stat);
   1212 
   1213 		error = ffs_wapbl_start(mp);
   1214 		if (error) {
   1215 			free(fs->fs_csp, M_UFSMNT);
   1216 			goto out;
   1217 		}
   1218 	}
   1219 #endif /* WAPBL */
   1220 #ifdef UFS_EXTATTR
   1221 	/*
   1222 	 * Initialize file-backed extended attributes on UFS1 file
   1223 	 * systems.
   1224 	 */
   1225 	if (ump->um_fstype == UFS1) {
   1226 		ufs_extattr_uepm_init(&ump->um_extattr);
   1227 #ifdef UFS_EXTATTR_AUTOSTART
   1228 		/*
   1229 		 * XXX Just ignore errors.  Not clear that we should
   1230 		 * XXX fail the mount in this case.
   1231 		 */
   1232 		(void) ufs_extattr_autostart(mp, l);
   1233 #endif
   1234 	}
   1235 #endif /* UFS_EXTATTR */
   1236 	return (0);
   1237 out:
   1238 #ifdef WAPBL
   1239 	if (mp->mnt_wapbl_replay) {
   1240 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1241 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1242 		mp->mnt_wapbl_replay = 0;
   1243 	}
   1244 #endif
   1245 
   1246 	fstrans_unmount(mp);
   1247 	if (fs)
   1248 		free(fs, M_UFSMNT);
   1249 	devvp->v_specmountpoint = NULL;
   1250 	if (bp)
   1251 		brelse(bp, bset);
   1252 	if (ump) {
   1253 		if (ump->um_oldfscompat)
   1254 			free(ump->um_oldfscompat, M_UFSMNT);
   1255 		mutex_destroy(&ump->um_lock);
   1256 		free(ump, M_UFSMNT);
   1257 		mp->mnt_data = NULL;
   1258 	}
   1259 	return (error);
   1260 }
   1261 
   1262 /*
   1263  * Sanity checks for loading old filesystem superblocks.
   1264  * See ffs_oldfscompat_write below for unwound actions.
   1265  *
   1266  * XXX - Parts get retired eventually.
   1267  * Unfortunately new bits get added.
   1268  */
   1269 static void
   1270 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1271 {
   1272 	off_t maxfilesize;
   1273 	int32_t *extrasave;
   1274 
   1275 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1276 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1277 		return;
   1278 
   1279 	if (!ump->um_oldfscompat)
   1280 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
   1281 		    M_UFSMNT, M_WAITOK);
   1282 
   1283 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1284 	extrasave = ump->um_oldfscompat;
   1285 	extrasave += 512/sizeof(int32_t);
   1286 	extrasave[0] = fs->fs_old_npsect;
   1287 	extrasave[1] = fs->fs_old_interleave;
   1288 	extrasave[2] = fs->fs_old_trackskew;
   1289 
   1290 	/* These fields will be overwritten by their
   1291 	 * original values in fs_oldfscompat_write, so it is harmless
   1292 	 * to modify them here.
   1293 	 */
   1294 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1295 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1296 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1297 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1298 
   1299 	fs->fs_maxbsize = fs->fs_bsize;
   1300 	fs->fs_time = fs->fs_old_time;
   1301 	fs->fs_size = fs->fs_old_size;
   1302 	fs->fs_dsize = fs->fs_old_dsize;
   1303 	fs->fs_csaddr = fs->fs_old_csaddr;
   1304 	fs->fs_sblockloc = sblockloc;
   1305 
   1306 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1307 
   1308 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1309 		fs->fs_old_nrpos = 8;
   1310 		fs->fs_old_npsect = fs->fs_old_nsect;
   1311 		fs->fs_old_interleave = 1;
   1312 		fs->fs_old_trackskew = 0;
   1313 	}
   1314 
   1315 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1316 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1317 		fs->fs_qbmask = ~fs->fs_bmask;
   1318 		fs->fs_qfmask = ~fs->fs_fmask;
   1319 	}
   1320 
   1321 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1322 	if (fs->fs_maxfilesize > maxfilesize)
   1323 		fs->fs_maxfilesize = maxfilesize;
   1324 
   1325 	/* Compatibility for old filesystems */
   1326 	if (fs->fs_avgfilesize <= 0)
   1327 		fs->fs_avgfilesize = AVFILESIZ;
   1328 	if (fs->fs_avgfpdir <= 0)
   1329 		fs->fs_avgfpdir = AFPDIR;
   1330 
   1331 #if 0
   1332 	if (bigcgs) {
   1333 		fs->fs_save_cgsize = fs->fs_cgsize;
   1334 		fs->fs_cgsize = fs->fs_bsize;
   1335 	}
   1336 #endif
   1337 }
   1338 
   1339 /*
   1340  * Unwinding superblock updates for old filesystems.
   1341  * See ffs_oldfscompat_read above for details.
   1342  *
   1343  * XXX - Parts get retired eventually.
   1344  * Unfortunately new bits get added.
   1345  */
   1346 static void
   1347 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1348 {
   1349 	int32_t *extrasave;
   1350 
   1351 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1352 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1353 		return;
   1354 
   1355 	fs->fs_old_time = fs->fs_time;
   1356 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1357 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1358 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1359 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1360 	fs->fs_old_flags = fs->fs_flags;
   1361 
   1362 #if 0
   1363 	if (bigcgs) {
   1364 		fs->fs_cgsize = fs->fs_save_cgsize;
   1365 	}
   1366 #endif
   1367 
   1368 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1369 	extrasave = ump->um_oldfscompat;
   1370 	extrasave += 512/sizeof(int32_t);
   1371 	fs->fs_old_npsect = extrasave[0];
   1372 	fs->fs_old_interleave = extrasave[1];
   1373 	fs->fs_old_trackskew = extrasave[2];
   1374 
   1375 }
   1376 
   1377 /*
   1378  * unmount vfs operation
   1379  */
   1380 int
   1381 ffs_unmount(struct mount *mp, int mntflags)
   1382 {
   1383 	struct lwp *l = curlwp;
   1384 	struct ufsmount *ump = VFSTOUFS(mp);
   1385 	struct fs *fs = ump->um_fs;
   1386 	int error, flags;
   1387 #ifdef WAPBL
   1388 	extern int doforce;
   1389 #endif
   1390 
   1391 	flags = 0;
   1392 	if (mntflags & MNT_FORCE)
   1393 		flags |= FORCECLOSE;
   1394 	if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1395 		return (error);
   1396 	error = UFS_WAPBL_BEGIN(mp);
   1397 	if (error == 0)
   1398 		if (fs->fs_ronly == 0 &&
   1399 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1400 		    fs->fs_clean & FS_WASCLEAN) {
   1401 			fs->fs_clean = FS_ISCLEAN;
   1402 			fs->fs_fmod = 0;
   1403 			(void) ffs_sbupdate(ump, MNT_WAIT);
   1404 		}
   1405 	if (error == 0)
   1406 		UFS_WAPBL_END(mp);
   1407 #ifdef WAPBL
   1408 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
   1409 	if (mp->mnt_wapbl_replay) {
   1410 		KDASSERT(fs->fs_ronly);
   1411 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1412 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1413 		mp->mnt_wapbl_replay = 0;
   1414 	}
   1415 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
   1416 	if (error) {
   1417 		return error;
   1418 	}
   1419 #endif /* WAPBL */
   1420 #ifdef UFS_EXTATTR
   1421 	if (ump->um_fstype == UFS1) {
   1422 		ufs_extattr_stop(mp, l);
   1423 		ufs_extattr_uepm_destroy(&ump->um_extattr);
   1424 	}
   1425 #endif /* UFS_EXTATTR */
   1426 
   1427 	if (ump->um_devvp->v_type != VBAD)
   1428 		ump->um_devvp->v_specmountpoint = NULL;
   1429 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1430 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
   1431 		NOCRED);
   1432 	vput(ump->um_devvp);
   1433 	free(fs->fs_csp, M_UFSMNT);
   1434 	free(fs, M_UFSMNT);
   1435 	if (ump->um_oldfscompat != NULL)
   1436 		free(ump->um_oldfscompat, M_UFSMNT);
   1437 	mutex_destroy(&ump->um_lock);
   1438 	ffs_snapshot_fini(ump);
   1439 	free(ump, M_UFSMNT);
   1440 	mp->mnt_data = NULL;
   1441 	mp->mnt_flag &= ~MNT_LOCAL;
   1442 	fstrans_unmount(mp);
   1443 	return (0);
   1444 }
   1445 
   1446 /*
   1447  * Flush out all the files in a filesystem.
   1448  */
   1449 int
   1450 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1451 {
   1452 	extern int doforce;
   1453 	struct ufsmount *ump;
   1454 	int error;
   1455 
   1456 	if (!doforce)
   1457 		flags &= ~FORCECLOSE;
   1458 	ump = VFSTOUFS(mp);
   1459 #ifdef QUOTA
   1460 	if (mp->mnt_flag & MNT_QUOTA) {
   1461 		int i;
   1462 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
   1463 			return (error);
   1464 		for (i = 0; i < MAXQUOTAS; i++) {
   1465 			if (ump->um_quotas[i] == NULLVP)
   1466 				continue;
   1467 			quotaoff(l, mp, i);
   1468 		}
   1469 		/*
   1470 		 * Here we fall through to vflush again to ensure
   1471 		 * that we have gotten rid of all the system vnodes.
   1472 		 */
   1473 	}
   1474 #endif
   1475 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1476 		return (error);
   1477 	ffs_snapshot_unmount(mp);
   1478 	/*
   1479 	 * Flush all the files.
   1480 	 */
   1481 	error = vflush(mp, NULLVP, flags);
   1482 	if (error)
   1483 		return (error);
   1484 	/*
   1485 	 * Flush filesystem metadata.
   1486 	 */
   1487 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1488 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
   1489 	VOP_UNLOCK(ump->um_devvp, 0);
   1490 	if (flags & FORCECLOSE) /* XXXDBJ */
   1491 		error = 0;
   1492 
   1493 #ifdef WAPBL
   1494 	if (error)
   1495 		return error;
   1496 	if (mp->mnt_wapbl) {
   1497 		error = wapbl_flush(mp->mnt_wapbl, 1);
   1498 		if (flags & FORCECLOSE)
   1499 			error = 0;
   1500 	}
   1501 #endif
   1502 
   1503 	return (error);
   1504 }
   1505 
   1506 /*
   1507  * Get file system statistics.
   1508  */
   1509 int
   1510 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
   1511 {
   1512 	struct ufsmount *ump;
   1513 	struct fs *fs;
   1514 
   1515 	ump = VFSTOUFS(mp);
   1516 	fs = ump->um_fs;
   1517 	mutex_enter(&ump->um_lock);
   1518 	sbp->f_bsize = fs->fs_bsize;
   1519 	sbp->f_frsize = fs->fs_fsize;
   1520 	sbp->f_iosize = fs->fs_bsize;
   1521 	sbp->f_blocks = fs->fs_dsize;
   1522 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1523 	    fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1524 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1525 	    fs->fs_minfree) / (u_int64_t) 100;
   1526 	if (sbp->f_bfree > sbp->f_bresvd)
   1527 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1528 	else
   1529 		sbp->f_bavail = 0;
   1530 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1531 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1532 	sbp->f_favail = sbp->f_ffree;
   1533 	sbp->f_fresvd = 0;
   1534 	mutex_exit(&ump->um_lock);
   1535 	copy_statvfs_info(sbp, mp);
   1536 
   1537 	return (0);
   1538 }
   1539 
   1540 /*
   1541  * Go through the disk queues to initiate sandbagged IO;
   1542  * go through the inodes to write those that have been modified;
   1543  * initiate the writing of the super block if it has been modified.
   1544  *
   1545  * Note: we are always called with the filesystem marked `MPBUSY'.
   1546  */
   1547 int
   1548 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1549 {
   1550 	struct vnode *vp, *mvp, *nvp;
   1551 	struct inode *ip;
   1552 	struct ufsmount *ump = VFSTOUFS(mp);
   1553 	struct fs *fs;
   1554 	int lk_flags, error, allerror = 0;
   1555 	bool is_suspending;
   1556 
   1557 	fs = ump->um_fs;
   1558 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1559 		printf("fs = %s\n", fs->fs_fsmnt);
   1560 		panic("update: rofs mod");
   1561 	}
   1562 
   1563 	/* Allocate a marker vnode. */
   1564 	if ((mvp = vnalloc(mp)) == NULL)
   1565 		return (ENOMEM);
   1566 
   1567 	fstrans_start(mp, FSTRANS_SHARED);
   1568 	is_suspending = (fstrans_getstate(mp) == FSTRANS_SUSPENDING);
   1569 	/*
   1570 	 * We can't lock vnodes while the file system is suspending because
   1571 	 * threads waiting on fstrans may have locked vnodes.
   1572 	 */
   1573 	if (is_suspending)
   1574 		lk_flags = LK_INTERLOCK;
   1575 	else
   1576 		lk_flags = LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK;
   1577 	/*
   1578 	 * Write back each (modified) inode.
   1579 	 */
   1580 	mutex_enter(&mntvnode_lock);
   1581 loop:
   1582 	/*
   1583 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
   1584 	 * and vclean() can be called indirectly
   1585 	 */
   1586 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
   1587 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
   1588 		/*
   1589 		 * If the vnode that we are about to sync is no longer
   1590 		 * associated with this mount point, start over.
   1591 		 */
   1592 		if (vp->v_mount != mp)
   1593 			goto loop;
   1594 		/*
   1595 		 * Don't interfere with concurrent scans of this FS.
   1596 		 */
   1597 		if (vismarker(vp))
   1598 			continue;
   1599 		mutex_enter(&vp->v_interlock);
   1600 		ip = VTOI(vp);
   1601 
   1602 		/*
   1603 		 * Skip the vnode/inode if inaccessible.
   1604 		 */
   1605 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
   1606 		    vp->v_type == VNON) {
   1607 			mutex_exit(&vp->v_interlock);
   1608 			continue;
   1609 		}
   1610 
   1611 		/*
   1612 		 * We deliberately update inode times here.  This will
   1613 		 * prevent a massive queue of updates accumulating, only
   1614 		 * to be handled by a call to unmount.
   1615 		 *
   1616 		 * XXX It would be better to have the syncer trickle these
   1617 		 * out.  Adjustment needed to allow registering vnodes for
   1618 		 * sync when the vnode is clean, but the inode dirty.  Or
   1619 		 * have ufs itself trickle out inode updates.
   1620 		 *
   1621 		 * If doing a lazy sync, we don't care about metadata or
   1622 		 * data updates, because they are handled by each vnode's
   1623 		 * synclist entry.  In this case we are only interested in
   1624 		 * writing back modified inodes.
   1625 		 */
   1626 		if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
   1627 		    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1628 		    (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1629 		    UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
   1630 			mutex_exit(&vp->v_interlock);
   1631 			continue;
   1632 		}
   1633 		if (vp->v_type == VBLK && is_suspending) {
   1634 			mutex_exit(&vp->v_interlock);
   1635 			continue;
   1636 		}
   1637 		vmark(mvp, vp);
   1638 		mutex_exit(&mntvnode_lock);
   1639 		error = vget(vp, lk_flags);
   1640 		if (error) {
   1641 			mutex_enter(&mntvnode_lock);
   1642 			nvp = vunmark(mvp);
   1643 			if (error == ENOENT) {
   1644 				goto loop;
   1645 			}
   1646 			continue;
   1647 		}
   1648 		if (waitfor == MNT_LAZY) {
   1649 			error = UFS_WAPBL_BEGIN(vp->v_mount);
   1650 			if (!error) {
   1651 				error = ffs_update(vp, NULL, NULL,
   1652 				    UPDATE_CLOSE);
   1653 				UFS_WAPBL_END(vp->v_mount);
   1654 			}
   1655 		} else {
   1656 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
   1657 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
   1658 		}
   1659 		if (error)
   1660 			allerror = error;
   1661 		if (is_suspending)
   1662 			vrele(vp);
   1663 		else
   1664 			vput(vp);
   1665 		mutex_enter(&mntvnode_lock);
   1666 		nvp = vunmark(mvp);
   1667 	}
   1668 	mutex_exit(&mntvnode_lock);
   1669 	/*
   1670 	 * Force stale file system control information to be flushed.
   1671 	 */
   1672 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1673 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1674 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1675 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1676 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
   1677 		    0, 0)) != 0)
   1678 			allerror = error;
   1679 		VOP_UNLOCK(ump->um_devvp, 0);
   1680 		if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
   1681 			mutex_enter(&mntvnode_lock);
   1682 			goto loop;
   1683 		}
   1684 	}
   1685 #ifdef QUOTA
   1686 	qsync(mp);
   1687 #endif
   1688 	/*
   1689 	 * Write back modified superblock.
   1690 	 */
   1691 	if (fs->fs_fmod != 0) {
   1692 		fs->fs_fmod = 0;
   1693 		fs->fs_time = time_second;
   1694 		error = UFS_WAPBL_BEGIN(mp);
   1695 		if (error)
   1696 			allerror = error;
   1697 		else {
   1698 			if ((error = ffs_cgupdate(ump, waitfor)))
   1699 				allerror = error;
   1700 			UFS_WAPBL_END(mp);
   1701 		}
   1702 	}
   1703 
   1704 #ifdef WAPBL
   1705 	if (mp->mnt_wapbl) {
   1706 		error = wapbl_flush(mp->mnt_wapbl, 0);
   1707 		if (error)
   1708 			allerror = error;
   1709 	}
   1710 #endif
   1711 
   1712 	fstrans_done(mp);
   1713 	vnfree(mvp);
   1714 	return (allerror);
   1715 }
   1716 
   1717 /*
   1718  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1719  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1720  * return the inode locked.  Detection and handling of mount points must be
   1721  * done by the calling routine.
   1722  */
   1723 int
   1724 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1725 {
   1726 	struct fs *fs;
   1727 	struct inode *ip;
   1728 	struct ufsmount *ump;
   1729 	struct buf *bp;
   1730 	struct vnode *vp;
   1731 	dev_t dev;
   1732 	int error;
   1733 
   1734 	ump = VFSTOUFS(mp);
   1735 	dev = ump->um_dev;
   1736 
   1737  retry:
   1738 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1739 		return (0);
   1740 
   1741 	/* Allocate a new vnode/inode. */
   1742 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1743 		*vpp = NULL;
   1744 		return (error);
   1745 	}
   1746 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
   1747 
   1748 	/*
   1749 	 * If someone beat us to it, put back the freshly allocated
   1750 	 * vnode/inode pair and retry.
   1751 	 */
   1752 	mutex_enter(&ufs_hashlock);
   1753 	if (ufs_ihashget(dev, ino, 0) != NULL) {
   1754 		mutex_exit(&ufs_hashlock);
   1755 		ungetnewvnode(vp);
   1756 		pool_cache_put(ffs_inode_cache, ip);
   1757 		goto retry;
   1758 	}
   1759 
   1760 	vp->v_vflag |= VV_LOCKSWORK;
   1761 
   1762 #ifdef XIP
   1763 	if ((vp->v_mount->mnt_iflag & IMNT_XIP) != 0) {
   1764 		vp->v_vflag |= VV_XIP;
   1765 	}
   1766 #endif
   1767 
   1768 	/*
   1769 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1770 	 * XXX create another pool for MFS inodes?
   1771 	 */
   1772 
   1773 	memset(ip, 0, sizeof(struct inode));
   1774 	vp->v_data = ip;
   1775 	ip->i_vnode = vp;
   1776 	ip->i_ump = ump;
   1777 	ip->i_fs = fs = ump->um_fs;
   1778 	ip->i_dev = dev;
   1779 	ip->i_number = ino;
   1780 #ifdef QUOTA
   1781 	ufsquota_init(ip);
   1782 #endif
   1783 
   1784 	/*
   1785 	 * Initialize genfs node, we might proceed to destroy it in
   1786 	 * error branches.
   1787 	 */
   1788 	genfs_node_init(vp, &ffs_genfsops);
   1789 
   1790 	/*
   1791 	 * Put it onto its hash chain and lock it so that other requests for
   1792 	 * this inode will block if they arrive while we are sleeping waiting
   1793 	 * for old data structures to be purged or for the contents of the
   1794 	 * disk portion of this inode to be read.
   1795 	 */
   1796 
   1797 	ufs_ihashins(ip);
   1798 	mutex_exit(&ufs_hashlock);
   1799 
   1800 	/* Read in the disk contents for the inode, copy into the inode. */
   1801 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1802 		      (int)fs->fs_bsize, NOCRED, 0, &bp);
   1803 	if (error) {
   1804 
   1805 		/*
   1806 		 * The inode does not contain anything useful, so it would
   1807 		 * be misleading to leave it on its hash chain. With mode
   1808 		 * still zero, it will be unlinked and returned to the free
   1809 		 * list by vput().
   1810 		 */
   1811 
   1812 		vput(vp);
   1813 		brelse(bp, 0);
   1814 		*vpp = NULL;
   1815 		return (error);
   1816 	}
   1817 	if (ip->i_ump->um_fstype == UFS1)
   1818 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
   1819 		    PR_WAITOK);
   1820 	else
   1821 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
   1822 		    PR_WAITOK);
   1823 	ffs_load_inode(bp, ip, fs, ino);
   1824 	brelse(bp, 0);
   1825 
   1826 	/*
   1827 	 * Initialize the vnode from the inode, check for aliases.
   1828 	 * Note that the underlying vnode may have changed.
   1829 	 */
   1830 
   1831 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1832 
   1833 	/*
   1834 	 * Finish inode initialization now that aliasing has been resolved.
   1835 	 */
   1836 
   1837 	ip->i_devvp = ump->um_devvp;
   1838 	vref(ip->i_devvp);
   1839 
   1840 	/*
   1841 	 * Ensure that uid and gid are correct. This is a temporary
   1842 	 * fix until fsck has been changed to do the update.
   1843 	 */
   1844 
   1845 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1846 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1847 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1848 	}							/* XXX */
   1849 	uvm_vnp_setsize(vp, ip->i_size);
   1850 	*vpp = vp;
   1851 	return (0);
   1852 }
   1853 
   1854 /*
   1855  * File handle to vnode
   1856  *
   1857  * Have to be really careful about stale file handles:
   1858  * - check that the inode number is valid
   1859  * - call ffs_vget() to get the locked inode
   1860  * - check for an unallocated inode (i_mode == 0)
   1861  * - check that the given client host has export rights and return
   1862  *   those rights via. exflagsp and credanonp
   1863  */
   1864 int
   1865 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1866 {
   1867 	struct ufid ufh;
   1868 	struct fs *fs;
   1869 
   1870 	if (fhp->fid_len != sizeof(struct ufid))
   1871 		return EINVAL;
   1872 
   1873 	memcpy(&ufh, fhp, sizeof(ufh));
   1874 	fs = VFSTOUFS(mp)->um_fs;
   1875 	if (ufh.ufid_ino < ROOTINO ||
   1876 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1877 		return (ESTALE);
   1878 	return (ufs_fhtovp(mp, &ufh, vpp));
   1879 }
   1880 
   1881 /*
   1882  * Vnode pointer to File handle
   1883  */
   1884 /* ARGSUSED */
   1885 int
   1886 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1887 {
   1888 	struct inode *ip;
   1889 	struct ufid ufh;
   1890 
   1891 	if (*fh_size < sizeof(struct ufid)) {
   1892 		*fh_size = sizeof(struct ufid);
   1893 		return E2BIG;
   1894 	}
   1895 	ip = VTOI(vp);
   1896 	*fh_size = sizeof(struct ufid);
   1897 	memset(&ufh, 0, sizeof(ufh));
   1898 	ufh.ufid_len = sizeof(struct ufid);
   1899 	ufh.ufid_ino = ip->i_number;
   1900 	ufh.ufid_gen = ip->i_gen;
   1901 	memcpy(fhp, &ufh, sizeof(ufh));
   1902 	return (0);
   1903 }
   1904 
   1905 void
   1906 ffs_init(void)
   1907 {
   1908 	if (ffs_initcount++ > 0)
   1909 		return;
   1910 
   1911 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
   1912 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
   1913 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
   1914 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
   1915 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
   1916 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
   1917 	ufs_init();
   1918 }
   1919 
   1920 void
   1921 ffs_reinit(void)
   1922 {
   1923 
   1924 	ufs_reinit();
   1925 }
   1926 
   1927 void
   1928 ffs_done(void)
   1929 {
   1930 	if (--ffs_initcount > 0)
   1931 		return;
   1932 
   1933 	ufs_done();
   1934 	pool_cache_destroy(ffs_dinode2_cache);
   1935 	pool_cache_destroy(ffs_dinode1_cache);
   1936 	pool_cache_destroy(ffs_inode_cache);
   1937 }
   1938 
   1939 /*
   1940  * Write a superblock and associated information back to disk.
   1941  */
   1942 int
   1943 ffs_sbupdate(struct ufsmount *mp, int waitfor)
   1944 {
   1945 	struct fs *fs = mp->um_fs;
   1946 	struct buf *bp;
   1947 	int error = 0;
   1948 	u_int32_t saveflag;
   1949 
   1950 	error = ffs_getblk(mp->um_devvp,
   1951 	    fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK,
   1952 	    fs->fs_sbsize, false, &bp);
   1953 	if (error)
   1954 		return error;
   1955 	saveflag = fs->fs_flags & FS_INTERNAL;
   1956 	fs->fs_flags &= ~FS_INTERNAL;
   1957 
   1958 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1959 
   1960 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1961 #ifdef FFS_EI
   1962 	if (mp->um_flags & UFS_NEEDSWAP)
   1963 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   1964 #endif
   1965 	fs->fs_flags |= saveflag;
   1966 
   1967 	if (waitfor == MNT_WAIT)
   1968 		error = bwrite(bp);
   1969 	else
   1970 		bawrite(bp);
   1971 	return (error);
   1972 }
   1973 
   1974 int
   1975 ffs_cgupdate(struct ufsmount *mp, int waitfor)
   1976 {
   1977 	struct fs *fs = mp->um_fs;
   1978 	struct buf *bp;
   1979 	int blks;
   1980 	void *space;
   1981 	int i, size, error = 0, allerror = 0;
   1982 
   1983 	allerror = ffs_sbupdate(mp, waitfor);
   1984 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1985 	space = fs->fs_csp;
   1986 	for (i = 0; i < blks; i += fs->fs_frag) {
   1987 		size = fs->fs_bsize;
   1988 		if (i + fs->fs_frag > blks)
   1989 			size = (blks - i) * fs->fs_fsize;
   1990 		error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1991 		    FFS_NOBLK, size, false, &bp);
   1992 		if (error)
   1993 			break;
   1994 #ifdef FFS_EI
   1995 		if (mp->um_flags & UFS_NEEDSWAP)
   1996 			ffs_csum_swap((struct csum*)space,
   1997 			    (struct csum*)bp->b_data, size);
   1998 		else
   1999 #endif
   2000 			memcpy(bp->b_data, space, (u_int)size);
   2001 		space = (char *)space + size;
   2002 		if (waitfor == MNT_WAIT)
   2003 			error = bwrite(bp);
   2004 		else
   2005 			bawrite(bp);
   2006 	}
   2007 	if (!allerror && error)
   2008 		allerror = error;
   2009 	return (allerror);
   2010 }
   2011 
   2012 int
   2013 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2014     int attrnamespace, const char *attrname)
   2015 {
   2016 #ifdef UFS_EXTATTR
   2017 	/*
   2018 	 * File-backed extended attributes are only supported on UFS1.
   2019 	 * UFS2 has native extended attributes.
   2020 	 */
   2021 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   2022 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
   2023 #endif
   2024 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
   2025 }
   2026 
   2027 int
   2028 ffs_suspendctl(struct mount *mp, int cmd)
   2029 {
   2030 	int error;
   2031 	struct lwp *l = curlwp;
   2032 
   2033 	switch (cmd) {
   2034 	case SUSPEND_SUSPEND:
   2035 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   2036 			return error;
   2037 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
   2038 		if (error == 0)
   2039 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   2040 #ifdef WAPBL
   2041 		if (error == 0 && mp->mnt_wapbl)
   2042 			error = wapbl_flush(mp->mnt_wapbl, 1);
   2043 #endif
   2044 		if (error != 0) {
   2045 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   2046 			return error;
   2047 		}
   2048 		return 0;
   2049 
   2050 	case SUSPEND_RESUME:
   2051 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   2052 
   2053 	default:
   2054 		return EINVAL;
   2055 	}
   2056 }
   2057 
   2058 /*
   2059  * Synch vnode for a mounted file system.  This is called for foreign
   2060  * vnodes, i.e. non-ffs.
   2061  */
   2062 static int
   2063 ffs_vfs_fsync(vnode_t *vp, int flags)
   2064 {
   2065 	int error, passes, skipmeta, i, pflags;
   2066 	buf_t *bp, *nbp;
   2067 #ifdef WAPBL
   2068 	struct mount *mp;
   2069 #endif
   2070 
   2071 	KASSERT(vp->v_type == VBLK);
   2072 	KASSERT(vp->v_specmountpoint != NULL);
   2073 
   2074 	/*
   2075 	 * Flush all dirty data associated with the vnode.
   2076 	 */
   2077 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
   2078 	if ((flags & FSYNC_WAIT) != 0)
   2079 		pflags |= PGO_SYNCIO;
   2080 	mutex_enter(&vp->v_interlock);
   2081 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
   2082 	if (error)
   2083 		return error;
   2084 
   2085 #ifdef WAPBL
   2086 	mp = vp->v_specmountpoint;
   2087 	if (mp && mp->mnt_wapbl) {
   2088 		/*
   2089 		 * Don't bother writing out metadata if the syncer is
   2090 		 * making the request.  We will let the sync vnode
   2091 		 * write it out in a single burst through a call to
   2092 		 * VFS_SYNC().
   2093 		 */
   2094 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
   2095 			return 0;
   2096 
   2097 		/*
   2098 		 * Don't flush the log if the vnode being flushed
   2099 		 * contains no dirty buffers that could be in the log.
   2100 		 */
   2101 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2102 			error = wapbl_flush(mp->mnt_wapbl, 0);
   2103 			if (error)
   2104 				return error;
   2105 		}
   2106 
   2107 		if ((flags & FSYNC_WAIT) != 0) {
   2108 			mutex_enter(&vp->v_interlock);
   2109 			while (vp->v_numoutput)
   2110 				cv_wait(&vp->v_cv, &vp->v_interlock);
   2111 			mutex_exit(&vp->v_interlock);
   2112 		}
   2113 
   2114 		return 0;
   2115 	}
   2116 #endif /* WAPBL */
   2117 
   2118 	/*
   2119 	 * Write out metadata for non-logging file systems. XXX This block
   2120 	 * should be simplified now that softdep is gone.
   2121 	 */
   2122 	passes = NIADDR + 1;
   2123 	skipmeta = 0;
   2124 	if (flags & FSYNC_WAIT)
   2125 		skipmeta = 1;
   2126 
   2127 loop:
   2128 	mutex_enter(&bufcache_lock);
   2129 	LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
   2130 		bp->b_cflags &= ~BC_SCANNED;
   2131 	}
   2132 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
   2133 		nbp = LIST_NEXT(bp, b_vnbufs);
   2134 		if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
   2135 			continue;
   2136 		if ((bp->b_oflags & BO_DELWRI) == 0)
   2137 			panic("ffs_fsync: not dirty");
   2138 		if (skipmeta && bp->b_lblkno < 0)
   2139 			continue;
   2140 		bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
   2141 		mutex_exit(&bufcache_lock);
   2142 		/*
   2143 		 * On our final pass through, do all I/O synchronously
   2144 		 * so that we can find out if our flush is failing
   2145 		 * because of write errors.
   2146 		 */
   2147 		if (passes > 0 || !(flags & FSYNC_WAIT))
   2148 			(void) bawrite(bp);
   2149 		else if ((error = bwrite(bp)) != 0)
   2150 			return (error);
   2151 		/*
   2152 		 * Since we unlocked during the I/O, we need
   2153 		 * to start from a known point.
   2154 		 */
   2155 		mutex_enter(&bufcache_lock);
   2156 		nbp = LIST_FIRST(&vp->v_dirtyblkhd);
   2157 	}
   2158 	mutex_exit(&bufcache_lock);
   2159 	if (skipmeta) {
   2160 		skipmeta = 0;
   2161 		goto loop;
   2162 	}
   2163 
   2164 	if ((flags & FSYNC_WAIT) != 0) {
   2165 		mutex_enter(&vp->v_interlock);
   2166 		while (vp->v_numoutput) {
   2167 			cv_wait(&vp->v_cv, &vp->v_interlock);
   2168 		}
   2169 		mutex_exit(&vp->v_interlock);
   2170 
   2171 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2172 			/*
   2173 			* Block devices associated with filesystems may
   2174 			* have new I/O requests posted for them even if
   2175 			* the vnode is locked, so no amount of trying will
   2176 			* get them clean. Thus we give block devices a
   2177 			* good effort, then just give up. For all other file
   2178 			* types, go around and try again until it is clean.
   2179 			*/
   2180 			if (passes > 0) {
   2181 				passes--;
   2182 				goto loop;
   2183 			}
   2184 #ifdef DIAGNOSTIC
   2185 			if (vp->v_type != VBLK)
   2186 				vprint("ffs_fsync: dirty", vp);
   2187 #endif
   2188 		}
   2189 	}
   2190 
   2191 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
   2192 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
   2193 		    kauth_cred_get());
   2194 	}
   2195 
   2196 	return error;
   2197 }
   2198