How to determine which file/inode occupies a given sector

Solution 1:

The rough idea would be to

  1. do some calculations to find out the filesytem's block number based on the physical sector number
  2. use debugfs testb/ncheck/icheck commands to find out if the block is in use and the name of the file(s) that are using it

There is a bad block HOWTO over at the smartmontools project site describing the process in more detail.

Solution 2:

  1. Find which partition the sector is in by running fdisk -lu /dev/sdb. Suppose it is "sdb2" which starts at sector 45612307.

  2. Subtract that from 95891008 to get 50278701.

  3. Next determine how many sectors per block: tune2fs -l /dev/sdb2 | grep Block. Suppose it is 4096.

  4. Calculate the block / sector ratio: 512 bytes/sector / 4096 bytes/block = 0.125 blocks/sector.

  5. Calculate the block from the sector: 50278701 * 0.125 = 6284837.625.

  6. Use debugfs to find out which is using the block. If icheck 6284837 returns inode 12345 then run ncheck 12345.

Caveats: You may need to turn off journaling. This may not work with ext4.