]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
blkdev: support blkdev size query on osx
authorNoah Watkins <noahwatkins@gmail.com>
Sun, 21 Jul 2013 01:41:39 +0000 (18:41 -0700)
committerNoah Watkins <noahwatkins@gmail.com>
Mon, 4 Nov 2013 18:13:50 +0000 (10:13 -0800)
Support OSX, add checks for platform specific headers.

Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
src/common/blkdev.cc

index b0dc0a54e9e2f37ff48ca563e92b95a9e6419b1c..9c7240c0aac6c87ffeae82d5a075f61c639135bc 100644 (file)
@@ -1,40 +1,56 @@
-#include "include/int_types.h"
-
-#include <fcntl.h>
-#include <sys/ioctl.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mount.h>
-#include <iostream>
-
-#include "acconfig.h"
-#include "include/compat.h"
+#include <sys/ioctl.h>
+#include "include/int_types.h"
 
-#if defined(__FreeBSD__)
-#include <sys/disk.h>
-#endif
+#ifdef __linux__
+#include <linux/fs.h>
 
 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(__APPLE__)
+#include <sys/disk.h>
+
+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 = (int64_t)nblocks * blocksize;
+  }
+  if (ret < 0)
+    ret = -errno;
+  return ret;
+}
+
+#elif defined(__FreeBSD__)
+#include <sys/disk.h>
+
+int get_block_device_size(int fd, int64_t *psize)
+{
+  int ret = ::ioctl(fd, DIOCGMEDIASIZE, psize);
+  if (ret < 0)
+    ret = -errno;
+  return ret;
+}
+
+#else
+# error "Unable to query block device size: unsupported platform, please report."
+#endif