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