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 * Copyright (c) 1989, 1991, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
50 #include <sys/endian.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
54 #include <sys/mutex.h>
56 #include <geom/geom.h>
57 #include <geom/geom_vfs.h>
59 #include <fs/ext2fs/ext2_mount.h>
60 #include <fs/ext2fs/inode.h>
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>
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;
80 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
81 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
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,
93 VFS_SET(ext2fs_vfsops, ext2fs, 0);
95 static int ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev,
97 static int compute_sb_data(struct vnode * devvp,
98 struct ext2fs * es, struct m_ext2fs * fs);
100 static const char *ext2_opts[] = { "acls", "async", "noatime", "noclusterr",
101 "noclusterw", "noexec", "export", "force", "from", "multilabel",
102 "suiddir", "nosymfollow", "sync", "union", NULL };
110 ext2_mount(struct mount *mp)
112 struct vfsoptlist *opts;
115 struct ext2mount *ump = 0;
117 struct nameidata nd, *ndp = &nd;
120 int error, flags, len;
123 opts = mp->mnt_optnew;
125 if (vfs_filteropt(opts, ext2_opts))
128 vfs_getopt(opts, "fspath", (void **)&path, NULL);
129 /* Double-check the length of path.. */
130 if (strlen(path) >= MAXMNTLEN)
131 return (ENAMETOOLONG);
134 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
135 if (!error && fspec[len - 1] != '\0')
139 * If updating, check whether changing from read-only to
140 * read/write; if there is no device name, that's all we do.
142 if (mp->mnt_flag & MNT_UPDATE) {
146 if (fs->e2fs_ronly == 0 &&
147 vfs_flagopt(opts, "ro", NULL, 0)) {
148 error = VFS_SYNC(mp, MNT_WAIT);
152 if (mp->mnt_flag & MNT_FORCE)
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);
160 vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
163 g_access(ump->um_cp, 0, -1, 0);
167 if (!error && (mp->mnt_flag & MNT_RELOAD))
168 error = ext2_reload(mp, td);
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))
177 * If upgrade to read-write by non-root, then verify
178 * that user has necessary permissions on the device.
180 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
181 error = VOP_ACCESS(devvp, VREAD | VWRITE,
184 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
186 VOP_UNLOCK(devvp, 0);
189 VOP_UNLOCK(devvp, 0);
192 error = g_access(ump->um_cp, 0, 1, 0);
198 if ((fs->e2fs->e2fs_state & E2FS_ISCLEAN) == 0 ||
199 (fs->e2fs->e2fs_state & E2FS_ERRORS)) {
200 if (mp->mnt_flag & MNT_FORCE) {
202 "WARNING: %s was not properly dismounted\n", fs->e2fs_fsmnt);
205 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
210 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN;
211 (void)ext2_cgupdate(ump, MNT_WAIT);
214 mp->mnt_flag &= ~MNT_RDONLY;
217 if (vfs_flagopt(opts, "export", NULL, 0)) {
218 /* Process export requests in vfs_mount.c. */
224 * Not an update, or updating the name: look up the name
225 * and verify that it refers to a sensible disk device.
229 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
230 if ((error = namei(ndp)) != 0)
232 NDFREE(ndp, NDF_ONLY_PNBUF);
235 if (!vn_isdisk(devvp, &error)) {
241 * If mount by non-root, then verify that user has necessary
242 * permissions on the device.
244 * XXXRW: VOP_ACCESS() enough?
247 if ((mp->mnt_flag & MNT_RDONLY) == 0)
249 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
251 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
257 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
258 error = ext2_mountfs(devvp, mp);
260 if (devvp != ump->um_devvp) {
262 return (EINVAL); /* needs translation */
274 * Note that this strncpy() is ok because of a check at the start
277 strncpy(fs->e2fs_fsmnt, path, MAXMNTLEN);
278 fs->e2fs_fsmnt[MAXMNTLEN - 1] = '\0';
279 vfs_mountedfrom(mp, fspec);
284 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 if (es->e2fs_features_incompat & ~(EXT2F_INCOMPAT_SUPP |
294 EXT4F_RO_INCOMPAT_SUPP)) {
296 "WARNING: mount of %s denied due to unsupported optional features\n",
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));
311 * This computes the fields of the m_ext2fs structure from the
312 * data in the ext2fs structure read in.
315 compute_sb_data(struct vnode *devvp, struct ext2fs *es,
320 int logic_sb_block = 1; /* XXX for now */
322 uint32_t e2fs_descpb;
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;
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 ;
337 fs->e2fs_isize = es->e2fs_inode_size;
340 * Simple sanity check for superblock inode size value.
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",
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");
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 = howmany(es->e2fs_bcount - es->e2fs_first_dblock,
361 EXT2_BLOCKS_PER_GROUP(fs));
362 e2fs_descpb = fs->e2fs_bsize / sizeof(struct ext2_gd);
363 db_count = howmany(fs->e2fs_gcount, 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);
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.
375 if(fs->e2fs_bsize > SBSIZE)
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);
382 free(fs->e2fs_contigdirs, M_EXT2MNT);
383 free(fs->e2fs_gd, M_EXT2MNT);
387 e2fs_cgload((struct ext2_gd *)bp->b_data,
389 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
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;
399 if (es->e2fs_rev == E2FS_REV0 ||
400 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_LARGEFILE))
401 fs->e2fs_maxfilesize = 0x7fffffff;
403 fs->e2fs_maxfilesize = 0x7fffffffffffffff;
408 * Reload all incore data for a filesystem (used after running fsck on
409 * the root filesystem and finding things to fix). The filesystem must
410 * be mounted read-only.
412 * Things to do to update the mount:
413 * 1) invalidate all cached meta-data.
414 * 2) re-read superblock from disk.
415 * 3) invalidate all cluster summary information.
416 * 4) invalidate all inactive vnodes.
417 * 5) invalidate all cached file data.
418 * 6) re-read inode data for all active vnodes.
419 * XXX we are missing some steps, in particular # 3, this has to be reviewed.
422 ext2_reload(struct mount *mp, struct thread *td)
424 struct vnode *vp, *mvp, *devvp;
433 if ((mp->mnt_flag & MNT_RDONLY) == 0)
436 * Step 1: invalidate all cached meta-data.
438 devvp = VFSTOEXT2(mp)->um_devvp;
439 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
440 if (vinvalbuf(devvp, 0, 0, 0) != 0)
441 panic("ext2_reload: dirty1");
442 VOP_UNLOCK(devvp, 0);
445 * Step 2: re-read superblock from disk.
446 * constants have been adjusted for ext2
448 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
450 es = (struct ext2fs *)bp->b_data;
451 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
453 return (EIO); /* XXX needs translation */
455 fs = VFSTOEXT2(mp)->um_e2fs;
456 bcopy(bp->b_data, fs->e2fs, sizeof(struct ext2fs));
458 if((error = compute_sb_data(devvp, es, fs)) != 0) {
463 if (fs->fs_sbsize < SBSIZE)
464 bp->b_flags |= B_INVAL;
469 * Step 3: invalidate all cluster summary information.
471 if (fs->e2fs_contigsumsize > 0) {
472 lp = fs->e2fs_maxcluster;
473 sump = fs->e2fs_clustersum;
474 for (i = 0; i < fs->e2fs_gcount; i++, sump++) {
475 *lp++ = fs->e2fs_contigsumsize;
477 bzero(sump->cs_sum, fs->e2fs_contigsumsize + 1);
482 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
484 * Step 4: invalidate all cached file data.
486 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
487 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
490 if (vinvalbuf(vp, 0, 0, 0))
491 panic("ext2_reload: dirty2");
494 * Step 5: re-read inode data for all active vnodes.
497 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
498 (int)fs->e2fs_bsize, NOCRED, &bp);
502 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
505 ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data +
506 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
515 * Common code for mount and mountroot.
518 ext2_mountfs(struct vnode *devvp, struct mount *mp)
520 struct ext2mount *ump;
524 struct cdev *dev = devvp->v_rdev;
525 struct g_consumer *cp;
533 int32_t e2fs_maxcontig;
535 ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
536 /* XXX: use VOP_ACESS to check FS perms */
539 error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
542 VOP_UNLOCK(devvp, 0);
546 /* XXX: should we check for some sectorsize or 512 instead? */
547 if (((SBSIZE % cp->provider->sectorsize) != 0) ||
548 (SBSIZE < cp->provider->sectorsize)) {
557 bo = &devvp->v_bufobj;
559 bo->bo_ops = g_vfs_bufops;
560 if (devvp->v_rdev->si_iosize_max != 0)
561 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
562 if (mp->mnt_iosize_max > MAXPHYS)
563 mp->mnt_iosize_max = MAXPHYS;
567 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
569 es = (struct ext2fs *)bp->b_data;
570 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
571 error = EINVAL; /* XXX needs translation */
574 if ((es->e2fs_state & E2FS_ISCLEAN) == 0 ||
575 (es->e2fs_state & E2FS_ERRORS)) {
576 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
578 "WARNING: Filesystem was not properly dismounted\n");
581 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
586 ump = malloc(sizeof(*ump), M_EXT2MNT, M_WAITOK | M_ZERO);
589 * I don't know whether this is the right strategy. Note that
590 * we dynamically allocate both an m_ext2fs and an ext2fs
591 * while Linux keeps the super block in a locked buffer.
593 ump->um_e2fs = malloc(sizeof(struct m_ext2fs),
594 M_EXT2MNT, M_WAITOK | M_ZERO);
595 ump->um_e2fs->e2fs = malloc(sizeof(struct ext2fs),
596 M_EXT2MNT, M_WAITOK);
597 mtx_init(EXT2_MTX(ump), "EXT2FS", "EXT2FS Lock", MTX_DEF);
598 bcopy(es, ump->um_e2fs->e2fs, (u_int)sizeof(struct ext2fs));
599 if ((error = compute_sb_data(devvp, ump->um_e2fs->e2fs, ump->um_e2fs)))
603 * Calculate the maximum contiguous blocks and size of cluster summary
604 * array. In FFS this is done by newfs; however, the superblock
605 * in ext2fs doesn't have these variables, so we can calculate
608 e2fs_maxcontig = MAX(1, MAXPHYS / ump->um_e2fs->e2fs_bsize);
609 ump->um_e2fs->e2fs_contigsumsize = MIN(e2fs_maxcontig, EXT2_MAXCONTIG);
610 if (ump->um_e2fs->e2fs_contigsumsize > 0) {
611 size = ump->um_e2fs->e2fs_gcount * sizeof(int32_t);
612 ump->um_e2fs->e2fs_maxcluster = malloc(size, M_EXT2MNT, M_WAITOK);
613 size = ump->um_e2fs->e2fs_gcount * sizeof(struct csum);
614 ump->um_e2fs->e2fs_clustersum = malloc(size, M_EXT2MNT, M_WAITOK);
615 lp = ump->um_e2fs->e2fs_maxcluster;
616 sump = ump->um_e2fs->e2fs_clustersum;
617 for (i = 0; i < ump->um_e2fs->e2fs_gcount; i++, sump++) {
618 *lp++ = ump->um_e2fs->e2fs_contigsumsize;
620 sump->cs_sum = malloc((ump->um_e2fs->e2fs_contigsumsize + 1) *
621 sizeof(int32_t), M_EXT2MNT, M_WAITOK | M_ZERO);
628 fs->e2fs_ronly = ronly; /* ronly is set according to mnt_flags */
631 * If the fs is not mounted read-only, make sure the super block is
632 * always written back on a sync().
634 fs->e2fs_wasvalid = fs->e2fs->e2fs_state & E2FS_ISCLEAN ? 1 : 0;
636 fs->e2fs_fmod = 1; /* mark it modified */
637 fs->e2fs->e2fs_state &= ~E2FS_ISCLEAN; /* set fs invalid */
640 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
641 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
642 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
644 mp->mnt_flag |= MNT_LOCAL;
648 ump->um_devvp = devvp;
649 ump->um_bo = &devvp->v_bufobj;
653 * Setting those two parameters allowed us to use
654 * ufs_bmap w/o changse!
656 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
657 ump->um_bptrtodb = fs->e2fs->e2fs_log_bsize + 1;
658 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
660 ext2_sbupdate(ump, MNT_WAIT);
662 * Initialize filesystem stat information in mount struct.
665 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
666 MNTK_EXTENDED_SHARED;
680 mtx_destroy(EXT2_MTX(ump));
681 free(ump->um_e2fs->e2fs_gd, M_EXT2MNT);
682 free(ump->um_e2fs->e2fs_contigdirs, M_EXT2MNT);
683 free(ump->um_e2fs->e2fs, M_EXT2MNT);
684 free(ump->um_e2fs, M_EXT2MNT);
685 free(ump, M_EXT2MNT);
692 * Unmount system call.
695 ext2_unmount(struct mount *mp, int mntflags)
697 struct ext2mount *ump;
700 int error, flags, i, ronly;
703 if (mntflags & MNT_FORCE) {
704 if (mp->mnt_flag & MNT_ROOTFS)
708 if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
712 ronly = fs->e2fs_ronly;
713 if (ronly == 0 && ext2_cgupdate(ump, MNT_WAIT) == 0) {
714 if (fs->e2fs_wasvalid)
715 fs->e2fs->e2fs_state |= E2FS_ISCLEAN;
716 ext2_sbupdate(ump, MNT_WAIT);
721 g_vfs_close(ump->um_cp);
724 vrele(ump->um_devvp);
725 sump = fs->e2fs_clustersum;
726 for (i = 0; i < fs->e2fs_gcount; i++, sump++)
727 free(sump->cs_sum, M_EXT2MNT);
728 free(fs->e2fs_clustersum, M_EXT2MNT);
729 free(fs->e2fs_maxcluster, M_EXT2MNT);
730 free(fs->e2fs_gd, M_EXT2MNT);
731 free(fs->e2fs_contigdirs, M_EXT2MNT);
732 free(fs->e2fs, M_EXT2MNT);
734 free(ump, M_EXT2MNT);
737 mp->mnt_flag &= ~MNT_LOCAL;
743 * Flush out all the files in a filesystem.
746 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
750 error = vflush(mp, 0, flags, td);
754 * Get filesystem statistics.
757 ext2_statfs(struct mount *mp, struct statfs *sbp)
759 struct ext2mount *ump;
761 uint32_t overhead, overhead_per_group, ngdb;
766 if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
767 panic("ext2_statfs");
770 * Compute the overhead (FS structures)
773 1 /* block bitmap */ +
774 1 /* inode bitmap */ +
776 overhead = fs->e2fs->e2fs_first_dblock +
777 fs->e2fs_gcount * overhead_per_group;
778 if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
779 fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
780 for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
785 ngroups = fs->e2fs_gcount;
787 ngdb = fs->e2fs_gdbcount;
788 if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
789 fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
790 ngdb += fs->e2fs->e2fs_reserved_ngdb;
791 overhead += ngroups * (1 /* superblock */ + ngdb);
793 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
794 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
795 sbp->f_blocks = fs->e2fs->e2fs_bcount - overhead;
796 sbp->f_bfree = fs->e2fs->e2fs_fbcount;
797 sbp->f_bavail = sbp->f_bfree - fs->e2fs->e2fs_rbcount;
798 sbp->f_files = fs->e2fs->e2fs_icount;
799 sbp->f_ffree = fs->e2fs->e2fs_ficount;
804 * Go through the disk queues to initiate sandbagged IO;
805 * go through the inodes to write those that have been modified;
806 * initiate the writing of the super block if it has been modified.
808 * Note: we are always called with the filesystem marked `MPBUSY'.
811 ext2_sync(struct mount *mp, int waitfor)
813 struct vnode *mvp, *vp;
816 struct ext2mount *ump = VFSTOEXT2(mp);
818 int error, allerror = 0;
822 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
823 printf("fs = %s\n", fs->e2fs_fsmnt);
824 panic("ext2_sync: rofs mod");
828 * Write back each (modified) inode.
831 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
832 if (vp->v_type == VNON) {
838 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
839 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
840 waitfor == MNT_LAZY)) {
844 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
846 if (error == ENOENT) {
847 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
852 if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
859 * Force stale filesystem control information to be flushed.
861 if (waitfor != MNT_LAZY) {
862 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
863 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
865 VOP_UNLOCK(ump->um_devvp, 0);
869 * Write back modified superblock.
871 if (fs->e2fs_fmod != 0) {
873 fs->e2fs->e2fs_wtime = time_second;
874 if ((error = ext2_cgupdate(ump, waitfor)) != 0)
881 * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
882 * in from disk. If it is in core, wait for the lock bit to clear, then
883 * return the inode locked. Detection and handling of mount points must be
884 * done by the calling routine.
887 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
891 struct ext2mount *ump;
899 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
900 if (error || *vpp != NULL)
906 * If this malloc() is performed after the getnewvnode()
907 * it might block, leaving a vnode with a NULL v_data to be
908 * found by ext2_sync() if a sync happens to fire right then,
909 * which will cause a panic because ext2_sync() blindly
910 * dereferences vp->v_data (as well it should).
912 ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
914 /* Allocate a new vnode/inode. */
915 if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
917 free(ip, M_EXT2NODE);
922 ip->i_e2fs = fs = ump->um_e2fs;
926 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
927 error = insmntque(vp, mp);
929 free(ip, M_EXT2NODE);
933 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
934 if (error || *vpp != NULL)
937 /* Read in the disk contents for the inode, copy into the inode. */
938 if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
939 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
941 * The inode does not contain anything useful, so it would
942 * be misleading to leave it on its hash chain. With mode
943 * still zero, it will be unlinked and returned to the free
951 /* convert ext2 inode to dinode */
952 ext2_ei2i((struct ext2fs_dinode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
953 ino_to_fsbo(fs, ino)), ip);
954 ip->i_block_group = ino_to_cg(fs, ino);
955 ip->i_next_alloc_block = 0;
956 ip->i_next_alloc_goal = 0;
959 * Now we want to make sure that block pointers for unused
960 * blocks are zeroed out - ext2_balloc depends on this
961 * although for regular files and directories only
963 * If IN_E4EXTENTS is enabled, unused blocks are not zeroed
964 * out because we could corrupt the extent tree.
966 if (!(ip->i_flag & IN_E4EXTENTS) &&
967 (S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode))) {
968 used_blocks = howmany(ip->i_size, fs->e2fs_bsize);
969 for (i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
973 ext2_print_inode(ip);
978 * Initialize the vnode from the inode, check for aliases.
979 * Note that the underlying vnode may have changed.
981 if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
988 * Finish inode initialization.
992 * Set up a generation number for this inode if it does not
993 * already have one. This should only happen on old filesystems.
995 if (ip->i_gen == 0) {
996 ip->i_gen = random() + 1;
997 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
998 ip->i_flag |= IN_MODIFIED;
1005 * File handle to vnode
1007 * Have to be really careful about stale file handles:
1008 * - check that the inode number is valid
1009 * - call ext2_vget() to get the locked inode
1010 * - check for an unallocated inode (i_mode == 0)
1011 * - check that the given client host has export rights and return
1012 * those rights via. exflagsp and credanonp
1015 ext2_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1020 struct m_ext2fs *fs;
1023 ufhp = (struct ufid *)fhp;
1024 fs = VFSTOEXT2(mp)->um_e2fs;
1025 if (ufhp->ufid_ino < EXT2_ROOTINO ||
1026 ufhp->ufid_ino > fs->e2fs_gcount * fs->e2fs->e2fs_ipg)
1029 error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1035 if (ip->i_mode == 0 ||
1036 ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1042 vnode_create_vobject(*vpp, 0, curthread);
1047 * Write a superblock and associated information back to disk.
1050 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1052 struct m_ext2fs *fs = mp->um_e2fs;
1053 struct ext2fs *es = fs->e2fs;
1057 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1058 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2fs));
1059 if (waitfor == MNT_WAIT)
1065 * The buffers for group descriptors, inode bitmaps and block bitmaps
1066 * are not busy at this point and are (hopefully) written by the
1067 * usual sync mechanism. No need to write them here.
1072 ext2_cgupdate(struct ext2mount *mp, int waitfor)
1074 struct m_ext2fs *fs = mp->um_e2fs;
1076 int i, error = 0, allerror = 0;
1078 allerror = ext2_sbupdate(mp, waitfor);
1079 for (i = 0; i < fs->e2fs_gdbcount; i++) {
1080 bp = getblk(mp->um_devvp, fsbtodb(fs,
1081 fs->e2fs->e2fs_first_dblock +
1082 1 /* superblock */ + i), fs->e2fs_bsize, 0, 0, 0);
1083 e2fs_cgsave(&fs->e2fs_gd[
1084 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1085 (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1086 if (waitfor == MNT_WAIT)
1092 if (!allerror && error)
1097 * Return the root of a filesystem.
1100 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1105 error = VFS_VGET(mp, EXT2_ROOTINO, LK_EXCLUSIVE, &nvp);