]>
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 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
)
89 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
91 if (major(stb
.st_rdev
) == MD_MAJOR
)
92 sprintf(mdi
->sys_name
, "md%d", (int)minor(stb
.st_rdev
));
93 else if (major(stb
.st_rdev
) == get_mdp_major())
94 sprintf(mdi
->sys_name
, "md_d%d",
95 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
97 /* must be an extended-minor partition. Look at the
98 * /sys/dev/block/%d:%d link which must look like
99 * ../../block/mdXXX/mdXXXpYY
105 sprintf(path
, "/sys/dev/block/%d:%d", major(stb
.st_rdev
),
107 n
= readlink(path
, link
, sizeof(link
)-1);
111 cp
= strrchr(link
, '/');
113 cp
= strchr(link
, '/');
114 if (cp
&& strncmp(cp
, "/md", 3) == 0)
115 strcpy(mdi
->sys_name
, cp
+1);
121 sprintf(mdi
->sys_name
, "md%d", devnum
);
123 sprintf(mdi
->sys_name
, "md_d%d",
128 struct mdinfo
*sysfs_read(int fd
, int devnum
, unsigned long options
)
130 /* Longest possible name in sysfs, mounted at /sys, is
131 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
132 * /sys/block/md_dXXX/md/metadata_version
133 * which is about 41 characters. 50 should do for now
144 sra
= malloc(sizeof(*sra
));
147 memset(sra
, 0, sizeof(*sra
));
148 sysfs_init(sra
, fd
, devnum
);
150 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
151 base
= fname
+ strlen(fname
);
154 if (options
& GET_VERSION
) {
155 strcpy(base
, "metadata_version");
156 if (load_sys(fname
, buf
))
158 if (strncmp(buf
, "none", 4) == 0) {
159 sra
->array
.major_version
=
160 sra
->array
.minor_version
= -1;
161 strcpy(sra
->text_version
, "");
162 } else if (strncmp(buf
, "external:", 9) == 0) {
163 sra
->array
.major_version
= -1;
164 sra
->array
.minor_version
= -2;
165 strcpy(sra
->text_version
, buf
+9);
168 &sra
->array
.major_version
,
169 &sra
->array
.minor_version
);
170 strcpy(sra
->text_version
, buf
);
173 if (options
& GET_LEVEL
) {
174 strcpy(base
, "level");
175 if (load_sys(fname
, buf
))
177 sra
->array
.level
= map_name(pers
, buf
);
179 if (options
& GET_LAYOUT
) {
180 strcpy(base
, "layout");
181 if (load_sys(fname
, buf
))
183 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
185 if (options
& GET_DISKS
) {
186 strcpy(base
, "raid_disks");
187 if (load_sys(fname
, buf
))
189 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
191 if (options
& GET_DEGRADED
) {
192 strcpy(base
, "degraded");
193 if (load_sys(fname
, buf
))
195 sra
->array
.failed_disks
= strtoul(buf
, NULL
, 0);
197 if (options
& GET_COMPONENT
) {
198 strcpy(base
, "component_size");
199 if (load_sys(fname
, buf
))
201 sra
->component_size
= strtoull(buf
, NULL
, 0);
202 /* sysfs reports "K", but we want sectors */
203 sra
->component_size
*= 2;
205 if (options
& GET_CHUNK
) {
206 strcpy(base
, "chunk_size");
207 if (load_sys(fname
, buf
))
209 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
211 if (options
& GET_CACHE
) {
212 strcpy(base
, "stripe_cache_size");
213 if (load_sys(fname
, buf
))
215 sra
->cache_size
= strtoul(buf
, NULL
, 0);
217 if (options
& GET_MISMATCH
) {
218 strcpy(base
, "mismatch_cnt");
219 if (load_sys(fname
, buf
))
221 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
223 if (options
& GET_SAFEMODE
) {
230 strcpy(base
, "safe_mode_delay");
231 if (load_sys(fname
, buf
))
234 /* remove a period, and count digits after it */
236 for (i
= 0; i
< len
; i
++) {
238 if (isdigit(buf
[i
])) {
243 } else if (buf
[i
] == '.') {
248 msec
= strtoul(buf
, NULL
, 10);
249 msec
= (msec
* 1000) / scale
;
250 sra
->safe_mode_delay
= msec
;
253 if (! (options
& GET_DEVS
))
256 /* Get all the devices as well */
258 dir
= opendir(fname
);
261 sra
->array
.spare_disks
= 0;
263 while ((de
= readdir(dir
)) != NULL
) {
265 if (de
->d_ino
== 0 ||
266 strncmp(de
->d_name
, "dev-", 4) != 0)
268 strcpy(base
, de
->d_name
);
269 dbase
= base
+ strlen(base
);
272 dev
= malloc(sizeof(*dev
));
276 /* Always get slot, major, minor */
277 strcpy(dbase
, "slot");
278 if (load_sys(fname
, buf
)) {
279 /* hmm... unable to read 'slot' maybe the device
282 strcpy(dbase
, "block");
283 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
284 errno
!= ENAMETOOLONG
) {
285 /* ...yup device is gone */
289 /* slot is unreadable but 'block' link
290 * still intact... something bad is happening
298 dev
->next
= sra
->devs
;
301 strcpy(dev
->sys_name
, de
->d_name
);
302 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
303 if (*ep
) dev
->disk
.raid_disk
= -1;
305 strcpy(dbase
, "block/dev");
306 if (load_sys(fname
, buf
))
308 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
310 if (options
& GET_OFFSET
) {
311 strcpy(dbase
, "offset");
312 if (load_sys(fname
, buf
))
314 dev
->data_offset
= strtoull(buf
, NULL
, 0);
316 if (options
& GET_SIZE
) {
317 strcpy(dbase
, "size");
318 if (load_sys(fname
, buf
))
320 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
322 if (options
& GET_STATE
) {
324 strcpy(dbase
, "state");
325 if (load_sys(fname
, buf
))
327 if (strstr(buf
, "in_sync"))
328 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
329 if (strstr(buf
, "faulty"))
330 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
331 if (dev
->disk
.state
== 0)
332 sra
->array
.spare_disks
++;
334 if (options
& GET_ERROR
) {
335 strcpy(buf
, "errors");
336 if (load_sys(fname
, buf
))
338 dev
->errors
= strtoul(buf
, NULL
, 0);
351 int sysfs_attr_match(const char *attr
, const char *str
)
353 /* See if attr, read from a sysfs file, matches
354 * str. They must either be the same, or attr can
355 * have a trailing newline or comma
357 while (*attr
&& *str
&& *attr
== *str
) {
362 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
367 int sysfs_match_word(const char *word
, char **list
)
370 for (n
=0; list
[n
]; n
++)
371 if (sysfs_attr_match(word
, list
[n
]))
376 unsigned long long get_component_size(int fd
)
378 /* Find out the component size of the array.
379 * We cannot trust GET_ARRAY_INFO ioctl as it's
380 * size field is only 32bits.
381 * So look in /sys/block/mdXXX/md/component_size
383 * This returns in units of sectors.
388 if (fstat(fd
, &stb
)) return 0;
389 if (major(stb
.st_rdev
) != get_mdp_major())
390 sprintf(fname
, "/sys/block/md%d/md/component_size",
391 (int)minor(stb
.st_rdev
));
393 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
394 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
395 fd
= open(fname
, O_RDONLY
);
398 n
= read(fd
, fname
, sizeof(fname
));
400 if (n
== sizeof(fname
))
403 return strtoull(fname
, NULL
, 10) * 2;
406 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
407 char *name
, char *val
)
413 sprintf(fname
, "/sys/block/%s/md/%s/%s",
414 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
415 fd
= open(fname
, O_WRONLY
);
418 n
= write(fd
, val
, strlen(val
));
420 if (n
!= strlen(val
)) {
421 dprintf(Name
": failed to write '%s' to '%s' (%s)\n",
422 val
, fname
, strerror(errno
));
428 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
429 char *name
, unsigned long long val
)
432 sprintf(valstr
, "%llu", val
);
433 return sysfs_set_str(sra
, dev
, name
, valstr
);
436 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
437 char *name
, unsigned long long *val
)
444 sprintf(fname
, "/sys/block/%s/md/%s/%s",
445 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
446 fd
= open(fname
, O_RDONLY
);
449 n
= read(fd
, buf
, sizeof(buf
));
454 *val
= strtoull(buf
, &ep
, 0);
455 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
460 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
469 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
470 /* this '\n' ^ needed for kernels older than 2.6.28 */
471 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
474 int sysfs_set_array(struct mdinfo
*info
, int vers
)
480 if (info
->array
.major_version
== -1 &&
481 info
->array
.minor_version
== -2) {
482 strcat(strcpy(ver
, "external:"), info
->text_version
);
484 if ((vers
% 100) < 2 ||
485 sysfs_set_str(info
, NULL
, "metadata_version",
487 fprintf(stderr
, Name
": This kernel does not "
488 "support external metadata.\n");
492 if (info
->array
.level
< 0)
493 return 0; /* FIXME */
494 rv
|= sysfs_set_str(info
, NULL
, "level",
495 map_num(pers
, info
->array
.level
));
496 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", info
->array
.raid_disks
);
497 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
498 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
499 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
500 if (info
->array
.level
> 0)
501 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
505 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
)
512 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
513 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
517 memset(nm
, 0, sizeof(nm
));
518 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
519 rv
= readlink(dv
, nm
, sizeof(nm
));
523 dname
= strrchr(nm
, '/');
525 strcpy(sd
->sys_name
, "dev-");
526 strcpy(sd
->sys_name
+4, dname
);
528 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
529 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
530 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
531 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
532 // rv |= sysfs_set_str(sra, sd, "state", "in_sync");
538 int sysfs_disk_to_sg(int fd
)
540 /* from an open block device, try find and open its corresponding
541 * scsi_generic interface
546 char sg_major_minor
[8];
550 int major
, minor
, rv
;
555 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
556 major(st
.st_rdev
), minor(st
.st_rdev
));
564 if (strncmp("scsi_generic:", de
->d_name
,
565 strlen("scsi_generic:")) == 0)
574 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
575 fd
= open(sg_path
, O_RDONLY
);
579 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
584 sg_major_minor
[rv
- 1] = '\0';
586 c
= strchr(sg_major_minor
, ':');
589 major
= strtol(sg_major_minor
, NULL
, 10);
590 minor
= strtol(c
, NULL
, 10);
591 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
592 (int) getpid(), major
, minor
);
593 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
594 fd
= open(path
, O_RDONLY
);
603 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
605 /* from an open block device, try to retrieve it scsi_id */
615 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
616 major(st
.st_rdev
), minor(st
.st_rdev
));
624 if (strncmp("scsi_disk:", de
->d_name
,
625 strlen("scsi_disk:")) == 0)
634 c1
= strchr(de
->d_name
, ':');
636 c2
= strchr(c1
, ':');
638 *id
= strtol(c1
, NULL
, 10) << 24; /* host */
640 c2
= strchr(c1
, ':');
642 *id
|= strtol(c1
, NULL
, 10) << 16; /* channel */
644 c2
= strchr(c1
, ':');
646 *id
|= strtol(c1
, NULL
, 10) << 8; /* lun */
648 *id
|= strtol(c1
, NULL
, 10); /* id */
654 int sysfs_unique_holder(int devnum
, long rdev
)
656 /* Check that devnum is a holder of rdev,
657 * and is the only holder.
658 * we should be locked against races by
659 * an O_EXCL on devnum
666 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
667 major(rdev
), minor(rdev
));
668 dir
= opendir(dirname
);
673 while ((de
= readdir(dir
)) != NULL
) {
682 if (de
->d_name
[0] == '.')
684 strcpy(dirname
+l
, "/");
685 strcat(dirname
+l
, de
->d_name
);
686 strcat(dirname
+l
, "/dev");
687 fd
= open(dirname
, O_RDONLY
);
692 n
= read(fd
, buf
, sizeof(buf
)-1);
695 if (sscanf(buf
, "%d:%d%c", &mj
, &mn
, &c
) != 3 ||