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