Skip to contentSkip to conversation

libqblock qemu block layer library

PATCH Superseded Wenchao Xia 2 participants 21 messages Sep 27, 2012 – Sep 28, 2012 Kbuild

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v4 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.

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

 .gitignore                        |    2 +
 Makefile                          |   15 +-
 Makefile.objs                     |    6 +
 block.c                           |    2 +-
 block.h                           |    1 +
 libqblock/Makefile                |   56 ++
 libqblock/libqblock-error.c       |   57 ++
 libqblock/libqblock-error.h       |   49 ++
 libqblock/libqblock-internal.h    |   57 ++
 libqblock/libqblock-types.h       |  268 +++++++++
 libqblock/libqblock.c             | 1165 +++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h             |  308 ++++++++++
 tests/Makefile                    |   45 ++
 tests/libqblock/libqblock-qcow2.c |  368 ++++++++++++
 14 files changed, 2396 insertions(+), 3 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 tests/libqblock/libqblock-qcow2.c
#1 lore raw

PATCH v4 1/5 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.

---
 Makefile                    |   14 +++++++++-
 Makefile.objs               |    6 ++++
 libqblock/Makefile          |   56 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 2 deletions(-)
 create mode 100644 libqblock/Makefile
 create mode 100644 libqblock/libqblock-error.c
 create mode 100644 libqblock/libqblock.c
Makefile +12 -2
index def2ae2..128bc6a 100644
@@ -164,6 +164,17 @@ 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
+ifeq ($(LIBTOOL),)
+$(libqblock-lib-la):
+	@echo "libtool is missing, please install and rerun configure"; exit 1
+else
+$(libqblock-lib-la):
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" $(libqblock-lib-la),)
+endif
+###########################################################################
+
 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  $@")
 
@@ -226,8 +237,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
-	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
Makefile.objs +6 -0
index 4412757..8a4c9fc 100644
@@ -248,3 +248,9 @@ nested-vars += \
 	common-obj-y \
 	extra-obj-y
 dummy := $(call unnest-vars)
+
+#############################################################################
+# libqblock
+
+libqblock-lib-la = libqblock.la
+libqblock-lib-path = libqblock
libqblock/Makefile new file +56 -0

56 lines not on this page · raw message

libqblock/libqblock-error.c 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
Signature · 1 line
1.7.1
#2 lore raw

Re: [Qemu-devel] [PATCH V4 1/5] libqblock build system

Il 27/09/2012 04:23, Wenchao Xia ha scritto:

Quoted 24 lines

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                    |   14 +++++++++-
 Makefile.objs               |    6 ++++
 libqblock/Makefile          |   56 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 2 deletions(-)
 create mode 100644 libqblock/Makefile
 create mode 100644 libqblock/libqblock-error.c
 create mode 100644 libqblock/libqblock.c
Makefile +0 -0
index def2ae2..128bc6a 100644
@@ -164,6 +164,17 @@ qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
 qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o

Makefile qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y) · line 175

Quoted 10 lines
Makefile +9 -0
 
+######################################################################
+# Support building shared library libqblock
+ifeq ($(LIBTOOL),)
+$(libqblock-lib-la):
+	@echo "libtool is missing, please install and rerun configure"; exit 1
+else
+$(libqblock-lib-la):
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" $(libqblock-lib-la),)
+endif

Please remove the useless indirection via $(libqblock-lib-la). In general, this patch should be redone more similar to how libcacard is build:

subdir-libcacard: $(oslib-obj-y) $(trace-obj-y) qemu-timer-common.o
...
libcacard.la: $(oslib-obj-y) qemu-timer-common.o $(trace-obj-y)

        $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libcacard V="$(V)" TARGET_DIR="$*/" libcacard.la,)

install-libcacard: libcacard.la

        $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libcacard V="$(V)" TARGET_DIR="$*/" install-libcacard,)

(The ifeq is not necessary, I'm going to remove it from libcacard too. The same error message is already raised in libcacard/Makefile).

Quoted 20 lines
Makefile +3 -2
+###########################################################################
+
 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  $@")
 
@@ -226,8 +237,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
-	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
Makefile.objs +0 -0
index 4412757..8a4c9fc 100644

Makefile.objs nested-vars += \ · line 256

Quoted 9 lines
Makefile.objs +6 -0
@@ -248,3 +248,9 @@ nested-vars += \
 	common-obj-y \
 	extra-obj-y
 dummy := $(call unnest-vars)
+
+#############################################################################
+# libqblock
+
+libqblock-lib-la = libqblock.la
+libqblock-lib-path = libqblock

Remove these two please.

Quoted 13 lines
libqblock/Makefile new file +7 -0
new file mode 100644
index 0000000..5f65613
@@ -0,0 +1,56 @@
+###########################################################################
+# libqblock Makefile
+# Todo:
+#    1 trace related files is generated in this directory, move
+#  them to the root directory.
+##########################################################################
+-include ../config-host.mak

libqblock/Makefile line 17

Quoted 10 lines
libqblock/Makefile new file +10 -0
+-include $(SRC_PATH)/Makefile.objs
+-include $(SRC_PATH)/rules.mak
+
+#############################################################################
+# Library settings
+#############################################################################
+$(call set-vpath, $(SRC_PATH))
+
+#expand the foldered vars,especially ./block
+dummy := $(call unnest-vars-1)

Please use unnest-vars.

Quoted 14 lines
libqblock/Makefile new file +14 -0
+#library objects
+libqblock-y=libqblock.o libqblock-error.o
+
+QEMU_OBJS= $(libqblock-y) $(block-obj-y)
+#filter out ./block
+QEMU_OBJS_FILTERED=$(filter %.o, $(QEMU_OBJS))
+QEMU_OBJS_LIB=$(patsubst %.o, %.lo, $(QEMU_OBJS_FILTERED))
+
+QEMU_CFLAGS+= -I../ -I../include
+#adding magic macro define for symbol hiding and exposing
+QEMU_CFLAGS+= -fvisibility=hidden -D LIBQB_BUILD
+
+#dependency libraries
+LIBS+=-lz $(LIBS_TOOLS)

libqblock/Makefile line 42

Quoted 10 lines
libqblock/Makefile new file +10 -0
+
+#################################################################
+# Runtime rules
+#################################################################
+clean:
+	rm -f *.lo *.o *.d *.la libqblock-test trace.c trace.c-timestamp
+	rm -rf .libs block trace
+
+help:
+	@echo type make libqblock-test at root dirtory, libtool is required

Please remove this rule.

Quoted 13 lines
libqblock/Makefile new file +13 -0
+#make directory ./block at runtime which would hold the output of block/*.c
+block:
+	@mkdir block
+
+ifeq ($(LIBTOOL),)
+$(libqblock-lib-la):
+	@echo "libtool is missing, please install and rerun configure"; exit 1
+else
+$(libqblock-lib-la): $(QEMU_OBJS_LIB)
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -rpath $(libdir) -o $@ $^ $(LIBS),"  lt LINK $@")
+endif
+
+.PHONY: libqblock.la
#3 lore raw

Re: [Qemu-devel] [PATCH V4 1/5] libqblock build system

Ok, I'll correct them.

Quoted 150 lines

Il 27/09/2012 04:23, Wenchao Xia ha scritto:

Quoted 24 lines

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                    |   14 +++++++++-
  Makefile.objs               |    6 ++++
  libqblock/Makefile          |   56 +++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 74 insertions(+), 2 deletions(-)

create mode 100644 libqblock/Makefile
create mode 100644 libqblock/libqblock-error.c
create mode 100644 libqblock/libqblock.c

Makefile +0 -0
index def2ae2..128bc6a 100644
@@ -164,6 +164,17 @@ qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
 
  qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o

Makefile qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y) · line 175

Quoted 10 lines
Makefile +9 -0
 
+######################################################################
+# Support building shared library libqblock
+ifeq ($(LIBTOOL),)
+$(libqblock-lib-la):
+	@echo "libtool is missing, please install and rerun configure"; exit 1
+else
+$(libqblock-lib-la):
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock V="$(V)" TARGET_DIR="$*/" $(libqblock-lib-la),)
+endif

Please remove the useless indirection via $(libqblock-lib-la). In general, this patch should be redone more similar to how libcacard is build:

subdir-libcacard: $(oslib-obj-y) $(trace-obj-y) qemu-timer-common.o
...
libcacard.la: $(oslib-obj-y) qemu-timer-common.o $(trace-obj-y)

         $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libcacard V="$(V)" TARGET_DIR="$*/" libcacard.la,)

install-libcacard: libcacard.la

         $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libcacard V="$(V)" TARGET_DIR="$*/" install-libcacard,)

(The ifeq is not necessary, I'm going to remove it from libcacard too. The same error message is already raised in libcacard/Makefile).

Quoted 20 lines
Makefile +3 -2
+###########################################################################
+
  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  $@")
 
@@ -226,8 +237,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
-	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
Makefile.objs +0 -0
index 4412757..8a4c9fc 100644

Makefile.objs nested-vars += \ · line 256

Quoted 9 lines
Makefile.objs +6 -0
@@ -248,3 +248,9 @@ nested-vars += \
  	common-obj-y \
  	extra-obj-y
  dummy := $(call unnest-vars)
+
+#############################################################################
+# libqblock
+
+libqblock-lib-la = libqblock.la
+libqblock-lib-path = libqblock

Remove these two please.

Quoted 13 lines
libqblock/Makefile new file +7 -0
new file mode 100644
index 0000000..5f65613
@@ -0,0 +1,56 @@
+###########################################################################
+# libqblock Makefile
+# Todo:
+#    1 trace related files is generated in this directory, move
+#  them to the root directory.
+##########################################################################
+-include ../config-host.mak

libqblock/Makefile line 17

Quoted 10 lines
libqblock/Makefile new file +10 -0
+-include $(SRC_PATH)/Makefile.objs
+-include $(SRC_PATH)/rules.mak
+
+#############################################################################
+# Library settings
+#############################################################################
+$(call set-vpath, $(SRC_PATH))
+
+#expand the foldered vars,especially ./block
+dummy := $(call unnest-vars-1)

Please use unnest-vars.

Quoted 14 lines
libqblock/Makefile new file +14 -0
+#library objects
+libqblock-y=libqblock.o libqblock-error.o
+
+QEMU_OBJS= $(libqblock-y) $(block-obj-y)
+#filter out ./block
+QEMU_OBJS_FILTERED=$(filter %.o, $(QEMU_OBJS))
+QEMU_OBJS_LIB=$(patsubst %.o, %.lo, $(QEMU_OBJS_FILTERED))
+
+QEMU_CFLAGS+= -I../ -I../include
+#adding magic macro define for symbol hiding and exposing
+QEMU_CFLAGS+= -fvisibility=hidden -D LIBQB_BUILD
+
+#dependency libraries
+LIBS+=-lz $(LIBS_TOOLS)

libqblock/Makefile line 42

Quoted 10 lines
libqblock/Makefile new file +10 -0
+
+#################################################################
+# Runtime rules
+#################################################################
+clean:
+	rm -f *.lo *.o *.d *.la libqblock-test trace.c trace.c-timestamp
+	rm -rf .libs block trace
+
+help:
+	@echo type make libqblock-test at root dirtory, libtool is required

Please remove this rule.

Quoted 13 lines
libqblock/Makefile new file +13 -0
+#make directory ./block at runtime which would hold the output of block/*.c
+block:
+	@mkdir block
+
+ifeq ($(LIBTOOL),)
+$(libqblock-lib-la):
+	@echo "libtool is missing, please install and rerun configure"; exit 1
+else
+$(libqblock-lib-la): $(QEMU_OBJS_LIB)
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -rpath $(libdir) -o $@ $^ $(LIBS),"  lt LINK $@")
+endif
+
+.PHONY: libqblock.la
Signature · 3 lines
Best Regards

Wenchao Xia
#4 lore raw

PATCH v4 2/5 libqblock type defines

In reply to Wenchao Xia

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 |   57 +++++++++
 libqblock/libqblock-types.h    |  268 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 325 insertions(+), 0 deletions(-)
 create mode 100644 libqblock/libqblock-internal.h
 create mode 100644 libqblock/libqblock-types.h
libqblock/libqblock-internal.h new file +57 -0
new file mode 100644
index 0000000..d3e983c
@@ -0,0 +1,57 @@
+/*
+ * 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 */
+};
+
+#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 new file +268 -0

268 lines not on this page · raw message

Signature · 1 line
1.7.1
#5 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

Il 27/09/2012 04:23, Wenchao Xia ha scritto:

Quoted 98 lines

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 |   57 +++++++++
 libqblock/libqblock-types.h    |  268 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 325 insertions(+), 0 deletions(-)
 create mode 100644 libqblock/libqblock-internal.h
 create mode 100644 libqblock/libqblock-types.h
libqblock/libqblock-internal.h new file +57 -0
new file mode 100644
index 0000000..d3e983c
@@ -0,0 +1,57 @@
+/*
+ * 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 */
+};
+
+#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 new file +18 -0
new file mode 100644
index 0000000..948ab8a
@@ -0,0 +1,268 @@
+/*
+ * 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_TYPES_H
+#define LIBQBLOCK_TYPES_H
+
+#include <sys/types.h>
+#include <stdint.h>

libqblock/libqblock-types.h line 28

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+#include <stdbool.h>
+
+#if defined(__GNUC__) && __GNUC__ >= 4
+    #ifdef LIBQB_BUILD
+        #define DLL_PUBLIC __attribute__((visibility("default")))
+    #else
+        #define DLL_PUBLIC
+    #endif
+#else
+    #warning : gcc compiler version < 4, symbols can not be hidden.

You don't need to warn. If LIBQB_BUILD is not defined, DLL_PUBLIC would be empty anyway. If LIBQB_BUILD is defined, you know GCC >= 4.0 because you pass -fvisibility=hidden from the Makefile.

Quoted 21 lines
libqblock/libqblock-types.h new file +21 -0
+#endif
+
+/* this library is designed around this core struct. */
+typedef struct QBlockState QBlockState;
+
+/* every thread should have a context. */
+typedef struct QBlockContext QBlockContext;
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)

libqblock/libqblock-types.h line 59

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+
+#define LIBQBLOCK_O_VALID_MASK \
+   (LIBQBLOCK_O_RDWR | LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | \
+    LIBQBLOCK_O_NO_BACKING | LIBQBLOCK_O_NO_FLUSH)
+
+typedef enum QBlockProtType {
+    QB_PROT_NONE = 0,
+    QB_PROT_FILE,
+    QB_PROT_MAX
+} QBlockProtType;

Use QBlockProtocol, QB_PROTO_*.

libqblock/libqblock-types.h line 63

Quoted 3 lines
libqblock/libqblock-types.h new file +3 -0
+typedef struct QBlockProtOptionFile {
+    const char *filename;
+} QBlockProtOptionFile;

Use QBlockProtocolOptionsFile

libqblock/libqblock-types.h line 69

Quoted 5 lines
libqblock/libqblock-types.h new file +5 -0
+#define QBLOCK_PROT_OPTIONS_UNION_SIZE (512)
+typedef union QBlockProtOptionsUnion {
+    QBlockProtOptionFile o_file;
+    uint8_t reserved[QBLOCK_PROT_OPTIONS_UNION_SIZE];
+} QBlockProtOptionsUnion;

Not really options, because they are required. I would just not name the union, and embed it in the struct. Also please change it to

     uint64_t reserved[QBLOCK_PROT_OPTIONS_UNION_SIZE / 8];

(Sorry for not thinking about this before). This will fix the alignment and ensure that future changes to the union do not change the ABI.

libqblock/libqblock-types.h line 80

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+/**
+ * struct QBlockProtInfo: contains information about how to find the image
+ *
+ * @prot_type: protocol type, now only support FILE.
+ * @prot_op: protocol related options.
+ */
+typedef struct QBlockProtInfo {
+    QBlockProtType prot_type;
+    QBlockProtOptionsUnion prot_op;
+} QBlockProtInfo;

What about QBlockLocation instead? Or QBlockLocationInfo.

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+
+/* format related options */
+typedef enum QBlockFmtType {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,

libqblock/libqblock-types.h line 97

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+} QBlockFmtType;

QBlockFormat; similarly remove "Type" from all enums.

libqblock/libqblock-types.h line 102

Quoted 4 lines
libqblock/libqblock-types.h new file +4 -0
+typedef struct QBlockFmtOptionCow {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+} QBlockFmtOptionCow;

QBlockFormatOptionsCOW. Similarly use the plural for all other formats.

libqblock/libqblock-types.h line 110

Quoted 7 lines
libqblock/libqblock-types.h new file +7 -0
+typedef struct QBlockFmtOptionQed {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    QBlockFmtType backing_fmt;
+    uint64_t cluster_size; /* unit is bytes */
+    uint64_t table_size; /* unit is clusters */
+} QBlockFmtOptionQed;

Similarly use uppercase for QED, QCOW, etc.

libqblock/libqblock-types.h line 123

Quoted 12 lines
libqblock/libqblock-types.h new file +12 -0
+typedef struct QBlockFmtOptionQcow {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    bool encrypt;
+} QBlockFmtOptionQcow;
+
+/* "Compatibility level (0.10 or 1.1)" */
+typedef enum QBlockFmtOptionQcow2CptLv {
+    QBO_FMT_QCOW2_CPT_NONE = 0,
+    QBO_FMT_QCOW2_CPT_V010,
+    QBO_FMT_QCOW2_CPT_V110,
+} QBlockFmtOptionQcow2CptLv;

Please use QBO_ instead of QB_ throughout. Also write COMPAT instead of CPT, and remove CPT_NONE since 0.10 is the default:

typedef enum QBlockFmtOptionQCOW2Compat {
    QB_FMT_QCOW2_COMPAT_V0_10,
    QB_FMT_QCOW2_COMPAT_V1_1,
} QBlockFmtOptionQCOW2Compat

libqblock/libqblock-types.h line 130

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+/* off or metadata */
+typedef enum QBlockFmtOptionQcow2PreAllocType {
+    QBO_FMT_QCOW2_PREALLOC_NONE = 0,
+    QBO_FMT_QCOW2_PREALLOC_OFF,
+    QBO_FMT_QCOW2_PREALLOC_METADATA,
+} QBlockFmtOptionQcow2PreAllocType;

Please remove NONE since OFF is the default. However, I would use the enum.

Quoted 15 lines
libqblock/libqblock-types.h new file +15 -0
+typedef struct QBlockFmtOptionQcow2 {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    QBlockFmtType backing_fmt;
+    bool encrypt;
+    uint64_t cluster_size; /* unit is bytes */
+    QBlockFmtOptionQcow2CptLv cpt_lv;
+    QBlockFmtOptionQcow2PreAllocType pre_mode;
+} QBlockFmtOptionQcow2;
+
+typedef struct QBlockFmtOptionRaw {
+    uint64_t virt_size;
+} QBlockFmtOptionRaw;
+
+typedef struct QBlockFmtOptionRbd {

libqblock/libqblock-types.h line 156

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    uint64_t virt_size;
+    uint64_t cluster_size;
+} QBlockFmtOptionRbd;
+
+/* off or full */
+typedef enum QBlockFmtOptionSheepdogPreAllocType {
+    QBO_FMT_SD_PREALLOC_NONE = 0,
+    QBO_FMT_SD_PREALLOC_OFF,
+    QBO_FMT_SD_PREALLOC_FULL,
+} QBlockFmtOptionSheepdogPreAllocType;

The default is false, so just make it a bool.

libqblock/libqblock-types.h line 168

Quoted 11 lines
libqblock/libqblock-types.h new file +11 -0
+typedef struct QBlockFmtOptionSheepdog {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    QBlockFmtOptionSheepdogPreAllocType pre_mode;
+} QBlockFmtOptionSheepdog;
+
+typedef enum QBlockFmtOptionVdiPreAllocType {
+    QBO_FMT_VDI_PREALLOC_NONE = 0,
+    QBO_FMT_VDI_PREALLOC_FALSE,
+    QBO_FMT_VDI_PREALLOC_TRUE,
+} QBlockFmtOptionVdiPreAllocType;

Please remove NONE, and replace FALSE/TRUE with OFF/METADATA (same as qcow2).

libqblock/libqblock-types.h line 181

Quoted 12 lines
libqblock/libqblock-types.h new file +12 -0
+typedef struct QBlockFmtOptionVdi {
+    uint64_t virt_size;
+    uint64_t cluster_size;
+    QBlockFmtOptionVdiPreAllocType pre_mode;
+} QBlockFmtOptionVdi;
+
+/* whether compact to vmdk verion 6 */
+typedef enum QBlockFmtOptionVmdkCptLv {
+    QBO_FMT_VMDK_CPT_NONE = 0,
+    QBO_FMT_VMDK_CPT_VMDKV6_FALSE,
+    QBO_FMT_VMDK_CPT_VMDKV6_TRUE,
+} QBlockFmtOptionVmdkCptLv;

Here the default is false, so just make it a bool.

libqblock/libqblock-types.h line 188

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+/* vmdk flat extent format, values:
+"{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+twoGbMaxExtentFlat | streamOptimized} */
+typedef enum QBlockFmtOptionVmdkSubfmtType {
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE = 0,
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE,

You can remove QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE (whose more appropriate name would be just ...SUBFMT_NONE) since the default is QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE.

Quoted 9 lines
libqblock/libqblock-types.h new file +9 -0
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_FLAT,
+    QBO_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_SPARSE,
+    QBO_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_FLAT,
+    QBO_FMT_VMDK_SUBFMT_STREAM_OPTIMIZED,
+} QBlockFmtOptionVmdkSubfmtType;
+
+typedef struct QBlockFmtOptionVmdk {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;

libqblock/libqblock-types.h line 207

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    QBlockFmtOptionVmdkCptLv cpt_lv;
+    QBlockFmtOptionVmdkSubfmtType subfmt;
+} QBlockFmtOptionVmdk;
+
+/* "{dynamic (default) | fixed} " */
+typedef enum QBlockFmtOptionVpcSubfmtType {
+    QBO_FMT_VPC_SUBFMT_NONE = 0,
+    QBO_FMT_VPC_SUBFMT_DYNAMIC,
+    QBO_FMT_VPC_SUBFMT_FIXED,
+} QBlockFmtOptionVpcSubfmtType;

Again, please remove NONE.

Quoted 8 lines
libqblock/libqblock-types.h new file +8 -0
+typedef struct QBlockFmtOptionVpc {
+    uint64_t virt_size;
+    QBlockFmtOptionVpcSubfmtType subfmt;
+} QBlockFmtOptionVpc;
+
+#define QBLOCK_FMT_OPTIONS_UNION_SIZE (QBLOCK_PROT_OPTIONS_UNION_SIZE*2)
+typedef union QBlockFmtOptionsUnion {
+    QBlockFmtOptionCow       o_cow;

libqblock/libqblock-types.h line 226

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    QBlockFmtOptionQed       o_qed;
+    QBlockFmtOptionQcow      o_qcow;
+    QBlockFmtOptionQcow2     o_qcow2;
+    QBlockFmtOptionRaw       o_raw;
+    QBlockFmtOptionRbd       o_rbd;
+    QBlockFmtOptionSheepdog  o_sheepdog;
+    QBlockFmtOptionVdi       o_vdi;
+    QBlockFmtOptionVmdk      o_vmdk;
+    QBlockFmtOptionVpc       o_vpc;
+    uint8_t reserved[QBLOCK_FMT_OPTIONS_UNION_SIZE];

Similarly use uint64_t here, and just embed the union in the struct.

libqblock/libqblock-types.h line 232

Quoted 5 lines
libqblock/libqblock-types.h new file +5 -0
+} QBlockFmtOptionsUnion;
+typedef struct QBlockFmtInfo {
+    QBlockFmtType fmt_type;
+    QBlockFmtOptionsUnion fmt_op;
+} QBlockFmtInfo;

QBlockFormatInfo.

Quoted 7 lines
libqblock/libqblock-types.h new file +7 -0
+/**
+ * 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.
+ *

libqblock/libqblock-types.h line 250

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+ *  @virt_size: virtual size, it is always not NULL.
+ *  @backing_loc: backing file location.
+ *  @encrypt: encryption flag.
+*/
+
+typedef struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockProtInfo *backing_loc;
+    bool *encrypt;
+} QBlockStaticInfoAddr;

Why the indirection?

Quoted 18 lines
libqblock/libqblock-types.h new file +17 -0
+/**
+ * QBlockStaticInfo: information about the block image.
+ *
+ * @member_addr: contains pointer which can be used to access the structure.
+ * @loc: location information.
+ * @fmt: format information.
+ * @sector_size: how many bytes in a sector, it is 512 usually.
+ */
+typedef struct QBlockStaticInfo {
+    QBlockStaticInfoAddr *member_addr;
+    QBlockProtInfo loc;
+    QBlockFmtInfo fmt;
+    int sector_size;
+} QBlockStaticInfo;
+
+
+#endif
#6 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

I will change the code for most of comments, but have some response
below.

Quoted 163 lines

Il 27/09/2012 04:23, Wenchao Xia ha scritto:

Quoted 98 lines

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 |   57 +++++++++
  libqblock/libqblock-types.h    |  268 ++++++++++++++++++++++++++++++++++++++++
  2 files changed, 325 insertions(+), 0 deletions(-)

create mode 100644 libqblock/libqblock-internal.h create mode 100644 libqblock/libqblock-types.h

libqblock/libqblock-internal.h new file +57 -0
new file mode 100644
index 0000000..d3e983c
@@ -0,0 +1,57 @@
+/*
+ * 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 */
+};
+
+#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 new file +18 -0
new file mode 100644
index 0000000..948ab8a
@@ -0,0 +1,268 @@
+/*
+ * 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_TYPES_H
+#define LIBQBLOCK_TYPES_H
+
+#include <sys/types.h>
+#include <stdint.h>

libqblock/libqblock-types.h line 28

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+#include <stdbool.h>
+
+#if defined(__GNUC__) && __GNUC__ >= 4
+    #ifdef LIBQB_BUILD
+        #define DLL_PUBLIC __attribute__((visibility("default")))
+    #else
+        #define DLL_PUBLIC
+    #endif
+#else
+    #warning : gcc compiler version < 4, symbols can not be hidden.

You don't need to warn. If LIBQB_BUILD is not defined, DLL_PUBLIC would be empty anyway. If LIBQB_BUILD is defined, you know GCC >= 4.0 because you pass -fvisibility=hidden from the Makefile.

Quoted 21 lines
libqblock/libqblock-types.h new file +21 -0
+#endif
+
+/* this library is designed around this core struct. */
+typedef struct QBlockState QBlockState;
+
+/* every thread should have a context. */
+typedef struct QBlockContext QBlockContext;
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)

libqblock/libqblock-types.h line 59

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+
+#define LIBQBLOCK_O_VALID_MASK \
+   (LIBQBLOCK_O_RDWR | LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | \
+    LIBQBLOCK_O_NO_BACKING | LIBQBLOCK_O_NO_FLUSH)
+
+typedef enum QBlockProtType {
+    QB_PROT_NONE = 0,
+    QB_PROT_FILE,
+    QB_PROT_MAX
+} QBlockProtType;

Use QBlockProtocol, QB_PROTO_*.

libqblock/libqblock-types.h line 63

Quoted 3 lines
libqblock/libqblock-types.h new file +3 -0
+typedef struct QBlockProtOptionFile {
+    const char *filename;
+} QBlockProtOptionFile;

Use QBlockProtocolOptionsFile

libqblock/libqblock-types.h line 69

Quoted 5 lines
libqblock/libqblock-types.h new file +5 -0
+#define QBLOCK_PROT_OPTIONS_UNION_SIZE (512)
+typedef union QBlockProtOptionsUnion {
+    QBlockProtOptionFile o_file;
+    uint8_t reserved[QBLOCK_PROT_OPTIONS_UNION_SIZE];
+} QBlockProtOptionsUnion;

Not really options, because they are required. I would just not name the union, and embed it in the struct. Also please change it to

Quoted 5 lines
      uint64_t reserved[QBLOCK_PROT_OPTIONS_UNION_SIZE / 8];

(Sorry for not thinking about this before). This will fix the alignment and ensure that future changes to the union do not change the ABI.

You are right, I forgot alignment issue before.

Quoted 49 lines

libqblock/libqblock-types.h line 80

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+/**
+ * struct QBlockProtInfo: contains information about how to find the image
+ *
+ * @prot_type: protocol type, now only support FILE.
+ * @prot_op: protocol related options.
+ */
+typedef struct QBlockProtInfo {
+    QBlockProtType prot_type;
+    QBlockProtOptionsUnion prot_op;
+} QBlockProtInfo;

What about QBlockLocation instead? Or QBlockLocationInfo.

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+
+/* format related options */
+typedef enum QBlockFmtType {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,

libqblock/libqblock-types.h line 97

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+} QBlockFmtType;

QBlockFormat; similarly remove "Type" from all enums.

libqblock/libqblock-types.h line 102

Quoted 4 lines
libqblock/libqblock-types.h new file +4 -0
+typedef struct QBlockFmtOptionCow {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+} QBlockFmtOptionCow;

QBlockFormatOptionsCOW. Similarly use the plural for all other formats.

libqblock/libqblock-types.h line 110

Quoted 7 lines
libqblock/libqblock-types.h new file +7 -0
+typedef struct QBlockFmtOptionQed {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    QBlockFmtType backing_fmt;
+    uint64_t cluster_size; /* unit is bytes */
+    uint64_t table_size; /* unit is clusters */
+} QBlockFmtOptionQed;

Similarly use uppercase for QED, QCOW, etc.

Quoted 16 lines

libqblock/libqblock-types.h line 123

Quoted 12 lines
libqblock/libqblock-types.h new file +12 -0
+typedef struct QBlockFmtOptionQcow {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    bool encrypt;
+} QBlockFmtOptionQcow;
+
+/* "Compatibility level (0.10 or 1.1)" */
+typedef enum QBlockFmtOptionQcow2CptLv {
+    QBO_FMT_QCOW2_CPT_NONE = 0,
+    QBO_FMT_QCOW2_CPT_V010,
+    QBO_FMT_QCOW2_CPT_V110,
+} QBlockFmtOptionQcow2CptLv;

Please use QBO_ instead of QB_ throughout. Also write COMPAT instead of CPT, and remove CPT_NONE since 0.10 is the default:

__NONE is used to indicate whether this property is set or get, so
it is actually have 3 status than just 2: Not set, V010, V110. It
is the same reason that I use __NONE in bool values, especially __NONE could represent it is not got in information retrieving. Maybe I should rename them to __NOTSET.

Quoted 145 lines
typedef enum QBlockFmtOptionQCOW2Compat {
     QB_FMT_QCOW2_COMPAT_V0_10,
     QB_FMT_QCOW2_COMPAT_V1_1,
} QBlockFmtOptionQCOW2Compat

libqblock/libqblock-types.h line 130

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+/* off or metadata */
+typedef enum QBlockFmtOptionQcow2PreAllocType {
+    QBO_FMT_QCOW2_PREALLOC_NONE = 0,
+    QBO_FMT_QCOW2_PREALLOC_OFF,
+    QBO_FMT_QCOW2_PREALLOC_METADATA,
+} QBlockFmtOptionQcow2PreAllocType;

Please remove NONE since OFF is the default. However, I would use the enum.

Quoted 15 lines
libqblock/libqblock-types.h new file +15 -0
+typedef struct QBlockFmtOptionQcow2 {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    QBlockFmtType backing_fmt;
+    bool encrypt;
+    uint64_t cluster_size; /* unit is bytes */
+    QBlockFmtOptionQcow2CptLv cpt_lv;
+    QBlockFmtOptionQcow2PreAllocType pre_mode;
+} QBlockFmtOptionQcow2;
+
+typedef struct QBlockFmtOptionRaw {
+    uint64_t virt_size;
+} QBlockFmtOptionRaw;
+
+typedef struct QBlockFmtOptionRbd {

libqblock/libqblock-types.h line 156

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    uint64_t virt_size;
+    uint64_t cluster_size;
+} QBlockFmtOptionRbd;
+
+/* off or full */
+typedef enum QBlockFmtOptionSheepdogPreAllocType {
+    QBO_FMT_SD_PREALLOC_NONE = 0,
+    QBO_FMT_SD_PREALLOC_OFF,
+    QBO_FMT_SD_PREALLOC_FULL,
+} QBlockFmtOptionSheepdogPreAllocType;

The default is false, so just make it a bool.

libqblock/libqblock-types.h line 168

Quoted 11 lines
libqblock/libqblock-types.h new file +11 -0
+typedef struct QBlockFmtOptionSheepdog {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;
+    QBlockFmtOptionSheepdogPreAllocType pre_mode;
+} QBlockFmtOptionSheepdog;
+
+typedef enum QBlockFmtOptionVdiPreAllocType {
+    QBO_FMT_VDI_PREALLOC_NONE = 0,
+    QBO_FMT_VDI_PREALLOC_FALSE,
+    QBO_FMT_VDI_PREALLOC_TRUE,
+} QBlockFmtOptionVdiPreAllocType;

Please remove NONE, and replace FALSE/TRUE with OFF/METADATA (same as qcow2).

libqblock/libqblock-types.h line 181

Quoted 12 lines
libqblock/libqblock-types.h new file +12 -0
+typedef struct QBlockFmtOptionVdi {
+    uint64_t virt_size;
+    uint64_t cluster_size;
+    QBlockFmtOptionVdiPreAllocType pre_mode;
+} QBlockFmtOptionVdi;
+
+/* whether compact to vmdk verion 6 */
+typedef enum QBlockFmtOptionVmdkCptLv {
+    QBO_FMT_VMDK_CPT_NONE = 0,
+    QBO_FMT_VMDK_CPT_VMDKV6_FALSE,
+    QBO_FMT_VMDK_CPT_VMDKV6_TRUE,
+} QBlockFmtOptionVmdkCptLv;

Here the default is false, so just make it a bool.

libqblock/libqblock-types.h line 188

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+/* vmdk flat extent format, values:
+"{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+twoGbMaxExtentFlat | streamOptimized} */
+typedef enum QBlockFmtOptionVmdkSubfmtType {
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE = 0,
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE,

You can remove QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE (whose more appropriate name would be just ...SUBFMT_NONE) since the default is QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE.

Quoted 9 lines
libqblock/libqblock-types.h new file +9 -0
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_FLAT,
+    QBO_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_SPARSE,
+    QBO_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_FLAT,
+    QBO_FMT_VMDK_SUBFMT_STREAM_OPTIMIZED,
+} QBlockFmtOptionVmdkSubfmtType;
+
+typedef struct QBlockFmtOptionVmdk {
+    uint64_t virt_size;
+    QBlockProtInfo backing_loc;

libqblock/libqblock-types.h line 207

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    QBlockFmtOptionVmdkCptLv cpt_lv;
+    QBlockFmtOptionVmdkSubfmtType subfmt;
+} QBlockFmtOptionVmdk;
+
+/* "{dynamic (default) | fixed} " */
+typedef enum QBlockFmtOptionVpcSubfmtType {
+    QBO_FMT_VPC_SUBFMT_NONE = 0,
+    QBO_FMT_VPC_SUBFMT_DYNAMIC,
+    QBO_FMT_VPC_SUBFMT_FIXED,
+} QBlockFmtOptionVpcSubfmtType;

Again, please remove NONE.

Quoted 8 lines
libqblock/libqblock-types.h new file +8 -0
+typedef struct QBlockFmtOptionVpc {
+    uint64_t virt_size;
+    QBlockFmtOptionVpcSubfmtType subfmt;
+} QBlockFmtOptionVpc;
+
+#define QBLOCK_FMT_OPTIONS_UNION_SIZE (QBLOCK_PROT_OPTIONS_UNION_SIZE*2)
+typedef union QBlockFmtOptionsUnion {
+    QBlockFmtOptionCow       o_cow;

libqblock/libqblock-types.h line 226

Quoted 10 lines
libqblock/libqblock-types.h new file +10 -0
+    QBlockFmtOptionQed       o_qed;
+    QBlockFmtOptionQcow      o_qcow;
+    QBlockFmtOptionQcow2     o_qcow2;
+    QBlockFmtOptionRaw       o_raw;
+    QBlockFmtOptionRbd       o_rbd;
+    QBlockFmtOptionSheepdog  o_sheepdog;
+    QBlockFmtOptionVdi       o_vdi;
+    QBlockFmtOptionVmdk      o_vmdk;
+    QBlockFmtOptionVpc       o_vpc;
+    uint8_t reserved[QBLOCK_FMT_OPTIONS_UNION_SIZE];

Similarly use uint64_t here, and just embed the union in the struct.

libqblock/libqblock-types.h line 232

Quoted 5 lines
libqblock/libqblock-types.h new file +5 -0
+} QBlockFmtOptionsUnion;
+typedef struct QBlockFmtInfo {
+    QBlockFmtType fmt_type;
+    QBlockFmtOptionsUnion fmt_op;
+} QBlockFmtInfo;

QBlockFormatInfo.

Quoted 11 lines
libqblock/libqblock-types.h new file +11 -0
+/**
+ * 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.
+*/
Quoted 9 lines

libqblock/libqblock-types.h line 250

Quoted 6 lines
libqblock/libqblock-types.h new file +6 -0
+
+typedef struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockProtInfo *backing_loc;
+    bool *encrypt;
+} QBlockStaticInfoAddr;

Why the indirection?

It helps user to get these important members, otherwise
user will need
Switch (fmt) {
case RAW:

    ....
  case QCOW2:
    ...
}

for every attribute. The indirection address will let user directly access the members.

Quoted 19 lines
Quoted 18 lines
libqblock/libqblock-types.h new file +17 -0
+/**
+ * QBlockStaticInfo: information about the block image.
+ *
+ * @member_addr: contains pointer which can be used to access the structure.
+ * @loc: location information.
+ * @fmt: format information.
+ * @sector_size: how many bytes in a sector, it is 512 usually.
+ */
+typedef struct QBlockStaticInfo {
+    QBlockStaticInfoAddr *member_addr;
+    QBlockProtInfo loc;
+    QBlockFmtInfo fmt;
+    int sector_size;
+} QBlockStaticInfo;
+
+
+#endif
Signature · 3 lines
Best Regards

Wenchao Xia
#7 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

Il 27/09/2012 11:52, Wenchao Xia ha scritto:

Quoted 8 lines
Quoted 4 lines

Please use QBO_ instead of QB_ throughout. Also write COMPAT instead of CPT, and remove CPT_NONE since 0.10 is the default:

__NONE is used to indicate whether this property is set or get, so
it is actually have 3 status than just 2: Not set, V010, V110. It is the same reason that I use __NONE in bool values, especially __NONE could represent it is not got in information retrieving.

Yes, that I guessed. I thought in many cases we can anticipate that the default is not going to change, but perhaps it's better to be safe (which is what you did).

Please do change FALSE/TRUE to OFF/METADATA for preallocation enums, and please remove MONOLITHIC from QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE.

Quoted 1 line

Maybe I should rename them to __NOTSET.

Perhaps "_DEFAULT" is even bette?

Paolo

Quoted 13 lines
Quoted 9 lines

libqblock/libqblock-types.h line 250

Quoted 6 lines
libqblock/libqblock-types.h new file +5 -0
+typedef struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockProtInfo *backing_loc;
+    bool *encrypt;
+} QBlockStaticInfoAddr;

Why the indirection?

It helps user to get these important members, otherwise
user will need
Switch (fmt) {
case RAW:

Quoted 6 lines
    ....
  case QCOW2:
    ...
}

for every attribute. The indirection address will let user directly access the members.

Ah, ok, now I understand. Interesting. An alternative could be to add generic accessors like these:

uint64_t qblock_get_virt_size(QBlockFmtInfo *fmt); QBlockProtInfo *qblock_get_backing_loc(QBlockFmtInfo *fmt); bool qblock_get_encrypt(QBlockFmtInfo *fmt);

etc. that include the switch statement.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

Quoted 10 lines · Paolo Bonzini

Il 27/09/2012 11:52, Wenchao Xia ha scritto:

Quoted 8 lines
Quoted 4 lines

Please use QBO_ instead of QB_ throughout. Also write COMPAT instead of CPT, and remove CPT_NONE since 0.10 is the default:

__NONE is used to indicate whether this property is set or get, so
it is actually have 3 status than just 2: Not set, V010, V110. It is the same reason that I use __NONE in bool values, especially __NONE could represent it is not got in information retrieving.

Quoted 7 lines

Yes, that I guessed. I thought in many cases we can anticipate that the default is not going to change, but perhaps it's better to be safe (which is what you did).

Please do change FALSE/TRUE to OFF/METADATA for preallocation enums, and please remove MONOLITHIC from QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE.

OK, these should be changed.

Quoted 6 lines
Quoted 1 line

Maybe I should rename them to __NOTSET.

Perhaps "_DEFAULT" is even bette?

Paolo

Yep, _DEFAULT seems good to me.

Quoted 20 lines
Quoted 11 lines
Quoted 9 lines

libqblock/libqblock-types.h line 250

Quoted 6 lines
libqblock/libqblock-types.h new file +5 -0
+typedef struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockProtInfo *backing_loc;
+    bool *encrypt;
+} QBlockStaticInfoAddr;

Why the indirection?

It helps user to get these important members, otherwise
user will need

Quoted 8 lines
Switch (fmt) {
   case RAW:
     ....
   case QCOW2:
     ...
}

for every attribute. The indirection address will let user directly access the members.

Quoted 9 lines

Ah, ok, now I understand. Interesting. An alternative could be to add generic accessors like these:

uint64_t qblock_get_virt_size(QBlockFmtInfo *fmt); QBlockProtInfo *qblock_get_backing_loc(QBlockFmtInfo *fmt); bool qblock_get_encrypt(QBlockFmtInfo *fmt);

etc. that include the switch statement.

I think it is reasonable use generic accessors instead of embbed
structure, maybe an additional helper function should be added as:

QBlockStaticInfoAddr *qblock_get_static_info_addr(QBlockStaticInfo
*info)

In this way only one switch statement is needed in source code,
and only one new API is added.

Quoted 2 lines

Paolo

Signature · 3 lines
Best Regards

Wenchao Xia
#9 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

Il 28/09/2012 05:00, Wenchao Xia ha scritto:

Quoted 8 lines
Quoted 8 lines

uint64_t qblock_get_virt_size(QBlockFmtInfo *fmt); QBlockProtInfo *qblock_get_backing_loc(QBlockFmtInfo *fmt); bool qblock_get_encrypt(QBlockFmtInfo *fmt);

etc. that include the switch statement.

Quoted 8 lines

I think it is reasonable use generic accessors instead of embbed
structure, maybe an additional helper function should be added as:

QBlockStaticInfoAddr *qblock_get_static_info_addr(QBlockStaticInfo
*info)

In this way only one switch statement is needed in source code,
and only one new API is added.

No problem for the implementation, but please make this function static.

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

Quoted 15 lines · Paolo Bonzini

Il 28/09/2012 05:00, Wenchao Xia ha scritto:

Quoted 14 lines
Quoted 8 lines

uint64_t qblock_get_virt_size(QBlockFmtInfo *fmt); QBlockProtInfo *qblock_get_backing_loc(QBlockFmtInfo *fmt); bool qblock_get_encrypt(QBlockFmtInfo *fmt);

etc. that include the switch statement.

I think it is reasonable use generic accessors instead of embbed
structure, maybe an additional helper function should be added as:

QBlockStaticInfoAddr *qblock_get_static_info_addr(QBlockStaticInfo
*info)

Quoted 7 lines
Quoted 2 lines

In this way only one switch statement is needed in source code,
and only one new API is added.

No problem for the implementation, but please make this function static.

Paolo

Do you mean declaring it as static function? I think user may need
call it to get information so it need to be public.

Signature · 3 lines
Best Regards

Wenchao Xia
#11 lore raw

Re: [Qemu-devel] [PATCH V4 2/5] libqblock type defines

Il 28/09/2012 12:52, Wenchao Xia ha scritto:

Quoted 4 lines
Quoted 1 line

No problem for the implementation, but please make this function static.

Do you mean declaring it as static function? I think user may need
call it to get information so it need to be public.

The user can use the accessors. Let's not add a function to the public interface until we're 101% sure it is a good idea.

Paolo

#12 lore raw

PATCH v4 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       | 1165 +++++++++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h       |  308 ++++++++++++
 6 files changed, 1581 insertions(+), 1 deletions(-)
 create mode 100644 libqblock/libqblock-error.h
 create mode 100644 libqblock/libqblock.h
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 new file +49 -0

49 lines not on this page · raw message

libqblock/libqblock.c +1165 -0

1,165 lines not on this page · raw message

libqblock/libqblock.h new file +308 -0

308 lines not on this page · raw message

Signature · 1 line
1.7.1
#13 lore raw

PATCH v4 4/5 libqblock test build system

In reply to Wenchao Xia

This patch will create a new directory in ./tests, make check-libqblock will
build an executable binary, make clean or make check-clean will delete generated binaries.

---
 .gitignore                        |    2 +
 Makefile                          |    1 +
 tests/Makefile                    |   45 +++++++++++++++++++++++++++++++++++++
 tests/libqblock/libqblock-qcow2.c |    4 +++
 4 files changed, 52 insertions(+), 0 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 -0
index 128bc6a..7cfb555 100644
@@ -237,6 +237,7 @@ clean:
 	rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
 	rm -rf qapi-generated
 	rm -rf qga/qapi-generated
+	$(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 +45 -0

68 lines not on this page · raw message

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
#14 lore raw

Re: [Qemu-devel] [PATCH V4 4/5] libqblock test build system

Il 27/09/2012 04:24, Wenchao Xia ha scritto:

tests/Makefile check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET) · line 89

Quoted 3 lines
tests/Makefile +3 -0
+#libqblock
+LIBQBLOCK_TEST_DIR=$(SRC_PATH)/tests/libqblock/test_images
+qtest-lib-y=$(patsubst %.o, %.lo,$(qtest-obj-y))

I don't think you need qtest-obj-y for anything.

tests/Makefile check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET) · line 95

Quoted 6 lines
tests/Makefile +6 -0
+libqblock-la-path = $(libqblock-lib-path)/$(libqblock-lib-la)
+
+tests/libqblock/%.lo: QEMU_INCLUDES += -I$(libqblock-lib-path) -Itests
+
+check-libqblock-y = tests/libqblock/check-libqblock-qcow2$(EXESUF)
+tests/libqblock/check-libqblock-qcow2$(EXESUF): tests/libqblock/libqblock-qcow2.lo $(libqblock-la-path) $(qtest-lib-y)

No need to use .lo here.

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 175

Quoted 6 lines
tests/Makefile +5 -0
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -shared -rpath $(libdir) -o $@ $^,"  lt LINK $@")
+
 
+$(libqblock-la-path):
+	@echo "Building libqblock.la..."
+	$(call quiet-command,$(MAKE) -C $(SRC_PATH) $(libqblock-lib-la),)

No need for this.

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 178

Quoted 3 lines

+$(LIBQBLOCK_TEST_DIR):

tests/Makefile +2 -0
+	@echo "Make libqblock test directory"
+	mkdir $(LIBQBLOCK_TEST_DIR)

You can leave the files in tests/ directly, and avoid this as well.

Quoted 1 line

+check-libqblock: $(libqblock-la-path) $(LIBQBLOCK_TEST_DIR) $(patsubst %,check-%, $(check-libqblock-y))

Please add

check: check-libqblock

here, so that if libtool is present the check is run automatically.

Paolo

#15 lore raw

Re: [Qemu-devel] [PATCH V4 4/5] libqblock test build system

Quoted 7 lines · Paolo Bonzini

Il 27/09/2012 04:24, Wenchao Xia ha scritto:

tests/Makefile check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET) · line 89

Quoted 3 lines
tests/Makefile +3 -0
+#libqblock
+LIBQBLOCK_TEST_DIR=$(SRC_PATH)/tests/libqblock/test_images
+qtest-lib-y=$(patsubst %.o, %.lo,$(qtest-obj-y))

I don't think you need qtest-obj-y for anything.

OK, I will remove it.

Quoted 9 lines

tests/Makefile check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET) · line 95

Quoted 6 lines
tests/Makefile +6 -0
+libqblock-la-path = $(libqblock-lib-path)/$(libqblock-lib-la)
+
+tests/libqblock/%.lo: QEMU_INCLUDES += -I$(libqblock-lib-path) -Itests
+
+check-libqblock-y = tests/libqblock/check-libqblock-qcow2$(EXESUF)
+tests/libqblock/check-libqblock-qcow2$(EXESUF): tests/libqblock/libqblock-qcow2.lo $(libqblock-la-path) $(qtest-lib-y)

No need to use .lo here.

OK.

Quoted 8 lines

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 175

Quoted 6 lines
tests/Makefile +5 -0
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -shared -rpath $(libdir) -o $@ $^,"  lt LINK $@")
+
 
+$(libqblock-la-path):
+	@echo "Building libqblock.la..."
+	$(call quiet-command,$(MAKE) -C $(SRC_PATH) $(libqblock-lib-la),)

No need for this.

OK.

Quoted 6 lines

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 178

Quoted 4 lines

+$(LIBQBLOCK_TEST_DIR):

tests/Makefile +2 -0
+	@echo "Make libqblock test directory"
+	mkdir $(LIBQBLOCK_TEST_DIR)

You can leave the files in tests/ directly, and avoid this as well.

Having a new directory will make clean easier, otherwise the script
will need to know each image file names created, whose filename are generated in test C code at runtime.

Quoted 8 lines
Quoted 2 lines

+check-libqblock: $(libqblock-la-path) $(LIBQBLOCK_TEST_DIR) $(patsubst %,check-%, $(check-libqblock-y))

Please add

check: check-libqblock

here, so that if libtool is present the check is run automatically.

OK.

Quoted 3 lines

Paolo

Signature · 3 lines
Best Regards

Wenchao Xia
#16 lore raw

Re: [Qemu-devel] [PATCH V4 4/5] libqblock test build system

Il 27/09/2012 11:56, Wenchao Xia ha scritto:

Quoted 9 lines
Quoted 6 lines

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 178

Quoted 4 lines

+$(LIBQBLOCK_TEST_DIR):

tests/Makefile +2 -0
+    @echo "Make libqblock test directory"
+    mkdir $(LIBQBLOCK_TEST_DIR)

You can leave the files in tests/ directly, and avoid this as well.

Having a new directory will make clean easier, otherwise the script
will need to know each image file names created, whose filename are generated in test C code at runtime.

If you want a subdirectory for images, you can create it in the test code.

Paolo

#17 lore raw

Re: [Qemu-devel] [PATCH V4 4/5] libqblock test build system

Quoted 6 lines · Paolo Bonzini

Il 27/09/2012 11:56, Wenchao Xia ha scritto:

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+$(LIBQBLOCK_TEST_DIR):

tests/Makefile +2 -0
+    @echo "Make libqblock test directory"
+    mkdir $(LIBQBLOCK_TEST_DIR)
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

You can leave the files in tests/ directly, and avoid this as well.

Having a new directory will make clean easier, otherwise the script
will need to know each image file names created, whose filename are generated in test C code at runtime.

If you want a subdirectory for images, you can create it in the test code.

My original idea is creating and deleting them in C test code, but I
found that there is many "assert" in the code, which may cause the clean step missing. So I think it is simpler to clean them in Makefile, make sure the temporal files are deleted.

Quoted 2 lines

Paolo

Signature · 3 lines
Best Regards

Wenchao Xia
#18 lore raw

Re: [Qemu-devel] [PATCH V4 4/5] libqblock test build system

Il 28/09/2012 05:04, Wenchao Xia ha scritto:

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 178

Quoted 3 lines

+$(LIBQBLOCK_TEST_DIR):

tests/Makefile +2 -0
+    @echo "Make libqblock test directory"
+    mkdir $(LIBQBLOCK_TEST_DIR)

You can leave the files in tests/ directly, and avoid this as well.

Having a new directory will make clean easier, otherwise the script
will need to know each image file names created, whose filename are generated in test C code at runtime.

Quoted 7 lines
Quoted 3 lines

If you want a subdirectory for images, you can create it in the test code.

My original idea is creating and deleting them in C test code, but I
found that there is many "assert" in the code, which may cause the clean step missing. So I think it is simpler to clean them in Makefile, make sure the temporal files are deleted.

Yes, you can mkdir() in the test code, and delete the whole directory from the Makefile.

Paolo

#19 lore raw

Re: [Qemu-devel] [PATCH V4 4/5] libqblock test build system

Quoted 13 lines · Paolo Bonzini

Il 28/09/2012 05:04, Wenchao Xia ha scritto:

Quoted 12 lines
Quoted 12 lines
Quoted 8 lines
Quoted 5 lines

tests/Makefile check-unit: $(patsubst %,check-%, $(check-unit-y)) · line 178

Quoted 3 lines

+$(LIBQBLOCK_TEST_DIR):

tests/Makefile +2 -0
+    @echo "Make libqblock test directory"
+    mkdir $(LIBQBLOCK_TEST_DIR)

You can leave the files in tests/ directly, and avoid this as well.

Having a new directory will make clean easier, otherwise the script
will need to know each image file names created, whose filename are generated in test C code at runtime.

If you want a subdirectory for images, you can create it in the test code.

Quoted 8 lines
Quoted 4 lines

My original idea is creating and deleting them in C test code, but I
found that there is many "assert" in the code, which may cause the clean step missing. So I think it is simpler to clean them in Makefile, make sure the temporal files are deleted.

Yes, you can mkdir() in the test code, and delete the whole directory from the Makefile.

Ok, I got your idea.

Quoted 2 lines

Paolo

Signature · 3 lines
Best Regards

Wenchao Xia
#20 lore raw

PATCH v4 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 |  366 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 365 insertions(+), 1 deletions(-)
tests/libqblock/libqblock-qcow2.c +365 -1
index c05c0c4..7985cb6 100644
@@ -1,4 +1,368 @@
+/*
+ * 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 "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(QBlockProtInfo *loc)
+{
+    if (loc == NULL) {
+        printf("backing file is NULL.");
+        return;
+    }
+    switch (loc->prot_type) {
+    case QB_PROT_NONE:
+        printf("protocol type [none].");
+        break;
+    case QB_PROT_FILE:
+        printf("protocol type [file], filename [%s].",
+               loc->prot_op.o_file.filename);
+        break;
+    default:
+        printf("protocol type not supported.");
+        break;
+    }
+}
+
+static void print_info_image_static(QBlockStaticInfo *info)
+{
+    printf("=======image location:\n");
+    print_loc(&info->loc);
+    printf("\nvirtual_size %" PRId64 ", format type %d [%s]",
+           *(info->member_addr->virt_size),
+           info->fmt.fmt_type, qb_fmttype2str(info->fmt.fmt_type));
+    printf("\nbacking image location:\n");
+    print_loc(info->member_addr->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;
+    QBlockProtInfo *prot_info = NULL;
+    QBlockFmtInfo *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_prot_info_new(context, &prot_info);
+    g_assert(ret == 0);
+
+    ret = qb_fmt_info_new(context, &fmt_info);
+    g_assert(ret == 0);
+
+    prot_info->prot_type = QB_PROT_FILE;
+    fmt_info->fmt_type = QB_FMT_QCOW2;
+    fmt_info->fmt_op.o_qcow2.virt_size = virt_size;
+    flag = 0;
+
+    index = 0;
+    while (index < num_backings) {
+        new_filename = generate_backing_filename(filename, index);
+        prot_info->prot_op.o_file.filename = new_filename;
+        if (backing_filename != NULL) {
+            fmt_info->fmt_op.o_qcow2.backing_loc.prot_type = QB_PROT_FILE;
+            relative_filename = get_filename(backing_filename);
+            fmt_info->fmt_op.o_qcow2.backing_loc.prot_op.o_file.filename =
+                                                             relative_filename;
+        }
+        ret = qb_create(context, qbs, prot_info, fmt_info, flag);
+        g_assert(ret == 0);
+        free(backing_filename);
+        backing_filename = new_filename;
+        new_filename = NULL;
+        index++;
+    }
+
+    prot_info->prot_op.o_file.filename = filename;
+    if (backing_filename != NULL) {
+        fmt_info->fmt_op.o_qcow2.backing_loc.prot_type = QB_PROT_FILE;
+        relative_filename = get_filename(backing_filename);
+        fmt_info->fmt_op.o_qcow2.backing_loc.prot_op.o_file.filename =
+                                                         relative_filename;
+    }
+    ret = qb_create(context, qbs, prot_info, fmt_info, flag);
+    g_assert(ret == 0);
+    free(backing_filename);
+
+    qb_fmt_info_delete(context, &fmt_info);
+    qb_prot_info_delete(context, &prot_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;
+    QBlockProtInfo *prot_info = NULL;
+    int ret, flag;
+    QBlockStaticInfo *info_st = NULL;
+    int i;
+    char *backing_filename = NULL;
+
+    ret = qb_context_new(&context);
+    g_assert(ret == 0);
+
+    ret = qb_state_new(context, &qbs);
+    g_assert(ret == 0);
+
+    ret = qb_prot_info_new(context, &prot_info);
+    g_assert(ret == 0);
+
+    prot_info->prot_type = QB_PROT_FILE;
+    prot_info->prot_op.o_file.filename = filename;
+    flag = LIBQBLOCK_O_NO_BACKING;
+    ret = qb_open(context, qbs, prot_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 == prot_info->prot_type);
+        ret = strcmp(info_st->loc.prot_op.o_file.filename,
+                     prot_info->prot_op.o_file.filename);
+
+        g_assert(ret == 0);
+        g_assert(info_st->fmt.fmt_type == QB_FMT_QCOW2);
+        g_assert(*(info_st->member_addr->virt_size) == image_size);
+        if (i >= 0) {
+            /* it should have backing file */
+            g_assert(info_st->member_addr->backing_loc->prot_type ==
+                                       QB_PROT_FILE);
+            backing_filename = generate_backing_filename(filename, i);
+            ret = strcmp(backing_filename,
+                   info_st->member_addr->backing_loc->prot_op.o_file.filename);
+            g_assert(ret == 0);
+            g_free(backing_filename);
+        } else {
+            g_assert(info_st->member_addr->backing_loc->prot_type ==
+                                       QB_FMT_NONE);
+        }
+        i--;
+        /* see if there is backing file need to check. */
+        if ((info_st->member_addr->backing_loc == NULL) ||
+            (info_st->member_addr->backing_loc->prot_type == QB_FMT_NONE)) {
+            break;
+        }
+        qb_prot_info_delete(context, &prot_info);
+        prot_info = qb_prot_info_dup(info_st->member_addr->backing_loc);
+        ret = qb_open(context, qbs, prot_info, NULL, flag);
+        g_assert(ret == 0);
+        qb_info_image_static_delete(context, &info_st);
+    }
+    qb_info_image_static_delete(context, &info_st);
+
+    qb_prot_info_delete(context, &prot_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;
+    QBlockProtInfo *prot_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_prot_info_new(context, &prot_info);
+    g_assert(ret == 0);
+
+
+    prot_info->prot_type = QB_PROT_FILE;
+    prot_info->prot_op.o_file.filename = filename;
+    flag = LIBQBLOCK_O_RDWR;
+    ret = qb_open(context, qbs, prot_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_prot_info_delete(context, &prot_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;
+    char *filename;
+
+    root_dir = getenv(LIBQB_TEST_ENV_DIR);
+    if (root_dir == NULL) {
+        root_dir = LIBQB_TEST_DEFAULT_DIR;
+    }
+
+    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);
+    return ret;
 }
Signature · 1 line
1.7.1