Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_vfsops.c revision 1.1
      1  1.1  bouyer /*	$NetBSD: ext2fs_vfsops.c,v 1.1 1997/06/11 09:34:07 bouyer Exp $	*/
      2  1.1  bouyer 
      3  1.1  bouyer /*
      4  1.1  bouyer  * Copyright (c) 1997 Manuel Bouyer.
      5  1.1  bouyer  * Copyright (c) 1989, 1991, 1993, 1994
      6  1.1  bouyer  *	The Regents of the University of California.  All rights reserved.
      7  1.1  bouyer  *
      8  1.1  bouyer  * Redistribution and use in source and binary forms, with or without
      9  1.1  bouyer  * modification, are permitted provided that the following conditions
     10  1.1  bouyer  * are met:
     11  1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     12  1.1  bouyer  *	notice, this list of conditions and the following disclaimer.
     13  1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  bouyer  *	notice, this list of conditions and the following disclaimer in the
     15  1.1  bouyer  *	documentation and/or other materials provided with the distribution.
     16  1.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     17  1.1  bouyer  *	must display the following acknowledgement:
     18  1.1  bouyer  *	This product includes software developed by the University of
     19  1.1  bouyer  *	California, Berkeley and its contributors.
     20  1.1  bouyer  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  bouyer  *	may be used to endorse or promote products derived from this software
     22  1.1  bouyer  *	without specific prior written permission.
     23  1.1  bouyer  *
     24  1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  bouyer  * SUCH DAMAGE.
     35  1.1  bouyer  *
     36  1.1  bouyer  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
     37  1.1  bouyer  * Modified for ext2fs by Manuel Bouyer.
     38  1.1  bouyer  */
     39  1.1  bouyer 
     40  1.1  bouyer #include <sys/param.h>
     41  1.1  bouyer #include <sys/systm.h>
     42  1.1  bouyer #include <sys/namei.h>
     43  1.1  bouyer #include <sys/proc.h>
     44  1.1  bouyer #include <sys/kernel.h>
     45  1.1  bouyer #include <sys/vnode.h>
     46  1.1  bouyer #include <sys/socket.h>
     47  1.1  bouyer #include <sys/mount.h>
     48  1.1  bouyer #include <sys/buf.h>
     49  1.1  bouyer #include <sys/device.h>
     50  1.1  bouyer #include <sys/mbuf.h>
     51  1.1  bouyer #include <sys/file.h>
     52  1.1  bouyer #include <sys/disklabel.h>
     53  1.1  bouyer #include <sys/ioctl.h>
     54  1.1  bouyer #include <sys/errno.h>
     55  1.1  bouyer #include <sys/malloc.h>
     56  1.1  bouyer 
     57  1.1  bouyer #include <miscfs/specfs/specdev.h>
     58  1.1  bouyer 
     59  1.1  bouyer #include <ufs/ufs/quota.h>
     60  1.1  bouyer #include <ufs/ufs/ufsmount.h>
     61  1.1  bouyer #include <ufs/ufs/inode.h>
     62  1.1  bouyer #include <ufs/ufs/dir.h>
     63  1.1  bouyer #include <ufs/ufs/ufs_extern.h>
     64  1.1  bouyer 
     65  1.1  bouyer #include <ufs/ext2fs/ext2fs.h>
     66  1.1  bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     67  1.1  bouyer 
     68  1.1  bouyer int ext2fs_sbupdate __P((struct ufsmount *, int));
     69  1.1  bouyer int ext2fs_check_export __P((struct mount *, struct ufid *, struct mbuf *,
     70  1.1  bouyer 	struct vnode **, int *, struct ucred **));
     71  1.1  bouyer 
     72  1.1  bouyer struct vfsops ext2fs_vfsops = {
     73  1.1  bouyer 	MOUNT_EXT2FS,
     74  1.1  bouyer 	ext2fs_mount,
     75  1.1  bouyer 	ufs_start,
     76  1.1  bouyer 	ext2fs_unmount,
     77  1.1  bouyer 	ufs_root,
     78  1.1  bouyer 	ufs_quotactl,
     79  1.1  bouyer 	ext2fs_statfs,
     80  1.1  bouyer 	ext2fs_sync,
     81  1.1  bouyer 	ext2fs_vget,
     82  1.1  bouyer 	ext2fs_fhtovp,
     83  1.1  bouyer 	ext2fs_vptofh,
     84  1.1  bouyer 	ext2fs_init,
     85  1.1  bouyer 	ext2fs_mountroot,
     86  1.1  bouyer };
     87  1.1  bouyer 
     88  1.1  bouyer extern u_long ext2gennumber;
     89  1.1  bouyer 
     90  1.1  bouyer /*
     91  1.1  bouyer  * This is the generic part of fhtovp called after the underlying
     92  1.1  bouyer  * filesystem has validated the file handle.
     93  1.1  bouyer  *
     94  1.1  bouyer  * Verify that a host should have access to a filesystem, and if so
     95  1.1  bouyer  * return a vnode for the presented file handle.
     96  1.1  bouyer  */
     97  1.1  bouyer int
     98  1.1  bouyer ext2fs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp)
     99  1.1  bouyer 	register struct mount *mp;
    100  1.1  bouyer 	struct ufid *ufhp;
    101  1.1  bouyer 	struct mbuf *nam;
    102  1.1  bouyer 	struct vnode **vpp;
    103  1.1  bouyer 	int *exflagsp;
    104  1.1  bouyer 	struct ucred **credanonp;
    105  1.1  bouyer {
    106  1.1  bouyer 	register struct inode *ip;
    107  1.1  bouyer 	register struct netcred *np;
    108  1.1  bouyer 	register struct ufsmount *ump = VFSTOUFS(mp);
    109  1.1  bouyer 	struct vnode *nvp;
    110  1.1  bouyer 	int error;
    111  1.1  bouyer 
    112  1.1  bouyer 	/*
    113  1.1  bouyer 	 * Get the export permission structure for this <mp, client> tuple.
    114  1.1  bouyer 	 */
    115  1.1  bouyer 	np = vfs_export_lookup(mp, &ump->um_export, nam);
    116  1.1  bouyer 	if (np == NULL)
    117  1.1  bouyer 		return (EACCES);
    118  1.1  bouyer 
    119  1.1  bouyer 	if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
    120  1.1  bouyer 		*vpp = NULLVP;
    121  1.1  bouyer 		return (error);
    122  1.1  bouyer 	}
    123  1.1  bouyer 	ip = VTOI(nvp);
    124  1.1  bouyer 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
    125  1.1  bouyer 		ip->i_e2fs_gen != ufhp->ufid_gen) {
    126  1.1  bouyer 		vput(nvp);
    127  1.1  bouyer 		*vpp = NULLVP;
    128  1.1  bouyer 		return (ESTALE);
    129  1.1  bouyer 	}
    130  1.1  bouyer 	*vpp = nvp;
    131  1.1  bouyer 	*exflagsp = np->netc_exflags;
    132  1.1  bouyer 	*credanonp = &np->netc_anon;
    133  1.1  bouyer 	return (0);
    134  1.1  bouyer }
    135  1.1  bouyer 
    136  1.1  bouyer 
    137  1.1  bouyer /*
    138  1.1  bouyer  * Called by main() when ext2fs is going to be mounted as root.
    139  1.1  bouyer  *
    140  1.1  bouyer  * Name is updated by mount(8) after booting.
    141  1.1  bouyer  */
    142  1.1  bouyer #define ROOTNAME	"root_device"
    143  1.1  bouyer 
    144  1.1  bouyer int
    145  1.1  bouyer ext2fs_mountroot()
    146  1.1  bouyer {
    147  1.1  bouyer 	extern struct vnode *rootvp;
    148  1.1  bouyer 	register struct m_ext2fs *fs;
    149  1.1  bouyer 	register struct mount *mp;
    150  1.1  bouyer 	struct proc *p = curproc;	/* XXX */
    151  1.1  bouyer 	struct ufsmount *ump;
    152  1.1  bouyer 	size_t size;
    153  1.1  bouyer 	int error;
    154  1.1  bouyer 
    155  1.1  bouyer 	if (root_device->dv_class != DV_DISK)
    156  1.1  bouyer 		return (ENODEV);
    157  1.1  bouyer 
    158  1.1  bouyer 	/*
    159  1.1  bouyer 	 * Get vnodes for swapdev and rootdev.
    160  1.1  bouyer 	 */
    161  1.1  bouyer 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
    162  1.1  bouyer 		panic("ext2fs_mountroot: can't setup bdevvp's");
    163  1.1  bouyer 
    164  1.1  bouyer 	mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
    165  1.1  bouyer 	bzero((char *)mp, sizeof(struct mount));
    166  1.1  bouyer 	mp->mnt_op = &ext2fs_vfsops;
    167  1.1  bouyer 	mp->mnt_flag = MNT_RDONLY;
    168  1.1  bouyer 	if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
    169  1.1  bouyer 		free(mp, M_MOUNT);
    170  1.1  bouyer 		return (error);
    171  1.1  bouyer 	}
    172  1.1  bouyer 	if ((error = vfs_lock(mp)) != 0) {
    173  1.1  bouyer 		(void)ext2fs_unmount(mp, 0, p);
    174  1.1  bouyer 		free(mp, M_MOUNT);
    175  1.1  bouyer 		return (error);
    176  1.1  bouyer 	}
    177  1.1  bouyer 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    178  1.1  bouyer 	mp->mnt_vnodecovered = NULLVP;
    179  1.1  bouyer 	ump = VFSTOUFS(mp);
    180  1.1  bouyer 	fs = ump->um_e2fs;
    181  1.1  bouyer 	bzero(fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt));
    182  1.1  bouyer 	fs->e2fs_fsmnt[0] = '/';
    183  1.1  bouyer 	bcopy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
    184  1.1  bouyer 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    185  1.1  bouyer 		&size);
    186  1.1  bouyer 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    187  1.1  bouyer 	(void)ext2fs_statfs(mp, &mp->mnt_stat, p);
    188  1.1  bouyer 	vfs_unlock(mp);
    189  1.1  bouyer 	inittodr(fs->e2fs.e2fs_wtime);
    190  1.1  bouyer 	return (0);
    191  1.1  bouyer }
    192  1.1  bouyer 
    193  1.1  bouyer /*
    194  1.1  bouyer  * VFS Operations.
    195  1.1  bouyer  *
    196  1.1  bouyer  * mount system call
    197  1.1  bouyer  */
    198  1.1  bouyer int
    199  1.1  bouyer ext2fs_mount(mp, path, data, ndp, p)
    200  1.1  bouyer 	register struct mount *mp;
    201  1.1  bouyer 	const char *path;
    202  1.1  bouyer 	void * data;
    203  1.1  bouyer 	struct nameidata *ndp;
    204  1.1  bouyer 	struct proc *p;
    205  1.1  bouyer {
    206  1.1  bouyer 	struct vnode *devvp;
    207  1.1  bouyer 	struct ufs_args args;
    208  1.1  bouyer 	struct ufsmount *ump = NULL;
    209  1.1  bouyer 	register struct m_ext2fs *fs;
    210  1.1  bouyer 	size_t size;
    211  1.1  bouyer 	int error, flags;
    212  1.1  bouyer 	mode_t accessmode;
    213  1.1  bouyer 
    214  1.1  bouyer 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    215  1.1  bouyer 	if (error)
    216  1.1  bouyer 		return (error);
    217  1.1  bouyer 	/*
    218  1.1  bouyer 	 * If updating, check whether changing from read-only to
    219  1.1  bouyer 	 * read/write; if there is no device name, that's all we do.
    220  1.1  bouyer 	 */
    221  1.1  bouyer 	if (mp->mnt_flag & MNT_UPDATE) {
    222  1.1  bouyer 		ump = VFSTOUFS(mp);
    223  1.1  bouyer 		fs = ump->um_e2fs;
    224  1.1  bouyer 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
    225  1.1  bouyer 			flags = WRITECLOSE;
    226  1.1  bouyer 			if (mp->mnt_flag & MNT_FORCE)
    227  1.1  bouyer 				flags |= FORCECLOSE;
    228  1.1  bouyer 			if (vfs_busy(mp))
    229  1.1  bouyer 				return (EBUSY);
    230  1.1  bouyer 			error = ext2fs_flushfiles(mp, flags, p);
    231  1.1  bouyer 			if (error == 0 &&
    232  1.1  bouyer 				ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    233  1.1  bouyer 				(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    234  1.1  bouyer 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    235  1.1  bouyer 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
    236  1.1  bouyer 			}
    237  1.1  bouyer 			vfs_unbusy(mp);
    238  1.1  bouyer 			if (error)
    239  1.1  bouyer 				return (error);
    240  1.1  bouyer 			fs->e2fs_ronly = 1;
    241  1.1  bouyer 		}
    242  1.1  bouyer 		if (mp->mnt_flag & MNT_RELOAD) {
    243  1.1  bouyer 			error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
    244  1.1  bouyer 			if (error)
    245  1.1  bouyer 				return (error);
    246  1.1  bouyer 		}
    247  1.1  bouyer 		if (fs->e2fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    248  1.1  bouyer 			/*
    249  1.1  bouyer 			 * If upgrade to read-write by non-root, then verify
    250  1.1  bouyer 			 * that user has necessary permissions on the device.
    251  1.1  bouyer 			 */
    252  1.1  bouyer 			if (p->p_ucred->cr_uid != 0) {
    253  1.1  bouyer 				devvp = ump->um_devvp;
    254  1.1  bouyer 				VOP_LOCK(devvp);
    255  1.1  bouyer 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
    256  1.1  bouyer 						   p->p_ucred, p);
    257  1.1  bouyer 				if (error) {
    258  1.1  bouyer 					VOP_UNLOCK(devvp);
    259  1.1  bouyer 					return (error);
    260  1.1  bouyer 				}
    261  1.1  bouyer 				VOP_UNLOCK(devvp);
    262  1.1  bouyer 			}
    263  1.1  bouyer 			fs->e2fs_ronly = 0;
    264  1.1  bouyer 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    265  1.1  bouyer 				fs->e2fs.e2fs_state = 0;
    266  1.1  bouyer 			else
    267  1.1  bouyer 				fs->e2fs.e2fs_state = E2FS_ERRORS;
    268  1.1  bouyer 			fs->e2fs_fmod = 1;
    269  1.1  bouyer 		}
    270  1.1  bouyer 		if (args.fspec == 0) {
    271  1.1  bouyer 			/*
    272  1.1  bouyer 			 * Process export requests.
    273  1.1  bouyer 			 */
    274  1.1  bouyer 			return (vfs_export(mp, &ump->um_export, &args.export));
    275  1.1  bouyer 		}
    276  1.1  bouyer 	}
    277  1.1  bouyer 	/*
    278  1.1  bouyer 	 * Not an update, or updating the name: look up the name
    279  1.1  bouyer 	 * and verify that it refers to a sensible block device.
    280  1.1  bouyer 	 */
    281  1.1  bouyer 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    282  1.1  bouyer 	if ((error = namei(ndp)) != 0)
    283  1.1  bouyer 		return (error);
    284  1.1  bouyer 	devvp = ndp->ni_vp;
    285  1.1  bouyer 
    286  1.1  bouyer 	if (devvp->v_type != VBLK) {
    287  1.1  bouyer 		vrele(devvp);
    288  1.1  bouyer 		return (ENOTBLK);
    289  1.1  bouyer 	}
    290  1.1  bouyer 	if (major(devvp->v_rdev) >= nblkdev) {
    291  1.1  bouyer 		vrele(devvp);
    292  1.1  bouyer 		return (ENXIO);
    293  1.1  bouyer 	}
    294  1.1  bouyer 	/*
    295  1.1  bouyer 	 * If mount by non-root, then verify that user has necessary
    296  1.1  bouyer 	 * permissions on the device.
    297  1.1  bouyer 	 */
    298  1.1  bouyer 	if (p->p_ucred->cr_uid != 0) {
    299  1.1  bouyer 		accessmode = VREAD;
    300  1.1  bouyer 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    301  1.1  bouyer 			accessmode |= VWRITE;
    302  1.1  bouyer 		VOP_LOCK(devvp);
    303  1.1  bouyer 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    304  1.1  bouyer 		if (error) {
    305  1.1  bouyer 			vput(devvp);
    306  1.1  bouyer 			return (error);
    307  1.1  bouyer 		}
    308  1.1  bouyer 		VOP_UNLOCK(devvp);
    309  1.1  bouyer 	}
    310  1.1  bouyer 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    311  1.1  bouyer 		error = ext2fs_mountfs(devvp, mp, p);
    312  1.1  bouyer 	else {
    313  1.1  bouyer 		if (devvp != ump->um_devvp)
    314  1.1  bouyer 			error = EINVAL;	/* needs translation */
    315  1.1  bouyer 		else
    316  1.1  bouyer 			vrele(devvp);
    317  1.1  bouyer 	}
    318  1.1  bouyer 	if (error) {
    319  1.1  bouyer 		vrele(devvp);
    320  1.1  bouyer 		return (error);
    321  1.1  bouyer 	}
    322  1.1  bouyer 	ump = VFSTOUFS(mp);
    323  1.1  bouyer 	fs = ump->um_e2fs;
    324  1.1  bouyer 	(void) copyinstr(path, fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt) - 1, &size);
    325  1.1  bouyer 	bzero(fs->e2fs_fsmnt + size, sizeof(fs->e2fs_fsmnt) - size);
    326  1.1  bouyer 	bcopy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
    327  1.1  bouyer 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    328  1.1  bouyer 		&size);
    329  1.1  bouyer 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    330  1.1  bouyer 	if (fs->e2fs_fmod != 0) {	/* XXX */
    331  1.1  bouyer 		fs->e2fs_fmod = 0;
    332  1.1  bouyer 		if (fs->e2fs.e2fs_state == 0)
    333  1.1  bouyer 			fs->e2fs.e2fs_wtime = time.tv_sec;
    334  1.1  bouyer 		else
    335  1.1  bouyer 			printf("%s: file system not clean; please fsck(8)\n",
    336  1.1  bouyer 				mp->mnt_stat.f_mntfromname);
    337  1.1  bouyer 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
    338  1.1  bouyer 	}
    339  1.1  bouyer 	return (0);
    340  1.1  bouyer }
    341  1.1  bouyer 
    342  1.1  bouyer /*
    343  1.1  bouyer  * Reload all incore data for a filesystem (used after running fsck on
    344  1.1  bouyer  * the root filesystem and finding things to fix). The filesystem must
    345  1.1  bouyer  * be mounted read-only.
    346  1.1  bouyer  *
    347  1.1  bouyer  * Things to do to update the mount:
    348  1.1  bouyer  *	1) invalidate all cached meta-data.
    349  1.1  bouyer  *	2) re-read superblock from disk.
    350  1.1  bouyer  *	3) re-read summary information from disk.
    351  1.1  bouyer  *	4) invalidate all inactive vnodes.
    352  1.1  bouyer  *	5) invalidate all cached file data.
    353  1.1  bouyer  *	6) re-read inode data for all active vnodes.
    354  1.1  bouyer  */
    355  1.1  bouyer int
    356  1.1  bouyer ext2fs_reload(mountp, cred, p)
    357  1.1  bouyer 	register struct mount *mountp;
    358  1.1  bouyer 	struct ucred *cred;
    359  1.1  bouyer 	struct proc *p;
    360  1.1  bouyer {
    361  1.1  bouyer 	register struct vnode *vp, *nvp, *devvp;
    362  1.1  bouyer 	struct inode *ip;
    363  1.1  bouyer 	struct buf *bp;
    364  1.1  bouyer 	struct m_ext2fs *fs;
    365  1.1  bouyer 	struct ext2fs *newfs;
    366  1.1  bouyer 	struct partinfo dpart;
    367  1.1  bouyer 	int i, size, error;
    368  1.1  bouyer 
    369  1.1  bouyer 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
    370  1.1  bouyer 		return (EINVAL);
    371  1.1  bouyer 	/*
    372  1.1  bouyer 	 * Step 1: invalidate all cached meta-data.
    373  1.1  bouyer 	 */
    374  1.1  bouyer 	devvp = VFSTOUFS(mountp)->um_devvp;
    375  1.1  bouyer 	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
    376  1.1  bouyer 		panic("ext2fs_reload: dirty1");
    377  1.1  bouyer 	/*
    378  1.1  bouyer 	 * Step 2: re-read superblock from disk.
    379  1.1  bouyer 	 */
    380  1.1  bouyer 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
    381  1.1  bouyer 		size = DEV_BSIZE;
    382  1.1  bouyer 	else
    383  1.1  bouyer 		size = dpart.disklab->d_secsize;
    384  1.1  bouyer 	error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
    385  1.1  bouyer 	if (error)
    386  1.1  bouyer 		return (error);
    387  1.1  bouyer 	newfs = (struct ext2fs *)bp->b_data;
    388  1.1  bouyer 	if (newfs->e2fs_magic != E2FS_MAGIC || newfs->e2fs_rev != E2FS_REV) {
    389  1.1  bouyer #ifdef DIAGNOSTIC
    390  1.1  bouyer 		printf("Wrong magic number: %x (expected %x for ext2 fs)",
    391  1.1  bouyer 					newfs->e2fs_magic, E2FS_MAGIC);
    392  1.1  bouyer 		printf("or wrong revision number: %x (expected %x for ext2 fs)",
    393  1.1  bouyer 					newfs->e2fs_rev, E2FS_REV);
    394  1.1  bouyer #endif
    395  1.1  bouyer 		brelse(bp);
    396  1.1  bouyer 		return (EIO);		/* XXX needs translation */
    397  1.1  bouyer 	}
    398  1.1  bouyer 	if (newfs->e2fs_log_bsize > 2) { /* block size = 1024|2048|4096 */
    399  1.1  bouyer #ifdef DIAGNOSTIC
    400  1.1  bouyer 		printf("wrong block size: %d (expected <=2 for ext2 fs)\n",
    401  1.1  bouyer 			newfs->e2fs_log_bsize);
    402  1.1  bouyer #endif
    403  1.1  bouyer 		brelse(bp);
    404  1.1  bouyer 		return (EIO);	   /* XXX needs translation */
    405  1.1  bouyer 	}
    406  1.1  bouyer 
    407  1.1  bouyer 
    408  1.1  bouyer 	fs = VFSTOUFS(mountp)->um_e2fs;
    409  1.1  bouyer 	/*
    410  1.1  bouyer 	 * copy in new superblock, and compute in-memory values
    411  1.1  bouyer 	 */
    412  1.1  bouyer 	bcopy(newfs, &fs->e2fs, SBSIZE);
    413  1.1  bouyer 	fs->e2fs_ncg = howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
    414  1.1  bouyer 		fs->e2fs.e2fs_bpg);
    415  1.1  bouyer 	/* XXX assume hw bsize = 512 */
    416  1.1  bouyer 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
    417  1.1  bouyer 	fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
    418  1.1  bouyer 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
    419  1.1  bouyer 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
    420  1.1  bouyer 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
    421  1.1  bouyer 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
    422  1.1  bouyer 			fs->e2fs_bsize / sizeof(struct ext2_gd));
    423  1.1  bouyer 	fs->e2fs_ipb = fs->e2fs_bsize / sizeof(struct ext2fs_dinode);
    424  1.1  bouyer 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
    425  1.1  bouyer 
    426  1.1  bouyer 	/*
    427  1.1  bouyer 	 * Step 3: re-read summary information from disk.
    428  1.1  bouyer 	 */
    429  1.1  bouyer 
    430  1.1  bouyer 	for (i=0; i < fs->e2fs_ngdb; i++) {
    431  1.1  bouyer 		error = bread(devvp , fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
    432  1.1  bouyer 				fs->e2fs_bsize, NOCRED, &bp);
    433  1.1  bouyer 		if (error)
    434  1.1  bouyer 			return (error);
    435  1.1  bouyer 		bcopy(bp->b_data,
    436  1.1  bouyer 			&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
    437  1.1  bouyer 			fs->e2fs_bsize);
    438  1.1  bouyer 		brelse(bp);
    439  1.1  bouyer 	}
    440  1.1  bouyer 
    441  1.1  bouyer loop:
    442  1.1  bouyer 	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    443  1.1  bouyer 		nvp = vp->v_mntvnodes.le_next;
    444  1.1  bouyer 		/*
    445  1.1  bouyer 		 * Step 4: invalidate all inactive vnodes.
    446  1.1  bouyer 		 */
    447  1.1  bouyer 		if (vp->v_usecount == 0) {
    448  1.1  bouyer 			vgone(vp);
    449  1.1  bouyer 			continue;
    450  1.1  bouyer 		}
    451  1.1  bouyer 		/*
    452  1.1  bouyer 		 * Step 5: invalidate all cached file data.
    453  1.1  bouyer 		 */
    454  1.1  bouyer 		if (vget(vp, 1))
    455  1.1  bouyer 			goto loop;
    456  1.1  bouyer 		if (vinvalbuf(vp, 0, cred, p, 0, 0))
    457  1.1  bouyer 			panic("ext2fs_reload: dirty2");
    458  1.1  bouyer 		/*
    459  1.1  bouyer 		 * Step 6: re-read inode data for all active vnodes.
    460  1.1  bouyer 		 */
    461  1.1  bouyer 		ip = VTOI(vp);
    462  1.1  bouyer 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
    463  1.1  bouyer 				  (int)fs->e2fs_bsize, NOCRED, &bp);
    464  1.1  bouyer 		if (error) {
    465  1.1  bouyer 			vput(vp);
    466  1.1  bouyer 			return (error);
    467  1.1  bouyer 		}
    468  1.1  bouyer 		bcopy((struct ext2fs_dinode *)bp->b_data +
    469  1.1  bouyer 			ino_to_fsbo(fs, ip->i_number),
    470  1.1  bouyer 			&ip->i_din.e2fs_din, sizeof(struct ext2fs_dinode));
    471  1.1  bouyer 		brelse(bp);
    472  1.1  bouyer 		vput(vp);
    473  1.1  bouyer 		if (vp->v_mount != mountp)
    474  1.1  bouyer 			goto loop;
    475  1.1  bouyer 	}
    476  1.1  bouyer 	return (0);
    477  1.1  bouyer }
    478  1.1  bouyer 
    479  1.1  bouyer /*
    480  1.1  bouyer  * Common code for mount and mountroot
    481  1.1  bouyer  */
    482  1.1  bouyer int
    483  1.1  bouyer ext2fs_mountfs(devvp, mp, p)
    484  1.1  bouyer 	register struct vnode *devvp;
    485  1.1  bouyer 	struct mount *mp;
    486  1.1  bouyer 	struct proc *p;
    487  1.1  bouyer {
    488  1.1  bouyer 	register struct ufsmount *ump;
    489  1.1  bouyer 	struct buf *bp;
    490  1.1  bouyer 	register struct ext2fs *fs;
    491  1.1  bouyer 	register struct m_ext2fs *m_fs;
    492  1.1  bouyer 	dev_t dev;
    493  1.1  bouyer 	struct partinfo dpart;
    494  1.1  bouyer 	int error, i, size, ronly;
    495  1.1  bouyer 	struct ucred *cred;
    496  1.1  bouyer 	extern struct vnode *rootvp;
    497  1.1  bouyer 
    498  1.1  bouyer 	dev = devvp->v_rdev;
    499  1.1  bouyer 	cred = p ? p->p_ucred : NOCRED;
    500  1.1  bouyer 	/*
    501  1.1  bouyer 	 * Disallow multiple mounts of the same device.
    502  1.1  bouyer 	 * Disallow mounting of a device that is currently in use
    503  1.1  bouyer 	 * (except for root, which might share swap device for miniroot).
    504  1.1  bouyer 	 * Flush out any old buffers remaining from a previous use.
    505  1.1  bouyer 	 */
    506  1.1  bouyer 	if ((error = vfs_mountedon(devvp)) != 0)
    507  1.1  bouyer 		return (error);
    508  1.1  bouyer 	if (vcount(devvp) > 1 && devvp != rootvp)
    509  1.1  bouyer 		return (EBUSY);
    510  1.1  bouyer 	if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
    511  1.1  bouyer 		return (error);
    512  1.1  bouyer 
    513  1.1  bouyer 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    514  1.1  bouyer 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    515  1.1  bouyer 	if (error)
    516  1.1  bouyer 		return (error);
    517  1.1  bouyer 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    518  1.1  bouyer 		size = DEV_BSIZE;
    519  1.1  bouyer 	else
    520  1.1  bouyer 		size = dpart.disklab->d_secsize;
    521  1.1  bouyer 
    522  1.1  bouyer 	bp = NULL;
    523  1.1  bouyer 	ump = NULL;
    524  1.1  bouyer 
    525  1.1  bouyer #ifdef DEBUG_EXT2
    526  1.1  bouyer 	printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
    527  1.1  bouyer 			sizeof(struct ext2fs_dinode));
    528  1.1  bouyer #endif
    529  1.1  bouyer 	error = bread(devvp, (SBOFF / DEV_BSIZE), SBSIZE, cred, &bp);
    530  1.1  bouyer 	if (error)
    531  1.1  bouyer 		goto out;
    532  1.1  bouyer 	fs = (struct ext2fs *)bp->b_data;
    533  1.1  bouyer 	if (fs->e2fs_magic != E2FS_MAGIC || fs->e2fs_rev != E2FS_REV) {
    534  1.1  bouyer #ifdef DIAGNOSTIC
    535  1.1  bouyer 		printf("Wrong magic number: %x (expected %x for ext2 fs)",
    536  1.1  bouyer 			fs->e2fs_magic, E2FS_MAGIC);
    537  1.1  bouyer 		printf(" or wrong revision number: %x (expected %x for ext2 fs)\n",
    538  1.1  bouyer 			fs->e2fs_rev, E2FS_REV);
    539  1.1  bouyer #endif
    540  1.1  bouyer 		error = EINVAL;		/* XXX needs translation */
    541  1.1  bouyer 		goto out;
    542  1.1  bouyer 	}
    543  1.1  bouyer 
    544  1.1  bouyer 	if (fs->e2fs_log_bsize > 2) { /* block size = 1024|2048|4096 */
    545  1.1  bouyer #ifdef DIAGNOSTIC
    546  1.1  bouyer 		printf("wrong block size: %d (expected <2 for ext2 fs)\n",
    547  1.1  bouyer 			fs->e2fs_log_bsize);
    548  1.1  bouyer #endif
    549  1.1  bouyer 		error = EINVAL;	 /* XXX needs translation */
    550  1.1  bouyer 		goto out;
    551  1.1  bouyer 	}
    552  1.1  bouyer 
    553  1.1  bouyer 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    554  1.1  bouyer 	bzero((caddr_t)ump, sizeof *ump);
    555  1.1  bouyer 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
    556  1.1  bouyer 	bcopy(bp->b_data, ump->um_e2fs, SBSIZE);
    557  1.1  bouyer 	brelse(bp);
    558  1.1  bouyer 	bp = NULL;
    559  1.1  bouyer 	m_fs = ump->um_e2fs;
    560  1.1  bouyer 	m_fs->e2fs_ronly = ronly;
    561  1.1  bouyer 	if (ronly == 0) {
    562  1.1  bouyer 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
    563  1.1  bouyer 			m_fs->e2fs.e2fs_state = 0;
    564  1.1  bouyer 		else
    565  1.1  bouyer 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
    566  1.1  bouyer 		m_fs->e2fs_fmod = 1;
    567  1.1  bouyer 	}
    568  1.1  bouyer 
    569  1.1  bouyer 	/* compute dynamic sb infos */
    570  1.1  bouyer 	m_fs->e2fs_ncg =
    571  1.1  bouyer 		howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
    572  1.1  bouyer 		m_fs->e2fs.e2fs_bpg);
    573  1.1  bouyer 	/* XXX assume hw bsize = 512 */
    574  1.1  bouyer 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
    575  1.1  bouyer 	m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
    576  1.1  bouyer 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
    577  1.1  bouyer 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
    578  1.1  bouyer 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
    579  1.1  bouyer 	m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
    580  1.1  bouyer 		m_fs->e2fs_bsize / sizeof(struct ext2_gd));
    581  1.1  bouyer 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / sizeof(struct ext2fs_dinode);
    582  1.1  bouyer 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
    583  1.1  bouyer 
    584  1.1  bouyer 	m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
    585  1.1  bouyer 		M_UFSMNT, M_WAITOK);
    586  1.1  bouyer 	for (i=0; i < m_fs->e2fs_ngdb; i++) {
    587  1.1  bouyer 		error = bread(devvp , fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)?0:1)+i+1),
    588  1.1  bouyer 			m_fs->e2fs_bsize, NOCRED, &bp);
    589  1.1  bouyer 		if (error) {
    590  1.1  bouyer 			free(m_fs->e2fs_gd, M_UFSMNT);
    591  1.1  bouyer 			goto out;
    592  1.1  bouyer 		}
    593  1.1  bouyer 		bcopy(bp->b_data,
    594  1.1  bouyer 			&m_fs->e2fs_gd[i* m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
    595  1.1  bouyer 			m_fs->e2fs_bsize);
    596  1.1  bouyer 		brelse(bp);
    597  1.1  bouyer 		bp = NULL;
    598  1.1  bouyer 	}
    599  1.1  bouyer 
    600  1.1  bouyer 	mp->mnt_data = (qaddr_t)ump;
    601  1.1  bouyer 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    602  1.1  bouyer 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_EXT2FS);
    603  1.1  bouyer 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
    604  1.1  bouyer 	mp->mnt_flag |= MNT_LOCAL;
    605  1.1  bouyer 	ump->um_mountp = mp;
    606  1.1  bouyer 	ump->um_dev = dev;
    607  1.1  bouyer 	ump->um_devvp = devvp;
    608  1.1  bouyer 	ump->um_nindir = NINDIR(m_fs);
    609  1.1  bouyer 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
    610  1.1  bouyer 	ump->um_seqinc = 1; /* no frags */
    611  1.1  bouyer 	devvp->v_specflags |= SI_MOUNTEDON;
    612  1.1  bouyer 	return (0);
    613  1.1  bouyer out:
    614  1.1  bouyer 	if (bp)
    615  1.1  bouyer 		brelse(bp);
    616  1.1  bouyer 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    617  1.1  bouyer 	if (ump) {
    618  1.1  bouyer 		free(ump->um_e2fs, M_UFSMNT);
    619  1.1  bouyer 		free(ump, M_UFSMNT);
    620  1.1  bouyer 		mp->mnt_data = (qaddr_t)0;
    621  1.1  bouyer 	}
    622  1.1  bouyer 	return (error);
    623  1.1  bouyer }
    624  1.1  bouyer 
    625  1.1  bouyer /*
    626  1.1  bouyer  * unmount system call
    627  1.1  bouyer  */
    628  1.1  bouyer int
    629  1.1  bouyer ext2fs_unmount(mp, mntflags, p)
    630  1.1  bouyer 	struct mount *mp;
    631  1.1  bouyer 	int mntflags;
    632  1.1  bouyer 	struct proc *p;
    633  1.1  bouyer {
    634  1.1  bouyer 	register struct ufsmount *ump;
    635  1.1  bouyer 	register struct m_ext2fs *fs;
    636  1.1  bouyer 	int error, flags;
    637  1.1  bouyer 
    638  1.1  bouyer 	flags = 0;
    639  1.1  bouyer 	if (mntflags & MNT_FORCE)
    640  1.1  bouyer 		flags |= FORCECLOSE;
    641  1.1  bouyer 	if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
    642  1.1  bouyer 		return (error);
    643  1.1  bouyer 	ump = VFSTOUFS(mp);
    644  1.1  bouyer 	fs = ump->um_e2fs;
    645  1.1  bouyer 	if (fs->e2fs_ronly == 0 &&
    646  1.1  bouyer 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
    647  1.1  bouyer 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
    648  1.1  bouyer 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
    649  1.1  bouyer 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
    650  1.1  bouyer 	}
    651  1.1  bouyer 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
    652  1.1  bouyer 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
    653  1.1  bouyer 		NOCRED, p);
    654  1.1  bouyer 	vrele(ump->um_devvp);
    655  1.1  bouyer 	free(fs->e2fs_gd, M_UFSMNT);
    656  1.1  bouyer 	free(fs, M_UFSMNT);
    657  1.1  bouyer 	free(ump, M_UFSMNT);
    658  1.1  bouyer 	mp->mnt_data = (qaddr_t)0;
    659  1.1  bouyer 	mp->mnt_flag &= ~MNT_LOCAL;
    660  1.1  bouyer 	return (error);
    661  1.1  bouyer }
    662  1.1  bouyer 
    663  1.1  bouyer /*
    664  1.1  bouyer  * Flush out all the files in a filesystem.
    665  1.1  bouyer  */
    666  1.1  bouyer int
    667  1.1  bouyer ext2fs_flushfiles(mp, flags, p)
    668  1.1  bouyer 	register struct mount *mp;
    669  1.1  bouyer 	int flags;
    670  1.1  bouyer 	struct proc *p;
    671  1.1  bouyer {
    672  1.1  bouyer 	extern int doforce;
    673  1.1  bouyer 	register struct ufsmount *ump;
    674  1.1  bouyer 	int error;
    675  1.1  bouyer 
    676  1.1  bouyer 	if (!doforce)
    677  1.1  bouyer 		flags &= ~FORCECLOSE;
    678  1.1  bouyer 	ump = VFSTOUFS(mp);
    679  1.1  bouyer 	error = vflush(mp, NULLVP, flags);
    680  1.1  bouyer 	return (error);
    681  1.1  bouyer }
    682  1.1  bouyer 
    683  1.1  bouyer /*
    684  1.1  bouyer  * Get file system statistics.
    685  1.1  bouyer  */
    686  1.1  bouyer int
    687  1.1  bouyer ext2fs_statfs(mp, sbp, p)
    688  1.1  bouyer 	struct mount *mp;
    689  1.1  bouyer 	register struct statfs *sbp;
    690  1.1  bouyer 	struct proc *p;
    691  1.1  bouyer {
    692  1.1  bouyer 	register struct ufsmount *ump;
    693  1.1  bouyer 	register struct m_ext2fs *fs;
    694  1.1  bouyer 	u_int32_t overhead, overhead_per_group;
    695  1.1  bouyer 
    696  1.1  bouyer 	ump = VFSTOUFS(mp);
    697  1.1  bouyer 	fs = ump->um_e2fs;
    698  1.1  bouyer 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
    699  1.1  bouyer 		panic("ext2fs_statfs");
    700  1.1  bouyer #ifdef COMPAT_09
    701  1.1  bouyer 	sbp->f_type = 1;
    702  1.1  bouyer #else
    703  1.1  bouyer 	sbp->f_type = 0;
    704  1.1  bouyer #endif
    705  1.1  bouyer 
    706  1.1  bouyer 	/*
    707  1.1  bouyer 	 * Compute the overhead (FS structures)
    708  1.1  bouyer 	 */
    709  1.1  bouyer 	overhead_per_group = 1 /* super block */ +
    710  1.1  bouyer 						 fs->e2fs_ngdb +
    711  1.1  bouyer 						 1 /* block bitmap */ +
    712  1.1  bouyer 						 1 /* inode bitmap */ +
    713  1.1  bouyer 						 fs->e2fs_itpg;
    714  1.1  bouyer 	overhead = fs->e2fs.e2fs_first_dblock +
    715  1.1  bouyer 		   fs->e2fs_ncg * overhead_per_group;
    716  1.1  bouyer 
    717  1.1  bouyer 
    718  1.1  bouyer 	sbp->f_bsize = fs->e2fs_bsize;
    719  1.1  bouyer 	sbp->f_iosize = fs->e2fs_bsize;
    720  1.1  bouyer 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
    721  1.1  bouyer 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
    722  1.1  bouyer 	sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
    723  1.1  bouyer 	sbp->f_files =  fs->e2fs.e2fs_icount;
    724  1.1  bouyer 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
    725  1.1  bouyer 	if (sbp != &mp->mnt_stat) {
    726  1.1  bouyer 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    727  1.1  bouyer 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    728  1.1  bouyer 	}
    729  1.1  bouyer 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    730  1.1  bouyer 	return (0);
    731  1.1  bouyer }
    732  1.1  bouyer 
    733  1.1  bouyer /*
    734  1.1  bouyer  * Go through the disk queues to initiate sandbagged IO;
    735  1.1  bouyer  * go through the inodes to write those that have been modified;
    736  1.1  bouyer  * initiate the writing of the super block if it has been modified.
    737  1.1  bouyer  *
    738  1.1  bouyer  * Note: we are always called with the filesystem marked `MPBUSY'.
    739  1.1  bouyer  */
    740  1.1  bouyer int
    741  1.1  bouyer ext2fs_sync(mp, waitfor, cred, p)
    742  1.1  bouyer 	struct mount *mp;
    743  1.1  bouyer 	int waitfor;
    744  1.1  bouyer 	struct ucred *cred;
    745  1.1  bouyer 	struct proc *p;
    746  1.1  bouyer {
    747  1.1  bouyer 	register struct vnode *vp;
    748  1.1  bouyer 	register struct inode *ip;
    749  1.1  bouyer 	register struct ufsmount *ump = VFSTOUFS(mp);
    750  1.1  bouyer 	register struct m_ext2fs *fs;
    751  1.1  bouyer 	int error, allerror = 0;
    752  1.1  bouyer 
    753  1.1  bouyer 	fs = ump->um_e2fs;
    754  1.1  bouyer 	/*
    755  1.1  bouyer 	 * Write back modified superblock.
    756  1.1  bouyer 	 * Consistency check that the superblock
    757  1.1  bouyer 	 * is still in the buffer cache.
    758  1.1  bouyer 	 */
    759  1.1  bouyer 	if (fs->e2fs_fmod != 0) {
    760  1.1  bouyer 		if (fs->e2fs_ronly != 0) {		/* XXX */
    761  1.1  bouyer 			printf("fs = %s\n", fs->e2fs_fsmnt);
    762  1.1  bouyer 			panic("update: rofs mod");
    763  1.1  bouyer 		}
    764  1.1  bouyer 		fs->e2fs_fmod = 0;
    765  1.1  bouyer 		fs->e2fs.e2fs_wtime = time.tv_sec;
    766  1.1  bouyer 		allerror = ext2fs_cgupdate(ump, waitfor);
    767  1.1  bouyer 	}
    768  1.1  bouyer 	/*
    769  1.1  bouyer 	 * Write back each (modified) inode.
    770  1.1  bouyer 	 */
    771  1.1  bouyer loop:
    772  1.1  bouyer 	for (vp = mp->mnt_vnodelist.lh_first;
    773  1.1  bouyer 		 vp != NULL;
    774  1.1  bouyer 		 vp = vp->v_mntvnodes.le_next) {
    775  1.1  bouyer 		/*
    776  1.1  bouyer 		 * If the vnode that we are about to sync is no longer
    777  1.1  bouyer 		 * associated with this mount point, start over.
    778  1.1  bouyer 		 */
    779  1.1  bouyer 		if (vp->v_mount != mp)
    780  1.1  bouyer 			goto loop;
    781  1.1  bouyer 		if (VOP_ISLOCKED(vp))
    782  1.1  bouyer 			continue;
    783  1.1  bouyer 		ip = VTOI(vp);
    784  1.1  bouyer 		if ((ip->i_flag &
    785  1.1  bouyer 			(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
    786  1.1  bouyer 			vp->v_dirtyblkhd.lh_first == NULL)
    787  1.1  bouyer 			continue;
    788  1.1  bouyer 		if (vget(vp, 1))
    789  1.1  bouyer 			goto loop;
    790  1.1  bouyer 		if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
    791  1.1  bouyer 			allerror = error;
    792  1.1  bouyer 		vput(vp);
    793  1.1  bouyer 	}
    794  1.1  bouyer 	/*
    795  1.1  bouyer 	 * Force stale file system control information to be flushed.
    796  1.1  bouyer 	 */
    797  1.1  bouyer 	if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
    798  1.1  bouyer 		allerror = error;
    799  1.1  bouyer 	return (allerror);
    800  1.1  bouyer }
    801  1.1  bouyer 
    802  1.1  bouyer /*
    803  1.1  bouyer  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
    804  1.1  bouyer  * in from disk.  If it is in core, wait for the lock bit to clear, then
    805  1.1  bouyer  * return the inode locked.  Detection and handling of mount points must be
    806  1.1  bouyer  * done by the calling routine.
    807  1.1  bouyer  */
    808  1.1  bouyer int
    809  1.1  bouyer ext2fs_vget(mp, ino, vpp)
    810  1.1  bouyer 	struct mount *mp;
    811  1.1  bouyer 	ino_t ino;
    812  1.1  bouyer 	struct vnode **vpp;
    813  1.1  bouyer {
    814  1.1  bouyer 	register struct m_ext2fs *fs;
    815  1.1  bouyer 	register struct inode *ip;
    816  1.1  bouyer 	struct ufsmount *ump;
    817  1.1  bouyer 	struct buf *bp;
    818  1.1  bouyer 	struct vnode *vp;
    819  1.1  bouyer 	dev_t dev;
    820  1.1  bouyer 	int error;
    821  1.1  bouyer 
    822  1.1  bouyer 	ump = VFSTOUFS(mp);
    823  1.1  bouyer 	dev = ump->um_dev;
    824  1.1  bouyer 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
    825  1.1  bouyer 		return (0);
    826  1.1  bouyer 
    827  1.1  bouyer 	/* Allocate a new vnode/inode. */
    828  1.1  bouyer 	if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
    829  1.1  bouyer 		*vpp = NULL;
    830  1.1  bouyer 		return (error);
    831  1.1  bouyer 	}
    832  1.1  bouyer 	MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2FSNODE, M_WAITOK);
    833  1.1  bouyer 	bzero((caddr_t)ip, sizeof(struct inode));
    834  1.1  bouyer 	vp->v_data = ip;
    835  1.1  bouyer 	ip->i_vnode = vp;
    836  1.1  bouyer 	ip->i_e2fs = fs = ump->um_e2fs;
    837  1.1  bouyer 	ip->i_dev = dev;
    838  1.1  bouyer 	ip->i_number = ino;
    839  1.1  bouyer 	ip->i_e2fs_last_lblk = 0;
    840  1.1  bouyer 	ip->i_e2fs_last_blk = 0;
    841  1.1  bouyer 
    842  1.1  bouyer 	/*
    843  1.1  bouyer 	 * Put it onto its hash chain and lock it so that other requests for
    844  1.1  bouyer 	 * this inode will block if they arrive while we are sleeping waiting
    845  1.1  bouyer 	 * for old data structures to be purged or for the contents of the
    846  1.1  bouyer 	 * disk portion of this inode to be read.
    847  1.1  bouyer 	 */
    848  1.1  bouyer 	ufs_ihashins(ip);
    849  1.1  bouyer 
    850  1.1  bouyer 	/* Read in the disk contents for the inode, copy into the inode. */
    851  1.1  bouyer 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
    852  1.1  bouyer 			  (int)fs->e2fs_bsize, NOCRED, &bp);
    853  1.1  bouyer 	if (error) {
    854  1.1  bouyer 		/*
    855  1.1  bouyer 		 * The inode does not contain anything useful, so it would
    856  1.1  bouyer 		 * be misleading to leave it on its hash chain. With mode
    857  1.1  bouyer 		 * still zero, it will be unlinked and returned to the free
    858  1.1  bouyer 		 * list by vput().
    859  1.1  bouyer 		 */
    860  1.1  bouyer 		vput(vp);
    861  1.1  bouyer 		brelse(bp);
    862  1.1  bouyer 		*vpp = NULL;
    863  1.1  bouyer 		return (error);
    864  1.1  bouyer 	}
    865  1.1  bouyer 	bcopy(((struct ext2fs_dinode*)bp->b_data + ino_to_fsbo(fs, ino)),
    866  1.1  bouyer 				&ip->i_din, sizeof(struct ext2fs_dinode));
    867  1.1  bouyer 
    868  1.1  bouyer 	brelse(bp);
    869  1.1  bouyer 
    870  1.1  bouyer 	/*
    871  1.1  bouyer 	 * Initialize the vnode from the inode, check for aliases.
    872  1.1  bouyer 	 * Note that the underlying vnode may have changed.
    873  1.1  bouyer 	 */
    874  1.1  bouyer 	error = ufs_vinit(mp, ext2fs_specop_p, EXT2FS_FIFOOPS, &vp);
    875  1.1  bouyer 	if (error) {
    876  1.1  bouyer 		vput(vp);
    877  1.1  bouyer 		*vpp = NULL;
    878  1.1  bouyer 		return (error);
    879  1.1  bouyer 	}
    880  1.1  bouyer 	/*
    881  1.1  bouyer 	 * Finish inode initialization now that aliasing has been resolved.
    882  1.1  bouyer 	 */
    883  1.1  bouyer 	ip->i_devvp = ump->um_devvp;
    884  1.1  bouyer 	VREF(ip->i_devvp);
    885  1.1  bouyer 	/*
    886  1.1  bouyer 	 * Set up a generation number for this inode if it does not
    887  1.1  bouyer 	 * already have one. This should only happen on old filesystems.
    888  1.1  bouyer 	 */
    889  1.1  bouyer 	if (ip->i_e2fs_gen == 0) {
    890  1.1  bouyer 		if (++ext2gennumber < (u_long)time.tv_sec)
    891  1.1  bouyer 			ext2gennumber = time.tv_sec;
    892  1.1  bouyer 		ip->i_e2fs_gen = ext2gennumber;
    893  1.1  bouyer 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    894  1.1  bouyer 			ip->i_flag |= IN_MODIFIED;
    895  1.1  bouyer 	}
    896  1.1  bouyer 
    897  1.1  bouyer 	*vpp = vp;
    898  1.1  bouyer 	return (0);
    899  1.1  bouyer }
    900  1.1  bouyer 
    901  1.1  bouyer /*
    902  1.1  bouyer  * File handle to vnode
    903  1.1  bouyer  *
    904  1.1  bouyer  * Have to be really careful about stale file handles:
    905  1.1  bouyer  * - check that the inode number is valid
    906  1.1  bouyer  * - call ext2fs_vget() to get the locked inode
    907  1.1  bouyer  * - check for an unallocated inode (i_mode == 0)
    908  1.1  bouyer  * - check that the given client host has export rights and return
    909  1.1  bouyer  *   those rights via. exflagsp and credanonp
    910  1.1  bouyer  */
    911  1.1  bouyer int
    912  1.1  bouyer ext2fs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
    913  1.1  bouyer 	register struct mount *mp;
    914  1.1  bouyer 	struct fid *fhp;
    915  1.1  bouyer 	struct mbuf *nam;
    916  1.1  bouyer 	struct vnode **vpp;
    917  1.1  bouyer 	int *exflagsp;
    918  1.1  bouyer 	struct ucred **credanonp;
    919  1.1  bouyer {
    920  1.1  bouyer 	register struct ufid *ufhp;
    921  1.1  bouyer 	struct m_ext2fs *fs;
    922  1.1  bouyer 
    923  1.1  bouyer 	ufhp = (struct ufid *)fhp;
    924  1.1  bouyer 	fs = VFSTOUFS(mp)->um_e2fs;
    925  1.1  bouyer 	if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
    926  1.1  bouyer 		ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
    927  1.1  bouyer 		return (ESTALE);
    928  1.1  bouyer 	return (ext2fs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
    929  1.1  bouyer }
    930  1.1  bouyer 
    931  1.1  bouyer /*
    932  1.1  bouyer  * Vnode pointer to File handle
    933  1.1  bouyer  */
    934  1.1  bouyer /* ARGSUSED */
    935  1.1  bouyer int
    936  1.1  bouyer ext2fs_vptofh(vp, fhp)
    937  1.1  bouyer 	struct vnode *vp;
    938  1.1  bouyer 	struct fid *fhp;
    939  1.1  bouyer {
    940  1.1  bouyer 	register struct inode *ip;
    941  1.1  bouyer 	register struct ufid *ufhp;
    942  1.1  bouyer 
    943  1.1  bouyer 	ip = VTOI(vp);
    944  1.1  bouyer 	ufhp = (struct ufid *)fhp;
    945  1.1  bouyer 	ufhp->ufid_len = sizeof(struct ufid);
    946  1.1  bouyer 	ufhp->ufid_ino = ip->i_number;
    947  1.1  bouyer 	ufhp->ufid_gen = ip->i_e2fs_gen;
    948  1.1  bouyer 	return (0);
    949  1.1  bouyer }
    950  1.1  bouyer 
    951  1.1  bouyer /*
    952  1.1  bouyer  * Write a superblock and associated information back to disk.
    953  1.1  bouyer  */
    954  1.1  bouyer int
    955  1.1  bouyer ext2fs_sbupdate(mp, waitfor)
    956  1.1  bouyer 	struct ufsmount *mp;
    957  1.1  bouyer 	int waitfor;
    958  1.1  bouyer {
    959  1.1  bouyer 	register struct m_ext2fs *fs = mp->um_e2fs;
    960  1.1  bouyer 	register struct buf *bp;
    961  1.1  bouyer 	int error = 0;
    962  1.1  bouyer 
    963  1.1  bouyer 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
    964  1.1  bouyer 	bcopy((caddr_t)(&fs->e2fs), bp->b_data, SBSIZE);
    965  1.1  bouyer 	if (waitfor == MNT_WAIT)
    966  1.1  bouyer 		error = bwrite(bp);
    967  1.1  bouyer 	else
    968  1.1  bouyer 		bawrite(bp);
    969  1.1  bouyer 	return (error);
    970  1.1  bouyer }
    971  1.1  bouyer 
    972  1.1  bouyer int
    973  1.1  bouyer ext2fs_cgupdate(mp, waitfor)
    974  1.1  bouyer 	struct ufsmount *mp;
    975  1.1  bouyer 	int waitfor;
    976  1.1  bouyer {
    977  1.1  bouyer 	register struct m_ext2fs *fs = mp->um_e2fs;
    978  1.1  bouyer 	register struct buf *bp;
    979  1.1  bouyer 	int i, error = 0, allerror = 0;
    980  1.1  bouyer 
    981  1.1  bouyer 	allerror = ext2fs_sbupdate(mp, waitfor);
    982  1.1  bouyer 	for (i = 0; i < fs->e2fs_ngdb; i++) {
    983  1.1  bouyer 		bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
    984  1.1  bouyer 			fs->e2fs_bsize, 0, 0);
    985  1.1  bouyer 		bcopy(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
    986  1.1  bouyer 		 bp->b_data, fs->e2fs_bsize);
    987  1.1  bouyer 		if (waitfor == MNT_WAIT)
    988  1.1  bouyer 			error = bwrite(bp);
    989  1.1  bouyer 		else
    990  1.1  bouyer 			bawrite(bp);
    991  1.1  bouyer 	}
    992  1.1  bouyer 
    993  1.1  bouyer 	if (!allerror && error)
    994  1.1  bouyer 		allerror = error;
    995  1.1  bouyer 	return (allerror);
    996  1.1  bouyer }
    997