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