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