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;
688 * Disallow multiple mounts of the same device.
689 * Disallow mounting of a device that is currently in use
690 * (except for root, which might share swap device for miniroot).
691 * Flush out any old buffers remaining from a previous use.
693 if ((error = vfs_mountedon(devvp)) != 0)
695 if (vcount(devvp) > 0)
697 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
700 /* turn on this to force it to be read-only */
701 mp->mnt_flag |= MNT_RDONLY;
704 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
705 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
706 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
711 if (dev->si_iosize_max != 0)
712 mp->mnt_iosize_max = dev->si_iosize_max;
713 if (mp->mnt_iosize_max > MAXPHYS)
714 mp->mnt_iosize_max = MAXPHYS;
718 if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
720 es = (struct ext2_super_block *)bp->b_data;
721 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
722 error = EINVAL; /* XXX needs translation */
725 if ((es->s_state & EXT2_VALID_FS) == 0 ||
726 (es->s_state & EXT2_ERROR_FS)) {
727 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
729 "WARNING: Filesystem was not properly dismounted\n");
732 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
737 ump = kmalloc(sizeof *ump, M_EXT2MNT, M_WAITOK | M_ZERO);
738 ump->um_malloctype = M_EXT2NODE;
739 ump->um_blkatoff = ext2_blkatoff;
740 ump->um_truncate = ext2_truncate;
741 ump->um_update = ext2_update;
742 ump->um_valloc = ext2_valloc;
743 ump->um_vfree = ext2_vfree;
744 /* I don't know whether this is the right strategy. Note that
745 we dynamically allocate both a ext2_sb_info and a ext2_super_block
746 while Linux keeps the super block in a locked buffer
748 ump->um_e2fs = kmalloc(sizeof(struct ext2_sb_info),
749 M_EXT2MNT, M_WAITOK);
750 ump->um_e2fs->s_es = kmalloc(sizeof(struct ext2_super_block),
751 M_EXT2MNT, M_WAITOK);
752 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
753 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
756 * We don't free the group descriptors allocated by compute_sb_data()
757 * until ext2_unmount(). This is OK since the mount will succeed.
762 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
763 /* if the fs is not mounted read-only, make sure the super block is
764 always written back on a sync()
766 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
768 fs->s_dirt = 1; /* mark it modified */
769 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
771 mp->mnt_data = (qaddr_t)ump;
772 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
773 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
774 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
775 mp->mnt_flag |= MNT_LOCAL;
778 ump->um_devvp = devvp;
779 /* setting those two parameters allows us to use
780 ext2_bmap w/o changse !
782 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
783 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
784 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
785 for (i = 0; i < MAXQUOTAS; i++)
786 ump->um_quotas[i] = NULLVP;
787 dev->si_mountpoint = mp;
789 vfs_add_vnodeops(mp, &ext2_vnode_vops, &mp->mnt_vn_norm_ops);
790 vfs_add_vnodeops(mp, &ext2_spec_vops, &mp->mnt_vn_spec_ops);
791 vfs_add_vnodeops(mp, &ext2_fifo_vops, &mp->mnt_vn_fifo_ops);
794 ext2_sbupdate(ump, MNT_WAIT);
799 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
800 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
803 kfree(ump->um_e2fs->s_es, M_EXT2MNT);
804 kfree(ump->um_e2fs, M_EXT2MNT);
805 kfree(ump, M_EXT2MNT);
806 mp->mnt_data = (qaddr_t)0;
812 * unmount system call
815 ext2_unmount(struct mount *mp, int mntflags)
817 struct ext2_mount *ump;
818 struct ext2_sb_info *fs;
819 int error, flags, ronly, i;
822 if (mntflags & MNT_FORCE) {
823 if (mp->mnt_flag & MNT_ROOTFS)
827 if ((error = ext2_flushfiles(mp, flags)) != 0)
831 ronly = fs->s_rd_only;
834 fs->s_es->s_state |= EXT2_VALID_FS;
835 ext2_sbupdate(ump, MNT_WAIT);
838 /* release buffers containing group descriptors */
839 for(i = 0; i < fs->s_db_per_group; i++)
840 ULCK_BUF(fs->s_group_desc[i])
841 kfree(fs->s_group_desc, M_EXT2MNT);
843 /* release cached inode/block bitmaps */
844 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
845 if (fs->s_inode_bitmap[i])
846 ULCK_BUF(fs->s_inode_bitmap[i])
848 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
849 if (fs->s_block_bitmap[i])
850 ULCK_BUF(fs->s_block_bitmap[i])
852 ump->um_devvp->v_rdev->si_mountpoint = NULL;
854 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
855 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
856 vn_unlock(ump->um_devvp);
858 vrele(ump->um_devvp);
859 kfree(fs->s_es, M_EXT2MNT);
860 kfree(fs, M_EXT2MNT);
861 kfree(ump, M_EXT2MNT);
862 mp->mnt_data = (qaddr_t)0;
863 mp->mnt_flag &= ~MNT_LOCAL;
868 * Flush out all the files in a filesystem.
871 ext2_flushfiles(struct mount *mp, int flags)
873 struct ext2_mount *ump;
881 if (mp->mnt_flag & MNT_QUOTA) {
882 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
884 for (i = 0; i < MAXQUOTAS; i++) {
885 if (ump->um_quotas[i] == NULLVP)
887 ext2_quotaoff(mp, i);
890 * Here we fall through to vflush again to ensure
891 * that we have gotten rid of all the system vnodes.
895 error = vflush(mp, 0, flags);
900 * Go through the disk queues to initiate sandbagged IO;
901 * go through the inodes to write those that have been modified;
902 * initiate the writing of the super block if it has been modified.
904 * Note: we are always called with the filesystem marked `MPBUSY'.
907 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
910 ext2_sync(struct mount *mp, int waitfor)
912 struct ext2_mount *ump = VFSTOEXT2(mp);
913 struct ext2_sb_info *fs;
914 struct scaninfo scaninfo;
918 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */
919 kprintf("fs = %s\n", fs->fs_fsmnt);
920 panic("ext2_sync: rofs mod");
924 * Write back each (modified) inode.
926 scaninfo.allerror = 0;
928 scaninfo.waitfor = waitfor;
929 while (scaninfo.rescan) {
931 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
932 NULL, ext2_sync_scan, &scaninfo);
936 * Force stale file system control information to be flushed.
938 if ((waitfor & MNT_LAZY) == 0) {
939 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
940 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
941 scaninfo.allerror = error;
942 vn_unlock(ump->um_devvp);
948 * Write back modified superblock.
950 if (fs->s_dirt != 0) {
952 fs->s_es->s_wtime = time_second;
953 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
954 scaninfo.allerror = error;
956 return (scaninfo.allerror);
960 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
962 struct scaninfo *info = data;
967 if (vp->v_type == VNON ||
969 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
970 (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
973 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
974 info->allerror = error;
979 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
980 * in from disk. If it is in core, wait for the lock bit to clear, then
981 * return the inode locked. Detection and handling of mount points must be
982 * done by the calling routine.
985 ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
987 struct ext2_sb_info *fs;
989 struct ext2_mount *ump;
999 if ((*vpp = ext2_ihashget(dev, ino)) != NULL)
1003 * Lock out the creation of new entries in the FFS hash table in
1004 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1007 if (ext2fs_inode_hash_lock) {
1008 while (ext2fs_inode_hash_lock) {
1009 ext2fs_inode_hash_lock = -1;
1010 tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1014 ext2fs_inode_hash_lock = 1;
1017 * If this MALLOC() is performed after the getnewvnode()
1018 * it might block, leaving a vnode with a NULL v_data to be
1019 * found by ext2_sync() if a sync happens to fire right then,
1020 * which will cause a panic because ext2_sync() blindly
1021 * dereferences vp->v_data (as well it should).
1023 ip = kmalloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1025 /* Allocate a new vnode/inode. */
1026 if ((error = getnewvnode(VT_EXT2FS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
1027 if (ext2fs_inode_hash_lock < 0)
1028 wakeup(&ext2fs_inode_hash_lock);
1029 ext2fs_inode_hash_lock = 0;
1031 kfree(ip, M_EXT2NODE);
1034 bzero((caddr_t)ip, sizeof(struct inode));
1037 ip->i_e2fs = fs = ump->um_e2fs;
1041 for (i = 0; i < MAXQUOTAS; i++)
1042 ip->i_dquot[i] = NODQUOT;
1045 * Put it onto its hash chain. Since our vnode is locked, other
1046 * requests for this inode will block if they arrive while we are
1047 * sleeping waiting for old data structures to be purged or for the
1048 * contents of the disk portion of this inode to be read.
1052 if (ext2fs_inode_hash_lock < 0)
1053 wakeup(&ext2fs_inode_hash_lock);
1054 ext2fs_inode_hash_lock = 0;
1056 /* Read in the disk contents for the inode, copy into the inode. */
1058 kprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1060 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1061 (int)fs->s_blocksize, &bp);
1064 * The inode does not contain anything useful, so it would
1065 * be misleading to leave it on its hash chain. With mode
1066 * still zero, it will be unlinked and returned to the free
1074 /* convert ext2 inode to dinode */
1075 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
1076 ino_to_fsbo(fs, ino)), &ip->i_din);
1077 ip->i_block_group = ino_to_cg(fs, ino);
1078 ip->i_next_alloc_block = 0;
1079 ip->i_next_alloc_goal = 0;
1080 ip->i_prealloc_count = 0;
1081 ip->i_prealloc_block = 0;
1082 /* now we want to make sure that block pointers for unused
1083 blocks are zeroed out - ext2_balloc depends on this
1084 although for regular files and directories only
1086 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1087 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1088 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1092 ext2_print_inode(ip);
1097 * Initialize the vnode from the inode, check for aliases.
1098 * Note that the underlying vnode may have changed.
1100 if ((error = ext2_vinit(mp, &vp)) != 0) {
1107 * Finish inode initialization now that aliasing has been resolved.
1109 ip->i_devvp = ump->um_devvp;
1112 * Set up a generation number for this inode if it does not
1113 * already have one. This should only happen on old filesystems.
1115 if (ip->i_gen == 0) {
1116 ip->i_gen = krandom() / 2 + 1;
1117 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1118 ip->i_flag |= IN_MODIFIED;
1121 * Return the locked and refd vnode.
1128 * File handle to vnode
1130 * Have to be really careful about stale file handles:
1131 * - check that the inode number is valid
1132 * - call ext2_vget() to get the locked inode
1133 * - check for an unallocated inode (i_mode == 0)
1134 * - check that the given client host has export rights and return
1135 * those rights via. exflagsp and credanonp
1138 ext2_fhtovp(struct mount *mp, struct vnode *rootvp,
1139 struct fid *fhp, struct vnode **vpp)
1142 struct ext2_sb_info *fs;
1147 ufhp = (struct ufid *)fhp;
1148 fs = VFSTOEXT2(mp)->um_e2fs;
1149 if (ufhp->ufid_ino < EXT2_ROOTINO ||
1150 ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1153 error = VFS_VGET(mp, rootvp, ufhp->ufid_ino, &nvp);
1159 if (ip->i_mode == 0 ||
1160 ip->i_gen != ufhp->ufid_gen ||
1161 (VFSTOEXT2(mp)->um_i_effnlink_valid ? ip->i_effnlink :
1162 ip->i_nlink) <= 0) {
1172 * Vnode pointer to File handle
1176 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1182 ufhp = (struct ufid *)fhp;
1183 ufhp->ufid_len = sizeof(struct ufid);
1184 ufhp->ufid_ino = ip->i_number;
1185 ufhp->ufid_gen = ip->i_gen;
1190 * This is the generic part of fhtovp called after the underlying
1191 * filesystem has validated the file handle.
1193 * Verify that a host should have access to a filesystem.
1196 ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1197 struct ucred **credanonp)
1200 struct ext2_mount *ump;
1202 ump = VFSTOEXT2(mp);
1204 * Get the export permission structure for this <mp, client> tuple.
1206 np = vfs_export_lookup(mp, &ump->um_export, nam);
1210 *exflagsp = np->netc_exflags;
1211 *credanonp = &np->netc_anon;
1216 * Write a superblock and associated information back to disk.
1219 ext2_sbupdate(struct ext2_mount *mp, int waitfor)
1221 struct ext2_sb_info *fs = mp->um_e2fs;
1222 struct ext2_super_block *es = fs->s_es;
1226 kprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1228 bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1229 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1230 if (waitfor == MNT_WAIT)
1236 * The buffers for group descriptors, inode bitmaps and block bitmaps
1237 * are not busy at this point and are (hopefully) written by the
1238 * usual sync mechanism. No need to write them here