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