admin-state.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "admin-state.h"
  6. #include "logger.h"
  7. #include "memory-alloc.h"
  8. #include "permassert.h"
  9. #include "completion.h"
  10. #include "types.h"
  11. static const struct admin_state_code VDO_CODE_NORMAL_OPERATION = {
  12. .name = "VDO_ADMIN_STATE_NORMAL_OPERATION",
  13. .normal = true,
  14. };
  15. const struct admin_state_code *VDO_ADMIN_STATE_NORMAL_OPERATION = &VDO_CODE_NORMAL_OPERATION;
  16. static const struct admin_state_code VDO_CODE_OPERATING = {
  17. .name = "VDO_ADMIN_STATE_OPERATING",
  18. .normal = true,
  19. .operating = true,
  20. };
  21. const struct admin_state_code *VDO_ADMIN_STATE_OPERATING = &VDO_CODE_OPERATING;
  22. static const struct admin_state_code VDO_CODE_FORMATTING = {
  23. .name = "VDO_ADMIN_STATE_FORMATTING",
  24. .operating = true,
  25. .loading = true,
  26. };
  27. const struct admin_state_code *VDO_ADMIN_STATE_FORMATTING = &VDO_CODE_FORMATTING;
  28. static const struct admin_state_code VDO_CODE_PRE_LOADING = {
  29. .name = "VDO_ADMIN_STATE_PRE_LOADING",
  30. .operating = true,
  31. .loading = true,
  32. };
  33. const struct admin_state_code *VDO_ADMIN_STATE_PRE_LOADING = &VDO_CODE_PRE_LOADING;
  34. static const struct admin_state_code VDO_CODE_PRE_LOADED = {
  35. .name = "VDO_ADMIN_STATE_PRE_LOADED",
  36. };
  37. const struct admin_state_code *VDO_ADMIN_STATE_PRE_LOADED = &VDO_CODE_PRE_LOADED;
  38. static const struct admin_state_code VDO_CODE_LOADING = {
  39. .name = "VDO_ADMIN_STATE_LOADING",
  40. .normal = true,
  41. .operating = true,
  42. .loading = true,
  43. };
  44. const struct admin_state_code *VDO_ADMIN_STATE_LOADING = &VDO_CODE_LOADING;
  45. static const struct admin_state_code VDO_CODE_LOADING_FOR_RECOVERY = {
  46. .name = "VDO_ADMIN_STATE_LOADING_FOR_RECOVERY",
  47. .operating = true,
  48. .loading = true,
  49. };
  50. const struct admin_state_code *VDO_ADMIN_STATE_LOADING_FOR_RECOVERY =
  51. &VDO_CODE_LOADING_FOR_RECOVERY;
  52. static const struct admin_state_code VDO_CODE_LOADING_FOR_REBUILD = {
  53. .name = "VDO_ADMIN_STATE_LOADING_FOR_REBUILD",
  54. .operating = true,
  55. .loading = true,
  56. };
  57. const struct admin_state_code *VDO_ADMIN_STATE_LOADING_FOR_REBUILD = &VDO_CODE_LOADING_FOR_REBUILD;
  58. static const struct admin_state_code VDO_CODE_WAITING_FOR_RECOVERY = {
  59. .name = "VDO_ADMIN_STATE_WAITING_FOR_RECOVERY",
  60. .operating = true,
  61. };
  62. const struct admin_state_code *VDO_ADMIN_STATE_WAITING_FOR_RECOVERY =
  63. &VDO_CODE_WAITING_FOR_RECOVERY;
  64. static const struct admin_state_code VDO_CODE_NEW = {
  65. .name = "VDO_ADMIN_STATE_NEW",
  66. .quiescent = true,
  67. };
  68. const struct admin_state_code *VDO_ADMIN_STATE_NEW = &VDO_CODE_NEW;
  69. static const struct admin_state_code VDO_CODE_INITIALIZED = {
  70. .name = "VDO_ADMIN_STATE_INITIALIZED",
  71. };
  72. const struct admin_state_code *VDO_ADMIN_STATE_INITIALIZED = &VDO_CODE_INITIALIZED;
  73. static const struct admin_state_code VDO_CODE_RECOVERING = {
  74. .name = "VDO_ADMIN_STATE_RECOVERING",
  75. .draining = true,
  76. .operating = true,
  77. };
  78. const struct admin_state_code *VDO_ADMIN_STATE_RECOVERING = &VDO_CODE_RECOVERING;
  79. static const struct admin_state_code VDO_CODE_REBUILDING = {
  80. .name = "VDO_ADMIN_STATE_REBUILDING",
  81. .draining = true,
  82. .operating = true,
  83. };
  84. const struct admin_state_code *VDO_ADMIN_STATE_REBUILDING = &VDO_CODE_REBUILDING;
  85. static const struct admin_state_code VDO_CODE_SAVING = {
  86. .name = "VDO_ADMIN_STATE_SAVING",
  87. .draining = true,
  88. .quiescing = true,
  89. .operating = true,
  90. };
  91. const struct admin_state_code *VDO_ADMIN_STATE_SAVING = &VDO_CODE_SAVING;
  92. static const struct admin_state_code VDO_CODE_SAVED = {
  93. .name = "VDO_ADMIN_STATE_SAVED",
  94. .quiescent = true,
  95. };
  96. const struct admin_state_code *VDO_ADMIN_STATE_SAVED = &VDO_CODE_SAVED;
  97. static const struct admin_state_code VDO_CODE_SCRUBBING = {
  98. .name = "VDO_ADMIN_STATE_SCRUBBING",
  99. .draining = true,
  100. .loading = true,
  101. .operating = true,
  102. };
  103. const struct admin_state_code *VDO_ADMIN_STATE_SCRUBBING = &VDO_CODE_SCRUBBING;
  104. static const struct admin_state_code VDO_CODE_SAVE_FOR_SCRUBBING = {
  105. .name = "VDO_ADMIN_STATE_SAVE_FOR_SCRUBBING",
  106. .draining = true,
  107. .operating = true,
  108. };
  109. const struct admin_state_code *VDO_ADMIN_STATE_SAVE_FOR_SCRUBBING = &VDO_CODE_SAVE_FOR_SCRUBBING;
  110. static const struct admin_state_code VDO_CODE_STOPPING = {
  111. .name = "VDO_ADMIN_STATE_STOPPING",
  112. .draining = true,
  113. .quiescing = true,
  114. .operating = true,
  115. };
  116. const struct admin_state_code *VDO_ADMIN_STATE_STOPPING = &VDO_CODE_STOPPING;
  117. static const struct admin_state_code VDO_CODE_STOPPED = {
  118. .name = "VDO_ADMIN_STATE_STOPPED",
  119. .quiescent = true,
  120. };
  121. const struct admin_state_code *VDO_ADMIN_STATE_STOPPED = &VDO_CODE_STOPPED;
  122. static const struct admin_state_code VDO_CODE_SUSPENDING = {
  123. .name = "VDO_ADMIN_STATE_SUSPENDING",
  124. .draining = true,
  125. .quiescing = true,
  126. .operating = true,
  127. };
  128. const struct admin_state_code *VDO_ADMIN_STATE_SUSPENDING = &VDO_CODE_SUSPENDING;
  129. static const struct admin_state_code VDO_CODE_SUSPENDED = {
  130. .name = "VDO_ADMIN_STATE_SUSPENDED",
  131. .quiescent = true,
  132. };
  133. const struct admin_state_code *VDO_ADMIN_STATE_SUSPENDED = &VDO_CODE_SUSPENDED;
  134. static const struct admin_state_code VDO_CODE_SUSPENDED_OPERATION = {
  135. .name = "VDO_ADMIN_STATE_SUSPENDED_OPERATION",
  136. .operating = true,
  137. };
  138. const struct admin_state_code *VDO_ADMIN_STATE_SUSPENDED_OPERATION = &VDO_CODE_SUSPENDED_OPERATION;
  139. static const struct admin_state_code VDO_CODE_RESUMING = {
  140. .name = "VDO_ADMIN_STATE_RESUMING",
  141. .operating = true,
  142. };
  143. const struct admin_state_code *VDO_ADMIN_STATE_RESUMING = &VDO_CODE_RESUMING;
  144. /**
  145. * get_next_state() - Determine the state which should be set after a given operation completes
  146. * based on the operation and the current state.
  147. * @operation The operation to be started.
  148. *
  149. * Return: The state to set when the operation completes or NULL if the operation can not be
  150. * started in the current state.
  151. */
  152. static const struct admin_state_code *get_next_state(const struct admin_state *state,
  153. const struct admin_state_code *operation)
  154. {
  155. const struct admin_state_code *code = vdo_get_admin_state_code(state);
  156. if (code->operating)
  157. return NULL;
  158. if (operation == VDO_ADMIN_STATE_SAVING)
  159. return (code == VDO_ADMIN_STATE_NORMAL_OPERATION ? VDO_ADMIN_STATE_SAVED : NULL);
  160. if (operation == VDO_ADMIN_STATE_SUSPENDING) {
  161. return (code == VDO_ADMIN_STATE_NORMAL_OPERATION
  162. ? VDO_ADMIN_STATE_SUSPENDED
  163. : NULL);
  164. }
  165. if (operation == VDO_ADMIN_STATE_STOPPING)
  166. return (code == VDO_ADMIN_STATE_NORMAL_OPERATION ? VDO_ADMIN_STATE_STOPPED : NULL);
  167. if (operation == VDO_ADMIN_STATE_PRE_LOADING)
  168. return (code == VDO_ADMIN_STATE_INITIALIZED ? VDO_ADMIN_STATE_PRE_LOADED : NULL);
  169. if (operation == VDO_ADMIN_STATE_SUSPENDED_OPERATION) {
  170. return (((code == VDO_ADMIN_STATE_SUSPENDED) ||
  171. (code == VDO_ADMIN_STATE_SAVED)) ? code : NULL);
  172. }
  173. return VDO_ADMIN_STATE_NORMAL_OPERATION;
  174. }
  175. /**
  176. * vdo_finish_operation() - Finish the current operation.
  177. *
  178. * Will notify the operation waiter if there is one. This method should be used for operations
  179. * started with vdo_start_operation(). For operations which were started with vdo_start_draining(),
  180. * use vdo_finish_draining() instead.
  181. *
  182. * Return: true if there was an operation to finish.
  183. */
  184. bool vdo_finish_operation(struct admin_state *state, int result)
  185. {
  186. if (!vdo_get_admin_state_code(state)->operating)
  187. return false;
  188. state->complete = state->starting;
  189. if (state->waiter != NULL)
  190. vdo_set_completion_result(state->waiter, result);
  191. if (!state->starting) {
  192. vdo_set_admin_state_code(state, state->next_state);
  193. if (state->waiter != NULL)
  194. vdo_launch_completion(vdo_forget(state->waiter));
  195. }
  196. return true;
  197. }
  198. /**
  199. * begin_operation() - Begin an operation if it may be started given the current state.
  200. * @waiter A completion to notify when the operation is complete; may be NULL.
  201. * @initiator The vdo_admin_initiator_fn to call if the operation may begin; may be NULL.
  202. *
  203. * Return: VDO_SUCCESS or an error.
  204. */
  205. static int __must_check begin_operation(struct admin_state *state,
  206. const struct admin_state_code *operation,
  207. struct vdo_completion *waiter,
  208. vdo_admin_initiator_fn initiator)
  209. {
  210. int result;
  211. const struct admin_state_code *next_state = get_next_state(state, operation);
  212. if (next_state == NULL) {
  213. result = vdo_log_error_strerror(VDO_INVALID_ADMIN_STATE,
  214. "Can't start %s from %s",
  215. operation->name,
  216. vdo_get_admin_state_code(state)->name);
  217. } else if (state->waiter != NULL) {
  218. result = vdo_log_error_strerror(VDO_COMPONENT_BUSY,
  219. "Can't start %s with extant waiter",
  220. operation->name);
  221. } else {
  222. state->waiter = waiter;
  223. state->next_state = next_state;
  224. vdo_set_admin_state_code(state, operation);
  225. if (initiator != NULL) {
  226. state->starting = true;
  227. initiator(state);
  228. state->starting = false;
  229. if (state->complete)
  230. vdo_finish_operation(state, VDO_SUCCESS);
  231. }
  232. return VDO_SUCCESS;
  233. }
  234. if (waiter != NULL)
  235. vdo_continue_completion(waiter, result);
  236. return result;
  237. }
  238. /**
  239. * start_operation() - Start an operation if it may be started given the current state.
  240. * @waiter A completion to notify when the operation is complete.
  241. * @initiator The vdo_admin_initiator_fn to call if the operation may begin; may be NULL.
  242. *
  243. * Return: true if the operation was started.
  244. */
  245. static inline bool __must_check start_operation(struct admin_state *state,
  246. const struct admin_state_code *operation,
  247. struct vdo_completion *waiter,
  248. vdo_admin_initiator_fn initiator)
  249. {
  250. return (begin_operation(state, operation, waiter, initiator) == VDO_SUCCESS);
  251. }
  252. /**
  253. * check_code() - Check the result of a state validation.
  254. * @valid true if the code is of an appropriate type.
  255. * @code The code which failed to be of the correct type.
  256. * @what What the code failed to be, for logging.
  257. * @waiter The completion to notify of the error; may be NULL.
  258. *
  259. * If the result failed, log an invalid state error and, if there is a waiter, notify it.
  260. *
  261. * Return: The result of the check.
  262. */
  263. static bool check_code(bool valid, const struct admin_state_code *code, const char *what,
  264. struct vdo_completion *waiter)
  265. {
  266. int result;
  267. if (valid)
  268. return true;
  269. result = vdo_log_error_strerror(VDO_INVALID_ADMIN_STATE,
  270. "%s is not a %s", code->name, what);
  271. if (waiter != NULL)
  272. vdo_continue_completion(waiter, result);
  273. return false;
  274. }
  275. /**
  276. * assert_vdo_drain_operation() - Check that an operation is a drain.
  277. * @waiter The completion to finish with an error if the operation is not a drain.
  278. *
  279. * Return: true if the specified operation is a drain.
  280. */
  281. static bool __must_check assert_vdo_drain_operation(const struct admin_state_code *operation,
  282. struct vdo_completion *waiter)
  283. {
  284. return check_code(operation->draining, operation, "drain operation", waiter);
  285. }
  286. /**
  287. * vdo_start_draining() - Initiate a drain operation if the current state permits it.
  288. * @operation The type of drain to initiate.
  289. * @waiter The completion to notify when the drain is complete.
  290. * @initiator The vdo_admin_initiator_fn to call if the operation may begin; may be NULL.
  291. *
  292. * Return: true if the drain was initiated, if not the waiter will be notified.
  293. */
  294. bool vdo_start_draining(struct admin_state *state,
  295. const struct admin_state_code *operation,
  296. struct vdo_completion *waiter, vdo_admin_initiator_fn initiator)
  297. {
  298. const struct admin_state_code *code = vdo_get_admin_state_code(state);
  299. if (!assert_vdo_drain_operation(operation, waiter))
  300. return false;
  301. if (code->quiescent) {
  302. vdo_launch_completion(waiter);
  303. return false;
  304. }
  305. if (!code->normal) {
  306. vdo_log_error_strerror(VDO_INVALID_ADMIN_STATE, "can't start %s from %s",
  307. operation->name, code->name);
  308. vdo_continue_completion(waiter, VDO_INVALID_ADMIN_STATE);
  309. return false;
  310. }
  311. return start_operation(state, operation, waiter, initiator);
  312. }
  313. /**
  314. * vdo_finish_draining() - Finish a drain operation if one was in progress.
  315. *
  316. * Return: true if the state was draining; will notify the waiter if so.
  317. */
  318. bool vdo_finish_draining(struct admin_state *state)
  319. {
  320. return vdo_finish_draining_with_result(state, VDO_SUCCESS);
  321. }
  322. /**
  323. * vdo_finish_draining_with_result() - Finish a drain operation with a status code.
  324. *
  325. * Return: true if the state was draining; will notify the waiter if so.
  326. */
  327. bool vdo_finish_draining_with_result(struct admin_state *state, int result)
  328. {
  329. return (vdo_is_state_draining(state) && vdo_finish_operation(state, result));
  330. }
  331. /**
  332. * vdo_assert_load_operation() - Check that an operation is a load.
  333. * @waiter The completion to finish with an error if the operation is not a load.
  334. *
  335. * Return: true if the specified operation is a load.
  336. */
  337. bool vdo_assert_load_operation(const struct admin_state_code *operation,
  338. struct vdo_completion *waiter)
  339. {
  340. return check_code(operation->loading, operation, "load operation", waiter);
  341. }
  342. /**
  343. * vdo_start_loading() - Initiate a load operation if the current state permits it.
  344. * @operation The type of load to initiate.
  345. * @waiter The completion to notify when the load is complete (may be NULL).
  346. * @initiator The vdo_admin_initiator_fn to call if the operation may begin; may be NULL.
  347. *
  348. * Return: true if the load was initiated, if not the waiter will be notified.
  349. */
  350. bool vdo_start_loading(struct admin_state *state,
  351. const struct admin_state_code *operation,
  352. struct vdo_completion *waiter, vdo_admin_initiator_fn initiator)
  353. {
  354. return (vdo_assert_load_operation(operation, waiter) &&
  355. start_operation(state, operation, waiter, initiator));
  356. }
  357. /**
  358. * vdo_finish_loading() - Finish a load operation if one was in progress.
  359. *
  360. * Return: true if the state was loading; will notify the waiter if so.
  361. */
  362. bool vdo_finish_loading(struct admin_state *state)
  363. {
  364. return vdo_finish_loading_with_result(state, VDO_SUCCESS);
  365. }
  366. /**
  367. * vdo_finish_loading_with_result() - Finish a load operation with a status code.
  368. * @result The result of the load operation.
  369. *
  370. * Return: true if the state was loading; will notify the waiter if so.
  371. */
  372. bool vdo_finish_loading_with_result(struct admin_state *state, int result)
  373. {
  374. return (vdo_is_state_loading(state) && vdo_finish_operation(state, result));
  375. }
  376. /**
  377. * assert_vdo_resume_operation() - Check whether an admin_state_code is a resume operation.
  378. * @waiter The completion to notify if the operation is not a resume operation; may be NULL.
  379. *
  380. * Return: true if the code is a resume operation.
  381. */
  382. static bool __must_check assert_vdo_resume_operation(const struct admin_state_code *operation,
  383. struct vdo_completion *waiter)
  384. {
  385. return check_code(operation == VDO_ADMIN_STATE_RESUMING, operation,
  386. "resume operation", waiter);
  387. }
  388. /**
  389. * vdo_start_resuming() - Initiate a resume operation if the current state permits it.
  390. * @operation The type of resume to start.
  391. * @waiter The completion to notify when the resume is complete (may be NULL).
  392. * @initiator The vdo_admin_initiator_fn to call if the operation may begin; may be NULL.
  393. *
  394. * Return: true if the resume was initiated, if not the waiter will be notified.
  395. */
  396. bool vdo_start_resuming(struct admin_state *state,
  397. const struct admin_state_code *operation,
  398. struct vdo_completion *waiter, vdo_admin_initiator_fn initiator)
  399. {
  400. return (assert_vdo_resume_operation(operation, waiter) &&
  401. start_operation(state, operation, waiter, initiator));
  402. }
  403. /**
  404. * vdo_finish_resuming() - Finish a resume operation if one was in progress.
  405. *
  406. * Return: true if the state was resuming; will notify the waiter if so.
  407. */
  408. bool vdo_finish_resuming(struct admin_state *state)
  409. {
  410. return vdo_finish_resuming_with_result(state, VDO_SUCCESS);
  411. }
  412. /**
  413. * vdo_finish_resuming_with_result() - Finish a resume operation with a status code.
  414. * @result The result of the resume operation.
  415. *
  416. * Return: true if the state was resuming; will notify the waiter if so.
  417. */
  418. bool vdo_finish_resuming_with_result(struct admin_state *state, int result)
  419. {
  420. return (vdo_is_state_resuming(state) && vdo_finish_operation(state, result));
  421. }
  422. /**
  423. * vdo_resume_if_quiescent() - Change the state to normal operation if the current state is
  424. * quiescent.
  425. *
  426. * Return: VDO_SUCCESS if the state resumed, VDO_INVALID_ADMIN_STATE otherwise.
  427. */
  428. int vdo_resume_if_quiescent(struct admin_state *state)
  429. {
  430. if (!vdo_is_state_quiescent(state))
  431. return VDO_INVALID_ADMIN_STATE;
  432. vdo_set_admin_state_code(state, VDO_ADMIN_STATE_NORMAL_OPERATION);
  433. return VDO_SUCCESS;
  434. }
  435. /**
  436. * vdo_start_operation() - Attempt to start an operation.
  437. *
  438. * Return: VDO_SUCCESS if the operation was started, VDO_INVALID_ADMIN_STATE if not
  439. */
  440. int vdo_start_operation(struct admin_state *state,
  441. const struct admin_state_code *operation)
  442. {
  443. return vdo_start_operation_with_waiter(state, operation, NULL, NULL);
  444. }
  445. /**
  446. * vdo_start_operation_with_waiter() - Attempt to start an operation.
  447. * @waiter the completion to notify when the operation completes or fails to start; may be NULL.
  448. * @initiator The vdo_admin_initiator_fn to call if the operation may begin; may be NULL.
  449. *
  450. * Return: VDO_SUCCESS if the operation was started, VDO_INVALID_ADMIN_STATE if not
  451. */
  452. int vdo_start_operation_with_waiter(struct admin_state *state,
  453. const struct admin_state_code *operation,
  454. struct vdo_completion *waiter,
  455. vdo_admin_initiator_fn initiator)
  456. {
  457. return (check_code(operation->operating, operation, "operation", waiter) ?
  458. begin_operation(state, operation, waiter, initiator) :
  459. VDO_INVALID_ADMIN_STATE);
  460. }