Skip to contentSkip to conversation

libata: Don't trust current capacity values in identify words 57-58

PATCH Superseded Robert Hancock 3 participants 10 messages Feb 16, 2009 – Mar 3, 2009 libata

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libata: Don't trust current capacity values in identify words 57-58

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older ATA specs were vague about what this field should contain and a number of drives used values with wrong byte order or that were totally bogus. There's no unique information that it conveys and so we can just calculate the number of sectors from the reported current CHS values.

---

Should likely go into -next for testing for a while. Of course, the fact that this problem showed up in the first place shows that the number of people that test libata with such boat anchors is fairly small :-)

drivers/ata/libata-core.c +1 -1
index 9fbf059..5389cbc 100644
@@ -1327,7 +1327,7 @@ static u64 ata_id_n_sectors(const u16 *id)
 			return ata_id_u32(id, 60);
 	} else {
 		if (ata_id_current_chs_valid(id))
-			return ata_id_u32(id, 57);
+			return id[54] * id[55] * id[56];
 		else
 			return id[1] * id[3] * id[6];
 	}
#1 lore raw
Quoted 12 lines · Robert Hancock

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

Quoted 7 lines

---

Should likely go into -next for testing for a while. Of course, the fact that this problem showed up in the first place shows that the number of people that test libata with such boat anchors is fairly small :-)

It's been tested well enough for years in the IDE core I think.

drivers/ata/libata-core.c static u64 ata_id_n_sectors(const u16 *id) · line 1330

Quoted 10 lines
drivers/ata/libata-core.c +1 -1
index 9fbf059..5389cbc 100644
@@ -1327,7 +1327,7 @@ static u64 ata_id_n_sectors(const u16 *id)
 			return ata_id_u32(id, 60);
 	} else {
 		if (ata_id_current_chs_valid(id))
-			return ata_id_u32(id, 57);
+			return id[54] * id[55] * id[56];

We have mnemonics for these 3 words in <linux/ata.h>...

MBR, Sergei

#2 lore raw

PATCH v2 1/1 libata: Don't trust current capacity values in identify words 57-58

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older ATA specs were vague about what this field should contain and a number of drives used values with wrong byte order or that were totally bogus. There's no unique information that it conveys and so we can just calculate the number of sectors from the reported current CHS values.

While we're at it, clean up this function to use named constants for the identify word values.

drivers/ata/libata-core.c +6 -4
index 9fbf059..33b5549 100644
@@ -1322,14 +1322,16 @@ static u64 ata_id_n_sectors(const u16 *id)
 {
 	if (ata_id_has_lba(id)) {
 		if (ata_id_has_lba48(id))
-			return ata_id_u64(id, 100);
+			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
 		else
-			return ata_id_u32(id, 60);
+			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
 	} else {
 		if (ata_id_current_chs_valid(id))
-			return ata_id_u32(id, 57);
+			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
+			       id[ATA_ID_CUR_SECTORS];
 		else
-			return id[1] * id[3] * id[6];
+			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
+			       id[ATA_ID_SECTORS];
 	}
 }
 
Acked-by on v2 1/1
#3 lore raw

Hello.

Quoted 12 lines · Robert Hancock

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

Quoted 5 lines

While we're at it, clean up this function to use named constants for the identify word values.

... though I'm not sure that the rename should be a part of this patch.

Quoted 9 lines
drivers/ata/libata-core.c +0 -1
index 9fbf059..33b5549 100644
@@ -1322,14 +1322,16 @@ static u64 ata_id_n_sectors(const u16 *id)
 {
 	if (ata_id_has_lba(id)) {
 		if (ata_id_has_lba48(id))
-			return ata_id_u64(id, 100);

drivers/ata/libata-core.c static u64 ata_id_n_sectors(const u16 *id) · line 1332

Quoted 10 lines
drivers/ata/libata-core.c +4 -2
+			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
 		else
-			return ata_id_u32(id, 60);
+			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
 	} else {
 		if (ata_id_current_chs_valid(id))
-			return ata_id_u32(id, 57);
+			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
+			       id[ATA_ID_CUR_SECTORS];
 

It won't hurt to comment on why the capacity is ignored.
Unfortunately, the IDE code lacks such comment.

MBR, Sergei

#4 lore raw
In reply to Robert Hancock
Quoted 12 lines · Robert Hancock

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

Quoted 4 lines

While we're at it, clean up this function to use named constants for the identify word values.

Jeff, any thoughts on this one? I would suggest it's -upstream material (for .30)..

Quoted 31 lines
drivers/ata/libata-core.c +6 -5
index 9fbf059..33b5549 100644
@@ -1322,14 +1322,16 @@ static u64 ata_id_n_sectors(const u16 *id)
 {
 	if (ata_id_has_lba(id)) {
 		if (ata_id_has_lba48(id))
-			return ata_id_u64(id, 100);
+			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
 		else
-			return ata_id_u32(id, 60);
+			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
 	} else {
 		if (ata_id_current_chs_valid(id))
-			return ata_id_u32(id, 57);
+			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
+			       id[ATA_ID_CUR_SECTORS];
 		else
-			return id[1] * id[3] * id[6];
+			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
+			       id[ATA_ID_SECTORS];
 	}
 }
 
--

To unsubscribe from this list: send the line "unsubscribe linux-ide" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to Robert Hancock
Quoted 13 lines · Robert Hancock

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

..

drivers/ata/libata-core.c static u64 ata_id_n_sectors(const u16 *id) · line 1332

Quoted 6 lines
drivers/ata/libata-core.c +1 -1
 	} else {
 		if (ata_id_current_chs_valid(id))
-			return ata_id_u32(id, 57);
+			return id[54] * id[55] * id[56];
 		else
 			return id[1] * id[3] * id[6];

..

NAK. That's not quite correct, either.

The LBA capacity can be larger than the CHS capacity, so we have to use the reported LBA values if at all possible.

That's why ata_id_is_lba_capacity_ok() exists,
and why it looks so peculiar.

Some of those early drives really did require that kind of logic.

Cheers

#6 lore raw
Quoted 25 lines · Mark Lord
Quoted 13 lines · Robert Hancock

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

..

drivers/ata/libata-core.c static u64 ata_id_n_sectors(const u16 *id) · line 1332

Quoted 6 lines
     } else {
         if (ata_id_current_chs_valid(id))
drivers/ata/libata-core.c +1 -1
-            return ata_id_u32(id, 57);
+            return id[54] * id[55] * id[56];
         else
             return id[1] * id[3] * id[6];

..

NAK. That's not quite correct, either.

Quoted 7 lines

The LBA capacity can be larger than the CHS capacity, so we have to use the reported LBA values if at all possible.

That's why ata_id_is_lba_capacity_ok() exists,
and why it looks so peculiar.

Some of those early drives really did require that kind of logic.

..

Mind you, one can do better than that, too.

The "10% solution" in there right now is a bit of a (working) hack. It really probably just needs to check for a flipped-LBA that is within +/- one full cylinder of the CHS capacity.

Cheers

#7 lore raw
In reply to Mark Lord
Quoted 25 lines · Mark Lord
Quoted 13 lines · Robert Hancock

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

..

drivers/ata/libata-core.c static u64 ata_id_n_sectors(const u16 *id) · line 1332

Quoted 6 lines
     } else {
         if (ata_id_current_chs_valid(id))
drivers/ata/libata-core.c +1 -1
-            return ata_id_u32(id, 57);
+            return id[54] * id[55] * id[56];
         else
             return id[1] * id[3] * id[6];

..

NAK. That's not quite correct, either.

Quoted 7 lines

The LBA capacity can be larger than the CHS capacity, so we have to use the reported LBA values if at all possible.

That's why ata_id_is_lba_capacity_ok() exists,
and why it looks so peculiar.

Some of those early drives really did require that kind of logic.

This is the !ata_id_has_lba code path. If the drive supports LBA then the LBA capacity will always be used, that hasn't changed.

#8 lore raw

Robert Hancock wrote:
..

Quoted 1 line

This is the !ata_id_has_lba code path. If the drive supports LBA then the LBA capacity will always be used, that hasn't changed.

..

Oh, good. No problems then!

Cheers

#9 lore raw
In reply to Mark Lord

Hello.

Quoted 22 lines · Mark Lord
Quoted 13 lines

Hanno Böck reported a problem where an old Conner CP30254 240MB hard drive was reported as 1.1TB in capacity by libata:

http://lkml.org/lkml/2009/2/13/134

This was caused by libata trusting the drive's reported current capacity in sectors in identify words 57 and 58 if the drive does not support LBA and the current CHS translation values appear valid. Unfortunately it seems older
ATA specs were vague about what this field should contain and a number of drives
used values with wrong byte order or that were totally bogus. There's no
unique information that it conveys and so we can just calculate the number
of sectors from the reported current CHS values.

..

Quoted 6 lines
     } else {
         if (ata_id_current_chs_valid(id))
drivers/ata/libata-core.c +1 -1
-            return ata_id_u32(id, 57);
+            return id[54] * id[55] * id[56];
         else
             return id[1] * id[3] * id[6];

..

Quoted 7 lines

NAK. That's not quite correct, either.

The LBA capacity can be larger than the CHS capacity, so we have to use the reported LBA values if at all possible.

That's why ata_id_is_lba_capacity_ok() exists,
and why it looks so peculiar.

I think that checking LBA validity is a matter of another patch. This
patch in itself should be sufficient.

Quoted 1 line

Some of those early drives really did require that kind of logic.

I hightly doubt that this 240 MB drive is LBA capable at all.

Quoted 1 line

Cheers

MBR, Sergei