Annotation of 42BSD/etc/badsect.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.