Skip to content

Commit a2505b7

Browse files
jankaraaxboe
authored andcommitted
loop: Split setting of lo_state from loop_clr_fd
Move setting of lo_state to Lo_rundown out into the callers. That will allow us to unlock loop_ctl_mutex while the loop device is protected from other changes by its special state. Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent a131654 commit a2505b7

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

drivers/block/loop.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
975975
loop_reread_partitions(lo, bdev);
976976

977977
/* Grab the block_device to prevent its destruction after we
978-
* put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
978+
* put /dev/loopXX inode. Later in __loop_clr_fd() we bdput(bdev).
979979
*/
980980
bdgrab(bdev);
981981
return 0;
@@ -1025,39 +1025,22 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
10251025
return err;
10261026
}
10271027

1028-
static int loop_clr_fd(struct loop_device *lo)
1028+
static int __loop_clr_fd(struct loop_device *lo)
10291029
{
10301030
struct file *filp = lo->lo_backing_file;
10311031
gfp_t gfp = lo->old_gfp_mask;
10321032
struct block_device *bdev = lo->lo_device;
10331033

1034-
if (lo->lo_state != Lo_bound)
1034+
if (WARN_ON_ONCE(lo->lo_state != Lo_rundown))
10351035
return -ENXIO;
10361036

1037-
/*
1038-
* If we've explicitly asked to tear down the loop device,
1039-
* and it has an elevated reference count, set it for auto-teardown when
1040-
* the last reference goes away. This stops $!~#$@ udev from
1041-
* preventing teardown because it decided that it needs to run blkid on
1042-
* the loopback device whenever they appear. xfstests is notorious for
1043-
* failing tests because blkid via udev races with a losetup
1044-
* <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1045-
* command to fail with EBUSY.
1046-
*/
1047-
if (atomic_read(&lo->lo_refcnt) > 1) {
1048-
lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1049-
mutex_unlock(&loop_ctl_mutex);
1050-
return 0;
1051-
}
1052-
10531037
if (filp == NULL)
10541038
return -EINVAL;
10551039

10561040
/* freeze request queue during the transition */
10571041
blk_mq_freeze_queue(lo->lo_queue);
10581042

10591043
spin_lock_irq(&lo->lo_lock);
1060-
lo->lo_state = Lo_rundown;
10611044
lo->lo_backing_file = NULL;
10621045
spin_unlock_irq(&lo->lo_lock);
10631046

@@ -1110,6 +1093,30 @@ static int loop_clr_fd(struct loop_device *lo)
11101093
return 0;
11111094
}
11121095

1096+
static int loop_clr_fd(struct loop_device *lo)
1097+
{
1098+
if (lo->lo_state != Lo_bound)
1099+
return -ENXIO;
1100+
/*
1101+
* If we've explicitly asked to tear down the loop device,
1102+
* and it has an elevated reference count, set it for auto-teardown when
1103+
* the last reference goes away. This stops $!~#$@ udev from
1104+
* preventing teardown because it decided that it needs to run blkid on
1105+
* the loopback device whenever they appear. xfstests is notorious for
1106+
* failing tests because blkid via udev races with a losetup
1107+
* <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1108+
* command to fail with EBUSY.
1109+
*/
1110+
if (atomic_read(&lo->lo_refcnt) > 1) {
1111+
lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1112+
mutex_unlock(&loop_ctl_mutex);
1113+
return 0;
1114+
}
1115+
lo->lo_state = Lo_rundown;
1116+
1117+
return __loop_clr_fd(lo);
1118+
}
1119+
11131120
static int
11141121
loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
11151122
{
@@ -1691,11 +1698,14 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
16911698
goto out_unlock;
16921699

16931700
if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
1701+
if (lo->lo_state != Lo_bound)
1702+
goto out_unlock;
1703+
lo->lo_state = Lo_rundown;
16941704
/*
16951705
* In autoclear mode, stop the loop thread
16961706
* and remove configuration after last close.
16971707
*/
1698-
err = loop_clr_fd(lo);
1708+
err = __loop_clr_fd(lo);
16991709
if (!err)
17001710
return;
17011711
} else if (lo->lo_state == Lo_bound) {

0 commit comments

Comments
 (0)