blob: 7c6b86d987002b73251a9725d71af5b281987c9f [file] [log] [blame]
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001// SPDX-License-Identifier: GPL-2.0
2#include <linux/vmalloc.h>
3#include "null_blk.h"
4
5/* zone_size in MBs to sectors. */
6#define ZONE_SIZE_SHIFT 11
7
8static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
9{
10 return sect >> ilog2(dev->zone_size_sects);
11}
12
13int null_zone_init(struct nullb_device *dev)
14{
15 sector_t dev_size = (sector_t)dev->size * 1024 * 1024;
16 sector_t sector = 0;
17 unsigned int i;
18
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;
22 }
23
24 dev->zone_size_sects = dev->zone_size << ZONE_SIZE_SHIFT;
25 dev->nr_zones = dev_size >>
26 (SECTOR_SHIFT + ilog2(dev->zone_size_sects));
27 dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone),
28 GFP_KERNEL | __GFP_ZERO);
29 if (!dev->zones)
30 return -ENOMEM;
31
32 for (i = 0; i < dev->nr_zones; i++) {
33 struct blk_zone *zone = &dev->zones[i];
34
35 zone->start = zone->wp = sector;
36 zone->len = dev->zone_size_sects;
37 zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
38 zone->cond = BLK_ZONE_COND_EMPTY;
39
40 sector += dev->zone_size_sects;
41 }
42
43 return 0;
44}
45
46void null_zone_exit(struct nullb_device *dev)
47{
48 kvfree(dev->zones);
49}
50
Jens Axboeb228ba12018-09-12 18:21:11 -060051static void null_zone_fill_bio(struct nullb_device *dev, struct bio *bio,
52 unsigned int zno, unsigned int nr_zones)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020053{
54 struct blk_zone_report_hdr *hdr = NULL;
55 struct bio_vec bvec;
56 struct bvec_iter iter;
57 void *addr;
58 unsigned int zones_to_cpy;
59
Jens Axboeb228ba12018-09-12 18:21:11 -060060 bio_for_each_segment(bvec, bio, iter) {
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020061 addr = kmap_atomic(bvec.bv_page);
62
63 zones_to_cpy = bvec.bv_len / sizeof(struct blk_zone);
64
65 if (!hdr) {
66 hdr = (struct blk_zone_report_hdr *)addr;
67 hdr->nr_zones = nr_zones;
68 zones_to_cpy--;
69 addr += sizeof(struct blk_zone_report_hdr);
70 }
71
72 zones_to_cpy = min_t(unsigned int, zones_to_cpy, nr_zones);
73
74 memcpy(addr, &dev->zones[zno],
75 zones_to_cpy * sizeof(struct blk_zone));
76
77 kunmap_atomic(addr);
78
79 nr_zones -= zones_to_cpy;
80 zno += zones_to_cpy;
81
82 if (!nr_zones)
83 break;
84 }
85}
86
Jens Axboeb228ba12018-09-12 18:21:11 -060087blk_status_t null_zone_report(struct nullb *nullb, struct bio *bio)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020088{
89 struct nullb_device *dev = nullb->dev;
Jens Axboeb228ba12018-09-12 18:21:11 -060090 unsigned int zno = null_zone_no(dev, bio->bi_iter.bi_sector);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020091 unsigned int nr_zones = dev->nr_zones - zno;
Jens Axboeb228ba12018-09-12 18:21:11 -060092 unsigned int max_zones;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020093
Jens Axboeb228ba12018-09-12 18:21:11 -060094 max_zones = (bio->bi_iter.bi_size / sizeof(struct blk_zone)) - 1;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020095 nr_zones = min_t(unsigned int, nr_zones, max_zones);
Jens Axboeb228ba12018-09-12 18:21:11 -060096 null_zone_fill_bio(nullb->dev, bio, zno, nr_zones);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020097
98 return BLK_STS_OK;
99}
100
Jens Axboeb228ba12018-09-12 18:21:11 -0600101void null_zone_write(struct nullb_cmd *cmd, sector_t sector,
102 unsigned int nr_sectors)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200103{
104 struct nullb_device *dev = cmd->nq->dev;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200105 unsigned int zno = null_zone_no(dev, sector);
106 struct blk_zone *zone = &dev->zones[zno];
107
108 switch (zone->cond) {
109 case BLK_ZONE_COND_FULL:
110 /* Cannot write to a full zone */
111 cmd->error = BLK_STS_IOERR;
112 break;
113 case BLK_ZONE_COND_EMPTY:
114 case BLK_ZONE_COND_IMP_OPEN:
115 /* Writes must be at the write pointer position */
Jens Axboeb228ba12018-09-12 18:21:11 -0600116 if (sector != zone->wp) {
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200117 cmd->error = BLK_STS_IOERR;
118 break;
119 }
120
121 if (zone->cond == BLK_ZONE_COND_EMPTY)
122 zone->cond = BLK_ZONE_COND_IMP_OPEN;
123
Jens Axboeb228ba12018-09-12 18:21:11 -0600124 zone->wp += nr_sectors;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200125 if (zone->wp == zone->start + zone->len)
126 zone->cond = BLK_ZONE_COND_FULL;
127 break;
128 default:
129 /* Invalid zone condition */
130 cmd->error = BLK_STS_IOERR;
131 break;
132 }
133}
134
Jens Axboeb228ba12018-09-12 18:21:11 -0600135void null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200136{
137 struct nullb_device *dev = cmd->nq->dev;
Jens Axboeb228ba12018-09-12 18:21:11 -0600138 unsigned int zno = null_zone_no(dev, sector);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200139 struct blk_zone *zone = &dev->zones[zno];
140
141 zone->cond = BLK_ZONE_COND_EMPTY;
142 zone->wp = zone->start;
143}