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