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