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 ioctl() on loopdevice with LOOP_SET_STATUS64 and LOOP_GET_STATUS64 flags.
12 *
13 * Tests lo_sizelimit field. If lo_sizelimit is 0, it means max
14 * available. If sizelimit is less than loop_size, loopsize will
15 * be truncated.
16 *
17 * Also uses LOOP_CONFIGURE ioctl to test lo_sizelimit field.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include "lapi/loop.h"
25 #include "tst_test.h"
26
27 static char dev_path[1024], sys_loop_sizepath[1024], sys_loop_sizelimitpath[1024];
28 static int dev_num, dev_fd, file_fd, attach_flag, loop_configure_sup = 1;
29 static struct loop_config loopconfig;
30
31 static struct tcase {
32 unsigned int set_sizelimit;
33 unsigned int exp_loopsize;
34 int ioctl_flag;
35 char *message;
36 } tcases[] = {
37 {1024 * 4096, 2048, LOOP_SET_STATUS64,
38 "When sizelimit is greater than loopsize by using LOOP_SET_STATUS64"},
39
40 {1024 * 512, 1024, LOOP_SET_STATUS64,
41 "When sizelimit is less than loopsize by using LOOP_SET_STATUS64"},
42
43 {1024 * 4096, 2048, LOOP_CONFIGURE,
44 "When sizelimit is greater than loopsize by using LOOP_CONFIGURE"},
45
46 {1024 * 512, 1024, LOOP_CONFIGURE,
47 "When sizelimit is less than loopsize by using LOOP_CONFIGURE"},
48 };
49
verify_ioctl_loop(unsigned int n)50 static void verify_ioctl_loop(unsigned int n)
51 {
52 struct tcase *tc = &tcases[n];
53 struct loop_info64 loopinfo, loopinfoget;
54
55 memset(&loopinfo, 0, sizeof(loopinfo));
56 memset(&loopinfoget, 0, sizeof(loopinfoget));
57
58 if (tc->ioctl_flag == LOOP_CONFIGURE) {
59 SAFE_IOCTL(dev_fd, LOOP_CONFIGURE, &loopconfig);
60 } else {
61 loopinfo.lo_sizelimit = tc->set_sizelimit;
62 TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo), TST_RETVAL_EQ0);
63 }
64
65 TST_ASSERT_INT(sys_loop_sizepath, tc->exp_loopsize);
66 TST_ASSERT_INT(sys_loop_sizelimitpath, tc->set_sizelimit);
67 SAFE_IOCTL(dev_fd, LOOP_GET_STATUS64, &loopinfoget);
68 if (loopinfoget.lo_sizelimit == tc->set_sizelimit)
69 tst_res(TPASS, "LOOP_GET_STATUS64 gets correct lo_sizelimit(%d)", tc->set_sizelimit);
70 else
71 tst_res(TFAIL, "LOOP_GET_STATUS64 gets wrong lo_sizelimit(%llu), expect %d",
72 loopinfoget.lo_sizelimit, tc->set_sizelimit);
73 /*Reset*/
74 if (tc->ioctl_flag == LOOP_CONFIGURE) {
75 tst_detach_device_by_fd(dev_path, dev_fd);
76 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
77 } else {
78 loopinfo.lo_sizelimit = 0;
79 TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
80 }
81 }
82
run(unsigned int n)83 static void run(unsigned int n)
84 {
85 struct tcase *tc = &tcases[n];
86
87 tst_res(TINFO, "%s", tc->message);
88
89 if (tc->ioctl_flag == LOOP_SET_STATUS64) {
90 if (!attach_flag) {
91 tst_attach_device(dev_path, "test.img");
92 attach_flag = 1;
93 }
94
95 verify_ioctl_loop(n);
96 return;
97 }
98
99 if (tc->ioctl_flag == LOOP_CONFIGURE && !loop_configure_sup) {
100 tst_res(TCONF, "LOOP_CONFIGURE ioctl not supported");
101 return;
102 }
103 if (attach_flag) {
104 tst_detach_device_by_fd(dev_path, dev_fd);
105 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
106 attach_flag = 0;
107 }
108 loopconfig.info.lo_sizelimit = tc->set_sizelimit;
109 verify_ioctl_loop(n);
110 }
111
setup(void)112 static void setup(void)
113 {
114 int ret;
115
116 dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
117 if (dev_num < 0)
118 tst_brk(TBROK, "Failed to find free loop device");
119
120 tst_fill_file("test.img", 0, 1024 * 1024, 1);
121 tst_attach_device(dev_path, "test.img");
122 attach_flag = 1;
123
124 sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
125 sprintf(sys_loop_sizelimitpath, "/sys/block/loop%d/loop/sizelimit", dev_num);
126
127 tst_detach_device(dev_path);
128 attach_flag = 0;
129
130 tst_res(TINFO, "original loop size 2048 sectors");
131 file_fd = SAFE_OPEN("test.img", O_RDWR);
132 dev_fd = SAFE_OPEN(dev_path, O_RDWR);
133
134 loopconfig.fd = -1;
135 ret = ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig);
136 if (ret && errno != EBADF) {
137 tst_res(TINFO | TERRNO, "LOOP_CONFIGURE is not supported");
138 loop_configure_sup = 0;
139 return;
140 }
141
142 loopconfig.fd = file_fd;
143 }
144
cleanup(void)145 static void cleanup(void)
146 {
147 if (dev_fd > 0)
148 SAFE_CLOSE(dev_fd);
149 if (file_fd > 0)
150 SAFE_CLOSE(file_fd);
151 if (attach_flag)
152 tst_detach_device(dev_path);
153 }
154
155 static struct tst_test test = {
156 .setup = setup,
157 .cleanup = cleanup,
158 .test = run,
159 .tcnt = ARRAY_SIZE(tcases),
160 .needs_root = 1,
161 .needs_tmpdir = 1,
162 .tags = (const struct tst_tag[]) {
163 {"linux-git", "79e5dc59e297"},
164 {}
165 },
166 .needs_drivers = (const char *const []) {
167 "loop",
168 NULL
169 }
170 };
171