Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.81
      1 /*	$NetBSD: ffs_vfsops.c,v 1.81 2001/05/30 11:57:18 mrg 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_OPT)
     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 const struct vnodeopv_desc * const 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 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    808 		NOCRED, p);
    809 	vput(ump->um_devvp);
    810 	free(fs->fs_csp[0], M_UFSMNT);
    811 	free(fs, M_UFSMNT);
    812 	free(ump, M_UFSMNT);
    813 	mp->mnt_data = (qaddr_t)0;
    814 	mp->mnt_flag &= ~MNT_LOCAL;
    815 	return (error);
    816 }
    817 
    818 /*
    819  * Flush out all the files in a filesystem.
    820  */
    821 int
    822 ffs_flushfiles(mp, flags, p)
    823 	struct mount *mp;
    824 	int flags;
    825 	struct proc *p;
    826 {
    827 	extern int doforce;
    828 	struct ufsmount *ump;
    829 	int error;
    830 
    831 	if (!doforce)
    832 		flags &= ~FORCECLOSE;
    833 	ump = VFSTOUFS(mp);
    834 #ifdef QUOTA
    835 	if (mp->mnt_flag & MNT_QUOTA) {
    836 		int i;
    837 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    838 			return (error);
    839 		for (i = 0; i < MAXQUOTAS; i++) {
    840 			if (ump->um_quotas[i] == NULLVP)
    841 				continue;
    842 			quotaoff(p, mp, i);
    843 		}
    844 		/*
    845 		 * Here we fall through to vflush again to ensure
    846 		 * that we have gotten rid of all the system vnodes.
    847 		 */
    848 	}
    849 #endif
    850 	/*
    851 	 * Flush all the files.
    852 	 */
    853 	error = vflush(mp, NULLVP, flags);
    854 	if (error)
    855 		return (error);
    856 	/*
    857 	 * Flush filesystem metadata.
    858 	 */
    859 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    860 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
    861 	VOP_UNLOCK(ump->um_devvp, 0);
    862 	return (error);
    863 }
    864 
    865 /*
    866  * Get file system statistics.
    867  */
    868 int
    869 ffs_statfs(mp, sbp, p)
    870 	struct mount *mp;
    871 	struct statfs *sbp;
    872 	struct proc *p;
    873 {
    874 	struct ufsmount *ump;
    875 	struct fs *fs;
    876 
    877 	ump = VFSTOUFS(mp);
    878 	fs = ump->um_fs;
    879 	if (fs->fs_magic != FS_MAGIC)
    880 		panic("ffs_statfs");
    881 #ifdef COMPAT_09
    882 	sbp->f_type = 1;
    883 #else
    884 	sbp->f_type = 0;
    885 #endif
    886 	sbp->f_bsize = fs->fs_fsize;
    887 	sbp->f_iosize = fs->fs_bsize;
    888 	sbp->f_blocks = fs->fs_dsize;
    889 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
    890 		fs->fs_cstotal.cs_nffree;
    891 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
    892 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
    893 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
    894 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
    895 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
    896 	if (sbp != &mp->mnt_stat) {
    897 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    898 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    899 	}
    900 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    901 	return (0);
    902 }
    903 
    904 /*
    905  * Go through the disk queues to initiate sandbagged IO;
    906  * go through the inodes to write those that have been modified;
    907  * initiate the writing of the super block if it has been modified.
    908  *
    909  * Note: we are always called with the filesystem marked `MPBUSY'.
    910  */
    911 int
    912 ffs_sync(mp, waitfor, cred, p)
    913 	struct mount *mp;
    914 	int waitfor;
    915 	struct ucred *cred;
    916 	struct proc *p;
    917 {
    918 	struct vnode *vp, *nvp;
    919 	struct inode *ip;
    920 	struct ufsmount *ump = VFSTOUFS(mp);
    921 	struct fs *fs;
    922 	int error, allerror = 0;
    923 
    924 	fs = ump->um_fs;
    925 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
    926 		printf("fs = %s\n", fs->fs_fsmnt);
    927 		panic("update: rofs mod");
    928 	}
    929 	/*
    930 	 * Write back each (modified) inode.
    931 	 */
    932 	simple_lock(&mntvnode_slock);
    933 loop:
    934 	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
    935 		/*
    936 		 * If the vnode that we are about to sync is no longer
    937 		 * associated with this mount point, start over.
    938 		 */
    939 		if (vp->v_mount != mp)
    940 			goto loop;
    941 		simple_lock(&vp->v_interlock);
    942 		nvp = LIST_NEXT(vp, v_mntvnodes);
    943 		ip = VTOI(vp);
    944 		if (vp->v_type == VNON ||
    945 		    ((ip->i_flag &
    946 		      (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
    947 		     LIST_EMPTY(&vp->v_dirtyblkhd) &&
    948 		     vp->v_uvm.u_obj.uo_npages == 0))
    949 		{
    950 			simple_unlock(&vp->v_interlock);
    951 			continue;
    952 		}
    953 		simple_unlock(&mntvnode_slock);
    954 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    955 		if (error) {
    956 			simple_lock(&mntvnode_slock);
    957 			if (error == ENOENT)
    958 				goto loop;
    959 			continue;
    960 		}
    961 		if ((error = VOP_FSYNC(vp, cred,
    962 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    963 			allerror = error;
    964 		vput(vp);
    965 		simple_lock(&mntvnode_slock);
    966 	}
    967 	simple_unlock(&mntvnode_slock);
    968 	/*
    969 	 * Force stale file system control information to be flushed.
    970 	 */
    971 	if (waitfor != MNT_LAZY) {
    972 		if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
    973 			waitfor = MNT_NOWAIT;
    974 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    975 		if ((error = VOP_FSYNC(ump->um_devvp, cred,
    976 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
    977 			allerror = error;
    978 		VOP_UNLOCK(ump->um_devvp, 0);
    979 	}
    980 #ifdef QUOTA
    981 	qsync(mp);
    982 #endif
    983 	/*
    984 	 * Write back modified superblock.
    985 	 */
    986 	if (fs->fs_fmod != 0) {
    987 		fs->fs_fmod = 0;
    988 		fs->fs_time = time.tv_sec;
    989 		if ((error = ffs_cgupdate(ump, waitfor)))
    990 			allerror = error;
    991 	}
    992 	return (allerror);
    993 }
    994 
    995 /*
    996  * Look up a FFS dinode number to find its incore vnode, otherwise read it
    997  * in from disk.  If it is in core, wait for the lock bit to clear, then
    998  * return the inode locked.  Detection and handling of mount points must be
    999  * done by the calling routine.
   1000  */
   1001 int
   1002 ffs_vget(mp, ino, vpp)
   1003 	struct mount *mp;
   1004 	ino_t ino;
   1005 	struct vnode **vpp;
   1006 {
   1007 	struct fs *fs;
   1008 	struct inode *ip;
   1009 	struct ufsmount *ump;
   1010 	struct buf *bp;
   1011 	struct vnode *vp;
   1012 	dev_t dev;
   1013 	int error;
   1014 	caddr_t cp;
   1015 
   1016 	ump = VFSTOUFS(mp);
   1017 	dev = ump->um_dev;
   1018 
   1019 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1020 		return (0);
   1021 
   1022 	/* Allocate a new vnode/inode. */
   1023 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
   1024 		*vpp = NULL;
   1025 		return (error);
   1026 	}
   1027 
   1028 	/*
   1029 	 * If someone beat us to it while sleeping in getnewvnode(),
   1030 	 * push back the freshly allocated vnode we don't need, and return.
   1031 	 */
   1032 	do {
   1033 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1034 			ungetnewvnode(vp);
   1035 			return (0);
   1036 		}
   1037 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1038 
   1039 	/*
   1040 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
   1041 	 * XXX create another pool for MFS inodes?
   1042 	 */
   1043 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
   1044 	memset((caddr_t)ip, 0, sizeof(struct inode));
   1045 	vp->v_data = ip;
   1046 	ip->i_vnode = vp;
   1047 	ip->i_fs = fs = ump->um_fs;
   1048 	ip->i_dev = dev;
   1049 	ip->i_number = ino;
   1050 	LIST_INIT(&ip->i_pcbufhd);
   1051 #ifdef QUOTA
   1052 	{
   1053 		int i;
   1054 
   1055 		for (i = 0; i < MAXQUOTAS; i++)
   1056 			ip->i_dquot[i] = NODQUOT;
   1057 	}
   1058 #endif
   1059 	/*
   1060 	 * Put it onto its hash chain and lock it so that other requests for
   1061 	 * this inode will block if they arrive while we are sleeping waiting
   1062 	 * for old data structures to be purged or for the contents of the
   1063 	 * disk portion of this inode to be read.
   1064 	 */
   1065 	ufs_ihashins(ip);
   1066 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1067 
   1068 	/* Read in the disk contents for the inode, copy into the inode. */
   1069 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
   1070 		      (int)fs->fs_bsize, NOCRED, &bp);
   1071 	if (error) {
   1072 		/*
   1073 		 * The inode does not contain anything useful, so it would
   1074 		 * be misleading to leave it on its hash chain. With mode
   1075 		 * still zero, it will be unlinked and returned to the free
   1076 		 * list by vput().
   1077 		 */
   1078 		vput(vp);
   1079 		brelse(bp);
   1080 		*vpp = NULL;
   1081 		return (error);
   1082 	}
   1083 	cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
   1084 #ifdef FFS_EI
   1085 	if (UFS_FSNEEDSWAP(fs))
   1086 		ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
   1087 	else
   1088 #endif
   1089 		memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
   1090 	if (DOINGSOFTDEP(vp))
   1091 		softdep_load_inodeblock(ip);
   1092 	else
   1093 		ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1094 	brelse(bp);
   1095 
   1096 	/*
   1097 	 * Initialize the vnode from the inode, check for aliases.
   1098 	 * Note that the underlying vnode may have changed.
   1099 	 */
   1100 	error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
   1101 	if (error) {
   1102 		vput(vp);
   1103 		*vpp = NULL;
   1104 		return (error);
   1105 	}
   1106 	/*
   1107 	 * Finish inode initialization now that aliasing has been resolved.
   1108 	 */
   1109 	ip->i_devvp = ump->um_devvp;
   1110 	VREF(ip->i_devvp);
   1111 	/*
   1112 	 * Ensure that uid and gid are correct. This is a temporary
   1113 	 * fix until fsck has been changed to do the update.
   1114 	 */
   1115 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1116 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
   1117 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
   1118 	}							/* XXX */
   1119 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1120 
   1121 	*vpp = vp;
   1122 	return (0);
   1123 }
   1124 
   1125 /*
   1126  * File handle to vnode
   1127  *
   1128  * Have to be really careful about stale file handles:
   1129  * - check that the inode number is valid
   1130  * - call ffs_vget() to get the locked inode
   1131  * - check for an unallocated inode (i_mode == 0)
   1132  * - check that the given client host has export rights and return
   1133  *   those rights via. exflagsp and credanonp
   1134  */
   1135 int
   1136 ffs_fhtovp(mp, fhp, vpp)
   1137 	struct mount *mp;
   1138 	struct fid *fhp;
   1139 	struct vnode **vpp;
   1140 {
   1141 	struct ufid *ufhp;
   1142 	struct fs *fs;
   1143 
   1144 	ufhp = (struct ufid *)fhp;
   1145 	fs = VFSTOUFS(mp)->um_fs;
   1146 	if (ufhp->ufid_ino < ROOTINO ||
   1147 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
   1148 		return (ESTALE);
   1149 	return (ufs_fhtovp(mp, ufhp, vpp));
   1150 }
   1151 
   1152 /*
   1153  * Vnode pointer to File handle
   1154  */
   1155 /* ARGSUSED */
   1156 int
   1157 ffs_vptofh(vp, fhp)
   1158 	struct vnode *vp;
   1159 	struct fid *fhp;
   1160 {
   1161 	struct inode *ip;
   1162 	struct ufid *ufhp;
   1163 
   1164 	ip = VTOI(vp);
   1165 	ufhp = (struct ufid *)fhp;
   1166 	ufhp->ufid_len = sizeof(struct ufid);
   1167 	ufhp->ufid_ino = ip->i_number;
   1168 	ufhp->ufid_gen = ip->i_ffs_gen;
   1169 	return (0);
   1170 }
   1171 
   1172 void
   1173 ffs_init()
   1174 {
   1175 	if (ffs_initcount++ > 0)
   1176 		return;
   1177 
   1178 	softdep_initialize();
   1179 	ufs_init();
   1180 
   1181 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1182 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
   1183 }
   1184 
   1185 void
   1186 ffs_done()
   1187 {
   1188 	if (--ffs_initcount > 0)
   1189 		return;
   1190 
   1191 	/* XXX softdep cleanup ? */
   1192 	ufs_done();
   1193 	pool_destroy(&ffs_inode_pool);
   1194 }
   1195 
   1196 int
   1197 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1198 	int *name;
   1199 	u_int namelen;
   1200 	void *oldp;
   1201 	size_t *oldlenp;
   1202 	void *newp;
   1203 	size_t newlen;
   1204 	struct proc *p;
   1205 {
   1206 	extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
   1207 	extern int ffs_log_changeopt;
   1208 
   1209 	/* all sysctl names at this level are terminal */
   1210 	if (namelen != 1)
   1211 		return (ENOTDIR);		/* overloaded */
   1212 
   1213 	switch (name[0]) {
   1214 	case FFS_CLUSTERREAD:
   1215 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1216 		    &doclusterread));
   1217 	case FFS_CLUSTERWRITE:
   1218 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1219 		    &doclusterwrite));
   1220 	case FFS_REALLOCBLKS:
   1221 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1222 		    &doreallocblks));
   1223 	case FFS_ASYNCFREE:
   1224 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1225 	case FFS_LOG_CHANGEOPT:
   1226 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1227 			&ffs_log_changeopt));
   1228 	default:
   1229 		return (EOPNOTSUPP);
   1230 	}
   1231 	/* NOTREACHED */
   1232 }
   1233 
   1234 /*
   1235  * Write a superblock and associated information back to disk.
   1236  */
   1237 int
   1238 ffs_sbupdate(mp, waitfor)
   1239 	struct ufsmount *mp;
   1240 	int waitfor;
   1241 {
   1242 	struct fs *fs = mp->um_fs;
   1243 	struct buf *bp;
   1244 	int i, error = 0;
   1245 	int32_t saved_nrpos = fs->fs_nrpos;
   1246 	int64_t saved_qbmask = fs->fs_qbmask;
   1247 	int64_t saved_qfmask = fs->fs_qfmask;
   1248 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1249 	u_int8_t saveflag;
   1250 
   1251 	/* Restore compatibility to old file systems.		   XXX */
   1252 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1253 		fs->fs_nrpos = -1;		/* XXX */
   1254 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1255 		int32_t *lp, tmp;				/* XXX */
   1256 								/* XXX */
   1257 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1258 		tmp = lp[4];					/* XXX */
   1259 		for (i = 4; i > 0; i--)				/* XXX */
   1260 			lp[i] = lp[i-1];			/* XXX */
   1261 		lp[0] = tmp;					/* XXX */
   1262 	}							/* XXX */
   1263 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1264 
   1265 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1266 	    (int)fs->fs_sbsize, 0, 0);
   1267 	saveflag = fs->fs_flags & FS_INTERNAL;
   1268 	fs->fs_flags &= ~FS_INTERNAL;
   1269 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1270 #ifdef FFS_EI
   1271 	if (mp->um_flags & UFS_NEEDSWAP)
   1272 		ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
   1273 #endif
   1274 
   1275 	fs->fs_flags |= saveflag;
   1276 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1277 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1278 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1279 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1280 
   1281 	if (waitfor == MNT_WAIT)
   1282 		error = bwrite(bp);
   1283 	else
   1284 		bawrite(bp);
   1285 	return (error);
   1286 }
   1287 
   1288 int
   1289 ffs_cgupdate(mp, waitfor)
   1290 	struct ufsmount *mp;
   1291 	int waitfor;
   1292 {
   1293 	struct fs *fs = mp->um_fs;
   1294 	struct buf *bp;
   1295 	int blks;
   1296 	caddr_t space;
   1297 	int i, size, error = 0, allerror = 0;
   1298 
   1299 	allerror = ffs_sbupdate(mp, waitfor);
   1300 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1301 	space = (caddr_t)fs->fs_csp[0];
   1302 	for (i = 0; i < blks; i += fs->fs_frag) {
   1303 		size = fs->fs_bsize;
   1304 		if (i + fs->fs_frag > blks)
   1305 			size = (blks - i) * fs->fs_fsize;
   1306 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1307 		    size, 0, 0);
   1308 #ifdef FFS_EI
   1309 		if (mp->um_flags & UFS_NEEDSWAP)
   1310 			ffs_csum_swap((struct csum*)space,
   1311 			    (struct csum*)bp->b_data, size);
   1312 		else
   1313 #endif
   1314 			memcpy(bp->b_data, space, (u_int)size);
   1315 		space += size;
   1316 		if (waitfor == MNT_WAIT)
   1317 			error = bwrite(bp);
   1318 		else
   1319 			bawrite(bp);
   1320 	}
   1321 	if (!allerror && error)
   1322 		allerror = error;
   1323 	return (allerror);
   1324 }
   1325