• 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  * Copyright (c) Linux Test Project, 2020-2022
5  * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
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];
31 static char *backing_file_path;
32 static int dev_num, attach_flag, dev_fd, parted_sup;
33 
34 /*
35  * In drivers/block/loop.c code, set status function doesn't handle
36  * LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with read only mode
37  * file_fd, lo_flags will include LO_FLAGS_READ_ONLY and it's the same for
38  * LO_FLAGS_DIRECT_IO.
39  */
40 #define SET_FLAGS (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
41 #define GET_FLAGS (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
42 
43 static char partscan_path[1024], autoclear_path[1024];
44 static char loop_partpath[1026], sys_loop_partpath[1026];
45 
check_loop_value(int set_flag,int get_flag,int autoclear_field)46 static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
47 {
48 	struct loop_info loopinfo = {0}, loopinfoget = {0};
49 	int ret;
50 
51 	loopinfo.lo_flags = set_flag;
52 	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
53 	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
54 
55 	if (loopinfoget.lo_flags & ~get_flag)
56 		tst_res(TFAIL, "expect %d but got %d", get_flag, loopinfoget.lo_flags);
57 	else
58 		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
59 
60 	TST_ASSERT_INT(partscan_path, 1);
61 	TST_ASSERT_INT(autoclear_path, autoclear_field);
62 
63 	if (!parted_sup) {
64 		tst_res(TINFO, "Current environment doesn't have parted disk, skip it");
65 		return;
66 	}
67 
68 	ret = TST_RETRY_FN_EXP_BACKOFF(access(loop_partpath, F_OK), TST_RETVAL_EQ0, 30);
69 	if (ret == 0)
70 		tst_res(TPASS, "access %s succeeds", loop_partpath);
71 	else
72 		tst_res(TFAIL, "access %s fails", loop_partpath);
73 
74 	ret = TST_RETRY_FN_EXP_BACKOFF(access(sys_loop_partpath, F_OK), TST_RETVAL_EQ0, 30);
75 	if (ret == 0)
76 		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
77 	else
78 		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
79 }
80 
verify_ioctl_loop(void)81 static void verify_ioctl_loop(void)
82 {
83 	tst_attach_device(dev_path, "test.img");
84 	attach_flag = 1;
85 
86 	TST_ASSERT_INT(partscan_path, 0);
87 	TST_ASSERT_INT(autoclear_path, 0);
88 	TST_ASSERT_STR(backing_path, backing_file_path);
89 
90 	check_loop_value(SET_FLAGS, GET_FLAGS, 1);
91 
92 	tst_res(TINFO, "Test flag can be clear");
93 	check_loop_value(0, LO_FLAGS_PARTSCAN, 0);
94 
95 	tst_detach_device_by_fd(dev_path, dev_fd);
96 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
97 	attach_flag = 0;
98 }
99 
setup(void)100 static void setup(void)
101 {
102 	int ret;
103 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
104 	                                  "primary", "ext4", "1M", "10M", NULL};
105 
106 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
107 	if (dev_num < 0)
108 		tst_brk(TBROK, "Failed to find free loop device");
109 
110 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
111 
112 	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
113 	switch (ret) {
114 	case 0:
115 		parted_sup = 1;
116 	break;
117 	case 255:
118 		tst_res(TCONF, "parted binary not installed or failed");
119 	break;
120 	default:
121 		tst_res(TCONF, "parted exited with %i", ret);
122 	break;
123 	}
124 
125 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
126 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
127 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
128 	sprintf(sys_loop_partpath, "/sys/block/loop%d/loop%dp1", dev_num, dev_num);
129 	backing_file_path = tst_tmpdir_genpath("test.img");
130 	sprintf(loop_partpath, "%sp1", dev_path);
131 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
132 }
133 
cleanup(void)134 static void cleanup(void)
135 {
136 	if (dev_fd > 0)
137 		SAFE_CLOSE(dev_fd);
138 	if (attach_flag)
139 		tst_detach_device(dev_path);
140 }
141 
142 static struct tst_test test = {
143 	.timeout = 1,
144 	.setup = setup,
145 	.cleanup = cleanup,
146 	.test_all = verify_ioctl_loop,
147 	.needs_root = 1,
148 	.needs_drivers = (const char *const []) {
149 		"loop",
150 		NULL
151 	},
152 	.tags = (const struct tst_tag[]) {
153 		{"linux-git", "10c70d95c0f2"},
154 		{"linux-git", "6ac92fb5cdff"},
155 		{}
156 	},
157 	.needs_tmpdir = 1,
158 };
159