1 /* $OpenBSD: badsect.c,v 1.15 2005/07/21 16:38:54 fgsch 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 const 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 const char rcsid[] = "$OpenBSD: badsect.c,v 1.15 2005/07/21 16:38:54 fgsch 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 #include <err.h>
71
72 static int chkuse(daddr_t, int);
73 static void rdfs(daddr_t, int, char *);
74
75 static union {
76 struct fs fs;
77 char fsx[SBSIZE];
78 } ufs;
79 #define sblock ufs.fs
80 static union {
81 struct cg cg;
82 char cgx[MAXBSIZE];
83 } ucg;
84 #define acg ucg.cg
85 static struct fs *fs;
86 static int fsi;
87 static int errs;
88 static long dev_bsize = 1;
89
90 int
main(int argc,char * argv[])91 main(int argc, char *argv[])
92 {
93 daddr_t number;
94 struct stat stbuf, devstat;
95 struct direct *dp;
96 DIR *dirp;
97 char name[BUFSIZ];
98 int len;
99
100 if (argc < 3) {
101 fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
102 exit(1);
103 }
104 if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0)
105 err(2, "%s", argv[1]);
106
107 strlcpy(name, _PATH_DEV, sizeof name);
108 len = strlen(name);
109 if ((dirp = opendir(name)) == NULL)
110 err(3, "%s", name);
111
112 while ((dp = readdir(dirp)) != NULL) {
113 strlcpy(&name[len], dp->d_name, sizeof name - len);
114 if (stat(name, &devstat) < 0)
115 err(4, "%s", name);
116
117 if (stbuf.st_dev == devstat.st_rdev &&
118 S_ISBLK(devstat.st_mode))
119 break;
120 }
121
122 /*
123 * We've found the block device, but since the filesystem
124 * is mounted, we must write to the raw (character) device
125 * instead. This is not guaranteed to work if someone has a
126 * /dev that doesn't follow standard naming conventions, but
127 * it's all we've got.
128 */
129 name[len] = 'r';
130 strlcpy(&name[len+1], dp->d_name, sizeof name - (len+1));
131 closedir(dirp);
132 if (dp == NULL)
133 err(5, "Cannot find dev 0%o corresponding to %s",
134 stbuf.st_rdev, argv[1]);
135
136 if ((fsi = open(name, 0)) < 0)
137 err(6, "%s", name);
138
139 fs = &sblock;
140 rdfs(SBOFF, SBSIZE, (char *)fs);
141 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
142 for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
143 number = atoi(*argv);
144 if (chkuse(number, 1))
145 continue;
146 if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR,
147 dbtofsb(fs, number)) < 0) {
148 warn("%s", *argv);
149 errs++;
150 }
151 }
152 printf("Don't forget to run ``fsck %s''\n", name);
153 exit(errs);
154 }
155
156 static int
chkuse(daddr_t blkno,int cnt)157 chkuse(daddr_t blkno, int cnt)
158 {
159 int cg;
160 daddr_t fsbn, bn;
161
162 fsbn = dbtofsb(fs, blkno);
163 if ((unsigned)(fsbn+cnt) > fs->fs_size) {
164 fprintf(stderr, "block %d out of range of file system\n", blkno);
165 return (1);
166 }
167 cg = dtog(fs, fsbn);
168 if (fsbn < cgdmin(fs, cg)) {
169 if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
170 fprintf(stderr, "block %d in non-data area: cannot attach\n",
171 blkno);
172 return (1);
173 }
174 } else {
175 if ((fsbn+cnt) > cgbase(fs, cg+1)) {
176 fprintf(stderr, "block %d in non-data area: cannot attach\n",
177 blkno);
178 return (1);
179 }
180 }
181 rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
182 (char *)&acg);
183 if (!cg_chkmagic(&acg)) {
184 fprintf(stderr, "cg %d: bad magic number\n", cg);
185 errs++;
186 return (1);
187 }
188 bn = dtogd(fs, fsbn);
189 if (isclr(cg_blksfree(&acg), bn))
190 fprintf(stderr, "Warning: sector %d is in use\n", blkno);
191 return (0);
192 }
193
194 /*
195 * read a block from the file system
196 */
197 static void
rdfs(daddr_t bno,int size,char * bf)198 rdfs(daddr_t bno, int size, char *bf)
199 {
200 int n;
201
202 if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) {
203 fprintf(stderr, "seek error: %lld\n", (long long)bno);
204 err(1, "rdfs");
205 }
206 n = read(fsi, bf, size);
207 if (n != size) {
208 fprintf(stderr, "read error: %lld\n", (long long)bno);
209 err(1, "rdfs");
210 }
211 }
212