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