blob: 7a2133f0a2bc5bfafb1f72167220b04e63c1437d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger38a33f92005-05-09 22:13:22 +00002/*
3 * getsize.c --- get the size of a partition.
4 *
5 * Copyright (C) 1995, 1995 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 * %End-Header%
11 */
12
13/* include this before sys/queues.h! */
14#include "blkidP.h"
15
16#include <stdio.h>
17#if HAVE_UNISTD_H
18#include <unistd.h>
19#endif
20#if HAVE_ERRNO_H
21#include <errno.h>
22#endif
23#include <fcntl.h>
24#ifdef HAVE_SYS_IOCTL_H
25#include <sys/ioctl.h>
26#endif
27#ifdef HAVE_LINUX_FD_H
28#include <linux/fd.h>
29#endif
30#ifdef HAVE_SYS_DISKLABEL_H
31#include <sys/disklabel.h>
32#include <sys/stat.h>
33#endif
34#ifdef HAVE_SYS_DISK_H
35#ifdef HAVE_SYS_QUEUE_H
36#include <sys/queue.h> /* for LIST_HEAD */
37#endif
38#include <sys/disk.h>
39#endif
40#ifdef __linux__
41#include <sys/utsname.h>
42#endif
43
44#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
45#define BLKGETSIZE _IO(0x12,96) /* return device size */
46#endif
47
48#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
49#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
50#endif
51
52#ifdef APPLE_DARWIN
53#define BLKGETSIZE DKIOCGETBLOCKCOUNT32
54#endif /* APPLE_DARWIN */
55
56static int valid_offset(int fd, blkid_loff_t offset)
57{
58 char ch;
59
60 if (blkid_llseek(fd, offset, 0) < 0)
61 return 0;
62 if (read(fd, &ch, 1) < 1)
63 return 0;
64 return 1;
65}
66
67/*
68 * Returns the number of blocks in a partition
69 */
70blkid_loff_t blkid_get_dev_size(int fd)
71{
72 int valid_blkgetsize64 = 1;
73#ifdef __linux__
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000074 struct utsname ut;
Mike Frysinger38a33f92005-05-09 22:13:22 +000075#endif
76 unsigned long long size64;
77 unsigned long size;
78 blkid_loff_t high, low;
79#ifdef FDGETPRM
80 struct floppy_struct this_floppy;
81#endif
82#ifdef HAVE_SYS_DISKLABEL_H
83 int part = -1;
84 struct disklabel lab;
85 struct partition *pp;
86 char ch;
87 struct stat st;
88#endif /* HAVE_SYS_DISKLABEL_H */
89
90#ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
91 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
92 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
93 && (size64 << 9 > 0xFFFFFFFF))
94 return 0; /* EFBIG */
95 return (blkid_loff_t) size64 << 9;
96 }
97#endif
98
99#ifdef BLKGETSIZE64
100#ifdef __linux__
101 if ((uname(&ut) == 0) &&
102 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
103 (ut.release[2] < '6') && (ut.release[3] == '.')))
104 valid_blkgetsize64 = 0;
105#endif
106 if (valid_blkgetsize64 &&
107 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
108 if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
109 && ((size64) > 0xFFFFFFFF))
110 return 0; /* EFBIG */
111 return size64;
112 }
113#endif
114
115#ifdef BLKGETSIZE
116 if (ioctl(fd, BLKGETSIZE, &size) >= 0)
117 return (blkid_loff_t)size << 9;
118#endif
119
120#ifdef FDGETPRM
121 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
122 return (blkid_loff_t)this_floppy.size << 9;
123#endif
124#ifdef HAVE_SYS_DISKLABEL_H
125#if 0
126 /*
127 * This should work in theory but I haven't tested it. Anyone
128 * on a BSD system want to test this for me? In the meantime,
129 * binary search mechanism should work just fine.
130 */
131 if ((fstat(fd, &st) >= 0) && S_ISBLK(st.st_mode))
132 part = st.st_rdev & 7;
133 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
134 pp = &lab.d_partitions[part];
135 if (pp->p_size)
136 return pp->p_size << 9;
137 }
138#endif
139#endif /* HAVE_SYS_DISKLABEL_H */
140
141 /*
142 * OK, we couldn't figure it out by using a specialized ioctl,
143 * which is generally the best way. So do binary search to
144 * find the size of the partition.
145 */
146 low = 0;
147 for (high = 1024; valid_offset(fd, high); high *= 2)
148 low = high;
149 while (low < high - 1)
150 {
151 const blkid_loff_t mid = (low + high) / 2;
152
153 if (valid_offset(fd, mid))
154 low = mid;
155 else
156 high = mid;
157 }
158 return low + 1;
159}
160
161#ifdef TEST_PROGRAM
162int main(int argc, char **argv)
163{
164 blkid_loff_t bytes;
165 int fd;
166
167 if (argc < 2) {
168 fprintf(stderr, "Usage: %s device\n"
169 "Determine the size of a device\n", argv[0]);
170 return 1;
171 }
172
173 if ((fd = open(argv[1], O_RDONLY)) < 0)
174 perror(argv[0]);
175
176 bytes = blkid_get_dev_size(fd);
Bernhard Reutner-Fischerd409c3a2006-03-29 22:34:47 +0000177 printf("Device %s has %lld 1k blocks.\n", argv[1], bytes >> 10);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000178
179 return 0;
180}
181#endif