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 /* include this before sys/queues.h! */
23 #ifdef HAVE_SYS_IOCTL_H
24 #include <sys/ioctl.h>
26 #ifdef HAVE_LINUX_FD_H
29 #ifdef HAVE_SYS_DISKLABEL_H
30 #include <sys/disklabel.h>
33 #ifdef HAVE_SYS_DISK_H
34 #ifdef HAVE_SYS_QUEUE_H
35 #include <sys/queue.h> /* for LIST_HEAD */
40 #include <sys/utsname.h>
43 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
44 #define BLKGETSIZE _IO(0x12,96) /* return device size */
47 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
48 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
52 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
53 #endif /* APPLE_DARWIN */
55 static int valid_offset(int fd, blkid_loff_t offset)
59 if (blkid_llseek(fd, offset, 0) < 0)
61 if (read(fd, &ch, 1) < 1)
67 * Returns the number of blocks in a partition
69 blkid_loff_t blkid_get_dev_size(int fd)
71 int valid_blkgetsize64 = 1;
75 unsigned long long size64;
77 blkid_loff_t high, low;
79 struct floppy_struct this_floppy;
81 #ifdef HAVE_SYS_DISKLABEL_H
87 #endif /* HAVE_SYS_DISKLABEL_H */
89 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
90 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
91 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
92 && (size64 << 9 > 0xFFFFFFFF))
94 return (blkid_loff_t) size64 << 9;
100 if ((uname(&ut) == 0) &&
101 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
102 (ut.release[2] < '6') && (ut.release[3] == '.')))
103 valid_blkgetsize64 = 0;
105 if (valid_blkgetsize64 &&
106 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
107 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
108 && ((size64) > 0xFFFFFFFF))
109 return 0; /* EFBIG */
115 if (ioctl(fd, BLKGETSIZE, &size) >= 0)
116 return (blkid_loff_t)size << 9;
120 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
121 return (blkid_loff_t)this_floppy.size << 9;
123 #ifdef HAVE_SYS_DISKLABEL_H
126 * This should work in theory but I haven't tested it. Anyone
127 * on a BSD system want to test this for me? In the meantime,
128 * binary search mechanism should work just fine.
130 if ((fstat(fd, &st) >= 0) && S_ISBLK(st.st_mode))
131 part = st.st_rdev & 7;
132 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
133 pp = &lab.d_partitions[part];
135 return pp->p_size << 9;
138 #endif /* HAVE_SYS_DISKLABEL_H */
141 * OK, we couldn't figure it out by using a specialized ioctl,
142 * which is generally the best way. So do binary search to
143 * find the size of the partition.
146 for (high = 1024; valid_offset(fd, high); high *= 2)
148 while (low < high - 1)
150 const blkid_loff_t mid = (low + high) / 2;
152 if (valid_offset(fd, mid))
161 int main(int argc, char **argv)
167 fprintf(stderr, "Usage: %s device\n"
168 "Determine the size of a device\n", argv[0]);
172 if ((fd = open(argv[1], O_RDONLY)) < 0)
175 bytes = blkid_get_dev_size(fd);
176 printf("Device %s has %Ld 1k blocks.\n", argv[1], bytes >> 10);