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. 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
36 * $FreeBSD: src/sys/gnu/ext2fs/ext2_vfsops.c,v 1.63.2.7 2002/07/01 00:18:51 iedowse Exp $
39 #include "opt_quota.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/nlookup.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
51 #include <sys/fcntl.h>
52 #include <sys/diskslice.h>
53 #include <sys/malloc.h>
55 #include <vm/vm_zone.h>
58 #include <sys/thread2.h>
63 #include "ext2_mount.h"
64 #include "ext2_extern.h"
68 #include "ext2_fs_sb.h"
70 extern struct vop_ops ext2_vnode_vops;
71 extern struct vop_ops ext2_spec_vops;
72 extern struct vop_ops ext2_fifo_vops;
74 static int ext2_fhtovp (struct mount *, struct vnode *,
75 struct fid *, struct vnode **);
76 static int ext2_flushfiles (struct mount *mp, int flags);
77 static int ext2_mount (struct mount *, char *, caddr_t, struct ucred *);
78 static int ext2_mountfs (struct vnode *, struct mount *, struct ucred *);
79 static int ext2_root(struct mount *, struct vnode **);
80 static int ext2_reload (struct mount *mountp, struct ucred *cred);
81 static int ext2_sbupdate (struct ext2_mount *, int);
82 static int ext2_sync (struct mount *, int);
83 static int ext2_unmount (struct mount *, int);
84 static int ext2_vget (struct mount *, struct vnode *, ino_t, struct vnode **);
85 static int ext2_init(struct vfsconf *);
86 static int ext2_vptofh (struct vnode *, struct fid *);
88 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
89 MALLOC_DEFINE(M_EXT2MNT, "EXT2 mount", "EXT2 mount structure");
91 static struct vfsops ext2fs_vfsops = {
93 .vfs_mount = ext2_mount,
94 .vfs_unmount = ext2_unmount,
95 .vfs_root = ext2_root, /* root inode via vget */
96 .vfs_quotactl = ext2_quotactl, /* quota operations */
97 .vfs_statfs = ext2_statfs,
98 .vfs_sync = ext2_sync,
99 .vfs_vget = ext2_vget,
100 .vfs_fhtovp = ext2_fhtovp,
101 .vfs_checkexp = ext2_check_export,
102 .vfs_vptofh = ext2_vptofh,
103 .vfs_init = ext2_init,
104 .vfs_uninit = ext2_uninit
107 VFS_SET(ext2fs_vfsops, ext2fs, 0);
108 MODULE_VERSION(ext2fs, 1);
110 static int ext2fs_inode_hash_lock;
112 static int ext2_check_sb_compat (struct ext2_super_block *es,
113 cdev_t dev, int ronly);
114 static int compute_sb_data (struct vnode *devvp,
115 struct ext2_super_block *es,
116 struct ext2_sb_info *fs);
119 ext2_root(struct mount *mp, struct vnode **vpp)
124 error = VFS_VGET(mp, NULL, (ino_t)EXT2_ROOTINO, &nvp);
132 * Do operations associated with quotas
135 ext2_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
141 int cmd, type, error;
143 type = cmds & SUBCMDMASK;
144 cmd = cmds >> SUBCMDSHIFT;
165 error = priv_check_cred(cred, PRIV_UFS_QUOTAON, 0);
169 error = priv_check_cred(cred, PRIV_UFS_QUOTAOFF, 0);
173 error = priv_check_cred(cred, PRIV_VFS_SETQUOTA, 0);
177 error = priv_check_cred(cred, PRIV_UFS_SETUSE, 0);
181 if (uid == cred->cr_ruid)
184 error = priv_check_cred(cred, PRIV_VFS_GETQUOTA, 0);
200 if ((uint)type >= MAXQUOTAS)
202 if (vfs_busy(mp, LK_NOWAIT))
208 error = ext2_quotaon(cred, mp, type, arg);
212 error = ext2_quotaoff(mp, type);
216 error = ext2_setquota(mp, uid, type, arg);
220 error = ext2_setuse(mp, uid, type, arg);
224 error = ext2_getquota(mp, uid, type, arg);
228 error = ext2_qsync(mp);
241 * Initial UFS filesystems, done only once.
244 ext2_init(struct vfsconf *vfsp)
264 * data: this is actually a (struct ext2_args *)
267 ext2_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
270 struct ext2_args args;
271 struct ext2_mount *ump = NULL;
272 struct ext2_sb_info *fs;
276 struct nlookupdata nd;
278 if ((error = copyin(data, (caddr_t)&args, sizeof (struct ext2_args))) != 0)
282 * If updating, check whether changing from read-only to
283 * read/write; if there is no device name, that's all we do.
285 if (mp->mnt_flag & MNT_UPDATE) {
288 devvp = ump->um_devvp;
290 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
292 if (mp->mnt_flag & MNT_FORCE)
294 if (vfs_busy(mp, LK_NOWAIT))
296 error = ext2_flushfiles(mp, flags);
298 if (!error && fs->s_wasvalid) {
299 fs->s_es->s_state |= EXT2_VALID_FS;
300 ext2_sbupdate(ump, MNT_WAIT);
303 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
304 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
305 VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
308 if (!error && (mp->mnt_flag & MNT_RELOAD))
309 error = ext2_reload(mp, cred);
312 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
313 (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
315 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
317 * If upgrade to read-write by non-root, then verify
318 * that user has necessary permissions on the device.
320 if (cred->cr_uid != 0) {
321 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
322 error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
330 if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
331 (fs->s_es->s_state & EXT2_ERROR_FS)) {
332 if (mp->mnt_flag & MNT_FORCE) {
334 "WARNING: %s was not properly dismounted\n",
338 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
343 fs->s_es->s_state &= ~EXT2_VALID_FS;
344 ext2_sbupdate(ump, MNT_WAIT);
346 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
347 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
348 VOP_CLOSE(devvp, FREAD, NULL);
351 if (args.fspec == NULL) {
353 * Process export requests.
355 return (vfs_export(mp, &ump->um_export, &args.export));
359 * Not an update, or updating the name: look up the name
360 * and verify that it refers to a sensible block device.
363 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
365 error = nlookup(&nd);
367 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
372 if (!vn_isdisk(devvp, &error)) {
378 * If mount by non-root, then verify that user has necessary
379 * permissions on the device.
381 if (cred->cr_uid != 0) {
383 if ((mp->mnt_flag & MNT_RDONLY) == 0)
384 accessmode |= VWRITE;
385 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
386 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
393 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
394 error = ext2_mountfs(devvp, mp, cred);
396 if (devvp != ump->um_devvp)
397 error = EINVAL; /* needs translation */
407 copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
408 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
409 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
410 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
411 ext2_statfs(mp, &mp->mnt_stat, cred);
416 ext2_check_sb_compat(struct ext2_super_block *es, cdev_t dev, int ronly)
418 if (es->s_magic != EXT2_SUPER_MAGIC) {
419 kprintf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
420 devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
423 if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
424 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
426 "WARNING: mount of %s denied due to unsupported optional features\n",
431 (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
433 "WARNING: R/W mount of %s denied due to unsupported optional features\n",
442 * this computes the fields of the ext2_sb_info structure from the
443 * data in the ext2_super_block structure read in
446 compute_sb_data(struct vnode *devvp, struct ext2_super_block *es,
447 struct ext2_sb_info *fs)
451 int logic_sb_block = 1; /* XXX for now */
456 #define V(v) kprintf(#v"= %d\n", fs->v);
459 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
461 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
463 fs->s_fsbtodb = es->s_log_block_size + 1;
465 fs->s_qbmask = fs->s_blocksize - 1;
467 fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
469 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
472 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
474 fs->s_blocks_per_group = es->s_blocks_per_group;
475 V(s_blocks_per_group)
476 fs->s_frags_per_group = es->s_frags_per_group;
478 fs->s_inodes_per_group = es->s_inodes_per_group;
479 V(s_inodes_per_group)
480 if (es->s_rev_level == EXT2_GOOD_OLD_REV) {
481 fs->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
482 fs->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
484 fs->s_first_ino = es->s_first_ino;
485 fs->s_inode_size = es->s_inode_size;
487 * Simple sanity check for superblock inode size value.
489 if (fs->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
490 fs->s_inode_size > fs->s_blocksize ||
491 (fs->s_inode_size & (fs->s_inode_size - 1)) != 0) {
492 kprintf("EXT2-fs: invalid inode size %d\n",
499 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE(fs);
500 V(s_inodes_per_block)
501 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
503 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
505 /* s_resuid / s_resgid ? */
506 fs->s_groups_count = (es->s_blocks_count - es->s_first_data_block +
507 EXT2_BLOCKS_PER_GROUP(fs) - 1) /
508 EXT2_BLOCKS_PER_GROUP(fs);
510 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
511 EXT2_DESC_PER_BLOCK(fs);
512 fs->s_db_per_group = db_count;
515 fs->s_group_desc = kmalloc(db_count * sizeof (struct buf *),
516 M_EXT2MNT, M_WAITOK);
518 /* adjust logic_sb_block */
519 if (fs->s_blocksize > SBSIZE)
521 * Godmar thinks: if the blocksize is greater than 1024,
522 * then, the superblock is logically part of block zero.
526 for (i = 0; i < db_count; i++) {
527 error = bread(devvp, fsbtodoff(fs, logic_sb_block + i + 1),
528 fs->s_blocksize, &fs->s_group_desc[i]);
530 for (j = 0; j < i; j++)
531 brelse(fs->s_group_desc[j]);
532 kfree(fs->s_group_desc, M_EXT2MNT);
533 kprintf("EXT2-fs: unable to read group descriptors (%d)\n", error);
536 /* Set the B_LOCKED flag on the buffer, then brelse() it */
537 LCK_BUF(fs->s_group_desc[i])
539 if (!ext2_check_descriptors(fs)) {
540 for (j = 0; j < db_count; j++)
541 ULCK_BUF(fs->s_group_desc[j])
542 kfree(fs->s_group_desc, M_EXT2MNT);
543 kprintf("EXT2-fs: (ext2_check_descriptors failure) "
544 "unable to read group descriptors\n");
548 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
549 fs->s_inode_bitmap_number[i] = 0;
550 fs->s_inode_bitmap[i] = NULL;
551 fs->s_block_bitmap_number[i] = 0;
552 fs->s_block_bitmap[i] = NULL;
554 fs->s_loaded_inode_bitmaps = 0;
555 fs->s_loaded_block_bitmaps = 0;
560 * Reload all incore data for a filesystem (used after running fsck on
561 * the root filesystem and finding things to fix). The filesystem must
562 * be mounted read-only.
564 * Things to do to update the mount:
565 * 1) invalidate all cached meta-data.
566 * 2) re-read superblock from disk.
567 * 3) re-read summary information from disk.
568 * 4) invalidate all inactive vnodes.
569 * 5) invalidate all cached file data.
570 * 6) re-read inode data for all active vnodes.
572 static int ext2_reload_scan(struct mount *mp, struct vnode *vp, void *rescan);
579 struct ext2_sb_info *fs;
583 ext2_reload(struct mount *mountp, struct ucred *cred)
587 struct ext2_super_block *es;
588 struct ext2_sb_info *fs;
590 struct scaninfo scaninfo;
592 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
595 * Step 1: invalidate all cached meta-data.
597 devvp = VFSTOEXT2(mountp)->um_devvp;
598 if (vinvalbuf(devvp, 0, 0, 0))
599 panic("ext2_reload: dirty1");
601 * Step 2: re-read superblock from disk.
602 * constants have been adjusted for ext2
604 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
606 es = (struct ext2_super_block *)bp->b_data;
607 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
609 return (EIO); /* XXX needs translation */
611 fs = VFSTOEXT2(mountp)->um_e2fs;
612 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
614 if((error = compute_sb_data(devvp, es, fs)) != 0) {
619 if (fs->fs_sbsize < SBSIZE)
620 bp->b_flags |= B_INVAL;
625 scaninfo.devvp = devvp;
627 while (error == 0 && scaninfo.rescan) {
629 error = vmntvnodescan(mountp, VMSC_GETVX,
630 NULL, ext2_reload_scan, &scaninfo);
636 ext2_reload_scan(struct mount *mp, struct vnode *vp, void *data)
638 struct scaninfo *info = data;
650 * Step 1: invalidate all cached file data.
652 if (vinvalbuf(vp, 0, 0, 0))
653 panic("ext2_reload: dirty2");
655 * Step 2: re-read inode data for all active vnodes.
658 error = bread(info->devvp,
659 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
660 (int)info->fs->s_blocksize, &bp);
663 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
664 EXT2_INODE_SIZE(info->fs) * ino_to_fsbo(info->fs, ip->i_number)),
671 * Common code for mount and mountroot
674 ext2_mountfs(struct vnode *devvp, struct mount *mp, struct ucred *cred)
676 struct ext2_mount *ump;
678 struct ext2_sb_info *fs;
679 struct ext2_super_block *es;
685 * Disallow multiple mounts of the same device.
686 * Disallow mounting of a device that is currently in use
687 * (except for root, which might share swap device for miniroot).
688 * Flush out any old buffers remaining from a previous use.
690 if ((error = vfs_mountedon(devvp)) != 0)
692 if (vcount(devvp) > 0)
694 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
697 /* turn on this to force it to be read-only */
698 mp->mnt_flag |= MNT_RDONLY;
701 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
702 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
703 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
708 if (dev->si_iosize_max != 0)
709 mp->mnt_iosize_max = dev->si_iosize_max;
710 if (mp->mnt_iosize_max > MAXPHYS)
711 mp->mnt_iosize_max = MAXPHYS;
715 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
717 es = (struct ext2_super_block *)bp->b_data;
718 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
719 error = EINVAL; /* XXX needs translation */
722 if ((es->s_state & EXT2_VALID_FS) == 0 ||
723 (es->s_state & EXT2_ERROR_FS)) {
724 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
726 "WARNING: Filesystem was not properly dismounted\n");
729 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
734 ump = kmalloc(sizeof *ump, M_EXT2MNT, M_WAITOK | M_ZERO);
735 ump->um_malloctype = M_EXT2NODE;
736 ump->um_blkatoff = ext2_blkatoff;
737 ump->um_truncate = ext2_truncate;
738 ump->um_update = ext2_update;
739 ump->um_valloc = ext2_valloc;
740 ump->um_vfree = ext2_vfree;
741 /* I don't know whether this is the right strategy. Note that
742 we dynamically allocate both a ext2_sb_info and a ext2_super_block
743 while Linux keeps the super block in a locked buffer
745 ump->um_e2fs = kmalloc(sizeof(struct ext2_sb_info),
746 M_EXT2MNT, M_WAITOK);
747 ump->um_e2fs->s_es = kmalloc(sizeof(struct ext2_super_block),
748 M_EXT2MNT, M_WAITOK);
749 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
750 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
753 * We don't free the group descriptors allocated by compute_sb_data()
754 * until ext2_unmount(). This is OK since the mount will succeed.
759 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
760 /* if the fs is not mounted read-only, make sure the super block is
761 always written back on a sync()
763 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
765 fs->s_dirt = 1; /* mark it modified */
766 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
768 mp->mnt_data = (qaddr_t)ump;
769 mp->mnt_stat.f_fsid.val[0] = devid_from_dev(dev);
770 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
771 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
772 mp->mnt_flag |= MNT_LOCAL;
775 ump->um_devvp = devvp;
776 /* setting those two parameters allows us to use
777 ext2_bmap w/o changse !
779 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
780 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
781 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
782 for (i = 0; i < MAXQUOTAS; i++)
783 ump->um_quotas[i] = NULLVP;
784 dev->si_mountpoint = mp;
786 vfs_add_vnodeops(mp, &ext2_vnode_vops, &mp->mnt_vn_norm_ops);
787 vfs_add_vnodeops(mp, &ext2_spec_vops, &mp->mnt_vn_spec_ops);
788 vfs_add_vnodeops(mp, &ext2_fifo_vops, &mp->mnt_vn_fifo_ops);
791 ext2_sbupdate(ump, MNT_WAIT);
796 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
797 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
800 kfree(ump->um_e2fs->s_es, M_EXT2MNT);
801 kfree(ump->um_e2fs, M_EXT2MNT);
802 kfree(ump, M_EXT2MNT);
803 mp->mnt_data = (qaddr_t)0;
809 * unmount system call
812 ext2_unmount(struct mount *mp, int mntflags)
814 struct ext2_mount *ump;
815 struct ext2_sb_info *fs;
816 int error, flags, ronly, i;
819 if (mntflags & MNT_FORCE) {
820 if (mp->mnt_flag & MNT_ROOTFS)
824 if ((error = ext2_flushfiles(mp, flags)) != 0)
828 ronly = fs->s_rd_only;
831 fs->s_es->s_state |= EXT2_VALID_FS;
832 ext2_sbupdate(ump, MNT_WAIT);
835 /* release buffers containing group descriptors */
836 for(i = 0; i < fs->s_db_per_group; i++)
837 ULCK_BUF(fs->s_group_desc[i])
838 kfree(fs->s_group_desc, M_EXT2MNT);
840 /* release cached inode/block bitmaps */
841 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
842 if (fs->s_inode_bitmap[i])
843 ULCK_BUF(fs->s_inode_bitmap[i])
845 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
846 if (fs->s_block_bitmap[i])
847 ULCK_BUF(fs->s_block_bitmap[i])
849 ump->um_devvp->v_rdev->si_mountpoint = NULL;
851 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
852 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
853 vn_unlock(ump->um_devvp);
855 vrele(ump->um_devvp);
856 kfree(fs->s_es, M_EXT2MNT);
857 kfree(fs, M_EXT2MNT);
858 kfree(ump, M_EXT2MNT);
859 mp->mnt_data = (qaddr_t)0;
860 mp->mnt_flag &= ~MNT_LOCAL;
865 * Flush out all the files in a filesystem.
868 ext2_flushfiles(struct mount *mp, int flags)
870 struct ext2_mount *ump;
878 if (mp->mnt_flag & MNT_QUOTA) {
879 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
881 for (i = 0; i < MAXQUOTAS; i++) {
882 if (ump->um_quotas[i] == NULLVP)
884 ext2_quotaoff(mp, i);
887 * Here we fall through to vflush again to ensure
888 * that we have gotten rid of all the system vnodes.
892 error = vflush(mp, 0, flags);
897 * Go through the disk queues to initiate sandbagged IO;
898 * go through the inodes to write those that have been modified;
899 * initiate the writing of the super block if it has been modified.
901 * Note: we are always called with the filesystem marked `MPBUSY'.
904 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
907 ext2_sync(struct mount *mp, int waitfor)
909 struct ext2_mount *ump = VFSTOEXT2(mp);
910 struct ext2_sb_info *fs;
911 struct scaninfo scaninfo;
915 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */
916 kprintf("fs = %s\n", fs->fs_fsmnt);
917 panic("ext2_sync: rofs mod");
921 * Write back each (modified) inode.
923 scaninfo.allerror = 0;
925 scaninfo.waitfor = waitfor;
926 while (scaninfo.rescan) {
928 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
929 NULL, ext2_sync_scan, &scaninfo);
933 * Force stale file system control information to be flushed.
935 if ((waitfor & MNT_LAZY) == 0) {
936 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
937 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
938 scaninfo.allerror = error;
939 vn_unlock(ump->um_devvp);
945 * Write back modified superblock.
947 if (fs->s_dirt != 0) {
949 fs->s_es->s_wtime = time_second;
950 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
951 scaninfo.allerror = error;
953 return (scaninfo.allerror);
957 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
959 struct scaninfo *info = data;
964 if (vp->v_type == VNON ||
966 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
967 (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
970 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
971 info->allerror = error;
976 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
977 * in from disk. If it is in core, wait for the lock bit to clear, then
978 * return the inode locked. Detection and handling of mount points must be
979 * done by the calling routine.
982 ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
984 struct ext2_sb_info *fs;
986 struct ext2_mount *ump;
996 if ((*vpp = ext2_ihashget(dev, ino)) != NULL)
1000 * Lock out the creation of new entries in the FFS hash table in
1001 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1004 if (ext2fs_inode_hash_lock) {
1005 while (ext2fs_inode_hash_lock) {
1006 ext2fs_inode_hash_lock = -1;
1007 tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1011 ext2fs_inode_hash_lock = 1;
1014 * If this MALLOC() is performed after the getnewvnode()
1015 * it might block, leaving a vnode with a NULL v_data to be
1016 * found by ext2_sync() if a sync happens to fire right then,
1017 * which will cause a panic because ext2_sync() blindly
1018 * dereferences vp->v_data (as well it should).
1020 ip = kmalloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1022 /* Allocate a new vnode/inode. */
1023 if ((error = getnewvnode(VT_EXT2FS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
1024 if (ext2fs_inode_hash_lock < 0)
1025 wakeup(&ext2fs_inode_hash_lock);
1026 ext2fs_inode_hash_lock = 0;
1028 kfree(ip, M_EXT2NODE);
1031 bzero((caddr_t)ip, sizeof(struct inode));
1034 ip->i_e2fs = fs = ump->um_e2fs;
1038 for (i = 0; i < MAXQUOTAS; i++)
1039 ip->i_dquot[i] = NODQUOT;
1042 * Put it onto its hash chain. Since our vnode is locked, other
1043 * requests for this inode will block if they arrive while we are
1044 * sleeping waiting for old data structures to be purged or for the
1045 * contents of the disk portion of this inode to be read.
1049 if (ext2fs_inode_hash_lock < 0)
1050 wakeup(&ext2fs_inode_hash_lock);
1051 ext2fs_inode_hash_lock = 0;
1053 /* Read in the disk contents for the inode, copy into the inode. */
1055 kprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1057 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1058 (int)fs->s_blocksize, &bp);
1061 * The inode does not contain anything useful, so it would
1062 * be misleading to leave it on its hash chain. With mode
1063 * still zero, it will be unlinked and returned to the free
1071 /* convert ext2 inode to dinode */
1072 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
1073 ino_to_fsbo(fs, ino)), &ip->i_din);
1074 ip->i_block_group = ino_to_cg(fs, ino);
1075 ip->i_next_alloc_block = 0;
1076 ip->i_next_alloc_goal = 0;
1077 ip->i_prealloc_count = 0;
1078 ip->i_prealloc_block = 0;
1079 /* now we want to make sure that block pointers for unused
1080 blocks are zeroed out - ext2_balloc depends on this
1081 although for regular files and directories only
1083 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1084 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1085 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1089 ext2_print_inode(ip);
1094 * Initialize the vnode from the inode, check for aliases.
1095 * Note that the underlying vnode may have changed.
1097 if ((error = ext2_vinit(mp, &vp)) != 0) {
1104 * Finish inode initialization now that aliasing has been resolved.
1106 ip->i_devvp = ump->um_devvp;
1109 * Set up a generation number for this inode if it does not
1110 * already have one. This should only happen on old filesystems.
1112 if (ip->i_gen == 0) {
1113 ip->i_gen = krandom() / 2 + 1;
1114 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1115 ip->i_flag |= IN_MODIFIED;
1118 * Return the locked and refd vnode.
1125 * File handle to vnode
1127 * Have to be really careful about stale file handles:
1128 * - check that the inode number is valid
1129 * - call ext2_vget() to get the locked inode
1130 * - check for an unallocated inode (i_mode == 0)
1131 * - check that the given client host has export rights and return
1132 * those rights via. exflagsp and credanonp
1135 ext2_fhtovp(struct mount *mp, struct vnode *rootvp,
1136 struct fid *fhp, struct vnode **vpp)
1139 struct ext2_sb_info *fs;
1144 ufhp = (struct ufid *)fhp;
1145 fs = VFSTOEXT2(mp)->um_e2fs;
1146 if (ufhp->ufid_ino < EXT2_ROOTINO ||
1147 ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1150 error = VFS_VGET(mp, rootvp, ufhp->ufid_ino, &nvp);
1156 if (ip->i_mode == 0 ||
1157 ip->i_gen != ufhp->ufid_gen ||
1158 (VFSTOEXT2(mp)->um_i_effnlink_valid ? ip->i_effnlink :
1159 ip->i_nlink) <= 0) {
1169 * Vnode pointer to File handle
1173 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1179 ufhp = (struct ufid *)fhp;
1180 ufhp->ufid_len = sizeof(struct ufid);
1181 ufhp->ufid_ino = ip->i_number;
1182 ufhp->ufid_gen = ip->i_gen;
1187 * This is the generic part of fhtovp called after the underlying
1188 * filesystem has validated the file handle.
1190 * Verify that a host should have access to a filesystem.
1193 ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1194 struct ucred **credanonp)
1197 struct ext2_mount *ump;
1199 ump = VFSTOEXT2(mp);
1201 * Get the export permission structure for this <mp, client> tuple.
1203 np = vfs_export_lookup(mp, &ump->um_export, nam);
1207 *exflagsp = np->netc_exflags;
1208 *credanonp = &np->netc_anon;
1213 * Write a superblock and associated information back to disk.
1216 ext2_sbupdate(struct ext2_mount *mp, int waitfor)
1218 struct ext2_sb_info *fs = mp->um_e2fs;
1219 struct ext2_super_block *es = fs->s_es;
1223 kprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1225 bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1226 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1227 if (waitfor == MNT_WAIT)
1233 * The buffers for group descriptors, inode bitmaps and block bitmaps
1234 * are not busy at this point and are (hopefully) written by the
1235 * usual sync mechanism. No need to write them here