erofs-utils: fix delta[1] out-of-bound of compact indexes
PATCH 1/5 erofs-utils: fix delta[1] out-of-bound of compact indexes
delta[1] should not exceed (1 << lclusterbits) - 1, usually 4095 blocks, which means a single compressed extent which isn't less than 16MiB could trigger this issue (it very rare happens).
--- lib/compress.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/compress.c +2 -1
@@ -627,7 +627,8 @@ static void *write_compacted_indexes(u8 *out, blkaddr += cblks; *dummy_head = false; }else if (i +1 == vcnt) { - offset = cv[i].u.delta[1 ]; + offset =min_t (u16, cv[i].u.delta[1 ], + (1 << logical_clusterbits) -1 ); }else { offset = cv[i].u.delta[0 ]; }
Signature · 1 line
2.24.4
PATCH 1/5 erofs-utils: fix delta[1] out-of-bound of compact indexes
delta[1] should not exceed (1 << lclusterbits) - 1, usually 4095 blocks, which means a single compressed extent which isn't less than 16MiB could trigger this issue (it very rare happens).
--- lib/compress.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/compress.c +2 -1
@@ -627,7 +627,8 @@ static void *write_compacted_indexes(u8 *out, blkaddr += cblks; *dummy_head = false; }else if (i +1 == vcnt) { - offset = cv[i].u.delta[1 ]; + offset =min_t (u16, cv[i].u.delta[1 ], + (1 << logical_clusterbits) -1 ); }else { offset = cv[i].u.delta[0 ]; }
Signature · 1 line
2.24.4
PATCH 2/5 erofs-utils: switch sha256 algorithm directly from LibTomCrypt
LibTomCrypt was released into public domain so that we don't need to worry about license issues against Apache-2.0 later.
--- lib/blobchunk.c | 4 +- lib/dedupe.c | 4 +- lib/sha256.c | 306 ++++++++++++++++++++++++------------------------ lib/sha256.h | 21 ++++ 4 files changed, 176 insertions(+), 159 deletions(-) create mode 100644 lib/sha256.h
lib/blobchunk.c +1 -3
@@ -10,11 +10,9 @@#include "erofs/block_list.h" #include "erofs/cache.h" #include "erofs/io.h" +#include "sha256.h" #include <unistd.h> -void erofs_sha256 (const unsigned char *in,unsigned long in_size, -unsigned char out[32 ]); -struct erofs_blobchunk {struct hashmap_entry ent;char sha256[32 ];
lib/dedupe.c +1 -3
@@ -6,9 +6,7 @@#include "erofs/print.h" #include "rb_tree.h" #include "rolling_hash.h" - -void erofs_sha256 (const unsigned char *in,unsigned long in_size, -unsigned char out[32 ]); +#include "sha256.h" static unsigned int window_size, rollinghash_rm;static struct rb_tree *dedupe_tree, *dedupe_subtree;
lib/sha256.c +153 -153
347 lines not on this page · raw message
lib/sha256.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +#ifndef __EROFS_LIB_SHA256_H +#define __EROFS_LIB_SHA256_H + +#include "erofs/defs.h" + +struct sha256_state { +u64 length; +u32 state[8 ], curlen; +u8 buf[64 ]; +}; + +void erofs_sha256_init (struct sha256_state *md); +int erofs_sha256_process (struct sha256_state *md, +const unsigned char *in,unsigned long inlen); +int erofs_sha256_done (struct sha256_state *md,unsigned char *out); + +void erofs_sha256 (const unsigned char *in,unsigned long in_size, +unsigned char out[32 ]); + +#endif
Signature · 1 line
2.24.4
PATCH 3/5 erofs-utils: avoid using a static srcpath
In reply to Gao XiangTo avoid unnecessary memory overhead.
--- include/erofs/internal.h | 2 +- lib/inode.c | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-)
include/erofs/internal.h +1 -1
@@ -180,7 +180,7 @@ struct erofs_inode { }; } u; -char i_srcpath[PATH_MAX +1 ]; +char *i_srcpath;unsigned char datalayout;unsigned char inode_isize;
lib/inode.c +6 -4
@@ -119,6 +119,8 @@ unsigned int erofs_iput(struct erofs_inode *inode)if (inode->eof_tailraw)free (inode->eof_tailraw);list_del (&inode->i_hash); +if (inode->i_srcpath) +free (inode->i_srcpath);free (inode);return 0 ; }
@@ -905,8 +907,9 @@ static int erofs_fill_inode(struct erofs_inode *inode, struct stat *st,return -EINVAL ; } -strncpy (inode->i_srcpath, path,sizeof (inode->i_srcpath) -1 ); - inode->i_srcpath[sizeof (inode->i_srcpath) -1 ] ='\0' ; + inode->i_srcpath =strdup (path); +if (!inode->i_srcpath) +return -ENOMEM ; inode->dev = st->st_dev; inode->i_ino[1 ] = st->st_ino;
@@ -977,10 +980,9 @@ static struct erofs_inode *erofs_iget_from_path(const char *path, bool is_src) ret =erofs_fill_inode (inode, &st, path);if (ret) { -free (inode); +erofs_iput (inode);return ERR_PTR (ret); } -return inode; }
Signature · 1 line
2.24.4
PATCH 4/5 erofs-utils: handle mmap failure when packing a whole file
In reply to Gao XiangDirectly read/write if mmap failure.
--- lib/fragments.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-)
lib/fragments.c +35 -8
@@ -223,14 +223,39 @@ int z_erofs_pack_file_from_fd(struct erofs_inode *inode, int fd,if (offset <0 )return -errno; - memblock =mmap (NULL , inode->i_size,PROT_READ ,MAP_SHARED , fd,0 ); -if (memblock ==MAP_FAILED ) -return -EFAULT ; - inode->fragmentoff = (erofs_off_t )offset; inode->fragment_size = inode->i_size; -if (fwrite (memblock, inode->fragment_size,1 , packedfile) !=1 ) { + memblock =mmap (NULL , inode->i_size,PROT_READ ,MAP_SHARED , fd,0 ); +if (memblock ==MAP_FAILED || !memblock) { +unsigned long long remaining = inode->fragment_size; + + memblock =NULL ; +while (remaining) { +char buf[32768 ]; + unsigned int sz = min_t(unsigned int, remaining, +sizeof (buf)); + + rc =read (fd, buf, sz); +if (rc != sz) { +if (rc <0 ) + rc = -errno; +else + rc = -EAGAIN ; + goto out; + } +if (fwrite (buf, sz,1 , packedfile) !=1 ) { + rc = -EIO ; + goto out; + } + remaining -= sz; + } + rc =lseek (fd,0 ,SEEK_SET ); +if (rc <0 ) { + rc = -errno; + goto out; + } + }else if (fwrite (memblock, inode->fragment_size,1 , packedfile) !=1 ) { rc = -EIO ; goto out; }
@@ -238,10 +263,12 @@ int z_erofs_pack_file_from_fd(struct erofs_inode *inode, int fd,erofs_dbg ("Recording %u fragment data at %lu" , inode->fragment_size, inode->fragmentoff); - rc =z_erofs_fragments_dedupe_insert (memblock, inode->fragment_size, - inode->fragmentoff, tofcrc); +if (memblock) + rc =z_erofs_fragments_dedupe_insert (memblock, + inode->fragment_size, inode->fragmentoff, tofcrc); out: -munmap (memblock, inode->i_size); +if (memblock) +munmap (memblock, inode->i_size);return rc; }
Signature · 1 line
2.24.4
PATCH 5/5 erofs-utils: dedupe more in the packed inode if possible
In reply to Gao XiangIn addition to just compare the in-memory data.
--- lib/compress.c | 3 ++- lib/fragments.c | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-)
lib/compress.c +2 -1
@@ -371,7 +371,8 @@ static bool z_erofs_fixup_deduped_fragment(struct z_erofs_vle_compress_ctx *ctx, inode->fragment_size, inode->fragmentoff | 0ULL);/* it's the end */ - ctx->head += newsize; +DBG_BUGON (ctx->tail - ctx->head + ctx->remaining != newsize); + ctx->head = ctx->tail; ctx->remaining =0 ;return true; }
lib/fragments.c +24 -1
@@ -55,6 +55,7 @@ static int z_erofs_fragments_dedupe_find(struct erofs_inode *inode, int fd,struct list_head *head;u8 *data;unsigned int length, e2, deduped; +erofs_off_t pos;int ret; head = &dupli_frags[FRAGMENT_HASH (crc)];
@@ -112,9 +113,31 @@ static int z_erofs_fragments_dedupe_find(struct erofs_inode *inode, int fd, goto out;DBG_BUGON (di->length < deduped); + pos = di->pos + di->length - deduped; +/* let's read more to dedupe as long as we can */ +if (deduped == di->length) { +fflush (packedfile); + +while (deduped < inode->i_size && pos) { +char buf[2 ][16384 ]; + unsigned int sz = min_t(unsigned int, pos, +sizeof (buf[0 ])); + +if (pread (fileno (packedfile), buf[0 ], sz, + pos - sz) != sz) +break ; +if (pread (fd, buf[1 ], sz, + inode->i_size - deduped - sz) != sz) +break ; +if (memcmp (buf[0 ], buf[1 ], sz)) +break ; + pos -= sz; + deduped += sz; + } + } inode->fragment_size = deduped; - inode->fragmentoff = di->pos + di->length - deduped; + inode->fragmentoff = pos;erofs_dbg ("Dedupe %u tail data at %llu" , inode->fragment_size, inode->fragmentoff |0ULL );
Signature · 1 line
2.24.4