Skip to contentSkip to conversation

libqblock qemu block layer library

PATCH Superseded Wenchao Xia 3 participants 11 messages Sep 29, 2012 – Oct 5, 2012 Kbuild

v5 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v5 cover libqblock qemu block layer library

This patch 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.

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

 .gitignore                        |    2 +
 Makefile                          |   13 +-
 block.c                           |    2 +-
 block.h                           |    1 +
 libqblock/Makefile                |   72 +++
 libqblock/libqblock-error.c       |   57 ++
 libqblock/libqblock-error.h       |   49 ++
 libqblock/libqblock-internal.h    |   75 +++
 libqblock/libqblock-types.h       |  256 ++++++++
 libqblock/libqblock.c             | 1183 +++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h             |  341 +++++++++++
 libqblock/libqblock.pc.in         |   13 +
 tests/Makefile                    |   36 ++-
 tests/libqblock/libqblock-qcow2.c |  392 ++++++++++++
 14 files changed, 2488 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-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
#2 lore raw

PATCH v5 2/5 libqblock type defines

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

---
 libqblock/libqblock-internal.h |   75 ++++++++++++
 libqblock/libqblock-types.h    |  256 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 331 insertions(+), 0 deletions(-)
libqblock/libqblock-internal.h +75 -0
index e69de29..52f567a 100644
@@ -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.
+*/
+
+struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockLocationInfo *backing_loc;
+    bool *encrypt;
+};
+
+#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 +256 -0

256 lines not on this page · raw message

Signature · 1 line
1.7.1
#1 lore raw

PATCH v5 1/5 libqblock build system

In reply to Wenchao Xia

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.

---
 Makefile                       |   11 +++++-
 libqblock/Makefile             |   72 ++++++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.pc.in      |   13 +++++++
 3 files changed, 95 insertions(+), 1 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
Makefile +10 -1
index 0464297..f16ef99 100644
@@ -169,6 +169,15 @@ 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  $@")
 
@@ -232,7 +241,7 @@ clean:
 	rm -rf qapi-generated
 	rm -rf qga/qapi-generated
 	$(MAKE) -C tests/tcg clean
-	for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard; do \
+	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
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-internal.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}
Signature · 1 line
1.7.1
#5 lore raw

PATCH v5 4/5 libqblock test build system

In reply to Wenchao Xia

This patch added build system for testing libqblock, make check-libqblock
will build and test it, make clean or make check-clean will delete generated binaries.

---
 .gitignore                        |    2 ++
 Makefile                          |    2 +-
 tests/Makefile                    |   36 +++++++++++++++++++++++++++++++++++-
 tests/libqblock/libqblock-qcow2.c |    4 ++++
 4 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 tests/libqblock/libqblock-qcow2.c
.gitignore +2 -0
index 824c0d2..080a88b 100644
@@ -95,3 +95,5 @@ cscope.*
 tags
 TAGS
 *~
+tests/libqblock/check-*
+tests/libqblock/test_images
Makefile +1 -1
index f16ef99..49f9bb0 100644
@@ -240,7 +240,7 @@ clean:
 	rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
 	rm -rf qapi-generated
 	rm -rf qga/qapi-generated
-	$(MAKE) -C tests/tcg clean
+	$(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; \
tests/Makefile +35 -1
index 26a67ce..3afb7b8 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=$(SRC_PATH)/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 += -Itests -I$(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..58fe692
@@ -0,0 +1,4 @@
+int main(void)
+{
+    return 0;
+}
Signature · 1 line
1.7.1
#6 lore raw

PATCH v5 5/5 libqblock test example

In reply to Wenchao Xia

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

---
 tests/libqblock/libqblock-qcow2.c |  392 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 390 insertions(+), 2 deletions(-)
tests/libqblock/libqblock-qcow2.c +390 -2
index 58fe692..15e81b8 100644
@@ -1,4 +1,392 @@
-int main(void)
+/*
+ * 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)
 {
-    return 0;
+    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)
+{
+    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
#3 lore raw

PATCH v5 3/5 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       | 1183 +++++++++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h       |  341 +++++++++++++
 6 files changed, 1632 insertions(+), 1 deletions(-)
block.c +1 -1
index 751ebdc..24ae396 100644
@@ -196,7 +196,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 b1095d8..de6818f 100644
@@ -423,4 +423,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 +1183 -0

1,183 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

Re: [Qemu-devel] [PATCH V5 3/5] libqblock API

Il 29/09/2012 10:13, Wenchao Xia ha scritto:

Quoted 50 lines
libqblock/libqblock.c +50 -0
+static void qb_setup_info_addr(QBlockStaticInfo *info,
+                               QBlockStaticInfoAddr *info_addr)
+{
+    uint64_t *virt_size = NULL;
+    QBlockLocationInfo *backing_loc = NULL;
+    bool *encrypt = NULL;
+    QBlockFormatInfo *fmt = &(info->fmt);
+
+    switch (fmt->fmt_type) {
+    case QB_FMT_COW:
+        virt_size = &(fmt->o_cow.virt_size);
+        backing_loc = &(fmt->o_cow.backing_loc);
+        break;
+    case QB_FMT_QED:
+        virt_size = &(fmt->o_qed.virt_size);
+        backing_loc = &(fmt->o_qed.backing_loc);
+        break;
+    case QB_FMT_QCOW:
+        virt_size = &(fmt->o_qcow.virt_size);
+        backing_loc = &(fmt->o_qcow.backing_loc);
+        encrypt = &(fmt->o_qcow.encrypt);
+        break;
+    case QB_FMT_QCOW2:
+        virt_size = &(fmt->o_qcow2.virt_size);
+        backing_loc = &(fmt->o_qcow2.backing_loc);
+        encrypt = &(fmt->o_qcow2.encrypt);
+        break;
+    case QB_FMT_RAW:
+        virt_size = &(fmt->o_raw.virt_size);
+        break;
+    case QB_FMT_RBD:
+        virt_size = &(fmt->o_rbd.virt_size);
+        break;
+    case QB_FMT_SHEEPDOG:
+        virt_size = &(fmt->o_sd.virt_size);
+        backing_loc = &(fmt->o_sd.backing_loc);
+        break;
+    case QB_FMT_VDI:
+        virt_size = &(fmt->o_vdi.virt_size);
+        break;
+    case QB_FMT_VMDK:
+        virt_size = &(fmt->o_vmdk.virt_size);
+        backing_loc = &(fmt->o_vmdk.backing_loc);
+        break;
+    case QB_FMT_VPC:
+        virt_size = &(fmt->o_vpc.virt_size);
+        break;
+    default:
+        break;
+    }

libqblock/libqblock.c line 1030

Quoted 10 lines
libqblock/libqblock.c +10 -0
+
+    info_addr->virt_size = virt_size;
+    info_addr->backing_loc = backing_loc;
+    info_addr->encrypt = encrypt;
+    return;
+}
+
+const uint64_t *qb_get_virt_size(const QBlockStaticInfo *info)
+{
+    return info->member_addr->virt_size;

Please change this to:

     QBlockStaticInfoAddr addrs;
     qb_setup_info_addr(info, &addrs);
     return *addrs->virt_size;

and similarly for the others.

QBlockStaticInfoAddr should not be a public struct. I'm sorry if this wasn't clear.

I'll review the rest on Monday.

Paolo

Quoted 12 lines
libqblock/libqblock.c +12 -0
+}
+
+const QBlockLocationInfo *qb_get_backing_loc(const QBlockStaticInfo *info)
+{
+    return info->member_addr->backing_loc;
+}
+
+const bool *qb_get_encrypt(const QBlockStaticInfo *info)
+{
+    return info->member_addr->encrypt;
+}
+
#7 lore raw

Re: [Qemu-devel] [PATCH V5 5/5] libqblock test example

In reply to Wenchao Xia
Quoted 10 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+const uint64_t *qb_get_virt_size(const QBlockStaticInfo *info)
+{
+    return info->member_addr->virt_size;

Please change this to:

    QBlockStaticInfoAddr addrs;
    qb_setup_info_addr(info, &addrs);
    return *addrs->virt_size;
Quoted 9 lines

and similarly for the others.

QBlockStaticInfoAddr should not be a public struct. I'm sorry if this wasn't clear.

I'll review the rest on Monday.

Paolo

Hi, QBlockStaticInfoAddr was declared as a pointer, user can't see
what it really is. It is actually defined in internal header files.

#8 lore raw

Re: [Qemu-devel] [PATCH V5 5/5] libqblock test example

Il 03/10/2012 13:31, wenchao xia ha scritto:

Quoted 15 lines
Quoted 15 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+const uint64_t *qb_get_virt_size(const QBlockStaticInfo *info)
+{
+    return info->member_addr->virt_size;

Please change this to:

    QBlockStaticInfoAddr addrs;
    qb_setup_info_addr(info, &addrs);
    return *addrs->virt_size;

and similarly for the others.

QBlockStaticInfoAddr should not be a public struct. I'm sorry if this wasn't clear.

Quoted 6 lines
Quoted 4 lines

I'll review the rest on Monday.

Paolo

Hi, QBlockStaticInfoAddr was declared as a pointer, user can't see
what it really is. It is actually defined in internal header files.

Yes, but even the type should be hidden. It should be purely an implementation detail, at least for now, to hide the switch in a single function.

Paolo

#9 lore raw

Re: [Qemu-devel] [PATCH V5 5/5] libqblock test example

Quoted 20 lines · Paolo Bonzini

Il 03/10/2012 13:31, wenchao xia ha scritto:

Quoted 19 lines
Quoted 19 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+const uint64_t *qb_get_virt_size(const QBlockStaticInfo *info)
+{
+    return info->member_addr->virt_size;

Please change this to:

     QBlockStaticInfoAddr addrs;
     qb_setup_info_addr(info, &addrs);
     return *addrs->virt_size;

and similarly for the others.

QBlockStaticInfoAddr should not be a public struct. I'm sorry if this wasn't clear.

I'll review the rest on Monday.

Paolo

Quoted 9 lines
Quoted 2 lines

Hi, QBlockStaticInfoAddr was declared as a pointer, user can't see
what it really is. It is actually defined in internal header files.

Yes, but even the type should be hidden. It should be purely an implementation detail, at least for now, to hide the switch in a single function.

Paolo

OK, then I'll remove it. Do you think there is other things need to
be modified?

#10 lore raw

Re: [Qemu-devel] [PATCH V5 5/5] libqblock test example

Il 05/10/2012 13:20, wenchao xia ha scritto:

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Hi, QBlockStaticInfoAddr was declared as a pointer, user can't see
what it really is. It is actually defined in internal header files.

Yes, but even the type should be hidden. It should be purely an implementation detail, at least for now, to hide the switch in a single function.

OK, then I'll remove it. Do you think there is other things need to be modified?

I don't recall anything.

Paolo