xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl_loop02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 LO_FLAGS_READ_ONLY (similar as losetup -r) and
12  * LOOP_CHANGE_FD flags.
13  *
14  * For LOOP_CHANGE_FD, this operation is possible only if the loop device
15  * is read-only and the new backing store is the same size and type as the
16  * old backing store.
17  *
18  * When using LOOP_CONFIGURE ioctl(), it can set LO_FLAGS_READ_ONLY
19  * flag even though backing file with write mode.
20  */
21 
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "lapi/loop.h"
27 #include "tst_test.h"
28 
29 static int file_fd, file_change_fd, file_fd_invalid;
30 static char backing_path[1024], backing_file_path[1024], backing_file_change_path[1024];
31 static int attach_flag, dev_fd, loop_configure_sup = 1;
32 static char loop_ro_path[1024], dev_path[1024];
33 static struct loop_config loopconfig;
34 
35 static struct tcase {
36 	int mode;
37 	int ioctl;
38 	char *message;
39 } tcases[] = {
40 	{O_RDONLY, LOOP_SET_FD, "Using LOOP_SET_FD to setup loopdevice"},
41 	{O_RDWR, LOOP_CONFIGURE, "Using LOOP_CONFIGURE with read_only flag"},
42 };
43 
verify_ioctl_loop(unsigned int n)44 static void verify_ioctl_loop(unsigned int n)
45 {
46 	struct tcase *tc = &tcases[n];
47 	struct loop_info loopinfoget;
48 
49 	if (tc->ioctl == LOOP_CONFIGURE && !loop_configure_sup) {
50 		tst_res(TCONF, "LOOP_CONFIGURE ioctl not supported");
51 		return;
52 	}
53 
54 	tst_res(TINFO, "%s", tc->message);
55 	file_fd = SAFE_OPEN("test.img", tc->mode);
56 
57 	if (tc->ioctl == LOOP_SET_FD) {
58 		SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd);
59 	} else {
60 		loopconfig.fd = file_fd;
61 		SAFE_IOCTL(dev_fd, LOOP_CONFIGURE, &loopconfig);
62 	}
63 	attach_flag = 1;
64 
65 	TST_ASSERT_INT(loop_ro_path, 1);
66 	TST_ASSERT_STR(backing_path, backing_file_path);
67 
68 	memset(&loopinfoget, 0, sizeof(loopinfoget));
69 
70 	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
71 
72 	if (loopinfoget.lo_flags & ~LO_FLAGS_READ_ONLY)
73 		tst_res(TFAIL, "lo_flags has unexpected %d flag", loopinfoget.lo_flags);
74 	else
75 		tst_res(TPASS, "lo_flags only has default LO_FLAGS_READ_ONLY flag");
76 
77 	TEST(write(dev_fd, "xx", 2));
78 	if (TST_RET != -1)
79 		tst_res(TFAIL, "write succeed unexpectedly");
80 	else
81 		tst_res(TPASS | TTERRNO, "Can not write data in RO mode");
82 
83 	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_change_fd));
84 	if (TST_RET) {
85 		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed");
86 	} else {
87 		tst_res(TPASS, "LOOP_CHANGE_FD succeeded");
88 		TST_ASSERT_INT(loop_ro_path, 1);
89 		TST_ASSERT_STR(backing_path, backing_file_change_path);
90 	}
91 
92 	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd_invalid));
93 	if (TST_RET) {
94 		if (TST_ERR == EINVAL)
95 			tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
96 		else
97 			tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
98 	} else {
99 		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded");
100 	}
101 
102 	SAFE_CLOSE(file_fd);
103 	tst_detach_device_by_fd(dev_path, dev_fd);
104 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
105 	attach_flag = 0;
106 }
107 
setup(void)108 static void setup(void)
109 {
110 	int dev_num;
111 	int ret;
112 
113 	char *tmpdir = tst_get_tmpdir();
114 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
115 	if (dev_num < 0)
116 		tst_brk(TBROK, "Failed to find free loop device");
117 
118 	tst_fill_file("test.img", 0, 1024, 10);
119 	tst_fill_file("test1.img", 0, 1024, 10);
120 	tst_fill_file("test2.img", 0, 2048, 20);
121 
122 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
123 	sprintf(backing_file_path, "%s/test.img", tmpdir);
124 	sprintf(backing_file_change_path, "%s/test1.img", tmpdir);
125 	sprintf(loop_ro_path, "/sys/block/loop%d/ro", dev_num);
126 
127 	free(tmpdir);
128 
129 	file_change_fd = SAFE_OPEN("test1.img", O_RDWR);
130 	file_fd_invalid = SAFE_OPEN("test2.img", O_RDWR);
131 
132 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
133 	loopconfig.fd = -1;
134 	ret = ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig);
135 
136 	if (ret && errno != EBADF) {
137 		tst_res(TINFO | TERRNO, "LOOP_CONFIGURE is not supported");
138 		loop_configure_sup = 0;
139 	}
140 	loopconfig.info.lo_flags = LO_FLAGS_READ_ONLY;
141 }
142 
cleanup(void)143 static void cleanup(void)
144 {
145 	if (dev_fd > 0)
146 		SAFE_CLOSE(dev_fd);
147 	if (file_fd > 0)
148 		SAFE_CLOSE(file_fd);
149 	if (file_change_fd > 0)
150 		SAFE_CLOSE(file_change_fd);
151 	if (file_fd_invalid > 0)
152 		SAFE_CLOSE(file_fd_invalid);
153 	if (attach_flag)
154 		tst_detach_device(dev_path);
155 }
156 
157 static struct tst_test test = {
158 	.setup = setup,
159 	.cleanup = cleanup,
160 	.tcnt = ARRAY_SIZE(tcases),
161 	.test = verify_ioctl_loop,
162 	.needs_root = 1,
163 	.needs_tmpdir = 1,
164 	.needs_drivers = (const char *const []) {
165 		"loop",
166 		NULL
167 	}
168 };
169