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