sun
[unix-history] / usr / src / sbin / newfs / mkfs.c
index 23b0fd7..81e78a5 100644 (file)
-static char *sccsid = "@(#)mkfs.c      1.9 (Berkeley) %G%";
+static char *sccsid = "@(#)mkfs.c      2.6 (Berkeley) %G%";
 
 /*
  * make file system for cylinder-group style file systems
  *
 
 /*
  * make file system for cylinder-group style file systems
  *
- * usage: mkfs fs proto
- * or: mkfs size [ nsect ntrak cpg ]
+ * usage: mkfs special size [ nsect ntrak bsize fsize cpg ]
  */
 
  */
 
-#define        NDIRECT (BSIZE/sizeof(struct direct))
-#define        MAXFN   500
+/*
+ * The following constants set the defaults used for the number
+ * of sectors (fs_nsect), and number of tracks (fs_ntrak).
+ */
+#define DFLNSECT       32
+#define DFLNTRAK       16
+
+/*
+ * The following two constants set the default block and fragment sizes.
+ * Both constants must be a power of 2 and meet the following constraints:
+ *     MINBSIZE <= DESBLKSIZE <= MAXBSIZE
+ *     DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
+ *     DESBLKSIZE / DESFRAGSIZE <= 8
+ */
+#define DESBLKSIZE     8192
+#define DESFRAGSIZE    1024
+
+/*
+ * Cylinder groups may have up to MAXCPG cylinders. The actual
+ * number used depends upon how much information can be stored
+ * on a single cylinder. The default is to used 16 cylinders
+ * per group.
+ */
+#define        DESCPG          16      /* desired fs_cpg */
+
+/*
+ * MINFREE gives the minimum acceptable percentage of file system
+ * blocks which may be free. If the freelist drops below this level
+ * only the superuser may continue to allocate blocks. This may
+ * be set to 0 if no reserve of free blocks is deemed necessary,
+ * however throughput drops by fifty percent if the file system
+ * is run at between 90% and 100% full; thus the default value of
+ * fs_minfree is 10%.
+ */
+#define MINFREE                10
+
+/*
+ * ROTDELAY gives the minimum number of milliseconds to initiate
+ * another disk transfer on the same cylinder. It is used in
+ * determining the rotationally optimal layout for disk blocks
+ * within a file; the default of fs_rotdelay is 2ms.
+ */
+#define ROTDELAY       2
+
+/*
+ * MAXCONTIG sets the default for the maximum number of blocks
+ * that may be allocated sequentially. Since UNIX drivers are
+ * not capable of scheduling multi-block transfers, this defaults
+ * to 1 (ie no contiguous blocks are allocated).
+ */
+#define MAXCONTIG      1
+
+/*
+ * MAXBLKPG determines the maximum number of data blocks which are
+ * placed in a single cylinder group. This is currently a function
+ * of the block and fragment size of the file system.
+ */
+#define MAXBLKPG(fs)   ((fs)->fs_fsize / sizeof(daddr_t))
+
+/*
+ * Each file system has a number of inodes statically allocated.
+ * We allocate one inode slot per NBPI bytes, expecting this
+ * to be far more than we will ever need.
+ */
+#define        NBPI            2048
 
 #ifndef STANDALONE
 #include <stdio.h>
 #include <a.out.h>
 #endif
 
 
 #ifndef STANDALONE
 #include <stdio.h>
 #include <a.out.h>
 #endif
 
+#ifndef SIMFS
+#include <sys/param.h>
+#include <sys/inode.h>
+#include <sys/fs.h>
+#else
 #include "../h/param.h"
 #include "../h/inode.h"
 #include "../h/fs.h"
 #include "../h/param.h"
 #include "../h/inode.h"
 #include "../h/fs.h"
-#include "../h/dir.h"
-
-time_t utime;
-
-#ifndef STANDALONE
-FILE   *fin;
-#else
-int    fin;
 #endif
 #endif
+#include <dir.h>
 
 
-int    fsi;
-int    fso;
-char   *charp;
-char   buf[BSIZE];
-#ifndef STANDALONE
-struct exec head;
-#endif
-char   string[50];
+#define UMASK          0755
+#define MAXINOPB       (MAXBSIZE / sizeof(struct dinode))
+#define POWEROF2(num)  (((num) & ((num) - 1)) == 0)
 
 union {
        struct fs fs;
 
 union {
        struct fs fs;
-       char pad[BSIZE];
+       char pad[MAXBSIZE];
 } fsun;
 #define        sblock  fsun.fs
 struct csum *fscs;
 
 union {
        struct cg cg;
 } fsun;
 #define        sblock  fsun.fs
 struct csum *fscs;
 
 union {
        struct cg cg;
-       char pad[BSIZE];
+       char pad[MAXBSIZE];
 } cgun;
 #define        acg     cgun.cg
 
 } cgun;
 #define        acg     cgun.cg
 
-#define        howmany(x, y)   (((x)+((y)-1))/(y))
-#define        roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
+struct dinode zino[MAXIPG];
 
 char   *fsys;
 
 char   *fsys;
-char   *proto;
-int    error;
-ino_t  ino = ROOTINO - 1;
-long   getnum();
+time_t utime;
+int    fsi;
+int    fso;
 daddr_t        alloc();
 
 daddr_t        alloc();
 
-struct dinode zino[MAXIPG];
-
 main(argc, argv)
 main(argc, argv)
-char *argv[];
+       int argc;
+       char *argv[];
 {
 {
-       int f, c;
-       long i,n;
+       long cylno, rpos, blk, i, j, inos, fssize, warn = 0;
 
 
-       argc--, argv++;
 #ifndef STANDALONE
 #ifndef STANDALONE
+       argc--, argv++;
        time(&utime);
        time(&utime);
-       if(argc < 2) {
-               printf("usage: mkfs sblock proto/size [ nsect ntrak cpg ]\n");
+       if (argc < 2) {
+               printf("usage: mkfs special size [ nsect ntrak bsize fsize cpg ]\n");
                exit(1);
        }
        fsys = argv[0];
                exit(1);
        }
        fsys = argv[0];
-       proto = argv[1];
+       fssize = atoi(argv[1]);
+       fso = creat(fsys, 0666);
+       if(fso < 0) {
+               printf("%s: cannot create\n", fsys);
+               exit(1);
+       }
+       fsi = open(fsys, 0);
+       if(fsi < 0) {
+               printf("%s: cannot open\n", fsys);
+               exit(1);
+       }
 #else
        {
                static char protos[60];
 #else
        {
                static char protos[60];
+               char fsbuf[100];
 
                printf("file sys size: ");
                gets(protos);
 
                printf("file sys size: ");
                gets(protos);
-               proto = protos;
-       }
-#endif
-#ifdef STANDALONE
-       {
-               char fsbuf[100];
-
+               fssize = atoi(protos);
                do {
                        printf("file system: ");
                        gets(fsbuf);
                do {
                        printf("file system: ");
                        gets(fsbuf);
@@ -97,229 +153,334 @@ char *argv[];
                        fsi = open(fsbuf, 0);
                } while (fso < 0 || fsi < 0);
        }
                        fsi = open(fsbuf, 0);
                } while (fso < 0 || fsi < 0);
        }
-       fin = NULL;
        argc = 0;
        argc = 0;
-#else
-       fso = creat(fsys, 0666);
-       if(fso < 0) {
-               printf("%s: cannot create\n", fsys);
+#endif
+       if (fssize <= 0)
+               printf("preposterous size %d\n", fssize), exit(1);
+       /*
+        * collect and verify the sector and track info
+        */
+       if (argc > 2)
+               sblock.fs_nsect = atoi(argv[2]);
+       else
+               sblock.fs_nsect = DFLNSECT;
+       if (argc > 3)
+               sblock.fs_ntrak = atoi(argv[3]);
+       else
+               sblock.fs_ntrak = DFLNTRAK;
+       if (sblock.fs_ntrak <= 0)
+               printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(1);
+       if (sblock.fs_nsect <= 0)
+               printf("preposterous nsect %d\n", sblock.fs_nsect), exit(1);
+       sblock.fs_spc = sblock.fs_ntrak * sblock.fs_nsect;
+       /*
+        * collect and verify the block and fragment sizes
+        */
+       if (argc > 4)
+               sblock.fs_bsize = atoi(argv[4]);
+       else
+               sblock.fs_bsize = DESBLKSIZE;
+       if (argc > 5)
+               sblock.fs_fsize = atoi(argv[5]);
+       else
+               sblock.fs_fsize = DESFRAGSIZE;
+       if (!POWEROF2(sblock.fs_bsize)) {
+               printf("block size must be a power of 2, not %d\n",
+                   sblock.fs_bsize);
                exit(1);
        }
                exit(1);
        }
-       fsi = open(fsys, 0);
-       if(fsi < 0) {
-               printf("%s: cannot open\n", fsys);
+       if (!POWEROF2(sblock.fs_fsize)) {
+               printf("fragment size must be a power of 2, not %d\n",
+                   sblock.fs_fsize);
                exit(1);
        }
                exit(1);
        }
-       fin = fopen(proto, "r");
-#endif
-#ifndef STANDALONE
-       if (fin != NULL) {
-               getstr();
-               f = open(string, 0);
-               if (f < 0) {
-                       printf("%s: cannot open init\n", string);
-                       goto noinit;
-               }
-               read(f, (char *)&head, sizeof head);
-               c = head.a_text + head.a_data;
-               if (c > BSIZE)
-                       printf("%s: too big\n", string);
-               else {
-                       read(f, buf, c);
-                       wtfs(BBLOCK, BSIZE, buf);
-               }
-               close(f);
-noinit:
-               n = sblock.fs_size = getnum();
-               sblock.fs_ntrak = getnum();
-               sblock.fs_nsect = getnum();
-               sblock.fs_cpg = getnum();
-       } else
-#endif
-       {
-               charp = "d--777 0 0 $ ";
-               n = 0;
-               for (f=0; c=proto[f]; f++) {
-                       if (c<'0' || c>'9') {
-                               printf("%s: cannot open\n", proto);
-                               exit(1);
-                       }
-                       n = n*10 + (c-'0');
+       if (sblock.fs_fsize < DEV_BSIZE) {
+               printf("fragment size %d is too small, minimum is %d\n",
+                   sblock.fs_fsize, DEV_BSIZE);
+               exit(1);
+       }
+       if (sblock.fs_bsize < MINBSIZE) {
+               printf("block size %d is too small, minimum is %d\n",
+                   sblock.fs_bsize, MINBSIZE);
+               exit(1);
+       }
+       if (sblock.fs_bsize < sblock.fs_fsize) {
+               printf("block size (%d) cannot be smaller than fragment size (%d)\n",
+                   sblock.fs_bsize, sblock.fs_fsize);
+               exit(1);
+       }
+       sblock.fs_bmask = ~(sblock.fs_bsize - 1);
+       sblock.fs_fmask = ~(sblock.fs_fsize - 1);
+       for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
+               sblock.fs_bshift++;
+       for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
+               sblock.fs_fshift++;
+       sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
+       if (sblock.fs_frag > MAXFRAG) {
+               printf("fragment size %d is too small, minimum with block size %d is %d\n",
+                   sblock.fs_fsize, sblock.fs_bsize,
+                   sblock.fs_bsize / MAXFRAG);
+               exit(1);
+       }
+       sblock.fs_sblkno =
+           roundup(howmany(BBSIZE + SBSIZE, sblock.fs_fsize), sblock.fs_frag);
+       sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
+           roundup(howmany(SBSIZE, sblock.fs_fsize), sblock.fs_frag));
+       sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
+       sblock.fs_cgoffset = roundup(
+           howmany(sblock.fs_nsect, sblock.fs_fsize / DEV_BSIZE),
+           sblock.fs_frag);
+       for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
+               sblock.fs_cgmask <<= 1;
+       if (!POWEROF2(sblock.fs_ntrak))
+               sblock.fs_cgmask <<= 1;
+       for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
+            sblock.fs_cpc > 1 && (i & 1) == 0;
+            sblock.fs_cpc >>= 1, i >>= 1)
+               /* void */;
+       if (sblock.fs_cpc > MAXCPG) {
+               printf("maximum block size with nsect %d and ntrak %d is %d\n",
+                   sblock.fs_nsect, sblock.fs_ntrak,
+                   sblock.fs_bsize / (sblock.fs_cpc / MAXCPG));
+               exit(1);
+       }
+       /* 
+        * collect and verify the number of cylinders per group
+        */
+       if (argc > 6) {
+               sblock.fs_cpg = atoi(argv[6]);
+               sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
+       } else {
+               sblock.fs_cpg = MAX(sblock.fs_cpc, DESCPG);
+               sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
+               while (sblock.fs_fpg / sblock.fs_frag > MAXBPG(&sblock) &&
+                   sblock.fs_cpg > sblock.fs_cpc) {
+                       sblock.fs_cpg -= sblock.fs_cpc;
+                       sblock.fs_fpg =
+                           (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
                }
                }
-               sblock.fs_size = n;
-               if (argc > 2)
-                       sblock.fs_nsect = atoi(argv[2]);
-               else
-                       sblock.fs_nsect = 32;
-               if (argc > 3)
-                       sblock.fs_ntrak = atoi(argv[3]);
-               else
-                       sblock.fs_ntrak = 19;
+       }
+       if (sblock.fs_cpg < 1) {
+               printf("cylinder groups must have at least 1 cylinder\n");
+               exit(1);
+       }
+       if (sblock.fs_cpg > MAXCPG) {
+               printf("cylinder groups are limited to %d cylinders\n", MAXCPG);
+               exit(1);
+       }
+       if (sblock.fs_cpg % sblock.fs_cpc != 0) {
+               printf("cylinder groups must have a multiple of %d cylinders\n",
+                   sblock.fs_cpc);
+               exit(1);
        }
        /*
         * Now have size for file system and nsect and ntrak.
        }
        /*
         * Now have size for file system and nsect and ntrak.
-        * (And, if coming from prototype, cpg).
-        * Determine number of cylinders occupied by file system.
+        * Determine number of cylinders and blocks in the file system.
         */
         */
-       if (sblock.fs_ntrak <= 0)
-               printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(1);
-       if (sblock.fs_nsect <= 0)
-               printf("preposterous nsect %d\n", sblock.fs_nsect), exit(1);
-       if (sblock.fs_size <= 0)
-               printf("preposterous size %d\n", sblock.fs_size), exit(1);
-       if (sblock.fs_ntrak * sblock.fs_nsect > MAXBPG * NSPB) {
-               printf("cylinder too large (%d sectors)\n", 
-                   sblock.fs_ntrak * sblock.fs_nsect);
-               printf("maximum cylinder size: %d sectors\n",
-                   MAXBPG * NSPB);
+       sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
+       sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
+       if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
+               sblock.fs_ncyl++;
+               warn = 1;
+       }
+       if (sblock.fs_ncyl < 1) {
+               printf("file systems must have at least one cylinder\n");
                exit(1);
        }
                exit(1);
        }
-       sblock.fs_ncyl = n * NSPF / (sblock.fs_nsect * sblock.fs_ntrak);
-       if (n * NSPF > sblock.fs_ncyl * sblock.fs_nsect * sblock.fs_ntrak) {
-               printf("%d sector(s) in last cylinder unused\n",
-                   n * NSPF - sblock.fs_ncyl * sblock.fs_nsect * sblock.fs_ntrak);
-               sblock.fs_ncyl++;
+       /*
+        * determine feasability/values of rotational layout tables
+        */
+       if (sblock.fs_ntrak == 1) {
+               sblock.fs_cpc = 0;
+               goto next;
+       }
+       if (sblock.fs_spc * sblock.fs_cpc > MAXBPC * NSPB(&sblock) ||
+           sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
+               printf("%s %s %d %s %d.%s",
+                   "Warning: insufficient space in super block for\n",
+                   "rotational layout tables with nsect", sblock.fs_nsect,
+                   "and ntrak", sblock.fs_ntrak,
+                   "\nFile system performance may be impared.\n");
+               sblock.fs_cpc = 0;
+               goto next;
        }
        }
-       sblock.fs_magic = FS_MAGIC;
+       /*
+        * calculate the available blocks for each rotational position
+        */
+       for (cylno = 0; cylno < MAXCPG; cylno++)
+               for (rpos = 0; rpos < NRPOS; rpos++)
+                       sblock.fs_postbl[cylno][rpos] = -1;
+       blk = sblock.fs_spc * sblock.fs_cpc / NSPF(&sblock);
+       for (i = 0; i < blk; i += sblock.fs_frag)
+               /* void */;
+       for (i -= sblock.fs_frag; i >= 0; i -= sblock.fs_frag) {
+               cylno = cbtocylno(&sblock, i);
+               rpos = cbtorpos(&sblock, i);
+               blk = i / sblock.fs_frag;
+               if (sblock.fs_postbl[cylno][rpos] == -1)
+                       sblock.fs_rotbl[blk] = 0;
+               else
+                       sblock.fs_rotbl[blk] =
+                           sblock.fs_postbl[cylno][rpos] - blk;
+               sblock.fs_postbl[cylno][rpos] = blk;
+       }
+next:
        /*
         * Validate specified/determined cpg.
         */
        /*
         * Validate specified/determined cpg.
         */
-#define        CGTOOBIG(fs)    ((fs).fs_nsect*(fs).fs_ntrak*(fs).fs_cpg/NSPB > MAXBPG)
-       if (argc > 4 || fin) {
-               if (fin == NULL)
-                       sblock.fs_cpg = atoi(argv[4]);
-               if (CGTOOBIG(sblock)) {
-                       printf("cylinder group too large (%d blocks); ",
-                           sblock.fs_cpg * sblock.fs_nsect * sblock.fs_ntrak / NSPB);
-                       printf("max: %d blocks\n", MAXBPG);
-                       exit(1);
-               }
-               if (sblock.fs_cpg > MAXCPG) {
-                       printf("cylinder groups are limited to %d cylinders\n",
-                           MAXCPG);
-                       exit(1);
+       if (sblock.fs_spc > MAXBPG(&sblock) * NSPB(&sblock)) {
+               printf("too many sectors per cylinder (%d sectors)\n",
+                   sblock.fs_spc);
+               while(sblock.fs_spc > MAXBPG(&sblock) * NSPB(&sblock)) {
+                       sblock.fs_bsize <<= 1;
+                       if (sblock.fs_frag < MAXFRAG)
+                               sblock.fs_frag <<= 1;
+                       else
+                               sblock.fs_fsize <<= 1;
                }
                }
-       } else {
-               sblock.fs_cpg = DESCPG;
-               while (CGTOOBIG(sblock))
-                       --sblock.fs_cpg;
+               printf("nsect %d, and ntrak %d, requires block size of %d,\n",
+                   sblock.fs_nsect, sblock.fs_ntrak, sblock.fs_bsize);
+               printf("\tand fragment size of %d\n", sblock.fs_fsize);
+               exit(1);
        }
        }
+       if (sblock.fs_fpg > MAXBPG(&sblock) * sblock.fs_frag) {
+               printf("cylinder group too large (%d cylinders); ",
+                   sblock.fs_cpg);
+               printf("max: %d cylinders per group\n",
+                   MAXBPG(&sblock) * sblock.fs_frag /
+                   (sblock.fs_fpg / sblock.fs_cpg));
+               exit(1);
+       }
+       sblock.fs_cgsize = fragroundup(&sblock,
+           sizeof(struct cg) + howmany(sblock.fs_fpg, NBBY));
        /*
         * Compute/validate number of cylinder groups.
         */
        sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
        if (sblock.fs_ncyl % sblock.fs_cpg)
                sblock.fs_ncg++;
        /*
         * Compute/validate number of cylinder groups.
         */
        sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
        if (sblock.fs_ncyl % sblock.fs_cpg)
                sblock.fs_ncg++;
-       if ((sblock.fs_nsect*sblock.fs_ntrak*sblock.fs_cpg) % NSPF) {
+       if ((sblock.fs_spc * sblock.fs_cpg) % NSPF(&sblock)) {
                printf("mkfs: nsect %d, ntrak %d, cpg %d is not tolerable\n",
                    sblock.fs_nsect, sblock.fs_ntrak, sblock.fs_cpg);
                printf("as this would would have cyl groups whose size\n");
                printf("mkfs: nsect %d, ntrak %d, cpg %d is not tolerable\n",
                    sblock.fs_nsect, sblock.fs_ntrak, sblock.fs_cpg);
                printf("as this would would have cyl groups whose size\n");
-               printf("is not a multiple of %d; choke!\n", FSIZE);
+               printf("is not a multiple of %d; choke!\n", sblock.fs_fsize);
                exit(1);
        }
                exit(1);
        }
-       fscs = (struct csum *)
-           calloc(1, roundup(sblock.fs_ncg * sizeof (struct csum), BSIZE));
        /*
         * Compute number of inode blocks per cylinder group.
         * Start with one inode per NBPI bytes; adjust as necessary.
         */
        /*
         * Compute number of inode blocks per cylinder group.
         * Start with one inode per NBPI bytes; adjust as necessary.
         */
-       n = ((n * BSIZE) / NBPI) / INOPB;
-       if (n <= 0)
-               n = 1;
-       if (n > 65500/INOPB)
-               n = 65500/INOPB;
-       sblock.fs_ipg = ((n / sblock.fs_ncg) + 1) * INOPB;
-       if (sblock.fs_ipg < INOPB)
-               sblock.fs_ipg = INOPB;
+       i = sblock.fs_iblkno + MAXIPG / INOPF(&sblock);
+       inos = (fssize - sblock.fs_ncg * i) * sblock.fs_fsize /
+           MAX(NBPI, sblock.fs_fsize) / INOPB(&sblock);
+       if (inos <= 0)
+               inos = 1;
+       sblock.fs_ipg = ((inos / sblock.fs_ncg) + 1) * INOPB(&sblock);
        if (sblock.fs_ipg > MAXIPG)
                sblock.fs_ipg = MAXIPG;
        if (sblock.fs_ipg > MAXIPG)
                sblock.fs_ipg = MAXIPG;
-       while (sblock.fs_ipg * sblock.fs_ncyl > 65500)
-               sblock.fs_ipg -= INOPB;
-       sblock.fs_spc = sblock.fs_ntrak * sblock.fs_nsect;
-       sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / (FSIZE / 512);
-       if (cgdmin(0,&sblock) >= sblock.fs_fpg)
+       sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
+       i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
+       if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
                printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
                printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
-                   cgdmin(0,&sblock)/FRAG, sblock.fs_fpg/FRAG), exit(1);
-       sblock.fs_cstotal.cs_nifree = sblock.fs_ipg * sblock.fs_ncg;
-       sblock.fs_cgsize = cgsize(&sblock);
-       sblock.fs_cssize = cssize(&sblock);
-       sblock.fs_sblkno = SBLOCK;
+                   cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
+                   sblock.fs_fpg / sblock.fs_frag);
+               printf("number of cylinders per cylinder group must be increased\n");
+               exit(1);
+       }
+       j = sblock.fs_ncg - 1;
+       if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
+           cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
+               printf("Warning: inode blocks/cyl group (%d) >= data blocks (%d) in last\n",
+                   (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
+                   i / sblock.fs_frag);
+               printf("    cylinder group. This implies %d sector(s) cannot be allocated.\n",
+                   i * NSPF(&sblock));
+               sblock.fs_ncg--;
+               sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
+               sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
+                   NSPF(&sblock);
+               warn = 0;
+       }
+       if (warn) {
+               printf("Warning: %d sector(s) in last cylinder unallocated\n",
+                   sblock.fs_spc -
+                   (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
+                   * sblock.fs_spc));
+       }
+       /*
+        * fill in remaining fields of the super block
+        */
+       sblock.fs_csaddr = cgdmin(&sblock, 0);
+       sblock.fs_cssize =
+           fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
+       fscs = (struct csum *)calloc(1, sblock.fs_cssize);
+       sblock.fs_magic = FS_MAGIC;
+       sblock.fs_rotdelay = ROTDELAY;
+       sblock.fs_minfree = MINFREE;
+       sblock.fs_maxcontig = MAXCONTIG;
+       sblock.fs_maxbpg = MAXBLKPG(&sblock);
+       sblock.fs_rps = 60;     /* assume disk speed == 60 HZ */
+       sblock.fs_cgrotor = 0;
+       sblock.fs_cstotal.cs_ndir = 0;
+       sblock.fs_cstotal.cs_nbfree = 0;
+       sblock.fs_cstotal.cs_nifree = 0;
+       sblock.fs_cstotal.cs_nffree = 0;
        sblock.fs_fmod = 0;
        sblock.fs_ronly = 0;
        sblock.fs_fmod = 0;
        sblock.fs_ronly = 0;
-
        /*
        /*
-        * Dump out information about file system.
+        * Dump out summary information about file system.
         */
        printf("%s:\t%d sectors in %d cylinders of %d tracks, %d sectors\n",
         */
        printf("%s:\t%d sectors in %d cylinders of %d tracks, %d sectors\n",
-           fsys, sblock.fs_size*NSPF, sblock.fs_ncyl, sblock.fs_ntrak, sblock.fs_nsect);
+           fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
+           sblock.fs_ntrak, sblock.fs_nsect);
        printf("\t%.1fMb in %d cyl groups (%d c/g, %.2fMb/g, %d i/g)\n",
        printf("\t%.1fMb in %d cyl groups (%d c/g, %.2fMb/g, %d i/g)\n",
-           (float)sblock.fs_size*FSIZE*1e-6, sblock.fs_ncg, sblock.fs_cpg,
-           (float)sblock.fs_fpg*FSIZE*1e-6, sblock.fs_ipg);
-/*
-       printf("%7d size (%d blocks)\n", sblock.fs_size, sblock.fs_size/FRAG);
-       printf("%7d cylinder groups\n", sblock.fs_ncg);
-       printf("%7d cylinder group block size\n", sblock.fs_cgsize);
-       printf("%7d tracks\n", sblock.fs_ntrak);
-       printf("%7d sectors\n", sblock.fs_nsect);
-       printf("%7d sectors per cylinder\n", sblock.fs_spc);
-       printf("%7d cylinders\n", sblock.fs_ncyl);
-       printf("%7d cylinders per group\n", sblock.fs_cpg);
-       printf("%7d blocks per group\n", sblock.fs_fpg/FRAG);
-       printf("%7d inodes per group\n", sblock.fs_ipg);
-       if (sblock.fs_ncyl % sblock.fs_cpg) {
-               printf("%7d cylinders in last group\n",
-                   i = sblock.fs_ncyl % sblock.fs_cpg);
-               printf("%7d blocks in last group\n",
-                   i * sblock.fs_spc / NSPB);
-       }
-*/
+           (float)sblock.fs_size * sblock.fs_fsize * 1e-6, sblock.fs_ncg,
+           sblock.fs_cpg, (float)sblock.fs_fpg * sblock.fs_fsize * 1e-6,
+           sblock.fs_ipg);
        /*
         * Now build the cylinders group blocks and
        /*
         * Now build the cylinders group blocks and
-        * then print out indices of cylinder groups forwarded
-        * past bad blocks or other obstructions.
+        * then print out indices of cylinder groups.
         */
         */
-       sblock.fs_cstotal.cs_ndir = 0;
-       sblock.fs_cstotal.cs_nbfree = 0;
-       sblock.fs_cstotal.cs_nifree = 0;
-       sblock.fs_cstotal.cs_nffree = 0;
-       sblock.fs_cgrotor = 0;
-       for (i = 0; i < NRPOS; i++)
-               sblock.fs_postbl[i] = -1;
-       for (i = 0; i < sblock.fs_spc; i += (NSPF * FRAG))
-               /* void */;
-       for (i -= (NSPF * FRAG); i >= 0; i -= (NSPF * FRAG)) {
-               c = i % sblock.fs_nsect * NRPOS / sblock.fs_nsect;
-               sblock.fs_rotbl[i / (NSPF * FRAG)] = sblock.fs_postbl[c];
-               sblock.fs_postbl[c] = i / (NSPF * FRAG);
-       }
-       for (c = 0; c < sblock.fs_ncg; c++)
-               initcg(c);
-       printf("\tsuper-block backups (for fsck -b#) at %d+k*%d (%d .. %d)\n",
-           SBLOCK, sblock.fs_fpg, SBLOCK+sblock.fs_fpg,
-           SBLOCK+(sblock.fs_ncg-1)*sblock.fs_fpg);
+       printf("super-block backups (for fsck -b#) at:");
+       for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
+               initcg(cylno);
+               if (cylno % 10 == 0)
+                       printf("\n");
+               printf(" %d,", fsbtodb(&sblock, cgsblock(&sblock, cylno)));
+       }
+       printf("\n%s\n%s\n",
+           "WRITE THESE NUMBERS DOWN!!!",
+           "fsck depends on them to recover this file system.");
        /*
        /*
-        * Now construct the initial file system, and
+        * Now construct the initial file system,
         * then write out the super-block.
         */
         * then write out the super-block.
         */
-       cfile((struct inode *)0);
+       fsinit();
        sblock.fs_time = utime;
        sblock.fs_time = utime;
-       wtfs(SBLOCK, BSIZE, (char *)&sblock);
-       for (i = 0; i < cssize(&sblock); i += BSIZE)
-               wtfs(csaddr(&sblock) + i/BSIZE, BSIZE, ((char *)fscs)+i);
-       for (c = 0; c < sblock.fs_ncg; c++)
-               wtfs(cgsblock(c, &sblock), BSIZE, (char *)&sblock);
+       wtfs(SBLOCK, SBSIZE, (char *)&sblock);
+       for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
+               wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
+                       sblock.fs_cssize - i < sblock.fs_bsize ?
+                           sblock.fs_cssize - i : sblock.fs_bsize,
+                       ((char *)fscs) + i);
+       /* 
+        * Write out the duplicate super blocks
+        */
+       for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
+               wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
+                   SBSIZE, (char *)&sblock);
 #ifndef STANDALONE
 #ifndef STANDALONE
-       exit(error);
+       exit(0);
 #endif
 }
 
 /*
  * Initialize a cylinder group.
  */
 #endif
 }
 
 /*
  * Initialize a cylinder group.
  */
-initcg(c)
-       int c;
+initcg(cylno)
+       int cylno;
 {
 {
-       daddr_t cbase, d, dmin, dmax;
+       daddr_t cbase, d, dlower, dupper, dmax;
        long i, j, s;
        register struct csum *cs;
 
        long i, j, s;
        register struct csum *cs;
 
@@ -328,311 +489,214 @@ initcg(c)
         * Allow space for super block summary information in first
         * cylinder group.
         */
         * Allow space for super block summary information in first
         * cylinder group.
         */
-       cbase = cgbase(c,&sblock);
+       cbase = cgbase(&sblock, cylno);
        dmax = cbase + sblock.fs_fpg;
        if (dmax > sblock.fs_size)
                dmax = sblock.fs_size;
        dmax = cbase + sblock.fs_fpg;
        if (dmax > sblock.fs_size)
                dmax = sblock.fs_size;
-       dmin = cgdmin(c,&sblock) - cbase;
-       d = cbase;
-       cs = fscs+c;
+       dlower = cgsblock(&sblock, cylno) - cbase;
+       dupper = cgdmin(&sblock, cylno) - cbase;
+       cs = fscs + cylno;
        acg.cg_time = utime;
        acg.cg_magic = CG_MAGIC;
        acg.cg_time = utime;
        acg.cg_magic = CG_MAGIC;
-       acg.cg_cgx = c;
-       acg.cg_ncyl = sblock.fs_cpg;
+       acg.cg_cgx = cylno;
+       if (cylno == sblock.fs_ncg - 1)
+               acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
+       else
+               acg.cg_ncyl = sblock.fs_cpg;
        acg.cg_niblk = sblock.fs_ipg;
        acg.cg_ndblk = dmax - cbase;
        acg.cg_cs.cs_ndir = 0;
        acg.cg_cs.cs_nffree = 0;
        acg.cg_cs.cs_nbfree = 0;
        acg.cg_cs.cs_nifree = 0;
        acg.cg_niblk = sblock.fs_ipg;
        acg.cg_ndblk = dmax - cbase;
        acg.cg_cs.cs_ndir = 0;
        acg.cg_cs.cs_nffree = 0;
        acg.cg_cs.cs_nbfree = 0;
        acg.cg_cs.cs_nifree = 0;
-       acg.cg_rotor = dmin;
-       acg.cg_frotor = dmin;
+       acg.cg_rotor = 0;
+       acg.cg_frotor = 0;
        acg.cg_irotor = 0;
        acg.cg_irotor = 0;
-       for (i = 0; i < FRAG; i++) {
+       for (i = 0; i < sblock.fs_frag; i++) {
                acg.cg_frsum[i] = 0;
        }
        for (i = 0; i < sblock.fs_ipg; ) {
                acg.cg_frsum[i] = 0;
        }
        for (i = 0; i < sblock.fs_ipg; ) {
-               for (j = INOPB; j > 0; j--) {
+               for (j = INOPB(&sblock); j > 0; j--) {
                        clrbit(acg.cg_iused, i);
                        i++;
                }
                        clrbit(acg.cg_iused, i);
                        i++;
                }
-               acg.cg_cs.cs_nifree += INOPB;
+               acg.cg_cs.cs_nifree += INOPB(&sblock);
        }
        }
+       if (cylno == 0)
+               for (i = 0; i < ROOTINO; i++) {
+                       setbit(acg.cg_iused, i);
+                       acg.cg_cs.cs_nifree--;
+               }
        while (i < MAXIPG) {
                clrbit(acg.cg_iused, i);
                i++;
        }
        while (i < MAXIPG) {
                clrbit(acg.cg_iused, i);
                i++;
        }
-       lseek(fso, cgimin(c,&sblock)*FSIZE, 0);
+       lseek(fso, fsbtodb(&sblock, cgimin(&sblock, cylno)) * DEV_BSIZE, 0);
        if (write(fso, (char *)zino, sblock.fs_ipg * sizeof (struct dinode)) !=
            sblock.fs_ipg * sizeof (struct dinode))
        if (write(fso, (char *)zino, sblock.fs_ipg * sizeof (struct dinode)) !=
            sblock.fs_ipg * sizeof (struct dinode))
-               printf("write error %D\n", tell(fso) / BSIZE);
-       for (i = 0; i < MAXCPG; i++)
+               printf("write error %D\n", numfrags(&sblock, tell(fso)));
+       for (i = 0; i < MAXCPG; i++) {
+               acg.cg_btot[i] = 0;
                for (j = 0; j < NRPOS; j++)
                        acg.cg_b[i][j] = 0;
                for (j = 0; j < NRPOS; j++)
                        acg.cg_b[i][j] = 0;
-       if (c == 0) {
-               dmin += howmany(cssize(&sblock), BSIZE) * FRAG;
        }
        }
-       for (d = 0; d < dmin; d += FRAG)
-               clrblock(acg.cg_free, d/FRAG);
-       while ((d+FRAG) <= dmax - cbase) {
-               setblock(acg.cg_free, d/FRAG);
+       if (cylno == 0) {
+               /*
+                * reserve space for summary info and Boot block
+                */
+               dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
+               for (d = 0; d < dlower; d += sblock.fs_frag)
+                       clrblock(&sblock, acg.cg_free, d/sblock.fs_frag);
+       } else {
+               for (d = 0; d < dlower; d += sblock.fs_frag) {
+                       setblock(&sblock, acg.cg_free, d/sblock.fs_frag);
+                       acg.cg_cs.cs_nbfree++;
+                       acg.cg_btot[cbtocylno(&sblock, d)]++;
+                       acg.cg_b[cbtocylno(&sblock, d)][cbtorpos(&sblock, d)]++;
+               }
+               sblock.fs_dsize += dlower;
+       }
+       sblock.fs_dsize += acg.cg_ndblk - dupper;
+       for (; d < dupper; d += sblock.fs_frag)
+               clrblock(&sblock, acg.cg_free, d/sblock.fs_frag);
+       if (d > dupper) {
+               acg.cg_frsum[d - dupper]++;
+               for (i = d - 1; i >= dupper; i--) {
+                       setbit(acg.cg_free, i);
+                       acg.cg_cs.cs_nffree++;
+               }
+       }
+       while ((d + sblock.fs_frag) <= dmax - cbase) {
+               setblock(&sblock, acg.cg_free, d/sblock.fs_frag);
                acg.cg_cs.cs_nbfree++;
                acg.cg_cs.cs_nbfree++;
-               s = d * NSPF;
-               acg.cg_b[s/sblock.fs_spc]
-                   [s%sblock.fs_nsect*NRPOS/sblock.fs_nsect]++;
-               d += FRAG;
+               acg.cg_btot[cbtocylno(&sblock, d)]++;
+               acg.cg_b[cbtocylno(&sblock, d)][cbtorpos(&sblock, d)]++;
+               d += sblock.fs_frag;
        }
        }
-       if (d < dmax - cbase)
+       if (d < dmax - cbase) {
+               acg.cg_frsum[dmax - cbase - d]++;
                for (; d < dmax - cbase; d++) {
                        setbit(acg.cg_free, d);
                        acg.cg_cs.cs_nffree++;
                }
                for (; d < dmax - cbase; d++) {
                        setbit(acg.cg_free, d);
                        acg.cg_cs.cs_nffree++;
                }
-       for (; d < MAXBPG; d++)
-               clrbit(acg.cg_free, d);
-       sblock.fs_dsize += acg.cg_ndblk - dmin;
+               for (; d % sblock.fs_frag != 0; d++)
+                       clrbit(acg.cg_free, d);
+       }
+       for (d /= sblock.fs_frag; d < MAXBPG(&sblock); d ++)
+               clrblock(&sblock, acg.cg_free, d);
        sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
        sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
        sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
        sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
        *cs = acg.cg_cs;
        sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
        sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
        sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
        sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
        *cs = acg.cg_cs;
-       wtfs(cgtod(c, &sblock), BSIZE, (char *)&acg);
+       wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
+               sblock.fs_bsize, (char *)&acg);
 }
 
 }
 
-cfile(par)
-struct inode *par;
+/*
+ * initialize the file system
+ */
+struct inode node;
+#define PREDEFDIR 3
+struct direct root_dir[] = {
+       { ROOTINO, sizeof(struct direct), 1, "." },
+       { ROOTINO, sizeof(struct direct), 2, ".." },
+       { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
+};
+struct direct lost_found_dir[] = {
+       { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
+       { ROOTINO, sizeof(struct direct), 2, ".." },
+       { 0, DIRBLKSIZ, 0, 0 },
+};
+char buf[MAXBSIZE];
+
+fsinit()
 {
 {
-       struct inode in;
-       int dbc, ibc;
-       char db[BSIZE];
-       daddr_t ib[NINDIR];
-       int i, f, c;
+       int i;
 
        /*
 
        /*
-        * get mode, uid and gid
+        * initialize the node
         */
         */
-
-       getstr();
-       in.i_mode = gmode(string[0], "-bcd", IFREG, IFBLK, IFCHR, IFDIR);
-       in.i_mode |= gmode(string[1], "-u", 0, ISUID, 0, 0);
-       in.i_mode |= gmode(string[2], "-g", 0, ISGID, 0, 0);
-       for(i=3; i<6; i++) {
-               c = string[i];
-               if(c<'0' || c>'7') {
-                       printf("%c/%s: bad octal mode digit\n", c, string);
-                       error = 1;
-                       c = 0;
-               }
-               in.i_mode |= (c-'0')<<(15-3*i);
-       }
-       in.i_uid = getnum();
-       in.i_gid = getnum();
-       in.i_atime = utime;
-       in.i_mtime = utime;
-       in.i_ctime = utime;
-
+       node.i_atime = utime;
+       node.i_mtime = utime;
+       node.i_ctime = utime;
        /*
        /*
-        * general initialization prior to
-        * switching on format
+        * create the lost+found directory
         */
         */
-
-       ino++;
-       in.i_number = ino;
-       for(i=0; i<BSIZE; i++)
-               db[i] = 0;
-       for(i=0; i<NINDIR; i++)
-               ib[i] = (daddr_t)0;
-       in.i_nlink = 1;
-       in.i_size = 0;
-       for(i=0; i<NDADDR; i++)
-               in.i_db[i] = (daddr_t)0;
-       for(i=0; i<NIADDR; i++)
-               in.i_ib[i] = (daddr_t)0;
-       if(par == (struct inode *)0) {
-               par = &in;
-               in.i_nlink--;
-       }
-       dbc = 0;
-       ibc = 0;
-       switch(in.i_mode&IFMT) {
-
-       case IFREG:
-               /*
-                * regular file
-                * contents is a file name
-                */
-
-               getstr();
-               f = open(string, 0);
-               if(f < 0) {
-                       printf("%s: cannot open\n", string);
-                       error = 1;
-                       break;
-               }
-               while((i=read(f, db, BSIZE)) > 0) {
-                       in.i_size += i;
-                       newblk(&dbc, db, &ibc, ib, ibc < NDADDR ? i : BSIZE, 0);
-               }
-               close(f);
-               break;
-
-       case IFBLK:
-       case IFCHR:
-               /*
-                * special file
-                * content is maj/min types
-                */
-
-               i = getnum() & 0377;
-               f = getnum() & 0377;
-               in.i_rdev = makedev(i, f);
-               break;
-
-       case IFDIR:
-               /*
-                * directory
-                * put in extra links
-                * call recursively until
-                * name of "$" found
-                */
-
-               par->i_nlink++;
-               in.i_nlink++;
-               entry(in.i_number, ".", &dbc, db, &ibc, ib);
-               entry(par->i_number, "..", &dbc, db, &ibc, ib);
-               in.i_size = 2*sizeof(struct direct);
-               for(;;) {
-                       getstr();
-                       if(string[0]=='$' && string[1]=='\0')
-                               break;
-                       if (in.i_size >= BSIZE * NDADDR) {
-                               printf("can't handle direct of > %d entries\n",
-                                   NDIRECT * NDADDR);
-                               exit(1);
-                       }
-                       entry(ino+1, string, &dbc, db, &ibc, ib);
-                       in.i_size += sizeof(struct direct);
-                       cfile(&in);
-               }
-               newblk(&dbc, db, &ibc, ib, roundup(dbc, FSIZE), IFDIR);
-               break;
-       }
-       iput(&in, &ibc, ib);
-}
-
-gmode(c, s, m0, m1, m2, m3)
-char c, *s;
-{
-       int i;
-
-       for(i=0; s[i]; i++)
-               if(c == s[i])
-                       return((&m0)[i]);
-       printf("%c/%s: bad mode\n", c, string);
-       error = 1;
-       return(0);
-}
-
-long
-getnum()
-{
-       int i, c;
-       long n;
-
-       getstr();
-       n = 0;
-       i = 0;
-       for(i=0; c=string[i]; i++) {
-               if(c<'0' || c>'9') {
-                       printf("%s: bad number\n", string);
-                       error = 1;
-                       return((long)0);
-               }
-               n = n*10 + (c-'0');
-       }
-       return(n);
-}
-
-getstr()
-{
-       int i, c;
-
-loop:
-       switch(c=getch()) {
-
-       case ' ':
-       case '\t':
-       case '\n':
-               goto loop;
-
-       case '\0':
-               printf("EOF\n");
-               exit(1);
-
-       case ':':
-               while(getch() != '\n');
-               goto loop;
-
-       }
-       i = 0;
-
-       do {
-               string[i++] = c;
-               c = getch();
-       } while(c!=' '&&c!='\t'&&c!='\n'&&c!='\0');
-       string[i] = '\0';
+       (void)makedir(lost_found_dir, 2);
+       for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
+               bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2]));
+       node.i_number = LOSTFOUNDINO;
+       node.i_mode = IFDIR | UMASK;
+       node.i_nlink = 2;
+       node.i_size = sblock.fs_bsize;
+       node.i_db[0] = alloc(node.i_size, node.i_mode);
+       wtfs(fsbtodb(&sblock, node.i_db[0]), node.i_size, buf);
+       iput(&node);
+       /*
+        * create the root directory
+        */
+       node.i_number = ROOTINO;
+       node.i_mode = IFDIR | UMASK;
+       node.i_nlink = PREDEFDIR;
+       node.i_size = makedir(root_dir, PREDEFDIR);
+       node.i_db[0] = alloc(sblock.fs_fsize, node.i_mode);
+       wtfs(fsbtodb(&sblock, node.i_db[0]), sblock.fs_fsize, buf);
+       iput(&node);
 }
 
 }
 
-rdfs(bno, size, bf)
-daddr_t bno;
-int size;
-char *bf;
+/*
+ * construct a set of directory entries in "buf".
+ * return size of directory.
+ */
+makedir(protodir, entries)
+       register struct direct *protodir;
+       int entries;
 {
 {
-       int n;
-
-       lseek(fsi, bno*FSIZE, 0);
-       n = read(fsi, bf, size);
-       if(n != size) {
-               printf("read error: %ld\n", bno);
-               exit(1);
-       }
-}
-
-wtfs(bno, size, bf)
-daddr_t bno;
-int size;
-char *bf;
-{
-       int n;
-
-       lseek(fso, bno*FSIZE, 0);
-       n = write(fso, bf, size);
-       if(n != size) {
-               printf("write error: %D\n", bno);
-               exit(1);
-       }
+       char *cp;
+       int i, spcleft;
+
+       spcleft = DIRBLKSIZ;
+       for (cp = buf, i = 0; i < entries - 1; i++) {
+               protodir[i].d_reclen = DIRSIZ(&protodir[i]);
+               bcopy(&protodir[i], cp, protodir[i].d_reclen);
+               cp += protodir[i].d_reclen;
+               spcleft -= protodir[i].d_reclen;
+       }
+       protodir[i].d_reclen = spcleft;
+       bcopy(&protodir[i], cp, DIRSIZ(&protodir[i]));
+       cp += DIRSIZ(&protodir[i]);
+       return (cp - buf);
 }
 
 }
 
+/*
+ * allocate a block or frag
+ */
 daddr_t
 alloc(size, mode)
        int size;
        int mode;
 {
 daddr_t
 alloc(size, mode)
        int size;
        int mode;
 {
-       int c, i, s, frag;
+       int i, frag;
        daddr_t d;
 
        daddr_t d;
 
-       c = 0;
-       rdfs(cgtod(0,&sblock), sblock.fs_cgsize, (char *)&acg);
+       rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
+           (char *)&acg);
+       if (acg.cg_magic != CG_MAGIC) {
+               printf("cg 0: bad magic number\n");
+               return (0);
+       }
        if (acg.cg_cs.cs_nbfree == 0) {
                printf("first cylinder group ran out of space\n");
                return (0);
        }
        if (acg.cg_cs.cs_nbfree == 0) {
                printf("first cylinder group ran out of space\n");
                return (0);
        }
-       for (d = 0; d < acg.cg_ndblk; d += FRAG)
-               if (isblock(acg.cg_free, d/FRAG))
+       for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
+               if (isblock(&sblock, acg.cg_free, d / sblock.fs_frag))
                        goto goth;
        printf("internal error: can't find block in cyl 0\n");
        return (0);
 goth:
                        goto goth;
        printf("internal error: can't find block in cyl 0\n");
        return (0);
 goth:
-       clrblock(acg.cg_free, d/FRAG);
+       clrblock(&sblock, acg.cg_free, d / sblock.fs_frag);
        acg.cg_cs.cs_nbfree--;
        sblock.fs_cstotal.cs_nbfree--;
        fscs[0].cs_nbfree--;
        acg.cg_cs.cs_nbfree--;
        sblock.fs_cstotal.cs_nbfree--;
        fscs[0].cs_nbfree--;
@@ -641,120 +705,205 @@ goth:
                sblock.fs_cstotal.cs_ndir++;
                fscs[0].cs_ndir++;
        }
                sblock.fs_cstotal.cs_ndir++;
                fscs[0].cs_ndir++;
        }
-       s = d * NSPF;
-       acg.cg_b[s/sblock.fs_spc][s%sblock.fs_nsect*NRPOS/sblock.fs_nsect]--;
-       if (size != BSIZE) {
-               frag = howmany(size, FSIZE);
-               fscs[0].cs_nffree += FRAG - frag;
-               sblock.fs_cstotal.cs_nffree += FRAG - frag;
-               acg.cg_cs.cs_nffree += FRAG - frag;
-               acg.cg_frsum[FRAG - frag]++;
-               for (i = frag; i < FRAG; i++)
-                       setbit(acg.cg_free, d+i);
-       }
-       wtfs(cgtod(0,&sblock), sblock.fs_cgsize, (char *)&acg);
+       acg.cg_btot[cbtocylno(&sblock, d)]--;
+       acg.cg_b[cbtocylno(&sblock, d)][cbtorpos(&sblock, d)]--;
+       if (size != sblock.fs_bsize) {
+               frag = howmany(size, sblock.fs_fsize);
+               fscs[0].cs_nffree += sblock.fs_frag - frag;
+               sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
+               acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
+               acg.cg_frsum[sblock.fs_frag - frag]++;
+               for (i = frag; i < sblock.fs_frag; i++)
+                       setbit(acg.cg_free, d + i);
+       }
+       wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
+           (char *)&acg);
        return (d);
 }
 
        return (d);
 }
 
-entry(inum, str, adbc, db, aibc, ib)
-ino_t inum;
-char *str;
-int *adbc, *aibc;
-char *db;
-daddr_t *ib;
+/*
+ * Allocate an inode on the disk
+ */
+iput(ip)
+       register struct inode *ip;
 {
 {
-       struct direct *dp;
-       int i;
+       struct dinode buf[MAXINOPB];
+       daddr_t d;
+       int c;
 
 
-       if (*adbc == NDIRECT)
-               newblk(adbc, db, aibc, ib, BSIZE, 0);
-       dp = (struct direct *)db;
-       dp += *adbc;
-       (*adbc)++;
-       dp->d_ino = inum;
-       for(i=0; i<DIRSIZ; i++)
-               dp->d_name[i] = 0;
-       for(i=0; i<DIRSIZ; i++)
-               if((dp->d_name[i] = str[i]) == 0)
-                       break;
+       c = itog(&sblock, ip->i_number);
+       rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
+           (char *)&acg);
+       if (acg.cg_magic != CG_MAGIC) {
+               printf("cg 0: bad magic number\n");
+               exit(1);
+       }
+       acg.cg_cs.cs_nifree--;
+       setbit(acg.cg_iused, ip->i_number);
+       wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
+           (char *)&acg);
+       sblock.fs_cstotal.cs_nifree--;
+       fscs[0].cs_nifree--;
+       if(ip->i_number >= sblock.fs_ipg * sblock.fs_ncg) {
+               printf("fsinit: inode value out of range (%d).\n",
+                   ip->i_number);
+               exit(1);
+       }
+       d = fsbtodb(&sblock, itod(&sblock, ip->i_number));
+       rdfs(d, sblock.fs_bsize, buf);
+       buf[itoo(&sblock, ip->i_number)].di_ic = ip->i_ic;
+       wtfs(d, sblock.fs_bsize, buf);
 }
 
 }
 
-newblk(adbc, db, aibc, ib, size, mode)
-       int *adbc, *aibc;
-       char *db;
-       daddr_t *ib;
+/*
+ * read a block from the file system
+ */
+rdfs(bno, size, bf)
+       daddr_t bno;
        int size;
        int size;
-       int mode;
+       char *bf;
 {
 {
-       int i;
-       daddr_t bno;
+       int n;
 
 
-       bno = alloc(size, mode);
-       wtfs(bno, size, db);
-       for(i=0; i<size; i++)
-               db[i] = 0;
-       *adbc = 0;
-       ib[*aibc] = bno;
-       (*aibc)++;
-       if(*aibc >= NINDIR) {
-               printf("indirect block full\n");
-               error = 1;
-               *aibc = 0;
+       if (lseek(fsi, bno * DEV_BSIZE, 0) < 0) {
+               printf("seek error: %ld\n", bno);
+               perror("rdfs");
+               exit(1);
+       }
+       n = read(fsi, bf, size);
+       if(n != size) {
+               printf("read error: %ld\n", bno);
+               perror("rdfs");
+               exit(1);
        }
 }
 
        }
 }
 
-getch()
+/*
+ * write a block to the file system
+ */
+wtfs(bno, size, bf)
+       daddr_t bno;
+       int size;
+       char *bf;
 {
 {
+       int n;
+
+       lseek(fso, bno * DEV_BSIZE, 0);
+       if (lseek(fso, bno * DEV_BSIZE, 0) < 0) {
+               printf("seek error: %ld\n", bno);
+               perror("wtfs");
+               exit(1);
+       }
+       n = write(fso, bf, size);
+       if(n != size) {
+               printf("write error: %D\n", bno);
+               perror("wtfs");
+               exit(1);
+       }
+}
 
 #ifndef STANDALONE
 
 #ifndef STANDALONE
-       if(charp)
+/*
+ * copy a block
+ */
+bcopy(from, to, size)
+       char *from, *to;
+       int size;
+{
+       asm("   movc3   12(ap),*4(ap),*8(ap)");
+}
 #endif
 #endif
-               return(*charp++);
-#ifndef STANDALONE
-       return(getc(fin));
+
+/*
+ * check if a block is available
+ */
+isblock(fs, cp, h)
+       struct fs *fs;
+       unsigned char *cp;
+       int h;
+{
+       unsigned char mask;
+
+       switch (fs->fs_frag) {
+       case 8:
+               return (cp[h] == 0xff);
+       case 4:
+               mask = 0x0f << ((h & 0x1) << 2);
+               return ((cp[h >> 1] & mask) == mask);
+       case 2:
+               mask = 0x03 << ((h & 0x3) << 1);
+               return ((cp[h >> 2] & mask) == mask);
+       case 1:
+               mask = 0x01 << (h & 0x7);
+               return ((cp[h >> 3] & mask) == mask);
+       default:
+#ifdef STANDALONE
+               printf("isblock bad fs_frag %d\n", fs->fs_frag);
+#else
+               fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
 #endif
 #endif
+               return;
+       }
 }
 
 }
 
-iput(ip, aibc, ib)
-struct inode *ip;
-int *aibc;
-daddr_t *ib;
+/*
+ * take a block out of the map
+ */
+clrblock(fs, cp, h)
+       struct fs *fs;
+       unsigned char *cp;
+       int h;
 {
 {
-       struct dinode *dp;
-       daddr_t d;
-       int i, c = ip->i_number / sblock.fs_ipg;
-
-       rdfs(cgtod(c,&sblock), sblock.fs_cgsize, (char *)&acg);
-       acg.cg_cs.cs_nifree--;
-       setbit(acg.cg_iused, ip->i_number);
-       wtfs(cgtod(c,&sblock), sblock.fs_cgsize, (char *)&acg);
-       sblock.fs_cstotal.cs_nifree--;
-       fscs[0].cs_nifree--;
-       if(ip->i_number >= sblock.fs_ipg) {
-               printf("mkfs: cant handle more than one cg of inodes (yet)\n");
-               exit(1);
-       }
-       if(ip->i_number >= sblock.fs_ipg * sblock.fs_ncg) {
-               if(error == 0)
-                       printf("ilist too small\n");
-               error = 1;
+       switch ((fs)->fs_frag) {
+       case 8:
+               cp[h] = 0;
+               return;
+       case 4:
+               cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
+               return;
+       case 2:
+               cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
+               return;
+       case 1:
+               cp[h >> 3] &= ~(0x01 << (h & 0x7));
+               return;
+       default:
+#ifdef STANDALONE
+               printf("clrblock bad fs_frag %d\n", fs->fs_frag);
+#else
+               fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
+#endif
                return;
        }
                return;
        }
-       d = itod(ip->i_number,&sblock);
-       rdfs(d, BSIZE, buf);
-       for(i=0; i<*aibc; i++) {
-               if(i >= NDADDR)
-                       break;
-               ip->i_db[i] = ib[i];
-       }
-       if(*aibc >= NDADDR) {
-               ip->i_ib[0] = alloc(BSIZE, 0);
-               for(i=0; i<NINDIR-NDADDR; i++) {
-                       ib[i] = ib[i+NDADDR];
-                       ib[i+NDADDR] = (daddr_t)0;
-               }
-               wtfs(ip->i_ib[0], (char *)ib);
+}
+
+/*
+ * put a block into the map
+ */
+setblock(fs, cp, h)
+       struct fs *fs;
+       unsigned char *cp;
+       int h;
+{
+       switch (fs->fs_frag) {
+       case 8:
+               cp[h] = 0xff;
+               return;
+       case 4:
+               cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
+               return;
+       case 2:
+               cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
+               return;
+       case 1:
+               cp[h >> 3] |= (0x01 << (h & 0x7));
+               return;
+       default:
+#ifdef STANDALONE
+               printf("setblock bad fs_frag %d\n", fs->fs_frag);
+#else
+               fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
+#endif
+               return;
        }
        }
-       ((struct dinode *)buf+itoo(ip->i_number))->di_ic = ip->i_ic;
-       wtfs(d, BSIZE, buf);
 }
 }