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