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
42 #if !defined(__FreeBSD__)
45 #include "opt_quota.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/namei.h>
52 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
57 #include <sys/fcntl.h>
58 #include <sys/disklabel.h>
59 #include <sys/malloc.h>
62 #include <miscfs/specfs/specdev.h>
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufs_extern.h>
69 #include <gnu/ext2fs/fs.h>
70 #include <gnu/ext2fs/ext2_extern.h>
71 #include <gnu/ext2fs/ext2_fs.h>
72 #include <gnu/ext2fs/ext2_fs_sb.h>
74 static int ext2_fhtovp __P((struct mount *, struct fid *, struct sockaddr *,
75 struct vnode **, int *, struct ucred **));
76 static int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p));
77 static int ext2_mount __P((struct mount *,
78 char *, caddr_t, struct nameidata *, struct proc *));
79 static int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *));
80 static int ext2_reload __P((struct mount *mountp, struct ucred *cred,
82 static int ext2_sbupdate __P((struct ufsmount *, int));
83 static int ext2_statfs __P((struct mount *, struct statfs *, struct proc *));
84 static int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *));
85 static int ext2_unmount __P((struct mount *, int, struct proc *));
86 static int ext2_vget __P((struct mount *, ino_t, struct vnode **));
87 static int ext2_vptofh __P((struct vnode *, struct fid *));
89 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
91 static struct vfsops ext2fs_vfsops = {
93 ufs_start, /* empty function */
95 ufs_root, /* root inode via vget */
96 ufs_quotactl, /* does operations associated with quotas */
105 #if defined(__FreeBSD__)
106 VFS_SET(ext2fs_vfsops, ext2fs, MOUNT_EXT2FS, 0);
107 #define bsd_malloc malloc
108 #define bsd_free free
111 extern u_long nextgennumber;
113 static int ext2fs_inode_hash_lock;
116 static int compute_sb_data __P((struct vnode * devvp,
117 struct ext2_super_block * es,
118 struct ext2_sb_info * fs));
121 static int ext2_mountroot __P((void));
124 * Called by main() when ext2fs is going to be mounted as root.
126 * Name is updated by mount(8) after booting.
128 #define ROOTNAME "root_device"
133 #if !defined(__FreeBSD__)
134 extern struct vnode *rootvp;
136 register struct ext2_sb_info *fs;
137 register struct mount *mp;
138 #if defined(__FreeBSD__)
139 struct proc *p = curproc;
141 struct proc *p = get_proc(); /* XXX */
143 struct ufsmount *ump;
147 if ((error = bdevvp(rootdev, &rootvp))) {
148 printf("ext2_mountroot: can't find rootvp");
151 mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
152 bzero((char *)mp, (u_long)sizeof(struct mount));
153 mp->mnt_op = &ext2fs_vfsops;
154 mp->mnt_flag = MNT_RDONLY;
155 if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERR)
156 mp->mnt_flag |= MNT_NOCLUSTERR;
157 if (bdevsw[major(rootdev)]->d_flags & D_NOCLUSTERW)
158 mp->mnt_flag |= MNT_NOCLUSTERW;
159 if (error = ext2_mountfs(rootvp, mp, p)) {
160 bsd_free(mp, M_MOUNT);
163 if (error = vfs_lock(mp)) {
164 (void)ext2_unmount(mp, 0, p);
165 bsd_free(mp, M_MOUNT);
168 #if defined(__FreeBSD__)
169 CIRCLEQ_INSERT_HEAD(&mountlist, mp, mnt_list);
171 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
173 mp->mnt_flag |= MNT_ROOTFS;
174 mp->mnt_vnodecovered = NULLVP;
177 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
178 fs->fs_fsmnt[0] = '/';
179 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
181 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
183 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
184 (void)ext2_statfs(mp, &mp->mnt_stat, p);
186 inittodr(fs->s_es->s_wtime); /* this helps to set the time */
197 ext2_mount(mp, path, data, ndp, p)
198 register struct mount *mp;
200 caddr_t data; /* this is actually a (struct ufs_args *) */
201 struct nameidata *ndp;
205 struct ufs_args args;
206 struct ufsmount *ump = 0;
207 register struct ext2_sb_info *fs;
211 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
214 * If updating, check whether changing from read-only to
215 * read/write; if there is no device name, that's all we do.
216 * Disallow clearing MNT_NOCLUSTERR and MNT_NOCLUSTERW flags,
217 * if block device requests.
219 if (mp->mnt_flag & MNT_UPDATE) {
223 if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERR)
224 mp->mnt_flag |= MNT_NOCLUSTERR;
225 if (bdevsw[major(ump->um_dev)]->d_flags & D_NOCLUSTERW)
226 mp->mnt_flag |= MNT_NOCLUSTERW;
227 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
229 if (mp->mnt_flag & MNT_FORCE)
231 if (vfs_busy(mp, LK_NOWAIT, 0, p))
233 error = ext2_flushfiles(mp, flags, p);
236 if (!error && (mp->mnt_flag & MNT_RELOAD))
237 error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p);
240 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR))
242 if (fs->s_rd_only == 0) {
243 /* don't say it's clean */
244 fs->s_es->s_state &= ~EXT2_VALID_FS;
245 ext2_sbupdate(ump, MNT_WAIT);
247 if (args.fspec == 0) {
249 * Process export requests.
251 return (vfs_export(mp, &ump->um_export, &args.export));
255 * Not an update, or updating the name: look up the name
256 * and verify that it refers to a sensible block device.
258 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
259 if (error = namei(ndp))
263 if (devvp->v_type != VBLK) {
267 if (major(devvp->v_rdev) >= nblkdev) {
271 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
272 if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERR)
273 mp->mnt_flag |= MNT_NOCLUSTERR;
274 if (bdevsw[major(devvp->v_rdev)]->d_flags & D_NOCLUSTERW)
275 mp->mnt_flag |= MNT_NOCLUSTERW;
276 error = ext2_mountfs(devvp, mp, p);
278 if (devvp != ump->um_devvp)
279 error = EINVAL; /* needs translation */
289 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
290 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
291 bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
293 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
295 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
296 (void)ext2_statfs(mp, &mp->mnt_stat, p);
301 * checks that the data in the descriptor blocks make sense
302 * this is taken from ext2/super.c
304 static int ext2_check_descriptors (struct ext2_sb_info * sb)
308 unsigned long block = sb->s_es->s_first_data_block;
309 struct ext2_group_desc * gdp = NULL;
311 /* ext2_debug ("Checking group descriptors"); */
313 for (i = 0; i < sb->s_groups_count; i++)
315 /* examine next descriptor block */
316 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
317 gdp = (struct ext2_group_desc *)
318 sb->s_group_desc[desc_block++]->b_data;
319 if (gdp->bg_block_bitmap < block ||
320 gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
322 printf ("ext2_check_descriptors: "
323 "Block bitmap for group %d"
324 " not in group (block %lu)!",
325 i, (unsigned long) gdp->bg_block_bitmap);
328 if (gdp->bg_inode_bitmap < block ||
329 gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
331 printf ("ext2_check_descriptors: "
332 "Inode bitmap for group %d"
333 " not in group (block %lu)!",
334 i, (unsigned long) gdp->bg_inode_bitmap);
337 if (gdp->bg_inode_table < block ||
338 gdp->bg_inode_table + sb->s_itb_per_group >=
339 block + EXT2_BLOCKS_PER_GROUP(sb))
341 printf ("ext2_check_descriptors: "
342 "Inode table for group %d"
343 " not in group (block %lu)!",
344 i, (unsigned long) gdp->bg_inode_table);
347 block += EXT2_BLOCKS_PER_GROUP(sb);
354 * this computes the fields of the ext2_sb_info structure from the
355 * data in the ext2_super_block structure read in
357 static int compute_sb_data(devvp, es, fs)
358 struct vnode * devvp;
359 struct ext2_super_block * es;
360 struct ext2_sb_info * fs;
364 int logic_sb_block = 1; /* XXX for now */
369 #define V(v) printf(#v"= %d\n", fs->v);
372 fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
374 fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
376 fs->s_fsbtodb = es->s_log_block_size + 1;
378 fs->s_qbmask = fs->s_blocksize - 1;
380 fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
382 fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
385 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
387 fs->s_blocks_per_group = es->s_blocks_per_group;
388 V(s_blocks_per_group)
389 fs->s_frags_per_group = es->s_frags_per_group;
391 fs->s_inodes_per_group = es->s_inodes_per_group;
392 V(s_inodes_per_group)
393 fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
394 V(s_inodes_per_block)
395 fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
397 fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
399 /* s_resuid / s_resgid ? */
400 fs->s_groups_count = (es->s_blocks_count -
401 es->s_first_data_block +
402 EXT2_BLOCKS_PER_GROUP(fs) - 1) /
403 EXT2_BLOCKS_PER_GROUP(fs);
405 db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
406 EXT2_DESC_PER_BLOCK(fs);
407 fs->s_db_per_group = db_count;
410 fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
413 /* adjust logic_sb_block */
414 if(fs->s_blocksize > SBSIZE)
415 /* Godmar thinks: if the blocksize is greater than 1024, then
416 the superblock is logically part of block zero.
420 for (i = 0; i < db_count; i++) {
421 error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
422 fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
424 for (j = 0; j < i; j++)
425 brelse(fs->s_group_desc[j]);
426 bsd_free(fs->s_group_desc, M_UFSMNT);
427 printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
430 /* Set the B_LOCKED flag on the buffer, then brelse() it */
431 LCK_BUF(fs->s_group_desc[i])
433 if(!ext2_check_descriptors(fs)) {
434 for (j = 0; j < db_count; j++)
435 ULCK_BUF(fs->s_group_desc[j])
436 bsd_free(fs->s_group_desc, M_UFSMNT);
437 printf("EXT2-fs: (ext2_check_descriptors failure) "
438 "unable to read group descriptors\n");
442 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
443 fs->s_inode_bitmap_number[i] = 0;
444 fs->s_inode_bitmap[i] = NULL;
445 fs->s_block_bitmap_number[i] = 0;
446 fs->s_block_bitmap[i] = NULL;
448 fs->s_loaded_inode_bitmaps = 0;
449 fs->s_loaded_block_bitmaps = 0;
454 * Reload all incore data for a filesystem (used after running fsck on
455 * the root filesystem and finding things to fix). The filesystem must
456 * be mounted read-only.
458 * Things to do to update the mount:
459 * 1) invalidate all cached meta-data.
460 * 2) re-read superblock from disk.
461 * 3) re-read summary information from disk.
462 * 4) invalidate all inactive vnodes.
463 * 5) invalidate all cached file data.
464 * 6) re-read inode data for all active vnodes.
467 ext2_reload(mountp, cred, p)
468 register struct mount *mountp;
472 register struct vnode *vp, *nvp, *devvp;
475 struct ext2_super_block * es;
476 struct ext2_sb_info *fs;
479 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
482 * Step 1: invalidate all cached meta-data.
484 devvp = VFSTOUFS(mountp)->um_devvp;
485 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
486 panic("ext2_reload: dirty1");
488 * Step 2: re-read superblock from disk.
489 * constants have been adjusted for ext2
491 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
493 es = (struct ext2_super_block *)bp->b_data;
494 if (es->s_magic != EXT2_SUPER_MAGIC) {
495 if(es->s_magic == EXT2_PRE_02B_MAGIC)
496 printf("This filesystem bears the magic number of a pre "
497 "0.2b version of ext2. This is not supported by "
500 printf("Wrong magic number: %x (expected %x for ext2 fs\n",
501 es->s_magic, EXT2_SUPER_MAGIC);
503 return (EIO); /* XXX needs translation */
505 fs = VFSTOUFS(mountp)->um_e2fs;
506 bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
508 if(error = compute_sb_data(devvp, es, fs)) {
513 if (fs->fs_sbsize < SBSIZE)
514 bp->b_flags |= B_INVAL;
519 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
520 nvp = vp->v_mntvnodes.le_next;
522 * Step 4: invalidate all inactive vnodes.
524 if (vp->v_usecount == 0) {
529 * Step 5: invalidate all cached file data.
531 if (vget(vp, LK_EXCLUSIVE, p))
533 if (vinvalbuf(vp, 0, cred, p, 0, 0))
534 panic("ext2_reload: dirty2");
536 * Step 6: re-read inode data for all active vnodes.
540 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
541 (int)fs->s_blocksize, NOCRED, &bp)) {
545 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
546 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)),
550 if (vp->v_mount != mountp)
557 * Common code for mount and mountroot
560 ext2_mountfs(devvp, mp, p)
561 register struct vnode *devvp;
565 register struct ufsmount *ump;
567 register struct ext2_sb_info *fs;
568 struct ext2_super_block * es;
569 dev_t dev = devvp->v_rdev;
570 struct partinfo dpart;
574 #if !defined(__FreeBSD__)
575 extern struct vnode *rootvp;
579 * Disallow multiple mounts of the same device.
580 * Disallow mounting of a device that is currently in use
581 * (except for root, which might share swap device for miniroot).
582 * Flush out any old buffers remaining from a previous use.
584 if (error = vfs_mountedon(devvp))
586 if (vcount(devvp) > 1 && devvp != rootvp)
588 if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
591 /* turn on this to force it to be read-only */
592 mp->mnt_flag |= MNT_RDONLY;
595 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
596 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
598 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
602 size = dpart.disklab->d_secsize;
607 if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
609 es = (struct ext2_super_block *)bp->b_data;
610 if (es->s_magic != EXT2_SUPER_MAGIC) {
611 if(es->s_magic == EXT2_PRE_02B_MAGIC)
612 printf("This filesystem bears the magic number of a pre "
613 "0.2b version of ext2. This is not supported by "
616 printf("Wrong magic number: %x (expected %x for EXT2FS)\n",
617 es->s_magic, EXT2_SUPER_MAGIC);
618 error = EINVAL; /* XXX needs translation */
621 ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
622 bzero((caddr_t)ump, sizeof *ump);
623 ump->um_malloctype = M_EXT2NODE;
624 ump->um_blkatoff = ext2_blkatoff;
625 ump->um_truncate = ext2_truncate;
626 ump->um_update = ext2_update;
627 ump->um_valloc = ext2_valloc;
628 ump->um_vfree = ext2_vfree;
629 /* I don't know whether this is the right strategy. Note that
630 we dynamically allocate both a ext2_sb_info and a ext2_super_block
631 while Linux keeps the super block in a locked buffer
633 ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
635 ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
637 bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
638 if(error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)) {
645 fs->s_rd_only = ronly; /* ronly is set according to mnt_flags */
646 if (!(fs->s_es->s_state & EXT2_VALID_FS)) {
647 printf("WARNING: %s was not properly dismounted\n",
650 /* if the fs is not mounted read-only, make sure the super block is
651 always written back on a sync()
654 fs->s_dirt = 1; /* mark it modified */
655 fs->s_es->s_state &= ~EXT2_VALID_FS; /* set fs invalid */
657 mp->mnt_data = (qaddr_t)ump;
658 mp->mnt_stat.f_fsid.val[0] = (long)dev;
659 mp->mnt_stat.f_fsid.val[1] = MOUNT_EXT2FS;
660 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
661 mp->mnt_flag |= MNT_LOCAL;
664 ump->um_devvp = devvp;
665 /* setting those two parameters allows us to use
666 ufs_bmap w/o changse !
668 ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
669 ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
670 ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
671 for (i = 0; i < MAXQUOTAS; i++)
672 ump->um_quotas[i] = NULLVP;
673 devvp->v_specflags |= SI_MOUNTEDON;
675 ext2_sbupdate(ump, MNT_WAIT);
680 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
682 bsd_free(ump->um_fs, M_UFSMNT);
683 bsd_free(ump, M_UFSMNT);
684 mp->mnt_data = (qaddr_t)0;
690 * unmount system call
693 ext2_unmount(mp, mntflags, p)
698 register struct ufsmount *ump;
699 register struct ext2_sb_info *fs;
700 int error, flags, ronly, i;
703 if (mntflags & MNT_FORCE) {
704 if (mp->mnt_flag & MNT_ROOTFS)
708 if (error = ext2_flushfiles(mp, flags, p))
712 ronly = fs->s_rd_only;
714 fs->s_es->s_state |= EXT2_VALID_FS; /* was fs_clean = 1 */
715 ext2_sbupdate(ump, MNT_WAIT);
718 /* release buffers containing group descriptors */
719 for(i = 0; i < fs->s_db_per_group; i++)
720 ULCK_BUF(fs->s_group_desc[i])
722 /* release cached inode/block bitmaps */
723 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
724 if (fs->s_inode_bitmap[i])
725 ULCK_BUF(fs->s_inode_bitmap[i])
727 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
728 if (fs->s_block_bitmap[i])
729 ULCK_BUF(fs->s_block_bitmap[i])
731 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
732 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
734 vrele(ump->um_devvp);
735 bsd_free(fs->s_es, M_UFSMNT);
736 bsd_free(fs, M_UFSMNT);
737 bsd_free(ump, M_UFSMNT);
738 mp->mnt_data = (qaddr_t)0;
739 mp->mnt_flag &= ~MNT_LOCAL;
744 * Flush out all the files in a filesystem.
747 ext2_flushfiles(mp, flags, p)
748 register struct mount *mp;
752 register struct ufsmount *ump;
760 if (mp->mnt_flag & MNT_QUOTA) {
761 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
763 for (i = 0; i < MAXQUOTAS; i++) {
764 if (ump->um_quotas[i] == NULLVP)
769 * Here we fall through to vflush again to ensure
770 * that we have gotten rid of all the system vnodes.
774 error = vflush(mp, NULLVP, flags);
779 * Get file system statistics.
780 * taken from ext2/super.c ext2_statfs
783 ext2_statfs(mp, sbp, p)
785 register struct statfs *sbp;
788 unsigned long overhead;
789 unsigned long overhead_per_group;
791 register struct ufsmount *ump;
792 register struct ext2_sb_info *fs;
793 register struct ext2_super_block *es;
799 if (es->s_magic != EXT2_SUPER_MAGIC)
800 panic("ext2_statfs - magic number spoiled");
803 * Compute the overhead (FS structures)
805 overhead_per_group = 1 /* super block */ +
807 1 /* block bitmap */ +
808 1 /* inode bitmap */ +
810 overhead = es->s_first_data_block +
811 fs->s_groups_count * overhead_per_group;
813 sbp->f_type = MOUNT_EXT2FS;
814 sbp->f_bsize = EXT2_FRAG_SIZE(fs);
815 sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
816 sbp->f_blocks = es->s_blocks_count - overhead;
817 sbp->f_bfree = es->s_free_blocks_count;
818 sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
819 sbp->f_files = es->s_inodes_count;
820 sbp->f_ffree = es->s_free_inodes_count;
821 if (sbp != &mp->mnt_stat) {
822 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
823 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
824 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
825 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
831 * Go through the disk queues to initiate sandbagged IO;
832 * go through the inodes to write those that have been modified;
833 * initiate the writing of the super block if it has been modified.
835 * Note: we are always called with the filesystem marked `MPBUSY'.
838 ext2_sync(mp, waitfor, cred, p)
844 register struct vnode *vp;
845 register struct inode *ip;
846 register struct ufsmount *ump = VFSTOUFS(mp);
847 register struct ext2_sb_info *fs;
848 int error, allerror = 0;
852 * Write back modified superblock.
853 * Consistency check that the superblock
854 * is still in the buffer cache.
857 #if !defined(__FreeBSD__)
861 if (fs->s_rd_only != 0) { /* XXX */
862 printf("fs = %s\n", fs->fs_fsmnt);
863 panic("update: rofs mod");
866 #if !defined(__FreeBSD__)
869 fs->s_es->s_wtime = time.tv_sec;
870 allerror = ext2_sbupdate(ump, waitfor);
873 * Write back each (modified) inode.
876 for (vp = mp->mnt_vnodelist.lh_first;
878 vp = vp->v_mntvnodes.le_next) {
880 * If the vnode that we are about to sync is no longer
881 * associated with this mount point, start over.
883 if (vp->v_mount != mp)
885 if (VOP_ISLOCKED(vp))
889 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
890 vp->v_dirtyblkhd.lh_first == NULL)
892 if (vget(vp, LK_EXCLUSIVE, p))
894 if (error = VOP_FSYNC(vp, cred, waitfor, p))
899 * Force stale file system control information to be flushed.
901 if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
910 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
911 * in from disk. If it is in core, wait for the lock bit to clear, then
912 * return the inode locked. Detection and handling of mount points must be
913 * done by the calling routine.
916 ext2_vget(mp, ino, vpp)
921 register struct ext2_sb_info *fs;
922 register struct inode *ip;
923 struct ufsmount *ump;
933 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
938 * Lock out the creation of new entries in the FFS hash table in
939 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
942 if (ext2fs_inode_hash_lock) {
943 while (ext2fs_inode_hash_lock) {
944 ext2fs_inode_hash_lock = -1;
945 tsleep(&ext2fs_inode_hash_lock, PVM, "ffsvgt", 0);
949 ext2fs_inode_hash_lock = 1;
952 /* Allocate a new vnode/inode. */
953 if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) {
957 MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
961 bzero((caddr_t)ip, sizeof(struct inode));
964 ip->i_e2fs = fs = ump->um_e2fs;
968 for (i = 0; i < MAXQUOTAS; i++)
969 ip->i_dquot[i] = NODQUOT;
972 * Put it onto its hash chain and lock it so that other requests for
973 * this inode will block if they arrive while we are sleeping waiting
974 * for old data structures to be purged or for the contents of the
975 * disk portion of this inode to be read.
980 if (ext2fs_inode_hash_lock < 0)
981 wakeup(&ext2fs_inode_hash_lock);
982 ext2fs_inode_hash_lock = 0;
985 /* Read in the disk contents for the inode, copy into the inode. */
986 /* Read in the disk contents for the inode, copy into the inode. */
988 printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
990 if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
991 (int)fs->s_blocksize, NOCRED, &bp)) {
993 * The inode does not contain anything useful, so it would
994 * be misleading to leave it on its hash chain. With mode
995 * still zero, it will be unlinked and returned to the free
1003 /* convert ext2 inode to dinode */
1004 ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
1005 ino_to_fsbo(fs, ino)), &ip->i_din);
1006 ip->i_block_group = ino_to_cg(fs, ino);
1007 ip->i_next_alloc_block = 0;
1008 ip->i_next_alloc_goal = 0;
1009 ip->i_prealloc_count = 0;
1010 ip->i_prealloc_block = 0;
1011 /* now we want to make sure that block pointers for unused
1012 blocks are zeroed out - ext2_balloc depends on this
1013 although for regular files and directories only
1015 if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1016 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1017 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1021 ext2_print_inode(ip);
1026 * Initialize the vnode from the inode, check for aliases.
1027 * Note that the underlying vnode may have changed.
1029 if (error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) {
1035 * Finish inode initialization now that aliasing has been resolved.
1037 ip->i_devvp = ump->um_devvp;
1040 * Set up a generation number for this inode if it does not
1041 * already have one. This should only happen on old filesystems.
1043 if (ip->i_gen == 0) {
1044 #if !defined(__FreeBSD__)
1045 struct timeval time;
1048 if (++nextgennumber < (u_long)time.tv_sec)
1049 nextgennumber = time.tv_sec;
1050 ip->i_gen = nextgennumber;
1051 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1052 ip->i_flag |= IN_MODIFIED;
1059 * File handle to vnode
1061 * Have to be really careful about stale file handles:
1062 * - check that the inode number is valid
1063 * - call ext2_vget() to get the locked inode
1064 * - check for an unallocated inode (i_mode == 0)
1065 * - check that the given client host has export rights and return
1066 * those rights via. exflagsp and credanonp
1069 ext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1070 register struct mount *mp;
1072 struct sockaddr *nam;
1075 struct ucred **credanonp;
1077 register struct ufid *ufhp;
1078 struct ext2_sb_info *fs;
1080 ufhp = (struct ufid *)fhp;
1081 fs = VFSTOUFS(mp)->um_e2fs;
1082 if (ufhp->ufid_ino < ROOTINO ||
1083 ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
1085 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
1089 * Vnode pointer to File handle
1093 ext2_vptofh(vp, fhp)
1097 register struct inode *ip;
1098 register struct ufid *ufhp;
1101 ufhp = (struct ufid *)fhp;
1102 ufhp->ufid_len = sizeof(struct ufid);
1103 ufhp->ufid_ino = ip->i_number;
1104 ufhp->ufid_gen = ip->i_gen;
1109 * Write a superblock and associated information back to disk.
1112 ext2_sbupdate(mp, waitfor)
1113 struct ufsmount *mp;
1116 register struct ext2_sb_info *fs = mp->um_e2fs;
1117 register struct ext2_super_block *es = fs->s_es;
1118 register struct buf *bp;
1121 printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1123 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1124 bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1125 if (waitfor == MNT_WAIT)
1131 * The buffers for group descriptors, inode bitmaps and block bitmaps
1132 * are not busy at this point and are (hopefully) written by the
1133 * usual sync mechanism. No need to write them here