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