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