Whamcloud - gitweb
tst_libext2fs: Avoid multiple definition of global variables
[tools/e2fsprogs.git] / lib / ext2fs / getsectsize.c
index 30faecc..d6bc376 100644 (file)
  * %End-Header%
  */
 
+#ifndef _LARGEFILE_SOURCE
 #define _LARGEFILE_SOURCE
+#endif
+#ifndef _LARGEFILE64_SOURCE
 #define _LARGEFILE64_SOURCE
+#endif
 
 #include "config.h"
 #include <stdio.h>
@@ -22,6 +26,9 @@
 #include <errno.h>
 #endif
 #include <fcntl.h>
+#ifdef HAVE_SYS_DISK_H
+#include <sys/disk.h>
+#endif
 #ifdef HAVE_LINUX_FD_H
 #include <sys/ioctl.h>
 #include <linux/fd.h>
@@ -56,12 +63,49 @@ errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
                return 0;
        }
 #endif
+#ifdef DIOCGSECTORSIZE
+       if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {
+               close(fd);
+               return 0;
+       }
+#endif
        *sectsize = 0;
        close(fd);
        return 0;
 }
 
 /*
+ * Return desired alignment for direct I/O
+ */
+int ext2fs_get_dio_alignment(int fd)
+{
+       int align = 0;
+
+#ifdef BLKSSZGET
+       if (ioctl(fd, BLKSSZGET, &align) < 0)
+               align = 0;
+#endif
+#ifdef DIOCGSECTORSIZE
+       if (align <= 0 &&
+           ioctl(fd, DIOCGSECTORSIZE, &align) < 0)
+               align = 0;
+#endif
+
+#ifdef _SC_PAGESIZE
+       if (align <= 0)
+               align = sysconf(_SC_PAGESIZE);
+#endif
+#ifdef HAVE_GETPAGESIZE
+       if (align <= 0)
+               align = getpagesize();
+#endif
+       if (align <= 0)
+               align = 4096;
+
+       return align;
+}
+
+/*
  * Returns the physical sector size of a device
  */
 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
@@ -78,6 +122,14 @@ errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
                return 0;
        }
 #endif
+#ifdef DIOCGSECTORSIZE
+       /* This isn't really the physical sector size, but FreeBSD
+        * doesn't seem to have this concept. */
+       if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {
+               close(fd);
+               return 0;
+       }
+#endif
        *sectsize = 0;
        close(fd);
        return 0;