BSD 4_4_Lite1 release
[unix-history] / usr / src / sbin / newfs / mkfs.c
index 58738b0..80cb734 100644 (file)
-static char *sccsid = "@(#)mkfs.c      1.10 (Berkeley) %G%";
-
 /*
 /*
- * make file system for cylinder-group style file systems
+ * Copyright (c) 1980, 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- * usage: mkfs fs proto
- * or: mkfs size [ bsize frag nsect ntrak cpg ]
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
  */
 
-#define        NDIRECT(fs)     ((fs)->fs_bsize/sizeof(struct direct))
-#define        MAXFN   500
+#ifndef lint
+static char sccsid[] = "@(#)mkfs.c     8.3 (Berkeley) 2/3/94";
+#endif /* not lint */
+
+#include <unistd.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <sys/resource.h>
+#include <ufs/ufs/dinode.h>
+#include <ufs/ufs/dir.h>
+#include <ufs/ffs/fs.h>
+#include <sys/disklabel.h>
 
 #ifndef STANDALONE
 
 #ifndef STANDALONE
-#include <stdio.h>
 #include <a.out.h>
 #include <a.out.h>
+#include <stdio.h>
 #endif
 
 #endif
 
-#include "../h/param.h"
-#include "../h/inode.h"
-#include "../h/fs.h"
-#include "../h/dir.h"
+/*
+ * make file system for cylinder-group style file systems
+ */
 
 
-time_t utime;
+/*
+ * We limit the size of the inode map to be no more than a
+ * third of the cylinder group space, since we must leave at
+ * least an equal amount of space for the block map.
+ *
+ * N.B.: MAXIPG must be a multiple of INOPB(fs).
+ */
+#define MAXIPG(fs)     roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
 
 
-#ifndef STANDALONE
-FILE   *fin;
-#else
-int    fin;
-#endif
+#define UMASK          0755
+#define MAXINOPB       (MAXBSIZE / sizeof(struct dinode))
+#define POWEROF2(num)  (((num) & ((num) - 1)) == 0)
 
 
-int    fsi;
-int    fso;
-char   *charp;
-char   buf[MAXBSIZE];
-#ifndef STANDALONE
-struct exec head;
-#endif
-char   string[50];
+/*
+ * variables set up by front end.
+ */
+extern int     mfs;            /* run as the memory based filesystem */
+extern int     Nflag;          /* run mkfs without writing file system */
+extern int     Oflag;          /* format as an 4.3BSD file system */
+extern int     fssize;         /* file system size */
+extern int     ntracks;        /* # tracks/cylinder */
+extern int     nsectors;       /* # sectors/track */
+extern int     nphyssectors;   /* # sectors/track including spares */
+extern int     secpercyl;      /* sectors per cylinder */
+extern int     sectorsize;     /* bytes/sector */
+extern int     rpm;            /* revolutions/minute of drive */
+extern int     interleave;     /* hardware sector interleave */
+extern int     trackskew;      /* sector 0 skew, per track */
+extern int     headswitch;     /* head switch time, usec */
+extern int     trackseek;      /* track-to-track seek, usec */
+extern int     fsize;          /* fragment size */
+extern int     bsize;          /* block size */
+extern int     cpg;            /* cylinders/cylinder group */
+extern int     cpgflg;         /* cylinders/cylinder group flag was given */
+extern int     minfree;        /* free space threshold */
+extern int     opt;            /* optimization preference (space or time) */
+extern int     density;        /* number of bytes per inode */
+extern int     maxcontig;      /* max contiguous blocks to allocate */
+extern int     rotdelay;       /* rotational delay between blocks */
+extern int     maxbpg;         /* maximum blocks per file in a cyl group */
+extern int     nrpos;          /* # of distinguished rotational positions */
+extern int     bbsize;         /* boot block size */
+extern int     sbsize;         /* superblock size */
+extern u_long  memleft;        /* virtual memory available */
+extern caddr_t membase;        /* start address of memory based filesystem */
+extern caddr_t malloc(), calloc();
 
 union {
        struct fs fs;
 
 union {
        struct fs fs;
-       char pad[MAXBSIZE];
+       char pad[SBSIZE];
 } fsun;
 #define        sblock  fsun.fs
 struct csum *fscs;
 } fsun;
 #define        sblock  fsun.fs
 struct csum *fscs;
@@ -50,316 +114,535 @@ union {
 } cgun;
 #define        acg     cgun.cg
 
 } cgun;
 #define        acg     cgun.cg
 
-char   *fsys;
-char   *proto;
-int    error;
-ino_t  ino = ROOTINO - 1;
-long   getnum();
-daddr_t        alloc();
+struct dinode zino[MAXBSIZE / sizeof(struct dinode)];
 
 
-struct dinode zino[MAXIPG];
+int    fsi, fso;
+daddr_t        alloc();
 
 
-main(argc, argv)
-       int argc;
-       char *argv[];
+mkfs(pp, fsys, fi, fo)
+       struct partition *pp;
+       char *fsys;
+       int fi, fo;
 {
 {
-       int f, c;
-       long i,n;
+       register long i, mincpc, mincpg, inospercg;
+       long cylno, rpos, blk, j, warn = 0;
+       long used, mincpgcnt, bpcg;
+       long mapcramped, inodecramped;
+       long postblsize, rotblsize, totalsbsize;
+       int ppid, status;
+       time_t utime;
+       quad_t sizepb;
+       void started();
 
 
-       argc--, argv++;
 #ifndef STANDALONE
        time(&utime);
 #ifndef STANDALONE
        time(&utime);
-       if(argc < 2) {
-               printf("usage: mkfs sblock proto/size [ bsize frag nsect ntrak cpg ]\n");
-               exit(1);
-       }
-       fsys = argv[0];
-       proto = argv[1];
-#else
-       {
-               static char protos[60];
-
-               printf("file sys size: ");
-               gets(protos);
-               proto = protos;
-       }
-#endif
-#ifdef STANDALONE
-       {
-               char fsbuf[100];
-
-               do {
-                       printf("file system: ");
-                       gets(fsbuf);
-                       fso = open(fsbuf, 1);
-                       fsi = open(fsbuf, 0);
-               } while (fso < 0 || fsi < 0);
-       }
-       fin = NULL;
-       argc = 0;
-#else
-       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);
-       }
-       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 > MAXBSIZE)
-                       printf("%s: too big\n", string);
-               else {
-                       read(f, buf, c);
-                       wtfs(BBLOCK, MAXBSIZE, buf);
-               }
-               close(f);
-noinit:
-               n = getnum();
-               sblock.fs_bsize = MAXBSIZE;
-               sblock.fs_frag = MAXFRAG;
-               sblock.fs_fsize = MAXBSIZE / MAXFRAG;
-               sblock.fs_rotdelay = ROTDELAY;
-               sblock.fs_minfree = MINFREE;
-               sblock.fs_ntrak = getnum();
-               sblock.fs_nsect = getnum();
-               sblock.fs_cpg = getnum();
-       } else
 #endif
 #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);
+       if (mfs) {
+               ppid = getpid();
+               (void) signal(SIGUSR1, started);
+               if (i = fork()) {
+                       if (i == -1) {
+                               perror("mfs");
+                               exit(10);
                        }
                        }
-                       n = n*10 + (c-'0');
+                       if (waitpid(i, &status, 0) != -1 && WIFEXITED(status))
+                               exit(WEXITSTATUS(status));
+                       exit(11);
+                       /* NOTREACHED */
                }
                }
-               if (argc > 2)
-                       sblock.fs_bsize = atoi(argv[2]);
-               else
-                       sblock.fs_bsize = MAXBSIZE;
-               if (argc > 3)
-                       sblock.fs_fsize = atoi(argv[3]);
-               else
-                       sblock.fs_fsize = MAXBSIZE / MAXFRAG;
-               if (argc > 4)
-                       sblock.fs_nsect = atoi(argv[4]);
-               else
-                       sblock.fs_nsect = 32;
-               if (argc > 5)
-                       sblock.fs_ntrak = atoi(argv[5]);
-               else
-                       sblock.fs_ntrak = 19;
-               sblock.fs_frag = sblock.fs_bsize / sblock.fs_fsize;
-               sblock.fs_rotdelay = ROTDELAY;
-               sblock.fs_minfree = MINFREE;
+               (void)malloc(0);
+               if (fssize * sectorsize > memleft)
+                       fssize = (memleft - 16384) / sectorsize;
+               if ((membase = malloc(fssize * sectorsize)) == 0)
+                       exit(12);
+       }
+       fsi = fi;
+       fso = fo;
+       if (Oflag) {
+               sblock.fs_inodefmt = FS_42INODEFMT;
+               sblock.fs_maxsymlinklen = 0;
+       } else {
+               sblock.fs_inodefmt = FS_44INODEFMT;
+               sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
        }
        /*
        }
        /*
-        * Now have size for file system and nsect and ntrak.
-        * (And, if coming from prototype, cpg).
-        * Determine number of cylinders occupied by file system.
+        * Validate the given file system size.
+        * Verify that its last block can actually be accessed.
+        */
+       if (fssize <= 0)
+               printf("preposterous size %d\n", fssize), exit(13);
+       wtfs(fssize - 1, sectorsize, (char *)&sblock);
+       /*
+        * collect and verify the sector and track info
+        */
+       sblock.fs_nsect = nsectors;
+       sblock.fs_ntrak = ntracks;
+       if (sblock.fs_ntrak <= 0)
+               printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
+       if (sblock.fs_nsect <= 0)
+               printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
+       /*
+        * collect and verify the block and fragment sizes
         */
         */
-       if (sblock.fs_fsize < DEV_BSIZE) {
+       sblock.fs_bsize = bsize;
+       sblock.fs_fsize = fsize;
+       if (!POWEROF2(sblock.fs_bsize)) {
+               printf("block size must be a power of 2, not %d\n",
+                   sblock.fs_bsize);
+               exit(16);
+       }
+       if (!POWEROF2(sblock.fs_fsize)) {
+               printf("fragment size must be a power of 2, not %d\n",
+                   sblock.fs_fsize);
+               exit(17);
+       }
+       if (sblock.fs_fsize < sectorsize) {
                printf("fragment size %d is too small, minimum is %d\n",
                printf("fragment size %d is too small, minimum is %d\n",
-                   sblock.fs_fsize, DEV_BSIZE);
-               exit(1);
+                   sblock.fs_fsize, sectorsize);
+               exit(18);
        }
        if (sblock.fs_bsize < MINBSIZE) {
                printf("block size %d is too small, minimum is %d\n",
                    sblock.fs_bsize, MINBSIZE);
        }
        if (sblock.fs_bsize < MINBSIZE) {
                printf("block size %d is too small, minimum is %d\n",
                    sblock.fs_bsize, MINBSIZE);
-               exit(1);
+               exit(19);
        }
        }
-       sblock.fs_size = n = dbtofsb(&sblock, n);
-       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(&sblock) * NSPB(&sblock)) {
-               printf("cylinder too large (%d sectors)\n", 
-                   sblock.fs_ntrak * sblock.fs_nsect);
-               printf("maximum cylinder size: %d sectors\n",
-                   MAXBPG(&sblock) * NSPB(&sblock));
-               exit(1);
-       }
-       sblock.fs_ncyl = n * NSPF(&sblock) / (sblock.fs_nsect * sblock.fs_ntrak);
-       if (n * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_nsect * sblock.fs_ntrak) {
-               printf("%d sector(s) in last cylinder unused\n",
-                   n * NSPF(&sblock) - sblock.fs_ncyl * sblock.fs_nsect * sblock.fs_ntrak);
-               sblock.fs_ncyl++;
+       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(20);
+       }
+       sblock.fs_bmask = ~(sblock.fs_bsize - 1);
+       sblock.fs_fmask = ~(sblock.fs_fsize - 1);
+       sblock.fs_qbmask = ~sblock.fs_bmask;
+       sblock.fs_qfmask = ~sblock.fs_fmask;
+       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);
+       for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
+               sblock.fs_fragshift++;
+       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(21);
+       }
+       sblock.fs_nrpos = nrpos;
+       sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
+       sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode);
+       sblock.fs_nspf = sblock.fs_fsize / sectorsize;
+       for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
+               sblock.fs_fsbtodb++;
+       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, NSPF(&sblock)), 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;
+       sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
+       for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
+               sizepb *= NINDIR(&sblock);
+               sblock.fs_maxfilesize += sizepb;
+       }
+       /*
+        * Validate specified/determined secpercyl
+        * and calculate minimum cylinders per group.
+        */
+       sblock.fs_spc = secpercyl;
+       for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
+            sblock.fs_cpc > 1 && (i & 1) == 0;
+            sblock.fs_cpc >>= 1, i >>= 1)
+               /* void */;
+       mincpc = sblock.fs_cpc;
+       bpcg = sblock.fs_spc * sectorsize;
+       inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock));
+       if (inospercg > MAXIPG(&sblock))
+               inospercg = MAXIPG(&sblock);
+       used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
+       mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
+           sblock.fs_spc);
+       mincpg = roundup(mincpgcnt, mincpc);
+       /*
+        * Ensure that cylinder group with mincpg has enough space
+        * for block maps.
+        */
+       sblock.fs_cpg = mincpg;
+       sblock.fs_ipg = inospercg;
+       if (maxcontig > 1)
+               sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
+       mapcramped = 0;
+       while (CGSIZE(&sblock) > sblock.fs_bsize) {
+               mapcramped = 1;
+               if (sblock.fs_bsize < MAXBSIZE) {
+                       sblock.fs_bsize <<= 1;
+                       if ((i & 1) == 0) {
+                               i >>= 1;
+                       } else {
+                               sblock.fs_cpc <<= 1;
+                               mincpc <<= 1;
+                               mincpg = roundup(mincpgcnt, mincpc);
+                               sblock.fs_cpg = mincpg;
+                       }
+                       sblock.fs_frag <<= 1;
+                       sblock.fs_fragshift += 1;
+                       if (sblock.fs_frag <= MAXFRAG)
+                               continue;
+               }
+               if (sblock.fs_fsize == sblock.fs_bsize) {
+                       printf("There is no block size that");
+                       printf(" can support this disk\n");
+                       exit(22);
+               }
+               sblock.fs_frag >>= 1;
+               sblock.fs_fragshift -= 1;
+               sblock.fs_fsize <<= 1;
+               sblock.fs_nspf <<= 1;
        }
        }
-       sblock.fs_magic = FS_MAGIC;
        /*
        /*
-        * Validate specified/determined cpg.
+        * Ensure that cylinder group with mincpg has enough space for inodes.
         */
         */
-#define        CGTOOBIG(fs) \
-   ((fs).fs_nsect * (fs).fs_ntrak * (fs).fs_cpg/NSPB(&sblock) > MAXBPG(&sblock))
-       if (argc > 6 || fin) {
-               if (fin == NULL)
-                       sblock.fs_cpg = atoi(argv[6]);
-               if (CGTOOBIG(sblock)) {
-                       printf("cylinder group too large (%d blocks); ",
-                           sblock.fs_cpg * sblock.fs_nsect * sblock.fs_ntrak / NSPB(&sblock));
-                       printf("max: %d blocks\n", MAXBPG(&sblock));
-                       exit(1);
+       inodecramped = 0;
+       used *= sectorsize;
+       inospercg = roundup((mincpg * bpcg - used) / density, INOPB(&sblock));
+       sblock.fs_ipg = inospercg;
+       while (inospercg > MAXIPG(&sblock)) {
+               inodecramped = 1;
+               if (mincpc == 1 || sblock.fs_frag == 1 ||
+                   sblock.fs_bsize == MINBSIZE)
+                       break;
+               printf("With a block size of %d %s %d\n", sblock.fs_bsize,
+                   "minimum bytes per inode is",
+                   (mincpg * bpcg - used) / MAXIPG(&sblock) + 1);
+               sblock.fs_bsize >>= 1;
+               sblock.fs_frag >>= 1;
+               sblock.fs_fragshift -= 1;
+               mincpc >>= 1;
+               sblock.fs_cpg = roundup(mincpgcnt, mincpc);
+               if (CGSIZE(&sblock) > sblock.fs_bsize) {
+                       sblock.fs_bsize <<= 1;
+                       break;
                }
                }
-               if (sblock.fs_cpg > MAXCPG) {
-                       printf("cylinder groups are limited to %d cylinders\n",
-                           MAXCPG);
-                       exit(1);
+               mincpg = sblock.fs_cpg;
+               inospercg =
+                   roundup((mincpg * bpcg - used) / density, INOPB(&sblock));
+               sblock.fs_ipg = inospercg;
+       }
+       if (inodecramped) {
+               if (inospercg > MAXIPG(&sblock)) {
+                       printf("Minimum bytes per inode is %d\n",
+                           (mincpg * bpcg - used) / MAXIPG(&sblock) + 1);
+               } else if (!mapcramped) {
+                       printf("With %d bytes per inode, ", density);
+                       printf("minimum cylinders per group is %d\n", mincpg);
                }
                }
+       }
+       if (mapcramped) {
+               printf("With %d sectors per cylinder, ", sblock.fs_spc);
+               printf("minimum cylinders per group is %d\n", mincpg);
+       }
+       if (inodecramped || mapcramped) {
+               if (sblock.fs_bsize != bsize)
+                       printf("%s to be changed from %d to %d\n",
+                           "This requires the block size",
+                           bsize, sblock.fs_bsize);
+               if (sblock.fs_fsize != fsize)
+                       printf("\t%s to be changed from %d to %d\n",
+                           "and the fragment size",
+                           fsize, sblock.fs_fsize);
+               exit(23);
+       }
+       /* 
+        * Calculate the number of cylinders per group
+        */
+       sblock.fs_cpg = cpg;
+       if (sblock.fs_cpg % mincpc != 0) {
+               printf("%s groups must have a multiple of %d cylinders\n",
+                       cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
+               sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
+               if (!cpgflg)
+                       cpg = sblock.fs_cpg;
+       }
+       /*
+        * Must ensure there is enough space for inodes.
+        */
+       sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
+               INOPB(&sblock));
+       while (sblock.fs_ipg > MAXIPG(&sblock)) {
+               inodecramped = 1;
+               sblock.fs_cpg -= mincpc;
+               sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
+                       INOPB(&sblock));
+       }
+       /*
+        * Must ensure there is enough space to hold block map.
+        */
+       while (CGSIZE(&sblock) > sblock.fs_bsize) {
+               mapcramped = 1;
+               sblock.fs_cpg -= mincpc;
+               sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
+                       INOPB(&sblock));
+       }
+       sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
+       if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
+               printf("panic (fs_cpg * fs_spc) % NSPF != 0");
+               exit(24);
+       }
+       if (sblock.fs_cpg < mincpg) {
+               printf("cylinder groups must have at least %d cylinders\n",
+                       mincpg);
+               exit(25);
+       } else if (sblock.fs_cpg != cpg) {
+               if (!cpgflg)
+                       printf("Warning: ");
+               else if (!mapcramped && !inodecramped)
+                       exit(26);
+               if (mapcramped && inodecramped)
+                       printf("Block size and bytes per inode restrict");
+               else if (mapcramped)
+                       printf("Block size restricts");
+               else
+                       printf("Bytes per inode restrict");
+               printf(" cylinders per group to %d.\n", sblock.fs_cpg);
+               if (cpgflg)
+                       exit(27);
+       }
+       sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
+       /*
+        * Now have size for file system and nsect and ntrak.
+        * Determine number of cylinders and blocks in the file system.
+        */
+       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(28);
+       }
+       /*
+        * Determine feasability/values of rotational layout tables.
+        *
+        * The size of the rotational layout tables is limited by the
+        * size of the superblock, SBSIZE. The amount of space available
+        * for tables is calculated as (SBSIZE - sizeof (struct fs)).
+        * The size of these tables is inversely proportional to the block
+        * size of the file system. The size increases if sectors per track
+        * are not powers of two, because more cylinders must be described
+        * by the tables before the rotational pattern repeats (fs_cpc).
+        */
+       sblock.fs_interleave = interleave;
+       sblock.fs_trackskew = trackskew;
+       sblock.fs_npsect = nphyssectors;
+       sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
+       sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
+       if (sblock.fs_ntrak == 1) {
+               sblock.fs_cpc = 0;
+               goto next;
+       }
+       postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(short);
+       rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
+       totalsbsize = sizeof(struct fs) + rotblsize;
+       if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
+               /* use old static table space */
+               sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
+                   (char *)(&sblock.fs_link);
+               sblock.fs_rotbloff = &sblock.fs_space[0] -
+                   (u_char *)(&sblock.fs_link);
        } else {
        } else {
-               sblock.fs_cpg = DESCPG;
-               while (CGTOOBIG(sblock))
-                       --sblock.fs_cpg;
+               /* use dynamic table space */
+               sblock.fs_postbloff = &sblock.fs_space[0] -
+                   (u_char *)(&sblock.fs_link);
+               sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
+               totalsbsize += postblsize;
+       }
+       if (totalsbsize > SBSIZE ||
+           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 impaired.\n");
+               sblock.fs_cpc = 0;
+               goto next;
+       }
+       sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
+       /*
+        * calculate the available blocks for each rotational position
+        */
+       for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
+               for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
+                       fs_postbl(&sblock, cylno)[rpos] = -1;
+       for (i = (rotblsize - 1) * sblock.fs_frag;
+            i >= 0; i -= sblock.fs_frag) {
+               cylno = cbtocylno(&sblock, i);
+               rpos = cbtorpos(&sblock, i);
+               blk = fragstoblks(&sblock, i);
+               if (fs_postbl(&sblock, cylno)[rpos] == -1)
+                       fs_rotbl(&sblock)[blk] = 0;
+               else
+                       fs_rotbl(&sblock)[blk] =
+                           fs_postbl(&sblock, cylno)[rpos] - blk;
+               fs_postbl(&sblock, cylno)[rpos] = blk;
        }
        }
+next:
        /*
         * 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(&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("is not a multiple of %d; choke!\n", sblock.fs_fsize);
-               exit(1);
-       }
-       fscs = (struct csum *)
-           calloc(1, roundup(sblock.fs_ncg * sizeof (struct csum),
-               sblock.fs_bsize));
-       /*
-        * Compute number of inode blocks per cylinder group.
-        * Start with one inode per NBPI bytes; adjust as necessary.
-        */
-       n = ((n * sblock.fs_fsize) / NBPI) / INOPB(&sblock);
-       if (n <= 0)
-               n = 1;
-       sblock.fs_ipg = ((n / sblock.fs_ncg) + 1) * INOPB(&sblock);
-       if (sblock.fs_ipg < INOPB(&sblock))
-               sblock.fs_ipg = INOPB(&sblock);
-       if (sblock.fs_ipg > MAXIPG)
-               sblock.fs_ipg = MAXIPG;
-       sblock.fs_spc = sblock.fs_ntrak * sblock.fs_nsect;
-       sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) /
-                       (sblock.fs_fsize / SECTSIZE);
-       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) / sblock.fs_frag,
+                   cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
                    sblock.fs_fpg / sblock.fs_frag);
                    sblock.fs_fpg / sblock.fs_frag);
-               exit(1);
+               printf("number of cylinders per cylinder group (%d) %s.\n",
+                   sblock.fs_cpg, "must be increased");
+               exit(29);
+       }
+       j = sblock.fs_ncg - 1;
+       if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
+           cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
+               if (j == 0) {
+                       printf("Filesystem must have at least %d sectors\n",
+                           NSPF(&sblock) *
+                           (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
+                       exit(30);
+               }
+               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;
        }
        }
-       sblock.fs_cstotal.cs_nifree = sblock.fs_ipg * sblock.fs_ncg;
-       sblock.fs_cgsize = cgsize(&sblock);
-       sblock.fs_csaddr = cgdmin(0, &sblock);
-       sblock.fs_cssize = sblock.fs_ncg * sizeof(struct csum);
-       sblock.fs_sblkno = SBLOCK;
+       if (warn && !mfs) {
+               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));
+       i = sblock.fs_bsize / sizeof(struct csum);
+       sblock.fs_csmask = ~(i - 1);
+       for (sblock.fs_csshift = 0; i > 1; i >>= 1)
+               sblock.fs_csshift++;
+       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_headswitch = headswitch;
+       sblock.fs_trkseek = trackseek;
+       sblock.fs_maxbpg = maxbpg;
+       sblock.fs_rps = rpm / 60;
+       sblock.fs_optim = opt;
+       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",
-           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",
-           (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);
-/*
-       printf("%7d size (%d blocks)\n",
-               sblock.fs_size, sblock.fs_size / sblock.fs_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/sblock.fs_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(&sblock));
-       }
-*/
+       if (!mfs) {
+               printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
+                   fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
+                   "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
+#define B2MBFACTOR (1 / (1024.0 * 1024.0))
+               printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
+                   (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
+                   sblock.fs_ncg, sblock.fs_cpg,
+                   (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
+                   sblock.fs_ipg);
+#undef B2MBFACTOR
+       }
        /*
         * 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(&sblock) * sblock.fs_frag))
-               /* void */;
-       for (i -= (NSPF(&sblock) * sblock.fs_frag);
-            i >= 0;
-            i -= (NSPF(&sblock) * sblock.fs_frag)) {
-               c = i % sblock.fs_nsect * NRPOS / sblock.fs_nsect;
-               sblock.fs_rotbl[i / (NSPF(&sblock) * sblock.fs_frag)] =
-                       sblock.fs_postbl[c];
-               sblock.fs_postbl[c] = i / (NSPF(&sblock) * sblock.fs_frag);
-       }
-       for (c = 0; c < sblock.fs_ncg; c++)
-               initcg(c);
-       if (sblock.fs_ncg == 1)
-               printf("Warning, no super-block backups with only one cylinder group\n");
-       else
-               printf("\tsuper-block backups (for fsck -b#) at %d+k*%d (%d .. %d)\n",
-                   SBLOCK, fsbtodb(&sblock, sblock.fs_fpg),
-                   SBLOCK + fsbtodb(&sblock, sblock.fs_fpg),
-                   SBLOCK + fsbtodb(&sblock, (sblock.fs_ncg - 1) *
-                       sblock.fs_fpg));
+       if (!mfs)
+               printf("super-block backups (for fsck -b #) at:");
+       for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
+               initcg(cylno, utime);
+               if (mfs)
+                       continue;
+               if (cylno % 9 == 0)
+                       printf("\n");
+               printf(" %d,", fsbtodb(&sblock, cgsblock(&sblock, cylno)));
+       }
+       if (!mfs)
+               printf("\n");
+       if (Nflag && !mfs)
+               exit(0);
        /*
        /*
-        * 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(utime);
        sblock.fs_time = utime;
        sblock.fs_time = utime;
-       wtfs(SBLOCK, MAXBSIZE, (char *)&sblock);
+       wtfs((int)SBOFF / sectorsize, sbsize, (char *)&sblock);
        for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
        for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
-               wtfs(fsbtodb(&sblock, sblock.fs_csaddr + i / sblock.fs_fsize),
-                       sblock.fs_bsize, ((char *)fscs) + i);
-       for (c = 0; c < sblock.fs_ncg; c++)
-               wtfs(fsbtodb(&sblock, cgsblock(c, &sblock)),
-                       MAXBSIZE, (char *)&sblock);
-#ifndef STANDALONE
-       exit(error);
-#endif
+               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);
+       /*
+        * Update information about this partion in pack
+        * label, to that it may be updated on disk.
+        */
+       pp->p_fstype = FS_BSDFFS;
+       pp->p_fsize = sblock.fs_fsize;
+       pp->p_frag = sblock.fs_frag;
+       pp->p_cpg = sblock.fs_cpg;
+       /*
+        * Notify parent process of success.
+        * Dissociate from session and tty.
+        */
+       if (mfs) {
+               kill(ppid, SIGUSR1);
+               (void) setsid();
+               (void) close(0);
+               (void) close(1);
+               (void) close(2);
+               (void) chdir("/");
+       }
 }
 
 /*
  * Initialize a cylinder group.
  */
 }
 
 /*
  * Initialize a cylinder group.
  */
-initcg(c)
-       int c;
+initcg(cylno, utime)
+       int cylno;
+       time_t utime;
 {
 {
-       daddr_t cbase, d, dmin, dmax;
+       daddr_t cbase, d, dlower, dupper, dmax, blkno;
        long i, j, s;
        register struct csum *cs;
 
        long i, j, s;
        register struct csum *cs;
 
@@ -368,317 +651,289 @@ 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;
+       if (cylno == 0)
+               dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
+       cs = fscs + cylno;
+       bzero(&acg, sblock.fs_cgsize);
        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_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_irotor = 0;
-       for (i = 0; i < sblock.fs_frag; i++) {
-               acg.cg_frsum[i] = 0;
-       }
-       for (i = 0; i < sblock.fs_ipg; ) {
-               for (j = INOPB(&sblock); j > 0; j--) {
-                       clrbit(acg.cg_iused, i);
-                       i++;
+       if (sblock.fs_contigsumsize > 0)
+               acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
+       acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_link);
+       acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(long);
+       acg.cg_iusedoff = acg.cg_boff + 
+               sblock.fs_cpg * sblock.fs_nrpos * sizeof(short);
+       acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
+       if (sblock.fs_contigsumsize <= 0) {
+               acg.cg_nextfreeoff = acg.cg_freeoff +
+                  howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
+       } else {
+               acg.cg_clustersumoff = acg.cg_freeoff + howmany
+                   (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
+                   sizeof(long);
+               acg.cg_clustersumoff =
+                   roundup(acg.cg_clustersumoff, sizeof(long));
+               acg.cg_clusteroff = acg.cg_clustersumoff +
+                   (sblock.fs_contigsumsize + 1) * sizeof(long);
+               acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
+                   (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
+       }
+       if (acg.cg_nextfreeoff - (long)(&acg.cg_link) > sblock.fs_cgsize) {
+               printf("Panic: cylinder group too big\n");
+               exit(37);
+       }
+       acg.cg_cs.cs_nifree += sblock.fs_ipg;
+       if (cylno == 0)
+               for (i = 0; i < ROOTINO; i++) {
+                       setbit(cg_inosused(&acg), i);
+                       acg.cg_cs.cs_nifree--;
+               }
+       for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
+               wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
+                   sblock.fs_bsize, (char *)zino);
+       if (cylno > 0) {
+               /*
+                * In cylno 0, beginning space is reserved
+                * for boot and super blocks.
+                */
+               for (d = 0; d < dlower; d += sblock.fs_frag) {
+                       blkno = d / sblock.fs_frag;
+                       setblock(&sblock, cg_blksfree(&acg), blkno);
+                       if (sblock.fs_contigsumsize > 0)
+                               setbit(cg_clustersfree(&acg), blkno);
+                       acg.cg_cs.cs_nbfree++;
+                       cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
+                       cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
+                           [cbtorpos(&sblock, d)]++;
                }
                }
-               acg.cg_cs.cs_nifree += INOPB(&sblock);
-       }
-       while (i < MAXIPG) {
-               clrbit(acg.cg_iused, i);
-               i++;
-       }
-       lseek(fso, fsbtodb(&sblock, cgimin(c,&sblock)) * DEV_BSIZE, 0);
-       if (write(fso, (char *)zino, sblock.fs_ipg * sizeof (struct dinode)) !=
-           sblock.fs_ipg * sizeof (struct dinode))
-               printf("write error %D\n", tell(fso) / sblock.fs_bsize);
-       for (i = 0; i < MAXCPG; i++)
-               for (j = 0; j < NRPOS; j++)
-                       acg.cg_b[i][j] = 0;
-       if (c == 0) {
-               dmin += howmany(sblock.fs_cssize, sblock.fs_bsize) *
-                       sblock.fs_frag;
-       }
-       for (d = 0; d < dmin; d += sblock.fs_frag)
-               clrblock(&sblock, acg.cg_free, d/sblock.fs_frag);
-       while ((d+sblock.fs_frag) <= dmax - cbase) {
-               setblock(&sblock, acg.cg_free, d/sblock.fs_frag);
+               sblock.fs_dsize += dlower;
+       }
+       sblock.fs_dsize += acg.cg_ndblk - dupper;
+       if (i = dupper % sblock.fs_frag) {
+               acg.cg_frsum[sblock.fs_frag - i]++;
+               for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
+                       setbit(cg_blksfree(&acg), dupper);
+                       acg.cg_cs.cs_nffree++;
+               }
+       }
+       for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
+               blkno = d / sblock.fs_frag;
+               setblock(&sblock, cg_blksfree(&acg), blkno);
+               if (sblock.fs_contigsumsize > 0)
+                       setbit(cg_clustersfree(&acg), blkno);
                acg.cg_cs.cs_nbfree++;
                acg.cg_cs.cs_nbfree++;
-               s = d * NSPF(&sblock);
-               acg.cg_b[s / sblock.fs_spc]
-                   [s % sblock.fs_nsect * NRPOS / sblock.fs_nsect]++;
+               cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
+               cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
+                   [cbtorpos(&sblock, d)]++;
                d += sblock.fs_frag;
        }
                d += sblock.fs_frag;
        }
-       if (d < dmax - cbase)
+       if (d < dmax - cbase) {
+               acg.cg_frsum[dmax - cbase - d]++;
                for (; d < dmax - cbase; d++) {
                for (; d < dmax - cbase; d++) {
-                       setbit(acg.cg_free, d);
+                       setbit(cg_blksfree(&acg), d);
                        acg.cg_cs.cs_nffree++;
                }
                        acg.cg_cs.cs_nffree++;
                }
-       for (; d < MAXBPG(&sblock); d++)
-               clrbit(acg.cg_free, d);
-       sblock.fs_dsize += acg.cg_ndblk - dmin;
+       }
+       if (sblock.fs_contigsumsize > 0) {
+               long *sump = cg_clustersum(&acg);
+               u_char *mapp = cg_clustersfree(&acg);
+               int map = *mapp++;
+               int bit = 1;
+               int run = 0;
+
+               for (i = 0; i < acg.cg_nclusterblks; i++) {
+                       if ((map & bit) != 0) {
+                               run++;
+                       } else if (run != 0) {
+                               if (run > sblock.fs_contigsumsize)
+                                       run = sblock.fs_contigsumsize;
+                               sump[run]++;
+                               run = 0;
+                       }
+                       if ((i & (NBBY - 1)) != (NBBY - 1)) {
+                               bit <<= 1;
+                       } else {
+                               map = *mapp++;
+                               bit = 1;
+                       }
+               }
+               if (run != 0) {
+                       if (run > sblock.fs_contigsumsize)
+                               run = sblock.fs_contigsumsize;
+                       sump[run]++;
+               }
+       }
        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(fsbtodb(&sblock, cgtod(c, &sblock)),
+       wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
                sblock.fs_bsize, (char *)&acg);
 }
 
                sblock.fs_bsize, (char *)&acg);
 }
 
-cfile(par)
-       struct inode *par;
-{
-       struct inode in;
-       int dbc, ibc;
-       char db[MAXBSIZE];
-       daddr_t ib[MAXBSIZE / sizeof(daddr_t)];
-       int i, f, c;
-
-       /*
-        * get mode, uid and gid
-        */
-
-       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;
-
-       /*
-        * general initialization prior to
-        * switching on format
-        */
-
-       ino++;
-       in.i_number = ino;
-       for(i=0; i<sblock.fs_bsize; i++)
-               db[i] = 0;
-       for(i=0; i<NINDIR(&sblock); 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, sblock.fs_bsize)) > 0) {
-                       in.i_size += i;
-                       newblk(&dbc, db, &ibc, ib,
-                               ibc < NDADDR ? i : sblock.fs_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;
+/*
+ * initialize the file system
+ */
+struct dinode node;
 
 
-       case IFDIR:
-               /*
-                * directory
-                * put in extra links
-                * call recursively until
-                * name of "$" found
-                */
+#ifdef LOSTDIR
+#define PREDEFDIR 3
+#else
+#define PREDEFDIR 2
+#endif
 
 
-               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 >= sblock.fs_bsize * NDADDR) {
-                               printf("can't handle direct of > %d entries\n",
-                                   NDIRECT(&sblock) * 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, sblock.fs_fsize),
-                       IFDIR);
-               break;
-       }
-       iput(&in, &ibc, ib);
-}
+struct direct root_dir[] = {
+       { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
+       { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
+#ifdef LOSTDIR
+       { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
+#endif
+};
+struct odirect {
+       u_long  d_ino;
+       u_short d_reclen;
+       u_short d_namlen;
+       u_char  d_name[MAXNAMLEN + 1];
+} oroot_dir[] = {
+       { ROOTINO, sizeof(struct direct), 1, "." },
+       { ROOTINO, sizeof(struct direct), 2, ".." },
+#ifdef LOSTDIR
+       { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
+#endif
+};
+#ifdef LOSTDIR
+struct direct lost_found_dir[] = {
+       { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
+       { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
+       { 0, DIRBLKSIZ, 0, 0, 0 },
+};
+struct odirect olost_found_dir[] = {
+       { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
+       { ROOTINO, sizeof(struct direct), 2, ".." },
+       { 0, DIRBLKSIZ, 0, 0 },
+};
+#endif
+char buf[MAXBSIZE];
 
 
-gmode(c, s, m0, m1, m2, m3)
-       char c, *s;
-       int m0, m1, m2, m3;
+fsinit(utime)
+       time_t utime;
 {
        int i;
 
 {
        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';
-}
-
-rdfs(bno, size, bf)
-       daddr_t bno;
-       int size;
-       char *bf;
-{
-       int n;
-
-       lseek(fsi, bno * DEV_BSIZE, 0);
-       n = read(fsi, bf, size);
-       if(n != size) {
-               printf("read error: %ld\n", bno);
-               exit(1);
+       /*
+        * initialize the node
+        */
+       node.di_atime.ts_sec = utime;
+       node.di_mtime.ts_sec = utime;
+       node.di_ctime.ts_sec = utime;
+#ifdef LOSTDIR
+       /*
+        * create the lost+found directory
+        */
+       if (Oflag) {
+               (void)makedir((struct direct *)olost_found_dir, 2);
+               for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
+                       bcopy(&olost_found_dir[2], &buf[i],
+                           DIRSIZ(0, &olost_found_dir[2]));
+       } else {
+               (void)makedir(lost_found_dir, 2);
+               for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
+                       bcopy(&lost_found_dir[2], &buf[i],
+                           DIRSIZ(0, &lost_found_dir[2]));
        }
        }
+       node.di_mode = IFDIR | UMASK;
+       node.di_nlink = 2;
+       node.di_size = sblock.fs_bsize;
+       node.di_db[0] = alloc(node.di_size, node.di_mode);
+       node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
+       wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
+       iput(&node, LOSTFOUNDINO);
+#endif
+       /*
+        * create the root directory
+        */
+       if (mfs)
+               node.di_mode = IFDIR | 01777;
+       else
+               node.di_mode = IFDIR | UMASK;
+       node.di_nlink = PREDEFDIR;
+       if (Oflag)
+               node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
+       else
+               node.di_size = makedir(root_dir, PREDEFDIR);
+       node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
+       node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
+       wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
+       iput(&node, ROOTINO);
 }
 
 }
 
-wtfs(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;
+       char *cp;
+       int i, spcleft;
 
 
-       lseek(fso, bno * DEV_BSIZE, 0);
-       n = write(fso, bf, size);
-       if(n != size) {
-               printf("write error: %D\n", bno);
-               exit(1);
+       spcleft = DIRBLKSIZ;
+       for (cp = buf, i = 0; i < entries - 1; i++) {
+               protodir[i].d_reclen = DIRSIZ(0, &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(0, &protodir[i]));
+       return (DIRBLKSIZ);
 }
 
 }
 
+/*
+ * 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;
-       daddr_t d;
+       int i, frag;
+       daddr_t d, blkno;
 
 
-       c = 0;
-       rdfs(fsbtodb(&sblock, 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);
        }
        for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
        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 += sblock.fs_frag)
-               if (isblock(&sblock, acg.cg_free, d / sblock.fs_frag))
+               if (isblock(&sblock, cg_blksfree(&acg), 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(&sblock, acg.cg_free, d / sblock.fs_frag);
+       blkno = fragstoblks(&sblock, d);
+       clrblock(&sblock, cg_blksfree(&acg), blkno);
+       clrbit(cg_clustersfree(&acg), blkno);
        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--;
@@ -687,9 +942,8 @@ goth:
                sblock.fs_cstotal.cs_ndir++;
                fscs[0].cs_ndir++;
        }
                sblock.fs_cstotal.cs_ndir++;
                fscs[0].cs_ndir++;
        }
-       s = d * NSPF(&sblock);
-       acg.cg_b[s / sblock.fs_spc]
-               [s % sblock.fs_nsect * NRPOS / sblock.fs_nsect]--;
+       cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
+       cg_blks(&sblock, &acg, 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;
        if (size != sblock.fs_bsize) {
                frag = howmany(size, sblock.fs_fsize);
                fscs[0].cs_nffree += sblock.fs_frag - frag;
@@ -697,123 +951,190 @@ goth:
                acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
                acg.cg_frsum[sblock.fs_frag - frag]++;
                for (i = frag; i < sblock.fs_frag; i++)
                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);
+                       setbit(cg_blksfree(&acg), d + i);
        }
        }
-       wtfs(fsbtodb(&sblock, cgtod(0,&sblock)),
-               roundup(sblock.fs_cgsize, DEV_BSIZE), (char *)&acg);
+       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, ino)
+       register struct dinode *ip;
+       register ino_t ino;
 {
 {
-       struct direct *dp;
-       int i;
+       struct dinode buf[MAXINOPB];
+       daddr_t d;
+       int c;
 
 
-       if (*adbc == NDIRECT(&sblock))
-               newblk(adbc, db, aibc, ib, sblock.fs_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 = ino_to_cg(&sblock, ino);
+       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(31);
+       }
+       acg.cg_cs.cs_nifree--;
+       setbit(cg_inosused(&acg), ino);
+       wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
+           (char *)&acg);
+       sblock.fs_cstotal.cs_nifree--;
+       fscs[0].cs_nifree--;
+       if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
+               printf("fsinit: inode value out of range (%d).\n", ino);
+               exit(32);
+       }
+       d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
+       rdfs(d, sblock.fs_bsize, buf);
+       buf[ino_to_fsbo(&sblock, ino)] = *ip;
+       wtfs(d, sblock.fs_bsize, buf);
 }
 
 }
 
-newblk(adbc, db, aibc, ib, size, mode)
-       int *adbc, *aibc;
-       char *db;
-       daddr_t *ib;
-       int size;
-       int mode;
+/*
+ * Notify parent process that the filesystem has created itself successfully.
+ */
+void
+started()
 {
 {
-       int i;
-       daddr_t bno;
 
 
-       bno = alloc(size, mode);
-       wtfs(fsbtodb(&sblock, bno), size, db);
-       for(i = 0; i < size; i++)
-               db[i] = 0;
-       *adbc = 0;
-       ib[*aibc] = bno;
-       (*aibc)++;
-       if(*aibc >= NINDIR(&sblock)) {
-               printf("indirect block full\n");
-               error = 1;
-               *aibc = 0;
+       exit(0);
+}
+
+/*
+ * Replace libc function with one suited to our needs.
+ */
+caddr_t
+malloc(size)
+       register u_long size;
+{
+       char *base, *i;
+       static u_long pgsz;
+       struct rlimit rlp;
+
+       if (pgsz == 0) {
+               base = sbrk(0);
+               pgsz = getpagesize() - 1;
+               i = (char *)((u_long)(base + pgsz) &~ pgsz);
+               base = sbrk(i - base);
+               if (getrlimit(RLIMIT_DATA, &rlp) < 0)
+                       perror("getrlimit");
+               rlp.rlim_cur = rlp.rlim_max;
+               if (setrlimit(RLIMIT_DATA, &rlp) < 0)
+                       perror("setrlimit");
+               memleft = rlp.rlim_max - (u_long)base;
        }
        }
+       size = (size + pgsz) &~ pgsz;
+       if (size > memleft)
+               size = memleft;
+       memleft -= size;
+       if (size == 0)
+               return (0);
+       return ((caddr_t)sbrk(size));
 }
 
 }
 
-getch()
+/*
+ * Replace libc function with one suited to our needs.
+ */
+caddr_t
+realloc(ptr, size)
+       char *ptr;
+       u_long size;
 {
 {
+       void *p;
 
 
-#ifndef STANDALONE
-       if(charp)
-#endif
-               return(*charp++);
-#ifndef STANDALONE
-       return(getc(fin));
-#endif
+       if ((p = malloc(size)) == NULL)
+               return (NULL);
+       bcopy(ptr, p, size);
+       free(ptr);
+       return (p);
 }
 
 }
 
-iput(ip, aibc, ib)
-       struct inode *ip;
-       int *aibc;
-       daddr_t *ib;
+/*
+ * Replace libc function with one suited to our needs.
+ */
+char *
+calloc(size, numelm)
+       u_long size, numelm;
 {
 {
-       struct dinode *dp;
-       daddr_t d;
-       int i, c = ip->i_number / sblock.fs_ipg;
+       caddr_t base;
 
 
-       rdfs(fsbtodb(&sblock, cgtod(c,&sblock)),
-               sblock.fs_cgsize, (char *)&acg);
-       acg.cg_cs.cs_nifree--;
-       setbit(acg.cg_iused, ip->i_number);
-       wtfs(fsbtodb(&sblock, cgtod(c,&sblock)),
-               roundup(sblock.fs_cgsize, DEV_BSIZE), (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;
+       size *= numelm;
+       base = malloc(size);
+       bzero(base, size);
+       return (base);
+}
+
+/*
+ * Replace libc function with one suited to our needs.
+ */
+free(ptr)
+       char *ptr;
+{
+       
+       /* do not worry about it for now */
+}
+
+/*
+ * read a block from the file system
+ */
+rdfs(bno, size, bf)
+       daddr_t bno;
+       int size;
+       char *bf;
+{
+       int n;
+
+       if (mfs) {
+               bcopy(membase + bno * sectorsize, bf, size);
                return;
        }
                return;
        }
-       d = itod(ip->i_number,&sblock);
-       rdfs(fsbtodb(&sblock, d), sblock.fs_bsize, buf);
-       for(i = 0; i < *aibc; i++) {
-               if(i >= NDADDR)
-                       break;
-               ip->i_db[i] = ib[i];
+       if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
+               printf("seek error: %ld\n", bno);
+               perror("rdfs");
+               exit(33);
        }
        }
-       if(*aibc >= NDADDR) {
-               ip->i_ib[0] = alloc(sblock.fs_bsize, 0);
-               for(i=0; i<NINDIR(&sblock)-NDADDR; i++) {
-                       ib[i] = ib[i+NDADDR];
-                       ib[i+NDADDR] = (daddr_t)0;
-               }
-               wtfs(fsbtodb(&sblock, ip->i_ib[0]), sblock.fs_bsize,
-                       (char *)ib);
+       n = read(fsi, bf, size);
+       if (n != size) {
+               printf("read error: %ld\n", bno);
+               perror("rdfs");
+               exit(34);
        }
        }
-       ((struct dinode *)buf + itoo(ip->i_number, &sblock))->di_ic = ip->i_ic;
-       wtfs(fsbtodb(&sblock, d), sblock.fs_bsize, buf);
 }
 
 /*
 }
 
 /*
- * block operations
+ * write a block to the file system
  */
  */
+wtfs(bno, size, bf)
+       daddr_t bno;
+       int size;
+       char *bf;
+{
+       int n;
 
 
+       if (mfs) {
+               bcopy(bf, membase + bno * sectorsize, size);
+               return;
+       }
+       if (Nflag)
+               return;
+       if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
+               printf("seek error: %ld\n", bno);
+               perror("wtfs");
+               exit(35);
+       }
+       n = write(fso, bf, size);
+       if (n != size) {
+               printf("write error: %ld\n", bno);
+               perror("wtfs");
+               exit(36);
+       }
+}
+
+/*
+ * check if a block is available
+ */
 isblock(fs, cp, h)
        struct fs *fs;
        unsigned char *cp;
 isblock(fs, cp, h)
        struct fs *fs;
        unsigned char *cp;
@@ -834,11 +1155,18 @@ isblock(fs, cp, h)
                mask = 0x01 << (h & 0x7);
                return ((cp[h >> 3] & mask) == mask);
        default:
                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);
                fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
-               return;
+#endif
+               return (0);
        }
 }
 
        }
 }
 
+/*
+ * take a block out of the map
+ */
 clrblock(fs, cp, h)
        struct fs *fs;
        unsigned char *cp;
 clrblock(fs, cp, h)
        struct fs *fs;
        unsigned char *cp;
@@ -858,11 +1186,18 @@ clrblock(fs, cp, h)
                cp[h >> 3] &= ~(0x01 << (h & 0x7));
                return;
        default:
                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);
                fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
+#endif
                return;
        }
 }
 
                return;
        }
 }
 
+/*
+ * put a block into the map
+ */
 setblock(fs, cp, h)
        struct fs *fs;
        unsigned char *cp;
 setblock(fs, cp, h)
        struct fs *fs;
        unsigned char *cp;
@@ -882,7 +1217,11 @@ setblock(fs, cp, h)
                cp[h >> 3] |= (0x01 << (h & 0x7));
                return;
        default:
                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);
                fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
+#endif
                return;
        }
 }
                return;
        }
 }