Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / gnu / vfs / 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  */
42
43 #include "opt_quota.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/nlookup.h>
48 #include <sys/proc.h>
49 #include <sys/priv.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/diskslice.h>
57 #include <sys/malloc.h>
58 #include <sys/stat.h>
59 #include <vm/vm_zone.h>
60
61 #include <sys/buf2.h>
62 #include <sys/thread2.h>
63
64 #include "quota.h"
65 #include "dinode.h"
66 #include "inode.h"
67 #include "ext2_mount.h"
68 #include "ext2_extern.h"
69
70 #include "fs.h"
71 #include "ext2_fs.h"
72 #include "ext2_fs_sb.h"
73
74 extern struct vop_ops ext2_vnode_vops;
75 extern struct vop_ops ext2_spec_vops;
76 extern struct vop_ops ext2_fifo_vops;
77
78 static int ext2_fhtovp (struct mount *, struct vnode *,
79                                 struct fid *, struct vnode **);
80 static int ext2_flushfiles (struct mount *mp, int flags);
81 static int ext2_mount (struct mount *, char *, caddr_t, struct ucred *);
82 static int ext2_mountfs (struct vnode *, struct mount *, struct ucred *);
83 static int ext2_root(struct mount *, struct vnode **);
84 static int ext2_reload (struct mount *mountp, struct ucred *cred);
85 static int ext2_sbupdate (struct ext2_mount *, int);
86 static int ext2_sync (struct mount *, int);
87 static int ext2_unmount (struct mount *, int);
88 static int ext2_vget (struct mount *, struct vnode *, ino_t, struct vnode **);
89 static int ext2_init(struct vfsconf *);
90 static int ext2_vptofh (struct vnode *, struct fid *);
91
92 static MALLOC_DEFINE(M_EXT2NODE, "EXT2 node", "EXT2 vnode private part");
93 MALLOC_DEFINE(M_EXT2MNT, "EXT2 mount", "EXT2 mount structure");
94
95 static struct vfsops ext2fs_vfsops = {
96         .vfs_mount =            ext2_mount,
97         .vfs_unmount =          ext2_unmount,
98         .vfs_root =             ext2_root,      /* root inode via vget */
99         .vfs_quotactl =         ext2_quotactl,  /* quota operations */
100         .vfs_statfs =           ext2_statfs,
101         .vfs_sync =             ext2_sync,
102         .vfs_vget =             ext2_vget,
103         .vfs_fhtovp =           ext2_fhtovp,
104         .vfs_checkexp =         ext2_check_export,
105         .vfs_vptofh =           ext2_vptofh,
106         .vfs_init =             ext2_init,
107         .vfs_uninit =           ext2_uninit
108 };
109
110 VFS_SET(ext2fs_vfsops, ext2fs, 0);
111 MODULE_VERSION(ext2fs, 1);
112
113 static int ext2fs_inode_hash_lock;
114
115 static int      ext2_check_sb_compat (struct ext2_super_block *es,
116                                           cdev_t dev, int ronly);
117 static int      compute_sb_data (struct vnode *devvp,
118                                      struct ext2_super_block *es,
119                                      struct ext2_sb_info *fs);
120
121 static int
122 ext2_root(struct mount *mp, struct vnode **vpp)
123 {
124         struct vnode *nvp;
125         int error;
126
127         error = VFS_VGET(mp, NULL, (ino_t)EXT2_ROOTINO, &nvp);
128         if (error)
129                 return (error);
130         *vpp = nvp;
131         return (0);
132 }
133
134 /*
135  * Do operations associated with quotas
136  */
137 int
138 ext2_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
139              struct ucred *cred)
140 {
141 #ifndef QUOTA
142         return (EOPNOTSUPP);
143 #else
144         int cmd, type, error;
145
146         type = cmds & SUBCMDMASK;
147         cmd = cmds >> SUBCMDSHIFT;
148
149         if (uid == -1) {
150                 switch(type) {
151                         case USRQUOTA:
152                                 uid = cred->cr_ruid;
153                                 break;
154                         case GRPQUOTA:
155                                 uid = cred->cr_rgid;
156                                 break;
157                         default:
158                                 return (EINVAL);
159                 }
160         }
161
162         /*
163          * Check permissions.
164          */
165         switch (cmd) {
166
167         case Q_QUOTAON:
168                 error = priv_check_cred(cred, PRIV_UFS_QUOTAON, 0);
169                 break;
170
171         case Q_QUOTAOFF:
172                 error = priv_check_cred(cred, PRIV_UFS_QUOTAOFF, 0);
173                 break;
174
175         case Q_SETQUOTA:
176                 error = priv_check_cred(cred, PRIV_VFS_SETQUOTA, 0);
177                 break;
178
179         case Q_SETUSE:
180                 error = priv_check_cred(cred, PRIV_UFS_SETUSE, 0);
181                 break;
182
183         case Q_GETQUOTA:
184                 if (uid == cred->cr_ruid)
185                         error = 0;
186                 else
187                         error = priv_check_cred(cred, PRIV_VFS_GETQUOTA, 0);
188                 break;
189
190         case Q_SYNC:
191                 error = 0;
192                 break;
193
194         default:
195                 error = EINVAL;
196                 break;
197         }
198
199         if (error)
200                 return (error);
201
202
203         if ((uint)type >= MAXQUOTAS)
204                 return (EINVAL);
205         if (vfs_busy(mp, LK_NOWAIT))
206                 return (0);
207
208         switch (cmd) {
209
210         case Q_QUOTAON:
211                 error = ext2_quotaon(cred, mp, type, arg);
212                 break;
213
214         case Q_QUOTAOFF:
215                 error = ext2_quotaoff(mp, type);
216                 break;
217
218         case Q_SETQUOTA:
219                 error = ext2_setquota(mp, uid, type, arg);
220                 break;
221
222         case Q_SETUSE:
223                 error = ext2_setuse(mp, uid, type, arg);
224                 break;
225
226         case Q_GETQUOTA:
227                 error = ext2_getquota(mp, uid, type, arg);
228                 break;
229
230         case Q_SYNC:
231                 error = ext2_qsync(mp);
232                 break;
233
234         default:
235                 error = EINVAL;
236                 break;
237         }
238         vfs_unbusy(mp);
239         return (error);
240 #endif
241 }
242
243 /*
244  * Initial UFS filesystems, done only once.
245  */
246 int
247 ext2_init(struct vfsconf *vfsp)
248 {
249         static int done;
250
251         if (done)
252                 return (0);
253         done = 1;
254         ext2_ihashinit();
255 #ifdef QUOTA
256         ext2_dqinit();
257 #endif
258         return (0);
259 }
260
261 /*
262  * VFS Operations.
263  *
264  * mount system call
265  *
266  * Parameters:
267  *      data:   this is actually a (struct ext2_args *)
268  */
269 static int
270 ext2_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
271 {
272         struct vnode *devvp;
273         struct ext2_args args;
274         struct ext2_mount *ump = NULL;
275         struct ext2_sb_info *fs;
276         size_t size;
277         int error, flags;
278         mode_t accessmode;
279         struct nlookupdata nd;
280
281         if ((error = copyin(data, (caddr_t)&args, sizeof (struct ext2_args))) != 0)
282                 return (error);
283
284         /*
285          * If updating, check whether changing from read-only to
286          * read/write; if there is no device name, that's all we do.
287          */
288         if (mp->mnt_flag & MNT_UPDATE) {
289                 ump = VFSTOEXT2(mp);
290                 fs = ump->um_e2fs;
291                 devvp = ump->um_devvp;
292                 error = 0;
293                 if (fs->s_rd_only == 0 && (mp->mnt_flag & MNT_RDONLY)) {
294                         flags = WRITECLOSE;
295                         if (mp->mnt_flag & MNT_FORCE)
296                                 flags |= FORCECLOSE;
297                         if (vfs_busy(mp, LK_NOWAIT))
298                                 return (EBUSY);
299                         error = ext2_flushfiles(mp, flags);
300                         vfs_unbusy(mp);
301                         if (!error && fs->s_wasvalid) {
302                                 fs->s_es->s_state |= EXT2_VALID_FS;
303                                 ext2_sbupdate(ump, MNT_WAIT);
304                         }
305                         fs->s_rd_only = 1;
306                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
307                         VOP_OPEN(devvp, FREAD, FSCRED, NULL);
308                         VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
309                         vn_unlock(devvp);
310                 }
311                 if (!error && (mp->mnt_flag & MNT_RELOAD))
312                         error = ext2_reload(mp, cred);
313                 if (error)
314                         return (error);
315                 if (ext2_check_sb_compat(fs->s_es, devvp->v_rdev,
316                     (mp->mnt_kern_flag & MNTK_WANTRDWR) == 0) != 0)
317                         return (EPERM);
318                 if (fs->s_rd_only && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
319                         /*
320                          * If upgrade to read-write by non-root, then verify
321                          * that user has necessary permissions on the device.
322                          */
323                         if (cred->cr_uid != 0) {
324                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
325                                 error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
326                                 if (error) {
327                                         vn_unlock(devvp);
328                                         return (error);
329                                 }
330                                 vn_unlock(devvp);
331                         }
332
333                         if ((fs->s_es->s_state & EXT2_VALID_FS) == 0 ||
334                             (fs->s_es->s_state & EXT2_ERROR_FS)) {
335                                 if (mp->mnt_flag & MNT_FORCE) {
336                                         kprintf(
337 "WARNING: %s was not properly dismounted\n",
338                                             fs->fs_fsmnt);
339                                 } else {
340                                         kprintf(
341 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
342                                             fs->fs_fsmnt);
343                                         return (EPERM);
344                                 }
345                         }
346                         fs->s_es->s_state &= ~EXT2_VALID_FS;
347                         ext2_sbupdate(ump, MNT_WAIT);
348                         fs->s_rd_only = 0;
349                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
350                         VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
351                         VOP_CLOSE(devvp, FREAD, NULL);
352                         vn_unlock(devvp);
353                 }
354                 if (args.fspec == NULL) {
355                         /*
356                          * Process export requests.
357                          */
358                         return (vfs_export(mp, &ump->um_export, &args.export));
359                 }
360         }
361         /*
362          * Not an update, or updating the name: look up the name
363          * and verify that it refers to a sensible block device.
364          */
365         devvp = NULL;
366         error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
367         if (error == 0)
368                 error = nlookup(&nd);
369         if (error == 0)
370                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
371         nlookup_done(&nd);
372         if (error)
373                 return (error);
374
375         if (!vn_isdisk(devvp, &error)) {
376                 vrele(devvp);
377                 return (error);
378         }
379
380         /*
381          * If mount by non-root, then verify that user has necessary
382          * permissions on the device.
383          */
384         if (cred->cr_uid != 0) {
385                 accessmode = VREAD;
386                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
387                         accessmode |= VWRITE;
388                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
389                 if ((error = VOP_EACCESS(devvp, accessmode, cred)) != 0) {
390                         vput(devvp);
391                         return (error);
392                 }
393                 vn_unlock(devvp);
394         }
395
396         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
397                 error = ext2_mountfs(devvp, mp, cred);
398         } else {
399                 if (devvp != ump->um_devvp)
400                         error = EINVAL; /* needs translation */
401                 else
402                         vrele(devvp);
403         }
404         if (error) {
405                 vrele(devvp);
406                 return (error);
407         }
408         ump = VFSTOEXT2(mp);
409         fs = ump->um_e2fs;
410         copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
411         bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
412         copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
413         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
414         ext2_statfs(mp, &mp->mnt_stat, cred);
415         return (0);
416 }
417
418 static int
419 ext2_check_sb_compat(struct ext2_super_block *es, cdev_t dev, int ronly)
420 {
421         if (es->s_magic != EXT2_SUPER_MAGIC) {
422                 kprintf("ext2fs: %s: wrong magic number %#x (expected %#x)\n",
423                     devtoname(dev), es->s_magic, EXT2_SUPER_MAGIC);
424                 return (1);
425         }
426         if (es->s_rev_level > EXT2_GOOD_OLD_REV) {
427                 if (es->s_feature_incompat & ~EXT2_FEATURE_INCOMPAT_SUPP) {
428                         kprintf(
429 "WARNING: mount of %s denied due to unsupported optional features\n",
430                             devtoname(dev));
431                         return (1);
432                 }
433                 if (!ronly &&
434                     (es->s_feature_ro_compat & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
435                         kprintf(
436 "WARNING: R/W mount of %s denied due to unsupported optional features\n",
437                             devtoname(dev));
438                         return (1);
439                 }
440         }
441         return (0);
442 }
443
444 /*
445  * this computes the fields of the  ext2_sb_info structure from the
446  * data in the ext2_super_block structure read in
447  */
448 static int
449 compute_sb_data(struct vnode *devvp, struct ext2_super_block *es,
450                 struct ext2_sb_info *fs)
451 {
452         int db_count, error;
453         int i, j;
454         int logic_sb_block = 1; /* XXX for now */
455
456 #if 1
457 #define V(v)
458 #else
459 #define V(v)  kprintf(#v"= %d\n", fs->v);
460 #endif
461
462         fs->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
463         V(s_blocksize)
464         fs->s_bshift = EXT2_MIN_BLOCK_LOG_SIZE + es->s_log_block_size;
465         V(s_bshift)
466         fs->s_fsbtodb = es->s_log_block_size + 1;
467         V(s_fsbtodb)
468         fs->s_qbmask = fs->s_blocksize - 1;
469         V(s_bmask)
470         fs->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(es);
471         V(s_blocksize_bits)
472         fs->s_frag_size = EXT2_MIN_FRAG_SIZE << es->s_log_frag_size;
473         V(s_frag_size)
474         if (fs->s_frag_size)
475                 fs->s_frags_per_block = fs->s_blocksize / fs->s_frag_size;
476         V(s_frags_per_block)
477         fs->s_blocks_per_group = es->s_blocks_per_group;
478         V(s_blocks_per_group)
479         fs->s_frags_per_group = es->s_frags_per_group;
480         V(s_frags_per_group)
481         fs->s_inodes_per_group = es->s_inodes_per_group;
482         V(s_inodes_per_group)
483         if (es->s_rev_level == EXT2_GOOD_OLD_REV) {
484                 fs->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
485                 fs->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
486         } else {
487                 fs->s_first_ino = es->s_first_ino;
488                 fs->s_inode_size = es->s_inode_size;
489                 /*
490                  * Simple sanity check for superblock inode size value.
491                  */
492                 if (fs->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
493                     fs->s_inode_size > fs->s_blocksize ||
494                     (fs->s_inode_size & (fs->s_inode_size - 1)) != 0) {
495                         kprintf("EXT2-fs: invalid inode size %d\n",
496                         fs->s_inode_size);
497                         return (EIO);
498                 }
499         }
500         V(s_first_ino)
501         V(s_inode_size)
502         fs->s_inodes_per_block = fs->s_blocksize / EXT2_INODE_SIZE(fs);
503         V(s_inodes_per_block)
504         fs->s_itb_per_group = fs->s_inodes_per_group /fs->s_inodes_per_block;
505         V(s_itb_per_group)
506         fs->s_desc_per_block = fs->s_blocksize / sizeof (struct ext2_group_desc);
507         V(s_desc_per_block)
508         /* s_resuid / s_resgid ? */
509         fs->s_groups_count = (es->s_blocks_count - es->s_first_data_block +
510                               EXT2_BLOCKS_PER_GROUP(fs) - 1) /
511                               EXT2_BLOCKS_PER_GROUP(fs);
512         V(s_groups_count)
513         db_count = (fs->s_groups_count + EXT2_DESC_PER_BLOCK(fs) - 1) /
514                                          EXT2_DESC_PER_BLOCK(fs);
515         fs->s_db_per_group = db_count;
516         V(s_db_per_group)
517
518         fs->s_group_desc = kmalloc(db_count * sizeof (struct buf *),
519                                 M_EXT2MNT, M_WAITOK);
520
521         /* adjust logic_sb_block */
522         if (fs->s_blocksize > SBSIZE)
523                 /*
524                  * Godmar thinks: if the blocksize is greater than 1024,
525                  * then, the superblock is logically part of block zero.
526                  */
527                 logic_sb_block = 0;
528
529         for (i = 0; i < db_count; i++) {
530                 error = bread(devvp, fsbtodoff(fs, logic_sb_block + i + 1),
531                                 fs->s_blocksize, &fs->s_group_desc[i]);
532                 if (error) {
533                         for (j = 0; j < i; j++)
534                                 brelse(fs->s_group_desc[j]);
535                         kfree(fs->s_group_desc, M_EXT2MNT);
536                         kprintf("EXT2-fs: unable to read group descriptors (%d)\n", error);
537                         return EIO;
538                 }
539                 /* Set the B_LOCKED flag on the buffer, then brelse() it */
540                 LCK_BUF(fs->s_group_desc[i])
541         }
542         if (!ext2_check_descriptors(fs)) {
543                 for (j = 0; j < db_count; j++)
544                         ULCK_BUF(fs->s_group_desc[j])
545                 kfree(fs->s_group_desc, M_EXT2MNT);
546                 kprintf("EXT2-fs: (ext2_check_descriptors failure) "
547                         "unable to read group descriptors\n");
548                 return EIO;
549         }
550
551         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
552                 fs->s_inode_bitmap_number[i] = 0;
553                 fs->s_inode_bitmap[i] = NULL;
554                 fs->s_block_bitmap_number[i] = 0;
555                 fs->s_block_bitmap[i] = NULL;
556         }
557         fs->s_loaded_inode_bitmaps = 0;
558         fs->s_loaded_block_bitmaps = 0;
559         return 0;
560 }
561
562 /*
563  * Reload all incore data for a filesystem (used after running fsck on
564  * the root filesystem and finding things to fix). The filesystem must
565  * be mounted read-only.
566  *
567  * Things to do to update the mount:
568  *      1) invalidate all cached meta-data.
569  *      2) re-read superblock from disk.
570  *      3) re-read summary information from disk.
571  *      4) invalidate all inactive vnodes.
572  *      5) invalidate all cached file data.
573  *      6) re-read inode data for all active vnodes.
574  */
575 static int ext2_reload_scan(struct mount *mp, struct vnode *vp, void *rescan);
576
577 struct scaninfo {
578         int rescan;
579         int allerror;
580         int waitfor;
581         struct vnode *devvp;
582         struct ext2_sb_info *fs;
583 };
584
585 static int
586 ext2_reload(struct mount *mountp, struct ucred *cred)
587 {
588         struct vnode *devvp;
589         struct buf *bp;
590         struct ext2_super_block *es;
591         struct ext2_sb_info *fs;
592         int error;
593         struct scaninfo scaninfo;
594
595         if ((mountp->mnt_flag & MNT_RDONLY) == 0)
596                 return (EINVAL);
597         /*
598          * Step 1: invalidate all cached meta-data.
599          */
600         devvp = VFSTOEXT2(mountp)->um_devvp;
601         if (vinvalbuf(devvp, 0, 0, 0))
602                 panic("ext2_reload: dirty1");
603         /*
604          * Step 2: re-read superblock from disk.
605          * constants have been adjusted for ext2
606          */
607         if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
608                 return (error);
609         es = (struct ext2_super_block *)bp->b_data;
610         if (ext2_check_sb_compat(es, devvp->v_rdev, 0) != 0) {
611                 brelse(bp);
612                 return (EIO);           /* XXX needs translation */
613         }
614         fs = VFSTOEXT2(mountp)->um_e2fs;
615         bcopy(bp->b_data, fs->s_es, sizeof(struct ext2_super_block));
616
617         if((error = compute_sb_data(devvp, es, fs)) != 0) {
618                 brelse(bp);
619                 return error;
620         }
621 #ifdef UNKLAR
622         if (fs->fs_sbsize < SBSIZE)
623                 bp->b_flags |= B_INVAL;
624 #endif
625         brelse(bp);
626
627         scaninfo.rescan = 1;
628         scaninfo.devvp = devvp;
629         scaninfo.fs = fs;
630         while (error == 0 && scaninfo.rescan) {
631             scaninfo.rescan = 0;
632             error = vmntvnodescan(mountp, VMSC_GETVX,
633                                   NULL, ext2_reload_scan, &scaninfo);
634         }
635         return(error);
636 }
637
638 static int
639 ext2_reload_scan(struct mount *mp, struct vnode *vp, void *data)
640 {
641         struct scaninfo *info = data;
642         struct inode *ip;
643         struct buf *bp;
644         int error;
645
646         /*
647          * Try to recycle
648          */
649         if (vrecycle(vp))
650                 return(0);
651
652         /*
653          * Step 1: invalidate all cached file data.
654          */
655         if (vinvalbuf(vp, 0, 0, 0))
656                 panic("ext2_reload: dirty2");
657         /*
658          * Step 2: re-read inode data for all active vnodes.
659          */
660         ip = VTOI(vp);
661         error = bread(info->devvp,
662                       fsbtodoff(info->fs, ino_to_fsba(info->fs, ip->i_number)),
663                       (int)info->fs->s_blocksize, &bp);
664         if (error)
665                 return (error);
666         ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data +
667             EXT2_INODE_SIZE(info->fs) * ino_to_fsbo(info->fs, ip->i_number)),
668             &ip->i_din);
669         brelse(bp);
670         return(0);
671 }
672
673 /*
674  * Common code for mount and mountroot
675  */
676 static int
677 ext2_mountfs(struct vnode *devvp, struct mount *mp, struct ucred *cred)
678 {
679         struct ext2_mount *ump;
680         struct buf *bp;
681         struct ext2_sb_info *fs;
682         struct ext2_super_block *es;
683         cdev_t dev;
684         int error, i;
685         int ronly;
686
687         /*
688          * Disallow multiple mounts of the same device.
689          * Disallow mounting of a device that is currently in use
690          * (except for root, which might share swap device for miniroot).
691          * Flush out any old buffers remaining from a previous use.
692          */
693         if ((error = vfs_mountedon(devvp)) != 0)
694                 return (error);
695         if (vcount(devvp) > 0)
696                 return (EBUSY);
697         if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
698                 return (error);
699 #ifdef READONLY
700 /* turn on this to force it to be read-only */
701         mp->mnt_flag |= MNT_RDONLY;
702 #endif
703
704         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
705         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
706         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
707         vn_unlock(devvp);
708         if (error)
709                 return (error);
710         dev = devvp->v_rdev;
711         if (dev->si_iosize_max != 0)
712                 mp->mnt_iosize_max = dev->si_iosize_max;
713         if (mp->mnt_iosize_max > MAXPHYS)
714                 mp->mnt_iosize_max = MAXPHYS;
715
716         bp = NULL;
717         ump = NULL;
718         if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
719                 goto out;
720         es = (struct ext2_super_block *)bp->b_data;
721         if (ext2_check_sb_compat(es, dev, ronly) != 0) {
722                 error = EINVAL;         /* XXX needs translation */
723                 goto out;
724         }
725         if ((es->s_state & EXT2_VALID_FS) == 0 ||
726             (es->s_state & EXT2_ERROR_FS)) {
727                 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
728                         kprintf(
729 "WARNING: Filesystem was not properly dismounted\n");
730                 } else {
731                         kprintf(
732 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
733                         error = EPERM;
734                         goto out;
735                 }
736         }
737         ump = kmalloc(sizeof *ump, M_EXT2MNT, M_WAITOK | M_ZERO);
738         ump->um_malloctype = M_EXT2NODE;
739         ump->um_blkatoff = ext2_blkatoff;
740         ump->um_truncate = ext2_truncate;
741         ump->um_update = ext2_update;
742         ump->um_valloc = ext2_valloc;
743         ump->um_vfree = ext2_vfree;
744         /* I don't know whether this is the right strategy. Note that
745            we dynamically allocate both a ext2_sb_info and a ext2_super_block
746            while Linux keeps the super block in a locked buffer
747          */
748         ump->um_e2fs = kmalloc(sizeof(struct ext2_sb_info),
749                 M_EXT2MNT, M_WAITOK);
750         ump->um_e2fs->s_es = kmalloc(sizeof(struct ext2_super_block),
751                 M_EXT2MNT, M_WAITOK);
752         bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
753         if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
754                 goto out;
755         /*
756          * We don't free the group descriptors allocated by compute_sb_data()
757          * until ext2_unmount().  This is OK since the mount will succeed.
758          */
759         brelse(bp);
760         bp = NULL;
761         fs = ump->um_e2fs;
762         fs->s_rd_only = ronly;  /* ronly is set according to mnt_flags */
763         /* if the fs is not mounted read-only, make sure the super block is
764            always written back on a sync()
765          */
766         fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
767         if (ronly == 0) {
768                 fs->s_dirt = 1;         /* mark it modified */
769                 fs->s_es->s_state &= ~EXT2_VALID_FS;    /* set fs invalid */
770         }
771         mp->mnt_data = (qaddr_t)ump;
772         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
773         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
774         mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
775         mp->mnt_flag |= MNT_LOCAL;
776         ump->um_mountp = mp;
777         ump->um_dev = dev;
778         ump->um_devvp = devvp;
779         /* setting those two parameters allows us to use
780            ext2_bmap w/o changse !
781         */
782         ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
783         ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
784         ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
785         for (i = 0; i < MAXQUOTAS; i++)
786                 ump->um_quotas[i] = NULLVP;
787         dev->si_mountpoint = mp;
788
789         vfs_add_vnodeops(mp, &ext2_vnode_vops, &mp->mnt_vn_norm_ops);
790         vfs_add_vnodeops(mp, &ext2_spec_vops, &mp->mnt_vn_spec_ops);
791         vfs_add_vnodeops(mp, &ext2_fifo_vops, &mp->mnt_vn_fifo_ops);
792
793         if (ronly == 0)
794                 ext2_sbupdate(ump, MNT_WAIT);
795         return (0);
796 out:
797         if (bp)
798                 brelse(bp);
799         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
800         VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
801         vn_unlock(devvp);
802         if (ump) {
803                 kfree(ump->um_e2fs->s_es, M_EXT2MNT);
804                 kfree(ump->um_e2fs, M_EXT2MNT);
805                 kfree(ump, M_EXT2MNT);
806                 mp->mnt_data = (qaddr_t)0;
807         }
808         return (error);
809 }
810
811 /*
812  * unmount system call
813  */
814 static int
815 ext2_unmount(struct mount *mp, int mntflags)
816 {
817         struct ext2_mount *ump;
818         struct ext2_sb_info *fs;
819         int error, flags, ronly, i;
820
821         flags = 0;
822         if (mntflags & MNT_FORCE) {
823                 if (mp->mnt_flag & MNT_ROOTFS)
824                         return (EINVAL);
825                 flags |= FORCECLOSE;
826         }
827         if ((error = ext2_flushfiles(mp, flags)) != 0)
828                 return (error);
829         ump = VFSTOEXT2(mp);
830         fs = ump->um_e2fs;
831         ronly = fs->s_rd_only;
832         if (ronly == 0) {
833                 if (fs->s_wasvalid)
834                         fs->s_es->s_state |= EXT2_VALID_FS;
835                 ext2_sbupdate(ump, MNT_WAIT);
836         }
837
838         /* release buffers containing group descriptors */
839         for(i = 0; i < fs->s_db_per_group; i++)
840                 ULCK_BUF(fs->s_group_desc[i])
841         kfree(fs->s_group_desc, M_EXT2MNT);
842
843         /* release cached inode/block bitmaps */
844         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
845                 if (fs->s_inode_bitmap[i])
846                         ULCK_BUF(fs->s_inode_bitmap[i])
847
848         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
849                 if (fs->s_block_bitmap[i])
850                         ULCK_BUF(fs->s_block_bitmap[i])
851
852         ump->um_devvp->v_rdev->si_mountpoint = NULL;
853
854         vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
855         error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
856         vn_unlock(ump->um_devvp);
857
858         vrele(ump->um_devvp);
859         kfree(fs->s_es, M_EXT2MNT);
860         kfree(fs, M_EXT2MNT);
861         kfree(ump, M_EXT2MNT);
862         mp->mnt_data = (qaddr_t)0;
863         mp->mnt_flag &= ~MNT_LOCAL;
864         return (error);
865 }
866
867 /*
868  * Flush out all the files in a filesystem.
869  */
870 static int
871 ext2_flushfiles(struct mount *mp, int flags)
872 {
873         struct ext2_mount *ump;
874         int error;
875 #if QUOTA
876         int i;
877 #endif
878
879         ump = VFSTOEXT2(mp);
880 #if QUOTA
881         if (mp->mnt_flag & MNT_QUOTA) {
882                 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
883                         return (error);
884                 for (i = 0; i < MAXQUOTAS; i++) {
885                         if (ump->um_quotas[i] == NULLVP)
886                                 continue;
887                         ext2_quotaoff(mp, i);
888                 }
889                 /*
890                  * Here we fall through to vflush again to ensure
891                  * that we have gotten rid of all the system vnodes.
892                  */
893         }
894 #endif
895         error = vflush(mp, 0, flags);
896         return (error);
897 }
898
899 /*
900  * Go through the disk queues to initiate sandbagged IO;
901  * go through the inodes to write those that have been modified;
902  * initiate the writing of the super block if it has been modified.
903  *
904  * Note: we are always called with the filesystem marked `MPBUSY'.
905  */
906
907 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
908
909 static int
910 ext2_sync(struct mount *mp, int waitfor)
911 {
912         struct ext2_mount *ump = VFSTOEXT2(mp);
913         struct ext2_sb_info *fs;
914         struct scaninfo scaninfo;
915         int error;
916
917         fs = ump->um_e2fs;
918         if (fs->s_dirt != 0 && fs->s_rd_only != 0) {            /* XXX */
919                 kprintf("fs = %s\n", fs->fs_fsmnt);
920                 panic("ext2_sync: rofs mod");
921         }
922
923         /*
924          * Write back each (modified) inode.
925          */
926         scaninfo.allerror = 0;
927         scaninfo.rescan = 1;
928         scaninfo.waitfor = waitfor;
929         while (scaninfo.rescan) {
930                 scaninfo.rescan = 0;
931                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
932                               NULL, ext2_sync_scan, &scaninfo);
933         }
934
935         /*
936          * Force stale file system control information to be flushed.
937          */
938         if ((waitfor & MNT_LAZY) == 0) {
939                 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
940                 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
941                         scaninfo.allerror = error;
942                 vn_unlock(ump->um_devvp);
943         }
944 #if QUOTA
945         ext2_qsync(mp);
946 #endif
947         /*
948          * Write back modified superblock.
949          */
950         if (fs->s_dirt != 0) {
951                 fs->s_dirt = 0;
952                 fs->s_es->s_wtime = time_second;
953                 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
954                         scaninfo.allerror = error;
955         }
956         return (scaninfo.allerror);
957 }
958
959 static int
960 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
961 {
962         struct scaninfo *info = data;
963         struct inode *ip;
964         int error;
965
966         ip = VTOI(vp);
967         if (vp->v_type == VNON ||
968             ((ip->i_flag &
969             (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
970             (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
971                 return(0);
972         }
973         if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
974                 info->allerror = error;
975         return(0);
976 }
977
978 /*
979  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
980  * in from disk.  If it is in core, wait for the lock bit to clear, then
981  * return the inode locked.  Detection and handling of mount points must be
982  * done by the calling routine.
983  */
984 static int
985 ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
986 {
987         struct ext2_sb_info *fs;
988         struct inode *ip;
989         struct ext2_mount *ump;
990         struct buf *bp;
991         struct vnode *vp;
992         cdev_t dev;
993         int i, error;
994         int used_blocks;
995
996         ump = VFSTOEXT2(mp);
997         dev = ump->um_dev;
998 restart:
999         if ((*vpp = ext2_ihashget(dev, ino)) != NULL)
1000                 return (0);
1001
1002         /*
1003          * Lock out the creation of new entries in the FFS hash table in
1004          * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1005          * may occur!
1006          */
1007         if (ext2fs_inode_hash_lock) {
1008                 while (ext2fs_inode_hash_lock) {
1009                         ext2fs_inode_hash_lock = -1;
1010                         tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1011                 }
1012                 goto restart;
1013         }
1014         ext2fs_inode_hash_lock = 1;
1015
1016         /*
1017          * If this MALLOC() is performed after the getnewvnode()
1018          * it might block, leaving a vnode with a NULL v_data to be
1019          * found by ext2_sync() if a sync happens to fire right then,
1020          * which will cause a panic because ext2_sync() blindly
1021          * dereferences vp->v_data (as well it should).
1022          */
1023         ip = kmalloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1024
1025         /* Allocate a new vnode/inode. */
1026         if ((error = getnewvnode(VT_EXT2FS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
1027                 if (ext2fs_inode_hash_lock < 0)
1028                         wakeup(&ext2fs_inode_hash_lock);
1029                 ext2fs_inode_hash_lock = 0;
1030                 *vpp = NULL;
1031                 kfree(ip, M_EXT2NODE);
1032                 return (error);
1033         }
1034         bzero((caddr_t)ip, sizeof(struct inode));
1035         vp->v_data = ip;
1036         ip->i_vnode = vp;
1037         ip->i_e2fs = fs = ump->um_e2fs;
1038         ip->i_dev = dev;
1039         ip->i_number = ino;
1040 #if QUOTA
1041         for (i = 0; i < MAXQUOTAS; i++)
1042                 ip->i_dquot[i] = NODQUOT;
1043 #endif
1044         /*
1045          * Put it onto its hash chain.  Since our vnode is locked, other
1046          * requests for this inode will block if they arrive while we are
1047          * sleeping waiting for old data structures to be purged or for the
1048          * contents of the disk portion of this inode to be read.
1049          */
1050         ext2_ihashins(ip);
1051
1052         if (ext2fs_inode_hash_lock < 0)
1053                 wakeup(&ext2fs_inode_hash_lock);
1054         ext2fs_inode_hash_lock = 0;
1055
1056         /* Read in the disk contents for the inode, copy into the inode. */
1057 #if 0
1058 kprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1059 #endif
1060         error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1061                       (int)fs->s_blocksize, &bp);
1062         if (error) {
1063                 /*
1064                  * The inode does not contain anything useful, so it would
1065                  * be misleading to leave it on its hash chain. With mode
1066                  * still zero, it will be unlinked and returned to the free
1067                  * list by vput().
1068                  */
1069                 brelse(bp);
1070                 vx_put(vp);
1071                 *vpp = NULL;
1072                 return (error);
1073         }
1074         /* convert ext2 inode to dinode */
1075         ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
1076                         ino_to_fsbo(fs, ino)), &ip->i_din);
1077         ip->i_block_group = ino_to_cg(fs, ino);
1078         ip->i_next_alloc_block = 0;
1079         ip->i_next_alloc_goal = 0;
1080         ip->i_prealloc_count = 0;
1081         ip->i_prealloc_block = 0;
1082         /* now we want to make sure that block pointers for unused
1083            blocks are zeroed out - ext2_balloc depends on this
1084            although for regular files and directories only
1085         */
1086         if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1087                 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1088                 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1089                         ip->i_db[i] = 0;
1090         }
1091 #if 0
1092         ext2_print_inode(ip);
1093 #endif
1094         bqrelse(bp);
1095
1096         /*
1097          * Initialize the vnode from the inode, check for aliases.
1098          * Note that the underlying vnode may have changed.
1099          */
1100         if ((error = ext2_vinit(mp, &vp)) != 0) {
1101                 vx_put(vp);
1102                 *vpp = NULL;
1103                 return (error);
1104         }
1105
1106         /*
1107          * Finish inode initialization now that aliasing has been resolved.
1108          */
1109         ip->i_devvp = ump->um_devvp;
1110         vref(ip->i_devvp);
1111         /*
1112          * Set up a generation number for this inode if it does not
1113          * already have one. This should only happen on old filesystems.
1114          */
1115         if (ip->i_gen == 0) {
1116                 ip->i_gen = krandom() / 2 + 1;
1117                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1118                         ip->i_flag |= IN_MODIFIED;
1119         }
1120         /*
1121          * Return the locked and refd vnode.
1122          */
1123         *vpp = vp;
1124         return (0);
1125 }
1126
1127 /*
1128  * File handle to vnode
1129  *
1130  * Have to be really careful about stale file handles:
1131  * - check that the inode number is valid
1132  * - call ext2_vget() to get the locked inode
1133  * - check for an unallocated inode (i_mode == 0)
1134  * - check that the given client host has export rights and return
1135  *   those rights via. exflagsp and credanonp
1136  */
1137 static int
1138 ext2_fhtovp(struct mount *mp, struct vnode *rootvp,
1139             struct fid *fhp, struct vnode **vpp)
1140 {
1141         struct ufid *ufhp;
1142         struct ext2_sb_info *fs;
1143         struct inode *ip;
1144         struct vnode *nvp;
1145         int error;
1146
1147         ufhp = (struct ufid *)fhp;
1148         fs = VFSTOEXT2(mp)->um_e2fs;
1149         if (ufhp->ufid_ino < EXT2_ROOTINO ||
1150             ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1151                 return (ESTALE);
1152
1153         error = VFS_VGET(mp, rootvp, ufhp->ufid_ino, &nvp);
1154         if (error) {
1155                 *vpp = NULLVP;
1156                 return (error);
1157         }
1158         ip = VTOI(nvp);
1159         if (ip->i_mode == 0 ||
1160             ip->i_gen != ufhp->ufid_gen ||
1161             (VFSTOEXT2(mp)->um_i_effnlink_valid ? ip->i_effnlink :
1162             ip->i_nlink) <= 0) {
1163                 vput(nvp);
1164                 *vpp = NULLVP;
1165                 return (ESTALE);
1166         }
1167         *vpp = nvp;
1168         return (0);
1169 }
1170
1171 /*
1172  * Vnode pointer to File handle
1173  */
1174 /* ARGSUSED */
1175 static int
1176 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1177 {
1178         struct inode *ip;
1179         struct ufid *ufhp;
1180
1181         ip = VTOI(vp);
1182         ufhp = (struct ufid *)fhp;
1183         ufhp->ufid_len = sizeof(struct ufid);
1184         ufhp->ufid_ino = ip->i_number;
1185         ufhp->ufid_gen = ip->i_gen;
1186         return (0);
1187 }
1188
1189 /*
1190  * This is the generic part of fhtovp called after the underlying
1191  * filesystem has validated the file handle.
1192  *
1193  * Verify that a host should have access to a filesystem.
1194  */
1195 int
1196 ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1197                  struct ucred **credanonp)
1198 {
1199         struct netcred *np;
1200         struct ext2_mount *ump;
1201
1202         ump = VFSTOEXT2(mp);
1203         /*
1204          * Get the export permission structure for this <mp, client> tuple.
1205          */
1206         np = vfs_export_lookup(mp, &ump->um_export, nam);
1207         if (np == NULL)
1208                 return (EACCES);
1209
1210         *exflagsp = np->netc_exflags;
1211         *credanonp = &np->netc_anon;
1212         return (0);
1213 }
1214
1215 /*
1216  * Write a superblock and associated information back to disk.
1217  */
1218 static int
1219 ext2_sbupdate(struct ext2_mount *mp, int waitfor)
1220 {
1221         struct ext2_sb_info *fs = mp->um_e2fs;
1222         struct ext2_super_block *es = fs->s_es;
1223         struct buf *bp;
1224         int error = 0;
1225 /*
1226 kprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1227 */
1228         bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1229         bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1230         if (waitfor == MNT_WAIT)
1231                 error = bwrite(bp);
1232         else
1233                 bawrite(bp);
1234
1235         /*
1236          * The buffers for group descriptors, inode bitmaps and block bitmaps
1237          * are not busy at this point and are (hopefully) written by the
1238          * usual sync mechanism. No need to write them here
1239          */
1240
1241         return (error);
1242 }