2 * getsize.c --- get the size of a partition.
4 * Copyright (C) 1995, 1995 Theodore Ts'o.
5 * Copyright (C) 2003 VMware, Inc.
7 * Windows version of ext2fs_get_device_size by Chris Li, VMware.
10 * This file may be redistributed under the terms of the GNU Public
15 #define _LARGEFILE_SOURCE
16 #define _LARGEFILE64_SOURCE
26 #ifdef HAVE_LINUX_FD_H
27 #include <sys/ioctl.h>
30 #ifdef HAVE_SYS_DISKLABEL_H
31 #include <sys/ioctl.h>
32 #include <sys/disklabel.h>
33 #endif /* HAVE_SYS_DISKLABEL_H */
34 #ifdef HAVE_SYS_DISK_H
35 #include <sys/queue.h> /* for LIST_HEAD */
37 #endif /* HAVE_SYS_DISK_H */
39 #include <sys/utsname.h>
42 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
43 #define BLKGETSIZE _IO(0x12,96) /* return device size */
46 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
47 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
51 #include <sys/ioctl.h>
54 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
55 #endif /* APPLE_DARWIN */
60 #if defined(__CYGWIN__) || defined (WIN32)
64 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
68 PARTITION_INFORMATION pi;
71 LARGE_INTEGER filesize;
73 dev = CreateFile(file, GENERIC_READ,
74 FILE_SHARE_READ | FILE_SHARE_WRITE ,
75 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
77 if (dev == INVALID_HANDLE_VALUE)
79 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
80 &pi, sizeof(PARTITION_INFORMATION),
81 &pi, sizeof(PARTITION_INFORMATION),
84 *retblocks = pi.PartitionLength.QuadPart / blocksize;
86 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
87 &gi, sizeof(DISK_GEOMETRY),
88 &gi, sizeof(DISK_GEOMETRY),
91 *retblocks = gi.BytesPerSector *
93 gi.TracksPerCylinder *
94 gi.Cylinders.QuadPart / blocksize;
96 } else if (GetFileSizeEx(dev, &filesize)) {
97 *retblocks = filesize.QuadPart / blocksize;
106 static int valid_offset (int fd, ext2_loff_t offset)
110 if (ext2fs_llseek (fd, offset, 0) < 0)
112 if (read (fd, &ch, 1) < 1)
118 * Returns the number of blocks in a partition
120 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
124 int valid_blkgetsize64 = 1;
128 unsigned long long size64;
130 ext2_loff_t high, low;
132 struct floppy_struct this_floppy;
134 #ifdef HAVE_SYS_DISKLABEL_H
136 struct disklabel lab;
137 struct partition *pp;
139 #endif /* HAVE_SYS_DISKLABEL_H */
142 fd = open64(file, O_RDONLY);
144 fd = open(file, O_RDONLY);
149 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
150 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
151 if ((sizeof(*retblocks) < sizeof(unsigned long long))
152 && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
155 *retblocks = size64 / (blocksize / 512);
162 if ((uname(&ut) == 0) &&
163 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
164 (ut.release[2] < '6') && (ut.release[3] == '.')))
165 valid_blkgetsize64 = 0;
167 if (valid_blkgetsize64 &&
168 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
169 if ((sizeof(*retblocks) < sizeof(unsigned long long))
170 && ((size64 / blocksize) > 0xFFFFFFFF))
173 *retblocks = size64 / blocksize;
179 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
181 *retblocks = size / (blocksize / 512);
187 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
189 *retblocks = this_floppy.size / (blocksize / 512);
194 #ifdef HAVE_SYS_DISKLABEL_H
195 #if defined(DIOCGMEDIASIZE)
199 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
200 *retblocks = ms / blocksize;
204 #elif defined(DIOCGDINFO)
205 /* old disklabel interface */
206 part = strlen(file) - 1;
211 else if (ch >= 'a' && ch <= 'h')
216 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
217 pp = &lab.d_partitions[part];
220 *retblocks = pp->p_size / (blocksize / 512);
224 #endif /* defined(DIOCG*) */
225 #endif /* HAVE_SYS_DISKLABEL_H */
228 * OK, we couldn't figure it out by using a specialized ioctl,
229 * which is generally the best way. So do binary search to
230 * find the size of the partition.
233 for (high = 1024; valid_offset (fd, high); high *= 2)
235 while (low < high - 1)
237 const ext2_loff_t mid = (low + high) / 2;
239 if (valid_offset (fd, mid))
244 valid_offset (fd, 0);
246 *retblocks = (low + 1) / blocksize;
253 int main(int argc, char **argv)
259 fprintf(stderr, "Usage: %s device\n", argv[0]);
263 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
265 com_err(argv[0], retval,
266 "while calling ext2fs_get_device_size");
269 printf("Device %s has %d 1k blocks.\n", argv[1], blocks);