a1745b013e3619fe2869ef7c7c6c4756c40202b0
[xfstests-dev.git] / src / getdevicesize.c
1 /*
2  * Copyright (c) 2004 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would 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, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * Test program that uses the same interfaces as mkfs.xfs for
21  * Linux, dumps out just the device size values from a driver.
22  */
23
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <stdint.h>
28 #include <sys/ioctl.h>
29 #include <sys/mount.h>
30
31 #ifndef BLKGETSIZE64
32 # define BLKGETSIZE64   _IOR(0x12,114,size_t)
33 #endif
34
35 int main(int argc, char **argv)
36 {
37         uint64_t        size;
38         long long       sz = -1;
39         int             error, fd;
40
41         if (argc != 2) {
42                 fputs("insufficient arguments\n", stderr);
43                 return 1;
44         }
45         fd = open(argv[1], O_RDONLY);
46         if (!fd) {
47                 perror(argv[1]);
48                 return 1;
49         }
50
51         error = ioctl(fd, BLKGETSIZE64, &size);
52         if (error >= 0) {
53                 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
54                 sz = (long long)(size >> 9);
55                 printf("%lld 512 byte blocks (BLKGETSIZE64)\n", sz);
56         } else {
57                 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
58                 unsigned long tmpsize;
59
60                 error = ioctl(fd, BLKGETSIZE, &tmpsize);
61                 if (error < 0) {
62                         fprintf(stderr, "can't determine device size");
63                         return 1;
64                 }
65                 sz = (long long)tmpsize;
66                 printf("%lld 512 byte blocks (BLKGETSIZE)\n", sz);
67         }
68         return 0;
69 }