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