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