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