* [PATCH v2] erofs-utils: get block device size correctly
@ 2019-08-07 14:36 shenmeng999
2019-08-07 18:26 ` Gao Xiang
0 siblings, 1 reply; 4+ messages in thread
From: shenmeng999 @ 2019-08-07 14:36 UTC (permalink / raw)
From: shenmeng996 <shenmeng999@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>
---
lib/io.c | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
mode change 100644 => 100755 lib/io.c
diff --git a/lib/io.c b/lib/io.c
old mode 100644
new mode 100755
index 93328d3..a93c509
--- a/lib/io.c
+++ b/lib/io.c
@@ -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);
--
1.8.3.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2] erofs-utils: get block device size correctly
2019-08-07 14:36 [PATCH v2] erofs-utils: get block device size correctly shenmeng999
@ 2019-08-07 18:26 ` Gao Xiang
2019-08-08 2:50 ` shenmeng999
0 siblings, 1 reply; 4+ messages in thread
From: Gao Xiang @ 2019-08-07 18:26 UTC (permalink / raw)
Hi shenmeng,
On Wed, Aug 07, 2019@10:36:55PM +0800, shenmeng999@126.com 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
diff --git a/configure.ac b/configure.ac
index 6f4eacc..fcdf30a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
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 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);
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2] erofs-utils: get block device size correctly
2019-08-07 18:26 ` Gao Xiang
@ 2019-08-08 2:50 ` shenmeng999
2019-08-08 3:20 ` Gao Xiang
0 siblings, 1 reply; 4+ messages in thread
From: shenmeng999 @ 2019-08-08 2:50 UTC (permalink / raw)
Hi hsiangkao:
I agree with your modification.It's getting better.Thanks!
At 2019-08-08 02:26:50, "Gao Xiang" <hsiangkao at aol.com>, said:
>Hi shenmeng,
>
>On Wed, Aug 07, 2019@10:36:55PM +0800, shenmeng999@126.com 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
>
>diff --git a/configure.ac b/configure.ac
>index 6f4eacc..fcdf30a 100644
>--- a/configure.ac
>+++ b/configure.ac
>@@ -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
>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 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);
>--
>2.17.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] erofs-utils: get block device size correctly
2019-08-08 2:50 ` shenmeng999
@ 2019-08-08 3:20 ` Gao Xiang
0 siblings, 0 replies; 4+ messages in thread
From: Gao Xiang @ 2019-08-08 3:20 UTC (permalink / raw)
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
>
>
> At 2019-08-08 02:26:50, "Gao Xiang" <hsiangkao at aol.com>, said:
> >Hi shenmeng,
> >
> >On Wed, Aug 07, 2019@10:36:55PM +0800, shenmeng999@126.com 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
> >
> >diff --git a/configure.ac b/configure.ac
> >index 6f4eacc..fcdf30a 100644
> >--- a/configure.ac
> >+++ b/configure.ac
> >@@ -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
> >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 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);
> >--
> >2.17.1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2019-08-08 3:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-07 14:36 [PATCH v2] erofs-utils: get block device size correctly shenmeng999
2019-08-07 18:26 ` Gao Xiang
2019-08-08 2:50 ` shenmeng999
2019-08-08 3:20 ` Gao Xiang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.