Blob Blame History Raw
From a24905f03e90fb2f418bd5b8816378a99bb535b2 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 1 Mar 2022 12:07:07 +0100
Subject: loopdev: add retries on EAGAIN

* add limit to number of attempts for LOOP_SET_STATUS64

* use the same for LOOP_SET_BLOCK_SIZE ioctl

Addresses: https://github.com/util-linux/util-linux/issues/1582
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 lib/loopdev.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/lib/loopdev.c b/lib/loopdev.c
index 511f8a0d6..e8ccf6ae6 100644
--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -43,6 +43,9 @@
 #include "debug.h"
 #include "fileutils.h"
 
+
+#define LOOPDEV_MAX_TRIES	10
+
 /*
  * Debug stuff (based on include/debug.h)
  */
@@ -1450,7 +1453,7 @@ err:
  */
 int loopcxt_ioctl_status(struct loopdev_cxt *lc)
 {
-	int dev_fd, rc = -1, err, again;
+	int dev_fd, rc = -1, err, again, tries = 0;
 
 	errno = 0;
 	dev_fd = loopcxt_get_fd(lc);
@@ -1464,9 +1467,12 @@ int loopcxt_ioctl_status(struct loopdev_cxt *lc)
 	do {
 		err = ioctl(dev_fd, LOOP_SET_STATUS64, &lc->config.info);
 		again = err && errno == EAGAIN;
-		if (again)
+		if (again) {
 			xusleep(250000);
-	} while (again);
+			tries++;
+		}
+	} while (again && tries <= LOOPDEV_MAX_TRIES);
+
 	if (err) {
 		rc = -errno;
 		DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
@@ -1520,16 +1526,24 @@ int loopcxt_ioctl_dio(struct loopdev_cxt *lc, unsigned long use_dio)
 int loopcxt_ioctl_blocksize(struct loopdev_cxt *lc, uint64_t blocksize)
 {
 	int fd = loopcxt_get_fd(lc);
+	int err, again, tries = 0;
 
 	if (fd < 0)
 		return -EINVAL;
 
-	/* Kernels prior to v4.14 don't support this ioctl */
-	if (ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) blocksize) < 0) {
-		int rc = -errno;
-		DBG(CXT, ul_debugobj(lc, "LOOP_SET_BLOCK_SIZE failed: %m"));
-		return rc;
-	}
+	do {
+		/* Kernels prior to v4.14 don't support this ioctl */
+		err = ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) blocksize);
+		again = err && errno == EAGAIN;
+		if (again) {
+			xusleep(250000);
+			tries++;
+		} else if (err) {
+			int rc = -errno;
+			DBG(CXT, ul_debugobj(lc, "LOOP_SET_BLOCK_SIZE failed: %m"));
+			return rc;
+		}
+	} while (again && tries <= LOOPDEV_MAX_TRIES);
 
 	DBG(CXT, ul_debugobj(lc, "logical block size set"));
 	return 0;
-- 
2.37.1