Linux Modules
 help / color / mirror / Atom feed
From: Lucas De Marchi <lucas.de.marchi@gmail.com>
To: linux-modules@vger.kernel.org
Cc: Luis Chamberlain <mcgrof@kernel.org>,
	Lucas De Marchi <lucas.de.marchi@gmail.com>
Subject: [PATCH 3/5] libkmod: Keep track of compression type
Date: Thu,  1 Jun 2023 15:39:59 -0700	[thread overview]
Message-ID: <20230601224001.23397-4-lucas.de.marchi@gmail.com> (raw)
In-Reply-To: <20230601224001.23397-1-lucas.de.marchi@gmail.com>

Do not only set the type as direct, but also keep track of the
compression being used. This will allow using the in-kernel compression
in future.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
---
 libkmod/libkmod-file.c     | 27 +++++++++++++++------------
 libkmod/libkmod-internal.h |  7 +++++++
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
index 1449c41..705770a 100644
--- a/libkmod/libkmod-file.c
+++ b/libkmod/libkmod-file.c
@@ -58,7 +58,7 @@ struct kmod_file {
 	gzFile gzf;
 #endif
 	int fd;
-	bool direct;
+	enum kmod_file_compression_type compression;
 	off_t size;
 	void *memory;
 	const struct file_ops *ops;
@@ -376,19 +376,20 @@ static const char magic_zlib[] = {0x1f, 0x8b};
 
 static const struct comp_type {
 	size_t magic_size;
+	enum kmod_file_compression_type compression;
 	const char *magic_bytes;
 	const struct file_ops ops;
 } comp_types[] = {
 #ifdef ENABLE_ZSTD
-	{sizeof(magic_zstd), magic_zstd, {load_zstd, unload_zstd}},
+	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, {load_zstd, unload_zstd}},
 #endif
 #ifdef ENABLE_XZ
-	{sizeof(magic_xz), magic_xz, {load_xz, unload_xz}},
+	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, {load_xz, unload_xz}},
 #endif
 #ifdef ENABLE_ZLIB
-	{sizeof(magic_zlib), magic_zlib, {load_zlib, unload_zlib}},
+	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, {load_zlib, unload_zlib}},
 #endif
-	{0, NULL, {NULL, NULL}}
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, {NULL, NULL}}
 };
 
 static int load_reg(struct kmod_file *file)
@@ -403,7 +404,7 @@ static int load_reg(struct kmod_file *file)
 			    file->fd, 0);
 	if (file->memory == MAP_FAILED)
 		return -errno;
-	file->direct = true;
+
 	return 0;
 }
 
@@ -448,7 +449,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 			magic_size_max = itr->magic_size;
 	}
 
-	file->direct = false;
 	if (magic_size_max > 0) {
 		char *buf = alloca(magic_size_max + 1);
 		ssize_t sz;
@@ -468,15 +468,18 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		}
 
 		for (itr = comp_types; itr->ops.load != NULL; itr++) {
-			if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0)
+			if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
+				file->ops = &itr->ops;
+				file->compression = itr->compression;
 				break;
+			}
 		}
-		if (itr->ops.load != NULL)
-			file->ops = &itr->ops;
 	}
 
-	if (file->ops == NULL)
+	if (file->ops == NULL) {
 		file->ops = &reg_ops;
+		file->compression = KMOD_FILE_COMPRESSION_NONE;
+	}
 
 	file->ctx = ctx;
 
@@ -512,7 +515,7 @@ off_t kmod_file_get_size(const struct kmod_file *file)
 
 bool kmod_file_get_direct(const struct kmod_file *file)
 {
-	return file->direct;
+	return file->compression == KMOD_FILE_COMPRESSION_NONE;
 }
 
 int kmod_file_get_fd(const struct kmod_file *file)
diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h
index 4799ed5..7b8a158 100644
--- a/libkmod/libkmod-internal.h
+++ b/libkmod/libkmod-internal.h
@@ -61,6 +61,13 @@ struct kmod_list {
 	void *data;
 };
 
+enum kmod_file_compression_type {
+	KMOD_FILE_COMPRESSION_NONE = 0,
+	KMOD_FILE_COMPRESSION_ZSTD,
+	KMOD_FILE_COMPRESSION_XZ,
+	KMOD_FILE_COMPRESSION_ZLIB,
+};
+
 struct kmod_list *kmod_list_append(struct kmod_list *list, const void *data) _must_check_ __attribute__((nonnull(2)));
 struct kmod_list *kmod_list_prepend(struct kmod_list *list, const void *data) _must_check_ __attribute__((nonnull(2)));
 struct kmod_list *kmod_list_remove(struct kmod_list *list) _must_check_;
-- 
2.40.1


  parent reply	other threads:[~2023-06-01 22:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-01 22:39 [PATCH 0/5] libkmod: Use kernel decompression support Lucas De Marchi
2023-06-01 22:39 ` [PATCH 1/5] libkmod: Do not inititialize file->memory on open Lucas De Marchi
2023-06-06 18:24   ` Luis Chamberlain
2023-06-01 22:39 ` [PATCH 2/5] libkmod: Extract finit_module vs init_module paths Lucas De Marchi
2023-06-06 18:27   ` Luis Chamberlain
2023-06-01 22:39 ` Lucas De Marchi [this message]
2023-06-06 18:28   ` [PATCH 3/5] libkmod: Keep track of compression type Luis Chamberlain
2023-06-01 22:40 ` [PATCH 4/5] libkmod: Keep track of in-kernel compression support Lucas De Marchi
2023-06-06 18:29   ` Luis Chamberlain
2023-06-06 18:30   ` Luis Chamberlain
2023-06-01 22:40 ` [PATCH 5/5] libkmod: Use kernel decompression when available Lucas De Marchi
2023-06-06 18:38   ` Luis Chamberlain
2023-06-06 19:01     ` Lucas De Marchi
2023-07-24 13:28 ` [PATCH 0/5] libkmod: Use kernel decompression support Lucas De Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230601224001.23397-4-lucas.de.marchi@gmail.com \
    --to=lucas.de.marchi@gmail.com \
    --cc=linux-modules@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox