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) 2017 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Basic test for the BLKROSET and BLKROGET ioctls.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * - Set the device read only, read the value back.
12*49cdfc7eSAndroid Build Coastguard Worker * - Try to mount the device read write, expect failure.
13*49cdfc7eSAndroid Build Coastguard Worker * - Try to mount the device read only, expect success.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker static int fd;
21*49cdfc7eSAndroid Build Coastguard Worker
verify_ioctl(void)22*49cdfc7eSAndroid Build Coastguard Worker static void verify_ioctl(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker int ro = 1;
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, BLKROGET, &ro);
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker if (ro == 0)
29*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "BLKROGET returned 0");
30*49cdfc7eSAndroid Build Coastguard Worker else
31*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "BLKROGET returned %i", ro);
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker ro = 1;
34*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, BLKROSET, &ro);
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker ro = 0;
37*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, BLKROGET, &ro);
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker if (ro == 0)
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "BLKROGET returned 0");
41*49cdfc7eSAndroid Build Coastguard Worker else
42*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "BLKROGET returned %i", ro);
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, 0, NULL));
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
47*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Mounting RO device RW succeeded");
48*49cdfc7eSAndroid Build Coastguard Worker tst_umount("mntpoint");
49*49cdfc7eSAndroid Build Coastguard Worker goto next;
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EACCES) {
53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "Mounting RO device RW failed");
54*49cdfc7eSAndroid Build Coastguard Worker goto next;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
58*49cdfc7eSAndroid Build Coastguard Worker "Mounting RO device RW failed unexpectedly expected EACCES");
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker next:
61*49cdfc7eSAndroid Build Coastguard Worker TEST(mount(tst_device->dev, "mntpoint", tst_device->fs_type, MS_RDONLY, NULL));
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
64*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Mounting RO device RO works");
65*49cdfc7eSAndroid Build Coastguard Worker tst_umount("mntpoint");
66*49cdfc7eSAndroid Build Coastguard Worker } else {
67*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "Mounting RO device RO failed");
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker ro = 0;
71*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, BLKROSET, &ro);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
setup(void)74*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR("mntpoint", 0777);
77*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
83*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker .format_device = 1,
88*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
89*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
91*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_ioctl,
92*49cdfc7eSAndroid Build Coastguard Worker };
93