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)) { |
| 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 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; |
| 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ø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 | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 69 | int null_zone_report(struct gendisk *disk, sector_t sector, |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 70 | struct blk_zone *zones, unsigned int *nr_zones) |
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 | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 74 | unsigned int zno, nrz = 0; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 75 | |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 76 | zno = null_zone_no(dev, sector); |
| 77 | if (zno < dev->nr_zones) { |
| 78 | nrz = min_t(unsigned int, *nr_zones, dev->nr_zones - zno); |
| 79 | memcpy(zones, &dev->zones[zno], nrz * sizeof(struct blk_zone)); |
| 80 | } |
| 81 | |
| 82 | *nr_zones = nrz; |
| 83 | |
| 84 | return 0; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 87 | 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] | 88 | unsigned int nr_sectors) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 89 | { |
| 90 | struct nullb_device *dev = cmd->nq->dev; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 91 | unsigned int zno = null_zone_no(dev, sector); |
| 92 | struct blk_zone *zone = &dev->zones[zno]; |
| 93 | |
| 94 | switch (zone->cond) { |
| 95 | case BLK_ZONE_COND_FULL: |
| 96 | /* Cannot write to a full zone */ |
| 97 | cmd->error = BLK_STS_IOERR; |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 98 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 99 | case BLK_ZONE_COND_EMPTY: |
| 100 | case BLK_ZONE_COND_IMP_OPEN: |
| 101 | /* Writes must be at the write pointer position */ |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 102 | if (sector != zone->wp) |
| 103 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 104 | |
| 105 | if (zone->cond == BLK_ZONE_COND_EMPTY) |
| 106 | zone->cond = BLK_ZONE_COND_IMP_OPEN; |
| 107 | |
Jens Axboe | b228ba1 | 2018-09-12 18:21:11 -0600 | [diff] [blame] | 108 | zone->wp += nr_sectors; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 109 | if (zone->wp == zone->start + zone->len) |
| 110 | zone->cond = BLK_ZONE_COND_FULL; |
| 111 | break; |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 112 | case BLK_ZONE_COND_NOT_WP: |
| 113 | break; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 114 | default: |
| 115 | /* Invalid zone condition */ |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 116 | return BLK_STS_IOERR; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 117 | } |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 118 | return BLK_STS_OK; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 121 | static blk_status_t null_zone_reset(struct nullb_cmd *cmd, sector_t sector) |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 122 | { |
| 123 | struct nullb_device *dev = cmd->nq->dev; |
Jens Axboe | b228ba1 | 2018-09-12 18:21:11 -0600 | [diff] [blame] | 124 | unsigned int zno = null_zone_no(dev, sector); |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 125 | struct blk_zone *zone = &dev->zones[zno]; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 126 | size_t i; |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 127 | |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 128 | switch (req_op(cmd->rq)) { |
| 129 | case REQ_OP_ZONE_RESET_ALL: |
| 130 | for (i = 0; i < dev->nr_zones; i++) { |
| 131 | if (zone[i].type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 132 | continue; |
| 133 | zone[i].cond = BLK_ZONE_COND_EMPTY; |
| 134 | zone[i].wp = zone[i].start; |
| 135 | } |
| 136 | break; |
| 137 | case REQ_OP_ZONE_RESET: |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 138 | if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) |
| 139 | return BLK_STS_IOERR; |
Chaitanya Kulkarni | a61dbfb | 2019-08-01 10:26:38 -0700 | [diff] [blame] | 140 | |
| 141 | zone->cond = BLK_ZONE_COND_EMPTY; |
| 142 | zone->wp = zone->start; |
| 143 | break; |
| 144 | default: |
| 145 | cmd->error = BLK_STS_NOTSUPP; |
| 146 | break; |
Masato Suzuki | ea2c18e | 2018-10-30 16:14:05 +0900 | [diff] [blame] | 147 | } |
Chaitanya Kulkarni | fceb5d1 | 2019-08-22 21:45:18 -0700 | [diff] [blame^] | 148 | return BLK_STS_OK; |
| 149 | } |
| 150 | |
| 151 | inline blk_status_t null_handle_zoned(struct nullb_cmd *cmd, |
| 152 | enum req_opf op, sector_t sector, |
| 153 | sector_t nr_sectors) |
| 154 | { |
| 155 | switch (op) { |
| 156 | case REQ_OP_WRITE: |
| 157 | return null_zone_write(cmd, sector, nr_sectors); |
| 158 | case REQ_OP_ZONE_RESET: |
| 159 | case REQ_OP_ZONE_RESET_ALL: |
| 160 | return null_zone_reset(cmd, sector); |
| 161 | default: |
| 162 | return BLK_STS_OK; |
| 163 | } |
Matias Bjørling | ca4b2a0 | 2018-07-06 19:38:39 +0200 | [diff] [blame] | 164 | } |