From bfd084454dda2db6cc0690fafa94d4bc85a874aa Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 20 Oct 2021 09:55:33 +0200 Subject: [PATCH 1/3] loop-util: minor coding style updates As suggested here: https://github.com/systemd/systemd/pull/21044#pullrequestreview-783530343 --- src/shared/loop-util.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 072acc8c47..52c47e208e 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -615,8 +615,7 @@ static uint32_t loop_flags_mangle(uint32_t loop_flags) { if (r < 0 && r != -ENXIO) log_debug_errno(r, "Failed to parse $SYSTEMD_LOOP_DIRECT_IO, ignoring: %m"); - SET_FLAG(loop_flags, LO_FLAGS_DIRECT_IO, r != 0); /* Turn on LO_FLAGS_DIRECT_IO by default, unless explicitly configured to off. */ - return loop_flags; + return UPDATE_FLAG(loop_flags, LO_FLAGS_DIRECT_IO, r != 0); /* Turn on LO_FLAGS_DIRECT_IO by default, unless explicitly configured to off. */ } int loop_device_make( @@ -629,16 +628,13 @@ int loop_device_make( assert(fd >= 0); assert(ret); - assert(IN_SET(open_flags, O_RDWR, O_RDONLY)); - - loop_flags = loop_flags_mangle(loop_flags); return loop_device_make_internal( fd, open_flags, offset, size, - loop_flags, + loop_flags_mangle(loop_flags), ret); } From aa4d3aa3ef05f64729cce1dd87ce9f7a237ede1a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 20 Oct 2021 09:56:20 +0200 Subject: [PATCH 2/3] loop-util: add debug logging about O_RDWR vs. O_RDONLY + O_DIRECT mode Once we managed to open the file let's log what we wanted and what we got. --- src/shared/loop-util.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 52c47e208e..f2f4ab481d 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -646,6 +646,7 @@ int loop_device_make_by_path( int r, basic_flags, direct_flags, rdwr_flags; _cleanup_close_ int fd = -1; + bool direct = false; assert(path); assert(ret); @@ -666,6 +667,8 @@ int loop_device_make_by_path( fd = open(path, basic_flags|direct_flags|rdwr_flags); if (fd < 0 && direct_flags != 0) /* If we had O_DIRECT on, and things failed with that, let's immediately try again without */ fd = open(path, basic_flags|rdwr_flags); + else + direct = direct_flags != 0; if (fd < 0) { r = -errno; @@ -676,6 +679,8 @@ int loop_device_make_by_path( fd = open(path, basic_flags|direct_flags|O_RDONLY); if (fd < 0 && direct_flags != 0) /* as above */ fd = open(path, basic_flags|O_RDONLY); + else + direct = direct_flags != 0; if (fd < 0) return r; /* Propagate original error */ @@ -683,6 +688,13 @@ int loop_device_make_by_path( } else if (open_flags < 0) open_flags = O_RDWR; + log_debug("Opened '%s' in %s access mode%s, with O_DIRECT %s%s.", + path, + open_flags == O_RDWR ? "O_RDWR" : "O_RDONLY", + open_flags != rdwr_flags ? " (O_RDWR was requested but not allowed)" : "", + direct ? "enabled" : "disabled", + direct != (direct_flags != 0) ? " (O_DIRECT was requested but not supported)" : ""); + return loop_device_make(fd, open_flags, 0, 0, loop_flags, ret); } From 0193b93eb5d4c9bb760f0e2727b4e698c392a47f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 20 Oct 2021 09:57:16 +0200 Subject: [PATCH 3/3] loop-util: call loop_device_make_internal() at the right place The whole reason loop_device_make_internal() exists (as opposed to just loop_device_make()) is to avoid mangling the loop flags value/call getenv twice. Hence let's actually call it when we already mangled the flags value. --- src/shared/loop-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index f2f4ab481d..2da101b748 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -695,7 +695,7 @@ int loop_device_make_by_path( direct ? "enabled" : "disabled", direct != (direct_flags != 0) ? " (O_DIRECT was requested but not supported)" : ""); - return loop_device_make(fd, open_flags, 0, 0, loop_flags, ret); + return loop_device_make_internal(fd, open_flags, 0, 0, loop_flags, ret); } LoopDevice* loop_device_unref(LoopDevice *d) {