Karel Zak <kzak@xxxxxxxxxx> wrote: > What about a new file lib/linux_version.c ? > > See linux_version_code() in mkswap. I think it's more generic > implementation. yes, that's nicer. Somehow I prefer get_linux_version() and kernel_version() from gpt.c, so I used that. Stefan Krah remarks: - changed (%u -> %d) in sscanf() of get_linux_version() - added blkdev_get_sector_size() - took over the kernel < 2.3.3 workaround from fdisk.c, but could not find the reason for the workaround. ======================================================================== diff --git a/lib/blkdevsize.c b/lib/blkdevsize.c new file mode 100644 index 0000000..834b00c --- /dev/null +++ b/lib/blkdevsize.c @@ -0,0 +1,95 @@ +#include <sys/types.h> +#include <sys/ioctl.h> +#include "blockdev.h" + + +#define BLKSSZGET _IO(0x12,104) /* hardware sector size (int *arg) */ +#define BLKGETSIZE _IO(0x12,96) /* size in 512-byte sectors (ulong *arg) */ +#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* size in bytes (u64 *arg) */ + + +/* get size in bytes */ +int +blkdev_get_size(int fd, unsigned long long *bytes) +{ + unsigned long size; + + /* kernels 2.4.15-2.4.17 had a broken BLKGETSIZE64 */ + if (get_linux_version() >= kernel_version(2,6,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) +{ + /* FIXME: supply reason for this */ + if (get_linux_version() < kernel_version(2,3,3)) { + *sector_size = 512; + return 0; + } + if (ioctl(fd, BLKSSZGET, sector_size) >= 0) + return 0; + + return -1; +} + + +#ifdef TEST_PROGRAM +#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(1); + } + + if ((fd = open(argv[1], O_RDONLY)) < 0) + err(1, "error"); + + if (blkdev_get_size(fd, &bytes) < 0) + err(1, "error"); + if (blkdev_get_sectors(fd, §ors) < 0) + err(1, "error"); + if (blkdev_get_sector_size(fd, §or_size) < 0) + err(1, "error"); + + + printf("bytes: %llu sectors: %llu sector size: %d\n", bytes, sectors, sector_size); + + return 0; +} +#endif /* TEST_PROGRAM */ + ======================================================================== diff --git a/lib/blockdev.h b/lib/blockdev.h new file mode 100644 index 0000000..2d6e6c0 --- /dev/null +++ b/lib/blockdev.h @@ -0,0 +1,17 @@ +#ifndef BLOCKDEV_H +#define BLOCKDEV_H + + +int kernel_version(int p, int q, int r); +int get_linux_version(void); + + +/* 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 /* BLOCKDEV_H */ ======================================================================== diff --git a/lib/linux_version.c b/lib/linux_version.c new file mode 100644 index 0000000..5b40bc3 --- /dev/null +++ b/lib/linux_version.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <sys/utsname.h> +#include "blockdev.h" + + +int +kernel_version (int p, int q, int r) +{ + return 65536*p + 256*q + r; +} + +int +get_linux_version (void) +{ + static int kver = -1; + struct utsname uts; + int major; + int minor; + int teeny; + + if (kver != -1) + return kver; + if (uname (&uts)) + return kver = 0; + if (sscanf (uts.release, "%d.%d.%d", &major, &minor, &teeny) != 3) + return kver = 0; + return kver = kernel_version (major, minor, teeny); +} + + - To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html