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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)ffs_vfsops.c 8.8 (Berkeley) 4/18/94
40 * $FreeBSD: src/sys/gnu/ext2fs/ext2_vfsops.c,v 1.63.2.7 2002/07/01 00:18:51 iedowse Exp $
43 #include "opt_quota.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/nlookup.h>
50 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
55 #include <sys/fcntl.h>
56 #include <sys/diskslice.h>
57 #include <sys/malloc.h>
59 #include <vm/vm_zone.h>
62 #include <sys/thread2.h>
67 #include "ext2_mount.h"
68 #include "ext2_extern.h"
72 #include "ext2_fs_sb.h"
74 extern struct vop_ops ext2_vnode_vops;
75 extern struct vop_ops ext2_spec_vops;
76 extern struct vop_ops ext2_fifo_vops;
78 static int ext2_fhtovp (struct mount *, struct vnode *,
79 struct fid *, struct vnode **);
80 static int ext2_flushfiles (struct mount *mp, int flags);
81 static int ext2_mount (struct mount *, char *, caddr_t, struct ucred *);
82 static int ext2_mountfs (struct vnode *, struct mount *, struct ucred *);
83 static int ext2_root(struct mount *, struct vnode **);
84 static int ext2_reload (struct mount *mountp, struct ucred *cred);
85 static int ext2_sbupdate (struct ext2_mount *, int);
86 static int ext2_sync (struct mount *, int);
87 static int ext2_unmount (struct mount *, int);
88 static int ext2_vget (struct mount *, struct vnode *, ino_t, struct vnode **);
89 static int ext2_init(struct vfsconf *);
90 static int ext2_vptofh (struct vnode *, struct fid *);
92 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
93 MALLOC_DEFINE(M_EXT2MNT, "EXT2 mount", "EXT2 mount structure");
95 static struct vfsops ext2fs_vfsops = {
96 .vfs_mount = ext2_mount,
97 .vfs_unmount = ext2_unmount,
98 .vfs_root = ext2_root, /* root inode via vget */
99 .vfs_quotactl = ext2_quotactl, /* quota operations */
100 .vfs_statfs = ext2_statfs,
101 .vfs_sync = ext2_sync,
102 .vfs_vget = ext2_vget,
103 .vfs_fhtovp = ext2_fhtovp,
104 .vfs_checkexp = ext2_check_export,
105 .vfs_vptofh = ext2_vptofh,
106 .vfs_init = ext2_init,
107 .vfs_uninit = ext2_uninit
110 VFS_SET(ext2fs_vfsops, ext2fs, 0);
111 MODULE_VERSION(ext2fs, 1);
113 static int ext2fs_inode_hash_lock;
115 static int ext2_check_sb_compat (struct ext2_super_block *es,
116 cdev_t dev, int ronly);
117 static int compute_sb_data (struct vnode *devvp,
118 struct ext2_super_block *es,
119 struct ext2_sb_info *fs);
122 ext2_root(struct mount *mp, struct vnode **vpp)
127 error = VFS_VGET(mp, NULL, (ino_t)EXT2_ROOTINO, &nvp);
135 * Do operations associated with quotas
138 ext2_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
144 int cmd, type, error;
146 type = cmds & SUBCMDMASK;
147 cmd = cmds >> SUBCMDSHIFT;
168 error = priv_check_cred(cred, PRIV_UFS_QUOTAON, 0);
172 error = priv_check_cred(cred, PRIV_UFS_QUOTAOFF, 0);
176 error = priv_check_cred(cred, PRIV_VFS_SETQUOTA, 0);
180 error = priv_check_cred(cred, PRIV_UFS_SETUSE, 0);
184 if (uid == cred->cr_ruid)
187 error = priv_check_cred(cred, PRIV_VFS_GETQUOTA, 0);
203 if ((uint)type >= MAXQUOTAS)
205 if (vfs_busy(mp, LK_NOWAIT))
211 error = ext2_quotaon(cred, mp, type, arg);
215 error = ext2_quotaoff(mp, type);
219 error = ext2_setquota(mp, uid, type, arg);
223 error = ext2_setuse(mp, uid, type, arg);
227 error = ext2_getquota(mp, uid, type, arg);
231 error = ext2_qsync(mp);
244 * Initial UFS filesystems, done only once.
247 ext2_init(struct vfsconf *vfsp)
267 * data: this is actually a (struct ext2_args *)
270 ext2_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
273 struct ext2_args args;
274 struct ext2_mount *ump = NULL;
275 struct ext2_sb_info *fs;
279 struct nlookupdata nd;
281 if ((error = copyin(data, (caddr_t)&args, sizeof (struct ext2_args))) != 0)
285 * If updating, check whether changing from read-only to
286 * read/write; if there is no device name, that's all we do.
288 if (mp->mnt_flag & MNT_UPDATE) {
291 devvp = ump->um_devvp;
293 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
295 if (mp->mnt_flag & MNT_FORCE)
297 if (vfs_busy(mp, LK_NOWAIT))
299 error = ext2_flushfiles(mp, flags);
301 if (!error && fs->s_wasvalid) {
302 fs->s_es->s_state |= EXT2_VALID_FS;
303 ext2_sbupdate(ump, MNT_WAIT);
306 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
307 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
308 VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
311 if (!error && (mp->mnt_flag & MNT_RELOAD))
312 error = ext2_reload(mp, cred);
315 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
316 (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
318 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
320 * If upgrade to read-write by non-root, then verify
321 * that user has necessary permissions on the device.
323 if (cred->cr_uid != 0) {
324 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
325 error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
333 if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
334 (fs->s_es->s_state & EXT2_ERROR_FS)) {
335 if (mp->mnt_flag & MNT_FORCE) {
337 "WARNING: %s was not properly dismounted\n",
341 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
346 fs->s_es->s_state &= ~EXT2_VALID_FS;
347 ext2_sbupdate(ump, MNT_WAIT);
349 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
350 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
351 VOP_CLOSE(devvp, FREAD, NULL);
354 if (args.fspec == NULL) {
356 * Process export requests.
358 return (vfs_export(mp, &ump->um_export, &args.export));
362 * Not an update, or updating the name: look up the name
363 * and verify that it refers to a sensible block device.
366 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
368 error = nlookup(&nd);
370 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
375 if (!vn_isdisk(devvp, &error)) {
381 * If mount by non-root, then verify that user has necessary
382 * permissions on the device.
384 if (cred->cr_uid != 0) {
386 if ((mp->mnt_flag & MNT_RDONLY) == 0)
387 accessmode |= VWRITE;
388 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
389 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
396 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
397 error = ext2_mountfs(devvp, mp, cred);
399 if (devvp != ump->um_devvp)
400 error = EINVAL; /* needs translation */
410 copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
411 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
412 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
413 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
414 ext2_statfs(mp, &mp->mnt_stat, cred);
419 ext2_check_sb_compat(struct ext2_super_block *es, cdev_t dev, int ronly)
421 if (es->s_magic != EXT2_SUPER_MAGIC) {
422 kprintf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
423 devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
426 if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
427 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
429 "WARNING: mount of %s denied due to unsupported optional features\n",
434 (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
436 "WARNING: R/W mount of %s denied due to unsupported optional features\n",
445 * this computes the fields of the ext2_sb_info structure from the
446 * data in the ext2_super_block structure read in
449 compute_sb_data(struct vnode *devvp, struct ext2_super_block *es,
450 struct ext2_sb_info *fs)
454 int logic_sb_block = 1; /* XXX for now */
459 #define V(v) kprintf(#v"= %d\n", fs->v);
462 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
464 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
466 fs->s_fsbtodb = es->s_log_block_size + 1;
468 fs->s_qbmask = fs->s_blocksize - 1;
470 fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
472 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
475 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
477 fs->s_blocks_per_group = es->s_blocks_per_group;
478 V(s_blocks_per_group)
479 fs->s_frags_per_group = es->s_frags_per_group;
481 fs->s_inodes_per_group = es->s_inodes_per_group;
482 V(s_inodes_per_group)
483 if (es->s_rev_level == EXT2_GOOD_OLD_REV) {
484 fs->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
485 fs->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
487 fs->s_first_ino = es->s_first_ino;
488 fs->s_inode_size = es->s_inode_size;
490 * Simple sanity check for superblock inode size value.
492 if (fs->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
493 fs->s_inode_size > fs->s_blocksize ||
494 (fs->s_inode_size & (fs->s_inode_size - 1)) != 0) {
495 kprintf("EXT2-fs: invalid inode size %d\n",
502 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE(fs);
503 V(s_inodes_per_block)
504 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
506 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
508 /* s_resuid / s_resgid ? */
509 fs->s_groups_count = (es->s_blocks_count - es->s_first_data_block +
510 EXT2_BLOCKS_PER_GROUP(fs) - 1) /
511 EXT2_BLOCKS_PER_GROUP(fs);
513 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
514 EXT2_DESC_PER_BLOCK(fs);
515 fs->s_db_per_group = db_count;
518 fs->s_group_desc = kmalloc(db_count * sizeof (struct buf *),
519 M_EXT2MNT, M_WAITOK);
521 /* adjust logic_sb_block */
522 if (fs->s_blocksize > SBSIZE)
524 * Godmar thinks: if the blocksize is greater than 1024,
525 * then, the superblock is logically part of block zero.
529 for (i = 0; i < db_count; i++) {
530 error = bread(devvp, fsbtodoff(fs, logic_sb_block + i + 1),
531 fs->s_blocksize, &fs->s_group_desc[i]);
533 for (j = 0; j < i; j++)
534 brelse(fs->s_group_desc[j]);
535 kfree(fs->s_group_desc, M_EXT2MNT);
536 kprintf("EXT2-fs: unable to read group descriptors (%d)\n", error);
539 /* Set the B_LOCKED flag on the buffer, then brelse() it */
540 LCK_BUF(fs->s_group_desc[i])
542 if (!ext2_check_descriptors(fs)) {
543 for (j = 0; j < db_count; j++)
544 ULCK_BUF(fs->s_group_desc[j])
545 kfree(fs->s_group_desc, M_EXT2MNT);
546 kprintf("EXT2-fs: (ext2_check_descriptors failure) "
547 "unable to read group descriptors\n");
551 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
552 fs->s_inode_bitmap_number[i] = 0;
553 fs->s_inode_bitmap[i] = NULL;
554 fs->s_block_bitmap_number[i] = 0;
555 fs->s_block_bitmap[i] = NULL;
557 fs->s_loaded_inode_bitmaps = 0;
558 fs->s_loaded_block_bitmaps = 0;
563 * Reload all incore data for a filesystem (used after running fsck on
564 * the root filesystem and finding things to fix). The filesystem must
565 * be mounted read-only.
567 * Things to do to update the mount:
568 * 1) invalidate all cached meta-data.
569 * 2) re-read superblock from disk.
570 * 3) re-read summary information from disk.
571 * 4) invalidate all inactive vnodes.
572 * 5) invalidate all cached file data.
573 * 6) re-read inode data for all active vnodes.
575 static int ext2_reload_scan(struct mount *mp, struct vnode *vp, void *rescan);
582 struct ext2_sb_info *fs;
586 ext2_reload(struct mount *mountp, struct ucred *cred)
590 struct ext2_super_block *es;
591 struct ext2_sb_info *fs;
593 struct scaninfo scaninfo;
595 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
598 * Step 1: invalidate all cached meta-data.
600 devvp = VFSTOEXT2(mountp)->um_devvp;
601 if (vinvalbuf(devvp, 0, 0, 0))
602 panic("ext2_reload: dirty1");
604 * Step 2: re-read superblock from disk.
605 * constants have been adjusted for ext2
607 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
609 es = (struct ext2_super_block *)bp->b_data;
610 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
612 return (EIO); /* XXX needs translation */
614 fs = VFSTOEXT2(mountp)->um_e2fs;
615 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
617 if((error = compute_sb_data(devvp, es, fs)) != 0) {
622 if (fs->fs_sbsize < SBSIZE)
623 bp->b_flags |= B_INVAL;
628 scaninfo.devvp = devvp;
630 while (error == 0 && scaninfo.rescan) {
632 error = vmntvnodescan(mountp, VMSC_GETVX,
633 NULL, ext2_reload_scan, &scaninfo);
639 ext2_reload_scan(struct mount *mp, struct vnode *vp, void *data)
641 struct scaninfo *info = data;
653 * Step 1: invalidate all cached file data.
655 if (vinvalbuf(vp, 0, 0, 0))
656 panic("ext2_reload: dirty2");
658 * Step 2: re-read inode data for all active vnodes.
661 error = bread(info->devvp,
662 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
663 (int)info->fs->s_blocksize, &bp);
666 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
667 EXT2_INODE_SIZE(info->fs) * ino_to_fsbo(info->fs, ip->i_number)),
674 * Common code for mount and mountroot
677 ext2_mountfs(struct vnode *devvp, struct mount *mp, struct ucred *cred)
679 struct ext2_mount *ump;
681 struct ext2_sb_info *fs;
682 struct ext2_super_block *es;
684 struct partinfo dpart;
689 * Disallow multiple mounts of the same device.
690 * Disallow mounting of a device that is currently in use
691 * (except for root, which might share swap device for miniroot).
692 * Flush out any old buffers remaining from a previous use.
694 if ((error = vfs_mountedon(devvp)) != 0)
696 if (vcount(devvp) > 0)
698 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
701 /* turn on this to force it to be read-only */
702 mp->mnt_flag |= MNT_RDONLY;
705 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
706 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
707 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
712 if (dev->si_iosize_max != 0)
713 mp->mnt_iosize_max = dev->si_iosize_max;
714 if (mp->mnt_iosize_max > MAXPHYS)
715 mp->mnt_iosize_max = MAXPHYS;
716 VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, NULL);
720 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
722 es = (struct ext2_super_block *)bp->b_data;
723 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
724 error = EINVAL; /* XXX needs translation */
727 if ((es->s_state & EXT2_VALID_FS) == 0 ||
728 (es->s_state & EXT2_ERROR_FS)) {
729 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
731 "WARNING: Filesystem was not properly dismounted\n");
734 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
739 ump = kmalloc(sizeof *ump, M_EXT2MNT, M_WAITOK | M_ZERO);
740 ump->um_malloctype = M_EXT2NODE;
741 ump->um_blkatoff = ext2_blkatoff;
742 ump->um_truncate = ext2_truncate;
743 ump->um_update = ext2_update;
744 ump->um_valloc = ext2_valloc;
745 ump->um_vfree = ext2_vfree;
746 /* I don't know whether this is the right strategy. Note that
747 we dynamically allocate both a ext2_sb_info and a ext2_super_block
748 while Linux keeps the super block in a locked buffer
750 ump->um_e2fs = kmalloc(sizeof(struct ext2_sb_info),
751 M_EXT2MNT, M_WAITOK);
752 ump->um_e2fs->s_es = kmalloc(sizeof(struct ext2_super_block),
753 M_EXT2MNT, M_WAITOK);
754 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
755 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
758 * We don't free the group descriptors allocated by compute_sb_data()
759 * until ext2_unmount(). This is OK since the mount will succeed.
764 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
765 /* if the fs is not mounted read-only, make sure the super block is
766 always written back on a sync()
768 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
770 fs->s_dirt = 1; /* mark it modified */
771 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
773 mp->mnt_data = (qaddr_t)ump;
774 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
775 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
776 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
777 mp->mnt_flag |= MNT_LOCAL;
780 ump->um_devvp = devvp;
781 /* setting those two parameters allows us to use
782 ext2_bmap w/o changse !
784 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
785 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
786 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
787 for (i = 0; i < MAXQUOTAS; i++)
788 ump->um_quotas[i] = NULLVP;
789 dev->si_mountpoint = mp;
791 vfs_add_vnodeops(mp, &ext2_vnode_vops, &mp->mnt_vn_norm_ops);
792 vfs_add_vnodeops(mp, &ext2_spec_vops, &mp->mnt_vn_spec_ops);
793 vfs_add_vnodeops(mp, &ext2_fifo_vops, &mp->mnt_vn_fifo_ops);
796 ext2_sbupdate(ump, MNT_WAIT);
801 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
802 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
805 kfree(ump->um_e2fs->s_es, M_EXT2MNT);
806 kfree(ump->um_e2fs, M_EXT2MNT);
807 kfree(ump, M_EXT2MNT);
808 mp->mnt_data = (qaddr_t)0;
814 * unmount system call
817 ext2_unmount(struct mount *mp, int mntflags)
819 struct ext2_mount *ump;
820 struct ext2_sb_info *fs;
821 int error, flags, ronly, i;
824 if (mntflags & MNT_FORCE) {
825 if (mp->mnt_flag & MNT_ROOTFS)
829 if ((error = ext2_flushfiles(mp, flags)) != 0)
833 ronly = fs->s_rd_only;
836 fs->s_es->s_state |= EXT2_VALID_FS;
837 ext2_sbupdate(ump, MNT_WAIT);
840 /* release buffers containing group descriptors */
841 for(i = 0; i < fs->s_db_per_group; i++)
842 ULCK_BUF(fs->s_group_desc[i])
843 kfree(fs->s_group_desc, M_EXT2MNT);
845 /* release cached inode/block bitmaps */
846 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
847 if (fs->s_inode_bitmap[i])
848 ULCK_BUF(fs->s_inode_bitmap[i])
850 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
851 if (fs->s_block_bitmap[i])
852 ULCK_BUF(fs->s_block_bitmap[i])
854 ump->um_devvp->v_rdev->si_mountpoint = NULL;
856 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
857 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
858 vn_unlock(ump->um_devvp);
860 vrele(ump->um_devvp);
861 kfree(fs->s_es, M_EXT2MNT);
862 kfree(fs, M_EXT2MNT);
863 kfree(ump, M_EXT2MNT);
864 mp->mnt_data = (qaddr_t)0;
865 mp->mnt_flag &= ~MNT_LOCAL;
870 * Flush out all the files in a filesystem.
873 ext2_flushfiles(struct mount *mp, int flags)
875 struct ext2_mount *ump;
883 if (mp->mnt_flag & MNT_QUOTA) {
884 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
886 for (i = 0; i < MAXQUOTAS; i++) {
887 if (ump->um_quotas[i] == NULLVP)
889 ext2_quotaoff(mp, i);
892 * Here we fall through to vflush again to ensure
893 * that we have gotten rid of all the system vnodes.
897 error = vflush(mp, 0, flags);
902 * Go through the disk queues to initiate sandbagged IO;
903 * go through the inodes to write those that have been modified;
904 * initiate the writing of the super block if it has been modified.
906 * Note: we are always called with the filesystem marked `MPBUSY'.
909 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
912 ext2_sync(struct mount *mp, int waitfor)
914 struct ext2_mount *ump = VFSTOEXT2(mp);
915 struct ext2_sb_info *fs;
916 struct scaninfo scaninfo;
920 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */
921 kprintf("fs = %s\n", fs->fs_fsmnt);
922 panic("ext2_sync: rofs mod");
926 * Write back each (modified) inode.
928 scaninfo.allerror = 0;
930 scaninfo.waitfor = waitfor;
931 while (scaninfo.rescan) {
933 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
934 NULL, ext2_sync_scan, &scaninfo);
938 * Force stale file system control information to be flushed.
940 if ((waitfor & MNT_LAZY) == 0) {
941 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
942 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
943 scaninfo.allerror = error;
944 vn_unlock(ump->um_devvp);
950 * Write back modified superblock.
952 if (fs->s_dirt != 0) {
954 fs->s_es->s_wtime = time_second;
955 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
956 scaninfo.allerror = error;
958 return (scaninfo.allerror);
962 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
964 struct scaninfo *info = data;
969 if (vp->v_type == VNON ||
971 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
972 (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
975 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
976 info->allerror = error;
981 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
982 * in from disk. If it is in core, wait for the lock bit to clear, then
983 * return the inode locked. Detection and handling of mount points must be
984 * done by the calling routine.
987 ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
989 struct ext2_sb_info *fs;
991 struct ext2_mount *ump;
1001 if ((*vpp = ext2_ihashget(dev, ino)) != NULL)
1005 * Lock out the creation of new entries in the FFS hash table in
1006 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1009 if (ext2fs_inode_hash_lock) {
1010 while (ext2fs_inode_hash_lock) {
1011 ext2fs_inode_hash_lock = -1;
1012 tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1016 ext2fs_inode_hash_lock = 1;
1019 * If this MALLOC() is performed after the getnewvnode()
1020 * it might block, leaving a vnode with a NULL v_data to be
1021 * found by ext2_sync() if a sync happens to fire right then,
1022 * which will cause a panic because ext2_sync() blindly
1023 * dereferences vp->v_data (as well it should).
1025 ip = kmalloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1027 /* Allocate a new vnode/inode. */
1028 if ((error = getnewvnode(VT_EXT2FS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
1029 if (ext2fs_inode_hash_lock < 0)
1030 wakeup(&ext2fs_inode_hash_lock);
1031 ext2fs_inode_hash_lock = 0;
1033 kfree(ip, M_EXT2NODE);
1036 bzero((caddr_t)ip, sizeof(struct inode));
1039 ip->i_e2fs = fs = ump->um_e2fs;
1043 for (i = 0; i < MAXQUOTAS; i++)
1044 ip->i_dquot[i] = NODQUOT;
1047 * Put it onto its hash chain. Since our vnode is locked, other
1048 * requests for this inode will block if they arrive while we are
1049 * sleeping waiting for old data structures to be purged or for the
1050 * contents of the disk portion of this inode to be read.
1054 if (ext2fs_inode_hash_lock < 0)
1055 wakeup(&ext2fs_inode_hash_lock);
1056 ext2fs_inode_hash_lock = 0;
1058 /* Read in the disk contents for the inode, copy into the inode. */
1060 kprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1062 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1063 (int)fs->s_blocksize, &bp);
1066 * The inode does not contain anything useful, so it would
1067 * be misleading to leave it on its hash chain. With mode
1068 * still zero, it will be unlinked and returned to the free
1076 /* convert ext2 inode to dinode */
1077 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
1078 ino_to_fsbo(fs, ino)), &ip->i_din);
1079 ip->i_block_group = ino_to_cg(fs, ino);
1080 ip->i_next_alloc_block = 0;
1081 ip->i_next_alloc_goal = 0;
1082 ip->i_prealloc_count = 0;
1083 ip->i_prealloc_block = 0;
1084 /* now we want to make sure that block pointers for unused
1085 blocks are zeroed out - ext2_balloc depends on this
1086 although for regular files and directories only
1088 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1089 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1090 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1094 ext2_print_inode(ip);
1099 * Initialize the vnode from the inode, check for aliases.
1100 * Note that the underlying vnode may have changed.
1102 if ((error = ext2_vinit(mp, &vp)) != 0) {
1109 * Finish inode initialization now that aliasing has been resolved.
1111 ip->i_devvp = ump->um_devvp;
1114 * Set up a generation number for this inode if it does not
1115 * already have one. This should only happen on old filesystems.
1117 if (ip->i_gen == 0) {
1118 ip->i_gen = krandom() / 2 + 1;
1119 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1120 ip->i_flag |= IN_MODIFIED;
1123 * Return the locked and refd vnode.
1130 * File handle to vnode
1132 * Have to be really careful about stale file handles:
1133 * - check that the inode number is valid
1134 * - call ext2_vget() to get the locked inode
1135 * - check for an unallocated inode (i_mode == 0)
1136 * - check that the given client host has export rights and return
1137 * those rights via. exflagsp and credanonp
1140 ext2_fhtovp(struct mount *mp, struct vnode *rootvp,
1141 struct fid *fhp, struct vnode **vpp)
1144 struct ext2_sb_info *fs;
1149 ufhp = (struct ufid *)fhp;
1150 fs = VFSTOEXT2(mp)->um_e2fs;
1151 if (ufhp->ufid_ino < EXT2_ROOTINO ||
1152 ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1155 error = VFS_VGET(mp, rootvp, ufhp->ufid_ino, &nvp);
1161 if (ip->i_mode == 0 ||
1162 ip->i_gen != ufhp->ufid_gen ||
1163 (VFSTOEXT2(mp)->um_i_effnlink_valid ? ip->i_effnlink :
1164 ip->i_nlink) <= 0) {
1174 * Vnode pointer to File handle
1178 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1184 ufhp = (struct ufid *)fhp;
1185 ufhp->ufid_len = sizeof(struct ufid);
1186 ufhp->ufid_ino = ip->i_number;
1187 ufhp->ufid_gen = ip->i_gen;
1192 * This is the generic part of fhtovp called after the underlying
1193 * filesystem has validated the file handle.
1195 * Verify that a host should have access to a filesystem.
1198 ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1199 struct ucred **credanonp)
1202 struct ext2_mount *ump;
1204 ump = VFSTOEXT2(mp);
1206 * Get the export permission structure for this <mp, client> tuple.
1208 np = vfs_export_lookup(mp, &ump->um_export, nam);
1212 *exflagsp = np->netc_exflags;
1213 *credanonp = &np->netc_anon;
1218 * Write a superblock and associated information back to disk.
1221 ext2_sbupdate(struct ext2_mount *mp, int waitfor)
1223 struct ext2_sb_info *fs = mp->um_e2fs;
1224 struct ext2_super_block *es = fs->s_es;
1228 kprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1230 bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1231 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1232 if (waitfor == MNT_WAIT)
1238 * The buffers for group descriptors, inode bitmaps and block bitmaps
1239 * are not busy at this point and are (hopefully) written by the
1240 * usual sync mechanism. No need to write them here