Debian Codegraph @

src/plugins/nvme/nvme-error.c

raw
1 /*
2 * Copyright (C) 2014-2021 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Tomas Bzatek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <sys/ioctl.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <malloc.h>
29
30 #include <libnvme.h>
31
32 #include <blockdev/utils.h>
33 #include <check_deps.h>
34 #include "nvme.h"
35 #include "nvme-private.h"
36
37
38 /**
39 * bd_nvme_error_quark: (skip)
40 */
41 GQuark bd_nvme_error_quark (void)
42 {
43 return g_quark_from_static_string ("g-bd-nvme-error-quark");
44 }
45
46 void _nvme_status_to_error (gint status, gboolean fabrics, GError **error)
47 {
48 gint code;
49
50 if (error == NULL)
51 return;
52 if (status == 0) {
53 g_clear_error (error);
54 return;
55 }
56
57 if (status < 0) {
58 /* generic errno errors */
59 switch (errno) {
60 case EWOULDBLOCK:
61 code = BD_NVME_ERROR_BUSY;
62 break;
63 default:
64 code = BD_NVME_ERROR_FAILED;
65 }
66 g_set_error_literal (error, BD_NVME_ERROR, code,
67 strerror_l (errno, _C_LOCALE));
68 return;
69 } else {
70 /* NVMe status codes */
71 switch (nvme_status_code_type (status)) {
72 case NVME_SCT_GENERIC:
73 code = BD_NVME_ERROR_SC_GENERIC;
74 break;
75 case NVME_SCT_CMD_SPECIFIC:
76 code = BD_NVME_ERROR_SC_CMD_SPECIFIC;
77 break;
78 case NVME_SCT_MEDIA:
79 code = BD_NVME_ERROR_SC_MEDIA;
80 break;
81 case NVME_SCT_PATH:
82 code = BD_NVME_ERROR_SC_PATH;
83 break;
84 case NVME_SCT_VS:
85 code = BD_NVME_ERROR_SC_VENDOR_SPECIFIC;
86 break;
87 default:
88 code = BD_NVME_ERROR_SC_GENERIC;
89 }
90 g_set_error_literal (error, BD_NVME_ERROR, code, nvme_status_to_string (status, fabrics));
91 return;
92 }
93 g_warn_if_reached ();
94 }
95
96 void _nvme_fabrics_errno_to_gerror (int result, int _errno, GError **error)
97 {
98 gint code;
99
100 if (error == NULL)
101 return;
102 if (result == 0) {
103 g_clear_error (error);
104 return;
105 }
106
107 if (_errno >= ENVME_CONNECT_RESOLVE) {
108 switch (_errno) {
109 case ENVME_CONNECT_ADDRFAM:
110 case ENVME_CONNECT_TRADDR:
111 case ENVME_CONNECT_TARG:
112 case ENVME_CONNECT_AARG:
113 case ENVME_CONNECT_INVAL_TR:
114 code = BD_NVME_ERROR_INVALID_ARGUMENT;
115 break;
116 case ENVME_CONNECT_RESOLVE:
117 case ENVME_CONNECT_OPEN:
118 case ENVME_CONNECT_WRITE:
119 case ENVME_CONNECT_READ:
120 case ENVME_CONNECT_PARSE:
121 case ENVME_CONNECT_LOOKUP_SUBSYS_NAME:
122 case ENVME_CONNECT_LOOKUP_SUBSYS:
123 code = BD_NVME_ERROR_CONNECT;
124 break;
125 case ENVME_CONNECT_ALREADY:
126 code = BD_NVME_ERROR_CONNECT_ALREADY;
127 break;
128 case ENVME_CONNECT_INVAL:
129 code = BD_NVME_ERROR_CONNECT_INVALID;
130 break;
131 case ENVME_CONNECT_ADDRINUSE:
132 code = BD_NVME_ERROR_CONNECT_ADDRINUSE;
133 break;
134 case ENVME_CONNECT_NODEV:
135 code = BD_NVME_ERROR_CONNECT_NODEV;
136 break;
137 case ENVME_CONNECT_OPNOTSUPP:
138 code = BD_NVME_ERROR_CONNECT_OPNOTSUPP;
139 break;
140 case ENVME_CONNECT_CONNREFUSED:
141 code = BD_NVME_ERROR_CONNECT_REFUSED;
142 break;
143 default:
144 code = BD_NVME_ERROR_CONNECT;
145 }
146 g_set_error_literal (error, BD_NVME_ERROR, code, nvme_errno_to_string (_errno));
147 return;
148 } else {
149 switch (errno) {
150 case EWOULDBLOCK:
151 code = BD_NVME_ERROR_BUSY;
152 break;
153 default:
154 code = BD_NVME_ERROR_FAILED;
155 }
156 g_set_error_literal (error, BD_NVME_ERROR, code, strerror_l (errno, _C_LOCALE));
157 return;
158 }
159 g_warn_if_reached ();
160 }