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 /* If we have an internal callback, call it first. */ 117 if (args->cb) { 118 int ret; 119 120 ret = args->cb(zone, args->zone_idx, args->data); 121 if (ret) 122 return ret; 123 } 124 125 return disk_report_zone(args->disk, zone, args->zone_idx++, 126 args->rep_args); 127 } 128 129 /* 130 * Helper for drivers of zoned targets to implement struct target_type 131 * report_zones operation. 132 */ 133 int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector, 134 struct dm_report_zones_args *args, unsigned int nr_zones) 135 { 136 /* 137 * Set the target mapping start sector first so that 138 * dm_report_zones_cb() can correctly remap zone information. 139 */ 140 args->start = start; 141 142 return blkdev_report_zones(bdev, sector, nr_zones, 143 dm_report_zones_cb, args); 144 } 145 EXPORT_SYMBOL_GPL(dm_report_zones); 146 147 bool dm_is_zone_write(struct mapped_device *md, struct bio *bio) 148 { 149 struct request_queue *q = md->queue; 150 151 if (!blk_queue_is_zoned(q)) 152 return false; 153 154 switch (bio_op(bio)) { 155 case REQ_OP_WRITE_ZEROES: 156 case REQ_OP_WRITE: 157 return !op_is_flush(bio->bi_opf) && bio_sectors(bio); 158 default: 159 return false; 160 } 161 } 162 163 /* 164 * Revalidate the zones of a mapped device to initialize resource necessary 165 * for zone append emulation. Note that we cannot simply use the block layer 166 * blk_revalidate_disk_zones() function here as the mapped device is suspended 167 * (this is called from __bind() context). 168 */ 169 int dm_revalidate_zones(struct dm_table *t, struct request_queue *q) 170 { 171 struct mapped_device *md = t->md; 172 struct gendisk *disk = md->disk; 173 unsigned int nr_zones = disk->nr_zones; 174 int ret; 175 176 if (!get_capacity(disk)) 177 return 0; 178 179 /* 180 * Do not revalidate if zone write plug resources have already 181 * been allocated. 182 */ 183 if (dm_has_zone_plugs(md)) 184 return 0; 185 186 DMINFO("%s using %s zone append", disk->disk_name, 187 queue_emulates_zone_append(q) ? "emulated" : "native"); 188 189 /* 190 * Our table is not live yet. So the call to dm_get_live_table() 191 * in dm_blk_report_zones() will fail. Set a temporary pointer to 192 * our table for dm_blk_report_zones() to use directly. 193 */ 194 md->zone_revalidate_map = t; 195 md->revalidate_map_task = current; 196 ret = blk_revalidate_disk_zones(disk); 197 md->revalidate_map_task = NULL; 198 md->zone_revalidate_map = NULL; 199 200 if (ret) { 201 DMERR("Revalidate zones failed %d", ret); 202 disk->nr_zones = nr_zones; 203 return ret; 204 } 205 206 md->nr_zones = disk->nr_zones; 207 208 return 0; 209 } 210 211 static int device_not_zone_append_capable(struct dm_target *ti, 212 struct dm_dev *dev, sector_t start, 213 sector_t len, void *data) 214 { 215 return !bdev_is_zoned(dev->bdev); 216 } 217 218 static bool dm_table_supports_zone_append(struct dm_table *t) 219 { 220 for (unsigned int i = 0; i < t->num_targets; i++) { 221 struct dm_target *ti = dm_table_get_target(t, i); 222 223 if (ti->emulate_zone_append) 224 return false; 225 226 if (!ti->type->iterate_devices || 227 ti->type->iterate_devices(ti, device_not_zone_append_capable, NULL)) 228 return false; 229 } 230 231 return true; 232 } 233 234 struct dm_device_zone_count { 235 sector_t start; 236 sector_t len; 237 unsigned int total_nr_seq_zones; 238 unsigned int target_nr_seq_zones; 239 }; 240 241 /* 242 * Count the total number of and the number of mapped sequential zones of a 243 * target zoned device. 244 */ 245 static int dm_device_count_zones_cb(struct blk_zone *zone, 246 unsigned int idx, void *data) 247 { 248 struct dm_device_zone_count *zc = data; 249 250 if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { 251 zc->total_nr_seq_zones++; 252 if (zone->start >= zc->start && 253 zone->start < zc->start + zc->len) 254 zc->target_nr_seq_zones++; 255 } 256 257 return 0; 258 } 259 260 static int dm_device_count_zones(struct dm_dev *dev, 261 struct dm_device_zone_count *zc) 262 { 263 int ret; 264 265 ret = blkdev_report_zones(dev->bdev, 0, BLK_ALL_ZONES, 266 dm_device_count_zones_cb, zc); 267 if (ret < 0) 268 return ret; 269 if (!ret) 270 return -EIO; 271 return 0; 272 } 273 274 struct dm_zone_resource_limits { 275 unsigned int mapped_nr_seq_zones; 276 struct queue_limits *lim; 277 bool reliable_limits; 278 }; 279 280 static int device_get_zone_resource_limits(struct dm_target *ti, 281 struct dm_dev *dev, sector_t start, 282 sector_t len, void *data) 283 { 284 struct dm_zone_resource_limits *zlim = data; 285 struct gendisk *disk = dev->bdev->bd_disk; 286 unsigned int max_open_zones, max_active_zones; 287 int ret; 288 struct dm_device_zone_count zc = { 289 .start = start, 290 .len = len, 291 }; 292 293 /* 294 * If the target is not the whole device, the device zone resources may 295 * be shared between different targets. Check this by counting the 296 * number of mapped sequential zones: if this number is smaller than the 297 * total number of sequential zones of the target device, then resource 298 * sharing may happen and the zone limits will not be reliable. 299 */ 300 ret = dm_device_count_zones(dev, &zc); 301 if (ret) { 302 DMERR("Count %s zones failed %d", disk->disk_name, ret); 303 return ret; 304 } 305 306 /* 307 * If the target does not map any sequential zones, then we do not need 308 * any zone resource limits. 309 */ 310 if (!zc.target_nr_seq_zones) 311 return 0; 312 313 /* 314 * If the target does not map all sequential zones, the limits 315 * will not be reliable and we cannot use REQ_OP_ZONE_RESET_ALL. 316 */ 317 if (zc.target_nr_seq_zones < zc.total_nr_seq_zones) { 318 zlim->reliable_limits = false; 319 ti->zone_reset_all_supported = false; 320 } 321 322 /* 323 * If the target maps less sequential zones than the limit values, then 324 * we do not have limits for this target. 325 */ 326 max_active_zones = disk->queue->limits.max_active_zones; 327 if (max_active_zones >= zc.target_nr_seq_zones) 328 max_active_zones = 0; 329 zlim->lim->max_active_zones = 330 min_not_zero(max_active_zones, zlim->lim->max_active_zones); 331 332 max_open_zones = disk->queue->limits.max_open_zones; 333 if (max_open_zones >= zc.target_nr_seq_zones) 334 max_open_zones = 0; 335 zlim->lim->max_open_zones = 336 min_not_zero(max_open_zones, zlim->lim->max_open_zones); 337 338 /* 339 * Also count the total number of sequential zones for the mapped 340 * device so that when we are done inspecting all its targets, we are 341 * able to check if the mapped device actually has any sequential zones. 342 */ 343 zlim->mapped_nr_seq_zones += zc.target_nr_seq_zones; 344 345 return 0; 346 } 347 348 int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q, 349 struct queue_limits *lim) 350 { 351 struct mapped_device *md = t->md; 352 struct gendisk *disk = md->disk; 353 struct dm_zone_resource_limits zlim = { 354 .reliable_limits = true, 355 .lim = lim, 356 }; 357 358 /* 359 * Check if zone append is natively supported, and if not, set the 360 * mapped device queue as needing zone append emulation. If zone 361 * append is natively supported, make sure that 362 * max_hw_zone_append_sectors is not set to 0. 363 */ 364 WARN_ON_ONCE(queue_is_mq(q)); 365 if (!dm_table_supports_zone_append(t)) 366 lim->max_hw_zone_append_sectors = 0; 367 else if (lim->max_hw_zone_append_sectors == 0) 368 lim->max_hw_zone_append_sectors = lim->max_zone_append_sectors; 369 370 /* 371 * Determine the max open and max active zone limits for the mapped 372 * device by inspecting the zone resource limits and the zones mapped 373 * by each target. 374 */ 375 for (unsigned int i = 0; i < t->num_targets; i++) { 376 struct dm_target *ti = dm_table_get_target(t, i); 377 378 /* 379 * Assume that the target can accept REQ_OP_ZONE_RESET_ALL. 380 * device_get_zone_resource_limits() may adjust this if one of 381 * the device used by the target does not have all its 382 * sequential write required zones mapped. 383 */ 384 ti->zone_reset_all_supported = true; 385 386 if (!ti->type->iterate_devices || 387 ti->type->iterate_devices(ti, 388 device_get_zone_resource_limits, &zlim)) { 389 DMERR("Could not determine %s zone resource limits", 390 disk->disk_name); 391 return -ENODEV; 392 } 393 } 394 395 /* 396 * If we only have conventional zones mapped, expose the mapped device 397 + as a regular device. 398 */ 399 if (!zlim.mapped_nr_seq_zones) { 400 lim->max_open_zones = 0; 401 lim->max_active_zones = 0; 402 lim->max_hw_zone_append_sectors = 0; 403 lim->max_zone_append_sectors = 0; 404 lim->zone_write_granularity = 0; 405 lim->chunk_sectors = 0; 406 lim->features &= ~BLK_FEAT_ZONED; 407 return 0; 408 } 409 410 if (get_capacity(disk) && dm_has_zone_plugs(t->md)) { 411 if (q->limits.chunk_sectors != lim->chunk_sectors) { 412 DMWARN("%s: device has zone write plug resources. " 413 "Cannot change zone size", 414 disk->disk_name); 415 return -EINVAL; 416 } 417 if (lim->max_hw_zone_append_sectors != 0 && 418 !dm_table_is_wildcard(t)) { 419 DMWARN("%s: device has zone write plug resources. " 420 "New table must emulate zone append", 421 disk->disk_name); 422 return -EINVAL; 423 } 424 } 425 /* 426 * Warn once (when the capacity is not yet set) if the mapped device is 427 * partially using zone resources of the target devices as that leads to 428 * unreliable limits, i.e. if another mapped device uses the same 429 * underlying devices, we cannot enforce zone limits to guarantee that 430 * writing will not lead to errors. Note that we really should return 431 * an error for such case but there is no easy way to find out if 432 * another mapped device uses the same underlying zoned devices. 433 */ 434 if (!get_capacity(disk) && !zlim.reliable_limits) 435 DMWARN("%s zone resource limits may be unreliable", 436 disk->disk_name); 437 438 if (lim->features & BLK_FEAT_ZONED && 439 !static_key_enabled(&zoned_enabled.key)) 440 static_branch_enable(&zoned_enabled); 441 return 0; 442 } 443 444 void dm_finalize_zone_settings(struct dm_table *t, struct queue_limits *lim) 445 { 446 struct mapped_device *md = t->md; 447 448 if (lim->features & BLK_FEAT_ZONED) { 449 if (dm_table_supports_zone_append(t)) 450 clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); 451 else 452 set_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); 453 } else { 454 clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags); 455 md->nr_zones = 0; 456 md->disk->nr_zones = 0; 457 } 458 } 459 460 461 /* 462 * IO completion callback called from clone_endio(). 463 */ 464 void dm_zone_endio(struct dm_io *io, struct bio *clone) 465 { 466 struct mapped_device *md = io->md; 467 struct gendisk *disk = md->disk; 468 struct bio *orig_bio = io->orig_bio; 469 470 /* 471 * Get the offset within the zone of the written sector 472 * and add that to the original bio sector position. 473 */ 474 if (clone->bi_status == BLK_STS_OK && 475 bio_op(clone) == REQ_OP_ZONE_APPEND) { 476 orig_bio->bi_iter.bi_sector += 477 bdev_offset_from_zone_start(disk->part0, 478 clone->bi_iter.bi_sector); 479 } 480 } 481 482 static int dm_zone_need_reset_cb(struct blk_zone *zone, unsigned int idx, 483 void *data) 484 { 485 /* 486 * For an all-zones reset, ignore conventional, empty, read-only 487 * and offline zones. 488 */ 489 switch (zone->cond) { 490 case BLK_ZONE_COND_NOT_WP: 491 case BLK_ZONE_COND_EMPTY: 492 case BLK_ZONE_COND_READONLY: 493 case BLK_ZONE_COND_OFFLINE: 494 return 0; 495 default: 496 set_bit(idx, (unsigned long *)data); 497 return 0; 498 } 499 } 500 501 int dm_zone_get_reset_bitmap(struct mapped_device *md, struct dm_table *t, 502 sector_t sector, unsigned int nr_zones, 503 unsigned long *need_reset) 504 { 505 struct dm_report_zones_args args = { 506 .disk = md->disk, 507 .next_sector = sector, 508 .cb = dm_zone_need_reset_cb, 509 .data = need_reset, 510 }; 511 int ret; 512 513 ret = dm_blk_do_report_zones(md, t, nr_zones, &args); 514 if (ret != nr_zones) { 515 DMERR("Get %s zone reset bitmap failed\n", 516 md->disk->disk_name); 517 return -EIO; 518 } 519 520 return 0; 521 } 522