Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.102
      1 /*	$NetBSD: ffs_vfsops.c,v 1.102 2002/09/21 18:14:50 christos 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_vfsops.c	8.31 (Berkeley) 5/20/95
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.102 2002/09/21 18:14:50 christos Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_ffs.h"
     43 #include "opt_quota.h"
     44 #include "opt_compat_netbsd.h"
     45 #include "opt_softdep.h"
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/namei.h>
     51 #include <sys/proc.h>
     52 #include <sys/kernel.h>
     53 #include <sys/vnode.h>
     54 #include <sys/socket.h>
     55 #include <sys/mount.h>
     56 #include <sys/buf.h>
     57 #include <sys/device.h>
     58 #include <sys/mbuf.h>
     59 #include <sys/file.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/errno.h>
     63 #include <sys/malloc.h>
     64 #include <sys/pool.h>
     65 #include <sys/lock.h>
     66 #include <sys/sysctl.h>
     67 #include <sys/conf.h>
     68 
     69 #include <miscfs/specfs/specdev.h>
     70 
     71 #include <ufs/ufs/quota.h>
     72 #include <ufs/ufs/ufsmount.h>
     73 #include <ufs/ufs/inode.h>
     74 #include <ufs/ufs/dir.h>
     75 #include <ufs/ufs/ufs_extern.h>
     76 #include <ufs/ufs/ufs_bswap.h>
     77 
     78 #include <ufs/ffs/fs.h>
     79 #include <ufs/ffs/ffs_extern.h>
     80 
     81 /* how many times ffs_init() was called */
     82 int ffs_initcount = 0;
     83 
     84 extern struct lock ufs_hashlock;
     85 
     86 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
     87 extern struct vnodeopv_desc ffs_specop_opv_desc;
     88 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
     89 
     90 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
     91 	&ffs_vnodeop_opv_desc,
     92 	&ffs_specop_opv_desc,
     93 	&ffs_fifoop_opv_desc,
     94 	NULL,
     95 };
     96 
     97 struct vfsops ffs_vfsops = {
     98 	MOUNT_FFS,
     99 	ffs_mount,
    100 	ufs_start,
    101 	ffs_unmount,
    102 	ufs_root,
    103 	ufs_quotactl,
    104 	ffs_statfs,
    105 	ffs_sync,
    106 	ffs_vget,
    107 	ffs_fhtovp,
    108 	ffs_vptofh,
    109 	ffs_init,
    110 	ffs_reinit,
    111 	ffs_done,
    112 	ffs_sysctl,
    113 	ffs_mountroot,
    114 	ufs_check_export,
    115 	ffs_vnodeopv_descs,
    116 };
    117 
    118 struct genfs_ops ffs_genfsops = {
    119 	ffs_gop_size,
    120 	ffs_gop_alloc,
    121 	genfs_gop_write,
    122 };
    123 
    124 struct pool ffs_inode_pool;
    125 
    126 /*
    127  * Called by main() when ffs is going to be mounted as root.
    128  */
    129 
    130 int
    131 ffs_mountroot()
    132 {
    133 	struct fs *fs;
    134 	struct mount *mp;
    135 	struct proc *p = curproc;	/* XXX */
    136 	struct ufsmount *ump;
    137 	int error;
    138 
    139 	if (root_device->dv_class != DV_DISK)
    140 		return (ENODEV);
    141 
    142 	/*
    143 	 * Get vnodes for rootdev.
    144 	 */
    145 	if (bdevvp(rootdev, &rootvp))
    146 		panic("ffs_mountroot: can't setup bdevvp's");
    147 
    148 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
    149 		vrele(rootvp);
    150 		return (error);
    151 	}
    152 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    153 		mp->mnt_op->vfs_refcount--;
    154 		vfs_unbusy(mp);
    155 		free(mp, M_MOUNT);
    156 		vrele(rootvp);
    157 		return (error);
    158 	}
    159 	simple_lock(&mountlist_slock);
    160 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    161 	simple_unlock(&mountlist_slock);
    162 	ump = VFSTOUFS(mp);
    163 	fs = ump->um_fs;
    164 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    165 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    166 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    167 	vfs_unbusy(mp);
    168 	inittodr(fs->fs_time);
    169 	return (0);
    170 }
    171 
    172 /*
    173  * VFS Operations.
    174  *
    175  * mount system call
    176  */
    177 int
    178 ffs_mount(mp, path, data, ndp, p)
    179 	struct mount *mp;
    180 	const char *path;
    181 	void *data;
    182 	struct nameidata *ndp;
    183 	struct proc *p;
    184 {
    185 	struct vnode *devvp;
    186 	struct ufs_args args;
    187 	struct ufsmount *ump = NULL;
    188 	struct fs *fs;
    189 	size_t size;
    190 	int error, flags, update;
    191 	mode_t accessmode;
    192 
    193 	if (mp->mnt_flag & MNT_GETARGS) {
    194 		ump = VFSTOUFS(mp);
    195 		if (ump == NULL)
    196 			return EIO;
    197 		args.fspec = NULL;
    198 		vfs_showexport(mp, &args.export, &ump->um_export);
    199 		return copyout(&args, data, sizeof(args));
    200 	}
    201 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    202 	if (error)
    203 		return (error);
    204 
    205 #if !defined(SOFTDEP)
    206 	mp->mnt_flag &= ~MNT_SOFTDEP;
    207 #endif
    208 
    209 	update = mp->mnt_flag & MNT_UPDATE;
    210 
    211 	/* Check arguments */
    212 	if (update) {
    213 		/* Use the extant mount */
    214 		ump = VFSTOUFS(mp);
    215 		devvp = ump->um_devvp;
    216 		if (args.fspec == NULL)
    217 			vref(devvp);
    218 	} else {
    219 		/* New mounts must have a filename for the device */
    220 		if (args.fspec == NULL)
    221 			return (EINVAL);
    222 	}
    223 
    224 	if (args.fspec != NULL) {
    225 		/*
    226 		 * Look up the name and verify that it's sane.
    227 		 */
    228 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    229 		if ((error = namei(ndp)) != 0)
    230 			return (error);
    231 		devvp = ndp->ni_vp;
    232 
    233 		if (!update) {
    234 			/*
    235 			 * Be sure this is a valid block device
    236 			 */
    237 			if (devvp->v_type != VBLK)
    238 				error = ENOTBLK;
    239 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    240 				error = ENXIO;
    241 		} else {
    242 			/*
    243 			 * Be sure we're still naming the same device
    244 			 * used for our initial mount
    245 			 */
    246 			if (devvp != ump->um_devvp)
    247 				error = EINVAL;
    248 		}
    249 	}
    250 
    251 	/*
    252 	 * If mount by non-root, then verify that user has necessary
    253 	 * permissions on the device.
    254 	 */
    255 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    256 		accessmode = VREAD;
    257 		if (update ?
    258 		    (mp->mnt_flag & MNT_WANTRDWR) != 0 :
    259 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    260 			accessmode |= VWRITE;
    261 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    262 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    263 		VOP_UNLOCK(devvp, 0);
    264 	}
    265 
    266 	if (error) {
    267 		vrele(devvp);
    268 		return (error);
    269 	}
    270 
    271 	if (!update) {
    272 		error = ffs_mountfs(devvp, mp, p);
    273 		if (error) {
    274 			vrele(devvp);
    275 			return (error);
    276 		}
    277 
    278 		ump = VFSTOUFS(mp);
    279 		fs = ump->um_fs;
    280 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    281 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    282 			printf("%s fs uses soft updates, "
    283 			    "ignoring async mode\n",
    284 			    fs->fs_fsmnt);
    285 			mp->mnt_flag &= ~MNT_ASYNC;
    286 		}
    287 	} else {
    288 		/*
    289 		 * Update the mount.
    290 		 */
    291 
    292 		/*
    293 		 * The initial mount got a reference on this
    294 		 * device, so drop the one obtained via
    295 		 * namei(), above.
    296 		 */
    297 		vrele(devvp);
    298 
    299 		fs = ump->um_fs;
    300 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    301 			/*
    302 			 * Changing from r/w to r/o
    303 			 */
    304 			flags = WRITECLOSE;
    305 			if (mp->mnt_flag & MNT_FORCE)
    306 				flags |= FORCECLOSE;
    307 			if (mp->mnt_flag & MNT_SOFTDEP)
    308 				error = softdep_flushfiles(mp, flags, p);
    309 			else
    310 				error = ffs_flushfiles(mp, flags, p);
    311 			if (fs->fs_pendingblocks != 0 ||
    312 			    fs->fs_pendinginodes != 0) {
    313 				printf("%s: update error: blocks %d files %d\n",
    314 				    fs->fs_fsmnt, fs->fs_pendingblocks,
    315 				    fs->fs_pendinginodes);
    316 				fs->fs_pendingblocks = 0;
    317 				fs->fs_pendinginodes = 0;
    318 			}
    319 			if (error == 0 &&
    320 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    321 			    fs->fs_clean & FS_WASCLEAN) {
    322 				if (mp->mnt_flag & MNT_SOFTDEP)
    323 					fs->fs_flags &= ~FS_DOSOFTDEP;
    324 				fs->fs_clean = FS_ISCLEAN;
    325 				(void) ffs_sbupdate(ump, MNT_WAIT);
    326 			}
    327 			if (error)
    328 				return (error);
    329 			fs->fs_ronly = 1;
    330 			fs->fs_fmod = 0;
    331 		}
    332 
    333 		/*
    334 		 * Flush soft dependencies if disabling it via an update
    335 		 * mount. This may leave some items to be processed,
    336 		 * so don't do this yet XXX.
    337 		 */
    338 		if ((fs->fs_flags & FS_DOSOFTDEP) &&
    339 		    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    340 #ifdef notyet
    341 			flags = WRITECLOSE;
    342 			if (mp->mnt_flag & MNT_FORCE)
    343 				flags |= FORCECLOSE;
    344 			error = softdep_flushfiles(mp, flags, p);
    345 			if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
    346 				fs->fs_flags &= ~FS_DOSOFTDEP;
    347 				(void) ffs_sbupdate(ump, MNT_WAIT);
    348 #elif defined(SOFTDEP)
    349 			mp->mnt_flag |= MNT_SOFTDEP;
    350 #endif
    351 		}
    352 
    353 		/*
    354 		 * When upgrading to a softdep mount, we must first flush
    355 		 * all vnodes. (not done yet -- see above)
    356 		 */
    357 		if (!(fs->fs_flags & FS_DOSOFTDEP) &&
    358 		    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
    359 #ifdef notyet
    360 			flags = WRITECLOSE;
    361 			if (mp->mnt_flag & MNT_FORCE)
    362 				flags |= FORCECLOSE;
    363 			error = ffs_flushfiles(mp, flags, p);
    364 #else
    365 			mp->mnt_flag &= ~MNT_SOFTDEP;
    366 #endif
    367 		}
    368 
    369 		if (mp->mnt_flag & MNT_RELOAD) {
    370 			error = ffs_reload(mp, p->p_ucred, p);
    371 			if (error)
    372 				return (error);
    373 		}
    374 
    375 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    376 			/*
    377 			 * Changing from read-only to read/write
    378 			 */
    379 			fs->fs_ronly = 0;
    380 			fs->fs_clean <<= 1;
    381 			fs->fs_fmod = 1;
    382 			if ((fs->fs_flags & FS_DOSOFTDEP)) {
    383 				error = softdep_mount(devvp, mp, fs,
    384 				    p->p_ucred);
    385 				if (error)
    386 					return (error);
    387 			}
    388 		}
    389 		if (args.fspec == 0) {
    390 			/*
    391 			 * Process export requests.
    392 			 */
    393 			return (vfs_export(mp, &ump->um_export, &args.export));
    394 		}
    395 		if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
    396 		    (MNT_SOFTDEP | MNT_ASYNC)) {
    397 			printf("%s fs uses soft updates, ignoring async mode\n",
    398 			    fs->fs_fsmnt);
    399 			mp->mnt_flag &= ~MNT_ASYNC;
    400 		}
    401 	}
    402 
    403 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    404 	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
    405 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
    406 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    407 	    &size);
    408 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    409 	if (mp->mnt_flag & MNT_SOFTDEP)
    410 		fs->fs_flags |= FS_DOSOFTDEP;
    411 	else
    412 		fs->fs_flags &= ~FS_DOSOFTDEP;
    413 	if (fs->fs_fmod != 0) {	/* XXX */
    414 		fs->fs_fmod = 0;
    415 		if (fs->fs_clean & FS_WASCLEAN)
    416 			fs->fs_time = time.tv_sec;
    417 		else {
    418 			printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
    419 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    420 			printf("%s: lost blocks %d files %d\n",
    421 			    mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
    422 			    fs->fs_pendinginodes);
    423 		}
    424 		(void) ffs_cgupdate(ump, MNT_WAIT);
    425 	}
    426 	return (0);
    427 }
    428 
    429 /*
    430  * Reload all incore data for a filesystem (used after running fsck on
    431  * the root filesystem and finding things to fix). The filesystem must
    432  * be mounted read-only.
    433  *
    434  * Things to do to update the mount:
    435  *	1) invalidate all cached meta-data.
    436  *	2) re-read superblock from disk.
    437  *	3) re-read summary information from disk.
    438  *	4) invalidate all inactive vnodes.
    439  *	5) invalidate all cached file data.
    440  *	6) re-read inode data for all active vnodes.
    441  */
    442 int
    443 ffs_reload(mountp, cred, p)
    444 	struct mount *mountp;
    445 	struct ucred *cred;
    446 	struct proc *p;
    447 {
    448 	struct vnode *vp, *nvp, *devvp;
    449 	struct inode *ip;
    450 	void *space;
    451 	struct buf *bp;
    452 	struct fs *fs, *newfs;
    453 	struct partinfo dpart;
    454 	int i, blks, size, error;
    455 	int32_t *lp;
    456 	caddr_t cp;
    457 
    458 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    459 		return (EINVAL);
    460 	/*
    461 	 * Step 1: invalidate all cached meta-data.
    462 	 */
    463 	devvp = VFSTOUFS(mountp)->um_devvp;
    464 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    465 	error = vinvalbuf(devvp, 0, cred, p, 0, 0);
    466 	VOP_UNLOCK(devvp, 0);
    467 	if (error)
    468 		panic("ffs_reload: dirty1");
    469 	/*
    470 	 * Step 2: re-read superblock from disk.
    471 	 */
    472 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
    473 		size = DEV_BSIZE;
    474 	else
    475 		size = dpart.disklab->d_secsize;
    476 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    477 	if (error) {
    478 		brelse(bp);
    479 		return (error);
    480 	}
    481 	fs = VFSTOUFS(mountp)->um_fs;
    482 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    483 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    484 #ifdef FFS_EI
    485 	if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP) {
    486 		ffs_sb_swap((struct fs*)bp->b_data, newfs);
    487 		fs->fs_flags |= FS_SWAPPED;
    488 	}
    489 #endif
    490 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
    491 	    newfs->fs_bsize < sizeof(struct fs)) {
    492 		brelse(bp);
    493 		free(newfs, M_UFSMNT);
    494 		return (EIO);		/* XXX needs translation */
    495 	}
    496 	/*
    497 	 * Copy pointer fields back into superblock before copying in	XXX
    498 	 * new superblock. These should really be in the ufsmount.	XXX
    499 	 * Note that important parameters (eg fs_ncg) are unchanged.
    500 	 */
    501 	newfs->fs_csp = fs->fs_csp;
    502 	newfs->fs_maxcluster = fs->fs_maxcluster;
    503 	newfs->fs_contigdirs = fs->fs_contigdirs;
    504 	newfs->fs_ronly = fs->fs_ronly;
    505 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    506 	if (fs->fs_sbsize < SBSIZE)
    507 		bp->b_flags |= B_INVAL;
    508 	brelse(bp);
    509 	free(newfs, M_UFSMNT);
    510 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    511 	ffs_oldfscompat(fs);
    512 		/* An old fsck may have zeroed these fields, so recheck them. */
    513 	if (fs->fs_avgfilesize <= 0)
    514 		fs->fs_avgfilesize = AVFILESIZ;
    515 	if (fs->fs_avgfpdir <= 0)
    516 		fs->fs_avgfpdir = AFPDIR;
    517 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    518 		fs->fs_pendingblocks = 0;
    519 		fs->fs_pendinginodes = 0;
    520 	}
    521 
    522 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    523 	/*
    524 	 * Step 3: re-read summary information from disk.
    525 	 */
    526 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    527 	space = fs->fs_csp;
    528 	for (i = 0; i < blks; i += fs->fs_frag) {
    529 		size = fs->fs_bsize;
    530 		if (i + fs->fs_frag > blks)
    531 			size = (blks - i) * fs->fs_fsize;
    532 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    533 			      NOCRED, &bp);
    534 		if (error) {
    535 			brelse(bp);
    536 			return (error);
    537 		}
    538 #ifdef FFS_EI
    539 		if (UFS_FSNEEDSWAP(fs))
    540 			ffs_csum_swap((struct csum *)bp->b_data,
    541 			    (struct csum *)space, size);
    542 		else
    543 #endif
    544 			memcpy(space, bp->b_data, (size_t)size);
    545 		space = (char *)space + size;
    546 		brelse(bp);
    547 	}
    548 	if ((fs->fs_flags & FS_DOSOFTDEP))
    549 		softdep_mount(devvp, mountp, fs, cred);
    550 	/*
    551 	 * We no longer know anything about clusters per cylinder group.
    552 	 */
    553 	if (fs->fs_contigsumsize > 0) {
    554 		lp = fs->fs_maxcluster;
    555 		for (i = 0; i < fs->fs_ncg; i++)
    556 			*lp++ = fs->fs_contigsumsize;
    557 	}
    558 
    559 loop:
    560 	simple_lock(&mntvnode_slock);
    561 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    562 		if (vp->v_mount != mountp) {
    563 			simple_unlock(&mntvnode_slock);
    564 			goto loop;
    565 		}
    566 		nvp = vp->v_mntvnodes.le_next;
    567 		/*
    568 		 * Step 4: invalidate all inactive vnodes.
    569 		 */
    570 		if (vrecycle(vp, &mntvnode_slock, p))
    571 			goto loop;
    572 		/*
    573 		 * Step 5: invalidate all cached file data.
    574 		 */
    575 		simple_lock(&vp->v_interlock);
    576 		simple_unlock(&mntvnode_slock);
    577 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    578 			goto loop;
    579 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    580 			panic("ffs_reload: dirty2");
    581 		/*
    582 		 * Step 6: re-read inode data for all active vnodes.
    583 		 */
    584 		ip = VTOI(vp);
    585 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    586 			      (int)fs->fs_bsize, NOCRED, &bp);
    587 		if (error) {
    588 			brelse(bp);
    589 			vput(vp);
    590 			return (error);
    591 		}
    592 		cp = (caddr_t)bp->b_data +
    593 		    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    594 #ifdef FFS_EI
    595 		if (UFS_FSNEEDSWAP(fs))
    596 			ffs_dinode_swap((struct dinode *)cp,
    597 			    &ip->i_din.ffs_din);
    598 		else
    599 #endif
    600 			memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
    601 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
    602 		brelse(bp);
    603 		vput(vp);
    604 		simple_lock(&mntvnode_slock);
    605 	}
    606 	simple_unlock(&mntvnode_slock);
    607 	return (0);
    608 }
    609 
    610 /*
    611  * Common code for mount and mountroot
    612  */
    613 int
    614 ffs_mountfs(devvp, mp, p)
    615 	struct vnode *devvp;
    616 	struct mount *mp;
    617 	struct proc *p;
    618 {
    619 	struct ufsmount *ump;
    620 	struct buf *bp;
    621 	struct fs *fs;
    622 	dev_t dev;
    623 	struct partinfo dpart;
    624 	void *space;
    625 	int blks;
    626 	int error, i, size, ronly;
    627 #ifdef FFS_EI
    628 	int needswap;
    629 #endif
    630 	int32_t *lp;
    631 	struct ucred *cred;
    632 	u_int64_t maxfilesize;					/* XXX */
    633 	u_int32_t sbsize;
    634 
    635 	dev = devvp->v_rdev;
    636 	cred = p ? p->p_ucred : NOCRED;
    637 	/*
    638 	 * Disallow multiple mounts of the same device.
    639 	 * Disallow mounting of a device that is currently in use
    640 	 * (except for root, which might share swap device for miniroot).
    641 	 * Flush out any old buffers remaining from a previous use.
    642 	 */
    643 	if ((error = vfs_mountedon(devvp)) != 0)
    644 		return (error);
    645 	if (vcount(devvp) > 1 && devvp != rootvp)
    646 		return (EBUSY);
    647 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    648 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    649 	VOP_UNLOCK(devvp, 0);
    650 	if (error)
    651 		return (error);
    652 
    653 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    654 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    655 	if (error)
    656 		return (error);
    657 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    658 		size = DEV_BSIZE;
    659 	else
    660 		size = dpart.disklab->d_secsize;
    661 
    662 	bp = NULL;
    663 	ump = NULL;
    664 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
    665 	if (error)
    666 		goto out;
    667 
    668 	fs = (struct fs*)bp->b_data;
    669 	if (fs->fs_magic == FS_MAGIC) {
    670 		sbsize = fs->fs_sbsize;
    671 #ifdef FFS_EI
    672 		needswap = 0;
    673 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    674 		sbsize = bswap32(fs->fs_sbsize);
    675 		needswap = 1;
    676 #endif
    677 	} else {
    678 		error = EINVAL;
    679 		goto out;
    680 	}
    681 	if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
    682 		error = EINVAL;
    683 		goto out;
    684 	}
    685 
    686 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    687 	memcpy(fs, bp->b_data, sbsize);
    688 #ifdef FFS_EI
    689 	if (needswap) {
    690 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    691 		fs->fs_flags |= FS_SWAPPED;
    692 	}
    693 #endif
    694 	ffs_oldfscompat(fs);
    695 
    696 	if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    697 		error = EINVAL;
    698 		goto out;
    699 	}
    700 	 /* make sure cylinder group summary area is a reasonable size. */
    701 	if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
    702 	    fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
    703 	    fs->fs_cssize >
    704 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
    705 		error = EINVAL;		/* XXX needs translation */
    706 		goto out2;
    707 	}
    708 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    709 		fs->fs_pendingblocks = 0;
    710 		fs->fs_pendinginodes = 0;
    711 	}
    712 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    713 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    714 		error = EROFS;		/* XXX what should be returned? */
    715 		goto out2;
    716 	}
    717 
    718 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    719 	memset((caddr_t)ump, 0, sizeof *ump);
    720 	ump->um_fs = fs;
    721 	if (fs->fs_sbsize < SBSIZE)
    722 		bp->b_flags |= B_INVAL;
    723 	brelse(bp);
    724 	bp = NULL;
    725 
    726 	/*
    727 	 * verify that we can access the last block in the fs
    728 	 * if we're mounting read/write.
    729 	 */
    730 
    731 	if (!ronly) {
    732 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    733 		    cred, &bp);
    734 		if (bp->b_bcount != fs->fs_fsize)
    735 			error = EINVAL;
    736 		bp->b_flags |= B_INVAL;
    737 		if (error)
    738 			goto out;
    739 		brelse(bp);
    740 		bp = NULL;
    741 	}
    742 
    743 	fs->fs_ronly = ronly;
    744 	if (ronly == 0) {
    745 		fs->fs_clean <<= 1;
    746 		fs->fs_fmod = 1;
    747 	}
    748 	size = fs->fs_cssize;
    749 	blks = howmany(size, fs->fs_fsize);
    750 	if (fs->fs_contigsumsize > 0)
    751 		size += fs->fs_ncg * sizeof(int32_t);
    752 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    753 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    754 	fs->fs_csp = space;
    755 	for (i = 0; i < blks; i += fs->fs_frag) {
    756 		size = fs->fs_bsize;
    757 		if (i + fs->fs_frag > blks)
    758 			size = (blks - i) * fs->fs_fsize;
    759 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    760 			      cred, &bp);
    761 		if (error) {
    762 			free(fs->fs_csp, M_UFSMNT);
    763 			goto out2;
    764 		}
    765 #ifdef FFS_EI
    766 		if (needswap)
    767 			ffs_csum_swap((struct csum *)bp->b_data,
    768 				(struct csum *)space, size);
    769 		else
    770 #endif
    771 			memcpy(space, bp->b_data, (u_int)size);
    772 
    773 		space = (char *)space + size;
    774 		brelse(bp);
    775 		bp = NULL;
    776 	}
    777 	if (fs->fs_contigsumsize > 0) {
    778 		fs->fs_maxcluster = lp = space;
    779 		for (i = 0; i < fs->fs_ncg; i++)
    780 			*lp++ = fs->fs_contigsumsize;
    781 		space = lp;
    782 	}
    783 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    784 	fs->fs_contigdirs = space;
    785 	space = (char *)space + size;
    786 	memset(fs->fs_contigdirs, 0, size);
    787 		/* Compatibility for old filesystems - XXX */
    788 	if (fs->fs_avgfilesize <= 0)
    789 		fs->fs_avgfilesize = AVFILESIZ;
    790 	if (fs->fs_avgfpdir <= 0)
    791 		fs->fs_avgfpdir = AFPDIR;
    792 	mp->mnt_data = ump;
    793 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    794 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    795 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    796 	mp->mnt_fs_bshift = fs->fs_bshift;
    797 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    798 	mp->mnt_flag |= MNT_LOCAL;
    799 #ifdef FFS_EI
    800 	if (needswap)
    801 		ump->um_flags |= UFS_NEEDSWAP;
    802 #endif
    803 	ump->um_mountp = mp;
    804 	ump->um_dev = dev;
    805 	ump->um_devvp = devvp;
    806 	ump->um_nindir = fs->fs_nindir;
    807 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    808 	ump->um_bptrtodb = fs->fs_fsbtodb;
    809 	ump->um_seqinc = fs->fs_frag;
    810 	for (i = 0; i < MAXQUOTAS; i++)
    811 		ump->um_quotas[i] = NULLVP;
    812 	devvp->v_specmountpoint = mp;
    813 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    814 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;	/* XXX */
    815 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    816 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    817 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    818 		error = softdep_mount(devvp, mp, fs, cred);
    819 		if (error) {
    820 			free(fs->fs_csp, M_UFSMNT);
    821 			goto out;
    822 		}
    823 	}
    824 	return (0);
    825 out2:
    826 	free(fs, M_UFSMNT);
    827 out:
    828 	devvp->v_specmountpoint = NULL;
    829 	if (bp)
    830 		brelse(bp);
    831 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    832 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    833 	VOP_UNLOCK(devvp, 0);
    834 	if (ump) {
    835 		free(ump, M_UFSMNT);
    836 		mp->mnt_data = NULL;
    837 	}
    838 	return (error);
    839 }
    840 
    841 /*
    842  * Sanity checks for old file systems.
    843  *
    844  * XXX - goes away some day.
    845  */
    846 int
    847 ffs_oldfscompat(fs)
    848 	struct fs *fs;
    849 {
    850 	int i;
    851 
    852 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    853 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    854 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    855 		fs->fs_nrpos = 8;				/* XXX */
    856 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    857 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    858 								/* XXX */
    859 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    860 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    861 			sizepb *= NINDIR(fs);			/* XXX */
    862 			fs->fs_maxfilesize += sizepb;		/* XXX */
    863 		}						/* XXX */
    864 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    865 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    866 	}							/* XXX */
    867 	return (0);
    868 }
    869 
    870 /*
    871  * unmount system call
    872  */
    873 int
    874 ffs_unmount(mp, mntflags, p)
    875 	struct mount *mp;
    876 	int mntflags;
    877 	struct proc *p;
    878 {
    879 	struct ufsmount *ump;
    880 	struct fs *fs;
    881 	int error, flags, penderr;
    882 
    883 	penderr = 0;
    884 	flags = 0;
    885 	if (mntflags & MNT_FORCE)
    886 		flags |= FORCECLOSE;
    887 	if (mp->mnt_flag & MNT_SOFTDEP) {
    888 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
    889 			return (error);
    890 	} else {
    891 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
    892 			return (error);
    893 	}
    894 	ump = VFSTOUFS(mp);
    895 	fs = ump->um_fs;
    896 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    897 		printf("%s: unmount pending error: blocks %d files %d\n",
    898 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
    899 		fs->fs_pendingblocks = 0;
    900 		fs->fs_pendinginodes = 0;
    901 		penderr = 1;
    902 	}
    903 	if (fs->fs_ronly == 0 &&
    904 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    905 	    fs->fs_clean & FS_WASCLEAN) {
    906 		/*
    907 		 * XXXX don't mark fs clean in the case of softdep
    908 		 * pending block errors, until they are fixed.
    909 		 */
    910 		if (penderr == 0) {
    911 			if (mp->mnt_flag & MNT_SOFTDEP)
    912 				fs->fs_flags &= ~FS_DOSOFTDEP;
    913 			fs->fs_clean = FS_ISCLEAN;
    914 		}
    915 		(void) ffs_sbupdate(ump, MNT_WAIT);
    916 	}
    917 	if (ump->um_devvp->v_type != VBAD)
    918 		ump->um_devvp->v_specmountpoint = NULL;
    919 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    920 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    921 		NOCRED, p);
    922 	vput(ump->um_devvp);
    923 	free(fs->fs_csp, M_UFSMNT);
    924 	free(fs, M_UFSMNT);
    925 	free(ump, M_UFSMNT);
    926 	mp->mnt_data = NULL;
    927 	mp->mnt_flag &= ~MNT_LOCAL;
    928 	return (error);
    929 }
    930 
    931 /*
    932  * Flush out all the files in a filesystem.
    933  */
    934 int
    935 ffs_flushfiles(mp, flags, p)
    936 	struct mount *mp;
    937 	int flags;
    938 	struct proc *p;
    939 {
    940 	extern int doforce;
    941 	struct ufsmount *ump;
    942 	int error;
    943 
    944 	if (!doforce)
    945 		flags &= ~FORCECLOSE;
    946 	ump = VFSTOUFS(mp);
    947 #ifdef QUOTA
    948 	if (mp->mnt_flag & MNT_QUOTA) {
    949 		int i;
    950 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    951 			return (error);
    952 		for (i = 0; i < MAXQUOTAS; i++) {
    953 			if (ump->um_quotas[i] == NULLVP)
    954 				continue;
    955 			quotaoff(p, mp, i);
    956 		}
    957 		/*
    958 		 * Here we fall through to vflush again to ensure
    959 		 * that we have gotten rid of all the system vnodes.
    960 		 */
    961 	}
    962 #endif
    963 	/*
    964 	 * Flush all the files.
    965 	 */
    966 	error = vflush(mp, NULLVP, flags);
    967 	if (error)
    968 		return (error);
    969 	/*
    970 	 * Flush filesystem metadata.
    971 	 */
    972 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    973 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
    974 	VOP_UNLOCK(ump->um_devvp, 0);
    975 	return (error);
    976 }
    977 
    978 /*
    979  * Get file system statistics.
    980  */
    981 int
    982 ffs_statfs(mp, sbp, p)
    983 	struct mount *mp;
    984 	struct statfs *sbp;
    985 	struct proc *p;
    986 {
    987 	struct ufsmount *ump;
    988 	struct fs *fs;
    989 
    990 	ump = VFSTOUFS(mp);
    991 	fs = ump->um_fs;
    992 	if (fs->fs_magic != FS_MAGIC)
    993 		panic("ffs_statfs");
    994 #ifdef COMPAT_09
    995 	sbp->f_type = 1;
    996 #else
    997 	sbp->f_type = 0;
    998 #endif
    999 	sbp->f_bsize = fs->fs_fsize;
   1000 	sbp->f_iosize = fs->fs_bsize;
   1001 	sbp->f_blocks = fs->fs_dsize;
   1002 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
   1003 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
   1004 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
   1005 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
   1006 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
   1007 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1008 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1009 	if (sbp != &mp->mnt_stat) {
   1010 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
   1011 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
   1012 	}
   1013 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
   1014 	return (0);
   1015 }
   1016 
   1017 /*
   1018  * Go through the disk queues to initiate sandbagged IO;
   1019  * go through the inodes to write those that have been modified;
   1020  * initiate the writing of the super block if it has been modified.
   1021  *
   1022  * Note: we are always called with the filesystem marked `MPBUSY'.
   1023  */
   1024 int
   1025 ffs_sync(mp, waitfor, cred, p)
   1026 	struct mount *mp;
   1027 	int waitfor;
   1028 	struct ucred *cred;
   1029 	struct proc *p;
   1030 {
   1031 	struct vnode *vp, *nvp;
   1032 	struct inode *ip;
   1033 	struct ufsmount *ump = VFSTOUFS(mp);
   1034 	struct fs *fs;
   1035 	int error, allerror = 0;
   1036 
   1037 	fs = ump->um_fs;
   1038 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1039 		printf("fs = %s\n", fs->fs_fsmnt);
   1040 		panic("update: rofs mod");
   1041 	}
   1042 	/*
   1043 	 * Write back each (modified) inode.
   1044 	 */
   1045 	simple_lock(&mntvnode_slock);
   1046 loop:
   1047 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1048 		/*
   1049 		 * If the vnode that we are about to sync is no longer
   1050 		 * associated with this mount point, start over.
   1051 		 */
   1052 		if (vp->v_mount != mp)
   1053 			goto loop;
   1054 		simple_lock(&vp->v_interlock);
   1055 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1056 		ip = VTOI(vp);
   1057 		if (vp->v_type == VNON ||
   1058 		    ((ip->i_flag &
   1059 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1060 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1061 		     vp->v_uobj.uo_npages == 0))
   1062 		{
   1063 			simple_unlock(&vp->v_interlock);
   1064 			continue;
   1065 		}
   1066 		simple_unlock(&mntvnode_slock);
   1067 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1068 		if (error) {
   1069 			simple_lock(&mntvnode_slock);
   1070 			if (error == ENOENT)
   1071 				goto loop;
   1072 			continue;
   1073 		}
   1074 		if ((error = VOP_FSYNC(vp, cred,
   1075 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1076 			allerror = error;
   1077 		vput(vp);
   1078 		simple_lock(&mntvnode_slock);
   1079 	}
   1080 	simple_unlock(&mntvnode_slock);
   1081 	/*
   1082 	 * Force stale file system control information to be flushed.
   1083 	 */
   1084 	if (waitfor != MNT_LAZY) {
   1085 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
   1086 			waitfor = MNT_NOWAIT;
   1087 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1088 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1089 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1090 			allerror = error;
   1091 		VOP_UNLOCK(ump->um_devvp, 0);
   1092 	}
   1093 #ifdef QUOTA
   1094 	qsync(mp);
   1095 #endif
   1096 	/*
   1097 	 * Write back modified superblock.
   1098 	 */
   1099 	if (fs->fs_fmod != 0) {
   1100 		fs->fs_fmod = 0;
   1101 		fs->fs_time = time.tv_sec;
   1102 		if ((error = ffs_cgupdate(ump, waitfor)))
   1103 			allerror = error;
   1104 	}
   1105 	return (allerror);
   1106 }
   1107 
   1108 /*
   1109  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1110  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1111  * return the inode locked.  Detection and handling of mount points must be
   1112  * done by the calling routine.
   1113  */
   1114 int
   1115 ffs_vget(mp, ino, vpp)
   1116 	struct mount *mp;
   1117 	ino_t ino;
   1118 	struct vnode **vpp;
   1119 {
   1120 	struct fs *fs;
   1121 	struct inode *ip;
   1122 	struct ufsmount *ump;
   1123 	struct buf *bp;
   1124 	struct vnode *vp;
   1125 	dev_t dev;
   1126 	int error;
   1127 	caddr_t cp;
   1128 
   1129 	ump = VFSTOUFS(mp);
   1130 	dev = ump->um_dev;
   1131 
   1132 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1133 		return (0);
   1134 
   1135 	/* Allocate a new vnode/inode. */
   1136 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1137 		*vpp = NULL;
   1138 		return (error);
   1139 	}
   1140 
   1141 	/*
   1142 	 * If someone beat us to it while sleeping in getnewvnode(),
   1143 	 * push back the freshly allocated vnode we don't need, and return.
   1144 	 */
   1145 
   1146 	do {
   1147 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1148 			ungetnewvnode(vp);
   1149 			return (0);
   1150 		}
   1151 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1152 
   1153 	/*
   1154 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1155 	 * XXX create another pool for MFS inodes?
   1156 	 */
   1157 
   1158 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1159 	memset(ip, 0, sizeof(struct inode));
   1160 	vp->v_data = ip;
   1161 	ip->i_vnode = vp;
   1162 	ip->i_fs = fs = ump->um_fs;
   1163 	ip->i_dev = dev;
   1164 	ip->i_number = ino;
   1165 	LIST_INIT(&ip->i_pcbufhd);
   1166 #ifdef QUOTA
   1167 	{
   1168 		int i;
   1169 
   1170 		for (i = 0; i < MAXQUOTAS; i++)
   1171 			ip->i_dquot[i] = NODQUOT;
   1172 	}
   1173 #endif
   1174 
   1175 	/*
   1176 	 * Put it onto its hash chain and lock it so that other requests for
   1177 	 * this inode will block if they arrive while we are sleeping waiting
   1178 	 * for old data structures to be purged or for the contents of the
   1179 	 * disk portion of this inode to be read.
   1180 	 */
   1181 
   1182 	ufs_ihashins(ip);
   1183 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1184 
   1185 	/* Read in the disk contents for the inode, copy into the inode. */
   1186 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1187 		      (int)fs->fs_bsize, NOCRED, &bp);
   1188 	if (error) {
   1189 
   1190 		/*
   1191 		 * The inode does not contain anything useful, so it would
   1192 		 * be misleading to leave it on its hash chain. With mode
   1193 		 * still zero, it will be unlinked and returned to the free
   1194 		 * list by vput().
   1195 		 */
   1196 
   1197 		vput(vp);
   1198 		brelse(bp);
   1199 		*vpp = NULL;
   1200 		return (error);
   1201 	}
   1202 	cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
   1203 #ifdef FFS_EI
   1204 	if (UFS_FSNEEDSWAP(fs))
   1205 		ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
   1206 	else
   1207 #endif
   1208 		memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
   1209 	if (DOINGSOFTDEP(vp))
   1210 		softdep_load_inodeblock(ip);
   1211 	else
   1212 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1213 	brelse(bp);
   1214 
   1215 	/*
   1216 	 * Initialize the vnode from the inode, check for aliases.
   1217 	 * Note that the underlying vnode may have changed.
   1218 	 */
   1219 
   1220 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1221 
   1222 	/*
   1223 	 * Finish inode initialization now that aliasing has been resolved.
   1224 	 */
   1225 
   1226 	genfs_node_init(vp, &ffs_genfsops);
   1227 	ip->i_devvp = ump->um_devvp;
   1228 	VREF(ip->i_devvp);
   1229 
   1230 	/*
   1231 	 * Ensure that uid and gid are correct. This is a temporary
   1232 	 * fix until fsck has been changed to do the update.
   1233 	 */
   1234 
   1235 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1236 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
   1237 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
   1238 	}							/* XXX */
   1239 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1240 	*vpp = vp;
   1241 	return (0);
   1242 }
   1243 
   1244 /*
   1245  * File handle to vnode
   1246  *
   1247  * Have to be really careful about stale file handles:
   1248  * - check that the inode number is valid
   1249  * - call ffs_vget() to get the locked inode
   1250  * - check for an unallocated inode (i_mode == 0)
   1251  * - check that the given client host has export rights and return
   1252  *   those rights via. exflagsp and credanonp
   1253  */
   1254 int
   1255 ffs_fhtovp(mp, fhp, vpp)
   1256 	struct mount *mp;
   1257 	struct fid *fhp;
   1258 	struct vnode **vpp;
   1259 {
   1260 	struct ufid *ufhp;
   1261 	struct fs *fs;
   1262 
   1263 	ufhp = (struct ufid *)fhp;
   1264 	fs = VFSTOUFS(mp)->um_fs;
   1265 	if (ufhp->ufid_ino < ROOTINO ||
   1266 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1267 		return (ESTALE);
   1268 	return (ufs_fhtovp(mp, ufhp, vpp));
   1269 }
   1270 
   1271 /*
   1272  * Vnode pointer to File handle
   1273  */
   1274 /* ARGSUSED */
   1275 int
   1276 ffs_vptofh(vp, fhp)
   1277 	struct vnode *vp;
   1278 	struct fid *fhp;
   1279 {
   1280 	struct inode *ip;
   1281 	struct ufid *ufhp;
   1282 
   1283 	ip = VTOI(vp);
   1284 	ufhp = (struct ufid *)fhp;
   1285 	ufhp->ufid_len = sizeof(struct ufid);
   1286 	ufhp->ufid_ino = ip->i_number;
   1287 	ufhp->ufid_gen = ip->i_ffs_gen;
   1288 	return (0);
   1289 }
   1290 
   1291 void
   1292 ffs_init()
   1293 {
   1294 	if (ffs_initcount++ > 0)
   1295 		return;
   1296 
   1297 	softdep_initialize();
   1298 	ufs_init();
   1299 
   1300 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1301 	    &pool_allocator_nointr);
   1302 }
   1303 
   1304 void
   1305 ffs_reinit()
   1306 {
   1307 	softdep_reinitialize();
   1308 	ufs_reinit();
   1309 }
   1310 
   1311 void
   1312 ffs_done()
   1313 {
   1314 	if (--ffs_initcount > 0)
   1315 		return;
   1316 
   1317 	/* XXX softdep cleanup ? */
   1318 	ufs_done();
   1319 	pool_destroy(&ffs_inode_pool);
   1320 }
   1321 
   1322 int
   1323 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1324 	int *name;
   1325 	u_int namelen;
   1326 	void *oldp;
   1327 	size_t *oldlenp;
   1328 	void *newp;
   1329 	size_t newlen;
   1330 	struct proc *p;
   1331 {
   1332 	extern int doasyncfree;
   1333 	extern int ffs_log_changeopt;
   1334 
   1335 	/* all sysctl names at this level are terminal */
   1336 	if (namelen != 1)
   1337 		return (ENOTDIR);		/* overloaded */
   1338 
   1339 	switch (name[0]) {
   1340 	case FFS_ASYNCFREE:
   1341 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1342 	case FFS_LOG_CHANGEOPT:
   1343 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1344 			&ffs_log_changeopt));
   1345 	default:
   1346 		return (EOPNOTSUPP);
   1347 	}
   1348 	/* NOTREACHED */
   1349 }
   1350 
   1351 /*
   1352  * Write a superblock and associated information back to disk.
   1353  */
   1354 int
   1355 ffs_sbupdate(mp, waitfor)
   1356 	struct ufsmount *mp;
   1357 	int waitfor;
   1358 {
   1359 	struct fs *fs = mp->um_fs;
   1360 	struct buf *bp;
   1361 	int i, error = 0;
   1362 	int32_t saved_nrpos = fs->fs_nrpos;
   1363 	int64_t saved_qbmask = fs->fs_qbmask;
   1364 	int64_t saved_qfmask = fs->fs_qfmask;
   1365 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1366 	u_int8_t saveflag;
   1367 
   1368 	/* Restore compatibility to old file systems.		   XXX */
   1369 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1370 		fs->fs_nrpos = -1;		/* XXX */
   1371 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1372 		int32_t *lp, tmp;				/* XXX */
   1373 								/* XXX */
   1374 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1375 		tmp = lp[4];					/* XXX */
   1376 		for (i = 4; i > 0; i--)				/* XXX */
   1377 			lp[i] = lp[i-1];			/* XXX */
   1378 		lp[0] = tmp;					/* XXX */
   1379 	}							/* XXX */
   1380 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1381 
   1382 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1383 	    (int)fs->fs_sbsize, 0, 0);
   1384 	saveflag = fs->fs_flags & FS_INTERNAL;
   1385 	fs->fs_flags &= ~FS_INTERNAL;
   1386 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1387 #ifdef FFS_EI
   1388 	if (mp->um_flags & UFS_NEEDSWAP)
   1389 		ffs_sb_swap(fs, (struct fs*)bp->b_data);
   1390 #endif
   1391 
   1392 	fs->fs_flags |= saveflag;
   1393 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1394 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1395 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1396 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1397 
   1398 	if (waitfor == MNT_WAIT)
   1399 		error = bwrite(bp);
   1400 	else
   1401 		bawrite(bp);
   1402 	return (error);
   1403 }
   1404 
   1405 int
   1406 ffs_cgupdate(mp, waitfor)
   1407 	struct ufsmount *mp;
   1408 	int waitfor;
   1409 {
   1410 	struct fs *fs = mp->um_fs;
   1411 	struct buf *bp;
   1412 	int blks;
   1413 	void *space;
   1414 	int i, size, error = 0, allerror = 0;
   1415 
   1416 	allerror = ffs_sbupdate(mp, waitfor);
   1417 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1418 	space = fs->fs_csp;
   1419 	for (i = 0; i < blks; i += fs->fs_frag) {
   1420 		size = fs->fs_bsize;
   1421 		if (i + fs->fs_frag > blks)
   1422 			size = (blks - i) * fs->fs_fsize;
   1423 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1424 		    size, 0, 0);
   1425 #ifdef FFS_EI
   1426 		if (mp->um_flags & UFS_NEEDSWAP)
   1427 			ffs_csum_swap((struct csum*)space,
   1428 			    (struct csum*)bp->b_data, size);
   1429 		else
   1430 #endif
   1431 			memcpy(bp->b_data, space, (u_int)size);
   1432 		space = (char *)space + size;
   1433 		if (waitfor == MNT_WAIT)
   1434 			error = bwrite(bp);
   1435 		else
   1436 			bawrite(bp);
   1437 	}
   1438 	if (!allerror && error)
   1439 		allerror = error;
   1440 	return (allerror);
   1441 }
   1442