xref: /freebsd/sys/fs/ext2fs/ext2_vfsops.c (revision 9a0c3479e22feda1bdb2db4b97f9deb1b5fa6269)
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/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 
67 static int	ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
68 static int	ext2_mountfs(struct vnode *, struct mount *);
69 static int	ext2_reload(struct mount *mp, struct thread *td);
70 static int	ext2_sbupdate(struct ext2mount *, int);
71 static int	ext2_cgupdate(struct ext2mount *, int);
72 static vfs_unmount_t		ext2_unmount;
73 static vfs_root_t		ext2_root;
74 static vfs_statfs_t		ext2_statfs;
75 static vfs_sync_t		ext2_sync;
76 static vfs_vget_t		ext2_vget;
77 static vfs_fhtovp_t		ext2_fhtovp;
78 static vfs_mount_t		ext2_mount;
79 
80 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
81 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
82 
83 static 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 
93 VFS_SET(ext2fs_vfsops, ext2fs, 0);
94 
95 static int	ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
96 		    int ronly);
97 static int	compute_sb_data(struct vnode * devvp,
98 		    struct ext2fs * es, struct m_ext2fs * fs);
99 
100 static 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  */
109 static int
110 ext2_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)
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 
283 static int
284 ext2_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  */
313 static int
314 compute_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 	uint32_t e2fs_descpb;
322 
323 	fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
324 	fs->e2fs_bsize = 1U << fs->e2fs_bshift;
325 	fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
326 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
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_isize = E2FS_REV0_INODE_SIZE ;
335 	} else {
336 		fs->e2fs_isize = es->e2fs_inode_size;
337 
338 		/*
339 		 * Simple sanity check for superblock inode size value.
340 		 */
341 		if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
342 		    EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
343 		    (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
344 			printf("ext2fs: invalid inode size %d\n",
345 			    fs->e2fs_isize);
346 			return (EIO);
347 		}
348 	}
349 	/* Check for extra isize in big inodes. */
350 	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
351 	    EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
352 		printf("ext2fs: no space for extra inode timestamps\n");
353 		return (EINVAL);
354 	}
355 
356 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
357 	fs->e2fs_itpg = fs->e2fs_ipg /fs->e2fs_ipb;
358 	/* s_resuid / s_resgid ? */
359 	fs->e2fs_gcount = (es->e2fs_bcount - es->e2fs_first_dblock +
360 	    EXT2_BLOCKS_PER_GROUP(fs) - 1) / EXT2_BLOCKS_PER_GROUP(fs);
361 	e2fs_descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
362 	db_count = (fs->e2fs_gcount + e2fs_descpb - 1) / e2fs_descpb;
363 	fs->e2fs_gdbcount = db_count;
364 	fs->e2fs_gd = malloc(db_count * fs->e2fs_bsize,
365 	    M_EXT2MNT, M_WAITOK);
366 	fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
367 	    sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK);
368 
369 	/*
370 	 * Adjust logic_sb_block.
371 	 * Godmar thinks: if the blocksize is greater than 1024, then
372 	 * the superblock is logically part of block zero.
373 	 */
374 	if(fs->e2fs_bsize > SBSIZE)
375 		logic_sb_block = 0;
376 	for (i = 0; i < db_count; i++) {
377 		error = bread(devvp ,
378 			 fsbtodb(fs, logic_sb_block + i + 1 ),
379 			fs->e2fs_bsize, NOCRED, &bp);
380 		if (error) {
381 			free(fs->e2fs_gd, M_EXT2MNT);
382 			brelse(bp);
383 			return (error);
384 		}
385 		e2fs_cgload((struct ext2_gd *)bp->b_data,
386 		    &fs->e2fs_gd[
387 			i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
388 		    fs->e2fs_bsize);
389 		brelse(bp);
390 		bp = NULL;
391 	}
392 	fs->e2fs_total_dir = 0;
393 	for (i=0; i < fs->e2fs_gcount; i++){
394 		fs->e2fs_total_dir += fs->e2fs_gd[i].ext2bgd_ndirs;
395 		fs->e2fs_contigdirs[i] = 0;
396 	}
397 	if (es->e2fs_rev == E2FS_REV0 ||
398 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
399 		fs->e2fs_maxfilesize = 0x7fffffff;
400 	else
401 		fs->e2fs_maxfilesize = 0x7fffffffffffffff;
402 
403 	if (es->e4fs_flags & E2FS_UNSIGNED_HASH) {
404 		fs->e2fs_uhash = 3;
405 	} else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) {
406 #ifdef __CHAR_UNSIGNED__
407 		es->e4fs_flags |= E2FS_UNSIGNED_HASH;
408 		fs->e2fs_uhash = 3;
409 #else
410 		es->e4fs_flags |= E2FS_SIGNED_HASH;
411 #endif
412 	}
413 
414 	return (0);
415 }
416 
417 /*
418  * Reload all incore data for a filesystem (used after running fsck on
419  * the root filesystem and finding things to fix). The filesystem must
420  * be mounted read-only.
421  *
422  * Things to do to update the mount:
423  *	1) invalidate all cached meta-data.
424  *	2) re-read superblock from disk.
425  *	3) invalidate all cluster summary information.
426  *	4) invalidate all inactive vnodes.
427  *	5) invalidate all cached file data.
428  *	6) re-read inode data for all active vnodes.
429  * XXX we are missing some steps, in particular # 3, this has to be reviewed.
430  */
431 static int
432 ext2_reload(struct mount *mp, struct thread *td)
433 {
434 	struct vnode *vp, *mvp, *devvp;
435 	struct inode *ip;
436 	struct buf *bp;
437 	struct ext2fs *es;
438 	struct m_ext2fs *fs;
439 	struct csum *sump;
440 	int error, i;
441 	int32_t *lp;
442 
443 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
444 		return (EINVAL);
445 	/*
446 	 * Step 1: invalidate all cached meta-data.
447 	 */
448 	devvp = VFSTOEXT2(mp)->um_devvp;
449 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
450 	if (vinvalbuf(devvp, 0, 0, 0) != 0)
451 		panic("ext2_reload: dirty1");
452 	VOP_UNLOCK(devvp, 0);
453 
454 	/*
455 	 * Step 2: re-read superblock from disk.
456 	 * constants have been adjusted for ext2
457 	 */
458 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
459 		return (error);
460 	es = (struct ext2fs *)bp->b_data;
461 	if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
462 		brelse(bp);
463 		return (EIO);		/* XXX needs translation */
464 	}
465 	fs = VFSTOEXT2(mp)->um_e2fs;
466 	bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
467 
468 	if((error = compute_sb_data(devvp, es, fs)) != 0) {
469 		brelse(bp);
470 		return (error);
471 	}
472 #ifdef UNKLAR
473 	if (fs->fs_sbsize < SBSIZE)
474 		bp->b_flags |= B_INVAL;
475 #endif
476 	brelse(bp);
477 
478 	/*
479 	 * Step 3: invalidate all cluster summary information.
480 	 */
481 	if (fs->e2fs_contigsumsize > 0) {
482 		lp = fs->e2fs_maxcluster;
483 		sump = fs->e2fs_clustersum;
484 		for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
485 			*lp++ = fs->e2fs_contigsumsize;
486 			sump->cs_init = 0;
487 			bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
488 		}
489 	}
490 
491 loop:
492 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
493 		/*
494 		 * Step 4: invalidate all cached file data.
495 		 */
496 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
497 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
498 			goto loop;
499 		}
500 		if (vinvalbuf(vp, 0, 0, 0))
501 			panic("ext2_reload: dirty2");
502 
503 		/*
504 		 * Step 5: re-read inode data for all active vnodes.
505 		 */
506 		ip = VTOI(vp);
507 		error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
508 		    (int)fs->e2fs_bsize, NOCRED, &bp);
509 		if (error) {
510 			VOP_UNLOCK(vp, 0);
511 			vrele(vp);
512 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
513 			return (error);
514 		}
515 		ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data +
516 		    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
517 		brelse(bp);
518 		VOP_UNLOCK(vp, 0);
519 		vrele(vp);
520 	}
521 	return (0);
522 }
523 
524 /*
525  * Common code for mount and mountroot.
526  */
527 static int
528 ext2_mountfs(struct vnode *devvp, struct mount *mp)
529 {
530 	struct ext2mount *ump;
531 	struct buf *bp;
532 	struct m_ext2fs *fs;
533 	struct ext2fs *es;
534 	struct cdev *dev = devvp->v_rdev;
535 	struct g_consumer *cp;
536 	struct bufobj *bo;
537 	struct csum *sump;
538 	int error;
539 	int ronly;
540 	int i, size;
541 	int32_t *lp;
542 	int32_t e2fs_maxcontig;
543 
544 	ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
545 	/* XXX: use VOP_ACESS to check FS perms */
546 	DROP_GIANT();
547 	g_topology_lock();
548 	error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
549 	g_topology_unlock();
550 	PICKUP_GIANT();
551 	VOP_UNLOCK(devvp, 0);
552 	if (error)
553 		return (error);
554 
555 	/* XXX: should we check for some sectorsize or 512 instead? */
556 	if (((SBSIZE % cp->provider->sectorsize) != 0) ||
557 	    (SBSIZE < cp->provider->sectorsize)) {
558 		DROP_GIANT();
559 		g_topology_lock();
560 		g_vfs_close(cp);
561 		g_topology_unlock();
562 		PICKUP_GIANT();
563 		return (EINVAL);
564 	}
565 
566 	bo = &devvp->v_bufobj;
567 	bo->bo_private = cp;
568 	bo->bo_ops = g_vfs_bufops;
569 	if (devvp->v_rdev->si_iosize_max != 0)
570 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
571 	if (mp->mnt_iosize_max > MAXPHYS)
572 		mp->mnt_iosize_max = MAXPHYS;
573 
574 	bp = NULL;
575 	ump = NULL;
576 	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
577 		goto out;
578 	es = (struct ext2fs *)bp->b_data;
579 	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
580 		error = EINVAL;		/* XXX needs translation */
581 		goto out;
582 	}
583 	if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
584 	    (es->e2fs_state & E2FS_ERRORS)) {
585 		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
586 			printf(
587 "WARNING: Filesystem was not properly dismounted\n");
588 		} else {
589 			printf(
590 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
591 			error = EPERM;
592 			goto out;
593 		}
594 	}
595 	ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
596 
597 	/*
598 	 * I don't know whether this is the right strategy. Note that
599 	 * we dynamically allocate both an ext2_sb_info and an ext2_super_block
600 	 * while Linux keeps the super block in a locked buffer.
601 	 */
602 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
603 		M_EXT2MNT, M_WAITOK);
604 	ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
605 		M_EXT2MNT, M_WAITOK);
606 	mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
607 	bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
608 	if ((error = compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
609 		goto out;
610 
611 	/*
612 	 * Calculate the maximum contiguous blocks and size of cluster summary
613 	 * array.  In FFS this is done by newfs; however, the superblock
614 	 * in ext2fs doesn't have these variables, so we can calculate
615 	 * them here.
616 	 */
617 	e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
618 	ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
619 	if (ump->um_e2fs->e2fs_contigsumsize > 0) {
620 		size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
621 		ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
622 		size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
623 		ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
624 		lp = ump->um_e2fs->e2fs_maxcluster;
625 		sump = ump->um_e2fs->e2fs_clustersum;
626 		for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
627 			*lp++ = ump->um_e2fs->e2fs_contigsumsize;
628 			sump->cs_init = 0;
629 			sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
630 			    sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
631 		}
632 	}
633 
634 	brelse(bp);
635 	bp = NULL;
636 	fs = ump->um_e2fs;
637 	fs->e2fs_ronly = ronly;	/* ronly is set according to mnt_flags */
638 
639 	/*
640 	 * If the fs is not mounted read-only, make sure the super block is
641 	 * always written back on a sync().
642 	 */
643 	fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
644 	if (ronly == 0) {
645 		fs->e2fs_fmod = 1;		/* mark it modified */
646 		fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;	/* set fs invalid */
647 	}
648 	mp->mnt_data = ump;
649 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
650 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
651 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
652 	MNT_ILOCK(mp);
653 	mp->mnt_flag |= MNT_LOCAL;
654 	MNT_IUNLOCK(mp);
655 	ump->um_mountp = mp;
656 	ump->um_dev = dev;
657 	ump->um_devvp = devvp;
658 	ump->um_bo = &devvp->v_bufobj;
659 	ump->um_cp = cp;
660 
661 	/*
662 	 * Setting those two parameters allowed us to use
663 	 * ufs_bmap w/o changse!
664 	 */
665 	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
666 	ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
667 	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
668 	if (ronly == 0)
669 		ext2_sbupdate(ump, MNT_WAIT);
670 	/*
671 	 * Initialize filesystem stat information in mount struct.
672 	 */
673 	MNT_ILOCK(mp);
674  	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
675 	MNT_IUNLOCK(mp);
676 	return (0);
677 out:
678 	if (bp)
679 		brelse(bp);
680 	if (cp != NULL) {
681 		DROP_GIANT();
682 		g_topology_lock();
683 		g_vfs_close(cp);
684 		g_topology_unlock();
685 		PICKUP_GIANT();
686 	}
687 	if (ump) {
688 	  	mtx_destroy(EXT2_MTX(ump));
689 		free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
690 		free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
691 		free(ump->um_e2fs->e2fs, M_EXT2MNT);
692 		free(ump->um_e2fs, M_EXT2MNT);
693 		free(ump, M_EXT2MNT);
694 		mp->mnt_data = NULL;
695 	}
696 	return (error);
697 }
698 
699 /*
700  * Unmount system call.
701  */
702 static int
703 ext2_unmount(struct mount *mp, int mntflags)
704 {
705 	struct ext2mount *ump;
706 	struct m_ext2fs *fs;
707 	struct csum *sump;
708 	int error, flags, i, ronly;
709 
710 	flags = 0;
711 	if (mntflags & MNT_FORCE) {
712 		if (mp->mnt_flag & MNT_ROOTFS)
713 			return (EINVAL);
714 		flags |= FORCECLOSE;
715 	}
716 	if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
717 		return (error);
718 	ump = VFSTOEXT2(mp);
719 	fs = ump->um_e2fs;
720 	ronly = fs->e2fs_ronly;
721 	if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
722 		if (fs->e2fs_wasvalid)
723  			fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
724  		ext2_sbupdate(ump, MNT_WAIT);
725 	}
726 
727 	DROP_GIANT();
728 	g_topology_lock();
729 	g_vfs_close(ump->um_cp);
730 	g_topology_unlock();
731 	PICKUP_GIANT();
732 	vrele(ump->um_devvp);
733 	sump = fs->e2fs_clustersum;
734 	for (i = 0; i < fs->e2fs_gcount; i++, sump++)
735 		free(sump->cs_sum, M_EXT2MNT);
736 	free(fs->e2fs_clustersum, M_EXT2MNT);
737 	free(fs->e2fs_maxcluster, M_EXT2MNT);
738 	free(fs->e2fs_gd, M_EXT2MNT);
739 	free(fs->e2fs_contigdirs, M_EXT2MNT);
740 	free(fs->e2fs, M_EXT2MNT);
741 	free(fs, M_EXT2MNT);
742 	free(ump, M_EXT2MNT);
743 	mp->mnt_data = NULL;
744 	MNT_ILOCK(mp);
745 	mp->mnt_flag &= ~MNT_LOCAL;
746 	MNT_IUNLOCK(mp);
747 	return (error);
748 }
749 
750 /*
751  * Flush out all the files in a filesystem.
752  */
753 static int
754 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
755 {
756 	int error;
757 
758 	error = vflush(mp, 0, flags, td);
759 	return (error);
760 }
761 /*
762  * Get filesystem statistics.
763  */
764 int
765 ext2_statfs(struct mount *mp, struct statfs *sbp)
766 {
767 	struct ext2mount *ump;
768 	struct m_ext2fs *fs;
769 	uint32_t overhead, overhead_per_group, ngdb;
770 	int i, ngroups;
771 
772 	ump = VFSTOEXT2(mp);
773 	fs = ump->um_e2fs;
774 	if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
775 		panic("ext2_statfs");
776 
777 	/*
778 	 * Compute the overhead (FS structures)
779 	 */
780 	overhead_per_group =
781 	    1 /* block bitmap */ +
782 	    1 /* inode bitmap */ +
783 	    fs->e2fs_itpg;
784 	overhead = fs->e2fs->e2fs_first_dblock +
785 	    fs->e2fs_gcount * overhead_per_group;
786 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
787 	    fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
788 		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
789 			if (cg_has_sb(i))
790 				ngroups++;
791 		}
792 	} else {
793 		ngroups = fs->e2fs_gcount;
794 	}
795 	ngdb = fs->e2fs_gdbcount;
796 	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
797 	    fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
798 		ngdb += fs->e2fs->e2fs_reserved_ngdb;
799 	overhead += ngroups * (1 /* superblock */ + ngdb);
800 
801 	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
802 	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
803 	sbp->f_blocks = fs->e2fs->e2fs_bcount - overhead;
804 	sbp->f_bfree = fs->e2fs->e2fs_fbcount;
805 	sbp->f_bavail = sbp->f_bfree - fs->e2fs->e2fs_rbcount;
806 	sbp->f_files = fs->e2fs->e2fs_icount;
807 	sbp->f_ffree = fs->e2fs->e2fs_ficount;
808 	return (0);
809 }
810 
811 /*
812  * Go through the disk queues to initiate sandbagged IO;
813  * go through the inodes to write those that have been modified;
814  * initiate the writing of the super block if it has been modified.
815  *
816  * Note: we are always called with the filesystem marked `MPBUSY'.
817  */
818 static int
819 ext2_sync(struct mount *mp, int waitfor)
820 {
821 	struct vnode *mvp, *vp;
822 	struct thread *td;
823 	struct inode *ip;
824 	struct ext2mount *ump = VFSTOEXT2(mp);
825 	struct m_ext2fs *fs;
826 	int error, allerror = 0;
827 
828 	td = curthread;
829 	fs = ump->um_e2fs;
830 	if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {		/* XXX */
831 		printf("fs = %s\n", fs->e2fs_fsmnt);
832 		panic("ext2_sync: rofs mod");
833 	}
834 
835 	/*
836 	 * Write back each (modified) inode.
837 	 */
838 loop:
839 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
840 		if (vp->v_type == VNON) {
841 			VI_UNLOCK(vp);
842 			continue;
843 		}
844 		ip = VTOI(vp);
845 		if ((ip->i_flag &
846 		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
847 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
848 		    waitfor == MNT_LAZY)) {
849 			VI_UNLOCK(vp);
850 			continue;
851 		}
852 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
853 		if (error) {
854 			if (error == ENOENT) {
855 				MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
856 				goto loop;
857 			}
858 			continue;
859 		}
860 		if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
861 			allerror = error;
862 		VOP_UNLOCK(vp, 0);
863 		vrele(vp);
864 	}
865 
866 	/*
867 	 * Force stale filesystem control information to be flushed.
868 	 */
869 	if (waitfor != MNT_LAZY) {
870 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
871 		if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
872 			allerror = error;
873 		VOP_UNLOCK(ump->um_devvp, 0);
874 	}
875 
876 	/*
877 	 * Write back modified superblock.
878 	 */
879 	if (fs->e2fs_fmod != 0) {
880 		fs->e2fs_fmod = 0;
881 		fs->e2fs->e2fs_wtime = time_second;
882 		if ((error = ext2_cgupdate(ump, waitfor)) != 0)
883 			allerror = error;
884 	}
885 	return (allerror);
886 }
887 
888 /*
889  * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
890  * in from disk.  If it is in core, wait for the lock bit to clear, then
891  * return the inode locked.  Detection and handling of mount points must be
892  * done by the calling routine.
893  */
894 static int
895 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
896 {
897 	struct m_ext2fs *fs;
898 	struct inode *ip;
899 	struct ext2mount *ump;
900 	struct buf *bp;
901 	struct vnode *vp;
902 	struct cdev *dev;
903 	struct thread *td;
904 	int i, error;
905 	int used_blocks;
906 
907 	td = curthread;
908 	error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
909 	if (error || *vpp != NULL)
910 		return (error);
911 
912 	ump = VFSTOEXT2(mp);
913 	dev = ump->um_dev;
914 	ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
915 
916 	/* Allocate a new vnode/inode. */
917 	if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
918 		*vpp = NULL;
919 		free(ip, M_EXT2NODE);
920 		return (error);
921 	}
922 	vp->v_data = ip;
923 	ip->i_vnode = vp;
924 	ip->i_e2fs = fs = ump->um_e2fs;
925 	ip->i_ump  = ump;
926 	ip->i_number = ino;
927 
928 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
929 	error = insmntque(vp, mp);
930 	if (error != 0) {
931 		free(ip, M_EXT2NODE);
932 		*vpp = NULL;
933 		return (error);
934 	}
935 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
936 	if (error || *vpp != NULL)
937 		return (error);
938 
939 	/* Read in the disk contents for the inode, copy into the inode. */
940 	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
941 	    (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
942 		/*
943 		 * The inode does not contain anything useful, so it would
944 		 * be misleading to leave it on its hash chain. With mode
945 		 * still zero, it will be unlinked and returned to the free
946 		 * list by vput().
947 		 */
948 		brelse(bp);
949 		vput(vp);
950 		*vpp = NULL;
951 		return (error);
952 	}
953 	/* convert ext2 inode to dinode */
954 	ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
955 			ino_to_fsbo(fs, ino)), ip);
956 	ip->i_block_group = ino_to_cg(fs, ino);
957 	ip->i_next_alloc_block = 0;
958 	ip->i_next_alloc_goal = 0;
959 
960 	/*
961 	 * Now we want to make sure that block pointers for unused
962 	 * blocks are zeroed out - ext2_balloc depends on this
963 	 * although for regular files and directories only
964 	 */
965 	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
966 		used_blocks = (ip->i_size+fs->e2fs_bsize-1) / fs->e2fs_bsize;
967 		for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
968 			ip->i_db[i] = 0;
969 	}
970 /*
971 	ext2_print_inode(ip);
972 */
973 	bqrelse(bp);
974 
975 	/*
976 	 * Initialize the vnode from the inode, check for aliases.
977 	 * Note that the underlying vnode may have changed.
978 	 */
979 	if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
980 		vput(vp);
981 		*vpp = NULL;
982 		return (error);
983 	}
984 
985 	/*
986 	 * Finish inode initialization.
987 	 */
988 
989 	/*
990 	 * Set up a generation number for this inode if it does not
991 	 * already have one. This should only happen on old filesystems.
992 	 */
993 	if (ip->i_gen == 0) {
994 		ip->i_gen = random() + 1;
995 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
996 			ip->i_flag |= IN_MODIFIED;
997 	}
998 	*vpp = vp;
999 	return (0);
1000 }
1001 
1002 /*
1003  * File handle to vnode
1004  *
1005  * Have to be really careful about stale file handles:
1006  * - check that the inode number is valid
1007  * - call ext2_vget() to get the locked inode
1008  * - check for an unallocated inode (i_mode == 0)
1009  * - check that the given client host has export rights and return
1010  *   those rights via. exflagsp and credanonp
1011  */
1012 static int
1013 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1014 {
1015 	struct inode *ip;
1016 	struct ufid *ufhp;
1017 	struct vnode *nvp;
1018 	struct m_ext2fs *fs;
1019 	int error;
1020 
1021 	ufhp = (struct ufid *)fhp;
1022 	fs = VFSTOEXT2(mp)->um_e2fs;
1023 	if (ufhp->ufid_ino < EXT2_ROOTINO ||
1024 	    ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1025 		return (ESTALE);
1026 
1027 	error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1028 	if (error) {
1029 		*vpp = NULLVP;
1030 		return (error);
1031 	}
1032 	ip = VTOI(nvp);
1033 	if (ip->i_mode == 0 ||
1034 	    ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1035 		vput(nvp);
1036 		*vpp = NULLVP;
1037 		return (ESTALE);
1038 	}
1039 	*vpp = nvp;
1040 	vnode_create_vobject(*vpp, 0, curthread);
1041 	return (0);
1042 }
1043 
1044 /*
1045  * Write a superblock and associated information back to disk.
1046  */
1047 static int
1048 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1049 {
1050 	struct m_ext2fs *fs = mp->um_e2fs;
1051 	struct ext2fs *es = fs->e2fs;
1052 	struct buf *bp;
1053 	int error = 0;
1054 
1055 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1056 	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1057 	if (waitfor == MNT_WAIT)
1058 		error = bwrite(bp);
1059 	else
1060 		bawrite(bp);
1061 
1062 	/*
1063 	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1064 	 * are not busy at this point and are (hopefully) written by the
1065 	 * usual sync mechanism. No need to write them here.
1066 	 */
1067 	return (error);
1068 }
1069 int
1070 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1071 {
1072 	struct m_ext2fs *fs = mp->um_e2fs;
1073 	struct buf *bp;
1074 	int i, error = 0, allerror = 0;
1075 
1076 	allerror = ext2_sbupdate(mp, waitfor);
1077 	for (i = 0; i < fs->e2fs_gdbcount; i++) {
1078 		bp = getblk(mp->um_devvp, fsbtodb(fs,
1079 		    fs->e2fs->e2fs_first_dblock +
1080 		    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0, 0);
1081 		e2fs_cgsave(&fs->e2fs_gd[
1082 		    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1083 		    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1084 		if (waitfor == MNT_WAIT)
1085 			error = bwrite(bp);
1086 		else
1087 			bawrite(bp);
1088 	}
1089 
1090 	if (!allerror && error)
1091 		allerror = error;
1092 	return (allerror);
1093 }
1094 /*
1095  * Return the root of a filesystem.
1096  */
1097 static int
1098 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1099 {
1100 	struct vnode *nvp;
1101 	int error;
1102 
1103 	error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);
1104 	if (error)
1105 		return (error);
1106 	*vpp = nvp;
1107 	return (0);
1108 }
1109