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