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 | /** |
| 74 | * blkdev_nr_zones - Get number of zones |
Christoph Hellwig | 9b38bb4 | 2019-12-03 10:39:04 +0100 | [diff] [blame] | 75 | * @disk: Target gendisk |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 76 | * |
Christoph Hellwig | 9b38bb4 | 2019-12-03 10:39:04 +0100 | [diff] [blame] | 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. |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 79 | */ |
Christoph Hellwig | 9b38bb4 | 2019-12-03 10:39:04 +0100 | [diff] [blame] | 80 | unsigned int blkdev_nr_zones(struct gendisk *disk) |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 81 | { |
Christoph Hellwig | 9b38bb4 | 2019-12-03 10:39:04 +0100 | [diff] [blame] | 82 | sector_t zone_sectors = blk_queue_zone_sectors(disk->queue); |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 83 | |
Christoph Hellwig | 9b38bb4 | 2019-12-03 10:39:04 +0100 | [diff] [blame] | 84 | if (!blk_queue_is_zoned(disk->queue)) |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 85 | return 0; |
Christoph Hellwig | 9b38bb4 | 2019-12-03 10:39:04 +0100 | [diff] [blame] | 86 | return (get_capacity(disk) + zone_sectors - 1) >> ilog2(zone_sectors); |
Damien Le Moal | a91e138 | 2018-10-12 19:08:43 +0900 | [diff] [blame] | 87 | } |
| 88 | EXPORT_SYMBOL_GPL(blkdev_nr_zones); |
| 89 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 90 | /** |
| 91 | * blkdev_report_zones - Get zones information |
| 92 | * @bdev: Target block device |
| 93 | * @sector: Sector from which to report zones |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 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 |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 97 | * |
| 98 | * Description: |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 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. |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 108 | */ |
Christoph Hellwig | e76239a | 2018-10-12 19:08:49 +0900 | [diff] [blame] | 109 | int blkdev_report_zones(struct block_device *bdev, sector_t sector, |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 110 | unsigned int nr_zones, report_zones_cb cb, void *data) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 111 | { |
Damien Le Moal | ceeb373 | 2019-11-11 11:39:24 +0900 | [diff] [blame] | 112 | struct gendisk *disk = bdev->bd_disk; |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 113 | sector_t capacity = get_capacity(disk); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 114 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 115 | if (!blk_queue_is_zoned(bdev_get_queue(bdev)) || |
| 116 | WARN_ON_ONCE(!disk->fops->report_zones)) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 117 | return -EOPNOTSUPP; |
| 118 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 119 | if (!nr_zones || sector >= capacity) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 120 | return 0; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 121 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 122 | return disk->fops->report_zones(disk, sector, nr_zones, cb, data); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 123 | } |
| 124 | EXPORT_SYMBOL_GPL(blkdev_report_zones); |
| 125 | |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 126 | 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] | 127 | sector_t sector, |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 128 | sector_t nr_sectors) |
| 129 | { |
| 130 | if (!blk_queue_zone_resetall(bdev_get_queue(bdev))) |
| 131 | return false; |
| 132 | |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 133 | /* |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 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. |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 136 | */ |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 137 | return !sector && nr_sectors == get_capacity(bdev->bd_disk); |
Chaitanya Kulkarni | 6e33dbf | 2019-08-01 10:26:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 140 | /** |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 141 | * 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] | 142 | * @bdev: Target block device |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 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. |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 147 | * @gfp_mask: Memory allocation flags (for bio_alloc) |
| 148 | * |
| 149 | * Description: |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 150 | * Perform the specified operation on the range of zones specified by |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 151 | * @sector..@sector+@nr_sectors. Specifying the entire disk sector range |
| 152 | * is valid, but the specified range should not contain conventional zones. |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 153 | * The operation to execute on each zone can be a zone reset, open, close |
| 154 | * or finish request. |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 155 | */ |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 156 | int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op, |
| 157 | sector_t sector, sector_t nr_sectors, |
| 158 | gfp_t gfp_mask) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 159 | { |
| 160 | struct request_queue *q = bdev_get_queue(bdev); |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 161 | sector_t zone_sectors = blk_queue_zone_sectors(q); |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 162 | sector_t capacity = get_capacity(bdev->bd_disk); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 163 | sector_t end_sector = sector + nr_sectors; |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 164 | struct bio *bio = NULL; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 165 | int ret; |
| 166 | |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 167 | if (!blk_queue_is_zoned(q)) |
| 168 | return -EOPNOTSUPP; |
| 169 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 170 | if (bdev_read_only(bdev)) |
| 171 | return -EPERM; |
| 172 | |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 173 | if (!op_is_zone_mgmt(op)) |
| 174 | return -EOPNOTSUPP; |
| 175 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 176 | if (!nr_sectors || end_sector > capacity) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 177 | /* Out of range */ |
| 178 | return -EINVAL; |
| 179 | |
| 180 | /* Check alignment (handle eventual smaller last zone) */ |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 181 | if (sector & (zone_sectors - 1)) |
| 182 | return -EINVAL; |
| 183 | |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 184 | if ((nr_sectors & (zone_sectors - 1)) && end_sector != capacity) |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | |
| 187 | while (sector < end_sector) { |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 188 | bio = blk_next_bio(bio, 0, gfp_mask); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 189 | bio_set_dev(bio, bdev); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 190 | |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 191 | /* |
| 192 | * Special case for the zone reset operation that reset all |
| 193 | * zones, this is useful for applications like mkfs. |
| 194 | */ |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 195 | if (op == REQ_OP_ZONE_RESET && |
| 196 | blkdev_allow_reset_all_zones(bdev, sector, nr_sectors)) { |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 197 | bio->bi_opf = REQ_OP_ZONE_RESET_ALL; |
| 198 | break; |
| 199 | } |
| 200 | |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 201 | bio->bi_opf = op; |
Damien Le Moal | c7a1d92 | 2019-10-27 23:05:43 +0900 | [diff] [blame] | 202 | bio->bi_iter.bi_sector = sector; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 203 | sector += zone_sectors; |
| 204 | |
| 205 | /* This may take a while, so be nice to others */ |
| 206 | cond_resched(); |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 209 | ret = submit_bio_wait(bio); |
| 210 | bio_put(bio); |
| 211 | |
Damien Le Moal | a2d6b3a | 2018-10-12 19:08:47 +0900 | [diff] [blame] | 212 | return ret; |
Hannes Reinecke | 6a0cb1b | 2016-10-18 15:40:33 +0900 | [diff] [blame] | 213 | } |
Ajay Joshi | 6c1b1da | 2019-10-27 23:05:45 +0900 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(blkdev_zone_mgmt); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 215 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 216 | struct zone_report_args { |
| 217 | struct blk_zone __user *zones; |
| 218 | }; |
| 219 | |
| 220 | static 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 | |
Bart Van Assche | 56c4bdd | 2018-03-08 15:28:50 -0800 | [diff] [blame] | 230 | /* |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 231 | * BLKREPORTZONE ioctl processing. |
| 232 | * Called from blkdev_ioctl. |
| 233 | */ |
| 234 | int 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; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 238 | struct zone_report_args args; |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 239 | struct request_queue *q; |
| 240 | struct blk_zone_report rep; |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 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 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 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; |
Bart Van Assche | 327ea4a | 2018-05-22 08:27:22 -0700 | [diff] [blame] | 267 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 268 | rep.nr_zones = ret; |
| 269 | if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report))) |
| 270 | return -EFAULT; |
| 271 | return 0; |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Bart Van Assche | 56c4bdd | 2018-03-08 15:28:50 -0800 | [diff] [blame] | 274 | /* |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 275 | * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing. |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 276 | * Called from blkdev_ioctl. |
| 277 | */ |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 278 | int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode, |
| 279 | unsigned int cmd, unsigned long arg) |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 280 | { |
| 281 | void __user *argp = (void __user *)arg; |
| 282 | struct request_queue *q; |
| 283 | struct blk_zone_range zrange; |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 284 | enum req_opf op; |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 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 | |
Ajay Joshi | e876df1 | 2019-10-27 23:05:46 +0900 | [diff] [blame] | 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); |
Shaun Tancheff | 3ed05a9 | 2016-10-18 15:40:35 +0900 | [diff] [blame] | 324 | } |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 325 | |
| 326 | static 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 | |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 333 | void blk_queue_free_zone_bitmaps(struct request_queue *q) |
| 334 | { |
Christoph Hellwig | f216fdd | 2019-12-03 10:39:05 +0100 | [diff] [blame] | 335 | kfree(q->conv_zones_bitmap); |
| 336 | q->conv_zones_bitmap = NULL; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 337 | kfree(q->seq_zones_wlock); |
| 338 | q->seq_zones_wlock = NULL; |
| 339 | } |
| 340 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 341 | struct blk_revalidate_zone_args { |
| 342 | struct gendisk *disk; |
Christoph Hellwig | f216fdd | 2019-12-03 10:39:05 +0100 | [diff] [blame] | 343 | unsigned long *conv_zones_bitmap; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 344 | unsigned long *seq_zones_wlock; |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 345 | unsigned int nr_zones; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 346 | sector_t sector; |
| 347 | }; |
| 348 | |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 349 | /* |
| 350 | * Helper function to check the validity of zones of a zoned block device. |
| 351 | */ |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 352 | static int blk_revalidate_zone_cb(struct blk_zone *zone, unsigned int idx, |
| 353 | void *data) |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 354 | { |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 355 | struct blk_revalidate_zone_args *args = data; |
| 356 | struct gendisk *disk = args->disk; |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 357 | struct request_queue *q = disk->queue; |
| 358 | sector_t zone_sectors = blk_queue_zone_sectors(q); |
| 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 | */ |
| 365 | if (zone->start + zone_sectors < capacity && |
| 366 | zone->len != zone_sectors) { |
| 367 | pr_warn("%s: Invalid zoned device with non constant zone size\n", |
| 368 | disk->disk_name); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | if (zone->start + zone->len >= capacity && |
| 373 | zone->len > zone_sectors) { |
| 374 | pr_warn("%s: Invalid zoned device with larger last zone size\n", |
| 375 | disk->disk_name); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 376 | return -ENODEV; |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Check for holes in the zone report */ |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 380 | if (zone->start != args->sector) { |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 381 | pr_warn("%s: Zone gap at sectors %llu..%llu\n", |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 382 | disk->disk_name, args->sector, zone->start); |
| 383 | return -ENODEV; |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* Check zone type */ |
| 387 | switch (zone->type) { |
| 388 | case BLK_ZONE_TYPE_CONVENTIONAL: |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 389 | if (!args->conv_zones_bitmap) { |
| 390 | args->conv_zones_bitmap = |
| 391 | blk_alloc_zone_bitmap(q->node, args->nr_zones); |
| 392 | if (!args->conv_zones_bitmap) |
| 393 | return -ENOMEM; |
| 394 | } |
| 395 | set_bit(idx, args->conv_zones_bitmap); |
| 396 | break; |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 397 | case BLK_ZONE_TYPE_SEQWRITE_REQ: |
| 398 | case BLK_ZONE_TYPE_SEQWRITE_PREF: |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 399 | if (!args->seq_zones_wlock) { |
| 400 | args->seq_zones_wlock = |
| 401 | blk_alloc_zone_bitmap(q->node, args->nr_zones); |
| 402 | if (!args->seq_zones_wlock) |
| 403 | return -ENOMEM; |
| 404 | } |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 405 | break; |
| 406 | default: |
| 407 | pr_warn("%s: Invalid zone type 0x%x at sectors %llu\n", |
| 408 | disk->disk_name, (int)zone->type, zone->start); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 409 | return -ENODEV; |
Damien Le Moal | d9dd730 | 2019-11-11 11:39:22 +0900 | [diff] [blame] | 410 | } |
| 411 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 412 | args->sector += zone->len; |
| 413 | return 0; |
| 414 | } |
| 415 | |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 416 | /** |
| 417 | * blk_revalidate_disk_zones - (re)allocate and initialize zone bitmaps |
| 418 | * @disk: Target disk |
| 419 | * |
| 420 | * Helper function for low-level device drivers to (re) allocate and initialize |
| 421 | * a disk request queue zone bitmaps. This functions should normally be called |
| 422 | * within the disk ->revalidate method. For BIO based queues, no zone bitmap |
| 423 | * is allocated. |
| 424 | */ |
| 425 | int blk_revalidate_disk_zones(struct gendisk *disk) |
| 426 | { |
| 427 | struct request_queue *q = disk->queue; |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 428 | struct blk_revalidate_zone_args args = { |
| 429 | .disk = disk, |
| 430 | .nr_zones = blkdev_nr_zones(disk), |
| 431 | }; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 432 | int ret = 0; |
| 433 | |
Christoph Hellwig | c98c3d09 | 2019-11-11 11:39:23 +0900 | [diff] [blame] | 434 | if (WARN_ON_ONCE(!blk_queue_is_zoned(q))) |
| 435 | return -EIO; |
| 436 | |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 437 | /* |
| 438 | * BIO based queues do not use a scheduler so only q->nr_zones |
| 439 | * needs to be updated so that the sysfs exposed value is correct. |
| 440 | */ |
Jens Axboe | 344e9ff | 2018-11-15 12:22:51 -0700 | [diff] [blame] | 441 | if (!queue_is_mq(q)) { |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 442 | q->nr_zones = args.nr_zones; |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 446 | /* |
| 447 | * Ensure that all memory allocations in this context are done as |
| 448 | * if GFP_NOIO was specified. |
| 449 | */ |
| 450 | if (args.nr_zones) { |
| 451 | unsigned int noio_flag = memalloc_noio_save(); |
| 452 | |
| 453 | ret = disk->fops->report_zones(disk, 0, args.nr_zones, |
| 454 | blk_revalidate_zone_cb, &args); |
| 455 | memalloc_noio_restore(noio_flag); |
| 456 | } |
Damien Le Moal | bd976e5 | 2019-07-01 14:09:16 +0900 | [diff] [blame] | 457 | |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 458 | /* |
| 459 | * Install the new bitmaps, making sure the queue is stopped and |
| 460 | * all I/Os are completed (i.e. a scheduler is not referencing the |
| 461 | * bitmaps). |
| 462 | */ |
| 463 | blk_mq_freeze_queue(q); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 464 | if (ret >= 0) { |
Christoph Hellwig | e94f581 | 2019-12-03 10:39:06 +0100 | [diff] [blame^] | 465 | q->nr_zones = args.nr_zones; |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 466 | swap(q->seq_zones_wlock, args.seq_zones_wlock); |
Christoph Hellwig | f216fdd | 2019-12-03 10:39:05 +0100 | [diff] [blame] | 467 | swap(q->conv_zones_bitmap, args.conv_zones_bitmap); |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 468 | ret = 0; |
| 469 | } else { |
| 470 | pr_warn("%s: failed to revalidate zones\n", disk->disk_name); |
| 471 | blk_queue_free_zone_bitmaps(q); |
| 472 | } |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 473 | blk_mq_unfreeze_queue(q); |
| 474 | |
Christoph Hellwig | d410035 | 2019-11-11 11:39:30 +0900 | [diff] [blame] | 475 | kfree(args.seq_zones_wlock); |
Christoph Hellwig | f216fdd | 2019-12-03 10:39:05 +0100 | [diff] [blame] | 476 | kfree(args.conv_zones_bitmap); |
Damien Le Moal | bf50545 | 2018-10-12 19:08:50 +0900 | [diff] [blame] | 477 | return ret; |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones); |