ext2_vfsops.c revision 76688
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 76688 2001-05-16 18:04:37Z iedowse $
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/disklabel.h>
57#include <sys/malloc.h>
58#include <sys/stat.h>
59#include <sys/mutex.h>
60
61#include <ufs/ufs/extattr.h>
62#include <ufs/ufs/quota.h>
63#include <ufs/ufs/ufsmount.h>
64#include <ufs/ufs/inode.h>
65#include <ufs/ufs/ufs_extern.h>
66
67
68#include <gnu/ext2fs/fs.h>
69#include <gnu/ext2fs/ext2_extern.h>
70#include <gnu/ext2fs/ext2_fs.h>
71#include <gnu/ext2fs/ext2_fs_sb.h>
72
73static int ext2_fhtovp __P((struct mount *, struct fid *, struct vnode **));
74static int ext2_flushfiles __P((struct mount *mp, int flags, struct proc *p));
75static int ext2_mount __P((struct mount *,
76	    char *, caddr_t, struct nameidata *, struct proc *));
77static int ext2_mountfs __P((struct vnode *, struct mount *, struct proc *));
78static int ext2_reload __P((struct mount *mountp, struct ucred *cred,
79			struct proc *p));
80static int ext2_sbupdate __P((struct ufsmount *, int));
81static int ext2_statfs __P((struct mount *, struct statfs *, struct proc *));
82static int ext2_sync __P((struct mount *, int, struct ucred *, struct proc *));
83static int ext2_unmount __P((struct mount *, int, struct proc *));
84static int ext2_vget __P((struct mount *, ino_t, struct vnode **));
85static int ext2_vptofh __P((struct vnode *, struct fid *));
86
87static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
88
89static struct vfsops ext2fs_vfsops = {
90	ext2_mount,
91	ufs_start,		/* empty function */
92	ext2_unmount,
93	ufs_root,		/* root inode via vget */
94	ufs_quotactl,		/* does operations associated with quotas */
95	ext2_statfs,
96	ext2_sync,
97	ext2_vget,
98	ext2_fhtovp,
99	vfs_stdcheckexp,
100	ext2_vptofh,
101	ext2_init,
102	vfs_stduninit,
103	vfs_stdextattrctl,
104};
105
106VFS_SET(ext2fs_vfsops, ext2fs, 0);
107#define bsd_malloc malloc
108#define bsd_free free
109
110static int ext2fs_inode_hash_lock;
111
112static int	ext2_check_sb_compat __P((struct ext2_super_block *es,
113					  dev_t dev, int ronly));
114static int	compute_sb_data __P((struct vnode * devvp,
115				     struct ext2_super_block * es,
116				     struct ext2_sb_info * fs));
117
118#ifdef notyet
119static int ext2_mountroot __P((void));
120
121/*
122 * Called by main() when ext2fs is going to be mounted as root.
123 *
124 * Name is updated by mount(8) after booting.
125 */
126#define ROOTNAME	"root_device"
127
128static int
129ext2_mountroot()
130{
131	register struct ext2_sb_info *fs;
132	register struct mount *mp;
133	struct proc *p = curproc;
134	struct ufsmount *ump;
135	u_int size;
136	int error;
137
138	if ((error = bdevvp(rootdev, &rootvp))) {
139		printf("ext2_mountroot: can't find rootvp\n");
140		return (error);
141	}
142	mp = bsd_malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
143	bzero((char *)mp, (u_long)sizeof(struct mount));
144	mp->mnt_op = &ext2fs_vfsops;
145	mp->mnt_flag = MNT_RDONLY;
146	if (error = ext2_mountfs(rootvp, mp, p)) {
147		bsd_free(mp, M_MOUNT);
148		return (error);
149	}
150	if (error = vfs_lock(mp)) {
151		(void)ext2_unmount(mp, 0, p);
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, p);
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, p)
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 proc *p;
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, p))
214				return (EBUSY);
215			error = ext2_flushfiles(mp, flags, p);
216			vfs_unbusy(mp, p);
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, p);
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(p)) {
237				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
238				if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
239				    p->p_ucred, p)) != 0) {
240					VOP_UNLOCK(devvp, 0, p);
241					return (error);
242				}
243				VOP_UNLOCK(devvp, 0, p);
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, p);
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(p)) {
290		accessmode = VREAD;
291		if ((mp->mnt_flag & MNT_RDONLY) == 0)
292			accessmode |= VWRITE;
293		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
294		if ((error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) != 0) {
295			vput(devvp);
296			return (error);
297		}
298		VOP_UNLOCK(devvp, 0, p);
299	}
300
301	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
302		error = ext2_mountfs(devvp, mp, p);
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, p);
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, p)
526	register struct mount *mountp;
527	struct ucred *cred;
528	struct proc *p;
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, p, 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 = LIST_FIRST(&mountp->mnt_vnodelist); vp != NULL; vp = nvp) {
572		if (vp->v_mount != mountp) {
573			mtx_unlock(&mntvnode_mtx);
574			goto loop;
575		}
576		nvp = LIST_NEXT(vp, v_mntvnodes);
577		/*
578		 * Step 4: invalidate all inactive vnodes.
579		 */
580  		if (vrecycle(vp, &mntvnode_mtx, p))
581  			goto loop;
582		/*
583		 * Step 5: invalidate all cached file data.
584		 */
585		mtx_lock(&vp->v_interlock);
586		mtx_unlock(&mntvnode_mtx);
587		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
588			goto loop;
589		}
590		if (vinvalbuf(vp, 0, cred, p, 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, p)
619	register struct vnode *devvp;
620	struct mount *mp;
621	struct proc *p;
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	struct partinfo dpart;
629	int havepart = 0;
630	int error, i, size;
631	int ronly;
632
633	/*
634	 * Disallow multiple mounts of the same device.
635	 * Disallow mounting of a device that is currently in use
636	 * (except for root, which might share swap device for miniroot).
637	 * Flush out any old buffers remaining from a previous use.
638	 */
639	if ((error = vfs_mountedon(devvp)) != 0)
640		return (error);
641	if (vcount(devvp) > 1 && devvp != rootvp)
642		return (EBUSY);
643	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
644		return (error);
645#ifdef READONLY
646/* turn on this to force it to be read-only */
647	mp->mnt_flag |= MNT_RDONLY;
648#endif
649
650	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
651	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
652	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
653	VOP_UNLOCK(devvp, 0, p);
654	if (error)
655		return (error);
656	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
657		size = DEV_BSIZE;
658	else {
659		havepart = 1;
660		size = dpart.disklab->d_secsize;
661	}
662
663	bp = NULL;
664	ump = NULL;
665	if ((error = bread(devvp, SBLOCK, SBSIZE, NOCRED, &bp)) != 0)
666		goto out;
667	es = (struct ext2_super_block *)bp->b_data;
668	if (ext2_check_sb_compat(es, dev, ronly) != 0) {
669		error = EINVAL;		/* XXX needs translation */
670		goto out;
671	}
672	if ((es->s_state & EXT2_VALID_FS) == 0 ||
673	    (es->s_state & EXT2_ERROR_FS)) {
674		if (ronly || (mp->mnt_flag & MNT_FORCE)) {
675			printf(
676"WARNING: Filesystem was not properly dismounted\n");
677		} else {
678			printf(
679"WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
680			error = EPERM;
681			goto out;
682		}
683	}
684	ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
685	bzero((caddr_t)ump, sizeof *ump);
686	ump->um_malloctype = M_EXT2NODE;
687	ump->um_blkatoff = ext2_blkatoff;
688	ump->um_truncate = ext2_truncate;
689	ump->um_update = ext2_update;
690	ump->um_valloc = ext2_valloc;
691	ump->um_vfree = ext2_vfree;
692	/* I don't know whether this is the right strategy. Note that
693	   we dynamically allocate both a ext2_sb_info and a ext2_super_block
694	   while Linux keeps the super block in a locked buffer
695	 */
696	ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info),
697		M_UFSMNT, M_WAITOK);
698	ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block),
699		M_UFSMNT, M_WAITOK);
700	bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
701	if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
702		goto out;
703	/*
704	 * We don't free the group descriptors allocated by compute_sb_data()
705	 * until ext2_unmount().  This is OK since the mount will succeed.
706	 */
707	brelse(bp);
708	bp = NULL;
709	fs = ump->um_e2fs;
710	fs->s_rd_only = ronly;	/* ronly is set according to mnt_flags */
711	/* if the fs is not mounted read-only, make sure the super block is
712	   always written back on a sync()
713	 */
714	fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
715	if (ronly == 0) {
716		fs->s_dirt = 1;		/* mark it modified */
717		fs->s_es->s_state &= ~EXT2_VALID_FS;	/* set fs invalid */
718	}
719	mp->mnt_data = (qaddr_t)ump;
720	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
721	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
722	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
723	mp->mnt_flag |= MNT_LOCAL;
724	ump->um_mountp = mp;
725	ump->um_dev = dev;
726	ump->um_devvp = devvp;
727	/* setting those two parameters allows us to use
728	   ufs_bmap w/o changse !
729	*/
730	ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
731	ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
732	ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
733	for (i = 0; i < MAXQUOTAS; i++)
734		ump->um_quotas[i] = NULLVP;
735	devvp->v_rdev->si_mountpoint = mp;
736	if (ronly == 0)
737		ext2_sbupdate(ump, MNT_WAIT);
738	return (0);
739out:
740	if (bp)
741		brelse(bp);
742	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
743	if (ump) {
744		bsd_free(ump->um_e2fs->s_es, M_UFSMNT);
745		bsd_free(ump->um_e2fs, M_UFSMNT);
746		bsd_free(ump, M_UFSMNT);
747		mp->mnt_data = (qaddr_t)0;
748	}
749	return (error);
750}
751
752/*
753 * unmount system call
754 */
755static int
756ext2_unmount(mp, mntflags, p)
757	struct mount *mp;
758	int mntflags;
759	struct proc *p;
760{
761	register struct ufsmount *ump;
762	register struct ext2_sb_info *fs;
763	int error, flags, ronly, i;
764
765	flags = 0;
766	if (mntflags & MNT_FORCE) {
767		if (mp->mnt_flag & MNT_ROOTFS)
768			return (EINVAL);
769		flags |= FORCECLOSE;
770	}
771	if ((error = ext2_flushfiles(mp, flags, p)) != 0)
772		return (error);
773	ump = VFSTOUFS(mp);
774	fs = ump->um_e2fs;
775	ronly = fs->s_rd_only;
776	if (ronly == 0) {
777		if (fs->s_wasvalid)
778			fs->s_es->s_state |= EXT2_VALID_FS;
779		ext2_sbupdate(ump, MNT_WAIT);
780	}
781
782	/* release buffers containing group descriptors */
783	for(i = 0; i < fs->s_db_per_group; i++)
784		ULCK_BUF(fs->s_group_desc[i])
785	bsd_free(fs->s_group_desc, M_UFSMNT);
786
787	/* release cached inode/block bitmaps */
788        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
789                if (fs->s_inode_bitmap[i])
790			ULCK_BUF(fs->s_inode_bitmap[i])
791
792        for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
793                if (fs->s_block_bitmap[i])
794			ULCK_BUF(fs->s_block_bitmap[i])
795
796	ump->um_devvp->v_rdev->si_mountpoint = NULL;
797	error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
798		NOCRED, p);
799	vrele(ump->um_devvp);
800	bsd_free(fs->s_es, M_UFSMNT);
801	bsd_free(fs, M_UFSMNT);
802	bsd_free(ump, M_UFSMNT);
803	mp->mnt_data = (qaddr_t)0;
804	mp->mnt_flag &= ~MNT_LOCAL;
805	return (error);
806}
807
808/*
809 * Flush out all the files in a filesystem.
810 */
811static int
812ext2_flushfiles(mp, flags, p)
813	register struct mount *mp;
814	int flags;
815	struct proc *p;
816{
817	register struct ufsmount *ump;
818	int error;
819#if QUOTA
820	int i;
821#endif
822
823	ump = VFSTOUFS(mp);
824#if QUOTA
825	if (mp->mnt_flag & MNT_QUOTA) {
826		if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
827			return (error);
828		for (i = 0; i < MAXQUOTAS; i++) {
829			if (ump->um_quotas[i] == NULLVP)
830				continue;
831			quotaoff(p, mp, i);
832		}
833		/*
834		 * Here we fall through to vflush again to ensure
835		 * that we have gotten rid of all the system vnodes.
836		 */
837	}
838#endif
839	error = vflush(mp, 0, flags);
840	return (error);
841}
842
843/*
844 * Get file system statistics.
845 * taken from ext2/super.c ext2_statfs
846 */
847static int
848ext2_statfs(mp, sbp, p)
849	struct mount *mp;
850	register struct statfs *sbp;
851	struct proc *p;
852{
853        unsigned long overhead;
854	register struct ufsmount *ump;
855	register struct ext2_sb_info *fs;
856	register struct ext2_super_block *es;
857	int i, nsb;
858
859	ump = VFSTOUFS(mp);
860	fs = ump->um_e2fs;
861	es = fs->s_es;
862
863	if (es->s_magic != EXT2_SUPER_MAGIC)
864		panic("ext2_statfs - magic number spoiled");
865
866	/*
867	 * Compute the overhead (FS structures)
868	 */
869	if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
870		nsb = 0;
871		for (i = 0 ; i < fs->s_groups_count; i++)
872			if (ext2_group_sparse(i))
873				nsb++;
874	} else
875		nsb = fs->s_groups_count;
876	overhead = es->s_first_data_block +
877	    /* Superblocks and block group descriptors: */
878	    nsb * (1 + fs->s_db_per_group) +
879	    /* Inode bitmap, block bitmap, and inode table: */
880	    fs->s_groups_count * (1 + 1 + fs->s_itb_per_group);
881
882	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
883	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
884	sbp->f_blocks = es->s_blocks_count - overhead;
885	sbp->f_bfree = es->s_free_blocks_count;
886	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
887	sbp->f_files = es->s_inodes_count;
888	sbp->f_ffree = es->s_free_inodes_count;
889	if (sbp != &mp->mnt_stat) {
890		sbp->f_type = mp->mnt_vfc->vfc_typenum;
891		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
892			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
893		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
894			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
895	}
896	return (0);
897}
898
899/*
900 * Go through the disk queues to initiate sandbagged IO;
901 * go through the inodes to write those that have been modified;
902 * initiate the writing of the super block if it has been modified.
903 *
904 * Note: we are always called with the filesystem marked `MPBUSY'.
905 */
906static int
907ext2_sync(mp, waitfor, cred, p)
908	struct mount *mp;
909	int waitfor;
910	struct ucred *cred;
911	struct proc *p;
912{
913	struct vnode *nvp, *vp;
914	struct inode *ip;
915	struct ufsmount *ump = VFSTOUFS(mp);
916	struct ext2_sb_info *fs;
917	int error, allerror = 0;
918
919	fs = ump->um_e2fs;
920	if (fs->s_dirt != 0 && fs->s_rd_only != 0) {		/* XXX */
921		printf("fs = %s\n", fs->fs_fsmnt);
922		panic("ext2_sync: rofs mod");
923	}
924	/*
925	 * Write back each (modified) inode.
926	 */
927	mtx_lock(&mntvnode_mtx);
928loop:
929	for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
930		/*
931		 * If the vnode that we are about to sync is no longer
932		 * associated with this mount point, start over.
933		 */
934		if (vp->v_mount != mp)
935			goto loop;
936		mtx_lock(&vp->v_interlock);
937		nvp = LIST_NEXT(vp, v_mntvnodes);
938		ip = VTOI(vp);
939		if (vp->v_type == VNON ||
940		    ((ip->i_flag &
941		    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
942		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
943			mtx_unlock(&vp->v_interlock);
944			continue;
945		}
946		mtx_unlock(&mntvnode_mtx);
947		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
948		if (error) {
949			mtx_lock(&mntvnode_mtx);
950			if (error == ENOENT)
951				goto loop;
952			continue;
953		}
954		if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
955			allerror = error;
956		VOP_UNLOCK(vp, 0, p);
957		vrele(vp);
958		mtx_lock(&mntvnode_mtx);
959	}
960	mtx_unlock(&mntvnode_mtx);
961	/*
962	 * Force stale file system control information to be flushed.
963	 */
964	if (waitfor != MNT_LAZY) {
965		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
966		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
967			allerror = error;
968		VOP_UNLOCK(ump->um_devvp, 0, p);
969	}
970#if QUOTA
971	qsync(mp);
972#endif
973	/*
974	 * Write back modified superblock.
975	 */
976	if (fs->s_dirt != 0) {
977		fs->s_dirt = 0;
978		fs->s_es->s_wtime = time_second;
979		if ((error = ext2_sbupdate(ump, waitfor)) != 0)
980			allerror = error;
981	}
982	return (allerror);
983}
984
985/*
986 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
987 * in from disk.  If it is in core, wait for the lock bit to clear, then
988 * return the inode locked.  Detection and handling of mount points must be
989 * done by the calling routine.
990 */
991static int
992ext2_vget(mp, ino, vpp)
993	struct mount *mp;
994	ino_t ino;
995	struct vnode **vpp;
996{
997	register struct ext2_sb_info *fs;
998	register struct inode *ip;
999	struct ufsmount *ump;
1000	struct buf *bp;
1001	struct vnode *vp;
1002	dev_t dev;
1003	int i, error;
1004	int used_blocks;
1005
1006	ump = VFSTOUFS(mp);
1007	dev = ump->um_dev;
1008restart:
1009	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
1010		return (0);
1011
1012	/*
1013	 * Lock out the creation of new entries in the FFS hash table in
1014	 * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1015	 * may occur!
1016	 */
1017	if (ext2fs_inode_hash_lock) {
1018		while (ext2fs_inode_hash_lock) {
1019			ext2fs_inode_hash_lock = -1;
1020			tsleep(&ext2fs_inode_hash_lock, PVM, "e2vget", 0);
1021		}
1022		goto restart;
1023	}
1024	ext2fs_inode_hash_lock = 1;
1025
1026	/*
1027	 * If this MALLOC() is performed after the getnewvnode()
1028	 * it might block, leaving a vnode with a NULL v_data to be
1029	 * found by ext2_sync() if a sync happens to fire right then,
1030	 * which will cause a panic because ext2_sync() blindly
1031	 * dereferences vp->v_data (as well it should).
1032	 */
1033	MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1034
1035	/* Allocate a new vnode/inode. */
1036	if ((error = getnewvnode(VT_UFS, mp, ext2_vnodeop_p, &vp)) != 0) {
1037		if (ext2fs_inode_hash_lock < 0)
1038			wakeup(&ext2fs_inode_hash_lock);
1039		ext2fs_inode_hash_lock = 0;
1040		*vpp = NULL;
1041		FREE(ip, M_EXT2NODE);
1042		return (error);
1043	}
1044	bzero((caddr_t)ip, sizeof(struct inode));
1045	lockinit(&vp->v_lock, PINOD, "ext2in", 0, 0);
1046	vp->v_data = ip;
1047	ip->i_vnode = vp;
1048	ip->i_e2fs = fs = ump->um_e2fs;
1049	ip->i_dev = dev;
1050	ip->i_number = ino;
1051#if QUOTA
1052	for (i = 0; i < MAXQUOTAS; i++)
1053		ip->i_dquot[i] = NODQUOT;
1054#endif
1055	/*
1056	 * Put it onto its hash chain and lock it so that other requests for
1057	 * this inode will block if they arrive while we are sleeping waiting
1058	 * for old data structures to be purged or for the contents of the
1059	 * disk portion of this inode to be read.
1060	 */
1061	ufs_ihashins(ip);
1062
1063	if (ext2fs_inode_hash_lock < 0)
1064		wakeup(&ext2fs_inode_hash_lock);
1065	ext2fs_inode_hash_lock = 0;
1066
1067	/* Read in the disk contents for the inode, copy into the inode. */
1068#if 0
1069printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1070#endif
1071	if ((error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1072	    (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
1073		/*
1074		 * The inode does not contain anything useful, so it would
1075		 * be misleading to leave it on its hash chain. With mode
1076		 * still zero, it will be unlinked and returned to the free
1077		 * list by vput().
1078		 */
1079		vput(vp);
1080		brelse(bp);
1081		*vpp = NULL;
1082		return (error);
1083	}
1084	/* convert ext2 inode to dinode */
1085	ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
1086			ino_to_fsbo(fs, ino)), &ip->i_din);
1087	ip->i_block_group = ino_to_cg(fs, ino);
1088	ip->i_next_alloc_block = 0;
1089	ip->i_next_alloc_goal = 0;
1090	ip->i_prealloc_count = 0;
1091	ip->i_prealloc_block = 0;
1092        /* now we want to make sure that block pointers for unused
1093           blocks are zeroed out - ext2_balloc depends on this
1094	   although for regular files and directories only
1095	*/
1096	if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1097		used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1098		for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1099			ip->i_db[i] = 0;
1100	}
1101/*
1102	ext2_print_inode(ip);
1103*/
1104	brelse(bp);
1105
1106	/*
1107	 * Initialize the vnode from the inode, check for aliases.
1108	 * Note that the underlying vnode may have changed.
1109	 */
1110	if ((error = ufs_vinit(mp, ext2_specop_p, ext2_fifoop_p, &vp)) != 0) {
1111		vput(vp);
1112		*vpp = NULL;
1113		return (error);
1114	}
1115	/*
1116	 * Finish inode initialization now that aliasing has been resolved.
1117	 */
1118	ip->i_devvp = ump->um_devvp;
1119	VREF(ip->i_devvp);
1120	/*
1121	 * Set up a generation number for this inode if it does not
1122	 * already have one. This should only happen on old filesystems.
1123	 */
1124	if (ip->i_gen == 0) {
1125		ip->i_gen = random() / 2 + 1;
1126		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1127			ip->i_flag |= IN_MODIFIED;
1128	}
1129	*vpp = vp;
1130	return (0);
1131}
1132
1133/*
1134 * File handle to vnode
1135 *
1136 * Have to be really careful about stale file handles:
1137 * - check that the inode number is valid
1138 * - call ext2_vget() to get the locked inode
1139 * - check for an unallocated inode (i_mode == 0)
1140 * - check that the given client host has export rights and return
1141 *   those rights via. exflagsp and credanonp
1142 */
1143static int
1144ext2_fhtovp(mp, fhp, vpp)
1145	register struct mount *mp;
1146	struct fid *fhp;
1147	struct vnode **vpp;
1148{
1149	register struct ufid *ufhp;
1150	struct ext2_sb_info *fs;
1151
1152	ufhp = (struct ufid *)fhp;
1153	fs = VFSTOUFS(mp)->um_e2fs;
1154	if (ufhp->ufid_ino < ROOTINO ||
1155	    ufhp->ufid_ino >= fs->s_groups_count * fs->s_es->s_inodes_per_group)
1156		return (ESTALE);
1157	return (ufs_fhtovp(mp, ufhp, vpp));
1158}
1159
1160/*
1161 * Vnode pointer to File handle
1162 */
1163/* ARGSUSED */
1164static int
1165ext2_vptofh(vp, fhp)
1166	struct vnode *vp;
1167	struct fid *fhp;
1168{
1169	register struct inode *ip;
1170	register struct ufid *ufhp;
1171
1172	ip = VTOI(vp);
1173	ufhp = (struct ufid *)fhp;
1174	ufhp->ufid_len = sizeof(struct ufid);
1175	ufhp->ufid_ino = ip->i_number;
1176	ufhp->ufid_gen = ip->i_gen;
1177	return (0);
1178}
1179
1180/*
1181 * Write a superblock and associated information back to disk.
1182 */
1183static int
1184ext2_sbupdate(mp, waitfor)
1185	struct ufsmount *mp;
1186	int waitfor;
1187{
1188	register struct ext2_sb_info *fs = mp->um_e2fs;
1189	register struct ext2_super_block *es = fs->s_es;
1190	register struct buf *bp;
1191	int error = 0;
1192/*
1193printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1194*/
1195	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1196	bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1197	if (waitfor == MNT_WAIT)
1198		error = bwrite(bp);
1199	else
1200		bawrite(bp);
1201
1202	/*
1203	 * The buffers for group descriptors, inode bitmaps and block bitmaps
1204	 * are not busy at this point and are (hopefully) written by the
1205	 * usual sync mechanism. No need to write them here
1206		 */
1207
1208	return (error);
1209}
1210