Patchwork [2/6] libqblock public type defines

login
register
mail settings
Submitter Wayne Xia
Date Sept. 3, 2012, 9:18 a.m.
Message ID <1346663926-20188-3-git-send-email-xiawenc@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/181340/
State New
Headers show

Comments

Wayne Xia - Sept. 3, 2012, 9:18 a.m.
This patch contains public type and defines used in APIs.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 libqblock/libqblock-types.h |  228 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 228 insertions(+), 0 deletions(-)
 create mode 100644 libqblock/libqblock-types.h
Paolo Bonzini - Sept. 3, 2012, 1:13 p.m.
Il 03/09/2012 11:18, Wenchao Xia ha scritto:
> +union QBlockOption_fmt {
> +    struct QBlockOption_fmt_cow       o_cow;
> +    struct QBlockOption_fmt_qed       o_qed;
> +    struct QBlockOption_fmt_qcow      o_qcow;
> +    struct QBlockOption_fmt_qcow2     o_qcow2;
> +    struct QBlockOption_fmt_raw       o_raw;
> +    struct QBlockOption_fmt_rbd       o_rbd;
> +    struct QBlockOption_fmt_sheepdog  o_sheepdog;
> +    struct QBlockOption_fmt_vdi       o_vdi;
> +    struct QBlockOption_fmt_vmdk      o_vmdk;
> +    struct QBlockOption_fmt_vpc       o_vpc;
> +};
> +
> +struct QBlockOptionFormat {
> +    enum QBlockFormat fmt_type;
> +    union QBlockOption_fmt fmt_op;
> +    uint8_t reserved[512];
> +};

Padding must be in the union not the struct.  For the fourth time.

Paolo
Eric Blake - Sept. 3, 2012, 2:20 p.m.
On 09/03/2012 03:18 AM, Wenchao Xia wrote:
>   This patch contains public type and defines used in APIs.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  libqblock/libqblock-types.h |  228 +++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 228 insertions(+), 0 deletions(-)
>  create mode 100644 libqblock/libqblock-types.h
> 
> diff --git a/libqblock/libqblock-types.h b/libqblock/libqblock-types.h
> new file mode 100644
> index 0000000..3389bda
> --- /dev/null
> +++ b/libqblock/libqblock-types.h
> @@ -0,0 +1,228 @@
> +#ifndef LIBQBLOCK_TYPES_H

Missing a copyright header.  Shame.

> +#define LIBQBLOCK_TYPES_H
> +
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <stdlib.h>
> +#include <stdbool.h>

I see use of stdint (uint8_t) and stdbool (bool), but isn't
<sys/types.h> better than <stdio.h> and <stdlib.h> for size_t?

> +
> +/**
> + * QBlockInfoImageStatic: information about the block image.
> + *
> + * @loc: location info.
> + * @fmt_type: format type.
> + * @virt_size: virtual size in bytes.
> + * @backing_loc: backing file location, its type is QB_PROT_NONE if not exist.
> + * @allocated_size: allocated size in bytes, negative if not available.

Reading this...

> + * @encrypt: encrypt flag.
> + */
> +struct QBlockInfoImageStatic {
> +    struct QBlockOptionLoc loc;
> +    enum QBlockFormat fmt_type;
> +    size_t virt_size;
> +    /* advance info */
> +    struct QBlockOptionLoc backing_loc;
> +    size_t allocated_size;

...negative is not possible for size_t.  Did you mean ssize_t?
Blue Swirl - Sept. 3, 2012, 7:31 p.m.
On Mon, Sep 3, 2012 at 9:18 AM, Wenchao Xia <xiawenc@linux.vnet.ibm.com> wrote:
>   This patch contains public type and defines used in APIs.
>
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  libqblock/libqblock-types.h |  228 +++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 228 insertions(+), 0 deletions(-)
>  create mode 100644 libqblock/libqblock-types.h
>
> diff --git a/libqblock/libqblock-types.h b/libqblock/libqblock-types.h
> new file mode 100644
> index 0000000..3389bda
> --- /dev/null
> +++ b/libqblock/libqblock-types.h
> @@ -0,0 +1,228 @@
> +#ifndef LIBQBLOCK_TYPES_H
> +#define LIBQBLOCK_TYPES_H
> +
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <stdlib.h>
> +#include <stdbool.h>
> +
> +/* this library is designed around this core struct. */
> +struct QBlockState;
> +
> +/* every thread would have a broker. */
> +struct QBroker;
> +
> +/* 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)
> +
> +#define LIBQBLOCK_O_VALID_MASK \
> +   (LIBQBLOCK_O_RDWR | LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | \
> +    LIBQBLOCK_O_NO_BACKING | LIBQBLOCK_O_NO_FLUSH)
> +
> +enum QBlockProtocol {
> +    QB_PROTO_NONE = 0,
> +    QB_PROTO_FILE,
> +    QB_PROTO_MAX
> +};
> +
> +enum QBlockFormat {
> +    QB_FMT_NONE = 0,
> +    QB_FMT_COW,
> +    QB_FMT_QED,
> +    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
> +};
> +
> +struct QBlockOption_prot_file {

QBlockOptionProtFile

> +    char *filename;

'const'

> +};
> +
> +union QBlockOption_prot {

QBlockOptionProt

> +    struct QBlockOption_prot_file o_file;
> +};
> +
> +/**
> + * struct QBlockOptionLoc: contains information about how to find the image
> + *
> + * @prot_type: protocol type, now only support FILE.
> + * @prot_op: protocol related options.
> + */
> +struct QBlockOptionLoc {
> +    enum QBlockProtocol prot_type;
> +    union QBlockOption_prot prot_op;
> +    uint8_t reserved[512];
> +};
> +
> +/* format related options */
> +struct QBlockOption_fmt_cow {

QBlockOptionFmtCOW

> +    size_t virt_size;
> +    struct QBlockOptionLoc backing_loc;
> +};
> +
> +struct QBlockOption_fmt_qed {

QBlockOptionFmtQED

etc. for the rest. Don't mix CamelCase with underscore style, struct
names must use CamelCase.

> +    size_t virt_size;
> +    struct QBlockOptionLoc backing_loc;
> +    enum QBlockFormat backing_fmt;
> +    size_t cluster_size; /* unit is bytes */
> +    size_t table_size; /* unit is clusters */
> +};
> +
> +struct QBlockOption_fmt_qcow {
> +    size_t virt_size;
> +    struct QBlockOptionLoc backing_loc;
> +    bool encrypt;
> +};
> +
> +/* "Compatibility level (0.10 or 1.1)" */
> +enum QBlockOption_fmt_qcow2_cpt {
> +    QBO_FMT_QCOW2_CPT_NONE = 0,
> +    QBO_FMT_QCOW2_CPT_V010,
> +    QBO_FMT_QCOW2_CPT_V110,
> +};
> +
> +/* off or metadata */
> +enum QBlockOption_fmt_qcow2_prealloc {
> +    QBO_FMT_QCOW2_PREALLOC_NONE = 0,
> +    QBO_FMT_QCOW2_PREALLOC_OFF,
> +    QBO_FMT_QCOW2_PREALLOC_METADATA,
> +};
> +
> +struct QBlockOption_fmt_qcow2 {
> +    size_t virt_size;
> +    struct QBlockOptionLoc backing_loc;
> +    enum QBlockFormat backing_fmt;
> +    bool encrypt;
> +    size_t cluster_size; /* unit is bytes */
> +    enum QBlockOption_fmt_qcow2_cpt cpt_lv;
> +    enum QBlockOption_fmt_qcow2_prealloc pre_mode;
> +};
> +
> +struct QBlockOption_fmt_raw {
> +    size_t virt_size;
> +};
> +
> +struct QBlockOption_fmt_rbd {
> +    size_t virt_size;
> +    size_t cluster_size;
> +};
> +
> +/* off or full */
> +enum QBlockOption_fmt_sheepdog_prealloc {
> +    QBO_FMT_SD_PREALLOC_NONE = 0,
> +    QBO_FMT_SD_PREALLOC_OFF,
> +    QBO_FMT_SD_PREALLOC_FULL,
> +};
> +
> +struct QBlockOption_fmt_sheepdog {
> +    size_t virt_size;
> +    struct QBlockOptionLoc backing_loc;
> +    enum QBlockOption_fmt_sheepdog_prealloc pre_mode;
> +};
> +
> +enum QBlockOption_fmt_vdi_prealloc {
> +    QBO_FMT_VDI_PREALLOC_NONE = 0,
> +    QBO_FMT_VDI_PREALLOC_FALSE,
> +    QBO_FMT_VDI_PREALLOC_TRUE,
> +};
> +
> +struct QBlockOption_fmt_vdi {
> +    size_t virt_size;
> +    size_t cluster_size;
> +    enum QBlockOption_fmt_vdi_prealloc pre_mode;
> +};
> +
> +/* whether compact to vmdk verion 6 */
> +enum QBlockOption_fmt_vmdk_cpt {
> +    QBO_FMT_VMDK_CPT_NONE = 0,
> +    QBO_FMT_VMDK_CPT_VMDKV6_FALSE,
> +    QBO_FMT_VMDK_CPT_VMDKV6_TRUE,
> +};
> +
> +/* vmdk flat extent format, values:
> +"{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
> +twoGbMaxExtentFlat | streamOptimized} */
> +enum QBlockOption_fmt_vmdk_subfmt {
> +    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE = 0,
> +    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE,
> +    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,
> +};
> +
> +struct QBlockOption_fmt_vmdk {
> +    size_t virt_size;
> +    struct QBlockOptionLoc backing_loc;
> +    enum QBlockOption_fmt_vmdk_cpt cpt_lv;
> +    enum QBlockOption_fmt_vmdk_subfmt subfmt;
> +};
> +
> +/* "{dynamic (default) | fixed} " */
> +enum QBlockOption_fmt_vpc_subfmt {
> +    QBO_FMT_VPC_SUBFMT_NONE = 0,
> +    QBO_FMT_VPC_SUBFMT_DYNAMIC,
> +    QBO_FMT_VPC_SUBFMT_FIXED,
> +};
> +
> +struct QBlockOption_fmt_vpc {
> +    size_t virt_size;
> +    enum QBlockOption_fmt_vpc_subfmt subfmt;
> +};
> +
> +union QBlockOption_fmt {
> +    struct QBlockOption_fmt_cow       o_cow;
> +    struct QBlockOption_fmt_qed       o_qed;
> +    struct QBlockOption_fmt_qcow      o_qcow;
> +    struct QBlockOption_fmt_qcow2     o_qcow2;
> +    struct QBlockOption_fmt_raw       o_raw;
> +    struct QBlockOption_fmt_rbd       o_rbd;
> +    struct QBlockOption_fmt_sheepdog  o_sheepdog;
> +    struct QBlockOption_fmt_vdi       o_vdi;
> +    struct QBlockOption_fmt_vmdk      o_vmdk;
> +    struct QBlockOption_fmt_vpc       o_vpc;
> +};
> +
> +struct QBlockOptionFormat {
> +    enum QBlockFormat fmt_type;
> +    union QBlockOption_fmt fmt_op;
> +    uint8_t reserved[512];
> +};
> +
> +/**
> + * QBlockInfoImageStatic: information about the block image.
> + *
> + * @loc: location info.
> + * @fmt_type: format type.
> + * @virt_size: virtual size in bytes.
> + * @backing_loc: backing file location, its type is QB_PROT_NONE if not exist.
> + * @allocated_size: allocated size in bytes, negative if not available.
> + * @encrypt: encrypt flag.
> + */
> +struct QBlockInfoImageStatic {
> +    struct QBlockOptionLoc loc;
> +    enum QBlockFormat fmt_type;
> +    size_t virt_size;
> +    /* advance info */
> +    struct QBlockOptionLoc backing_loc;
> +    size_t allocated_size;
> +    bool encrypt;
> +};
> +#endif
> --
> 1.7.1
>
>
>
Wayne Xia - Sept. 4, 2012, 2 a.m.
于 2012-9-3 21:13, Paolo Bonzini 写道:
> Il 03/09/2012 11:18, Wenchao Xia ha scritto:
>> +union QBlockOption_fmt {
>> +    struct QBlockOption_fmt_cow       o_cow;
>> +    struct QBlockOption_fmt_qed       o_qed;
>> +    struct QBlockOption_fmt_qcow      o_qcow;
>> +    struct QBlockOption_fmt_qcow2     o_qcow2;
>> +    struct QBlockOption_fmt_raw       o_raw;
>> +    struct QBlockOption_fmt_rbd       o_rbd;
>> +    struct QBlockOption_fmt_sheepdog  o_sheepdog;
>> +    struct QBlockOption_fmt_vdi       o_vdi;
>> +    struct QBlockOption_fmt_vmdk      o_vmdk;
>> +    struct QBlockOption_fmt_vpc       o_vpc;
>> +};
>> +
>> +struct QBlockOptionFormat {
>> +    enum QBlockFormat fmt_type;
>> +    union QBlockOption_fmt fmt_op;
>> +    uint8_t reserved[512];
>> +};
>
> Padding must be in the union not the struct.  For the fourth time.
>
> Paolo
>
   I must have left it in some other patches, sorry about this key point
missing.
Wayne Xia - Sept. 4, 2012, 7:10 a.m.
于 2012-9-3 22:20, Eric Blake 写道:
> On 09/03/2012 03:18 AM, Wenchao Xia wrote:
>>    This patch contains public type and defines used in APIs.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> ---
>>   libqblock/libqblock-types.h |  228 +++++++++++++++++++++++++++++++++++++++++++
>>   1 files changed, 228 insertions(+), 0 deletions(-)
>>   create mode 100644 libqblock/libqblock-types.h
>>
>> diff --git a/libqblock/libqblock-types.h b/libqblock/libqblock-types.h
>> new file mode 100644
>> index 0000000..3389bda
>> --- /dev/null
>> +++ b/libqblock/libqblock-types.h
>> @@ -0,0 +1,228 @@
>> +#ifndef LIBQBLOCK_TYPES_H
>
> Missing a copyright header.  Shame.
>
>> +#define LIBQBLOCK_TYPES_H
>> +
>> +#include <stdio.h>
>> +#include <stdint.h>
>> +#include <stdlib.h>
>> +#include <stdbool.h>
>
> I see use of stdint (uint8_t) and stdbool (bool), but isn't
> <sys/types.h> better than <stdio.h> and <stdlib.h> for size_t?
>
   right, thanks.

>> +
>> +/**
>> + * QBlockInfoImageStatic: information about the block image.
>> + *
>> + * @loc: location info.
>> + * @fmt_type: format type.
>> + * @virt_size: virtual size in bytes.
>> + * @backing_loc: backing file location, its type is QB_PROT_NONE if not exist.
>> + * @allocated_size: allocated size in bytes, negative if not available.
>
> Reading this...
>
>> + * @encrypt: encrypt flag.
>> + */
>> +struct QBlockInfoImageStatic {
>> +    struct QBlockOptionLoc loc;
>> +    enum QBlockFormat fmt_type;
>> +    size_t virt_size;
>> +    /* advance info */
>> +    struct QBlockOptionLoc backing_loc;
>> +    size_t allocated_size;
>
> ...negative is not possible for size_t.  Did you mean ssize_t?
>
   you are right.
Wayne Xia - Sept. 4, 2012, 7:19 a.m.
> On Mon, Sep 3, 2012 at 9:18 AM, Wenchao Xia <xiawenc@linux.vnet.ibm.com> wrote:
>>    This patch contains public type and defines used in APIs.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> ---
>>   libqblock/libqblock-types.h |  228 +++++++++++++++++++++++++++++++++++++++++++
>>   1 files changed, 228 insertions(+), 0 deletions(-)
>>   create mode 100644 libqblock/libqblock-types.h
>>
>> diff --git a/libqblock/libqblock-types.h b/libqblock/libqblock-types.h
>> new file mode 100644
>> index 0000000..3389bda
>> --- /dev/null
>> +++ b/libqblock/libqblock-types.h
>> @@ -0,0 +1,228 @@
>> +#ifndef LIBQBLOCK_TYPES_H
>> +#define LIBQBLOCK_TYPES_H
>> +
>> +#include <stdio.h>
>> +#include <stdint.h>
>> +#include <stdlib.h>
>> +#include <stdbool.h>
>> +
>> +/* this library is designed around this core struct. */
>> +struct QBlockState;
>> +
>> +/* every thread would have a broker. */
>> +struct QBroker;
>> +
>> +/* 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)
>> +
>> +#define LIBQBLOCK_O_VALID_MASK \
>> +   (LIBQBLOCK_O_RDWR | LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | \
>> +    LIBQBLOCK_O_NO_BACKING | LIBQBLOCK_O_NO_FLUSH)
>> +
>> +enum QBlockProtocol {
>> +    QB_PROTO_NONE = 0,
>> +    QB_PROTO_FILE,
>> +    QB_PROTO_MAX
>> +};
>> +
>> +enum QBlockFormat {
>> +    QB_FMT_NONE = 0,
>> +    QB_FMT_COW,
>> +    QB_FMT_QED,
>> +    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
>> +};
>> +
>> +struct QBlockOption_prot_file {
>
> QBlockOptionProtFile
>
>> +    char *filename;
>
> 'const'
>
   There is a problem, this member would be used in information
retrieving, so it will be set to a pointer to a string allocated
at runtime, and later be freed. I am not sure if const fits for this
situation, let me check.

>> +};
>> +
>> +union QBlockOption_prot {
>
> QBlockOptionProt
>
>> +    struct QBlockOption_prot_file o_file;
>> +};
>> +
>> +/**
>> + * struct QBlockOptionLoc: contains information about how to find the image
>> + *
>> + * @prot_type: protocol type, now only support FILE.
>> + * @prot_op: protocol related options.
>> + */
>> +struct QBlockOptionLoc {
>> +    enum QBlockProtocol prot_type;
>> +    union QBlockOption_prot prot_op;
>> +    uint8_t reserved[512];
>> +};
>> +
>> +/* format related options */
>> +struct QBlockOption_fmt_cow {
>
> QBlockOptionFmtCOW
>
>> +    size_t virt_size;
>> +    struct QBlockOptionLoc backing_loc;
>> +};
>> +
>> +struct QBlockOption_fmt_qed {
>
> QBlockOptionFmtQED
>
> etc. for the rest. Don't mix CamelCase with underscore style, struct
> names must use CamelCase.
>
>> +    size_t virt_size;
>> +    struct QBlockOptionLoc backing_loc;
>> +    enum QBlockFormat backing_fmt;
>> +    size_t cluster_size; /* unit is bytes */
>> +    size_t table_size; /* unit is clusters */
>> +};
>> +
>> +struct QBlockOption_fmt_qcow {
>> +    size_t virt_size;
>> +    struct QBlockOptionLoc backing_loc;
>> +    bool encrypt;
>> +};
>> +
>> +/* "Compatibility level (0.10 or 1.1)" */
>> +enum QBlockOption_fmt_qcow2_cpt {
>> +    QBO_FMT_QCOW2_CPT_NONE = 0,
>> +    QBO_FMT_QCOW2_CPT_V010,
>> +    QBO_FMT_QCOW2_CPT_V110,
>> +};
>> +
>> +/* off or metadata */
>> +enum QBlockOption_fmt_qcow2_prealloc {
>> +    QBO_FMT_QCOW2_PREALLOC_NONE = 0,
>> +    QBO_FMT_QCOW2_PREALLOC_OFF,
>> +    QBO_FMT_QCOW2_PREALLOC_METADATA,
>> +};
>> +
>> +struct QBlockOption_fmt_qcow2 {
>> +    size_t virt_size;
>> +    struct QBlockOptionLoc backing_loc;
>> +    enum QBlockFormat backing_fmt;
>> +    bool encrypt;
>> +    size_t cluster_size; /* unit is bytes */
>> +    enum QBlockOption_fmt_qcow2_cpt cpt_lv;
>> +    enum QBlockOption_fmt_qcow2_prealloc pre_mode;
>> +};
>> +
>> +struct QBlockOption_fmt_raw {
>> +    size_t virt_size;
>> +};
>> +
>> +struct QBlockOption_fmt_rbd {
>> +    size_t virt_size;
>> +    size_t cluster_size;
>> +};
>> +
>> +/* off or full */
>> +enum QBlockOption_fmt_sheepdog_prealloc {
>> +    QBO_FMT_SD_PREALLOC_NONE = 0,
>> +    QBO_FMT_SD_PREALLOC_OFF,
>> +    QBO_FMT_SD_PREALLOC_FULL,
>> +};
>> +
>> +struct QBlockOption_fmt_sheepdog {
>> +    size_t virt_size;
>> +    struct QBlockOptionLoc backing_loc;
>> +    enum QBlockOption_fmt_sheepdog_prealloc pre_mode;
>> +};
>> +
>> +enum QBlockOption_fmt_vdi_prealloc {
>> +    QBO_FMT_VDI_PREALLOC_NONE = 0,
>> +    QBO_FMT_VDI_PREALLOC_FALSE,
>> +    QBO_FMT_VDI_PREALLOC_TRUE,
>> +};
>> +
>> +struct QBlockOption_fmt_vdi {
>> +    size_t virt_size;
>> +    size_t cluster_size;
>> +    enum QBlockOption_fmt_vdi_prealloc pre_mode;
>> +};
>> +
>> +/* whether compact to vmdk verion 6 */
>> +enum QBlockOption_fmt_vmdk_cpt {
>> +    QBO_FMT_VMDK_CPT_NONE = 0,
>> +    QBO_FMT_VMDK_CPT_VMDKV6_FALSE,
>> +    QBO_FMT_VMDK_CPT_VMDKV6_TRUE,
>> +};
>> +
>> +/* vmdk flat extent format, values:
>> +"{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
>> +twoGbMaxExtentFlat | streamOptimized} */
>> +enum QBlockOption_fmt_vmdk_subfmt {
>> +    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE = 0,
>> +    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE,
>> +    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,
>> +};
>> +
>> +struct QBlockOption_fmt_vmdk {
>> +    size_t virt_size;
>> +    struct QBlockOptionLoc backing_loc;
>> +    enum QBlockOption_fmt_vmdk_cpt cpt_lv;
>> +    enum QBlockOption_fmt_vmdk_subfmt subfmt;
>> +};
>> +
>> +/* "{dynamic (default) | fixed} " */
>> +enum QBlockOption_fmt_vpc_subfmt {
>> +    QBO_FMT_VPC_SUBFMT_NONE = 0,
>> +    QBO_FMT_VPC_SUBFMT_DYNAMIC,
>> +    QBO_FMT_VPC_SUBFMT_FIXED,
>> +};
>> +
>> +struct QBlockOption_fmt_vpc {
>> +    size_t virt_size;
>> +    enum QBlockOption_fmt_vpc_subfmt subfmt;
>> +};
>> +
>> +union QBlockOption_fmt {
>> +    struct QBlockOption_fmt_cow       o_cow;
>> +    struct QBlockOption_fmt_qed       o_qed;
>> +    struct QBlockOption_fmt_qcow      o_qcow;
>> +    struct QBlockOption_fmt_qcow2     o_qcow2;
>> +    struct QBlockOption_fmt_raw       o_raw;
>> +    struct QBlockOption_fmt_rbd       o_rbd;
>> +    struct QBlockOption_fmt_sheepdog  o_sheepdog;
>> +    struct QBlockOption_fmt_vdi       o_vdi;
>> +    struct QBlockOption_fmt_vmdk      o_vmdk;
>> +    struct QBlockOption_fmt_vpc       o_vpc;
>> +};
>> +
>> +struct QBlockOptionFormat {
>> +    enum QBlockFormat fmt_type;
>> +    union QBlockOption_fmt fmt_op;
>> +    uint8_t reserved[512];
>> +};
>> +
>> +/**
>> + * QBlockInfoImageStatic: information about the block image.
>> + *
>> + * @loc: location info.
>> + * @fmt_type: format type.
>> + * @virt_size: virtual size in bytes.
>> + * @backing_loc: backing file location, its type is QB_PROT_NONE if not exist.
>> + * @allocated_size: allocated size in bytes, negative if not available.
>> + * @encrypt: encrypt flag.
>> + */
>> +struct QBlockInfoImageStatic {
>> +    struct QBlockOptionLoc loc;
>> +    enum QBlockFormat fmt_type;
>> +    size_t virt_size;
>> +    /* advance info */
>> +    struct QBlockOptionLoc backing_loc;
>> +    size_t allocated_size;
>> +    bool encrypt;
>> +};
>> +#endif
>> --
>> 1.7.1
>>
>>
>>
>
Paolo Bonzini - Sept. 4, 2012, 7:37 a.m.
Il 04/09/2012 09:10, Wenchao Xia ha scritto:
> 
>>> +
>>> +/**
>>> + * QBlockInfoImageStatic: information about the block image.
>>> + *
>>> + * @loc: location info.
>>> + * @fmt_type: format type.
>>> + * @virt_size: virtual size in bytes.
>>> + * @backing_loc: backing file location, its type is QB_PROT_NONE if
>>> not exist.
>>> + * @allocated_size: allocated size in bytes, negative if not available.
>>
>> Reading this...
>>
>>> + * @encrypt: encrypt flag.
>>> + */
>>> +struct QBlockInfoImageStatic {
>>> +    struct QBlockOptionLoc loc;
>>> +    enum QBlockFormat fmt_type;
>>> +    size_t virt_size;
>>> +    /* advance info */
>>> +    struct QBlockOptionLoc backing_loc;
>>> +    size_t allocated_size;
>>
>> ...negative is not possible for size_t.  Did you mean ssize_t?
>>
>   you are right.

size_t is a *pointer* size.  It is 32-bits on

Use int64_t or uint64_t always when dealing with file sizes and offsets.

In particular, do not use off_t, it is a recipe for pain and ABI
incompatibilities.

Paolo
Paolo Bonzini - Sept. 4, 2012, 7:38 a.m.
Il 04/09/2012 09:19, Wenchao Xia ha scritto:
>>>
>>> +struct QBlockOption_prot_file {
>>
>> QBlockOptionProtFile
>>
>>> +    char *filename;
>>
>> 'const'
>>
>   There is a problem, this member would be used in information
> retrieving, so it will be set to a pointer to a string allocated
> at runtime, and later be freed. I am not sure if const fits for this
> situation, let me check.

No, const would add useless complication.  In C++ it is different
because you have constructors.

Paolo
Blue Swirl - Sept. 4, 2012, 7:22 p.m.
On Tue, Sep 4, 2012 at 7:38 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 04/09/2012 09:19, Wenchao Xia ha scritto:
>>>>
>>>> +struct QBlockOption_prot_file {
>>>
>>> QBlockOptionProtFile
>>>
>>>> +    char *filename;
>>>
>>> 'const'
>>>
>>   There is a problem, this member would be used in information
>> retrieving, so it will be set to a pointer to a string allocated
>> at runtime, and later be freed. I am not sure if const fits for this
>> situation, let me check.
>
> No, const would add useless complication.  In C++ it is different
> because you have constructors.

Using 'const' would mean that it's also possible for the client to use
constant strings without casts which remove 'const' qualifier. This
will be important if the client code is compiled with -Wcast-qual.
QEMU does not use it ATM, but we also don't control what compiler
flags are used for the client.

'const' also tells the developer that the library will not modify or
free the string, so there's documentation value.

>
> Paolo
Wayne Xia - Sept. 10, 2012, 8:22 a.m.
changed to const, thanks.

> On Tue, Sep 4, 2012 at 7:38 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> Il 04/09/2012 09:19, Wenchao Xia ha scritto:
>>>>>
>>>>> +struct QBlockOption_prot_file {
>>>>
>>>> QBlockOptionProtFile
>>>>
>>>>> +    char *filename;
>>>>
>>>> 'const'
>>>>
>>>    There is a problem, this member would be used in information
>>> retrieving, so it will be set to a pointer to a string allocated
>>> at runtime, and later be freed. I am not sure if const fits for this
>>> situation, let me check.
>>
>> No, const would add useless complication.  In C++ it is different
>> because you have constructors.
>
> Using 'const' would mean that it's also possible for the client to use
> constant strings without casts which remove 'const' qualifier. This
> will be important if the client code is compiled with -Wcast-qual.
> QEMU does not use it ATM, but we also don't control what compiler
> flags are used for the client.
>
> 'const' also tells the developer that the library will not modify or
> free the string, so there's documentation value.
>
>>
>> Paolo
>

Patch

diff --git a/libqblock/libqblock-types.h b/libqblock/libqblock-types.h
new file mode 100644
index 0000000..3389bda
--- /dev/null
+++ b/libqblock/libqblock-types.h
@@ -0,0 +1,228 @@ 
+#ifndef LIBQBLOCK_TYPES_H
+#define LIBQBLOCK_TYPES_H
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/* every thread would have a broker. */
+struct QBroker;
+
+/* 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)
+
+#define LIBQBLOCK_O_VALID_MASK \
+   (LIBQBLOCK_O_RDWR | LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | \
+    LIBQBLOCK_O_NO_BACKING | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_NONE = 0,
+    QB_PROTO_FILE,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,
+    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
+};
+
+struct QBlockOption_prot_file {
+    char *filename;
+};
+
+union QBlockOption_prot {
+    struct QBlockOption_prot_file o_file;
+};
+
+/**
+ * struct QBlockOptionLoc: contains information about how to find the image
+ *
+ * @prot_type: protocol type, now only support FILE.
+ * @prot_op: protocol related options.
+ */
+struct QBlockOptionLoc {
+    enum QBlockProtocol prot_type;
+    union QBlockOption_prot prot_op;
+    uint8_t reserved[512];
+};
+
+/* format related options */
+struct QBlockOption_fmt_cow {
+    size_t virt_size;
+    struct QBlockOptionLoc backing_loc;
+};
+
+struct QBlockOption_fmt_qed {
+    size_t virt_size;
+    struct QBlockOptionLoc backing_loc;
+    enum QBlockFormat backing_fmt;
+    size_t cluster_size; /* unit is bytes */
+    size_t table_size; /* unit is clusters */
+};
+
+struct QBlockOption_fmt_qcow {
+    size_t virt_size;
+    struct QBlockOptionLoc backing_loc;
+    bool encrypt;
+};
+
+/* "Compatibility level (0.10 or 1.1)" */
+enum QBlockOption_fmt_qcow2_cpt {
+    QBO_FMT_QCOW2_CPT_NONE = 0,
+    QBO_FMT_QCOW2_CPT_V010,
+    QBO_FMT_QCOW2_CPT_V110,
+};
+
+/* off or metadata */
+enum QBlockOption_fmt_qcow2_prealloc {
+    QBO_FMT_QCOW2_PREALLOC_NONE = 0,
+    QBO_FMT_QCOW2_PREALLOC_OFF,
+    QBO_FMT_QCOW2_PREALLOC_METADATA,
+};
+
+struct QBlockOption_fmt_qcow2 {
+    size_t virt_size;
+    struct QBlockOptionLoc backing_loc;
+    enum QBlockFormat backing_fmt;
+    bool encrypt;
+    size_t cluster_size; /* unit is bytes */
+    enum QBlockOption_fmt_qcow2_cpt cpt_lv;
+    enum QBlockOption_fmt_qcow2_prealloc pre_mode;
+};
+
+struct QBlockOption_fmt_raw {
+    size_t virt_size;
+};
+
+struct QBlockOption_fmt_rbd {
+    size_t virt_size;
+    size_t cluster_size;
+};
+
+/* off or full */
+enum QBlockOption_fmt_sheepdog_prealloc {
+    QBO_FMT_SD_PREALLOC_NONE = 0,
+    QBO_FMT_SD_PREALLOC_OFF,
+    QBO_FMT_SD_PREALLOC_FULL,
+};
+
+struct QBlockOption_fmt_sheepdog {
+    size_t virt_size;
+    struct QBlockOptionLoc backing_loc;
+    enum QBlockOption_fmt_sheepdog_prealloc pre_mode;
+};
+
+enum QBlockOption_fmt_vdi_prealloc {
+    QBO_FMT_VDI_PREALLOC_NONE = 0,
+    QBO_FMT_VDI_PREALLOC_FALSE,
+    QBO_FMT_VDI_PREALLOC_TRUE,
+};
+
+struct QBlockOption_fmt_vdi {
+    size_t virt_size;
+    size_t cluster_size;
+    enum QBlockOption_fmt_vdi_prealloc pre_mode;
+};
+
+/* whether compact to vmdk verion 6 */
+enum QBlockOption_fmt_vmdk_cpt {
+    QBO_FMT_VMDK_CPT_NONE = 0,
+    QBO_FMT_VMDK_CPT_VMDKV6_FALSE,
+    QBO_FMT_VMDK_CPT_VMDKV6_TRUE,
+};
+
+/* vmdk flat extent format, values:
+"{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+twoGbMaxExtentFlat | streamOptimized} */
+enum QBlockOption_fmt_vmdk_subfmt {
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_NONE = 0,
+    QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE,
+    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,
+};
+
+struct QBlockOption_fmt_vmdk {
+    size_t virt_size;
+    struct QBlockOptionLoc backing_loc;
+    enum QBlockOption_fmt_vmdk_cpt cpt_lv;
+    enum QBlockOption_fmt_vmdk_subfmt subfmt;
+};
+
+/* "{dynamic (default) | fixed} " */
+enum QBlockOption_fmt_vpc_subfmt {
+    QBO_FMT_VPC_SUBFMT_NONE = 0,
+    QBO_FMT_VPC_SUBFMT_DYNAMIC,
+    QBO_FMT_VPC_SUBFMT_FIXED,
+};
+
+struct QBlockOption_fmt_vpc {
+    size_t virt_size;
+    enum QBlockOption_fmt_vpc_subfmt subfmt;
+};
+
+union QBlockOption_fmt {
+    struct QBlockOption_fmt_cow       o_cow;
+    struct QBlockOption_fmt_qed       o_qed;
+    struct QBlockOption_fmt_qcow      o_qcow;
+    struct QBlockOption_fmt_qcow2     o_qcow2;
+    struct QBlockOption_fmt_raw       o_raw;
+    struct QBlockOption_fmt_rbd       o_rbd;
+    struct QBlockOption_fmt_sheepdog  o_sheepdog;
+    struct QBlockOption_fmt_vdi       o_vdi;
+    struct QBlockOption_fmt_vmdk      o_vmdk;
+    struct QBlockOption_fmt_vpc       o_vpc;
+};
+
+struct QBlockOptionFormat {
+    enum QBlockFormat fmt_type;
+    union QBlockOption_fmt fmt_op;
+    uint8_t reserved[512];
+};
+
+/**
+ * QBlockInfoImageStatic: information about the block image.
+ *
+ * @loc: location info.
+ * @fmt_type: format type.
+ * @virt_size: virtual size in bytes.
+ * @backing_loc: backing file location, its type is QB_PROT_NONE if not exist.
+ * @allocated_size: allocated size in bytes, negative if not available.
+ * @encrypt: encrypt flag.
+ */
+struct QBlockInfoImageStatic {
+    struct QBlockOptionLoc loc;
+    enum QBlockFormat fmt_type;
+    size_t virt_size;
+    /* advance info */
+    struct QBlockOptionLoc backing_loc;
+    size_t allocated_size;
+    bool encrypt;
+};
+#endif