1 /* vi: set sw=4 ts=4: */
3 * getsize.c --- get the size of a partition.
5 * Copyright (C) 1995, 1995 Theodore Ts'o.
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
13 /* include this before sys/queues.h! */
24 #ifdef HAVE_SYS_IOCTL_H
25 #include <sys/ioctl.h>
27 #ifdef HAVE_LINUX_FD_H
30 #ifdef HAVE_SYS_DISKLABEL_H
31 #include <sys/disklabel.h>
34 #ifdef HAVE_SYS_DISK_H
35 #ifdef HAVE_SYS_QUEUE_H
36 #include <sys/queue.h> /* for LIST_HEAD */
41 #include <sys/utsname.h>
44 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
45 #define BLKGETSIZE _IO(0x12,96) /* return device size */
48 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
49 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
53 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
54 #endif /* APPLE_DARWIN */
56 static int valid_offset(int fd, blkid_loff_t offset)
60 if (blkid_llseek(fd, offset, 0) < 0)
62 if (read(fd, &ch, 1) < 1)
68 * Returns the number of blocks in a partition
70 blkid_loff_t blkid_get_dev_size(int fd)
72 int valid_blkgetsize64 = 1;
76 unsigned long long size64;
78 blkid_loff_t high, low;
80 struct floppy_struct this_floppy;
82 #ifdef HAVE_SYS_DISKLABEL_H
88 #endif /* HAVE_SYS_DISKLABEL_H */
90 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
91 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
92 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
93 && (size64 << 9 > 0xFFFFFFFF))
95 return (blkid_loff_t) size64 << 9;
101 if ((uname(&ut) == 0) &&
102 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
103 (ut.release[2] < '6') && (ut.release[3] == '.')))
104 valid_blkgetsize64 = 0;
106 if (valid_blkgetsize64 &&
107 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
108 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
109 && ((size64) > 0xFFFFFFFF))
110 return 0; /* EFBIG */
116 if (ioctl(fd, BLKGETSIZE, &size) >= 0)
117 return (blkid_loff_t)size << 9;
121 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
122 return (blkid_loff_t)this_floppy.size << 9;
124 #ifdef HAVE_SYS_DISKLABEL_H
127 * This should work in theory but I haven't tested it. Anyone
128 * on a BSD system want to test this for me? In the meantime,
129 * binary search mechanism should work just fine.
131 if ((fstat(fd, &st) >= 0) && S_ISBLK(st.st_mode))
132 part = st.st_rdev & 7;
133 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
134 pp = &lab.d_partitions[part];
136 return pp->p_size << 9;
139 #endif /* HAVE_SYS_DISKLABEL_H */
142 * OK, we couldn't figure it out by using a specialized ioctl,
143 * which is generally the best way. So do binary search to
144 * find the size of the partition.
147 for (high = 1024; valid_offset(fd, high); high *= 2)
149 while (low < high - 1)
151 const blkid_loff_t mid = (low + high) / 2;
153 if (valid_offset(fd, mid))
162 int main(int argc, char **argv)
168 fprintf(stderr, "Usage: %s device\n"
169 "Determine the size of a device\n", argv[0]);
173 if ((fd = open(argv[1], O_RDONLY)) < 0)
176 bytes = blkid_get_dev_size(fd);
177 printf("Device %s has %lld 1k blocks.\n", argv[1], bytes >> 10);