[PATCH 1/1] loop device: report mount failure in case of set_loop failure

Thomas De Schampheleire patrickdepinguin at gmail.com
Mon May 18 12:03:11 UTC 2020


From: Philippe Belet <philippe.belet at nokia.com>

When mounting, in parallel, multiple loop devices (squashfs for the
submitter's case), the following behavior can be observed:

stat64(/path/to/image, {st_mode=S_IFREG|0644, st_size=4096, ...}) = 0
openat(AT_FDCWD, /path/to/image, O_RDWR|O_LARGEFILE) = 3
openat(AT_FDCWD, /dev/loop-control, O_RDWR|O_LARGEFILE|O_CLOEXEC) = 4
ioctl(4, LOOP_CTL_GET_FREE)             = 12
close(4)                                = 0
openat(AT_FDCWD, /dev/loop12, O_RDWR|O_LARGEFILE) = 4
ioctl(4, LOOP_GET_STATUS64, {lo_offset=0, lo_number=12, lo_flags=LO_FLAGS_AUTOCLEAR, lo_file_name=/path/to/image, ...}) = 0
close(4)                                = 0
close(3)                                = 0
write(2, "mount: can't setup loop device\n", 31mount: can't setup loop device
) = 31
exit_group(0)                           = ?
+++ exited with 0 +++

The ioctl LOOP_CTL_GET_FREE has resulted in the same result for
a competing mount process. The subsequent ioctl LOOP_GET_STATUS64
fails, having succeeded for the competing mount process. Next we
see a series of other system calls.

The mount code checks the errno value of the last close()
syscall, resulting in exit code 0 for the process, despite
mount failure.

Signed-off-by: Philippe Belet <philippe.belet at nokia.com>
Signed-off-by: Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
---
 libbb/loop.c       | 2 +-
 util-linux/mount.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libbb/loop.c b/libbb/loop.c
index ada0c7638..23799e633 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -204,7 +204,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse
 				}
 			}
 		} else {
-			rc = -1;
+			rc = errno ? -errno : -EBUSY;
 		}
 		if (rc != 0) {
 			close(dfd);
diff --git a/util-linux/mount.c b/util-linux/mount.c
index 84c85c057..93b5ebac2 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -2046,11 +2046,11 @@ static int singlemount(struct mntent *mp, int ignore_busy)
 						| BB_LO_FLAGS_AUTOCLEAR
 			);
 			if (loopfd < 0) {
-				if (errno == EPERM || errno == EACCES)
+				if (loopfd == -EPERM || loopfd == -EACCES)
 					bb_simple_error_msg(bb_msg_perm_denied_are_you_root);
 				else
 					bb_simple_perror_msg("can't setup loop device");
-				return errno;
+				return loopfd;
 			}
 
 		// Autodetect bind mounts
-- 
2.26.2



More information about the busybox mailing list