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