386BSD 0.1 development
[unix-history] / usr / src / sbin / fsck / setup.c
index 153664d..e29d8e5 100644 (file)
@@ -1,12 +1,39 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1980, 1986 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.
+ *
+ * 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.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)setup.c    5.23 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)setup.c    5.33 (Berkeley) 2/22/91";
+#endif /* not lint */
 
 #define DKTYPENAMES
 #include <sys/param.h>
 
 #define DKTYPENAMES
 #include <sys/param.h>
@@ -16,11 +43,13 @@ static char sccsid[] = "@(#)setup.c 5.23 (Berkeley) %G%";
 #include <sys/ioctl.h>
 #include <sys/disklabel.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/disklabel.h>
 #include <sys/file.h>
-#include <machine/endian.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 #include "fsck.h"
 
 #include <ctype.h>
 #include "fsck.h"
 
-BUFAREA asblk;
+struct bufarea asblk;
 #define altsblock (*asblk.b_un.b_fs)
 #define POWEROF2(num)  (((num) & ((num) - 1)) == 0)
 
 #define altsblock (*asblk.b_un.b_fs)
 #define POWEROF2(num)  (((num) & ((num) - 1)) == 0)
 
@@ -37,55 +66,43 @@ BUFAREA asblk;
     /* inode map */    howmany((fs)->fs_ipg, NBBY) + \
     /* block map */    howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
 
     /* inode map */    howmany((fs)->fs_ipg, NBBY) + \
     /* block map */    howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
 
-char   *malloc(), *calloc();
 char   *index();
 struct disklabel *getdisklabel();
 
 setup(dev)
        char *dev;
 {
 char   *index();
 struct disklabel *getdisklabel();
 
 setup(dev)
        char *dev;
 {
-       dev_t rootdev;
-       long cg, ncg, size, asked, i, j;
+       long cg, size, asked, i, j;
+       long bmapsize;
        struct disklabel *lp;
        struct stat statb;
        struct fs proto;
 
        havesb = 0;
        struct disklabel *lp;
        struct stat statb;
        struct fs proto;
 
        havesb = 0;
-       if (stat("/", &statb) < 0)
-               errexit("Can't stat root\n");
-       rootdev = statb.st_dev;
        if (stat(dev, &statb) < 0) {
        if (stat(dev, &statb) < 0) {
-               perror(dev);
-               printf("Can't stat %s\n", dev);
+               printf("Can't stat %s: %s\n", dev, strerror(errno));
                return (0);
        }
                return (0);
        }
-       rawflg = 0;
-       if ((statb.st_mode & S_IFMT) == S_IFBLK)
-               ;
-       else if ((statb.st_mode & S_IFMT) == S_IFCHR)
-               rawflg++;
-       else {
-               if (reply("file is not a block or character device; OK") == 0)
+       if ((statb.st_mode & S_IFMT) != S_IFCHR) {
+               pfatal("%s is not a character device", dev);
+               if (reply("CONTINUE") == 0)
                        return (0);
        }
                        return (0);
        }
-       if (rootdev == statb.st_rdev)
-               hotroot++;
-       if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) {
-               perror(dev);
-               printf("Can't open %s\n", dev);
+       if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
+               printf("Can't open %s: %s\n", dev, strerror(errno));
                return (0);
        }
        if (preen == 0)
                printf("** %s", dev);
                return (0);
        }
        if (preen == 0)
                printf("** %s", dev);
-       if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) {
-               dfile.wfdes = -1;
+       if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
+               fswritefd = -1;
                if (preen)
                        pfatal("NO WRITE ACCESS");
                printf(" (NO WRITE)");
        }
        if (preen == 0)
                printf("\n");
                if (preen)
                        pfatal("NO WRITE ACCESS");
                printf(" (NO WRITE)");
        }
        if (preen == 0)
                printf("\n");
-       dfile.mod = 0;
+       fsmodified = 0;
        lfdir = 0;
        initbarea(&sblk);
        initbarea(&asblk);
        lfdir = 0;
        initbarea(&sblk);
        initbarea(&asblk);
@@ -93,15 +110,23 @@ setup(dev)
        asblk.b_un.b_buf = malloc(SBSIZE);
        if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
                errexit("cannot allocate space for superblock\n");
        asblk.b_un.b_buf = malloc(SBSIZE);
        if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
                errexit("cannot allocate space for superblock\n");
-       if (lp = getdisklabel((char *)NULL, dfile.rfdes))
+       if (lp = getdisklabel((char *)NULL, fsreadfd))
                dev_bsize = secsize = lp->d_secsize;
        else
                dev_bsize = secsize = DEV_BSIZE;
                dev_bsize = secsize = lp->d_secsize;
        else
                dev_bsize = secsize = DEV_BSIZE;
+#ifdef tahoe
+       /*
+        * On the tahoe, the disk label and the disk driver disagree.
+        * The label knows that sectors are 512 bytes, but the disk
+        * drivers will only transfer in 1024 sized pieces.
+        */
+       secsize = 1024;
+#endif
        /*
         * Read in the superblock, looking for alternates if necessary
         */
        if (readsb(1) == 0) {
        /*
         * Read in the superblock, looking for alternates if necessary
         */
        if (readsb(1) == 0) {
-               if (bflag || preen || calcsb(dev, dfile.rfdes, &proto) == 0)
+               if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
                        return(0);
                if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
                        return (0);
                        return(0);
                if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
                        return (0);
@@ -122,8 +147,8 @@ setup(dev)
                }
                pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
        }
                }
                pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
        }
-       fmax = sblock.fs_size;
-       imax = sblock.fs_ncg * sblock.fs_ipg;
+       maxfsblock = sblock.fs_size;
+       maxino = sblock.fs_ncg * sblock.fs_ipg;
        /*
         * Check and potentially fix certain fields in the super block.
         */
        /*
         * Check and potentially fix certain fields in the super block.
         */
@@ -142,16 +167,26 @@ setup(dev)
                        sbdirty();
                }
        }
                        sbdirty();
                }
        }
-       if (sblock.fs_interleave < 1) {
+       if (sblock.fs_interleave < 1 || 
+           sblock.fs_interleave > sblock.fs_nsect) {
+               pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
                        sblock.fs_interleave);
                sblock.fs_interleave = 1;
                        sblock.fs_interleave);
                sblock.fs_interleave = 1;
+               if (preen)
+                       printf(" (FIXED)\n");
+               if (preen || reply("SET TO DEFAULT") == 1) {
                        sbdirty();
                        dirty(&asblk);
                }
        }
                        sbdirty();
                        dirty(&asblk);
                }
        }
-       if (sblock.fs_npsect < sblock.fs_nsect) {
+       if (sblock.fs_npsect < sblock.fs_nsect || 
+           sblock.fs_npsect > sblock.fs_nsect*2) {
+               pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
                        sblock.fs_npsect);
                sblock.fs_npsect = sblock.fs_nsect;
                        sblock.fs_npsect);
                sblock.fs_npsect = sblock.fs_nsect;
+               if (preen)
+                       printf(" (FIXED)\n");
+               if (preen || reply("SET TO DEFAULT") == 1) {
                        sbdirty();
                        dirty(&asblk);
                }
                        sbdirty();
                        dirty(&asblk);
                }
@@ -216,8 +251,9 @@ setup(dev)
                }
        }
        if (asblk.b_dirty) {
                }
        }
        if (asblk.b_dirty) {
-               bcopy((char *)&sblock, (char *)&altsblock, sblock.fs_sbsize);
-               flush(&dfile, &asblk);
+               bcopy((char *)&sblock, (char *)&altsblock,
+                       (size_t)sblock.fs_sbsize);
+               flush(fswritefd, &asblk);
        }
        /*
         * read in the summary info.
        }
        /*
         * read in the summary info.
@@ -227,7 +263,7 @@ setup(dev)
                size = sblock.fs_cssize - i < sblock.fs_bsize ?
                    sblock.fs_cssize - i : sblock.fs_bsize;
                sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
                size = sblock.fs_cssize - i < sblock.fs_bsize ?
                    sblock.fs_cssize - i : sblock.fs_bsize;
                sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
-               if (bread(&dfile, (char *)sblock.fs_csp[j],
+               if (bread(fsreadfd, (char *)sblock.fs_csp[j],
                    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
                    size) != 0 && !asked) {
                        pfatal("BAD SUMMARY INFORMATION");
                    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
                    size) != 0 && !asked) {
                        pfatal("BAD SUMMARY INFORMATION");
@@ -239,24 +275,37 @@ setup(dev)
        /*
         * allocate and initialize the necessary maps
         */
        /*
         * allocate and initialize the necessary maps
         */
-       bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
-       blockmap = calloc((unsigned)bmapsz, sizeof (char));
+       bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
+       blockmap = calloc((unsigned)bmapsize, sizeof (char));
        if (blockmap == NULL) {
        if (blockmap == NULL) {
-               printf("cannot alloc %d bytes for blockmap\n", bmapsz);
+               printf("cannot alloc %u bytes for blockmap\n",
+                   (unsigned)bmapsize);
                goto badsb;
        }
                goto badsb;
        }
-       statemap = calloc((unsigned)(imax + 1), sizeof(char));
+       statemap = calloc((unsigned)(maxino + 1), sizeof(char));
        if (statemap == NULL) {
        if (statemap == NULL) {
-               printf("cannot alloc %d bytes for statemap\n", imax + 1);
+               printf("cannot alloc %u bytes for statemap\n",
+                   (unsigned)(maxino + 1));
                goto badsb;
        }
                goto badsb;
        }
-       lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short));
+       lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
        if (lncntp == NULL) {
        if (lncntp == NULL) {
-               printf("cannot alloc %d bytes for lncntp\n", 
-                   (imax + 1) * sizeof(short));
+               printf("cannot alloc %u bytes for lncntp\n", 
+                   (unsigned)(maxino + 1) * sizeof(short));
+               goto badsb;
+       }
+       numdirs = sblock.fs_cstotal.cs_ndir;
+       inplast = 0;
+       listmax = numdirs + 10;
+       inpsort = (struct inoinfo **)calloc((unsigned)listmax,
+           sizeof(struct inoinfo *));
+       inphead = (struct inoinfo **)calloc((unsigned)numdirs,
+           sizeof(struct inoinfo *));
+       if (inpsort == NULL || inphead == NULL) {
+               printf("cannot alloc %u bytes for inphead\n", 
+                   (unsigned)numdirs * sizeof(struct inoinfo *));
                goto badsb;
        }
                goto badsb;
        }
-
        bufinit();
        return (1);
 
        bufinit();
        return (1);
 
@@ -271,10 +320,9 @@ badsb:
 readsb(listerr)
        int listerr;
 {
 readsb(listerr)
        int listerr;
 {
-       off_t sboff;
        daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
 
        daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
 
-       if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) != 0)
+       if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
                return (0);
        sblk.b_bno = super;
        sblk.b_size = SBSIZE;
                return (0);
        sblk.b_bno = super;
        sblk.b_size = SBSIZE;
@@ -392,7 +440,7 @@ calcsb(dev, devfd, fs)
                        fstypenames[pp->p_fstype] : "unknown");
                return (0);
        }
                        fstypenames[pp->p_fstype] : "unknown");
                return (0);
        }
-       bzero(fs, sizeof(struct fs));
+       bzero((char *)fs, sizeof(struct fs));
        fs->fs_fsize = pp->p_fsize;
        fs->fs_frag = pp->p_frag;
        fs->fs_cpg = pp->p_cpg;
        fs->fs_fsize = pp->p_fsize;
        fs->fs_frag = pp->p_frag;
        fs->fs_cpg = pp->p_cpg;
@@ -429,9 +477,8 @@ getdisklabel(s, fd)
        if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
                if (s == NULL)
                        return ((struct disklabel *)NULL);
        if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
                if (s == NULL)
                        return ((struct disklabel *)NULL);
-               pwarn("");
-               perror("ioctl (GDINFO)");
-               errexit("%s: can't read disk label", s);
+               pwarn("ioctl (GCINFO): %s\n", strerror(errno));
+               errexit("%s: can't read disk label\n", s);
        }
        return (&lab);
 }
        }
        return (&lab);
 }