ext2_vfsops.c revision 59259
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
4051138Salfred *	$FreeBSD: head/sys/gnu/fs/ext2fs/ext2_vfsops.c 59259 2000-04-15 17:14:22Z rwatson $
4112115Sdyson */
4212115Sdyson
4313260Swollman#include "opt_quota.h"
4412115Sdyson
4512115Sdyson#include <sys/param.h>
4612115Sdyson#include <sys/systm.h>
4712115Sdyson#include <sys/namei.h>
4812115Sdyson#include <sys/proc.h>
4912115Sdyson#include <sys/kernel.h>
5012115Sdyson#include <sys/vnode.h>
5112115Sdyson#include <sys/mount.h>
5212115Sdyson#include <sys/buf.h>
5329906Skato#include <sys/conf.h>
5424131Sbde#include <sys/fcntl.h>
5512115Sdyson#include <sys/disklabel.h>
5612115Sdyson#include <sys/malloc.h>
5712115Sdyson#include <sys/stat.h>
5812115Sdyson
5959259Srwatson#include <ufs/ufs/extattr.h>
6012115Sdyson#include <ufs/ufs/quota.h>
6112115Sdyson#include <ufs/ufs/ufsmount.h>
6212115Sdyson#include <ufs/ufs/inode.h>
6312115Sdyson#include <ufs/ufs/ufs_extern.h>
6412115Sdyson
6554655Seivind#include <vm/vm_zone.h>
6654655Seivind
6712115Sdyson#include <gnu/ext2fs/fs.h>
6812115Sdyson#include <gnu/ext2fs/ext2_extern.h>
6912115Sdyson#include <gnu/ext2fs/ext2_fs.h>
7012115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h>
7112115Sdyson
7251138Salfredstatic int ext2_fhtovp __P((struct mount *, struct fid *, struct vnode **));
7312911Sphkstatic int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p));
7412911Sphkstatic int ext2_mount __P((struct mount *,
7512911Sphk	    char *, caddr_t, struct nameidata *, struct proc *));
7612911Sphkstatic int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *));
7712911Sphkstatic int ext2_reload __P((struct mount *mountp, struct ucred *cred,
7812911Sphk			struct proc *p));
7912911Sphkstatic int ext2_sbupdate __P((struct ufsmount *, int));
8012911Sphkstatic int ext2_statfs __P((struct mount *, struct statfs *, struct proc *));
8112911Sphkstatic int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *));
8212911Sphkstatic int ext2_unmount __P((struct mount *, int, struct proc *));
8312911Sphkstatic int ext2_vget __P((struct mount *, ino_t, struct vnode **));
8412911Sphkstatic int ext2_vptofh __P((struct vnode *, struct fid *));
8512115Sdyson
8631315Sbdestatic MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
8730280Sphk
8812911Sphkstatic struct vfsops ext2fs_vfsops = {
8912115Sdyson	ext2_mount,
9012115Sdyson	ufs_start,		/* empty function */
9112115Sdyson	ext2_unmount,
9212115Sdyson	ufs_root,		/* root inode via vget */
9312115Sdyson	ufs_quotactl,		/* does operations associated with quotas */
9412115Sdyson	ext2_statfs,
9512115Sdyson	ext2_sync,
9612115Sdyson	ext2_vget,
9712115Sdyson	ext2_fhtovp,
9851138Salfred	ufs_check_export,
9912115Sdyson	ext2_vptofh,
10012115Sdyson	ext2_init,
10154803Srwatson	vfs_stduninit,
10254803Srwatson	vfs_stdextattrctl,
10312115Sdyson};
10412115Sdyson
10538909SbdeVFS_SET(ext2fs_vfsops, ext2fs, 0);
10612115Sdyson#define bsd_malloc malloc
10712115Sdyson#define bsd_free free
10812115Sdyson
10912911Sphkstatic int ext2fs_inode_hash_lock;
11012115Sdyson
11155313Sbdestatic int	ext2_check_sb_compat __P((struct ext2_super_block *es,
11255313Sbde					  dev_t dev, int ronly));
11316322Sgpalmerstatic int	compute_sb_data __P((struct vnode * devvp,
11416322Sgpalmer				     struct ext2_super_block * es,
11516322Sgpalmer				     struct ext2_sb_info * fs));
11616322Sgpalmer
11716322Sgpalmer#ifdef notyet
11816322Sgpalmerstatic int ext2_mountroot __P((void));
11916322Sgpalmer
12012115Sdyson/*
12116322Sgpalmer * Called by main() when ext2fs is going to be mounted as root.
12212115Sdyson *
12312115Sdyson * Name is updated by mount(8) after booting.
12412115Sdyson */
12512115Sdyson#define ROOTNAME	"root_device"
12612115Sdyson
12712911Sphkstatic int
12812115Sdysonext2_mountroot()
12912115Sdyson{
13012115Sdyson	register struct ext2_sb_info *fs;
13112115Sdyson	register struct mount *mp;
13212115Sdyson	struct proc *p = curproc;
13312115Sdyson	struct ufsmount *ump;
13412115Sdyson	u_int size;
13512115Sdyson	int error;
13612115Sdyson
13729208Sbde	if ((error = bdevvp(rootdev, &rootvp))) {
13852782Smsmith		printf("ext2_mountroot: can't find rootvp\n");
13929208Sbde		return (error);
14029208Sbde	}
14112115Sdyson	mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
14212115Sdyson	bzero((char *)mp, (u_long)sizeof(struct mount));
14312115Sdyson	mp->mnt_op = &ext2fs_vfsops;
14412115Sdyson	mp->mnt_flag = MNT_RDONLY;
14512115Sdyson	if (error = ext2_mountfs(rootvp, mp, p)) {
14612115Sdyson		bsd_free(mp, M_MOUNT);
14712115Sdyson		return (error);
14812115Sdyson	}
14912115Sdyson	if (error = vfs_lock(mp)) {
15012115Sdyson		(void)ext2_unmount(mp, 0, p);
15112115Sdyson		bsd_free(mp, M_MOUNT);
15212115Sdyson		return (error);
15312115Sdyson	}
15453452Sphk	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
15512115Sdyson	mp->mnt_flag |= MNT_ROOTFS;
15612115Sdyson	mp->mnt_vnodecovered = NULLVP;
15712115Sdyson	ump = VFSTOUFS(mp);
15812115Sdyson	fs = ump->um_e2fs;
15912115Sdyson	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
16012115Sdyson	fs->fs_fsmnt[0] = '/';
16112115Sdyson	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
16212115Sdyson	    MNAMELEN);
16312115Sdyson	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
16412115Sdyson	    &size);
16512115Sdyson	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
16612115Sdyson	(void)ext2_statfs(mp, &mp->mnt_stat, p);
16712115Sdyson	vfs_unlock(mp);
16812115Sdyson	inittodr(fs->s_es->s_wtime);		/* this helps to set the time */
16912115Sdyson	return (0);
17012115Sdyson}
17116322Sgpalmer#endif
17212115Sdyson
17312115Sdyson/*
17412115Sdyson * VFS Operations.
17512115Sdyson *
17612115Sdyson * mount system call
17712115Sdyson */
17812911Sphkstatic int
17912115Sdysonext2_mount(mp, path, data, ndp, p)
18012115Sdyson	register struct mount *mp;
18112115Sdyson	char *path;
18212115Sdyson	caddr_t data;		/* this is actually a (struct ufs_args *) */
18312115Sdyson	struct nameidata *ndp;
18412115Sdyson	struct proc *p;
18512115Sdyson{
18612115Sdyson	struct vnode *devvp;
18712115Sdyson	struct ufs_args args;
18812115Sdyson	struct ufsmount *ump = 0;
18912115Sdyson	register struct ext2_sb_info *fs;
19012115Sdyson	u_int size;
19112115Sdyson	int error, flags;
19239028Sbde	mode_t accessmode;
19312115Sdyson
19443301Sdillon	if ((error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) != 0)
19512115Sdyson		return (error);
19612115Sdyson	/*
19712115Sdyson	 * If updating, check whether changing from read-only to
19812115Sdyson	 * read/write; if there is no device name, that's all we do.
19912115Sdyson	 */
20012115Sdyson	if (mp->mnt_flag & MNT_UPDATE) {
20112115Sdyson		ump = VFSTOUFS(mp);
20212115Sdyson		fs = ump->um_e2fs;
20312115Sdyson		error = 0;
20412115Sdyson		if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
20512115Sdyson			flags = WRITECLOSE;
20612115Sdyson			if (mp->mnt_flag & MNT_FORCE)
20712115Sdyson				flags |= FORCECLOSE;
20822521Sdyson			if (vfs_busy(mp, LK_NOWAIT, 0, p))
20912115Sdyson				return (EBUSY);
21012115Sdyson			error = ext2_flushfiles(mp, flags, p);
21122521Sdyson			vfs_unbusy(mp, p);
21239670Sbde			if (!error && fs->s_wasvalid) {
21339670Sbde				fs->s_es->s_state |= EXT2_VALID_FS;
21439670Sbde				ext2_sbupdate(ump, MNT_WAIT);
21539670Sbde			}
21639670Sbde			fs->s_rd_only = 1;
21712115Sdyson		}
21812115Sdyson		if (!error && (mp->mnt_flag & MNT_RELOAD))
21912115Sdyson			error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p);
22012115Sdyson		if (error)
22112115Sdyson			return (error);
22257839Sbde		devvp = ump->um_devvp;
22357839Sbde		if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
22457839Sbde		    (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
22557839Sbde			return (EPERM);
22639028Sbde		if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
22739028Sbde			/*
22839028Sbde			 * If upgrade to read-write by non-root, then verify
22939028Sbde			 * that user has necessary permissions on the device.
23039028Sbde			 */
23139028Sbde			if (p->p_ucred->cr_uid != 0) {
23239028Sbde				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
23343301Sdillon				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
23443301Sdillon				    p->p_ucred, p)) != 0) {
23539028Sbde					VOP_UNLOCK(devvp, 0, p);
23639028Sbde					return (error);
23739028Sbde				}
23839028Sbde				VOP_UNLOCK(devvp, 0, p);
23939028Sbde			}
24039028Sbde
24139670Sbde			if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
24239670Sbde			    (fs->s_es->s_state & EXT2_ERROR_FS)) {
24339670Sbde				if (mp->mnt_flag & MNT_FORCE) {
24439670Sbde					printf(
24539670Sbde"WARNING: %s was not properly dismounted\n",
24639670Sbde					    fs->fs_fsmnt);
24739670Sbde				} else {
24839670Sbde					printf(
24939670Sbde"WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
25039670Sbde					    fs->fs_fsmnt);
25139670Sbde					return (EPERM);
25239670Sbde				}
25339670Sbde			}
25412115Sdyson			fs->s_es->s_state &= ~EXT2_VALID_FS;
25512115Sdyson			ext2_sbupdate(ump, MNT_WAIT);
25639670Sbde			fs->s_rd_only = 0;
25712115Sdyson		}
25812115Sdyson		if (args.fspec == 0) {
25912115Sdyson			/*
26012115Sdyson			 * Process export requests.
26112115Sdyson			 */
26212115Sdyson			return (vfs_export(mp, &ump->um_export, &args.export));
26312115Sdyson		}
26412115Sdyson	}
26512115Sdyson	/*
26612115Sdyson	 * Not an update, or updating the name: look up the name
26712115Sdyson	 * and verify that it refers to a sensible block device.
26812115Sdyson	 */
26912115Sdyson	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
27043301Sdillon	if ((error = namei(ndp)) != 0)
27112115Sdyson		return (error);
27254655Seivind	NDFREE(ndp, NDF_ONLY_PNBUF);
27312115Sdyson	devvp = ndp->ni_vp;
27412115Sdyson
27555756Sphk	if (!vn_isdisk(devvp, &error)) {
27612115Sdyson		vrele(devvp);
27755756Sphk		return (error);
27812115Sdyson	}
27939028Sbde
28039028Sbde	/*
28139028Sbde	 * If mount by non-root, then verify that user has necessary
28239028Sbde	 * permissions on the device.
28339028Sbde	 */
28439028Sbde	if (p->p_ucred->cr_uid != 0) {
28539028Sbde		accessmode = VREAD;
28639028Sbde		if ((mp->mnt_flag & MNT_RDONLY) == 0)
28739028Sbde			accessmode |= VWRITE;
28839028Sbde		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
28943301Sdillon		if ((error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) != 0) {
29039028Sbde			vput(devvp);
29139028Sbde			return (error);
29239028Sbde		}
29339028Sbde		VOP_UNLOCK(devvp, 0, p);
29439028Sbde	}
29539028Sbde
29629888Skato	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
29712115Sdyson		error = ext2_mountfs(devvp, mp, p);
29829888Skato	} else {
29912115Sdyson		if (devvp != ump->um_devvp)
30012115Sdyson			error = EINVAL;	/* needs translation */
30112115Sdyson		else
30212115Sdyson			vrele(devvp);
30312115Sdyson	}
30412115Sdyson	if (error) {
30512115Sdyson		vrele(devvp);
30612115Sdyson		return (error);
30712115Sdyson	}
30812115Sdyson	ump = VFSTOUFS(mp);
30912115Sdyson	fs = ump->um_e2fs;
31012115Sdyson	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
31112115Sdyson	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
31212115Sdyson	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
31312115Sdyson	    MNAMELEN);
31412115Sdyson	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
31512115Sdyson	    &size);
31612115Sdyson	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
31712115Sdyson	(void)ext2_statfs(mp, &mp->mnt_stat, p);
31812115Sdyson	return (0);
31912115Sdyson}
32012115Sdyson
32112115Sdyson/*
32212115Sdyson * checks that the data in the descriptor blocks make sense
32312115Sdyson * this is taken from ext2/super.c
32412115Sdyson */
32512115Sdysonstatic int ext2_check_descriptors (struct ext2_sb_info * sb)
32612115Sdyson{
32712115Sdyson        int i;
32812115Sdyson        int desc_block = 0;
32912115Sdyson        unsigned long block = sb->s_es->s_first_data_block;
33012115Sdyson        struct ext2_group_desc * gdp = NULL;
33112115Sdyson
33212115Sdyson        /* ext2_debug ("Checking group descriptors"); */
33312115Sdyson
33412115Sdyson        for (i = 0; i < sb->s_groups_count; i++)
33512115Sdyson        {
33612115Sdyson		/* examine next descriptor block */
33712115Sdyson                if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
33812115Sdyson                        gdp = (struct ext2_group_desc *)
33912115Sdyson				sb->s_group_desc[desc_block++]->b_data;
34012115Sdyson                if (gdp->bg_block_bitmap < block ||
34112115Sdyson                    gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
34212115Sdyson                {
34312115Sdyson                        printf ("ext2_check_descriptors: "
34412115Sdyson                                    "Block bitmap for group %d"
34539671Sbde                                    " not in group (block %lu)!\n",
34612115Sdyson                                    i, (unsigned long) gdp->bg_block_bitmap);
34712115Sdyson                        return 0;
34812115Sdyson                }
34912115Sdyson                if (gdp->bg_inode_bitmap < block ||
35012115Sdyson                    gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
35112115Sdyson                {
35212115Sdyson                        printf ("ext2_check_descriptors: "
35312115Sdyson                                    "Inode bitmap for group %d"
35439671Sbde                                    " not in group (block %lu)!\n",
35512115Sdyson                                    i, (unsigned long) gdp->bg_inode_bitmap);
35612115Sdyson                        return 0;
35712115Sdyson                }
35812115Sdyson                if (gdp->bg_inode_table < block ||
35912115Sdyson                    gdp->bg_inode_table + sb->s_itb_per_group >=
36012115Sdyson                    block + EXT2_BLOCKS_PER_GROUP(sb))
36112115Sdyson                {
36212115Sdyson                        printf ("ext2_check_descriptors: "
36312115Sdyson                                    "Inode table for group %d"
36439671Sbde                                    " not in group (block %lu)!\n",
36512115Sdyson                                    i, (unsigned long) gdp->bg_inode_table);
36612115Sdyson                        return 0;
36712115Sdyson                }
36812115Sdyson                block += EXT2_BLOCKS_PER_GROUP(sb);
36912115Sdyson                gdp++;
37012115Sdyson        }
37112115Sdyson        return 1;
37212115Sdyson}
37312115Sdyson
37455313Sbdestatic int
37555313Sbdeext2_check_sb_compat(es, dev, ronly)
37655313Sbde	struct ext2_super_block *es;
37755313Sbde	dev_t dev;
37855313Sbde	int ronly;
37955313Sbde{
38055313Sbde
38155313Sbde	if (es->s_magic != EXT2_SUPER_MAGIC) {
38255313Sbde		printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
38355313Sbde		    devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
38455313Sbde		return (1);
38555313Sbde	}
38655313Sbde	if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
38755313Sbde		if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
38855313Sbde			printf(
38955313Sbde"WARNING: mount of %s denied due to unsupported optional features\n",
39055313Sbde			    devtoname(dev));
39155313Sbde			return (1);
39255313Sbde		}
39355313Sbde		if (!ronly &&
39455313Sbde		    (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
39555313Sbde			printf(
39655313Sbde"WARNING: R/W mount of %s denied due to unsupported optional features\n",
39755313Sbde			    devtoname(dev));
39855313Sbde			return (1);
39955313Sbde		}
40055313Sbde	}
40155313Sbde	return (0);
40255313Sbde}
40355313Sbde
40412115Sdyson/*
40512115Sdyson * this computes the fields of the  ext2_sb_info structure from the
40612115Sdyson * data in the ext2_super_block structure read in
40712115Sdyson */
40812115Sdysonstatic int compute_sb_data(devvp, es, fs)
40912115Sdyson	struct vnode * devvp;
41012115Sdyson	struct ext2_super_block * es;
41112115Sdyson	struct ext2_sb_info * fs;
41212115Sdyson{
41312115Sdyson    int db_count, error;
41412115Sdyson    int i, j;
41512115Sdyson    int logic_sb_block = 1;	/* XXX for now */
41612115Sdyson
41712115Sdyson#if 1
41812115Sdyson#define V(v)
41912115Sdyson#else
42012115Sdyson#define V(v)  printf(#v"= %d\n", fs->v);
42112115Sdyson#endif
42212115Sdyson
42312115Sdyson    fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
42412115Sdyson    V(s_blocksize)
42512115Sdyson    fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
42612115Sdyson    V(s_bshift)
42712115Sdyson    fs->s_fsbtodb = es->s_log_block_size + 1;
42812115Sdyson    V(s_fsbtodb)
42912115Sdyson    fs->s_qbmask = fs->s_blocksize - 1;
43012115Sdyson    V(s_bmask)
43112115Sdyson    fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
43212115Sdyson    V(s_blocksize_bits)
43312115Sdyson    fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
43412115Sdyson    V(s_frag_size)
43512115Sdyson    if (fs->s_frag_size)
43612115Sdyson	fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
43712115Sdyson    V(s_frags_per_block)
43812115Sdyson    fs->s_blocks_per_group = es->s_blocks_per_group;
43912115Sdyson    V(s_blocks_per_group)
44012115Sdyson    fs->s_frags_per_group = es->s_frags_per_group;
44112115Sdyson    V(s_frags_per_group)
44212115Sdyson    fs->s_inodes_per_group = es->s_inodes_per_group;
44312115Sdyson    V(s_inodes_per_group)
44412115Sdyson    fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
44512115Sdyson    V(s_inodes_per_block)
44612115Sdyson    fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
44712115Sdyson    V(s_itb_per_group)
44812115Sdyson    fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
44912115Sdyson    V(s_desc_per_block)
45012115Sdyson    /* s_resuid / s_resgid ? */
45112115Sdyson    fs->s_groups_count = (es->s_blocks_count -
45212115Sdyson			  es->s_first_data_block +
45312115Sdyson			  EXT2_BLOCKS_PER_GROUP(fs) - 1) /
45412115Sdyson			 EXT2_BLOCKS_PER_GROUP(fs);
45512115Sdyson    V(s_groups_count)
45612115Sdyson    db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
45712115Sdyson	EXT2_DESC_PER_BLOCK(fs);
45812115Sdyson    fs->s_db_per_group = db_count;
45912115Sdyson    V(s_db_per_group)
46012115Sdyson
46112115Sdyson    fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
46212115Sdyson		M_UFSMNT, M_WAITOK);
46312115Sdyson
46412115Sdyson    /* adjust logic_sb_block */
46512115Sdyson    if(fs->s_blocksize > SBSIZE)
46612115Sdyson	/* Godmar thinks: if the blocksize is greater than 1024, then
46712115Sdyson	   the superblock is logically part of block zero.
46812115Sdyson	 */
46912115Sdyson        logic_sb_block = 0;
47012115Sdyson
47112115Sdyson    for (i = 0; i < db_count; i++) {
47212115Sdyson	error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
47312115Sdyson		fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
47412115Sdyson	if(error) {
47512115Sdyson	    for (j = 0; j < i; j++)
47612115Sdyson		brelse(fs->s_group_desc[j]);
47712115Sdyson	    bsd_free(fs->s_group_desc, M_UFSMNT);
47812115Sdyson	    printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
47912115Sdyson	    return EIO;
48012115Sdyson	}
48127881Sdyson	/* Set the B_LOCKED flag on the buffer, then brelse() it */
48227881Sdyson	LCK_BUF(fs->s_group_desc[i])
48312115Sdyson    }
48412115Sdyson    if(!ext2_check_descriptors(fs)) {
48512115Sdyson	    for (j = 0; j < db_count; j++)
48627881Sdyson		    ULCK_BUF(fs->s_group_desc[j])
48712115Sdyson	    bsd_free(fs->s_group_desc, M_UFSMNT);
48812115Sdyson	    printf("EXT2-fs: (ext2_check_descriptors failure) "
48912115Sdyson		   "unable to read group descriptors\n");
49012115Sdyson	    return EIO;
49112115Sdyson    }
49212115Sdyson
49312115Sdyson    for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
49412115Sdyson	    fs->s_inode_bitmap_number[i] = 0;
49512115Sdyson	    fs->s_inode_bitmap[i] = NULL;
49612115Sdyson	    fs->s_block_bitmap_number[i] = 0;
49712115Sdyson	    fs->s_block_bitmap[i] = NULL;
49812115Sdyson    }
49912115Sdyson    fs->s_loaded_inode_bitmaps = 0;
50012115Sdyson    fs->s_loaded_block_bitmaps = 0;
50112115Sdyson    return 0;
50212115Sdyson}
50312115Sdyson
50412115Sdyson/*
50512115Sdyson * Reload all incore data for a filesystem (used after running fsck on
50612115Sdyson * the root filesystem and finding things to fix). The filesystem must
50712115Sdyson * be mounted read-only.
50812115Sdyson *
50912115Sdyson * Things to do to update the mount:
51012115Sdyson *	1) invalidate all cached meta-data.
51112115Sdyson *	2) re-read superblock from disk.
51212115Sdyson *	3) re-read summary information from disk.
51312115Sdyson *	4) invalidate all inactive vnodes.
51412115Sdyson *	5) invalidate all cached file data.
51512115Sdyson *	6) re-read inode data for all active vnodes.
51612115Sdyson */
51712911Sphkstatic int
51812115Sdysonext2_reload(mountp, cred, p)
51912115Sdyson	register struct mount *mountp;
52012115Sdyson	struct ucred *cred;
52112115Sdyson	struct proc *p;
52212115Sdyson{
52312115Sdyson	register struct vnode *vp, *nvp, *devvp;
52412115Sdyson	struct inode *ip;
52512115Sdyson	struct buf *bp;
52612115Sdyson	struct ext2_super_block * es;
52712115Sdyson	struct ext2_sb_info *fs;
52812147Sdyson	int error;
52912115Sdyson
53012115Sdyson	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
53112115Sdyson		return (EINVAL);
53212115Sdyson	/*
53312115Sdyson	 * Step 1: invalidate all cached meta-data.
53412115Sdyson	 */
53512115Sdyson	devvp = VFSTOUFS(mountp)->um_devvp;
53612115Sdyson	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
53712115Sdyson		panic("ext2_reload: dirty1");
53812115Sdyson	/*
53912115Sdyson	 * Step 2: re-read superblock from disk.
54012115Sdyson	 * constants have been adjusted for ext2
54112115Sdyson	 */
54243301Sdillon	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
54312115Sdyson		return (error);
54412115Sdyson	es = (struct ext2_super_block *)bp->b_data;
54555313Sbde	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
54612115Sdyson		brelse(bp);
54712115Sdyson		return (EIO);		/* XXX needs translation */
54812115Sdyson	}
54912115Sdyson	fs = VFSTOUFS(mountp)->um_e2fs;
55012115Sdyson	bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
55112115Sdyson
55243301Sdillon	if((error = compute_sb_data(devvp, es, fs)) != 0) {
55312115Sdyson		brelse(bp);
55412115Sdyson		return error;
55512115Sdyson	}
55612115Sdyson#ifdef UNKLAR
55712115Sdyson	if (fs->fs_sbsize < SBSIZE)
55812115Sdyson		bp->b_flags |= B_INVAL;
55912115Sdyson#endif
56012115Sdyson	brelse(bp);
56112115Sdyson
56212115Sdysonloop:
56339678Sbde	simple_lock(&mntvnode_slock);
56412115Sdyson	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
56539678Sbde		if (vp->v_mount != mountp) {
56639678Sbde			simple_unlock(&mntvnode_slock);
56739678Sbde			goto loop;
56839678Sbde		}
56912115Sdyson		nvp = vp->v_mntvnodes.le_next;
57012115Sdyson		/*
57112115Sdyson		 * Step 4: invalidate all inactive vnodes.
57212115Sdyson		 */
57339678Sbde  		if (vrecycle(vp, &mntvnode_slock, p))
57439678Sbde  			goto loop;
57512115Sdyson		/*
57612115Sdyson		 * Step 5: invalidate all cached file data.
57712115Sdyson		 */
57839678Sbde		simple_lock(&vp->v_interlock);
57939678Sbde		simple_unlock(&mntvnode_slock);
58039678Sbde		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
58112115Sdyson			goto loop;
58239678Sbde		}
58312115Sdyson		if (vinvalbuf(vp, 0, cred, p, 0, 0))
58412115Sdyson			panic("ext2_reload: dirty2");
58512115Sdyson		/*
58612115Sdyson		 * Step 6: re-read inode data for all active vnodes.
58712115Sdyson		 */
58812115Sdyson		ip = VTOI(vp);
58939678Sbde		error =
59012115Sdyson		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
59139678Sbde		    (int)fs->s_blocksize, NOCRED, &bp);
59239678Sbde		if (error) {
59312115Sdyson			vput(vp);
59412115Sdyson			return (error);
59512115Sdyson		}
59612115Sdyson		ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
59739678Sbde		    EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)),
59839678Sbde		    &ip->i_din);
59912115Sdyson		brelse(bp);
60012115Sdyson		vput(vp);
60139678Sbde		simple_lock(&mntvnode_slock);
60212115Sdyson	}
60339678Sbde	simple_unlock(&mntvnode_slock);
60412115Sdyson	return (0);
60512115Sdyson}
60612115Sdyson
60712115Sdyson/*
60812115Sdyson * Common code for mount and mountroot
60912115Sdyson */
61012911Sphkstatic int
61112115Sdysonext2_mountfs(devvp, mp, p)
61212115Sdyson	register struct vnode *devvp;
61312115Sdyson	struct mount *mp;
61412115Sdyson	struct proc *p;
61512115Sdyson{
61612115Sdyson	register struct ufsmount *ump;
61712115Sdyson	struct buf *bp;
61812115Sdyson	register struct ext2_sb_info *fs;
61912115Sdyson	struct ext2_super_block * es;
62012115Sdyson	dev_t dev = devvp->v_rdev;
62112115Sdyson	struct partinfo dpart;
62212115Sdyson	int havepart = 0;
62312115Sdyson	int error, i, size;
62412115Sdyson	int ronly;
62512115Sdyson
62612115Sdyson	/*
62712115Sdyson	 * Disallow multiple mounts of the same device.
62812115Sdyson	 * Disallow mounting of a device that is currently in use
62912115Sdyson	 * (except for root, which might share swap device for miniroot).
63012115Sdyson	 * Flush out any old buffers remaining from a previous use.
63112115Sdyson	 */
63243301Sdillon	if ((error = vfs_mountedon(devvp)) != 0)
63312115Sdyson		return (error);
63412115Sdyson	if (vcount(devvp) > 1 && devvp != rootvp)
63512115Sdyson		return (EBUSY);
63643301Sdillon	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
63712115Sdyson		return (error);
63812115Sdyson#ifdef READONLY
63912115Sdyson/* turn on this to force it to be read-only */
64012115Sdyson	mp->mnt_flag |= MNT_RDONLY;
64112115Sdyson#endif
64212115Sdyson
64312115Sdyson	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
64453059Sphk	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
64553059Sphk	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
64653059Sphk	VOP_UNLOCK(devvp, 0, p);
64753059Sphk	if (error)
64812115Sdyson		return (error);
64912115Sdyson	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
65012115Sdyson		size = DEV_BSIZE;
65112115Sdyson	else {
65212115Sdyson		havepart = 1;
65312115Sdyson		size = dpart.disklab->d_secsize;
65412115Sdyson	}
65512115Sdyson
65612115Sdyson	bp = NULL;
65712115Sdyson	ump = NULL;
65843301Sdillon	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
65912115Sdyson		goto out;
66012115Sdyson	es = (struct ext2_super_block *)bp->b_data;
66155313Sbde	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
66212115Sdyson		error = EINVAL;		/* XXX needs translation */
66312115Sdyson		goto out;
66412115Sdyson	}
66539670Sbde	if ((es->s_state & EXT2_VALID_FS) == 0 ||
66639670Sbde	    (es->s_state & EXT2_ERROR_FS)) {
66739670Sbde		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
66839670Sbde			printf(
66939670Sbde"WARNING: Filesystem was not properly dismounted\n");
67039670Sbde		} else {
67139670Sbde			printf(
67239670Sbde"WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
67339670Sbde			error = EPERM;
67439670Sbde			goto out;
67539670Sbde		}
67639670Sbde	}
67712115Sdyson	ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
67812115Sdyson	bzero((caddr_t)ump, sizeof *ump);
67930280Sphk	ump->um_malloctype = M_EXT2NODE;
68030474Sphk	ump->um_blkatoff = ext2_blkatoff;
68130474Sphk	ump->um_truncate = ext2_truncate;
68230492Sphk	ump->um_update = ext2_update;
68330474Sphk	ump->um_valloc = ext2_valloc;
68430474Sphk	ump->um_vfree = ext2_vfree;
68512115Sdyson	/* I don't know whether this is the right strategy. Note that
68612115Sdyson	   we dynamically allocate both a ext2_sb_info and a ext2_super_block
68712115Sdyson	   while Linux keeps the super block in a locked buffer
68812115Sdyson	 */
68912115Sdyson	ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
69012115Sdyson		M_UFSMNT, M_WAITOK);
69112115Sdyson	ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
69212115Sdyson		M_UFSMNT, M_WAITOK);
69312115Sdyson	bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
69439671Sbde	if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
69539671Sbde		goto out;
69639671Sbde	/*
69739671Sbde	 * We don't free the group descriptors allocated by compute_sb_data()
69839671Sbde	 * until ext2_unmount().  This is OK since the mount will succeed.
69939671Sbde	 */
70012115Sdyson	brelse(bp);
70112115Sdyson	bp = NULL;
70212115Sdyson	fs = ump->um_e2fs;
70312115Sdyson	fs->s_rd_only = ronly;	/* ronly is set according to mnt_flags */
70412115Sdyson	/* if the fs is not mounted read-only, make sure the super block is
70512115Sdyson	   always written back on a sync()
70612115Sdyson	 */
70739670Sbde	fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
70812115Sdyson	if (ronly == 0) {
70912115Sdyson		fs->s_dirt = 1;		/* mark it modified */
71012115Sdyson		fs->s_es->s_state &= ~EXT2_VALID_FS;	/* set fs invalid */
71112115Sdyson	}
71212115Sdyson	mp->mnt_data = (qaddr_t)ump;
71350256Sbde	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
71438909Sbde	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
71512115Sdyson	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
71612115Sdyson	mp->mnt_flag |= MNT_LOCAL;
71712115Sdyson	ump->um_mountp = mp;
71812115Sdyson	ump->um_dev = dev;
71912115Sdyson	ump->um_devvp = devvp;
72012115Sdyson	/* setting those two parameters allows us to use
72112115Sdyson	   ufs_bmap w/o changse !
72212115Sdyson	*/
72312115Sdyson	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
72412115Sdyson	ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
72512115Sdyson	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
72612115Sdyson	for (i = 0; i < MAXQUOTAS; i++)
72712115Sdyson		ump->um_quotas[i] = NULLVP;
72834430Seivind	devvp->v_specmountpoint = mp;
72934430Seivind	if (ronly == 0)
73034430Seivind		ext2_sbupdate(ump, MNT_WAIT);
73112115Sdyson	return (0);
73212115Sdysonout:
73312115Sdyson	if (bp)
73412115Sdyson		brelse(bp);
73512115Sdyson	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
73612115Sdyson	if (ump) {
73739671Sbde		bsd_free(ump->um_e2fs->s_es, M_UFSMNT);
73839671Sbde		bsd_free(ump->um_e2fs, M_UFSMNT);
73912115Sdyson		bsd_free(ump, M_UFSMNT);
74012115Sdyson		mp->mnt_data = (qaddr_t)0;
74112115Sdyson	}
74212115Sdyson	return (error);
74312115Sdyson}
74412115Sdyson
74512115Sdyson/*
74612115Sdyson * unmount system call
74712115Sdyson */
74812911Sphkstatic int
74912115Sdysonext2_unmount(mp, mntflags, p)
75012115Sdyson	struct mount *mp;
75112115Sdyson	int mntflags;
75212115Sdyson	struct proc *p;
75312115Sdyson{
75412115Sdyson	register struct ufsmount *ump;
75512115Sdyson	register struct ext2_sb_info *fs;
75612115Sdyson	int error, flags, ronly, i;
75712115Sdyson
75812115Sdyson	flags = 0;
75912115Sdyson	if (mntflags & MNT_FORCE) {
76012115Sdyson		if (mp->mnt_flag & MNT_ROOTFS)
76112115Sdyson			return (EINVAL);
76212115Sdyson		flags |= FORCECLOSE;
76312115Sdyson	}
76443301Sdillon	if ((error = ext2_flushfiles(mp, flags, p)) != 0)
76512115Sdyson		return (error);
76612115Sdyson	ump = VFSTOUFS(mp);
76712115Sdyson	fs = ump->um_e2fs;
76812115Sdyson	ronly = fs->s_rd_only;
76939670Sbde	if (ronly == 0) {
77039670Sbde		if (fs->s_wasvalid)
77139670Sbde			fs->s_es->s_state |= EXT2_VALID_FS;
77212115Sdyson		ext2_sbupdate(ump, MNT_WAIT);
77312115Sdyson	}
77427881Sdyson
77512115Sdyson	/* release buffers containing group descriptors */
77612115Sdyson	for(i = 0; i < fs->s_db_per_group; i++)
77727881Sdyson		ULCK_BUF(fs->s_group_desc[i])
77839671Sbde	bsd_free(fs->s_group_desc, M_UFSMNT);
77927881Sdyson
78012115Sdyson	/* release cached inode/block bitmaps */
78112115Sdyson        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
78212115Sdyson                if (fs->s_inode_bitmap[i])
78327881Sdyson			ULCK_BUF(fs->s_inode_bitmap[i])
78427881Sdyson
78512115Sdyson        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
78612115Sdyson                if (fs->s_block_bitmap[i])
78727881Sdyson			ULCK_BUF(fs->s_block_bitmap[i])
78812115Sdyson
78934430Seivind	ump->um_devvp->v_specmountpoint = NULL;
79012115Sdyson	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
79112115Sdyson		NOCRED, p);
79212115Sdyson	vrele(ump->um_devvp);
79312115Sdyson	bsd_free(fs->s_es, M_UFSMNT);
79412115Sdyson	bsd_free(fs, M_UFSMNT);
79512115Sdyson	bsd_free(ump, M_UFSMNT);
79612115Sdyson	mp->mnt_data = (qaddr_t)0;
79712115Sdyson	mp->mnt_flag &= ~MNT_LOCAL;
79812115Sdyson	return (error);
79912115Sdyson}
80012115Sdyson
80112115Sdyson/*
80212115Sdyson * Flush out all the files in a filesystem.
80312115Sdyson */
80412911Sphkstatic int
80512115Sdysonext2_flushfiles(mp, flags, p)
80612115Sdyson	register struct mount *mp;
80712115Sdyson	int flags;
80812115Sdyson	struct proc *p;
80912115Sdyson{
81012115Sdyson	register struct ufsmount *ump;
81112147Sdyson	int error;
81212746Sbde#if QUOTA
81312746Sbde	int i;
81412746Sbde#endif
81512115Sdyson
81612115Sdyson	ump = VFSTOUFS(mp);
81712115Sdyson#if QUOTA
81812115Sdyson	if (mp->mnt_flag & MNT_QUOTA) {
81943301Sdillon		if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
82012115Sdyson			return (error);
82112115Sdyson		for (i = 0; i < MAXQUOTAS; i++) {
82212115Sdyson			if (ump->um_quotas[i] == NULLVP)
82312115Sdyson				continue;
82412115Sdyson			quotaoff(p, mp, i);
82512115Sdyson		}
82612115Sdyson		/*
82712115Sdyson		 * Here we fall through to vflush again to ensure
82812115Sdyson		 * that we have gotten rid of all the system vnodes.
82912115Sdyson		 */
83012115Sdyson	}
83112115Sdyson#endif
83212115Sdyson	error = vflush(mp, NULLVP, flags);
83312115Sdyson	return (error);
83412115Sdyson}
83512115Sdyson
83612115Sdyson/*
83712115Sdyson * Get file system statistics.
83812115Sdyson * taken from ext2/super.c ext2_statfs
83912115Sdyson */
84012911Sphkstatic int
84112115Sdysonext2_statfs(mp, sbp, p)
84212115Sdyson	struct mount *mp;
84312115Sdyson	register struct statfs *sbp;
84412115Sdyson	struct proc *p;
84512115Sdyson{
84612115Sdyson        unsigned long overhead;
84712115Sdyson	unsigned long overhead_per_group;
84812115Sdyson
84912115Sdyson	register struct ufsmount *ump;
85012115Sdyson	register struct ext2_sb_info *fs;
85112115Sdyson	register struct ext2_super_block *es;
85212115Sdyson
85312115Sdyson	ump = VFSTOUFS(mp);
85412115Sdyson	fs = ump->um_e2fs;
85512115Sdyson	es = fs->s_es;
85612115Sdyson
85712115Sdyson	if (es->s_magic != EXT2_SUPER_MAGIC)
85812115Sdyson		panic("ext2_statfs - magic number spoiled");
85912115Sdyson
86012115Sdyson	/*
86112115Sdyson	 * Compute the overhead (FS structures)
86212115Sdyson	 */
86312115Sdyson	overhead_per_group = 1 /* super block */ +
86412115Sdyson			     fs->s_db_per_group +
86512115Sdyson			     1 /* block bitmap */ +
86612115Sdyson			     1 /* inode bitmap */ +
86712115Sdyson			     fs->s_itb_per_group;
86812115Sdyson	overhead = es->s_first_data_block +
86912115Sdyson		   fs->s_groups_count * overhead_per_group;
87012115Sdyson
87112115Sdyson	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
87212115Sdyson	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
87312115Sdyson	sbp->f_blocks = es->s_blocks_count - overhead;
87412115Sdyson	sbp->f_bfree = es->s_free_blocks_count;
87512115Sdyson	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
87612115Sdyson	sbp->f_files = es->s_inodes_count;
87712115Sdyson	sbp->f_ffree = es->s_free_inodes_count;
87812115Sdyson	if (sbp != &mp->mnt_stat) {
87938909Sbde		sbp->f_type = mp->mnt_vfc->vfc_typenum;
88012115Sdyson		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
88112115Sdyson			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
88212115Sdyson		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
88312115Sdyson			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
88412115Sdyson	}
88512115Sdyson	return (0);
88612115Sdyson}
88712115Sdyson
88812115Sdyson/*
88912115Sdyson * Go through the disk queues to initiate sandbagged IO;
89012115Sdyson * go through the inodes to write those that have been modified;
89112115Sdyson * initiate the writing of the super block if it has been modified.
89212115Sdyson *
89312115Sdyson * Note: we are always called with the filesystem marked `MPBUSY'.
89412115Sdyson */
89512911Sphkstatic int
89612115Sdysonext2_sync(mp, waitfor, cred, p)
89712115Sdyson	struct mount *mp;
89812115Sdyson	int waitfor;
89912115Sdyson	struct ucred *cred;
90012115Sdyson	struct proc *p;
90112115Sdyson{
90239678Sbde	struct vnode *nvp, *vp;
90339678Sbde	struct inode *ip;
90439678Sbde	struct ufsmount *ump = VFSTOUFS(mp);
90539678Sbde	struct ext2_sb_info *fs;
90612115Sdyson	int error, allerror = 0;
90712115Sdyson
90812115Sdyson	fs = ump->um_e2fs;
90939678Sbde	if (fs->s_dirt != 0 && fs->s_rd_only != 0) {		/* XXX */
91039678Sbde		printf("fs = %s\n", fs->fs_fsmnt);
91139678Sbde		panic("ext2_sync: rofs mod");
91212115Sdyson	}
91312115Sdyson	/*
91412115Sdyson	 * Write back each (modified) inode.
91512115Sdyson	 */
91639678Sbde	simple_lock(&mntvnode_slock);
91712115Sdysonloop:
91839678Sbde	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
91912115Sdyson		/*
92012115Sdyson		 * If the vnode that we are about to sync is no longer
92112115Sdyson		 * associated with this mount point, start over.
92212115Sdyson		 */
92312115Sdyson		if (vp->v_mount != mp)
92412115Sdyson			goto loop;
92539678Sbde		simple_lock(&vp->v_interlock);
92639678Sbde		nvp = vp->v_mntvnodes.le_next;
92712115Sdyson		ip = VTOI(vp);
92839678Sbde		if (vp->v_type == VNON ||
92943309Sdillon		    ((ip->i_flag &
93012115Sdyson		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
93143395Sbde		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
93239678Sbde			simple_unlock(&vp->v_interlock);
93312115Sdyson			continue;
93439678Sbde		}
93539678Sbde		simple_unlock(&mntvnode_slock);
93639678Sbde		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
93739678Sbde		if (error) {
93839678Sbde			simple_lock(&mntvnode_slock);
93939678Sbde			if (error == ENOENT)
94039678Sbde				goto loop;
94139678Sbde			continue;
94239678Sbde		}
94343301Sdillon		if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
94412115Sdyson			allerror = error;
94539678Sbde		VOP_UNLOCK(vp, 0, p);
94639678Sbde		vrele(vp);
94739678Sbde		simple_lock(&mntvnode_slock);
94812115Sdyson	}
94939678Sbde	simple_unlock(&mntvnode_slock);
95012115Sdyson	/*
95112115Sdyson	 * Force stale file system control information to be flushed.
95212115Sdyson	 */
95339678Sbde	if (waitfor != MNT_LAZY) {
95439678Sbde		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
95539678Sbde		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
95639678Sbde			allerror = error;
95739678Sbde		VOP_UNLOCK(ump->um_devvp, 0, p);
95839678Sbde	}
95912115Sdyson#if QUOTA
96012115Sdyson	qsync(mp);
96112115Sdyson#endif
96239678Sbde	/*
96339678Sbde	 * Write back modified superblock.
96439678Sbde	 */
96539678Sbde	if (fs->s_dirt != 0) {
96639678Sbde		fs->s_dirt = 0;
96739678Sbde		fs->s_es->s_wtime = time_second;
96839678Sbde		if ((error = ext2_sbupdate(ump, waitfor)) != 0)
96939678Sbde			allerror = error;
97039678Sbde	}
97112115Sdyson	return (allerror);
97212115Sdyson}
97312115Sdyson
97412115Sdyson/*
97512115Sdyson * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
97612115Sdyson * in from disk.  If it is in core, wait for the lock bit to clear, then
97712115Sdyson * return the inode locked.  Detection and handling of mount points must be
97812115Sdyson * done by the calling routine.
97912115Sdyson */
98012911Sphkstatic int
98112115Sdysonext2_vget(mp, ino, vpp)
98212115Sdyson	struct mount *mp;
98312115Sdyson	ino_t ino;
98412115Sdyson	struct vnode **vpp;
98512115Sdyson{
98612115Sdyson	register struct ext2_sb_info *fs;
98712115Sdyson	register struct inode *ip;
98812115Sdyson	struct ufsmount *ump;
98912115Sdyson	struct buf *bp;
99012115Sdyson	struct vnode *vp;
99112115Sdyson	dev_t dev;
99230280Sphk	int i, error;
99312115Sdyson	int used_blocks;
99412115Sdyson
99512115Sdyson	ump = VFSTOUFS(mp);
99612115Sdyson	dev = ump->um_dev;
99712406Sdysonrestart:
99812115Sdyson	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
99912115Sdyson		return (0);
100012115Sdyson
100112406Sdyson	/*
100212406Sdyson	 * Lock out the creation of new entries in the FFS hash table in
100312406Sdyson	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
100412406Sdyson	 * may occur!
100512406Sdyson	 */
100612406Sdyson	if (ext2fs_inode_hash_lock) {
100712406Sdyson		while (ext2fs_inode_hash_lock) {
100812406Sdyson			ext2fs_inode_hash_lock = -1;
100936102Sbde			tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0);
101012406Sdyson		}
101112406Sdyson		goto restart;
101212406Sdyson	}
101312406Sdyson	ext2fs_inode_hash_lock = 1;
101412406Sdyson
101536102Sbde	/*
101636102Sbde	 * If this MALLOC() is performed after the getnewvnode()
101736102Sbde	 * it might block, leaving a vnode with a NULL v_data to be
101836102Sbde	 * found by ext2_sync() if a sync happens to fire right then,
101936102Sbde	 * which will cause a panic because ext2_sync() blindly
102036102Sbde	 * dereferences vp->v_data (as well it should).
102136102Sbde	 */
102236102Sbde	MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
102336102Sbde
102412115Sdyson	/* Allocate a new vnode/inode. */
102543301Sdillon	if ((error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) != 0) {
102636102Sbde		if (ext2fs_inode_hash_lock < 0)
102736102Sbde			wakeup(&ext2fs_inode_hash_lock);
102836102Sbde		ext2fs_inode_hash_lock = 0;
102912115Sdyson		*vpp = NULL;
103036102Sbde		FREE(ip, M_EXT2NODE);
103112115Sdyson		return (error);
103212115Sdyson	}
103312115Sdyson	bzero((caddr_t)ip, sizeof(struct inode));
103438998Sbde	lockinit(&ip->i_lock, PINOD, "ext2in", 0, 0);
103512115Sdyson	vp->v_data = ip;
103612115Sdyson	ip->i_vnode = vp;
103712115Sdyson	ip->i_e2fs = fs = ump->um_e2fs;
103812115Sdyson	ip->i_dev = dev;
103912115Sdyson	ip->i_number = ino;
104012115Sdyson#if QUOTA
104112115Sdyson	for (i = 0; i < MAXQUOTAS; i++)
104212115Sdyson		ip->i_dquot[i] = NODQUOT;
104312115Sdyson#endif
104412115Sdyson	/*
104512115Sdyson	 * Put it onto its hash chain and lock it so that other requests for
104612115Sdyson	 * this inode will block if they arrive while we are sleeping waiting
104712115Sdyson	 * for old data structures to be purged or for the contents of the
104812115Sdyson	 * disk portion of this inode to be read.
104912115Sdyson	 */
105012115Sdyson	ufs_ihashins(ip);
105112115Sdyson
105212406Sdyson	if (ext2fs_inode_hash_lock < 0)
105312406Sdyson		wakeup(&ext2fs_inode_hash_lock);
105412406Sdyson	ext2fs_inode_hash_lock = 0;
105512406Sdyson
105612115Sdyson	/* Read in the disk contents for the inode, copy into the inode. */
105712115Sdyson#if 0
105812115Sdysonprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
105912115Sdyson#endif
106043301Sdillon	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
106143301Sdillon	    (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
106212115Sdyson		/*
106312115Sdyson		 * The inode does not contain anything useful, so it would
106412115Sdyson		 * be misleading to leave it on its hash chain. With mode
106512115Sdyson		 * still zero, it will be unlinked and returned to the free
106612115Sdyson		 * list by vput().
106712115Sdyson		 */
106812115Sdyson		vput(vp);
106912115Sdyson		brelse(bp);
107012115Sdyson		*vpp = NULL;
107112115Sdyson		return (error);
107212115Sdyson	}
107312115Sdyson	/* convert ext2 inode to dinode */
107412115Sdyson	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
107512115Sdyson			ino_to_fsbo(fs, ino)), &ip->i_din);
107612115Sdyson	ip->i_block_group = ino_to_cg(fs, ino);
107712115Sdyson	ip->i_next_alloc_block = 0;
107812115Sdyson	ip->i_next_alloc_goal = 0;
107912115Sdyson	ip->i_prealloc_count = 0;
108012115Sdyson	ip->i_prealloc_block = 0;
108112115Sdyson        /* now we want to make sure that block pointers for unused
108212115Sdyson           blocks are zeroed out - ext2_balloc depends on this
108312115Sdyson	   although for regular files and directories only
108412115Sdyson	*/
108512115Sdyson	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
108612115Sdyson		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
108712115Sdyson		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
108812115Sdyson			ip->i_db[i] = 0;
108912115Sdyson	}
109012115Sdyson/*
109112115Sdyson	ext2_print_inode(ip);
109212115Sdyson*/
109312115Sdyson	brelse(bp);
109412115Sdyson
109512115Sdyson	/*
109612115Sdyson	 * Initialize the vnode from the inode, check for aliases.
109712115Sdyson	 * Note that the underlying vnode may have changed.
109812115Sdyson	 */
109943301Sdillon	if ((error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) != 0) {
110012115Sdyson		vput(vp);
110112115Sdyson		*vpp = NULL;
110212115Sdyson		return (error);
110312115Sdyson	}
110412115Sdyson	/*
110512115Sdyson	 * Finish inode initialization now that aliasing has been resolved.
110612115Sdyson	 */
110712115Sdyson	ip->i_devvp = ump->um_devvp;
110812115Sdyson	VREF(ip->i_devvp);
110912115Sdyson	/*
111012115Sdyson	 * Set up a generation number for this inode if it does not
111112115Sdyson	 * already have one. This should only happen on old filesystems.
111212115Sdyson	 */
111312115Sdyson	if (ip->i_gen == 0) {
111431485Sbde		ip->i_gen = random() / 2 + 1;
111512115Sdyson		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
111612115Sdyson			ip->i_flag |= IN_MODIFIED;
111712115Sdyson	}
111812115Sdyson	*vpp = vp;
111912115Sdyson	return (0);
112012115Sdyson}
112112115Sdyson
112212115Sdyson/*
112312115Sdyson * File handle to vnode
112412115Sdyson *
112512115Sdyson * Have to be really careful about stale file handles:
112612115Sdyson * - check that the inode number is valid
112712115Sdyson * - call ext2_vget() to get the locked inode
112812115Sdyson * - check for an unallocated inode (i_mode == 0)
112912115Sdyson * - check that the given client host has export rights and return
113012115Sdyson *   those rights via. exflagsp and credanonp
113112115Sdyson */
113212911Sphkstatic int
113351138Salfredext2_fhtovp(mp, fhp, vpp)
113412115Sdyson	register struct mount *mp;
113512115Sdyson	struct fid *fhp;
113612115Sdyson	struct vnode **vpp;
113712115Sdyson{
113812115Sdyson	register struct ufid *ufhp;
113912115Sdyson	struct ext2_sb_info *fs;
114012115Sdyson
114112115Sdyson	ufhp = (struct ufid *)fhp;
114212115Sdyson	fs = VFSTOUFS(mp)->um_e2fs;
114312115Sdyson	if (ufhp->ufid_ino < ROOTINO ||
114412115Sdyson	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
114512115Sdyson		return (ESTALE);
114651138Salfred	return (ufs_fhtovp(mp, ufhp, vpp));
114712115Sdyson}
114812115Sdyson
114912115Sdyson/*
115012115Sdyson * Vnode pointer to File handle
115112115Sdyson */
115212115Sdyson/* ARGSUSED */
115312911Sphkstatic int
115412115Sdysonext2_vptofh(vp, fhp)
115512115Sdyson	struct vnode *vp;
115612115Sdyson	struct fid *fhp;
115712115Sdyson{
115812115Sdyson	register struct inode *ip;
115912115Sdyson	register struct ufid *ufhp;
116012115Sdyson
116112115Sdyson	ip = VTOI(vp);
116212115Sdyson	ufhp = (struct ufid *)fhp;
116312115Sdyson	ufhp->ufid_len = sizeof(struct ufid);
116412115Sdyson	ufhp->ufid_ino = ip->i_number;
116512115Sdyson	ufhp->ufid_gen = ip->i_gen;
116612115Sdyson	return (0);
116712115Sdyson}
116812115Sdyson
116912115Sdyson/*
117012115Sdyson * Write a superblock and associated information back to disk.
117112115Sdyson */
117212911Sphkstatic int
117312115Sdysonext2_sbupdate(mp, waitfor)
117412115Sdyson	struct ufsmount *mp;
117512115Sdyson	int waitfor;
117612115Sdyson{
117712115Sdyson	register struct ext2_sb_info *fs = mp->um_e2fs;
117812115Sdyson	register struct ext2_super_block *es = fs->s_es;
117912115Sdyson	register struct buf *bp;
118041591Sarchie	int error = 0;
118112115Sdyson/*
118212115Sdysonprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
118312115Sdyson*/
118412115Sdyson	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
118512115Sdyson	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
118612115Sdyson	if (waitfor == MNT_WAIT)
118712115Sdyson		error = bwrite(bp);
118812115Sdyson	else
118912115Sdyson		bawrite(bp);
119012115Sdyson
119127881Sdyson	/*
119227881Sdyson	 * The buffers for group descriptors, inode bitmaps and block bitmaps
119327881Sdyson	 * are not busy at this point and are (hopefully) written by the
119427881Sdyson	 * usual sync mechanism. No need to write them here
119512115Sdyson		 */
119612115Sdyson
119712115Sdyson	return (error);
119812115Sdyson}
1199