1/*
2 * getsize.c --- get the size of a partition.
3 *
4 * Copyright (C) 1995, 1995 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 * %End-Header%
10 */
11
12#define _LARGEFILE_SOURCE
13#define _LARGEFILE64_SOURCE
14
15/* include this before sys/queues.h! */
16#include "blkidP.h"
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
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
61static int valid_offset(int fd, blkid_loff_t offset)
62{
63	char ch;
64
65	if (blkid_llseek(fd, offset, 0) < 0)
66		return 0;
67	if (read(fd, &ch, 1) < 1)
68		return 0;
69	return 1;
70}
71
72/*
73 * Returns the number of bytes in a partition
74 */
75blkid_loff_t blkid_get_dev_size(int fd)
76{
77	unsigned long long size64;
78	blkid_loff_t high, low;
79
80#ifdef DKIOCGETBLOCKCOUNT	/* For Apple Darwin */
81	if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
82		if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
83		    (size64 << 9) > 0xFFFFFFFF)
84			return 0; /* EFBIG */
85		return (blkid_loff_t)size64 << 9;
86	}
87#endif
88
89#ifdef BLKGETSIZE64
90	{
91		int valid_blkgetsize64 = 1;
92#ifdef __linux__
93		struct		utsname ut;
94
95		if ((uname(&ut) == 0) &&
96		    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
97		     (ut.release[2] < '6') && (ut.release[3] == '.')))
98			valid_blkgetsize64 = 0;
99#endif
100		if (valid_blkgetsize64 &&
101		    ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
102			if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
103			    (size64 > 0xFFFFFFFF))
104				return 0; /* EFBIG */
105			return size64;
106		}
107	}
108#endif /* BLKGETSIZE64 */
109
110#ifdef BLKGETSIZE
111	{
112		unsigned long size;
113
114		if (ioctl(fd, BLKGETSIZE, &size) >= 0)
115			return (blkid_loff_t)size << 9;
116	}
117#endif
118
119/* tested on FreeBSD 6.1-RELEASE i386 */
120#ifdef DIOCGMEDIASIZE
121	if (ioctl(fd, DIOCGMEDIASIZE, &size64) >= 0)
122		return (off_t)size64;
123#endif /* DIOCGMEDIASIZE */
124
125#ifdef FDGETPRM
126	{
127		struct floppy_struct this_floppy;
128
129		if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
130			return (blkid_loff_t)this_floppy.size << 9;
131	}
132#endif
133#ifdef HAVE_SYS_DISKLABEL_H
134	{
135		int part = -1;
136		struct disklabel lab;
137		struct partition *pp;
138		char ch;
139		struct stat st;
140
141		/*
142		 * This code works for FreeBSD 4.11 i386, except for the full
143		 * device (such as /dev/ad0). It doesn't work properly for
144		 * newer FreeBSD though. FreeBSD >= 5.0 should be covered by
145		 * the DIOCGMEDIASIZE above however.
146		 *
147		 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
148		 * character) devices, so we need to check for S_ISCHR, too.
149		 */
150		if (fstat(fd, &st) >= 0 &&
151		    (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
152			part = st.st_rdev & 7;
153
154		if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
155			pp = &lab.d_partitions[part];
156			if (pp->p_size)
157				return pp->p_size << 9;
158		}
159	}
160#endif /* HAVE_SYS_DISKLABEL_H */
161	{
162#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
163		struct stat64   st;
164		if (fstat64(fd, &st) == 0)
165#else
166		struct stat	st;
167		if (fstat(fd, &st) == 0)
168#endif
169			if (S_ISREG(st.st_mode))
170				return st.st_size;
171	}
172
173	/*
174	 * OK, we couldn't figure it out by using a specialized ioctl,
175	 * which is generally the best way.  So do binary search to
176	 * find the size of the partition.
177	 */
178	low = 0;
179	for (high = 1024; valid_offset(fd, high); high *= 2)
180		low = high;
181	while (low < high - 1) {
182		const blkid_loff_t mid = (low + high) / 2;
183
184		if (valid_offset(fd, mid))
185			low = mid;
186		else
187			high = mid;
188	}
189	return low + 1;
190}
191
192#ifdef TEST_PROGRAM
193int main(int argc, char **argv)
194{
195	long long bytes;
196	int	fd;
197
198	if (argc < 2) {
199		fprintf(stderr, "Usage: %s device\n"
200			"Determine the size of a device\n", argv[0]);
201		return 1;
202	}
203
204	if ((fd = open(argv[1], O_RDONLY)) < 0)
205		perror(argv[0]);
206
207	bytes = blkid_get_dev_size(fd);
208	printf("Device %s has %lld 1k blocks.\n", argv[1],
209	       (unsigned long long)bytes >> 10);
210
211	return 0;
212}
213#endif
214