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

Annotation of src/sys/ufs/ffs/ffs_vfsops.c, Revision 1.199

1.199   ! hannken     1: /*     $NetBSD: ffs_vfsops.c,v 1.198 2007/04/07 14:21:52 hannken Exp $ */
1.4       cgd         2:
1.1       mycroft     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
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.119     agc        15:  * 3. Neither the name of the University nor the names of its contributors
1.1       mycroft    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:  *
1.33      fvdl       31:  *     @(#)ffs_vfsops.c        8.31 (Berkeley) 5/20/95
1.1       mycroft    32:  */
1.88      lukem      33:
                     34: #include <sys/cdefs.h>
1.199   ! hannken    35: __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.198 2007/04/07 14:21:52 hannken Exp $");
1.36      scottr     36:
1.81      mrg        37: #if defined(_KERNEL_OPT)
1.45      thorpej    38: #include "opt_ffs.h"
1.36      scottr     39: #include "opt_quota.h"
1.66      matt       40: #include "opt_softdep.h"
1.37      scottr     41: #endif
1.1       mycroft    42:
                     43: #include <sys/param.h>
                     44: #include <sys/systm.h>
                     45: #include <sys/namei.h>
                     46: #include <sys/proc.h>
                     47: #include <sys/kernel.h>
                     48: #include <sys/vnode.h>
                     49: #include <sys/socket.h>
                     50: #include <sys/mount.h>
                     51: #include <sys/buf.h>
1.23      thorpej    52: #include <sys/device.h>
1.1       mycroft    53: #include <sys/mbuf.h>
                     54: #include <sys/file.h>
                     55: #include <sys/disklabel.h>
                     56: #include <sys/ioctl.h>
                     57: #include <sys/errno.h>
                     58: #include <sys/malloc.h>
1.43      thorpej    59: #include <sys/pool.h>
1.29      fvdl       60: #include <sys/lock.h>
1.33      fvdl       61: #include <sys/sysctl.h>
1.101     gehenna    62: #include <sys/conf.h>
1.181     elad       63: #include <sys/kauth.h>
1.193     hannken    64: #include <sys/fstrans.h>
1.1       mycroft    65:
                     66: #include <miscfs/specfs/specdev.h>
                     67:
                     68: #include <ufs/ufs/quota.h>
                     69: #include <ufs/ufs/ufsmount.h>
                     70: #include <ufs/ufs/inode.h>
1.25      bouyer     71: #include <ufs/ufs/dir.h>
1.1       mycroft    72: #include <ufs/ufs/ufs_extern.h>
1.34      bouyer     73: #include <ufs/ufs/ufs_bswap.h>
1.1       mycroft    74:
                     75: #include <ufs/ffs/fs.h>
                     76: #include <ufs/ffs/ffs_extern.h>
                     77:
1.59      jdolecek   78: /* how many times ffs_init() was called */
                     79: int ffs_initcount = 0;
                     80:
1.195     ad         81: extern kmutex_t ufs_hashlock;
1.29      fvdl       82:
1.105     matt       83: extern const struct vnodeopv_desc ffs_vnodeop_opv_desc;
                     84: extern const struct vnodeopv_desc ffs_specop_opv_desc;
                     85: extern const struct vnodeopv_desc ffs_fifoop_opv_desc;
1.32      thorpej    86:
1.79      jdolecek   87: const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = {
1.32      thorpej    88:        &ffs_vnodeop_opv_desc,
                     89:        &ffs_specop_opv_desc,
                     90:        &ffs_fifoop_opv_desc,
                     91:        NULL,
                     92: };
                     93:
1.17      mycroft    94: struct vfsops ffs_vfsops = {
                     95:        MOUNT_FFS,
1.1       mycroft    96:        ffs_mount,
                     97:        ufs_start,
                     98:        ffs_unmount,
                     99:        ufs_root,
                    100:        ufs_quotactl,
1.143     christos  101:        ffs_statvfs,
1.1       mycroft   102:        ffs_sync,
                    103:        ffs_vget,
                    104:        ffs_fhtovp,
                    105:        ffs_vptofh,
                    106:        ffs_init,
1.86      chs       107:        ffs_reinit,
1.59      jdolecek  108:        ffs_done,
1.23      thorpej   109:        ffs_mountroot,
1.149     hannken   110:        ffs_snapshot,
1.170     thorpej   111:        ffs_extattrctl,
1.193     hannken   112:        ffs_suspendctl,
1.32      thorpej   113:        ffs_vnodeopv_descs,
1.185     christos  114:        0,
                    115:        { NULL, NULL },
1.1       mycroft   116: };
1.163     thorpej   117: VFS_ATTACH(ffs_vfsops);
1.1       mycroft   118:
1.165     yamt      119: static const struct genfs_ops ffs_genfsops = {
                    120:        .gop_size = ffs_gop_size,
                    121:        .gop_alloc = ufs_gop_alloc,
                    122:        .gop_write = genfs_gop_write,
1.167     yamt      123:        .gop_markupdate = ufs_gop_markupdate,
1.87      chs       124: };
                    125:
1.175     yamt      126: static const struct ufs_ops ffs_ufsops = {
                    127:        .uo_itimes = ffs_itimes,
1.176     yamt      128:        .uo_update = ffs_update,
                    129:        .uo_truncate = ffs_truncate,
                    130:        .uo_valloc = ffs_valloc,
                    131:        .uo_vfree = ffs_vfree,
                    132:        .uo_balloc = ffs_balloc,
1.175     yamt      133: };
                    134:
1.144     simonb    135: POOL_INIT(ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1.197     ad        136:     &pool_allocator_nointr, IPL_NONE);
1.144     simonb    137: POOL_INIT(ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "dino1pl",
1.197     ad        138:     &pool_allocator_nointr, IPL_NONE);
1.144     simonb    139: POOL_INIT(ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0, "dino2pl",
1.197     ad        140:     &pool_allocator_nointr, IPL_NONE);
1.110     fvdl      141:
1.146     simonb    142: static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t);
1.110     fvdl      143: static void ffs_oldfscompat_write(struct fs *, struct ufsmount *);
1.43      thorpej   144:
1.1       mycroft   145: /*
1.33      fvdl      146:  * Called by main() when ffs is going to be mounted as root.
1.1       mycroft   147:  */
                    148:
1.19      christos  149: int
1.166     thorpej   150: ffs_mountroot(void)
1.1       mycroft   151: {
1.33      fvdl      152:        struct fs *fs;
                    153:        struct mount *mp;
1.177     christos  154:        struct lwp *l = curlwp;                 /* XXX */
1.1       mycroft   155:        struct ufsmount *ump;
                    156:        int error;
1.23      thorpej   157:
1.180     thorpej   158:        if (device_class(root_device) != DV_DISK)
1.23      thorpej   159:                return (ENODEV);
                    160:
1.51      wrstuden  161:        if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
                    162:                vrele(rootvp);
1.33      fvdl      163:                return (error);
1.51      wrstuden  164:        }
1.177     christos  165:        if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
1.33      fvdl      166:                mp->mnt_op->vfs_refcount--;
                    167:                vfs_unbusy(mp);
1.1       mycroft   168:                free(mp, M_MOUNT);
                    169:                return (error);
                    170:        }
1.33      fvdl      171:        simple_lock(&mountlist_slock);
1.11      mycroft   172:        CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1.33      fvdl      173:        simple_unlock(&mountlist_slock);
1.1       mycroft   174:        ump = VFSTOUFS(mp);
                    175:        fs = ump->um_fs;
1.42      perry     176:        memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
1.33      fvdl      177:        (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
1.177     christos  178:        (void)ffs_statvfs(mp, &mp->mnt_stat, l);
1.33      fvdl      179:        vfs_unbusy(mp);
1.151     pk        180:        setrootfstime((time_t)fs->fs_time);
1.1       mycroft   181:        return (0);
                    182: }
                    183:
                    184: /*
                    185:  * VFS Operations.
                    186:  *
                    187:  * mount system call
                    188:  */
                    189: int
1.166     thorpej   190: ffs_mount(struct mount *mp, const char *path, void *data,
1.177     christos  191:     struct nameidata *ndp, struct lwp *l)
1.1       mycroft   192: {
1.104     scw       193:        struct vnode *devvp = NULL;
1.1       mycroft   194:        struct ufs_args args;
1.19      christos  195:        struct ufsmount *ump = NULL;
1.61      augustss  196:        struct fs *fs;
1.95      christos  197:        int error, flags, update;
1.9       mycroft   198:        mode_t accessmode;
1.1       mycroft   199:
1.102     christos  200:        if (mp->mnt_flag & MNT_GETARGS) {
                    201:                ump = VFSTOUFS(mp);
                    202:                if (ump == NULL)
                    203:                        return EIO;
                    204:                args.fspec = NULL;
                    205:                return copyout(&args, data, sizeof(args));
                    206:        }
1.108     dsl       207:        error = copyin(data, &args, sizeof (struct ufs_args));
1.19      christos  208:        if (error)
1.1       mycroft   209:                return (error);
1.66      matt      210:
                    211: #if !defined(SOFTDEP)
1.67      perseant  212:        mp->mnt_flag &= ~MNT_SOFTDEP;
1.66      matt      213: #endif
                    214:
1.95      christos  215:        update = mp->mnt_flag & MNT_UPDATE;
                    216:
                    217:        /* Check arguments */
1.97      enami     218:        if (args.fspec != NULL) {
1.95      christos  219:                /*
                    220:                 * Look up the name and verify that it's sane.
                    221:                 */
1.177     christos  222:                NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
1.95      christos  223:                if ((error = namei(ndp)) != 0)
                    224:                        return (error);
                    225:                devvp = ndp->ni_vp;
                    226:
                    227:                if (!update) {
                    228:                        /*
                    229:                         * Be sure this is a valid block device
                    230:                         */
                    231:                        if (devvp->v_type != VBLK)
                    232:                                error = ENOTBLK;
1.101     gehenna   233:                        else if (bdevsw_lookup(devvp->v_rdev) == NULL)
1.95      christos  234:                                error = ENXIO;
                    235:                } else {
                    236:                        /*
                    237:                         * Be sure we're still naming the same device
                    238:                         * used for our initial mount
                    239:                         */
1.160     mycroft   240:                        ump = VFSTOUFS(mp);
1.186     jld       241:                        if (devvp != ump->um_devvp) {
                    242:                                if (devvp->v_rdev != ump->um_devvp->v_rdev)
                    243:                                        error = EINVAL;
                    244:                                else {
                    245:                                        vrele(devvp);
                    246:                                        devvp = ump->um_devvp;
                    247:                                        vref(devvp);
                    248:                                }
                    249:                        }
1.95      christos  250:                }
1.160     mycroft   251:        } else {
                    252:                if (!update) {
                    253:                        /* New mounts must have a filename for the device */
                    254:                        return (EINVAL);
                    255:                } else {
                    256:                        /* Use the extant mount */
                    257:                        ump = VFSTOUFS(mp);
                    258:                        devvp = ump->um_devvp;
                    259:                        vref(devvp);
                    260:                }
1.95      christos  261:        }
                    262:
1.1       mycroft   263:        /*
1.95      christos  264:         * If mount by non-root, then verify that user has necessary
                    265:         * permissions on the device.
1.1       mycroft   266:         */
1.191     elad      267:        if (error == 0 && kauth_authorize_generic(l->l_cred,
                    268:            KAUTH_GENERIC_ISSUSER, NULL) != 0) {
1.95      christos  269:                accessmode = VREAD;
1.96      christos  270:                if (update ?
1.124     dbj       271:                    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
1.96      christos  272:                    (mp->mnt_flag & MNT_RDONLY) == 0)
1.95      christos  273:                        accessmode |= VWRITE;
                    274:                vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.184     ad        275:                error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
1.95      christos  276:                VOP_UNLOCK(devvp, 0);
                    277:        }
                    278:
                    279:        if (error) {
                    280:                vrele(devvp);
                    281:                return (error);
                    282:        }
                    283:
                    284:        if (!update) {
1.164     christos  285:                int xflags;
1.159     mycroft   286:
                    287:                /*
                    288:                 * Disallow multiple mounts of the same device.
                    289:                 * Disallow mounting of a device that is currently in use
                    290:                 * (except for root, which might share swap device for
                    291:                 * miniroot).
                    292:                 */
                    293:                error = vfs_mountedon(devvp);
                    294:                if (error)
                    295:                        goto fail;
                    296:                if (vcount(devvp) > 1 && devvp != rootvp) {
                    297:                        error = EBUSY;
                    298:                        goto fail;
                    299:                }
                    300:                if (mp->mnt_flag & MNT_RDONLY)
1.164     christos  301:                        xflags = FREAD;
1.159     mycroft   302:                else
1.164     christos  303:                        xflags = FREAD|FWRITE;
1.177     christos  304:                error = VOP_OPEN(devvp, xflags, FSCRED, l);
1.159     mycroft   305:                if (error)
                    306:                        goto fail;
1.177     christos  307:                error = ffs_mountfs(devvp, mp, l);
1.95      christos  308:                if (error) {
1.159     mycroft   309:                        vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.177     christos  310:                        (void)VOP_CLOSE(devvp, xflags, NOCRED, l);
1.159     mycroft   311:                        VOP_UNLOCK(devvp, 0);
                    312:                        goto fail;
1.95      christos  313:                }
                    314:
1.1       mycroft   315:                ump = VFSTOUFS(mp);
                    316:                fs = ump->um_fs;
1.95      christos  317:                if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
                    318:                    (MNT_SOFTDEP | MNT_ASYNC)) {
                    319:                        printf("%s fs uses soft updates, "
1.96      christos  320:                            "ignoring async mode\n",
                    321:                            fs->fs_fsmnt);
1.95      christos  322:                        mp->mnt_flag &= ~MNT_ASYNC;
                    323:                }
                    324:        } else {
                    325:                /*
1.96      christos  326:                 * Update the mount.
                    327:                 */
                    328:
                    329:                /*
                    330:                 * The initial mount got a reference on this
                    331:                 * device, so drop the one obtained via
                    332:                 * namei(), above.
1.95      christos  333:                 */
1.96      christos  334:                vrele(devvp);
                    335:
1.160     mycroft   336:                ump = VFSTOUFS(mp);
1.95      christos  337:                fs = ump->um_fs;
1.1       mycroft   338:                if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
1.95      christos  339:                        /*
                    340:                         * Changing from r/w to r/o
                    341:                         */
1.1       mycroft   342:                        flags = WRITECLOSE;
                    343:                        if (mp->mnt_flag & MNT_FORCE)
                    344:                                flags |= FORCECLOSE;
1.55      fvdl      345:                        if (mp->mnt_flag & MNT_SOFTDEP)
1.177     christos  346:                                error = softdep_flushfiles(mp, flags, l);
1.55      fvdl      347:                        else
1.177     christos  348:                                error = ffs_flushfiles(mp, flags, l);
1.89      fvdl      349:                        if (fs->fs_pendingblocks != 0 ||
                    350:                            fs->fs_pendinginodes != 0) {
1.110     fvdl      351:                                printf("%s: update error: blocks %" PRId64
                    352:                                       " files %d\n",
1.89      fvdl      353:                                    fs->fs_fsmnt, fs->fs_pendingblocks,
                    354:                                    fs->fs_pendinginodes);
                    355:                                fs->fs_pendingblocks = 0;
                    356:                                fs->fs_pendinginodes = 0;
                    357:                        }
1.15      mycroft   358:                        if (error == 0 &&
                    359:                            ffs_cgupdate(ump, MNT_WAIT) == 0 &&
                    360:                            fs->fs_clean & FS_WASCLEAN) {
1.65      fvdl      361:                                if (mp->mnt_flag & MNT_SOFTDEP)
                    362:                                        fs->fs_flags &= ~FS_DOSOFTDEP;
1.15      mycroft   363:                                fs->fs_clean = FS_ISCLEAN;
                    364:                                (void) ffs_sbupdate(ump, MNT_WAIT);
                    365:                        }
                    366:                        if (error)
                    367:                                return (error);
                    368:                        fs->fs_ronly = 1;
1.78      mycroft   369:                        fs->fs_fmod = 0;
1.1       mycroft   370:                }
1.65      fvdl      371:
                    372:                /*
                    373:                 * Flush soft dependencies if disabling it via an update
                    374:                 * mount. This may leave some items to be processed,
                    375:                 * so don't do this yet XXX.
                    376:                 */
                    377:                if ((fs->fs_flags & FS_DOSOFTDEP) &&
                    378:                    !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
                    379: #ifdef notyet
                    380:                        flags = WRITECLOSE;
                    381:                        if (mp->mnt_flag & MNT_FORCE)
                    382:                                flags |= FORCECLOSE;
1.177     christos  383:                        error = softdep_flushfiles(mp, flags, l);
1.65      fvdl      384:                        if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
                    385:                                fs->fs_flags &= ~FS_DOSOFTDEP;
                    386:                                (void) ffs_sbupdate(ump, MNT_WAIT);
1.66      matt      387: #elif defined(SOFTDEP)
1.65      fvdl      388:                        mp->mnt_flag |= MNT_SOFTDEP;
                    389: #endif
                    390:                }
                    391:
                    392:                /*
                    393:                 * When upgrading to a softdep mount, we must first flush
                    394:                 * all vnodes. (not done yet -- see above)
                    395:                 */
                    396:                if (!(fs->fs_flags & FS_DOSOFTDEP) &&
                    397:                    (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
                    398: #ifdef notyet
                    399:                        flags = WRITECLOSE;
                    400:                        if (mp->mnt_flag & MNT_FORCE)
                    401:                                flags |= FORCECLOSE;
1.177     christos  402:                        error = ffs_flushfiles(mp, flags, l);
1.65      fvdl      403: #else
                    404:                        mp->mnt_flag &= ~MNT_SOFTDEP;
                    405: #endif
                    406:                }
                    407:
1.15      mycroft   408:                if (mp->mnt_flag & MNT_RELOAD) {
1.184     ad        409:                        error = ffs_reload(mp, l->l_cred, l);
1.15      mycroft   410:                        if (error)
                    411:                                return (error);
                    412:                }
1.95      christos  413:
1.124     dbj       414:                if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
1.9       mycroft   415:                        /*
1.95      christos  416:                         * Changing from read-only to read/write
1.9       mycroft   417:                         */
1.1       mycroft   418:                        fs->fs_ronly = 0;
1.15      mycroft   419:                        fs->fs_clean <<= 1;
                    420:                        fs->fs_fmod = 1;
1.55      fvdl      421:                        if ((fs->fs_flags & FS_DOSOFTDEP)) {
                    422:                                error = softdep_mount(devvp, mp, fs,
1.184     ad        423:                                    l->l_cred);
1.55      fvdl      424:                                if (error)
                    425:                                        return (error);
1.65      fvdl      426:                        }
1.149     hannken   427:                        if (fs->fs_snapinum[0] != 0)
                    428:                                ffs_snapshot_mount(mp);
1.9       mycroft   429:                }
1.174     jmmv      430:                if (args.fspec == NULL)
                    431:                        return EINVAL;
1.55      fvdl      432:                if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
                    433:                    (MNT_SOFTDEP | MNT_ASYNC)) {
                    434:                        printf("%s fs uses soft updates, ignoring async mode\n",
                    435:                            fs->fs_fsmnt);
                    436:                        mp->mnt_flag &= ~MNT_ASYNC;
                    437:                }
1.1       mycroft   438:        }
                    439:
1.143     christos  440:        error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
1.177     christos  441:            UIO_USERSPACE, mp, l);
1.114     christos  442:        if (error == 0)
                    443:                (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
                    444:                    sizeof(fs->fs_fsmnt));
1.65      fvdl      445:        if (mp->mnt_flag & MNT_SOFTDEP)
                    446:                fs->fs_flags |= FS_DOSOFTDEP;
1.74      fvdl      447:        else
                    448:                fs->fs_flags &= ~FS_DOSOFTDEP;
1.15      mycroft   449:        if (fs->fs_fmod != 0) { /* XXX */
                    450:                fs->fs_fmod = 0;
                    451:                if (fs->fs_clean & FS_WASCLEAN)
1.182     kardel    452:                        fs->fs_time = time_second;
1.89      fvdl      453:                else {
1.82      lukem     454:                        printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
1.34      bouyer    455:                            mp->mnt_stat.f_mntfromname, fs->fs_clean);
1.110     fvdl      456:                        printf("%s: lost blocks %" PRId64 " files %d\n",
1.89      fvdl      457:                            mp->mnt_stat.f_mntfromname, fs->fs_pendingblocks,
                    458:                            fs->fs_pendinginodes);
                    459:                }
1.15      mycroft   460:                (void) ffs_cgupdate(ump, MNT_WAIT);
                    461:        }
1.159     mycroft   462:        return (error);
                    463:
                    464: fail:
                    465:        vrele(devvp);
                    466:        return (error);
1.1       mycroft   467: }
                    468:
                    469: /*
                    470:  * Reload all incore data for a filesystem (used after running fsck on
                    471:  * the root filesystem and finding things to fix). The filesystem must
                    472:  * be mounted read-only.
                    473:  *
                    474:  * Things to do to update the mount:
                    475:  *     1) invalidate all cached meta-data.
                    476:  *     2) re-read superblock from disk.
                    477:  *     3) re-read summary information from disk.
                    478:  *     4) invalidate all inactive vnodes.
                    479:  *     5) invalidate all cached file data.
                    480:  *     6) re-read inode data for all active vnodes.
                    481:  */
1.19      christos  482: int
1.181     elad      483: ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
1.1       mycroft   484: {
1.189     reinoud   485:        struct vnode *vp, *nvp, *devvp;
1.1       mycroft   486:        struct inode *ip;
1.84      lukem     487:        void *space;
1.1       mycroft   488:        struct buf *bp;
1.18      cgd       489:        struct fs *fs, *newfs;
1.1       mycroft   490:        struct partinfo dpart;
                    491:        int i, blks, size, error;
1.18      cgd       492:        int32_t *lp;
1.111     fvdl      493:        struct ufsmount *ump;
1.141     dbj       494:        daddr_t sblockloc;
1.1       mycroft   495:
1.153     mycroft   496:        if ((mp->mnt_flag & MNT_RDONLY) == 0)
1.1       mycroft   497:                return (EINVAL);
1.111     fvdl      498:
1.153     mycroft   499:        ump = VFSTOUFS(mp);
1.1       mycroft   500:        /*
                    501:         * Step 1: invalidate all cached meta-data.
                    502:         */
1.111     fvdl      503:        devvp = ump->um_devvp;
1.55      fvdl      504:        vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.177     christos  505:        error = vinvalbuf(devvp, 0, cred, l, 0, 0);
1.55      fvdl      506:        VOP_UNLOCK(devvp, 0);
                    507:        if (error)
1.1       mycroft   508:                panic("ffs_reload: dirty1");
                    509:        /*
                    510:         * Step 2: re-read superblock from disk.
                    511:         */
1.111     fvdl      512:        fs = ump->um_fs;
1.177     christos  513:        if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, l) != 0)
1.1       mycroft   514:                size = DEV_BSIZE;
                    515:        else
                    516:                size = dpart.disklab->d_secsize;
1.141     dbj       517:        /* XXX we don't handle possibility that superblock moved. */
1.110     fvdl      518:        error = bread(devvp, fs->fs_sblockloc / size, fs->fs_sbsize,
                    519:                      NOCRED, &bp);
1.47      bouyer    520:        if (error) {
                    521:                brelse(bp);
1.1       mycroft   522:                return (error);
1.47      bouyer    523:        }
1.34      bouyer    524:        newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
1.42      perry     525:        memcpy(newfs, bp->b_data, fs->fs_sbsize);
1.34      bouyer    526: #ifdef FFS_EI
1.111     fvdl      527:        if (ump->um_flags & UFS_NEEDSWAP) {
1.83      lukem     528:                ffs_sb_swap((struct fs*)bp->b_data, newfs);
1.55      fvdl      529:                fs->fs_flags |= FS_SWAPPED;
1.121     bouyer    530:        } else
1.34      bouyer    531: #endif
1.121     bouyer    532:                fs->fs_flags &= ~FS_SWAPPED;
1.161     perry     533:        if ((newfs->fs_magic != FS_UFS1_MAGIC &&
1.110     fvdl      534:             newfs->fs_magic != FS_UFS2_MAGIC)||
                    535:             newfs->fs_bsize > MAXBSIZE ||
                    536:             newfs->fs_bsize < sizeof(struct fs)) {
1.1       mycroft   537:                brelse(bp);
1.34      bouyer    538:                free(newfs, M_UFSMNT);
1.1       mycroft   539:                return (EIO);           /* XXX needs translation */
                    540:        }
1.141     dbj       541:        /* Store off old fs_sblockloc for fs_oldfscompat_read. */
                    542:        sblockloc = fs->fs_sblockloc;
1.161     perry     543:        /*
1.18      cgd       544:         * Copy pointer fields back into superblock before copying in   XXX
                    545:         * new superblock. These should really be in the ufsmount.      XXX
                    546:         * Note that important parameters (eg fs_ncg) are unchanged.
                    547:         */
1.84      lukem     548:        newfs->fs_csp = fs->fs_csp;
1.18      cgd       549:        newfs->fs_maxcluster = fs->fs_maxcluster;
1.85      lukem     550:        newfs->fs_contigdirs = fs->fs_contigdirs;
1.76      mycroft   551:        newfs->fs_ronly = fs->fs_ronly;
1.110     fvdl      552:        newfs->fs_active = fs->fs_active;
1.42      perry     553:        memcpy(fs, newfs, (u_int)fs->fs_sbsize);
1.1       mycroft   554:        brelse(bp);
1.34      bouyer    555:        free(newfs, M_UFSMNT);
1.103     dbj       556:
                    557:        /* Recheck for apple UFS filesystem */
1.153     mycroft   558:        ump->um_flags &= ~UFS_ISAPPLEUFS;
1.103     dbj       559:        /* First check to see if this is tagged as an Apple UFS filesystem
                    560:         * in the disklabel
                    561:         */
1.177     christos  562:        if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
1.103     dbj       563:                (dpart.part->p_fstype == FS_APPLEUFS)) {
1.153     mycroft   564:                ump->um_flags |= UFS_ISAPPLEUFS;
1.103     dbj       565:        }
                    566: #ifdef APPLE_UFS
                    567:        else {
                    568:                /* Manually look for an apple ufs label, and if a valid one
                    569:                 * is found, then treat it like an Apple UFS filesystem anyway
                    570:                 */
1.106     fvdl      571:                error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
1.103     dbj       572:                        APPLEUFS_LABEL_SIZE, cred, &bp);
                    573:                if (error) {
                    574:                        brelse(bp);
                    575:                        return (error);
                    576:                }
                    577:                error = ffs_appleufs_validate(fs->fs_fsmnt,
                    578:                        (struct appleufslabel *)bp->b_data,NULL);
1.153     mycroft   579:                if (error == 0)
                    580:                        ump->um_flags |= UFS_ISAPPLEUFS;
1.103     dbj       581:                brelse(bp);
                    582:                bp = NULL;
                    583:        }
                    584: #else
1.153     mycroft   585:        if (ump->um_flags & UFS_ISAPPLEUFS)
1.103     dbj       586:                return (EIO);
                    587: #endif
                    588:
1.153     mycroft   589:        if (UFS_MPISAPPLEUFS(ump)) {
1.103     dbj       590:                /* see comment about NeXT below */
1.153     mycroft   591:                ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
                    592:                ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
                    593:                mp->mnt_iflag |= IMNT_DTYPE;
                    594:        } else {
                    595:                ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
                    596:                ump->um_dirblksiz = DIRBLKSIZ;
                    597:                if (ump->um_maxsymlinklen > 0)
                    598:                        mp->mnt_iflag |= IMNT_DTYPE;
                    599:                else
                    600:                        mp->mnt_iflag &= ~IMNT_DTYPE;
1.103     dbj       601:        }
1.154     yamt      602:        ffs_oldfscompat_read(fs, ump, sblockloc);
1.153     mycroft   603:        ump->um_maxfilesize = fs->fs_maxfilesize;
1.89      fvdl      604:        if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
                    605:                fs->fs_pendingblocks = 0;
                    606:                fs->fs_pendinginodes = 0;
                    607:        }
1.85      lukem     608:
1.177     christos  609:        ffs_statvfs(mp, &mp->mnt_stat, l);
1.1       mycroft   610:        /*
                    611:         * Step 3: re-read summary information from disk.
                    612:         */
                    613:        blks = howmany(fs->fs_cssize, fs->fs_fsize);
1.84      lukem     614:        space = fs->fs_csp;
1.1       mycroft   615:        for (i = 0; i < blks; i += fs->fs_frag) {
                    616:                size = fs->fs_bsize;
                    617:                if (i + fs->fs_frag > blks)
                    618:                        size = (blks - i) * fs->fs_fsize;
1.19      christos  619:                error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
                    620:                              NOCRED, &bp);
1.47      bouyer    621:                if (error) {
                    622:                        brelse(bp);
1.1       mycroft   623:                        return (error);
1.47      bouyer    624:                }
1.34      bouyer    625: #ifdef FFS_EI
1.55      fvdl      626:                if (UFS_FSNEEDSWAP(fs))
1.84      lukem     627:                        ffs_csum_swap((struct csum *)bp->b_data,
                    628:                            (struct csum *)space, size);
1.34      bouyer    629:                else
                    630: #endif
1.84      lukem     631:                        memcpy(space, bp->b_data, (size_t)size);
                    632:                space = (char *)space + size;
1.1       mycroft   633:                brelse(bp);
                    634:        }
1.55      fvdl      635:        if ((fs->fs_flags & FS_DOSOFTDEP))
1.153     mycroft   636:                softdep_mount(devvp, mp, fs, cred);
1.149     hannken   637:        if (fs->fs_snapinum[0] != 0)
1.153     mycroft   638:                ffs_snapshot_mount(mp);
1.18      cgd       639:        /*
                    640:         * We no longer know anything about clusters per cylinder group.
                    641:         */
                    642:        if (fs->fs_contigsumsize > 0) {
                    643:                lp = fs->fs_maxcluster;
                    644:                for (i = 0; i < fs->fs_ncg; i++)
                    645:                        *lp++ = fs->fs_contigsumsize;
                    646:        }
                    647:
1.1       mycroft   648: loop:
1.189     reinoud   649:        /*
                    650:         * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
                    651:         * and vclean() can be called indirectly
                    652:         */
1.33      fvdl      653:        simple_lock(&mntvnode_slock);
1.189     reinoud   654:        for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1.153     mycroft   655:                if (vp->v_mount != mp) {
1.33      fvdl      656:                        simple_unlock(&mntvnode_slock);
                    657:                        goto loop;
                    658:                }
1.1       mycroft   659:                /*
                    660:                 * Step 4: invalidate all inactive vnodes.
                    661:                 */
1.177     christos  662:                if (vrecycle(vp, &mntvnode_slock, l))
1.33      fvdl      663:                        goto loop;
1.1       mycroft   664:                /*
                    665:                 * Step 5: invalidate all cached file data.
                    666:                 */
1.33      fvdl      667:                simple_lock(&vp->v_interlock);
1.189     reinoud   668:                nvp = TAILQ_NEXT(vp, v_mntvnodes);
1.33      fvdl      669:                simple_unlock(&mntvnode_slock);
1.117     thorpej   670:                if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
1.1       mycroft   671:                        goto loop;
1.177     christos  672:                if (vinvalbuf(vp, 0, cred, l, 0, 0))
1.1       mycroft   673:                        panic("ffs_reload: dirty2");
                    674:                /*
                    675:                 * Step 6: re-read inode data for all active vnodes.
                    676:                 */
                    677:                ip = VTOI(vp);
1.19      christos  678:                error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
                    679:                              (int)fs->fs_bsize, NOCRED, &bp);
                    680:                if (error) {
1.47      bouyer    681:                        brelse(bp);
1.1       mycroft   682:                        vput(vp);
                    683:                        return (error);
                    684:                }
1.110     fvdl      685:                ffs_load_inode(bp, ip, fs, ip->i_number);
                    686:                ip->i_ffs_effnlink = ip->i_nlink;
1.1       mycroft   687:                brelse(bp);
                    688:                vput(vp);
1.33      fvdl      689:                simple_lock(&mntvnode_slock);
1.1       mycroft   690:        }
1.33      fvdl      691:        simple_unlock(&mntvnode_slock);
1.1       mycroft   692:        return (0);
                    693: }
                    694:
                    695: /*
1.110     fvdl      696:  * Possible superblock locations ordered from most to least likely.
                    697:  */
1.135     jdolecek  698: static const int sblock_try[] = SBLOCKSEARCH;
1.110     fvdl      699:
                    700: /*
1.1       mycroft   701:  * Common code for mount and mountroot
                    702:  */
                    703: int
1.177     christos  704: ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
1.1       mycroft   705: {
1.34      bouyer    706:        struct ufsmount *ump;
1.1       mycroft   707:        struct buf *bp;
1.34      bouyer    708:        struct fs *fs;
1.9       mycroft   709:        dev_t dev;
1.1       mycroft   710:        struct partinfo dpart;
1.84      lukem     711:        void *space;
1.110     fvdl      712:        daddr_t sblockloc, fsblockloc;
                    713:        int blks, fstype;
1.52      drochner  714:        int error, i, size, ronly;
                    715: #ifdef FFS_EI
1.110     fvdl      716:        int needswap = 0;               /* keep gcc happy */
1.52      drochner  717: #endif
1.9       mycroft   718:        int32_t *lp;
1.181     elad      719:        kauth_cred_t cred;
1.110     fvdl      720:        u_int32_t sbsize = 8192;        /* keep gcc happy*/
1.1       mycroft   721:
1.9       mycroft   722:        dev = devvp->v_rdev;
1.184     ad        723:        cred = l ? l->l_cred : NOCRED;
1.159     mycroft   724:
                    725:        /* Flush out any old buffers remaining from a previous use. */
1.55      fvdl      726:        vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1.177     christos  727:        error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
1.55      fvdl      728:        VOP_UNLOCK(devvp, 0);
                    729:        if (error)
1.1       mycroft   730:                return (error);
                    731:
                    732:        ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
1.177     christos  733:        if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) != 0)
1.1       mycroft   734:                size = DEV_BSIZE;
                    735:        else
                    736:                size = dpart.disklab->d_secsize;
                    737:
                    738:        bp = NULL;
                    739:        ump = NULL;
1.110     fvdl      740:        fs = NULL;
1.138     dsl       741:        sblockloc = 0;
1.110     fvdl      742:        fstype = 0;
1.34      bouyer    743:
1.110     fvdl      744:        /*
1.192     isaki     745:         * Try reading the superblock in each of its possible locations.
                    746:         */
1.138     dsl       747:        for (i = 0; ; i++) {
                    748:                if (bp != NULL) {
                    749:                        bp->b_flags |= B_NOCACHE;
                    750:                        brelse(bp);
                    751:                        bp = NULL;
                    752:                }
                    753:                if (sblock_try[i] == -1) {
                    754:                        error = EINVAL;
                    755:                        fs = NULL;
                    756:                        goto out;
                    757:                }
1.110     fvdl      758:                error = bread(devvp, sblock_try[i] / size, SBLOCKSIZE, cred,
                    759:                              &bp);
1.168     drochner  760:                if (error) {
                    761:                        fs = NULL;
1.110     fvdl      762:                        goto out;
1.168     drochner  763:                }
1.110     fvdl      764:                fs = (struct fs*)bp->b_data;
                    765:                fsblockloc = sblockloc = sblock_try[i];
                    766:                if (fs->fs_magic == FS_UFS1_MAGIC) {
                    767:                        sbsize = fs->fs_sbsize;
                    768:                        fstype = UFS1;
1.34      bouyer    769: #ifdef FFS_EI
1.110     fvdl      770:                        needswap = 0;
                    771:                } else if (fs->fs_magic == bswap32(FS_UFS1_MAGIC)) {
                    772:                        sbsize = bswap32(fs->fs_sbsize);
                    773:                        fstype = UFS1;
                    774:                        needswap = 1;
1.34      bouyer    775: #endif
1.110     fvdl      776:                } else if (fs->fs_magic == FS_UFS2_MAGIC) {
                    777:                        sbsize = fs->fs_sbsize;
                    778:                        fstype = UFS2;
                    779: #ifdef FFS_EI
                    780:                        needswap = 0;
                    781:                } else if (fs->fs_magic == bswap32(FS_UFS2_MAGIC)) {
                    782:                        sbsize = bswap32(fs->fs_sbsize);
                    783:                        fstype = UFS2;
                    784:                        needswap = 1;
                    785: #endif
1.112     fvdl      786:                } else
1.138     dsl       787:                        continue;
                    788:
                    789:
                    790:                /* fs->fs_sblockloc isn't defined for old filesystems */
1.140     dsl       791:                if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) {
                    792:                        if (sblockloc == SBLOCK_UFS2)
1.138     dsl       793:                                /*
                    794:                                 * This is likely to be the first alternate
                    795:                                 * in a filesystem with 64k blocks.
                    796:                                 * Don't use it.
                    797:                                 */
                    798:                                continue;
                    799:                        fsblockloc = sblockloc;
                    800:                } else {
                    801:                        fsblockloc = fs->fs_sblockloc;
                    802: #ifdef FFS_EI
                    803:                        if (needswap)
                    804:                                fsblockloc = bswap64(fsblockloc);
                    805: #endif
                    806:                }
1.110     fvdl      807:
1.138     dsl       808:                /* Check we haven't found an alternate superblock */
                    809:                if (fsblockloc != sblockloc)
                    810:                        continue;
1.112     fvdl      811:
1.138     dsl       812:                /* Validate size of superblock */
                    813:                if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs))
                    814:                        continue;
1.110     fvdl      815:
1.138     dsl       816:                /* Ok seems to be a good superblock */
                    817:                break;
1.34      bouyer    818:        }
                    819:
                    820:        fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
1.42      perry     821:        memcpy(fs, bp->b_data, sbsize);
1.111     fvdl      822:
                    823:        ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
                    824:        memset(ump, 0, sizeof *ump);
1.149     hannken   825:        TAILQ_INIT(&ump->um_snapshots);
1.111     fvdl      826:        ump->um_fs = fs;
1.175     yamt      827:        ump->um_ops = &ffs_ufsops;
1.111     fvdl      828:
1.34      bouyer    829: #ifdef FFS_EI
1.55      fvdl      830:        if (needswap) {
1.83      lukem     831:                ffs_sb_swap((struct fs*)bp->b_data, fs);
1.55      fvdl      832:                fs->fs_flags |= FS_SWAPPED;
1.121     bouyer    833:        } else
1.34      bouyer    834: #endif
1.121     bouyer    835:                fs->fs_flags &= ~FS_SWAPPED;
1.56      drochner  836:
1.154     yamt      837:        ffs_oldfscompat_read(fs, ump, sblockloc);
1.153     mycroft   838:        ump->um_maxfilesize = fs->fs_maxfilesize;
1.131     dbj       839:
1.89      fvdl      840:        if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
                    841:                fs->fs_pendingblocks = 0;
                    842:                fs->fs_pendinginodes = 0;
                    843:        }
1.56      drochner  844:
1.110     fvdl      845:        ump->um_fstype = fstype;
                    846:        if (fs->fs_sbsize < SBLOCKSIZE)
1.1       mycroft   847:                bp->b_flags |= B_INVAL;
                    848:        brelse(bp);
                    849:        bp = NULL;
1.94      chs       850:
1.103     dbj       851:        /* First check to see if this is tagged as an Apple UFS filesystem
                    852:         * in the disklabel
                    853:         */
1.177     christos  854:        if ((VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) == 0) &&
1.103     dbj       855:                (dpart.part->p_fstype == FS_APPLEUFS)) {
                    856:                ump->um_flags |= UFS_ISAPPLEUFS;
                    857:        }
                    858: #ifdef APPLE_UFS
                    859:        else {
                    860:                /* Manually look for an apple ufs label, and if a valid one
                    861:                 * is found, then treat it like an Apple UFS filesystem anyway
                    862:                 */
1.106     fvdl      863:                error = bread(devvp, (daddr_t)(APPLEUFS_LABEL_OFFSET / size),
1.103     dbj       864:                        APPLEUFS_LABEL_SIZE, cred, &bp);
                    865:                if (error)
                    866:                        goto out;
                    867:                error = ffs_appleufs_validate(fs->fs_fsmnt,
                    868:                        (struct appleufslabel *)bp->b_data,NULL);
                    869:                if (error == 0) {
                    870:                        ump->um_flags |= UFS_ISAPPLEUFS;
                    871:                }
                    872:                brelse(bp);
                    873:                bp = NULL;
                    874:        }
                    875: #else
                    876:        if (ump->um_flags & UFS_ISAPPLEUFS) {
                    877:                error = EINVAL;
                    878:                goto out;
                    879:        }
                    880: #endif
                    881:
1.94      chs       882:        /*
1.99      chs       883:         * verify that we can access the last block in the fs
                    884:         * if we're mounting read/write.
1.94      chs       885:         */
                    886:
1.99      chs       887:        if (!ronly) {
                    888:                error = bread(devvp, fsbtodb(fs, fs->fs_size - 1), fs->fs_fsize,
                    889:                    cred, &bp);
                    890:                if (bp->b_bcount != fs->fs_fsize)
                    891:                        error = EINVAL;
                    892:                bp->b_flags |= B_INVAL;
                    893:                if (error)
                    894:                        goto out;
                    895:                brelse(bp);
                    896:                bp = NULL;
                    897:        }
1.94      chs       898:
1.1       mycroft   899:        fs->fs_ronly = ronly;
1.15      mycroft   900:        if (ronly == 0) {
                    901:                fs->fs_clean <<= 1;
1.1       mycroft   902:                fs->fs_fmod = 1;
1.15      mycroft   903:        }
1.9       mycroft   904:        size = fs->fs_cssize;
                    905:        blks = howmany(size, fs->fs_fsize);
                    906:        if (fs->fs_contigsumsize > 0)
                    907:                size += fs->fs_ncg * sizeof(int32_t);
1.85      lukem     908:        size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
1.84      lukem     909:        space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
                    910:        fs->fs_csp = space;
1.1       mycroft   911:        for (i = 0; i < blks; i += fs->fs_frag) {
                    912:                size = fs->fs_bsize;
                    913:                if (i + fs->fs_frag > blks)
                    914:                        size = (blks - i) * fs->fs_fsize;
1.19      christos  915:                error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
                    916:                              cred, &bp);
                    917:                if (error) {
1.84      lukem     918:                        free(fs->fs_csp, M_UFSMNT);
1.128     dbj       919:                        goto out;
1.1       mycroft   920:                }
1.34      bouyer    921: #ifdef FFS_EI
                    922:                if (needswap)
1.84      lukem     923:                        ffs_csum_swap((struct csum *)bp->b_data,
                    924:                                (struct csum *)space, size);
1.34      bouyer    925:                else
                    926: #endif
1.42      perry     927:                        memcpy(space, bp->b_data, (u_int)size);
1.161     perry     928:
1.84      lukem     929:                space = (char *)space + size;
1.1       mycroft   930:                brelse(bp);
                    931:                bp = NULL;
                    932:        }
1.9       mycroft   933:        if (fs->fs_contigsumsize > 0) {
1.85      lukem     934:                fs->fs_maxcluster = lp = space;
1.9       mycroft   935:                for (i = 0; i < fs->fs_ncg; i++)
                    936:                        *lp++ = fs->fs_contigsumsize;
1.85      lukem     937:                space = lp;
1.9       mycroft   938:        }
1.85      lukem     939:        size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
                    940:        fs->fs_contigdirs = space;
                    941:        space = (char *)space + size;
                    942:        memset(fs->fs_contigdirs, 0, size);
                    943:                /* Compatibility for old filesystems - XXX */
                    944:        if (fs->fs_avgfilesize <= 0)
                    945:                fs->fs_avgfilesize = AVFILESIZ;
                    946:        if (fs->fs_avgfpdir <= 0)
                    947:                fs->fs_avgfpdir = AFPDIR;
1.150     hannken   948:        fs->fs_active = NULL;
1.100     soren     949:        mp->mnt_data = ump;
1.143     christos  950:        mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
                    951:        mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS);
                    952:        mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
1.169     christos  953:        mp->mnt_stat.f_namemax = FFS_MAXNAMLEN;
1.153     mycroft   954:        if (UFS_MPISAPPLEUFS(ump)) {
1.103     dbj       955:                /* NeXT used to keep short symlinks in the inode even
                    956:                 * when using FS_42INODEFMT.  In that case fs->fs_maxsymlinklen
                    957:                 * is probably -1, but we still need to be able to identify
                    958:                 * short symlinks.
                    959:                 */
1.153     mycroft   960:                ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
                    961:                ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ;
                    962:                mp->mnt_iflag |= IMNT_DTYPE;
                    963:        } else {
                    964:                ump->um_maxsymlinklen = fs->fs_maxsymlinklen;
                    965:                ump->um_dirblksiz = DIRBLKSIZ;
                    966:                if (ump->um_maxsymlinklen > 0)
                    967:                        mp->mnt_iflag |= IMNT_DTYPE;
                    968:                else
                    969:                        mp->mnt_iflag &= ~IMNT_DTYPE;
1.103     dbj       970:        }
1.73      chs       971:        mp->mnt_fs_bshift = fs->fs_bshift;
                    972:        mp->mnt_dev_bshift = DEV_BSHIFT;        /* XXX */
1.1       mycroft   973:        mp->mnt_flag |= MNT_LOCAL;
1.193     hannken   974:        mp->mnt_iflag |= IMNT_HAS_TRANS;
1.34      bouyer    975: #ifdef FFS_EI
                    976:        if (needswap)
                    977:                ump->um_flags |= UFS_NEEDSWAP;
                    978: #endif
1.1       mycroft   979:        ump->um_mountp = mp;
                    980:        ump->um_dev = dev;
                    981:        ump->um_devvp = devvp;
                    982:        ump->um_nindir = fs->fs_nindir;
1.73      chs       983:        ump->um_lognindir = ffs(fs->fs_nindir) - 1;
1.1       mycroft   984:        ump->um_bptrtodb = fs->fs_fsbtodb;
                    985:        ump->um_seqinc = fs->fs_frag;
                    986:        for (i = 0; i < MAXQUOTAS; i++)
                    987:                ump->um_quotas[i] = NULLVP;
1.55      fvdl      988:        devvp->v_specmountpoint = mp;
                    989:        if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
                    990:                error = softdep_mount(devvp, mp, fs, cred);
                    991:                if (error) {
1.84      lukem     992:                        free(fs->fs_csp, M_UFSMNT);
1.55      fvdl      993:                        goto out;
                    994:                }
                    995:        }
1.149     hannken   996:        if (ronly == 0 && fs->fs_snapinum[0] != 0)
                    997:                ffs_snapshot_mount(mp);
1.170     thorpej   998: #ifdef UFS_EXTATTR
                    999:        /*
                   1000:         * Initialize file-backed extended attributes on UFS1 file
                   1001:         * systems.
                   1002:         */
                   1003:        if (ump->um_fstype == UFS1) {
                   1004:                ufs_extattr_uepm_init(&ump->um_extattr);
                   1005: #ifdef UFS_EXTATTR_AUTOSTART
                   1006:                /*
                   1007:                 * XXX Just ignore errors.  Not clear that we should
                   1008:                 * XXX fail the mount in this case.
                   1009:                 */
1.178     rpaulo   1010:                (void) ufs_extattr_autostart(mp, l);
1.170     thorpej  1011: #endif
                   1012:        }
                   1013: #endif /* UFS_EXTATTR */
1.1       mycroft  1014:        return (0);
                   1015: out:
1.128     dbj      1016:        if (fs)
                   1017:                free(fs, M_UFSMNT);
1.55      fvdl     1018:        devvp->v_specmountpoint = NULL;
1.1       mycroft  1019:        if (bp)
                   1020:                brelse(bp);
                   1021:        if (ump) {
1.131     dbj      1022:                if (ump->um_oldfscompat)
                   1023:                        free(ump->um_oldfscompat, M_UFSMNT);
1.1       mycroft  1024:                free(ump, M_UFSMNT);
1.100     soren    1025:                mp->mnt_data = NULL;
1.1       mycroft  1026:        }
                   1027:        return (error);
                   1028: }
                   1029:
                   1030: /*
1.110     fvdl     1031:  * Sanity checks for loading old filesystem superblocks.
                   1032:  * See ffs_oldfscompat_write below for unwound actions.
1.1       mycroft  1033:  *
1.110     fvdl     1034:  * XXX - Parts get retired eventually.
                   1035:  * Unfortunately new bits get added.
1.1       mycroft  1036:  */
1.110     fvdl     1037: static void
1.166     thorpej  1038: ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc)
1.110     fvdl     1039: {
                   1040:        off_t maxfilesize;
1.131     dbj      1041:        int32_t *extrasave;
1.110     fvdl     1042:
1.131     dbj      1043:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1044:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.111     fvdl     1045:                return;
                   1046:
1.131     dbj      1047:        if (!ump->um_oldfscompat)
                   1048:                ump->um_oldfscompat = malloc(512 + 3*sizeof(int32_t),
                   1049:                    M_UFSMNT, M_WAITOK);
                   1050:
                   1051:        memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512);
                   1052:        extrasave = ump->um_oldfscompat;
                   1053:        extrasave += 512/sizeof(int32_t);
                   1054:        extrasave[0] = fs->fs_old_npsect;
                   1055:        extrasave[1] = fs->fs_old_interleave;
                   1056:        extrasave[2] = fs->fs_old_trackskew;
                   1057:
                   1058:        /* These fields will be overwritten by their
                   1059:         * original values in fs_oldfscompat_write, so it is harmless
                   1060:         * to modify them here.
                   1061:         */
                   1062:        fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
                   1063:        fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
                   1064:        fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
                   1065:        fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
                   1066:
                   1067:        fs->fs_maxbsize = fs->fs_bsize;
                   1068:        fs->fs_time = fs->fs_old_time;
                   1069:        fs->fs_size = fs->fs_old_size;
                   1070:        fs->fs_dsize = fs->fs_old_dsize;
                   1071:        fs->fs_csaddr = fs->fs_old_csaddr;
                   1072:        fs->fs_sblockloc = sblockloc;
                   1073:
1.162     christos 1074:         fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL);
1.122     enami    1075:
1.131     dbj      1076:        if (fs->fs_old_postblformat == FS_42POSTBLFMT) {
                   1077:                fs->fs_old_nrpos = 8;
                   1078:                fs->fs_old_npsect = fs->fs_old_nsect;
                   1079:                fs->fs_old_interleave = 1;
                   1080:                fs->fs_old_trackskew = 0;
1.111     fvdl     1081:        }
                   1082:
                   1083:        if (fs->fs_old_inodefmt < FS_44INODEFMT) {
1.153     mycroft  1084:                ump->um_maxfilesize = (u_quad_t) 1LL << 39;
1.110     fvdl     1085:                fs->fs_qbmask = ~fs->fs_bmask;
                   1086:                fs->fs_qfmask = ~fs->fs_fmask;
                   1087:        }
1.111     fvdl     1088:
                   1089:        maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1;
1.153     mycroft  1090:        if (ump->um_maxfilesize > maxfilesize)
                   1091:                ump->um_maxfilesize = maxfilesize;
1.111     fvdl     1092:
1.110     fvdl     1093:        /* Compatibility for old filesystems */
                   1094:        if (fs->fs_avgfilesize <= 0)
                   1095:                fs->fs_avgfilesize = AVFILESIZ;
                   1096:        if (fs->fs_avgfpdir <= 0)
                   1097:                fs->fs_avgfpdir = AFPDIR;
1.131     dbj      1098:
1.110     fvdl     1099: #if 0
                   1100:        if (bigcgs) {
                   1101:                fs->fs_save_cgsize = fs->fs_cgsize;
                   1102:                fs->fs_cgsize = fs->fs_bsize;
                   1103:        }
                   1104: #endif
                   1105: }
                   1106:
                   1107: /*
                   1108:  * Unwinding superblock updates for old filesystems.
                   1109:  * See ffs_oldfscompat_read above for details.
                   1110:  *
                   1111:  * XXX - Parts get retired eventually.
                   1112:  * Unfortunately new bits get added.
                   1113:  */
                   1114: static void
1.166     thorpej  1115: ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump)
1.1       mycroft  1116: {
1.131     dbj      1117:        int32_t *extrasave;
                   1118:
                   1119:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1120:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.111     fvdl     1121:                return;
1.115     fvdl     1122:
1.111     fvdl     1123:        fs->fs_old_time = fs->fs_time;
                   1124:        fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
                   1125:        fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
                   1126:        fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
                   1127:        fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
1.131     dbj      1128:        fs->fs_old_flags = fs->fs_flags;
1.111     fvdl     1129:
1.110     fvdl     1130: #if 0
                   1131:        if (bigcgs) {
                   1132:                fs->fs_cgsize = fs->fs_save_cgsize;
                   1133:        }
                   1134: #endif
1.131     dbj      1135:
                   1136:        memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512);
                   1137:        extrasave = ump->um_oldfscompat;
                   1138:        extrasave += 512/sizeof(int32_t);
                   1139:        fs->fs_old_npsect = extrasave[0];
                   1140:        fs->fs_old_interleave = extrasave[1];
                   1141:        fs->fs_old_trackskew = extrasave[2];
                   1142:
1.1       mycroft  1143: }
                   1144:
                   1145: /*
                   1146:  * unmount system call
                   1147:  */
                   1148: int
1.177     christos 1149: ffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
1.1       mycroft  1150: {
1.170     thorpej  1151:        struct ufsmount *ump = VFSTOUFS(mp);
                   1152:        struct fs *fs = ump->um_fs;
1.91      fvdl     1153:        int error, flags, penderr;
1.1       mycroft  1154:
1.91      fvdl     1155:        penderr = 0;
1.1       mycroft  1156:        flags = 0;
1.11      mycroft  1157:        if (mntflags & MNT_FORCE)
1.1       mycroft  1158:                flags |= FORCECLOSE;
1.170     thorpej  1159: #ifdef UFS_EXTATTR
                   1160:        if (ump->um_fstype == UFS1) {
1.178     rpaulo   1161:                error = ufs_extattr_stop(mp, l);
1.173     rpaulo   1162:                if (error) {
                   1163:                        if (error != EOPNOTSUPP)
                   1164:                                printf("%s: ufs_extattr_stop returned %d\n",
                   1165:                                    fs->fs_fsmnt, error);
                   1166:                } else
1.170     thorpej  1167:                        ufs_extattr_uepm_destroy(&ump->um_extattr);
                   1168:        }
                   1169: #endif /* UFS_EXTATTR */
1.55      fvdl     1170:        if (mp->mnt_flag & MNT_SOFTDEP) {
1.177     christos 1171:                if ((error = softdep_flushfiles(mp, flags, l)) != 0)
1.55      fvdl     1172:                        return (error);
                   1173:        } else {
1.177     christos 1174:                if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1.55      fvdl     1175:                        return (error);
                   1176:        }
1.89      fvdl     1177:        if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
1.110     fvdl     1178:                printf("%s: unmount pending error: blocks %" PRId64
                   1179:                       " files %d\n",
1.89      fvdl     1180:                    fs->fs_fsmnt, fs->fs_pendingblocks, fs->fs_pendinginodes);
                   1181:                fs->fs_pendingblocks = 0;
                   1182:                fs->fs_pendinginodes = 0;
1.91      fvdl     1183:                penderr = 1;
1.89      fvdl     1184:        }
1.15      mycroft  1185:        if (fs->fs_ronly == 0 &&
                   1186:            ffs_cgupdate(ump, MNT_WAIT) == 0 &&
                   1187:            fs->fs_clean & FS_WASCLEAN) {
1.91      fvdl     1188:                /*
                   1189:                 * XXXX don't mark fs clean in the case of softdep
                   1190:                 * pending block errors, until they are fixed.
                   1191:                 */
                   1192:                if (penderr == 0) {
                   1193:                        if (mp->mnt_flag & MNT_SOFTDEP)
                   1194:                                fs->fs_flags &= ~FS_DOSOFTDEP;
                   1195:                        fs->fs_clean = FS_ISCLEAN;
                   1196:                }
1.109     fvdl     1197:                fs->fs_fmod = 0;
1.15      mycroft  1198:                (void) ffs_sbupdate(ump, MNT_WAIT);
                   1199:        }
1.54      enami    1200:        if (ump->um_devvp->v_type != VBAD)
1.55      fvdl     1201:                ump->um_devvp->v_specmountpoint = NULL;
1.53      wrstuden 1202:        vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1.129     dbj      1203:        (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
1.177     christos 1204:                NOCRED, l);
1.53      wrstuden 1205:        vput(ump->um_devvp);
1.84      lukem    1206:        free(fs->fs_csp, M_UFSMNT);
1.1       mycroft  1207:        free(fs, M_UFSMNT);
1.131     dbj      1208:        if (ump->um_oldfscompat != NULL)
                   1209:                free(ump->um_oldfscompat, M_UFSMNT);
1.1       mycroft  1210:        free(ump, M_UFSMNT);
1.100     soren    1211:        mp->mnt_data = NULL;
1.1       mycroft  1212:        mp->mnt_flag &= ~MNT_LOCAL;
1.129     dbj      1213:        return (0);
1.1       mycroft  1214: }
                   1215:
                   1216: /*
                   1217:  * Flush out all the files in a filesystem.
                   1218:  */
1.19      christos 1219: int
1.177     christos 1220: ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1.1       mycroft  1221: {
                   1222:        extern int doforce;
1.61      augustss 1223:        struct ufsmount *ump;
1.19      christos 1224:        int error;
1.1       mycroft  1225:
                   1226:        if (!doforce)
                   1227:                flags &= ~FORCECLOSE;
                   1228:        ump = VFSTOUFS(mp);
                   1229: #ifdef QUOTA
                   1230:        if (mp->mnt_flag & MNT_QUOTA) {
1.19      christos 1231:                int i;
                   1232:                if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
1.1       mycroft  1233:                        return (error);
                   1234:                for (i = 0; i < MAXQUOTAS; i++) {
                   1235:                        if (ump->um_quotas[i] == NULLVP)
                   1236:                                continue;
1.177     christos 1237:                        quotaoff(l, mp, i);
1.1       mycroft  1238:                }
                   1239:                /*
                   1240:                 * Here we fall through to vflush again to ensure
                   1241:                 * that we have gotten rid of all the system vnodes.
                   1242:                 */
                   1243:        }
                   1244: #endif
1.149     hannken  1245:        if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
                   1246:                return (error);
                   1247:        ffs_snapshot_unmount(mp);
1.55      fvdl     1248:        /*
                   1249:         * Flush all the files.
                   1250:         */
1.1       mycroft  1251:        error = vflush(mp, NULLVP, flags);
1.55      fvdl     1252:        if (error)
                   1253:                return (error);
                   1254:        /*
                   1255:         * Flush filesystem metadata.
                   1256:         */
                   1257:        vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1.184     ad       1258:        error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0, l);
1.55      fvdl     1259:        VOP_UNLOCK(ump->um_devvp, 0);
1.1       mycroft  1260:        return (error);
                   1261: }
                   1262:
                   1263: /*
                   1264:  * Get file system statistics.
                   1265:  */
                   1266: int
1.190     christos 1267: ffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
1.1       mycroft  1268: {
1.61      augustss 1269:        struct ufsmount *ump;
                   1270:        struct fs *fs;
1.1       mycroft  1271:
                   1272:        ump = VFSTOUFS(mp);
                   1273:        fs = ump->um_fs;
1.143     christos 1274:        sbp->f_bsize = fs->fs_bsize;
                   1275:        sbp->f_frsize = fs->fs_fsize;
1.1       mycroft  1276:        sbp->f_iosize = fs->fs_bsize;
                   1277:        sbp->f_blocks = fs->fs_dsize;
1.98      mycroft  1278:        sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1.89      fvdl     1279:                fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1.143     christos 1280:        sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
                   1281:            fs->fs_minfree) / (u_int64_t) 100;
                   1282:        if (sbp->f_bfree > sbp->f_bresvd)
                   1283:                sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
                   1284:        else
                   1285:                sbp->f_bavail = 0;
1.1       mycroft  1286:        sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
1.89      fvdl     1287:        sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1.143     christos 1288:        sbp->f_favail = sbp->f_ffree;
                   1289:        sbp->f_fresvd = 0;
                   1290:        copy_statvfs_info(sbp, mp);
1.1       mycroft  1291:        return (0);
                   1292: }
                   1293:
                   1294: /*
                   1295:  * Go through the disk queues to initiate sandbagged IO;
                   1296:  * go through the inodes to write those that have been modified;
                   1297:  * initiate the writing of the super block if it has been modified.
                   1298:  *
                   1299:  * Note: we are always called with the filesystem marked `MPBUSY'.
                   1300:  */
                   1301: int
1.181     elad     1302: ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *l)
1.1       mycroft  1303: {
1.189     reinoud  1304:        struct vnode *vp, *nvp;
1.33      fvdl     1305:        struct inode *ip;
                   1306:        struct ufsmount *ump = VFSTOUFS(mp);
                   1307:        struct fs *fs;
1.132     hannken  1308:        int error, count, allerror = 0;
1.1       mycroft  1309:
                   1310:        fs = ump->um_fs;
1.33      fvdl     1311:        if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {            /* XXX */
                   1312:                printf("fs = %s\n", fs->fs_fsmnt);
                   1313:                panic("update: rofs mod");
1.1       mycroft  1314:        }
1.199   ! hannken  1315:        fstrans_start(mp, FSTRANS_SHARED);
1.1       mycroft  1316:        /*
                   1317:         * Write back each (modified) inode.
                   1318:         */
1.33      fvdl     1319:        simple_lock(&mntvnode_slock);
1.1       mycroft  1320: loop:
1.189     reinoud  1321:        /*
                   1322:         * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
                   1323:         * and vclean() can be called indirectly
                   1324:         */
                   1325:        for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1.1       mycroft  1326:                /*
                   1327:                 * If the vnode that we are about to sync is no longer
                   1328:                 * associated with this mount point, start over.
                   1329:                 */
                   1330:                if (vp->v_mount != mp)
                   1331:                        goto loop;
1.33      fvdl     1332:                simple_lock(&vp->v_interlock);
1.189     reinoud  1333:                nvp = TAILQ_NEXT(vp, v_mntvnodes);
1.1       mycroft  1334:                ip = VTOI(vp);
1.57      fvdl     1335:                if (vp->v_type == VNON ||
                   1336:                    ((ip->i_flag &
1.152     mycroft  1337:                      (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1.75      chs      1338:                     LIST_EMPTY(&vp->v_dirtyblkhd) &&
1.87      chs      1339:                     vp->v_uobj.uo_npages == 0))
1.57      fvdl     1340:                {
1.33      fvdl     1341:                        simple_unlock(&vp->v_interlock);
                   1342:                        continue;
                   1343:                }
1.193     hannken  1344:                if (vp->v_type == VBLK &&
1.194     hannken  1345:                    fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
1.193     hannken  1346:                        simple_unlock(&vp->v_interlock);
                   1347:                        continue;
                   1348:                }
1.33      fvdl     1349:                simple_unlock(&mntvnode_slock);
1.117     thorpej  1350:                error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1.33      fvdl     1351:                if (error) {
                   1352:                        simple_lock(&mntvnode_slock);
                   1353:                        if (error == ENOENT)
                   1354:                                goto loop;
1.1       mycroft  1355:                        continue;
1.33      fvdl     1356:                }
1.152     mycroft  1357:                if (vp->v_type == VREG && waitfor == MNT_LAZY)
1.176     yamt     1358:                        error = ffs_update(vp, NULL, NULL, 0);
1.152     mycroft  1359:                else
                   1360:                        error = VOP_FSYNC(vp, cred,
1.177     christos 1361:                            waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
1.152     mycroft  1362:                if (error)
1.1       mycroft  1363:                        allerror = error;
                   1364:                vput(vp);
1.33      fvdl     1365:                simple_lock(&mntvnode_slock);
1.1       mycroft  1366:        }
1.33      fvdl     1367:        simple_unlock(&mntvnode_slock);
1.1       mycroft  1368:        /*
                   1369:         * Force stale file system control information to be flushed.
                   1370:         */
1.132     hannken  1371:        if (waitfor == MNT_WAIT && (ump->um_mountp->mnt_flag & MNT_SOFTDEP)) {
1.177     christos 1372:                if ((error = softdep_flushworklist(ump->um_mountp, &count, l)))
1.132     hannken  1373:                        allerror = error;
                   1374:                /* Flushed work items may create new vnodes to clean */
                   1375:                if (allerror == 0 && count) {
                   1376:                        simple_lock(&mntvnode_slock);
                   1377:                        goto loop;
                   1378:                }
                   1379:        }
                   1380:        if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
                   1381:            !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1.55      fvdl     1382:                vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
                   1383:                if ((error = VOP_FSYNC(ump->um_devvp, cred,
1.177     christos 1384:                    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
1.55      fvdl     1385:                        allerror = error;
                   1386:                VOP_UNLOCK(ump->um_devvp, 0);
1.132     hannken  1387:                if (allerror == 0 && waitfor == MNT_WAIT) {
                   1388:                        simple_lock(&mntvnode_slock);
                   1389:                        goto loop;
                   1390:                }
1.55      fvdl     1391:        }
1.1       mycroft  1392: #ifdef QUOTA
1.118     fvdl     1393:        qsync(mp);
1.1       mycroft  1394: #endif
1.33      fvdl     1395:        /*
                   1396:         * Write back modified superblock.
                   1397:         */
                   1398:        if (fs->fs_fmod != 0) {
                   1399:                fs->fs_fmod = 0;
1.182     kardel   1400:                fs->fs_time = time_second;
1.64      mycroft  1401:                if ((error = ffs_cgupdate(ump, waitfor)))
                   1402:                        allerror = error;
1.33      fvdl     1403:        }
1.193     hannken  1404:        fstrans_done(mp);
1.1       mycroft  1405:        return (allerror);
                   1406: }
                   1407:
                   1408: /*
                   1409:  * Look up a FFS dinode number to find its incore vnode, otherwise read it
                   1410:  * in from disk.  If it is in core, wait for the lock bit to clear, then
                   1411:  * return the inode locked.  Detection and handling of mount points must be
                   1412:  * done by the calling routine.
                   1413:  */
                   1414: int
1.166     thorpej  1415: ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1.1       mycroft  1416: {
1.33      fvdl     1417:        struct fs *fs;
                   1418:        struct inode *ip;
1.1       mycroft  1419:        struct ufsmount *ump;
                   1420:        struct buf *bp;
                   1421:        struct vnode *vp;
                   1422:        dev_t dev;
1.43      thorpej  1423:        int error;
1.1       mycroft  1424:
                   1425:        ump = VFSTOUFS(mp);
                   1426:        dev = ump->um_dev;
1.68      fvdl     1427:
1.117     thorpej  1428:        if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1.68      fvdl     1429:                return (0);
1.1       mycroft  1430:
                   1431:        /* Allocate a new vnode/inode. */
1.19      christos 1432:        if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1.1       mycroft  1433:                *vpp = NULL;
                   1434:                return (error);
                   1435:        }
1.195     ad       1436:        ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1.68      fvdl     1437:
                   1438:        /*
                   1439:         * If someone beat us to it while sleeping in getnewvnode(),
                   1440:         * push back the freshly allocated vnode we don't need, and return.
                   1441:         */
1.195     ad       1442:        mutex_enter(&ufs_hashlock);
                   1443:        if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
                   1444:                mutex_exit(&ufs_hashlock);
                   1445:                ungetnewvnode(vp);
                   1446:                pool_put(&ffs_inode_pool, ip);
                   1447:                return (0);
                   1448:        }
1.68      fvdl     1449:
1.155     thorpej  1450:        vp->v_flag |= VLOCKSWORK;
                   1451:
1.43      thorpej  1452:        /*
                   1453:         * XXX MFS ends up here, too, to allocate an inode.  Should we
                   1454:         * XXX create another pool for MFS inodes?
                   1455:         */
1.87      chs      1456:
                   1457:        memset(ip, 0, sizeof(struct inode));
1.1       mycroft  1458:        vp->v_data = ip;
                   1459:        ip->i_vnode = vp;
1.110     fvdl     1460:        ip->i_ump = ump;
1.1       mycroft  1461:        ip->i_fs = fs = ump->um_fs;
                   1462:        ip->i_dev = dev;
                   1463:        ip->i_number = ino;
1.77      chs      1464:        LIST_INIT(&ip->i_pcbufhd);
1.1       mycroft  1465: #ifdef QUOTA
1.19      christos 1466:        {
                   1467:                int i;
                   1468:
                   1469:                for (i = 0; i < MAXQUOTAS; i++)
                   1470:                        ip->i_dquot[i] = NODQUOT;
                   1471:        }
1.1       mycroft  1472: #endif
1.86      chs      1473:
1.1       mycroft  1474:        /*
                   1475:         * Put it onto its hash chain and lock it so that other requests for
                   1476:         * this inode will block if they arrive while we are sleeping waiting
                   1477:         * for old data structures to be purged or for the contents of the
                   1478:         * disk portion of this inode to be read.
                   1479:         */
1.87      chs      1480:
1.1       mycroft  1481:        ufs_ihashins(ip);
1.195     ad       1482:        mutex_exit(&ufs_hashlock);
1.1       mycroft  1483:
                   1484:        /* Read in the disk contents for the inode, copy into the inode. */
1.19      christos 1485:        error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
                   1486:                      (int)fs->fs_bsize, NOCRED, &bp);
                   1487:        if (error) {
1.87      chs      1488:
1.1       mycroft  1489:                /*
                   1490:                 * The inode does not contain anything useful, so it would
                   1491:                 * be misleading to leave it on its hash chain. With mode
                   1492:                 * still zero, it will be unlinked and returned to the free
                   1493:                 * list by vput().
                   1494:                 */
1.87      chs      1495:
1.1       mycroft  1496:                vput(vp);
                   1497:                brelse(bp);
                   1498:                *vpp = NULL;
                   1499:                return (error);
                   1500:        }
1.110     fvdl     1501:        if (ip->i_ump->um_fstype == UFS1)
                   1502:                ip->i_din.ffs1_din = pool_get(&ffs_dinode1_pool, PR_WAITOK);
                   1503:        else
                   1504:                ip->i_din.ffs2_din = pool_get(&ffs_dinode2_pool, PR_WAITOK);
                   1505:        ffs_load_inode(bp, ip, fs, ino);
1.55      fvdl     1506:        if (DOINGSOFTDEP(vp))
                   1507:                softdep_load_inodeblock(ip);
                   1508:        else
1.110     fvdl     1509:                ip->i_ffs_effnlink = ip->i_nlink;
1.1       mycroft  1510:        brelse(bp);
                   1511:
                   1512:        /*
                   1513:         * Initialize the vnode from the inode, check for aliases.
                   1514:         * Note that the underlying vnode may have changed.
                   1515:         */
1.87      chs      1516:
                   1517:        ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
                   1518:
1.1       mycroft  1519:        /*
                   1520:         * Finish inode initialization now that aliasing has been resolved.
                   1521:         */
1.87      chs      1522:
                   1523:        genfs_node_init(vp, &ffs_genfsops);
1.1       mycroft  1524:        ip->i_devvp = ump->um_devvp;
                   1525:        VREF(ip->i_devvp);
1.87      chs      1526:
1.1       mycroft  1527:        /*
                   1528:         * Ensure that uid and gid are correct. This is a temporary
                   1529:         * fix until fsck has been changed to do the update.
                   1530:         */
1.87      chs      1531:
1.110     fvdl     1532:        if (fs->fs_old_inodefmt < FS_44INODEFMT) {              /* XXX */
                   1533:                ip->i_uid = ip->i_ffs1_ouid;                    /* XXX */
                   1534:                ip->i_gid = ip->i_ffs1_ogid;                    /* XXX */
1.38      kleink   1535:        }                                                       /* XXX */
1.110     fvdl     1536:        uvm_vnp_setsize(vp, ip->i_size);
1.1       mycroft  1537:        *vpp = vp;
                   1538:        return (0);
                   1539: }
                   1540:
                   1541: /*
                   1542:  * File handle to vnode
                   1543:  *
                   1544:  * Have to be really careful about stale file handles:
                   1545:  * - check that the inode number is valid
                   1546:  * - call ffs_vget() to get the locked inode
                   1547:  * - check for an unallocated inode (i_mode == 0)
                   1548:  * - check that the given client host has export rights and return
                   1549:  *   those rights via. exflagsp and credanonp
                   1550:  */
                   1551: int
1.166     thorpej  1552: ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1.1       mycroft  1553: {
1.183     martin   1554:        struct ufid ufh;
1.1       mycroft  1555:        struct fs *fs;
                   1556:
1.183     martin   1557:        if (fhp->fid_len != sizeof(struct ufid))
                   1558:                return EINVAL;
                   1559:
                   1560:        memcpy(&ufh, fhp, sizeof(ufh));
1.1       mycroft  1561:        fs = VFSTOUFS(mp)->um_fs;
1.183     martin   1562:        if (ufh.ufid_ino < ROOTINO ||
                   1563:            ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1.1       mycroft  1564:                return (ESTALE);
1.183     martin   1565:        return (ufs_fhtovp(mp, &ufh, vpp));
1.1       mycroft  1566: }
                   1567:
                   1568: /*
                   1569:  * Vnode pointer to File handle
                   1570:  */
                   1571: /* ARGSUSED */
1.19      christos 1572: int
1.183     martin   1573: ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1.1       mycroft  1574: {
1.61      augustss 1575:        struct inode *ip;
1.183     martin   1576:        struct ufid ufh;
1.1       mycroft  1577:
1.183     martin   1578:        if (*fh_size < sizeof(struct ufid)) {
                   1579:                *fh_size = sizeof(struct ufid);
                   1580:                return E2BIG;
                   1581:        }
1.1       mycroft  1582:        ip = VTOI(vp);
1.183     martin   1583:        *fh_size = sizeof(struct ufid);
                   1584:        memset(&ufh, 0, sizeof(ufh));
                   1585:        ufh.ufid_len = sizeof(struct ufid);
                   1586:        ufh.ufid_ino = ip->i_number;
                   1587:        ufh.ufid_gen = ip->i_gen;
                   1588:        memcpy(fhp, &ufh, sizeof(ufh));
1.1       mycroft  1589:        return (0);
1.33      fvdl     1590: }
                   1591:
                   1592: void
1.166     thorpej  1593: ffs_init(void)
1.33      fvdl     1594: {
1.59      jdolecek 1595:        if (ffs_initcount++ > 0)
                   1596:                return;
                   1597:
1.147     atatat   1598: #ifdef _LKM
                   1599:        pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0,
1.197     ad       1600:                  "ffsinopl", &pool_allocator_nointr, IPL_NONE);
1.161     perry    1601:        pool_init(&ffs_dinode1_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
1.197     ad       1602:                  "dino1pl", &pool_allocator_nointr, IPL_NONE);
1.147     atatat   1603:        pool_init(&ffs_dinode2_pool, sizeof(struct ufs2_dinode), 0, 0, 0,
1.197     ad       1604:                  "dino2pl", &pool_allocator_nointr, IPL_NONE);
1.147     atatat   1605: #endif
1.55      fvdl     1606:        softdep_initialize();
1.33      fvdl     1607:        ufs_init();
1.86      chs      1608: }
                   1609:
                   1610: void
1.166     thorpej  1611: ffs_reinit(void)
1.86      chs      1612: {
                   1613:        softdep_reinitialize();
                   1614:        ufs_reinit();
1.59      jdolecek 1615: }
                   1616:
                   1617: void
1.166     thorpej  1618: ffs_done(void)
1.59      jdolecek 1619: {
                   1620:        if (--ffs_initcount > 0)
                   1621:                return;
                   1622:
                   1623:        /* XXX softdep cleanup ? */
                   1624:        ufs_done();
1.147     atatat   1625: #ifdef _LKM
                   1626:        pool_destroy(&ffs_dinode2_pool);
                   1627:        pool_destroy(&ffs_dinode1_pool);
1.59      jdolecek 1628:        pool_destroy(&ffs_inode_pool);
1.147     atatat   1629: #endif
1.33      fvdl     1630: }
                   1631:
1.130     atatat   1632: SYSCTL_SETUP(sysctl_vfs_ffs_setup, "sysctl vfs.ffs subtree setup")
1.33      fvdl     1633: {
1.176     yamt     1634: #if 0
1.87      chs      1635:        extern int doasyncfree;
1.176     yamt     1636: #endif
1.62      jdolecek 1637:        extern int ffs_log_changeopt;
1.33      fvdl     1638:
1.139     atatat   1639:        sysctl_createv(clog, 0, NULL, NULL,
                   1640:                       CTLFLAG_PERMANENT,
1.130     atatat   1641:                       CTLTYPE_NODE, "vfs", NULL,
                   1642:                       NULL, 0, NULL, 0,
                   1643:                       CTL_VFS, CTL_EOL);
1.139     atatat   1644:        sysctl_createv(clog, 0, NULL, NULL,
                   1645:                       CTLFLAG_PERMANENT,
1.148     atatat   1646:                       CTLTYPE_NODE, "ffs",
                   1647:                       SYSCTL_DESCR("Berkeley Fast File System"),
1.130     atatat   1648:                       NULL, 0, NULL, 0,
                   1649:                       CTL_VFS, 1, CTL_EOL);
                   1650:
                   1651:        /*
                   1652:         * @@@ should we even bother with these first three?
                   1653:         */
1.139     atatat   1654:        sysctl_createv(clog, 0, NULL, NULL,
1.156     jdolecek 1655:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.130     atatat   1656:                       CTLTYPE_INT, "doclusterread", NULL,
                   1657:                       sysctl_notavail, 0, NULL, 0,
                   1658:                       CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL);
1.139     atatat   1659:        sysctl_createv(clog, 0, NULL, NULL,
1.156     jdolecek 1660:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.130     atatat   1661:                       CTLTYPE_INT, "doclusterwrite", NULL,
                   1662:                       sysctl_notavail, 0, NULL, 0,
                   1663:                       CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL);
1.139     atatat   1664:        sysctl_createv(clog, 0, NULL, NULL,
1.156     jdolecek 1665:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.130     atatat   1666:                       CTLTYPE_INT, "doreallocblks", NULL,
                   1667:                       sysctl_notavail, 0, NULL, 0,
                   1668:                       CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL);
1.176     yamt     1669: #if 0
1.139     atatat   1670:        sysctl_createv(clog, 0, NULL, NULL,
1.156     jdolecek 1671:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.148     atatat   1672:                       CTLTYPE_INT, "doasyncfree",
                   1673:                       SYSCTL_DESCR("Release dirty blocks asynchronously"),
1.130     atatat   1674:                       NULL, 0, &doasyncfree, 0,
                   1675:                       CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL);
1.176     yamt     1676: #endif
1.139     atatat   1677:        sysctl_createv(clog, 0, NULL, NULL,
1.156     jdolecek 1678:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.148     atatat   1679:                       CTLTYPE_INT, "log_changeopt",
                   1680:                       SYSCTL_DESCR("Log changes in optimization strategy"),
1.130     atatat   1681:                       NULL, 0, &ffs_log_changeopt, 0,
                   1682:                       CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL);
1.1       mycroft  1683: }
                   1684:
                   1685: /*
                   1686:  * Write a superblock and associated information back to disk.
                   1687:  */
                   1688: int
1.166     thorpej  1689: ffs_sbupdate(struct ufsmount *mp, int waitfor)
1.1       mycroft  1690: {
1.61      augustss 1691:        struct fs *fs = mp->um_fs;
                   1692:        struct buf *bp;
1.110     fvdl     1693:        int error = 0;
                   1694:        u_int32_t saveflag;
1.34      bouyer   1695:
1.110     fvdl     1696:        bp = getblk(mp->um_devvp,
                   1697:            fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb),
1.34      bouyer   1698:            (int)fs->fs_sbsize, 0, 0);
1.55      fvdl     1699:        saveflag = fs->fs_flags & FS_INTERNAL;
                   1700:        fs->fs_flags &= ~FS_INTERNAL;
1.161     perry    1701:
1.42      perry    1702:        memcpy(bp->b_data, fs, fs->fs_sbsize);
1.110     fvdl     1703:
                   1704:        ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1.34      bouyer   1705: #ifdef FFS_EI
                   1706:        if (mp->um_flags & UFS_NEEDSWAP)
1.123     enami    1707:                ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1.111     fvdl     1708: #endif
1.55      fvdl     1709:        fs->fs_flags |= saveflag;
1.34      bouyer   1710:
1.1       mycroft  1711:        if (waitfor == MNT_WAIT)
                   1712:                error = bwrite(bp);
                   1713:        else
                   1714:                bawrite(bp);
1.15      mycroft  1715:        return (error);
                   1716: }
                   1717:
                   1718: int
1.166     thorpej  1719: ffs_cgupdate(struct ufsmount *mp, int waitfor)
1.15      mycroft  1720: {
1.61      augustss 1721:        struct fs *fs = mp->um_fs;
                   1722:        struct buf *bp;
1.15      mycroft  1723:        int blks;
1.84      lukem    1724:        void *space;
1.15      mycroft  1725:        int i, size, error = 0, allerror = 0;
                   1726:
                   1727:        allerror = ffs_sbupdate(mp, waitfor);
1.1       mycroft  1728:        blks = howmany(fs->fs_cssize, fs->fs_fsize);
1.84      lukem    1729:        space = fs->fs_csp;
1.1       mycroft  1730:        for (i = 0; i < blks; i += fs->fs_frag) {
                   1731:                size = fs->fs_bsize;
                   1732:                if (i + fs->fs_frag > blks)
                   1733:                        size = (blks - i) * fs->fs_fsize;
                   1734:                bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
                   1735:                    size, 0, 0);
1.34      bouyer   1736: #ifdef FFS_EI
                   1737:                if (mp->um_flags & UFS_NEEDSWAP)
                   1738:                        ffs_csum_swap((struct csum*)space,
1.38      kleink   1739:                            (struct csum*)bp->b_data, size);
1.34      bouyer   1740:                else
                   1741: #endif
1.42      perry    1742:                        memcpy(bp->b_data, space, (u_int)size);
1.84      lukem    1743:                space = (char *)space + size;
1.1       mycroft  1744:                if (waitfor == MNT_WAIT)
                   1745:                        error = bwrite(bp);
                   1746:                else
                   1747:                        bawrite(bp);
                   1748:        }
1.15      mycroft  1749:        if (!allerror && error)
                   1750:                allerror = error;
                   1751:        return (allerror);
1.1       mycroft  1752: }
1.170     thorpej  1753:
                   1754: int
                   1755: ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
1.177     christos 1756:     int attrnamespace, const char *attrname, struct lwp *l)
1.170     thorpej  1757: {
                   1758: #ifdef UFS_EXTATTR
                   1759:        /*
                   1760:         * File-backed extended attributes are only supported on UFS1.
                   1761:         * UFS2 has native extended attributes.
                   1762:         */
                   1763:        if (VFSTOUFS(mp)->um_fstype == UFS1)
                   1764:                return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname,
1.177     christos 1765:                                       l));
1.170     thorpej  1766: #endif
1.177     christos 1767:        return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname, l));
1.170     thorpej  1768: }
1.193     hannken  1769:
                   1770: int
                   1771: ffs_suspendctl(struct mount *mp, int cmd)
                   1772: {
                   1773:        int error;
                   1774:        struct lwp *l = curlwp;
                   1775:
                   1776:        switch (cmd) {
                   1777:        case SUSPEND_SUSPEND:
1.194     hannken  1778:                if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
1.193     hannken  1779:                        return error;
                   1780:                error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred, l);
                   1781:                if (error == 0)
1.194     hannken  1782:                        error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
1.193     hannken  1783:                if (error != 0) {
1.194     hannken  1784:                        (void) fstrans_setstate(mp, FSTRANS_NORMAL);
1.193     hannken  1785:                        return error;
                   1786:                }
                   1787:                return 0;
                   1788:
                   1789:        case SUSPEND_RESUME:
1.194     hannken  1790:                return fstrans_setstate(mp, FSTRANS_NORMAL);
1.193     hannken  1791:
                   1792:        default:
                   1793:                return EINVAL;
                   1794:        }
                   1795: }

CVSweb <webmaster@jp.NetBSD.org>