sd: implement ->get_unique_id
[sfrench/cifs-2.6.git] / drivers / scsi / sd.c
index d8f6add416c0aa04430e71844d1b12a7c7a01de2..9b386ca39a913369b2d3797b1d1b9dff5647aadc 100644 (file)
@@ -1757,6 +1757,44 @@ static void sd_rescan(struct device *dev)
        sd_revalidate_disk(sdkp->disk);
 }
 
+static int sd_get_unique_id(struct gendisk *disk, u8 id[16],
+               enum blk_unique_id type)
+{
+       struct scsi_device *sdev = scsi_disk(disk)->device;
+       const struct scsi_vpd *vpd;
+       const unsigned char *d;
+       int ret = -ENXIO, len;
+
+       rcu_read_lock();
+       vpd = rcu_dereference(sdev->vpd_pg83);
+       if (!vpd)
+               goto out_unlock;
+
+       ret = -EINVAL;
+       for (d = vpd->data + 4; d < vpd->data + vpd->len; d += d[3] + 4) {
+               /* we only care about designators with LU association */
+               if (((d[1] >> 4) & 0x3) != 0x00)
+                       continue;
+               if ((d[1] & 0xf) != type)
+                       continue;
+
+               /*
+                * Only exit early if a 16-byte descriptor was found.  Otherwise
+                * keep looking as one with more entropy might still show up.
+                */
+               len = d[3];
+               if (len != 8 && len != 12 && len != 16)
+                       continue;
+               ret = len;
+               memcpy(id, d + 4, len);
+               if (len == 16)
+                       break;
+       }
+out_unlock:
+       rcu_read_unlock();
+       return ret;
+}
+
 static char sd_pr_type(enum pr_type type)
 {
        switch (type) {
@@ -1861,6 +1899,7 @@ static const struct block_device_operations sd_fops = {
        .check_events           = sd_check_events,
        .unlock_native_capacity = sd_unlock_native_capacity,
        .report_zones           = sd_zbc_report_zones,
+       .get_unique_id          = sd_get_unique_id,
        .pr_ops                 = &sd_pr_ops,
 };