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