>>>>> "Mike" == Mike Snitzer <snitzer@xxxxxxxxxx> writes: Mike> Turns out that this LUN has a 4K granularity requirement (will Mike> fail the WRITE SAME if the granularity requirements are not met). That's lame. The devices I've been working with just ignore the portions of the block range that are not aligned / do not constitute entire blocks. I've had the following patch kicking around in my tree for a while. It does not yet handle offset discard alignment, though. -- Martin K. Petersen Oracle Linux Engineering diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 2e9c6bd..60632ed 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -420,12 +420,22 @@ static int sd_prepare_discard(struct request *rq) { struct scsi_disk *sdkp = scsi_disk(rq->rq_disk); struct bio *bio = rq->bio; - sector_t sector = bio->bi_sector; - unsigned int num = bio_sectors(bio); + sector_t lba; + unsigned int xfer_length, logical_block_shift; + unsigned int discard_shift, discard_mask; + + logical_block_shift = blksize_bits(sdkp->device->sector_size) - 9; + lba = bio->bi_sector >> logical_block_shift; + xfer_length = bio_sectors(bio) >> logical_block_shift; + + discard_shift = blksize_bits(rq->q->limits.discard_granularity) - 9; + + if (discard_shift) { + discard_mask = (1 << discard_shift) - 1; + sector_t end_lba = (lba + xfer_length) & ~discard_mask; - if (sdkp->device->sector_size == 4096) { - sector >>= 3; - num >>= 3; + lba = (lba + discard_mask) & ~discard_mask; + xfer_length = end_lba - lba; } rq->cmd_type = REQ_TYPE_BLOCK_PC; @@ -445,15 +455,15 @@ static int sd_prepare_discard(struct request *rq) put_unaligned_be16(6 + 16, &buf[0]); put_unaligned_be16(16, &buf[2]); - put_unaligned_be64(sector, &buf[8]); - put_unaligned_be32(num, &buf[16]); + put_unaligned_be64(lba, &buf[8]); + put_unaligned_be32(xfer_length, &buf[16]); kunmap_atomic(buf, KM_USER0); } else { rq->cmd[0] = WRITE_SAME_16; rq->cmd[1] = 0x8; /* UNMAP */ - put_unaligned_be64(sector, &rq->cmd[2]); - put_unaligned_be32(num, &rq->cmd[10]); + put_unaligned_be64(lba, &rq->cmd[2]); + put_unaligned_be32(xfer_length, &rq->cmd[10]); rq->cmd_len = 16; } -- To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html