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