]>
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
= xcalloc(1, sizeof(*sra
));
109 sysfs_init(sra
, fd
, devnum
);
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;
243 while ((de
= readdir(dir
)) != NULL
) {
245 if (de
->d_ino
== 0 ||
246 strncmp(de
->d_name
, "dev-", 4) != 0)
248 strcpy(base
, de
->d_name
);
249 dbase
= base
+ strlen(base
);
252 dev
= xmalloc(sizeof(*dev
));
254 /* Always get slot, major, minor */
255 strcpy(dbase
, "slot");
256 if (load_sys(fname
, buf
)) {
257 /* hmm... unable to read 'slot' maybe the device
260 strcpy(dbase
, "block");
261 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
262 errno
!= ENAMETOOLONG
) {
263 /* ...yup device is gone */
267 /* slot is unreadable but 'block' link
268 * still intact... something bad is happening
276 strcpy(dev
->sys_name
, de
->d_name
);
277 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
278 if (*ep
) dev
->disk
.raid_disk
= -1;
280 strcpy(dbase
, "block/dev");
281 if (load_sys(fname
, buf
)) {
282 /* assume this is a stale reference to a hot
288 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
290 /* special case check for block devices that can go 'offline' */
291 strcpy(dbase
, "block/device/state");
292 if (load_sys(fname
, buf
) == 0 &&
293 strncmp(buf
, "offline", 7) == 0) {
298 /* finally add this disk to the array */
299 dev
->next
= sra
->devs
;
302 if (options
& GET_OFFSET
) {
303 strcpy(dbase
, "offset");
304 if (load_sys(fname
, buf
))
306 dev
->data_offset
= strtoull(buf
, NULL
, 0);
307 strcpy(dbase
, "new_offset");
308 if (load_sys(fname
, buf
) == 0)
309 dev
->new_data_offset
= strtoull(buf
, NULL
, 0);
311 dev
->new_data_offset
= dev
->data_offset
;
313 if (options
& GET_SIZE
) {
314 strcpy(dbase
, "size");
315 if (load_sys(fname
, buf
))
317 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
319 if (options
& GET_STATE
) {
321 strcpy(dbase
, "state");
322 if (load_sys(fname
, buf
))
324 if (strstr(buf
, "in_sync"))
325 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
326 if (strstr(buf
, "faulty"))
327 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
328 if (dev
->disk
.state
== 0)
329 sra
->array
.spare_disks
++;
331 if (options
& GET_ERROR
) {
332 strcpy(buf
, "errors");
333 if (load_sys(fname
, buf
))
335 dev
->errors
= strtoul(buf
, NULL
, 0);
348 int sysfs_attr_match(const char *attr
, const char *str
)
350 /* See if attr, read from a sysfs file, matches
351 * str. They must either be the same, or attr can
352 * have a trailing newline or comma
354 while (*attr
&& *str
&& *attr
== *str
) {
359 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
364 int sysfs_match_word(const char *word
, char **list
)
367 for (n
=0; list
[n
]; n
++)
368 if (sysfs_attr_match(word
, list
[n
]))
373 unsigned long long get_component_size(int fd
)
375 /* Find out the component size of the array.
376 * We cannot trust GET_ARRAY_INFO ioctl as it's
377 * size field is only 32bits.
378 * So look in /sys/block/mdXXX/md/component_size
380 * This returns in units of sectors.
385 if (fstat(fd
, &stb
)) return 0;
386 if (major(stb
.st_rdev
) != (unsigned)get_mdp_major())
387 sprintf(fname
, "/sys/block/md%d/md/component_size",
388 (int)minor(stb
.st_rdev
));
390 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
391 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
392 fd
= open(fname
, O_RDONLY
);
395 n
= read(fd
, fname
, sizeof(fname
));
397 if (n
< 0 || n
== sizeof(fname
))
400 return strtoull(fname
, NULL
, 10) * 2;
403 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
404 char *name
, char *val
)
410 sprintf(fname
, "/sys/block/%s/md/%s/%s",
411 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
412 fd
= open(fname
, O_WRONLY
);
415 n
= write(fd
, val
, strlen(val
));
417 if (n
!= strlen(val
)) {
418 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
419 val
, fname
, strerror(errno
));
425 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
426 char *name
, unsigned long long val
)
429 sprintf(valstr
, "%llu", val
);
430 return sysfs_set_str(sra
, dev
, name
, valstr
);
433 int sysfs_set_num_signed(struct mdinfo
*sra
, struct mdinfo
*dev
,
434 char *name
, long long val
)
437 sprintf(valstr
, "%lli", val
);
438 return sysfs_set_str(sra
, dev
, name
, valstr
);
441 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
447 sprintf(fname
, "/sys/block/%s/uevent",
449 fd
= open(fname
, O_WRONLY
);
452 n
= write(fd
, event
, strlen(event
));
454 if (n
!= (int)strlen(event
)) {
455 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
456 event
, fname
, strerror(errno
));
462 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
467 sprintf(fname
, "/sys/block/%s/md/%s/%s",
468 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
470 return stat(fname
, &st
) == 0;
473 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
479 sprintf(fname
, "/sys/block/%s/md/%s/%s",
480 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
481 fd
= open(fname
, O_RDWR
);
483 fd
= open(fname
, O_RDONLY
);
487 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
494 n
= read(fd
, buf
, sizeof(buf
));
498 *val
= strtoull(buf
, &ep
, 0);
499 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
504 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
505 char *name
, unsigned long long *val
)
510 fd
= sysfs_get_fd(sra
, dev
, name
);
513 n
= sysfs_fd_get_ll(fd
, val
);
518 int sysfs_fd_get_str(int fd
, char *val
, int size
)
523 n
= read(fd
, val
, size
);
530 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
531 char *name
, char *val
, int size
)
536 fd
= sysfs_get_fd(sra
, dev
, name
);
539 n
= sysfs_fd_get_str(fd
, val
, size
);
544 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
553 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
554 /* this '\n' ^ needed for kernels older than 2.6.28 */
555 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
558 int sysfs_set_array(struct mdinfo
*info
, int vers
)
562 int raid_disks
= info
->array
.raid_disks
;
565 if (info
->array
.major_version
== -1 &&
566 info
->array
.minor_version
== -2) {
569 strcat(strcpy(ver
, "external:"), info
->text_version
);
571 /* meta version might already be set if we are setting
572 * new geometry for a reshape. In that case we don't
573 * want to over-write the 'readonly' flag that is
574 * stored in the metadata version. So read the current
575 * version first, and preserve the flag
577 if (sysfs_get_str(info
, NULL
, "metadata_version",
579 if (strlen(buf
) >= 9 && buf
[9] == '-')
582 if ((vers
% 100) < 2 ||
583 sysfs_set_str(info
, NULL
, "metadata_version",
585 pr_err("This kernel does not "
586 "support external metadata.\n");
590 if (info
->array
.level
< 0)
591 return 0; /* FIXME */
592 rv
|= sysfs_set_str(info
, NULL
, "level",
593 map_num(pers
, info
->array
.level
));
594 if (info
->reshape_active
&& info
->delta_disks
!= UnSet
)
595 raid_disks
-= info
->delta_disks
;
596 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", raid_disks
);
597 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
598 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
599 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
600 if (info
->custom_array_size
) {
603 rc
= sysfs_set_num(info
, NULL
, "array_size",
604 info
->custom_array_size
/2);
605 if (rc
&& errno
== ENOENT
) {
606 pr_err("This kernel does not "
607 "have the md/array_size attribute, "
608 "the array may be larger than expected\n");
614 if (info
->array
.level
> 0)
615 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
617 if (info
->reshape_active
) {
618 rv
|= sysfs_set_num(info
, NULL
, "reshape_position",
619 info
->reshape_progress
);
620 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->new_chunk
);
621 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->new_layout
);
622 rv
|= sysfs_set_num(info
, NULL
, "raid_disks",
623 info
->array
.raid_disks
);
624 /* We don't set 'new_level' here. That can only happen
625 * once the reshape completes.
631 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
638 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
639 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
643 memset(nm
, 0, sizeof(nm
));
644 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
645 rv
= readlink(dv
, nm
, sizeof(nm
)-1);
649 dname
= strrchr(nm
, '/');
651 strcpy(sd
->sys_name
, "dev-");
652 strcpy(sd
->sys_name
+4, dname
);
654 /* test write to see if 'recovery_start' is available */
655 if (resume
&& sd
->recovery_start
< MaxSector
&&
656 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
657 sysfs_set_str(sra
, sd
, "state", "remove");
661 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
662 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
663 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
664 if (sd
->recovery_start
== MaxSector
)
665 /* This can correctly fail if array isn't started,
666 * yet, so just ignore status for now.
668 sysfs_set_str(sra
, sd
, "state", "insync");
669 if (sd
->disk
.raid_disk
>= 0)
670 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
672 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
678 int sysfs_disk_to_sg(int fd
)
680 /* from an open block device, try find and open its corresponding
681 * scsi_generic interface
686 char sg_major_minor
[8];
690 int major
, minor
, rv
;
695 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
696 major(st
.st_rdev
), minor(st
.st_rdev
));
704 if (strncmp("scsi_generic:", de
->d_name
,
705 strlen("scsi_generic:")) == 0)
714 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
715 fd
= open(sg_path
, O_RDONLY
);
719 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
724 sg_major_minor
[rv
- 1] = '\0';
726 c
= strchr(sg_major_minor
, ':');
729 major
= strtol(sg_major_minor
, NULL
, 10);
730 minor
= strtol(c
, NULL
, 10);
731 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
732 (int) getpid(), major
, minor
);
733 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
734 fd
= open(path
, O_RDONLY
);
743 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
745 /* from an open block device, try to retrieve it scsi_id */
750 int host
, bus
, target
, lun
;
755 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device/scsi_device",
756 major(st
.st_rdev
), minor(st
.st_rdev
));
762 for (de
= readdir(dir
); de
; de
= readdir(dir
)) {
765 if (de
->d_type
!= DT_DIR
)
768 count
= sscanf(de
->d_name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
);
777 *id
= (host
<< 24) | (bus
<< 16) | (target
<< 8) | (lun
<< 0);
782 int sysfs_unique_holder(int devnum
, long rdev
)
784 /* Check that devnum is a holder of rdev,
785 * and is the only holder.
786 * we should be locked against races by
787 * an O_EXCL on devnum
789 * 0 - not unique, not even a holder
790 * 1 - unique, this is the only holder.
791 * 2/3 - not unique, there is another holder
792 * -1 - error, cannot find the holders
799 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
800 major(rdev
), minor(rdev
));
801 dir
= opendir(dirname
);
805 while ((de
= readdir(dir
)) != NULL
) {
814 if (de
->d_name
[0] == '.')
816 strcpy(dirname
+l
, "/");
817 strcat(dirname
+l
, de
->d_name
);
818 strcat(dirname
+l
, "/dev");
819 fd
= open(dirname
, O_RDONLY
);
821 /* Probably a race, just ignore this */
824 n
= read(fd
, buf
, sizeof(buf
)-1);
829 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||
844 int sysfs_freeze_array(struct mdinfo
*sra
)
846 /* Try to freeze resync/rebuild on this array/container.
847 * Return -1 if the array is busy,
848 * return 0 if this kernel doesn't support 'frozen'
849 * return 1 if it worked.
853 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
854 return 1; /* no sync_action == frozen */
855 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
857 if (strcmp(buf
, "frozen\n") == 0)
860 if (strcmp(buf
, "idle\n") != 0)
862 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)