Linux 4.19.133
[linux/fpc-iii.git] / drivers / block / null_blk_zoned.c
blobd1725ac636c0407fac5007c9728862ad0a0ffc50
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/vmalloc.h>
3 #include "null_blk.h"
5 /* zone_size in MBs to sectors. */
6 #define ZONE_SIZE_SHIFT 11
8 static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
10 return sect >> ilog2(dev->zone_size_sects);
13 int null_zone_init(struct nullb_device *dev)
15 sector_t dev_size = (sector_t)dev->size * 1024 * 1024;
16 sector_t sector = 0;
17 unsigned int i;
19 if (!is_power_of_2(dev->zone_size)) {
20 pr_err("null_blk: zone_size must be power-of-two\n");
21 return -EINVAL;
23 if (dev->zone_size > dev->size) {
24 pr_err("Zone size larger than device capacity\n");
25 return -EINVAL;
28 dev->zone_size_sects = dev->zone_size << ZONE_SIZE_SHIFT;
29 dev->nr_zones = dev_size >>
30 (SECTOR_SHIFT + ilog2(dev->zone_size_sects));
31 dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone),
32 GFP_KERNEL | __GFP_ZERO);
33 if (!dev->zones)
34 return -ENOMEM;
36 for (i = 0; i < dev->nr_zones; i++) {
37 struct blk_zone *zone = &dev->zones[i];
39 zone->start = zone->wp = sector;
40 zone->len = dev->zone_size_sects;
41 zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
42 zone->cond = BLK_ZONE_COND_EMPTY;
44 sector += dev->zone_size_sects;
47 return 0;
50 void null_zone_exit(struct nullb_device *dev)
52 kvfree(dev->zones);
55 static void null_zone_fill_bio(struct nullb_device *dev, struct bio *bio,
56 unsigned int zno, unsigned int nr_zones)
58 struct blk_zone_report_hdr *hdr = NULL;
59 struct bio_vec bvec;
60 struct bvec_iter iter;
61 void *addr;
62 unsigned int zones_to_cpy;
64 bio_for_each_segment(bvec, bio, iter) {
65 addr = kmap_atomic(bvec.bv_page);
67 zones_to_cpy = bvec.bv_len / sizeof(struct blk_zone);
69 if (!hdr) {
70 hdr = (struct blk_zone_report_hdr *)addr;
71 hdr->nr_zones = nr_zones;
72 zones_to_cpy--;
73 addr += sizeof(struct blk_zone_report_hdr);
76 zones_to_cpy = min_t(unsigned int, zones_to_cpy, nr_zones);
78 memcpy(addr, &dev->zones[zno],
79 zones_to_cpy * sizeof(struct blk_zone));
81 kunmap_atomic(addr);
83 nr_zones -= zones_to_cpy;
84 zno += zones_to_cpy;
86 if (!nr_zones)
87 break;
91 blk_status_t null_zone_report(struct nullb *nullb, struct bio *bio)
93 struct nullb_device *dev = nullb->dev;
94 unsigned int zno = null_zone_no(dev, bio->bi_iter.bi_sector);
95 unsigned int nr_zones = dev->nr_zones - zno;
96 unsigned int max_zones;
98 max_zones = (bio->bi_iter.bi_size / sizeof(struct blk_zone)) - 1;
99 nr_zones = min_t(unsigned int, nr_zones, max_zones);
100 null_zone_fill_bio(nullb->dev, bio, zno, nr_zones);
102 return BLK_STS_OK;
105 void null_zone_write(struct nullb_cmd *cmd, sector_t sector,
106 unsigned int nr_sectors)
108 struct nullb_device *dev = cmd->nq->dev;
109 unsigned int zno = null_zone_no(dev, sector);
110 struct blk_zone *zone = &dev->zones[zno];
112 switch (zone->cond) {
113 case BLK_ZONE_COND_FULL:
114 /* Cannot write to a full zone */
115 cmd->error = BLK_STS_IOERR;
116 break;
117 case BLK_ZONE_COND_EMPTY:
118 case BLK_ZONE_COND_IMP_OPEN:
119 /* Writes must be at the write pointer position */
120 if (sector != zone->wp) {
121 cmd->error = BLK_STS_IOERR;
122 break;
125 if (zone->cond == BLK_ZONE_COND_EMPTY)
126 zone->cond = BLK_ZONE_COND_IMP_OPEN;
128 zone->wp += nr_sectors;
129 if (zone->wp == zone->start + zone->len)
130 zone->cond = BLK_ZONE_COND_FULL;
131 break;
132 default:
133 /* Invalid zone condition */
134 cmd->error = BLK_STS_IOERR;
135 break;
139 void null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
141 struct nullb_device *dev = cmd->nq->dev;
142 unsigned int zno = null_zone_no(dev, sector);
143 struct blk_zone *zone = &dev->zones[zno];
145 zone->cond = BLK_ZONE_COND_EMPTY;
146 zone->wp = zone->start;