• 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  * Copyright (c) Linux Test Project, 2022
5  * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
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 		return;
63 	}
64 	if (TST_ERR == EINVAL)
65 		tst_res(TPASS | TTERRNO, "Set block size failed as expected");
66 	else
67 		tst_res(TFAIL | TTERRNO, "Set block size failed expected EINVAL got");
68 }
69 
run(unsigned int n)70 static void run(unsigned int n)
71 {
72 	struct tcase *tc = &tcases[n];
73 
74 	tst_res(TINFO, "%s", tc->message);
75 	if (tc->ioctl_flag == LOOP_SET_BLOCK_SIZE) {
76 		if (!attach_flag) {
77 			tst_attach_device(dev_path, "test.img");
78 			attach_flag = 1;
79 		}
80 		verify_ioctl_loop(n);
81 		return;
82 	}
83 
84 	if (tc->ioctl_flag == LOOP_CONFIGURE && !loop_configure_sup) {
85 		tst_res(TCONF, "LOOP_CONFIGURE ioctl not supported");
86 		return;
87 	}
88 	if (attach_flag) {
89 		tst_detach_device_by_fd(dev_path, dev_fd);
90 		attach_flag = 0;
91 	}
92 	loopconfig.block_size = *(tc->setvalue);
93 	verify_ioctl_loop(n);
94 }
95 
setup(void)96 static void setup(void)
97 {
98 	unsigned int pg_size;
99 	int ret;
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 	tst_fill_file("test.img", 0, 1024, 1024);
106 	half_value = 256;
107 	pg_size = getpagesize();
108 	invalid_value = pg_size * 2 ;
109 	unalign_value = pg_size - 1;
110 
111 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
112 	ret = ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, 512);
113 
114 	if (ret && (errno == EINVAL || errno == ENOTTY))
115 		tst_brk(TCONF, "LOOP_SET_BLOCK_SIZE is not supported");
116 
117 	file_fd = SAFE_OPEN("test.img", O_RDWR);
118 	loopconfig.fd = -1;
119 	ret = ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig);
120 	if (ret && errno != EBADF) {
121 		tst_res(TINFO | TERRNO, "LOOP_CONFIGURE is not supported");
122 		loop_configure_sup = 0;
123 		return;
124 	}
125 	loopconfig.fd = file_fd;
126 }
127 
cleanup(void)128 static void cleanup(void)
129 {
130 	if (dev_fd > 0)
131 		SAFE_CLOSE(dev_fd);
132 	if (file_fd > 0)
133 		SAFE_CLOSE(file_fd);
134 	if (attach_flag)
135 		tst_detach_device(dev_path);
136 }
137 
138 static struct tst_test test = {
139 	.setup = setup,
140 	.cleanup = cleanup,
141 	.test = run,
142 	.tcnt = ARRAY_SIZE(tcases),
143 	.needs_root = 1,
144 	.needs_tmpdir = 1,
145 	.needs_drivers = (const char *const []) {
146 		"loop",
147 		NULL
148 	}
149 };
150