Home | History | Annotate | Download | only in blkid
      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 #define _LARGEFILE_SOURCE
     13 #define _LARGEFILE64_SOURCE
     14 
     15 /* include this before sys/queues.h! */
     16 #include "blkidP.h"
     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 
     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 static int valid_offset(int fd, blkid_loff_t offset)
     62 {
     63 	char ch;
     64 
     65 	if (blkid_llseek(fd, offset, 0) < 0)
     66 		return 0;
     67 	if (read(fd, &ch, 1) < 1)
     68 		return 0;
     69 	return 1;
     70 }
     71 
     72 /*
     73  * Returns the number of bytes in a partition
     74  */
     75 blkid_loff_t blkid_get_dev_size(int fd)
     76 {
     77 	int valid_blkgetsize64 = 1;
     78 #ifdef __linux__
     79 	struct 		utsname ut;
     80 #endif
     81 	unsigned long long size64;
     82 	unsigned long size;
     83 	blkid_loff_t high, low;
     84 #ifdef FDGETPRM
     85 	struct floppy_struct this_floppy;
     86 #endif
     87 #ifdef HAVE_SYS_DISKLABEL_H
     88 	int part = -1;
     89 	struct disklabel lab;
     90 	struct partition *pp;
     91 	char ch;
     92 	struct stat st;
     93 #endif /* HAVE_SYS_DISKLABEL_H */
     94 
     95 #ifdef DKIOCGETBLOCKCOUNT	/* For Apple Darwin */
     96 	if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
     97 		if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
     98 		    && (size64 << 9 > 0xFFFFFFFF))
     99 			return 0; /* EFBIG */
    100 		return (blkid_loff_t) size64 << 9;
    101 	}
    102 #endif
    103 
    104 #ifdef BLKGETSIZE64
    105 #ifdef __linux__
    106 	if ((uname(&ut) == 0) &&
    107 	    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
    108 	     (ut.release[2] < '6') && (ut.release[3] == '.')))
    109 		valid_blkgetsize64 = 0;
    110 #endif
    111 	if (valid_blkgetsize64 &&
    112 	    ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
    113 		if ((sizeof(blkid_loff_t) < sizeof(unsigned long long))
    114 		    && ((size64) > 0xFFFFFFFF))
    115 			return 0; /* EFBIG */
    116 		return size64;
    117 	}
    118 #endif
    119 
    120 #ifdef BLKGETSIZE
    121 	if (ioctl(fd, BLKGETSIZE, &size) >= 0)
    122 		return (blkid_loff_t)size << 9;
    123 #endif
    124 
    125 /* tested on FreeBSD 6.1-RELEASE i386 */
    126 #ifdef DIOCGMEDIASIZE
    127 	if (ioctl(fd, DIOCGMEDIASIZE, &size64) >= 0)
    128 		return (off_t)size64;
    129 #endif /* DIOCGMEDIASIZE */
    130 
    131 #ifdef FDGETPRM
    132 	if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
    133 		return (blkid_loff_t)this_floppy.size << 9;
    134 #endif
    135 #ifdef HAVE_SYS_DISKLABEL_H
    136 	/*
    137 	 * This code works for FreeBSD 4.11 i386, except for the full device
    138 	 * (such as /dev/ad0). It doesn't work properly for newer FreeBSD
    139 	 * though. FreeBSD >= 5.0 should be covered by the DIOCGMEDIASIZE
    140 	 * above however.
    141 	 *
    142 	 * Note that FreeBSD >= 4.0 has disk devices as unbuffered (raw,
    143 	 * character) devices, so we need to check for S_ISCHR, too.
    144 	 */
    145 	if ((fstat(fd, &st) >= 0) && (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)))
    146 		part = st.st_rdev & 7;
    147 	if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
    148 		pp = &lab.d_partitions[part];
    149 		if (pp->p_size)
    150 			return pp->p_size << 9;
    151 	}
    152 #endif /* HAVE_SYS_DISKLABEL_H */
    153 	{
    154 #ifdef HAVE_FSTAT64
    155 		struct stat64   st;
    156 		if (fstat64(fd, &st) == 0)
    157 #else
    158 		struct stat	st;
    159 		if (fstat(fd, &st) == 0)
    160 #endif
    161 			if (S_ISREG(st.st_mode))
    162 				return st.st_size;
    163 	}
    164 
    165 
    166 	/*
    167 	 * OK, we couldn't figure it out by using a specialized ioctl,
    168 	 * which is generally the best way.  So do binary search to
    169 	 * find the size of the partition.
    170 	 */
    171 	low = 0;
    172 	for (high = 1024; valid_offset(fd, high); high *= 2)
    173 		low = high;
    174 	while (low < high - 1)
    175 	{
    176 		const blkid_loff_t mid = (low + high) / 2;
    177 
    178 		if (valid_offset(fd, mid))
    179 			low = mid;
    180 		else
    181 			high = mid;
    182 	}
    183 	return low + 1;
    184 }
    185 
    186 #ifdef TEST_PROGRAM
    187 int main(int argc, char **argv)
    188 {
    189 	long long bytes;
    190 	int	fd;
    191 
    192 	if (argc < 2) {
    193 		fprintf(stderr, "Usage: %s device\n"
    194 			"Determine the size of a device\n", argv[0]);
    195 		return 1;
    196 	}
    197 
    198 	if ((fd = open(argv[1], O_RDONLY)) < 0)
    199 		perror(argv[0]);
    200 
    201 	bytes = blkid_get_dev_size(fd);
    202 	printf("Device %s has %Ld 1k blocks.\n", argv[1],
    203 	       (unsigned long long) bytes >> 10);
    204 
    205 	return 0;
    206 }
    207 #endif
    208