]>
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(struct mdinfo
*mdi
, int fd
, char *devnm
)
82 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
88 strcpy(mdi
->sys_name
, devnm
);
92 struct mdinfo
*sysfs_read(int fd
, char *devnm
, unsigned long options
)
103 sra
= xcalloc(1, sizeof(*sra
));
104 sysfs_init(sra
, fd
, devnm
);
105 if (sra
->sys_name
[0] == 0) {
110 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
111 base
= fname
+ strlen(fname
);
114 if (options
& GET_VERSION
) {
115 strcpy(base
, "metadata_version");
116 if (load_sys(fname
, buf
))
118 if (strncmp(buf
, "none", 4) == 0) {
119 sra
->array
.major_version
=
120 sra
->array
.minor_version
= -1;
121 strcpy(sra
->text_version
, "");
122 } else if (strncmp(buf
, "external:", 9) == 0) {
123 sra
->array
.major_version
= -1;
124 sra
->array
.minor_version
= -2;
125 strcpy(sra
->text_version
, buf
+9);
128 &sra
->array
.major_version
,
129 &sra
->array
.minor_version
);
130 strcpy(sra
->text_version
, buf
);
133 if (options
& GET_LEVEL
) {
134 strcpy(base
, "level");
135 if (load_sys(fname
, buf
))
137 sra
->array
.level
= map_name(pers
, buf
);
139 if (options
& GET_LAYOUT
) {
140 strcpy(base
, "layout");
141 if (load_sys(fname
, buf
))
143 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
145 if (options
& GET_DISKS
) {
146 strcpy(base
, "raid_disks");
147 if (load_sys(fname
, buf
))
149 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
151 if (options
& GET_DEGRADED
) {
152 strcpy(base
, "degraded");
153 if (load_sys(fname
, buf
))
155 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
157 if (options
& GET_COMPONENT
) {
158 strcpy(base
, "component_size");
159 if (load_sys(fname
, buf
))
161 sra
->component_size
= strtoull(buf
, NULL
, 0);
162 /* sysfs reports "K", but we want sectors */
163 sra
->component_size
*= 2;
165 if (options
& GET_CHUNK
) {
166 strcpy(base
, "chunk_size");
167 if (load_sys(fname
, buf
))
169 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
171 if (options
& GET_CACHE
) {
172 strcpy(base
, "stripe_cache_size");
173 if (load_sys(fname
, buf
))
174 /* Probably level doesn't support it */
177 sra
->cache_size
= strtoul(buf
, NULL
, 0);
179 if (options
& GET_MISMATCH
) {
180 strcpy(base
, "mismatch_cnt");
181 if (load_sys(fname
, buf
))
183 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
185 if (options
& GET_SAFEMODE
) {
192 strcpy(base
, "safe_mode_delay");
193 if (load_sys(fname
, buf
))
196 /* remove a period, and count digits after it */
198 for (i
= 0; i
< len
; i
++) {
200 if (isdigit(buf
[i
])) {
205 } else if (buf
[i
] == '.') {
210 msec
= strtoul(buf
, NULL
, 10);
211 msec
= (msec
* 1000) / scale
;
212 sra
->safe_mode_delay
= msec
;
214 if (options
& GET_BITMAP_LOCATION
) {
215 strcpy(base
, "bitmap/location");
216 if (load_sys(fname
, buf
))
218 if (strncmp(buf
, "file", 4) == 0)
219 sra
->bitmap_offset
= 1;
220 else if (strncmp(buf
, "none", 4) == 0)
221 sra
->bitmap_offset
= 0;
222 else if (buf
[0] == '+')
223 sra
->bitmap_offset
= strtol(buf
+1, NULL
, 10);
228 if (! (options
& GET_DEVS
))
231 /* Get all the devices as well */
233 dir
= opendir(fname
);
236 sra
->array
.spare_disks
= 0;
238 while ((de
= readdir(dir
)) != NULL
) {
240 if (de
->d_ino
== 0 ||
241 strncmp(de
->d_name
, "dev-", 4) != 0)
243 strcpy(base
, de
->d_name
);
244 dbase
= base
+ strlen(base
);
247 dev
= xmalloc(sizeof(*dev
));
249 /* Always get slot, major, minor */
250 strcpy(dbase
, "slot");
251 if (load_sys(fname
, buf
)) {
252 /* hmm... unable to read 'slot' maybe the device
255 strcpy(dbase
, "block");
256 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
257 errno
!= ENAMETOOLONG
) {
258 /* ...yup device is gone */
262 /* slot is unreadable but 'block' link
263 * still intact... something bad is happening
271 strcpy(dev
->sys_name
, de
->d_name
);
272 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
273 if (*ep
) dev
->disk
.raid_disk
= -1;
275 strcpy(dbase
, "block/dev");
276 if (load_sys(fname
, buf
)) {
277 /* assume this is a stale reference to a hot
283 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
285 /* special case check for block devices that can go 'offline' */
286 strcpy(dbase
, "block/device/state");
287 if (load_sys(fname
, buf
) == 0 &&
288 strncmp(buf
, "offline", 7) == 0) {
293 /* finally add this disk to the array */
294 dev
->next
= sra
->devs
;
297 if (options
& GET_OFFSET
) {
298 strcpy(dbase
, "offset");
299 if (load_sys(fname
, buf
))
301 dev
->data_offset
= strtoull(buf
, NULL
, 0);
302 strcpy(dbase
, "new_offset");
303 if (load_sys(fname
, buf
) == 0)
304 dev
->new_data_offset
= strtoull(buf
, NULL
, 0);
306 dev
->new_data_offset
= dev
->data_offset
;
308 if (options
& GET_SIZE
) {
309 strcpy(dbase
, "size");
310 if (load_sys(fname
, buf
))
312 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
314 if (options
& GET_STATE
) {
316 strcpy(dbase
, "state");
317 if (load_sys(fname
, buf
))
319 if (strstr(buf
, "in_sync"))
320 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
321 if (strstr(buf
, "faulty"))
322 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
323 if (dev
->disk
.state
== 0)
324 sra
->array
.spare_disks
++;
326 if (options
& GET_ERROR
) {
327 strcpy(buf
, "errors");
328 if (load_sys(fname
, buf
))
330 dev
->errors
= strtoul(buf
, NULL
, 0);
343 int sysfs_attr_match(const char *attr
, const char *str
)
345 /* See if attr, read from a sysfs file, matches
346 * str. They must either be the same, or attr can
347 * have a trailing newline or comma
349 while (*attr
&& *str
&& *attr
== *str
) {
354 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
359 int sysfs_match_word(const char *word
, char **list
)
362 for (n
=0; list
[n
]; n
++)
363 if (sysfs_attr_match(word
, list
[n
]))
368 unsigned long long get_component_size(int fd
)
370 /* Find out the component size of the array.
371 * We cannot trust GET_ARRAY_INFO ioctl as it's
372 * size field is only 32bits.
373 * So look in /sys/block/mdXXX/md/component_size
375 * This returns in units of sectors.
380 if (fstat(fd
, &stb
)) return 0;
381 if (major(stb
.st_rdev
) != (unsigned)get_mdp_major())
382 sprintf(fname
, "/sys/block/md%d/md/component_size",
383 (int)minor(stb
.st_rdev
));
385 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
386 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
387 fd
= open(fname
, O_RDONLY
);
390 n
= read(fd
, fname
, sizeof(fname
));
392 if (n
< 0 || n
== sizeof(fname
))
395 return strtoull(fname
, NULL
, 10) * 2;
398 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
399 char *name
, char *val
)
405 sprintf(fname
, "/sys/block/%s/md/%s/%s",
406 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
407 fd
= open(fname
, O_WRONLY
);
410 n
= write(fd
, val
, strlen(val
));
412 if (n
!= strlen(val
)) {
413 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
414 val
, fname
, strerror(errno
));
420 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
421 char *name
, unsigned long long val
)
424 sprintf(valstr
, "%llu", val
);
425 return sysfs_set_str(sra
, dev
, name
, valstr
);
428 int sysfs_set_num_signed(struct mdinfo
*sra
, struct mdinfo
*dev
,
429 char *name
, long long val
)
432 sprintf(valstr
, "%lli", val
);
433 return sysfs_set_str(sra
, dev
, name
, valstr
);
436 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
442 sprintf(fname
, "/sys/block/%s/uevent",
444 fd
= open(fname
, O_WRONLY
);
447 n
= write(fd
, event
, strlen(event
));
449 if (n
!= (int)strlen(event
)) {
450 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
451 event
, fname
, strerror(errno
));
457 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
462 sprintf(fname
, "/sys/block/%s/md/%s/%s",
463 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
465 return stat(fname
, &st
) == 0;
468 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
474 sprintf(fname
, "/sys/block/%s/md/%s/%s",
475 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
476 fd
= open(fname
, O_RDWR
);
478 fd
= open(fname
, O_RDONLY
);
482 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
489 n
= read(fd
, buf
, sizeof(buf
));
493 *val
= strtoull(buf
, &ep
, 0);
494 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
499 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
500 char *name
, unsigned long long *val
)
505 fd
= sysfs_get_fd(sra
, dev
, name
);
508 n
= sysfs_fd_get_ll(fd
, val
);
513 int sysfs_fd_get_str(int fd
, char *val
, int size
)
518 n
= read(fd
, val
, size
);
525 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
526 char *name
, char *val
, int size
)
531 fd
= sysfs_get_fd(sra
, dev
, name
);
534 n
= sysfs_fd_get_str(fd
, val
, size
);
539 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
548 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
549 /* this '\n' ^ needed for kernels older than 2.6.28 */
550 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
553 int sysfs_set_array(struct mdinfo
*info
, int vers
)
557 int raid_disks
= info
->array
.raid_disks
;
560 if (info
->array
.major_version
== -1 &&
561 info
->array
.minor_version
== -2) {
564 strcat(strcpy(ver
, "external:"), info
->text_version
);
566 /* meta version might already be set if we are setting
567 * new geometry for a reshape. In that case we don't
568 * want to over-write the 'readonly' flag that is
569 * stored in the metadata version. So read the current
570 * version first, and preserve the flag
572 if (sysfs_get_str(info
, NULL
, "metadata_version",
574 if (strlen(buf
) >= 9 && buf
[9] == '-')
577 if ((vers
% 100) < 2 ||
578 sysfs_set_str(info
, NULL
, "metadata_version",
580 pr_err("This kernel does not "
581 "support external metadata.\n");
585 if (info
->array
.level
< 0)
586 return 0; /* FIXME */
587 rv
|= sysfs_set_str(info
, NULL
, "level",
588 map_num(pers
, info
->array
.level
));
589 if (info
->reshape_active
&& info
->delta_disks
!= UnSet
)
590 raid_disks
-= info
->delta_disks
;
591 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", raid_disks
);
592 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
593 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
594 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
595 if (info
->custom_array_size
) {
598 rc
= sysfs_set_num(info
, NULL
, "array_size",
599 info
->custom_array_size
/2);
600 if (rc
&& errno
== ENOENT
) {
601 pr_err("This kernel does not "
602 "have the md/array_size attribute, "
603 "the array may be larger than expected\n");
609 if (info
->array
.level
> 0)
610 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
612 if (info
->reshape_active
) {
613 rv
|= sysfs_set_num(info
, NULL
, "reshape_position",
614 info
->reshape_progress
);
615 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->new_chunk
);
616 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->new_layout
);
617 rv
|= sysfs_set_num(info
, NULL
, "raid_disks",
618 info
->array
.raid_disks
);
619 /* We don't set 'new_level' here. That can only happen
620 * once the reshape completes.
626 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
633 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
634 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
638 memset(nm
, 0, sizeof(nm
));
639 dname
= devid2devnm(makedev(sd
->disk
.major
, sd
->disk
.minor
));
640 strcpy(sd
->sys_name
, "dev-");
641 strcpy(sd
->sys_name
+4, dname
);
643 /* test write to see if 'recovery_start' is available */
644 if (resume
&& sd
->recovery_start
< MaxSector
&&
645 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
646 sysfs_set_str(sra
, sd
, "state", "remove");
650 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
651 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
652 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
653 if (sd
->recovery_start
== MaxSector
)
654 /* This can correctly fail if array isn't started,
655 * yet, so just ignore status for now.
657 sysfs_set_str(sra
, sd
, "state", "insync");
658 if (sd
->disk
.raid_disk
>= 0)
659 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
661 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
667 int sysfs_disk_to_sg(int fd
)
669 /* from an open block device, try find and open its corresponding
670 * scsi_generic interface
675 char sg_major_minor
[8];
679 int major
, minor
, rv
;
684 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
685 major(st
.st_rdev
), minor(st
.st_rdev
));
693 if (strncmp("scsi_generic:", de
->d_name
,
694 strlen("scsi_generic:")) == 0)
703 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
704 fd
= open(sg_path
, O_RDONLY
);
708 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
713 sg_major_minor
[rv
- 1] = '\0';
715 c
= strchr(sg_major_minor
, ':');
718 major
= strtol(sg_major_minor
, NULL
, 10);
719 minor
= strtol(c
, NULL
, 10);
720 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
721 (int) getpid(), major
, minor
);
722 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
723 fd
= open(path
, O_RDONLY
);
732 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
734 /* from an open block device, try to retrieve it scsi_id */
739 int host
, bus
, target
, lun
;
744 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device/scsi_device",
745 major(st
.st_rdev
), minor(st
.st_rdev
));
751 for (de
= readdir(dir
); de
; de
= readdir(dir
)) {
754 if (de
->d_type
!= DT_DIR
)
757 count
= sscanf(de
->d_name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
);
766 *id
= (host
<< 24) | (bus
<< 16) | (target
<< 8) | (lun
<< 0);
771 int sysfs_unique_holder(char *devnm
, long rdev
)
773 /* Check that devnm is a holder of rdev,
774 * and is the only holder.
775 * we should be locked against races by
778 * 0 - not unique, not even a holder
779 * 1 - unique, this is the only holder.
780 * 2/3 - not unique, there is another holder
781 * -1 - error, cannot find the holders
788 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
789 major(rdev
), minor(rdev
));
790 dir
= opendir(dirname
);
794 while ((de
= readdir(dir
)) != NULL
) {
801 if (de
->d_name
[0] == '.')
803 strcpy(dirname
+l
, "/");
804 strcat(dirname
+l
, de
->d_name
);
805 n
= readlink(dirname
, buf
, sizeof(buf
)-1);
809 sl
= strrchr(buf
, '/');
814 if (strcmp(devnm
, sl
) == 0)
823 int sysfs_freeze_array(struct mdinfo
*sra
)
825 /* Try to freeze resync/rebuild on this array/container.
826 * Return -1 if the array is busy,
827 * return 0 if this kernel doesn't support 'frozen'
828 * return 1 if it worked.
832 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
833 return 1; /* no sync_action == frozen */
834 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
836 if (strcmp(buf
, "frozen\n") == 0)
839 if (strcmp(buf
, "idle\n") != 0)
841 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)