Skip to contentSkip to conversation

erofs-utils: remove global erofs_devfd

PATCH New Huang Jianan 1 participant 4 messages Sep 9, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 erofs-utils: remove global erofs_devfd

Move global erofs_dev related variables to sbi->dev, which will make erofs-utils more library friendly.

---
 dump/main.c              |  20 ++++--
 fsck/main.c              |  28 +++++---
 fuse/main.c              |  14 ++--
 include/erofs/cache.h    |   6 +-
 include/erofs/config.h   |   2 +
 include/erofs/internal.h |   8 +++
 include/erofs/io.h       |  37 ++++++-----
 lib/blobchunk.c          |  11 ++--
 lib/cache.c              |  12 ++--
 lib/compress.c           |  11 ++--
 lib/config.c             |  12 ++++
 lib/data.c               |   6 +-
 lib/inode.c              |  28 ++++----
 lib/io.c                 | 139 ++++++++++++++++++++-------------------
 lib/namei.c              |   5 +-
 lib/super.c              |   4 +-
 lib/xattr.c              |  17 ++---
 lib/zmap.c               |   4 +-
 mkfs/main.c              |  19 ++++--
 19 files changed, 225 insertions(+), 158 deletions(-)
dump/main.c +14 -6
index f2a09b6..07fa151 100644
@@ -152,7 +152,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 			usage();
 			exit(0);
 		case 3:
-			err = blob_open_ro(optarg);
+			err = blob_open_ro(sbi.dev, optarg);
 			if (err)
 				return err;
 			++sbi.extra_devices;
@@ -599,17 +599,24 @@ int main(int argc, char **argv)
 	int err;
 
 	erofs_init_configure();
+
+	sbi.dev = erofs_init_dev();
+	if (IS_ERR(sbi.dev)) {
+		err = PTR_ERR(sbi.dev);
+		goto exit;
+	}
+
 	err = erofsdump_parse_options_cfg(argc, argv);
 	if (err) {
 		if (err == -EINVAL)
 			usage();
-		goto exit;
+		goto exit_blob_close;
 	}
 
-	err = dev_open_ro(cfg.c_img_path);
+	err = dev_open_ro(sbi.dev, cfg.c_img_path);
 	if (err) {
 		erofs_err("failed to open image file");
-		goto exit;
+		goto exit_blob_close;
 	}
 
 	err = erofs_read_superblock();
@@ -639,9 +646,10 @@ int main(int argc, char **argv)
 exit_put_super:
 	erofs_put_super();
 exit_dev_close:
-	dev_close();
+	dev_close(sbi.dev);
+exit_blob_close:
+	blob_closeall(sbi.dev);
 exit:
-	blob_closeall();
 	erofs_exit_configure();
 	return err;
 }
fsck/main.c +18 -10
index 410e756..1c8f567 100644
@@ -143,7 +143,7 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)
 			}
 			break;
 		case 3:
-			ret = blob_open_ro(optarg);
+			ret = blob_open_ro(sbi.dev, optarg);
 			if (ret)
 				return ret;
 			++sbi.extra_devices;
@@ -263,7 +263,7 @@ static int erofs_check_sb_chksum(void)
 	u32 crc;
 	struct erofs_super_block *sb;
 
-	ret = blk_read(0, buf, 0, 1);
+	ret = blk_read(sbi.dev, 0, buf, 0, 1);
 	if (ret) {
 		erofs_err("failed to read superblock to check checksum: %d",
 			  ret);
@@ -291,6 +291,7 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 	unsigned int ofs, xattr_shared_count;
 	struct erofs_xattr_ibody_header *ih;
 	struct erofs_xattr_entry *entry;
+	struct erofs_device *dev = sbi.dev;
 	int i, remaining = inode->xattr_isize, ret = 0;
 	char buf[EROFS_BLKSIZ];
 
@@ -309,7 +310,7 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 	}
 
 	addr = iloc(inode->nid) + inode->inode_isize;
-	ret = dev_read(0, buf, addr, xattr_hdr_size);
+	ret = dev_read(dev, 0, buf, addr, xattr_hdr_size);
 	if (ret < 0) {
 		erofs_err("failed to read xattr header @ nid %llu: %d",
 			  inode->nid | 0ULL, ret);
@@ -339,7 +340,7 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 	while (remaining > 0) {
 		unsigned int entry_sz;
 
-		ret = dev_read(0, buf, addr, xattr_entry_size);
+		ret = dev_read(dev, 0, buf, addr, xattr_entry_size);
 		if (ret) {
 			erofs_err("failed to read xattr entry @ nid %llu: %d",
 				  inode->nid | 0ULL, ret);
@@ -445,7 +446,7 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 			BUG_ON(!buffer);
 		}
 
-		ret = dev_read(mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
+		ret = dev_read(sbi.dev, mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
 		if (ret < 0) {
 			erofs_err("failed to read data of m_pa %" PRIu64 ", m_plen %" PRIu64 " @ nid %llu: %d",
 				  mdev.m_pa, map.m_plen, inode->nid | 0ULL,
@@ -781,6 +782,12 @@ int main(int argc, char **argv)
 
 	erofs_init_configure();
 
+	sbi.dev = erofs_init_dev();
+	if (IS_ERR(sbi.dev)) {
+		err = PTR_ERR(sbi.dev);
+		goto exit;
+	}
+
 	fsckcfg.physical_blocks = 0;
 	fsckcfg.logical_blocks = 0;
 	fsckcfg.extract_path = NULL;
@@ -799,13 +806,13 @@ int main(int argc, char **argv)
 	if (err) {
 		if (err == -EINVAL)
 			usage();
-		goto exit;
+		goto exit_blob_close;
 	}
 
-	err = dev_open_ro(cfg.c_img_path);
+	err = dev_open_ro(sbi.dev, cfg.c_img_path);
 	if (err) {
 		erofs_err("failed to open image file");
-		goto exit;
+		goto exit_blob_close;
 	}
 
 	err = erofs_read_superblock();
@@ -844,9 +851,10 @@ int main(int argc, char **argv)
 exit_put_super:
 	erofs_put_super();
 exit_dev_close:
-	dev_close();
+	dev_close(sbi.dev);
+exit_blob_close:
+	blob_closeall(sbi.dev);
 exit:
-	blob_closeall();
 	erofs_exit_configure();
 	return err ? 1 : 0;
 }
fuse/main.c +10 -4
index f1d1b47..ee86e50 100644
@@ -239,7 +239,7 @@ static int optional_opt_func(void *data, const char *arg, int key,
 
 	switch (key) {
 	case 1:
-		ret = blob_open_ro(arg + sizeof("--device=") - 1);
+		ret = blob_open_ro(sbi.dev, arg + sizeof("--device=") - 1);
 		if (ret)
 			return -1;
 		++sbi.extra_devices;
@@ -297,6 +297,12 @@ int main(int argc, char *argv[])
 	erofs_init_configure();
 	printf("%s %s\n", basename(argv[0]), cfg.c_version);
 
+	sbi.dev = erofs_init_dev();
+	if (IS_ERR(sbi.dev)) {
+		ret = PTR_ERR(sbi.dev);
+		goto err;
+	}
+
 #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
 	if (signal(SIGSEGV, signal_handle_sigsegv) == SIG_ERR) {
 		fprintf(stderr, "failed to initialize signals\n");
@@ -320,7 +326,7 @@ int main(int argc, char *argv[])
 	cfg.c_offset = fusecfg.offset;
 
 	erofsfuse_dumpcfg();
-	ret = dev_open_ro(fusecfg.disk);
+	ret = dev_open_ro(sbi.dev, fusecfg.disk);
 	if (ret) {
 		fprintf(stderr, "failed to open: %s\n", fusecfg.disk);
 		goto err_fuse_free_args;
@@ -336,8 +342,8 @@ int main(int argc, char *argv[])
 
 	erofs_put_super();
 err_dev_close:
-	blob_closeall();
-	dev_close();
+	blob_closeall(sbi.dev);
+	dev_close(sbi.dev);
 err_fuse_free_args:
 	fuse_opt_free_args(&args);
 err:
include/erofs/cache.h +4 -2
index de12399..e7aec2b 100644
@@ -45,6 +45,7 @@ struct erofs_buffer_head {
 struct erofs_buffer_block {
 	struct list_head list;
 	struct list_head mapped_list;
+	struct erofs_device *dev;
 
 	erofs_blk_t blkaddr;
 	int type;
@@ -98,14 +99,15 @@ static inline bool erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
 struct erofs_buffer_head *erofs_buffer_init(void);
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
 
-struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
+struct erofs_buffer_head *erofs_balloc(struct erofs_device *dev,
+				       int type, erofs_off_t size,
 				       unsigned int required_ext,
 				       unsigned int inline_ext);
 struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
 					int type, unsigned int size);
 
 erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb);
-bool erofs_bflush(struct erofs_buffer_block *bb);
+bool erofs_bflush(struct erofs_device *dev, struct erofs_buffer_block *bb);
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
 
include/erofs/config.h +2 -0
index 539d813..6c96664 100644
@@ -85,6 +85,8 @@ void erofs_init_configure(void);
 void erofs_show_config(void);
 void erofs_exit_configure(void);
 
+struct erofs_device *erofs_init_dev(void);
+
 void erofs_set_fs_root(const char *rootdir);
 const char *erofs_fspath(const char *fullpath);
 
include/erofs/internal.h +8 -0
index 2e0aae8..c4b36e9 100644
@@ -65,12 +65,20 @@ typedef u32 erofs_blk_t;
 
 struct erofs_buffer_head;
 
+struct erofs_device {
+	const char *name;
+	int fd;
+	u64 size;
+	unsigned int nblobs, blobfd[256];
+};
+
 struct erofs_device_info {
 	u32 blocks;
 	u32 mapped_blkaddr;
 };
 
 struct erofs_sb_info {
+	struct erofs_device *dev;
 	struct erofs_device_info *devs;
 
 	u64 total_blocks;
include/erofs/io.h +20 -17
index 0f58c70..abbb2e0 100644
@@ -22,17 +22,20 @@ extern "C"
 #define O_BINARY	0
 #endif
 
-void blob_closeall(void);
-int blob_open_ro(const char *dev);
-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(int device_id, void *buf, u64 offset, size_t len);
-int dev_fillzero(u64 offset, size_t len, bool padding);
-int dev_fsync(void);
-int dev_resize(erofs_blk_t nblocks);
-u64 dev_length(void);
+void blob_closeall(struct erofs_device *dev);
+int blob_open_ro(struct erofs_device *dev, const char *devname);
+int dev_open(struct erofs_device *dev, const char *devname);
+int dev_open_ro(struct erofs_device *dev, const char *devname);
+void dev_close(struct erofs_device *dev);
+int dev_write(struct erofs_device *dev,
+	      const void *buf, u64 offset, size_t len);
+int dev_read(struct erofs_device *dev, int device_id,
+	     void *buf, u64 offset, size_t len);
+int dev_fillzero(struct erofs_device *dev,
+		 u64 offset, size_t len, bool padding);
+int dev_fsync(struct erofs_device *dev);
+int dev_resize(struct erofs_device *dev, erofs_blk_t nblocks);
+u64 dev_length(struct erofs_device *dev);
 
 extern int erofs_devfd;
 
@@ -40,17 +43,17 @@ ssize_t erofs_copy_file_range(int fd_in, erofs_off_t *off_in,
 			      int fd_out, erofs_off_t *off_out,
 			      size_t length);
 
-static inline int blk_write(const void *buf, erofs_blk_t blkaddr,
-			    u32 nblocks)
+static inline int blk_write(struct erofs_device *dev, const void *buf,
+			    erofs_blk_t blkaddr, u32 nblocks)
 {
-	return dev_write(buf, blknr_to_addr(blkaddr),
+	return dev_write(dev, buf, blknr_to_addr(blkaddr),
 			 blknr_to_addr(nblocks));
 }
 
-static inline int blk_read(int device_id, void *buf,
-			   erofs_blk_t start, u32 nblocks)
+static inline int blk_read(struct erofs_device *dev, int device_id,
+			   void *buf, erofs_blk_t start, u32 nblocks)
 {
-	return dev_read(device_id, buf, blknr_to_addr(start),
+	return dev_read(dev, device_id, buf, blknr_to_addr(start),
 			 blknr_to_addr(nblocks));
 }
 
lib/blobchunk.c +6 -5
index 77b0c17..d11d7db 100644
@@ -158,7 +158,7 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 	erofs_droid_blocklist_write_extent(inode, extent_start, extents_blks,
 					   first_extent, true);
 
-	return dev_write(inode->chunkindexes, off, inode->extent_isize);
+	return dev_write(sbi.dev, inode->chunkindexes, off, inode->extent_isize);
 }
 
 int erofs_blob_write_chunked_file(struct erofs_inode *inode)
@@ -214,6 +214,7 @@ err:
 
 int erofs_blob_remap(void)
 {
+	struct erofs_device *dev = sbi.dev;
 	struct erofs_buffer_head *bh;
 	ssize_t length;
 	erofs_off_t pos_in, pos_out;
@@ -229,7 +230,7 @@ int erofs_blob_remap(void)
 		};
 
 		pos_out = erofs_btell(bh_devt, false);
-		ret = dev_write(&dis, pos_out, sizeof(dis));
+		ret = dev_write(dev, &dis, pos_out, sizeof(dis));
 		if (ret)
 			return ret;
 
@@ -237,7 +238,7 @@ int erofs_blob_remap(void)
 		erofs_bdrop(bh_devt, false);
 		return 0;
 	}
-	bh = erofs_balloc(DATA, length, 0, 0);
+	bh = erofs_balloc(dev, DATA, length, 0, 0);
 	if (IS_ERR(bh))
 		return PTR_ERR(bh);
 
@@ -246,7 +247,7 @@ int erofs_blob_remap(void)
 	pos_in = 0;
 	remapped_base = erofs_blknr(pos_out);
 	ret = erofs_copy_file_range(fileno(blobfile), &pos_in,
-				    erofs_devfd, &pos_out, length);
+				    dev->fd, &pos_out, length);
 	bh->op = &erofs_drop_directly_bhops;
 	erofs_bdrop(bh, false);
 	return ret < length ? -EIO : 0;
@@ -287,7 +288,7 @@ int erofs_generate_devtable(void)
 	if (!multidev)
 		return 0;
 
-	bh_devt = erofs_balloc(DEVT, sizeof(dis), 0, 0);
+	bh_devt = erofs_balloc(sbi.dev, DEVT, sizeof(dis), 0, 0);
 	if (IS_ERR(bh_devt))
 		return PTR_ERR(bh_devt);
 
lib/cache.c +7 -5
index c735363..60b0ee6 100644
@@ -45,7 +45,7 @@ int erofs_bh_flush_generic_write(struct erofs_buffer_head *bh, void *buf)
 	erofs_off_t offset = erofs_btell(bh, false);
 
 	DBG_BUGON(nbh->off < bh->off);
-	return dev_write(buf, offset, nbh->off - bh->off);
+	return dev_write(bh->block->dev, buf, offset, nbh->off - bh->off);
 }
 
 static bool erofs_bh_flush_buf_write(struct erofs_buffer_head *bh)
@@ -66,7 +66,7 @@ const struct erofs_bhops erofs_buf_write_bhops = {
 struct erofs_buffer_head *erofs_buffer_init(void)
 {
 	int i, j;
-	struct erofs_buffer_head *bh = erofs_balloc(META, 0, 0, 0);
+	struct erofs_buffer_head *bh = erofs_balloc(sbi.dev, META, 0, 0, 0);
 
 	if (IS_ERR(bh))
 		return bh;
@@ -252,7 +252,8 @@ skip_mapped:
 	return 0;
 }
 
-struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
+struct erofs_buffer_head *erofs_balloc(struct erofs_device *dev,
+				       int type, erofs_off_t size,
 				       unsigned int required_ext,
 				       unsigned int inline_ext)
 {
@@ -284,6 +285,7 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
 		if (!bb)
 			return ERR_PTR(-ENOMEM);
 
+		bb->dev = dev;
 		bb->type = type;
 		bb->blkaddr = NULL_ADDR;
 		bb->buffers.off = 0;
@@ -367,7 +369,7 @@ erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb)
 	return tail_blkaddr;
 }
 
-bool erofs_bflush(struct erofs_buffer_block *bb)
+bool erofs_bflush(struct erofs_device *dev, struct erofs_buffer_block *bb)
 {
 	struct erofs_buffer_block *p, *n;
 	erofs_blk_t blkaddr;
@@ -398,7 +400,7 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 
 		padding = EROFS_BLKSIZ - p->buffers.off % EROFS_BLKSIZ;
 		if (padding != EROFS_BLKSIZ)
-			dev_fillzero(blknr_to_addr(blkaddr) - padding,
+			dev_fillzero(dev, blknr_to_addr(blkaddr) - padding,
 				     padding, true);
 
 		DBG_BUGON(!list_empty(&p->buffers.list));
lib/compress.c +6 -5
index fd02053..ded0399 100644
@@ -161,7 +161,7 @@ static int write_uncompressed_extent(struct z_erofs_vle_compress_ctx *ctx,
 
 	erofs_dbg("Writing %u uncompressed data to block %u",
 		  count, ctx->blkaddr);
-	ret = blk_write(dst, ctx->blkaddr, 1);
+	ret = blk_write(sbi.dev, dst, ctx->blkaddr, 1);
 	if (ret)
 		return ret;
 	return count;
@@ -317,7 +317,7 @@ nocompression:
 			erofs_dbg("Writing %u compressed data to %u of %u blocks",
 				  count, ctx->blkaddr, ctx->compressedblks);
 
-			ret = blk_write(dst - padding, ctx->blkaddr,
+			ret = blk_write(sbi.dev, dst - padding, ctx->blkaddr,
 					ctx->compressedblks);
 			if (ret)
 				return ret;
@@ -625,7 +625,7 @@ int erofs_write_compressed_file(struct erofs_inode *inode)
 	}
 
 	/* allocate main data buffer */
-	bh = erofs_balloc(DATA, 0, 0, 0);
+	bh = erofs_balloc(sbi.dev, DATA, 0, 0, 0);
 	if (IS_ERR(bh)) {
 		ret = PTR_ERR(bh);
 		goto err_close;
@@ -746,6 +746,7 @@ static int erofs_get_compress_algorithm_id(const char *name)
 int z_erofs_build_compr_cfgs(struct erofs_buffer_head *sb_bh)
 {
 	struct erofs_buffer_head *bh = sb_bh;
+	struct erofs_device *dev = sbi.dev;
 	int ret = 0;
 
 	if (sbi.available_compr_algs & (1 << Z_EROFS_COMPRESSION_LZ4)) {
@@ -767,7 +768,7 @@ int z_erofs_build_compr_cfgs(struct erofs_buffer_head *sb_bh)
 			return PTR_ERR(bh);
 		}
 		erofs_mapbh(bh->block);
-		ret = dev_write(&lz4alg, erofs_btell(bh, false),
+		ret = dev_write(dev, &lz4alg, erofs_btell(bh, false),
 				sizeof(lz4alg));
 		bh->op = &erofs_drop_directly_bhops;
 	}
@@ -789,7 +790,7 @@ int z_erofs_build_compr_cfgs(struct erofs_buffer_head *sb_bh)
 			return PTR_ERR(bh);
 		}
 		erofs_mapbh(bh->block);
-		ret = dev_write(&lzmaalg, erofs_btell(bh, false),
+		ret = dev_write(dev, &lzmaalg, erofs_btell(bh, false),
 				sizeof(lzmaalg));
 		bh->op = &erofs_drop_directly_bhops;
 	}
lib/config.c +12 -0
index 3963df2..3cc1d22 100644
@@ -57,6 +57,18 @@ void erofs_exit_configure(void)
 		free(cfg.c_img_path);
 }
 
+struct erofs_device *erofs_init_dev(void)
+{
+	struct erofs_device *dev;
+	dev = calloc(1, sizeof(struct erofs_device));
+	if (!dev)
+		return ERR_PTR(-ENOMEM);
+
+	dev->fd = -1;
+
+	return dev;
+}
+
 static unsigned int fullpath_prefix;	/* root directory prefix length */
 
 void erofs_set_fs_root(const char *rootdir)
lib/data.c +3 -3
index ad7b2cb..abfa3f4 100644
@@ -92,7 +92,7 @@ int erofs_map_blocks(struct erofs_inode *inode,
 	pos = roundup(iloc(vi->nid) + vi->inode_isize +
 		      vi->xattr_isize, unit) + unit * chunknr;
 
-	err = blk_read(0, buf, erofs_blknr(pos), 1);
+	err = blk_read(sbi.dev, 0, buf, erofs_blknr(pos), 1);
 	if (err < 0)
 		return -EIO;
 
@@ -208,7 +208,7 @@ static int erofs_read_raw_data(struct erofs_inode *inode, char *buffer,
 			map.m_la = ptr;
 		}
 
-		ret = dev_read(mdev.m_deviceid, estart, mdev.m_pa,
+		ret = dev_read(sbi.dev, mdev.m_deviceid, estart, mdev.m_pa,
 			       eend - map.m_la);
 		if (ret < 0)
 			return -EIO;
@@ -283,7 +283,7 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 				break;
 			}
 		}
-		ret = dev_read(mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
+		ret = dev_read(sbi.dev, mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
 		if (ret < 0)
 			break;
 
lib/inode.c +15 -13
index 4da28b3..09cd79f 100644
@@ -151,7 +151,7 @@ static int __allocate_inode_bh_data(struct erofs_inode *inode,
 	}
 
 	/* allocate main data buffer */
-	bh = erofs_balloc(DATA, blknr_to_addr(nblocks), 0, 0);
+	bh = erofs_balloc(sbi.dev, DATA, blknr_to_addr(nblocks), 0, 0);
 	if (IS_ERR(bh))
 		return PTR_ERR(bh);
 
@@ -278,7 +278,7 @@ static int write_dirblock(unsigned int q, struct erofs_dentry *head,
 	char buf[EROFS_BLKSIZ];
 
 	fill_dirblock(buf, EROFS_BLKSIZ, q, head, end);
-	return blk_write(buf, blkaddr, 1);
+	return blk_write(sbi.dev, buf, blkaddr, 1);
 }
 
 static int erofs_write_dir_file(struct erofs_inode *dir)
@@ -340,7 +340,7 @@ static int erofs_write_file_from_buffer(struct erofs_inode *inode, char *buf)
 		return ret;
 
 	if (nblocks)
-		blk_write(buf, inode->u.i_blkaddr, nblocks);
+		blk_write(sbi.dev, buf, inode->u.i_blkaddr, nblocks);
 	inode->idata_size = inode->i_size % EROFS_BLKSIZ;
 	if (inode->idata_size) {
 		inode->idata = malloc(inode->idata_size);
@@ -382,7 +382,7 @@ static int write_uncompressed_file_from_fd(struct erofs_inode *inode, int fd)
 			return -EAGAIN;
 		}
 
-		ret = blk_write(buf, inode->u.i_blkaddr + i, 1);
+		ret = blk_write(sbi.dev, buf, inode->u.i_blkaddr + i, 1);
 		if (ret)
 			return ret;
 	}
@@ -443,6 +443,7 @@ int erofs_write_file(struct erofs_inode *inode)
 static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 {
 	struct erofs_inode *const inode = bh->fsprivate;
+	struct erofs_device *dev = sbi.dev;
 	const u16 icount = EROFS_INODE_XATTR_ICOUNT(inode->xattr_isize);
 	erofs_off_t off = erofs_btell(bh, false);
 	union {
@@ -529,7 +530,7 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 		BUG_ON(1);
 	}
 
-	ret = dev_write(&u, off, inode->inode_isize);
+	ret = dev_write(dev, &u, off, inode->inode_isize);
 	if (ret)
 		return false;
 	off += inode->inode_isize;
@@ -540,7 +541,7 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 		if (IS_ERR(xattrs))
 			return false;
 
-		ret = dev_write(xattrs, off, inode->xattr_isize);
+		ret = dev_write(dev, xattrs, off, inode->xattr_isize);
 		free(xattrs);
 		if (ret)
 			return false;
@@ -556,7 +557,7 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 		} else {
 			/* write compression metadata */
 			off = Z_EROFS_VLE_EXTENT_ALIGN(off);
-			ret = dev_write(inode->compressmeta, off,
+			ret = dev_write(dev, inode->compressmeta, off,
 					inode->extent_isize);
 			if (ret)
 				return false;
@@ -583,7 +584,7 @@ static int erofs_prepare_tail_block(struct erofs_inode *inode)
 
 	bh = inode->bh_data;
 	if (!bh) {
-		bh = erofs_balloc(DATA, EROFS_BLKSIZ, 0, 0);
+		bh = erofs_balloc(sbi.dev, DATA, EROFS_BLKSIZ, 0, 0);
 		if (IS_ERR(bh))
 			return PTR_ERR(bh);
 		bh->op = &erofs_skip_write_bhops;
@@ -631,7 +632,7 @@ static int erofs_prepare_inode_buffer(struct erofs_inode *inode)
 			inode->datalayout = EROFS_INODE_FLAT_PLAIN;
 	}
 
-	bh = erofs_balloc(INODE, inodesize, 0, inode->idata_size);
+	bh = erofs_balloc(sbi.dev, INODE, inodesize, 0, inode->idata_size);
 	if (bh == ERR_PTR(-ENOSPC)) {
 		int ret;
 
@@ -644,7 +645,7 @@ noinline:
 		ret = erofs_prepare_tail_block(inode);
 		if (ret)
 			return ret;
-		bh = erofs_balloc(INODE, inodesize, 0, 0);
+		bh = erofs_balloc(sbi.dev, INODE, inodesize, 0, 0);
 		if (IS_ERR(bh))
 			return PTR_ERR(bh);
 		DBG_BUGON(inode->bh_inline);
@@ -684,7 +685,7 @@ static bool erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
 	const erofs_off_t off = erofs_btell(bh, false);
 	int ret;
 
-	ret = dev_write(inode->idata, off, inode->idata_size);
+	ret = dev_write(sbi.dev, inode->idata, off, inode->idata_size);
 	if (ret)
 		return false;
 
@@ -703,6 +704,7 @@ static struct erofs_bhops erofs_write_inline_bhops = {
 static int erofs_write_tail_end(struct erofs_inode *inode)
 {
 	struct erofs_buffer_head *bh, *ibh;
+	struct erofs_device *dev = sbi.dev;
 
 	bh = inode->bh_data;
 
@@ -732,13 +734,13 @@ static int erofs_write_tail_end(struct erofs_inode *inode)
 			/* pad 0'ed data for the other cases */
 			zero_pos = pos + inode->idata_size;
 		}
-		ret = dev_write(inode->idata, pos, inode->idata_size);
+		ret = dev_write(dev, inode->idata, pos, inode->idata_size);
 		if (ret)
 			return ret;
 
 		DBG_BUGON(inode->idata_size > EROFS_BLKSIZ);
 		if (inode->idata_size < EROFS_BLKSIZ) {
-			ret = dev_fillzero(zero_pos,
+			ret = dev_fillzero(dev, zero_pos,
 					   EROFS_BLKSIZ - inode->idata_size,
 					   false);
 			if (ret)
lib/io.c +72 -67

305 lines not on this page · raw message

lib/namei.c +3 -2
index 7b69a59..bb14d30 100644
@@ -28,9 +28,10 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	char buf[sizeof(struct erofs_inode_extended)];
 	struct erofs_inode_compact *dic;
 	struct erofs_inode_extended *die;
+	struct erofs_device *dev = sbi.dev;
 	const erofs_off_t inode_loc = iloc(vi->nid);
 
-	ret = dev_read(0, buf, inode_loc, sizeof(*dic));
+	ret = dev_read(dev, 0, buf, inode_loc, sizeof(*dic));
 	if (ret < 0)
 		return -EIO;
 
@@ -47,7 +48,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	case EROFS_INODE_LAYOUT_EXTENDED:
 		vi->inode_isize = sizeof(struct erofs_inode_extended);
 
-		ret = dev_read(0, buf + sizeof(*dic), inode_loc + sizeof(*dic),
+		ret = dev_read(dev, 0, buf + sizeof(*dic), inode_loc + sizeof(*dic),
 			       sizeof(*die) - sizeof(*dic));
 		if (ret < 0)
 			return -EIO;
lib/super.c +2 -2
index b267412..8727161 100644
@@ -53,7 +53,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 		struct erofs_deviceslot dis;
 		int ret;
 
-		ret = dev_read(0, &dis, pos, sizeof(dis));
+		ret = dev_read(sbi->dev, 0, &dis, pos, sizeof(dis));
 		if (ret < 0) {
 			free(sbi->devs);
 			return ret;
@@ -73,7 +73,7 @@ int erofs_read_superblock(void)
 	unsigned int blkszbits;
 	int ret;
 
-	ret = blk_read(0, data, 0, 1);
+	ret = blk_read(sbi.dev, 0, data, 0, 1);
 	if (ret < 0) {
 		erofs_err("cannot read erofs superblock: %d", ret);
 		return -EIO;
lib/xattr.c +9 -8
index 8979dcc..e0bf922 100644
@@ -549,7 +549,7 @@ static void erofs_cleanxattrs(bool sharedxattrs)
 static bool erofs_bh_flush_write_shared_xattrs(struct erofs_buffer_head *bh)
 {
 	void *buf = bh->fsprivate;
-	int err = dev_write(buf, erofs_btell(bh, false), shared_xattrs_size);
+	int err = dev_write(sbi.dev, buf, erofs_btell(bh, false), shared_xattrs_size);
 
 	if (err)
 		return false;
@@ -609,7 +609,7 @@ int erofs_build_shared_xattrs_from_path(const char *path)
 	if (!buf)
 		return -ENOMEM;
 
-	bh = erofs_balloc(XATTR, shared_xattrs_size, 0, 0);
+	bh = erofs_balloc(sbi.dev, XATTR, shared_xattrs_size, 0, 0);
 	if (IS_ERR(bh)) {
 		free(buf);
 		return PTR_ERR(bh);
@@ -729,6 +729,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 	struct xattr_iter it;
 	unsigned int i;
 	struct erofs_xattr_ibody_header *ih;
+	struct erofs_device *dev = sbi.dev;
 	int ret = 0;
 
 	/* the most case is that xattrs of this inode are initialized. */
@@ -759,7 +760,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 	it.blkaddr = erofs_blknr(iloc(vi->nid) + vi->inode_isize);
 	it.ofs = erofs_blkoff(iloc(vi->nid) + vi->inode_isize);
 
-	ret = blk_read(0, it.page, it.blkaddr, 1);
+	ret = blk_read(dev, 0, it.page, it.blkaddr, 1);
 	if (ret < 0)
 		return -EIO;
 
@@ -779,7 +780,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 			/* cannot be unaligned */
 			DBG_BUGON(it.ofs != EROFS_BLKSIZ);
 
-			ret = blk_read(0, it.page, ++it.blkaddr, 1);
+			ret = blk_read(dev, 0, it.page, ++it.blkaddr, 1);
 			if (ret < 0) {
 				free(vi->xattr_shared_xattrs);
 				vi->xattr_shared_xattrs = NULL;
@@ -824,7 +825,7 @@ static inline int xattr_iter_fixup(struct xattr_iter *it)
 
 	it->blkaddr += erofs_blknr(it->ofs);
 
-	ret = blk_read(0, it->page, it->blkaddr, 1);
+	ret = blk_read(sbi.dev, 0, it->page, it->blkaddr, 1);
 	if (ret < 0)
 		return -EIO;
 
@@ -850,7 +851,7 @@ static int inline_xattr_iter_pre(struct xattr_iter *it,
 	it->blkaddr = erofs_blknr(iloc(vi->nid) + inline_xattr_ofs);
 	it->ofs = erofs_blkoff(iloc(vi->nid) + inline_xattr_ofs);
 
-	ret = blk_read(0, it->page, it->blkaddr, 1);
+	ret = blk_read(sbi.dev, 0, it->page, it->blkaddr, 1);
 	if (ret < 0)
 		return -EIO;
 
@@ -1047,7 +1048,7 @@ static int shared_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 		it->it.ofs = xattrblock_offset(vi->xattr_shared_xattrs[i]);
 
 		if (!i || blkaddr != it->it.blkaddr) {
-			ret = blk_read(0, it->it.page, blkaddr, 1);
+			ret = blk_read(sbi.dev, 0, it->it.page, blkaddr, 1);
 			if (ret < 0)
 				return -EIO;
 
@@ -1186,7 +1187,7 @@ static int shared_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 
 		it->it.ofs = xattrblock_offset(vi->xattr_shared_xattrs[i]);
 		if (!i || blkaddr != it->it.blkaddr) {
-			ret = blk_read(0, it->it.page, blkaddr, 1);
+			ret = blk_read(sbi.dev, 0, it->it.page, blkaddr, 1);
 			if (ret < 0)
 				return -EIO;
 
lib/zmap.c +2 -2
index abe0d31..6e5a5d4 100644
@@ -44,7 +44,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 		  vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
 	pos = round_up(iloc(vi->nid) + vi->inode_isize + vi->xattr_isize, 8);
 
-	ret = dev_read(0, buf, pos, sizeof(buf));
+	ret = dev_read(sbi.dev, 0, buf, pos, sizeof(buf));
 	if (ret < 0)
 		return -EIO;
 
@@ -111,7 +111,7 @@ static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
 	if (map->index == eblk)
 		return 0;
 
-	ret = blk_read(0, mpage, eblk, 1);
+	ret = blk_read(sbi.dev, 0, mpage, eblk, 1);
 	if (ret < 0)
 		return -EIO;
 
mkfs/main.c +12 -7
index b969b35..e3ad9d9 100644
@@ -508,8 +508,9 @@ static int erofs_mkfs_superblock_csum_set(void)
 	u8 buf[EROFS_BLKSIZ];
 	u32 crc;
 	struct erofs_super_block *sb;
+	struct erofs_device *dev = sbi.dev;
 
-	ret = blk_read(0, buf, 0, 1);
+	ret = blk_read(dev, 0, buf, 0, 1);
 	if (ret) {
 		erofs_err("failed to read superblock to set checksum: %s",
 			  erofs_strerror(ret));
@@ -535,7 +536,7 @@ static int erofs_mkfs_superblock_csum_set(void)
 	/* set up checksum field to erofs_super_block */
 	sb->checksum = cpu_to_le32(crc);
 
-	ret = blk_write(buf, 0, 1);
+	ret = blk_write(dev, buf, 0, 1);
 	if (ret) {
 		erofs_err("failed to write checksummed superblock: %s",
 			  erofs_strerror(ret));
@@ -607,8 +608,12 @@ int main(int argc, char **argv)
 	char uuid_str[37] = "not available";
 
 	erofs_init_configure();
-	erofs_mkfs_default_options();
 
+	sbi.dev = erofs_init_dev();
+	if (IS_ERR(sbi.dev))
+		return 1;
+
+	erofs_mkfs_default_options();
 	err = mkfs_parse_options_cfg(argc, argv);
 	erofs_show_progs(argc, argv);
 	if (err) {
@@ -647,7 +652,7 @@ int main(int argc, char **argv)
 		sbi.build_time_nsec = t.tv_usec;
 	}
 
-	err = dev_open(cfg.c_img_path);
+	err = dev_open(sbi.dev, cfg.c_img_path);
 	if (err) {
 		usage();
 		return 1;
@@ -744,10 +749,10 @@ int main(int argc, char **argv)
 		goto exit;
 
 	/* flush all remaining buffers */
-	if (!erofs_bflush(NULL))
+	if (!erofs_bflush(sbi.dev, NULL))
 		err = -EIO;
 	else
-		err = dev_resize(nblocks);
+		err = dev_resize(sbi.dev, nblocks);
 
 	if (!err && erofs_sb_has_sb_chksum())
 		err = erofs_mkfs_superblock_csum_set();
@@ -756,7 +761,7 @@ exit:
 #ifdef WITH_ANDROID
 	erofs_droid_blocklist_fclose();
 #endif
-	dev_close();
+	dev_close(sbi.dev);
 	erofs_cleanup_compress_hints();
 	erofs_cleanup_exclude_rules();
 	if (cfg.c_chunkbits)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/4 erofs-utils: remove global sbi

Remove global sbi which will make erofs-utils more library friendly.

---
 dump/main.c                | 71 ++++++++++++++++-------------
 fsck/main.c                | 55 +++++++++++++----------
 fuse/main.c                | 42 +++++++++++-------
 include/erofs/blobchunk.h  |  4 +-
 include/erofs/cache.h      |  2 +-
 include/erofs/compress.h   |  3 +-
 include/erofs/config.h     |  5 ++-
 include/erofs/decompress.h |  1 +
 include/erofs/dir.h        |  4 +-
 include/erofs/inode.h      |  3 +-
 include/erofs/internal.h   | 25 +++++------
 include/erofs/xattr.h      |  8 ++--
 lib/blobchunk.c            | 17 +++----
 lib/cache.c                |  4 +-
 lib/compress.c             | 71 +++++++++++++++--------------
 lib/compressor.h           |  1 +
 lib/compressor_lz4.c       |  2 +-
 lib/compressor_lz4hc.c     |  2 +-
 lib/config.c               | 17 +++++--
 lib/data.c                 | 21 +++++----
 lib/decompress.c           |  2 +-
 lib/dir.c                  | 17 ++++---
 lib/inode.c                | 68 ++++++++++++++++------------
 lib/io.c                   |  2 +
 lib/namei.c                | 19 +++++---
 lib/super.c                | 38 ++++++++--------
 lib/xattr.c                | 40 ++++++++++-------
 lib/zmap.c                 | 26 ++++++-----
 mkfs/main.c                | 91 ++++++++++++++++++++------------------
 29 files changed, 382 insertions(+), 279 deletions(-)
dump/main.c +41 -30
index 07fa151..586302b 100644
@@ -152,10 +152,10 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 			usage();
 			exit(0);
 		case 3:
-			err = blob_open_ro(sbi.dev, optarg);
+			err = blob_open_ro(cfg.sbi->dev, optarg);
 			if (err)
 				return err;
-			++sbi.extra_devices;
+			++cfg.sbi->extra_devices;
 			break;
 		case 4:
 			dumpcfg.inode_path = optarg;
@@ -281,7 +281,10 @@ static int erofsdump_readdir(struct erofs_dir_context *ctx)
 {
 	int err;
 	erofs_off_t occupied_size = 0;
-	struct erofs_inode vi = { .nid = ctx->de_nid };
+	struct erofs_inode vi = {
+		.sbi = ctx->sbi,
+		.nid = ctx->de_nid,
+	};
 
 	err = erofs_read_inode_from_disk(&vi);
 	if (err) {
@@ -307,6 +310,7 @@ static int erofsdump_readdir(struct erofs_dir_context *ctx)
 	/* XXXX: the dir depth should be restricted in order to avoid loops */
 	if (S_ISDIR(vi.i_mode)) {
 		struct erofs_dir_context nctx = {
+			.sbi = ctx->sbi,
 			.flags = ctx->dir ? EROFS_READDIR_VALID_PNID : 0,
 			.pnid = ctx->dir ? ctx->dir->nid : 0,
 			.dir = &vi,
@@ -326,7 +330,7 @@ static int erofsdump_map_blocks(struct erofs_inode *inode,
 	return erofs_map_blocks(inode, map, flags);
 }
 
-static void erofsdump_show_fileinfo(bool show_extent)
+static void erofsdump_show_fileinfo(struct erofs_sb_info *sbi, bool show_extent)
 {
 	const char *ext_fmt[] = {
 		"%4d: %8" PRIu64 "..%8" PRIu64 " | %7" PRIu64 " : %10" PRIu64 "..%10" PRIu64 " | %7" PRIu64 "\n",
@@ -335,11 +339,14 @@ static void erofsdump_show_fileinfo(bool show_extent)
 	int err, i;
 	erofs_off_t size;
 	u16 access_mode;
-	struct erofs_inode inode = { .nid = dumpcfg.nid };
 	char path[PATH_MAX];
 	char access_mode_str[] = "rwxrwxrwx";
 	char timebuf[128] = {0};
 	unsigned int extent_count = 0;
+	struct erofs_inode inode = {
+		.sbi = sbi,
+		.nid = dumpcfg.nid,
+	};
 	struct erofs_map_blocks map = {
 		.index = UINT_MAX,
 		.m_la = 0,
@@ -366,7 +373,7 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(inode.nid, path, sizeof(path));
+	err = erofs_get_pathname(sbi, inode.nid, path, sizeof(path));
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
@@ -396,6 +403,7 @@ static void erofsdump_show_fileinfo(bool show_extent)
 
 	if (dumpcfg.show_subdirectories) {
 		struct erofs_dir_context ctx = {
+			.sbi = sbi,
 			.flags = EROFS_READDIR_VALID_PNID,
 			.pnid = inode.nid,
 			.dir = &inode,
@@ -431,7 +439,7 @@ static void erofsdump_show_fileinfo(bool show_extent)
 			.m_deviceid = map.m_deviceid,
 			.m_pa = map.m_pa,
 		};
-		err = erofs_map_dev(&sbi, &mdev);
+		err = erofs_map_dev(sbi, &mdev);
 		if (err) {
 			erofs_err("failed to map device");
 			return;
@@ -531,15 +539,16 @@ static void erofsdump_file_statistic(void)
 			stats.compress_rate);
 }
 
-static void erofsdump_print_statistic(void)
+static void erofsdump_print_statistic(struct erofs_sb_info *sbi)
 {
 	int err;
 	struct erofs_dir_context ctx = {
+		.sbi = sbi,
 		.flags = 0,
 		.pnid = 0,
 		.dir = NULL,
 		.cb = erofsdump_dirent_iter,
-		.de_nid = sbi.root_nid,
+		.de_nid = sbi->root_nid,
 		.dname = "",
 		.de_namelen = 0,
 	};
@@ -559,36 +568,36 @@ static void erofsdump_print_statistic(void)
 	erofsdump_filetype_distribution(file_types, OTHERFILETYPE);
 }
 
-static void erofsdump_show_superblock(void)
+static void erofsdump_show_superblock(struct erofs_sb_info *sbi)
 {
-	time_t time = sbi.build_time;
+	time_t time = sbi->build_time;
 	char uuid_str[37] = "not available";
 	int i = 0;
 
 	fprintf(stdout, "Filesystem magic number:                      0x%04X\n",
 			EROFS_SUPER_MAGIC_V1);
 	fprintf(stdout, "Filesystem blocks:                            %llu\n",
-			sbi.total_blocks | 0ULL);
+			sbi->total_blocks | 0ULL);
 	fprintf(stdout, "Filesystem inode metadata start block:        %u\n",
-			sbi.meta_blkaddr);
+			sbi->meta_blkaddr);
 	fprintf(stdout, "Filesystem shared xattr metadata start block: %u\n",
-			sbi.xattr_blkaddr);
+			sbi->xattr_blkaddr);
 	fprintf(stdout, "Filesystem root nid:                          %llu\n",
-			sbi.root_nid | 0ULL);
+			sbi->root_nid | 0ULL);
 	fprintf(stdout, "Filesystem inode count:                       %llu\n",
-			sbi.inos | 0ULL);
+			sbi->inos | 0ULL);
 	fprintf(stdout, "Filesystem created:                           %s",
 			ctime(&time));
 	fprintf(stdout, "Filesystem features:                          ");
 	for (; i < ARRAY_SIZE(feature_lists); i++) {
 		u32 feat = le32_to_cpu(feature_lists[i].compat ?
-				       sbi.feature_compat :
-				       sbi.feature_incompat);
+				       sbi->feature_compat :
+				       sbi->feature_incompat);
 		if (feat & feature_lists[i].flag)
 			fprintf(stdout, "%s ", feature_lists[i].name);
 	}
 #ifdef HAVE_LIBUUID
-	uuid_unparse_lower(sbi.uuid, uuid_str);
+	uuid_unparse_lower(sbi->uuid, uuid_str);
 #endif
 	fprintf(stdout, "\nFilesystem UUID:                              %s\n",
 			uuid_str);
@@ -597,14 +606,16 @@ static void erofsdump_show_superblock(void)
 int main(int argc, char **argv)
 {
 	int err;
+	struct erofs_sb_info *sbi;
 
 	erofs_init_configure();
 
-	sbi.dev = erofs_init_dev();
-	if (IS_ERR(sbi.dev)) {
-		err = PTR_ERR(sbi.dev);
+	sbi = erofs_init_sbi();
+	if (IS_ERR(sbi)) {
+		err = PTR_ERR(sbi);
 		goto exit;
 	}
+	cfg.sbi = sbi;
 
 	err = erofsdump_parse_options_cfg(argc, argv);
 	if (err) {
@@ -613,13 +624,13 @@ int main(int argc, char **argv)
 		goto exit_blob_close;
 	}
 
-	err = dev_open_ro(sbi.dev, cfg.c_img_path);
+	err = dev_open_ro(sbi->dev, cfg.c_img_path);
 	if (err) {
 		erofs_err("failed to open image file");
 		goto exit_blob_close;
 	}
 
-	err = erofs_read_superblock();
+	err = erofs_read_superblock(sbi);
 	if (err) {
 		erofs_err("failed to read superblock");
 		goto exit_dev_close;
@@ -630,10 +641,10 @@ int main(int argc, char **argv)
 		dumpcfg.totalshow = 1;
 	}
 	if (dumpcfg.show_superblock)
-		erofsdump_show_superblock();
+		erofsdump_show_superblock(sbi);
 
 	if (dumpcfg.show_statistics)
-		erofsdump_print_statistic();
+		erofsdump_print_statistic(sbi);
 
 	if (dumpcfg.show_extent && !dumpcfg.show_inode) {
 		usage();
@@ -641,14 +652,14 @@ int main(int argc, char **argv)
 	}
 
 	if (dumpcfg.show_inode)
-		erofsdump_show_fileinfo(dumpcfg.show_extent);
+		erofsdump_show_fileinfo(sbi, dumpcfg.show_extent);
 
 exit_put_super:
-	erofs_put_super();
+	erofs_put_super(sbi);
 exit_dev_close:
-	dev_close(sbi.dev);
+	dev_close(sbi->dev);
 exit_blob_close:
-	blob_closeall(sbi.dev);
+	blob_closeall(sbi->dev);
 exit:
 	erofs_exit_configure();
 	return err;
fsck/main.c +32 -23

187 lines not on this page · raw message

fuse/main.c +25 -17

117 lines not on this page · raw message

include/erofs/blobchunk.h +2 -2
index 49cb7bf..82426b7 100644
@@ -16,10 +16,10 @@ extern "C"
 
 int erofs_blob_write_chunk_indexes(struct erofs_inode *inode, erofs_off_t off);
 int erofs_blob_write_chunked_file(struct erofs_inode *inode);
-int erofs_blob_remap(void);
+int erofs_blob_remap(struct erofs_sb_info *sbi);
 void erofs_blob_exit(void);
 int erofs_blob_init(const char *blobfile_path);
-int erofs_generate_devtable(void);
+int erofs_generate_devtable(struct erofs_sb_info *sbi);
 
 #ifdef __cplusplus
 }
include/erofs/cache.h +1 -1
index e7aec2b..c11d676 100644
@@ -96,7 +96,7 @@ static inline bool erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
 	return true;
 }
 
-struct erofs_buffer_head *erofs_buffer_init(void);
+struct erofs_buffer_head *erofs_buffer_init(struct erofs_sb_info *sbi);
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
 
 struct erofs_buffer_head *erofs_balloc(struct erofs_device *dev,
include/erofs/compress.h +2 -1
index 24f6204..b2c1fda 100644
@@ -20,7 +20,8 @@ extern "C"
 void z_erofs_drop_inline_pcluster(struct erofs_inode *inode);
 int erofs_write_compressed_file(struct erofs_inode *inode);
 
-int z_erofs_compress_init(struct erofs_buffer_head *bh);
+int z_erofs_compress_init(struct erofs_sb_info *sbi,
+			  struct erofs_buffer_head *bh);
 int z_erofs_compress_exit(void);
 
 const char *z_erofs_list_available_compressors(unsigned int i);
include/erofs/config.h +4 -1
index 6c96664..1129989 100644
@@ -14,6 +14,7 @@ extern "C"
 
 #include "defs.h"
 #include "err.h"
+#include "internal.h"
 
 
 enum {
@@ -77,6 +78,8 @@ struct erofs_configure {
 
 	/* offset when reading multi partition images */
 	u64 c_offset;
+
+	struct erofs_sb_info *sbi;
 };
 
 extern struct erofs_configure cfg;
@@ -85,7 +88,7 @@ void erofs_init_configure(void);
 void erofs_show_config(void);
 void erofs_exit_configure(void);
 
-struct erofs_device *erofs_init_dev(void);
+struct erofs_sb_info *erofs_init_sbi(void);
 
 void erofs_set_fs_root(const char *rootdir);
 const char *erofs_fspath(const char *fullpath);
include/erofs/decompress.h +1 -0
index 82bf7b8..dd7eee2 100644
@@ -14,6 +14,7 @@ extern "C"
 #include "internal.h"
 
 struct z_erofs_decompress_req {
+	struct erofs_sb_info *sbi;
 	char *in, *out;
 
 	/*
include/erofs/dir.h +3 -1
index 74bffb5..cd257c4 100644
@@ -39,6 +39,7 @@ typedef int (*erofs_readdir_cb)(struct erofs_dir_context *);
  * the callback context. |de_namelen| is the exact dirent name length.
  */
 struct erofs_dir_context {
+	struct erofs_sb_info *sbi;
 	/*
 	 * During execution of |erofs_iterate_dir|, the function needs to
 	 * read the values inside |erofs_inode* dir|. So it is important
@@ -62,7 +63,8 @@ struct erofs_dir_context {
 /* Iterate over inodes that are in directory */
 int erofs_iterate_dir(struct erofs_dir_context *ctx, bool fsck);
 /* Get a full pathname of the inode NID */
-int erofs_get_pathname(erofs_nid_t nid, char *buf, size_t size);
+int erofs_get_pathname(struct erofs_sb_info *sbi, erofs_nid_t nid,
+		       char *buf, size_t size);
 
 #ifdef __cplusplus
 }
include/erofs/inode.h +2 -1
index 79b8d89..f76c675 100644
@@ -20,7 +20,8 @@ unsigned char erofs_ftype_to_dtype(unsigned int filetype);
 void erofs_inode_manager_init(void);
 unsigned int erofs_iput(struct erofs_inode *inode);
 erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);
-struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_inode *parent,
+struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi,
+						    struct erofs_inode *parent,
 						    const char *path);
 
 #ifdef __cplusplus
include/erofs/internal.h +12 -13
index c4b36e9..98cfed1 100644
@@ -112,26 +112,23 @@ struct erofs_sb_info {
 	};
 };
 
-/* global sbi */
-extern struct erofs_sb_info sbi;
-
-static inline erofs_off_t iloc(erofs_nid_t nid)
+static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
 {
-	return blknr_to_addr(sbi.meta_blkaddr) + (nid << sbi.islotbits);
+	return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
 }
 
 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
-static inline bool erofs_sb_has_##name(void) \
+static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
 { \
-	return sbi.feature_##compat & EROFS_FEATURE_##feature; \
+	return sbi->feature_##compat & EROFS_FEATURE_##feature; \
 } \
-static inline void erofs_sb_set_##name(void) \
+static inline void erofs_sb_set_##name(struct erofs_sb_info *sbi) \
 { \
-	sbi.feature_##compat |= EROFS_FEATURE_##feature; \
+	sbi->feature_##compat |= EROFS_FEATURE_##feature; \
 } \
-static inline void erofs_sb_clear_##name(void) \
+static inline void erofs_sb_clear_##name(struct erofs_sb_info *sbi) \
 { \
-	sbi.feature_##compat &= ~EROFS_FEATURE_##feature; \
+	sbi->feature_##compat &= ~EROFS_FEATURE_##feature; \
 }
 
 EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
@@ -156,6 +153,7 @@ struct erofs_inode {
 	};
 	unsigned int i_count;
 	struct erofs_inode *i_parent;
+	struct erofs_sb_info *sbi;
 
 	umode_t i_mode;
 	erofs_off_t i_size;
@@ -246,6 +244,7 @@ static inline unsigned int erofs_inode_datalayout(unsigned int value)
 
 struct erofs_dentry {
 	struct list_head d_child;	/* child of parent list */
+	struct erofs_sb_info *sbi;
 
 	unsigned int type;
 	char name[EROFS_NAME_LEN];
@@ -328,8 +327,8 @@ struct erofs_map_dev {
 };
 
 /* super.c */
-int erofs_read_superblock(void);
-void erofs_put_super(void);
+int erofs_read_superblock(struct erofs_sb_info *sbi);
+void erofs_put_super(struct erofs_sb_info *sbi);
 
 /* namei.c */
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
include/erofs/xattr.h +5 -3
index a0528c0..0b63439 100644
@@ -24,9 +24,10 @@ static inline unsigned int inlinexattr_header_size(struct erofs_inode *vi)
 		sizeof(u32) * vi->xattr_shared_count;
 }
 
-static inline erofs_blk_t xattrblock_addr(unsigned int xattr_id)
+static inline erofs_blk_t xattrblock_addr(struct erofs_sb_info *sbi,
+					  unsigned int xattr_id)
 {
-	return sbi.xattr_blkaddr +
+	return sbi->xattr_blkaddr +
 		xattr_id * sizeof(__u32) / EROFS_BLKSIZ;
 }
 
@@ -68,7 +69,8 @@ static inline unsigned int xattrblock_offset(unsigned int xattr_id)
 
 int erofs_prepare_xattr_ibody(struct erofs_inode *inode);
 char *erofs_export_xattr_ibody(struct list_head *ixattrs, unsigned int size);
-int erofs_build_shared_xattrs_from_path(const char *path);
+int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi,
+					const char *path);
 
 #ifdef __cplusplus
 }
lib/blobchunk.c +9 -8
index d11d7db..b91a0cf 100644
@@ -104,6 +104,7 @@ static int erofs_blob_hashmap_cmp(const void *a, const void *b,
 int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 				   erofs_off_t off)
 {
+	struct erofs_device *dev = inode->sbi->dev;
 	struct erofs_inode_chunk_index idx = {0};
 	erofs_blk_t extent_start = EROFS_NULL_ADDR;
 	erofs_blk_t extent_end, extents_blks;
@@ -158,7 +159,7 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 	erofs_droid_blocklist_write_extent(inode, extent_start, extents_blks,
 					   first_extent, true);
 
-	return dev_write(sbi.dev, inode->chunkindexes, off, inode->extent_isize);
+	return dev_write(dev, inode->chunkindexes, off, inode->extent_isize);
 }
 
 int erofs_blob_write_chunked_file(struct erofs_inode *inode)
@@ -212,9 +213,9 @@ err:
 	return ret;
 }
 
-int erofs_blob_remap(void)
+int erofs_blob_remap(struct erofs_sb_info *sbi)
 {
-	struct erofs_device *dev = sbi.dev;
+	struct erofs_device *dev = sbi->dev;
 	struct erofs_buffer_head *bh;
 	ssize_t length;
 	erofs_off_t pos_in, pos_out;
@@ -281,22 +282,22 @@ int erofs_blob_init(const char *blobfile_path)
 	return 0;
 }
 
-int erofs_generate_devtable(void)
+int erofs_generate_devtable(struct erofs_sb_info *sbi)
 {
 	struct erofs_deviceslot dis;
 
 	if (!multidev)
 		return 0;
 
-	bh_devt = erofs_balloc(sbi.dev, DEVT, sizeof(dis), 0, 0);
+	bh_devt = erofs_balloc(sbi->dev, DEVT, sizeof(dis), 0, 0);
 	if (IS_ERR(bh_devt))
 		return PTR_ERR(bh_devt);
 
 	dis = (struct erofs_deviceslot) {};
 	erofs_mapbh(bh_devt->block);
 	bh_devt->op = &erofs_skip_write_bhops;
-	sbi.devt_slotoff = erofs_btell(bh_devt, false) / EROFS_DEVT_SLOT_SIZE;
-	sbi.extra_devices = 1;
-	erofs_sb_set_device_table();
+	sbi->devt_slotoff = erofs_btell(bh_devt, false) / EROFS_DEVT_SLOT_SIZE;
+	sbi->extra_devices = 1;
+	erofs_sb_set_device_table(sbi);
 	return 0;
 }
lib/cache.c +2 -2
index 60b0ee6..478ac17 100644
@@ -63,10 +63,10 @@ const struct erofs_bhops erofs_buf_write_bhops = {
 };
 
 /* return buffer_head of erofs super block (with size 0) */
-struct erofs_buffer_head *erofs_buffer_init(void)
+struct erofs_buffer_head *erofs_buffer_init(struct erofs_sb_info *sbi)
 {
 	int i, j;
-	struct erofs_buffer_head *bh = erofs_balloc(sbi.dev, META, 0, 0, 0);
+	struct erofs_buffer_head *bh = erofs_balloc(sbi->dev, META, 0, 0, 0);
 
 	if (IS_ERR(bh))
 		return bh;
lib/compress.c +39 -32

226 lines not on this page · raw message

lib/compressor.h +1 -0
index cf063f1..6b2401a 100644
@@ -28,6 +28,7 @@ struct erofs_compressor {
 
 struct erofs_compress {
 	const struct erofs_compressor *alg;
+	struct erofs_sb_info *sbi;
 
 	unsigned int compress_threshold;
 	unsigned int compression_level;
lib/compressor_lz4.c +1 -1
index b6f6e7e..e507b70 100644
@@ -33,7 +33,7 @@ static int compressor_lz4_exit(struct erofs_compress *c)
 static int compressor_lz4_init(struct erofs_compress *c)
 {
 	c->alg = &erofs_compressor_lz4;
-	sbi.lz4_max_distance = LZ4_DISTANCE_MAX;
+	c->sbi->lz4_max_distance = LZ4_DISTANCE_MAX;
 	return 0;
 }
 
lib/compressor_lz4hc.c +1 -1
index eec1c84..f2120d8 100644
@@ -44,7 +44,7 @@ static int compressor_lz4hc_init(struct erofs_compress *c)
 	if (!c->private_data)
 		return -ENOMEM;
 
-	sbi.lz4_max_distance = LZ4_DISTANCE_MAX;
+	c->sbi->lz4_max_distance = LZ4_DISTANCE_MAX;
 	return 0;
 }
 
lib/config.c +13 -4
index 3cc1d22..53a54e8 100644
@@ -15,7 +15,6 @@
 #endif
 
 struct erofs_configure cfg;
-struct erofs_sb_info sbi;
 
 void erofs_init_configure(void)
 {
@@ -55,18 +54,28 @@ void erofs_exit_configure(void)
 #endif
 	if (cfg.c_img_path)
 		free(cfg.c_img_path);
+	if (cfg.sbi)
+		free(cfg.sbi);
 }
 
-struct erofs_device *erofs_init_dev(void)
+struct erofs_sb_info *erofs_init_sbi(void)
 {
 	struct erofs_device *dev;
+	struct erofs_sb_info *sbi;
+
 	dev = calloc(1, sizeof(struct erofs_device));
 	if (!dev)
 		return ERR_PTR(-ENOMEM);
-
 	dev->fd = -1;
 
-	return dev;
+	sbi = calloc(1, sizeof(struct erofs_sb_info));
+	if (!dev) {
+		free(dev);
+		return ERR_PTR(-ENOMEM);
+	}
+	sbi->dev = dev;
+
+	return sbi;
 }
 
 static unsigned int fullpath_prefix;	/* root directory prefix length */
lib/data.c +13 -8
index abfa3f4..b87a505 100644
@@ -33,7 +33,7 @@ static int erofs_map_blocks_flatmode(struct erofs_inode *inode,
 		map->m_plen = blknr_to_addr(lastblk) - offset;
 	} else if (tailendpacking) {
 		/* 2 - inode inline B: inode, [xattrs], inline last blk... */
-		map->m_pa = iloc(vi->nid) + vi->inode_isize +
+		map->m_pa = iloc(vi->sbi, vi->nid) + vi->inode_isize +
 			vi->xattr_isize + erofs_blkoff(map->m_la);
 		map->m_plen = inode->i_size - offset;
 
@@ -65,6 +65,8 @@ int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
 	struct erofs_inode *vi = inode;
+	struct erofs_sb_info *sbi = vi->sbi;
+	struct erofs_device *dev = vi->sbi->dev;
 	struct erofs_inode_chunk_index *idx;
 	u8 buf[EROFS_BLKSIZ];
 	u64 chunknr;
@@ -89,10 +91,10 @@ int erofs_map_blocks(struct erofs_inode *inode,
 		unit = EROFS_BLOCK_MAP_ENTRY_SIZE;	/* block map */
 
 	chunknr = map->m_la >> vi->u.chunkbits;
-	pos = roundup(iloc(vi->nid) + vi->inode_isize +
+	pos = roundup(iloc(sbi, vi->nid) + vi->inode_isize +
 		      vi->xattr_isize, unit) + unit * chunknr;
 
-	err = blk_read(sbi.dev, 0, buf, erofs_blknr(pos), 1);
+	err = blk_read(dev, 0, buf, erofs_blknr(pos), 1);
 	if (err < 0)
 		return -EIO;
 
@@ -120,7 +122,7 @@ int erofs_map_blocks(struct erofs_inode *inode,
 		break;
 	default:
 		map->m_deviceid = le16_to_cpu(idx->device_id) &
-			sbi.device_id_mask;
+			sbi->device_id_mask;
 		map->m_pa = blknr_to_addr(le32_to_cpu(idx->blkaddr));
 		map->m_flags = EROFS_MAP_MAPPED;
 		break;
@@ -161,6 +163,7 @@ int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map)
 static int erofs_read_raw_data(struct erofs_inode *inode, char *buffer,
 			       erofs_off_t size, erofs_off_t offset)
 {
+	struct erofs_sb_info *sbi = inode->sbi;
 	struct erofs_map_blocks map = {
 		.index = UINT_MAX,
 	};
@@ -183,7 +186,7 @@ static int erofs_read_raw_data(struct erofs_inode *inode, char *buffer,
 			.m_deviceid = map.m_deviceid,
 			.m_pa = map.m_pa,
 		};
-		ret = erofs_map_dev(&sbi, &mdev);
+		ret = erofs_map_dev(sbi, &mdev);
 		if (ret)
 			return ret;
 
@@ -208,7 +211,7 @@ static int erofs_read_raw_data(struct erofs_inode *inode, char *buffer,
 			map.m_la = ptr;
 		}
 
-		ret = dev_read(sbi.dev, mdev.m_deviceid, estart, mdev.m_pa,
+		ret = dev_read(sbi->dev, mdev.m_deviceid, estart, mdev.m_pa,
 			       eend - map.m_la);
 		if (ret < 0)
 			return -EIO;
@@ -225,6 +228,7 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 		.index = UINT_MAX,
 	};
 	struct erofs_map_dev mdev;
+	struct erofs_sb_info *sbi = inode->sbi;
 	bool partial;
 	unsigned int bufsize = 0;
 	char *raw = NULL;
@@ -242,7 +246,7 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 		mdev = (struct erofs_map_dev) {
 			.m_pa = map.m_pa,
 		};
-		ret = erofs_map_dev(&sbi, &mdev);
+		ret = erofs_map_dev(sbi, &mdev);
 		if (ret) {
 			DBG_BUGON(1);
 			break;
@@ -283,11 +287,12 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 				break;
 			}
 		}
-		ret = dev_read(sbi.dev, mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
+		ret = dev_read(sbi->dev, mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
 		if (ret < 0)
 			break;
 
 		ret = z_erofs_decompress(&(struct z_erofs_decompress_req) {
+					.sbi = sbi,
 					.in = raw,
 					.out = buffer + end - offset,
 					.decodedskip = skip,
lib/decompress.c +1 -1
index 1661f91..fc4e049 100644
@@ -82,7 +82,7 @@ static int z_erofs_decompress_lz4(struct z_erofs_decompress_req *rq)
 	bool support_0padding = false;
 	unsigned int inputmargin = 0;
 
-	if (erofs_sb_has_lz4_0padding()) {
+	if (erofs_sb_has_lz4_0padding(rq->sbi)) {
 		support_0padding = true;
 
 		while (!src[inputmargin & ~PAGE_MASK])
lib/dir.c +12 -5
index e6b9283..073d704 100644
@@ -76,8 +76,8 @@ static int traverse_dirents(struct erofs_dir_context *ctx,
 					goto out;
 				}
 				ctx->flags |= EROFS_READDIR_DOTDOT_FOUND;
-				if (sbi.root_nid == ctx->dir->nid) {
-					ctx->pnid = sbi.root_nid;
+				if (ctx->sbi->root_nid == ctx->dir->nid) {
+					ctx->pnid = ctx->sbi->root_nid;
 					ctx->flags |= EROFS_READDIR_VALID_PNID;
 				}
 				if (fsck &&
@@ -203,7 +203,9 @@ static int erofs_get_pathname_iter(struct erofs_dir_context *ctx)
 	}
 
 	if (ctx->de_ftype == EROFS_FT_DIR || ctx->de_ftype == EROFS_FT_UNKNOWN) {
-		struct erofs_inode dir = { .nid = ctx->de_nid };
+		struct erofs_inode dir = {
+			.sbi = ctx->sbi,
+			.nid = ctx->de_nid, };
 
 		ret = erofs_read_inode_from_disk(&dir);
 		if (ret) {
@@ -229,11 +231,16 @@ static int erofs_get_pathname_iter(struct erofs_dir_context *ctx)
 	return 0;
 }
 
-int erofs_get_pathname(erofs_nid_t nid, char *buf, size_t size)
+int erofs_get_pathname(struct erofs_sb_info *sbi, erofs_nid_t nid,
+		       char *buf, size_t size)
 {
 	int ret;
-	struct erofs_inode root = { .nid = sbi.root_nid };
+	struct erofs_inode root = {
+		.sbi = sbi,
+		.nid = sbi->root_nid,
+	};
 	struct erofs_get_pathname_context pathctx = {
+		.ctx.sbi = sbi,
 		.ctx.flags = 0,
 		.ctx.dir = &root,
 		.ctx.cb = erofs_get_pathname_iter,
lib/inode.c +40 -28

239 lines not on this page · raw message

lib/io.c +2 -0
index 2b9811e..d4d127e 100644
@@ -10,6 +10,7 @@
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include "erofs/io.h"
@@ -52,6 +53,7 @@ void dev_close(struct erofs_device *dev)
 	dev->name = NULL;
 	dev->fd   = -1;
 	dev->size   = 0;
+	free(dev);
 }
 
 int dev_open(struct erofs_device *dev, const char *devname)
lib/namei.c +12 -7
index bb14d30..8612b81 100644
@@ -28,8 +28,9 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	char buf[sizeof(struct erofs_inode_extended)];
 	struct erofs_inode_compact *dic;
 	struct erofs_inode_extended *die;
-	struct erofs_device *dev = sbi.dev;
-	const erofs_off_t inode_loc = iloc(vi->nid);
+	struct erofs_sb_info *sbi = vi->sbi;
+	struct erofs_device *dev = vi->sbi->dev;
+	const erofs_off_t inode_loc = iloc(sbi, vi->nid);
 
 	ret = dev_read(dev, 0, buf, inode_loc, sizeof(*dic));
 	if (ret < 0)
@@ -115,8 +116,8 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		vi->i_gid = le16_to_cpu(dic->i_gid);
 		vi->i_nlink = le16_to_cpu(dic->i_nlink);
 
-		vi->i_mtime = sbi.build_time;
-		vi->i_mtime_nsec = sbi.build_time_nsec;
+		vi->i_mtime = sbi->build_time;
+		vi->i_mtime_nsec = sbi->build_time_nsec;
 
 		vi->i_size = le32_to_cpu(dic->i_size);
 		if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
@@ -183,6 +184,7 @@ struct erofs_dirent *find_target_dirent(erofs_nid_t pnid,
 }
 
 struct nameidata {
+	struct erofs_sb_info *sbi;
 	erofs_nid_t	nid;
 	unsigned int	ftype;
 };
@@ -193,7 +195,10 @@ int erofs_namei(struct nameidata *nd,
 	erofs_nid_t nid = nd->nid;
 	int ret;
 	char buf[EROFS_BLKSIZ];
-	struct erofs_inode vi = { .nid = nid };
+	struct erofs_inode vi = {
+		.sbi = nd->sbi,
+		.nid = nid,
+	};
 	erofs_off_t offset;
 
 	ret = erofs_read_inode_from_disk(&vi);
@@ -235,7 +240,7 @@ int erofs_namei(struct nameidata *nd,
 
 static int link_path_walk(const char *name, struct nameidata *nd)
 {
-	nd->nid = sbi.root_nid;
+	nd->nid = nd->sbi->root_nid;
 
 	while (*name == '/')
 		name++;
@@ -265,7 +270,7 @@ static int link_path_walk(const char *name, struct nameidata *nd)
 int erofs_ilookup(const char *path, struct erofs_inode *vi)
 {
 	int ret;
-	struct nameidata nd;
+	struct nameidata nd = { .sbi = vi->sbi };
 
 	ret = link_path_walk(path, &nd);
 	if (ret)
lib/super.c +19 -19
index 8727161..fcd27b6 100644
@@ -31,7 +31,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 
 	sbi->total_blocks = sbi->primarydevice_blocks;
 
-	if (!erofs_sb_has_device_table())
+	if (!erofs_sb_has_device_table(sbi))
 		ondisk_extradevs = 0;
 	else
 		ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
@@ -66,14 +66,14 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 	return 0;
 }
 
-int erofs_read_superblock(void)
+int erofs_read_superblock(struct erofs_sb_info *sbi)
 {
 	char data[EROFS_BLKSIZ];
 	struct erofs_super_block *dsb;
 	unsigned int blkszbits;
 	int ret;
 
-	ret = blk_read(sbi.dev, 0, data, 0, 1);
+	ret = blk_read(sbi->dev, 0, data, 0, 1);
 	if (ret < 0) {
 		erofs_err("cannot read erofs superblock: %d", ret);
 		return -EIO;
@@ -86,7 +86,7 @@ int erofs_read_superblock(void)
 		return ret;
 	}
 
-	sbi.feature_compat = le32_to_cpu(dsb->feature_compat);
+	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
 
 	blkszbits = dsb->blkszbits;
 	/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
@@ -96,26 +96,26 @@ int erofs_read_superblock(void)
 		return ret;
 	}
 
-	if (!check_layout_compatibility(&sbi, dsb))
+	if (!check_layout_compatibility(sbi, dsb))
 		return ret;
 
-	sbi.primarydevice_blocks = le32_to_cpu(dsb->blocks);
-	sbi.meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
-	sbi.xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
-	sbi.islotbits = EROFS_ISLOTBITS;
-	sbi.root_nid = le16_to_cpu(dsb->root_nid);
-	sbi.inos = le64_to_cpu(dsb->inos);
-	sbi.checksum = le32_to_cpu(dsb->checksum);
+	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
+	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
+	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
+	sbi->islotbits = EROFS_ISLOTBITS;
+	sbi->root_nid = le16_to_cpu(dsb->root_nid);
+	sbi->inos = le64_to_cpu(dsb->inos);
+	sbi->checksum = le32_to_cpu(dsb->checksum);
 
-	sbi.build_time = le64_to_cpu(dsb->build_time);
-	sbi.build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
+	sbi->build_time = le64_to_cpu(dsb->build_time);
+	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
 
-	memcpy(&sbi.uuid, dsb->uuid, sizeof(dsb->uuid));
-	return erofs_init_devices(&sbi, dsb);
+	memcpy(sbi->uuid, dsb->uuid, sizeof(dsb->uuid));
+	return erofs_init_devices(sbi, dsb);
 }
 
-void erofs_put_super(void)
+void erofs_put_super(struct erofs_sb_info *sbi)
 {
-	if (sbi.devs)
-		free(sbi.devs);
+	if (sbi->devs)
+		free(sbi->devs);
 }
lib/xattr.c +25 -15
index e0bf922..2496004 100644
@@ -549,7 +549,8 @@ static void erofs_cleanxattrs(bool sharedxattrs)
 static bool erofs_bh_flush_write_shared_xattrs(struct erofs_buffer_head *bh)
 {
 	void *buf = bh->fsprivate;
-	int err = dev_write(sbi.dev, buf, erofs_btell(bh, false), shared_xattrs_size);
+	int err = dev_write(bh->block->dev, buf, erofs_btell(bh, false),
+			    shared_xattrs_size);
 
 	if (err)
 		return false;
@@ -579,7 +580,8 @@ static int comp_xattr_item(const void *a, const void *b)
 	return la > lb;
 }
 
-int erofs_build_shared_xattrs_from_path(const char *path)
+int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi,
+					const char *path)
 {
 	int ret;
 	struct erofs_buffer_head *bh;
@@ -609,7 +611,7 @@ int erofs_build_shared_xattrs_from_path(const char *path)
 	if (!buf)
 		return -ENOMEM;
 
-	bh = erofs_balloc(sbi.dev, XATTR, shared_xattrs_size, 0, 0);
+	bh = erofs_balloc(sbi->dev, XATTR, shared_xattrs_size, 0, 0);
 	if (IS_ERR(bh)) {
 		free(buf);
 		return PTR_ERR(bh);
@@ -619,7 +621,7 @@ int erofs_build_shared_xattrs_from_path(const char *path)
 	erofs_mapbh(bh->block);
 	off = erofs_btell(bh, false);
 
-	sbi.xattr_blkaddr = off / EROFS_BLKSIZ;
+	sbi->xattr_blkaddr = off / EROFS_BLKSIZ;
 	off %= EROFS_BLKSIZ;
 	p = 0;
 
@@ -716,6 +718,7 @@ char *erofs_export_xattr_ibody(struct list_head *ixattrs, unsigned int size)
 }
 
 struct xattr_iter {
+	struct erofs_sb_info *sbi;
 	char page[EROFS_BLKSIZ];
 
 	void *kaddr;
@@ -729,7 +732,8 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 	struct xattr_iter it;
 	unsigned int i;
 	struct erofs_xattr_ibody_header *ih;
-	struct erofs_device *dev = sbi.dev;
+	struct erofs_sb_info *sbi = vi->sbi;
+	struct erofs_device *dev = vi->sbi->dev;
 	int ret = 0;
 
 	/* the most case is that xattrs of this inode are initialized. */
@@ -757,8 +761,8 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 		return -ENOATTR;
 	}
 
-	it.blkaddr = erofs_blknr(iloc(vi->nid) + vi->inode_isize);
-	it.ofs = erofs_blkoff(iloc(vi->nid) + vi->inode_isize);
+	it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
+	it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
 
 	ret = blk_read(dev, 0, it.page, it.blkaddr, 1);
 	if (ret < 0)
@@ -825,7 +829,7 @@ static inline int xattr_iter_fixup(struct xattr_iter *it)
 
 	it->blkaddr += erofs_blknr(it->ofs);
 
-	ret = blk_read(sbi.dev, 0, it->page, it->blkaddr, 1);
+	ret = blk_read(it->sbi->dev, 0, it->page, it->blkaddr, 1);
 	if (ret < 0)
 		return -EIO;
 
@@ -837,6 +841,8 @@ static inline int xattr_iter_fixup(struct xattr_iter *it)
 static int inline_xattr_iter_pre(struct xattr_iter *it,
 				   struct erofs_inode *vi)
 {
+	struct erofs_sb_info *sbi = vi->sbi;
+	struct erofs_device *dev = vi->sbi->dev;
 	unsigned int xattr_header_sz, inline_xattr_ofs;
 	int ret;
 
@@ -848,10 +854,10 @@ static int inline_xattr_iter_pre(struct xattr_iter *it,
 
 	inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
 
-	it->blkaddr = erofs_blknr(iloc(vi->nid) + inline_xattr_ofs);
-	it->ofs = erofs_blkoff(iloc(vi->nid) + inline_xattr_ofs);
+	it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
+	it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
 
-	ret = blk_read(sbi.dev, 0, it->page, it->blkaddr, 1);
+	ret = blk_read(dev, 0, it->page, it->blkaddr, 1);
 	if (ret < 0)
 		return -EIO;
 
@@ -1038,17 +1044,18 @@ static int inline_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 
 static int shared_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 {
+	struct erofs_sb_info *sbi = vi->sbi;
 	unsigned int i;
 	int ret = -ENOATTR;
 
 	for (i = 0; i < vi->xattr_shared_count; ++i) {
 		erofs_blk_t blkaddr =
-			xattrblock_addr(vi->xattr_shared_xattrs[i]);
+			xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
 
 		it->it.ofs = xattrblock_offset(vi->xattr_shared_xattrs[i]);
 
 		if (!i || blkaddr != it->it.blkaddr) {
-			ret = blk_read(sbi.dev, 0, it->it.page, blkaddr, 1);
+			ret = blk_read(sbi->dev, 0, it->it.page, blkaddr, 1);
 			if (ret < 0)
 				return -EIO;
 
@@ -1091,6 +1098,7 @@ int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,
 	it.buffer = buffer;
 	it.buffer_size = buffer_size;
 
+	it.it.sbi = vi->sbi;
 	ret = inline_getxattr(vi, &it);
 	if (ret == -ENOATTR)
 		ret = shared_getxattr(vi, &it);
@@ -1178,16 +1186,17 @@ static int inline_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 
 static int shared_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 {
+	struct erofs_sb_info *sbi = vi->sbi;
 	unsigned int i;
 	int ret = 0;
 
 	for (i = 0; i < vi->xattr_shared_count; ++i) {
 		erofs_blk_t blkaddr =
-			xattrblock_addr(vi->xattr_shared_xattrs[i]);
+			xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
 
 		it->it.ofs = xattrblock_offset(vi->xattr_shared_xattrs[i]);
 		if (!i || blkaddr != it->it.blkaddr) {
-			ret = blk_read(sbi.dev, 0, it->it.page, blkaddr, 1);
+			ret = blk_read(sbi->dev, 0, it->it.page, blkaddr, 1);
 			if (ret < 0)
 				return -EIO;
 
@@ -1218,6 +1227,7 @@ int erofs_listxattr(struct erofs_inode *vi, char *buffer, size_t buffer_size)
 	it.buffer_size = buffer_size;
 	it.buffer_ofs = 0;
 
+	it.it.sbi = vi->sbi;
 	ret = inline_listxattr(vi, &it);
 	if (ret < 0 && ret != -ENOATTR)
 		return ret;
lib/zmap.c +15 -11
index 6e5a5d4..0ddc922 100644
@@ -16,8 +16,10 @@ static int z_erofs_do_map_blocks(struct erofs_inode *vi,
 
 int z_erofs_fill_inode(struct erofs_inode *vi)
 {
-	if (!erofs_sb_has_big_pcluster() &&
-	    !erofs_sb_has_ztailpacking() &&
+	struct erofs_sb_info *sbi = vi->sbi;
+
+	if (!erofs_sb_has_big_pcluster(sbi) &&
+	    !erofs_sb_has_ztailpacking(sbi) &&
 	    vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
 		vi->z_advise = 0;
 		vi->z_algorithmtype[0] = 0;
@@ -31,6 +33,7 @@ int z_erofs_fill_inode(struct erofs_inode *vi)
 
 static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 {
+	struct erofs_sb_info *sbi = vi->sbi;
 	int ret;
 	erofs_off_t pos;
 	struct z_erofs_map_header *h;
@@ -39,12 +42,13 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 	if (vi->flags & EROFS_I_Z_INITED)
 		return 0;
 
-	DBG_BUGON(!erofs_sb_has_big_pcluster() &&
-		  !erofs_sb_has_ztailpacking() &&
+	DBG_BUGON(!erofs_sb_has_big_pcluster(sbi) &&
+		  !erofs_sb_has_ztailpacking(sbi) &&
 		  vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
-	pos = round_up(iloc(vi->nid) + vi->inode_isize + vi->xattr_isize, 8);
+	pos = round_up(iloc(vi->sbi, vi->nid) + vi->inode_isize +
+		       vi->xattr_isize, 8);
 
-	ret = dev_read(sbi.dev, 0, buf, pos, sizeof(buf));
+	ret = dev_read(sbi->dev, 0, buf, pos, sizeof(buf));
 	if (ret < 0)
 		return -EIO;
 
@@ -111,7 +115,7 @@ static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
 	if (map->index == eblk)
 		return 0;
 
-	ret = blk_read(sbi.dev, 0, mpage, eblk, 1);
+	ret = blk_read(m->inode->sbi->dev, 0, mpage, eblk, 1);
 	if (ret < 0)
 		return -EIO;
 
@@ -124,7 +128,7 @@ static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
 					 unsigned long lcn)
 {
 	struct erofs_inode *const vi = m->inode;
-	const erofs_off_t ibase = iloc(vi->nid);
+	const erofs_off_t ibase = iloc(vi->sbi, vi->nid);
 	const erofs_off_t pos =
 		Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
 					       vi->xattr_isize) +
@@ -322,9 +326,9 @@ static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
 {
 	struct erofs_inode *const vi = m->inode;
 	const unsigned int lclusterbits = vi->z_logical_clusterbits;
-	const erofs_off_t ebase = round_up(iloc(vi->nid) + vi->inode_isize +
-					   vi->xattr_isize, 8) +
-		sizeof(struct z_erofs_map_header);
+	const erofs_off_t ebase = round_up(iloc(vi->sbi, vi->nid) +
+					   vi->inode_isize + vi->xattr_isize, 8) +
+				  sizeof(struct z_erofs_map_header);
 	const unsigned int totalidx = BLK_ROUND_UP(vi->i_size);
 	unsigned int compacted_4b_initial, compacted_2b;
 	unsigned int amortizedshift;
mkfs/main.c +47 -44

238 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/4 erofs-utils: tests: add test for dump.erofs

In reply to Huang Jianan

Add basic functional check for dump.erofs.

---
 tests/Makefile.am   |  6 +++-
 tests/erofs/021     | 83 +++++++++++++++++++++++++++++++++++++++++++++
 tests/erofs/021.out |  2 ++
 3 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100755 tests/erofs/021
 create mode 100644 tests/erofs/021.out
tests/Makefile.am +5 -1
index b85ae89..bb1624d 100644
@@ -22,7 +22,8 @@ TESTS_ENVIRONMENT = \
 	fi; \
 	[ -z $$MKFS_EROFS_PROG ] && export MKFS_EROFS_PROG=../mkfs/mkfs.erofs; \
 	[ -z $$FSCK_EROFS_PROG ] && export FSCK_EROFS_PROG=../fsck/fsck.erofs; \
-	[ -z $$EROFSFUSE_PROG ] && export EROFSFUSE_PROG=../fuse/erofsfuse;
+	[ -z $$EROFSFUSE_PROG ] && export EROFSFUSE_PROG=../fuse/erofsfuse; \
+	[ -z $$DUMP_EROFS_PROG ] && export DUMP_EROFS_PROG=../dump/dump.erofs;
 
 if ENABLE_LZ4
 TESTS_ENVIRONMENT += export lz4_on=1;
@@ -91,6 +92,9 @@ TESTS += erofs/018
 # 019 - check extended attribute functionality
 TESTS += erofs/019
 
+# 021 - check dump basic functionality
+TESTS += erofs/021
+
 EXTRA_DIST = common/rc erofs
 
 clean-local: clean-local-check
tests/erofs/021 new file +83 -0

83 lines not on this page · raw message

tests/erofs/021.out new file +2 -0
new file mode 100644
index 0000000..09f4062
@@ -0,0 +1,2 @@
+QA output created by 021
+Silence is golden
Signature · 1 line
2.34.1
#3 lore raw

PATCH 4/4 erofs-utils: tests: add test for fsck.erofs

In reply to Huang Jianan

Add basic functional check for fsck.erofs.

---
 tests/Makefile.am   |  3 +++
 tests/common/rc     | 46 +++++++++++++++++++++++++++++++++
 tests/erofs/022     | 62 +++++++++++++++++++++++++++++++++++++++++++++
 tests/erofs/022.out |  2 ++
 4 files changed, 113 insertions(+)
 create mode 100755 tests/erofs/022
 create mode 100644 tests/erofs/022.out
tests/Makefile.am +3 -0
index bb1624d..25c1400 100644
@@ -95,6 +95,9 @@ TESTS += erofs/019
 # 021 - check dump basic functionality
 TESTS += erofs/021
 
+# 021 - check fsck basic functionality
+TESTS += erofs/022
+
 EXTRA_DIST = common/rc erofs
 
 clean-local: clean-local-check
tests/common/rc +46 -0

52 lines not on this page · raw message

tests/erofs/022 new file +62 -0

62 lines not on this page · raw message

tests/erofs/022.out new file +2 -0
new file mode 100644
index 0000000..394c6a7
@@ -0,0 +1,2 @@
+QA output created by 022
+Silence is golden
Signature · 1 line
2.34.1