@@ -1146,20 +1146,12 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
1146
1146
return error ;
1147
1147
}
1148
1148
1149
- static void __loop_clr_fd (struct loop_device * lo , bool release )
1149
+ static void __loop_clr_fd (struct loop_device * lo )
1150
1150
{
1151
1151
struct queue_limits lim ;
1152
1152
struct file * filp ;
1153
1153
gfp_t gfp = lo -> old_gfp_mask ;
1154
1154
1155
- /*
1156
- * Freeze the request queue when unbinding on a live file descriptor and
1157
- * thus an open device. When called from ->release we are guaranteed
1158
- * that there is no I/O in progress already.
1159
- */
1160
- if (!release )
1161
- blk_mq_freeze_queue (lo -> lo_queue );
1162
-
1163
1155
spin_lock_irq (& lo -> lo_lock );
1164
1156
filp = lo -> lo_backing_file ;
1165
1157
lo -> lo_backing_file = NULL ;
@@ -1184,8 +1176,6 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
1184
1176
mapping_set_gfp_mask (filp -> f_mapping , gfp );
1185
1177
/* This is safe: open() is still holding a reference. */
1186
1178
module_put (THIS_MODULE );
1187
- if (!release )
1188
- blk_mq_unfreeze_queue (lo -> lo_queue );
1189
1179
1190
1180
disk_force_media_change (lo -> lo_disk );
1191
1181
@@ -1200,11 +1190,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
1200
1190
* must be at least one and it can only become zero when the
1201
1191
* current holder is released.
1202
1192
*/
1203
- if (!release )
1204
- mutex_lock (& lo -> lo_disk -> open_mutex );
1205
1193
err = bdev_disk_changed (lo -> lo_disk , false);
1206
- if (!release )
1207
- mutex_unlock (& lo -> lo_disk -> open_mutex );
1208
1194
if (err )
1209
1195
pr_warn ("%s: partition scan of loop%d failed (rc=%d)\n" ,
1210
1196
__func__ , lo -> lo_number , err );
@@ -1253,24 +1239,16 @@ static int loop_clr_fd(struct loop_device *lo)
1253
1239
return - ENXIO ;
1254
1240
}
1255
1241
/*
1256
- * If we've explicitly asked to tear down the loop device,
1257
- * and it has an elevated reference count, set it for auto-teardown when
1258
- * the last reference goes away. This stops $!~#$@ udev from
1259
- * preventing teardown because it decided that it needs to run blkid on
1260
- * the loopback device whenever they appear. xfstests is notorious for
1261
- * failing tests because blkid via udev races with a losetup
1262
- * <dev>/do something like mkfs/losetup -d <dev> causing the losetup -d
1263
- * command to fail with EBUSY.
1242
+ * Mark the device for removing the backing device on last close.
1243
+ * If we are the only opener, also switch the state to roundown here to
1244
+ * prevent new openers from coming in.
1264
1245
*/
1265
- if (disk_openers (lo -> lo_disk ) > 1 ) {
1266
- lo -> lo_flags |= LO_FLAGS_AUTOCLEAR ;
1267
- loop_global_unlock (lo , true);
1268
- return 0 ;
1269
- }
1270
- lo -> lo_state = Lo_rundown ;
1246
+
1247
+ lo -> lo_flags |= LO_FLAGS_AUTOCLEAR ;
1248
+ if (disk_openers (lo -> lo_disk ) == 1 )
1249
+ lo -> lo_state = Lo_rundown ;
1271
1250
loop_global_unlock (lo , true);
1272
1251
1273
- __loop_clr_fd (lo , false);
1274
1252
return 0 ;
1275
1253
}
1276
1254
@@ -1737,25 +1715,43 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
1737
1715
}
1738
1716
#endif
1739
1717
1718
+ static int lo_open (struct gendisk * disk , blk_mode_t mode )
1719
+ {
1720
+ struct loop_device * lo = disk -> private_data ;
1721
+ int err ;
1722
+
1723
+ err = mutex_lock_killable (& lo -> lo_mutex );
1724
+ if (err )
1725
+ return err ;
1726
+
1727
+ if (lo -> lo_state == Lo_deleting || lo -> lo_state == Lo_rundown )
1728
+ err = - ENXIO ;
1729
+ mutex_unlock (& lo -> lo_mutex );
1730
+ return err ;
1731
+ }
1732
+
1740
1733
static void lo_release (struct gendisk * disk )
1741
1734
{
1742
1735
struct loop_device * lo = disk -> private_data ;
1736
+ bool need_clear = false;
1743
1737
1744
1738
if (disk_openers (disk ) > 0 )
1745
1739
return ;
1740
+ /*
1741
+ * Clear the backing device information if this is the last close of
1742
+ * a device that's been marked for auto clear, or on which LOOP_CLR_FD
1743
+ * has been called.
1744
+ */
1746
1745
1747
1746
mutex_lock (& lo -> lo_mutex );
1748
- if (lo -> lo_state == Lo_bound && (lo -> lo_flags & LO_FLAGS_AUTOCLEAR )) {
1747
+ if (lo -> lo_state == Lo_bound && (lo -> lo_flags & LO_FLAGS_AUTOCLEAR ))
1749
1748
lo -> lo_state = Lo_rundown ;
1750
- mutex_unlock (& lo -> lo_mutex );
1751
- /*
1752
- * In autoclear mode, stop the loop thread
1753
- * and remove configuration after last close.
1754
- */
1755
- __loop_clr_fd (lo , true);
1756
- return ;
1757
- }
1749
+
1750
+ need_clear = (lo -> lo_state == Lo_rundown );
1758
1751
mutex_unlock (& lo -> lo_mutex );
1752
+
1753
+ if (need_clear )
1754
+ __loop_clr_fd (lo );
1759
1755
}
1760
1756
1761
1757
static void lo_free_disk (struct gendisk * disk )
@@ -1772,6 +1768,7 @@ static void lo_free_disk(struct gendisk *disk)
1772
1768
1773
1769
static const struct block_device_operations lo_fops = {
1774
1770
.owner = THIS_MODULE ,
1771
+ .open = lo_open ,
1775
1772
.release = lo_release ,
1776
1773
.ioctl = lo_ioctl ,
1777
1774
#ifdef CONFIG_COMPAT
0 commit comments