erofs-utils: lib: Fix calculation of minextblks when working with sparse files
authorSandeep Dhavale <dhavale@google.com>
Wed, 3 Apr 2024 07:07:00 +0000 (00:07 -0700)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Sun, 7 Apr 2024 07:36:01 +0000 (15:36 +0800)
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>
Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240403070700.1716252-1-dhavale@google.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
lib/blobchunk.c

index ee1219413bf1f496d9f74895bf46d90408e32f13..641e3d4d549a1b8bced4082d7d4a3fab4027acb9 100644 (file)
@@ -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,14 @@ 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,