Skip to contentSkip to conversation

libqblock qemu block layer library

PATCH Superseded Wenchao Xia 3 participants 11 messages Oct 7, 2012 – Oct 11, 2012 Kbuild

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover libqblock qemu block layer library

From: Wenchao Xia <[email protected]>

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.

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

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       |  252 ++++++++
 libqblock/libqblock.c             | 1191 +++++++++++++++++++++++++++++++++++++
 libqblock/libqblock.h             |  341 +++++++++++
 libqblock/libqblock.pc.in         |   13 +
 tests/Makefile                    |   36 ++-
 tests/libqblock/libqblock-qcow2.c |  392 ++++++++++++
 14 files changed, 2492 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
#1 lore raw

PATCH v6 1/5 libqblock build system

From: Wenchao Xia <[email protected]>

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 a9c22bf..53006c1 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
#2 lore raw

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

Quoted 29 lines · Wenchao Xia

From: Wenchao Xia <[email protected]>

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 +0 -0
index a9c22bf..53006c1 100644

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

Quoted 7 lines
Makefile +4 -0
@@ -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,)

Does not work for out of tree build:

$ make -C obj-amd64 libqblock.la
make: Entering directory `/src/qemu/obj-amd64'
make: Entering an unknown directory
make: *** libqblock: No such file or directory.  Stop.
make: Leaving an unknown directory
make: *** [libqblock.la] Error 2
make: Leaving directory `/src/qemu/obj-amd64'

If I fake what 'configure' should be doing and add the directory and link to Makefile, there are still problems:

$ make -C obj-amd64 libqblock.la
make: Entering directory `/src/qemu/obj-amd64'
make[1]: *** No rule to make target `libqblock.lo', needed by
`libqblock.la'.  Stop.
make: *** [libqblock.la] Error 2

I think the library should be built by default, like vscclient which probably nobody uses.

Quoted 137 lines
Makefile +6 -1
+
+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
new file mode 100644
index 0000000..cd5f4c1
@@ -0,0 +1,72 @@
+###########################################################################
+# libqblock Makefile
+# Todo:
+#    1 trace related files is generated in this directory, move
+#  them to the root directory.
+##########################################################################
+-include ../config-host.mak
+-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)
+
+#library objects
+libqblock-y=libqblock.o libqblock-error.o
+tools-obj-y =  $(oslib-obj-y) $(trace-obj-y) qemu-tool.o qemu-timer.o \
+       qemu-timer-common.o main-loop.o notify.o \
+       iohandler.o cutils.o iov.o async.o
+tools-obj-$(CONFIG_POSIX) += compatfd.o
+
+
+QEMU_OBJS= $(libqblock-y) $(block-obj-y) $(tools-obj-y)
+QEMU_OBJS_LIB=$(patsubst %.o, %.lo, $(QEMU_OBJS))
+
+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)
+
+#header files to be installed
+libqblock-pub-headers=libqblock.h libqblock-types.h libqblock-error.h
+libqblock_includedir=$(includedir)/qblock
+libqblock_srcpath=$(SRC_PATH)/libqblock
+
+#################################################################
+# Runtime rules
+#################################################################
+clean:
+       rm -f *.lo *.o *.d *.la libqblock-test trace.c trace.c-timestamp *.pc
+       rm -rf .libs block trace audio fsdev hw net qapi qga qom slirp ui
+
+all: libqblock.la libqblock.pc
+# Dummy command so that make thinks it has done something
+       @true
+
+libqblock.la: $(QEMU_OBJS_LIB)
+       $(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -rpath $(libdir) -o $@ $^ $(LIBS),"  lt LINK $@")
+
+libqblock.pc: $(libqblock_srcpath)/libqblock.pc.in
+       $(call quiet-command,sed -e 's|@LIBDIR@|$(libdir)|' \
+               -e 's|@INCLUDEDIR@|$(libqblock_includedir)|' \
+           -e 's|@VERSION@|$(shell cat $(SRC_PATH)/VERSION)|' \
+               -e 's|@PREFIX@|$(prefix)|' \
+               < $(libqblock_srcpath)/libqblock.pc.in > libqblock.pc,\
+       "  GEN   $@")
+
+.PHONY: install-libqblock
+
+install-libqblock: libqblock.la libqblock.pc
+       $(INSTALL_DIR) "$(DESTDIR)$(libdir)"
+       $(INSTALL_DIR) "$(DESTDIR)$(libdir)/pkgconfig"
+       $(INSTALL_DIR) "$(DESTDIR)$(libqblock_includedir)"
+       $(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.la "$(DESTDIR)$(libdir)"
+       $(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.pc "$(DESTDIR)$(libdir)/pkgconfig"
+       $(LIBTOOL) --mode=install $(INSTALL_DATA) $(libqblock-pub-headers) "$(DESTDIR)$(libqblock_includedir)"
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 -1
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}
--

1.7.1

#3 lore raw

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

Quoted 48 lines · Wenchao Xia
Quoted 29 lines

From: Wenchao Xia <[email protected]>

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 +0 -0
index a9c22bf..53006c1 100644

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

Quoted 7 lines
Makefile +4 -0
@@ -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,)

Does not work for out of tree build:

$ make -C obj-amd64 libqblock.la
make: Entering directory `/src/qemu/obj-amd64'
make: Entering an unknown directory
make: *** libqblock: No such file or directory.  Stop.
make: Leaving an unknown directory
make: *** [libqblock.la] Error 2
make: Leaving directory `/src/qemu/obj-amd64'
Quoted 8 lines

If I fake what 'configure' should be doing and add the directory and link to Makefile, there are still problems:

$ make -C obj-amd64 libqblock.la
make: Entering directory `/src/qemu/obj-amd64'
make[1]: *** No rule to make target `libqblock.lo', needed by
`libqblock.la'.  Stop.
make: *** [libqblock.la] Error 2

Could u give more tip about this error? what is obj-amd64?
My understanding is you tried to build the library from another directory similar as:
/extend_disk/xiawenc/qemu/qemu have the qemu source code, then [xiawenc@RedHat62GAWSWenchao xiawenc]$ pwd /extend_disk/xiawenc
[xiawenc@RedHat62GAWSWenchao xiawenc]$ make -C qemu/qemu libqblock.la failed.
Please correct me.

Quoted 3 lines

I think the library should be built by default, like vscclient which probably nobody uses.

libvirt can't link to it now for OOM issue, but I will wrapper the
APIs with RPC later.

Quoted 137 lines
Quoted 137 lines
Makefile +6 -1
+
+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
new file mode 100644
index 0000000..cd5f4c1
@@ -0,0 +1,72 @@
+###########################################################################
+# libqblock Makefile
+# Todo:
+#    1 trace related files is generated in this directory, move
+#  them to the root directory.
+##########################################################################
+-include ../config-host.mak
+-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)
+
+#library objects
+libqblock-y=libqblock.o libqblock-error.o
+tools-obj-y =  $(oslib-obj-y) $(trace-obj-y) qemu-tool.o qemu-timer.o \
+       qemu-timer-common.o main-loop.o notify.o \
+       iohandler.o cutils.o iov.o async.o
+tools-obj-$(CONFIG_POSIX) += compatfd.o
+
+
+QEMU_OBJS= $(libqblock-y) $(block-obj-y) $(tools-obj-y)
+QEMU_OBJS_LIB=$(patsubst %.o, %.lo, $(QEMU_OBJS))
+
+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)
+
+#header files to be installed
+libqblock-pub-headers=libqblock.h libqblock-types.h libqblock-error.h
+libqblock_includedir=$(includedir)/qblock
+libqblock_srcpath=$(SRC_PATH)/libqblock
+
+#################################################################
+# Runtime rules
+#################################################################
+clean:
+       rm -f *.lo *.o *.d *.la libqblock-test trace.c trace.c-timestamp *.pc
+       rm -rf .libs block trace audio fsdev hw net qapi qga qom slirp ui
+
+all: libqblock.la libqblock.pc
+# Dummy command so that make thinks it has done something
+       @true
+
+libqblock.la: $(QEMU_OBJS_LIB)
+       $(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -rpath $(libdir) -o $@ $^ $(LIBS),"  lt LINK $@")
+
+libqblock.pc: $(libqblock_srcpath)/libqblock.pc.in
+       $(call quiet-command,sed -e 's|@LIBDIR@|$(libdir)|' \
+               -e 's|@INCLUDEDIR@|$(libqblock_includedir)|' \
+           -e 's|@VERSION@|$(shell cat $(SRC_PATH)/VERSION)|' \
+               -e 's|@PREFIX@|$(prefix)|' \
+               < $(libqblock_srcpath)/libqblock.pc.in > libqblock.pc,\
+       "  GEN   $@")
+
+.PHONY: install-libqblock
+
+install-libqblock: libqblock.la libqblock.pc
+       $(INSTALL_DIR) "$(DESTDIR)$(libdir)"
+       $(INSTALL_DIR) "$(DESTDIR)$(libdir)/pkgconfig"
+       $(INSTALL_DIR) "$(DESTDIR)$(libqblock_includedir)"
+       $(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.la "$(DESTDIR)$(libdir)"
+       $(LIBTOOL) --mode=install $(INSTALL_DATA) libqblock.pc "$(DESTDIR)$(libdir)/pkgconfig"
+       $(LIBTOOL) --mode=install $(INSTALL_DATA) $(libqblock-pub-headers) "$(DESTDIR)$(libqblock_includedir)"
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 -1
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}
--

1.7.1

#4 lore raw

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

Quoted 33 lines
Quoted 15 lines
Quoted 5 lines
Makefile +4 -0
+######################################################################
+# Support building shared library libqblock
+libqblock.la:
+       $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libqblock

V="$(V)" TARGET_DIR="$*/" libqblock.la,)

Does not work for out of tree build:

$ make -C obj-amd64 libqblock.la
make: Entering directory `/src/qemu/obj-amd64'
make: Entering an unknown directory
make: *** libqblock: No such file or directory.  Stop.
make: Leaving an unknown directory
make: *** [libqblock.la] Error 2
make: Leaving directory `/src/qemu/obj-amd64'
Quoted 8 lines

If I fake what 'configure' should be doing and add the directory and link to Makefile, there are still problems:

$ make -C obj-amd64 libqblock.la
make: Entering directory `/src/qemu/obj-amd64'
make[1]: *** No rule to make target `libqblock.lo', needed by
`libqblock.la'.  Stop.
make: *** [libqblock.la] Error 2

Could u give more tip about this error? what is obj-amd64?
My understanding is you tried to build the library from another directory similar as:
/extend_disk/xiawenc/qemu/qemu have the qemu source code, then [xiawenc@RedHat62GAWSWenchao xiawenc]$ pwd /extend_disk/xiawenc
[xiawenc@RedHat62GAWSWenchao xiawenc]$ make -C qemu/qemu libqblock.la failed.
Please correct me.

Quoted 6 lines
Quoted 3 lines

I think the library should be built by default, like vscclient which probably nobody uses.

libvirt can't link to it now for OOM issue, but I will wrapper the
APIs with RPC later.

I can't reproduce the issue, hope you can show how to make it happen.

#5 lore raw

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

Il 09/10/2012 02:01, wenchao xia ha scritto:

Quoted 10 lines
Quoted 1 line

Could u give more tip about this error? what is obj-amd64?
My understanding is you tried to build the library from another directory similar as:
/extend_disk/xiawenc/qemu/qemu have the qemu source code, then [xiawenc@RedHat62GAWSWenchao xiawenc]$ pwd /extend_disk/xiawenc
[xiawenc@RedHat62GAWSWenchao xiawenc]$ make -C qemu/qemu libqblock.la failed.
Please correct me.

mkdir obj-amd64
cd obj-amd64
../configure
make

Paolo

#6 lore raw

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

Quoted 12 lines · Paolo Bonzini

Il 09/10/2012 02:01, wenchao xia ha scritto:

Quoted 10 lines
Quoted 1 line

Could u give more tip about this error? what is obj-amd64?
My understanding is you tried to build the library from another directory similar as:
/extend_disk/xiawenc/qemu/qemu have the qemu source code, then [xiawenc@RedHat62GAWSWenchao xiawenc]$ pwd /extend_disk/xiawenc
[xiawenc@RedHat62GAWSWenchao xiawenc]$ make -C qemu/qemu libqblock.la failed.
Please correct me.

Quoted 7 lines

mkdir obj-amd64
cd obj-amd64
../configure
make

Paolo

Thank you for the declaration, I'll fix it.

#7 lore raw

PATCH v6 2/5 libqblock type defines

In reply to Wenchao Xia

From: Wenchao Xia <[email protected]>

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    |  252 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 327 insertions(+), 0 deletions(-)
libqblock/libqblock-internal.h +75 -0
index e69de29..75c0452 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.
+*/
+
+typedef struct QBlockStaticInfoAddr {
+    uint64_t *virt_size;
+    QBlockLocationInfo *backing_loc;
+    bool *encrypt;
+} QBlockStaticInfoAddr;
+
+#define G_LIBQBLOCK_ERROR g_libqbock_error_quark()
+
+static inline GQuark g_libqbock_error_quark(void)
+{
+    return g_quark_from_static_string("g-libqblock-error-quark");
+}
+#endif
libqblock/libqblock-types.h +252 -0

252 lines not on this page · raw message

Signature · 1 line
1.7.1
#8 lore raw

PATCH v6 3/5 libqblock API

In reply to Wenchao Xia

From: Wenchao Xia <[email protected]>

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

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

57 lines not on this page · raw message

libqblock/libqblock-error.h +49 -0

49 lines not on this page · raw message

libqblock/libqblock.c +1191 -0

1,191 lines not on this page · raw message

libqblock/libqblock.h +341 -0

341 lines not on this page · raw message

Signature · 1 line
1.7.1
#9 lore raw

PATCH v6 4/5 libqblock test build system

In reply to Wenchao Xia

From: Wenchao Xia <[email protected]>

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 bd6ba1c..76207fe 100644
@@ -93,3 +93,5 @@ cscope.*
 tags
 TAGS
 *~
+tests/libqblock/check-*
+tests/libqblock/test_images
Makefile +1 -1
index 53006c1..68e3579 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
#10 lore raw

PATCH v6 5/5 libqblock test example

In reply to Wenchao Xia

From: Wenchao Xia <[email protected]>

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