blob: ae665e49085898c77f99795dda12b85314f1fa32 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +09002/*
3 * Zoned block device handling
4 *
5 * Copyright (c) 2015, Hannes Reinecke
6 * Copyright (c) 2015, SUSE Linux GmbH
7 *
8 * Copyright (c) 2016, Damien Le Moal
9 * Copyright (c) 2016, Western Digital
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/rbtree.h>
15#include <linux/blkdev.h>
Damien Le Moalbf505452018-10-12 19:08:50 +090016#include <linux/blk-mq.h>
Damien Le Moal26202922019-07-01 14:09:18 +090017#include <linux/mm.h>
18#include <linux/vmalloc.h>
Damien Le Moalbd976e52019-07-01 14:09:16 +090019#include <linux/sched/mm.h>
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +090020
Damien Le Moala2d6b3a2018-10-12 19:08:47 +090021#include "blk.h"
22
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +090023static inline sector_t blk_zone_start(struct request_queue *q,
24 sector_t sector)
25{
Damien Le Moalf99e8642017-01-12 07:58:32 -070026 sector_t zone_mask = blk_queue_zone_sectors(q) - 1;
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +090027
28 return sector & ~zone_mask;
29}
30
31/*
Christoph Hellwig6cc77e92017-12-21 15:43:38 +090032 * Return true if a request is a write requests that needs zone write locking.
33 */
34bool blk_req_needs_zone_write_lock(struct request *rq)
35{
36 if (!rq->q->seq_zones_wlock)
37 return false;
38
39 if (blk_rq_is_passthrough(rq))
40 return false;
41
42 switch (req_op(rq)) {
43 case REQ_OP_WRITE_ZEROES:
44 case REQ_OP_WRITE_SAME:
45 case REQ_OP_WRITE:
46 return blk_rq_zone_is_seq(rq);
47 default:
48 return false;
49 }
50}
51EXPORT_SYMBOL_GPL(blk_req_needs_zone_write_lock);
52
53void __blk_req_zone_write_lock(struct request *rq)
54{
55 if (WARN_ON_ONCE(test_and_set_bit(blk_rq_zone_no(rq),
56 rq->q->seq_zones_wlock)))
57 return;
58
59 WARN_ON_ONCE(rq->rq_flags & RQF_ZONE_WRITE_LOCKED);
60 rq->rq_flags |= RQF_ZONE_WRITE_LOCKED;
61}
62EXPORT_SYMBOL_GPL(__blk_req_zone_write_lock);
63
64void __blk_req_zone_write_unlock(struct request *rq)
65{
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));
70}
71EXPORT_SYMBOL_GPL(__blk_req_zone_write_unlock);
72
Damien Le Moala91e1382018-10-12 19:08:43 +090073static inline unsigned int __blkdev_nr_zones(struct request_queue *q,
74 sector_t nr_sectors)
75{
Damien Le Moal113ab722019-07-10 13:53:10 +090076 sector_t zone_sectors = blk_queue_zone_sectors(q);
Damien Le Moala91e1382018-10-12 19:08:43 +090077
78 return (nr_sectors + zone_sectors - 1) >> ilog2(zone_sectors);
79}
80
81/**
82 * blkdev_nr_zones - Get number of zones
83 * @bdev: Target block device
84 *
85 * Description:
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.
88 */
89unsigned int blkdev_nr_zones(struct block_device *bdev)
90{
91 struct request_queue *q = bdev_get_queue(bdev);
92
93 if (!blk_queue_is_zoned(q))
94 return 0;
95
Damien Le Moal5eac3eb2019-11-11 11:39:25 +090096 return __blkdev_nr_zones(q, get_capacity(bdev->bd_disk));
Damien Le Moala91e1382018-10-12 19:08:43 +090097}
98EXPORT_SYMBOL_GPL(blkdev_nr_zones);
99
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900100/**
101 * blkdev_report_zones - Get zones information
102 * @bdev: Target block device
103 * @sector: Sector from which to report zones
104 * @zones: Array of zone structures where to return the zones information
105 * @nr_zones: Number of zone structures in the zone array
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900106 *
107 * Description:
108 * Get zone information starting from the zone containing @sector.
109 * The number of zone information reported may be less than the number
110 * requested by @nr_zones. The number of zones actually reported is
111 * returned in @nr_zones.
Damien Le Moalbd976e52019-07-01 14:09:16 +0900112 * The caller must use memalloc_noXX_save/restore() calls to control
113 * memory allocations done within this function (zone array and command
114 * buffer allocation by the device driver).
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900115 */
Christoph Hellwige76239a2018-10-12 19:08:49 +0900116int blkdev_report_zones(struct block_device *bdev, sector_t sector,
Damien Le Moalbd976e52019-07-01 14:09:16 +0900117 struct blk_zone *zones, unsigned int *nr_zones)
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900118{
119 struct request_queue *q = bdev_get_queue(bdev);
Damien Le Moalceeb3732019-11-11 11:39:24 +0900120 struct gendisk *disk = bdev->bd_disk;
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900121 sector_t capacity = get_capacity(disk);
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900122
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900123 if (!blk_queue_is_zoned(q))
124 return -EOPNOTSUPP;
125
Christoph Hellwige76239a2018-10-12 19:08:49 +0900126 /*
127 * A block device that advertized itself as zoned must have a
128 * report_zones method. If it does not have one defined, the device
129 * driver has a bug. So warn about that.
130 */
Damien Le Moalceeb3732019-11-11 11:39:24 +0900131 if (WARN_ON_ONCE(!disk->fops->report_zones))
Christoph Hellwige76239a2018-10-12 19:08:49 +0900132 return -EOPNOTSUPP;
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900133
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900134 if (!*nr_zones || sector >= capacity) {
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900135 *nr_zones = 0;
136 return 0;
137 }
138
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900139 *nr_zones = min(*nr_zones, __blkdev_nr_zones(q, capacity - sector));
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900140
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900141 return disk->fops->report_zones(disk, sector, zones, nr_zones);
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900142}
143EXPORT_SYMBOL_GPL(blkdev_report_zones);
144
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700145static inline bool blkdev_allow_reset_all_zones(struct block_device *bdev,
Damien Le Moalc7a1d922019-10-27 23:05:43 +0900146 sector_t sector,
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700147 sector_t nr_sectors)
148{
149 if (!blk_queue_zone_resetall(bdev_get_queue(bdev)))
150 return false;
151
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700152 /*
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900153 * REQ_OP_ZONE_RESET_ALL can be executed only if the number of sectors
154 * of the applicable zone range is the entire disk.
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700155 */
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900156 return !sector && nr_sectors == get_capacity(bdev->bd_disk);
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700157}
158
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900159/**
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900160 * blkdev_zone_mgmt - Execute a zone management operation on a range of zones
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900161 * @bdev: Target block device
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900162 * @op: Operation to be performed on the zones
163 * @sector: Start sector of the first zone to operate on
164 * @nr_sectors: Number of sectors, should be at least the length of one zone and
165 * must be zone size aligned.
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900166 * @gfp_mask: Memory allocation flags (for bio_alloc)
167 *
168 * Description:
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900169 * Perform the specified operation on the range of zones specified by
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900170 * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
171 * is valid, but the specified range should not contain conventional zones.
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900172 * The operation to execute on each zone can be a zone reset, open, close
173 * or finish request.
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900174 */
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900175int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
176 sector_t sector, sector_t nr_sectors,
177 gfp_t gfp_mask)
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900178{
179 struct request_queue *q = bdev_get_queue(bdev);
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900180 sector_t zone_sectors = blk_queue_zone_sectors(q);
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900181 sector_t capacity = get_capacity(bdev->bd_disk);
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900182 sector_t end_sector = sector + nr_sectors;
Damien Le Moala2d6b3a2018-10-12 19:08:47 +0900183 struct bio *bio = NULL;
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900184 int ret;
185
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900186 if (!blk_queue_is_zoned(q))
187 return -EOPNOTSUPP;
188
Damien Le Moala2d6b3a2018-10-12 19:08:47 +0900189 if (bdev_read_only(bdev))
190 return -EPERM;
191
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900192 if (!op_is_zone_mgmt(op))
193 return -EOPNOTSUPP;
194
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900195 if (!nr_sectors || end_sector > capacity)
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900196 /* Out of range */
197 return -EINVAL;
198
199 /* Check alignment (handle eventual smaller last zone) */
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900200 if (sector & (zone_sectors - 1))
201 return -EINVAL;
202
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900203 if ((nr_sectors & (zone_sectors - 1)) && end_sector != capacity)
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900204 return -EINVAL;
205
206 while (sector < end_sector) {
Damien Le Moala2d6b3a2018-10-12 19:08:47 +0900207 bio = blk_next_bio(bio, 0, gfp_mask);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200208 bio_set_dev(bio, bdev);
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900209
Damien Le Moalc7a1d922019-10-27 23:05:43 +0900210 /*
211 * Special case for the zone reset operation that reset all
212 * zones, this is useful for applications like mkfs.
213 */
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900214 if (op == REQ_OP_ZONE_RESET &&
215 blkdev_allow_reset_all_zones(bdev, sector, nr_sectors)) {
Damien Le Moalc7a1d922019-10-27 23:05:43 +0900216 bio->bi_opf = REQ_OP_ZONE_RESET_ALL;
217 break;
218 }
219
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900220 bio->bi_opf = op;
Damien Le Moalc7a1d922019-10-27 23:05:43 +0900221 bio->bi_iter.bi_sector = sector;
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900222 sector += zone_sectors;
223
224 /* This may take a while, so be nice to others */
225 cond_resched();
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900226 }
227
Damien Le Moala2d6b3a2018-10-12 19:08:47 +0900228 ret = submit_bio_wait(bio);
229 bio_put(bio);
230
Damien Le Moala2d6b3a2018-10-12 19:08:47 +0900231 return ret;
Hannes Reinecke6a0cb1b2016-10-18 15:40:33 +0900232}
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900233EXPORT_SYMBOL_GPL(blkdev_zone_mgmt);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900234
Bart Van Assche56c4bdd2018-03-08 15:28:50 -0800235/*
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900236 * BLKREPORTZONE ioctl processing.
237 * Called from blkdev_ioctl.
238 */
239int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
240 unsigned int cmd, unsigned long arg)
241{
242 void __user *argp = (void __user *)arg;
243 struct request_queue *q;
244 struct blk_zone_report rep;
245 struct blk_zone *zones;
246 int ret;
247
248 if (!argp)
249 return -EINVAL;
250
251 q = bdev_get_queue(bdev);
252 if (!q)
253 return -ENXIO;
254
255 if (!blk_queue_is_zoned(q))
256 return -ENOTTY;
257
258 if (!capable(CAP_SYS_ADMIN))
259 return -EACCES;
260
261 if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report)))
262 return -EFAULT;
263
264 if (!rep.nr_zones)
265 return -EINVAL;
266
Damien Le Moal2e85fba2018-10-12 19:08:44 +0900267 rep.nr_zones = min(blkdev_nr_zones(bdev), rep.nr_zones);
Bart Van Assche327ea4a2018-05-22 08:27:22 -0700268
Kees Cook344476e2018-06-12 14:04:32 -0700269 zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone),
270 GFP_KERNEL | __GFP_ZERO);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900271 if (!zones)
272 return -ENOMEM;
273
Damien Le Moalbd976e52019-07-01 14:09:16 +0900274 ret = blkdev_report_zones(bdev, rep.sector, zones, &rep.nr_zones);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900275 if (ret)
276 goto out;
277
278 if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report))) {
279 ret = -EFAULT;
280 goto out;
281 }
282
283 if (rep.nr_zones) {
284 if (copy_to_user(argp + sizeof(struct blk_zone_report), zones,
285 sizeof(struct blk_zone) * rep.nr_zones))
286 ret = -EFAULT;
287 }
288
289 out:
Bart Van Assche327ea4a2018-05-22 08:27:22 -0700290 kvfree(zones);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900291
292 return ret;
293}
294
Bart Van Assche56c4bdd2018-03-08 15:28:50 -0800295/*
Ajay Joshie876df12019-10-27 23:05:46 +0900296 * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900297 * Called from blkdev_ioctl.
298 */
Ajay Joshie876df12019-10-27 23:05:46 +0900299int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
300 unsigned int cmd, unsigned long arg)
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900301{
302 void __user *argp = (void __user *)arg;
303 struct request_queue *q;
304 struct blk_zone_range zrange;
Ajay Joshie876df12019-10-27 23:05:46 +0900305 enum req_opf op;
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900306
307 if (!argp)
308 return -EINVAL;
309
310 q = bdev_get_queue(bdev);
311 if (!q)
312 return -ENXIO;
313
314 if (!blk_queue_is_zoned(q))
315 return -ENOTTY;
316
317 if (!capable(CAP_SYS_ADMIN))
318 return -EACCES;
319
320 if (!(mode & FMODE_WRITE))
321 return -EBADF;
322
323 if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))
324 return -EFAULT;
325
Ajay Joshie876df12019-10-27 23:05:46 +0900326 switch (cmd) {
327 case BLKRESETZONE:
328 op = REQ_OP_ZONE_RESET;
329 break;
330 case BLKOPENZONE:
331 op = REQ_OP_ZONE_OPEN;
332 break;
333 case BLKCLOSEZONE:
334 op = REQ_OP_ZONE_CLOSE;
335 break;
336 case BLKFINISHZONE:
337 op = REQ_OP_ZONE_FINISH;
338 break;
339 default:
340 return -ENOTTY;
341 }
342
343 return blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors,
344 GFP_KERNEL);
Shaun Tancheff3ed05a92016-10-18 15:40:35 +0900345}
Damien Le Moalbf505452018-10-12 19:08:50 +0900346
347static inline unsigned long *blk_alloc_zone_bitmap(int node,
348 unsigned int nr_zones)
349{
350 return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
351 GFP_NOIO, node);
352}
353
354/*
355 * Allocate an array of struct blk_zone to get nr_zones zone information.
356 * The allocated array may be smaller than nr_zones.
357 */
Damien Le Moal26202922019-07-01 14:09:18 +0900358static struct blk_zone *blk_alloc_zones(unsigned int *nr_zones)
Damien Le Moalbf505452018-10-12 19:08:50 +0900359{
Damien Le Moal26202922019-07-01 14:09:18 +0900360 struct blk_zone *zones;
361 size_t nrz = min(*nr_zones, BLK_ZONED_REPORT_MAX_ZONES);
Damien Le Moalbf505452018-10-12 19:08:50 +0900362
Damien Le Moal26202922019-07-01 14:09:18 +0900363 /*
364 * GFP_KERNEL here is meaningless as the caller task context has
365 * the PF_MEMALLOC_NOIO flag set in blk_revalidate_disk_zones()
366 * with memalloc_noio_save().
367 */
368 zones = kvcalloc(nrz, sizeof(struct blk_zone), GFP_KERNEL);
369 if (!zones) {
370 *nr_zones = 0;
371 return NULL;
Damien Le Moalbf505452018-10-12 19:08:50 +0900372 }
373
Damien Le Moal26202922019-07-01 14:09:18 +0900374 *nr_zones = nrz;
375
376 return zones;
Damien Le Moalbf505452018-10-12 19:08:50 +0900377}
378
379void blk_queue_free_zone_bitmaps(struct request_queue *q)
380{
381 kfree(q->seq_zones_bitmap);
382 q->seq_zones_bitmap = NULL;
383 kfree(q->seq_zones_wlock);
384 q->seq_zones_wlock = NULL;
385}
386
Damien Le Moald9dd7302019-11-11 11:39:22 +0900387/*
388 * Helper function to check the validity of zones of a zoned block device.
389 */
390static bool blk_zone_valid(struct gendisk *disk, struct blk_zone *zone,
391 sector_t *sector)
392{
393 struct request_queue *q = disk->queue;
394 sector_t zone_sectors = blk_queue_zone_sectors(q);
395 sector_t capacity = get_capacity(disk);
396
397 /*
398 * All zones must have the same size, with the exception on an eventual
399 * smaller last zone.
400 */
401 if (zone->start + zone_sectors < capacity &&
402 zone->len != zone_sectors) {
403 pr_warn("%s: Invalid zoned device with non constant zone size\n",
404 disk->disk_name);
405 return false;
406 }
407
408 if (zone->start + zone->len >= capacity &&
409 zone->len > zone_sectors) {
410 pr_warn("%s: Invalid zoned device with larger last zone size\n",
411 disk->disk_name);
412 return false;
413 }
414
415 /* Check for holes in the zone report */
416 if (zone->start != *sector) {
417 pr_warn("%s: Zone gap at sectors %llu..%llu\n",
418 disk->disk_name, *sector, zone->start);
419 return false;
420 }
421
422 /* Check zone type */
423 switch (zone->type) {
424 case BLK_ZONE_TYPE_CONVENTIONAL:
425 case BLK_ZONE_TYPE_SEQWRITE_REQ:
426 case BLK_ZONE_TYPE_SEQWRITE_PREF:
427 break;
428 default:
429 pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n",
430 disk->disk_name, (int)zone->type, zone->start);
431 return false;
432 }
433
434 *sector += zone->len;
435
436 return true;
437}
438
Damien Le Moalbf505452018-10-12 19:08:50 +0900439/**
440 * blk_revalidate_disk_zones - (re)allocate and initialize zone bitmaps
441 * @disk: Target disk
442 *
443 * Helper function for low-level device drivers to (re) allocate and initialize
444 * a disk request queue zone bitmaps. This functions should normally be called
445 * within the disk ->revalidate method. For BIO based queues, no zone bitmap
446 * is allocated.
447 */
448int blk_revalidate_disk_zones(struct gendisk *disk)
449{
450 struct request_queue *q = disk->queue;
451 unsigned int nr_zones = __blkdev_nr_zones(q, get_capacity(disk));
452 unsigned long *seq_zones_wlock = NULL, *seq_zones_bitmap = NULL;
453 unsigned int i, rep_nr_zones = 0, z = 0, nrz;
454 struct blk_zone *zones = NULL;
Damien Le Moalbd976e52019-07-01 14:09:16 +0900455 unsigned int noio_flag;
Damien Le Moalbf505452018-10-12 19:08:50 +0900456 sector_t sector = 0;
457 int ret = 0;
458
Christoph Hellwigc98c3d092019-11-11 11:39:23 +0900459 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
460 return -EIO;
461
Damien Le Moalbf505452018-10-12 19:08:50 +0900462 /*
463 * BIO based queues do not use a scheduler so only q->nr_zones
464 * needs to be updated so that the sysfs exposed value is correct.
465 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700466 if (!queue_is_mq(q)) {
Damien Le Moalbf505452018-10-12 19:08:50 +0900467 q->nr_zones = nr_zones;
468 return 0;
469 }
470
Damien Le Moalbd976e52019-07-01 14:09:16 +0900471 /*
472 * Ensure that all memory allocations in this context are done as
473 * if GFP_NOIO was specified.
474 */
475 noio_flag = memalloc_noio_save();
476
Christoph Hellwigc98c3d092019-11-11 11:39:23 +0900477 if (!nr_zones)
Damien Le Moalbf505452018-10-12 19:08:50 +0900478 goto update;
Damien Le Moalbf505452018-10-12 19:08:50 +0900479
480 /* Allocate bitmaps */
481 ret = -ENOMEM;
482 seq_zones_wlock = blk_alloc_zone_bitmap(q->node, nr_zones);
483 if (!seq_zones_wlock)
484 goto out;
485 seq_zones_bitmap = blk_alloc_zone_bitmap(q->node, nr_zones);
486 if (!seq_zones_bitmap)
487 goto out;
488
Damien Le Moald9dd7302019-11-11 11:39:22 +0900489 /*
490 * Get zone information to check the zones and initialize
491 * seq_zones_bitmap.
492 */
Damien Le Moalbf505452018-10-12 19:08:50 +0900493 rep_nr_zones = nr_zones;
Damien Le Moal26202922019-07-01 14:09:18 +0900494 zones = blk_alloc_zones(&rep_nr_zones);
Damien Le Moalbf505452018-10-12 19:08:50 +0900495 if (!zones)
496 goto out;
497
498 while (z < nr_zones) {
499 nrz = min(nr_zones - z, rep_nr_zones);
Damien Le Moalceeb3732019-11-11 11:39:24 +0900500 ret = disk->fops->report_zones(disk, sector, zones, &nrz);
Damien Le Moalbf505452018-10-12 19:08:50 +0900501 if (ret)
502 goto out;
503 if (!nrz)
504 break;
505 for (i = 0; i < nrz; i++) {
Damien Le Moald9dd7302019-11-11 11:39:22 +0900506 if (!blk_zone_valid(disk, &zones[i], &sector)) {
507 ret = -ENODEV;
508 goto out;
509 }
Damien Le Moalbf505452018-10-12 19:08:50 +0900510 if (zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL)
511 set_bit(z, seq_zones_bitmap);
512 z++;
513 }
Damien Le Moalbf505452018-10-12 19:08:50 +0900514 }
515
516 if (WARN_ON(z != nr_zones)) {
517 ret = -EIO;
518 goto out;
519 }
520
521update:
522 /*
523 * Install the new bitmaps, making sure the queue is stopped and
524 * all I/Os are completed (i.e. a scheduler is not referencing the
525 * bitmaps).
526 */
527 blk_mq_freeze_queue(q);
528 q->nr_zones = nr_zones;
529 swap(q->seq_zones_wlock, seq_zones_wlock);
530 swap(q->seq_zones_bitmap, seq_zones_bitmap);
531 blk_mq_unfreeze_queue(q);
532
533out:
Damien Le Moalbd976e52019-07-01 14:09:16 +0900534 memalloc_noio_restore(noio_flag);
535
Damien Le Moal26202922019-07-01 14:09:18 +0900536 kvfree(zones);
Damien Le Moalbf505452018-10-12 19:08:50 +0900537 kfree(seq_zones_wlock);
538 kfree(seq_zones_bitmap);
539
540 if (ret) {
541 pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
542 blk_mq_freeze_queue(q);
543 blk_queue_free_zone_bitmaps(q);
544 blk_mq_unfreeze_queue(q);
545 }
546
547 return ret;
548}
549EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones);
550