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