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