[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.151

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

CVSweb <webmaster@jp.NetBSD.org>