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