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
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>
32 #ifdef HAVE_SYS_DISK_H
33 #ifdef HAVE_SYS_QUEUE_H
34 #include <sys/queue.h> /* for LIST_HEAD */
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 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
52 #endif /* APPLE_DARWIN */
57 #if defined(__CYGWIN__) || defined (WIN32)
61 #if (_WIN32_WINNT >= 0x0500)
62 #define HAVE_GET_FILE_SIZE_EX 1
65 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
69 PARTITION_INFORMATION pi;
72 #ifdef HAVE_GET_FILE_SIZE_EX
73 LARGE_INTEGER filesize;
76 #endif /* HAVE_GET_FILE_SIZE_EX */
78 dev = CreateFile(file, GENERIC_READ,
79 FILE_SHARE_READ | FILE_SHARE_WRITE ,
80 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
82 if (dev == INVALID_HANDLE_VALUE)
84 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
85 &pi, sizeof(PARTITION_INFORMATION),
86 &pi, sizeof(PARTITION_INFORMATION),
89 *retblocks = pi.PartitionLength.QuadPart / blocksize;
91 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
92 &gi, sizeof(DISK_GEOMETRY),
93 &gi, sizeof(DISK_GEOMETRY),
96 *retblocks = gi.BytesPerSector *
98 gi.TracksPerCylinder *
99 gi.Cylinders.QuadPart / blocksize;
101 #ifdef HAVE_GET_FILE_SIZE_EX
102 } else if (GetFileSizeEx(dev, &filesize)) {
103 *retblocks = filesize.QuadPart / blocksize;
107 filesize = GetFileSize(dev, NULL);
108 if (INVALID_FILE_SIZE != filesize) {
109 *retblocks = filesize / blocksize;
112 #endif /* HAVE_GET_FILE_SIZE_EX */
120 static int valid_offset (int fd, ext2_loff_t offset)
124 if (ext2fs_llseek (fd, offset, 0) < 0)
126 if (read (fd, &ch, 1) < 1)
132 * Returns the number of blocks in a partition
134 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
138 int valid_blkgetsize64 = 1;
142 unsigned long long size64;
144 ext2_loff_t high, low;
146 struct floppy_struct this_floppy;
148 #ifdef HAVE_SYS_DISKLABEL_H
150 struct disklabel lab;
151 struct partition *pp;
153 #endif /* HAVE_SYS_DISKLABEL_H */
156 fd = open64(file, O_RDONLY);
158 fd = open(file, O_RDONLY);
163 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
164 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
165 if ((sizeof(*retblocks) < sizeof(unsigned long long))
166 && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
169 *retblocks = size64 / (blocksize / 512);
176 if ((uname(&ut) == 0) &&
177 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
178 (ut.release[2] < '6') && (ut.release[3] == '.')))
179 valid_blkgetsize64 = 0;
181 if (valid_blkgetsize64 &&
182 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
183 if ((sizeof(*retblocks) < sizeof(unsigned long long))
184 && ((size64 / blocksize) > 0xFFFFFFFF))
187 *retblocks = size64 / blocksize;
193 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
195 *retblocks = size / (blocksize / 512);
201 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
203 *retblocks = this_floppy.size / (blocksize / 512);
208 #ifdef HAVE_SYS_DISKLABEL_H
209 #if defined(DIOCGMEDIASIZE)
213 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
214 *retblocks = ms / blocksize;
218 #elif defined(DIOCGDINFO)
219 /* old disklabel interface */
220 part = strlen(file) - 1;
225 else if (ch >= 'a' && ch <= 'h')
230 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
231 pp = &lab.d_partitions[part];
234 *retblocks = pp->p_size / (blocksize / 512);
238 #endif /* defined(DIOCG*) */
239 #endif /* HAVE_SYS_DISKLABEL_H */
242 * OK, we couldn't figure it out by using a specialized ioctl,
243 * which is generally the best way. So do binary search to
244 * find the size of the partition.
247 for (high = 1024; valid_offset (fd, high); high *= 2)
249 while (low < high - 1)
251 const ext2_loff_t mid = (low + high) / 2;
253 if (valid_offset (fd, mid))
258 valid_offset (fd, 0);
261 if ((sizeof(*retblocks) < sizeof(unsigned long long))
262 && ((size64 / blocksize) > 0xFFFFFFFF))
264 *retblocks = size64 / blocksize;
271 int main(int argc, char **argv)
277 fprintf(stderr, "Usage: %s device\n", argv[0]);
281 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
283 com_err(argv[0], retval,
284 "while calling ext2fs_get_device_size");
287 printf("Device %s has %d 1k blocks.\n", argv[1], blocks);