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