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