ext2_vfsops.c revision 243311
1/*-
2 *  modified for EXT2FS support in Lites 1.1
3 *
4 *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5 *  University of Utah, Department of Computer Science
6 */
7/*-
8 * Copyright (c) 1989, 1991, 1993, 1994
9 *	The Regents of the University of California.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)ffs_vfsops.c	8.8 (Berkeley) 4/18/94
36 * $FreeBSD: head/sys/fs/ext2fs/ext2_vfsops.c 243311 2012-11-19 22:43:45Z attilio $
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/namei.h>
42#include <sys/priv.h>
43#include <sys/proc.h>
44#include <sys/kernel.h>
45#include <sys/vnode.h>
46#include <sys/mount.h>
47#include <sys/bio.h>
48#include <sys/buf.h>
49#include <sys/conf.h>
50#include <sys/endian.h>
51#include <sys/fcntl.h>
52#include <sys/malloc.h>
53#include <sys/stat.h>
54#include <sys/mutex.h>
55
56#include <geom/geom.h>
57#include <geom/geom_vfs.h>
58
59#include <fs/ext2fs/ext2_mount.h>
60#include <fs/ext2fs/inode.h>
61
62#include <fs/ext2fs/fs.h>
63#include <fs/ext2fs/ext2fs.h>
64#include <fs/ext2fs/ext2_dinode.h>
65#include <fs/ext2fs/ext2_extern.h>
66
67static int	ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
68static int	ext2_mountfs(struct vnode *, struct mount *);
69static int	ext2_reload(struct mount *mp, struct thread *td);
70static int	ext2_sbupdate(struct ext2mount *, int);
71static int	ext2_cgupdate(struct ext2mount *, int);
72static vfs_unmount_t		ext2_unmount;
73static vfs_root_t		ext2_root;
74static vfs_statfs_t		ext2_statfs;
75static vfs_sync_t		ext2_sync;
76static vfs_vget_t		ext2_vget;
77static vfs_fhtovp_t		ext2_fhtovp;
78static vfs_mount_t		ext2_mount;
79
80MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
81static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
82
83static struct vfsops ext2fs_vfsops = {
84	.vfs_fhtovp =		ext2_fhtovp,
85	.vfs_mount =		ext2_mount,
86	.vfs_root =		ext2_root,	/* root inode via vget */
87	.vfs_statfs =		ext2_statfs,
88	.vfs_sync =		ext2_sync,
89	.vfs_unmount =		ext2_unmount,
90	.vfs_vget =		ext2_vget,
91};
92
93VFS_SET(ext2fs_vfsops, ext2fs, 0);
94
95static int	ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
96		    int ronly);
97static int	compute_sb_data(struct vnode * devvp,
98		    struct ext2fs * es, struct m_ext2fs * fs);
99
100static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr",
101    "noclusterw", "noexec", "export", "force", "from", "multilabel",
102    "suiddir", "nosymfollow", "sync", "union", NULL };
103
104/*
105 * VFS Operations.
106 *
107 * mount system call
108 */
109static int
110ext2_mount(struct mount *mp)
111{
112	struct vfsoptlist *opts;
113	struct vnode *devvp;
114	struct thread *td;
115	struct ext2mount *ump = NULL;
116	struct m_ext2fs *fs;
117	struct nameidata nd, *ndp = &nd;
118	accmode_t accmode;
119	char *path, *fspec;
120	int error, flags, len;
121
122	td = curthread;
123	opts = mp->mnt_optnew;
124
125	if (vfs_filteropt(opts, ext2_opts))
126		return (EINVAL);
127
128	vfs_getopt(opts, "fspath", (void **)&path, NULL);
129	/* Double-check the length of path.. */
130	if (strlen(path) >= MAXMNTLEN - 1)
131		return (ENAMETOOLONG);
132
133	fspec = NULL;
134	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
135	if (!error && fspec[len - 1] != '\0')
136		return (EINVAL);
137
138	/*
139	 * If updating, check whether changing from read-only to
140	 * read/write; if there is no device name, that's all we do.
141	 */
142	if (mp->mnt_flag & MNT_UPDATE) {
143		ump = VFSTOEXT2(mp);
144		fs = ump->um_e2fs;
145		error = 0;
146		if (fs->e2fs_ronly == 0 &&
147		    vfs_flagopt(opts, "ro", NULL, 0)) {
148			error = VFS_SYNC(mp, MNT_WAIT);
149			if (error)
150				return (error);
151			flags = WRITECLOSE;
152			if (mp->mnt_flag & MNT_FORCE)
153				flags |= FORCECLOSE;
154			error = ext2_flushfiles(mp, flags, td);
155			if ( error == 0 && fs->e2fs_wasvalid && ext2_cgupdate(ump, MNT_WAIT) == 0) {
156				fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
157				ext2_sbupdate(ump, MNT_WAIT);
158			}
159			fs->e2fs_ronly = 1;
160			vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
161			DROP_GIANT();
162			g_topology_lock();
163			g_access(ump->um_cp, 0, -1, 0);
164			g_topology_unlock();
165			PICKUP_GIANT();
166		}
167		if (!error && (mp->mnt_flag & MNT_RELOAD))
168			error = ext2_reload(mp, td);
169		if (error)
170			return (error);
171		devvp = ump->um_devvp;
172		if (fs->e2fs_ronly && !vfs_flagopt(opts, "ro", NULL, 0)) {
173			if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
174				return (EPERM);
175
176			/*
177			 * If upgrade to read-write by non-root, then verify
178			 * that user has necessary permissions on the device.
179			 */
180			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
181			error = VOP_ACCESS(devvp, VREAD | VWRITE,
182			    td->td_ucred, td);
183			if (error)
184				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
185			if (error) {
186				VOP_UNLOCK(devvp, 0);
187				return (error);
188			}
189			VOP_UNLOCK(devvp, 0);
190			DROP_GIANT();
191			g_topology_lock();
192			error = g_access(ump->um_cp, 0, 1, 0);
193			g_topology_unlock();
194			PICKUP_GIANT();
195			if (error)
196				return (error);
197
198			if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 ||
199			    (fs->e2fs->e2fs_state & E2FS_ERRORS)) {
200				if (mp->mnt_flag & MNT_FORCE) {
201					printf(
202"WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
203				} else {
204					printf(
205"WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
206					    fs->e2fs_fsmnt);
207					return (EPERM);
208				}
209			}
210			fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;
211			(void)ext2_cgupdate(ump, MNT_WAIT);
212			fs->e2fs_ronly = 0;
213			MNT_ILOCK(mp);
214			mp->mnt_flag &= ~MNT_RDONLY;
215			MNT_IUNLOCK(mp);
216		}
217		if (vfs_flagopt(opts, "export", NULL, 0)) {
218			/* Process export requests in vfs_mount.c. */
219			return (error);
220		}
221	}
222
223	/*
224	 * Not an update, or updating the name: look up the name
225	 * and verify that it refers to a sensible disk device.
226	 */
227	if (fspec == NULL)
228		return (EINVAL);
229	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
230	if ((error = namei(ndp)) != 0)
231		return (error);
232	NDFREE(ndp, NDF_ONLY_PNBUF);
233	devvp = ndp->ni_vp;
234
235	if (!vn_isdisk(devvp, &error)) {
236		vput(devvp);
237		return (error);
238	}
239
240	/*
241	 * If mount by non-root, then verify that user has necessary
242	 * permissions on the device.
243	 *
244	 * XXXRW: VOP_ACCESS() enough?
245	 */
246	accmode = VREAD;
247	if ((mp->mnt_flag & MNT_RDONLY) == 0)
248		accmode |= VWRITE;
249	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
250	if (error)
251		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
252	if (error) {
253		vput(devvp);
254		return (error);
255	}
256
257	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
258		error = ext2_mountfs(devvp, mp);
259	} else {
260		if (devvp != ump->um_devvp) {
261			vput(devvp);
262			return (EINVAL);	/* needs translation */
263		} else
264			vput(devvp);
265	}
266	if (error) {
267		vrele(devvp);
268		return (error);
269	}
270	ump = VFSTOEXT2(mp);
271	fs = ump->um_e2fs;
272
273	/*
274	 * Note that this strncpy() is ok because of a check at the start
275	 * of ext2_mount().
276	 */
277	strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN);
278	fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0';
279	vfs_mountedfrom(mp, fspec);
280	return (0);
281}
282
283static int
284ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
285{
286
287	if (es->e2fs_magic != E2FS_MAGIC) {
288		printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
289		    devtoname(dev), es->e2fs_magic, E2FS_MAGIC);
290		return (1);
291	}
292	if (es->e2fs_rev > E2FS_REV0) {
293		if (es->e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) {
294			printf(
295"WARNING: mount of %s denied due to unsupported optional features\n",
296			    devtoname(dev));
297			return (1);
298		}
299		if (!ronly &&
300		    (es->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP)) {
301			printf("WARNING: R/W mount of %s denied due to "
302			    "unsupported optional features\n", devtoname(dev));
303			return (1);
304		}
305	}
306	return (0);
307}
308
309/*
310 * This computes the fields of the  ext2_sb_info structure from the
311 * data in the ext2_super_block structure read in.
312 */
313static int
314compute_sb_data(struct vnode *devvp, struct ext2fs *es,
315    struct m_ext2fs *fs)
316{
317	int db_count, error;
318	int i;
319	int logic_sb_block = 1;	/* XXX for now */
320	struct buf *bp;
321
322	fs->e2fs_bsize = EXT2_MIN_BLOCK_SIZE << es->e2fs_log_bsize;
323	fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
324	fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
325	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
326	fs->e2fs_blocksize_bits = es->e2fs_log_bsize + 10;
327	fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize;
328	if (fs->e2fs_fsize)
329		fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
330	fs->e2fs_bpg = es->e2fs_bpg;
331	fs->e2fs_fpg = es->e2fs_fpg;
332	fs->e2fs_ipg = es->e2fs_ipg;
333	if (es->e2fs_rev == E2FS_REV0) {
334		fs->e2fs_first_inode = EXT2_FIRSTINO;
335		fs->e2fs_isize = E2FS_REV0_INODE_SIZE ;
336	} else {
337		fs->e2fs_first_inode = es->e2fs_first_ino;
338		fs->e2fs_isize = es->e2fs_inode_size;
339
340		/*
341		 * Simple sanity check for superblock inode size value.
342		 */
343		if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
344		    EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
345		    (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
346			printf("ext2fs: invalid inode size %d\n",
347			    fs->e2fs_isize);
348			return (EIO);
349		}
350	}
351	/* Check for extra isize in big inodes. */
352	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT4F_ROCOMPAT_EXTRA_ISIZE) &&
353	    EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
354		printf("ext2fs: no space for extra inode timestamps\n");
355		return (EINVAL);
356	}
357
358	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
359	fs->e2fs_itpg = fs->e2fs_ipg /fs->e2fs_ipb;
360	fs->e2fs_descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
361	/* s_resuid / s_resgid ? */
362	fs->e2fs_gcount = (es->e2fs_bcount - es->e2fs_first_dblock +
363	    EXT2_BLOCKS_PER_GROUP(fs) - 1) / EXT2_BLOCKS_PER_GROUP(fs);
364	db_count = (fs->e2fs_gcount + EXT2_DESC_PER_BLOCK(fs) - 1) /
365	    EXT2_DESC_PER_BLOCK(fs);
366	fs->e2fs_gdbcount = db_count;
367	fs->e2fs_gd = malloc(db_count * fs->e2fs_bsize,
368	    M_EXT2MNT, M_WAITOK);
369	fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
370	    sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK);
371
372	/*
373	 * Adjust logic_sb_block.
374	 * Godmar thinks: if the blocksize is greater than 1024, then
375	 * the superblock is logically part of block zero.
376	 */
377	if(fs->e2fs_bsize > SBSIZE)
378		logic_sb_block = 0;
379	for (i = 0; i < db_count; i++) {
380		error = bread(devvp ,
381			 fsbtodb(fs, logic_sb_block + i + 1 ),
382			fs->e2fs_bsize, NOCRED, &bp);
383		if (error) {
384			free(fs->e2fs_gd, M_EXT2MNT);
385			brelse(bp);
386			return (error);
387		}
388		e2fs_cgload((struct ext2_gd *)bp->b_data,
389		    &fs->e2fs_gd[
390			i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
391		    fs->e2fs_bsize);
392		brelse(bp);
393		bp = NULL;
394	}
395	fs->e2fs_total_dir = 0;
396	for (i=0; i < fs->e2fs_gcount; i++){
397		fs->e2fs_total_dir += fs->e2fs_gd[i].ext2bgd_ndirs;
398		fs->e2fs_contigdirs[i] = 0;
399	}
400	if (es->e2fs_rev == E2FS_REV0 ||
401	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
402		fs->e2fs_maxfilesize = 0x7fffffff;
403	else
404		fs->e2fs_maxfilesize = 0x7fffffffffffffff;
405	return (0);
406}
407
408/*
409 * Reload all incore data for a filesystem (used after running fsck on
410 * the root filesystem and finding things to fix). The filesystem must
411 * be mounted read-only.
412 *
413 * Things to do to update the mount:
414 *	1) invalidate all cached meta-data.
415 *	2) re-read superblock from disk.
416 *	3) invalidate all cluster summary information.
417 *	4) invalidate all inactive vnodes.
418 *	5) invalidate all cached file data.
419 *	6) re-read inode data for all active vnodes.
420 * XXX we are missing some steps, in particular # 3, this has to be reviewed.
421 */
422static int
423ext2_reload(struct mount *mp, struct thread *td)
424{
425	struct vnode *vp, *mvp, *devvp;
426	struct inode *ip;
427	struct buf *bp;
428	struct ext2fs *es;
429	struct m_ext2fs *fs;
430	struct csum *sump;
431	int error, i;
432	int32_t *lp;
433
434	if ((mp->mnt_flag & MNT_RDONLY) == 0)
435		return (EINVAL);
436	/*
437	 * Step 1: invalidate all cached meta-data.
438	 */
439	devvp = VFSTOEXT2(mp)->um_devvp;
440	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
441	if (vinvalbuf(devvp, 0, 0, 0) != 0)
442		panic("ext2_reload: dirty1");
443	VOP_UNLOCK(devvp, 0);
444
445	/*
446	 * Step 2: re-read superblock from disk.
447	 * constants have been adjusted for ext2
448	 */
449	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
450		return (error);
451	es = (struct ext2fs *)bp->b_data;
452	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
453		brelse(bp);
454		return (EIO);		/* XXX needs translation */
455	}
456	fs = VFSTOEXT2(mp)->um_e2fs;
457	bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
458
459	if((error = compute_sb_data(devvp, es, fs)) != 0) {
460		brelse(bp);
461		return (error);
462	}
463#ifdef UNKLAR
464	if (fs->fs_sbsize < SBSIZE)
465		bp->b_flags |= B_INVAL;
466#endif
467	brelse(bp);
468
469	/*
470	 * Step 3: invalidate all cluster summary information.
471	 */
472	if (fs->e2fs_contigsumsize > 0) {
473		lp = fs->e2fs_maxcluster;
474		sump = fs->e2fs_clustersum;
475		for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
476			*lp++ = fs->e2fs_contigsumsize;
477			sump->cs_init = 0;
478			bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
479		}
480	}
481
482loop:
483	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
484		/*
485		 * Step 4: invalidate all cached file data.
486		 */
487		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
488			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
489			goto loop;
490		}
491		if (vinvalbuf(vp, 0, 0, 0))
492			panic("ext2_reload: dirty2");
493
494		/*
495		 * Step 5: re-read inode data for all active vnodes.
496		 */
497		ip = VTOI(vp);
498		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
499		    (int)fs->e2fs_bsize, NOCRED, &bp);
500		if (error) {
501			VOP_UNLOCK(vp, 0);
502			vrele(vp);
503			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
504			return (error);
505		}
506		ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data +
507		    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
508		brelse(bp);
509		VOP_UNLOCK(vp, 0);
510		vrele(vp);
511	}
512	return (0);
513}
514
515/*
516 * Common code for mount and mountroot.
517 */
518static int
519ext2_mountfs(struct vnode *devvp, struct mount *mp)
520{
521	struct ext2mount *ump;
522	struct buf *bp;
523	struct m_ext2fs *fs;
524	struct ext2fs *es;
525	struct cdev *dev = devvp->v_rdev;
526	struct g_consumer *cp;
527	struct bufobj *bo;
528	struct csum *sump;
529	int error;
530	int ronly;
531	int i, size;
532	int32_t *lp;
533
534	ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
535	/* XXX: use VOP_ACESS to check FS perms */
536	DROP_GIANT();
537	g_topology_lock();
538	error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
539	g_topology_unlock();
540	PICKUP_GIANT();
541	VOP_UNLOCK(devvp, 0);
542	if (error)
543		return (error);
544
545	/* XXX: should we check for some sectorsize or 512 instead? */
546	if (((SBSIZE % cp->provider->sectorsize) != 0) ||
547	    (SBSIZE < cp->provider->sectorsize)) {
548		DROP_GIANT();
549		g_topology_lock();
550		g_vfs_close(cp);
551		g_topology_unlock();
552		PICKUP_GIANT();
553		return (EINVAL);
554	}
555
556	bo = &devvp->v_bufobj;
557	bo->bo_private = cp;
558	bo->bo_ops = g_vfs_bufops;
559	if (devvp->v_rdev->si_iosize_max != 0)
560		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
561	if (mp->mnt_iosize_max > MAXPHYS)
562		mp->mnt_iosize_max = MAXPHYS;
563
564	bp = NULL;
565	ump = NULL;
566	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
567		goto out;
568	es = (struct ext2fs *)bp->b_data;
569	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
570		error = EINVAL;		/* XXX needs translation */
571		goto out;
572	}
573	if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
574	    (es->e2fs_state & E2FS_ERRORS)) {
575		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
576			printf(
577"WARNING: Filesystem was not properly dismounted\n");
578		} else {
579			printf(
580"WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
581			error = EPERM;
582			goto out;
583		}
584	}
585	ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
586
587	/*
588	 * I don't know whether this is the right strategy. Note that
589	 * we dynamically allocate both an ext2_sb_info and an ext2_super_block
590	 * while Linux keeps the super block in a locked buffer.
591	 */
592	ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
593		M_EXT2MNT, M_WAITOK);
594	ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
595		M_EXT2MNT, M_WAITOK);
596	mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
597	bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
598	if ((error = compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
599		goto out;
600
601	/*
602	 * Calculate the maximum contiguous blocks and size of cluster summary
603	 * array.  In FFS this is done by newfs; however, the superblock
604	 * in ext2fs doesn't have these variables, so we can calculate
605	 * them here.
606	 */
607	ump->um_e2fs->e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
608	if (ump->um_e2fs->e2fs_maxcontig > 0)
609		ump->um_e2fs->e2fs_contigsumsize =
610		    MIN(ump->um_e2fs->e2fs_maxcontig, EXT2_MAXCONTIG);
611	else
612		ump->um_e2fs->e2fs_contigsumsize = 0;
613	if (ump->um_e2fs->e2fs_contigsumsize > 0) {
614		size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
615		ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
616		size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
617		ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
618		lp = ump->um_e2fs->e2fs_maxcluster;
619		sump = ump->um_e2fs->e2fs_clustersum;
620		for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
621			*lp++ = ump->um_e2fs->e2fs_contigsumsize;
622			sump->cs_init = 0;
623			sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
624			    sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
625		}
626	}
627
628	brelse(bp);
629	bp = NULL;
630	fs = ump->um_e2fs;
631	fs->e2fs_ronly = ronly;	/* ronly is set according to mnt_flags */
632
633	/*
634	 * If the fs is not mounted read-only, make sure the super block is
635	 * always written back on a sync().
636	 */
637	fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
638	if (ronly == 0) {
639		fs->e2fs_fmod = 1;		/* mark it modified */
640		fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;	/* set fs invalid */
641	}
642	mp->mnt_data = ump;
643	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
644	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
645	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
646	MNT_ILOCK(mp);
647	mp->mnt_flag |= MNT_LOCAL;
648	MNT_IUNLOCK(mp);
649	ump->um_mountp = mp;
650	ump->um_dev = dev;
651	ump->um_devvp = devvp;
652	ump->um_bo = &devvp->v_bufobj;
653	ump->um_cp = cp;
654
655	/*
656	 * Setting those two parameters allowed us to use
657	 * ufs_bmap w/o changse!
658	 */
659	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
660	ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
661	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
662	if (ronly == 0)
663		ext2_sbupdate(ump, MNT_WAIT);
664	/*
665	 * Initialize filesystem stat information in mount struct.
666	 */
667	MNT_ILOCK(mp);
668 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
669	MNT_IUNLOCK(mp);
670	return (0);
671out:
672	if (bp)
673		brelse(bp);
674	if (cp != NULL) {
675		DROP_GIANT();
676		g_topology_lock();
677		g_vfs_close(cp);
678		g_topology_unlock();
679		PICKUP_GIANT();
680	}
681	if (ump) {
682	  	mtx_destroy(EXT2_MTX(ump));
683		free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
684		free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
685		free(ump->um_e2fs->e2fs, M_EXT2MNT);
686		free(ump->um_e2fs, M_EXT2MNT);
687		free(ump, M_EXT2MNT);
688		mp->mnt_data = NULL;
689	}
690	return (error);
691}
692
693/*
694 * Unmount system call.
695 */
696static int
697ext2_unmount(struct mount *mp, int mntflags)
698{
699	struct ext2mount *ump;
700	struct m_ext2fs *fs;
701	struct csum *sump;
702	int error, flags, i, ronly;
703
704	flags = 0;
705	if (mntflags & MNT_FORCE) {
706		if (mp->mnt_flag & MNT_ROOTFS)
707			return (EINVAL);
708		flags |= FORCECLOSE;
709	}
710	if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
711		return (error);
712	ump = VFSTOEXT2(mp);
713	fs = ump->um_e2fs;
714	ronly = fs->e2fs_ronly;
715	if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
716		if (fs->e2fs_wasvalid)
717 			fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
718 		ext2_sbupdate(ump, MNT_WAIT);
719	}
720
721	DROP_GIANT();
722	g_topology_lock();
723	g_vfs_close(ump->um_cp);
724	g_topology_unlock();
725	PICKUP_GIANT();
726	vrele(ump->um_devvp);
727	sump = fs->e2fs_clustersum;
728	for (i = 0; i < fs->e2fs_gcount; i++, sump++)
729		free(sump->cs_sum, M_EXT2MNT);
730	free(fs->e2fs_clustersum, M_EXT2MNT);
731	free(fs->e2fs_maxcluster, M_EXT2MNT);
732	free(fs->e2fs_gd, M_EXT2MNT);
733	free(fs->e2fs_contigdirs, M_EXT2MNT);
734	free(fs->e2fs, M_EXT2MNT);
735	free(fs, M_EXT2MNT);
736	free(ump, M_EXT2MNT);
737	mp->mnt_data = NULL;
738	MNT_ILOCK(mp);
739	mp->mnt_flag &= ~MNT_LOCAL;
740	MNT_IUNLOCK(mp);
741	return (error);
742}
743
744/*
745 * Flush out all the files in a filesystem.
746 */
747static int
748ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
749{
750	int error;
751
752	error = vflush(mp, 0, flags, td);
753	return (error);
754}
755/*
756 * Get file system statistics.
757 */
758int
759ext2_statfs(struct mount *mp, struct statfs *sbp)
760{
761	struct ext2mount *ump;
762	struct m_ext2fs *fs;
763	uint32_t overhead, overhead_per_group, ngdb;
764	int i, ngroups;
765
766	ump = VFSTOEXT2(mp);
767	fs = ump->um_e2fs;
768	if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
769		panic("ext2fs_statfs");
770
771	/*
772	 * Compute the overhead (FS structures)
773	 */
774	overhead_per_group =
775	    1 /* block bitmap */ +
776	    1 /* inode bitmap */ +
777	    fs->e2fs_itpg;
778	overhead = fs->e2fs->e2fs_first_dblock +
779	    fs->e2fs_gcount * overhead_per_group;
780	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
781	    fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
782		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
783			if (cg_has_sb(i))
784				ngroups++;
785		}
786	} else {
787		ngroups = fs->e2fs_gcount;
788	}
789	ngdb = fs->e2fs_gdbcount;
790	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
791	    fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
792		ngdb += fs->e2fs->e2fs_reserved_ngdb;
793	overhead += ngroups * (1 /* superblock */ + ngdb);
794
795	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
796	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
797	sbp->f_blocks = fs->e2fs->e2fs_bcount - overhead;
798	sbp->f_bfree = fs->e2fs->e2fs_fbcount;
799	sbp->f_bavail = sbp->f_bfree - fs->e2fs->e2fs_rbcount;
800	sbp->f_files = fs->e2fs->e2fs_icount;
801	sbp->f_ffree = fs->e2fs->e2fs_ficount;
802	return (0);
803}
804
805/*
806 * Go through the disk queues to initiate sandbagged IO;
807 * go through the inodes to write those that have been modified;
808 * initiate the writing of the super block if it has been modified.
809 *
810 * Note: we are always called with the filesystem marked `MPBUSY'.
811 */
812static int
813ext2_sync(struct mount *mp, int waitfor)
814{
815	struct vnode *mvp, *vp;
816	struct thread *td;
817	struct inode *ip;
818	struct ext2mount *ump = VFSTOEXT2(mp);
819	struct m_ext2fs *fs;
820	int error, allerror = 0;
821
822	td = curthread;
823	fs = ump->um_e2fs;
824	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {		/* XXX */
825		printf("fs = %s\n", fs->e2fs_fsmnt);
826		panic("ext2_sync: rofs mod");
827	}
828
829	/*
830	 * Write back each (modified) inode.
831	 */
832loop:
833	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
834		if (vp->v_type == VNON) {
835			VI_UNLOCK(vp);
836			continue;
837		}
838		ip = VTOI(vp);
839		if ((ip->i_flag &
840		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
841		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
842		    waitfor == MNT_LAZY)) {
843			VI_UNLOCK(vp);
844			continue;
845		}
846		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
847		if (error) {
848			if (error == ENOENT) {
849				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
850				goto loop;
851			}
852			continue;
853		}
854		if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
855			allerror = error;
856		VOP_UNLOCK(vp, 0);
857		vrele(vp);
858	}
859
860	/*
861	 * Force stale file system control information to be flushed.
862	 */
863	if (waitfor != MNT_LAZY) {
864		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
865		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
866			allerror = error;
867		VOP_UNLOCK(ump->um_devvp, 0);
868	}
869
870	/*
871	 * Write back modified superblock.
872	 */
873	if (fs->e2fs_fmod != 0) {
874		fs->e2fs_fmod = 0;
875		fs->e2fs->e2fs_wtime = time_second;
876		if ((error = ext2_cgupdate(ump, waitfor)) != 0)
877			allerror = error;
878	}
879	return (allerror);
880}
881
882/*
883 * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
884 * in from disk.  If it is in core, wait for the lock bit to clear, then
885 * return the inode locked.  Detection and handling of mount points must be
886 * done by the calling routine.
887 */
888static int
889ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
890{
891	struct m_ext2fs *fs;
892	struct inode *ip;
893	struct ext2mount *ump;
894	struct buf *bp;
895	struct vnode *vp;
896	struct cdev *dev;
897	struct thread *td;
898	int i, error;
899	int used_blocks;
900
901	td = curthread;
902	error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
903	if (error || *vpp != NULL)
904		return (error);
905
906	ump = VFSTOEXT2(mp);
907	dev = ump->um_dev;
908	ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
909
910	/* Allocate a new vnode/inode. */
911	if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
912		*vpp = NULL;
913		free(ip, M_EXT2NODE);
914		return (error);
915	}
916	vp->v_data = ip;
917	ip->i_vnode = vp;
918	ip->i_e2fs = fs = ump->um_e2fs;
919	ip->i_ump  = ump;
920	ip->i_number = ino;
921
922	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
923	error = insmntque(vp, mp);
924	if (error != 0) {
925		free(ip, M_EXT2NODE);
926		*vpp = NULL;
927		return (error);
928	}
929	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
930	if (error || *vpp != NULL)
931		return (error);
932
933	/* Read in the disk contents for the inode, copy into the inode. */
934	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
935	    (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
936		/*
937		 * The inode does not contain anything useful, so it would
938		 * be misleading to leave it on its hash chain. With mode
939		 * still zero, it will be unlinked and returned to the free
940		 * list by vput().
941		 */
942		brelse(bp);
943		vput(vp);
944		*vpp = NULL;
945		return (error);
946	}
947	/* convert ext2 inode to dinode */
948	ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
949			ino_to_fsbo(fs, ino)), ip);
950	ip->i_block_group = ino_to_cg(fs, ino);
951	ip->i_next_alloc_block = 0;
952	ip->i_next_alloc_goal = 0;
953
954	/*
955	 * Now we want to make sure that block pointers for unused
956	 * blocks are zeroed out - ext2_balloc depends on this
957	 * although for regular files and directories only
958	 */
959	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
960		used_blocks = (ip->i_size+fs->e2fs_bsize-1) / fs->e2fs_bsize;
961		for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
962			ip->i_db[i] = 0;
963	}
964/*
965	ext2_print_inode(ip);
966*/
967	bqrelse(bp);
968
969	/*
970	 * Initialize the vnode from the inode, check for aliases.
971	 * Note that the underlying vnode may have changed.
972	 */
973	if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
974		vput(vp);
975		*vpp = NULL;
976		return (error);
977	}
978
979	/*
980	 * Finish inode initialization.
981	 */
982
983	/*
984	 * Set up a generation number for this inode if it does not
985	 * already have one. This should only happen on old filesystems.
986	 */
987	if (ip->i_gen == 0) {
988		ip->i_gen = random() / 2 + 1;
989		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
990			ip->i_flag |= IN_MODIFIED;
991	}
992	*vpp = vp;
993	return (0);
994}
995
996/*
997 * File handle to vnode
998 *
999 * Have to be really careful about stale file handles:
1000 * - check that the inode number is valid
1001 * - call ext2_vget() to get the locked inode
1002 * - check for an unallocated inode (i_mode == 0)
1003 * - check that the given client host has export rights and return
1004 *   those rights via. exflagsp and credanonp
1005 */
1006static int
1007ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1008{
1009	struct inode *ip;
1010	struct ufid *ufhp;
1011	struct vnode *nvp;
1012	struct m_ext2fs *fs;
1013	int error;
1014
1015	ufhp = (struct ufid *)fhp;
1016	fs = VFSTOEXT2(mp)->um_e2fs;
1017	if (ufhp->ufid_ino < EXT2_ROOTINO ||
1018	    ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1019		return (ESTALE);
1020
1021	error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1022	if (error) {
1023		*vpp = NULLVP;
1024		return (error);
1025	}
1026	ip = VTOI(nvp);
1027	if (ip->i_mode == 0 ||
1028	    ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1029		vput(nvp);
1030		*vpp = NULLVP;
1031		return (ESTALE);
1032	}
1033	*vpp = nvp;
1034	vnode_create_vobject(*vpp, 0, curthread);
1035	return (0);
1036}
1037
1038/*
1039 * Write a superblock and associated information back to disk.
1040 */
1041static int
1042ext2_sbupdate(struct ext2mount *mp, int waitfor)
1043{
1044	struct m_ext2fs *fs = mp->um_e2fs;
1045	struct ext2fs *es = fs->e2fs;
1046	struct buf *bp;
1047	int error = 0;
1048
1049	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1050	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1051	if (waitfor == MNT_WAIT)
1052		error = bwrite(bp);
1053	else
1054		bawrite(bp);
1055
1056	/*
1057	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1058	 * are not busy at this point and are (hopefully) written by the
1059	 * usual sync mechanism. No need to write them here.
1060	 */
1061	return (error);
1062}
1063int
1064ext2_cgupdate(struct ext2mount *mp, int waitfor)
1065{
1066	struct m_ext2fs *fs = mp->um_e2fs;
1067	struct buf *bp;
1068	int i, error = 0, allerror = 0;
1069
1070	allerror = ext2_sbupdate(mp, waitfor);
1071	for (i = 0; i < fs->e2fs_gdbcount; i++) {
1072		bp = getblk(mp->um_devvp, fsbtodb(fs,
1073		    fs->e2fs->e2fs_first_dblock +
1074		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0, 0);
1075		e2fs_cgsave(&fs->e2fs_gd[
1076		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1077		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1078		if (waitfor == MNT_WAIT)
1079			error = bwrite(bp);
1080		else
1081			bawrite(bp);
1082	}
1083
1084	if (!allerror && error)
1085		allerror = error;
1086	return (allerror);
1087}
1088/*
1089 * Return the root of a filesystem.
1090 */
1091static int
1092ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1093{
1094	struct vnode *nvp;
1095	int error;
1096
1097	error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);
1098	if (error)
1099		return (error);
1100	*vpp = nvp;
1101	return (0);
1102}
1103