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