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