dm-zoned.h 7.3 KB

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