xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioctl/ioctl06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Basic test for the BLKRASET and BLKRAGET ioctls.
10  *
11  * Sets device read-ahead, reads it back and compares the values.
12  *
13  * The read-ahead value was choosen to be multiple of 512, since it's rounded
14  * based on page size on BLKRASET and 512 should be safe enough for everyone.
15  */
16 
17 #include <errno.h>
18 #include <sys/mount.h>
19 #include "tst_test.h"
20 
21 static int fd;
22 
verify_ioctl(void)23 static void verify_ioctl(void)
24 {
25 	unsigned long ra, rab, rao;
26 
27 	SAFE_IOCTL(fd, BLKRAGET, &rao);
28 
29 	tst_res(TINFO, "BLKRAGET original value %lu", rao);
30 
31 	for (ra = 0; ra <= 4096; ra += 512) {
32 		SAFE_IOCTL(fd, BLKRASET, ra);
33 		SAFE_IOCTL(fd, BLKRAGET, &rab);
34 
35 		if (ra == rab)
36 			tst_res(TPASS, "BLKRASET %lu read back correctly", ra);
37 		else
38 			tst_res(TFAIL, "BLKRASET %lu read back %lu", ra, rab);
39 	}
40 
41 	tst_res(TINFO, "BLKRASET restoring original value %lu", rao);
42 
43 	SAFE_IOCTL(fd, BLKRASET, rao);
44 }
45 
setup(void)46 static void setup(void)
47 {
48 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
49 }
50 
cleanup(void)51 static void cleanup(void)
52 {
53 	if (fd > 0)
54 		SAFE_CLOSE(fd);
55 }
56 
57 static struct tst_test test = {
58 	.needs_root = 1,
59 	.needs_device = 1,
60 	.setup = setup,
61 	.cleanup = cleanup,
62 	.test_all = verify_ioctl,
63 };
64