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 sysarray
*sra
)
50 struct sysdev
*d
= sra
->devs
;
57 struct sysarray
*sysfs_read(int fd
, int devnum
, unsigned long options
)
59 /* Longest possible name in sysfs, mounted at /sys, is
60 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
61 * /sys/block/md_dXXX/md/metadata_version
62 * which is about 41 characters. 50 should do for now
73 sra
= malloc(sizeof(*sra
));
79 if (fstat(fd
, &stb
)) return NULL
;
80 if (major(stb
.st_rdev
)==9)
81 sprintf(sra
->name
, "md%d", minor(stb
.st_rdev
));
83 sprintf(sra
->name
, "md_d%d",
84 minor(stb
.st_rdev
)>>MdpMinorShift
);
87 sprintf(sra
->name
, "md%d", devnum
);
89 sprintf(sra
->name
, "md_d%d",
92 sprintf(fname
, "/sys/block/%s/md/", sra
->name
);
93 base
= fname
+ strlen(fname
);
96 if (options
& GET_VERSION
) {
97 strcpy(base
, "metadata_version");
98 if (load_sys(fname
, buf
))
100 if (strncmp(buf
, "none", 4) == 0)
101 sra
->major_version
= sra
->minor_version
= -1;
104 &sra
->major_version
, &sra
->minor_version
);
106 if (options
& GET_LEVEL
) {
107 strcpy(base
, "level");
108 if (load_sys(fname
, buf
))
110 sra
->level
= map_name(pers
, buf
);
112 if (options
& GET_LAYOUT
) {
113 strcpy(base
, "layout");
114 if (load_sys(fname
, buf
))
116 sra
->layout
= strtoul(buf
, NULL
, 0);
118 if (options
& GET_COMPONENT
) {
119 strcpy(base
, "component_size");
120 if (load_sys(fname
, buf
))
122 sra
->component_size
= strtoull(buf
, NULL
, 0);
123 /* sysfs reports "K", but we want sectors */
124 sra
->component_size
*= 2;
126 if (options
& GET_CHUNK
) {
127 strcpy(base
, "chunk_size");
128 if (load_sys(fname
, buf
))
130 sra
->chunk
= strtoul(buf
, NULL
, 0);
132 if (options
& GET_CACHE
) {
133 strcpy(base
, "stripe_cache_size");
134 if (load_sys(fname
, buf
))
136 sra
->cache_size
= strtoul(buf
, NULL
, 0);
138 if (options
& GET_MISMATCH
) {
139 strcpy(base
, "mismatch_cnt");
140 if (load_sys(fname
, buf
))
142 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
145 if (! (options
& GET_DEVS
))
148 /* Get all the devices as well */
150 dir
= opendir(fname
);
155 while ((de
= readdir(dir
)) != NULL
) {
157 if (de
->d_ino
== 0 ||
158 strncmp(de
->d_name
, "dev-", 4) != 0)
160 strcpy(base
, de
->d_name
);
161 dbase
= base
+ strlen(base
);
164 dev
= malloc(sizeof(*dev
));
167 dev
->next
= sra
->devs
;
169 strcpy(dev
->name
, de
->d_name
);
171 /* Always get slot, major, minor */
172 strcpy(dbase
, "slot");
173 if (load_sys(fname
, buf
))
175 dev
->role
= strtoul(buf
, &ep
, 10);
176 if (*ep
) dev
->role
= -1;
178 strcpy(dbase
, "block/dev");
179 if (load_sys(fname
, buf
))
181 sscanf(buf
, "%d:%d", &dev
->major
, &dev
->minor
);
183 if (options
& GET_OFFSET
) {
184 strcpy(dbase
, "offset");
185 if (load_sys(fname
, buf
))
187 dev
->offset
= strtoull(buf
, NULL
, 0);
189 if (options
& GET_SIZE
) {
190 strcpy(dbase
, "size");
191 if (load_sys(fname
, buf
))
193 dev
->size
= strtoull(buf
, NULL
, 0);
195 if (options
& GET_STATE
) {
197 strcpy(dbase
, "state");
198 if (load_sys(fname
, buf
))
200 if (strstr(buf
, "in_sync"))
201 dev
->state
|= (1<<MD_DISK_SYNC
);
202 if (strstr(buf
, "faulty"))
203 dev
->state
|= (1<<MD_DISK_FAULTY
);
207 if (options
& GET_ERROR
) {
208 strcpy(buf
, "errors");
209 if (load_sys(fname
, buf
))
211 dev
->errors
= strtoul(buf
, NULL
, 0);
221 unsigned long long get_component_size(int fd
)
223 /* Find out the component size of the array.
224 * We cannot trust GET_ARRAY_INFO ioctl as it's
225 * size field is only 32bits.
226 * So look in /sys/block/mdXXX/md/component_size
228 * This returns in units of sectors.
233 if (fstat(fd
, &stb
)) return 0;
234 if (major(stb
.st_rdev
) == 9)
235 sprintf(fname
, "/sys/block/md%d/md/component_size",
238 sprintf(fname
, "/sys/block/md_d%d/md/component_size",
239 minor(stb
.st_rdev
)>>MdpMinorShift
);
240 fd
= open(fname
, O_RDONLY
);
243 n
= read(fd
, fname
, sizeof(fname
));
245 if (n
== sizeof(fname
))
248 return strtoull(fname
, NULL
, 10) * 2;
251 int sysfs_set_str(struct sysarray
*sra
, struct sysdev
*dev
,
252 char *name
, char *val
)
257 sprintf(fname
, "/sys/block/%s/md/%s/%s",
258 sra
->name
, dev
?dev
->name
:"", name
);
259 fd
= open(fname
, O_WRONLY
);
262 n
= write(fd
, val
, strlen(val
));
264 if (n
!= strlen(val
))
269 int sysfs_set_num(struct sysarray
*sra
, struct sysdev
*dev
,
270 char *name
, unsigned long long val
)
273 sprintf(valstr
, "%llu", val
);
274 return sysfs_set_str(sra
, dev
, name
, valstr
);
277 int sysfs_get_ll(struct sysarray
*sra
, struct sysdev
*dev
,
278 char *name
, unsigned long long *val
)
285 sprintf(fname
, "/sys/block/%s/md/%s/%s",
286 sra
->name
, dev
?dev
->name
:"", name
);
287 fd
= open(fname
, O_RDONLY
);
290 n
= read(fd
, buf
, sizeof(buf
));
295 *val
= strtoull(buf
, &ep
, 0);
296 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))