Major BUF/BIO work commit. Make I/O BIO-centric and specify the disk or
[dragonfly.git] / sys / vfs / 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  *      $FreeBSD: src/sys/gnu/ext2fs/ext2_vfsops.c,v 1.63.2.7 2002/07/01 00:18:51 iedowse Exp $
41  *      $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_vfsops.c,v 1.33 2006/03/24 18:35:33 dillon Exp $
42  */
43
44 #include "opt_quota.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/nlookup.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/buf.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/disklabel.h>
57 #include <sys/malloc.h>
58 #include <sys/stat.h>
59 #include <sys/buf2.h>
60 #include <sys/thread2.h>
61
62 #include <vfs/ufs/quota.h>
63 #include <vfs/ufs/ufsmount.h>
64 #include <vfs/ufs/inode.h>
65 #include <vfs/ufs/ufs_extern.h>
66
67 #include <vm/vm_zone.h>
68
69 #include "fs.h"
70 #include "ext2_extern.h"
71 #include "ext2_fs.h"
72 #include "ext2_fs_sb.h"
73
74 extern struct vnodeopv_entry_desc ext2_vnodeop_entries[];
75 extern struct vnodeopv_entry_desc ext2_specop_entries[];
76 extern struct vnodeopv_entry_desc ext2_fifoop_entries[];
77
78 static int ext2_fhtovp (struct mount *, struct fid *, struct vnode **);
79 static int ext2_flushfiles (struct mount *mp, int flags, struct thread *td);
80 static int ext2_mount (struct mount *, char *, caddr_t, struct thread *);
81 static int ext2_mountfs (struct vnode *, struct mount *, struct thread *);
82 static int ext2_reload (struct mount *mountp, struct ucred *cred,
83                         struct thread *p);
84 static int ext2_sbupdate (struct ufsmount *, int);
85 static int ext2_statfs (struct mount *, struct statfs *, struct thread *);
86 static int ext2_sync (struct mount *, int, struct thread *);
87 static int ext2_unmount (struct mount *, int, struct thread *);
88 static int ext2_vget (struct mount *, ino_t, struct vnode **);
89 static int ext2_vptofh (struct vnode *, struct fid *);
90
91 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
92
93 static struct vfsops ext2fs_vfsops = {
94         .vfs_mount =            ext2_mount,
95         .vfs_unmount =          ext2_unmount,
96         .vfs_root =             ufs_root,       /* root inode via vget */
97         .vfs_quotactl =         ufs_quotactl,   /* quota operations */
98         .vfs_statfs =           ext2_statfs,
99         .vfs_sync =             ext2_sync,
100         .vfs_vget =             ext2_vget,
101         .vfs_fhtovp =           ext2_fhtovp,
102         .vfs_checkexp =         ufs_check_export,
103         .vfs_vptofh =           ext2_vptofh,
104         .vfs_init =             ufs_init,
105         .vfs_uninit =           ufs_uninit
106 };
107
108 VFS_SET(ext2fs_vfsops, ext2fs, 0);
109 #define bsd_malloc malloc
110 #define bsd_free free
111
112 static int ext2fs_inode_hash_lock;
113
114 static int      ext2_check_sb_compat (struct ext2_super_block *es,
115                                           dev_t dev, int ronly);
116 static int      compute_sb_data (struct vnode * devvp,
117                                      struct ext2_super_block * es,
118                                      struct ext2_sb_info * fs);
119
120 /*
121  * VFS Operations.
122  *
123  * mount system call
124  *
125  * Parameters:
126  *      data:   this is actually a (struct ufs_args *)
127  */
128 static int
129 ext2_mount(struct mount *mp, char *path, caddr_t data,
130            struct thread *td)
131 {
132         struct vnode *devvp;
133         struct ufs_args args;
134         struct ufsmount *ump = 0;
135         struct ext2_sb_info *fs;
136         size_t size;
137         int error, flags;
138         mode_t accessmode;
139         struct ucred *cred;
140         struct nlookupdata nd;
141
142         if ((error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args))) != 0)
143                 return (error);
144
145         cred = td->td_proc->p_ucred;
146         /*
147          * If updating, check whether changing from read-only to
148          * read/write; if there is no device name, that's all we do.
149          */
150         if (mp->mnt_flag & MNT_UPDATE) {
151                 ump = VFSTOUFS(mp);
152                 fs = ump->um_e2fs;
153                 error = 0;
154                 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
155                         flags = WRITECLOSE;
156                         if (mp->mnt_flag & MNT_FORCE)
157                                 flags |= FORCECLOSE;
158                         if (vfs_busy(mp, LK_NOWAIT, td))
159                                 return (EBUSY);
160                         error = ext2_flushfiles(mp, flags, td);
161                         vfs_unbusy(mp, td);
162                         if (!error && fs->s_wasvalid) {
163                                 fs->s_es->s_state |= EXT2_VALID_FS;
164                                 ext2_sbupdate(ump, MNT_WAIT);
165                         }
166                         fs->s_rd_only = 1;
167                 }
168                 if (!error && (mp->mnt_flag & MNT_RELOAD))
169                         error = ext2_reload(mp, proc0.p_ucred, td);
170                 if (error)
171                         return (error);
172                 devvp = ump->um_devvp;
173                 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
174                     (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
175                         return (EPERM);
176                 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
177                         /*
178                          * If upgrade to read-write by non-root, then verify
179                          * that user has necessary permissions on the device.
180                          */
181                         if (cred->cr_uid != 0) {
182                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
183                                 if ((error = VOP_ACCESS(devvp, VREAD | VWRITE,
184                                     cred, td)) != 0) {
185                                         VOP_UNLOCK(devvp, 0, td);
186                                         return (error);
187                                 }
188                                 VOP_UNLOCK(devvp, 0, td);
189                         }
190
191                         if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
192                             (fs->s_es->s_state & EXT2_ERROR_FS)) {
193                                 if (mp->mnt_flag & MNT_FORCE) {
194                                         printf(
195 "WARNING: %s was not properly dismounted\n",
196                                             fs->fs_fsmnt);
197                                 } else {
198                                         printf(
199 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
200                                             fs->fs_fsmnt);
201                                         return (EPERM);
202                                 }
203                         }
204                         fs->s_es->s_state &= ~EXT2_VALID_FS;
205                         ext2_sbupdate(ump, MNT_WAIT);
206                         fs->s_rd_only = 0;
207                 }
208                 if (args.fspec == 0) {
209                         /*
210                          * Process export requests.
211                          */
212                         return (vfs_export(mp, &ump->um_export, &args.export));
213                 }
214         }
215         /*
216          * Not an update, or updating the name: look up the name
217          * and verify that it refers to a sensible block device.
218          */
219         devvp = NULL;
220         error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
221         if (error == 0)
222                 error = nlookup(&nd);
223         if (error == 0)
224                 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
225         nlookup_done(&nd);
226         if (error)
227                 return (error);
228
229         if (!vn_isdisk(devvp, &error)) {
230                 vrele(devvp);
231                 return (error);
232         }
233
234         /*
235          * If mount by non-root, then verify that user has necessary
236          * permissions on the device.
237          */
238         if (cred->cr_uid != 0) {
239                 accessmode = VREAD;
240                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
241                         accessmode |= VWRITE;
242                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
243                 if ((error = VOP_ACCESS(devvp, accessmode, cred, td)) != 0) {
244                         vput(devvp);
245                         return (error);
246                 }
247                 VOP_UNLOCK(devvp, 0, td);
248         }
249
250         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
251                 error = ext2_mountfs(devvp, mp, td);
252         } else {
253                 if (devvp != ump->um_devvp)
254                         error = EINVAL; /* needs translation */
255                 else
256                         vrele(devvp);
257         }
258         if (error) {
259                 vrele(devvp);
260                 return (error);
261         }
262         ump = VFSTOUFS(mp);
263         fs = ump->um_e2fs;
264         copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
265         bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
266         copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
267         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
268         ext2_statfs(mp, &mp->mnt_stat, td);
269         return (0);
270 }
271
272 /*
273  * checks that the data in the descriptor blocks make sense
274  * this is taken from ext2/super.c
275  */
276 static int
277 ext2_check_descriptors(struct ext2_sb_info *sb)
278 {
279         int i;
280         int desc_block = 0;
281         unsigned long block = sb->s_es->s_first_data_block;
282         struct ext2_group_desc * gdp = NULL;
283
284         /* ext2_debug ("Checking group descriptors"); */
285
286         for (i = 0; i < sb->s_groups_count; i++)
287         {
288                 /* examine next descriptor block */
289                 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
290                         gdp = (struct ext2_group_desc *) 
291                                 sb->s_group_desc[desc_block++]->b_data;
292                 if (gdp->bg_block_bitmap < block ||
293                     gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
294                 {
295                         printf ("ext2_check_descriptors: "
296                                     "Block bitmap for group %d"
297                                     " not in group (block %lu)!\n",
298                                     i, (unsigned long) gdp->bg_block_bitmap);
299                         return 0;
300                 }
301                 if (gdp->bg_inode_bitmap < block ||
302                     gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
303                 {
304                         printf ("ext2_check_descriptors: "
305                                     "Inode bitmap for group %d"
306                                     " not in group (block %lu)!\n",
307                                     i, (unsigned long) gdp->bg_inode_bitmap);
308                         return 0;
309                 }
310                 if (gdp->bg_inode_table < block ||
311                     gdp->bg_inode_table + sb->s_itb_per_group >=
312                     block + EXT2_BLOCKS_PER_GROUP(sb))
313                 {
314                         printf ("ext2_check_descriptors: "
315                                     "Inode table for group %d"
316                                     " not in group (block %lu)!\n",
317                                     i, (unsigned long) gdp->bg_inode_table);
318                         return 0;
319                 }
320                 block += EXT2_BLOCKS_PER_GROUP(sb);
321                 gdp++;
322         }
323         return 1;
324 }
325
326 static int
327 ext2_check_sb_compat(struct ext2_super_block *es, dev_t dev, int ronly)
328 {
329         if (es->s_magic != EXT2_SUPER_MAGIC) {
330                 printf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
331                     devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
332                 return (1);
333         }
334         if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
335                 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
336                         printf(
337 "WARNING: mount of %s denied due to unsupported optional features\n",
338                             devtoname(dev));
339                         return (1);
340                 }
341                 if (!ronly &&
342                     (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
343                         printf(
344 "WARNING: R/W mount of %s denied due to unsupported optional features\n",
345                             devtoname(dev));
346                         return (1);
347                 }
348         }
349         return (0);
350 }
351
352 /*
353  * this computes the fields of the  ext2_sb_info structure from the
354  * data in the ext2_super_block structure read in
355  */
356 static int
357 compute_sb_data(struct vnode *devvp, 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, fsbtodoff(fs, logic_sb_block + i + 1), 
420                       fs->s_blocksize, &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 ext2_reload_scan1(struct mount *mp, struct vnode *vp, void *rescan);
465 static int ext2_reload_scan2(struct mount *mp, struct vnode *vp, void *rescan);
466
467 struct scaninfo {
468         int rescan;
469         int allerror;
470         int waitfor;
471         thread_t td;
472         struct vnode *devvp;
473         struct ext2_sb_info *fs;
474 };
475
476 static int
477 ext2_reload(struct mount *mountp, struct ucred *cred, struct thread *td)
478 {
479         struct vnode *devvp;
480         struct buf *bp;
481         struct ext2_super_block * es;
482         struct ext2_sb_info *fs;
483         int error;
484         struct scaninfo scaninfo;
485
486         if ((mountp->mnt_flag & MNT_RDONLY) == 0)
487                 return (EINVAL);
488         /*
489          * Step 1: invalidate all cached meta-data.
490          */
491         devvp = VFSTOUFS(mountp)->um_devvp;
492         if (vinvalbuf(devvp, 0, td, 0, 0))
493                 panic("ext2_reload: dirty1");
494         /*
495          * Step 2: re-read superblock from disk.
496          * constants have been adjusted for ext2
497          */
498         if ((error = bread(devvp, dbtob(SBLOCK), SBSIZE, &bp)) != 0)
499                 return (error);
500         es = (struct ext2_super_block *)bp->b_data;
501         if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
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)) != 0) {
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         scaninfo.rescan = 1;
519         scaninfo.td = td;
520         scaninfo.devvp = devvp;
521         scaninfo.fs = fs;
522         while (error == 0 && scaninfo.rescan) {
523             scaninfo.rescan = 0;
524             error = vmntvnodescan(mountp, VMSC_GETVX, ext2_reload_scan1, 
525                                 ext2_reload_scan2, &scaninfo);
526         }
527         return(error);
528 }
529
530 static int
531 ext2_reload_scan1(struct mount *mp, struct vnode *vp, void *data)
532 {
533         /*struct scaninfo *info = data;*/
534
535         return(0);
536 }
537
538 static int
539 ext2_reload_scan2(struct mount *mp, struct vnode *vp, void *data)
540 {
541         struct scaninfo *info = data;
542         struct inode *ip;
543         struct buf *bp;
544         int error;
545
546         /*
547          * Try to recycle
548          */
549         if (vrecycle(vp, curthread))
550                 return(0);
551
552         /*
553          * Step 5: invalidate all cached file data.
554          */
555         if (vinvalbuf(vp, 0, info->td, 0, 0))
556                 panic("ext2_reload: dirty2");
557         /*
558          * Step 6: re-read inode data for all active vnodes.
559          */
560         ip = VTOI(vp);
561         error = bread(info->devvp, 
562                       fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
563                       (int)info->fs->s_blocksize, &bp);
564         if (error)
565                 return (error);
566         ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + 
567             EXT2_INODE_SIZE * ino_to_fsbo(info->fs, ip->i_number)), 
568             &ip->i_din);
569         brelse(bp);
570         return(0);
571 }
572
573 /*
574  * Common code for mount and mountroot
575  */
576 static int
577 ext2_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td)
578 {
579         struct ufsmount *ump;
580         struct buf *bp;
581         struct ext2_sb_info *fs;
582         struct ext2_super_block * es;
583         dev_t dev;
584         struct partinfo dpart;
585         int havepart = 0;
586         int error, i, size;
587         int ronly;
588
589         /*
590          * Disallow multiple mounts of the same device.
591          * Disallow mounting of a device that is currently in use
592          * (except for root, which might share swap device for miniroot).
593          * Flush out any old buffers remaining from a previous use.
594          */
595         if ((error = vfs_mountedon(devvp)) != 0)
596                 return (error);
597         if (count_udev(devvp->v_udev) > 0)
598                 return (EBUSY);
599         if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)) != 0)
600                 return (error);
601 #ifdef READONLY
602 /* turn on this to force it to be read-only */
603         mp->mnt_flag |= MNT_RDONLY;
604 #endif
605
606         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
607         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
608         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL, td);
609         VOP_UNLOCK(devvp, 0, td);
610         if (error)
611                 return (error);
612         dev = devvp->v_rdev;
613         if (dev->si_iosize_max != 0)
614                 mp->mnt_iosize_max = dev->si_iosize_max;
615         if (mp->mnt_iosize_max > MAXPHYS)
616                 mp->mnt_iosize_max = MAXPHYS;
617         if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, td) != 0)
618                 size = DEV_BSIZE;
619         else {
620                 havepart = 1;
621                 size = dpart.disklab->d_secsize;
622         }
623
624         bp = NULL;
625         ump = NULL;
626         if ((error = bread(devvp, dbtob(SBLOCK), SBSIZE, &bp)) != 0)
627                 goto out;
628         es = (struct ext2_super_block *)bp->b_data;
629         if (ext2_check_sb_compat(es, dev, ronly) != 0) {
630                 error = EINVAL;         /* XXX needs translation */
631                 goto out;
632         }
633         if ((es->s_state & EXT2_VALID_FS) == 0 ||
634             (es->s_state & EXT2_ERROR_FS)) {
635                 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
636                         printf(
637 "WARNING: Filesystem was not properly dismounted\n");
638                 } else {
639                         printf(
640 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
641                         error = EPERM;
642                         goto out;
643                 }
644         }
645         ump = bsd_malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
646         bzero((caddr_t)ump, sizeof *ump);
647         ump->um_malloctype = M_EXT2NODE;
648         ump->um_blkatoff = ext2_blkatoff;
649         ump->um_truncate = ext2_truncate;
650         ump->um_update = ext2_update;
651         ump->um_valloc = ext2_valloc;
652         ump->um_vfree = ext2_vfree;
653         /* I don't know whether this is the right strategy. Note that
654            we dynamically allocate both a ext2_sb_info and a ext2_super_block
655            while Linux keeps the super block in a locked buffer
656          */
657         ump->um_e2fs = bsd_malloc(sizeof(struct ext2_sb_info), 
658                 M_UFSMNT, M_WAITOK);
659         ump->um_e2fs->s_es = bsd_malloc(sizeof(struct ext2_super_block), 
660                 M_UFSMNT, M_WAITOK);
661         bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
662         if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
663                 goto out;
664         /*
665          * We don't free the group descriptors allocated by compute_sb_data()
666          * until ext2_unmount().  This is OK since the mount will succeed.
667          */
668         brelse(bp);
669         bp = NULL;
670         fs = ump->um_e2fs;
671         fs->s_rd_only = ronly;  /* ronly is set according to mnt_flags */
672         /* if the fs is not mounted read-only, make sure the super block is 
673            always written back on a sync()
674          */
675         fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
676         if (ronly == 0) {
677                 fs->s_dirt = 1;         /* mark it modified */
678                 fs->s_es->s_state &= ~EXT2_VALID_FS;    /* set fs invalid */
679         }
680         mp->mnt_data = (qaddr_t)ump;
681         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
682         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
683         mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
684         mp->mnt_flag |= MNT_LOCAL;
685         ump->um_mountp = mp;
686         ump->um_dev = dev;
687         ump->um_devvp = devvp;
688         /* setting those two parameters allows us to use 
689            ufs_bmap w/o changse !
690         */
691         ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
692         ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
693         ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
694         for (i = 0; i < MAXQUOTAS; i++)
695                 ump->um_quotas[i] = NULLVP; 
696         dev->si_mountpoint = mp;
697
698         vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops, 
699                          ext2_vnodeop_entries, 0);
700         vfs_add_vnodeops(mp, &mp->mnt_vn_spec_ops,
701                          ext2_specop_entries, 0);
702         vfs_add_vnodeops(mp, &mp->mnt_vn_fifo_ops,
703                          ext2_fifoop_entries, 0);
704
705         if (ronly == 0) 
706                 ext2_sbupdate(ump, MNT_WAIT);
707         return (0);
708 out:
709         if (bp)
710                 brelse(bp);
711         VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, td);
712         if (ump) {
713                 bsd_free(ump->um_e2fs->s_es, M_UFSMNT);
714                 bsd_free(ump->um_e2fs, M_UFSMNT);
715                 bsd_free(ump, M_UFSMNT);
716                 mp->mnt_data = (qaddr_t)0;
717         }
718         return (error);
719 }
720
721 /*
722  * unmount system call
723  */
724 static int
725 ext2_unmount(struct mount *mp, int mntflags, struct thread *td)
726 {
727         struct ufsmount *ump;
728         struct ext2_sb_info *fs;
729         int error, flags, ronly, i;
730
731         flags = 0;
732         if (mntflags & MNT_FORCE) {
733                 if (mp->mnt_flag & MNT_ROOTFS)
734                         return (EINVAL);
735                 flags |= FORCECLOSE;
736         }
737         if ((error = ext2_flushfiles(mp, flags, td)) != 0)
738                 return (error);
739         ump = VFSTOUFS(mp);
740         fs = ump->um_e2fs;
741         ronly = fs->s_rd_only;
742         if (ronly == 0) {
743                 if (fs->s_wasvalid)
744                         fs->s_es->s_state |= EXT2_VALID_FS;
745                 ext2_sbupdate(ump, MNT_WAIT);
746         }
747
748         /* release buffers containing group descriptors */
749         for(i = 0; i < fs->s_db_per_group; i++) 
750                 ULCK_BUF(fs->s_group_desc[i])
751         bsd_free(fs->s_group_desc, M_UFSMNT);
752
753         /* release cached inode/block bitmaps */
754         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
755                 if (fs->s_inode_bitmap[i])
756                         ULCK_BUF(fs->s_inode_bitmap[i])
757
758         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
759                 if (fs->s_block_bitmap[i])
760                         ULCK_BUF(fs->s_block_bitmap[i])
761
762         ump->um_devvp->v_rdev->si_mountpoint = NULL;
763         error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, td);
764         vrele(ump->um_devvp);
765         bsd_free(fs->s_es, M_UFSMNT);
766         bsd_free(fs, M_UFSMNT);
767         bsd_free(ump, M_UFSMNT);
768         mp->mnt_data = (qaddr_t)0;
769         mp->mnt_flag &= ~MNT_LOCAL;
770         return (error);
771 }
772
773 /*
774  * Flush out all the files in a filesystem.
775  */
776 static int
777 ext2_flushfiles(struct mount *mp, int flags, struct thread *td)
778 {
779         struct ufsmount *ump;
780         int error;
781 #if QUOTA
782         int i;
783 #endif
784
785         ump = VFSTOUFS(mp);
786 #if QUOTA
787         if (mp->mnt_flag & MNT_QUOTA) {
788                 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
789                         return (error);
790                 for (i = 0; i < MAXQUOTAS; i++) {
791                         if (ump->um_quotas[i] == NULLVP)
792                                 continue;
793                         quotaoff(td, mp, i);
794                 }
795                 /*
796                  * Here we fall through to vflush again to ensure
797                  * that we have gotten rid of all the system vnodes.
798                  */
799         }
800 #endif
801         error = vflush(mp, 0, flags);
802         return (error);
803 }
804
805 /*
806  * Get file system statistics.
807  * taken from ext2/super.c ext2_statfs
808  */
809 static int
810 ext2_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
811 {
812         unsigned long overhead;
813         struct ufsmount *ump;
814         struct ext2_sb_info *fs;
815         struct ext2_super_block *es;
816         int i, nsb;
817
818         ump = VFSTOUFS(mp);
819         fs = ump->um_e2fs;
820         es = fs->s_es;
821
822         if (es->s_magic != EXT2_SUPER_MAGIC)
823                 panic("ext2_statfs - magic number spoiled");
824
825         /*
826          * Compute the overhead (FS structures)
827          */
828         if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
829                 nsb = 0;
830                 for (i = 0 ; i < fs->s_groups_count; i++)
831                         if (ext2_group_sparse(i))
832                                 nsb++;
833         } else
834                 nsb = fs->s_groups_count;
835         overhead = es->s_first_data_block + 
836             /* Superblocks and block group descriptors: */
837             nsb * (1 + fs->s_db_per_group) +
838             /* Inode bitmap, block bitmap, and inode table: */
839             fs->s_groups_count * (1 + 1 + fs->s_itb_per_group);
840
841         sbp->f_bsize = EXT2_FRAG_SIZE(fs);      
842         sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
843         sbp->f_blocks = es->s_blocks_count - overhead;
844         sbp->f_bfree = es->s_free_blocks_count; 
845         sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count; 
846         sbp->f_files = es->s_inodes_count; 
847         sbp->f_ffree = es->s_free_inodes_count; 
848         if (sbp != &mp->mnt_stat) {
849                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
850                 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
851                         (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
852         }
853         return (0);
854 }
855
856 /*
857  * Go through the disk queues to initiate sandbagged IO;
858  * go through the inodes to write those that have been modified;
859  * initiate the writing of the super block if it has been modified.
860  *
861  * Note: we are always called with the filesystem marked `MPBUSY'.
862  */
863
864 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
865
866 static int
867 ext2_sync(struct mount *mp, int waitfor, struct thread *td)
868 {
869         struct ufsmount *ump = VFSTOUFS(mp);
870         struct ext2_sb_info *fs;
871         struct scaninfo scaninfo;
872         int error;
873
874         fs = ump->um_e2fs;
875         if (fs->s_dirt != 0 && fs->s_rd_only != 0) {            /* XXX */
876                 printf("fs = %s\n", fs->fs_fsmnt);
877                 panic("ext2_sync: rofs mod");
878         }
879
880         /*
881          * Write back each (modified) inode.
882          */
883         scaninfo.allerror = 0;
884         scaninfo.rescan = 1;
885         scaninfo.waitfor = waitfor;
886         scaninfo.td = td;
887         while (scaninfo.rescan) {
888                 scaninfo.rescan = 0;
889                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, 
890                                 NULL, ext2_sync_scan, &scaninfo);
891         }
892
893         /*
894          * Force stale file system control information to be flushed.
895          */
896         if (waitfor != MNT_LAZY) {
897                 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
898                 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, td)) != 0)
899                         scaninfo.allerror = error;
900                 VOP_UNLOCK(ump->um_devvp, 0, td);
901         }
902 #if QUOTA
903         qsync(mp);
904 #endif
905         /*
906          * Write back modified superblock.
907          */
908         if (fs->s_dirt != 0) {
909                 fs->s_dirt = 0;
910                 fs->s_es->s_wtime = time_second;
911                 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
912                         scaninfo.allerror = error;
913         }
914         return (scaninfo.allerror);
915 }
916
917 static int
918 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
919 {
920         struct scaninfo *info = data;
921         struct inode *ip;
922         int error;
923
924         ip = VTOI(vp);
925         if (vp->v_type == VNON ||
926             ((ip->i_flag &
927             (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
928             (RB_EMPTY(&vp->v_rbdirty_tree) || info->waitfor == MNT_LAZY))) {
929                 return(0);
930         }
931         if ((error = VOP_FSYNC(vp, info->waitfor, info->td)) != 0)
932                 info->allerror = error;
933         return(0);
934 }
935
936 /*
937  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
938  * in from disk.  If it is in core, wait for the lock bit to clear, then
939  * return the inode locked.  Detection and handling of mount points must be
940  * done by the calling routine.
941  */
942 static int
943 ext2_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
944 {
945         struct ext2_sb_info *fs;
946         struct inode *ip;
947         struct ufsmount *ump;
948         struct buf *bp;
949         struct vnode *vp;
950         dev_t dev;
951         int i, error;
952         int used_blocks;
953
954         ump = VFSTOUFS(mp);
955         dev = ump->um_dev;
956 restart:
957         if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
958                 return (0);
959
960         /*
961          * Lock out the creation of new entries in the FFS hash table in
962          * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
963          * may occur!
964          */
965         if (ext2fs_inode_hash_lock) {
966                 while (ext2fs_inode_hash_lock) {
967                         ext2fs_inode_hash_lock = -1;
968                         tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
969                 }
970                 goto restart;
971         }
972         ext2fs_inode_hash_lock = 1;
973
974         /*
975          * If this MALLOC() is performed after the getnewvnode()
976          * it might block, leaving a vnode with a NULL v_data to be
977          * found by ext2_sync() if a sync happens to fire right then,
978          * which will cause a panic because ext2_sync() blindly
979          * dereferences vp->v_data (as well it should).
980          */
981         MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2NODE, M_WAITOK);
982
983         /* Allocate a new vnode/inode. */
984         if ((error = getnewvnode(VT_UFS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
985                 if (ext2fs_inode_hash_lock < 0)
986                         wakeup(&ext2fs_inode_hash_lock);
987                 ext2fs_inode_hash_lock = 0;
988                 *vpp = NULL;
989                 FREE(ip, M_EXT2NODE);
990                 return (error);
991         }
992         bzero((caddr_t)ip, sizeof(struct inode));
993         vp->v_data = ip;
994         ip->i_vnode = vp;
995         ip->i_e2fs = fs = ump->um_e2fs;
996         ip->i_dev = dev;
997         ip->i_number = ino;
998 #if QUOTA
999         for (i = 0; i < MAXQUOTAS; i++)
1000                 ip->i_dquot[i] = NODQUOT;
1001 #endif
1002         /*
1003          * Put it onto its hash chain.  Since our vnode is locked, other
1004          * requests for this inode will block if they arrive while we are
1005          * sleeping waiting for old data structures to be purged or for the
1006          * contents of the disk portion of this inode to be read.
1007          */
1008         ufs_ihashins(ip);
1009
1010         if (ext2fs_inode_hash_lock < 0)
1011                 wakeup(&ext2fs_inode_hash_lock);
1012         ext2fs_inode_hash_lock = 0;
1013
1014         /* Read in the disk contents for the inode, copy into the inode. */
1015 #if 0
1016 printf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1017 #endif
1018         error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1019                       (int)fs->s_blocksize, &bp);
1020         if (error) {
1021                 /*
1022                  * The inode does not contain anything useful, so it would
1023                  * be misleading to leave it on its hash chain. With mode
1024                  * still zero, it will be unlinked and returned to the free
1025                  * list by vput().
1026                  */
1027                 vx_put(vp);
1028                 brelse(bp);
1029                 *vpp = NULL;
1030                 return (error);
1031         }
1032         /* convert ext2 inode to dinode */
1033         ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
1034                         ino_to_fsbo(fs, ino)), &ip->i_din);
1035         ip->i_block_group = ino_to_cg(fs, ino);
1036         ip->i_next_alloc_block = 0;
1037         ip->i_next_alloc_goal = 0;
1038         ip->i_prealloc_count = 0;
1039         ip->i_prealloc_block = 0;
1040         /* now we want to make sure that block pointers for unused
1041            blocks are zeroed out - ext2_balloc depends on this 
1042            although for regular files and directories only
1043         */
1044         if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1045                 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1046                 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1047                         ip->i_db[i] = 0;
1048         }
1049 /*
1050         ext2_print_inode(ip);
1051 */
1052         brelse(bp);
1053
1054         /*
1055          * Initialize the vnode from the inode, check for aliases.
1056          * Note that the underlying vnode may have changed.
1057          */
1058         if ((error = ufs_vinit(mp, &vp)) != 0) {
1059                 vx_put(vp);
1060                 *vpp = NULL;
1061                 return (error);
1062         }
1063         /*
1064          * Finish inode initialization now that aliasing has been resolved.
1065          */
1066         ip->i_devvp = ump->um_devvp;
1067         vref(ip->i_devvp);
1068         /*
1069          * Set up a generation number for this inode if it does not
1070          * already have one. This should only happen on old filesystems.
1071          */
1072         if (ip->i_gen == 0) {
1073                 ip->i_gen = random() / 2 + 1;
1074                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1075                         ip->i_flag |= IN_MODIFIED;
1076         }
1077         /*
1078          * Return the locked and refd vnode.
1079          */
1080         *vpp = vp;
1081         return (0);
1082 }
1083
1084 /*
1085  * File handle to vnode
1086  *
1087  * Have to be really careful about stale file handles:
1088  * - check that the inode number is valid
1089  * - call ext2_vget() to get the locked inode
1090  * - check for an unallocated inode (i_mode == 0)
1091  * - check that the given client host has export rights and return
1092  *   those rights via. exflagsp and credanonp
1093  */
1094 static int
1095 ext2_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1096 {
1097         struct ufid *ufhp;
1098         struct ext2_sb_info *fs;
1099
1100         ufhp = (struct ufid *)fhp;
1101         fs = VFSTOUFS(mp)->um_e2fs;
1102         if (ufhp->ufid_ino < ROOTINO ||
1103             ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1104                 return (ESTALE);
1105         return (ufs_fhtovp(mp, ufhp, vpp));
1106 }
1107
1108 /*
1109  * Vnode pointer to File handle
1110  */
1111 /* ARGSUSED */
1112 static int
1113 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1114 {
1115         struct inode *ip;
1116         struct ufid *ufhp;
1117
1118         ip = VTOI(vp);
1119         ufhp = (struct ufid *)fhp;
1120         ufhp->ufid_len = sizeof(struct ufid);
1121         ufhp->ufid_ino = ip->i_number;
1122         ufhp->ufid_gen = ip->i_gen;
1123         return (0);
1124 }
1125
1126 /*
1127  * Write a superblock and associated information back to disk.
1128  */
1129 static int
1130 ext2_sbupdate(struct ufsmount *mp, int waitfor)
1131 {
1132         struct ext2_sb_info *fs = mp->um_e2fs;
1133         struct ext2_super_block *es = fs->s_es;
1134         struct buf *bp;
1135         int error = 0;
1136 /*
1137 printf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1138 */
1139         bp = getblk(mp->um_devvp, lblktodoff(fs, SBLOCK), SBSIZE, 0, 0);
1140         bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1141         if (waitfor == MNT_WAIT)
1142                 error = bwrite(bp);
1143         else
1144                 bawrite(bp);
1145
1146         /*
1147          * The buffers for group descriptors, inode bitmaps and block bitmaps
1148          * are not busy at this point and are (hopefully) written by the
1149          * usual sync mechanism. No need to write them here
1150          */
1151
1152         return (error);
1153 }