]>
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 | ||
549e9569 NB |
60 | int sysfs_open(int devnum, char *devname, char *attr) |
61 | { | |
62 | char fname[50]; | |
549e9569 | 63 | int fd; |
1e4bc070 | 64 | char *mdname = devnum2devname(devnum); |
549e9569 | 65 | |
1e4bc070 DW |
66 | if (!mdname) |
67 | return -1; | |
68 | ||
69 | sprintf(fname, "/sys/block/%s/md/", mdname); | |
549e9569 NB |
70 | if (devname) { |
71 | strcat(fname, devname); | |
72 | strcat(fname, "/"); | |
73 | } | |
74 | strcat(fname, attr); | |
75 | fd = open(fname, O_RDWR); | |
ea6d09b0 | 76 | if (fd < 0 && errno == EACCES) |
549e9569 | 77 | fd = open(fname, O_RDONLY); |
1e4bc070 | 78 | free(mdname); |
549e9569 NB |
79 | return fd; |
80 | } | |
81 | ||
f35f2525 N |
82 | void sysfs_init(struct mdinfo *mdi, int fd, int devnum) |
83 | { | |
678a4a36 | 84 | mdi->sys_name[0] = 0; |
f35f2525 | 85 | if (fd >= 0) { |
f35f2525 | 86 | mdu_version_t vers; |
f35f2525 N |
87 | if (ioctl(fd, RAID_VERSION, &vers) != 0) |
88 | return; | |
d7ab966b | 89 | devnum = fd2devnum(fd); |
f35f2525 | 90 | } |
d7ab966b N |
91 | if (devnum == NoMdDev) |
92 | return; | |
93 | if (devnum >= 0) | |
94 | sprintf(mdi->sys_name, "md%d", devnum); | |
95 | else | |
96 | sprintf(mdi->sys_name, "md_d%d", | |
97 | -1-devnum); | |
f35f2525 N |
98 | } |
99 | ||
d7ab966b | 100 | |
7e0f6979 | 101 | struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options) |
e86c9dd6 | 102 | { |
33a6535d AW |
103 | char fname[PATH_MAX]; |
104 | char buf[PATH_MAX]; | |
e86c9dd6 NB |
105 | char *base; |
106 | char *dbase; | |
7e0f6979 | 107 | struct mdinfo *sra; |
06c7f68e | 108 | struct mdinfo *dev; |
355726fa | 109 | DIR *dir = NULL; |
e86c9dd6 NB |
110 | struct dirent *de; |
111 | ||
112 | sra = malloc(sizeof(*sra)); | |
113 | if (sra == NULL) | |
114 | return sra; | |
f35f2525 N |
115 | memset(sra, 0, sizeof(*sra)); |
116 | sysfs_init(sra, fd, devnum); | |
678a4a36 N |
117 | if (sra->sys_name[0] == 0) { |
118 | free(sra); | |
119 | return NULL; | |
120 | } | |
e86c9dd6 | 121 | |
7e0f6979 | 122 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
123 | base = fname + strlen(fname); |
124 | ||
125 | sra->devs = NULL; | |
8382f19b NB |
126 | if (options & GET_VERSION) { |
127 | strcpy(base, "metadata_version"); | |
128 | if (load_sys(fname, buf)) | |
129 | goto abort; | |
294d6f45 | 130 | if (strncmp(buf, "none", 4) == 0) { |
7e0f6979 NB |
131 | sra->array.major_version = |
132 | sra->array.minor_version = -1; | |
294d6f45 NB |
133 | strcpy(sra->text_version, ""); |
134 | } else if (strncmp(buf, "external:", 9) == 0) { | |
142cb9e1 NB |
135 | sra->array.major_version = -1; |
136 | sra->array.minor_version = -2; | |
137 | strcpy(sra->text_version, buf+9); | |
b8ac1967 | 138 | } else { |
8382f19b | 139 | sscanf(buf, "%d.%d", |
7e0f6979 NB |
140 | &sra->array.major_version, |
141 | &sra->array.minor_version); | |
b8ac1967 NB |
142 | strcpy(sra->text_version, buf); |
143 | } | |
8382f19b | 144 | } |
e86c9dd6 NB |
145 | if (options & GET_LEVEL) { |
146 | strcpy(base, "level"); | |
147 | if (load_sys(fname, buf)) | |
148 | goto abort; | |
7e0f6979 | 149 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
150 | } |
151 | if (options & GET_LAYOUT) { | |
152 | strcpy(base, "layout"); | |
153 | if (load_sys(fname, buf)) | |
154 | goto abort; | |
7e0f6979 | 155 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 | 156 | } |
549e9569 NB |
157 | if (options & GET_DISKS) { |
158 | strcpy(base, "raid_disks"); | |
159 | if (load_sys(fname, buf)) | |
160 | goto abort; | |
161 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
f1d26766 DW |
162 | } |
163 | if (options & GET_DEGRADED) { | |
164 | strcpy(base, "degraded"); | |
165 | if (load_sys(fname, buf)) | |
166 | goto abort; | |
167 | sra->array.failed_disks = strtoul(buf, NULL, 0); | |
549e9569 | 168 | } |
e86c9dd6 NB |
169 | if (options & GET_COMPONENT) { |
170 | strcpy(base, "component_size"); | |
171 | if (load_sys(fname, buf)) | |
172 | goto abort; | |
173 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
174 | /* sysfs reports "K", but we want sectors */ |
175 | sra->component_size *= 2; | |
e86c9dd6 NB |
176 | } |
177 | if (options & GET_CHUNK) { | |
178 | strcpy(base, "chunk_size"); | |
179 | if (load_sys(fname, buf)) | |
180 | goto abort; | |
7e0f6979 | 181 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 182 | } |
758d3a8e NB |
183 | if (options & GET_CACHE) { |
184 | strcpy(base, "stripe_cache_size"); | |
185 | if (load_sys(fname, buf)) | |
186 | goto abort; | |
187 | sra->cache_size = strtoul(buf, NULL, 0); | |
188 | } | |
37dfc3d6 NB |
189 | if (options & GET_MISMATCH) { |
190 | strcpy(base, "mismatch_cnt"); | |
191 | if (load_sys(fname, buf)) | |
192 | goto abort; | |
193 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
194 | } | |
1770662b DW |
195 | if (options & GET_SAFEMODE) { |
196 | int scale = 1; | |
197 | int dot = 0; | |
198 | int i; | |
199 | unsigned long msec; | |
200 | size_t len; | |
201 | ||
202 | strcpy(base, "safe_mode_delay"); | |
203 | if (load_sys(fname, buf)) | |
204 | goto abort; | |
205 | ||
206 | /* remove a period, and count digits after it */ | |
207 | len = strlen(buf); | |
208 | for (i = 0; i < len; i++) { | |
209 | if (dot) { | |
210 | if (isdigit(buf[i])) { | |
211 | buf[i-1] = buf[i]; | |
212 | scale *= 10; | |
213 | } | |
214 | buf[i] = 0; | |
215 | } else if (buf[i] == '.') { | |
216 | dot=1; | |
217 | buf[i] = 0; | |
218 | } | |
219 | } | |
220 | msec = strtoul(buf, NULL, 10); | |
221 | msec = (msec * 1000) / scale; | |
222 | sra->safe_mode_delay = msec; | |
223 | } | |
e86c9dd6 NB |
224 | |
225 | if (! (options & GET_DEVS)) | |
226 | return sra; | |
227 | ||
228 | /* Get all the devices as well */ | |
229 | *base = 0; | |
230 | dir = opendir(fname); | |
231 | if (!dir) | |
232 | goto abort; | |
7e0f6979 | 233 | sra->array.spare_disks = 0; |
e86c9dd6 NB |
234 | |
235 | while ((de = readdir(dir)) != NULL) { | |
236 | char *ep; | |
237 | if (de->d_ino == 0 || | |
238 | strncmp(de->d_name, "dev-", 4) != 0) | |
239 | continue; | |
240 | strcpy(base, de->d_name); | |
241 | dbase = base + strlen(base); | |
242 | *dbase++ = '/'; | |
243 | ||
244 | dev = malloc(sizeof(*dev)); | |
245 | if (!dev) | |
246 | goto abort; | |
e86c9dd6 NB |
247 | |
248 | /* Always get slot, major, minor */ | |
249 | strcpy(dbase, "slot"); | |
4795982e DW |
250 | if (load_sys(fname, buf)) { |
251 | /* hmm... unable to read 'slot' maybe the device | |
252 | * is going away? | |
253 | */ | |
254 | strcpy(dbase, "block"); | |
255 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
256 | errno != ENAMETOOLONG) { | |
257 | /* ...yup device is gone */ | |
258 | free(dev); | |
259 | continue; | |
260 | } else { | |
261 | /* slot is unreadable but 'block' link | |
262 | * still intact... something bad is happening | |
263 | * so abort | |
264 | */ | |
265 | free(dev); | |
266 | goto abort; | |
267 | } | |
268 | ||
269 | } | |
4795982e | 270 | strcpy(dev->sys_name, de->d_name); |
06c7f68e NB |
271 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
272 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
273 | |
274 | strcpy(dbase, "block/dev"); | |
dab4a513 DW |
275 | if (load_sys(fname, buf)) { |
276 | free(dev); | |
277 | if (options & SKIP_GONE_DEVS) | |
278 | continue; | |
279 | else | |
280 | goto abort; | |
281 | } | |
06c7f68e | 282 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 | 283 | |
dab4a513 DW |
284 | /* special case check for block devices that can go 'offline' */ |
285 | if (options & SKIP_GONE_DEVS) { | |
286 | strcpy(dbase, "block/device/state"); | |
287 | if (load_sys(fname, buf) == 0 && | |
288 | strncmp(buf, "offline", 7) == 0) { | |
289 | free(dev); | |
290 | continue; | |
291 | } | |
292 | } | |
293 | ||
294 | /* finally add this disk to the array */ | |
295 | dev->next = sra->devs; | |
296 | sra->devs = dev; | |
297 | ||
e86c9dd6 NB |
298 | if (options & GET_OFFSET) { |
299 | strcpy(dbase, "offset"); | |
300 | if (load_sys(fname, buf)) | |
301 | goto abort; | |
06c7f68e | 302 | dev->data_offset = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
303 | } |
304 | if (options & GET_SIZE) { | |
305 | strcpy(dbase, "size"); | |
306 | if (load_sys(fname, buf)) | |
307 | goto abort; | |
047d2e49 | 308 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
309 | } |
310 | if (options & GET_STATE) { | |
06c7f68e | 311 | dev->disk.state = 0; |
e86c9dd6 NB |
312 | strcpy(dbase, "state"); |
313 | if (load_sys(fname, buf)) | |
314 | goto abort; | |
315 | if (strstr(buf, "in_sync")) | |
06c7f68e | 316 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 317 | if (strstr(buf, "faulty")) |
06c7f68e NB |
318 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
319 | if (dev->disk.state == 0) | |
7e0f6979 | 320 | sra->array.spare_disks++; |
e86c9dd6 NB |
321 | } |
322 | if (options & GET_ERROR) { | |
323 | strcpy(buf, "errors"); | |
324 | if (load_sys(fname, buf)) | |
325 | goto abort; | |
326 | dev->errors = strtoul(buf, NULL, 0); | |
327 | } | |
328 | } | |
355726fa | 329 | closedir(dir); |
e86c9dd6 NB |
330 | return sra; |
331 | ||
332 | abort: | |
355726fa NB |
333 | if (dir) |
334 | closedir(dir); | |
8382f19b | 335 | sysfs_free(sra); |
e86c9dd6 NB |
336 | return NULL; |
337 | } | |
338 | ||
1770662b DW |
339 | int sysfs_attr_match(const char *attr, const char *str) |
340 | { | |
341 | /* See if attr, read from a sysfs file, matches | |
342 | * str. They must either be the same, or attr can | |
343 | * have a trailing newline or comma | |
344 | */ | |
345 | while (*attr && *str && *attr == *str) { | |
346 | attr++; | |
347 | str++; | |
348 | } | |
349 | ||
350 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
351 | return 0; | |
352 | return 1; | |
353 | } | |
354 | ||
355 | int sysfs_match_word(const char *word, char **list) | |
356 | { | |
357 | int n; | |
358 | for (n=0; list[n]; n++) | |
359 | if (sysfs_attr_match(word, list[n])) | |
360 | break; | |
361 | return n; | |
362 | } | |
363 | ||
e86c9dd6 NB |
364 | unsigned long long get_component_size(int fd) |
365 | { | |
366 | /* Find out the component size of the array. | |
367 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
368 | * size field is only 32bits. | |
369 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 370 | * |
8686f3ed | 371 | * This returns in units of sectors. |
e86c9dd6 NB |
372 | */ |
373 | struct stat stb; | |
374 | char fname[50]; | |
375 | int n; | |
376 | if (fstat(fd, &stb)) return 0; | |
4ebd3237 | 377 | if (major(stb.st_rdev) != get_mdp_major()) |
e86c9dd6 | 378 | sprintf(fname, "/sys/block/md%d/md/component_size", |
ea24acd0 | 379 | (int)minor(stb.st_rdev)); |
e86c9dd6 NB |
380 | else |
381 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
ea24acd0 | 382 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
383 | fd = open(fname, O_RDONLY); |
384 | if (fd < 0) | |
385 | return 0; | |
386 | n = read(fd, fname, sizeof(fname)); | |
387 | close(fd); | |
388 | if (n == sizeof(fname)) | |
389 | return 0; | |
390 | fname[n] = 0; | |
8686f3ed | 391 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
392 | } |
393 | ||
7e0f6979 | 394 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
395 | char *name, char *val) |
396 | { | |
397 | char fname[50]; | |
398 | int n; | |
399 | int fd; | |
7e1432fb | 400 | |
e86c9dd6 | 401 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 402 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
403 | fd = open(fname, O_WRONLY); |
404 | if (fd < 0) | |
405 | return -1; | |
406 | n = write(fd, val, strlen(val)); | |
407 | close(fd); | |
2a24d7b6 DW |
408 | if (n != strlen(val)) { |
409 | dprintf(Name ": failed to write '%s' to '%s' (%s)\n", | |
410 | val, fname, strerror(errno)); | |
e86c9dd6 | 411 | return -1; |
2a24d7b6 | 412 | } |
e86c9dd6 NB |
413 | return 0; |
414 | } | |
415 | ||
7e0f6979 | 416 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
417 | char *name, unsigned long long val) |
418 | { | |
419 | char valstr[50]; | |
420 | sprintf(valstr, "%llu", val); | |
421 | return sysfs_set_str(sra, dev, name, valstr); | |
422 | } | |
423 | ||
97590376 N |
424 | int sysfs_uevent(struct mdinfo *sra, char *event) |
425 | { | |
426 | char fname[50]; | |
427 | int n; | |
428 | int fd; | |
429 | ||
430 | sprintf(fname, "/sys/block/%s/uevent", | |
431 | sra->sys_name); | |
432 | fd = open(fname, O_WRONLY); | |
433 | if (fd < 0) | |
434 | return -1; | |
435 | n = write(fd, event, strlen(event)); | |
436 | close(fd); | |
437 | return 0; | |
438 | } | |
439 | ||
a6288483 N |
440 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, |
441 | char *name) | |
e86c9dd6 NB |
442 | { |
443 | char fname[50]; | |
e86c9dd6 | 444 | int fd; |
a6288483 | 445 | |
e86c9dd6 | 446 | sprintf(fname, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 447 | sra->sys_name, dev?dev->sys_name:"", name); |
a6288483 | 448 | fd = open(fname, O_RDWR); |
e86c9dd6 | 449 | if (fd < 0) |
a6288483 N |
450 | fd = open(fname, O_RDONLY); |
451 | return fd; | |
452 | } | |
453 | ||
454 | int sysfs_fd_get_ll(int fd, unsigned long long *val) | |
455 | { | |
456 | char buf[50]; | |
457 | int n; | |
458 | char *ep; | |
459 | ||
460 | lseek(fd, 0, 0); | |
e86c9dd6 | 461 | n = read(fd, buf, sizeof(buf)); |
e86c9dd6 NB |
462 | if (n <= 0) |
463 | return -1; | |
464 | buf[n] = 0; | |
465 | *val = strtoull(buf, &ep, 0); | |
466 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
467 | return -1; | |
468 | return 0; | |
469 | } | |
2503d23b | 470 | |
a6288483 N |
471 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
472 | char *name, unsigned long long *val) | |
473 | { | |
474 | int n; | |
475 | int fd; | |
476 | ||
477 | fd = sysfs_get_fd(sra, dev, name); | |
478 | if (fd < 0) | |
479 | return -1; | |
480 | n = sysfs_fd_get_ll(fd, val); | |
481 | close(fd); | |
482 | return n; | |
483 | } | |
484 | ||
7236ee7a N |
485 | int sysfs_fd_get_str(int fd, char *val, int size) |
486 | { | |
487 | int n; | |
488 | ||
489 | lseek(fd, 0, 0); | |
490 | n = read(fd, val, size); | |
491 | if (n <= 0) | |
492 | return -1; | |
493 | val[n] = 0; | |
494 | return n; | |
495 | } | |
496 | ||
93ecfa01 N |
497 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, |
498 | char *name, char *val, int size) | |
499 | { | |
93ecfa01 N |
500 | int n; |
501 | int fd; | |
7236ee7a N |
502 | |
503 | fd = sysfs_get_fd(sra, dev, name); | |
93ecfa01 N |
504 | if (fd < 0) |
505 | return -1; | |
7236ee7a | 506 | n = sysfs_fd_get_str(fd, val, size); |
93ecfa01 | 507 | close(fd); |
93ecfa01 N |
508 | return n; |
509 | } | |
510 | ||
8ed3e5e1 DW |
511 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) |
512 | { | |
513 | unsigned long sec; | |
514 | unsigned long msec; | |
515 | char delay[30]; | |
516 | ||
517 | sec = ms / 1000; | |
0dd3ba30 | 518 | msec = ms % 1000; |
8ed3e5e1 | 519 | |
0dd3ba30 DW |
520 | sprintf(delay, "%ld.%03ld\n", sec, msec); |
521 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
8ed3e5e1 DW |
522 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); |
523 | } | |
524 | ||
f35f2525 | 525 | int sysfs_set_array(struct mdinfo *info, int vers) |
2503d23b NB |
526 | { |
527 | int rv = 0; | |
f35f2525 N |
528 | char ver[100]; |
529 | ||
530 | ver[0] = 0; | |
531 | if (info->array.major_version == -1 && | |
532 | info->array.minor_version == -2) { | |
533 | strcat(strcpy(ver, "external:"), info->text_version); | |
534 | ||
535 | if ((vers % 100) < 2 || | |
536 | sysfs_set_str(info, NULL, "metadata_version", | |
537 | ver) < 0) { | |
538 | fprintf(stderr, Name ": This kernel does not " | |
539 | "support external metadata.\n"); | |
540 | return 1; | |
541 | } | |
542 | } | |
2503d23b NB |
543 | if (info->array.level < 0) |
544 | return 0; /* FIXME */ | |
f35f2525 | 545 | rv |= sysfs_set_str(info, NULL, "level", |
2503d23b | 546 | map_num(pers, info->array.level)); |
f35f2525 N |
547 | rv |= sysfs_set_num(info, NULL, "raid_disks", info->array.raid_disks); |
548 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); | |
549 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
550 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
da9b4a62 DW |
551 | if (info->custom_array_size) { |
552 | int rc; | |
553 | ||
554 | rc = sysfs_set_num(info, NULL, "array_size", | |
555 | info->custom_array_size/2); | |
556 | if (rc && errno == ENOENT) { | |
557 | fprintf(stderr, Name ": This kernel does not " | |
558 | "have the md/array_size attribute, " | |
559 | "the array may be larger than expected\n"); | |
560 | rc = 0; | |
561 | } | |
562 | rv |= rc; | |
563 | } | |
564 | ||
f35f2525 N |
565 | if (info->array.level > 0) |
566 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
2503d23b NB |
567 | return rv; |
568 | } | |
569 | ||
2904b26f | 570 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) |
2503d23b | 571 | { |
33a6535d AW |
572 | char dv[PATH_MAX]; |
573 | char nm[PATH_MAX]; | |
2503d23b NB |
574 | char *dname; |
575 | int rv; | |
576 | ||
577 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
578 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
579 | if (rv) | |
580 | return rv; | |
581 | ||
582 | memset(nm, 0, sizeof(nm)); | |
583 | sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor); | |
73649188 N |
584 | rv = readlink(dv, nm, sizeof(nm)); |
585 | if (rv <= 0) | |
2503d23b | 586 | return -1; |
73649188 | 587 | nm[rv] = '\0'; |
2503d23b NB |
588 | dname = strrchr(nm, '/'); |
589 | if (dname) dname++; | |
590 | strcpy(sd->sys_name, "dev-"); | |
591 | strcpy(sd->sys_name+4, dname); | |
592 | ||
2904b26f DW |
593 | /* test write to see if 'recovery_start' is available */ |
594 | if (resume && sd->recovery_start < MaxSector && | |
595 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
596 | sysfs_set_str(sra, sd, "state", "remove"); | |
597 | return -1; | |
598 | } | |
599 | ||
73649188 | 600 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
601 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
602 | if (sra->array.level != LEVEL_CONTAINER) { | |
d23534e4 | 603 | if (sd->recovery_start == MaxSector) |
462906cd N |
604 | /* This can correctly fail if array isn't started, |
605 | * yet, so just ignore status for now. | |
606 | */ | |
d23534e4 | 607 | sysfs_set_str(sra, sd, "state", "insync"); |
2503d23b | 608 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); |
2904b26f DW |
609 | if (resume) |
610 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
2503d23b | 611 | } |
2503d23b NB |
612 | return rv; |
613 | } | |
90c8b707 | 614 | |
755c99fa | 615 | #if 0 |
90c8b707 DW |
616 | int sysfs_disk_to_sg(int fd) |
617 | { | |
618 | /* from an open block device, try find and open its corresponding | |
619 | * scsi_generic interface | |
620 | */ | |
621 | struct stat st; | |
622 | char path[256]; | |
623 | char sg_path[256]; | |
624 | char sg_major_minor[8]; | |
625 | char *c; | |
626 | DIR *dir; | |
627 | struct dirent *de; | |
628 | int major, minor, rv; | |
629 | ||
630 | if (fstat(fd, &st)) | |
631 | return -1; | |
632 | ||
633 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
634 | major(st.st_rdev), minor(st.st_rdev)); | |
635 | ||
636 | dir = opendir(path); | |
637 | if (!dir) | |
638 | return -1; | |
639 | ||
640 | de = readdir(dir); | |
641 | while (de) { | |
642 | if (strncmp("scsi_generic:", de->d_name, | |
643 | strlen("scsi_generic:")) == 0) | |
644 | break; | |
645 | de = readdir(dir); | |
646 | } | |
647 | closedir(dir); | |
648 | ||
649 | if (!de) | |
650 | return -1; | |
651 | ||
652 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
653 | fd = open(sg_path, O_RDONLY); | |
654 | if (fd < 0) | |
655 | return fd; | |
656 | ||
657 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
658 | close(fd); | |
659 | if (rv < 0) | |
660 | return -1; | |
661 | else | |
662 | sg_major_minor[rv - 1] = '\0'; | |
663 | ||
664 | c = strchr(sg_major_minor, ':'); | |
665 | *c = '\0'; | |
666 | c++; | |
667 | major = strtol(sg_major_minor, NULL, 10); | |
668 | minor = strtol(c, NULL, 10); | |
669 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
670 | (int) getpid(), major, minor); | |
671 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
672 | fd = open(path, O_RDONLY); | |
673 | unlink(path); | |
674 | return fd; | |
675 | } | |
676 | ||
677 | return -1; | |
678 | } | |
755c99fa | 679 | #endif |
90c8b707 | 680 | |
f1665f72 DW |
681 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
682 | { | |
683 | /* from an open block device, try to retrieve it scsi_id */ | |
684 | struct stat st; | |
685 | char path[256]; | |
686 | char *c1, *c2; | |
687 | DIR *dir; | |
688 | struct dirent *de; | |
689 | ||
690 | if (fstat(fd, &st)) | |
691 | return 1; | |
692 | ||
693 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
694 | major(st.st_rdev), minor(st.st_rdev)); | |
695 | ||
696 | dir = opendir(path); | |
697 | if (!dir) | |
698 | return 1; | |
699 | ||
700 | de = readdir(dir); | |
701 | while (de) { | |
702 | if (strncmp("scsi_disk:", de->d_name, | |
703 | strlen("scsi_disk:")) == 0) | |
704 | break; | |
705 | de = readdir(dir); | |
706 | } | |
707 | closedir(dir); | |
708 | ||
709 | if (!de) | |
710 | return 1; | |
711 | ||
712 | c1 = strchr(de->d_name, ':'); | |
713 | c1++; | |
714 | c2 = strchr(c1, ':'); | |
715 | *c2 = '\0'; | |
716 | *id = strtol(c1, NULL, 10) << 24; /* host */ | |
717 | c1 = c2 + 1; | |
718 | c2 = strchr(c1, ':'); | |
719 | *c2 = '\0'; | |
720 | *id |= strtol(c1, NULL, 10) << 16; /* channel */ | |
721 | c1 = c2 + 1; | |
722 | c2 = strchr(c1, ':'); | |
723 | *c2 = '\0'; | |
724 | *id |= strtol(c1, NULL, 10) << 8; /* lun */ | |
725 | c1 = c2 + 1; | |
726 | *id |= strtol(c1, NULL, 10); /* id */ | |
727 | ||
728 | return 0; | |
729 | } | |
f94d52f4 NB |
730 | |
731 | ||
732 | int sysfs_unique_holder(int devnum, long rdev) | |
733 | { | |
734 | /* Check that devnum is a holder of rdev, | |
735 | * and is the only holder. | |
736 | * we should be locked against races by | |
737 | * an O_EXCL on devnum | |
738 | */ | |
739 | DIR *dir; | |
740 | struct dirent *de; | |
741 | char dirname[100]; | |
742 | char l; | |
743 | int found = 0; | |
744 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", | |
745 | major(rdev), minor(rdev)); | |
746 | dir = opendir(dirname); | |
747 | errno = ENOENT; | |
748 | if (!dir) | |
749 | return 0; | |
750 | l = strlen(dirname); | |
751 | while ((de = readdir(dir)) != NULL) { | |
752 | char buf[10]; | |
753 | int n; | |
754 | int mj, mn; | |
755 | char c; | |
756 | int fd; | |
757 | ||
758 | if (de->d_ino == 0) | |
759 | continue; | |
760 | if (de->d_name[0] == '.') | |
761 | continue; | |
762 | strcpy(dirname+l, "/"); | |
763 | strcat(dirname+l, de->d_name); | |
764 | strcat(dirname+l, "/dev"); | |
765 | fd = open(dirname, O_RDONLY); | |
766 | if (fd < 0) { | |
767 | errno = ENOENT; | |
768 | break; | |
769 | } | |
770 | n = read(fd, buf, sizeof(buf)-1); | |
771 | close(fd); | |
772 | buf[n] = 0; | |
773 | if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 || | |
774 | c != '\n') { | |
775 | errno = ENOENT; | |
776 | break; | |
777 | } | |
778 | if (mj != MD_MAJOR) | |
779 | mn = -1-(mn>>6); | |
780 | ||
781 | if (devnum != mn) { | |
782 | errno = EEXIST; | |
783 | break; | |
784 | } | |
785 | found = 1; | |
786 | } | |
787 | closedir(dir); | |
788 | if (de) | |
789 | return 0; | |
790 | else | |
791 | return found; | |
792 | } | |
38a07ed6 N |
793 | |
794 | #ifndef MDASSEMBLE | |
795 | ||
796 | static char *clean_states[] = { | |
797 | "clear", "inactive", "readonly", "read-auto", "clean", NULL }; | |
798 | ||
9f1da824 | 799 | int WaitClean(char *dev, int sock, int verbose) |
38a07ed6 N |
800 | { |
801 | int fd; | |
802 | struct mdinfo *mdi; | |
803 | int rv = 1; | |
804 | int devnum; | |
805 | ||
806 | fd = open(dev, O_RDONLY); | |
807 | if (fd < 0) { | |
808 | if (verbose) | |
809 | fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno)); | |
810 | return 1; | |
811 | } | |
812 | ||
813 | devnum = fd2devnum(fd); | |
814 | mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE); | |
815 | if (!mdi) { | |
816 | if (verbose) | |
817 | fprintf(stderr, Name ": Failed to read sysfs attributes for " | |
818 | "%s\n", dev); | |
819 | close(fd); | |
820 | return 0; | |
821 | } | |
822 | ||
823 | switch(mdi->array.level) { | |
824 | case LEVEL_LINEAR: | |
825 | case LEVEL_MULTIPATH: | |
826 | case 0: | |
827 | /* safemode delay is irrelevant for these levels */ | |
828 | rv = 0; | |
829 | ||
830 | } | |
831 | ||
832 | /* for internal metadata the kernel handles the final clean | |
833 | * transition, containers can never be dirty | |
834 | */ | |
835 | if (!is_subarray(mdi->text_version)) | |
836 | rv = 0; | |
837 | ||
838 | /* safemode disabled ? */ | |
839 | if (mdi->safe_mode_delay == 0) | |
840 | rv = 0; | |
841 | ||
842 | if (rv) { | |
843 | int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state"); | |
844 | char buf[20]; | |
845 | fd_set fds; | |
846 | struct timeval tm; | |
847 | ||
848 | /* minimize the safe_mode_delay and prepare to wait up to 5s | |
849 | * for writes to quiesce | |
850 | */ | |
851 | sysfs_set_safemode(mdi, 1); | |
852 | tm.tv_sec = 5; | |
853 | tm.tv_usec = 0; | |
854 | ||
855 | /* give mdmon a chance to checkpoint resync */ | |
856 | sysfs_set_str(mdi, NULL, "sync_action", "idle"); | |
857 | ||
858 | FD_ZERO(&fds); | |
859 | ||
860 | /* wait for array_state to be clean */ | |
861 | while (1) { | |
862 | rv = read(state_fd, buf, sizeof(buf)); | |
863 | if (rv < 0) | |
864 | break; | |
865 | if (sysfs_match_word(buf, clean_states) <= 4) | |
866 | break; | |
867 | FD_SET(state_fd, &fds); | |
868 | rv = select(state_fd + 1, NULL, NULL, &fds, &tm); | |
869 | if (rv < 0 && errno != EINTR) | |
870 | break; | |
871 | lseek(state_fd, 0, SEEK_SET); | |
872 | } | |
873 | if (rv < 0) | |
874 | rv = 1; | |
9f1da824 DW |
875 | else if (fping_monitor(sock) == 0 || |
876 | ping_monitor(mdi->text_version) == 0) { | |
38a07ed6 N |
877 | /* we need to ping to close the window between array |
878 | * state transitioning to clean and the metadata being | |
879 | * marked clean | |
880 | */ | |
881 | rv = 0; | |
882 | } else | |
883 | rv = 1; | |
884 | if (rv && verbose) | |
885 | fprintf(stderr, Name ": Error waiting for %s to be clean\n", | |
886 | dev); | |
887 | ||
888 | /* restore the original safe_mode_delay */ | |
889 | sysfs_set_safemode(mdi, mdi->safe_mode_delay); | |
890 | close(state_fd); | |
891 | } | |
892 | ||
893 | sysfs_free(mdi); | |
894 | close(fd); | |
895 | ||
896 | return rv; | |
897 | } | |
898 | #endif /* MDASSEMBLE */ |