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