xref: /csrg-svn/sbin/badsect/badsect.c (revision 30558)
122486Sdist /*
222486Sdist  * Copyright (c) 1983 Regents of the University of California.
322486Sdist  * All rights reserved.  The Berkeley software License Agreement
422486Sdist  * specifies the terms and conditions for redistribution.
522486Sdist  */
63724Sroot 
722486Sdist #ifndef lint
822486Sdist char copyright[] =
922486Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1022486Sdist  All rights reserved.\n";
1122486Sdist #endif not lint
1222486Sdist 
1322486Sdist #ifndef lint
14*30558Smckusick static char sccsid[] = "@(#)badsect.c	5.3 (Berkeley) 02/23/87";
1522486Sdist #endif not lint
1622486Sdist 
173724Sroot /*
183724Sroot  * badsect
193724Sroot  *
203724Sroot  * Badsect takes a list of file-system relative sector numbers
213724Sroot  * and makes files containing the blocks of which these sectors are a part.
223724Sroot  * It can be used to contain sectors which have problems if these sectors
233724Sroot  * are not part of the bad file for the pack (see bad144).  For instance,
243724Sroot  * this program can be used if the driver for the file system in question
253724Sroot  * does not support bad block forwarding.
263724Sroot  */
2711272Smckusick #include <stdio.h>
283724Sroot #include <sys/param.h>
2911272Smckusick #include <sys/fs.h>
3011272Smckusick #include <sys/dir.h>
3111272Smckusick #include <sys/stat.h>
3211272Smckusick #include <sys/inode.h>
333724Sroot 
3411272Smckusick union {
3511272Smckusick 	struct	fs fs;
3611272Smckusick 	char	fsx[SBSIZE];
3711272Smckusick } ufs;
3811272Smckusick #define sblock	ufs.fs
3911272Smckusick union {
4011272Smckusick 	struct	cg cg;
4111272Smckusick 	char	cgx[MAXBSIZE];
4211272Smckusick } ucg;
4311272Smckusick #define	acg	ucg.cg
4411272Smckusick struct	fs *fs;
4511272Smckusick int	fso, fsi;
4611272Smckusick int	errs;
47*30558Smckusick long	dev_bsize = 1;
4811272Smckusick 
4911272Smckusick char buf[MAXBSIZE];
5011272Smckusick 
5111272Smckusick 
523724Sroot main(argc, argv)
533724Sroot 	int argc;
5411272Smckusick 	char *argv[];
553724Sroot {
5611272Smckusick 	daddr_t number;
5711272Smckusick 	struct stat stbuf, devstat;
5811272Smckusick 	register struct direct *dp;
5911272Smckusick 	DIR *dirp;
6011272Smckusick 	int fd;
6111272Smckusick 	char name[BUFSIZ];
623724Sroot 
6311272Smckusick 	if (argc < 3) {
6411272Smckusick 		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
6511272Smckusick 		exit(1);
663724Sroot 	}
6711272Smckusick 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
6811272Smckusick 		perror(argv[1]);
6911272Smckusick 		exit(2);
7011272Smckusick 	}
7111272Smckusick 	strcpy(name, "/dev/");
7211272Smckusick 	if ((dirp = opendir(name)) == NULL) {
7311272Smckusick 		perror(name);
7411272Smckusick 		exit(3);
7511272Smckusick 	}
7611272Smckusick 	while ((dp = readdir(dirp)) != NULL) {
7711272Smckusick 		strcpy(&name[5], dp->d_name);
7811272Smckusick 		if (stat(name, &devstat) < 0) {
7911272Smckusick 			perror(name);
8011272Smckusick 			exit(4);
8111272Smckusick 		}
8211272Smckusick 		if (stbuf.st_dev == devstat.st_rdev &&
8311272Smckusick 		    (devstat.st_mode & IFMT) == IFBLK)
8411272Smckusick 			break;
8511272Smckusick 	}
8611272Smckusick 	closedir(dirp);
8711272Smckusick 	if (dp == NULL) {
8811272Smckusick 		printf("Cannot find dev 0%o corresponding to %s\n",
8911272Smckusick 			stbuf.st_rdev, argv[1]);
9011272Smckusick 		exit(5);
9111272Smckusick 	}
9211272Smckusick 	if ((fsi = open(name, 0)) < 0) {
9311272Smckusick 		perror(name);
9411272Smckusick 		exit(6);
9511272Smckusick 	}
9611272Smckusick 	fs = &sblock;
97*30558Smckusick 	rdfs(SBOFF, SBSIZE, (char *)fs);
98*30558Smckusick 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
9911272Smckusick 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
10011272Smckusick 		number = atoi(*argv);
10111272Smckusick 		if (chkuse(number, 1))
10211272Smckusick 			continue;
10311272Smckusick 		if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
10411272Smckusick 			perror(*argv);
10511272Smckusick 			errs++;
10611272Smckusick 		}
10711272Smckusick 	}
10811272Smckusick 	printf("Don't forget to run ``fsck %s''\n", name);
1093724Sroot 	exit(errs);
1103724Sroot }
11111272Smckusick 
11211272Smckusick chkuse(blkno, cnt)
11311272Smckusick 	daddr_t blkno;
11411272Smckusick 	int cnt;
11511272Smckusick {
11611272Smckusick 	int cg;
11711272Smckusick 	daddr_t fsbn, bn;
11811272Smckusick 
11911272Smckusick 	fsbn = dbtofsb(fs, blkno);
12011272Smckusick 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
12111272Smckusick 		printf("block %d out of range of file system\n", blkno);
12211272Smckusick 		return (1);
12311272Smckusick 	}
12411272Smckusick 	cg = dtog(fs, fsbn);
12511272Smckusick 	if (fsbn < cgdmin(fs, cg)) {
12611272Smckusick 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
12711272Smckusick 			printf("block %d in non-data area: cannot attach\n",
12811272Smckusick 				blkno);
12911272Smckusick 			return (1);
13011272Smckusick 		}
13111272Smckusick 	} else {
13211272Smckusick 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
13311272Smckusick 			printf("block %d in non-data area: cannot attach\n",
13411272Smckusick 				blkno);
13511272Smckusick 			return (1);
13611272Smckusick 		}
13711272Smckusick 	}
13811272Smckusick 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
13911272Smckusick 	    (char *)&acg);
14011272Smckusick 	if (acg.cg_magic != CG_MAGIC) {
14111272Smckusick 		fprintf(stderr, "cg %d: bad magic number\n", cg);
14211272Smckusick 		errs++;
14323722Sbloom 		return (1);
14411272Smckusick 	}
14511272Smckusick 	bn = dtogd(fs, fsbn);
14611272Smckusick 	if (isclr(acg.cg_free, bn))
14711272Smckusick 		printf("Warning: sector %d is in use\n", blkno);
14811272Smckusick 	return (0);
14911272Smckusick }
15011272Smckusick 
15111272Smckusick /*
15211272Smckusick  * read a block from the file system
15311272Smckusick  */
15411272Smckusick rdfs(bno, size, bf)
15511272Smckusick 	int bno, size;
15611272Smckusick 	char *bf;
15711272Smckusick {
15811272Smckusick 	int n;
15911272Smckusick 
160*30558Smckusick 	if (lseek(fsi, bno * dev_bsize, 0) < 0) {
16111272Smckusick 		printf("seek error: %ld\n", bno);
16211272Smckusick 		perror("rdfs");
16311272Smckusick 		exit(1);
16411272Smckusick 	}
16511272Smckusick 	n = read(fsi, bf, size);
16611272Smckusick 	if(n != size) {
16711272Smckusick 		printf("read error: %ld\n", bno);
16811272Smckusick 		perror("rdfs");
16911272Smckusick 		exit(1);
17011272Smckusick 	}
17111272Smckusick }
172