Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.162.2.2
      1 /*	$NetBSD: ffs_vfsops.c,v 1.162.2.2 2006/03/10 13:02:08 tron 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.162.2.2 2006/03/10 13:02:08 tron 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 
    115 static const struct genfs_ops ffs_genfsops = {
    116 	.gop_size = ffs_gop_size,
    117 	.gop_alloc = ufs_gop_alloc,
    118 	.gop_write = genfs_gop_write,
    119 	.gop_markupdate = ufs_gop_markupdate,
    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 flags;
    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 			flags = FREAD;
    286 		else
    287 			flags = FREAD|FWRITE;
    288 		error = VOP_OPEN(devvp, flags, 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, flags, 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 			fs = NULL;
    757 			goto out;
    758 		}
    759 		fs = (struct fs*)bp->b_data;
    760 		fsblockloc = sblockloc = sblock_try[i];
    761 		if (fs->fs_magic == FS_UFS1_MAGIC) {
    762 			sbsize = fs->fs_sbsize;
    763 			fstype = UFS1;
    764 #ifdef FFS_EI
    765 			needswap = 0;
    766 		} else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
    767 			sbsize = bswap32(fs->fs_sbsize);
    768 			fstype = UFS1;
    769 			needswap = 1;
    770 #endif
    771 		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
    772 			sbsize = fs->fs_sbsize;
    773 			fstype = UFS2;
    774 #ifdef FFS_EI
    775 			needswap = 0;
    776 		} else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
    777 			sbsize = bswap32(fs->fs_sbsize);
    778 			fstype = UFS2;
    779 			needswap = 1;
    780 #endif
    781 		} else
    782 			continue;
    783 
    784 
    785 		/* fs->fs_sblockloc isn't defined for old filesystems */
    786 		if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
    787 			if (sblockloc == SBLOCK_UFS2)
    788 				/*
    789 				 * This is likely to be the first alternate
    790 				 * in a filesystem with 64k blocks.
    791 				 * Don't use it.
    792 				 */
    793 				continue;
    794 			fsblockloc = sblockloc;
    795 		} else {
    796 			fsblockloc = fs->fs_sblockloc;
    797 #ifdef FFS_EI
    798 			if (needswap)
    799 				fsblockloc = bswap64(fsblockloc);
    800 #endif
    801 		}
    802 
    803 		/* Check we haven't found an alternate superblock */
    804 		if (fsblockloc != sblockloc)
    805 			continue;
    806 
    807 		/* Validate size of superblock */
    808 		if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
    809 			continue;
    810 
    811 		/* Ok seems to be a good superblock */
    812 		break;
    813 	}
    814 
    815 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    816 	memcpy(fs, bp->b_data, sbsize);
    817 
    818 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    819 	memset(ump, 0, sizeof *ump);
    820 	TAILQ_INIT(&ump->um_snapshots);
    821 	ump->um_fs = fs;
    822 
    823 #ifdef FFS_EI
    824 	if (needswap) {
    825 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    826 		fs->fs_flags |= FS_SWAPPED;
    827 	} else
    828 #endif
    829 		fs->fs_flags &= ~FS_SWAPPED;
    830 
    831 	ffs_oldfscompat_read(fs, ump, sblockloc);
    832 	ump->um_maxfilesize = fs->fs_maxfilesize;
    833 
    834 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    835 		fs->fs_pendingblocks = 0;
    836 		fs->fs_pendinginodes = 0;
    837 	}
    838 
    839 	ump->um_fstype = fstype;
    840 	if (fs->fs_sbsize < SBLOCKSIZE)
    841 		bp->b_flags |= B_INVAL;
    842 	brelse(bp);
    843 	bp = NULL;
    844 
    845 	/* First check to see if this is tagged as an Apple UFS filesystem
    846 	 * in the disklabel
    847 	 */
    848 	if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) == 0) &&
    849 		(dpart.part->p_fstype == FS_APPLEUFS)) {
    850 		ump->um_flags |= UFS_ISAPPLEUFS;
    851 	}
    852 #ifdef APPLE_UFS
    853 	else {
    854 		/* Manually look for an apple ufs label, and if a valid one
    855 		 * is found, then treat it like an Apple UFS filesystem anyway
    856 		 */
    857 		error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
    858 			APPLEUFS_LABEL_SIZE, cred, &bp);
    859 		if (error)
    860 			goto out;
    861 		error = ffs_appleufs_validate(fs->fs_fsmnt,
    862 			(struct appleufslabel *)bp->b_data,NULL);
    863 		if (error == 0) {
    864 			ump->um_flags |= UFS_ISAPPLEUFS;
    865 		}
    866 		brelse(bp);
    867 		bp = NULL;
    868 	}
    869 #else
    870 	if (ump->um_flags & UFS_ISAPPLEUFS) {
    871 		error = EINVAL;
    872 		goto out;
    873 	}
    874 #endif
    875 
    876 	/*
    877 	 * verify that we can access the last block in the fs
    878 	 * if we're mounting read/write.
    879 	 */
    880 
    881 	if (!ronly) {
    882 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    883 		    cred, &bp);
    884 		if (bp->b_bcount != fs->fs_fsize)
    885 			error = EINVAL;
    886 		bp->b_flags |= B_INVAL;
    887 		if (error)
    888 			goto out;
    889 		brelse(bp);
    890 		bp = NULL;
    891 	}
    892 
    893 	fs->fs_ronly = ronly;
    894 	if (ronly == 0) {
    895 		fs->fs_clean <<= 1;
    896 		fs->fs_fmod = 1;
    897 	}
    898 	size = fs->fs_cssize;
    899 	blks = howmany(size, fs->fs_fsize);
    900 	if (fs->fs_contigsumsize > 0)
    901 		size += fs->fs_ncg * sizeof(int32_t);
    902 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    903 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    904 	fs->fs_csp = space;
    905 	for (i = 0; i < blks; i += fs->fs_frag) {
    906 		size = fs->fs_bsize;
    907 		if (i + fs->fs_frag > blks)
    908 			size = (blks - i) * fs->fs_fsize;
    909 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    910 			      cred, &bp);
    911 		if (error) {
    912 			free(fs->fs_csp, M_UFSMNT);
    913 			goto out;
    914 		}
    915 #ifdef FFS_EI
    916 		if (needswap)
    917 			ffs_csum_swap((struct csum *)bp->b_data,
    918 				(struct csum *)space, size);
    919 		else
    920 #endif
    921 			memcpy(space, bp->b_data, (u_int)size);
    922 
    923 		space = (char *)space + size;
    924 		brelse(bp);
    925 		bp = NULL;
    926 	}
    927 	if (fs->fs_contigsumsize > 0) {
    928 		fs->fs_maxcluster = lp = space;
    929 		for (i = 0; i < fs->fs_ncg; i++)
    930 			*lp++ = fs->fs_contigsumsize;
    931 		space = lp;
    932 	}
    933 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    934 	fs->fs_contigdirs = space;
    935 	space = (char *)space + size;
    936 	memset(fs->fs_contigdirs, 0, size);
    937 		/* Compatibility for old filesystems - XXX */
    938 	if (fs->fs_avgfilesize <= 0)
    939 		fs->fs_avgfilesize = AVFILESIZ;
    940 	if (fs->fs_avgfpdir <= 0)
    941 		fs->fs_avgfpdir = AFPDIR;
    942 	fs->fs_active = NULL;
    943 	mp->mnt_data = ump;
    944 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    945 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
    946 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    947 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    948 	if (UFS_MPISAPPLEUFS(ump)) {
    949 		/* NeXT used to keep short symlinks in the inode even
    950 		 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
    951 		 * is probably -1, but we still need to be able to identify
    952 		 * short symlinks.
    953 		 */
    954 		ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
    955 		ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
    956 		mp->mnt_iflag |= IMNT_DTYPE;
    957 	} else {
    958 		ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
    959 		ump->um_dirblksiz = DIRBLKSIZ;
    960 		if (ump->um_maxsymlinklen > 0)
    961 			mp->mnt_iflag |= IMNT_DTYPE;
    962 		else
    963 			mp->mnt_iflag &= ~IMNT_DTYPE;
    964 	}
    965 	mp->mnt_fs_bshift = fs->fs_bshift;
    966 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    967 	mp->mnt_flag |= MNT_LOCAL;
    968 #ifdef FFS_EI
    969 	if (needswap)
    970 		ump->um_flags |= UFS_NEEDSWAP;
    971 #endif
    972 	ump->um_mountp = mp;
    973 	ump->um_dev = dev;
    974 	ump->um_devvp = devvp;
    975 	ump->um_nindir = fs->fs_nindir;
    976 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    977 	ump->um_bptrtodb = fs->fs_fsbtodb;
    978 	ump->um_seqinc = fs->fs_frag;
    979 	for (i = 0; i < MAXQUOTAS; i++)
    980 		ump->um_quotas[i] = NULLVP;
    981 	devvp->v_specmountpoint = mp;
    982 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    983 		error = softdep_mount(devvp, mp, fs, cred);
    984 		if (error) {
    985 			free(fs->fs_csp, M_UFSMNT);
    986 			goto out;
    987 		}
    988 	}
    989 	if (ronly == 0 && fs->fs_snapinum[0] != 0)
    990 		ffs_snapshot_mount(mp);
    991 	return (0);
    992 out:
    993 	if (fs)
    994 		free(fs, M_UFSMNT);
    995 	devvp->v_specmountpoint = NULL;
    996 	if (bp)
    997 		brelse(bp);
    998 	if (ump) {
    999 		if (ump->um_oldfscompat)
   1000 			free(ump->um_oldfscompat, M_UFSMNT);
   1001 		free(ump, M_UFSMNT);
   1002 		mp->mnt_data = NULL;
   1003 	}
   1004 	return (error);
   1005 }
   1006 
   1007 /*
   1008  * Sanity checks for loading old filesystem superblocks.
   1009  * See ffs_oldfscompat_write below for unwound actions.
   1010  *
   1011  * XXX - Parts get retired eventually.
   1012  * Unfortunately new bits get added.
   1013  */
   1014 static void
   1015 ffs_oldfscompat_read(fs, ump, sblockloc)
   1016 	struct fs *fs;
   1017 	struct ufsmount *ump;
   1018 	daddr_t sblockloc;
   1019 {
   1020 	off_t maxfilesize;
   1021 	int32_t *extrasave;
   1022 
   1023 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1024 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1025 		return;
   1026 
   1027 	if (!ump->um_oldfscompat)
   1028 		ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
   1029 		    M_UFSMNT, M_WAITOK);
   1030 
   1031 	memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
   1032 	extrasave = ump->um_oldfscompat;
   1033 	extrasave += 512/sizeof(int32_t);
   1034 	extrasave[0] = fs->fs_old_npsect;
   1035 	extrasave[1] = fs->fs_old_interleave;
   1036 	extrasave[2] = fs->fs_old_trackskew;
   1037 
   1038 	/* These fields will be overwritten by their
   1039 	 * original values in fs_oldfscompat_write, so it is harmless
   1040 	 * to modify them here.
   1041 	 */
   1042 	fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
   1043 	fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
   1044 	fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
   1045 	fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
   1046 
   1047 	fs->fs_maxbsize = fs->fs_bsize;
   1048 	fs->fs_time = fs->fs_old_time;
   1049 	fs->fs_size = fs->fs_old_size;
   1050 	fs->fs_dsize = fs->fs_old_dsize;
   1051 	fs->fs_csaddr = fs->fs_old_csaddr;
   1052 	fs->fs_sblockloc = sblockloc;
   1053 
   1054         fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
   1055 
   1056 	if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
   1057 		fs->fs_old_nrpos = 8;
   1058 		fs->fs_old_npsect = fs->fs_old_nsect;
   1059 		fs->fs_old_interleave = 1;
   1060 		fs->fs_old_trackskew = 0;
   1061 	}
   1062 
   1063 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {
   1064 		ump->um_maxfilesize = (u_quad_t) 1LL << 39;
   1065 		fs->fs_qbmask = ~fs->fs_bmask;
   1066 		fs->fs_qfmask = ~fs->fs_fmask;
   1067 	}
   1068 
   1069 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
   1070 	if (ump->um_maxfilesize > maxfilesize)
   1071 		ump->um_maxfilesize = maxfilesize;
   1072 
   1073 	/* Compatibility for old filesystems */
   1074 	if (fs->fs_avgfilesize <= 0)
   1075 		fs->fs_avgfilesize = AVFILESIZ;
   1076 	if (fs->fs_avgfpdir <= 0)
   1077 		fs->fs_avgfpdir = AFPDIR;
   1078 
   1079 #if 0
   1080 	if (bigcgs) {
   1081 		fs->fs_save_cgsize = fs->fs_cgsize;
   1082 		fs->fs_cgsize = fs->fs_bsize;
   1083 	}
   1084 #endif
   1085 }
   1086 
   1087 /*
   1088  * Unwinding superblock updates for old filesystems.
   1089  * See ffs_oldfscompat_read above for details.
   1090  *
   1091  * XXX - Parts get retired eventually.
   1092  * Unfortunately new bits get added.
   1093  */
   1094 static void
   1095 ffs_oldfscompat_write(fs, ump)
   1096 	struct fs *fs;
   1097 	struct ufsmount *ump;
   1098 {
   1099 	int32_t *extrasave;
   1100 
   1101 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1102 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1103 		return;
   1104 
   1105 	fs->fs_old_time = fs->fs_time;
   1106 	fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
   1107 	fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
   1108 	fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
   1109 	fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
   1110 	fs->fs_old_flags = fs->fs_flags;
   1111 
   1112 #if 0
   1113 	if (bigcgs) {
   1114 		fs->fs_cgsize = fs->fs_save_cgsize;
   1115 	}
   1116 #endif
   1117 
   1118 	memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
   1119 	extrasave = ump->um_oldfscompat;
   1120 	extrasave += 512/sizeof(int32_t);
   1121 	fs->fs_old_npsect = extrasave[0];
   1122 	fs->fs_old_interleave = extrasave[1];
   1123 	fs->fs_old_trackskew = extrasave[2];
   1124 
   1125 }
   1126 
   1127 /*
   1128  * unmount system call
   1129  */
   1130 int
   1131 ffs_unmount(mp, mntflags, p)
   1132 	struct mount *mp;
   1133 	int mntflags;
   1134 	struct proc *p;
   1135 {
   1136 	struct ufsmount *ump;
   1137 	struct fs *fs;
   1138 	int error, flags, penderr;
   1139 
   1140 	penderr = 0;
   1141 	flags = 0;
   1142 	if (mntflags & MNT_FORCE)
   1143 		flags |= FORCECLOSE;
   1144 	if (mp->mnt_flag & MNT_SOFTDEP) {
   1145 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
   1146 			return (error);
   1147 	} else {
   1148 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
   1149 			return (error);
   1150 	}
   1151 	ump = VFSTOUFS(mp);
   1152 	fs = ump->um_fs;
   1153 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
   1154 		printf("%s: unmount pending error: blocks %" PRId64
   1155 		       " files %d\n",
   1156 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
   1157 		fs->fs_pendingblocks = 0;
   1158 		fs->fs_pendinginodes = 0;
   1159 		penderr = 1;
   1160 	}
   1161 	if (fs->fs_ronly == 0 &&
   1162 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
   1163 	    fs->fs_clean & FS_WASCLEAN) {
   1164 		/*
   1165 		 * XXXX don't mark fs clean in the case of softdep
   1166 		 * pending block errors, until they are fixed.
   1167 		 */
   1168 		if (penderr == 0) {
   1169 			if (mp->mnt_flag & MNT_SOFTDEP)
   1170 				fs->fs_flags &= ~FS_DOSOFTDEP;
   1171 			fs->fs_clean = FS_ISCLEAN;
   1172 		}
   1173 		fs->fs_fmod = 0;
   1174 		(void) ffs_sbupdate(ump, MNT_WAIT);
   1175 	}
   1176 	if (ump->um_devvp->v_type != VBAD)
   1177 		ump->um_devvp->v_specmountpoint = NULL;
   1178 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1179 	(void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
   1180 		NOCRED, p);
   1181 	vput(ump->um_devvp);
   1182 	free(fs->fs_csp, M_UFSMNT);
   1183 	free(fs, M_UFSMNT);
   1184 	if (ump->um_oldfscompat != NULL)
   1185 		free(ump->um_oldfscompat, M_UFSMNT);
   1186 	free(ump, M_UFSMNT);
   1187 	mp->mnt_data = NULL;
   1188 	mp->mnt_flag &= ~MNT_LOCAL;
   1189 	return (0);
   1190 }
   1191 
   1192 /*
   1193  * Flush out all the files in a filesystem.
   1194  */
   1195 int
   1196 ffs_flushfiles(mp, flags, p)
   1197 	struct mount *mp;
   1198 	int flags;
   1199 	struct proc *p;
   1200 {
   1201 	extern int doforce;
   1202 	struct ufsmount *ump;
   1203 	int error;
   1204 
   1205 	if (!doforce)
   1206 		flags &= ~FORCECLOSE;
   1207 	ump = VFSTOUFS(mp);
   1208 #ifdef QUOTA
   1209 	if (mp->mnt_flag & MNT_QUOTA) {
   1210 		int i;
   1211 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
   1212 			return (error);
   1213 		for (i = 0; i < MAXQUOTAS; i++) {
   1214 			if (ump->um_quotas[i] == NULLVP)
   1215 				continue;
   1216 			quotaoff(p, mp, i);
   1217 		}
   1218 		/*
   1219 		 * Here we fall through to vflush again to ensure
   1220 		 * that we have gotten rid of all the system vnodes.
   1221 		 */
   1222 	}
   1223 #endif
   1224 	if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
   1225 		return (error);
   1226 	ffs_snapshot_unmount(mp);
   1227 	/*
   1228 	 * Flush all the files.
   1229 	 */
   1230 	error = vflush(mp, NULLVP, flags);
   1231 	if (error)
   1232 		return (error);
   1233 	/*
   1234 	 * Flush filesystem metadata.
   1235 	 */
   1236 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1237 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
   1238 	VOP_UNLOCK(ump->um_devvp, 0);
   1239 	return (error);
   1240 }
   1241 
   1242 /*
   1243  * Get file system statistics.
   1244  */
   1245 int
   1246 ffs_statvfs(mp, sbp, p)
   1247 	struct mount *mp;
   1248 	struct statvfs *sbp;
   1249 	struct proc *p;
   1250 {
   1251 	struct ufsmount *ump;
   1252 	struct fs *fs;
   1253 
   1254 	ump = VFSTOUFS(mp);
   1255 	fs = ump->um_fs;
   1256 	sbp->f_bsize = fs->fs_bsize;
   1257 	sbp->f_frsize = fs->fs_fsize;
   1258 	sbp->f_iosize = fs->fs_bsize;
   1259 	sbp->f_blocks = fs->fs_dsize;
   1260 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1261 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1262 	sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
   1263 	    fs->fs_minfree) / (u_int64_t) 100;
   1264 	if (sbp->f_bfree > sbp->f_bresvd)
   1265 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1266 	else
   1267 		sbp->f_bavail = 0;
   1268 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1269 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1270 	sbp->f_favail = sbp->f_ffree;
   1271 	sbp->f_fresvd = 0;
   1272 	copy_statvfs_info(sbp, mp);
   1273 	return (0);
   1274 }
   1275 
   1276 /*
   1277  * Go through the disk queues to initiate sandbagged IO;
   1278  * go through the inodes to write those that have been modified;
   1279  * initiate the writing of the super block if it has been modified.
   1280  *
   1281  * Note: we are always called with the filesystem marked `MPBUSY'.
   1282  */
   1283 int
   1284 ffs_sync(mp, waitfor, cred, p)
   1285 	struct mount *mp;
   1286 	int waitfor;
   1287 	struct ucred *cred;
   1288 	struct proc *p;
   1289 {
   1290 	struct vnode *vp, *nvp;
   1291 	struct inode *ip;
   1292 	struct ufsmount *ump = VFSTOUFS(mp);
   1293 	struct fs *fs;
   1294 	int error, count, allerror = 0;
   1295 
   1296 	fs = ump->um_fs;
   1297 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1298 		printf("fs = %s\n", fs->fs_fsmnt);
   1299 		panic("update: rofs mod");
   1300 	}
   1301 	/*
   1302 	 * Write back each (modified) inode.
   1303 	 */
   1304 	simple_lock(&mntvnode_slock);
   1305 loop:
   1306 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1307 		/*
   1308 		 * If the vnode that we are about to sync is no longer
   1309 		 * associated with this mount point, start over.
   1310 		 */
   1311 		if (vp->v_mount != mp)
   1312 			goto loop;
   1313 		simple_lock(&vp->v_interlock);
   1314 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1315 		ip = VTOI(vp);
   1316 		if (vp->v_type == VNON ||
   1317 		    ((ip->i_flag &
   1318 		      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
   1319 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1320 		     vp->v_uobj.uo_npages == 0))
   1321 		{
   1322 			simple_unlock(&vp->v_interlock);
   1323 			continue;
   1324 		}
   1325 		simple_unlock(&mntvnode_slock);
   1326 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1327 		if (error) {
   1328 			simple_lock(&mntvnode_slock);
   1329 			if (error == ENOENT)
   1330 				goto loop;
   1331 			continue;
   1332 		}
   1333 		if (vp->v_type == VREG && waitfor == MNT_LAZY)
   1334 			error = VOP_UPDATE(vp, NULL, NULL, 0);
   1335 		else
   1336 			error = VOP_FSYNC(vp, cred,
   1337 			    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
   1338 		if (error)
   1339 			allerror = error;
   1340 		vput(vp);
   1341 		simple_lock(&mntvnode_slock);
   1342 	}
   1343 	simple_unlock(&mntvnode_slock);
   1344 	/*
   1345 	 * Force stale file system control information to be flushed.
   1346 	 */
   1347 	if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
   1348 		if ((error = softdep_flushworklist(ump->um_mountp, &count, p)))
   1349 			allerror = error;
   1350 		/* Flushed work items may create new vnodes to clean */
   1351 		if (allerror == 0 && count) {
   1352 			simple_lock(&mntvnode_slock);
   1353 			goto loop;
   1354 		}
   1355 	}
   1356 	if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
   1357 	    !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
   1358 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1359 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1360 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1361 			allerror = error;
   1362 		VOP_UNLOCK(ump->um_devvp, 0);
   1363 		if (allerror == 0 && waitfor == MNT_WAIT) {
   1364 			simple_lock(&mntvnode_slock);
   1365 			goto loop;
   1366 		}
   1367 	}
   1368 #ifdef QUOTA
   1369 	qsync(mp);
   1370 #endif
   1371 	/*
   1372 	 * Write back modified superblock.
   1373 	 */
   1374 	if (fs->fs_fmod != 0) {
   1375 		fs->fs_fmod = 0;
   1376 		fs->fs_time = time.tv_sec;
   1377 		if ((error = ffs_cgupdate(ump, waitfor)))
   1378 			allerror = error;
   1379 	}
   1380 	return (allerror);
   1381 }
   1382 
   1383 /*
   1384  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1385  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1386  * return the inode locked.  Detection and handling of mount points must be
   1387  * done by the calling routine.
   1388  */
   1389 int
   1390 ffs_vget(mp, ino, vpp)
   1391 	struct mount *mp;
   1392 	ino_t ino;
   1393 	struct vnode **vpp;
   1394 {
   1395 	struct fs *fs;
   1396 	struct inode *ip;
   1397 	struct ufsmount *ump;
   1398 	struct buf *bp;
   1399 	struct vnode *vp;
   1400 	dev_t dev;
   1401 	int error;
   1402 
   1403 	ump = VFSTOUFS(mp);
   1404 	dev = ump->um_dev;
   1405 
   1406 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1407 		return (0);
   1408 
   1409 	/* Allocate a new vnode/inode. */
   1410 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1411 		*vpp = NULL;
   1412 		return (error);
   1413 	}
   1414 
   1415 	/*
   1416 	 * If someone beat us to it while sleeping in getnewvnode(),
   1417 	 * push back the freshly allocated vnode we don't need, and return.
   1418 	 */
   1419 
   1420 	do {
   1421 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1422 			ungetnewvnode(vp);
   1423 			return (0);
   1424 		}
   1425 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1426 
   1427 	vp->v_flag |= VLOCKSWORK;
   1428 
   1429 	/*
   1430 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1431 	 * XXX create another pool for MFS inodes?
   1432 	 */
   1433 
   1434 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1435 	memset(ip, 0, sizeof(struct inode));
   1436 	vp->v_data = ip;
   1437 	ip->i_vnode = vp;
   1438 	ip->i_ump = ump;
   1439 	ip->i_fs = fs = ump->um_fs;
   1440 	ip->i_dev = dev;
   1441 	ip->i_number = ino;
   1442 	LIST_INIT(&ip->i_pcbufhd);
   1443 #ifdef QUOTA
   1444 	{
   1445 		int i;
   1446 
   1447 		for (i = 0; i < MAXQUOTAS; i++)
   1448 			ip->i_dquot[i] = NODQUOT;
   1449 	}
   1450 #endif
   1451 
   1452 	/*
   1453 	 * Put it onto its hash chain and lock it so that other requests for
   1454 	 * this inode will block if they arrive while we are sleeping waiting
   1455 	 * for old data structures to be purged or for the contents of the
   1456 	 * disk portion of this inode to be read.
   1457 	 */
   1458 
   1459 	ufs_ihashins(ip);
   1460 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1461 
   1462 	/* Read in the disk contents for the inode, copy into the inode. */
   1463 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1464 		      (int)fs->fs_bsize, NOCRED, &bp);
   1465 	if (error) {
   1466 
   1467 		/*
   1468 		 * The inode does not contain anything useful, so it would
   1469 		 * be misleading to leave it on its hash chain. With mode
   1470 		 * still zero, it will be unlinked and returned to the free
   1471 		 * list by vput().
   1472 		 */
   1473 
   1474 		vput(vp);
   1475 		brelse(bp);
   1476 		*vpp = NULL;
   1477 		return (error);
   1478 	}
   1479 	if (ip->i_ump->um_fstype == UFS1)
   1480 		ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
   1481 	else
   1482 		ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
   1483 	ffs_load_inode(bp, ip, fs, ino);
   1484 	if (DOINGSOFTDEP(vp))
   1485 		softdep_load_inodeblock(ip);
   1486 	else
   1487 		ip->i_ffs_effnlink = ip->i_nlink;
   1488 	brelse(bp);
   1489 
   1490 	/*
   1491 	 * Initialize the vnode from the inode, check for aliases.
   1492 	 * Note that the underlying vnode may have changed.
   1493 	 */
   1494 
   1495 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1496 
   1497 	/*
   1498 	 * Finish inode initialization now that aliasing has been resolved.
   1499 	 */
   1500 
   1501 	genfs_node_init(vp, &ffs_genfsops);
   1502 	ip->i_devvp = ump->um_devvp;
   1503 	VREF(ip->i_devvp);
   1504 
   1505 	/*
   1506 	 * Ensure that uid and gid are correct. This is a temporary
   1507 	 * fix until fsck has been changed to do the update.
   1508 	 */
   1509 
   1510 	if (fs->fs_old_inodefmt < FS_44INODEFMT) {		/* XXX */
   1511 		ip->i_uid = ip->i_ffs1_ouid;			/* XXX */
   1512 		ip->i_gid = ip->i_ffs1_ogid;			/* XXX */
   1513 	}							/* XXX */
   1514 	uvm_vnp_setsize(vp, ip->i_size);
   1515 	*vpp = vp;
   1516 	return (0);
   1517 }
   1518 
   1519 /*
   1520  * File handle to vnode
   1521  *
   1522  * Have to be really careful about stale file handles:
   1523  * - check that the inode number is valid
   1524  * - call ffs_vget() to get the locked inode
   1525  * - check for an unallocated inode (i_mode == 0)
   1526  * - check that the given client host has export rights and return
   1527  *   those rights via. exflagsp and credanonp
   1528  */
   1529 int
   1530 ffs_fhtovp(mp, fhp, vpp)
   1531 	struct mount *mp;
   1532 	struct fid *fhp;
   1533 	struct vnode **vpp;
   1534 {
   1535 	struct ufid *ufhp;
   1536 	struct fs *fs;
   1537 
   1538 	ufhp = (struct ufid *)fhp;
   1539 	fs = VFSTOUFS(mp)->um_fs;
   1540 	if (ufhp->ufid_ino < ROOTINO ||
   1541 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1542 		return (ESTALE);
   1543 	return (ufs_fhtovp(mp, ufhp, vpp));
   1544 }
   1545 
   1546 /*
   1547  * Vnode pointer to File handle
   1548  */
   1549 /* ARGSUSED */
   1550 int
   1551 ffs_vptofh(vp, fhp)
   1552 	struct vnode *vp;
   1553 	struct fid *fhp;
   1554 {
   1555 	struct inode *ip;
   1556 	struct ufid *ufhp;
   1557 
   1558 	ip = VTOI(vp);
   1559 	ufhp = (struct ufid *)fhp;
   1560 	ufhp->ufid_len = sizeof(struct ufid);
   1561 	ufhp->ufid_ino = ip->i_number;
   1562 	ufhp->ufid_gen = ip->i_gen;
   1563 	return (0);
   1564 }
   1565 
   1566 void
   1567 ffs_init()
   1568 {
   1569 	if (ffs_initcount++ > 0)
   1570 		return;
   1571 
   1572 #ifdef _LKM
   1573 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
   1574 		  "ffsinopl", &pool_allocator_nointr);
   1575 	pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
   1576 		  "dino1pl", &pool_allocator_nointr);
   1577 	pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
   1578 		  "dino2pl", &pool_allocator_nointr);
   1579 #endif
   1580 	softdep_initialize();
   1581 	ufs_init();
   1582 }
   1583 
   1584 void
   1585 ffs_reinit()
   1586 {
   1587 	softdep_reinitialize();
   1588 	ufs_reinit();
   1589 }
   1590 
   1591 void
   1592 ffs_done()
   1593 {
   1594 	if (--ffs_initcount > 0)
   1595 		return;
   1596 
   1597 	/* XXX softdep cleanup ? */
   1598 	ufs_done();
   1599 #ifdef _LKM
   1600 	pool_destroy(&ffs_dinode2_pool);
   1601 	pool_destroy(&ffs_dinode1_pool);
   1602 	pool_destroy(&ffs_inode_pool);
   1603 #endif
   1604 }
   1605 
   1606 SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
   1607 {
   1608 	extern int doasyncfree;
   1609 	extern int ffs_log_changeopt;
   1610 
   1611 	sysctl_createv(clog, 0, NULL, NULL,
   1612 		       CTLFLAG_PERMANENT,
   1613 		       CTLTYPE_NODE, "vfs", NULL,
   1614 		       NULL, 0, NULL, 0,
   1615 		       CTL_VFS, CTL_EOL);
   1616 	sysctl_createv(clog, 0, NULL, NULL,
   1617 		       CTLFLAG_PERMANENT,
   1618 		       CTLTYPE_NODE, "ffs",
   1619 		       SYSCTL_DESCR("Berkeley Fast File System"),
   1620 		       NULL, 0, NULL, 0,
   1621 		       CTL_VFS, 1, CTL_EOL);
   1622 
   1623 	/*
   1624 	 * @@@ should we even bother with these first three?
   1625 	 */
   1626 	sysctl_createv(clog, 0, NULL, NULL,
   1627 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1628 		       CTLTYPE_INT, "doclusterread", NULL,
   1629 		       sysctl_notavail, 0, NULL, 0,
   1630 		       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
   1631 	sysctl_createv(clog, 0, NULL, NULL,
   1632 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1633 		       CTLTYPE_INT, "doclusterwrite", NULL,
   1634 		       sysctl_notavail, 0, NULL, 0,
   1635 		       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
   1636 	sysctl_createv(clog, 0, NULL, NULL,
   1637 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1638 		       CTLTYPE_INT, "doreallocblks", NULL,
   1639 		       sysctl_notavail, 0, NULL, 0,
   1640 		       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
   1641 	sysctl_createv(clog, 0, NULL, NULL,
   1642 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1643 		       CTLTYPE_INT, "doasyncfree",
   1644 		       SYSCTL_DESCR("Release dirty blocks asynchronously"),
   1645 		       NULL, 0, &doasyncfree, 0,
   1646 		       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
   1647 	sysctl_createv(clog, 0, NULL, NULL,
   1648 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1649 		       CTLTYPE_INT, "log_changeopt",
   1650 		       SYSCTL_DESCR("Log changes in optimization strategy"),
   1651 		       NULL, 0, &ffs_log_changeopt, 0,
   1652 		       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
   1653 }
   1654 
   1655 /*
   1656  * Write a superblock and associated information back to disk.
   1657  */
   1658 int
   1659 ffs_sbupdate(mp, waitfor)
   1660 	struct ufsmount *mp;
   1661 	int waitfor;
   1662 {
   1663 	struct fs *fs = mp->um_fs;
   1664 	struct buf *bp;
   1665 	int error = 0;
   1666 	u_int32_t saveflag;
   1667 
   1668 	bp = getblk(mp->um_devvp,
   1669 	    fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
   1670 	    (int)fs->fs_sbsize, 0, 0);
   1671 	saveflag = fs->fs_flags & FS_INTERNAL;
   1672 	fs->fs_flags &= ~FS_INTERNAL;
   1673 
   1674 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1675 
   1676 	ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
   1677 #ifdef FFS_EI
   1678 	if (mp->um_flags & UFS_NEEDSWAP)
   1679 		ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
   1680 #endif
   1681 	fs->fs_flags |= saveflag;
   1682 
   1683 	if (waitfor == MNT_WAIT)
   1684 		error = bwrite(bp);
   1685 	else
   1686 		bawrite(bp);
   1687 	return (error);
   1688 }
   1689 
   1690 int
   1691 ffs_cgupdate(mp, waitfor)
   1692 	struct ufsmount *mp;
   1693 	int waitfor;
   1694 {
   1695 	struct fs *fs = mp->um_fs;
   1696 	struct buf *bp;
   1697 	int blks;
   1698 	void *space;
   1699 	int i, size, error = 0, allerror = 0;
   1700 
   1701 	allerror = ffs_sbupdate(mp, waitfor);
   1702 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1703 	space = fs->fs_csp;
   1704 	for (i = 0; i < blks; i += fs->fs_frag) {
   1705 		size = fs->fs_bsize;
   1706 		if (i + fs->fs_frag > blks)
   1707 			size = (blks - i) * fs->fs_fsize;
   1708 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1709 		    size, 0, 0);
   1710 #ifdef FFS_EI
   1711 		if (mp->um_flags & UFS_NEEDSWAP)
   1712 			ffs_csum_swap((struct csum*)space,
   1713 			    (struct csum*)bp->b_data, size);
   1714 		else
   1715 #endif
   1716 			memcpy(bp->b_data, space, (u_int)size);
   1717 		space = (char *)space + size;
   1718 		if (waitfor == MNT_WAIT)
   1719 			error = bwrite(bp);
   1720 		else
   1721 			bawrite(bp);
   1722 	}
   1723 	if (!allerror && error)
   1724 		allerror = error;
   1725 	return (allerror);
   1726 }
   1727