summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Watkins <noahwatkins@gmail.com>2013-07-20 18:41:39 -0700
committerNoah Watkins <noahwatkins@gmail.com>2013-07-20 18:41:39 -0700
commit6a08271922217e1a34be884156d035745893e52f (patch)
tree8e316618f027eee22a6d301a9ca90edbcd703d27
parent1ef6fd1c5847ed5c57376e62797080993b6f8bf8 (diff)
downloadceph-6a08271922217e1a34be884156d035745893e52f.tar.gz
blkdev: support blkdev size query on osx
Support OSX, add checks for platform specific headers. Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
-rw-r--r--configure.ac2
-rw-r--r--src/common/blkdev.cc60
2 files changed, 47 insertions, 15 deletions
diff --git a/configure.ac b/configure.ac
index e94d4868d83..692b39f14c3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -537,6 +537,8 @@ AC_CHECK_MEMBER([struct stat.st_mtimespec.tv_nsec],
[AC_DEFINE(HAVE_STAT_ST_TIMESPEC, 1,
[Define if you have struct stat.st_mtimespec.tv_nsec])])
+AC_CHECK_HEADERS([linux/fs.h])
+AC_CHECK_HEADERS([sys/disk.h])
AC_CHECK_HEADERS([sys/prctl.h])
AC_CHECK_FUNCS([prctl])
diff --git a/src/common/blkdev.cc b/src/common/blkdev.cc
index 22b0aa67b3e..1a6d400bf62 100644
--- a/src/common/blkdev.cc
+++ b/src/common/blkdev.cc
@@ -1,3 +1,5 @@
+#include <acconfig.h>
+
#include <inttypes.h>
#include <fcntl.h>
#include <sys/ioctl.h>
@@ -7,34 +9,62 @@
#include <sys/stat.h>
#include <sys/mount.h>
#include <iostream>
-
-#include "acconfig.h"
#include "include/compat.h"
-#if defined(__FreeBSD__)
+#ifdef HAVE_SYS_DISK_H
#include <sys/disk.h>
#endif
+#ifdef HAVE_LINUX_FS_H
+#include <linux/fs.h>
+#endif
+
+#ifdef __linux__
+
int get_block_device_size(int fd, int64_t *psize)
{
- int ret = 0;
-
-#if defined(__FreeBSD__)
- ret = ::ioctl(fd, DIOCGMEDIASIZE, psize);
-#elif defined(__linux__)
#ifdef BLKGETSIZE64
- // ioctl block device
- ret = ::ioctl(fd, BLKGETSIZE64, psize);
+ int ret = ::ioctl(fd, BLKGETSIZE64, psize);
#elif BLKGETSIZE
- // hrm, try the 32 bit ioctl?
unsigned long sectors = 0;
- ret = ::ioctl(fd, BLKGETSIZE, &sectors);
+ int ret = ::ioctl(fd, BLKGETSIZE, &sectors);
*psize = sectors * 512ULL;
-#endif
#else
-#error "Compile error: we don't know how to get the size of a raw block device."
-#endif /* !__FreeBSD__ */
+# error "Linux configuration error (get_block_device_size)"
+#endif
+ if (ret < 0)
+ ret = -errno;
+ return ret;
+}
+
+#elif defined(DARWIN)
+
+int get_block_device_size(int fd, int64_t *psize)
+{
+ unsigned long blocksize = 0;
+ int ret = ::ioctl(fd, DKIOCGETBLOCKSIZE, &blocksize);
+ if (!ret) {
+ unsigned long nblocks;
+ ret = ::ioctl(fd, DKIOCGETBLOCKCOUNT, &nblocks);
+ if (!ret)
+ *psize = nblocks * blocksize;
+ }
if (ret < 0)
ret = -errno;
return ret;
}
+
+#elif defined(__FreeBSD__)
+
+int get_block_device_size(int fd, int64_t *psize)
+{
+ int ret = ::ioctl(fd, DIOCGMEDIASIZE, psize);
+ if (ret < 0)
+ ret = -errno;
+ return ret;
+}
+
+#else
+# warning "Unsupported platform. Please report."
+# error "Unable to query block device size."
+#endif