ext2_vfsops.c revision 12746
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#if QUOTA
717	int i;
718#endif
719
720	if (!doforce)
721		flags &= ~FORCECLOSE;
722	ump = VFSTOUFS(mp);
723#if QUOTA
724	if (mp->mnt_flag & MNT_QUOTA) {
725		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
726			return (error);
727		for (i = 0; i < MAXQUOTAS; i++) {
728			if (ump->um_quotas[i] == NULLVP)
729				continue;
730			quotaoff(p, mp, i);
731		}
732		/*
733		 * Here we fall through to vflush again to ensure
734		 * that we have gotten rid of all the system vnodes.
735		 */
736	}
737#endif
738	error = vflush(mp, NULLVP, flags);
739	return (error);
740}
741
742/*
743 * Get file system statistics.
744 * taken from ext2/super.c ext2_statfs
745 */
746int
747ext2_statfs(mp, sbp, p)
748	struct mount *mp;
749	register struct statfs *sbp;
750	struct proc *p;
751{
752        unsigned long overhead;
753	unsigned long overhead_per_group;
754
755	register struct ufsmount *ump;
756	register struct ext2_sb_info *fs;
757	register struct ext2_super_block *es;
758
759	ump = VFSTOUFS(mp);
760	fs = ump->um_e2fs;
761	es = fs->s_es;
762
763	if (es->s_magic != EXT2_SUPER_MAGIC)
764		panic("ext2_statfs - magic number spoiled");
765
766	/*
767	 * Compute the overhead (FS structures)
768	 */
769	overhead_per_group = 1 /* super block */ +
770			     fs->s_db_per_group +
771			     1 /* block bitmap */ +
772			     1 /* inode bitmap */ +
773			     fs->s_itb_per_group;
774	overhead = es->s_first_data_block +
775		   fs->s_groups_count * overhead_per_group;
776
777	sbp->f_type = MOUNT_EXT2FS;
778	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
779	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
780	sbp->f_blocks = es->s_blocks_count - overhead;
781	sbp->f_bfree = es->s_free_blocks_count;
782	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
783	sbp->f_files = es->s_inodes_count;
784	sbp->f_ffree = es->s_free_inodes_count;
785	if (sbp != &mp->mnt_stat) {
786		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
787			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
788		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
789			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
790	}
791	return (0);
792}
793
794/*
795 * Go through the disk queues to initiate sandbagged IO;
796 * go through the inodes to write those that have been modified;
797 * initiate the writing of the super block if it has been modified.
798 *
799 * Note: we are always called with the filesystem marked `MPBUSY'.
800 */
801int
802ext2_sync(mp, waitfor, cred, p)
803	struct mount *mp;
804	int waitfor;
805	struct ucred *cred;
806	struct proc *p;
807{
808	register struct vnode *vp;
809	register struct inode *ip;
810	register struct ufsmount *ump = VFSTOUFS(mp);
811	register struct ext2_sb_info *fs;
812	int error, allerror = 0;
813
814	fs = ump->um_e2fs;
815	/*
816	 * Write back modified superblock.
817	 * Consistency check that the superblock
818	 * is still in the buffer cache.
819	 */
820	if (fs->s_dirt) {
821#if !defined(__FreeBSD__)
822		struct timeval time;
823#endif
824
825		if (fs->s_rd_only != 0) {		/* XXX */
826			printf("fs = %s\n", fs->fs_fsmnt);
827			panic("update: rofs mod");
828		}
829		fs->s_dirt = 0;
830#if !defined(__FreeBSD__)
831		get_time(&time);
832#endif
833		fs->s_es->s_wtime = time.tv_sec;
834		allerror = ext2_sbupdate(ump, waitfor);
835	}
836	/*
837	 * Write back each (modified) inode.
838	 */
839loop:
840	for (vp = mp->mnt_vnodelist.lh_first;
841	     vp != NULL;
842	     vp = vp->v_mntvnodes.le_next) {
843		/*
844		 * If the vnode that we are about to sync is no longer
845		 * associated with this mount point, start over.
846		 */
847		if (vp->v_mount != mp)
848			goto loop;
849		if (VOP_ISLOCKED(vp))
850			continue;
851		ip = VTOI(vp);
852		if ((ip->i_flag &
853		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
854		    vp->v_dirtyblkhd.lh_first == NULL)
855			continue;
856		if (vget(vp, 1))
857			goto loop;
858		if (error = VOP_FSYNC(vp, cred, waitfor, p))
859			allerror = error;
860		vput(vp);
861	}
862	/*
863	 * Force stale file system control information to be flushed.
864	 */
865	if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
866		allerror = error;
867#if QUOTA
868	qsync(mp);
869#endif
870	return (allerror);
871}
872
873/*
874 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
875 * in from disk.  If it is in core, wait for the lock bit to clear, then
876 * return the inode locked.  Detection and handling of mount points must be
877 * done by the calling routine.
878 */
879int
880ext2_vget(mp, ino, vpp)
881	struct mount *mp;
882	ino_t ino;
883	struct vnode **vpp;
884{
885	register struct ext2_sb_info *fs;
886	register struct inode *ip;
887	struct ufsmount *ump;
888	struct buf *bp;
889	struct vnode *vp;
890	dev_t dev;
891	int i, type, error;
892	int used_blocks;
893
894	ump = VFSTOUFS(mp);
895	dev = ump->um_dev;
896restart:
897	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
898		return (0);
899
900#ifdef __FreeBSD__
901	/*
902	 * Lock out the creation of new entries in the FFS hash table in
903	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
904	 * may occur!
905	 */
906	if (ext2fs_inode_hash_lock) {
907		while (ext2fs_inode_hash_lock) {
908			ext2fs_inode_hash_lock = -1;
909			tsleep(&ext2fs_inode_hash_lock, PVM, "ffsvgt", 0);
910		}
911		goto restart;
912	}
913	ext2fs_inode_hash_lock = 1;
914#endif
915
916	/* Allocate a new vnode/inode. */
917	if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) {
918		*vpp = NULL;
919		return (error);
920	}
921	/* I don't really know what this 'type' does. I suppose it's some kind
922	 * of memory accounting. Let's just book this memory on FFS's account
923	 * If I'm not mistaken, this stuff isn't implemented anyway in Lites
924	 */
925	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
926	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
927#ifndef __FreeBSD__
928	insmntque(vp, mp);
929#endif
930	bzero((caddr_t)ip, sizeof(struct inode));
931	vp->v_data = ip;
932	ip->i_vnode = vp;
933	ip->i_e2fs = fs = ump->um_e2fs;
934	ip->i_dev = dev;
935	ip->i_number = ino;
936#if QUOTA
937	for (i = 0; i < MAXQUOTAS; i++)
938		ip->i_dquot[i] = NODQUOT;
939#endif
940	/*
941	 * Put it onto its hash chain and lock it so that other requests for
942	 * this inode will block if they arrive while we are sleeping waiting
943	 * for old data structures to be purged or for the contents of the
944	 * disk portion of this inode to be read.
945	 */
946	ufs_ihashins(ip);
947
948#ifdef __FreeBSD__
949	if (ext2fs_inode_hash_lock < 0)
950		wakeup(&ext2fs_inode_hash_lock);
951	ext2fs_inode_hash_lock = 0;
952#endif
953
954	/* Read in the disk contents for the inode, copy into the inode. */
955	/* Read in the disk contents for the inode, copy into the inode. */
956#if 0
957printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
958#endif
959	if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
960	    (int)fs->s_blocksize, NOCRED, &bp)) {
961		/*
962		 * The inode does not contain anything useful, so it would
963		 * be misleading to leave it on its hash chain. With mode
964		 * still zero, it will be unlinked and returned to the free
965		 * list by vput().
966		 */
967		vput(vp);
968		brelse(bp);
969		*vpp = NULL;
970		return (error);
971	}
972	/* convert ext2 inode to dinode */
973	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
974			ino_to_fsbo(fs, ino)), &ip->i_din);
975	ip->i_block_group = ino_to_cg(fs, ino);
976	ip->i_next_alloc_block = 0;
977	ip->i_next_alloc_goal = 0;
978	ip->i_prealloc_count = 0;
979	ip->i_prealloc_block = 0;
980        /* now we want to make sure that block pointers for unused
981           blocks are zeroed out - ext2_balloc depends on this
982	   although for regular files and directories only
983	*/
984	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
985		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
986		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
987			ip->i_db[i] = 0;
988	}
989/*
990	ext2_print_inode(ip);
991*/
992	brelse(bp);
993
994	/*
995	 * Initialize the vnode from the inode, check for aliases.
996	 * Note that the underlying vnode may have changed.
997	 */
998	if (error = ufs_vinit(mp, ext2_specop_p, EXT2_FIFOOPS, &vp)) {
999		vput(vp);
1000		*vpp = NULL;
1001		return (error);
1002	}
1003	/*
1004	 * Finish inode initialization now that aliasing has been resolved.
1005	 */
1006	ip->i_devvp = ump->um_devvp;
1007	VREF(ip->i_devvp);
1008	/*
1009	 * Set up a generation number for this inode if it does not
1010	 * already have one. This should only happen on old filesystems.
1011	 */
1012	if (ip->i_gen == 0) {
1013#if !defined(__FreeBSD__)
1014		struct timeval time;
1015		get_time(&time);
1016#endif
1017		if (++nextgennumber < (u_long)time.tv_sec)
1018			nextgennumber = time.tv_sec;
1019		ip->i_gen = nextgennumber;
1020		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1021			ip->i_flag |= IN_MODIFIED;
1022	}
1023	*vpp = vp;
1024	return (0);
1025}
1026
1027/*
1028 * File handle to vnode
1029 *
1030 * Have to be really careful about stale file handles:
1031 * - check that the inode number is valid
1032 * - call ext2_vget() to get the locked inode
1033 * - check for an unallocated inode (i_mode == 0)
1034 * - check that the given client host has export rights and return
1035 *   those rights via. exflagsp and credanonp
1036 */
1037int
1038ext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1039	register struct mount *mp;
1040	struct fid *fhp;
1041	struct mbuf *nam;
1042	struct vnode **vpp;
1043	int *exflagsp;
1044	struct ucred **credanonp;
1045{
1046	register struct ufid *ufhp;
1047	struct ext2_sb_info *fs;
1048
1049	ufhp = (struct ufid *)fhp;
1050	fs = VFSTOUFS(mp)->um_e2fs;
1051	if (ufhp->ufid_ino < ROOTINO ||
1052	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
1053		return (ESTALE);
1054	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
1055}
1056
1057/*
1058 * Vnode pointer to File handle
1059 */
1060/* ARGSUSED */
1061int
1062ext2_vptofh(vp, fhp)
1063	struct vnode *vp;
1064	struct fid *fhp;
1065{
1066	register struct inode *ip;
1067	register struct ufid *ufhp;
1068
1069	ip = VTOI(vp);
1070	ufhp = (struct ufid *)fhp;
1071	ufhp->ufid_len = sizeof(struct ufid);
1072	ufhp->ufid_ino = ip->i_number;
1073	ufhp->ufid_gen = ip->i_gen;
1074	return (0);
1075}
1076
1077/*
1078 * Write a superblock and associated information back to disk.
1079 */
1080int
1081ext2_sbupdate(mp, waitfor)
1082	struct ufsmount *mp;
1083	int waitfor;
1084{
1085	register struct ext2_sb_info *fs = mp->um_e2fs;
1086	register struct ext2_super_block *es = fs->s_es;
1087	register struct buf *bp;
1088	int i, error = 0;
1089/*
1090printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1091*/
1092	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1093	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1094	if (waitfor == MNT_WAIT)
1095		error = bwrite(bp);
1096	else
1097		bawrite(bp);
1098
1099	/* write group descriptors back on disk */
1100	for(i = 0; i < fs->s_db_per_group; i++)
1101		/* Godmar thinks: we must avoid using any of the b*write
1102		 * functions here: we want to keep the buffer locked
1103		 * so we use my 'housemade' write routine:
1104		 */
1105		error |= ll_w_block(fs->s_group_desc[i], waitfor == MNT_WAIT);
1106
1107        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
1108                if (fs->s_inode_bitmap[i])
1109                        ll_w_block (fs->s_inode_bitmap[i], 1);
1110        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
1111                if (fs->s_block_bitmap[i])
1112                        ll_w_block (fs->s_block_bitmap[i], 1);
1113
1114	return (error);
1115}
1116