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