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
|
/*
* sysfs - extract md related information from sysfs. Part of:
* mdadm - manage Linux "md" devices aka RAID arrays.
*
* Copyright (C) 2006 Neil Brown <neilb@suse.de>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Neil Brown
* Email: <neilb@suse.de>
*/
#include "mdadm.h"
#include <dirent.h>
int load_sys(char *path, char *buf)
{
int fd = open(path, O_RDONLY);
int n;
if (fd < 0)
return -1;
n = read(fd, buf, 1024);
close(fd);
if (n <=0 || n >= 1024)
return -1;
buf[n] = 0;
if (buf[n-1] == '\n')
buf[n-1] = 0;
return 0;
}
void sysfs_free(struct sysarray *sra)
{
if (!sra)
return;
while (sra->devs) {
struct sysdev *d = sra->devs;
sra->devs = d->next;
free(d);
}
free(sra);
}
struct sysarray *sysfs_read(int fd, int devnum, unsigned long options)
{
/* Longest possible name in sysfs, mounted at /sys, is
* /sys/block/md_dXXX/md/dev-XXXXX/block/dev
* /sys/block/md_dXXX/md/metadata_version
* which is about 41 characters. 50 should do for now
*/
char fname[50];
char buf[1024];
char *base;
char *dbase;
struct sysarray *sra;
struct sysdev *dev;
DIR *dir;
struct dirent *de;
sra = malloc(sizeof(*sra));
if (sra == NULL)
return sra;
if (fd >= 0) {
struct stat stb;
if (fstat(fd, &stb)) return NULL;
if (major(stb.st_rdev)==9)
sprintf(sra->name, "md%d", minor(stb.st_rdev));
else
sprintf(sra->name, "md_d%d",
minor(stb.st_rdev)>>MdpMinorShift);
} else {
if (devnum >= 0)
sprintf(sra->name, "md%d", devnum);
else
sprintf(sra->name, "md_d%d",
-1-devnum);
}
sprintf(fname, "/sys/block/%s/md/", sra->name);
base = fname + strlen(fname);
sra->devs = NULL;
if (options & GET_VERSION) {
strcpy(base, "metadata_version");
if (load_sys(fname, buf))
goto abort;
if (strncmp(buf, "none", 4) == 0)
sra->major_version = sra->minor_version = -1;
else
sscanf(buf, "%d.%d",
&sra->major_version, &sra->minor_version);
}
if (options & GET_LEVEL) {
strcpy(base, "level");
if (load_sys(fname, buf))
goto abort;
sra->level = map_name(pers, buf);
}
if (options & GET_LAYOUT) {
strcpy(base, "layout");
if (load_sys(fname, buf))
goto abort;
sra->layout = strtoul(buf, NULL, 0);
}
if (options & GET_COMPONENT) {
strcpy(base, "component_size");
if (load_sys(fname, buf))
goto abort;
sra->component_size = strtoull(buf, NULL, 0);
/* sysfs reports "K", but we want sectors */
sra->component_size *= 2;
}
if (options & GET_CHUNK) {
strcpy(base, "chunk_size");
if (load_sys(fname, buf))
goto abort;
sra->chunk = strtoul(buf, NULL, 0);
}
if (options & GET_CACHE) {
strcpy(base, "stripe_cache_size");
if (load_sys(fname, buf))
goto abort;
sra->cache_size = strtoul(buf, NULL, 0);
}
if (options & GET_MISMATCH) {
strcpy(base, "mismatch_cnt");
if (load_sys(fname, buf))
goto abort;
sra->mismatch_cnt = strtoul(buf, NULL, 0);
}
if (! (options & GET_DEVS))
return sra;
/* Get all the devices as well */
*base = 0;
dir = opendir(fname);
if (!dir)
goto abort;
sra->spares = 0;
while ((de = readdir(dir)) != NULL) {
char *ep;
if (de->d_ino == 0 ||
strncmp(de->d_name, "dev-", 4) != 0)
continue;
strcpy(base, de->d_name);
dbase = base + strlen(base);
*dbase++ = '/';
dev = malloc(sizeof(*dev));
if (!dev)
goto abort;
dev->next = sra->devs;
sra->devs = dev;
strcpy(dev->name, de->d_name);
/* Always get slot, major, minor */
strcpy(dbase, "slot");
if (load_sys(fname, buf))
goto abort;
dev->role = strtoul(buf, &ep, 10);
if (*ep) dev->role = -1;
strcpy(dbase, "block/dev");
if (load_sys(fname, buf))
goto abort;
sscanf(buf, "%d:%d", &dev->major, &dev->minor);
if (options & GET_OFFSET) {
strcpy(dbase, "offset");
if (load_sys(fname, buf))
goto abort;
dev->offset = strtoull(buf, NULL, 0);
}
if (options & GET_SIZE) {
strcpy(dbase, "size");
if (load_sys(fname, buf))
goto abort;
dev->size = strtoull(buf, NULL, 0);
}
if (options & GET_STATE) {
dev->state = 0;
strcpy(dbase, "state");
if (load_sys(fname, buf))
goto abort;
if (strstr(buf, "in_sync"))
dev->state |= (1<<MD_DISK_SYNC);
if (strstr(buf, "faulty"))
dev->state |= (1<<MD_DISK_FAULTY);
if (dev->state == 0)
sra->spares++;
}
if (options & GET_ERROR) {
strcpy(buf, "errors");
if (load_sys(fname, buf))
goto abort;
dev->errors = strtoul(buf, NULL, 0);
}
}
return sra;
abort:
sysfs_free(sra);
return NULL;
}
unsigned long long get_component_size(int fd)
{
/* Find out the component size of the array.
* We cannot trust GET_ARRAY_INFO ioctl as it's
* size field is only 32bits.
* So look in /sys/block/mdXXX/md/component_size
*
* This returns in units of sectors.
*/
struct stat stb;
char fname[50];
int n;
if (fstat(fd, &stb)) return 0;
if (major(stb.st_rdev) == 9)
sprintf(fname, "/sys/block/md%d/md/component_size",
minor(stb.st_rdev));
else
sprintf(fname, "/sys/block/md_d%d/md/component_size",
minor(stb.st_rdev)>>MdpMinorShift);
fd = open(fname, O_RDONLY);
if (fd < 0)
return 0;
n = read(fd, fname, sizeof(fname));
close(fd);
if (n == sizeof(fname))
return 0;
fname[n] = 0;
return strtoull(fname, NULL, 10) * 2;
}
int sysfs_set_str(struct sysarray *sra, struct sysdev *dev,
char *name, char *val)
{
char fname[50];
int n;
int fd;
sprintf(fname, "/sys/block/%s/md/%s/%s",
sra->name, dev?dev->name:"", name);
fd = open(fname, O_WRONLY);
if (fd < 0)
return -1;
n = write(fd, val, strlen(val));
close(fd);
if (n != strlen(val))
return -1;
return 0;
}
int sysfs_set_num(struct sysarray *sra, struct sysdev *dev,
char *name, unsigned long long val)
{
char valstr[50];
sprintf(valstr, "%llu", val);
return sysfs_set_str(sra, dev, name, valstr);
}
int sysfs_get_ll(struct sysarray *sra, struct sysdev *dev,
char *name, unsigned long long *val)
{
char fname[50];
char buf[50];
int n;
int fd;
char *ep;
sprintf(fname, "/sys/block/%s/md/%s/%s",
sra->name, dev?dev->name:"", name);
fd = open(fname, O_RDONLY);
if (fd < 0)
return -1;
n = read(fd, buf, sizeof(buf));
close(fd);
if (n <= 0)
return -1;
buf[n] = 0;
*val = strtoull(buf, &ep, 0);
if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
return -1;
return 0;
}
|