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