]>
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
)
64 char *mdname
= devnum2devname(devnum
);
69 sprintf(fname
, "/sys/block/%s/md/", mdname
);
71 strcat(fname
, devname
);
75 fd
= open(fname
, O_RDWR
);
76 if (fd
< 0 && errno
== EACCES
)
77 fd
= open(fname
, O_RDONLY
);
82 void sysfs_init(struct mdinfo
*mdi
, int fd
, int devnum
)
86 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
88 devnum
= fd2devnum(fd
);
90 if (devnum
== NoMdDev
)
93 sprintf(mdi
->sys_name
, "md%d", devnum
);
95 sprintf(mdi
->sys_name
, "md_d%d",
100 struct mdinfo
*sysfs_read(int fd
, int devnum
, unsigned long options
)
102 /* Longest possible name in sysfs, mounted at /sys, is
103 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
104 * /sys/block/md_dXXX/md/metadata_version
105 * which is about 41 characters. 50 should do for now
116 sra
= malloc(sizeof(*sra
));
119 memset(sra
, 0, sizeof(*sra
));
120 sysfs_init(sra
, fd
, devnum
);
122 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
123 base
= fname
+ strlen(fname
);
126 if (options
& GET_VERSION
) {
127 strcpy(base
, "metadata_version");
128 if (load_sys(fname
, buf
))
130 if (strncmp(buf
, "none", 4) == 0) {
131 sra
->array
.major_version
=
132 sra
->array
.minor_version
= -1;
133 strcpy(sra
->text_version
, "");
134 } else if (strncmp(buf
, "external:", 9) == 0) {
135 sra
->array
.major_version
= -1;
136 sra
->array
.minor_version
= -2;
137 strcpy(sra
->text_version
, buf
+9);
140 &sra
->array
.major_version
,
141 &sra
->array
.minor_version
);
142 strcpy(sra
->text_version
, buf
);
145 if (options
& GET_LEVEL
) {
146 strcpy(base
, "level");
147 if (load_sys(fname
, buf
))
149 sra
->array
.level
= map_name(pers
, buf
);
151 if (options
& GET_LAYOUT
) {
152 strcpy(base
, "layout");
153 if (load_sys(fname
, buf
))
155 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
157 if (options
& GET_DISKS
) {
158 strcpy(base
, "raid_disks");
159 if (load_sys(fname
, buf
))
161 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
163 if (options
& GET_DEGRADED
) {
164 strcpy(base
, "degraded");
165 if (load_sys(fname
, buf
))
167 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
169 if (options
& GET_COMPONENT
) {
170 strcpy(base
, "component_size");
171 if (load_sys(fname
, buf
))
173 sra
->component_size
= strtoull(buf
, NULL
, 0);
174 /* sysfs reports "K", but we want sectors */
175 sra
->component_size
*= 2;
177 if (options
& GET_CHUNK
) {
178 strcpy(base
, "chunk_size");
179 if (load_sys(fname
, buf
))
181 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
183 if (options
& GET_CACHE
) {
184 strcpy(base
, "stripe_cache_size");
185 if (load_sys(fname
, buf
))
187 sra
->cache_size
= strtoul(buf
, NULL
, 0);
189 if (options
& GET_MISMATCH
) {
190 strcpy(base
, "mismatch_cnt");
191 if (load_sys(fname
, buf
))
193 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
195 if (options
& GET_SAFEMODE
) {
202 strcpy(base
, "safe_mode_delay");
203 if (load_sys(fname
, buf
))
206 /* remove a period, and count digits after it */
208 for (i
= 0; i
< len
; i
++) {
210 if (isdigit(buf
[i
])) {
215 } else if (buf
[i
] == '.') {
220 msec
= strtoul(buf
, NULL
, 10);
221 msec
= (msec
* 1000) / scale
;
222 sra
->safe_mode_delay
= msec
;
225 if (! (options
& GET_DEVS
))
228 /* Get all the devices as well */
230 dir
= opendir(fname
);
233 sra
->array
.spare_disks
= 0;
235 while ((de
= readdir(dir
)) != NULL
) {
237 if (de
->d_ino
== 0 ||
238 strncmp(de
->d_name
, "dev-", 4) != 0)
240 strcpy(base
, de
->d_name
);
241 dbase
= base
+ strlen(base
);
244 dev
= malloc(sizeof(*dev
));
248 /* Always get slot, major, minor */
249 strcpy(dbase
, "slot");
250 if (load_sys(fname
, buf
)) {
251 /* hmm... unable to read 'slot' maybe the device
254 strcpy(dbase
, "block");
255 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
256 errno
!= ENAMETOOLONG
) {
257 /* ...yup device is gone */
261 /* slot is unreadable but 'block' link
262 * still intact... something bad is happening
270 dev
->next
= sra
->devs
;
273 strcpy(dev
->sys_name
, de
->d_name
);
274 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
275 if (*ep
) dev
->disk
.raid_disk
= -1;
277 strcpy(dbase
, "block/dev");
278 if (load_sys(fname
, buf
))
280 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
282 if (options
& GET_OFFSET
) {
283 strcpy(dbase
, "offset");
284 if (load_sys(fname
, buf
))
286 dev
->data_offset
= strtoull(buf
, NULL
, 0);
288 if (options
& GET_SIZE
) {
289 strcpy(dbase
, "size");
290 if (load_sys(fname
, buf
))
292 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
294 if (options
& GET_STATE
) {
296 strcpy(dbase
, "state");
297 if (load_sys(fname
, buf
))
299 if (strstr(buf
, "in_sync"))
300 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
301 if (strstr(buf
, "faulty"))
302 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
303 if (dev
->disk
.state
== 0)
304 sra
->array
.spare_disks
++;
306 if (options
& GET_ERROR
) {
307 strcpy(buf
, "errors");
308 if (load_sys(fname
, buf
))
310 dev
->errors
= strtoul(buf
, NULL
, 0);
323 int sysfs_attr_match(const char *attr
, const char *str
)
325 /* See if attr, read from a sysfs file, matches
326 * str. They must either be the same, or attr can
327 * have a trailing newline or comma
329 while (*attr
&& *str
&& *attr
== *str
) {
334 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
339 int sysfs_match_word(const char *word
, char **list
)
342 for (n
=0; list
[n
]; n
++)
343 if (sysfs_attr_match(word
, list
[n
]))
348 unsigned long long get_component_size(int fd
)
350 /* Find out the component size of the array.
351 * We cannot trust GET_ARRAY_INFO ioctl as it's
352 * size field is only 32bits.
353 * So look in /sys/block/mdXXX/md/component_size
355 * This returns in units of sectors.
360 if (fstat(fd
, &stb
)) return 0;
361 if (major(stb
.st_rdev
) != get_mdp_major())
362 sprintf(fname
, "/sys/block/md%d/md/component_size",
363 (int)minor(stb
.st_rdev
));
365 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
366 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
367 fd
= open(fname
, O_RDONLY
);
370 n
= read(fd
, fname
, sizeof(fname
));
372 if (n
== sizeof(fname
))
375 return strtoull(fname
, NULL
, 10) * 2;
378 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
379 char *name
, char *val
)
385 sprintf(fname
, "/sys/block/%s/md/%s/%s",
386 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
387 fd
= open(fname
, O_WRONLY
);
390 n
= write(fd
, val
, strlen(val
));
392 if (n
!= strlen(val
)) {
393 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
394 val
, fname
, strerror(errno
));
400 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
401 char *name
, unsigned long long val
)
404 sprintf(valstr
, "%llu", val
);
405 return sysfs_set_str(sra
, dev
, name
, valstr
);
408 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
409 char *name
, unsigned long long *val
)
416 sprintf(fname
, "/sys/block/%s/md/%s/%s",
417 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
418 fd
= open(fname
, O_RDONLY
);
421 n
= read(fd
, buf
, sizeof(buf
));
426 *val
= strtoull(buf
, &ep
, 0);
427 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
432 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
441 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
442 /* this '\n' ^ needed for kernels older than 2.6.28 */
443 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
446 int sysfs_set_array(struct mdinfo
*info
, int vers
)
452 if (info
->array
.major_version
== -1 &&
453 info
->array
.minor_version
== -2) {
454 strcat(strcpy(ver
, "external:"), info
->text_version
);
456 if ((vers
% 100) < 2 ||
457 sysfs_set_str(info
, NULL
, "metadata_version",
459 fprintf(stderr
, Name
": This kernel does not "
460 "support external metadata.\n");
464 if (info
->array
.level
< 0)
465 return 0; /* FIXME */
466 rv
|= sysfs_set_str(info
, NULL
, "level",
467 map_num(pers
, info
->array
.level
));
468 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", info
->array
.raid_disks
);
469 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
470 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
471 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
472 if (info
->array
.level
> 0)
473 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
477 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
)
484 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
485 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
489 memset(nm
, 0, sizeof(nm
));
490 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
491 rv
= readlink(dv
, nm
, sizeof(nm
));
495 dname
= strrchr(nm
, '/');
497 strcpy(sd
->sys_name
, "dev-");
498 strcpy(sd
->sys_name
+4, dname
);
500 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
501 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
502 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
503 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
504 // rv |= sysfs_set_str(sra, sd, "state", "in_sync");
510 int sysfs_disk_to_sg(int fd
)
512 /* from an open block device, try find and open its corresponding
513 * scsi_generic interface
518 char sg_major_minor
[8];
522 int major
, minor
, rv
;
527 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
528 major(st
.st_rdev
), minor(st
.st_rdev
));
536 if (strncmp("scsi_generic:", de
->d_name
,
537 strlen("scsi_generic:")) == 0)
546 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
547 fd
= open(sg_path
, O_RDONLY
);
551 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
556 sg_major_minor
[rv
- 1] = '\0';
558 c
= strchr(sg_major_minor
, ':');
561 major
= strtol(sg_major_minor
, NULL
, 10);
562 minor
= strtol(c
, NULL
, 10);
563 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
564 (int) getpid(), major
, minor
);
565 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
566 fd
= open(path
, O_RDONLY
);
575 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
577 /* from an open block device, try to retrieve it scsi_id */
587 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
588 major(st
.st_rdev
), minor(st
.st_rdev
));
596 if (strncmp("scsi_disk:", de
->d_name
,
597 strlen("scsi_disk:")) == 0)
606 c1
= strchr(de
->d_name
, ':');
608 c2
= strchr(c1
, ':');
610 *id
= strtol(c1
, NULL
, 10) << 24; /* host */
612 c2
= strchr(c1
, ':');
614 *id
|= strtol(c1
, NULL
, 10) << 16; /* channel */
616 c2
= strchr(c1
, ':');
618 *id
|= strtol(c1
, NULL
, 10) << 8; /* lun */
620 *id
|= strtol(c1
, NULL
, 10); /* id */
626 int sysfs_unique_holder(int devnum
, long rdev
)
628 /* Check that devnum is a holder of rdev,
629 * and is the only holder.
630 * we should be locked against races by
631 * an O_EXCL on devnum
638 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
639 major(rdev
), minor(rdev
));
640 dir
= opendir(dirname
);
645 while ((de
= readdir(dir
)) != NULL
) {
654 if (de
->d_name
[0] == '.')
656 strcpy(dirname
+l
, "/");
657 strcat(dirname
+l
, de
->d_name
);
658 strcat(dirname
+l
, "/dev");
659 fd
= open(dirname
, O_RDONLY
);
664 n
= read(fd
, buf
, sizeof(buf
)-1);
667 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||