From: Stanislav Brabec Date: Wed, 17 Aug 2016 10:54:40 +0000 (+0200) Subject: losetup: Prevent AUTOCLEAR detach race X-Git-Tag: v2.29-rc1~107 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f27d989c67eb0d85b3bfc54807e626ff16e71f56;p=thirdparty%2Futil-linux.git losetup: Prevent AUTOCLEAR detach race Kernel needs some time to delete a device after losetup --detach. If the losetup --find --nooverlay is called just after losetup --delete, it can sometimes attempt to recycle the device that is just being released. To prevent this race, clear the AUTOCLEAR flag of the device. [kzak@redhat.com: - rebase to the new version of the code] Signed-off-by: Stanislav Brabec Signed-off-by: Karel Zak --- diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c index ba1363de3b..606789c427 100644 --- a/sys-utils/losetup.c +++ b/sys-utils/losetup.c @@ -481,6 +481,12 @@ static int create_loop(struct loopdev_cxt *lc, loopcxt_deinit(lc); errx(EXIT_FAILURE, _("%s: overlapping encrypted loop device exists"), file); } + + lc->info.lo_flags &= !LO_FLAGS_AUTOCLEAR; + if (loopcxt_set_status(lc)) { + loopcxt_deinit(lc); + errx(EXIT_FAILURE, _("%s: failed to re-use loop device"), file); + } return 0; /* success, re-use */ } default: /* error */