xref: /openbsd-src/sbin/badsect/badsect.c (revision ea6c4d24ae401ec1965061ca9210f7211471ae5d)
1 /*	$OpenBSD: badsect.c,v 1.29 2022/10/12 23:11:32 krw Exp $	*/
2 /*	$NetBSD: badsect.c,v 1.10 1995/03/18 14:54:28 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1981, 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * badsect
35  *
36  * Badsect takes a list of file-system relative sector numbers
37  * and makes files containing the blocks of which these sectors are a part.
38  * It can be used to contain sectors which have problems. For instance,
39  * this program can be used if the driver for the file system in question
40  * does not support bad block forwarding.
41  */
42 #include <sys/param.h>	/* MAXBSIZE DEV_BSIZE isclr */
43 #include <sys/stat.h>
44 
45 #include <ufs/ffs/fs.h>
46 #include <ufs/ufs/dinode.h>
47 
48 #include <dirent.h>
49 #include <fcntl.h>
50 #include <paths.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <limits.h>
56 #include <err.h>
57 
58 static int chkuse(daddr_t, int);
59 static void rdfs(daddr_t, int, char *);
60 
61 static union {
62 	struct	fs fs;
63 	char	fsx[SBSIZE];
64 } ufs;
65 #define sblock	ufs.fs
66 static union {
67 	struct	cg cg;
68 	char	cgx[MAXBSIZE];
69 } ucg;
70 #define	acg	ucg.cg
71 static struct	fs *fs;
72 static int	fsi;
73 static int	errs;
74 
75 int
main(int argc,char * argv[])76 main(int argc, char *argv[])
77 {
78 	daddr_t number;
79 	struct stat stbuf, devstat;
80 	struct dirent *dp;
81 	DIR *dirp;
82 	char name[BUFSIZ];
83 	int len;
84 
85 	if (argc < 3) {
86 		fprintf(stderr, "usage: badsect bbdir sector ...\n");
87 		exit(1);
88 	}
89 	if (chdir(argv[1]) == -1 || stat(".", &stbuf) == -1)
90 		err(2, "%s", argv[1]);
91 
92 	strlcpy(name, _PATH_DEV, sizeof name);
93 	len = strlen(name);
94 	if ((dirp = opendir(name)) == NULL)
95 		err(3, "%s", name);
96 
97 	while ((dp = readdir(dirp)) != NULL) {
98 		strlcpy(&name[len], dp->d_name, sizeof name - len);
99 		if (stat(name, &devstat) == -1)
100 			err(4, "%s", name);
101 
102 		if (stbuf.st_dev == devstat.st_rdev &&
103 		    S_ISBLK(devstat.st_mode))
104 			break;
105 	}
106 
107 	/*
108 	 * We've found the block device, but since the filesystem
109 	 * is mounted, we must write to the raw (character) device
110 	 * instead. This is not guaranteed to work if someone has a
111 	 * /dev that doesn't follow standard naming conventions, but
112 	 * it's all we've got.
113 	 */
114 	name[len] = 'r';
115 	strlcpy(&name[len+1], dp->d_name, sizeof name - (len+1));
116 	closedir(dirp);
117 	if (dp == NULL)
118 		err(5, "Cannot find dev 0%o corresponding to %s",
119 			stbuf.st_rdev, argv[1]);
120 
121 	if ((fsi = open(name, O_RDONLY)) == -1)
122 		err(6, "%s", name);
123 
124 	fs = &sblock;
125 	rdfs(SBOFF, SBSIZE, (char *)fs);
126 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
127 		number = strtonum(*argv, 0, QUAD_MAX, NULL);
128 		if (chkuse(number, 1))
129 			continue;
130 		if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR,
131 		    dbtofsb(fs, number)) < 0) {
132 			warn("%s", *argv);
133 			errs++;
134 		}
135 	}
136 	printf("Don't forget to run ``fsck %s''\n", name);
137 	exit(errs);
138 }
139 
140 static int
chkuse(daddr_t blkno,int cnt)141 chkuse(daddr_t blkno, int cnt)
142 {
143 	int cg;
144 	daddr_t fsbn, bn;
145 
146 	fsbn = dbtofsb(fs, blkno);
147 	if (fsbn+cnt > fs->fs_ffs1_size) {
148 		fprintf(stderr, "block %lld out of range of file system\n",
149 		    (long long)blkno);
150 		return (1);
151 	}
152 	cg = dtog(fs, fsbn);
153 	if (fsbn < cgdmin(fs, cg)) {
154 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
155 			fprintf(stderr, "block %lld in non-data area: cannot "
156 			    "attach\n", (long long)blkno);
157 			return (1);
158 		}
159 	} else {
160 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
161 			fprintf(stderr, "block %lld in non-data area: cannot "
162 			    "attach\n", (long long)blkno);
163 			return (1);
164 		}
165 	}
166 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
167 	    (char *)&acg);
168 	if (!cg_chkmagic(&acg)) {
169 		fprintf(stderr, "cg %d: bad magic number\n", cg);
170 		errs++;
171 		return (1);
172 	}
173 	bn = dtogd(fs, fsbn);
174 	if (isclr(cg_blksfree(&acg), bn))
175 		fprintf(stderr, "Warning: sector %lld is in use\n",
176 		    (long long)blkno);
177 	return (0);
178 }
179 
180 /*
181  * read a block from the file system
182  */
183 static void
rdfs(daddr_t bno,int size,char * bf)184 rdfs(daddr_t bno, int size, char *bf)
185 {
186 	if (pread(fsi, bf, size, bno * DEV_BSIZE) != size) {
187 		fprintf(stderr, "read error: %lld\n", (long long)bno);
188 		err(1, "rdfs");
189 	}
190 }
191