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