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