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, 2022
5 * Author: Yang Xu <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Tests invalid block size of loopdevice by using ioctl() with
12 * LOOP_SET_BLOCK_SIZE and LOOP_CONFIGURE flags.
13 */
14
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <stdlib.h>
19 #include "lapi/loop.h"
20 #include "tst_test.h"
21
22 static char dev_path[1024];
23 static int dev_num, dev_fd, file_fd, attach_flag, loop_configure_sup = 1;
24 static unsigned int invalid_value, half_value, unalign_value;
25 static struct loop_config loopconfig;
26
27 static struct tcase {
28 unsigned int *setvalue;
29 int ioctl_flag;
30 char *message;
31 } tcases[] = {
32 {&half_value, LOOP_SET_BLOCK_SIZE,
33 "Using LOOP_SET_BLOCK_SIZE with arg < 512"},
34
35 {&invalid_value, LOOP_SET_BLOCK_SIZE,
36 "Using LOOP_SET_BLOCK_SIZE with arg > PAGE_SIZE"},
37
38 {&unalign_value, LOOP_SET_BLOCK_SIZE,
39 "Using LOOP_SET_BLOCK_SIZE with arg != power_of_2"},
40
41 {&half_value, LOOP_CONFIGURE,
42 "Using LOOP_CONFIGURE with block_size < 512"},
43
44 {&invalid_value, LOOP_CONFIGURE,
45 "Using LOOP_CONFIGURE with block_size > PAGE_SIZE"},
46
47 {&unalign_value, LOOP_CONFIGURE,
48 "Using LOOP_CONFIGURE with block_size != power_of_2"},
49 };
50
verify_ioctl_loop(unsigned int n)51 static void verify_ioctl_loop(unsigned int n)
52 {
53 if (tcases[n].ioctl_flag == LOOP_CONFIGURE)
54 TEST(ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig));
55 else
56 TEST(ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, *(tcases[n].setvalue)));
57
58 if (TST_RET == 0) {
59 tst_res(TFAIL, "Set block size succeed unexpectedly");
60 if (tcases[n].ioctl_flag == LOOP_CONFIGURE) {
61 tst_detach_device_by_fd(dev_path, dev_fd);
62 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
63 }
64 return;
65 }
66 if (TST_ERR == EINVAL)
67 tst_res(TPASS | TTERRNO, "Set block size failed as expected");
68 else
69 tst_res(TFAIL | TTERRNO, "Set block size failed expected EINVAL got");
70 }
71
run(unsigned int n)72 static void run(unsigned int n)
73 {
74 struct tcase *tc = &tcases[n];
75
76 tst_res(TINFO, "%s", tc->message);
77 if (tc->ioctl_flag == LOOP_SET_BLOCK_SIZE) {
78 if (!attach_flag) {
79 tst_attach_device(dev_path, "test.img");
80 attach_flag = 1;
81 }
82 verify_ioctl_loop(n);
83 return;
84 }
85
86 if (tc->ioctl_flag == LOOP_CONFIGURE && !loop_configure_sup) {
87 tst_res(TCONF, "LOOP_CONFIGURE ioctl not supported");
88 return;
89 }
90 if (attach_flag) {
91 tst_detach_device_by_fd(dev_path, dev_fd);
92 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
93 attach_flag = 0;
94 }
95 loopconfig.block_size = *(tc->setvalue);
96 verify_ioctl_loop(n);
97 }
98
setup(void)99 static void setup(void)
100 {
101 unsigned int pg_size;
102 int ret;
103
104 dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
105 if (dev_num < 0)
106 tst_brk(TBROK, "Failed to find free loop device");
107
108 tst_fill_file("test.img", 0, 1024, 1024);
109 half_value = 256;
110 pg_size = getpagesize();
111 invalid_value = pg_size * 2 ;
112 unalign_value = pg_size - 1;
113
114 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
115 ret = ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, 512);
116
117 if (ret && (errno == EINVAL || errno == ENOTTY))
118 tst_brk(TCONF, "LOOP_SET_BLOCK_SIZE is not supported");
119
120 file_fd = SAFE_OPEN("test.img", O_RDWR);
121 loopconfig.fd = -1;
122 ret = ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig);
123 if (ret && errno != EBADF) {
124 tst_res(TINFO | TERRNO, "LOOP_CONFIGURE is not supported");
125 loop_configure_sup = 0;
126 return;
127 }
128 loopconfig.fd = file_fd;
129 }
130
cleanup(void)131 static void cleanup(void)
132 {
133 if (dev_fd > 0)
134 SAFE_CLOSE(dev_fd);
135 if (file_fd > 0)
136 SAFE_CLOSE(file_fd);
137 if (attach_flag)
138 tst_detach_device(dev_path);
139 }
140
141 static struct tst_test test = {
142 .setup = setup,
143 .cleanup = cleanup,
144 .test = run,
145 .tcnt = ARRAY_SIZE(tcases),
146 .needs_root = 1,
147 .needs_tmpdir = 1,
148 .needs_drivers = (const char *const []) {
149 "loop",
150 NULL
151 }
152 };
153