xref: /netbsd-src/sbin/badsect/badsect.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: badsect.c,v 1.22 2003/04/02 10:39:22 fvdl Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1981, 1983, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)badsect.c	8.2 (Berkeley) 5/4/95";
45 #else
46 __RCSID("$NetBSD: badsect.c,v 1.22 2003/04/02 10:39:22 fvdl Exp $");
47 #endif
48 #endif /* not lint */
49 
50 /*
51  * badsect
52  *
53  * Badsect takes a list of file-system relative sector numbers
54  * and makes files containing the blocks of which these sectors are a part.
55  * It can be used to contain sectors which have problems if these sectors
56  * are not part of the bad file for the pack (see bad144).  For instance,
57  * this program can be used if the driver for the file system in question
58  * does not support bad block forwarding.
59  */
60 #include <sys/param.h>
61 #include <sys/dir.h>
62 #include <sys/stat.h>
63 
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ufs/ufs_bswap.h>
66 #include <ufs/ffs/fs.h>
67 #include <ufs/ffs/ffs_extern.h>
68 
69 #include <fcntl.h>
70 #include <paths.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <err.h>
76 
77 union {
78 	struct	fs fs;
79 	char	fsx[SBLOCKSIZE];
80 } ufs;
81 #define sblock	ufs.fs
82 union {
83 	struct	cg cg;
84 	char	cgx[MAXBSIZE];
85 } ucg;
86 #define	acg	ucg.cg
87 struct	fs *fs;
88 int	fso, fsi;
89 int	errs;
90 long	dev_bsize = 1;
91 int needswap = 0;
92 int is_ufs2;
93 
94 char buf[MAXBSIZE];
95 
96 void	rdfs __P((daddr_t, int, char *));
97 int	chkuse __P((daddr_t, int));
98 int	main __P((int, char *[]));
99 
100 const off_t sblock_try[] = SBLOCKSEARCH;
101 
102 int
103 main(argc, argv)
104 	int argc;
105 	char *argv[];
106 {
107 	daddr_t number;
108 	struct stat stbuf, devstat;
109 	struct direct *dp;
110 	int i;
111 	DIR *dirp;
112 	char name[MAXPATHLEN];
113 
114 	if (argc < 3) {
115 		(void) fprintf(stderr, "Usage: %s bbdir blkno [ blkno ]\n",
116 		    getprogname());
117 		exit(1);
118 	}
119 	if (chdir(argv[1]) == -1)
120 		err(1, "Cannot change directory to `%s'", argv[1]);
121 
122 	if (stat(".", &stbuf) == -1)
123 		err(1, "Cannot stat `%s'", argv[1]);
124 
125 	(void) strcpy(name, _PATH_DEV);
126 	if ((dirp = opendir(name)) == NULL)
127 		err(1, "Cannot opendir `%s'", argv[1]);
128 
129 	while ((dp = readdir(dirp)) != NULL) {
130 		(void) snprintf(name, sizeof(name), "%s%s", _PATH_DEV,
131 		    dp->d_name);
132 		if (stat(name, &devstat) == -1)
133 			err(1, "Cannot stat `%s'", name);
134 		if (stbuf.st_dev == devstat.st_rdev &&
135 		    S_ISBLK(devstat.st_mode))
136 			break;
137 	}
138 	if (dp == NULL) {
139 		closedir(dirp);
140 		errx(1, "Cannot find dev 0%o corresponding to %s",
141 		    stbuf.st_rdev, argv[1]);
142 	}
143 
144 	/*
145 	 * The filesystem is mounted; use the character device instead.
146 	 * XXX - Assume that prepending an `r' will give us the name of
147 	 * the character device.
148 	 */
149 	(void) snprintf(name, sizeof(name), "%sr%s", _PATH_DEV, dp->d_name);
150 
151 	closedir(dirp); /* now *dp is invalid */
152 
153 	if ((fsi = open(name, O_RDONLY)) == -1)
154 		err(1, "Cannot open `%s'", argv[1]);
155 
156 	fs = &sblock;
157 
158 	for (i = 0; sblock_try[i] != -1; i++) {
159 		rdfs(sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, (char *)fs);
160 		switch (fs->fs_magic) {
161 		case FS_UFS2_MAGIC:
162 			is_ufs2 = 1;
163 			/* FALLTHROUGH */
164 		case FS_UFS1_MAGIC:
165 			goto found;
166 		case FS_UFS2_MAGIC_SWAPPED:
167 			is_ufs2 = 1;
168 			/* FALLTHROUGH */
169 		case FS_UFS1_MAGIC_SWAPPED:
170 			needswap = 1;
171 			goto found;
172 		default:
173 			continue;
174 		}
175 	}
176 	errx(1, "%s: bad superblock", name);
177 found:
178 	if (needswap)
179 		ffs_sb_swap(fs, fs);
180 
181 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
182 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
183 		number = atoi(*argv);
184 		if (chkuse(number, 1))
185 			continue;
186 		if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR,
187 		    dbtofsb(fs, number)) == -1) {
188 			warn("Cannot mknod `%s'", *argv);
189 			errs++;
190 		}
191 	}
192 
193 	warnx("Don't forget to run ``fsck %s''", name);
194 	return errs;
195 }
196 
197 int
198 chkuse(blkno, cnt)
199 	daddr_t blkno;
200 	int cnt;
201 {
202 	int cg;
203 	daddr_t fsbn, bn;
204 
205 	fsbn = dbtofsb(fs, blkno);
206 	if (fsbn+cnt > fs->fs_size) {
207 		warnx("block %lld out of range of file system",
208 		    (long long)blkno);
209 		return (1);
210 	}
211 
212 	cg = dtog(fs, fsbn);
213 	if (fsbn < cgdmin(fs, cg)) {
214 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
215 			warnx("block %lld in non-data area: cannot attach",
216 			    (long long)blkno);
217 			return (1);
218 		}
219 	} else {
220 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
221 			warnx("block %lld in non-data area: cannot attach",
222 			    (long long)blkno);
223 			return (1);
224 		}
225 	}
226 
227 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
228 	    (char *)&acg);
229 
230 	if (!cg_chkmagic(&acg, needswap)) {
231 		warnx("cg %d: bad magic number", cg);
232 		errs++;
233 		return (1);
234 	}
235 
236 	bn = dtogd(fs, fsbn);
237 	if (isclr(cg_blksfree(&acg, needswap), bn))
238 		warnx("Warning: sector %lld is in use", (long long)blkno);
239 
240 	return (0);
241 }
242 
243 /*
244  * read a block from the file system
245  */
246 void
247 rdfs(bno, size, bf)
248 	daddr_t bno;
249 	int size;
250 	char *bf;
251 {
252 	int n;
253 
254 	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) == -1)
255 		err(1, "seek error at block %lld", (long long)bno);
256 
257 	switch (n = read(fsi, bf, size)) {
258 	case -1:
259 		err(1, "read error at block %lld", (long long)bno);
260 		break;
261 
262 	default:
263 		if (n == size)
264 			return;
265 		errx(1, "incomplete read at block %lld", (long long)bno);
266 	}
267 }
268