forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
227 lines (206 loc) · 7.08 KB
/
util.h
File metadata and controls
227 lines (206 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of libnvme.
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
*
* Authors: Keith Busch <[email protected]>
* Chaitanya Kulkarni <[email protected]>
*/
#pragma once
#include <nvme/types.h>
/**
* DOC: util.h
*
* libnvme utility functions
*/
/**
* enum nvme_connect_err - nvme connect error codes
* @ENVME_CONNECT_RESOLVE: failed to resolve host
* @ENVME_CONNECT_ADDRFAM: unrecognized address family
* @ENVME_CONNECT_TRADDR: failed to get traddr
* @ENVME_CONNECT_TARG: need a transport (-t) argument
* @ENVME_CONNECT_AARG: need a address (-a) argument
* @ENVME_CONNECT_OPEN: failed to open nvme-fabrics device
* @ENVME_CONNECT_WRITE: failed to write to nvme-fabrics device
* @ENVME_CONNECT_READ: failed to read from nvme-fabrics device
* @ENVME_CONNECT_PARSE: failed to parse ctrl info
* @ENVME_CONNECT_INVAL_TR: invalid transport type
* @ENVME_CONNECT_LOOKUP_SUBSYS_NAME: failed to lookup subsystem name
* @ENVME_CONNECT_LOOKUP_SUBSYS: failed to lookup subsystem
* @ENVME_CONNECT_ALREADY: the connect attempt failed, already connected
* @ENVME_CONNECT_INVAL: invalid arguments/configuration
* @ENVME_CONNECT_ADDRINUSE: hostnqn already in use
* @ENVME_CONNECT_NODEV: invalid interface
* @ENVME_CONNECT_OPNOTSUPP: not supported
* @ENVME_CONNECT_CONNREFUSED: connection refused
* @ENVME_CONNECT_ADDRNOTAVAIL: cannot assign requested address
* @ENVME_CONNECT_IGNORED: connect attempt is ignored due to configuration
* @ENVME_CONNECT_NOKEY: the TLS key is missing
*/
enum nvme_connect_err {
ENVME_CONNECT_RESOLVE = 1000,
ENVME_CONNECT_ADDRFAM,
ENVME_CONNECT_TRADDR,
ENVME_CONNECT_TARG,
ENVME_CONNECT_AARG,
ENVME_CONNECT_OPEN,
ENVME_CONNECT_WRITE,
ENVME_CONNECT_READ,
ENVME_CONNECT_PARSE,
ENVME_CONNECT_INVAL_TR,
ENVME_CONNECT_LOOKUP_SUBSYS_NAME,
ENVME_CONNECT_LOOKUP_SUBSYS,
ENVME_CONNECT_ALREADY,
ENVME_CONNECT_INVAL,
ENVME_CONNECT_ADDRINUSE,
ENVME_CONNECT_NODEV,
ENVME_CONNECT_OPNOTSUPP,
ENVME_CONNECT_CONNREFUSED,
ENVME_CONNECT_ADDRNOTAVAIL,
ENVME_CONNECT_IGNORED,
ENVME_CONNECT_NOKEY,
};
/**
* nvme_status_to_errno() - Converts nvme return status to errno
* @status: Return status from an nvme passthrough command
* @fabrics: Set to true if &status is to a fabrics target.
*
* Return: An errno representing the nvme status if it is an nvme status field,
* or unchanged status is < 0 since errno is already set.
*/
__u8 nvme_status_to_errno(int status, bool fabrics);
/**
* nvme_status_to_string() - Returns string describing nvme return status.
* @status: Return status from an nvme passthrough command
* @fabrics: Set to true if &status is to a fabrics target.
*
* Return: String representation of the nvme status if it is an nvme status field,
* or a standard errno string if status is < 0.
*/
const char *nvme_status_to_string(int status, bool fabrics);
/**
* nvme_sanitize_ns_status_to_string() - Returns sanitize ns status string.
* @sc: Return status code from an sanitize ns command
*
* Return: The sanitize ns status string if it is a specific status code.
*/
static inline const char *
nvme_sanitize_ns_status_to_string(__u16 sc)
{
switch (sc) {
case NVME_SC_EXCEEDS_MAX_NS_SANITIZE:
return "Req Exceeds Max NS Sanitize Operations In Progress";
default:
break;
}
return NULL;
};
/**
* nvme_opcode_status_to_string() - Returns nvme opcode status string.
* @status: Return status from an nvme passthrough command
* @admin: Set to true if an admin command
* @opcode: Opcode from an nvme passthrough command
*
* Return: The nvme opcode status string if it is an nvme status field,
* or a standard errno string if status is < 0.
*/
static inline const char *
nvme_opcode_status_to_string(int status, bool admin, __u8 opcode)
{
__u16 sct = nvme_status_code_type(status);
__u16 sc = nvme_status_code(status);
const char *s = NULL;
if (status >= 0 && sct == NVME_SCT_CMD_SPECIFIC) {
if (admin && opcode == nvme_admin_sanitize_ns)
s = nvme_sanitize_ns_status_to_string(sc);
}
if (s)
return s;
return nvme_status_to_string(status, false);
}
/**
* nvme_errno_to_string() - Returns string describing nvme connect failures
* @err: Returned error code from nvme_add_ctrl()
*
* Return: String representation of the nvme connect error codes
*/
const char *nvme_errno_to_string(int err);
/**
* nvme_strerror() - Returns string describing nvme errors and errno
* @err: Returned error codes from all libnvme functions
*
* Return: String representation of either the nvme connect error codes
* (positive values) or errno string (negative values)
*/
const char *nvme_strerror(int err);
/**
* nvmf_exat_ptr_next - Increment @p to the next element in the array.
* @p: Pointer to an element of an array of "struct nvmf_ext_attr".
*
* Extended attributes are saved to an array of "struct nvmf_ext_attr"
* where each element of the array is of variable size. In order to
* move to the next element in the array one must increment the
* pointer to the current element (@p) by the size of the current
* element.
*
* Return: Pointer to the next element in the array.
*/
struct nvmf_ext_attr *nvmf_exat_ptr_next(struct nvmf_ext_attr *p);
/**
* enum nvme_version - Selector for version to be returned by @nvme_get_version
*
* @NVME_VERSION_PROJECT: Project release version
* @NVME_VERSION_GIT: Git reference
*/
enum nvme_version {
NVME_VERSION_PROJECT = 0,
NVME_VERSION_GIT = 1,
};
/**
* nvme_get_version - Return version libnvme string
* @type: Selects which version type (see @struct nvme_version)
*
* Return: Returns version string for known types or else "n/a"
*/
const char *nvme_get_version(enum nvme_version type);
/**
* nvme_uuid_to_string - Return string represenation of encoded UUID
* @uuid: Binary encoded input UUID
* @str: Output string represenation of UUID
*
* Return: Returns error code if type conversion fails.
*/
int nvme_uuid_to_string(unsigned char uuid[NVME_UUID_LEN], char *str);
/**
* nvme_uuid_from_string - Return encoded UUID represenation of string UUID
* @uuid: Binary encoded input UUID
* @str: Output string represenation of UUID
*
* Return: Returns error code if type conversion fails.
*/
int nvme_uuid_from_string(const char *str, unsigned char uuid[NVME_UUID_LEN]);
/**
* nvme_uuid_random - Generate random UUID
* @uuid: Generated random UUID
*
* Generate random number according
* https://www.rfc-editor.org/rfc/rfc4122#section-4.4
*
* Return: Returns error code if generating of random number fails.
*/
int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN]);
/**
* nvme_uuid_find - Find UUID position on UUID list
* @uuid_list: UUID list returned by identify UUID
* @uuid: Binary encoded input UUID
*
* Return: The array position where given UUID is present, or -1 on failure with errno set.
*/
int nvme_uuid_find(struct nvme_id_uuid_list *uuid_list, const unsigned char uuid[NVME_UUID_LEN]);
/**
* nvme_basename - Return the final path component (the one after the last '/')
* @path: A string containing a filesystem path
*
* Return: A pointer into the original null-terminated path string.
*/
char *nvme_basename(const char *path);