Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/makefs Refactor the filesystem specific portions ou...



details:   https://anonhg.NetBSD.org/src/rev/8ab34f6ac7d4
branches:  trunk
changeset: 572123:8ab34f6ac7d4
user:      jmc <jmc%NetBSD.org@localhost>
date:      Mon Dec 20 20:51:42 2004 +0000

description:
Refactor the filesystem specific portions out of makefs.c/makefs.h completely.
Instead of extending fsinfo_t it now holds a void * to file system specific
data. This is then setup/cleaned up by the additional of 2 additional
callbacks. Makes adding new filesystems simpler as almost no code has
to be updated in the generic makefs code now.

diffstat:

 usr.sbin/makefs/README     |   19 ++++-
 usr.sbin/makefs/ffs.c      |  166 +++++++++++++++++++++++++++++---------------
 usr.sbin/makefs/ffs.h      |   64 +++++++++++++++++
 usr.sbin/makefs/ffs/mkfs.c |   30 ++++---
 usr.sbin/makefs/makefs.c   |   27 +++---
 usr.sbin/makefs/makefs.h   |   26 +-----
 6 files changed, 222 insertions(+), 110 deletions(-)

diffs (truncated from 686 to 300 lines):

diff -r 93f026df450b -r 8ab34f6ac7d4 usr.sbin/makefs/README
--- a/usr.sbin/makefs/README    Mon Dec 20 18:42:03 2004 +0000
+++ b/usr.sbin/makefs/README    Mon Dec 20 20:51:42 2004 +0000
@@ -1,4 +1,4 @@
-$NetBSD: README,v 1.2 2004/05/31 22:21:12 lukem Exp $
+$NetBSD: README,v 1.3 2004/12/20 20:51:42 jmc Exp $
 
 makefs - build a file system image from a directory tree
 
@@ -76,9 +76,15 @@
 
 Each fs-specific module should have the following external interfaces:
 
+    prepare_options    optional file system specific defaults that need to be
+                       setup before parsing fs-specific options.
+
     parse_options      parse the string for fs-specific options, feeding
                        errors back to the user as appropriate
 
+    cleanup_options    optional file system specific data that need to be
+                       cleaned up when done with this filesystem.
+
     make_fs            take the data structures representing the
                        directory tree and fs parameters,
                        validate that the parameters are valid
@@ -86,6 +92,17 @@
                        create the image, and
                        populate the image
 
+prepare_options and cleanup_options are optional and can be NULL.
+
+NOTE: All file system specific options are referenced via the fs_specific
+pointer from the fsinfo_t strucutre. It is up to the filesystem to allocate
+and free any data needed for this via the prepare and cleanup callbacks.
+
+Each fs-specific module will need to add it's routines to the dispatch array
+in makefs.c and add prototypes for these to makefs.h
+
+All other implementation details should not need to change any of the
+generic code.
 
 ffs implementation
 ------------------
diff -r 93f026df450b -r 8ab34f6ac7d4 usr.sbin/makefs/ffs.c
--- a/usr.sbin/makefs/ffs.c     Mon Dec 20 18:42:03 2004 +0000
+++ b/usr.sbin/makefs/ffs.c     Mon Dec 20 20:51:42 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ffs.c,v 1.32 2004/10/12 03:28:30 jmc Exp $     */
+/*     $NetBSD: ffs.c,v 1.33 2004/12/20 20:51:42 jmc Exp $     */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -71,7 +71,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: ffs.c,v 1.32 2004/10/12 03:28:30 jmc Exp $");
+__RCSID("$NetBSD: ffs.c,v 1.33 2004/12/20 20:51:42 jmc Exp $");
 #endif /* !__lint */
 
 #include <sys/param.h>
@@ -90,6 +90,7 @@
 #include <unistd.h>
 
 #include "makefs.h"
+#include "ffs.h"
 
 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
 #include <sys/statvfs.h>
@@ -106,7 +107,7 @@
 
 #undef DIP
 #define DIP(dp, field) \
-       ((fsopts->version == 1) ? \
+       ((ffs_opts->version == 1) ? \
        (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
 
 /*
@@ -151,29 +152,61 @@
 
        /* publically visible functions */
 
+void
+ffs_prep_opts(fsinfo_t *fsopts)
+{
+       ffs_opt_t *ffs_opts;
+
+       if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL)
+               err(1, "Allocating memory for ffs_options");
+
+       fsopts->fs_specific = ffs_opts;
+
+       ffs_opts->bsize= -1;
+       ffs_opts->fsize= -1;
+       ffs_opts->cpg= -1;
+       ffs_opts->density= -1;
+       ffs_opts->minfree= -1;
+       ffs_opts->optimization= -1;
+       ffs_opts->maxcontig= -1;
+       ffs_opts->maxbpg= -1;
+       ffs_opts->avgfilesize= -1;
+       ffs_opts->avgfpdir= -1;
+       ffs_opts->version = 1;
+}
+
+void
+ffs_cleanup_opts(fsinfo_t *fsopts)
+{
+       if (fsopts->fs_specific)
+               free(fsopts->fs_specific);
+}
+
 int
 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
 {
+       ffs_opt_t       *ffs_opts = fsopts->fs_specific;
+
        option_t ffs_options[] = {
-               { "bsize",      &fsopts->bsize,         1,      INT_MAX,
+               { "bsize",      &ffs_opts->bsize,       1,      INT_MAX,
                                        "block size" },
-               { "fsize",      &fsopts->fsize,         1,      INT_MAX,
+               { "fsize",      &ffs_opts->fsize,       1,      INT_MAX,
                                        "fragment size" },
-               { "density",    &fsopts->density,       1,      INT_MAX,
+               { "density",    &ffs_opts->density,     1,      INT_MAX,
                                        "bytes per inode" },
-               { "minfree",    &fsopts->minfree,       0,      99,
+               { "minfree",    &ffs_opts->minfree,     0,      99,
                                        "minfree" },
-               { "maxbpf",     &fsopts->maxbpg,        1,      INT_MAX,
+               { "maxbpf",     &ffs_opts->maxbpg,      1,      INT_MAX,
                                        "max blocks per file in a cg" },
-               { "avgfilesize", &fsopts->avgfilesize,  1,      INT_MAX,
+               { "avgfilesize", &ffs_opts->avgfilesize,1,      INT_MAX,
                                        "expected average file size" },
-               { "avgfpdir",   &fsopts->avgfpdir,      1,      INT_MAX,
+               { "avgfpdir",   &ffs_opts->avgfpdir,    1,      INT_MAX,
                                        "expected # of files per directory" },
-               { "extent",     &fsopts->maxbsize,      1,      INT_MAX,
+               { "extent",     &ffs_opts->maxbsize,    1,      INT_MAX,
                                        "maximum # extent size" },
-               { "maxbpcg",    &fsopts->maxblkspercg,  1,      INT_MAX,
+               { "maxbpcg",    &ffs_opts->maxblkspercg,1,      INT_MAX,
                                        "max # of blocks per group" },
-               { "version",    &fsopts->version,       1,      2,
+               { "version",    &ffs_opts->version,     1,      2,
                                        "UFS version" },
                { NULL }
        };
@@ -181,9 +214,10 @@
        char    *var, *val;
        int     rv;
 
-       (void)&ffs_options;
        assert(option != NULL);
        assert(fsopts != NULL);
+       assert(ffs_opts != NULL);
+       (void)&ffs_options;
 
        if (debug & DEBUG_FS_PARSE_OPTS)
                printf("ffs_parse_opts: got `%s'\n", option);
@@ -200,9 +234,9 @@
 
        if (strcmp(var, "optimization") == 0) {
                if (strcmp(val, "time") == 0) {
-                       fsopts->optimization = FS_OPTTIME;
+                       ffs_opts->optimization = FS_OPTTIME;
                } else if (strcmp(val, "space") == 0) {
-                       fsopts->optimization = FS_OPTSPACE;
+                       ffs_opts->optimization = FS_OPTSPACE;
                } else {
                        warnx("Invalid optimization `%s'", val);
                        goto leave_ffs_parse_opts;
@@ -289,10 +323,12 @@
 #if notyet
        int32_t spc, nspf, ncyl, fssize;
 #endif
+       ffs_opt_t       *ffs_opts = fsopts->fs_specific;
 
        assert(dir != NULL);
        assert(root != NULL);
        assert(fsopts != NULL);
+       assert(ffs_opts != NULL);
 
        if (debug & DEBUG_FS_VALIDATE) {
                printf("ffs_validate: before defaults set:\n");
@@ -302,31 +338,31 @@
                /* set FFS defaults */
        if (fsopts->sectorsize == -1)
                fsopts->sectorsize = DFL_SECSIZE;
-       if (fsopts->fsize == -1)
-               fsopts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
-       if (fsopts->bsize == -1)
-               fsopts->bsize = MIN(DFL_BLKSIZE, 8 * fsopts->fsize);
-       if (fsopts->cpg == -1)
-               fsopts->cpg = DFL_CYLSPERGROUP;
+       if (ffs_opts->fsize == -1)
+               ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
+       if (ffs_opts->bsize == -1)
+               ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
+       if (ffs_opts->cpg == -1)
+               ffs_opts->cpg = DFL_CYLSPERGROUP;
        else
-               fsopts->cpgflg = 1;
+               ffs_opts->cpgflg = 1;
                                /* fsopts->density is set below */
-       if (fsopts->nsectors == -1)
-               fsopts->nsectors = DFL_NSECTORS;
-       if (fsopts->minfree == -1)
-               fsopts->minfree = MINFREE;
-       if (fsopts->optimization == -1)
-               fsopts->optimization = DEFAULTOPT;
-       if (fsopts->maxcontig == -1)
-               fsopts->maxcontig =
-                   MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / fsopts->bsize);
+       if (ffs_opts->nsectors == -1)
+               ffs_opts->nsectors = DFL_NSECTORS;
+       if (ffs_opts->minfree == -1)
+               ffs_opts->minfree = MINFREE;
+       if (ffs_opts->optimization == -1)
+               ffs_opts->optimization = DEFAULTOPT;
+       if (ffs_opts->maxcontig == -1)
+               ffs_opts->maxcontig =
+                   MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
        /* XXX ondisk32 */
-       if (fsopts->maxbpg == -1)
-               fsopts->maxbpg = fsopts->bsize / sizeof(int32_t);
-       if (fsopts->avgfilesize == -1)
-               fsopts->avgfilesize = AVFILESIZ;
-       if (fsopts->avgfpdir == -1)
-               fsopts->avgfpdir = AFPDIR;
+       if (ffs_opts->maxbpg == -1)
+               ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
+       if (ffs_opts->avgfilesize == -1)
+               ffs_opts->avgfilesize = AVFILESIZ;
+       if (ffs_opts->avgfpdir == -1)
+               ffs_opts->avgfpdir = AFPDIR;
 
                /* calculate size of tree */
        ffs_size_dir(root, fsopts);
@@ -354,17 +390,19 @@
         */
        fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
                /* add space needed to store inodes, x3 for blockmaps, etc */
-       if (fsopts->version == 1)
+       if (ffs_opts->version == 1)
                fsopts->size += ncg * DINODE1_SIZE *
-                   roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE1_SIZE);
+                   roundup(fsopts->inodes / ncg, 
+                       ffs_opts->bsize / DINODE1_SIZE);
        else
                fsopts->size += ncg * DINODE2_SIZE *
-                   roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE2_SIZE);
+                   roundup(fsopts->inodes / ncg, 
+                       ffs_opts->bsize / DINODE2_SIZE);
 
                /* add minfree */
-       if (fsopts->minfree > 0)
+       if (ffs_opts->minfree > 0)
                fsopts->size =
-                   fsopts->size * (100 + fsopts->minfree) / 100;
+                   fsopts->size * (100 + ffs_opts->minfree) / 100;
        /*
         * XXX  any other fs slop to add, such as csum's, bitmaps, etc ??
         */
@@ -373,11 +411,11 @@
                fsopts->size = fsopts->minsize;
 
                /* round up to the next block */
-       fsopts->size = roundup(fsopts->size, fsopts->bsize);
+       fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
 
                /* calculate density if necessary */
-       if (fsopts->density == -1)
-               fsopts->density = fsopts->size / fsopts->inodes + 1;
+       if (ffs_opts->density == -1)
+               ffs_opts->density = fsopts->size / fsopts->inodes + 1;
 
        if (debug & DEBUG_FS_VALIDATE) {
                printf("ffs_validate: after defaults set:\n");
@@ -399,6 +437,8 @@
 ffs_dump_fsinfo(fsinfo_t *f)
 {
 
+       ffs_opt_t       *fs = f->fs_specific;
+
        printf("fsopts at %p\n", f);
 
        printf("\tsize %lld, inodes %lld, curinode %u\n",
@@ -413,13 +453,13 @@
        printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
 
        printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
-           f->bsize, f->fsize, f->cpg, f->density);
+           fs->bsize, fs->fsize, fs->cpg, fs->density);
        printf("\tnsectors %d, rpm %d, minfree %d\n",
-           f->nsectors, f->rpm, f->minfree);
+           fs->nsectors, fs->rpm, fs->minfree);
        printf("\tmaxcontig %d, maxbpg %d\n",



Home | Main Index | Thread Index | Old Index