@@ -975,7 +975,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
975
975
loop_reread_partitions (lo , bdev );
976
976
977
977
/* 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).
979
979
*/
980
980
bdgrab (bdev );
981
981
return 0 ;
@@ -1025,39 +1025,22 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
1025
1025
return err ;
1026
1026
}
1027
1027
1028
- static int loop_clr_fd (struct loop_device * lo )
1028
+ static int __loop_clr_fd (struct loop_device * lo )
1029
1029
{
1030
1030
struct file * filp = lo -> lo_backing_file ;
1031
1031
gfp_t gfp = lo -> old_gfp_mask ;
1032
1032
struct block_device * bdev = lo -> lo_device ;
1033
1033
1034
- if (lo -> lo_state != Lo_bound )
1034
+ if (WARN_ON_ONCE ( lo -> lo_state != Lo_rundown ) )
1035
1035
return - ENXIO ;
1036
1036
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
-
1053
1037
if (filp == NULL )
1054
1038
return - EINVAL ;
1055
1039
1056
1040
/* freeze request queue during the transition */
1057
1041
blk_mq_freeze_queue (lo -> lo_queue );
1058
1042
1059
1043
spin_lock_irq (& lo -> lo_lock );
1060
- lo -> lo_state = Lo_rundown ;
1061
1044
lo -> lo_backing_file = NULL ;
1062
1045
spin_unlock_irq (& lo -> lo_lock );
1063
1046
@@ -1110,6 +1093,30 @@ static int loop_clr_fd(struct loop_device *lo)
1110
1093
return 0 ;
1111
1094
}
1112
1095
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
+
1113
1120
static int
1114
1121
loop_set_status (struct loop_device * lo , const struct loop_info64 * info )
1115
1122
{
@@ -1691,11 +1698,14 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
1691
1698
goto out_unlock ;
1692
1699
1693
1700
if (lo -> lo_flags & LO_FLAGS_AUTOCLEAR ) {
1701
+ if (lo -> lo_state != Lo_bound )
1702
+ goto out_unlock ;
1703
+ lo -> lo_state = Lo_rundown ;
1694
1704
/*
1695
1705
* In autoclear mode, stop the loop thread
1696
1706
* and remove configuration after last close.
1697
1707
*/
1698
- err = loop_clr_fd (lo );
1708
+ err = __loop_clr_fd (lo );
1699
1709
if (!err )
1700
1710
return ;
1701
1711
} else if (lo -> lo_state == Lo_bound ) {
0 commit comments