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