Skip to contentSkip to conversation

libqblock qemu block layer library

PATCH Superseded Wenchao Xia 1 participant 6 messages Oct 15, 2012 – Oct 26, 2012 Kbuild

v8 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v8 cover libqblock qemu block layer library

These patches introduce libqblock API, make libqblock.la and make
check-libqblock could build this library. Functionalities:
1 create a new image.
2 sync access of an image.
3 basic image information retrieving such as backing file. 4 detect if a sector is allocated in an image.
Supported Formats:
ALL using file protocols.

v2:
Insert reserved bytes into union.
Use uint64_t instead of size_t, offset.
Use const char * in filename pointer.
Initialization function removed and it was automatically executed when
library is loaded.
Added compile flag visibility=hidden, to avoid name space pollution. Structure naming style changed.
Using byte unit instead of sector for every API.
Added a member in image static information structure, to report logical
sector size, which is always 512 now.
Read and write API can take request not aligned to 512 now. It returns the
byte number that have succeed in operation, but now either negative value or the number requested would be returned, because qemu block sync I/O API would not return such number.
Typo fix due to comments and improved documents.

v3:
Removed the code about OOM error, introduced GError.
Used a table to map from string to enum types about format.
Use typedef for every structure.
Improved the gcc compiler macro to warn if gcc was not used. Global variable name changed with prefix libqb_.
The struct QBlockStaticInfo was changed to folder full format related
information inside, and a new member with pointers pointing to the mostly used members, such as backing file, virt size, was added. This would allow the user to get full information about how it is created in the future.
Each patch in the serial can work with qemu now.
Typo fixes.

v4:
Renamed QBroker to QBlockContext.
Removed tool objs out of libqblock.
Added a check in initialization about structure size for ABI. Added a new helper API to duplicate protocol information, helps to open files
in a backing file chain.
Check-libqblock will not rebuild libqblock every time now.
Test case file renamed to "libqblock-[FMT].c".
Test use gtest framework now.
Test do random creation of test file now, added check for information API in
it.
Test do random sync io instead of fixed offset io now.
Test accept one parameter about where to place the test image, now it is
./tests/libqblock/test_images.

v5:
Makefile of libqblock was adjusted to be similar as libcacard, added spec
file and install section.
Removed warning when GCC was not found.
Structure names were changed to better ones.
Removed the union typedef that contain reserved bytes to reduce the folder
depth.
Some format related enum options was changed to better name. Added accessors about image static information, hide indirect accessing
member detail in the structure.
Test Makefile do not create diretory now, test case create it themself. Test build system do not use libtool now, and removed qtest-obj-y in its
dependency, make check will automatically execute test anyway now.
Removed "ifeq ($(LIBTOOL),)" in Makefile.

v6:
Remove address pointer member in image static info structure.

v7:
Support out of tree building.

v8:
Fix a bug in out of tree building.

Wenchao Xia (4):
libqblock build system
libqblock type defines
libqblock API
libqblock test example

 .gitignore                        |    2 +
 Makefile                          |   14 +-
 block.c                           |    2 +-
 block.h                           |    1 +
 configure                         |    4 +-
 libqblock/Makefile                |   72 +++
 libqblock/libqblock-error.c       |   57 ++
 libqblock/libqblock-error.h       |   49 ++
 libqblock/libqblock-internal.h    |   75 +++
 libqblock/libqblock-types.h       |  252 ++++++++
 libqblock/libqblock.c             | 1191 +++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h             |  341 +++++++++++
 libqblock/libqblock.pc.in         |   13 +
 tests/Makefile                    |   36 ++-
 tests/libqblock/libqblock-qcow2.c |  392 ++++++++++++
 15 files changed, 2496 insertions(+), 5 deletions(-)
 create mode 100644 libqblock/Makefile
 create mode 100644 libqblock/libqblock-error.c
 create mode 100644 libqblock/libqblock-error.h
 create mode 100644 libqblock/libqblock-internal.h
 create mode 100644 libqblock/libqblock-types.h
 create mode 100644 libqblock/libqblock.c
 create mode 100644 libqblock/libqblock.h
 create mode 100644 libqblock/libqblock.pc.in
 create mode 100644 tests/libqblock/libqblock-qcow2.c
#1 lore raw

PATCH v8 1/4 libqblock build system

Libqblock was placed in new directory ./libqblock, libtool will build
dynamic library there, source files of block layer remains in ./block. So block related source code will generate 3 sets of binary, first is old ones used in qemu, second and third are non PIC and PIC ones in ./libqblock.
GCC compiler flag visibility=hidden was used with special macro, to export
only symbols that was marked as PUBLIC.
For testing, make check-libqblock will build binaries and execute it, make
clean or make check-clean will delete generated binaries.
By default this library will be built and tested, out of tree building is
supported.

---
 .gitignore                        |    2 +
 Makefile                          |   14 ++++++-
 configure                         |    4 ++-
 libqblock/Makefile                |   72 +++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.pc.in         |   13 +++++++
 tests/Makefile                    |   36 ++++++++++++++++++-
 tests/libqblock/libqblock-qcow2.c |    4 ++
 7 files changed, 141 insertions(+), 4 deletions(-)
 create mode 100644 libqblock/Makefile
 create mode 100644 libqblock/libqblock-error.c
 create mode 100644 libqblock/libqblock-error.h
 create mode 100644 libqblock/libqblock-types.h
 create mode 100644 libqblock/libqblock.c
 create mode 100644 libqblock/libqblock.h
 create mode 100644 libqblock/libqblock.pc.in
 create mode 100644 tests/libqblock/libqblock-qcow2.c
.gitignore +2 -0
index bd6ba1c..76207fe 100644
@@ -93,3 +93,5 @@ cscope.*
 tags
 TAGS
 *~
+tests/libqblock/check-*
+tests/libqblock/test_images
Makefile +12 -2
index a9c22bf..87d2ff2 100644
@@ -169,6 +169,16 @@ qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
 qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
 
+######################################################################
+# Support building shared library libqblock
+libqblock.la:
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" libqblock.la,)
+
+install-libqblock:
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" install-libqblock,)
+
+###########################################################################
+
 vscclient$(EXESUF): $(libcacard-y) $(oslib-obj-y) $(trace-obj-y) $(tools-obj-y) qemu-timer-common.o libcacard/vscclient.o
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $^ $(libcacard_libs) $(LIBS),"  LINK  $@")
 
@@ -231,8 +241,8 @@ clean:
 	rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
 	rm -rf qapi-generated
 	rm -rf qga/qapi-generated
-	$(MAKE) -C tests/tcg clean
-	for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard; do \
+	$(MAKE) check-clean
+	for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard libqblock; do \
 	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
 	rm -f $$d/qemu-options.def; \
         done
configure +3 -1
index 353d788..d88c485 100755
@@ -3072,7 +3072,7 @@ tools=""
 if test "$want_tools" = "yes" ; then
   tools="qemu-img\$(EXESUF) qemu-io\$(EXESUF) $tools"
   if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then
-    tools="qemu-nbd\$(EXESUF) $tools"
+    tools="libqblock.la qemu-nbd\$(EXESUF) $tools"
   fi
 fi
 if test "$softmmu" = yes ; then
@@ -4112,12 +4112,14 @@ DIRS="$DIRS pc-bios/optionrom pc-bios/spapr-rtas"
 DIRS="$DIRS roms/seabios roms/vgabios"
 DIRS="$DIRS qapi-generated"
 DIRS="$DIRS libcacard libcacard/libcacard libcacard/trace"
+DIRS="$DIRS libqblock tests/libqblock"
 FILES="Makefile tests/tcg/Makefile qdict-test-data.txt"
 FILES="$FILES tests/tcg/cris/Makefile tests/tcg/cris/.gdbinit"
 FILES="$FILES tests/tcg/lm32/Makefile libcacard/Makefile"
 FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps"
 FILES="$FILES pc-bios/spapr-rtas/Makefile"
 FILES="$FILES roms/seabios/Makefile roms/vgabios/Makefile"
+FILES="$FILES libqblock/Makefile libqblock/libqblock.h libqblock/libqblock-error.h libqblock/libqblock-types.h"
 for bios_file in \
     $source_path/pc-bios/*.bin \
     $source_path/pc-bios/*.rom \
libqblock/Makefile new file +72 -0

72 lines not on this page · raw message

libqblock/libqblock-error.c new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-error.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock-types.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock.c new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock.h new file +0 -0
new file mode 100644
index 0000000..e69de29
libqblock/libqblock.pc.in new file +13 -0
new file mode 100644
index 0000000..d2a7d06
@@ -0,0 +1,13 @@
+prefix=@PREFIX@
+exec_prefix=${prefix}
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: qblock
+Description: QEMU block layer library
+Version: @VERSION@
+
+Requires:  rt gthread-2.0 glib-2.0 z curl cap-ng uuid
+Libs: -L${libdir} -lqblock
+Libs.private:
+Cflags: -I${includedir}
tests/Makefile +35 -1
index 26a67ce..8a1b61b 100644
@@ -84,6 +84,22 @@ check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET)
 qtest-obj-y = tests/libqtest.o $(oslib-obj-y) $(tools-obj-y)
 $(check-qtest-y): $(qtest-obj-y)
 
+#libqblock test rules
+
+LIBQBLOCK_TEST_DIR= tests/libqblock/test_images
+LIBQBLOCK_DIR = libqblock
+LIBQBLOCK_SO = $(LIBQBLOCK_DIR)/.libs/libqblock.so
+LIBQBLOCK_SO_LINK_FLAG= -Wl,-rpath,$(LIBQBLOCK_DIR)/.libs
+
+tests/libqblock/%.o: QEMU_CFLAGS:= $(subst -fPIE,-fPIC, $(QEMU_CFLAGS))
+tests/libqblock/%.o: QEMU_CFLAGS:= $(subst -DPIE,-DPIC, $(QEMU_CFLAGS))
+tests/libqblock/%.o: QEMU_INCLUDES += -I$(SRC_PATH)/tests -I$(SRC_PATH)/$(LIBQBLOCK_DIR)
+
+check-libqblock-y = tests/libqblock/check-libqblock-qcow2$(EXESUF)
+
+tests/libqblock/check-libqblock-qcow2$(EXESUF): tests/libqblock/libqblock-qcow2.o $(LIBQBLOCK_SO)
+	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) $(LIBQBLOCK_SO_LINK_FLAG),"  LINK  $@")
+
 .PHONY: check-help
 check-help:
 	@echo "Regression testing targets:"
@@ -93,7 +109,9 @@ check-help:
 	@echo " make check-qtest          Run qtest tests"
 	@echo " make check-unit           Run qobject tests"
 	@echo " make check-block          Run block tests"
+	@echo " make check-libqblock      Run libqblock tests"
 	@echo " make check-report.html    Generates an HTML test report"
+	@echo " make check-clean          Clean the test"
 	@echo
 	@echo "Please note that HTML reports do not regenerate if the unit tests"
 	@echo "has not changed."
@@ -116,6 +134,11 @@ $(patsubst %, check-qtest-%, $(QTEST_TARGETS)): check-qtest-%: $(check-qtest-y)
 $(patsubst %, check-%, $(check-unit-y)): check-%: %
 	$(call quiet-command,gtester $(GTESTER_OPTIONS) -m=$(SPEED) $*,"GTESTER $*")
 
+#libqblock tests
+.PHONY: $(patsubst %, check-%, $(check-libqblock-y))
+$(patsubst %, check-%, $(check-libqblock-y)): check-%: %
+	$(call quiet-command, LIBQBLOCK_TEST_DIR=$(LIBQBLOCK_TEST_DIR) gtester $(GTESTER_OPTIONS) -m=$(SPEED) $*,"GTESTER $*")
+
 # gtester tests with XML output
 
 $(patsubst %, check-report-qtest-%.xml, $(QTEST_TARGETS)): check-report-qtest-%.xml: $(check-qtest-y)
@@ -146,6 +169,17 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF)
 check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
 check-unit: $(patsubst %,check-%, $(check-unit-y))
 check-block: $(patsubst %,check-%, $(check-block-y))
-check: check-unit check-qtest
+check-libqblock: $(patsubst %,check-%, $(check-libqblock-y))
+check: check-unit check-qtest check-libqblock
+
+check-clean:
+	$(MAKE) -C tests/tcg clean
+	rm -f $(check-unit-y)
+	rm -f $(check-qtest-i386-y) $(check-qtest-x86_64-y) $(check-qtest-sparc64-y) $(check-qtest-sparc-y)
+	rm -f tests/*.o
+	rm -f tests/libqblock/*.o
+	rm -f $(check-libqblock-y)
+	rm -rf $(LIBQBLOCK_TEST_DIR)
+
 
 -include $(wildcard tests/*.d)
tests/libqblock/libqblock-qcow2.c new file +4 -0
new file mode 100644
index 0000000..c05c0c4
@@ -0,0 +1,4 @@
+int main(int argc, char **argv)
+{
+    return 0;
+}
Signature · 1 line
1.7.1
#3 lore raw

PATCH v8 3/4 libqblock API

In reply to Wenchao Xia

This patch contains the major APIs in the library. For ABI some reserved
members were used.
Important APIs:
1 QBlockContext. This structure was used to retrieve errors, every thread
must create one first.
2 QBlockState. It stands for an block image object.
3 QBlockStaticInfo. It contains static information such as location, backing
file, size.
4 Sync I/O. It is similar to C file open, read, write and close operations.

---
 block.c                     |    2 +-
 block.h                     |    1 +
 libqblock/libqblock-error.c |   57 ++
 libqblock/libqblock-error.h |   49 ++
 libqblock/libqblock.c       | 1191 +++++++++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h       |  341 +++++++++++++
 6 files changed, 1640 insertions(+), 1 deletions(-)
block.c +1 -1
index e95f613..ce0068a 100644
@@ -198,7 +198,7 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
 }
 
 /* check if the path starts with "<protocol>:" */
-static int path_has_protocol(const char *path)
+int path_has_protocol(const char *path)
 {
     const char *p;
 
block.h +1 -0
index e2d89d7..45f83f9 100644
@@ -426,4 +426,5 @@ typedef enum {
 #define BLKDBG_EVENT(bs, evt) bdrv_debug_event(bs, evt)
 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event);
 
+int path_has_protocol(const char *path);
 #endif
libqblock/libqblock-error.c +57 -0

57 lines not on this page · raw message

libqblock/libqblock-error.h +49 -0

49 lines not on this page · raw message

libqblock/libqblock.c +1191 -0

1,191 lines not on this page · raw message

libqblock/libqblock.h +341 -0

341 lines not on this page · raw message

Signature · 1 line
1.7.1
#4 lore raw

PATCH v8 4/4 libqblock test example

In reply to Wenchao Xia

In this example, first it will create some qcow2 images, then try get
information including backing file relationship, then it will do sync IO on the image.

---
 tests/libqblock/libqblock-qcow2.c |  390 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 389 insertions(+), 1 deletions(-)
tests/libqblock/libqblock-qcow2.c +389 -1
index c05c0c4..15e81b8 100644
@@ -1,4 +1,392 @@
+/*
+ * QEMU block layer library test
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ * Limitation:
+ *    1 filename do not support relative path, to save trouble in creating
+ * backing files.
+ */
+
+#include <glib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
+#include "libqblock.h"
+#include "libqtest.h"
+
+#define LIBQB_TEST_ENV_DIR "LIBQBLOCK_TEST_DIR"
+#define LIBQB_TEST_DEFAULT_DIR "/tmp"
+#define LIBQB_TEST_DEFAULT_FILENAME "libqblock_qcow2_test_img"
+
+typedef struct LibqbTestSettings {
+    const char *image_filename;
+    uint64_t image_size;
+    unsigned int num_backings;
+    unsigned int io_buf_size;
+    uint64_t io_offset;
+    int print_flag;
+} LibqbTestSettings;
+
+LibqbTestSettings libqb_test_settings;
+
+static void print_loc(const QBlockLocationInfo *loc)
+{
+    if (loc == NULL) {
+        printf("loc is NULL.");
+        return;
+    }
+    switch (loc->prot_type) {
+    case QB_PROTO_NONE:
+        printf("protocol type [none].");
+        break;
+    case QB_PROTO_FILE:
+        printf("protocol type [file], filename [%s].",
+               loc->o_file.filename);
+        break;
+    default:
+        printf("protocol type not supported.");
+        break;
+    }
+}
+
+static void print_info_image_static(QBlockStaticInfo *info)
+{
+    const uint64_t *virt_size = qb_get_virt_size(info);
+    const QBlockLocationInfo *backing_loc = qb_get_backing_loc(info);
+    g_assert(virt_size != NULL);
+
+    printf("=======image location:\n");
+    print_loc(&info->loc);
+    printf("\nvirtual_size %" PRId64 ", format type %d [%s]",
+           *(virt_size),
+           info->fmt.fmt_type, qb_fmttype2str(info->fmt.fmt_type));
+    printf("\nbacking image location:\n");
+    print_loc(backing_loc);
+    printf("\n");
+}
+
+static char *generate_backing_filename(const char *filename, int index)
+{
+    char *backing_filename = NULL;
+
+    backing_filename = g_strdup_printf("%s_backing_%d", filename, index);
+    return backing_filename;
+}
+
+/* get filename in a full path */
+static const char *get_filename(const char *path)
+{
+    const char *filename;
+    filename = strrchr(path, '/');
+    if (filename == NULL) {
+        filename = path;
+    } else {
+        filename++;
+    }
+    return filename;
+}
+
+/* create a chain of files, num_backings must >= 0. */
+static void files_create_qcow2(const char *filename,
+                               int num_backings,
+                               uint64_t virt_size)
+{
+    QBlockContext *context = NULL;
+    QBlockState *qbs = NULL;
+    QBlockLocationInfo *loc_info = NULL;
+    QBlockFormatInfo *fmt_info = NULL;
+    int ret;
+    int index;
+    int flag;
+    char *backing_filename = NULL, *new_filename = NULL;
+    const char *relative_filename = NULL;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_state_new(context, &qbs);
+    g_assert(ret == 0);
+
+    ret = qb_loc_info_new(context, &loc_info);
+    g_assert(ret == 0);
+
+    ret = qb_fmt_info_new(context, &fmt_info);
+    g_assert(ret == 0);
+
+    loc_info->prot_type = QB_PROTO_FILE;
+    fmt_info->fmt_type = QB_FMT_QCOW2;
+    fmt_info->o_qcow2.virt_size = virt_size;
+    flag = 0;
+
+    index = 0;
+    while (index < num_backings) {
+        new_filename = generate_backing_filename(filename, index);
+        loc_info->o_file.filename = new_filename;
+        if (backing_filename != NULL) {
+            fmt_info->o_qcow2.backing_loc.prot_type = QB_PROTO_FILE;
+            relative_filename = get_filename(backing_filename);
+            fmt_info->o_qcow2.backing_loc.o_file.filename =
+                                                             relative_filename;
+        }
+        ret = qb_create(context, qbs, loc_info, fmt_info, flag);
+        g_assert(ret == 0);
+        free(backing_filename);
+        backing_filename = new_filename;
+        new_filename = NULL;
+        index++;
+    }
+
+    loc_info->o_file.filename = filename;
+    if (backing_filename != NULL) {
+        fmt_info->o_qcow2.backing_loc.prot_type = QB_PROTO_FILE;
+        relative_filename = get_filename(backing_filename);
+        fmt_info->o_qcow2.backing_loc.o_file.filename =
+                                                         relative_filename;
+    }
+    ret = qb_create(context, qbs, loc_info, fmt_info, flag);
+    g_assert(ret == 0);
+    free(backing_filename);
+
+    qb_fmt_info_delete(context, &fmt_info);
+    qb_loc_info_delete(context, &loc_info);
+    qb_state_delete(context, &qbs);
+    qb_context_delete(&context);
+    return;
+}
+
+static void files_delete(const char *filename,
+                         int num_backings,
+                         uint64_t virt_size)
+{
+    char *new_filename = NULL;
+    int index = 0;
+    while (index < num_backings) {
+        new_filename = generate_backing_filename(filename, index);
+        unlink(new_filename);
+        free(new_filename);
+        index++;
+    }
+    unlink(filename);
+}
+
+static void testcase_info_image_static(void)
+{
+    const char *filename = libqb_test_settings.image_filename;
+    uint64_t image_size = libqb_test_settings.image_size;
+    int num_backings = libqb_test_settings.num_backings;
+    QBlockContext *context = NULL;
+    QBlockState *qbs = NULL;
+    QBlockLocationInfo *loc_info = NULL;
+    int ret, flag;
+    QBlockStaticInfo *info_st = NULL;
+    int i;
+    char *backing_filename = NULL;
+    const uint64_t *virt_size = NULL;
+    const QBlockLocationInfo *backing_loc = NULL;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_state_new(context, &qbs);
+    g_assert(ret == 0);
+
+    ret = qb_loc_info_new(context, &loc_info);
+    g_assert(ret == 0);
+
+    loc_info->prot_type = QB_PROTO_FILE;
+    loc_info->o_file.filename = filename;
+    flag = LIBQBLOCK_O_NO_BACKING;
+    ret = qb_open(context, qbs, loc_info, NULL, flag);
+    g_assert(ret == 0);
+
+    i = num_backings - 1;
+    while (1) {
+        ret = qb_info_image_static_get(context, qbs, &info_st);
+        g_assert(ret == 0);
+        if (libqb_test_settings.print_flag > 0) {
+            print_info_image_static(info_st);
+        }
+        qb_close(context, qbs);
+
+        /* checking the information */
+        g_assert(info_st->loc.prot_type == loc_info->prot_type);
+        ret = strcmp(info_st->loc.o_file.filename,
+                     loc_info->o_file.filename);
+
+        g_assert(ret == 0);
+        g_assert(info_st->fmt.fmt_type == QB_FMT_QCOW2);
+        virt_size = qb_get_virt_size(info_st);
+        g_assert(virt_size != NULL);
+        g_assert(*virt_size == image_size);
+        backing_loc = qb_get_backing_loc(info_st);
+        /* qcow2 have always backing file property */
+        g_assert(backing_loc != NULL);
+        if (i >= 0) {
+            /* it should have backing file */
+            g_assert(backing_loc->prot_type == QB_PROTO_FILE);
+            backing_filename = generate_backing_filename(filename, i);
+            ret = strcmp(backing_filename, backing_loc->o_file.filename);
+            g_assert(ret == 0);
+            g_free(backing_filename);
+        } else {
+            g_assert(backing_loc->prot_type == QB_FMT_NONE);
+        }
+        i--;
+        /* see if there is backing file need to check. */
+        if (backing_loc->prot_type == QB_FMT_NONE) {
+            g_assert(i < 0);
+            break;
+        }
+        qb_loc_info_delete(context, &loc_info);
+        loc_info = qb_loc_info_dup(backing_loc);
+        ret = qb_open(context, qbs, loc_info, NULL, flag);
+        g_assert(ret == 0);
+        qb_info_image_static_delete(context, &info_st);
+    }
+    qb_info_image_static_delete(context, &info_st);
+
+    qb_loc_info_delete(context, &loc_info);
+    qb_state_delete(context, &qbs);
+    qb_context_delete(&context);
+    return;
+}
+
+/* assuming the image is new created */
+static void testcase_sync_io(void)
+{
+    const char *filename = libqb_test_settings.image_filename;
+    uint64_t io_buf_size = libqb_test_settings.io_buf_size;
+    uint64_t io_offset = libqb_test_settings.io_offset;;
+    unsigned char *buf_r, *buf_w;
+    uint64_t i;
+    unsigned int rand_value;
+
+    buf_r = g_malloc0(io_buf_size);
+    buf_w = g_malloc0(io_buf_size);
+    for (i = 0; i < io_buf_size; i++) {
+        rand_value = g_test_rand_int_range(0, 255);
+        buf_w[i] = (unsigned char)rand_value;
+    }
+
+    QBlockContext *context = NULL;
+    QBlockState *qbs = NULL;
+    QBlockLocationInfo *loc_info = NULL;
+    int ret, flag;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_state_new(context, &qbs);
+    g_assert(ret == 0);
+
+    ret = qb_loc_info_new(context, &loc_info);
+    g_assert(ret == 0);
+
+
+    loc_info->prot_type = QB_PROTO_FILE;
+    loc_info->o_file.filename = filename;
+    flag = LIBQBLOCK_O_RDWR;
+    ret = qb_open(context, qbs, loc_info, NULL, flag);
+    g_assert(ret == 0);
+
+    int status;
+    int64_t len;
+    ret = qb_check_allocation(context, qbs, io_offset, io_buf_size,
+                                       &status, &len);
+    g_assert(ret == 0);
+    g_assert(status == 0);
+    g_assert(len == io_buf_size);
+
+    ret = qb_write(context, qbs, buf_w, io_buf_size, io_offset);
+    g_assert(ret == io_buf_size);
+
+    ret = qb_read(context, qbs, buf_r, io_buf_size, io_offset);
+    g_assert(ret == io_buf_size);
+
+    int cmp = memcmp(buf_r, buf_w, io_buf_size);
+    g_assert(cmp == 0);
+
+    ret = qb_check_allocation(context, qbs, io_offset, io_buf_size,
+                                       &status, &len);
+    g_assert(ret == 0);
+    g_assert(status == 1);
+    g_assert(len == io_buf_size);
+
+    qb_close(context, qbs);
+
+    qb_loc_info_delete(context, &loc_info);
+    qb_state_delete(context, &qbs);
+    qb_context_delete(&context);
+    g_free(buf_r);
+    g_free(buf_w);
+    return;
+}
+
 int main(int argc, char **argv)
 {
-    return 0;
+    const char *root_dir;
+    const char *name = LIBQB_TEST_DEFAULT_FILENAME;
+    int ret;
+    int dir_created = 0;
+    char *filename;
+
+    root_dir = getenv(LIBQB_TEST_ENV_DIR);
+    if (root_dir == NULL) {
+        root_dir = LIBQB_TEST_DEFAULT_DIR;
+    }
+
+    ret = access(root_dir, F_OK);
+    if (ret != 0) {
+        /*try mkdir */
+        ret = mkdir(root_dir, 0755);
+        g_assert(ret == 0);
+        dir_created = 1;
+    }
+
+    filename = g_strdup_printf("%s/%s", root_dir, name);
+
+    g_test_init(&argc, &argv, NULL);
+    g_test_rand_int();
+
+    libqb_test_settings.image_filename = filename;
+    libqb_test_settings.image_size =
+                            1024 * 1024 * g_test_rand_int_range(1, 1024);
+    libqb_test_settings.num_backings = g_test_rand_int_range(0, 10);
+    libqb_test_settings.io_buf_size = g_test_rand_int_range(1, 1024 * 1024);
+    libqb_test_settings.io_offset = g_test_rand_int_range(0,
+            libqb_test_settings.image_size - libqb_test_settings.io_buf_size);
+
+    files_create_qcow2(libqb_test_settings.image_filename,
+                       libqb_test_settings.num_backings,
+                       libqb_test_settings.image_size);
+
+    g_test_add_func("/libqblock/sync_info_image_static",
+                      testcase_info_image_static);
+    g_test_add_func("/libqblock/sync_io", testcase_sync_io);
+
+    ret = g_test_run();
+    files_delete(libqb_test_settings.image_filename,
+                 libqb_test_settings.num_backings,
+                 libqb_test_settings.image_size);
+
+    g_free(filename);
+    if (dir_created) {
+        ret = rmdir(root_dir);
+        g_assert(ret == 0);
+    }
+    return ret;
 }
Signature · 1 line
1.7.1
#2 lore raw

PATCH v8 2/4 libqblock type defines

In reply to Wenchao Xia

This patch contains type and macro defines used in APIs, one file for public
usage, one for libqblock internal usage.

---
 libqblock/libqblock-internal.h |   75 ++++++++++++
 libqblock/libqblock-types.h    |  252 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 327 insertions(+), 0 deletions(-)
 create mode 100644 libqblock/libqblock-internal.h
libqblock/libqblock-internal.h new file +75 -0
new file mode 100644
index 0000000..75c0452
@@ -0,0 +1,75 @@
+/*
+ * QEMU block layer library
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef LIBQBLOCK_INTERNAL
+#define LIBQBLOCK_INTERNAL
+
+#include "glib.h"
+
+#include "block.h"
+#include "block_int.h"
+#include "libqblock-types.h"
+
+/* this file contains defines and types used inside the library. */
+
+#define FUNC_FREE(p) g_free((p))
+#define FUNC_MALLOC(size) g_malloc((size))
+#define FUNC_CALLOC(nmemb, size) g_malloc0((nmemb)*(size))
+#define FUNC_STRDUP(p) g_strdup((p))
+
+#define CLEAN_FREE(p) { \
+        FUNC_FREE(p); \
+        (p) = NULL; \
+}
+
+/* details should be hidden to user */
+struct QBlockState {
+    BlockDriverState *bdrvs;
+    /* internal used file name now, if it is not NULL, it means
+       image was opened.
+    */
+    char *filename;
+};
+
+struct QBlockContext {
+    /* last error */
+    GError *g_error;
+    int err_ret; /* 1st level of error, the libqblock error number */
+    int err_no; /* 2nd level of error, errno what below reports */
+};
+
+/**
+ * QBlockStaticInfoAddr: a structure contains a set of pointer.
+ *
+ *    this struct contains a set of pointer pointing to some
+ *  property related to format or protocol. If a property is not available,
+ *  it will be set as NULL. User could use this to get properties directly.
+ *
+ *  @virt_size: virtual size, it is always not NULL.
+ *  @backing_loc: backing file location.
+ *  @encrypt: encryption flag.
+*/
+
+typedef struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockLocationInfo *backing_loc;
+    bool *encrypt;
+} QBlockStaticInfoAddr;
+
+#define G_LIBQBLOCK_ERROR g_libqbock_error_quark()
+
+static inline GQuark g_libqbock_error_quark(void)
+{
+    return g_quark_from_static_string("g-libqblock-error-quark");
+}
+#endif
libqblock/libqblock-types.h +252 -0

252 lines not on this page · raw message

Signature · 1 line
1.7.1
#5 lore raw
In reply to Wenchao Xia

Hi, Kevin
Could u take a look at this patch to see if anything need to be
improved, many thanks.

Quoted 112 lines

These patches introduce libqblock API, make libqblock.la and make
check-libqblock could build this library. Functionalities:
1 create a new image.
2 sync access of an image.
3 basic image information retrieving such as backing file. 4 detect if a sector is allocated in an image.
Supported Formats:
ALL using file protocols.

v2:
Insert reserved bytes into union.
Use uint64_t instead of size_t, offset.
Use const char * in filename pointer.
Initialization function removed and it was automatically executed when
library is loaded.
Added compile flag visibility=hidden, to avoid name space pollution. Structure naming style changed.
Using byte unit instead of sector for every API.
Added a member in image static information structure, to report logical
sector size, which is always 512 now.
Read and write API can take request not aligned to 512 now. It returns the
byte number that have succeed in operation, but now either negative value or the number requested would be returned, because qemu block sync I/O API would not return such number.
Typo fix due to comments and improved documents.

v3:
Removed the code about OOM error, introduced GError.
Used a table to map from string to enum types about format. Use typedef for every structure.
Improved the gcc compiler macro to warn if gcc was not used. Global variable name changed with prefix libqb_.
The struct QBlockStaticInfo was changed to folder full format related
information inside, and a new member with pointers pointing to the mostly used members, such as backing file, virt size, was added. This would allow the user to get full information about how it is created in the future.
Each patch in the serial can work with qemu now.
Typo fixes.

v4:
Renamed QBroker to QBlockContext.
Removed tool objs out of libqblock.
Added a check in initialization about structure size for ABI. Added a new helper API to duplicate protocol information, helps to open files
in a backing file chain.
Check-libqblock will not rebuild libqblock every time now.
Test case file renamed to "libqblock-[FMT].c".
Test use gtest framework now.
Test do random creation of test file now, added check for information API in
it.
Test do random sync io instead of fixed offset io now.
Test accept one parameter about where to place the test image, now it is
./tests/libqblock/test_images.

v5:
Makefile of libqblock was adjusted to be similar as libcacard, added spec
file and install section.
Removed warning when GCC was not found.
Structure names were changed to better ones.
Removed the union typedef that contain reserved bytes to reduce the folder
depth.
Some format related enum options was changed to better name. Added accessors about image static information, hide indirect accessing
member detail in the structure.
Test Makefile do not create diretory now, test case create it themself. Test build system do not use libtool now, and removed qtest-obj-y in its
dependency, make check will automatically execute test anyway now.
Removed "ifeq ($(LIBTOOL),)" in Makefile.

v6:
Remove address pointer member in image static info structure.

v7:
Support out of tree building.

v8:
Fix a bug in out of tree building.

Wenchao Xia (4):
libqblock build system
libqblock type defines
libqblock API
libqblock test example

  .gitignore                        |    2 +
  Makefile                          |   14 +-
  block.c                           |    2 +-
  block.h                           |    1 +
  configure                         |    4 +-
  libqblock/Makefile                |   72 +++
  libqblock/libqblock-error.c       |   57 ++
  libqblock/libqblock-error.h       |   49 ++
  libqblock/libqblock-internal.h    |   75 +++
  libqblock/libqblock-types.h       |  252 ++++++++
  libqblock/libqblock.c             | 1191 +++++++++++++++++++++++++++++++++++++
  libqblock/libqblock.h             |  341 +++++++++++
  libqblock/libqblock.pc.in         |   13 +
  tests/Makefile                    |   36 ++-
  tests/libqblock/libqblock-qcow2.c |  392 ++++++++++++
  15 files changed, 2496 insertions(+), 5 deletions(-)

create mode 100644 libqblock/Makefile create mode 100644 libqblock/libqblock-error.c create mode 100644 libqblock/libqblock-error.h create mode 100644 libqblock/libqblock-internal.h create mode 100644 libqblock/libqblock-types.h create mode 100644 libqblock/libqblock.c create mode 100644 libqblock/libqblock.h create mode 100644 libqblock/libqblock.pc.in create mode 100644 tests/libqblock/libqblock-qcow2.c

Signature · 3 lines
Best Regards

Wenchao Xia