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