xref: /netbsd-src/sbin/badsect/badsect.c (revision 87ba0e2a319653af510fae24a6d2975d3589735b)
1*87ba0e2aSchs /*	$NetBSD: badsect.c,v 1.35 2022/11/17 06:40:38 chs Exp $	*/
20114e805Scgd 
361f28255Scgd /*
4e681ff5fSpk  * Copyright (c) 1981, 1983, 1993
5e681ff5fSpk  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15bf07c871Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3259ae79ebSchristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
346543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1981, 1983, 1993\
356543a91fSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
390114e805Scgd #if 0
406cdb4c3fSlukem static char sccsid[] = "@(#)badsect.c	8.2 (Berkeley) 5/4/95";
410114e805Scgd #else
42*87ba0e2aSchs __RCSID("$NetBSD: badsect.c,v 1.35 2022/11/17 06:40:38 chs Exp $");
430114e805Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd /*
4761f28255Scgd  * badsect
4861f28255Scgd  *
4961f28255Scgd  * Badsect takes a list of file-system relative sector numbers
5061f28255Scgd  * and makes files containing the blocks of which these sectors are a part.
5161f28255Scgd  * It can be used to contain sectors which have problems if these sectors
5261f28255Scgd  * are not part of the bad file for the pack (see bad144).  For instance,
5361f28255Scgd  * this program can be used if the driver for the file system in question
5461f28255Scgd  * does not support bad block forwarding.
5561f28255Scgd  */
5661f28255Scgd #include <sys/param.h>
57e681ff5fSpk #include <sys/dir.h>
5861f28255Scgd #include <sys/stat.h>
59e681ff5fSpk 
60e681ff5fSpk #include <ufs/ufs/dinode.h>
6129e97528Sbouyer #include <ufs/ufs/ufs_bswap.h>
626cdb4c3fSlukem #include <ufs/ffs/fs.h>
6329e97528Sbouyer #include <ufs/ffs/ffs_extern.h>
64e681ff5fSpk 
65e681ff5fSpk #include <fcntl.h>
6661f28255Scgd #include <paths.h>
67e681ff5fSpk #include <stdio.h>
68e681ff5fSpk #include <stdlib.h>
6965a9ae98Scgd #include <string.h>
70e681ff5fSpk #include <unistd.h>
7159ae79ebSchristos #include <err.h>
7261f28255Scgd 
737abcefadSchristos static union {
7461f28255Scgd 	struct	fs fs;
7542614ed3Sfvdl 	char	fsx[SBLOCKSIZE];
7661f28255Scgd } ufs;
7761f28255Scgd #define sblock	ufs.fs
787abcefadSchristos static union {
7961f28255Scgd 	struct	cg cg;
8061f28255Scgd 	char	cgx[MAXBSIZE];
8161f28255Scgd } ucg;
8261f28255Scgd #define	acg	ucg.cg
837abcefadSchristos static struct	fs *fs;
84fe9cbbdcSchristos static int	fsi;
857abcefadSchristos static int	errs;
867abcefadSchristos static off_t	dev_bsize = 1;
877abcefadSchristos static int needswap = 0;
887abcefadSchristos static int is_ufs2;
8961f28255Scgd 
90fe9cbbdcSchristos static void	rdfs(off_t, size_t, void *);
917abcefadSchristos static int	chkuse(daddr_t, int);
9261f28255Scgd 
937abcefadSchristos static const off_t sblock_try[] = SBLOCKSEARCH;
9442614ed3Sfvdl 
95e681ff5fSpk int
main(int argc,char * argv[])967abcefadSchristos main(int argc, char *argv[])
9761f28255Scgd {
9861f28255Scgd 	daddr_t number;
9961f28255Scgd 	struct stat stbuf, devstat;
100bc8059ebSlukem 	struct direct *dp;
1017abcefadSchristos 	int i, did = 0;
10261f28255Scgd 	DIR *dirp;
10359ae79ebSchristos 	char name[MAXPATHLEN];
1047abcefadSchristos 	size_t dl = sizeof(_PATH_DEV);
10561f28255Scgd 
10661f28255Scgd 	if (argc < 3) {
107b635f565Sjmmv 		(void)fprintf(stderr, "usage: %s bbdir blkno [ blkno ]\n",
1088a986b2eScgd 		    getprogname());
10961f28255Scgd 		exit(1);
11061f28255Scgd 	}
11159ae79ebSchristos 	if (chdir(argv[1]) == -1)
11259ae79ebSchristos 		err(1, "Cannot change directory to `%s'", argv[1]);
113b9656d47Sscottr 
11459ae79ebSchristos 	if (stat(".", &stbuf) == -1)
11559ae79ebSchristos 		err(1, "Cannot stat `%s'", argv[1]);
11659ae79ebSchristos 
11769501841Sitojun 	(void)strlcpy(name, _PATH_DEV, sizeof(name));
11859ae79ebSchristos 	if ((dirp = opendir(name)) == NULL)
11959ae79ebSchristos 		err(1, "Cannot opendir `%s'", argv[1]);
12059ae79ebSchristos 
12161f28255Scgd 	while ((dp = readdir(dirp)) != NULL) {
1227abcefadSchristos 		(void)strlcpy(name + dl - 1, dp->d_name, sizeof(name) - dl + 1);
12359ae79ebSchristos 		if (stat(name, &devstat) == -1)
12459ae79ebSchristos 			err(1, "Cannot stat `%s'", name);
12561f28255Scgd 		if (stbuf.st_dev == devstat.st_rdev &&
1260c1f0c97Smycroft 		    S_ISBLK(devstat.st_mode))
12761f28255Scgd 			break;
12861f28255Scgd 	}
1297abcefadSchristos 
1307abcefadSchristos 	if (dp == NULL)
131ac3b5d9cSchristos 		errx(1, "Cannot find dev 0%llo corresponding to %s",
132ac3b5d9cSchristos 		    (long long)stbuf.st_rdev, argv[1]);
133b9656d47Sscottr 
134b9656d47Sscottr 	/*
135b9656d47Sscottr 	 * The filesystem is mounted; use the character device instead.
136b9656d47Sscottr 	 * XXX - Assume that prepending an `r' will give us the name of
137b9656d47Sscottr 	 * the character device.
138b9656d47Sscottr 	 */
1397abcefadSchristos 	name[dl - 1] = 'r';
1407abcefadSchristos 	(void)strlcpy(name + dl,  dp->d_name, sizeof(name) - dl);
1417abcefadSchristos 	(void)closedir(dirp); /* now *dp is invalid */
1426205a5edSdrochner 
14359ae79ebSchristos 	if ((fsi = open(name, O_RDONLY)) == -1)
14459ae79ebSchristos 		err(1, "Cannot open `%s'", argv[1]);
145b9656d47Sscottr 
14661f28255Scgd 	fs = &sblock;
14742614ed3Sfvdl 
148de51f399Sdsl 	for (i = 0; ; i++) {
149de51f399Sdsl 		if (sblock_try[i] == -1)
150de51f399Sdsl 			errx(1, "%s: bad superblock", name);
1517abcefadSchristos 		rdfs(sblock_try[i], SBLOCKSIZE, fs);
15242614ed3Sfvdl 		switch (fs->fs_magic) {
15342614ed3Sfvdl 		case FS_UFS2_MAGIC:
154*87ba0e2aSchs 		case FS_UFS2EA_MAGIC:
15542614ed3Sfvdl 			is_ufs2 = 1;
15642614ed3Sfvdl 			/* FALLTHROUGH */
15742614ed3Sfvdl 		case FS_UFS1_MAGIC:
158de51f399Sdsl 			break;
15942614ed3Sfvdl 		case FS_UFS2_MAGIC_SWAPPED:
160*87ba0e2aSchs 		case FS_UFS2EA_MAGIC_SWAPPED:
16142614ed3Sfvdl 			is_ufs2 = 1;
16242614ed3Sfvdl 			/* FALLTHROUGH */
16342614ed3Sfvdl 		case FS_UFS1_MAGIC_SWAPPED:
16429e97528Sbouyer 			needswap = 1;
165de51f399Sdsl 			ffs_sb_swap(fs, fs);
166de51f399Sdsl 			break;
16742614ed3Sfvdl 		default:
16842614ed3Sfvdl 			continue;
169029a64ccSross 		}
170de51f399Sdsl 
171de51f399Sdsl 		/* Ensure we don't use 1st alternate if ffsv1 and bs=64k */
17233599967Sdsl 		if (is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED) {
173de51f399Sdsl 			if (fs->fs_sblockloc != sblock_try[i])
174de51f399Sdsl 				continue;
175de51f399Sdsl 		} else {
176de51f399Sdsl 			if (sblock_try[i] == SBLOCK_UFS2)
177de51f399Sdsl 				continue;
17842614ed3Sfvdl 		}
179de51f399Sdsl 		break;
180de51f399Sdsl 	}
18142614ed3Sfvdl 
1822737439dSdholland 	dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1);
18361f28255Scgd 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
18461f28255Scgd 		number = atoi(*argv);
18561f28255Scgd 		if (chkuse(number, 1))
18661f28255Scgd 			continue;
1870c1f0c97Smycroft 		if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR,
1882737439dSdholland 		    (dev_t)FFS_DBTOFSB(fs, number)) == -1) {
18959ae79ebSchristos 			warn("Cannot mknod `%s'", *argv);
19061f28255Scgd 			errs++;
1917abcefadSchristos 			continue;
19261f28255Scgd 		}
1937abcefadSchristos 		did++;
19461f28255Scgd 	}
195b9656d47Sscottr 
1967abcefadSchristos 	if (did)
1977abcefadSchristos 		warnx("Don't forget to run `fsck %s'", name);
1987abcefadSchristos 	else
1997abcefadSchristos 		warnx("File system `%s' was not modified", name);
20059ae79ebSchristos 	return errs;
20161f28255Scgd }
20261f28255Scgd 
2037abcefadSchristos static int
chkuse(off_t blkno,int cnt)2047abcefadSchristos chkuse(off_t blkno, int cnt)
20561f28255Scgd {
20661f28255Scgd 	int cg;
2077abcefadSchristos 	off_t fsbn, bn, fsbe;
20861f28255Scgd 
2092737439dSdholland 	fsbn = FFS_DBTOFSB(fs, blkno);
2107abcefadSchristos 	fsbe = fsbn + cnt;
2117abcefadSchristos 	if (fsbe > fs->fs_size) {
212a3ff3a30Sfvdl 		warnx("block %lld out of range of file system",
213a3ff3a30Sfvdl 		    (long long)blkno);
2147abcefadSchristos 		return 1;
21561f28255Scgd 	}
216b9656d47Sscottr 
217fe9cbbdcSchristos 	cg = (int)dtog(fs, fsbn);
21861f28255Scgd 	if (fsbn < cgdmin(fs, cg)) {
2197abcefadSchristos 		if (cg == 0 || fsbe > cgsblock(fs, cg)) {
2207abcefadSchristos 			warnx("block %lld in superblock area: cannot attach",
221a3ff3a30Sfvdl 			    (long long)blkno);
2227abcefadSchristos 			return 1;
22361f28255Scgd 		}
22461f28255Scgd 	} else {
2257abcefadSchristos 		if (fsbe > cgbase(fs, cg + 1)) {
2267abcefadSchristos 			warnx("block %lld in beyond end of cylinder group: "
2277abcefadSchristos 			    "cannot attach", (long long)blkno);
2287abcefadSchristos 			return 1;
22961f28255Scgd 		}
23061f28255Scgd 	}
231b9656d47Sscottr 
2322737439dSdholland 	rdfs(FFS_FSBTODB(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize, &acg);
233b9656d47Sscottr 
23429e97528Sbouyer 	if (!cg_chkmagic(&acg, needswap)) {
23559ae79ebSchristos 		warnx("cg %d: bad magic number", cg);
23661f28255Scgd 		errs++;
2377abcefadSchristos 		return 1;
23861f28255Scgd 	}
239b9656d47Sscottr 
24061f28255Scgd 	bn = dtogd(fs, fsbn);
24129e97528Sbouyer 	if (isclr(cg_blksfree(&acg, needswap), bn))
242a3ff3a30Sfvdl 		warnx("Warning: sector %lld is in use", (long long)blkno);
243b9656d47Sscottr 
2447abcefadSchristos 	return 0;
24561f28255Scgd }
24661f28255Scgd 
24761f28255Scgd /*
24861f28255Scgd  * read a block from the file system
24961f28255Scgd  */
2507abcefadSchristos static void
rdfs(off_t bno,size_t size,void * bf)251fe9cbbdcSchristos rdfs(off_t bno, size_t size, void *bf)
25261f28255Scgd {
253c1237301Slukem 	ssize_t n;
25461f28255Scgd 
2557abcefadSchristos 	if (lseek(fsi, bno * dev_bsize, SEEK_SET) == -1)
256a3ff3a30Sfvdl 		err(1, "seek error at block %lld", (long long)bno);
257b9656d47Sscottr 
25859ae79ebSchristos 	switch (n = read(fsi, bf, size)) {
25959ae79ebSchristos 	case -1:
260a3ff3a30Sfvdl 		err(1, "read error at block %lld", (long long)bno);
26159ae79ebSchristos 		break;
26259ae79ebSchristos 
26359ae79ebSchristos 	default:
264c1237301Slukem 		if ((size_t)n == size)
26559ae79ebSchristos 			return;
266a3ff3a30Sfvdl 		errx(1, "incomplete read at block %lld", (long long)bno);
26761f28255Scgd 	}
26861f28255Scgd }
269