@@ -68,7 +68,6 @@ struct loop_device {
6868 struct list_head idle_worker_list ;
6969 struct rb_root worker_tree ;
7070 struct timer_list timer ;
71- bool use_dio ;
7271 bool sysfs_inited ;
7372
7473 struct request_queue * lo_queue ;
@@ -196,32 +195,30 @@ static bool lo_can_use_dio(struct loop_device *lo)
196195 return true;
197196}
198197
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+ */
199206static inline void loop_update_dio (struct loop_device * lo )
200207{
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 ;
203209
204210 lockdep_assert_held (& lo -> lo_mutex );
205211 WARN_ON_ONCE (lo -> lo_state == Lo_bound &&
206212 lo -> lo_queue -> mq_freeze_depth == 0 );
207213
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 )
222215 lo -> lo_flags |= LO_FLAGS_DIRECT_IO ;
223- else
216+ if (( lo -> lo_flags & LO_FLAGS_DIRECT_IO ) && ! lo_can_use_dio ( lo ))
224217 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 );
225222}
226223
227224/**
@@ -1089,7 +1086,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
10891086 disk_force_media_change (lo -> lo_disk );
10901087 set_disk_ro (lo -> lo_disk , (lo -> lo_flags & LO_FLAGS_READ_ONLY ) != 0 );
10911088
1092- lo -> use_dio = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
10931089 lo -> lo_device = bdev ;
10941090 lo -> lo_backing_file = file ;
10951091 lo -> old_gfp_mask = mapping_gfp_mask (mapping );
@@ -1454,7 +1450,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
14541450
14551451 if (lo -> lo_state != Lo_bound )
14561452 return - ENXIO ;
1457- if (use_dio == lo -> use_dio )
1453+ if (use_dio == !!( lo -> lo_flags & LO_FLAGS_DIRECT_IO ) )
14581454 return 0 ;
14591455
14601456 if (use_dio ) {
@@ -1465,7 +1461,6 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
14651461 }
14661462
14671463 blk_mq_freeze_queue (lo -> lo_queue );
1468- lo -> use_dio = use_dio ;
14691464 if (use_dio )
14701465 lo -> lo_flags |= LO_FLAGS_DIRECT_IO ;
14711466 else
@@ -1876,7 +1871,7 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
18761871 cmd -> use_aio = false;
18771872 break ;
18781873 default :
1879- cmd -> use_aio = lo -> use_dio ;
1874+ cmd -> use_aio = lo -> lo_flags & LO_FLAGS_DIRECT_IO ;
18801875 break ;
18811876 }
18821877
0 commit comments