]>
Commit | Line | Data |
---|---|---|
e86c9dd6 NB |
1 | /* |
2 | * sysfs - extract md related information from sysfs. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006 Neil Brown <neilb@suse.de> | |
6 | * | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | */ | |
25 | ||
26 | #include "mdadm.h" | |
27 | #include <dirent.h> | |
28 | ||
29 | int load_sys(char *path, char *buf) | |
30 | { | |
31 | int fd = open(path, O_RDONLY); | |
32 | int n; | |
33 | if (fd < 0) | |
34 | return -1; | |
35 | n = read(fd, buf, 1024); | |
36 | close(fd); | |
37 | if (n <=0 || n >= 1024) | |
38 | return -1; | |
39 | buf[n] = 0; | |
40 | if (buf[n-1] == '\n') | |
41 | buf[n-1] = 0; | |
42 | return 0; | |
43 | } | |
44 | ||
7e0f6979 | 45 | void sysfs_free(struct mdinfo *sra) |
8382f19b | 46 | { |
7e0f6979 NB |
47 | while (sra) { |
48 | struct mdinfo *sra2 = sra->next; | |
49 | while (sra->devs) { | |
50 | struct mdinfo *d = sra->devs; | |
51 | sra->devs = d->next; | |
52 | free(d); | |
53 | } | |
54 | free(sra); | |
55 | sra = sra2; | |
8382f19b | 56 | } |
8382f19b NB |
57 | } |
58 | ||
7e0f6979 | 59 | struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options) |
e86c9dd6 NB |
60 | { |
61 | /* Longest possible name in sysfs, mounted at /sys, is | |
62 | * /sys/block/md_dXXX/md/dev-XXXXX/block/dev | |
63 | * /sys/block/md_dXXX/md/metadata_version | |
64 | * which is about 41 characters. 50 should do for now | |
65 | */ | |
66 | char fname[50]; | |
67 | char buf[1024]; | |
68 | char *base; | |
69 | char *dbase; | |
7e0f6979 | 70 | struct mdinfo *sra; |
06c7f68e | 71 | struct mdinfo *dev; |
e86c9dd6 NB |
72 | DIR *dir; |
73 | struct dirent *de; | |
74 | ||
75 | sra = malloc(sizeof(*sra)); | |
76 | if (sra == NULL) | |
77 | return sra; | |
7e0f6979 | 78 | sra->next = NULL; |
e86c9dd6 NB |
79 | |
80 | if (fd >= 0) { | |
81 | struct stat stb; | |
2faf1f5f NB |
82 | mdu_version_t vers; |
83 | if (fstat(fd, &stb)) return NULL; | |
84 | if (ioctl(fd, RAID_VERSION, &vers) != 0) | |
85 | return NULL; | |
4ebd3237 | 86 | if (major(stb.st_rdev) == MD_MAJOR) |
ea24acd0 | 87 | sprintf(sra->sys_name, "md%d", (int)minor(stb.st_rdev)); |
4ebd3237 | 88 | else if (major(stb.st_rdev) == get_mdp_major()) |
7e0f6979 | 89 | sprintf(sra->sys_name, "md_d%d", |
ea24acd0 | 90 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
4ebd3237 N |
91 | else { |
92 | /* must be an extended-minor partition. Look at the | |
93 | * /sys/dev/block/%d:%d link which must look like | |
94 | * ../../block/mdXXX/mdXXXpYY | |
95 | */ | |
96 | char path[30]; | |
97 | char link[200]; | |
98 | char *cp; | |
99 | int n; | |
100 | sprintf(path, "/sys/dev/block/%d:%d", major(stb.st_rdev), | |
101 | minor(stb.st_rdev)); | |
102 | n = readlink(path, link, sizeof(link)-1); | |
103 | if (n <= 0) | |
104 | return NULL; | |
105 | link[n] = 0; | |
106 | cp = strrchr(link, '/'); | |
107 | if (cp) *cp = 0; | |
108 | cp = strchr(link, '/'); | |
109 | if (cp && strncmp(cp, "/md", 3) == 0) | |
110 | strcpy(sra->sys_name, cp+1); | |
111 | else | |
112 | return NULL; | |
113 | } | |
e86c9dd6 NB |
114 | } else { |
115 | if (devnum >= 0) | |
7e0f6979 | 116 | sprintf(sra->sys_name, "md%d", devnum); |
e86c9dd6 | 117 | else |
7e0f6979 | 118 | sprintf(sra->sys_name, "md_d%d", |
e86c9dd6 NB |
119 | -1-devnum); |
120 | } | |
7e0f6979 | 121 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
122 | base = fname + strlen(fname); |
123 | ||
124 | sra->devs = NULL; | |
8382f19b NB |
125 | if (options & GET_VERSION) { |
126 | strcpy(base, "metadata_version"); | |
127 | if (load_sys(fname, buf)) | |
128 | goto abort; | |
294d6f45 | 129 | if (strncmp(buf, "none", 4) == 0) { |
7e0f6979 NB |
130 | sra->array.major_version = |
131 | sra->array.minor_version = -1; | |
294d6f45 NB |
132 | strcpy(sra->text_version, ""); |
133 | } else if (strncmp(buf, "external:", 9) == 0) { | |
142cb9e1 NB |
134 | sra->array.major_version = -1; |
135 | sra->array.minor_version = -2; | |
136 | strcpy(sra->text_version, buf+9); | |
137 | } else | |
8382f19b | 138 | sscanf(buf, "%d.%d", |
7e0f6979 NB |
139 | &sra->array.major_version, |
140 | &sra->array.minor_version); | |
8382f19b | 141 | } |
e86c9dd6 NB |
142 | if (options & GET_LEVEL) { |
143 | strcpy(base, "level"); | |
144 | if (load_sys(fname, buf)) | |
145 | goto abort; | |
7e0f6979 | 146 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
147 | } |
148 | if (options & GET_LAYOUT) { | |
149 | strcpy(base, "layout"); | |
150 | if (load_sys(fname, buf)) | |
151 | goto abort; | |
7e0f6979 | 152 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 NB |
153 | } |
154 | if (options & GET_COMPONENT) { | |
155 | strcpy(base, "component_size"); | |
156 | if (load_sys(fname, buf)) | |
157 | goto abort; | |
158 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
159 | /* sysfs reports "K", but we want sectors */ |
160 | sra->component_size *= 2; | |
e86c9dd6 NB |
161 | } |
162 | if (options & GET_CHUNK) { | |
163 | strcpy(base, "chunk_size"); | |
164 | if (load_sys(fname, buf)) | |
165 | goto abort; | |
7e0f6979 | 166 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 167 | } |
758d3a8e NB |
168 | if (options & GET_CACHE) { |
169 | strcpy(base, "stripe_cache_size"); | |
170 | if (load_sys(fname, buf)) | |
171 | goto abort; | |
172 | sra->cache_size = strtoul(buf, NULL, 0); | |
173 | } | |
37dfc3d6 NB |
174 | if (options & GET_MISMATCH) { |
175 | strcpy(base, "mismatch_cnt"); | |
176 | if (load_sys(fname, buf)) | |
177 | goto abort; | |
178 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
179 | } | |
e86c9dd6 NB |
180 | |
181 | if (! (options & GET_DEVS)) | |
182 | return sra; | |
183 | ||
184 | /* Get all the devices as well */ | |
185 | *base = 0; | |
186 | dir = opendir(fname); | |
187 | if (!dir) | |
188 | goto abort; | |
7e0f6979 | 189 | sra->array.spare_disks = 0; |
e86c9dd6 NB |
190 | |
191 | while ((de = readdir(dir)) != NULL) { | |
192 | char *ep; | |
193 | if (de->d_ino == 0 || | |
194 | strncmp(de->d_name, "dev-", 4) != 0) | |
195 | continue; | |
196 | strcpy(base, de->d_name); | |
197 | dbase = base + strlen(base); | |
198 | *dbase++ = '/'; | |
199 | ||
200 | dev = malloc(sizeof(*dev)); | |
201 | if (!dev) | |
202 | goto abort; | |
203 | dev->next = sra->devs; | |
204 | sra->devs = dev; | |
06c7f68e | 205 | strcpy(dev->sys_name, de->d_name); |
e86c9dd6 NB |
206 | |
207 | /* Always get slot, major, minor */ | |
208 | strcpy(dbase, "slot"); | |
209 | if (load_sys(fname, buf)) | |
210 | goto abort; | |
06c7f68e NB |
211 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
212 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
213 | |
214 | strcpy(dbase, "block/dev"); | |
215 | if (load_sys(fname, buf)) | |
216 | goto abort; | |
06c7f68e | 217 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 NB |
218 | |
219 | if (options & GET_OFFSET) { | |
220 | strcpy(dbase, "offset"); | |
221 | if (load_sys(fname, buf)) | |
222 | goto abort; | |
06c7f68e | 223 | dev->data_offset = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
224 | } |
225 | if (options & GET_SIZE) { | |
226 | strcpy(dbase, "size"); | |
227 | if (load_sys(fname, buf)) | |
228 | goto abort; | |
06c7f68e | 229 | dev->component_size = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
230 | } |
231 | if (options & GET_STATE) { | |
06c7f68e | 232 | dev->disk.state = 0; |
e86c9dd6 NB |
233 | strcpy(dbase, "state"); |
234 | if (load_sys(fname, buf)) | |
235 | goto abort; | |
236 | if (strstr(buf, "in_sync")) | |
06c7f68e | 237 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 238 | if (strstr(buf, "faulty")) |
06c7f68e NB |
239 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
240 | if (dev->disk.state == 0) | |
7e0f6979 | 241 | sra->array.spare_disks++; |
e86c9dd6 NB |
242 | } |
243 | if (options & GET_ERROR) { | |
244 | strcpy(buf, "errors"); | |
245 | if (load_sys(fname, buf)) | |
246 | goto abort; | |
247 | dev->errors = strtoul(buf, NULL, 0); | |
248 | } | |
249 | } | |
250 | return sra; | |
251 | ||
252 | abort: | |
8382f19b | 253 | sysfs_free(sra); |
e86c9dd6 NB |
254 | return NULL; |
255 | } | |
256 | ||
257 | unsigned long long get_component_size(int fd) | |
258 | { | |
259 | /* Find out the component size of the array. | |
260 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
261 | * size field is only 32bits. | |
262 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 263 | * |
8686f3ed | 264 | * This returns in units of sectors. |
e86c9dd6 NB |
265 | */ |
266 | struct stat stb; | |
267 | char fname[50]; | |
268 | int n; | |
269 | if (fstat(fd, &stb)) return 0; | |
4ebd3237 | 270 | if (major(stb.st_rdev) != get_mdp_major()) |
e86c9dd6 | 271 | sprintf(fname, "/sys/block/md%d/md/component_size", |
ea24acd0 | 272 | (int)minor(stb.st_rdev)); |
e86c9dd6 NB |
273 | else |
274 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
ea24acd0 | 275 | (int)minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
276 | fd = open(fname, O_RDONLY); |
277 | if (fd < 0) | |
278 | return 0; | |
279 | n = read(fd, fname, sizeof(fname)); | |
280 | close(fd); | |
281 | if (n == sizeof(fname)) | |
282 | return 0; | |
283 | fname[n] = 0; | |
8686f3ed | 284 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
285 | } |
286 | ||
7e0f6979 | 287 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
288 | char *name, char *val) |
289 | { | |
290 | char fname[50]; | |
291 | int n; | |
292 | int fd; | |
293 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
7e0f6979 | 294 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
295 | fd = open(fname, O_WRONLY); |
296 | if (fd < 0) | |
297 | return -1; | |
298 | n = write(fd, val, strlen(val)); | |
299 | close(fd); | |
300 | if (n != strlen(val)) | |
301 | return -1; | |
302 | return 0; | |
303 | } | |
304 | ||
7e0f6979 | 305 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
306 | char *name, unsigned long long val) |
307 | { | |
308 | char valstr[50]; | |
309 | sprintf(valstr, "%llu", val); | |
310 | return sysfs_set_str(sra, dev, name, valstr); | |
311 | } | |
312 | ||
7e0f6979 | 313 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
314 | char *name, unsigned long long *val) |
315 | { | |
316 | char fname[50]; | |
317 | char buf[50]; | |
318 | int n; | |
319 | int fd; | |
320 | char *ep; | |
321 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
7e0f6979 | 322 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
323 | fd = open(fname, O_RDONLY); |
324 | if (fd < 0) | |
325 | return -1; | |
326 | n = read(fd, buf, sizeof(buf)); | |
327 | close(fd); | |
328 | if (n <= 0) | |
329 | return -1; | |
330 | buf[n] = 0; | |
331 | *val = strtoull(buf, &ep, 0); | |
332 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
333 | return -1; | |
334 | return 0; | |
335 | } |