ext2_vfsops.c revision 29906
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
4212115Sdyson#if !defined(__FreeBSD__)
4312115Sdyson#include "quota.h"
4413260Swollman#else
4513260Swollman#include "opt_quota.h"
4612115Sdyson#endif
4712115Sdyson
4812115Sdyson#include <sys/param.h>
4912115Sdyson#include <sys/systm.h>
5012115Sdyson#include <sys/namei.h>
5112115Sdyson#include <sys/proc.h>
5212115Sdyson#include <sys/kernel.h>
5312115Sdyson#include <sys/vnode.h>
5412115Sdyson#include <sys/mount.h>
5512115Sdyson#include <sys/buf.h>
5629906Skato#include <sys/conf.h>
5724131Sbde#include <sys/fcntl.h>
5812115Sdyson#include <sys/disklabel.h>
5912115Sdyson#include <sys/malloc.h>
6012115Sdyson#include <sys/stat.h>
6112115Sdyson
6212115Sdyson#include <miscfs/specfs/specdev.h>
6312115Sdyson
6412115Sdyson#include <ufs/ufs/quota.h>
6512115Sdyson#include <ufs/ufs/ufsmount.h>
6612115Sdyson#include <ufs/ufs/inode.h>
6712115Sdyson#include <ufs/ufs/ufs_extern.h>
6812115Sdyson
6912115Sdyson#include <gnu/ext2fs/fs.h>
7012115Sdyson#include <gnu/ext2fs/ext2_extern.h>
7112115Sdyson#include <gnu/ext2fs/ext2_fs.h>
7212115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h>
7312115Sdyson
7428270Swollmanstatic int ext2_fhtovp __P((struct mount *, struct fid *, struct sockaddr *,
7512911Sphk	    struct vnode **, int *, struct ucred **));
7612911Sphkstatic int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p));
7712911Sphkstatic int ext2_mount __P((struct mount *,
7812911Sphk	    char *, caddr_t, struct nameidata *, struct proc *));
7912911Sphkstatic int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *));
8012911Sphkstatic int ext2_reload __P((struct mount *mountp, struct ucred *cred,
8112911Sphk			struct proc *p));
8212911Sphkstatic int ext2_sbupdate __P((struct ufsmount *, int));
8312911Sphkstatic int ext2_statfs __P((struct mount *, struct statfs *, struct proc *));
8412911Sphkstatic int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *));
8512911Sphkstatic int ext2_unmount __P((struct mount *, int, struct proc *));
8612911Sphkstatic int ext2_vget __P((struct mount *, ino_t, struct vnode **));
8712911Sphkstatic int ext2_vptofh __P((struct vnode *, struct fid *));
8812115Sdyson
8912911Sphkstatic struct vfsops ext2fs_vfsops = {
9012115Sdyson	ext2_mount,
9112115Sdyson	ufs_start,		/* empty function */
9212115Sdyson	ext2_unmount,
9312115Sdyson	ufs_root,		/* root inode via vget */
9412115Sdyson	ufs_quotactl,		/* does operations associated with quotas */
9512115Sdyson	ext2_statfs,
9612115Sdyson	ext2_sync,
9712115Sdyson	ext2_vget,
9812115Sdyson	ext2_fhtovp,
9912115Sdyson	ext2_vptofh,
10012115Sdyson	ext2_init,
10112115Sdyson};
10212115Sdyson
10312115Sdyson#if defined(__FreeBSD__)
10412115SdysonVFS_SET(ext2fs_vfsops, ext2fs, MOUNT_EXT2FS, 0);
10512115Sdyson#define bsd_malloc malloc
10612115Sdyson#define bsd_free free
10712115Sdyson#endif
10812115Sdyson
10912115Sdysonextern u_long nextgennumber;
11012406Sdyson#ifdef __FreeBSD__
11112911Sphkstatic int ext2fs_inode_hash_lock;
11212406Sdyson#endif
11312115Sdyson
11416322Sgpalmerstatic int	compute_sb_data __P((struct vnode * devvp,
11516322Sgpalmer				     struct ext2_super_block * es,
11616322Sgpalmer				     struct ext2_sb_info * fs));
11716322Sgpalmer
11816322Sgpalmer#ifdef notyet
11916322Sgpalmerstatic int ext2_mountroot __P((void));
12016322Sgpalmer
12112115Sdyson/*
12216322Sgpalmer * Called by main() when ext2fs is going to be mounted as root.
12312115Sdyson *
12412115Sdyson * Name is updated by mount(8) after booting.
12512115Sdyson */
12612115Sdyson#define ROOTNAME	"root_device"
12712115Sdyson
12812911Sphkstatic int
12912115Sdysonext2_mountroot()
13012115Sdyson{
13112115Sdyson#if !defined(__FreeBSD__)
13212115Sdyson	extern struct vnode *rootvp;
13312115Sdyson#endif
13412115Sdyson	register struct ext2_sb_info *fs;
13512115Sdyson	register struct mount *mp;
13612115Sdyson#if defined(__FreeBSD__)
13712115Sdyson	struct proc *p = curproc;
13812115Sdyson#else
13912115Sdyson	struct proc *p = get_proc();	/* XXX */
14012115Sdyson#endif
14112115Sdyson	struct ufsmount *ump;
14212115Sdyson	u_int size;
14312115Sdyson	int error;
14412115Sdyson
14529208Sbde	if ((error = bdevvp(rootdev, &rootvp))) {
14629208Sbde		printf("ext2_mountroot: can't find rootvp");
14729208Sbde		return (error);
14829208Sbde	}
14912115Sdyson	mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
15012115Sdyson	bzero((char *)mp, (u_long)sizeof(struct mount));
15112115Sdyson	mp->mnt_op = &ext2fs_vfsops;
15212115Sdyson	mp->mnt_flag = MNT_RDONLY;
15329888Skato	if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERR)
15429888Skato		mp->mnt_flag |= MNT_NOCLUSTERR;
15529888Skato	if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERW)
15629888Skato		mp->mnt_flag |= MNT_NOCLUSTERW;
15712115Sdyson	if (error = ext2_mountfs(rootvp, mp, p)) {
15812115Sdyson		bsd_free(mp, M_MOUNT);
15912115Sdyson		return (error);
16012115Sdyson	}
16112115Sdyson	if (error = vfs_lock(mp)) {
16212115Sdyson		(void)ext2_unmount(mp, 0, p);
16312115Sdyson		bsd_free(mp, M_MOUNT);
16412115Sdyson		return (error);
16512115Sdyson	}
16612115Sdyson#if defined(__FreeBSD__)
16712115Sdyson	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
16812115Sdyson#else
16912115Sdyson	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
17012115Sdyson#endif
17112115Sdyson	mp->mnt_flag |= MNT_ROOTFS;
17212115Sdyson	mp->mnt_vnodecovered = NULLVP;
17312115Sdyson	ump = VFSTOUFS(mp);
17412115Sdyson	fs = ump->um_e2fs;
17512115Sdyson	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
17612115Sdyson	fs->fs_fsmnt[0] = '/';
17712115Sdyson	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
17812115Sdyson	    MNAMELEN);
17912115Sdyson	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
18012115Sdyson	    &size);
18112115Sdyson	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
18212115Sdyson	(void)ext2_statfs(mp, &mp->mnt_stat, p);
18312115Sdyson	vfs_unlock(mp);
18412115Sdyson	inittodr(fs->s_es->s_wtime);		/* this helps to set the time */
18512115Sdyson	return (0);
18612115Sdyson}
18716322Sgpalmer#endif
18812115Sdyson
18912115Sdyson/*
19012115Sdyson * VFS Operations.
19112115Sdyson *
19212115Sdyson * mount system call
19312115Sdyson */
19412911Sphkstatic int
19512115Sdysonext2_mount(mp, path, data, ndp, p)
19612115Sdyson	register struct mount *mp;
19712115Sdyson	char *path;
19812115Sdyson	caddr_t data;		/* this is actually a (struct ufs_args *) */
19912115Sdyson	struct nameidata *ndp;
20012115Sdyson	struct proc *p;
20112115Sdyson{
20212115Sdyson	struct vnode *devvp;
20312115Sdyson	struct ufs_args args;
20412115Sdyson	struct ufsmount *ump = 0;
20512115Sdyson	register struct ext2_sb_info *fs;
20612115Sdyson	u_int size;
20712115Sdyson	int error, flags;
20812115Sdyson
20912115Sdyson	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
21012115Sdyson		return (error);
21112115Sdyson	/*
21212115Sdyson	 * If updating, check whether changing from read-only to
21312115Sdyson	 * read/write; if there is no device name, that's all we do.
21429888Skato	 * Disallow clearing MNT_NOCLUSTERR and MNT_NOCLUSTERW flags,
21529888Skato	 * if block device requests.
21612115Sdyson	 */
21712115Sdyson	if (mp->mnt_flag & MNT_UPDATE) {
21812115Sdyson		ump = VFSTOUFS(mp);
21912115Sdyson		fs = ump->um_e2fs;
22012115Sdyson		error = 0;
22129888Skato		if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERR)
22229888Skato			mp->mnt_flag |= MNT_NOCLUSTERR;
22329888Skato		if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERW)
22429888Skato			mp->mnt_flag |= MNT_NOCLUSTERW;
22512115Sdyson		if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
22612115Sdyson			flags = WRITECLOSE;
22712115Sdyson			if (mp->mnt_flag & MNT_FORCE)
22812115Sdyson				flags |= FORCECLOSE;
22922521Sdyson			if (vfs_busy(mp, LK_NOWAIT, 0, p))
23012115Sdyson				return (EBUSY);
23112115Sdyson			error = ext2_flushfiles(mp, flags, p);
23222521Sdyson			vfs_unbusy(mp, p);
23312115Sdyson		}
23412115Sdyson		if (!error && (mp->mnt_flag & MNT_RELOAD))
23512115Sdyson			error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p);
23612115Sdyson		if (error)
23712115Sdyson			return (error);
23812115Sdyson		if (fs->s_rd_only && (mp->mnt_flag & MNT_WANTRDWR))
23912115Sdyson			fs->s_rd_only = 0;
24012115Sdyson		if (fs->s_rd_only == 0) {
24112115Sdyson			/* don't say it's clean */
24212115Sdyson			fs->s_es->s_state &= ~EXT2_VALID_FS;
24312115Sdyson			ext2_sbupdate(ump, MNT_WAIT);
24412115Sdyson		}
24512115Sdyson		if (args.fspec == 0) {
24612115Sdyson			/*
24712115Sdyson			 * Process export requests.
24812115Sdyson			 */
24912115Sdyson			return (vfs_export(mp, &ump->um_export, &args.export));
25012115Sdyson		}
25112115Sdyson	}
25212115Sdyson	/*
25312115Sdyson	 * Not an update, or updating the name: look up the name
25412115Sdyson	 * and verify that it refers to a sensible block device.
25512115Sdyson	 */
25612115Sdyson	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
25712115Sdyson	if (error = namei(ndp))
25812115Sdyson		return (error);
25912115Sdyson	devvp = ndp->ni_vp;
26012115Sdyson
26112115Sdyson	if (devvp->v_type != VBLK) {
26212115Sdyson		vrele(devvp);
26312115Sdyson		return (ENOTBLK);
26412115Sdyson	}
26512115Sdyson	if (major(devvp->v_rdev) >= nblkdev) {
26612115Sdyson		vrele(devvp);
26712115Sdyson		return (ENXIO);
26812115Sdyson	}
26929888Skato	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
27029888Skato		if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERR)
27129888Skato			mp->mnt_flag |= MNT_NOCLUSTERR;
27229888Skato		if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERW)
27329888Skato			mp->mnt_flag |= MNT_NOCLUSTERW;
27412115Sdyson		error = ext2_mountfs(devvp, mp, p);
27529888Skato	} else {
27612115Sdyson		if (devvp != ump->um_devvp)
27712115Sdyson			error = EINVAL;	/* needs translation */
27812115Sdyson		else
27912115Sdyson			vrele(devvp);
28012115Sdyson	}
28112115Sdyson	if (error) {
28212115Sdyson		vrele(devvp);
28312115Sdyson		return (error);
28412115Sdyson	}
28512115Sdyson	ump = VFSTOUFS(mp);
28612115Sdyson	fs = ump->um_e2fs;
28712115Sdyson	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
28812115Sdyson	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
28912115Sdyson	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
29012115Sdyson	    MNAMELEN);
29112115Sdyson	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
29212115Sdyson	    &size);
29312115Sdyson	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
29412115Sdyson	(void)ext2_statfs(mp, &mp->mnt_stat, p);
29512115Sdyson	return (0);
29612115Sdyson}
29712115Sdyson
29812115Sdyson/*
29912115Sdyson * checks that the data in the descriptor blocks make sense
30012115Sdyson * this is taken from ext2/super.c
30112115Sdyson */
30212115Sdysonstatic int ext2_check_descriptors (struct ext2_sb_info * sb)
30312115Sdyson{
30412115Sdyson        int i;
30512115Sdyson        int desc_block = 0;
30612115Sdyson        unsigned long block = sb->s_es->s_first_data_block;
30712115Sdyson        struct ext2_group_desc * gdp = NULL;
30812115Sdyson
30912115Sdyson        /* ext2_debug ("Checking group descriptors"); */
31012115Sdyson
31112115Sdyson        for (i = 0; i < sb->s_groups_count; i++)
31212115Sdyson        {
31312115Sdyson		/* examine next descriptor block */
31412115Sdyson                if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
31512115Sdyson                        gdp = (struct ext2_group_desc *)
31612115Sdyson				sb->s_group_desc[desc_block++]->b_data;
31712115Sdyson                if (gdp->bg_block_bitmap < block ||
31812115Sdyson                    gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
31912115Sdyson                {
32012115Sdyson                        printf ("ext2_check_descriptors: "
32112115Sdyson                                    "Block bitmap for group %d"
32212115Sdyson                                    " not in group (block %lu)!",
32312115Sdyson                                    i, (unsigned long) gdp->bg_block_bitmap);
32412115Sdyson                        return 0;
32512115Sdyson                }
32612115Sdyson                if (gdp->bg_inode_bitmap < block ||
32712115Sdyson                    gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
32812115Sdyson                {
32912115Sdyson                        printf ("ext2_check_descriptors: "
33012115Sdyson                                    "Inode bitmap for group %d"
33112115Sdyson                                    " not in group (block %lu)!",
33212115Sdyson                                    i, (unsigned long) gdp->bg_inode_bitmap);
33312115Sdyson                        return 0;
33412115Sdyson                }
33512115Sdyson                if (gdp->bg_inode_table < block ||
33612115Sdyson                    gdp->bg_inode_table + sb->s_itb_per_group >=
33712115Sdyson                    block + EXT2_BLOCKS_PER_GROUP(sb))
33812115Sdyson                {
33912115Sdyson                        printf ("ext2_check_descriptors: "
34012115Sdyson                                    "Inode table for group %d"
34112115Sdyson                                    " not in group (block %lu)!",
34212115Sdyson                                    i, (unsigned long) gdp->bg_inode_table);
34312115Sdyson                        return 0;
34412115Sdyson                }
34512115Sdyson                block += EXT2_BLOCKS_PER_GROUP(sb);
34612115Sdyson                gdp++;
34712115Sdyson        }
34812115Sdyson        return 1;
34912115Sdyson}
35012115Sdyson
35112115Sdyson/*
35212115Sdyson * this computes the fields of the  ext2_sb_info structure from the
35312115Sdyson * data in the ext2_super_block structure read in
35412115Sdyson */
35512115Sdysonstatic int compute_sb_data(devvp, es, fs)
35612115Sdyson	struct vnode * devvp;
35712115Sdyson	struct ext2_super_block * es;
35812115Sdyson	struct ext2_sb_info * fs;
35912115Sdyson{
36012115Sdyson    int db_count, error;
36112115Sdyson    int i, j;
36212115Sdyson    int logic_sb_block = 1;	/* XXX for now */
36312115Sdyson
36412115Sdyson#if 1
36512115Sdyson#define V(v)
36612115Sdyson#else
36712115Sdyson#define V(v)  printf(#v"= %d\n", fs->v);
36812115Sdyson#endif
36912115Sdyson
37012115Sdyson    fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
37112115Sdyson    V(s_blocksize)
37212115Sdyson    fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
37312115Sdyson    V(s_bshift)
37412115Sdyson    fs->s_fsbtodb = es->s_log_block_size + 1;
37512115Sdyson    V(s_fsbtodb)
37612115Sdyson    fs->s_qbmask = fs->s_blocksize - 1;
37712115Sdyson    V(s_bmask)
37812115Sdyson    fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
37912115Sdyson    V(s_blocksize_bits)
38012115Sdyson    fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
38112115Sdyson    V(s_frag_size)
38212115Sdyson    if (fs->s_frag_size)
38312115Sdyson	fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
38412115Sdyson    V(s_frags_per_block)
38512115Sdyson    fs->s_blocks_per_group = es->s_blocks_per_group;
38612115Sdyson    V(s_blocks_per_group)
38712115Sdyson    fs->s_frags_per_group = es->s_frags_per_group;
38812115Sdyson    V(s_frags_per_group)
38912115Sdyson    fs->s_inodes_per_group = es->s_inodes_per_group;
39012115Sdyson    V(s_inodes_per_group)
39112115Sdyson    fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
39212115Sdyson    V(s_inodes_per_block)
39312115Sdyson    fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
39412115Sdyson    V(s_itb_per_group)
39512115Sdyson    fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
39612115Sdyson    V(s_desc_per_block)
39712115Sdyson    /* s_resuid / s_resgid ? */
39812115Sdyson    fs->s_groups_count = (es->s_blocks_count -
39912115Sdyson			  es->s_first_data_block +
40012115Sdyson			  EXT2_BLOCKS_PER_GROUP(fs) - 1) /
40112115Sdyson			 EXT2_BLOCKS_PER_GROUP(fs);
40212115Sdyson    V(s_groups_count)
40312115Sdyson    db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
40412115Sdyson	EXT2_DESC_PER_BLOCK(fs);
40512115Sdyson    fs->s_db_per_group = db_count;
40612115Sdyson    V(s_db_per_group)
40712115Sdyson
40812115Sdyson    fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
40912115Sdyson		M_UFSMNT, M_WAITOK);
41012115Sdyson
41112115Sdyson    /* adjust logic_sb_block */
41212115Sdyson    if(fs->s_blocksize > SBSIZE)
41312115Sdyson	/* Godmar thinks: if the blocksize is greater than 1024, then
41412115Sdyson	   the superblock is logically part of block zero.
41512115Sdyson	 */
41612115Sdyson        logic_sb_block = 0;
41712115Sdyson
41812115Sdyson    for (i = 0; i < db_count; i++) {
41912115Sdyson	error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
42012115Sdyson		fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
42112115Sdyson	if(error) {
42212115Sdyson	    for (j = 0; j < i; j++)
42312115Sdyson		brelse(fs->s_group_desc[j]);
42412115Sdyson	    bsd_free(fs->s_group_desc, M_UFSMNT);
42512115Sdyson	    printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
42612115Sdyson	    return EIO;
42712115Sdyson	}
42827881Sdyson	/* Set the B_LOCKED flag on the buffer, then brelse() it */
42927881Sdyson	LCK_BUF(fs->s_group_desc[i])
43012115Sdyson    }
43112115Sdyson    if(!ext2_check_descriptors(fs)) {
43212115Sdyson	    for (j = 0; j < db_count; j++)
43327881Sdyson		    ULCK_BUF(fs->s_group_desc[j])
43412115Sdyson	    bsd_free(fs->s_group_desc, M_UFSMNT);
43512115Sdyson	    printf("EXT2-fs: (ext2_check_descriptors failure) "
43612115Sdyson		   "unable to read group descriptors\n");
43712115Sdyson	    return EIO;
43812115Sdyson    }
43912115Sdyson
44012115Sdyson    for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
44112115Sdyson	    fs->s_inode_bitmap_number[i] = 0;
44212115Sdyson	    fs->s_inode_bitmap[i] = NULL;
44312115Sdyson	    fs->s_block_bitmap_number[i] = 0;
44412115Sdyson	    fs->s_block_bitmap[i] = NULL;
44512115Sdyson    }
44612115Sdyson    fs->s_loaded_inode_bitmaps = 0;
44712115Sdyson    fs->s_loaded_block_bitmaps = 0;
44812115Sdyson    return 0;
44912115Sdyson}
45012115Sdyson
45112115Sdyson/*
45212115Sdyson * Reload all incore data for a filesystem (used after running fsck on
45312115Sdyson * the root filesystem and finding things to fix). The filesystem must
45412115Sdyson * be mounted read-only.
45512115Sdyson *
45612115Sdyson * Things to do to update the mount:
45712115Sdyson *	1) invalidate all cached meta-data.
45812115Sdyson *	2) re-read superblock from disk.
45912115Sdyson *	3) re-read summary information from disk.
46012115Sdyson *	4) invalidate all inactive vnodes.
46112115Sdyson *	5) invalidate all cached file data.
46212115Sdyson *	6) re-read inode data for all active vnodes.
46312115Sdyson */
46412911Sphkstatic int
46512115Sdysonext2_reload(mountp, cred, p)
46612115Sdyson	register struct mount *mountp;
46712115Sdyson	struct ucred *cred;
46812115Sdyson	struct proc *p;
46912115Sdyson{
47012115Sdyson	register struct vnode *vp, *nvp, *devvp;
47112115Sdyson	struct inode *ip;
47212115Sdyson	struct buf *bp;
47312115Sdyson	struct ext2_super_block * es;
47412115Sdyson	struct ext2_sb_info *fs;
47512147Sdyson	int error;
47612115Sdyson
47712115Sdyson	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
47812115Sdyson		return (EINVAL);
47912115Sdyson	/*
48012115Sdyson	 * Step 1: invalidate all cached meta-data.
48112115Sdyson	 */
48212115Sdyson	devvp = VFSTOUFS(mountp)->um_devvp;
48312115Sdyson	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
48412115Sdyson		panic("ext2_reload: dirty1");
48512115Sdyson	/*
48612115Sdyson	 * Step 2: re-read superblock from disk.
48712115Sdyson	 * constants have been adjusted for ext2
48812115Sdyson	 */
48912115Sdyson	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
49012115Sdyson		return (error);
49112115Sdyson	es = (struct ext2_super_block *)bp->b_data;
49212115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC) {
49312115Sdyson		if(es->s_magic == EXT2_PRE_02B_MAGIC)
49412115Sdyson		    printf("This filesystem bears the magic number of a pre "
49512115Sdyson			   "0.2b version of ext2. This is not supported by "
49612115Sdyson			   "Lites.\n");
49712115Sdyson		else
49812115Sdyson		    printf("Wrong magic number: %x (expected %x for ext2 fs\n",
49912115Sdyson			es->s_magic, EXT2_SUPER_MAGIC);
50012115Sdyson		brelse(bp);
50112115Sdyson		return (EIO);		/* XXX needs translation */
50212115Sdyson	}
50312115Sdyson	fs = VFSTOUFS(mountp)->um_e2fs;
50412115Sdyson	bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
50512115Sdyson
50612115Sdyson	if(error = compute_sb_data(devvp, es, fs)) {
50712115Sdyson		brelse(bp);
50812115Sdyson		return error;
50912115Sdyson	}
51012115Sdyson#ifdef UNKLAR
51112115Sdyson	if (fs->fs_sbsize < SBSIZE)
51212115Sdyson		bp->b_flags |= B_INVAL;
51312115Sdyson#endif
51412115Sdyson	brelse(bp);
51512115Sdyson
51612115Sdysonloop:
51712115Sdyson	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
51812115Sdyson		nvp = vp->v_mntvnodes.le_next;
51912115Sdyson		/*
52012115Sdyson		 * Step 4: invalidate all inactive vnodes.
52112115Sdyson		 */
52212115Sdyson		if (vp->v_usecount == 0) {
52312115Sdyson			vgone(vp);
52412115Sdyson			continue;
52512115Sdyson		}
52612115Sdyson		/*
52712115Sdyson		 * Step 5: invalidate all cached file data.
52812115Sdyson		 */
52922521Sdyson		if (vget(vp, LK_EXCLUSIVE, p))
53012115Sdyson			goto loop;
53112115Sdyson		if (vinvalbuf(vp, 0, cred, p, 0, 0))
53212115Sdyson			panic("ext2_reload: dirty2");
53312115Sdyson		/*
53412115Sdyson		 * Step 6: re-read inode data for all active vnodes.
53512115Sdyson		 */
53612115Sdyson		ip = VTOI(vp);
53712115Sdyson		if (error =
53812115Sdyson		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
53912115Sdyson		    (int)fs->s_blocksize, NOCRED, &bp)) {
54012115Sdyson			vput(vp);
54112115Sdyson			return (error);
54212115Sdyson		}
54312115Sdyson		ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
54412115Sdyson			EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)),
54512115Sdyson			&ip->i_din);
54612115Sdyson		brelse(bp);
54712115Sdyson		vput(vp);
54812115Sdyson		if (vp->v_mount != mountp)
54912115Sdyson			goto loop;
55012115Sdyson	}
55112115Sdyson	return (0);
55212115Sdyson}
55312115Sdyson
55412115Sdyson/*
55512115Sdyson * Common code for mount and mountroot
55612115Sdyson */
55712911Sphkstatic int
55812115Sdysonext2_mountfs(devvp, mp, p)
55912115Sdyson	register struct vnode *devvp;
56012115Sdyson	struct mount *mp;
56112115Sdyson	struct proc *p;
56212115Sdyson{
56312115Sdyson	register struct ufsmount *ump;
56412115Sdyson	struct buf *bp;
56512115Sdyson	register struct ext2_sb_info *fs;
56612115Sdyson	struct ext2_super_block * es;
56712115Sdyson	dev_t dev = devvp->v_rdev;
56812115Sdyson	struct partinfo dpart;
56912115Sdyson	int havepart = 0;
57012115Sdyson	int error, i, size;
57112115Sdyson	int ronly;
57212115Sdyson#if !defined(__FreeBSD__)
57312115Sdyson	extern struct vnode *rootvp;
57412115Sdyson#endif
57512115Sdyson
57612115Sdyson	/*
57712115Sdyson	 * Disallow multiple mounts of the same device.
57812115Sdyson	 * Disallow mounting of a device that is currently in use
57912115Sdyson	 * (except for root, which might share swap device for miniroot).
58012115Sdyson	 * Flush out any old buffers remaining from a previous use.
58112115Sdyson	 */
58212115Sdyson	if (error = vfs_mountedon(devvp))
58312115Sdyson		return (error);
58412115Sdyson	if (vcount(devvp) > 1 && devvp != rootvp)
58512115Sdyson		return (EBUSY);
58612115Sdyson	if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
58712115Sdyson		return (error);
58812115Sdyson#ifdef READONLY
58912115Sdyson/* turn on this to force it to be read-only */
59012115Sdyson	mp->mnt_flag |= MNT_RDONLY;
59112115Sdyson#endif
59212115Sdyson
59312115Sdyson	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
59412115Sdyson	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
59512115Sdyson		return (error);
59612115Sdyson	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
59712115Sdyson		size = DEV_BSIZE;
59812115Sdyson	else {
59912115Sdyson		havepart = 1;
60012115Sdyson		size = dpart.disklab->d_secsize;
60112115Sdyson	}
60212115Sdyson
60312115Sdyson	bp = NULL;
60412115Sdyson	ump = NULL;
60512115Sdyson	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
60612115Sdyson		goto out;
60712115Sdyson	es = (struct ext2_super_block *)bp->b_data;
60812115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC) {
60912115Sdyson		if(es->s_magic == EXT2_PRE_02B_MAGIC)
61012115Sdyson		    printf("This filesystem bears the magic number of a pre "
61112115Sdyson			   "0.2b version of ext2. This is not supported by "
61212115Sdyson			   "Lites.\n");
61312115Sdyson		else
61412115Sdyson		    printf("Wrong magic number: %x (expected %x for EXT2FS)\n",
61512115Sdyson			es->s_magic, EXT2_SUPER_MAGIC);
61612115Sdyson		error = EINVAL;		/* XXX needs translation */
61712115Sdyson		goto out;
61812115Sdyson	}
61912115Sdyson	ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
62012115Sdyson	bzero((caddr_t)ump, sizeof *ump);
62112115Sdyson	/* I don't know whether this is the right strategy. Note that
62212115Sdyson	   we dynamically allocate both a ext2_sb_info and a ext2_super_block
62312115Sdyson	   while Linux keeps the super block in a locked buffer
62412115Sdyson	 */
62512115Sdyson	ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
62612115Sdyson		M_UFSMNT, M_WAITOK);
62712115Sdyson	ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
62812115Sdyson		M_UFSMNT, M_WAITOK);
62912115Sdyson	bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
63012115Sdyson	if(error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)) {
63112115Sdyson		brelse(bp);
63212115Sdyson		return error;
63312115Sdyson	}
63412115Sdyson	brelse(bp);
63512115Sdyson	bp = NULL;
63612115Sdyson	fs = ump->um_e2fs;
63712115Sdyson	fs->s_rd_only = ronly;	/* ronly is set according to mnt_flags */
63812115Sdyson	if (!(fs->s_es->s_state & EXT2_VALID_FS)) {
63912115Sdyson		printf("WARNING: %s was not properly dismounted\n",
64012115Sdyson			fs->fs_fsmnt);
64112115Sdyson	}
64212115Sdyson	/* if the fs is not mounted read-only, make sure the super block is
64312115Sdyson	   always written back on a sync()
64412115Sdyson	 */
64512115Sdyson	if (ronly == 0) {
64612115Sdyson		fs->s_dirt = 1;		/* mark it modified */
64712115Sdyson		fs->s_es->s_state &= ~EXT2_VALID_FS;	/* set fs invalid */
64812115Sdyson	}
64912115Sdyson	mp->mnt_data = (qaddr_t)ump;
65012115Sdyson	mp->mnt_stat.f_fsid.val[0] = (long)dev;
65112115Sdyson	mp->mnt_stat.f_fsid.val[1] = MOUNT_EXT2FS;
65212115Sdyson	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
65312115Sdyson	mp->mnt_flag |= MNT_LOCAL;
65412115Sdyson	ump->um_mountp = mp;
65512115Sdyson	ump->um_dev = dev;
65612115Sdyson	ump->um_devvp = devvp;
65712115Sdyson	/* setting those two parameters allows us to use
65812115Sdyson	   ufs_bmap w/o changse !
65912115Sdyson	*/
66012115Sdyson	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
66112115Sdyson	ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
66212115Sdyson	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
66312115Sdyson	for (i = 0; i < MAXQUOTAS; i++)
66412115Sdyson		ump->um_quotas[i] = NULLVP;
66512115Sdyson		devvp->v_specflags |= SI_MOUNTEDON;
66612115Sdyson		if (ronly == 0)
66712115Sdyson			ext2_sbupdate(ump, MNT_WAIT);
66812115Sdyson	return (0);
66912115Sdysonout:
67012115Sdyson	if (bp)
67112115Sdyson		brelse(bp);
67212115Sdyson	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
67312115Sdyson	if (ump) {
67412115Sdyson		bsd_free(ump->um_fs, M_UFSMNT);
67512115Sdyson		bsd_free(ump, M_UFSMNT);
67612115Sdyson		mp->mnt_data = (qaddr_t)0;
67712115Sdyson	}
67812115Sdyson	return (error);
67912115Sdyson}
68012115Sdyson
68112115Sdyson/*
68212115Sdyson * unmount system call
68312115Sdyson */
68412911Sphkstatic int
68512115Sdysonext2_unmount(mp, mntflags, p)
68612115Sdyson	struct mount *mp;
68712115Sdyson	int mntflags;
68812115Sdyson	struct proc *p;
68912115Sdyson{
69012115Sdyson	register struct ufsmount *ump;
69112115Sdyson	register struct ext2_sb_info *fs;
69212115Sdyson	int error, flags, ronly, i;
69312115Sdyson
69412115Sdyson	flags = 0;
69512115Sdyson	if (mntflags & MNT_FORCE) {
69612115Sdyson		if (mp->mnt_flag & MNT_ROOTFS)
69712115Sdyson			return (EINVAL);
69812115Sdyson		flags |= FORCECLOSE;
69912115Sdyson	}
70012115Sdyson	if (error = ext2_flushfiles(mp, flags, p))
70112115Sdyson		return (error);
70212115Sdyson	ump = VFSTOUFS(mp);
70312115Sdyson	fs = ump->um_e2fs;
70412115Sdyson	ronly = fs->s_rd_only;
70512115Sdyson	if (!ronly) {
70612115Sdyson		fs->s_es->s_state |= EXT2_VALID_FS;	/* was fs_clean = 1 */
70712115Sdyson		ext2_sbupdate(ump, MNT_WAIT);
70812115Sdyson	}
70927881Sdyson
71012115Sdyson	/* release buffers containing group descriptors */
71112115Sdyson	for(i = 0; i < fs->s_db_per_group; i++)
71227881Sdyson		ULCK_BUF(fs->s_group_desc[i])
71327881Sdyson
71412115Sdyson	/* release cached inode/block bitmaps */
71512115Sdyson        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
71612115Sdyson                if (fs->s_inode_bitmap[i])
71727881Sdyson			ULCK_BUF(fs->s_inode_bitmap[i])
71827881Sdyson
71912115Sdyson        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
72012115Sdyson                if (fs->s_block_bitmap[i])
72127881Sdyson			ULCK_BUF(fs->s_block_bitmap[i])
72212115Sdyson
72312115Sdyson	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
72412115Sdyson	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
72512115Sdyson		NOCRED, p);
72612115Sdyson	vrele(ump->um_devvp);
72712115Sdyson	bsd_free(fs->s_es, M_UFSMNT);
72812115Sdyson	bsd_free(fs, M_UFSMNT);
72912115Sdyson	bsd_free(ump, M_UFSMNT);
73012115Sdyson	mp->mnt_data = (qaddr_t)0;
73112115Sdyson	mp->mnt_flag &= ~MNT_LOCAL;
73212115Sdyson	return (error);
73312115Sdyson}
73412115Sdyson
73512115Sdyson/*
73612115Sdyson * Flush out all the files in a filesystem.
73712115Sdyson */
73812911Sphkstatic int
73912115Sdysonext2_flushfiles(mp, flags, p)
74012115Sdyson	register struct mount *mp;
74112115Sdyson	int flags;
74212115Sdyson	struct proc *p;
74312115Sdyson{
74412115Sdyson	register struct ufsmount *ump;
74512147Sdyson	int error;
74612746Sbde#if QUOTA
74712746Sbde	int i;
74812746Sbde#endif
74912115Sdyson
75012115Sdyson	ump = VFSTOUFS(mp);
75112115Sdyson#if QUOTA
75212115Sdyson	if (mp->mnt_flag & MNT_QUOTA) {
75312115Sdyson		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
75412115Sdyson			return (error);
75512115Sdyson		for (i = 0; i < MAXQUOTAS; i++) {
75612115Sdyson			if (ump->um_quotas[i] == NULLVP)
75712115Sdyson				continue;
75812115Sdyson			quotaoff(p, mp, i);
75912115Sdyson		}
76012115Sdyson		/*
76112115Sdyson		 * Here we fall through to vflush again to ensure
76212115Sdyson		 * that we have gotten rid of all the system vnodes.
76312115Sdyson		 */
76412115Sdyson	}
76512115Sdyson#endif
76612115Sdyson	error = vflush(mp, NULLVP, flags);
76712115Sdyson	return (error);
76812115Sdyson}
76912115Sdyson
77012115Sdyson/*
77112115Sdyson * Get file system statistics.
77212115Sdyson * taken from ext2/super.c ext2_statfs
77312115Sdyson */
77412911Sphkstatic int
77512115Sdysonext2_statfs(mp, sbp, p)
77612115Sdyson	struct mount *mp;
77712115Sdyson	register struct statfs *sbp;
77812115Sdyson	struct proc *p;
77912115Sdyson{
78012115Sdyson        unsigned long overhead;
78112115Sdyson	unsigned long overhead_per_group;
78212115Sdyson
78312115Sdyson	register struct ufsmount *ump;
78412115Sdyson	register struct ext2_sb_info *fs;
78512115Sdyson	register struct ext2_super_block *es;
78612115Sdyson
78712115Sdyson	ump = VFSTOUFS(mp);
78812115Sdyson	fs = ump->um_e2fs;
78912115Sdyson	es = fs->s_es;
79012115Sdyson
79112115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC)
79212115Sdyson		panic("ext2_statfs - magic number spoiled");
79312115Sdyson
79412115Sdyson	/*
79512115Sdyson	 * Compute the overhead (FS structures)
79612115Sdyson	 */
79712115Sdyson	overhead_per_group = 1 /* super block */ +
79812115Sdyson			     fs->s_db_per_group +
79912115Sdyson			     1 /* block bitmap */ +
80012115Sdyson			     1 /* inode bitmap */ +
80112115Sdyson			     fs->s_itb_per_group;
80212115Sdyson	overhead = es->s_first_data_block +
80312115Sdyson		   fs->s_groups_count * overhead_per_group;
80412115Sdyson
80512115Sdyson	sbp->f_type = MOUNT_EXT2FS;
80612115Sdyson	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
80712115Sdyson	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
80812115Sdyson	sbp->f_blocks = es->s_blocks_count - overhead;
80912115Sdyson	sbp->f_bfree = es->s_free_blocks_count;
81012115Sdyson	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
81112115Sdyson	sbp->f_files = es->s_inodes_count;
81212115Sdyson	sbp->f_ffree = es->s_free_inodes_count;
81312115Sdyson	if (sbp != &mp->mnt_stat) {
81412115Sdyson		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
81512115Sdyson			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
81612115Sdyson		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
81712115Sdyson			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
81812115Sdyson	}
81912115Sdyson	return (0);
82012115Sdyson}
82112115Sdyson
82212115Sdyson/*
82312115Sdyson * Go through the disk queues to initiate sandbagged IO;
82412115Sdyson * go through the inodes to write those that have been modified;
82512115Sdyson * initiate the writing of the super block if it has been modified.
82612115Sdyson *
82712115Sdyson * Note: we are always called with the filesystem marked `MPBUSY'.
82812115Sdyson */
82912911Sphkstatic int
83012115Sdysonext2_sync(mp, waitfor, cred, p)
83112115Sdyson	struct mount *mp;
83212115Sdyson	int waitfor;
83312115Sdyson	struct ucred *cred;
83412115Sdyson	struct proc *p;
83512115Sdyson{
83612115Sdyson	register struct vnode *vp;
83712115Sdyson	register struct inode *ip;
83812115Sdyson	register struct ufsmount *ump = VFSTOUFS(mp);
83912115Sdyson	register struct ext2_sb_info *fs;
84012115Sdyson	int error, allerror = 0;
84112115Sdyson
84212115Sdyson	fs = ump->um_e2fs;
84312115Sdyson	/*
84412115Sdyson	 * Write back modified superblock.
84512115Sdyson	 * Consistency check that the superblock
84612115Sdyson	 * is still in the buffer cache.
84712115Sdyson	 */
84812115Sdyson	if (fs->s_dirt) {
84912115Sdyson#if !defined(__FreeBSD__)
85012115Sdyson		struct timeval time;
85112115Sdyson#endif
85212115Sdyson
85312115Sdyson		if (fs->s_rd_only != 0) {		/* XXX */
85412115Sdyson			printf("fs = %s\n", fs->fs_fsmnt);
85512115Sdyson			panic("update: rofs mod");
85612115Sdyson		}
85712115Sdyson		fs->s_dirt = 0;
85812115Sdyson#if !defined(__FreeBSD__)
85912115Sdyson		get_time(&time);
86012115Sdyson#endif
86112115Sdyson		fs->s_es->s_wtime = time.tv_sec;
86212115Sdyson		allerror = ext2_sbupdate(ump, waitfor);
86312115Sdyson	}
86412115Sdyson	/*
86512115Sdyson	 * Write back each (modified) inode.
86612115Sdyson	 */
86712115Sdysonloop:
86812115Sdyson	for (vp = mp->mnt_vnodelist.lh_first;
86912115Sdyson	     vp != NULL;
87012115Sdyson	     vp = vp->v_mntvnodes.le_next) {
87112115Sdyson		/*
87212115Sdyson		 * If the vnode that we are about to sync is no longer
87312115Sdyson		 * associated with this mount point, start over.
87412115Sdyson		 */
87512115Sdyson		if (vp->v_mount != mp)
87612115Sdyson			goto loop;
87712115Sdyson		if (VOP_ISLOCKED(vp))
87812115Sdyson			continue;
87912115Sdyson		ip = VTOI(vp);
88012115Sdyson		if ((ip->i_flag &
88112115Sdyson		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
88212115Sdyson		    vp->v_dirtyblkhd.lh_first == NULL)
88312115Sdyson			continue;
88422521Sdyson		if (vget(vp, LK_EXCLUSIVE, p))
88512115Sdyson			goto loop;
88612115Sdyson		if (error = VOP_FSYNC(vp, cred, waitfor, p))
88712115Sdyson			allerror = error;
88812115Sdyson		vput(vp);
88912115Sdyson	}
89012115Sdyson	/*
89112115Sdyson	 * Force stale file system control information to be flushed.
89212115Sdyson	 */
89312115Sdyson	if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
89412115Sdyson		allerror = error;
89512115Sdyson#if QUOTA
89612115Sdyson	qsync(mp);
89712115Sdyson#endif
89812115Sdyson	return (allerror);
89912115Sdyson}
90012115Sdyson
90112115Sdyson/*
90212115Sdyson * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
90312115Sdyson * in from disk.  If it is in core, wait for the lock bit to clear, then
90412115Sdyson * return the inode locked.  Detection and handling of mount points must be
90512115Sdyson * done by the calling routine.
90612115Sdyson */
90712911Sphkstatic int
90812115Sdysonext2_vget(mp, ino, vpp)
90912115Sdyson	struct mount *mp;
91012115Sdyson	ino_t ino;
91112115Sdyson	struct vnode **vpp;
91212115Sdyson{
91312115Sdyson	register struct ext2_sb_info *fs;
91412115Sdyson	register struct inode *ip;
91512115Sdyson	struct ufsmount *ump;
91612115Sdyson	struct buf *bp;
91712115Sdyson	struct vnode *vp;
91812115Sdyson	dev_t dev;
91912115Sdyson	int i, type, error;
92012115Sdyson	int used_blocks;
92112115Sdyson
92212115Sdyson	ump = VFSTOUFS(mp);
92312115Sdyson	dev = ump->um_dev;
92412406Sdysonrestart:
92512115Sdyson	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
92612115Sdyson		return (0);
92712115Sdyson
92812406Sdyson#ifdef __FreeBSD__
92912406Sdyson	/*
93012406Sdyson	 * Lock out the creation of new entries in the FFS hash table in
93112406Sdyson	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
93212406Sdyson	 * may occur!
93312406Sdyson	 */
93412406Sdyson	if (ext2fs_inode_hash_lock) {
93512406Sdyson		while (ext2fs_inode_hash_lock) {
93612406Sdyson			ext2fs_inode_hash_lock = -1;
93712406Sdyson			tsleep(&ext2fs_inode_hash_lock, PVM, "ffsvgt", 0);
93812406Sdyson		}
93912406Sdyson		goto restart;
94012406Sdyson	}
94112406Sdyson	ext2fs_inode_hash_lock = 1;
94212406Sdyson#endif
94312406Sdyson
94412115Sdyson	/* Allocate a new vnode/inode. */
94512115Sdyson	if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) {
94612115Sdyson		*vpp = NULL;
94712115Sdyson		return (error);
94812115Sdyson	}
94912115Sdyson	/* I don't really know what this 'type' does. I suppose it's some kind
95012115Sdyson	 * of memory accounting. Let's just book this memory on FFS's account
95112115Sdyson	 * If I'm not mistaken, this stuff isn't implemented anyway in Lites
95212115Sdyson	 */
95312115Sdyson	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
95412115Sdyson	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
95512406Sdyson#ifndef __FreeBSD__
95612115Sdyson	insmntque(vp, mp);
95712406Sdyson#endif
95812115Sdyson	bzero((caddr_t)ip, sizeof(struct inode));
95912115Sdyson	vp->v_data = ip;
96012115Sdyson	ip->i_vnode = vp;
96112115Sdyson	ip->i_e2fs = fs = ump->um_e2fs;
96212115Sdyson	ip->i_dev = dev;
96312115Sdyson	ip->i_number = ino;
96412115Sdyson#if QUOTA
96512115Sdyson	for (i = 0; i < MAXQUOTAS; i++)
96612115Sdyson		ip->i_dquot[i] = NODQUOT;
96712115Sdyson#endif
96812115Sdyson	/*
96912115Sdyson	 * Put it onto its hash chain and lock it so that other requests for
97012115Sdyson	 * this inode will block if they arrive while we are sleeping waiting
97112115Sdyson	 * for old data structures to be purged or for the contents of the
97212115Sdyson	 * disk portion of this inode to be read.
97312115Sdyson	 */
97412115Sdyson	ufs_ihashins(ip);
97512115Sdyson
97612406Sdyson#ifdef __FreeBSD__
97712406Sdyson	if (ext2fs_inode_hash_lock < 0)
97812406Sdyson		wakeup(&ext2fs_inode_hash_lock);
97912406Sdyson	ext2fs_inode_hash_lock = 0;
98012406Sdyson#endif
98112406Sdyson
98212115Sdyson	/* Read in the disk contents for the inode, copy into the inode. */
98312406Sdyson	/* Read in the disk contents for the inode, copy into the inode. */
98412115Sdyson#if 0
98512115Sdysonprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
98612115Sdyson#endif
98712115Sdyson	if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
98812115Sdyson	    (int)fs->s_blocksize, NOCRED, &bp)) {
98912115Sdyson		/*
99012115Sdyson		 * The inode does not contain anything useful, so it would
99112115Sdyson		 * be misleading to leave it on its hash chain. With mode
99212115Sdyson		 * still zero, it will be unlinked and returned to the free
99312115Sdyson		 * list by vput().
99412115Sdyson		 */
99512115Sdyson		vput(vp);
99612115Sdyson		brelse(bp);
99712115Sdyson		*vpp = NULL;
99812115Sdyson		return (error);
99912115Sdyson	}
100012115Sdyson	/* convert ext2 inode to dinode */
100112115Sdyson	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
100212115Sdyson			ino_to_fsbo(fs, ino)), &ip->i_din);
100312115Sdyson	ip->i_block_group = ino_to_cg(fs, ino);
100412115Sdyson	ip->i_next_alloc_block = 0;
100512115Sdyson	ip->i_next_alloc_goal = 0;
100612115Sdyson	ip->i_prealloc_count = 0;
100712115Sdyson	ip->i_prealloc_block = 0;
100812115Sdyson        /* now we want to make sure that block pointers for unused
100912115Sdyson           blocks are zeroed out - ext2_balloc depends on this
101012115Sdyson	   although for regular files and directories only
101112115Sdyson	*/
101212115Sdyson	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
101312115Sdyson		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
101412115Sdyson		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
101512115Sdyson			ip->i_db[i] = 0;
101612115Sdyson	}
101712115Sdyson/*
101812115Sdyson	ext2_print_inode(ip);
101912115Sdyson*/
102012115Sdyson	brelse(bp);
102112115Sdyson
102212115Sdyson	/*
102312115Sdyson	 * Initialize the vnode from the inode, check for aliases.
102412115Sdyson	 * Note that the underlying vnode may have changed.
102512115Sdyson	 */
102614249Sbde	if (error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) {
102712115Sdyson		vput(vp);
102812115Sdyson		*vpp = NULL;
102912115Sdyson		return (error);
103012115Sdyson	}
103112115Sdyson	/*
103212115Sdyson	 * Finish inode initialization now that aliasing has been resolved.
103312115Sdyson	 */
103412115Sdyson	ip->i_devvp = ump->um_devvp;
103512115Sdyson	VREF(ip->i_devvp);
103612115Sdyson	/*
103712115Sdyson	 * Set up a generation number for this inode if it does not
103812115Sdyson	 * already have one. This should only happen on old filesystems.
103912115Sdyson	 */
104012115Sdyson	if (ip->i_gen == 0) {
104112115Sdyson#if !defined(__FreeBSD__)
104212115Sdyson		struct timeval time;
104312115Sdyson		get_time(&time);
104412115Sdyson#endif
104512115Sdyson		if (++nextgennumber < (u_long)time.tv_sec)
104612115Sdyson			nextgennumber = time.tv_sec;
104712115Sdyson		ip->i_gen = nextgennumber;
104812115Sdyson		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
104912115Sdyson			ip->i_flag |= IN_MODIFIED;
105012115Sdyson	}
105112115Sdyson	*vpp = vp;
105212115Sdyson	return (0);
105312115Sdyson}
105412115Sdyson
105512115Sdyson/*
105612115Sdyson * File handle to vnode
105712115Sdyson *
105812115Sdyson * Have to be really careful about stale file handles:
105912115Sdyson * - check that the inode number is valid
106012115Sdyson * - call ext2_vget() to get the locked inode
106112115Sdyson * - check for an unallocated inode (i_mode == 0)
106212115Sdyson * - check that the given client host has export rights and return
106312115Sdyson *   those rights via. exflagsp and credanonp
106412115Sdyson */
106512911Sphkstatic int
106612115Sdysonext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
106712115Sdyson	register struct mount *mp;
106812115Sdyson	struct fid *fhp;
106928270Swollman	struct sockaddr *nam;
107012115Sdyson	struct vnode **vpp;
107112115Sdyson	int *exflagsp;
107212115Sdyson	struct ucred **credanonp;
107312115Sdyson{
107412115Sdyson	register struct ufid *ufhp;
107512115Sdyson	struct ext2_sb_info *fs;
107612115Sdyson
107712115Sdyson	ufhp = (struct ufid *)fhp;
107812115Sdyson	fs = VFSTOUFS(mp)->um_e2fs;
107912115Sdyson	if (ufhp->ufid_ino < ROOTINO ||
108012115Sdyson	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
108112115Sdyson		return (ESTALE);
108212115Sdyson	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
108312115Sdyson}
108412115Sdyson
108512115Sdyson/*
108612115Sdyson * Vnode pointer to File handle
108712115Sdyson */
108812115Sdyson/* ARGSUSED */
108912911Sphkstatic int
109012115Sdysonext2_vptofh(vp, fhp)
109112115Sdyson	struct vnode *vp;
109212115Sdyson	struct fid *fhp;
109312115Sdyson{
109412115Sdyson	register struct inode *ip;
109512115Sdyson	register struct ufid *ufhp;
109612115Sdyson
109712115Sdyson	ip = VTOI(vp);
109812115Sdyson	ufhp = (struct ufid *)fhp;
109912115Sdyson	ufhp->ufid_len = sizeof(struct ufid);
110012115Sdyson	ufhp->ufid_ino = ip->i_number;
110112115Sdyson	ufhp->ufid_gen = ip->i_gen;
110212115Sdyson	return (0);
110312115Sdyson}
110412115Sdyson
110512115Sdyson/*
110612115Sdyson * Write a superblock and associated information back to disk.
110712115Sdyson */
110812911Sphkstatic int
110912115Sdysonext2_sbupdate(mp, waitfor)
111012115Sdyson	struct ufsmount *mp;
111112115Sdyson	int waitfor;
111212115Sdyson{
111312115Sdyson	register struct ext2_sb_info *fs = mp->um_e2fs;
111412115Sdyson	register struct ext2_super_block *es = fs->s_es;
111512115Sdyson	register struct buf *bp;
111612147Sdyson	int i, error = 0;
111712115Sdyson/*
111812115Sdysonprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
111912115Sdyson*/
112012115Sdyson	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
112112115Sdyson	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
112212115Sdyson	if (waitfor == MNT_WAIT)
112312115Sdyson		error = bwrite(bp);
112412115Sdyson	else
112512115Sdyson		bawrite(bp);
112612115Sdyson
112727881Sdyson	/*
112827881Sdyson	 * The buffers for group descriptors, inode bitmaps and block bitmaps
112927881Sdyson	 * are not busy at this point and are (hopefully) written by the
113027881Sdyson	 * usual sync mechanism. No need to write them here
113112115Sdyson		 */
113212115Sdyson
113312115Sdyson	return (error);
113412115Sdyson}
1135