Skip to contentSkip to conversation

erofs-utils: get block device size correctly

PATCH Superseded [email protected] 3 participants 4 messages Aug 7, 2019 – Aug 8, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 erofs-utils: get block device size correctly

From: shenmeng996 <[email protected]>

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

Signed-off-by shenmeng996 <shenmeng999 at 126.com>
---
 lib/io.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 lib/io.c
lib/io.c +25 -1
old mode 100644
new mode 100755
index 93328d3..a93c509
@@ -9,6 +9,8 @@
 #define _LARGEFILE64_SOURCE
 #define _GNU_SOURCE
 #include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
 #include "erofs/io.h"
 #ifdef HAVE_LINUX_FALLOC_H
 #include <linux/falloc.h>
@@ -21,6 +23,23 @@ static const char *erofs_devname;
 static int erofs_devfd = -1;
 static u64 erofs_devsz;
 
+int dev_get_blkdev_size(int fd, u64 *bytes)
+{
+	unsigned long size;
+
+#ifdef BLKGETSIZE64
+	if (ioctl(fd, BLKGETSIZE64, bytes) >= 0)
+		return 0;
+#endif
+
+	if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
+		*bytes = ((u64)size << 9);
+		return 0;
+	}
+
+	return -errno;
+}
+
 void dev_close(void)
 {
 	close(erofs_devfd);
@@ -49,7 +68,12 @@ int dev_open(const char *dev)
 
 	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);
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/1 erofs-utils: get block device size correctly

Hi shenmeng,

Quoted 6 lines · On Wed, Aug 07, 2019@10:36:55PM +0800, [email protected] wrote:

From: shenmeng996 <shenmeng999 at 126.com>

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

Signed-off-by shenmeng996 <shenmeng999 at 126.com>

Thanks for your patch v2 :)

It looks good to me, and I update this patch so that
autoconf can check these new header files.
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/commit/?h=experimental&id=0f225449962b4a9716985e7530a395a9500d0de6
Do you agree with this modification? please share your thought about this.

Thanks,
Gao Xiang

configure.ac +2 -0
index 6f4eacc..fcdf30a 100644
@@ -73,12 +73,14 @@ AC_CHECK_HEADERS(m4_flatten([
 	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
lib/io.c +30 -1
index 93328d3..15c5a35 100644
@@ -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 const char *erofs_devname;
 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 @@ int dev_open(const char *dev)
 
 	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);
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 1/1 erofs-utils: get block device size correctly

Hi hsiangkao:
I agree with your modification.It's getting better.Thanks!

Quoted 99 lines · At 2019-08-08 02:26:50, "Gao Xiang" <hsiangkao at aol.com>, said:

Hi shenmeng,

Quoted 6 lines · On Wed, Aug 07, 2019@10:36:55PM +0800, [email protected] wrote:

From: shenmeng996 <shenmeng999 at 126.com>

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

Signed-off-by shenmeng996 <shenmeng999 at 126.com>

Thanks for your patch v2 :)

It looks good to me, and I update this patch so that
autoconf can check these new header files.
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/commit/?h=experimental&id=0f225449962b4a9716985e7530a395a9500d0de6
Do you agree with this modification? please share your thought about this.

Thanks,
Gao Xiang

configure.ac +2 -0
index 6f4eacc..fcdf30a 100644
@@ -73,12 +73,14 @@ AC_CHECK_HEADERS(m4_flatten([
 	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
lib/io.c +30 -1
index 93328d3..15c5a35 100644
@@ -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 const char *erofs_devname;
 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 @@ int dev_open(const char *dev)
 
 	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);
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 1/1 erofs-utils: get block device size correctly

Quoted 2 lines · On Thu, Aug 08, 2019@10:50:24AM +0800, shenmeng999 wrote:

Hi hsiangkao:
I agree with your modification.It's getting better.Thanks!

OK, Let me update it to dev branch as well this evening :)

Thanks,
Gao Xiang

Quoted 102 lines
Quoted 99 lines · At 2019-08-08 02:26:50, "Gao Xiang" <hsiangkao at aol.com>, said:

Hi shenmeng,

Quoted 6 lines · On Wed, Aug 07, 2019@10:36:55PM +0800, [email protected] wrote:

From: shenmeng996 <shenmeng999 at 126.com>

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

Signed-off-by shenmeng996 <shenmeng999 at 126.com>

Thanks for your patch v2 :)

It looks good to me, and I update this patch so that
autoconf can check these new header files.
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/commit/?h=experimental&id=0f225449962b4a9716985e7530a395a9500d0de6
Do you agree with this modification? please share your thought about this.

Thanks,
Gao Xiang

configure.ac +2 -0
index 6f4eacc..fcdf30a 100644
@@ -73,12 +73,14 @@ AC_CHECK_HEADERS(m4_flatten([
 	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
lib/io.c +30 -1
index 93328d3..15c5a35 100644
@@ -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 const char *erofs_devname;
 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 @@ int dev_open(const char *dev)
 
 	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);
Signature · 1 line
2.17.1