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/namei.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
54 #include <sys/fcntl.h>
55 #include <sys/disklabel.h>
56 #include <sys/malloc.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_extern.h>
64 #include <vm/vm_zone.h>
66 #include <gnu/ext2fs/fs.h>
67 #include <gnu/ext2fs/ext2_extern.h>
68 #include <gnu/ext2fs/ext2_fs.h>
69 #include <gnu/ext2fs/ext2_fs_sb.h>
71 static int ext2_fhtovp __P((struct mount *, struct fid *, struct vnode **));
72 static int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p));
73 static int ext2_mount __P((struct mount *,
74 char *, caddr_t, struct nameidata *, struct proc *));
75 static int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *));
76 static int ext2_reload __P((struct mount *mountp, struct ucred *cred,
78 static int ext2_sbupdate __P((struct ufsmount *, int));
79 static int ext2_statfs __P((struct mount *, struct statfs *, struct proc *));
80 static int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *));
81 static int ext2_unmount __P((struct mount *, int, struct proc *));
82 static int ext2_vget __P((struct mount *, ino_t, struct vnode **));
83 static int ext2_vptofh __P((struct vnode *, struct fid *));
85 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
87 static struct vfsops ext2fs_vfsops = {
89 ufs_start, /* empty function */
91 ufs_root, /* root inode via vget */
92 ufs_quotactl, /* does operations associated with quotas */
104 VFS_SET(ext2fs_vfsops, ext2fs, 0);
105 #define bsd_malloc malloc
106 #define bsd_free free
108 static int ext2fs_inode_hash_lock;
110 static int ext2_check_sb_compat __P((struct ext2_super_block *es,
111 dev_t dev, int ronly));
112 static int compute_sb_data __P((struct vnode * devvp,
113 struct ext2_super_block * es,
114 struct ext2_sb_info * fs));
117 static int ext2_mountroot __P((void));
120 * Called by main() when ext2fs is going to be mounted as root.
122 * Name is updated by mount(8) after booting.
124 #define ROOTNAME "root_device"
129 register struct ext2_sb_info *fs;
130 register struct mount *mp;
131 struct proc *p = curproc;
132 struct ufsmount *ump;
136 if ((error = bdevvp(rootdev, &rootvp))) {
137 printf("ext2_mountroot: can't find rootvp\n");
140 mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
141 bzero((char *)mp, (u_long)sizeof(struct mount));
142 TAILQ_INIT(&mp->mnt_nvnodelist);
143 TAILQ_INIT(&mp->mnt_reservedvnlist);
144 mp->mnt_op = &ext2fs_vfsops;
145 mp->mnt_flag = MNT_RDONLY;
146 if (error = ext2_mountfs(rootvp, mp, p)) {
147 bsd_free(mp, M_MOUNT);
150 if (error = vfs_lock(mp)) {
151 (void)ext2_unmount(mp, 0, p);
152 bsd_free(mp, M_MOUNT);
155 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
156 mp->mnt_flag |= MNT_ROOTFS;
157 mp->mnt_vnodecovered = NULLVP;
160 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
161 fs->fs_fsmnt[0] = '/';
162 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
164 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
166 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
167 (void)ext2_statfs(mp, &mp->mnt_stat, p);
169 inittodr(fs->s_es->s_wtime); /* this helps to set the time */
180 ext2_mount(mp, path, data, ndp, p)
181 register struct mount *mp;
183 caddr_t data; /* this is actually a (struct ufs_args *) */
184 struct nameidata *ndp;
188 struct ufs_args args;
189 struct ufsmount *ump = 0;
190 register struct ext2_sb_info *fs;
195 if ((error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) != 0)
198 * If updating, check whether changing from read-only to
199 * read/write; if there is no device name, that's all we do.
201 if (mp->mnt_flag & MNT_UPDATE) {
205 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
207 if (mp->mnt_flag & MNT_FORCE)
209 if (vfs_busy(mp, LK_NOWAIT, 0, p))
211 error = ext2_flushfiles(mp, flags, p);
213 if (!error && fs->s_wasvalid) {
214 fs->s_es->s_state |= EXT2_VALID_FS;
215 ext2_sbupdate(ump, MNT_WAIT);
219 if (!error && (mp->mnt_flag & MNT_RELOAD))
220 error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p);
223 devvp = ump->um_devvp;
224 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
225 (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
227 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
229 * If upgrade to read-write by non-root, then verify
230 * that user has necessary permissions on the device.
232 if (p->p_ucred->cr_uid != 0) {
233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
234 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
235 p->p_ucred, p)) != 0) {
236 VOP_UNLOCK(devvp, 0, p);
239 VOP_UNLOCK(devvp, 0, p);
242 if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
243 (fs->s_es->s_state & EXT2_ERROR_FS)) {
244 if (mp->mnt_flag & MNT_FORCE) {
246 "WARNING: %s was not properly dismounted\n",
250 "WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck\n",
255 fs->s_es->s_state &= ~EXT2_VALID_FS;
256 ext2_sbupdate(ump, MNT_WAIT);
259 if (args.fspec == 0) {
261 * Process export requests.
263 return (vfs_export(mp, &ump->um_export, &args.export));
267 * Not an update, or updating the name: look up the name
268 * and verify that it refers to a sensible block device.
270 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
271 if ((error = namei(ndp)) != 0)
273 NDFREE(ndp, NDF_ONLY_PNBUF);
276 if (!vn_isdisk(devvp, &error)) {
282 * If mount by non-root, then verify that user has necessary
283 * permissions on the device.
285 if (p->p_ucred->cr_uid != 0) {
287 if ((mp->mnt_flag & MNT_RDONLY) == 0)
288 accessmode |= VWRITE;
289 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
290 if ((error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) != 0) {
294 VOP_UNLOCK(devvp, 0, p);
297 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
298 error = ext2_mountfs(devvp, mp, p);
300 if (devvp != ump->um_devvp)
301 error = EINVAL; /* needs translation */
311 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
312 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
313 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
315 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
317 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
318 (void)ext2_statfs(mp, &mp->mnt_stat, p);
323 * checks that the data in the descriptor blocks make sense
324 * this is taken from ext2/super.c
326 static int ext2_check_descriptors (struct ext2_sb_info * sb)
330 unsigned long block = sb->s_es->s_first_data_block;
331 struct ext2_group_desc * gdp = NULL;
333 /* ext2_debug ("Checking group descriptors"); */
335 for (i = 0; i < sb->s_groups_count; i++)
337 /* examine next descriptor block */
338 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
339 gdp = (struct ext2_group_desc *)
340 sb->s_group_desc[desc_block++]->b_data;
341 if (gdp->bg_block_bitmap < block ||
342 gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
344 printf ("ext2_check_descriptors: "
345 "Block bitmap for group %d"
346 " not in group (block %lu)!\n",
347 i, (unsigned long) gdp->bg_block_bitmap);
350 if (gdp->bg_inode_bitmap < block ||
351 gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
353 printf ("ext2_check_descriptors: "
354 "Inode bitmap for group %d"
355 " not in group (block %lu)!\n",
356 i, (unsigned long) gdp->bg_inode_bitmap);
359 if (gdp->bg_inode_table < block ||
360 gdp->bg_inode_table + sb->s_itb_per_group >=
361 block + EXT2_BLOCKS_PER_GROUP(sb))
363 printf ("ext2_check_descriptors: "
364 "Inode table for group %d"
365 " not in group (block %lu)!\n",
366 i, (unsigned long) gdp->bg_inode_table);
369 block += EXT2_BLOCKS_PER_GROUP(sb);
376 ext2_check_sb_compat(es, dev, ronly)
377 struct ext2_super_block *es;
382 if (es->s_magic != EXT2_SUPER_MAGIC) {
383 printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
384 devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
387 if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
388 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
390 "WARNING: mount of %s denied due to unsupported optional features\n",
395 (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
397 "WARNING: R/W mount of %s denied due to unsupported optional features\n",
406 * this computes the fields of the ext2_sb_info structure from the
407 * data in the ext2_super_block structure read in
409 static int compute_sb_data(devvp, es, fs)
410 struct vnode * devvp;
411 struct ext2_super_block * es;
412 struct ext2_sb_info * fs;
416 int logic_sb_block = 1; /* XXX for now */
421 #define V(v) printf(#v"= %d\n", fs->v);
424 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
426 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
428 fs->s_fsbtodb = es->s_log_block_size + 1;
430 fs->s_qbmask = fs->s_blocksize - 1;
432 fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
434 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
437 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
439 fs->s_blocks_per_group = es->s_blocks_per_group;
440 V(s_blocks_per_group)
441 fs->s_frags_per_group = es->s_frags_per_group;
443 fs->s_inodes_per_group = es->s_inodes_per_group;
444 V(s_inodes_per_group)
445 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
446 V(s_inodes_per_block)
447 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
449 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
451 /* s_resuid / s_resgid ? */
452 fs->s_groups_count = (es->s_blocks_count -
453 es->s_first_data_block +
454 EXT2_BLOCKS_PER_GROUP(fs) - 1) /
455 EXT2_BLOCKS_PER_GROUP(fs);
457 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
458 EXT2_DESC_PER_BLOCK(fs);
459 fs->s_db_per_group = db_count;
462 fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
465 /* adjust logic_sb_block */
466 if(fs->s_blocksize > SBSIZE)
467 /* Godmar thinks: if the blocksize is greater than 1024, then
468 the superblock is logically part of block zero.
472 for (i = 0; i < db_count; i++) {
473 error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
474 fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
476 for (j = 0; j < i; j++)
477 brelse(fs->s_group_desc[j]);
478 bsd_free(fs->s_group_desc, M_UFSMNT);
479 printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
482 /* Set the B_LOCKED flag on the buffer, then brelse() it */
483 LCK_BUF(fs->s_group_desc[i])
485 if(!ext2_check_descriptors(fs)) {
486 for (j = 0; j < db_count; j++)
487 ULCK_BUF(fs->s_group_desc[j])
488 bsd_free(fs->s_group_desc, M_UFSMNT);
489 printf("EXT2-fs: (ext2_check_descriptors failure) "
490 "unable to read group descriptors\n");
494 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
495 fs->s_inode_bitmap_number[i] = 0;
496 fs->s_inode_bitmap[i] = NULL;
497 fs->s_block_bitmap_number[i] = 0;
498 fs->s_block_bitmap[i] = NULL;
500 fs->s_loaded_inode_bitmaps = 0;
501 fs->s_loaded_block_bitmaps = 0;
506 * Reload all incore data for a filesystem (used after running fsck on
507 * the root filesystem and finding things to fix). The filesystem must
508 * be mounted read-only.
510 * Things to do to update the mount:
511 * 1) invalidate all cached meta-data.
512 * 2) re-read superblock from disk.
513 * 3) re-read summary information from disk.
514 * 4) invalidate all inactive vnodes.
515 * 5) invalidate all cached file data.
516 * 6) re-read inode data for all active vnodes.
519 ext2_reload(mountp, cred, p)
520 register struct mount *mountp;
524 register struct vnode *vp, *nvp, *devvp;
527 struct ext2_super_block * es;
528 struct ext2_sb_info *fs;
531 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
534 * Step 1: invalidate all cached meta-data.
536 devvp = VFSTOUFS(mountp)->um_devvp;
537 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
538 panic("ext2_reload: dirty1");
540 * Step 2: re-read superblock from disk.
541 * constants have been adjusted for ext2
543 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
545 es = (struct ext2_super_block *)bp->b_data;
546 if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
548 return (EIO); /* XXX needs translation */
550 fs = VFSTOUFS(mountp)->um_e2fs;
551 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
553 if((error = compute_sb_data(devvp, es, fs)) != 0) {
558 if (fs->fs_sbsize < SBSIZE)
559 bp->b_flags |= B_INVAL;
564 simple_lock(&mntvnode_slock);
565 for (vp = TAILQ_FIRST(&mountp->mnt_nvnodelist); vp != NULL; vp = nvp) {
566 if (vp->v_mount != mountp) {
567 simple_unlock(&mntvnode_slock);
570 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
572 * Step 4: invalidate all inactive vnodes.
574 if (vrecycle(vp, &mntvnode_slock, p))
577 * Step 5: invalidate all cached file data.
579 simple_lock(&vp->v_interlock);
580 simple_unlock(&mntvnode_slock);
581 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
584 if (vinvalbuf(vp, 0, cred, p, 0, 0))
585 panic("ext2_reload: dirty2");
587 * Step 6: re-read inode data for all active vnodes.
591 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
592 (int)fs->s_blocksize, NOCRED, &bp);
597 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
598 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)),
602 simple_lock(&mntvnode_slock);
604 simple_unlock(&mntvnode_slock);
609 * Common code for mount and mountroot
612 ext2_mountfs(devvp, mp, p)
613 register struct vnode *devvp;
617 register struct ufsmount *ump;
619 register struct ext2_sb_info *fs;
620 struct ext2_super_block * es;
621 dev_t dev = devvp->v_rdev;
622 struct partinfo dpart;
628 * Disallow multiple mounts of the same device.
629 * Disallow mounting of a device that is currently in use
630 * (except for root, which might share swap device for miniroot).
631 * Flush out any old buffers remaining from a previous use.
633 if ((error = vfs_mountedon(devvp)) != 0)
635 if (vcount(devvp) > 1 && devvp != rootvp)
637 if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
640 /* turn on this to force it to be read-only */
641 mp->mnt_flag |= MNT_RDONLY;
644 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
645 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
646 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
647 VOP_UNLOCK(devvp, 0, p);
650 if (devvp->v_rdev->si_iosize_max != 0)
651 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
652 if (mp->mnt_iosize_max > MAXPHYS)
653 mp->mnt_iosize_max = MAXPHYS;
654 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
658 size = dpart.disklab->d_secsize;
663 if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
665 es = (struct ext2_super_block *)bp->b_data;
666 if (ext2_check_sb_compat(es, dev, ronly) != 0) {
667 error = EINVAL; /* XXX needs translation */
670 if ((es->s_state & EXT2_VALID_FS) == 0 ||
671 (es->s_state & EXT2_ERROR_FS)) {
672 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
674 "WARNING: Filesystem was not properly dismounted\n");
677 "WARNING: R/W mount denied. Filesystem is not clean - run fsck\n");
682 ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
683 bzero((caddr_t)ump, sizeof *ump);
684 ump->um_malloctype = M_EXT2NODE;
685 ump->um_blkatoff = ext2_blkatoff;
686 ump->um_truncate = ext2_truncate;
687 ump->um_update = ext2_update;
688 ump->um_valloc = ext2_valloc;
689 ump->um_vfree = ext2_vfree;
690 /* I don't know whether this is the right strategy. Note that
691 we dynamically allocate both a ext2_sb_info and a ext2_super_block
692 while Linux keeps the super block in a locked buffer
694 ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
696 ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
698 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
699 if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
702 * We don't free the group descriptors allocated by compute_sb_data()
703 * until ext2_unmount(). This is OK since the mount will succeed.
708 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
709 /* if the fs is not mounted read-only, make sure the super block is
710 always written back on a sync()
712 fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
714 fs->s_dirt = 1; /* mark it modified */
715 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
717 mp->mnt_data = (qaddr_t)ump;
718 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
719 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
720 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
721 mp->mnt_flag |= MNT_LOCAL;
724 ump->um_devvp = devvp;
725 /* setting those two parameters allows us to use
726 ufs_bmap w/o changse !
728 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
729 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
730 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
731 for (i = 0; i < MAXQUOTAS; i++)
732 ump->um_quotas[i] = NULLVP;
733 devvp->v_specmountpoint = mp;
735 ext2_sbupdate(ump, MNT_WAIT);
740 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
742 bsd_free(ump->um_e2fs->s_es, M_UFSMNT);
743 bsd_free(ump->um_e2fs, M_UFSMNT);
744 bsd_free(ump, M_UFSMNT);
745 mp->mnt_data = (qaddr_t)0;
751 * unmount system call
754 ext2_unmount(mp, mntflags, p)
759 register struct ufsmount *ump;
760 register struct ext2_sb_info *fs;
761 int error, flags, ronly, i;
764 if (mntflags & MNT_FORCE) {
765 if (mp->mnt_flag & MNT_ROOTFS)
769 if ((error = ext2_flushfiles(mp, flags, p)) != 0)
773 ronly = fs->s_rd_only;
776 fs->s_es->s_state |= EXT2_VALID_FS;
777 ext2_sbupdate(ump, MNT_WAIT);
780 /* release buffers containing group descriptors */
781 for(i = 0; i < fs->s_db_per_group; i++)
782 ULCK_BUF(fs->s_group_desc[i])
783 bsd_free(fs->s_group_desc, M_UFSMNT);
785 /* release cached inode/block bitmaps */
786 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
787 if (fs->s_inode_bitmap[i])
788 ULCK_BUF(fs->s_inode_bitmap[i])
790 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
791 if (fs->s_block_bitmap[i])
792 ULCK_BUF(fs->s_block_bitmap[i])
794 ump->um_devvp->v_specmountpoint = NULL;
795 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
797 vrele(ump->um_devvp);
798 bsd_free(fs->s_es, M_UFSMNT);
799 bsd_free(fs, M_UFSMNT);
800 bsd_free(ump, M_UFSMNT);
801 mp->mnt_data = (qaddr_t)0;
802 mp->mnt_flag &= ~MNT_LOCAL;
807 * Flush out all the files in a filesystem.
810 ext2_flushfiles(mp, flags, p)
811 register struct mount *mp;
815 register struct ufsmount *ump;
823 if (mp->mnt_flag & MNT_QUOTA) {
824 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
826 for (i = 0; i < MAXQUOTAS; i++) {
827 if (ump->um_quotas[i] == NULLVP)
832 * Here we fall through to vflush again to ensure
833 * that we have gotten rid of all the system vnodes.
837 error = vflush(mp, 0, flags);
842 * Get file system statistics.
843 * taken from ext2/super.c ext2_statfs
846 ext2_statfs(mp, sbp, p)
848 register struct statfs *sbp;
851 unsigned long overhead;
852 register struct ufsmount *ump;
853 register struct ext2_sb_info *fs;
854 register struct ext2_super_block *es;
861 if (es->s_magic != EXT2_SUPER_MAGIC)
862 panic("ext2_statfs - magic number spoiled");
865 * Compute the overhead (FS structures)
867 if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
869 for (i = 0 ; i < fs->s_groups_count; i++)
870 if (ext2_group_sparse(i))
873 nsb = fs->s_groups_count;
874 overhead = es->s_first_data_block +
875 /* Superblocks and block group descriptors: */
876 nsb * (1 + fs->s_db_per_group) +
877 /* Inode bitmap, block bitmap, and inode table: */
878 fs->s_groups_count * (1 + 1 + fs->s_itb_per_group);
880 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
881 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
882 sbp->f_blocks = es->s_blocks_count - overhead;
883 sbp->f_bfree = es->s_free_blocks_count;
884 sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
885 sbp->f_files = es->s_inodes_count;
886 sbp->f_ffree = es->s_free_inodes_count;
887 if (sbp != &mp->mnt_stat) {
888 sbp->f_type = mp->mnt_vfc->vfc_typenum;
889 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
890 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
891 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
892 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
898 * Go through the disk queues to initiate sandbagged IO;
899 * go through the inodes to write those that have been modified;
900 * initiate the writing of the super block if it has been modified.
902 * Note: we are always called with the filesystem marked `MPBUSY'.
905 ext2_sync(mp, waitfor, cred, p)
911 struct vnode *nvp, *vp;
913 struct ufsmount *ump = VFSTOUFS(mp);
914 struct ext2_sb_info *fs;
915 int error, allerror = 0;
918 if (fs->s_dirt != 0 && fs->s_rd_only != 0) { /* XXX */
919 printf("fs = %s\n", fs->fs_fsmnt);
920 panic("ext2_sync: rofs mod");
923 * Write back each (modified) inode.
925 simple_lock(&mntvnode_slock);
927 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
929 * If the vnode that we are about to sync is no longer
930 * associated with this mount point, start over.
932 if (vp->v_mount != mp)
934 simple_lock(&vp->v_interlock);
935 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
937 if (vp->v_type == VNON ||
939 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
940 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
941 simple_unlock(&vp->v_interlock);
944 simple_unlock(&mntvnode_slock);
945 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
947 simple_lock(&mntvnode_slock);
952 if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
954 VOP_UNLOCK(vp, 0, p);
956 simple_lock(&mntvnode_slock);
958 simple_unlock(&mntvnode_slock);
960 * Force stale file system control information to be flushed.
962 if (waitfor != MNT_LAZY) {
963 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
964 if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
966 VOP_UNLOCK(ump->um_devvp, 0, p);
972 * Write back modified superblock.
974 if (fs->s_dirt != 0) {
976 fs->s_es->s_wtime = time_second;
977 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
984 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
985 * in from disk. If it is in core, wait for the lock bit to clear, then
986 * return the inode locked. Detection and handling of mount points must be
987 * done by the calling routine.
990 ext2_vget(mp, ino, vpp)
995 register struct ext2_sb_info *fs;
996 register struct inode *ip;
997 struct ufsmount *ump;
1007 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
1011 * Lock out the creation of new entries in the FFS hash table in
1012 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1015 if (ext2fs_inode_hash_lock) {
1016 while (ext2fs_inode_hash_lock) {
1017 ext2fs_inode_hash_lock = -1;
1018 tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0);
1022 ext2fs_inode_hash_lock = 1;
1025 * If this MALLOC() is performed after the getnewvnode()
1026 * it might block, leaving a vnode with a NULL v_data to be
1027 * found by ext2_sync() if a sync happens to fire right then,
1028 * which will cause a panic because ext2_sync() blindly
1029 * dereferences vp->v_data (as well it should).
1031 MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1033 /* Allocate a new vnode/inode. */
1034 if ((error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) != 0) {
1035 if (ext2fs_inode_hash_lock < 0)
1036 wakeup(&ext2fs_inode_hash_lock);
1037 ext2fs_inode_hash_lock = 0;
1039 FREE(ip, M_EXT2NODE);
1042 bzero((caddr_t)ip, sizeof(struct inode));
1043 lockinit(&ip->i_lock, PINOD, "ext2in", 0, 0);
1046 ip->i_e2fs = fs = ump->um_e2fs;
1050 for (i = 0; i < MAXQUOTAS; i++)
1051 ip->i_dquot[i] = NODQUOT;
1054 * Put it onto its hash chain and lock it so that other requests for
1055 * this inode will block if they arrive while we are sleeping waiting
1056 * for old data structures to be purged or for the contents of the
1057 * disk portion of this inode to be read.
1061 if (ext2fs_inode_hash_lock < 0)
1062 wakeup(&ext2fs_inode_hash_lock);
1063 ext2fs_inode_hash_lock = 0;
1065 /* Read in the disk contents for the inode, copy into the inode. */
1067 printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1069 if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1070 (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
1072 * The inode does not contain anything useful, so it would
1073 * be misleading to leave it on its hash chain. With mode
1074 * still zero, it will be unlinked and returned to the free
1082 /* convert ext2 inode to dinode */
1083 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
1084 ino_to_fsbo(fs, ino)), &ip->i_din);
1085 ip->i_block_group = ino_to_cg(fs, ino);
1086 ip->i_next_alloc_block = 0;
1087 ip->i_next_alloc_goal = 0;
1088 ip->i_prealloc_count = 0;
1089 ip->i_prealloc_block = 0;
1090 /* now we want to make sure that block pointers for unused
1091 blocks are zeroed out - ext2_balloc depends on this
1092 although for regular files and directories only
1094 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1095 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1096 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1100 ext2_print_inode(ip);
1105 * Initialize the vnode from the inode, check for aliases.
1106 * Note that the underlying vnode may have changed.
1108 if ((error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) != 0) {
1114 * Finish inode initialization now that aliasing has been resolved.
1116 ip->i_devvp = ump->um_devvp;
1119 * Set up a generation number for this inode if it does not
1120 * already have one. This should only happen on old filesystems.
1122 if (ip->i_gen == 0) {
1123 ip->i_gen = random() / 2 + 1;
1124 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1125 ip->i_flag |= IN_MODIFIED;
1132 * File handle to vnode
1134 * Have to be really careful about stale file handles:
1135 * - check that the inode number is valid
1136 * - call ext2_vget() to get the locked inode
1137 * - check for an unallocated inode (i_mode == 0)
1138 * - check that the given client host has export rights and return
1139 * those rights via. exflagsp and credanonp
1142 ext2_fhtovp(mp, fhp, vpp)
1143 register struct mount *mp;
1147 register struct ufid *ufhp;
1148 struct ext2_sb_info *fs;
1150 ufhp = (struct ufid *)fhp;
1151 fs = VFSTOUFS(mp)->um_e2fs;
1152 if (ufhp->ufid_ino < ROOTINO ||
1153 ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1155 return (ufs_fhtovp(mp, ufhp, vpp));
1159 * Vnode pointer to File handle
1163 ext2_vptofh(vp, fhp)
1167 register struct inode *ip;
1168 register struct ufid *ufhp;
1171 ufhp = (struct ufid *)fhp;
1172 ufhp->ufid_len = sizeof(struct ufid);
1173 ufhp->ufid_ino = ip->i_number;
1174 ufhp->ufid_gen = ip->i_gen;
1179 * Write a superblock and associated information back to disk.
1182 ext2_sbupdate(mp, waitfor)
1183 struct ufsmount *mp;
1186 register struct ext2_sb_info *fs = mp->um_e2fs;
1187 register struct ext2_super_block *es = fs->s_es;
1188 register struct buf *bp;
1191 printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1193 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1194 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1195 if (waitfor == MNT_WAIT)
1201 * The buffers for group descriptors, inode bitmaps and block bitmaps
1202 * are not busy at this point and are (hopefully) written by the
1203 * usual sync mechanism. No need to write them here