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 * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
327590Sbdestatic const char copyright[] =
331558Srgrimes"@(#) Copyright (c) 1981, 1983, 1993\n\
341558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351558Srgrimes#endif /* not lint */
361558Srgrimes
371558Srgrimes#ifndef lint
387590Sbdestatic const char sccsid[] = "@(#)badsect.c	8.1 (Berkeley) 6/5/93";
3936626Scharnier#endif
40114589Sobrien#endif
41114589Sobrien#include <sys/cdefs.h>
42114589Sobrien__FBSDID("$FreeBSD$");
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,
51102231Strhodes * 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/stat.h>
5696478Sphk#include <sys/disklabel.h>
571558Srgrimes
5898542Smckusick#include <ufs/ufs/dinode.h>
591558Srgrimes#include <ufs/ffs/fs.h>
601558Srgrimes
6136626Scharnier#include <err.h>
62139648Srwatson#include <errno.h>
6318485Sbde#include <dirent.h>
641558Srgrimes#include <fcntl.h>
65109600Sjmallett#include <libufs.h>
661558Srgrimes#include <paths.h>
671558Srgrimes#include <stdio.h>
681558Srgrimes#include <stdlib.h>
6978732Sdd#include <string.h>
701558Srgrimes#include <unistd.h>
711558Srgrimes
72109600Sjmallett#define sblock	disk.d_fs
73109600Sjmallett#define	acg	disk.d_cg
74227081Sedstatic struct	uufsd disk;
75227081Sedstatic struct	fs *fs = &sblock;
76227081Sedstatic int	errs;
771558Srgrimes
7892538Simpint	chkuse(daddr_t, int);
791558Srgrimes
8036626Scharnierstatic void
8136626Scharnierusage(void)
8236626Scharnier{
8336626Scharnier	fprintf(stderr, "usage: badsect bbdir blkno ...\n");
8436626Scharnier	exit(1);
8536626Scharnier}
8636626Scharnier
871558Srgrimesint
8892538Simpmain(int argc, char *argv[])
891558Srgrimes{
907589Sbde	daddr_t diskbn;
911558Srgrimes	daddr_t number;
921558Srgrimes	struct stat stbuf, devstat;
9392753Simp	struct dirent *dp;
941558Srgrimes	DIR *dirp;
957589Sbde	char name[2 * MAXPATHLEN];
967589Sbde	char *name_dir_end;
971558Srgrimes
9836626Scharnier	if (argc < 3)
9936626Scharnier		usage();
10036626Scharnier	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0)
10136626Scharnier		err(2, "%s", argv[1]);
1021558Srgrimes	strcpy(name, _PATH_DEV);
10336626Scharnier	if ((dirp = opendir(name)) == NULL)
10436626Scharnier		err(3, "%s", name);
1057589Sbde	name_dir_end = name + strlen(name);
1061558Srgrimes	while ((dp = readdir(dirp)) != NULL) {
1077589Sbde		strcpy(name_dir_end, dp->d_name);
10836626Scharnier		if (lstat(name, &devstat) < 0)
10936626Scharnier			err(4, "%s", name);
1101558Srgrimes		if (stbuf.st_dev == devstat.st_rdev &&
11168521Sadrian		    (devstat.st_mode & IFMT) == IFCHR)
1121558Srgrimes			break;
1131558Srgrimes	}
1141558Srgrimes	closedir(dirp);
1151558Srgrimes	if (dp == NULL) {
1167590Sbde		printf("Cannot find dev 0%lo corresponding to %s\n",
11737233Sbde		    (u_long)stbuf.st_rdev, argv[1]);
1181558Srgrimes		exit(5);
1191558Srgrimes	}
120109600Sjmallett	if (ufs_disk_fillout(&disk, name) == -1) {
121109600Sjmallett		if (disk.d_error != NULL)
122109600Sjmallett			errx(6, "%s: %s", name, disk.d_error);
123109600Sjmallett		else
124109600Sjmallett			err(7, "%s", name);
12598542Smckusick	}
1261558Srgrimes	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
127139648Srwatson		number = strtol(*argv, NULL, 0);
128139648Srwatson		if (errno == EINVAL || errno == ERANGE)
129139648Srwatson			err(8, "%s", *argv);
1301558Srgrimes		if (chkuse(number, 1))
1311558Srgrimes			continue;
1327589Sbde		/*
1337589Sbde		 * Print a warning if converting the block number to a dev_t
1347589Sbde		 * will truncate it.  badsect was not very useful in versions
1357589Sbde		 * of BSD before 4.4 because dev_t was 16 bits and another
1367589Sbde		 * bit was lost by bogus sign extensions.
1377589Sbde		 */
1387589Sbde		diskbn = dbtofsb(fs, number);
1397589Sbde		if ((dev_t)diskbn != diskbn) {
1407590Sbde			printf("sector %ld cannot be represented as a dev_t\n",
14137233Sbde			    (long)number);
1427589Sbde			errs++;
1437589Sbde		}
1447589Sbde		else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) {
14536626Scharnier			warn("%s", *argv);
1461558Srgrimes			errs++;
1471558Srgrimes		}
1481558Srgrimes	}
149109600Sjmallett	ufs_disk_close(&disk);
1501558Srgrimes	printf("Don't forget to run ``fsck %s''\n", name);
1511558Srgrimes	exit(errs);
1521558Srgrimes}
1531558Srgrimes
1541558Srgrimesint
15592538Simpchkuse(daddr_t blkno, int cnt)
1561558Srgrimes{
1571558Srgrimes	int cg;
1581558Srgrimes	daddr_t fsbn, bn;
1591558Srgrimes
1601558Srgrimes	fsbn = dbtofsb(fs, blkno);
1611558Srgrimes	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
162102231Strhodes		printf("block %ld out of range of file system\n", (long)blkno);
1631558Srgrimes		return (1);
1641558Srgrimes	}
1651558Srgrimes	cg = dtog(fs, fsbn);
1661558Srgrimes	if (fsbn < cgdmin(fs, cg)) {
1671558Srgrimes		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
1687590Sbde			printf("block %ld in non-data area: cannot attach\n",
16937233Sbde			    (long)blkno);
1701558Srgrimes			return (1);
1711558Srgrimes		}
1721558Srgrimes	} else {
1731558Srgrimes		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
1747590Sbde			printf("block %ld in non-data area: cannot attach\n",
17537233Sbde			    (long)blkno);
1761558Srgrimes			return (1);
1771558Srgrimes		}
1781558Srgrimes	}
179109600Sjmallett	if (cgread1(&disk, cg) != 1) {
180109600Sjmallett		fprintf(stderr, "cg %d: could not be read\n", cg);
181109600Sjmallett		errs++;
182109600Sjmallett		return (1);
183109600Sjmallett	}
1841558Srgrimes	if (!cg_chkmagic(&acg)) {
1851558Srgrimes		fprintf(stderr, "cg %d: bad magic number\n", cg);
1861558Srgrimes		errs++;
1871558Srgrimes		return (1);
1881558Srgrimes	}
1891558Srgrimes	bn = dtogd(fs, fsbn);
1901558Srgrimes	if (isclr(cg_blksfree(&acg), bn))
19137233Sbde		printf("Warning: sector %ld is in use\n", (long)blkno);
1921558Srgrimes	return (0);
1931558Srgrimes}
194