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