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