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)) {
177 *retblocks = size64 / (blocksize / 512);
184 if ((uname(&ut) == 0) &&
185 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
186 (ut.release[2] < '6') && (ut.release[3] == '.')))
187 valid_blkgetsize64 = 0;
189 if (valid_blkgetsize64 &&
190 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
191 if ((sizeof(*retblocks) < sizeof(unsigned long long)) &&
192 ((size64 / blocksize) > 0xFFFFFFFF)) {
196 *retblocks = size64 / blocksize;
202 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
203 *retblocks = size / (blocksize / 512);
209 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
210 *retblocks = this_floppy.size / (blocksize / 512);
215 #ifdef HAVE_SYS_DISKLABEL_H
216 #if defined(DIOCGMEDIASIZE)
220 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
221 *retblocks = ms / blocksize;
225 #elif defined(DIOCGDINFO)
226 /* old disklabel interface */
227 part = strlen(file) - 1;
232 else if (ch >= 'a' && ch <= 'h')
237 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
238 pp = &lab.d_partitions[part];
240 *retblocks = pp->p_size / (blocksize / 512);
244 #endif /* defined(DIOCG*) */
245 #endif /* HAVE_SYS_DISKLABEL_H */
250 if (fstat64(fd, &st) == 0)
253 if (fstat(fd, &st) == 0)
255 if (S_ISREG(st.st_mode)) {
256 if ((sizeof(*retblocks) < sizeof(unsigned long long)) &&
257 ((st.st_size / blocksize) > 0xFFFFFFFF)) {
261 *retblocks = st.st_size / blocksize;
267 * OK, we couldn't figure it out by using a specialized ioctl,
268 * which is generally the best way. So do binary search to
269 * find the size of the partition.
272 for (high = 1024; valid_offset (fd, high); high *= 2)
274 while (low < high - 1)
276 const ext2_loff_t mid = (low + high) / 2;
278 if (valid_offset (fd, mid))
283 valid_offset (fd, 0);
285 if ((sizeof(*retblocks) < sizeof(unsigned long long))
286 && ((size64 / blocksize) > 0xFFFFFFFF)) {
290 *retblocks = size64 / blocksize;
299 int main(int argc, char **argv)
305 fprintf(stderr, "Usage: %s device\n", argv[0]);
309 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
311 com_err(argv[0], retval,
312 "while calling ext2fs_get_device_size");
315 printf("Device %s has %u 1k blocks.\n", argv[1], blocks);