]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
util: make env checking more generic
[thirdparty/mdadm.git] / super-intel.c
index 80427e4a7c25d29e8e0e2e5771b9ddfa31f04451..f5ce06bdacb055f5373369d076e800eb8e7b983b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * mdadm - Intel(R) Matrix Storage Manager Support
  *
- * Copyright (C) 2002-2007 Intel Corporation
+ * Copyright (C) 2002-2008 Intel Corporation
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
@@ -180,6 +180,7 @@ struct intel_super {
                int fd;
        } *disks;
        struct dl *add; /* list of disks to add while mdmon active */
+       struct dl *missing; /* disks removed while we weren't looking */
        struct bbm_log *bbm_log;
 };
 
@@ -212,17 +213,6 @@ struct imsm_update_add_disk {
        enum imsm_update_type type;
 };
 
-static int imsm_env_devname_as_serial(void)
-{
-       char *val = getenv("IMSM_DEVNAME_AS_SERIAL");
-
-       if (val && atoi(val) == 1)
-               return 1;
-
-       return 0;
-}
-
-
 static struct supertype *match_metadata_desc_imsm(char *arg)
 {
        struct supertype *st;
@@ -492,6 +482,8 @@ static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
 }
 
 #ifndef MDASSEMBLE
+static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
+
 static void print_imsm_dev(struct imsm_dev *dev, int index)
 {
        __u64 sz;
@@ -500,7 +492,7 @@ static void print_imsm_dev(struct imsm_dev *dev, int index)
        __u32 ord;
 
        printf("\n");
-       printf("[%s]:\n", dev->volume);
+       printf("[%.16s]:\n", dev->volume);
        printf("     RAID Level : %d\n", get_imsm_raid_level(map));
        printf("        Members : %d\n", map->num_members);
        for (slot = 0; slot < map->num_members; slot++)
@@ -570,15 +562,21 @@ static void examine_super_imsm(struct supertype *st, char *homehost)
        struct imsm_super *mpb = super->anchor;
        char str[MAX_SIGNATURE_LENGTH];
        int i;
+       struct mdinfo info;
+       char nbuf[64];
        __u32 sum;
        __u32 reserved = imsm_reserved_sectors(super, super->disks);
 
+
        snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
        printf("          Magic : %s\n", str);
        snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
        printf("        Version : %s\n", get_imsm_version(mpb));
        printf("         Family : %08x\n", __le32_to_cpu(mpb->family_num));
        printf("     Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
+       getinfo_super_imsm(st, &info);
+       fname_from_uuid(st, &info, nbuf,'-');
+       printf("           UUID : %s\n", nbuf + 5);
        sum = __le32_to_cpu(mpb->check_sum);
        printf("       Checksum : %08x %s\n", sum,
                __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
@@ -610,14 +608,27 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
 
 static void brief_examine_super_imsm(struct supertype *st)
 {
-       /* We just write a generic DDF ARRAY entry
-        */
+       /* We just write a generic IMSM ARRAY entry */
        struct mdinfo info;
        char nbuf[64];
+       struct intel_super *super = st->sb;
+       int i;
+
+       if (!super->anchor->num_raid_devs)
+               return;
 
        getinfo_super_imsm(st, &info);
        fname_from_uuid(st, &info, nbuf,'-');
-       printf("ARRAY /dev/imsm metadata=imsm UUID=%s\n", nbuf + 5);
+       printf("ARRAY /dev/imsm metadata=imsm auto=md UUID=%s\n", nbuf + 5);
+       for (i = 0; i < super->anchor->num_raid_devs; i++) {
+               struct imsm_dev *dev = get_imsm_dev(super, i);
+
+               super->current_vol = i;
+               getinfo_super_imsm(st, &info);
+               fname_from_uuid(st, &info, nbuf,'-');
+               printf("ARRAY /dev/md/%.16s container=/dev/imsm member=%d auto=mdp UUID=%s\n",
+                      dev->volume, i, nbuf + 5);
+       }
 }
 
 static void detail_super_imsm(struct supertype *st, char *homehost)
@@ -810,16 +821,21 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
                __u32 reserved = imsm_reserved_sectors(super, super->disks);
 
                disk = &super->disks->disk;
-               info->disk.number = super->disks->index;
-               info->disk.raid_disk = super->disks->index;
                info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
                info->component_size = reserved;
                s = __le32_to_cpu(disk->status);
                info->disk.state  = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
                info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
-               info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
+               info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
        }
-       uuid_from_super_imsm(st, info->uuid);
+
+       /* only call uuid_from_super_imsm when this disk is part of a populated container,
+        * ->compare_super may have updated the 'num_raid_devs' field for spares
+        */
+       if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
+               uuid_from_super_imsm(st, info->uuid);
+       else
+               memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
 }
 
 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
@@ -925,6 +941,24 @@ static int compare_super_imsm(struct supertype *st, struct supertype *tst)
         */
        if (first->anchor->num_raid_devs == 0 &&
            sec->anchor->num_raid_devs > 0) {
+               int i;
+
+               /* we need to copy raid device info from sec if an allocation
+                * fails here we don't associate the spare
+                */
+               for (i = 0; i < sec->anchor->num_raid_devs; i++) {
+                       first->dev_tbl[i] = malloc(sizeof(struct imsm_dev));
+                       if (!first->dev_tbl) {
+                               while (--i >= 0) {
+                                       free(first->dev_tbl[i]);
+                                       first->dev_tbl[i] = NULL;
+                               }
+                               fprintf(stderr, "imsm: failed to associate spare\n"); 
+                               return 3;
+                       }
+                       *first->dev_tbl[i] = *sec->dev_tbl[i];
+               }
+
                first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
                first->anchor->family_num = sec->anchor->family_num;
        }
@@ -972,7 +1006,7 @@ static int imsm_read_serial(int fd, char *devname,
 
        rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
 
-       if (rv && imsm_env_devname_as_serial()) {
+       if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
                memset(serial, 0, MAX_RAID_SERIAL_LEN);
                fd2devname(fd, (char *) serial);
                return 0;
@@ -1096,6 +1130,13 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
                }
        }
 
+       /* no match, maybe a stale failed drive */
+       if (i == super->anchor->num_disks && dl->index >= 0) {
+               dl->disk = *__get_imsm_disk(super->anchor, dl->index);
+               if (__le32_to_cpu(dl->disk.status) & FAILED_DISK)
+                       dl->index = -2;
+       }
+
        if (alloc)
                super->disks = dl;
 
@@ -1321,12 +1362,19 @@ static void __free_imsm_disk(struct dl *d)
 }
 static void free_imsm_disks(struct intel_super *super)
 {
-       while (super->disks) {
-               struct dl *d = super->disks;
+       struct dl *d;
 
+       while (super->disks) {
+               d = super->disks;
                super->disks = d->next;
                __free_imsm_disk(d);
        }
+       while (super->missing) {
+               d = super->missing;
+               super->missing = d->next;
+               __free_imsm_disk(d);
+       }
+
 }
 
 /* free all the pieces hanging off of a super pointer */
@@ -1378,6 +1426,49 @@ static struct intel_super *alloc_super(int creating_imsm)
 }
 
 #ifndef MDASSEMBLE
+/* find_missing - helper routine for load_super_imsm_all that identifies
+ * disks that have disappeared from the system.  This routine relies on
+ * the mpb being uptodate, which it is at load time.
+ */
+static int find_missing(struct intel_super *super)
+{
+       int i;
+       struct imsm_super *mpb = super->anchor;
+       struct dl *dl;
+       struct imsm_disk *disk;
+       __u32 status;
+
+       for (i = 0; i < mpb->num_disks; i++) {
+               disk = __get_imsm_disk(mpb, i);
+               for (dl = super->disks; dl; dl = dl->next)
+                       if (serialcmp(dl->disk.serial, disk->serial) == 0)
+                               break;
+               if (dl)
+                       continue;
+               /* ok we have a 'disk' without a live entry in
+                * super->disks
+                */
+               status = __le32_to_cpu(disk->status);
+               if (status & FAILED_DISK || !(status & USABLE_DISK))
+                       continue; /* never mind, already marked */
+
+               dl = malloc(sizeof(*dl));
+               if (!dl)
+                       return 1;
+               dl->major = 0;
+               dl->minor = 0;
+               dl->fd = -1;
+               dl->devname = strdup("missing");
+               dl->index = i;
+               serialcpy(dl->serial, disk->serial);
+               dl->disk = *disk;
+               dl->next = super->missing;
+               super->missing = dl;
+       }
+
+       return 0;
+}
+
 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
                               char *devname, int keep_fd)
 {
@@ -1462,6 +1553,12 @@ static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
                        close(dfd);
        }
 
+
+       if (find_missing(super) != 0) {
+               free_imsm(super);
+               return 2;
+       }
+
        if (st->subarray[0]) {
                if (atoi(st->subarray) <= super->anchor->num_raid_devs)
                        super->current_vol = atoi(st->subarray);
@@ -1854,14 +1951,15 @@ static int write_super_imsm(struct intel_super *super, int doclose)
        generation++;
        mpb->generation_num = __cpu_to_le32(generation);
 
+       mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
        for (d = super->disks; d; d = d->next) {
                if (d->index == -1)
                        spares++;
-               else {
+               else
                        mpb->disk[d->index] = d->disk;
-                       mpb_size += sizeof(struct imsm_disk);
-               }
        }
+       for (d = super->missing; d; d = d->next)
+               mpb->disk[d->index] = d->disk;
 
        for (i = 0; i < mpb->num_raid_devs; i++) {
                struct imsm_dev *dev = __get_imsm_dev(mpb, i);
@@ -2374,7 +2472,8 @@ static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
                 * slot+1
                 */
                int i;
-               int insync;
+               /* gcc -Os complains that this is unused */
+               int insync = insync;
 
                for (i = 0; i < map->num_members; i++) {
                        __u32 ord = get_imsm_ord_tbl_ent(dev, i);
@@ -2472,6 +2571,18 @@ static int is_rebuilding(struct imsm_dev *dev)
                return 0;
 }
 
+static void mark_failure(struct imsm_disk *disk)
+{
+       __u32 status = __le32_to_cpu(disk->status);
+
+       if (status & FAILED_DISK)
+               return;
+       status |= FAILED_DISK;
+       disk->status = __cpu_to_le32(status);
+       disk->scsi_id = __cpu_to_le32(~(__u32)0);
+       memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
+}
+
 /* Handle dirty -> clean transititions and resync.  Degraded and rebuild
  * states are handled in imsm_set_disk() with one exception, when a
  * resync is stopped due to a new failure this routine will set the
@@ -2486,6 +2597,17 @@ static int imsm_set_array_state(struct active_array *a, int consistent)
        int failed = imsm_count_failed(super, dev);
        __u8 map_state = imsm_check_degraded(super, dev, failed);
 
+       /* before we activate this array handle any missing disks */
+       if (consistent == 2 && super->missing) {
+               struct dl *dl;
+
+               dprintf("imsm: mark missing\n");
+               end_migration(dev, map_state);
+               for (dl = super->missing; dl; dl = dl->next)
+                       mark_failure(&dl->disk);
+               super->updates_pending++;
+       }
+               
        if (consistent == 2 &&
            (!is_resync_complete(a) ||
             map_state != IMSM_T_STATE_NORMAL ||
@@ -2558,12 +2680,10 @@ static void imsm_set_disk(struct active_array *a, int n, int state)
        /* check for new failures */
        status = __le32_to_cpu(disk->status);
        if ((state & DS_FAULTY) && !(status & FAILED_DISK)) {
-               status |= FAILED_DISK;
-               disk->status = __cpu_to_le32(status);
-               disk->scsi_id = __cpu_to_le32(~(__u32)0);
-               memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
+               mark_failure(disk);
                super->updates_pending++;
        }
+
        /* check if in_sync */
        if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD) {
                struct imsm_map *migr_map = get_imsm_map(dev, 1);
@@ -2798,6 +2918,8 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
  
                /* found a usable disk with enough space */
                di = malloc(sizeof(*di));
+               if (!di)
+                       continue;
                memset(di, 0, sizeof(*di));
 
                /* dl->index will be -1 in the case we are activating a
@@ -2837,7 +2959,23 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
         * disk_ord_tbl for the array
         */
        mu = malloc(sizeof(*mu));
-       mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
+       if (mu) {
+               mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
+               if (mu->buf == NULL) {
+                       free(mu);
+                       mu = NULL;
+               }
+       }
+       if (!mu) {
+               while (rv) {
+                       struct mdinfo *n = rv->next;
+
+                       free(rv);
+                       rv = n;
+               }
+               return NULL;
+       }
+                       
        mu->space = NULL;
        mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
        mu->next = *updates;
@@ -2987,12 +3125,18 @@ static void imsm_process_update(struct supertype *st,
                if (!found) {
                        struct dl **dlp;
 
+                       /* We know that 'manager' isn't touching anything,
+                        * so it is safe to delete
+                        */
                        for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
                                if ((*dlp)->index == victim)
                                        break;
-                       /* We know that 'manager' isn't touching anything,
-                        * so it is safe to:
-                        */
+
+                       /* victim may be on the missing list */
+                       if (!*dlp)
+                               for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
+                                       if ((*dlp)->index == victim)
+                                               break;
                        imsm_delete(super, dlp, victim);
                }
                break;
@@ -3090,6 +3234,8 @@ static void imsm_process_update(struct supertype *st,
                 * being added */
                if (super->add) {
                        struct active_array *a;
+
+                       super->updates_pending++;
                        for (a = st->arrays; a; a = a->next)
                                a->check_degraded = 1;
                }
@@ -3175,6 +3321,9 @@ static void imsm_delete(struct intel_super *super, struct dl **dlp, int index)
        for (iter = super->disks; iter; iter = iter->next)
                if (iter->index > index)
                        iter->index--;
+       for (iter = super->missing; iter; iter = iter->next)
+               if (iter->index > index)
+                       iter->index--;
 
        for (i = 0; i < mpb->num_raid_devs; i++) {
                dev = get_imsm_dev(super, i);