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