Line data Source code
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 <tbzatek@redhat.com>
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 0 : GQuark bd_nvme_error_quark (void)
42 : {
43 0 : return g_quark_from_static_string ("g-bd-nvme-error-quark");
44 : }
45 :
46 31 : void _nvme_status_to_error (gint status, gboolean fabrics, GError **error)
47 : {
48 : gint code;
49 :
50 31 : if (error == NULL)
51 0 : return;
52 31 : if (status == 0) {
53 0 : g_clear_error (error);
54 0 : return;
55 : }
56 :
57 31 : if (status < 0) {
58 : /* generic errno errors */
59 10 : switch (errno) {
60 0 : case EWOULDBLOCK:
61 0 : code = BD_NVME_ERROR_BUSY;
62 0 : break;
63 10 : default:
64 10 : code = BD_NVME_ERROR_FAILED;
65 : }
66 10 : g_set_error_literal (error, BD_NVME_ERROR, code,
67 10 : strerror_l (errno, _C_LOCALE));
68 10 : return;
69 : } else {
70 : /* NVMe status codes */
71 21 : switch (nvme_status_code_type (status)) {
72 21 : case NVME_SCT_GENERIC:
73 21 : code = BD_NVME_ERROR_SC_GENERIC;
74 21 : break;
75 0 : case NVME_SCT_CMD_SPECIFIC:
76 0 : code = BD_NVME_ERROR_SC_CMD_SPECIFIC;
77 0 : break;
78 0 : case NVME_SCT_MEDIA:
79 0 : code = BD_NVME_ERROR_SC_MEDIA;
80 0 : break;
81 0 : case NVME_SCT_PATH:
82 0 : code = BD_NVME_ERROR_SC_PATH;
83 0 : break;
84 0 : case NVME_SCT_VS:
85 0 : code = BD_NVME_ERROR_SC_VENDOR_SPECIFIC;
86 0 : break;
87 0 : default:
88 0 : code = BD_NVME_ERROR_SC_GENERIC;
89 : }
90 21 : g_set_error_literal (error, BD_NVME_ERROR, code, nvme_status_to_string (status, fabrics));
91 21 : return;
92 : }
93 : g_warn_if_reached ();
94 : }
95 :
96 6 : void _nvme_fabrics_errno_to_gerror (int result, int _errno, GError **error)
97 : {
98 : gint code;
99 :
100 6 : if (error == NULL)
101 0 : return;
102 6 : if (result == 0) {
103 0 : g_clear_error (error);
104 0 : return;
105 : }
106 :
107 6 : if (_errno >= ENVME_CONNECT_RESOLVE) {
108 6 : switch (_errno) {
109 0 : 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 0 : code = BD_NVME_ERROR_INVALID_ARGUMENT;
115 0 : break;
116 6 : 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 6 : code = BD_NVME_ERROR_CONNECT;
124 6 : break;
125 0 : case ENVME_CONNECT_ALREADY:
126 0 : code = BD_NVME_ERROR_CONNECT_ALREADY;
127 0 : break;
128 0 : case ENVME_CONNECT_INVAL:
129 0 : code = BD_NVME_ERROR_CONNECT_INVALID;
130 0 : break;
131 0 : case ENVME_CONNECT_ADDRINUSE:
132 0 : code = BD_NVME_ERROR_CONNECT_ADDRINUSE;
133 0 : break;
134 0 : case ENVME_CONNECT_NODEV:
135 0 : code = BD_NVME_ERROR_CONNECT_NODEV;
136 0 : break;
137 0 : case ENVME_CONNECT_OPNOTSUPP:
138 0 : code = BD_NVME_ERROR_CONNECT_OPNOTSUPP;
139 0 : break;
140 0 : case ENVME_CONNECT_CONNREFUSED:
141 0 : code = BD_NVME_ERROR_CONNECT_REFUSED;
142 0 : break;
143 0 : default:
144 0 : code = BD_NVME_ERROR_CONNECT;
145 : }
146 6 : g_set_error_literal (error, BD_NVME_ERROR, code, nvme_errno_to_string (_errno));
147 6 : return;
148 : } else {
149 0 : switch (errno) {
150 0 : case EWOULDBLOCK:
151 0 : code = BD_NVME_ERROR_BUSY;
152 0 : break;
153 0 : default:
154 0 : code = BD_NVME_ERROR_FAILED;
155 : }
156 0 : g_set_error_literal (error, BD_NVME_ERROR, code, strerror_l (errno, _C_LOCALE));
157 0 : return;
158 : }
159 : g_warn_if_reached ();
160 : }
|