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