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