]>
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 NB |
29 | |
30 | int load_sys(char *path, char *buf) | |
31 | { | |
32 | int fd = open(path, O_RDONLY); | |
33 | int n; | |
34 | if (fd < 0) | |
35 | return -1; | |
36 | n = read(fd, buf, 1024); | |
37 | close(fd); | |
2f6079dc | 38 | if (n <0 || n >= 1024) |
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"); | |
121 | if (load_sys(fname, buf)) | |
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"); | |
140 | if (load_sys(fname, buf)) | |
141 | goto abort; | |
7e0f6979 | 142 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
143 | } |
144 | if (options & GET_LAYOUT) { | |
145 | strcpy(base, "layout"); | |
146 | if (load_sys(fname, buf)) | |
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"); | |
152 | if (load_sys(fname, buf)) | |
153 | goto abort; | |
154 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
f1d26766 DW |
155 | } |
156 | if (options & GET_DEGRADED) { | |
157 | strcpy(base, "degraded"); | |
158 | if (load_sys(fname, buf)) | |
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"); | |
164 | if (load_sys(fname, buf)) | |
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"); | |
172 | if (load_sys(fname, buf)) | |
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"); | |
178 | if (load_sys(fname, 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"); | |
186 | if (load_sys(fname, buf)) | |
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"); | |
198 | if (load_sys(fname, buf)) | |
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"); | |
221 | if (load_sys(fname, buf)) | |
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"); | |
235 | if (load_sys(fname, sra->sysfs_array_state)) | |
236 | goto abort; | |
237 | } else | |
238 | sra->sysfs_array_state[0] = 0; | |
239 | ||
e86c9dd6 NB |
240 | if (! (options & GET_DEVS)) |
241 | return sra; | |
242 | ||
243 | /* Get all the devices as well */ | |
244 | *base = 0; | |
245 | dir = opendir(fname); | |
246 | if (!dir) | |
247 | goto abort; | |
7e0f6979 | 248 | sra->array.spare_disks = 0; |
e86c9dd6 | 249 | |
64e103fe N |
250 | devp = &sra->devs; |
251 | sra->devs = NULL; | |
e86c9dd6 NB |
252 | while ((de = readdir(dir)) != NULL) { |
253 | char *ep; | |
254 | if (de->d_ino == 0 || | |
255 | strncmp(de->d_name, "dev-", 4) != 0) | |
256 | continue; | |
257 | strcpy(base, de->d_name); | |
258 | dbase = base + strlen(base); | |
259 | *dbase++ = '/'; | |
260 | ||
503975b9 | 261 | dev = xmalloc(sizeof(*dev)); |
e86c9dd6 NB |
262 | |
263 | /* Always get slot, major, minor */ | |
264 | strcpy(dbase, "slot"); | |
4795982e DW |
265 | if (load_sys(fname, buf)) { |
266 | /* hmm... unable to read 'slot' maybe the device | |
267 | * is going away? | |
268 | */ | |
269 | strcpy(dbase, "block"); | |
270 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
271 | errno != ENAMETOOLONG) { | |
272 | /* ...yup device is gone */ | |
273 | free(dev); | |
274 | continue; | |
275 | } else { | |
276 | /* slot is unreadable but 'block' link | |
277 | * still intact... something bad is happening | |
278 | * so abort | |
279 | */ | |
280 | free(dev); | |
281 | goto abort; | |
282 | } | |
64e103fe | 283 | |
4795982e | 284 | } |
4795982e | 285 | strcpy(dev->sys_name, de->d_name); |
06c7f68e NB |
286 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
287 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
288 | |
289 | strcpy(dbase, "block/dev"); | |
dab4a513 | 290 | if (load_sys(fname, buf)) { |
b526e52d DW |
291 | /* assume this is a stale reference to a hot |
292 | * removed device | |
293 | */ | |
dab4a513 | 294 | free(dev); |
b526e52d | 295 | continue; |
dab4a513 | 296 | } |
e6fc80a8 | 297 | sra->array.nr_disks++; |
06c7f68e | 298 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 | 299 | |
dab4a513 | 300 | /* special case check for block devices that can go 'offline' */ |
b526e52d DW |
301 | strcpy(dbase, "block/device/state"); |
302 | if (load_sys(fname, buf) == 0 && | |
303 | strncmp(buf, "offline", 7) == 0) { | |
304 | free(dev); | |
305 | continue; | |
dab4a513 DW |
306 | } |
307 | ||
308 | /* finally add this disk to the array */ | |
64e103fe N |
309 | *devp = dev; |
310 | devp = & dev->next; | |
311 | dev->next = NULL; | |
dab4a513 | 312 | |
e86c9dd6 NB |
313 | if (options & GET_OFFSET) { |
314 | strcpy(dbase, "offset"); | |
315 | if (load_sys(fname, buf)) | |
316 | goto abort; | |
06c7f68e | 317 | dev->data_offset = strtoull(buf, NULL, 0); |
fe384ca0 N |
318 | strcpy(dbase, "new_offset"); |
319 | if (load_sys(fname, buf) == 0) | |
320 | dev->new_data_offset = strtoull(buf, NULL, 0); | |
321 | else | |
322 | dev->new_data_offset = dev->data_offset; | |
e86c9dd6 NB |
323 | } |
324 | if (options & GET_SIZE) { | |
325 | strcpy(dbase, "size"); | |
326 | if (load_sys(fname, buf)) | |
327 | goto abort; | |
047d2e49 | 328 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
329 | } |
330 | if (options & GET_STATE) { | |
06c7f68e | 331 | dev->disk.state = 0; |
e86c9dd6 NB |
332 | strcpy(dbase, "state"); |
333 | if (load_sys(fname, buf)) | |
334 | goto abort; | |
335 | if (strstr(buf, "in_sync")) | |
06c7f68e | 336 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 337 | if (strstr(buf, "faulty")) |
06c7f68e NB |
338 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
339 | if (dev->disk.state == 0) | |
7e0f6979 | 340 | sra->array.spare_disks++; |
e86c9dd6 NB |
341 | } |
342 | if (options & GET_ERROR) { | |
343 | strcpy(buf, "errors"); | |
344 | if (load_sys(fname, buf)) | |
345 | goto abort; | |
346 | dev->errors = strtoul(buf, NULL, 0); | |
347 | } | |
348 | } | |
355726fa | 349 | closedir(dir); |
e86c9dd6 NB |
350 | return sra; |
351 | ||
352 | abort: | |
355726fa NB |
353 | if (dir) |
354 | closedir(dir); | |
8382f19b | 355 | sysfs_free(sra); |
e86c9dd6 NB |
356 | return NULL; |
357 | } | |
358 | ||
1770662b DW |
359 | int sysfs_attr_match(const char *attr, const char *str) |
360 | { | |
361 | /* See if attr, read from a sysfs file, matches | |
362 | * str. They must either be the same, or attr can | |
363 | * have a trailing newline or comma | |
364 | */ | |
365 | while (*attr && *str && *attr == *str) { | |
366 | attr++; | |
367 | str++; | |
368 | } | |
369 | ||
370 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
371 | return 0; | |
372 | return 1; | |
373 | } | |
374 | ||
375 | int sysfs_match_word(const char *word, char **list) | |
376 | { | |
377 | int n; | |
378 | for (n=0; list[n]; n++) | |
379 | if (sysfs_attr_match(word, list[n])) | |
380 | break; | |
381 | return n; | |
382 | } | |
383 | ||
e86c9dd6 NB |
384 | unsigned long long get_component_size(int fd) |
385 | { | |
386 | /* Find out the component size of the array. | |
387 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
388 | * size field is only 32bits. | |
389 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 390 | * |
8686f3ed | 391 | * This returns in units of sectors. |
e86c9dd6 NB |
392 | */ |
393 | struct stat stb; | |
394 | char fname[50]; | |
395 | int n; | |
396 | if (fstat(fd, &stb)) return 0; | |
f21e18ca | 397 | if (major(stb.st_rdev) != (unsigned)get_mdp_major()) |
e86c9dd6 | 398 | sprintf(fname, "/sys/block/md%d/md/component_size", |
ea24acd0 | 399 | (int)minor(stb.st_rdev)); |
e86c9dd6 NB |
400 | else |
401 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
ea24acd0 | 402 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
403 | fd = open(fname, O_RDONLY); |
404 | if (fd < 0) | |
405 | return 0; | |
406 | n = read(fd, fname, sizeof(fname)); | |
407 | close(fd); | |
99f6e521 | 408 | if (n < 0 || n == sizeof(fname)) |
e86c9dd6 NB |
409 | return 0; |
410 | fname[n] = 0; | |
8686f3ed | 411 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
412 | } |
413 | ||
7e0f6979 | 414 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
415 | char *name, char *val) |
416 | { | |
417 | char fname[50]; | |
f21e18ca | 418 | unsigned int n; |
e86c9dd6 | 419 | int fd; |
7e1432fb | 420 | |
e86c9dd6 | 421 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 422 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
423 | fd = open(fname, O_WRONLY); |
424 | if (fd < 0) | |
425 | return -1; | |
426 | n = write(fd, val, strlen(val)); | |
427 | close(fd); | |
2a24d7b6 | 428 | if (n != strlen(val)) { |
1ade5cc1 N |
429 | dprintf("failed to write '%s' to '%s' (%s)\n", |
430 | val, fname, strerror(errno)); | |
e86c9dd6 | 431 | return -1; |
2a24d7b6 | 432 | } |
e86c9dd6 NB |
433 | return 0; |
434 | } | |
435 | ||
7e0f6979 | 436 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
437 | char *name, unsigned long long val) |
438 | { | |
439 | char valstr[50]; | |
440 | sprintf(valstr, "%llu", val); | |
441 | return sysfs_set_str(sra, dev, name, valstr); | |
442 | } | |
443 | ||
012a8641 JS |
444 | int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev, |
445 | char *name, long long val) | |
446 | { | |
447 | char valstr[50]; | |
448 | sprintf(valstr, "%lli", val); | |
449 | return sysfs_set_str(sra, dev, name, valstr); | |
450 | } | |
451 | ||
97590376 N |
452 | int sysfs_uevent(struct mdinfo *sra, char *event) |
453 | { | |
454 | char fname[50]; | |
455 | int n; | |
456 | int fd; | |
457 | ||
458 | sprintf(fname, "/sys/block/%s/uevent", | |
459 | sra->sys_name); | |
460 | fd = open(fname, O_WRONLY); | |
461 | if (fd < 0) | |
462 | return -1; | |
463 | n = write(fd, event, strlen(event)); | |
464 | close(fd); | |
e4c72d1d | 465 | if (n != (int)strlen(event)) { |
1ade5cc1 N |
466 | dprintf("failed to write '%s' to '%s' (%s)\n", |
467 | event, fname, strerror(errno)); | |
e4c72d1d LB |
468 | return -1; |
469 | } | |
97590376 | 470 | return 0; |
1011e834 | 471 | } |
97590376 | 472 | |
bc77ed53 DW |
473 | int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name) |
474 | { | |
475 | char fname[50]; | |
476 | struct stat st; | |
477 | ||
478 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
479 | sra->sys_name, dev?dev->sys_name:"", name); | |
480 | ||
481 | return stat(fname, &st) == 0; | |
482 | } | |
483 | ||
a6288483 N |
484 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, |
485 | char *name) | |
e86c9dd6 NB |
486 | { |
487 | char fname[50]; | |
e86c9dd6 | 488 | int fd; |
a6288483 | 489 | |
e86c9dd6 | 490 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 491 | sra->sys_name, dev?dev->sys_name:"", name); |
a6288483 | 492 | fd = open(fname, O_RDWR); |
e86c9dd6 | 493 | if (fd < 0) |
a6288483 N |
494 | fd = open(fname, O_RDONLY); |
495 | return fd; | |
496 | } | |
497 | ||
498 | int sysfs_fd_get_ll(int fd, unsigned long long *val) | |
499 | { | |
500 | char buf[50]; | |
501 | int n; | |
502 | char *ep; | |
503 | ||
504 | lseek(fd, 0, 0); | |
e86c9dd6 | 505 | n = read(fd, buf, sizeof(buf)); |
5418499a | 506 | if (n <= 0 || n == sizeof(buf)) |
6560987b | 507 | return -2; |
e86c9dd6 NB |
508 | buf[n] = 0; |
509 | *val = strtoull(buf, &ep, 0); | |
510 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
511 | return -1; | |
512 | return 0; | |
513 | } | |
2503d23b | 514 | |
a6288483 N |
515 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
516 | char *name, unsigned long long *val) | |
517 | { | |
518 | int n; | |
519 | int fd; | |
520 | ||
521 | fd = sysfs_get_fd(sra, dev, name); | |
522 | if (fd < 0) | |
523 | return -1; | |
524 | n = sysfs_fd_get_ll(fd, val); | |
525 | close(fd); | |
526 | return n; | |
527 | } | |
528 | ||
2eba8496 N |
529 | int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2) |
530 | { | |
531 | /* two numbers in this sysfs file, either | |
532 | * NNN (NNN) | |
533 | * or | |
534 | * NNN / NNN | |
535 | */ | |
536 | char buf[80]; | |
537 | int n; | |
538 | char *ep, *ep2; | |
539 | ||
540 | lseek(fd, 0, 0); | |
541 | n = read(fd, buf, sizeof(buf)); | |
5418499a | 542 | if (n <= 0 || n == sizeof(buf)) |
2eba8496 N |
543 | return -2; |
544 | buf[n] = 0; | |
545 | *v1 = strtoull(buf, &ep, 0); | |
546 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
547 | return -1; | |
548 | while (*ep == ' ' || *ep == '/' || *ep == '(') | |
549 | ep++; | |
550 | *v2 = strtoull(ep, &ep2, 0); | |
551 | if (ep2 == ep || (*ep2 != 0 && *ep2 != '\n' && *ep2 != ' ' && *ep2 != ')')) { | |
552 | *v2 = *v1; | |
553 | return 1; | |
554 | } | |
555 | return 2; | |
556 | } | |
557 | ||
558 | int sysfs_get_two(struct mdinfo *sra, struct mdinfo *dev, | |
559 | char *name, unsigned long long *v1, unsigned long long *v2) | |
560 | { | |
561 | int n; | |
562 | int fd; | |
563 | ||
564 | fd = sysfs_get_fd(sra, dev, name); | |
565 | if (fd < 0) | |
566 | return -1; | |
567 | n = sysfs_fd_get_two(fd, v1, v2); | |
568 | close(fd); | |
569 | return n; | |
570 | } | |
571 | ||
7236ee7a N |
572 | int sysfs_fd_get_str(int fd, char *val, int size) |
573 | { | |
574 | int n; | |
575 | ||
576 | lseek(fd, 0, 0); | |
577 | n = read(fd, val, size); | |
5418499a | 578 | if (n <= 0 || n == size) |
7236ee7a N |
579 | return -1; |
580 | val[n] = 0; | |
581 | return n; | |
582 | } | |
583 | ||
93ecfa01 N |
584 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, |
585 | char *name, char *val, int size) | |
586 | { | |
93ecfa01 N |
587 | int n; |
588 | int fd; | |
7236ee7a N |
589 | |
590 | fd = sysfs_get_fd(sra, dev, name); | |
93ecfa01 N |
591 | if (fd < 0) |
592 | return -1; | |
7236ee7a | 593 | n = sysfs_fd_get_str(fd, val, size); |
93ecfa01 | 594 | close(fd); |
93ecfa01 N |
595 | return n; |
596 | } | |
597 | ||
8ed3e5e1 DW |
598 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) |
599 | { | |
600 | unsigned long sec; | |
601 | unsigned long msec; | |
602 | char delay[30]; | |
603 | ||
604 | sec = ms / 1000; | |
0dd3ba30 | 605 | msec = ms % 1000; |
8ed3e5e1 | 606 | |
0dd3ba30 DW |
607 | sprintf(delay, "%ld.%03ld\n", sec, msec); |
608 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
8ed3e5e1 DW |
609 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); |
610 | } | |
611 | ||
f35f2525 | 612 | int sysfs_set_array(struct mdinfo *info, int vers) |
2503d23b NB |
613 | { |
614 | int rv = 0; | |
f35f2525 | 615 | char ver[100]; |
a5062b1c | 616 | int raid_disks = info->array.raid_disks; |
f35f2525 N |
617 | |
618 | ver[0] = 0; | |
619 | if (info->array.major_version == -1 && | |
620 | info->array.minor_version == -2) { | |
ddb12f6c AK |
621 | char buf[1024]; |
622 | ||
f35f2525 N |
623 | strcat(strcpy(ver, "external:"), info->text_version); |
624 | ||
ddb12f6c AK |
625 | /* meta version might already be set if we are setting |
626 | * new geometry for a reshape. In that case we don't | |
627 | * want to over-write the 'readonly' flag that is | |
628 | * stored in the metadata version. So read the current | |
629 | * version first, and preserve the flag | |
630 | */ | |
631 | if (sysfs_get_str(info, NULL, "metadata_version", | |
632 | buf, 1024) > 0) | |
633 | if (strlen(buf) >= 9 && buf[9] == '-') | |
634 | ver[9] = '-'; | |
635 | ||
f35f2525 N |
636 | if ((vers % 100) < 2 || |
637 | sysfs_set_str(info, NULL, "metadata_version", | |
638 | ver) < 0) { | |
7a862a02 | 639 | pr_err("This kernel does not support external metadata.\n"); |
f35f2525 N |
640 | return 1; |
641 | } | |
642 | } | |
2503d23b NB |
643 | if (info->array.level < 0) |
644 | return 0; /* FIXME */ | |
f35f2525 | 645 | rv |= sysfs_set_str(info, NULL, "level", |
2503d23b | 646 | map_num(pers, info->array.level)); |
a5062b1c AK |
647 | if (info->reshape_active && info->delta_disks != UnSet) |
648 | raid_disks -= info->delta_disks; | |
649 | rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks); | |
f35f2525 N |
650 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); |
651 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
652 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
da9b4a62 DW |
653 | if (info->custom_array_size) { |
654 | int rc; | |
655 | ||
656 | rc = sysfs_set_num(info, NULL, "array_size", | |
657 | info->custom_array_size/2); | |
658 | if (rc && errno == ENOENT) { | |
7a862a02 | 659 | pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n"); |
da9b4a62 DW |
660 | rc = 0; |
661 | } | |
662 | rv |= rc; | |
663 | } | |
664 | ||
f35f2525 N |
665 | if (info->array.level > 0) |
666 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
f897078e N |
667 | |
668 | if (info->reshape_active) { | |
669 | rv |= sysfs_set_num(info, NULL, "reshape_position", | |
670 | info->reshape_progress); | |
671 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk); | |
20a40eca N |
672 | rv |= sysfs_set_num(info, NULL, "layout", info->new_layout); |
673 | rv |= sysfs_set_num(info, NULL, "raid_disks", | |
a5062b1c | 674 | info->array.raid_disks); |
20a40eca N |
675 | /* We don't set 'new_level' here. That can only happen |
676 | * once the reshape completes. | |
f897078e N |
677 | */ |
678 | } | |
2503d23b NB |
679 | return rv; |
680 | } | |
681 | ||
2904b26f | 682 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) |
2503d23b | 683 | { |
33a6535d AW |
684 | char dv[PATH_MAX]; |
685 | char nm[PATH_MAX]; | |
2503d23b NB |
686 | char *dname; |
687 | int rv; | |
688 | ||
689 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
690 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
691 | if (rv) | |
692 | return rv; | |
693 | ||
694 | memset(nm, 0, sizeof(nm)); | |
bc17158d | 695 | dname = devid2kname(makedev(sd->disk.major, sd->disk.minor)); |
2503d23b NB |
696 | strcpy(sd->sys_name, "dev-"); |
697 | strcpy(sd->sys_name+4, dname); | |
698 | ||
2904b26f DW |
699 | /* test write to see if 'recovery_start' is available */ |
700 | if (resume && sd->recovery_start < MaxSector && | |
701 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
702 | sysfs_set_str(sra, sd, "state", "remove"); | |
703 | return -1; | |
704 | } | |
705 | ||
73649188 | 706 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
707 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
708 | if (sra->array.level != LEVEL_CONTAINER) { | |
d23534e4 | 709 | if (sd->recovery_start == MaxSector) |
462906cd N |
710 | /* This can correctly fail if array isn't started, |
711 | * yet, so just ignore status for now. | |
712 | */ | |
d23534e4 | 713 | sysfs_set_str(sra, sd, "state", "insync"); |
899aead0 AK |
714 | if (sd->disk.raid_disk >= 0) |
715 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
2904b26f DW |
716 | if (resume) |
717 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
2503d23b | 718 | } |
2503d23b NB |
719 | return rv; |
720 | } | |
90c8b707 | 721 | |
755c99fa | 722 | #if 0 |
90c8b707 DW |
723 | int sysfs_disk_to_sg(int fd) |
724 | { | |
725 | /* from an open block device, try find and open its corresponding | |
726 | * scsi_generic interface | |
727 | */ | |
728 | struct stat st; | |
729 | char path[256]; | |
730 | char sg_path[256]; | |
5418499a | 731 | char sg_major_minor[10]; |
90c8b707 DW |
732 | char *c; |
733 | DIR *dir; | |
734 | struct dirent *de; | |
735 | int major, minor, rv; | |
736 | ||
737 | if (fstat(fd, &st)) | |
738 | return -1; | |
739 | ||
740 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
741 | major(st.st_rdev), minor(st.st_rdev)); | |
742 | ||
743 | dir = opendir(path); | |
744 | if (!dir) | |
745 | return -1; | |
746 | ||
747 | de = readdir(dir); | |
748 | while (de) { | |
749 | if (strncmp("scsi_generic:", de->d_name, | |
750 | strlen("scsi_generic:")) == 0) | |
751 | break; | |
752 | de = readdir(dir); | |
753 | } | |
754 | closedir(dir); | |
755 | ||
756 | if (!de) | |
757 | return -1; | |
758 | ||
759 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
760 | fd = open(sg_path, O_RDONLY); | |
761 | if (fd < 0) | |
762 | return fd; | |
763 | ||
764 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
765 | close(fd); | |
5418499a | 766 | if (rv < 0 || rv == sizeof(sg_major_minor)) |
90c8b707 DW |
767 | return -1; |
768 | else | |
769 | sg_major_minor[rv - 1] = '\0'; | |
770 | ||
771 | c = strchr(sg_major_minor, ':'); | |
772 | *c = '\0'; | |
773 | c++; | |
774 | major = strtol(sg_major_minor, NULL, 10); | |
775 | minor = strtol(c, NULL, 10); | |
776 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
777 | (int) getpid(), major, minor); | |
778 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
779 | fd = open(path, O_RDONLY); | |
780 | unlink(path); | |
781 | return fd; | |
782 | } | |
783 | ||
784 | return -1; | |
785 | } | |
755c99fa | 786 | #endif |
90c8b707 | 787 | |
f1665f72 DW |
788 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
789 | { | |
790 | /* from an open block device, try to retrieve it scsi_id */ | |
791 | struct stat st; | |
792 | char path[256]; | |
f1665f72 DW |
793 | DIR *dir; |
794 | struct dirent *de; | |
d8924477 | 795 | int host, bus, target, lun; |
f1665f72 DW |
796 | |
797 | if (fstat(fd, &st)) | |
798 | return 1; | |
799 | ||
fa89bdee | 800 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device", |
f1665f72 DW |
801 | major(st.st_rdev), minor(st.st_rdev)); |
802 | ||
803 | dir = opendir(path); | |
804 | if (!dir) | |
805 | return 1; | |
806 | ||
d8924477 DW |
807 | for (de = readdir(dir); de; de = readdir(dir)) { |
808 | int count; | |
809 | ||
810 | if (de->d_type != DT_DIR) | |
811 | continue; | |
812 | ||
813 | count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun); | |
814 | if (count == 4) | |
f1665f72 | 815 | break; |
f1665f72 DW |
816 | } |
817 | closedir(dir); | |
818 | ||
819 | if (!de) | |
820 | return 1; | |
821 | ||
d8924477 | 822 | *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0); |
f1665f72 DW |
823 | return 0; |
824 | } | |
f94d52f4 | 825 | |
4dd2df09 | 826 | int sysfs_unique_holder(char *devnm, long rdev) |
f94d52f4 | 827 | { |
4dd2df09 | 828 | /* Check that devnm is a holder of rdev, |
f94d52f4 NB |
829 | * and is the only holder. |
830 | * we should be locked against races by | |
4dd2df09 | 831 | * an O_EXCL on devnm |
aab15415 N |
832 | * Return values: |
833 | * 0 - not unique, not even a holder | |
834 | * 1 - unique, this is the only holder. | |
835 | * 2/3 - not unique, there is another holder | |
836 | * -1 - error, cannot find the holders | |
f94d52f4 NB |
837 | */ |
838 | DIR *dir; | |
839 | struct dirent *de; | |
840 | char dirname[100]; | |
841 | char l; | |
aab15415 | 842 | int ret = 0; |
f94d52f4 NB |
843 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", |
844 | major(rdev), minor(rdev)); | |
845 | dir = opendir(dirname); | |
f94d52f4 | 846 | if (!dir) |
aab15415 | 847 | return -1; |
f94d52f4 NB |
848 | l = strlen(dirname); |
849 | while ((de = readdir(dir)) != NULL) { | |
4dd2df09 N |
850 | char buf[100]; |
851 | char *sl; | |
f94d52f4 | 852 | int n; |
f94d52f4 NB |
853 | |
854 | if (de->d_ino == 0) | |
855 | continue; | |
856 | if (de->d_name[0] == '.') | |
857 | continue; | |
858 | strcpy(dirname+l, "/"); | |
859 | strcat(dirname+l, de->d_name); | |
4dd2df09 N |
860 | n = readlink(dirname, buf, sizeof(buf)-1); |
861 | if (n <= 0) | |
93f1df33 | 862 | continue; |
f94d52f4 | 863 | buf[n] = 0; |
4dd2df09 N |
864 | sl = strrchr(buf, '/'); |
865 | if (!sl) | |
aab15415 | 866 | continue; |
4dd2df09 | 867 | sl++; |
f94d52f4 | 868 | |
4dd2df09 | 869 | if (strcmp(devnm, sl) == 0) |
aab15415 N |
870 | ret |= 1; |
871 | else | |
872 | ret |= 2; | |
f94d52f4 NB |
873 | } |
874 | closedir(dir); | |
aab15415 | 875 | return ret; |
f94d52f4 | 876 | } |
38a07ed6 | 877 | |
bc77ed53 DW |
878 | int sysfs_freeze_array(struct mdinfo *sra) |
879 | { | |
880 | /* Try to freeze resync/rebuild on this array/container. | |
881 | * Return -1 if the array is busy, | |
bc77ed53 DW |
882 | * return 0 if this kernel doesn't support 'frozen' |
883 | * return 1 if it worked. | |
884 | */ | |
885 | char buf[20]; | |
886 | ||
887 | if (!sysfs_attribute_available(sra, NULL, "sync_action")) | |
888 | return 1; /* no sync_action == frozen */ | |
889 | if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0) | |
890 | return 0; | |
fd324b08 N |
891 | if (strcmp(buf, "frozen\n") == 0) |
892 | /* Already frozen */ | |
893 | return 0; | |
dea3786a | 894 | if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0) |
bc77ed53 DW |
895 | return -1; |
896 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
897 | return 0; | |
898 | return 1; | |
899 | } | |
efc67e8e N |
900 | |
901 | int sysfs_wait(int fd, int *msec) | |
902 | { | |
903 | /* Wait up to '*msec' for fd to have an exception condition. | |
904 | * if msec == NULL, wait indefinitely. | |
905 | */ | |
906 | fd_set fds; | |
907 | int n; | |
908 | FD_ZERO(&fds); | |
909 | FD_SET(fd, &fds); | |
910 | if (msec == NULL) | |
911 | n = select(fd+1, NULL, NULL, &fds, NULL); | |
912 | else if (*msec < 0) | |
913 | n = 0; | |
914 | else { | |
915 | struct timeval start, end, tv; | |
916 | gettimeofday(&start, NULL); | |
4bffc964 N |
917 | if (*msec < 1000) { |
918 | tv.tv_sec = 0; | |
efc67e8e | 919 | tv.tv_usec = (*msec)*1000; |
4bffc964 | 920 | } else { |
efc67e8e | 921 | tv.tv_sec = (*msec)/1000; |
4bffc964 N |
922 | tv.tv_usec = 0; |
923 | } | |
efc67e8e N |
924 | n = select(fd+1, NULL, NULL, &fds, &tv); |
925 | gettimeofday(&end, NULL); | |
926 | end.tv_sec -= start.tv_sec; | |
927 | *msec -= (end.tv_sec * 1000 + end.tv_usec/1000 | |
4bffc964 | 928 | - start.tv_usec/1000) + 1; |
efc67e8e N |
929 | } |
930 | return n; | |
931 | } |