1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright 2023 Red Hat
6 #ifndef VDO_COMPLETION_H
7 #define VDO_COMPLETION_H
9 #include "permassert.h"
11 #include "status-codes.h"
15 * vdo_run_completion() - Run a completion's callback or error handler on the current thread.
17 * Context: This function must be called from the correct callback thread.
19 static inline void vdo_run_completion(struct vdo_completion
*completion
)
21 if ((completion
->result
!= VDO_SUCCESS
) && (completion
->error_handler
!= NULL
)) {
22 completion
->error_handler(completion
);
26 completion
->callback(completion
);
29 void vdo_set_completion_result(struct vdo_completion
*completion
, int result
);
31 void vdo_initialize_completion(struct vdo_completion
*completion
, struct vdo
*vdo
,
32 enum vdo_completion_type type
);
35 * vdo_reset_completion() - Reset a completion to a clean state, while keeping the type, vdo and
38 static inline void vdo_reset_completion(struct vdo_completion
*completion
)
40 completion
->result
= VDO_SUCCESS
;
41 completion
->complete
= false;
44 void vdo_launch_completion_with_priority(struct vdo_completion
*completion
,
45 enum vdo_completion_priority priority
);
48 * vdo_launch_completion() - Launch a completion with default priority.
50 static inline void vdo_launch_completion(struct vdo_completion
*completion
)
52 vdo_launch_completion_with_priority(completion
, VDO_WORK_Q_DEFAULT_PRIORITY
);
56 * vdo_continue_completion() - Continue processing a completion.
57 * @result: The current result (will not mask older errors).
59 * Continue processing a completion by setting the current result and calling
60 * vdo_launch_completion().
62 static inline void vdo_continue_completion(struct vdo_completion
*completion
, int result
)
64 vdo_set_completion_result(completion
, result
);
65 vdo_launch_completion(completion
);
68 void vdo_finish_completion(struct vdo_completion
*completion
);
71 * vdo_fail_completion() - Set the result of a completion if it does not already have an error,
74 static inline void vdo_fail_completion(struct vdo_completion
*completion
, int result
)
76 vdo_set_completion_result(completion
, result
);
77 vdo_finish_completion(completion
);
81 * vdo_assert_completion_type() - Assert that a completion is of the correct type.
83 * Return: VDO_SUCCESS or an error
85 static inline int vdo_assert_completion_type(struct vdo_completion
*completion
,
86 enum vdo_completion_type expected
)
88 return VDO_ASSERT(expected
== completion
->type
,
89 "completion type should be %u, not %u", expected
,
93 static inline void vdo_set_completion_callback(struct vdo_completion
*completion
,
94 vdo_action_fn callback
,
95 thread_id_t callback_thread_id
)
97 completion
->callback
= callback
;
98 completion
->callback_thread_id
= callback_thread_id
;
102 * vdo_launch_completion_callback() - Set the callback for a completion and launch it immediately.
104 static inline void vdo_launch_completion_callback(struct vdo_completion
*completion
,
105 vdo_action_fn callback
,
106 thread_id_t callback_thread_id
)
108 vdo_set_completion_callback(completion
, callback
, callback_thread_id
);
109 vdo_launch_completion(completion
);
113 * vdo_prepare_completion() - Prepare a completion for launch.
115 * Resets the completion, and then sets its callback, error handler, callback thread, and parent.
117 static inline void vdo_prepare_completion(struct vdo_completion
*completion
,
118 vdo_action_fn callback
,
119 vdo_action_fn error_handler
,
120 thread_id_t callback_thread_id
, void *parent
)
122 vdo_reset_completion(completion
);
123 vdo_set_completion_callback(completion
, callback
, callback_thread_id
);
124 completion
->error_handler
= error_handler
;
125 completion
->parent
= parent
;
129 * vdo_prepare_completion_for_requeue() - Prepare a completion for launch ensuring that it will
130 * always be requeued.
132 * Resets the completion, and then sets its callback, error handler, callback thread, and parent.
134 static inline void vdo_prepare_completion_for_requeue(struct vdo_completion
*completion
,
135 vdo_action_fn callback
,
136 vdo_action_fn error_handler
,
137 thread_id_t callback_thread_id
,
140 vdo_prepare_completion(completion
, callback
, error_handler
,
141 callback_thread_id
, parent
);
142 completion
->requeue
= true;
145 void vdo_enqueue_completion(struct vdo_completion
*completion
,
146 enum vdo_completion_priority priority
);
149 bool vdo_requeue_completion_if_needed(struct vdo_completion
*completion
,
150 thread_id_t callback_thread_id
);
152 #endif /* VDO_COMPLETION_H */