chiark / gitweb /
[PATCH] more extras/multipath changes
[elogind.git] / extras / multipath / main.c
index 0ed74c52a922a977fced2cd23c9f82691637ba41..9402888c58a3c91872a11ab2b3796c54d4f1253b 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <linux/kdev_t.h>
-#include <errno.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <libsysfs.h>
-#include <libdevmapper.h>
+#include "libdevmapper/libdevmapper.h"
 #include "main.h"
 
 static int
 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
        void *resp, int mx_resp_len, int noisy)
 {
-       int res;
        unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
            { INQUIRY_CMD, 0, 0, 0, 0, 0 };
        unsigned char sense_b[SENSE_BUFF_LEN];
@@ -61,14 +59,27 @@ do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
                perror("SG_IO (inquiry) error");
                return -1;
        }
-       res = sg_err_category3(&io_hdr);
-       switch (res) {
-       case SG_ERR_CAT_CLEAN:
-       case SG_ERR_CAT_RECOVERED:
+
+       /* treat SG_ERR here to get rid of sg_err.[ch] */
+       io_hdr.status &= 0x7e;
+       if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
+           (0 == io_hdr.driver_status))
                return 0;
-       default:
-               return -1;
+       if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
+           (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
+           (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
+               if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
+                       int sense_key;
+                       unsigned char * sense_buffer = io_hdr.sbp;
+                       if (sense_buffer[0] & 0x2)
+                               sense_key = sense_buffer[1] & 0xf;
+                       else
+                               sense_key = sense_buffer[2] & 0xf;
+                       if(RECOVERED_ERROR == sense_key)
+                               return 0;
+               }
        }
+       return -1;
 }
 
 static int
@@ -149,8 +160,11 @@ get_serial (int fd, char * str)
 */
 
 /* hardware vendor specifics : add support for new models below */
+
+/* this one get EVPD page 0x83 off 8 */
+/* tested ok with StorageWorks */
 static int
-get_storageworks_wwid(int fd, char *str)
+get_evpd_wwid(int fd, char *str)
 {
        char buff[64];
 
@@ -165,12 +179,39 @@ get_storageworks_wwid(int fd, char *str)
 static int
 get_unique_id(int fd, struct path * mypath)
 {
-       if (strncmp(mypath->product_id, "HSV110 (C)COMPAQ", 16) == 0 ||
-           strncmp(mypath->product_id, "HSG80           ", 16) == 0) {
-               get_storageworks_wwid(fd, mypath->wwid);
-               return 0;
+       int i;
+       static struct {
+               char * vendor;
+               char * product;
+               int (*getuid) (int fd, char * wwid);
+       } wlist[] = {
+               {"COMPAQ  ", "HSV110 (C)COMPAQ", &get_evpd_wwid},
+               {"COMPAQ  ", "MSA1000         ", &get_evpd_wwid},
+               {"COMPAQ  ", "MSA1000 VOLUME  ", &get_evpd_wwid},
+               {"DEC     ", "HSG80           ", &get_evpd_wwid},
+               {"HP      ", "HSV100          ", &get_evpd_wwid},
+               {"HP      ", "A6189A          ", &get_evpd_wwid},
+               {"HP      ", "OPEN-           ", &get_evpd_wwid},
+               {"DDN     ", "SAN DataDirector", &get_evpd_wwid},
+               {"FSC     ", "CentricStor     ", &get_evpd_wwid},
+               {"HITACHI ", "DF400           ", &get_evpd_wwid},
+               {"HITACHI ", "DF500           ", &get_evpd_wwid},
+               {"HITACHI ", "DF600           ", &get_evpd_wwid},
+               {"IBM     ", "ProFibre 4000R  ", &get_evpd_wwid},
+               {"SGI     ", "TP9100          ", &get_evpd_wwid},
+               {"SGI     ", "TP9300          ", &get_evpd_wwid},
+               {"SGI     ", "TP9400          ", &get_evpd_wwid},
+               {"SGI     ", "TP9500          ", &get_evpd_wwid},
+               {NULL, NULL, NULL},
+       };
+
+       for (i = 0; wlist[i].vendor; i++) {
+               if (strncmp(mypath->vendor_id, wlist[i].vendor, 8) == 0 &&
+                   strncmp(mypath->product_id, wlist[i].product, 16) == 0) {
+                       wlist[i].getuid(fd, mypath->wwid);
+                       return 0;
+               }
        }
-
        return 1;
 }
 
@@ -184,6 +225,31 @@ basename(char * str1, char * str2)
        strcpy(str2, ++p);
 }
 
+static int
+blacklist (char * dev) {
+       int i;
+       static struct {
+               char * headstr;
+               int lengh;
+       } blist[] = {
+               {"cciss", 5},
+               {"hd", 2},
+               {"md", 2},
+               {"dm", 2},
+               {"sr", 2},
+               {"scd", 3},
+               {"ram", 3},
+               {"raw", 3},
+               {NULL, 0},
+       };
+
+       for (i = 0; blist[i].lengh; i++) {
+               if (strncmp(dev, blist[i].headstr, blist[i].lengh) == 0)
+                       return 1;
+       }
+       return 0;
+}
+
 static int
 get_all_paths_sysfs(struct env * conf, struct path * all_paths)
 {
@@ -191,7 +257,7 @@ get_all_paths_sysfs(struct env * conf, struct path * all_paths)
        int sg_fd;
        struct sysfs_directory * sdir;
        struct sysfs_directory * devp;
-       struct sysfs_dlink * linkp;
+       struct sysfs_link * linkp;
        char buff[FILE_NAME_SIZE];
 
        char block_path[FILE_NAME_SIZE];
@@ -199,17 +265,17 @@ get_all_paths_sysfs(struct env * conf, struct path * all_paths)
        sprintf(block_path, "%s/block", conf->sysfs_path);
        sdir = sysfs_open_directory(block_path);
        sysfs_read_directory(sdir);
-       devp = sdir->subdirs;
-       while (devp != NULL) {
+       dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
+               if (blacklist(devp->name))
+                       continue;
                sysfs_read_directory(devp);
-               linkp = devp->links;
-               while (linkp != NULL) {
+               if(devp->links == NULL)
+                       continue;
+               dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
                        if (!strncmp(linkp->name, "device", 6))
                                break;
-                       linkp = linkp->next;
                }
                if (linkp == NULL) {
-                       devp = devp->next;
                        continue;
                }
 
@@ -217,21 +283,22 @@ get_all_paths_sysfs(struct env * conf, struct path * all_paths)
                sprintf(all_paths[k].sg_dev, "/dev/%s", buff);
                strcpy(all_paths[k].dev, all_paths[k].sg_dev);
                if ((sg_fd = open(all_paths[k].sg_dev, O_RDONLY)) < 0) {
-                       devp = devp->next;
+                       if (conf->verbose)
+                               fprintf(stderr, "can't open %s. mknod ?",
+                                       all_paths[k].sg_dev); 
                        continue;
                }
                get_lun_strings(sg_fd, &all_paths[k]);
                get_unique_id(sg_fd, &all_paths[k]);
                all_paths[k].state = do_tur(sg_fd);
                close(sg_fd);
-               basename(linkp->target->path, buff);
+               basename(linkp->target, buff);
                sscanf(buff, "%i:%i:%i:%i",
                        &all_paths[k].sg_id.host_no,
                        &all_paths[k].sg_id.channel,
                        &all_paths[k].sg_id.scsi_id,
                        &all_paths[k].sg_id.lun);
                k++;
-               devp = devp->next;
        }
        sysfs_close_directory(sdir);
 
@@ -251,8 +318,12 @@ get_all_paths_nosysfs(struct env * conf, struct path * all_paths,
                sprintf(buff, "%d", k);
                strncat(file_name, buff, FILE_NAME_SIZE);
                strcpy(all_paths[k].sg_dev, file_name);
-               if ((sg_fd = open(file_name, O_RDONLY)) < 0)
+               if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
+                       if (conf->verbose)
+                               fprintf(stderr, "can't open %s. mknod ?",
+                                       file_name); 
                        continue;
+               }
                get_lun_strings(sg_fd, &all_paths[k]);
                get_unique_id(sg_fd, &all_paths[k]);
                all_paths[k].state = do_tur(sg_fd);
@@ -306,8 +377,12 @@ get_all_scsi_ids(struct env * conf, struct scsi_dev * all_scsi_ids)
                } else
                        strcat(fname, "xxxx");
 
-               if ((fd = open(fname, O_RDONLY)) < 0)
+               if ((fd = open(fname, O_RDONLY)) < 0) {
+                       if (conf->verbose)
+                               fprintf(stderr, "can't open %s. mknod ?",
+                                       fname); 
                        continue;
+               }
 
                res = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &my_scsi_id);
                if (res < 0) {
@@ -451,9 +526,7 @@ static int
 make_dm_node(char * str)
 {
        int r = 0;
-       dev_t dev;
        char buff[FILE_NAME_SIZE];
-       int major, minor;
        struct dm_names * names;
         unsigned next = 0;
        struct dm_task *dmt;
@@ -479,14 +552,9 @@ make_dm_node(char * str)
                 names = (void *) names + next;
         } while (next);
 
-       major = (int) MAJOR(names->dev);
-       minor = (int) MINOR(names->dev);
-
-       dev = major << sizeof(dev_t);
-       dev = dev | minor;
        sprintf(buff, "/dev/mapper/%s", str);
        unlink(buff);
-       mknod(buff, 0600 | S_IFBLK, dev);
+       mknod(buff, 0600 | S_IFBLK, names->dev);
 
        out:
        dm_task_destroy(dmt);
@@ -524,6 +592,17 @@ add_map(struct env * conf, struct path * all_paths,
        int i, np;
        long size = -1;
 
+       /* defaults for multipath target */
+       int dm_nr_path_args         = 2;
+       int dm_path_test_int        = 10;
+       char * dm_ps_name           = "round-robin";
+       int dm_ps_nr_args           = 2;
+       int dm_path_failback_int    = 10;
+       int dm_path_nr_fail         = 2;
+       int dm_ps_prio              = 1;
+       int dm_ps_min_io            = 2;
+
+
        if (!(dmt = dm_task_create(op)))
                return 0;
 
@@ -539,7 +618,14 @@ add_map(struct env * conf, struct path * all_paths,
        }
        if (np == 0)
                goto addout;
-       params_p += sprintf(params_p, "%i 32", np);
+       /* temporarily disable creation of single path maps */
+       /* Sistina should modify the target limits */
+       if (np < 2)
+               goto addout;
+
+       params_p += sprintf(params_p, "%i %i %i %s %i",
+                           np, dm_nr_path_args, dm_path_test_int,
+                           dm_ps_name, dm_ps_nr_args);
        
        for (i=0; i<=mp[index].npaths; i++) {
                if (( 0 == all_paths[PINDEX(index,i)].state) ||
@@ -547,8 +633,10 @@ add_map(struct env * conf, struct path * all_paths,
                        continue;
                if (size < 0)
                        size = get_disk_size(conf, all_paths[PINDEX(index,0)].dev);
-               params_p += sprintf(params_p, " %s %i",
-                       all_paths[PINDEX(index,i)].dev, 0);
+               params_p += sprintf(params_p, " %s %i %i %i %i",
+                                   all_paths[PINDEX(index,i)].dev,
+                                   dm_path_failback_int, dm_path_nr_fail,
+                                   dm_ps_prio, dm_ps_min_io);
        }
 
        if (size < 0)
@@ -660,9 +748,20 @@ usage(char * progname)
        exit(1);
 }
 
+static int
+filepresent(char * run) {
+       struct stat buf;
+
+       if(!stat(run, &buf))
+               return 1;
+       return 0;
+}
+
 int
 main(int argc, char *argv[])
 {
+       char * run = "/var/run/multipath.run";
+       char * resched = "/var/run/multipath.reschedule";
        struct multipath * mp;
        struct path * all_paths;
        struct scsi_dev * all_scsi_ids;
@@ -705,11 +804,27 @@ main(int argc, char *argv[])
 
        }
 
+       if (filepresent(run)) {
+               if (conf.verbose) {
+                       fprintf(stderr, "Already running.\n");
+                       fprintf(stderr, "If you know what you do, please ");
+                       fprintf(stderr, "remove %s\n", run);
+               }
+               /* leave a trace that we were called while already running */
+               open(resched, O_CREAT);
+               return 1;
+       }
+
        /* dynamic allocations */
        mp = malloc(conf.max_devs * sizeof(struct multipath));
        all_paths = malloc(conf.max_devs * sizeof(struct path));
        all_scsi_ids = malloc(conf.max_devs * sizeof(struct scsi_dev));
-       if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL)
+       if (mp == NULL || all_paths == NULL || all_scsi_ids == NULL) {
+               unlink(run);
+               exit(1);
+       }
+start:
+       if(!open(run, O_CREAT))
                exit(1);
 
        if (!conf.with_sysfs) {
@@ -722,13 +837,15 @@ main(int argc, char *argv[])
 
        if (conf.verbose) {
                print_all_path(&conf, all_paths);
-               printf("\n");
+               fprintf(stdout, "\n");
                print_all_mp(all_paths, mp, nmp);
-               printf("\n");
+               fprintf(stdout, "\n");
        }
 
-       if (conf.dry_run)
+       if (conf.dry_run) {
+               unlink(run);
                exit(0);
+       }
 
        for (k=0; k<=nmp; k++) {
                if (map_present(mp[k].wwid)) {
@@ -737,5 +854,13 @@ main(int argc, char *argv[])
                        add_map(&conf, all_paths, mp, k, DM_DEVICE_CREATE);
                }
        }
+       unlink(run);
+
+       /* start again if we were ask to during this process run */
+       /* ie. do not loose an event-asked run */
+       if (filepresent(resched)) {
+               unlink(resched);
+               goto start;
+       }
        exit(0);
 }