From: Yu Watanabe Date: Thu, 8 Sep 2022 20:04:08 +0000 (+0900) Subject: loop-util: always check if backing file is not attached yet X-Git-Tag: v252-rc1~219^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53274fdb59ea2c68253cb143791de1aea5168183;p=thirdparty%2Fsystemd.git loop-util: always check if backing file is not attached yet An image file without partition table may be attached. --- diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 69f35951798..1cc3f2642eb 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -264,6 +264,14 @@ static int loop_configure( if (lock_fd < 0) return lock_fd; + /* Let's see if backing file is really unattached. Someone may already attach a backing file without + * taking BSD lock. */ + r = loop_is_bound(fd); + if (r < 0) + return r; + if (r > 0) + return -EBUSY; + /* Let's see if the device is really detached, i.e. currently has no associated partition block * devices. On various kernels (such as 5.8) it is possible to have a loopback block device that * superficially is detached but still has partition block devices associated for it. Let's then @@ -273,14 +281,8 @@ static int loop_configure( if (r < 0) return r; if (r > 0) { - r = loop_is_bound(fd); - if (r < 0) - return r; - if (r > 0) - return -EBUSY; - - /* Unbound but has children? Remove all partitions, and report this to the caller, to try - * again, and count this as an attempt. */ + /* Remove all partitions, and report this to the caller, to try again, and count this as + * an attempt. */ r = block_device_remove_all_partitions(dev, fd); if (r < 0)