2 * Copyright (c) 1981, 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1981, 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static const char sccsid[] = "@(#)badsect.c 8.1 (Berkeley) 6/5/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
47 * Badsect takes a list of file-system relative sector numbers
48 * and makes files containing the blocks of which these sectors are a part.
49 * It can be used to contain sectors which have problems if these sectors
50 * are not part of the bad file for the pack (see bad144). For instance,
51 * this program can be used if the driver for the file system in question
52 * does not support bad block forwarding.
54 #include <sys/param.h>
56 #include <sys/disklabel.h>
58 #include <ufs/ufs/dinode.h>
59 #include <ufs/ffs/fs.h>
72 #define sblock disk.d_fs
74 static struct uufsd disk;
75 static struct fs *fs = &sblock;
78 int chkuse(daddr_t, int);
83 fprintf(stderr, "usage: badsect bbdir blkno ...\n");
88 main(int argc, char *argv[])
92 struct stat stbuf, devstat;
95 char name[2 * MAXPATHLEN];
100 if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0)
101 err(2, "%s", argv[1]);
102 strcpy(name, _PATH_DEV);
103 if ((dirp = opendir(name)) == NULL)
105 name_dir_end = name + strlen(name);
106 while ((dp = readdir(dirp)) != NULL) {
107 strcpy(name_dir_end, dp->d_name);
108 if (lstat(name, &devstat) < 0)
110 if (stbuf.st_dev == devstat.st_rdev &&
111 (devstat.st_mode & IFMT) == IFCHR)
116 printf("Cannot find dev 0%lo corresponding to %s\n",
117 (u_long)stbuf.st_rdev, argv[1]);
120 if (ufs_disk_fillout(&disk, name) == -1) {
121 if (disk.d_error != NULL)
122 errx(6, "%s: %s", name, disk.d_error);
126 for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
127 number = strtol(*argv, NULL, 0);
128 if (errno == EINVAL || errno == ERANGE)
130 if (chkuse(number, 1))
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.
138 diskbn = dbtofsb(fs, number);
139 if ((dev_t)diskbn != diskbn) {
140 printf("sector %ld cannot be represented as a dev_t\n",
144 else if (mknod(*argv, IFMT|0600, (dev_t)diskbn) < 0) {
149 ufs_disk_close(&disk);
150 printf("Don't forget to run ``fsck %s''\n", name);
155 chkuse(daddr_t blkno, int cnt)
160 fsbn = dbtofsb(fs, blkno);
161 if ((unsigned)(fsbn+cnt) > fs->fs_size) {
162 printf("block %ld out of range of file system\n", (long)blkno);
166 if (fsbn < cgdmin(fs, cg)) {
167 if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
168 printf("block %ld in non-data area: cannot attach\n",
173 if ((fsbn+cnt) > cgbase(fs, cg+1)) {
174 printf("block %ld in non-data area: cannot attach\n",
179 if (cgread1(&disk, cg) != 1) {
180 fprintf(stderr, "cg %d: could not be read\n", cg);
184 if (!cg_chkmagic(&acg)) {
185 fprintf(stderr, "cg %d: bad magic number\n", cg);
189 bn = dtogd(fs, fsbn);
190 if (isclr(cg_blksfree(&acg), bn))
191 printf("Warning: sector %ld is in use\n", (long)blkno);