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