[clue-tech] filesystem: which sector is file x on?

Dennis J Perkins dperkins at frii.com
Wed Mar 7 20:53:58 MST 2007


On Wed, 2007-03-07 at 18:59 -0700, Jim Ockers wrote:
> Hi,
> 
> So someone asked me today how to tell which file is on a given
> sector of a disk.  For instance if the kernel is throwing bad 
> sector errors with sector number 1234567, how could we tell what
> file in a filesystem is likely to be affected.
> 
> I know there are lots of things to consider so let's assume the
> following:
> 
> - IDE hard drive
> - Standard IDE controller
> - Generic IDE driver
> - ext2 filesystem, 4k blocksize
> 
> So my guess is that block device drivers make all block devices 
> appear be n consecutive blocks, all error free.
> 
> I think the drive does all the sector stuff within the drive and
> there is no way to tell what sector a particular block (or file)
> is on.  I told him this might be like trying to get a tape drive
> to tell you how many inches of tape were spooled on each spool
> for a paritcular bunch of bits on the tape.
> 
> But maybe someone out there has some idea how?  Also maybe SCSI is
> different from IDE?
> 
> This blog post states: http://prefetch.net/blog/index.php/category/linux-storage/
> 
> "all modern disk drive controllers contain logic to remap faulty sectors when they are detected"
> 
> So maybe the info is in the controller?  Does that mean the drive
> electronics?
> 
> Thanks,
> Jim
> 

It's been 14 years since I have worked on hard drives but I don't think
much of the basic structure has changed since then.

The information is stored on the disk.  The smallest unit the drive
controller deals with are sectors.  The beginning of each track has a
special marker, let's call it BOT (beginning of track, but I forget the
real name), that is guaranteed never to occur anywhere else and is used
to synchonize the drive electronics with the data.  Each sector has a
sector header that contains the track number and the sector number, and
some additional info, and a checksum.  The sector header is only used by
the drive.  The OS knows nothing about it.

The sector size depends on the drive capacity; the larger the capacity,
the bigger the sector.  The OS might group sectors into blocks.

If a sector is bad, it can be flagged as unusable.  Somre drives are
probably shipped with a few bad sectors that are flagged.  This is due
to small manufacturing defects.  There might be a few tracks that are
spares, but I don't know how modern drives handle this.  At one time it
was possible to use a utility to flag additional sectors, if necessary,
but I think they discouraged people from using it.

I think modern drives hide some of the complexity, such as heads and
tracks, from the OS, and simply presents itself as a linear set of
sectors.  So you could have a sector error, but be unable to identify
the track and surface.  Flagging it might mean a spare sector is
assigned the same sector number.  A defect on a disk surface should
probably cause a set of errors grouped together.


Linux uses inodes and tables to organize the sectors into something
useful and easy to use.  Inodes contain the filename, permissions, and
other file metadata.  If I recall correctly, it also contains pointers
to the first few data blocks of the file.  As the file grows, a pointer
points to a block that contains pointers to other data blocks, allowing
the file to grow.  Some Unix/Linux filesystems can run out of inodes.






More information about the clue-tech mailing list