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