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