diff mbox series

[2/2] fs/squashfs: add support for ZSTD decompression

Message ID 20200811131755.22396-3-joaomarcos.costa@bootlin.com
State Changes Requested
Delegated to: Tom Rini
Headers show
Series fs/squashfs: Add new decompression algorithms | expand

Commit Message

Joao Marcos Costa Aug. 11, 2020, 1:17 p.m. UTC
Add call to ZSTD's ZSTD_decompressDCtx(). In this use case, the caller
can upper bound the decompressed size, which will be the SquashFS data
block (or metadata block) size, so there is no need to use streaming
API.

Signed-off-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
---
 fs/squashfs/sqfs_decompressor.c | 39 +++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

Comments

Thomas Petazzoni Aug. 11, 2020, 1:29 p.m. UTC | #1
On Tue, 11 Aug 2020 15:17:55 +0200
Joao Marcos Costa <joaomarcos.costa@bootlin.com> wrote:

> +#if IS_ENABLED(CONFIG_ZSTD)
> +static int sqfs_zstd_decompress(void *dest, unsigned long dest_len,
> +				void *source, u32 src_len)
> +{
> +	void *workspace;
> +	ZSTD_DCtx *ctx;
> +	size_t wsize;
> +	int ret;
> +
> +	wsize = ZSTD_DCtxWorkspaceBound();

So apparently this "workspace" has a constant size, which does not
depend on the size of the input buffer. Correct ?

If that's the case, can we instead allocate it once for all in
sqfs_probe() if the filesystem is zstd-compressed ? I.e perhaps
sqfs_probe() could call a sqfs_decompressor_init() function,
implemented in sqfs_decompressor.c, which will do this sort of
initialization. And of course this memory area would be freed up in
sqfs_close(), calling a sqfs_decompressor_{deinit,cleanup,close}
function.

Best regards,

Thomas
Joao Marcos Costa Aug. 11, 2020, 2:13 p.m. UTC | #2
On Tue, 11 Aug 2020 15:29:51 +0200
Thomas Petazzoni <thomas.petazzoni@bootlin.com> wrote:

> On Tue, 11 Aug 2020 15:17:55 +0200
> Joao Marcos Costa <joaomarcos.costa@bootlin.com> wrote:
> 
> > +#if IS_ENABLED(CONFIG_ZSTD)
> > +static int sqfs_zstd_decompress(void *dest, unsigned long dest_len,
> > +				void *source, u32 src_len)
> > +{
> > +	void *workspace;
> > +	ZSTD_DCtx *ctx;
> > +	size_t wsize;
> > +	int ret;
> > +
> > +	wsize = ZSTD_DCtxWorkspaceBound();  
> 
> So apparently this "workspace" has a constant size, which does not
> depend on the size of the input buffer. Correct ?

Yes, correct.

> If that's the case, can we instead allocate it once for all in
> sqfs_probe() if the filesystem is zstd-compressed ? I.e perhaps
> sqfs_probe() could call a sqfs_decompressor_init() function,
> implemented in sqfs_decompressor.c, which will do this sort of
> initialization. And of course this memory area would be freed up in
> sqfs_close(), calling a sqfs_decompressor_{deinit,cleanup,close}
> function.

Sure, it can be done. I will prepare a v2 addressing your suggestion.
Thanks!

> Best regards,
> 
> Thomas

Best regards,
Joao
diff mbox series

Patch

diff --git a/fs/squashfs/sqfs_decompressor.c b/fs/squashfs/sqfs_decompressor.c
index 9285df5d3b..b5a9d92808 100644
--- a/fs/squashfs/sqfs_decompressor.c
+++ b/fs/squashfs/sqfs_decompressor.c
@@ -18,6 +18,10 @@ 
 #include <u-boot/zlib.h>
 #endif
 
+#if IS_ENABLED(CONFIG_ZSTD)
+#include <linux/zstd.h>
+#endif
+
 #include "sqfs_decompressor.h"
 #include "sqfs_filesystem.h"
 #include "sqfs_utils.h"
@@ -39,6 +43,31 @@  static void zlib_decompression_status(int ret)
 }
 #endif
 
+#if IS_ENABLED(CONFIG_ZSTD)
+static int sqfs_zstd_decompress(void *dest, unsigned long dest_len,
+				void *source, u32 src_len)
+{
+	void *workspace;
+	ZSTD_DCtx *ctx;
+	size_t wsize;
+	int ret;
+
+	wsize = ZSTD_DCtxWorkspaceBound();
+
+	workspace = malloc(wsize);
+	if (!workspace)
+		return -ENOMEM;
+
+	ctx = ZSTD_initDCtx(workspace, wsize);
+
+	ret = ZSTD_decompressDCtx(ctx, dest, dest_len, source, src_len);
+
+	free(workspace);
+
+	return ZSTD_isError(ret);
+}
+#endif /* CONFIG_ZSTD */
+
 int sqfs_decompress(u16 comp_type, void *dest, unsigned long *dest_len,
 		    void *source, u32 src_len)
 {
@@ -67,6 +96,16 @@  int sqfs_decompress(u16 comp_type, void *dest, unsigned long *dest_len,
 		break;
 	}
 #endif
+#if IS_ENABLED(CONFIG_ZSTD)
+	case SQFS_COMP_ZSTD:
+		ret = sqfs_zstd_decompress(dest, *dest_len, source, src_len);
+		if (ret) {
+			printf("ZSTD Error code: %d\n", ZSTD_getErrorCode(ret));
+			return -EINVAL;
+		}
+
+		break;
+#endif /* CONFIG_ZSTD */
 	default:
 		printf("Error: unknown compression type.\n");
 		return -EINVAL;