badsect.c revision 96478
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 4136626Scharnier#if 0 427590Sbdestatic const char sccsid[] = "@(#)badsect.c 8.1 (Berkeley) 6/5/93"; 4336626Scharnier#endif 4436626Scharnierstatic const char rcsid[] = 4550476Speter "$FreeBSD: head/sbin/badsect/badsect.c 96478 2002-05-12 21:37:08Z phk $"; 461558Srgrimes#endif /* not lint */ 471558Srgrimes 481558Srgrimes/* 491558Srgrimes * badsect 501558Srgrimes * 511558Srgrimes * Badsect takes a list of file-system relative sector numbers 521558Srgrimes * and makes files containing the blocks of which these sectors are a part. 531558Srgrimes * It can be used to contain sectors which have problems if these sectors 541558Srgrimes * are not part of the bad file for the pack (see bad144). For instance, 551558Srgrimes * this program can be used if the driver for the file system in question 561558Srgrimes * does not support bad block forwarding. 571558Srgrimes */ 581558Srgrimes#include <sys/param.h> 591558Srgrimes#include <sys/stat.h> 6096478Sphk#include <sys/disklabel.h> 611558Srgrimes 621558Srgrimes#include <ufs/ffs/fs.h> 631558Srgrimes#include <ufs/ufs/dinode.h> 641558Srgrimes 6536626Scharnier#include <err.h> 6618485Sbde#include <dirent.h> 671558Srgrimes#include <fcntl.h> 681558Srgrimes#include <paths.h> 691558Srgrimes#include <stdio.h> 701558Srgrimes#include <stdlib.h> 7178732Sdd#include <string.h> 721558Srgrimes#include <unistd.h> 731558Srgrimes 741558Srgrimesunion { 751558Srgrimes struct fs fs; 761558Srgrimes char fsx[SBSIZE]; 771558Srgrimes} ufs; 781558Srgrimes#define sblock ufs.fs 791558Srgrimesunion { 801558Srgrimes struct cg cg; 811558Srgrimes char cgx[MAXBSIZE]; 821558Srgrimes} ucg; 831558Srgrimes#define acg ucg.cg 841558Srgrimesstruct fs *fs; 851558Srgrimesint fso, fsi; 861558Srgrimesint errs; 871558Srgrimeslong dev_bsize = 1; 881558Srgrimes 891558Srgrimeschar buf[MAXBSIZE]; 901558Srgrimes 9192538Simpvoid rdfs(daddr_t, int, char *); 9292538Simpint chkuse(daddr_t, int); 931558Srgrimes 9436626Scharnierstatic void 9536626Scharnierusage(void) 9636626Scharnier{ 9736626Scharnier fprintf(stderr, "usage: badsect bbdir blkno ...\n"); 9836626Scharnier exit(1); 9936626Scharnier} 10036626Scharnier 1011558Srgrimesint 10292538Simpmain(int argc, char *argv[]) 1031558Srgrimes{ 1047589Sbde daddr_t diskbn; 1051558Srgrimes daddr_t number; 1061558Srgrimes struct stat stbuf, devstat; 10792753Simp struct dirent *dp; 1081558Srgrimes DIR *dirp; 1097589Sbde char name[2 * MAXPATHLEN]; 1107589Sbde char *name_dir_end; 1111558Srgrimes 11236626Scharnier if (argc < 3) 11336626Scharnier usage(); 11436626Scharnier if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) 11536626Scharnier err(2, "%s", argv[1]); 1161558Srgrimes strcpy(name, _PATH_DEV); 11736626Scharnier if ((dirp = opendir(name)) == NULL) 11836626Scharnier err(3, "%s", name); 1197589Sbde name_dir_end = name + strlen(name); 1201558Srgrimes while ((dp = readdir(dirp)) != NULL) { 1217589Sbde strcpy(name_dir_end, dp->d_name); 12236626Scharnier if (lstat(name, &devstat) < 0) 12336626Scharnier err(4, "%s", name); 1241558Srgrimes if (stbuf.st_dev == devstat.st_rdev && 12568521Sadrian (devstat.st_mode & IFMT) == IFCHR) 1261558Srgrimes break; 1271558Srgrimes } 1281558Srgrimes closedir(dirp); 1291558Srgrimes if (dp == NULL) { 1307590Sbde printf("Cannot find dev 0%lo corresponding to %s\n", 13137233Sbde (u_long)stbuf.st_rdev, argv[1]); 1321558Srgrimes exit(5); 1331558Srgrimes } 13436626Scharnier if ((fsi = open(name, O_RDONLY)) < 0) 13536626Scharnier err(6, "%s", name); 1361558Srgrimes fs = &sblock; 1371558Srgrimes rdfs(SBOFF, SBSIZE, (char *)fs); 1381558Srgrimes dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 1391558Srgrimes for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 1407589Sbde number = atol(*argv); 1411558Srgrimes if (chkuse(number, 1)) 1421558Srgrimes continue; 1437589Sbde /* 1447589Sbde * Print a warning if converting the block number to a dev_t 1457589Sbde * will truncate it. badsect was not very useful in versions 1467589Sbde * of BSD before 4.4 because dev_t was 16 bits and another 1477589Sbde * bit was lost by bogus sign extensions. 1487589Sbde */ 1497589Sbde diskbn = dbtofsb(fs, number); 1507589Sbde if ((dev_t)diskbn != diskbn) { 1517590Sbde printf("sector %ld cannot be represented as a dev_t\n", 15237233Sbde (long)number); 1537589Sbde errs++; 1547589Sbde } 1557589Sbde else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) { 15636626Scharnier warn("%s", *argv); 1571558Srgrimes errs++; 1581558Srgrimes } 1591558Srgrimes } 1601558Srgrimes printf("Don't forget to run ``fsck %s''\n", name); 1611558Srgrimes exit(errs); 1621558Srgrimes} 1631558Srgrimes 1641558Srgrimesint 16592538Simpchkuse(daddr_t blkno, int cnt) 1661558Srgrimes{ 1671558Srgrimes int cg; 1681558Srgrimes daddr_t fsbn, bn; 1691558Srgrimes 1701558Srgrimes fsbn = dbtofsb(fs, blkno); 1711558Srgrimes if ((unsigned)(fsbn+cnt) > fs->fs_size) { 17237233Sbde printf("block %ld out of range of file system\n", (long)blkno); 1731558Srgrimes return (1); 1741558Srgrimes } 1751558Srgrimes cg = dtog(fs, fsbn); 1761558Srgrimes if (fsbn < cgdmin(fs, cg)) { 1771558Srgrimes if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) { 1787590Sbde printf("block %ld in non-data area: cannot attach\n", 17937233Sbde (long)blkno); 1801558Srgrimes return (1); 1811558Srgrimes } 1821558Srgrimes } else { 1831558Srgrimes if ((fsbn+cnt) > cgbase(fs, cg+1)) { 1847590Sbde printf("block %ld in non-data area: cannot attach\n", 18537233Sbde (long)blkno); 1861558Srgrimes return (1); 1871558Srgrimes } 1881558Srgrimes } 1891558Srgrimes rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, 1901558Srgrimes (char *)&acg); 1911558Srgrimes if (!cg_chkmagic(&acg)) { 1921558Srgrimes fprintf(stderr, "cg %d: bad magic number\n", cg); 1931558Srgrimes errs++; 1941558Srgrimes return (1); 1951558Srgrimes } 1961558Srgrimes bn = dtogd(fs, fsbn); 1971558Srgrimes if (isclr(cg_blksfree(&acg), bn)) 19837233Sbde printf("Warning: sector %ld is in use\n", (long)blkno); 1991558Srgrimes return (0); 2001558Srgrimes} 2011558Srgrimes 2021558Srgrimes/* 2031558Srgrimes * read a block from the file system 2041558Srgrimes */ 2051558Srgrimesvoid 20692538Simprdfs(daddr_t bno, int size, char *bf) 2071558Srgrimes{ 2081558Srgrimes int n; 2091558Srgrimes 2101558Srgrimes if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) { 21137233Sbde printf("seek error: %ld\n", (long)bno); 21236626Scharnier err(1, "rdfs"); 2131558Srgrimes } 2141558Srgrimes n = read(fsi, bf, size); 2151558Srgrimes if (n != size) { 21637233Sbde printf("read error: %ld\n", (long)bno); 21736626Scharnier err(1, "rdfs"); 2181558Srgrimes } 2191558Srgrimes} 220