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