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
40 * COPYRIGHT.INFO says this has some GPL'd code from ext2_super.c in it
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License as published by
44 * the Free Software Foundation; either version 2 of the License.
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
51 * You should have received a copy of the GNU General Public License
52 * along with this program; if not, write to the Free Software
53 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/namei.h>
62 #include <sys/kernel.h>
63 #include <sys/vnode.h>
64 #include <sys/mount.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h>
71 #include <sys/mutex.h>
73 #include <geom/geom.h>
74 #include <geom/geom_vfs.h>
76 #include <gnu/fs/ext2fs/ext2_mount.h>
77 #include <gnu/fs/ext2fs/inode.h>
79 #include <gnu/fs/ext2fs/fs.h>
80 #include <gnu/fs/ext2fs/ext2_extern.h>
81 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
82 #include <gnu/fs/ext2fs/ext2_fs.h>
84 static int ext2_flushfiles(struct mount *mp, int flags, struct thread *td);
85 static int ext2_mountfs(struct vnode *, struct mount *);
86 static int ext2_reload(struct mount *mp, struct thread *td);
87 static int ext2_sbupdate(struct ext2mount *, int);
89 static vfs_unmount_t ext2_unmount;
90 static vfs_root_t ext2_root;
91 static vfs_statfs_t ext2_statfs;
92 static vfs_sync_t ext2_sync;
93 static vfs_vget_t ext2_vget;
94 static vfs_fhtovp_t ext2_fhtovp;
95 static vfs_mount_t ext2_mount;
97 MALLOC_DEFINE(M_EXT2NODE, "ext2_node", "EXT2 vnode private part");
98 static MALLOC_DEFINE(M_EXT2MNT, "ext2_mount", "EXT2 mount structure");
100 static struct vfsops ext2fs_vfsops = {
101 .vfs_fhtovp = ext2_fhtovp,
102 .vfs_mount = ext2_mount,
103 .vfs_root = ext2_root, /* root inode via vget */
104 .vfs_statfs = ext2_statfs,
105 .vfs_sync = ext2_sync,
106 .vfs_unmount = ext2_unmount,
107 .vfs_vget = ext2_vget,
110 VFS_SET(ext2fs_vfsops, ext2fs, 0);
112 static int ext2_check_sb_compat(struct ext2_super_block *es, struct cdev *dev,
114 static int compute_sb_data(struct vnode * devvp,
115 struct ext2_super_block * es, struct ext2_sb_info * fs);
117 static const char *ext2_opts[] = { "from", "export", "acls", "noexec",
118 "noatime", "union", "suiddir", "multilabel", "nosymfollow",
119 "noclusterr", "noclusterw", "force", NULL };
127 ext2_mount(struct mount *mp)
129 struct vfsoptlist *opts;
132 struct ext2mount *ump = 0;
133 struct ext2_sb_info *fs;
134 struct nameidata nd, *ndp = &nd;
137 int error, flags, len;
140 opts = mp->mnt_optnew;
142 if (vfs_filteropt(opts, ext2_opts))
145 vfs_getopt(opts, "fspath", (void **)&path, NULL);
146 /* Double-check the length of path.. */
147 if (strlen(path) >= MAXMNTLEN - 1)
148 return (ENAMETOOLONG);
151 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
152 if (!error && fspec[len - 1] != '\0')
156 * If updating, check whether changing from read-only to
157 * read/write; if there is no device name, that's all we do.
159 if (mp->mnt_flag & MNT_UPDATE) {
163 if (fs->s_rd_only == 0 &&
164 vfs_flagopt(opts, "ro", NULL, 0)) {
165 error = VFS_SYNC(mp, MNT_WAIT);
169 if (mp->mnt_flag & MNT_FORCE)
171 if (vfs_busy(mp, MBF_NOWAIT))
173 error = ext2_flushfiles(mp, flags, td);
175 if (!error && fs->s_wasvalid) {
176 fs->s_es->s_state |= EXT2_VALID_FS;
177 ext2_sbupdate(ump, MNT_WAIT);
180 vfs_flagopt(opts, "ro", &mp->mnt_flag, MNT_RDONLY);
183 g_access(ump->um_cp, 0, -1, 0);
187 if (!error && (mp->mnt_flag & MNT_RELOAD))
188 error = ext2_reload(mp, td);
191 devvp = ump->um_devvp;
192 if (fs->s_rd_only && !vfs_flagopt(opts, "ro", NULL, 0)) {
193 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev, 0))
197 * If upgrade to read-write by non-root, then verify
198 * that user has necessary permissions on the device.
200 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
201 error = VOP_ACCESS(devvp, VREAD | VWRITE,
204 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
206 VOP_UNLOCK(devvp, 0);
209 VOP_UNLOCK(devvp, 0);
212 error = g_access(ump->um_cp, 0, 1, 0);
218 if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
219 (fs->s_es->s_state & EXT2_ERROR_FS)) {
220 if (mp->mnt_flag & MNT_FORCE) {
222 "WARNING: %s was not properly dismounted\n", fs->fs_fsmnt);
225 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
230 fs->s_es->s_state &= ~EXT2_VALID_FS;
231 ext2_sbupdate(ump, MNT_WAIT);
234 mp->mnt_flag &= ~MNT_RDONLY;
237 if (vfs_flagopt(opts, "export", NULL, 0)) {
238 /* Process export requests in vfs_mount.c. */
244 * Not an update, or updating the name: look up the name
245 * and verify that it refers to a sensible disk device.
249 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
250 if ((error = namei(ndp)) != 0)
252 NDFREE(ndp, NDF_ONLY_PNBUF);
255 if (!vn_isdisk(devvp, &error)) {
261 * If mount by non-root, then verify that user has necessary
262 * permissions on the device.
264 * XXXRW: VOP_ACCESS() enough?
267 if ((mp->mnt_flag & MNT_RDONLY) == 0)
269 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
271 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
277 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
278 error = ext2_mountfs(devvp, mp);
280 if (devvp != ump->um_devvp) {
282 return (EINVAL); /* needs translation */
294 * Note that this strncpy() is ok because of a check at the start
297 strncpy(fs->fs_fsmnt, path, MAXMNTLEN);
298 fs->fs_fsmnt[MAXMNTLEN - 1] = '\0';
299 vfs_mountedfrom(mp, fspec);
304 * Checks that the data in the descriptor blocks make sense
305 * this is taken from ext2/super.c.
308 ext2_check_descriptors(struct ext2_sb_info *sb)
310 struct ext2_group_desc *gdp = NULL;
311 unsigned long block = sb->s_es->s_first_data_block;
315 for (i = 0; i < sb->s_groups_count; i++) {
316 /* examine next descriptor block */
317 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
318 gdp = (struct ext2_group_desc *)
319 sb->s_group_desc[desc_block++]->b_data;
320 if (gdp->bg_block_bitmap < block ||
321 gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) {
322 printf ("ext2_check_descriptors: "
323 "Block bitmap for group %d"
324 " not in group (block %lu)!\n",
325 i, (unsigned long) gdp->bg_block_bitmap);
328 if (gdp->bg_inode_bitmap < block ||
329 gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb)) {
330 printf ("ext2_check_descriptors: "
331 "Inode bitmap for group %d"
332 " not in group (block %lu)!\n",
333 i, (unsigned long) gdp->bg_inode_bitmap);
336 if (gdp->bg_inode_table < block ||
337 gdp->bg_inode_table + sb->s_itb_per_group >=
338 block + EXT2_BLOCKS_PER_GROUP(sb)) {
339 printf ("ext2_check_descriptors: "
340 "Inode table for group %d"
341 " not in group (block %lu)!\n",
342 i, (unsigned long) gdp->bg_inode_table);
345 block += EXT2_BLOCKS_PER_GROUP(sb);
352 ext2_check_sb_compat(struct ext2_super_block *es, struct cdev *dev, int ronly)
355 if (es->s_magic != EXT2_SUPER_MAGIC) {
356 printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
357 devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
360 if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
361 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
363 "WARNING: mount of %s denied due to unsupported optional features\n",
368 (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
369 printf("WARNING: R/W mount of %s denied due to "
370 "unsupported optional features\n", devtoname(dev));
378 * This computes the fields of the ext2_sb_info structure from the
379 * data in the ext2_super_block structure read in.
382 compute_sb_data(struct vnode *devvp, struct ext2_super_block *es,
383 struct ext2_sb_info *fs)
387 int logic_sb_block = 1; /* XXX for now */
389 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
390 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
391 fs->s_fsbtodb = es->s_log_block_size + 1;
392 fs->s_qbmask = fs->s_blocksize - 1;
393 fs->s_blocksize_bits = es->s_log_block_size + 10;
394 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
396 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
397 fs->s_blocks_per_group = es->s_blocks_per_group;
398 fs->s_frags_per_group = es->s_frags_per_group;
399 fs->s_inodes_per_group = es->s_inodes_per_group;
400 if (es->s_rev_level == EXT2_GOOD_OLD_REV) {
401 fs->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
402 fs->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
404 fs->s_first_ino = es->s_first_ino;
405 fs->s_inode_size = es->s_inode_size;
408 * Simple sanity check for superblock inode size value.
410 if (fs->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
411 fs->s_inode_size > fs->s_blocksize ||
412 (fs->s_inode_size & (fs->s_inode_size - 1)) != 0) {
413 printf("EXT2-fs: invalid inode size %d\n",
418 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE(fs);
419 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
420 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
421 /* s_resuid / s_resgid ? */
422 fs->s_groups_count = (es->s_blocks_count - es->s_first_data_block +
423 EXT2_BLOCKS_PER_GROUP(fs) - 1) / EXT2_BLOCKS_PER_GROUP(fs);
424 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
425 EXT2_DESC_PER_BLOCK(fs);
426 fs->s_gdb_count = db_count;
427 fs->s_group_desc = malloc(db_count * sizeof (struct buf *),
428 M_EXT2MNT, M_WAITOK);
431 * Adjust logic_sb_block.
432 * Godmar thinks: if the blocksize is greater than 1024, then
433 * the superblock is logically part of block zero.
435 if(fs->s_blocksize > SBSIZE)
438 for (i = 0; i < db_count; i++) {
439 error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
440 fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
442 for (j = 0; j < i; j++)
443 brelse(fs->s_group_desc[j]);
444 free(fs->s_group_desc, M_EXT2MNT);
445 printf("EXT2-fs: unable to read group descriptors"
449 LCK_BUF(fs->s_group_desc[i])
451 if(!ext2_check_descriptors(fs)) {
452 for (j = 0; j < db_count; j++)
453 ULCK_BUF(fs->s_group_desc[j])
454 free(fs->s_group_desc, M_EXT2MNT);
455 printf("EXT2-fs: (ext2_check_descriptors failure) "
456 "unable to read group descriptors\n");
459 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
460 fs->s_inode_bitmap_number[i] = 0;
461 fs->s_inode_bitmap[i] = NULL;
462 fs->s_block_bitmap_number[i] = 0;
463 fs->s_block_bitmap[i] = NULL;
465 fs->s_loaded_inode_bitmaps = 0;
466 fs->s_loaded_block_bitmaps = 0;
467 if (es->s_rev_level == EXT2_GOOD_OLD_REV ||
468 (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_LARGE_FILE) == 0)
469 fs->fs_maxfilesize = 0x7fffffff;
471 fs->fs_maxfilesize = 0x7fffffffffffffff;
476 * Reload all incore data for a filesystem (used after running fsck on
477 * the root filesystem and finding things to fix). The filesystem must
478 * be mounted read-only.
480 * Things to do to update the mount:
481 * 1) invalidate all cached meta-data.
482 * 2) re-read superblock from disk.
483 * 3) re-read summary information from disk.
484 * 4) invalidate all inactive vnodes.
485 * 5) invalidate all cached file data.
486 * 6) re-read inode data for all active vnodes.
489 ext2_reload(struct mount *mp, struct thread *td)
491 struct vnode *vp, *mvp, *devvp;
494 struct ext2_super_block *es;
495 struct ext2_sb_info *fs;
498 if ((mp->mnt_flag & MNT_RDONLY) == 0)
501 * Step 1: invalidate all cached meta-data.
503 devvp = VFSTOEXT2(mp)->um_devvp;
504 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
505 if (vinvalbuf(devvp, 0, 0, 0) != 0)
506 panic("ext2_reload: dirty1");
507 VOP_UNLOCK(devvp, 0);
510 * Step 2: re-read superblock from disk.
511 * constants have been adjusted for ext2
513 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
515 es = (struct ext2_super_block *)bp->b_data;
516 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
518 return (EIO); /* XXX needs translation */
520 fs = VFSTOEXT2(mp)->um_e2fs;
521 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
523 if((error = compute_sb_data(devvp, es, fs)) != 0) {
528 if (fs->fs_sbsize < SBSIZE)
529 bp->b_flags |= B_INVAL;
535 MNT_VNODE_FOREACH(vp, mp, mvp) {
537 if (vp->v_iflag & VI_DOOMED) {
543 * Step 4: invalidate all cached file data.
545 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
546 MNT_VNODE_FOREACH_ABORT(mp, mvp);
549 if (vinvalbuf(vp, 0, 0, 0))
550 panic("ext2_reload: dirty2");
553 * Step 5: re-read inode data for all active vnodes.
556 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
557 (int)fs->s_blocksize, NOCRED, &bp);
561 MNT_VNODE_FOREACH_ABORT(mp, mvp);
564 ext2_ei2i((struct ext2_inode *) ((char *)bp->b_data +
565 EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)), ip);
576 * Common code for mount and mountroot.
579 ext2_mountfs(struct vnode *devvp, struct mount *mp)
581 struct ext2mount *ump;
583 struct ext2_sb_info *fs;
584 struct ext2_super_block *es;
585 struct cdev *dev = devvp->v_rdev;
586 struct g_consumer *cp;
591 ronly = vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0);
592 /* XXX: use VOP_ACESS to check FS perms */
595 error = g_vfs_open(devvp, &cp, "ext2fs", ronly ? 0 : 1);
598 VOP_UNLOCK(devvp, 0);
602 /* XXX: should we check for some sectorsize or 512 instead? */
603 if (((SBSIZE % cp->provider->sectorsize) != 0) ||
604 (SBSIZE < cp->provider->sectorsize)) {
613 bo = &devvp->v_bufobj;
615 bo->bo_ops = g_vfs_bufops;
616 if (devvp->v_rdev->si_iosize_max != 0)
617 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
618 if (mp->mnt_iosize_max > MAXPHYS)
619 mp->mnt_iosize_max = MAXPHYS;
623 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
625 es = (struct ext2_super_block *)bp->b_data;
626 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
627 error = EINVAL; /* XXX needs translation */
630 if ((es->s_state & EXT2_VALID_FS) == 0 ||
631 (es->s_state & EXT2_ERROR_FS)) {
632 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
634 "WARNING: Filesystem was not properly dismounted\n");
637 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
642 ump = malloc(sizeof *ump, M_EXT2MNT, M_WAITOK);
643 bzero((caddr_t)ump, sizeof *ump);
646 * I don't know whether this is the right strategy. Note that
647 * we dynamically allocate both an ext2_sb_info and an ext2_super_block
648 * while Linux keeps the super block in a locked buffer.
650 ump->um_e2fs = malloc(sizeof(struct ext2_sb_info),
651 M_EXT2MNT, M_WAITOK);
652 ump->um_e2fs->s_es = malloc(sizeof(struct ext2_super_block),
653 M_EXT2MNT, M_WAITOK);
654 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
655 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
659 * We don't free the group descriptors allocated by compute_sb_data()
660 * until ext2_unmount(). This is OK since the mount will succeed.
665 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
668 * If the fs is not mounted read-only, make sure the super block is
669 * always written back on a sync().
671 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
673 fs->s_dirt = 1; /* mark it modified */
674 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
677 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
678 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
679 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
681 mp->mnt_flag |= MNT_LOCAL;
685 ump->um_devvp = devvp;
686 ump->um_bo = &devvp->v_bufobj;
690 * Setting those two parameters allowed us to use
691 * ufs_bmap w/o changse!
693 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
694 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
695 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
697 ext2_sbupdate(ump, MNT_WAIT);
710 free(ump->um_e2fs->s_es, M_EXT2MNT);
711 free(ump->um_e2fs, M_EXT2MNT);
712 free(ump, M_EXT2MNT);
719 * Unmount system call.
722 ext2_unmount(struct mount *mp, int mntflags)
724 struct ext2mount *ump;
725 struct ext2_sb_info *fs;
726 int error, flags, ronly, i;
729 if (mntflags & MNT_FORCE) {
730 if (mp->mnt_flag & MNT_ROOTFS)
734 if ((error = ext2_flushfiles(mp, flags, curthread)) != 0)
738 ronly = fs->s_rd_only;
741 fs->s_es->s_state |= EXT2_VALID_FS;
742 ext2_sbupdate(ump, MNT_WAIT);
745 /* release buffers containing group descriptors */
746 for(i = 0; i < fs->s_gdb_count; i++)
747 ULCK_BUF(fs->s_group_desc[i])
748 free(fs->s_group_desc, M_EXT2MNT);
750 /* release cached inode/block bitmaps */
751 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
752 if (fs->s_inode_bitmap[i])
753 ULCK_BUF(fs->s_inode_bitmap[i])
754 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
755 if (fs->s_block_bitmap[i])
756 ULCK_BUF(fs->s_block_bitmap[i])
760 g_vfs_close(ump->um_cp);
763 vrele(ump->um_devvp);
764 free(fs->s_es, M_EXT2MNT);
766 free(ump, M_EXT2MNT);
769 mp->mnt_flag &= ~MNT_LOCAL;
775 * Flush out all the files in a filesystem.
778 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
782 error = vflush(mp, 0, flags, td);
787 * Get file system statistics.
788 * taken from ext2/super.c ext2_statfs.
791 ext2_statfs(struct mount *mp, struct statfs *sbp)
793 struct ext2mount *ump;
794 struct ext2_sb_info *fs;
795 struct ext2_super_block *es;
796 unsigned long overhead;
803 if (es->s_magic != EXT2_SUPER_MAGIC)
804 panic("ext2_statfs - magic number spoiled");
807 * Compute the overhead (FS structures)
809 if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
811 for (i = 0 ; i < fs->s_groups_count; i++)
812 if (ext2_group_sparse(i))
815 nsb = fs->s_groups_count;
816 overhead = es->s_first_data_block +
817 /* Superblocks and block group descriptors: */
818 nsb * (1 + fs->s_gdb_count) +
819 /* Inode bitmap, block bitmap, and inode table: */
820 fs->s_groups_count * (1 + 1 + fs->s_itb_per_group);
822 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
823 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
824 sbp->f_blocks = es->s_blocks_count - overhead;
825 sbp->f_bfree = es->s_free_blocks_count;
826 sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
827 sbp->f_files = es->s_inodes_count;
828 sbp->f_ffree = es->s_free_inodes_count;
833 * Go through the disk queues to initiate sandbagged IO;
834 * go through the inodes to write those that have been modified;
835 * initiate the writing of the super block if it has been modified.
837 * Note: we are always called with the filesystem marked `MPBUSY'.
840 ext2_sync(struct mount *mp, int waitfor)
842 struct vnode *mvp, *vp;
845 struct ext2mount *ump = VFSTOEXT2(mp);
846 struct ext2_sb_info *fs;
847 int error, allerror = 0;
851 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */
852 printf("fs = %s\n", fs->fs_fsmnt);
853 panic("ext2_sync: rofs mod");
857 * Write back each (modified) inode.
861 MNT_VNODE_FOREACH(vp, mp, mvp) {
863 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
870 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
871 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
872 waitfor == MNT_LAZY)) {
877 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
880 if (error == ENOENT) {
881 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
886 if ((error = VOP_FSYNC(vp, waitfor, td)) != 0)
895 * Force stale file system control information to be flushed.
897 if (waitfor != MNT_LAZY) {
898 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
899 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
901 VOP_UNLOCK(ump->um_devvp, 0);
905 * Write back modified superblock.
907 if (fs->s_dirt != 0) {
909 fs->s_es->s_wtime = time_second;
910 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
917 * Look up an EXT2FS dinode number to find its incore vnode, otherwise read it
918 * in from disk. If it is in core, wait for the lock bit to clear, then
919 * return the inode locked. Detection and handling of mount points must be
920 * done by the calling routine.
923 ext2_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
925 struct ext2_sb_info *fs;
927 struct ext2mount *ump;
936 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
937 if (error || *vpp != NULL)
944 * If this malloc() is performed after the getnewvnode()
945 * it might block, leaving a vnode with a NULL v_data to be
946 * found by ext2_sync() if a sync happens to fire right then,
947 * which will cause a panic because ext2_sync() blindly
948 * dereferences vp->v_data (as well it should).
950 ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
952 /* Allocate a new vnode/inode. */
953 if ((error = getnewvnode("ext2fs", mp, &ext2_vnodeops, &vp)) != 0) {
955 free(ip, M_EXT2NODE);
960 ip->i_e2fs = fs = ump->um_e2fs;
963 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
964 error = insmntque(vp, mp);
966 free(ip, M_EXT2NODE);
970 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
971 if (error || *vpp != NULL)
974 /* Read in the disk contents for the inode, copy into the inode. */
975 if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
976 (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
978 * The inode does not contain anything useful, so it would
979 * be misleading to leave it on its hash chain. With mode
980 * still zero, it will be unlinked and returned to the free
988 /* convert ext2 inode to dinode */
989 ext2_ei2i((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
990 ino_to_fsbo(fs, ino)), ip);
991 ip->i_block_group = ino_to_cg(fs, ino);
992 ip->i_next_alloc_block = 0;
993 ip->i_next_alloc_goal = 0;
994 ip->i_prealloc_count = 0;
995 ip->i_prealloc_block = 0;
998 * Now we want to make sure that block pointers for unused
999 * blocks are zeroed out - ext2_balloc depends on this
1000 * although for regular files and directories only
1002 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1003 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1004 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1008 ext2_print_inode(ip);
1013 * Initialize the vnode from the inode, check for aliases.
1014 * Note that the underlying vnode may have changed.
1016 if ((error = ext2_vinit(mp, &ext2_fifoops, &vp)) != 0) {
1023 * Finish inode initialization now that aliasing has been resolved.
1025 ip->i_devvp = ump->um_devvp;
1028 * Set up a generation number for this inode if it does not
1029 * already have one. This should only happen on old filesystems.
1031 if (ip->i_gen == 0) {
1032 ip->i_gen = random() / 2 + 1;
1033 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1034 ip->i_flag |= IN_MODIFIED;
1041 * File handle to vnode
1043 * Have to be really careful about stale file handles:
1044 * - check that the inode number is valid
1045 * - call ext2_vget() to get the locked inode
1046 * - check for an unallocated inode (i_mode == 0)
1047 * - check that the given client host has export rights and return
1048 * those rights via. exflagsp and credanonp
1051 ext2_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1056 struct ext2_sb_info *fs;
1059 ufhp = (struct ufid *)fhp;
1060 fs = VFSTOEXT2(mp)->um_e2fs;
1061 if (ufhp->ufid_ino < ROOTINO ||
1062 ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1065 error = VFS_VGET(mp, ufhp->ufid_ino, LK_EXCLUSIVE, &nvp);
1071 if (ip->i_mode == 0 ||
1072 ip->i_gen != ufhp->ufid_gen || ip->i_nlink <= 0) {
1078 vnode_create_vobject(*vpp, 0, curthread);
1083 * Write a superblock and associated information back to disk.
1086 ext2_sbupdate(struct ext2mount *mp, int waitfor)
1088 struct ext2_sb_info *fs = mp->um_e2fs;
1089 struct ext2_super_block *es = fs->s_es;
1093 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0, 0);
1094 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1095 if (waitfor == MNT_WAIT)
1101 * The buffers for group descriptors, inode bitmaps and block bitmaps
1102 * are not busy at this point and are (hopefully) written by the
1103 * usual sync mechanism. No need to write them here.
1109 * Return the root of a filesystem.
1112 ext2_root(struct mount *mp, int flags, struct vnode **vpp)
1117 error = VFS_VGET(mp, (ino_t)ROOTINO, LK_EXCLUSIVE, &nvp);