@@ -68,7 +68,6 @@ struct loop_device {
68
68
struct list_head idle_worker_list ;
69
69
struct rb_root worker_tree ;
70
70
struct timer_list timer ;
71
- bool use_dio ;
72
71
bool sysfs_inited ;
73
72
74
73
struct request_queue * lo_queue ;
@@ -196,32 +195,30 @@ static bool lo_can_use_dio(struct loop_device *lo)
196
195
return true;
197
196
}
198
197
198
+ /*
199
+ * Direct I/O can be enabled either by using an O_DIRECT file descriptor, or by
200
+ * passing in the LO_FLAGS_DIRECT_IO flag from userspace. It will be silently
201
+ * disabled when the device block size is too small or the offset is unaligned.
202
+ *
203
+ * loop_get_status will always report the effective LO_FLAGS_DIRECT_IO flag and
204
+ * not the originally passed in one.
205
+ */
199
206
static inline void loop_update_dio (struct loop_device * lo )
200
207
{
201
- bool dio = lo -> use_dio || (lo -> lo_backing_file -> f_flags & O_DIRECT );
202
- bool use_dio = dio && lo_can_use_dio (lo );
208
+ bool dio_in_use = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
203
209
204
210
lockdep_assert_held (& lo -> lo_mutex );
205
211
WARN_ON_ONCE (lo -> lo_state == Lo_bound &&
206
212
lo -> lo_queue -> mq_freeze_depth == 0 );
207
213
208
- if (lo -> use_dio == use_dio )
209
- return ;
210
-
211
- /* flush dirty pages before starting to use direct I/O */
212
- if (use_dio )
213
- vfs_fsync (lo -> lo_backing_file , 0 );
214
-
215
- /*
216
- * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
217
- * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
218
- * will get updated by ioctl(LOOP_GET_STATUS)
219
- */
220
- lo -> use_dio = use_dio ;
221
- if (use_dio )
214
+ if (lo -> lo_backing_file -> f_flags & O_DIRECT )
222
215
lo -> lo_flags |= LO_FLAGS_DIRECT_IO ;
223
- else
216
+ if (( lo -> lo_flags & LO_FLAGS_DIRECT_IO ) && ! lo_can_use_dio ( lo ))
224
217
lo -> lo_flags &= ~LO_FLAGS_DIRECT_IO ;
218
+
219
+ /* flush dirty pages before starting to issue direct I/O */
220
+ if ((lo -> lo_flags & LO_FLAGS_DIRECT_IO ) && !dio_in_use )
221
+ vfs_fsync (lo -> lo_backing_file , 0 );
225
222
}
226
223
227
224
/**
@@ -1089,7 +1086,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
1089
1086
disk_force_media_change (lo -> lo_disk );
1090
1087
set_disk_ro (lo -> lo_disk , (lo -> lo_flags & LO_FLAGS_READ_ONLY ) != 0 );
1091
1088
1092
- lo -> use_dio = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
1093
1089
lo -> lo_device = bdev ;
1094
1090
lo -> lo_backing_file = file ;
1095
1091
lo -> old_gfp_mask = mapping_gfp_mask (mapping );
@@ -1454,7 +1450,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1454
1450
1455
1451
if (lo -> lo_state != Lo_bound )
1456
1452
return - ENXIO ;
1457
- if (use_dio == lo -> use_dio )
1453
+ if (use_dio == !!( lo -> lo_flags & LO_FLAGS_DIRECT_IO ) )
1458
1454
return 0 ;
1459
1455
1460
1456
if (use_dio ) {
@@ -1465,7 +1461,6 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
1465
1461
}
1466
1462
1467
1463
blk_mq_freeze_queue (lo -> lo_queue );
1468
- lo -> use_dio = use_dio ;
1469
1464
if (use_dio )
1470
1465
lo -> lo_flags |= LO_FLAGS_DIRECT_IO ;
1471
1466
else
@@ -1876,7 +1871,7 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
1876
1871
cmd -> use_aio = false;
1877
1872
break ;
1878
1873
default :
1879
- cmd -> use_aio = lo -> use_dio ;
1874
+ cmd -> use_aio = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
1880
1875
break ;
1881
1876
}
1882
1877
0 commit comments