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);
73 static inline unsigned int __blkdev_nr_zones(struct request_queue *q,
76 sector_t zone_sectors = blk_queue_zone_sectors(q);
78 return (nr_sectors + zone_sectors - 1) >> ilog2(zone_sectors);
82 * blkdev_nr_zones - Get number of zones
83 * @bdev: Target block device
86 * Return the total number of zones of a zoned block device.
87 * For a regular block device, the number of zones is always 0.
89 unsigned int blkdev_nr_zones(struct block_device *bdev)
91 struct request_queue *q = bdev_get_queue(bdev);
93 if (!blk_queue_is_zoned(q))
96 return __blkdev_nr_zones(q, get_capacity(bdev->bd_disk));
98 EXPORT_SYMBOL_GPL(blkdev_nr_zones);
101 * blkdev_report_zones - Get zones information
102 * @bdev: Target block device
103 * @sector: Sector from which to report zones
104 * @nr_zones: Maximum number of zones to report
105 * @cb: Callback function called for each reported zone
106 * @data: Private data for the callback
109 * Get zone information starting from the zone containing @sector for at most
110 * @nr_zones, and call @cb for each zone reported by the device.
111 * To report all zones in a device starting from @sector, the BLK_ALL_ZONES
112 * constant can be passed to @nr_zones.
113 * Returns the number of zones reported by the device, or a negative errno
114 * value in case of failure.
116 * Note: The caller must use memalloc_noXX_save/restore() calls to control
117 * memory allocations done within this function.
119 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
120 unsigned int nr_zones, report_zones_cb cb, void *data)
122 struct gendisk *disk = bdev->bd_disk;
123 sector_t capacity = get_capacity(disk);
125 if (!blk_queue_is_zoned(bdev_get_queue(bdev)) ||
126 WARN_ON_ONCE(!disk->fops->report_zones))
129 if (!nr_zones || sector >= capacity)
132 return disk->fops->report_zones(disk, sector, nr_zones, cb, data);
134 EXPORT_SYMBOL_GPL(blkdev_report_zones);
136 static inline bool blkdev_allow_reset_all_zones(struct block_device *bdev,
140 if (!blk_queue_zone_resetall(bdev_get_queue(bdev)))
144 * REQ_OP_ZONE_RESET_ALL can be executed only if the number of sectors
145 * of the applicable zone range is the entire disk.
147 return !sector && nr_sectors == get_capacity(bdev->bd_disk);
151 * blkdev_zone_mgmt - Execute a zone management operation on a range of zones
152 * @bdev: Target block device
153 * @op: Operation to be performed on the zones
154 * @sector: Start sector of the first zone to operate on
155 * @nr_sectors: Number of sectors, should be at least the length of one zone and
156 * must be zone size aligned.
157 * @gfp_mask: Memory allocation flags (for bio_alloc)
160 * Perform the specified operation on the range of zones specified by
161 * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
162 * is valid, but the specified range should not contain conventional zones.
163 * The operation to execute on each zone can be a zone reset, open, close
166 int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
167 sector_t sector, sector_t nr_sectors,
170 struct request_queue *q = bdev_get_queue(bdev);
171 sector_t zone_sectors = blk_queue_zone_sectors(q);
172 sector_t capacity = get_capacity(bdev->bd_disk);
173 sector_t end_sector = sector + nr_sectors;
174 struct bio *bio = NULL;
177 if (!blk_queue_is_zoned(q))
180 if (bdev_read_only(bdev))
183 if (!op_is_zone_mgmt(op))
186 if (!nr_sectors || end_sector > capacity)
190 /* Check alignment (handle eventual smaller last zone) */
191 if (sector & (zone_sectors - 1))
194 if ((nr_sectors & (zone_sectors - 1)) && end_sector != capacity)
197 while (sector < end_sector) {
198 bio = blk_next_bio(bio, 0, gfp_mask);
199 bio_set_dev(bio, bdev);
202 * Special case for the zone reset operation that reset all
203 * zones, this is useful for applications like mkfs.
205 if (op == REQ_OP_ZONE_RESET &&
206 blkdev_allow_reset_all_zones(bdev, sector, nr_sectors)) {
207 bio->bi_opf = REQ_OP_ZONE_RESET_ALL;
212 bio->bi_iter.bi_sector = sector;
213 sector += zone_sectors;
215 /* This may take a while, so be nice to others */
219 ret = submit_bio_wait(bio);
224 EXPORT_SYMBOL_GPL(blkdev_zone_mgmt);
226 struct zone_report_args {
227 struct blk_zone __user *zones;
230 static int blkdev_copy_zone_to_user(struct blk_zone *zone, unsigned int idx,
233 struct zone_report_args *args = data;
235 if (copy_to_user(&args->zones[idx], zone, sizeof(struct blk_zone)))
241 * BLKREPORTZONE ioctl processing.
242 * Called from blkdev_ioctl.
244 int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
245 unsigned int cmd, unsigned long arg)
247 void __user *argp = (void __user *)arg;
248 struct zone_report_args args;
249 struct request_queue *q;
250 struct blk_zone_report rep;
256 q = bdev_get_queue(bdev);
260 if (!blk_queue_is_zoned(q))
263 if (!capable(CAP_SYS_ADMIN))
266 if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report)))
272 args.zones = argp + sizeof(struct blk_zone_report);
273 ret = blkdev_report_zones(bdev, rep.sector, rep.nr_zones,
274 blkdev_copy_zone_to_user, &args);
279 if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report)))
285 * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
286 * Called from blkdev_ioctl.
288 int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
289 unsigned int cmd, unsigned long arg)
291 void __user *argp = (void __user *)arg;
292 struct request_queue *q;
293 struct blk_zone_range zrange;
299 q = bdev_get_queue(bdev);
303 if (!blk_queue_is_zoned(q))
306 if (!capable(CAP_SYS_ADMIN))
309 if (!(mode & FMODE_WRITE))
312 if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))
317 op = REQ_OP_ZONE_RESET;
320 op = REQ_OP_ZONE_OPEN;
323 op = REQ_OP_ZONE_CLOSE;
326 op = REQ_OP_ZONE_FINISH;
332 return blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors,
336 static inline unsigned long *blk_alloc_zone_bitmap(int node,
337 unsigned int nr_zones)
339 return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
343 void blk_queue_free_zone_bitmaps(struct request_queue *q)
345 kfree(q->seq_zones_bitmap);
346 q->seq_zones_bitmap = NULL;
347 kfree(q->seq_zones_wlock);
348 q->seq_zones_wlock = NULL;
351 struct blk_revalidate_zone_args {
352 struct gendisk *disk;
353 unsigned long *seq_zones_bitmap;
354 unsigned long *seq_zones_wlock;
359 * Helper function to check the validity of zones of a zoned block device.
361 static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
364 struct blk_revalidate_zone_args *args = data;
365 struct gendisk *disk = args->disk;
366 struct request_queue *q = disk->queue;
367 sector_t zone_sectors = blk_queue_zone_sectors(q);
368 sector_t capacity = get_capacity(disk);
371 * All zones must have the same size, with the exception on an eventual
374 if (zone->start + zone_sectors < capacity &&
375 zone->len != zone_sectors) {
376 pr_warn("%s: Invalid zoned device with non constant zone size\n",
381 if (zone->start + zone->len >= capacity &&
382 zone->len > zone_sectors) {
383 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 case BLK_ZONE_TYPE_SEQWRITE_REQ:
399 case BLK_ZONE_TYPE_SEQWRITE_PREF:
402 pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n",
403 disk->disk_name, (int)zone->type, zone->start);
407 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL)
408 set_bit(idx, args->seq_zones_bitmap);
410 args->sector += zone->len;
414 static int blk_update_zone_info(struct gendisk *disk, unsigned int nr_zones,
415 struct blk_revalidate_zone_args *args)
418 * Ensure that all memory allocations in this context are done as
419 * if GFP_NOIO was specified.
421 unsigned int noio_flag = memalloc_noio_save();
422 struct request_queue *q = disk->queue;
425 args->seq_zones_wlock = blk_alloc_zone_bitmap(q->node, nr_zones);
426 if (!args->seq_zones_wlock)
428 args->seq_zones_bitmap = blk_alloc_zone_bitmap(q->node, nr_zones);
429 if (!args->seq_zones_bitmap)
432 ret = disk->fops->report_zones(disk, 0, nr_zones,
433 blk_revalidate_zone_cb, args);
434 memalloc_noio_restore(noio_flag);
439 * blk_revalidate_disk_zones - (re)allocate and initialize zone bitmaps
442 * Helper function for low-level device drivers to (re) allocate and initialize
443 * a disk request queue zone bitmaps. This functions should normally be called
444 * within the disk ->revalidate method. For BIO based queues, no zone bitmap
447 int blk_revalidate_disk_zones(struct gendisk *disk)
449 struct request_queue *q = disk->queue;
450 unsigned int nr_zones = __blkdev_nr_zones(q, get_capacity(disk));
451 struct blk_revalidate_zone_args args = { .disk = disk };
454 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
458 * BIO based queues do not use a scheduler so only q->nr_zones
459 * needs to be updated so that the sysfs exposed value is correct.
461 if (!queue_is_mq(q)) {
462 q->nr_zones = nr_zones;
467 ret = blk_update_zone_info(disk, nr_zones, &args);
470 * Install the new bitmaps, making sure the queue is stopped and
471 * all I/Os are completed (i.e. a scheduler is not referencing the
474 blk_mq_freeze_queue(q);
476 q->nr_zones = nr_zones;
477 swap(q->seq_zones_wlock, args.seq_zones_wlock);
478 swap(q->seq_zones_bitmap, args.seq_zones_bitmap);
481 pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
482 blk_queue_free_zone_bitmaps(q);
484 blk_mq_unfreeze_queue(q);
486 kfree(args.seq_zones_wlock);
487 kfree(args.seq_zones_bitmap);
490 EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones);