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