• 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.
7  * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flag.
8  *
9  * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear field value in sys
10  * directory and also get lo_flags by using LOOP_GET_STATUS.
11  *
12  * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
13  * But we also check whether we can scan partition table correctly ie check
14  * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
15  *
16  * For LO_FLAGS_AUTOCLEAR flag, it can be clear. For LO_FLAGS_PARTSCAN flag,
17  * it cannot be clear. We also check this.
18  *
19  * It is also a regression test for kernel
20  * commit 10c70d95c0f2 ("block: remove the bd_openers checks in blk_drop_partitions")
21  * commit 6ac92fb5cdff ("loop: Fix wrong masking of status flags").
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 	attach_flag = 0;
96 }
97 
setup(void)98 static void setup(void)
99 {
100 	int ret;
101 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
102 	                                  "primary", "ext4", "1M", "10M", NULL};
103 
104 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
105 	if (dev_num < 0)
106 		tst_brk(TBROK, "Failed to find free loop device");
107 
108 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
109 
110 	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
111 	switch (ret) {
112 	case 0:
113 		parted_sup = 1;
114 	break;
115 	case 255:
116 		tst_res(TCONF, "parted binary not installed or failed");
117 	break;
118 	default:
119 		tst_res(TCONF, "parted exited with %i", ret);
120 	break;
121 	}
122 
123 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
124 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
125 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
126 	sprintf(sys_loop_partpath, "/sys/block/loop%d/loop%dp1", dev_num, dev_num);
127 	sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir());
128 	sprintf(loop_partpath, "%sp1", dev_path);
129 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
130 }
131 
cleanup(void)132 static void cleanup(void)
133 {
134 	if (dev_fd > 0)
135 		SAFE_CLOSE(dev_fd);
136 	if (attach_flag)
137 		tst_detach_device(dev_path);
138 }
139 
140 static struct tst_test test = {
141 	.setup = setup,
142 	.cleanup = cleanup,
143 	.test_all = verify_ioctl_loop,
144 	.needs_root = 1,
145 	.needs_drivers = (const char *const []) {
146 		"loop",
147 		NULL
148 	},
149 	.tags = (const struct tst_tag[]) {
150 		{"linux-git", "10c70d95c0f2"},
151 		{"linux-git", "6ac92fb5cdff"},
152 		{}
153 	},
154 	.needs_tmpdir = 1,
155 };
156