Home | History | Annotate | Line # | Download | only in badsect
badsect.c revision 1.17
      1 /*	$NetBSD: badsect.c,v 1.17 1999/07/30 15:56:58 drochner 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.17 1999/07/30 15:56:58 drochner 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/ufs/ufs_bswap.h>
     66 #include <ufs/ffs/fs.h>
     67 #include <ufs/ffs/ffs_extern.h>
     68 
     69 #include <fcntl.h>
     70 #include <paths.h>
     71 #include <stdio.h>
     72 #include <stdlib.h>
     73 #include <string.h>
     74 #include <unistd.h>
     75 #include <err.h>
     76 
     77 union {
     78 	struct	fs fs;
     79 	char	fsx[SBSIZE];
     80 } ufs;
     81 #define sblock	ufs.fs
     82 union {
     83 	struct	cg cg;
     84 	char	cgx[MAXBSIZE];
     85 } ucg;
     86 #define	acg	ucg.cg
     87 struct	fs *fs;
     88 int	fso, fsi;
     89 int	errs;
     90 long	dev_bsize = 1;
     91 int needswap = 0;
     92 
     93 char buf[MAXBSIZE];
     94 
     95 void	rdfs __P((daddr_t, int, char *));
     96 int	chkuse __P((daddr_t, int));
     97 int	main __P((int, char *[]));
     98 
     99 int
    100 main(argc, argv)
    101 	int argc;
    102 	char *argv[];
    103 {
    104 	daddr_t number;
    105 	struct stat stbuf, devstat;
    106 	struct direct *dp;
    107 	DIR *dirp;
    108 	char name[MAXPATHLEN];
    109 	extern char *__progname;
    110 
    111 	if (argc < 3) {
    112 		(void) fprintf(stderr, "Usage: %s bbdir blkno [ blkno ]\n",
    113 		    __progname);
    114 		exit(1);
    115 	}
    116 	if (chdir(argv[1]) == -1)
    117 		err(1, "Cannot change directory to `%s'", argv[1]);
    118 
    119 	if (stat(".", &stbuf) == -1)
    120 		err(1, "Cannot stat `%s'", argv[1]);
    121 
    122 	(void) strcpy(name, _PATH_DEV);
    123 	if ((dirp = opendir(name)) == NULL)
    124 		err(1, "Cannot opendir `%s'", argv[1]);
    125 
    126 	while ((dp = readdir(dirp)) != NULL) {
    127 		(void) snprintf(name, sizeof(name), "%s%s", _PATH_DEV,
    128 		    dp->d_name);
    129 		if (stat(name, &devstat) == -1)
    130 			err(1, "Cannot stat `%s'", name);
    131 		if (stbuf.st_dev == devstat.st_rdev &&
    132 		    S_ISBLK(devstat.st_mode))
    133 			break;
    134 	}
    135 	if (dp == NULL) {
    136 		closedir(dirp);
    137 		errx(1, "Cannot find dev 0%o corresponding to %s",
    138 		    stbuf.st_rdev, argv[1]);
    139 	}
    140 
    141 	/*
    142 	 * The filesystem is mounted; use the character device instead.
    143 	 * XXX - Assume that prepending an `r' will give us the name of
    144 	 * the character device.
    145 	 */
    146 	(void) snprintf(name, sizeof(name), "%sr%s", _PATH_DEV, dp->d_name);
    147 
    148 	closedir(dirp); /* now *dp is invalid */
    149 
    150 	if ((fsi = open(name, O_RDONLY)) == -1)
    151 		err(1, "Cannot open `%s'", argv[1]);
    152 
    153 	fs = &sblock;
    154 	rdfs(SBOFF, SBSIZE, (char *)fs);
    155 	if (fs->fs_magic != FS_MAGIC) {
    156 		if(fs->fs_magic == bswap32(FS_MAGIC))
    157 			needswap = 1;
    158 		else
    159 			errx(1, "%s: bad superblock", name);
    160 	}
    161 	if (needswap)
    162 		ffs_sb_swap(fs, fs, 0);
    163 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    164 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
    165 		number = atoi(*argv);
    166 		if (chkuse(number, 1))
    167 			continue;
    168 		if (mknod(*argv, S_IFMT|S_IRUSR|S_IWUSR,
    169 		    dbtofsb(fs, number)) == -1) {
    170 			warn("Cannot mknod `%s'", *argv);
    171 			errs++;
    172 		}
    173 	}
    174 
    175 	warnx("Don't forget to run ``fsck %s''", name);
    176 	return errs;
    177 }
    178 
    179 int
    180 chkuse(blkno, cnt)
    181 	daddr_t blkno;
    182 	int cnt;
    183 {
    184 	int cg;
    185 	daddr_t fsbn, bn;
    186 
    187 	fsbn = dbtofsb(fs, blkno);
    188 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
    189 		warnx("block %d out of range of file system", blkno);
    190 		return (1);
    191 	}
    192 
    193 	cg = dtog(fs, fsbn);
    194 	if (fsbn < cgdmin(fs, cg)) {
    195 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
    196 			warnx("block %d in non-data area: cannot attach",
    197 			    blkno);
    198 			return (1);
    199 		}
    200 	} else {
    201 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
    202 			warnx("block %d in non-data area: cannot attach",
    203 			    blkno);
    204 			return (1);
    205 		}
    206 	}
    207 
    208 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
    209 	    (char *)&acg);
    210 
    211 	if (!cg_chkmagic(&acg, needswap)) {
    212 		warnx("cg %d: bad magic number", cg);
    213 		errs++;
    214 		return (1);
    215 	}
    216 
    217 	bn = dtogd(fs, fsbn);
    218 	if (isclr(cg_blksfree(&acg, needswap), bn))
    219 		warnx("Warning: sector %d is in use", blkno);
    220 
    221 	return (0);
    222 }
    223 
    224 /*
    225  * read a block from the file system
    226  */
    227 void
    228 rdfs(bno, size, bf)
    229 	daddr_t bno;
    230 	int size;
    231 	char *bf;
    232 {
    233 	int n;
    234 
    235 	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) == -1)
    236 		err(1, "seek error at block %d", bno);
    237 
    238 	switch (n = read(fsi, bf, size)) {
    239 	case -1:
    240 		err(1, "read error at block %d", bno);
    241 		break;
    242 
    243 	default:
    244 		if (n == size)
    245 			return;
    246 		errx(1, "incomplete read at block %d", bno);
    247 	}
    248 }
    249