badsect.c revision 7590
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1981, 1983, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 357590Sbdestatic const char copyright[] = 361558Srgrimes"@(#) Copyright (c) 1981, 1983, 1993\n\ 371558Srgrimes The Regents of the University of California. All rights reserved.\n"; 381558Srgrimes#endif /* not lint */ 391558Srgrimes 401558Srgrimes#ifndef lint 417590Sbdestatic const char sccsid[] = "@(#)badsect.c 8.1 (Berkeley) 6/5/93"; 421558Srgrimes#endif /* not lint */ 431558Srgrimes 441558Srgrimes/* 451558Srgrimes * badsect 461558Srgrimes * 471558Srgrimes * Badsect takes a list of file-system relative sector numbers 481558Srgrimes * and makes files containing the blocks of which these sectors are a part. 491558Srgrimes * It can be used to contain sectors which have problems if these sectors 501558Srgrimes * are not part of the bad file for the pack (see bad144). For instance, 511558Srgrimes * this program can be used if the driver for the file system in question 521558Srgrimes * does not support bad block forwarding. 531558Srgrimes */ 541558Srgrimes#include <sys/param.h> 551558Srgrimes#include <sys/dir.h> 561558Srgrimes#include <sys/stat.h> 571558Srgrimes 581558Srgrimes#include <ufs/ffs/fs.h> 591558Srgrimes#include <ufs/ufs/dinode.h> 601558Srgrimes 611558Srgrimes#include <fcntl.h> 621558Srgrimes#include <paths.h> 631558Srgrimes#include <stdio.h> 641558Srgrimes#include <stdlib.h> 651558Srgrimes#include <unistd.h> 661558Srgrimes 671558Srgrimesunion { 681558Srgrimes struct fs fs; 691558Srgrimes char fsx[SBSIZE]; 701558Srgrimes} ufs; 711558Srgrimes#define sblock ufs.fs 721558Srgrimesunion { 731558Srgrimes struct cg cg; 741558Srgrimes char cgx[MAXBSIZE]; 751558Srgrimes} ucg; 761558Srgrimes#define acg ucg.cg 771558Srgrimesstruct fs *fs; 781558Srgrimesint fso, fsi; 791558Srgrimesint errs; 801558Srgrimeslong dev_bsize = 1; 811558Srgrimes 821558Srgrimeschar buf[MAXBSIZE]; 831558Srgrimes 841558Srgrimesvoid rdfs __P((daddr_t, int, char *)); 851558Srgrimesint chkuse __P((daddr_t, int)); 861558Srgrimes 871558Srgrimesint 881558Srgrimesmain(argc, argv) 891558Srgrimes int argc; 901558Srgrimes char *argv[]; 911558Srgrimes{ 927589Sbde daddr_t diskbn; 931558Srgrimes daddr_t number; 941558Srgrimes struct stat stbuf, devstat; 951558Srgrimes register struct direct *dp; 961558Srgrimes DIR *dirp; 977589Sbde char name[2 * MAXPATHLEN]; 987589Sbde char *name_dir_end; 991558Srgrimes 1001558Srgrimes if (argc < 3) { 1011558Srgrimes fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n"); 1021558Srgrimes exit(1); 1031558Srgrimes } 1041558Srgrimes if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) { 1051558Srgrimes perror(argv[1]); 1061558Srgrimes exit(2); 1071558Srgrimes } 1081558Srgrimes strcpy(name, _PATH_DEV); 1091558Srgrimes if ((dirp = opendir(name)) == NULL) { 1101558Srgrimes perror(name); 1111558Srgrimes exit(3); 1121558Srgrimes } 1137589Sbde name_dir_end = name + strlen(name); 1141558Srgrimes while ((dp = readdir(dirp)) != NULL) { 1157589Sbde strcpy(name_dir_end, dp->d_name); 1167589Sbde if (lstat(name, &devstat) < 0) { 1171558Srgrimes perror(name); 1181558Srgrimes exit(4); 1191558Srgrimes } 1201558Srgrimes if (stbuf.st_dev == devstat.st_rdev && 1211558Srgrimes (devstat.st_mode & IFMT) == IFBLK) 1221558Srgrimes break; 1231558Srgrimes } 1241558Srgrimes closedir(dirp); 1251558Srgrimes if (dp == NULL) { 1267590Sbde printf("Cannot find dev 0%lo corresponding to %s\n", 1271558Srgrimes stbuf.st_rdev, argv[1]); 1281558Srgrimes exit(5); 1291558Srgrimes } 1307589Sbde /* 1317589Sbde * Opening of a mounted on device is not allowed. 1327589Sbde * Attempt to open the raw device instead. 1337589Sbde */ 1347589Sbde memcpy(name_dir_end + 1, name_dir_end, strlen(name_dir_end) + 1); 1357589Sbde *name_dir_end = 'r'; 1367589Sbde if ((fsi = open(name, O_RDONLY)) < 0) { 1371558Srgrimes perror(name); 1381558Srgrimes exit(6); 1391558Srgrimes } 1401558Srgrimes fs = &sblock; 1411558Srgrimes rdfs(SBOFF, SBSIZE, (char *)fs); 1421558Srgrimes dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 1431558Srgrimes for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 1447589Sbde number = atol(*argv); 1451558Srgrimes if (chkuse(number, 1)) 1461558Srgrimes continue; 1477589Sbde /* 1487589Sbde * Print a warning if converting the block number to a dev_t 1497589Sbde * will truncate it. badsect was not very useful in versions 1507589Sbde * of BSD before 4.4 because dev_t was 16 bits and another 1517589Sbde * bit was lost by bogus sign extensions. 1527589Sbde */ 1537589Sbde diskbn = dbtofsb(fs, number); 1547589Sbde if ((dev_t)diskbn != diskbn) { 1557590Sbde printf("sector %ld cannot be represented as a dev_t\n", 1567589Sbde number); 1577589Sbde errs++; 1587589Sbde } 1597589Sbde else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) { 1601558Srgrimes perror(*argv); 1611558Srgrimes errs++; 1621558Srgrimes } 1631558Srgrimes } 1641558Srgrimes printf("Don't forget to run ``fsck %s''\n", name); 1651558Srgrimes exit(errs); 1661558Srgrimes} 1671558Srgrimes 1681558Srgrimesint 1691558Srgrimeschkuse(blkno, cnt) 1701558Srgrimes daddr_t blkno; 1711558Srgrimes int cnt; 1721558Srgrimes{ 1731558Srgrimes int cg; 1741558Srgrimes daddr_t fsbn, bn; 1751558Srgrimes 1761558Srgrimes fsbn = dbtofsb(fs, blkno); 1771558Srgrimes if ((unsigned)(fsbn+cnt) > fs->fs_size) { 1787590Sbde printf("block %ld out of range of file system\n", blkno); 1791558Srgrimes return (1); 1801558Srgrimes } 1811558Srgrimes cg = dtog(fs, fsbn); 1821558Srgrimes if (fsbn < cgdmin(fs, cg)) { 1831558Srgrimes if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) { 1847590Sbde printf("block %ld in non-data area: cannot attach\n", 1851558Srgrimes blkno); 1861558Srgrimes return (1); 1871558Srgrimes } 1881558Srgrimes } else { 1891558Srgrimes if ((fsbn+cnt) > cgbase(fs, cg+1)) { 1907590Sbde printf("block %ld in non-data area: cannot attach\n", 1911558Srgrimes blkno); 1921558Srgrimes return (1); 1931558Srgrimes } 1941558Srgrimes } 1951558Srgrimes rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, 1961558Srgrimes (char *)&acg); 1971558Srgrimes if (!cg_chkmagic(&acg)) { 1981558Srgrimes fprintf(stderr, "cg %d: bad magic number\n", cg); 1991558Srgrimes errs++; 2001558Srgrimes return (1); 2011558Srgrimes } 2021558Srgrimes bn = dtogd(fs, fsbn); 2031558Srgrimes if (isclr(cg_blksfree(&acg), bn)) 2047590Sbde printf("Warning: sector %ld is in use\n", blkno); 2051558Srgrimes return (0); 2061558Srgrimes} 2071558Srgrimes 2081558Srgrimes/* 2091558Srgrimes * read a block from the file system 2101558Srgrimes */ 2111558Srgrimesvoid 2121558Srgrimesrdfs(bno, size, bf) 2131558Srgrimes daddr_t bno; 2141558Srgrimes int size; 2151558Srgrimes char *bf; 2161558Srgrimes{ 2171558Srgrimes int n; 2181558Srgrimes 2191558Srgrimes if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) { 2201558Srgrimes printf("seek error: %ld\n", bno); 2211558Srgrimes perror("rdfs"); 2221558Srgrimes exit(1); 2231558Srgrimes } 2241558Srgrimes n = read(fsi, bf, size); 2251558Srgrimes if (n != size) { 2261558Srgrimes printf("read error: %ld\n", bno); 2271558Srgrimes perror("rdfs"); 2281558Srgrimes exit(1); 2291558Srgrimes } 2301558Srgrimes} 231