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