Skip to content

Commit e08f2ae

Browse files
damien-lemoalaxboe
authored andcommitted
nvme: Introduce nvme_lba_to_sect()
Introduce the new helper function nvme_lba_to_sect() to convert a device logical block number to a 512B sector number. Use this new helper in obvious places, cleaning up the code. Reviewed-by: Johannes Thumshirn <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Damien Le Moal <[email protected]> Signed-off-by: Keith Busch <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 314d48d commit e08f2ae

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

drivers/nvme/host/core.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
16111611

16121612
static void nvme_set_chunk_size(struct nvme_ns *ns)
16131613
{
1614-
u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1614+
u32 chunk_size = nvme_lba_to_sect(ns, ns->noiob);
16151615
blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
16161616
}
16171617

@@ -1648,8 +1648,7 @@ static void nvme_config_discard(struct gendisk *disk, struct nvme_ns *ns)
16481648

16491649
static void nvme_config_write_zeroes(struct gendisk *disk, struct nvme_ns *ns)
16501650
{
1651-
u32 max_sectors;
1652-
unsigned short bs = 1 << ns->lba_shift;
1651+
u64 max_blocks;
16531652

16541653
if (!(ns->ctrl->oncs & NVME_CTRL_ONCS_WRITE_ZEROES) ||
16551654
(ns->ctrl->quirks & NVME_QUIRK_DISABLE_WRITE_ZEROES))
@@ -1665,11 +1664,12 @@ static void nvme_config_write_zeroes(struct gendisk *disk, struct nvme_ns *ns)
16651664
* nvme_init_identify() if available.
16661665
*/
16671666
if (ns->ctrl->max_hw_sectors == UINT_MAX)
1668-
max_sectors = ((u32)(USHRT_MAX + 1) * bs) >> 9;
1667+
max_blocks = (u64)USHRT_MAX + 1;
16691668
else
1670-
max_sectors = ((u32)(ns->ctrl->max_hw_sectors + 1) * bs) >> 9;
1669+
max_blocks = ns->ctrl->max_hw_sectors + 1;
16711670

1672-
blk_queue_max_write_zeroes_sectors(disk->queue, max_sectors);
1671+
blk_queue_max_write_zeroes_sectors(disk->queue,
1672+
nvme_lba_to_sect(ns, max_blocks));
16731673
}
16741674

16751675
static int nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
@@ -1712,7 +1712,7 @@ static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
17121712
static void nvme_update_disk_info(struct gendisk *disk,
17131713
struct nvme_ns *ns, struct nvme_id_ns *id)
17141714
{
1715-
sector_t capacity = le64_to_cpu(id->nsze) << (ns->lba_shift - 9);
1715+
sector_t capacity = nvme_lba_to_sect(ns, le64_to_cpu(id->nsze));
17161716
unsigned short bs = 1 << ns->lba_shift;
17171717
u32 atomic_bs, phys_bs, io_opt;
17181718

drivers/nvme/host/nvme.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ static inline u64 nvme_sect_to_lba(struct nvme_ns *ns, sector_t sector)
426426
return sector >> (ns->lba_shift - SECTOR_SHIFT);
427427
}
428428

429+
/*
430+
* Convert a device logical block number to a 512B sector number.
431+
*/
432+
static inline sector_t nvme_lba_to_sect(struct nvme_ns *ns, u64 lba)
433+
{
434+
return lba << (ns->lba_shift - SECTOR_SHIFT);
435+
}
436+
429437
static inline void nvme_end_request(struct request *req, __le16 status,
430438
union nvme_result result)
431439
{

0 commit comments

Comments
 (0)