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