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 #ifndef _LARGEFILE_SOURCE
13 #define _LARGEFILE_SOURCE
14 #endif
15 #ifndef _LARGEFILE64_SOURCE
16 #define _LARGEFILE64_SOURCE
17 #endif
18 
19 #include "config.h"
20 #include "blkidP.h"
21 
22 #include <stdio.h>
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #if HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29 #include <fcntl.h>
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
32 #endif
33 #ifdef HAVE_LINUX_FD_H
34 #include <linux/fd.h>
35 #endif
36 #ifdef HAVE_SYS_DISKLABEL_H
37 #include <sys/disklabel.h>
38 #endif
39 #ifdef HAVE_SYS_DISK_H
40 #include <sys/disk.h>
41 #endif
42 #ifdef __linux__
43 #include <sys/utsname.h>
44 #endif
45 #if HAVE_SYS_STAT_H
46 #include <sys/stat.h>
47 #endif
48 
49 
50 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
51 #define BLKGETSIZE _IO(0x12,96)	/* return device size */
52 #endif
53 
54 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
55 #define BLKGETSIZE64 _IOR(0x12,114,size_t)	/* return device size in bytes (u64 *arg) */
56 #endif
57 
58 #ifdef APPLE_DARWIN
59 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
60 #endif /* APPLE_DARWIN */
61 
valid_offset(int fd,blkid_loff_t offset)62 static int valid_offset(int fd, blkid_loff_t offset)
63 {
64 	char ch;
65 
66 	if (blkid_llseek(fd, offset, 0) < 0)
67 		return 0;
68 	if (read(fd, &ch, 1) < 1)
69 		return 0;
70 	return 1;
71 }
72 
73 /*
74  * Returns the number of bytes in a partition
75  */
blkid_get_dev_size(int fd)76 blkid_loff_t blkid_get_dev_size(int fd)
77 {
78 	unsigned long long size64;
79 	blkid_loff_t high, low;
80 
81 #if defined DKIOCGETBLOCKCOUNT && defined DKIOCGETBLOCKSIZE	/* For Apple Darwin */
82 	unsigned int size;
83 
84 	if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0 &&
85 	    ioctl(fd, DKIOCGETBLOCKSIZE, &size) >= 0) {
86 		if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
87 		    (size64 * size) > 0xFFFFFFFF)
88 			return 0; /* EFBIG */
89 		return (blkid_loff_t)size64 * size;
90 	}
91 #endif
92 
93 #ifdef BLKGETSIZE64
94 	{
95 		int valid_blkgetsize64 = 1;
96 #ifdef __linux__
97 		struct		utsname ut;
98 
99 		if ((uname(&ut) == 0) &&
100 		    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
101 		     (ut.release[2] < '6') && (ut.release[3] == '.')))
102 			valid_blkgetsize64 = 0;
103 #endif
104 		if (valid_blkgetsize64 &&
105 		    ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
106 			if (sizeof(blkid_loff_t) < sizeof(unsigned long long) &&
107 			    (size64 > 0xFFFFFFFF))
108 				return 0; /* EFBIG */
109 			return size64;
110 		}
111 	}
112 #endif /* BLKGETSIZE64 */
113 
114 #ifdef BLKGETSIZE
115 	{
116 		unsigned long size;
117 
118 		if (ioctl(fd, BLKGETSIZE, &size) >= 0)
119 			return (blkid_loff_t)size << 9;
120 	}
121 #endif
122 
123 /* tested on FreeBSD 6.1-RELEASE i386 */
124 #ifdef DIOCGMEDIASIZE
125 	if (ioctl(fd, DIOCGMEDIASIZE, &size64) >= 0)
126 		return (off_t)size64;
127 #endif /* DIOCGMEDIASIZE */
128 
129 #ifdef FDGETPRM
130 	{
131 		struct floppy_struct this_floppy;
132 
133 		if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
134 			return (blkid_loff_t)this_floppy.size << 9;
135 	}
136 #endif
137 #if defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO)
138 	{
139 		int part = -1;
140 		struct disklabel lab;
141 		struct partition *pp;
142 		char ch;
143 		struct stat st;
144 
145 		/*
146 		 * This code works for FreeBSD 4.11 i386, except for the full
147 		 * device (such as /dev/ad0). It doesn't work properly for
148 		 * newer FreeBSD though. FreeBSD >= 5.0 should be covered by
149 		 * the DIOCGMEDIASIZE above however.
150 		 *
151 		 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
152 		 * character) devices, so we need to check for S_ISCHR, too.
153 		 */
154 		if (fstat(fd, &st) >= 0 &&
155 		    blkidP_is_disk_device(st.st_mode))
156 			part = st.st_rdev & 7;
157 
158 		if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
159 			pp = &lab.d_partitions[part];
160 			if (pp->p_size)
161 				return pp->p_size << 9;
162 		}
163 	}
164 #endif /* defined(HAVE_SYS_DISKLABEL_H) && defined(DIOCGDINFO) */
165 	{
166 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
167 		struct stat64   st;
168 		if (fstat64(fd, &st) == 0)
169 #else
170 		struct stat	st;
171 		if (fstat(fd, &st) == 0)
172 #endif
173 			if (S_ISREG(st.st_mode))
174 				return st.st_size;
175 	}
176 
177 	/*
178 	 * OK, we couldn't figure it out by using a specialized ioctl,
179 	 * which is generally the best way.  So do binary search to
180 	 * find the size of the partition.
181 	 */
182 	low = 0;
183 	for (high = 1024; valid_offset(fd, high); high *= 2)
184 		low = high;
185 	while (low < high - 1) {
186 		const blkid_loff_t mid = (low + high) / 2;
187 
188 		if (valid_offset(fd, mid))
189 			low = mid;
190 		else
191 			high = mid;
192 	}
193 	return low + 1;
194 }
195 
196 #ifdef TEST_PROGRAM
main(int argc,char ** argv)197 int main(int argc, char **argv)
198 {
199 	long long bytes;
200 	int	fd;
201 
202 	if (argc < 2) {
203 		fprintf(stderr, "Usage: %s device\n"
204 			"Determine the size of a device\n", argv[0]);
205 		return 1;
206 	}
207 
208 	if ((fd = open(argv[1], O_RDONLY)) < 0)
209 		perror(argv[0]);
210 
211 	bytes = blkid_get_dev_size(fd);
212 	printf("Device %s has %lld 1k blocks.\n", argv[1],
213 	       (unsigned long long)bytes >> 10);
214 
215 	return 0;
216 }
217 #endif
218