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