badsect.c revision 1.34
1/* $NetBSD: badsect.c,v 1.34 2016/09/05 01:09:57 sevan Exp $ */ 2 3/* 4 * Copyright (c) 1981, 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT("@(#) Copyright (c) 1981, 1983, 1993\ 35 The Regents of the University of California. All rights reserved."); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)badsect.c 8.2 (Berkeley) 5/4/95"; 41#else 42__RCSID("$NetBSD: badsect.c,v 1.34 2016/09/05 01:09:57 sevan Exp $"); 43#endif 44#endif /* not lint */ 45 46/* 47 * badsect 48 * 49 * Badsect takes a list of file-system relative sector numbers 50 * and makes files containing the blocks of which these sectors are a part. 51 * It can be used to contain sectors which have problems if these sectors 52 * are not part of the bad file for the pack (see bad144). For instance, 53 * this program can be used if the driver for the file system in question 54 * does not support bad block forwarding. 55 */ 56#include <sys/param.h> 57#include <sys/dir.h> 58#include <sys/stat.h> 59 60#include <ufs/ufs/dinode.h> 61#include <ufs/ufs/ufs_bswap.h> 62#include <ufs/ffs/fs.h> 63#include <ufs/ffs/ffs_extern.h> 64 65#include <fcntl.h> 66#include <paths.h> 67#include <stdio.h> 68#include <stdlib.h> 69#include <string.h> 70#include <unistd.h> 71#include <err.h> 72 73static union { 74 struct fs fs; 75 char fsx[SBLOCKSIZE]; 76} ufs; 77#define sblock ufs.fs 78static union { 79 struct cg cg; 80 char cgx[MAXBSIZE]; 81} ucg; 82#define acg ucg.cg 83static struct fs *fs; 84static int fsi; 85static int errs; 86static off_t dev_bsize = 1; 87static int needswap = 0; 88static int is_ufs2; 89 90static void rdfs(off_t, size_t, void *); 91static int chkuse(daddr_t, int); 92 93static const off_t sblock_try[] = SBLOCKSEARCH; 94 95int 96main(int argc, char *argv[]) 97{ 98 daddr_t number; 99 struct stat stbuf, devstat; 100 struct direct *dp; 101 int i, did = 0; 102 DIR *dirp; 103 char name[MAXPATHLEN]; 104 size_t dl = sizeof(_PATH_DEV); 105 106 if (argc < 3) { 107 (void)fprintf(stderr, "usage: %s bbdir blkno [ blkno ]\n", 108 getprogname()); 109 exit(1); 110 } 111 if (chdir(argv[1]) == -1) 112 err(1, "Cannot change directory to `%s'", argv[1]); 113 114 if (stat(".", &stbuf) == -1) 115 err(1, "Cannot stat `%s'", argv[1]); 116 117 (void)strlcpy(name, _PATH_DEV, sizeof(name)); 118 if ((dirp = opendir(name)) == NULL) 119 err(1, "Cannot opendir `%s'", argv[1]); 120 121 while ((dp = readdir(dirp)) != NULL) { 122 (void)strlcpy(name + dl - 1, dp->d_name, sizeof(name) - dl + 1); 123 if (stat(name, &devstat) == -1) 124 err(1, "Cannot stat `%s'", name); 125 if (stbuf.st_dev == devstat.st_rdev && 126 S_ISBLK(devstat.st_mode)) 127 break; 128 } 129 130 if (dp == NULL) 131 errx(1, "Cannot find dev 0%llo corresponding to %s", 132 (long long)stbuf.st_rdev, argv[1]); 133 134 /* 135 * The filesystem is mounted; use the character device instead. 136 * XXX - Assume that prepending an `r' will give us the name of 137 * the character device. 138 */ 139 name[dl - 1] = 'r'; 140 (void)strlcpy(name + dl, dp->d_name, sizeof(name) - dl); 141 (void)closedir(dirp); /* now *dp is invalid */ 142 143 if ((fsi = open(name, O_RDONLY)) == -1) 144 err(1, "Cannot open `%s'", argv[1]); 145 146 fs = &sblock; 147 148 for (i = 0; ; i++) { 149 if (sblock_try[i] == -1) 150 errx(1, "%s: bad superblock", name); 151 rdfs(sblock_try[i], SBLOCKSIZE, fs); 152 switch (fs->fs_magic) { 153 case FS_UFS2_MAGIC: 154 is_ufs2 = 1; 155 /* FALLTHROUGH */ 156 case FS_UFS1_MAGIC: 157 break; 158 case FS_UFS2_MAGIC_SWAPPED: 159 is_ufs2 = 1; 160 /* FALLTHROUGH */ 161 case FS_UFS1_MAGIC_SWAPPED: 162 needswap = 1; 163 ffs_sb_swap(fs, fs); 164 break; 165 default: 166 continue; 167 } 168 169 /* Ensure we don't use 1st alternate if ffsv1 and bs=64k */ 170 if (is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED) { 171 if (fs->fs_sblockloc != sblock_try[i]) 172 continue; 173 } else { 174 if (sblock_try[i] == SBLOCK_UFS2) 175 continue; 176 } 177 break; 178 } 179 180 dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1); 181 for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 182 number = atoi(*argv); 183 if (chkuse(number, 1)) 184 continue; 185 if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR, 186 (dev_t)FFS_DBTOFSB(fs, number)) == -1) { 187 warn("Cannot mknod `%s'", *argv); 188 errs++; 189 continue; 190 } 191 did++; 192 } 193 194 if (did) 195 warnx("Don't forget to run `fsck %s'", name); 196 else 197 warnx("File system `%s' was not modified", name); 198 return errs; 199} 200 201static int 202chkuse(off_t blkno, int cnt) 203{ 204 int cg; 205 off_t fsbn, bn, fsbe; 206 207 fsbn = FFS_DBTOFSB(fs, blkno); 208 fsbe = fsbn + cnt; 209 if (fsbe > fs->fs_size) { 210 warnx("block %lld out of range of file system", 211 (long long)blkno); 212 return 1; 213 } 214 215 cg = (int)dtog(fs, fsbn); 216 if (fsbn < cgdmin(fs, cg)) { 217 if (cg == 0 || fsbe > cgsblock(fs, cg)) { 218 warnx("block %lld in superblock area: cannot attach", 219 (long long)blkno); 220 return 1; 221 } 222 } else { 223 if (fsbe > cgbase(fs, cg + 1)) { 224 warnx("block %lld in beyond end of cylinder group: " 225 "cannot attach", (long long)blkno); 226 return 1; 227 } 228 } 229 230 rdfs(FFS_FSBTODB(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, &acg); 231 232 if (!cg_chkmagic(&acg, needswap)) { 233 warnx("cg %d: bad magic number", cg); 234 errs++; 235 return 1; 236 } 237 238 bn = dtogd(fs, fsbn); 239 if (isclr(cg_blksfree(&acg, needswap), bn)) 240 warnx("Warning: sector %lld is in use", (long long)blkno); 241 242 return 0; 243} 244 245/* 246 * read a block from the file system 247 */ 248static void 249rdfs(off_t bno, size_t size, void *bf) 250{ 251 ssize_t n; 252 253 if (lseek(fsi, bno * dev_bsize, SEEK_SET) == -1) 254 err(1, "seek error at block %lld", (long long)bno); 255 256 switch (n = read(fsi, bf, size)) { 257 case -1: 258 err(1, "read error at block %lld", (long long)bno); 259 break; 260 261 default: 262 if ((size_t)n == size) 263 return; 264 errx(1, "incomplete read at block %lld", (long long)bno); 265 } 266} 267