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