History

block: loop: prepare for supporing direct IO

This patches provides one interface for enabling direct IO from user space: - userspace(such as losetup) can pass 'file' which is opened/fcntl as O_DIRECT Also __loop_update_dio() is introduced to check if direct I/O can be used on current loop setting. The last big change is to introduce LO_FLAGS_DIRECT_IO flag for userspace to know if direct IO is used to access backing file.

Ming Lei committed 11 years ago

Commit 2e5ab5f1 parent e03a3d73 files · +79 −0Browse files

Unified · Split

3 files
  1. drivers/block/loop.c +77 −0
  2. drivers/block/loop.h +1 −0
  3. include/uapi/linux/loop.h +1 −0
drivers/block/loop.c+77−0
@@ -164,6 +164,62 @@ static loff_t get_loop_size(struct loop_device *lo, struct file *file) · 163 unmodified lines
164164 return get_size(lo->lo_offset, lo->lo_sizelimit, file);
165165 }
166166
167+static void __loop_update_dio(struct loop_device *lo, bool dio)
168+{
169+ struct file *file = lo->lo_backing_file;
170+ struct address_space *mapping = file->f_mapping;
171+ struct inode *inode = mapping->host;
172+ unsigned short sb_bsize = 0;
173+ unsigned dio_align = 0;
174+ bool use_dio;
175+
176+ if (inode->i_sb->s_bdev) {
177+ sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);
178+ dio_align = sb_bsize - 1;
179+ }
180+
181+ /*
182+ * We support direct I/O only if lo_offset is aligned with the
183+ * logical I/O size of backing device, and the logical block
184+ * size of loop is bigger than the backing device's and the loop
185+ * needn't transform transfer.
186+ *
187+ * TODO: the above condition may be loosed in the future, and
188+ * direct I/O may be switched runtime at that time because most
189+ * of requests in sane appplications should be PAGE_SIZE algined
190+ */
191+ if (dio) {
192+ if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
193+ !(lo->lo_offset & dio_align) &&
194+ mapping->a_ops->direct_IO &&
195+ !lo->transfer)
196+ use_dio = true;
197+ else
198+ use_dio = false;
199+ } else {
200+ use_dio = false;
201+ }
202+
203+ if (lo->use_dio == use_dio)
204+ return;
205+
206+ /* flush dirty pages before changing direct IO */
207+ vfs_fsync(file, 0);
208+
209+ /*
210+ * The flag of LO_FLAGS_DIRECT_IO is handled similarly with
211+ * LO_FLAGS_READ_ONLY, both are set from kernel, and losetup
212+ * will get updated by ioctl(LOOP_GET_STATUS)
213+ */
214+ blk_mq_freeze_queue(lo->lo_queue);
215+ lo->use_dio = use_dio;
216+ if (use_dio)
217+ lo->lo_flags |= LO_FLAGS_DIRECT_IO;
218+ else
219+ lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
220+ blk_mq_unfreeze_queue(lo->lo_queue);
221+}
222+
167223 static int
168224 figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
169225 {
@@ -421,6 +477,12 @@ struct switch_request { · 251 unmodified lines
421477 struct completion wait;
422478 };
423479
480+static inline void loop_update_dio(struct loop_device *lo)
481+{
482+ __loop_update_dio(lo, io_is_direct(lo->lo_backing_file) |
483+ lo->use_dio);
484+}
485+
424486 /*
425487 * Do the actual switch; called from the BIO completion routine
426488 */
@@ -441,6 +503,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p) · 14 unmodified lines
441503 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
442504 lo->old_gfp_mask = mapping_gfp_mask(mapping);
443505 mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
506+ loop_update_dio(lo);
444507 }
445508
446509 /*
@@ -627,11 +690,19 @@ static ssize_t loop_attr_partscan_show(struct loop_device *lo, char *buf) · 180 unmodified lines
627690 return sprintf(buf, "%s\n", partscan ? "1" : "0");
628691 }
629692
693+static ssize_t loop_attr_dio_show(struct loop_device *lo, char *buf)
694+{
695+ int dio = (lo->lo_flags & LO_FLAGS_DIRECT_IO);
696+
697+ return sprintf(buf, "%s\n", dio ? "1" : "0");
698+}
699+
630700 LOOP_ATTR_RO(backing_file);
631701 LOOP_ATTR_RO(offset);
632702 LOOP_ATTR_RO(sizelimit);
633703 LOOP_ATTR_RO(autoclear);
634704 LOOP_ATTR_RO(partscan);
705+LOOP_ATTR_RO(dio);
635706
636707 static struct attribute *loop_attrs[] = {
637708 &loop_attr_backing_file.attr,
@@ -639,6 +710,7 @@ static struct attribute *loop_attrs[] = { · 1 unmodified line
639710 &loop_attr_sizelimit.attr,
640711 &loop_attr_autoclear.attr,
641712 &loop_attr_partscan.attr,
713+ &loop_attr_dio.attr,
642714 NULL,
643715 };
644716
@@ -770,6 +842,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, · 125 unmodified lines
770842
771843 set_device_ro(bdev, (lo_flags & LO_FLAGS_READ_ONLY) != 0);
772844
845+ lo->use_dio = false;
773846 lo->lo_blocksize = lo_blocksize;
774847 lo->lo_device = bdev;
775848 lo->lo_flags = lo_flags;
@@ -783,6 +856,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, · 7 unmodified lines
783856 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
784857 blk_queue_flush(lo->lo_queue, REQ_FLUSH);
785858
859+ loop_update_dio(lo);
786860 set_capacity(lo->lo_disk, size);
787861 bd_set_size(bdev, size << 9);
788862 loop_sysfs_init(lo);
@@ -1001,6 +1075,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info) · 212 unmodified lines
10011075 lo->lo_key_owner = uid;
10021076 }
10031077
1078+ /* update dio if lo_offset or transfer is changed */
1079+ __loop_update_dio(lo, lo->use_dio);
1080+
10041081 return 0;
10051082 }
10061083
drivers/block/loop.h+1−0
@@ -58,6 +58,7 @@ struct loop_device { · 57 unmodified lines
5858 struct mutex lo_ctl_mutex;
5959 struct kthread_worker worker;
6060 struct task_struct *worker_task;
61+ bool use_dio;
6162
6263 struct request_queue *lo_queue;
6364 struct blk_mq_tag_set tag_set;
include/uapi/linux/loop.h+1−0
@@ -21,6 +21,7 @@ enum { · 20 unmodified lines
2121 LO_FLAGS_READ_ONLY = 1,
2222 LO_FLAGS_AUTOCLEAR = 4,
2323 LO_FLAGS_PARTSCAN = 8,
24+ LO_FLAGS_DIRECT_IO = 16,
2425 };
2526
2627 #include <asm/posix_types.h> /* for __kernel_old_dev_t */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t