1 // 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
null_zone_no(struct nullb_device * dev,sector_t sect)8 static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
9 {
10 return sect >> ilog2(dev->zone_size_sects);
11 }
12
null_zone_init(struct nullb_device * dev)13 int 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("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 if (dev->zone_nr_conv >= dev->nr_zones) {
33 dev->zone_nr_conv = dev->nr_zones - 1;
34 pr_info("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++) {
51 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
null_zone_exit(struct nullb_device * dev)64 void null_zone_exit(struct nullb_device *dev)
65 {
66 kvfree(dev->zones);
67 }
68
null_report_zones(struct gendisk * disk,sector_t sector,unsigned int nr_zones,report_zones_cb cb,void * data)69 int null_report_zones(struct gendisk *disk, sector_t sector,
70 unsigned int nr_zones, report_zones_cb cb, void *data)
71 {
72 struct nullb *nullb = disk->private_data;
73 struct nullb_device *dev = nullb->dev;
74 unsigned int first_zone, i;
75 struct blk_zone zone;
76 int error;
77
78 first_zone = null_zone_no(dev, sector);
79 if (first_zone >= dev->nr_zones)
80 return 0;
81
82 nr_zones = min(nr_zones, dev->nr_zones - first_zone);
83 for (i = 0; i < nr_zones; i++) {
84 /*
85 * Stacked DM target drivers will remap the zone information by
86 * modifying the zone information passed to the report callback.
87 * So use a local copy to avoid corruption of the device zone
88 * array.
89 */
90 memcpy(&zone, &dev->zones[first_zone + i],
91 sizeof(struct blk_zone));
92 error = cb(&zone, i, data);
93 if (error)
94 return error;
95 }
96
97 return nr_zones;
98 }
99
null_zone_valid_read_len(struct nullb * nullb,sector_t sector,unsigned int len)100 size_t null_zone_valid_read_len(struct nullb *nullb,
101 sector_t sector, unsigned int len)
102 {
103 struct nullb_device *dev = nullb->dev;
104 struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
105 unsigned int nr_sectors = len >> SECTOR_SHIFT;
106
107 /* Read must be below the write pointer position */
108 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL ||
109 sector + nr_sectors <= zone->wp)
110 return len;
111
112 if (sector > zone->wp)
113 return 0;
114
115 return (zone->wp - sector) << SECTOR_SHIFT;
116 }
117
null_zone_write(struct nullb_cmd * cmd,sector_t sector,unsigned int nr_sectors)118 static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector,
119 unsigned int nr_sectors)
120 {
121 struct nullb_device *dev = cmd->nq->dev;
122 unsigned int zno = null_zone_no(dev, sector);
123 struct blk_zone *zone = &dev->zones[zno];
124
125 switch (zone->cond) {
126 case BLK_ZONE_COND_FULL:
127 /* Cannot write to a full zone */
128 cmd->error = BLK_STS_IOERR;
129 return BLK_STS_IOERR;
130 case BLK_ZONE_COND_EMPTY:
131 case BLK_ZONE_COND_IMP_OPEN:
132 /* Writes must be at the write pointer position */
133 if (sector != zone->wp)
134 return BLK_STS_IOERR;
135
136 if (zone->cond == BLK_ZONE_COND_EMPTY)
137 zone->cond = BLK_ZONE_COND_IMP_OPEN;
138
139 zone->wp += nr_sectors;
140 if (zone->wp == zone->start + zone->len)
141 zone->cond = BLK_ZONE_COND_FULL;
142 break;
143 case BLK_ZONE_COND_NOT_WP:
144 break;
145 default:
146 /* Invalid zone condition */
147 return BLK_STS_IOERR;
148 }
149 return BLK_STS_OK;
150 }
151
null_zone_reset(struct nullb_cmd * cmd,sector_t sector)152 static blk_status_t null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
153 {
154 struct nullb_device *dev = cmd->nq->dev;
155 struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)];
156 size_t i;
157
158 switch (req_op(cmd->rq)) {
159 case REQ_OP_ZONE_RESET_ALL:
160 for (i = 0; i < dev->nr_zones; i++) {
161 if (zone[i].type == BLK_ZONE_TYPE_CONVENTIONAL)
162 continue;
163 zone[i].cond = BLK_ZONE_COND_EMPTY;
164 zone[i].wp = zone[i].start;
165 }
166 break;
167 case REQ_OP_ZONE_RESET:
168 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
169 return BLK_STS_IOERR;
170
171 zone->cond = BLK_ZONE_COND_EMPTY;
172 zone->wp = zone->start;
173 break;
174 default:
175 return BLK_STS_NOTSUPP;
176 }
177 return BLK_STS_OK;
178 }
179
null_handle_zoned(struct nullb_cmd * cmd,enum req_opf op,sector_t sector,sector_t nr_sectors)180 blk_status_t null_handle_zoned(struct nullb_cmd *cmd, enum req_opf op,
181 sector_t sector, sector_t nr_sectors)
182 {
183 switch (op) {
184 case REQ_OP_WRITE:
185 return null_zone_write(cmd, sector, nr_sectors);
186 case REQ_OP_ZONE_RESET:
187 case REQ_OP_ZONE_RESET_ALL:
188 return null_zone_reset(cmd, sector);
189 default:
190 return BLK_STS_OK;
191 }
192 }
193