Skip to contentSkip to conversation

ata: libata-scsi: fix REQUESTED MAX LBA field for REMOVE ELEMENT AND TRUNCATE

PATCH Applied Nick Spooner 3 participants 5 messages Nov 13 – Nov 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ata: libata-scsi: fix REQUESTED MAX LBA field for REMOVE ELEMENT AND TRUNCATE

ata_scsi_remove_element_and_truncate_xlat() copies the REQUESTED CAPACITY field, req_capacity, to REQUESTED MAX LBA for every block count except 1.

However, SAT-6 specifies that if REQUESTED CAPACITY is greater than one, the REQUESTED MAX LBA shall be set to REQUESTED CAPACITY minus one, since the max LBA = count - 1 (see SAT-6 rev2, 9.16.1, Table 74). For example, if a capacity C > 1 is requested, the capacity ends up one block larger if the drive can provide C + 1. If the drive can provide C but not C + 1, a request that should be valid is instead rejected with INVALID FIELD IN CDB (see ACS-5 rev10, 7.33.3.3).

Modify ata_scsi_remove_element_and_truncate_xlat() to decrement req_capacity, except when equal to 1 or 0 (rejected if 1, let the device choose if 0).

Tested by issuing sg_rem_rest_elem on a Seagate drive ST32000NM001F, firmware db02 and checking the taskfile.

Fixes db496721cb0d ("ata: libata-scsi: add support for the REMOVE ELEMENT AND TRUNCATE command")

---
Tested on libata/for-7.4-fixes by capturing the trace of what libata sends to the drive.

  sg_rem_rest_elem --remove --element=0x30 --capacity=<C> /dev/sda

Unpatched, C=1000000:

    ata_qc_issue: ata_port=2 ata_dev=0 tag=12 proto=ATA_PROT_NODATA cmd=0x7c  tf=(7c/00:30:40:42:0f/00:00:00:00:00/40)

Patched, C=1000000, 0, 2:

    ata_qc_issue: ata_port=2 ata_dev=0 tag=0 proto=ATA_PROT_NODATA cmd=0x7c  tf=(7c/00:30:3f:42:0f/00:00:00:00:00/40)
    ... tf=(7c/00:30:00:00:00/00:00:00:00:00/40)
    ... tf=(7c/00:30:01:00:00/00:00:00:00:00/40)
 drivers/ata/libata-scsi.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/ata/libata-scsi.c +3 -0
index 87a57495d2e4..20ea1c997d56 100644
@@ -4991,6 +4991,9 @@ ata_scsi_remove_element_and_truncate_xlat(struct ata_queued_cmd *qc)
 		return 1;
 	}
 
+	if (req_capacity > 1)
+		req_capacity--;
+
 	id = get_unaligned_be32(&cdb[10]);
 
 	tf->protocol = ATA_PROT_NODATA;
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 15 lines · Nick Spooner

ata_scsi_remove_element_and_truncate_xlat() copies the REQUESTED CAPACITY field, req_capacity, to REQUESTED MAX LBA for every block count except 1.

However, SAT-6 specifies that if REQUESTED CAPACITY is greater than one, the REQUESTED MAX LBA shall be set to REQUESTED CAPACITY minus one, since the max LBA = count - 1 (see SAT-6 rev2, 9.16.1, Table 74). For example, if a capacity C > 1 is requested, the capacity ends up one block larger if the drive can provide C + 1. If the drive can provide C but not C + 1, a request that should be valid is instead rejected with INVALID FIELD IN CDB (see ACS-5 rev10, 7.33.3.3).

Modify ata_scsi_remove_element_and_truncate_xlat() to decrement req_capacity, except when equal to 1 or 0 (rejected if 1, let the device choose if 0).

Quoted 6 lines

Tested by issuing sg_rem_rest_elem on a Seagate drive ST32000NM001F, firmware db02 and checking the taskfile.

Fixes db496721cb0d ("ata: libata-scsi: add support for the REMOVE ELEMENT AND TRUNCATE command")

Good catch for the "- 1", but checking SAT, it says that:

If the REQUESTED CAPACITY field is set to one, then the REMOVE ELEMENT AND TRUNCATE command shall be terminated with a CHECK CONDITION status with the sense key set to ILLEGAL REQUEST and the additional sense code set to INVALID FIELD IN CDB.

So we should not silently ignore the 1 case and fail it. Can you send a v2 with that failure added?

Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
Quoted 2 lines · Damien Le Moal

So we should not silently ignore the 1 case and fail it. Can you send a v2 with that failure added?

The 1 case should already fail. The check was in the existing code just above where I patched, but the diff context cut it off:

	req_capacity = get_unaligned_be64(&cdb[2]);
	if (req_capacity == 1) {
		ata_scsi_set_invalid_field(dev, scmd, 2, 0);
		return 1;
	}

	if (req_capacity > 1)
		req_capacity--;

I believe that should cover all three cases in SAT-6.

maintainer
#3 lore raw
Quoted 16 lines · Nick Spooner
Quoted 2 lines · Damien Le Moal

So we should not silently ignore the 1 case and fail it. Can you send a v2 with that failure added?

The 1 case should already fail. The check was in the existing code just above where I patched, but the diff context cut it off:

	req_capacity = get_unaligned_be64(&cdb[2]);
	if (req_capacity == 1) {
		ata_scsi_set_invalid_field(dev, scmd, 2, 0);
		return 1;
	}

	if (req_capacity > 1)
		req_capacity--;
Quoted 1 line

I believe that should cover all three cases in SAT-6.

Good. Thanks for checking!

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer Applied
#4 lore raw
In reply to Nick Spooner
Quoted 11 lines · Nick Spooner

ata_scsi_remove_element_and_truncate_xlat() copies the REQUESTED CAPACITY field, req_capacity, to REQUESTED MAX LBA for every block count except 1.

However, SAT-6 specifies that if REQUESTED CAPACITY is greater than one, the REQUESTED MAX LBA shall be set to REQUESTED CAPACITY minus one, since the max LBA = count - 1 (see SAT-6 rev2, 9.16.1, Table 74). For example, if a capacity C > 1 is requested, the capacity ends up one block larger if the drive can provide C + 1. If the drive can provide C but not C + 1, a request that should be valid is instead rejected with INVALID FIELD IN CDB (see ACS-5 rev10, 7.33.3.3).

Quoted 1 line

[...]

Applied to libata/linux.git (for-7.4-fixes), thanks!

[1/1] ata: libata-scsi: fix REQUESTED MAX LBA field for REMOVE ELEMENT AND TRUNCATE

      https://git.kernel.org/libata/linux/c/9016b8b9

Kind regards,
Niklas