Skip to content

Commit a1ecac3

Browse files
Dave Chinneraxboe
authored andcommitted
loop: Make explicit loop device destruction lazy
xfstests has always had random failures of tests due to loop devices failing to be torn down and hence leaving filesytems that cannot be unmounted. This causes test runs to immediately stop. Over the past 6 or 7 years we've added hacks like explicit unmount -d commands for loop mounts, losetup -d after unmount -d fails, etc, but still the problems persist. Recently, the frequency of loop related failures increased again to the point that xfstests 259 will reliably fail with a stray loop device that was not torn down. That is despite the fact the test is above as simple as it gets - loop 5 or 6 times running mkfs.xfs with different paramters: lofile=$(losetup -f) losetup $lofile "$testfile" "$MKFS_XFS_PROG" -b size=512 $lofile >/dev/null || echo "mkfs failed!" sync losetup -d $lofile And losteup -d $lofile is failing with EBUSY on 1-3 of these loops every time the test is run. Turns out that blkid is running simultaneously with losetup -d, and so it sees an elevated reference count and returns EBUSY. But why is blkid running? It's obvious, isn't it? udev has decided to try and find out what is on the block device as a result of a creation notification. And it is racing with mkfs, so might still be scanning the device when mkfs finishes and we try to tear it down. So, make losetup -d force autoremove behaviour. That is, when the last reference goes away, tear down the device. xfstests wants it *gone*, not causing random teardown failures when we know that all the operations the tests have specifically run on the device have completed and are no longer referencing the loop device. Signed-off-by: Dave Chinner <dchinner@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 4453bc8 commit a1ecac3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

drivers/block/loop.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,21 @@ static int loop_clr_fd(struct loop_device *lo)
976976
if (lo->lo_state != Lo_bound)
977977
return -ENXIO;
978978

979-
if (lo->lo_refcnt > 1) /* we needed one fd for the ioctl */
980-
return -EBUSY;
979+
/*
980+
* If we've explicitly asked to tear down the loop device,
981+
* and it has an elevated reference count, set it for auto-teardown when
982+
* the last reference goes away. This stops $!~#$@ udev from
983+
* preventing teardown because it decided that it needs to run blkid on
984+
* the loopback device whenever they appear. xfstests is notorious for
985+
* failing tests because blkid via udev races with a losetup
986+
* <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
987+
* command to fail with EBUSY.
988+
*/
989+
if (lo->lo_refcnt > 1) {
990+
lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
991+
mutex_unlock(&lo->lo_ctl_mutex);
992+
return 0;
993+
}
981994

982995
if (filp == NULL)
983996
return -EINVAL;

0 commit comments

Comments
 (0)