ext2_vfsops.c revision 12406
1/*
2 *  modified for EXT2FS support in Lites 1.1
3 *
4 *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5 *  University of Utah, Department of Computer Science
6 */
7/*
8 * Copyright (c) 1989, 1991, 1993, 1994
9 *	The Regents of the University of California.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 *
39 *	@(#)ffs_vfsops.c	8.8 (Berkeley) 4/18/94
40 */
41
42#if !defined(__FreeBSD__)
43#include "quota.h"
44#endif
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/namei.h>
49#include <sys/proc.h>
50#include <sys/kernel.h>
51#include <sys/vnode.h>
52#include <sys/socket.h>
53#include <sys/mount.h>
54#include <sys/buf.h>
55#include <sys/mbuf.h>
56#include <sys/file.h>
57#include <sys/disklabel.h>
58#include <sys/ioctl.h>
59#include <sys/errno.h>
60#include <sys/malloc.h>
61#include <sys/stat.h>
62
63#include <miscfs/specfs/specdev.h>
64
65#include <ufs/ufs/quota.h>
66#include <ufs/ufs/ufsmount.h>
67#include <ufs/ufs/inode.h>
68#include <ufs/ufs/ufs_extern.h>
69
70#include <gnu/ext2fs/fs.h>
71#include <gnu/ext2fs/ext2_extern.h>
72#include <gnu/ext2fs/ext2_fs.h>
73#include <gnu/ext2fs/ext2_fs_sb.h>
74
75int ext2_sbupdate __P((struct ufsmount *, int));
76
77struct vfsops ext2fs_vfsops = {
78	ext2_mount,
79	ufs_start,		/* empty function */
80	ext2_unmount,
81	ufs_root,		/* root inode via vget */
82	ufs_quotactl,		/* does operations associated with quotas */
83	ext2_statfs,
84	ext2_sync,
85	ext2_vget,
86	ext2_fhtovp,
87	ext2_vptofh,
88	ext2_init,
89};
90
91#if defined(__FreeBSD__)
92VFS_SET(ext2fs_vfsops, ext2fs, MOUNT_EXT2FS, 0);
93#define bsd_malloc malloc
94#define bsd_free free
95#endif
96
97extern u_long nextgennumber;
98#ifdef __FreeBSD__
99int ext2fs_inode_hash_lock;
100#endif
101
102/*
103 * Called by main() when ufs is going to be mounted as root.
104 *
105 * Name is updated by mount(8) after booting.
106 */
107#define ROOTNAME	"root_device"
108
109static int	compute_sb_data __P((struct vnode * devvp,
110				     struct ext2_super_block * es,
111				     struct ext2_sb_info * fs));
112
113int
114ext2_mountroot()
115{
116#if !defined(__FreeBSD__)
117	extern struct vnode *rootvp;
118#endif
119	register struct ext2_sb_info *fs;
120	register struct mount *mp;
121#if defined(__FreeBSD__)
122	struct proc *p = curproc;
123#else
124	struct proc *p = get_proc();	/* XXX */
125#endif
126	struct ufsmount *ump;
127	u_int size;
128	int error;
129
130	/*
131	 * Get vnodes for swapdev and rootdev.
132	 */
133	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
134		panic("ext2_mountroot: can't setup bdevvp's");
135
136	mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
137	bzero((char *)mp, (u_long)sizeof(struct mount));
138	mp->mnt_op = &ext2fs_vfsops;
139	mp->mnt_flag = MNT_RDONLY;
140	if (error = ext2_mountfs(rootvp, mp, p)) {
141		bsd_free(mp, M_MOUNT);
142		return (error);
143	}
144	if (error = vfs_lock(mp)) {
145		(void)ext2_unmount(mp, 0, p);
146		bsd_free(mp, M_MOUNT);
147		return (error);
148	}
149#if defined(__FreeBSD__)
150	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
151#else
152	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
153#endif
154	mp->mnt_flag |= MNT_ROOTFS;
155	mp->mnt_vnodecovered = NULLVP;
156	ump = VFSTOUFS(mp);
157	fs = ump->um_e2fs;
158	bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
159	fs->fs_fsmnt[0] = '/';
160	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
161	    MNAMELEN);
162	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
163	    &size);
164	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
165	(void)ext2_statfs(mp, &mp->mnt_stat, p);
166	vfs_unlock(mp);
167	inittodr(fs->s_es->s_wtime);		/* this helps to set the time */
168	return (0);
169}
170
171/*
172 * VFS Operations.
173 *
174 * mount system call
175 */
176int
177ext2_mount(mp, path, data, ndp, p)
178	register struct mount *mp;
179	char *path;
180	caddr_t data;		/* this is actually a (struct ufs_args *) */
181	struct nameidata *ndp;
182	struct proc *p;
183{
184	struct vnode *devvp;
185	struct ufs_args args;
186	struct ufsmount *ump = 0;
187	register struct ext2_sb_info *fs;
188	u_int size;
189	int error, flags;
190
191	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
192		return (error);
193	/*
194	 * If updating, check whether changing from read-only to
195	 * read/write; if there is no device name, that's all we do.
196	 */
197	if (mp->mnt_flag & MNT_UPDATE) {
198		ump = VFSTOUFS(mp);
199		fs = ump->um_e2fs;
200		error = 0;
201		if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
202			flags = WRITECLOSE;
203			if (mp->mnt_flag & MNT_FORCE)
204				flags |= FORCECLOSE;
205			if (vfs_busy(mp))
206				return (EBUSY);
207			error = ext2_flushfiles(mp, flags, p);
208			vfs_unbusy(mp);
209		}
210		if (!error && (mp->mnt_flag & MNT_RELOAD))
211			error = ext2_reload(mp, ndp->ni_cnd.cn_cred, p);
212		if (error)
213			return (error);
214		if (fs->s_rd_only && (mp->mnt_flag & MNT_WANTRDWR))
215			fs->s_rd_only = 0;
216		if (fs->s_rd_only == 0) {
217			/* don't say it's clean */
218			fs->s_es->s_state &= ~EXT2_VALID_FS;
219			ext2_sbupdate(ump, MNT_WAIT);
220		}
221		if (args.fspec == 0) {
222			/*
223			 * Process export requests.
224			 */
225			return (vfs_export(mp, &ump->um_export, &args.export));
226		}
227	}
228	/*
229	 * Not an update, or updating the name: look up the name
230	 * and verify that it refers to a sensible block device.
231	 */
232	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
233	if (error = namei(ndp))
234		return (error);
235	devvp = ndp->ni_vp;
236
237	if (devvp->v_type != VBLK) {
238		vrele(devvp);
239		return (ENOTBLK);
240	}
241	if (major(devvp->v_rdev) >= nblkdev) {
242		vrele(devvp);
243		return (ENXIO);
244	}
245	if ((mp->mnt_flag & MNT_UPDATE) == 0)
246		error = ext2_mountfs(devvp, mp, p);
247	else {
248		if (devvp != ump->um_devvp)
249			error = EINVAL;	/* needs translation */
250		else
251			vrele(devvp);
252	}
253	if (error) {
254		vrele(devvp);
255		return (error);
256	}
257	ump = VFSTOUFS(mp);
258	fs = ump->um_e2fs;
259	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
260	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
261	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
262	    MNAMELEN);
263	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
264	    &size);
265	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
266	(void)ext2_statfs(mp, &mp->mnt_stat, p);
267	return (0);
268}
269
270/*
271 * checks that the data in the descriptor blocks make sense
272 * this is taken from ext2/super.c
273 */
274static int ext2_check_descriptors (struct ext2_sb_info * sb)
275{
276        int i;
277        int desc_block = 0;
278        unsigned long block = sb->s_es->s_first_data_block;
279        struct ext2_group_desc * gdp = NULL;
280
281        /* ext2_debug ("Checking group descriptors"); */
282
283        for (i = 0; i < sb->s_groups_count; i++)
284        {
285		/* examine next descriptor block */
286                if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
287                        gdp = (struct ext2_group_desc *)
288				sb->s_group_desc[desc_block++]->b_data;
289                if (gdp->bg_block_bitmap < block ||
290                    gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
291                {
292                        printf ("ext2_check_descriptors: "
293                                    "Block bitmap for group %d"
294                                    " not in group (block %lu)!",
295                                    i, (unsigned long) gdp->bg_block_bitmap);
296                        return 0;
297                }
298                if (gdp->bg_inode_bitmap < block ||
299                    gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
300                {
301                        printf ("ext2_check_descriptors: "
302                                    "Inode bitmap for group %d"
303                                    " not in group (block %lu)!",
304                                    i, (unsigned long) gdp->bg_inode_bitmap);
305                        return 0;
306                }
307                if (gdp->bg_inode_table < block ||
308                    gdp->bg_inode_table + sb->s_itb_per_group >=
309                    block + EXT2_BLOCKS_PER_GROUP(sb))
310                {
311                        printf ("ext2_check_descriptors: "
312                                    "Inode table for group %d"
313                                    " not in group (block %lu)!",
314                                    i, (unsigned long) gdp->bg_inode_table);
315                        return 0;
316                }
317                block += EXT2_BLOCKS_PER_GROUP(sb);
318                gdp++;
319        }
320        return 1;
321}
322
323/*
324 * this computes the fields of the  ext2_sb_info structure from the
325 * data in the ext2_super_block structure read in
326 */
327static int compute_sb_data(devvp, es, fs)
328	struct vnode * devvp;
329	struct ext2_super_block * es;
330	struct ext2_sb_info * fs;
331{
332    int db_count, error;
333    int i, j;
334    int logic_sb_block = 1;	/* XXX for now */
335
336#if 1
337#define V(v)
338#else
339#define V(v)  printf(#v"= %d\n", fs->v);
340#endif
341
342    fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
343    V(s_blocksize)
344    fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
345    V(s_bshift)
346    fs->s_fsbtodb = es->s_log_block_size + 1;
347    V(s_fsbtodb)
348    fs->s_qbmask = fs->s_blocksize - 1;
349    V(s_bmask)
350    fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
351    V(s_blocksize_bits)
352    fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
353    V(s_frag_size)
354    if (fs->s_frag_size)
355	fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
356    V(s_frags_per_block)
357    fs->s_blocks_per_group = es->s_blocks_per_group;
358    V(s_blocks_per_group)
359    fs->s_frags_per_group = es->s_frags_per_group;
360    V(s_frags_per_group)
361    fs->s_inodes_per_group = es->s_inodes_per_group;
362    V(s_inodes_per_group)
363    fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE;
364    V(s_inodes_per_block)
365    fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
366    V(s_itb_per_group)
367    fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
368    V(s_desc_per_block)
369    /* s_resuid / s_resgid ? */
370    fs->s_groups_count = (es->s_blocks_count -
371			  es->s_first_data_block +
372			  EXT2_BLOCKS_PER_GROUP(fs) - 1) /
373			 EXT2_BLOCKS_PER_GROUP(fs);
374    V(s_groups_count)
375    db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
376	EXT2_DESC_PER_BLOCK(fs);
377    fs->s_db_per_group = db_count;
378    V(s_db_per_group)
379
380    fs->s_group_desc = bsd_malloc(db_count * sizeof (struct buf *),
381		M_UFSMNT, M_WAITOK);
382
383    /* adjust logic_sb_block */
384    if(fs->s_blocksize > SBSIZE)
385	/* Godmar thinks: if the blocksize is greater than 1024, then
386	   the superblock is logically part of block zero.
387	 */
388        logic_sb_block = 0;
389
390    for (i = 0; i < db_count; i++) {
391	error = bread(devvp , fsbtodb(fs, logic_sb_block + i + 1),
392		fs->s_blocksize, NOCRED, &fs->s_group_desc[i]);
393	if(error) {
394	    for (j = 0; j < i; j++)
395		brelse(fs->s_group_desc[j]);
396	    bsd_free(fs->s_group_desc, M_UFSMNT);
397	    printf("EXT2-fs: unable to read group descriptors (%d)\n", error);
398	    return EIO;
399	}
400    }
401    if(!ext2_check_descriptors(fs)) {
402	    for (j = 0; j < db_count; j++)
403		brelse(fs->s_group_desc[j]);
404	    bsd_free(fs->s_group_desc, M_UFSMNT);
405	    printf("EXT2-fs: (ext2_check_descriptors failure) "
406		   "unable to read group descriptors\n");
407	    return EIO;
408    }
409
410    for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
411	    fs->s_inode_bitmap_number[i] = 0;
412	    fs->s_inode_bitmap[i] = NULL;
413	    fs->s_block_bitmap_number[i] = 0;
414	    fs->s_block_bitmap[i] = NULL;
415    }
416    fs->s_loaded_inode_bitmaps = 0;
417    fs->s_loaded_block_bitmaps = 0;
418    return 0;
419}
420
421/*
422 * Reload all incore data for a filesystem (used after running fsck on
423 * the root filesystem and finding things to fix). The filesystem must
424 * be mounted read-only.
425 *
426 * Things to do to update the mount:
427 *	1) invalidate all cached meta-data.
428 *	2) re-read superblock from disk.
429 *	3) re-read summary information from disk.
430 *	4) invalidate all inactive vnodes.
431 *	5) invalidate all cached file data.
432 *	6) re-read inode data for all active vnodes.
433 */
434int
435ext2_reload(mountp, cred, p)
436	register struct mount *mountp;
437	struct ucred *cred;
438	struct proc *p;
439{
440	register struct vnode *vp, *nvp, *devvp;
441	struct inode *ip;
442	struct buf *bp;
443	struct ext2_super_block * es;
444	struct ext2_sb_info *fs;
445	int error;
446
447	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
448		return (EINVAL);
449	/*
450	 * Step 1: invalidate all cached meta-data.
451	 */
452	devvp = VFSTOUFS(mountp)->um_devvp;
453	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
454		panic("ext2_reload: dirty1");
455	/*
456	 * Step 2: re-read superblock from disk.
457	 * constants have been adjusted for ext2
458	 */
459	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
460		return (error);
461	es = (struct ext2_super_block *)bp->b_data;
462	if (es->s_magic != EXT2_SUPER_MAGIC) {
463		if(es->s_magic == EXT2_PRE_02B_MAGIC)
464		    printf("This filesystem bears the magic number of a pre "
465			   "0.2b version of ext2. This is not supported by "
466			   "Lites.\n");
467		else
468		    printf("Wrong magic number: %x (expected %x for ext2 fs\n",
469			es->s_magic, EXT2_SUPER_MAGIC);
470		brelse(bp);
471		return (EIO);		/* XXX needs translation */
472	}
473	fs = VFSTOUFS(mountp)->um_e2fs;
474	bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
475
476	if(error = compute_sb_data(devvp, es, fs)) {
477		brelse(bp);
478		return error;
479	}
480#ifdef UNKLAR
481	if (fs->fs_sbsize < SBSIZE)
482		bp->b_flags |= B_INVAL;
483#endif
484	brelse(bp);
485
486loop:
487	for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
488		nvp = vp->v_mntvnodes.le_next;
489		/*
490		 * Step 4: invalidate all inactive vnodes.
491		 */
492		if (vp->v_usecount == 0) {
493			vgone(vp);
494			continue;
495		}
496		/*
497		 * Step 5: invalidate all cached file data.
498		 */
499		if (vget(vp, 1))
500			goto loop;
501		if (vinvalbuf(vp, 0, cred, p, 0, 0))
502			panic("ext2_reload: dirty2");
503		/*
504		 * Step 6: re-read inode data for all active vnodes.
505		 */
506		ip = VTOI(vp);
507		if (error =
508		    bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
509		    (int)fs->s_blocksize, NOCRED, &bp)) {
510			vput(vp);
511			return (error);
512		}
513		ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
514			EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)),
515			&ip->i_din);
516		brelse(bp);
517		vput(vp);
518		if (vp->v_mount != mountp)
519			goto loop;
520	}
521	return (0);
522}
523
524/*
525 * Common code for mount and mountroot
526 */
527int
528ext2_mountfs(devvp, mp, p)
529	register struct vnode *devvp;
530	struct mount *mp;
531	struct proc *p;
532{
533	register struct ufsmount *ump;
534	struct buf *bp;
535	register struct ext2_sb_info *fs;
536	struct ext2_super_block * es;
537	dev_t dev = devvp->v_rdev;
538	struct partinfo dpart;
539	int havepart = 0;
540	int error, i, size;
541	int ronly;
542#if !defined(__FreeBSD__)
543	extern struct vnode *rootvp;
544#endif
545
546	/*
547	 * Disallow multiple mounts of the same device.
548	 * Disallow mounting of a device that is currently in use
549	 * (except for root, which might share swap device for miniroot).
550	 * Flush out any old buffers remaining from a previous use.
551	 */
552	if (error = vfs_mountedon(devvp))
553		return (error);
554	if (vcount(devvp) > 1 && devvp != rootvp)
555		return (EBUSY);
556	if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
557		return (error);
558#ifdef READONLY
559/* turn on this to force it to be read-only */
560	mp->mnt_flag |= MNT_RDONLY;
561#endif
562
563	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
564	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
565		return (error);
566	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
567		size = DEV_BSIZE;
568	else {
569		havepart = 1;
570		size = dpart.disklab->d_secsize;
571	}
572
573	bp = NULL;
574	ump = NULL;
575	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
576		goto out;
577	es = (struct ext2_super_block *)bp->b_data;
578	if (es->s_magic != EXT2_SUPER_MAGIC) {
579		if(es->s_magic == EXT2_PRE_02B_MAGIC)
580		    printf("This filesystem bears the magic number of a pre "
581			   "0.2b version of ext2. This is not supported by "
582			   "Lites.\n");
583		else
584		    printf("Wrong magic number: %x (expected %x for EXT2FS)\n",
585			es->s_magic, EXT2_SUPER_MAGIC);
586		error = EINVAL;		/* XXX needs translation */
587		goto out;
588	}
589	ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
590	bzero((caddr_t)ump, sizeof *ump);
591	/* I don't know whether this is the right strategy. Note that
592	   we dynamically allocate both a ext2_sb_info and a ext2_super_block
593	   while Linux keeps the super block in a locked buffer
594	 */
595	ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
596		M_UFSMNT, M_WAITOK);
597	ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
598		M_UFSMNT, M_WAITOK);
599	bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
600	if(error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)) {
601		brelse(bp);
602		return error;
603	}
604	brelse(bp);
605	bp = NULL;
606	fs = ump->um_e2fs;
607	fs->s_rd_only = ronly;	/* ronly is set according to mnt_flags */
608	if (!(fs->s_es->s_state & EXT2_VALID_FS)) {
609		printf("WARNING: %s was not properly dismounted\n",
610			fs->fs_fsmnt);
611	}
612	/* if the fs is not mounted read-only, make sure the super block is
613	   always written back on a sync()
614	 */
615	if (ronly == 0) {
616		fs->s_dirt = 1;		/* mark it modified */
617		fs->s_es->s_state &= ~EXT2_VALID_FS;	/* set fs invalid */
618	}
619	mp->mnt_data = (qaddr_t)ump;
620	mp->mnt_stat.f_fsid.val[0] = (long)dev;
621	mp->mnt_stat.f_fsid.val[1] = MOUNT_EXT2FS;
622	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
623	mp->mnt_flag |= MNT_LOCAL;
624	ump->um_mountp = mp;
625	ump->um_dev = dev;
626	ump->um_devvp = devvp;
627	/* setting those two parameters allows us to use
628	   ufs_bmap w/o changse !
629	*/
630	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
631	ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
632	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
633	for (i = 0; i < MAXQUOTAS; i++)
634		ump->um_quotas[i] = NULLVP;
635		devvp->v_specflags |= SI_MOUNTEDON;
636		if (ronly == 0)
637			ext2_sbupdate(ump, MNT_WAIT);
638	return (0);
639out:
640	if (bp)
641		brelse(bp);
642	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
643	if (ump) {
644		bsd_free(ump->um_fs, M_UFSMNT);
645		bsd_free(ump, M_UFSMNT);
646		mp->mnt_data = (qaddr_t)0;
647	}
648	return (error);
649}
650
651/*
652 * unmount system call
653 */
654int
655ext2_unmount(mp, mntflags, p)
656	struct mount *mp;
657	int mntflags;
658	struct proc *p;
659{
660	register struct ufsmount *ump;
661	register struct ext2_sb_info *fs;
662	int error, flags, ronly, i;
663
664	flags = 0;
665	if (mntflags & MNT_FORCE) {
666		if (mp->mnt_flag & MNT_ROOTFS)
667			return (EINVAL);
668		flags |= FORCECLOSE;
669	}
670	if (error = ext2_flushfiles(mp, flags, p))
671		return (error);
672	ump = VFSTOUFS(mp);
673	fs = ump->um_e2fs;
674	ronly = fs->s_rd_only;
675	if (!ronly) {
676		fs->s_es->s_state |= EXT2_VALID_FS;	/* was fs_clean = 1 */
677		ext2_sbupdate(ump, MNT_WAIT);
678	}
679	/* release buffers containing group descriptors */
680	for(i = 0; i < fs->s_db_per_group; i++)
681		brelse(fs->s_group_desc[i]);
682	/* release cached inode/block bitmaps */
683        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
684                if (fs->s_inode_bitmap[i])
685                        brelse (fs->s_inode_bitmap[i]);
686        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
687                if (fs->s_block_bitmap[i])
688                        brelse (fs->s_block_bitmap[i]);
689
690	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
691	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
692		NOCRED, p);
693	vrele(ump->um_devvp);
694	bsd_free(fs->s_es, M_UFSMNT);
695	bsd_free(fs, M_UFSMNT);
696	bsd_free(ump, M_UFSMNT);
697	mp->mnt_data = (qaddr_t)0;
698	mp->mnt_flag &= ~MNT_LOCAL;
699	return (error);
700}
701
702/*
703 * Flush out all the files in a filesystem.
704 */
705int
706ext2_flushfiles(mp, flags, p)
707	register struct mount *mp;
708	int flags;
709	struct proc *p;
710{
711#if !defined(__FreeBSD__)
712	extern int doforce;
713#endif
714	register struct ufsmount *ump;
715	int error;
716
717	if (!doforce)
718		flags &= ~FORCECLOSE;
719	ump = VFSTOUFS(mp);
720#if QUOTA
721	if (mp->mnt_flag & MNT_QUOTA) {
722		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
723			return (error);
724		for (i = 0; i < MAXQUOTAS; i++) {
725			if (ump->um_quotas[i] == NULLVP)
726				continue;
727			quotaoff(p, mp, i);
728		}
729		/*
730		 * Here we fall through to vflush again to ensure
731		 * that we have gotten rid of all the system vnodes.
732		 */
733	}
734#endif
735	error = vflush(mp, NULLVP, flags);
736	return (error);
737}
738
739/*
740 * Get file system statistics.
741 * taken from ext2/super.c ext2_statfs
742 */
743int
744ext2_statfs(mp, sbp, p)
745	struct mount *mp;
746	register struct statfs *sbp;
747	struct proc *p;
748{
749        unsigned long overhead;
750	unsigned long overhead_per_group;
751
752	register struct ufsmount *ump;
753	register struct ext2_sb_info *fs;
754	register struct ext2_super_block *es;
755
756	ump = VFSTOUFS(mp);
757	fs = ump->um_e2fs;
758	es = fs->s_es;
759
760	if (es->s_magic != EXT2_SUPER_MAGIC)
761		panic("ext2_statfs - magic number spoiled");
762
763	/*
764	 * Compute the overhead (FS structures)
765	 */
766	overhead_per_group = 1 /* super block */ +
767			     fs->s_db_per_group +
768			     1 /* block bitmap */ +
769			     1 /* inode bitmap */ +
770			     fs->s_itb_per_group;
771	overhead = es->s_first_data_block +
772		   fs->s_groups_count * overhead_per_group;
773
774	sbp->f_type = MOUNT_EXT2FS;
775	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
776	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
777	sbp->f_blocks = es->s_blocks_count - overhead;
778	sbp->f_bfree = es->s_free_blocks_count;
779	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
780	sbp->f_files = es->s_inodes_count;
781	sbp->f_ffree = es->s_free_inodes_count;
782	if (sbp != &mp->mnt_stat) {
783		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
784			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
785		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
786			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
787	}
788	return (0);
789}
790
791/*
792 * Go through the disk queues to initiate sandbagged IO;
793 * go through the inodes to write those that have been modified;
794 * initiate the writing of the super block if it has been modified.
795 *
796 * Note: we are always called with the filesystem marked `MPBUSY'.
797 */
798int
799ext2_sync(mp, waitfor, cred, p)
800	struct mount *mp;
801	int waitfor;
802	struct ucred *cred;
803	struct proc *p;
804{
805	register struct vnode *vp;
806	register struct inode *ip;
807	register struct ufsmount *ump = VFSTOUFS(mp);
808	register struct ext2_sb_info *fs;
809	int error, allerror = 0;
810
811	fs = ump->um_e2fs;
812	/*
813	 * Write back modified superblock.
814	 * Consistency check that the superblock
815	 * is still in the buffer cache.
816	 */
817	if (fs->s_dirt) {
818#if !defined(__FreeBSD__)
819		struct timeval time;
820#endif
821
822		if (fs->s_rd_only != 0) {		/* XXX */
823			printf("fs = %s\n", fs->fs_fsmnt);
824			panic("update: rofs mod");
825		}
826		fs->s_dirt = 0;
827#if !defined(__FreeBSD__)
828		get_time(&time);
829#endif
830		fs->s_es->s_wtime = time.tv_sec;
831		allerror = ext2_sbupdate(ump, waitfor);
832	}
833	/*
834	 * Write back each (modified) inode.
835	 */
836loop:
837	for (vp = mp->mnt_vnodelist.lh_first;
838	     vp != NULL;
839	     vp = vp->v_mntvnodes.le_next) {
840		/*
841		 * If the vnode that we are about to sync is no longer
842		 * associated with this mount point, start over.
843		 */
844		if (vp->v_mount != mp)
845			goto loop;
846		if (VOP_ISLOCKED(vp))
847			continue;
848		ip = VTOI(vp);
849		if ((ip->i_flag &
850		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
851		    vp->v_dirtyblkhd.lh_first == NULL)
852			continue;
853		if (vget(vp, 1))
854			goto loop;
855		if (error = VOP_FSYNC(vp, cred, waitfor, p))
856			allerror = error;
857		vput(vp);
858	}
859	/*
860	 * Force stale file system control information to be flushed.
861	 */
862	if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
863		allerror = error;
864#if QUOTA
865	qsync(mp);
866#endif
867	return (allerror);
868}
869
870/*
871 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
872 * in from disk.  If it is in core, wait for the lock bit to clear, then
873 * return the inode locked.  Detection and handling of mount points must be
874 * done by the calling routine.
875 */
876int
877ext2_vget(mp, ino, vpp)
878	struct mount *mp;
879	ino_t ino;
880	struct vnode **vpp;
881{
882	register struct ext2_sb_info *fs;
883	register struct inode *ip;
884	struct ufsmount *ump;
885	struct buf *bp;
886	struct vnode *vp;
887	dev_t dev;
888	int i, type, error;
889	int used_blocks;
890
891	ump = VFSTOUFS(mp);
892	dev = ump->um_dev;
893restart:
894	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
895		return (0);
896
897#ifdef __FreeBSD__
898	/*
899	 * Lock out the creation of new entries in the FFS hash table in
900	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
901	 * may occur!
902	 */
903	if (ext2fs_inode_hash_lock) {
904		while (ext2fs_inode_hash_lock) {
905			ext2fs_inode_hash_lock = -1;
906			tsleep(&ext2fs_inode_hash_lock, PVM, "ffsvgt", 0);
907		}
908		goto restart;
909	}
910	ext2fs_inode_hash_lock = 1;
911#endif
912
913	/* Allocate a new vnode/inode. */
914	if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) {
915		*vpp = NULL;
916		return (error);
917	}
918	/* I don't really know what this 'type' does. I suppose it's some kind
919	 * of memory accounting. Let's just book this memory on FFS's account
920	 * If I'm not mistaken, this stuff isn't implemented anyway in Lites
921	 */
922	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
923	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
924#ifndef __FreeBSD__
925	insmntque(vp, mp);
926#endif
927	bzero((caddr_t)ip, sizeof(struct inode));
928	vp->v_data = ip;
929	ip->i_vnode = vp;
930	ip->i_e2fs = fs = ump->um_e2fs;
931	ip->i_dev = dev;
932	ip->i_number = ino;
933#if QUOTA
934	for (i = 0; i < MAXQUOTAS; i++)
935		ip->i_dquot[i] = NODQUOT;
936#endif
937	/*
938	 * Put it onto its hash chain and lock it so that other requests for
939	 * this inode will block if they arrive while we are sleeping waiting
940	 * for old data structures to be purged or for the contents of the
941	 * disk portion of this inode to be read.
942	 */
943	ufs_ihashins(ip);
944
945#ifdef __FreeBSD__
946	if (ext2fs_inode_hash_lock < 0)
947		wakeup(&ext2fs_inode_hash_lock);
948	ext2fs_inode_hash_lock = 0;
949#endif
950
951	/* Read in the disk contents for the inode, copy into the inode. */
952	/* Read in the disk contents for the inode, copy into the inode. */
953#if 0
954printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
955#endif
956	if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
957	    (int)fs->s_blocksize, NOCRED, &bp)) {
958		/*
959		 * The inode does not contain anything useful, so it would
960		 * be misleading to leave it on its hash chain. With mode
961		 * still zero, it will be unlinked and returned to the free
962		 * list by vput().
963		 */
964		vput(vp);
965		brelse(bp);
966		*vpp = NULL;
967		return (error);
968	}
969	/* convert ext2 inode to dinode */
970	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
971			ino_to_fsbo(fs, ino)), &ip->i_din);
972	ip->i_block_group = ino_to_cg(fs, ino);
973	ip->i_next_alloc_block = 0;
974	ip->i_next_alloc_goal = 0;
975	ip->i_prealloc_count = 0;
976	ip->i_prealloc_block = 0;
977        /* now we want to make sure that block pointers for unused
978           blocks are zeroed out - ext2_balloc depends on this
979	   although for regular files and directories only
980	*/
981	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
982		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
983		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
984			ip->i_db[i] = 0;
985	}
986/*
987	ext2_print_inode(ip);
988*/
989	brelse(bp);
990
991	/*
992	 * Initialize the vnode from the inode, check for aliases.
993	 * Note that the underlying vnode may have changed.
994	 */
995	if (error = ufs_vinit(mp, ext2_specop_p, EXT2_FIFOOPS, &vp)) {
996		vput(vp);
997		*vpp = NULL;
998		return (error);
999	}
1000	/*
1001	 * Finish inode initialization now that aliasing has been resolved.
1002	 */
1003	ip->i_devvp = ump->um_devvp;
1004	VREF(ip->i_devvp);
1005	/*
1006	 * Set up a generation number for this inode if it does not
1007	 * already have one. This should only happen on old filesystems.
1008	 */
1009	if (ip->i_gen == 0) {
1010#if !defined(__FreeBSD__)
1011		struct timeval time;
1012		get_time(&time);
1013#endif
1014		if (++nextgennumber < (u_long)time.tv_sec)
1015			nextgennumber = time.tv_sec;
1016		ip->i_gen = nextgennumber;
1017		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1018			ip->i_flag |= IN_MODIFIED;
1019	}
1020	*vpp = vp;
1021	return (0);
1022}
1023
1024/*
1025 * File handle to vnode
1026 *
1027 * Have to be really careful about stale file handles:
1028 * - check that the inode number is valid
1029 * - call ext2_vget() to get the locked inode
1030 * - check for an unallocated inode (i_mode == 0)
1031 * - check that the given client host has export rights and return
1032 *   those rights via. exflagsp and credanonp
1033 */
1034int
1035ext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1036	register struct mount *mp;
1037	struct fid *fhp;
1038	struct mbuf *nam;
1039	struct vnode **vpp;
1040	int *exflagsp;
1041	struct ucred **credanonp;
1042{
1043	register struct ufid *ufhp;
1044	struct ext2_sb_info *fs;
1045
1046	ufhp = (struct ufid *)fhp;
1047	fs = VFSTOUFS(mp)->um_e2fs;
1048	if (ufhp->ufid_ino < ROOTINO ||
1049	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
1050		return (ESTALE);
1051	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
1052}
1053
1054/*
1055 * Vnode pointer to File handle
1056 */
1057/* ARGSUSED */
1058int
1059ext2_vptofh(vp, fhp)
1060	struct vnode *vp;
1061	struct fid *fhp;
1062{
1063	register struct inode *ip;
1064	register struct ufid *ufhp;
1065
1066	ip = VTOI(vp);
1067	ufhp = (struct ufid *)fhp;
1068	ufhp->ufid_len = sizeof(struct ufid);
1069	ufhp->ufid_ino = ip->i_number;
1070	ufhp->ufid_gen = ip->i_gen;
1071	return (0);
1072}
1073
1074/*
1075 * Write a superblock and associated information back to disk.
1076 */
1077int
1078ext2_sbupdate(mp, waitfor)
1079	struct ufsmount *mp;
1080	int waitfor;
1081{
1082	register struct ext2_sb_info *fs = mp->um_e2fs;
1083	register struct ext2_super_block *es = fs->s_es;
1084	register struct buf *bp;
1085	int i, error = 0;
1086/*
1087printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1088*/
1089	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1090	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1091	if (waitfor == MNT_WAIT)
1092		error = bwrite(bp);
1093	else
1094		bawrite(bp);
1095
1096	/* write group descriptors back on disk */
1097	for(i = 0; i < fs->s_db_per_group; i++)
1098		/* Godmar thinks: we must avoid using any of the b*write
1099		 * functions here: we want to keep the buffer locked
1100		 * so we use my 'housemade' write routine:
1101		 */
1102		error |= ll_w_block(fs->s_group_desc[i], waitfor == MNT_WAIT);
1103
1104        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
1105                if (fs->s_inode_bitmap[i])
1106                        ll_w_block (fs->s_inode_bitmap[i], 1);
1107        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
1108                if (fs->s_block_bitmap[i])
1109                        ll_w_block (fs->s_block_bitmap[i], 1);
1110
1111	return (error);
1112}
1113