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