xref: /csrg-svn/sbin/badsect/badsect.c (revision 69257)
122486Sdist /*
261476Sbostic  * Copyright (c) 1981, 1983, 1993
361476Sbostic  *	The Regents of the University of California.  All rights reserved.
436194Sbostic  *
542698Sbostic  * %sccs.include.redist.c%
622486Sdist  */
73724Sroot 
822486Sdist #ifndef lint
961476Sbostic static char copyright[] =
1061476Sbostic "@(#) Copyright (c) 1981, 1983, 1993\n\
1161476Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1236194Sbostic #endif /* not lint */
1322486Sdist 
1422486Sdist #ifndef lint
15*69257Smckusick static char sccsid[] = "@(#)badsect.c	8.2 (Berkeley) 05/04/95";
1636194Sbostic #endif /* not lint */
1722486Sdist 
183724Sroot /*
193724Sroot  * badsect
203724Sroot  *
213724Sroot  * Badsect takes a list of file-system relative sector numbers
223724Sroot  * and makes files containing the blocks of which these sectors are a part.
233724Sroot  * It can be used to contain sectors which have problems if these sectors
243724Sroot  * are not part of the bad file for the pack (see bad144).  For instance,
253724Sroot  * this program can be used if the driver for the file system in question
263724Sroot  * does not support bad block forwarding.
273724Sroot  */
283724Sroot #include <sys/param.h>
2911272Smckusick #include <sys/dir.h>
3011272Smckusick #include <sys/stat.h>
3160144Sbostic 
32*69257Smckusick #include <ufs/ufs/dinode.h>
3351613Sbostic #include <ufs/ffs/fs.h>
3460144Sbostic 
3560144Sbostic #include <fcntl.h>
3660144Sbostic #include <paths.h>
3760144Sbostic #include <stdio.h>
3860144Sbostic #include <stdlib.h>
3957994Sralph #include <unistd.h>
403724Sroot 
4111272Smckusick union {
4211272Smckusick 	struct	fs fs;
4311272Smckusick 	char	fsx[SBSIZE];
4411272Smckusick } ufs;
4511272Smckusick #define sblock	ufs.fs
4611272Smckusick union {
4711272Smckusick 	struct	cg cg;
4811272Smckusick 	char	cgx[MAXBSIZE];
4911272Smckusick } ucg;
5011272Smckusick #define	acg	ucg.cg
5111272Smckusick struct	fs *fs;
5211272Smckusick int	fso, fsi;
5311272Smckusick int	errs;
5430558Smckusick long	dev_bsize = 1;
5511272Smckusick 
5611272Smckusick char buf[MAXBSIZE];
5711272Smckusick 
5860144Sbostic void	rdfs __P((daddr_t, int, char *));
5960144Sbostic int	chkuse __P((daddr_t, int));
6011272Smckusick 
6160144Sbostic int
main(argc,argv)623724Sroot main(argc, argv)
633724Sroot 	int argc;
6411272Smckusick 	char *argv[];
653724Sroot {
6611272Smckusick 	daddr_t number;
6711272Smckusick 	struct stat stbuf, devstat;
6811272Smckusick 	register struct direct *dp;
6911272Smckusick 	DIR *dirp;
7011272Smckusick 	char name[BUFSIZ];
713724Sroot 
7211272Smckusick 	if (argc < 3) {
7311272Smckusick 		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
7411272Smckusick 		exit(1);
753724Sroot 	}
7611272Smckusick 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
7711272Smckusick 		perror(argv[1]);
7811272Smckusick 		exit(2);
7911272Smckusick 	}
8037944Sbostic 	strcpy(name, _PATH_DEV);
8111272Smckusick 	if ((dirp = opendir(name)) == NULL) {
8211272Smckusick 		perror(name);
8311272Smckusick 		exit(3);
8411272Smckusick 	}
8511272Smckusick 	while ((dp = readdir(dirp)) != NULL) {
8611272Smckusick 		strcpy(&name[5], dp->d_name);
8711272Smckusick 		if (stat(name, &devstat) < 0) {
8811272Smckusick 			perror(name);
8911272Smckusick 			exit(4);
9011272Smckusick 		}
9111272Smckusick 		if (stbuf.st_dev == devstat.st_rdev &&
9211272Smckusick 		    (devstat.st_mode & IFMT) == IFBLK)
9311272Smckusick 			break;
9411272Smckusick 	}
9511272Smckusick 	closedir(dirp);
9611272Smckusick 	if (dp == NULL) {
9711272Smckusick 		printf("Cannot find dev 0%o corresponding to %s\n",
9811272Smckusick 			stbuf.st_rdev, argv[1]);
9911272Smckusick 		exit(5);
10011272Smckusick 	}
10111272Smckusick 	if ((fsi = open(name, 0)) < 0) {
10211272Smckusick 		perror(name);
10311272Smckusick 		exit(6);
10411272Smckusick 	}
10511272Smckusick 	fs = &sblock;
10630558Smckusick 	rdfs(SBOFF, SBSIZE, (char *)fs);
10730558Smckusick 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
10811272Smckusick 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
10911272Smckusick 		number = atoi(*argv);
11011272Smckusick 		if (chkuse(number, 1))
11111272Smckusick 			continue;
11211272Smckusick 		if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
11311272Smckusick 			perror(*argv);
11411272Smckusick 			errs++;
11511272Smckusick 		}
11611272Smckusick 	}
11711272Smckusick 	printf("Don't forget to run ``fsck %s''\n", name);
1183724Sroot 	exit(errs);
1193724Sroot }
12011272Smckusick 
12160144Sbostic int
chkuse(blkno,cnt)12211272Smckusick chkuse(blkno, cnt)
12311272Smckusick 	daddr_t blkno;
12411272Smckusick 	int cnt;
12511272Smckusick {
12611272Smckusick 	int cg;
12711272Smckusick 	daddr_t fsbn, bn;
12811272Smckusick 
12911272Smckusick 	fsbn = dbtofsb(fs, blkno);
13011272Smckusick 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
13111272Smckusick 		printf("block %d out of range of file system\n", blkno);
13211272Smckusick 		return (1);
13311272Smckusick 	}
13411272Smckusick 	cg = dtog(fs, fsbn);
13511272Smckusick 	if (fsbn < cgdmin(fs, cg)) {
13611272Smckusick 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
13711272Smckusick 			printf("block %d in non-data area: cannot attach\n",
13811272Smckusick 				blkno);
13911272Smckusick 			return (1);
14011272Smckusick 		}
14111272Smckusick 	} else {
14211272Smckusick 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
14311272Smckusick 			printf("block %d in non-data area: cannot attach\n",
14411272Smckusick 				blkno);
14511272Smckusick 			return (1);
14611272Smckusick 		}
14711272Smckusick 	}
14811272Smckusick 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
14911272Smckusick 	    (char *)&acg);
15034575Smckusick 	if (!cg_chkmagic(&acg)) {
15111272Smckusick 		fprintf(stderr, "cg %d: bad magic number\n", cg);
15211272Smckusick 		errs++;
15323722Sbloom 		return (1);
15411272Smckusick 	}
15511272Smckusick 	bn = dtogd(fs, fsbn);
15634575Smckusick 	if (isclr(cg_blksfree(&acg), bn))
15711272Smckusick 		printf("Warning: sector %d is in use\n", blkno);
15811272Smckusick 	return (0);
15911272Smckusick }
16011272Smckusick 
16111272Smckusick /*
16211272Smckusick  * read a block from the file system
16311272Smckusick  */
16460144Sbostic void
rdfs(bno,size,bf)16511272Smckusick rdfs(bno, size, bf)
16657994Sralph 	daddr_t bno;
16757994Sralph 	int size;
16811272Smckusick 	char *bf;
16911272Smckusick {
17011272Smckusick 	int n;
17111272Smckusick 
17257994Sralph 	if (lseek(fsi, (off_t)bno * dev_bsize, SEEK_SET) < 0) {
17311272Smckusick 		printf("seek error: %ld\n", bno);
17411272Smckusick 		perror("rdfs");
17511272Smckusick 		exit(1);
17611272Smckusick 	}
17711272Smckusick 	n = read(fsi, bf, size);
17857994Sralph 	if (n != size) {
17911272Smckusick 		printf("read error: %ld\n", bno);
18011272Smckusick 		perror("rdfs");
18111272Smckusick 		exit(1);
18211272Smckusick 	}
18311272Smckusick }
184