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