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