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