Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 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> |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 16 | #include <linux/blk-mq.h> |
Damien Le Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/vmalloc.h> |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 19 | #include <linux/sched/mm.h> |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 20 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 21 | #include "blk.h" |
| 22 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 23 | static inline sector_t blk_zone_start(struct request_queue *q, |
| 24 | sector_t sector) |
| 25 | { |
Damien Le Moal | f99e864 | 2017-01-12 07:58:32 -0700 | [diff] [blame] | 26 | sector_t zone_mask = blk_queue_zone_sectors(q) - 1; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 27 | |
| 28 | return sector & ~zone_mask; |
| 29 | } |
| 30 | |
| 31 | /* |
Christoph Hellwig | 6cc77e9 | 2017-12-21 15:43:38 +0900 | [diff] [blame] | 32 | * Return true if a request is a write requests that needs zone write locking. |
| 33 | */ |
| 34 | bool 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 | } |
| 51 | EXPORT_SYMBOL_GPL(blk_req_needs_zone_write_lock); |
| 52 | |
| 53 | void __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 | } |
| 62 | EXPORT_SYMBOL_GPL(__blk_req_zone_write_lock); |
| 63 | |
| 64 | void __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 | } |
| 71 | EXPORT_SYMBOL_GPL(__blk_req_zone_write_unlock); |
| 72 | |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 73 | static inline unsigned int __blkdev_nr_zones(struct request_queue *q, |
| 74 | sector_t nr_sectors) |
| 75 | { |
Damien Le Moal | 113ab72 | 2019-07-10 13:53:10 +0900 | [diff] [blame] | 76 | sector_t zone_sectors = blk_queue_zone_sectors(q); |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 77 | |
| 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 | */ |
| 89 | unsigned 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 Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 96 | return __blkdev_nr_zones(q, get_capacity(bdev->bd_disk)); |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 97 | } |
| 98 | EXPORT_SYMBOL_GPL(blkdev_nr_zones); |
| 99 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 100 | /** |
| 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 Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 106 | * |
| 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 Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 112 | * 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 Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 115 | */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 116 | int blkdev_report_zones(struct block_device *bdev, sector_t sector, |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 117 | struct blk_zone *zones, unsigned int *nr_zones) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 118 | { |
| 119 | struct request_queue *q = bdev_get_queue(bdev); |
Damien Le Moal | ceeb373 | 2019-11-11 11:39:24 +0900 | [diff] [blame] | 120 | struct gendisk *disk = bdev->bd_disk; |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 121 | sector_t capacity = get_capacity(disk); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 122 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 123 | if (!blk_queue_is_zoned(q)) |
| 124 | return -EOPNOTSUPP; |
| 125 | |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 126 | /* |
| 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 Moal | ceeb373 | 2019-11-11 11:39:24 +0900 | [diff] [blame] | 131 | if (WARN_ON_ONCE(!disk->fops->report_zones)) |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 132 | return -EOPNOTSUPP; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 133 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 134 | if (!*nr_zones || sector >= capacity) { |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 135 | *nr_zones = 0; |
| 136 | return 0; |
| 137 | } |
| 138 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 139 | *nr_zones = min(*nr_zones, __blkdev_nr_zones(q, capacity - sector)); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 140 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 141 | return disk->fops->report_zones(disk, sector, zones, nr_zones); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 142 | } |
| 143 | EXPORT_SYMBOL_GPL(blkdev_report_zones); |
| 144 | |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 145 | static inline bool blkdev_allow_reset_all_zones(struct block_device *bdev, |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 146 | sector_t sector, |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 147 | sector_t nr_sectors) |
| 148 | { |
| 149 | if (!blk_queue_zone_resetall(bdev_get_queue(bdev))) |
| 150 | return false; |
| 151 | |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 152 | /* |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 153 | * 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 Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 155 | */ |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 156 | return !sector && nr_sectors == get_capacity(bdev->bd_disk); |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 159 | /** |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 160 | * blkdev_zone_mgmt - Execute a zone management operation on a range of zones |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 161 | * @bdev: Target block device |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 162 | * @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 Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 166 | * @gfp_mask: Memory allocation flags (for bio_alloc) |
| 167 | * |
| 168 | * Description: |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 169 | * Perform the specified operation on the range of zones specified by |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 170 | * @sector..@sector+@nr_sectors. Specifying the entire disk sector range |
| 171 | * is valid, but the specified range should not contain conventional zones. |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 172 | * The operation to execute on each zone can be a zone reset, open, close |
| 173 | * or finish request. |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 174 | */ |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 175 | int 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 Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 178 | { |
| 179 | struct request_queue *q = bdev_get_queue(bdev); |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 180 | sector_t zone_sectors = blk_queue_zone_sectors(q); |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 181 | sector_t capacity = get_capacity(bdev->bd_disk); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 182 | sector_t end_sector = sector + nr_sectors; |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 183 | struct bio *bio = NULL; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 184 | int ret; |
| 185 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 186 | if (!blk_queue_is_zoned(q)) |
| 187 | return -EOPNOTSUPP; |
| 188 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 189 | if (bdev_read_only(bdev)) |
| 190 | return -EPERM; |
| 191 | |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 192 | if (!op_is_zone_mgmt(op)) |
| 193 | return -EOPNOTSUPP; |
| 194 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 195 | if (!nr_sectors || end_sector > capacity) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 196 | /* Out of range */ |
| 197 | return -EINVAL; |
| 198 | |
| 199 | /* Check alignment (handle eventual smaller last zone) */ |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 200 | if (sector & (zone_sectors - 1)) |
| 201 | return -EINVAL; |
| 202 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame^] | 203 | if ((nr_sectors & (zone_sectors - 1)) && end_sector != capacity) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
| 206 | while (sector < end_sector) { |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 207 | bio = blk_next_bio(bio, 0, gfp_mask); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 208 | bio_set_dev(bio, bdev); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 209 | |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 210 | /* |
| 211 | * Special case for the zone reset operation that reset all |
| 212 | * zones, this is useful for applications like mkfs. |
| 213 | */ |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 214 | if (op == REQ_OP_ZONE_RESET && |
| 215 | blkdev_allow_reset_all_zones(bdev, sector, nr_sectors)) { |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 216 | bio->bi_opf = REQ_OP_ZONE_RESET_ALL; |
| 217 | break; |
| 218 | } |
| 219 | |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 220 | bio->bi_opf = op; |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 221 | bio->bi_iter.bi_sector = sector; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 222 | sector += zone_sectors; |
| 223 | |
| 224 | /* This may take a while, so be nice to others */ |
| 225 | cond_resched(); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 226 | } |
| 227 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 228 | ret = submit_bio_wait(bio); |
| 229 | bio_put(bio); |
| 230 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 231 | return ret; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 232 | } |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(blkdev_zone_mgmt); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 234 | |
Bart Van Assche | 56c4bdd | 2018-03-08 15:28:50 -0800 | [diff] [blame] | 235 | /* |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 236 | * BLKREPORTZONE ioctl processing. |
| 237 | * Called from blkdev_ioctl. |
| 238 | */ |
| 239 | int 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 Moal | 2e85fba | 2018-10-12 19:08:44 +0900 | [diff] [blame] | 267 | rep.nr_zones = min(blkdev_nr_zones(bdev), rep.nr_zones); |
Bart Van Assche | 327ea4a | 2018-05-22 08:27:22 -0700 | [diff] [blame] | 268 | |
Kees Cook | 344476e | 2018-06-12 14:04:32 -0700 | [diff] [blame] | 269 | zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone), |
| 270 | GFP_KERNEL | __GFP_ZERO); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 271 | if (!zones) |
| 272 | return -ENOMEM; |
| 273 | |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 274 | ret = blkdev_report_zones(bdev, rep.sector, zones, &rep.nr_zones); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 275 | 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 Assche | 327ea4a | 2018-05-22 08:27:22 -0700 | [diff] [blame] | 290 | kvfree(zones); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
Bart Van Assche | 56c4bdd | 2018-03-08 15:28:50 -0800 | [diff] [blame] | 295 | /* |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 296 | * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing. |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 297 | * Called from blkdev_ioctl. |
| 298 | */ |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 299 | int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode, |
| 300 | unsigned int cmd, unsigned long arg) |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 301 | { |
| 302 | void __user *argp = (void __user *)arg; |
| 303 | struct request_queue *q; |
| 304 | struct blk_zone_range zrange; |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 305 | enum req_opf op; |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 306 | |
| 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 Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 326 | 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 Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 345 | } |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 346 | |
| 347 | static 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 Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 358 | static struct blk_zone *blk_alloc_zones(unsigned int *nr_zones) |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 359 | { |
Damien Le Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 360 | struct blk_zone *zones; |
| 361 | size_t nrz = min(*nr_zones, BLK_ZONED_REPORT_MAX_ZONES); |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 362 | |
Damien Le Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 363 | /* |
| 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 Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 372 | } |
| 373 | |
Damien Le Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 374 | *nr_zones = nrz; |
| 375 | |
| 376 | return zones; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void 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 Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 387 | /* |
| 388 | * Helper function to check the validity of zones of a zoned block device. |
| 389 | */ |
| 390 | static 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 Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 439 | /** |
| 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 | */ |
| 448 | int 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 Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 455 | unsigned int noio_flag; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 456 | sector_t sector = 0; |
| 457 | int ret = 0; |
| 458 | |
Christoph Hellwig | c98c3d09 | 2019-11-11 11:39:23 +0900 | [diff] [blame] | 459 | if (WARN_ON_ONCE(!blk_queue_is_zoned(q))) |
| 460 | return -EIO; |
| 461 | |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 462 | /* |
| 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 Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 466 | if (!queue_is_mq(q)) { |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 467 | q->nr_zones = nr_zones; |
| 468 | return 0; |
| 469 | } |
| 470 | |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 471 | /* |
| 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 Hellwig | c98c3d09 | 2019-11-11 11:39:23 +0900 | [diff] [blame] | 477 | if (!nr_zones) |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 478 | goto update; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 479 | |
| 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 Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 489 | /* |
| 490 | * Get zone information to check the zones and initialize |
| 491 | * seq_zones_bitmap. |
| 492 | */ |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 493 | rep_nr_zones = nr_zones; |
Damien Le Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 494 | zones = blk_alloc_zones(&rep_nr_zones); |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 495 | if (!zones) |
| 496 | goto out; |
| 497 | |
| 498 | while (z < nr_zones) { |
| 499 | nrz = min(nr_zones - z, rep_nr_zones); |
Damien Le Moal | ceeb373 | 2019-11-11 11:39:24 +0900 | [diff] [blame] | 500 | ret = disk->fops->report_zones(disk, sector, zones, &nrz); |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 501 | if (ret) |
| 502 | goto out; |
| 503 | if (!nrz) |
| 504 | break; |
| 505 | for (i = 0; i < nrz; i++) { |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 506 | if (!blk_zone_valid(disk, &zones[i], §or)) { |
| 507 | ret = -ENODEV; |
| 508 | goto out; |
| 509 | } |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 510 | if (zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL) |
| 511 | set_bit(z, seq_zones_bitmap); |
| 512 | z++; |
| 513 | } |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | if (WARN_ON(z != nr_zones)) { |
| 517 | ret = -EIO; |
| 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | update: |
| 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 | |
| 533 | out: |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 534 | memalloc_noio_restore(noio_flag); |
| 535 | |
Damien Le Moal | 2620292 | 2019-07-01 14:09:18 +0900 | [diff] [blame] | 536 | kvfree(zones); |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 537 | 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 | } |
| 549 | EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones); |
| 550 | |