]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
2 * sysfs - extract md related information from sysfs. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006 Neil Brown <neilb@suse.de>
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.
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.
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
23 * Email: <neilb@suse.de>
30 int load_sys(char *path
, char *buf
)
32 int fd
= open(path
, O_RDONLY
);
36 n
= read(fd
, buf
, 1024);
38 if (n
<0 || n
>= 1024)
41 if (n
&& buf
[n
-1] == '\n')
46 void sysfs_free(struct mdinfo
*sra
)
49 struct mdinfo
*sra2
= sra
->next
;
51 struct mdinfo
*d
= sra
->devs
;
60 int sysfs_open(int devnum
, char *devname
, char *attr
)
65 sprintf(fname
, "/sys/block/%s/md/", devnum2devname(devnum
));
67 strcat(fname
, devname
);
71 fd
= open(fname
, O_RDWR
);
72 if (fd
< 0 && errno
== EACCES
)
73 fd
= open(fname
, O_RDONLY
);
77 struct mdinfo
*sysfs_read(int fd
, int devnum
, unsigned long options
)
79 /* Longest possible name in sysfs, mounted at /sys, is
80 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
81 * /sys/block/md_dXXX/md/metadata_version
82 * which is about 41 characters. 50 should do for now
93 sra
= malloc(sizeof(*sra
));
101 if (fstat(fd
, &stb
)) return NULL
;
102 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
104 if (major(stb
.st_rdev
)==9)
105 sprintf(sra
->sys_name
, "md%d", (int)minor(stb
.st_rdev
));
107 sprintf(sra
->sys_name
, "md_d%d",
108 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
111 sprintf(sra
->sys_name
, "md%d", devnum
);
113 sprintf(sra
->sys_name
, "md_d%d",
116 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
117 base
= fname
+ strlen(fname
);
120 if (options
& GET_VERSION
) {
121 strcpy(base
, "metadata_version");
122 if (load_sys(fname
, buf
))
124 if (strncmp(buf
, "none", 4) == 0) {
125 sra
->array
.major_version
=
126 sra
->array
.minor_version
= -1;
127 strcpy(sra
->text_version
, "");
128 } else if (strncmp(buf
, "external:", 9) == 0) {
129 sra
->array
.major_version
= -1;
130 sra
->array
.minor_version
= -2;
131 strcpy(sra
->text_version
, buf
+9);
134 &sra
->array
.major_version
,
135 &sra
->array
.minor_version
);
136 strcpy(sra
->text_version
, buf
);
139 if (options
& GET_LEVEL
) {
140 strcpy(base
, "level");
141 if (load_sys(fname
, buf
))
143 sra
->array
.level
= map_name(pers
, buf
);
145 if (options
& GET_LAYOUT
) {
146 strcpy(base
, "layout");
147 if (load_sys(fname
, buf
))
149 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
151 if (options
& GET_DISKS
) {
152 strcpy(base
, "raid_disks");
153 if (load_sys(fname
, buf
))
155 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
157 if (options
& GET_DEGRADED
) {
158 strcpy(base
, "degraded");
159 if (load_sys(fname
, buf
))
161 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
163 if (options
& GET_COMPONENT
) {
164 strcpy(base
, "component_size");
165 if (load_sys(fname
, buf
))
167 sra
->component_size
= strtoull(buf
, NULL
, 0);
168 /* sysfs reports "K", but we want sectors */
169 sra
->component_size
*= 2;
171 if (options
& GET_CHUNK
) {
172 strcpy(base
, "chunk_size");
173 if (load_sys(fname
, buf
))
175 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
177 if (options
& GET_CACHE
) {
178 strcpy(base
, "stripe_cache_size");
179 if (load_sys(fname
, buf
))
181 sra
->cache_size
= strtoul(buf
, NULL
, 0);
183 if (options
& GET_MISMATCH
) {
184 strcpy(base
, "mismatch_cnt");
185 if (load_sys(fname
, buf
))
187 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
189 if (options
& GET_SAFEMODE
) {
196 strcpy(base
, "safe_mode_delay");
197 if (load_sys(fname
, buf
))
200 /* remove a period, and count digits after it */
202 for (i
= 0; i
< len
; i
++) {
204 if (isdigit(buf
[i
])) {
209 } else if (buf
[i
] == '.') {
214 msec
= strtoul(buf
, NULL
, 10);
215 msec
= (msec
* 1000) / scale
;
216 sra
->safe_mode_delay
= msec
;
219 if (! (options
& GET_DEVS
))
222 /* Get all the devices as well */
224 dir
= opendir(fname
);
227 sra
->array
.spare_disks
= 0;
229 while ((de
= readdir(dir
)) != NULL
) {
231 if (de
->d_ino
== 0 ||
232 strncmp(de
->d_name
, "dev-", 4) != 0)
234 strcpy(base
, de
->d_name
);
235 dbase
= base
+ strlen(base
);
238 dev
= malloc(sizeof(*dev
));
242 /* Always get slot, major, minor */
243 strcpy(dbase
, "slot");
244 if (load_sys(fname
, buf
)) {
245 /* hmm... unable to read 'slot' maybe the device
248 strcpy(dbase
, "block");
249 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
250 errno
!= ENAMETOOLONG
) {
251 /* ...yup device is gone */
255 /* slot is unreadable but 'block' link
256 * still intact... something bad is happening
264 dev
->next
= sra
->devs
;
267 strcpy(dev
->sys_name
, de
->d_name
);
268 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
269 if (*ep
) dev
->disk
.raid_disk
= -1;
271 strcpy(dbase
, "block/dev");
272 if (load_sys(fname
, buf
))
274 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
276 if (options
& GET_OFFSET
) {
277 strcpy(dbase
, "offset");
278 if (load_sys(fname
, buf
))
280 dev
->data_offset
= strtoull(buf
, NULL
, 0);
282 if (options
& GET_SIZE
) {
283 strcpy(dbase
, "size");
284 if (load_sys(fname
, buf
))
286 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
288 if (options
& GET_STATE
) {
290 strcpy(dbase
, "state");
291 if (load_sys(fname
, buf
))
293 if (strstr(buf
, "in_sync"))
294 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
295 if (strstr(buf
, "faulty"))
296 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
297 if (dev
->disk
.state
== 0)
298 sra
->array
.spare_disks
++;
300 if (options
& GET_ERROR
) {
301 strcpy(buf
, "errors");
302 if (load_sys(fname
, buf
))
304 dev
->errors
= strtoul(buf
, NULL
, 0);
317 int sysfs_attr_match(const char *attr
, const char *str
)
319 /* See if attr, read from a sysfs file, matches
320 * str. They must either be the same, or attr can
321 * have a trailing newline or comma
323 while (*attr
&& *str
&& *attr
== *str
) {
328 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
333 int sysfs_match_word(const char *word
, char **list
)
336 for (n
=0; list
[n
]; n
++)
337 if (sysfs_attr_match(word
, list
[n
]))
342 unsigned long long get_component_size(int fd
)
344 /* Find out the component size of the array.
345 * We cannot trust GET_ARRAY_INFO ioctl as it's
346 * size field is only 32bits.
347 * So look in /sys/block/mdXXX/md/component_size
349 * This returns in units of sectors.
354 if (fstat(fd
, &stb
)) return 0;
355 if (major(stb
.st_rdev
) == 9)
356 sprintf(fname
, "/sys/block/md%d/md/component_size",
357 (int)minor(stb
.st_rdev
));
359 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
360 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
361 fd
= open(fname
, O_RDONLY
);
364 n
= read(fd
, fname
, sizeof(fname
));
366 if (n
== sizeof(fname
))
369 return strtoull(fname
, NULL
, 10) * 2;
372 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
373 char *name
, char *val
)
379 sprintf(fname
, "/sys/block/%s/md/%s/%s",
380 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
381 fd
= open(fname
, O_WRONLY
);
384 n
= write(fd
, val
, strlen(val
));
386 if (n
!= strlen(val
))
391 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
392 char *name
, unsigned long long val
)
395 sprintf(valstr
, "%llu", val
);
396 return sysfs_set_str(sra
, dev
, name
, valstr
);
399 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
400 char *name
, unsigned long long *val
)
407 sprintf(fname
, "/sys/block/%s/md/%s/%s",
408 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
409 fd
= open(fname
, O_RDONLY
);
412 n
= read(fd
, buf
, sizeof(buf
));
417 *val
= strtoull(buf
, &ep
, 0);
418 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
423 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
430 msec
= ms
- (sec
* 1000);
432 sprintf(delay
, "%ld.%ld", sec
, msec
);
433 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
436 int sysfs_set_array(struct mdinfo
*sra
,
440 sra
->array
= info
->array
;
442 if (info
->array
.level
< 0)
443 return 0; /* FIXME */
444 rv
|= sysfs_set_str(sra
, NULL
, "level",
445 map_num(pers
, info
->array
.level
));
446 rv
|= sysfs_set_num(sra
, NULL
, "raid_disks", info
->array
.raid_disks
);
447 rv
|= sysfs_set_num(sra
, NULL
, "chunk_size", info
->array
.chunk_size
);
448 rv
|= sysfs_set_num(sra
, NULL
, "layout", info
->array
.layout
);
449 rv
|= sysfs_set_num(sra
, NULL
, "component_size", info
->component_size
/2);
450 rv
|= sysfs_set_num(sra
, NULL
, "resync_start", info
->resync_start
);
451 sra
->array
= info
->array
;
455 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
)
463 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
464 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
468 memset(nm
, 0, sizeof(nm
));
469 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
470 rv
= readlink(dv
, nm
, sizeof(nm
));
474 dname
= strrchr(nm
, '/');
476 strcpy(sd
->sys_name
, "dev-");
477 strcpy(sd
->sys_name
+4, dname
);
479 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
480 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
481 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
482 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
483 // rv |= sysfs_set_str(sra, sd, "state", "in_sync");
486 sd2
= malloc(sizeof(*sd2
));
488 sd2
->next
= sra
->devs
;
495 int sysfs_disk_to_sg(int fd
)
497 /* from an open block device, try find and open its corresponding
498 * scsi_generic interface
503 char sg_major_minor
[8];
507 int major
, minor
, rv
;
512 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
513 major(st
.st_rdev
), minor(st
.st_rdev
));
521 if (strncmp("scsi_generic:", de
->d_name
,
522 strlen("scsi_generic:")) == 0)
531 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
532 fd
= open(sg_path
, O_RDONLY
);
536 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
541 sg_major_minor
[rv
- 1] = '\0';
543 c
= strchr(sg_major_minor
, ':');
546 major
= strtol(sg_major_minor
, NULL
, 10);
547 minor
= strtol(c
, NULL
, 10);
548 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
549 (int) getpid(), major
, minor
);
550 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
551 fd
= open(path
, O_RDONLY
);
560 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
562 /* from an open block device, try to retrieve it scsi_id */
572 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
573 major(st
.st_rdev
), minor(st
.st_rdev
));
581 if (strncmp("scsi_disk:", de
->d_name
,
582 strlen("scsi_disk:")) == 0)
591 c1
= strchr(de
->d_name
, ':');
593 c2
= strchr(c1
, ':');
595 *id
= strtol(c1
, NULL
, 10) << 24; /* host */
597 c2
= strchr(c1
, ':');
599 *id
|= strtol(c1
, NULL
, 10) << 16; /* channel */
601 c2
= strchr(c1
, ':');
603 *id
|= strtol(c1
, NULL
, 10) << 8; /* lun */
605 *id
|= strtol(c1
, NULL
, 10); /* id */
611 int sysfs_unique_holder(int devnum
, long rdev
)
613 /* Check that devnum is a holder of rdev,
614 * and is the only holder.
615 * we should be locked against races by
616 * an O_EXCL on devnum
623 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
624 major(rdev
), minor(rdev
));
625 dir
= opendir(dirname
);
630 while ((de
= readdir(dir
)) != NULL
) {
639 if (de
->d_name
[0] == '.')
641 strcpy(dirname
+l
, "/");
642 strcat(dirname
+l
, de
->d_name
);
643 strcat(dirname
+l
, "/dev");
644 fd
= open(dirname
, O_RDONLY
);
649 n
= read(fd
, buf
, sizeof(buf
)-1);
652 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||