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