dm-zoned.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2017 Western Digital Corporation or its affiliates.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #ifndef DM_ZONED_H
  8. #define DM_ZONED_H
  9. #include <linux/types.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/device-mapper.h>
  12. #include <linux/dm-kcopyd.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/mutex.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/radix-tree.h>
  20. #include <linux/shrinker.h>
  21. /*
  22. * dm-zoned creates block devices with 4KB blocks, always.
  23. */
  24. #define DMZ_BLOCK_SHIFT 12
  25. #define DMZ_BLOCK_SIZE (1 << DMZ_BLOCK_SHIFT)
  26. #define DMZ_BLOCK_MASK (DMZ_BLOCK_SIZE - 1)
  27. #define DMZ_BLOCK_SHIFT_BITS (DMZ_BLOCK_SHIFT + 3)
  28. #define DMZ_BLOCK_SIZE_BITS (1 << DMZ_BLOCK_SHIFT_BITS)
  29. #define DMZ_BLOCK_MASK_BITS (DMZ_BLOCK_SIZE_BITS - 1)
  30. #define DMZ_BLOCK_SECTORS_SHIFT (DMZ_BLOCK_SHIFT - SECTOR_SHIFT)
  31. #define DMZ_BLOCK_SECTORS (DMZ_BLOCK_SIZE >> SECTOR_SHIFT)
  32. #define DMZ_BLOCK_SECTORS_MASK (DMZ_BLOCK_SECTORS - 1)
  33. /*
  34. * 4KB block <-> 512B sector conversion.
  35. */
  36. #define dmz_blk2sect(b) ((sector_t)(b) << DMZ_BLOCK_SECTORS_SHIFT)
  37. #define dmz_sect2blk(s) ((sector_t)(s) >> DMZ_BLOCK_SECTORS_SHIFT)
  38. #define dmz_bio_block(bio) dmz_sect2blk((bio)->bi_iter.bi_sector)
  39. #define dmz_bio_blocks(bio) dmz_sect2blk(bio_sectors(bio))
  40. /*
  41. * Zoned block device information.
  42. */
  43. struct dmz_dev {
  44. struct block_device *bdev;
  45. char name[BDEVNAME_SIZE];
  46. sector_t capacity;
  47. unsigned int nr_zones;
  48. unsigned int flags;
  49. sector_t zone_nr_sectors;
  50. unsigned int zone_nr_sectors_shift;
  51. sector_t zone_nr_blocks;
  52. sector_t zone_nr_blocks_shift;
  53. };
  54. #define dmz_bio_chunk(dev, bio) ((bio)->bi_iter.bi_sector >> \
  55. (dev)->zone_nr_sectors_shift)
  56. #define dmz_chunk_block(dev, b) ((b) & ((dev)->zone_nr_blocks - 1))
  57. /* Device flags. */
  58. #define DMZ_BDEV_DYING (1 << 0)
  59. #define DMZ_CHECK_BDEV (2 << 0)
  60. /*
  61. * Zone descriptor.
  62. */
  63. struct dm_zone {
  64. /* For listing the zone depending on its state */
  65. struct list_head link;
  66. /* Zone type and state */
  67. unsigned long flags;
  68. /* Zone activation reference count */
  69. atomic_t refcount;
  70. /* Zone write pointer block (relative to the zone start block) */
  71. unsigned int wp_block;
  72. /* Zone weight (number of valid blocks in the zone) */
  73. unsigned int weight;
  74. /* The chunk that the zone maps */
  75. unsigned int chunk;
  76. /*
  77. * For a sequential data zone, pointer to the random zone
  78. * used as a buffer for processing unaligned writes.
  79. * For a buffer zone, this points back to the data zone.
  80. */
  81. struct dm_zone *bzone;
  82. };
  83. /*
  84. * Zone flags.
  85. */
  86. enum {
  87. /* Zone write type */
  88. DMZ_RND,
  89. DMZ_SEQ,
  90. /* Zone critical condition */
  91. DMZ_OFFLINE,
  92. DMZ_READ_ONLY,
  93. /* How the zone is being used */
  94. DMZ_META,
  95. DMZ_DATA,
  96. DMZ_BUF,
  97. /* Zone internal state */
  98. DMZ_RECLAIM,
  99. DMZ_SEQ_WRITE_ERR,
  100. };
  101. /*
  102. * Zone data accessors.
  103. */
  104. #define dmz_is_rnd(z) test_bit(DMZ_RND, &(z)->flags)
  105. #define dmz_is_seq(z) test_bit(DMZ_SEQ, &(z)->flags)
  106. #define dmz_is_empty(z) ((z)->wp_block == 0)
  107. #define dmz_is_offline(z) test_bit(DMZ_OFFLINE, &(z)->flags)
  108. #define dmz_is_readonly(z) test_bit(DMZ_READ_ONLY, &(z)->flags)
  109. #define dmz_in_reclaim(z) test_bit(DMZ_RECLAIM, &(z)->flags)
  110. #define dmz_seq_write_err(z) test_bit(DMZ_SEQ_WRITE_ERR, &(z)->flags)
  111. #define dmz_is_meta(z) test_bit(DMZ_META, &(z)->flags)
  112. #define dmz_is_buf(z) test_bit(DMZ_BUF, &(z)->flags)
  113. #define dmz_is_data(z) test_bit(DMZ_DATA, &(z)->flags)
  114. #define dmz_weight(z) ((z)->weight)
  115. /*
  116. * Message functions.
  117. */
  118. #define dmz_dev_info(dev, format, args...) \
  119. DMINFO("(%s): " format, (dev)->name, ## args)
  120. #define dmz_dev_err(dev, format, args...) \
  121. DMERR("(%s): " format, (dev)->name, ## args)
  122. #define dmz_dev_warn(dev, format, args...) \
  123. DMWARN("(%s): " format, (dev)->name, ## args)
  124. #define dmz_dev_debug(dev, format, args...) \
  125. DMDEBUG("(%s): " format, (dev)->name, ## args)
  126. struct dmz_metadata;
  127. struct dmz_reclaim;
  128. /*
  129. * Functions defined in dm-zoned-metadata.c
  130. */
  131. int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **zmd);
  132. void dmz_dtr_metadata(struct dmz_metadata *zmd);
  133. int dmz_resume_metadata(struct dmz_metadata *zmd);
  134. void dmz_lock_map(struct dmz_metadata *zmd);
  135. void dmz_unlock_map(struct dmz_metadata *zmd);
  136. void dmz_lock_metadata(struct dmz_metadata *zmd);
  137. void dmz_unlock_metadata(struct dmz_metadata *zmd);
  138. void dmz_lock_flush(struct dmz_metadata *zmd);
  139. void dmz_unlock_flush(struct dmz_metadata *zmd);
  140. int dmz_flush_metadata(struct dmz_metadata *zmd);
  141. unsigned int dmz_id(struct dmz_metadata *zmd, struct dm_zone *zone);
  142. sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone);
  143. sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone);
  144. unsigned int dmz_nr_chunks(struct dmz_metadata *zmd);
  145. #define DMZ_ALLOC_RND 0x01
  146. #define DMZ_ALLOC_RECLAIM 0x02
  147. struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags);
  148. void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
  149. void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
  150. unsigned int chunk);
  151. void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
  152. unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd);
  153. unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd);
  154. /*
  155. * Activate a zone (increment its reference count).
  156. */
  157. static inline void dmz_activate_zone(struct dm_zone *zone)
  158. {
  159. atomic_inc(&zone->refcount);
  160. }
  161. /*
  162. * Deactivate a zone. This decrement the zone reference counter
  163. * indicating that all BIOs to the zone have completed when the count is 0.
  164. */
  165. static inline void dmz_deactivate_zone(struct dm_zone *zone)
  166. {
  167. atomic_dec(&zone->refcount);
  168. }
  169. /*
  170. * Test if a zone is active, that is, has a refcount > 0.
  171. */
  172. static inline bool dmz_is_active(struct dm_zone *zone)
  173. {
  174. return atomic_read(&zone->refcount);
  175. }
  176. int dmz_lock_zone_reclaim(struct dm_zone *zone);
  177. void dmz_unlock_zone_reclaim(struct dm_zone *zone);
  178. struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd);
  179. struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd,
  180. unsigned int chunk, int op);
  181. void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *zone);
  182. struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
  183. struct dm_zone *dzone);
  184. int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
  185. sector_t chunk_block, unsigned int nr_blocks);
  186. int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
  187. sector_t chunk_block, unsigned int nr_blocks);
  188. int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
  189. sector_t chunk_block);
  190. int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
  191. sector_t *chunk_block);
  192. int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
  193. struct dm_zone *to_zone);
  194. int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
  195. struct dm_zone *to_zone, sector_t chunk_block);
  196. /*
  197. * Functions defined in dm-zoned-reclaim.c
  198. */
  199. int dmz_ctr_reclaim(struct dmz_dev *dev, struct dmz_metadata *zmd,
  200. struct dmz_reclaim **zrc);
  201. void dmz_dtr_reclaim(struct dmz_reclaim *zrc);
  202. void dmz_suspend_reclaim(struct dmz_reclaim *zrc);
  203. void dmz_resume_reclaim(struct dmz_reclaim *zrc);
  204. void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc);
  205. void dmz_schedule_reclaim(struct dmz_reclaim *zrc);
  206. /*
  207. * Functions defined in dm-zoned-target.c
  208. */
  209. bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev);
  210. bool dmz_check_bdev(struct dmz_dev *dmz_dev);
  211. #endif /* DM_ZONED_H */