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 | |
| 5 | /* zone_size in MBs to sectors. */ |
| 6 | #define ZONE_SIZE_SHIFT 11 |
| 7 | |
| 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 | |
| 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)) { |
André Almeida | 9c7eddf | 2019-09-16 11:07:59 -0300 | [diff] [blame] | 20 | pr_err("zone_size must be power-of-two\n"); |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 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 Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 32 | if (dev->zone_nr_conv >= dev->nr_zones) { |
| 33 | dev->zone_nr_conv = dev->nr_zones - 1; |
André Almeida | 9c7eddf | 2019-09-16 11:07:59 -0300 | [diff] [blame] | 34 | pr_info("changed the number of conventional zones to %u", |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 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ørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 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 | |
| 64 | void null_zone_exit(struct nullb_device *dev) |
| 65 | { |
| 66 | kvfree(dev->zones); |
| 67 | } |
| 68 | |
Christoph Hellwig | 7fc8fb5 | 2019-11-11 11:39:27 +0900 | [diff] [blame] | 69 | int null_report_zones(struct gendisk *disk, sector_t sector, |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 70 | unsigned int nr_zones, report_zones_cb cb, void *data) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 71 | { |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 72 | struct nullb *nullb = disk->private_data; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 73 | struct nullb_device *dev = nullb->dev; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 74 | unsigned int first_zone, i; |
| 75 | struct blk_zone zone; |
| 76 | int error; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 77 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 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; |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 97 | return nr_zones; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Ajay Joshi | dd85b49 | 2019-10-17 14:19:43 -0700 | [diff] [blame] | 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 | |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 118 | static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector, |
Jens Axboe | b228ba1 | 2018-09-12 18:21:11 -0600 | [diff] [blame] | 119 | unsigned int nr_sectors) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 120 | { |
| 121 | struct nullb_device *dev = cmd->nq->dev; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 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; |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 129 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 130 | case BLK_ZONE_COND_EMPTY: |
| 131 | case BLK_ZONE_COND_IMP_OPEN: |
Damien Le Moal | 16c731f | 2020-01-09 14:03:55 +0900 | [diff] [blame] | 132 | case BLK_ZONE_COND_EXP_OPEN: |
| 133 | case BLK_ZONE_COND_CLOSED: |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 134 | /* Writes must be at the write pointer position */ |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 135 | if (sector != zone->wp) |
| 136 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 137 | |
Damien Le Moal | 16c731f | 2020-01-09 14:03:55 +0900 | [diff] [blame] | 138 | if (zone->cond != BLK_ZONE_COND_EXP_OPEN) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 139 | zone->cond = BLK_ZONE_COND_IMP_OPEN; |
| 140 | |
Jens Axboe | b228ba1 | 2018-09-12 18:21:11 -0600 | [diff] [blame] | 141 | zone->wp += nr_sectors; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 142 | if (zone->wp == zone->start + zone->len) |
| 143 | zone->cond = BLK_ZONE_COND_FULL; |
| 144 | break; |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 145 | case BLK_ZONE_COND_NOT_WP: |
| 146 | break; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 147 | default: |
| 148 | /* Invalid zone condition */ |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 149 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 150 | } |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 151 | return BLK_STS_OK; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 154 | static blk_status_t null_zone_mgmt(struct nullb_cmd *cmd, enum req_opf op, |
| 155 | sector_t sector) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 156 | { |
| 157 | struct nullb_device *dev = cmd->nq->dev; |
Ajay Joshi | dd85b49 | 2019-10-17 14:19:43 -0700 | [diff] [blame] | 158 | struct blk_zone *zone = &dev->zones[null_zone_no(dev, sector)]; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 159 | size_t i; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 160 | |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 161 | switch (op) { |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 162 | case REQ_OP_ZONE_RESET_ALL: |
| 163 | for (i = 0; i < dev->nr_zones; i++) { |
| 164 | if (zone[i].type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 165 | continue; |
| 166 | zone[i].cond = BLK_ZONE_COND_EMPTY; |
| 167 | zone[i].wp = zone[i].start; |
| 168 | } |
| 169 | break; |
| 170 | case REQ_OP_ZONE_RESET: |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 171 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 172 | return BLK_STS_IOERR; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 173 | |
| 174 | zone->cond = BLK_ZONE_COND_EMPTY; |
| 175 | zone->wp = zone->start; |
| 176 | break; |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 177 | case REQ_OP_ZONE_OPEN: |
| 178 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 179 | return BLK_STS_IOERR; |
| 180 | if (zone->cond == BLK_ZONE_COND_FULL) |
| 181 | return BLK_STS_IOERR; |
| 182 | |
| 183 | zone->cond = BLK_ZONE_COND_EXP_OPEN; |
| 184 | break; |
| 185 | case REQ_OP_ZONE_CLOSE: |
| 186 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 187 | return BLK_STS_IOERR; |
| 188 | if (zone->cond == BLK_ZONE_COND_FULL) |
| 189 | return BLK_STS_IOERR; |
| 190 | |
Damien Le Moal | c7d776f | 2019-12-26 15:54:25 +0900 | [diff] [blame] | 191 | if (zone->wp == zone->start) |
| 192 | zone->cond = BLK_ZONE_COND_EMPTY; |
| 193 | else |
| 194 | zone->cond = BLK_ZONE_COND_CLOSED; |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 195 | break; |
| 196 | case REQ_OP_ZONE_FINISH: |
| 197 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 198 | return BLK_STS_IOERR; |
| 199 | |
| 200 | zone->cond = BLK_ZONE_COND_FULL; |
| 201 | zone->wp = zone->start + zone->len; |
| 202 | break; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 203 | default: |
Keith Busch | 79a85e2 | 2019-10-10 00:38:13 +0900 | [diff] [blame] | 204 | return BLK_STS_NOTSUPP; |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 205 | } |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 206 | return BLK_STS_OK; |
| 207 | } |
| 208 | |
Jens Axboe | 38b4e09 | 2019-08-23 12:49:00 -0600 | [diff] [blame] | 209 | blk_status_t null_handle_zoned(struct nullb_cmd *cmd, enum req_opf op, |
| 210 | sector_t sector, sector_t nr_sectors) |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 211 | { |
| 212 | switch (op) { |
| 213 | case REQ_OP_WRITE: |
| 214 | return null_zone_write(cmd, sector, nr_sectors); |
| 215 | case REQ_OP_ZONE_RESET: |
| 216 | case REQ_OP_ZONE_RESET_ALL: |
Ajay Joshi | da644b2 | 2019-10-27 23:05:49 +0900 | [diff] [blame] | 217 | case REQ_OP_ZONE_OPEN: |
| 218 | case REQ_OP_ZONE_CLOSE: |
| 219 | case REQ_OP_ZONE_FINISH: |
| 220 | return null_zone_mgmt(cmd, op, sector); |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame] | 221 | default: |
| 222 | return BLK_STS_OK; |
| 223 | } |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 224 | } |