diff mbox series

[04/49] btrfs: Use U-Boot API for decompression

Message ID 20210503171001.4.I7327e42043265556e3988928849ff2ebdc7b21e6@changeid (mailing list archive)
State New, archived
Headers show
Series image: Reduce #ifdefs and ad-hoc defines in image code | expand

Commit Message

Simon Glass May 3, 2021, 11:10 p.m. UTC
Use the common function to avoid code duplication.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 fs/btrfs/compression.c | 51 +++++-------------------------------------
 1 file changed, 5 insertions(+), 46 deletions(-)

Comments

Qu Wenruo May 3, 2021, 11:25 p.m. UTC | #1
On 2021/5/4 上午7:10, Simon Glass wrote:
> Use the common function to avoid code duplication.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>

Acked-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu
> ---
>
> (no changes since v1)
>
>   fs/btrfs/compression.c | 51 +++++-------------------------------------
>   1 file changed, 5 insertions(+), 46 deletions(-)
>
> diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
> index 23efefa1997..7adfbb04a7c 100644
> --- a/fs/btrfs/compression.c
> +++ b/fs/btrfs/compression.c
> @@ -6,6 +6,7 @@
>    */
>
>   #include "btrfs.h"
> +#include <abuf.h>
>   #include <log.h>
>   #include <malloc.h>
>   #include <linux/lzo.h>
> @@ -136,54 +137,12 @@ static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
>
>   static u32 decompress_zstd(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
>   {
> -	ZSTD_DStream *dstream;
> -	ZSTD_inBuffer in_buf;
> -	ZSTD_outBuffer out_buf;
> -	void *workspace;
> -	size_t wsize;
> -	u32 res = -1;
> -
> -	wsize = ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT);
> -	workspace = malloc(wsize);
> -	if (!workspace) {
> -		debug("%s: cannot allocate workspace of size %zu\n", __func__,
> -		      wsize);
> -		return -1;
> -	}
> -
> -	dstream = ZSTD_initDStream(ZSTD_BTRFS_MAX_INPUT, workspace, wsize);
> -	if (!dstream) {
> -		printf("%s: ZSTD_initDStream failed\n", __func__);
> -		goto err_free;
> -	}
> +	struct abuf in, out;
>
> -	in_buf.src = cbuf;
> -	in_buf.pos = 0;
> -	in_buf.size = clen;
> +	abuf_init_set(&in, (u8 *)cbuf, clen);
> +	abuf_init_set(&out, dbuf, dlen);
>
> -	out_buf.dst = dbuf;
> -	out_buf.pos = 0;
> -	out_buf.size = dlen;
> -
> -	while (1) {
> -		size_t ret;
> -
> -		ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
> -		if (ZSTD_isError(ret)) {
> -			printf("%s: ZSTD_decompressStream error %d\n", __func__,
> -			       ZSTD_getErrorCode(ret));
> -			goto err_free;
> -		}
> -
> -		if (in_buf.pos >= clen || !ret)
> -			break;
> -	}
> -
> -	res = out_buf.pos;
> -
> -err_free:
> -	free(workspace);
> -	return res;
> +	return zstd_decompress(&in, &out);
>   }
>
>   u32 btrfs_decompress(u8 type, const char *c, u32 clen, char *d, u32 dlen)
>
Marek Behún May 3, 2021, 11:45 p.m. UTC | #2
On Mon,  3 May 2021 17:10:51 -0600
Simon Glass <sjg@chromium.org> wrote:

> Use the common function to avoid code duplication.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Is this tested? Why only zstd?

marek
Simon Glass May 4, 2021, 4:58 p.m. UTC | #3
Hi Marek,

On Mon, 3 May 2021 at 17:45, Marek Behun <marek.behun@nic.cz> wrote:
>
> On Mon,  3 May 2021 17:10:51 -0600
> Simon Glass <sjg@chromium.org> wrote:
>
> > Use the common function to avoid code duplication.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
>
> Is this tested? Why only zstd?

No, there are no tests for zstd, as I mentioned in the other patch.
Are you able to add some to the compression.c file? It should really
be done if we expect it to keep working in U-Boot.

The other compression algos already have a suitable function, so don't
need to be reworked.

Regards,
Simon
diff mbox series

Patch

diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 23efefa1997..7adfbb04a7c 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -6,6 +6,7 @@ 
  */
 
 #include "btrfs.h"
+#include <abuf.h>
 #include <log.h>
 #include <malloc.h>
 #include <linux/lzo.h>
@@ -136,54 +137,12 @@  static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
 
 static u32 decompress_zstd(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
 {
-	ZSTD_DStream *dstream;
-	ZSTD_inBuffer in_buf;
-	ZSTD_outBuffer out_buf;
-	void *workspace;
-	size_t wsize;
-	u32 res = -1;
-
-	wsize = ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT);
-	workspace = malloc(wsize);
-	if (!workspace) {
-		debug("%s: cannot allocate workspace of size %zu\n", __func__,
-		      wsize);
-		return -1;
-	}
-
-	dstream = ZSTD_initDStream(ZSTD_BTRFS_MAX_INPUT, workspace, wsize);
-	if (!dstream) {
-		printf("%s: ZSTD_initDStream failed\n", __func__);
-		goto err_free;
-	}
+	struct abuf in, out;
 
-	in_buf.src = cbuf;
-	in_buf.pos = 0;
-	in_buf.size = clen;
+	abuf_init_set(&in, (u8 *)cbuf, clen);
+	abuf_init_set(&out, dbuf, dlen);
 
-	out_buf.dst = dbuf;
-	out_buf.pos = 0;
-	out_buf.size = dlen;
-
-	while (1) {
-		size_t ret;
-
-		ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
-		if (ZSTD_isError(ret)) {
-			printf("%s: ZSTD_decompressStream error %d\n", __func__,
-			       ZSTD_getErrorCode(ret));
-			goto err_free;
-		}
-
-		if (in_buf.pos >= clen || !ret)
-			break;
-	}
-
-	res = out_buf.pos;
-
-err_free:
-	free(workspace);
-	return res;
+	return zstd_decompress(&in, &out);
 }
 
 u32 btrfs_decompress(u8 type, const char *c, u32 clen, char *d, u32 dlen)