1 // SPDX-License-Identifier: GPL-2.0
3 * Zoned block device handling
5 * Copyright (c) 2015, Hannes Reinecke
6 * Copyright (c) 2015, SUSE Linux GmbH
8 * Copyright (c) 2016, Damien Le Moal
9 * Copyright (c) 2016, Western Digital
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/rbtree.h>
15 #include <linux/blkdev.h>
16 #include <linux/blk-mq.h>
18 #include <linux/vmalloc.h>
19 #include <linux/sched/mm.h>
23 static inline sector_t
blk_zone_start(struct request_queue
*q
,
26 sector_t zone_mask
= blk_queue_zone_sectors(q
) - 1;
28 return sector
& ~zone_mask
;
32 * Return true if a request is a write requests that needs zone write locking.
34 bool blk_req_needs_zone_write_lock(struct request
*rq
)
36 if (!rq
->q
->seq_zones_wlock
)
39 if (blk_rq_is_passthrough(rq
))
43 case REQ_OP_WRITE_ZEROES
:
44 case REQ_OP_WRITE_SAME
:
46 return blk_rq_zone_is_seq(rq
);
51 EXPORT_SYMBOL_GPL(blk_req_needs_zone_write_lock
);
53 void __blk_req_zone_write_lock(struct request
*rq
)
55 if (WARN_ON_ONCE(test_and_set_bit(blk_rq_zone_no(rq
),
56 rq
->q
->seq_zones_wlock
)))
59 WARN_ON_ONCE(rq
->rq_flags
& RQF_ZONE_WRITE_LOCKED
);
60 rq
->rq_flags
|= RQF_ZONE_WRITE_LOCKED
;
62 EXPORT_SYMBOL_GPL(__blk_req_zone_write_lock
);
64 void __blk_req_zone_write_unlock(struct request
*rq
)
66 rq
->rq_flags
&= ~RQF_ZONE_WRITE_LOCKED
;
67 if (rq
->q
->seq_zones_wlock
)
68 WARN_ON_ONCE(!test_and_clear_bit(blk_rq_zone_no(rq
),
69 rq
->q
->seq_zones_wlock
));
71 EXPORT_SYMBOL_GPL(__blk_req_zone_write_unlock
);
74 * blkdev_nr_zones - Get number of zones
75 * @disk: Target gendisk
77 * Return the total number of zones of a zoned block device. For a block
78 * device without zone capabilities, the number of zones is always 0.
80 unsigned int blkdev_nr_zones(struct gendisk
*disk
)
82 sector_t zone_sectors
= blk_queue_zone_sectors(disk
->queue
);
84 if (!blk_queue_is_zoned(disk
->queue
))
86 return (get_capacity(disk
) + zone_sectors
- 1) >> ilog2(zone_sectors
);
88 EXPORT_SYMBOL_GPL(blkdev_nr_zones
);
91 * blkdev_report_zones - Get zones information
92 * @bdev: Target block device
93 * @sector: Sector from which to report zones
94 * @nr_zones: Maximum number of zones to report
95 * @cb: Callback function called for each reported zone
96 * @data: Private data for the callback
99 * Get zone information starting from the zone containing @sector for at most
100 * @nr_zones, and call @cb for each zone reported by the device.
101 * To report all zones in a device starting from @sector, the BLK_ALL_ZONES
102 * constant can be passed to @nr_zones.
103 * Returns the number of zones reported by the device, or a negative errno
104 * value in case of failure.
106 * Note: The caller must use memalloc_noXX_save/restore() calls to control
107 * memory allocations done within this function.
109 int blkdev_report_zones(struct block_device
*bdev
, sector_t sector
,
110 unsigned int nr_zones
, report_zones_cb cb
, void *data
)
112 struct gendisk
*disk
= bdev
->bd_disk
;
113 sector_t capacity
= get_capacity(disk
);
115 if (!blk_queue_is_zoned(bdev_get_queue(bdev
)) ||
116 WARN_ON_ONCE(!disk
->fops
->report_zones
))
119 if (!nr_zones
|| sector
>= capacity
)
122 return disk
->fops
->report_zones(disk
, sector
, nr_zones
, cb
, data
);
124 EXPORT_SYMBOL_GPL(blkdev_report_zones
);
126 static inline bool blkdev_allow_reset_all_zones(struct block_device
*bdev
,
130 if (!blk_queue_zone_resetall(bdev_get_queue(bdev
)))
134 * REQ_OP_ZONE_RESET_ALL can be executed only if the number of sectors
135 * of the applicable zone range is the entire disk.
137 return !sector
&& nr_sectors
== get_capacity(bdev
->bd_disk
);
141 * blkdev_zone_mgmt - Execute a zone management operation on a range of zones
142 * @bdev: Target block device
143 * @op: Operation to be performed on the zones
144 * @sector: Start sector of the first zone to operate on
145 * @nr_sectors: Number of sectors, should be at least the length of one zone and
146 * must be zone size aligned.
147 * @gfp_mask: Memory allocation flags (for bio_alloc)
150 * Perform the specified operation on the range of zones specified by
151 * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
152 * is valid, but the specified range should not contain conventional zones.
153 * The operation to execute on each zone can be a zone reset, open, close
156 int blkdev_zone_mgmt(struct block_device
*bdev
, enum req_opf op
,
157 sector_t sector
, sector_t nr_sectors
,
160 struct request_queue
*q
= bdev_get_queue(bdev
);
161 sector_t zone_sectors
= blk_queue_zone_sectors(q
);
162 sector_t capacity
= get_capacity(bdev
->bd_disk
);
163 sector_t end_sector
= sector
+ nr_sectors
;
164 struct bio
*bio
= NULL
;
167 if (!blk_queue_is_zoned(q
))
170 if (bdev_read_only(bdev
))
173 if (!op_is_zone_mgmt(op
))
176 if (!nr_sectors
|| end_sector
> capacity
)
180 /* Check alignment (handle eventual smaller last zone) */
181 if (sector
& (zone_sectors
- 1))
184 if ((nr_sectors
& (zone_sectors
- 1)) && end_sector
!= capacity
)
187 while (sector
< end_sector
) {
188 bio
= blk_next_bio(bio
, 0, gfp_mask
);
189 bio_set_dev(bio
, bdev
);
192 * Special case for the zone reset operation that reset all
193 * zones, this is useful for applications like mkfs.
195 if (op
== REQ_OP_ZONE_RESET
&&
196 blkdev_allow_reset_all_zones(bdev
, sector
, nr_sectors
)) {
197 bio
->bi_opf
= REQ_OP_ZONE_RESET_ALL
;
201 bio
->bi_opf
= op
| REQ_SYNC
;
202 bio
->bi_iter
.bi_sector
= sector
;
203 sector
+= zone_sectors
;
205 /* This may take a while, so be nice to others */
209 ret
= submit_bio_wait(bio
);
214 EXPORT_SYMBOL_GPL(blkdev_zone_mgmt
);
216 struct zone_report_args
{
217 struct blk_zone __user
*zones
;
220 static int blkdev_copy_zone_to_user(struct blk_zone
*zone
, unsigned int idx
,
223 struct zone_report_args
*args
= data
;
225 if (copy_to_user(&args
->zones
[idx
], zone
, sizeof(struct blk_zone
)))
231 * BLKREPORTZONE ioctl processing.
232 * Called from blkdev_ioctl.
234 int blkdev_report_zones_ioctl(struct block_device
*bdev
, fmode_t mode
,
235 unsigned int cmd
, unsigned long arg
)
237 void __user
*argp
= (void __user
*)arg
;
238 struct zone_report_args args
;
239 struct request_queue
*q
;
240 struct blk_zone_report rep
;
246 q
= bdev_get_queue(bdev
);
250 if (!blk_queue_is_zoned(q
))
253 if (!capable(CAP_SYS_ADMIN
))
256 if (copy_from_user(&rep
, argp
, sizeof(struct blk_zone_report
)))
262 args
.zones
= argp
+ sizeof(struct blk_zone_report
);
263 ret
= blkdev_report_zones(bdev
, rep
.sector
, rep
.nr_zones
,
264 blkdev_copy_zone_to_user
, &args
);
269 if (copy_to_user(argp
, &rep
, sizeof(struct blk_zone_report
)))
275 * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
276 * Called from blkdev_ioctl.
278 int blkdev_zone_mgmt_ioctl(struct block_device
*bdev
, fmode_t mode
,
279 unsigned int cmd
, unsigned long arg
)
281 void __user
*argp
= (void __user
*)arg
;
282 struct request_queue
*q
;
283 struct blk_zone_range zrange
;
289 q
= bdev_get_queue(bdev
);
293 if (!blk_queue_is_zoned(q
))
296 if (!capable(CAP_SYS_ADMIN
))
299 if (!(mode
& FMODE_WRITE
))
302 if (copy_from_user(&zrange
, argp
, sizeof(struct blk_zone_range
)))
307 op
= REQ_OP_ZONE_RESET
;
310 op
= REQ_OP_ZONE_OPEN
;
313 op
= REQ_OP_ZONE_CLOSE
;
316 op
= REQ_OP_ZONE_FINISH
;
322 return blkdev_zone_mgmt(bdev
, op
, zrange
.sector
, zrange
.nr_sectors
,
326 static inline unsigned long *blk_alloc_zone_bitmap(int node
,
327 unsigned int nr_zones
)
329 return kcalloc_node(BITS_TO_LONGS(nr_zones
), sizeof(unsigned long),
333 void blk_queue_free_zone_bitmaps(struct request_queue
*q
)
335 kfree(q
->conv_zones_bitmap
);
336 q
->conv_zones_bitmap
= NULL
;
337 kfree(q
->seq_zones_wlock
);
338 q
->seq_zones_wlock
= NULL
;
341 struct blk_revalidate_zone_args
{
342 struct gendisk
*disk
;
343 unsigned long *conv_zones_bitmap
;
344 unsigned long *seq_zones_wlock
;
345 unsigned int nr_zones
;
346 sector_t zone_sectors
;
351 * Helper function to check the validity of zones of a zoned block device.
353 static int blk_revalidate_zone_cb(struct blk_zone
*zone
, unsigned int idx
,
356 struct blk_revalidate_zone_args
*args
= data
;
357 struct gendisk
*disk
= args
->disk
;
358 struct request_queue
*q
= disk
->queue
;
359 sector_t capacity
= get_capacity(disk
);
362 * All zones must have the same size, with the exception on an eventual
365 if (zone
->start
== 0) {
366 if (zone
->len
== 0 || !is_power_of_2(zone
->len
)) {
367 pr_warn("%s: Invalid zoned device with non power of two zone size (%llu)\n",
368 disk
->disk_name
, zone
->len
);
372 args
->zone_sectors
= zone
->len
;
373 args
->nr_zones
= (capacity
+ zone
->len
- 1) >> ilog2(zone
->len
);
374 } else if (zone
->start
+ args
->zone_sectors
< capacity
) {
375 if (zone
->len
!= args
->zone_sectors
) {
376 pr_warn("%s: Invalid zoned device with non constant zone size\n",
381 if (zone
->len
> args
->zone_sectors
) {
382 pr_warn("%s: Invalid zoned device with larger last zone size\n",
388 /* Check for holes in the zone report */
389 if (zone
->start
!= args
->sector
) {
390 pr_warn("%s: Zone gap at sectors %llu..%llu\n",
391 disk
->disk_name
, args
->sector
, zone
->start
);
395 /* Check zone type */
396 switch (zone
->type
) {
397 case BLK_ZONE_TYPE_CONVENTIONAL
:
398 if (!args
->conv_zones_bitmap
) {
399 args
->conv_zones_bitmap
=
400 blk_alloc_zone_bitmap(q
->node
, args
->nr_zones
);
401 if (!args
->conv_zones_bitmap
)
404 set_bit(idx
, args
->conv_zones_bitmap
);
406 case BLK_ZONE_TYPE_SEQWRITE_REQ
:
407 case BLK_ZONE_TYPE_SEQWRITE_PREF
:
408 if (!args
->seq_zones_wlock
) {
409 args
->seq_zones_wlock
=
410 blk_alloc_zone_bitmap(q
->node
, args
->nr_zones
);
411 if (!args
->seq_zones_wlock
)
416 pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n",
417 disk
->disk_name
, (int)zone
->type
, zone
->start
);
421 args
->sector
+= zone
->len
;
426 * blk_revalidate_disk_zones - (re)allocate and initialize zone bitmaps
429 * Helper function for low-level device drivers to (re) allocate and initialize
430 * a disk request queue zone bitmaps. This functions should normally be called
431 * within the disk ->revalidate method for blk-mq based drivers. For BIO based
432 * drivers only q->nr_zones needs to be updated so that the sysfs exposed value
435 int blk_revalidate_disk_zones(struct gendisk
*disk
)
437 struct request_queue
*q
= disk
->queue
;
438 struct blk_revalidate_zone_args args
= {
441 unsigned int noio_flag
;
444 if (WARN_ON_ONCE(!blk_queue_is_zoned(q
)))
446 if (WARN_ON_ONCE(!queue_is_mq(q
)))
450 * Ensure that all memory allocations in this context are done as if
451 * GFP_NOIO was specified.
453 noio_flag
= memalloc_noio_save();
454 ret
= disk
->fops
->report_zones(disk
, 0, UINT_MAX
,
455 blk_revalidate_zone_cb
, &args
);
456 memalloc_noio_restore(noio_flag
);
459 * Install the new bitmaps and update nr_zones only once the queue is
460 * stopped and all I/Os are completed (i.e. a scheduler is not
461 * referencing the bitmaps).
463 blk_mq_freeze_queue(q
);
465 blk_queue_chunk_sectors(q
, args
.zone_sectors
);
466 q
->nr_zones
= args
.nr_zones
;
467 swap(q
->seq_zones_wlock
, args
.seq_zones_wlock
);
468 swap(q
->conv_zones_bitmap
, args
.conv_zones_bitmap
);
471 pr_warn("%s: failed to revalidate zones\n", disk
->disk_name
);
472 blk_queue_free_zone_bitmaps(q
);
474 blk_mq_unfreeze_queue(q
);
476 kfree(args
.seq_zones_wlock
);
477 kfree(args
.conv_zones_bitmap
);
480 EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones
);