On 06/10/2015 11:11 AM, Stanislav Kholmanskikh wrote:
On systems employing the "lazy loop device removal",
kernel commit commit a1ecac3b0656a68259927c234e505804d33a7b83
("loop: Make explicit loop device destruction lazy"),
ioctl(LOOP_CLR_FD) may return 0 when the loop device is in use,
for example by udev.

Therefore, there is a chance that a quick attach/detach may not
give udev enough time to complete, like this:

open("file.img", O_RDWR)           = 4
ioctl(3, LOOP_SET_FD, 0x4)         = 0
close(3)                           = 0
close(4)                           = 0
open("/dev/loop0", O_RDONLY)       = 3
ioctl(3, LOOP_CLR_FD, 0)           = 0
close(3)                           = 0
open("/dev/loop0", O_RDWR)         = 3
open("file.img", O_RDWR)           = 4
ioctl(3, LOOP_SET_FD, 0x4)         = -1 EBUSY (Device or resource busy)

So let's wait until the kernel frees the loop device, i.e.
when ioctl(LOOP_CLR_FD) starts failing with ENXIO.

Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmansk...@oracle.com>

I tested it the attached test case (plus you need to remove 'static' next to attach_device and detach_device).

Without the patch it fails with:

[root@kholmanskikh test]# ./loop_test
loop_test 1 TBROK : tst_device.c:144: ioctl(/dev/loop0, LOOP_SET_FD, file.img) failed: EBUSY
loop_test    2  TBROK  :  tst_device.c:144: Remaining cases broken

#include "test.h"
#include "safe_macros.h"

char *TCID = "loop_test";
int TST_TOTAL = 1;

void attach_device(void (*cleanup_fn)(void),
		   const char *dev, const char *file);
void detach_device(void (*cleanup_fn)(void), const char *dev);

int main(void)
{
	int i, fd;

	tst_tmpdir();

	fd = SAFE_OPEN(NULL, "file.img", O_RDWR | O_CREAT, 0644);
	SAFE_FTRUNCATE(NULL, fd, 1024 * 1024UL);

	SAFE_CLOSE(NULL, fd);

	for (i = 0; i < 1000; i++) {
		attach_device(NULL, "/dev/loop0", "file.img");
		detach_device(NULL, "/dev/loop0");
	}

	tst_rmdir();

	tst_resm(TPASS, "Finished");
	tst_exit();
}
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to