Home | History | Annotate | Line # | Download | only in badsect
badsect.c revision 1.8
      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[] = "from: @(#)badsect.c	8.1 (Berkeley) 6/5/93";*/
     42 static char *rcsid = "$Id: badsect.c,v 1.8 1994/10/31 04:19:00 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * badsect
     47  *
     48  * Badsect takes a list of file-system relative sector numbers
     49  * and makes files containing the blocks of which these sectors are a part.
     50  * It can be used to contain sectors which have problems if these sectors
     51  * are not part of the bad file for the pack (see bad144).  For instance,
     52  * this program can be used if the driver for the file system in question
     53  * does not support bad block forwarding.
     54  */
     55 #include <sys/param.h>
     56 #include <sys/dir.h>
     57 #include <sys/stat.h>
     58 
     59 #include <ufs/ffs/fs.h>
     60 #include <ufs/ufs/dinode.h>
     61 
     62 #include <fcntl.h>
     63 #include <paths.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 
     69 union {
     70 	struct	fs fs;
     71 	char	fsx[SBSIZE];
     72 } ufs;
     73 #define sblock	ufs.fs
     74 union {
     75 	struct	cg cg;
     76 	char	cgx[MAXBSIZE];
     77 } ucg;
     78 #define	acg	ucg.cg
     79 struct	fs *fs;
     80 int	fso, fsi;
     81 int	errs;
     82 long	dev_bsize = 1;
     83 
     84 char buf[MAXBSIZE];
     85 
     86 void	rdfs __P((daddr_t, int, char *));
     87 int	chkuse __P((daddr_t, int));
     88 
     89 int
     90 main(argc, argv)
     91 	int argc;
     92 	char *argv[];
     93 {
     94 	daddr_t number;
     95 	struct stat stbuf, devstat;
     96 	register struct direct *dp;
     97 	DIR *dirp;
     98 	char name[BUFSIZ];
     99 
    100 	if (argc < 3) {
    101 		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
    102 		exit(1);
    103 	}
    104 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
    105 		perror(argv[1]);
    106 		exit(2);
    107 	}
    108 	strcpy(name, _PATH_DEV);
    109 	if ((dirp = opendir(name)) == NULL) {
    110 		perror(name);
    111 		exit(3);
    112 	}
    113 	while ((dp = readdir(dirp)) != NULL) {
    114 		strcpy(&name[5], dp->d_name);
    115 		if (stat(name, &devstat) < 0) {
    116 			perror(name);
    117 			exit(4);
    118 		}
    119 		if (stbuf.st_dev == devstat.st_rdev &&
    120 		    (devstat.st_mode & IFMT) == IFBLK)
    121 			break;
    122 	}
    123 	closedir(dirp);
    124 	if (dp == NULL) {
    125 		printf("Cannot find dev 0%o corresponding to %s\n",
    126 			stbuf.st_rdev, argv[1]);
    127 		exit(5);
    128 	}
    129 	if ((fsi = open(name, 0)) < 0) {
    130 		perror(name);
    131 		exit(6);
    132 	}
    133 	fs = &sblock;
    134 	rdfs(SBOFF, SBSIZE, (char *)fs);
    135 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    136 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
    137 		number = atoi(*argv);
    138 		if (chkuse(number, 1))
    139 			continue;
    140 		if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
    141 			perror(*argv);
    142 			errs++;
    143 		}
    144 	}
    145 	printf("Don't forget to run ``fsck %s''\n", name);
    146 	exit(errs);
    147 }
    148 
    149 int
    150 chkuse(blkno, cnt)
    151 	daddr_t blkno;
    152 	int cnt;
    153 {
    154 	int cg;
    155 	daddr_t fsbn, bn;
    156 
    157 	fsbn = dbtofsb(fs, blkno);
    158 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
    159 		printf("block %d out of range of file system\n", blkno);
    160 		return (1);
    161 	}
    162 	cg = dtog(fs, fsbn);
    163 	if (fsbn < cgdmin(fs, cg)) {
    164 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
    165 			printf("block %d in non-data area: cannot attach\n",
    166 				blkno);
    167 			return (1);
    168 		}
    169 	} else {
    170 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
    171 			printf("block %d in non-data area: cannot attach\n",
    172 				blkno);
    173 			return (1);
    174 		}
    175 	}
    176 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
    177 	    (char *)&acg);
    178 	if (!cg_chkmagic(&acg)) {
    179 		fprintf(stderr, "cg %d: bad magic number\n", cg);
    180 		errs++;
    181 		return (1);
    182 	}
    183 	bn = dtogd(fs, fsbn);
    184 	if (isclr(cg_blksfree(&acg), bn))
    185 		printf("Warning: sector %d is in use\n", blkno);
    186 	return (0);
    187 }
    188 
    189 /*
    190  * read a block from the file system
    191  */
    192 void
    193 rdfs(bno, size, bf)
    194 	daddr_t bno;
    195 	int size;
    196 	char *bf;
    197 {
    198 	int n;
    199 
    200 	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) {
    201 		printf("seek error: %ld\n", bno);
    202 		perror("rdfs");
    203 		exit(1);
    204 	}
    205 	n = read(fsi, bf, size);
    206 	if (n != size) {
    207 		printf("read error: %ld\n", bno);
    208 		perror("rdfs");
    209 		exit(1);
    210 	}
    211 }
    212