258bc8e
From 6113361316d5ce5bfdc118d188e5617a1fcd747c Mon Sep 17 00:00:00 2001
258bc8e
From: Sean Purcell <me@seanp.xyz>
258bc8e
Date: Mon, 14 Aug 2017 22:46:04 -0700
258bc8e
Subject: [PATCH 1/4] squashfs-tools: Add zstd support
258bc8e
258bc8e
This patch adds zstd support to squashfs-tools. It works with zstd
258bc8e
versions >= 1.0.0. It was originally written by Sean Purcell.
258bc8e
258bc8e
Signed-off-by: Sean Purcell <me@seanp.xyz>
258bc8e
Signed-off-by: Nick Terrell <terrelln@fb.com>
258bc8e
---
258bc8e
 squashfs-tools/Makefile       |  20 +++
258bc8e
 squashfs-tools/compressor.c   |   8 ++
258bc8e
 squashfs-tools/squashfs_fs.h  |   1 +
258bc8e
 squashfs-tools/zstd_wrapper.c | 254 ++++++++++++++++++++++++++++++++++
258bc8e
 squashfs-tools/zstd_wrapper.h |  48 +++++++
258bc8e
 5 files changed, 331 insertions(+)
258bc8e
 create mode 100644 squashfs-tools/zstd_wrapper.c
258bc8e
 create mode 100644 squashfs-tools/zstd_wrapper.h
258bc8e
258bc8e
diff --git a/squashfs-tools/Makefile b/squashfs-tools/Makefile
258bc8e
index 52d2582..22fc559 100644
258bc8e
--- a/squashfs-tools/Makefile
258bc8e
+++ b/squashfs-tools/Makefile
258bc8e
@@ -75,6 +75,18 @@ GZIP_SUPPORT = 1
258bc8e
 #LZMA_SUPPORT = 1
258bc8e
 #LZMA_DIR = ../../../../LZMA/lzma465
258bc8e
 
258bc8e
+
258bc8e
+########### Building ZSTD support ############
258bc8e
+#
258bc8e
+# The ZSTD library is supported
258bc8e
+# ZSTD homepage: http://zstd.net
258bc8e
+# ZSTD source repository: https://github.com/facebook/zstd
258bc8e
+#
258bc8e
+# To build using the ZSTD library - install the library and uncomment the
258bc8e
+# ZSTD_SUPPORT line below.
258bc8e
+#
258bc8e
+#ZSTD_SUPPORT = 1
258bc8e
+
258bc8e
 ######## Specifying default compression ########
258bc8e
 #
258bc8e
 # The next line specifies which compression algorithm is used by default
258bc8e
@@ -177,6 +189,14 @@ LIBS += -llz4
258bc8e
 COMPRESSORS += lz4
258bc8e
 endif
258bc8e
 
258bc8e
+ifeq ($(ZSTD_SUPPORT),1)
258bc8e
+CFLAGS += -DZSTD_SUPPORT
258bc8e
+MKSQUASHFS_OBJS += zstd_wrapper.o
258bc8e
+UNSQUASHFS_OBJS += zstd_wrapper.o
258bc8e
+LIBS += -lzstd
258bc8e
+COMPRESSORS += zstd
258bc8e
+endif
258bc8e
+
258bc8e
 ifeq ($(XATTR_SUPPORT),1)
258bc8e
 ifeq ($(XATTR_DEFAULT),1)
258bc8e
 CFLAGS += -DXATTR_SUPPORT -DXATTR_DEFAULT
258bc8e
diff --git a/squashfs-tools/compressor.c b/squashfs-tools/compressor.c
258bc8e
index 525e316..02b5e90 100644
258bc8e
--- a/squashfs-tools/compressor.c
258bc8e
+++ b/squashfs-tools/compressor.c
258bc8e
@@ -65,6 +65,13 @@ static struct compressor xz_comp_ops = {
258bc8e
 extern struct compressor xz_comp_ops;
258bc8e
 #endif
258bc8e
 
258bc8e
+#ifndef ZSTD_SUPPORT
258bc8e
+static struct compressor zstd_comp_ops = {
258bc8e
+	ZSTD_COMPRESSION, "zstd"
258bc8e
+};
258bc8e
+#else
258bc8e
+extern struct compressor zstd_comp_ops;
258bc8e
+#endif
258bc8e
 
258bc8e
 static struct compressor unknown_comp_ops = {
258bc8e
 	0, "unknown"
258bc8e
@@ -77,6 +84,7 @@ struct compressor *compressor[] = {
258bc8e
 	&lzo_comp_ops,
258bc8e
 	&lz4_comp_ops,
258bc8e
 	&xz_comp_ops,
258bc8e
+	&zstd_comp_ops,
258bc8e
 	&unknown_comp_ops
258bc8e
 };
258bc8e
 
258bc8e
diff --git a/squashfs-tools/squashfs_fs.h b/squashfs-tools/squashfs_fs.h
258bc8e
index 791fe12..afca918 100644
258bc8e
--- a/squashfs-tools/squashfs_fs.h
258bc8e
+++ b/squashfs-tools/squashfs_fs.h
258bc8e
@@ -277,6 +277,7 @@ typedef long long		squashfs_inode;
258bc8e
 #define LZO_COMPRESSION		3
258bc8e
 #define XZ_COMPRESSION		4
258bc8e
 #define LZ4_COMPRESSION		5
258bc8e
+#define ZSTD_COMPRESSION	6
258bc8e
 
258bc8e
 struct squashfs_super_block {
258bc8e
 	unsigned int		s_magic;
258bc8e
diff --git a/squashfs-tools/zstd_wrapper.c b/squashfs-tools/zstd_wrapper.c
258bc8e
new file mode 100644
258bc8e
index 0000000..dcab75a
258bc8e
--- /dev/null
258bc8e
+++ b/squashfs-tools/zstd_wrapper.c
258bc8e
@@ -0,0 +1,254 @@
258bc8e
+/*
258bc8e
+ * Copyright (c) 2017
258bc8e
+ * Phillip Lougher <phillip@squashfs.org.uk>
258bc8e
+ *
258bc8e
+ * This program is free software; you can redistribute it and/or
258bc8e
+ * modify it under the terms of the GNU General Public License
258bc8e
+ * as published by the Free Software Foundation; either version 2,
258bc8e
+ * or (at your option) any later version.
258bc8e
+ *
258bc8e
+ * This program is distributed in the hope that it will be useful,
258bc8e
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
258bc8e
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
258bc8e
+ * GNU General Public License for more details.
258bc8e
+ *
258bc8e
+ * zstd_wrapper.c
258bc8e
+ *
258bc8e
+ * Support for ZSTD compression http://zstd.net
258bc8e
+ */
258bc8e
+
258bc8e
+#include <stdio.h>
258bc8e
+#include <string.h>
258bc8e
+#include <stdlib.h>
258bc8e
+#include <zstd.h>
258bc8e
+#include <zstd_errors.h>
258bc8e
+
258bc8e
+#include "squashfs_fs.h"
258bc8e
+#include "zstd_wrapper.h"
258bc8e
+#include "compressor.h"
258bc8e
+
258bc8e
+static int compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
258bc8e
+
258bc8e
+/*
258bc8e
+ * This function is called by the options parsing code in mksquashfs.c
258bc8e
+ * to parse any -X compressor option.
258bc8e
+ *
258bc8e
+ * This function returns:
258bc8e
+ *	>=0 (number of additional args parsed) on success
258bc8e
+ *	-1 if the option was unrecognised, or
258bc8e
+ *	-2 if the option was recognised, but otherwise bad in
258bc8e
+ *	   some way (e.g. invalid parameter)
258bc8e
+ *
258bc8e
+ * Note: this function sets internal compressor state, but does not
258bc8e
+ * pass back the results of the parsing other than success/failure.
258bc8e
+ * The zstd_dump_options() function is called later to get the options in
258bc8e
+ * a format suitable for writing to the filesystem.
258bc8e
+ */
258bc8e
+static int zstd_options(char *argv[], int argc)
258bc8e
+{
258bc8e
+	if (strcmp(argv[0], "-Xcompression-level") == 0) {
258bc8e
+		if (argc < 2) {
258bc8e
+			fprintf(stderr, "zstd: -Xcompression-level missing "
258bc8e
+				"compression level\n");
258bc8e
+			fprintf(stderr, "zstd: -Xcompression-level it should "
258bc8e
+				"be 1 <= n <= %d\n", ZSTD_maxCLevel());
258bc8e
+			goto failed;
258bc8e
+		}
258bc8e
+
258bc8e
+		compression_level = atoi(argv[1]);
258bc8e
+		if (compression_level < 1 ||
258bc8e
+		    compression_level > ZSTD_maxCLevel()) {
258bc8e
+			fprintf(stderr, "zstd: -Xcompression-level invalid, it "
258bc8e
+				"should be 1 <= n <= %d\n", ZSTD_maxCLevel());
258bc8e
+			goto failed;
258bc8e
+		}
258bc8e
+
258bc8e
+		return 1;
258bc8e
+	}
258bc8e
+
258bc8e
+	return -1;
258bc8e
+failed:
258bc8e
+	return -2;
258bc8e
+}
258bc8e
+
258bc8e
+/*
258bc8e
+ * This function is called by mksquashfs to dump the parsed
258bc8e
+ * compressor options in a format suitable for writing to the
258bc8e
+ * compressor options field in the filesystem (stored immediately
258bc8e
+ * after the superblock).
258bc8e
+ *
258bc8e
+ * This function returns a pointer to the compression options structure
258bc8e
+ * to be stored (and the size), or NULL if there are no compression
258bc8e
+ * options.
258bc8e
+ */
258bc8e
+static void *zstd_dump_options(int block_size, int *size)
258bc8e
+{
258bc8e
+	static struct zstd_comp_opts comp_opts;
258bc8e
+
258bc8e
+	/* don't return anything if the options are all default */
258bc8e
+	if (compression_level == ZSTD_DEFAULT_COMPRESSION_LEVEL)
258bc8e
+		return NULL;
258bc8e
+
258bc8e
+	comp_opts.compression_level = compression_level;
258bc8e
+
258bc8e
+	SQUASHFS_INSWAP_COMP_OPTS(&comp_opts);
258bc8e
+
258bc8e
+	*size = sizeof(comp_opts);
258bc8e
+	return &comp_opts;
258bc8e
+}
258bc8e
+
258bc8e
+/*
258bc8e
+ * This function is a helper specifically for the append mode of
258bc8e
+ * mksquashfs.  Its purpose is to set the internal compressor state
258bc8e
+ * to the stored compressor options in the passed compressor options
258bc8e
+ * structure.
258bc8e
+ *
258bc8e
+ * In effect this function sets up the compressor options
258bc8e
+ * to the same state they were when the filesystem was originally
258bc8e
+ * generated, this is to ensure on appending, the compressor uses
258bc8e
+ * the same compression options that were used to generate the
258bc8e
+ * original filesystem.
258bc8e
+ *
258bc8e
+ * Note, even if there are no compressor options, this function is still
258bc8e
+ * called with an empty compressor structure (size == 0), to explicitly
258bc8e
+ * set the default options, this is to ensure any user supplied
258bc8e
+ * -X options on the appending mksquashfs command line are over-ridden.
258bc8e
+ *
258bc8e
+ * This function returns 0 on sucessful extraction of options, and -1 on error.
258bc8e
+ */
258bc8e
+static int zstd_extract_options(int block_size, void *buffer, int size)
258bc8e
+{
258bc8e
+	struct zstd_comp_opts *comp_opts = buffer;
258bc8e
+
258bc8e
+	if (size == 0) {
258bc8e
+		/* Set default values */
258bc8e
+		compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
258bc8e
+		return 0;
258bc8e
+	}
258bc8e
+
258bc8e
+	/* we expect a comp_opts structure of sufficient size to be present */
258bc8e
+	if (size < sizeof(*comp_opts))
258bc8e
+		goto failed;
258bc8e
+
258bc8e
+	SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
258bc8e
+
258bc8e
+	if (comp_opts->compression_level < 1 ||
258bc8e
+	    comp_opts->compression_level > ZSTD_maxCLevel()) {
258bc8e
+		fprintf(stderr, "zstd: bad compression level in compression "
258bc8e
+			"options structure\n");
258bc8e
+		goto failed;
258bc8e
+	}
258bc8e
+
258bc8e
+	compression_level = comp_opts->compression_level;
258bc8e
+
258bc8e
+	return 0;
258bc8e
+
258bc8e
+failed:
258bc8e
+	fprintf(stderr, "zstd: error reading stored compressor options from "
258bc8e
+		"filesystem!\n");
258bc8e
+
258bc8e
+	return -1;
258bc8e
+}
258bc8e
+
258bc8e
+static void zstd_display_options(void *buffer, int size)
258bc8e
+{
258bc8e
+	struct zstd_comp_opts *comp_opts = buffer;
258bc8e
+
258bc8e
+	/* we expect a comp_opts structure of sufficient size to be present */
258bc8e
+	if (size < sizeof(*comp_opts))
258bc8e
+		goto failed;
258bc8e
+
258bc8e
+	SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
258bc8e
+
258bc8e
+	if (comp_opts->compression_level < 1 ||
258bc8e
+	    comp_opts->compression_level > ZSTD_maxCLevel()) {
258bc8e
+		fprintf(stderr, "zstd: bad compression level in compression "
258bc8e
+			"options structure\n");
258bc8e
+		goto failed;
258bc8e
+	}
258bc8e
+
258bc8e
+	printf("\tcompression-level %d\n", comp_opts->compression_level);
258bc8e
+
258bc8e
+	return;
258bc8e
+
258bc8e
+failed:
258bc8e
+	fprintf(stderr, "zstd: error reading stored compressor options from "
258bc8e
+		"filesystem!\n");
258bc8e
+}
258bc8e
+
258bc8e
+/*
258bc8e
+ * This function is called by mksquashfs to initialise the
258bc8e
+ * compressor, before compress() is called.
258bc8e
+ *
258bc8e
+ * This function returns 0 on success, and -1 on error.
258bc8e
+ */
258bc8e
+static int zstd_init(void **strm, int block_size, int datablock)
258bc8e
+{
258bc8e
+	ZSTD_CCtx *cctx = ZSTD_createCCtx();
258bc8e
+
258bc8e
+	if (!cctx) {
258bc8e
+		fprintf(stderr, "zstd: failed to allocate compression "
258bc8e
+			"context!\n");
258bc8e
+		return -1;
258bc8e
+	}
258bc8e
+
258bc8e
+	*strm = cctx;
258bc8e
+	return 0;
258bc8e
+}
258bc8e
+
258bc8e
+static int zstd_compress(void *strm, void *dest, void *src, int size,
258bc8e
+			 int block_size, int *error)
258bc8e
+{
258bc8e
+	const size_t res = ZSTD_compressCCtx((ZSTD_CCtx*)strm, dest, block_size,
258bc8e
+					     src, size, compression_level);
258bc8e
+
258bc8e
+	if (ZSTD_isError(res)) {
258bc8e
+		/* FIXME:
258bc8e
+		 * zstd does not expose stable error codes. The error enum may
258bc8e
+		 * change between versions. Until upstream zstd stablizes the
258bc8e
+		 * error codes, we have no way of knowing why the error occurs.
258bc8e
+		 * zstd shouldn't fail to compress any input unless there isn't
258bc8e
+		 * enough output space. We assume that is the cause and return
258bc8e
+		 * the special error code for not enough output space.
258bc8e
+		 */
258bc8e
+		return 0;
258bc8e
+	}
258bc8e
+
258bc8e
+	return (int)res;
258bc8e
+}
258bc8e
+
258bc8e
+static int zstd_uncompress(void *dest, void *src, int size, int outsize,
258bc8e
+			   int *error)
258bc8e
+{
258bc8e
+	const size_t res = ZSTD_decompress(dest, outsize, src, size);
258bc8e
+
258bc8e
+	if (ZSTD_isError(res)) {
258bc8e
+		fprintf(stderr, "\t%d %d\n", outsize, size);
258bc8e
+
258bc8e
+		*error = (int)ZSTD_getErrorCode(res);
258bc8e
+		return -1;
258bc8e
+	}
258bc8e
+
258bc8e
+	return (int)res;
258bc8e
+}
258bc8e
+
258bc8e
+static void zstd_usage(void)
258bc8e
+{
258bc8e
+	fprintf(stderr, "\t  -Xcompression-level <compression-level>\n");
258bc8e
+	fprintf(stderr, "\t\t<compression-level> should be 1 .. %d (default "
258bc8e
+		"%d)\n", ZSTD_maxCLevel(), ZSTD_DEFAULT_COMPRESSION_LEVEL);
258bc8e
+}
258bc8e
+
258bc8e
+struct compressor zstd_comp_ops = {
258bc8e
+	.init = zstd_init,
258bc8e
+	.compress = zstd_compress,
258bc8e
+	.uncompress = zstd_uncompress,
258bc8e
+	.options = zstd_options,
258bc8e
+	.dump_options = zstd_dump_options,
258bc8e
+	.extract_options = zstd_extract_options,
258bc8e
+	.display_options = zstd_display_options,
258bc8e
+	.usage = zstd_usage,
258bc8e
+	.id = ZSTD_COMPRESSION,
258bc8e
+	.name = "zstd",
258bc8e
+	.supported = 1
258bc8e
+};
258bc8e
diff --git a/squashfs-tools/zstd_wrapper.h b/squashfs-tools/zstd_wrapper.h
258bc8e
new file mode 100644
258bc8e
index 0000000..4fbef0a
258bc8e
--- /dev/null
258bc8e
+++ b/squashfs-tools/zstd_wrapper.h
258bc8e
@@ -0,0 +1,48 @@
258bc8e
+#ifndef ZSTD_WRAPPER_H
258bc8e
+#define ZSTD_WRAPPER_H
258bc8e
+/*
258bc8e
+ * Squashfs
258bc8e
+ *
258bc8e
+ * Copyright (c) 2017
258bc8e
+ * Phillip Lougher <phillip@squashfs.org.uk>
258bc8e
+ *
258bc8e
+ * This program is free software; you can redistribute it and/or
258bc8e
+ * modify it under the terms of the GNU General Public License
258bc8e
+ * as published by the Free Software Foundation; either version 2,
258bc8e
+ * or (at your option) any later version.
258bc8e
+ *
258bc8e
+ * This program is distributed in the hope that it will be useful,
258bc8e
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
258bc8e
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
258bc8e
+ * GNU General Public License for more details.
258bc8e
+ *
258bc8e
+ * zstd_wrapper.h
258bc8e
+ *
258bc8e
+ */
258bc8e
+
258bc8e
+#ifndef linux
258bc8e
+#define __BYTE_ORDER BYTE_ORDER
258bc8e
+#define __BIG_ENDIAN BIG_ENDIAN
258bc8e
+#define __LITTLE_ENDIAN LITTLE_ENDIAN
258bc8e
+#else
258bc8e
+#include <endian.h>
258bc8e
+#endif
258bc8e
+
258bc8e
+#if __BYTE_ORDER == __BIG_ENDIAN
258bc8e
+extern unsigned int inswap_le16(unsigned short);
258bc8e
+extern unsigned int inswap_le32(unsigned int);
258bc8e
+
258bc8e
+#define SQUASHFS_INSWAP_COMP_OPTS(s) { \
258bc8e
+	(s)->compression_level = inswap_le32((s)->compression_level); \
258bc8e
+}
258bc8e
+#else
258bc8e
+#define SQUASHFS_INSWAP_COMP_OPTS(s)
258bc8e
+#endif
258bc8e
+
258bc8e
+/* Default compression */
258bc8e
+#define ZSTD_DEFAULT_COMPRESSION_LEVEL 15
258bc8e
+
258bc8e
+struct zstd_comp_opts {
258bc8e
+	int compression_level;
258bc8e
+};
258bc8e
+#endif
258bc8e
-- 
258bc8e
2.19.1
258bc8e