[BACK]Return to ext2fs_vfsops.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ufs / ext2fs

Annotation of src/sys/ufs/ext2fs/ext2fs_vfsops.c, Revision 1.183.2.2

1.183.2.2! martin      1: /*     $NetBSD: ext2fs_vfsops.c,v 1.183.2.1 2014/08/22 19:20:15 martin Exp $   */
1.1       bouyer      2:
                      3: /*
                      4:  * Copyright (c) 1989, 1991, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
1.18      christos   11:  *    notice, this list of conditions and the following disclaimer.
1.1       bouyer     12:  * 2. Redistributions in binary form must reproduce the above copyright
1.18      christos   13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.61      agc        15:  * 3. Neither the name of the University nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  *
                     31:  *     @(#)ffs_vfsops.c        8.14 (Berkeley) 11/28/94
                     32:  * Modified for ext2fs by Manuel Bouyer.
                     33:  */
                     34:
                     35: /*
                     36:  * Copyright (c) 1997 Manuel Bouyer.
                     37:  *
                     38:  * Redistribution and use in source and binary forms, with or without
                     39:  * modification, are permitted provided that the following conditions
                     40:  * are met:
                     41:  * 1. Redistributions of source code must retain the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer.
                     43:  * 2. Redistributions in binary form must reproduce the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer in the
                     45:  *    documentation and/or other materials provided with the distribution.
1.1       bouyer     46:  *
1.65      bouyer     47:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     48:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     49:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     50:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     51:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     52:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     53:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     54:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     55:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     56:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       bouyer     57:  *
                     58:  *     @(#)ffs_vfsops.c        8.14 (Berkeley) 11/28/94
                     59:  * Modified for ext2fs by Manuel Bouyer.
                     60:  */
1.48      lukem      61:
                     62: #include <sys/cdefs.h>
1.183.2.2! martin     63: __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.183.2.1 2014/08/22 19:20:15 martin Exp $");
1.15      jonathan   64:
1.43      mrg        65: #if defined(_KERNEL_OPT)
1.15      jonathan   66: #include "opt_compat_netbsd.h"
                     67: #endif
1.13      sommerfe   68:
1.1       bouyer     69: #include <sys/param.h>
                     70: #include <sys/systm.h>
1.64      atatat     71: #include <sys/sysctl.h>
1.1       bouyer     72: #include <sys/namei.h>
                     73: #include <sys/proc.h>
                     74: #include <sys/kernel.h>
                     75: #include <sys/vnode.h>
                     76: #include <sys/socket.h>
                     77: #include <sys/mount.h>
                     78: #include <sys/buf.h>
                     79: #include <sys/device.h>
                     80: #include <sys/mbuf.h>
                     81: #include <sys/file.h>
                     82: #include <sys/disklabel.h>
                     83: #include <sys/ioctl.h>
                     84: #include <sys/errno.h>
1.17      thorpej    85: #include <sys/pool.h>
1.3       bouyer     86: #include <sys/lock.h>
1.51      gehenna    87: #include <sys/conf.h>
1.97      elad       88: #include <sys/kauth.h>
1.135     rumble     89: #include <sys/module.h>
1.1       bouyer     90:
1.129     dholland   91: #include <miscfs/genfs/genfs.h>
1.1       bouyer     92: #include <miscfs/specfs/specdev.h>
                     93:
                     94: #include <ufs/ufs/quota.h>
                     95: #include <ufs/ufs/ufsmount.h>
                     96: #include <ufs/ufs/inode.h>
                     97: #include <ufs/ufs/dir.h>
                     98: #include <ufs/ufs/ufs_extern.h>
                     99:
                    100: #include <ufs/ext2fs/ext2fs.h>
1.88      christos  101: #include <ufs/ext2fs/ext2fs_dir.h>
1.1       bouyer    102: #include <ufs/ext2fs/ext2fs_extern.h>
                    103:
1.139     ad        104: MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
1.135     rumble    105:
1.89      xtraeme   106: int ext2fs_sbupdate(struct ufsmount *, int);
                    107: static int ext2fs_checksb(struct ext2fs *, int);
1.7       thorpej   108:
1.137     rumble    109: static struct sysctllog *ext2fs_sysctl_log;
                    110:
1.42      jdolecek  111: extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
                    112: extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
                    113: extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
1.7       thorpej   114:
1.42      jdolecek  115: const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
1.7       thorpej   116:        &ext2fs_vnodeop_opv_desc,
                    117:        &ext2fs_specop_opv_desc,
                    118:        &ext2fs_fifoop_opv_desc,
                    119:        NULL,
                    120: };
1.1       bouyer    121:
                    122: struct vfsops ext2fs_vfsops = {
1.179     hannken   123:        .vfs_name = MOUNT_EXT2FS,
                    124:        .vfs_min_mount_data = sizeof (struct ufs_args),
                    125:        .vfs_mount = ext2fs_mount,
                    126:        .vfs_start = ufs_start,
                    127:        .vfs_unmount = ext2fs_unmount,
                    128:        .vfs_root = ufs_root,
                    129:        .vfs_quotactl = ufs_quotactl,
                    130:        .vfs_statvfs = ext2fs_statvfs,
                    131:        .vfs_sync = ext2fs_sync,
1.181     hannken   132:        .vfs_vget = ufs_vget,
                    133:        .vfs_loadvnode = ext2fs_loadvnode,
1.179     hannken   134:        .vfs_fhtovp = ext2fs_fhtovp,
                    135:        .vfs_vptofh = ext2fs_vptofh,
                    136:        .vfs_init = ext2fs_init,
                    137:        .vfs_reinit = ext2fs_reinit,
                    138:        .vfs_done = ext2fs_done,
                    139:        .vfs_mountroot = ext2fs_mountroot,
                    140:        .vfs_snapshot = (void *)eopnotsupp,
                    141:        .vfs_extattrctl = vfs_stdextattrctl,
                    142:        .vfs_suspendctl = (void *)eopnotsupp,
                    143:        .vfs_renamelock_enter = genfs_renamelock_enter,
                    144:        .vfs_renamelock_exit = genfs_renamelock_exit,
                    145:        .vfs_fsync = (void *)eopnotsupp,
                    146:        .vfs_opv_descs = ext2fs_vnodeopv_descs
1.1       bouyer    147: };
                    148:
1.86      yamt      149: static const struct genfs_ops ext2fs_genfsops = {
                    150:        .gop_size = genfs_size,
                    151:        .gop_alloc = ext2fs_gop_alloc,
                    152:        .gop_write = genfs_gop_write,
1.87      yamt      153:        .gop_markupdate = ufs_gop_markupdate,
1.45      chs       154: };
                    155:
1.92      yamt      156: static const struct ufs_ops ext2fs_ufsops = {
                    157:        .uo_itimes = ext2fs_itimes,
1.93      yamt      158:        .uo_update = ext2fs_update,
1.126     ad        159:        .uo_vfree = ext2fs_vfree,
1.92      yamt      160: };
                    161:
1.140     mrg       162: /* Fill in the inode uid/gid from ext2 halves.  */
1.152     pooka     163: void
1.140     mrg       164: ext2fs_set_inode_guid(struct inode *ip)
                    165: {
                    166:
                    167:        ip->i_gid = ip->i_e2fs_gid;
                    168:        ip->i_uid = ip->i_e2fs_uid;
                    169:        if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
                    170:                ip->i_gid |= ip->i_e2fs_gid_high << 16;
                    171:                ip->i_uid |= ip->i_e2fs_uid_high << 16;
                    172:        }
                    173: }
                    174:
1.135     rumble    175: static int
                    176: ext2fs_modcmd(modcmd_t cmd, void *arg)
                    177: {
1.137     rumble    178:        int error;
1.135     rumble    179:
                    180:        switch (cmd) {
                    181:        case MODULE_CMD_INIT:
1.137     rumble    182:                error = vfs_attach(&ext2fs_vfsops);
                    183:                if (error != 0)
                    184:                        break;
                    185:                sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
                    186:                               CTLFLAG_PERMANENT,
                    187:                               CTLTYPE_NODE, "ext2fs",
                    188:                               SYSCTL_DESCR("Linux EXT2FS file system"),
                    189:                               NULL, 0, NULL, 0,
                    190:                               CTL_VFS, 17, CTL_EOL);
                    191:                /*
                    192:                 * XXX the "17" above could be dynamic, thereby eliminating
                    193:                 * one more instance of the "number to vfs" mapping problem,
                    194:                 * but "17" is the order as taken from sys/mount.h
                    195:                 */
                    196:                break;
1.135     rumble    197:        case MODULE_CMD_FINI:
1.137     rumble    198:                error = vfs_detach(&ext2fs_vfsops);
                    199:                if (error != 0)
                    200:                        break;
                    201:                sysctl_teardown(&ext2fs_sysctl_log);
                    202:                break;
1.135     rumble    203:        default:
1.137     rumble    204:                error = ENOTTY;
                    205:                break;
1.135     rumble    206:        }
1.137     rumble    207:
                    208:        return (error);
1.135     rumble    209: }
                    210:
1.68      simonb    211: /*
                    212:  * XXX Same structure as FFS inodes?  Should we share a common pool?
                    213:  */
1.112     pooka     214: struct pool ext2fs_inode_pool;
                    215: struct pool ext2fs_dinode_pool;
1.17      thorpej   216:
1.1       bouyer    217: extern u_long ext2gennumber;
                    218:
1.9       fvdl      219: void
1.89      xtraeme   220: ext2fs_init(void)
1.9       fvdl      221: {
1.112     pooka     222:
1.70      atatat    223:        pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
1.110     ad        224:            "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
1.70      atatat    225:        pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
1.110     ad        226:            "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
1.19      bouyer    227:        ufs_init();
1.44      chs       228: }
                    229:
                    230: void
1.89      xtraeme   231: ext2fs_reinit(void)
1.44      chs       232: {
                    233:        ufs_reinit();
1.9       fvdl      234: }
                    235:
1.34      jdolecek  236: void
1.89      xtraeme   237: ext2fs_done(void)
1.34      jdolecek  238: {
1.112     pooka     239:
1.34      jdolecek  240:        ufs_done();
                    241:        pool_destroy(&ext2fs_inode_pool);
1.70      atatat    242:        pool_destroy(&ext2fs_dinode_pool);
1.34      jdolecek  243: }
1.1       bouyer    244:
                    245: /*
                    246:  * Called by main() when ext2fs is going to be mounted as root.
                    247:  *
                    248:  * Name is updated by mount(8) after booting.
                    249:  */
                    250:
                    251: int
1.89      xtraeme   252: ext2fs_mountroot(void)
1.1       bouyer    253: {
                    254:        extern struct vnode *rootvp;
1.9       fvdl      255:        struct m_ext2fs *fs;
                    256:        struct mount *mp;
1.1       bouyer    257:        struct ufsmount *ump;
                    258:        int error;
                    259:
1.95      thorpej   260:        if (device_class(root_device) != DV_DISK)
1.1       bouyer    261:                return (ENODEV);
1.83      perry     262:
1.27      wrstuden  263:        if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
                    264:                vrele(rootvp);
1.9       fvdl      265:                return (error);
1.27      wrstuden  266:        }
1.9       fvdl      267:
1.123     pooka     268:        if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
1.133     ad        269:                vfs_unbusy(mp, false, NULL);
1.134     ad        270:                vfs_destroy(mp);
1.1       bouyer    271:                return (error);
                    272:        }
1.175     christos  273:        mountlist_append(mp);
1.1       bouyer    274:        ump = VFSTOUFS(mp);
                    275:        fs = ump->um_e2fs;
1.16      perry     276:        memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
1.9       fvdl      277:        (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
1.31      bouyer    278:            sizeof(fs->e2fs_fsmnt) - 1, 0);
                    279:        if (fs->e2fs.e2fs_rev > E2FS_REV0) {
                    280:                memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
                    281:                (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
                    282:                    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
                    283:        }
1.123     pooka     284:        (void)ext2fs_statvfs(mp, &mp->mnt_stat);
1.133     ad        285:        vfs_unbusy(mp, false, NULL);
1.73      pk        286:        setrootfstime((time_t)fs->e2fs.e2fs_wtime);
1.1       bouyer    287:        return (0);
                    288: }
                    289:
                    290: /*
                    291:  * VFS Operations.
                    292:  *
                    293:  * mount system call
                    294:  */
                    295: int
1.123     pooka     296: ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
1.1       bouyer    297: {
1.123     pooka     298:        struct lwp *l = curlwp;
1.1       bouyer    299:        struct vnode *devvp;
1.113     dsl       300:        struct ufs_args *args = data;
1.1       bouyer    301:        struct ufsmount *ump = NULL;
1.35      augustss  302:        struct m_ext2fs *fs;
1.1       bouyer    303:        size_t size;
1.113     dsl       304:        int error = 0, flags, update;
1.1       bouyer    305:        mode_t accessmode;
1.52      christos  306:
1.180     maxv      307:        if (args == NULL)
                    308:                return EINVAL;
1.113     dsl       309:        if (*data_len < sizeof *args)
                    310:                return EINVAL;
                    311:
1.52      christos  312:        if (mp->mnt_flag & MNT_GETARGS) {
                    313:                ump = VFSTOUFS(mp);
                    314:                if (ump == NULL)
                    315:                        return EIO;
1.113     dsl       316:                memset(args, 0, sizeof *args);
                    317:                args->fspec = NULL;
                    318:                *data_len = sizeof *args;
                    319:                return 0;
1.52      christos  320:        }
1.80      mycroft   321:
                    322:        update = mp->mnt_flag & MNT_UPDATE;
                    323:
1.81      mycroft   324:        /* Check arguments */
1.113     dsl       325:        if (args->fspec != NULL) {
1.80      mycroft   326:                /*
                    327:                 * Look up the name and verify that it's sane.
                    328:                 */
1.144     dholland  329:                error = namei_simple_user(args->fspec,
                    330:                                        NSM_FOLLOW_NOEMULROOT, &devvp);
                    331:                if (error != 0)
1.80      mycroft   332:                        return (error);
                    333:
                    334:                if (!update) {
                    335:                        /*
                    336:                         * Be sure this is a valid block device
                    337:                         */
                    338:                        if (devvp->v_type != VBLK)
                    339:                                error = ENOTBLK;
                    340:                        else if (bdevsw_lookup(devvp->v_rdev) == NULL)
                    341:                                error = ENXIO;
                    342:                } else {
                    343:                        /*
                    344:                         * Be sure we're still naming the same device
                    345:                         * used for our initial mount
                    346:                         */
1.81      mycroft   347:                        ump = VFSTOUFS(mp);
1.146     tsutsui   348:                        if (devvp != ump->um_devvp) {
                    349:                                if (devvp->v_rdev != ump->um_devvp->v_rdev)
                    350:                                        error = EINVAL;
                    351:                                else {
                    352:                                        vrele(devvp);
                    353:                                        devvp = ump->um_devvp;
                    354:                                        vref(devvp);
                    355:                                }
                    356:                        }
1.80      mycroft   357:                }
1.81      mycroft   358:        } else {
                    359:                if (!update) {
                    360:                        /* New mounts must have a filename for the device */
                    361:                        return (EINVAL);
                    362:                } else {
                    363:                        ump = VFSTOUFS(mp);
                    364:                        devvp = ump->um_devvp;
                    365:                        vref(devvp);
                    366:                }
1.80      mycroft   367:        }
                    368:
                    369:        /*
                    370:         * If mount by non-root, then verify that user has necessary
                    371:         * permissions on the device.
1.143     elad      372:         *
                    373:         * Permission to update a mount is checked higher, so here we presume
                    374:         * updating the mount is okay (for example, as far as securelevel goes)
                    375:         * which leaves us with the normal check.
1.80      mycroft   376:         */
1.145     tsutsui   377:        if (error == 0) {
                    378:                accessmode = VREAD;
                    379:                if (update ?
                    380:                    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
                    381:                    (mp->mnt_flag & MNT_RDONLY) == 0)
                    382:                        accessmode |= VWRITE;
                    383:                vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.163     elad      384:                error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
                    385:                    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
                    386:                    KAUTH_ARG(accessmode));
1.157     hannken   387:                VOP_UNLOCK(devvp);
1.145     tsutsui   388:        }
1.80      mycroft   389:
                    390:        if (error) {
                    391:                vrele(devvp);
                    392:                return (error);
                    393:        }
                    394:
                    395:        if (!update) {
1.85      christos  396:                int xflags;
1.80      mycroft   397:
                    398:                if (mp->mnt_flag & MNT_RDONLY)
1.85      christos  399:                        xflags = FREAD;
1.80      mycroft   400:                else
1.85      christos  401:                        xflags = FREAD|FWRITE;
1.162     hannken   402:                vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.123     pooka     403:                error = VOP_OPEN(devvp, xflags, FSCRED);
1.162     hannken   404:                VOP_UNLOCK(devvp);
1.80      mycroft   405:                if (error)
                    406:                        goto fail;
1.123     pooka     407:                error = ext2fs_mountfs(devvp, mp);
1.80      mycroft   408:                if (error) {
                    409:                        vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.123     pooka     410:                        (void)VOP_CLOSE(devvp, xflags, NOCRED);
1.157     hannken   411:                        VOP_UNLOCK(devvp);
1.80      mycroft   412:                        goto fail;
                    413:                }
                    414:
1.1       bouyer    415:                ump = VFSTOUFS(mp);
                    416:                fs = ump->um_e2fs;
1.80      mycroft   417:        } else {
                    418:                /*
                    419:                 * Update the mount.
                    420:                 */
                    421:
                    422:                /*
                    423:                 * The initial mount got a reference on this
                    424:                 * device, so drop the one obtained via
                    425:                 * namei(), above.
                    426:                 */
                    427:                vrele(devvp);
                    428:
1.81      mycroft   429:                ump = VFSTOUFS(mp);
1.80      mycroft   430:                fs = ump->um_e2fs;
1.1       bouyer    431:                if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
1.80      mycroft   432:                        /*
                    433:                         * Changing from r/w to r/o
                    434:                         */
1.1       bouyer    435:                        flags = WRITECLOSE;
                    436:                        if (mp->mnt_flag & MNT_FORCE)
                    437:                                flags |= FORCECLOSE;
1.123     pooka     438:                        error = ext2fs_flushfiles(mp, flags);
1.1       bouyer    439:                        if (error == 0 &&
1.80      mycroft   440:                            ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
                    441:                            (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
1.1       bouyer    442:                                fs->e2fs.e2fs_state = E2FS_ISCLEAN;
                    443:                                (void) ext2fs_sbupdate(ump, MNT_WAIT);
                    444:                        }
                    445:                        if (error)
                    446:                                return (error);
                    447:                        fs->e2fs_ronly = 1;
                    448:                }
1.80      mycroft   449:
1.1       bouyer    450:                if (mp->mnt_flag & MNT_RELOAD) {
1.149     tsutsui   451:                        error = ext2fs_reload(mp, l->l_cred, l);
1.1       bouyer    452:                        if (error)
                    453:                                return (error);
                    454:                }
1.80      mycroft   455:
1.63      dbj       456:                if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
1.1       bouyer    457:                        /*
1.80      mycroft   458:                         * Changing from read-only to read/write
1.1       bouyer    459:                         */
                    460:                        fs->e2fs_ronly = 0;
                    461:                        if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
                    462:                                fs->e2fs.e2fs_state = 0;
                    463:                        else
                    464:                                fs->e2fs.e2fs_state = E2FS_ERRORS;
                    465:                        fs->e2fs_fmod = 1;
                    466:                }
1.113     dsl       467:                if (args->fspec == NULL)
1.147     tsutsui   468:                        return 0;
1.1       bouyer    469:        }
                    470:
1.113     dsl       471:        error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
1.114     pooka     472:            UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
1.57      christos  473:        (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
                    474:            sizeof(fs->e2fs_fsmnt) - 1, &size);
1.16      perry     475:        memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
1.31      bouyer    476:        if (fs->e2fs.e2fs_rev > E2FS_REV0) {
                    477:                (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
                    478:                    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
                    479:                memset(fs->e2fs.e2fs_fsmnt, 0,
                    480:                    sizeof(fs->e2fs.e2fs_fsmnt) - size);
                    481:        }
1.1       bouyer    482:        if (fs->e2fs_fmod != 0) {       /* XXX */
                    483:                fs->e2fs_fmod = 0;
                    484:                if (fs->e2fs.e2fs_state == 0)
1.98      kardel    485:                        fs->e2fs.e2fs_wtime = time_second;
1.1       bouyer    486:                else
                    487:                        printf("%s: file system not clean; please fsck(8)\n",
                    488:                                mp->mnt_stat.f_mntfromname);
                    489:                (void) ext2fs_cgupdate(ump, MNT_WAIT);
                    490:        }
1.80      mycroft   491:        return (error);
                    492:
                    493: fail:
                    494:        vrele(devvp);
                    495:        return (error);
1.1       bouyer    496: }
                    497:
                    498: /*
                    499:  * Reload all incore data for a filesystem (used after running fsck on
                    500:  * the root filesystem and finding things to fix). The filesystem must
                    501:  * be mounted read-only.
                    502:  *
                    503:  * Things to do to update the mount:
                    504:  *     1) invalidate all cached meta-data.
                    505:  *     2) re-read superblock from disk.
                    506:  *     3) re-read summary information from disk.
                    507:  *     4) invalidate all inactive vnodes.
                    508:  *     5) invalidate all cached file data.
                    509:  *     6) re-read inode data for all active vnodes.
                    510:  */
                    511: int
1.149     tsutsui   512: ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
1.1       bouyer    513: {
1.177     hannken   514:        struct vnode *vp, *devvp;
1.1       bouyer    515:        struct inode *ip;
                    516:        struct buf *bp;
                    517:        struct m_ext2fs *fs;
                    518:        struct ext2fs *newfs;
1.154     mlelstv   519:        int i, error;
1.109     christos  520:        void *cp;
1.149     tsutsui   521:        struct ufsmount *ump;
1.177     hannken   522:        struct vnode_iterator *marker;
1.1       bouyer    523:
1.149     tsutsui   524:        if ((mp->mnt_flag & MNT_RDONLY) == 0)
1.1       bouyer    525:                return (EINVAL);
1.126     ad        526:
1.149     tsutsui   527:        ump = VFSTOUFS(mp);
1.1       bouyer    528:        /*
                    529:         * Step 1: invalidate all cached meta-data.
                    530:         */
1.149     tsutsui   531:        devvp = ump->um_devvp;
1.38      jdolecek  532:        vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.94      christos  533:        error = vinvalbuf(devvp, 0, cred, l, 0, 0);
1.157     hannken   534:        VOP_UNLOCK(devvp);
1.38      jdolecek  535:        if (error)
1.1       bouyer    536:                panic("ext2fs_reload: dirty1");
                    537:        /*
                    538:         * Step 2: re-read superblock from disk.
                    539:         */
1.155     mlelstv   540:        error = bread(devvp, SBLOCK, SBSIZE, NOCRED, 0, &bp);
1.23      bouyer    541:        if (error) {
1.1       bouyer    542:                return (error);
1.23      bouyer    543:        }
1.1       bouyer    544:        newfs = (struct ext2fs *)bp->b_data;
1.149     tsutsui   545:        error = ext2fs_checksb(newfs, (mp->mnt_flag & MNT_RDONLY) != 0);
1.31      bouyer    546:        if (error) {
1.118     ad        547:                brelse(bp, 0);
1.31      bouyer    548:                return (error);
1.1       bouyer    549:        }
                    550:
1.149     tsutsui   551:        fs = ump->um_e2fs;
1.83      perry     552:        /*
1.1       bouyer    553:         * copy in new superblock, and compute in-memory values
                    554:         */
1.4       bouyer    555:        e2fs_sbload(newfs, &fs->e2fs);
1.23      bouyer    556:        fs->e2fs_ncg =
                    557:            howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
                    558:            fs->e2fs.e2fs_bpg);
1.155     mlelstv   559:        fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
1.122     tsutsui   560:        fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
1.1       bouyer    561:        fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
                    562:        fs->e2fs_qbmask = fs->e2fs_bsize - 1;
                    563:        fs->e2fs_bmask = ~fs->e2fs_qbmask;
1.122     tsutsui   564:        fs->e2fs_ngdb =
                    565:            howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
1.142     christos  566:        fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
1.122     tsutsui   567:        fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
1.148     tsutsui   568:        brelse(bp, 0);
1.1       bouyer    569:
                    570:        /*
                    571:         * Step 3: re-read summary information from disk.
                    572:         */
                    573:
1.122     tsutsui   574:        for (i = 0; i < fs->e2fs_ngdb; i++) {
1.23      bouyer    575:                error = bread(devvp ,
1.171     dholland  576:                    EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
1.124     tsutsui   577:                    1 /* superblock */ + i),
1.136     hannken   578:                    fs->e2fs_bsize, NOCRED, 0, &bp);
1.23      bouyer    579:                if (error) {
1.1       bouyer    580:                        return (error);
1.23      bouyer    581:                }
1.122     tsutsui   582:                e2fs_cgload((struct ext2_gd *)bp->b_data,
                    583:                    &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1.23      bouyer    584:                    fs->e2fs_bsize);
1.118     ad        585:                brelse(bp, 0);
1.1       bouyer    586:        }
1.83      perry     587:
1.177     hannken   588:        vfs_vnode_iterator_init(mp, &marker);
1.182     christos  589:        while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
1.1       bouyer    590:                /*
                    591:                 * Step 4: invalidate all inactive vnodes.
                    592:                 */
1.177     hannken   593:                if (vrecycle(vp))
                    594:                        continue;
1.1       bouyer    595:                /*
                    596:                 * Step 5: invalidate all cached file data.
                    597:                 */
1.177     hannken   598:                if (vn_lock(vp, LK_EXCLUSIVE)) {
                    599:                        vrele(vp);
                    600:                        continue;
1.126     ad        601:                }
1.94      christos  602:                if (vinvalbuf(vp, 0, cred, l, 0, 0))
1.1       bouyer    603:                        panic("ext2fs_reload: dirty2");
                    604:                /*
                    605:                 * Step 6: re-read inode data for all active vnodes.
                    606:                 */
                    607:                ip = VTOI(vp);
1.171     dholland  608:                error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
1.136     hannken   609:                    (int)fs->e2fs_bsize, NOCRED, 0, &bp);
1.1       bouyer    610:                if (error) {
                    611:                        vput(vp);
1.126     ad        612:                        break;
1.1       bouyer    613:                }
1.109     christos  614:                cp = (char *)bp->b_data +
1.142     christos  615:                    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
1.55      fvdl      616:                e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
1.140     mrg       617:                ext2fs_set_inode_guid(ip);
1.118     ad        618:                brelse(bp, 0);
1.1       bouyer    619:                vput(vp);
                    620:        }
1.177     hannken   621:        vfs_vnode_iterator_destroy(marker);
1.126     ad        622:        return (error);
1.1       bouyer    623: }
                    624:
                    625: /*
                    626:  * Common code for mount and mountroot
                    627:  */
                    628: int
1.123     pooka     629: ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
1.1       bouyer    630: {
1.123     pooka     631:        struct lwp *l = curlwp;
1.35      augustss  632:        struct ufsmount *ump;
1.1       bouyer    633:        struct buf *bp;
1.35      augustss  634:        struct ext2fs *fs;
                    635:        struct m_ext2fs *m_fs;
1.1       bouyer    636:        dev_t dev;
1.154     mlelstv   637:        int error, i, ronly;
1.97      elad      638:        kauth_cred_t cred;
1.1       bouyer    639:
                    640:        dev = devvp->v_rdev;
1.100     ad        641:        cred = l ? l->l_cred : NOCRED;
1.80      mycroft   642:
                    643:        /* Flush out any old buffers remaining from a previous use. */
1.38      jdolecek  644:        vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.94      christos  645:        error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
1.157     hannken   646:        VOP_UNLOCK(devvp);
1.38      jdolecek  647:        if (error)
1.1       bouyer    648:                return (error);
                    649:
                    650:        ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
                    651:
                    652:        bp = NULL;
                    653:        ump = NULL;
                    654:
                    655: #ifdef DEBUG_EXT2
1.159     jakllsch  656:        printf("ext2 sb size: %zu\n", sizeof(struct ext2fs));
1.1       bouyer    657: #endif
1.155     mlelstv   658:        error = bread(devvp, SBLOCK, SBSIZE, cred, 0, &bp);
1.1       bouyer    659:        if (error)
                    660:                goto out;
                    661:        fs = (struct ext2fs *)bp->b_data;
1.31      bouyer    662:        error = ext2fs_checksb(fs, ronly);
                    663:        if (error)
1.1       bouyer    664:                goto out;
1.164     rmind     665:        ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
1.55      fvdl      666:        ump->um_fstype = UFS1;
1.92      yamt      667:        ump->um_ops = &ext2fs_ufsops;
1.164     rmind     668:        ump->um_e2fs = kmem_zalloc(sizeof(struct m_ext2fs), KM_SLEEP);
1.122     tsutsui   669:        e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
1.118     ad        670:        brelse(bp, 0);
1.1       bouyer    671:        bp = NULL;
                    672:        m_fs = ump->um_e2fs;
                    673:        m_fs->e2fs_ronly = ronly;
1.142     christos  674:
                    675: #ifdef DEBUG_EXT2
1.159     jakllsch  676:        printf("ext2 ino size %zu\n", EXT2_DINODE_SIZE(m_fs));
1.142     christos  677: #endif
1.1       bouyer    678:        if (ronly == 0) {
                    679:                if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
                    680:                        m_fs->e2fs.e2fs_state = 0;
                    681:                else
                    682:                        m_fs->e2fs.e2fs_state = E2FS_ERRORS;
                    683:                m_fs->e2fs_fmod = 1;
                    684:        }
                    685:
                    686:        /* compute dynamic sb infos */
                    687:        m_fs->e2fs_ncg =
1.122     tsutsui   688:            howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
                    689:            m_fs->e2fs.e2fs_bpg);
1.155     mlelstv   690:        m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
1.122     tsutsui   691:        m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
1.1       bouyer    692:        m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
                    693:        m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
                    694:        m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
1.122     tsutsui   695:        m_fs->e2fs_ngdb =
                    696:            howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
1.142     christos  697:        m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
1.122     tsutsui   698:        m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
1.1       bouyer    699:
1.164     rmind     700:        m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize, KM_SLEEP);
1.122     tsutsui   701:        for (i = 0; i < m_fs->e2fs_ngdb; i++) {
1.23      bouyer    702:                error = bread(devvp ,
1.171     dholland  703:                    EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
1.124     tsutsui   704:                    1 /* superblock */ + i),
1.136     hannken   705:                    m_fs->e2fs_bsize, NOCRED, 0, &bp);
1.1       bouyer    706:                if (error) {
1.164     rmind     707:                        kmem_free(m_fs->e2fs_gd,
                    708:                            m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
1.1       bouyer    709:                        goto out;
                    710:                }
1.122     tsutsui   711:                e2fs_cgload((struct ext2_gd *)bp->b_data,
1.23      bouyer    712:                    &m_fs->e2fs_gd[
                    713:                        i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
                    714:                    m_fs->e2fs_bsize);
1.118     ad        715:                brelse(bp, 0);
1.1       bouyer    716:                bp = NULL;
                    717:        }
                    718:
1.50      soren     719:        mp->mnt_data = ump;
1.67      christos  720:        mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
                    721:        mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
                    722:        mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1.88      christos  723:        mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
1.1       bouyer    724:        mp->mnt_flag |= MNT_LOCAL;
1.40      chs       725:        mp->mnt_dev_bshift = DEV_BSHIFT;        /* XXX */
                    726:        mp->mnt_fs_bshift = m_fs->e2fs_bshift;
1.75      mycroft   727:        mp->mnt_iflag |= IMNT_DTYPE;
1.11      bouyer    728:        ump->um_flags = 0;
1.1       bouyer    729:        ump->um_mountp = mp;
                    730:        ump->um_dev = dev;
                    731:        ump->um_devvp = devvp;
1.170     dholland  732:        ump->um_nindir = EXT2_NINDIR(m_fs);
                    733:        ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
1.1       bouyer    734:        ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
                    735:        ump->um_seqinc = 1; /* no frags */
1.75      mycroft   736:        ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
                    737:        ump->um_dirblksiz = m_fs->e2fs_bsize;
1.122     tsutsui   738:        ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
1.173     hannken   739:        spec_node_setmountedfs(devvp, mp);
1.1       bouyer    740:        return (0);
1.40      chs       741:
1.1       bouyer    742: out:
1.168     hannken   743:        if (bp != NULL)
                    744:                brelse(bp, 0);
1.1       bouyer    745:        if (ump) {
1.164     rmind     746:                kmem_free(ump->um_e2fs, sizeof(struct m_ext2fs));
                    747:                kmem_free(ump, sizeof(*ump));
1.50      soren     748:                mp->mnt_data = NULL;
1.1       bouyer    749:        }
                    750:        return (error);
                    751: }
                    752:
                    753: /*
                    754:  * unmount system call
                    755:  */
                    756: int
1.123     pooka     757: ext2fs_unmount(struct mount *mp, int mntflags)
1.1       bouyer    758: {
1.35      augustss  759:        struct ufsmount *ump;
                    760:        struct m_ext2fs *fs;
1.1       bouyer    761:        int error, flags;
                    762:
                    763:        flags = 0;
                    764:        if (mntflags & MNT_FORCE)
                    765:                flags |= FORCECLOSE;
1.123     pooka     766:        if ((error = ext2fs_flushfiles(mp, flags)) != 0)
1.1       bouyer    767:                return (error);
                    768:        ump = VFSTOUFS(mp);
                    769:        fs = ump->um_e2fs;
                    770:        if (fs->e2fs_ronly == 0 &&
                    771:                ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
                    772:                (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
                    773:                fs->e2fs.e2fs_state = E2FS_ISCLEAN;
                    774:                (void) ext2fs_sbupdate(ump, MNT_WAIT);
                    775:        }
1.29      enami     776:        if (ump->um_devvp->v_type != VBAD)
1.173     hannken   777:                spec_node_setmountedfs(ump->um_devvp, NULL);
1.28      wrstuden  778:        vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1.1       bouyer    779:        error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
1.123     pooka     780:            NOCRED);
1.28      wrstuden  781:        vput(ump->um_devvp);
1.164     rmind     782:        kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * fs->e2fs_bsize);
                    783:        kmem_free(fs, sizeof(*fs));
                    784:        kmem_free(ump, sizeof(*ump));
1.50      soren     785:        mp->mnt_data = NULL;
1.1       bouyer    786:        mp->mnt_flag &= ~MNT_LOCAL;
                    787:        return (error);
                    788: }
                    789:
                    790: /*
                    791:  * Flush out all the files in a filesystem.
                    792:  */
                    793: int
1.123     pooka     794: ext2fs_flushfiles(struct mount *mp, int flags)
1.1       bouyer    795: {
                    796:        extern int doforce;
                    797:        int error;
                    798:
                    799:        if (!doforce)
                    800:                flags &= ~FORCECLOSE;
                    801:        error = vflush(mp, NULLVP, flags);
                    802:        return (error);
                    803: }
                    804:
                    805: /*
                    806:  * Get file system statistics.
                    807:  */
                    808: int
1.123     pooka     809: ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
1.1       bouyer    810: {
1.35      augustss  811:        struct ufsmount *ump;
                    812:        struct m_ext2fs *fs;
1.122     tsutsui   813:        uint32_t overhead, overhead_per_group, ngdb;
1.32      bouyer    814:        int i, ngroups;
1.1       bouyer    815:
                    816:        ump = VFSTOUFS(mp);
                    817:        fs = ump->um_e2fs;
                    818:        if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
1.67      christos  819:                panic("ext2fs_statvfs");
1.1       bouyer    820:
                    821:        /*
                    822:         * Compute the overhead (FS structures)
                    823:         */
1.122     tsutsui   824:        overhead_per_group =
                    825:            1 /* block bitmap */ +
                    826:            1 /* inode bitmap */ +
                    827:            fs->e2fs_itpg;
1.1       bouyer    828:        overhead = fs->e2fs.e2fs_first_dblock +
1.122     tsutsui   829:            fs->e2fs_ncg * overhead_per_group;
1.32      bouyer    830:        if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
                    831:            fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
                    832:                for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
                    833:                        if (cg_has_sb(i))
                    834:                                ngroups++;
                    835:                }
                    836:        } else {
                    837:                ngroups = fs->e2fs_ncg;
                    838:        }
1.121     tsutsui   839:        ngdb = fs->e2fs_ngdb;
                    840:        if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
                    841:            fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
                    842:                ngdb += fs->e2fs.e2fs_reserved_ngdb;
1.122     tsutsui   843:        overhead += ngroups * (1 /* superblock */ + ngdb);
1.1       bouyer    844:
                    845:        sbp->f_bsize = fs->e2fs_bsize;
1.122     tsutsui   846:        sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
1.1       bouyer    847:        sbp->f_iosize = fs->e2fs_bsize;
                    848:        sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
                    849:        sbp->f_bfree = fs->e2fs.e2fs_fbcount;
1.67      christos  850:        sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
                    851:        if (sbp->f_bfree > sbp->f_bresvd)
                    852:                sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
                    853:        else
                    854:                sbp->f_bavail = 0;
1.1       bouyer    855:        sbp->f_files =  fs->e2fs.e2fs_icount;
                    856:        sbp->f_ffree = fs->e2fs.e2fs_ficount;
1.67      christos  857:        sbp->f_favail = fs->e2fs.e2fs_ficount;
                    858:        sbp->f_fresvd = 0;
                    859:        copy_statvfs_info(sbp, mp);
1.1       bouyer    860:        return (0);
                    861: }
                    862:
1.182     christos  863: static bool
                    864: ext2fs_sync_selector(void *cl, struct vnode *vp)
                    865: {
                    866:        struct inode *ip;
                    867:
                    868:        ip = VTOI(vp);
                    869:        /*
                    870:         * Skip the vnode/inode if inaccessible.
                    871:         */
                    872:        if (ip == NULL || vp->v_type == VNON)
                    873:                return false;
                    874:
                    875:        if (((ip->i_flag &
                    876:              (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
                    877:             LIST_EMPTY(&vp->v_dirtyblkhd) &&
                    878:             UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
                    879:                return false;
                    880:        return true;
                    881: }
                    882:
1.1       bouyer    883: /*
                    884:  * Go through the disk queues to initiate sandbagged IO;
                    885:  * go through the inodes to write those that have been modified;
                    886:  * initiate the writing of the super block if it has been modified.
                    887:  *
                    888:  * Note: we are always called with the filesystem marked `MPBUSY'.
                    889:  */
                    890: int
1.123     pooka     891: ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1.1       bouyer    892: {
1.178     hannken   893:        struct vnode *vp;
1.9       fvdl      894:        struct ufsmount *ump = VFSTOUFS(mp);
                    895:        struct m_ext2fs *fs;
1.178     hannken   896:        struct vnode_iterator *marker;
1.1       bouyer    897:        int error, allerror = 0;
                    898:
                    899:        fs = ump->um_e2fs;
1.36      mycroft   900:        if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) {        /* XXX */
1.9       fvdl      901:                printf("fs = %s\n", fs->e2fs_fsmnt);
                    902:                panic("update: rofs mod");
1.1       bouyer    903:        }
1.126     ad        904:
1.1       bouyer    905:        /*
                    906:         * Write back each (modified) inode.
                    907:         */
1.178     hannken   908:        vfs_vnode_iterator_init(mp, &marker);
1.182     christos  909:        while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
                    910:            NULL)))
                    911:        {
1.178     hannken   912:                error = vn_lock(vp, LK_EXCLUSIVE);
                    913:                if (error) {
                    914:                        vrele(vp);
1.126     ad        915:                        continue;
1.178     hannken   916:                }
1.74      mycroft   917:                if (vp->v_type == VREG && waitfor == MNT_LAZY)
1.93      yamt      918:                        error = ext2fs_update(vp, NULL, NULL, 0);
1.74      mycroft   919:                else
                    920:                        error = VOP_FSYNC(vp, cred,
1.123     pooka     921:                            waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
1.74      mycroft   922:                if (error)
1.1       bouyer    923:                        allerror = error;
1.22      bouyer    924:                vput(vp);
1.1       bouyer    925:        }
1.178     hannken   926:        vfs_vnode_iterator_destroy(marker);
1.1       bouyer    927:        /*
                    928:         * Force stale file system control information to be flushed.
                    929:         */
1.36      mycroft   930:        if (waitfor != MNT_LAZY) {
                    931:                vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
                    932:                if ((error = VOP_FSYNC(ump->um_devvp, cred,
1.123     pooka     933:                    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
1.36      mycroft   934:                        allerror = error;
1.157     hannken   935:                VOP_UNLOCK(ump->um_devvp);
1.36      mycroft   936:        }
1.9       fvdl      937:        /*
                    938:         * Write back modified superblock.
                    939:         */
                    940:        if (fs->e2fs_fmod != 0) {
                    941:                fs->e2fs_fmod = 0;
1.98      kardel    942:                fs->e2fs.e2fs_wtime = time_second;
1.9       fvdl      943:                if ((error = ext2fs_cgupdate(ump, waitfor)))
                    944:                        allerror = error;
                    945:        }
1.1       bouyer    946:        return (allerror);
                    947: }
                    948:
                    949: /*
1.181     hannken   950:  * Read an inode from disk and initialize this vnode / inode pair.
                    951:  * Caller assures no other thread will try to load this inode.
1.1       bouyer    952:  */
                    953: int
1.181     hannken   954: ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
                    955:     const void *key, size_t key_len, const void **new_key)
1.1       bouyer    956: {
1.181     hannken   957:        ino_t ino;
1.9       fvdl      958:        struct m_ext2fs *fs;
                    959:        struct inode *ip;
1.1       bouyer    960:        struct ufsmount *ump;
                    961:        struct buf *bp;
                    962:        dev_t dev;
                    963:        int error;
1.109     christos  964:        void *cp;
1.1       bouyer    965:
1.181     hannken   966:        KASSERT(key_len == sizeof(ino));
                    967:        memcpy(&ino, key, key_len);
1.1       bouyer    968:        ump = VFSTOUFS(mp);
                    969:        dev = ump->um_dev;
1.181     hannken   970:        fs = ump->um_e2fs;
1.1       bouyer    971:
1.181     hannken   972:        /* Read in the disk contents for the inode, copy into the inode. */
                    973:        error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
                    974:            (int)fs->e2fs_bsize, NOCRED, 0, &bp);
                    975:        if (error)
                    976:                return error;
                    977:
                    978:        /* Allocate and initialize inode. */
1.108     ad        979:        ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
1.181     hannken   980:        memset(ip, 0, sizeof(struct inode));
                    981:        vp->v_tag = VT_EXT2FS;
                    982:        vp->v_op = ext2fs_vnodeop_p;
1.119     ad        983:        vp->v_vflag |= VV_LOCKSWORK;
1.1       bouyer    984:        vp->v_data = ip;
                    985:        ip->i_vnode = vp;
1.55      fvdl      986:        ip->i_ump = ump;
1.181     hannken   987:        ip->i_e2fs = fs;
1.1       bouyer    988:        ip->i_dev = dev;
                    989:        ip->i_number = ino;
                    990:        ip->i_e2fs_last_lblk = 0;
                    991:        ip->i_e2fs_last_blk = 0;
1.181     hannken   992:
                    993:        /* Initialize genfs node. */
1.131     ad        994:        genfs_node_init(vp, &ext2fs_genfsops);
1.1       bouyer    995:
1.142     christos  996:        cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
1.56      fvdl      997:        ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
1.55      fvdl      998:        e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
1.140     mrg       999:        ext2fs_set_inode_guid(ip);
1.118     ad       1000:        brelse(bp, 0);
1.1       bouyer   1001:
1.5       bouyer   1002:        /* If the inode was deleted, reset all fields */
                   1003:        if (ip->i_e2fs_dtime != 0) {
1.167     jakllsch 1004:                ip->i_e2fs_mode = 0;
1.82      ws       1005:                (void)ext2fs_setsize(ip, 0);
1.167     jakllsch 1006:                (void)ext2fs_setnblock(ip, 0);
1.16      perry    1007:                memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
1.5       bouyer   1008:        }
                   1009:
1.181     hannken  1010:        /* Initialize the vnode from the inode. */
                   1011:        ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1.45      chs      1012:
1.181     hannken  1013:        /* Finish inode initialization. */
1.1       bouyer   1014:        ip->i_devvp = ump->um_devvp;
1.153     pooka    1015:        vref(ip->i_devvp);
1.45      chs      1016:
1.1       bouyer   1017:        /*
                   1018:         * Set up a generation number for this inode if it does not
                   1019:         * already have one. This should only happen on old filesystems.
                   1020:         */
1.45      chs      1021:
1.1       bouyer   1022:        if (ip->i_e2fs_gen == 0) {
1.98      kardel   1023:                if (++ext2gennumber < (u_long)time_second)
                   1024:                        ext2gennumber = time_second;
1.1       bouyer   1025:                ip->i_e2fs_gen = ext2gennumber;
1.183.2.1  martin   1026:                if ((mp->mnt_flag & MNT_RDONLY) == 0)
1.1       bouyer   1027:                        ip->i_flag |= IN_MODIFIED;
                   1028:        }
1.111     yamt     1029:        uvm_vnp_setsize(vp, ext2fs_size(ip));
1.181     hannken  1030:        *new_key = &ip->i_number;
                   1031:        return 0;
1.1       bouyer   1032: }
                   1033:
                   1034: /*
                   1035:  * File handle to vnode
                   1036:  *
                   1037:  * Have to be really careful about stale file handles:
                   1038:  * - check that the inode number is valid
                   1039:  * - call ext2fs_vget() to get the locked inode
                   1040:  * - check for an unallocated inode (i_mode == 0)
                   1041:  */
                   1042: int
1.89      xtraeme  1043: ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1.1       bouyer   1044: {
1.35      augustss 1045:        struct inode *ip;
1.24      wrstuden 1046:        struct vnode *nvp;
                   1047:        int error;
1.99      martin   1048:        struct ufid ufh;
1.1       bouyer   1049:        struct m_ext2fs *fs;
                   1050:
1.99      martin   1051:        if (fhp->fid_len != sizeof(struct ufid))
                   1052:                return EINVAL;
                   1053:
                   1054:        memcpy(&ufh, fhp, sizeof(struct ufid));
1.1       bouyer   1055:        fs = VFSTOUFS(mp)->um_e2fs;
1.99      martin   1056:        if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
                   1057:                ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1.1       bouyer   1058:                return (ESTALE);
1.24      wrstuden 1059:
1.99      martin   1060:        if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
1.24      wrstuden 1061:                *vpp = NULLVP;
                   1062:                return (error);
                   1063:        }
                   1064:        ip = VTOI(nvp);
1.83      perry    1065:        if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1.99      martin   1066:                ip->i_e2fs_gen != ufh.ufid_gen) {
1.24      wrstuden 1067:                vput(nvp);
                   1068:                *vpp = NULLVP;
                   1069:                return (ESTALE);
                   1070:        }
                   1071:        *vpp = nvp;
                   1072:        return (0);
1.1       bouyer   1073: }
                   1074:
                   1075: /*
                   1076:  * Vnode pointer to File handle
                   1077:  */
                   1078: /* ARGSUSED */
                   1079: int
1.99      martin   1080: ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1.1       bouyer   1081: {
1.35      augustss 1082:        struct inode *ip;
1.99      martin   1083:        struct ufid ufh;
                   1084:
                   1085:        if (*fh_size < sizeof(struct ufid)) {
                   1086:                *fh_size = sizeof(struct ufid);
                   1087:                return E2BIG;
                   1088:        }
                   1089:        *fh_size = sizeof(struct ufid);
1.1       bouyer   1090:
                   1091:        ip = VTOI(vp);
1.99      martin   1092:        memset(&ufh, 0, sizeof(ufh));
                   1093:        ufh.ufid_len = sizeof(struct ufid);
                   1094:        ufh.ufid_ino = ip->i_number;
                   1095:        ufh.ufid_gen = ip->i_e2fs_gen;
                   1096:        memcpy(fhp, &ufh, sizeof(ufh));
1.1       bouyer   1097:        return (0);
1.9       fvdl     1098: }
                   1099:
1.1       bouyer   1100: /*
                   1101:  * Write a superblock and associated information back to disk.
                   1102:  */
                   1103: int
1.89      xtraeme  1104: ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1.1       bouyer   1105: {
1.35      augustss 1106:        struct m_ext2fs *fs = mp->um_e2fs;
                   1107:        struct buf *bp;
1.1       bouyer   1108:        int error = 0;
                   1109:
                   1110:        bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1.4       bouyer   1111:        e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1.1       bouyer   1112:        if (waitfor == MNT_WAIT)
                   1113:                error = bwrite(bp);
                   1114:        else
                   1115:                bawrite(bp);
                   1116:        return (error);
                   1117: }
                   1118:
                   1119: int
1.89      xtraeme  1120: ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1.1       bouyer   1121: {
1.35      augustss 1122:        struct m_ext2fs *fs = mp->um_e2fs;
                   1123:        struct buf *bp;
1.1       bouyer   1124:        int i, error = 0, allerror = 0;
                   1125:
                   1126:        allerror = ext2fs_sbupdate(mp, waitfor);
                   1127:        for (i = 0; i < fs->e2fs_ngdb; i++) {
1.171     dholland 1128:                bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
1.124     tsutsui  1129:                    fs->e2fs.e2fs_first_dblock +
                   1130:                    1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1.122     tsutsui  1131:                e2fs_cgsave(&fs->e2fs_gd[
                   1132:                    i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
                   1133:                    (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1.1       bouyer   1134:                if (waitfor == MNT_WAIT)
                   1135:                        error = bwrite(bp);
                   1136:                else
                   1137:                        bawrite(bp);
                   1138:        }
                   1139:
                   1140:        if (!allerror && error)
                   1141:                allerror = error;
                   1142:        return (allerror);
1.31      bouyer   1143: }
                   1144:
                   1145: static int
1.89      xtraeme  1146: ext2fs_checksb(struct ext2fs *fs, int ronly)
1.31      bouyer   1147: {
1.165     chs      1148:        uint32_t u32;
1.122     tsutsui  1149:
1.31      bouyer   1150:        if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
1.80      mycroft  1151:                return (EINVAL);                /* XXX needs translation */
1.31      bouyer   1152:        }
                   1153:        if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
                   1154: #ifdef DIAGNOSTIC
1.165     chs      1155:                printf("ext2fs: unsupported revision number: %x\n",
1.122     tsutsui  1156:                    fs2h32(fs->e2fs_rev));
1.31      bouyer   1157: #endif
1.80      mycroft  1158:                return (EINVAL);                /* XXX needs translation */
1.31      bouyer   1159:        }
                   1160:        if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
                   1161: #ifdef DIAGNOSTIC
1.165     chs      1162:                printf("ext2fs: bad block size: %d "
1.122     tsutsui  1163:                    "(expected <= 2 for ext2 fs)\n",
                   1164:                    fs2h32(fs->e2fs_log_bsize));
1.31      bouyer   1165: #endif
1.80      mycroft  1166:                return (EINVAL);           /* XXX needs translation */
1.31      bouyer   1167:        }
                   1168:        if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
1.166     christos 1169:                char buf[256];
1.142     christos 1170:                if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
1.165     chs      1171:                        printf("ext2fs: unsupported first inode position\n");
1.33      bouyer   1172:                        return (EINVAL);      /* XXX needs translation */
                   1173:                }
1.165     chs      1174:                u32 = fs2h32(fs->e2fs_features_incompat) & ~EXT2F_INCOMPAT_SUPP;
                   1175:                if (u32) {
1.166     christos 1176:                        snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
                   1177:                        printf("ext2fs: unsupported incompat features: %s\n",
                   1178:                            buf);
                   1179:                        return EINVAL;  /* XXX needs translation */
1.31      bouyer   1180:                }
1.165     chs      1181:                u32 = fs2h32(fs->e2fs_features_rocompat) & ~EXT2F_ROCOMPAT_SUPP;
                   1182:                if (!ronly && u32) {
1.166     christos 1183:                        snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
                   1184:                        printf("ext2fs: unsupported ro-incompat features: %s\n",
                   1185:                            buf);
                   1186:                        return EROFS;   /* XXX needs translation */
1.31      bouyer   1187:                }
                   1188:        }
                   1189:        return (0);
1.1       bouyer   1190: }

CVSweb <webmaster@jp.NetBSD.org>