erofs-utils: tar: support gzip index generation
PATCH v2 1/2 erofs-utils: tar: support gzip index generation
Let's support AWS SOCI-compatible zinfo version v2 generation.
Since an OCI image layer cannot be randomly accessed, the new gzip index can be used for OCI gzip random access.
Example:
$ mkfs.erofs --tar=i --gzinfo=foo.zinfo foo.tarmeta.erofs foo.tgz
Signed-off-by Gao Xiang <[email protected]>
---
changes since v1:
- add support for (e)stargz gzip streams.
include/erofs/tar.h | 7 +- lib/Makefile.am | 2 + lib/gzran.c | 223 +++++++++++++++++++++++++++++++++++++++++++ lib/liberofs_gzran.h | 21 ++++ lib/tar.c | 20 ++++ mkfs/main.c | 25 ++++- 6 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 lib/gzran.c create mode 100644 lib/liberofs_gzran.h
include/erofs/tar.h +6 -1
@@ -24,12 +24,17 @@ struct erofs_pax_header {#define EROFS_IOS_DECODER_NONE 0#define EROFS_IOS_DECODER_GZIP 1#define EROFS_IOS_DECODER_LIBLZMA 2 +#define EROFS_IOS_DECODER_GZRAN 3struct erofs_iostream_liblzma ; +struct erofs_gzran_builder ;struct erofs_iostream {union { -struct erofs_vfile vf; +struct { +struct erofs_vfile vf; +struct erofs_gzran_builder *gb; + };void *handler;#ifdef HAVE_LIBLZMA struct erofs_iostream_liblzma *lzma;
lib/Makefile.am +2 -0
@@ -31,6 +31,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \ $(top_srcdir)/lib/liberofs_compress.h \ $(top_srcdir)/lib/liberofs_private.h \ $(top_srcdir)/lib/liberofs_xxhash.h \ + $(top_srcdir)/lib/liberofs_gzran.h \ $(top_srcdir)/lib/liberofs_metabox.h \ $(top_srcdir)/lib/liberofs_nbd.h \ $(top_srcdir)/lib/liberofs_s3.h
@@ -86,3 +87,4 @@ endif liberofs_la_SOURCES += remotes/oci.c liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +liberofs_la_SOURCES += gzran.c
lib/gzran.c new file +223 -0
223 lines not on this page · raw message
lib/liberofs_gzran.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Alibaba Cloud +*/ +#ifndef __EROFS_LIB_LIBEROFS_GZRAN_H +#define __EROFS_LIB_LIBEROFS_GZRAN_H + +#include "erofs/io.h" + +#define EROFS_GZRAN_WINSIZE 32768 + +struct erofs_gzran_builder ; + +struct erofs_gzran_builder *erofs_gzran_builder_init (struct erofs_vfile *vf, +u32 span_size); +int erofs_gzran_builder_read (struct erofs_gzran_builder *gb,char *window); +int erofs_gzran_builder_export_zinfo (struct erofs_gzran_builder *gb, +struct erofs_vfile *zinfo_vf); +int erofs_gzran_builder_final (struct erofs_gzran_builder *gb); + +#endif
lib/tar.c +20 -0
@@ -17,6 +17,7 @@#endif #include "liberofs_base64.h" #include "liberofs_cache.h" +#include "liberofs_gzran.h" /* This file is a tape/volume header. Ignore it on extraction. */ #define GNUTYPE_VOLHDR 'V'
@@ -65,6 +66,9 @@ void erofs_iostream_close(struct erofs_iostream *ios)free (ios->lzma);#endif return ; + }else if (ios->decoder ==EROFS_IOS_DECODER_GZRAN ) { +erofs_gzran_builder_final (ios->gb); +return ; }erofs_io_close (&ios->vf); }
@@ -105,6 +109,14 @@ int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder)#else return -EOPNOTSUPP ;#endif + }else if (decoder ==EROFS_IOS_DECODER_GZRAN ) { + ios->vf.fd = fd; + ios->feof = false; + ios->sz =0 ; + ios->bufsize =EROFS_GZRAN_WINSIZE *2 ; + ios->gb =erofs_gzran_builder_init (&ios->vf,4194304 ); +if (IS_ERR (ios->gb)) +return PTR_ERR (ios->gb); }else { ios->vf.fd = fd; fsz =lseek (fd,0 ,SEEK_END );
@@ -204,6 +216,14 @@ int erofs_iostream_read(struct erofs_iostream *ios, void **buf, u64 bytes)#else return -EOPNOTSUPP ;#endif + }else if (ios->decoder ==EROFS_IOS_DECODER_GZRAN ) { + ret =erofs_gzran_builder_read (ios->gb, ios->buffer + rabytes); +if (ret <0 ) +return ret; + ios->tail += ret; +DBG_BUGON (ios->tail > ios->bufsize); +if (!ret) + ios->feof = true; }else { ret =erofs_io_read (&ios->vf, ios->buffer + rabytes, ios->bufsize - rabytes);
mkfs/main.c +24 -1
@@ -28,6 +28,7 @@#include "erofs/fragments.h" #include "erofs/rebuild.h" #include "../lib/compressor.h" +#include "../lib/liberofs_gzran.h" #include "../lib/liberofs_metabox.h" #include "../lib/liberofs_oci.h" #include "../lib/liberofs_private.h"
@@ -71,6 +72,7 @@ static struct option long_options[] = { #ifdef HAVE_ZLIB {"gzip", no_argument, NULL, 518}, {"ungzip", optional_argument, NULL, 518}, + {"gzinfo", optional_argument, NULL, 535}, #endif #ifdef HAVE_LIBLZMA {"unlzma", optional_argument, NULL, 519},
@@ -233,6 +235,9 @@ static void usage(int argc, char **argv)#ifdef HAVE_LIBLZMA " --unxz[=X] try to filter the tarball stream through xz/lzma/lzip\n" " (and optionally dump the raw stream to X together)\n" +#endif +#ifdef HAVE_ZLIB +" --gzinfo[=X] generate AWS SOCI-compatible zinfo in order to support random gzip access\n" #endif" --vmdk-desc=X generate a VMDK descriptor file to merge sub-filesystems\n" #ifdef EROFS_MT_ENABLED
@@ -298,6 +303,7 @@ static bool valid_fixeduuid;static unsigned int dsunit;static int tarerofs_decoder;static FILE *vmdk_dcf; +static char *mkfs_aws_zinfo_file; static int erofs_mkfs_feat_set_legacy_compress(struct erofs_importer_params *params, bool en, const char *val,
@@ -1368,6 +1374,11 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params, source_mode =EROFS_MKFS_SOURCE_OCI ;break ;#endif +case 535 : +if (optarg) + mkfs_aws_zinfo_file =strdup (optarg); + tarerofs_decoder =EROFS_IOS_DECODER_GZRAN ; +break ;case 'V' :version ();exit (0 );
@@ -1943,7 +1954,19 @@ exit:erofs_xattr_cleanup_name_prefixes ();erofs_rebuild_cleanup ();erofs_diskbuf_exit (); -if (source_mode ==EROFS_MKFS_SOURCE_TAR ) { +if (!err && source_mode ==EROFS_MKFS_SOURCE_TAR ) { +if (mkfs_aws_zinfo_file) { +struct erofs_vfile vf; +int fd; + + fd =open (mkfs_aws_zinfo_file,O_WRONLY |O_CREAT |O_TRUNC ,0644 ); +if (fd <0 ) { + err = -errno; + }else { + vf = (struct erofs_vfile ){ .fd = fd }; + err =erofs_gzran_builder_export_zinfo (erofstar.ios.gb, &vf); + } + }erofs_iostream_close (&erofstar.ios);if (erofstar.ios.dumpfd >=0 )close (erofstar.ios.dumpfd);
Signature · 1 line
2.43.5
PATCH v2 2/2 erofs-utils: lib: add gzran virtual file support
Add a .pread() interface to support gzip random access with the help of the previous zinfo gzip indices.
Note that (e)stargz formats are also supported.
Signed-off-by Gao Xiang <[email protected]>
--- lib/gzran.c | 176 +++++++++++++++++++++++++++++++++++++++++++ lib/liberofs_gzran.h | 2 + 2 files changed, 178 insertions(+)
lib/gzran.c +176 -0
@@ -201,6 +201,176 @@ int erofs_gzran_builder_final(struct erofs_gzran_builder *gb)free (gb);return 0 ; } + +struct erofs_gzran_iostream { +struct erofs_vfile *vin; +struct erofs_gzran_cutpoint *cp; +u32 entries; +u32 span_size; +}; + +static void erofs_gzran_ios_vfclose (struct erofs_vfile *vf) +{ +struct erofs_gzran_iostream *ios = + (struct erofs_gzran_iostream *)vf->payload; +free (ios->cp); +free (vf); +} + +static ssize_t erofs_gzran_ios_vfpread (struct erofs_vfile *vf,void *buf,size_t len,u64 offset) +{ +struct erofs_gzran_iostream *ios = + (struct erofs_gzran_iostream *)vf->payload; +struct erofs_gzran_cutpoint *cp = ios->cp; +u8 src[1 <<14 ], discard[EROFS_GZRAN_WINSIZE ]; +unsigned int bits; +bool skip = true; +u64 inpos; +z_stream strm; +int ret; + +while (cp < ios->cp + ios->entries -1 && cp[1 ].outpos <= offset) + ++cp; + + strm.zalloc =Z_NULL ; + strm.zfree =Z_NULL ; + strm.opaque =Z_NULL ; + strm.avail_in =0 ; + strm.next_in =Z_NULL ; + ret =inflateInit2 (&strm,-15 );/* raw inflate */ +if (ret !=Z_OK ) +return -EFAULT ; + + bits = cp->in_bitpos &7 ; + inpos = (cp->in_bitpos >>3 ) - (bits ?1 :0 ); + ret =erofs_io_pread (ios->vin, src,sizeof (src), inpos); +if (ret <0 ) +return ret; + +if (bits) { +inflatePrime (&strm, bits, src[0 ] >> (8 - bits)); + strm.next_in = src +1 ; + strm.avail_in = ret -1 ; + }else { + strm.next_in = src; + strm.avail_in = ret; + } + inpos += ret; + (void )inflateSetDictionary (&strm, cp->window,sizeof (cp->window)); + + offset -= cp->outpos; +do { +/* define where to put uncompressed data, and how much */ +if (!offset && skip) {/* at offset now */ + strm.avail_out = len; + strm.next_out = buf; + skip = false;/* only do this once */ + }else if (offset >sizeof (discard)) {/* skip WINSIZE bytes */ + strm.avail_out =sizeof (discard); + strm.next_out = discard; + offset -=sizeof (discard); + }else if (offset) {/* last skip */ + strm.avail_out = (unsigned int )offset; + strm.next_out = discard; + offset =0 ; + } + +/* uncompress until avail_out filled, or end of stream */ +do { +if (!strm.avail_in) { + ret =erofs_io_pread (ios->vin, src,sizeof (src), + inpos); +if (ret <0 ) +return ret; +if (!ret) +return -EIO ; + inpos += ret; + strm.avail_in = ret; + strm.next_in = src; + } + ret =inflate (&strm,Z_NO_FLUSH );/* normal inflate */ +if (ret ==Z_NEED_DICT ) + ret =Z_DATA_ERROR ; +if (ret ==Z_MEM_ERROR || ret ==Z_DATA_ERROR ) +return -EIO ; +if (ret ==Z_STREAM_END ) +break ; + }while (strm.avail_out); + +/* if reach end of stream, then don't keep trying to get more */ +if (ret ==Z_STREAM_END ) +break ; + +/* do until offset reached and requested data read, or stream ends */ + }while (skip); +return len - strm.avail_out; +} + +static struct erofs_vfops erofs_gzran_ios_vfops = { + .pread = erofs_gzran_ios_vfpread, + .close = erofs_gzran_ios_vfclose, +}; + +struct erofs_vfile *erofs_gzran_zinfo_open (struct erofs_vfile *vin, +void *zinfo_buf,unsigned int len) +{ +struct aws_soci_zinfo_header *h; +struct aws_soci_zinfo_ckpt *c; +struct erofs_vfile *vf; +struct erofs_gzran_iostream *ios; +unsigned int v2_size, version; +int ret, i; + +if (len && len <sizeof (*h)) +return ERR_PTR (-EINVAL ); + + vf =malloc (sizeof (*vf) +sizeof (*ios)); +if (!vf) +return ERR_PTR (-ENOMEM ); + + ios = (struct erofs_gzran_iostream *)vf->payload; + h = zinfo_buf; + ios->entries =le32_to_cpu (h->have); + ios->span_size =le32_to_cpu (h->span_size); + + v2_size =sizeof (*c) * ios->entries +sizeof (*h); +if (!len || v2_size == len) { + version =2 ; + }else if (v2_size -sizeof (*c) == len) { + version =1 ; + }else { + ret = -EOPNOTSUPP ; + goto err_ios; + } + + ios->cp =malloc (sizeof (*ios->cp) * ios->entries); +if (!ios->cp) { + ret = -ENOMEM ; + goto err_ios; + } + + i =0 ; +if (version ==1 ) { + ios->cp[0 ] = (struct erofs_gzran_cutpoint ) { + .in_bitpos =10 <<3 , + .outpos =0 , + }; + i =1 ; + } + + c = (struct aws_soci_zinfo_ckpt *)(h +1 ); +for (; i < ios->entries; ++i, ++c) { + ios->cp[i].in_bitpos = (le64_to_cpu (c->in) <<3 ) | c->bits; + ios->cp[i].outpos =le64_to_cpu (c->out); +memcpy (ios->cp[i].window, c->window,sizeof (*c->window)); + } + ios->vin = vin; + vf->ops = &erofs_gzran_ios_vfops; +return vf; +err_ios: +free (vf); +return ERR_PTR (ret); +}#else struct erofs_gzran_builder *erofs_gzran_builder_init (struct erofs_vfile *vf,u32 span_size)
@@ -220,4 +390,10 @@ int erofs_gzran_builder_final(struct erofs_gzran_builder *gb) {return 0 ; } + +struct erofs_vfile *erofs_gzran_zinfo_open (struct erofs_vfile *vin, +void *zinfo_buf,unsigned int len) +{ +return ERR_PTR (-EOPNOTSUPP ); +}#endif
lib/liberofs_gzran.h +2 -0
@@ -18,4 +18,6 @@ int erofs_gzran_builder_export_zinfo(struct erofs_gzran_builder *gb, struct erofs_vfile *zinfo_vf);int erofs_gzran_builder_final (struct erofs_gzran_builder *gb); +struct erofs_vfile *erofs_gzran_zinfo_open (struct erofs_vfile *vin, +void *zinfo_buf,unsigned int len);#endif
Signature · 1 line
2.43.5