]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Detail.c
Show all bitmaps while examining bitmap
[thirdparty/mdadm.git] / Detail.c
index ab49803dbaa2c2abbfa5f3692da965f7f55b4616..dd72eded995d27e8b5b6405abf203f801a608726 100644 (file)
--- a/Detail.c
+++ b/Detail.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
 #include       "md_u.h"
 #include       <dirent.h>
 
+static int cmpstringp(const void *p1, const void *p2)
+{
+       return strcmp(* (char * const *) p1, * (char * const *) p2);
+}
+
+static int add_device(const char *dev, char ***p_devices,
+                     int *p_max_devices, int n_devices)
+{
+       if (n_devices + 1 >= *p_max_devices) {
+               *p_max_devices += 16;
+               *p_devices = xrealloc(*p_devices, *p_max_devices *
+                                     sizeof(**p_devices));
+               if (!*p_devices) {
+                       *p_max_devices = 0;
+                       return 0;
+               }
+       };
+       (*p_devices)[n_devices] = xstrdup(dev);
+       return n_devices + 1;
+}
+
 int Detail(char *dev, struct context *c)
 {
        /*
@@ -42,7 +63,8 @@ int Detail(char *dev, struct context *c)
        int d;
        time_t atime;
        char *str;
-       char *devices = NULL;
+       char **devices = NULL;
+       int max_devices = 0, n_devices = 0;
        int spares = 0;
        struct stat stb;
        int is_26 = get_linux_version() >= 2006000;
@@ -53,12 +75,15 @@ int Detail(char *dev, struct context *c)
        int max_disks = MD_SB_DISKS; /* just a default */
        struct mdinfo *info = NULL;
        struct mdinfo *sra;
+       struct mdinfo *subdev;
        char *member = NULL;
        char *container = NULL;
 
        int rv = c->test ? 4 : 1;
        int avail_disks = 0;
        char *avail = NULL;
+       int external;
+       int inactive;
 
        if (fd < 0) {
                pr_err("cannot open %s: %s\n",
@@ -78,18 +103,21 @@ int Detail(char *dev, struct context *c)
                close(fd);
                return rv;
        }
-       if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
-               if (errno == ENODEV)
-                       pr_err("md device %s does not appear to be active.\n",
-                               dev);
-               else
-                       pr_err("cannot get array detail for %s: %s\n",
-                               dev, strerror(errno));
+       sra = sysfs_read(fd, NULL, GET_VERSION|GET_DEVS);
+       external = (sra != NULL && sra->array.major_version == -1
+                   && sra->array.minor_version == -2);
+       st = super_by_fd(fd, &subarray);
+       if (ioctl(fd, GET_ARRAY_INFO, &array) == 0) {
+               inactive = 0;
+       } else if (errno == ENODEV && sra) {
+               array = sra->array;
+               inactive = 1;
+       } else {
+               pr_err("cannot get array detail for %s: %s\n",
+                      dev, strerror(errno));
                close(fd);
                return rv;
        }
-       sra = sysfs_read(fd, 0, GET_VERSION);
-       st = super_by_fd(fd, &subarray);
 
        if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
                stb.st_rdev = 0;
@@ -102,25 +130,40 @@ int Detail(char *dev, struct context *c)
                /* This is a subarray of some container.
                 * We want the name of the container, and the member
                 */
-               int dn = st->container_dev;
+               int devid = devnm2devid(st->container_devnm);
+               int cfd, err;
 
                member = subarray;
-               container = map_dev_preferred(dev2major(dn), dev2minor(dn), 1, c->prefer);
+               container = map_dev_preferred(major(devid), minor(devid),
+                                             1, c->prefer);
+               cfd = open_dev(st->container_devnm);
+               if (cfd >= 0) {
+                       err = st->ss->load_container(st, cfd, NULL);
+                       close(cfd);
+                       if (err == 0)
+                               info = st->ss->container_content(st, subarray);
+               }
        }
 
-       /* try to load a superblock */
-       if (st) for (d = 0; d < max_disks; d++) {
+       /* try to load a superblock. Try sra->devs first, then try ioctl */
+       if (st && !info) for (d = 0, subdev = sra ? sra->devs : NULL;
+                             d < max_disks || subdev;
+                             subdev ? (void)(subdev = subdev->next) : (void)(d++)){
                mdu_disk_info_t disk;
                char *dv;
                int fd2;
                int err;
-               disk.number = d;
-               if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
-                       continue;
-               if (d >= array.raid_disks &&
-                   disk.major == 0 &&
-                   disk.minor == 0)
-                       continue;
+               if (subdev)
+                       disk = subdev->disk;
+               else {
+                       disk.number = d;
+                       if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
+                               continue;
+                       if (d >= array.raid_disks &&
+                           disk.major == 0 &&
+                           disk.minor == 0)
+                               continue;
+               }
 
                if (array.raid_disks > 0 &&
                    (disk.state & (1 << MD_DISK_ACTIVE)) == 0)
@@ -184,7 +227,8 @@ int Detail(char *dev, struct context *c)
                                printf("MD_LEVEL=%s\n", str);
                        printf("MD_DEVICES=%d\n", array.raid_disks);
                } else {
-                       printf("MD_LEVEL=container\n");
+                       if (!inactive)
+                               printf("MD_LEVEL=container\n");
                        printf("MD_DEVICES=%d\n", array.nr_disks);
                }
                if (container) {
@@ -217,7 +261,7 @@ int Detail(char *dev, struct context *c)
                } else {
                        struct map_ent *mp, *map = NULL;
                        char nbuf[64];
-                       mp = map_by_devnum(&map, fd2devnum(fd));
+                       mp = map_by_devnm(&map, fd2devnm(fd));
                        if (mp) {
                                __fname_from_uuid(mp->uuid, 0, nbuf, ':');
                                printf("MD_UUID=%s\n", nbuf+5);
@@ -229,18 +273,44 @@ int Detail(char *dev, struct context *c)
                                putchar('\n');
                        }
                }
+               if (sra) {
+                       struct mdinfo *mdi;
+                       for (mdi  = sra->devs; mdi; mdi = mdi->next) {
+                               char *path =
+                                       map_dev(mdi->disk.major,
+                                               mdi->disk.minor, 0);
+
+                               if (mdi->disk.raid_disk >= 0)
+                                       printf("MD_DEVICE_%s_ROLE=%d\n",
+                                              mdi->sys_name+4,
+                                              mdi->disk.raid_disk);
+                               else
+                                       printf("MD_DEVICE_%s_ROLE=spare\n",
+                                              mdi->sys_name+4);
+                               if (path)
+                                       printf("MD_DEVICE_%s_DEV=%s\n",
+                                              mdi->sys_name+4, path);
+                       }
+               }
                goto out;
        }
 
-       disks = xmalloc(max_disks * sizeof(mdu_disk_info_t));
-       for (d = 0; d < max_disks; d++) {
+       disks = xmalloc(max_disks * 2 * sizeof(mdu_disk_info_t));
+       for (d = 0; d < max_disks * 2; d++) {
                disks[d].state = (1<<MD_DISK_REMOVED);
                disks[d].major = disks[d].minor = 0;
                disks[d].number = disks[d].raid_disk = d;
        }
 
        next = array.raid_disks*2;
-       for (d = 0; d < max_disks; d++) {
+       if (inactive) {
+               struct mdinfo *mdi;
+               if (sra != NULL)
+                       for (mdi = sra->devs; mdi; mdi = mdi->next) {
+                               disks[next++] = mdi->disk;
+                               disks[next-1].number = -1;
+                       }
+       } else for (d = 0; d < max_disks; d++) {
                mdu_disk_info_t disk;
                disk.number = d;
                if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
@@ -257,7 +327,7 @@ int Detail(char *dev, struct context *c)
                else if (disk.raid_disk >= 0 && disk.raid_disk < array.raid_disks
                         && disks[disk.raid_disk*2+1].state == (1<<MD_DISK_REMOVED))
                        disks[disk.raid_disk*2+1] = disk;
-               else if (next < max_disks)
+               else if (next < max_disks*2)
                        disks[next++] = disk;
        }
 
@@ -274,15 +344,17 @@ int Detail(char *dev, struct context *c)
 
        if (c->brief) {
                mdu_bitmap_file_t bmf;
-               printf("ARRAY %s", dev);
-               if (c->brief > 1) {
+               printf("%sARRAY %s", inactive ? "INACTIVE-":"", dev);
+               if (c->verbose > 0) {
                        if (array.raid_disks)
                                printf(" level=%s num-devices=%d",
                                       str?str:"-unknown-",
                                       array.raid_disks );
-                       else
+                       else if (!inactive)
                                printf(" level=container num-devices=%d",
                                       array.nr_disks);
+                       else
+                               printf(" num-devices=%d", array.nr_disks);
                }
                if (container) {
                        printf(" container=%s", container);
@@ -306,12 +378,11 @@ int Detail(char *dev, struct context *c)
                unsigned long long larray_size;
                struct mdstat_ent *ms = mdstat_read(0, 0);
                struct mdstat_ent *e;
-               int devnum = array.md_minor;
-               if (major(stb.st_rdev) == (unsigned)get_mdp_major())
-                       devnum = -1 - devnum;
+               char *devnm;
 
+               devnm = stat2devnm(&stb);
                for (e=ms; e; e=e->next)
-                       if (e->devnum == devnum)
+                       if (strcmp(e->devnm, devnm) == 0)
                                break;
                if (!get_dev_size(fd, NULL, &larray_size))
                        larray_size = 0;
@@ -331,9 +402,10 @@ int Detail(char *dev, struct context *c)
                atime = array.ctime;
                if (atime)
                        printf("  Creation Time : %.24s\n", ctime(&atime));
-               if (array.raid_disks == 0)
+               if (array.raid_disks == 0 && external)
                        str = "container";
-               printf("     Raid Level : %s\n", str?str:"-unknown-");
+               if (str)
+                       printf("     Raid Level : %s\n", str);
                if (larray_size)
                        printf("     Array Size : %llu%s\n", (larray_size>>10),
                               human_size(larray_size));
@@ -393,12 +465,15 @@ int Detail(char *dev, struct context *c)
                               (!e || (e->percent < 0 && e->percent != RESYNC_PENDING &&
                               e->percent != RESYNC_DELAYED)) ? "" : sync_action[e->resync],
                               larray_size ? "": ", Not Started",
-                              e->percent == RESYNC_DELAYED ? " (DELAYED)": "",
-                              e->percent == RESYNC_PENDING ? " (PENDING)": "");
+                              (e && e->percent == RESYNC_DELAYED) ? " (DELAYED)": "",
+                              (e && e->percent == RESYNC_PENDING) ? " (PENDING)": "");
+               } else if (inactive) {
+                       printf("          State : inactive\n");
                }
                if (array.raid_disks)
                        printf(" Active Devices : %d\n", array.active_disks);
-               printf("Working Devices : %d\n", array.working_disks);
+               if (array.working_disks > 0)
+                       printf("Working Devices : %d\n", array.working_disks);
                if (array.raid_disks) {
                        printf(" Failed Devices : %d\n", array.failed_disks);
                        printf("  Spare Devices : %d\n", array.spare_disks);
@@ -498,7 +573,7 @@ This is pretty boring
                                char path[200];
                                char vbuf[1024];
                                int nlen = strlen(sra->sys_name);
-                               int dn;
+                               int devid;
                                if (de->d_name[0] == '.')
                                        continue;
                                sprintf(path, "/sys/block/%s/md/metadata_version",
@@ -510,10 +585,10 @@ This is pretty boring
                                    strncmp(vbuf+10, sra->sys_name, nlen) != 0 ||
                                    vbuf[10+nlen] != '/')
                                        continue;
-                               dn = devname2devnum(de->d_name);
+                               devid = devnm2devid(de->d_name);
                                printf(" %s", map_dev_preferred(
-                                              dev2major(dn),
-                                              dev2minor(dn), 1, c->prefer));
+                                              major(devid),
+                                              minor(devid), 1, c->prefer));
                        }
                        if (dir)
                                closedir(dir);
@@ -527,7 +602,7 @@ This is pretty boring
        }
        free(info);
 
-       for (d= 0; d < max_disks; d++) {
+       for (d= 0; d < max_disks * 2; d++) {
                char *dv;
                mdu_disk_info_t disk = disks[d];
 
@@ -541,7 +616,10 @@ This is pretty boring
                        continue;
                if (!c->brief) {
                        if (d == array.raid_disks*2) printf("\n");
-                       if (disk.raid_disk < 0)
+                       if (disk.number < 0)
+                               printf("       -   %5d    %5d        -     ",
+                                      disk.major, disk.minor);
+                       else if (disk.raid_disk < 0)
                                printf("   %5d   %5d    %5d        -     ",
                                       disk.number, disk.major, disk.minor);
                        else
@@ -598,14 +676,11 @@ This is pretty boring
                        rv |= 1;
                dv=map_dev_preferred(disk.major, disk.minor, 0, c->prefer);
                if (dv != NULL) {
-                       if (c->brief) {
-                               if (devices) {
-                                       devices = xrealloc(devices,
-                                                         strlen(devices)+1+strlen(dv)+1);
-                                       strcat(strcat(devices,","),dv);
-                               } else
-                                       devices = xstrdup(dv);
-                       } else
+                       if (c->brief)
+                               n_devices = add_device(dv, &devices,
+                                                      &max_devices,
+                                                      n_devices);
+                       else
                                printf("   %s", dv);
                }
                if (!c->brief) printf("\n");
@@ -616,7 +691,12 @@ This is pretty boring
        if (st)
                st->ss->free_super(st);
 
-       if (c->brief > 1 && devices) printf("\n   devices=%s", devices);
+       if (c->brief && c->verbose > 0 && devices) {
+               qsort(devices, n_devices, sizeof(*devices), cmpstringp);
+               printf("\n   devices=%s", devices[0]);
+               for (d = 1; d < n_devices; d++)
+                       printf(",%s", devices[d]);
+       }
        if (c->brief)
                printf("\n");
        if (c->test &&
@@ -629,6 +709,9 @@ out:
        close(fd);
        free(subarray);
        free(avail);
+       for (d = 0; d < n_devices; d++)
+               free(devices[d]);
+       free(devices);
        sysfs_free(sra);
        return rv;
 }