1 /* 2 * Copyright (c) 1981, 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#) Copyright (c) 1981, 1983, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)badsect.c 8.1 (Berkeley) 6/5/93 35 * $FreeBSD: src/sbin/badsect/badsect.c,v 1.7.2.2 2001/07/30 10:30:04 dd Exp $ 36 * $DragonFly: src/sbin/badsect/badsect.c,v 1.3 2003/08/08 04:18:37 dillon Exp $ 37 */ 38 39 /* 40 * badsect 41 * 42 * Badsect takes a list of file-system relative sector numbers 43 * and makes files containing the blocks of which these sectors are a part. 44 * It can be used to contain sectors which have problems if these sectors 45 * are not part of the bad file for the pack (see bad144). For instance, 46 * this program can be used if the driver for the file system in question 47 * does not support bad block forwarding. 48 */ 49 #include <sys/param.h> 50 #include <sys/stat.h> 51 52 #include <vfs/ufs/fs.h> 53 #include <vfs/ufs/dinode.h> 54 55 #include <err.h> 56 #include <dirent.h> 57 #include <fcntl.h> 58 #include <paths.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 union { 65 struct fs fs; 66 char fsx[SBSIZE]; 67 } ufs; 68 #define sblock ufs.fs 69 union { 70 struct cg cg; 71 char cgx[MAXBSIZE]; 72 } ucg; 73 #define acg ucg.cg 74 struct fs *fs; 75 int fso, fsi; 76 int errs; 77 long dev_bsize = 1; 78 79 char buf[MAXBSIZE]; 80 81 void rdfs __P((daddr_t, int, char *)); 82 int chkuse __P((daddr_t, int)); 83 84 static void 85 usage(void) 86 { 87 fprintf(stderr, "usage: badsect bbdir blkno ...\n"); 88 exit(1); 89 } 90 91 int 92 main(argc, argv) 93 int argc; 94 char *argv[]; 95 { 96 daddr_t diskbn; 97 daddr_t number; 98 struct stat stbuf, devstat; 99 register struct dirent *dp; 100 DIR *dirp; 101 char name[2 * MAXPATHLEN]; 102 char *name_dir_end; 103 104 if (argc < 3) 105 usage(); 106 if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) 107 err(2, "%s", argv[1]); 108 strcpy(name, _PATH_DEV); 109 if ((dirp = opendir(name)) == NULL) 110 err(3, "%s", name); 111 name_dir_end = name + strlen(name); 112 while ((dp = readdir(dirp)) != NULL) { 113 strcpy(name_dir_end, dp->d_name); 114 if (lstat(name, &devstat) < 0) 115 err(4, "%s", name); 116 if (stbuf.st_dev == devstat.st_rdev && 117 (devstat.st_mode & IFMT) == IFCHR) 118 break; 119 } 120 closedir(dirp); 121 if (dp == NULL) { 122 printf("Cannot find dev 0%lo corresponding to %s\n", 123 (u_long)stbuf.st_rdev, argv[1]); 124 exit(5); 125 } 126 if ((fsi = open(name, O_RDONLY)) < 0) 127 err(6, "%s", name); 128 fs = &sblock; 129 rdfs(SBOFF, SBSIZE, (char *)fs); 130 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 131 for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 132 number = atol(*argv); 133 if (chkuse(number, 1)) 134 continue; 135 /* 136 * Print a warning if converting the block number to a dev_t 137 * will truncate it. badsect was not very useful in versions 138 * of BSD before 4.4 because dev_t was 16 bits and another 139 * bit was lost by bogus sign extensions. 140 */ 141 diskbn = dbtofsb(fs, number); 142 if ((dev_t)diskbn != diskbn) { 143 printf("sector %ld cannot be represented as a dev_t\n", 144 (long)number); 145 errs++; 146 } 147 else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) { 148 warn("%s", *argv); 149 errs++; 150 } 151 } 152 printf("Don't forget to run ``fsck %s''\n", name); 153 exit(errs); 154 } 155 156 int 157 chkuse(blkno, cnt) 158 daddr_t blkno; 159 int cnt; 160 { 161 int cg; 162 daddr_t fsbn, bn; 163 164 fsbn = dbtofsb(fs, blkno); 165 if ((unsigned)(fsbn+cnt) > fs->fs_size) { 166 printf("block %ld out of range of file system\n", (long)blkno); 167 return (1); 168 } 169 cg = dtog(fs, fsbn); 170 if (fsbn < cgdmin(fs, cg)) { 171 if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) { 172 printf("block %ld in non-data area: cannot attach\n", 173 (long)blkno); 174 return (1); 175 } 176 } else { 177 if ((fsbn+cnt) > cgbase(fs, cg+1)) { 178 printf("block %ld in non-data area: cannot attach\n", 179 (long)blkno); 180 return (1); 181 } 182 } 183 rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, 184 (char *)&acg); 185 if (!cg_chkmagic(&acg)) { 186 fprintf(stderr, "cg %d: bad magic number\n", cg); 187 errs++; 188 return (1); 189 } 190 bn = dtogd(fs, fsbn); 191 if (isclr(cg_blksfree(&acg), bn)) 192 printf("Warning: sector %ld is in use\n", (long)blkno); 193 return (0); 194 } 195 196 /* 197 * read a block from the file system 198 */ 199 void 200 rdfs(bno, size, bf) 201 daddr_t bno; 202 int size; 203 char *bf; 204 { 205 int n; 206 207 if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) { 208 printf("seek error: %ld\n", (long)bno); 209 err(1, "rdfs"); 210 } 211 n = read(fsi, bf, size); 212 if (n != size) { 213 printf("read error: %ld\n", (long)bno); 214 err(1, "rdfs"); 215 } 216 } 217