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