• 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  * Basic test for the BLKRRPART ioctl, it is the same as blockdev
7  * --rereadpt command.
8  */
9 
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <sys/mount.h>
14 #include <stdbool.h>
15 #include "lapi/loop.h"
16 #include "tst_test.h"
17 
18 #define RETVAL_CHECK(x) \
19        ({ value ? TST_RETVAL_EQ0(x) : TST_RETVAL_NOTNULL(x); })
20 
21 static char dev_path[1024];
22 static int dev_num, attach_flag, dev_fd;
23 static char loop_partpath[1026], sys_loop_partpath[1026];
24 
change_partition(const char * const cmd[])25 static void change_partition(const char *const cmd[])
26 {
27 	int ret;
28 
29 	ret = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL);
30 	if (ret)
31 		tst_brk(TBROK, "parted return %i", ret);
32 }
33 
check_partition(int part_num,bool value)34 static void check_partition(int part_num, bool value)
35 {
36 	int ret;
37 
38 	sprintf(sys_loop_partpath, "/sys/block/loop%d/loop%dp%d",
39 		dev_num, dev_num, part_num);
40 	sprintf(loop_partpath, "%sp%d", dev_path, part_num);
41 
42 	ret = TST_RETRY_FN_EXP_BACKOFF(access(sys_loop_partpath, F_OK), RETVAL_CHECK, 30);
43 	if (ret == 0)
44 		tst_res(value ? TPASS : TFAIL, "access %s succeeds",
45 			sys_loop_partpath);
46 	else
47 		tst_res(value ? TFAIL : TPASS, "access %s fails",
48 			sys_loop_partpath);
49 
50 	ret = TST_RETRY_FN_EXP_BACKOFF(access(loop_partpath, F_OK), RETVAL_CHECK, 30);
51 	if (ret == 0)
52 		tst_res(value ? TPASS : TFAIL, "access %s succeeds",
53 			loop_partpath);
54 	else
55 		tst_res(value ? TFAIL : TPASS, "access %s fails",
56 			loop_partpath);
57 }
58 
verify_ioctl(void)59 static void verify_ioctl(void)
60 {
61 	const char *const cmd_parted_old[] = {"parted", "-s", "test.img",
62 					      "mklabel", "msdos", "mkpart",
63 					      "primary", "ext4", "1M", "10M",
64 					      NULL};
65 	const char *const cmd_parted_new[] = {"parted", "-s", "test.img",
66 					      "mklabel", "msdos", "mkpart",
67 					      "primary", "ext4", "1M", "10M",
68 					      "mkpart", "primary", "ext4",
69 					      "10M", "20M", NULL};
70 	struct loop_info loopinfo = {0};
71 
72 	change_partition(cmd_parted_old);
73 	tst_attach_device(dev_path, "test.img");
74 	attach_flag = 1;
75 
76 	loopinfo.lo_flags =  LO_FLAGS_PARTSCAN;
77 	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
78 	check_partition(1, true);
79 	check_partition(2, false);
80 
81 	change_partition(cmd_parted_new);
82 	TST_RETRY_FUNC(ioctl(dev_fd, BLKRRPART, 0), TST_RETVAL_EQ0);
83 	check_partition(1, true);
84 	check_partition(2, true);
85 
86 	tst_detach_device_by_fd(dev_path, dev_fd);
87 	attach_flag = 0;
88 }
89 
setup(void)90 static void setup(void)
91 {
92 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
93 	if (dev_num < 0)
94 		tst_brk(TBROK, "Failed to find free loop device");
95 	tst_prealloc_file("test.img", 1024 * 1024, 20);
96 	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
97 }
98 
cleanup(void)99 static void cleanup(void)
100 {
101 	if (dev_fd > 0)
102 		SAFE_CLOSE(dev_fd);
103 	if (attach_flag)
104 		tst_detach_device(dev_path);
105 }
106 
107 static struct tst_test test = {
108 	.setup = setup,
109 	.cleanup = cleanup,
110 	.test_all = verify_ioctl,
111 	.needs_root = 1,
112 	.needs_drivers = (const char *const []) {
113 		"loop",
114 		NULL
115 	},
116 	.needs_cmds = (const char *const []) {
117 		"parted",
118 		NULL
119 	},
120 	.needs_tmpdir = 1,
121 };
122