blob: 5d1c261a2cfd588b04284c3e18548e3b812d918e [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
Masato Suzukiea2c18e2018-10-30 16:14:05 +090032 if (dev->zone_nr_conv >= dev->nr_zones) {
33 dev->zone_nr_conv = dev->nr_zones - 1;
34 pr_info("null_blk: changed the number of conventional zones to %u",
35 dev->zone_nr_conv);
36 }
37
38 for (i = 0; i < dev->zone_nr_conv; i++) {
39 struct blk_zone *zone = &dev->zones[i];
40
41 zone->start = sector;
42 zone->len = dev->zone_size_sects;
43 zone->wp = zone->start + zone->len;
44 zone->type = BLK_ZONE_TYPE_CONVENTIONAL;
45 zone->cond = BLK_ZONE_COND_NOT_WP;
46
47 sector += dev->zone_size_sects;
48 }
49
50 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020051 struct blk_zone *zone = &dev->zones[i];
52
53 zone->start = zone->wp = sector;
54 zone->len = dev->zone_size_sects;
55 zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
56 zone->cond = BLK_ZONE_COND_EMPTY;
57
58 sector += dev->zone_size_sects;
59 }
60
61 return 0;
62}
63
64void null_zone_exit(struct nullb_device *dev)
65{
66 kvfree(dev->zones);
67}
68
Christoph Hellwige76239a2018-10-12 19:08:49 +090069int null_zone_report(struct gendisk *disk, sector_t sector,
70 struct blk_zone *zones, unsigned int *nr_zones,
71 gfp_t gfp_mask)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020072{
Christoph Hellwige76239a2018-10-12 19:08:49 +090073 struct nullb *nullb = disk->private_data;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020074 struct nullb_device *dev = nullb->dev;
Christoph Hellwige76239a2018-10-12 19:08:49 +090075 unsigned int zno, nrz = 0;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020076
Christoph Hellwige76239a2018-10-12 19:08:49 +090077 if (!dev->zoned)
78 /* Not a zoned null device */
79 return -EOPNOTSUPP;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020080
Christoph Hellwige76239a2018-10-12 19:08:49 +090081 zno = null_zone_no(dev, sector);
82 if (zno < dev->nr_zones) {
83 nrz = min_t(unsigned int, *nr_zones, dev->nr_zones - zno);
84 memcpy(zones, &dev->zones[zno], nrz * sizeof(struct blk_zone));
85 }
86
87 *nr_zones = nrz;
88
89 return 0;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020090}
91
Jens Axboeb228ba12018-09-12 18:21:11 -060092void null_zone_write(struct nullb_cmd *cmd, sector_t sector,
93 unsigned int nr_sectors)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020094{
95 struct nullb_device *dev = cmd->nq->dev;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +020096 unsigned int zno = null_zone_no(dev, sector);
97 struct blk_zone *zone = &dev->zones[zno];
98
99 switch (zone->cond) {
100 case BLK_ZONE_COND_FULL:
101 /* Cannot write to a full zone */
102 cmd->error = BLK_STS_IOERR;
103 break;
104 case BLK_ZONE_COND_EMPTY:
105 case BLK_ZONE_COND_IMP_OPEN:
106 /* Writes must be at the write pointer position */
Jens Axboeb228ba12018-09-12 18:21:11 -0600107 if (sector != zone->wp) {
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200108 cmd->error = BLK_STS_IOERR;
109 break;
110 }
111
112 if (zone->cond == BLK_ZONE_COND_EMPTY)
113 zone->cond = BLK_ZONE_COND_IMP_OPEN;
114
Jens Axboeb228ba12018-09-12 18:21:11 -0600115 zone->wp += nr_sectors;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200116 if (zone->wp == zone->start + zone->len)
117 zone->cond = BLK_ZONE_COND_FULL;
118 break;
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900119 case BLK_ZONE_COND_NOT_WP:
120 break;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200121 default:
122 /* Invalid zone condition */
123 cmd->error = BLK_STS_IOERR;
124 break;
125 }
126}
127
Jens Axboeb228ba12018-09-12 18:21:11 -0600128void null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200129{
130 struct nullb_device *dev = cmd->nq->dev;
Jens Axboeb228ba12018-09-12 18:21:11 -0600131 unsigned int zno = null_zone_no(dev, sector);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200132 struct blk_zone *zone = &dev->zones[zno];
133
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900134 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) {
135 cmd->error = BLK_STS_IOERR;
136 return;
137 }
138
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200139 zone->cond = BLK_ZONE_COND_EMPTY;
140 zone->wp = zone->start;
141}