badsect.c revision 114589
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 34114589Sobrien#if 0 351558Srgrimes#ifndef lint 367590Sbdestatic const char copyright[] = 371558Srgrimes"@(#) Copyright (c) 1981, 1983, 1993\n\ 381558Srgrimes The Regents of the University of California. All rights reserved.\n"; 391558Srgrimes#endif /* not lint */ 401558Srgrimes 411558Srgrimes#ifndef lint 427590Sbdestatic const char sccsid[] = "@(#)badsect.c 8.1 (Berkeley) 6/5/93"; 4336626Scharnier#endif 44114589Sobrien#endif 45114589Sobrien#include <sys/cdefs.h> 46114589Sobrien__FBSDID("$FreeBSD: head/sbin/badsect/badsect.c 114589 2003-05-03 18:41:59Z obrien $"); 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, 55102231Strhodes * 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 6298542Smckusick#include <ufs/ufs/dinode.h> 631558Srgrimes#include <ufs/ffs/fs.h> 641558Srgrimes 6536626Scharnier#include <err.h> 6618485Sbde#include <dirent.h> 671558Srgrimes#include <fcntl.h> 68109600Sjmallett#include <libufs.h> 691558Srgrimes#include <paths.h> 701558Srgrimes#include <stdio.h> 711558Srgrimes#include <stdlib.h> 7278732Sdd#include <string.h> 731558Srgrimes#include <unistd.h> 741558Srgrimes 75109600Sjmallett#define sblock disk.d_fs 76109600Sjmallett#define acg disk.d_cg 77109600Sjmallettstruct uufsd disk; 78109600Sjmallettstruct fs *fs = &sblock; 791558Srgrimesint errs; 801558Srgrimes 8192538Simpint chkuse(daddr_t, int); 821558Srgrimes 8336626Scharnierstatic void 8436626Scharnierusage(void) 8536626Scharnier{ 8636626Scharnier fprintf(stderr, "usage: badsect bbdir blkno ...\n"); 8736626Scharnier exit(1); 8836626Scharnier} 8936626Scharnier 901558Srgrimesint 9192538Simpmain(int argc, char *argv[]) 921558Srgrimes{ 937589Sbde daddr_t diskbn; 941558Srgrimes daddr_t number; 951558Srgrimes struct stat stbuf, devstat; 9692753Simp struct dirent *dp; 971558Srgrimes DIR *dirp; 987589Sbde char name[2 * MAXPATHLEN]; 997589Sbde char *name_dir_end; 1001558Srgrimes 10136626Scharnier if (argc < 3) 10236626Scharnier usage(); 10336626Scharnier if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) 10436626Scharnier err(2, "%s", argv[1]); 1051558Srgrimes strcpy(name, _PATH_DEV); 10636626Scharnier if ((dirp = opendir(name)) == NULL) 10736626Scharnier err(3, "%s", name); 1087589Sbde name_dir_end = name + strlen(name); 1091558Srgrimes while ((dp = readdir(dirp)) != NULL) { 1107589Sbde strcpy(name_dir_end, dp->d_name); 11136626Scharnier if (lstat(name, &devstat) < 0) 11236626Scharnier err(4, "%s", name); 1131558Srgrimes if (stbuf.st_dev == devstat.st_rdev && 11468521Sadrian (devstat.st_mode & IFMT) == IFCHR) 1151558Srgrimes break; 1161558Srgrimes } 1171558Srgrimes closedir(dirp); 1181558Srgrimes if (dp == NULL) { 1197590Sbde printf("Cannot find dev 0%lo corresponding to %s\n", 12037233Sbde (u_long)stbuf.st_rdev, argv[1]); 1211558Srgrimes exit(5); 1221558Srgrimes } 123109600Sjmallett if (ufs_disk_fillout(&disk, name) == -1) { 124109600Sjmallett if (disk.d_error != NULL) 125109600Sjmallett errx(6, "%s: %s", name, disk.d_error); 126109600Sjmallett else 127109600Sjmallett err(7, "%s", name); 12898542Smckusick } 1291558Srgrimes for (argc -= 2, argv += 2; argc > 0; argc--, argv++) { 1307589Sbde number = atol(*argv); 1311558Srgrimes if (chkuse(number, 1)) 1321558Srgrimes continue; 1337589Sbde /* 1347589Sbde * Print a warning if converting the block number to a dev_t 1357589Sbde * will truncate it. badsect was not very useful in versions 1367589Sbde * of BSD before 4.4 because dev_t was 16 bits and another 1377589Sbde * bit was lost by bogus sign extensions. 1387589Sbde */ 1397589Sbde diskbn = dbtofsb(fs, number); 1407589Sbde if ((dev_t)diskbn != diskbn) { 1417590Sbde printf("sector %ld cannot be represented as a dev_t\n", 14237233Sbde (long)number); 1437589Sbde errs++; 1447589Sbde } 1457589Sbde else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) { 14636626Scharnier warn("%s", *argv); 1471558Srgrimes errs++; 1481558Srgrimes } 1491558Srgrimes } 150109600Sjmallett ufs_disk_close(&disk); 1511558Srgrimes printf("Don't forget to run ``fsck %s''\n", name); 1521558Srgrimes exit(errs); 1531558Srgrimes} 1541558Srgrimes 1551558Srgrimesint 15692538Simpchkuse(daddr_t blkno, int cnt) 1571558Srgrimes{ 1581558Srgrimes int cg; 1591558Srgrimes daddr_t fsbn, bn; 1601558Srgrimes 1611558Srgrimes fsbn = dbtofsb(fs, blkno); 1621558Srgrimes if ((unsigned)(fsbn+cnt) > fs->fs_size) { 163102231Strhodes printf("block %ld out of range of file system\n", (long)blkno); 1641558Srgrimes return (1); 1651558Srgrimes } 1661558Srgrimes cg = dtog(fs, fsbn); 1671558Srgrimes if (fsbn < cgdmin(fs, cg)) { 1681558Srgrimes if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) { 1697590Sbde printf("block %ld in non-data area: cannot attach\n", 17037233Sbde (long)blkno); 1711558Srgrimes return (1); 1721558Srgrimes } 1731558Srgrimes } else { 1741558Srgrimes if ((fsbn+cnt) > cgbase(fs, cg+1)) { 1757590Sbde printf("block %ld in non-data area: cannot attach\n", 17637233Sbde (long)blkno); 1771558Srgrimes return (1); 1781558Srgrimes } 1791558Srgrimes } 180109600Sjmallett if (cgread1(&disk, cg) != 1) { 181109600Sjmallett fprintf(stderr, "cg %d: could not be read\n", cg); 182109600Sjmallett errs++; 183109600Sjmallett return (1); 184109600Sjmallett } 1851558Srgrimes if (!cg_chkmagic(&acg)) { 1861558Srgrimes fprintf(stderr, "cg %d: bad magic number\n", cg); 1871558Srgrimes errs++; 1881558Srgrimes return (1); 1891558Srgrimes } 1901558Srgrimes bn = dtogd(fs, fsbn); 1911558Srgrimes if (isclr(cg_blksfree(&acg), bn)) 19237233Sbde printf("Warning: sector %ld is in use\n", (long)blkno); 1931558Srgrimes return (0); 1941558Srgrimes} 195