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