Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.239.2.5
      1 /*	$NetBSD: ffs_vfsops.c,v 1.239.2.5 2014/04/25 15:43:51 sborrill 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.239.2.5 2014/04/25 15:43:51 sborrill 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 int	ffs_vfs_fsync(vnode_t *, int);
    115 
    116 static struct sysctllog *ffs_sysctl_log;
    117 
    118 /* how many times ffs_init() was called */
    119 int ffs_initcount = 0;
    120 
    121 extern kmutex_t ufs_hashlock;
    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 };
    177 
    178 static int
    179 ffs_modcmd(modcmd_t cmd, void *arg)
    180 {
    181 	int error;
    182 
    183 #if 0
    184 	extern int doasyncfree;
    185 #endif
    186 	extern int ffs_log_changeopt;
    187 
    188 	switch (cmd) {
    189 	case MODULE_CMD_INIT:
    190 		error = vfs_attach(&ffs_vfsops);
    191 		if (error != 0)
    192 			break;
    193 
    194 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    195 			       CTLFLAG_PERMANENT,
    196 			       CTLTYPE_NODE, "vfs", NULL,
    197 			       NULL, 0, NULL, 0,
    198 			       CTL_VFS, CTL_EOL);
    199 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    200 			       CTLFLAG_PERMANENT,
    201 			       CTLTYPE_NODE, "ffs",
    202 			       SYSCTL_DESCR("Berkeley Fast File System"),
    203 			       NULL, 0, NULL, 0,
    204 			       CTL_VFS, 1, CTL_EOL);
    205 
    206 		/*
    207 		 * @@@ should we even bother with these first three?
    208 		 */
    209 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    210 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    211 			       CTLTYPE_INT, "doclusterread", NULL,
    212 			       sysctl_notavail, 0, NULL, 0,
    213 			       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
    214 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    215 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    216 			       CTLTYPE_INT, "doclusterwrite", NULL,
    217 			       sysctl_notavail, 0, NULL, 0,
    218 			       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
    219 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    220 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    221 			       CTLTYPE_INT, "doreallocblks", NULL,
    222 			       sysctl_notavail, 0, NULL, 0,
    223 			       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
    224 #if 0
    225 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    226 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    227 			       CTLTYPE_INT, "doasyncfree",
    228 			       SYSCTL_DESCR("Release dirty blocks asynchronously"),
    229 			       NULL, 0, &doasyncfree, 0,
    230 			       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
    231 #endif
    232 		sysctl_createv(&ffs_sysctl_log, 0, NULL, NULL,
    233 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    234 			       CTLTYPE_INT, "log_changeopt",
    235 			       SYSCTL_DESCR("Log changes in optimization strategy"),
    236 			       NULL, 0, &ffs_log_changeopt, 0,
    237 			       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
    238 		break;
    239 	case MODULE_CMD_FINI:
    240 		error = vfs_detach(&ffs_vfsops);
    241 		if (error != 0)
    242 			break;
    243 		sysctl_teardown(&ffs_sysctl_log);
    244 		break;
    245 	default:
    246 		error = ENOTTY;
    247 		break;
    248 	}
    249 
    250 	return (error);
    251 }
    252 
    253 pool_cache_t ffs_inode_cache;
    254 pool_cache_t ffs_dinode1_cache;
    255 pool_cache_t ffs_dinode2_cache;
    256 
    257 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
    258 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    259 
    260 /*
    261  * Called by main() when ffs is going to be mounted as root.
    262  */
    263 
    264 int
    265 ffs_mountroot(void)
    266 {
    267 	struct fs *fs;
    268 	struct mount *mp;
    269 	struct lwp *l = curlwp;			/* XXX */
    270 	struct ufsmount *ump;
    271 	int error;
    272 
    273 	if (device_class(root_device) != DV_DISK)
    274 		return (ENODEV);
    275 
    276 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    277 		vrele(rootvp);
    278 		return (error);
    279 	}
    280 
    281 	/*
    282 	 * We always need to be able to mount the root file system.
    283 	 */
    284 	mp->mnt_flag |= MNT_FORCE;
    285 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    286 		vfs_unbusy(mp, false, NULL);
    287 		vfs_destroy(mp);
    288 		return (error);
    289 	}
    290 	mp->mnt_flag &= ~MNT_FORCE;
    291 	mutex_enter(&mountlist_lock);
    292 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    293 	mutex_exit(&mountlist_lock);
    294 	ump = VFSTOUFS(mp);
    295 	fs = ump->um_fs;
    296 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    297 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    298 	(void)ffs_statvfs(mp, &mp->mnt_stat);
    299 	vfs_unbusy(mp, false, NULL);
    300 	setrootfstime((time_t)fs->fs_time);
    301 	return (0);
    302 }
    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 (args == NULL)
    322 		return EINVAL;
    323 	if (*data_len < sizeof *args)
    324 		return EINVAL;
    325 
    326 	if (mp->mnt_flag & MNT_GETARGS) {
    327 		ump = VFSTOUFS(mp);
    328 		if (ump == NULL)
    329 			return EIO;
    330 		args->fspec = NULL;
    331 		*data_len = sizeof *args;
    332 		return 0;
    333 	}
    334 
    335 #if !defined(SOFTDEP)
    336 	mp->mnt_flag &= ~MNT_SOFTDEP;
    337 #endif
    338 
    339 	update = mp->mnt_flag & MNT_UPDATE;
    340 
    341 	/* Check arguments */
    342 	if (args->fspec != NULL) {
    343 		/*
    344 		 * Look up the name and verify that it's sane.
    345 		 */
    346 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
    347 		if ((error = namei(&nd)) != 0)
    348 			return (error);
    349 		devvp = nd.ni_vp;
    350 
    351 		if (!update) {
    352 			/*
    353 			 * Be sure this is a valid block device
    354 			 */
    355 			if (devvp->v_type != VBLK)
    356 				error = ENOTBLK;
    357 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    358 				error = ENXIO;
    359 		} else {
    360 			/*
    361 			 * Be sure we're still naming the same device
    362 			 * used for our initial mount
    363 			 */
    364 			ump = VFSTOUFS(mp);
    365 			if (devvp != ump->um_devvp) {
    366 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    367 					error = EINVAL;
    368 				else {
    369 					vrele(devvp);
    370 					devvp = ump->um_devvp;
    371 					vref(devvp);
    372 				}
    373 			}
    374 		}
    375 	} else {
    376 		if (!update) {
    377 			/* New mounts must have a filename for the device */
    378 			return (EINVAL);
    379 		} else {
    380 			/* Use the extant mount */
    381 			ump = VFSTOUFS(mp);
    382 			devvp = ump->um_devvp;
    383 			vref(devvp);
    384 		}
    385 	}
    386 
    387 	/*
    388 	 * Mark the device and any existing vnodes as involved in
    389 	 * softdep processing.
    390 	 */
    391 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0) {
    392 		devvp->v_uflag |= VU_SOFTDEP;
    393 		mutex_enter(&mntvnode_lock);
    394 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
    395 			if (vp->v_mount != mp || vismarker(vp))
    396 				continue;
    397 			vp->v_uflag |= VU_SOFTDEP;
    398 		}
    399 		mutex_exit(&mntvnode_lock);
    400 	}
    401 
    402 	/*
    403 	 * If mount by non-root, then verify that user has necessary
    404 	 * permissions on the device.
    405 	 */
    406 	if (error == 0 && kauth_authorize_generic(l->l_cred,
    407 	    KAUTH_GENERIC_ISSUSER, NULL) != 0) {
    408 		accessmode = VREAD;
    409 		if (update ?
    410 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    411 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    412 			accessmode |= VWRITE;
    413 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    414 		error = VOP_ACCESS(devvp, accessmode, l->l_cred);
    415 		VOP_UNLOCK(devvp, 0);
    416 	}
    417 
    418 	if (error) {
    419 		vrele(devvp);
    420 		return (error);
    421 	}
    422 
    423 #ifdef WAPBL
    424 	/*
    425 	 * WAPBL can only be enabled on a r/w mount
    426 	 * that does not use softdep.
    427 	 */
    428 	if ((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) {
    429 		mp->mnt_flag &= ~MNT_LOG;
    430 	}
    431 	if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_LOG)) ==
    432 			(MNT_SOFTDEP | MNT_LOG)) {
    433 		printf("%s fs is journalled, ignoring soft update mode\n",
    434 			VFSTOUFS(mp)->um_fs->fs_fsmnt);
    435 		mp->mnt_flag &= ~MNT_SOFTDEP;
    436 	}
    437 #else /* !WAPBL */
    438 	mp->mnt_flag &= ~MNT_LOG;
    439 #endif /* !WAPBL */
    440 
    441 	if (!update) {
    442 		int xflags;
    443 
    444 		if (mp->mnt_flag & MNT_RDONLY)
    445 			xflags = FREAD;
    446 		else
    447 			xflags = FREAD | FWRITE;
    448 		error = VOP_OPEN(devvp, xflags, FSCRED);
    449 		if (error)
    450 			goto fail;
    451 		error = ffs_mountfs(devvp, mp, l);
    452 		if (error) {
    453 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    454 			(void)VOP_CLOSE(devvp, xflags, NOCRED);
    455 			VOP_UNLOCK(devvp, 0);
    456 			goto fail;
    457 		}
    458 
    459 		ump = VFSTOUFS(mp);
    460 		fs = ump->um_fs;
    461 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    462 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    463 			printf("%s fs uses soft updates, "
    464 			    "ignoring async mode\n",
    465 			    fs->fs_fsmnt);
    466 			mp->mnt_flag &= ~MNT_ASYNC;
    467 		}
    468 	} else {
    469 		/*
    470 		 * Update the mount.
    471 		 */
    472 
    473 		/*
    474 		 * The initial mount got a reference on this
    475 		 * device, so drop the one obtained via
    476 		 * namei(), above.
    477 		 */
    478 		vrele(devvp);
    479 
    480 		ump = VFSTOUFS(mp);
    481 		fs = ump->um_fs;
    482 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    483 			/*
    484 			 * Changing from r/w to r/o
    485 			 */
    486 			flags = WRITECLOSE;
    487 			if (mp->mnt_flag & MNT_FORCE)
    488 				flags |= FORCECLOSE;
    489 			if (mp->mnt_flag & MNT_SOFTDEP)
    490 				error = softdep_flushfiles(mp, flags, l);
    491 			else
    492 				error = ffs_flushfiles(mp, flags, l);
    493 			if (fs->fs_pendingblocks != 0 ||
    494 			    fs->fs_pendinginodes != 0) {
    495 				printf("%s: update error: blocks %" PRId64
    496 				       " files %d\n",
    497 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    498 				    fs->fs_pendinginodes);
    499 				fs->fs_pendingblocks = 0;
    500 				fs->fs_pendinginodes = 0;
    501 			}
    502 			if (error == 0)
    503 				error = UFS_WAPBL_BEGIN(mp);
    504 			if (error == 0 &&
    505 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    506 			    fs->fs_clean & FS_WASCLEAN) {
    507 				if (mp->mnt_flag & MNT_SOFTDEP)
    508 					fs->fs_flags &= ~FS_DOSOFTDEP;
    509 				fs->fs_clean = FS_ISCLEAN;
    510 				(void) ffs_sbupdate(ump, MNT_WAIT);
    511 			}
    512 			if (error == 0)
    513 				UFS_WAPBL_END(mp);
    514 			if (error)
    515 				return (error);
    516 		}
    517 
    518 #ifdef WAPBL
    519 		if ((mp->mnt_flag & MNT_LOG) == 0) {
    520 			error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE);
    521 			if (error)
    522 				return error;
    523 		}
    524 #endif /* WAPBL */
    525 
    526 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    527 			/*
    528 			 * Finish change from r/w to r/o
    529 			 */
    530 			fs->fs_ronly = 1;
    531 			fs->fs_fmod = 0;
    532 		}
    533 
    534 		/*
    535 		 * Flush soft dependencies if disabling it via an update
    536 		 * mount. This may leave some items to be processed,
    537 		 * so don't do this yet XXX.
    538 		 */
    539 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    540 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    541 #ifdef notyet
    542 			flags = WRITECLOSE;
    543 			if (mp->mnt_flag & MNT_FORCE)
    544 				flags |= FORCECLOSE;
    545 			error = softdep_flushfiles(mp, flags, l);
    546 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    547 				fs->fs_flags &= ~FS_DOSOFTDEP;
    548 				(void) ffs_sbupdate(ump, MNT_WAIT);
    549 #elif defined(SOFTDEP)
    550 			mp->mnt_flag |= MNT_SOFTDEP;
    551 #endif
    552 		}
    553 
    554 		/*
    555 		 * When upgrading to a softdep mount, we must first flush
    556 		 * all vnodes. (not done yet -- see above)
    557 		 */
    558 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    559 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    560 #ifdef notyet
    561 			flags = WRITECLOSE;
    562 			if (mp->mnt_flag & MNT_FORCE)
    563 				flags |= FORCECLOSE;
    564 			error = ffs_flushfiles(mp, flags, l);
    565 #else
    566 			mp->mnt_flag &= ~MNT_SOFTDEP;
    567 #endif
    568 		}
    569 
    570 		if (mp->mnt_flag & MNT_RELOAD) {
    571 			error = ffs_reload(mp, l->l_cred, l);
    572 			if (error)
    573 				return (error);
    574 		}
    575 
    576 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    577 			/*
    578 			 * Changing from read-only to read/write
    579 			 */
    580 			fs->fs_ronly = 0;
    581 			fs->fs_clean <<= 1;
    582 			fs->fs_fmod = 1;
    583 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    584 				error = softdep_mount(devvp, mp, fs,
    585 				    l->l_cred);
    586 				if (error)
    587 					return (error);
    588 			}
    589 #ifdef WAPBL
    590 			if (fs->fs_flags & FS_DOWAPBL) {
    591 				printf("%s: replaying log to disk\n",
    592 				    fs->fs_fsmnt);
    593 				KDASSERT(mp->mnt_wapbl_replay);
    594 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
    595 							   devvp);
    596 				if (error) {
    597 					return error;
    598 				}
    599 				wapbl_replay_stop(mp->mnt_wapbl_replay);
    600 				fs->fs_clean = FS_WASCLEAN;
    601 			}
    602 #endif /* WAPBL */
    603 			if (fs->fs_snapinum[0] != 0)
    604 				ffs_snapshot_mount(mp);
    605 		}
    606 
    607 #ifdef WAPBL
    608 		error = ffs_wapbl_start(mp);
    609 		if (error)
    610 			return error;
    611 #endif /* WAPBL */
    612 
    613 		if (args->fspec == NULL)
    614 			return EINVAL;
    615 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    616 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    617 			printf("%s fs uses soft updates, ignoring async mode\n",
    618 			    fs->fs_fsmnt);
    619 			mp->mnt_flag &= ~MNT_ASYNC;
    620 		}
    621 	}
    622 
    623 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    624 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    625 	if (error == 0)
    626 		(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    627 		    sizeof(fs->fs_fsmnt));
    628 	if (mp->mnt_flag & MNT_SOFTDEP)
    629 		fs->fs_flags |= FS_DOSOFTDEP;
    630 	else
    631 		fs->fs_flags &= ~FS_DOSOFTDEP;
    632 	if (fs->fs_fmod != 0) {	/* XXX */
    633 		int err;
    634 
    635 		fs->fs_fmod = 0;
    636 		if (fs->fs_clean & FS_WASCLEAN)
    637 			fs->fs_time = time_second;
    638 		else {
    639 			printf("%s: file system not clean (fs_clean=%#x); "
    640 			    "please fsck(8)\n", mp->mnt_stat.f_mntfromname,
    641 			    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 && (mp->mnt_flag & MNT_FORCE) == 0)
   1061 			goto out;
   1062 		if (!error) {
   1063 			if (!ronly) {
   1064 				/* XXX fsmnt may be stale. */
   1065 				printf("%s: replaying log to disk\n",
   1066 				    fs->fs_fsmnt);
   1067 				error = wapbl_replay_write(mp->mnt_wapbl_replay,
   1068 				    devvp);
   1069 				if (error)
   1070 					goto out;
   1071 				wapbl_replay_stop(mp->mnt_wapbl_replay);
   1072 				fs->fs_clean = FS_WASCLEAN;
   1073 			} else {
   1074 				/* XXX fsmnt may be stale */
   1075 				printf("%s: replaying log to memory\n",
   1076 				    fs->fs_fsmnt);
   1077 			}
   1078 
   1079 			/* Force a re-read of the superblock */
   1080 			brelse(bp, BC_INVAL);
   1081 			bp = NULL;
   1082 			free(fs, M_UFSMNT);
   1083 			fs = NULL;
   1084 			goto sbagain;
   1085 		}
   1086 	}
   1087 #else /* !WAPBL */
   1088 	if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) {
   1089 		error = EPERM;
   1090 		goto out;
   1091 	}
   1092 #endif /* !WAPBL */
   1093 
   1094 	ffs_oldfscompat_read(fs, ump, sblockloc);
   1095 	ump->um_maxfilesize = fs->fs_maxfilesize;
   1096 
   1097 	if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) {
   1098 		uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n",
   1099 		    mp->mnt_stat.f_mntonname, fs->fs_flags,
   1100 		    (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting");
   1101 		if ((mp->mnt_flag & MNT_FORCE) == 0) {
   1102 			error = EINVAL;
   1103 			goto out;
   1104 		}
   1105 	}
   1106 
   1107 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1108 		fs->fs_pendingblocks = 0;
   1109 		fs->fs_pendinginodes = 0;
   1110 	}
   1111 
   1112 	ump->um_fstype = fstype;
   1113 	if (fs->fs_sbsize < SBLOCKSIZE)
   1114 		brelse(bp, BC_INVAL);
   1115 	else
   1116 		brelse(bp, 0);
   1117 	bp = NULL;
   1118 
   1119 	/* First check to see if this is tagged as an Apple UFS filesystem
   1120 	 * in the disklabel
   1121 	 */
   1122 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) == 0) &&
   1123 		(dpart.part->p_fstype == FS_APPLEUFS)) {
   1124 		ump->um_flags |= UFS_ISAPPLEUFS;
   1125 	}
   1126 #ifdef APPLE_UFS
   1127 	else {
   1128 		/* Manually look for an apple ufs label, and if a valid one
   1129 		 * is found, then treat it like an Apple UFS filesystem anyway
   1130 		 */
   1131 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
   1132 			APPLEUFS_LABEL_SIZE, cred, 0, &bp);
   1133 		if (error)
   1134 			goto out;
   1135 		error = ffs_appleufs_validate(fs->fs_fsmnt,
   1136 			(struct appleufslabel *)bp->b_data, NULL);
   1137 		if (error == 0) {
   1138 			ump->um_flags |= UFS_ISAPPLEUFS;
   1139 		}
   1140 		brelse(bp, 0);
   1141 		bp = NULL;
   1142 	}
   1143 #else
   1144 	if (ump->um_flags & UFS_ISAPPLEUFS) {
   1145 		error = EINVAL;
   1146 		goto out;
   1147 	}
   1148 #endif
   1149 
   1150 #if 0
   1151 /*
   1152  * XXX This code changes the behaviour of mounting dirty filesystems, to
   1153  * XXX require "mount -f ..." to mount them.  This doesn't match what
   1154  * XXX mount(8) describes and is disabled for now.
   1155  */
   1156 	/*
   1157 	 * If the file system is not clean, don't allow it to be mounted
   1158 	 * unless MNT_FORCE is specified.  (Note: MNT_FORCE is always set
   1159 	 * for the root file system.)
   1160 	 */
   1161 	if (fs->fs_flags & FS_DOWAPBL) {
   1162 		/*
   1163 		 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL
   1164 		 * bit is set, although there's a window in unmount where it
   1165 		 * could be FS_ISCLEAN
   1166 		 */
   1167 		if ((mp->mnt_flag & MNT_FORCE) == 0 &&
   1168 		    (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) {
   1169 			error = EPERM;
   1170 			goto out;
   1171 		}
   1172 	} else
   1173 		if ((fs->fs_clean & FS_ISCLEAN) == 0 &&
   1174 		    (mp->mnt_flag & MNT_FORCE) == 0) {
   1175 			error = EPERM;
   1176 			goto out;
   1177 		}
   1178 #endif
   1179 
   1180 	/*
   1181 	 * verify that we can access the last block in the fs
   1182 	 * if we're mounting read/write.
   1183 	 */
   1184 
   1185 	if (!ronly) {
   1186 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
   1187 		    cred, 0, &bp);
   1188 		if (bp->b_bcount != fs->fs_fsize)
   1189 			error = EINVAL;
   1190 		if (error) {
   1191 			bset = BC_INVAL;
   1192 			goto out;
   1193 		}
   1194 		brelse(bp, BC_INVAL);
   1195 		bp = NULL;
   1196 	}
   1197 
   1198 	fs->fs_ronly = ronly;
   1199 	/* Don't bump fs_clean if we're replaying journal */
   1200 	if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN)))
   1201 		if (ronly == 0) {
   1202 			fs->fs_clean <<= 1;
   1203 			fs->fs_fmod = 1;
   1204 		}
   1205 	size = fs->fs_cssize;
   1206 	blks = howmany(size, fs->fs_fsize);
   1207 	if (fs->fs_contigsumsize > 0)
   1208 		size += fs->fs_ncg * sizeof(int32_t);
   1209 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1210 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
   1211 	fs->fs_csp = space;
   1212 	for (i = 0; i < blks; i += fs->fs_frag) {
   1213 		size = fs->fs_bsize;
   1214 		if (i + fs->fs_frag > blks)
   1215 			size = (blks - i) * fs->fs_fsize;
   1216 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
   1217 			      cred, 0, &bp);
   1218 		if (error) {
   1219 			free(fs->fs_csp, M_UFSMNT);
   1220 			goto out;
   1221 		}
   1222 #ifdef FFS_EI
   1223 		if (needswap)
   1224 			ffs_csum_swap((struct csum *)bp->b_data,
   1225 				(struct csum *)space, size);
   1226 		else
   1227 #endif
   1228 			memcpy(space, bp->b_data, (u_int)size);
   1229 
   1230 		space = (char *)space + size;
   1231 		brelse(bp, 0);
   1232 		bp = NULL;
   1233 	}
   1234 	if (fs->fs_contigsumsize > 0) {
   1235 		fs->fs_maxcluster = lp = space;
   1236 		for (i = 0; i < fs->fs_ncg; i++)
   1237 			*lp++ = fs->fs_contigsumsize;
   1238 		space = lp;
   1239 	}
   1240 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
   1241 	fs->fs_contigdirs = space;
   1242 	space = (char *)space + size;
   1243 	memset(fs->fs_contigdirs, 0, size);
   1244 		/* Compatibility for old filesystems - XXX */
   1245 	if (fs->fs_avgfilesize <= 0)
   1246 		fs->fs_avgfilesize = AVFILESIZ;
   1247 	if (fs->fs_avgfpdir <= 0)
   1248 		fs->fs_avgfpdir = AFPDIR;
   1249 	fs->fs_active = NULL;
   1250 	mp->mnt_data = ump;
   1251 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1252 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
   1253 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1254 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
   1255 	if (UFS_MPISAPPLEUFS(ump)) {
   1256 		/* NeXT used to keep short symlinks in the inode even
   1257 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
   1258 		 * is probably -1, but we still need to be able to identify
   1259 		 * short symlinks.
   1260 		 */
   1261 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
   1262 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
   1263 		mp->mnt_iflag |= IMNT_DTYPE;
   1264 	} else {
   1265 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
   1266 		ump->um_dirblksiz = DIRBLKSIZ;
   1267 		if (ump->um_maxsymlinklen > 0)
   1268 			mp->mnt_iflag |= IMNT_DTYPE;
   1269 		else
   1270 			mp->mnt_iflag &= ~IMNT_DTYPE;
   1271 	}
   1272 	mp->mnt_fs_bshift = fs->fs_bshift;
   1273 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
   1274 	mp->mnt_flag |= MNT_LOCAL;
   1275 	mp->mnt_iflag |= IMNT_MPSAFE;
   1276 #ifdef FFS_EI
   1277 	if (needswap)
   1278 		ump->um_flags |= UFS_NEEDSWAP;
   1279 #endif
   1280 	ump->um_mountp = mp;
   1281 	ump->um_dev = dev;
   1282 	ump->um_devvp = devvp;
   1283 	ump->um_nindir = fs->fs_nindir;
   1284 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
   1285 	ump->um_bptrtodb = fs->fs_fsbtodb;
   1286 	ump->um_seqinc = fs->fs_frag;
   1287 	for (i = 0; i < MAXQUOTAS; i++)
   1288 		ump->um_quotas[i] = NULLVP;
   1289 	devvp->v_specmountpoint = mp;
   1290 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
   1291 		error = softdep_mount(devvp, mp, fs, cred);
   1292 		if (error) {
   1293 			free(fs->fs_csp, M_UFSMNT);
   1294 			goto out;
   1295 		}
   1296 	}
   1297 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1298 		ffs_snapshot_mount(mp);
   1299 
   1300 #ifdef WAPBL
   1301 	if (!ronly) {
   1302 		KDASSERT(fs->fs_ronly == 0);
   1303 		/*
   1304 		 * ffs_wapbl_start() needs mp->mnt_stat initialised if it
   1305 		 * needs to create a new log file in-filesystem.
   1306 		 */
   1307 		ffs_statvfs(mp, &mp->mnt_stat);
   1308 
   1309 		error = ffs_wapbl_start(mp);
   1310 		if (error) {
   1311 			free(fs->fs_csp, M_UFSMNT);
   1312 			goto out;
   1313 		}
   1314 	}
   1315 #endif /* WAPBL */
   1316 #ifdef UFS_EXTATTR
   1317 	/*
   1318 	 * Initialize file-backed extended attributes on UFS1 file
   1319 	 * systems.
   1320 	 */
   1321 	if (ump->um_fstype == UFS1) {
   1322 		ufs_extattr_uepm_init(&ump->um_extattr);
   1323 #ifdef UFS_EXTATTR_AUTOSTART
   1324 		/*
   1325 		 * XXX Just ignore errors.  Not clear that we should
   1326 		 * XXX fail the mount in this case.
   1327 		 */
   1328 		(void) ufs_extattr_autostart(mp, l);
   1329 #endif
   1330 	}
   1331 #endif /* UFS_EXTATTR */
   1332 	return (0);
   1333 out:
   1334 #ifdef WAPBL
   1335 	if (mp->mnt_wapbl_replay) {
   1336 		if (wapbl_replay_isopen(mp->mnt_wapbl_replay))
   1337 			wapbl_replay_stop(mp->mnt_wapbl_replay);
   1338 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1339 		mp->mnt_wapbl_replay = 0;
   1340 	}
   1341 #endif
   1342 
   1343 	fstrans_unmount(mp);
   1344 	if (fs)
   1345 		free(fs, M_UFSMNT);
   1346 	devvp->v_specmountpoint = NULL;
   1347 	if (bp)
   1348 		brelse(bp, bset);
   1349 	if (ump) {
   1350 		if (ump->um_oldfscompat)
   1351 			free(ump->um_oldfscompat, M_UFSMNT);
   1352 		mutex_destroy(&ump->um_lock);
   1353 		free(ump, M_UFSMNT);
   1354 		mp->mnt_data = NULL;
   1355 	}
   1356 	return (error);
   1357 }
   1358 
   1359 /*
   1360  * Sanity checks for loading old filesystem superblocks.
   1361  * See ffs_oldfscompat_write below for unwound actions.
   1362  *
   1363  * XXX - Parts get retired eventually.
   1364  * Unfortunately new bits get added.
   1365  */
   1366 static void
   1367 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1368 {
   1369 	off_t maxfilesize;
   1370 	int32_t *extrasave;
   1371 
   1372 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1373 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1374 		return;
   1375 
   1376 	if (!ump->um_oldfscompat)
   1377 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
   1378 		    M_UFSMNT, M_WAITOK);
   1379 
   1380 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1381 	extrasave = ump->um_oldfscompat;
   1382 	extrasave += 512/sizeof(int32_t);
   1383 	extrasave[0] = fs->fs_old_npsect;
   1384 	extrasave[1] = fs->fs_old_interleave;
   1385 	extrasave[2] = fs->fs_old_trackskew;
   1386 
   1387 	/* These fields will be overwritten by their
   1388 	 * original values in fs_oldfscompat_write, so it is harmless
   1389 	 * to modify them here.
   1390 	 */
   1391 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1392 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1393 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1394 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1395 
   1396 	fs->fs_maxbsize = fs->fs_bsize;
   1397 	fs->fs_time = fs->fs_old_time;
   1398 	fs->fs_size = fs->fs_old_size;
   1399 	fs->fs_dsize = fs->fs_old_dsize;
   1400 	fs->fs_csaddr = fs->fs_old_csaddr;
   1401 	fs->fs_sblockloc = sblockloc;
   1402 
   1403 	fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1404 
   1405 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1406 		fs->fs_old_nrpos = 8;
   1407 		fs->fs_old_npsect = fs->fs_old_nsect;
   1408 		fs->fs_old_interleave = 1;
   1409 		fs->fs_old_trackskew = 0;
   1410 	}
   1411 
   1412 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1413 		fs->fs_maxfilesize = (u_quad_t) 1LL << 39;
   1414 		fs->fs_qbmask = ~fs->fs_bmask;
   1415 		fs->fs_qfmask = ~fs->fs_fmask;
   1416 	}
   1417 
   1418 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1419 	if (fs->fs_maxfilesize > maxfilesize)
   1420 		fs->fs_maxfilesize = maxfilesize;
   1421 
   1422 	/* Compatibility for old filesystems */
   1423 	if (fs->fs_avgfilesize <= 0)
   1424 		fs->fs_avgfilesize = AVFILESIZ;
   1425 	if (fs->fs_avgfpdir <= 0)
   1426 		fs->fs_avgfpdir = AFPDIR;
   1427 
   1428 #if 0
   1429 	if (bigcgs) {
   1430 		fs->fs_save_cgsize = fs->fs_cgsize;
   1431 		fs->fs_cgsize = fs->fs_bsize;
   1432 	}
   1433 #endif
   1434 }
   1435 
   1436 /*
   1437  * Unwinding superblock updates for old filesystems.
   1438  * See ffs_oldfscompat_read above for details.
   1439  *
   1440  * XXX - Parts get retired eventually.
   1441  * Unfortunately new bits get added.
   1442  */
   1443 static void
   1444 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1445 {
   1446 	int32_t *extrasave;
   1447 
   1448 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1449 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1450 		return;
   1451 
   1452 	fs->fs_old_time = fs->fs_time;
   1453 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1454 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1455 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1456 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1457 	fs->fs_old_flags = fs->fs_flags;
   1458 
   1459 #if 0
   1460 	if (bigcgs) {
   1461 		fs->fs_cgsize = fs->fs_save_cgsize;
   1462 	}
   1463 #endif
   1464 
   1465 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1466 	extrasave = ump->um_oldfscompat;
   1467 	extrasave += 512/sizeof(int32_t);
   1468 	fs->fs_old_npsect = extrasave[0];
   1469 	fs->fs_old_interleave = extrasave[1];
   1470 	fs->fs_old_trackskew = extrasave[2];
   1471 
   1472 }
   1473 
   1474 /*
   1475  * unmount system call
   1476  */
   1477 int
   1478 ffs_unmount(struct mount *mp, int mntflags)
   1479 {
   1480 	struct lwp *l = curlwp;
   1481 	struct ufsmount *ump = VFSTOUFS(mp);
   1482 	struct fs *fs = ump->um_fs;
   1483 	int error, flags, penderr;
   1484 #ifdef WAPBL
   1485 	extern int doforce;
   1486 #endif
   1487 
   1488 	penderr = 0;
   1489 	flags = 0;
   1490 	if (mntflags & MNT_FORCE)
   1491 		flags |= FORCECLOSE;
   1492 #ifdef UFS_EXTATTR
   1493 	if (ump->um_fstype == UFS1) {
   1494 		ufs_extattr_stop(mp, l);
   1495 		ufs_extattr_uepm_destroy(&ump->um_extattr);
   1496 	}
   1497 #endif /* UFS_EXTATTR */
   1498 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1499 		if ((error = softdep_flushfiles(mp, flags, l)) != 0)
   1500 			return (error);
   1501 	} else {
   1502 		if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1503 			return (error);
   1504 	}
   1505 	mutex_enter(&ump->um_lock);
   1506 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1507 		printf("%s: unmount pending error: blocks %" PRId64
   1508 		       " files %d\n",
   1509 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1510 		fs->fs_pendingblocks = 0;
   1511 		fs->fs_pendinginodes = 0;
   1512 		penderr = 1;
   1513 	}
   1514 	mutex_exit(&ump->um_lock);
   1515 	error = UFS_WAPBL_BEGIN(mp);
   1516 	if (error == 0)
   1517 		if (fs->fs_ronly == 0 &&
   1518 		    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1519 		    fs->fs_clean & FS_WASCLEAN) {
   1520 			/*
   1521 			 * XXXX don't mark fs clean in the case of softdep
   1522 			 * pending block errors, until they are fixed.
   1523 			 */
   1524 			if (penderr == 0) {
   1525 				if (mp->mnt_flag & MNT_SOFTDEP)
   1526 					fs->fs_flags &= ~FS_DOSOFTDEP;
   1527 				fs->fs_clean = FS_ISCLEAN;
   1528 			}
   1529 			fs->fs_fmod = 0;
   1530 			(void) ffs_sbupdate(ump, MNT_WAIT);
   1531 		}
   1532 	if (error == 0)
   1533 		UFS_WAPBL_END(mp);
   1534 #ifdef WAPBL
   1535 	KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
   1536 	if (mp->mnt_wapbl_replay) {
   1537 		KDASSERT(fs->fs_ronly);
   1538 		wapbl_replay_stop(mp->mnt_wapbl_replay);
   1539 		wapbl_replay_free(mp->mnt_wapbl_replay);
   1540 		mp->mnt_wapbl_replay = 0;
   1541 	}
   1542 	error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
   1543 	if (error) {
   1544 		return error;
   1545 	}
   1546 #endif /* WAPBL */
   1547 	if (ump->um_devvp->v_type != VBAD)
   1548 		ump->um_devvp->v_specmountpoint = NULL;
   1549 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1550 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
   1551 		NOCRED);
   1552 	vput(ump->um_devvp);
   1553 	free(fs->fs_csp, M_UFSMNT);
   1554 	free(fs, M_UFSMNT);
   1555 	if (ump->um_oldfscompat != NULL)
   1556 		free(ump->um_oldfscompat, M_UFSMNT);
   1557 	softdep_unmount(mp);
   1558 	mutex_destroy(&ump->um_lock);
   1559 	ffs_snapshot_fini(ump);
   1560 	free(ump, M_UFSMNT);
   1561 	mp->mnt_data = NULL;
   1562 	mp->mnt_flag &= ~MNT_LOCAL;
   1563 	fstrans_unmount(mp);
   1564 	return (0);
   1565 }
   1566 
   1567 /*
   1568  * Flush out all the files in a filesystem.
   1569  */
   1570 int
   1571 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1572 {
   1573 	extern int doforce;
   1574 	struct ufsmount *ump;
   1575 	int error;
   1576 
   1577 	if (!doforce)
   1578 		flags &= ~FORCECLOSE;
   1579 	ump = VFSTOUFS(mp);
   1580 #ifdef QUOTA
   1581 	if (mp->mnt_flag & MNT_QUOTA) {
   1582 		int i;
   1583 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
   1584 			return (error);
   1585 		for (i = 0; i < MAXQUOTAS; i++) {
   1586 			if (ump->um_quotas[i] == NULLVP)
   1587 				continue;
   1588 			quotaoff(l, mp, i);
   1589 		}
   1590 		/*
   1591 		 * Here we fall through to vflush again to ensure
   1592 		 * that we have gotten rid of all the system vnodes.
   1593 		 */
   1594 	}
   1595 #endif
   1596 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1597 		return (error);
   1598 	ffs_snapshot_unmount(mp);
   1599 	/*
   1600 	 * Flush all the files.
   1601 	 */
   1602 	error = vflush(mp, NULLVP, flags);
   1603 	if (error)
   1604 		return (error);
   1605 	/*
   1606 	 * Flush filesystem metadata.
   1607 	 */
   1608 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1609 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
   1610 	VOP_UNLOCK(ump->um_devvp, 0);
   1611 	if (flags & FORCECLOSE) /* XXXDBJ */
   1612 		error = 0;
   1613 
   1614 #ifdef WAPBL
   1615 	if (error)
   1616 		return error;
   1617 	if (mp->mnt_wapbl) {
   1618 		error = wapbl_flush(mp->mnt_wapbl, 1);
   1619 		if (flags & FORCECLOSE)
   1620 			error = 0;
   1621 	}
   1622 #endif
   1623 
   1624 	return (error);
   1625 }
   1626 
   1627 /*
   1628  * Get file system statistics.
   1629  */
   1630 int
   1631 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
   1632 {
   1633 	struct ufsmount *ump;
   1634 	struct fs *fs;
   1635 
   1636 	ump = VFSTOUFS(mp);
   1637 	fs = ump->um_fs;
   1638 	mutex_enter(&ump->um_lock);
   1639 	sbp->f_bsize = fs->fs_bsize;
   1640 	sbp->f_frsize = fs->fs_fsize;
   1641 	sbp->f_iosize = fs->fs_bsize;
   1642 	sbp->f_blocks = fs->fs_dsize;
   1643 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1644 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1645 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1646 	    fs->fs_minfree) / (u_int64_t) 100;
   1647 	if (sbp->f_bfree > sbp->f_bresvd)
   1648 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1649 	else
   1650 		sbp->f_bavail = 0;
   1651 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1652 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1653 	sbp->f_favail = sbp->f_ffree;
   1654 	sbp->f_fresvd = 0;
   1655 	mutex_exit(&ump->um_lock);
   1656 	copy_statvfs_info(sbp, mp);
   1657 
   1658 	return (0);
   1659 }
   1660 
   1661 /*
   1662  * Go through the disk queues to initiate sandbagged IO;
   1663  * go through the inodes to write those that have been modified;
   1664  * initiate the writing of the super block if it has been modified.
   1665  *
   1666  * Note: we are always called with the filesystem marked `MPBUSY'.
   1667  */
   1668 int
   1669 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1670 {
   1671 	struct lwp *l = curlwp;
   1672 	struct vnode *vp, *mvp, *nvp;
   1673 	struct inode *ip;
   1674 	struct ufsmount *ump = VFSTOUFS(mp);
   1675 	struct fs *fs;
   1676 	int error, count, allerror = 0;
   1677 
   1678 	fs = ump->um_fs;
   1679 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1680 		printf("fs = %s\n", fs->fs_fsmnt);
   1681 		panic("update: rofs mod");
   1682 	}
   1683 
   1684 	/* Allocate a marker vnode. */
   1685 	if ((mvp = vnalloc(mp)) == NULL)
   1686 		return (ENOMEM);
   1687 
   1688 	fstrans_start(mp, FSTRANS_SHARED);
   1689 	/*
   1690 	 * Write back each (modified) inode.
   1691 	 */
   1692 	mutex_enter(&mntvnode_lock);
   1693 loop:
   1694 	/*
   1695 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
   1696 	 * and vclean() can be called indirectly
   1697 	 */
   1698 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
   1699 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
   1700 		/*
   1701 		 * If the vnode that we are about to sync is no longer
   1702 		 * associated with this mount point, start over.
   1703 		 */
   1704 		if (vp->v_mount != mp)
   1705 			goto loop;
   1706 		/*
   1707 		 * Don't interfere with concurrent scans of this FS.
   1708 		 */
   1709 		if (vismarker(vp))
   1710 			continue;
   1711 		mutex_enter(&vp->v_interlock);
   1712 		ip = VTOI(vp);
   1713 
   1714 		/*
   1715 		 * Skip the vnode/inode if inaccessible.
   1716 		 */
   1717 		if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
   1718 		    vp->v_type == VNON) {
   1719 			mutex_exit(&vp->v_interlock);
   1720 			continue;
   1721 		}
   1722 
   1723 		/*
   1724 		 * We deliberately update inode times here.  This will
   1725 		 * prevent a massive queue of updates accumulating, only
   1726 		 * to be handled by a call to unmount.
   1727 		 *
   1728 		 * XXX It would be better to have the syncer trickle these
   1729 		 * out.  Adjustment needed to allow registering vnodes for
   1730 		 * sync when the vnode is clean, but the inode dirty.  Or
   1731 		 * have ufs itself trickle out inode updates.
   1732 		 *
   1733 		 * If doing a lazy sync, we don't care about metadata or
   1734 		 * data updates, because they are handled by each vnode's
   1735 		 * synclist entry.  In this case we are only interested in
   1736 		 * writing back modified inodes.
   1737 		 */
   1738 		if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
   1739 		    IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1740 		    (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1741 		    UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
   1742 			mutex_exit(&vp->v_interlock);
   1743 			continue;
   1744 		}
   1745 		if (vp->v_type == VBLK &&
   1746 		    fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
   1747 			mutex_exit(&vp->v_interlock);
   1748 			continue;
   1749 		}
   1750 		vmark(mvp, vp);
   1751 		mutex_exit(&mntvnode_lock);
   1752 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1753 		if (error) {
   1754 			mutex_enter(&mntvnode_lock);
   1755 			nvp = vunmark(mvp);
   1756 			if (error == ENOENT) {
   1757 				goto loop;
   1758 			}
   1759 			continue;
   1760 		}
   1761 		if (waitfor == MNT_LAZY) {
   1762 			error = UFS_WAPBL_BEGIN(vp->v_mount);
   1763 			if (!error) {
   1764 				error = ffs_update(vp, NULL, NULL,
   1765 				    UPDATE_CLOSE);
   1766 				UFS_WAPBL_END(vp->v_mount);
   1767 			}
   1768 		} else {
   1769 			error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
   1770 			    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
   1771 		}
   1772 		if (error)
   1773 			allerror = error;
   1774 		vput(vp);
   1775 		mutex_enter(&mntvnode_lock);
   1776 		nvp = vunmark(mvp);
   1777 	}
   1778 	mutex_exit(&mntvnode_lock);
   1779 	/*
   1780 	 * Force stale file system control information to be flushed.
   1781 	 */
   1782 	if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
   1783 		if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
   1784 			allerror = error;
   1785 		/* Flushed work items may create new vnodes to clean */
   1786 		if (allerror == 0 && count) {
   1787 			mutex_enter(&mntvnode_lock);
   1788 			goto loop;
   1789 		}
   1790 	}
   1791 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1792 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1793 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1794 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1795 		    (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
   1796 		    0, 0)) != 0)
   1797 			allerror = error;
   1798 		VOP_UNLOCK(ump->um_devvp, 0);
   1799 		if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
   1800 			mutex_enter(&mntvnode_lock);
   1801 			goto loop;
   1802 		}
   1803 	}
   1804 #ifdef QUOTA
   1805 	qsync(mp);
   1806 #endif
   1807 	/*
   1808 	 * Write back modified superblock.
   1809 	 */
   1810 	if (fs->fs_fmod != 0) {
   1811 		fs->fs_fmod = 0;
   1812 		fs->fs_time = time_second;
   1813 		error = UFS_WAPBL_BEGIN(mp);
   1814 		if (error)
   1815 			allerror = error;
   1816 		else {
   1817 			if ((error = ffs_cgupdate(ump, waitfor)))
   1818 				allerror = error;
   1819 			UFS_WAPBL_END(mp);
   1820 		}
   1821 	}
   1822 
   1823 #ifdef WAPBL
   1824 	if (mp->mnt_wapbl) {
   1825 		error = wapbl_flush(mp->mnt_wapbl, 0);
   1826 		if (error)
   1827 			allerror = error;
   1828 	}
   1829 #endif
   1830 
   1831 	fstrans_done(mp);
   1832 	vnfree(mvp);
   1833 	return (allerror);
   1834 }
   1835 
   1836 /*
   1837  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1838  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1839  * return the inode locked.  Detection and handling of mount points must be
   1840  * done by the calling routine.
   1841  */
   1842 int
   1843 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1844 {
   1845 	struct fs *fs;
   1846 	struct inode *ip;
   1847 	struct ufsmount *ump;
   1848 	struct buf *bp;
   1849 	struct vnode *vp;
   1850 	dev_t dev;
   1851 	int error;
   1852 
   1853 	ump = VFSTOUFS(mp);
   1854 	dev = ump->um_dev;
   1855 
   1856  retry:
   1857 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1858 		return (0);
   1859 
   1860 	/* Allocate a new vnode/inode. */
   1861 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1862 		*vpp = NULL;
   1863 		return (error);
   1864 	}
   1865 	ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
   1866 
   1867 	/*
   1868 	 * If someone beat us to it, put back the freshly allocated
   1869 	 * vnode/inode pair and retry.
   1870 	 */
   1871 	mutex_enter(&ufs_hashlock);
   1872 	if (ufs_ihashget(dev, ino, 0) != NULL) {
   1873 		mutex_exit(&ufs_hashlock);
   1874 		ungetnewvnode(vp);
   1875 		pool_cache_put(ffs_inode_cache, ip);
   1876 		goto retry;
   1877 	}
   1878 
   1879 	vp->v_vflag |= VV_LOCKSWORK;
   1880 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0)
   1881 		vp->v_uflag |= VU_SOFTDEP;
   1882 
   1883 	/*
   1884 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1885 	 * XXX create another pool for MFS inodes?
   1886 	 */
   1887 
   1888 	memset(ip, 0, sizeof(struct inode));
   1889 	vp->v_data = ip;
   1890 	ip->i_vnode = vp;
   1891 	ip->i_ump = ump;
   1892 	ip->i_fs = fs = ump->um_fs;
   1893 	ip->i_dev = dev;
   1894 	ip->i_number = ino;
   1895 	LIST_INIT(&ip->i_pcbufhd);
   1896 #ifdef QUOTA
   1897 	ufsquota_init(ip);
   1898 #endif
   1899 
   1900 	/*
   1901 	 * Initialize genfs node, we might proceed to destroy it in
   1902 	 * error branches.
   1903 	 */
   1904 	genfs_node_init(vp, &ffs_genfsops);
   1905 
   1906 	/*
   1907 	 * Put it onto its hash chain and lock it so that other requests for
   1908 	 * this inode will block if they arrive while we are sleeping waiting
   1909 	 * for old data structures to be purged or for the contents of the
   1910 	 * disk portion of this inode to be read.
   1911 	 */
   1912 
   1913 	ufs_ihashins(ip);
   1914 	mutex_exit(&ufs_hashlock);
   1915 
   1916 	/* Read in the disk contents for the inode, copy into the inode. */
   1917 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1918 		      (int)fs->fs_bsize, NOCRED, 0, &bp);
   1919 	if (error) {
   1920 
   1921 		/*
   1922 		 * The inode does not contain anything useful, so it would
   1923 		 * be misleading to leave it on its hash chain. With mode
   1924 		 * still zero, it will be unlinked and returned to the free
   1925 		 * list by vput().
   1926 		 */
   1927 
   1928 		vput(vp);
   1929 		brelse(bp, 0);
   1930 		*vpp = NULL;
   1931 		return (error);
   1932 	}
   1933 	if (ip->i_ump->um_fstype == UFS1)
   1934 		ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
   1935 		    PR_WAITOK);
   1936 	else
   1937 		ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
   1938 		    PR_WAITOK);
   1939 	ffs_load_inode(bp, ip, fs, ino);
   1940 	if (DOINGSOFTDEP(vp))
   1941 		softdep_load_inodeblock(ip);
   1942 	else
   1943 		ip->i_ffs_effnlink = ip->i_nlink;
   1944 	brelse(bp, 0);
   1945 
   1946 	/*
   1947 	 * Initialize the vnode from the inode, check for aliases.
   1948 	 * Note that the underlying vnode may have changed.
   1949 	 */
   1950 
   1951 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1952 
   1953 	/*
   1954 	 * Finish inode initialization now that aliasing has been resolved.
   1955 	 */
   1956 
   1957 	ip->i_devvp = ump->um_devvp;
   1958 	VREF(ip->i_devvp);
   1959 
   1960 	/*
   1961 	 * Ensure that uid and gid are correct. This is a temporary
   1962 	 * fix until fsck has been changed to do the update.
   1963 	 */
   1964 
   1965 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1966 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1967 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1968 	}							/* XXX */
   1969 	uvm_vnp_setsize(vp, ip->i_size);
   1970 	*vpp = vp;
   1971 	return (0);
   1972 }
   1973 
   1974 /*
   1975  * File handle to vnode
   1976  *
   1977  * Have to be really careful about stale file handles:
   1978  * - check that the inode number is valid
   1979  * - call ffs_vget() to get the locked inode
   1980  * - check for an unallocated inode (i_mode == 0)
   1981  * - check that the given client host has export rights and return
   1982  *   those rights via. exflagsp and credanonp
   1983  */
   1984 int
   1985 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1986 {
   1987 	struct ufid ufh;
   1988 	struct fs *fs;
   1989 
   1990 	if (fhp->fid_len != sizeof(struct ufid))
   1991 		return EINVAL;
   1992 
   1993 	memcpy(&ufh, fhp, sizeof(ufh));
   1994 	fs = VFSTOUFS(mp)->um_fs;
   1995 	if (ufh.ufid_ino < ROOTINO ||
   1996 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1997 		return (ESTALE);
   1998 	return (ufs_fhtovp(mp, &ufh, vpp));
   1999 }
   2000 
   2001 /*
   2002  * Vnode pointer to File handle
   2003  */
   2004 /* ARGSUSED */
   2005 int
   2006 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   2007 {
   2008 	struct inode *ip;
   2009 	struct ufid ufh;
   2010 
   2011 	if (*fh_size < sizeof(struct ufid)) {
   2012 		*fh_size = sizeof(struct ufid);
   2013 		return E2BIG;
   2014 	}
   2015 	ip = VTOI(vp);
   2016 	*fh_size = sizeof(struct ufid);
   2017 	memset(&ufh, 0, sizeof(ufh));
   2018 	ufh.ufid_len = sizeof(struct ufid);
   2019 	ufh.ufid_ino = ip->i_number;
   2020 	ufh.ufid_gen = ip->i_gen;
   2021 	memcpy(fhp, &ufh, sizeof(ufh));
   2022 	return (0);
   2023 }
   2024 
   2025 void
   2026 ffs_init(void)
   2027 {
   2028 	if (ffs_initcount++ > 0)
   2029 		return;
   2030 
   2031 	ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
   2032 	    "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
   2033 	ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
   2034 	    "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
   2035 	ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
   2036 	    "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
   2037 	softdep_initialize();
   2038 	ufs_init();
   2039 }
   2040 
   2041 void
   2042 ffs_reinit(void)
   2043 {
   2044 	softdep_reinitialize();
   2045 	ufs_reinit();
   2046 }
   2047 
   2048 void
   2049 ffs_done(void)
   2050 {
   2051 	if (--ffs_initcount > 0)
   2052 		return;
   2053 
   2054 	/* XXX softdep cleanup ? */
   2055 	ufs_done();
   2056 	pool_cache_destroy(ffs_dinode2_cache);
   2057 	pool_cache_destroy(ffs_dinode1_cache);
   2058 	pool_cache_destroy(ffs_inode_cache);
   2059 }
   2060 
   2061 /*
   2062  * Write a superblock and associated information back to disk.
   2063  */
   2064 int
   2065 ffs_sbupdate(struct ufsmount *mp, int waitfor)
   2066 {
   2067 	struct fs *fs = mp->um_fs;
   2068 	struct buf *bp;
   2069 	int error = 0;
   2070 	u_int32_t saveflag;
   2071 
   2072 	error = ffs_getblk(mp->um_devvp,
   2073 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb), FFS_NOBLK,
   2074 	    fs->fs_sbsize, false, &bp);
   2075 	if (error)
   2076 		return error;
   2077 	saveflag = fs->fs_flags & FS_INTERNAL;
   2078 	fs->fs_flags &= ~FS_INTERNAL;
   2079 
   2080 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   2081 
   2082 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   2083 #ifdef FFS_EI
   2084 	if (mp->um_flags & UFS_NEEDSWAP)
   2085 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   2086 #endif
   2087 	fs->fs_flags |= saveflag;
   2088 
   2089 	if (waitfor == MNT_WAIT)
   2090 		error = bwrite(bp);
   2091 	else
   2092 		bawrite(bp);
   2093 	return (error);
   2094 }
   2095 
   2096 int
   2097 ffs_cgupdate(struct ufsmount *mp, int waitfor)
   2098 {
   2099 	struct fs *fs = mp->um_fs;
   2100 	struct buf *bp;
   2101 	int blks;
   2102 	void *space;
   2103 	int i, size, error = 0, allerror = 0;
   2104 
   2105 	allerror = ffs_sbupdate(mp, waitfor);
   2106 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   2107 	space = fs->fs_csp;
   2108 	for (i = 0; i < blks; i += fs->fs_frag) {
   2109 		size = fs->fs_bsize;
   2110 		if (i + fs->fs_frag > blks)
   2111 			size = (blks - i) * fs->fs_fsize;
   2112 		error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   2113 		    FFS_NOBLK, size, false, &bp);
   2114 		if (error)
   2115 			break;
   2116 #ifdef FFS_EI
   2117 		if (mp->um_flags & UFS_NEEDSWAP)
   2118 			ffs_csum_swap((struct csum*)space,
   2119 			    (struct csum*)bp->b_data, size);
   2120 		else
   2121 #endif
   2122 			memcpy(bp->b_data, space, (u_int)size);
   2123 		space = (char *)space + size;
   2124 		if (waitfor == MNT_WAIT)
   2125 			error = bwrite(bp);
   2126 		else
   2127 			bawrite(bp);
   2128 	}
   2129 	if (!allerror && error)
   2130 		allerror = error;
   2131 	return (allerror);
   2132 }
   2133 
   2134 int
   2135 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2136     int attrnamespace, const char *attrname)
   2137 {
   2138 #ifdef UFS_EXTATTR
   2139 	/*
   2140 	 * File-backed extended attributes are only supported on UFS1.
   2141 	 * UFS2 has native extended attributes.
   2142 	 */
   2143 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   2144 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
   2145 #endif
   2146 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
   2147 }
   2148 
   2149 int
   2150 ffs_suspendctl(struct mount *mp, int cmd)
   2151 {
   2152 	int error;
   2153 	struct lwp *l = curlwp;
   2154 
   2155 	switch (cmd) {
   2156 	case SUSPEND_SUSPEND:
   2157 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   2158 			return error;
   2159 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
   2160 		if (error == 0)
   2161 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   2162 #ifdef WAPBL
   2163 		if (error == 0 && mp->mnt_wapbl)
   2164 			error = wapbl_flush(mp->mnt_wapbl, 1);
   2165 #endif
   2166 		if (error != 0) {
   2167 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   2168 			return error;
   2169 		}
   2170 		return 0;
   2171 
   2172 	case SUSPEND_RESUME:
   2173 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   2174 
   2175 	default:
   2176 		return EINVAL;
   2177 	}
   2178 }
   2179 
   2180 /*
   2181  * Synch vnode for a mounted file system.  This is called for foreign
   2182  * vnodes, i.e. non-ffs.
   2183  */
   2184 static int
   2185 ffs_vfs_fsync(vnode_t *vp, int flags)
   2186 {
   2187 	int error, passes, skipmeta, i, pflags;
   2188 	buf_t *bp, *nbp;
   2189 	struct mount *mp;
   2190 
   2191 	KASSERT(vp->v_type == VBLK);
   2192 	KASSERT(vp->v_specmountpoint != NULL);
   2193 
   2194 	mp = vp->v_specmountpoint;
   2195 	if ((mp->mnt_flag & MNT_SOFTDEP) != 0)
   2196 		softdep_fsync_mountdev(vp);
   2197 
   2198 	/*
   2199 	 * Flush all dirty data associated with the vnode.
   2200 	 */
   2201 	pflags = PGO_ALLPAGES | PGO_CLEANIT;
   2202 	if ((flags & FSYNC_WAIT) != 0)
   2203 		pflags |= PGO_SYNCIO;
   2204 	mutex_enter(&vp->v_interlock);
   2205 	error = VOP_PUTPAGES(vp, 0, 0, pflags);
   2206 	if (error)
   2207 		return error;
   2208 
   2209 #ifdef WAPBL
   2210 	if (mp && mp->mnt_wapbl) {
   2211 		/*
   2212 		 * Don't bother writing out metadata if the syncer is
   2213 		 * making the request.  We will let the sync vnode
   2214 		 * write it out in a single burst through a call to
   2215 		 * VFS_SYNC().
   2216 		 */
   2217 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
   2218 			return 0;
   2219 
   2220 		/*
   2221 		 * Don't flush the log if the vnode being flushed
   2222 		 * contains no dirty buffers that could be in the log.
   2223 		 */
   2224 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2225 			error = wapbl_flush(mp->mnt_wapbl, 0);
   2226 			if (error)
   2227 				return error;
   2228 		}
   2229 
   2230 		if ((flags & FSYNC_WAIT) != 0) {
   2231 			mutex_enter(&vp->v_interlock);
   2232 			while (vp->v_numoutput)
   2233 				cv_wait(&vp->v_cv, &vp->v_interlock);
   2234 			mutex_exit(&vp->v_interlock);
   2235 		}
   2236 
   2237 		return 0;
   2238 	}
   2239 #endif /* WAPBL */
   2240 
   2241 	/*
   2242 	 * Write out metadata for non-logging file systems.  This block can
   2243 	 * be simplified once softdep goes.
   2244 	 */
   2245 	passes = NIADDR + 1;
   2246 	skipmeta = 0;
   2247 	if (flags & FSYNC_WAIT)
   2248 		skipmeta = 1;
   2249 
   2250 loop:
   2251 	mutex_enter(&bufcache_lock);
   2252 	LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
   2253 		bp->b_cflags &= ~BC_SCANNED;
   2254 	}
   2255 	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
   2256 		nbp = LIST_NEXT(bp, b_vnbufs);
   2257 		if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
   2258 			continue;
   2259 		if ((bp->b_oflags & BO_DELWRI) == 0)
   2260 			panic("ffs_fsync: not dirty");
   2261 		if (skipmeta && bp->b_lblkno < 0)
   2262 			continue;
   2263 		bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
   2264 		mutex_exit(&bufcache_lock);
   2265 		/*
   2266 		 * On our final pass through, do all I/O synchronously
   2267 		 * so that we can find out if our flush is failing
   2268 		 * because of write errors.
   2269 		 */
   2270 		if (passes > 0 || !(flags & FSYNC_WAIT))
   2271 			(void) bawrite(bp);
   2272 		else if ((error = bwrite(bp)) != 0)
   2273 			return (error);
   2274 		/*
   2275 		 * Since we unlocked during the I/O, we need
   2276 		 * to start from a known point.
   2277 		 */
   2278 		mutex_enter(&bufcache_lock);
   2279 		nbp = LIST_FIRST(&vp->v_dirtyblkhd);
   2280 	}
   2281 	mutex_exit(&bufcache_lock);
   2282 	if (skipmeta) {
   2283 		skipmeta = 0;
   2284 		goto loop;
   2285 	}
   2286 
   2287 	if ((flags & FSYNC_WAIT) != 0) {
   2288 		mutex_enter(&vp->v_interlock);
   2289 		while (vp->v_numoutput) {
   2290 			cv_wait(&vp->v_cv, &vp->v_interlock);
   2291 		}
   2292 		mutex_exit(&vp->v_interlock);
   2293 
   2294 		/*
   2295 		 * Ensure that any filesystem metadata associated
   2296 		 * with the vnode has been written.
   2297 		 */
   2298 		if ((error = softdep_sync_metadata(vp)) != 0)
   2299 			return (error);
   2300 
   2301 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
   2302 			/*
   2303 			* Block devices associated with filesystems may
   2304 			* have new I/O requests posted for them even if
   2305 			* the vnode is locked, so no amount of trying will
   2306 			* get them clean. Thus we give block devices a
   2307 			* good effort, then just give up. For all other file
   2308 			* types, go around and try again until it is clean.
   2309 			*/
   2310 			if (passes > 0) {
   2311 				passes--;
   2312 				goto loop;
   2313 			}
   2314 #ifdef DIAGNOSTIC
   2315 			if (vp->v_type != VBLK)
   2316 				vprint("ffs_fsync: dirty", vp);
   2317 #endif
   2318 		}
   2319 	}
   2320 
   2321 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
   2322 		(void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
   2323 		    kauth_cred_get());
   2324 	}
   2325 
   2326 	return error;
   2327 }
   2328