xref: /csrg-svn/sbin/badsect/badsect.c (revision 36194)
122486Sdist /*
2*36194Sbostic  * Copyright (c) 1981, 1983 The Regents of the University of California.
3*36194Sbostic  * All rights reserved.
4*36194Sbostic  *
5*36194Sbostic  * Redistribution and use in source and binary forms are permitted
6*36194Sbostic  * provided that the above copyright notice and this paragraph are
7*36194Sbostic  * duplicated in all such forms and that any documentation,
8*36194Sbostic  * advertising materials, and other materials related to such
9*36194Sbostic  * distribution and use acknowledge that the software was developed
10*36194Sbostic  * by the University of California, Berkeley.  The name of the
11*36194Sbostic  * University may not be used to endorse or promote products derived
12*36194Sbostic  * from this software without specific prior written permission.
13*36194Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*36194Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*36194Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622486Sdist  */
173724Sroot 
1822486Sdist #ifndef lint
1922486Sdist char copyright[] =
20*36194Sbostic "@(#) Copyright (c) 1981, 1983 The Regents of the University of California.\n\
2122486Sdist  All rights reserved.\n";
22*36194Sbostic #endif /* not lint */
2322486Sdist 
2422486Sdist #ifndef lint
25*36194Sbostic static char sccsid[] = "@(#)badsect.c	5.5 (Berkeley) 11/01/88";
26*36194Sbostic #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  */
3811272Smckusick #include <stdio.h>
393724Sroot #include <sys/param.h>
4011272Smckusick #include <sys/fs.h>
4111272Smckusick #include <sys/dir.h>
4211272Smckusick #include <sys/stat.h>
4311272Smckusick #include <sys/inode.h>
443724Sroot 
4511272Smckusick union {
4611272Smckusick 	struct	fs fs;
4711272Smckusick 	char	fsx[SBSIZE];
4811272Smckusick } ufs;
4911272Smckusick #define sblock	ufs.fs
5011272Smckusick union {
5111272Smckusick 	struct	cg cg;
5211272Smckusick 	char	cgx[MAXBSIZE];
5311272Smckusick } ucg;
5411272Smckusick #define	acg	ucg.cg
5511272Smckusick struct	fs *fs;
5611272Smckusick int	fso, fsi;
5711272Smckusick int	errs;
5830558Smckusick long	dev_bsize = 1;
5911272Smckusick 
6011272Smckusick char buf[MAXBSIZE];
6111272Smckusick 
6211272Smckusick 
633724Sroot main(argc, argv)
643724Sroot 	int argc;
6511272Smckusick 	char *argv[];
663724Sroot {
6711272Smckusick 	daddr_t number;
6811272Smckusick 	struct stat stbuf, devstat;
6911272Smckusick 	register struct direct *dp;
7011272Smckusick 	DIR *dirp;
7111272Smckusick 	int fd;
7211272Smckusick 	char name[BUFSIZ];
733724Sroot 
7411272Smckusick 	if (argc < 3) {
7511272Smckusick 		fprintf(stderr, "usage: badsect bbdir blkno [ blkno ]\n");
7611272Smckusick 		exit(1);
773724Sroot 	}
7811272Smckusick 	if (chdir(argv[1]) < 0 || stat(".", &stbuf) < 0) {
7911272Smckusick 		perror(argv[1]);
8011272Smckusick 		exit(2);
8111272Smckusick 	}
8211272Smckusick 	strcpy(name, "/dev/");
8311272Smckusick 	if ((dirp = opendir(name)) == NULL) {
8411272Smckusick 		perror(name);
8511272Smckusick 		exit(3);
8611272Smckusick 	}
8711272Smckusick 	while ((dp = readdir(dirp)) != NULL) {
8811272Smckusick 		strcpy(&name[5], dp->d_name);
8911272Smckusick 		if (stat(name, &devstat) < 0) {
9011272Smckusick 			perror(name);
9111272Smckusick 			exit(4);
9211272Smckusick 		}
9311272Smckusick 		if (stbuf.st_dev == devstat.st_rdev &&
9411272Smckusick 		    (devstat.st_mode & IFMT) == IFBLK)
9511272Smckusick 			break;
9611272Smckusick 	}
9711272Smckusick 	closedir(dirp);
9811272Smckusick 	if (dp == NULL) {
9911272Smckusick 		printf("Cannot find dev 0%o corresponding to %s\n",
10011272Smckusick 			stbuf.st_rdev, argv[1]);
10111272Smckusick 		exit(5);
10211272Smckusick 	}
10311272Smckusick 	if ((fsi = open(name, 0)) < 0) {
10411272Smckusick 		perror(name);
10511272Smckusick 		exit(6);
10611272Smckusick 	}
10711272Smckusick 	fs = &sblock;
10830558Smckusick 	rdfs(SBOFF, SBSIZE, (char *)fs);
10930558Smckusick 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
11011272Smckusick 	for (argc -= 2, argv += 2; argc > 0; argc--, argv++) {
11111272Smckusick 		number = atoi(*argv);
11211272Smckusick 		if (chkuse(number, 1))
11311272Smckusick 			continue;
11411272Smckusick 		if (mknod(*argv, IFMT|0600, dbtofsb(fs, number)) < 0) {
11511272Smckusick 			perror(*argv);
11611272Smckusick 			errs++;
11711272Smckusick 		}
11811272Smckusick 	}
11911272Smckusick 	printf("Don't forget to run ``fsck %s''\n", name);
1203724Sroot 	exit(errs);
1213724Sroot }
12211272Smckusick 
12311272Smckusick chkuse(blkno, cnt)
12411272Smckusick 	daddr_t blkno;
12511272Smckusick 	int cnt;
12611272Smckusick {
12711272Smckusick 	int cg;
12811272Smckusick 	daddr_t fsbn, bn;
12911272Smckusick 
13011272Smckusick 	fsbn = dbtofsb(fs, blkno);
13111272Smckusick 	if ((unsigned)(fsbn+cnt) > fs->fs_size) {
13211272Smckusick 		printf("block %d out of range of file system\n", blkno);
13311272Smckusick 		return (1);
13411272Smckusick 	}
13511272Smckusick 	cg = dtog(fs, fsbn);
13611272Smckusick 	if (fsbn < cgdmin(fs, cg)) {
13711272Smckusick 		if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
13811272Smckusick 			printf("block %d in non-data area: cannot attach\n",
13911272Smckusick 				blkno);
14011272Smckusick 			return (1);
14111272Smckusick 		}
14211272Smckusick 	} else {
14311272Smckusick 		if ((fsbn+cnt) > cgbase(fs, cg+1)) {
14411272Smckusick 			printf("block %d in non-data area: cannot attach\n",
14511272Smckusick 				blkno);
14611272Smckusick 			return (1);
14711272Smckusick 		}
14811272Smckusick 	}
14911272Smckusick 	rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
15011272Smckusick 	    (char *)&acg);
15134575Smckusick 	if (!cg_chkmagic(&acg)) {
15211272Smckusick 		fprintf(stderr, "cg %d: bad magic number\n", cg);
15311272Smckusick 		errs++;
15423722Sbloom 		return (1);
15511272Smckusick 	}
15611272Smckusick 	bn = dtogd(fs, fsbn);
15734575Smckusick 	if (isclr(cg_blksfree(&acg), bn))
15811272Smckusick 		printf("Warning: sector %d is in use\n", blkno);
15911272Smckusick 	return (0);
16011272Smckusick }
16111272Smckusick 
16211272Smckusick /*
16311272Smckusick  * read a block from the file system
16411272Smckusick  */
16511272Smckusick rdfs(bno, size, bf)
16611272Smckusick 	int bno, size;
16711272Smckusick 	char *bf;
16811272Smckusick {
16911272Smckusick 	int n;
17011272Smckusick 
17130558Smckusick 	if (lseek(fsi, bno * dev_bsize, 0) < 0) {
17211272Smckusick 		printf("seek error: %ld\n", bno);
17311272Smckusick 		perror("rdfs");
17411272Smckusick 		exit(1);
17511272Smckusick 	}
17611272Smckusick 	n = read(fsi, bf, size);
17711272Smckusick 	if(n != size) {
17811272Smckusick 		printf("read error: %ld\n", bno);
17911272Smckusick 		perror("rdfs");
18011272Smckusick 		exit(1);
18111272Smckusick 	}
18211272Smckusick }
183