]>
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
)
94 sprintf(mdi
->sys_name
, "md%d", devnum
);
96 sprintf(mdi
->sys_name
, "md_d%d",
101 struct mdinfo
*sysfs_read(int fd
, int devnum
, unsigned long options
)
103 char fname
[PATH_MAX
];
112 sra
= malloc(sizeof(*sra
));
115 memset(sra
, 0, sizeof(*sra
));
116 sysfs_init(sra
, fd
, devnum
);
117 if (sra
->sys_name
[0] == 0) {
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 strcpy(dev
->sys_name
, de
->d_name
);
271 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
272 if (*ep
) dev
->disk
.raid_disk
= -1;
274 strcpy(dbase
, "block/dev");
275 if (load_sys(fname
, buf
)) {
276 /* assume this is a stale reference to a hot
282 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
284 /* special case check for block devices that can go 'offline' */
285 strcpy(dbase
, "block/device/state");
286 if (load_sys(fname
, buf
) == 0 &&
287 strncmp(buf
, "offline", 7) == 0) {
292 /* finally add this disk to the array */
293 dev
->next
= sra
->devs
;
296 if (options
& GET_OFFSET
) {
297 strcpy(dbase
, "offset");
298 if (load_sys(fname
, buf
))
300 dev
->data_offset
= strtoull(buf
, NULL
, 0);
302 if (options
& GET_SIZE
) {
303 strcpy(dbase
, "size");
304 if (load_sys(fname
, buf
))
306 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
308 if (options
& GET_STATE
) {
310 strcpy(dbase
, "state");
311 if (load_sys(fname
, buf
))
313 if (strstr(buf
, "in_sync"))
314 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
315 if (strstr(buf
, "faulty"))
316 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
317 if (dev
->disk
.state
== 0)
318 sra
->array
.spare_disks
++;
320 if (options
& GET_ERROR
) {
321 strcpy(buf
, "errors");
322 if (load_sys(fname
, buf
))
324 dev
->errors
= strtoul(buf
, NULL
, 0);
337 int sysfs_attr_match(const char *attr
, const char *str
)
339 /* See if attr, read from a sysfs file, matches
340 * str. They must either be the same, or attr can
341 * have a trailing newline or comma
343 while (*attr
&& *str
&& *attr
== *str
) {
348 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
353 int sysfs_match_word(const char *word
, char **list
)
356 for (n
=0; list
[n
]; n
++)
357 if (sysfs_attr_match(word
, list
[n
]))
362 unsigned long long get_component_size(int fd
)
364 /* Find out the component size of the array.
365 * We cannot trust GET_ARRAY_INFO ioctl as it's
366 * size field is only 32bits.
367 * So look in /sys/block/mdXXX/md/component_size
369 * This returns in units of sectors.
374 if (fstat(fd
, &stb
)) return 0;
375 if (major(stb
.st_rdev
) != (unsigned)get_mdp_major())
376 sprintf(fname
, "/sys/block/md%d/md/component_size",
377 (int)minor(stb
.st_rdev
));
379 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
380 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
381 fd
= open(fname
, O_RDONLY
);
384 n
= read(fd
, fname
, sizeof(fname
));
386 if (n
== sizeof(fname
))
389 return strtoull(fname
, NULL
, 10) * 2;
392 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
393 char *name
, char *val
)
399 sprintf(fname
, "/sys/block/%s/md/%s/%s",
400 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
401 fd
= open(fname
, O_WRONLY
);
404 n
= write(fd
, val
, strlen(val
));
406 if (n
!= strlen(val
)) {
407 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
408 val
, fname
, strerror(errno
));
414 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
415 char *name
, unsigned long long val
)
418 sprintf(valstr
, "%llu", val
);
419 return sysfs_set_str(sra
, dev
, name
, valstr
);
422 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
428 sprintf(fname
, "/sys/block/%s/uevent",
430 fd
= open(fname
, O_WRONLY
);
433 n
= write(fd
, event
, strlen(event
));
438 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
443 sprintf(fname
, "/sys/block/%s/md/%s/%s",
444 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
446 return stat(fname
, &st
) == 0;
449 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
455 sprintf(fname
, "/sys/block/%s/md/%s/%s",
456 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
457 fd
= open(fname
, O_RDWR
);
459 fd
= open(fname
, O_RDONLY
);
463 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
470 n
= read(fd
, buf
, sizeof(buf
));
474 *val
= strtoull(buf
, &ep
, 0);
475 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
480 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
481 char *name
, unsigned long long *val
)
486 fd
= sysfs_get_fd(sra
, dev
, name
);
489 n
= sysfs_fd_get_ll(fd
, val
);
494 int sysfs_fd_get_str(int fd
, char *val
, int size
)
499 n
= read(fd
, val
, size
);
506 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
507 char *name
, char *val
, int size
)
512 fd
= sysfs_get_fd(sra
, dev
, name
);
515 n
= sysfs_fd_get_str(fd
, val
, size
);
520 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
529 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
530 /* this '\n' ^ needed for kernels older than 2.6.28 */
531 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
534 int sysfs_set_array(struct mdinfo
*info
, int vers
)
540 if (info
->array
.major_version
== -1 &&
541 info
->array
.minor_version
== -2) {
542 strcat(strcpy(ver
, "external:"), info
->text_version
);
544 if ((vers
% 100) < 2 ||
545 sysfs_set_str(info
, NULL
, "metadata_version",
547 fprintf(stderr
, Name
": This kernel does not "
548 "support external metadata.\n");
552 if (info
->array
.level
< 0)
553 return 0; /* FIXME */
554 rv
|= sysfs_set_str(info
, NULL
, "level",
555 map_num(pers
, info
->array
.level
));
556 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", info
->array
.raid_disks
);
557 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
558 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
559 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
560 if (info
->custom_array_size
) {
563 rc
= sysfs_set_num(info
, NULL
, "array_size",
564 info
->custom_array_size
/2);
565 if (rc
&& errno
== ENOENT
) {
566 fprintf(stderr
, Name
": This kernel does not "
567 "have the md/array_size attribute, "
568 "the array may be larger than expected\n");
574 if (info
->array
.level
> 0)
575 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
579 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
586 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
587 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
591 memset(nm
, 0, sizeof(nm
));
592 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
593 rv
= readlink(dv
, nm
, sizeof(nm
));
597 dname
= strrchr(nm
, '/');
599 strcpy(sd
->sys_name
, "dev-");
600 strcpy(sd
->sys_name
+4, dname
);
602 /* test write to see if 'recovery_start' is available */
603 if (resume
&& sd
->recovery_start
< MaxSector
&&
604 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
605 sysfs_set_str(sra
, sd
, "state", "remove");
609 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
610 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
611 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
612 if (sd
->recovery_start
== MaxSector
)
613 /* This can correctly fail if array isn't started,
614 * yet, so just ignore status for now.
616 sysfs_set_str(sra
, sd
, "state", "insync");
617 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
619 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
625 int sysfs_disk_to_sg(int fd
)
627 /* from an open block device, try find and open its corresponding
628 * scsi_generic interface
633 char sg_major_minor
[8];
637 int major
, minor
, rv
;
642 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
643 major(st
.st_rdev
), minor(st
.st_rdev
));
651 if (strncmp("scsi_generic:", de
->d_name
,
652 strlen("scsi_generic:")) == 0)
661 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
662 fd
= open(sg_path
, O_RDONLY
);
666 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
671 sg_major_minor
[rv
- 1] = '\0';
673 c
= strchr(sg_major_minor
, ':');
676 major
= strtol(sg_major_minor
, NULL
, 10);
677 minor
= strtol(c
, NULL
, 10);
678 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
679 (int) getpid(), major
, minor
);
680 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
681 fd
= open(path
, O_RDONLY
);
690 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
692 /* from an open block device, try to retrieve it scsi_id */
702 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
703 major(st
.st_rdev
), minor(st
.st_rdev
));
711 if (strncmp("scsi_disk:", de
->d_name
,
712 strlen("scsi_disk:")) == 0)
721 c1
= strchr(de
->d_name
, ':');
723 c2
= strchr(c1
, ':');
725 *id
= strtol(c1
, NULL
, 10) << 24; /* host */
727 c2
= strchr(c1
, ':');
729 *id
|= strtol(c1
, NULL
, 10) << 16; /* channel */
731 c2
= strchr(c1
, ':');
733 *id
|= strtol(c1
, NULL
, 10) << 8; /* lun */
735 *id
|= strtol(c1
, NULL
, 10); /* id */
741 int sysfs_unique_holder(int devnum
, long rdev
)
743 /* Check that devnum is a holder of rdev,
744 * and is the only holder.
745 * we should be locked against races by
746 * an O_EXCL on devnum
753 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
754 major(rdev
), minor(rdev
));
755 dir
= opendir(dirname
);
760 while ((de
= readdir(dir
)) != NULL
) {
769 if (de
->d_name
[0] == '.')
771 strcpy(dirname
+l
, "/");
772 strcat(dirname
+l
, de
->d_name
);
773 strcat(dirname
+l
, "/dev");
774 fd
= open(dirname
, O_RDONLY
);
779 n
= read(fd
, buf
, sizeof(buf
)-1);
782 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||
803 int sysfs_freeze_array(struct mdinfo
*sra
)
805 /* Try to freeze resync/rebuild on this array/container.
806 * Return -1 if the array is busy,
807 * return -2 container cannot be frozen,
808 * return 0 if this kernel doesn't support 'frozen'
809 * return 1 if it worked.
813 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
814 return 1; /* no sync_action == frozen */
815 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
817 if (strcmp(buf
, "idle\n") != 0 &&
818 strcmp(buf
, "frozen\n") != 0)
820 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
827 static char *clean_states
[] = {
828 "clear", "inactive", "readonly", "read-auto", "clean", NULL
};
830 int WaitClean(char *dev
, int sock
, int verbose
)
837 fd
= open(dev
, O_RDONLY
);
840 fprintf(stderr
, Name
": Couldn't open %s: %s\n", dev
, strerror(errno
));
844 devnum
= fd2devnum(fd
);
845 mdi
= sysfs_read(fd
, devnum
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
848 fprintf(stderr
, Name
": Failed to read sysfs attributes for "
854 switch(mdi
->array
.level
) {
856 case LEVEL_MULTIPATH
:
858 /* safemode delay is irrelevant for these levels */
863 /* for internal metadata the kernel handles the final clean
864 * transition, containers can never be dirty
866 if (!is_subarray(mdi
->text_version
))
869 /* safemode disabled ? */
870 if (mdi
->safe_mode_delay
== 0)
874 int state_fd
= sysfs_open(fd2devnum(fd
), NULL
, "array_state");
879 /* minimize the safe_mode_delay and prepare to wait up to 5s
880 * for writes to quiesce
882 sysfs_set_safemode(mdi
, 1);
888 /* wait for array_state to be clean */
890 rv
= read(state_fd
, buf
, sizeof(buf
));
893 if (sysfs_match_word(buf
, clean_states
) <= 4)
895 FD_SET(state_fd
, &fds
);
896 rv
= select(state_fd
+ 1, NULL
, NULL
, &fds
, &tm
);
897 if (rv
< 0 && errno
!= EINTR
)
899 lseek(state_fd
, 0, SEEK_SET
);
903 else if (fping_monitor(sock
) == 0 ||
904 ping_monitor(mdi
->text_version
) == 0) {
905 /* we need to ping to close the window between array
906 * state transitioning to clean and the metadata being
913 fprintf(stderr
, Name
": Error waiting for %s to be clean\n",
916 /* restore the original safe_mode_delay */
917 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);
926 #endif /* MDASSEMBLE */