[comp.os.minix] Cdif on badblocks.c

ast@cs.vu.nl (Andy Tanenbaum) (02/17/91)

Here is a cdif against 1.5.10 for badblocks.c.  It is thought to work
on both V1 and V2 file systems, and for sizes > 32K.  Please post any
bug reports to the net.

Andy Tanenbaum (ast@cs.vu.nl)


---------------------------------------------------------------------------

echo x - badblocks.cdif
sed '/^X/s///' > badblocks.cdif << '/'
X*** 1.5/command/badblocks.c  crc=49945  13140	Sat Apr 21 22:26:11 1990
X--- 1.6.11/command/badblocks.c  crc=46496  14337	Sun Dec  9 23:50:49 1990
X***************
X*** 4,10 ****
X  
X  /* This program is written to handle BADBLOCKS on a hard or floppy disk.
X   * The program asks for block_numbers. These numbers can be obtained with
X!  * the program disk_check; written by A. Tanenbaum.  It then creates a
X   * file on the disk containing up to 7 bad blocks.
X   *
X   * BUG:
X--- 4,10 ----
X  
X  /* This program is written to handle BADBLOCKS on a hard or floppy disk.
X   * The program asks for block_numbers. These numbers can be obtained with
X!  * the program readall, written by A. Tanenbaum.  It then creates a
X   * file on the disk containing up to 7 bad blocks.
X   *
X   * BUG:
X***************
X*** 26,35 ****
X  #include <sys/stat.h>
X  #include <fcntl.h>
X  #include <unistd.h>
X- #include <stdio.h>
X  
X  
X  
X  /* 		Super block table.
X   *
X   * 	The disk layout is:
X--- 26,40 ----
X  #include <sys/stat.h>
X  #include <fcntl.h>
X  #include <unistd.h>
X  
X+ #include "../fs/const.h"	/* must be included before stdio.h */
X+ #undef printf			/* so its define of printf can be undone */
X+ #include "../fs/type.h"
X  
X+ #include <string.h>
X+ #include <stdio.h>
X  
X+ 
X  /* 		Super block table.
X   *
X   * 	The disk layout is:
X***************
X*** 39,80 ****
X   *    super block     1
X   *    inode map     s_imap_blocks
X   *    zone map      s_zmap_blocks
X!  *    inodes        (s_ninodes + 1 + INODES_PER_BLOCK - 1)/INODES_PER_BLOCK
X   *    unused
X   *    data zones    (s_nzones - s_firstdatazone) << s_log_zone_size
X   *
X   */
X  
X- 
X- struct super_block {
X-   ino_t s_ninodes;		/* # usable inodes on the minor device */
X-   zone_nr s_nzones;		/* total device size, including bit maps etc */
X-   unshort s_imap_block;		/* # of block used by inode bit map */
X-   unshort s_zmap_block;		/* # of block used by zone bit map */
X-   zone_nr s_firstdatazone;	/* number of first data zone */
X-   short int s_log_zone_size;	/* log2 of block/zone */
X-   off_t s_max_size;		/* maximum file size on this device */
X-   int s_magic;			/* magic number to recognize super-block */
X- } super_block;
X- 
X- #define SUPER_MAGIC	0x137F
X- 
X- 
X- #define NR_ZONE_NUMS	9
X- #define NR_DZONE_NUMS	(NR_ZONE_NUMS -2 )
X- 
X- struct d_inode {		/* disk inode. */
X-   mode_t i_mode;		/* file type, protection, etc. */
X-   uid_t i_uid;			/* user id of the file's owner */
X-   off_t i_size;			/* current file size in bytes */
X-   time_t i_mtime;		/* when was file data last changed */
X-   gid_t i_gid;			/* group number */
X-   nlink_t i_nlinks;		/* how many links to this file */
X-   zone_nr i_zone[NR_ZONE_NUMS];	/* blocks nums for direct, ind, and dbl ind */
X- } d_inode;
X- 
X- 
X- 
X  #define OK	0
X  #define NOT_OK	1
X  #define QUIT	2
X--- 44,55 ----
X   *    super block     1
X   *    inode map     s_imap_blocks
X   *    zone map      s_zmap_blocks
X!  *    inodes        (s_ninodes + 1 + inodes_per_block - 1)/inodes_per_block
X   *    unused
X   *    data zones    (s_nzones - s_firstdatazone) << s_log_zone_size
X   *
X   */
X  
X  #define OK	0
X  #define NOT_OK	1
X  #define QUIT	2
X***************
X*** 88,100 ****
X  #define FILE_EXISTS	3
X  #define SUCCESS		4
X  
X! #define BLOCK_SIZE	1024
X! #define INODES_PER_BLOCK	(BLOCK_SIZE/sizeof(struct d_inode))
X! 
X! #define INODE_SIZE (sizeof (struct d_inode) )
X! #define SUPER_SIZE (sizeof (struct super_block) )
X  #define SIZE_OF_INT   (sizeof (int) )
X  
X   /* ====== globals ======= */
X  
X  char *dev_name;
X--- 63,94 ----
X  #define FILE_EXISTS	3
X  #define SUCCESS		4
X  
X! #define BYTE         0377
X! #define BLOCK_SIZE   1024
X  #define SIZE_OF_INT   (sizeof (int) )
X  
X+ /* Define V_NR_DZONES as the larger of V1_NR_DZONES and V2_NR_DZONES. */
X+ #if (V1_NR_DZONES > V2_NR_DZONES)
X+ #define V_NR_DZONES V1_NR_DZONES
X+ #define V_SMALLER   V2_NR_DZONES
X+ #else
X+ #define V_NR_DZONES V2_NR_DZONES
X+ #define V_SMALLER   V1_NR_DZONES
X+ #endif
X+ 
X+ struct super_block {
X+   ino_t s_ninodes;		/* # usable inodes on the minor device */
X+   zone1_t  s_nzones;		/* total device size, including bit maps etc */
X+   short s_imap_blocks;		/* # of blocks used by inode bit map */
X+   short s_zmap_blocks;		/* # of blocks used by zone bit map */
X+   zone1_t s_firstdatazone;	/* number of first data zone */
X+   short s_log_zone_size;	/* log2 of blocks/zone */
X+   off_t s_max_size;		/* maximum file size on this device */
X+   short s_magic;		/* magic number to recognize super-blocks */
X+   short s_pad;			/* try to avoid compiler-dependent padding */
X+   zone_t s_zones;		/* number of zones (replaces s_nzones in V2) */
X+ } super_block;
X+ 
X   /* ====== globals ======= */
X  
X  char *dev_name;
X***************
X*** 102,108 ****
X  char file_name[50];
X  char dir_name[] = "/tmpXXXXXX";
X  
X! int block[NR_DZONE_NUMS + 1];	/* last block contains zero */
X  int interactive;		/* 1 if interactive (argc == 2) */
X  int position = 2;		/* next block # is argv[position] */
X  
X--- 96,102 ----
X  char file_name[50];
X  char dir_name[] = "/tmpXXXXXX";
X  
X! zone_t block[V_NR_DZONES + 1];	/* last block contains zero */
X  int interactive;		/* 1 if interactive (argc == 2) */
X  int position = 2;		/* next block # is argv[position] */
X  
X***************
X*** 110,119 ****
X  int fd;
X  int eofseen;			/* set if '\n' seen */
X  struct stat stat_buf;
X- struct d_inode *ip;
X  struct super_block *sp;
X  
X! extern char *strcat();
X  
X  
X   /* ====== super block routines ======= */
X--- 104,117 ----
X  int fd;
X  int eofseen;			/* set if '\n' seen */
X  struct stat stat_buf;
X  struct super_block *sp;
X+ int inodes_per_block, inode_size;
X+ int v1fs;			/* TRUE for V1 file system, FALSE for V2 */
X  
X! d1_inode d1inode;		/* declare a V1 disk inode */
X! d1_inode *ip1;
X! d2_inode d2inode;		/* declare a V2 disk inode */
X! d2_inode *ip2;
X  
X  
X   /* ====== super block routines ======= */
X***************
X*** 142,149 ****
X  {
X    rw_super(READ);
X  
X!   if (sp->s_magic != SUPER_MAGIC) {	/* check */
X! 	printf("Bad magic number in super_block?!\n");
X  	done(DIR_CREATED);
X    }
X  }
X--- 140,154 ----
X  {
X    rw_super(READ);
X  
X!   if (sp->s_magic == SUPER_MAGIC) {
X! 	/* This is a V1 file system. */
X! 	v1fs = 1;		/* file system is not V2 */
X!   } else if (sp->s_magic == SUPER_V2) {
X! 	/* This is a V2 file system. */
X! 	v1fs = 0;		/* this is a V2 file system */
X!   } else {
X! 	/* Neither V1 nor V2. */
X! 	printf("Bad magic number in super_block (0x%x)\n", sp->s_magic);
X  	done(DIR_CREATED);
X    }
X  }
X***************
X*** 165,187 ****
X  
X    i_num = stat_ptr->st_ino;
X  
X!   blk = (long) (2 + sp->s_imap_block + sp->s_zmap_block);
X!   blk += (long) ((i_num - 1) / INODES_PER_BLOCK);
X    blk *= (long) (BLOCK_SIZE);	/* this block */
X  
X!   offset = (long) ((i_num - 1) % INODES_PER_BLOCK);
X!   offset *= (long) (INODE_SIZE);/* and this offset */
X  
X    lseek(fd, 0L, SEEK_SET);	/* rewind */
X    lseek(fd, (long) (blk + offset), SEEK_SET);	/* seek */
X  
X    /* Pointer is at the inode */
X!   if (rw_mode == READ) {	/* read it */
X! 	rwd = read(fd, (char *) ip, INODE_SIZE);
X!   } else {			/* write it */
X! 	rwd = write(fd, (char *) ip, INODE_SIZE);
X    }
X!   if (rwd != INODE_SIZE) {	/* ok ? */
X  	printf("Bad %s in get_inode()\n", (rw_mode == READ) ? "read" :
X  	       "write");
X  	done(DIR_CREATED);
X--- 170,203 ----
X  
X    i_num = stat_ptr->st_ino;
X  
X!   blk = (long) (2 + sp->s_imap_blocks + sp->s_zmap_blocks);
X!   blk += (long) ((i_num - 1) / inodes_per_block);
X    blk *= (long) (BLOCK_SIZE);	/* this block */
X  
X!   offset = (long) ((i_num - 1) % inodes_per_block);
X!   offset *= (long) (inode_size);/* and this offset */
X  
X    lseek(fd, 0L, SEEK_SET);	/* rewind */
X    lseek(fd, (long) (blk + offset), SEEK_SET);	/* seek */
X  
X    /* Pointer is at the inode */
X!   if (v1fs) {
X! 	/* This is a V1 file system. */
X! 	if (rw_mode == READ) {	/* read it */
X! 		rwd = read(fd, (char *) ip1, inode_size);
X! 	} else {			/* write it */
X! 		rwd = write(fd, (char *) ip1, inode_size);
X! 	}
X!   } else {
X! 	/* This is a V2 file system. */
X! 	if (rw_mode == READ) {	/* read it */
X! 		rwd = read(fd, (char *) ip2, inode_size);
X! 	} else {			/* write it */
X! 		rwd = write(fd, (char *) ip2, inode_size);
X! 	}
X    }
X! 
X!   if (rwd != inode_size) {	/* ok ? */
X  	printf("Bad %s in get_inode()\n", (rw_mode == READ) ? "read" :
X  	       "write");
X  	done(DIR_CREATED);
X***************
X*** 196,203 ****
X  
X    rw_inode(stat_ptr, READ);
X  
X!   for (cnt = 0; cnt < NR_ZONE_NUMS; cnt++)
X! 	ip->i_zone[cnt] = 0;	/* Just to be safe */
X  }
X  
X  put_inode(stat_ptr)
X--- 212,224 ----
X  
X    rw_inode(stat_ptr, READ);
X  
X!   if (v1fs) {
X! 	for (cnt = 0; cnt < V1_NR_TZONES; cnt++)
X! 		ip1->d1_zone[cnt] = 0;	/* Just to be safe */
X!   } else {
X! 	for (cnt = 0; cnt < V2_NR_TZONES; cnt++)
X! 		ip2->d2_zone[cnt] = 0;	/* Just to be safe */
X!   }
X  }
X  
X  put_inode(stat_ptr)
X***************
X*** 216,222 ****
X    struct stat dev_stat;
X  
X    sp = &super_block;
X!   ip = &d_inode;
X  
X    if (argc < 2 || argc > 9) {
X  	fprintf(stderr, "Usage: %s block_special [up_to_7_blocks]\n", argv[0]);
X--- 237,244 ----
X    struct stat dev_stat;
X  
X    sp = &super_block;
X!   ip1 = &d1inode;
X!   ip2 = &d2inode;
X  
X    if (argc < 2 || argc > 9) {
X  	fprintf(stderr, "Usage: %s block_special [up_to_7_blocks]\n", argv[0]);
X***************
X*** 299,313 ****
X  	printf("This program can not handle it\n");
X  	done(DIR_CREATED);
X    }
X    get_inode(&stat_buf);
X  
X    for (finished = 0; !finished;) {
X  	if (interactive)
X  		printf("Give up to %d bad block numbers separated by spaces\n",
X! 							       NR_DZONE_NUMS);
X  	reset_blks();
X  	cnt = 0;		/* cnt keep track of the zone's */
X! 	while (cnt < NR_DZONE_NUMS) {
X  		int tst;
X  
X  		if (interactive)
X--- 321,345 ----
X  	printf("This program can not handle it\n");
X  	done(DIR_CREATED);
X    }
X+ 
X+   /* The number of inodes in a block differs in V1 and V2. */
X+   if (v1fs) {
X+ 	inodes_per_block = V1_INODES_PER_BLOCK;
X+ 	inode_size = V1_INODE_SIZE;
X+   } else {
X+ 	inodes_per_block = V2_INODES_PER_BLOCK;
X+ 	inode_size = V2_INODE_SIZE;
X+   }
X+ 
X    get_inode(&stat_buf);
X  
X    for (finished = 0; !finished;) {
X  	if (interactive)
X  		printf("Give up to %d bad block numbers separated by spaces\n",
X! 							       V_SMALLER);
X  	reset_blks();
X  	cnt = 0;		/* cnt keep track of the zone's */
X! 	while (cnt < V_SMALLER) {
X  		int tst;
X  
X  		if (interactive)
X***************
X*** 356,366 ****
X    int i;
X  
X    if (nr_blocks == 0) return;
X!   for (i = 0; i < nr_blocks; i++) {
X! 	set_bit(block[i]);
X! 	ip->i_zone[i] = block[i];
X    }
X!   ip->i_size = (long) (BLOCK_SIZE * nr_blocks);	/* give file size */
X    put_inode(&stat_buf);		/* save the inode on disk */
X    put_super();			/* bit_maps too */
X  }
X--- 388,411 ----
X    int i;
X  
X    if (nr_blocks == 0) return;
X!   if (v1fs) {
X! 	/* This is a V1 file system. */
X! 	for (i = 0; i < nr_blocks; i++) {
X! 		set_bit(block[i]);
X! 		ip1->d1_zone[i] = block[i];
X! 	}
X!   } else {
X! 	/* This is a V2 file system. */
X! 	for (i = 0; i < nr_blocks; i++) {
X! 		set_bit(block[i]);
X! 		ip2->d2_zone[i] = block[i];
X! 	}
X    }
X!   if (v1fs)
X! 	ip1->d1_size = (long) (BLOCK_SIZE * nr_blocks);	/* give file size */
X!   else
X! 	ip2->d2_size = (long) (BLOCK_SIZE * nr_blocks);	/* give file size */
X! 
X    put_inode(&stat_buf);		/* save the inode on disk */
X    put_super();			/* bit_maps too */
X  }
X***************
X*** 378,384 ****
X  {
X    int i;
X  
X!   for (i = 0; i <= NR_DZONE_NUMS; i++)
X  	block[i] = 0;		/* Note: Last block_number is set to zero */
X    blk_cnt = 0;
X  }
X--- 423,429 ----
X  {
X    int i;
X  
X!   for (i = 0; i <= V_NR_DZONES; i++)
X  	block[i] = 0;		/* Note: Last block_number is set to zero */
X    blk_cnt = 0;
X  }
X***************
X*** 387,393 ****
X  {
X    int i;
X  
X!   for (i = 0; i < blk_cnt; i++) printf("Block[%d] = %d\n", i, block[i]);
X  }
X  
X  blk_is_used(blk_num)
X--- 432,438 ----
X  {
X    int i;
X  
X!   for (i = 0; i < blk_cnt; i++) printf("Block[%d] = %ld\n", i, block[i]);
X  }
X  
X  blk_is_used(blk_num)
X***************
X*** 430,436 ****
X    offset = z_num - (block << BIT_MAP_SHIFT);	/* offset */
X    words = z_num / INT_BITS;	/* which word */
X  
X!   blk_offset = (long) (2 + sp->s_imap_block);	/* zone map */
X    blk_offset *= (long) BLOCK_SIZE;	/* of course in block */
X    blk_offset += (long) (words * SIZE_OF_INT);	/* offset */
X  
X--- 475,481 ----
X    offset = z_num - (block << BIT_MAP_SHIFT);	/* offset */
X    words = z_num / INT_BITS;	/* which word */
X  
X!   blk_offset = (long) (2 + sp->s_imap_blocks);	/* zone map */
X    blk_offset *= (long) BLOCK_SIZE;	/* of course in block */
X    blk_offset += (long) (words * SIZE_OF_INT);	/* offset */
X  
X***************
X*** 457,463 ****
X  }
X  
X  set_bit(num)			/* write in the bitmap */
X! unsigned num;
X  {
X    int rwd;
X    long blk_offset;
X--- 502,508 ----
X  }
X  
X  set_bit(num)			/* write in the bitmap */
X! zone_t num;
X  {
X    int rwd;
X    long blk_offset;
X***************
X*** 465,477 ****
X    unsigned z_num;
X    char wrd_str[17], bit_str[17];
X  
X!   z_num = num + 1 - sp->s_firstdatazone;
X  
X    block = z_num >> BIT_MAP_SHIFT;	/* which block */
X    offset = z_num - (block << BIT_MAP_SHIFT);	/* offset in block */
X!   words = offset / INT_BITS;	/* which word */
X  
X!   blk_offset = (long) (2 + sp->s_imap_block);
X    blk_offset *= (long) BLOCK_SIZE;
X    blk_offset += (long) (words * SIZE_OF_INT);
X  
X--- 510,522 ----
X    unsigned z_num;
X    char wrd_str[17], bit_str[17];
X  
X!   z_num = num - (sp->s_firstdatazone - 1);
X  
X    block = z_num >> BIT_MAP_SHIFT;	/* which block */
X    offset = z_num - (block << BIT_MAP_SHIFT);	/* offset in block */
X!   words = z_num / INT_BITS;	/* which word */
X  
X!   blk_offset = (long) (2 + sp->s_imap_blocks);
X    blk_offset *= (long) BLOCK_SIZE;
X    blk_offset += (long) (words * SIZE_OF_INT);
X  
/