APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

bfind.c for SCO Unix 3.2v4.x

This was useful when the OS reported a bad disk block - you could track down what file used that block with this (knowing what file was damaged is obviously useful as to what needs to be restored once the disk problem is fixed.)

You coukd rewrite this for any fs, but it has become redundant and mostly unneeded.

If you are running 3.2v5.x, see man badblk - DO NOT USE THIS!!

See radek Tomis's fsrd utility, which can perform these functions for XENIX, S51K, ES51K, AFS, EAFS or HTFS.

For Linux, it is the -c option to fsck (for ext2/3) that will add a call to "badblocks".

I found findblk, which is similar to the "bfind" here and this Bad Block How TO gives an example of using debugfs to trace a block number back to an inode.


/* bfind.c finds the inode for a given disk block
   or prints block numbers for an inode (-i) (pretty print)
   or prints block numbers for an inode (-l) (simple list)
   (C) October 1993 Anthony Lawrence
(781) 784-7547
*/
#include <stdio.h>  
#include <sys/types.h>
#include <sys/fs/s5param.h>
#include <sys/fs/s5filsys.h>
#include <sys/ino.h>

char SCCSID[]="@(#) bfind.c 1.6 (C) 10/14/93 Lawrence & Clark, Inc";

int inum=0;
int Blocksize=1024; /* default, reset later if necessary */
int pblocks=0; /* default action, looking for blocks, not inodes */
char lister=' ';  /* -i or -l */
int level=0; /* used for tabbing in on -l */
int alevel=0; /* used to determine depth of indirection (see below) */

main(int argc,char **argv)
 {
 struct filsys buf;
 struct dinode node;
 FILE *dsk;
 struct filsys *d=&buf;
 struct dinode *i=&node;
 int x,start_at;
 ulong num_inodes;
 unsigned long wants;

 if (argc < 3)
  {
  printf("bfind filesys block-no\n)");
  exit(1);
  }
 wants=atol(argv[2]);
 
 if ((dsk=fopen(argv[1],"r"))== NULL) 
  {
  printf("bfind filesys block-no\n)");
  exit(1);
  }
 fseek(dsk,512,SEEK_SET);
 fread(d,1,512,dsk);
/* Checking to be sure this is a filesystem that we know how
   to handle, etc.  See /usr/include/sys/filesys.h */
 if (d->s_magic != FsMAGIC && d->s_magic != FsEMAGIC)
   {
   fprintf(stderr,"(%x) Unknown File System: can't.\n",d->s_magic);
   exit(0);
   }
 if (d->s_fsize <=0 || d->s_isize > d->s_fsize)
   {
   fprintf(stderr,"Corrupted File System: can't.\n",d->s_magic);
   exit(0);
   }
 if (d->s_state+(long)d->s_time != FsOKAY && d->s_state != FsACTIVE)
   {
   fprintf(stderr,"State: (%x) Unclean or corrupted File System: can't.\n",d->s_state);
   exit(0);
   }

 switch(d->s_type)
   {
   case 1:
      Blocksize=512;break;
   case 2:
      Blocksize=1024;break;
   case 3:
      Blocksize=2048;break;
   default :
      fprintf(stderr,"I don't know this type of filesystem %d\n",d->s_type);
      exit(0);
   }
 num_inodes=Blocksize * (d->s_isize-2)/(sizeof(node));  
 printf("%u %u byte inodes, %u block-size\n",num_inodes,sizeof(struct dinode),Blocksize);
 fseek(dsk,3 * 512,SEEK_SET);
 fread(d,1,512,dsk);
 fread(i,1,sizeof(node),dsk);
 
 start_at=0;
 if (argc > 3) /* -i or -l */
   {
   start_at=atoi(argv[3])-2;
   lister=argv[2][1];
   if (start_at > num_inodes+2)
     {
     fprintf(stderr,"Too high inum %u only %u\n",start_at,num_inodes);
     exit(1);
     }
   fseek(dsk,start_at * sizeof(node),SEEK_CUR);
   pblocks=1;  
   }
 
 for (x=start_at ; x < num_inodes-2 ; x++)
  { 
  int deep;
  inum=x+2; /* first inum is 2, but we are indexing from 0 */
  if (pblocks)
   printf("Checking Inode %d\n",inum);
  fread(i,1,sizeof(node),dsk);
  if (i->di_size) /* if this is zero, we have a "special" file: no blocks */
   {
     /* There are 13 sets of 3 byte integers that hold the 
        disk block #'s.  The first 10 (0-9) are direct blocks
        (file data will be found in the blocks), the 11th is
        indirect (it points to direct blocks that point to data)
        and the 12th is Double Indirect (it points to indirect
        blocks that point to direct blocks that point to data).
        The 13th is Triple Indirect (points to double indirect
        which... as usual)

        There are undoubtedly easier ways to extract these numbers,
        but the method below shows (I think!) what's really there.
      */
    for (deep=0; deep < 39; deep += 3)
     {
     unsigned long z;
     z= i->di_addr[deep+0] & 0xff;
     z +=  (i->di_addr[deep+1] << 8) & 0xff00;
     z +=  (i->di_addr[deep+2] << 16) & 0xff0000;
     if (pblocks)
        do_blocks(z,deep);
     if (z == wants && z)  /* wants is zero if pblock is true */
      {
      printf("%lu is inode %d \n",z,inum);
      fclose(dsk);
      exit(0);
      /* found it and done */
      }
      if (deep > 27 && z ) /* indirect block */
        {
        level++;
        alevel=deep/3;
        descend(dsk,deep,wants,z);
        level--;
        }
      }
    }
  if (pblocks) /* done, but may need flushing */
    {
    do_blocks(0,0); /* The zero will force a flush */
    printf("\n");
    exit(0);
    }
   }
 }
 
 int descend(FILE *dsk, int deep, unsigned long wants,unsigned long now)
   {
   unsigned long cpos=ftell(dsk);
   unsigned long z;
   int x=Blocksize / sizeof(long);
   int y=0;
     
   
   fseek(dsk,now * Blocksize,SEEK_SET);
   for ( y=0; y < x; y++)
      {
      fread((char *) &z,sizeof(long),1,dsk);
      if (pblocks)  
        do_blocks(z,deep);
      if (z == wants && z)
        {
        printf("%lu is inode %d \n",z,inum);
        fclose(dsk);
        exit(0);
        }
     if (deep > 30 && z)
       {
       level++;
       alevel=deep/3-1;
       descend( dsk, deep - 3, wants, z);
       level--;
       }
      }
 
   fseek(dsk,cpos,SEEK_SET);
   return(0);
   }

int do_blocks(unsigned z,int depth)
{
static int reached_depth=0;
static int in_range=0;
static int in_zero=0;
static int zeros=0;
static ulong last_block=0;
int tabs;

if (!z)
     {
     zeros++;
     in_zero++;
     }
/* Above counts blocks pointing to 0.  Could be 'holes', so we
   want to print them if they are.
   Below looks to see if we have been counting zeros */
 
if (z && in_zero)      
  {
  printf("\n %d Zero Blocks (holes)\n",zeros);
  in_zero=0;
  zeros=0;
  }

if (lister=='l' && z) /* straight list of blocks */
     {
     for (tabs=0; tabs < level; tabs++)
        printf("   ");
     printf("%u\n",z);
     }
if (lister=='i') /* prettied list of blocks */
  {
  if (alevel > 9)  
  {
  if (in_range)
     {
     printf("- %u ",last_block);
     in_range=0;
     }
    switch(alevel)
      {
      case 10: printf("\n(Indirect) %u\n",last_block);break;
      case 11: printf("\n(Double Indirect) %u\n",last_block);break;
      case 12: printf("\n(Triple Indirect) %u\n",last_block);break;
      }
  last_block=z;
  alevel--;
  printf("%u ",z);
  return(0);
  }
  if (z == last_block+1)
     {
     in_range=1;
     last_block=z;
     return(0);
     }
  if (!in_range && z)
     {
     printf("\n%u ",z);
     last_block=z;
     return(0);
     }
  if (in_range)
     {
     printf("- %u",last_block);
     last_block=z;
     in_range=0;
     printf("\n%u ",z);
     }
  }
}




Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Unix Programs: BFIND.C




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





An adversary capable of implanting the right virus or accessing the right terminal can cause massive damage. (George Tenet, director of the U.S. Central Intelligence Agency)

If you ask "Should we be in space?" you ask a nonsense question. We are in space. We will be in space. (Frank Herbert)












This post tagged: