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