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