386BSD 0.1 development
[unix-history] / usr / src / sbin / fsck / setup.c
index f298e37..e29d8e5 100644 (file)
@@ -2,21 +2,37 @@
  * Copyright (c) 1980, 1986 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980, 1986 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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.26 (Berkeley) %G%";
+static char sccsid[] = "@(#)setup.c    5.33 (Berkeley) 2/22/91";
 #endif /* not lint */
 
 #define DKTYPENAMES
 #endif /* not lint */
 
 #define DKTYPENAMES
@@ -27,7 +43,9 @@ static char sccsid[] = "@(#)setup.c   5.26 (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"
 
@@ -48,14 +66,12 @@ struct 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, size, asked, i, j;
        long bmapsize;
        struct disklabel *lp;
        long cg, size, asked, i, j;
        long bmapsize;
        struct disklabel *lp;
@@ -63,23 +79,17 @@ setup(dev)
        struct fs proto;
 
        havesb = 0;
        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);
        }
-       if ((statb.st_mode & S_IFMT) != S_IFBLK &&
-           (statb.st_mode & S_IFMT) != S_IFCHR &&
-           reply("file is not a block or character device; OK") == 0)
-               return (0);
-       if (rootdev == statb.st_rdev)
-               hotroot++;
+       if ((statb.st_mode & S_IFMT) != S_IFCHR) {
+               pfatal("%s is not a character device", dev);
+               if (reply("CONTINUE") == 0)
+                       return (0);
+       }
        if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
        if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
-               perror(dev);
-               printf("Can't open %s\n", dev);
+               printf("Can't open %s: %s\n", dev, strerror(errno));
                return (0);
        }
        if (preen == 0)
                return (0);
        }
        if (preen == 0)
@@ -104,6 +114,14 @@ setup(dev)
                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
         */
        /*
         * Read in the superblock, looking for alternates if necessary
         */
@@ -149,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);
                }
@@ -224,7 +252,7 @@ setup(dev)
        }
        if (asblk.b_dirty) {
                bcopy((char *)&sblock, (char *)&altsblock,
        }
        if (asblk.b_dirty) {
                bcopy((char *)&sblock, (char *)&altsblock,
-                       (int)sblock.fs_sbsize);
+                       (size_t)sblock.fs_sbsize);
                flush(fswritefd, &asblk);
        }
        /*
                flush(fswritefd, &asblk);
        }
        /*
@@ -250,29 +278,32 @@ setup(dev)
        bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
        blockmap = calloc((unsigned)bmapsize, sizeof (char));
        if (blockmap == NULL) {
        bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
        blockmap = calloc((unsigned)bmapsize, sizeof (char));
        if (blockmap == NULL) {
-               printf("cannot alloc %d bytes for blockmap\n", bmapsize);
+               printf("cannot alloc %u bytes for blockmap\n",
+                   (unsigned)bmapsize);
                goto badsb;
        }
        statemap = calloc((unsigned)(maxino + 1), sizeof(char));
        if (statemap == NULL) {
                goto badsb;
        }
        statemap = calloc((unsigned)(maxino + 1), sizeof(char));
        if (statemap == NULL) {
-               printf("cannot alloc %d bytes for statemap\n", maxino + 1);
+               printf("cannot alloc %u bytes for statemap\n",
+                   (unsigned)(maxino + 1));
                goto badsb;
        }
        lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
        if (lncntp == NULL) {
                goto badsb;
        }
        lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
        if (lncntp == NULL) {
-               printf("cannot alloc %d bytes for lncntp\n", 
-                   (maxino + 1) * sizeof(short));
+               printf("cannot alloc %u bytes for lncntp\n", 
+                   (unsigned)(maxino + 1) * sizeof(short));
                goto badsb;
        }
        numdirs = sblock.fs_cstotal.cs_ndir;
                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) {
        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 %d bytes for inphead\n", 
-                   numdirs * sizeof(struct inoinfo *));
+               printf("cannot alloc %u bytes for inphead\n", 
+                   (unsigned)numdirs * sizeof(struct inoinfo *));
                goto badsb;
        }
        bufinit();
                goto badsb;
        }
        bufinit();
@@ -446,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);
 }