xref: /openbsd/sbin/badsect/badsect.c (revision db3296cf)
1 /*	$OpenBSD: badsect.c,v 1.12 2003/06/02 20:06:14 millert 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 #ifndef lint
34 static char copyright[] =
35 "@(#) Copyright (c) 1981, 1983, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)badsect.c	8.1 (Berkeley) 6/5/93";
42 #else
43 static char rcsid[] = "$OpenBSD: badsect.c,v 1.12 2003/06/02 20:06:14 millert Exp $";
44 #endif
45 #endif /* not lint */
46 
47 /*
48  * badsect
49  *
50  * Badsect takes a list of file-system relative sector numbers
51  * and makes files containing the blocks of which these sectors are a part.
52  * It can be used to contain sectors which have problems if these sectors
53  * are not part of the bad file for the pack (see bad144).  For instance,
54  * this program can be used if the driver for the file system in question
55  * does not support bad block forwarding.
56  */
57 #include <sys/param.h>
58 #include <sys/dir.h>
59 #include <sys/stat.h>
60 
61 #include <ufs/ffs/fs.h>
62 #include <ufs/ufs/dinode.h>
63 
64 #include <fcntl.h>
65 #include <paths.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 
71 union {
72 	struct	fs fs;
73 	char	fsx[SBSIZE];
74 } ufs;
75 #define sblock	ufs.fs
76 union {
77 	struct	cg cg;
78 	char	cgx[MAXBSIZE];
79 } ucg;
80 #define	acg	ucg.cg
81 struct	fs *fs;
82 int	fso, fsi;
83 int	errs;
84 long	dev_bsize = 1;
85 
86 char buf[MAXBSIZE];
87 
88 void	rdfs(daddr_t, int, char *);
89 int	chkuse(daddr_t, int);
90 
91 int
92 main(int argc, char *argv[])
93 {
94 	daddr_t number;
95 	struct stat stbuf, devstat;
96 	struct direct *dp;
97 	DIR *dirp;
98 	char name[BUFSIZ];
99 	int len;
100 
101 	if (argc < 3) {
102 		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
103 		exit(1);
104 	}
105 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
106 		perror(argv[1]);
107 		exit(2);
108 	}
109 	strlcpy(name, _PATH_DEV, sizeof name);
110 	len = strlen(name);
111 	if ((dirp = opendir(name)) == NULL) {
112 		perror(name);
113 		exit(3);
114 	}
115 	while ((dp = readdir(dirp)) != NULL) {
116 		strlcpy(&name[len], dp->d_name, sizeof name - len);
117 		if (stat(name, &devstat) < 0) {
118 			perror(name);
119 			exit(4);
120 		}
121 		if (stbuf.st_dev == devstat.st_rdev &&
122 		    S_ISBLK(devstat.st_mode))
123 			break;
124 	}
125 
126 	/*
127 	 * We've found the block device, but since the filesystem
128 	 * is mounted, we must write to the raw (character) device
129 	 * instead. This is not guaranteed to work if someone has a
130 	 * /dev that doesn't follow standard naming conventions, but
131 	 * it's all we've got.
132 	 */
133 	name[len] = 'r';
134 	strlcpy(&name[len+1], dp->d_name, sizeof name - (len+1));
135 	closedir(dirp);
136 	if (dp == NULL) {
137 		printf("Cannot find dev 0%o corresponding to %s\n",
138 			stbuf.st_rdev, argv[1]);
139 		exit(5);
140 	}
141 	if ((fsi = open(name, 0)) < 0) {
142 		perror(name);
143 		exit(6);
144 	}
145 	fs = &sblock;
146 	rdfs(SBOFF, SBSIZE, (char *)fs);
147 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
148 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
149 		number = atoi(*argv);
150 		if (chkuse(number, 1))
151 			continue;
152 		if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR,
153 		    dbtofsb(fs, number)) < 0) {
154 			perror(*argv);
155 			errs++;
156 		}
157 	}
158 	printf("Don't forget to run ``fsck %s''\n", name);
159 	exit(errs);
160 }
161 
162 int
163 chkuse(daddr_t blkno, int cnt)
164 {
165 	int cg;
166 	daddr_t fsbn, bn;
167 
168 	fsbn = dbtofsb(fs, blkno);
169 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
170 		printf("block %d out of range of file system\n", blkno);
171 		return (1);
172 	}
173 	cg = dtog(fs, fsbn);
174 	if (fsbn < cgdmin(fs, cg)) {
175 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
176 			printf("block %d in non-data area: cannot attach\n",
177 				blkno);
178 			return (1);
179 		}
180 	} else {
181 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
182 			printf("block %d in non-data area: cannot attach\n",
183 				blkno);
184 			return (1);
185 		}
186 	}
187 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
188 	    (char *)&acg);
189 	if (!cg_chkmagic(&acg)) {
190 		fprintf(stderr, "cg %d: bad magic number\n", cg);
191 		errs++;
192 		return (1);
193 	}
194 	bn = dtogd(fs, fsbn);
195 	if (isclr(cg_blksfree(&acg), bn))
196 		printf("Warning: sector %d is in use\n", blkno);
197 	return (0);
198 }
199 
200 /*
201  * read a block from the file system
202  */
203 void
204 rdfs(daddr_t bno, int size, char *bf)
205 {
206 	int n;
207 
208 	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) {
209 		printf("seek error: %lld\n", (long long)bno);
210 		perror("rdfs");
211 		exit(1);
212 	}
213 	n = read(fsi, bf, size);
214 	if (n != size) {
215 		printf("read error: %lld\n", (long long)bno);
216 		perror("rdfs");
217 		exit(1);
218 	}
219 }
220