2 * getsize.c --- get the size of a partition.
4 * Copyright (C) 1995, 1995 Theodore Ts'o.
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
12 #ifndef _LARGEFILE_SOURCE
13 #define _LARGEFILE_SOURCE
15 #ifndef _LARGEFILE64_SOURCE
16 #define _LARGEFILE64_SOURCE
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
33 #ifdef HAVE_LINUX_FD_H
36 #ifdef HAVE_SYS_DISKLABEL_H
37 #include <sys/disklabel.h>
39 #ifdef HAVE_SYS_DISK_H
43 #include <sys/utsname.h>
50 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
51 #define BLKGETSIZE _IO(0x12,96) /* return device size */
54 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
55 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
59 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
60 #endif /* APPLE_DARWIN */
62 static int valid_offset(int fd, blkid_loff_t offset)
66 if (blkid_llseek(fd, offset, 0) < 0)
68 if (read(fd, &ch, 1) < 1)
74 * Returns the number of bytes in a partition
76 blkid_loff_t blkid_get_dev_size(int fd)
78 unsigned long long size64 __BLKID_ATTR((unused));
79 blkid_loff_t high, low;
81 #if defined DKIOCGETBLOCKCOUNT && defined DKIOCGETBLOCKSIZE /* For Apple Darwin */
84 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0 &&
85 ioctl(fd, DKIOCGETBLOCKSIZE, &size) >= 0) {
86 if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
87 (size64 * size) > 0xFFFFFFFF)
89 return (blkid_loff_t)size64 * size;
95 int valid_blkgetsize64 = 1;
99 if ((uname(&ut) == 0) &&
100 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
101 (ut.release[2] < '6') && (ut.release[3] == '.')))
102 valid_blkgetsize64 = 0;
104 if (valid_blkgetsize64 &&
105 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
106 if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
107 (size64 > 0xFFFFFFFF))
108 return 0; /* EFBIG */
112 #endif /* BLKGETSIZE64 */
118 if (ioctl(fd, BLKGETSIZE, &size) >= 0)
119 return (blkid_loff_t)size << 9;
123 /* tested on FreeBSD 6.1-RELEASE i386 */
124 #ifdef DIOCGMEDIASIZE
125 if (ioctl(fd, DIOCGMEDIASIZE, &size64) >= 0)
126 return (off_t)size64;
127 #endif /* DIOCGMEDIASIZE */
131 struct floppy_struct this_floppy;
133 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
134 return (blkid_loff_t)this_floppy.size << 9;
137 #if defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO)
140 struct disklabel lab;
141 struct partition *pp;
146 * This code works for FreeBSD 4.11 i386, except for the full
147 * device (such as /dev/ad0). It doesn't work properly for
148 * newer FreeBSD though. FreeBSD >= 5.0 should be covered by
149 * the DIOCGMEDIASIZE above however.
151 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
152 * character) devices, so we need to check for S_ISCHR, too.
154 if (fstat(fd, &st) >= 0 &&
155 blkidP_is_disk_device(st.st_mode))
156 part = st.st_rdev & 7;
158 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
159 pp = &lab.d_partitions[part];
161 return pp->p_size << 9;
164 #endif /* defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO) */
166 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
168 if (fstat64(fd, &st) == 0)
171 if (fstat(fd, &st) == 0)
173 if (S_ISREG(st.st_mode))
178 * OK, we couldn't figure it out by using a specialized ioctl,
179 * which is generally the best way. So do binary search to
180 * find the size of the partition.
183 for (high = 1024; valid_offset(fd, high); high *= 2)
185 while (low < high - 1) {
186 const blkid_loff_t mid = (low + high) / 2;
188 if (valid_offset(fd, mid))
197 int main(int argc, char **argv)
203 fprintf(stderr, "Usage: %s device\n"
204 "Determine the size of a device\n", argv[0]);
208 if ((fd = open(argv[1], O_RDONLY)) < 0)
211 bytes = blkid_get_dev_size(fd);
212 printf("Device %s has %lld 1k blocks.\n", argv[1],
213 (unsigned long long)bytes >> 10);