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