Skip to contentSkip to conversation

common/rc: destroy loop dev before fallback recreation

PATCH In review Brian Foster 3 participants 3 messages Sep 26, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 common/rc: destroy loop dev before fallback recreation

When running fstests on an s390x box I observed failure to unmount filesystem errors due to stale loop devices being left around. This root caused down to generic/361 leaving around an attached loop0 device. On further inspection, the test actually created two loop devices (loop0 and loop1), and executed on and cleaned up the latter.

The origin of the former appears to be that the initial losetup command in _create_loop_device() fails due to $dio_args in this environment, but still creates the loop device. For example:

 # losetup --direct-io=on -f --show /mnt/scratch/fs.img
 /dev/loop0
 losetup: /dev/loop0: set direct io failed: Invalid argument
 # losetup -a
 /dev/loop0: [64771]:131 (/mnt/scratch/fs.img)

The helper then goes on to create loop1, but it or the test never deals with loop0. To avoid this problem, detach any old loop device if one was set up before the fallback losetup command.

---

v2:
- Add comment to document wonky losetup behavior.
v1: https://lore.kernel.org/fstests/[email protected]/

 common/rc | 3 +++
 1 file changed, 3 insertions(+)
common/rc +3 -0
index 81587dad..1ec84263 100644
@@ -4596,6 +4596,9 @@ _create_loop_device()
 	# size to the directio alignment of the underlying fs, so if we want to
 	# use our own sector size, we need to specify that at creation time.
 	if ! dev="$(losetup $dio_args $args -f --show $file 2>/dev/null)"; then
+		# losetup can return error for certain configuration settings
+		# but still create a loop device.
+		test -n "$dev" && losetup -d "$dev" > /dev/null 2>&1
 		dev="$(losetup $args -f --show $file)" || \
 			_fail "Cannot assign $file to a loop device ($args)"
 	fi
Signature · 1 line
2.51.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 39 lines · Brian Foster

When running fstests on an s390x box I observed failure to unmount filesystem errors due to stale loop devices being left around. This root caused down to generic/361 leaving around an attached loop0 device. On further inspection, the test actually created two loop devices (loop0 and loop1), and executed on and cleaned up the latter.

The origin of the former appears to be that the initial losetup command in _create_loop_device() fails due to $dio_args in this environment, but still creates the loop device. For example:

 # losetup --direct-io=on -f --show /mnt/scratch/fs.img
 /dev/loop0
 losetup: /dev/loop0: set direct io failed: Invalid argument
 # losetup -a
 /dev/loop0: [64771]:131 (/mnt/scratch/fs.img)

The helper then goes on to create loop1, but it or the test never deals with loop0. To avoid this problem, detach any old loop device if one was set up before the fallback losetup command.

---

v2:
- Add comment to document wonky losetup behavior.
v1: https://lore.kernel.org/fstests/[email protected]/

 common/rc | 3 +++
 1 file changed, 3 insertions(+)
common/rc +0 -0
index 81587dad..1ec84263 100644

common/rc _create_loop_device() · line 4601

Quoted 6 lines
common/rc +3 -0
@@ -4596,6 +4596,9 @@ _create_loop_device()
 	# size to the directio alignment of the underlying fs, so if we want to
 	# use our own sector size, we need to specify that at creation time.
 	if ! dev="$(losetup $dio_args $args -f --show $file 2>/dev/null)"; then
+		# losetup can return error for certain configuration settings
+		# but still create a loop device.
+		test -n "$dev" && losetup -d "$dev" > /dev/null 2>&1

I'm not sure if "-b" is better than "-n", anyway "-n" makes sense too.

Quoted 7 lines
common/rc +0 -0
 		dev="$(losetup $args -f --show $file)" || \
 			_fail "Cannot assign $file to a loop device ($args)"
 	fi
Signature · 1 line
2.51.0
Reviewed-by
#2 lore raw
Quoted 35 lines · Zorro Lang
Quoted 34 lines · Brian Foster

When running fstests on an s390x box I observed failure to unmount filesystem errors due to stale loop devices being left around. This root caused down to generic/361 leaving around an attached loop0 device. On further inspection, the test actually created two loop devices (loop0 and loop1), and executed on and cleaned up the latter.

The origin of the former appears to be that the initial losetup command in _create_loop_device() fails due to $dio_args in this environment, but still creates the loop device. For example:

 # losetup --direct-io=on -f --show /mnt/scratch/fs.img
 /dev/loop0
 losetup: /dev/loop0: set direct io failed: Invalid argument
 # losetup -a
 /dev/loop0: [64771]:131 (/mnt/scratch/fs.img)

The helper then goes on to create loop1, but it or the test never deals with loop0. To avoid this problem, detach any old loop device if one was set up before the fallback losetup command.

---

v2:
- Add comment to document wonky losetup behavior.
v1: https://lore.kernel.org/fstests/[email protected]/

 common/rc | 3 +++
 1 file changed, 3 insertions(+)
Quoted 13 lines

common/rc _create_loop_device() · line 4601

Quoted 11 lines
common/rc +3 -0
index 81587dad..1ec84263 100644
@@ -4596,6 +4596,9 @@ _create_loop_device()
 	# size to the directio alignment of the underlying fs, so if we want to
 	# use our own sector size, we need to specify that at creation time.
 	if ! dev="$(losetup $dio_args $args -f --show $file 2>/dev/null)"; then
+		# losetup can return error for certain configuration settings
+		# but still create a loop device.
+		test -n "$dev" && losetup -d "$dev" > /dev/null 2>&1

I'm not sure if "-b" is better than "-n", anyway "-n" makes sense too.

I doubt it makes much difference, as long as nobody races us to delete and recreate the same loopdev.

Thanks for adding the comments,

--D

Quoted 10 lines
Quoted 7 lines
common/rc +0 -0
 		dev="$(losetup $args -f --show $file)" || \
 			_fail "Cannot assign $file to a loop device ($args)"
 	fi
Signature · 1 line
2.51.0