dm-zone.c 14 KB

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