Blame SOURCES/0075-lib-loopdev-retry-LOOP_SET_STATUS64-and-LOOP_SET_BLO.patch

02ae29
From 97a5abd36eeab4e07a31b27f6a2c2078d42e2e33 Mon Sep 17 00:00:00 2001
02ae29
From: Karel Zak <kzak@redhat.com>
02ae29
Date: Tue, 8 Mar 2022 11:40:58 +0100
02ae29
Subject: lib/loopdev: retry LOOP_SET_STATUS64 and LOOP_SET_BLOCK_SIZE on
02ae29
 EAGAIN
02ae29
02ae29
Upstream: http://github.com/util-linux/util-linux/commit/0ae7bb11c29aa11c8ef25b1ef2f82ee4701b856d
02ae29
Upstream: http://github.com/util-linux/util-linux/commit/eab90ef8d4f66394285e0cff1dfc0a27242c05aa
02ae29
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=2058176
02ae29
Signed-off-by: Karel Zak <kzak@redhat.com>
02ae29
---
02ae29
 lib/loopdev.c | 42 +++++++++++++++++++++++++++++++++++++-----
02ae29
 1 file changed, 37 insertions(+), 5 deletions(-)
02ae29
02ae29
diff --git a/lib/loopdev.c b/lib/loopdev.c
02ae29
index 54d337ea3..48af82aef 100644
02ae29
--- a/lib/loopdev.c
02ae29
+++ b/lib/loopdev.c
02ae29
@@ -42,6 +42,8 @@
02ae29
 #include "blkdev.h"
02ae29
 #include "debug.h"
02ae29
 
02ae29
+#define LOOPDEV_MAX_TRIES       10
02ae29
+
02ae29
 /*
02ae29
  * Debug stuff (based on include/debug.h)
02ae29
  */
02ae29
@@ -1260,6 +1262,7 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)
02ae29
 {
02ae29
 	int file_fd, dev_fd, mode = O_RDWR, rc = -1, cnt = 0;
02ae29
 	int errsv = 0;
02ae29
+	int err, again;
02ae29
 
02ae29
 	if (!lc || !*lc->device || !lc->filename)
02ae29
 		return -EINVAL;
02ae29
@@ -1331,7 +1334,17 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)
02ae29
 
02ae29
 	DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD: OK"));
02ae29
 
02ae29
-	if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) {
02ae29
+	cnt = 0;
02ae29
+	do {
02ae29
+		err = ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info);
02ae29
+		again = err && errno == EAGAIN;
02ae29
+		if (again) {
02ae29
+			xusleep(250000);
02ae29
+			cnt++;
02ae29
+		}
02ae29
+	} while (again && cnt <= LOOPDEV_MAX_TRIES);
02ae29
+
02ae29
+	if (err) {
02ae29
 		rc = -errno;
02ae29
 		errsv = errno;
02ae29
 		DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
02ae29
@@ -1376,7 +1389,7 @@ err:
02ae29
  */
02ae29
 int loopcxt_set_status(struct loopdev_cxt *lc)
02ae29
 {
02ae29
-	int dev_fd, rc = -1;
02ae29
+	int dev_fd, rc = -1, err, again, tries = 0;
02ae29
 
02ae29
 	errno = 0;
02ae29
 	dev_fd = loopcxt_get_fd(lc);
02ae29
@@ -1387,7 +1400,16 @@ int loopcxt_set_status(struct loopdev_cxt *lc)
02ae29
 	}
02ae29
 	DBG(SETUP, ul_debugobj(lc, "device open: OK"));
02ae29
 
02ae29
-	if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) {
02ae29
+	do {
02ae29
+		err = ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info);
02ae29
+		again = err && errno == EAGAIN;
02ae29
+		if (again) {
02ae29
+			xusleep(250000);
02ae29
+			tries++;
02ae29
+		}
02ae29
+	} while (again && tries <= LOOPDEV_MAX_TRIES);
02ae29
+
02ae29
+	if (err) {
02ae29
 		rc = -errno;
02ae29
 		DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
02ae29
 		return rc;
02ae29
@@ -1440,12 +1462,22 @@ int loopcxt_set_dio(struct loopdev_cxt *lc, unsigned long use_dio)
02ae29
 int loopcxt_set_blocksize(struct loopdev_cxt *lc, uint64_t blocksize)
02ae29
 {
02ae29
 	int fd = loopcxt_get_fd(lc);
02ae29
+	int err, again, tries = 0;
02ae29
 
02ae29
 	if (fd < 0)
02ae29
 		return -EINVAL;
02ae29
 
02ae29
-	/* Kernels prior to v4.14 don't support this ioctl */
02ae29
-	if (ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) blocksize) < 0) {
02ae29
+	do {
02ae29
+		/* Kernels prior to v4.14 don't support this ioctl */
02ae29
+		err = ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) blocksize);
02ae29
+		again = err && errno == EAGAIN;
02ae29
+		if (again) {
02ae29
+			xusleep(250000);
02ae29
+			tries++;
02ae29
+		}
02ae29
+	} while (again && tries <= LOOPDEV_MAX_TRIES);
02ae29
+
02ae29
+	if (err) {
02ae29
 		int rc = -errno;
02ae29
 		DBG(CXT, ul_debugobj(lc, "LOOP_SET_BLOCK_SIZE failed: %m"));
02ae29
 		return rc;
02ae29
-- 
02ae29
2.34.1
02ae29