This source file includes following definitions.
- null_zone_no
- null_zone_init
- null_zone_exit
- null_zone_report
- null_zone_write
- null_zone_reset
- null_handle_zoned
1
2 #include <linux/vmalloc.h>
3 #include "null_blk.h"
4
5
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("zone_size must be power-of-two\n");
21 return -EINVAL;
22 }
23 if (dev->zone_size > dev->size) {
24 pr_err("Zone size larger than device capacity\n");
25 return -EINVAL;
26 }
27
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;
35
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);
40 }
41
42 for (i = 0; i < dev->zone_nr_conv; i++) {
43 struct blk_zone *zone = &dev->zones[i];
44
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;
50
51 sector += dev->zone_size_sects;
52 }
53
54 for (i = dev->zone_nr_conv; i < dev->nr_zones; i++) {
55 struct blk_zone *zone = &dev->zones[i];
56
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;
61
62 sector += dev->zone_size_sects;
63 }
64
65 return 0;
66 }
67
68 void null_zone_exit(struct nullb_device *dev)
69 {
70 kvfree(dev->zones);
71 }
72
73 int null_zone_report(struct gendisk *disk, sector_t sector,
74 struct blk_zone *zones, unsigned int *nr_zones)
75 {
76 struct nullb *nullb = disk->private_data;
77 struct nullb_device *dev = nullb->dev;
78 unsigned int zno, nrz = 0;
79
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));
84 }
85
86 *nr_zones = nrz;
87
88 return 0;
89 }
90
91 static blk_status_t null_zone_write(struct nullb_cmd *cmd, sector_t sector,
92 unsigned int nr_sectors)
93 {
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];
97
98 switch (zone->cond) {
99 case BLK_ZONE_COND_FULL:
100
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
106 if (sector != zone->wp)
107 return BLK_STS_IOERR;
108
109 if (zone->cond == BLK_ZONE_COND_EMPTY)
110 zone->cond = BLK_ZONE_COND_IMP_OPEN;
111
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
120 return BLK_STS_IOERR;
121 }
122 return BLK_STS_OK;
123 }
124
125 static blk_status_t null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
126 {
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;
131
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;
139 }
140 break;
141 case REQ_OP_ZONE_RESET:
142 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
143 return BLK_STS_IOERR;
144
145 zone->cond = BLK_ZONE_COND_EMPTY;
146 zone->wp = zone->start;
147 break;
148 default:
149 return BLK_STS_NOTSUPP;
150 }
151 return BLK_STS_OK;
152 }
153
154 blk_status_t null_handle_zoned(struct nullb_cmd *cmd, enum req_opf op,
155 sector_t sector, sector_t nr_sectors)
156 {
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;
165 }
166 }