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