]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: [blkdev.c] add blkdev_get_physector_size()
authorDavidlohr Bueso <dave@gnu.org>
Tue, 21 Jun 2011 02:51:47 +0000 (22:51 -0400)
committerKarel Zak <kzak@redhat.com>
Mon, 27 Jun 2011 13:57:31 +0000 (15:57 +0200)
This function uses the BLKPBSZGET ioctl to get the physical block size
of the device.

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/blkdev.h
lib/blkdev.c

index 6a641723f844814eeadac8fbb7b032280ca79b6e..3ce8111f67360d3ba81c67c46af9e6c758de73f2 100644 (file)
@@ -95,4 +95,7 @@ int blkdev_get_sector_size(int fd, int *sector_size);
 /* specifies whether or not the device is misaligned */
 int blkdev_is_misaligned(int fd);
 
+/* get physical block device size */
+int blkdev_get_physector_size(int fd, int *sector_size);
+
 #endif /* BLKDEV_H */
index 5cb6554df6313158dadc796b7f0f9488cb1d54e4..b6f9bfd328147814cae8be7d8a916bab4c71b669 100644 (file)
@@ -204,6 +204,31 @@ blkdev_get_sector_size(int fd, int *sector_size)
 #endif
 }
 
+/*
+ * Get physical block device size. The BLKPBSZGET is supported since Linux
+ * 2.6.32. For old kernels is probably the best to assume that physical sector
+ * size is the same as logical sector size.
+ *
+ * Example:
+ *
+ * rc = blkdev_get_physector_size(fd, &physec);
+ * if (rc || physec == 0) {
+ *     rc = blkdev_get_sector_size(fd, &physec);
+ *     if (rc)
+ *             physec = DEFAULT_SECTOR_SIZE;
+ * }
+ */
+int blkdev_get_physector_size(int fd, int *sector_size)
+{
+#ifdef BLKPBSZGET
+       if (ioctl(fd, BLKPBSZGET, &sector_size) >= 0)
+               return 0;
+       return -1;
+#else
+       *sector_size = DEFAULT_SECTOR_SIZE;
+       return 0;
+#endif
+}
 
 /*
  * Return the alignment status of a device
@@ -221,6 +246,7 @@ int blkdev_is_misaligned(int fd)
 #endif
 }
 
+
 #ifdef TEST_PROGRAM
 #include <stdio.h>
 #include <stdlib.h>
@@ -230,7 +256,7 @@ main(int argc, char **argv)
 {
        unsigned long long bytes;
        unsigned long long sectors;
-       int sector_size;
+       int sector_size, phy_sector_size;
        int fd;
 
        if (argc != 2) {
@@ -247,10 +273,13 @@ main(int argc, char **argv)
                err(EXIT_FAILURE, "blkdev_get_sectors() failed");
        if (blkdev_get_sector_size(fd, &sector_size) < 0)
                err(EXIT_FAILURE, "blkdev_get_sector_size() failed");
+       if (blkdev_get_physector_size(fd, &phy_sector_size) < 0)
+               err(EXIT_FAILURE, "blkdev_get_physector_size() failed");
 
-       printf("bytes %llu\n", bytes);
-       printf("sectors %llu\n", sectors);
-       printf("sectorsize %d\n", sector_size);
+       printf("          bytes: %llu\n", bytes);
+       printf("        sectors: %llu\n", sectors);
+       printf("    sector size: %d\n", sector_size);
+       printf("phy-sector size: %d\n", phy_sector_size);
 
        return EXIT_SUCCESS;
 }