Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.195
      1 /*	$NetBSD: ffs_vfsops.c,v 1.195 2007/02/15 15:40:54 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.195 2007/02/15 15:40:54 ad Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_ffs.h"
     39 #include "opt_quota.h"
     40 #include "opt_softdep.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/namei.h>
     46 #include <sys/proc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/vnode.h>
     49 #include <sys/socket.h>
     50 #include <sys/mount.h>
     51 #include <sys/buf.h>
     52 #include <sys/device.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/file.h>
     55 #include <sys/disklabel.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/malloc.h>
     59 #include <sys/pool.h>
     60 #include <sys/lock.h>
     61 #include <sys/sysctl.h>
     62 #include <sys/conf.h>
     63 #include <sys/kauth.h>
     64 #include <sys/fstrans.h>
     65 
     66 #include <miscfs/specfs/specdev.h>
     67 
     68 #include <ufs/ufs/quota.h>
     69 #include <ufs/ufs/ufsmount.h>
     70 #include <ufs/ufs/inode.h>
     71 #include <ufs/ufs/dir.h>
     72 #include <ufs/ufs/ufs_extern.h>
     73 #include <ufs/ufs/ufs_bswap.h>
     74 
     75 #include <ufs/ffs/fs.h>
     76 #include <ufs/ffs/ffs_extern.h>
     77 
     78 /* how many times ffs_init() was called */
     79 int ffs_initcount = 0;
     80 
     81 extern kmutex_t ufs_hashlock;
     82 
     83 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
     84 extern const struct vnodeopv_desc ffs_specop_opv_desc;
     85 extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
     86 
     87 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
     88 	&ffs_vnodeop_opv_desc,
     89 	&ffs_specop_opv_desc,
     90 	&ffs_fifoop_opv_desc,
     91 	NULL,
     92 };
     93 
     94 struct vfsops ffs_vfsops = {
     95 	MOUNT_FFS,
     96 	ffs_mount,
     97 	ufs_start,
     98 	ffs_unmount,
     99 	ufs_root,
    100 	ufs_quotactl,
    101 	ffs_statvfs,
    102 	ffs_sync,
    103 	ffs_vget,
    104 	ffs_fhtovp,
    105 	ffs_vptofh,
    106 	ffs_init,
    107 	ffs_reinit,
    108 	ffs_done,
    109 	ffs_mountroot,
    110 	ffs_snapshot,
    111 	ffs_extattrctl,
    112 	ffs_suspendctl,
    113 	ffs_vnodeopv_descs,
    114 	0,
    115 	{ NULL, NULL },
    116 };
    117 VFS_ATTACH(ffs_vfsops);
    118 
    119 static const struct genfs_ops ffs_genfsops = {
    120 	.gop_size = ffs_gop_size,
    121 	.gop_alloc = ufs_gop_alloc,
    122 	.gop_write = genfs_gop_write,
    123 	.gop_markupdate = ufs_gop_markupdate,
    124 };
    125 
    126 static const struct ufs_ops ffs_ufsops = {
    127 	.uo_itimes = ffs_itimes,
    128 	.uo_update = ffs_update,
    129 	.uo_truncate = ffs_truncate,
    130 	.uo_valloc = ffs_valloc,
    131 	.uo_vfree = ffs_vfree,
    132 	.uo_balloc = ffs_balloc,
    133 };
    134 
    135 POOL_INIT(ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
    136     &pool_allocator_nointr);
    137 POOL_INIT(ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "dino1pl",
    138     &pool_allocator_nointr);
    139 POOL_INIT(ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0, "dino2pl",
    140     &pool_allocator_nointr);
    141 
    142 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
    143 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
    144 
    145 /*
    146  * Called by main() when ffs is going to be mounted as root.
    147  */
    148 
    149 int
    150 ffs_mountroot(void)
    151 {
    152 	struct fs *fs;
    153 	struct mount *mp;
    154 	struct lwp *l = curlwp;			/* XXX */
    155 	struct ufsmount *ump;
    156 	int error;
    157 
    158 	if (device_class(root_device) != DV_DISK)
    159 		return (ENODEV);
    160 
    161 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    162 		vrele(rootvp);
    163 		return (error);
    164 	}
    165 	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
    166 		mp->mnt_op->vfs_refcount--;
    167 		vfs_unbusy(mp);
    168 		free(mp, M_MOUNT);
    169 		return (error);
    170 	}
    171 	simple_lock(&mountlist_slock);
    172 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    173 	simple_unlock(&mountlist_slock);
    174 	ump = VFSTOUFS(mp);
    175 	fs = ump->um_fs;
    176 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    177 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    178 	(void)ffs_statvfs(mp, &mp->mnt_stat, l);
    179 	vfs_unbusy(mp);
    180 	setrootfstime((time_t)fs->fs_time);
    181 	return (0);
    182 }
    183 
    184 /*
    185  * VFS Operations.
    186  *
    187  * mount system call
    188  */
    189 int
    190 ffs_mount(struct mount *mp, const char *path, void *data,
    191     struct nameidata *ndp, struct lwp *l)
    192 {
    193 	struct vnode *devvp = NULL;
    194 	struct ufs_args args;
    195 	struct ufsmount *ump = NULL;
    196 	struct fs *fs;
    197 	int error, flags, update;
    198 	mode_t accessmode;
    199 
    200 	if (mp->mnt_flag & MNT_GETARGS) {
    201 		ump = VFSTOUFS(mp);
    202 		if (ump == NULL)
    203 			return EIO;
    204 		args.fspec = NULL;
    205 		return copyout(&args, data, sizeof(args));
    206 	}
    207 	error = copyin(data, &args, sizeof (struct ufs_args));
    208 	if (error)
    209 		return (error);
    210 
    211 #if !defined(SOFTDEP)
    212 	mp->mnt_flag &= ~MNT_SOFTDEP;
    213 #endif
    214 
    215 	update = mp->mnt_flag & MNT_UPDATE;
    216 
    217 	/* Check arguments */
    218 	if (args.fspec != NULL) {
    219 		/*
    220 		 * Look up the name and verify that it's sane.
    221 		 */
    222 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
    223 		if ((error = namei(ndp)) != 0)
    224 			return (error);
    225 		devvp = ndp->ni_vp;
    226 
    227 		if (!update) {
    228 			/*
    229 			 * Be sure this is a valid block device
    230 			 */
    231 			if (devvp->v_type != VBLK)
    232 				error = ENOTBLK;
    233 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    234 				error = ENXIO;
    235 		} else {
    236 			/*
    237 			 * Be sure we're still naming the same device
    238 			 * used for our initial mount
    239 			 */
    240 			ump = VFSTOUFS(mp);
    241 			if (devvp != ump->um_devvp) {
    242 				if (devvp->v_rdev != ump->um_devvp->v_rdev)
    243 					error = EINVAL;
    244 				else {
    245 					vrele(devvp);
    246 					devvp = ump->um_devvp;
    247 					vref(devvp);
    248 				}
    249 			}
    250 		}
    251 	} else {
    252 		if (!update) {
    253 			/* New mounts must have a filename for the device */
    254 			return (EINVAL);
    255 		} else {
    256 			/* Use the extant mount */
    257 			ump = VFSTOUFS(mp);
    258 			devvp = ump->um_devvp;
    259 			vref(devvp);
    260 		}
    261 	}
    262 
    263 	/*
    264 	 * If mount by non-root, then verify that user has necessary
    265 	 * permissions on the device.
    266 	 */
    267 	if (error == 0 && kauth_authorize_generic(l->l_cred,
    268 	    KAUTH_GENERIC_ISSUSER, NULL) != 0) {
    269 		accessmode = VREAD;
    270 		if (update ?
    271 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    272 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    273 			accessmode |= VWRITE;
    274 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    275 		error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
    276 		VOP_UNLOCK(devvp, 0);
    277 	}
    278 
    279 	if (error) {
    280 		vrele(devvp);
    281 		return (error);
    282 	}
    283 
    284 	if (!update) {
    285 		int xflags;
    286 
    287 		/*
    288 		 * Disallow multiple mounts of the same device.
    289 		 * Disallow mounting of a device that is currently in use
    290 		 * (except for root, which might share swap device for
    291 		 * miniroot).
    292 		 */
    293 		error = vfs_mountedon(devvp);
    294 		if (error)
    295 			goto fail;
    296 		if (vcount(devvp) > 1 && devvp != rootvp) {
    297 			error = EBUSY;
    298 			goto fail;
    299 		}
    300 		if (mp->mnt_flag & MNT_RDONLY)
    301 			xflags = FREAD;
    302 		else
    303 			xflags = FREAD|FWRITE;
    304 		error = VOP_OPEN(devvp, xflags, FSCRED, l);
    305 		if (error)
    306 			goto fail;
    307 		error = ffs_mountfs(devvp, mp, l);
    308 		if (error) {
    309 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    310 			(void)VOP_CLOSE(devvp, xflags, NOCRED, l);
    311 			VOP_UNLOCK(devvp, 0);
    312 			goto fail;
    313 		}
    314 
    315 		ump = VFSTOUFS(mp);
    316 		fs = ump->um_fs;
    317 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    318 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    319 			printf("%s fs uses soft updates, "
    320 			    "ignoring async mode\n",
    321 			    fs->fs_fsmnt);
    322 			mp->mnt_flag &= ~MNT_ASYNC;
    323 		}
    324 	} else {
    325 		/*
    326 		 * Update the mount.
    327 		 */
    328 
    329 		/*
    330 		 * The initial mount got a reference on this
    331 		 * device, so drop the one obtained via
    332 		 * namei(), above.
    333 		 */
    334 		vrele(devvp);
    335 
    336 		ump = VFSTOUFS(mp);
    337 		fs = ump->um_fs;
    338 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    339 			/*
    340 			 * Changing from r/w to r/o
    341 			 */
    342 			vn_start_write(NULL, &mp, V_WAIT);
    343 			flags = WRITECLOSE;
    344 			if (mp->mnt_flag & MNT_FORCE)
    345 				flags |= FORCECLOSE;
    346 			if (mp->mnt_flag & MNT_SOFTDEP)
    347 				error = softdep_flushfiles(mp, flags, l);
    348 			else
    349 				error = ffs_flushfiles(mp, flags, l);
    350 			if (fs->fs_pendingblocks != 0 ||
    351 			    fs->fs_pendinginodes != 0) {
    352 				printf("%s: update error: blocks %" PRId64
    353 				       " files %d\n",
    354 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    355 				    fs->fs_pendinginodes);
    356 				fs->fs_pendingblocks = 0;
    357 				fs->fs_pendinginodes = 0;
    358 			}
    359 			if (error == 0 &&
    360 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    361 			    fs->fs_clean & FS_WASCLEAN) {
    362 				if (mp->mnt_flag & MNT_SOFTDEP)
    363 					fs->fs_flags &= ~FS_DOSOFTDEP;
    364 				fs->fs_clean = FS_ISCLEAN;
    365 				(void) ffs_sbupdate(ump, MNT_WAIT);
    366 			}
    367 			vn_finished_write(mp, 0);
    368 			if (error)
    369 				return (error);
    370 			fs->fs_ronly = 1;
    371 			fs->fs_fmod = 0;
    372 		}
    373 
    374 		/*
    375 		 * Flush soft dependencies if disabling it via an update
    376 		 * mount. This may leave some items to be processed,
    377 		 * so don't do this yet XXX.
    378 		 */
    379 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    380 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    381 #ifdef notyet
    382 			vn_start_write(NULL, &mp, V_WAIT);
    383 			flags = WRITECLOSE;
    384 			if (mp->mnt_flag & MNT_FORCE)
    385 				flags |= FORCECLOSE;
    386 			error = softdep_flushfiles(mp, flags, l);
    387 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    388 				fs->fs_flags &= ~FS_DOSOFTDEP;
    389 				(void) ffs_sbupdate(ump, MNT_WAIT);
    390 			vn_finished_write(mp);
    391 #elif defined(SOFTDEP)
    392 			mp->mnt_flag |= MNT_SOFTDEP;
    393 #endif
    394 		}
    395 
    396 		/*
    397 		 * When upgrading to a softdep mount, we must first flush
    398 		 * all vnodes. (not done yet -- see above)
    399 		 */
    400 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    401 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    402 #ifdef notyet
    403 			vn_start_write(NULL, &mp, V_WAIT);
    404 			flags = WRITECLOSE;
    405 			if (mp->mnt_flag & MNT_FORCE)
    406 				flags |= FORCECLOSE;
    407 			error = ffs_flushfiles(mp, flags, l);
    408 			vn_finished_write(mp);
    409 #else
    410 			mp->mnt_flag &= ~MNT_SOFTDEP;
    411 #endif
    412 		}
    413 
    414 		if (mp->mnt_flag & MNT_RELOAD) {
    415 			error = ffs_reload(mp, l->l_cred, l);
    416 			if (error)
    417 				return (error);
    418 		}
    419 
    420 		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    421 			/*
    422 			 * Changing from read-only to read/write
    423 			 */
    424 			fs->fs_ronly = 0;
    425 			fs->fs_clean <<= 1;
    426 			fs->fs_fmod = 1;
    427 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    428 				error = softdep_mount(devvp, mp, fs,
    429 				    l->l_cred);
    430 				if (error)
    431 					return (error);
    432 			}
    433 			if (fs->fs_snapinum[0] != 0)
    434 				ffs_snapshot_mount(mp);
    435 		}
    436 		if (args.fspec == NULL)
    437 			return EINVAL;
    438 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    439 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    440 			printf("%s fs uses soft updates, ignoring async mode\n",
    441 			    fs->fs_fsmnt);
    442 			mp->mnt_flag &= ~MNT_ASYNC;
    443 		}
    444 	}
    445 
    446 	error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
    447 	    UIO_USERSPACE, mp, l);
    448 	if (error == 0)
    449 		(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
    450 		    sizeof(fs->fs_fsmnt));
    451 	if (mp->mnt_flag & MNT_SOFTDEP)
    452 		fs->fs_flags |= FS_DOSOFTDEP;
    453 	else
    454 		fs->fs_flags &= ~FS_DOSOFTDEP;
    455 	if (fs->fs_fmod != 0) {	/* XXX */
    456 		fs->fs_fmod = 0;
    457 		if (fs->fs_clean & FS_WASCLEAN)
    458 			fs->fs_time = time_second;
    459 		else {
    460 			printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
    461 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    462 			printf("%s: lost blocks %" PRId64 " files %d\n",
    463 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    464 			    fs->fs_pendinginodes);
    465 		}
    466 		(void) ffs_cgupdate(ump, MNT_WAIT);
    467 	}
    468 	return (error);
    469 
    470 fail:
    471 	vrele(devvp);
    472 	return (error);
    473 }
    474 
    475 /*
    476  * Reload all incore data for a filesystem (used after running fsck on
    477  * the root filesystem and finding things to fix). The filesystem must
    478  * be mounted read-only.
    479  *
    480  * Things to do to update the mount:
    481  *	1) invalidate all cached meta-data.
    482  *	2) re-read superblock from disk.
    483  *	3) re-read summary information from disk.
    484  *	4) invalidate all inactive vnodes.
    485  *	5) invalidate all cached file data.
    486  *	6) re-read inode data for all active vnodes.
    487  */
    488 int
    489 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
    490 {
    491 	struct vnode *vp, *nvp, *devvp;
    492 	struct inode *ip;
    493 	void *space;
    494 	struct buf *bp;
    495 	struct fs *fs, *newfs;
    496 	struct partinfo dpart;
    497 	int i, blks, size, error;
    498 	int32_t *lp;
    499 	struct ufsmount *ump;
    500 	daddr_t sblockloc;
    501 
    502 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    503 		return (EINVAL);
    504 
    505 	ump = VFSTOUFS(mp);
    506 	/*
    507 	 * Step 1: invalidate all cached meta-data.
    508 	 */
    509 	devvp = ump->um_devvp;
    510 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    511 	error = vinvalbuf(devvp, 0, cred, l, 0, 0);
    512 	VOP_UNLOCK(devvp, 0);
    513 	if (error)
    514 		panic("ffs_reload: dirty1");
    515 	/*
    516 	 * Step 2: re-read superblock from disk.
    517 	 */
    518 	fs = ump->um_fs;
    519 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, l) != 0)
    520 		size = DEV_BSIZE;
    521 	else
    522 		size = dpart.disklab->d_secsize;
    523 	/* XXX we don't handle possibility that superblock moved. */
    524 	error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
    525 		      NOCRED, &bp);
    526 	if (error) {
    527 		brelse(bp);
    528 		return (error);
    529 	}
    530 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    531 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    532 #ifdef FFS_EI
    533 	if (ump->um_flags & UFS_NEEDSWAP) {
    534 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    535 		fs->fs_flags |= FS_SWAPPED;
    536 	} else
    537 #endif
    538 		fs->fs_flags &= ~FS_SWAPPED;
    539 	if ((newfs->fs_magic != FS_UFS1_MAGIC &&
    540 	     newfs->fs_magic != FS_UFS2_MAGIC)||
    541 	     newfs->fs_bsize > MAXBSIZE ||
    542 	     newfs->fs_bsize < sizeof(struct fs)) {
    543 		brelse(bp);
    544 		free(newfs, M_UFSMNT);
    545 		return (EIO);		/* XXX needs translation */
    546 	}
    547 	/* Store off old fs_sblockloc for fs_oldfscompat_read. */
    548 	sblockloc = fs->fs_sblockloc;
    549 	/*
    550 	 * Copy pointer fields back into superblock before copying in	XXX
    551 	 * new superblock. These should really be in the ufsmount.	XXX
    552 	 * Note that important parameters (eg fs_ncg) are unchanged.
    553 	 */
    554 	newfs->fs_csp = fs->fs_csp;
    555 	newfs->fs_maxcluster = fs->fs_maxcluster;
    556 	newfs->fs_contigdirs = fs->fs_contigdirs;
    557 	newfs->fs_ronly = fs->fs_ronly;
    558 	newfs->fs_active = fs->fs_active;
    559 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    560 	brelse(bp);
    561 	free(newfs, M_UFSMNT);
    562 
    563 	/* Recheck for apple UFS filesystem */
    564 	ump->um_flags &= ~UFS_ISAPPLEUFS;
    565 	/* First check to see if this is tagged as an Apple UFS filesystem
    566 	 * in the disklabel
    567 	 */
    568 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
    569 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    570 		ump->um_flags |= UFS_ISAPPLEUFS;
    571 	}
    572 #ifdef APPLE_UFS
    573 	else {
    574 		/* Manually look for an apple ufs label, and if a valid one
    575 		 * is found, then treat it like an Apple UFS filesystem anyway
    576 		 */
    577 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    578 			APPLEUFS_LABEL_SIZE, cred, &bp);
    579 		if (error) {
    580 			brelse(bp);
    581 			return (error);
    582 		}
    583 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    584 			(struct appleufslabel *)bp->b_data,NULL);
    585 		if (error == 0)
    586 			ump->um_flags |= UFS_ISAPPLEUFS;
    587 		brelse(bp);
    588 		bp = NULL;
    589 	}
    590 #else
    591 	if (ump->um_flags & UFS_ISAPPLEUFS)
    592 		return (EIO);
    593 #endif
    594 
    595 	if (UFS_MPISAPPLEUFS(ump)) {
    596 		/* see comment about NeXT below */
    597 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    598 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
    599 		mp->mnt_iflag |= IMNT_DTYPE;
    600 	} else {
    601 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
    602 		ump->um_dirblksiz = DIRBLKSIZ;
    603 		if (ump->um_maxsymlinklen > 0)
    604 			mp->mnt_iflag |= IMNT_DTYPE;
    605 		else
    606 			mp->mnt_iflag &= ~IMNT_DTYPE;
    607 	}
    608 	ffs_oldfscompat_read(fs, ump, sblockloc);
    609 	ump->um_maxfilesize = fs->fs_maxfilesize;
    610 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    611 		fs->fs_pendingblocks = 0;
    612 		fs->fs_pendinginodes = 0;
    613 	}
    614 
    615 	ffs_statvfs(mp, &mp->mnt_stat, l);
    616 	/*
    617 	 * Step 3: re-read summary information from disk.
    618 	 */
    619 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    620 	space = fs->fs_csp;
    621 	for (i = 0; i < blks; i += fs->fs_frag) {
    622 		size = fs->fs_bsize;
    623 		if (i + fs->fs_frag > blks)
    624 			size = (blks - i) * fs->fs_fsize;
    625 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    626 			      NOCRED, &bp);
    627 		if (error) {
    628 			brelse(bp);
    629 			return (error);
    630 		}
    631 #ifdef FFS_EI
    632 		if (UFS_FSNEEDSWAP(fs))
    633 			ffs_csum_swap((struct csum *)bp->b_data,
    634 			    (struct csum *)space, size);
    635 		else
    636 #endif
    637 			memcpy(space, bp->b_data, (size_t)size);
    638 		space = (char *)space + size;
    639 		brelse(bp);
    640 	}
    641 	if ((fs->fs_flags & FS_DOSOFTDEP))
    642 		softdep_mount(devvp, mp, fs, cred);
    643 	if (fs->fs_snapinum[0] != 0)
    644 		ffs_snapshot_mount(mp);
    645 	/*
    646 	 * We no longer know anything about clusters per cylinder group.
    647 	 */
    648 	if (fs->fs_contigsumsize > 0) {
    649 		lp = fs->fs_maxcluster;
    650 		for (i = 0; i < fs->fs_ncg; i++)
    651 			*lp++ = fs->fs_contigsumsize;
    652 	}
    653 
    654 loop:
    655 	/*
    656 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    657 	 * and vclean() can be called indirectly
    658 	 */
    659 	simple_lock(&mntvnode_slock);
    660 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    661 		if (vp->v_mount != mp) {
    662 			simple_unlock(&mntvnode_slock);
    663 			goto loop;
    664 		}
    665 		/*
    666 		 * Step 4: invalidate all inactive vnodes.
    667 		 */
    668 		if (vrecycle(vp, &mntvnode_slock, l))
    669 			goto loop;
    670 		/*
    671 		 * Step 5: invalidate all cached file data.
    672 		 */
    673 		simple_lock(&vp->v_interlock);
    674 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    675 		simple_unlock(&mntvnode_slock);
    676 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    677 			goto loop;
    678 		if (vinvalbuf(vp, 0, cred, l, 0, 0))
    679 			panic("ffs_reload: dirty2");
    680 		/*
    681 		 * Step 6: re-read inode data for all active vnodes.
    682 		 */
    683 		ip = VTOI(vp);
    684 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    685 			      (int)fs->fs_bsize, NOCRED, &bp);
    686 		if (error) {
    687 			brelse(bp);
    688 			vput(vp);
    689 			return (error);
    690 		}
    691 		ffs_load_inode(bp, ip, fs, ip->i_number);
    692 		ip->i_ffs_effnlink = ip->i_nlink;
    693 		brelse(bp);
    694 		vput(vp);
    695 		simple_lock(&mntvnode_slock);
    696 	}
    697 	simple_unlock(&mntvnode_slock);
    698 	return (0);
    699 }
    700 
    701 /*
    702  * Possible superblock locations ordered from most to least likely.
    703  */
    704 static const int sblock_try[] = SBLOCKSEARCH;
    705 
    706 /*
    707  * Common code for mount and mountroot
    708  */
    709 int
    710 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    711 {
    712 	struct ufsmount *ump;
    713 	struct buf *bp;
    714 	struct fs *fs;
    715 	dev_t dev;
    716 	struct partinfo dpart;
    717 	void *space;
    718 	daddr_t sblockloc, fsblockloc;
    719 	int blks, fstype;
    720 	int error, i, size, ronly;
    721 #ifdef FFS_EI
    722 	int needswap = 0;		/* keep gcc happy */
    723 #endif
    724 	int32_t *lp;
    725 	kauth_cred_t cred;
    726 	u_int32_t sbsize = 8192;	/* keep gcc happy*/
    727 
    728 	dev = devvp->v_rdev;
    729 	cred = l ? l->l_cred : NOCRED;
    730 
    731 	/* Flush out any old buffers remaining from a previous use. */
    732 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    733 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    734 	VOP_UNLOCK(devvp, 0);
    735 	if (error)
    736 		return (error);
    737 
    738 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    739 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) != 0)
    740 		size = DEV_BSIZE;
    741 	else
    742 		size = dpart.disklab->d_secsize;
    743 
    744 	bp = NULL;
    745 	ump = NULL;
    746 	fs = NULL;
    747 	sblockloc = 0;
    748 	fstype = 0;
    749 
    750 	/*
    751 	 * Try reading the superblock in each of its possible locations.
    752 	 */
    753 	for (i = 0; ; i++) {
    754 		if (bp != NULL) {
    755 			bp->b_flags |= B_NOCACHE;
    756 			brelse(bp);
    757 			bp = NULL;
    758 		}
    759 		if (sblock_try[i] == -1) {
    760 			error = EINVAL;
    761 			fs = NULL;
    762 			goto out;
    763 		}
    764 		error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
    765 			      &bp);
    766 		if (error) {
    767 			fs = NULL;
    768 			goto out;
    769 		}
    770 		fs = (struct fs*)bp->b_data;
    771 		fsblockloc = sblockloc = sblock_try[i];
    772 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    773 			sbsize = fs->fs_sbsize;
    774 			fstype = UFS1;
    775 #ifdef FFS_EI
    776 			needswap = 0;
    777 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    778 			sbsize = bswap32(fs->fs_sbsize);
    779 			fstype = UFS1;
    780 			needswap = 1;
    781 #endif
    782 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    783 			sbsize = fs->fs_sbsize;
    784 			fstype = UFS2;
    785 #ifdef FFS_EI
    786 			needswap = 0;
    787 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    788 			sbsize = bswap32(fs->fs_sbsize);
    789 			fstype = UFS2;
    790 			needswap = 1;
    791 #endif
    792 		} else
    793 			continue;
    794 
    795 
    796 		/* fs->fs_sblockloc isn't defined for old filesystems */
    797 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
    798 			if (sblockloc == SBLOCK_UFS2)
    799 				/*
    800 				 * This is likely to be the first alternate
    801 				 * in a filesystem with 64k blocks.
    802 				 * Don't use it.
    803 				 */
    804 				continue;
    805 			fsblockloc = sblockloc;
    806 		} else {
    807 			fsblockloc = fs->fs_sblockloc;
    808 #ifdef FFS_EI
    809 			if (needswap)
    810 				fsblockloc = bswap64(fsblockloc);
    811 #endif
    812 		}
    813 
    814 		/* Check we haven't found an alternate superblock */
    815 		if (fsblockloc != sblockloc)
    816 			continue;
    817 
    818 		/* Validate size of superblock */
    819 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
    820 			continue;
    821 
    822 		/* Ok seems to be a good superblock */
    823 		break;
    824 	}
    825 
    826 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    827 	memcpy(fs, bp->b_data, sbsize);
    828 
    829 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    830 	memset(ump, 0, sizeof *ump);
    831 	TAILQ_INIT(&ump->um_snapshots);
    832 	ump->um_fs = fs;
    833 	ump->um_ops = &ffs_ufsops;
    834 
    835 #ifdef FFS_EI
    836 	if (needswap) {
    837 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    838 		fs->fs_flags |= FS_SWAPPED;
    839 	} else
    840 #endif
    841 		fs->fs_flags &= ~FS_SWAPPED;
    842 
    843 	ffs_oldfscompat_read(fs, ump, sblockloc);
    844 	ump->um_maxfilesize = fs->fs_maxfilesize;
    845 
    846 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    847 		fs->fs_pendingblocks = 0;
    848 		fs->fs_pendinginodes = 0;
    849 	}
    850 
    851 	ump->um_fstype = fstype;
    852 	if (fs->fs_sbsize < SBLOCKSIZE)
    853 		bp->b_flags |= B_INVAL;
    854 	brelse(bp);
    855 	bp = NULL;
    856 
    857 	/* First check to see if this is tagged as an Apple UFS filesystem
    858 	 * in the disklabel
    859 	 */
    860 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
    861 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    862 		ump->um_flags |= UFS_ISAPPLEUFS;
    863 	}
    864 #ifdef APPLE_UFS
    865 	else {
    866 		/* Manually look for an apple ufs label, and if a valid one
    867 		 * is found, then treat it like an Apple UFS filesystem anyway
    868 		 */
    869 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    870 			APPLEUFS_LABEL_SIZE, cred, &bp);
    871 		if (error)
    872 			goto out;
    873 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    874 			(struct appleufslabel *)bp->b_data,NULL);
    875 		if (error == 0) {
    876 			ump->um_flags |= UFS_ISAPPLEUFS;
    877 		}
    878 		brelse(bp);
    879 		bp = NULL;
    880 	}
    881 #else
    882 	if (ump->um_flags & UFS_ISAPPLEUFS) {
    883 		error = EINVAL;
    884 		goto out;
    885 	}
    886 #endif
    887 
    888 	/*
    889 	 * verify that we can access the last block in the fs
    890 	 * if we're mounting read/write.
    891 	 */
    892 
    893 	if (!ronly) {
    894 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    895 		    cred, &bp);
    896 		if (bp->b_bcount != fs->fs_fsize)
    897 			error = EINVAL;
    898 		bp->b_flags |= B_INVAL;
    899 		if (error)
    900 			goto out;
    901 		brelse(bp);
    902 		bp = NULL;
    903 	}
    904 
    905 	fs->fs_ronly = ronly;
    906 	if (ronly == 0) {
    907 		fs->fs_clean <<= 1;
    908 		fs->fs_fmod = 1;
    909 	}
    910 	size = fs->fs_cssize;
    911 	blks = howmany(size, fs->fs_fsize);
    912 	if (fs->fs_contigsumsize > 0)
    913 		size += fs->fs_ncg * sizeof(int32_t);
    914 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    915 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    916 	fs->fs_csp = space;
    917 	for (i = 0; i < blks; i += fs->fs_frag) {
    918 		size = fs->fs_bsize;
    919 		if (i + fs->fs_frag > blks)
    920 			size = (blks - i) * fs->fs_fsize;
    921 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    922 			      cred, &bp);
    923 		if (error) {
    924 			free(fs->fs_csp, M_UFSMNT);
    925 			goto out;
    926 		}
    927 #ifdef FFS_EI
    928 		if (needswap)
    929 			ffs_csum_swap((struct csum *)bp->b_data,
    930 				(struct csum *)space, size);
    931 		else
    932 #endif
    933 			memcpy(space, bp->b_data, (u_int)size);
    934 
    935 		space = (char *)space + size;
    936 		brelse(bp);
    937 		bp = NULL;
    938 	}
    939 	if (fs->fs_contigsumsize > 0) {
    940 		fs->fs_maxcluster = lp = space;
    941 		for (i = 0; i < fs->fs_ncg; i++)
    942 			*lp++ = fs->fs_contigsumsize;
    943 		space = lp;
    944 	}
    945 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    946 	fs->fs_contigdirs = space;
    947 	space = (char *)space + size;
    948 	memset(fs->fs_contigdirs, 0, size);
    949 		/* Compatibility for old filesystems - XXX */
    950 	if (fs->fs_avgfilesize <= 0)
    951 		fs->fs_avgfilesize = AVFILESIZ;
    952 	if (fs->fs_avgfpdir <= 0)
    953 		fs->fs_avgfpdir = AFPDIR;
    954 	fs->fs_active = NULL;
    955 	mp->mnt_data = ump;
    956 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    957 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
    958 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    959 	mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
    960 	if (UFS_MPISAPPLEUFS(ump)) {
    961 		/* NeXT used to keep short symlinks in the inode even
    962 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
    963 		 * is probably -1, but we still need to be able to identify
    964 		 * short symlinks.
    965 		 */
    966 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    967 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
    968 		mp->mnt_iflag |= IMNT_DTYPE;
    969 	} else {
    970 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
    971 		ump->um_dirblksiz = DIRBLKSIZ;
    972 		if (ump->um_maxsymlinklen > 0)
    973 			mp->mnt_iflag |= IMNT_DTYPE;
    974 		else
    975 			mp->mnt_iflag &= ~IMNT_DTYPE;
    976 	}
    977 	mp->mnt_fs_bshift = fs->fs_bshift;
    978 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    979 	mp->mnt_flag |= MNT_LOCAL;
    980 #ifdef NEWVNGATE
    981 	mp->mnt_iflag |= IMNT_HAS_TRANS;
    982 #endif
    983 #ifdef FFS_EI
    984 	if (needswap)
    985 		ump->um_flags |= UFS_NEEDSWAP;
    986 #endif
    987 	ump->um_mountp = mp;
    988 	ump->um_dev = dev;
    989 	ump->um_devvp = devvp;
    990 	ump->um_nindir = fs->fs_nindir;
    991 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    992 	ump->um_bptrtodb = fs->fs_fsbtodb;
    993 	ump->um_seqinc = fs->fs_frag;
    994 	for (i = 0; i < MAXQUOTAS; i++)
    995 		ump->um_quotas[i] = NULLVP;
    996 	devvp->v_specmountpoint = mp;
    997 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    998 		error = softdep_mount(devvp, mp, fs, cred);
    999 		if (error) {
   1000 			free(fs->fs_csp, M_UFSMNT);
   1001 			goto out;
   1002 		}
   1003 	}
   1004 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
   1005 		ffs_snapshot_mount(mp);
   1006 #ifdef UFS_EXTATTR
   1007 	/*
   1008 	 * Initialize file-backed extended attributes on UFS1 file
   1009 	 * systems.
   1010 	 */
   1011 	if (ump->um_fstype == UFS1) {
   1012 		ufs_extattr_uepm_init(&ump->um_extattr);
   1013 #ifdef UFS_EXTATTR_AUTOSTART
   1014 		/*
   1015 		 * XXX Just ignore errors.  Not clear that we should
   1016 		 * XXX fail the mount in this case.
   1017 		 */
   1018 		(void) ufs_extattr_autostart(mp, l);
   1019 #endif
   1020 	}
   1021 #endif /* UFS_EXTATTR */
   1022 	return (0);
   1023 out:
   1024 	if (fs)
   1025 		free(fs, M_UFSMNT);
   1026 	devvp->v_specmountpoint = NULL;
   1027 	if (bp)
   1028 		brelse(bp);
   1029 	if (ump) {
   1030 		if (ump->um_oldfscompat)
   1031 			free(ump->um_oldfscompat, M_UFSMNT);
   1032 		free(ump, M_UFSMNT);
   1033 		mp->mnt_data = NULL;
   1034 	}
   1035 	return (error);
   1036 }
   1037 
   1038 /*
   1039  * Sanity checks for loading old filesystem superblocks.
   1040  * See ffs_oldfscompat_write below for unwound actions.
   1041  *
   1042  * XXX - Parts get retired eventually.
   1043  * Unfortunately new bits get added.
   1044  */
   1045 static void
   1046 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
   1047 {
   1048 	off_t maxfilesize;
   1049 	int32_t *extrasave;
   1050 
   1051 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1052 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1053 		return;
   1054 
   1055 	if (!ump->um_oldfscompat)
   1056 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
   1057 		    M_UFSMNT, M_WAITOK);
   1058 
   1059 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1060 	extrasave = ump->um_oldfscompat;
   1061 	extrasave += 512/sizeof(int32_t);
   1062 	extrasave[0] = fs->fs_old_npsect;
   1063 	extrasave[1] = fs->fs_old_interleave;
   1064 	extrasave[2] = fs->fs_old_trackskew;
   1065 
   1066 	/* These fields will be overwritten by their
   1067 	 * original values in fs_oldfscompat_write, so it is harmless
   1068 	 * to modify them here.
   1069 	 */
   1070 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1071 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1072 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1073 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1074 
   1075 	fs->fs_maxbsize = fs->fs_bsize;
   1076 	fs->fs_time = fs->fs_old_time;
   1077 	fs->fs_size = fs->fs_old_size;
   1078 	fs->fs_dsize = fs->fs_old_dsize;
   1079 	fs->fs_csaddr = fs->fs_old_csaddr;
   1080 	fs->fs_sblockloc = sblockloc;
   1081 
   1082         fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1083 
   1084 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1085 		fs->fs_old_nrpos = 8;
   1086 		fs->fs_old_npsect = fs->fs_old_nsect;
   1087 		fs->fs_old_interleave = 1;
   1088 		fs->fs_old_trackskew = 0;
   1089 	}
   1090 
   1091 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1092 		ump->um_maxfilesize = (u_quad_t) 1LL << 39;
   1093 		fs->fs_qbmask = ~fs->fs_bmask;
   1094 		fs->fs_qfmask = ~fs->fs_fmask;
   1095 	}
   1096 
   1097 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1098 	if (ump->um_maxfilesize > maxfilesize)
   1099 		ump->um_maxfilesize = maxfilesize;
   1100 
   1101 	/* Compatibility for old filesystems */
   1102 	if (fs->fs_avgfilesize <= 0)
   1103 		fs->fs_avgfilesize = AVFILESIZ;
   1104 	if (fs->fs_avgfpdir <= 0)
   1105 		fs->fs_avgfpdir = AFPDIR;
   1106 
   1107 #if 0
   1108 	if (bigcgs) {
   1109 		fs->fs_save_cgsize = fs->fs_cgsize;
   1110 		fs->fs_cgsize = fs->fs_bsize;
   1111 	}
   1112 #endif
   1113 }
   1114 
   1115 /*
   1116  * Unwinding superblock updates for old filesystems.
   1117  * See ffs_oldfscompat_read above for details.
   1118  *
   1119  * XXX - Parts get retired eventually.
   1120  * Unfortunately new bits get added.
   1121  */
   1122 static void
   1123 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
   1124 {
   1125 	int32_t *extrasave;
   1126 
   1127 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1128 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1129 		return;
   1130 
   1131 	fs->fs_old_time = fs->fs_time;
   1132 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1133 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1134 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1135 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1136 	fs->fs_old_flags = fs->fs_flags;
   1137 
   1138 #if 0
   1139 	if (bigcgs) {
   1140 		fs->fs_cgsize = fs->fs_save_cgsize;
   1141 	}
   1142 #endif
   1143 
   1144 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1145 	extrasave = ump->um_oldfscompat;
   1146 	extrasave += 512/sizeof(int32_t);
   1147 	fs->fs_old_npsect = extrasave[0];
   1148 	fs->fs_old_interleave = extrasave[1];
   1149 	fs->fs_old_trackskew = extrasave[2];
   1150 
   1151 }
   1152 
   1153 /*
   1154  * unmount system call
   1155  */
   1156 int
   1157 ffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
   1158 {
   1159 	struct ufsmount *ump = VFSTOUFS(mp);
   1160 	struct fs *fs = ump->um_fs;
   1161 	int error, flags, penderr;
   1162 
   1163 	penderr = 0;
   1164 	flags = 0;
   1165 	if (mntflags & MNT_FORCE)
   1166 		flags |= FORCECLOSE;
   1167 #ifdef UFS_EXTATTR
   1168 	if (ump->um_fstype == UFS1) {
   1169 		error = ufs_extattr_stop(mp, l);
   1170 		if (error) {
   1171 			if (error != EOPNOTSUPP)
   1172 				printf("%s: ufs_extattr_stop returned %d\n",
   1173 				    fs->fs_fsmnt, error);
   1174 		} else
   1175 			ufs_extattr_uepm_destroy(&ump->um_extattr);
   1176 	}
   1177 #endif /* UFS_EXTATTR */
   1178 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1179 		if ((error = softdep_flushfiles(mp, flags, l)) != 0)
   1180 			return (error);
   1181 	} else {
   1182 		if ((error = ffs_flushfiles(mp, flags, l)) != 0)
   1183 			return (error);
   1184 	}
   1185 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1186 		printf("%s: unmount pending error: blocks %" PRId64
   1187 		       " files %d\n",
   1188 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1189 		fs->fs_pendingblocks = 0;
   1190 		fs->fs_pendinginodes = 0;
   1191 		penderr = 1;
   1192 	}
   1193 	if (fs->fs_ronly == 0 &&
   1194 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1195 	    fs->fs_clean & FS_WASCLEAN) {
   1196 		/*
   1197 		 * XXXX don't mark fs clean in the case of softdep
   1198 		 * pending block errors, until they are fixed.
   1199 		 */
   1200 		if (penderr == 0) {
   1201 			if (mp->mnt_flag & MNT_SOFTDEP)
   1202 				fs->fs_flags &= ~FS_DOSOFTDEP;
   1203 			fs->fs_clean = FS_ISCLEAN;
   1204 		}
   1205 		fs->fs_fmod = 0;
   1206 		(void) ffs_sbupdate(ump, MNT_WAIT);
   1207 	}
   1208 	if (ump->um_devvp->v_type != VBAD)
   1209 		ump->um_devvp->v_specmountpoint = NULL;
   1210 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1211 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
   1212 		NOCRED, l);
   1213 	vput(ump->um_devvp);
   1214 	free(fs->fs_csp, M_UFSMNT);
   1215 	free(fs, M_UFSMNT);
   1216 	if (ump->um_oldfscompat != NULL)
   1217 		free(ump->um_oldfscompat, M_UFSMNT);
   1218 	free(ump, M_UFSMNT);
   1219 	mp->mnt_data = NULL;
   1220 	mp->mnt_flag &= ~MNT_LOCAL;
   1221 	return (0);
   1222 }
   1223 
   1224 /*
   1225  * Flush out all the files in a filesystem.
   1226  */
   1227 int
   1228 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
   1229 {
   1230 	extern int doforce;
   1231 	struct ufsmount *ump;
   1232 	int error;
   1233 
   1234 	if (!doforce)
   1235 		flags &= ~FORCECLOSE;
   1236 	ump = VFSTOUFS(mp);
   1237 #ifdef QUOTA
   1238 	if (mp->mnt_flag & MNT_QUOTA) {
   1239 		int i;
   1240 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
   1241 			return (error);
   1242 		for (i = 0; i < MAXQUOTAS; i++) {
   1243 			if (ump->um_quotas[i] == NULLVP)
   1244 				continue;
   1245 			quotaoff(l, mp, i);
   1246 		}
   1247 		/*
   1248 		 * Here we fall through to vflush again to ensure
   1249 		 * that we have gotten rid of all the system vnodes.
   1250 		 */
   1251 	}
   1252 #endif
   1253 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1254 		return (error);
   1255 	ffs_snapshot_unmount(mp);
   1256 	/*
   1257 	 * Flush all the files.
   1258 	 */
   1259 	error = vflush(mp, NULLVP, flags);
   1260 	if (error)
   1261 		return (error);
   1262 	/*
   1263 	 * Flush filesystem metadata.
   1264 	 */
   1265 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1266 	error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0, l);
   1267 	VOP_UNLOCK(ump->um_devvp, 0);
   1268 	return (error);
   1269 }
   1270 
   1271 /*
   1272  * Get file system statistics.
   1273  */
   1274 int
   1275 ffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
   1276 {
   1277 	struct ufsmount *ump;
   1278 	struct fs *fs;
   1279 
   1280 	ump = VFSTOUFS(mp);
   1281 	fs = ump->um_fs;
   1282 	sbp->f_bsize = fs->fs_bsize;
   1283 	sbp->f_frsize = fs->fs_fsize;
   1284 	sbp->f_iosize = fs->fs_bsize;
   1285 	sbp->f_blocks = fs->fs_dsize;
   1286 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1287 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1288 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1289 	    fs->fs_minfree) / (u_int64_t) 100;
   1290 	if (sbp->f_bfree > sbp->f_bresvd)
   1291 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1292 	else
   1293 		sbp->f_bavail = 0;
   1294 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1295 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1296 	sbp->f_favail = sbp->f_ffree;
   1297 	sbp->f_fresvd = 0;
   1298 	copy_statvfs_info(sbp, mp);
   1299 	return (0);
   1300 }
   1301 
   1302 /*
   1303  * Go through the disk queues to initiate sandbagged IO;
   1304  * go through the inodes to write those that have been modified;
   1305  * initiate the writing of the super block if it has been modified.
   1306  *
   1307  * Note: we are always called with the filesystem marked `MPBUSY'.
   1308  */
   1309 int
   1310 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *l)
   1311 {
   1312 	struct vnode *vp, *nvp;
   1313 	struct inode *ip;
   1314 	struct ufsmount *ump = VFSTOUFS(mp);
   1315 	struct fs *fs;
   1316 	int error, count, allerror = 0;
   1317 
   1318 	fs = ump->um_fs;
   1319 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1320 		printf("fs = %s\n", fs->fs_fsmnt);
   1321 		panic("update: rofs mod");
   1322 	}
   1323 	if ((error = fstrans_start(mp, FSTRANS_SHARED)) != 0)
   1324 		return error;
   1325 	/*
   1326 	 * Write back each (modified) inode.
   1327 	 */
   1328 	simple_lock(&mntvnode_slock);
   1329 loop:
   1330 	/*
   1331 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
   1332 	 * and vclean() can be called indirectly
   1333 	 */
   1334 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
   1335 		/*
   1336 		 * If the vnode that we are about to sync is no longer
   1337 		 * associated with this mount point, start over.
   1338 		 */
   1339 		if (vp->v_mount != mp)
   1340 			goto loop;
   1341 		simple_lock(&vp->v_interlock);
   1342 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
   1343 		ip = VTOI(vp);
   1344 		if (vp->v_type == VNON ||
   1345 		    ((ip->i_flag &
   1346 		      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
   1347 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1348 		     vp->v_uobj.uo_npages == 0))
   1349 		{
   1350 			simple_unlock(&vp->v_interlock);
   1351 			continue;
   1352 		}
   1353 		if (vp->v_type == VBLK &&
   1354 		    fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
   1355 			simple_unlock(&vp->v_interlock);
   1356 			continue;
   1357 		}
   1358 		simple_unlock(&mntvnode_slock);
   1359 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1360 		if (error) {
   1361 			simple_lock(&mntvnode_slock);
   1362 			if (error == ENOENT)
   1363 				goto loop;
   1364 			continue;
   1365 		}
   1366 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
   1367 			error = ffs_update(vp, NULL, NULL, 0);
   1368 		else
   1369 			error = VOP_FSYNC(vp, cred,
   1370 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
   1371 		if (error)
   1372 			allerror = error;
   1373 		vput(vp);
   1374 		simple_lock(&mntvnode_slock);
   1375 	}
   1376 	simple_unlock(&mntvnode_slock);
   1377 	/*
   1378 	 * Force stale file system control information to be flushed.
   1379 	 */
   1380 	if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
   1381 		if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
   1382 			allerror = error;
   1383 		/* Flushed work items may create new vnodes to clean */
   1384 		if (allerror == 0 && count) {
   1385 			simple_lock(&mntvnode_slock);
   1386 			goto loop;
   1387 		}
   1388 	}
   1389 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1390 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1391 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1392 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1393 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
   1394 			allerror = error;
   1395 		VOP_UNLOCK(ump->um_devvp, 0);
   1396 		if (allerror == 0 && waitfor == MNT_WAIT) {
   1397 			simple_lock(&mntvnode_slock);
   1398 			goto loop;
   1399 		}
   1400 	}
   1401 #ifdef QUOTA
   1402 	qsync(mp);
   1403 #endif
   1404 	/*
   1405 	 * Write back modified superblock.
   1406 	 */
   1407 	if (fs->fs_fmod != 0) {
   1408 		fs->fs_fmod = 0;
   1409 		fs->fs_time = time_second;
   1410 		if ((error = ffs_cgupdate(ump, waitfor)))
   1411 			allerror = error;
   1412 	}
   1413 	fstrans_done(mp);
   1414 	return (allerror);
   1415 }
   1416 
   1417 /*
   1418  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1419  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1420  * return the inode locked.  Detection and handling of mount points must be
   1421  * done by the calling routine.
   1422  */
   1423 int
   1424 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1425 {
   1426 	struct fs *fs;
   1427 	struct inode *ip;
   1428 	struct ufsmount *ump;
   1429 	struct buf *bp;
   1430 	struct vnode *vp;
   1431 	dev_t dev;
   1432 	int error;
   1433 
   1434 	ump = VFSTOUFS(mp);
   1435 	dev = ump->um_dev;
   1436 
   1437 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1438 		return (0);
   1439 
   1440 	/* Allocate a new vnode/inode. */
   1441 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1442 		*vpp = NULL;
   1443 		return (error);
   1444 	}
   1445 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1446 
   1447 	/*
   1448 	 * If someone beat us to it while sleeping in getnewvnode(),
   1449 	 * push back the freshly allocated vnode we don't need, and return.
   1450 	 */
   1451 	mutex_enter(&ufs_hashlock);
   1452 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1453 		mutex_exit(&ufs_hashlock);
   1454 		ungetnewvnode(vp);
   1455 		pool_put(&ffs_inode_pool, ip);
   1456 		return (0);
   1457 	}
   1458 
   1459 	vp->v_flag |= VLOCKSWORK;
   1460 
   1461 	/*
   1462 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1463 	 * XXX create another pool for MFS inodes?
   1464 	 */
   1465 
   1466 	memset(ip, 0, sizeof(struct inode));
   1467 	vp->v_data = ip;
   1468 	ip->i_vnode = vp;
   1469 	ip->i_ump = ump;
   1470 	ip->i_fs = fs = ump->um_fs;
   1471 	ip->i_dev = dev;
   1472 	ip->i_number = ino;
   1473 	LIST_INIT(&ip->i_pcbufhd);
   1474 #ifdef QUOTA
   1475 	{
   1476 		int i;
   1477 
   1478 		for (i = 0; i < MAXQUOTAS; i++)
   1479 			ip->i_dquot[i] = NODQUOT;
   1480 	}
   1481 #endif
   1482 
   1483 	/*
   1484 	 * Put it onto its hash chain and lock it so that other requests for
   1485 	 * this inode will block if they arrive while we are sleeping waiting
   1486 	 * for old data structures to be purged or for the contents of the
   1487 	 * disk portion of this inode to be read.
   1488 	 */
   1489 
   1490 	ufs_ihashins(ip);
   1491 	mutex_exit(&ufs_hashlock);
   1492 
   1493 	/* Read in the disk contents for the inode, copy into the inode. */
   1494 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1495 		      (int)fs->fs_bsize, NOCRED, &bp);
   1496 	if (error) {
   1497 
   1498 		/*
   1499 		 * The inode does not contain anything useful, so it would
   1500 		 * be misleading to leave it on its hash chain. With mode
   1501 		 * still zero, it will be unlinked and returned to the free
   1502 		 * list by vput().
   1503 		 */
   1504 
   1505 		vput(vp);
   1506 		brelse(bp);
   1507 		*vpp = NULL;
   1508 		return (error);
   1509 	}
   1510 	if (ip->i_ump->um_fstype == UFS1)
   1511 		ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
   1512 	else
   1513 		ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
   1514 	ffs_load_inode(bp, ip, fs, ino);
   1515 	if (DOINGSOFTDEP(vp))
   1516 		softdep_load_inodeblock(ip);
   1517 	else
   1518 		ip->i_ffs_effnlink = ip->i_nlink;
   1519 	brelse(bp);
   1520 
   1521 	/*
   1522 	 * Initialize the vnode from the inode, check for aliases.
   1523 	 * Note that the underlying vnode may have changed.
   1524 	 */
   1525 
   1526 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1527 
   1528 	/*
   1529 	 * Finish inode initialization now that aliasing has been resolved.
   1530 	 */
   1531 
   1532 	genfs_node_init(vp, &ffs_genfsops);
   1533 	ip->i_devvp = ump->um_devvp;
   1534 	VREF(ip->i_devvp);
   1535 
   1536 	/*
   1537 	 * Ensure that uid and gid are correct. This is a temporary
   1538 	 * fix until fsck has been changed to do the update.
   1539 	 */
   1540 
   1541 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1542 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1543 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1544 	}							/* XXX */
   1545 	uvm_vnp_setsize(vp, ip->i_size);
   1546 	*vpp = vp;
   1547 	return (0);
   1548 }
   1549 
   1550 /*
   1551  * File handle to vnode
   1552  *
   1553  * Have to be really careful about stale file handles:
   1554  * - check that the inode number is valid
   1555  * - call ffs_vget() to get the locked inode
   1556  * - check for an unallocated inode (i_mode == 0)
   1557  * - check that the given client host has export rights and return
   1558  *   those rights via. exflagsp and credanonp
   1559  */
   1560 int
   1561 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1562 {
   1563 	struct ufid ufh;
   1564 	struct fs *fs;
   1565 
   1566 	if (fhp->fid_len != sizeof(struct ufid))
   1567 		return EINVAL;
   1568 
   1569 	memcpy(&ufh, fhp, sizeof(ufh));
   1570 	fs = VFSTOUFS(mp)->um_fs;
   1571 	if (ufh.ufid_ino < ROOTINO ||
   1572 	    ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1573 		return (ESTALE);
   1574 	return (ufs_fhtovp(mp, &ufh, vpp));
   1575 }
   1576 
   1577 /*
   1578  * Vnode pointer to File handle
   1579  */
   1580 /* ARGSUSED */
   1581 int
   1582 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1583 {
   1584 	struct inode *ip;
   1585 	struct ufid ufh;
   1586 
   1587 	if (*fh_size < sizeof(struct ufid)) {
   1588 		*fh_size = sizeof(struct ufid);
   1589 		return E2BIG;
   1590 	}
   1591 	ip = VTOI(vp);
   1592 	*fh_size = sizeof(struct ufid);
   1593 	memset(&ufh, 0, sizeof(ufh));
   1594 	ufh.ufid_len = sizeof(struct ufid);
   1595 	ufh.ufid_ino = ip->i_number;
   1596 	ufh.ufid_gen = ip->i_gen;
   1597 	memcpy(fhp, &ufh, sizeof(ufh));
   1598 	return (0);
   1599 }
   1600 
   1601 void
   1602 ffs_init(void)
   1603 {
   1604 	if (ffs_initcount++ > 0)
   1605 		return;
   1606 
   1607 #ifdef _LKM
   1608 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
   1609 		  "ffsinopl", &pool_allocator_nointr);
   1610 	pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
   1611 		  "dino1pl", &pool_allocator_nointr);
   1612 	pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
   1613 		  "dino2pl", &pool_allocator_nointr);
   1614 #endif
   1615 	softdep_initialize();
   1616 	ufs_init();
   1617 }
   1618 
   1619 void
   1620 ffs_reinit(void)
   1621 {
   1622 	softdep_reinitialize();
   1623 	ufs_reinit();
   1624 }
   1625 
   1626 void
   1627 ffs_done(void)
   1628 {
   1629 	if (--ffs_initcount > 0)
   1630 		return;
   1631 
   1632 	/* XXX softdep cleanup ? */
   1633 	ufs_done();
   1634 #ifdef _LKM
   1635 	pool_destroy(&ffs_dinode2_pool);
   1636 	pool_destroy(&ffs_dinode1_pool);
   1637 	pool_destroy(&ffs_inode_pool);
   1638 #endif
   1639 }
   1640 
   1641 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
   1642 {
   1643 #if 0
   1644 	extern int doasyncfree;
   1645 #endif
   1646 	extern int ffs_log_changeopt;
   1647 
   1648 	sysctl_createv(clog, 0, NULL, NULL,
   1649 		       CTLFLAG_PERMANENT,
   1650 		       CTLTYPE_NODE, "vfs", NULL,
   1651 		       NULL, 0, NULL, 0,
   1652 		       CTL_VFS, CTL_EOL);
   1653 	sysctl_createv(clog, 0, NULL, NULL,
   1654 		       CTLFLAG_PERMANENT,
   1655 		       CTLTYPE_NODE, "ffs",
   1656 		       SYSCTL_DESCR("Berkeley Fast File System"),
   1657 		       NULL, 0, NULL, 0,
   1658 		       CTL_VFS, 1, CTL_EOL);
   1659 
   1660 	/*
   1661 	 * @@@ should we even bother with these first three?
   1662 	 */
   1663 	sysctl_createv(clog, 0, NULL, NULL,
   1664 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1665 		       CTLTYPE_INT, "doclusterread", NULL,
   1666 		       sysctl_notavail, 0, NULL, 0,
   1667 		       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
   1668 	sysctl_createv(clog, 0, NULL, NULL,
   1669 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1670 		       CTLTYPE_INT, "doclusterwrite", NULL,
   1671 		       sysctl_notavail, 0, NULL, 0,
   1672 		       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
   1673 	sysctl_createv(clog, 0, NULL, NULL,
   1674 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1675 		       CTLTYPE_INT, "doreallocblks", NULL,
   1676 		       sysctl_notavail, 0, NULL, 0,
   1677 		       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
   1678 #if 0
   1679 	sysctl_createv(clog, 0, NULL, NULL,
   1680 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1681 		       CTLTYPE_INT, "doasyncfree",
   1682 		       SYSCTL_DESCR("Release dirty blocks asynchronously"),
   1683 		       NULL, 0, &doasyncfree, 0,
   1684 		       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
   1685 #endif
   1686 	sysctl_createv(clog, 0, NULL, NULL,
   1687 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1688 		       CTLTYPE_INT, "log_changeopt",
   1689 		       SYSCTL_DESCR("Log changes in optimization strategy"),
   1690 		       NULL, 0, &ffs_log_changeopt, 0,
   1691 		       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
   1692 }
   1693 
   1694 /*
   1695  * Write a superblock and associated information back to disk.
   1696  */
   1697 int
   1698 ffs_sbupdate(struct ufsmount *mp, int waitfor)
   1699 {
   1700 	struct fs *fs = mp->um_fs;
   1701 	struct buf *bp;
   1702 	int error = 0;
   1703 	u_int32_t saveflag;
   1704 
   1705 	bp = getblk(mp->um_devvp,
   1706 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
   1707 	    (int)fs->fs_sbsize, 0, 0);
   1708 	saveflag = fs->fs_flags & FS_INTERNAL;
   1709 	fs->fs_flags &= ~FS_INTERNAL;
   1710 
   1711 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1712 
   1713 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1714 #ifdef FFS_EI
   1715 	if (mp->um_flags & UFS_NEEDSWAP)
   1716 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   1717 #endif
   1718 	fs->fs_flags |= saveflag;
   1719 
   1720 	if (waitfor == MNT_WAIT)
   1721 		error = bwrite(bp);
   1722 	else
   1723 		bawrite(bp);
   1724 	return (error);
   1725 }
   1726 
   1727 int
   1728 ffs_cgupdate(struct ufsmount *mp, int waitfor)
   1729 {
   1730 	struct fs *fs = mp->um_fs;
   1731 	struct buf *bp;
   1732 	int blks;
   1733 	void *space;
   1734 	int i, size, error = 0, allerror = 0;
   1735 
   1736 	allerror = ffs_sbupdate(mp, waitfor);
   1737 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1738 	space = fs->fs_csp;
   1739 	for (i = 0; i < blks; i += fs->fs_frag) {
   1740 		size = fs->fs_bsize;
   1741 		if (i + fs->fs_frag > blks)
   1742 			size = (blks - i) * fs->fs_fsize;
   1743 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1744 		    size, 0, 0);
   1745 #ifdef FFS_EI
   1746 		if (mp->um_flags & UFS_NEEDSWAP)
   1747 			ffs_csum_swap((struct csum*)space,
   1748 			    (struct csum*)bp->b_data, size);
   1749 		else
   1750 #endif
   1751 			memcpy(bp->b_data, space, (u_int)size);
   1752 		space = (char *)space + size;
   1753 		if (waitfor == MNT_WAIT)
   1754 			error = bwrite(bp);
   1755 		else
   1756 			bawrite(bp);
   1757 	}
   1758 	if (!allerror && error)
   1759 		allerror = error;
   1760 	return (allerror);
   1761 }
   1762 
   1763 int
   1764 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   1765     int attrnamespace, const char *attrname, struct lwp *l)
   1766 {
   1767 #ifdef UFS_EXTATTR
   1768 	/*
   1769 	 * File-backed extended attributes are only supported on UFS1.
   1770 	 * UFS2 has native extended attributes.
   1771 	 */
   1772 	if (VFSTOUFS(mp)->um_fstype == UFS1)
   1773 		return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname,
   1774 				       l));
   1775 #endif
   1776 	return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname, l));
   1777 }
   1778 
   1779 int
   1780 ffs_suspendctl(struct mount *mp, int cmd)
   1781 {
   1782 	int error;
   1783 	struct lwp *l = curlwp;
   1784 
   1785 	switch (cmd) {
   1786 	case SUSPEND_SUSPEND:
   1787 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   1788 			return error;
   1789 		error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred, l);
   1790 		if (error == 0)
   1791 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   1792 		if (error != 0) {
   1793 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   1794 			return error;
   1795 		}
   1796 		return 0;
   1797 
   1798 	case SUSPEND_RESUME:
   1799 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   1800 
   1801 	default:
   1802 		return EINVAL;
   1803 	}
   1804 }
   1805