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 BLKGETSIZE and BLKGETSIZE64 ioctls.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * - BLKGETSIZE returns size in 512 byte blocks BLKGETSIZE64 in bytes
12*49cdfc7eSAndroid Build Coastguard Worker * compare that they return the same value.
13*49cdfc7eSAndroid Build Coastguard Worker * - lseek to the end of the device, this should work
14*49cdfc7eSAndroid Build Coastguard Worker * - try to read from the device, read should return 0
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker static int fd;
23*49cdfc7eSAndroid Build Coastguard Worker
verify_ioctl(void)24*49cdfc7eSAndroid Build Coastguard Worker static void verify_ioctl(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker unsigned long size = 0;
27*49cdfc7eSAndroid Build Coastguard Worker uint64_t size64 = 0;
28*49cdfc7eSAndroid Build Coastguard Worker char buf;
29*49cdfc7eSAndroid Build Coastguard Worker int ret;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, BLKGETSIZE, &size);
34*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, BLKGETSIZE64, &size64);
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker if (size == size64/512) {
37*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "BLKGETSIZE returned %lu, BLKGETSIZE64 %llu",
38*49cdfc7eSAndroid Build Coastguard Worker size, (unsigned long long)size64);
39*49cdfc7eSAndroid Build Coastguard Worker } else {
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
41*49cdfc7eSAndroid Build Coastguard Worker "BLKGETSIZE returned %lu, BLKGETSIZE64 returned %llu",
42*49cdfc7eSAndroid Build Coastguard Worker size, (unsigned long long)size64);
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker if (lseek(fd, size * 512, SEEK_SET) != (off_t)size * 512) {
46*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO,
47*49cdfc7eSAndroid Build Coastguard Worker "Cannot lseek to the end of the device");
48*49cdfc7eSAndroid Build Coastguard Worker } else {
49*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Could lseek to the end of the device");
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker ret = read(fd, &buf, 1);
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker if (ret == 0) {
55*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
56*49cdfc7eSAndroid Build Coastguard Worker "Got EOF when trying to read after the end of device");
57*49cdfc7eSAndroid Build Coastguard Worker } else {
58*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO,
59*49cdfc7eSAndroid Build Coastguard Worker "Read at the end of device returned %i", ret);
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)65*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
68*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
72*49cdfc7eSAndroid Build Coastguard Worker .needs_device = 1,
73*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
74*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
75*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_ioctl,
76*49cdfc7eSAndroid Build Coastguard Worker };
77