Merge branch 'vendor/TRE'
[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         struct partinfo dpart;
685         int error, i;
686         int ronly;
687
688         /*
689          * Disallow multiple mounts of the same device.
690          * Disallow mounting of a device that is currently in use
691          * (except for root, which might share swap device for miniroot).
692          * Flush out any old buffers remaining from a previous use.
693          */
694         if ((error = vfs_mountedon(devvp)) != 0)
695                 return (error);
696         if (vcount(devvp) > 0)
697                 return (EBUSY);
698         if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)) != 0)
699                 return (error);
700 #ifdef READONLY
701 /* turn on this to force it to be read-only */
702         mp->mnt_flag |= MNT_RDONLY;
703 #endif
704
705         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
706         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
707         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
708         vn_unlock(devvp);
709         if (error)
710                 return (error);
711         dev = devvp->v_rdev;
712         if (dev->si_iosize_max != 0)
713                 mp->mnt_iosize_max = dev->si_iosize_max;
714         if (mp->mnt_iosize_max > MAXPHYS)
715                 mp->mnt_iosize_max = MAXPHYS;
716         VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, NULL);
717
718         bp = NULL;
719         ump = NULL;
720         if ((error = bread(devvp, SBOFF, SBSIZE, &bp)) != 0)
721                 goto out;
722         es = (struct ext2_super_block *)bp->b_data;
723         if (ext2_check_sb_compat(es, dev, ronly) != 0) {
724                 error = EINVAL;         /* XXX needs translation */
725                 goto out;
726         }
727         if ((es->s_state & EXT2_VALID_FS) == 0 ||
728             (es->s_state & EXT2_ERROR_FS)) {
729                 if (ronly || (mp->mnt_flag & MNT_FORCE)) {
730                         kprintf(
731 "WARNING: Filesystem was not properly dismounted\n");
732                 } else {
733                         kprintf(
734 "WARNING: R/W mount denied.  Filesystem is not clean - run fsck\n");
735                         error = EPERM;
736                         goto out;
737                 }
738         }
739         ump = kmalloc(sizeof *ump, M_EXT2MNT, M_WAITOK | M_ZERO);
740         ump->um_malloctype = M_EXT2NODE;
741         ump->um_blkatoff = ext2_blkatoff;
742         ump->um_truncate = ext2_truncate;
743         ump->um_update = ext2_update;
744         ump->um_valloc = ext2_valloc;
745         ump->um_vfree = ext2_vfree;
746         /* I don't know whether this is the right strategy. Note that
747            we dynamically allocate both a ext2_sb_info and a ext2_super_block
748            while Linux keeps the super block in a locked buffer
749          */
750         ump->um_e2fs = kmalloc(sizeof(struct ext2_sb_info),
751                 M_EXT2MNT, M_WAITOK);
752         ump->um_e2fs->s_es = kmalloc(sizeof(struct ext2_super_block),
753                 M_EXT2MNT, M_WAITOK);
754         bcopy(es, ump->um_e2fs->s_es, (u_int)sizeof(struct ext2_super_block));
755         if ((error = compute_sb_data(devvp, ump->um_e2fs->s_es, ump->um_e2fs)))
756                 goto out;
757         /*
758          * We don't free the group descriptors allocated by compute_sb_data()
759          * until ext2_unmount().  This is OK since the mount will succeed.
760          */
761         brelse(bp);
762         bp = NULL;
763         fs = ump->um_e2fs;
764         fs->s_rd_only = ronly;  /* ronly is set according to mnt_flags */
765         /* if the fs is not mounted read-only, make sure the super block is
766            always written back on a sync()
767          */
768         fs->s_wasvalid = fs->s_es->s_state & EXT2_VALID_FS ? 1 : 0;
769         if (ronly == 0) {
770                 fs->s_dirt = 1;         /* mark it modified */
771                 fs->s_es->s_state &= ~EXT2_VALID_FS;    /* set fs invalid */
772         }
773         mp->mnt_data = (qaddr_t)ump;
774         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
775         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
776         mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
777         mp->mnt_flag |= MNT_LOCAL;
778         ump->um_mountp = mp;
779         ump->um_dev = dev;
780         ump->um_devvp = devvp;
781         /* setting those two parameters allows us to use
782            ext2_bmap w/o changse !
783         */
784         ump->um_nindir = EXT2_ADDR_PER_BLOCK(fs);
785         ump->um_bptrtodb = fs->s_es->s_log_block_size + 1;
786         ump->um_seqinc = EXT2_FRAGS_PER_BLOCK(fs);
787         for (i = 0; i < MAXQUOTAS; i++)
788                 ump->um_quotas[i] = NULLVP;
789         dev->si_mountpoint = mp;
790
791         vfs_add_vnodeops(mp, &ext2_vnode_vops, &mp->mnt_vn_norm_ops);
792         vfs_add_vnodeops(mp, &ext2_spec_vops, &mp->mnt_vn_spec_ops);
793         vfs_add_vnodeops(mp, &ext2_fifo_vops, &mp->mnt_vn_fifo_ops);
794
795         if (ronly == 0)
796                 ext2_sbupdate(ump, MNT_WAIT);
797         return (0);
798 out:
799         if (bp)
800                 brelse(bp);
801         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
802         VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
803         vn_unlock(devvp);
804         if (ump) {
805                 kfree(ump->um_e2fs->s_es, M_EXT2MNT);
806                 kfree(ump->um_e2fs, M_EXT2MNT);
807                 kfree(ump, M_EXT2MNT);
808                 mp->mnt_data = (qaddr_t)0;
809         }
810         return (error);
811 }
812
813 /*
814  * unmount system call
815  */
816 static int
817 ext2_unmount(struct mount *mp, int mntflags)
818 {
819         struct ext2_mount *ump;
820         struct ext2_sb_info *fs;
821         int error, flags, ronly, i;
822
823         flags = 0;
824         if (mntflags & MNT_FORCE) {
825                 if (mp->mnt_flag & MNT_ROOTFS)
826                         return (EINVAL);
827                 flags |= FORCECLOSE;
828         }
829         if ((error = ext2_flushfiles(mp, flags)) != 0)
830                 return (error);
831         ump = VFSTOEXT2(mp);
832         fs = ump->um_e2fs;
833         ronly = fs->s_rd_only;
834         if (ronly == 0) {
835                 if (fs->s_wasvalid)
836                         fs->s_es->s_state |= EXT2_VALID_FS;
837                 ext2_sbupdate(ump, MNT_WAIT);
838         }
839
840         /* release buffers containing group descriptors */
841         for(i = 0; i < fs->s_db_per_group; i++)
842                 ULCK_BUF(fs->s_group_desc[i])
843         kfree(fs->s_group_desc, M_EXT2MNT);
844
845         /* release cached inode/block bitmaps */
846         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
847                 if (fs->s_inode_bitmap[i])
848                         ULCK_BUF(fs->s_inode_bitmap[i])
849
850         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
851                 if (fs->s_block_bitmap[i])
852                         ULCK_BUF(fs->s_block_bitmap[i])
853
854         ump->um_devvp->v_rdev->si_mountpoint = NULL;
855
856         vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
857         error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE, NULL);
858         vn_unlock(ump->um_devvp);
859
860         vrele(ump->um_devvp);
861         kfree(fs->s_es, M_EXT2MNT);
862         kfree(fs, M_EXT2MNT);
863         kfree(ump, M_EXT2MNT);
864         mp->mnt_data = (qaddr_t)0;
865         mp->mnt_flag &= ~MNT_LOCAL;
866         return (error);
867 }
868
869 /*
870  * Flush out all the files in a filesystem.
871  */
872 static int
873 ext2_flushfiles(struct mount *mp, int flags)
874 {
875         struct ext2_mount *ump;
876         int error;
877 #if QUOTA
878         int i;
879 #endif
880
881         ump = VFSTOEXT2(mp);
882 #if QUOTA
883         if (mp->mnt_flag & MNT_QUOTA) {
884                 if ((error = vflush(mp, 0, SKIPSYSTEM|flags)) != 0)
885                         return (error);
886                 for (i = 0; i < MAXQUOTAS; i++) {
887                         if (ump->um_quotas[i] == NULLVP)
888                                 continue;
889                         ext2_quotaoff(mp, i);
890                 }
891                 /*
892                  * Here we fall through to vflush again to ensure
893                  * that we have gotten rid of all the system vnodes.
894                  */
895         }
896 #endif
897         error = vflush(mp, 0, flags);
898         return (error);
899 }
900
901 /*
902  * Go through the disk queues to initiate sandbagged IO;
903  * go through the inodes to write those that have been modified;
904  * initiate the writing of the super block if it has been modified.
905  *
906  * Note: we are always called with the filesystem marked `MPBUSY'.
907  */
908
909 static int ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data);
910
911 static int
912 ext2_sync(struct mount *mp, int waitfor)
913 {
914         struct ext2_mount *ump = VFSTOEXT2(mp);
915         struct ext2_sb_info *fs;
916         struct scaninfo scaninfo;
917         int error;
918
919         fs = ump->um_e2fs;
920         if (fs->s_dirt != 0 && fs->s_rd_only != 0) {            /* XXX */
921                 kprintf("fs = %s\n", fs->fs_fsmnt);
922                 panic("ext2_sync: rofs mod");
923         }
924
925         /*
926          * Write back each (modified) inode.
927          */
928         scaninfo.allerror = 0;
929         scaninfo.rescan = 1;
930         scaninfo.waitfor = waitfor;
931         while (scaninfo.rescan) {
932                 scaninfo.rescan = 0;
933                 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT,
934                               NULL, ext2_sync_scan, &scaninfo);
935         }
936
937         /*
938          * Force stale file system control information to be flushed.
939          */
940         if ((waitfor & MNT_LAZY) == 0) {
941                 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
942                 if ((error = VOP_FSYNC(ump->um_devvp, waitfor, 0)) != 0)
943                         scaninfo.allerror = error;
944                 vn_unlock(ump->um_devvp);
945         }
946 #if QUOTA
947         ext2_qsync(mp);
948 #endif
949         /*
950          * Write back modified superblock.
951          */
952         if (fs->s_dirt != 0) {
953                 fs->s_dirt = 0;
954                 fs->s_es->s_wtime = time_second;
955                 if ((error = ext2_sbupdate(ump, waitfor)) != 0)
956                         scaninfo.allerror = error;
957         }
958         return (scaninfo.allerror);
959 }
960
961 static int
962 ext2_sync_scan(struct mount *mp, struct vnode *vp, void *data)
963 {
964         struct scaninfo *info = data;
965         struct inode *ip;
966         int error;
967
968         ip = VTOI(vp);
969         if (vp->v_type == VNON ||
970             ((ip->i_flag &
971             (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
972             (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
973                 return(0);
974         }
975         if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
976                 info->allerror = error;
977         return(0);
978 }
979
980 /*
981  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
982  * in from disk.  If it is in core, wait for the lock bit to clear, then
983  * return the inode locked.  Detection and handling of mount points must be
984  * done by the calling routine.
985  */
986 static int
987 ext2_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
988 {
989         struct ext2_sb_info *fs;
990         struct inode *ip;
991         struct ext2_mount *ump;
992         struct buf *bp;
993         struct vnode *vp;
994         cdev_t dev;
995         int i, error;
996         int used_blocks;
997
998         ump = VFSTOEXT2(mp);
999         dev = ump->um_dev;
1000 restart:
1001         if ((*vpp = ext2_ihashget(dev, ino)) != NULL)
1002                 return (0);
1003
1004         /*
1005          * Lock out the creation of new entries in the FFS hash table in
1006          * case getnewvnode() or MALLOC() blocks, otherwise a duplicate
1007          * may occur!
1008          */
1009         if (ext2fs_inode_hash_lock) {
1010                 while (ext2fs_inode_hash_lock) {
1011                         ext2fs_inode_hash_lock = -1;
1012                         tsleep(&ext2fs_inode_hash_lock, 0, "e2vget", 0);
1013                 }
1014                 goto restart;
1015         }
1016         ext2fs_inode_hash_lock = 1;
1017
1018         /*
1019          * If this MALLOC() is performed after the getnewvnode()
1020          * it might block, leaving a vnode with a NULL v_data to be
1021          * found by ext2_sync() if a sync happens to fire right then,
1022          * which will cause a panic because ext2_sync() blindly
1023          * dereferences vp->v_data (as well it should).
1024          */
1025         ip = kmalloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK);
1026
1027         /* Allocate a new vnode/inode. */
1028         if ((error = getnewvnode(VT_EXT2FS, mp, &vp, 0, LK_CANRECURSE)) != 0) {
1029                 if (ext2fs_inode_hash_lock < 0)
1030                         wakeup(&ext2fs_inode_hash_lock);
1031                 ext2fs_inode_hash_lock = 0;
1032                 *vpp = NULL;
1033                 kfree(ip, M_EXT2NODE);
1034                 return (error);
1035         }
1036         bzero((caddr_t)ip, sizeof(struct inode));
1037         vp->v_data = ip;
1038         ip->i_vnode = vp;
1039         ip->i_e2fs = fs = ump->um_e2fs;
1040         ip->i_dev = dev;
1041         ip->i_number = ino;
1042 #if QUOTA
1043         for (i = 0; i < MAXQUOTAS; i++)
1044                 ip->i_dquot[i] = NODQUOT;
1045 #endif
1046         /*
1047          * Put it onto its hash chain.  Since our vnode is locked, other
1048          * requests for this inode will block if they arrive while we are
1049          * sleeping waiting for old data structures to be purged or for the
1050          * contents of the disk portion of this inode to be read.
1051          */
1052         ext2_ihashins(ip);
1053
1054         if (ext2fs_inode_hash_lock < 0)
1055                 wakeup(&ext2fs_inode_hash_lock);
1056         ext2fs_inode_hash_lock = 0;
1057
1058         /* Read in the disk contents for the inode, copy into the inode. */
1059 #if 0
1060 kprintf("ext2_vget(%d) dbn= %d ", ino, fsbtodb(fs, ino_to_fsba(fs, ino)));
1061 #endif
1062         error = bread(ump->um_devvp, fsbtodoff(fs, ino_to_fsba(fs, ino)),
1063                       (int)fs->s_blocksize, &bp);
1064         if (error) {
1065                 /*
1066                  * The inode does not contain anything useful, so it would
1067                  * be misleading to leave it on its hash chain. With mode
1068                  * still zero, it will be unlinked and returned to the free
1069                  * list by vput().
1070                  */
1071                 brelse(bp);
1072                 vx_put(vp);
1073                 *vpp = NULL;
1074                 return (error);
1075         }
1076         /* convert ext2 inode to dinode */
1077         ext2_ei2di((struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE(fs) *
1078                         ino_to_fsbo(fs, ino)), &ip->i_din);
1079         ip->i_block_group = ino_to_cg(fs, ino);
1080         ip->i_next_alloc_block = 0;
1081         ip->i_next_alloc_goal = 0;
1082         ip->i_prealloc_count = 0;
1083         ip->i_prealloc_block = 0;
1084         /* now we want to make sure that block pointers for unused
1085            blocks are zeroed out - ext2_balloc depends on this
1086            although for regular files and directories only
1087         */
1088         if(S_ISDIR(ip->i_mode) || S_ISREG(ip->i_mode)) {
1089                 used_blocks = (ip->i_size+fs->s_blocksize-1) / fs->s_blocksize;
1090                 for(i = used_blocks; i < EXT2_NDIR_BLOCKS; i++)
1091                         ip->i_db[i] = 0;
1092         }
1093 #if 0
1094         ext2_print_inode(ip);
1095 #endif
1096         bqrelse(bp);
1097
1098         /*
1099          * Initialize the vnode from the inode, check for aliases.
1100          * Note that the underlying vnode may have changed.
1101          */
1102         if ((error = ext2_vinit(mp, &vp)) != 0) {
1103                 vx_put(vp);
1104                 *vpp = NULL;
1105                 return (error);
1106         }
1107
1108         /*
1109          * Finish inode initialization now that aliasing has been resolved.
1110          */
1111         ip->i_devvp = ump->um_devvp;
1112         vref(ip->i_devvp);
1113         /*
1114          * Set up a generation number for this inode if it does not
1115          * already have one. This should only happen on old filesystems.
1116          */
1117         if (ip->i_gen == 0) {
1118                 ip->i_gen = krandom() / 2 + 1;
1119                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1120                         ip->i_flag |= IN_MODIFIED;
1121         }
1122         /*
1123          * Return the locked and refd vnode.
1124          */
1125         *vpp = vp;
1126         return (0);
1127 }
1128
1129 /*
1130  * File handle to vnode
1131  *
1132  * Have to be really careful about stale file handles:
1133  * - check that the inode number is valid
1134  * - call ext2_vget() to get the locked inode
1135  * - check for an unallocated inode (i_mode == 0)
1136  * - check that the given client host has export rights and return
1137  *   those rights via. exflagsp and credanonp
1138  */
1139 static int
1140 ext2_fhtovp(struct mount *mp, struct vnode *rootvp,
1141             struct fid *fhp, struct vnode **vpp)
1142 {
1143         struct ufid *ufhp;
1144         struct ext2_sb_info *fs;
1145         struct inode *ip;
1146         struct vnode *nvp;
1147         int error;
1148
1149         ufhp = (struct ufid *)fhp;
1150         fs = VFSTOEXT2(mp)->um_e2fs;
1151         if (ufhp->ufid_ino < EXT2_ROOTINO ||
1152             ufhp->ufid_ino > fs->s_groups_count * fs->s_es->s_inodes_per_group)
1153                 return (ESTALE);
1154
1155         error = VFS_VGET(mp, rootvp, ufhp->ufid_ino, &nvp);
1156         if (error) {
1157                 *vpp = NULLVP;
1158                 return (error);
1159         }
1160         ip = VTOI(nvp);
1161         if (ip->i_mode == 0 ||
1162             ip->i_gen != ufhp->ufid_gen ||
1163             (VFSTOEXT2(mp)->um_i_effnlink_valid ? ip->i_effnlink :
1164             ip->i_nlink) <= 0) {
1165                 vput(nvp);
1166                 *vpp = NULLVP;
1167                 return (ESTALE);
1168         }
1169         *vpp = nvp;
1170         return (0);
1171 }
1172
1173 /*
1174  * Vnode pointer to File handle
1175  */
1176 /* ARGSUSED */
1177 static int
1178 ext2_vptofh(struct vnode *vp, struct fid *fhp)
1179 {
1180         struct inode *ip;
1181         struct ufid *ufhp;
1182
1183         ip = VTOI(vp);
1184         ufhp = (struct ufid *)fhp;
1185         ufhp->ufid_len = sizeof(struct ufid);
1186         ufhp->ufid_ino = ip->i_number;
1187         ufhp->ufid_gen = ip->i_gen;
1188         return (0);
1189 }
1190
1191 /*
1192  * This is the generic part of fhtovp called after the underlying
1193  * filesystem has validated the file handle.
1194  *
1195  * Verify that a host should have access to a filesystem.
1196  */
1197 int
1198 ext2_check_export(struct mount *mp, struct sockaddr *nam, int *exflagsp,
1199                  struct ucred **credanonp)
1200 {
1201         struct netcred *np;
1202         struct ext2_mount *ump;
1203
1204         ump = VFSTOEXT2(mp);
1205         /*
1206          * Get the export permission structure for this <mp, client> tuple.
1207          */
1208         np = vfs_export_lookup(mp, &ump->um_export, nam);
1209         if (np == NULL)
1210                 return (EACCES);
1211
1212         *exflagsp = np->netc_exflags;
1213         *credanonp = &np->netc_anon;
1214         return (0);
1215 }
1216
1217 /*
1218  * Write a superblock and associated information back to disk.
1219  */
1220 static int
1221 ext2_sbupdate(struct ext2_mount *mp, int waitfor)
1222 {
1223         struct ext2_sb_info *fs = mp->um_e2fs;
1224         struct ext2_super_block *es = fs->s_es;
1225         struct buf *bp;
1226         int error = 0;
1227 /*
1228 kprintf("\nupdating superblock, waitfor=%s\n", waitfor == MNT_WAIT ? "yes":"no");
1229 */
1230         bp = getblk(mp->um_devvp, SBOFF, SBSIZE, 0, 0);
1231         bcopy((caddr_t)es, bp->b_data, (u_int)sizeof(struct ext2_super_block));
1232         if (waitfor == MNT_WAIT)
1233                 error = bwrite(bp);
1234         else
1235                 bawrite(bp);
1236
1237         /*
1238          * The buffers for group descriptors, inode bitmaps and block bitmaps
1239          * are not busy at this point and are (hopefully) written by the
1240          * usual sync mechanism. No need to write them here
1241          */
1242
1243         return (error);
1244 }