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