net: DCB: Validate DCB_ATTR_DCB_BUFFER argument
[linux/fpc-iii.git] / drivers / block / null_blk_zoned.c
blob2553e05e07253a99dba9f6b917212ba83d17ee6b
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/vmalloc.h>
3 #include "null_blk.h"
5 /* zone_size in MBs to sectors. */
6 #define ZONE_SIZE_SHIFT 11
8 static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
10 return sect >> ilog2(dev->zone_size_sects);
13 int null_zone_init(struct nullb_device *dev)
15 sector_t dev_size = (sector_t)dev->size * 1024 * 1024;
16 sector_t sector = 0;
17 unsigned int i;
19 if (!is_power_of_2(dev->zone_size)) {
20 pr_err("zone_size must be power-of-two\n");
21 return -EINVAL;
23 if (dev->zone_size > dev->size) {
24 pr_err("Zone size larger than device capacity\n");
25 return -EINVAL;
28 dev->zone_size_sects = dev->zone_size << ZONE_SIZE_SHIFT;
29 dev->nr_zones = dev_size >>
30 (SECTOR_SHIFT + ilog2(dev->zone_size_sects));
31 dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone),
32 GFP_KERNEL | __GFP_ZERO);
33 if (!dev->zones)
34 return -ENOMEM;
36 if (dev->zone_nr_conv >= dev->nr_zones) {
37 dev->zone_nr_conv = dev->nr_zones - 1;
38 pr_info("changed the number of conventional zones to %u",
39 dev->zone_nr_conv);
42 for (i = 0; i < dev->zone_nr_conv; i++) {
43 struct blk_zone *zone = &dev->zones[i];
45 zone->start = sector;
46 zone->len = dev->zone_size_sects;
47 zone->wp = zone->start + zone->len;
48 zone->type = BLK_ZONE_TYPE_CONVENTIONAL;
49 zone->cond = BLK_ZONE_COND_NOT_WP;
51 sector += dev->zone_size_sects;
54 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
55 struct blk_zone *zone = &dev->zones[i];
57 zone->start = zone->wp = sector;
58 zone->len = dev->zone_size_sects;
59 zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
60 zone->cond = BLK_ZONE_COND_EMPTY;
62 sector += dev->zone_size_sects;
65 return 0;
68 void null_zone_exit(struct nullb_device *dev)
70 kvfree(dev->zones);
73 int null_zone_report(struct gendisk *disk, sector_t sector,
74 struct blk_zone *zones, unsigned int *nr_zones)
76 struct nullb *nullb = disk->private_data;
77 struct nullb_device *dev = nullb->dev;
78 unsigned int zno, nrz = 0;
80 zno = null_zone_no(dev, sector);
81 if (zno < dev->nr_zones) {
82 nrz = min_t(unsigned int, *nr_zones, dev->nr_zones - zno);
83 memcpy(zones, &dev->zones[zno], nrz * sizeof(struct blk_zone));
86 *nr_zones = nrz;
88 return 0;
91 static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector,
92 unsigned int nr_sectors)
94 struct nullb_device *dev = cmd->nq->dev;
95 unsigned int zno = null_zone_no(dev, sector);
96 struct blk_zone *zone = &dev->zones[zno];
98 switch (zone->cond) {
99 case BLK_ZONE_COND_FULL:
100 /* Cannot write to a full zone */
101 cmd->error = BLK_STS_IOERR;
102 return BLK_STS_IOERR;
103 case BLK_ZONE_COND_EMPTY:
104 case BLK_ZONE_COND_IMP_OPEN:
105 /* Writes must be at the write pointer position */
106 if (sector != zone->wp)
107 return BLK_STS_IOERR;
109 if (zone->cond == BLK_ZONE_COND_EMPTY)
110 zone->cond = BLK_ZONE_COND_IMP_OPEN;
112 zone->wp += nr_sectors;
113 if (zone->wp == zone->start + zone->len)
114 zone->cond = BLK_ZONE_COND_FULL;
115 break;
116 case BLK_ZONE_COND_NOT_WP:
117 break;
118 default:
119 /* Invalid zone condition */
120 return BLK_STS_IOERR;
122 return BLK_STS_OK;
125 static blk_status_t null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
127 struct nullb_device *dev = cmd->nq->dev;
128 unsigned int zno = null_zone_no(dev, sector);
129 struct blk_zone *zone = &dev->zones[zno];
130 size_t i;
132 switch (req_op(cmd->rq)) {
133 case REQ_OP_ZONE_RESET_ALL:
134 for (i = 0; i < dev->nr_zones; i++) {
135 if (zone[i].type == BLK_ZONE_TYPE_CONVENTIONAL)
136 continue;
137 zone[i].cond = BLK_ZONE_COND_EMPTY;
138 zone[i].wp = zone[i].start;
140 break;
141 case REQ_OP_ZONE_RESET:
142 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
143 return BLK_STS_IOERR;
145 zone->cond = BLK_ZONE_COND_EMPTY;
146 zone->wp = zone->start;
147 break;
148 default:
149 return BLK_STS_NOTSUPP;
151 return BLK_STS_OK;
154 blk_status_t null_handle_zoned(struct nullb_cmd *cmd, enum req_opf op,
155 sector_t sector, sector_t nr_sectors)
157 switch (op) {
158 case REQ_OP_WRITE:
159 return null_zone_write(cmd, sector, nr_sectors);
160 case REQ_OP_ZONE_RESET:
161 case REQ_OP_ZONE_RESET_ALL:
162 return null_zone_reset(cmd, sector);
163 default:
164 return BLK_STS_OK;