xref: /dragonfly/sbin/badsect/badsect.c (revision 768af85b)
1 /*
2  * Copyright (c) 1981, 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1981, 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)badsect.c	8.1 (Berkeley) 6/5/93
35  * $FreeBSD: src/sbin/badsect/badsect.c,v 1.7.2.2 2001/07/30 10:30:04 dd Exp $
36  * $DragonFly: src/sbin/badsect/badsect.c,v 1.11 2006/04/03 01:58:46 dillon Exp $
37  */
38 
39 /*
40  * badsect
41  *
42  * Badsect takes a list of file-system relative sector numbers
43  * and makes files containing the blocks of which these sectors are a part.
44  * It can be used to contain sectors which have problems if these sectors
45  * are not part of the bad file for the pack (see bad144).  For instance,
46  * this program can be used if the driver for the file system in question
47  * does not support bad block forwarding.
48  */
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 
52 #include <vfs/ufs/dinode.h>
53 #include <vfs/ufs/fs.h>
54 
55 #include <err.h>
56 #include <errno.h>
57 #include <dirent.h>
58 #include <fcntl.h>
59 #include <paths.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 struct fs	sblock, *fs;
66 struct cg	acg;
67 int		fso, fsi;
68 int		errs;
69 long		dev_bsize = 1;
70 
71 char		buf[MAXBSIZE];
72 
73 static void	rdfs(daddr_t, int, char *);
74 static int	chkuse(daddr_t, int);
75 static void	usage(void);
76 
77 static void
78 usage(void)
79 {
80 	fprintf(stderr, "usage: badsect bbdir blkno ...\n");
81 	exit(1);
82 }
83 
84 int
85 main(int argc, char **argv)
86 {
87 	daddr_t diskbn;
88 	daddr_t number;
89 	dev_t dev;
90 	struct stat stbuf, devstat;
91 	struct dirent *dp;
92 	DIR *dirp;
93 	char name[2 * MAXPATHLEN];
94 
95 	if (argc < 3)
96 		usage();
97 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0)
98 		err(2, "%s", argv[1]);
99 	if (strlcpy(name, _PATH_DEV, sizeof(name)) >= sizeof(name)) {
100 		errno = ENAMETOOLONG;
101 		err(1, "cannot build path name");
102 	}
103 	if ((dirp = opendir(name)) == NULL)
104 		err(3, "%s", name);
105 	while ((dp = readdir(dirp)) != NULL) {
106 		strlcpy(name, _PATH_DEV, sizeof(name));
107 		if (strlcat(name, dp->d_name, sizeof(name)) >= sizeof(name)) {
108 			errno = ENAMETOOLONG;
109 			err(1, "cannot build path name");
110 		}
111 		if (lstat(name, &devstat) < 0)
112 			err(4, "%s", name);
113 		if (stbuf.st_dev == devstat.st_rdev &&
114 		    (devstat.st_mode & IFMT) == IFCHR)
115 			break;
116 	}
117 	closedir(dirp);
118 	if (dp == NULL) {
119 		printf("Cannot find dev 0%lo corresponding to %s\n",
120 		    (u_long)stbuf.st_rdev, argv[1]);
121 		exit(5);
122 	}
123 	if ((fsi = open(name, O_RDONLY)) < 0)
124 		err(6, "%s", name);
125 	fs = &sblock;
126 	rdfs(SBOFF, SBSIZE, (char *)fs);
127 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
128 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
129 		number = atol(*argv);
130 		if (chkuse(number, 1))
131 			continue;
132 		/*
133 		 * Print a warning if converting the block number to a dev_t
134 		 * will truncate it.  badsect was not very useful in versions
135 		 * of BSD before 4.4 because dev_t was 16 bits and another
136 		 * bit was lost by bogus sign extensions.
137 		 */
138 		diskbn = dbtofsb(fs, number);
139 		dev = (dev_t)diskbn;
140 		if ((daddr_t)dev != diskbn) {
141 			printf("sector %ld cannot be represented as a dev_t\n",
142 			    (long)number);
143 			errs++;
144 		} else if (mknod(*argv, IFMT | 0600, dev) < 0) {
145 			warn("%s", *argv);
146 			errs++;
147 		}
148 	}
149 	printf("Don't forget to run ``fsck %s''\n", name);
150 	exit(errs);
151 }
152 
153 static int
154 chkuse(daddr_t blkno, int cnt)
155 {
156 	int cg;
157 	daddr_t fsbn, bn;
158 
159 	fsbn = dbtofsb(fs, blkno);
160 	if (fs->fs_size < 0 || fsbn + cnt < 0) {
161 		printf("internal error: negative fs size or block number\n");
162 		return (1);
163 	}
164 	if (fsbn + cnt > fs->fs_size) {
165 		printf("block %ld out of range of file system\n", (long)blkno);
166 		return (1);
167 	}
168 	cg = dtog(fs, fsbn);
169 	if (fsbn < cgdmin(fs, cg)) {
170 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
171 			printf("block %ld in non-data area: cannot attach\n",
172 			    (long)blkno);
173 			return (1);
174 		}
175 	} else {
176 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
177 			printf("block %ld in non-data area: cannot attach\n",
178 			    (long)blkno);
179 			return (1);
180 		}
181 	}
182 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
183 	    (char *)&acg);
184 	if (!cg_chkmagic(&acg)) {
185 		fprintf(stderr, "cg %d: bad magic number\n", cg);
186 		errs++;
187 		return (1);
188 	}
189 	bn = dtogd(fs, fsbn);
190 	if (isclr(cg_blksfree(&acg), bn))
191 		printf("Warning: sector %ld is in use\n", (long)blkno);
192 	return (0);
193 }
194 
195 /*
196  * read a block from the file system
197  */
198 static void
199 rdfs(daddr_t bno, int size, char *bf)
200 {
201 	int n;
202 
203 	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) {
204 		printf("seek error: %ld\n", (long)bno);
205 		err(1, "rdfs");
206 	}
207 	n = read(fsi, bf, size);
208 	if (n != size) {
209 		printf("read error: %ld\n", (long)bno);
210 		err(1, "rdfs");
211 	}
212 }
213