1 /*
2  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 /*
18  * Basic test for the BLKGETSIZE and BLKGETSIZE64 ioctls.
19  *
20  * - BLKGETSIZE returns size in 512 byte blocks BLKGETSIZE64 in bytes
21  *   compare that they return the same value.
22  * - lseek to the end of the device, this should work
23  * - try to read from the device, read should return 0
24  */
25 
26 #include <stdint.h>
27 #include <errno.h>
28 #include <sys/mount.h>
29 #include "tst_test.h"
30 
31 static int fd;
32 
verify_ioctl(void)33 static void verify_ioctl(void)
34 {
35 	unsigned long size = 0;
36 	uint64_t size64 = 0;
37 	char buf;
38 	int ret;
39 
40 	fd = SAFE_OPEN(tst_device->dev, O_RDONLY);
41 
42 	SAFE_IOCTL(fd, BLKGETSIZE, &size);
43 	SAFE_IOCTL(fd, BLKGETSIZE64, &size64);
44 
45 	if (size == size64/512) {
46 		tst_res(TPASS, "BLKGETSIZE returned %lu, BLKGETSIZE64 %llu",
47 			size, (unsigned long long)size64);
48 	} else {
49 		tst_res(TFAIL,
50 			"BLKGETSIZE returned %lu, BLKGETSIZE64 returned %llu",
51 			size, (unsigned long long)size64);
52 	}
53 
54 	if (lseek(fd, size * 512, SEEK_SET) !=  (off_t)size * 512) {
55 		tst_res(TFAIL | TERRNO,
56 			"Cannot lseek to the end of the device");
57 	} else {
58 		tst_res(TPASS, "Could lseek to the end of the device");
59 	}
60 
61 	ret = read(fd, &buf, 1);
62 
63 	if (ret == 0) {
64 		tst_res(TPASS,
65 			"Got EOF when trying to read after the end of device");
66 	} else {
67 		tst_res(TFAIL | TERRNO,
68 			"Read at the end of device returned %i", ret);
69 	}
70 
71 	SAFE_CLOSE(fd);
72 }
73 
cleanup(void)74 static void cleanup(void)
75 {
76 	if (fd > 0)
77 		SAFE_CLOSE(fd);
78 }
79 
80 static struct tst_test test = {
81 	.needs_device = 1,
82 	.needs_root = 1,
83 	.cleanup = cleanup,
84 	.test_all = verify_ioctl,
85 };
86