[WIP] [PATCH 6/5] erofs-utils: fuse: get rid of duplicated I/O ops

Gao Xiang hsiangkao at aol.com
Sun Oct 25 02:27:18 AEDT 2020


[ will be folded to the original patch. ]

Signed-off-by: Gao Xiang <hsiangkao at aol.com>
---
 fuse/Makefile.am   |  2 +-
 fuse/disk_io.c     | 72 ----------------------------------------------
 fuse/disk_io.h     | 21 --------------
 fuse/init.c        |  6 ++--
 fuse/main.c        |  4 +--
 fuse/namei.c       | 13 +++++----
 fuse/read.c        | 18 ++++++------
 fuse/readir.c      | 10 +++----
 fuse/zmap.c        | 10 +++----
 include/erofs/io.h |  1 +
 lib/io.c           | 16 +++++++++++
 11 files changed, 49 insertions(+), 124 deletions(-)
 delete mode 100644 fuse/disk_io.c
 delete mode 100644 fuse/disk_io.h

diff --git a/fuse/Makefile.am b/fuse/Makefile.am
index 8b8c4e10d90d..dc8839c84d73 100644
--- a/fuse/Makefile.am
+++ b/fuse/Makefile.am
@@ -3,7 +3,7 @@
 
 AUTOMAKE_OPTIONS = foreign
 bin_PROGRAMS     = erofsfuse
-erofsfuse_SOURCES = main.c dentry.c getattr.c namei.c read.c disk_io.c init.c open.c readir.c zmap.c
+erofsfuse_SOURCES = main.c dentry.c getattr.c namei.c read.c init.c open.c readir.c zmap.c
 if ENABLE_LZ4
 erofsfuse_SOURCES += decompress.c
 endif
diff --git a/fuse/disk_io.c b/fuse/disk_io.c
deleted file mode 100644
index bb1ee9a202db..000000000000
--- a/fuse/disk_io.c
+++ /dev/null
@@ -1,72 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * erofs-utils/fuse/disk_io.c
- *
- * Created by Li Guifu <blucerlee at gmail.com>
- */
-#define _XOPEN_SOURCE 500
-#include "disk_io.h"
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <pthread.h>
-#include <errno.h>
-
-#include "erofs/print.h"
-
-#ifdef __FreeBSD__
-#include <string.h>
-#endif
-
-static const char *erofs_devname;
-static int erofs_devfd = -1;
-static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
-
-int dev_open(const char *path)
-{
-	int fd = open(path, O_RDONLY);
-
-	if (fd < 0)
-		return -errno;
-
-	erofs_devfd = fd;
-	erofs_devname = path;
-
-	return 0;
-}
-
-static inline int pread_wrapper(int fd, void *buf, size_t count, off_t offset)
-{
-	return pread(fd, buf, count, offset);
-}
-
-int dev_read(void *buf, size_t count, off_t offset)
-{
-	ssize_t pread_ret;
-	int lerrno;
-
-	DBG_BUGON(erofs_devfd < 0);
-
-	pthread_mutex_lock(&read_lock);
-	pread_ret = pread_wrapper(erofs_devfd, buf, count, offset);
-	lerrno = errno;
-	erofs_dbg("Disk Read: offset[0x%jx] count[%zd] pread_ret=%zd %s",
-	     offset, count, pread_ret, strerror(lerrno));
-	pthread_mutex_unlock(&read_lock);
-	if (count == 0)
-		erofs_warn("Read operation with 0 size");
-
-	DBG_BUGON((size_t)pread_ret != count);
-
-	return pread_ret;
-}
-
-void dev_close(void)
-{
-	if (erofs_devfd >= 0) {
-		close(erofs_devfd);
-		erofs_devfd = -1;
-	}
-}
diff --git a/fuse/disk_io.h b/fuse/disk_io.h
deleted file mode 100644
index d2c3dd598bc0..000000000000
--- a/fuse/disk_io.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * erofs-utils/fuse/disk_io.h
- *
- * Created by Li Guifu <blucerlee at gmail.com>
- */
-#ifndef __DISK_IO_H
-#define __DISK_IO_H
-
-#include "erofs/defs.h"
-#include "erofs/internal.h"
-
-int dev_open(const char *path);
-void dev_close(void);
-int dev_read(void *buf, size_t count, off_t offset);
-
-static inline int dev_read_blk(void *buf, uint32_t nr)
-{
-	return dev_read(buf, EROFS_BLKSIZ, blknr_to_addr(nr));
-}
-#endif
diff --git a/fuse/init.c b/fuse/init.c
index 6917e995370b..867f4bf90e9a 100644
--- a/fuse/init.c
+++ b/fuse/init.c
@@ -10,7 +10,7 @@
 #include <asm-generic/errno-base.h>
 
 #include "namei.h"
-#include "disk_io.h"
+#include "erofs/io.h"
 #include "erofs/print.h"
 
 #define STR(_X) (#_X)
@@ -43,8 +43,8 @@ int erofs_init_super(void)
 	struct erofs_super_block *sb;
 
 	memset(buf, 0, sizeof(buf));
-	ret = dev_read_blk(buf, 0);
-	if (ret != EROFS_BLKSIZ) {
+	ret = blk_read(buf, 0, 1);
+	if (ret < 0) {
 		erofs_err("Failed to read super block ret=%d", ret);
 		return -EINVAL;
 	}
diff --git a/fuse/main.c b/fuse/main.c
index 9008fea32639..26f49f6fc299 100644
--- a/fuse/main.c
+++ b/fuse/main.c
@@ -17,7 +17,7 @@
 #include "getattr.h"
 #include "open.h"
 #include "readir.h"
-#include "disk_io.h"
+#include "erofs/io.h"
 
 /* XXX: after liberofs is linked in, it should be removed */
 struct erofs_configure cfg;
@@ -142,7 +142,7 @@ int main(int argc, char *argv[])
 
 	cfg.c_dbg_lvl = fusecfg.debug_lvl;
 
-	if (dev_open(fusecfg.disk) < 0) {
+	if (dev_open_ro(fusecfg.disk) < 0) {
 		fprintf(stderr, "Failed to open disk:%s\n", fusecfg.disk);
 		goto exit;
 	}
diff --git a/fuse/namei.c b/fuse/namei.c
index 172e1bcdb457..79273f89be1b 100644
--- a/fuse/namei.c
+++ b/fuse/namei.c
@@ -15,7 +15,7 @@
 
 #include "erofs/defs.h"
 #include "erofs/print.h"
-#include "disk_io.h"
+#include "erofs/io.h"
 #include "dentry.h"
 #include "init.h"
 
@@ -55,8 +55,8 @@ int erofs_iget_by_nid(erofs_nid_t nid, struct erofs_vnode *vi)
 	const erofs_off_t addr = nid2addr(nid);
 	const size_t size = EROFS_BLKSIZ - erofs_blkoff(addr);
 
-	ret = dev_read(buf, size, addr);
-	if (ret != (int)size)
+	ret = dev_read(buf, addr, size);
+	if (ret < 0)
 		return -EIO;
 
 	v1 = (struct erofs_inode_compact *)buf;
@@ -160,7 +160,8 @@ struct dcache_entry *disk_lookup(struct dcache_entry *parent, const char *name,
 
 	nr_cnt = 0;
 	while (nr_cnt < dir_nr) {
-		if (dev_read_blk(buf, blkno + nr_cnt) != EROFS_BLKSIZ)
+		ret = blk_read(buf, blkno + nr_cnt, 1);
+		if (ret < 0)
 			return NULL;
 
 		entry = list_name(buf, parent, name, name_len, EROFS_BLKSIZ);
@@ -176,8 +177,8 @@ struct dcache_entry *disk_lookup(struct dcache_entry *parent, const char *name,
 			v.inode_isize + v.xattr_isize;
 
 		memset(buf, 0, sizeof(buf));
-		ret = dev_read(buf, dir_off, dir_addr);
-		if (ret < 0 && (uint32_t)ret != dir_off)
+		ret = dev_read(buf, dir_addr, dir_off);
+		if (ret < 0)
 			return NULL;
 
 		entry = list_name(buf, parent, name, name_len, dir_off);
diff --git a/fuse/read.c b/fuse/read.c
index 86a2bfa1b165..8e332e89478f 100644
--- a/fuse/read.c
+++ b/fuse/read.c
@@ -15,7 +15,7 @@
 #include "erofs/internal.h"
 #include "erofs/print.h"
 #include "namei.h"
-#include "disk_io.h"
+#include "erofs/io.h"
 #include "init.h"
 #include "decompress.h"
 
@@ -31,8 +31,8 @@ size_t erofs_read_data(struct erofs_vnode *vnode, char *buffer,
 	while (rdsz < sum) {
 		size_t count = min(EROFS_BLKSIZ, (uint32_t)(sum - rdsz));
 
-		ret = dev_read(buffer + rdsz, count, addr + rdsz);
-		if (ret < 0 || (size_t)ret != count)
+		ret = dev_read(buffer + rdsz, addr + rdsz, count);
+		if (ret < 0)
 			return -EIO;
 		rdsz += count;
 	}
@@ -57,8 +57,8 @@ size_t erofs_read_data_inline(struct erofs_vnode *vnode, char *buffer,
 	while (rdsz < sum) {
 		size_t count = min(EROFS_BLKSIZ, (uint32_t)(sum - rdsz));
 
-		ret = dev_read(buffer + rdsz, count, addr + rdsz);
-		if (ret < 0 || (uint32_t)ret != count)
+		ret = dev_read(buffer + rdsz, addr + rdsz, count);
+		if (ret < 0)
 			return -EIO;
 		rdsz += count;
 	}
@@ -67,8 +67,8 @@ size_t erofs_read_data_inline(struct erofs_vnode *vnode, char *buffer,
 		goto finished;
 
 	addr = nid2addr(vnode->nid) + vnode->inode_isize + vnode->xattr_isize;
-	ret = dev_read(buffer + rdsz, suminline, addr);
-	if (ret < 0 || (size_t)ret != suminline)
+	ret = dev_read(buffer + rdsz, addr, suminline);
+	if (ret < 0)
 		return -EIO;
 	rdsz += suminline;
 
@@ -104,8 +104,8 @@ size_t erofs_read_data_compression(struct erofs_vnode *vnode, char *buffer,
 			continue;
 		}
 
-		ret = dev_read(raw, EROFS_BLKSIZ, map.m_pa);
-		if (ret < 0 || (size_t)ret != EROFS_BLKSIZ)
+		ret = dev_read(raw, map.m_pa, EROFS_BLKSIZ);
+		if (ret < 0)
 			return -EIO;
 
 		algorithmformat = map.m_flags & EROFS_MAP_ZIPPED ?
diff --git a/fuse/readir.c b/fuse/readir.c
index 0fefcd8fd0cb..8111047803df 100644
--- a/fuse/readir.c
+++ b/fuse/readir.c
@@ -13,7 +13,7 @@
 #include "erofs/internal.h"
 #include "erofs_fs.h"
 #include "namei.h"
-#include "disk_io.h"
+#include "erofs/io.h"
 #include "erofs/print.h"
 #include "init.h"
 
@@ -98,8 +98,8 @@ int erofs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
 	while (nr_cnt < dir_nr) {
 		memset(dirsbuf, 0, sizeof(dirsbuf));
-		ret = dev_read_blk(dirsbuf, v.raw_blkaddr + nr_cnt);
-		if (ret != EROFS_BLKSIZ)
+		ret = blk_read(dirsbuf, v.raw_blkaddr + nr_cnt, 1);
+		if (ret < 0)
 			return -EIO;
 		fill_dir(dirsbuf, filler, buf, EROFS_BLKSIZ);
 		++nr_cnt;
@@ -111,8 +111,8 @@ int erofs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 		addr = nid2addr(nid) + v.inode_isize + v.xattr_isize;
 
 		memset(dirsbuf, 0, sizeof(dirsbuf));
-		ret = dev_read(dirsbuf, dir_off, addr);
-		if (ret < 0 || (uint32_t)ret != dir_off)
+		ret = dev_read(dirsbuf, addr, dir_off);
+		if (ret < 0)
 			return -EIO;
 		fill_dir(dirsbuf, filler, buf, dir_off);
 	}
diff --git a/fuse/zmap.c b/fuse/zmap.c
index 85034d385c58..9860b770362c 100644
--- a/fuse/zmap.c
+++ b/fuse/zmap.c
@@ -10,7 +10,7 @@
  * Modified by Huang Jianan <huangjianan at oppo.com>
  */
 #include "init.h"
-#include "disk_io.h"
+#include "erofs/io.h"
 #include "erofs/print.h"
 
 int z_erofs_fill_inode(struct erofs_vnode *vi)
@@ -42,8 +42,8 @@ static int z_erofs_fill_inode_lazy(struct erofs_vnode *vi)
 
 	pos = round_up(nid2addr(vi->nid) + vi->inode_isize + vi->xattr_isize, 8);
 
-	ret = dev_read(buf, 8, pos);
-	if (ret < 0 && (uint32_t)ret != 8)
+	ret = dev_read(buf, pos, 8);
+	if (ret < 0)
 		return -EIO;
 
 	h = (struct z_erofs_map_header *)buf;
@@ -97,8 +97,8 @@ static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
 	if (map->index == eblk)
 		return 0;
 
-	ret = dev_read(mpage, EROFS_BLKSIZ, blknr_to_addr(eblk));
-	if (ret < 0 && (uint32_t)ret != EROFS_BLKSIZ)
+	ret = blk_read(mpage, eblk, 1);
+	if (ret < 0)
 		return -EIO;
 
 	map->index = eblk;
diff --git a/include/erofs/io.h b/include/erofs/io.h
index a23de64541c6..557424578ece 100644
--- a/include/erofs/io.h
+++ b/include/erofs/io.h
@@ -17,6 +17,7 @@
 #endif
 
 int dev_open(const char *devname);
+int dev_open_ro(const char *dev);
 void dev_close(void);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_read(void *buf, u64 offset, size_t len);
diff --git a/lib/io.c b/lib/io.c
index 4f5d9a6edaa4..d835f34da50f 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -108,6 +108,22 @@ int dev_open(const char *dev)
 	return 0;
 }
 
+/* XXX: temporary soluation. Disk I/O implementation needs to be refactored. */
+int dev_open_ro(const char *dev)
+{
+	int fd = open(dev, O_RDONLY | O_BINARY);
+
+	if (fd < 0) {
+		erofs_err("failed to open(%s).", dev);
+		return -errno;
+	}
+
+	erofs_devfd = fd;
+	erofs_devname = dev;
+	erofs_devsz = INT64_MAX;
+	return 0;
+}
+
 u64 dev_length(void)
 {
 	return erofs_devsz;
-- 
2.24.0



More information about the Linux-erofs mailing list