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