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