Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.98.4.2
      1 /*	$NetBSD: ffs_vfsops.c,v 1.98.4.2 2003/09/24 11:02:14 tron 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.98.4.2 2003/09/24 11:02:14 tron 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 	} else
    480 #endif
    481 		fs->fs_flags &= ~FS_SWAPPED;
    482 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
    483 	    newfs->fs_bsize < sizeof(struct fs)) {
    484 		brelse(bp);
    485 		free(newfs, M_UFSMNT);
    486 		return (EIO);		/* XXX needs translation */
    487 	}
    488 	/*
    489 	 * Copy pointer fields back into superblock before copying in	XXX
    490 	 * new superblock. These should really be in the ufsmount.	XXX
    491 	 * Note that important parameters (eg fs_ncg) are unchanged.
    492 	 */
    493 	newfs->fs_csp = fs->fs_csp;
    494 	newfs->fs_maxcluster = fs->fs_maxcluster;
    495 	newfs->fs_contigdirs = fs->fs_contigdirs;
    496 	newfs->fs_ronly = fs->fs_ronly;
    497 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    498 	if (fs->fs_sbsize < SBSIZE)
    499 		bp->b_flags |= B_INVAL;
    500 	brelse(bp);
    501 	free(newfs, M_UFSMNT);
    502 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    503 	ffs_oldfscompat(fs);
    504 		/* An old fsck may have zeroed these fields, so recheck them. */
    505 	if (fs->fs_avgfilesize <= 0)
    506 		fs->fs_avgfilesize = AVFILESIZ;
    507 	if (fs->fs_avgfpdir <= 0)
    508 		fs->fs_avgfpdir = AFPDIR;
    509 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    510 		fs->fs_pendingblocks = 0;
    511 		fs->fs_pendinginodes = 0;
    512 	}
    513 
    514 	ffs_statfs(mountp, &mountp->mnt_stat, p);
    515 	/*
    516 	 * Step 3: re-read summary information from disk.
    517 	 */
    518 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    519 	space = fs->fs_csp;
    520 	for (i = 0; i < blks; i += fs->fs_frag) {
    521 		size = fs->fs_bsize;
    522 		if (i + fs->fs_frag > blks)
    523 			size = (blks - i) * fs->fs_fsize;
    524 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    525 			      NOCRED, &bp);
    526 		if (error) {
    527 			brelse(bp);
    528 			return (error);
    529 		}
    530 #ifdef FFS_EI
    531 		if (UFS_FSNEEDSWAP(fs))
    532 			ffs_csum_swap((struct csum *)bp->b_data,
    533 			    (struct csum *)space, size);
    534 		else
    535 #endif
    536 			memcpy(space, bp->b_data, (size_t)size);
    537 		space = (char *)space + size;
    538 		brelse(bp);
    539 	}
    540 	if ((fs->fs_flags & FS_DOSOFTDEP))
    541 		softdep_mount(devvp, mountp, fs, cred);
    542 	/*
    543 	 * We no longer know anything about clusters per cylinder group.
    544 	 */
    545 	if (fs->fs_contigsumsize > 0) {
    546 		lp = fs->fs_maxcluster;
    547 		for (i = 0; i < fs->fs_ncg; i++)
    548 			*lp++ = fs->fs_contigsumsize;
    549 	}
    550 
    551 loop:
    552 	simple_lock(&mntvnode_slock);
    553 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    554 		if (vp->v_mount != mountp) {
    555 			simple_unlock(&mntvnode_slock);
    556 			goto loop;
    557 		}
    558 		nvp = vp->v_mntvnodes.le_next;
    559 		/*
    560 		 * Step 4: invalidate all inactive vnodes.
    561 		 */
    562 		if (vrecycle(vp, &mntvnode_slock, p))
    563 			goto loop;
    564 		/*
    565 		 * Step 5: invalidate all cached file data.
    566 		 */
    567 		simple_lock(&vp->v_interlock);
    568 		simple_unlock(&mntvnode_slock);
    569 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    570 			goto loop;
    571 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    572 			panic("ffs_reload: dirty2");
    573 		/*
    574 		 * Step 6: re-read inode data for all active vnodes.
    575 		 */
    576 		ip = VTOI(vp);
    577 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    578 			      (int)fs->fs_bsize, NOCRED, &bp);
    579 		if (error) {
    580 			brelse(bp);
    581 			vput(vp);
    582 			return (error);
    583 		}
    584 		cp = (caddr_t)bp->b_data +
    585 		    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
    586 #ifdef FFS_EI
    587 		if (UFS_FSNEEDSWAP(fs))
    588 			ffs_dinode_swap((struct dinode *)cp,
    589 			    &ip->i_din.ffs_din);
    590 		else
    591 #endif
    592 			memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
    593 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
    594 		brelse(bp);
    595 		vput(vp);
    596 		simple_lock(&mntvnode_slock);
    597 	}
    598 	simple_unlock(&mntvnode_slock);
    599 	return (0);
    600 }
    601 
    602 /*
    603  * Common code for mount and mountroot
    604  */
    605 int
    606 ffs_mountfs(devvp, mp, p)
    607 	struct vnode *devvp;
    608 	struct mount *mp;
    609 	struct proc *p;
    610 {
    611 	struct ufsmount *ump;
    612 	struct buf *bp;
    613 	struct fs *fs;
    614 	dev_t dev;
    615 	struct partinfo dpart;
    616 	void *space;
    617 	int blks;
    618 	int error, i, size, ronly;
    619 #ifdef FFS_EI
    620 	int needswap;
    621 #endif
    622 	int32_t *lp;
    623 	struct ucred *cred;
    624 	u_int64_t maxfilesize;					/* XXX */
    625 	u_int32_t sbsize;
    626 
    627 	dev = devvp->v_rdev;
    628 	cred = p ? p->p_ucred : NOCRED;
    629 	/*
    630 	 * Disallow multiple mounts of the same device.
    631 	 * Disallow mounting of a device that is currently in use
    632 	 * (except for root, which might share swap device for miniroot).
    633 	 * Flush out any old buffers remaining from a previous use.
    634 	 */
    635 	if ((error = vfs_mountedon(devvp)) != 0)
    636 		return (error);
    637 	if (vcount(devvp) > 1 && devvp != rootvp)
    638 		return (EBUSY);
    639 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    640 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    641 	VOP_UNLOCK(devvp, 0);
    642 	if (error)
    643 		return (error);
    644 
    645 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    646 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    647 	if (error)
    648 		return (error);
    649 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    650 		size = DEV_BSIZE;
    651 	else
    652 		size = dpart.disklab->d_secsize;
    653 
    654 	bp = NULL;
    655 	ump = NULL;
    656 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
    657 	if (error)
    658 		goto out;
    659 
    660 	fs = (struct fs*)bp->b_data;
    661 	if (fs->fs_magic == FS_MAGIC) {
    662 		sbsize = fs->fs_sbsize;
    663 #ifdef FFS_EI
    664 		needswap = 0;
    665 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    666 		sbsize = bswap32(fs->fs_sbsize);
    667 		needswap = 1;
    668 #endif
    669 	} else {
    670 		error = EINVAL;
    671 		goto out;
    672 	}
    673 	if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
    674 		error = EINVAL;
    675 		goto out;
    676 	}
    677 
    678 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    679 	memcpy(fs, bp->b_data, sbsize);
    680 #ifdef FFS_EI
    681 	if (needswap) {
    682 		ffs_sb_swap((struct fs*)bp->b_data, fs);
    683 		fs->fs_flags |= FS_SWAPPED;
    684 	} else
    685 #endif
    686 		fs->fs_flags &= ~FS_SWAPPED;
    687 	ffs_oldfscompat(fs);
    688 
    689 	if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
    690 		error = EINVAL;
    691 		goto out;
    692 	}
    693 	 /* make sure cylinder group summary area is a reasonable size. */
    694 	if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
    695 	    fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
    696 	    fs->fs_cssize >
    697 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
    698 		error = EINVAL;		/* XXX needs translation */
    699 		goto out2;
    700 	}
    701 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    702 		fs->fs_pendingblocks = 0;
    703 		fs->fs_pendinginodes = 0;
    704 	}
    705 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    706 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    707 		error = EROFS;		/* XXX what should be returned? */
    708 		goto out2;
    709 	}
    710 
    711 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    712 	memset((caddr_t)ump, 0, sizeof *ump);
    713 	ump->um_fs = fs;
    714 	if (fs->fs_sbsize < SBSIZE)
    715 		bp->b_flags |= B_INVAL;
    716 	brelse(bp);
    717 	bp = NULL;
    718 
    719 	/*
    720 	 * verify that we can access the last block in the fs
    721 	 * if we're mounting read/write.
    722 	 */
    723 
    724 	if (!ronly) {
    725 		error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
    726 		    cred, &bp);
    727 		if (bp->b_bcount != fs->fs_fsize)
    728 			error = EINVAL;
    729 		bp->b_flags |= B_INVAL;
    730 		if (error)
    731 			goto out;
    732 		brelse(bp);
    733 		bp = NULL;
    734 	}
    735 
    736 	fs->fs_ronly = ronly;
    737 	if (ronly == 0) {
    738 		fs->fs_clean <<= 1;
    739 		fs->fs_fmod = 1;
    740 	}
    741 	size = fs->fs_cssize;
    742 	blks = howmany(size, fs->fs_fsize);
    743 	if (fs->fs_contigsumsize > 0)
    744 		size += fs->fs_ncg * sizeof(int32_t);
    745 	size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    746 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    747 	fs->fs_csp = space;
    748 	for (i = 0; i < blks; i += fs->fs_frag) {
    749 		size = fs->fs_bsize;
    750 		if (i + fs->fs_frag > blks)
    751 			size = (blks - i) * fs->fs_fsize;
    752 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    753 			      cred, &bp);
    754 		if (error) {
    755 			free(fs->fs_csp, M_UFSMNT);
    756 			goto out2;
    757 		}
    758 #ifdef FFS_EI
    759 		if (needswap)
    760 			ffs_csum_swap((struct csum *)bp->b_data,
    761 				(struct csum *)space, size);
    762 		else
    763 #endif
    764 			memcpy(space, bp->b_data, (u_int)size);
    765 
    766 		space = (char *)space + size;
    767 		brelse(bp);
    768 		bp = NULL;
    769 	}
    770 	if (fs->fs_contigsumsize > 0) {
    771 		fs->fs_maxcluster = lp = space;
    772 		for (i = 0; i < fs->fs_ncg; i++)
    773 			*lp++ = fs->fs_contigsumsize;
    774 		space = lp;
    775 	}
    776 	size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
    777 	fs->fs_contigdirs = space;
    778 	space = (char *)space + size;
    779 	memset(fs->fs_contigdirs, 0, size);
    780 		/* Compatibility for old filesystems - XXX */
    781 	if (fs->fs_avgfilesize <= 0)
    782 		fs->fs_avgfilesize = AVFILESIZ;
    783 	if (fs->fs_avgfpdir <= 0)
    784 		fs->fs_avgfpdir = AFPDIR;
    785 	mp->mnt_data = (qaddr_t)ump;
    786 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    787 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    788 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    789 	mp->mnt_fs_bshift = fs->fs_bshift;
    790 	mp->mnt_dev_bshift = DEV_BSHIFT;	/* XXX */
    791 	mp->mnt_flag |= MNT_LOCAL;
    792 #ifdef FFS_EI
    793 	if (needswap)
    794 		ump->um_flags |= UFS_NEEDSWAP;
    795 #endif
    796 	ump->um_mountp = mp;
    797 	ump->um_dev = dev;
    798 	ump->um_devvp = devvp;
    799 	ump->um_nindir = fs->fs_nindir;
    800 	ump->um_lognindir = ffs(fs->fs_nindir) - 1;
    801 	ump->um_bptrtodb = fs->fs_fsbtodb;
    802 	ump->um_seqinc = fs->fs_frag;
    803 	for (i = 0; i < MAXQUOTAS; i++)
    804 		ump->um_quotas[i] = NULLVP;
    805 	devvp->v_specmountpoint = mp;
    806 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    807 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;	/* XXX */
    808 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    809 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    810 	if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
    811 		error = softdep_mount(devvp, mp, fs, cred);
    812 		if (error) {
    813 			free(fs->fs_csp, M_UFSMNT);
    814 			goto out;
    815 		}
    816 	}
    817 	return (0);
    818 out2:
    819 	free(fs, M_UFSMNT);
    820 out:
    821 	devvp->v_specmountpoint = NULL;
    822 	if (bp)
    823 		brelse(bp);
    824 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    825 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    826 	VOP_UNLOCK(devvp, 0);
    827 	if (ump) {
    828 		free(ump, M_UFSMNT);
    829 		mp->mnt_data = (qaddr_t)0;
    830 	}
    831 	return (error);
    832 }
    833 
    834 /*
    835  * Sanity checks for old file systems.
    836  *
    837  * XXX - goes away some day.
    838  */
    839 int
    840 ffs_oldfscompat(fs)
    841 	struct fs *fs;
    842 {
    843 	int i;
    844 
    845 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    846 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    847 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    848 		fs->fs_nrpos = 8;				/* XXX */
    849 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    850 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    851 								/* XXX */
    852 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    853 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    854 			sizepb *= NINDIR(fs);			/* XXX */
    855 			fs->fs_maxfilesize += sizepb;		/* XXX */
    856 		}						/* XXX */
    857 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    858 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    859 	}							/* XXX */
    860 	return (0);
    861 }
    862 
    863 /*
    864  * unmount system call
    865  */
    866 int
    867 ffs_unmount(mp, mntflags, p)
    868 	struct mount *mp;
    869 	int mntflags;
    870 	struct proc *p;
    871 {
    872 	struct ufsmount *ump;
    873 	struct fs *fs;
    874 	int error, flags, penderr;
    875 
    876 	penderr = 0;
    877 	flags = 0;
    878 	if (mntflags & MNT_FORCE)
    879 		flags |= FORCECLOSE;
    880 	if (mp->mnt_flag & MNT_SOFTDEP) {
    881 		if ((error = softdep_flushfiles(mp, flags, p)) != 0)
    882 			return (error);
    883 	} else {
    884 		if ((error = ffs_flushfiles(mp, flags, p)) != 0)
    885 			return (error);
    886 	}
    887 	ump = VFSTOUFS(mp);
    888 	fs = ump->um_fs;
    889 	if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
    890 		printf("%s: unmount pending error: blocks %d files %d\n",
    891 		    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
    892 		fs->fs_pendingblocks = 0;
    893 		fs->fs_pendinginodes = 0;
    894 		penderr = 1;
    895 	}
    896 	if (fs->fs_ronly == 0 &&
    897 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    898 	    fs->fs_clean & FS_WASCLEAN) {
    899 		/*
    900 		 * XXXX don't mark fs clean in the case of softdep
    901 		 * pending block errors, until they are fixed.
    902 		 */
    903 		if (penderr == 0) {
    904 			if (mp->mnt_flag & MNT_SOFTDEP)
    905 				fs->fs_flags &= ~FS_DOSOFTDEP;
    906 			fs->fs_clean = FS_ISCLEAN;
    907 		}
    908 		(void) ffs_sbupdate(ump, MNT_WAIT);
    909 	}
    910 	if (ump->um_devvp->v_type != VBAD)
    911 		ump->um_devvp->v_specmountpoint = NULL;
    912 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    913 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    914 		NOCRED, p);
    915 	vput(ump->um_devvp);
    916 	free(fs->fs_csp, M_UFSMNT);
    917 	free(fs, M_UFSMNT);
    918 	free(ump, M_UFSMNT);
    919 	mp->mnt_data = (qaddr_t)0;
    920 	mp->mnt_flag &= ~MNT_LOCAL;
    921 	return (error);
    922 }
    923 
    924 /*
    925  * Flush out all the files in a filesystem.
    926  */
    927 int
    928 ffs_flushfiles(mp, flags, p)
    929 	struct mount *mp;
    930 	int flags;
    931 	struct proc *p;
    932 {
    933 	extern int doforce;
    934 	struct ufsmount *ump;
    935 	int error;
    936 
    937 	if (!doforce)
    938 		flags &= ~FORCECLOSE;
    939 	ump = VFSTOUFS(mp);
    940 #ifdef QUOTA
    941 	if (mp->mnt_flag & MNT_QUOTA) {
    942 		int i;
    943 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    944 			return (error);
    945 		for (i = 0; i < MAXQUOTAS; i++) {
    946 			if (ump->um_quotas[i] == NULLVP)
    947 				continue;
    948 			quotaoff(p, mp, i);
    949 		}
    950 		/*
    951 		 * Here we fall through to vflush again to ensure
    952 		 * that we have gotten rid of all the system vnodes.
    953 		 */
    954 	}
    955 #endif
    956 	/*
    957 	 * Flush all the files.
    958 	 */
    959 	error = vflush(mp, NULLVP, flags);
    960 	if (error)
    961 		return (error);
    962 	/*
    963 	 * Flush filesystem metadata.
    964 	 */
    965 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    966 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
    967 	VOP_UNLOCK(ump->um_devvp, 0);
    968 	return (error);
    969 }
    970 
    971 /*
    972  * Get file system statistics.
    973  */
    974 int
    975 ffs_statfs(mp, sbp, p)
    976 	struct mount *mp;
    977 	struct statfs *sbp;
    978 	struct proc *p;
    979 {
    980 	struct ufsmount *ump;
    981 	struct fs *fs;
    982 
    983 	ump = VFSTOUFS(mp);
    984 	fs = ump->um_fs;
    985 	if (fs->fs_magic != FS_MAGIC)
    986 		panic("ffs_statfs");
    987 #ifdef COMPAT_09
    988 	sbp->f_type = 1;
    989 #else
    990 	sbp->f_type = 0;
    991 #endif
    992 	sbp->f_bsize = fs->fs_fsize;
    993 	sbp->f_iosize = fs->fs_bsize;
    994 	sbp->f_blocks = fs->fs_dsize;
    995 	sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
    996 		fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
    997 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
    998 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
    999 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
   1000 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
   1001 	sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
   1002 	if (sbp != &mp->mnt_stat) {
   1003 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
   1004 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
   1005 	}
   1006 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
   1007 	return (0);
   1008 }
   1009 
   1010 /*
   1011  * Go through the disk queues to initiate sandbagged IO;
   1012  * go through the inodes to write those that have been modified;
   1013  * initiate the writing of the super block if it has been modified.
   1014  *
   1015  * Note: we are always called with the filesystem marked `MPBUSY'.
   1016  */
   1017 int
   1018 ffs_sync(mp, waitfor, cred, p)
   1019 	struct mount *mp;
   1020 	int waitfor;
   1021 	struct ucred *cred;
   1022 	struct proc *p;
   1023 {
   1024 	struct vnode *vp, *nvp;
   1025 	struct inode *ip;
   1026 	struct ufsmount *ump = VFSTOUFS(mp);
   1027 	struct fs *fs;
   1028 	int error, allerror = 0;
   1029 
   1030 	fs = ump->um_fs;
   1031 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
   1032 		printf("fs = %s\n", fs->fs_fsmnt);
   1033 		panic("update: rofs mod");
   1034 	}
   1035 	/*
   1036 	 * Write back each (modified) inode.
   1037 	 */
   1038 	simple_lock(&mntvnode_slock);
   1039 loop:
   1040 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
   1041 		/*
   1042 		 * If the vnode that we are about to sync is no longer
   1043 		 * associated with this mount point, start over.
   1044 		 */
   1045 		if (vp->v_mount != mp)
   1046 			goto loop;
   1047 		simple_lock(&vp->v_interlock);
   1048 		nvp = LIST_NEXT(vp, v_mntvnodes);
   1049 		ip = VTOI(vp);
   1050 		if (vp->v_type == VNON ||
   1051 		    ((ip->i_flag &
   1052 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
   1053 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
   1054 		     vp->v_uobj.uo_npages == 0))
   1055 		{
   1056 			simple_unlock(&vp->v_interlock);
   1057 			continue;
   1058 		}
   1059 		simple_unlock(&mntvnode_slock);
   1060 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   1061 		if (error) {
   1062 			simple_lock(&mntvnode_slock);
   1063 			if (error == ENOENT)
   1064 				goto loop;
   1065 			continue;
   1066 		}
   1067 		if ((error = VOP_FSYNC(vp, cred,
   1068 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1069 			allerror = error;
   1070 		vput(vp);
   1071 		simple_lock(&mntvnode_slock);
   1072 	}
   1073 	simple_unlock(&mntvnode_slock);
   1074 	/*
   1075 	 * Force stale file system control information to be flushed.
   1076 	 */
   1077 	if (waitfor != MNT_LAZY) {
   1078 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
   1079 			waitfor = MNT_NOWAIT;
   1080 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1081 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
   1082 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
   1083 			allerror = error;
   1084 		VOP_UNLOCK(ump->um_devvp, 0);
   1085 	}
   1086 #ifdef QUOTA
   1087 	qsync(mp);
   1088 #endif
   1089 	/*
   1090 	 * Write back modified superblock.
   1091 	 */
   1092 	if (fs->fs_fmod != 0) {
   1093 		fs->fs_fmod = 0;
   1094 		fs->fs_time = time.tv_sec;
   1095 		if ((error = ffs_cgupdate(ump, waitfor)))
   1096 			allerror = error;
   1097 	}
   1098 	return (allerror);
   1099 }
   1100 
   1101 /*
   1102  * Look up a FFS dinode number to find its incore vnode, otherwise read it
   1103  * in from disk.  If it is in core, wait for the lock bit to clear, then
   1104  * return the inode locked.  Detection and handling of mount points must be
   1105  * done by the calling routine.
   1106  */
   1107 int
   1108 ffs_vget(mp, ino, vpp)
   1109 	struct mount *mp;
   1110 	ino_t ino;
   1111 	struct vnode **vpp;
   1112 {
   1113 	struct fs *fs;
   1114 	struct inode *ip;
   1115 	struct ufsmount *ump;
   1116 	struct buf *bp;
   1117 	struct vnode *vp;
   1118 	dev_t dev;
   1119 	int error;
   1120 	caddr_t cp;
   1121 
   1122 	ump = VFSTOUFS(mp);
   1123 	dev = ump->um_dev;
   1124 
   1125 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1126 		return (0);
   1127 
   1128 	/* Allocate a new vnode/inode. */
   1129 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1130 		*vpp = NULL;
   1131 		return (error);
   1132 	}
   1133 
   1134 	/*
   1135 	 * If someone beat us to it while sleeping in getnewvnode(),
   1136 	 * push back the freshly allocated vnode we don't need, and return.
   1137 	 */
   1138 
   1139 	do {
   1140 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1141 			ungetnewvnode(vp);
   1142 			return (0);
   1143 		}
   1144 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1145 
   1146 	/*
   1147 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1148 	 * XXX create another pool for MFS inodes?
   1149 	 */
   1150 
   1151 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1152 	memset(ip, 0, sizeof(struct inode));
   1153 	vp->v_data = ip;
   1154 	ip->i_vnode = vp;
   1155 	ip->i_fs = fs = ump->um_fs;
   1156 	ip->i_dev = dev;
   1157 	ip->i_number = ino;
   1158 	LIST_INIT(&ip->i_pcbufhd);
   1159 #ifdef QUOTA
   1160 	{
   1161 		int i;
   1162 
   1163 		for (i = 0; i < MAXQUOTAS; i++)
   1164 			ip->i_dquot[i] = NODQUOT;
   1165 	}
   1166 #endif
   1167 
   1168 	/*
   1169 	 * Put it onto its hash chain and lock it so that other requests for
   1170 	 * this inode will block if they arrive while we are sleeping waiting
   1171 	 * for old data structures to be purged or for the contents of the
   1172 	 * disk portion of this inode to be read.
   1173 	 */
   1174 
   1175 	ufs_ihashins(ip);
   1176 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1177 
   1178 	/* Read in the disk contents for the inode, copy into the inode. */
   1179 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1180 		      (int)fs->fs_bsize, NOCRED, &bp);
   1181 	if (error) {
   1182 
   1183 		/*
   1184 		 * The inode does not contain anything useful, so it would
   1185 		 * be misleading to leave it on its hash chain. With mode
   1186 		 * still zero, it will be unlinked and returned to the free
   1187 		 * list by vput().
   1188 		 */
   1189 
   1190 		vput(vp);
   1191 		brelse(bp);
   1192 		*vpp = NULL;
   1193 		return (error);
   1194 	}
   1195 	cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
   1196 #ifdef FFS_EI
   1197 	if (UFS_FSNEEDSWAP(fs))
   1198 		ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
   1199 	else
   1200 #endif
   1201 		memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
   1202 	if (DOINGSOFTDEP(vp))
   1203 		softdep_load_inodeblock(ip);
   1204 	else
   1205 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1206 	brelse(bp);
   1207 
   1208 	/*
   1209 	 * Initialize the vnode from the inode, check for aliases.
   1210 	 * Note that the underlying vnode may have changed.
   1211 	 */
   1212 
   1213 	ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1214 
   1215 	/*
   1216 	 * Finish inode initialization now that aliasing has been resolved.
   1217 	 */
   1218 
   1219 	genfs_node_init(vp, &ffs_genfsops);
   1220 	ip->i_devvp = ump->um_devvp;
   1221 	VREF(ip->i_devvp);
   1222 
   1223 	/*
   1224 	 * Ensure that uid and gid are correct. This is a temporary
   1225 	 * fix until fsck has been changed to do the update.
   1226 	 */
   1227 
   1228 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1229 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
   1230 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
   1231 	}							/* XXX */
   1232 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1233 	*vpp = vp;
   1234 	return (0);
   1235 }
   1236 
   1237 /*
   1238  * File handle to vnode
   1239  *
   1240  * Have to be really careful about stale file handles:
   1241  * - check that the inode number is valid
   1242  * - call ffs_vget() to get the locked inode
   1243  * - check for an unallocated inode (i_mode == 0)
   1244  * - check that the given client host has export rights and return
   1245  *   those rights via. exflagsp and credanonp
   1246  */
   1247 int
   1248 ffs_fhtovp(mp, fhp, vpp)
   1249 	struct mount *mp;
   1250 	struct fid *fhp;
   1251 	struct vnode **vpp;
   1252 {
   1253 	struct ufid *ufhp;
   1254 	struct fs *fs;
   1255 
   1256 	ufhp = (struct ufid *)fhp;
   1257 	fs = VFSTOUFS(mp)->um_fs;
   1258 	if (ufhp->ufid_ino < ROOTINO ||
   1259 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1260 		return (ESTALE);
   1261 	return (ufs_fhtovp(mp, ufhp, vpp));
   1262 }
   1263 
   1264 /*
   1265  * Vnode pointer to File handle
   1266  */
   1267 /* ARGSUSED */
   1268 int
   1269 ffs_vptofh(vp, fhp)
   1270 	struct vnode *vp;
   1271 	struct fid *fhp;
   1272 {
   1273 	struct inode *ip;
   1274 	struct ufid *ufhp;
   1275 
   1276 	ip = VTOI(vp);
   1277 	ufhp = (struct ufid *)fhp;
   1278 	ufhp->ufid_len = sizeof(struct ufid);
   1279 	ufhp->ufid_ino = ip->i_number;
   1280 	ufhp->ufid_gen = ip->i_ffs_gen;
   1281 	return (0);
   1282 }
   1283 
   1284 void
   1285 ffs_init()
   1286 {
   1287 	if (ffs_initcount++ > 0)
   1288 		return;
   1289 
   1290 	softdep_initialize();
   1291 	ufs_init();
   1292 
   1293 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1294 	    &pool_allocator_nointr);
   1295 }
   1296 
   1297 void
   1298 ffs_reinit()
   1299 {
   1300 	softdep_reinitialize();
   1301 	ufs_reinit();
   1302 }
   1303 
   1304 void
   1305 ffs_done()
   1306 {
   1307 	if (--ffs_initcount > 0)
   1308 		return;
   1309 
   1310 	/* XXX softdep cleanup ? */
   1311 	ufs_done();
   1312 	pool_destroy(&ffs_inode_pool);
   1313 }
   1314 
   1315 int
   1316 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1317 	int *name;
   1318 	u_int namelen;
   1319 	void *oldp;
   1320 	size_t *oldlenp;
   1321 	void *newp;
   1322 	size_t newlen;
   1323 	struct proc *p;
   1324 {
   1325 	extern int doasyncfree;
   1326 	extern int ffs_log_changeopt;
   1327 
   1328 	/* all sysctl names at this level are terminal */
   1329 	if (namelen != 1)
   1330 		return (ENOTDIR);		/* overloaded */
   1331 
   1332 	switch (name[0]) {
   1333 	case FFS_ASYNCFREE:
   1334 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1335 	case FFS_LOG_CHANGEOPT:
   1336 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1337 			&ffs_log_changeopt));
   1338 	default:
   1339 		return (EOPNOTSUPP);
   1340 	}
   1341 	/* NOTREACHED */
   1342 }
   1343 
   1344 /*
   1345  * Write a superblock and associated information back to disk.
   1346  */
   1347 int
   1348 ffs_sbupdate(mp, waitfor)
   1349 	struct ufsmount *mp;
   1350 	int waitfor;
   1351 {
   1352 	struct fs *fs = mp->um_fs;
   1353 	struct buf *bp;
   1354 	int i, error = 0;
   1355 	int32_t saved_nrpos = fs->fs_nrpos;
   1356 	int64_t saved_qbmask = fs->fs_qbmask;
   1357 	int64_t saved_qfmask = fs->fs_qfmask;
   1358 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1359 	u_int8_t saveflag;
   1360 
   1361 	/* Restore compatibility to old file systems.		   XXX */
   1362 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1363 		fs->fs_nrpos = -1;		/* XXX */
   1364 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1365 		int32_t *lp, tmp;				/* XXX */
   1366 								/* XXX */
   1367 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1368 		tmp = lp[4];					/* XXX */
   1369 		for (i = 4; i > 0; i--)				/* XXX */
   1370 			lp[i] = lp[i-1];			/* XXX */
   1371 		lp[0] = tmp;					/* XXX */
   1372 	}							/* XXX */
   1373 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1374 
   1375 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1376 	    (int)fs->fs_sbsize, 0, 0);
   1377 	saveflag = fs->fs_flags & FS_INTERNAL;
   1378 	fs->fs_flags &= ~FS_INTERNAL;
   1379 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1380 #ifdef FFS_EI
   1381 	if (mp->um_flags & UFS_NEEDSWAP)
   1382 		ffs_sb_swap(fs, (struct fs*)bp->b_data);
   1383 #endif
   1384 
   1385 	fs->fs_flags |= saveflag;
   1386 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1387 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1388 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1389 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1390 
   1391 	if (waitfor == MNT_WAIT)
   1392 		error = bwrite(bp);
   1393 	else
   1394 		bawrite(bp);
   1395 	return (error);
   1396 }
   1397 
   1398 int
   1399 ffs_cgupdate(mp, waitfor)
   1400 	struct ufsmount *mp;
   1401 	int waitfor;
   1402 {
   1403 	struct fs *fs = mp->um_fs;
   1404 	struct buf *bp;
   1405 	int blks;
   1406 	void *space;
   1407 	int i, size, error = 0, allerror = 0;
   1408 
   1409 	allerror = ffs_sbupdate(mp, waitfor);
   1410 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1411 	space = fs->fs_csp;
   1412 	for (i = 0; i < blks; i += fs->fs_frag) {
   1413 		size = fs->fs_bsize;
   1414 		if (i + fs->fs_frag > blks)
   1415 			size = (blks - i) * fs->fs_fsize;
   1416 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1417 		    size, 0, 0);
   1418 #ifdef FFS_EI
   1419 		if (mp->um_flags & UFS_NEEDSWAP)
   1420 			ffs_csum_swap((struct csum*)space,
   1421 			    (struct csum*)bp->b_data, size);
   1422 		else
   1423 #endif
   1424 			memcpy(bp->b_data, space, (u_int)size);
   1425 		space = (char *)space + size;
   1426 		if (waitfor == MNT_WAIT)
   1427 			error = bwrite(bp);
   1428 		else
   1429 			bawrite(bp);
   1430 	}
   1431 	if (!allerror && error)
   1432 		allerror = error;
   1433 	return (allerror);
   1434 }
   1435