1/*
2 * getsize.c --- get the size of a partition.
3 *
4 * Copyright (C) 1995, 1995 Theodore Ts'o.
5 * Copyright (C) 2003 VMware, Inc.
6 *
7 * Windows version of ext2fs_get_device_size by Chris Li, VMware.
8 *
9 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Library
11 * General Public License, version 2.
12 * %End-Header%
13 */
14
15#define _LARGEFILE_SOURCE
16#define _LARGEFILE64_SOURCE
17
18#include <stdio.h>
19#if HAVE_UNISTD_H
20#include <unistd.h>
21#endif
22#if HAVE_ERRNO_H
23#include <errno.h>
24#endif
25#include <fcntl.h>
26#ifdef HAVE_SYS_IOCTL_H
27#include <sys/ioctl.h>
28#endif
29#ifdef HAVE_LINUX_FD_H
30#include <linux/fd.h>
31#endif
32#ifdef HAVE_SYS_DISKLABEL_H
33#include <sys/disklabel.h>
34#endif
35#ifdef HAVE_SYS_DISK_H
36#ifdef HAVE_SYS_QUEUE_H
37#include <sys/queue.h> /* for LIST_HEAD */
38#endif
39#include <sys/disk.h>
40#endif
41#ifdef __linux__
42#include <sys/utsname.h>
43#endif
44#if HAVE_SYS_STAT_H
45#include <sys/stat.h>
46#endif
47#include <ctype.h>
48
49#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
50#define BLKGETSIZE _IO(0x12,96)	/* return device size */
51#endif
52
53#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
54#define BLKGETSIZE64 _IOR(0x12,114,size_t)	/* return device size in bytes (u64 *arg) */
55#endif
56
57#ifdef APPLE_DARWIN
58#define BLKGETSIZE DKIOCGETBLOCKCOUNT32
59#endif /* APPLE_DARWIN */
60
61#include "ext2_fs.h"
62#include "ext2fs.h"
63
64#if defined(__CYGWIN__) || defined (WIN32)
65#include "windows.h"
66#include "winioctl.h"
67
68#if (_WIN32_WINNT >= 0x0500)
69#define HAVE_GET_FILE_SIZE_EX 1
70#endif
71
72errcode_t ext2fs_get_device_size(const char *file, int blocksize,
73				 blk_t *retblocks)
74{
75	HANDLE dev;
76	PARTITION_INFORMATION pi;
77	DISK_GEOMETRY gi;
78	DWORD retbytes;
79#ifdef HAVE_GET_FILE_SIZE_EX
80	LARGE_INTEGER filesize;
81#else
82	DWORD filesize;
83#endif /* HAVE_GET_FILE_SIZE_EX */
84
85	dev = CreateFile(file, GENERIC_READ,
86			 FILE_SHARE_READ | FILE_SHARE_WRITE ,
87                	 NULL,  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,  NULL);
88
89	if (dev == INVALID_HANDLE_VALUE)
90		return EBADF;
91	if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
92			    &pi, sizeof(PARTITION_INFORMATION),
93			    &pi, sizeof(PARTITION_INFORMATION),
94			    &retbytes, NULL)) {
95
96		*retblocks = pi.PartitionLength.QuadPart / blocksize;
97
98	} else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
99				&gi, sizeof(DISK_GEOMETRY),
100				&gi, sizeof(DISK_GEOMETRY),
101				&retbytes, NULL)) {
102
103		*retblocks = gi.BytesPerSector *
104			     gi.SectorsPerTrack *
105			     gi.TracksPerCylinder *
106			     gi.Cylinders.QuadPart / blocksize;
107
108#ifdef HAVE_GET_FILE_SIZE_EX
109	} else if (GetFileSizeEx(dev, &filesize)) {
110		*retblocks = filesize.QuadPart / blocksize;
111	}
112#else
113	} else {
114		filesize = GetFileSize(dev, NULL);
115		if (INVALID_FILE_SIZE != filesize) {
116			*retblocks = filesize / blocksize;
117		}
118	}
119#endif /* HAVE_GET_FILE_SIZE_EX */
120
121	CloseHandle(dev);
122	return 0;
123}
124
125#else
126
127static int valid_offset (int fd, ext2_loff_t offset)
128{
129	char ch;
130
131	if (ext2fs_llseek (fd, offset, 0) < 0)
132		return 0;
133	if (read (fd, &ch, 1) < 1)
134		return 0;
135	return 1;
136}
137
138/*
139 * Returns the number of blocks in a partition
140 */
141errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
142				  blk64_t *retblocks)
143{
144	int	fd, rc = 0;
145	unsigned long long size64;
146	ext2_loff_t high, low;
147
148	fd = ext2fs_open_file(file, O_RDONLY, 0);
149	if (fd < 0)
150		return errno;
151
152#ifdef DKIOCGETBLOCKCOUNT	/* For Apple Darwin */
153	if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
154		*retblocks = size64 / (blocksize / 512);
155		goto out;
156	}
157#endif
158
159#ifdef BLKGETSIZE64
160	{
161		int valid_blkgetsize64 = 1;
162#ifdef __linux__
163		struct utsname ut;
164
165		if ((uname(&ut) == 0) &&
166		    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
167		     (ut.release[2] < '6') && (ut.release[3] == '.')))
168			valid_blkgetsize64 = 0;
169#endif
170		if (valid_blkgetsize64 &&
171		    ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
172			*retblocks = size64 / blocksize;
173			goto out;
174		}
175	}
176#endif /* BLKGETSIZE64 */
177
178#ifdef BLKGETSIZE
179	{
180		unsigned long	size;
181
182		if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
183			*retblocks = size / (blocksize / 512);
184			goto out;
185		}
186	}
187#endif
188
189#ifdef FDGETPRM
190	{
191		struct floppy_struct this_floppy;
192
193		if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
194			*retblocks = this_floppy.size / (blocksize / 512);
195			goto out;
196		}
197	}
198#endif
199
200#ifdef HAVE_SYS_DISKLABEL_H
201	{
202		int part;
203		struct disklabel lab;
204		struct partition *pp;
205		char ch;
206
207#if defined(DIOCGMEDIASIZE)
208		{
209			off_t ms;
210			u_int bs;
211			if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
212				*retblocks = ms / blocksize;
213				goto out;
214			}
215		}
216#elif defined(DIOCGDINFO)
217		/* old disklabel interface */
218		part = strlen(file) - 1;
219		if (part >= 0) {
220			ch = file[part];
221			if (isdigit(ch))
222				part = 0;
223			else if (ch >= 'a' && ch <= 'h')
224				part = ch - 'a';
225			else
226				part = -1;
227		}
228		if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
229			pp = &lab.d_partitions[part];
230			if (pp->p_size) {
231				*retblocks = pp->p_size / (blocksize / 512);
232				goto out;
233			}
234		}
235#endif /* defined(DIOCG*) */
236	}
237#endif /* HAVE_SYS_DISKLABEL_H */
238
239	{
240		ext2fs_struct_stat st;
241
242		if (ext2fs_fstat(fd, &st) == 0)
243			if (S_ISREG(st.st_mode)) {
244				*retblocks = st.st_size / blocksize;
245				goto out;
246			}
247	}
248
249	/*
250	 * OK, we couldn't figure it out by using a specialized ioctl,
251	 * which is generally the best way.  So do binary search to
252	 * find the size of the partition.
253	 */
254	low = 0;
255	for (high = 1024; valid_offset(fd, high); high *= 2)
256		low = high;
257	while (low < high - 1) {
258		const ext2_loff_t mid = (low + high) / 2;
259
260		if (valid_offset (fd, mid))
261			low = mid;
262		else
263			high = mid;
264	}
265	valid_offset(fd, 0);
266	size64 = low + 1;
267	*retblocks = size64 / blocksize;
268out:
269	close(fd);
270	return rc;
271}
272
273errcode_t ext2fs_get_device_size(const char *file, int blocksize,
274				 blk_t *retblocks)
275{
276	errcode_t retval;
277	blk64_t	blocks;
278
279	retval = ext2fs_get_device_size2(file, blocksize, &blocks);
280	if (retval)
281		return retval;
282	if (blocks >= (1ULL << 32))
283		return EFBIG;
284	*retblocks = (blk_t) blocks;
285	return 0;
286}
287
288#endif /* WIN32 */
289
290#ifdef DEBUG
291int main(int argc, char **argv)
292{
293	blk_t	blocks;
294	int	retval;
295
296	if (argc < 2) {
297		fprintf(stderr, "Usage: %s device\n", argv[0]);
298		exit(1);
299	}
300
301	retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
302	if (retval) {
303		com_err(argv[0], retval,
304			"while calling ext2fs_get_device_size");
305		exit(1);
306	}
307	printf("Device %s has %u 1k blocks.\n", argv[1], blocks);
308	exit(0);
309}
310#endif
311