Skip to content

Commit 37f0dc2

Browse files
Ming LeiChristoph Hellwig
authored andcommitted
nvme: fix handling single range discard request
When investigating one customer report on warning in nvme_setup_discard, we observed the controller(nvme/tcp) actually exposes queue_max_discard_segments(req->q) == 1. Obviously the current code can't handle this situation, since contiguity merge like normal RW request is taken. Fix the issue by building range from request sector/nr_sectors directly. Fixes: b35ba01 ("nvme: support ranged discard requests") Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Chaitanya Kulkarni <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 54686b6 commit 37f0dc2

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

drivers/nvme/host/core.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -781,16 +781,26 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
781781
range = page_address(ns->ctrl->discard_page);
782782
}
783783

784-
__rq_for_each_bio(bio, req) {
785-
u64 slba = nvme_sect_to_lba(ns, bio->bi_iter.bi_sector);
786-
u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
787-
788-
if (n < segments) {
789-
range[n].cattr = cpu_to_le32(0);
790-
range[n].nlb = cpu_to_le32(nlb);
791-
range[n].slba = cpu_to_le64(slba);
784+
if (queue_max_discard_segments(req->q) == 1) {
785+
u64 slba = nvme_sect_to_lba(ns, blk_rq_pos(req));
786+
u32 nlb = blk_rq_sectors(req) >> (ns->lba_shift - 9);
787+
788+
range[0].cattr = cpu_to_le32(0);
789+
range[0].nlb = cpu_to_le32(nlb);
790+
range[0].slba = cpu_to_le64(slba);
791+
n = 1;
792+
} else {
793+
__rq_for_each_bio(bio, req) {
794+
u64 slba = nvme_sect_to_lba(ns, bio->bi_iter.bi_sector);
795+
u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
796+
797+
if (n < segments) {
798+
range[n].cattr = cpu_to_le32(0);
799+
range[n].nlb = cpu_to_le32(nlb);
800+
range[n].slba = cpu_to_le64(slba);
801+
}
802+
n++;
792803
}
793-
n++;
794804
}
795805

796806
if (WARN_ON_ONCE(n != segments)) {

0 commit comments

Comments
 (0)