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_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>
35 #ifdef HAVE_SYS_DISK_H
36 #ifdef HAVE_SYS_QUEUE_H
37 #include <sys/queue.h> /* for LIST_HEAD */
42 #include <sys/utsname.h>
49 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
50 #define BLKGETSIZE _IO(0x12,96) /* return device size */
53 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
54 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
58 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
59 #endif /* APPLE_DARWIN */
64 #if defined(__CYGWIN__) || defined (WIN32)
68 #if (_WIN32_WINNT >= 0x0500)
69 #define HAVE_GET_FILE_SIZE_EX 1
72 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
76 PARTITION_INFORMATION pi;
79 #ifdef HAVE_GET_FILE_SIZE_EX
80 LARGE_INTEGER filesize;
83 #endif /* HAVE_GET_FILE_SIZE_EX */
85 dev = CreateFile(file, GENERIC_READ,
86 FILE_SHARE_READ | FILE_SHARE_WRITE ,
87 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
89 if (dev == INVALID_HANDLE_VALUE)
91 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
92 &pi, sizeof(PARTITION_INFORMATION),
93 &pi, sizeof(PARTITION_INFORMATION),
96 *retblocks = pi.PartitionLength.QuadPart / blocksize;
98 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
99 &gi, sizeof(DISK_GEOMETRY),
100 &gi, sizeof(DISK_GEOMETRY),
103 *retblocks = gi.BytesPerSector *
105 gi.TracksPerCylinder *
106 gi.Cylinders.QuadPart / blocksize;
108 #ifdef HAVE_GET_FILE_SIZE_EX
109 } else if (GetFileSizeEx(dev, &filesize)) {
110 *retblocks = filesize.QuadPart / blocksize;
114 filesize = GetFileSize(dev, NULL);
115 if (INVALID_FILE_SIZE != filesize) {
116 *retblocks = filesize / blocksize;
119 #endif /* HAVE_GET_FILE_SIZE_EX */
127 static int valid_offset (int fd, ext2_loff_t offset)
131 if (ext2fs_llseek (fd, offset, 0) < 0)
133 if (read (fd, &ch, 1) < 1)
139 * Returns the number of blocks in a partition
141 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
145 int valid_blkgetsize64 = 1;
149 unsigned long long size64;
151 ext2_loff_t high, low;
153 struct floppy_struct this_floppy;
155 #ifdef HAVE_SYS_DISKLABEL_H
157 struct disklabel lab;
158 struct partition *pp;
160 #endif /* HAVE_SYS_DISKLABEL_H */
163 fd = open64(file, O_RDONLY);
165 fd = open(file, O_RDONLY);
170 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
171 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
172 if ((sizeof(*retblocks) < sizeof(unsigned long long))
173 && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
175 *retblocks = size64 / (blocksize / 512);
182 if ((uname(&ut) == 0) &&
183 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
184 (ut.release[2] < '6') && (ut.release[3] == '.')))
185 valid_blkgetsize64 = 0;
187 if (valid_blkgetsize64 &&
188 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
189 if ((sizeof(*retblocks) < sizeof(unsigned long long)) &&
190 ((size64 / blocksize) > 0xFFFFFFFF)) {
194 *retblocks = size64 / blocksize;
200 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
201 *retblocks = size / (blocksize / 512);
207 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
208 *retblocks = this_floppy.size / (blocksize / 512);
213 #ifdef HAVE_SYS_DISKLABEL_H
214 #if defined(DIOCGMEDIASIZE)
218 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
219 *retblocks = ms / blocksize;
223 #elif defined(DIOCGDINFO)
224 /* old disklabel interface */
225 part = strlen(file) - 1;
230 else if (ch >= 'a' && ch <= 'h')
235 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
236 pp = &lab.d_partitions[part];
238 *retblocks = pp->p_size / (blocksize / 512);
242 #endif /* defined(DIOCG*) */
243 #endif /* HAVE_SYS_DISKLABEL_H */
248 if (fstat64(fd, &st) == 0)
251 if (fstat(fd, &st) == 0)
253 if (S_ISREG(st.st_mode)) {
254 *retblocks = st.st_size / blocksize;
260 * OK, we couldn't figure it out by using a specialized ioctl,
261 * which is generally the best way. So do binary search to
262 * find the size of the partition.
265 for (high = 1024; valid_offset (fd, high); high *= 2)
267 while (low < high - 1)
269 const ext2_loff_t mid = (low + high) / 2;
271 if (valid_offset (fd, mid))
276 valid_offset (fd, 0);
278 if ((sizeof(*retblocks) < sizeof(unsigned long long))
279 && ((size64 / blocksize) > 0xFFFFFFFF))
281 *retblocks = size64 / blocksize;
290 int main(int argc, char **argv)
296 fprintf(stderr, "Usage: %s device\n", argv[0]);
300 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
302 com_err(argv[0], retval,
303 "while calling ext2fs_get_device_size");
306 printf("Device %s has %d 1k blocks.\n", argv[1], blocks);