]>
Commit | Line | Data |
---|---|---|
e86c9dd6 NB |
1 | /* |
2 | * sysfs - extract md related information from sysfs. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
e736b623 | 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
e86c9dd6 NB |
6 | * |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | */ | |
25 | ||
26 | #include "mdadm.h" | |
27 | #include <dirent.h> | |
1770662b | 28 | #include <ctype.h> |
e86c9dd6 | 29 | |
193b6c0b | 30 | int load_sys(char *path, char *buf, int len) |
e86c9dd6 NB |
31 | { |
32 | int fd = open(path, O_RDONLY); | |
33 | int n; | |
34 | if (fd < 0) | |
35 | return -1; | |
193b6c0b | 36 | n = read(fd, buf, len); |
e86c9dd6 | 37 | close(fd); |
193b6c0b | 38 | if (n <0 || n >= len) |
e86c9dd6 NB |
39 | return -1; |
40 | buf[n] = 0; | |
8dfb8619 | 41 | if (n && buf[n-1] == '\n') |
e86c9dd6 NB |
42 | buf[n-1] = 0; |
43 | return 0; | |
44 | } | |
45 | ||
7e0f6979 | 46 | void sysfs_free(struct mdinfo *sra) |
8382f19b | 47 | { |
7e0f6979 NB |
48 | while (sra) { |
49 | struct mdinfo *sra2 = sra->next; | |
50 | while (sra->devs) { | |
51 | struct mdinfo *d = sra->devs; | |
52 | sra->devs = d->next; | |
53 | free(d); | |
54 | } | |
55 | free(sra); | |
56 | sra = sra2; | |
8382f19b | 57 | } |
8382f19b NB |
58 | } |
59 | ||
4dd2df09 | 60 | int sysfs_open(char *devnm, char *devname, char *attr) |
549e9569 NB |
61 | { |
62 | char fname[50]; | |
549e9569 | 63 | int fd; |
549e9569 | 64 | |
4dd2df09 | 65 | sprintf(fname, "/sys/block/%s/md/", devnm); |
549e9569 NB |
66 | if (devname) { |
67 | strcat(fname, devname); | |
68 | strcat(fname, "/"); | |
69 | } | |
70 | strcat(fname, attr); | |
71 | fd = open(fname, O_RDWR); | |
ea6d09b0 | 72 | if (fd < 0 && errno == EACCES) |
549e9569 NB |
73 | fd = open(fname, O_RDONLY); |
74 | return fd; | |
75 | } | |
76 | ||
9465f170 GJ |
77 | void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid) |
78 | { | |
79 | snprintf(mdi->sys_name, | |
80 | sizeof(mdi->sys_name), "dev-%s", devid2kname(devid)); | |
81 | } | |
82 | ||
4dd2df09 | 83 | void sysfs_init(struct mdinfo *mdi, int fd, char *devnm) |
f35f2525 | 84 | { |
678a4a36 | 85 | mdi->sys_name[0] = 0; |
f35f2525 | 86 | if (fd >= 0) { |
f35f2525 | 87 | mdu_version_t vers; |
f35f2525 N |
88 | if (ioctl(fd, RAID_VERSION, &vers) != 0) |
89 | return; | |
4dd2df09 | 90 | devnm = fd2devnm(fd); |
f35f2525 | 91 | } |
4dd2df09 | 92 | if (devnm == NULL) |
d7ab966b | 93 | return; |
4dd2df09 | 94 | strcpy(mdi->sys_name, devnm); |
f35f2525 N |
95 | } |
96 | ||
4dd2df09 | 97 | struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options) |
e86c9dd6 | 98 | { |
33a6535d AW |
99 | char fname[PATH_MAX]; |
100 | char buf[PATH_MAX]; | |
e86c9dd6 NB |
101 | char *base; |
102 | char *dbase; | |
7e0f6979 | 103 | struct mdinfo *sra; |
64e103fe | 104 | struct mdinfo *dev, **devp; |
355726fa | 105 | DIR *dir = NULL; |
e86c9dd6 NB |
106 | struct dirent *de; |
107 | ||
503975b9 | 108 | sra = xcalloc(1, sizeof(*sra)); |
4dd2df09 | 109 | sysfs_init(sra, fd, devnm); |
678a4a36 N |
110 | if (sra->sys_name[0] == 0) { |
111 | free(sra); | |
112 | return NULL; | |
113 | } | |
e86c9dd6 | 114 | |
7e0f6979 | 115 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
116 | base = fname + strlen(fname); |
117 | ||
118 | sra->devs = NULL; | |
8382f19b NB |
119 | if (options & GET_VERSION) { |
120 | strcpy(base, "metadata_version"); | |
193b6c0b | 121 | if (load_sys(fname, buf, sizeof(buf))) |
8382f19b | 122 | goto abort; |
294d6f45 | 123 | if (strncmp(buf, "none", 4) == 0) { |
7e0f6979 NB |
124 | sra->array.major_version = |
125 | sra->array.minor_version = -1; | |
294d6f45 NB |
126 | strcpy(sra->text_version, ""); |
127 | } else if (strncmp(buf, "external:", 9) == 0) { | |
142cb9e1 NB |
128 | sra->array.major_version = -1; |
129 | sra->array.minor_version = -2; | |
130 | strcpy(sra->text_version, buf+9); | |
b8ac1967 | 131 | } else { |
8382f19b | 132 | sscanf(buf, "%d.%d", |
7e0f6979 NB |
133 | &sra->array.major_version, |
134 | &sra->array.minor_version); | |
b8ac1967 NB |
135 | strcpy(sra->text_version, buf); |
136 | } | |
8382f19b | 137 | } |
e86c9dd6 NB |
138 | if (options & GET_LEVEL) { |
139 | strcpy(base, "level"); | |
193b6c0b | 140 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 141 | goto abort; |
7e0f6979 | 142 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
143 | } |
144 | if (options & GET_LAYOUT) { | |
145 | strcpy(base, "layout"); | |
193b6c0b | 146 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 147 | goto abort; |
7e0f6979 | 148 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 | 149 | } |
549e9569 NB |
150 | if (options & GET_DISKS) { |
151 | strcpy(base, "raid_disks"); | |
193b6c0b | 152 | if (load_sys(fname, buf, sizeof(buf))) |
549e9569 NB |
153 | goto abort; |
154 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
f1d26766 DW |
155 | } |
156 | if (options & GET_DEGRADED) { | |
157 | strcpy(base, "degraded"); | |
193b6c0b | 158 | if (load_sys(fname, buf, sizeof(buf))) |
f1d26766 DW |
159 | goto abort; |
160 | sra->array.failed_disks = strtoul(buf, NULL, 0); | |
549e9569 | 161 | } |
e86c9dd6 NB |
162 | if (options & GET_COMPONENT) { |
163 | strcpy(base, "component_size"); | |
193b6c0b | 164 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
165 | goto abort; |
166 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
167 | /* sysfs reports "K", but we want sectors */ |
168 | sra->component_size *= 2; | |
e86c9dd6 NB |
169 | } |
170 | if (options & GET_CHUNK) { | |
171 | strcpy(base, "chunk_size"); | |
193b6c0b | 172 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 173 | goto abort; |
7e0f6979 | 174 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 175 | } |
758d3a8e NB |
176 | if (options & GET_CACHE) { |
177 | strcpy(base, "stripe_cache_size"); | |
193b6c0b | 178 | if (load_sys(fname, buf, sizeof(buf))) |
6a67848a N |
179 | /* Probably level doesn't support it */ |
180 | sra->cache_size = 0; | |
181 | else | |
182 | sra->cache_size = strtoul(buf, NULL, 0); | |
758d3a8e | 183 | } |
37dfc3d6 NB |
184 | if (options & GET_MISMATCH) { |
185 | strcpy(base, "mismatch_cnt"); | |
193b6c0b | 186 | if (load_sys(fname, buf, sizeof(buf))) |
37dfc3d6 NB |
187 | goto abort; |
188 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
189 | } | |
1770662b DW |
190 | if (options & GET_SAFEMODE) { |
191 | int scale = 1; | |
192 | int dot = 0; | |
f21e18ca | 193 | unsigned i; |
1770662b DW |
194 | unsigned long msec; |
195 | size_t len; | |
196 | ||
197 | strcpy(base, "safe_mode_delay"); | |
193b6c0b | 198 | if (load_sys(fname, buf, sizeof(buf))) |
1770662b DW |
199 | goto abort; |
200 | ||
201 | /* remove a period, and count digits after it */ | |
202 | len = strlen(buf); | |
203 | for (i = 0; i < len; i++) { | |
204 | if (dot) { | |
205 | if (isdigit(buf[i])) { | |
206 | buf[i-1] = buf[i]; | |
207 | scale *= 10; | |
208 | } | |
209 | buf[i] = 0; | |
210 | } else if (buf[i] == '.') { | |
211 | dot=1; | |
212 | buf[i] = 0; | |
213 | } | |
214 | } | |
215 | msec = strtoul(buf, NULL, 10); | |
216 | msec = (msec * 1000) / scale; | |
217 | sra->safe_mode_delay = msec; | |
218 | } | |
c0c1acd6 N |
219 | if (options & GET_BITMAP_LOCATION) { |
220 | strcpy(base, "bitmap/location"); | |
193b6c0b | 221 | if (load_sys(fname, buf, sizeof(buf))) |
c0c1acd6 N |
222 | goto abort; |
223 | if (strncmp(buf, "file", 4) == 0) | |
224 | sra->bitmap_offset = 1; | |
225 | else if (strncmp(buf, "none", 4) == 0) | |
226 | sra->bitmap_offset = 0; | |
227 | else if (buf[0] == '+') | |
fbdef498 | 228 | sra->bitmap_offset = strtol(buf+1, NULL, 10); |
c0c1acd6 N |
229 | else |
230 | goto abort; | |
231 | } | |
e86c9dd6 | 232 | |
5aa644c6 SL |
233 | if (options & GET_ARRAY_STATE) { |
234 | strcpy(base, "array_state"); | |
193b6c0b JS |
235 | if (load_sys(fname, sra->sysfs_array_state, |
236 | sizeof(sra->sysfs_array_state))) | |
5aa644c6 SL |
237 | goto abort; |
238 | } else | |
239 | sra->sysfs_array_state[0] = 0; | |
240 | ||
e86c9dd6 NB |
241 | if (! (options & GET_DEVS)) |
242 | return sra; | |
243 | ||
244 | /* Get all the devices as well */ | |
245 | *base = 0; | |
246 | dir = opendir(fname); | |
247 | if (!dir) | |
248 | goto abort; | |
7e0f6979 | 249 | sra->array.spare_disks = 0; |
e86c9dd6 | 250 | |
64e103fe N |
251 | devp = &sra->devs; |
252 | sra->devs = NULL; | |
e86c9dd6 NB |
253 | while ((de = readdir(dir)) != NULL) { |
254 | char *ep; | |
255 | if (de->d_ino == 0 || | |
256 | strncmp(de->d_name, "dev-", 4) != 0) | |
257 | continue; | |
258 | strcpy(base, de->d_name); | |
259 | dbase = base + strlen(base); | |
260 | *dbase++ = '/'; | |
261 | ||
503975b9 | 262 | dev = xmalloc(sizeof(*dev)); |
e86c9dd6 NB |
263 | |
264 | /* Always get slot, major, minor */ | |
265 | strcpy(dbase, "slot"); | |
193b6c0b | 266 | if (load_sys(fname, buf, sizeof(buf))) { |
4795982e DW |
267 | /* hmm... unable to read 'slot' maybe the device |
268 | * is going away? | |
269 | */ | |
270 | strcpy(dbase, "block"); | |
271 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
272 | errno != ENAMETOOLONG) { | |
273 | /* ...yup device is gone */ | |
274 | free(dev); | |
275 | continue; | |
276 | } else { | |
277 | /* slot is unreadable but 'block' link | |
278 | * still intact... something bad is happening | |
279 | * so abort | |
280 | */ | |
281 | free(dev); | |
282 | goto abort; | |
283 | } | |
64e103fe | 284 | |
4795982e | 285 | } |
4795982e | 286 | strcpy(dev->sys_name, de->d_name); |
06c7f68e NB |
287 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
288 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
289 | |
290 | strcpy(dbase, "block/dev"); | |
193b6c0b | 291 | if (load_sys(fname, buf, sizeof(buf))) { |
b526e52d DW |
292 | /* assume this is a stale reference to a hot |
293 | * removed device | |
294 | */ | |
dab4a513 | 295 | free(dev); |
b526e52d | 296 | continue; |
dab4a513 | 297 | } |
e6fc80a8 | 298 | sra->array.nr_disks++; |
06c7f68e | 299 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 | 300 | |
dab4a513 | 301 | /* special case check for block devices that can go 'offline' */ |
b526e52d | 302 | strcpy(dbase, "block/device/state"); |
193b6c0b | 303 | if (load_sys(fname, buf, sizeof(buf)) == 0 && |
b526e52d DW |
304 | strncmp(buf, "offline", 7) == 0) { |
305 | free(dev); | |
306 | continue; | |
dab4a513 DW |
307 | } |
308 | ||
309 | /* finally add this disk to the array */ | |
64e103fe N |
310 | *devp = dev; |
311 | devp = & dev->next; | |
312 | dev->next = NULL; | |
dab4a513 | 313 | |
e86c9dd6 NB |
314 | if (options & GET_OFFSET) { |
315 | strcpy(dbase, "offset"); | |
193b6c0b | 316 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 317 | goto abort; |
06c7f68e | 318 | dev->data_offset = strtoull(buf, NULL, 0); |
fe384ca0 | 319 | strcpy(dbase, "new_offset"); |
193b6c0b | 320 | if (load_sys(fname, buf, sizeof(buf)) == 0) |
fe384ca0 N |
321 | dev->new_data_offset = strtoull(buf, NULL, 0); |
322 | else | |
323 | dev->new_data_offset = dev->data_offset; | |
e86c9dd6 NB |
324 | } |
325 | if (options & GET_SIZE) { | |
326 | strcpy(dbase, "size"); | |
193b6c0b | 327 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 328 | goto abort; |
047d2e49 | 329 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
330 | } |
331 | if (options & GET_STATE) { | |
06c7f68e | 332 | dev->disk.state = 0; |
e86c9dd6 | 333 | strcpy(dbase, "state"); |
193b6c0b | 334 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
335 | goto abort; |
336 | if (strstr(buf, "in_sync")) | |
06c7f68e | 337 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 338 | if (strstr(buf, "faulty")) |
06c7f68e NB |
339 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
340 | if (dev->disk.state == 0) | |
7e0f6979 | 341 | sra->array.spare_disks++; |
e86c9dd6 NB |
342 | } |
343 | if (options & GET_ERROR) { | |
344 | strcpy(buf, "errors"); | |
193b6c0b | 345 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
346 | goto abort; |
347 | dev->errors = strtoul(buf, NULL, 0); | |
348 | } | |
349 | } | |
355726fa | 350 | closedir(dir); |
e86c9dd6 NB |
351 | return sra; |
352 | ||
353 | abort: | |
355726fa NB |
354 | if (dir) |
355 | closedir(dir); | |
8382f19b | 356 | sysfs_free(sra); |
e86c9dd6 NB |
357 | return NULL; |
358 | } | |
359 | ||
1770662b DW |
360 | int sysfs_attr_match(const char *attr, const char *str) |
361 | { | |
362 | /* See if attr, read from a sysfs file, matches | |
363 | * str. They must either be the same, or attr can | |
364 | * have a trailing newline or comma | |
365 | */ | |
366 | while (*attr && *str && *attr == *str) { | |
367 | attr++; | |
368 | str++; | |
369 | } | |
370 | ||
371 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
372 | return 0; | |
373 | return 1; | |
374 | } | |
375 | ||
376 | int sysfs_match_word(const char *word, char **list) | |
377 | { | |
378 | int n; | |
379 | for (n=0; list[n]; n++) | |
380 | if (sysfs_attr_match(word, list[n])) | |
381 | break; | |
382 | return n; | |
383 | } | |
384 | ||
e86c9dd6 NB |
385 | unsigned long long get_component_size(int fd) |
386 | { | |
387 | /* Find out the component size of the array. | |
388 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
389 | * size field is only 32bits. | |
390 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 391 | * |
8686f3ed | 392 | * This returns in units of sectors. |
e86c9dd6 NB |
393 | */ |
394 | struct stat stb; | |
395 | char fname[50]; | |
396 | int n; | |
397 | if (fstat(fd, &stb)) return 0; | |
f21e18ca | 398 | if (major(stb.st_rdev) != (unsigned)get_mdp_major()) |
e86c9dd6 | 399 | sprintf(fname, "/sys/block/md%d/md/component_size", |
ea24acd0 | 400 | (int)minor(stb.st_rdev)); |
e86c9dd6 NB |
401 | else |
402 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
ea24acd0 | 403 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
404 | fd = open(fname, O_RDONLY); |
405 | if (fd < 0) | |
406 | return 0; | |
407 | n = read(fd, fname, sizeof(fname)); | |
408 | close(fd); | |
99f6e521 | 409 | if (n < 0 || n == sizeof(fname)) |
e86c9dd6 NB |
410 | return 0; |
411 | fname[n] = 0; | |
8686f3ed | 412 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
413 | } |
414 | ||
7e0f6979 | 415 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
416 | char *name, char *val) |
417 | { | |
418 | char fname[50]; | |
f21e18ca | 419 | unsigned int n; |
e86c9dd6 | 420 | int fd; |
7e1432fb | 421 | |
e86c9dd6 | 422 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 423 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
424 | fd = open(fname, O_WRONLY); |
425 | if (fd < 0) | |
426 | return -1; | |
427 | n = write(fd, val, strlen(val)); | |
428 | close(fd); | |
2a24d7b6 | 429 | if (n != strlen(val)) { |
1ade5cc1 N |
430 | dprintf("failed to write '%s' to '%s' (%s)\n", |
431 | val, fname, strerror(errno)); | |
e86c9dd6 | 432 | return -1; |
2a24d7b6 | 433 | } |
e86c9dd6 NB |
434 | return 0; |
435 | } | |
436 | ||
7e0f6979 | 437 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
438 | char *name, unsigned long long val) |
439 | { | |
440 | char valstr[50]; | |
441 | sprintf(valstr, "%llu", val); | |
442 | return sysfs_set_str(sra, dev, name, valstr); | |
443 | } | |
444 | ||
012a8641 JS |
445 | int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev, |
446 | char *name, long long val) | |
447 | { | |
448 | char valstr[50]; | |
449 | sprintf(valstr, "%lli", val); | |
450 | return sysfs_set_str(sra, dev, name, valstr); | |
451 | } | |
452 | ||
97590376 N |
453 | int sysfs_uevent(struct mdinfo *sra, char *event) |
454 | { | |
455 | char fname[50]; | |
456 | int n; | |
457 | int fd; | |
458 | ||
459 | sprintf(fname, "/sys/block/%s/uevent", | |
460 | sra->sys_name); | |
461 | fd = open(fname, O_WRONLY); | |
462 | if (fd < 0) | |
463 | return -1; | |
464 | n = write(fd, event, strlen(event)); | |
465 | close(fd); | |
e4c72d1d | 466 | if (n != (int)strlen(event)) { |
1ade5cc1 N |
467 | dprintf("failed to write '%s' to '%s' (%s)\n", |
468 | event, fname, strerror(errno)); | |
e4c72d1d LB |
469 | return -1; |
470 | } | |
97590376 | 471 | return 0; |
1011e834 | 472 | } |
97590376 | 473 | |
bc77ed53 DW |
474 | int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name) |
475 | { | |
476 | char fname[50]; | |
477 | struct stat st; | |
478 | ||
479 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
480 | sra->sys_name, dev?dev->sys_name:"", name); | |
481 | ||
482 | return stat(fname, &st) == 0; | |
483 | } | |
484 | ||
a6288483 N |
485 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, |
486 | char *name) | |
e86c9dd6 NB |
487 | { |
488 | char fname[50]; | |
e86c9dd6 | 489 | int fd; |
a6288483 | 490 | |
e86c9dd6 | 491 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 492 | sra->sys_name, dev?dev->sys_name:"", name); |
a6288483 | 493 | fd = open(fname, O_RDWR); |
e86c9dd6 | 494 | if (fd < 0) |
a6288483 N |
495 | fd = open(fname, O_RDONLY); |
496 | return fd; | |
497 | } | |
498 | ||
499 | int sysfs_fd_get_ll(int fd, unsigned long long *val) | |
500 | { | |
501 | char buf[50]; | |
502 | int n; | |
503 | char *ep; | |
504 | ||
505 | lseek(fd, 0, 0); | |
e86c9dd6 | 506 | n = read(fd, buf, sizeof(buf)); |
5418499a | 507 | if (n <= 0 || n == sizeof(buf)) |
6560987b | 508 | return -2; |
e86c9dd6 NB |
509 | buf[n] = 0; |
510 | *val = strtoull(buf, &ep, 0); | |
511 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
512 | return -1; | |
513 | return 0; | |
514 | } | |
2503d23b | 515 | |
a6288483 N |
516 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
517 | char *name, unsigned long long *val) | |
518 | { | |
519 | int n; | |
520 | int fd; | |
521 | ||
522 | fd = sysfs_get_fd(sra, dev, name); | |
523 | if (fd < 0) | |
524 | return -1; | |
525 | n = sysfs_fd_get_ll(fd, val); | |
526 | close(fd); | |
527 | return n; | |
528 | } | |
529 | ||
2eba8496 N |
530 | int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2) |
531 | { | |
532 | /* two numbers in this sysfs file, either | |
533 | * NNN (NNN) | |
534 | * or | |
535 | * NNN / NNN | |
536 | */ | |
537 | char buf[80]; | |
538 | int n; | |
539 | char *ep, *ep2; | |
540 | ||
541 | lseek(fd, 0, 0); | |
542 | n = read(fd, buf, sizeof(buf)); | |
5418499a | 543 | if (n <= 0 || n == sizeof(buf)) |
2eba8496 N |
544 | return -2; |
545 | buf[n] = 0; | |
546 | *v1 = strtoull(buf, &ep, 0); | |
547 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
548 | return -1; | |
549 | while (*ep == ' ' || *ep == '/' || *ep == '(') | |
550 | ep++; | |
551 | *v2 = strtoull(ep, &ep2, 0); | |
552 | if (ep2 == ep || (*ep2 != 0 && *ep2 != '\n' && *ep2 != ' ' && *ep2 != ')')) { | |
553 | *v2 = *v1; | |
554 | return 1; | |
555 | } | |
556 | return 2; | |
557 | } | |
558 | ||
559 | int sysfs_get_two(struct mdinfo *sra, struct mdinfo *dev, | |
560 | char *name, unsigned long long *v1, unsigned long long *v2) | |
561 | { | |
562 | int n; | |
563 | int fd; | |
564 | ||
565 | fd = sysfs_get_fd(sra, dev, name); | |
566 | if (fd < 0) | |
567 | return -1; | |
568 | n = sysfs_fd_get_two(fd, v1, v2); | |
569 | close(fd); | |
570 | return n; | |
571 | } | |
572 | ||
7236ee7a N |
573 | int sysfs_fd_get_str(int fd, char *val, int size) |
574 | { | |
575 | int n; | |
576 | ||
577 | lseek(fd, 0, 0); | |
578 | n = read(fd, val, size); | |
5418499a | 579 | if (n <= 0 || n == size) |
7236ee7a N |
580 | return -1; |
581 | val[n] = 0; | |
582 | return n; | |
583 | } | |
584 | ||
93ecfa01 N |
585 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, |
586 | char *name, char *val, int size) | |
587 | { | |
93ecfa01 N |
588 | int n; |
589 | int fd; | |
7236ee7a N |
590 | |
591 | fd = sysfs_get_fd(sra, dev, name); | |
93ecfa01 N |
592 | if (fd < 0) |
593 | return -1; | |
7236ee7a | 594 | n = sysfs_fd_get_str(fd, val, size); |
93ecfa01 | 595 | close(fd); |
93ecfa01 N |
596 | return n; |
597 | } | |
598 | ||
8ed3e5e1 DW |
599 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) |
600 | { | |
601 | unsigned long sec; | |
602 | unsigned long msec; | |
603 | char delay[30]; | |
604 | ||
605 | sec = ms / 1000; | |
0dd3ba30 | 606 | msec = ms % 1000; |
8ed3e5e1 | 607 | |
0dd3ba30 DW |
608 | sprintf(delay, "%ld.%03ld\n", sec, msec); |
609 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
8ed3e5e1 DW |
610 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); |
611 | } | |
612 | ||
f35f2525 | 613 | int sysfs_set_array(struct mdinfo *info, int vers) |
2503d23b NB |
614 | { |
615 | int rv = 0; | |
f35f2525 | 616 | char ver[100]; |
a5062b1c | 617 | int raid_disks = info->array.raid_disks; |
f35f2525 N |
618 | |
619 | ver[0] = 0; | |
620 | if (info->array.major_version == -1 && | |
621 | info->array.minor_version == -2) { | |
ddb12f6c AK |
622 | char buf[1024]; |
623 | ||
f35f2525 N |
624 | strcat(strcpy(ver, "external:"), info->text_version); |
625 | ||
ddb12f6c AK |
626 | /* meta version might already be set if we are setting |
627 | * new geometry for a reshape. In that case we don't | |
628 | * want to over-write the 'readonly' flag that is | |
629 | * stored in the metadata version. So read the current | |
630 | * version first, and preserve the flag | |
631 | */ | |
632 | if (sysfs_get_str(info, NULL, "metadata_version", | |
633 | buf, 1024) > 0) | |
634 | if (strlen(buf) >= 9 && buf[9] == '-') | |
635 | ver[9] = '-'; | |
636 | ||
f35f2525 N |
637 | if ((vers % 100) < 2 || |
638 | sysfs_set_str(info, NULL, "metadata_version", | |
639 | ver) < 0) { | |
7a862a02 | 640 | pr_err("This kernel does not support external metadata.\n"); |
f35f2525 N |
641 | return 1; |
642 | } | |
643 | } | |
2503d23b NB |
644 | if (info->array.level < 0) |
645 | return 0; /* FIXME */ | |
f35f2525 | 646 | rv |= sysfs_set_str(info, NULL, "level", |
2503d23b | 647 | map_num(pers, info->array.level)); |
a5062b1c AK |
648 | if (info->reshape_active && info->delta_disks != UnSet) |
649 | raid_disks -= info->delta_disks; | |
650 | rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks); | |
f35f2525 N |
651 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); |
652 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
653 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
da9b4a62 DW |
654 | if (info->custom_array_size) { |
655 | int rc; | |
656 | ||
657 | rc = sysfs_set_num(info, NULL, "array_size", | |
658 | info->custom_array_size/2); | |
659 | if (rc && errno == ENOENT) { | |
7a862a02 | 660 | pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n"); |
da9b4a62 DW |
661 | rc = 0; |
662 | } | |
663 | rv |= rc; | |
664 | } | |
665 | ||
f35f2525 N |
666 | if (info->array.level > 0) |
667 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
f897078e N |
668 | |
669 | if (info->reshape_active) { | |
670 | rv |= sysfs_set_num(info, NULL, "reshape_position", | |
671 | info->reshape_progress); | |
672 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk); | |
20a40eca N |
673 | rv |= sysfs_set_num(info, NULL, "layout", info->new_layout); |
674 | rv |= sysfs_set_num(info, NULL, "raid_disks", | |
a5062b1c | 675 | info->array.raid_disks); |
20a40eca N |
676 | /* We don't set 'new_level' here. That can only happen |
677 | * once the reshape completes. | |
f897078e N |
678 | */ |
679 | } | |
2503d23b NB |
680 | return rv; |
681 | } | |
682 | ||
2904b26f | 683 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) |
2503d23b | 684 | { |
33a6535d AW |
685 | char dv[PATH_MAX]; |
686 | char nm[PATH_MAX]; | |
2503d23b NB |
687 | char *dname; |
688 | int rv; | |
689 | ||
690 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
691 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
692 | if (rv) | |
693 | return rv; | |
694 | ||
695 | memset(nm, 0, sizeof(nm)); | |
bc17158d | 696 | dname = devid2kname(makedev(sd->disk.major, sd->disk.minor)); |
2503d23b NB |
697 | strcpy(sd->sys_name, "dev-"); |
698 | strcpy(sd->sys_name+4, dname); | |
699 | ||
2904b26f DW |
700 | /* test write to see if 'recovery_start' is available */ |
701 | if (resume && sd->recovery_start < MaxSector && | |
702 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
703 | sysfs_set_str(sra, sd, "state", "remove"); | |
704 | return -1; | |
705 | } | |
706 | ||
73649188 | 707 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
708 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
709 | if (sra->array.level != LEVEL_CONTAINER) { | |
d23534e4 | 710 | if (sd->recovery_start == MaxSector) |
462906cd N |
711 | /* This can correctly fail if array isn't started, |
712 | * yet, so just ignore status for now. | |
713 | */ | |
d23534e4 | 714 | sysfs_set_str(sra, sd, "state", "insync"); |
899aead0 AK |
715 | if (sd->disk.raid_disk >= 0) |
716 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
2904b26f DW |
717 | if (resume) |
718 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
2503d23b | 719 | } |
2503d23b NB |
720 | return rv; |
721 | } | |
90c8b707 | 722 | |
755c99fa | 723 | #if 0 |
90c8b707 DW |
724 | int sysfs_disk_to_sg(int fd) |
725 | { | |
726 | /* from an open block device, try find and open its corresponding | |
727 | * scsi_generic interface | |
728 | */ | |
729 | struct stat st; | |
730 | char path[256]; | |
731 | char sg_path[256]; | |
5418499a | 732 | char sg_major_minor[10]; |
90c8b707 DW |
733 | char *c; |
734 | DIR *dir; | |
735 | struct dirent *de; | |
736 | int major, minor, rv; | |
737 | ||
738 | if (fstat(fd, &st)) | |
739 | return -1; | |
740 | ||
741 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
742 | major(st.st_rdev), minor(st.st_rdev)); | |
743 | ||
744 | dir = opendir(path); | |
745 | if (!dir) | |
746 | return -1; | |
747 | ||
748 | de = readdir(dir); | |
749 | while (de) { | |
750 | if (strncmp("scsi_generic:", de->d_name, | |
751 | strlen("scsi_generic:")) == 0) | |
752 | break; | |
753 | de = readdir(dir); | |
754 | } | |
755 | closedir(dir); | |
756 | ||
757 | if (!de) | |
758 | return -1; | |
759 | ||
760 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
761 | fd = open(sg_path, O_RDONLY); | |
762 | if (fd < 0) | |
763 | return fd; | |
764 | ||
765 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
766 | close(fd); | |
5418499a | 767 | if (rv < 0 || rv == sizeof(sg_major_minor)) |
90c8b707 DW |
768 | return -1; |
769 | else | |
770 | sg_major_minor[rv - 1] = '\0'; | |
771 | ||
772 | c = strchr(sg_major_minor, ':'); | |
773 | *c = '\0'; | |
774 | c++; | |
775 | major = strtol(sg_major_minor, NULL, 10); | |
776 | minor = strtol(c, NULL, 10); | |
777 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
778 | (int) getpid(), major, minor); | |
779 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
780 | fd = open(path, O_RDONLY); | |
781 | unlink(path); | |
782 | return fd; | |
783 | } | |
784 | ||
785 | return -1; | |
786 | } | |
755c99fa | 787 | #endif |
90c8b707 | 788 | |
f1665f72 DW |
789 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
790 | { | |
791 | /* from an open block device, try to retrieve it scsi_id */ | |
792 | struct stat st; | |
793 | char path[256]; | |
f1665f72 DW |
794 | DIR *dir; |
795 | struct dirent *de; | |
d8924477 | 796 | int host, bus, target, lun; |
f1665f72 DW |
797 | |
798 | if (fstat(fd, &st)) | |
799 | return 1; | |
800 | ||
fa89bdee | 801 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device", |
f1665f72 DW |
802 | major(st.st_rdev), minor(st.st_rdev)); |
803 | ||
804 | dir = opendir(path); | |
805 | if (!dir) | |
806 | return 1; | |
807 | ||
d8924477 DW |
808 | for (de = readdir(dir); de; de = readdir(dir)) { |
809 | int count; | |
810 | ||
811 | if (de->d_type != DT_DIR) | |
812 | continue; | |
813 | ||
814 | count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun); | |
815 | if (count == 4) | |
f1665f72 | 816 | break; |
f1665f72 DW |
817 | } |
818 | closedir(dir); | |
819 | ||
820 | if (!de) | |
821 | return 1; | |
822 | ||
d8924477 | 823 | *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0); |
f1665f72 DW |
824 | return 0; |
825 | } | |
f94d52f4 | 826 | |
4dd2df09 | 827 | int sysfs_unique_holder(char *devnm, long rdev) |
f94d52f4 | 828 | { |
4dd2df09 | 829 | /* Check that devnm is a holder of rdev, |
f94d52f4 NB |
830 | * and is the only holder. |
831 | * we should be locked against races by | |
4dd2df09 | 832 | * an O_EXCL on devnm |
aab15415 N |
833 | * Return values: |
834 | * 0 - not unique, not even a holder | |
835 | * 1 - unique, this is the only holder. | |
836 | * 2/3 - not unique, there is another holder | |
837 | * -1 - error, cannot find the holders | |
f94d52f4 NB |
838 | */ |
839 | DIR *dir; | |
840 | struct dirent *de; | |
841 | char dirname[100]; | |
842 | char l; | |
aab15415 | 843 | int ret = 0; |
f94d52f4 NB |
844 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", |
845 | major(rdev), minor(rdev)); | |
846 | dir = opendir(dirname); | |
f94d52f4 | 847 | if (!dir) |
aab15415 | 848 | return -1; |
f94d52f4 NB |
849 | l = strlen(dirname); |
850 | while ((de = readdir(dir)) != NULL) { | |
4dd2df09 N |
851 | char buf[100]; |
852 | char *sl; | |
f94d52f4 | 853 | int n; |
f94d52f4 NB |
854 | |
855 | if (de->d_ino == 0) | |
856 | continue; | |
857 | if (de->d_name[0] == '.') | |
858 | continue; | |
859 | strcpy(dirname+l, "/"); | |
860 | strcat(dirname+l, de->d_name); | |
4dd2df09 N |
861 | n = readlink(dirname, buf, sizeof(buf)-1); |
862 | if (n <= 0) | |
93f1df33 | 863 | continue; |
f94d52f4 | 864 | buf[n] = 0; |
4dd2df09 N |
865 | sl = strrchr(buf, '/'); |
866 | if (!sl) | |
aab15415 | 867 | continue; |
4dd2df09 | 868 | sl++; |
f94d52f4 | 869 | |
4dd2df09 | 870 | if (strcmp(devnm, sl) == 0) |
aab15415 N |
871 | ret |= 1; |
872 | else | |
873 | ret |= 2; | |
f94d52f4 NB |
874 | } |
875 | closedir(dir); | |
aab15415 | 876 | return ret; |
f94d52f4 | 877 | } |
38a07ed6 | 878 | |
bc77ed53 DW |
879 | int sysfs_freeze_array(struct mdinfo *sra) |
880 | { | |
881 | /* Try to freeze resync/rebuild on this array/container. | |
882 | * Return -1 if the array is busy, | |
bc77ed53 DW |
883 | * return 0 if this kernel doesn't support 'frozen' |
884 | * return 1 if it worked. | |
885 | */ | |
886 | char buf[20]; | |
887 | ||
888 | if (!sysfs_attribute_available(sra, NULL, "sync_action")) | |
889 | return 1; /* no sync_action == frozen */ | |
890 | if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0) | |
891 | return 0; | |
fd324b08 N |
892 | if (strcmp(buf, "frozen\n") == 0) |
893 | /* Already frozen */ | |
894 | return 0; | |
dea3786a | 895 | if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0) |
bc77ed53 DW |
896 | return -1; |
897 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
898 | return 0; | |
899 | return 1; | |
900 | } | |
efc67e8e N |
901 | |
902 | int sysfs_wait(int fd, int *msec) | |
903 | { | |
904 | /* Wait up to '*msec' for fd to have an exception condition. | |
905 | * if msec == NULL, wait indefinitely. | |
906 | */ | |
907 | fd_set fds; | |
908 | int n; | |
909 | FD_ZERO(&fds); | |
910 | FD_SET(fd, &fds); | |
911 | if (msec == NULL) | |
912 | n = select(fd+1, NULL, NULL, &fds, NULL); | |
913 | else if (*msec < 0) | |
914 | n = 0; | |
915 | else { | |
916 | struct timeval start, end, tv; | |
917 | gettimeofday(&start, NULL); | |
4bffc964 N |
918 | if (*msec < 1000) { |
919 | tv.tv_sec = 0; | |
efc67e8e | 920 | tv.tv_usec = (*msec)*1000; |
4bffc964 | 921 | } else { |
efc67e8e | 922 | tv.tv_sec = (*msec)/1000; |
4bffc964 N |
923 | tv.tv_usec = 0; |
924 | } | |
efc67e8e N |
925 | n = select(fd+1, NULL, NULL, &fds, &tv); |
926 | gettimeofday(&end, NULL); | |
927 | end.tv_sec -= start.tv_sec; | |
928 | *msec -= (end.tv_sec * 1000 + end.tv_usec/1000 | |
4bffc964 | 929 | - start.tv_usec/1000) + 1; |
efc67e8e N |
930 | } |
931 | return n; | |
932 | } |