badsect.c revision 78732
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 78732 2001-06-24 23:04:23Z dd $";
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>
601558Srgrimes
611558Srgrimes#include <ufs/ffs/fs.h>
621558Srgrimes#include <ufs/ufs/dinode.h>
631558Srgrimes
6436626Scharnier#include <err.h>
6518485Sbde#include <dirent.h>
661558Srgrimes#include <fcntl.h>
671558Srgrimes#include <paths.h>
681558Srgrimes#include <stdio.h>
691558Srgrimes#include <stdlib.h>
7078732Sdd#include <string.h>
711558Srgrimes#include <unistd.h>
721558Srgrimes
731558Srgrimesunion {
741558Srgrimes	struct	fs fs;
751558Srgrimes	char	fsx[SBSIZE];
761558Srgrimes} ufs;
771558Srgrimes#define sblock	ufs.fs
781558Srgrimesunion {
791558Srgrimes	struct	cg cg;
801558Srgrimes	char	cgx[MAXBSIZE];
811558Srgrimes} ucg;
821558Srgrimes#define	acg	ucg.cg
831558Srgrimesstruct	fs *fs;
841558Srgrimesint	fso, fsi;
851558Srgrimesint	errs;
861558Srgrimeslong	dev_bsize = 1;
871558Srgrimes
881558Srgrimeschar buf[MAXBSIZE];
891558Srgrimes
901558Srgrimesvoid	rdfs __P((daddr_t, int, char *));
911558Srgrimesint	chkuse __P((daddr_t, int));
921558Srgrimes
9336626Scharnierstatic void
9436626Scharnierusage(void)
9536626Scharnier{
9636626Scharnier	fprintf(stderr, "usage: badsect bbdir blkno ...\n");
9736626Scharnier	exit(1);
9836626Scharnier}
9936626Scharnier
1001558Srgrimesint
1011558Srgrimesmain(argc, argv)
1021558Srgrimes	int argc;
1031558Srgrimes	char *argv[];
1041558Srgrimes{
1057589Sbde	daddr_t diskbn;
1061558Srgrimes	daddr_t number;
1071558Srgrimes	struct stat stbuf, devstat;
10818485Sbde	register struct dirent *dp;
1091558Srgrimes	DIR *dirp;
1107589Sbde	char name[2 * MAXPATHLEN];
1117589Sbde	char *name_dir_end;
1121558Srgrimes
11336626Scharnier	if (argc < 3)
11436626Scharnier		usage();
11536626Scharnier	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0)
11636626Scharnier		err(2, "%s", argv[1]);
1171558Srgrimes	strcpy(name, _PATH_DEV);
11836626Scharnier	if ((dirp = opendir(name)) == NULL)
11936626Scharnier		err(3, "%s", name);
1207589Sbde	name_dir_end = name + strlen(name);
1211558Srgrimes	while ((dp = readdir(dirp)) != NULL) {
1227589Sbde		strcpy(name_dir_end, dp->d_name);
12336626Scharnier		if (lstat(name, &devstat) < 0)
12436626Scharnier			err(4, "%s", name);
1251558Srgrimes		if (stbuf.st_dev == devstat.st_rdev &&
12668521Sadrian		    (devstat.st_mode & IFMT) == IFCHR)
1271558Srgrimes			break;
1281558Srgrimes	}
1291558Srgrimes	closedir(dirp);
1301558Srgrimes	if (dp == NULL) {
1317590Sbde		printf("Cannot find dev 0%lo corresponding to %s\n",
13237233Sbde		    (u_long)stbuf.st_rdev, argv[1]);
1331558Srgrimes		exit(5);
1341558Srgrimes	}
13536626Scharnier	if ((fsi = open(name, O_RDONLY)) < 0)
13636626Scharnier		err(6, "%s", name);
1371558Srgrimes	fs = &sblock;
1381558Srgrimes	rdfs(SBOFF, SBSIZE, (char *)fs);
1391558Srgrimes	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
1401558Srgrimes	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
1417589Sbde		number = atol(*argv);
1421558Srgrimes		if (chkuse(number, 1))
1431558Srgrimes			continue;
1447589Sbde		/*
1457589Sbde		 * Print a warning if converting the block number to a dev_t
1467589Sbde		 * will truncate it.  badsect was not very useful in versions
1477589Sbde		 * of BSD before 4.4 because dev_t was 16 bits and another
1487589Sbde		 * bit was lost by bogus sign extensions.
1497589Sbde		 */
1507589Sbde		diskbn = dbtofsb(fs, number);
1517589Sbde		if ((dev_t)diskbn != diskbn) {
1527590Sbde			printf("sector %ld cannot be represented as a dev_t\n",
15337233Sbde			    (long)number);
1547589Sbde			errs++;
1557589Sbde		}
1567589Sbde		else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) {
15736626Scharnier			warn("%s", *argv);
1581558Srgrimes			errs++;
1591558Srgrimes		}
1601558Srgrimes	}
1611558Srgrimes	printf("Don't forget to run ``fsck %s''\n", name);
1621558Srgrimes	exit(errs);
1631558Srgrimes}
1641558Srgrimes
1651558Srgrimesint
1661558Srgrimeschkuse(blkno, cnt)
1671558Srgrimes	daddr_t blkno;
1681558Srgrimes	int cnt;
1691558Srgrimes{
1701558Srgrimes	int cg;
1711558Srgrimes	daddr_t fsbn, bn;
1721558Srgrimes
1731558Srgrimes	fsbn = dbtofsb(fs, blkno);
1741558Srgrimes	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
17537233Sbde		printf("block %ld out of range of file system\n", (long)blkno);
1761558Srgrimes		return (1);
1771558Srgrimes	}
1781558Srgrimes	cg = dtog(fs, fsbn);
1791558Srgrimes	if (fsbn < cgdmin(fs, cg)) {
1801558Srgrimes		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
1817590Sbde			printf("block %ld in non-data area: cannot attach\n",
18237233Sbde			    (long)blkno);
1831558Srgrimes			return (1);
1841558Srgrimes		}
1851558Srgrimes	} else {
1861558Srgrimes		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
1877590Sbde			printf("block %ld in non-data area: cannot attach\n",
18837233Sbde			    (long)blkno);
1891558Srgrimes			return (1);
1901558Srgrimes		}
1911558Srgrimes	}
1921558Srgrimes	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
1931558Srgrimes	    (char *)&acg);
1941558Srgrimes	if (!cg_chkmagic(&acg)) {
1951558Srgrimes		fprintf(stderr, "cg %d: bad magic number\n", cg);
1961558Srgrimes		errs++;
1971558Srgrimes		return (1);
1981558Srgrimes	}
1991558Srgrimes	bn = dtogd(fs, fsbn);
2001558Srgrimes	if (isclr(cg_blksfree(&acg), bn))
20137233Sbde		printf("Warning: sector %ld is in use\n", (long)blkno);
2021558Srgrimes	return (0);
2031558Srgrimes}
2041558Srgrimes
2051558Srgrimes/*
2061558Srgrimes * read a block from the file system
2071558Srgrimes */
2081558Srgrimesvoid
2091558Srgrimesrdfs(bno, size, bf)
2101558Srgrimes	daddr_t bno;
2111558Srgrimes	int size;
2121558Srgrimes	char *bf;
2131558Srgrimes{
2141558Srgrimes	int n;
2151558Srgrimes
2161558Srgrimes	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) {
21737233Sbde		printf("seek error: %ld\n", (long)bno);
21836626Scharnier		err(1, "rdfs");
2191558Srgrimes	}
2201558Srgrimes	n = read(fsi, bf, size);
2211558Srgrimes	if (n != size) {
22237233Sbde		printf("read error: %ld\n", (long)bno);
22336626Scharnier		err(1, "rdfs");
2241558Srgrimes	}
2251558Srgrimes}
226