2 * modified for EXT2FS support in Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * SPDX-License-Identifier: BSD-3-Clause
10 * Copyright (c) 1989, 1991, 1993, 1994
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
52 #include <sys/endian.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
56 #include <sys/mutex.h>
58 #include <geom/geom.h>
59 #include <geom/geom_vfs.h>
61 #include <fs/ext2fs/ext2_mount.h>
62 #include <fs/ext2fs/inode.h>
64 #include <fs/ext2fs/fs.h>
65 #include <fs/ext2fs/ext2fs.h>
66 #include <fs/ext2fs/ext2_dinode.h>
67 #include <fs/ext2fs/ext2_extern.h>
69 static int ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
70 static int ext2_mountfs(struct vnode *, struct mount *);
71 static int ext2_reload(struct mount *mp, struct thread *td);
72 static int ext2_sbupdate(struct ext2mount *, int);
73 static int ext2_cgupdate(struct ext2mount *, int);
74 static vfs_unmount_t ext2_unmount;
75 static vfs_root_t ext2_root;
76 static vfs_statfs_t ext2_statfs;
77 static vfs_sync_t ext2_sync;
78 static vfs_vget_t ext2_vget;
79 static vfs_fhtovp_t ext2_fhtovp;
80 static vfs_mount_t ext2_mount;
82 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
83 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
85 static struct vfsops ext2fs_vfsops = {
86 .vfs_fhtovp = ext2_fhtovp,
87 .vfs_mount = ext2_mount,
88 .vfs_root = ext2_root, /* root inode via vget */
89 .vfs_statfs = ext2_statfs,
90 .vfs_sync = ext2_sync,
91 .vfs_unmount = ext2_unmount,
92 .vfs_vget = ext2_vget,
95 VFS_SET(ext2fs_vfsops, ext2fs, 0);
97 static int ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
99 static int compute_sb_data(struct vnode * devvp,
100 struct ext2fs * es, struct m_ext2fs * fs);
102 static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr",
103 "noclusterw", "noexec", "export", "force", "from", "multilabel",
104 "suiddir", "nosymfollow", "sync", "union", NULL };
112 ext2_mount(struct mount *mp)
114 struct vfsoptlist *opts;
117 struct ext2mount *ump = NULL;
119 struct nameidata nd, *ndp = &nd;
122 int error, flags, len;
125 opts = mp->mnt_optnew;
127 if (vfs_filteropt(opts, ext2_opts))
130 vfs_getopt(opts, "fspath", (void **)&path, NULL);
131 /* Double-check the length of path.. */
132 if (strlen(path) >= MAXMNTLEN)
133 return (ENAMETOOLONG);
136 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
137 if (!error && fspec[len - 1] != '\0')
141 * If updating, check whether changing from read-only to
142 * read/write; if there is no device name, that's all we do.
144 if (mp->mnt_flag & MNT_UPDATE) {
148 if (fs->e2fs_ronly == 0 &&
149 vfs_flagopt(opts, "ro", NULL, 0)) {
150 error = VFS_SYNC(mp, MNT_WAIT);
154 if (mp->mnt_flag & MNT_FORCE)
156 error = ext2_flushfiles(mp, flags, td);
157 if (error == 0 && fs->e2fs_wasvalid &&
158 ext2_cgupdate(ump, MNT_WAIT) == 0) {
159 fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
160 ext2_sbupdate(ump, MNT_WAIT);
163 vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
165 g_access(ump->um_cp, 0, -1, 0);
168 if (!error && (mp->mnt_flag & MNT_RELOAD))
169 error = ext2_reload(mp, td);
172 devvp = ump->um_devvp;
173 if (fs->e2fs_ronly && !vfs_flagopt(opts, "ro", NULL, 0)) {
174 if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
178 * If upgrade to read-write by non-root, then verify
179 * that user has necessary permissions on the device.
181 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
182 error = VOP_ACCESS(devvp, VREAD | VWRITE,
185 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
187 VOP_UNLOCK(devvp, 0);
190 VOP_UNLOCK(devvp, 0);
192 error = g_access(ump->um_cp, 0, 1, 0);
197 if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 ||
198 (fs->e2fs->e2fs_state & E2FS_ERRORS)) {
199 if (mp->mnt_flag & MNT_FORCE) {
201 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
204 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
209 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;
210 (void)ext2_cgupdate(ump, MNT_WAIT);
213 mp->mnt_flag &= ~MNT_RDONLY;
216 if (vfs_flagopt(opts, "export", NULL, 0)) {
217 /* Process export requests in vfs_mount.c. */
223 * Not an update, or updating the name: look up the name
224 * and verify that it refers to a sensible disk device.
228 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
229 if ((error = namei(ndp)) != 0)
231 NDFREE(ndp, NDF_ONLY_PNBUF);
234 if (!vn_isdisk(devvp, &error)) {
240 * If mount by non-root, then verify that user has necessary
241 * permissions on the device.
243 * XXXRW: VOP_ACCESS() enough?
246 if ((mp->mnt_flag & MNT_RDONLY) == 0)
248 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
250 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
256 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
257 error = ext2_mountfs(devvp, mp);
259 if (devvp != ump->um_devvp) {
261 return (EINVAL); /* needs translation */
273 * Note that this strncpy() is ok because of a check at the start
276 strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN);
277 fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0';
278 vfs_mountedfrom(mp, fspec);
283 ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
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);
292 if (es->e2fs_rev > E2FS_REV0) {
293 mask = es->e2fs_features_incompat & ~(EXT2F_INCOMPAT_SUPP);
295 printf("WARNING: mount of %s denied due to "
296 "unsupported optional features:\n", devtoname(dev));
298 i < sizeof(incompat)/sizeof(struct ext2_feature);
300 if (mask & incompat[i].mask)
301 printf("%s ", incompat[i].name);
305 mask = es->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
306 if (!ronly && mask) {
307 printf("WARNING: R/W mount of %s denied due to "
308 "unsupported optional features:\n", devtoname(dev));
310 i < sizeof(ro_compat)/sizeof(struct ext2_feature);
312 if (mask & ro_compat[i].mask)
313 printf("%s ", ro_compat[i].name);
322 cg_location(struct m_ext2fs *fs, int number)
324 int cg, descpb, logical_sb, has_super = 0;
327 * Adjust logical superblock block number.
328 * Godmar thinks: if the blocksize is greater than 1024, then
329 * the superblock is logically part of block zero.
331 logical_sb = fs->e2fs_bsize > SBSIZE ? 0 : 1;
333 if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
334 number < fs->e2fs->e3fs_first_meta_bg)
335 return (logical_sb + number + 1);
337 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT))
338 descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
340 descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
342 cg = descpb * number;
344 if (ext2_cg_has_sb(fs, cg))
347 return (has_super + cg * (e4fs_daddr_t)EXT2_BLOCKS_PER_GROUP(fs) +
348 fs->e2fs->e2fs_first_dblock);
352 * This computes the fields of the m_ext2fs structure from the
353 * data in the ext2fs structure read in.
356 compute_sb_data(struct vnode *devvp, struct ext2fs *es,
359 int g_count = 0, error;
362 uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
364 fs->e2fs_bcount = es->e2fs_bcount;
365 fs->e2fs_rbcount = es->e2fs_rbcount;
366 fs->e2fs_fbcount = es->e2fs_fbcount;
367 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
368 fs->e2fs_bcount |= (uint64_t)(es->e4fs_bcount_hi) << 32;
369 fs->e2fs_rbcount |= (uint64_t)(es->e4fs_rbcount_hi) << 32;
370 fs->e2fs_fbcount |= (uint64_t)(es->e4fs_fbcount_hi) << 32;
372 fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
373 fs->e2fs_bsize = 1U << fs->e2fs_bshift;
374 fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
375 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
376 fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize;
378 fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
379 fs->e2fs_bpg = es->e2fs_bpg;
380 fs->e2fs_fpg = es->e2fs_fpg;
381 fs->e2fs_ipg = es->e2fs_ipg;
382 if (es->e2fs_rev == E2FS_REV0) {
383 fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
385 fs->e2fs_isize = es->e2fs_inode_size;
388 * Simple sanity check for superblock inode size value.
390 if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
391 EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
392 (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
393 printf("ext2fs: invalid inode size %d\n",
398 /* Check for extra isize in big inodes. */
399 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
400 EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
401 printf("ext2fs: no space for extra inode timestamps\n");
404 /* Check checksum features */
405 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) &&
406 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
407 printf("ext2fs: incorrect checksum features combination\n");
410 /* Check for group descriptor size */
411 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
412 (es->e3fs_desc_size != sizeof(struct ext2_gd))) {
413 printf("ext2fs: group descriptor size unsupported %d\n",
417 /* Check for group size */
418 if (fs->e2fs_bpg != fs->e2fs_bsize * 8) {
419 printf("ext2fs: non-standard group size unsupported %d\n",
424 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
425 fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
426 /* s_resuid / s_resgid ? */
427 fs->e2fs_gcount = howmany(fs->e2fs_bcount - es->e2fs_first_dblock,
428 EXT2_BLOCKS_PER_GROUP(fs));
429 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
430 e2fs_descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
431 e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
433 e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
434 e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
435 fs->e2fs_bsize / sizeof(struct ext2_gd));
437 fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
438 fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
439 M_EXT2MNT, M_WAITOK | M_ZERO);
440 fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
441 sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
443 for (i = 0; i < fs->e2fs_gdbcount; i++) {
445 fsbtodb(fs, cg_location(fs, i)),
446 fs->e2fs_bsize, NOCRED, &bp);
448 free(fs->e2fs_contigdirs, M_EXT2MNT);
449 free(fs->e2fs_gd, M_EXT2MNT);
453 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
455 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
456 bp->b_data, fs->e2fs_bsize);
458 for (j = 0; j < e2fs_descpb &&
459 g_count < fs->e2fs_gcount; j++, g_count++)
460 memcpy(&fs->e2fs_gd[g_count],
461 bp->b_data + j * E2FS_REV0_GD_SIZE,
467 /* Precompute checksum seed for all metadata */
468 ext2_sb_csum_set_seed(fs);
470 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
471 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
472 error = ext2_gd_csum_verify(fs, devvp->v_rdev);
476 /* Initialization for the ext2 Orlov allocator variant. */
477 fs->e2fs_total_dir = 0;
478 for (i = 0; i < fs->e2fs_gcount; i++)
479 fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]);
481 if (es->e2fs_rev == E2FS_REV0 ||
482 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
483 fs->e2fs_maxfilesize = 0x7fffffff;
485 fs->e2fs_maxfilesize = 0xffffffffffff;
486 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
487 fs->e2fs_maxfilesize = 0x7fffffffffffffff;
489 if (es->e4fs_flags & E2FS_UNSIGNED_HASH) {
491 } else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) {
492 #ifdef __CHAR_UNSIGNED__
493 es->e4fs_flags |= E2FS_UNSIGNED_HASH;
496 es->e4fs_flags |= E2FS_SIGNED_HASH;
499 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
500 error = ext2_sb_csum_verify(fs);
506 * Reload all incore data for a filesystem (used after running fsck on
507 * the root filesystem and finding things to fix). The filesystem must
508 * be mounted read-only.
510 * Things to do to update the mount:
511 * 1) invalidate all cached meta-data.
512 * 2) re-read superblock from disk.
513 * 3) invalidate all cluster summary information.
514 * 4) invalidate all inactive vnodes.
515 * 5) invalidate all cached file data.
516 * 6) re-read inode data for all active vnodes.
517 * XXX we are missing some steps, in particular # 3, this has to be reviewed.
520 ext2_reload(struct mount *mp, struct thread *td)
522 struct vnode *vp, *mvp, *devvp;
531 if ((mp->mnt_flag & MNT_RDONLY) == 0)
534 * Step 1: invalidate all cached meta-data.
536 devvp = VFSTOEXT2(mp)->um_devvp;
537 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
538 if (vinvalbuf(devvp, 0, 0, 0) != 0)
539 panic("ext2_reload: dirty1");
540 VOP_UNLOCK(devvp, 0);
543 * Step 2: re-read superblock from disk.
544 * constants have been adjusted for ext2
546 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
548 es = (struct ext2fs *)bp->b_data;
549 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
551 return (EIO); /* XXX needs translation */
553 fs = VFSTOEXT2(mp)->um_e2fs;
554 bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
556 if ((error = compute_sb_data(devvp, es, fs)) != 0) {
561 if (fs->fs_sbsize < SBSIZE)
562 bp->b_flags |= B_INVAL;
567 * Step 3: invalidate all cluster summary information.
569 if (fs->e2fs_contigsumsize > 0) {
570 lp = fs->e2fs_maxcluster;
571 sump = fs->e2fs_clustersum;
572 for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
573 *lp++ = fs->e2fs_contigsumsize;
575 bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
580 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
582 * Step 4: invalidate all cached file data.
584 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
585 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
588 if (vinvalbuf(vp, 0, 0, 0))
589 panic("ext2_reload: dirty2");
592 * Step 5: re-read inode data for all active vnodes.
595 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
596 (int)fs->e2fs_bsize, NOCRED, &bp);
600 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
603 ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
604 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
613 * Common code for mount and mountroot.
616 ext2_mountfs(struct vnode *devvp, struct mount *mp)
618 struct ext2mount *ump;
622 struct cdev *dev = devvp->v_rdev;
623 struct g_consumer *cp;
631 int32_t e2fs_maxcontig;
633 ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
634 /* XXX: use VOP_ACESS to check FS perms */
636 error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
638 VOP_UNLOCK(devvp, 0);
642 /* XXX: should we check for some sectorsize or 512 instead? */
643 if (((SBSIZE % cp->provider->sectorsize) != 0) ||
644 (SBSIZE < cp->provider->sectorsize)) {
651 bo = &devvp->v_bufobj;
653 bo->bo_ops = g_vfs_bufops;
654 if (devvp->v_rdev->si_iosize_max != 0)
655 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
656 if (mp->mnt_iosize_max > MAXPHYS)
657 mp->mnt_iosize_max = MAXPHYS;
661 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
663 es = (struct ext2fs *)bp->b_data;
664 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
665 error = EINVAL; /* XXX needs translation */
668 if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
669 (es->e2fs_state & E2FS_ERRORS)) {
670 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
672 "WARNING: Filesystem was not properly dismounted\n");
675 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
680 ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
683 * I don't know whether this is the right strategy. Note that
684 * we dynamically allocate both an m_ext2fs and an ext2fs
685 * while Linux keeps the super block in a locked buffer.
687 ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
688 M_EXT2MNT, M_WAITOK | M_ZERO);
689 ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
690 M_EXT2MNT, M_WAITOK);
691 mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
692 bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
693 if ((error = compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
697 * Calculate the maximum contiguous blocks and size of cluster summary
698 * array. In FFS this is done by newfs; however, the superblock
699 * in ext2fs doesn't have these variables, so we can calculate
702 e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
703 ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
704 if (ump->um_e2fs->e2fs_contigsumsize > 0) {
705 size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
706 ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
707 size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
708 ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
709 lp = ump->um_e2fs->e2fs_maxcluster;
710 sump = ump->um_e2fs->e2fs_clustersum;
711 for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
712 *lp++ = ump->um_e2fs->e2fs_contigsumsize;
714 sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
715 sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
722 fs->e2fs_ronly = ronly; /* ronly is set according to mnt_flags */
725 * If the fs is not mounted read-only, make sure the super block is
726 * always written back on a sync().
728 fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
730 fs->e2fs_fmod = 1; /* mark it modified */
731 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN; /* set fs invalid */
734 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
735 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
736 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
738 mp->mnt_flag |= MNT_LOCAL;
742 ump->um_devvp = devvp;
743 ump->um_bo = &devvp->v_bufobj;
747 * Setting those two parameters allowed us to use
748 * ufs_bmap w/o changse!
750 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
751 ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
752 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
754 ext2_sbupdate(ump, MNT_WAIT);
756 * Initialize filesystem stat information in mount struct.
759 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
772 mtx_destroy(EXT2_MTX(ump));
773 free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
774 free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
775 free(ump->um_e2fs->e2fs, M_EXT2MNT);
776 free(ump->um_e2fs, M_EXT2MNT);
777 free(ump, M_EXT2MNT);
784 * Unmount system call.
787 ext2_unmount(struct mount *mp, int mntflags)
789 struct ext2mount *ump;
792 int error, flags, i, ronly;
795 if (mntflags & MNT_FORCE) {
796 if (mp->mnt_flag & MNT_ROOTFS)
800 if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
804 ronly = fs->e2fs_ronly;
805 if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
806 if (fs->e2fs_wasvalid)
807 fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
808 ext2_sbupdate(ump, MNT_WAIT);
812 g_vfs_close(ump->um_cp);
814 vrele(ump->um_devvp);
815 sump = fs->e2fs_clustersum;
816 for (i = 0; i < fs->e2fs_gcount; i++, sump++)
817 free(sump->cs_sum, M_EXT2MNT);
818 free(fs->e2fs_clustersum, M_EXT2MNT);
819 free(fs->e2fs_maxcluster, M_EXT2MNT);
820 free(fs->e2fs_gd, M_EXT2MNT);
821 free(fs->e2fs_contigdirs, M_EXT2MNT);
822 free(fs->e2fs, M_EXT2MNT);
824 free(ump, M_EXT2MNT);
827 mp->mnt_flag &= ~MNT_LOCAL;
833 * Flush out all the files in a filesystem.
836 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
840 error = vflush(mp, 0, flags, td);
845 * Get filesystem statistics.
848 ext2_statfs(struct mount *mp, struct statfs *sbp)
850 struct ext2mount *ump;
852 uint32_t overhead, overhead_per_group, ngdb;
857 if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
858 panic("ext2_statfs");
861 * Compute the overhead (FS structures)
864 1 /* block bitmap */ +
865 1 /* inode bitmap */ +
867 overhead = fs->e2fs->e2fs_first_dblock +
868 fs->e2fs_gcount * overhead_per_group;
869 if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
870 fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
871 for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
872 if (ext2_cg_has_sb(fs, i))
876 ngroups = fs->e2fs_gcount;
878 ngdb = fs->e2fs_gdbcount;
879 if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
880 fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
881 ngdb += fs->e2fs->e2fs_reserved_ngdb;
882 overhead += ngroups * (1 /* superblock */ + ngdb);
884 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
885 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
886 sbp->f_blocks = fs->e2fs_bcount - overhead;
887 sbp->f_bfree = fs->e2fs_fbcount;
888 sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
889 sbp->f_files = fs->e2fs->e2fs_icount;
890 sbp->f_ffree = fs->e2fs->e2fs_ficount;
895 * Go through the disk queues to initiate sandbagged IO;
896 * go through the inodes to write those that have been modified;
897 * initiate the writing of the super block if it has been modified.
899 * Note: we are always called with the filesystem marked `MPBUSY'.
902 ext2_sync(struct mount *mp, int waitfor)
904 struct vnode *mvp, *vp;
907 struct ext2mount *ump = VFSTOEXT2(mp);
909 int error, allerror = 0;
913 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
914 printf("fs = %s\n", fs->e2fs_fsmnt);
915 panic("ext2_sync: rofs mod");
919 * Write back each (modified) inode.
922 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
923 if (vp->v_type == VNON) {
929 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
930 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
931 waitfor == MNT_LAZY)) {
935 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
937 if (error == ENOENT) {
938 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
943 if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
950 * Force stale filesystem control information to be flushed.
952 if (waitfor != MNT_LAZY) {
953 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
954 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
956 VOP_UNLOCK(ump->um_devvp, 0);
960 * Write back modified superblock.
962 if (fs->e2fs_fmod != 0) {
964 fs->e2fs->e2fs_wtime = time_second;
965 if ((error = ext2_cgupdate(ump, waitfor)) != 0)
972 * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
973 * in from disk. If it is in core, wait for the lock bit to clear, then
974 * return the inode locked. Detection and handling of mount points must be
975 * done by the calling routine.
978 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
982 struct ext2mount *ump;
990 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
991 if (error || *vpp != NULL)
995 ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
997 /* Allocate a new vnode/inode. */
998 if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
1000 free(ip, M_EXT2NODE);
1005 ip->i_e2fs = fs = ump->um_e2fs;
1009 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1010 error = insmntque(vp, mp);
1012 free(ip, M_EXT2NODE);
1016 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
1017 if (error || *vpp != NULL)
1020 /* Read in the disk contents for the inode, copy into the inode. */
1021 if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1022 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
1024 * The inode does not contain anything useful, so it would
1025 * be misleading to leave it on its hash chain. With mode
1026 * still zero, it will be unlinked and returned to the free
1034 /* convert ext2 inode to dinode */
1035 error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
1036 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip);
1038 printf("ext2fs: Bad inode %lu csum - run fsck\n",
1039 (unsigned long)ino);
1045 ip->i_block_group = ino_to_cg(fs, ino);
1046 ip->i_next_alloc_block = 0;
1047 ip->i_next_alloc_goal = 0;
1050 * Now we want to make sure that block pointers for unused
1051 * blocks are zeroed out - ext2_balloc depends on this
1052 * although for regular files and directories only
1054 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1055 * out because we could corrupt the extent tree.
1057 if (!(ip->i_flag & IN_E4EXTENTS) &&
1058 (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1059 used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1060 for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1064 ext2_print_inode(ip);
1065 ext4_ext_print_extent_tree_status(ip);
1070 * Initialize the vnode from the inode, check for aliases.
1071 * Note that the underlying vnode may have changed.
1073 if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
1080 * Finish inode initialization.
1088 * File handle to vnode
1090 * Have to be really careful about stale file handles:
1091 * - check that the inode number is valid
1092 * - call ext2_vget() to get the locked inode
1093 * - check for an unallocated inode (i_mode == 0)
1094 * - check that the given client host has export rights and return
1095 * those rights via. exflagsp and credanonp
1098 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1103 struct m_ext2fs *fs;
1106 ufhp = (struct ufid *)fhp;
1107 fs = VFSTOEXT2(mp)->um_e2fs;
1108 if (ufhp->ufid_ino < EXT2_ROOTINO ||
1109 ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1112 error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1118 if (ip->i_mode == 0 ||
1119 ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1125 vnode_create_vobject(*vpp, 0, curthread);
1130 * Write a superblock and associated information back to disk.
1133 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1135 struct m_ext2fs *fs = mp->um_e2fs;
1136 struct ext2fs *es = fs->e2fs;
1140 es->e2fs_bcount = fs->e2fs_bcount & 0xffffffff;
1141 es->e2fs_rbcount = fs->e2fs_rbcount & 0xffffffff;
1142 es->e2fs_fbcount = fs->e2fs_fbcount & 0xffffffff;
1143 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1144 es->e4fs_bcount_hi = fs->e2fs_bcount >> 32;
1145 es->e4fs_rbcount_hi = fs->e2fs_rbcount >> 32;
1146 es->e4fs_fbcount_hi = fs->e2fs_fbcount >> 32;
1149 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1150 ext2_sb_csum_set(fs);
1152 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1153 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1154 if (waitfor == MNT_WAIT)
1160 * The buffers for group descriptors, inode bitmaps and block bitmaps
1161 * are not busy at this point and are (hopefully) written by the
1162 * usual sync mechanism. No need to write them here.
1167 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1169 struct m_ext2fs *fs = mp->um_e2fs;
1171 int i, j, g_count = 0, error = 0, allerror = 0;
1173 allerror = ext2_sbupdate(mp, waitfor);
1175 /* Update gd csums */
1176 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1177 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1178 ext2_gd_csum_set(fs);
1180 for (i = 0; i < fs->e2fs_gdbcount; i++) {
1181 bp = getblk(mp->um_devvp, fsbtodb(fs,
1182 cg_location(fs, i)),
1183 fs->e2fs_bsize, 0, 0, 0);
1184 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1185 memcpy(bp->b_data, &fs->e2fs_gd[
1186 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1189 for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1190 g_count < fs->e2fs_gcount; j++, g_count++)
1191 memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1192 &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1194 if (waitfor == MNT_WAIT)
1200 if (!allerror && error)
1206 * Return the root of a filesystem.
1209 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1214 error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);