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