Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/vmalloc.h> |
| 3 | #include "null_blk.h" |
| 4 | |
Chaitanya Kulkarni | 766c329 | 2020-03-25 10:49:56 -0700 | [diff] [blame] | 5 | #define CREATE_TRACE_POINTS |
| 6 | #include "null_blk_trace.h" |
| 7 | |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 8 | /* zone_size in MBs to sectors. */ |
| 9 | #define ZONE_SIZE_SHIFT 11 |
| 10 | |
| 11 | static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect) |
| 12 | { |
| 13 | return sect >> ilog2(dev->zone_size_sects); |
| 14 | } |
| 15 | |
Damien Le Moal | d205bde | 2020-04-23 12:02:38 +0900 | [diff] [blame] | 16 | int null_init_zoned_dev(struct nullb_device *dev, struct request_queue *q) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 17 | { |
| 18 | sector_t dev_size = (sector_t)dev->size * 1024 * 1024; |
| 19 | sector_t sector = 0; |
| 20 | unsigned int i; |
| 21 | |
| 22 | if (!is_power_of_2(dev->zone_size)) { |
André Almeida | 9c7eddf | 2019-09-16 11:07:59 -0300 | [diff] [blame] | 23 | pr_err("zone_size must be power-of-two\n"); |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 24 | return -EINVAL; |
| 25 | } |
Chaitanya Kulkarni | e274832 | 2020-05-20 16:01:51 -0700 | [diff] [blame] | 26 | if (dev->zone_size > dev->size) { |
| 27 | pr_err("Zone size larger than device capacity\n"); |
| 28 | return -EINVAL; |
| 29 | } |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 30 | |
Aravind Ramesh | 089565f | 2020-06-29 12:06:38 -0700 | [diff] [blame] | 31 | if (!dev->zone_capacity) |
| 32 | dev->zone_capacity = dev->zone_size; |
| 33 | |
| 34 | if (dev->zone_capacity > dev->zone_size) { |
| 35 | pr_err("null_blk: zone capacity (%lu MB) larger than zone size (%lu MB)\n", |
| 36 | dev->zone_capacity, dev->zone_size); |
| 37 | return -EINVAL; |
| 38 | } |
| 39 | |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 40 | dev->zone_size_sects = dev->zone_size << ZONE_SIZE_SHIFT; |
| 41 | dev->nr_zones = dev_size >> |
| 42 | (SECTOR_SHIFT + ilog2(dev->zone_size_sects)); |
| 43 | dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone), |
| 44 | GFP_KERNEL | __GFP_ZERO); |
| 45 | if (!dev->zones) |
| 46 | return -ENOMEM; |
| 47 | |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 48 | if (dev->zone_nr_conv >= dev->nr_zones) { |
| 49 | dev->zone_nr_conv = dev->nr_zones - 1; |
André Almeida | 9c7eddf | 2019-09-16 11:07:59 -0300 | [diff] [blame] | 50 | pr_info("changed the number of conventional zones to %u", |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 51 | dev->zone_nr_conv); |
| 52 | } |
| 53 | |
| 54 | for (i = 0; i < dev->zone_nr_conv; i++) { |
| 55 | struct blk_zone *zone = &dev->zones[i]; |
| 56 | |
| 57 | zone->start = sector; |
| 58 | zone->len = dev->zone_size_sects; |
Matias Bjørling | 82394db | 2020-06-29 12:06:37 -0700 | [diff] [blame] | 59 | zone->capacity = zone->len; |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 60 | zone->wp = zone->start + zone->len; |
| 61 | zone->type = BLK_ZONE_TYPE_CONVENTIONAL; |
| 62 | zone->cond = BLK_ZONE_COND_NOT_WP; |
| 63 | |
| 64 | sector += dev->zone_size_sects; |
| 65 | } |
| 66 | |
| 67 | for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) { |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 68 | struct blk_zone *zone = &dev->zones[i]; |
| 69 | |
| 70 | zone->start = zone->wp = sector; |
| 71 | zone->len = dev->zone_size_sects; |
Aravind Ramesh | 089565f | 2020-06-29 12:06:38 -0700 | [diff] [blame] | 72 | zone->capacity = dev->zone_capacity << ZONE_SIZE_SHIFT; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 73 | zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ; |
| 74 | zone->cond = BLK_ZONE_COND_EMPTY; |
| 75 | |
| 76 | sector += dev->zone_size_sects; |
| 77 | } |
| 78 | |
Damien Le Moal | d205bde | 2020-04-23 12:02:38 +0900 | [diff] [blame] | 79 | q->limits.zoned = BLK_ZONED_HM; |
| 80 | blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q); |
| 81 | blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE); |
| 82 | |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Damien Le Moal | d205bde | 2020-04-23 12:02:38 +0900 | [diff] [blame] | 86 | int null_register_zoned_dev(struct nullb *nullb) |
| 87 | { |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 88 | struct nullb_device *dev = nullb->dev; |
Damien Le Moal | d205bde | 2020-04-23 12:02:38 +0900 | [diff] [blame] | 89 | struct request_queue *q = nullb->q; |
| 90 | |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 91 | if (queue_is_mq(q)) { |
| 92 | int ret = blk_revalidate_disk_zones(nullb->disk, NULL); |
Damien Le Moal | d205bde | 2020-04-23 12:02:38 +0900 | [diff] [blame] | 93 | |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 94 | if (ret) |
| 95 | return ret; |
| 96 | } else { |
| 97 | blk_queue_chunk_sectors(q, dev->zone_size_sects); |
| 98 | q->nr_zones = blkdev_nr_zones(nullb->disk); |
| 99 | } |
| 100 | |
| 101 | blk_queue_max_zone_append_sectors(q, dev->zone_size_sects); |
Damien Le Moal | d205bde | 2020-04-23 12:02:38 +0900 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | void null_free_zoned_dev(struct nullb_device *dev) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 107 | { |
| 108 | kvfree(dev->zones); |
| 109 | } |
| 110 | |
Christoph Hellwig | 7fc8fb5 | 2019-11-11 11:39:27 +0900 | [diff] [blame] | 111 | int null_report_zones(struct gendisk *disk, sector_t sector, |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 112 | unsigned int nr_zones, report_zones_cb cb, void *data) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 113 | { |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 114 | struct nullb *nullb = disk->private_data; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 115 | struct nullb_device *dev = nullb->dev; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 116 | unsigned int first_zone, i; |
| 117 | struct blk_zone zone; |
| 118 | int error; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 119 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 120 | first_zone = null_zone_no(dev, sector); |
| 121 | if (first_zone >= dev->nr_zones) |
| 122 | return 0; |
| 123 | |
| 124 | nr_zones = min(nr_zones, dev->nr_zones - first_zone); |
Chaitanya Kulkarni | 766c329 | 2020-03-25 10:49:56 -0700 | [diff] [blame] | 125 | trace_nullb_report_zones(nullb, nr_zones); |
| 126 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 127 | for (i = 0; i < nr_zones; i++) { |
| 128 | /* |
| 129 | * Stacked DM target drivers will remap the zone information by |
| 130 | * modifying the zone information passed to the report callback. |
| 131 | * So use a local copy to avoid corruption of the device zone |
| 132 | * array. |
| 133 | */ |
| 134 | memcpy(&zone, &dev->zones[first_zone + i], |
| 135 | sizeof(struct blk_zone)); |
| 136 | error = cb(&zone, i, data); |
| 137 | if (error) |
| 138 | return error; |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 141 | return nr_zones; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Ajay Joshi | dd85b49 | 2019-10-17 14:19:43 -0700 | [diff] [blame] | 144 | size_t null_zone_valid_read_len(struct nullb *nullb, |
| 145 | sector_t sector, unsigned int len) |
| 146 | { |
| 147 | struct nullb_device *dev = nullb->dev; |
| 148 | struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)]; |
| 149 | unsigned int nr_sectors = len >> SECTOR_SHIFT; |
| 150 | |
| 151 | /* Read must be below the write pointer position */ |
| 152 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL || |
| 153 | sector + nr_sectors <= zone->wp) |
| 154 | return len; |
| 155 | |
| 156 | if (sector > zone->wp) |
| 157 | return 0; |
| 158 | |
| 159 | return (zone->wp - sector) << SECTOR_SHIFT; |
| 160 | } |
| 161 | |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 162 | static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector, |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 163 | unsigned int nr_sectors, bool append) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 164 | { |
| 165 | struct nullb_device *dev = cmd->nq->dev; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 166 | unsigned int zno = null_zone_no(dev, sector); |
| 167 | struct blk_zone *zone = &dev->zones[zno]; |
Damien Le Moal | 9dd44c7 | 2020-04-23 12:02:37 +0900 | [diff] [blame] | 168 | blk_status_t ret; |
| 169 | |
| 170 | trace_nullb_zone_op(cmd, zno, zone->cond); |
| 171 | |
| 172 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 173 | return null_process_cmd(cmd, REQ_OP_WRITE, sector, nr_sectors); |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 174 | |
| 175 | switch (zone->cond) { |
| 176 | case BLK_ZONE_COND_FULL: |
| 177 | /* Cannot write to a full zone */ |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 178 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 179 | case BLK_ZONE_COND_EMPTY: |
| 180 | case BLK_ZONE_COND_IMP_OPEN: |
Damien Le Moal | 16c731f | 2020-01-09 14:03:55 +0900 | [diff] [blame] | 181 | case BLK_ZONE_COND_EXP_OPEN: |
| 182 | case BLK_ZONE_COND_CLOSED: |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 183 | /* |
| 184 | * Regular writes must be at the write pointer position. |
| 185 | * Zone append writes are automatically issued at the write |
| 186 | * pointer and the position returned using the request or BIO |
| 187 | * sector. |
| 188 | */ |
| 189 | if (append) { |
| 190 | sector = zone->wp; |
| 191 | if (cmd->bio) |
| 192 | cmd->bio->bi_iter.bi_sector = sector; |
| 193 | else |
| 194 | cmd->rq->__sector = sector; |
| 195 | } else if (sector != zone->wp) { |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 196 | return BLK_STS_IOERR; |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 197 | } |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 198 | |
Aravind Ramesh | 089565f | 2020-06-29 12:06:38 -0700 | [diff] [blame] | 199 | if (zone->wp + nr_sectors > zone->start + zone->capacity) |
| 200 | return BLK_STS_IOERR; |
| 201 | |
Damien Le Moal | 16c731f | 2020-01-09 14:03:55 +0900 | [diff] [blame] | 202 | if (zone->cond != BLK_ZONE_COND_EXP_OPEN) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 203 | zone->cond = BLK_ZONE_COND_IMP_OPEN; |
| 204 | |
Damien Le Moal | 9dd44c7 | 2020-04-23 12:02:37 +0900 | [diff] [blame] | 205 | ret = null_process_cmd(cmd, REQ_OP_WRITE, sector, nr_sectors); |
| 206 | if (ret != BLK_STS_OK) |
| 207 | return ret; |
| 208 | |
Jens Axboe | b228ba1 | 2018-09-12 18:21:11 -0600 | [diff] [blame] | 209 | zone->wp += nr_sectors; |
Aravind Ramesh | 089565f | 2020-06-29 12:06:38 -0700 | [diff] [blame] | 210 | if (zone->wp == zone->start + zone->capacity) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 211 | zone->cond = BLK_ZONE_COND_FULL; |
Damien Le Moal | 9dd44c7 | 2020-04-23 12:02:37 +0900 | [diff] [blame] | 212 | return BLK_STS_OK; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 213 | default: |
| 214 | /* Invalid zone condition */ |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 215 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 219 | static blk_status_t null_zone_mgmt(struct nullb_cmd *cmd, enum req_opf op, |
| 220 | sector_t sector) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 221 | { |
| 222 | struct nullb_device *dev = cmd->nq->dev; |
Chaitanya Kulkarni | 766c329 | 2020-03-25 10:49:56 -0700 | [diff] [blame] | 223 | unsigned int zone_no = null_zone_no(dev, sector); |
| 224 | struct blk_zone *zone = &dev->zones[zone_no]; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 225 | size_t i; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 226 | |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 227 | switch (op) { |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 228 | case REQ_OP_ZONE_RESET_ALL: |
| 229 | for (i = 0; i < dev->nr_zones; i++) { |
| 230 | if (zone[i].type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 231 | continue; |
| 232 | zone[i].cond = BLK_ZONE_COND_EMPTY; |
| 233 | zone[i].wp = zone[i].start; |
| 234 | } |
| 235 | break; |
| 236 | case REQ_OP_ZONE_RESET: |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 237 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 238 | return BLK_STS_IOERR; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 239 | |
| 240 | zone->cond = BLK_ZONE_COND_EMPTY; |
| 241 | zone->wp = zone->start; |
| 242 | break; |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 243 | case REQ_OP_ZONE_OPEN: |
| 244 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 245 | return BLK_STS_IOERR; |
| 246 | if (zone->cond == BLK_ZONE_COND_FULL) |
| 247 | return BLK_STS_IOERR; |
| 248 | |
| 249 | zone->cond = BLK_ZONE_COND_EXP_OPEN; |
| 250 | break; |
| 251 | case REQ_OP_ZONE_CLOSE: |
| 252 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 253 | return BLK_STS_IOERR; |
| 254 | if (zone->cond == BLK_ZONE_COND_FULL) |
| 255 | return BLK_STS_IOERR; |
| 256 | |
Damien Le Moal | c7d776f | 2019-12-26 15:54:25 +0900 | [diff] [blame] | 257 | if (zone->wp == zone->start) |
| 258 | zone->cond = BLK_ZONE_COND_EMPTY; |
| 259 | else |
| 260 | zone->cond = BLK_ZONE_COND_CLOSED; |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 261 | break; |
| 262 | case REQ_OP_ZONE_FINISH: |
| 263 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 264 | return BLK_STS_IOERR; |
| 265 | |
| 266 | zone->cond = BLK_ZONE_COND_FULL; |
| 267 | zone->wp = zone->start + zone->len; |
| 268 | break; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 269 | default: |
Keith Busch | 79a85e2 | 2019-10-10 00:38:13 +0900 | [diff] [blame] | 270 | return BLK_STS_NOTSUPP; |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 271 | } |
Chaitanya Kulkarni | 766c329 | 2020-03-25 10:49:56 -0700 | [diff] [blame] | 272 | |
| 273 | trace_nullb_zone_op(cmd, zone_no, zone->cond); |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 274 | return BLK_STS_OK; |
| 275 | } |
| 276 | |
Damien Le Moal | 9dd44c7 | 2020-04-23 12:02:37 +0900 | [diff] [blame] | 277 | blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_opf op, |
| 278 | sector_t sector, sector_t nr_sectors) |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 279 | { |
| 280 | switch (op) { |
| 281 | case REQ_OP_WRITE: |
Damien Le Moal | e0489ed | 2020-05-12 17:55:52 +0900 | [diff] [blame] | 282 | return null_zone_write(cmd, sector, nr_sectors, false); |
| 283 | case REQ_OP_ZONE_APPEND: |
| 284 | return null_zone_write(cmd, sector, nr_sectors, true); |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 285 | case REQ_OP_ZONE_RESET: |
| 286 | case REQ_OP_ZONE_RESET_ALL: |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 287 | case REQ_OP_ZONE_OPEN: |
| 288 | case REQ_OP_ZONE_CLOSE: |
| 289 | case REQ_OP_ZONE_FINISH: |
| 290 | return null_zone_mgmt(cmd, op, sector); |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 291 | default: |
Damien Le Moal | 9dd44c7 | 2020-04-23 12:02:37 +0900 | [diff] [blame] | 292 | return null_process_cmd(cmd, op, sector, nr_sectors); |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 293 | } |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 294 | } |