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.4 (Berkeley) 05/31/88"; 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 long dev_bsize = 1; 48 49 char buf[MAXBSIZE]; 50 51 52 main(argc, argv) 53 int argc; 54 char *argv[]; 55 { 56 daddr_t number; 57 struct stat stbuf, devstat; 58 register struct direct *dp; 59 DIR *dirp; 60 int fd; 61 char name[BUFSIZ]; 62 63 if (argc < 3) { 64 fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n"); 65 exit(1); 66 } 67 if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) { 68 perror(argv[1]); 69 exit(2); 70 } 71 strcpy(name, "/dev/"); 72 if ((dirp = opendir(name)) == NULL) { 73 perror(name); 74 exit(3); 75 } 76 while ((dp = readdir(dirp)) != NULL) { 77 strcpy(&name[5], dp->d_name); 78 if (stat(name, &devstat) < 0) { 79 perror(name); 80 exit(4); 81 } 82 if (stbuf.st_dev == devstat.st_rdev && 83 (devstat.st_mode & IFMT) == IFBLK) 84 break; 85 } 86 closedir(dirp); 87 if (dp == NULL) { 88 printf("Cannot find dev 0%o corresponding to %s\n", 89 stbuf.st_rdev, argv[1]); 90 exit(5); 91 } 92 if ((fsi = open(name, 0)) < 0) { 93 perror(name); 94 exit(6); 95 } 96 fs = &sblock; 97 rdfs(SBOFF, SBSIZE, (char *)fs); 98 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 99 for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 100 number = atoi(*argv); 101 if (chkuse(number, 1)) 102 continue; 103 if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) { 104 perror(*argv); 105 errs++; 106 } 107 } 108 printf("Don't forget to run ``fsck %s''\n", name); 109 exit(errs); 110 } 111 112 chkuse(blkno, cnt) 113 daddr_t blkno; 114 int cnt; 115 { 116 int cg; 117 daddr_t fsbn, bn; 118 119 fsbn = dbtofsb(fs, blkno); 120 if ((unsigned)(fsbn+cnt) > fs->fs_size) { 121 printf("block %d out of range of file system\n", blkno); 122 return (1); 123 } 124 cg = dtog(fs, fsbn); 125 if (fsbn < cgdmin(fs, cg)) { 126 if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) { 127 printf("block %d in non-data area: cannot attach\n", 128 blkno); 129 return (1); 130 } 131 } else { 132 if ((fsbn+cnt) > cgbase(fs, cg+1)) { 133 printf("block %d in non-data area: cannot attach\n", 134 blkno); 135 return (1); 136 } 137 } 138 rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, 139 (char *)&acg); 140 if (!cg_chkmagic(&acg)) { 141 fprintf(stderr, "cg %d: bad magic number\n", cg); 142 errs++; 143 return (1); 144 } 145 bn = dtogd(fs, fsbn); 146 if (isclr(cg_blksfree(&acg), bn)) 147 printf("Warning: sector %d is in use\n", blkno); 148 return (0); 149 } 150 151 /* 152 * read a block from the file system 153 */ 154 rdfs(bno, size, bf) 155 int bno, size; 156 char *bf; 157 { 158 int n; 159 160 if (lseek(fsi, bno * dev_bsize, 0) < 0) { 161 printf("seek error: %ld\n", bno); 162 perror("rdfs"); 163 exit(1); 164 } 165 n = read(fsi, bf, size); 166 if(n != size) { 167 printf("read error: %ld\n", bno); 168 perror("rdfs"); 169 exit(1); 170 } 171 } 172