vio.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #ifndef VIO_H
  6. #define VIO_H
  7. #include <linux/bio.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/compiler.h>
  10. #include <linux/kernel.h>
  11. #include <linux/list.h>
  12. #include "completion.h"
  13. #include "constants.h"
  14. #include "types.h"
  15. #include "vdo.h"
  16. enum {
  17. MAX_BLOCKS_PER_VIO = (BIO_MAX_VECS << PAGE_SHIFT) / VDO_BLOCK_SIZE,
  18. };
  19. struct pooled_vio {
  20. /* The underlying vio */
  21. struct vio vio;
  22. /* The list entry for chaining pooled vios together */
  23. struct list_head list_entry;
  24. /* The context set by the pool */
  25. void *context;
  26. /* The list entry used by the pool */
  27. struct list_head pool_entry;
  28. /* The pool this vio is allocated from */
  29. struct vio_pool *pool;
  30. };
  31. /**
  32. * as_vio() - Convert a generic vdo_completion to a vio.
  33. * @completion: The completion to convert.
  34. *
  35. * Return: The completion as a vio.
  36. */
  37. static inline struct vio *as_vio(struct vdo_completion *completion)
  38. {
  39. vdo_assert_completion_type(completion, VIO_COMPLETION);
  40. return container_of(completion, struct vio, completion);
  41. }
  42. /**
  43. * get_vio_bio_zone_thread_id() - Get the thread id of the bio zone in which a vio should submit
  44. * its I/O.
  45. * @vio: The vio.
  46. *
  47. * Return: The id of the bio zone thread the vio should use.
  48. */
  49. static inline thread_id_t __must_check get_vio_bio_zone_thread_id(struct vio *vio)
  50. {
  51. return vio->completion.vdo->thread_config.bio_threads[vio->bio_zone];
  52. }
  53. physical_block_number_t __must_check pbn_from_vio_bio(struct bio *bio);
  54. /**
  55. * assert_vio_in_bio_zone() - Check that a vio is running on the correct thread for its bio zone.
  56. * @vio: The vio to check.
  57. */
  58. static inline void assert_vio_in_bio_zone(struct vio *vio)
  59. {
  60. thread_id_t expected = get_vio_bio_zone_thread_id(vio);
  61. thread_id_t thread_id = vdo_get_callback_thread_id();
  62. VDO_ASSERT_LOG_ONLY((expected == thread_id),
  63. "vio I/O for physical block %llu on thread %u, should be on bio zone thread %u",
  64. (unsigned long long) pbn_from_vio_bio(vio->bio), thread_id,
  65. expected);
  66. }
  67. int vdo_create_bio(struct bio **bio_ptr);
  68. void vdo_free_bio(struct bio *bio);
  69. int allocate_vio_components(struct vdo *vdo, enum vio_type vio_type,
  70. enum vio_priority priority, void *parent,
  71. unsigned int block_count, char *data, struct vio *vio);
  72. int __must_check create_multi_block_metadata_vio(struct vdo *vdo, enum vio_type vio_type,
  73. enum vio_priority priority,
  74. void *parent, unsigned int block_count,
  75. char *data, struct vio **vio_ptr);
  76. static inline int __must_check create_metadata_vio(struct vdo *vdo, enum vio_type vio_type,
  77. enum vio_priority priority,
  78. void *parent, char *data,
  79. struct vio **vio_ptr)
  80. {
  81. return create_multi_block_metadata_vio(vdo, vio_type, priority, parent, 1, data,
  82. vio_ptr);
  83. }
  84. void free_vio_components(struct vio *vio);
  85. void free_vio(struct vio *vio);
  86. /**
  87. * initialize_vio() - Initialize a vio.
  88. * @vio: The vio to initialize.
  89. * @bio: The bio this vio should use for its I/O.
  90. * @block_count: The size of this vio in vdo blocks.
  91. * @vio_type: The vio type.
  92. * @priority: The relative priority of the vio.
  93. * @vdo: The vdo for this vio.
  94. */
  95. static inline void initialize_vio(struct vio *vio, struct bio *bio,
  96. unsigned int block_count, enum vio_type vio_type,
  97. enum vio_priority priority, struct vdo *vdo)
  98. {
  99. /* data_vio's may not span multiple blocks */
  100. BUG_ON((vio_type == VIO_TYPE_DATA) && (block_count != 1));
  101. vio->bio = bio;
  102. vio->block_count = block_count;
  103. vio->type = vio_type;
  104. vio->priority = priority;
  105. vdo_initialize_completion(&vio->completion, vdo, VIO_COMPLETION);
  106. }
  107. void vdo_set_bio_properties(struct bio *bio, struct vio *vio, bio_end_io_t callback,
  108. blk_opf_t bi_opf, physical_block_number_t pbn);
  109. int vio_reset_bio(struct vio *vio, char *data, bio_end_io_t callback,
  110. blk_opf_t bi_opf, physical_block_number_t pbn);
  111. int vio_reset_bio_with_size(struct vio *vio, char *data, int size, bio_end_io_t callback,
  112. blk_opf_t bi_opf, physical_block_number_t pbn);
  113. void update_vio_error_stats(struct vio *vio, const char *format, ...)
  114. __printf(2, 3);
  115. /**
  116. * is_data_vio() - Check whether a vio is servicing an external data request.
  117. * @vio: The vio to check.
  118. */
  119. static inline bool is_data_vio(struct vio *vio)
  120. {
  121. return (vio->type == VIO_TYPE_DATA);
  122. }
  123. /**
  124. * get_metadata_priority() - Convert a vio's priority to a work item priority.
  125. * @vio: The vio.
  126. *
  127. * Return: The priority with which to submit the vio's bio.
  128. */
  129. static inline enum vdo_completion_priority get_metadata_priority(struct vio *vio)
  130. {
  131. return ((vio->priority == VIO_PRIORITY_HIGH) ?
  132. BIO_Q_HIGH_PRIORITY :
  133. BIO_Q_METADATA_PRIORITY);
  134. }
  135. /**
  136. * continue_vio() - Enqueue a vio to run its next callback.
  137. * @vio: The vio to continue.
  138. * @result: The result of the current operation.
  139. */
  140. static inline void continue_vio(struct vio *vio, int result)
  141. {
  142. if (unlikely(result != VDO_SUCCESS))
  143. vdo_set_completion_result(&vio->completion, result);
  144. vdo_enqueue_completion(&vio->completion, VDO_WORK_Q_DEFAULT_PRIORITY);
  145. }
  146. void vdo_count_bios(struct atomic_bio_stats *bio_stats, struct bio *bio);
  147. void vdo_count_completed_bios(struct bio *bio);
  148. /**
  149. * continue_vio_after_io() - Continue a vio now that its I/O has returned.
  150. * @vio: The vio to continue.
  151. * @callback: The next operation for this vio.
  152. * @thread: Which thread to run the next operation on.
  153. */
  154. static inline void continue_vio_after_io(struct vio *vio, vdo_action_fn callback,
  155. thread_id_t thread)
  156. {
  157. vdo_count_completed_bios(vio->bio);
  158. vdo_set_completion_callback(&vio->completion, callback, thread);
  159. continue_vio(vio, blk_status_to_errno(vio->bio->bi_status));
  160. }
  161. void vio_record_metadata_io_error(struct vio *vio);
  162. /* A vio_pool is a collection of preallocated vios used to write arbitrary metadata blocks. */
  163. static inline struct pooled_vio *vio_as_pooled_vio(struct vio *vio)
  164. {
  165. return container_of(vio, struct pooled_vio, vio);
  166. }
  167. struct vio_pool;
  168. int __must_check make_vio_pool(struct vdo *vdo, size_t pool_size, size_t block_count,
  169. thread_id_t thread_id, enum vio_type vio_type,
  170. enum vio_priority priority, void *context,
  171. struct vio_pool **pool_ptr);
  172. void free_vio_pool(struct vio_pool *pool);
  173. bool __must_check is_vio_pool_busy(struct vio_pool *pool);
  174. void acquire_vio_from_pool(struct vio_pool *pool, struct vdo_waiter *waiter);
  175. void return_vio_to_pool(struct pooled_vio *vio);
  176. #endif /* VIO_H */