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>
57 #include <sys/mutex.h>
59 #include <geom/geom.h>
60 #include <geom/geom_vfs.h>
62 #include <fs/ext2fs/fs.h>
63 #include <fs/ext2fs/ext2_mount.h>
64 #include <fs/ext2fs/inode.h>
66 #include <fs/ext2fs/ext2fs.h>
67 #include <fs/ext2fs/ext2_dinode.h>
68 #include <fs/ext2fs/ext2_extern.h>
69 #include <fs/ext2fs/ext2_extents.h>
71 SDT_PROVIDER_DECLARE(ext2fs);
74 * arg0: verbosity. Higher numbers give more verbose messages
75 * arg1: Textual message
77 SDT_PROBE_DEFINE2(ext2fs, , vfsops, trace, "int", "char*");
78 SDT_PROBE_DEFINE2(ext2fs, , vfsops, ext2_cg_validate_error, "char*", "int");
79 SDT_PROBE_DEFINE1(ext2fs, , vfsops, ext2_compute_sb_data_error, "char*");
82 static int ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
83 static int ext2_mountfs(struct vnode *, struct mount *);
84 static int ext2_reload(struct mount *mp, struct thread *td);
85 static int ext2_sbupdate(struct ext2mount *, int);
86 static int ext2_cgupdate(struct ext2mount *, int);
87 static vfs_unmount_t ext2_unmount;
88 static vfs_root_t ext2_root;
89 static vfs_statfs_t ext2_statfs;
90 static vfs_sync_t ext2_sync;
91 static vfs_vget_t ext2_vget;
92 static vfs_fhtovp_t ext2_fhtovp;
93 static vfs_mount_t ext2_mount;
95 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
96 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
98 static struct vfsops ext2fs_vfsops = {
99 .vfs_fhtovp = ext2_fhtovp,
100 .vfs_mount = ext2_mount,
101 .vfs_root = ext2_root, /* root inode via vget */
102 .vfs_statfs = ext2_statfs,
103 .vfs_sync = ext2_sync,
104 .vfs_unmount = ext2_unmount,
105 .vfs_vget = ext2_vget,
108 VFS_SET(ext2fs_vfsops, ext2fs, 0);
110 static int ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
112 static int ext2_compute_sb_data(struct vnode * devvp,
113 struct ext2fs * es, struct m_ext2fs * fs);
115 static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr",
116 "noclusterw", "noexec", "export", "force", "from", "multilabel",
117 "suiddir", "nosymfollow", "sync", "union", NULL };
125 ext2_mount(struct mount *mp)
127 struct vfsoptlist *opts;
130 struct ext2mount *ump = NULL;
132 struct nameidata nd, *ndp = &nd;
135 int error, flags, len;
138 opts = mp->mnt_optnew;
140 if (vfs_filteropt(opts, ext2_opts))
143 vfs_getopt(opts, "fspath", (void **)&path, NULL);
144 /* Double-check the length of path.. */
145 if (strlen(path) >= MAXMNTLEN)
146 return (ENAMETOOLONG);
149 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
150 if (!error && fspec[len - 1] != '\0')
154 * If updating, check whether changing from read-only to
155 * read/write; if there is no device name, that's all we do.
157 if (mp->mnt_flag & MNT_UPDATE) {
161 if (fs->e2fs_ronly == 0 &&
162 vfs_flagopt(opts, "ro", NULL, 0)) {
163 error = VFS_SYNC(mp, MNT_WAIT);
167 if (mp->mnt_flag & MNT_FORCE)
169 error = ext2_flushfiles(mp, flags, td);
170 if (error == 0 && fs->e2fs_wasvalid &&
171 ext2_cgupdate(ump, MNT_WAIT) == 0) {
172 fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
173 ext2_sbupdate(ump, MNT_WAIT);
176 vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
178 g_access(ump->um_cp, 0, -1, 0);
181 if (!error && (mp->mnt_flag & MNT_RELOAD))
182 error = ext2_reload(mp, td);
185 devvp = ump->um_devvp;
186 if (fs->e2fs_ronly && !vfs_flagopt(opts, "ro", NULL, 0)) {
187 if (ext2_check_sb_compat(fs->e2fs, devvp->v_rdev, 0))
191 * If upgrade to read-write by non-root, then verify
192 * that user has necessary permissions on the device.
194 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
195 error = VOP_ACCESS(devvp, VREAD | VWRITE,
198 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
200 VOP_UNLOCK(devvp, 0);
203 VOP_UNLOCK(devvp, 0);
205 error = g_access(ump->um_cp, 0, 1, 0);
210 if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 ||
211 (fs->e2fs->e2fs_state & E2FS_ERRORS)) {
212 if (mp->mnt_flag & MNT_FORCE) {
214 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
217 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
222 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;
223 (void)ext2_cgupdate(ump, MNT_WAIT);
226 mp->mnt_flag &= ~MNT_RDONLY;
229 if (vfs_flagopt(opts, "export", NULL, 0)) {
230 /* Process export requests in vfs_mount.c. */
236 * Not an update, or updating the name: look up the name
237 * and verify that it refers to a sensible disk device.
241 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
242 if ((error = namei(ndp)) != 0)
244 NDFREE(ndp, NDF_ONLY_PNBUF);
247 if (!vn_isdisk(devvp, &error)) {
253 * If mount by non-root, then verify that user has necessary
254 * permissions on the device.
256 * XXXRW: VOP_ACCESS() enough?
259 if ((mp->mnt_flag & MNT_RDONLY) == 0)
261 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
263 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
269 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
270 error = ext2_mountfs(devvp, mp);
272 if (devvp != ump->um_devvp) {
274 return (EINVAL); /* needs translation */
286 * Note that this strncpy() is ok because of a check at the start
289 strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN);
290 fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0';
291 vfs_mountedfrom(mp, fspec);
296 ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
300 if (es->e2fs_magic != E2FS_MAGIC) {
301 printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
302 devtoname(dev), es->e2fs_magic, E2FS_MAGIC);
305 if (es->e2fs_rev > E2FS_REV0) {
306 mask = es->e2fs_features_incompat & ~(EXT2F_INCOMPAT_SUPP);
308 printf("WARNING: mount of %s denied due to "
309 "unsupported optional features:\n", devtoname(dev));
311 i < sizeof(incompat)/sizeof(struct ext2_feature);
313 if (mask & incompat[i].mask)
314 printf("%s ", incompat[i].name);
318 mask = es->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
319 if (!ronly && mask) {
320 printf("WARNING: R/W mount of %s denied due to "
321 "unsupported optional features:\n", devtoname(dev));
323 i < sizeof(ro_compat)/sizeof(struct ext2_feature);
325 if (mask & ro_compat[i].mask)
326 printf("%s ", ro_compat[i].name);
335 ext2_cg_location(struct m_ext2fs *fs, int number)
337 int cg, descpb, logical_sb, has_super = 0;
340 * Adjust logical superblock block number.
341 * Godmar thinks: if the blocksize is greater than 1024, then
342 * the superblock is logically part of block zero.
344 logical_sb = fs->e2fs_bsize > SBSIZE ? 0 : 1;
346 if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
347 number < fs->e2fs->e3fs_first_meta_bg)
348 return (logical_sb + number + 1);
350 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT))
351 descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
353 descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
355 cg = descpb * number;
357 if (ext2_cg_has_sb(fs, cg))
360 return (has_super + cg * (e4fs_daddr_t)EXT2_BLOCKS_PER_GROUP(fs) +
361 fs->e2fs->e2fs_first_dblock);
365 ext2_cg_validate(struct m_ext2fs *fs)
370 uint64_t first_block, last_block, last_cg_block;
372 unsigned int i, cg_count;
374 first_block = fs->e2fs->e2fs_first_dblock;
375 last_cg_block = ext2_cg_number_gdb(fs, 0);
376 cg_count = fs->e2fs_gcount;
378 for (i = 0; i < fs->e2fs_gcount; i++) {
379 gd = &fs->e2fs_gd[i];
381 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
382 i == fs->e2fs_gcount - 1) {
383 last_block = fs->e2fs_bcount - 1;
385 last_block = first_block +
386 (EXT2_BLOCKS_PER_GROUP(fs) - 1);
389 if ((cg_count == fs->e2fs_gcount) &&
390 !(gd->ext4bgd_flags & EXT2_BG_INODE_ZEROED))
393 b_bitmap = e2fs_gd_get_b_bitmap(gd);
395 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
396 "block bitmap is zero", i);
400 if (b_bitmap <= last_cg_block) {
401 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
402 "block bitmap overlaps gds", i);
405 if (b_bitmap < first_block || b_bitmap > last_block) {
406 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
407 "block bitmap not in group", i);
411 i_bitmap = e2fs_gd_get_i_bitmap(gd);
413 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
414 "inode bitmap is zero", i);
417 if (i_bitmap <= last_cg_block) {
418 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
419 "inode bitmap overlaps gds", i);
422 if (i_bitmap < first_block || i_bitmap > last_block) {
423 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
424 "inode bitmap not in group blk", i);
428 i_tables = e2fs_gd_get_i_tables(gd);
430 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
431 "inode table is zero", i);
434 if (i_tables <= last_cg_block) {
435 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
436 "inode talbes overlaps gds", i);
439 if (i_tables < first_block ||
440 i_tables + fs->e2fs_itpg - 1 > last_block) {
441 SDT_PROBE2(ext2fs, , vfsops, ext2_cg_validate_error,
442 "inode tables not in group blk", i);
446 if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG))
447 first_block += EXT2_BLOCKS_PER_GROUP(fs);
454 * This computes the fields of the m_ext2fs structure from the
455 * data in the ext2fs structure read in.
458 ext2_compute_sb_data(struct vnode *devvp, struct ext2fs *es,
462 uint32_t e2fs_descpb, e2fs_gdbcount_alloc;
467 /* Check checksum features */
468 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) &&
469 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
470 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
471 "incorrect checksum features combination");
475 /* Precompute checksum seed for all metadata */
476 ext2_sb_csum_set_seed(fs);
478 /* Verify sb csum if possible */
479 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
480 error = ext2_sb_csum_verify(fs);
486 /* Check for block size = 1K|2K|4K */
487 if (es->e2fs_log_bsize > 2) {
488 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
493 fs->e2fs_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->e2fs_log_bsize;
494 fs->e2fs_bsize = 1U << fs->e2fs_bshift;
495 fs->e2fs_fsbtodb = es->e2fs_log_bsize + 1;
496 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
498 /* Check for fragment size */
499 if (es->e2fs_log_fsize >
500 (EXT2_MAX_FRAG_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
501 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
502 "invalid log cluster size");
506 fs->e2fs_fsize = EXT2_MIN_FRAG_SIZE << es->e2fs_log_fsize;
507 if (fs->e2fs_fsize != fs->e2fs_bsize) {
508 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
509 "fragment size != block size");
513 fs->e2fs_fpb = fs->e2fs_bsize / fs->e2fs_fsize;
515 /* Check reserved gdt blocks for future filesystem expansion */
516 if (es->e2fs_reserved_ngdb > (fs->e2fs_bsize / 4)) {
517 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
518 "number of reserved GDT blocks too large");
522 if (es->e2fs_rev == E2FS_REV0) {
523 fs->e2fs_isize = E2FS_REV0_INODE_SIZE;
525 fs->e2fs_isize = es->e2fs_inode_size;
530 if (es->e2fs_first_ino < EXT2_FIRSTINO) {
531 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
532 "invalid first ino");
537 * Simple sanity check for superblock inode size value.
539 if (EXT2_INODE_SIZE(fs) < E2FS_REV0_INODE_SIZE ||
540 EXT2_INODE_SIZE(fs) > fs->e2fs_bsize ||
541 (fs->e2fs_isize & (fs->e2fs_isize - 1)) != 0) {
542 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
543 "invalid inode size");
548 /* Check group descriptors */
549 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
550 es->e3fs_desc_size != E2FS_64BIT_GD_SIZE) {
551 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
552 "unsupported 64bit descriptor size");
556 fs->e2fs_bpg = es->e2fs_bpg;
557 fs->e2fs_fpg = es->e2fs_fpg;
558 if (fs->e2fs_bpg == 0 || fs->e2fs_fpg == 0) {
559 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
560 "zero blocks/fragments per group");
562 } else if (fs->e2fs_bpg != fs->e2fs_fpg) {
563 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
564 "blocks per group not equal fragments per group");
568 if (fs->e2fs_bpg != fs->e2fs_bsize * 8) {
569 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
570 "non-standard group size unsupported");
574 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_INODE_SIZE(fs);
575 if (fs->e2fs_ipb == 0 ||
576 fs->e2fs_ipb > fs->e2fs_bsize / E2FS_REV0_INODE_SIZE) {
577 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
578 "bad inodes per block size");
582 fs->e2fs_ipg = es->e2fs_ipg;
583 if (fs->e2fs_ipg < fs->e2fs_ipb || fs->e2fs_ipg > fs->e2fs_bsize * 8) {
584 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
585 "invalid inodes per group");
589 fs->e2fs_itpg = fs->e2fs_ipg / fs->e2fs_ipb;
591 fs->e2fs_bcount = es->e2fs_bcount;
592 fs->e2fs_rbcount = es->e2fs_rbcount;
593 fs->e2fs_fbcount = es->e2fs_fbcount;
594 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
595 fs->e2fs_bcount |= (uint64_t)(es->e4fs_bcount_hi) << 32;
596 fs->e2fs_rbcount |= (uint64_t)(es->e4fs_rbcount_hi) << 32;
597 fs->e2fs_fbcount |= (uint64_t)(es->e4fs_fbcount_hi) << 32;
599 if (fs->e2fs_rbcount > fs->e2fs_bcount ||
600 fs->e2fs_fbcount > fs->e2fs_bcount) {
601 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
602 "invalid block count");
605 if (es->e2fs_first_dblock >= fs->e2fs_bcount) {
606 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
607 "first data block out of range");
611 fs->e2fs_gcount = howmany(fs->e2fs_bcount - es->e2fs_first_dblock,
612 EXT2_BLOCKS_PER_GROUP(fs));
613 if (fs->e2fs_gcount > ((uint64_t)1 << 32) - EXT2_DESCS_PER_BLOCK(fs)) {
614 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
615 "groups count too large");
619 /* Check for extra isize in big inodes. */
620 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) &&
621 EXT2_INODE_SIZE(fs) < sizeof(struct ext2fs_dinode)) {
622 SDT_PROBE1(ext2fs, , vfsops, ext2_compute_sb_data_error,
623 "no space for extra inode timestamps");
627 /* s_resuid / s_resgid ? */
629 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
630 e2fs_descpb = fs->e2fs_bsize / E2FS_64BIT_GD_SIZE;
631 e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount, e2fs_descpb);
633 e2fs_descpb = fs->e2fs_bsize / E2FS_REV0_GD_SIZE;
634 e2fs_gdbcount_alloc = howmany(fs->e2fs_gcount,
635 fs->e2fs_bsize / sizeof(struct ext2_gd));
637 fs->e2fs_gdbcount = howmany(fs->e2fs_gcount, e2fs_descpb);
638 fs->e2fs_gd = malloc(e2fs_gdbcount_alloc * fs->e2fs_bsize,
639 M_EXT2MNT, M_WAITOK | M_ZERO);
640 fs->e2fs_contigdirs = malloc(fs->e2fs_gcount *
641 sizeof(*fs->e2fs_contigdirs), M_EXT2MNT, M_WAITOK | M_ZERO);
643 for (i = 0; i < fs->e2fs_gdbcount; i++) {
645 fsbtodb(fs, ext2_cg_location(fs, i)),
646 fs->e2fs_bsize, NOCRED, &bp);
649 * fs->e2fs_gd and fs->e2fs_contigdirs
650 * will be freed later by the caller,
651 * because this function could be called from
657 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
659 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
660 bp->b_data, fs->e2fs_bsize);
662 for (j = 0; j < e2fs_descpb &&
663 g_count < fs->e2fs_gcount; j++, g_count++)
664 memcpy(&fs->e2fs_gd[g_count],
665 bp->b_data + j * E2FS_REV0_GD_SIZE,
672 /* Validate cgs consistency */
673 error = ext2_cg_validate(fs);
678 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
679 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
680 error = ext2_gd_csum_verify(fs, devvp->v_rdev);
684 /* Initialization for the ext2 Orlov allocator variant. */
685 fs->e2fs_total_dir = 0;
686 for (i = 0; i < fs->e2fs_gcount; i++)
687 fs->e2fs_total_dir += e2fs_gd_get_ndirs(&fs->e2fs_gd[i]);
689 if (es->e2fs_rev == E2FS_REV0 ||
690 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
691 fs->e2fs_maxfilesize = 0x7fffffff;
693 fs->e2fs_maxfilesize = 0xffffffffffff;
694 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE))
695 fs->e2fs_maxfilesize = 0x7fffffffffffffff;
697 if (es->e4fs_flags & E2FS_UNSIGNED_HASH) {
699 } else if ((es->e4fs_flags & E2FS_SIGNED_HASH) == 0) {
700 #ifdef __CHAR_UNSIGNED__
701 es->e4fs_flags |= E2FS_UNSIGNED_HASH;
704 es->e4fs_flags |= E2FS_SIGNED_HASH;
707 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
708 error = ext2_sb_csum_verify(fs);
714 * Reload all incore data for a filesystem (used after running fsck on
715 * the root filesystem and finding things to fix). The filesystem must
716 * be mounted read-only.
718 * Things to do to update the mount:
719 * 1) invalidate all cached meta-data.
720 * 2) re-read superblock from disk.
721 * 3) invalidate all cluster summary information.
722 * 4) invalidate all inactive vnodes.
723 * 5) invalidate all cached file data.
724 * 6) re-read inode data for all active vnodes.
725 * XXX we are missing some steps, in particular # 3, this has to be reviewed.
728 ext2_reload(struct mount *mp, struct thread *td)
730 struct vnode *vp, *mvp, *devvp;
739 if ((mp->mnt_flag & MNT_RDONLY) == 0)
742 * Step 1: invalidate all cached meta-data.
744 devvp = VFSTOEXT2(mp)->um_devvp;
745 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
746 if (vinvalbuf(devvp, 0, 0, 0) != 0)
747 panic("ext2_reload: dirty1");
748 VOP_UNLOCK(devvp, 0);
751 * Step 2: re-read superblock from disk.
752 * constants have been adjusted for ext2
754 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
756 es = (struct ext2fs *)bp->b_data;
757 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
759 return (EIO); /* XXX needs translation */
761 fs = VFSTOEXT2(mp)->um_e2fs;
762 bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
764 if ((error = ext2_compute_sb_data(devvp, es, fs)) != 0) {
769 if (fs->fs_sbsize < SBSIZE)
770 bp->b_flags |= B_INVAL;
775 * Step 3: invalidate all cluster summary information.
777 if (fs->e2fs_contigsumsize > 0) {
778 lp = fs->e2fs_maxcluster;
779 sump = fs->e2fs_clustersum;
780 for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
781 *lp++ = fs->e2fs_contigsumsize;
783 bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
788 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
790 * Step 4: invalidate all cached file data.
792 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
793 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
796 if (vinvalbuf(vp, 0, 0, 0))
797 panic("ext2_reload: dirty2");
800 * Step 5: re-read inode data for all active vnodes.
803 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
804 (int)fs->e2fs_bsize, NOCRED, &bp);
808 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
812 error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
813 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
820 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
828 * Common code for mount and mountroot.
831 ext2_mountfs(struct vnode *devvp, struct mount *mp)
833 struct ext2mount *ump;
837 struct cdev *dev = devvp->v_rdev;
838 struct g_consumer *cp;
846 int32_t e2fs_maxcontig;
848 ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
849 /* XXX: use VOP_ACESS to check FS perms */
851 error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
853 VOP_UNLOCK(devvp, 0);
857 /* XXX: should we check for some sectorsize or 512 instead? */
858 if (((SBSIZE % cp->provider->sectorsize) != 0) ||
859 (SBSIZE < cp->provider->sectorsize)) {
866 bo = &devvp->v_bufobj;
868 bo->bo_ops = g_vfs_bufops;
869 if (devvp->v_rdev->si_iosize_max != 0)
870 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
871 if (mp->mnt_iosize_max > MAXPHYS)
872 mp->mnt_iosize_max = MAXPHYS;
876 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
878 es = (struct ext2fs *)bp->b_data;
879 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
880 error = EINVAL; /* XXX needs translation */
883 if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
884 (es->e2fs_state & E2FS_ERRORS)) {
885 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
887 "WARNING: Filesystem was not properly dismounted\n");
890 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
895 ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
898 * I don't know whether this is the right strategy. Note that
899 * we dynamically allocate both an m_ext2fs and an ext2fs
900 * while Linux keeps the super block in a locked buffer.
902 ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
903 M_EXT2MNT, M_WAITOK | M_ZERO);
904 ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
905 M_EXT2MNT, M_WAITOK);
906 mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
907 bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
908 if ((error = ext2_compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
912 * Calculate the maximum contiguous blocks and size of cluster summary
913 * array. In FFS this is done by newfs; however, the superblock
914 * in ext2fs doesn't have these variables, so we can calculate
917 e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
918 ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
919 if (ump->um_e2fs->e2fs_contigsumsize > 0) {
920 size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
921 ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
922 size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
923 ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
924 lp = ump->um_e2fs->e2fs_maxcluster;
925 sump = ump->um_e2fs->e2fs_clustersum;
926 for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
927 *lp++ = ump->um_e2fs->e2fs_contigsumsize;
929 sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
930 sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
937 fs->e2fs_ronly = ronly; /* ronly is set according to mnt_flags */
940 * If the fs is not mounted read-only, make sure the super block is
941 * always written back on a sync().
943 fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
945 fs->e2fs_fmod = 1; /* mark it modified */
946 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN; /* set fs invalid */
949 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
950 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
951 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
953 mp->mnt_flag |= MNT_LOCAL;
957 ump->um_devvp = devvp;
958 ump->um_bo = &devvp->v_bufobj;
962 * Setting those two parameters allowed us to use
963 * ufs_bmap w/o changse!
965 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
966 ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
967 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
969 ext2_sbupdate(ump, MNT_WAIT);
971 * Initialize filesystem stat information in mount struct.
974 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED |
987 mtx_destroy(EXT2_MTX(ump));
988 free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
989 free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
990 free(ump->um_e2fs->e2fs, M_EXT2MNT);
991 free(ump->um_e2fs, M_EXT2MNT);
992 free(ump, M_EXT2MNT);
999 * Unmount system call.
1002 ext2_unmount(struct mount *mp, int mntflags)
1004 struct ext2mount *ump;
1005 struct m_ext2fs *fs;
1007 int error, flags, i, ronly;
1010 if (mntflags & MNT_FORCE) {
1011 if (mp->mnt_flag & MNT_ROOTFS)
1013 flags |= FORCECLOSE;
1015 if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
1017 ump = VFSTOEXT2(mp);
1019 ronly = fs->e2fs_ronly;
1020 if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
1021 if (fs->e2fs_wasvalid)
1022 fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
1023 ext2_sbupdate(ump, MNT_WAIT);
1027 g_vfs_close(ump->um_cp);
1028 g_topology_unlock();
1029 vrele(ump->um_devvp);
1030 sump = fs->e2fs_clustersum;
1031 for (i = 0; i < fs->e2fs_gcount; i++, sump++)
1032 free(sump->cs_sum, M_EXT2MNT);
1033 free(fs->e2fs_clustersum, M_EXT2MNT);
1034 free(fs->e2fs_maxcluster, M_EXT2MNT);
1035 free(fs->e2fs_gd, M_EXT2MNT);
1036 free(fs->e2fs_contigdirs, M_EXT2MNT);
1037 free(fs->e2fs, M_EXT2MNT);
1038 free(fs, M_EXT2MNT);
1039 free(ump, M_EXT2MNT);
1040 mp->mnt_data = NULL;
1042 mp->mnt_flag &= ~MNT_LOCAL;
1048 * Flush out all the files in a filesystem.
1051 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
1055 error = vflush(mp, 0, flags, td);
1060 * Get filesystem statistics.
1063 ext2_statfs(struct mount *mp, struct statfs *sbp)
1065 struct ext2mount *ump;
1066 struct m_ext2fs *fs;
1067 uint32_t overhead, overhead_per_group, ngdb;
1070 ump = VFSTOEXT2(mp);
1072 if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
1073 panic("ext2_statfs");
1076 * Compute the overhead (FS structures)
1078 overhead_per_group =
1079 1 /* block bitmap */ +
1080 1 /* inode bitmap */ +
1082 overhead = fs->e2fs->e2fs_first_dblock +
1083 fs->e2fs_gcount * overhead_per_group;
1084 if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
1085 fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
1086 for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
1087 if (ext2_cg_has_sb(fs, i))
1091 ngroups = fs->e2fs_gcount;
1093 ngdb = fs->e2fs_gdbcount;
1094 if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
1095 fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
1096 ngdb += fs->e2fs->e2fs_reserved_ngdb;
1097 overhead += ngroups * (1 /* superblock */ + ngdb);
1099 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
1100 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
1101 sbp->f_blocks = fs->e2fs_bcount - overhead;
1102 sbp->f_bfree = fs->e2fs_fbcount;
1103 sbp->f_bavail = sbp->f_bfree - fs->e2fs_rbcount;
1104 sbp->f_files = fs->e2fs->e2fs_icount;
1105 sbp->f_ffree = fs->e2fs->e2fs_ficount;
1110 * Go through the disk queues to initiate sandbagged IO;
1111 * go through the inodes to write those that have been modified;
1112 * initiate the writing of the super block if it has been modified.
1114 * Note: we are always called with the filesystem marked `MPBUSY'.
1117 ext2_sync(struct mount *mp, int waitfor)
1119 struct vnode *mvp, *vp;
1122 struct ext2mount *ump = VFSTOEXT2(mp);
1123 struct m_ext2fs *fs;
1124 int error, allerror = 0;
1128 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
1129 panic("ext2_sync: rofs mod fs=%s", fs->e2fs_fsmnt);
1133 * Write back each (modified) inode.
1136 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1137 if (vp->v_type == VNON) {
1143 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
1144 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1145 waitfor == MNT_LAZY)) {
1149 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
1151 if (error == ENOENT) {
1152 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1157 if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
1164 * Force stale filesystem control information to be flushed.
1166 if (waitfor != MNT_LAZY) {
1167 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1168 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
1170 VOP_UNLOCK(ump->um_devvp, 0);
1174 * Write back modified superblock.
1176 if (fs->e2fs_fmod != 0) {
1178 fs->e2fs->e2fs_wtime = time_second;
1179 if ((error = ext2_cgupdate(ump, waitfor)) != 0)
1186 * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
1187 * in from disk. If it is in core, wait for the lock bit to clear, then
1188 * return the inode locked. Detection and handling of mount points must be
1189 * done by the calling routine.
1192 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
1194 struct m_ext2fs *fs;
1196 struct ext2mount *ump;
1200 unsigned int i, used_blocks;
1204 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
1205 if (error || *vpp != NULL)
1208 ump = VFSTOEXT2(mp);
1209 ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
1211 /* Allocate a new vnode/inode. */
1212 if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
1214 free(ip, M_EXT2NODE);
1219 ip->i_e2fs = fs = ump->um_e2fs;
1223 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
1224 error = insmntque(vp, mp);
1226 free(ip, M_EXT2NODE);
1230 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
1231 if (error || *vpp != NULL)
1234 /* Read in the disk contents for the inode, copy into the inode. */
1235 if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1236 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
1238 * The inode does not contain anything useful, so it would
1239 * be misleading to leave it on its hash chain. With mode
1240 * still zero, it will be unlinked and returned to the free
1248 /* convert ext2 inode to dinode */
1249 error = ext2_ei2i((struct ext2fs_dinode *)((char *)bp->b_data +
1250 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ino)), ip);
1257 ip->i_block_group = ino_to_cg(fs, ino);
1258 ip->i_next_alloc_block = 0;
1259 ip->i_next_alloc_goal = 0;
1262 * Now we want to make sure that block pointers for unused
1263 * blocks are zeroed out - ext2_balloc depends on this
1264 * although for regular files and directories only
1266 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
1267 * out because we could corrupt the extent tree.
1269 if (!(ip->i_flag & IN_E4EXTENTS) &&
1270 (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
1271 used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
1272 for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1275 #ifdef EXT2FS_PRINT_EXTENTS
1276 ext2_print_inode(ip);
1277 ext4_ext_print_extent_tree_status(ip);
1282 * Initialize the vnode from the inode, check for aliases.
1283 * Note that the underlying vnode may have changed.
1285 if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
1292 * Finish inode initialization.
1300 * File handle to vnode
1302 * Have to be really careful about stale file handles:
1303 * - check that the inode number is valid
1304 * - call ext2_vget() to get the locked inode
1305 * - check for an unallocated inode (i_mode == 0)
1306 * - check that the given client host has export rights and return
1307 * those rights via. exflagsp and credanonp
1310 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1315 struct m_ext2fs *fs;
1318 ufhp = (struct ufid *)fhp;
1319 fs = VFSTOEXT2(mp)->um_e2fs;
1320 if (ufhp->ufid_ino < EXT2_ROOTINO ||
1321 ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1324 error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1330 if (ip->i_mode == 0 ||
1331 ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1337 vnode_create_vobject(*vpp, 0, curthread);
1342 * Write a superblock and associated information back to disk.
1345 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1347 struct m_ext2fs *fs = mp->um_e2fs;
1348 struct ext2fs *es = fs->e2fs;
1352 es->e2fs_bcount = fs->e2fs_bcount & 0xffffffff;
1353 es->e2fs_rbcount = fs->e2fs_rbcount & 0xffffffff;
1354 es->e2fs_fbcount = fs->e2fs_fbcount & 0xffffffff;
1355 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1356 es->e4fs_bcount_hi = fs->e2fs_bcount >> 32;
1357 es->e4fs_rbcount_hi = fs->e2fs_rbcount >> 32;
1358 es->e4fs_fbcount_hi = fs->e2fs_fbcount >> 32;
1361 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1362 ext2_sb_csum_set(fs);
1364 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1365 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1366 if (waitfor == MNT_WAIT)
1372 * The buffers for group descriptors, inode bitmaps and block bitmaps
1373 * are not busy at this point and are (hopefully) written by the
1374 * usual sync mechanism. No need to write them here.
1379 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1381 struct m_ext2fs *fs = mp->um_e2fs;
1383 int i, j, g_count = 0, error = 0, allerror = 0;
1385 allerror = ext2_sbupdate(mp, waitfor);
1387 /* Update gd csums */
1388 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1389 EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
1390 ext2_gd_csum_set(fs);
1392 for (i = 0; i < fs->e2fs_gdbcount; i++) {
1393 bp = getblk(mp->um_devvp, fsbtodb(fs,
1394 ext2_cg_location(fs, i)),
1395 fs->e2fs_bsize, 0, 0, 0);
1396 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT)) {
1397 memcpy(bp->b_data, &fs->e2fs_gd[
1398 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1401 for (j = 0; j < fs->e2fs_bsize / E2FS_REV0_GD_SIZE &&
1402 g_count < fs->e2fs_gcount; j++, g_count++)
1403 memcpy(bp->b_data + j * E2FS_REV0_GD_SIZE,
1404 &fs->e2fs_gd[g_count], E2FS_REV0_GD_SIZE);
1406 if (waitfor == MNT_WAIT)
1412 if (!allerror && error)
1418 * Return the root of a filesystem.
1421 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1426 error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);