open_by_handle: add filename to error reports
[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 <sys/ioctl.h>
28 #include <sys/mount.h>
29
30 #ifndef BLKGETSIZE64
31 # define BLKGETSIZE64   _IOR(0x12,114,size_t)
32 #endif
33
34 int main(int argc, char **argv)
35 {
36         __uint64_t      size;
37         long long       sz = -1;
38         int             error, fd;
39
40         if (argc != 2) {
41                 fputs("insufficient arguments\n", stderr);
42                 return 1;
43         }
44         fd = open(argv[1], O_RDONLY);
45         if (!fd) {
46                 perror(argv[1]);
47                 return 1;
48         }
49
50         error = ioctl(fd, BLKGETSIZE64, &size);
51         if (error >= 0) {
52                 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
53                 sz = (long long)(size >> 9);
54                 printf("%lld 512 byte blocks (BLKGETSIZE64)\n", sz);
55         } else {
56                 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
57                 unsigned long tmpsize;
58
59                 error = ioctl(fd, BLKGETSIZE, &tmpsize);
60                 if (error < 0) {
61                         fprintf(stderr, "can't determine device size");
62                         return 1;
63                 }
64                 sz = (long long)tmpsize;
65                 printf("%lld 512 byte blocks (BLKGETSIZE)\n", sz);
66         }
67         return 0;
68 }