]>
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(char *devnm
, char *devname
, char *attr
)
65 sprintf(fname
, "/sys/block/%s/md/", devnm
);
67 strcat(fname
, devname
);
71 fd
= open(fname
, O_RDWR
);
72 if (fd
< 0 && errno
== EACCES
)
73 fd
= open(fname
, O_RDONLY
);
77 void sysfs_init_dev(struct mdinfo
*mdi
, unsigned long devid
)
79 snprintf(mdi
->sys_name
,
80 sizeof(mdi
->sys_name
), "dev-%s", devid2kname(devid
));
83 void sysfs_init(struct mdinfo
*mdi
, int fd
, char *devnm
)
88 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
94 strcpy(mdi
->sys_name
, devnm
);
97 struct mdinfo
*sysfs_read(int fd
, char *devnm
, unsigned long options
)
104 struct mdinfo
*dev
, **devp
;
108 sra
= xcalloc(1, sizeof(*sra
));
109 sysfs_init(sra
, fd
, devnm
);
110 if (sra
->sys_name
[0] == 0) {
115 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
116 base
= fname
+ strlen(fname
);
119 if (options
& GET_VERSION
) {
120 strcpy(base
, "metadata_version");
121 if (load_sys(fname
, buf
))
123 if (strncmp(buf
, "none", 4) == 0) {
124 sra
->array
.major_version
=
125 sra
->array
.minor_version
= -1;
126 strcpy(sra
->text_version
, "");
127 } else if (strncmp(buf
, "external:", 9) == 0) {
128 sra
->array
.major_version
= -1;
129 sra
->array
.minor_version
= -2;
130 strcpy(sra
->text_version
, buf
+9);
133 &sra
->array
.major_version
,
134 &sra
->array
.minor_version
);
135 strcpy(sra
->text_version
, buf
);
138 if (options
& GET_LEVEL
) {
139 strcpy(base
, "level");
140 if (load_sys(fname
, buf
))
142 sra
->array
.level
= map_name(pers
, buf
);
144 if (options
& GET_LAYOUT
) {
145 strcpy(base
, "layout");
146 if (load_sys(fname
, buf
))
148 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
150 if (options
& GET_DISKS
) {
151 strcpy(base
, "raid_disks");
152 if (load_sys(fname
, buf
))
154 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
156 if (options
& GET_DEGRADED
) {
157 strcpy(base
, "degraded");
158 if (load_sys(fname
, buf
))
160 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
162 if (options
& GET_COMPONENT
) {
163 strcpy(base
, "component_size");
164 if (load_sys(fname
, buf
))
166 sra
->component_size
= strtoull(buf
, NULL
, 0);
167 /* sysfs reports "K", but we want sectors */
168 sra
->component_size
*= 2;
170 if (options
& GET_CHUNK
) {
171 strcpy(base
, "chunk_size");
172 if (load_sys(fname
, buf
))
174 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
176 if (options
& GET_CACHE
) {
177 strcpy(base
, "stripe_cache_size");
178 if (load_sys(fname
, buf
))
179 /* Probably level doesn't support it */
182 sra
->cache_size
= strtoul(buf
, NULL
, 0);
184 if (options
& GET_MISMATCH
) {
185 strcpy(base
, "mismatch_cnt");
186 if (load_sys(fname
, buf
))
188 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
190 if (options
& GET_SAFEMODE
) {
197 strcpy(base
, "safe_mode_delay");
198 if (load_sys(fname
, buf
))
201 /* remove a period, and count digits after it */
203 for (i
= 0; i
< len
; i
++) {
205 if (isdigit(buf
[i
])) {
210 } else if (buf
[i
] == '.') {
215 msec
= strtoul(buf
, NULL
, 10);
216 msec
= (msec
* 1000) / scale
;
217 sra
->safe_mode_delay
= msec
;
219 if (options
& GET_BITMAP_LOCATION
) {
220 strcpy(base
, "bitmap/location");
221 if (load_sys(fname
, buf
))
223 if (strncmp(buf
, "file", 4) == 0)
224 sra
->bitmap_offset
= 1;
225 else if (strncmp(buf
, "none", 4) == 0)
226 sra
->bitmap_offset
= 0;
227 else if (buf
[0] == '+')
228 sra
->bitmap_offset
= strtol(buf
+1, NULL
, 10);
233 if (! (options
& GET_DEVS
))
236 /* Get all the devices as well */
238 dir
= opendir(fname
);
241 sra
->array
.spare_disks
= 0;
245 while ((de
= readdir(dir
)) != NULL
) {
247 if (de
->d_ino
== 0 ||
248 strncmp(de
->d_name
, "dev-", 4) != 0)
250 strcpy(base
, de
->d_name
);
251 dbase
= base
+ strlen(base
);
254 dev
= xmalloc(sizeof(*dev
));
256 /* Always get slot, major, minor */
257 strcpy(dbase
, "slot");
258 if (load_sys(fname
, buf
)) {
259 /* hmm... unable to read 'slot' maybe the device
262 strcpy(dbase
, "block");
263 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
264 errno
!= ENAMETOOLONG
) {
265 /* ...yup device is gone */
269 /* slot is unreadable but 'block' link
270 * still intact... something bad is happening
278 strcpy(dev
->sys_name
, de
->d_name
);
279 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
280 if (*ep
) dev
->disk
.raid_disk
= -1;
282 strcpy(dbase
, "block/dev");
283 if (load_sys(fname
, buf
)) {
284 /* assume this is a stale reference to a hot
290 sra
->array
.nr_disks
++;
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 */
306 if (options
& GET_OFFSET
) {
307 strcpy(dbase
, "offset");
308 if (load_sys(fname
, buf
))
310 dev
->data_offset
= strtoull(buf
, NULL
, 0);
311 strcpy(dbase
, "new_offset");
312 if (load_sys(fname
, buf
) == 0)
313 dev
->new_data_offset
= strtoull(buf
, NULL
, 0);
315 dev
->new_data_offset
= dev
->data_offset
;
317 if (options
& GET_SIZE
) {
318 strcpy(dbase
, "size");
319 if (load_sys(fname
, buf
))
321 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
323 if (options
& GET_STATE
) {
325 strcpy(dbase
, "state");
326 if (load_sys(fname
, buf
))
328 if (strstr(buf
, "in_sync"))
329 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
330 if (strstr(buf
, "faulty"))
331 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
332 if (dev
->disk
.state
== 0)
333 sra
->array
.spare_disks
++;
335 if (options
& GET_ERROR
) {
336 strcpy(buf
, "errors");
337 if (load_sys(fname
, buf
))
339 dev
->errors
= strtoul(buf
, NULL
, 0);
352 int sysfs_attr_match(const char *attr
, const char *str
)
354 /* See if attr, read from a sysfs file, matches
355 * str. They must either be the same, or attr can
356 * have a trailing newline or comma
358 while (*attr
&& *str
&& *attr
== *str
) {
363 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
368 int sysfs_match_word(const char *word
, char **list
)
371 for (n
=0; list
[n
]; n
++)
372 if (sysfs_attr_match(word
, list
[n
]))
377 unsigned long long get_component_size(int fd
)
379 /* Find out the component size of the array.
380 * We cannot trust GET_ARRAY_INFO ioctl as it's
381 * size field is only 32bits.
382 * So look in /sys/block/mdXXX/md/component_size
384 * This returns in units of sectors.
389 if (fstat(fd
, &stb
)) return 0;
390 if (major(stb
.st_rdev
) != (unsigned)get_mdp_major())
391 sprintf(fname
, "/sys/block/md%d/md/component_size",
392 (int)minor(stb
.st_rdev
));
394 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
395 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
396 fd
= open(fname
, O_RDONLY
);
399 n
= read(fd
, fname
, sizeof(fname
));
401 if (n
< 0 || n
== sizeof(fname
))
404 return strtoull(fname
, NULL
, 10) * 2;
407 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
408 char *name
, char *val
)
414 sprintf(fname
, "/sys/block/%s/md/%s/%s",
415 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
416 fd
= open(fname
, O_WRONLY
);
419 n
= write(fd
, val
, strlen(val
));
421 if (n
!= strlen(val
)) {
422 dprintf("failed to write '%s' to '%s' (%s)\n",
423 val
, fname
, strerror(errno
));
429 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
430 char *name
, unsigned long long val
)
433 sprintf(valstr
, "%llu", val
);
434 return sysfs_set_str(sra
, dev
, name
, valstr
);
437 int sysfs_set_num_signed(struct mdinfo
*sra
, struct mdinfo
*dev
,
438 char *name
, long long val
)
441 sprintf(valstr
, "%lli", val
);
442 return sysfs_set_str(sra
, dev
, name
, valstr
);
445 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
451 sprintf(fname
, "/sys/block/%s/uevent",
453 fd
= open(fname
, O_WRONLY
);
456 n
= write(fd
, event
, strlen(event
));
458 if (n
!= (int)strlen(event
)) {
459 dprintf("failed to write '%s' to '%s' (%s)\n",
460 event
, fname
, strerror(errno
));
466 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
471 sprintf(fname
, "/sys/block/%s/md/%s/%s",
472 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
474 return stat(fname
, &st
) == 0;
477 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
483 sprintf(fname
, "/sys/block/%s/md/%s/%s",
484 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
485 fd
= open(fname
, O_RDWR
);
487 fd
= open(fname
, O_RDONLY
);
491 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
498 n
= read(fd
, buf
, sizeof(buf
));
499 if (n
<= 0 || n
== sizeof(buf
))
502 *val
= strtoull(buf
, &ep
, 0);
503 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
508 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
509 char *name
, unsigned long long *val
)
514 fd
= sysfs_get_fd(sra
, dev
, name
);
517 n
= sysfs_fd_get_ll(fd
, val
);
522 int sysfs_fd_get_two(int fd
, unsigned long long *v1
, unsigned long long *v2
)
524 /* two numbers in this sysfs file, either
534 n
= read(fd
, buf
, sizeof(buf
));
535 if (n
<= 0 || n
== sizeof(buf
))
538 *v1
= strtoull(buf
, &ep
, 0);
539 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
541 while (*ep
== ' ' || *ep
== '/' || *ep
== '(')
543 *v2
= strtoull(ep
, &ep2
, 0);
544 if (ep2
== ep
|| (*ep2
!= 0 && *ep2
!= '\n' && *ep2
!= ' ' && *ep2
!= ')')) {
551 int sysfs_get_two(struct mdinfo
*sra
, struct mdinfo
*dev
,
552 char *name
, unsigned long long *v1
, unsigned long long *v2
)
557 fd
= sysfs_get_fd(sra
, dev
, name
);
560 n
= sysfs_fd_get_two(fd
, v1
, v2
);
565 int sysfs_fd_get_str(int fd
, char *val
, int size
)
570 n
= read(fd
, val
, size
);
571 if (n
<= 0 || n
== size
)
577 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
578 char *name
, char *val
, int size
)
583 fd
= sysfs_get_fd(sra
, dev
, name
);
586 n
= sysfs_fd_get_str(fd
, val
, size
);
591 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
600 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
601 /* this '\n' ^ needed for kernels older than 2.6.28 */
602 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
605 int sysfs_set_array(struct mdinfo
*info
, int vers
)
609 int raid_disks
= info
->array
.raid_disks
;
612 if (info
->array
.major_version
== -1 &&
613 info
->array
.minor_version
== -2) {
616 strcat(strcpy(ver
, "external:"), info
->text_version
);
618 /* meta version might already be set if we are setting
619 * new geometry for a reshape. In that case we don't
620 * want to over-write the 'readonly' flag that is
621 * stored in the metadata version. So read the current
622 * version first, and preserve the flag
624 if (sysfs_get_str(info
, NULL
, "metadata_version",
626 if (strlen(buf
) >= 9 && buf
[9] == '-')
629 if ((vers
% 100) < 2 ||
630 sysfs_set_str(info
, NULL
, "metadata_version",
632 pr_err("This kernel does not support external metadata.\n");
636 if (info
->array
.level
< 0)
637 return 0; /* FIXME */
638 rv
|= sysfs_set_str(info
, NULL
, "level",
639 map_num(pers
, info
->array
.level
));
640 if (info
->reshape_active
&& info
->delta_disks
!= UnSet
)
641 raid_disks
-= info
->delta_disks
;
642 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", raid_disks
);
643 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
644 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
645 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
646 if (info
->custom_array_size
) {
649 rc
= sysfs_set_num(info
, NULL
, "array_size",
650 info
->custom_array_size
/2);
651 if (rc
&& errno
== ENOENT
) {
652 pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n");
658 if (info
->array
.level
> 0)
659 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
661 if (info
->reshape_active
) {
662 rv
|= sysfs_set_num(info
, NULL
, "reshape_position",
663 info
->reshape_progress
);
664 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->new_chunk
);
665 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->new_layout
);
666 rv
|= sysfs_set_num(info
, NULL
, "raid_disks",
667 info
->array
.raid_disks
);
668 /* We don't set 'new_level' here. That can only happen
669 * once the reshape completes.
675 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
682 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
683 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
687 memset(nm
, 0, sizeof(nm
));
688 dname
= devid2kname(makedev(sd
->disk
.major
, sd
->disk
.minor
));
689 strcpy(sd
->sys_name
, "dev-");
690 strcpy(sd
->sys_name
+4, dname
);
692 /* test write to see if 'recovery_start' is available */
693 if (resume
&& sd
->recovery_start
< MaxSector
&&
694 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
695 sysfs_set_str(sra
, sd
, "state", "remove");
699 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
700 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
701 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
702 if (sd
->recovery_start
== MaxSector
)
703 /* This can correctly fail if array isn't started,
704 * yet, so just ignore status for now.
706 sysfs_set_str(sra
, sd
, "state", "insync");
707 if (sd
->disk
.raid_disk
>= 0)
708 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
710 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
716 int sysfs_disk_to_sg(int fd
)
718 /* from an open block device, try find and open its corresponding
719 * scsi_generic interface
724 char sg_major_minor
[10];
728 int major
, minor
, rv
;
733 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
734 major(st
.st_rdev
), minor(st
.st_rdev
));
742 if (strncmp("scsi_generic:", de
->d_name
,
743 strlen("scsi_generic:")) == 0)
752 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
753 fd
= open(sg_path
, O_RDONLY
);
757 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
759 if (rv
< 0 || rv
== sizeof(sg_major_minor
))
762 sg_major_minor
[rv
- 1] = '\0';
764 c
= strchr(sg_major_minor
, ':');
767 major
= strtol(sg_major_minor
, NULL
, 10);
768 minor
= strtol(c
, NULL
, 10);
769 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
770 (int) getpid(), major
, minor
);
771 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
772 fd
= open(path
, O_RDONLY
);
781 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
783 /* from an open block device, try to retrieve it scsi_id */
788 int host
, bus
, target
, lun
;
793 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device/scsi_device",
794 major(st
.st_rdev
), minor(st
.st_rdev
));
800 for (de
= readdir(dir
); de
; de
= readdir(dir
)) {
803 if (de
->d_type
!= DT_DIR
)
806 count
= sscanf(de
->d_name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
);
815 *id
= (host
<< 24) | (bus
<< 16) | (target
<< 8) | (lun
<< 0);
819 int sysfs_unique_holder(char *devnm
, long rdev
)
821 /* Check that devnm is a holder of rdev,
822 * and is the only holder.
823 * we should be locked against races by
826 * 0 - not unique, not even a holder
827 * 1 - unique, this is the only holder.
828 * 2/3 - not unique, there is another holder
829 * -1 - error, cannot find the holders
836 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
837 major(rdev
), minor(rdev
));
838 dir
= opendir(dirname
);
842 while ((de
= readdir(dir
)) != NULL
) {
849 if (de
->d_name
[0] == '.')
851 strcpy(dirname
+l
, "/");
852 strcat(dirname
+l
, de
->d_name
);
853 n
= readlink(dirname
, buf
, sizeof(buf
)-1);
857 sl
= strrchr(buf
, '/');
862 if (strcmp(devnm
, sl
) == 0)
871 int sysfs_freeze_array(struct mdinfo
*sra
)
873 /* Try to freeze resync/rebuild on this array/container.
874 * Return -1 if the array is busy,
875 * return 0 if this kernel doesn't support 'frozen'
876 * return 1 if it worked.
880 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
881 return 1; /* no sync_action == frozen */
882 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
884 if (strcmp(buf
, "frozen\n") == 0)
887 if (strcmp(buf
, "idle\n") != 0 && strcmp(buf
, "recover\n") != 0)
889 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
894 int sysfs_wait(int fd
, int *msec
)
896 /* Wait up to '*msec' for fd to have an exception condition.
897 * if msec == NULL, wait indefinitely.
904 n
= select(fd
+1, NULL
, NULL
, &fds
, NULL
);
908 struct timeval start
, end
, tv
;
909 gettimeofday(&start
, NULL
);
912 tv
.tv_usec
= (*msec
)*1000;
914 tv
.tv_sec
= (*msec
)/1000;
917 n
= select(fd
+1, NULL
, NULL
, &fds
, &tv
);
918 gettimeofday(&end
, NULL
);
919 end
.tv_sec
-= start
.tv_sec
;
920 *msec
-= (end
.tv_sec
* 1000 + end
.tv_usec
/1000
921 - start
.tv_usec
/1000) + 1;