]>
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 | * | |
e736b623 | 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
e86c9dd6 NB |
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> | |
1770662b | 28 | #include <ctype.h> |
e86c9dd6 | 29 | |
bbb52f2b TM |
30 | #define MAX_SYSFS_PATH_LEN 120 |
31 | ||
193b6c0b | 32 | int load_sys(char *path, char *buf, int len) |
e86c9dd6 NB |
33 | { |
34 | int fd = open(path, O_RDONLY); | |
35 | int n; | |
36 | if (fd < 0) | |
37 | return -1; | |
193b6c0b | 38 | n = read(fd, buf, len); |
e86c9dd6 | 39 | close(fd); |
193b6c0b | 40 | if (n <0 || n >= len) |
e86c9dd6 NB |
41 | return -1; |
42 | buf[n] = 0; | |
8dfb8619 | 43 | if (n && buf[n-1] == '\n') |
e86c9dd6 NB |
44 | buf[n-1] = 0; |
45 | return 0; | |
46 | } | |
47 | ||
7e0f6979 | 48 | void sysfs_free(struct mdinfo *sra) |
8382f19b | 49 | { |
7e0f6979 NB |
50 | while (sra) { |
51 | struct mdinfo *sra2 = sra->next; | |
52 | while (sra->devs) { | |
53 | struct mdinfo *d = sra->devs; | |
54 | sra->devs = d->next; | |
bb758cca | 55 | free(d->bb.entries); |
7e0f6979 NB |
56 | free(d); |
57 | } | |
bb758cca | 58 | free(sra->bb.entries); |
7e0f6979 NB |
59 | free(sra); |
60 | sra = sra2; | |
8382f19b | 61 | } |
8382f19b NB |
62 | } |
63 | ||
4dd2df09 | 64 | int sysfs_open(char *devnm, char *devname, char *attr) |
549e9569 | 65 | { |
bbb52f2b | 66 | char fname[MAX_SYSFS_PATH_LEN]; |
549e9569 | 67 | int fd; |
549e9569 | 68 | |
bbb52f2b | 69 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/", devnm); |
549e9569 | 70 | if (devname) { |
bbb52f2b TM |
71 | strncat(fname, devname, MAX_SYSFS_PATH_LEN - strlen(fname)); |
72 | strncat(fname, "/", MAX_SYSFS_PATH_LEN - strlen(fname)); | |
549e9569 | 73 | } |
bbb52f2b | 74 | strncat(fname, attr, MAX_SYSFS_PATH_LEN - strlen(fname)); |
549e9569 | 75 | fd = open(fname, O_RDWR); |
ea6d09b0 | 76 | if (fd < 0 && errno == EACCES) |
549e9569 NB |
77 | fd = open(fname, O_RDONLY); |
78 | return fd; | |
79 | } | |
80 | ||
9465f170 GJ |
81 | void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid) |
82 | { | |
83 | snprintf(mdi->sys_name, | |
84 | sizeof(mdi->sys_name), "dev-%s", devid2kname(devid)); | |
85 | } | |
86 | ||
dae13137 | 87 | int sysfs_init(struct mdinfo *mdi, int fd, char *devnm) |
f35f2525 | 88 | { |
67a02d52 JS |
89 | struct stat stb; |
90 | char fname[MAX_SYSFS_PATH_LEN]; | |
dae13137 | 91 | int retval = -ENODEV; |
67a02d52 | 92 | |
678a4a36 | 93 | mdi->sys_name[0] = 0; |
67a02d52 | 94 | if (fd >= 0) |
4dd2df09 | 95 | devnm = fd2devnm(fd); |
67a02d52 | 96 | |
4dd2df09 | 97 | if (devnm == NULL) |
dae13137 | 98 | goto out; |
67a02d52 JS |
99 | |
100 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md", devnm); | |
101 | ||
102 | if (stat(fname, &stb)) | |
dae13137 | 103 | goto out; |
67a02d52 | 104 | if (!S_ISDIR(stb.st_mode)) |
dae13137 | 105 | goto out; |
4dd2df09 | 106 | strcpy(mdi->sys_name, devnm); |
dae13137 JS |
107 | |
108 | retval = 0; | |
109 | out: | |
110 | return retval; | |
f35f2525 N |
111 | } |
112 | ||
4dd2df09 | 113 | struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options) |
e86c9dd6 | 114 | { |
33a6535d AW |
115 | char fname[PATH_MAX]; |
116 | char buf[PATH_MAX]; | |
e86c9dd6 NB |
117 | char *base; |
118 | char *dbase; | |
7e0f6979 | 119 | struct mdinfo *sra; |
64e103fe | 120 | struct mdinfo *dev, **devp; |
355726fa | 121 | DIR *dir = NULL; |
e86c9dd6 NB |
122 | struct dirent *de; |
123 | ||
503975b9 | 124 | sra = xcalloc(1, sizeof(*sra)); |
dae13137 | 125 | if (sysfs_init(sra, fd, devnm)) { |
678a4a36 N |
126 | free(sra); |
127 | return NULL; | |
128 | } | |
e86c9dd6 | 129 | |
7e0f6979 | 130 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
131 | base = fname + strlen(fname); |
132 | ||
133 | sra->devs = NULL; | |
8382f19b NB |
134 | if (options & GET_VERSION) { |
135 | strcpy(base, "metadata_version"); | |
193b6c0b | 136 | if (load_sys(fname, buf, sizeof(buf))) |
8382f19b | 137 | goto abort; |
294d6f45 | 138 | if (strncmp(buf, "none", 4) == 0) { |
7e0f6979 NB |
139 | sra->array.major_version = |
140 | sra->array.minor_version = -1; | |
294d6f45 NB |
141 | strcpy(sra->text_version, ""); |
142 | } else if (strncmp(buf, "external:", 9) == 0) { | |
142cb9e1 NB |
143 | sra->array.major_version = -1; |
144 | sra->array.minor_version = -2; | |
145 | strcpy(sra->text_version, buf+9); | |
b8ac1967 | 146 | } else { |
8382f19b | 147 | sscanf(buf, "%d.%d", |
7e0f6979 NB |
148 | &sra->array.major_version, |
149 | &sra->array.minor_version); | |
b8ac1967 NB |
150 | strcpy(sra->text_version, buf); |
151 | } | |
8382f19b | 152 | } |
e86c9dd6 NB |
153 | if (options & GET_LEVEL) { |
154 | strcpy(base, "level"); | |
193b6c0b | 155 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 156 | goto abort; |
7e0f6979 | 157 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
158 | } |
159 | if (options & GET_LAYOUT) { | |
160 | strcpy(base, "layout"); | |
193b6c0b | 161 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 162 | goto abort; |
7e0f6979 | 163 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 | 164 | } |
549e9569 NB |
165 | if (options & GET_DISKS) { |
166 | strcpy(base, "raid_disks"); | |
193b6c0b | 167 | if (load_sys(fname, buf, sizeof(buf))) |
549e9569 NB |
168 | goto abort; |
169 | sra->array.raid_disks = strtoul(buf, NULL, 0); | |
f1d26766 DW |
170 | } |
171 | if (options & GET_DEGRADED) { | |
172 | strcpy(base, "degraded"); | |
193b6c0b | 173 | if (load_sys(fname, buf, sizeof(buf))) |
f1d26766 DW |
174 | goto abort; |
175 | sra->array.failed_disks = strtoul(buf, NULL, 0); | |
549e9569 | 176 | } |
e86c9dd6 NB |
177 | if (options & GET_COMPONENT) { |
178 | strcpy(base, "component_size"); | |
193b6c0b | 179 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
180 | goto abort; |
181 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
182 | /* sysfs reports "K", but we want sectors */ |
183 | sra->component_size *= 2; | |
e86c9dd6 NB |
184 | } |
185 | if (options & GET_CHUNK) { | |
186 | strcpy(base, "chunk_size"); | |
193b6c0b | 187 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 188 | goto abort; |
7e0f6979 | 189 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 190 | } |
758d3a8e NB |
191 | if (options & GET_CACHE) { |
192 | strcpy(base, "stripe_cache_size"); | |
193b6c0b | 193 | if (load_sys(fname, buf, sizeof(buf))) |
6a67848a N |
194 | /* Probably level doesn't support it */ |
195 | sra->cache_size = 0; | |
196 | else | |
197 | sra->cache_size = strtoul(buf, NULL, 0); | |
758d3a8e | 198 | } |
37dfc3d6 NB |
199 | if (options & GET_MISMATCH) { |
200 | strcpy(base, "mismatch_cnt"); | |
193b6c0b | 201 | if (load_sys(fname, buf, sizeof(buf))) |
37dfc3d6 NB |
202 | goto abort; |
203 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
204 | } | |
1770662b DW |
205 | if (options & GET_SAFEMODE) { |
206 | int scale = 1; | |
207 | int dot = 0; | |
f21e18ca | 208 | unsigned i; |
1770662b DW |
209 | unsigned long msec; |
210 | size_t len; | |
211 | ||
212 | strcpy(base, "safe_mode_delay"); | |
193b6c0b | 213 | if (load_sys(fname, buf, sizeof(buf))) |
1770662b DW |
214 | goto abort; |
215 | ||
216 | /* remove a period, and count digits after it */ | |
217 | len = strlen(buf); | |
218 | for (i = 0; i < len; i++) { | |
219 | if (dot) { | |
220 | if (isdigit(buf[i])) { | |
221 | buf[i-1] = buf[i]; | |
222 | scale *= 10; | |
223 | } | |
224 | buf[i] = 0; | |
225 | } else if (buf[i] == '.') { | |
226 | dot=1; | |
227 | buf[i] = 0; | |
228 | } | |
229 | } | |
230 | msec = strtoul(buf, NULL, 10); | |
231 | msec = (msec * 1000) / scale; | |
232 | sra->safe_mode_delay = msec; | |
233 | } | |
c0c1acd6 N |
234 | if (options & GET_BITMAP_LOCATION) { |
235 | strcpy(base, "bitmap/location"); | |
193b6c0b | 236 | if (load_sys(fname, buf, sizeof(buf))) |
c0c1acd6 N |
237 | goto abort; |
238 | if (strncmp(buf, "file", 4) == 0) | |
239 | sra->bitmap_offset = 1; | |
240 | else if (strncmp(buf, "none", 4) == 0) | |
241 | sra->bitmap_offset = 0; | |
242 | else if (buf[0] == '+') | |
fbdef498 | 243 | sra->bitmap_offset = strtol(buf+1, NULL, 10); |
c0c1acd6 N |
244 | else |
245 | goto abort; | |
246 | } | |
e86c9dd6 | 247 | |
5aa644c6 SL |
248 | if (options & GET_ARRAY_STATE) { |
249 | strcpy(base, "array_state"); | |
5e4ca8bb | 250 | if (load_sys(fname, buf, sizeof(buf))) |
5aa644c6 | 251 | goto abort; |
5e4ca8bb | 252 | sra->array_state = map_name(sysfs_array_states, buf); |
5e4ca8bb | 253 | } |
5aa644c6 | 254 | |
5308f117 AP |
255 | if (options & GET_CONSISTENCY_POLICY) { |
256 | strcpy(base, "consistency_policy"); | |
257 | if (load_sys(fname, buf, sizeof(buf))) { | |
258 | sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN; | |
259 | } else { | |
260 | sra->consistency_policy = map_name(consistency_policies, buf); | |
261 | if (sra->consistency_policy == UnSet) | |
262 | sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN; | |
263 | } | |
264 | } | |
265 | ||
e86c9dd6 NB |
266 | if (! (options & GET_DEVS)) |
267 | return sra; | |
268 | ||
269 | /* Get all the devices as well */ | |
270 | *base = 0; | |
271 | dir = opendir(fname); | |
272 | if (!dir) | |
273 | goto abort; | |
7e0f6979 | 274 | sra->array.spare_disks = 0; |
e86c9dd6 | 275 | |
64e103fe N |
276 | devp = &sra->devs; |
277 | sra->devs = NULL; | |
e86c9dd6 NB |
278 | while ((de = readdir(dir)) != NULL) { |
279 | char *ep; | |
280 | if (de->d_ino == 0 || | |
281 | strncmp(de->d_name, "dev-", 4) != 0) | |
282 | continue; | |
283 | strcpy(base, de->d_name); | |
284 | dbase = base + strlen(base); | |
285 | *dbase++ = '/'; | |
286 | ||
bb758cca | 287 | dev = xcalloc(1, sizeof(*dev)); |
e86c9dd6 NB |
288 | |
289 | /* Always get slot, major, minor */ | |
290 | strcpy(dbase, "slot"); | |
193b6c0b | 291 | if (load_sys(fname, buf, sizeof(buf))) { |
4795982e DW |
292 | /* hmm... unable to read 'slot' maybe the device |
293 | * is going away? | |
294 | */ | |
295 | strcpy(dbase, "block"); | |
296 | if (readlink(fname, buf, sizeof(buf)) < 0 && | |
297 | errno != ENAMETOOLONG) { | |
298 | /* ...yup device is gone */ | |
299 | free(dev); | |
300 | continue; | |
301 | } else { | |
302 | /* slot is unreadable but 'block' link | |
303 | * still intact... something bad is happening | |
304 | * so abort | |
305 | */ | |
306 | free(dev); | |
307 | goto abort; | |
308 | } | |
64e103fe | 309 | |
4795982e | 310 | } |
4795982e | 311 | strcpy(dev->sys_name, de->d_name); |
06c7f68e NB |
312 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
313 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
314 | |
315 | strcpy(dbase, "block/dev"); | |
193b6c0b | 316 | if (load_sys(fname, buf, sizeof(buf))) { |
b526e52d DW |
317 | /* assume this is a stale reference to a hot |
318 | * removed device | |
319 | */ | |
dab4a513 | 320 | free(dev); |
b526e52d | 321 | continue; |
dab4a513 | 322 | } |
e6fc80a8 | 323 | sra->array.nr_disks++; |
06c7f68e | 324 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 | 325 | |
dab4a513 | 326 | /* special case check for block devices that can go 'offline' */ |
b526e52d | 327 | strcpy(dbase, "block/device/state"); |
193b6c0b | 328 | if (load_sys(fname, buf, sizeof(buf)) == 0 && |
b526e52d DW |
329 | strncmp(buf, "offline", 7) == 0) { |
330 | free(dev); | |
331 | continue; | |
dab4a513 DW |
332 | } |
333 | ||
334 | /* finally add this disk to the array */ | |
64e103fe N |
335 | *devp = dev; |
336 | devp = & dev->next; | |
337 | dev->next = NULL; | |
dab4a513 | 338 | |
e86c9dd6 NB |
339 | if (options & GET_OFFSET) { |
340 | strcpy(dbase, "offset"); | |
193b6c0b | 341 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 342 | goto abort; |
06c7f68e | 343 | dev->data_offset = strtoull(buf, NULL, 0); |
fe384ca0 | 344 | strcpy(dbase, "new_offset"); |
193b6c0b | 345 | if (load_sys(fname, buf, sizeof(buf)) == 0) |
fe384ca0 N |
346 | dev->new_data_offset = strtoull(buf, NULL, 0); |
347 | else | |
348 | dev->new_data_offset = dev->data_offset; | |
e86c9dd6 NB |
349 | } |
350 | if (options & GET_SIZE) { | |
351 | strcpy(dbase, "size"); | |
193b6c0b | 352 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 | 353 | goto abort; |
047d2e49 | 354 | dev->component_size = strtoull(buf, NULL, 0) * 2; |
e86c9dd6 NB |
355 | } |
356 | if (options & GET_STATE) { | |
06c7f68e | 357 | dev->disk.state = 0; |
e86c9dd6 | 358 | strcpy(dbase, "state"); |
193b6c0b | 359 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
360 | goto abort; |
361 | if (strstr(buf, "in_sync")) | |
06c7f68e | 362 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 363 | if (strstr(buf, "faulty")) |
06c7f68e NB |
364 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
365 | if (dev->disk.state == 0) | |
7e0f6979 | 366 | sra->array.spare_disks++; |
e86c9dd6 NB |
367 | } |
368 | if (options & GET_ERROR) { | |
369 | strcpy(buf, "errors"); | |
193b6c0b | 370 | if (load_sys(fname, buf, sizeof(buf))) |
e86c9dd6 NB |
371 | goto abort; |
372 | dev->errors = strtoul(buf, NULL, 0); | |
373 | } | |
374 | } | |
355726fa | 375 | closedir(dir); |
e86c9dd6 NB |
376 | return sra; |
377 | ||
378 | abort: | |
355726fa NB |
379 | if (dir) |
380 | closedir(dir); | |
8382f19b | 381 | sysfs_free(sra); |
e86c9dd6 NB |
382 | return NULL; |
383 | } | |
384 | ||
1770662b DW |
385 | int sysfs_attr_match(const char *attr, const char *str) |
386 | { | |
387 | /* See if attr, read from a sysfs file, matches | |
388 | * str. They must either be the same, or attr can | |
389 | * have a trailing newline or comma | |
390 | */ | |
391 | while (*attr && *str && *attr == *str) { | |
392 | attr++; | |
393 | str++; | |
394 | } | |
395 | ||
396 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
397 | return 0; | |
398 | return 1; | |
399 | } | |
400 | ||
401 | int sysfs_match_word(const char *word, char **list) | |
402 | { | |
403 | int n; | |
404 | for (n=0; list[n]; n++) | |
405 | if (sysfs_attr_match(word, list[n])) | |
406 | break; | |
407 | return n; | |
408 | } | |
409 | ||
e86c9dd6 NB |
410 | unsigned long long get_component_size(int fd) |
411 | { | |
412 | /* Find out the component size of the array. | |
413 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
414 | * size field is only 32bits. | |
415 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 416 | * |
8686f3ed | 417 | * This returns in units of sectors. |
e86c9dd6 NB |
418 | */ |
419 | struct stat stb; | |
bbb52f2b | 420 | char fname[MAX_SYSFS_PATH_LEN]; |
e86c9dd6 | 421 | int n; |
36138e4e JS |
422 | if (fstat(fd, &stb)) |
423 | return 0; | |
c07566f1 N |
424 | snprintf(fname, MAX_SYSFS_PATH_LEN, |
425 | "/sys/block/%s/md/component_size", stat2devnm(&stb)); | |
e86c9dd6 NB |
426 | fd = open(fname, O_RDONLY); |
427 | if (fd < 0) | |
428 | return 0; | |
429 | n = read(fd, fname, sizeof(fname)); | |
430 | close(fd); | |
99f6e521 | 431 | if (n < 0 || n == sizeof(fname)) |
e86c9dd6 NB |
432 | return 0; |
433 | fname[n] = 0; | |
8686f3ed | 434 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
435 | } |
436 | ||
7e0f6979 | 437 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
438 | char *name, char *val) |
439 | { | |
bbb52f2b | 440 | char fname[MAX_SYSFS_PATH_LEN]; |
f21e18ca | 441 | unsigned int n; |
e86c9dd6 | 442 | int fd; |
7e1432fb | 443 | |
bbb52f2b | 444 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 445 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
446 | fd = open(fname, O_WRONLY); |
447 | if (fd < 0) | |
448 | return -1; | |
449 | n = write(fd, val, strlen(val)); | |
450 | close(fd); | |
2a24d7b6 | 451 | if (n != strlen(val)) { |
1ade5cc1 N |
452 | dprintf("failed to write '%s' to '%s' (%s)\n", |
453 | val, fname, strerror(errno)); | |
e86c9dd6 | 454 | return -1; |
2a24d7b6 | 455 | } |
e86c9dd6 NB |
456 | return 0; |
457 | } | |
458 | ||
7e0f6979 | 459 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
460 | char *name, unsigned long long val) |
461 | { | |
462 | char valstr[50]; | |
463 | sprintf(valstr, "%llu", val); | |
464 | return sysfs_set_str(sra, dev, name, valstr); | |
465 | } | |
466 | ||
012a8641 JS |
467 | int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev, |
468 | char *name, long long val) | |
469 | { | |
470 | char valstr[50]; | |
471 | sprintf(valstr, "%lli", val); | |
472 | return sysfs_set_str(sra, dev, name, valstr); | |
473 | } | |
474 | ||
97590376 N |
475 | int sysfs_uevent(struct mdinfo *sra, char *event) |
476 | { | |
bbb52f2b | 477 | char fname[MAX_SYSFS_PATH_LEN]; |
97590376 N |
478 | int n; |
479 | int fd; | |
480 | ||
bbb52f2b | 481 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/uevent", |
97590376 N |
482 | sra->sys_name); |
483 | fd = open(fname, O_WRONLY); | |
484 | if (fd < 0) | |
485 | return -1; | |
486 | n = write(fd, event, strlen(event)); | |
487 | close(fd); | |
e4c72d1d | 488 | if (n != (int)strlen(event)) { |
1ade5cc1 N |
489 | dprintf("failed to write '%s' to '%s' (%s)\n", |
490 | event, fname, strerror(errno)); | |
e4c72d1d LB |
491 | return -1; |
492 | } | |
97590376 | 493 | return 0; |
1011e834 | 494 | } |
97590376 | 495 | |
bc77ed53 DW |
496 | int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name) |
497 | { | |
bbb52f2b | 498 | char fname[MAX_SYSFS_PATH_LEN]; |
bc77ed53 DW |
499 | struct stat st; |
500 | ||
bbb52f2b | 501 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", |
bc77ed53 DW |
502 | sra->sys_name, dev?dev->sys_name:"", name); |
503 | ||
504 | return stat(fname, &st) == 0; | |
505 | } | |
506 | ||
a6288483 N |
507 | int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev, |
508 | char *name) | |
e86c9dd6 | 509 | { |
bbb52f2b | 510 | char fname[MAX_SYSFS_PATH_LEN]; |
e86c9dd6 | 511 | int fd; |
a6288483 | 512 | |
bbb52f2b | 513 | snprintf(fname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/%s/%s", |
7e0f6979 | 514 | sra->sys_name, dev?dev->sys_name:"", name); |
a6288483 | 515 | fd = open(fname, O_RDWR); |
e86c9dd6 | 516 | if (fd < 0) |
a6288483 N |
517 | fd = open(fname, O_RDONLY); |
518 | return fd; | |
519 | } | |
520 | ||
521 | int sysfs_fd_get_ll(int fd, unsigned long long *val) | |
522 | { | |
523 | char buf[50]; | |
524 | int n; | |
525 | char *ep; | |
526 | ||
527 | lseek(fd, 0, 0); | |
e86c9dd6 | 528 | n = read(fd, buf, sizeof(buf)); |
5418499a | 529 | if (n <= 0 || n == sizeof(buf)) |
6560987b | 530 | return -2; |
e86c9dd6 NB |
531 | buf[n] = 0; |
532 | *val = strtoull(buf, &ep, 0); | |
533 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
534 | return -1; | |
535 | return 0; | |
536 | } | |
2503d23b | 537 | |
a6288483 N |
538 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
539 | char *name, unsigned long long *val) | |
540 | { | |
541 | int n; | |
542 | int fd; | |
543 | ||
544 | fd = sysfs_get_fd(sra, dev, name); | |
545 | if (fd < 0) | |
546 | return -1; | |
547 | n = sysfs_fd_get_ll(fd, val); | |
548 | close(fd); | |
549 | return n; | |
550 | } | |
551 | ||
2eba8496 N |
552 | int sysfs_fd_get_two(int fd, unsigned long long *v1, unsigned long long *v2) |
553 | { | |
554 | /* two numbers in this sysfs file, either | |
555 | * NNN (NNN) | |
556 | * or | |
557 | * NNN / NNN | |
558 | */ | |
559 | char buf[80]; | |
560 | int n; | |
561 | char *ep, *ep2; | |
562 | ||
563 | lseek(fd, 0, 0); | |
564 | n = read(fd, buf, sizeof(buf)); | |
5418499a | 565 | if (n <= 0 || n == sizeof(buf)) |
2eba8496 N |
566 | return -2; |
567 | buf[n] = 0; | |
568 | *v1 = strtoull(buf, &ep, 0); | |
569 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
570 | return -1; | |
571 | while (*ep == ' ' || *ep == '/' || *ep == '(') | |
572 | ep++; | |
573 | *v2 = strtoull(ep, &ep2, 0); | |
574 | if (ep2 == ep || (*ep2 != 0 && *ep2 != '\n' && *ep2 != ' ' && *ep2 != ')')) { | |
575 | *v2 = *v1; | |
576 | return 1; | |
577 | } | |
578 | return 2; | |
579 | } | |
580 | ||
581 | int sysfs_get_two(struct mdinfo *sra, struct mdinfo *dev, | |
582 | char *name, unsigned long long *v1, unsigned long long *v2) | |
583 | { | |
584 | int n; | |
585 | int fd; | |
586 | ||
587 | fd = sysfs_get_fd(sra, dev, name); | |
588 | if (fd < 0) | |
589 | return -1; | |
590 | n = sysfs_fd_get_two(fd, v1, v2); | |
591 | close(fd); | |
592 | return n; | |
593 | } | |
594 | ||
7236ee7a N |
595 | int sysfs_fd_get_str(int fd, char *val, int size) |
596 | { | |
597 | int n; | |
598 | ||
599 | lseek(fd, 0, 0); | |
600 | n = read(fd, val, size); | |
5418499a | 601 | if (n <= 0 || n == size) |
7236ee7a N |
602 | return -1; |
603 | val[n] = 0; | |
604 | return n; | |
605 | } | |
606 | ||
93ecfa01 N |
607 | int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev, |
608 | char *name, char *val, int size) | |
609 | { | |
93ecfa01 N |
610 | int n; |
611 | int fd; | |
7236ee7a N |
612 | |
613 | fd = sysfs_get_fd(sra, dev, name); | |
93ecfa01 N |
614 | if (fd < 0) |
615 | return -1; | |
7236ee7a | 616 | n = sysfs_fd_get_str(fd, val, size); |
93ecfa01 | 617 | close(fd); |
93ecfa01 N |
618 | return n; |
619 | } | |
620 | ||
8ed3e5e1 DW |
621 | int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms) |
622 | { | |
623 | unsigned long sec; | |
624 | unsigned long msec; | |
625 | char delay[30]; | |
626 | ||
627 | sec = ms / 1000; | |
0dd3ba30 | 628 | msec = ms % 1000; |
8ed3e5e1 | 629 | |
0dd3ba30 DW |
630 | sprintf(delay, "%ld.%03ld\n", sec, msec); |
631 | /* this '\n' ^ needed for kernels older than 2.6.28 */ | |
8ed3e5e1 DW |
632 | return sysfs_set_str(sra, NULL, "safe_mode_delay", delay); |
633 | } | |
634 | ||
f35f2525 | 635 | int sysfs_set_array(struct mdinfo *info, int vers) |
2503d23b NB |
636 | { |
637 | int rv = 0; | |
f35f2525 | 638 | char ver[100]; |
a5062b1c | 639 | int raid_disks = info->array.raid_disks; |
f35f2525 N |
640 | |
641 | ver[0] = 0; | |
642 | if (info->array.major_version == -1 && | |
643 | info->array.minor_version == -2) { | |
ddb12f6c AK |
644 | char buf[1024]; |
645 | ||
f35f2525 N |
646 | strcat(strcpy(ver, "external:"), info->text_version); |
647 | ||
ddb12f6c AK |
648 | /* meta version might already be set if we are setting |
649 | * new geometry for a reshape. In that case we don't | |
650 | * want to over-write the 'readonly' flag that is | |
651 | * stored in the metadata version. So read the current | |
652 | * version first, and preserve the flag | |
653 | */ | |
654 | if (sysfs_get_str(info, NULL, "metadata_version", | |
655 | buf, 1024) > 0) | |
656 | if (strlen(buf) >= 9 && buf[9] == '-') | |
657 | ver[9] = '-'; | |
658 | ||
f35f2525 N |
659 | if ((vers % 100) < 2 || |
660 | sysfs_set_str(info, NULL, "metadata_version", | |
661 | ver) < 0) { | |
7a862a02 | 662 | pr_err("This kernel does not support external metadata.\n"); |
f35f2525 N |
663 | return 1; |
664 | } | |
665 | } | |
2503d23b NB |
666 | if (info->array.level < 0) |
667 | return 0; /* FIXME */ | |
f35f2525 | 668 | rv |= sysfs_set_str(info, NULL, "level", |
2503d23b | 669 | map_num(pers, info->array.level)); |
a5062b1c AK |
670 | if (info->reshape_active && info->delta_disks != UnSet) |
671 | raid_disks -= info->delta_disks; | |
672 | rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks); | |
f35f2525 N |
673 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size); |
674 | rv |= sysfs_set_num(info, NULL, "layout", info->array.layout); | |
675 | rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2); | |
da9b4a62 DW |
676 | if (info->custom_array_size) { |
677 | int rc; | |
678 | ||
679 | rc = sysfs_set_num(info, NULL, "array_size", | |
680 | info->custom_array_size/2); | |
681 | if (rc && errno == ENOENT) { | |
7a862a02 | 682 | pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n"); |
da9b4a62 DW |
683 | rc = 0; |
684 | } | |
685 | rv |= rc; | |
686 | } | |
687 | ||
f35f2525 N |
688 | if (info->array.level > 0) |
689 | rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start); | |
f897078e N |
690 | |
691 | if (info->reshape_active) { | |
692 | rv |= sysfs_set_num(info, NULL, "reshape_position", | |
693 | info->reshape_progress); | |
694 | rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk); | |
20a40eca N |
695 | rv |= sysfs_set_num(info, NULL, "layout", info->new_layout); |
696 | rv |= sysfs_set_num(info, NULL, "raid_disks", | |
a5062b1c | 697 | info->array.raid_disks); |
20a40eca N |
698 | /* We don't set 'new_level' here. That can only happen |
699 | * once the reshape completes. | |
f897078e N |
700 | */ |
701 | } | |
2432ce9b AP |
702 | |
703 | if (info->consistency_policy == CONSISTENCY_POLICY_PPL) { | |
704 | if (sysfs_set_str(info, NULL, "consistency_policy", | |
705 | map_num(consistency_policies, | |
706 | info->consistency_policy))) { | |
707 | pr_err("This kernel does not support PPL\n"); | |
708 | return 1; | |
709 | } | |
710 | } | |
711 | ||
2503d23b NB |
712 | return rv; |
713 | } | |
714 | ||
2904b26f | 715 | int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume) |
2503d23b | 716 | { |
33a6535d AW |
717 | char dv[PATH_MAX]; |
718 | char nm[PATH_MAX]; | |
2503d23b NB |
719 | char *dname; |
720 | int rv; | |
bb758cca | 721 | int i; |
2503d23b NB |
722 | |
723 | sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor); | |
724 | rv = sysfs_set_str(sra, NULL, "new_dev", dv); | |
725 | if (rv) | |
726 | return rv; | |
727 | ||
728 | memset(nm, 0, sizeof(nm)); | |
bc17158d | 729 | dname = devid2kname(makedev(sd->disk.major, sd->disk.minor)); |
2503d23b NB |
730 | strcpy(sd->sys_name, "dev-"); |
731 | strcpy(sd->sys_name+4, dname); | |
732 | ||
2904b26f DW |
733 | /* test write to see if 'recovery_start' is available */ |
734 | if (resume && sd->recovery_start < MaxSector && | |
735 | sysfs_set_num(sra, sd, "recovery_start", 0)) { | |
736 | sysfs_set_str(sra, sd, "state", "remove"); | |
737 | return -1; | |
738 | } | |
739 | ||
73649188 | 740 | rv = sysfs_set_num(sra, sd, "offset", sd->data_offset); |
2503d23b NB |
741 | rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2); |
742 | if (sra->array.level != LEVEL_CONTAINER) { | |
2432ce9b AP |
743 | if (sd->consistency_policy == CONSISTENCY_POLICY_PPL) { |
744 | rv |= sysfs_set_num(sra, sd, "ppl_sector", sd->ppl_sector); | |
745 | rv |= sysfs_set_num(sra, sd, "ppl_size", sd->ppl_size); | |
746 | } | |
d23534e4 | 747 | if (sd->recovery_start == MaxSector) |
462906cd N |
748 | /* This can correctly fail if array isn't started, |
749 | * yet, so just ignore status for now. | |
750 | */ | |
d23534e4 | 751 | sysfs_set_str(sra, sd, "state", "insync"); |
899aead0 AK |
752 | if (sd->disk.raid_disk >= 0) |
753 | rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk); | |
2904b26f DW |
754 | if (resume) |
755 | sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start); | |
2503d23b | 756 | } |
bb758cca TM |
757 | if (sd->bb.supported) { |
758 | if (sysfs_set_str(sra, sd, "state", "external_bbl")) { | |
759 | /* | |
760 | * backward compatibility - if kernel doesn't support | |
761 | * bad blocks for external metadata, let it continue | |
762 | * as long as there are none known so far | |
763 | */ | |
764 | if (sd->bb.count) { | |
765 | pr_err("The kernel has no support for bad blocks in external metadata\n"); | |
766 | return -1; | |
767 | } | |
768 | } | |
769 | ||
770 | for (i = 0; i < sd->bb.count; i++) { | |
771 | char s[30]; | |
772 | const struct md_bb_entry *entry = &sd->bb.entries[i]; | |
773 | ||
774 | snprintf(s, sizeof(s) - 1, "%llu %d\n", entry->sector, | |
775 | entry->length); | |
776 | rv |= sysfs_set_str(sra, sd, "bad_blocks", s); | |
777 | } | |
778 | } | |
2503d23b NB |
779 | return rv; |
780 | } | |
90c8b707 | 781 | |
755c99fa | 782 | #if 0 |
90c8b707 DW |
783 | int sysfs_disk_to_sg(int fd) |
784 | { | |
785 | /* from an open block device, try find and open its corresponding | |
786 | * scsi_generic interface | |
787 | */ | |
788 | struct stat st; | |
789 | char path[256]; | |
790 | char sg_path[256]; | |
5418499a | 791 | char sg_major_minor[10]; |
90c8b707 DW |
792 | char *c; |
793 | DIR *dir; | |
794 | struct dirent *de; | |
795 | int major, minor, rv; | |
796 | ||
797 | if (fstat(fd, &st)) | |
798 | return -1; | |
799 | ||
800 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", | |
801 | major(st.st_rdev), minor(st.st_rdev)); | |
802 | ||
803 | dir = opendir(path); | |
804 | if (!dir) | |
805 | return -1; | |
806 | ||
807 | de = readdir(dir); | |
808 | while (de) { | |
809 | if (strncmp("scsi_generic:", de->d_name, | |
810 | strlen("scsi_generic:")) == 0) | |
811 | break; | |
812 | de = readdir(dir); | |
813 | } | |
814 | closedir(dir); | |
815 | ||
816 | if (!de) | |
817 | return -1; | |
818 | ||
819 | snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); | |
820 | fd = open(sg_path, O_RDONLY); | |
821 | if (fd < 0) | |
822 | return fd; | |
823 | ||
824 | rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); | |
825 | close(fd); | |
5418499a | 826 | if (rv < 0 || rv == sizeof(sg_major_minor)) |
90c8b707 DW |
827 | return -1; |
828 | else | |
829 | sg_major_minor[rv - 1] = '\0'; | |
830 | ||
831 | c = strchr(sg_major_minor, ':'); | |
832 | *c = '\0'; | |
833 | c++; | |
834 | major = strtol(sg_major_minor, NULL, 10); | |
835 | minor = strtol(c, NULL, 10); | |
836 | snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", | |
837 | (int) getpid(), major, minor); | |
838 | if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { | |
839 | fd = open(path, O_RDONLY); | |
840 | unlink(path); | |
841 | return fd; | |
842 | } | |
843 | ||
844 | return -1; | |
845 | } | |
755c99fa | 846 | #endif |
90c8b707 | 847 | |
f1665f72 DW |
848 | int sysfs_disk_to_scsi_id(int fd, __u32 *id) |
849 | { | |
850 | /* from an open block device, try to retrieve it scsi_id */ | |
851 | struct stat st; | |
852 | char path[256]; | |
f1665f72 DW |
853 | DIR *dir; |
854 | struct dirent *de; | |
d8924477 | 855 | int host, bus, target, lun; |
f1665f72 DW |
856 | |
857 | if (fstat(fd, &st)) | |
858 | return 1; | |
859 | ||
fa89bdee | 860 | snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device", |
f1665f72 DW |
861 | major(st.st_rdev), minor(st.st_rdev)); |
862 | ||
863 | dir = opendir(path); | |
864 | if (!dir) | |
865 | return 1; | |
866 | ||
d8924477 DW |
867 | for (de = readdir(dir); de; de = readdir(dir)) { |
868 | int count; | |
869 | ||
870 | if (de->d_type != DT_DIR) | |
871 | continue; | |
872 | ||
873 | count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun); | |
874 | if (count == 4) | |
f1665f72 | 875 | break; |
f1665f72 DW |
876 | } |
877 | closedir(dir); | |
878 | ||
879 | if (!de) | |
880 | return 1; | |
881 | ||
d8924477 | 882 | *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0); |
f1665f72 DW |
883 | return 0; |
884 | } | |
f94d52f4 | 885 | |
4dd2df09 | 886 | int sysfs_unique_holder(char *devnm, long rdev) |
f94d52f4 | 887 | { |
4dd2df09 | 888 | /* Check that devnm is a holder of rdev, |
f94d52f4 NB |
889 | * and is the only holder. |
890 | * we should be locked against races by | |
4dd2df09 | 891 | * an O_EXCL on devnm |
aab15415 N |
892 | * Return values: |
893 | * 0 - not unique, not even a holder | |
894 | * 1 - unique, this is the only holder. | |
895 | * 2/3 - not unique, there is another holder | |
896 | * -1 - error, cannot find the holders | |
f94d52f4 NB |
897 | */ |
898 | DIR *dir; | |
899 | struct dirent *de; | |
900 | char dirname[100]; | |
901 | char l; | |
aab15415 | 902 | int ret = 0; |
f94d52f4 NB |
903 | sprintf(dirname, "/sys/dev/block/%d:%d/holders", |
904 | major(rdev), minor(rdev)); | |
905 | dir = opendir(dirname); | |
f94d52f4 | 906 | if (!dir) |
aab15415 | 907 | return -1; |
f94d52f4 NB |
908 | l = strlen(dirname); |
909 | while ((de = readdir(dir)) != NULL) { | |
4dd2df09 N |
910 | char buf[100]; |
911 | char *sl; | |
f94d52f4 | 912 | int n; |
f94d52f4 NB |
913 | |
914 | if (de->d_ino == 0) | |
915 | continue; | |
916 | if (de->d_name[0] == '.') | |
917 | continue; | |
918 | strcpy(dirname+l, "/"); | |
919 | strcat(dirname+l, de->d_name); | |
4dd2df09 N |
920 | n = readlink(dirname, buf, sizeof(buf)-1); |
921 | if (n <= 0) | |
93f1df33 | 922 | continue; |
f94d52f4 | 923 | buf[n] = 0; |
4dd2df09 N |
924 | sl = strrchr(buf, '/'); |
925 | if (!sl) | |
aab15415 | 926 | continue; |
4dd2df09 | 927 | sl++; |
f94d52f4 | 928 | |
4dd2df09 | 929 | if (strcmp(devnm, sl) == 0) |
aab15415 N |
930 | ret |= 1; |
931 | else | |
932 | ret |= 2; | |
f94d52f4 NB |
933 | } |
934 | closedir(dir); | |
aab15415 | 935 | return ret; |
f94d52f4 | 936 | } |
38a07ed6 | 937 | |
bc77ed53 DW |
938 | int sysfs_freeze_array(struct mdinfo *sra) |
939 | { | |
940 | /* Try to freeze resync/rebuild on this array/container. | |
941 | * Return -1 if the array is busy, | |
bc77ed53 DW |
942 | * return 0 if this kernel doesn't support 'frozen' |
943 | * return 1 if it worked. | |
944 | */ | |
945 | char buf[20]; | |
946 | ||
947 | if (!sysfs_attribute_available(sra, NULL, "sync_action")) | |
948 | return 1; /* no sync_action == frozen */ | |
949 | if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0) | |
950 | return 0; | |
fd324b08 N |
951 | if (strcmp(buf, "frozen\n") == 0) |
952 | /* Already frozen */ | |
953 | return 0; | |
dea3786a | 954 | if (strcmp(buf, "idle\n") != 0 && strcmp(buf, "recover\n") != 0) |
bc77ed53 DW |
955 | return -1; |
956 | if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0) | |
957 | return 0; | |
958 | return 1; | |
959 | } | |
efc67e8e N |
960 | |
961 | int sysfs_wait(int fd, int *msec) | |
962 | { | |
963 | /* Wait up to '*msec' for fd to have an exception condition. | |
964 | * if msec == NULL, wait indefinitely. | |
965 | */ | |
966 | fd_set fds; | |
967 | int n; | |
968 | FD_ZERO(&fds); | |
969 | FD_SET(fd, &fds); | |
970 | if (msec == NULL) | |
971 | n = select(fd+1, NULL, NULL, &fds, NULL); | |
972 | else if (*msec < 0) | |
973 | n = 0; | |
974 | else { | |
975 | struct timeval start, end, tv; | |
976 | gettimeofday(&start, NULL); | |
4bffc964 N |
977 | if (*msec < 1000) { |
978 | tv.tv_sec = 0; | |
efc67e8e | 979 | tv.tv_usec = (*msec)*1000; |
4bffc964 | 980 | } else { |
efc67e8e | 981 | tv.tv_sec = (*msec)/1000; |
4bffc964 N |
982 | tv.tv_usec = 0; |
983 | } | |
efc67e8e N |
984 | n = select(fd+1, NULL, NULL, &fds, &tv); |
985 | gettimeofday(&end, NULL); | |
986 | end.tv_sec -= start.tv_sec; | |
987 | *msec -= (end.tv_sec * 1000 + end.tv_usec/1000 | |
4bffc964 | 988 | - start.tv_usec/1000) + 1; |
efc67e8e N |
989 | } |
990 | return n; | |
991 | } |