summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Krah2007-11-07 12:37:30 +0100
committerKarel Zak2007-11-07 12:37:30 +0100
commit3738a48e0889cf5d99353432bc9d425258e8e350 (patch)
tree06aeb8b6c5ec32fcc43c643805c8231c2f39c073
parentmkswap: linux_version() code consolidation (diff)
downloadkernel-qcow2-util-linux-3738a48e0889cf5d99353432bc9d425258e8e350.tar.gz
kernel-qcow2-util-linux-3738a48e0889cf5d99353432bc9d425258e8e350.tar.xz
kernel-qcow2-util-linux-3738a48e0889cf5d99353432bc9d425258e8e350.zip
lib: add blkdev.{c,h}
Now we duplicate BLK* ioctls on many places... This patch also fix BLKGETSIZE64 usage in dependence on kernel version. Co-Author: Karel Zak <kzak@redhat.com> Signed-off-by: Stefan Krah <stefan@bytereef.org> Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--include/Makefile.am2
-rw-r--r--include/blkdev.h55
-rw-r--r--lib/blkdev.c93
-rw-r--r--tests/helpers/Makefile.am6
4 files changed, 154 insertions, 2 deletions
diff --git a/include/Makefile.am b/include/Makefile.am
index 275661dee..e2b99d6c0 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -2,4 +2,4 @@ include $(top_srcdir)/config/include-Makefile.am
dist_noinst_HEADERS = carefulputc.h env.h linux_reboot.h md5.h \
nls.h pathnames.h setproctitle.h widechar.h xstrncpy.h \
- linux_version.h
+ linux_version.h blkdev.h
diff --git a/include/blkdev.h b/include/blkdev.h
new file mode 100644
index 000000000..8065acc1e
--- /dev/null
+++ b/include/blkdev.h
@@ -0,0 +1,55 @@
+#ifndef BLKDEV_H
+#define BLKDEV_H
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#define DEFAULT_SECTOR_SIZE 512
+
+#ifndef BLKROSET
+
+#define BLKROSET _IO(0x12,93) /* set device read-only (0 = read-write) */
+#define BLKROGET _IO(0x12,94) /* get read-only status (0 = read_write) */
+#define BLKRRPART _IO(0x12,95) /* re-read partition table */
+#define BLKGETSIZE _IO(0x12,96) /* return device size /512 (long *arg) */
+#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
+#define BLKRASET _IO(0x12,98) /* set read ahead for block device */
+#define BLKRAGET _IO(0x12,99) /* get current read ahead setting */
+#define BLKFRASET _IO(0x12,100)/* set filesystem (mm/filemap.c) read-ahead */
+#define BLKFRAGET _IO(0x12,101)/* get filesystem (mm/filemap.c) read-ahead */
+#define BLKSECTSET _IO(0x12,102)/* set max sectors per request (ll_rw_blk.c) */
+#define BLKSECTGET _IO(0x12,103)/* get max sectors per request (ll_rw_blk.c) */
+#define BLKSSZGET _IO(0x12,104)/* get block device sector size */
+
+#define BLKELVGET _IOR(0x12,106,size_t) /* elevator get */
+#define BLKELVSET _IOW(0x12,107,size_t) /* elevator set */
+
+#define BLKGETLASTSECT _IO(0x12,108) /* get last sector of block device */
+
+#define BLKBSZGET _IOR(0x12,112,size_t)
+#define BLKBSZSET _IOW(0x12,113,size_t)
+#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
+
+#endif /* BLKROSET */
+
+#ifndef HDIO_GETGEO
+#define HDIO_GETGEO 0x0301
+struct hd_geometry {
+ unsigned char heads;
+ unsigned char sectors;
+ unsigned short cylinders; /* truncated */
+ unsigned long start;
+};
+#endif
+
+/* get size in bytes */
+int blkdev_get_size(int fd, unsigned long long *bytes);
+
+/* get 512-byte sector count */
+int blkdev_get_sectors(int fd, unsigned long long *sectors);
+
+/* get hardware sector size */
+int blkdev_get_sector_size(int fd, int *sector_size);
+
+
+#endif /* BLKDEV_H */
diff --git a/lib/blkdev.c b/lib/blkdev.c
new file mode 100644
index 000000000..29c5d0bd4
--- /dev/null
+++ b/lib/blkdev.c
@@ -0,0 +1,93 @@
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include "blkdev.h"
+#include "linux_version.h"
+
+/* get size in bytes */
+int
+blkdev_get_size(int fd, unsigned long long *bytes)
+{
+ unsigned long size;
+ int ver = get_linux_version();
+
+ /* kernels 2.4.15-2.4.17, had a broken BLKGETSIZE64 */
+ if (ver >= KERNEL_VERSION (2,6,0) ||
+ (ver >= KERNEL_VERSION (2,4,18) && ver < KERNEL_VERSION (2,5,0))) {
+
+ if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
+ return 0;
+ }
+ if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
+ *bytes = ((unsigned long long)size << 9);
+ return 0;
+ }
+
+ return -1;
+}
+
+/* get 512-byte sector count */
+int
+blkdev_get_sectors(int fd, unsigned long long *sectors)
+{
+ unsigned long long bytes;
+
+ if (blkdev_get_size(fd, &bytes) == 0) {
+ *sectors = (bytes >> 9);
+ return 0;
+ }
+
+ return -1;
+}
+
+/* get hardware sector size */
+int
+blkdev_get_sector_size(int fd, int *sector_size)
+{
+ if (get_linux_version() < KERNEL_VERSION(2,3,3)) {
+ *sector_size = DEFAULT_SECTOR_SIZE;
+ return 0;
+ }
+ if (ioctl(fd, BLKSSZGET, sector_size) >= 0)
+ return 0;
+
+ return -1;
+}
+
+
+#ifdef MAIN_TEST_BLKDEV
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <err.h>
+int
+main(int argc, char **argv)
+{
+ unsigned long long bytes;
+ unsigned long long sectors;
+ int sector_size;
+ int fd;
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s device\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ if ((fd = open(argv[1], O_RDONLY)) < 0)
+ err(EXIT_FAILURE, "open %s failed", argv[1]);
+
+ if (blkdev_get_size(fd, &bytes) < 0)
+ err(EXIT_FAILURE, "blkdev_get_size() failed");
+ if (blkdev_get_sectors(fd, &sectors) < 0)
+ err(EXIT_FAILURE, "blkdev_get_sectors() failed");
+ if (blkdev_get_sector_size(fd, &sector_size) < 0)
+ err(EXIT_FAILURE, "blkdev_get_sector_size() failed");
+
+ printf("bytes %llu\n", bytes);
+ printf("sectors %llu\n", sectors);
+ printf("sectorsize %d\n", sector_size);
+
+ return EXIT_SUCCESS;
+}
+#endif /* TEST_PROGRAM */
diff --git a/tests/helpers/Makefile.am b/tests/helpers/Makefile.am
index b6f0557f4..0f28a7f8a 100644
--- a/tests/helpers/Makefile.am
+++ b/tests/helpers/Makefile.am
@@ -1,5 +1,9 @@
include $(top_srcdir)/config/include-Makefile.am
-noinst_PROGRAMS = mnt_test_sysinfo
+noinst_PROGRAMS = mnt_test_sysinfo test_blkdev
mnt_test_sysinfo_SOURCES = mnt_test_sysinfo.c
+test_blkdev_SOURCES = $(top_srcdir)/lib/blkdev.c \
+ $(top_srcdir)/lib/linux_version.c
+test_blkdev_CFLAGS = -DMAIN_TEST_BLKDEV
+