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