All of lore.kernel.org
 help / color / mirror / Atom feed
* ata_tf_read_block() question
@ 2009-08-15 13:48 Atsushi Nemoto
  2009-08-16  2:15 ` Tejun Heo
  0 siblings, 1 reply; 5+ messages in thread
From: Atsushi Nemoto @ 2009-08-15 13:48 UTC (permalink / raw)
  To: Jeff Garzik, Tejun Heo; +Cc: linux-ide, linux-kernel

I have a question on CHS calculation in ata_tf_read_block().

The calculation in ata_tf_read_block() is:
	block = (cyl * dev->heads + head) * dev->sectors + sect;

but ata_build_rw_tf() does:
	track = (u32)block / dev->sectors;
	cyl   = track / dev->heads;
	head  = track % dev->heads;
	sect  = (u32)block % dev->sectors + 1;

It seems inconsistent.  The correct calculation is:
	block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
isn't it?

I don't have any real problem.  Just noticed by code reading.
---
Atsushi Nemoto

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ata_tf_read_block() question
  2009-08-15 13:48 ata_tf_read_block() question Atsushi Nemoto
@ 2009-08-16  2:15 ` Tejun Heo
  2009-08-16  9:33   ` Atsushi Nemoto
  0 siblings, 1 reply; 5+ messages in thread
From: Tejun Heo @ 2009-08-16  2:15 UTC (permalink / raw)
  To: Atsushi Nemoto; +Cc: Jeff Garzik, linux-ide, linux-kernel

Hello, Atsushi.

Atsushi Nemoto wrote:
> I have a question on CHS calculation in ata_tf_read_block().
> 
> The calculation in ata_tf_read_block() is:
> 	block = (cyl * dev->heads + head) * dev->sectors + sect;
> 
> but ata_build_rw_tf() does:
> 	track = (u32)block / dev->sectors;
> 	cyl   = track / dev->heads;
> 	head  = track % dev->heads;
> 	sect  = (u32)block % dev->sectors + 1;
> 
> It seems inconsistent.  The correct calculation is:
> 	block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
> isn't it?

Yes, indeed.

> I don't have any real problem.  Just noticed by code reading.

ata_tf_read_block() currently is used only when reporting failed block
address to upper layer so off-by-one bug there wouldn't be too
visible, especially for the venerable CHS addressing.

Care to submit a patch w/ warning message and capping for sect == 0
case?

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ata_tf_read_block() question
  2009-08-16  2:15 ` Tejun Heo
@ 2009-08-16  9:33   ` Atsushi Nemoto
  2009-08-16 12:21     ` [PATCH #upstream-fixes] libata: fix off-by-one error in ata_tf_read_block() Tejun Heo
  0 siblings, 1 reply; 5+ messages in thread
From: Atsushi Nemoto @ 2009-08-16  9:33 UTC (permalink / raw)
  To: htejun; +Cc: jgarzik, linux-ide, linux-kernel

On Sun, 16 Aug 2009 11:15:31 +0900, Tejun Heo <htejun@gmail.com> wrote:
> > It seems inconsistent.  The correct calculation is:
> > 	block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
> > isn't it?
> 
> Yes, indeed.
> 
> > I don't have any real problem.  Just noticed by code reading.
> 
> ata_tf_read_block() currently is used only when reporting failed block
> address to upper layer so off-by-one bug there wouldn't be too
> visible, especially for the venerable CHS addressing.

Thank you for checking.

> Care to submit a patch w/ warning message and capping for sect == 0
> case?

Well, I expect fix by you (or other libata hackers) since I think you
can write better warning message and changelog than me ;)

---
Atsushi Nemoto

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH #upstream-fixes] libata: fix off-by-one error in ata_tf_read_block()
  2009-08-16  9:33   ` Atsushi Nemoto
@ 2009-08-16 12:21     ` Tejun Heo
  2009-09-09  1:19       ` Jeff Garzik
  0 siblings, 1 reply; 5+ messages in thread
From: Tejun Heo @ 2009-08-16 12:21 UTC (permalink / raw)
  To: Atsushi Nemoto; +Cc: jgarzik, linux-ide, linux-kernel, stable

ata_tf_read_block() has off-by-one error when converting CHS address
to LBA.  The bug isn't very visible because ata_tf_read_block() is
used only when generating sense data for a failed RW command and CHS
addressing isn't used too often these days.

This problem was spotted by Atsushi Nemoto.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
---
> Well, I expect fix by you (or other libata hackers) since I think you
> can write better warning message and changelog than me ;)

In that case, sure.  Thanks a lot for the nice spotting.  :-)

 drivers/ata/libata-core.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 072ba5e..e71149b 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -709,7 +709,13 @@ u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
 		head = tf->device & 0xf;
 		sect = tf->lbal;
 
-		block = (cyl * dev->heads + head) * dev->sectors + sect;
+		if (!sect) {
+			ata_dev_printk(dev, KERN_WARNING, "device reported "
+				       "invalid CHS sector 0\n");
+			sect = 1; /* oh well */
+		}
+
+		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
 	}
 
 	return block;


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH #upstream-fixes] libata: fix off-by-one error in ata_tf_read_block()
  2009-08-16 12:21     ` [PATCH #upstream-fixes] libata: fix off-by-one error in ata_tf_read_block() Tejun Heo
@ 2009-09-09  1:19       ` Jeff Garzik
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff Garzik @ 2009-09-09  1:19 UTC (permalink / raw)
  To: Tejun Heo; +Cc: Atsushi Nemoto, linux-ide, linux-kernel, stable

On 08/16/2009 08:21 AM, Tejun Heo wrote:
> ata_tf_read_block() has off-by-one error when converting CHS address
> to LBA.  The bug isn't very visible because ata_tf_read_block() is
> used only when generating sense data for a failed RW command and CHS
> addressing isn't used too often these days.
>
> This problem was spotted by Atsushi Nemoto.
>
> Signed-off-by: Tejun Heo<tj@kernel.org>
> Reported-by: Atsushi Nemoto<anemo@mba.ocn.ne.jp>
> ---
>> Well, I expect fix by you (or other libata hackers) since I think you
>> can write better warning message and changelog than me ;)
>
> In that case, sure.  Thanks a lot for the nice spotting.  :-)
>
>   drivers/ata/libata-core.c |    8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)

applied



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-09-09  1:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-15 13:48 ata_tf_read_block() question Atsushi Nemoto
2009-08-16  2:15 ` Tejun Heo
2009-08-16  9:33   ` Atsushi Nemoto
2009-08-16 12:21     ` [PATCH #upstream-fixes] libata: fix off-by-one error in ata_tf_read_block() Tejun Heo
2009-09-09  1:19       ` Jeff Garzik

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.