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