-
Notifications
You must be signed in to change notification settings - Fork 4
/
configfs.cpp
316 lines (269 loc) · 7.04 KB
/
configfs.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (c) 2017 Red Hat, Inc.
*
* This file is licensed to you under your choice of the GNU Lesser
* General Public License, version 2.1 or any later version (LGPLv2.1 or
* later), or the Apache License 2.0.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>
#include "libtcmu_common.h"
#include "libtcmu_priv.h"
#include <photon/common/alog.h>
#define CFGFS_BUF_SIZE 4096
int tcmu_cfgfs_get_int(const char *path)
{
int fd;
char buf[16];
ssize_t ret;
unsigned long val;
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
LOG_ERROR("Kernel does not support configfs file `", path);
} else {
LOG_ERROR("Could not open configfs file `: `", path, strerror(errno));
}
return -errno;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
LOG_ERROR("Could not read configfs to read attribute `: `", path, strerror(errno));
return -errno;
}
val = strtoul(buf, NULL, 0);
if (val > INT_MAX ) {
LOG_ERROR("could not convert string ` to value", buf);
return -EINVAL;
}
return val;
}
int tcmu_cfgfs_dev_get_attr_int(struct tcmu_device *dev, const char *name)
{
char path[PATH_MAX];
snprintf(path, sizeof(path), CFGFS_CORE"/%s/%s/attrib/%s",
dev->tcm_hba_name, dev->tcm_dev_name, name);
return tcmu_cfgfs_get_int(path);
}
uint64_t tcmu_cfgfs_dev_get_info_u64(struct tcmu_device *dev, const char *name,
int *fn_ret)
{
int fd;
char path[PATH_MAX];
char buf[CFGFS_BUF_SIZE];
ssize_t ret;
char *rover;
char *search_pattern;
uint64_t val;
*fn_ret = 0;
snprintf(path, sizeof(path), CFGFS_CORE"/%s/%s/info",
dev->tcm_hba_name, dev->tcm_dev_name);
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
LOG_ERROR("Kernel does not support device info file `.", path);
} else {
LOG_ERROR("Could not open device info file `: `", path, strerror(errno));
}
*fn_ret = -errno;
return 0;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
LOG_ERROR("Could not read configfs to read dev info: `", strerror(errno));
*fn_ret = -EINVAL;
return 0;
} else if (ret == 0) {
LOG_ERROR("Invalid device info.");
*fn_ret = -EINVAL;
return 0;
}
buf[ret-1] = '\0'; /* paranoid? Ensure null terminated */
if (asprintf(&search_pattern, " %s: ", name) < 0) {
LOG_ERROR("Could not create search string.");
*fn_ret = -ENOMEM;
return 0;
}
rover = strstr(buf, search_pattern);
free(search_pattern);
if (!rover) {
LOG_ERROR("Could not find \" `: \" in `: `", name, path, strerror(errno));
*fn_ret = -EINVAL;
return 0;
}
rover += strlen(name) + 3; /* name plus ':' and spaces before/after */
val = strtoull(rover, NULL, 0);
if (val == ULLONG_MAX) {
LOG_ERROR("Could not get `: `", name, strerror(errno));
*fn_ret = -EINVAL;
return 0;
}
return val;
}
int tcmu_cfgfs_dev_set_ctrl_u64(struct tcmu_device *dev, const char *key,
uint64_t val)
{
char path[PATH_MAX];
char buf[CFGFS_BUF_SIZE];
snprintf(path, sizeof(path), CFGFS_CORE"/%s/%s/control",
dev->tcm_hba_name, dev->tcm_dev_name);
snprintf(buf, sizeof(buf), "%s=%" PRIu64"", key, val);
return tcmu_cfgfs_set_str(path, buf, strlen(buf) + 1);
}
int tcmu_cfgfs_mod_param_set_u32(const char *name, uint32_t val)
{
char path[PATH_MAX];
snprintf(path, sizeof(path), CFGFS_MOD_PARAM"/%s", name);
return tcmu_cfgfs_set_u32(path, val);
}
/*
* Return a string that contains the device's WWN, or NULL.
*
* Callers must free the result with free().
*/
char *tcmu_cfgfs_dev_get_wwn(struct tcmu_device *dev)
{
int fd;
char path[PATH_MAX];
char buf[CFGFS_BUF_SIZE];
char *ret_buf;
int ret;
snprintf(path, sizeof(path),
CFGFS_CORE"/%s/%s/wwn/vpd_unit_serial",
dev->tcm_hba_name, dev->tcm_dev_name);
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
LOG_ERROR("Kernel does not support unit serial file `", path);
} else {
LOG_ERROR("Could not open unit serial file `: `", path, strerror(errno));
}
return NULL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
LOG_ERROR("Could not read configfs to read unit serial: `", strerror(errno));
return NULL;
} else if (ret == 0) {
LOG_ERROR("Invalid VPD serial number.");
return NULL;
}
/* Kill the trailing '\n' */
buf[ret-1] = '\0';
/* Skip to the good stuff */
ret = asprintf(&ret_buf, "%s", &buf[28]);
if (ret == -1) {
LOG_ERROR("could not convert string to value: `", strerror(errno));
return NULL;
}
return ret_buf;
}
char *tcmu_cfgfs_get_str(const char *path)
{
int fd, n;
char buf[CFGFS_BUF_SIZE];
ssize_t ret;
char *val;
memset(buf, 0, sizeof(buf));
fd = open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
LOG_ERROR("Kernel does not support configfs file `", path);
} else {
LOG_ERROR("Could not open configfs file `: `", path, strerror(errno));
}
return NULL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
LOG_ERROR("Could not read configfs to read attribute `: `", path, strerror(errno));
return NULL;
}
if (ret == 0)
return NULL;
/*
* Some files like members will terminate each member/line with a null
* char. Except for the last one, replace it with '\n' so parsers will
* just see an empty member.
*/
if (ret != (ssize_t)strlen(buf)) {
do {
n = strlen(buf);
buf[n] = '\n';
} while (n < ret);
}
/*
* Some files like members ends with a null char, but other files like
* the alua ones end with a newline.
*/
if (buf[ret - 1] == '\n')
buf[ret - 1] = '\0';
if (buf[ret - 1] != '\0') {
if (ret >= CFGFS_BUF_SIZE) {
LOG_ERROR("Invalid cfgfs file `: not enough space for ending null char.",
path);
return NULL;
}
/*
* In case the file does "return sprintf()" with no ending
* newline add the ending null so we will not crash below.
*/
buf[ret] = '\0';
}
val = strdup(buf);
if (!val) {
LOG_ERROR("could not copy buffer ` : `", buf, strerror(errno));
return NULL;
}
return val;
}
int tcmu_cfgfs_set_str(const char *path, const char *val, int val_len)
{
int fd;
ssize_t ret;
fd = open(path, O_WRONLY);
if (fd == -1) {
if (errno == ENOENT) {
LOG_ERROR("Kernel does not support configfs file `", path);
} else {
LOG_ERROR("Could not open configfs file `: `", path, strerror(errno));
}
return -errno;
}
ret = write(fd, val, val_len);
close(fd);
if (ret == -1) {
LOG_ERROR("Could not write configfs to write attribute `: `",
path, strerror(errno));
return -errno;
}
return 0;
}
int tcmu_cfgfs_set_u32(const char *path, uint32_t val)
{
char buf[20];
sprintf(buf, "%" PRIu32"", val);
return tcmu_cfgfs_set_str(path, buf, strlen(buf) + 1);
}
int tcmu_cfgfs_dev_exec_action(struct tcmu_device *dev, const char *name,
uint32_t val)
{
char path[PATH_MAX];
int ret;
snprintf(path, sizeof(path), CFGFS_CORE"/%s/%s/action/%s",
dev->tcm_hba_name, dev->tcm_dev_name, name);
LOG_DEBUG("dev: `, executing action `", dev->tcm_dev_name, name);
ret = tcmu_cfgfs_set_u32(path, val);
LOG_DEBUG("dev: `, action ` done", dev->tcm_dev_name, name);
return ret;
}