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