Skip to contentSkip to conversation

erofs-utils: lib: fix erofs_io_sendfile() again

PATCH New Gao Xiang 1 participant 2 messages Oct 17, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: lib: fix erofs_io_sendfile() again

The number of bytes written should be returned.

Fixes 29466e7f1cbf ("erofs-utils: lib: fix erofs_io_sendfile()")
---
 lib/io.c     | 13 +++++++------
 mount/main.c | 12 ++++++------
 2 files changed, 13 insertions(+), 12 deletions(-)
lib/io.c +7 -6
index 440f69b..90d2e54 100644
@@ -581,6 +581,7 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 			  off_t *pos, size_t count)
 {
 	ssize_t read, written;
+	size_t rem = count;
 
 	if (vin->ops || vout->ops) {
 		if (vin->ops && vin->ops->sendfile)
@@ -600,13 +601,13 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 			}
 			break;
 		}
-		count -= written;
+		rem -= written;
 	} while (written);
 #endif
-	while (count) {
+	while (rem) {
 		char buf[max(EROFS_MAX_BLOCK_SIZE, 32768)];
 
-		read = min_t(u64, count, sizeof(buf));
+		read = min_t(u64, rem, sizeof(buf));
 		if (pos)
 			read = erofs_io_pread(vin, buf, read, *pos);
 		else
@@ -615,17 +616,17 @@ ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
 			written = read;
 			break;
 		}
-		count -= read;
+		rem -= read;
 		if (pos)
 			*pos += read;
 		do {
 			written = erofs_io_write(vout, buf, read);
 			if (written < 0)
-				break;
+				return written;
 			read -= written;
 		} while (read);
 	}
-	return written < 0 ? written : count;
+	return written < 0 ? written : count - rem;
 }
 
 int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
mount/main.c +6 -6
index 53bd2b2..e25134c 100644
@@ -577,7 +577,7 @@ static void *erofsmount_nbd_loopfn(void *arg)
 
 	while (1) {
 		struct erofs_nbd_request rq;
-		ssize_t rem;
+		ssize_t written;
 		off_t pos;
 
 		err = erofs_nbd_get_request(ctx->sk.fd, &rq);
@@ -597,13 +597,13 @@ static void *erofsmount_nbd_loopfn(void *arg)
 		erofs_nbd_send_reply_header(ctx->sk.fd, rq.cookie, 0);
 		pos = rq.from;
 		do {
-			rem = erofs_io_sendfile(&ctx->sk, &ctx->vd, &pos, rq.len);
-			if (rem == -EINTR) {
-				err = rem;
+			written = erofs_io_sendfile(&ctx->sk, &ctx->vd, &pos, rq.len);
+			if (written == -EINTR) {
+				err = written;
 				goto out;
 			}
-		} while (rem < 0);
-		err = __erofs_0write(ctx->sk.fd, rem);
+		} while (written < 0);
+		err = __erofs_0write(ctx->sk.fd, rq.len - written);
 		if (err) {
 			if (err > 0)
 				err = -EIO;
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/2 erofs-utils: mkfs: support directory compression

Although directories can be compressed using various approaches supported by regular inodes, it is difficult to predict the total compressed size due to a lack of (parent) NIDs or child NIDs at that time.

To simplify this, dump entire directory data into the packed inode (all-fragments) as a first step. Therefore, Linux 6.1+ is required for directory compression for now.

_____________________________________________________________________

|__ Testset __|_________|_______ Command line _________|____ Size ____|
|             | Vanilla | -zlzma -Efragments -C1048576 |  2553946112  |
|             |         |______________________________|_ [2436 MiB] _|
|             |         | [..] -m4096                  |  2524413952  |
| Fedora KIWI |_________|______________________________|_ [2408 MiB] _|
|             | After   | [..] --zD                    |  2542051328  |
|             |         |______________________________|_ [2425 MiB] _|
|             |         | [..] -m4096 --zD             |  2521423872  |
|_____________|_________|______________________________|_ [2405 MiB] _|
|             | Vanilla | -zlzma -Efragments -C1048576 |   4837376    |
|   OpenWrt   |_________|______________________________|_ [4724 KiB] _|
|             | After   | [..] -m4096 --zD             |   4730880    |
|_____________|_________|______________________________|_ [4620 KiB] _|

-m4096 Enable inode metadata compression in 4K pclusters.
--zD Enable directory data compression.

Note that incremental builds are still unsupported for compressed directories, but this can be implemented later.

---
 include/erofs/importer.h |  1 +
 lib/compress.c           | 45 +++++++++++++++++++++++++++++++++++++++-
 lib/fragments.c          |  8 +++++--
 lib/importer.c           | 17 +++++++--------
 lib/inode.c              | 31 ++++++++++++++++++++-------
 lib/liberofs_compress.h  |  4 ++++
 mkfs/main.c              |  8 +++++++
 7 files changed, 93 insertions(+), 21 deletions(-)
include/erofs/importer.h +1 -0
index 48fe47e..60f81d6 100644
@@ -48,6 +48,7 @@ struct erofs_importer_params {
 	bool dedupe;
 	bool fragments;
 	bool all_fragments;
+	bool compress_dir;
 	char fragdedupe;
 };
 
lib/compress.c +44 -1

57 lines not on this page · raw message

lib/fragments.c +6 -2
index 15092e1..0f07e33 100644
@@ -257,11 +257,13 @@ int erofs_pack_file_from_fd(struct erofs_inode *inode,
 	if (memblock == MAP_FAILED || !memblock) {
 		erofs_off_t remaining = inode->i_size;
 		struct erofs_vfile vout = { .fd = epi->fd };
+		bool noseek = vf->ops && !vf->ops->pread;
 		off_t pos = fpos;
 
 		do {
 			sz = min_t(u64, remaining, UINT_MAX);
-			rc = erofs_io_sendfile(&vout, vf, &pos, sz);
+			rc = erofs_io_sendfile(&vout, vf,
+					       noseek ? NULL : &pos, sz);
 			if (rc <= 0)
 				break;
 			remaining -= rc;
@@ -372,10 +374,12 @@ int erofs_flush_packed_inode(struct erofs_importer *im)
 	struct erofs_inode *inode;
 
 	if (!epi || !erofs_sb_has_fragments(sbi))
-		return -EINVAL;
+		return 0;
 
 	if (lseek(epi->fd, 0, SEEK_CUR) <= 0)
 		return 0;
+
+	erofs_update_progressinfo("Processing packed data ...");
 	inode = erofs_mkfs_build_special_from_fd(im, epi->fd,
 						 EROFS_PACKED_INODE);
 	sbi->packed_nid = erofs_lookupnid(inode);
lib/importer.c +7 -10
index ad4bed8..c73dde2 100644
@@ -57,12 +57,14 @@ int erofs_importer_init(struct erofs_importer *im)
 	if (err)
 		goto out_err;
 
-	if (params->fragments || cfg.c_extra_ea_name_prefixes) {
+	if (params->fragments || cfg.c_extra_ea_name_prefixes ||
+	    params->compress_dir) {
 		subsys = "packedfile";
 		if (!params->pclusterblks_packed)
 			params->pclusterblks_packed = params->pclusterblks_def;
 
-		err = erofs_packedfile_init(sbi, params->fragments);
+		err = erofs_packedfile_init(sbi, params->fragments ||
+						params->compress_dir);
 		if (err)
 			goto out_err;
 	}
@@ -90,7 +92,6 @@ out_err:
 
 int erofs_importer_flush_all(struct erofs_importer *im)
 {
-	const struct erofs_importer_params *params = im->params;
 	struct erofs_sb_info *sbi = im->sbi;
 	unsigned int fsalignblks;
 	int err;
@@ -102,13 +103,9 @@ int erofs_importer_flush_all(struct erofs_importer *im)
 			return err;
 	}
 
-	if ((params->fragments || cfg.c_extra_ea_name_prefixes) &&
-	    erofs_sb_has_fragments(sbi)) {
-		erofs_update_progressinfo("Handling packed data ...");
-		err = erofs_flush_packed_inode(im);
-		if (err)
-			return err;
-	}
+	err = erofs_flush_packed_inode(im);
+	if (err)
+		return err;
 
 	fsalignblks = im->params->fsalignblks ?
 		roundup_pow_of_two(im->params->fsalignblks) : 1;
lib/inode.c +23 -8
index 7587248..f9b5ee9 100644
@@ -344,12 +344,7 @@ static int erofs_prepare_dir_file(struct erofs_importer *im,
 		return -EFAULT;
 	}
 	dir->i_size = d_size;
-
-	/* no compression for all dirs */
-	dir->datalayout = EROFS_INODE_FLAT_INLINE;
-
-	/* it will be used in erofs_prepare_inode_buffer */
-	dir->idata_size = d_size % erofs_blksiz(sbi);
+	dir->datalayout = EROFS_INODE_DATALAYOUT_MAX;
 	return 0;
 }
 
@@ -703,12 +698,16 @@ static int erofs_write_dir_file(struct erofs_inode *dir)
 	struct erofs_vfile *vf;
 	int err;
 
-	DBG_BUGON(dir->idata_size != (dir->i_size & (bsz - 1)));
 	vf = erofs_dirwriter_open(dir);
 	if (IS_ERR(vf))
 		return PTR_ERR(vf);
 
-	err = erofs_write_unencoded_data(dir, vf, 0, true);
+	if (erofs_inode_is_data_compressed(dir->datalayout)) {
+		err = erofs_write_compress_dir(dir, vf);
+	} else {
+		DBG_BUGON(dir->idata_size != (dir->i_size & (bsz - 1)));
+		err = erofs_write_unencoded_data(dir, vf, 0, true);
+	}
 	erofs_io_close(vf);
 	return err;
 }
@@ -1510,6 +1509,22 @@ static int erofs_mkfs_jobfn(struct erofs_importer *im,
 		return erofs_mkfs_handle_nondirectory(im, &item->u.ndir);
 
 	if (item->type == EROFS_MKFS_JOB_DIR) {
+		unsigned int bsz = erofs_blksiz(inode->sbi);
+
+		if (inode->datalayout == EROFS_INODE_DATALAYOUT_MAX) {
+			inode->datalayout = EROFS_INODE_FLAT_INLINE;
+
+			ret = erofs_begin_compress_dir(im, inode);
+			if (ret && ret != -ENOSPC)
+				return ret;
+		} else {
+			DBG_BUGON(inode->datalayout != EROFS_INODE_FLAT_PLAIN);
+		}
+
+		/* it will be used in erofs_prepare_inode_buffer */
+		if (inode->datalayout == EROFS_INODE_FLAT_INLINE)
+			inode->idata_size = inode->i_size & (bsz - 1);
+
 		ret = erofs_prepare_inode_buffer(im, inode);
 		if (ret)
 			return ret;
lib/liberofs_compress.h +4 -0
index 8b39735..4b9dd42 100644
@@ -22,6 +22,10 @@ void erofs_bind_compressed_file_with_fd(struct z_erofs_compress_ictx *ictx,
 int erofs_begin_compressed_file(struct z_erofs_compress_ictx *ictx);
 int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx);
 
+int erofs_begin_compress_dir(struct erofs_importer *im,
+			     struct erofs_inode *inode);
+int erofs_write_compress_dir(struct erofs_inode *inode, struct erofs_vfile *vf);
+
 int z_erofs_compress_init(struct erofs_importer *im);
 int z_erofs_compress_exit(struct erofs_sb_info *sbi);
 
mkfs/main.c +8 -0
index 11e3032..f1ea7df 100644
@@ -100,6 +100,7 @@ static struct option long_options[] = {
 #ifdef OCIEROFS_ENABLED
 	{"oci", optional_argument, NULL, 534},
 #endif
+	{"zD", optional_argument, NULL, 536},
 	{0, 0, 0, 0},
 };
 
@@ -174,6 +175,7 @@ static void usage(int argc, char **argv)
 		"    --all-time         the timestamp is also applied to all files (default)\n"
 		"    --mkfs-time        the timestamp is applied as build time only\n"
 		" -UX                   use a given filesystem UUID\n"
+		" --zD[=<0|1>]          specify directory compression: 0=disable [default], 1=enable\n"
 		" --all-root            make all files owned by root\n"
 #ifdef EROFS_MT_ENABLED
 		" --async-queue-limit=# specify the maximum number of entries in the multi-threaded job queue\n"
@@ -1404,6 +1406,12 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 				mkfs_aws_zinfo_file = strdup(optarg);
 			tarerofs_decoder = EROFS_IOS_DECODER_GZRAN;
 			break;
+		case 536:
+			if (!optarg || strcmp(optarg, "1"))
+				params->compress_dir = true;
+			else
+				params->compress_dir = false;
+			break;
 		case 'V':
 			version();
 			exit(0);
Signature · 1 line
2.43.5