new copyright notice
[unix-history] / usr / src / sbin / fsck / setup.c
index e1265e4..66e67b5 100644 (file)
@@ -1,17 +1,18 @@
 /*
 /*
- * 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.
+ *
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)setup.c    5.20 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)setup.c    5.29 (Berkeley) %G%";
+#endif /* not lint */
 
 #define DKTYPENAMES
 #include <sys/param.h>
 
 #define DKTYPENAMES
 #include <sys/param.h>
-#include <sys/inode.h>
-#include <sys/fs.h>
+#include <ufs/dinode.h>
+#include <ufs/fs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/disklabel.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/disklabel.h>
@@ -20,7 +21,7 @@ static char sccsid[] = "@(#)setup.c   5.20 (Berkeley) %G%";
 #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,62 +38,54 @@ 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   *calloc();
+char   *malloc(), *calloc();
 char   *index();
 char   *index();
+struct disklabel *getdisklabel();
 
 setup(dev)
        char *dev;
 {
 
 setup(dev)
        char *dev;
 {
-       dev_t rootdev;
-       long cg, ncg, size, asked, i, j;
-       struct disklabel *getdisklabel(), *lp;
+       long cg, size, asked, i, j;
+       long bmapsize;
+       struct disklabel *lp;
        struct stat statb;
        struct fs proto;
 
        havesb = 0;
        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) {
                perror(dev);
                printf("Can't stat %s\n", dev);
                return (0);
        }
        if (stat(dev, &statb) < 0) {
                perror(dev);
                printf("Can't stat %s\n", dev);
                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) {
+       if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
                perror(dev);
                printf("Can't open %s\n", dev);
                return (0);
        }
        if (preen == 0)
                printf("** %s", dev);
                perror(dev);
                printf("Can't open %s\n", 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);
-       sblk.b_un.b_buf = (char *)malloc(SBSIZE);
-       asblk.b_un.b_buf = (char *)malloc(SBSIZE);
-       if (sblk.b_un.b_buf == 0 || asblk.b_un.b_buf == 0)
+       sblk.b_un.b_buf = malloc(SBSIZE);
+       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");
                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;
@@ -100,7 +93,7 @@ setup(dev)
         * 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);
@@ -121,8 +114,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.
         */
@@ -215,8 +208,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,
+                       (int)sblock.fs_sbsize);
+               flush(fswritefd, &asblk);
        }
        /*
         * read in the summary info.
        }
        /*
         * read in the summary info.
@@ -226,7 +220,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");
@@ -238,24 +232,35 @@ 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 %d bytes for blockmap\n", 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 %d bytes for statemap\n", 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) {
                printf("cannot alloc %d bytes for lncntp\n", 
        if (lncntp == NULL) {
                printf("cannot alloc %d bytes for lncntp\n", 
-                   (imax + 1) * sizeof(short));
+                   (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 %d bytes for inphead\n", 
+                   numdirs * sizeof(struct inoinfo *));
                goto badsb;
        }
                goto badsb;
        }
-
        bufinit();
        return (1);
 
        bufinit();
        return (1);
 
@@ -270,10 +275,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;
@@ -305,7 +309,7 @@ readsb(listerr)
         * When an alternate super-block is specified this check is skipped.
         */
        getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
         * When an alternate super-block is specified this check is skipped.
         */
        getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
-       if (asblk.b_errs != NULL)
+       if (asblk.b_errs)
                return (0);
        if (bflag) {
                havesb = 1;
                return (0);
        if (bflag) {
                havesb = 1;
@@ -374,7 +378,6 @@ calcsb(dev, devfd, fs)
        register struct partition *pp;
        register char *cp;
        int i;
        register struct partition *pp;
        register char *cp;
        int i;
-       struct disklabel *getdisklabel();
 
        cp = index(dev, '\0') - 1;
        if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
 
        cp = index(dev, '\0') - 1;
        if (cp == (char *)-1 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp)) {
@@ -392,7 +395,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;