dm-zone.c 12 KB

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