]>
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
;
221 if (! (options
& GET_DEVS
))
224 /* Get all the devices as well */
226 dir
= opendir(fname
);
229 sra
->array
.spare_disks
= 0;
231 while ((de
= readdir(dir
)) != NULL
) {
233 if (de
->d_ino
== 0 ||
234 strncmp(de
->d_name
, "dev-", 4) != 0)
236 strcpy(base
, de
->d_name
);
237 dbase
= base
+ strlen(base
);
240 dev
= malloc(sizeof(*dev
));
244 /* Always get slot, major, minor */
245 strcpy(dbase
, "slot");
246 if (load_sys(fname
, buf
)) {
247 /* hmm... unable to read 'slot' maybe the device
250 strcpy(dbase
, "block");
251 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
252 errno
!= ENAMETOOLONG
) {
253 /* ...yup device is gone */
257 /* slot is unreadable but 'block' link
258 * still intact... something bad is happening
266 strcpy(dev
->sys_name
, de
->d_name
);
267 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
268 if (*ep
) dev
->disk
.raid_disk
= -1;
270 strcpy(dbase
, "block/dev");
271 if (load_sys(fname
, buf
)) {
272 /* assume this is a stale reference to a hot
278 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
280 /* special case check for block devices that can go 'offline' */
281 strcpy(dbase
, "block/device/state");
282 if (load_sys(fname
, buf
) == 0 &&
283 strncmp(buf
, "offline", 7) == 0) {
288 /* finally add this disk to the array */
289 dev
->next
= sra
->devs
;
292 if (options
& GET_OFFSET
) {
293 strcpy(dbase
, "offset");
294 if (load_sys(fname
, buf
))
296 dev
->data_offset
= strtoull(buf
, NULL
, 0);
298 if (options
& GET_SIZE
) {
299 strcpy(dbase
, "size");
300 if (load_sys(fname
, buf
))
302 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
304 if (options
& GET_STATE
) {
306 strcpy(dbase
, "state");
307 if (load_sys(fname
, buf
))
309 if (strstr(buf
, "in_sync"))
310 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
311 if (strstr(buf
, "faulty"))
312 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
313 if (dev
->disk
.state
== 0)
314 sra
->array
.spare_disks
++;
316 if (options
& GET_ERROR
) {
317 strcpy(buf
, "errors");
318 if (load_sys(fname
, buf
))
320 dev
->errors
= strtoul(buf
, NULL
, 0);
333 int sysfs_attr_match(const char *attr
, const char *str
)
335 /* See if attr, read from a sysfs file, matches
336 * str. They must either be the same, or attr can
337 * have a trailing newline or comma
339 while (*attr
&& *str
&& *attr
== *str
) {
344 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
349 int sysfs_match_word(const char *word
, char **list
)
352 for (n
=0; list
[n
]; n
++)
353 if (sysfs_attr_match(word
, list
[n
]))
358 unsigned long long get_component_size(int fd
)
360 /* Find out the component size of the array.
361 * We cannot trust GET_ARRAY_INFO ioctl as it's
362 * size field is only 32bits.
363 * So look in /sys/block/mdXXX/md/component_size
365 * This returns in units of sectors.
370 if (fstat(fd
, &stb
)) return 0;
371 if (major(stb
.st_rdev
) != (unsigned)get_mdp_major())
372 sprintf(fname
, "/sys/block/md%d/md/component_size",
373 (int)minor(stb
.st_rdev
));
375 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
376 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
377 fd
= open(fname
, O_RDONLY
);
380 n
= read(fd
, fname
, sizeof(fname
));
382 if (n
== sizeof(fname
))
385 return strtoull(fname
, NULL
, 10) * 2;
388 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
389 char *name
, char *val
)
395 sprintf(fname
, "/sys/block/%s/md/%s/%s",
396 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
397 fd
= open(fname
, O_WRONLY
);
400 n
= write(fd
, val
, strlen(val
));
402 if (n
!= strlen(val
)) {
403 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
404 val
, fname
, strerror(errno
));
410 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
411 char *name
, unsigned long long val
)
414 sprintf(valstr
, "%llu", val
);
415 return sysfs_set_str(sra
, dev
, name
, valstr
);
418 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
424 sprintf(fname
, "/sys/block/%s/uevent",
426 fd
= open(fname
, O_WRONLY
);
429 n
= write(fd
, event
, strlen(event
));
434 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
439 sprintf(fname
, "/sys/block/%s/md/%s/%s",
440 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
442 return stat(fname
, &st
) == 0;
445 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
451 sprintf(fname
, "/sys/block/%s/md/%s/%s",
452 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
453 fd
= open(fname
, O_RDWR
);
455 fd
= open(fname
, O_RDONLY
);
459 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
466 n
= read(fd
, buf
, sizeof(buf
));
470 *val
= strtoull(buf
, &ep
, 0);
471 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
476 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
477 char *name
, unsigned long long *val
)
482 fd
= sysfs_get_fd(sra
, dev
, name
);
485 n
= sysfs_fd_get_ll(fd
, val
);
490 int sysfs_fd_get_str(int fd
, char *val
, int size
)
495 n
= read(fd
, val
, size
);
502 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
503 char *name
, char *val
, int size
)
508 fd
= sysfs_get_fd(sra
, dev
, name
);
511 n
= sysfs_fd_get_str(fd
, val
, size
);
516 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
525 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
526 /* this '\n' ^ needed for kernels older than 2.6.28 */
527 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
530 int sysfs_set_array(struct mdinfo
*info
, int vers
)
536 if (info
->array
.major_version
== -1 &&
537 info
->array
.minor_version
== -2) {
538 strcat(strcpy(ver
, "external:"), info
->text_version
);
540 if ((vers
% 100) < 2 ||
541 sysfs_set_str(info
, NULL
, "metadata_version",
543 fprintf(stderr
, Name
": This kernel does not "
544 "support external metadata.\n");
548 if (info
->array
.level
< 0)
549 return 0; /* FIXME */
550 rv
|= sysfs_set_str(info
, NULL
, "level",
551 map_num(pers
, info
->array
.level
));
552 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", info
->array
.raid_disks
);
553 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
554 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
555 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
556 if (info
->custom_array_size
) {
559 rc
= sysfs_set_num(info
, NULL
, "array_size",
560 info
->custom_array_size
/2);
561 if (rc
&& errno
== ENOENT
) {
562 fprintf(stderr
, Name
": This kernel does not "
563 "have the md/array_size attribute, "
564 "the array may be larger than expected\n");
570 if (info
->array
.level
> 0)
571 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
575 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
582 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
583 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
587 memset(nm
, 0, sizeof(nm
));
588 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
589 rv
= readlink(dv
, nm
, sizeof(nm
));
593 dname
= strrchr(nm
, '/');
595 strcpy(sd
->sys_name
, "dev-");
596 strcpy(sd
->sys_name
+4, dname
);
598 /* test write to see if 'recovery_start' is available */
599 if (resume
&& sd
->recovery_start
< MaxSector
&&
600 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
601 sysfs_set_str(sra
, sd
, "state", "remove");
605 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
606 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
607 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
608 if (sd
->recovery_start
== MaxSector
)
609 /* This can correctly fail if array isn't started,
610 * yet, so just ignore status for now.
612 sysfs_set_str(sra
, sd
, "state", "insync");
613 if (sd
->disk
.raid_disk
>= 0)
614 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
616 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
622 int sysfs_disk_to_sg(int fd
)
624 /* from an open block device, try find and open its corresponding
625 * scsi_generic interface
630 char sg_major_minor
[8];
634 int major
, minor
, rv
;
639 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
640 major(st
.st_rdev
), minor(st
.st_rdev
));
648 if (strncmp("scsi_generic:", de
->d_name
,
649 strlen("scsi_generic:")) == 0)
658 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
659 fd
= open(sg_path
, O_RDONLY
);
663 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
668 sg_major_minor
[rv
- 1] = '\0';
670 c
= strchr(sg_major_minor
, ':');
673 major
= strtol(sg_major_minor
, NULL
, 10);
674 minor
= strtol(c
, NULL
, 10);
675 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
676 (int) getpid(), major
, minor
);
677 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
678 fd
= open(path
, O_RDONLY
);
687 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
689 /* from an open block device, try to retrieve it scsi_id */
699 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
700 major(st
.st_rdev
), minor(st
.st_rdev
));
708 if (strncmp("scsi_disk:", de
->d_name
,
709 strlen("scsi_disk:")) == 0)
718 c1
= strchr(de
->d_name
, ':');
720 c2
= strchr(c1
, ':');
722 *id
= strtol(c1
, NULL
, 10) << 24; /* host */
724 c2
= strchr(c1
, ':');
726 *id
|= strtol(c1
, NULL
, 10) << 16; /* channel */
728 c2
= strchr(c1
, ':');
730 *id
|= strtol(c1
, NULL
, 10) << 8; /* lun */
732 *id
|= strtol(c1
, NULL
, 10); /* id */
738 int sysfs_unique_holder(int devnum
, long rdev
)
740 /* Check that devnum is a holder of rdev,
741 * and is the only holder.
742 * we should be locked against races by
743 * an O_EXCL on devnum
750 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
751 major(rdev
), minor(rdev
));
752 dir
= opendir(dirname
);
757 while ((de
= readdir(dir
)) != NULL
) {
766 if (de
->d_name
[0] == '.')
768 strcpy(dirname
+l
, "/");
769 strcat(dirname
+l
, de
->d_name
);
770 strcat(dirname
+l
, "/dev");
771 fd
= open(dirname
, O_RDONLY
);
776 n
= read(fd
, buf
, sizeof(buf
)-1);
779 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||
800 int sysfs_freeze_array(struct mdinfo
*sra
)
802 /* Try to freeze resync/rebuild on this array/container.
803 * Return -1 if the array is busy,
804 * return -2 container cannot be frozen,
805 * return 0 if this kernel doesn't support 'frozen'
806 * return 1 if it worked.
810 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
811 return 1; /* no sync_action == frozen */
812 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
814 if (strcmp(buf
, "idle\n") != 0 &&
815 strcmp(buf
, "frozen\n") != 0)
817 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
824 static char *clean_states
[] = {
825 "clear", "inactive", "readonly", "read-auto", "clean", NULL
};
827 int WaitClean(char *dev
, int sock
, int verbose
)
834 fd
= open(dev
, O_RDONLY
);
837 fprintf(stderr
, Name
": Couldn't open %s: %s\n", dev
, strerror(errno
));
841 devnum
= fd2devnum(fd
);
842 mdi
= sysfs_read(fd
, devnum
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
845 fprintf(stderr
, Name
": Failed to read sysfs attributes for "
851 switch(mdi
->array
.level
) {
853 case LEVEL_MULTIPATH
:
855 /* safemode delay is irrelevant for these levels */
860 /* for internal metadata the kernel handles the final clean
861 * transition, containers can never be dirty
863 if (!is_subarray(mdi
->text_version
))
866 /* safemode disabled ? */
867 if (mdi
->safe_mode_delay
== 0)
871 int state_fd
= sysfs_open(fd2devnum(fd
), NULL
, "array_state");
876 /* minimize the safe_mode_delay and prepare to wait up to 5s
877 * for writes to quiesce
879 sysfs_set_safemode(mdi
, 1);
885 /* wait for array_state to be clean */
887 rv
= read(state_fd
, buf
, sizeof(buf
));
890 if (sysfs_match_word(buf
, clean_states
) <= 4)
892 FD_SET(state_fd
, &fds
);
893 rv
= select(state_fd
+ 1, NULL
, NULL
, &fds
, &tm
);
894 if (rv
< 0 && errno
!= EINTR
)
896 lseek(state_fd
, 0, SEEK_SET
);
900 else if (fping_monitor(sock
) == 0 ||
901 ping_monitor(mdi
->text_version
) == 0) {
902 /* we need to ping to close the window between array
903 * state transitioning to clean and the metadata being
910 fprintf(stderr
, Name
": Error waiting for %s to be clean\n",
913 /* restore the original safe_mode_delay */
914 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);
923 #endif /* MDASSEMBLE */