badsect.c revision 1.3
1/*
2 * Copyright (c) 1981, 1983 The Regents of the University of California.
3 * 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
34#ifndef lint
35char copyright[] =
36"@(#) Copyright (c) 1981, 1983 The Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)badsect.c	5.9 (Berkeley) 6/1/90";
42static char rcsid[] = "$Header: /usr/local/opengrok/home/cvs-mirror/mirrors/netbsd/src/sbin/badsect/badsect.c,v 1.3 1993/03/23 00:26:28 cgd Exp $";
43#endif /* not lint */
44
45/*
46 * badsect
47 *
48 * Badsect takes a list of file-system relative sector numbers
49 * and makes files containing the blocks of which these sectors are a part.
50 * It can be used to contain sectors which have problems if these sectors
51 * are not part of the bad file for the pack (see bad144).  For instance,
52 * this program can be used if the driver for the file system in question
53 * does not support bad block forwarding.
54 */
55#include <sys/param.h>
56#include <sys/dir.h>
57#include <sys/stat.h>
58#include <ufs/fs.h>
59#include <ufs/dinode.h>
60#include <stdio.h>
61#include <paths.h>
62
63union {
64	struct	fs fs;
65	char	fsx[SBSIZE];
66} ufs;
67#define sblock	ufs.fs
68union {
69	struct	cg cg;
70	char	cgx[MAXBSIZE];
71} ucg;
72#define	acg	ucg.cg
73struct	fs *fs;
74int	fso, fsi;
75int	errs;
76long	dev_bsize = 1;
77
78char buf[MAXBSIZE];
79
80
81main(argc, argv)
82	int argc;
83	char *argv[];
84{
85	daddr_t number;
86	struct stat stbuf, devstat;
87	register struct direct *dp;
88	DIR *dirp;
89	int fd;
90	char name[BUFSIZ];
91
92	if (argc < 3) {
93		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
94		exit(1);
95	}
96	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
97		perror(argv[1]);
98		exit(2);
99	}
100	strcpy(name, _PATH_DEV);
101	if ((dirp = opendir(name)) == NULL) {
102		perror(name);
103		exit(3);
104	}
105	while ((dp = readdir(dirp)) != NULL) {
106		strcpy(&name[5], dp->d_name);
107		if (stat(name, &devstat) < 0) {
108			perror(name);
109			exit(4);
110		}
111		if (stbuf.st_dev == devstat.st_rdev &&
112		    (devstat.st_mode & IFMT) == IFBLK)
113			break;
114	}
115	closedir(dirp);
116	if (dp == NULL) {
117		printf("Cannot find dev 0%o corresponding to %s\n",
118			stbuf.st_rdev, argv[1]);
119		exit(5);
120	}
121	if ((fsi = open(name, 0)) < 0) {
122		perror(name);
123		exit(6);
124	}
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 = atoi(*argv);
130		if (chkuse(number, 1))
131			continue;
132		if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
133			perror(*argv);
134			errs++;
135		}
136	}
137	printf("Don't forget to run ``fsck %s''\n", name);
138	exit(errs);
139}
140
141chkuse(blkno, cnt)
142	daddr_t blkno;
143	int cnt;
144{
145	int cg;
146	daddr_t fsbn, bn;
147
148	fsbn = dbtofsb(fs, blkno);
149	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
150		printf("block %d out of range of file system\n", blkno);
151		return (1);
152	}
153	cg = dtog(fs, fsbn);
154	if (fsbn < cgdmin(fs, cg)) {
155		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
156			printf("block %d in non-data area: cannot attach\n",
157				blkno);
158			return (1);
159		}
160	} else {
161		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
162			printf("block %d in non-data area: cannot attach\n",
163				blkno);
164			return (1);
165		}
166	}
167	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
168	    (char *)&acg);
169	if (!cg_chkmagic(&acg)) {
170		fprintf(stderr, "cg %d: bad magic number\n", cg);
171		errs++;
172		return (1);
173	}
174	bn = dtogd(fs, fsbn);
175	if (isclr(cg_blksfree(&acg), bn))
176		printf("Warning: sector %d is in use\n", blkno);
177	return (0);
178}
179
180/*
181 * read a block from the file system
182 */
183rdfs(bno, size, bf)
184	int bno, size;
185	char *bf;
186{
187	int n;
188
189	if (lseek(fsi, bno * dev_bsize, 0) < 0) {
190		printf("seek error: %ld\n", bno);
191		perror("rdfs");
192		exit(1);
193	}
194	n = read(fsi, bf, size);
195	if(n != size) {
196		printf("read error: %ld\n", bno);
197		perror("rdfs");
198		exit(1);
199	}
200}
201