ethtool: set message mask with DEBUG_SET request
[linux-block.git] / block / blk-zoned.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
6a0cb1bc
HR
2/*
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>
bf505456 16#include <linux/blk-mq.h>
26202928
DLM
17#include <linux/mm.h>
18#include <linux/vmalloc.h>
bd976e52 19#include <linux/sched/mm.h>
6a0cb1bc 20
a2d6b3a2
DLM
21#include "blk.h"
22
6a0cb1bc
HR
23static inline sector_t blk_zone_start(struct request_queue *q,
24 sector_t sector)
25{
f99e8648 26 sector_t zone_mask = blk_queue_zone_sectors(q) - 1;
6a0cb1bc
HR
27
28 return sector & ~zone_mask;
29}
30
6cc77e9c
CH
31/*
32 * 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
a91e1380
DLM
73/**
74 * blkdev_nr_zones - Get number of zones
9b38bb4b 75 * @disk: Target gendisk
a91e1380 76 *
9b38bb4b
CH
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.
a91e1380 79 */
9b38bb4b 80unsigned int blkdev_nr_zones(struct gendisk *disk)
a91e1380 81{
9b38bb4b 82 sector_t zone_sectors = blk_queue_zone_sectors(disk->queue);
a91e1380 83
9b38bb4b 84 if (!blk_queue_is_zoned(disk->queue))
a91e1380 85 return 0;
9b38bb4b 86 return (get_capacity(disk) + zone_sectors - 1) >> ilog2(zone_sectors);
a91e1380
DLM
87}
88EXPORT_SYMBOL_GPL(blkdev_nr_zones);
89
6a0cb1bc
HR
90/**
91 * blkdev_report_zones - Get zones information
92 * @bdev: Target block device
93 * @sector: Sector from which to report zones
d4100351
CH
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
6a0cb1bc
HR
97 *
98 * Description:
d4100351
CH
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.
105 *
106 * Note: The caller must use memalloc_noXX_save/restore() calls to control
107 * memory allocations done within this function.
6a0cb1bc 108 */
e76239a3 109int blkdev_report_zones(struct block_device *bdev, sector_t sector,
d4100351 110 unsigned int nr_zones, report_zones_cb cb, void *data)
6a0cb1bc 111{
ceeb373a 112 struct gendisk *disk = bdev->bd_disk;
5eac3eb3 113 sector_t capacity = get_capacity(disk);
6a0cb1bc 114
d4100351
CH
115 if (!blk_queue_is_zoned(bdev_get_queue(bdev)) ||
116 WARN_ON_ONCE(!disk->fops->report_zones))
e76239a3 117 return -EOPNOTSUPP;
6a0cb1bc 118
d4100351 119 if (!nr_zones || sector >= capacity)
6a0cb1bc 120 return 0;
6a0cb1bc 121
d4100351 122 return disk->fops->report_zones(disk, sector, nr_zones, cb, data);
6a0cb1bc
HR
123}
124EXPORT_SYMBOL_GPL(blkdev_report_zones);
125
6e33dbf2 126static inline bool blkdev_allow_reset_all_zones(struct block_device *bdev,
c7a1d926 127 sector_t sector,
6e33dbf2
CK
128 sector_t nr_sectors)
129{
130 if (!blk_queue_zone_resetall(bdev_get_queue(bdev)))
131 return false;
132
6e33dbf2 133 /*
5eac3eb3
DLM
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.
6e33dbf2 136 */
5eac3eb3 137 return !sector && nr_sectors == get_capacity(bdev->bd_disk);
6e33dbf2
CK
138}
139
6a0cb1bc 140/**
6c1b1da5 141 * blkdev_zone_mgmt - Execute a zone management operation on a range of zones
6a0cb1bc 142 * @bdev: Target block device
6c1b1da5
AJ
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.
6a0cb1bc
HR
147 * @gfp_mask: Memory allocation flags (for bio_alloc)
148 *
149 * Description:
6c1b1da5 150 * Perform the specified operation on the range of zones specified by
6a0cb1bc
HR
151 * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
152 * is valid, but the specified range should not contain conventional zones.
6c1b1da5
AJ
153 * The operation to execute on each zone can be a zone reset, open, close
154 * or finish request.
6a0cb1bc 155 */
6c1b1da5
AJ
156int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
157 sector_t sector, sector_t nr_sectors,
158 gfp_t gfp_mask)
6a0cb1bc
HR
159{
160 struct request_queue *q = bdev_get_queue(bdev);
6c1b1da5 161 sector_t zone_sectors = blk_queue_zone_sectors(q);
5eac3eb3 162 sector_t capacity = get_capacity(bdev->bd_disk);
6a0cb1bc 163 sector_t end_sector = sector + nr_sectors;
a2d6b3a2 164 struct bio *bio = NULL;
6a0cb1bc
HR
165 int ret;
166
6a0cb1bc
HR
167 if (!blk_queue_is_zoned(q))
168 return -EOPNOTSUPP;
169
a2d6b3a2
DLM
170 if (bdev_read_only(bdev))
171 return -EPERM;
172
6c1b1da5
AJ
173 if (!op_is_zone_mgmt(op))
174 return -EOPNOTSUPP;
175
5eac3eb3 176 if (!nr_sectors || end_sector > capacity)
6a0cb1bc
HR
177 /* Out of range */
178 return -EINVAL;
179
180 /* Check alignment (handle eventual smaller last zone) */
6a0cb1bc
HR
181 if (sector & (zone_sectors - 1))
182 return -EINVAL;
183
5eac3eb3 184 if ((nr_sectors & (zone_sectors - 1)) && end_sector != capacity)
6a0cb1bc
HR
185 return -EINVAL;
186
187 while (sector < end_sector) {
a2d6b3a2 188 bio = blk_next_bio(bio, 0, gfp_mask);
74d46992 189 bio_set_dev(bio, bdev);
6a0cb1bc 190
c7a1d926
DLM
191 /*
192 * Special case for the zone reset operation that reset all
193 * zones, this is useful for applications like mkfs.
194 */
6c1b1da5
AJ
195 if (op == REQ_OP_ZONE_RESET &&
196 blkdev_allow_reset_all_zones(bdev, sector, nr_sectors)) {
c7a1d926
DLM
197 bio->bi_opf = REQ_OP_ZONE_RESET_ALL;
198 break;
199 }
200
6c1b1da5 201 bio->bi_opf = op;
c7a1d926 202 bio->bi_iter.bi_sector = sector;
6a0cb1bc
HR
203 sector += zone_sectors;
204
205 /* This may take a while, so be nice to others */
206 cond_resched();
6a0cb1bc
HR
207 }
208
a2d6b3a2
DLM
209 ret = submit_bio_wait(bio);
210 bio_put(bio);
211
a2d6b3a2 212 return ret;
6a0cb1bc 213}
6c1b1da5 214EXPORT_SYMBOL_GPL(blkdev_zone_mgmt);
3ed05a98 215
d4100351
CH
216struct zone_report_args {
217 struct blk_zone __user *zones;
218};
219
220static int blkdev_copy_zone_to_user(struct blk_zone *zone, unsigned int idx,
221 void *data)
222{
223 struct zone_report_args *args = data;
224
225 if (copy_to_user(&args->zones[idx], zone, sizeof(struct blk_zone)))
226 return -EFAULT;
227 return 0;
228}
229
56c4bddb 230/*
3ed05a98
ST
231 * BLKREPORTZONE ioctl processing.
232 * Called from blkdev_ioctl.
233 */
234int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
235 unsigned int cmd, unsigned long arg)
236{
237 void __user *argp = (void __user *)arg;
d4100351 238 struct zone_report_args args;
3ed05a98
ST
239 struct request_queue *q;
240 struct blk_zone_report rep;
3ed05a98
ST
241 int ret;
242
243 if (!argp)
244 return -EINVAL;
245
246 q = bdev_get_queue(bdev);
247 if (!q)
248 return -ENXIO;
249
250 if (!blk_queue_is_zoned(q))
251 return -ENOTTY;
252
253 if (!capable(CAP_SYS_ADMIN))
254 return -EACCES;
255
256 if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report)))
257 return -EFAULT;
258
259 if (!rep.nr_zones)
260 return -EINVAL;
261
d4100351
CH
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);
265 if (ret < 0)
266 return ret;
3ed05a98 267
d4100351
CH
268 rep.nr_zones = ret;
269 if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report)))
270 return -EFAULT;
271 return 0;
3ed05a98
ST
272}
273
56c4bddb 274/*
e876df1f 275 * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
3ed05a98
ST
276 * Called from blkdev_ioctl.
277 */
e876df1f
AJ
278int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
279 unsigned int cmd, unsigned long arg)
3ed05a98
ST
280{
281 void __user *argp = (void __user *)arg;
282 struct request_queue *q;
283 struct blk_zone_range zrange;
e876df1f 284 enum req_opf op;
3ed05a98
ST
285
286 if (!argp)
287 return -EINVAL;
288
289 q = bdev_get_queue(bdev);
290 if (!q)
291 return -ENXIO;
292
293 if (!blk_queue_is_zoned(q))
294 return -ENOTTY;
295
296 if (!capable(CAP_SYS_ADMIN))
297 return -EACCES;
298
299 if (!(mode & FMODE_WRITE))
300 return -EBADF;
301
302 if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))
303 return -EFAULT;
304
e876df1f
AJ
305 switch (cmd) {
306 case BLKRESETZONE:
307 op = REQ_OP_ZONE_RESET;
308 break;
309 case BLKOPENZONE:
310 op = REQ_OP_ZONE_OPEN;
311 break;
312 case BLKCLOSEZONE:
313 op = REQ_OP_ZONE_CLOSE;
314 break;
315 case BLKFINISHZONE:
316 op = REQ_OP_ZONE_FINISH;
317 break;
318 default:
319 return -ENOTTY;
320 }
321
322 return blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors,
323 GFP_KERNEL);
3ed05a98 324}
bf505456
DLM
325
326static inline unsigned long *blk_alloc_zone_bitmap(int node,
327 unsigned int nr_zones)
328{
329 return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
330 GFP_NOIO, node);
331}
332
bf505456
DLM
333void blk_queue_free_zone_bitmaps(struct request_queue *q)
334{
f216fdd7
CH
335 kfree(q->conv_zones_bitmap);
336 q->conv_zones_bitmap = NULL;
bf505456
DLM
337 kfree(q->seq_zones_wlock);
338 q->seq_zones_wlock = NULL;
339}
340
d4100351
CH
341struct blk_revalidate_zone_args {
342 struct gendisk *disk;
f216fdd7 343 unsigned long *conv_zones_bitmap;
d4100351 344 unsigned long *seq_zones_wlock;
e94f5819 345 unsigned int nr_zones;
6c6b3549 346 sector_t zone_sectors;
d4100351
CH
347 sector_t sector;
348};
349
d9dd7308
DLM
350/*
351 * Helper function to check the validity of zones of a zoned block device.
352 */
d4100351
CH
353static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx,
354 void *data)
d9dd7308 355{
d4100351
CH
356 struct blk_revalidate_zone_args *args = data;
357 struct gendisk *disk = args->disk;
d9dd7308 358 struct request_queue *q = disk->queue;
d9dd7308
DLM
359 sector_t capacity = get_capacity(disk);
360
361 /*
362 * All zones must have the same size, with the exception on an eventual
363 * smaller last zone.
364 */
6c6b3549
CH
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);
369 return -ENODEV;
370 }
d9dd7308 371
6c6b3549
CH
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",
377 disk->disk_name);
378 return -ENODEV;
379 }
380 } else {
381 if (zone->len > args->zone_sectors) {
382 pr_warn("%s: Invalid zoned device with larger last zone size\n",
383 disk->disk_name);
384 return -ENODEV;
385 }
d9dd7308
DLM
386 }
387
388 /* Check for holes in the zone report */
d4100351 389 if (zone->start != args->sector) {
d9dd7308 390 pr_warn("%s: Zone gap at sectors %llu..%llu\n",
d4100351
CH
391 disk->disk_name, args->sector, zone->start);
392 return -ENODEV;
d9dd7308
DLM
393 }
394
395 /* Check zone type */
396 switch (zone->type) {
397 case BLK_ZONE_TYPE_CONVENTIONAL:
e94f5819
CH
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)
402 return -ENOMEM;
403 }
404 set_bit(idx, args->conv_zones_bitmap);
405 break;
d9dd7308
DLM
406 case BLK_ZONE_TYPE_SEQWRITE_REQ:
407 case BLK_ZONE_TYPE_SEQWRITE_PREF:
e94f5819
CH
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)
412 return -ENOMEM;
413 }
d9dd7308
DLM
414 break;
415 default:
416 pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n",
417 disk->disk_name, (int)zone->type, zone->start);
d4100351 418 return -ENODEV;
d9dd7308
DLM
419 }
420
d4100351
CH
421 args->sector += zone->len;
422 return 0;
423}
424
bf505456
DLM
425/**
426 * blk_revalidate_disk_zones - (re)allocate and initialize zone bitmaps
427 * @disk: Target disk
428 *
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
ae58954d
CH
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
433 * is correct.
bf505456
DLM
434 */
435int blk_revalidate_disk_zones(struct gendisk *disk)
436{
437 struct request_queue *q = disk->queue;
e94f5819
CH
438 struct blk_revalidate_zone_args args = {
439 .disk = disk,
e94f5819 440 };
6c6b3549
CH
441 unsigned int noio_flag;
442 int ret;
bf505456 443
c98c3d09
CH
444 if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
445 return -EIO;
ae58954d
CH
446 if (WARN_ON_ONCE(!queue_is_mq(q)))
447 return -EIO;
bf505456 448
e94f5819 449 /*
6c6b3549
CH
450 * Ensure that all memory allocations in this context are done as if
451 * GFP_NOIO was specified.
e94f5819 452 */
6c6b3549
CH
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);
bf505456 457
bf505456 458 /*
6c6b3549
CH
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).
bf505456
DLM
462 */
463 blk_mq_freeze_queue(q);
d4100351 464 if (ret >= 0) {
6c6b3549 465 blk_queue_chunk_sectors(q, args.zone_sectors);
e94f5819 466 q->nr_zones = args.nr_zones;
d4100351 467 swap(q->seq_zones_wlock, args.seq_zones_wlock);
f216fdd7 468 swap(q->conv_zones_bitmap, args.conv_zones_bitmap);
d4100351
CH
469 ret = 0;
470 } else {
bf505456 471 pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
bf505456 472 blk_queue_free_zone_bitmaps(q);
bf505456 473 }
d4100351 474 blk_mq_unfreeze_queue(q);
bf505456 475
d4100351 476 kfree(args.seq_zones_wlock);
f216fdd7 477 kfree(args.conv_zones_bitmap);
bf505456
DLM
478 return ret;
479}
480EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones);