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