daec4f1a8f42338fc758dd14b00f42ffcfdf806c
[thirdparty/mdadm.git] / Detail.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
5  *
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *    Author: Neil Brown
22  *    Email: <neilb@suse.de>
23  */
24
25 #include        "mdadm.h"
26 #include        "md_p.h"
27 #include        "md_u.h"
28 #include        <ctype.h>
29 #include        <dirent.h>
30
31 static int cmpstringp(const void *p1, const void *p2)
32 {
33         return strcmp(* (char * const *) p1, * (char * const *) p2);
34 }
35
36 static int add_device(const char *dev, char ***p_devices,
37                       int *p_max_devices, int n_devices)
38 {
39         if (n_devices + 1 >= *p_max_devices) {
40                 *p_max_devices += 16;
41                 *p_devices = xrealloc(*p_devices, *p_max_devices *
42                                       sizeof(**p_devices));
43                 if (!*p_devices) {
44                         *p_max_devices = 0;
45                         return 0;
46                 }
47         };
48         (*p_devices)[n_devices] = xstrdup(dev);
49         return n_devices + 1;
50 }
51
52 int Detail(char *dev, struct context *c)
53 {
54         /*
55          * Print out details for an md array
56          */
57         int fd = open(dev, O_RDONLY);
58         mdu_array_info_t array;
59         mdu_disk_info_t *disks = NULL;
60         int next;
61         int d;
62         time_t atime;
63         char *str;
64         char **devices = NULL;
65         int max_devices = 0, n_devices = 0;
66         int spares = 0;
67         struct stat stb;
68         int failed = 0;
69         struct supertype *st;
70         char *subarray = NULL;
71         int max_disks = MD_SB_DISKS; /* just a default */
72         struct mdinfo *info = NULL;
73         struct mdinfo *sra;
74         struct mdinfo *subdev;
75         char *member = NULL;
76         char *container = NULL;
77
78         int rv = c->test ? 4 : 1;
79         int avail_disks = 0;
80         char *avail = NULL;
81         int external;
82         int inactive;
83         int is_container = 0;
84         char *arrayst;
85
86         if (fd < 0) {
87                 pr_err("cannot open %s: %s\n",
88                         dev, strerror(errno));
89                 return rv;
90         }
91         sra = sysfs_read(fd, NULL, GET_VERSION | GET_DEVS |
92                         GET_ARRAY_STATE | GET_STATE);
93         if (!sra) {
94                 if (md_get_array_info(fd, &array)) {
95                         pr_err("%s does not appear to be an md device\n", dev);
96                         close(fd);
97                         return rv;
98                 }
99         }
100         external = (sra != NULL && sra->array.major_version == -1 &&
101                     sra->array.minor_version == -2);
102         inactive = (sra != NULL && !md_array_is_active(sra));
103         st = super_by_fd(fd, &subarray);
104         if (md_get_array_info(fd, &array)) {
105                 if (errno == ENODEV) {
106                         if (sra->array.major_version == -1 &&
107                             sra->array.minor_version == -1 &&
108                             sra->devs == NULL) {
109                                 pr_err("Array associated with md device %s does not exist.\n",
110                                        dev);
111                                 close(fd);
112                                 sysfs_free(sra);
113                                 return rv;
114                         }
115                         array = sra->array;
116                 } else {
117                         pr_err("cannot get array detail for %s: %s\n",
118                                dev, strerror(errno));
119                         close(fd);
120                         return rv;
121                 }
122         }
123
124         if (array.raid_disks == 0 && external)
125                 is_container = 1;
126         if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
127                 stb.st_rdev = 0;
128         rv = 0;
129
130         if (st)
131                 max_disks = st->max_devs;
132
133         if (subarray) {
134                 /* This is a subarray of some container.
135                  * We want the name of the container, and the member
136                  */
137                 dev_t devid = devnm2devid(st->container_devnm);
138                 int cfd, err;
139
140                 member = subarray;
141                 container = map_dev_preferred(major(devid), minor(devid),
142                                               1, c->prefer);
143                 cfd = open_dev(st->container_devnm);
144                 if (cfd >= 0) {
145                         err = st->ss->load_container(st, cfd, NULL);
146                         close(cfd);
147                         if (err == 0)
148                                 info = st->ss->container_content(st, subarray);
149                 }
150         }
151
152         /* try to load a superblock. Try sra->devs first, then try ioctl */
153         if (st && !info)
154                 for (d = 0, subdev = sra ? sra->devs : NULL;
155                      d < max_disks || subdev;
156                      subdev ? (void)(subdev = subdev->next) : (void)(d++)){
157                 mdu_disk_info_t disk;
158                 char *dv;
159                 int fd2;
160                 int err;
161
162                 if (subdev)
163                         disk = subdev->disk;
164                 else {
165                         disk.number = d;
166                         if (md_get_disk_info(fd, &disk) < 0)
167                                 continue;
168                         if (d >= array.raid_disks &&
169                             disk.major == 0 && disk.minor == 0)
170                                 continue;
171                 }
172
173                 if (array.raid_disks > 0 &&
174                     (disk.state & (1 << MD_DISK_ACTIVE)) == 0)
175                         continue;
176
177                 dv = map_dev(disk.major, disk.minor, 1);
178                 if (!dv)
179                         continue;
180
181                 fd2 = dev_open(dv, O_RDONLY);
182                 if (fd2 < 0)
183                         continue;
184
185                 if (st->sb)
186                         st->ss->free_super(st);
187
188                 err = st->ss->load_super(st, fd2, NULL);
189                 close(fd2);
190                 if (err)
191                         continue;
192                 if (info)
193                         free(info);
194                 if (subarray)
195                         info = st->ss->container_content(st, subarray);
196                 else {
197                         info = xmalloc(sizeof(*info));
198                         st->ss->getinfo_super(st, info, NULL);
199                 }
200                 if (!info)
201                         continue;
202
203                 if (array.raid_disks != 0 && /* container */
204                     (info->array.ctime != array.ctime ||
205                      info->array.level != array.level)) {
206                         st->ss->free_super(st);
207                         continue;
208                 }
209                 /* some formats (imsm) have free-floating-spares
210                  * with a uuid of uuid_zero, they don't
211                  * have very good info about the rest of the
212                  * container, so keep searching when
213                  * encountering such a device.  Otherwise, stop
214                  * after the first successful call to
215                  * ->load_super.
216                  */
217                 if (memcmp(uuid_zero,
218                            info->uuid,
219                            sizeof(uuid_zero)) == 0) {
220                         st->ss->free_super(st);
221                         continue;
222                 }
223                 break;
224         }
225
226         /* Ok, we have some info to print... */
227         str = map_num(pers, array.level);
228
229         if (c->export) {
230                 if (array.raid_disks) {
231                         if (str)
232                                 printf("MD_LEVEL=%s\n", str);
233                         printf("MD_DEVICES=%d\n", array.raid_disks);
234                 } else {
235                         if (is_container)
236                                 printf("MD_LEVEL=container\n");
237                         printf("MD_DEVICES=%d\n", array.nr_disks);
238                 }
239                 if (container) {
240                         printf("MD_CONTAINER=%s\n", container);
241                         printf("MD_MEMBER=%s\n", member);
242                 } else {
243                         if (sra && sra->array.major_version < 0)
244                                 printf("MD_METADATA=%s\n", sra->text_version);
245                         else
246                                 printf("MD_METADATA=%d.%d\n",
247                                        array.major_version,
248                                        array.minor_version);
249                 }
250
251                 if (st && st->sb && info) {
252                         char nbuf[64];
253                         struct map_ent *mp, *map = NULL;
254
255                         fname_from_uuid(st, info, nbuf, ':');
256                         printf("MD_UUID=%s\n", nbuf + 5);
257                         mp = map_by_uuid(&map, info->uuid);
258                         if (mp && mp->path &&
259                             strncmp(mp->path, "/dev/md/", 8) == 0) {
260                                 printf("MD_DEVNAME=");
261                                 print_escape(mp->path + 8);
262                                 putchar('\n');
263                         }
264
265                         if (st->ss->export_detail_super)
266                                 st->ss->export_detail_super(st);
267                         map_free(map);
268                 } else {
269                         struct map_ent *mp, *map = NULL;
270                         char nbuf[64];
271                         mp = map_by_devnm(&map, fd2devnm(fd));
272                         if (mp) {
273                                 __fname_from_uuid(mp->uuid, 0, nbuf, ':');
274                                 printf("MD_UUID=%s\n", nbuf+5);
275                         }
276                         if (mp && mp->path &&
277                             strncmp(mp->path, "/dev/md/", 8) == 0) {
278                                 printf("MD_DEVNAME=");
279                                 print_escape(mp->path+8);
280                                 putchar('\n');
281                         }
282                         map_free(map);
283                 }
284                 if (!c->no_devices && sra) {
285                         struct mdinfo *mdi;
286                         for (mdi  = sra->devs; mdi; mdi = mdi->next) {
287                                 char *path;
288                                 char *sysdev = xstrdup(mdi->sys_name);
289                                 char *cp;
290
291                                 path = map_dev(mdi->disk.major,
292                                                mdi->disk.minor, 0);
293                                 for (cp = sysdev; *cp; cp++)
294                                         if (!isalnum(*cp))
295                                                 *cp = '_';
296
297                                 if (mdi->disk.raid_disk >= 0)
298                                         printf("MD_DEVICE_%s_ROLE=%d\n",
299                                                sysdev,
300                                                mdi->disk.raid_disk);
301                                 else
302                                         printf("MD_DEVICE_%s_ROLE=spare\n",
303                                                sysdev);
304                                 if (path)
305                                         printf("MD_DEVICE_%s_DEV=%s\n",
306                                                sysdev, path);
307                         }
308                 }
309                 goto out;
310         }
311
312         disks = xmalloc(max_disks * 2 * sizeof(mdu_disk_info_t));
313         for (d = 0; d < max_disks * 2; d++) {
314                 disks[d].state = (1 << MD_DISK_REMOVED);
315                 disks[d].major = disks[d].minor = 0;
316                 disks[d].number = -1;
317                 disks[d].raid_disk = d / 2;
318         }
319
320         next = array.raid_disks * 2;
321         if (inactive) {
322                 struct mdinfo *mdi;
323                 for (mdi = sra->devs; mdi; mdi = mdi->next) {
324                         disks[next++] = mdi->disk;
325                         disks[next - 1].number = -1;
326                 }
327         } else for (d = 0; d < max_disks; d++) {
328                 mdu_disk_info_t disk;
329                 disk.number = d;
330                 if (md_get_disk_info(fd, &disk) < 0) {
331                         if (d < array.raid_disks)
332                                 pr_err("cannot get device detail for device %d: %s\n",
333                                         d, strerror(errno));
334                         continue;
335                 }
336                 if (disk.major == 0 && disk.minor == 0)
337                         continue;
338                 if (disk.raid_disk >= 0 && disk.raid_disk < array.raid_disks &&
339                     disks[disk.raid_disk * 2].state == (1 << MD_DISK_REMOVED) &&
340                     ((disk.state & (1 << MD_DISK_JOURNAL)) == 0))
341                         disks[disk.raid_disk * 2] = disk;
342                 else if (disk.raid_disk >= 0 &&
343                          disk.raid_disk < array.raid_disks &&
344                          disks[disk.raid_disk * 2 + 1].state ==
345                          (1 << MD_DISK_REMOVED) &&
346                          !(disk.state & (1 << MD_DISK_JOURNAL)))
347                         disks[disk.raid_disk * 2 + 1] = disk;
348                 else if (next < max_disks * 2)
349                         disks[next++] = disk;
350         }
351
352         avail = xcalloc(array.raid_disks, 1);
353
354         for (d = 0; d < array.raid_disks; d++) {
355
356                 if ((disks[d*2].state & (1<<MD_DISK_SYNC)) ||
357                     (disks[d*2+1].state & (1<<MD_DISK_SYNC))) {
358                         avail_disks ++;
359                         avail[d] = 1;
360                 } else
361                         rv |= !! c->test;
362         }
363
364         if (c->brief) {
365                 mdu_bitmap_file_t bmf;
366                 if (inactive && !is_container)
367                         printf("INACTIVE-ARRAY %s", dev);
368                 else
369                         printf("ARRAY %s", dev);
370                 if (c->verbose > 0) {
371                         if (array.raid_disks)
372                                 printf(" level=%s num-devices=%d",
373                                        str ? str : "-unknown-",
374                                        array.raid_disks);
375                         else if (is_container)
376                                 printf(" level=container num-devices=%d",
377                                        array.nr_disks);
378                         else
379                                 printf(" num-devices=%d", array.nr_disks);
380                 }
381                 if (container) {
382                         printf(" container=%s", container);
383                         printf(" member=%s", member);
384                 } else {
385                         if (sra && sra->array.major_version < 0)
386                                 printf(" metadata=%s", sra->text_version);
387                         else
388                                 printf(" metadata=%d.%d", array.major_version,
389                                        array.minor_version);
390                 }
391
392                 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
393                 if (ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 && bmf.pathname[0]) {
394                         printf(" bitmap=%s", bmf.pathname);
395                 }
396         } else {
397                 mdu_bitmap_file_t bmf;
398                 unsigned long long larray_size;
399                 struct mdstat_ent *ms = mdstat_read(0, 0);
400                 struct mdstat_ent *e;
401                 char *devnm;
402
403                 devnm = stat2devnm(&stb);
404                 for (e = ms; e; e = e->next)
405                         if (strcmp(e->devnm, devnm) == 0)
406                                 break;
407                 if (!get_dev_size(fd, NULL, &larray_size))
408                         larray_size = 0;
409
410                 printf("%s:\n", dev);
411
412                 if (container)
413                         printf("         Container : %s, member %s\n",
414                                container, member);
415                 else {
416                         if (sra && sra->array.major_version < 0)
417                                 printf("           Version : %s\n",
418                                        sra->text_version);
419                         else
420                                 printf("           Version : %d.%d\n",
421                                        array.major_version,
422                                        array.minor_version);
423                 }
424
425                 atime = array.ctime;
426                 if (atime)
427                         printf("     Creation Time : %.24s\n", ctime(&atime));
428                 if (is_container)
429                         str = "container";
430                 if (str)
431                         printf("        Raid Level : %s\n", str);
432                 if (larray_size)
433                         printf("        Array Size : %llu%s\n",
434                                (larray_size >> 10),
435                                human_size(larray_size));
436                 if (array.level >= 1) {
437                         if (sra)
438                                 array.major_version = sra->array.major_version;
439                         if (array.major_version != 0 &&
440                             (larray_size >= 0xFFFFFFFFULL|| array.size == 0)) {
441                                 unsigned long long dsize;
442
443                                 dsize = get_component_size(fd);
444                                 if (dsize > 0)
445                                         printf("     Used Dev Size : %llu%s\n",
446                                                dsize/2,
447                                          human_size((long long)dsize<<9));
448                                 else
449                                         printf("     Used Dev Size : unknown\n");
450                         } else
451                                 printf("     Used Dev Size : %lu%s\n",
452                                        (unsigned long)array.size,
453                                        human_size((unsigned long long)
454                                                   array.size << 10));
455                 }
456                 if (array.raid_disks)
457                         printf("      Raid Devices : %d\n", array.raid_disks);
458                 printf("     Total Devices : %d\n", array.nr_disks);
459                 if (!container &&
460                     ((sra == NULL && array.major_version == 0) ||
461                      (sra && sra->array.major_version == 0)))
462                         printf("   Preferred Minor : %d\n", array.md_minor);
463                 if (sra == NULL || sra->array.major_version >= 0)
464                         printf("       Persistence : Superblock is %spersistent\n",
465                                array.not_persistent ? "not " : "");
466                 printf("\n");
467                 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
468                 if (ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 && bmf.pathname[0]) {
469                         printf("     Intent Bitmap : %s\n", bmf.pathname);
470                         printf("\n");
471                 } else if (array.state & (1<<MD_SB_CLUSTERED))
472                         printf("     Intent Bitmap : Internal(Clustered)\n\n");
473                 else if (array.state & (1<<MD_SB_BITMAP_PRESENT))
474                         printf("     Intent Bitmap : Internal\n\n");
475                 atime = array.utime;
476                 if (atime)
477                         printf("       Update Time : %.24s\n", ctime(&atime));
478                 if (array.raid_disks) {
479                         static char *sync_action[] = {
480                                 ", recovering",  ", resyncing",
481                                 ", reshaping",   ", checking"  };
482                         char *st;
483                         if (avail_disks == array.raid_disks)
484                                 st = "";
485                         else if (!enough(array.level, array.raid_disks,
486                                          array.layout, 1, avail))
487                                 st = ", FAILED";
488                         else
489                                 st = ", degraded";
490
491                         if (array.state & (1 << MD_SB_CLEAN)) {
492                                 if ((array.level == 0) ||
493                                     (array.level == LEVEL_LINEAR))
494                                         arrayst = map_num(sysfs_array_states,
495                                                           sra->array_state);
496                                 else
497                                         arrayst = "clean";
498                         } else
499                                 arrayst = "active";
500
501                         printf("             State : %s%s%s%s%s%s \n",
502                                arrayst, st,
503                                (!e || (e->percent < 0 &&
504                                        e->percent != RESYNC_PENDING &&
505                                        e->percent != RESYNC_DELAYED)) ?
506                                "" : sync_action[e->resync],
507                                larray_size ? "": ", Not Started",
508                                (e && e->percent == RESYNC_DELAYED) ?
509                                " (DELAYED)": "",
510                                (e && e->percent == RESYNC_PENDING) ?
511                                " (PENDING)": "");
512                 } else if (inactive && !is_container) {
513                         printf("             State : inactive\n");
514                 }
515                 if (array.raid_disks)
516                         printf("    Active Devices : %d\n", array.active_disks);
517                 if (array.working_disks > 0)
518                         printf("   Working Devices : %d\n",
519                                array.working_disks);
520                 if (array.raid_disks) {
521                         printf("    Failed Devices : %d\n", array.failed_disks);
522                         printf("     Spare Devices : %d\n", array.spare_disks);
523                 }
524                 printf("\n");
525                 if (array.level == 5) {
526                         str = map_num(r5layout, array.layout);
527                         printf("            Layout : %s\n",
528                                str ? str : "-unknown-");
529                 }
530                 if (array.level == 0 && array.layout) {
531                         str = map_num(r0layout, array.layout);
532                         printf("            Layout : %s\n",
533                                str ? str : "-unknown-");
534                 }
535                 if (array.level == 6) {
536                         str = map_num(r6layout, array.layout);
537                         printf("            Layout : %s\n",
538                                str ? str : "-unknown-");
539                 }
540                 if (array.level == 10) {
541                         printf("            Layout :");
542                         print_r10_layout(array.layout);
543                         printf("\n");
544                 }
545                 switch (array.level) {
546                 case 0:
547                 case 4:
548                 case 5:
549                 case 10:
550                 case 6:
551                         if (array.chunk_size)
552                                 printf("        Chunk Size : %dK\n\n",
553                                        array.chunk_size/1024);
554                         break;
555                 case -1:
556                         printf("          Rounding : %dK\n\n",
557                                array.chunk_size/1024);
558                         break;
559                 default:
560                         break;
561                 }
562
563                 if (array.raid_disks) {
564                         struct mdinfo *mdi;
565
566                         mdi = sysfs_read(fd, NULL, GET_CONSISTENCY_POLICY);
567                         if (mdi) {
568                                 char *policy = map_num(consistency_policies,
569                                                        mdi->consistency_policy);
570                                 sysfs_free(mdi);
571                                 if (policy)
572                                         printf("Consistency Policy : %s\n\n",
573                                                policy);
574                         }
575                 }
576
577                 if (e && e->percent >= 0) {
578                         static char *sync_action[] = {
579                                 "Rebuild", "Resync", "Reshape", "Check"};
580                         printf("    %7s Status : %d%% complete\n",
581                                sync_action[e->resync], e->percent);
582                 }
583
584                 if ((st && st->sb) && (info && info->reshape_active)) {
585 #if 0
586 This is pretty boring
587                         printf("     Reshape pos'n : %llu%s\n",
588                                (unsigned long long) info->reshape_progress << 9,
589                                human_size((unsigned long long)
590                                           info->reshape_progress << 9));
591 #endif
592                         if (info->delta_disks != 0)
593                                 printf("     Delta Devices : %d, (%d->%d)\n",
594                                        info->delta_disks,
595                                        array.raid_disks - info->delta_disks,
596                                        array.raid_disks);
597                         if (info->new_level != array.level) {
598                                 str = map_num(pers, info->new_level);
599                                 printf("         New Level : %s\n",
600                                        str ? str : "-unknown-");
601                         }
602                         if (info->new_level != array.level ||
603                             info->new_layout != array.layout) {
604                                 if (info->new_level == 5) {
605                                         str = map_num(r5layout,
606                                                       info->new_layout);
607                                         printf("        New Layout : %s\n",
608                                                str ? str : "-unknown-");
609                                 }
610                                 if (info->new_level == 6) {
611                                         str = map_num(r6layout,
612                                                       info->new_layout);
613                                         printf("        New Layout : %s\n",
614                                                str ? str : "-unknown-");
615                                 }
616                                 if (info->new_level == 10) {
617                                         printf("        New Layout : near=%d, %s=%d\n",
618                                                info->new_layout & 255,
619                                                (info->new_layout & 0x10000) ?
620                                                "offset" : "far",
621                                                (info->new_layout >> 8) & 255);
622                                 }
623                         }
624                         if (info->new_chunk != array.chunk_size)
625                                 printf("     New Chunksize : %dK\n",
626                                        info->new_chunk/1024);
627                         printf("\n");
628                 } else if (e && e->percent >= 0)
629                         printf("\n");
630                 free_mdstat(ms);
631
632                 if (st && st->sb)
633                         st->ss->detail_super(st, c->homehost, subarray);
634
635                 if (array.raid_disks == 0 && sra &&
636                     sra->array.major_version == -1 &&
637                     sra->array.minor_version == -2 &&
638                     sra->text_version[0] != '/') {
639                         /* This looks like a container.  Find any active arrays
640                          * That claim to be a member.
641                          */
642                         DIR *dir = opendir("/sys/block");
643                         struct dirent *de;
644
645                         printf("     Member Arrays :");
646
647                         while (dir && (de = readdir(dir)) != NULL) {
648                                 char path[287];
649                                 char vbuf[1024];
650                                 int nlen = strlen(sra->sys_name);
651                                 dev_t devid;
652                                 if (de->d_name[0] == '.')
653                                         continue;
654                                 sprintf(path,
655                                         "/sys/block/%s/md/metadata_version",
656                                         de->d_name);
657                                 if (load_sys(path, vbuf, sizeof(vbuf)) < 0)
658                                         continue;
659                                 if (strncmp(vbuf, "external:", 9) ||
660                                     !is_subarray(vbuf + 9) ||
661                                     strncmp(vbuf + 10, sra->sys_name, nlen) ||
662                                     vbuf[10 + nlen] != '/')
663                                         continue;
664                                 devid = devnm2devid(de->d_name);
665                                 printf(" %s",
666                                        map_dev_preferred(major(devid),
667                                                          minor(devid), 1,
668                                                          c->prefer));
669                         }
670                         if (dir)
671                                 closedir(dir);
672                         printf("\n\n");
673                 }
674
675                 if (!c->no_devices) {
676                         if (array.raid_disks)
677                                 printf("    Number   Major   Minor   RaidDevice State\n");
678                         else
679                                 printf("    Number   Major   Minor   RaidDevice\n");
680                 }
681         }
682
683         /* if --no_devices specified, not print component devices info */
684         if (c->no_devices)
685                 goto skip_devices_state;
686
687         for (d = 0; d < max_disks * 2; d++) {
688                 char *dv;
689                 mdu_disk_info_t disk = disks[d];
690
691                 if (d >= array.raid_disks * 2 &&
692                     disk.major == 0 && disk.minor == 0)
693                         continue;
694                 if ((d & 1) && disk.major == 0 && disk.minor == 0)
695                         continue;
696                 if (!c->brief) {
697                         if (d == array.raid_disks*2)
698                                 printf("\n");
699                         if (disk.number < 0 && disk.raid_disk < 0)
700                                 printf("       -   %5d    %5d        -     ",
701                                        disk.major, disk.minor);
702                         else if (disk.raid_disk < 0 ||
703                                  disk.state & (1 << MD_DISK_JOURNAL))
704                                 printf("   %5d   %5d    %5d        -     ",
705                                        disk.number, disk.major, disk.minor);
706                         else if (disk.number < 0)
707                                 printf("       -   %5d    %5d    %5d     ",
708                                        disk.major, disk.minor, disk.raid_disk);
709                         else
710                                 printf("   %5d   %5d    %5d    %5d     ",
711                                        disk.number, disk.major, disk.minor,
712                                        disk.raid_disk);
713                 }
714                 if (!c->brief && array.raid_disks) {
715                         if (disk.state & (1 << MD_DISK_FAULTY)) {
716                                 printf(" faulty");
717                                 if (disk.raid_disk < array.raid_disks &&
718                                     disk.raid_disk >= 0)
719                                         failed++;
720                         }
721                         if (disk.state & (1 << MD_DISK_ACTIVE))
722                                 printf(" active");
723                         if (disk.state & (1 << MD_DISK_SYNC)) {
724                                 printf(" sync");
725                                 if (array.level == 10 &&
726                                     (array.layout & ~0x1FFFF) == 0) {
727                                         int nc = array.layout & 0xff;
728                                         int fc = (array.layout >> 8) & 0xff;
729                                         int copies = nc*fc;
730                                         if (fc == 1 &&
731                                             array.raid_disks % copies == 0 &&
732                                             copies <= 26) {
733                                                 /* We can divide the devices
734                                                    into 'sets' */
735                                                 int set;
736                                                 set = disk.raid_disk % copies;
737                                                 printf(" set-%c", set + 'A');
738                                         }
739                                 }
740                         }
741                         if (disk.state & (1 << MD_DISK_REMOVED))
742                                 printf(" removed");
743                         if (disk.state & (1 << MD_DISK_WRITEMOSTLY))
744                                 printf(" writemostly");
745                         if (disk.state & (1 << MD_DISK_FAILFAST))
746                                 printf(" failfast");
747                         if (disk.state & (1 << MD_DISK_JOURNAL))
748                                 printf(" journal");
749                         if ((disk.state &
750                              ((1 << MD_DISK_ACTIVE) | (1 << MD_DISK_SYNC) |
751                               (1 << MD_DISK_REMOVED) | (1 << MD_DISK_FAULTY) |
752                               (1 << MD_DISK_JOURNAL))) == 0) {
753                                 printf(" spare");
754                                 if (disk.raid_disk < array.raid_disks &&
755                                     disk.raid_disk >= 0)
756                                         printf(" rebuilding");
757                         }
758                 }
759                 if (disk.state == 0)
760                         spares++;
761                 dv = map_dev_preferred(disk.major, disk.minor, 0, c->prefer);
762                 if (dv != NULL) {
763                         if (c->brief)
764                                 n_devices = add_device(dv, &devices,
765                                                        &max_devices, n_devices);
766                         else
767                                 printf("   %s", dv);
768                 }
769                 if (!c->brief)
770                         printf("\n");
771         }
772
773 skip_devices_state:
774         if (spares && c->brief && array.raid_disks)
775                 printf(" spares=%d", spares);
776         if (c->brief && st && st->sb)
777                 st->ss->brief_detail_super(st, subarray);
778         if (st)
779                 st->ss->free_super(st);
780
781         if (c->brief && c->verbose > 0 && devices) {
782                 qsort(devices, n_devices, sizeof(*devices), cmpstringp);
783                 printf("\n   devices=%s", devices[0]);
784                 for (d = 1; d < n_devices; d++)
785                         printf(",%s", devices[d]);
786         }
787         if (c->brief)
788                 printf("\n");
789         if (c->test &&
790             !enough(array.level, array.raid_disks, array.layout, 1, avail))
791                 rv = 2;
792
793 out:
794         free(info);
795         free(disks);
796         close(fd);
797         free(subarray);
798         free(avail);
799         for (d = 0; d < n_devices; d++)
800                 free(devices[d]);
801         free(devices);
802         sysfs_free(sra);
803         return rv;
804 }
805
806 int Detail_Platform(struct superswitch *ss, int scan, int verbose, int export, char *controller_path)
807 {
808         /* display platform capabilities for the given metadata format
809          * 'scan' in this context means iterate over all metadata types
810          */
811         int i;
812         int err = 1;
813
814         if (ss && export && ss->export_detail_platform)
815                 err = ss->export_detail_platform(verbose, controller_path);
816         else if (ss && ss->detail_platform)
817                 err = ss->detail_platform(verbose, 0, controller_path);
818         else if (ss) {
819                 if (verbose > 0)
820                         pr_err("%s metadata is platform independent\n",
821                                 ss->name ? : "[no name]");
822         } else if (!scan) {
823                 if (verbose > 0)
824                         pr_err("specify a metadata type or --scan\n");
825         }
826
827         if (!scan)
828                 return err;
829
830         err = 0;
831         for (i = 0; superlist[i]; i++) {
832                 struct superswitch *meta = superlist[i];
833
834                 if (meta == ss)
835                         continue;
836                 if (verbose > 0)
837                         pr_err("checking metadata %s\n",
838                                 meta->name ? : "[no name]");
839                 if (!meta->detail_platform) {
840                         if (verbose > 0)
841                                 pr_err("%s metadata is platform independent\n",
842                                         meta->name ? : "[no name]");
843                 } else if (export && meta->export_detail_platform) {
844                         err |= meta->export_detail_platform(verbose, controller_path);
845                 } else
846                         err |= meta->detail_platform(verbose, 0, controller_path);
847         }
848
849         return err;
850 }