Skip to content

Commit 366c246

Browse files
committed
[SCSI] sd: handle bad lba in sense information
Some devices report medium error locations incorrectly. Add guards to make sure the reported bad lba is actually in the request that caused it. Additionally remove the large case statment for sector sizes and replace it with the proper u64 divisions. Tested-by: Mike Snitzer <snitzer@gmail.com> Cc: Stable Tree <stable@kernel.org> Cc: Tony Battersby <tonyb@cybernetics.com> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
1 parent d7402cd commit 366c246

1 file changed

Lines changed: 16 additions & 18 deletions

File tree

‎drivers/scsi/sd.c‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ static int sd_done(struct scsi_cmnd *SCpnt)
929929
unsigned int xfer_size = scsi_bufflen(SCpnt);
930930
unsigned int good_bytes = result ? 0 : xfer_size;
931931
u64 start_lba = SCpnt->request->sector;
932+
u64 end_lba = SCpnt->request->sector + (xfer_size / 512);
932933
u64 bad_lba;
933934
struct scsi_sense_hdr sshdr;
934935
int sense_valid = 0;
@@ -967,26 +968,23 @@ static int sd_done(struct scsi_cmnd *SCpnt)
967968
goto out;
968969
if (xfer_size <= SCpnt->device->sector_size)
969970
goto out;
970-
switch (SCpnt->device->sector_size) {
971-
case 256:
971+
if (SCpnt->device->sector_size < 512) {
972+
/* only legitimate sector_size here is 256 */
972973
start_lba <<= 1;
973-
break;
974-
case 512:
975-
break;
976-
case 1024:
977-
start_lba >>= 1;
978-
break;
979-
case 2048:
980-
start_lba >>= 2;
981-
break;
982-
case 4096:
983-
start_lba >>= 3;
984-
break;
985-
default:
986-
/* Print something here with limiting frequency. */
987-
goto out;
988-
break;
974+
end_lba <<= 1;
975+
} else {
976+
/* be careful ... don't want any overflows */
977+
u64 factor = SCpnt->device->sector_size / 512;
978+
do_div(start_lba, factor);
979+
do_div(end_lba, factor);
989980
}
981+
982+
if (bad_lba < start_lba || bad_lba >= end_lba)
983+
/* the bad lba was reported incorrectly, we have
984+
* no idea where the error is
985+
*/
986+
goto out;
987+
990988
/* This computation should always be done in terms of
991989
* the resolution of the device's medium.
992990
*/

0 commit comments

Comments
 (0)