Whamcloud - gitweb
Supplied changelog for resize inode patch.
[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 Public
11  * License.
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
45 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
46 #define BLKGETSIZE _IO(0x12,96) /* return device size */
47 #endif
48
49 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
50 #define BLKGETSIZE64 _IOR(0x12,114,size_t)      /* return device size in bytes (u64 *arg) */
51 #endif
52
53 #ifdef APPLE_DARWIN
54 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
55 #endif /* APPLE_DARWIN */
56
57 #include "ext2_fs.h"
58 #include "ext2fs.h"
59
60 #if defined(__CYGWIN__) || defined (WIN32)
61 #include "windows.h"
62 #include "winioctl.h"
63
64 #if (_WIN32_WINNT >= 0x0500)
65 #define HAVE_GET_FILE_SIZE_EX 1
66 #endif
67
68 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
69                                  blk_t *retblocks)
70 {
71         HANDLE dev;
72         PARTITION_INFORMATION pi;
73         DISK_GEOMETRY gi;
74         DWORD retbytes;
75 #ifdef HAVE_GET_FILE_SIZE_EX
76         LARGE_INTEGER filesize;
77 #else
78         DWORD filesize;
79 #endif /* HAVE_GET_FILE_SIZE_EX */
80
81         dev = CreateFile(file, GENERIC_READ, 
82                          FILE_SHARE_READ | FILE_SHARE_WRITE ,
83                          NULL,  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,  NULL); 
84  
85         if (dev == INVALID_HANDLE_VALUE)
86                 return EBADF;
87         if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
88                             &pi, sizeof(PARTITION_INFORMATION),
89                             &pi, sizeof(PARTITION_INFORMATION),
90                             &retbytes, NULL)) {
91
92                 *retblocks = pi.PartitionLength.QuadPart / blocksize;
93         
94         } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
95                                 &gi, sizeof(DISK_GEOMETRY),
96                                 &gi, sizeof(DISK_GEOMETRY),
97                                 &retbytes, NULL)) {
98
99                 *retblocks = gi.BytesPerSector *
100                              gi.SectorsPerTrack *
101                              gi.TracksPerCylinder *
102                              gi.Cylinders.QuadPart / blocksize;
103
104 #ifdef HAVE_GET_FILE_SIZE_EX
105         } else if (GetFileSizeEx(dev, &filesize)) {
106                 *retblocks = filesize.QuadPart / blocksize;
107         }
108 #else
109         } else {
110                 filesize = GetFileSize(dev, NULL);
111                 if (INVALID_FILE_SIZE != filesize) {
112                         *retblocks = filesize / blocksize;
113                 }
114         }
115 #endif /* HAVE_GET_FILE_SIZE_EX */
116
117         CloseHandle(dev);
118         return 0;
119 }
120
121 #else
122
123 static int valid_offset (int fd, ext2_loff_t offset)
124 {
125         char ch;
126
127         if (ext2fs_llseek (fd, offset, 0) < 0)
128                 return 0;
129         if (read (fd, &ch, 1) < 1)
130                 return 0;
131         return 1;
132 }
133
134 /*
135  * Returns the number of blocks in a partition
136  */
137 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
138                                  blk_t *retblocks)
139 {
140         int     fd;
141         int valid_blkgetsize64 = 1;
142 #ifdef __linux__
143         struct          utsname ut;
144 #endif
145         unsigned long long size64;
146         unsigned long   size;
147         ext2_loff_t high, low;
148 #ifdef FDGETPRM
149         struct floppy_struct this_floppy;
150 #endif
151 #ifdef HAVE_SYS_DISKLABEL_H
152         int part;
153         struct disklabel lab;
154         struct partition *pp;
155         char ch;
156 #endif /* HAVE_SYS_DISKLABEL_H */
157
158 #ifdef HAVE_OPEN64
159         fd = open64(file, O_RDONLY);
160 #else
161         fd = open(file, O_RDONLY);
162 #endif
163         if (fd < 0)
164                 return errno;
165
166 #ifdef DKIOCGETBLOCKCOUNT       /* For Apple Darwin */
167         if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
168                 if ((sizeof(*retblocks) < sizeof(unsigned long long))
169                     && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
170                         return EFBIG;
171                 close(fd);
172                 *retblocks = size64 / (blocksize / 512);
173                 return 0;
174         }
175 #endif
176
177 #ifdef BLKGETSIZE64
178 #ifdef __linux__
179         if ((uname(&ut) == 0) &&
180             ((ut.release[0] == '2') && (ut.release[1] == '.') &&
181              (ut.release[2] < '6') && (ut.release[3] == '.')))
182                 valid_blkgetsize64 = 0;
183 #endif
184         if (valid_blkgetsize64 &&
185             ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
186                 if ((sizeof(*retblocks) < sizeof(unsigned long long))
187                     && ((size64 / blocksize) > 0xFFFFFFFF))
188                         return EFBIG;
189                 close(fd);
190                 *retblocks = size64 / blocksize;
191                 return 0;
192         }
193 #endif
194
195 #ifdef BLKGETSIZE
196         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
197                 close(fd);
198                 *retblocks = size / (blocksize / 512);
199                 return 0;
200         }
201 #endif
202
203 #ifdef FDGETPRM
204         if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
205                 close(fd);
206                 *retblocks = this_floppy.size / (blocksize / 512);
207                 return 0;
208         }
209 #endif
210
211 #ifdef HAVE_SYS_DISKLABEL_H
212 #if defined(DIOCGMEDIASIZE)
213         {
214             off_t ms;
215             u_int bs;
216             if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
217                 *retblocks = ms / blocksize;
218                 return 0;
219             }
220         }
221 #elif defined(DIOCGDINFO)
222         /* old disklabel interface */
223         part = strlen(file) - 1;
224         if (part >= 0) {
225                 ch = file[part];
226                 if (isdigit(ch))
227                         part = 0;
228                 else if (ch >= 'a' && ch <= 'h')
229                         part = ch - 'a';
230                 else
231                         part = -1;
232         }
233         if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
234                 pp = &lab.d_partitions[part];
235                 if (pp->p_size) {
236                         close(fd);
237                         *retblocks = pp->p_size / (blocksize / 512);
238                         return 0;
239                 }
240         }
241 #endif /* defined(DIOCG*) */
242 #endif /* HAVE_SYS_DISKLABEL_H */
243
244         /*
245          * OK, we couldn't figure it out by using a specialized ioctl,
246          * which is generally the best way.  So do binary search to
247          * find the size of the partition.
248          */
249         low = 0;
250         for (high = 1024; valid_offset (fd, high); high *= 2)
251                 low = high;
252         while (low < high - 1)
253         {
254                 const ext2_loff_t mid = (low + high) / 2;
255
256                 if (valid_offset (fd, mid))
257                         low = mid;
258                 else
259                         high = mid;
260         }
261         valid_offset (fd, 0);
262         close(fd);
263         *retblocks = (low + 1) / blocksize;
264         return 0;
265 }
266
267 #endif /* WIN32 */
268
269 #ifdef DEBUG
270 int main(int argc, char **argv)
271 {
272         blk_t   blocks;
273         int     retval;
274         
275         if (argc < 2) {
276                 fprintf(stderr, "Usage: %s device\n", argv[0]);
277                 exit(1);
278         }
279
280         retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
281         if (retval) {
282                 com_err(argv[0], retval,
283                         "while calling ext2fs_get_device_size");
284                 exit(1);
285         }
286         printf("Device %s has %d 1k blocks.\n", argv[1], blocks);
287         exit(0);
288 }
289 #endif