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 $
41 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_vfsops.c,v 1.34 2006/04/01 20:46:53 dillon Exp $
44 #include "opt_quota.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #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/disklabel.h>
57 #include <sys/malloc.h>
60 #include <sys/thread2.h>
62 #include <vfs/ufs/quota.h>
63 #include <vfs/ufs/ufsmount.h>
64 #include <vfs/ufs/inode.h>
65 #include <vfs/ufs/ufs_extern.h>
67 #include <vm/vm_zone.h>
70 #include "ext2_extern.h"
72 #include "ext2_fs_sb.h"
74 extern struct vnodeopv_entry_desc ext2_vnodeop_entries[];
75 extern struct vnodeopv_entry_desc ext2_specop_entries[];
76 extern struct vnodeopv_entry_desc ext2_fifoop_entries[];
78 static int ext2_fhtovp (struct mount *, struct fid *, struct vnode **);
79 static int ext2_flushfiles (struct mount *mp, int flags, struct thread *td);
80 static int ext2_mount (struct mount *, char *, caddr_t, struct thread *);
81 static int ext2_mountfs (struct vnode *, struct mount *, struct thread *);
82 static int ext2_reload (struct mount *mountp, struct ucred *cred,
84 static int ext2_sbupdate (struct ufsmount *, int);
85 static int ext2_statfs (struct mount *, struct statfs *, struct thread *);
86 static int ext2_sync (struct mount *, int, struct thread *);
87 static int ext2_unmount (struct mount *, int, struct thread *);
88 static int ext2_vget (struct mount *, ino_t, struct vnode **);
89 static int ext2_vptofh (struct vnode *, struct fid *);
91 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
93 static struct vfsops ext2fs_vfsops = {
94 .vfs_mount = ext2_mount,
95 .vfs_unmount = ext2_unmount,
96 .vfs_root = ufs_root, /* root inode via vget */
97 .vfs_quotactl = ufs_quotactl, /* quota operations */
98 .vfs_statfs = ext2_statfs,
99 .vfs_sync = ext2_sync,
100 .vfs_vget = ext2_vget,
101 .vfs_fhtovp = ext2_fhtovp,
102 .vfs_checkexp = ufs_check_export,
103 .vfs_vptofh = ext2_vptofh,
104 .vfs_init = ufs_init,
105 .vfs_uninit = ufs_uninit
108 VFS_SET(ext2fs_vfsops, ext2fs, 0);
109 #define bsd_malloc malloc
110 #define bsd_free free
112 static int ext2fs_inode_hash_lock;
114 static int ext2_check_sb_compat (struct ext2_super_block *es,
115 dev_t dev, int ronly);
116 static int compute_sb_data (struct vnode * devvp,
117 struct ext2_super_block * es,
118 struct ext2_sb_info * fs);
126 * data: this is actually a (struct ufs_args *)
129 ext2_mount(struct mount *mp, char *path, caddr_t data,
133 struct ufs_args args;
134 struct ufsmount *ump = 0;
135 struct ext2_sb_info *fs;
140 struct nlookupdata nd;
142 if ((error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) != 0)
145 cred = td->td_proc->p_ucred;
147 * If updating, check whether changing from read-only to
148 * read/write; if there is no device name, that's all we do.
150 if (mp->mnt_flag & MNT_UPDATE) {
153 devvp = ump->um_devvp;
155 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
157 if (mp->mnt_flag & MNT_FORCE)
159 if (vfs_busy(mp, LK_NOWAIT, td))
161 error = ext2_flushfiles(mp, flags, td);
163 if (!error && fs->s_wasvalid) {
164 fs->s_es->s_state |= EXT2_VALID_FS;
165 ext2_sbupdate(ump, MNT_WAIT);
168 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
169 VOP_OPEN(devvp, FREAD, FSCRED, NULL, td);
170 VOP_CLOSE(devvp, FREAD|FWRITE, td);
171 VOP_UNLOCK(devvp, 0, td);
173 if (!error && (mp->mnt_flag & MNT_RELOAD))
174 error = ext2_reload(mp, proc0.p_ucred, td);
177 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
178 (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
180 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
182 * If upgrade to read-write by non-root, then verify
183 * that user has necessary permissions on the device.
185 if (cred->cr_uid != 0) {
186 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
187 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
189 VOP_UNLOCK(devvp, 0, td);
192 VOP_UNLOCK(devvp, 0, td);
195 if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
196 (fs->s_es->s_state & EXT2_ERROR_FS)) {
197 if (mp->mnt_flag & MNT_FORCE) {
199 "WARNING: %s was not properly dismounted\n",
203 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
208 fs->s_es->s_state &= ~EXT2_VALID_FS;
209 ext2_sbupdate(ump, MNT_WAIT);
211 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
212 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL, td);
213 VOP_CLOSE(devvp, FREAD, td);
214 VOP_UNLOCK(devvp, 0, td);
216 if (args.fspec == 0) {
218 * Process export requests.
220 return (vfs_export(mp, &ump->um_export, &args.export));
224 * Not an update, or updating the name: look up the name
225 * and verify that it refers to a sensible block device.
228 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
230 error = nlookup(&nd);
232 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
237 if (!vn_isdisk(devvp, &error)) {
243 * If mount by non-root, then verify that user has necessary
244 * permissions on the device.
246 if (cred->cr_uid != 0) {
248 if ((mp->mnt_flag & MNT_RDONLY) == 0)
249 accessmode |= VWRITE;
250 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
251 if ((error = VOP_ACCESS(devvp, accessmode, cred, td)) != 0) {
255 VOP_UNLOCK(devvp, 0, td);
258 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
259 error = ext2_mountfs(devvp, mp, td);
261 if (devvp != ump->um_devvp)
262 error = EINVAL; /* needs translation */
272 copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
273 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
274 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
275 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
276 ext2_statfs(mp, &mp->mnt_stat, td);
281 * checks that the data in the descriptor blocks make sense
282 * this is taken from ext2/super.c
285 ext2_check_descriptors(struct ext2_sb_info *sb)
289 unsigned long block = sb->s_es->s_first_data_block;
290 struct ext2_group_desc * gdp = NULL;
292 /* ext2_debug ("Checking group descriptors"); */
294 for (i = 0; i < sb->s_groups_count; i++)
296 /* examine next descriptor block */
297 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
298 gdp = (struct ext2_group_desc *)
299 sb->s_group_desc[desc_block++]->b_data;
300 if (gdp->bg_block_bitmap < block ||
301 gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
303 printf ("ext2_check_descriptors: "
304 "Block bitmap for group %d"
305 " not in group (block %lu)!\n",
306 i, (unsigned long) gdp->bg_block_bitmap);
309 if (gdp->bg_inode_bitmap < block ||
310 gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
312 printf ("ext2_check_descriptors: "
313 "Inode bitmap for group %d"
314 " not in group (block %lu)!\n",
315 i, (unsigned long) gdp->bg_inode_bitmap);
318 if (gdp->bg_inode_table < block ||
319 gdp->bg_inode_table + sb->s_itb_per_group >=
320 block + EXT2_BLOCKS_PER_GROUP(sb))
322 printf ("ext2_check_descriptors: "
323 "Inode table for group %d"
324 " not in group (block %lu)!\n",
325 i, (unsigned long) gdp->bg_inode_table);
328 block += EXT2_BLOCKS_PER_GROUP(sb);
335 ext2_check_sb_compat(struct ext2_super_block *es, dev_t dev, int ronly)
337 if (es->s_magic != EXT2_SUPER_MAGIC) {
338 printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
339 devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
342 if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
343 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
345 "WARNING: mount of %s denied due to unsupported optional features\n",
350 (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
352 "WARNING: R/W mount of %s denied due to unsupported optional features\n",
361 * this computes the fields of the ext2_sb_info structure from the
362 * data in the ext2_super_block structure read in
365 compute_sb_data(struct vnode *devvp, struct ext2_super_block *es,
366 struct ext2_sb_info *fs)
370 int logic_sb_block = 1; /* XXX for now */
375 #define V(v) printf(#v"= %d\n", fs->v);
378 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
380 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
382 fs->s_fsbtodb = es->s_log_block_size + 1;
384 fs->s_qbmask = fs->s_blocksize - 1;
386 fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
388 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
391 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
393 fs->s_blocks_per_group = es->s_blocks_per_group;
394 V(s_blocks_per_group)
395 fs->s_frags_per_group = es->s_frags_per_group;
397 fs->s_inodes_per_group = es->s_inodes_per_group;
398 V(s_inodes_per_group)
399 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
400 V(s_inodes_per_block)
401 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
403 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
405 /* s_resuid / s_resgid ? */
406 fs->s_groups_count = (es->s_blocks_count -
407 es->s_first_data_block +
408 EXT2_BLOCKS_PER_GROUP(fs) - 1) /
409 EXT2_BLOCKS_PER_GROUP(fs);
411 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
412 EXT2_DESC_PER_BLOCK(fs);
413 fs->s_db_per_group = db_count;
416 fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
419 /* adjust logic_sb_block */
420 if(fs->s_blocksize > SBSIZE)
421 /* Godmar thinks: if the blocksize is greater than 1024, then
422 the superblock is logically part of block zero.
426 for (i = 0; i < db_count; i++) {
427 error = bread(devvp, fsbtodoff(fs, logic_sb_block + i + 1),
428 fs->s_blocksize, &fs->s_group_desc[i]);
430 for (j = 0; j < i; j++)
431 brelse(fs->s_group_desc[j]);
432 bsd_free(fs->s_group_desc, M_UFSMNT);
433 printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
436 /* Set the B_LOCKED flag on the buffer, then brelse() it */
437 LCK_BUF(fs->s_group_desc[i])
439 if(!ext2_check_descriptors(fs)) {
440 for (j = 0; j < db_count; j++)
441 ULCK_BUF(fs->s_group_desc[j])
442 bsd_free(fs->s_group_desc, M_UFSMNT);
443 printf("EXT2-fs: (ext2_check_descriptors failure) "
444 "unable to read group descriptors\n");
448 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
449 fs->s_inode_bitmap_number[i] = 0;
450 fs->s_inode_bitmap[i] = NULL;
451 fs->s_block_bitmap_number[i] = 0;
452 fs->s_block_bitmap[i] = NULL;
454 fs->s_loaded_inode_bitmaps = 0;
455 fs->s_loaded_block_bitmaps = 0;
460 * Reload all incore data for a filesystem (used after running fsck on
461 * the root filesystem and finding things to fix). The filesystem must
462 * be mounted read-only.
464 * Things to do to update the mount:
465 * 1) invalidate all cached meta-data.
466 * 2) re-read superblock from disk.
467 * 3) re-read summary information from disk.
468 * 4) invalidate all inactive vnodes.
469 * 5) invalidate all cached file data.
470 * 6) re-read inode data for all active vnodes.
472 static int ext2_reload_scan1(struct mount *mp, struct vnode *vp, void *rescan);
473 static int ext2_reload_scan2(struct mount *mp, struct vnode *vp, void *rescan);
481 struct ext2_sb_info *fs;
485 ext2_reload(struct mount *mountp, struct ucred *cred, struct thread *td)
489 struct ext2_super_block * es;
490 struct ext2_sb_info *fs;
492 struct scaninfo scaninfo;
494 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
497 * Step 1: invalidate all cached meta-data.
499 devvp = VFSTOUFS(mountp)->um_devvp;
500 if (vinvalbuf(devvp, 0, td, 0, 0))
501 panic("ext2_reload: dirty1");
503 * Step 2: re-read superblock from disk.
504 * constants have been adjusted for ext2
506 if ((error = bread(devvp, dbtob(SBLOCK), SBSIZE, &bp)) != 0)
508 es = (struct ext2_super_block *)bp->b_data;
509 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
511 return (EIO); /* XXX needs translation */
513 fs = VFSTOUFS(mountp)->um_e2fs;
514 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
516 if((error = compute_sb_data(devvp, es, fs)) != 0) {
521 if (fs->fs_sbsize < SBSIZE)
522 bp->b_flags |= B_INVAL;
528 scaninfo.devvp = devvp;
530 while (error == 0 && scaninfo.rescan) {
532 error = vmntvnodescan(mountp, VMSC_GETVX, ext2_reload_scan1,
533 ext2_reload_scan2, &scaninfo);
539 ext2_reload_scan1(struct mount *mp, struct vnode *vp, void *data)
541 /*struct scaninfo *info = data;*/
547 ext2_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
549 struct scaninfo *info = data;
557 if (vrecycle(vp, curthread))
561 * Step 5: invalidate all cached file data.
563 if (vinvalbuf(vp, 0, info->td, 0, 0))
564 panic("ext2_reload: dirty2");
566 * Step 6: re-read inode data for all active vnodes.
569 error = bread(info->devvp,
570 fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
571 (int)info->fs->s_blocksize, &bp);
574 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
575 EXT2_INODE_SIZE * ino_to_fsbo(info->fs, ip->i_number)),
582 * Common code for mount and mountroot
585 ext2_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td)
587 struct ufsmount *ump;
589 struct ext2_sb_info *fs;
590 struct ext2_super_block * es;
592 struct partinfo dpart;
598 * Disallow multiple mounts of the same device.
599 * Disallow mounting of a device that is currently in use
600 * (except for root, which might share swap device for miniroot).
601 * Flush out any old buffers remaining from a previous use.
603 if ((error = vfs_mountedon(devvp)) != 0)
605 if (count_udev(devvp->v_udev) > 0)
607 if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)) != 0)
610 /* turn on this to force it to be read-only */
611 mp->mnt_flag |= MNT_RDONLY;
614 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
615 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
616 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
617 VOP_UNLOCK(devvp, 0, td);
621 if (dev->si_iosize_max != 0)
622 mp->mnt_iosize_max = dev->si_iosize_max;
623 if (mp->mnt_iosize_max > MAXPHYS)
624 mp->mnt_iosize_max = MAXPHYS;
625 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, td) != 0)
629 size = dpart.disklab->d_secsize;
634 if ((error = bread(devvp, dbtob(SBLOCK), SBSIZE, &bp)) != 0)
636 es = (struct ext2_super_block *)bp->b_data;
637 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
638 error = EINVAL; /* XXX needs translation */
641 if ((es->s_state & EXT2_VALID_FS) == 0 ||
642 (es->s_state & EXT2_ERROR_FS)) {
643 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
645 "WARNING: Filesystem was not properly dismounted\n");
648 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
653 ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
654 bzero((caddr_t)ump, sizeof *ump);
655 ump->um_malloctype = M_EXT2NODE;
656 ump->um_blkatoff = ext2_blkatoff;
657 ump->um_truncate = ext2_truncate;
658 ump->um_update = ext2_update;
659 ump->um_valloc = ext2_valloc;
660 ump->um_vfree = ext2_vfree;
661 /* I don't know whether this is the right strategy. Note that
662 we dynamically allocate both a ext2_sb_info and a ext2_super_block
663 while Linux keeps the super block in a locked buffer
665 ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
667 ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
669 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
670 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
673 * We don't free the group descriptors allocated by compute_sb_data()
674 * until ext2_unmount(). This is OK since the mount will succeed.
679 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
680 /* if the fs is not mounted read-only, make sure the super block is
681 always written back on a sync()
683 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
685 fs->s_dirt = 1; /* mark it modified */
686 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
688 mp->mnt_data = (qaddr_t)ump;
689 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
690 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
691 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
692 mp->mnt_flag |= MNT_LOCAL;
695 ump->um_devvp = devvp;
696 /* setting those two parameters allows us to use
697 ufs_bmap w/o changse !
699 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
700 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
701 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
702 for (i = 0; i < MAXQUOTAS; i++)
703 ump->um_quotas[i] = NULLVP;
704 dev->si_mountpoint = mp;
706 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops,
707 ext2_vnodeop_entries, 0);
708 vfs_add_vnodeops(mp, &mp->mnt_vn_spec_ops,
709 ext2_specop_entries, 0);
710 vfs_add_vnodeops(mp, &mp->mnt_vn_fifo_ops,
711 ext2_fifoop_entries, 0);
714 ext2_sbupdate(ump, MNT_WAIT);
719 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
721 bsd_free(ump->um_e2fs->s_es, M_UFSMNT);
722 bsd_free(ump->um_e2fs, M_UFSMNT);
723 bsd_free(ump, M_UFSMNT);
724 mp->mnt_data = (qaddr_t)0;
730 * unmount system call
733 ext2_unmount(struct mount *mp, int mntflags, struct thread *td)
735 struct ufsmount *ump;
736 struct ext2_sb_info *fs;
737 int error, flags, ronly, i;
740 if (mntflags & MNT_FORCE) {
741 if (mp->mnt_flag & MNT_ROOTFS)
745 if ((error = ext2_flushfiles(mp, flags, td)) != 0)
749 ronly = fs->s_rd_only;
752 fs->s_es->s_state |= EXT2_VALID_FS;
753 ext2_sbupdate(ump, MNT_WAIT);
756 /* release buffers containing group descriptors */
757 for(i = 0; i < fs->s_db_per_group; i++)
758 ULCK_BUF(fs->s_group_desc[i])
759 bsd_free(fs->s_group_desc, M_UFSMNT);
761 /* release cached inode/block bitmaps */
762 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
763 if (fs->s_inode_bitmap[i])
764 ULCK_BUF(fs->s_inode_bitmap[i])
766 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
767 if (fs->s_block_bitmap[i])
768 ULCK_BUF(fs->s_block_bitmap[i])
770 ump->um_devvp->v_rdev->si_mountpoint = NULL;
771 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, td);
772 vrele(ump->um_devvp);
773 bsd_free(fs->s_es, M_UFSMNT);
774 bsd_free(fs, M_UFSMNT);
775 bsd_free(ump, M_UFSMNT);
776 mp->mnt_data = (qaddr_t)0;
777 mp->mnt_flag &= ~MNT_LOCAL;
782 * Flush out all the files in a filesystem.
785 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
787 struct ufsmount *ump;
795 if (mp->mnt_flag & MNT_QUOTA) {
796 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
798 for (i = 0; i < MAXQUOTAS; i++) {
799 if (ump->um_quotas[i] == NULLVP)
804 * Here we fall through to vflush again to ensure
805 * that we have gotten rid of all the system vnodes.
809 error = vflush(mp, 0, flags);
814 * Get file system statistics.
815 * taken from ext2/super.c ext2_statfs
818 ext2_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
820 unsigned long overhead;
821 struct ufsmount *ump;
822 struct ext2_sb_info *fs;
823 struct ext2_super_block *es;
830 if (es->s_magic != EXT2_SUPER_MAGIC)
831 panic("ext2_statfs - magic number spoiled");
834 * Compute the overhead (FS structures)
836 if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
838 for (i = 0 ; i < fs->s_groups_count; i++)
839 if (ext2_group_sparse(i))
842 nsb = fs->s_groups_count;
843 overhead = es->s_first_data_block +
844 /* Superblocks and block group descriptors: */
845 nsb * (1 + fs->s_db_per_group) +
846 /* Inode bitmap, block bitmap, and inode table: */
847 fs->s_groups_count * (1 + 1 + fs->s_itb_per_group);
849 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
850 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
851 sbp->f_blocks = es->s_blocks_count - overhead;
852 sbp->f_bfree = es->s_free_blocks_count;
853 sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
854 sbp->f_files = es->s_inodes_count;
855 sbp->f_ffree = es->s_free_inodes_count;
856 if (sbp != &mp->mnt_stat) {
857 sbp->f_type = mp->mnt_vfc->vfc_typenum;
858 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
859 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
865 * Go through the disk queues to initiate sandbagged IO;
866 * go through the inodes to write those that have been modified;
867 * initiate the writing of the super block if it has been modified.
869 * Note: we are always called with the filesystem marked `MPBUSY'.
872 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
875 ext2_sync(struct mount *mp, int waitfor, struct thread *td)
877 struct ufsmount *ump = VFSTOUFS(mp);
878 struct ext2_sb_info *fs;
879 struct scaninfo scaninfo;
883 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */
884 printf("fs = %s\n", fs->fs_fsmnt);
885 panic("ext2_sync: rofs mod");
889 * Write back each (modified) inode.
891 scaninfo.allerror = 0;
893 scaninfo.waitfor = waitfor;
895 while (scaninfo.rescan) {
897 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
898 NULL, ext2_sync_scan, &scaninfo);
902 * Force stale file system control information to be flushed.
904 if (waitfor != MNT_LAZY) {
905 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
906 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
907 scaninfo.allerror = error;
908 VOP_UNLOCK(ump->um_devvp, 0, td);
914 * Write back modified superblock.
916 if (fs->s_dirt != 0) {
918 fs->s_es->s_wtime = time_second;
919 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
920 scaninfo.allerror = error;
922 return (scaninfo.allerror);
926 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
928 struct scaninfo *info = data;
933 if (vp->v_type == VNON ||
935 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
936 (RB_EMPTY(&vp->v_rbdirty_tree) || info->waitfor == MNT_LAZY))) {
939 if ((error = VOP_FSYNC(vp, info->waitfor, info->td)) != 0)
940 info->allerror = error;
945 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
946 * in from disk. If it is in core, wait for the lock bit to clear, then
947 * return the inode locked. Detection and handling of mount points must be
948 * done by the calling routine.
951 ext2_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
953 struct ext2_sb_info *fs;
955 struct ufsmount *ump;
965 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
969 * Lock out the creation of new entries in the FFS hash table in
970 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
973 if (ext2fs_inode_hash_lock) {
974 while (ext2fs_inode_hash_lock) {
975 ext2fs_inode_hash_lock = -1;
976 tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
980 ext2fs_inode_hash_lock = 1;
983 * If this MALLOC() is performed after the getnewvnode()
984 * it might block, leaving a vnode with a NULL v_data to be
985 * found by ext2_sync() if a sync happens to fire right then,
986 * which will cause a panic because ext2_sync() blindly
987 * dereferences vp->v_data (as well it should).
989 MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
991 /* Allocate a new vnode/inode. */
992 if ((error = getnewvnode(VT_UFS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
993 if (ext2fs_inode_hash_lock < 0)
994 wakeup(&ext2fs_inode_hash_lock);
995 ext2fs_inode_hash_lock = 0;
997 FREE(ip, M_EXT2NODE);
1000 bzero((caddr_t)ip, sizeof(struct inode));
1003 ip->i_e2fs = fs = ump->um_e2fs;
1007 for (i = 0; i < MAXQUOTAS; i++)
1008 ip->i_dquot[i] = NODQUOT;
1011 * Put it onto its hash chain. Since our vnode is locked, other
1012 * requests for this inode will block if they arrive while we are
1013 * sleeping waiting for old data structures to be purged or for the
1014 * contents of the disk portion of this inode to be read.
1018 if (ext2fs_inode_hash_lock < 0)
1019 wakeup(&ext2fs_inode_hash_lock);
1020 ext2fs_inode_hash_lock = 0;
1022 /* Read in the disk contents for the inode, copy into the inode. */
1024 printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1026 error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1027 (int)fs->s_blocksize, &bp);
1030 * The inode does not contain anything useful, so it would
1031 * be misleading to leave it on its hash chain. With mode
1032 * still zero, it will be unlinked and returned to the free
1040 /* convert ext2 inode to dinode */
1041 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
1042 ino_to_fsbo(fs, ino)), &ip->i_din);
1043 ip->i_block_group = ino_to_cg(fs, ino);
1044 ip->i_next_alloc_block = 0;
1045 ip->i_next_alloc_goal = 0;
1046 ip->i_prealloc_count = 0;
1047 ip->i_prealloc_block = 0;
1048 /* now we want to make sure that block pointers for unused
1049 blocks are zeroed out - ext2_balloc depends on this
1050 although for regular files and directories only
1052 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1053 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1054 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1058 ext2_print_inode(ip);
1063 * Initialize the vnode from the inode, check for aliases.
1064 * Note that the underlying vnode may have changed.
1066 if ((error = ufs_vinit(mp, &vp)) != 0) {
1072 * Finish inode initialization now that aliasing has been resolved.
1074 ip->i_devvp = ump->um_devvp;
1077 * Set up a generation number for this inode if it does not
1078 * already have one. This should only happen on old filesystems.
1080 if (ip->i_gen == 0) {
1081 ip->i_gen = random() / 2 + 1;
1082 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1083 ip->i_flag |= IN_MODIFIED;
1086 * Return the locked and refd vnode.
1093 * File handle to vnode
1095 * Have to be really careful about stale file handles:
1096 * - check that the inode number is valid
1097 * - call ext2_vget() to get the locked inode
1098 * - check for an unallocated inode (i_mode == 0)
1099 * - check that the given client host has export rights and return
1100 * those rights via. exflagsp and credanonp
1103 ext2_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1106 struct ext2_sb_info *fs;
1108 ufhp = (struct ufid *)fhp;
1109 fs = VFSTOUFS(mp)->um_e2fs;
1110 if (ufhp->ufid_ino < ROOTINO ||
1111 ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1113 return (ufs_fhtovp(mp, ufhp, vpp));
1117 * Vnode pointer to File handle
1121 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1127 ufhp = (struct ufid *)fhp;
1128 ufhp->ufid_len = sizeof(struct ufid);
1129 ufhp->ufid_ino = ip->i_number;
1130 ufhp->ufid_gen = ip->i_gen;
1135 * Write a superblock and associated information back to disk.
1138 ext2_sbupdate(struct ufsmount *mp, int waitfor)
1140 struct ext2_sb_info *fs = mp->um_e2fs;
1141 struct ext2_super_block *es = fs->s_es;
1145 printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1147 bp = getblk(mp->um_devvp, lblktodoff(fs, SBLOCK), SBSIZE, 0, 0);
1148 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1149 if (waitfor == MNT_WAIT)
1155 * The buffers for group descriptors, inode bitmaps and block bitmaps
1156 * are not busy at this point and are (hopefully) written by the
1157 * usual sync mechanism. No need to write them here