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 #define _LARGEFILE_SOURCE
13 #define _LARGEFILE64_SOURCE
15 /* include this before sys/queues.h! */
26 #ifdef HAVE_SYS_IOCTL_H
27 #include <sys/ioctl.h>
29 #ifdef HAVE_LINUX_FD_H
32 #ifdef HAVE_SYS_DISKLABEL_H
33 #include <sys/disklabel.h>
35 #ifdef HAVE_SYS_DISK_H
36 #ifdef HAVE_SYS_QUEUE_H
37 #include <sys/queue.h> /* for LIST_HEAD */
42 #include <sys/utsname.h>
49 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
50 #define BLKGETSIZE _IO(0x12,96) /* return device size */
53 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
54 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
58 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
59 #endif /* APPLE_DARWIN */
61 static int valid_offset(int fd, blkid_loff_t offset)
65 if (blkid_llseek(fd, offset, 0) < 0)
67 if (read(fd, &ch, 1) < 1)
73 * Returns the number of bytes in a partition
75 blkid_loff_t blkid_get_dev_size(int fd)
77 int valid_blkgetsize64 = 1;
81 unsigned long long size64;
83 blkid_loff_t high, low;
85 struct floppy_struct this_floppy;
87 #ifdef HAVE_SYS_DISKLABEL_H
93 #endif /* HAVE_SYS_DISKLABEL_H */
95 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
96 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
97 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
98 && (size64 << 9 > 0xFFFFFFFF))
100 return (blkid_loff_t) size64 << 9;
106 if ((uname(&ut) == 0) &&
107 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
108 (ut.release[2] < '6') && (ut.release[3] == '.')))
109 valid_blkgetsize64 = 0;
111 if (valid_blkgetsize64 &&
112 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
113 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
114 && ((size64) > 0xFFFFFFFF))
115 return 0; /* EFBIG */
121 if (ioctl(fd, BLKGETSIZE, &size) >= 0)
122 return (blkid_loff_t)size << 9;
125 /* tested on FreeBSD 6.1-RELEASE i386 */
126 #ifdef DIOCGMEDIASIZE
127 if (ioctl(fd, DIOCGMEDIASIZE, &size64) >= 0)
128 return (off_t)size64;
129 #endif /* DIOCGMEDIASIZE */
132 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
133 return (blkid_loff_t)this_floppy.size << 9;
135 #ifdef HAVE_SYS_DISKLABEL_H
137 * This code works for FreeBSD 4.11 i386, except for the full device
138 * (such as /dev/ad0). It doesn't work properly for newer FreeBSD
139 * though. FreeBSD >= 5.0 should be covered by the DIOCGMEDIASIZE
142 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
143 * character) devices, so we need to check for S_ISCHR, too.
145 if ((fstat(fd, &st) >= 0) && (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
146 part = st.st_rdev & 7;
147 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
148 pp = &lab.d_partitions[part];
150 return pp->p_size << 9;
152 #endif /* HAVE_SYS_DISKLABEL_H */
156 if (fstat64(fd, &st) == 0)
159 if (fstat(fd, &st) == 0)
161 if (S_ISREG(st.st_mode))
167 * OK, we couldn't figure it out by using a specialized ioctl,
168 * which is generally the best way. So do binary search to
169 * find the size of the partition.
172 for (high = 1024; valid_offset(fd, high); high *= 2)
174 while (low < high - 1)
176 const blkid_loff_t mid = (low + high) / 2;
178 if (valid_offset(fd, mid))
187 int main(int argc, char **argv)
193 fprintf(stderr, "Usage: %s device\n"
194 "Determine the size of a device\n", argv[0]);
198 if ((fd = open(argv[1], O_RDONLY)) < 0)
201 bytes = blkid_get_dev_size(fd);
202 printf("Device %s has %Ld 1k blocks.\n", argv[1],
203 (unsigned long long) bytes >> 10);