]> git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
Merge branch 'master' into devel-3.0
[thirdparty/mdadm.git] / sysfs.c
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 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 if (fd >= 0) {
85 struct stat stb;
86 mdu_version_t vers;
87 if (fstat(fd, &stb))
88 return;
89 if (ioctl(fd, RAID_VERSION, &vers) != 0)
90 return;
91 if (major(stb.st_rdev)==9)
92 sprintf(mdi->sys_name, "md%d", (int)minor(stb.st_rdev));
93 else
94 sprintf(mdi->sys_name, "md_d%d",
95 (int)minor(stb.st_rdev)>>MdpMinorShift);
96 } else {
97 if (devnum >= 0)
98 sprintf(mdi->sys_name, "md%d", devnum);
99 else
100 sprintf(mdi->sys_name, "md_d%d",
101 -1-devnum);
102 }
103 }
104
105 struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options)
106 {
107 /* Longest possible name in sysfs, mounted at /sys, is
108 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
109 * /sys/block/md_dXXX/md/metadata_version
110 * which is about 41 characters. 50 should do for now
111 */
112 char fname[50];
113 char buf[1024];
114 char *base;
115 char *dbase;
116 struct mdinfo *sra;
117 struct mdinfo *dev;
118 DIR *dir = NULL;
119 struct dirent *de;
120
121 sra = malloc(sizeof(*sra));
122 if (sra == NULL)
123 return sra;
124 memset(sra, 0, sizeof(*sra));
125 sysfs_init(sra, fd, devnum);
126
127 sprintf(fname, "/sys/block/%s/md/", sra->sys_name);
128 base = fname + strlen(fname);
129
130 sra->devs = NULL;
131 if (options & GET_VERSION) {
132 strcpy(base, "metadata_version");
133 if (load_sys(fname, buf))
134 goto abort;
135 if (strncmp(buf, "none", 4) == 0) {
136 sra->array.major_version =
137 sra->array.minor_version = -1;
138 strcpy(sra->text_version, "");
139 } else if (strncmp(buf, "external:", 9) == 0) {
140 sra->array.major_version = -1;
141 sra->array.minor_version = -2;
142 strcpy(sra->text_version, buf+9);
143 } else {
144 sscanf(buf, "%d.%d",
145 &sra->array.major_version,
146 &sra->array.minor_version);
147 strcpy(sra->text_version, buf);
148 }
149 }
150 if (options & GET_LEVEL) {
151 strcpy(base, "level");
152 if (load_sys(fname, buf))
153 goto abort;
154 sra->array.level = map_name(pers, buf);
155 }
156 if (options & GET_LAYOUT) {
157 strcpy(base, "layout");
158 if (load_sys(fname, buf))
159 goto abort;
160 sra->array.layout = strtoul(buf, NULL, 0);
161 }
162 if (options & GET_DISKS) {
163 strcpy(base, "raid_disks");
164 if (load_sys(fname, buf))
165 goto abort;
166 sra->array.raid_disks = strtoul(buf, NULL, 0);
167 }
168 if (options & GET_DEGRADED) {
169 strcpy(base, "degraded");
170 if (load_sys(fname, buf))
171 goto abort;
172 sra->array.failed_disks = strtoul(buf, NULL, 0);
173 }
174 if (options & GET_COMPONENT) {
175 strcpy(base, "component_size");
176 if (load_sys(fname, buf))
177 goto abort;
178 sra->component_size = strtoull(buf, NULL, 0);
179 /* sysfs reports "K", but we want sectors */
180 sra->component_size *= 2;
181 }
182 if (options & GET_CHUNK) {
183 strcpy(base, "chunk_size");
184 if (load_sys(fname, buf))
185 goto abort;
186 sra->array.chunk_size = strtoul(buf, NULL, 0);
187 }
188 if (options & GET_CACHE) {
189 strcpy(base, "stripe_cache_size");
190 if (load_sys(fname, buf))
191 goto abort;
192 sra->cache_size = strtoul(buf, NULL, 0);
193 }
194 if (options & GET_MISMATCH) {
195 strcpy(base, "mismatch_cnt");
196 if (load_sys(fname, buf))
197 goto abort;
198 sra->mismatch_cnt = strtoul(buf, NULL, 0);
199 }
200 if (options & GET_SAFEMODE) {
201 int scale = 1;
202 int dot = 0;
203 int i;
204 unsigned long msec;
205 size_t len;
206
207 strcpy(base, "safe_mode_delay");
208 if (load_sys(fname, buf))
209 goto abort;
210
211 /* remove a period, and count digits after it */
212 len = strlen(buf);
213 for (i = 0; i < len; i++) {
214 if (dot) {
215 if (isdigit(buf[i])) {
216 buf[i-1] = buf[i];
217 scale *= 10;
218 }
219 buf[i] = 0;
220 } else if (buf[i] == '.') {
221 dot=1;
222 buf[i] = 0;
223 }
224 }
225 msec = strtoul(buf, NULL, 10);
226 msec = (msec * 1000) / scale;
227 sra->safe_mode_delay = msec;
228 }
229
230 if (! (options & GET_DEVS))
231 return sra;
232
233 /* Get all the devices as well */
234 *base = 0;
235 dir = opendir(fname);
236 if (!dir)
237 goto abort;
238 sra->array.spare_disks = 0;
239
240 while ((de = readdir(dir)) != NULL) {
241 char *ep;
242 if (de->d_ino == 0 ||
243 strncmp(de->d_name, "dev-", 4) != 0)
244 continue;
245 strcpy(base, de->d_name);
246 dbase = base + strlen(base);
247 *dbase++ = '/';
248
249 dev = malloc(sizeof(*dev));
250 if (!dev)
251 goto abort;
252
253 /* Always get slot, major, minor */
254 strcpy(dbase, "slot");
255 if (load_sys(fname, buf)) {
256 /* hmm... unable to read 'slot' maybe the device
257 * is going away?
258 */
259 strcpy(dbase, "block");
260 if (readlink(fname, buf, sizeof(buf)) < 0 &&
261 errno != ENAMETOOLONG) {
262 /* ...yup device is gone */
263 free(dev);
264 continue;
265 } else {
266 /* slot is unreadable but 'block' link
267 * still intact... something bad is happening
268 * so abort
269 */
270 free(dev);
271 goto abort;
272 }
273
274 }
275 dev->next = sra->devs;
276 sra->devs = dev;
277
278 strcpy(dev->sys_name, de->d_name);
279 dev->disk.raid_disk = strtoul(buf, &ep, 10);
280 if (*ep) dev->disk.raid_disk = -1;
281
282 strcpy(dbase, "block/dev");
283 if (load_sys(fname, buf))
284 goto abort;
285 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
286
287 if (options & GET_OFFSET) {
288 strcpy(dbase, "offset");
289 if (load_sys(fname, buf))
290 goto abort;
291 dev->data_offset = strtoull(buf, NULL, 0);
292 }
293 if (options & GET_SIZE) {
294 strcpy(dbase, "size");
295 if (load_sys(fname, buf))
296 goto abort;
297 dev->component_size = strtoull(buf, NULL, 0) * 2;
298 }
299 if (options & GET_STATE) {
300 dev->disk.state = 0;
301 strcpy(dbase, "state");
302 if (load_sys(fname, buf))
303 goto abort;
304 if (strstr(buf, "in_sync"))
305 dev->disk.state |= (1<<MD_DISK_SYNC);
306 if (strstr(buf, "faulty"))
307 dev->disk.state |= (1<<MD_DISK_FAULTY);
308 if (dev->disk.state == 0)
309 sra->array.spare_disks++;
310 }
311 if (options & GET_ERROR) {
312 strcpy(buf, "errors");
313 if (load_sys(fname, buf))
314 goto abort;
315 dev->errors = strtoul(buf, NULL, 0);
316 }
317 }
318 closedir(dir);
319 return sra;
320
321 abort:
322 if (dir)
323 closedir(dir);
324 sysfs_free(sra);
325 return NULL;
326 }
327
328 int sysfs_attr_match(const char *attr, const char *str)
329 {
330 /* See if attr, read from a sysfs file, matches
331 * str. They must either be the same, or attr can
332 * have a trailing newline or comma
333 */
334 while (*attr && *str && *attr == *str) {
335 attr++;
336 str++;
337 }
338
339 if (*str || (*attr && *attr != ',' && *attr != '\n'))
340 return 0;
341 return 1;
342 }
343
344 int sysfs_match_word(const char *word, char **list)
345 {
346 int n;
347 for (n=0; list[n]; n++)
348 if (sysfs_attr_match(word, list[n]))
349 break;
350 return n;
351 }
352
353 unsigned long long get_component_size(int fd)
354 {
355 /* Find out the component size of the array.
356 * We cannot trust GET_ARRAY_INFO ioctl as it's
357 * size field is only 32bits.
358 * So look in /sys/block/mdXXX/md/component_size
359 *
360 * This returns in units of sectors.
361 */
362 struct stat stb;
363 char fname[50];
364 int n;
365 if (fstat(fd, &stb)) return 0;
366 if (major(stb.st_rdev) == 9)
367 sprintf(fname, "/sys/block/md%d/md/component_size",
368 (int)minor(stb.st_rdev));
369 else
370 sprintf(fname, "/sys/block/md_d%d/md/component_size",
371 (int)minor(stb.st_rdev)>>MdpMinorShift);
372 fd = open(fname, O_RDONLY);
373 if (fd < 0)
374 return 0;
375 n = read(fd, fname, sizeof(fname));
376 close(fd);
377 if (n == sizeof(fname))
378 return 0;
379 fname[n] = 0;
380 return strtoull(fname, NULL, 10) * 2;
381 }
382
383 int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
384 char *name, char *val)
385 {
386 char fname[50];
387 int n;
388 int fd;
389
390 sprintf(fname, "/sys/block/%s/md/%s/%s",
391 sra->sys_name, dev?dev->sys_name:"", name);
392 fd = open(fname, O_WRONLY);
393 if (fd < 0)
394 return -1;
395 n = write(fd, val, strlen(val));
396 close(fd);
397 if (n != strlen(val)) {
398 dprintf(Name ": failed to write '%s' to '%s' (%s)\n",
399 val, fname, strerror(errno));
400 return -1;
401 }
402 return 0;
403 }
404
405 int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
406 char *name, unsigned long long val)
407 {
408 char valstr[50];
409 sprintf(valstr, "%llu", val);
410 return sysfs_set_str(sra, dev, name, valstr);
411 }
412
413 int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
414 char *name, unsigned long long *val)
415 {
416 char fname[50];
417 char buf[50];
418 int n;
419 int fd;
420 char *ep;
421 sprintf(fname, "/sys/block/%s/md/%s/%s",
422 sra->sys_name, dev?dev->sys_name:"", name);
423 fd = open(fname, O_RDONLY);
424 if (fd < 0)
425 return -1;
426 n = read(fd, buf, sizeof(buf));
427 close(fd);
428 if (n <= 0)
429 return -1;
430 buf[n] = 0;
431 *val = strtoull(buf, &ep, 0);
432 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
433 return -1;
434 return 0;
435 }
436
437 int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms)
438 {
439 unsigned long sec;
440 unsigned long msec;
441 char delay[30];
442
443 sec = ms / 1000;
444 msec = ms % 1000;
445
446 sprintf(delay, "%ld.%03ld\n", sec, msec);
447 /* this '\n' ^ needed for kernels older than 2.6.28 */
448 return sysfs_set_str(sra, NULL, "safe_mode_delay", delay);
449 }
450
451 int sysfs_set_array(struct mdinfo *info, int vers)
452 {
453 int rv = 0;
454 char ver[100];
455
456 ver[0] = 0;
457 if (info->array.major_version == -1 &&
458 info->array.minor_version == -2) {
459 strcat(strcpy(ver, "external:"), info->text_version);
460
461 if ((vers % 100) < 2 ||
462 sysfs_set_str(info, NULL, "metadata_version",
463 ver) < 0) {
464 fprintf(stderr, Name ": This kernel does not "
465 "support external metadata.\n");
466 return 1;
467 }
468 }
469 if (info->array.level < 0)
470 return 0; /* FIXME */
471 rv |= sysfs_set_str(info, NULL, "level",
472 map_num(pers, info->array.level));
473 rv |= sysfs_set_num(info, NULL, "raid_disks", info->array.raid_disks);
474 rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size);
475 rv |= sysfs_set_num(info, NULL, "layout", info->array.layout);
476 rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2);
477 if (info->array.level > 0)
478 rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start);
479 return rv;
480 }
481
482 int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd)
483 {
484 char dv[100];
485 char nm[100];
486 char *dname;
487 int rv;
488
489 sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor);
490 rv = sysfs_set_str(sra, NULL, "new_dev", dv);
491 if (rv)
492 return rv;
493
494 memset(nm, 0, sizeof(nm));
495 sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor);
496 rv = readlink(dv, nm, sizeof(nm));
497 if (rv <= 0)
498 return -1;
499 nm[rv] = '\0';
500 dname = strrchr(nm, '/');
501 if (dname) dname++;
502 strcpy(sd->sys_name, "dev-");
503 strcpy(sd->sys_name+4, dname);
504
505 rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
506 rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
507 if (sra->array.level != LEVEL_CONTAINER) {
508 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
509 // rv |= sysfs_set_str(sra, sd, "state", "in_sync");
510 }
511 return rv;
512 }
513
514 #if 0
515 int sysfs_disk_to_sg(int fd)
516 {
517 /* from an open block device, try find and open its corresponding
518 * scsi_generic interface
519 */
520 struct stat st;
521 char path[256];
522 char sg_path[256];
523 char sg_major_minor[8];
524 char *c;
525 DIR *dir;
526 struct dirent *de;
527 int major, minor, rv;
528
529 if (fstat(fd, &st))
530 return -1;
531
532 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
533 major(st.st_rdev), minor(st.st_rdev));
534
535 dir = opendir(path);
536 if (!dir)
537 return -1;
538
539 de = readdir(dir);
540 while (de) {
541 if (strncmp("scsi_generic:", de->d_name,
542 strlen("scsi_generic:")) == 0)
543 break;
544 de = readdir(dir);
545 }
546 closedir(dir);
547
548 if (!de)
549 return -1;
550
551 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
552 fd = open(sg_path, O_RDONLY);
553 if (fd < 0)
554 return fd;
555
556 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
557 close(fd);
558 if (rv < 0)
559 return -1;
560 else
561 sg_major_minor[rv - 1] = '\0';
562
563 c = strchr(sg_major_minor, ':');
564 *c = '\0';
565 c++;
566 major = strtol(sg_major_minor, NULL, 10);
567 minor = strtol(c, NULL, 10);
568 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
569 (int) getpid(), major, minor);
570 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
571 fd = open(path, O_RDONLY);
572 unlink(path);
573 return fd;
574 }
575
576 return -1;
577 }
578 #endif
579
580 int sysfs_disk_to_scsi_id(int fd, __u32 *id)
581 {
582 /* from an open block device, try to retrieve it scsi_id */
583 struct stat st;
584 char path[256];
585 char *c1, *c2;
586 DIR *dir;
587 struct dirent *de;
588
589 if (fstat(fd, &st))
590 return 1;
591
592 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
593 major(st.st_rdev), minor(st.st_rdev));
594
595 dir = opendir(path);
596 if (!dir)
597 return 1;
598
599 de = readdir(dir);
600 while (de) {
601 if (strncmp("scsi_disk:", de->d_name,
602 strlen("scsi_disk:")) == 0)
603 break;
604 de = readdir(dir);
605 }
606 closedir(dir);
607
608 if (!de)
609 return 1;
610
611 c1 = strchr(de->d_name, ':');
612 c1++;
613 c2 = strchr(c1, ':');
614 *c2 = '\0';
615 *id = strtol(c1, NULL, 10) << 24; /* host */
616 c1 = c2 + 1;
617 c2 = strchr(c1, ':');
618 *c2 = '\0';
619 *id |= strtol(c1, NULL, 10) << 16; /* channel */
620 c1 = c2 + 1;
621 c2 = strchr(c1, ':');
622 *c2 = '\0';
623 *id |= strtol(c1, NULL, 10) << 8; /* lun */
624 c1 = c2 + 1;
625 *id |= strtol(c1, NULL, 10); /* id */
626
627 return 0;
628 }
629
630
631 int sysfs_unique_holder(int devnum, long rdev)
632 {
633 /* Check that devnum is a holder of rdev,
634 * and is the only holder.
635 * we should be locked against races by
636 * an O_EXCL on devnum
637 */
638 DIR *dir;
639 struct dirent *de;
640 char dirname[100];
641 char l;
642 int found = 0;
643 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
644 major(rdev), minor(rdev));
645 dir = opendir(dirname);
646 errno = ENOENT;
647 if (!dir)
648 return 0;
649 l = strlen(dirname);
650 while ((de = readdir(dir)) != NULL) {
651 char buf[10];
652 int n;
653 int mj, mn;
654 char c;
655 int fd;
656
657 if (de->d_ino == 0)
658 continue;
659 if (de->d_name[0] == '.')
660 continue;
661 strcpy(dirname+l, "/");
662 strcat(dirname+l, de->d_name);
663 strcat(dirname+l, "/dev");
664 fd = open(dirname, O_RDONLY);
665 if (fd < 0) {
666 errno = ENOENT;
667 break;
668 }
669 n = read(fd, buf, sizeof(buf)-1);
670 close(fd);
671 buf[n] = 0;
672 if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 ||
673 c != '\n') {
674 errno = ENOENT;
675 break;
676 }
677 if (mj != MD_MAJOR)
678 mn = -1-(mn>>6);
679
680 if (devnum != mn) {
681 errno = EEXIST;
682 break;
683 }
684 found = 1;
685 }
686 closedir(dir);
687 if (de)
688 return 0;
689 else
690 return found;
691 }