Home | History | Annotate | Line # | Download | only in ffs
ffs_vfsops.c revision 1.43
      1 /*	$NetBSD: ffs_vfsops.c,v 1.43 1998/09/01 03:11:08 thorpej 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_quota.h"
     40 #include "opt_compat_netbsd.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/namei.h>
     46 #include <sys/proc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/vnode.h>
     49 #include <sys/socket.h>
     50 #include <sys/mount.h>
     51 #include <sys/buf.h>
     52 #include <sys/device.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/file.h>
     55 #include <sys/disklabel.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/malloc.h>
     59 #include <sys/pool.h>
     60 #include <sys/lock.h>
     61 #include <vm/vm.h>
     62 #include <sys/sysctl.h>
     63 
     64 #include <miscfs/specfs/specdev.h>
     65 
     66 #include <ufs/ufs/quota.h>
     67 #include <ufs/ufs/ufsmount.h>
     68 #include <ufs/ufs/inode.h>
     69 #include <ufs/ufs/dir.h>
     70 #include <ufs/ufs/ufs_extern.h>
     71 #include <ufs/ufs/ufs_bswap.h>
     72 
     73 #include <ufs/ffs/fs.h>
     74 #include <ufs/ffs/ffs_extern.h>
     75 
     76 extern struct lock ufs_hashlock;
     77 
     78 int ffs_sbupdate __P((struct ufsmount *, int));
     79 
     80 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
     81 extern struct vnodeopv_desc ffs_specop_opv_desc;
     82 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
     83 
     84 struct vnodeopv_desc *ffs_vnodeopv_descs[] = {
     85 	&ffs_vnodeop_opv_desc,
     86 	&ffs_specop_opv_desc,
     87 	&ffs_fifoop_opv_desc,
     88 	NULL,
     89 };
     90 
     91 struct vfsops ffs_vfsops = {
     92 	MOUNT_FFS,
     93 	ffs_mount,
     94 	ufs_start,
     95 	ffs_unmount,
     96 	ufs_root,
     97 	ufs_quotactl,
     98 	ffs_statfs,
     99 	ffs_sync,
    100 	ffs_vget,
    101 	ffs_fhtovp,
    102 	ffs_vptofh,
    103 	ffs_init,
    104 	ffs_sysctl,
    105 	ffs_mountroot,
    106 	ffs_vnodeopv_descs,
    107 };
    108 
    109 struct pool ffs_inode_pool;
    110 
    111 /*
    112  * Called by main() when ffs is going to be mounted as root.
    113  */
    114 
    115 int
    116 ffs_mountroot()
    117 {
    118 	extern struct vnode *rootvp;
    119 	struct fs *fs;
    120 	struct mount *mp;
    121 	struct proc *p = curproc;	/* XXX */
    122 	struct ufsmount *ump;
    123 	int error;
    124 
    125 	if (root_device->dv_class != DV_DISK)
    126 		return (ENODEV);
    127 
    128 	/*
    129 	 * Get vnodes for rootdev.
    130 	 */
    131 	if (bdevvp(rootdev, &rootvp))
    132 		panic("ffs_mountroot: can't setup bdevvp's");
    133 
    134 	if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp)))
    135 		return (error);
    136 	if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
    137 		mp->mnt_op->vfs_refcount--;
    138 		vfs_unbusy(mp);
    139 		free(mp, M_MOUNT);
    140 		return (error);
    141 	}
    142 	simple_lock(&mountlist_slock);
    143 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    144 	simple_unlock(&mountlist_slock);
    145 	ump = VFSTOUFS(mp);
    146 	fs = ump->um_fs;
    147 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
    148 	(void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
    149 	(void)ffs_statfs(mp, &mp->mnt_stat, p);
    150 	vfs_unbusy(mp);
    151 	inittodr(fs->fs_time);
    152 	return (0);
    153 }
    154 
    155 /*
    156  * VFS Operations.
    157  *
    158  * mount system call
    159  */
    160 int
    161 ffs_mount(mp, path, data, ndp, p)
    162 	register struct mount *mp;
    163 	const char *path;
    164 	void *data;
    165 	struct nameidata *ndp;
    166 	struct proc *p;
    167 {
    168 	struct vnode *devvp;
    169 	struct ufs_args args;
    170 	struct ufsmount *ump = NULL;
    171 	register struct fs *fs;
    172 	size_t size;
    173 	int error, flags;
    174 	mode_t accessmode;
    175 
    176 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    177 	if (error)
    178 		return (error);
    179 	/*
    180 	 * If updating, check whether changing from read-only to
    181 	 * read/write; if there is no device name, that's all we do.
    182 	 */
    183 	if (mp->mnt_flag & MNT_UPDATE) {
    184 		ump = VFSTOUFS(mp);
    185 		fs = ump->um_fs;
    186 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    187 			flags = WRITECLOSE;
    188 			if (mp->mnt_flag & MNT_FORCE)
    189 				flags |= FORCECLOSE;
    190 			error = ffs_flushfiles(mp, flags, p);
    191 			if (error == 0 &&
    192 			    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    193 			    fs->fs_clean & FS_WASCLEAN) {
    194 				fs->fs_clean = FS_ISCLEAN;
    195 				(void) ffs_sbupdate(ump, MNT_WAIT);
    196 			}
    197 			if (error)
    198 				return (error);
    199 			fs->fs_ronly = 1;
    200 		}
    201 		if (mp->mnt_flag & MNT_RELOAD) {
    202 			error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
    203 			if (error)
    204 				return (error);
    205 		}
    206 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    207 			/*
    208 			 * If upgrade to read-write by non-root, then verify
    209 			 * that user has necessary permissions on the device.
    210 			 */
    211 			if (p->p_ucred->cr_uid != 0) {
    212 				devvp = ump->um_devvp;
    213 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    214 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
    215 						   p->p_ucred, p);
    216 				VOP_UNLOCK(devvp, 0);
    217 				if (error)
    218 					return (error);
    219 			}
    220 			fs->fs_ronly = 0;
    221 			fs->fs_clean <<= 1;
    222 			fs->fs_fmod = 1;
    223 		}
    224 		if (args.fspec == 0) {
    225 			/*
    226 			 * Process export requests.
    227 			 */
    228 			return (vfs_export(mp, &ump->um_export, &args.export));
    229 		}
    230 	}
    231 	/*
    232 	 * Not an update, or updating the name: look up the name
    233 	 * and verify that it refers to a sensible block device.
    234 	 */
    235 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    236 	if ((error = namei(ndp)) != 0)
    237 		return (error);
    238 	devvp = ndp->ni_vp;
    239 
    240 	if (devvp->v_type != VBLK) {
    241 		vrele(devvp);
    242 		return (ENOTBLK);
    243 	}
    244 	if (major(devvp->v_rdev) >= nblkdev) {
    245 		vrele(devvp);
    246 		return (ENXIO);
    247 	}
    248 	/*
    249 	 * If mount by non-root, then verify that user has necessary
    250 	 * permissions on the device.
    251 	 */
    252 	if (p->p_ucred->cr_uid != 0) {
    253 		accessmode = VREAD;
    254 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    255 			accessmode |= VWRITE;
    256 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    257 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    258 		VOP_UNLOCK(devvp, 0);
    259 		if (error) {
    260 			vrele(devvp);
    261 			return (error);
    262 		}
    263 	}
    264 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    265 		error = ffs_mountfs(devvp, mp, p);
    266 	else {
    267 		if (devvp != ump->um_devvp)
    268 			error = EINVAL;	/* needs translation */
    269 		else
    270 			vrele(devvp);
    271 	}
    272 	if (error) {
    273 		vrele(devvp);
    274 		return (error);
    275 	}
    276 	ump = VFSTOUFS(mp);
    277 	fs = ump->um_fs;
    278 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
    279 	memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
    280 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
    281 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    282 	    &size);
    283 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    284 	if (fs->fs_fmod != 0) {	/* XXX */
    285 		fs->fs_fmod = 0;
    286 		if (fs->fs_clean & FS_WASCLEAN)
    287 			fs->fs_time = time.tv_sec;
    288 		else
    289 			printf("%s: file system not clean (fs_flags=%x); please fsck(8)\n",
    290 			    mp->mnt_stat.f_mntfromname, fs->fs_clean);
    291 		(void) ffs_cgupdate(ump, MNT_WAIT);
    292 	}
    293 	return (0);
    294 }
    295 
    296 /*
    297  * Reload all incore data for a filesystem (used after running fsck on
    298  * the root filesystem and finding things to fix). The filesystem must
    299  * be mounted read-only.
    300  *
    301  * Things to do to update the mount:
    302  *	1) invalidate all cached meta-data.
    303  *	2) re-read superblock from disk.
    304  *	3) re-read summary information from disk.
    305  *	4) invalidate all inactive vnodes.
    306  *	5) invalidate all cached file data.
    307  *	6) re-read inode data for all active vnodes.
    308  */
    309 int
    310 ffs_reload(mountp, cred, p)
    311 	register struct mount *mountp;
    312 	struct ucred *cred;
    313 	struct proc *p;
    314 {
    315 	register struct vnode *vp, *nvp, *devvp;
    316 	struct inode *ip;
    317 	struct buf *bp;
    318 	struct fs *fs, *newfs;
    319 	struct partinfo dpart;
    320 	int i, blks, size, error;
    321 	int32_t *lp;
    322 
    323 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    324 		return (EINVAL);
    325 	/*
    326 	 * Step 1: invalidate all cached meta-data.
    327 	 */
    328 	devvp = VFSTOUFS(mountp)->um_devvp;
    329 	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
    330 		panic("ffs_reload: dirty1");
    331 	/*
    332 	 * Step 2: re-read superblock from disk.
    333 	 */
    334 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
    335 		size = DEV_BSIZE;
    336 	else
    337 		size = dpart.disklab->d_secsize;
    338 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    339 	if (error)
    340 		return (error);
    341 	fs = VFSTOUFS(mountp)->um_fs;
    342 	newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
    343 	memcpy(newfs, bp->b_data, fs->fs_sbsize);
    344 #ifdef FFS_EI
    345 	if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP)
    346 		ffs_sb_swap((struct fs*)bp->b_data, newfs, 0);
    347 #endif
    348 	if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
    349 	    newfs->fs_bsize < sizeof(struct fs)) {
    350 		brelse(bp);
    351 		free(newfs, M_UFSMNT);
    352 		return (EIO);		/* XXX needs translation */
    353 	}
    354 	/*
    355 	 * Copy pointer fields back into superblock before copying in	XXX
    356 	 * new superblock. These should really be in the ufsmount.	XXX
    357 	 * Note that important parameters (eg fs_ncg) are unchanged.
    358 	 */
    359 	memcpy(&newfs->fs_csp[0], &fs->fs_csp[0], sizeof(fs->fs_csp));
    360 	newfs->fs_maxcluster = fs->fs_maxcluster;
    361 	memcpy(fs, newfs, (u_int)fs->fs_sbsize);
    362 	if (fs->fs_sbsize < SBSIZE)
    363 		bp->b_flags |= B_INVAL;
    364 	brelse(bp);
    365 	free(newfs, M_UFSMNT);
    366 	mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    367 	ffs_oldfscompat(fs);
    368 	/*
    369 	 * Step 3: re-read summary information from disk.
    370 	 */
    371 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
    372 	for (i = 0; i < blks; i += fs->fs_frag) {
    373 		size = fs->fs_bsize;
    374 		if (i + fs->fs_frag > blks)
    375 			size = (blks - i) * fs->fs_fsize;
    376 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    377 			      NOCRED, &bp);
    378 		if (error)
    379 			return (error);
    380 #ifdef FFS_EI
    381 		if (UFS_MPNEEDSWAP(mountp))
    382 			ffs_csum_swap((struct csum*)bp->b_data,
    383 			    (struct csum*)fs->fs_csp[fragstoblks(fs, i)], size);
    384 		else
    385 #endif
    386 			memcpy(fs->fs_csp[fragstoblks(fs, i)], bp->b_data,
    387 			    (size_t)size);
    388 		brelse(bp);
    389 	}
    390 	/*
    391 	 * We no longer know anything about clusters per cylinder group.
    392 	 */
    393 	if (fs->fs_contigsumsize > 0) {
    394 		lp = fs->fs_maxcluster;
    395 		for (i = 0; i < fs->fs_ncg; i++)
    396 			*lp++ = fs->fs_contigsumsize;
    397 	}
    398 
    399 loop:
    400 	simple_lock(&mntvnode_slock);
    401 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    402 		if (vp->v_mount != mountp) {
    403 			simple_unlock(&mntvnode_slock);
    404 			goto loop;
    405 		}
    406 		nvp = vp->v_mntvnodes.le_next;
    407 		/*
    408 		 * Step 4: invalidate all inactive vnodes.
    409 		 */
    410 		if (vrecycle(vp, &mntvnode_slock, p))
    411 			goto loop;
    412 		/*
    413 		 * Step 5: invalidate all cached file data.
    414 		 */
    415 		simple_lock(&vp->v_interlock);
    416 		simple_unlock(&mntvnode_slock);
    417 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    418 			goto loop;
    419 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    420 			panic("ffs_reload: dirty2");
    421 		/*
    422 		 * Step 6: re-read inode data for all active vnodes.
    423 		 */
    424 		ip = VTOI(vp);
    425 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    426 			      (int)fs->fs_bsize, NOCRED, &bp);
    427 		if (error) {
    428 			vput(vp);
    429 			return (error);
    430 		}
    431 #ifdef FFS_EI
    432 		if (UFS_MPNEEDSWAP(mountp))
    433 			ffs_dinode_swap((struct dinode *)bp->b_data +
    434 			    ino_to_fsbo(fs, ip->i_number), &ip->i_din.ffs_din);
    435 		else
    436 #endif
    437 			ip->i_din.ffs_din = *((struct dinode *)bp->b_data +
    438 			    ino_to_fsbo(fs, ip->i_number));
    439 		brelse(bp);
    440 		vput(vp);
    441 		simple_lock(&mntvnode_slock);
    442 	}
    443 	simple_unlock(&mntvnode_slock);
    444 	return (0);
    445 }
    446 
    447 /*
    448  * Common code for mount and mountroot
    449  */
    450 int
    451 ffs_mountfs(devvp, mp, p)
    452 	register struct vnode *devvp;
    453 	struct mount *mp;
    454 	struct proc *p;
    455 {
    456 	struct ufsmount *ump;
    457 	struct buf *bp;
    458 	struct fs *fs;
    459 	dev_t dev;
    460 	struct partinfo dpart;
    461 	caddr_t base, space;
    462 	int blks;
    463 	int error, i, size, ronly, needswap;
    464 	int32_t *lp;
    465 	struct ucred *cred;
    466 	extern struct vnode *rootvp;
    467 	u_int64_t maxfilesize;					/* XXX */
    468 	u_int32_t sbsize;
    469 
    470 	dev = devvp->v_rdev;
    471 	cred = p ? p->p_ucred : NOCRED;
    472 	/*
    473 	 * Disallow multiple mounts of the same device.
    474 	 * Disallow mounting of a device that is currently in use
    475 	 * (except for root, which might share swap device for miniroot).
    476 	 * Flush out any old buffers remaining from a previous use.
    477 	 */
    478 	if ((error = vfs_mountedon(devvp)) != 0)
    479 		return (error);
    480 	if (vcount(devvp) > 1 && devvp != rootvp)
    481 		return (EBUSY);
    482 	if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
    483 		return (error);
    484 
    485 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    486 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    487 	if (error)
    488 		return (error);
    489 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    490 		size = DEV_BSIZE;
    491 	else
    492 		size = dpart.disklab->d_secsize;
    493 
    494 	bp = NULL;
    495 	ump = NULL;
    496 	error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
    497 	if (error)
    498 		goto out;
    499 
    500 	fs = (struct fs*)bp->b_data;
    501 	if (fs->fs_magic == FS_MAGIC) {
    502 		needswap = 0;
    503 		sbsize = fs->fs_sbsize;
    504 #ifdef FFS_EI
    505 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    506 		needswap = 1;
    507 		sbsize = bswap32(fs->fs_sbsize);
    508 #endif
    509 	} else {
    510 		error = EINVAL;
    511 		goto out;
    512 
    513 	}
    514 
    515 	fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
    516 	memcpy(fs, bp->b_data, sbsize);
    517 #ifdef FFS_EI
    518 	if (needswap)
    519 		ffs_sb_swap((struct fs*)bp->b_data, fs, 0);
    520 #endif
    521 
    522 	if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
    523 	    fs->fs_bsize < sizeof(struct fs)) {
    524 		error = EINVAL;		/* XXX needs translation */
    525 		goto out2;
    526 	}
    527 	/* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
    528 	if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
    529 		error = EROFS;		/* XXX what should be returned? */
    530 		goto out2;
    531 	}
    532 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    533 	memset((caddr_t)ump, 0, sizeof *ump);
    534 	ump->um_fs = fs;
    535 	if (fs->fs_sbsize < SBSIZE)
    536 		bp->b_flags |= B_INVAL;
    537 	brelse(bp);
    538 	bp = NULL;
    539 	fs->fs_ronly = ronly;
    540 	if (ronly == 0) {
    541 		fs->fs_clean <<= 1;
    542 		fs->fs_fmod = 1;
    543 	}
    544 	size = fs->fs_cssize;
    545 	blks = howmany(size, fs->fs_fsize);
    546 	if (fs->fs_contigsumsize > 0)
    547 		size += fs->fs_ncg * sizeof(int32_t);
    548 	base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    549 	for (i = 0; i < blks; i += fs->fs_frag) {
    550 		size = fs->fs_bsize;
    551 		if (i + fs->fs_frag > blks)
    552 			size = (blks - i) * fs->fs_fsize;
    553 		error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
    554 			      cred, &bp);
    555 		if (error) {
    556 			free(base, M_UFSMNT);
    557 			goto out2;
    558 		}
    559 #ifdef FFS_EI
    560 		if (needswap)
    561 			ffs_csum_swap((struct csum*)bp->b_data,
    562 				(struct csum*)space, size);
    563 		else
    564 #endif
    565 			memcpy(space, bp->b_data, (u_int)size);
    566 
    567 		fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
    568 		space += size;
    569 		brelse(bp);
    570 		bp = NULL;
    571 	}
    572 	if (fs->fs_contigsumsize > 0) {
    573 		fs->fs_maxcluster = lp = (int32_t *)space;
    574 		for (i = 0; i < fs->fs_ncg; i++)
    575 			*lp++ = fs->fs_contigsumsize;
    576 	}
    577 	mp->mnt_data = (qaddr_t)ump;
    578 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    579 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
    580 	mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
    581 	mp->mnt_flag |= MNT_LOCAL;
    582 #ifdef FFS_EI
    583 	if (needswap)
    584 		ump->um_flags |= UFS_NEEDSWAP;
    585 #endif
    586 	ump->um_mountp = mp;
    587 	ump->um_dev = dev;
    588 	ump->um_devvp = devvp;
    589 	ump->um_nindir = fs->fs_nindir;
    590 	ump->um_bptrtodb = fs->fs_fsbtodb;
    591 	ump->um_seqinc = fs->fs_frag;
    592 	for (i = 0; i < MAXQUOTAS; i++)
    593 		ump->um_quotas[i] = NULLVP;
    594 	devvp->v_specflags |= SI_MOUNTEDON;
    595 	ffs_oldfscompat(fs);
    596 	ump->um_savedmaxfilesize = fs->fs_maxfilesize;		/* XXX */
    597 	maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;	/* XXX */
    598 	if (fs->fs_maxfilesize > maxfilesize)			/* XXX */
    599 		fs->fs_maxfilesize = maxfilesize;		/* XXX */
    600 	return (0);
    601 out2:
    602 	free(fs, M_UFSMNT);
    603 out:
    604 	if (bp)
    605 		brelse(bp);
    606 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    607 	if (ump) {
    608 		free(ump, M_UFSMNT);
    609 		mp->mnt_data = (qaddr_t)0;
    610 	}
    611 	return (error);
    612 }
    613 
    614 /*
    615  * Sanity checks for old file systems.
    616  *
    617  * XXX - goes away some day.
    618  */
    619 int
    620 ffs_oldfscompat(fs)
    621 	struct fs *fs;
    622 {
    623 	int i;
    624 
    625 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    626 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    627 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    628 		fs->fs_nrpos = 8;				/* XXX */
    629 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    630 		u_int64_t sizepb = fs->fs_bsize;		/* XXX */
    631 								/* XXX */
    632 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    633 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    634 			sizepb *= NINDIR(fs);			/* XXX */
    635 			fs->fs_maxfilesize += sizepb;		/* XXX */
    636 		}						/* XXX */
    637 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    638 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    639 	}							/* XXX */
    640 	return (0);
    641 }
    642 
    643 /*
    644  * unmount system call
    645  */
    646 int
    647 ffs_unmount(mp, mntflags, p)
    648 	struct mount *mp;
    649 	int mntflags;
    650 	struct proc *p;
    651 {
    652 	register struct ufsmount *ump;
    653 	register struct fs *fs;
    654 	int error, flags;
    655 
    656 	flags = 0;
    657 	if (mntflags & MNT_FORCE)
    658 		flags |= FORCECLOSE;
    659 	if ((error = ffs_flushfiles(mp, flags, p)) != 0)
    660 		return (error);
    661 	ump = VFSTOUFS(mp);
    662 	fs = ump->um_fs;
    663 	if (fs->fs_ronly == 0 &&
    664 	    ffs_cgupdate(ump, MNT_WAIT) == 0 &&
    665 	    fs->fs_clean & FS_WASCLEAN) {
    666 		fs->fs_clean = FS_ISCLEAN;
    667 		(void) ffs_sbupdate(ump, MNT_WAIT);
    668 	}
    669 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
    670 	error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
    671 		NOCRED, p);
    672 	vrele(ump->um_devvp);
    673 	free(fs->fs_csp[0], M_UFSMNT);
    674 	free(fs, M_UFSMNT);
    675 	free(ump, M_UFSMNT);
    676 	mp->mnt_data = (qaddr_t)0;
    677 	mp->mnt_flag &= ~MNT_LOCAL;
    678 	return (error);
    679 }
    680 
    681 /*
    682  * Flush out all the files in a filesystem.
    683  */
    684 int
    685 ffs_flushfiles(mp, flags, p)
    686 	register struct mount *mp;
    687 	int flags;
    688 	struct proc *p;
    689 {
    690 	extern int doforce;
    691 	register struct ufsmount *ump;
    692 	int error;
    693 
    694 	if (!doforce)
    695 		flags &= ~FORCECLOSE;
    696 	ump = VFSTOUFS(mp);
    697 #ifdef QUOTA
    698 	if (mp->mnt_flag & MNT_QUOTA) {
    699 		int i;
    700 		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
    701 			return (error);
    702 		for (i = 0; i < MAXQUOTAS; i++) {
    703 			if (ump->um_quotas[i] == NULLVP)
    704 				continue;
    705 			quotaoff(p, mp, i);
    706 		}
    707 		/*
    708 		 * Here we fall through to vflush again to ensure
    709 		 * that we have gotten rid of all the system vnodes.
    710 		 */
    711 	}
    712 #endif
    713 	error = vflush(mp, NULLVP, flags);
    714 	return (error);
    715 }
    716 
    717 /*
    718  * Get file system statistics.
    719  */
    720 int
    721 ffs_statfs(mp, sbp, p)
    722 	struct mount *mp;
    723 	register struct statfs *sbp;
    724 	struct proc *p;
    725 {
    726 	register struct ufsmount *ump;
    727 	register struct fs *fs;
    728 
    729 	ump = VFSTOUFS(mp);
    730 	fs = ump->um_fs;
    731 	if (fs->fs_magic != FS_MAGIC)
    732 		panic("ffs_statfs");
    733 #ifdef COMPAT_09
    734 	sbp->f_type = 1;
    735 #else
    736 	sbp->f_type = 0;
    737 #endif
    738 	sbp->f_bsize = fs->fs_fsize;
    739 	sbp->f_iosize = fs->fs_bsize;
    740 	sbp->f_blocks = fs->fs_dsize;
    741 	sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
    742 		fs->fs_cstotal.cs_nffree;
    743 	sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
    744 	    (100 - fs->fs_minfree) / (u_int64_t) 100) -
    745 	    (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
    746 	sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
    747 	sbp->f_ffree = fs->fs_cstotal.cs_nifree;
    748 	if (sbp != &mp->mnt_stat) {
    749 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    750 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    751 	}
    752 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    753 	return (0);
    754 }
    755 
    756 /*
    757  * Go through the disk queues to initiate sandbagged IO;
    758  * go through the inodes to write those that have been modified;
    759  * initiate the writing of the super block if it has been modified.
    760  *
    761  * Note: we are always called with the filesystem marked `MPBUSY'.
    762  */
    763 int
    764 ffs_sync(mp, waitfor, cred, p)
    765 	struct mount *mp;
    766 	int waitfor;
    767 	struct ucred *cred;
    768 	struct proc *p;
    769 {
    770 	struct vnode *vp, *nvp;
    771 	struct inode *ip;
    772 	struct ufsmount *ump = VFSTOUFS(mp);
    773 	struct fs *fs;
    774 	int error, allerror = 0;
    775 
    776 	fs = ump->um_fs;
    777 	if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {		/* XXX */
    778 		printf("fs = %s\n", fs->fs_fsmnt);
    779 		panic("update: rofs mod");
    780 	}
    781 	/*
    782 	 * Write back each (modified) inode.
    783 	 */
    784 	simple_lock(&mntvnode_slock);
    785 loop:
    786 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    787 		/*
    788 		 * If the vnode that we are about to sync is no longer
    789 		 * associated with this mount point, start over.
    790 		 */
    791 		if (vp->v_mount != mp)
    792 			goto loop;
    793 		simple_lock(&vp->v_interlock);
    794 		nvp = vp->v_mntvnodes.le_next;
    795 		ip = VTOI(vp);
    796 		if ((ip->i_flag &
    797 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
    798 		    vp->v_dirtyblkhd.lh_first == NULL) {
    799 			simple_unlock(&vp->v_interlock);
    800 			continue;
    801 		}
    802 		simple_unlock(&mntvnode_slock);
    803 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    804 		if (error) {
    805 			simple_lock(&mntvnode_slock);
    806 			if (error == ENOENT)
    807 				goto loop;
    808 			continue;
    809 		}
    810 		if ((error = VOP_FSYNC(vp, cred,
    811 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
    812 			allerror = error;
    813 		vput(vp);
    814 		simple_lock(&mntvnode_slock);
    815 	}
    816 	simple_unlock(&mntvnode_slock);
    817 	/*
    818 	 * Force stale file system control information to be flushed.
    819 	 */
    820 	if ((error = VOP_FSYNC(ump->um_devvp, cred,
    821 	    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
    822 		allerror = error;
    823 #ifdef QUOTA
    824 	qsync(mp);
    825 #endif
    826 	/*
    827 	 * Write back modified superblock.
    828 	 */
    829 	if (fs->fs_fmod != 0) {
    830 		fs->fs_fmod = 0;
    831 		fs->fs_time = time.tv_sec;
    832 		allerror = ffs_cgupdate(ump, waitfor);
    833 	}
    834 	return (allerror);
    835 }
    836 
    837 /*
    838  * Look up a FFS dinode number to find its incore vnode, otherwise read it
    839  * in from disk.  If it is in core, wait for the lock bit to clear, then
    840  * return the inode locked.  Detection and handling of mount points must be
    841  * done by the calling routine.
    842  */
    843 int
    844 ffs_vget(mp, ino, vpp)
    845 	struct mount *mp;
    846 	ino_t ino;
    847 	struct vnode **vpp;
    848 {
    849 	struct fs *fs;
    850 	struct inode *ip;
    851 	struct ufsmount *ump;
    852 	struct buf *bp;
    853 	struct vnode *vp;
    854 	dev_t dev;
    855 	int error;
    856 
    857 	ump = VFSTOUFS(mp);
    858 	dev = ump->um_dev;
    859 	do {
    860 		if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
    861 			return (0);
    862 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    863 
    864 	/* Allocate a new vnode/inode. */
    865 	if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
    866 		*vpp = NULL;
    867 		lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    868 		return (error);
    869 	}
    870 	/*
    871 	 * XXX MFS ends up here, too, to allocate an inode.  Should we
    872 	 * XXX create another pool for MFS inodes?
    873 	 */
    874 	ip = pool_get(&ffs_inode_pool, PR_WAITOK);
    875 	memset((caddr_t)ip, 0, sizeof(struct inode));
    876 	lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
    877 	vp->v_data = ip;
    878 	ip->i_vnode = vp;
    879 	ip->i_fs = fs = ump->um_fs;
    880 	ip->i_dev = dev;
    881 	ip->i_number = ino;
    882 #ifdef QUOTA
    883 	{
    884 		int i;
    885 
    886 		for (i = 0; i < MAXQUOTAS; i++)
    887 			ip->i_dquot[i] = NODQUOT;
    888 	}
    889 #endif
    890 	/*
    891 	 * Put it onto its hash chain and lock it so that other requests for
    892 	 * this inode will block if they arrive while we are sleeping waiting
    893 	 * for old data structures to be purged or for the contents of the
    894 	 * disk portion of this inode to be read.
    895 	 */
    896 	ufs_ihashins(ip);
    897 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    898 
    899 	/* Read in the disk contents for the inode, copy into the inode. */
    900 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
    901 		      (int)fs->fs_bsize, NOCRED, &bp);
    902 	if (error) {
    903 		/*
    904 		 * The inode does not contain anything useful, so it would
    905 		 * be misleading to leave it on its hash chain. With mode
    906 		 * still zero, it will be unlinked and returned to the free
    907 		 * list by vput().
    908 		 */
    909 		vput(vp);
    910 		brelse(bp);
    911 		*vpp = NULL;
    912 		return (error);
    913 	}
    914 #ifdef FFS_EI
    915 	if (UFS_MPNEEDSWAP(mp))
    916 		ffs_dinode_swap((struct dinode *)bp->b_data +
    917 		    ino_to_fsbo(fs, ino), &(ip->i_din.ffs_din));
    918 	else
    919 #endif
    920 		ip->i_din.ffs_din =
    921 		    *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
    922 	brelse(bp);
    923 
    924 	/*
    925 	 * Initialize the vnode from the inode, check for aliases.
    926 	 * Note that the underlying vnode may have changed.
    927 	 */
    928 	error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
    929 	if (error) {
    930 		vput(vp);
    931 		*vpp = NULL;
    932 		return (error);
    933 	}
    934 	/*
    935 	 * Finish inode initialization now that aliasing has been resolved.
    936 	 */
    937 	ip->i_devvp = ump->um_devvp;
    938 	VREF(ip->i_devvp);
    939 	/*
    940 	 * Ensure that uid and gid are correct. This is a temporary
    941 	 * fix until fsck has been changed to do the update.
    942 	 */
    943 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    944 		ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid;	/* XXX */
    945 		ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid;	/* XXX */
    946 	}							/* XXX */
    947 
    948 	*vpp = vp;
    949 	return (0);
    950 }
    951 
    952 /*
    953  * File handle to vnode
    954  *
    955  * Have to be really careful about stale file handles:
    956  * - check that the inode number is valid
    957  * - call ffs_vget() to get the locked inode
    958  * - check for an unallocated inode (i_mode == 0)
    959  * - check that the given client host has export rights and return
    960  *   those rights via. exflagsp and credanonp
    961  */
    962 int
    963 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
    964 	register struct mount *mp;
    965 	struct fid *fhp;
    966 	struct mbuf *nam;
    967 	struct vnode **vpp;
    968 	int *exflagsp;
    969 	struct ucred **credanonp;
    970 {
    971 	register struct ufid *ufhp;
    972 	struct fs *fs;
    973 
    974 	ufhp = (struct ufid *)fhp;
    975 	fs = VFSTOUFS(mp)->um_fs;
    976 	if (ufhp->ufid_ino < ROOTINO ||
    977 	    ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
    978 		return (ESTALE);
    979 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
    980 }
    981 
    982 /*
    983  * Vnode pointer to File handle
    984  */
    985 /* ARGSUSED */
    986 int
    987 ffs_vptofh(vp, fhp)
    988 	struct vnode *vp;
    989 	struct fid *fhp;
    990 {
    991 	register struct inode *ip;
    992 	register struct ufid *ufhp;
    993 
    994 	ip = VTOI(vp);
    995 	ufhp = (struct ufid *)fhp;
    996 	ufhp->ufid_len = sizeof(struct ufid);
    997 	ufhp->ufid_ino = ip->i_number;
    998 	ufhp->ufid_gen = ip->i_ffs_gen;
    999 	return (0);
   1000 }
   1001 
   1002 void
   1003 ffs_init()
   1004 {
   1005 	ufs_init();
   1006 
   1007 	pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
   1008 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
   1009 }
   1010 
   1011 int
   1012 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1013 	int *name;
   1014 	u_int namelen;
   1015 	void *oldp;
   1016 	size_t *oldlenp;
   1017 	void *newp;
   1018 	size_t newlen;
   1019 	struct proc *p;
   1020 {
   1021 	extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
   1022 
   1023 	/* all sysctl names at this level are terminal */
   1024 	if (namelen != 1)
   1025 		return (ENOTDIR);		/* overloaded */
   1026 
   1027 	switch (name[0]) {
   1028 	case FFS_CLUSTERREAD:
   1029 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1030 		    &doclusterread));
   1031 	case FFS_CLUSTERWRITE:
   1032 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1033 		    &doclusterwrite));
   1034 	case FFS_REALLOCBLKS:
   1035 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1036 		    &doreallocblks));
   1037 	case FFS_ASYNCFREE:
   1038 		return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
   1039 	default:
   1040 		return (EOPNOTSUPP);
   1041 	}
   1042 	/* NOTREACHED */
   1043 }
   1044 
   1045 /*
   1046  * Write a superblock and associated information back to disk.
   1047  */
   1048 int
   1049 ffs_sbupdate(mp, waitfor)
   1050 	struct ufsmount *mp;
   1051 	int waitfor;
   1052 {
   1053 	register struct fs *fs = mp->um_fs;
   1054 	register struct buf *bp;
   1055 	int i, error = 0;
   1056 	int32_t saved_nrpos = fs->fs_nrpos;
   1057 	int64_t saved_qbmask = fs->fs_qbmask;
   1058 	int64_t saved_qfmask = fs->fs_qfmask;
   1059 	u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
   1060 
   1061 	/* Restore compatibility to old file systems.		   XXX */
   1062 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
   1063 		fs->fs_nrpos = -1;		/* XXX */
   1064 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
   1065 		int32_t *lp, tmp;				/* XXX */
   1066 								/* XXX */
   1067 		lp = (int32_t *)&fs->fs_qbmask;	/* XXX nuke qfmask too */
   1068 		tmp = lp[4];					/* XXX */
   1069 		for (i = 4; i > 0; i--)				/* XXX */
   1070 			lp[i] = lp[i-1];			/* XXX */
   1071 		lp[0] = tmp;					/* XXX */
   1072 	}							/* XXX */
   1073 	fs->fs_maxfilesize = mp->um_savedmaxfilesize;	/* XXX */
   1074 
   1075 	bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
   1076 	    (int)fs->fs_sbsize, 0, 0);
   1077 	memcpy(bp->b_data, fs, fs->fs_sbsize);
   1078 #ifdef FFS_EI
   1079 	if (mp->um_flags & UFS_NEEDSWAP)
   1080 		ffs_sb_swap(fs, (struct fs*)bp->b_data, 1);
   1081 #endif
   1082 
   1083 	fs->fs_nrpos = saved_nrpos; /* XXX */
   1084 	fs->fs_qbmask = saved_qbmask; /* XXX */
   1085 	fs->fs_qfmask = saved_qfmask; /* XXX */
   1086 	fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
   1087 
   1088 	if (waitfor == MNT_WAIT)
   1089 		error = bwrite(bp);
   1090 	else
   1091 		bawrite(bp);
   1092 	return (error);
   1093 }
   1094 
   1095 int
   1096 ffs_cgupdate(mp, waitfor)
   1097 	struct ufsmount *mp;
   1098 	int waitfor;
   1099 {
   1100 	register struct fs *fs = mp->um_fs;
   1101 	register struct buf *bp;
   1102 	int blks;
   1103 	caddr_t space;
   1104 	int i, size, error = 0, allerror = 0;
   1105 
   1106 	allerror = ffs_sbupdate(mp, waitfor);
   1107 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
   1108 	space = (caddr_t)fs->fs_csp[0];
   1109 	for (i = 0; i < blks; i += fs->fs_frag) {
   1110 		size = fs->fs_bsize;
   1111 		if (i + fs->fs_frag > blks)
   1112 			size = (blks - i) * fs->fs_fsize;
   1113 		bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
   1114 		    size, 0, 0);
   1115 #ifdef FFS_EI
   1116 		if (mp->um_flags & UFS_NEEDSWAP)
   1117 			ffs_csum_swap((struct csum*)space,
   1118 			    (struct csum*)bp->b_data, size);
   1119 		else
   1120 #endif
   1121 			memcpy(bp->b_data, space, (u_int)size);
   1122 		space += size;
   1123 		if (waitfor == MNT_WAIT)
   1124 			error = bwrite(bp);
   1125 		else
   1126 			bawrite(bp);
   1127 	}
   1128 	if (!allerror && error)
   1129 		allerror = error;
   1130 	return (allerror);
   1131 }
   1132