]>
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-2009 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
)
87 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
89 devnum
= fd2devnum(fd
);
91 if (devnum
== NoMdDev
)
93 fmt_devname(mdi
->sys_name
, devnum
);
97 struct mdinfo
*sysfs_read(int fd
, int devnum
, unsigned long options
)
108 sra
= malloc(sizeof(*sra
));
111 memset(sra
, 0, sizeof(*sra
));
112 sysfs_init(sra
, fd
, devnum
);
113 if (sra
->sys_name
[0] == 0) {
118 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
119 base
= fname
+ strlen(fname
);
122 if (options
& GET_VERSION
) {
123 strcpy(base
, "metadata_version");
124 if (load_sys(fname
, buf
))
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);
136 &sra
->array
.major_version
,
137 &sra
->array
.minor_version
);
138 strcpy(sra
->text_version
, buf
);
141 if (options
& GET_LEVEL
) {
142 strcpy(base
, "level");
143 if (load_sys(fname
, buf
))
145 sra
->array
.level
= map_name(pers
, buf
);
147 if (options
& GET_LAYOUT
) {
148 strcpy(base
, "layout");
149 if (load_sys(fname
, buf
))
151 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
153 if (options
& GET_DISKS
) {
154 strcpy(base
, "raid_disks");
155 if (load_sys(fname
, buf
))
157 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
159 if (options
& GET_DEGRADED
) {
160 strcpy(base
, "degraded");
161 if (load_sys(fname
, buf
))
163 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
165 if (options
& GET_COMPONENT
) {
166 strcpy(base
, "component_size");
167 if (load_sys(fname
, buf
))
169 sra
->component_size
= strtoull(buf
, NULL
, 0);
170 /* sysfs reports "K", but we want sectors */
171 sra
->component_size
*= 2;
173 if (options
& GET_CHUNK
) {
174 strcpy(base
, "chunk_size");
175 if (load_sys(fname
, buf
))
177 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
179 if (options
& GET_CACHE
) {
180 strcpy(base
, "stripe_cache_size");
181 if (load_sys(fname
, buf
))
183 sra
->cache_size
= strtoul(buf
, NULL
, 0);
185 if (options
& GET_MISMATCH
) {
186 strcpy(base
, "mismatch_cnt");
187 if (load_sys(fname
, buf
))
189 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
191 if (options
& GET_SAFEMODE
) {
198 strcpy(base
, "safe_mode_delay");
199 if (load_sys(fname
, buf
))
202 /* remove a period, and count digits after it */
204 for (i
= 0; i
< len
; i
++) {
206 if (isdigit(buf
[i
])) {
211 } else if (buf
[i
] == '.') {
216 msec
= strtoul(buf
, NULL
, 10);
217 msec
= (msec
* 1000) / scale
;
218 sra
->safe_mode_delay
= msec
;
220 if (options
& GET_BITMAP_LOCATION
) {
221 strcpy(base
, "bitmap/location");
222 if (load_sys(fname
, buf
))
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
= strtoul(buf
+1, NULL
, 10);
234 if (! (options
& GET_DEVS
))
237 /* Get all the devices as well */
239 dir
= opendir(fname
);
242 sra
->array
.spare_disks
= 0;
244 while ((de
= readdir(dir
)) != NULL
) {
246 if (de
->d_ino
== 0 ||
247 strncmp(de
->d_name
, "dev-", 4) != 0)
249 strcpy(base
, de
->d_name
);
250 dbase
= base
+ strlen(base
);
253 dev
= malloc(sizeof(*dev
));
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
263 strcpy(dbase
, "block");
264 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
265 errno
!= ENAMETOOLONG
) {
266 /* ...yup device is gone */
270 /* slot is unreadable but 'block' link
271 * still intact... something bad is happening
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;
283 strcpy(dbase
, "block/dev");
284 if (load_sys(fname
, buf
)) {
285 /* assume this is a stale reference to a hot
291 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
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) {
301 /* finally add this disk to the array */
302 dev
->next
= sra
->devs
;
305 if (options
& GET_OFFSET
) {
306 strcpy(dbase
, "offset");
307 if (load_sys(fname
, buf
))
309 dev
->data_offset
= strtoull(buf
, NULL
, 0);
311 if (options
& GET_SIZE
) {
312 strcpy(dbase
, "size");
313 if (load_sys(fname
, buf
))
315 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
317 if (options
& GET_STATE
) {
319 strcpy(dbase
, "state");
320 if (load_sys(fname
, buf
))
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
++;
329 if (options
& GET_ERROR
) {
330 strcpy(buf
, "errors");
331 if (load_sys(fname
, buf
))
333 dev
->errors
= strtoul(buf
, NULL
, 0);
346 int sysfs_attr_match(const char *attr
, const char *str
)
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
352 while (*attr
&& *str
&& *attr
== *str
) {
357 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
362 int sysfs_match_word(const char *word
, char **list
)
365 for (n
=0; list
[n
]; n
++)
366 if (sysfs_attr_match(word
, list
[n
]))
371 unsigned long long get_component_size(int fd
)
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
378 * This returns in units of sectors.
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
));
388 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
389 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
390 fd
= open(fname
, O_RDONLY
);
393 n
= read(fd
, fname
, sizeof(fname
));
395 if (n
< 0 || n
== sizeof(fname
))
398 return strtoull(fname
, NULL
, 10) * 2;
401 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
402 char *name
, char *val
)
408 sprintf(fname
, "/sys/block/%s/md/%s/%s",
409 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
410 fd
= open(fname
, O_WRONLY
);
413 n
= write(fd
, val
, strlen(val
));
415 if (n
!= strlen(val
)) {
416 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
417 val
, fname
, strerror(errno
));
423 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
424 char *name
, unsigned long long val
)
427 sprintf(valstr
, "%llu", val
);
428 return sysfs_set_str(sra
, dev
, name
, valstr
);
431 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
437 sprintf(fname
, "/sys/block/%s/uevent",
439 fd
= open(fname
, O_WRONLY
);
442 n
= write(fd
, event
, strlen(event
));
444 if (n
!= (int)strlen(event
)) {
445 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
446 event
, fname
, strerror(errno
));
452 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
457 sprintf(fname
, "/sys/block/%s/md/%s/%s",
458 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
460 return stat(fname
, &st
) == 0;
463 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
469 sprintf(fname
, "/sys/block/%s/md/%s/%s",
470 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
471 fd
= open(fname
, O_RDWR
);
473 fd
= open(fname
, O_RDONLY
);
477 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
484 n
= read(fd
, buf
, sizeof(buf
));
488 *val
= strtoull(buf
, &ep
, 0);
489 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
494 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
495 char *name
, unsigned long long *val
)
500 fd
= sysfs_get_fd(sra
, dev
, name
);
503 n
= sysfs_fd_get_ll(fd
, val
);
508 int sysfs_fd_get_str(int fd
, char *val
, int size
)
513 n
= read(fd
, val
, size
);
520 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
521 char *name
, char *val
, int size
)
526 fd
= sysfs_get_fd(sra
, dev
, name
);
529 n
= sysfs_fd_get_str(fd
, val
, size
);
534 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
543 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
544 /* this '\n' ^ needed for kernels older than 2.6.28 */
545 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
548 int sysfs_set_array(struct mdinfo
*info
, int vers
)
552 int raid_disks
= info
->array
.raid_disks
;
555 if (info
->array
.major_version
== -1 &&
556 info
->array
.minor_version
== -2) {
559 strcat(strcpy(ver
, "external:"), info
->text_version
);
561 /* meta version might already be set if we are setting
562 * new geometry for a reshape. In that case we don't
563 * want to over-write the 'readonly' flag that is
564 * stored in the metadata version. So read the current
565 * version first, and preserve the flag
567 if (sysfs_get_str(info
, NULL
, "metadata_version",
569 if (strlen(buf
) >= 9 && buf
[9] == '-')
572 if ((vers
% 100) < 2 ||
573 sysfs_set_str(info
, NULL
, "metadata_version",
575 fprintf(stderr
, Name
": This kernel does not "
576 "support external metadata.\n");
580 if (info
->array
.level
< 0)
581 return 0; /* FIXME */
582 rv
|= sysfs_set_str(info
, NULL
, "level",
583 map_num(pers
, info
->array
.level
));
584 if (info
->reshape_active
&& info
->delta_disks
!= UnSet
)
585 raid_disks
-= info
->delta_disks
;
586 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", raid_disks
);
587 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
588 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
589 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
590 if (info
->custom_array_size
) {
593 rc
= sysfs_set_num(info
, NULL
, "array_size",
594 info
->custom_array_size
/2);
595 if (rc
&& errno
== ENOENT
) {
596 fprintf(stderr
, Name
": This kernel does not "
597 "have the md/array_size attribute, "
598 "the array may be larger than expected\n");
604 if (info
->array
.level
> 0)
605 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
607 if (info
->reshape_active
) {
608 rv
|= sysfs_set_num(info
, NULL
, "reshape_position",
609 info
->reshape_progress
);
610 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->new_chunk
);
611 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->new_layout
);
612 rv
|= sysfs_set_num(info
, NULL
, "raid_disks",
613 info
->array
.raid_disks
);
614 /* We don't set 'new_level' here. That can only happen
615 * once the reshape completes.
621 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
628 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
629 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
633 memset(nm
, 0, sizeof(nm
));
634 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
635 rv
= readlink(dv
, nm
, sizeof(nm
)-1);
639 dname
= strrchr(nm
, '/');
641 strcpy(sd
->sys_name
, "dev-");
642 strcpy(sd
->sys_name
+4, dname
);
644 /* test write to see if 'recovery_start' is available */
645 if (resume
&& sd
->recovery_start
< MaxSector
&&
646 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
647 sysfs_set_str(sra
, sd
, "state", "remove");
651 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
652 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
653 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
654 if (sd
->recovery_start
== MaxSector
)
655 /* This can correctly fail if array isn't started,
656 * yet, so just ignore status for now.
658 sysfs_set_str(sra
, sd
, "state", "insync");
659 if (sd
->disk
.raid_disk
>= 0)
660 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
662 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
668 int sysfs_disk_to_sg(int fd
)
670 /* from an open block device, try find and open its corresponding
671 * scsi_generic interface
676 char sg_major_minor
[8];
680 int major
, minor
, rv
;
685 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
686 major(st
.st_rdev
), minor(st
.st_rdev
));
694 if (strncmp("scsi_generic:", de
->d_name
,
695 strlen("scsi_generic:")) == 0)
704 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
705 fd
= open(sg_path
, O_RDONLY
);
709 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
714 sg_major_minor
[rv
- 1] = '\0';
716 c
= strchr(sg_major_minor
, ':');
719 major
= strtol(sg_major_minor
, NULL
, 10);
720 minor
= strtol(c
, NULL
, 10);
721 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
722 (int) getpid(), major
, minor
);
723 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
724 fd
= open(path
, O_RDONLY
);
733 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
735 /* from an open block device, try to retrieve it scsi_id */
740 int host
, bus
, target
, lun
;
745 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device/scsi_device",
746 major(st
.st_rdev
), minor(st
.st_rdev
));
752 for (de
= readdir(dir
); de
; de
= readdir(dir
)) {
755 if (de
->d_type
!= DT_DIR
)
758 count
= sscanf(de
->d_name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
);
767 *id
= (host
<< 24) | (bus
<< 16) | (target
<< 8) | (lun
<< 0);
772 int sysfs_unique_holder(int devnum
, long rdev
)
774 /* Check that devnum is a holder of rdev,
775 * and is the only holder.
776 * we should be locked against races by
777 * an O_EXCL on devnum
784 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
785 major(rdev
), minor(rdev
));
786 dir
= opendir(dirname
);
791 while ((de
= readdir(dir
)) != NULL
) {
800 if (de
->d_name
[0] == '.')
802 strcpy(dirname
+l
, "/");
803 strcat(dirname
+l
, de
->d_name
);
804 strcat(dirname
+l
, "/dev");
805 fd
= open(dirname
, O_RDONLY
);
810 n
= read(fd
, buf
, sizeof(buf
)-1);
815 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||
836 int sysfs_freeze_array(struct mdinfo
*sra
)
838 /* Try to freeze resync/rebuild on this array/container.
839 * Return -1 if the array is busy,
840 * return -2 container cannot be frozen,
841 * return 0 if this kernel doesn't support 'frozen'
842 * return 1 if it worked.
846 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
847 return 1; /* no sync_action == frozen */
848 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
850 if (strcmp(buf
, "idle\n") != 0 &&
851 strcmp(buf
, "frozen\n") != 0)
853 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)