Linux-EROFS Archive mirror
 help / color / mirror / Atom feed
From: Sandeep Dhavale via Linux-erofs <linux-erofs@lists.ozlabs.org>
To: linux-erofs@lists.ozlabs.org
Cc: hsiangkao@linux.alibaba.com, kernel-team@android.com
Subject: [PATCH v2] erofs-utils: lib: Fix calculation of minextblks when working with sparse files
Date: Wed,  3 Apr 2024 00:07:00 -0700	[thread overview]
Message-ID: <20240403070700.1716252-1-dhavale@google.com> (raw)

When we work with sparse files (files with holes), we need to consider
when the contiguous data block starts after each hole to correctly calculate
minextblks so we can merge consecutive chunks later.
Now that we need to recalculate minextblks multiple places, put the logic
in helper function for avoiding repetition and easier reading.

Fixes: 7b46f7a0160a (erofs-utils: lib: merge consecutive chunks if possible)
Signed-off-by: Sandeep Dhavale <dhavale@google.com>
---
Changes since v1
  - Better name for helper function
  - Change helper function to modify minextblks directly

 lib/blobchunk.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/lib/blobchunk.c b/lib/blobchunk.c
index ee12194..f7aa986 100644
--- a/lib/blobchunk.c
+++ b/lib/blobchunk.c
@@ -223,6 +223,15 @@ out:
 	return 0;
 }
 
+static void erofs_update_minextblks(struct erofs_sb_info *sbi,
+		    erofs_off_t start, erofs_off_t end, erofs_blk_t *minextblks)
+{
+	erofs_blk_t lb;
+	lb = lowbit((end - start) >> sbi->blkszbits);
+	if (lb && lb < *minextblks)
+		*minextblks = lb;
+}
+
 int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 				  erofs_off_t startoff)
 {
@@ -231,8 +240,8 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 	unsigned int count, unit;
 	struct erofs_blobchunk *chunk, *lastch;
 	struct erofs_inode_chunk_index *idx;
-	erofs_off_t pos, len, chunksize;
-	erofs_blk_t lb, minextblks;
+	erofs_off_t pos, len, chunksize, interval_start;
+	erofs_blk_t minextblks;
 	u8 *chunkdata;
 	int ret;
 
@@ -267,9 +276,10 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 		goto err;
 	}
 	idx = inode->chunkindexes;
-
 	lastch = NULL;
 	minextblks = BLK_ROUND_UP(sbi, inode->i_size);
+	interval_start = 0;
+
 	for (pos = 0; pos < inode->i_size; pos += len) {
 #ifdef SEEK_DATA
 		off_t offset = lseek(fd, pos + startoff, SEEK_DATA);
@@ -294,12 +304,15 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 
 		if (offset > pos) {
 			len = 0;
+			erofs_update_minextblks(sbi, interval_start, pos,
+						&minextblks);
 			do {
 				*(void **)idx++ = &erofs_holechunk;
 				pos += chunksize;
 			} while (pos < offset);
 			DBG_BUGON(pos != offset);
 			lastch = NULL;
+			interval_start = pos;
 			continue;
 		}
 #endif
@@ -320,13 +333,15 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 		if (lastch && (lastch->device_id != chunk->device_id ||
 		    erofs_pos(sbi, lastch->blkaddr) + lastch->chunksize !=
 		    erofs_pos(sbi, chunk->blkaddr))) {
-			lb = lowbit(pos >> sbi->blkszbits);
-			if (lb && lb < minextblks)
-				minextblks = lb;
+			erofs_update_minextblks(sbi, interval_start, pos,
+						&minextblks);
+			interval_start = pos;
 		}
 		*(void **)idx++ = chunk;
 		lastch = chunk;
 	}
+	erofs_update_minextblks(sbi, interval_start, pos,
+				&minextblks);
 	inode->datalayout = EROFS_INODE_CHUNK_BASED;
 	free(chunkdata);
 	return erofs_blob_mergechunks(inode, chunkbits,
-- 
2.44.0.478.gd926399ef9-goog


             reply	other threads:[~2024-04-03  7:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-03  7:07 Sandeep Dhavale via Linux-erofs [this message]
2024-04-03  7:10 ` [PATCH v2] erofs-utils: lib: Fix calculation of minextblks when working with sparse files Gao Xiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240403070700.1716252-1-dhavale@google.com \
    --to=linux-erofs@lists.ozlabs.org \
    --cc=dhavale@google.com \
    --cc=hsiangkao@linux.alibaba.com \
    --cc=kernel-team@android.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).