]>
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 /* Longest possible name in sysfs, mounted at /sys, is
104 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
105 * /sys/block/md_dXXX/md/metadata_version
106 * which is about 41 characters. 50 should do for now
117 sra
= malloc(sizeof(*sra
));
120 memset(sra
, 0, sizeof(*sra
));
121 sysfs_init(sra
, fd
, devnum
);
122 if (sra
->sys_name
[0] == 0) {
127 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
128 base
= fname
+ strlen(fname
);
131 if (options
& GET_VERSION
) {
132 strcpy(base
, "metadata_version");
133 if (load_sys(fname
, buf
))
135 if (strncmp(buf
, "none", 4) == 0) {
136 sra
->array
.major_version
=
137 sra
->array
.minor_version
= -1;
138 strcpy(sra
->text_version
, "");
139 } else if (strncmp(buf
, "external:", 9) == 0) {
140 sra
->array
.major_version
= -1;
141 sra
->array
.minor_version
= -2;
142 strcpy(sra
->text_version
, buf
+9);
145 &sra
->array
.major_version
,
146 &sra
->array
.minor_version
);
147 strcpy(sra
->text_version
, buf
);
150 if (options
& GET_LEVEL
) {
151 strcpy(base
, "level");
152 if (load_sys(fname
, buf
))
154 sra
->array
.level
= map_name(pers
, buf
);
156 if (options
& GET_LAYOUT
) {
157 strcpy(base
, "layout");
158 if (load_sys(fname
, buf
))
160 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
162 if (options
& GET_DISKS
) {
163 strcpy(base
, "raid_disks");
164 if (load_sys(fname
, buf
))
166 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
168 if (options
& GET_DEGRADED
) {
169 strcpy(base
, "degraded");
170 if (load_sys(fname
, buf
))
172 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
174 if (options
& GET_COMPONENT
) {
175 strcpy(base
, "component_size");
176 if (load_sys(fname
, buf
))
178 sra
->component_size
= strtoull(buf
, NULL
, 0);
179 /* sysfs reports "K", but we want sectors */
180 sra
->component_size
*= 2;
182 if (options
& GET_CHUNK
) {
183 strcpy(base
, "chunk_size");
184 if (load_sys(fname
, buf
))
186 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
188 if (options
& GET_CACHE
) {
189 strcpy(base
, "stripe_cache_size");
190 if (load_sys(fname
, buf
))
192 sra
->cache_size
= strtoul(buf
, NULL
, 0);
194 if (options
& GET_MISMATCH
) {
195 strcpy(base
, "mismatch_cnt");
196 if (load_sys(fname
, buf
))
198 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
200 if (options
& GET_SAFEMODE
) {
207 strcpy(base
, "safe_mode_delay");
208 if (load_sys(fname
, buf
))
211 /* remove a period, and count digits after it */
213 for (i
= 0; i
< len
; i
++) {
215 if (isdigit(buf
[i
])) {
220 } else if (buf
[i
] == '.') {
225 msec
= strtoul(buf
, NULL
, 10);
226 msec
= (msec
* 1000) / scale
;
227 sra
->safe_mode_delay
= msec
;
230 if (! (options
& GET_DEVS
))
233 /* Get all the devices as well */
235 dir
= opendir(fname
);
238 sra
->array
.spare_disks
= 0;
240 while ((de
= readdir(dir
)) != NULL
) {
242 if (de
->d_ino
== 0 ||
243 strncmp(de
->d_name
, "dev-", 4) != 0)
245 strcpy(base
, de
->d_name
);
246 dbase
= base
+ strlen(base
);
249 dev
= malloc(sizeof(*dev
));
253 /* Always get slot, major, minor */
254 strcpy(dbase
, "slot");
255 if (load_sys(fname
, buf
)) {
256 /* hmm... unable to read 'slot' maybe the device
259 strcpy(dbase
, "block");
260 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
261 errno
!= ENAMETOOLONG
) {
262 /* ...yup device is gone */
266 /* slot is unreadable but 'block' link
267 * still intact... something bad is happening
275 strcpy(dev
->sys_name
, de
->d_name
);
276 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
277 if (*ep
) dev
->disk
.raid_disk
= -1;
279 strcpy(dbase
, "block/dev");
280 if (load_sys(fname
, buf
)) {
282 if (options
& SKIP_GONE_DEVS
)
287 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
289 /* special case check for block devices that can go 'offline' */
290 if (options
& SKIP_GONE_DEVS
) {
291 strcpy(dbase
, "block/device/state");
292 if (load_sys(fname
, buf
) == 0 &&
293 strncmp(buf
, "offline", 7) == 0) {
299 /* finally add this disk to the array */
300 dev
->next
= sra
->devs
;
303 if (options
& GET_OFFSET
) {
304 strcpy(dbase
, "offset");
305 if (load_sys(fname
, buf
))
307 dev
->data_offset
= strtoull(buf
, NULL
, 0);
309 if (options
& GET_SIZE
) {
310 strcpy(dbase
, "size");
311 if (load_sys(fname
, buf
))
313 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
315 if (options
& GET_STATE
) {
317 strcpy(dbase
, "state");
318 if (load_sys(fname
, buf
))
320 if (strstr(buf
, "in_sync"))
321 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
322 if (strstr(buf
, "faulty"))
323 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
324 if (dev
->disk
.state
== 0)
325 sra
->array
.spare_disks
++;
327 if (options
& GET_ERROR
) {
328 strcpy(buf
, "errors");
329 if (load_sys(fname
, buf
))
331 dev
->errors
= strtoul(buf
, NULL
, 0);
344 int sysfs_attr_match(const char *attr
, const char *str
)
346 /* See if attr, read from a sysfs file, matches
347 * str. They must either be the same, or attr can
348 * have a trailing newline or comma
350 while (*attr
&& *str
&& *attr
== *str
) {
355 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
360 int sysfs_match_word(const char *word
, char **list
)
363 for (n
=0; list
[n
]; n
++)
364 if (sysfs_attr_match(word
, list
[n
]))
369 unsigned long long get_component_size(int fd
)
371 /* Find out the component size of the array.
372 * We cannot trust GET_ARRAY_INFO ioctl as it's
373 * size field is only 32bits.
374 * So look in /sys/block/mdXXX/md/component_size
376 * This returns in units of sectors.
381 if (fstat(fd
, &stb
)) return 0;
382 if (major(stb
.st_rdev
) != get_mdp_major())
383 sprintf(fname
, "/sys/block/md%d/md/component_size",
384 (int)minor(stb
.st_rdev
));
386 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
387 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
388 fd
= open(fname
, O_RDONLY
);
391 n
= read(fd
, fname
, sizeof(fname
));
393 if (n
== sizeof(fname
))
396 return strtoull(fname
, NULL
, 10) * 2;
399 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
400 char *name
, char *val
)
406 sprintf(fname
, "/sys/block/%s/md/%s/%s",
407 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
408 fd
= open(fname
, O_WRONLY
);
411 n
= write(fd
, val
, strlen(val
));
413 if (n
!= strlen(val
)) {
414 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
415 val
, fname
, strerror(errno
));
421 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
422 char *name
, unsigned long long val
)
425 sprintf(valstr
, "%llu", val
);
426 return sysfs_set_str(sra
, dev
, name
, valstr
);
429 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
435 sprintf(fname
, "/sys/block/%s/uevent",
437 fd
= open(fname
, O_WRONLY
);
440 n
= write(fd
, event
, strlen(event
));
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 in_sync
)
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 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
599 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
600 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
602 /* This can correctly fail if array isn't started,
603 * yet, so just ignore status for now.
605 sysfs_set_str(sra
, sd
, "state", "in_sync");
606 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
612 int sysfs_disk_to_sg(int fd
)
614 /* from an open block device, try find and open its corresponding
615 * scsi_generic interface
620 char sg_major_minor
[8];
624 int major
, minor
, rv
;
629 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
630 major(st
.st_rdev
), minor(st
.st_rdev
));
638 if (strncmp("scsi_generic:", de
->d_name
,
639 strlen("scsi_generic:")) == 0)
648 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
649 fd
= open(sg_path
, O_RDONLY
);
653 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
658 sg_major_minor
[rv
- 1] = '\0';
660 c
= strchr(sg_major_minor
, ':');
663 major
= strtol(sg_major_minor
, NULL
, 10);
664 minor
= strtol(c
, NULL
, 10);
665 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
666 (int) getpid(), major
, minor
);
667 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
668 fd
= open(path
, O_RDONLY
);
677 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
679 /* from an open block device, try to retrieve it scsi_id */
689 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
690 major(st
.st_rdev
), minor(st
.st_rdev
));
698 if (strncmp("scsi_disk:", de
->d_name
,
699 strlen("scsi_disk:")) == 0)
708 c1
= strchr(de
->d_name
, ':');
710 c2
= strchr(c1
, ':');
712 *id
= strtol(c1
, NULL
, 10) << 24; /* host */
714 c2
= strchr(c1
, ':');
716 *id
|= strtol(c1
, NULL
, 10) << 16; /* channel */
718 c2
= strchr(c1
, ':');
720 *id
|= strtol(c1
, NULL
, 10) << 8; /* lun */
722 *id
|= strtol(c1
, NULL
, 10); /* id */
728 int sysfs_unique_holder(int devnum
, long rdev
)
730 /* Check that devnum is a holder of rdev,
731 * and is the only holder.
732 * we should be locked against races by
733 * an O_EXCL on devnum
740 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
741 major(rdev
), minor(rdev
));
742 dir
= opendir(dirname
);
747 while ((de
= readdir(dir
)) != NULL
) {
756 if (de
->d_name
[0] == '.')
758 strcpy(dirname
+l
, "/");
759 strcat(dirname
+l
, de
->d_name
);
760 strcat(dirname
+l
, "/dev");
761 fd
= open(dirname
, O_RDONLY
);
766 n
= read(fd
, buf
, sizeof(buf
)-1);
769 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||
792 static char *clean_states
[] = {
793 "clear", "inactive", "readonly", "read-auto", "clean", NULL
};
795 int WaitClean(char *dev
, int sock
, int verbose
)
802 fd
= open(dev
, O_RDONLY
);
805 fprintf(stderr
, Name
": Couldn't open %s: %s\n", dev
, strerror(errno
));
809 devnum
= fd2devnum(fd
);
810 mdi
= sysfs_read(fd
, devnum
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
813 fprintf(stderr
, Name
": Failed to read sysfs attributes for "
819 switch(mdi
->array
.level
) {
821 case LEVEL_MULTIPATH
:
823 /* safemode delay is irrelevant for these levels */
828 /* for internal metadata the kernel handles the final clean
829 * transition, containers can never be dirty
831 if (!is_subarray(mdi
->text_version
))
834 /* safemode disabled ? */
835 if (mdi
->safe_mode_delay
== 0)
839 int state_fd
= sysfs_open(fd2devnum(fd
), NULL
, "array_state");
844 /* minimize the safe_mode_delay and prepare to wait up to 5s
845 * for writes to quiesce
847 sysfs_set_safemode(mdi
, 1);
851 /* give mdmon a chance to checkpoint resync */
852 sysfs_set_str(mdi
, NULL
, "sync_action", "idle");
856 /* wait for array_state to be clean */
858 rv
= read(state_fd
, buf
, sizeof(buf
));
861 if (sysfs_match_word(buf
, clean_states
) <= 4)
863 FD_SET(state_fd
, &fds
);
864 rv
= select(state_fd
+ 1, NULL
, NULL
, &fds
, &tm
);
865 if (rv
< 0 && errno
!= EINTR
)
867 lseek(state_fd
, 0, SEEK_SET
);
871 else if (fping_monitor(sock
) == 0 ||
872 ping_monitor(mdi
->text_version
) == 0) {
873 /* we need to ping to close the window between array
874 * state transitioning to clean and the metadata being
881 fprintf(stderr
, Name
": Error waiting for %s to be clean\n",
884 /* restore the original safe_mode_delay */
885 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);
894 #endif /* MDASSEMBLE */