xref: /csrg-svn/sbin/badsect/badsect.c (revision 37944)
122486Sdist /*
236194Sbostic  * Copyright (c) 1981, 1983 The Regents of the University of California.
336194Sbostic  * All rights reserved.
436194Sbostic  *
536194Sbostic  * Redistribution and use in source and binary forms are permitted
636194Sbostic  * provided that the above copyright notice and this paragraph are
736194Sbostic  * duplicated in all such forms and that any documentation,
836194Sbostic  * advertising materials, and other materials related to such
936194Sbostic  * distribution and use acknowledge that the software was developed
1036194Sbostic  * by the University of California, Berkeley.  The name of the
1136194Sbostic  * University may not be used to endorse or promote products derived
1236194Sbostic  * from this software without specific prior written permission.
1336194Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1436194Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1536194Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622486Sdist  */
173724Sroot 
1822486Sdist #ifndef lint
1922486Sdist char copyright[] =
2036194Sbostic "@(#) Copyright (c) 1981, 1983 The Regents of the University of California.\n\
2122486Sdist  All rights reserved.\n";
2236194Sbostic #endif /* not lint */
2322486Sdist 
2422486Sdist #ifndef lint
25*37944Sbostic static char sccsid[] = "@(#)badsect.c	5.6 (Berkeley) 05/11/89";
2636194Sbostic #endif /* not lint */
2722486Sdist 
283724Sroot /*
293724Sroot  * badsect
303724Sroot  *
313724Sroot  * Badsect takes a list of file-system relative sector numbers
323724Sroot  * and makes files containing the blocks of which these sectors are a part.
333724Sroot  * It can be used to contain sectors which have problems if these sectors
343724Sroot  * are not part of the bad file for the pack (see bad144).  For instance,
353724Sroot  * this program can be used if the driver for the file system in question
363724Sroot  * does not support bad block forwarding.
373724Sroot  */
383724Sroot #include <sys/param.h>
3911272Smckusick #include <sys/dir.h>
4011272Smckusick #include <sys/stat.h>
41*37944Sbostic #include <sys/fs.h>
4211272Smckusick #include <sys/inode.h>
43*37944Sbostic #include <stdio.h>
44*37944Sbostic #include <paths.h>
453724Sroot 
4611272Smckusick union {
4711272Smckusick 	struct	fs fs;
4811272Smckusick 	char	fsx[SBSIZE];
4911272Smckusick } ufs;
5011272Smckusick #define sblock	ufs.fs
5111272Smckusick union {
5211272Smckusick 	struct	cg cg;
5311272Smckusick 	char	cgx[MAXBSIZE];
5411272Smckusick } ucg;
5511272Smckusick #define	acg	ucg.cg
5611272Smckusick struct	fs *fs;
5711272Smckusick int	fso, fsi;
5811272Smckusick int	errs;
5930558Smckusick long	dev_bsize = 1;
6011272Smckusick 
6111272Smckusick char buf[MAXBSIZE];
6211272Smckusick 
6311272Smckusick 
643724Sroot main(argc, argv)
653724Sroot 	int argc;
6611272Smckusick 	char *argv[];
673724Sroot {
6811272Smckusick 	daddr_t number;
6911272Smckusick 	struct stat stbuf, devstat;
7011272Smckusick 	register struct direct *dp;
7111272Smckusick 	DIR *dirp;
7211272Smckusick 	int fd;
7311272Smckusick 	char name[BUFSIZ];
743724Sroot 
7511272Smckusick 	if (argc < 3) {
7611272Smckusick 		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
7711272Smckusick 		exit(1);
783724Sroot 	}
7911272Smckusick 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
8011272Smckusick 		perror(argv[1]);
8111272Smckusick 		exit(2);
8211272Smckusick 	}
83*37944Sbostic 	strcpy(name, _PATH_DEV);
8411272Smckusick 	if ((dirp = opendir(name)) == NULL) {
8511272Smckusick 		perror(name);
8611272Smckusick 		exit(3);
8711272Smckusick 	}
8811272Smckusick 	while ((dp = readdir(dirp)) != NULL) {
8911272Smckusick 		strcpy(&name[5], dp->d_name);
9011272Smckusick 		if (stat(name, &devstat) < 0) {
9111272Smckusick 			perror(name);
9211272Smckusick 			exit(4);
9311272Smckusick 		}
9411272Smckusick 		if (stbuf.st_dev == devstat.st_rdev &&
9511272Smckusick 		    (devstat.st_mode & IFMT) == IFBLK)
9611272Smckusick 			break;
9711272Smckusick 	}
9811272Smckusick 	closedir(dirp);
9911272Smckusick 	if (dp == NULL) {
10011272Smckusick 		printf("Cannot find dev 0%o corresponding to %s\n",
10111272Smckusick 			stbuf.st_rdev, argv[1]);
10211272Smckusick 		exit(5);
10311272Smckusick 	}
10411272Smckusick 	if ((fsi = open(name, 0)) < 0) {
10511272Smckusick 		perror(name);
10611272Smckusick 		exit(6);
10711272Smckusick 	}
10811272Smckusick 	fs = &sblock;
10930558Smckusick 	rdfs(SBOFF, SBSIZE, (char *)fs);
11030558Smckusick 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
11111272Smckusick 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
11211272Smckusick 		number = atoi(*argv);
11311272Smckusick 		if (chkuse(number, 1))
11411272Smckusick 			continue;
11511272Smckusick 		if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
11611272Smckusick 			perror(*argv);
11711272Smckusick 			errs++;
11811272Smckusick 		}
11911272Smckusick 	}
12011272Smckusick 	printf("Don't forget to run ``fsck %s''\n", name);
1213724Sroot 	exit(errs);
1223724Sroot }
12311272Smckusick 
12411272Smckusick chkuse(blkno, cnt)
12511272Smckusick 	daddr_t blkno;
12611272Smckusick 	int cnt;
12711272Smckusick {
12811272Smckusick 	int cg;
12911272Smckusick 	daddr_t fsbn, bn;
13011272Smckusick 
13111272Smckusick 	fsbn = dbtofsb(fs, blkno);
13211272Smckusick 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
13311272Smckusick 		printf("block %d out of range of file system\n", blkno);
13411272Smckusick 		return (1);
13511272Smckusick 	}
13611272Smckusick 	cg = dtog(fs, fsbn);
13711272Smckusick 	if (fsbn < cgdmin(fs, cg)) {
13811272Smckusick 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
13911272Smckusick 			printf("block %d in non-data area: cannot attach\n",
14011272Smckusick 				blkno);
14111272Smckusick 			return (1);
14211272Smckusick 		}
14311272Smckusick 	} else {
14411272Smckusick 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
14511272Smckusick 			printf("block %d in non-data area: cannot attach\n",
14611272Smckusick 				blkno);
14711272Smckusick 			return (1);
14811272Smckusick 		}
14911272Smckusick 	}
15011272Smckusick 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
15111272Smckusick 	    (char *)&acg);
15234575Smckusick 	if (!cg_chkmagic(&acg)) {
15311272Smckusick 		fprintf(stderr, "cg %d: bad magic number\n", cg);
15411272Smckusick 		errs++;
15523722Sbloom 		return (1);
15611272Smckusick 	}
15711272Smckusick 	bn = dtogd(fs, fsbn);
15834575Smckusick 	if (isclr(cg_blksfree(&acg), bn))
15911272Smckusick 		printf("Warning: sector %d is in use\n", blkno);
16011272Smckusick 	return (0);
16111272Smckusick }
16211272Smckusick 
16311272Smckusick /*
16411272Smckusick  * read a block from the file system
16511272Smckusick  */
16611272Smckusick rdfs(bno, size, bf)
16711272Smckusick 	int bno, size;
16811272Smckusick 	char *bf;
16911272Smckusick {
17011272Smckusick 	int n;
17111272Smckusick 
17230558Smckusick 	if (lseek(fsi, bno * dev_bsize, 0) < 0) {
17311272Smckusick 		printf("seek error: %ld\n", bno);
17411272Smckusick 		perror("rdfs");
17511272Smckusick 		exit(1);
17611272Smckusick 	}
17711272Smckusick 	n = read(fsi, bf, size);
17811272Smckusick 	if(n != size) {
17911272Smckusick 		printf("read error: %ld\n", bno);
18011272Smckusick 		perror("rdfs");
18111272Smckusick 		exit(1);
18211272Smckusick 	}
18311272Smckusick }
184