erofs-utils: get block device size correctly

fstat return block device's size of zero.
use ioctl to get block device's size.

Signed-off-by: shenmeng996 <shenmeng999@126.com>
[ Gao Xiang: check header files in configure.ac ]
Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
diff --git a/configure.ac b/configure.ac
index 6f4eacc..fcdf30a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -73,12 +73,14 @@
 	fcntl.h
 	inttypes.h
 	linux/falloc.h
+	linux/fs.h
 	linux/types.h
 	limits.h
 	stddef.h
 	stdint.h
 	stdlib.h
 	string.h
+	sys/ioctl.h
 	sys/stat.h
 	sys/time.h
 	unistd.h
diff --git a/lib/io.c b/lib/io.c
index 93328d3..15c5a35 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -9,7 +9,11 @@
 #define _LARGEFILE64_SOURCE
 #define _GNU_SOURCE
 #include <sys/stat.h>
+#include <sys/ioctl.h>
 #include "erofs/io.h"
+#ifdef HAVE_LINUX_FS_H
+#include <linux/fs.h>
+#endif
 #ifdef HAVE_LINUX_FALLOC_H
 #include <linux/falloc.h>
 #endif
@@ -21,6 +25,26 @@
 static int erofs_devfd = -1;
 static u64 erofs_devsz;
 
+int dev_get_blkdev_size(int fd, u64 *bytes)
+{
+	errno = ENOTSUP;
+#ifdef BLKGETSIZE64
+	if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
+		return 0;
+#endif
+
+#ifdef BLKGETSIZE
+	{
+		unsigned long size;
+		if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
+			*bytes = ((u64)size << 9);
+			return 0;
+		}
+	}
+#endif
+	return -errno;
+}
+
 void dev_close(void)
 {
 	close(erofs_devfd);
@@ -49,7 +73,12 @@
 
 	switch (st.st_mode & S_IFMT) {
 	case S_IFBLK:
-		erofs_devsz = st.st_size;
+		ret = dev_get_blkdev_size(fd, &erofs_devsz);
+		if (ret) {
+			erofs_err("failed to get block device size(%s).", dev);
+			close(fd);
+			return ret;
+		}
 		break;
 	case S_IFREG:
 		ret = ftruncate(fd, 0);