status-codes.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "status-codes.h"
  6. #include "errors.h"
  7. #include "logger.h"
  8. #include "permassert.h"
  9. #include "thread-utils.h"
  10. const struct error_info vdo_status_list[] = {
  11. { "VDO_NOT_IMPLEMENTED", "Not implemented" },
  12. { "VDO_OUT_OF_RANGE", "Out of range" },
  13. { "VDO_REF_COUNT_INVALID", "Reference count would become invalid" },
  14. { "VDO_NO_SPACE", "Out of space" },
  15. { "VDO_BAD_CONFIGURATION", "Bad configuration option" },
  16. { "VDO_COMPONENT_BUSY", "Prior operation still in progress" },
  17. { "VDO_BAD_PAGE", "Corrupt or incorrect page" },
  18. { "VDO_UNSUPPORTED_VERSION", "Unsupported component version" },
  19. { "VDO_INCORRECT_COMPONENT", "Component id mismatch in decoder" },
  20. { "VDO_PARAMETER_MISMATCH", "Parameters have conflicting values" },
  21. { "VDO_UNKNOWN_PARTITION", "No partition exists with a given id" },
  22. { "VDO_PARTITION_EXISTS", "A partition already exists with a given id" },
  23. { "VDO_INCREMENT_TOO_SMALL", "Physical block growth of too few blocks" },
  24. { "VDO_CHECKSUM_MISMATCH", "Incorrect checksum" },
  25. { "VDO_LOCK_ERROR", "A lock is held incorrectly" },
  26. { "VDO_READ_ONLY", "The device is in read-only mode" },
  27. { "VDO_SHUTTING_DOWN", "The device is shutting down" },
  28. { "VDO_CORRUPT_JOURNAL", "Recovery journal corrupted" },
  29. { "VDO_TOO_MANY_SLABS", "Exceeds maximum number of slabs supported" },
  30. { "VDO_INVALID_FRAGMENT", "Compressed block fragment is invalid" },
  31. { "VDO_RETRY_AFTER_REBUILD", "Retry operation after rebuilding finishes" },
  32. { "VDO_BAD_MAPPING", "Invalid page mapping" },
  33. { "VDO_BIO_CREATION_FAILED", "Bio creation failed" },
  34. { "VDO_BAD_MAGIC", "Bad magic number" },
  35. { "VDO_BAD_NONCE", "Bad nonce" },
  36. { "VDO_JOURNAL_OVERFLOW", "Journal sequence number overflow" },
  37. { "VDO_INVALID_ADMIN_STATE", "Invalid operation for current state" },
  38. };
  39. /**
  40. * vdo_register_status_codes() - Register the VDO status codes.
  41. * Return: A success or error code.
  42. */
  43. int vdo_register_status_codes(void)
  44. {
  45. int result;
  46. BUILD_BUG_ON((VDO_STATUS_CODE_LAST - VDO_STATUS_CODE_BASE) !=
  47. ARRAY_SIZE(vdo_status_list));
  48. result = uds_register_error_block("VDO Status", VDO_STATUS_CODE_BASE,
  49. VDO_STATUS_CODE_BLOCK_END, vdo_status_list,
  50. sizeof(vdo_status_list));
  51. return (result == UDS_SUCCESS) ? VDO_SUCCESS : result;
  52. }
  53. /**
  54. * vdo_status_to_errno() - Given an error code, return a value we can return to the OS.
  55. * @error: The error code to convert.
  56. *
  57. * The input error code may be a system-generated value (such as -EIO), an errno macro used in our
  58. * code (such as EIO), or a UDS or VDO status code; the result must be something the rest of the OS
  59. * can consume (negative errno values such as -EIO, in the case of the kernel).
  60. *
  61. * Return: A system error code value.
  62. */
  63. int vdo_status_to_errno(int error)
  64. {
  65. char error_name[VDO_MAX_ERROR_NAME_SIZE];
  66. char error_message[VDO_MAX_ERROR_MESSAGE_SIZE];
  67. /* 0 is success, negative a system error code */
  68. if (likely(error <= 0))
  69. return error;
  70. if (error < 1024)
  71. return -error;
  72. /* VDO or UDS error */
  73. switch (error) {
  74. case VDO_NO_SPACE:
  75. return -ENOSPC;
  76. case VDO_READ_ONLY:
  77. return -EIO;
  78. default:
  79. vdo_log_info("%s: mapping internal status code %d (%s: %s) to EIO",
  80. __func__, error,
  81. uds_string_error_name(error, error_name, sizeof(error_name)),
  82. uds_string_error(error, error_message, sizeof(error_message)));
  83. return -EIO;
  84. }
  85. }