Blame SOURCES/0053-imsm-Change-the-way-of-printing-nvme-drives-in-detai.patch

8e8941
From 6da53c0e2aab200605722795798b1e4f2352cd64 Mon Sep 17 00:00:00 2001
8e8941
From: Blazej Kucman <blazej.kucman@intel.com>
8e8941
Date: Mon, 2 Dec 2019 10:52:05 +0100
8e8941
Subject: [RHEL8.2 PATCH 53/61] imsm: Change the way of printing nvme drives in
8e8941
 detail-platform.
8e8941
8e8941
Change NVMe controller path to device node path
8e8941
in mdadm --detail-platform and print serial number.
8e8941
The method imsm_read_serial always trimes serial to
8e8941
MAX_RAID_SERIAL_LEN, added parameter 'serial_buf_len'
8e8941
will be used to check the serial fit
8e8941
to passed buffor, if not, will be trimed.
8e8941
8e8941
Signed-off-by: Blazej Kucman <blazej.kucman@intel.com>
8e8941
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
8e8941
---
8e8941
 super-intel.c | 97 ++++++++++++++++++++++++++++-------------------------------
8e8941
 1 file changed, 46 insertions(+), 51 deletions(-)
8e8941
8e8941
diff --git a/super-intel.c b/super-intel.c
8e8941
index 86dcb69..5c1f759 100644
8e8941
--- a/super-intel.c
8e8941
+++ b/super-intel.c
8e8941
@@ -2218,7 +2218,8 @@ static void brief_detail_super_imsm(struct supertype *st, char *subarray)
8e8941
 	super->current_vol = temp_vol;
8e8941
 }
8e8941
 
8e8941
-static int imsm_read_serial(int fd, char *devname, __u8 *serial);
8e8941
+static int imsm_read_serial(int fd, char *devname, __u8 *serial,
8e8941
+			    size_t serial_buf_len);
8e8941
 static void fd2devname(int fd, char *name);
8e8941
 
8e8941
 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
8e8941
@@ -2364,8 +2365,9 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
8e8941
 		else {
8e8941
 			fd2devname(fd, buf);
8e8941
 			printf("          Port%d : %s", port, buf);
8e8941
-			if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
8e8941
-				printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf);
8e8941
+			if (imsm_read_serial(fd, NULL, (__u8 *)buf,
8e8941
+					     sizeof(buf)) == 0)
8e8941
+				printf(" (%s)\n", buf);
8e8941
 			else
8e8941
 				printf(" ()\n");
8e8941
 			close(fd);
8e8941
@@ -2388,52 +2390,45 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
8e8941
 	return err;
8e8941
 }
8e8941
 
8e8941
-static int print_vmd_attached_devs(struct sys_dev *hba)
8e8941
+static int print_nvme_info(struct sys_dev *hba)
8e8941
 {
8e8941
+	char buf[1024];
8e8941
 	struct dirent *ent;
8e8941
 	DIR *dir;
8e8941
-	char path[292];
8e8941
-	char link[256];
8e8941
-	char *c, *rp;
8e8941
-
8e8941
-	if (hba->type != SYS_DEV_VMD)
8e8941
-		return 1;
8e8941
+	char *rp;
8e8941
+	int fd;
8e8941
 
8e8941
-	/* scroll through /sys/dev/block looking for devices attached to
8e8941
-	 * this hba
8e8941
-	 */
8e8941
-	dir = opendir("/sys/bus/pci/drivers/nvme");
8e8941
+	dir = opendir("/sys/block/");
8e8941
 	if (!dir)
8e8941
 		return 1;
8e8941
 
8e8941
 	for (ent = readdir(dir); ent; ent = readdir(dir)) {
8e8941
-		int n;
8e8941
-
8e8941
-		/* is 'ent' a device? check that the 'subsystem' link exists and
8e8941
-		 * that its target matches 'bus'
8e8941
-		 */
8e8941
-		sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem",
8e8941
-			ent->d_name);
8e8941
-		n = readlink(path, link, sizeof(link));
8e8941
-		if (n < 0 || n >= (int)sizeof(link))
8e8941
-			continue;
8e8941
-		link[n] = '\0';
8e8941
-		c = strrchr(link, '/');
8e8941
-		if (!c)
8e8941
-			continue;
8e8941
-		if (strncmp("pci", c+1, strlen("pci")) != 0)
8e8941
-			continue;
8e8941
-
8e8941
-		sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name);
8e8941
-
8e8941
-		rp = realpath(path, NULL);
8e8941
-		if (!rp)
8e8941
-			continue;
8e8941
+		if (strstr(ent->d_name, "nvme")) {
8e8941
+			sprintf(buf, "/sys/block/%s", ent->d_name);
8e8941
+			rp = realpath(buf, NULL);
8e8941
+			if (!rp)
8e8941
+				continue;
8e8941
+			if (path_attached_to_hba(rp, hba->path)) {
8e8941
+				fd = open_dev(ent->d_name);
8e8941
+				if (fd < 0) {
8e8941
+					free(rp);
8e8941
+					continue;
8e8941
+				}
8e8941
 
8e8941
-		if (path_attached_to_hba(rp, hba->path)) {
8e8941
-			printf(" NVMe under VMD : %s\n", rp);
8e8941
+				fd2devname(fd, buf);
8e8941
+				if (hba->type == SYS_DEV_VMD)
8e8941
+					printf(" NVMe under VMD : %s", buf);
8e8941
+				else if (hba->type == SYS_DEV_NVME)
8e8941
+					printf("    NVMe Device : %s", buf);
8e8941
+				if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
8e8941
+						      sizeof(buf)))
8e8941
+					printf(" (%s)\n", buf);
8e8941
+				else
8e8941
+					printf("()\n");
8e8941
+				close(fd);
8e8941
+			}
8e8941
+			free(rp);
8e8941
 		}
8e8941
-		free(rp);
8e8941
 	}
8e8941
 
8e8941
 	closedir(dir);
8e8941
@@ -2648,7 +2643,7 @@ static int detail_platform_imsm(int verbose, int enumerate_only, char *controlle
8e8941
 					char buf[PATH_MAX];
8e8941
 					printf(" I/O Controller : %s (%s)\n",
8e8941
 						vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
8e8941
-					if (print_vmd_attached_devs(hba)) {
8e8941
+					if (print_nvme_info(hba)) {
8e8941
 						if (verbose > 0)
8e8941
 							pr_err("failed to get devices attached to VMD domain.\n");
8e8941
 						result |= 2;
8e8941
@@ -2663,7 +2658,7 @@ static int detail_platform_imsm(int verbose, int enumerate_only, char *controlle
8e8941
 		if (entry->type == SYS_DEV_NVME) {
8e8941
 			for (hba = list; hba; hba = hba->next) {
8e8941
 				if (hba->type == SYS_DEV_NVME)
8e8941
-					printf("    NVMe Device : %s\n", hba->path);
8e8941
+					print_nvme_info(hba);
8e8941
 			}
8e8941
 			printf("\n");
8e8941
 			continue;
8e8941
@@ -4028,11 +4023,11 @@ static int nvme_get_serial(int fd, void *buf, size_t buf_len)
8e8941
 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
8e8941
 
8e8941
 static int imsm_read_serial(int fd, char *devname,
8e8941
-			    __u8 serial[MAX_RAID_SERIAL_LEN])
8e8941
+			    __u8 *serial, size_t serial_buf_len)
8e8941
 {
8e8941
 	char buf[50];
8e8941
 	int rv;
8e8941
-	int len;
8e8941
+	size_t len;
8e8941
 	char *dest;
8e8941
 	char *src;
8e8941
 	unsigned int i;
8e8941
@@ -4075,13 +4070,13 @@ static int imsm_read_serial(int fd, char *devname,
8e8941
 	len = dest - buf;
8e8941
 	dest = buf;
8e8941
 
8e8941
-	/* truncate leading characters */
8e8941
-	if (len > MAX_RAID_SERIAL_LEN) {
8e8941
-		dest += len - MAX_RAID_SERIAL_LEN;
8e8941
-		len = MAX_RAID_SERIAL_LEN;
8e8941
+	if (len > serial_buf_len) {
8e8941
+		/* truncate leading characters */
8e8941
+		dest += len - serial_buf_len;
8e8941
+		len = serial_buf_len;
8e8941
 	}
8e8941
 
8e8941
-	memset(serial, 0, MAX_RAID_SERIAL_LEN);
8e8941
+	memset(serial, 0, serial_buf_len);
8e8941
 	memcpy(serial, dest, len);
8e8941
 
8e8941
 	return 0;
8e8941
@@ -4136,7 +4131,7 @@ load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
8e8941
 	char name[40];
8e8941
 	__u8 serial[MAX_RAID_SERIAL_LEN];
8e8941
 
8e8941
-	rv = imsm_read_serial(fd, devname, serial);
8e8941
+	rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
8e8941
 
8e8941
 	if (rv != 0)
8e8941
 		return 2;
8e8941
@@ -5844,7 +5839,7 @@ int mark_spare(struct dl *disk)
8e8941
 		return ret_val;
8e8941
 
8e8941
 	ret_val = 0;
8e8941
-	if (!imsm_read_serial(disk->fd, NULL, serial)) {
8e8941
+	if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
8e8941
 		/* Restore disk serial number, because takeover marks disk
8e8941
 		 * as failed and adds to serial ':0' before it becomes
8e8941
 		 * a spare disk.
8e8941
@@ -5895,7 +5890,7 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
8e8941
 	dd->fd = fd;
8e8941
 	dd->e = NULL;
8e8941
 	dd->action = DISK_ADD;
8e8941
-	rv = imsm_read_serial(fd, devname, dd->serial);
8e8941
+	rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
8e8941
 	if (rv) {
8e8941
 		pr_err("failed to retrieve scsi serial, aborting\n");
8e8941
 		if (dd->devname)
8e8941
-- 
8e8941
2.7.5
8e8941