1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2021 Western Digital Corporation or its affiliates. 4 */ 5 6 #include <linux/blkdev.h> 7 #include <linux/mm.h> 8 #include <linux/sched/mm.h> 9 #include <linux/slab.h> 10 #include <linux/bitmap.h> 11 12 #include "dm-core.h" 13 14 #define DM_MSG_PREFIX "zone" 15 16 /* 17 * For internal zone reports bypassing the top BIO submission path. 18 */ 19 static int dm_blk_do_report_zones(struct mapped_device *md, struct dm_table *t, 20 unsigned int nr_zones, 21 struct dm_report_zones_args *args) 22 { 23 do { 24 struct dm_target *tgt; 25 int ret; 26 27 tgt = dm_table_find_target(t, args->next_sector); 28 if (WARN_ON_ONCE(!tgt->type->report_zones)) 29 return -EIO; 30 31 args->tgt = tgt; 32 ret = tgt->type->report_zones(tgt, args, 33 nr_zones - args->zone_idx); 34 if (ret < 0) 35 return ret; 36 } while (args->zone_idx < nr_zones && 37 args->next_sector < get_capacity(md->disk)); 38 39 return args->zone_idx; 40 } 41 42 /* 43 * User facing dm device block device report zone operation. This calls the 44 * report_zones operation for each target of a device table. This operation is 45 * generally implemented by targets using dm_report_zones(). 46 */ 47 int dm_blk_report_zones(struct gendisk *disk, sector_t sector, 48 unsigned int nr_zones, 49 struct blk_report_zones_args *args) 50 { 51 struct mapped_device *md = disk->private_data; 52 struct dm_table *map; 53 struct dm_table *zone_revalidate_map = md->zone_revalidate_map; 54 int srcu_idx, ret = -EIO; 55 bool put_table = false; 56 57 if (!zone_revalidate_map || md->revalidate_map_task != current) { 58 /* 59 * Regular user context or 60 * Zone revalidation during __bind() is in progress, but this 61 * call is from a different process 62 */ 63 if (dm_suspended_md(md)) 64 return -EAGAIN; 65 66 map = dm_get_live_table(md, &srcu_idx); 67 put_table = true; 68 } else { 69 /* Zone revalidation during __bind() */ 70 map = zone_revalidate_map; 71 } 72 73 if (map) { 74 struct dm_report_zones_args dm_args = { 75 .disk = md->disk, 76 .next_sector = sector, 77 .rep_args = args, 78 }; 79 ret = dm_blk_do_report_zones(md, map, nr_zones, &dm_args); 80 } 81 82 if (put_table) 83 dm_put_live_table(md, srcu_idx); 84 85 return ret; 86 } 87 88 static int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, 89 void *data) 90 { 91 struct dm_report_zones_args *args = data; 92 sector_t sector_diff = args->tgt->begin - args->start; 93 94 /* 95 * Ignore zones beyond the target range. 96 */ 97 if (zone->start >= args->start + args->tgt->len) 98 return 0; 99 100 /* 101 * Remap the start sector and write pointer position of the zone 102 * to match its position in the target range. 103 */ 104 zone->start += sector_diff; 105 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { 106 if (zone->cond == BLK_ZONE_COND_FULL) 107 zone->wp = zone->start + zone->len; 108 else if (zone->cond == BLK_ZONE_COND_EMPTY) 109 zone->wp = zone->start; 110 else 111 zone->wp += sector_diff; 112 } 113 114 args->next_sector = zone->start + zone->len; 115 116 return disk_report_zone(args->disk, zone, args->zone_idx++, 117 args->rep_args); 118 } 119 120 /* 121 * Helper for drivers of zoned targets to implement struct target_type 122 * report_zones operation. 123 */ 124 int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector, 125 struct dm_report_zones_args *args, unsigned int nr_zones) 126 { 127 /* 128 * Set the target mapping start sector first so that 129 * dm_report_zones_cb() can correctly remap zone information. 130 */ 131 args->start = start; 132 133 return blkdev_report_zones(bdev, sector, nr_zones, 134 dm_report_zones_cb, args); 135 } 136 EXPORT_SYMBOL_GPL(dm_report_zones); 137 138 bool dm_is_zone_write(struct mapped_device *md, struct bio *bio) 139 { 140 struct request_queue *q = md->queue; 141 142 if (!blk_queue_is_zoned(q)) 143 return false; 144 145 switch (bio_op(bio)) { 146 case REQ_OP_WRITE_ZEROES: 147 case REQ_OP_WRITE: 148 return !op_is_flush(bio->bi_opf) && bio_sectors(bio); 149 default: 150 return false; 151 } 152 } 153 154 /* 155 * Revalidate the zones of a mapped device to initialize resource necessary 156 * for zone append emulation. Note that we cannot simply use the block layer 157 * blk_revalidate_disk_zones() function here as the mapped device is suspended 158 * (this is called from __bind() context). 159 */ 160 int dm_revalidate_zones(struct dm_table *t, struct request_queue *q) 161 { 162 struct mapped_device *md = t->md; 163 struct gendisk *disk = md->disk; 164 unsigned int nr_zones = disk->nr_zones; 165 int ret; 166 167 if (!get_capacity(disk)) 168 return 0; 169 170 /* 171 * Do not revalidate if zone write plug resources have already 172 * been allocated. 173 */ 174 if (dm_has_zone_plugs(md)) 175 return 0; 176 177 DMINFO("%s using %s zone append", disk->disk_name, 178 queue_emulates_zone_append(q) ? "emulated" : "native"); 179 180 /* 181 * Our table is not live yet. So the call to dm_get_live_table() 182 * in dm_blk_report_zones() will fail. Set a temporary pointer to 183 * our table for dm_blk_report_zones() to use directly. 184 */ 185 md->zone_revalidate_map = t; 186 md->revalidate_map_task = current; 187 ret = blk_revalidate_disk_zones(disk); 188 md->revalidate_map_task = NULL; 189 md->zone_revalidate_map = NULL; 190 191 if (ret) { 192 DMERR("Revalidate zones failed %d", ret); 193 disk->nr_zones = nr_zones; 194 return ret; 195 } 196 197 md->nr_zones = disk->nr_zones; 198 199 return 0; 200 } 201 202 static int device_not_zone_append_capable(struct dm_target *ti, 203 struct dm_dev *dev, sector_t start, 204 sector_t len, void *data) 205 { 206 return !bdev_is_zoned(dev->bdev); 207 } 208 209 static bool dm_table_supports_zone_append(struct dm_table *t) 210 { 211 for (unsigned int i = 0; i < t->num_targets; i++) { 212 struct dm_target *ti = dm_table_get_target(t, i); 213 214 if (ti->emulate_zone_append) 215 return false; 216 217 if (!ti->type->iterate_devices || 218 ti->type->iterate_devices(ti, device_not_zone_append_capable, NULL)) 219 return false; 220 } 221 222 return true; 223 } 224 225 struct dm_device_zone_count { 226 sector_t start; 227 sector_t len; 228 unsigned int total_nr_seq_zones; 229 unsigned int target_nr_seq_zones; 230 }; 231 232 /* 233 * Count the total number of and the number of mapped sequential zones of a 234 * target zoned device. 235 */ 236 static int dm_device_count_zones_cb(struct blk_zone *zone, 237 unsigned int idx, void *data) 238 { 239 struct dm_device_zone_count *zc = data; 240 241 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { 242 zc->total_nr_seq_zones++; 243 if (zone->start >= zc->start && 244 zone->start < zc->start + zc->len) 245 zc->target_nr_seq_zones++; 246 } 247 248 return 0; 249 } 250 251 static int dm_device_count_zones(struct dm_dev *dev, 252 struct dm_device_zone_count *zc) 253 { 254 int ret; 255 256 ret = blkdev_report_zones(dev->bdev, 0, BLK_ALL_ZONES, 257 dm_device_count_zones_cb, zc); 258 if (ret < 0) 259 return ret; 260 if (!ret) 261 return -EIO; 262 return 0; 263 } 264 265 struct dm_zone_resource_limits { 266 unsigned int mapped_nr_seq_zones; 267 struct queue_limits *lim; 268 bool reliable_limits; 269 }; 270 271 static int device_get_zone_resource_limits(struct dm_target *ti, 272 struct dm_dev *dev, sector_t start, 273 sector_t len, void *data) 274 { 275 struct dm_zone_resource_limits *zlim = data; 276 struct gendisk *disk = dev->bdev->bd_disk; 277 unsigned int max_open_zones, max_active_zones; 278 int ret; 279 struct dm_device_zone_count zc = { 280 .start = start, 281 .len = len, 282 }; 283 284 /* 285 * If the target is not the whole device, the device zone resources may 286 * be shared between different targets. Check this by counting the 287 * number of mapped sequential zones: if this number is smaller than the 288 * total number of sequential zones of the target device, then resource 289 * sharing may happen and the zone limits will not be reliable. 290 */ 291 ret = dm_device_count_zones(dev, &zc); 292 if (ret) { 293 DMERR("Count %s zones failed %d", disk->disk_name, ret); 294 return ret; 295 } 296 297 /* 298 * If the target does not map any sequential zones, then we do not need 299 * any zone resource limits. 300 */ 301 if (!zc.target_nr_seq_zones) 302 return 0; 303 304 /* 305 * If the target does not map all sequential zones, the limits 306 * will not be reliable and we cannot use REQ_OP_ZONE_RESET_ALL. 307 */ 308 if (zc.target_nr_seq_zones < zc.total_nr_seq_zones) { 309 zlim->reliable_limits = false; 310 ti->zone_reset_all_supported = false; 311 } 312 313 /* 314 * If the target maps less sequential zones than the limit values, then 315 * we do not have limits for this target. 316 */ 317 max_active_zones = disk->queue->limits.max_active_zones; 318 if (max_active_zones >= zc.target_nr_seq_zones) 319 max_active_zones = 0; 320 zlim->lim->max_active_zones = 321 min_not_zero(max_active_zones, zlim->lim->max_active_zones); 322 323 max_open_zones = disk->queue->limits.max_open_zones; 324 if (max_open_zones >= zc.target_nr_seq_zones) 325 max_open_zones = 0; 326 zlim->lim->max_open_zones = 327 min_not_zero(max_open_zones, zlim->lim->max_open_zones); 328 329 /* 330 * Also count the total number of sequential zones for the mapped 331 * device so that when we are done inspecting all its targets, we are 332 * able to check if the mapped device actually has any sequential zones. 333 */ 334 zlim->mapped_nr_seq_zones += zc.target_nr_seq_zones; 335 336 return 0; 337 } 338 339 int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q, 340 struct queue_limits *lim) 341 { 342 struct mapped_device *md = t->md; 343 struct gendisk *disk = md->disk; 344 struct dm_zone_resource_limits zlim = { 345 .reliable_limits = true, 346 .lim = lim, 347 }; 348 349 /* 350 * Check if zone append is natively supported, and if not, set the 351 * mapped device queue as needing zone append emulation. If zone 352 * append is natively supported, make sure that 353 * max_hw_zone_append_sectors is not set to 0. 354 */ 355 WARN_ON_ONCE(queue_is_mq(q)); 356 if (!dm_table_supports_zone_append(t)) 357 lim->max_hw_zone_append_sectors = 0; 358 else if (lim->max_hw_zone_append_sectors == 0) 359 lim->max_hw_zone_append_sectors = lim->max_zone_append_sectors; 360 361 /* 362 * Determine the max open and max active zone limits for the mapped 363 * device by inspecting the zone resource limits and the zones mapped 364 * by each target. 365 */ 366 for (unsigned int i = 0; i < t->num_targets; i++) { 367 struct dm_target *ti = dm_table_get_target(t, i); 368 369 /* 370 * Assume that the target can accept REQ_OP_ZONE_RESET_ALL. 371 * device_get_zone_resource_limits() may adjust this if one of 372 * the device used by the target does not have all its 373 * sequential write required zones mapped. 374 */ 375 ti->zone_reset_all_supported = true; 376 377 if (!ti->type->iterate_devices || 378 ti->type->iterate_devices(ti, 379 device_get_zone_resource_limits, &zlim)) { 380 DMERR("Could not determine %s zone resource limits", 381 disk->disk_name); 382 return -ENODEV; 383 } 384 } 385 386 /* 387 * If we only have conventional zones mapped, expose the mapped device 388 + as a regular device. 389 */ 390 if (!zlim.mapped_nr_seq_zones) { 391 lim->max_open_zones = 0; 392 lim->max_active_zones = 0; 393 lim->max_hw_zone_append_sectors = 0; 394 lim->max_zone_append_sectors = 0; 395 lim->zone_write_granularity = 0; 396 lim->chunk_sectors = 0; 397 lim->features &= ~BLK_FEAT_ZONED; 398 return 0; 399 } 400 401 if (get_capacity(disk) && dm_has_zone_plugs(t->md)) { 402 if (q->limits.chunk_sectors != lim->chunk_sectors) { 403 DMWARN("%s: device has zone write plug resources. " 404 "Cannot change zone size", 405 disk->disk_name); 406 return -EINVAL; 407 } 408 if (lim->max_hw_zone_append_sectors != 0 && 409 !dm_table_is_wildcard(t)) { 410 DMWARN("%s: device has zone write plug resources. " 411 "New table must emulate zone append", 412 disk->disk_name); 413 return -EINVAL; 414 } 415 } 416 /* 417 * Warn once (when the capacity is not yet set) if the mapped device is 418 * partially using zone resources of the target devices as that leads to 419 * unreliable limits, i.e. if another mapped device uses the same 420 * underlying devices, we cannot enforce zone limits to guarantee that 421 * writing will not lead to errors. Note that we really should return 422 * an error for such case but there is no easy way to find out if 423 * another mapped device uses the same underlying zoned devices. 424 */ 425 if (!get_capacity(disk) && !zlim.reliable_limits) 426 DMWARN("%s zone resource limits may be unreliable", 427 disk->disk_name); 428 429 if (lim->features & BLK_FEAT_ZONED && 430 !static_key_enabled(&zoned_enabled.key)) 431 static_branch_enable(&zoned_enabled); 432 return 0; 433 } 434 435 void dm_finalize_zone_settings(struct dm_table *t, struct queue_limits *lim) 436 { 437 struct mapped_device *md = t->md; 438 439 if (lim->features & BLK_FEAT_ZONED) { 440 if (dm_table_supports_zone_append(t)) 441 clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); 442 else 443 set_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); 444 } else { 445 clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); 446 md->nr_zones = 0; 447 md->disk->nr_zones = 0; 448 } 449 } 450 451 452 /* 453 * IO completion callback called from clone_endio(). 454 */ 455 void dm_zone_endio(struct dm_io *io, struct bio *clone) 456 { 457 struct mapped_device *md = io->md; 458 struct gendisk *disk = md->disk; 459 struct bio *orig_bio = io->orig_bio; 460 461 /* 462 * Get the offset within the zone of the written sector 463 * and add that to the original bio sector position. 464 */ 465 if (clone->bi_status == BLK_STS_OK && 466 bio_op(clone) == REQ_OP_ZONE_APPEND) { 467 orig_bio->bi_iter.bi_sector += 468 bdev_offset_from_zone_start(disk->part0, 469 clone->bi_iter.bi_sector); 470 } 471 } 472 473 static int dm_zone_need_reset_cb(struct blk_zone *zone, unsigned int idx, 474 void *data) 475 { 476 /* 477 * For an all-zones reset, ignore conventional, empty, read-only 478 * and offline zones. 479 */ 480 switch (zone->cond) { 481 case BLK_ZONE_COND_NOT_WP: 482 case BLK_ZONE_COND_EMPTY: 483 case BLK_ZONE_COND_READONLY: 484 case BLK_ZONE_COND_OFFLINE: 485 return 0; 486 default: 487 set_bit(idx, (unsigned long *)data); 488 return 0; 489 } 490 } 491 492 int dm_zone_get_reset_bitmap(struct mapped_device *md, struct dm_table *t, 493 sector_t sector, unsigned int nr_zones, 494 unsigned long *need_reset) 495 { 496 struct dm_report_zones_args args = { 497 .disk = md->disk, 498 .next_sector = sector, 499 .cb = dm_zone_need_reset_cb, 500 .data = need_reset, 501 }; 502 int ret; 503 504 ret = dm_blk_do_report_zones(md, t, nr_zones, &args); 505 if (ret != nr_zones) { 506 DMERR("Get %s zone reset bitmap failed\n", 507 md->disk->disk_name); 508 return -EIO; 509 } 510 511 return 0; 512 } 513