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>
36 #ifdef HAVE_SYS_DISK_H
37 #ifdef HAVE_SYS_QUEUE_H
38 #include <sys/queue.h> /* for LIST_HEAD */
43 #include <sys/utsname.h>
46 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
47 #define BLKGETSIZE _IO(0x12,96) /* return device size */
50 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
51 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
55 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
56 #endif /* APPLE_DARWIN */
58 static int valid_offset(int fd, blkid_loff_t offset)
62 if (blkid_llseek(fd, offset, 0) < 0)
64 if (read(fd, &ch, 1) < 1)
70 * Returns the number of blocks in a partition
72 blkid_loff_t blkid_get_dev_size(int fd)
74 int valid_blkgetsize64 = 1;
78 unsigned long long size64;
80 blkid_loff_t high, low;
82 struct floppy_struct this_floppy;
84 #ifdef HAVE_SYS_DISKLABEL_H
90 #endif /* HAVE_SYS_DISKLABEL_H */
92 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
93 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
94 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
95 && (size64 << 9 > 0xFFFFFFFF))
97 return (blkid_loff_t) size64 << 9;
103 if ((uname(&ut) == 0) &&
104 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
105 (ut.release[2] < '6') && (ut.release[3] == '.')))
106 valid_blkgetsize64 = 0;
108 if (valid_blkgetsize64 &&
109 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
110 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
111 && ((size64) > 0xFFFFFFFF))
112 return 0; /* EFBIG */
118 if (ioctl(fd, BLKGETSIZE, &size) >= 0)
119 return (blkid_loff_t)size << 9;
123 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
124 return (blkid_loff_t)this_floppy.size << 9;
126 #ifdef HAVE_SYS_DISKLABEL_H
129 * This should work in theory but I haven't tested it. Anyone
130 * on a BSD system want to test this for me? In the meantime,
131 * binary search mechanism should work just fine.
133 if ((fstat(fd, &st) >= 0) && S_ISBLK(st.st_mode))
134 part = st.st_rdev & 7;
135 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
136 pp = &lab.d_partitions[part];
138 return pp->p_size << 9;
141 #endif /* HAVE_SYS_DISKLABEL_H */
144 * OK, we couldn't figure it out by using a specialized ioctl,
145 * which is generally the best way. So do binary search to
146 * find the size of the partition.
149 for (high = 1024; valid_offset(fd, high); high *= 2)
151 while (low < high - 1)
153 const blkid_loff_t mid = (low + high) / 2;
155 if (valid_offset(fd, mid))
164 int main(int argc, char **argv)
170 fprintf(stderr, "Usage: %s device\n"
171 "Determine the size of a device\n", argv[0]);
175 if ((fd = open(argv[1], O_RDONLY)) < 0)
178 bytes = blkid_get_dev_size(fd);
179 printf("Device %s has %Ld 1k blocks.\n", argv[1], bytes >> 10);