122486Sdist /* 236194Sbostic * Copyright (c) 1981, 1983 The Regents of the University of California. 336194Sbostic * All rights reserved. 436194Sbostic * 5*42698Sbostic * %sccs.include.redist.c% 622486Sdist */ 73724Sroot 822486Sdist #ifndef lint 922486Sdist char copyright[] = 1036194Sbostic "@(#) Copyright (c) 1981, 1983 The Regents of the University of California.\n\ 1122486Sdist All rights reserved.\n"; 1236194Sbostic #endif /* not lint */ 1322486Sdist 1422486Sdist #ifndef lint 15*42698Sbostic static char sccsid[] = "@(#)badsect.c 5.9 (Berkeley) 06/01/90"; 1636194Sbostic #endif /* not lint */ 1722486Sdist 183724Sroot /* 193724Sroot * badsect 203724Sroot * 213724Sroot * Badsect takes a list of file-system relative sector numbers 223724Sroot * and makes files containing the blocks of which these sectors are a part. 233724Sroot * It can be used to contain sectors which have problems if these sectors 243724Sroot * are not part of the bad file for the pack (see bad144). For instance, 253724Sroot * this program can be used if the driver for the file system in question 263724Sroot * does not support bad block forwarding. 273724Sroot */ 283724Sroot #include <sys/param.h> 2911272Smckusick #include <sys/dir.h> 3011272Smckusick #include <sys/stat.h> 3138502Sbostic #include <ufs/fs.h> 3241377Smckusick #include <ufs/dinode.h> 3337944Sbostic #include <stdio.h> 3437944Sbostic #include <paths.h> 353724Sroot 3611272Smckusick union { 3711272Smckusick struct fs fs; 3811272Smckusick char fsx[SBSIZE]; 3911272Smckusick } ufs; 4011272Smckusick #define sblock ufs.fs 4111272Smckusick union { 4211272Smckusick struct cg cg; 4311272Smckusick char cgx[MAXBSIZE]; 4411272Smckusick } ucg; 4511272Smckusick #define acg ucg.cg 4611272Smckusick struct fs *fs; 4711272Smckusick int fso, fsi; 4811272Smckusick int errs; 4930558Smckusick long dev_bsize = 1; 5011272Smckusick 5111272Smckusick char buf[MAXBSIZE]; 5211272Smckusick 5311272Smckusick 543724Sroot main(argc, argv) 553724Sroot int argc; 5611272Smckusick char *argv[]; 573724Sroot { 5811272Smckusick daddr_t number; 5911272Smckusick struct stat stbuf, devstat; 6011272Smckusick register struct direct *dp; 6111272Smckusick DIR *dirp; 6211272Smckusick int fd; 6311272Smckusick char name[BUFSIZ]; 643724Sroot 6511272Smckusick if (argc < 3) { 6611272Smckusick fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n"); 6711272Smckusick exit(1); 683724Sroot } 6911272Smckusick if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) { 7011272Smckusick perror(argv[1]); 7111272Smckusick exit(2); 7211272Smckusick } 7337944Sbostic strcpy(name, _PATH_DEV); 7411272Smckusick if ((dirp = opendir(name)) == NULL) { 7511272Smckusick perror(name); 7611272Smckusick exit(3); 7711272Smckusick } 7811272Smckusick while ((dp = readdir(dirp)) != NULL) { 7911272Smckusick strcpy(&name[5], dp->d_name); 8011272Smckusick if (stat(name, &devstat) < 0) { 8111272Smckusick perror(name); 8211272Smckusick exit(4); 8311272Smckusick } 8411272Smckusick if (stbuf.st_dev == devstat.st_rdev && 8511272Smckusick (devstat.st_mode & IFMT) == IFBLK) 8611272Smckusick break; 8711272Smckusick } 8811272Smckusick closedir(dirp); 8911272Smckusick if (dp == NULL) { 9011272Smckusick printf("Cannot find dev 0%o corresponding to %s\n", 9111272Smckusick stbuf.st_rdev, argv[1]); 9211272Smckusick exit(5); 9311272Smckusick } 9411272Smckusick if ((fsi = open(name, 0)) < 0) { 9511272Smckusick perror(name); 9611272Smckusick exit(6); 9711272Smckusick } 9811272Smckusick fs = &sblock; 9930558Smckusick rdfs(SBOFF, SBSIZE, (char *)fs); 10030558Smckusick dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 10111272Smckusick for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 10211272Smckusick number = atoi(*argv); 10311272Smckusick if (chkuse(number, 1)) 10411272Smckusick continue; 10511272Smckusick if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) { 10611272Smckusick perror(*argv); 10711272Smckusick errs++; 10811272Smckusick } 10911272Smckusick } 11011272Smckusick printf("Don't forget to run ``fsck %s''\n", name); 1113724Sroot exit(errs); 1123724Sroot } 11311272Smckusick 11411272Smckusick chkuse(blkno, cnt) 11511272Smckusick daddr_t blkno; 11611272Smckusick int cnt; 11711272Smckusick { 11811272Smckusick int cg; 11911272Smckusick daddr_t fsbn, bn; 12011272Smckusick 12111272Smckusick fsbn = dbtofsb(fs, blkno); 12211272Smckusick if ((unsigned)(fsbn+cnt) > fs->fs_size) { 12311272Smckusick printf("block %d out of range of file system\n", blkno); 12411272Smckusick return (1); 12511272Smckusick } 12611272Smckusick cg = dtog(fs, fsbn); 12711272Smckusick if (fsbn < cgdmin(fs, cg)) { 12811272Smckusick if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) { 12911272Smckusick printf("block %d in non-data area: cannot attach\n", 13011272Smckusick blkno); 13111272Smckusick return (1); 13211272Smckusick } 13311272Smckusick } else { 13411272Smckusick if ((fsbn+cnt) > cgbase(fs, cg+1)) { 13511272Smckusick printf("block %d in non-data area: cannot attach\n", 13611272Smckusick blkno); 13711272Smckusick return (1); 13811272Smckusick } 13911272Smckusick } 14011272Smckusick rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, 14111272Smckusick (char *)&acg); 14234575Smckusick if (!cg_chkmagic(&acg)) { 14311272Smckusick fprintf(stderr, "cg %d: bad magic number\n", cg); 14411272Smckusick errs++; 14523722Sbloom return (1); 14611272Smckusick } 14711272Smckusick bn = dtogd(fs, fsbn); 14834575Smckusick if (isclr(cg_blksfree(&acg), bn)) 14911272Smckusick printf("Warning: sector %d is in use\n", blkno); 15011272Smckusick return (0); 15111272Smckusick } 15211272Smckusick 15311272Smckusick /* 15411272Smckusick * read a block from the file system 15511272Smckusick */ 15611272Smckusick rdfs(bno, size, bf) 15711272Smckusick int bno, size; 15811272Smckusick char *bf; 15911272Smckusick { 16011272Smckusick int n; 16111272Smckusick 16230558Smckusick if (lseek(fsi, bno * dev_bsize, 0) < 0) { 16311272Smckusick printf("seek error: %ld\n", bno); 16411272Smckusick perror("rdfs"); 16511272Smckusick exit(1); 16611272Smckusick } 16711272Smckusick n = read(fsi, bf, size); 16811272Smckusick if(n != size) { 16911272Smckusick printf("read error: %ld\n", bno); 17011272Smckusick perror("rdfs"); 17111272Smckusick exit(1); 17211272Smckusick } 17311272Smckusick } 174