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