ext2_vfsops.c revision 43395
112115Sdyson/*
212115Sdyson *  modified for EXT2FS support in Lites 1.1
312115Sdyson *
412115Sdyson *  Aug 1995, Godmar Back (gback@cs.utah.edu)
512115Sdyson *  University of Utah, Department of Computer Science
612115Sdyson */
712115Sdyson/*
812115Sdyson * Copyright (c) 1989, 1991, 1993, 1994
912115Sdyson *	The Regents of the University of California.  All rights reserved.
1012115Sdyson *
1112115Sdyson * Redistribution and use in source and binary forms, with or without
1212115Sdyson * modification, are permitted provided that the following conditions
1312115Sdyson * are met:
1412115Sdyson * 1. Redistributions of source code must retain the above copyright
1512115Sdyson *    notice, this list of conditions and the following disclaimer.
1612115Sdyson * 2. Redistributions in binary form must reproduce the above copyright
1712115Sdyson *    notice, this list of conditions and the following disclaimer in the
1812115Sdyson *    documentation and/or other materials provided with the distribution.
1912115Sdyson * 3. All advertising materials mentioning features or use of this software
2012115Sdyson *    must display the following acknowledgement:
2112115Sdyson *	This product includes software developed by the University of
2212115Sdyson *	California, Berkeley and its contributors.
2312115Sdyson * 4. Neither the name of the University nor the names of its contributors
2412115Sdyson *    may be used to endorse or promote products derived from this software
2512115Sdyson *    without specific prior written permission.
2612115Sdyson *
2712115Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2812115Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2912115Sdyson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3012115Sdyson * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3112115Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3212115Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3312115Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3412115Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3512115Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3612115Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3712115Sdyson * SUCH DAMAGE.
3812115Sdyson *
3912115Sdyson *	@(#)ffs_vfsops.c	8.8 (Berkeley) 4/18/94
4012115Sdyson */
4112115Sdyson
4213260Swollman#include "opt_quota.h"
4312115Sdyson
4412115Sdyson#include <sys/param.h>
4512115Sdyson#include <sys/systm.h>
4612115Sdyson#include <sys/namei.h>
4712115Sdyson#include <sys/proc.h>
4812115Sdyson#include <sys/kernel.h>
4912115Sdyson#include <sys/vnode.h>
5012115Sdyson#include <sys/mount.h>
5112115Sdyson#include <sys/buf.h>
5229906Skato#include <sys/conf.h>
5324131Sbde#include <sys/fcntl.h>
5412115Sdyson#include <sys/disklabel.h>
5512115Sdyson#include <sys/malloc.h>
5612115Sdyson#include <sys/stat.h>
5712115Sdyson
5812115Sdyson#include <miscfs/specfs/specdev.h>
5912115Sdyson
6012115Sdyson#include <ufs/ufs/quota.h>
6112115Sdyson#include <ufs/ufs/ufsmount.h>
6212115Sdyson#include <ufs/ufs/inode.h>
6312115Sdyson#include <ufs/ufs/ufs_extern.h>
6412115Sdyson
6512115Sdyson#include <gnu/ext2fs/fs.h>
6612115Sdyson#include <gnu/ext2fs/ext2_extern.h>
6712115Sdyson#include <gnu/ext2fs/ext2_fs.h>
6812115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h>
6912115Sdyson
7028270Swollmanstatic int ext2_fhtovp __P((struct mount *, struct fid *, struct sockaddr *,
7112911Sphk	    struct vnode **, int *, struct ucred **));
7212911Sphkstatic int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p));
7312911Sphkstatic int ext2_mount __P((struct mount *,
7412911Sphk	    char *, caddr_t, struct nameidata *, struct proc *));
7512911Sphkstatic int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *));
7612911Sphkstatic int ext2_reload __P((struct mount *mountp, struct ucred *cred,
7712911Sphk			struct proc *p));
7812911Sphkstatic int ext2_sbupdate __P((struct ufsmount *, int));
7912911Sphkstatic int ext2_statfs __P((struct mount *, struct statfs *, struct proc *));
8012911Sphkstatic int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *));
8112911Sphkstatic int ext2_unmount __P((struct mount *, int, struct proc *));
8212911Sphkstatic int ext2_vget __P((struct mount *, ino_t, struct vnode **));
8312911Sphkstatic int ext2_vptofh __P((struct vnode *, struct fid *));
8412115Sdyson
8531315Sbdestatic MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
8630280Sphk
8712911Sphkstatic struct vfsops ext2fs_vfsops = {
8812115Sdyson	ext2_mount,
8912115Sdyson	ufs_start,		/* empty function */
9012115Sdyson	ext2_unmount,
9112115Sdyson	ufs_root,		/* root inode via vget */
9212115Sdyson	ufs_quotactl,		/* does operations associated with quotas */
9312115Sdyson	ext2_statfs,
9412115Sdyson	ext2_sync,
9512115Sdyson	ext2_vget,
9612115Sdyson	ext2_fhtovp,
9712115Sdyson	ext2_vptofh,
9812115Sdyson	ext2_init,
9912115Sdyson};
10012115Sdyson
10138909SbdeVFS_SET(ext2fs_vfsops, ext2fs, 0);
10212115Sdyson#define bsd_malloc malloc
10312115Sdyson#define bsd_free free
10412115Sdyson
10512911Sphkstatic int ext2fs_inode_hash_lock;
10612115Sdyson
10716322Sgpalmerstatic int	compute_sb_data __P((struct vnode * devvp,
10816322Sgpalmer				     struct ext2_super_block * es,
10916322Sgpalmer				     struct ext2_sb_info * fs));
11016322Sgpalmer
11116322Sgpalmer#ifdef notyet
11216322Sgpalmerstatic int ext2_mountroot __P((void));
11316322Sgpalmer
11412115Sdyson/*
11516322Sgpalmer * Called by main() when ext2fs is going to be mounted as root.
11612115Sdyson *
11712115Sdyson * Name is updated by mount(8) after booting.
11812115Sdyson */
11912115Sdyson#define ROOTNAME	"root_device"
12012115Sdyson
12112911Sphkstatic int
12212115Sdysonext2_mountroot()
12312115Sdyson{
12412115Sdyson	register struct ext2_sb_info *fs;
12512115Sdyson	register struct mount *mp;
12612115Sdyson	struct proc *p = curproc;
12712115Sdyson	struct ufsmount *ump;
12812115Sdyson	u_int size;
12912115Sdyson	int error;
13012115Sdyson
13129208Sbde	if ((error = bdevvp(rootdev, &rootvp))) {
13229208Sbde		printf("ext2_mountroot: can't find rootvp");
13329208Sbde		return (error);
13429208Sbde	}
13512115Sdyson	mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
13612115Sdyson	bzero((char *)mp, (u_long)sizeof(struct mount));
13712115Sdyson	mp->mnt_op = &ext2fs_vfsops;
13812115Sdyson	mp->mnt_flag = MNT_RDONLY;
13929888Skato	if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERR)
14029888Skato		mp->mnt_flag |= MNT_NOCLUSTERR;
14129888Skato	if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERW)
14229888Skato		mp->mnt_flag |= MNT_NOCLUSTERW;
14312115Sdyson	if (error = ext2_mountfs(rootvp, mp, p)) {
14412115Sdyson		bsd_free(mp, M_MOUNT);
14512115Sdyson		return (error);
14612115Sdyson	}
14712115Sdyson	if (error = vfs_lock(mp)) {
14812115Sdyson		(void)ext2_unmount(mp, 0, p);
14912115Sdyson		bsd_free(mp, M_MOUNT);
15012115Sdyson		return (error);
15112115Sdyson	}
15230469Sjulian	CIRCLEQ_INSERT_HEAD(&mountlist, mp, mnt_list);
15312115Sdyson	mp->mnt_flag |= MNT_ROOTFS;
15412115Sdyson	mp->mnt_vnodecovered = NULLVP;
15512115Sdyson	ump = VFSTOUFS(mp);
15612115Sdyson	fs = ump->um_e2fs;
15712115Sdyson	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
15812115Sdyson	fs->fs_fsmnt[0] = '/';
15912115Sdyson	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
16012115Sdyson	    MNAMELEN);
16112115Sdyson	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
16212115Sdyson	    &size);
16312115Sdyson	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
16412115Sdyson	(void)ext2_statfs(mp, &mp->mnt_stat, p);
16512115Sdyson	vfs_unlock(mp);
16612115Sdyson	inittodr(fs->s_es->s_wtime);		/* this helps to set the time */
16712115Sdyson	return (0);
16812115Sdyson}
16916322Sgpalmer#endif
17012115Sdyson
17112115Sdyson/*
17212115Sdyson * VFS Operations.
17312115Sdyson *
17412115Sdyson * mount system call
17512115Sdyson */
17612911Sphkstatic int
17712115Sdysonext2_mount(mp, path, data, ndp, p)
17812115Sdyson	register struct mount *mp;
17912115Sdyson	char *path;
18012115Sdyson	caddr_t data;		/* this is actually a (struct ufs_args *) */
18112115Sdyson	struct nameidata *ndp;
18212115Sdyson	struct proc *p;
18312115Sdyson{
18412115Sdyson	struct vnode *devvp;
18512115Sdyson	struct ufs_args args;
18612115Sdyson	struct ufsmount *ump = 0;
18712115Sdyson	register struct ext2_sb_info *fs;
18812115Sdyson	u_int size;
18912115Sdyson	int error, flags;
19039028Sbde	mode_t accessmode;
19112115Sdyson
19243301Sdillon	if ((error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) != 0)
19312115Sdyson		return (error);
19412115Sdyson	/*
19512115Sdyson	 * If updating, check whether changing from read-only to
19612115Sdyson	 * read/write; if there is no device name, that's all we do.
19729888Skato	 * Disallow clearing MNT_NOCLUSTERR and MNT_NOCLUSTERW flags,
19829888Skato	 * if block device requests.
19912115Sdyson	 */
20012115Sdyson	if (mp->mnt_flag & MNT_UPDATE) {
20112115Sdyson		ump = VFSTOUFS(mp);
20212115Sdyson		fs = ump->um_e2fs;
20312115Sdyson		error = 0;
20429888Skato		if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERR)
20529888Skato			mp->mnt_flag |= MNT_NOCLUSTERR;
20629888Skato		if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERW)
20729888Skato			mp->mnt_flag |= MNT_NOCLUSTERW;
20812115Sdyson		if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
20912115Sdyson			flags = WRITECLOSE;
21012115Sdyson			if (mp->mnt_flag & MNT_FORCE)
21112115Sdyson				flags |= FORCECLOSE;
21222521Sdyson			if (vfs_busy(mp, LK_NOWAIT, 0, p))
21312115Sdyson				return (EBUSY);
21412115Sdyson			error = ext2_flushfiles(mp, flags, p);
21522521Sdyson			vfs_unbusy(mp, p);
21639670Sbde			if (!error && fs->s_wasvalid) {
21739670Sbde				fs->s_es->s_state |= EXT2_VALID_FS;
21839670Sbde				ext2_sbupdate(ump, MNT_WAIT);
21939670Sbde			}
22039670Sbde			fs->s_rd_only = 1;
22112115Sdyson		}
22212115Sdyson		if (!error && (mp->mnt_flag & MNT_RELOAD))
22312115Sdyson			error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p);
22412115Sdyson		if (error)
22512115Sdyson			return (error);
22639028Sbde		if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
22739028Sbde			/*
22839028Sbde			 * If upgrade to read-write by non-root, then verify
22939028Sbde			 * that user has necessary permissions on the device.
23039028Sbde			 */
23139028Sbde			if (p->p_ucred->cr_uid != 0) {
23239028Sbde				devvp = ump->um_devvp;
23339028Sbde				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
23443301Sdillon				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
23543301Sdillon				    p->p_ucred, p)) != 0) {
23639028Sbde					VOP_UNLOCK(devvp, 0, p);
23739028Sbde					return (error);
23839028Sbde				}
23939028Sbde				VOP_UNLOCK(devvp, 0, p);
24039028Sbde			}
24139028Sbde
24239670Sbde			if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
24339670Sbde			    (fs->s_es->s_state & EXT2_ERROR_FS)) {
24439670Sbde				if (mp->mnt_flag & MNT_FORCE) {
24539670Sbde					printf(
24639670Sbde"WARNING: %s was not properly dismounted\n",
24739670Sbde					    fs->fs_fsmnt);
24839670Sbde				} else {
24939670Sbde					printf(
25039670Sbde"WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
25139670Sbde					    fs->fs_fsmnt);
25239670Sbde					return (EPERM);
25339670Sbde				}
25439670Sbde			}
25512115Sdyson			fs->s_es->s_state &= ~EXT2_VALID_FS;
25612115Sdyson			ext2_sbupdate(ump, MNT_WAIT);
25739670Sbde			fs->s_rd_only = 0;
25812115Sdyson		}
25912115Sdyson		if (args.fspec == 0) {
26012115Sdyson			/*
26112115Sdyson			 * Process export requests.
26212115Sdyson			 */
26312115Sdyson			return (vfs_export(mp, &ump->um_export, &args.export));
26412115Sdyson		}
26512115Sdyson	}
26612115Sdyson	/*
26712115Sdyson	 * Not an update, or updating the name: look up the name
26812115Sdyson	 * and verify that it refers to a sensible block device.
26912115Sdyson	 */
27012115Sdyson	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
27143301Sdillon	if ((error = namei(ndp)) != 0)
27212115Sdyson		return (error);
27312115Sdyson	devvp = ndp->ni_vp;
27412115Sdyson
27512115Sdyson	if (devvp->v_type != VBLK) {
27612115Sdyson		vrele(devvp);
27712115Sdyson		return (ENOTBLK);
27812115Sdyson	}
27940651Sbde	if (major(devvp->v_rdev) >= nblkdev ||
28040651Sbde	    bdevsw[major(devvp->v_rdev)] == NULL) {
28112115Sdyson		vrele(devvp);
28212115Sdyson		return (ENXIO);
28312115Sdyson	}
28439028Sbde
28539028Sbde	/*
28639028Sbde	 * If mount by non-root, then verify that user has necessary
28739028Sbde	 * permissions on the device.
28839028Sbde	 */
28939028Sbde	if (p->p_ucred->cr_uid != 0) {
29039028Sbde		accessmode = VREAD;
29139028Sbde		if ((mp->mnt_flag & MNT_RDONLY) == 0)
29239028Sbde			accessmode |= VWRITE;
29339028Sbde		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
29443301Sdillon		if ((error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) != 0) {
29539028Sbde			vput(devvp);
29639028Sbde			return (error);
29739028Sbde		}
29839028Sbde		VOP_UNLOCK(devvp, 0, p);
29939028Sbde	}
30039028Sbde
30129888Skato	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
30229888Skato		if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERR)
30329888Skato			mp->mnt_flag |= MNT_NOCLUSTERR;
30429888Skato		if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERW)
30529888Skato			mp->mnt_flag |= MNT_NOCLUSTERW;
30612115Sdyson		error = ext2_mountfs(devvp, mp, p);
30729888Skato	} else {
30812115Sdyson		if (devvp != ump->um_devvp)
30912115Sdyson			error = EINVAL;	/* needs translation */
31012115Sdyson		else
31112115Sdyson			vrele(devvp);
31212115Sdyson	}
31312115Sdyson	if (error) {
31412115Sdyson		vrele(devvp);
31512115Sdyson		return (error);
31612115Sdyson	}
31712115Sdyson	ump = VFSTOUFS(mp);
31812115Sdyson	fs = ump->um_e2fs;
31912115Sdyson	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
32012115Sdyson	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
32112115Sdyson	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
32212115Sdyson	    MNAMELEN);
32312115Sdyson	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
32412115Sdyson	    &size);
32512115Sdyson	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
32612115Sdyson	(void)ext2_statfs(mp, &mp->mnt_stat, p);
32712115Sdyson	return (0);
32812115Sdyson}
32912115Sdyson
33012115Sdyson/*
33112115Sdyson * checks that the data in the descriptor blocks make sense
33212115Sdyson * this is taken from ext2/super.c
33312115Sdyson */
33412115Sdysonstatic int ext2_check_descriptors (struct ext2_sb_info * sb)
33512115Sdyson{
33612115Sdyson        int i;
33712115Sdyson        int desc_block = 0;
33812115Sdyson        unsigned long block = sb->s_es->s_first_data_block;
33912115Sdyson        struct ext2_group_desc * gdp = NULL;
34012115Sdyson
34112115Sdyson        /* ext2_debug ("Checking group descriptors"); */
34212115Sdyson
34312115Sdyson        for (i = 0; i < sb->s_groups_count; i++)
34412115Sdyson        {
34512115Sdyson		/* examine next descriptor block */
34612115Sdyson                if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
34712115Sdyson                        gdp = (struct ext2_group_desc *)
34812115Sdyson				sb->s_group_desc[desc_block++]->b_data;
34912115Sdyson                if (gdp->bg_block_bitmap < block ||
35012115Sdyson                    gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
35112115Sdyson                {
35212115Sdyson                        printf ("ext2_check_descriptors: "
35312115Sdyson                                    "Block bitmap for group %d"
35439671Sbde                                    " not in group (block %lu)!\n",
35512115Sdyson                                    i, (unsigned long) gdp->bg_block_bitmap);
35612115Sdyson                        return 0;
35712115Sdyson                }
35812115Sdyson                if (gdp->bg_inode_bitmap < block ||
35912115Sdyson                    gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
36012115Sdyson                {
36112115Sdyson                        printf ("ext2_check_descriptors: "
36212115Sdyson                                    "Inode bitmap for group %d"
36339671Sbde                                    " not in group (block %lu)!\n",
36412115Sdyson                                    i, (unsigned long) gdp->bg_inode_bitmap);
36512115Sdyson                        return 0;
36612115Sdyson                }
36712115Sdyson                if (gdp->bg_inode_table < block ||
36812115Sdyson                    gdp->bg_inode_table + sb->s_itb_per_group >=
36912115Sdyson                    block + EXT2_BLOCKS_PER_GROUP(sb))
37012115Sdyson                {
37112115Sdyson                        printf ("ext2_check_descriptors: "
37212115Sdyson                                    "Inode table for group %d"
37339671Sbde                                    " not in group (block %lu)!\n",
37412115Sdyson                                    i, (unsigned long) gdp->bg_inode_table);
37512115Sdyson                        return 0;
37612115Sdyson                }
37712115Sdyson                block += EXT2_BLOCKS_PER_GROUP(sb);
37812115Sdyson                gdp++;
37912115Sdyson        }
38012115Sdyson        return 1;
38112115Sdyson}
38212115Sdyson
38312115Sdyson/*
38412115Sdyson * this computes the fields of the  ext2_sb_info structure from the
38512115Sdyson * data in the ext2_super_block structure read in
38612115Sdyson */
38712115Sdysonstatic int compute_sb_data(devvp, es, fs)
38812115Sdyson	struct vnode * devvp;
38912115Sdyson	struct ext2_super_block * es;
39012115Sdyson	struct ext2_sb_info * fs;
39112115Sdyson{
39212115Sdyson    int db_count, error;
39312115Sdyson    int i, j;
39412115Sdyson    int logic_sb_block = 1;	/* XXX for now */
39512115Sdyson
39612115Sdyson#if 1
39712115Sdyson#define V(v)
39812115Sdyson#else
39912115Sdyson#define V(v)  printf(#v"= %d\n", fs->v);
40012115Sdyson#endif
40112115Sdyson
40212115Sdyson    fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
40312115Sdyson    V(s_blocksize)
40412115Sdyson    fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
40512115Sdyson    V(s_bshift)
40612115Sdyson    fs->s_fsbtodb = es->s_log_block_size + 1;
40712115Sdyson    V(s_fsbtodb)
40812115Sdyson    fs->s_qbmask = fs->s_blocksize - 1;
40912115Sdyson    V(s_bmask)
41012115Sdyson    fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
41112115Sdyson    V(s_blocksize_bits)
41212115Sdyson    fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
41312115Sdyson    V(s_frag_size)
41412115Sdyson    if (fs->s_frag_size)
41512115Sdyson	fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
41612115Sdyson    V(s_frags_per_block)
41712115Sdyson    fs->s_blocks_per_group = es->s_blocks_per_group;
41812115Sdyson    V(s_blocks_per_group)
41912115Sdyson    fs->s_frags_per_group = es->s_frags_per_group;
42012115Sdyson    V(s_frags_per_group)
42112115Sdyson    fs->s_inodes_per_group = es->s_inodes_per_group;
42212115Sdyson    V(s_inodes_per_group)
42312115Sdyson    fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
42412115Sdyson    V(s_inodes_per_block)
42512115Sdyson    fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
42612115Sdyson    V(s_itb_per_group)
42712115Sdyson    fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
42812115Sdyson    V(s_desc_per_block)
42912115Sdyson    /* s_resuid / s_resgid ? */
43012115Sdyson    fs->s_groups_count = (es->s_blocks_count -
43112115Sdyson			  es->s_first_data_block +
43212115Sdyson			  EXT2_BLOCKS_PER_GROUP(fs) - 1) /
43312115Sdyson			 EXT2_BLOCKS_PER_GROUP(fs);
43412115Sdyson    V(s_groups_count)
43512115Sdyson    db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
43612115Sdyson	EXT2_DESC_PER_BLOCK(fs);
43712115Sdyson    fs->s_db_per_group = db_count;
43812115Sdyson    V(s_db_per_group)
43912115Sdyson
44012115Sdyson    fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
44112115Sdyson		M_UFSMNT, M_WAITOK);
44212115Sdyson
44312115Sdyson    /* adjust logic_sb_block */
44412115Sdyson    if(fs->s_blocksize > SBSIZE)
44512115Sdyson	/* Godmar thinks: if the blocksize is greater than 1024, then
44612115Sdyson	   the superblock is logically part of block zero.
44712115Sdyson	 */
44812115Sdyson        logic_sb_block = 0;
44912115Sdyson
45012115Sdyson    for (i = 0; i < db_count; i++) {
45112115Sdyson	error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
45212115Sdyson		fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
45312115Sdyson	if(error) {
45412115Sdyson	    for (j = 0; j < i; j++)
45512115Sdyson		brelse(fs->s_group_desc[j]);
45612115Sdyson	    bsd_free(fs->s_group_desc, M_UFSMNT);
45712115Sdyson	    printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
45812115Sdyson	    return EIO;
45912115Sdyson	}
46027881Sdyson	/* Set the B_LOCKED flag on the buffer, then brelse() it */
46127881Sdyson	LCK_BUF(fs->s_group_desc[i])
46212115Sdyson    }
46312115Sdyson    if(!ext2_check_descriptors(fs)) {
46412115Sdyson	    for (j = 0; j < db_count; j++)
46527881Sdyson		    ULCK_BUF(fs->s_group_desc[j])
46612115Sdyson	    bsd_free(fs->s_group_desc, M_UFSMNT);
46712115Sdyson	    printf("EXT2-fs: (ext2_check_descriptors failure) "
46812115Sdyson		   "unable to read group descriptors\n");
46912115Sdyson	    return EIO;
47012115Sdyson    }
47112115Sdyson
47212115Sdyson    for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
47312115Sdyson	    fs->s_inode_bitmap_number[i] = 0;
47412115Sdyson	    fs->s_inode_bitmap[i] = NULL;
47512115Sdyson	    fs->s_block_bitmap_number[i] = 0;
47612115Sdyson	    fs->s_block_bitmap[i] = NULL;
47712115Sdyson    }
47812115Sdyson    fs->s_loaded_inode_bitmaps = 0;
47912115Sdyson    fs->s_loaded_block_bitmaps = 0;
48012115Sdyson    return 0;
48112115Sdyson}
48212115Sdyson
48312115Sdyson/*
48412115Sdyson * Reload all incore data for a filesystem (used after running fsck on
48512115Sdyson * the root filesystem and finding things to fix). The filesystem must
48612115Sdyson * be mounted read-only.
48712115Sdyson *
48812115Sdyson * Things to do to update the mount:
48912115Sdyson *	1) invalidate all cached meta-data.
49012115Sdyson *	2) re-read superblock from disk.
49112115Sdyson *	3) re-read summary information from disk.
49212115Sdyson *	4) invalidate all inactive vnodes.
49312115Sdyson *	5) invalidate all cached file data.
49412115Sdyson *	6) re-read inode data for all active vnodes.
49512115Sdyson */
49612911Sphkstatic int
49712115Sdysonext2_reload(mountp, cred, p)
49812115Sdyson	register struct mount *mountp;
49912115Sdyson	struct ucred *cred;
50012115Sdyson	struct proc *p;
50112115Sdyson{
50212115Sdyson	register struct vnode *vp, *nvp, *devvp;
50312115Sdyson	struct inode *ip;
50412115Sdyson	struct buf *bp;
50512115Sdyson	struct ext2_super_block * es;
50612115Sdyson	struct ext2_sb_info *fs;
50712147Sdyson	int error;
50812115Sdyson
50912115Sdyson	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
51012115Sdyson		return (EINVAL);
51112115Sdyson	/*
51212115Sdyson	 * Step 1: invalidate all cached meta-data.
51312115Sdyson	 */
51412115Sdyson	devvp = VFSTOUFS(mountp)->um_devvp;
51512115Sdyson	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
51612115Sdyson		panic("ext2_reload: dirty1");
51712115Sdyson	/*
51812115Sdyson	 * Step 2: re-read superblock from disk.
51912115Sdyson	 * constants have been adjusted for ext2
52012115Sdyson	 */
52143301Sdillon	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
52212115Sdyson		return (error);
52312115Sdyson	es = (struct ext2_super_block *)bp->b_data;
52412115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC) {
52512115Sdyson		if(es->s_magic == EXT2_PRE_02B_MAGIC)
52612115Sdyson		    printf("This filesystem bears the magic number of a pre "
52712115Sdyson			   "0.2b version of ext2. This is not supported by "
52812115Sdyson			   "Lites.\n");
52912115Sdyson		else
53012115Sdyson		    printf("Wrong magic number: %x (expected %x for ext2 fs\n",
53112115Sdyson			es->s_magic, EXT2_SUPER_MAGIC);
53212115Sdyson		brelse(bp);
53312115Sdyson		return (EIO);		/* XXX needs translation */
53412115Sdyson	}
53512115Sdyson	fs = VFSTOUFS(mountp)->um_e2fs;
53612115Sdyson	bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
53712115Sdyson
53843301Sdillon	if((error = compute_sb_data(devvp, es, fs)) != 0) {
53912115Sdyson		brelse(bp);
54012115Sdyson		return error;
54112115Sdyson	}
54212115Sdyson#ifdef UNKLAR
54312115Sdyson	if (fs->fs_sbsize < SBSIZE)
54412115Sdyson		bp->b_flags |= B_INVAL;
54512115Sdyson#endif
54612115Sdyson	brelse(bp);
54712115Sdyson
54812115Sdysonloop:
54939678Sbde	simple_lock(&mntvnode_slock);
55012115Sdyson	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
55139678Sbde		if (vp->v_mount != mountp) {
55239678Sbde			simple_unlock(&mntvnode_slock);
55339678Sbde			goto loop;
55439678Sbde		}
55512115Sdyson		nvp = vp->v_mntvnodes.le_next;
55612115Sdyson		/*
55712115Sdyson		 * Step 4: invalidate all inactive vnodes.
55812115Sdyson		 */
55939678Sbde  		if (vrecycle(vp, &mntvnode_slock, p))
56039678Sbde  			goto loop;
56112115Sdyson		/*
56212115Sdyson		 * Step 5: invalidate all cached file data.
56312115Sdyson		 */
56439678Sbde		simple_lock(&vp->v_interlock);
56539678Sbde		simple_unlock(&mntvnode_slock);
56639678Sbde		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
56712115Sdyson			goto loop;
56839678Sbde		}
56912115Sdyson		if (vinvalbuf(vp, 0, cred, p, 0, 0))
57012115Sdyson			panic("ext2_reload: dirty2");
57112115Sdyson		/*
57212115Sdyson		 * Step 6: re-read inode data for all active vnodes.
57312115Sdyson		 */
57412115Sdyson		ip = VTOI(vp);
57539678Sbde		error =
57612115Sdyson		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
57739678Sbde		    (int)fs->s_blocksize, NOCRED, &bp);
57839678Sbde		if (error) {
57912115Sdyson			vput(vp);
58012115Sdyson			return (error);
58112115Sdyson		}
58212115Sdyson		ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
58339678Sbde		    EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)),
58439678Sbde		    &ip->i_din);
58512115Sdyson		brelse(bp);
58612115Sdyson		vput(vp);
58739678Sbde		simple_lock(&mntvnode_slock);
58812115Sdyson	}
58939678Sbde	simple_unlock(&mntvnode_slock);
59012115Sdyson	return (0);
59112115Sdyson}
59212115Sdyson
59312115Sdyson/*
59412115Sdyson * Common code for mount and mountroot
59512115Sdyson */
59612911Sphkstatic int
59712115Sdysonext2_mountfs(devvp, mp, p)
59812115Sdyson	register struct vnode *devvp;
59912115Sdyson	struct mount *mp;
60012115Sdyson	struct proc *p;
60112115Sdyson{
60212115Sdyson	register struct ufsmount *ump;
60312115Sdyson	struct buf *bp;
60412115Sdyson	register struct ext2_sb_info *fs;
60512115Sdyson	struct ext2_super_block * es;
60612115Sdyson	dev_t dev = devvp->v_rdev;
60712115Sdyson	struct partinfo dpart;
60812115Sdyson	int havepart = 0;
60912115Sdyson	int error, i, size;
61012115Sdyson	int ronly;
61112115Sdyson
61212115Sdyson	/*
61312115Sdyson	 * Disallow multiple mounts of the same device.
61412115Sdyson	 * Disallow mounting of a device that is currently in use
61512115Sdyson	 * (except for root, which might share swap device for miniroot).
61612115Sdyson	 * Flush out any old buffers remaining from a previous use.
61712115Sdyson	 */
61843301Sdillon	if ((error = vfs_mountedon(devvp)) != 0)
61912115Sdyson		return (error);
62012115Sdyson	if (vcount(devvp) > 1 && devvp != rootvp)
62112115Sdyson		return (EBUSY);
62243301Sdillon	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
62312115Sdyson		return (error);
62412115Sdyson#ifdef READONLY
62512115Sdyson/* turn on this to force it to be read-only */
62612115Sdyson	mp->mnt_flag |= MNT_RDONLY;
62712115Sdyson#endif
62812115Sdyson
62912115Sdyson	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
63043301Sdillon	if ((error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) != 0)
63112115Sdyson		return (error);
63212115Sdyson	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
63312115Sdyson		size = DEV_BSIZE;
63412115Sdyson	else {
63512115Sdyson		havepart = 1;
63612115Sdyson		size = dpart.disklab->d_secsize;
63712115Sdyson	}
63812115Sdyson
63912115Sdyson	bp = NULL;
64012115Sdyson	ump = NULL;
64143301Sdillon	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
64212115Sdyson		goto out;
64312115Sdyson	es = (struct ext2_super_block *)bp->b_data;
64412115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC) {
64512115Sdyson		if(es->s_magic == EXT2_PRE_02B_MAGIC)
64612115Sdyson		    printf("This filesystem bears the magic number of a pre "
64712115Sdyson			   "0.2b version of ext2. This is not supported by "
64812115Sdyson			   "Lites.\n");
64912115Sdyson		else
65012115Sdyson		    printf("Wrong magic number: %x (expected %x for EXT2FS)\n",
65112115Sdyson			es->s_magic, EXT2_SUPER_MAGIC);
65212115Sdyson		error = EINVAL;		/* XXX needs translation */
65312115Sdyson		goto out;
65412115Sdyson	}
65539670Sbde	if ((es->s_state & EXT2_VALID_FS) == 0 ||
65639670Sbde	    (es->s_state & EXT2_ERROR_FS)) {
65739670Sbde		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
65839670Sbde			printf(
65939670Sbde"WARNING: Filesystem was not properly dismounted\n");
66039670Sbde		} else {
66139670Sbde			printf(
66239670Sbde"WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
66339670Sbde			error = EPERM;
66439670Sbde			goto out;
66539670Sbde		}
66639670Sbde	}
66712115Sdyson	ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
66812115Sdyson	bzero((caddr_t)ump, sizeof *ump);
66930280Sphk	ump->um_malloctype = M_EXT2NODE;
67030474Sphk	ump->um_blkatoff = ext2_blkatoff;
67130474Sphk	ump->um_truncate = ext2_truncate;
67230492Sphk	ump->um_update = ext2_update;
67330474Sphk	ump->um_valloc = ext2_valloc;
67430474Sphk	ump->um_vfree = ext2_vfree;
67512115Sdyson	/* I don't know whether this is the right strategy. Note that
67612115Sdyson	   we dynamically allocate both a ext2_sb_info and a ext2_super_block
67712115Sdyson	   while Linux keeps the super block in a locked buffer
67812115Sdyson	 */
67912115Sdyson	ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
68012115Sdyson		M_UFSMNT, M_WAITOK);
68112115Sdyson	ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
68212115Sdyson		M_UFSMNT, M_WAITOK);
68312115Sdyson	bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
68439671Sbde	if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
68539671Sbde		goto out;
68639671Sbde	/*
68739671Sbde	 * We don't free the group descriptors allocated by compute_sb_data()
68839671Sbde	 * until ext2_unmount().  This is OK since the mount will succeed.
68939671Sbde	 */
69012115Sdyson	brelse(bp);
69112115Sdyson	bp = NULL;
69212115Sdyson	fs = ump->um_e2fs;
69312115Sdyson	fs->s_rd_only = ronly;	/* ronly is set according to mnt_flags */
69412115Sdyson	/* if the fs is not mounted read-only, make sure the super block is
69512115Sdyson	   always written back on a sync()
69612115Sdyson	 */
69739670Sbde	fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
69812115Sdyson	if (ronly == 0) {
69912115Sdyson		fs->s_dirt = 1;		/* mark it modified */
70012115Sdyson		fs->s_es->s_state &= ~EXT2_VALID_FS;	/* set fs invalid */
70112115Sdyson	}
70212115Sdyson	mp->mnt_data = (qaddr_t)ump;
70312115Sdyson	mp->mnt_stat.f_fsid.val[0] = (long)dev;
70438909Sbde	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
70512115Sdyson	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
70612115Sdyson	mp->mnt_flag |= MNT_LOCAL;
70712115Sdyson	ump->um_mountp = mp;
70812115Sdyson	ump->um_dev = dev;
70912115Sdyson	ump->um_devvp = devvp;
71012115Sdyson	/* setting those two parameters allows us to use
71112115Sdyson	   ufs_bmap w/o changse !
71212115Sdyson	*/
71312115Sdyson	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
71412115Sdyson	ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
71512115Sdyson	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
71612115Sdyson	for (i = 0; i < MAXQUOTAS; i++)
71712115Sdyson		ump->um_quotas[i] = NULLVP;
71834430Seivind	devvp->v_specmountpoint = mp;
71934430Seivind	if (ronly == 0)
72034430Seivind		ext2_sbupdate(ump, MNT_WAIT);
72112115Sdyson	return (0);
72212115Sdysonout:
72312115Sdyson	if (bp)
72412115Sdyson		brelse(bp);
72512115Sdyson	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
72612115Sdyson	if (ump) {
72739671Sbde		bsd_free(ump->um_e2fs->s_es, M_UFSMNT);
72839671Sbde		bsd_free(ump->um_e2fs, M_UFSMNT);
72912115Sdyson		bsd_free(ump, M_UFSMNT);
73012115Sdyson		mp->mnt_data = (qaddr_t)0;
73112115Sdyson	}
73212115Sdyson	return (error);
73312115Sdyson}
73412115Sdyson
73512115Sdyson/*
73612115Sdyson * unmount system call
73712115Sdyson */
73812911Sphkstatic int
73912115Sdysonext2_unmount(mp, mntflags, p)
74012115Sdyson	struct mount *mp;
74112115Sdyson	int mntflags;
74212115Sdyson	struct proc *p;
74312115Sdyson{
74412115Sdyson	register struct ufsmount *ump;
74512115Sdyson	register struct ext2_sb_info *fs;
74612115Sdyson	int error, flags, ronly, i;
74712115Sdyson
74812115Sdyson	flags = 0;
74912115Sdyson	if (mntflags & MNT_FORCE) {
75012115Sdyson		if (mp->mnt_flag & MNT_ROOTFS)
75112115Sdyson			return (EINVAL);
75212115Sdyson		flags |= FORCECLOSE;
75312115Sdyson	}
75443301Sdillon	if ((error = ext2_flushfiles(mp, flags, p)) != 0)
75512115Sdyson		return (error);
75612115Sdyson	ump = VFSTOUFS(mp);
75712115Sdyson	fs = ump->um_e2fs;
75812115Sdyson	ronly = fs->s_rd_only;
75939670Sbde	if (ronly == 0) {
76039670Sbde		if (fs->s_wasvalid)
76139670Sbde			fs->s_es->s_state |= EXT2_VALID_FS;
76212115Sdyson		ext2_sbupdate(ump, MNT_WAIT);
76312115Sdyson	}
76427881Sdyson
76512115Sdyson	/* release buffers containing group descriptors */
76612115Sdyson	for(i = 0; i < fs->s_db_per_group; i++)
76727881Sdyson		ULCK_BUF(fs->s_group_desc[i])
76839671Sbde	bsd_free(fs->s_group_desc, M_UFSMNT);
76927881Sdyson
77012115Sdyson	/* release cached inode/block bitmaps */
77112115Sdyson        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
77212115Sdyson                if (fs->s_inode_bitmap[i])
77327881Sdyson			ULCK_BUF(fs->s_inode_bitmap[i])
77427881Sdyson
77512115Sdyson        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
77612115Sdyson                if (fs->s_block_bitmap[i])
77727881Sdyson			ULCK_BUF(fs->s_block_bitmap[i])
77812115Sdyson
77934430Seivind	ump->um_devvp->v_specmountpoint = NULL;
78012115Sdyson	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
78112115Sdyson		NOCRED, p);
78212115Sdyson	vrele(ump->um_devvp);
78312115Sdyson	bsd_free(fs->s_es, M_UFSMNT);
78412115Sdyson	bsd_free(fs, M_UFSMNT);
78512115Sdyson	bsd_free(ump, M_UFSMNT);
78612115Sdyson	mp->mnt_data = (qaddr_t)0;
78712115Sdyson	mp->mnt_flag &= ~MNT_LOCAL;
78812115Sdyson	return (error);
78912115Sdyson}
79012115Sdyson
79112115Sdyson/*
79212115Sdyson * Flush out all the files in a filesystem.
79312115Sdyson */
79412911Sphkstatic int
79512115Sdysonext2_flushfiles(mp, flags, p)
79612115Sdyson	register struct mount *mp;
79712115Sdyson	int flags;
79812115Sdyson	struct proc *p;
79912115Sdyson{
80012115Sdyson	register struct ufsmount *ump;
80112147Sdyson	int error;
80212746Sbde#if QUOTA
80312746Sbde	int i;
80412746Sbde#endif
80512115Sdyson
80612115Sdyson	ump = VFSTOUFS(mp);
80712115Sdyson#if QUOTA
80812115Sdyson	if (mp->mnt_flag & MNT_QUOTA) {
80943301Sdillon		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
81012115Sdyson			return (error);
81112115Sdyson		for (i = 0; i < MAXQUOTAS; i++) {
81212115Sdyson			if (ump->um_quotas[i] == NULLVP)
81312115Sdyson				continue;
81412115Sdyson			quotaoff(p, mp, i);
81512115Sdyson		}
81612115Sdyson		/*
81712115Sdyson		 * Here we fall through to vflush again to ensure
81812115Sdyson		 * that we have gotten rid of all the system vnodes.
81912115Sdyson		 */
82012115Sdyson	}
82112115Sdyson#endif
82212115Sdyson	error = vflush(mp, NULLVP, flags);
82312115Sdyson	return (error);
82412115Sdyson}
82512115Sdyson
82612115Sdyson/*
82712115Sdyson * Get file system statistics.
82812115Sdyson * taken from ext2/super.c ext2_statfs
82912115Sdyson */
83012911Sphkstatic int
83112115Sdysonext2_statfs(mp, sbp, p)
83212115Sdyson	struct mount *mp;
83312115Sdyson	register struct statfs *sbp;
83412115Sdyson	struct proc *p;
83512115Sdyson{
83612115Sdyson        unsigned long overhead;
83712115Sdyson	unsigned long overhead_per_group;
83812115Sdyson
83912115Sdyson	register struct ufsmount *ump;
84012115Sdyson	register struct ext2_sb_info *fs;
84112115Sdyson	register struct ext2_super_block *es;
84212115Sdyson
84312115Sdyson	ump = VFSTOUFS(mp);
84412115Sdyson	fs = ump->um_e2fs;
84512115Sdyson	es = fs->s_es;
84612115Sdyson
84712115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC)
84812115Sdyson		panic("ext2_statfs - magic number spoiled");
84912115Sdyson
85012115Sdyson	/*
85112115Sdyson	 * Compute the overhead (FS structures)
85212115Sdyson	 */
85312115Sdyson	overhead_per_group = 1 /* super block */ +
85412115Sdyson			     fs->s_db_per_group +
85512115Sdyson			     1 /* block bitmap */ +
85612115Sdyson			     1 /* inode bitmap */ +
85712115Sdyson			     fs->s_itb_per_group;
85812115Sdyson	overhead = es->s_first_data_block +
85912115Sdyson		   fs->s_groups_count * overhead_per_group;
86012115Sdyson
86112115Sdyson	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
86212115Sdyson	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
86312115Sdyson	sbp->f_blocks = es->s_blocks_count - overhead;
86412115Sdyson	sbp->f_bfree = es->s_free_blocks_count;
86512115Sdyson	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
86612115Sdyson	sbp->f_files = es->s_inodes_count;
86712115Sdyson	sbp->f_ffree = es->s_free_inodes_count;
86812115Sdyson	if (sbp != &mp->mnt_stat) {
86938909Sbde		sbp->f_type = mp->mnt_vfc->vfc_typenum;
87012115Sdyson		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
87112115Sdyson			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
87212115Sdyson		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
87312115Sdyson			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
87412115Sdyson	}
87512115Sdyson	return (0);
87612115Sdyson}
87712115Sdyson
87812115Sdyson/*
87912115Sdyson * Go through the disk queues to initiate sandbagged IO;
88012115Sdyson * go through the inodes to write those that have been modified;
88112115Sdyson * initiate the writing of the super block if it has been modified.
88212115Sdyson *
88312115Sdyson * Note: we are always called with the filesystem marked `MPBUSY'.
88412115Sdyson */
88512911Sphkstatic int
88612115Sdysonext2_sync(mp, waitfor, cred, p)
88712115Sdyson	struct mount *mp;
88812115Sdyson	int waitfor;
88912115Sdyson	struct ucred *cred;
89012115Sdyson	struct proc *p;
89112115Sdyson{
89239678Sbde	struct vnode *nvp, *vp;
89339678Sbde	struct inode *ip;
89439678Sbde	struct ufsmount *ump = VFSTOUFS(mp);
89539678Sbde	struct ext2_sb_info *fs;
89612115Sdyson	int error, allerror = 0;
89712115Sdyson
89812115Sdyson	fs = ump->um_e2fs;
89939678Sbde	if (fs->s_dirt != 0 && fs->s_rd_only != 0) {		/* XXX */
90039678Sbde		printf("fs = %s\n", fs->fs_fsmnt);
90139678Sbde		panic("ext2_sync: rofs mod");
90212115Sdyson	}
90312115Sdyson	/*
90412115Sdyson	 * Write back each (modified) inode.
90512115Sdyson	 */
90639678Sbde	simple_lock(&mntvnode_slock);
90712115Sdysonloop:
90839678Sbde	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
90912115Sdyson		/*
91012115Sdyson		 * If the vnode that we are about to sync is no longer
91112115Sdyson		 * associated with this mount point, start over.
91212115Sdyson		 */
91312115Sdyson		if (vp->v_mount != mp)
91412115Sdyson			goto loop;
91539678Sbde		simple_lock(&vp->v_interlock);
91639678Sbde		nvp = vp->v_mntvnodes.le_next;
91712115Sdyson		ip = VTOI(vp);
91839678Sbde		if (vp->v_type == VNON ||
91943309Sdillon		    ((ip->i_flag &
92012115Sdyson		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
92143395Sbde		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
92239678Sbde			simple_unlock(&vp->v_interlock);
92312115Sdyson			continue;
92439678Sbde		}
92539678Sbde		simple_unlock(&mntvnode_slock);
92639678Sbde		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
92739678Sbde		if (error) {
92839678Sbde			simple_lock(&mntvnode_slock);
92939678Sbde			if (error == ENOENT)
93039678Sbde				goto loop;
93139678Sbde			continue;
93239678Sbde		}
93343301Sdillon		if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
93412115Sdyson			allerror = error;
93539678Sbde		VOP_UNLOCK(vp, 0, p);
93639678Sbde		vrele(vp);
93739678Sbde		simple_lock(&mntvnode_slock);
93812115Sdyson	}
93939678Sbde	simple_unlock(&mntvnode_slock);
94012115Sdyson	/*
94112115Sdyson	 * Force stale file system control information to be flushed.
94212115Sdyson	 */
94339678Sbde	if (waitfor != MNT_LAZY) {
94439678Sbde		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
94539678Sbde		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
94639678Sbde			allerror = error;
94739678Sbde		VOP_UNLOCK(ump->um_devvp, 0, p);
94839678Sbde	}
94912115Sdyson#if QUOTA
95012115Sdyson	qsync(mp);
95112115Sdyson#endif
95239678Sbde	/*
95339678Sbde	 * Write back modified superblock.
95439678Sbde	 */
95539678Sbde	if (fs->s_dirt != 0) {
95639678Sbde		fs->s_dirt = 0;
95739678Sbde		fs->s_es->s_wtime = time_second;
95839678Sbde		if ((error = ext2_sbupdate(ump, waitfor)) != 0)
95939678Sbde			allerror = error;
96039678Sbde	}
96112115Sdyson	return (allerror);
96212115Sdyson}
96312115Sdyson
96412115Sdyson/*
96512115Sdyson * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
96612115Sdyson * in from disk.  If it is in core, wait for the lock bit to clear, then
96712115Sdyson * return the inode locked.  Detection and handling of mount points must be
96812115Sdyson * done by the calling routine.
96912115Sdyson */
97012911Sphkstatic int
97112115Sdysonext2_vget(mp, ino, vpp)
97212115Sdyson	struct mount *mp;
97312115Sdyson	ino_t ino;
97412115Sdyson	struct vnode **vpp;
97512115Sdyson{
97612115Sdyson	register struct ext2_sb_info *fs;
97712115Sdyson	register struct inode *ip;
97812115Sdyson	struct ufsmount *ump;
97912115Sdyson	struct buf *bp;
98012115Sdyson	struct vnode *vp;
98112115Sdyson	dev_t dev;
98230280Sphk	int i, error;
98312115Sdyson	int used_blocks;
98412115Sdyson
98512115Sdyson	ump = VFSTOUFS(mp);
98612115Sdyson	dev = ump->um_dev;
98712406Sdysonrestart:
98812115Sdyson	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
98912115Sdyson		return (0);
99012115Sdyson
99112406Sdyson	/*
99212406Sdyson	 * Lock out the creation of new entries in the FFS hash table in
99312406Sdyson	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
99412406Sdyson	 * may occur!
99512406Sdyson	 */
99612406Sdyson	if (ext2fs_inode_hash_lock) {
99712406Sdyson		while (ext2fs_inode_hash_lock) {
99812406Sdyson			ext2fs_inode_hash_lock = -1;
99936102Sbde			tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0);
100012406Sdyson		}
100112406Sdyson		goto restart;
100212406Sdyson	}
100312406Sdyson	ext2fs_inode_hash_lock = 1;
100412406Sdyson
100536102Sbde	/*
100636102Sbde	 * If this MALLOC() is performed after the getnewvnode()
100736102Sbde	 * it might block, leaving a vnode with a NULL v_data to be
100836102Sbde	 * found by ext2_sync() if a sync happens to fire right then,
100936102Sbde	 * which will cause a panic because ext2_sync() blindly
101036102Sbde	 * dereferences vp->v_data (as well it should).
101136102Sbde	 */
101236102Sbde	MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
101336102Sbde
101412115Sdyson	/* Allocate a new vnode/inode. */
101543301Sdillon	if ((error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) != 0) {
101636102Sbde		if (ext2fs_inode_hash_lock < 0)
101736102Sbde			wakeup(&ext2fs_inode_hash_lock);
101836102Sbde		ext2fs_inode_hash_lock = 0;
101912115Sdyson		*vpp = NULL;
102036102Sbde		FREE(ip, M_EXT2NODE);
102112115Sdyson		return (error);
102212115Sdyson	}
102312115Sdyson	bzero((caddr_t)ip, sizeof(struct inode));
102438998Sbde	lockinit(&ip->i_lock, PINOD, "ext2in", 0, 0);
102512115Sdyson	vp->v_data = ip;
102612115Sdyson	ip->i_vnode = vp;
102712115Sdyson	ip->i_e2fs = fs = ump->um_e2fs;
102812115Sdyson	ip->i_dev = dev;
102912115Sdyson	ip->i_number = ino;
103012115Sdyson#if QUOTA
103112115Sdyson	for (i = 0; i < MAXQUOTAS; i++)
103212115Sdyson		ip->i_dquot[i] = NODQUOT;
103312115Sdyson#endif
103412115Sdyson	/*
103512115Sdyson	 * Put it onto its hash chain and lock it so that other requests for
103612115Sdyson	 * this inode will block if they arrive while we are sleeping waiting
103712115Sdyson	 * for old data structures to be purged or for the contents of the
103812115Sdyson	 * disk portion of this inode to be read.
103912115Sdyson	 */
104012115Sdyson	ufs_ihashins(ip);
104112115Sdyson
104212406Sdyson	if (ext2fs_inode_hash_lock < 0)
104312406Sdyson		wakeup(&ext2fs_inode_hash_lock);
104412406Sdyson	ext2fs_inode_hash_lock = 0;
104512406Sdyson
104612115Sdyson	/* Read in the disk contents for the inode, copy into the inode. */
104712115Sdyson#if 0
104812115Sdysonprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
104912115Sdyson#endif
105043301Sdillon	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
105143301Sdillon	    (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
105212115Sdyson		/*
105312115Sdyson		 * The inode does not contain anything useful, so it would
105412115Sdyson		 * be misleading to leave it on its hash chain. With mode
105512115Sdyson		 * still zero, it will be unlinked and returned to the free
105612115Sdyson		 * list by vput().
105712115Sdyson		 */
105812115Sdyson		vput(vp);
105912115Sdyson		brelse(bp);
106012115Sdyson		*vpp = NULL;
106112115Sdyson		return (error);
106212115Sdyson	}
106312115Sdyson	/* convert ext2 inode to dinode */
106412115Sdyson	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
106512115Sdyson			ino_to_fsbo(fs, ino)), &ip->i_din);
106612115Sdyson	ip->i_block_group = ino_to_cg(fs, ino);
106712115Sdyson	ip->i_next_alloc_block = 0;
106812115Sdyson	ip->i_next_alloc_goal = 0;
106912115Sdyson	ip->i_prealloc_count = 0;
107012115Sdyson	ip->i_prealloc_block = 0;
107112115Sdyson        /* now we want to make sure that block pointers for unused
107212115Sdyson           blocks are zeroed out - ext2_balloc depends on this
107312115Sdyson	   although for regular files and directories only
107412115Sdyson	*/
107512115Sdyson	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
107612115Sdyson		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
107712115Sdyson		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
107812115Sdyson			ip->i_db[i] = 0;
107912115Sdyson	}
108012115Sdyson/*
108112115Sdyson	ext2_print_inode(ip);
108212115Sdyson*/
108312115Sdyson	brelse(bp);
108412115Sdyson
108512115Sdyson	/*
108612115Sdyson	 * Initialize the vnode from the inode, check for aliases.
108712115Sdyson	 * Note that the underlying vnode may have changed.
108812115Sdyson	 */
108943301Sdillon	if ((error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) != 0) {
109012115Sdyson		vput(vp);
109112115Sdyson		*vpp = NULL;
109212115Sdyson		return (error);
109312115Sdyson	}
109412115Sdyson	/*
109512115Sdyson	 * Finish inode initialization now that aliasing has been resolved.
109612115Sdyson	 */
109712115Sdyson	ip->i_devvp = ump->um_devvp;
109812115Sdyson	VREF(ip->i_devvp);
109912115Sdyson	/*
110012115Sdyson	 * Set up a generation number for this inode if it does not
110112115Sdyson	 * already have one. This should only happen on old filesystems.
110212115Sdyson	 */
110312115Sdyson	if (ip->i_gen == 0) {
110431485Sbde		ip->i_gen = random() / 2 + 1;
110512115Sdyson		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
110612115Sdyson			ip->i_flag |= IN_MODIFIED;
110712115Sdyson	}
110812115Sdyson	*vpp = vp;
110912115Sdyson	return (0);
111012115Sdyson}
111112115Sdyson
111212115Sdyson/*
111312115Sdyson * File handle to vnode
111412115Sdyson *
111512115Sdyson * Have to be really careful about stale file handles:
111612115Sdyson * - check that the inode number is valid
111712115Sdyson * - call ext2_vget() to get the locked inode
111812115Sdyson * - check for an unallocated inode (i_mode == 0)
111912115Sdyson * - check that the given client host has export rights and return
112012115Sdyson *   those rights via. exflagsp and credanonp
112112115Sdyson */
112212911Sphkstatic int
112312115Sdysonext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
112412115Sdyson	register struct mount *mp;
112512115Sdyson	struct fid *fhp;
112628270Swollman	struct sockaddr *nam;
112712115Sdyson	struct vnode **vpp;
112812115Sdyson	int *exflagsp;
112912115Sdyson	struct ucred **credanonp;
113012115Sdyson{
113112115Sdyson	register struct ufid *ufhp;
113212115Sdyson	struct ext2_sb_info *fs;
113312115Sdyson
113412115Sdyson	ufhp = (struct ufid *)fhp;
113512115Sdyson	fs = VFSTOUFS(mp)->um_e2fs;
113612115Sdyson	if (ufhp->ufid_ino < ROOTINO ||
113712115Sdyson	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
113812115Sdyson		return (ESTALE);
113912115Sdyson	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
114012115Sdyson}
114112115Sdyson
114212115Sdyson/*
114312115Sdyson * Vnode pointer to File handle
114412115Sdyson */
114512115Sdyson/* ARGSUSED */
114612911Sphkstatic int
114712115Sdysonext2_vptofh(vp, fhp)
114812115Sdyson	struct vnode *vp;
114912115Sdyson	struct fid *fhp;
115012115Sdyson{
115112115Sdyson	register struct inode *ip;
115212115Sdyson	register struct ufid *ufhp;
115312115Sdyson
115412115Sdyson	ip = VTOI(vp);
115512115Sdyson	ufhp = (struct ufid *)fhp;
115612115Sdyson	ufhp->ufid_len = sizeof(struct ufid);
115712115Sdyson	ufhp->ufid_ino = ip->i_number;
115812115Sdyson	ufhp->ufid_gen = ip->i_gen;
115912115Sdyson	return (0);
116012115Sdyson}
116112115Sdyson
116212115Sdyson/*
116312115Sdyson * Write a superblock and associated information back to disk.
116412115Sdyson */
116512911Sphkstatic int
116612115Sdysonext2_sbupdate(mp, waitfor)
116712115Sdyson	struct ufsmount *mp;
116812115Sdyson	int waitfor;
116912115Sdyson{
117012115Sdyson	register struct ext2_sb_info *fs = mp->um_e2fs;
117112115Sdyson	register struct ext2_super_block *es = fs->s_es;
117212115Sdyson	register struct buf *bp;
117341591Sarchie	int error = 0;
117412115Sdyson/*
117512115Sdysonprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
117612115Sdyson*/
117712115Sdyson	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
117812115Sdyson	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
117912115Sdyson	if (waitfor == MNT_WAIT)
118012115Sdyson		error = bwrite(bp);
118112115Sdyson	else
118212115Sdyson		bawrite(bp);
118312115Sdyson
118427881Sdyson	/*
118527881Sdyson	 * The buffers for group descriptors, inode bitmaps and block bitmaps
118627881Sdyson	 * are not busy at this point and are (hopefully) written by the
118727881Sdyson	 * usual sync mechanism. No need to write them here
118812115Sdyson		 */
118912115Sdyson
119012115Sdyson	return (error);
119112115Sdyson}
1192