Whamcloud - gitweb
libext2fs: add FreeBSD support to getsectsize.c
authorTheodore Ts'o <tytso@mit.edu>
Tue, 13 Sep 2016 22:31:20 +0000 (18:31 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 13 Sep 2016 22:31:20 +0000 (18:31 -0400)
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/Makefile.in
lib/ext2fs/getsectsize.c
lib/ext2fs/tst_getsectsize.c

index 6a3656d..1e90dd8 100644 (file)
@@ -312,10 +312,12 @@ tst_bitops: tst_bitops.o $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
        $(Q) $(CC) -o tst_bitops tst_bitops.o $(ALL_CFLAGS) $(ALL_LDFLAGS) \
                $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) $(SYSLIBS)
 
-tst_getsectsize: tst_getsectsize.o $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
+tst_getsectsize: tst_getsectsize.o getsectsize.o $(STATIC_LIBEXT2FS) \
+                       $(DEPSTATIC_LIBCOM_ERR)
        $(E) "  LD $@"
-       $(Q) $(CC) -o tst_sectgetsize tst_getsectsize.o $(ALL_LDFLAGS) \
-               $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) $(SYSLIBS)
+       $(Q) $(CC) -o tst_getsectsize tst_getsectsize.o getsectsize.o \
+               $(ALL_LDFLAGS) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) \
+               $(SYSLIBS)
 
 tst_types.o: $(srcdir)/tst_types.c ext2_types.h 
 
@@ -521,7 +523,7 @@ mkjournal: mkjournal.c $(STATIC_LIBEXT2FS) $(DEPLIBCOM_ERR)
 check:: tst_bitops tst_badblocks tst_iscan tst_types tst_icount \
     tst_super_size tst_types tst_inode_size tst_csum tst_crc32c tst_bitmaps \
     tst_inline tst_inline_data tst_libext2fs tst_sha256 tst_sha512 \
-    tst_digest_encode
+    tst_digest_encode tst_getsize tst_getsectsize
        $(TESTENV) ./tst_bitops
        $(TESTENV) ./tst_badblocks
        $(TESTENV) ./tst_iscan
@@ -574,7 +576,7 @@ uninstall::
 clean::
        $(RM) -f \#* *.s *.o *.a *~ *.bak core profiled/* \
                tst_badblocks tst_iscan ext2_err.et ext2_err.c ext2_err.h \
-               tst_byteswap tst_ismounted tst_getsize tst_sectgetsize \
+               tst_byteswap tst_ismounted tst_getsize tst_getsectsize \
                tst_bitops tst_types tst_icount tst_super_size tst_csum \
                tst_bitmaps tst_bitmaps_out tst_extents tst_inline \
                tst_inline_data tst_inode_size tst_bitmaps_cmd.c \
index b57bf56..d6bc376 100644 (file)
@@ -26,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>
@@ -60,6 +63,12 @@ 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;
@@ -76,6 +85,11 @@ int ext2fs_get_dio_alignment(int fd)
        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)
@@ -108,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;
index a6b234e..d616965 100644 (file)
@@ -30,6 +30,7 @@ int main(int argc, char **argv)
 {
        int     lsectsize, psectsize;
        int     retval;
+       int     fd;
 
        if (argc < 2) {
                fprintf(stderr, "Usage: %s device\n", argv[0]);
@@ -50,5 +51,13 @@ int main(int argc, char **argv)
        }
        printf("Device %s has logical/physical sector size of %d/%d.\n",
               argv[1], lsectsize, psectsize);
+       fd = open(argv[1], O_RDONLY);
+       if (fd < 0) {
+               perror("open");
+               exit(1);
+       }
+       printf("The device's DIO alignment is %d\n",
+              ext2fs_get_dio_alignment(fd));
+       close(fd);
        exit(0);
 }