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