1: /*
   2:  * Copyright (c) 1983 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: char copyright[] =
   9: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  10:  All rights reserved.\n";
  11: #endif not lint
  12: 
  13: #ifndef lint
  14: static char sccsid[] = "@(#)badsect.c	5.2 (Berkeley) 6/24/85";
  15: #endif not lint
  16: 
  17: /*
  18:  * badsect
  19:  *
  20:  * Badsect takes a list of file-system relative sector numbers
  21:  * and makes files containing the blocks of which these sectors are a part.
  22:  * It can be used to contain sectors which have problems if these sectors
  23:  * are not part of the bad file for the pack (see bad144).  For instance,
  24:  * this program can be used if the driver for the file system in question
  25:  * does not support bad block forwarding.
  26:  */
  27: #include <stdio.h>
  28: #include <sys/param.h>
  29: #include <sys/fs.h>
  30: #include <sys/dir.h>
  31: #include <sys/stat.h>
  32: #include <sys/inode.h>
  33: 
  34: union {
  35:     struct  fs fs;
  36:     char    fsx[SBSIZE];
  37: } ufs;
  38: #define sblock  ufs.fs
  39: union {
  40:     struct  cg cg;
  41:     char    cgx[MAXBSIZE];
  42: } ucg;
  43: #define acg ucg.cg
  44: struct  fs *fs;
  45: int fso, fsi;
  46: int errs;
  47: 
  48: char buf[MAXBSIZE];
  49: 
  50: 
  51: main(argc, argv)
  52:     int argc;
  53:     char *argv[];
  54: {
  55:     daddr_t number;
  56:     struct stat stbuf, devstat;
  57:     register struct direct *dp;
  58:     DIR *dirp;
  59:     int fd;
  60:     char name[BUFSIZ];
  61: 
  62:     if (argc < 3) {
  63:         fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
  64:         exit(1);
  65:     }
  66:     if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
  67:         perror(argv[1]);
  68:         exit(2);
  69:     }
  70:     strcpy(name, "/dev/");
  71:     if ((dirp = opendir(name)) == NULL) {
  72:         perror(name);
  73:         exit(3);
  74:     }
  75:     while ((dp = readdir(dirp)) != NULL) {
  76:         strcpy(&name[5], dp->d_name);
  77:         if (stat(name, &devstat) < 0) {
  78:             perror(name);
  79:             exit(4);
  80:         }
  81:         if (stbuf.st_dev == devstat.st_rdev &&
  82:             (devstat.st_mode & IFMT) == IFBLK)
  83:             break;
  84:     }
  85:     closedir(dirp);
  86:     if (dp == NULL) {
  87:         printf("Cannot find dev 0%o corresponding to %s\n",
  88:             stbuf.st_rdev, argv[1]);
  89:         exit(5);
  90:     }
  91:     if ((fsi = open(name, 0)) < 0) {
  92:         perror(name);
  93:         exit(6);
  94:     }
  95:     fs = &sblock;
  96:     rdfs(SBLOCK, SBSIZE, (char *)fs);
  97:     for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
  98:         number = atoi(*argv);
  99:         if (chkuse(number, 1))
 100:             continue;
 101:         if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
 102:             perror(*argv);
 103:             errs++;
 104:         }
 105:     }
 106:     printf("Don't forget to run ``fsck %s''\n", name);
 107:     exit(errs);
 108: }
 109: 
 110: chkuse(blkno, cnt)
 111:     daddr_t blkno;
 112:     int cnt;
 113: {
 114:     int cg;
 115:     daddr_t fsbn, bn;
 116: 
 117:     fsbn = dbtofsb(fs, blkno);
 118:     if ((unsigned)(fsbn+cnt) > fs->fs_size) {
 119:         printf("block %d out of range of file system\n", blkno);
 120:         return (1);
 121:     }
 122:     cg = dtog(fs, fsbn);
 123:     if (fsbn < cgdmin(fs, cg)) {
 124:         if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
 125:             printf("block %d in non-data area: cannot attach\n",
 126:                 blkno);
 127:             return (1);
 128:         }
 129:     } else {
 130:         if ((fsbn+cnt) > cgbase(fs, cg+1)) {
 131:             printf("block %d in non-data area: cannot attach\n",
 132:                 blkno);
 133:             return (1);
 134:         }
 135:     }
 136:     rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
 137:         (char *)&acg);
 138:     if (acg.cg_magic != CG_MAGIC) {
 139:         fprintf(stderr, "cg %d: bad magic number\n", cg);
 140:         errs++;
 141:         return (1);
 142:     }
 143:     bn = dtogd(fs, fsbn);
 144:     if (isclr(acg.cg_free, bn))
 145:         printf("Warning: sector %d is in use\n", blkno);
 146:     return (0);
 147: }
 148: 
 149: /*
 150:  * read a block from the file system
 151:  */
 152: rdfs(bno, size, bf)
 153:     int bno, size;
 154:     char *bf;
 155: {
 156:     int n;
 157: 
 158:     if (lseek(fsi, bno * DEV_BSIZE, 0) < 0) {
 159:         printf("seek error: %ld\n", bno);
 160:         perror("rdfs");
 161:         exit(1);
 162:     }
 163:     n = read(fsi, bf, size);
 164:     if(n != size) {
 165:         printf("read error: %ld\n", bno);
 166:         perror("rdfs");
 167:         exit(1);
 168:     }
 169: }

Defined functions

chkuse defined in line 110; used 1 times
  • in line 99
main defined in line 51; never used
rdfs defined in line 152; used 2 times

Defined variables

buf defined in line 48; never used
copyright defined in line 8; never used
errs defined in line 46; used 3 times
fs defined in line 44; used 14 times
fsi defined in line 45; used 3 times
fso defined in line 45; never used
sccsid defined in line 14; never used

Defined macros

acg defined in line 43; used 3 times
sblock defined in line 38; used 2 times
Last modified: 1985-06-24
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 776
Valid CSS Valid XHTML 1.0 Strict