Blame SOURCES/bz2109159-storage_mon-4-fix-possible-false-negatives.patch

ff491c
From 0bb52cf9985bda47e13940761b3d8e2eaddf377c Mon Sep 17 00:00:00 2001
ff491c
From: Kazunori INOUE <kazunori_inoue@newson.co.jp>
ff491c
Date: Wed, 10 Aug 2022 17:35:54 +0900
ff491c
Subject: [PATCH 1/4] storage_mon: Use the O_DIRECT flag in open() to eliminate
ff491c
 cache effects
ff491c
ff491c
---
ff491c
 tools/Makefile.am   |  1 +
ff491c
 tools/storage_mon.c | 82 +++++++++++++++++++++++++++++++++------------
ff491c
 2 files changed, 61 insertions(+), 22 deletions(-)
ff491c
ff491c
diff --git a/tools/Makefile.am b/tools/Makefile.am
ff491c
index 1309223b4..08323fee3 100644
ff491c
--- a/tools/Makefile.am
ff491c
+++ b/tools/Makefile.am
ff491c
@@ -74,6 +74,7 @@ sfex_stat_LDADD		= $(GLIBLIB) -lplumb -lplumbgpl
ff491c
 findif_SOURCES		= findif.c
ff491c
 
ff491c
 storage_mon_SOURCES	= storage_mon.c
ff491c
+storage_mon_CFLAGS	= -D_GNU_SOURCE
ff491c
 
ff491c
 if BUILD_TICKLE
ff491c
 halib_PROGRAMS		+= tickle_tcp
ff491c
diff --git a/tools/storage_mon.c b/tools/storage_mon.c
ff491c
index 930ead41c..ba87492fc 100644
ff491c
--- a/tools/storage_mon.c
ff491c
+++ b/tools/storage_mon.c
ff491c
@@ -31,23 +31,27 @@ static void usage(char *name, FILE *f)
ff491c
 	fprintf(f, "      --help           print this message\n");
ff491c
 }
ff491c
 
ff491c
-/* Check one device */
ff491c
-static void *test_device(const char *device, int verbose, int inject_error_percent)
ff491c
+static int open_device(const char *device, int verbose)
ff491c
 {
ff491c
-	uint64_t devsize;
ff491c
 	int device_fd;
ff491c
 	int res;
ff491c
+	uint64_t devsize;
ff491c
 	off_t seek_spot;
ff491c
-	char buffer[512];
ff491c
 
ff491c
-	if (verbose) {
ff491c
-		printf("Testing device %s\n", device);
ff491c
+#if defined(__linux__) || defined(__FreeBSD__)
ff491c
+	device_fd = open(device, O_RDONLY|O_DIRECT);
ff491c
+	if (device_fd >= 0) {
ff491c
+		return device_fd;
ff491c
+	} else if (errno != EINVAL) {
ff491c
+		fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
+		return -1;
ff491c
 	}
ff491c
+#endif
ff491c
 
ff491c
 	device_fd = open(device, O_RDONLY);
ff491c
 	if (device_fd < 0) {
ff491c
 		fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
-		exit(-1);
ff491c
+		return -1;
ff491c
 	}
ff491c
 #ifdef __FreeBSD__
ff491c
 	res = ioctl(device_fd, DIOCGMEDIASIZE, &devsize);
ff491c
@@ -57,11 +61,12 @@ static void *test_device(const char *device, int verbose, int inject_error_perce
ff491c
 	if (res != 0) {
ff491c
 		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
 		close(device_fd);
ff491c
-		exit(-1);
ff491c
+		return -1;
ff491c
 	}
ff491c
 	if (verbose) {
ff491c
 		fprintf(stderr, "%s: size=%zu\n", device, devsize);
ff491c
 	}
ff491c
+
ff491c
 	/* Don't fret about real randomness */
ff491c
 	srand(time(NULL) + getpid());
ff491c
 	/* Pick a random place on the device - sector aligned */
ff491c
@@ -70,35 +75,64 @@ static void *test_device(const char *device, int verbose, int inject_error_perce
ff491c
 	if (res < 0) {
ff491c
 		fprintf(stderr, "Failed to seek %s: %s\n", device, strerror(errno));
ff491c
 		close(device_fd);
ff491c
-		exit(-1);
ff491c
+		return -1;
ff491c
 	}
ff491c
-
ff491c
 	if (verbose) {
ff491c
 		printf("%s: reading from pos %ld\n", device, seek_spot);
ff491c
 	}
ff491c
+	return device_fd;
ff491c
+}
ff491c
+
ff491c
+/* Check one device */
ff491c
+static void *test_device(const char *device, int verbose, int inject_error_percent)
ff491c
+{
ff491c
+	int device_fd;
ff491c
+	int sec_size = 0;
ff491c
+	int res;
ff491c
+	void *buffer;
ff491c
+
ff491c
+	if (verbose) {
ff491c
+		printf("Testing device %s\n", device);
ff491c
+	}
ff491c
+
ff491c
+	device_fd = open_device(device, verbose);
ff491c
+	if (device_fd < 0) {
ff491c
+		exit(-1);
ff491c
+	}
ff491c
+
ff491c
+	ioctl(device_fd, BLKSSZGET, &sec_size);
ff491c
+	if (sec_size == 0) {
ff491c
+		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
+		goto error;
ff491c
+	}
ff491c
 
ff491c
-	res = read(device_fd, buffer, sizeof(buffer));
ff491c
+	if (posix_memalign(&buffer, sysconf(_SC_PAGESIZE), sec_size) != 0) {
ff491c
+		fprintf(stderr, "Failed to allocate aligned memory: %s\n", strerror(errno));
ff491c
+		goto error;
ff491c
+	}
ff491c
+
ff491c
+	res = read(device_fd, buffer, sec_size);
ff491c
+	free(buffer);
ff491c
 	if (res < 0) {
ff491c
 		fprintf(stderr, "Failed to read %s: %s\n", device, strerror(errno));
ff491c
-		close(device_fd);
ff491c
-		exit(-1);
ff491c
+		goto error;
ff491c
 	}
ff491c
-	if (res < (int)sizeof(buffer)) {
ff491c
-		fprintf(stderr, "Failed to read %ld bytes from %s, got %d\n", sizeof(buffer), device, res);
ff491c
-		close(device_fd);
ff491c
-		exit(-1);
ff491c
+	if (res < sec_size) {
ff491c
+		fprintf(stderr, "Failed to read %d bytes from %s, got %d\n", sec_size, device, res);
ff491c
+		goto error;
ff491c
 	}
ff491c
 
ff491c
 	/* Fake an error */
ff491c
-	if (inject_error_percent && ((rand() % 100) < inject_error_percent)) {
ff491c
-		fprintf(stderr, "People, please fasten your seatbelts, injecting errors!\n");
ff491c
-		close(device_fd);
ff491c
-		exit(-1);
ff491c
+	if (inject_error_percent) {
ff491c
+		srand(time(NULL) + getpid());
ff491c
+		if ((rand() % 100) < inject_error_percent) {
ff491c
+			fprintf(stderr, "People, please fasten your seatbelts, injecting errors!\n");
ff491c
+			goto error;
ff491c
+		}
ff491c
 	}
ff491c
 	res = close(device_fd);
ff491c
 	if (res != 0) {
ff491c
 		fprintf(stderr, "Failed to close %s: %s\n", device, strerror(errno));
ff491c
-		close(device_fd);
ff491c
 		exit(-1);
ff491c
 	}
ff491c
 
ff491c
@@ -106,6 +140,10 @@ static void *test_device(const char *device, int verbose, int inject_error_perce
ff491c
 		printf("%s: done\n", device);
ff491c
 	}
ff491c
 	exit(0);
ff491c
+
ff491c
+error:
ff491c
+	close(device_fd);
ff491c
+	exit(-1);
ff491c
 }
ff491c
 
ff491c
 int main(int argc, char *argv[])
ff491c
ff491c
From ce4e632f29ed6b86b82a959eac5844655baed153 Mon Sep 17 00:00:00 2001
ff491c
From: Kazunori INOUE <kazunori_inoue@newson.co.jp>
ff491c
Date: Mon, 15 Aug 2022 19:17:21 +0900
ff491c
Subject: [PATCH 2/4] storage_mon: fix build-related issues
ff491c
ff491c
---
ff491c
 tools/storage_mon.c | 6 ++++--
ff491c
 1 file changed, 4 insertions(+), 2 deletions(-)
ff491c
ff491c
diff --git a/tools/storage_mon.c b/tools/storage_mon.c
ff491c
index ba87492fc..e34d1975a 100644
ff491c
--- a/tools/storage_mon.c
ff491c
+++ b/tools/storage_mon.c
ff491c
@@ -38,7 +38,6 @@ static int open_device(const char *device, int verbose)
ff491c
 	uint64_t devsize;
ff491c
 	off_t seek_spot;
ff491c
 
ff491c
-#if defined(__linux__) || defined(__FreeBSD__)
ff491c
 	device_fd = open(device, O_RDONLY|O_DIRECT);
ff491c
 	if (device_fd >= 0) {
ff491c
 		return device_fd;
ff491c
@@ -46,7 +45,6 @@ static int open_device(const char *device, int verbose)
ff491c
 		fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
 		return -1;
ff491c
 	}
ff491c
-#endif
ff491c
 
ff491c
 	device_fd = open(device, O_RDONLY);
ff491c
 	if (device_fd < 0) {
ff491c
@@ -100,7 +98,11 @@ static void *test_device(const char *device, int verbose, int inject_error_perce
ff491c
 		exit(-1);
ff491c
 	}
ff491c
 
ff491c
+#ifdef __FreeBSD__
ff491c
+	ioctl(device_fd, DIOCGSECTORSIZE, &sec_size);
ff491c
+#else
ff491c
 	ioctl(device_fd, BLKSSZGET, &sec_size);
ff491c
+#endif
ff491c
 	if (sec_size == 0) {
ff491c
 		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
 		goto error;
ff491c
ff491c
From 7a0aaa0dfdebeab3fae9fe9ddc412c3d1f610273 Mon Sep 17 00:00:00 2001
ff491c
From: Kazunori INOUE <kazunori_inoue@newson.co.jp>
ff491c
Date: Wed, 24 Aug 2022 17:36:23 +0900
ff491c
Subject: [PATCH 3/4] storage_mon: do random lseek even with O_DIRECT, etc
ff491c
ff491c
---
ff491c
 tools/storage_mon.c | 118 ++++++++++++++++++++++----------------------
ff491c
 1 file changed, 58 insertions(+), 60 deletions(-)
ff491c
ff491c
diff --git a/tools/storage_mon.c b/tools/storage_mon.c
ff491c
index e34d1975a..0bdb48649 100644
ff491c
--- a/tools/storage_mon.c
ff491c
+++ b/tools/storage_mon.c
ff491c
@@ -31,38 +31,43 @@ static void usage(char *name, FILE *f)
ff491c
 	fprintf(f, "      --help           print this message\n");
ff491c
 }
ff491c
 
ff491c
-static int open_device(const char *device, int verbose)
ff491c
+/* Check one device */
ff491c
+static void *test_device(const char *device, int verbose, int inject_error_percent)
ff491c
 {
ff491c
+	uint64_t devsize;
ff491c
+	int flags = O_RDONLY | O_DIRECT;
ff491c
 	int device_fd;
ff491c
 	int res;
ff491c
-	uint64_t devsize;
ff491c
 	off_t seek_spot;
ff491c
 
ff491c
-	device_fd = open(device, O_RDONLY|O_DIRECT);
ff491c
-	if (device_fd >= 0) {
ff491c
-		return device_fd;
ff491c
-	} else if (errno != EINVAL) {
ff491c
-		fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
-		return -1;
ff491c
+	if (verbose) {
ff491c
+		printf("Testing device %s\n", device);
ff491c
 	}
ff491c
 
ff491c
-	device_fd = open(device, O_RDONLY);
ff491c
+	device_fd = open(device, flags);
ff491c
 	if (device_fd < 0) {
ff491c
-		fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
-		return -1;
ff491c
+		if (errno != EINVAL) {
ff491c
+			fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
+			exit(-1);
ff491c
+		}
ff491c
+		flags &= ~O_DIRECT;
ff491c
+		device_fd = open(device, flags);
ff491c
+		if (device_fd < 0) {
ff491c
+			fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
ff491c
+			exit(-1);
ff491c
+		}
ff491c
 	}
ff491c
 #ifdef __FreeBSD__
ff491c
 	res = ioctl(device_fd, DIOCGMEDIASIZE, &devsize);
ff491c
 #else
ff491c
 	res = ioctl(device_fd, BLKGETSIZE64, &devsize);
ff491c
 #endif
ff491c
-	if (res != 0) {
ff491c
+	if (res < 0) {
ff491c
 		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
-		close(device_fd);
ff491c
-		return -1;
ff491c
+		goto error;
ff491c
 	}
ff491c
 	if (verbose) {
ff491c
-		fprintf(stderr, "%s: size=%zu\n", device, devsize);
ff491c
+		printf("%s: opened %s O_DIRECT, size=%zu\n", device, (flags & O_DIRECT)?"with":"without", devsize);
ff491c
 	}
ff491c
 
ff491c
 	/* Don't fret about real randomness */
ff491c
@@ -72,65 +77,58 @@ static int open_device(const char *device, int verbose)
ff491c
 	res = lseek(device_fd, seek_spot, SEEK_SET);
ff491c
 	if (res < 0) {
ff491c
 		fprintf(stderr, "Failed to seek %s: %s\n", device, strerror(errno));
ff491c
-		close(device_fd);
ff491c
-		return -1;
ff491c
+		goto error;
ff491c
 	}
ff491c
 	if (verbose) {
ff491c
 		printf("%s: reading from pos %ld\n", device, seek_spot);
ff491c
 	}
ff491c
-	return device_fd;
ff491c
-}
ff491c
-
ff491c
-/* Check one device */
ff491c
-static void *test_device(const char *device, int verbose, int inject_error_percent)
ff491c
-{
ff491c
-	int device_fd;
ff491c
-	int sec_size = 0;
ff491c
-	int res;
ff491c
-	void *buffer;
ff491c
-
ff491c
-	if (verbose) {
ff491c
-		printf("Testing device %s\n", device);
ff491c
-	}
ff491c
 
ff491c
-	device_fd = open_device(device, verbose);
ff491c
-	if (device_fd < 0) {
ff491c
-		exit(-1);
ff491c
-	}
ff491c
+	if (flags & O_DIRECT) {
ff491c
+		int sec_size = 0;
ff491c
+		void *buffer;
ff491c
 
ff491c
 #ifdef __FreeBSD__
ff491c
-	ioctl(device_fd, DIOCGSECTORSIZE, &sec_size);
ff491c
+		res = ioctl(device_fd, DIOCGSECTORSIZE, &sec_size);
ff491c
 #else
ff491c
-	ioctl(device_fd, BLKSSZGET, &sec_size);
ff491c
+		res = ioctl(device_fd, BLKSSZGET, &sec_size);
ff491c
 #endif
ff491c
-	if (sec_size == 0) {
ff491c
-		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
-		goto error;
ff491c
-	}
ff491c
+		if (res < 0) {
ff491c
+			fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
+			goto error;
ff491c
+		}
ff491c
 
ff491c
-	if (posix_memalign(&buffer, sysconf(_SC_PAGESIZE), sec_size) != 0) {
ff491c
-		fprintf(stderr, "Failed to allocate aligned memory: %s\n", strerror(errno));
ff491c
-		goto error;
ff491c
-	}
ff491c
+		if (posix_memalign(&buffer, sysconf(_SC_PAGESIZE), sec_size) != 0) {
ff491c
+			fprintf(stderr, "Failed to allocate aligned memory: %s\n", strerror(errno));
ff491c
+			goto error;
ff491c
+		}
ff491c
+		res = read(device_fd, buffer, sec_size);
ff491c
+		free(buffer);
ff491c
+		if (res < 0) {
ff491c
+			fprintf(stderr, "Failed to read %s: %s\n", device, strerror(errno));
ff491c
+			goto error;
ff491c
+		}
ff491c
+		if (res < sec_size) {
ff491c
+			fprintf(stderr, "Failed to read %d bytes from %s, got %d\n", sec_size, device, res);
ff491c
+			goto error;
ff491c
+		}
ff491c
+	} else {
ff491c
+		char buffer[512];
ff491c
 
ff491c
-	res = read(device_fd, buffer, sec_size);
ff491c
-	free(buffer);
ff491c
-	if (res < 0) {
ff491c
-		fprintf(stderr, "Failed to read %s: %s\n", device, strerror(errno));
ff491c
-		goto error;
ff491c
-	}
ff491c
-	if (res < sec_size) {
ff491c
-		fprintf(stderr, "Failed to read %d bytes from %s, got %d\n", sec_size, device, res);
ff491c
-		goto error;
ff491c
+		res = read(device_fd, buffer, sizeof(buffer));
ff491c
+		if (res < 0) {
ff491c
+			fprintf(stderr, "Failed to read %s: %s\n", device, strerror(errno));
ff491c
+			goto error;
ff491c
+		}
ff491c
+		if (res < (int)sizeof(buffer)) {
ff491c
+			fprintf(stderr, "Failed to read %ld bytes from %s, got %d\n", sizeof(buffer), device, res);
ff491c
+			goto error;
ff491c
+		}
ff491c
 	}
ff491c
 
ff491c
 	/* Fake an error */
ff491c
-	if (inject_error_percent) {
ff491c
-		srand(time(NULL) + getpid());
ff491c
-		if ((rand() % 100) < inject_error_percent) {
ff491c
-			fprintf(stderr, "People, please fasten your seatbelts, injecting errors!\n");
ff491c
-			goto error;
ff491c
-		}
ff491c
+	if (inject_error_percent && ((rand() % 100) < inject_error_percent)) {
ff491c
+		fprintf(stderr, "People, please fasten your seatbelts, injecting errors!\n");
ff491c
+		goto error;
ff491c
 	}
ff491c
 	res = close(device_fd);
ff491c
 	if (res != 0) {
ff491c
ff491c
From db97e055a17526cec056c595844a9d8851e3ee19 Mon Sep 17 00:00:00 2001
ff491c
From: Kazunori INOUE <kazunori_inoue@newson.co.jp>
ff491c
Date: Thu, 25 Aug 2022 16:03:46 +0900
ff491c
Subject: [PATCH 4/4] storage_mon: improve error messages when ioctl() fails
ff491c
ff491c
---
ff491c
 tools/storage_mon.c | 4 ++--
ff491c
 1 file changed, 2 insertions(+), 2 deletions(-)
ff491c
ff491c
diff --git a/tools/storage_mon.c b/tools/storage_mon.c
ff491c
index 0bdb48649..f829c5081 100644
ff491c
--- a/tools/storage_mon.c
ff491c
+++ b/tools/storage_mon.c
ff491c
@@ -63,7 +63,7 @@ static void *test_device(const char *device, int verbose, int inject_error_perce
ff491c
 	res = ioctl(device_fd, BLKGETSIZE64, &devsize);
ff491c
 #endif
ff491c
 	if (res < 0) {
ff491c
-		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
+		fprintf(stderr, "Failed to get device size for %s: %s\n", device, strerror(errno));
ff491c
 		goto error;
ff491c
 	}
ff491c
 	if (verbose) {
ff491c
@@ -93,7 +93,7 @@ static void *test_device(const char *device, int verbose, int inject_error_perce
ff491c
 		res = ioctl(device_fd, BLKSSZGET, &sec_size);
ff491c
 #endif
ff491c
 		if (res < 0) {
ff491c
-			fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
ff491c
+			fprintf(stderr, "Failed to get block device sector size for %s: %s\n", device, strerror(errno));
ff491c
 			goto error;
ff491c
 		}
ff491c