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