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