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