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