1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)setup.c	5.3 (Berkeley) 5/15/86";
   9: #endif not lint
  10: 
  11: #include <sys/param.h>
  12: #include <sys/inode.h>
  13: #include <sys/fs.h>
  14: #include <sys/stat.h>
  15: #include "fsck.h"
  16: 
  17: char    *calloc();
  18: 
  19: setup(dev)
  20:     char *dev;
  21: {
  22:     dev_t rootdev;
  23:     struct stat statb;
  24:     daddr_t super = bflag ? bflag : SBLOCK;
  25:     int i, j;
  26:     long size;
  27:     BUFAREA asblk;
  28: #	define altsblock asblk.b_un.b_fs
  29: 
  30:     if (stat("/", &statb) < 0)
  31:         errexit("Can't stat root\n");
  32:     rootdev = statb.st_dev;
  33:     if (stat(dev, &statb) < 0) {
  34:         printf("Can't stat %s\n", dev);
  35:         return (0);
  36:     }
  37:     rawflg = 0;
  38:     if ((statb.st_mode & S_IFMT) == S_IFBLK)
  39:         ;
  40:     else if ((statb.st_mode & S_IFMT) == S_IFCHR)
  41:         rawflg++;
  42:     else {
  43:         if (reply("file is not a block or character device; OK") == 0)
  44:             return (0);
  45:     }
  46:     if (rootdev == statb.st_rdev)
  47:         hotroot++;
  48:     if ((dfile.rfdes = open(dev, 0)) < 0) {
  49:         printf("Can't open %s\n", dev);
  50:         return (0);
  51:     }
  52:     if (preen == 0)
  53:         printf("** %s", dev);
  54:     if (nflag || (dfile.wfdes = open(dev, 1)) < 0) {
  55:         dfile.wfdes = -1;
  56:         if (preen)
  57:             pfatal("NO WRITE ACCESS");
  58:         printf(" (NO WRITE)");
  59:     }
  60:     if (preen == 0)
  61:         printf("\n");
  62:     dfile.mod = 0;
  63:     lfdir = 0;
  64:     initbarea(&sblk);
  65:     initbarea(&fileblk);
  66:     initbarea(&inoblk);
  67:     initbarea(&cgblk);
  68:     initbarea(&asblk);
  69:     /*
  70: 	 * Read in the super block and its summary info.
  71: 	 */
  72:     if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) != 0)
  73:         return (0);
  74:     sblk.b_bno = super;
  75:     sblk.b_size = SBSIZE;
  76:     /*
  77: 	 * run a few consistency checks of the super block
  78: 	 */
  79:     if (sblock.fs_magic != FS_MAGIC)
  80:         { badsb("MAGIC NUMBER WRONG"); return (0); }
  81:     if (sblock.fs_ncg < 1)
  82:         { badsb("NCG OUT OF RANGE"); return (0); }
  83:     if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG)
  84:         { badsb("CPG OUT OF RANGE"); return (0); }
  85:     if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
  86:         (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
  87:         { badsb("NCYL DOES NOT JIVE WITH NCG*CPG"); return (0); }
  88:     if (sblock.fs_sbsize > SBSIZE)
  89:         { badsb("SIZE PREPOSTEROUSLY LARGE"); return (0); }
  90:     /*
  91: 	 * Check and potentially fix certain fields in the super block.
  92: 	 */
  93:     if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
  94:         pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
  95:         if (reply("SET TO DEFAULT") == 1) {
  96:             sblock.fs_optim = FS_OPTTIME;
  97:             sbdirty();
  98:         }
  99:     }
 100:     if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
 101:         pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
 102:             sblock.fs_minfree);
 103:         if (reply("SET TO DEFAULT") == 1) {
 104:             sblock.fs_minfree = 10;
 105:             sbdirty();
 106:         }
 107:     }
 108:     /*
 109: 	 * Set all possible fields that could differ, then do check
 110: 	 * of whole super block against an alternate super block.
 111: 	 * When an alternate super-block is specified this check is skipped.
 112: 	 */
 113:     if (bflag)
 114:         goto sbok;
 115:     getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
 116:     if (asblk.b_errs != NULL)
 117:         return (0);
 118:     altsblock.fs_link = sblock.fs_link;
 119:     altsblock.fs_rlink = sblock.fs_rlink;
 120:     altsblock.fs_time = sblock.fs_time;
 121:     altsblock.fs_cstotal = sblock.fs_cstotal;
 122:     altsblock.fs_cgrotor = sblock.fs_cgrotor;
 123:     altsblock.fs_fmod = sblock.fs_fmod;
 124:     altsblock.fs_clean = sblock.fs_clean;
 125:     altsblock.fs_ronly = sblock.fs_ronly;
 126:     altsblock.fs_flags = sblock.fs_flags;
 127:     altsblock.fs_maxcontig = sblock.fs_maxcontig;
 128:     altsblock.fs_minfree = sblock.fs_minfree;
 129:     altsblock.fs_optim = sblock.fs_optim;
 130:     altsblock.fs_rotdelay = sblock.fs_rotdelay;
 131:     altsblock.fs_maxbpg = sblock.fs_maxbpg;
 132:     bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
 133:         sizeof sblock.fs_csp);
 134:     bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
 135:         sizeof sblock.fs_fsmnt);
 136:     if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize))
 137:         { badsb("TRASHED VALUES IN SUPER BLOCK"); return (0); }
 138: sbok:
 139:     fmax = sblock.fs_size;
 140:     imax = sblock.fs_ncg * sblock.fs_ipg;
 141:     /*
 142: 	 * read in the summary info.
 143: 	 */
 144:     for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
 145:         size = sblock.fs_cssize - i < sblock.fs_bsize ?
 146:             sblock.fs_cssize - i : sblock.fs_bsize;
 147:         sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
 148:         if (bread(&dfile, (char *)sblock.fs_csp[j],
 149:             fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
 150:             size) != 0)
 151:             return (0);
 152:     }
 153:     /*
 154: 	 * allocate and initialize the necessary maps
 155: 	 */
 156:     bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
 157:     blockmap = calloc((unsigned)bmapsz, sizeof (char));
 158:     if (blockmap == NULL) {
 159:         printf("cannot alloc %d bytes for blockmap\n", bmapsz);
 160:         goto badsb;
 161:     }
 162:     statemap = calloc((unsigned)(imax + 1), sizeof(char));
 163:     if (statemap == NULL) {
 164:         printf("cannot alloc %d bytes for statemap\n", imax + 1);
 165:         goto badsb;
 166:     }
 167:     lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short));
 168:     if (lncntp == NULL) {
 169:         printf("cannot alloc %d bytes for lncntp\n",
 170:             (imax + 1) * sizeof(short));
 171:         goto badsb;
 172:     }
 173: 
 174:     return (1);
 175: 
 176: badsb:
 177:     ckfini();
 178:     return (0);
 179: #	undef altsblock
 180: }
 181: 
 182: badsb(s)
 183:     char *s;
 184: {
 185: 
 186:     if (preen)
 187:         printf("%s: ", devname);
 188:     printf("BAD SUPER BLOCK: %s\n", s);
 189:     pwarn("USE -b OPTION TO FSCK TO SPECIFY LOCATION OF AN ALTERNATE\n");
 190:     pfatal("SUPER-BLOCK TO SUPPLY NEEDED INFORMATION; SEE fsck(8).\n");
 191: }

Defined functions

badsb defined in line 182; used 6 times
setup defined in line 19; used 1 times

Defined variables

sccsid defined in line 8; never used

Defined macros

altsblock defined in line 28; used 18 times
Last modified: 1986-05-15
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1196
Valid CSS Valid XHTML 1.0 Strict