1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4 * Copyright (c) Linux Test Project, 2020-2022
5 * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Tests ioctl() on loopdevice with LOOP_SET_DIRECT_IO flag.
12 *
13 * Tests whether LOOP_SET_DIRECT_IO can update a live loop device dio mode.
14 * It requires the backing file also supports dio mode and the lo_offset is
15 * aligned with the logical block size.
16 *
17 * The direct I/O error handling is a bit messy on Linux, some filesystems
18 * return error when it coudln't be enabled, some silently fall back to regular
19 * buffered I/O.
20 *
21 * The LOOP_SET_DIRECT_IO ioctl() may ignore all checks if it cannot get the
22 * logical block size which is the case if the block device pointer in the
23 * backing file inode is not set. In this case the direct I/O appears to be
24 * enabled but falls back to buffered I/O later on. This is the case at least
25 * for Btrfs. Because of that the test passes both with failure as well as
26 * success with non-zero offset.
27 */
28
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <sys/mount.h>
34 #include "lapi/loop.h"
35 #include "tst_test.h"
36
37 #define DIO_MESSAGE "In dio mode"
38 #define NON_DIO_MESSAGE "In non dio mode"
39
40 static char dev_path[1024], sys_loop_diopath[1024], backing_file_path[1024];
41 static int dev_num, dev_fd, block_devfd, attach_flag, logical_block_size;
42
check_dio_value(int flag)43 static void check_dio_value(int flag)
44 {
45 struct loop_info loopinfoget;
46
47 memset(&loopinfoget, 0, sizeof(loopinfoget));
48
49 SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
50 tst_res(TINFO, "%s", flag ? DIO_MESSAGE : NON_DIO_MESSAGE);
51
52 if (loopinfoget.lo_flags & LO_FLAGS_DIRECT_IO)
53 tst_res(flag ? TPASS : TFAIL, "lo_flags has LO_FLAGS_DIRECT_IO flag");
54 else
55 tst_res(flag ? TFAIL : TPASS, "lo_flags doesn't have LO_FLAGS_DIRECT_IO flag");
56
57 TST_ASSERT_INT(sys_loop_diopath, flag);
58 }
59
verify_ioctl_loop(void)60 static void verify_ioctl_loop(void)
61 {
62 struct loop_info loopinfo;
63
64 memset(&loopinfo, 0, sizeof(loopinfo));
65 TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
66
67 tst_res(TINFO, "Without setting lo_offset or sizelimit");
68 SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 1);
69 check_dio_value(1);
70
71 SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
72 check_dio_value(0);
73
74 tst_res(TINFO, "With offset equal to logical_block_size");
75 loopinfo.lo_offset = logical_block_size;
76 TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
77 TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
78 if (TST_RET == 0) {
79 tst_res(TPASS, "LOOP_SET_DIRECT_IO succeeded");
80 check_dio_value(1);
81 SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
82 } else {
83 tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed");
84 }
85
86 tst_res(TINFO, "With nonzero offset less than logical_block_size");
87 loopinfo.lo_offset = logical_block_size / 2;
88 TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
89
90 TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
91 if (TST_RET == 0) {
92 tst_res(TPASS, "LOOP_SET_DIRECT_IO succeeded, offset is ignored");
93 SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
94 return;
95 }
96 if (TST_ERR == EINVAL)
97 tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
98 else
99 tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
100 }
101
setup(void)102 static void setup(void)
103 {
104 char bd_path[100];
105
106 dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
107 if (dev_num < 0)
108 tst_brk(TBROK, "Failed to find free loop device");
109
110 sprintf(sys_loop_diopath, "/sys/block/loop%d/loop/dio", dev_num);
111 tst_fill_file("test.img", 0, 1024, 1024);
112
113 tst_attach_device(dev_path, "test.img");
114 attach_flag = 1;
115 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
116
117 if (ioctl(dev_fd, LOOP_SET_DIRECT_IO, 0) && errno == EINVAL)
118 tst_brk(TCONF, "LOOP_SET_DIRECT_IO is not supported");
119
120 /*
121 * from __loop_update_dio():
122 * We support direct I/O only if lo_offset is aligned with the
123 * logical I/O size of backing device, and the logical block
124 * size of loop is bigger than the backing device's and the loop
125 * needn't transform transfer.
126 */
127 sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir());
128 tst_find_backing_dev(backing_file_path, bd_path, sizeof(bd_path));
129 block_devfd = SAFE_OPEN(bd_path, O_RDWR);
130 SAFE_IOCTL(block_devfd, BLKSSZGET, &logical_block_size);
131 tst_res(TINFO, "backing dev(%s) logical_block_size is %d", bd_path, logical_block_size);
132 SAFE_CLOSE(block_devfd);
133 if (logical_block_size > 512)
134 TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, logical_block_size), TST_RETVAL_EQ0);
135 }
136
cleanup(void)137 static void cleanup(void)
138 {
139 if (dev_fd > 0)
140 SAFE_CLOSE(dev_fd);
141 if (block_devfd > 0)
142 SAFE_CLOSE(block_devfd);
143 if (attach_flag)
144 tst_detach_device(dev_path);
145 }
146
147 static struct tst_test test = {
148 .setup = setup,
149 .cleanup = cleanup,
150 .test_all = verify_ioctl_loop,
151 .needs_root = 1,
152 .needs_tmpdir = 1,
153 .skip_filesystems = (const char *const []) {
154 "tmpfs",
155 "overlayfs",
156 NULL
157 },
158 .needs_drivers = (const char *const []) {
159 "loop",
160 NULL
161 }
162 };
163