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