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 Library
11 * General Public License, version 2.
15 #ifndef _LARGEFILE_SOURCE
16 #define _LARGEFILE_SOURCE
18 #ifndef _LARGEFILE64_SOURCE
19 #define _LARGEFILE64_SOURCE
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
34 #ifdef HAVE_LINUX_FD_H
37 #ifdef HAVE_SYS_DISKLABEL_H
38 #include <sys/disklabel.h>
40 #ifdef HAVE_SYS_DISK_H
44 #include <sys/utsname.h>
51 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
52 #define BLKGETSIZE _IO(0x12,96) /* return device size */
55 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
56 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
60 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
61 #endif /* APPLE_DARWIN */
66 #if defined(__CYGWIN__) || defined (WIN32)
70 #if (_WIN32_WINNT >= 0x0500)
71 #define HAVE_GET_FILE_SIZE_EX 1
74 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
78 PARTITION_INFORMATION pi;
81 #ifdef HAVE_GET_FILE_SIZE_EX
82 LARGE_INTEGER filesize;
85 #endif /* HAVE_GET_FILE_SIZE_EX */
87 dev = CreateFile(file, GENERIC_READ,
88 FILE_SHARE_READ | FILE_SHARE_WRITE ,
89 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
91 if (dev == INVALID_HANDLE_VALUE)
93 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
94 &pi, sizeof(PARTITION_INFORMATION),
95 &pi, sizeof(PARTITION_INFORMATION),
98 *retblocks = pi.PartitionLength.QuadPart / blocksize;
100 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
101 &gi, sizeof(DISK_GEOMETRY),
102 &gi, sizeof(DISK_GEOMETRY),
105 *retblocks = gi.BytesPerSector *
107 gi.TracksPerCylinder *
108 gi.Cylinders.QuadPart / blocksize;
110 #ifdef HAVE_GET_FILE_SIZE_EX
111 } else if (GetFileSizeEx(dev, &filesize)) {
112 *retblocks = filesize.QuadPart / blocksize;
116 filesize = GetFileSize(dev, NULL);
117 if (INVALID_FILE_SIZE != filesize) {
118 *retblocks = filesize / blocksize;
121 #endif /* HAVE_GET_FILE_SIZE_EX */
129 static int valid_offset (int fd, ext2_loff_t offset)
133 if (ext2fs_llseek (fd, offset, 0) < 0)
135 if (read (fd, &ch, 1) < 1)
141 * Returns the number of blocks in a partition
143 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
147 unsigned long long size64;
148 ext2_loff_t high, low;
150 fd = ext2fs_open_file(file, O_RDONLY, 0);
154 #if defined DKIOCGETBLOCKCOUNT && defined DKIOCGETBLOCKSIZE /* For Apple Darwin */
157 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0 &&
158 ioctl(fd, DKIOCGETBLOCKSIZE, &size) >= 0) {
159 *retblocks = size64 * size / blocksize;
166 int valid_blkgetsize64 = 1;
170 if ((uname(&ut) == 0) &&
171 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
172 (ut.release[2] < '6') && (ut.release[3] == '.')))
173 valid_blkgetsize64 = 0;
175 if (valid_blkgetsize64 &&
176 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
177 *retblocks = size64 / blocksize;
181 #endif /* BLKGETSIZE64 */
187 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
188 *retblocks = size / (blocksize / 512);
196 struct floppy_struct this_floppy;
198 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
199 *retblocks = this_floppy.size / (blocksize / 512);
205 #ifdef HAVE_SYS_DISKLABEL_H
208 struct disklabel lab;
209 struct partition *pp;
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];
236 *retblocks = pp->p_size / (blocksize / 512);
240 #endif /* defined(DIOCG*) */
242 #endif /* HAVE_SYS_DISKLABEL_H */
245 ext2fs_struct_stat st;
247 if (ext2fs_fstat(fd, &st) == 0)
248 if (S_ISREG(st.st_mode)) {
249 *retblocks = st.st_size / blocksize;
255 * OK, we couldn't figure it out by using a specialized ioctl,
256 * which is generally the best way. So do binary search to
257 * find the size of the partition.
260 for (high = 1024; valid_offset(fd, high); high *= 2)
262 while (low < high - 1) {
263 const ext2_loff_t mid = (low + high) / 2;
265 if (valid_offset (fd, mid))
272 *retblocks = size64 / blocksize;
280 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
286 retval = ext2fs_get_device_size2(file, blocksize, &blocks);
289 if (blocks >= (1ULL << 32))
291 *retblocks = (blk_t) blocks;
296 int main(int argc, char **argv)
302 fprintf(stderr, "Usage: %s device\n", argv[0]);
306 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
308 com_err(argv[0], retval,
309 "while calling ext2fs_get_device_size");
312 printf("Device %s has %u 1k blocks.\n", argv[1], blocks);