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