Identifying file associated with unreadable disk sector

THIS DOCUMENT SHOWS HOW TO IDENTIFY THE FILE ASSOCIATED WITH AN
UNREADABLE DISK SECTOR, AND HOW TO FORCE THAT SECTOR TO REALLOCATE.

Assumptions: Linux OS, ext2 or ext3 file system.

Bruce Allen <smartmontools-support@lists.sourceforge.net>

This document is version $Id: BadBlockHowTo.txt,v 1.4 2004/03/21 20:38:32 ballen4705 Exp $
It is Copyright Bruce Allen (2004) and distributed under GPL2.


Thanks to Sergey Vlasov, Theodore Ts'o, Michael Bendzick, and others
for explaining this to me. I would like to add text showing how to do
this for other file systems, in particular ReiserFS, XFS, and JFS:
please email me if you can provide this information.


In this example, the disk is failing self-tests at Logical Block
Address LBA = 0x016561e9 = 23421417. The LBA counts sectors in units
of 512 bytes, and starts at zero.

-----------------------------------------------------------------------------------------------
root]# smartctl -l selftest /dev/hda:

SMART Self-test log structure revision number 1
Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error
# 1 Extended offline Completed: read failure 90% 217 0x016561e9
-----------------------------------------------------------------------------------------------

Note that other signs that there is a bad sector on the disk can be
found in the non-zero value of the Current Pending Sector count:
-----------------------------------------------------------------------------------------------
root]# smartctl -A /dev/hda
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 0
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 0
197 Current_Pending_Sector 0x0022 100 100 000 Old_age Always - 1
198 Offline_Uncorrectable 0x0008 100 100 000 Old_age Offline - 1
-----------------------------------------------------------------------------------------------

First Step: We need to locate the partition on which this sector of
the disk lives:
-----------------------------------------------------------------------------------------------
root]# fdisk -lu /dev/hda

Disk /dev/hda: 123.5 GB, 123522416640 bytes
255 heads, 63 sectors/track, 15017 cylinders, total 241254720 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 63 4209029 2104483+ 83 Linux
/dev/hda2 4209030 5269319 530145 82 Linux swap
/dev/hda3 5269320 238227884 116479282+ 83 Linux
/dev/hda4 238227885 241248104 1510110 83 Linux
-----------------------------------------------------------------------------------------------

The partition /dev/hda3 starts at LBA 5269320 and extends past the
'problem' LBA. The 'problem' LBA is offset 23421417 - 5269320 =
18152097 sectors into the partition /dev/hda3.

To verify the type of the file system and the mount point, look in
/etc/fstab:
-----------------------------------------------------------------------------------------------
root]# grep hda3 /etc/fstab
/dev/hda3 /data ext2 defaults 1 2
-----------------------------------------------------------------------------------------------
You can see that this is an ext2 file system, mounted at /data.

Second Step: we need to find the blocksize of the file system
(normally 4096 bytes for ext2):
-----------------------------------------------------------------------------------------------
root]# tune2fs -l /dev/hda3 | grep Block
Block count: 29119820
Block size: 4096
-----------------------------------------------------------------------------------------------
In this case the block size is 4096 bytes.

Third Step: we need to determine which File System Block contains this
LBA. The formula is:
b = (int)((L-S)*512/B)
where:
b = File System block number
B = File system block size in bytes
L = LBA of bad sector
S = Starting sector of partition as shown by fdisk -lu
and (int) denotes the integer part.

In our example, L=23421417, S=5269320, and B=4096. Hence the
'problem' LBA is in block number
b = (int)18152097*512/4096 = (int)2269012.125
so b=2269012.

Note: the fractional part of 0.125 indicates that this problem LBA is
actually the second of the eight sectors that make up this file system
block.

Fourth Step: we use debugfs to locate the inode stored in this block,
and the file that contains that inode:
-----------------------------------------------------------------------------------------------
root]# debugfs
debugfs 1.32 (09-Nov-2002)
debugfs: open /dev/hda3
debugfs: icheck 2269012
Block Inode number
2269012 41032
debugfs: ncheck 41032
Inode Pathname
41032 /S1/R/H/714197568-714203359/H-R-714202192-16.gwf
-----------------------------------------------------------------------------------------------

In this example, you can see that the problematic file (with the mount
point included in the path) is:
/data/S1/R/H/714197568-714203359/H-R-714202192-16.gwf


To force the disk to reallocate this bad block we'll write zeros to
the bad block, and sync the disk:
-----------------------------------------------------------------------------------------------
root]# dd if=/dev/zero of=/dev/hda3 bs=4096 count=1 seek=2269012
root]# sync
-----------------------------------------------------------------------------------------------

NOTE: THIS LAST STEP HAS PERMANENTLY AND IRRETREVIABLY DESTROYED SOME
OF THE DATA THAT WAS IN THIS FILE. DON'T DO THIS UNLESS YOU DON'T
NEED THE FILE OR YOU CAN REPLACE IT WITH A FRESH OR CORRECT VERSION.


Now everything is back to normal: the sector has been reallocated.
Compare the output just below to similar output near the top of this
article:
-----------------------------------------------------------------------------------------------
root]# smartctl -A /dev/hda
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 1
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 1
197 Current_Pending_Sector 0x0022 100 100 000 Old_age Always - 0
198 Offline_Uncorrectable 0x0008 100 100 000 Old_age Offline - 1
-----------------------------------------------------------------------------------------------

Note: for some disks it may be necessary to update the SMART Attribute values by using
smartctl -t offline /dev/hda

The disk now passes its self-tests again:

-----------------------------------------------------------------------------------------------
root]# smartctl -t long /dev/hda [wait until test completes, then]
root]# smartctl -l selftest /dev/hda

SMART Self-test log structure revision number 1
Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error
# 1 Extended offline Completed without error 00% 239 -
# 2 Extended offline Completed: read failure 90% 217 0x016561e9
# 3 Extended offline Completed: read failure 90% 212 0x016561e9
# 4 Extended offline Completed: read failure 90% 181 0x016561e9
# 5 Extended offline Completed without error 00% 14 -
# 6 Extended offline Completed without error 00% 4 -
-----------------------------------------------------------------------------------------------

and no longer shows any offline uncorrectable sectors:

-----------------------------------------------------------------------------------------------
root]# smartctl -A /dev/hda
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 1
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 1
197 Current_Pending_Sector 0x0022 100 100 000 Old_age Always - 0
198 Offline_Uncorrectable 0x0008 100 100 000 Old_age Offline - 0
-----------------------------------------------------------------------------------------------



A SECOND EXAMPLE

On this drive, the first sign of trouble was this email from smartd:

To: ballen
Subject: SMART error (selftest) detected on host: medusa-slave166.medusa.phys.uwm.edu

This email was generated by the smartd daemon running on host:
medusa-slave166.medusa.phys.uwm.edu in the domain: master001-nis

The following warning/error was logged by the smartd daemon:
Device: /dev/hda, Self-Test Log error count increased from 0 to 1


Running smartctl -a /dev/hda confirmed the problem:

Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error
# 1 Extended offline Completed: read failure 80% 682 0x021d9f44

Note that the failing LBA reported is 0x021d9f44 (base 16) = 35495748 (base 10)

ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 0
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 0
197 Current_Pending_Sector 0x0022 100 100 000 Old_age Always - 3
198 Offline_Uncorrectable 0x0008 100 100 000 Old_age Offline - 3

and one can see above that there are 3 sectors on the list of pending
sectors that the disk can't read but would like to reallocate.

The device also shows errors in the SMART error log:

Error 212 occurred at disk power-on lifetime: 690 hours
After command completion occurred, registers were:
ER ST SC SN CL CH DH
-- -- -- -- -- -- --
40 51 12 46 9f 1d e2 Error: UNC 18 sectors at LBA = 0x021d9f46 = 35495750

Commands leading to the command that caused the error were:
CR FR SC SN CL CH DH DC Timestamp Command/Feature_Name
-- -- -- -- -- -- -- -- --------- --------------------
25 00 12 46 9f 1d e0 00 2485545.000 READ DMA EXT

Signs of trouble at this LBA may also be found in SYSLOG:

[root]# grep LBA /var/log/messages | awk '{print $12}' | sort | uniq
LBAsect=35495748
LBAsect=35495750

So I decide to do a quick check to see how many bad sectors there
really are. Using the bash shell I check 70 sectors around the trouble
area:

[root]# export i=35495730
[root]# while [ $i -lt 35495800 ]
> do echo $i
> dd if=/dev/hda of=/dev/null bs=512 count=1 skip=$i
> let i+=1
> done

<SNIP>

35495734
1+0 records in
1+0 records out
35495735
dd: reading `/dev/hda': Input/output error
0+0 records in
0+0 records out

<SNIP>

35495751
dd: reading `/dev/hda': Input/output error
0+0 records in
0+0 records out
35495752
1+0 records in
1+0 records out

<SNIP>

which shows that the seventeen sectors 35495735-35495751 (inclusive)
are not readable.

Next, we identify the files at those locations. The partitioning
information on this disk is identical to the first example above, and
as in that case the problem sectors are on the third partition
/dev/hda3. So we have:
L=35495735 to 35495751
S=5269320
B=4096
so that b=3778301 to 3778303 are the three bad blocks in the file
system.

[root]# debugfs
debugfs 1.32 (09-Nov-2002)
debugfs: open /dev/hda3
debugfs: icheck 3778301
Block Inode number
3778301 45192
debugfs: icheck 3778302
Block Inode number
3778302 45192
debugfs: icheck 3778303
Block Inode number
3778303 45192
debugfs: ncheck 45192
Inode Pathname
45192 /S1/R/H/714979488-714985279/H-R-714979984-16.gwf
debugfs: quit

And finally, just to confirm that this is really the damaged file:

[root]# md5sum /data/S1/R/H/714979488-714985279/H-R-714979984-16.gwf
md5sum: /data/S1/R/H/714979488-714985279/H-R-714979984-16.gwf: Input/output error

Finally we force the disk to reallocate the three bad blocks:
[root]# dd if=/dev/zero of=/dev/hda3 bs=4096 count=3 seek=3778301
[root]# sync

We could also probably use:
[root]# dd if=/dev/zero of=/dev/hda bs=512 count=17 seek=35495735

At this point we now have:
ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 005 Pre-fail Always - 0
196 Reallocated_Event_Count 0x0032 100 100 000 Old_age Always - 0
197 Current_Pending_Sector 0x0022 100 100 000 Old_age Always - 0
198 Offline_Uncorrectable 0x0008 100 100 000 Old_age Offline - 0

which is encouraging, since the pending sectors count is now zero.
Note that the drive reallocation count has not yet increased: the
drive may now have confidence in these sectors and have decided not to
reallocate them..

A device self test:
[root#] smartctl -t long /dev/hda
(then wait about an hour) shows no unreadable sectors or errors:

Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error
# 1 Extended offline Completed without error 00% 692 -
# 2 Extended offline Completed: read failure 80% 682 0x021d9f44

This document is version $Id: BadBlockHowTo.txt,v 1.4 2004/03/21 20:38:32 ballen4705 Exp $
It is Copyright Bruce Allen (2004) and distributed under GPL2.



 


Trackback

Trackback URL for this entry:
http://www.gra2.com/trackback.php/20041015232512624

Here's what others have to say about 'Identifying file associated with unreadable disk sector':

Applepie Solutions Weblog &raquo; Blog Archive &raquo; I/O error, dev hdb, sector 92813943
Tracked on Friday, September 28 2007 @ 06:59 PM CEST

Comments

Post a comment

Hi, can you please tell me if this procedure actually repairs the hd, I mean if it is reformated and a new OS which is can be windows is installed in, it will still have no problems?

Anonymous on Friday, December 09 2005 @ 01:13 PM CET Reply | #

Following this howto will allow you to identify bad blocks on an HD, and writing zeroes in the sector as explained will force the system to reallocate the damaged sector, so you can still use the disk despite of having some bad blocks.

I'm not sure if doing this will help you in installing windows. I mean, this does not repair the disk, it only "marks" the bad blocks as unusable (more or less). This is useful for fixing a linux partition without reinstalling.

Anyway, when you install a new system, Linux, Windows or whatever, the installation procedure usually checks for bad blocks when partitioning, so there's no need to do it manually. It should mark as unusable your bad blocks.

Regards,
Daniel.

---
And love, is not the easy thing, the only baggage you can bring, is all
that you can't leave behind.
- Bono, The Edge, Adam Clayton and Larry Mullen

daniel on Friday, December 09 2005 @ 10:45 PM CET Reply | #

Just wanted to say Hello to everyone.
Much to read and learn here, I'm sure I will enjoy !

Anonymous on Tuesday, March 18 2008 @ 12:31 AM CET Reply | #

Surprsiingly well-written and informative for a free online article.

Sharky on Saturday, December 17 2011 @ 06:34 PM CET Reply | #

No qeutsion this is the place to get this info, thanks y'all.

Libby on Saturday, December 17 2011 @ 06:49 PM CET Reply | #

Very useful, Thank You. Succesfully located the file and it was non-critical so I simply renamed (mv'd) it with .bad.sector at the end of the filename ;) I have also started making (more) backups ;) I'm not very experienced with shell programming. It would be cool if there was a script that could be run to perform many of the steps automatically. Or is that left as an exercise for the reader? LOL Thanks Again Marty

Anonymous on Monday, September 08 2008 @ 12:57 AM CEST Reply | #

I also agree..

Anonymous on Sunday, January 04 2009 @ 08:00 AM CET Reply | #

Hey, you have a great blog here! I'm definitely going to bookmark you!

Anonymous on Sunday, January 04 2009 @ 07:21 PM CET Reply | #

I also agree..

Anonymous on Sunday, January 04 2009 @ 07:35 PM CET Reply | #

Hey, you have a great blog here! I'm definitely going to bookmark you!

Anonymous on Sunday, January 04 2009 @ 07:48 PM CET Reply | #

Hey, I was searching blogs, and came onto yours, and I like it.

Anonymous on Thursday, January 08 2009 @ 09:00 PM CET Reply | #

Hello, you have a great blog here! I'm definitely going to bookmark you!

Anonymous on Thursday, January 08 2009 @ 09:10 PM CET Reply | #

Nice post.

Anonymous on Thursday, January 08 2009 @ 09:33 PM CET Reply | #

Excellent post.. a great reference. It's always challenging to remember how to perform good sector/block arithmetic

Anonymous on Friday, August 14 2009 @ 11:39 PM CEST Reply | #

j'essc3a9ye de m'accroche en pensent trc3a9s fort que tout dsiesns des dessinateur m'aideramais je suis sur que personne me connais physiquementen souhaitent allc3a9 a angoulc3a9me c'etait pour solidifier notres amitier les dessin me fond rc3a9c3a9lc3a9ment tc3a9nir physiquementcar sa me donne de compter etre protc3a9ger

Tiago on Tuesday, April 17 2012 @ 09:05 PM CEST Reply | #

QSpzId Gra7noI59Unral92Bb7wf

daria on Wednesday, August 19 2009 @ 03:37 AM CEST Reply | #

asfdasf

Anonymous on Wednesday, February 23 2011 @ 03:17 PM CET Reply | #

Hallelujah! I ndeeed this-you're my savior.

Louise on Sunday, December 18 2011 @ 09:22 AM CET Reply | #

DyAatI I came across to your post and found it very useful for me. Thanks and good luck!)

Treadmills for Sale on Friday, July 08 2011 @ 04:58 AM CEST Reply | #

HzOO3o I read online (computer problems) positive feedback about your resource. Didnt even believe it, and now saw myself. It turned out that I was not fooled.

Treadmills for Sale on Friday, July 08 2011 @ 08:31 PM CEST Reply | #

e2V21X Sometimes I also see something like this, but earlier I didn`t pay much attention to this!...

Buy cheap software on Thursday, September 29 2011 @ 01:09 PM CEST Reply | #

Yo me voy a mojar, pero antes de entrar en la "porra", me gatusreda decir un par de cositas, porque algunos estais cargando sin razf3n,bajo mi punto de vista, a la organizacif3n de los premios.A los que deceds que falta la categoreda de deportes, os digo que tambien falta la categoreda de ciencia, de sexo, Literatura, etc...Pero los que habeis inscrito los podcast sabiais lo que habeda no? La mejor manera de protestar es no presentar al podcast, y que la organizacif3n se de cuenta de que algo se este1 haciendo mal.Nada me1s que salen las protestas cuando llegan los premios, pero antes, teneis TODOS la boca callada, una actitud muy cobarde bajo mi punto de vista.Decir que no tenga nada que ver ni con la Asociacif3n y con la organizacif3n de las Jpod11, y a mi podcast (La Buhardilla 2.0) es uno a los que ha afectado la reduccif3n de categoreda (Ciencia) y este af1o hemos decidido participar ACATANDO las bases, no echando mierda a posteriori. La reduccif3n es algo normal. Cuantos podcast hay de ciencia? tres? Y de deportes? cuatro? y de sexo? dos?.Despue9s de soltar esto, voy con mi "porra":Cultura--> El podcast del BuhoAudioVisual-->TeleadictosEntretenimiento--> Gravina82Magazine-->KafelogActualidad-->Dame La VozTecnologeda-->iCharlasMejor Podcaster Femenina-->Tamara Leon (Trending Podcast)Mejor Podcaster Masculino--> Daniel Rodriguez (Teleadictos)Mejor Edicif3n y Montaje--> Los DankoPremio del pfablico--> Ni puta ideaUn saludo

Victoria on Wednesday, April 18 2012 @ 09:33 PM CEST Reply | #

AF6zud i bet this will b most commented

bmezine sarah strattera on Wednesday, January 04 2012 @ 05:35 PM CET Reply | #

w0w this is good

80710723 on Sunday, January 29 2012 @ 01:01 AM CET Reply | #

takw it back!

windows xp doesnt detect dvdrom on Monday, January 30 2012 @ 07:47 AM CET Reply | #

this helped me as hell

buy Acomplia on Wednesday, February 01 2012 @ 04:27 AM CET Reply | #

sweet post

pozycjonowanie stron on Wednesday, February 01 2012 @ 11:05 AM CET Reply | #

Yes, Zapatero is a disgrace for every Spaniard, he's not only a caorwd but also a terrorist, his words are nothing but surrender and justification for the terrorists. But what it's worse is the huge amount of brainless extreme lefties in Spain that support Palestinian and arab terrorists, even though they (the terrorist) would kill the lefties too if they had any chance. Fortunately there are also other people in the country who support Danmark, US, Israel and the free world. We still have the hope of a change of government in the next poll.

Gather on Tuesday, April 17 2012 @ 08:48 PM CEST Reply | #

good seo here

pozycjonowanie stron on Wednesday, February 01 2012 @ 10:40 PM CET Reply | #

sweet template

wypadki on Thursday, February 02 2012 @ 12:05 AM CET Reply | #

nUUGTW omg this is dopeeeeeeee

playstation3 error on Thursday, February 02 2012 @ 03:38 AM CET Reply | #

16UJxg thissi sad.. maybe not..

cyprus on Saturday, February 04 2012 @ 09:19 AM CET Reply | #

Search



About

newton.gra2.com is a blog about technology, opinion and random thoughts written by Daniel Alvarez, a computer engineer currently living in Zurich, Switzerland.

Topics

News (20/0)
Manuals (24/0)
Security (7/0)
Music (3/0)
Weeklog (1/0)
Personal (34/0)
Photos (3/0)
Opinion (14/0)
Windows (5/0)

Blogroll

Pros i contres (Jordi)
Entrepa de fusta (Oriol)
Spaghetti Code (Isaac)
Made in net (Eric)
Nogare (Juan)
Blog de Isaac Jimenez
Web d'en Jaume Benet
Montcada Wireless (Fran)
Blog d'en Ricard Forniol
Angela Fabregues
in.solit.us

Libertad Digital
FOX News
The Wall Street Journal
The Washington Times
The Jerusalem Post

Michelle Malkin
Eurabian News
Nihil Obstat
Barcepundit
Expose the left
Davids Medienkritik
Johan Norberg
Ayaan Hirsi Ali

User Functions

:

:


Lost your password?

Latest posts

Stories

No new stories

Comments last 2 days

No new comments

Trackbacks last 2 days

No new trackback comments

Links last 2 weeks

No recent new links