From: Ajay Joshi <[email protected]>

A zoned block device maintains a write pointer within a zone, and reads
beyond the write pointer are undefined. Fill data buffer returned above
the write pointer with 0xFF.

Signed-off-by: Ajay Joshi <[email protected]>
Reviewed-by: Damien Le Moal <[email protected]>
Reviewed-by: Matias Bjørling <[email protected]>
Signed-off-by: Chaitanya Kulkarni <[email protected]>
---
Hi,

This patch is originally posted in July :-
https://www.spinics.net/lists/linux-block/msg42403.html.
Resending this patch based on latest linux-block tree for-next branch.

-Chaitanya
---
 drivers/block/null_blk.h       |  8 ++++++++
 drivers/block/null_blk_main.c  | 26 +++++++++++++++++++++++++-
 drivers/block/null_blk_zoned.c | 21 +++++++++++++++++++--
 3 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/drivers/block/null_blk.h b/drivers/block/null_blk.h
index a235c45e22a7..93c2a3d403da 100644
--- a/drivers/block/null_blk.h
+++ b/drivers/block/null_blk.h
@@ -96,6 +96,8 @@ int null_zone_report(struct gendisk *disk, sector_t sector,
 blk_status_t null_handle_zoned(struct nullb_cmd *cmd,
                                enum req_opf op, sector_t sector,
                                sector_t nr_sectors);
+size_t null_zone_valid_read_len(struct nullb *nullb,
+                               sector_t sector, unsigned int len);
 #else
 static inline int null_zone_init(struct nullb_device *dev)
 {
@@ -115,5 +117,11 @@ static inline blk_status_t null_handle_zoned(struct 
nullb_cmd *cmd,
 {
        return BLK_STS_NOTSUPP;
 }
+static inline size_t null_zone_valid_read_len(struct nullb *nullb,
+                                             sector_t sector,
+                                             unsigned int len)
+{
+       return len;
+}
 #endif /* CONFIG_BLK_DEV_ZONED */
 #endif /* __NULL_BLK_H */
diff --git a/drivers/block/null_blk_main.c b/drivers/block/null_blk_main.c
index f5e0dffb4624..ea7a4d6b7848 100644
--- a/drivers/block/null_blk_main.c
+++ b/drivers/block/null_blk_main.c
@@ -1022,6 +1022,16 @@ static int copy_from_nullb(struct nullb *nullb, struct 
page *dest,
        return 0;
 }
 
+static void nullb_fill_pattern(struct nullb *nullb, struct page *page,
+                              unsigned int len, unsigned int off)
+{
+       void *dst;
+
+       dst = kmap_atomic(page);
+       memset(dst + off, 0xFF, len);
+       kunmap_atomic(dst);
+}
+
 static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
 {
        size_t temp;
@@ -1062,10 +1072,24 @@ static int null_transfer(struct nullb *nullb, struct 
page *page,
        unsigned int len, unsigned int off, bool is_write, sector_t sector,
        bool is_fua)
 {
+       struct nullb_device *dev = nullb->dev;
+       unsigned int valid_len = len;
        int err = 0;
 
        if (!is_write) {
-               err = copy_from_nullb(nullb, page, off, sector, len);
+               if (dev->zoned)
+                       valid_len = null_zone_valid_read_len(nullb,
+                               sector, len);
+
+               if (valid_len) {
+                       err = copy_from_nullb(nullb, page, off,
+                               sector, valid_len);
+                       off += valid_len;
+                       len -= valid_len;
+               }
+
+               if (len)
+                       nullb_fill_pattern(nullb, page, len, off);
                flush_dcache_page(page);
        } else {
                flush_dcache_page(page);
diff --git a/drivers/block/null_blk_zoned.c b/drivers/block/null_blk_zoned.c
index eabc116832a7..e020f17dac9f 100644
--- a/drivers/block/null_blk_zoned.c
+++ b/drivers/block/null_blk_zoned.c
@@ -84,6 +84,24 @@ int null_zone_report(struct gendisk *disk, sector_t sector,
        return 0;
 }
 
+size_t null_zone_valid_read_len(struct nullb *nullb,
+                               sector_t sector, unsigned int len)
+{
+       struct nullb_device *dev = nullb->dev;
+       struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
+       unsigned int nr_sectors = len >> SECTOR_SHIFT;
+
+       /* Read must be below the write pointer position */
+       if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL ||
+           sector + nr_sectors <= zone->wp)
+               return len;
+
+       if (sector > zone->wp)
+               return 0;
+
+       return (zone->wp - sector) << SECTOR_SHIFT;
+}
+
 static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector,
                     unsigned int nr_sectors)
 {
@@ -121,8 +139,7 @@ static blk_status_t null_zone_write(struct nullb_cmd *cmd, 
sector_t sector,
 static blk_status_t null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
 {
        struct nullb_device *dev = cmd->nq->dev;
-       unsigned int zno = null_zone_no(dev, sector);
-       struct blk_zone *zone = &dev->zones[zno];
+       struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
        size_t i;
 
        switch (req_op(cmd->rq)) {
-- 
2.22.1


Reply via email to