@@ -1027,15 +1027,22 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
1027
1027
1028
1028
static int __loop_clr_fd (struct loop_device * lo )
1029
1029
{
1030
- struct file * filp = lo -> lo_backing_file ;
1030
+ struct file * filp = NULL ;
1031
1031
gfp_t gfp = lo -> old_gfp_mask ;
1032
1032
struct block_device * bdev = lo -> lo_device ;
1033
+ int err = 0 ;
1033
1034
1034
- if (WARN_ON_ONCE (lo -> lo_state != Lo_rundown ))
1035
- return - ENXIO ;
1035
+ mutex_lock (& loop_ctl_mutex );
1036
+ if (WARN_ON_ONCE (lo -> lo_state != Lo_rundown )) {
1037
+ err = - ENXIO ;
1038
+ goto out_unlock ;
1039
+ }
1036
1040
1037
- if (filp == NULL )
1038
- return - EINVAL ;
1041
+ filp = lo -> lo_backing_file ;
1042
+ if (filp == NULL ) {
1043
+ err = - EINVAL ;
1044
+ goto out_unlock ;
1045
+ }
1039
1046
1040
1047
/* freeze request queue during the transition */
1041
1048
blk_mq_freeze_queue (lo -> lo_queue );
@@ -1082,21 +1089,30 @@ static int __loop_clr_fd(struct loop_device *lo)
1082
1089
if (!part_shift )
1083
1090
lo -> lo_disk -> flags |= GENHD_FL_NO_PART_SCAN ;
1084
1091
loop_unprepare_queue (lo );
1092
+ out_unlock :
1085
1093
mutex_unlock (& loop_ctl_mutex );
1086
1094
/*
1087
1095
* Need not hold loop_ctl_mutex to fput backing file.
1088
1096
* Calling fput holding loop_ctl_mutex triggers a circular
1089
1097
* lock dependency possibility warning as fput can take
1090
1098
* bd_mutex which is usually taken before loop_ctl_mutex.
1091
1099
*/
1092
- fput (filp );
1093
- return 0 ;
1100
+ if (filp )
1101
+ fput (filp );
1102
+ return err ;
1094
1103
}
1095
1104
1096
1105
static int loop_clr_fd (struct loop_device * lo )
1097
1106
{
1098
- if (lo -> lo_state != Lo_bound )
1107
+ int err ;
1108
+
1109
+ err = mutex_lock_killable_nested (& loop_ctl_mutex , 1 );
1110
+ if (err )
1111
+ return err ;
1112
+ if (lo -> lo_state != Lo_bound ) {
1113
+ mutex_unlock (& loop_ctl_mutex );
1099
1114
return - ENXIO ;
1115
+ }
1100
1116
/*
1101
1117
* If we've explicitly asked to tear down the loop device,
1102
1118
* and it has an elevated reference count, set it for auto-teardown when
@@ -1113,6 +1129,7 @@ static int loop_clr_fd(struct loop_device *lo)
1113
1129
return 0 ;
1114
1130
}
1115
1131
lo -> lo_state = Lo_rundown ;
1132
+ mutex_unlock (& loop_ctl_mutex );
1116
1133
1117
1134
return __loop_clr_fd (lo );
1118
1135
}
@@ -1447,14 +1464,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
1447
1464
mutex_unlock (& loop_ctl_mutex );
1448
1465
break ;
1449
1466
case LOOP_CLR_FD :
1450
- err = mutex_lock_killable_nested (& loop_ctl_mutex , 1 );
1451
- if (err )
1452
- return err ;
1453
- /* loop_clr_fd would have unlocked loop_ctl_mutex on success */
1454
- err = loop_clr_fd (lo );
1455
- if (err )
1456
- mutex_unlock (& loop_ctl_mutex );
1457
- break ;
1467
+ return loop_clr_fd (lo );
1458
1468
case LOOP_SET_STATUS :
1459
1469
err = - EPERM ;
1460
1470
if ((mode & FMODE_WRITE ) || capable (CAP_SYS_ADMIN )) {
@@ -1690,7 +1700,6 @@ static int lo_open(struct block_device *bdev, fmode_t mode)
1690
1700
static void lo_release (struct gendisk * disk , fmode_t mode )
1691
1701
{
1692
1702
struct loop_device * lo ;
1693
- int err ;
1694
1703
1695
1704
mutex_lock (& loop_ctl_mutex );
1696
1705
lo = disk -> private_data ;
@@ -1701,13 +1710,13 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
1701
1710
if (lo -> lo_state != Lo_bound )
1702
1711
goto out_unlock ;
1703
1712
lo -> lo_state = Lo_rundown ;
1713
+ mutex_unlock (& loop_ctl_mutex );
1704
1714
/*
1705
1715
* In autoclear mode, stop the loop thread
1706
1716
* and remove configuration after last close.
1707
1717
*/
1708
- err = __loop_clr_fd (lo );
1709
- if (!err )
1710
- return ;
1718
+ __loop_clr_fd (lo );
1719
+ return ;
1711
1720
} else if (lo -> lo_state == Lo_bound ) {
1712
1721
/*
1713
1722
* Otherwise keep thread (if running) and config,
0 commit comments