forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfs.c
258 lines (217 loc) · 5.51 KB
/
configfs.c
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
/*
* Copyright 2017, Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "libtcmu_log.h"
#include "libtcmu_common.h"
#include "libtcmu_priv.h"
#define CFGFS_BUF_SIZE 4096
int tcmu_get_cfgfs_int(const char *path)
{
int fd;
char buf[16];
ssize_t ret;
unsigned long val;
fd = open(path, O_RDONLY);
if (fd == -1) {
tcmu_err("Could not open configfs to read attribute %s: %s\n",
path, strerror(errno));
return -errno;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read attribute %s: %s\n",
path, strerror(errno));
return -errno;
}
val = strtoul(buf, NULL, 0);
if (val > INT_MAX ) {
tcmu_err("could not convert string %s to value\n", buf);
return -EINVAL;
}
return val;
}
int tcmu_get_attribute(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_get_cfgfs_int(path);
}
/*
* Return a string that contains the device's WWN, or NULL.
*
* Callers must free the result with free().
*/
char *tcmu_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) {
tcmu_err("Could not open configfs to read unit serial: %s\n",
strerror(errno));
return NULL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read unit serial: %s\n",
strerror(errno));
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) {
tcmu_err("could not convert string to value: %s\n",
strerror(errno));
return NULL;
}
return ret_buf;
}
long long tcmu_get_device_size(struct tcmu_device *dev)
{
int fd;
char path[PATH_MAX];
char buf[CFGFS_BUF_SIZE];
ssize_t ret;
char *rover;
unsigned long long size;
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) {
tcmu_err("Could not open configfs to read dev info: %s\n",
strerror(errno));
return -EINVAL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read dev info: %s\n",
strerror(errno));
return -EINVAL;
}
buf[sizeof(buf)-1] = '\0'; /* paranoid? Ensure null terminated */
rover = strstr(buf, " Size: ");
if (!rover) {
tcmu_err("Could not find \" Size: \" in %s: %s\n", path,
strerror(errno));
return -EINVAL;
}
rover += 7; /* get to the value */
size = strtoull(rover, NULL, 0);
if (size == ULLONG_MAX) {
tcmu_err("Could not get size: %s\n", strerror(errno));
return -EINVAL;
}
return size;
}
char *tcmu_get_cfgfs_str(const char *path)
{
int fd;
char buf[CFGFS_BUF_SIZE];
ssize_t ret;
char *val;
memset(buf, 0, sizeof(buf));
fd = open(path, O_RDONLY);
if (fd == -1) {
tcmu_err("Could not open configfs to read attribute %s: %s\n",
path, strerror(errno));
return NULL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read attribute %s: %s\n",
path, strerror(errno));
return NULL;
}
if (ret == 0)
return NULL;
/*
* 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) {
tcmu_err("Invalid cfgfs file %s: not enough space for ending null char.\n",
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) {
tcmu_err("could not copy buffer %s : %s\n",
buf, strerror(errno));
return NULL;
}
return val;
}
int tcmu_set_cfgfs_str(const char *path, const char *val, int val_len)
{
int fd;
ssize_t ret;
fd = open(path, O_WRONLY);
if (fd == -1) {
tcmu_err("Could not open configfs to write attribute %s: %s\n",
path, strerror(errno));
return -errno;
}
ret = write(fd, val, val_len);
close(fd);
if (ret == -1) {
tcmu_err("Could not write configfs to write attribute %s: %s\n",
path, strerror(errno));
return -errno;
}
return 0;
}
int tcmu_set_cfgfs_ul(const char *path, unsigned long val)
{
char buf[20];
sprintf(buf, "%lu", val);
return tcmu_set_cfgfs_str(path, buf, strlen(buf) + 1);
}
int tcmu_exec_cfgfs_dev_action(struct tcmu_device *dev, const char *name,
unsigned long val)
{
char path[PATH_MAX];
snprintf(path, sizeof(path), CFGFS_CORE"/%s/%s/action/%s",
dev->tcm_hba_name, dev->tcm_dev_name, name);
return tcmu_set_cfgfs_ul(path, val);
}