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