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