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>
45 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
46 #define BLKGETSIZE _IO(0x12,96) /* return device size */
49 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
50 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
54 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
55 #endif /* APPLE_DARWIN */
60 #if defined(__CYGWIN__) || defined (WIN32)
64 #if (_WIN32_WINNT >= 0x0500)
65 #define HAVE_GET_FILE_SIZE_EX 1
68 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
72 PARTITION_INFORMATION pi;
75 #ifdef HAVE_GET_FILE_SIZE_EX
76 LARGE_INTEGER filesize;
79 #endif /* HAVE_GET_FILE_SIZE_EX */
81 dev = CreateFile(file, GENERIC_READ,
82 FILE_SHARE_READ | FILE_SHARE_WRITE ,
83 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
85 if (dev == INVALID_HANDLE_VALUE)
87 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
88 &pi, sizeof(PARTITION_INFORMATION),
89 &pi, sizeof(PARTITION_INFORMATION),
92 *retblocks = pi.PartitionLength.QuadPart / blocksize;
94 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
95 &gi, sizeof(DISK_GEOMETRY),
96 &gi, sizeof(DISK_GEOMETRY),
99 *retblocks = gi.BytesPerSector *
101 gi.TracksPerCylinder *
102 gi.Cylinders.QuadPart / blocksize;
104 #ifdef HAVE_GET_FILE_SIZE_EX
105 } else if (GetFileSizeEx(dev, &filesize)) {
106 *retblocks = filesize.QuadPart / blocksize;
110 filesize = GetFileSize(dev, NULL);
111 if (INVALID_FILE_SIZE != filesize) {
112 *retblocks = filesize / blocksize;
115 #endif /* HAVE_GET_FILE_SIZE_EX */
123 static int valid_offset (int fd, ext2_loff_t offset)
127 if (ext2fs_llseek (fd, offset, 0) < 0)
129 if (read (fd, &ch, 1) < 1)
135 * Returns the number of blocks in a partition
137 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
141 int valid_blkgetsize64 = 1;
145 unsigned long long size64;
147 ext2_loff_t high, low;
149 struct floppy_struct this_floppy;
151 #ifdef HAVE_SYS_DISKLABEL_H
153 struct disklabel lab;
154 struct partition *pp;
156 #endif /* HAVE_SYS_DISKLABEL_H */
159 fd = open64(file, O_RDONLY);
161 fd = open(file, O_RDONLY);
166 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
167 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
168 if ((sizeof(*retblocks) < sizeof(unsigned long long))
169 && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
172 *retblocks = size64 / (blocksize / 512);
179 if ((uname(&ut) == 0) &&
180 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
181 (ut.release[2] < '6') && (ut.release[3] == '.')))
182 valid_blkgetsize64 = 0;
184 if (valid_blkgetsize64 &&
185 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
186 if ((sizeof(*retblocks) < sizeof(unsigned long long))
187 && ((size64 / blocksize) > 0xFFFFFFFF))
190 *retblocks = size64 / blocksize;
196 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
198 *retblocks = size / (blocksize / 512);
204 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
206 *retblocks = this_floppy.size / (blocksize / 512);
211 #ifdef HAVE_SYS_DISKLABEL_H
212 #if defined(DIOCGMEDIASIZE)
216 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
217 *retblocks = ms / blocksize;
221 #elif defined(DIOCGDINFO)
222 /* old disklabel interface */
223 part = strlen(file) - 1;
228 else if (ch >= 'a' && ch <= 'h')
233 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
234 pp = &lab.d_partitions[part];
237 *retblocks = pp->p_size / (blocksize / 512);
241 #endif /* defined(DIOCG*) */
242 #endif /* HAVE_SYS_DISKLABEL_H */
245 * OK, we couldn't figure it out by using a specialized ioctl,
246 * which is generally the best way. So do binary search to
247 * find the size of the partition.
250 for (high = 1024; valid_offset (fd, high); high *= 2)
252 while (low < high - 1)
254 const ext2_loff_t mid = (low + high) / 2;
256 if (valid_offset (fd, mid))
261 valid_offset (fd, 0);
263 *retblocks = (low + 1) / blocksize;
270 int main(int argc, char **argv)
276 fprintf(stderr, "Usage: %s device\n", argv[0]);
280 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
282 com_err(argv[0], retval,
283 "while calling ext2fs_get_device_size");
286 printf("Device %s has %d 1k blocks.\n", argv[1], blocks);