chiark / gitweb /
Export ID_WWN_VENDOR_EXTENSION and ID_WWN_WITH_EXTENSION
[elogind.git] / extras / scsi_id / scsi_serial.c
index 20d30cf0df64f8398392cf38271aeb79924e0782..0c9d9c3095e9723627c0f38740771329d6fcf976 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
  * scsi_serial.c
  *
  * Code related to requesting and getting an id from a scsi device
 #include <stdlib.h>
 #include <unistd.h>
 #include <syslog.h>
+#include <time.h>
 #include <inttypes.h>
 #include <scsi/scsi.h>
 #include <scsi/sg.h>
 #include <linux/types.h>
-/* #include <linux/bsg.h> */
-#include "bsg.h"
+#include <linux/bsg.h>
 
-#include "../../udev/udev.h"
+#include "libudev.h"
+#include "libudev-private.h"
 #include "scsi.h"
 #include "scsi_id.h"
 
@@ -84,11 +86,15 @@ static const char hex_str[]="0123456789abcdef";
 #define SG_ERR_CAT_MEDIA_CHANGED       1       /* interpreted from sense buffer */
 #define SG_ERR_CAT_RESET               2       /* interpreted from sense buffer */
 #define SG_ERR_CAT_TIMEOUT             3
-#define SG_ERR_CAT_RECOVERED           4       /* Successful command after recovered err */
-#define SG_ERR_CAT_NOTSUPPORTED        5       /* Illegal / unsupported command */
+#define SG_ERR_CAT_RECOVERED           4       /* Successful command after recovered err */
+#define SG_ERR_CAT_NOTSUPPORTED                5       /* Illegal / unsupported command */
 #define SG_ERR_CAT_SENSE               98      /* Something else in the sense buffer */
 #define SG_ERR_CAT_OTHER               99      /* Some other error/warning */
 
+static int do_scsi_page80_inquiry(struct udev *udev,
+                                 struct scsi_id_device *dev_scsi, int fd,
+                                 char *serial, char *serial_short, int max_len);
+
 static int sg_err_category_new(struct udev *udev,
                               int scsi_status, int msg_status, int
                               host_status, int driver_status, const
@@ -168,7 +174,6 @@ static int scsi_dump_sense(struct udev *udev,
        int code;
        int sense_class;
        int sense_key;
-       int descriptor_format;
        int asc, ascq;
 #ifdef DUMP_SENSE
        char out_buffer[256];
@@ -208,7 +213,6 @@ static int scsi_dump_sense(struct udev *udev,
                        return -1;
                }
                if ((code == 0x0) || (code == 0x1)) {
-                       descriptor_format = 0;
                        sense_key = sense_buffer[2] & 0xf;
                        if (s < 14) {
                                /*
@@ -221,7 +225,6 @@ static int scsi_dump_sense(struct udev *udev,
                        asc = sense_buffer[12];
                        ascq = sense_buffer[13];
                } else if ((code == 0x2) || (code == 0x3)) {
-                       descriptor_format = 1;
                        sense_key = sense_buffer[1] & 0xf;
                        asc = sense_buffer[2];
                        ascq = sense_buffer[3];
@@ -422,7 +425,7 @@ static int do_scsi_page0_inquiry(struct udev *udev,
                return 1;
        }
        if (buffer[3] > len) {
-               info(udev, "%s: page 0 buffer too long %d\n", dev_scsi->kernel,  buffer[3]);
+               info(udev, "%s: page 0 buffer too long %d\n", dev_scsi->kernel,  buffer[3]);
                return 1;
        }
 
@@ -456,11 +459,8 @@ static int prepend_vendor_model(struct udev *udev,
        int ind;
 
        strncpy(serial, dev_scsi->vendor, VENDOR_LENGTH);
-       ind = strlen(serial) - 1;
-
        strncat(serial, dev_scsi->model, MODEL_LENGTH);
-       ind = strlen(serial) - 1;
-       ind++;
+       ind = strlen(serial);
 
        /*
         * This is not a complete check, since we are using strncat/cpy
@@ -469,7 +469,7 @@ static int prepend_vendor_model(struct udev *udev,
        if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) {
                info(udev, "%s: expected length %d, got length %d\n",
                     dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind);
-               return 1;
+               return -1;
        }
        return ind;
 }
@@ -482,7 +482,9 @@ static int check_fill_0x83_id(struct udev *udev,
                              struct scsi_id_device *dev_scsi,
                              unsigned char *page_83,
                              const struct scsi_id_search_values
-                             *id_search, char *serial, char *serial_short, int max_len)
+                             *id_search, char *serial, char *serial_short, int max_len,
+                              char *wwn,
+                             char *wwn_vendor_extension)
 {
        int i, j, s, len;
 
@@ -566,6 +568,13 @@ static int check_fill_0x83_id(struct udev *udev,
        }
 
        strcpy(serial_short, &serial[s]);
+
+        if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) {
+                strncpy(wwn, &serial[s], 16);
+               if (wwn_vendor_extension != NULL) {
+                       strncpy(wwn_vendor_extension, &serial[s + 16], 16);
+               }
+        }
        return 0;
 }
 
@@ -583,11 +592,12 @@ static int check_fill_0x83_prespc3(struct udev *udev,
        /* serial has been memset to zero before */
        j = strlen(serial);     /* j = 1; */
 
-       for (i = 0; i < page_83[3]; ++i) {
+       for (i = 0; (i < page_83[3]) && (j < max_len-3); ++i) {
                serial[j++] = hex_str[(page_83[4+i] & 0xf0) >> 4];
                serial[j++] = hex_str[ page_83[4+i] & 0x0f];
        }
-       strcpy(serial_short, serial);
+       serial[max_len-1] = 0;
+       strncpy(serial_short, serial, max_len-1);
        return 0;
 }
 
@@ -595,12 +605,17 @@ static int check_fill_0x83_prespc3(struct udev *udev,
 /* Get device identification VPD page */
 static int do_scsi_page83_inquiry(struct udev *udev,
                                  struct scsi_id_device *dev_scsi, int fd,
-                                 char *serial, char *serial_short, int len)
+                                 char *serial, char *serial_short, int len,
+                                  char *unit_serial_number, char *wwn,
+                                 char *wwn_vendor_extension)
 {
        int retval;
        unsigned int id_ind, j;
        unsigned char page_83[SCSI_INQ_BUFF_LEN];
 
+        /* also pick up the page 80 serial number */
+        do_scsi_page80_inquiry(udev, dev_scsi, fd, NULL, unit_serial_number, MAX_SERIAL_LEN);
+
        memset(page_83, 0, SCSI_INQ_BUFF_LEN);
        retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83,
                              SCSI_INQ_BUFF_LEN);
@@ -614,7 +629,7 @@ static int do_scsi_page83_inquiry(struct udev *udev,
        
        /*
         * XXX Some devices (IBM 3542) return all spaces for an identifier if
-        * the LUN is not actually configured. This leads to identifers of
+        * the LUN is not actually configured. This leads to identifiers of
         * the form: "1            ".
         */
 
@@ -647,7 +662,8 @@ static int do_scsi_page83_inquiry(struct udev *udev,
                                               serial, serial_short, len);
 
        /*
-        * Search for a match in the prioritized id_search_list.
+        * Search for a match in the prioritized id_search_list - since WWN ids
+         * come first we can pick up the WWN in check_fill_0x83_id().
         */
        for (id_ind = 0;
             id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]);
@@ -660,7 +676,9 @@ static int do_scsi_page83_inquiry(struct udev *udev,
                        retval = check_fill_0x83_id(udev,
                                                    dev_scsi, &page_83[j],
                                                    &id_search_list[id_ind],
-                                                   serial, serial_short, len);
+                                                   serial, serial_short, len,
+                                                    wwn,
+                                                   wwn_vendor_extension);
                        dbg(udev, "%s id desc %d/%d/%d\n", dev_scsi->kernel,
                                id_search_list[id_ind].id_type,
                                id_search_list[id_ind].naa_type,
@@ -772,22 +790,26 @@ static int do_scsi_page80_inquiry(struct udev *udev,
        len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
        if (max_len < len) {
                info(udev, "%s: length %d too short - need %d\n",
-                   dev_scsi->kernel, max_len, len);
+                    dev_scsi->kernel, max_len, len);
                return 1;
        }
        /*
         * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
         * specific type where we prepend '0' + vendor + model.
         */
-       serial[0] = 'S';
-       ser_ind = prepend_vendor_model(udev, dev_scsi, &serial[1]);
-       if (ser_ind < 0)
-               return 1;
-       len = buf[3];
-       for (i = 4; i < len + 4; i++, ser_ind++)
-               serial[ser_ind] = buf[i];
-       memcpy(serial_short, &buf[4], len);
-       serial_short[len] = '\0';
+        len = buf[3];
+        if (serial != NULL) {
+                serial[0] = 'S';
+                ser_ind = prepend_vendor_model(udev, dev_scsi, &serial[1]);
+                if (ser_ind < 0)
+                        return 1;
+                for (i = 4; i < len + 4; i++, ser_ind++)
+                        serial[ser_ind] = buf[i];
+        }
+        if (serial_short != NULL) {
+                memcpy(serial_short, &buf[4], len);
+                serial_short[len] = '\0';
+        }
        return 0;
 }
 
@@ -837,7 +859,7 @@ out:
 
 int scsi_get_serial(struct udev *udev,
                    struct scsi_id_device *dev_scsi, const char *devname,
-                   int page_code, char *serial_short, int len)
+                   int page_code, int len)
 {
        unsigned char page0[SCSI_INQ_BUFF_LEN];
        int fd = -1;
@@ -848,19 +870,21 @@ int scsi_get_serial(struct udev *udev,
        memset(dev_scsi->serial, 0, len);
        dbg(udev, "opening %s\n", devname);
        while (--cnt) {
+               const struct timespec duration = { 0, 500 * 1000 * 1000 };
+
                fd = open(devname, O_RDONLY | O_NONBLOCK);
                if (fd >= 0)
                        break;
                info(udev, "%s: cannot open %s: %s\n", dev_scsi->kernel, devname, strerror(errno));
                if (errno != EBUSY)
                        break;
-               usleep(500000 + (rand() % 100000) );
+               nanosleep(&duration, NULL);
        }
        if (fd < 0)
                return 1;
 
        if (page_code == PAGE_80) {
-               if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+               if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
                        retval = 1;
                        goto completed;
                } else  {
@@ -868,7 +892,7 @@ int scsi_get_serial(struct udev *udev,
                        goto completed;
                }
        } else if (page_code == PAGE_83) {
-               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension)) {
                        retval = 1;
                        goto completed;
                } else  {
@@ -876,7 +900,7 @@ int scsi_get_serial(struct udev *udev,
                        goto completed;
                }
        } else if (page_code == PAGE_83_PRE_SPC3) {
-               retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len);
+               retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len);
                if (retval) {
                        /*
                         * Fallback to servicing a SPC-2/3 compliant page 83
@@ -884,7 +908,7 @@ int scsi_get_serial(struct udev *udev,
                         * conform to pre-SPC3 expectations.
                         */
                        if (retval == 2) {
-                               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+                               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension)) {
                                        retval = 1;
                                        goto completed;
                                } else  {
@@ -924,7 +948,7 @@ int scsi_get_serial(struct udev *udev,
        for (ind = 4; ind <= page0[3] + 3; ind++)
                if (page0[ind] == PAGE_83)
                        if (!do_scsi_page83_inquiry(udev, dev_scsi, fd,
-                                                   dev_scsi->serial, serial_short, len)) {
+                                                   dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension)) {
                                /*
                                 * Success
                                 */
@@ -935,7 +959,7 @@ int scsi_get_serial(struct udev *udev,
        for (ind = 4; ind <= page0[3] + 3; ind++)
                if (page0[ind] == PAGE_80)
                        if (!do_scsi_page80_inquiry(udev, dev_scsi, fd,
-                                                   dev_scsi->serial, serial_short, len)) {
+                                                   dev_scsi->serial, dev_scsi->serial_short, len)) {
                                /*
                                 * Success
                                 */