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