1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/vmalloc.h>
5 #define CREATE_TRACE_POINTS
6 #include "null_blk_trace.h"
8 /* zone_size in MBs to sectors. */
9 #define ZONE_SIZE_SHIFT 11
11 static inline unsigned int null_zone_no(struct nullb_device
*dev
, sector_t sect
)
13 return sect
>> ilog2(dev
->zone_size_sects
);
16 int null_init_zoned_dev(struct nullb_device
*dev
, struct request_queue
*q
)
18 sector_t dev_size
= (sector_t
)dev
->size
* 1024 * 1024;
22 if (!is_power_of_2(dev
->zone_size
)) {
23 pr_err("zone_size must be power-of-two\n");
27 dev
->zone_size_sects
= dev
->zone_size
<< ZONE_SIZE_SHIFT
;
28 dev
->nr_zones
= dev_size
>>
29 (SECTOR_SHIFT
+ ilog2(dev
->zone_size_sects
));
30 dev
->zones
= kvmalloc_array(dev
->nr_zones
, sizeof(struct blk_zone
),
31 GFP_KERNEL
| __GFP_ZERO
);
35 if (dev
->zone_nr_conv
>= dev
->nr_zones
) {
36 dev
->zone_nr_conv
= dev
->nr_zones
- 1;
37 pr_info("changed the number of conventional zones to %u",
41 for (i
= 0; i
< dev
->zone_nr_conv
; i
++) {
42 struct blk_zone
*zone
= &dev
->zones
[i
];
45 zone
->len
= dev
->zone_size_sects
;
46 zone
->wp
= zone
->start
+ zone
->len
;
47 zone
->type
= BLK_ZONE_TYPE_CONVENTIONAL
;
48 zone
->cond
= BLK_ZONE_COND_NOT_WP
;
50 sector
+= dev
->zone_size_sects
;
53 for (i
= dev
->zone_nr_conv
; i
< dev
->nr_zones
; i
++) {
54 struct blk_zone
*zone
= &dev
->zones
[i
];
56 zone
->start
= zone
->wp
= sector
;
57 zone
->len
= dev
->zone_size_sects
;
58 zone
->type
= BLK_ZONE_TYPE_SEQWRITE_REQ
;
59 zone
->cond
= BLK_ZONE_COND_EMPTY
;
61 sector
+= dev
->zone_size_sects
;
64 q
->limits
.zoned
= BLK_ZONED_HM
;
65 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL
, q
);
66 blk_queue_required_elevator_features(q
, ELEVATOR_F_ZBD_SEQ_WRITE
);
71 int null_register_zoned_dev(struct nullb
*nullb
)
73 struct request_queue
*q
= nullb
->q
;
76 return blk_revalidate_disk_zones(nullb
->disk
);
78 blk_queue_chunk_sectors(q
, nullb
->dev
->zone_size_sects
);
79 q
->nr_zones
= blkdev_nr_zones(nullb
->disk
);
84 void null_free_zoned_dev(struct nullb_device
*dev
)
89 int null_report_zones(struct gendisk
*disk
, sector_t sector
,
90 unsigned int nr_zones
, report_zones_cb cb
, void *data
)
92 struct nullb
*nullb
= disk
->private_data
;
93 struct nullb_device
*dev
= nullb
->dev
;
94 unsigned int first_zone
, i
;
98 first_zone
= null_zone_no(dev
, sector
);
99 if (first_zone
>= dev
->nr_zones
)
102 nr_zones
= min(nr_zones
, dev
->nr_zones
- first_zone
);
103 trace_nullb_report_zones(nullb
, nr_zones
);
105 for (i
= 0; i
< nr_zones
; i
++) {
107 * Stacked DM target drivers will remap the zone information by
108 * modifying the zone information passed to the report callback.
109 * So use a local copy to avoid corruption of the device zone
112 memcpy(&zone
, &dev
->zones
[first_zone
+ i
],
113 sizeof(struct blk_zone
));
114 error
= cb(&zone
, i
, data
);
122 size_t null_zone_valid_read_len(struct nullb
*nullb
,
123 sector_t sector
, unsigned int len
)
125 struct nullb_device
*dev
= nullb
->dev
;
126 struct blk_zone
*zone
= &dev
->zones
[null_zone_no(dev
, sector
)];
127 unsigned int nr_sectors
= len
>> SECTOR_SHIFT
;
129 /* Read must be below the write pointer position */
130 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
||
131 sector
+ nr_sectors
<= zone
->wp
)
134 if (sector
> zone
->wp
)
137 return (zone
->wp
- sector
) << SECTOR_SHIFT
;
140 static blk_status_t
null_zone_write(struct nullb_cmd
*cmd
, sector_t sector
,
141 unsigned int nr_sectors
)
143 struct nullb_device
*dev
= cmd
->nq
->dev
;
144 unsigned int zno
= null_zone_no(dev
, sector
);
145 struct blk_zone
*zone
= &dev
->zones
[zno
];
148 trace_nullb_zone_op(cmd
, zno
, zone
->cond
);
150 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
151 return null_process_cmd(cmd
, REQ_OP_WRITE
, sector
, nr_sectors
);
153 switch (zone
->cond
) {
154 case BLK_ZONE_COND_FULL
:
155 /* Cannot write to a full zone */
156 return BLK_STS_IOERR
;
157 case BLK_ZONE_COND_EMPTY
:
158 case BLK_ZONE_COND_IMP_OPEN
:
159 case BLK_ZONE_COND_EXP_OPEN
:
160 case BLK_ZONE_COND_CLOSED
:
161 /* Writes must be at the write pointer position */
162 if (sector
!= zone
->wp
)
163 return BLK_STS_IOERR
;
165 if (zone
->cond
!= BLK_ZONE_COND_EXP_OPEN
)
166 zone
->cond
= BLK_ZONE_COND_IMP_OPEN
;
168 ret
= null_process_cmd(cmd
, REQ_OP_WRITE
, sector
, nr_sectors
);
169 if (ret
!= BLK_STS_OK
)
172 zone
->wp
+= nr_sectors
;
173 if (zone
->wp
== zone
->start
+ zone
->len
)
174 zone
->cond
= BLK_ZONE_COND_FULL
;
177 /* Invalid zone condition */
178 return BLK_STS_IOERR
;
182 static blk_status_t
null_zone_mgmt(struct nullb_cmd
*cmd
, enum req_opf op
,
185 struct nullb_device
*dev
= cmd
->nq
->dev
;
186 unsigned int zone_no
= null_zone_no(dev
, sector
);
187 struct blk_zone
*zone
= &dev
->zones
[zone_no
];
191 case REQ_OP_ZONE_RESET_ALL
:
192 for (i
= 0; i
< dev
->nr_zones
; i
++) {
193 if (zone
[i
].type
== BLK_ZONE_TYPE_CONVENTIONAL
)
195 zone
[i
].cond
= BLK_ZONE_COND_EMPTY
;
196 zone
[i
].wp
= zone
[i
].start
;
199 case REQ_OP_ZONE_RESET
:
200 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
201 return BLK_STS_IOERR
;
203 zone
->cond
= BLK_ZONE_COND_EMPTY
;
204 zone
->wp
= zone
->start
;
206 case REQ_OP_ZONE_OPEN
:
207 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
208 return BLK_STS_IOERR
;
209 if (zone
->cond
== BLK_ZONE_COND_FULL
)
210 return BLK_STS_IOERR
;
212 zone
->cond
= BLK_ZONE_COND_EXP_OPEN
;
214 case REQ_OP_ZONE_CLOSE
:
215 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
216 return BLK_STS_IOERR
;
217 if (zone
->cond
== BLK_ZONE_COND_FULL
)
218 return BLK_STS_IOERR
;
220 if (zone
->wp
== zone
->start
)
221 zone
->cond
= BLK_ZONE_COND_EMPTY
;
223 zone
->cond
= BLK_ZONE_COND_CLOSED
;
225 case REQ_OP_ZONE_FINISH
:
226 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
227 return BLK_STS_IOERR
;
229 zone
->cond
= BLK_ZONE_COND_FULL
;
230 zone
->wp
= zone
->start
+ zone
->len
;
233 return BLK_STS_NOTSUPP
;
236 trace_nullb_zone_op(cmd
, zone_no
, zone
->cond
);
240 blk_status_t
null_process_zoned_cmd(struct nullb_cmd
*cmd
, enum req_opf op
,
241 sector_t sector
, sector_t nr_sectors
)
245 return null_zone_write(cmd
, sector
, nr_sectors
);
246 case REQ_OP_ZONE_RESET
:
247 case REQ_OP_ZONE_RESET_ALL
:
248 case REQ_OP_ZONE_OPEN
:
249 case REQ_OP_ZONE_CLOSE
:
250 case REQ_OP_ZONE_FINISH
:
251 return null_zone_mgmt(cmd
, op
, sector
);
253 return null_process_cmd(cmd
, op
, sector
, nr_sectors
);