ext2_vfsops.c revision 12115
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 i, size, 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	caddr_t base;
533	int havepart = 0;
534	int error, i, size;
535	int ronly;
536#if !defined(__FreeBSD__)
537	extern struct vnode *rootvp;
538#endif
539
540	/*
541	 * Disallow multiple mounts of the same device.
542	 * Disallow mounting of a device that is currently in use
543	 * (except for root, which might share swap device for miniroot).
544	 * Flush out any old buffers remaining from a previous use.
545	 */
546	if (error = vfs_mountedon(devvp))
547		return (error);
548	if (vcount(devvp) > 1 && devvp != rootvp)
549		return (EBUSY);
550	if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
551		return (error);
552#ifdef READONLY
553/* turn on this to force it to be read-only */
554	mp->mnt_flag |= MNT_RDONLY;
555#endif
556
557	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
558	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
559		return (error);
560	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
561		size = DEV_BSIZE;
562	else {
563		havepart = 1;
564		size = dpart.disklab->d_secsize;
565	}
566
567	bp = NULL;
568	ump = NULL;
569	if (error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp))
570		goto out;
571	es = (struct ext2_super_block *)bp->b_data;
572	if (es->s_magic != EXT2_SUPER_MAGIC) {
573		if(es->s_magic == EXT2_PRE_02B_MAGIC)
574		    printf("This filesystem bears the magic number of a pre "
575			   "0.2b version of ext2. This is not supported by "
576			   "Lites.\n");
577		else
578		    printf("Wrong magic number: %x (expected %x for EXT2FS)\n",
579			es->s_magic, EXT2_SUPER_MAGIC);
580		error = EINVAL;		/* XXX needs translation */
581		goto out;
582	}
583	ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
584	bzero((caddr_t)ump, sizeof *ump);
585	/* I don't know whether this is the right strategy. Note that
586	   we dynamically allocate both a ext2_sb_info and a ext2_super_block
587	   while Linux keeps the super block in a locked buffer
588	 */
589	ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
590		M_UFSMNT, M_WAITOK);
591	ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
592		M_UFSMNT, M_WAITOK);
593	bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
594	if(error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)) {
595		brelse(bp);
596		return error;
597	}
598	brelse(bp);
599	bp = NULL;
600	fs = ump->um_e2fs;
601	fs->s_rd_only = ronly;	/* ronly is set according to mnt_flags */
602	if (!(fs->s_es->s_state & EXT2_VALID_FS)) {
603		printf("WARNING: %s was not properly dismounted\n",
604			fs->fs_fsmnt);
605	}
606	/* if the fs is not mounted read-only, make sure the super block is
607	   always written back on a sync()
608	 */
609	if (ronly == 0) {
610		fs->s_dirt = 1;		/* mark it modified */
611		fs->s_es->s_state &= ~EXT2_VALID_FS;	/* set fs invalid */
612	}
613	mp->mnt_data = (qaddr_t)ump;
614	mp->mnt_stat.f_fsid.val[0] = (long)dev;
615	mp->mnt_stat.f_fsid.val[1] = MOUNT_EXT2FS;
616	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
617	mp->mnt_flag |= MNT_LOCAL;
618	ump->um_mountp = mp;
619	ump->um_dev = dev;
620	ump->um_devvp = devvp;
621	/* setting those two parameters allows us to use
622	   ufs_bmap w/o changse !
623	*/
624	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
625	ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
626	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
627	for (i = 0; i < MAXQUOTAS; i++)
628		ump->um_quotas[i] = NULLVP;
629		devvp->v_specflags |= SI_MOUNTEDON;
630		if (ronly == 0)
631			ext2_sbupdate(ump, MNT_WAIT);
632	return (0);
633out:
634	if (bp)
635		brelse(bp);
636	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
637	if (ump) {
638		bsd_free(ump->um_fs, M_UFSMNT);
639		bsd_free(ump, M_UFSMNT);
640		mp->mnt_data = (qaddr_t)0;
641	}
642	return (error);
643}
644
645/*
646 * unmount system call
647 */
648int
649ext2_unmount(mp, mntflags, p)
650	struct mount *mp;
651	int mntflags;
652	struct proc *p;
653{
654	register struct ufsmount *ump;
655	register struct ext2_sb_info *fs;
656	int error, flags, ronly, i;
657
658	flags = 0;
659	if (mntflags & MNT_FORCE) {
660		if (mp->mnt_flag & MNT_ROOTFS)
661			return (EINVAL);
662		flags |= FORCECLOSE;
663	}
664	if (error = ext2_flushfiles(mp, flags, p))
665		return (error);
666	ump = VFSTOUFS(mp);
667	fs = ump->um_e2fs;
668	ronly = fs->s_rd_only;
669	if (!ronly) {
670		fs->s_es->s_state |= EXT2_VALID_FS;	/* was fs_clean = 1 */
671		ext2_sbupdate(ump, MNT_WAIT);
672	}
673	/* release buffers containing group descriptors */
674	for(i = 0; i < fs->s_db_per_group; i++)
675		brelse(fs->s_group_desc[i]);
676	/* release cached inode/block bitmaps */
677        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
678                if (fs->s_inode_bitmap[i])
679                        brelse (fs->s_inode_bitmap[i]);
680        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
681                if (fs->s_block_bitmap[i])
682                        brelse (fs->s_block_bitmap[i]);
683
684	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
685	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
686		NOCRED, p);
687	vrele(ump->um_devvp);
688	bsd_free(fs->s_es, M_UFSMNT);
689	bsd_free(fs, M_UFSMNT);
690	bsd_free(ump, M_UFSMNT);
691	mp->mnt_data = (qaddr_t)0;
692	mp->mnt_flag &= ~MNT_LOCAL;
693	return (error);
694}
695
696/*
697 * Flush out all the files in a filesystem.
698 */
699int
700ext2_flushfiles(mp, flags, p)
701	register struct mount *mp;
702	int flags;
703	struct proc *p;
704{
705#if !defined(__FreeBSD__)
706	extern int doforce;
707#endif
708	register struct ufsmount *ump;
709	int i, error;
710
711	if (!doforce)
712		flags &= ~FORCECLOSE;
713	ump = VFSTOUFS(mp);
714#if QUOTA
715	if (mp->mnt_flag & MNT_QUOTA) {
716		if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
717			return (error);
718		for (i = 0; i < MAXQUOTAS; i++) {
719			if (ump->um_quotas[i] == NULLVP)
720				continue;
721			quotaoff(p, mp, i);
722		}
723		/*
724		 * Here we fall through to vflush again to ensure
725		 * that we have gotten rid of all the system vnodes.
726		 */
727	}
728#endif
729	error = vflush(mp, NULLVP, flags);
730	return (error);
731}
732
733/*
734 * Get file system statistics.
735 * taken from ext2/super.c ext2_statfs
736 */
737int
738ext2_statfs(mp, sbp, p)
739	struct mount *mp;
740	register struct statfs *sbp;
741	struct proc *p;
742{
743        unsigned long overhead;
744	unsigned long overhead_per_group;
745
746	register struct ufsmount *ump;
747	register struct ext2_sb_info *fs;
748	register struct ext2_super_block *es;
749
750	ump = VFSTOUFS(mp);
751	fs = ump->um_e2fs;
752	es = fs->s_es;
753
754	if (es->s_magic != EXT2_SUPER_MAGIC)
755		panic("ext2_statfs - magic number spoiled");
756
757	/*
758	 * Compute the overhead (FS structures)
759	 */
760	overhead_per_group = 1 /* super block */ +
761			     fs->s_db_per_group +
762			     1 /* block bitmap */ +
763			     1 /* inode bitmap */ +
764			     fs->s_itb_per_group;
765	overhead = es->s_first_data_block +
766		   fs->s_groups_count * overhead_per_group;
767
768	sbp->f_type = MOUNT_EXT2FS;
769	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
770	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
771	sbp->f_blocks = es->s_blocks_count - overhead;
772	sbp->f_bfree = es->s_free_blocks_count;
773	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
774	sbp->f_files = es->s_inodes_count;
775	sbp->f_ffree = es->s_free_inodes_count;
776	if (sbp != &mp->mnt_stat) {
777		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
778			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
779		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
780			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
781	}
782	return (0);
783}
784
785/*
786 * Go through the disk queues to initiate sandbagged IO;
787 * go through the inodes to write those that have been modified;
788 * initiate the writing of the super block if it has been modified.
789 *
790 * Note: we are always called with the filesystem marked `MPBUSY'.
791 */
792int
793ext2_sync(mp, waitfor, cred, p)
794	struct mount *mp;
795	int waitfor;
796	struct ucred *cred;
797	struct proc *p;
798{
799	register struct vnode *vp;
800	register struct inode *ip;
801	register struct ufsmount *ump = VFSTOUFS(mp);
802	register struct ext2_sb_info *fs;
803	int error, allerror = 0;
804
805	fs = ump->um_e2fs;
806	/*
807	 * Write back modified superblock.
808	 * Consistency check that the superblock
809	 * is still in the buffer cache.
810	 */
811	if (fs->s_dirt) {
812#if !defined(__FreeBSD__)
813		struct timeval time;
814#endif
815
816		if (fs->s_rd_only != 0) {		/* XXX */
817			printf("fs = %s\n", fs->fs_fsmnt);
818			panic("update: rofs mod");
819		}
820		fs->s_dirt = 0;
821#if !defined(__FreeBSD__)
822		get_time(&time);
823#endif
824		fs->s_es->s_wtime = time.tv_sec;
825		allerror = ext2_sbupdate(ump, waitfor);
826	}
827	/*
828	 * Write back each (modified) inode.
829	 */
830loop:
831	for (vp = mp->mnt_vnodelist.lh_first;
832	     vp != NULL;
833	     vp = vp->v_mntvnodes.le_next) {
834		/*
835		 * If the vnode that we are about to sync is no longer
836		 * associated with this mount point, start over.
837		 */
838		if (vp->v_mount != mp)
839			goto loop;
840		if (VOP_ISLOCKED(vp))
841			continue;
842		ip = VTOI(vp);
843		if ((ip->i_flag &
844		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
845		    vp->v_dirtyblkhd.lh_first == NULL)
846			continue;
847		if (vget(vp, 1))
848			goto loop;
849		if (error = VOP_FSYNC(vp, cred, waitfor, p))
850			allerror = error;
851		vput(vp);
852	}
853	/*
854	 * Force stale file system control information to be flushed.
855	 */
856	if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
857		allerror = error;
858#if QUOTA
859	qsync(mp);
860#endif
861	return (allerror);
862}
863
864/*
865 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
866 * in from disk.  If it is in core, wait for the lock bit to clear, then
867 * return the inode locked.  Detection and handling of mount points must be
868 * done by the calling routine.
869 */
870int
871ext2_vget(mp, ino, vpp)
872	struct mount *mp;
873	ino_t ino;
874	struct vnode **vpp;
875{
876	register struct ext2_sb_info *fs;
877	register struct inode *ip;
878	struct ufsmount *ump;
879	struct buf *bp;
880	struct vnode *vp;
881	dev_t dev;
882	int i, type, error;
883	int used_blocks;
884
885	ump = VFSTOUFS(mp);
886	dev = ump->um_dev;
887	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
888		return (0);
889
890	/* Allocate a new vnode/inode. */
891	if (error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) {
892		*vpp = NULL;
893		return (error);
894	}
895	/* I don't really know what this 'type' does. I suppose it's some kind
896	 * of memory accounting. Let's just book this memory on FFS's account
897	 * If I'm not mistaken, this stuff isn't implemented anyway in Lites
898	 */
899	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
900	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
901	insmntque(vp, mp);
902	bzero((caddr_t)ip, sizeof(struct inode));
903	vp->v_data = ip;
904	ip->i_vnode = vp;
905	ip->i_e2fs = fs = ump->um_e2fs;
906	ip->i_dev = dev;
907	ip->i_number = ino;
908#if QUOTA
909	for (i = 0; i < MAXQUOTAS; i++)
910		ip->i_dquot[i] = NODQUOT;
911#endif
912	/*
913	 * Put it onto its hash chain and lock it so that other requests for
914	 * this inode will block if they arrive while we are sleeping waiting
915	 * for old data structures to be purged or for the contents of the
916	 * disk portion of this inode to be read.
917	 */
918	ufs_ihashins(ip);
919
920	/* Read in the disk contents for the inode, copy into the inode. */
921#if 0
922printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
923#endif
924	if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
925	    (int)fs->s_blocksize, NOCRED, &bp)) {
926		/*
927		 * The inode does not contain anything useful, so it would
928		 * be misleading to leave it on its hash chain. With mode
929		 * still zero, it will be unlinked and returned to the free
930		 * list by vput().
931		 */
932		vput(vp);
933		brelse(bp);
934		*vpp = NULL;
935		return (error);
936	}
937	/* convert ext2 inode to dinode */
938	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
939			ino_to_fsbo(fs, ino)), &ip->i_din);
940	ip->i_block_group = ino_to_cg(fs, ino);
941	ip->i_next_alloc_block = 0;
942	ip->i_next_alloc_goal = 0;
943	ip->i_prealloc_count = 0;
944	ip->i_prealloc_block = 0;
945        /* now we want to make sure that block pointers for unused
946           blocks are zeroed out - ext2_balloc depends on this
947	   although for regular files and directories only
948	*/
949	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
950		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
951		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
952			ip->i_db[i] = 0;
953	}
954/*
955	ext2_print_inode(ip);
956*/
957	brelse(bp);
958
959	/*
960	 * Initialize the vnode from the inode, check for aliases.
961	 * Note that the underlying vnode may have changed.
962	 */
963	if (error = ufs_vinit(mp, ext2_specop_p, EXT2_FIFOOPS, &vp)) {
964		vput(vp);
965		*vpp = NULL;
966		return (error);
967	}
968	/*
969	 * Finish inode initialization now that aliasing has been resolved.
970	 */
971	ip->i_devvp = ump->um_devvp;
972	VREF(ip->i_devvp);
973	/*
974	 * Set up a generation number for this inode if it does not
975	 * already have one. This should only happen on old filesystems.
976	 */
977	if (ip->i_gen == 0) {
978#if !defined(__FreeBSD__)
979		struct timeval time;
980		get_time(&time);
981#endif
982		if (++nextgennumber < (u_long)time.tv_sec)
983			nextgennumber = time.tv_sec;
984		ip->i_gen = nextgennumber;
985		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
986			ip->i_flag |= IN_MODIFIED;
987	}
988	*vpp = vp;
989	return (0);
990}
991
992/*
993 * File handle to vnode
994 *
995 * Have to be really careful about stale file handles:
996 * - check that the inode number is valid
997 * - call ext2_vget() to get the locked inode
998 * - check for an unallocated inode (i_mode == 0)
999 * - check that the given client host has export rights and return
1000 *   those rights via. exflagsp and credanonp
1001 */
1002int
1003ext2_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1004	register struct mount *mp;
1005	struct fid *fhp;
1006	struct mbuf *nam;
1007	struct vnode **vpp;
1008	int *exflagsp;
1009	struct ucred **credanonp;
1010{
1011	register struct ufid *ufhp;
1012	struct ext2_sb_info *fs;
1013
1014	ufhp = (struct ufid *)fhp;
1015	fs = VFSTOUFS(mp)->um_e2fs;
1016	if (ufhp->ufid_ino < ROOTINO ||
1017	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
1018		return (ESTALE);
1019	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
1020}
1021
1022/*
1023 * Vnode pointer to File handle
1024 */
1025/* ARGSUSED */
1026int
1027ext2_vptofh(vp, fhp)
1028	struct vnode *vp;
1029	struct fid *fhp;
1030{
1031	register struct inode *ip;
1032	register struct ufid *ufhp;
1033
1034	ip = VTOI(vp);
1035	ufhp = (struct ufid *)fhp;
1036	ufhp->ufid_len = sizeof(struct ufid);
1037	ufhp->ufid_ino = ip->i_number;
1038	ufhp->ufid_gen = ip->i_gen;
1039	return (0);
1040}
1041
1042/*
1043 * Write a superblock and associated information back to disk.
1044 */
1045int
1046ext2_sbupdate(mp, waitfor)
1047	struct ufsmount *mp;
1048	int waitfor;
1049{
1050	register struct ext2_sb_info *fs = mp->um_e2fs;
1051	register struct ext2_super_block *es = fs->s_es;
1052	register struct buf *bp;
1053	int blks;
1054	caddr_t space;
1055	int i, size, error = 0;
1056/*
1057printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1058*/
1059	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1060	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1061	if (waitfor == MNT_WAIT)
1062		error = bwrite(bp);
1063	else
1064		bawrite(bp);
1065
1066	/* write group descriptors back on disk */
1067	for(i = 0; i < fs->s_db_per_group; i++)
1068		/* Godmar thinks: we must avoid using any of the b*write
1069		 * functions here: we want to keep the buffer locked
1070		 * so we use my 'housemade' write routine:
1071		 */
1072		error |= ll_w_block(fs->s_group_desc[i], waitfor == MNT_WAIT);
1073
1074        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
1075                if (fs->s_inode_bitmap[i])
1076                        ll_w_block (fs->s_inode_bitmap[i], 1);
1077        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
1078                if (fs->s_block_bitmap[i])
1079                        ll_w_block (fs->s_block_bitmap[i], 1);
1080
1081	return (error);
1082}
1083