completion.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #ifndef VDO_COMPLETION_H
  6. #define VDO_COMPLETION_H
  7. #include "permassert.h"
  8. #include "status-codes.h"
  9. #include "types.h"
  10. /**
  11. * vdo_run_completion() - Run a completion's callback or error handler on the current thread.
  12. *
  13. * Context: This function must be called from the correct callback thread.
  14. */
  15. static inline void vdo_run_completion(struct vdo_completion *completion)
  16. {
  17. if ((completion->result != VDO_SUCCESS) && (completion->error_handler != NULL)) {
  18. completion->error_handler(completion);
  19. return;
  20. }
  21. completion->callback(completion);
  22. }
  23. void vdo_set_completion_result(struct vdo_completion *completion, int result);
  24. void vdo_initialize_completion(struct vdo_completion *completion, struct vdo *vdo,
  25. enum vdo_completion_type type);
  26. /**
  27. * vdo_reset_completion() - Reset a completion to a clean state, while keeping the type, vdo and
  28. * parent information.
  29. */
  30. static inline void vdo_reset_completion(struct vdo_completion *completion)
  31. {
  32. completion->result = VDO_SUCCESS;
  33. completion->complete = false;
  34. }
  35. void vdo_launch_completion_with_priority(struct vdo_completion *completion,
  36. enum vdo_completion_priority priority);
  37. /**
  38. * vdo_launch_completion() - Launch a completion with default priority.
  39. */
  40. static inline void vdo_launch_completion(struct vdo_completion *completion)
  41. {
  42. vdo_launch_completion_with_priority(completion, VDO_WORK_Q_DEFAULT_PRIORITY);
  43. }
  44. /**
  45. * vdo_continue_completion() - Continue processing a completion.
  46. * @result: The current result (will not mask older errors).
  47. *
  48. * Continue processing a completion by setting the current result and calling
  49. * vdo_launch_completion().
  50. */
  51. static inline void vdo_continue_completion(struct vdo_completion *completion, int result)
  52. {
  53. vdo_set_completion_result(completion, result);
  54. vdo_launch_completion(completion);
  55. }
  56. void vdo_finish_completion(struct vdo_completion *completion);
  57. /**
  58. * vdo_fail_completion() - Set the result of a completion if it does not already have an error,
  59. * then finish it.
  60. */
  61. static inline void vdo_fail_completion(struct vdo_completion *completion, int result)
  62. {
  63. vdo_set_completion_result(completion, result);
  64. vdo_finish_completion(completion);
  65. }
  66. /**
  67. * vdo_assert_completion_type() - Assert that a completion is of the correct type.
  68. *
  69. * Return: VDO_SUCCESS or an error
  70. */
  71. static inline int vdo_assert_completion_type(struct vdo_completion *completion,
  72. enum vdo_completion_type expected)
  73. {
  74. return VDO_ASSERT(expected == completion->type,
  75. "completion type should be %u, not %u", expected,
  76. completion->type);
  77. }
  78. static inline void vdo_set_completion_callback(struct vdo_completion *completion,
  79. vdo_action_fn callback,
  80. thread_id_t callback_thread_id)
  81. {
  82. completion->callback = callback;
  83. completion->callback_thread_id = callback_thread_id;
  84. }
  85. /**
  86. * vdo_launch_completion_callback() - Set the callback for a completion and launch it immediately.
  87. */
  88. static inline void vdo_launch_completion_callback(struct vdo_completion *completion,
  89. vdo_action_fn callback,
  90. thread_id_t callback_thread_id)
  91. {
  92. vdo_set_completion_callback(completion, callback, callback_thread_id);
  93. vdo_launch_completion(completion);
  94. }
  95. /**
  96. * vdo_prepare_completion() - Prepare a completion for launch.
  97. *
  98. * Resets the completion, and then sets its callback, error handler, callback thread, and parent.
  99. */
  100. static inline void vdo_prepare_completion(struct vdo_completion *completion,
  101. vdo_action_fn callback,
  102. vdo_action_fn error_handler,
  103. thread_id_t callback_thread_id, void *parent)
  104. {
  105. vdo_reset_completion(completion);
  106. vdo_set_completion_callback(completion, callback, callback_thread_id);
  107. completion->error_handler = error_handler;
  108. completion->parent = parent;
  109. }
  110. /**
  111. * vdo_prepare_completion_for_requeue() - Prepare a completion for launch ensuring that it will
  112. * always be requeued.
  113. *
  114. * Resets the completion, and then sets its callback, error handler, callback thread, and parent.
  115. */
  116. static inline void vdo_prepare_completion_for_requeue(struct vdo_completion *completion,
  117. vdo_action_fn callback,
  118. vdo_action_fn error_handler,
  119. thread_id_t callback_thread_id,
  120. void *parent)
  121. {
  122. vdo_prepare_completion(completion, callback, error_handler,
  123. callback_thread_id, parent);
  124. completion->requeue = true;
  125. }
  126. void vdo_enqueue_completion(struct vdo_completion *completion,
  127. enum vdo_completion_priority priority);
  128. bool vdo_requeue_completion_if_needed(struct vdo_completion *completion,
  129. thread_id_t callback_thread_id);
  130. #endif /* VDO_COMPLETION_H */