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