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