Skip to contentSkip to conversation

block: Fix libbz2 library dependency regresssion

PATCH Superseded Fam Zheng 3 participants 9 messages Aug 17, 2016 – Sep 5, 2016 Block layer

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v3 cover block: Fix libbz2 library dependency regresssion

v3: Fix typo in copyright header. [Max]
Fix pre-existing type casting. [Max]

v2: Rebase on top of Max's block-next tree, which has Colin's patches to
dynamically load block modules.
Two more tweaks to the module system is added.

v1 was submitted as a single patch:

https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg00508.html

Fam Zheng (3):
scripts: Allow block module to not define BlockDriver
module: Don't load the same module if requested multiple times
dmg: Move libbz2 code to dmg-bz2.so

 block/Makefile.objs             |  3 +-
 block/dmg-bz2.c                 | 62 ++++++++++++++++++++++++++++++++++++
 block/dmg.c                     | 69 ++++++++++-------------------------------
 block/dmg.h                     | 59 +++++++++++++++++++++++++++++++++++
 scripts/modules/module_block.py |  5 ---
 util/module.c                   | 19 ++++++++++--
 6 files changed, 157 insertions(+), 60 deletions(-)
 create mode 100644 block/dmg-bz2.c
 create mode 100644 block/dmg.h
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v3 1/3 scripts: Allow block module to not define BlockDriver

---
 scripts/modules/module_block.py | 5 -----
 1 file changed, 5 deletions(-)
scripts/modules/module_block.py +0 -5
index db4fb54..7efec00 100644
@@ -56,11 +56,6 @@ def process_file(fheader, filename):
                 format_name = ""
                 protocol_name = ""
 
-        if not found_something:
-            print("No BlockDriver struct found in " + filename + ". \
-                    Is this really a module?", file=sys.stderr)
-            sys.exit(1)
-
 def print_top(fheader):
     fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
 /*
Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.8 v3 1/3] scripts: Allow block module to not define BlockDriver

Quoted 10 lines · Fam Zheng
---
 scripts/modules/module_block.py | 5 -----
 1 file changed, 5 deletions(-)
scripts/modules/module_block.py +0 -0
index db4fb54..7efec00 100644

scripts/modules/module_block.py def process_file(fheader, filename): · line 62

Quoted 7 lines
scripts/modules/module_block.py +0 -4
@@ -56,11 +56,6 @@ def process_file(fheader, filename):
                 format_name = ""
                 protocol_name = ""
 
-        if not found_something:
-            print("No BlockDriver struct found in " + filename + ". \
-                    Is this really a module?", file=sys.stderr)
-            sys.exit(1)

found_something is now unused. Do you want to remove it?

Max

Quoted 5 lines
scripts/modules/module_block.py +0 -1
-
 def print_top(fheader):
     fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
 /*
author
#3 lore raw

PATCH v3 2/3 module: Don't load the same module if requested multiple times

In reply to Fam Zheng

Use a hash table to keep record of all loaded modules, and return early if the requested module is already loaded.

---
 util/module.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
util/module.c +17 -2
index a5f7fbd..63efad6 100644
@@ -163,14 +163,29 @@ void module_load_one(const char *prefix, const char *lib_name)
     char *fname = NULL;
     char *exec_dir;
     char *dirs[3];
+    char *module_name;
     int i = 0;
     int ret;
+    static GHashTable *loaded_modules;
 
     if (!g_module_supported()) {
         fprintf(stderr, "Module is not supported by system.\n");
         return;
     }
 
+    if (!loaded_modules) {
+        loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
+    }
+
+    module_name = g_strdup_printf("%s%s", prefix, lib_name);
+
+    if (g_hash_table_lookup(loaded_modules, module_name)) {
+        fprintf(stderr, "module is already loaded: %s\n", module_name);
+        g_free(module_name);
+        return;
+    }
+    g_hash_table_insert(loaded_modules, module_name, module_name);
+
     exec_dir = qemu_get_exec_dir();
     dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
     dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : "");
@@ -180,8 +195,8 @@ void module_load_one(const char *prefix, const char *lib_name)
     exec_dir = NULL;
 
     for (i = 0; i < ARRAY_SIZE(dirs); i++) {
-        fname = g_strdup_printf("%s/%s%s%s",
-                dirs[i], prefix, lib_name, HOST_DSOSUF);
+        fname = g_strdup_printf("%s/%s%s",
+                dirs[i], module_name, HOST_DSOSUF);
         ret = module_load_file(fname);
         g_free(fname);
         fname = NULL;
Signature · 1 line
2.7.4
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.8 v3 2/3] module: Don't load the same module if requested multiple times

Quoted 24 lines · Fam Zheng

Use a hash table to keep record of all loaded modules, and return early if the requested module is already loaded.

---
 util/module.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
util/module.c +2 -0
index a5f7fbd..63efad6 100644
@@ -163,14 +163,29 @@ void module_load_one(const char *prefix, const char *lib_name)
     char *fname = NULL;
     char *exec_dir;
     char *dirs[3];
+    char *module_name;
     int i = 0;
     int ret;
+    static GHashTable *loaded_modules;
 
     if (!g_module_supported()) {
         fprintf(stderr, "Module is not supported by system.\n");
         return;

util/module.c void module_load_one(const char *prefix, const char *lib_name) · line 183

Quoted 10 lines
util/module.c +8 -0
     }
 
+    if (!loaded_modules) {
+        loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
+    }
+
+    module_name = g_strdup_printf("%s%s", prefix, lib_name);
+
+    if (g_hash_table_lookup(loaded_modules, module_name)) {
+        fprintf(stderr, "module is already loaded: %s\n", module_name);

I'm not quite happy with this warning message. Loading a module is automatically initiated by internal code in qemu, i.e. never done by the user. Therefore, printing a message for the user does not make much sense to me since the user cannot do anything about this.

If it is truly wrong to attempt to load a module more than once, this should be an assertion.

However, I think it's perfectly fine to just allow qemu code to try to load a module more than once and just ignore the request if we've already loaded the module (as the commit message implies). In this case, we don't need an error message or warning, though.

Max

Quoted 20 lines
util/module.c +7 -2
+        g_free(module_name);
+        return;
+    }
+    g_hash_table_insert(loaded_modules, module_name, module_name);
+
     exec_dir = qemu_get_exec_dir();
     dirs[i++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
     dirs[i++] = g_strdup_printf("%s/..", exec_dir ? : "");
@@ -180,8 +195,8 @@ void module_load_one(const char *prefix, const char *lib_name)
     exec_dir = NULL;
 
     for (i = 0; i < ARRAY_SIZE(dirs); i++) {
-        fname = g_strdup_printf("%s/%s%s%s",
-                dirs[i], prefix, lib_name, HOST_DSOSUF);
+        fname = g_strdup_printf("%s/%s%s",
+                dirs[i], module_name, HOST_DSOSUF);
         ret = module_load_file(fname);
         g_free(fname);
         fname = NULL;
author
#5 lore raw

Re: [Qemu-devel] [PATCH for-2.8 v3 2/3] module: Don't load the same module if requested multiple times

Quoted 41 lines · Max Reitz
Quoted 24 lines · Fam Zheng

Use a hash table to keep record of all loaded modules, and return early if the requested module is already loaded.

---
 util/module.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
util/module.c +2 -0
index a5f7fbd..63efad6 100644
@@ -163,14 +163,29 @@ void module_load_one(const char *prefix, const char *lib_name)
     char *fname = NULL;
     char *exec_dir;
     char *dirs[3];
+    char *module_name;
     int i = 0;
     int ret;
+    static GHashTable *loaded_modules;
 
     if (!g_module_supported()) {
         fprintf(stderr, "Module is not supported by system.\n");
         return;

util/module.c void module_load_one(const char *prefix, const char *lib_name) · line 183

Quoted 10 lines
util/module.c +8 -0
     }
 
+    if (!loaded_modules) {
+        loaded_modules = g_hash_table_new(g_str_hash, g_str_equal);
+    }
+
+    module_name = g_strdup_printf("%s%s", prefix, lib_name);
+
+    if (g_hash_table_lookup(loaded_modules, module_name)) {
+        fprintf(stderr, "module is already loaded: %s\n", module_name);

I'm not quite happy with this warning message. Loading a module is automatically initiated by internal code in qemu, i.e. never done by the user. Therefore, printing a message for the user does not make much sense to me since the user cannot do anything about this.

Quoted 8 lines

If it is truly wrong to attempt to load a module more than once, this should be an assertion.

However, I think it's perfectly fine to just allow qemu code to try to load a module more than once and just ignore the request if we've already loaded the module (as the commit message implies). In this case, we don't need an error message or warning, though.

Agreed. I'll remove the warning. Thanks!

Fam

author
#6 lore raw

PATCH v3 3/3 dmg: Move libbz2 code to dmg-bz2.so

In reply to Fam Zheng

dmg.o was moved to block-obj-m in 5505e8b76 to become a separate module, so that its reference to libbz2, since 6b383c08c, doesn't add an extra library to the main executable.

Until recently, commit 06e60f70a (blockdev: Add dynamic module loading for block drivers) moved it back to block-obj-y to simplify the design of dynamic loading of block modules. But we don't want to lose the feature of less library dependency on the main executable.

The solution here is to move only the bz2 related code to a separate DSO file, and load it when dmg_open is called.

dmg_probe doesn't depend on bz2 support to work, and is the only code in this file which can run before dmg_open.

While we are at it, fix the unhelpful cast of last argument passed to dmg_uncompress_bz2.

---
 block/Makefile.objs |  3 ++-
 block/dmg-bz2.c     | 62 +++++++++++++++++++++++++++++++++++++++++++++++
 block/dmg.c         | 69 +++++++++++++----------------------------------------
 block/dmg.h         | 59 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+), 53 deletions(-)
 create mode 100644 block/dmg-bz2.c
 create mode 100644 block/dmg.h
block/Makefile.objs +2 -1
index fa4d8b8..141dec5 100644
@@ -40,6 +40,7 @@ gluster.o-libs     := $(GLUSTERFS_LIBS)
 ssh.o-cflags       := $(LIBSSH2_CFLAGS)
 ssh.o-libs         := $(LIBSSH2_LIBS)
 archipelago.o-libs := $(ARCHIPELAGO_LIBS)
-dmg.o-libs         := $(BZIP2_LIBS)
+block-obj-$(if $(CONFIG_BZIP2),m,n) += dmg-bz2.o
+dmg-bz2.o-libs     := $(BZIP2_LIBS)
 qcow.o-libs        := -lz
 linux-aio.o-libs   := -laio
block/dmg-bz2.c new file +62 -0

62 lines not on this page · raw message

block/dmg.c +17 -52

115 lines not on this page · raw message

block/dmg.h new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.7.4
#7 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH for-2.8 v3 3/3] dmg: Move libbz2 code to dmg-bz2.so

Quoted 4 lines · Fam Zheng
block/dmg-bz2.c new file +4 -0
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "dmg.h"
+#include <bzlib.h>

...

Quoted 5 lines
block/dmg.h new file +0 -0
new file mode 100644
index 0000000..b592d6f

...

block/dmg.h line 32

Quoted 4 lines
block/dmg.h new file +4 -0
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "block/block_int.h"
+#include <zlib.h>

If you respin please put system headers before user headers.

That way it's clear that user headers don't define anything that affects the behavior of system headers.

Reviewed-by on the v3 series
#8 lore raw
In reply to Fam Zheng
Quoted 16 lines · Fam Zheng

v3: Fix typo in copyright header. [Max]
Fix pre-existing type casting. [Max]

v2: Rebase on top of Max's block-next tree, which has Colin's patches to
dynamically load block modules.
Two more tweaks to the module system is added.

v1 was submitted as a single patch:

https://lists.gnu.org/archive/html/qemu-devel/2016-08/msg00508.html

Fam Zheng (3):
scripts: Allow block module to not define BlockDriver
module: Don't load the same module if requested multiple times
dmg: Move libbz2 code to dmg-bz2.so

Quoted 14 lines
 block/Makefile.objs             |  3 +-
 block/dmg-bz2.c                 | 62 ++++++++++++++++++++++++++++++++++++
 block/dmg.c                     | 69 ++++++++++-------------------------------
 block/dmg.h                     | 59 +++++++++++++++++++++++++++++++++++
 scripts/modules/module_block.py |  5 ---
 util/module.c                   | 19 ++++++++++--
 6 files changed, 157 insertions(+), 60 deletions(-)
 create mode 100644 block/dmg-bz2.c
 create mode 100644 block/dmg.h
Signature · 1 line
2.7.4