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