xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl_loop01.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, 2020-2022
5  * Author: Yang Xu <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Tests ioctl() on loopdevice with LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flags.
12  *
13  * For LO_FLAGS_AUTOCLEAR flag, only checks autoclear field value in sysfs
14  * and also gets lo_flags by using LOOP_GET_STATUS.
15  *
16  * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
17  * But also checks whether it can scan partition table correctly i.e. checks
18  * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
19  *
20  * For LO_FLAGS_AUTOCLEAR flag, it can be clear. For LO_FLAGS_PARTSCAN flag,
21  * it cannot be clear. Test checks this.
22  */
23 
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include "lapi/loop.h"
28 #include "tst_test.h"
29 
30 static char dev_path[1024], backing_path[1024], backing_file_path[1024];
31 static int dev_num, attach_flag, dev_fd, parted_sup;
32 
33 /*
34  * In drivers/block/loop.c code, set status function doesn't handle
35  * LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with read only mode
36  * file_fd, lo_flags will include LO_FLAGS_READ_ONLY and it's the same for
37  * LO_FLAGS_DIRECT_IO.
38  */
39 #define SET_FLAGS (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
40 #define GET_FLAGS (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
41 
42 static char partscan_path[1024], autoclear_path[1024];
43 static char loop_partpath[1026], sys_loop_partpath[1026];
44 
check_loop_value(int set_flag,int get_flag,int autoclear_field)45 static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
46 {
47 	struct loop_info loopinfo = {0}, loopinfoget = {0};
48 	int ret;
49 
50 	loopinfo.lo_flags = set_flag;
51 	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
52 	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
53 
54 	if (loopinfoget.lo_flags & ~get_flag)
55 		tst_res(TFAIL, "expect %d but got %d", get_flag, loopinfoget.lo_flags);
56 	else
57 		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
58 
59 	TST_ASSERT_INT(partscan_path, 1);
60 	TST_ASSERT_INT(autoclear_path, autoclear_field);
61 
62 	if (!parted_sup) {
63 		tst_res(TINFO, "Current environment doesn't have parted disk, skip it");
64 		return;
65 	}
66 
67 	ret = TST_RETRY_FN_EXP_BACKOFF(access(loop_partpath, F_OK), TST_RETVAL_EQ0, 30);
68 	if (ret == 0)
69 		tst_res(TPASS, "access %s succeeds", loop_partpath);
70 	else
71 		tst_res(TFAIL, "access %s fails", loop_partpath);
72 
73 	ret = TST_RETRY_FN_EXP_BACKOFF(access(sys_loop_partpath, F_OK), TST_RETVAL_EQ0, 30);
74 	if (ret == 0)
75 		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
76 	else
77 		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
78 }
79 
verify_ioctl_loop(void)80 static void verify_ioctl_loop(void)
81 {
82 	tst_attach_device(dev_path, "test.img");
83 	attach_flag = 1;
84 
85 	TST_ASSERT_INT(partscan_path, 0);
86 	TST_ASSERT_INT(autoclear_path, 0);
87 	TST_ASSERT_STR(backing_path, backing_file_path);
88 
89 	check_loop_value(SET_FLAGS, GET_FLAGS, 1);
90 
91 	tst_res(TINFO, "Test flag can be clear");
92 	check_loop_value(0, LO_FLAGS_PARTSCAN, 0);
93 
94 	tst_detach_device_by_fd(dev_path, dev_fd);
95 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
96 	attach_flag = 0;
97 }
98 
setup(void)99 static void setup(void)
100 {
101 	int ret;
102 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
103 	                                  "primary", "ext4", "1M", "10M", NULL};
104 
105 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
106 	if (dev_num < 0)
107 		tst_brk(TBROK, "Failed to find free loop device");
108 
109 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
110 
111 	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
112 	switch (ret) {
113 	case 0:
114 		parted_sup = 1;
115 	break;
116 	case 255:
117 		tst_res(TCONF, "parted binary not installed or failed");
118 	break;
119 	default:
120 		tst_res(TCONF, "parted exited with %i", ret);
121 	break;
122 	}
123 
124 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
125 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
126 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
127 	sprintf(sys_loop_partpath, "/sys/block/loop%d/loop%dp1", dev_num, dev_num);
128 	sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir());
129 	sprintf(loop_partpath, "%sp1", dev_path);
130 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
131 }
132 
cleanup(void)133 static void cleanup(void)
134 {
135 	if (dev_fd > 0)
136 		SAFE_CLOSE(dev_fd);
137 	if (attach_flag)
138 		tst_detach_device(dev_path);
139 }
140 
141 static struct tst_test test = {
142 	.setup = setup,
143 	.cleanup = cleanup,
144 	.test_all = verify_ioctl_loop,
145 	.needs_root = 1,
146 	.needs_drivers = (const char *const []) {
147 		"loop",
148 		NULL
149 	},
150 	.tags = (const struct tst_tag[]) {
151 		{"linux-git", "10c70d95c0f2"},
152 		{"linux-git", "6ac92fb5cdff"},
153 		{}
154 	},
155 	.needs_tmpdir = 1,
156 };
157