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 BLKRASET and BLKRAGET ioctls.
7 *
8 * Sets device read-ahead, reads it back and compares the values.
9 *
10 * The read-ahead value was choosen to be multiple of 512, since it's rounded
11 * based on page size on BLKRASET and 512 should be safe enough for everyone.
12 */
13
14 #include <errno.h>
15 #include <sys/mount.h>
16 #include "tst_test.h"
17
18 static int fd;
19
verify_ioctl(void)20 static void verify_ioctl(void)
21 {
22 unsigned long ra, rab, rao;
23
24 SAFE_IOCTL(fd, BLKRAGET, &rao);
25
26 tst_res(TINFO, "BLKRAGET original value %lu", rao);
27
28 for (ra = 0; ra <= 4096; ra += 512) {
29 SAFE_IOCTL(fd, BLKRASET, ra);
30 SAFE_IOCTL(fd, BLKRAGET, &rab);
31
32 if (ra == rab)
33 tst_res(TPASS, "BLKRASET %lu read back correctly", ra);
34 else
35 tst_res(TFAIL, "BLKRASET %lu read back %lu", ra, rab);
36 }
37
38 tst_res(TINFO, "BLKRASET restoring original value %lu", rao);
39
40 SAFE_IOCTL(fd, BLKRASET, rao);
41 }
42
setup(void)43 static void setup(void)
44 {
45 fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
46 }
47
cleanup(void)48 static void cleanup(void)
49 {
50 if (fd > 0)
51 SAFE_CLOSE(fd);
52 }
53
54 static struct tst_test test = {
55 .needs_root = 1,
56 .needs_device = 1,
57 .setup = setup,
58 .cleanup = cleanup,
59 .test_all = verify_ioctl,
60 };
61