]>
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>
29 int load_sys(char *path
, char *buf
)
31 int fd
= open(path
, O_RDONLY
);
35 n
= read(fd
, buf
, 1024);
37 if (n
<0 || n
>= 1024)
45 void sysfs_free(struct mdinfo
*sra
)
48 struct mdinfo
*sra2
= sra
->next
;
50 struct mdinfo
*d
= sra
->devs
;
59 int sysfs_open(int devnum
, char *devname
, char *attr
)
65 sprintf(sys_name
, "md%d", devnum
);
67 sprintf(sys_name
, "md_d%d",
70 sprintf(fname
, "/sys/block/%s/md/", sys_name
);
72 strcat(fname
, devname
);
76 fd
= open(fname
, O_RDWR
);
77 if (fd
< 0 && errno
== -EACCES
)
78 fd
= open(fname
, O_RDONLY
);
82 struct mdinfo
*sysfs_read(int fd
, int devnum
, unsigned long options
)
84 /* Longest possible name in sysfs, mounted at /sys, is
85 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
86 * /sys/block/md_dXXX/md/metadata_version
87 * which is about 41 characters. 50 should do for now
98 sra
= malloc(sizeof(*sra
));
106 if (fstat(fd
, &stb
)) return NULL
;
107 if (ioctl(fd
, RAID_VERSION
, &vers
) != 0)
109 if (major(stb
.st_rdev
)==9)
110 sprintf(sra
->sys_name
, "md%d", (int)minor(stb
.st_rdev
));
112 sprintf(sra
->sys_name
, "md_d%d",
113 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
116 sprintf(sra
->sys_name
, "md%d", devnum
);
118 sprintf(sra
->sys_name
, "md_d%d",
121 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
122 base
= fname
+ strlen(fname
);
125 if (options
& GET_VERSION
) {
126 strcpy(base
, "metadata_version");
127 if (load_sys(fname
, buf
))
129 if (strncmp(buf
, "none", 4) == 0) {
130 sra
->array
.major_version
=
131 sra
->array
.minor_version
= -1;
132 strcpy(sra
->text_version
, "");
133 } else if (strncmp(buf
, "external:", 9) == 0) {
134 sra
->array
.major_version
= -1;
135 sra
->array
.minor_version
= -2;
136 strcpy(sra
->text_version
, buf
+9);
139 &sra
->array
.major_version
,
140 &sra
->array
.minor_version
);
142 if (options
& GET_LEVEL
) {
143 strcpy(base
, "level");
144 if (load_sys(fname
, buf
))
146 sra
->array
.level
= map_name(pers
, buf
);
148 if (options
& GET_LAYOUT
) {
149 strcpy(base
, "layout");
150 if (load_sys(fname
, buf
))
152 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
154 if (options
& GET_DISKS
) {
155 strcpy(base
, "raid_disks");
156 if (load_sys(fname
, buf
))
158 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
160 if (options
& GET_COMPONENT
) {
161 strcpy(base
, "component_size");
162 if (load_sys(fname
, buf
))
164 sra
->component_size
= strtoull(buf
, NULL
, 0);
165 /* sysfs reports "K", but we want sectors */
166 sra
->component_size
*= 2;
168 if (options
& GET_CHUNK
) {
169 strcpy(base
, "chunk_size");
170 if (load_sys(fname
, buf
))
172 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
174 if (options
& GET_CACHE
) {
175 strcpy(base
, "stripe_cache_size");
176 if (load_sys(fname
, buf
))
178 sra
->cache_size
= strtoul(buf
, NULL
, 0);
180 if (options
& GET_MISMATCH
) {
181 strcpy(base
, "mismatch_cnt");
182 if (load_sys(fname
, buf
))
184 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
187 if (! (options
& GET_DEVS
))
190 /* Get all the devices as well */
192 dir
= opendir(fname
);
195 sra
->array
.spare_disks
= 0;
197 while ((de
= readdir(dir
)) != NULL
) {
199 if (de
->d_ino
== 0 ||
200 strncmp(de
->d_name
, "dev-", 4) != 0)
202 strcpy(base
, de
->d_name
);
203 dbase
= base
+ strlen(base
);
206 dev
= malloc(sizeof(*dev
));
209 dev
->next
= sra
->devs
;
211 strcpy(dev
->sys_name
, de
->d_name
);
213 /* Always get slot, major, minor */
214 strcpy(dbase
, "slot");
215 if (load_sys(fname
, buf
))
217 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
218 if (*ep
) dev
->disk
.raid_disk
= -1;
220 strcpy(dbase
, "block/dev");
221 if (load_sys(fname
, buf
))
223 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
225 if (options
& GET_OFFSET
) {
226 strcpy(dbase
, "offset");
227 if (load_sys(fname
, buf
))
229 dev
->data_offset
= strtoull(buf
, NULL
, 0);
231 if (options
& GET_SIZE
) {
232 strcpy(dbase
, "size");
233 if (load_sys(fname
, buf
))
235 dev
->component_size
= strtoull(buf
, NULL
, 0);
237 if (options
& GET_STATE
) {
239 strcpy(dbase
, "state");
240 if (load_sys(fname
, buf
))
242 if (strstr(buf
, "in_sync"))
243 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
244 if (strstr(buf
, "faulty"))
245 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
246 if (dev
->disk
.state
== 0)
247 sra
->array
.spare_disks
++;
249 if (options
& GET_ERROR
) {
250 strcpy(buf
, "errors");
251 if (load_sys(fname
, buf
))
253 dev
->errors
= strtoul(buf
, NULL
, 0);
263 unsigned long long get_component_size(int fd
)
265 /* Find out the component size of the array.
266 * We cannot trust GET_ARRAY_INFO ioctl as it's
267 * size field is only 32bits.
268 * So look in /sys/block/mdXXX/md/component_size
270 * This returns in units of sectors.
275 if (fstat(fd
, &stb
)) return 0;
276 if (major(stb
.st_rdev
) == 9)
277 sprintf(fname
, "/sys/block/md%d/md/component_size",
278 (int)minor(stb
.st_rdev
));
280 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
281 (int)minor(stb
.st_rdev
)>>MdpMinorShift
);
282 fd
= open(fname
, O_RDONLY
);
285 n
= read(fd
, fname
, sizeof(fname
));
287 if (n
== sizeof(fname
))
290 return strtoull(fname
, NULL
, 10) * 2;
293 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
294 char *name
, char *val
)
299 sprintf(fname
, "/sys/block/%s/md/%s/%s",
300 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
301 fd
= open(fname
, O_WRONLY
);
304 n
= write(fd
, val
, strlen(val
));
306 if (n
!= strlen(val
))
311 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
312 char *name
, unsigned long long val
)
315 sprintf(valstr
, "%llu", val
);
316 return sysfs_set_str(sra
, dev
, name
, valstr
);
319 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
320 char *name
, unsigned long long *val
)
327 sprintf(fname
, "/sys/block/%s/md/%s/%s",
328 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
329 fd
= open(fname
, O_RDONLY
);
332 n
= read(fd
, buf
, sizeof(buf
));
337 *val
= strtoull(buf
, &ep
, 0);
338 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
343 int sysfs_set_array(struct mdinfo
*sra
,
347 sra
->array
= info
->array
;
349 if (info
->array
.level
< 0)
350 return 0; /* FIXME */
351 rv
|= sysfs_set_str(sra
, NULL
, "level",
352 map_num(pers
, info
->array
.level
));
353 rv
|= sysfs_set_num(sra
, NULL
, "raid_disks", info
->array
.raid_disks
);
354 rv
|= sysfs_set_num(sra
, NULL
, "chunk_size", info
->array
.chunk_size
);
355 rv
|= sysfs_set_num(sra
, NULL
, "layout", info
->array
.layout
);
356 rv
|= sysfs_set_num(sra
, NULL
, "component_size", info
->component_size
);
357 rv
|= sysfs_set_num(sra
, NULL
, "resync_start", info
->resync_start
);
358 sra
->array
= info
->array
;
362 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
)
370 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
371 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
375 memset(nm
, 0, sizeof(nm
));
376 sprintf(dv
, "/sys/dev/block/%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
377 if (readlink(dv
, nm
, sizeof(nm
)) < 0)
379 dname
= strrchr(nm
, '/');
381 strcpy(sd
->sys_name
, "dev-");
382 strcpy(sd
->sys_name
+4, dname
);
384 rv
|= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
385 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
386 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
387 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
388 // rv |= sysfs_set_str(sra, sd, "state", "in_sync");
390 sd2
= malloc(sizeof(*sd2
));
392 sd2
->next
= sra
->devs
;